-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
543 lines (483 loc) · 14.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
const MAX_CACHE_TIMEOUT = '60000';
const DEFAULT_MAX_RETRIES = '3';
export type ToolRunAsistantHint = { assistant_hint: string };
export type ToolRunSuccess<TResult> = ToolRunAsistantHint & {
status: 'success';
result: TResult;
};
/**
* Tool run failed
*
* The tool run ended with an error.
*/
export type ToolRunError = ToolRunAsistantHint & {
status: 'error';
error: string;
};
/**
* Tool run requires action
*
* The response requires an action from the user.
* For example, the user may need to connect (authenticate) an 3rd party account.
*/
export type ToolRunActionRequired = ToolRunAsistantHint & {
status: 'requires_action';
action_type: string;
action_url: string | null;
};
/**
* Tool run response
*
* Superface responses are either successful, failed, or require action.
* The response contains an assistant hint that can help LLM understand the result and what to do next.
*
*/
export type ToolRun<TResult = unknown> =
| ToolRunSuccess<TResult>
| ToolRunError
| ToolRunActionRequired;
/**
* Response with a link to the user's connections
*/
export type UserConnectionsLink = {
status: 'success';
url: string;
assistant_hint: string;
};
/**
* Tool definition
*
* Currenty only function tools are supported.
*
* @param type Type of the tool (currently only 'function' is supported)
* @param function Function tool definition
* @param function.name Name of the tool
* @param function.description Description of the tool
* @param function.parameters Parameters of the tool. Defined as JSON schema.
*/
export type ToolDefinition = {
type: 'function';
function: {
name: string;
description: string;
parameters: Record<string, unknown>; // TODO: JSONSchema type
};
};
/**
* @param apiKey API key for the Superface
* @param applicationReturnLink Optional return link from Connections page to the application
* @param cacheTimeout Optional cache timeout in milliseconds (default 60000)
* @param maxRetries Optional maximum number of retries for requests (default 3)
*/
export type SuperfaceOptions = {
apiKey?: string;
applicationReturnLink?: ApplicationReturnLink;
cacheTimeout?: number; // ms
maxRetries?: number;
};
/**
* @param appName Name of the application (showed on Connections page)
* @param appUrl URL of the application where user should be redirected from Connections page
*/
export type ApplicationReturnLink = {
appName: string;
appUrl: string;
};
/**
* Generic error thrown by the Superface client
*
* This should make intercepting errors easier.
* To access the original error, use the `originalError` property.
*/
export class SuperfaceError extends Error {
originalError?: unknown;
constructor(message: string, error?: unknown) {
super(message);
this.name = 'SuperfaceError';
this.originalError = error;
}
}
const USER_ID_REGEX = /^[a-zA-Z0-9-_|]{1,100}$/;
/**
* @returns Whether the user ID is valid
*/
export function isUserIdValid(userId: string): boolean {
return new RegExp(USER_ID_REGEX).test(userId);
}
/**
* @throws {SuperfaceError} If the user ID is invalid
*/
export function assertUserId(userId: string): asserts userId is string {
if (!isUserIdValid(userId)) {
throw new SuperfaceError(
`Invalid user ID: ${userId}. Must match ${USER_ID_REGEX}`
);
}
}
/**
* Superface HTTP Client
*
* Low level client for the Superface API.
* Consider using framework-specific clients.
*
* @example
* import Superface from '@superfaceai/client';
* const superface = new Superface({ apiKey });
*
* // Get and pass the tools to the LLM Call
* const tools = await superface.getTools();
*
* // Once the LLM Call is done, check messages for tool calls and run them
* const result = await superface.runTool({ userId, name, args });
*/
export class Superface {
private superfaceUrl: string;
private apiKey: string;
private returnLink?: ApplicationReturnLink;
private cacheTimeout: number;
private maxRetries: number;
private toolsCache?: { timestamp: number; body: ToolDefinition[] };
constructor(opts: SuperfaceOptions = {}) {
this.superfaceUrl =
(process.env.SUPERFACE_URL as string) || 'https://pod.superface.ai';
this.apiKey = opts.apiKey ?? (process.env.SUPERFACE_API_KEY as string);
this.returnLink = opts.applicationReturnLink;
this.cacheTimeout =
opts.cacheTimeout ??
parseInt(process.env.SUPERFACE_CACHE_TIMEOUT || MAX_CACHE_TIMEOUT, 10);
this.maxRetries =
opts.maxRetries ??
parseInt(process.env.SUPERFACE_MAX_RETRIES || DEFAULT_MAX_RETRIES, 10);
if (!this.apiKey) {
throw new SuperfaceError(`
The SUPERFACE_API_KEY environment variable is missing or empty; either provide it, or instantiate the SuperfaceClient with an apiKey option, like new SuperfaceClient({ apiKey: 'My API Key' })`);
}
}
/**
* List installed tools
*
* Tool definitions of installed tools in the Superface.
* Contains the name, description, and input parameters of the tool.
*
* Result is cached. Use cacheTimeout option on Superface to set the cache timeout or set env variable SUPERFACE_CACHE_TIMEOUT. Default is 60000ms.
*
* @throws {SuperfaceError} If the request fails
*/
async getTools(): Promise<ToolDefinition[]> {
const now = Date.now();
if (
this.toolsCache &&
now - this.toolsCache.timestamp < this.cacheTimeout
) {
return this.toolsCache.body;
}
let response: Response;
let lastError: SuperfaceError | undefined;
for (let attempt = 0; attempt < this.maxRetries; attempt++) {
try {
response = await fetch(`${this.superfaceUrl}/api/hub/fd`, {
headers: {
Authorization: `Bearer ${this.apiKey}`,
},
});
} catch (err: unknown) {
lastError = new SuperfaceError(`Unable to fetch tool definitions`, err);
await delay(attempt);
continue;
}
if (!response.ok) {
if (response.status === 401) {
const body = (await response.json()) as {
title: string;
detail: string;
};
throw new SuperfaceError(`${body.title}. ${body.detail}`);
}
lastError = new SuperfaceError(
`Failed to fetch tool definitions. HTTP status ${response.status}`
);
await delay(attempt);
continue;
}
let body: ToolDefinition[];
try {
body = await response.json();
} catch (err: unknown) {
lastError = new SuperfaceError(`Unable to parse tool definitions`, err);
await delay(attempt);
continue;
}
this.toolsCache = { timestamp: now, body };
return body;
}
throw (
lastError ??
new SuperfaceError(
`Failed to fetch tool definitions. Maximum retries reached.`
)
);
}
/**
* Run a tool
*
* Runs a tool with the given name and arguments in the Superface.
* The tool must be installed in the Superface.
*
* Request is retried up to 3 times with exponential backoff. Max retries can be set with SUPERFACE_MAX_RETRIES environment variable.
*
* @param userId User ID
* @param name Name of the tool
* @param args Arguments for the tool
*
* @throws {SuperfaceError}
*
* @example
* const toolRun = await superface.runTool({
* userId: 'example_user',
* name: toolCall.function.name,
* args: JSON.parse(toolCall.function.arguments),
* });
*
* switch (toolRun.status) {
* case 'success': {
* console.log('✅', 'Tool run successful');
* console.log(toolRun.result);
* break;
* }
* case 'error': {
* console.error('❌', 'Tool run failed');
* console.error(toolRun.error);
* break;
* }
* case 'requires_action': {
* console.error('❗', 'Tool run requires action');
* console.error(toolRun.action_type, ' => ', toolRun.action_url);
* break;
* }
* }
*/
async runTool<
TArgs extends Record<string, unknown> = Record<string, unknown>,
TResult = unknown
>({
userId,
name,
args,
}: {
name: string;
userId: string;
args: TArgs;
}): Promise<ToolRun> {
assertUserId(userId);
let response: Response;
let lastErrorResult: ToolRun | undefined;
for (let attempt = 0; attempt < this.maxRetries; attempt++) {
try {
const headers = new Headers({
'Content-Type': 'application/json',
Authorization: `Bearer ${this.apiKey}`,
'x-superface-user-id': userId,
});
if (this.returnLink?.appName && this.returnLink.appUrl) {
headers.append('x-superface-app-name', this.returnLink.appName);
headers.append('x-superface-app-url', this.returnLink.appUrl);
}
response = await fetch(`${this.superfaceUrl}/api/hub/perform/${name}`, {
method: 'POST',
body: JSON.stringify(args),
headers,
});
if (!response.ok) {
if (response.status === 401) {
const body = (await response.json()) as {
title: string;
detail: string;
};
throw new SuperfaceError(`${body.title}. ${body.detail}`);
}
lastErrorResult = {
status: 'error',
error: `Failed to connect to Superface. HTTP status ${response.status}`,
assistant_hint: 'Please try again.',
};
}
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
try {
const body = await response.json();
return body as ToolRun<TResult>;
} catch (err) {
return {
status: 'error',
error: 'Failed to parse response from Superface ' + err,
assistant_hint: 'Please try again.',
};
}
} else {
lastErrorResult = {
status: 'error',
error: `Failed to connect to Superface Wrong content type ${contentType} received`,
assistant_hint: 'Please try again.',
};
}
} catch (err) {
lastErrorResult = {
status: 'error',
error: `Failed to connect to Superface (attempt ${
attempt + 1
}): ${err}`,
assistant_hint: 'Please try again.',
};
}
await delay(attempt);
}
return (
lastErrorResult ?? {
status: 'error',
error: 'Failed to connect to Superface. Maximum retries reached.',
assistant_hint: 'Please try again.',
}
);
}
/**
* Get link to users connections
*
* Fetches a configuration link for the user to manage connections in the Superface.
*
* @throws {SuperfaceError}
*
* @example
* const link = await superface.linkToUserConnections({ userId: 'example_user' });
* redirect(link.url);
*/
async linkToUserConnections({
userId,
}: {
userId: string;
}): Promise<UserConnectionsLink> {
assertUserId(userId);
const headers = new Headers({
'Content-Type': 'application/json',
Authorization: `Bearer ${this.apiKey}`,
'x-superface-user-id': userId,
});
if (this.returnLink?.appName && this.returnLink.appUrl) {
headers.append('x-superface-app-name', this.returnLink.appName);
headers.append('x-superface-app-url', this.returnLink.appUrl);
}
let lastError: SuperfaceError | undefined;
for (let attempt = 0; attempt < this.maxRetries; attempt++) {
try {
const response = await fetch(`${this.superfaceUrl}/api/hub/session`, {
method: 'POST',
headers,
});
if (!response.ok) {
if (response.status === 401) {
const body = (await response.json()) as {
title: string;
detail: string;
};
throw new SuperfaceError(`${body.title}. ${body.detail}`);
}
lastError = new SuperfaceError(
`Failed to fetch configuration link. HTTP status ${response.status}`
);
await delay(attempt);
continue;
}
const body = (await response.json()) as {
status: 'success';
configuration_url: string;
assistant_hint: string;
};
return {
status: body.status,
url: body.configuration_url,
assistant_hint: body.assistant_hint,
};
} catch (err) {
lastError = new SuperfaceError(
`Unable to fetch configuration link.`,
err
);
await delay(attempt);
}
}
throw (
lastError ??
new SuperfaceError(
`Failed to fetch configuration link. Maximum retries reached.`
)
);
}
/**
* Check if a tool is connected for a user
* @param userId User ID to check
* @param toolName Name of the tool to check
* @returns Object containing provider ID and connection status
*/
async isToolConnected({
userId,
toolName,
}: {
userId: string;
toolName: string;
}): Promise<{
provider: string;
connected: boolean;
}> {
assertUserId(userId);
let response: Response;
let lastError: SuperfaceError | undefined;
for (let attempt = 0; attempt < this.maxRetries; attempt++) {
try {
response = await fetch(`${this.superfaceUrl}/api/hub/tools/${toolName}`, {
headers: {
Authorization: `Bearer ${this.apiKey}`,
'x-superface-user-id': userId,
},
});
if (!response.ok) {
if ([400, 401, 404, 422].includes(response.status)) {
const body = (await response.json()) as {
title: string;
detail: string;
};
throw new SuperfaceError(`${body.title}. ${body.detail}`);
}
lastError = new SuperfaceError(
`Failed to check tool connection. HTTP status ${response.status}`
);
await delay(attempt);
continue;
}
const body = await response.json();
return {
provider: body.provider,
connected: body.connected ?? false
};
} catch (err) {
lastError = new SuperfaceError(
`Unable to check tool connection state`,
err
);
await delay(attempt);
}
}
throw (
lastError ??
new SuperfaceError(
`Failed to check tool connection. Maximum retries reached.`
)
);
}
}
export default Superface;
/**
* Exponential backoff delay
*/
function delay(attempt: number) {
const waitTime = Math.pow(4, attempt) * 100; // 100ms, 400ms, 1600ms
return new Promise((resolve) => setTimeout(resolve, waitTime));
}