@amigo-ai/platform-sdk 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -0
- package/api.md +347 -0
- package/dist/core/errors.js +26 -4
- package/dist/core/errors.js.map +1 -1
- package/dist/core/openapi-client.js +164 -28
- package/dist/core/openapi-client.js.map +1 -1
- package/dist/core/request-options.js +80 -0
- package/dist/core/request-options.js.map +1 -0
- package/dist/core/retry.js +5 -2
- package/dist/core/retry.js.map +1 -1
- package/dist/core/utils.js +48 -2
- package/dist/core/utils.js.map +1 -1
- package/dist/core/webhooks.js +9 -0
- package/dist/core/webhooks.js.map +1 -1
- package/dist/index.cjs +538 -84
- package/dist/index.cjs.map +4 -4
- package/dist/index.js +113 -30
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +538 -84
- package/dist/index.mjs.map +4 -4
- package/dist/resources/base.js +33 -0
- package/dist/resources/base.js.map +1 -1
- package/dist/types/core/errors.d.ts +6 -0
- package/dist/types/core/errors.d.ts.map +1 -1
- package/dist/types/core/openapi-client.d.ts +26 -1
- package/dist/types/core/openapi-client.d.ts.map +1 -1
- package/dist/types/core/request-options.d.ts +66 -0
- package/dist/types/core/request-options.d.ts.map +1 -0
- package/dist/types/core/retry.d.ts +1 -1
- package/dist/types/core/retry.d.ts.map +1 -1
- package/dist/types/core/utils.d.ts +27 -1
- package/dist/types/core/utils.d.ts.map +1 -1
- package/dist/types/core/webhooks.d.ts.map +1 -1
- package/dist/types/index.d.ts +31 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/resources/actions.d.ts +5 -5
- package/dist/types/resources/agents.d.ts +7 -7
- package/dist/types/resources/analytics.d.ts +11 -11
- package/dist/types/resources/api-keys.d.ts +4 -4
- package/dist/types/resources/audit.d.ts +6 -6
- package/dist/types/resources/base.d.ts +8 -3
- package/dist/types/resources/base.d.ts.map +1 -1
- package/dist/types/resources/billing.d.ts +6 -6
- package/dist/types/resources/calls.d.ts +6 -6
- package/dist/types/resources/compliance.d.ts +3 -3
- package/dist/types/resources/context-graphs.d.ts +7 -7
- package/dist/types/resources/data-sources.d.ts +6 -6
- package/dist/types/resources/functions.d.ts +6 -6
- package/dist/types/resources/integrations.d.ts +6 -6
- package/dist/types/resources/memory.d.ts +3 -3
- package/dist/types/resources/operators.d.ts +18 -18
- package/dist/types/resources/personas.d.ts +4 -4
- package/dist/types/resources/phone-numbers.d.ts +5 -5
- package/dist/types/resources/recordings.d.ts +2 -2
- package/dist/types/resources/review-queue.d.ts +17 -17
- package/dist/types/resources/safety.d.ts +5 -5
- package/dist/types/resources/services.d.ts +4 -4
- package/dist/types/resources/settings.d.ts +14 -14
- package/dist/types/resources/simulations.d.ts +6 -6
- package/dist/types/resources/skills.d.ts +5 -5
- package/dist/types/resources/triggers.d.ts +8 -8
- package/dist/types/resources/webhook-destinations.d.ts +6 -6
- package/dist/types/resources/workspaces.d.ts +5 -5
- package/dist/types/resources/world.d.ts +18 -18
- package/package.json +6 -2
package/dist/index.mjs
CHANGED
|
@@ -115,6 +115,13 @@ var NetworkError = class extends AmigoError {
|
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
};
|
|
118
|
+
var RequestTimeoutError = class extends NetworkError {
|
|
119
|
+
timeoutMs;
|
|
120
|
+
constructor(message, timeoutMs, cause) {
|
|
121
|
+
super(message, cause);
|
|
122
|
+
this.timeoutMs = timeoutMs;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
118
125
|
var ParseError = class extends AmigoError {
|
|
119
126
|
body;
|
|
120
127
|
constructor(message, body) {
|
|
@@ -188,6 +195,9 @@ function isRateLimitError(err) {
|
|
|
188
195
|
function isAuthenticationError(err) {
|
|
189
196
|
return err instanceof AuthenticationError;
|
|
190
197
|
}
|
|
198
|
+
function isRequestTimeoutError(err) {
|
|
199
|
+
return err instanceof RequestTimeoutError;
|
|
200
|
+
}
|
|
191
201
|
|
|
192
202
|
// src/core/openapi-client.ts
|
|
193
203
|
import createClientImport from "openapi-fetch";
|
|
@@ -210,6 +220,104 @@ function createAuthMiddleware(config) {
|
|
|
210
220
|
};
|
|
211
221
|
}
|
|
212
222
|
|
|
223
|
+
// src/core/rate-limit.ts
|
|
224
|
+
function parseRateLimitHeaders(headers) {
|
|
225
|
+
const limit = headers.get("x-ratelimit-limit");
|
|
226
|
+
const remaining = headers.get("x-ratelimit-remaining");
|
|
227
|
+
const reset = headers.get("x-ratelimit-reset");
|
|
228
|
+
const retryAfter = headers.get("retry-after");
|
|
229
|
+
return {
|
|
230
|
+
limit: limit ? parseInt(limit, 10) : null,
|
|
231
|
+
remaining: remaining ? parseInt(remaining, 10) : null,
|
|
232
|
+
reset: reset ? new Date(parseInt(reset, 10) * 1e3) : null,
|
|
233
|
+
retryAfter: retryAfter ? parseInt(retryAfter, 10) : null
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// src/core/request-options.ts
|
|
238
|
+
function stripRequestControls(options) {
|
|
239
|
+
if (!options) {
|
|
240
|
+
return void 0;
|
|
241
|
+
}
|
|
242
|
+
const rest = { ...options };
|
|
243
|
+
delete rest.timeout;
|
|
244
|
+
delete rest.maxRetries;
|
|
245
|
+
delete rest.retry;
|
|
246
|
+
return rest;
|
|
247
|
+
}
|
|
248
|
+
function mergeRequestOptions(base, override) {
|
|
249
|
+
if (!base) {
|
|
250
|
+
return override;
|
|
251
|
+
}
|
|
252
|
+
if (!override) {
|
|
253
|
+
return base;
|
|
254
|
+
}
|
|
255
|
+
const mergeableOverride = override;
|
|
256
|
+
return {
|
|
257
|
+
...base,
|
|
258
|
+
...override,
|
|
259
|
+
headers: mergeHeaders(base.headers, mergeableOverride.headers),
|
|
260
|
+
signal: mergeableOverride.signal ?? base.signal,
|
|
261
|
+
timeout: mergeableOverride.timeout ?? base.timeout,
|
|
262
|
+
maxRetries: mergeableOverride.maxRetries ?? base.maxRetries,
|
|
263
|
+
retry: mergeableOverride.retry ?? base.retry
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
function mergeScopedRequestOptions(base, override) {
|
|
267
|
+
return mergeRequestOptions(base, override) ?? override;
|
|
268
|
+
}
|
|
269
|
+
function mergeHeaders(base, override) {
|
|
270
|
+
if (!base && !override) {
|
|
271
|
+
return void 0;
|
|
272
|
+
}
|
|
273
|
+
const headers = new Headers();
|
|
274
|
+
let hasEntries = false;
|
|
275
|
+
applyHeaders(headers, base, () => {
|
|
276
|
+
hasEntries = true;
|
|
277
|
+
});
|
|
278
|
+
applyHeaders(headers, override, () => {
|
|
279
|
+
hasEntries = true;
|
|
280
|
+
});
|
|
281
|
+
return hasEntries ? headers : void 0;
|
|
282
|
+
}
|
|
283
|
+
function applyHeaders(target, source, onSet) {
|
|
284
|
+
if (!source) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (source instanceof Headers) {
|
|
288
|
+
source.forEach((value, key) => {
|
|
289
|
+
target.set(key, value);
|
|
290
|
+
onSet();
|
|
291
|
+
});
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (Array.isArray(source)) {
|
|
295
|
+
for (const [key, value] of source) {
|
|
296
|
+
if (!key) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
if (value === null || value === void 0) {
|
|
300
|
+
target.delete(key);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
target.set(key, String(value));
|
|
304
|
+
onSet();
|
|
305
|
+
}
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
for (const [key, value] of Object.entries(source)) {
|
|
309
|
+
if (value === null || value === void 0) {
|
|
310
|
+
target.delete(key);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
target.set(
|
|
314
|
+
key,
|
|
315
|
+
Array.isArray(value) ? value.map((item) => String(item)).join(", ") : String(value)
|
|
316
|
+
);
|
|
317
|
+
onSet();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
213
321
|
// src/core/retry.ts
|
|
214
322
|
var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
215
323
|
var POST_RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([429]);
|
|
@@ -243,28 +351,177 @@ function parseRetryAfterHeader(header) {
|
|
|
243
351
|
}
|
|
244
352
|
return void 0;
|
|
245
353
|
}
|
|
246
|
-
function resolveRetryOptions(opts) {
|
|
354
|
+
function resolveRetryOptions(opts, maxRetries) {
|
|
355
|
+
const maxAttempts = typeof maxRetries === "number" && Number.isFinite(maxRetries) ? Math.max(1, Math.floor(maxRetries) + 1) : opts?.maxAttempts ?? 3;
|
|
247
356
|
return {
|
|
248
|
-
maxAttempts
|
|
357
|
+
maxAttempts,
|
|
249
358
|
baseDelayMs: opts?.baseDelayMs ?? 250,
|
|
250
359
|
maxDelayMs: opts?.maxDelayMs ?? 3e4
|
|
251
360
|
};
|
|
252
361
|
}
|
|
253
362
|
|
|
363
|
+
// src/core/utils.ts
|
|
364
|
+
function extractRequestId(response) {
|
|
365
|
+
return response.headers.get("x-request-id");
|
|
366
|
+
}
|
|
367
|
+
function buildLastResponse(response) {
|
|
368
|
+
return {
|
|
369
|
+
requestId: extractRequestId(response),
|
|
370
|
+
statusCode: response.status,
|
|
371
|
+
headers: response.headers,
|
|
372
|
+
rateLimit: parseRateLimitHeaders(response.headers)
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function extractData(result) {
|
|
376
|
+
if (result.data !== void 0) {
|
|
377
|
+
return attachResponseMetadata(result.data, result.response);
|
|
378
|
+
}
|
|
379
|
+
throw new ParseError(
|
|
380
|
+
"Unexpected empty response from API",
|
|
381
|
+
result.error !== void 0 ? JSON.stringify(result.error) : void 0
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
function withResponse(result) {
|
|
385
|
+
const data = extractData(result);
|
|
386
|
+
const lastResponse = buildLastResponse(result.response);
|
|
387
|
+
return {
|
|
388
|
+
data,
|
|
389
|
+
response: result.response,
|
|
390
|
+
requestId: lastResponse.requestId,
|
|
391
|
+
rateLimit: lastResponse.rateLimit
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
async function* paginate(fetcher) {
|
|
395
|
+
let token = void 0;
|
|
396
|
+
while (true) {
|
|
397
|
+
const page = await fetcher(token);
|
|
398
|
+
for (const item of page.items) {
|
|
399
|
+
yield item;
|
|
400
|
+
}
|
|
401
|
+
if (!page.has_more || page.continuation_token === null) break;
|
|
402
|
+
token = page.continuation_token;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function attachResponseMetadata(data, response) {
|
|
406
|
+
if (!response || typeof data !== "object" || data === null) {
|
|
407
|
+
return data;
|
|
408
|
+
}
|
|
409
|
+
const target = data;
|
|
410
|
+
const lastResponse = buildLastResponse(response);
|
|
411
|
+
defineHiddenMetadata(target, "_request_id", lastResponse.requestId);
|
|
412
|
+
defineHiddenMetadata(target, "lastResponse", lastResponse);
|
|
413
|
+
return target;
|
|
414
|
+
}
|
|
415
|
+
function defineHiddenMetadata(target, key, value) {
|
|
416
|
+
try {
|
|
417
|
+
Object.defineProperty(target, key, {
|
|
418
|
+
value,
|
|
419
|
+
enumerable: false,
|
|
420
|
+
configurable: true,
|
|
421
|
+
writable: false
|
|
422
|
+
});
|
|
423
|
+
} catch {
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
254
427
|
// src/core/openapi-client.ts
|
|
255
428
|
var createClient = typeof createClientImport === "function" ? createClientImport : createClientImport.default;
|
|
429
|
+
var platformClientContext = /* @__PURE__ */ new WeakMap();
|
|
256
430
|
function createPlatformClient(config) {
|
|
257
|
-
const
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
431
|
+
const transport = toRequestTransport(config.fetch ?? globalThis.fetch);
|
|
432
|
+
const defaults = {
|
|
433
|
+
retry: config.retry,
|
|
434
|
+
maxRetries: config.maxRetries,
|
|
435
|
+
timeout: config.timeout
|
|
436
|
+
};
|
|
437
|
+
const client = createClient({
|
|
438
|
+
baseUrl: config.baseUrl,
|
|
439
|
+
fetch: createRetryingFetch(transport, defaults),
|
|
440
|
+
headers: config.headers
|
|
441
|
+
});
|
|
442
|
+
platformClientContext.set(client, { transport, defaults });
|
|
443
|
+
const errorMiddleware = {
|
|
444
|
+
async onResponse({ response }) {
|
|
445
|
+
if (!response.ok) {
|
|
446
|
+
throw await createApiError(response);
|
|
447
|
+
}
|
|
448
|
+
return response;
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
const authMiddleware = createAuthMiddleware({ apiKey: config.apiKey });
|
|
452
|
+
const hookMiddleware = config.hooks ? {
|
|
453
|
+
async onRequest({ request, schemaPath, id }) {
|
|
454
|
+
await config.hooks?.onRequest?.({ request, schemaPath, id });
|
|
455
|
+
return request;
|
|
456
|
+
},
|
|
457
|
+
async onResponse({ request, response, schemaPath, id }) {
|
|
458
|
+
await config.hooks?.onResponse?.({
|
|
459
|
+
id,
|
|
460
|
+
request,
|
|
461
|
+
response,
|
|
462
|
+
schemaPath,
|
|
463
|
+
requestId: extractRequestId(response),
|
|
464
|
+
rateLimit: parseRateLimitHeaders(response.headers)
|
|
465
|
+
});
|
|
466
|
+
return response;
|
|
467
|
+
},
|
|
468
|
+
async onError({ request, error, schemaPath, id }) {
|
|
469
|
+
await config.hooks?.onError?.({ id, request, error, schemaPath });
|
|
470
|
+
}
|
|
471
|
+
} : void 0;
|
|
472
|
+
client.use(authMiddleware);
|
|
473
|
+
client.use(errorMiddleware);
|
|
474
|
+
if (hookMiddleware) {
|
|
475
|
+
client.use(hookMiddleware);
|
|
476
|
+
}
|
|
477
|
+
return client;
|
|
478
|
+
}
|
|
479
|
+
function applyPlatformRequestOptions(client, init) {
|
|
480
|
+
if (!init) {
|
|
481
|
+
return void 0;
|
|
482
|
+
}
|
|
483
|
+
const context = platformClientContext.get(client);
|
|
484
|
+
const stripped = stripRequestControls(init);
|
|
485
|
+
if (!context) {
|
|
486
|
+
return stripped;
|
|
487
|
+
}
|
|
488
|
+
const overrideFetch = stripped?.fetch;
|
|
489
|
+
const hasControlOverride = overrideFetch !== void 0 || init.timeout !== void 0 || init.maxRetries !== void 0 || init.retry !== void 0;
|
|
490
|
+
if (!hasControlOverride) {
|
|
491
|
+
return stripped;
|
|
492
|
+
}
|
|
493
|
+
const transport = toRequestTransport(
|
|
494
|
+
overrideFetch ?? context.transport
|
|
495
|
+
);
|
|
496
|
+
const fetch = createRetryingFetch(transport, {
|
|
497
|
+
timeout: init.timeout ?? context.defaults.timeout,
|
|
498
|
+
maxRetries: init.maxRetries ?? context.defaults.maxRetries,
|
|
499
|
+
retry: init.retry ?? context.defaults.retry
|
|
500
|
+
});
|
|
501
|
+
return {
|
|
502
|
+
...stripped,
|
|
503
|
+
fetch
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
function createRetryingFetch(transport, defaults) {
|
|
507
|
+
return async (input, init) => {
|
|
508
|
+
const baseRequest = input instanceof Request ? input : new Request(input, init);
|
|
509
|
+
const method = baseRequest.method.toUpperCase();
|
|
510
|
+
const retryOpts = resolveRetryOptions(defaults.retry, defaults.maxRetries);
|
|
511
|
+
const timeoutMs = defaults.timeout;
|
|
262
512
|
const isIdempotent = method === "GET" || method === "HEAD" || method === "OPTIONS";
|
|
263
513
|
for (let attempt = 0; attempt < retryOpts.maxAttempts; attempt++) {
|
|
264
514
|
let response;
|
|
265
515
|
let error;
|
|
516
|
+
let timedOut = false;
|
|
266
517
|
try {
|
|
267
|
-
|
|
518
|
+
const prepared = prepareRequestForAttempt(baseRequest, timeoutMs);
|
|
519
|
+
try {
|
|
520
|
+
response = await transport(prepared.request);
|
|
521
|
+
} finally {
|
|
522
|
+
timedOut = prepared.timedOut;
|
|
523
|
+
prepared.cleanup();
|
|
524
|
+
}
|
|
268
525
|
} catch (err) {
|
|
269
526
|
error = err;
|
|
270
527
|
}
|
|
@@ -272,6 +529,9 @@ function createPlatformClient(config) {
|
|
|
272
529
|
const ctx = { method, attempt, response, options: retryOpts };
|
|
273
530
|
const attemptsRemain = attempt + 1 < retryOpts.maxAttempts;
|
|
274
531
|
if (error) {
|
|
532
|
+
if (timedOut) {
|
|
533
|
+
throw new RequestTimeoutError(`Request timed out after ${timeoutMs}ms`, timeoutMs, error);
|
|
534
|
+
}
|
|
275
535
|
if (isIdempotent && attemptsRemain) {
|
|
276
536
|
await sleep(computeDelay(attempt, new Response(), retryOpts));
|
|
277
537
|
continue;
|
|
@@ -283,7 +543,7 @@ function createPlatformClient(config) {
|
|
|
283
543
|
}
|
|
284
544
|
if (response && attemptsRemain && shouldRetry(ctx)) {
|
|
285
545
|
const delay = computeDelay(attempt, response, retryOpts);
|
|
286
|
-
if (signal
|
|
546
|
+
if (baseRequest.signal.aborted) return response;
|
|
287
547
|
await sleep(delay);
|
|
288
548
|
continue;
|
|
289
549
|
}
|
|
@@ -291,54 +551,163 @@ function createPlatformClient(config) {
|
|
|
291
551
|
}
|
|
292
552
|
throw new NetworkError("Retry loop exhausted");
|
|
293
553
|
};
|
|
294
|
-
const client = createClient({
|
|
295
|
-
baseUrl: config.baseUrl,
|
|
296
|
-
fetch: retryingFetch
|
|
297
|
-
});
|
|
298
|
-
const errorMiddleware = {
|
|
299
|
-
async onResponse({ response }) {
|
|
300
|
-
if (!response.ok) {
|
|
301
|
-
throw await createApiError(response);
|
|
302
|
-
}
|
|
303
|
-
return response;
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
const authMiddleware = createAuthMiddleware({ apiKey: config.apiKey });
|
|
307
|
-
client.use(authMiddleware);
|
|
308
|
-
client.use(errorMiddleware);
|
|
309
|
-
return client;
|
|
310
554
|
}
|
|
311
555
|
function sleep(ms) {
|
|
312
556
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
313
557
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
function extractData(result) {
|
|
317
|
-
if (result.data !== void 0) return result.data;
|
|
318
|
-
throw new ParseError(
|
|
319
|
-
"Unexpected empty response from API",
|
|
320
|
-
result.error !== void 0 ? JSON.stringify(result.error) : void 0
|
|
321
|
-
);
|
|
558
|
+
function toRequestTransport(fetcher) {
|
|
559
|
+
return async (input) => fetcher(input);
|
|
322
560
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
561
|
+
function prepareRequestForAttempt(request, timeoutMs) {
|
|
562
|
+
const attemptRequest = request.clone();
|
|
563
|
+
const timeout = createTimeoutSignal(attemptRequest.signal, timeoutMs);
|
|
564
|
+
if (!timeout.signal) {
|
|
565
|
+
return {
|
|
566
|
+
request: attemptRequest,
|
|
567
|
+
timedOut: false,
|
|
568
|
+
cleanup: timeout.cleanup
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
return {
|
|
572
|
+
request: new Request(attemptRequest, { signal: timeout.signal }),
|
|
573
|
+
get timedOut() {
|
|
574
|
+
return timeout.didTimeout;
|
|
575
|
+
},
|
|
576
|
+
cleanup: timeout.cleanup
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
function createTimeoutSignal(upstream, timeoutMs) {
|
|
580
|
+
if (!timeoutMs || !Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
581
|
+
return {
|
|
582
|
+
signal: upstream ?? void 0,
|
|
583
|
+
didTimeout: false,
|
|
584
|
+
cleanup: () => {
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
const controller = new AbortController();
|
|
589
|
+
let didTimeout = false;
|
|
590
|
+
const cleanups = [];
|
|
591
|
+
const onAbort = () => controller.abort(upstream?.reason);
|
|
592
|
+
if (upstream) {
|
|
593
|
+
if (upstream.aborted) {
|
|
594
|
+
controller.abort(upstream.reason);
|
|
595
|
+
} else {
|
|
596
|
+
upstream.addEventListener("abort", onAbort, { once: true });
|
|
597
|
+
cleanups.push(() => upstream.removeEventListener("abort", onAbort));
|
|
329
598
|
}
|
|
330
|
-
if (!page.has_more || page.continuation_token === null) break;
|
|
331
|
-
token = page.continuation_token;
|
|
332
599
|
}
|
|
600
|
+
const timer = setTimeout(() => {
|
|
601
|
+
didTimeout = true;
|
|
602
|
+
controller.abort();
|
|
603
|
+
}, timeoutMs);
|
|
604
|
+
cleanups.push(() => clearTimeout(timer));
|
|
605
|
+
return {
|
|
606
|
+
signal: controller.signal,
|
|
607
|
+
get didTimeout() {
|
|
608
|
+
return didTimeout;
|
|
609
|
+
},
|
|
610
|
+
cleanup: () => {
|
|
611
|
+
for (const cleanup of cleanups) {
|
|
612
|
+
cleanup();
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
};
|
|
333
616
|
}
|
|
334
617
|
|
|
335
618
|
// src/resources/base.ts
|
|
619
|
+
var scopedClientState = /* @__PURE__ */ new WeakMap();
|
|
336
620
|
var WorkspaceScopedResource = class {
|
|
337
621
|
constructor(client, workspaceId2) {
|
|
338
622
|
this.client = client;
|
|
339
623
|
this.workspaceId = workspaceId2;
|
|
340
624
|
}
|
|
625
|
+
withOptions(options) {
|
|
626
|
+
const ResourceCtor = this.constructor;
|
|
627
|
+
return new ResourceCtor(scopePlatformClient(this.client, options), this.workspaceId);
|
|
628
|
+
}
|
|
341
629
|
};
|
|
630
|
+
function scopePlatformClient(client, options) {
|
|
631
|
+
const { baseClient, options: existingOptions } = resolveScopedPlatformClient(client);
|
|
632
|
+
const mergedOptions = mergeScopedRequestOptions(existingOptions, options);
|
|
633
|
+
const scopedClient = {
|
|
634
|
+
request: (method, path, init) => baseClient.request(
|
|
635
|
+
method,
|
|
636
|
+
path,
|
|
637
|
+
applyPlatformRequestOptions(
|
|
638
|
+
baseClient,
|
|
639
|
+
mergeRequestOptions(mergedOptions, init)
|
|
640
|
+
)
|
|
641
|
+
),
|
|
642
|
+
GET: (path, init) => baseClient.GET(
|
|
643
|
+
path,
|
|
644
|
+
applyPlatformRequestOptions(
|
|
645
|
+
baseClient,
|
|
646
|
+
mergeRequestOptions(mergedOptions, init)
|
|
647
|
+
)
|
|
648
|
+
),
|
|
649
|
+
PUT: (path, init) => baseClient.PUT(
|
|
650
|
+
path,
|
|
651
|
+
applyPlatformRequestOptions(
|
|
652
|
+
baseClient,
|
|
653
|
+
mergeRequestOptions(mergedOptions, init)
|
|
654
|
+
)
|
|
655
|
+
),
|
|
656
|
+
POST: (path, init) => baseClient.POST(
|
|
657
|
+
path,
|
|
658
|
+
applyPlatformRequestOptions(
|
|
659
|
+
baseClient,
|
|
660
|
+
mergeRequestOptions(mergedOptions, init)
|
|
661
|
+
)
|
|
662
|
+
),
|
|
663
|
+
DELETE: (path, init) => baseClient.DELETE(
|
|
664
|
+
path,
|
|
665
|
+
applyPlatformRequestOptions(
|
|
666
|
+
baseClient,
|
|
667
|
+
mergeRequestOptions(mergedOptions, init)
|
|
668
|
+
)
|
|
669
|
+
),
|
|
670
|
+
OPTIONS: (path, init) => baseClient.OPTIONS(
|
|
671
|
+
path,
|
|
672
|
+
applyPlatformRequestOptions(
|
|
673
|
+
baseClient,
|
|
674
|
+
mergeRequestOptions(mergedOptions, init)
|
|
675
|
+
)
|
|
676
|
+
),
|
|
677
|
+
HEAD: (path, init) => baseClient.HEAD(
|
|
678
|
+
path,
|
|
679
|
+
applyPlatformRequestOptions(
|
|
680
|
+
baseClient,
|
|
681
|
+
mergeRequestOptions(mergedOptions, init)
|
|
682
|
+
)
|
|
683
|
+
),
|
|
684
|
+
PATCH: (path, init) => baseClient.PATCH(
|
|
685
|
+
path,
|
|
686
|
+
applyPlatformRequestOptions(
|
|
687
|
+
baseClient,
|
|
688
|
+
mergeRequestOptions(mergedOptions, init)
|
|
689
|
+
)
|
|
690
|
+
),
|
|
691
|
+
TRACE: (path, init) => baseClient.TRACE(
|
|
692
|
+
path,
|
|
693
|
+
applyPlatformRequestOptions(
|
|
694
|
+
baseClient,
|
|
695
|
+
mergeRequestOptions(mergedOptions, init)
|
|
696
|
+
)
|
|
697
|
+
),
|
|
698
|
+
use: (...middleware) => baseClient.use(...middleware),
|
|
699
|
+
eject: (...middleware) => baseClient.eject(...middleware)
|
|
700
|
+
};
|
|
701
|
+
scopedClientState.set(scopedClient, { baseClient, options: mergedOptions });
|
|
702
|
+
return scopedClient;
|
|
703
|
+
}
|
|
704
|
+
function resolveScopedPlatformClient(client) {
|
|
705
|
+
const existing = scopedClientState.get(client);
|
|
706
|
+
return {
|
|
707
|
+
baseClient: existing?.baseClient ?? client,
|
|
708
|
+
options: existing?.options
|
|
709
|
+
};
|
|
710
|
+
}
|
|
342
711
|
|
|
343
712
|
// src/resources/workspaces.ts
|
|
344
713
|
var WorkspacesResource = class extends WorkspaceScopedResource {
|
|
@@ -2108,23 +2477,10 @@ var simulationSessionId = (id) => id;
|
|
|
2108
2477
|
var functionId = (id) => id;
|
|
2109
2478
|
var dataSourceId = (id) => id;
|
|
2110
2479
|
|
|
2111
|
-
// src/core/rate-limit.ts
|
|
2112
|
-
function parseRateLimitHeaders(headers) {
|
|
2113
|
-
const limit = headers.get("x-ratelimit-limit");
|
|
2114
|
-
const remaining = headers.get("x-ratelimit-remaining");
|
|
2115
|
-
const reset = headers.get("x-ratelimit-reset");
|
|
2116
|
-
const retryAfter = headers.get("retry-after");
|
|
2117
|
-
return {
|
|
2118
|
-
limit: limit ? parseInt(limit, 10) : null,
|
|
2119
|
-
remaining: remaining ? parseInt(remaining, 10) : null,
|
|
2120
|
-
reset: reset ? new Date(parseInt(reset, 10) * 1e3) : null,
|
|
2121
|
-
retryAfter: retryAfter ? parseInt(retryAfter, 10) : null
|
|
2122
|
-
};
|
|
2123
|
-
}
|
|
2124
|
-
|
|
2125
2480
|
// src/core/webhooks.ts
|
|
2126
2481
|
var textEncoder = new TextEncoder();
|
|
2127
2482
|
var MAX_TIMESTAMP_SKEW_MS = 5 * 60 * 1e3;
|
|
2483
|
+
var webCryptoPromise;
|
|
2128
2484
|
var WebhookVerificationError = class extends Error {
|
|
2129
2485
|
constructor(message) {
|
|
2130
2486
|
super(message);
|
|
@@ -2212,6 +2568,7 @@ function toUint8Array(payload) {
|
|
|
2212
2568
|
return new Uint8Array(payload);
|
|
2213
2569
|
}
|
|
2214
2570
|
async function signWebhookPayload(payload, secret, timestamp) {
|
|
2571
|
+
const crypto = await resolveWebCrypto();
|
|
2215
2572
|
const key = await crypto.subtle.importKey(
|
|
2216
2573
|
"raw",
|
|
2217
2574
|
textEncoder.encode(secret),
|
|
@@ -2223,6 +2580,13 @@ async function signWebhookPayload(payload, secret, timestamp) {
|
|
|
2223
2580
|
const mac = await crypto.subtle.sign("HMAC", key, toCryptoBuffer(message));
|
|
2224
2581
|
return new Uint8Array(mac);
|
|
2225
2582
|
}
|
|
2583
|
+
async function resolveWebCrypto() {
|
|
2584
|
+
if (globalThis.crypto?.subtle) {
|
|
2585
|
+
return globalThis.crypto;
|
|
2586
|
+
}
|
|
2587
|
+
webCryptoPromise ??= import("node:crypto").then(({ webcrypto }) => webcrypto);
|
|
2588
|
+
return await webCryptoPromise;
|
|
2589
|
+
}
|
|
2226
2590
|
function normalizeSignature(signature) {
|
|
2227
2591
|
const normalized = signature.startsWith("sha256=") ? signature.slice(7) : signature;
|
|
2228
2592
|
if (!/^[a-fA-F0-9]+$/.test(normalized) || normalized.length % 2 !== 0) {
|
|
@@ -2263,7 +2627,9 @@ function toCryptoBuffer(bytes) {
|
|
|
2263
2627
|
|
|
2264
2628
|
// src/index.ts
|
|
2265
2629
|
var DEFAULT_BASE_URL = "https://api.platform.amigo.ai";
|
|
2266
|
-
var AmigoClient = class {
|
|
2630
|
+
var AmigoClient = class _AmigoClient {
|
|
2631
|
+
workspaceId;
|
|
2632
|
+
baseUrl;
|
|
2267
2633
|
workspaces;
|
|
2268
2634
|
apiKeys;
|
|
2269
2635
|
agents;
|
|
@@ -2292,6 +2658,7 @@ var AmigoClient = class {
|
|
|
2292
2658
|
safety;
|
|
2293
2659
|
compliance;
|
|
2294
2660
|
functions;
|
|
2661
|
+
api;
|
|
2295
2662
|
constructor(config) {
|
|
2296
2663
|
if (!config.apiKey || typeof config.apiKey !== "string") {
|
|
2297
2664
|
throw new ConfigurationError("apiKey is required and must be a non-empty string");
|
|
@@ -2304,38 +2671,121 @@ var AmigoClient = class {
|
|
|
2304
2671
|
apiKey: config.apiKey,
|
|
2305
2672
|
baseUrl,
|
|
2306
2673
|
retry: config.retry,
|
|
2674
|
+
maxRetries: config.maxRetries,
|
|
2675
|
+
timeout: config.timeout,
|
|
2676
|
+
headers: config.headers,
|
|
2677
|
+
hooks: config.hooks,
|
|
2307
2678
|
fetch: config.fetch
|
|
2308
2679
|
});
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
this.
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
this.
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
this.
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
this.
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
this.
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
this.
|
|
2335
|
-
|
|
2336
|
-
|
|
2680
|
+
_AmigoClient.hydrate(this, client, config.workspaceId, baseUrl);
|
|
2681
|
+
}
|
|
2682
|
+
withOptions(options) {
|
|
2683
|
+
return _AmigoClient.fromPlatformClient(
|
|
2684
|
+
scopePlatformClient(this.api, options),
|
|
2685
|
+
this.workspaceId,
|
|
2686
|
+
this.baseUrl
|
|
2687
|
+
);
|
|
2688
|
+
}
|
|
2689
|
+
async GET(path, ...[init]) {
|
|
2690
|
+
return withResponse(await this.resolveApiRequest(path, "GET", init));
|
|
2691
|
+
}
|
|
2692
|
+
async POST(path, ...[init]) {
|
|
2693
|
+
return withResponse(await this.resolveApiRequest(path, "POST", init));
|
|
2694
|
+
}
|
|
2695
|
+
async PUT(path, ...[init]) {
|
|
2696
|
+
return withResponse(await this.resolveApiRequest(path, "PUT", init));
|
|
2697
|
+
}
|
|
2698
|
+
async PATCH(path, ...[init]) {
|
|
2699
|
+
return withResponse(await this.resolveApiRequest(path, "PATCH", init));
|
|
2700
|
+
}
|
|
2701
|
+
async DELETE(path, ...[init]) {
|
|
2702
|
+
return withResponse(await this.resolveApiRequest(path, "DELETE", init));
|
|
2703
|
+
}
|
|
2704
|
+
async HEAD(path, ...[init]) {
|
|
2705
|
+
return withResponse(await this.resolveApiRequest(path, "HEAD", init));
|
|
2706
|
+
}
|
|
2707
|
+
async OPTIONS(path, ...[init]) {
|
|
2708
|
+
return withResponse(await this.resolveApiRequest(path, "OPTIONS", init));
|
|
2709
|
+
}
|
|
2710
|
+
static fromPlatformClient(client, workspaceId2, baseUrl) {
|
|
2711
|
+
const instance = Object.create(_AmigoClient.prototype);
|
|
2712
|
+
_AmigoClient.hydrate(instance, client, workspaceId2, baseUrl);
|
|
2713
|
+
return instance;
|
|
2714
|
+
}
|
|
2715
|
+
static hydrate(target, client, workspaceId2, baseUrl) {
|
|
2716
|
+
const mutable = target;
|
|
2717
|
+
mutable.workspaceId = workspaceId2;
|
|
2718
|
+
mutable.baseUrl = baseUrl;
|
|
2719
|
+
target.api = client;
|
|
2720
|
+
mutable.workspaces = new WorkspacesResource(client, workspaceId2);
|
|
2721
|
+
mutable.apiKeys = new ApiKeysResource(client, workspaceId2);
|
|
2722
|
+
mutable.agents = new AgentsResource(client, workspaceId2);
|
|
2723
|
+
mutable.skills = new SkillsResource(client, workspaceId2);
|
|
2724
|
+
mutable.actions = new ActionsResource(client, workspaceId2);
|
|
2725
|
+
mutable.operators = new OperatorsResource(client, workspaceId2);
|
|
2726
|
+
mutable.triggers = new TriggersResource(client, workspaceId2);
|
|
2727
|
+
mutable.services = new ServicesResource(client, workspaceId2);
|
|
2728
|
+
mutable.contextGraphs = new ContextGraphsResource(client, workspaceId2);
|
|
2729
|
+
mutable.dataSources = new DataSourcesResource(client, workspaceId2);
|
|
2730
|
+
mutable.world = new WorldResource(client, workspaceId2);
|
|
2731
|
+
mutable.calls = new CallsResource(client, workspaceId2);
|
|
2732
|
+
mutable.phoneNumbers = new PhoneNumbersResource(client, workspaceId2);
|
|
2733
|
+
mutable.integrations = new IntegrationsResource(client, workspaceId2);
|
|
2734
|
+
mutable.analytics = new AnalyticsResource(client, workspaceId2);
|
|
2735
|
+
mutable.simulations = new SimulationsResource(client, workspaceId2);
|
|
2736
|
+
mutable.settings = new SettingsResource(client, workspaceId2);
|
|
2737
|
+
mutable.billing = new BillingResource(client, workspaceId2);
|
|
2738
|
+
mutable.memory = new MemoryResource(client, workspaceId2);
|
|
2739
|
+
mutable.personas = new PersonasResource(client, workspaceId2);
|
|
2740
|
+
mutable.reviewQueue = new ReviewQueueResource(client, workspaceId2);
|
|
2741
|
+
mutable.recordings = new RecordingsResource(client, workspaceId2);
|
|
2742
|
+
mutable.audit = new AuditResource(client, workspaceId2);
|
|
2743
|
+
mutable.webhookDestinations = new WebhookDestinationsResource(client, workspaceId2);
|
|
2744
|
+
mutable.safety = new SafetyResource(client, workspaceId2);
|
|
2745
|
+
mutable.compliance = new ComplianceResource(client, workspaceId2);
|
|
2746
|
+
mutable.functions = new FunctionsResource(client, workspaceId2);
|
|
2747
|
+
}
|
|
2748
|
+
async resolveApiRequest(path, method, init) {
|
|
2749
|
+
const { baseClient, options } = resolveScopedPlatformClient(this.api);
|
|
2750
|
+
const mergedInit = mergeRequestOptions(options, withWorkspaceId(path, init, this.workspaceId));
|
|
2751
|
+
const requestInit = applyPlatformRequestOptions(
|
|
2752
|
+
baseClient,
|
|
2753
|
+
mergedInit
|
|
2754
|
+
);
|
|
2755
|
+
switch (method) {
|
|
2756
|
+
case "GET":
|
|
2757
|
+
return await baseClient.GET(path, requestInit);
|
|
2758
|
+
case "POST":
|
|
2759
|
+
return await baseClient.POST(path, requestInit);
|
|
2760
|
+
case "PUT":
|
|
2761
|
+
return await baseClient.PUT(path, requestInit);
|
|
2762
|
+
case "PATCH":
|
|
2763
|
+
return await baseClient.PATCH(path, requestInit);
|
|
2764
|
+
case "DELETE":
|
|
2765
|
+
return await baseClient.DELETE(path, requestInit);
|
|
2766
|
+
case "HEAD":
|
|
2767
|
+
return await baseClient.HEAD(path, requestInit);
|
|
2768
|
+
case "OPTIONS":
|
|
2769
|
+
return await baseClient.OPTIONS(path, requestInit);
|
|
2770
|
+
}
|
|
2337
2771
|
}
|
|
2338
2772
|
};
|
|
2773
|
+
function withWorkspaceId(path, init, workspaceId2) {
|
|
2774
|
+
if (!path.includes("{workspace_id}")) {
|
|
2775
|
+
return init ?? {};
|
|
2776
|
+
}
|
|
2777
|
+
const current = init ?? {};
|
|
2778
|
+
return {
|
|
2779
|
+
...current,
|
|
2780
|
+
params: {
|
|
2781
|
+
...current.params ?? {},
|
|
2782
|
+
path: {
|
|
2783
|
+
...current.params?.path ?? {},
|
|
2784
|
+
workspace_id: workspaceId2
|
|
2785
|
+
}
|
|
2786
|
+
}
|
|
2787
|
+
};
|
|
2788
|
+
}
|
|
2339
2789
|
export {
|
|
2340
2790
|
AmigoClient,
|
|
2341
2791
|
AmigoError,
|
|
@@ -2349,6 +2799,7 @@ export {
|
|
|
2349
2799
|
ParseError,
|
|
2350
2800
|
PermissionError,
|
|
2351
2801
|
RateLimitError,
|
|
2802
|
+
RequestTimeoutError,
|
|
2352
2803
|
ServerError,
|
|
2353
2804
|
ServiceUnavailableError,
|
|
2354
2805
|
ValidationError,
|
|
@@ -2356,17 +2807,20 @@ export {
|
|
|
2356
2807
|
actionId,
|
|
2357
2808
|
agentId,
|
|
2358
2809
|
apiKeyId,
|
|
2810
|
+
buildLastResponse,
|
|
2359
2811
|
callId,
|
|
2360
2812
|
contextGraphId,
|
|
2361
2813
|
dataSourceId,
|
|
2362
2814
|
entityId,
|
|
2363
2815
|
eventId,
|
|
2816
|
+
extractRequestId,
|
|
2364
2817
|
functionId,
|
|
2365
2818
|
integrationId,
|
|
2366
2819
|
isAmigoError,
|
|
2367
2820
|
isAuthenticationError,
|
|
2368
2821
|
isNotFoundError,
|
|
2369
2822
|
isRateLimitError,
|
|
2823
|
+
isRequestTimeoutError,
|
|
2370
2824
|
paginate,
|
|
2371
2825
|
parseRateLimitHeaders,
|
|
2372
2826
|
parseWebhookEvent,
|