@getsupervisor/agents-studio-sdk 1.41.0-patch.4 → 1.41.1-beta.162

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/dist/index.js CHANGED
@@ -179,7 +179,8 @@ function createHttp(cfg) {
179
179
  }
180
180
  return res;
181
181
  } catch (e) {
182
- if (e.name === "AbortError") throw new TimeoutError(timeout, targetUrl);
182
+ if (e?.name === "AbortError")
183
+ throw new TimeoutError(timeout, targetUrl);
183
184
  if (e instanceof HttpError) throw e;
184
185
  throw new NetworkError(e, targetUrl);
185
186
  }
@@ -200,6 +201,11 @@ function createHttp(cfg) {
200
201
  }
201
202
 
202
203
  // src/utils/query.ts
204
+ function buildSearchParams(entries) {
205
+ const searchParams = new URLSearchParams();
206
+ entries.forEach(([key, value]) => appendParam(searchParams, key, value));
207
+ return searchParams;
208
+ }
203
209
  function serializeListOptions(options = {}, extra = {}) {
204
210
  const params = new URLSearchParams();
205
211
  appendParam(params, "page", options.page);
@@ -885,6 +891,117 @@ function createAgentVersionsApi(cfg) {
885
891
  };
886
892
  }
887
893
 
894
+ // src/utils/sse-stream.ts
895
+ function extractField(line, prefix) {
896
+ if (!line.startsWith(prefix)) {
897
+ return null;
898
+ }
899
+ return line.slice(prefix.length);
900
+ }
901
+ function parseSseMessage(raw) {
902
+ let type = "";
903
+ let data = "";
904
+ for (const line of raw.split("\n")) {
905
+ type = extractField(line, "event: ") ?? type;
906
+ data = extractField(line, "data: ") ?? data;
907
+ }
908
+ if (!type || !data) {
909
+ return null;
910
+ }
911
+ return { type, data };
912
+ }
913
+ function dispatchSseEvent(event, handlers) {
914
+ const handler = handlers[event.type];
915
+ if (!handler) {
916
+ return;
917
+ }
918
+ try {
919
+ const parsed = JSON.parse(event.data);
920
+ handler(parsed);
921
+ return parsed;
922
+ } catch {
923
+ return;
924
+ }
925
+ }
926
+ function processRawPart(raw, handlers, resolveEvent) {
927
+ const trimmed = raw.trim();
928
+ if (!trimmed) {
929
+ return;
930
+ }
931
+ const event = parseSseMessage(trimmed);
932
+ if (!event) {
933
+ return;
934
+ }
935
+ const result = dispatchSseEvent(event, handlers);
936
+ if (event.type !== resolveEvent) {
937
+ return;
938
+ }
939
+ return result;
940
+ }
941
+ function flushRemainingBuffer(buffer) {
942
+ if (!buffer.trim()) {
943
+ return "";
944
+ }
945
+ return buffer + "\n\n";
946
+ }
947
+ function consumeSseStream(response, handlers, resolveEvent, options = {}) {
948
+ if (!response.body) {
949
+ throw new Error("No response body for SSE stream");
950
+ }
951
+ return new Promise((resolve, reject) => {
952
+ let resolved;
953
+ let settled = false;
954
+ let buffer = "";
955
+ const reader = response.body.getReader();
956
+ const decoder = new TextDecoder();
957
+ const processBuffer = () => {
958
+ const parts = buffer.split("\n\n");
959
+ buffer = parts.pop() ?? "";
960
+ for (const part of parts) {
961
+ const result = processRawPart(part, handlers, resolveEvent);
962
+ if (result) {
963
+ resolved = result;
964
+ }
965
+ }
966
+ };
967
+ const tryResolveImmediately = () => {
968
+ if (!options.resolveImmediately || resolved === void 0 || settled) {
969
+ return false;
970
+ }
971
+ settled = true;
972
+ resolve(resolved);
973
+ reader.cancel().catch(() => void 0);
974
+ return true;
975
+ };
976
+ const finalize = () => {
977
+ if (settled) return;
978
+ buffer = flushRemainingBuffer(buffer);
979
+ processBuffer();
980
+ if (resolved) {
981
+ resolve(resolved);
982
+ return;
983
+ }
984
+ reject(new Error(`SSE stream ended without '${resolveEvent}' event`));
985
+ };
986
+ const read = () => {
987
+ if (settled) return;
988
+ reader.read().then(({ done, value }) => {
989
+ if (done) {
990
+ finalize();
991
+ return;
992
+ }
993
+ buffer += decoder.decode(value, { stream: true });
994
+ processBuffer();
995
+ if (tryResolveImmediately()) {
996
+ return;
997
+ }
998
+ read();
999
+ }).catch(reject);
1000
+ };
1001
+ read();
1002
+ });
1003
+ }
1004
+
888
1005
  // src/entities/agent.ts
889
1006
  var bindAgentStageTriggers = (api, agentId, blueprintId, stageId) => ({
890
1007
  list(opts) {
@@ -1071,7 +1188,8 @@ var createAgentEntity = (dto, options) => {
1071
1188
  stageTriggersApi,
1072
1189
  reload,
1073
1190
  updateAgent,
1074
- deleteAgent
1191
+ deleteAgent,
1192
+ cloneAgent
1075
1193
  } = options;
1076
1194
  const schedulesHelper = bindAgentSchedules(
1077
1195
  scheduleApi,
@@ -1090,6 +1208,9 @@ var createAgentEntity = (dto, options) => {
1090
1208
  async save(patch) {
1091
1209
  return updateAgent(dto.agentId, patch);
1092
1210
  },
1211
+ async clone(payload) {
1212
+ return cloneAgent(dto.agentId, payload);
1213
+ },
1093
1214
  async delete() {
1094
1215
  await deleteAgent(dto.agentId);
1095
1216
  },
@@ -1101,8 +1222,38 @@ var createAgentEntity = (dto, options) => {
1101
1222
  };
1102
1223
 
1103
1224
  // src/api/agents.ts
1225
+ function normalizeCloneSelection(selection) {
1226
+ if (!selection) {
1227
+ return [];
1228
+ }
1229
+ if (Array.isArray(selection)) {
1230
+ return selection;
1231
+ }
1232
+ return Object.entries(selection).filter(([, enabled]) => enabled).map(([component]) => component);
1233
+ }
1234
+ function buildCloneRequest(payload) {
1235
+ const { clone, ...rest } = payload;
1236
+ return { ...rest, clone: normalizeCloneSelection(clone) };
1237
+ }
1238
+ function buildSseHandlers(callbacks) {
1239
+ const onAgent = (data) => callbacks.onAgentCreated?.(data);
1240
+ return {
1241
+ "agent-created": onAgent,
1242
+ agent: onAgent,
1243
+ "runtime-progress": (data) => callbacks.onProgress?.(data),
1244
+ "runtime-complete": (data) => callbacks.onComplete?.(data),
1245
+ "runtime-error": (data) => callbacks.onError?.(data)
1246
+ };
1247
+ }
1248
+ function resolveAgentId(agent) {
1249
+ if (typeof agent === "string") {
1250
+ return agent;
1251
+ }
1252
+ return agent.agentId;
1253
+ }
1104
1254
  function createAgentsApi(cfg, relatedApis) {
1105
- const { base, doFetch, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1255
+ const { base, doFetch, buildHeaders, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1256
+ const jsonHeaders = { "content-type": "application/json" };
1106
1257
  const requireWorkspace = () => {
1107
1258
  const workspaceId = resolveWorkspaceId();
1108
1259
  if (typeof workspaceId === "string" && workspaceId.trim().length > 0) {
@@ -1114,132 +1265,186 @@ function createAgentsApi(cfg, relatedApis) {
1114
1265
  }
1115
1266
  throw new WorkspaceNotSelectedError();
1116
1267
  };
1117
- const jsonHeaders = { "content-type": "application/json" };
1118
- const fetchAgentsPage = async (options = {}) => {
1268
+ const postJson = async (url, payload) => {
1119
1269
  requireWorkspace();
1120
- const sanitizedOptions = {
1121
- page: options.page,
1122
- limit: options.limit,
1123
- filter: options.filter
1124
- };
1125
- const query = serializeListOptions(sanitizedOptions);
1126
- const res = await doFetch(`${base}/agents`, {
1127
- method: "GET",
1128
- query
1129
- });
1130
- return res.json();
1131
- };
1132
- const listAgents = async (options = {}) => {
1133
- const normalizedOptions = { ...options ?? {} };
1134
- const response = await fetchAgentsPage(normalizedOptions);
1135
- return attachPaginator(response, fetchAgentsPage, normalizedOptions);
1136
- };
1137
- const getAgentDetail = async (agentId) => {
1138
- requireWorkspace();
1139
- const res = await doFetch(`${base}/agents/${agentId}`, {
1140
- method: "GET"
1141
- });
1142
- return res.json();
1143
- };
1144
- const createAgent = async (payload) => {
1145
- requireWorkspace();
1146
- const res = await doFetch(`${base}/agents`, {
1270
+ const res = await doFetch(url, {
1147
1271
  method: "POST",
1148
1272
  body: JSON.stringify(payload),
1149
1273
  headers: jsonHeaders
1150
1274
  });
1151
1275
  return res.json();
1152
1276
  };
1153
- const forkAgentFromTemplate = async (payload) => {
1277
+ const postWithSse = async (url, payload, callbacks) => {
1154
1278
  requireWorkspace();
1155
- const res = await doFetch(`${base}/agents/from-template`, {
1279
+ const headers = buildHeaders({ "content-type": "application/json" });
1280
+ const fx = cfg.fetchImpl ?? fetch;
1281
+ const res = await fx(url, {
1156
1282
  method: "POST",
1157
1283
  body: JSON.stringify(payload),
1158
- headers: jsonHeaders
1284
+ headers
1159
1285
  });
1160
- return res.json();
1286
+ if (!res.ok) {
1287
+ const body = await res.text();
1288
+ throw new Error(`POST ${url} failed with status ${res.status}: ${body}`);
1289
+ }
1290
+ const contentType = res.headers.get("content-type") ?? "";
1291
+ if (contentType.includes("application/json")) {
1292
+ const agent = await res.json();
1293
+ callbacks.onAgentCreated?.(agent);
1294
+ return agent;
1295
+ }
1296
+ return consumeSseStream(
1297
+ res,
1298
+ buildSseHandlers(callbacks),
1299
+ "agent-created"
1300
+ );
1161
1301
  };
1162
- const updateAgent = async (agentId, payload) => {
1302
+ const getWithSse = async (url, callbacks) => {
1163
1303
  requireWorkspace();
1164
- const res = await doFetch(`${base}/agents/${agentId}`, {
1165
- method: "PATCH",
1166
- body: JSON.stringify(payload),
1167
- headers: jsonHeaders
1168
- });
1169
- return res.json();
1170
- };
1171
- const resolveAgentId = (agent) => {
1172
- return typeof agent === "string" ? agent : agent.agentId;
1304
+ const headers = buildHeaders({});
1305
+ const fx = cfg.fetchImpl ?? fetch;
1306
+ const res = await fx(url, { method: "GET", headers });
1307
+ if (!res.ok) {
1308
+ const body = await res.text();
1309
+ throw new Error(`GET ${url} failed with status ${res.status}: ${body}`);
1310
+ }
1311
+ const contentType = res.headers.get("content-type") ?? "";
1312
+ if (contentType.includes("application/json")) {
1313
+ const agent = await res.json();
1314
+ callbacks.onAgentCreated?.(agent);
1315
+ return agent;
1316
+ }
1317
+ return consumeSseStream(
1318
+ res,
1319
+ buildSseHandlers(callbacks),
1320
+ "agent"
1321
+ );
1173
1322
  };
1174
- const deleteAgent = async (agent) => {
1323
+ const fetchPage = async (options = {}) => {
1175
1324
  requireWorkspace();
1176
- const agentId = resolveAgentId(agent);
1177
- await doFetch(`${base}/agents/${agentId}`, {
1178
- method: "DELETE"
1325
+ const query = serializeListOptions({
1326
+ page: options.page,
1327
+ limit: options.limit,
1328
+ filter: options.filter
1179
1329
  });
1330
+ const res = await doFetch(`${base}/agents`, { method: "GET", query });
1331
+ return res.json();
1180
1332
  };
1181
1333
  const baseApi = {
1182
- list: listAgents,
1183
- get: getAgentDetail,
1184
- create: createAgent,
1185
- forkFromTemplate: forkAgentFromTemplate,
1186
- update: updateAgent,
1187
- delete: deleteAgent
1334
+ async list(options = {}) {
1335
+ const opts = { ...options ?? {} };
1336
+ const response = await fetchPage(opts);
1337
+ return attachPaginator(response, fetchPage, opts);
1338
+ },
1339
+ async get(agentId, callbacks) {
1340
+ if (callbacks) {
1341
+ return getWithSse(`${base}/agents/${agentId}`, callbacks);
1342
+ }
1343
+ requireWorkspace();
1344
+ const res = await doFetch(`${base}/agents/${agentId}`, { method: "GET" });
1345
+ const contentType = res.headers.get("content-type") ?? "";
1346
+ if (contentType.includes("text/event-stream")) {
1347
+ return consumeSseStream(
1348
+ res,
1349
+ buildSseHandlers({}),
1350
+ "agent",
1351
+ {
1352
+ resolveImmediately: true
1353
+ }
1354
+ );
1355
+ }
1356
+ return res.json();
1357
+ },
1358
+ async create(payload, callbacks) {
1359
+ if (callbacks) {
1360
+ return postWithSse(`${base}/agents`, payload, callbacks);
1361
+ }
1362
+ return postJson(`${base}/agents`, payload);
1363
+ },
1364
+ async clone(agentId, payload) {
1365
+ return postJson(
1366
+ `${base}/agents/${agentId}/clone`,
1367
+ buildCloneRequest(payload)
1368
+ );
1369
+ },
1370
+ async forkFromTemplate(payload, callbacks) {
1371
+ if (callbacks) {
1372
+ return postWithSse(`${base}/agents/from-template`, payload, callbacks);
1373
+ }
1374
+ return postJson(`${base}/agents/from-template`, payload);
1375
+ },
1376
+ async rebuild(agentId, callbacks) {
1377
+ if (callbacks) {
1378
+ return postWithSse(`${base}/agents/${agentId}/rebuild`, {}, callbacks);
1379
+ }
1380
+ return postJson(`${base}/agents/${agentId}/rebuild`, {});
1381
+ },
1382
+ async update(agentId, payload) {
1383
+ requireWorkspace();
1384
+ const res = await doFetch(`${base}/agents/${agentId}`, {
1385
+ method: "PATCH",
1386
+ body: JSON.stringify(payload),
1387
+ headers: jsonHeaders
1388
+ });
1389
+ return res.json();
1390
+ },
1391
+ async delete(agent) {
1392
+ requireWorkspace();
1393
+ await doFetch(`${base}/agents/${resolveAgentId(agent)}`, {
1394
+ method: "DELETE"
1395
+ });
1396
+ }
1188
1397
  };
1189
1398
  if (!relatedApis) {
1190
1399
  return baseApi;
1191
1400
  }
1401
+ return withEntityWrapping(baseApi, fetchPage, relatedApis);
1402
+ }
1403
+ function withEntityWrapping(api, fetchPage, deps) {
1192
1404
  const wrapAgent = (detail) => createAgentEntity(detail, {
1193
- tagsApi: relatedApis.tagsApi,
1194
- phonesApi: relatedApis.phonesApi,
1195
- scheduleApi: relatedApis.scheduleApi,
1196
- scheduleExceptionsApi: relatedApis.scheduleExceptionsApi,
1197
- versionsApi: relatedApis.versionsApi,
1198
- blueprintsApi: relatedApis.blueprintsApi,
1199
- stagesApi: relatedApis.stagesApi,
1200
- stageTriggersApi: relatedApis.stageTriggersApi,
1405
+ ...deps,
1201
1406
  reload: async (agentId) => {
1202
- const latest = await getAgentDetail(agentId);
1407
+ const latest = await api.get(agentId);
1203
1408
  return wrapAgent(latest);
1204
1409
  },
1205
1410
  updateAgent: async (agentId, payload) => {
1206
- const updated = await updateAgent(agentId, payload);
1411
+ const updated = await api.update(agentId, payload);
1207
1412
  return wrapAgent(updated);
1208
1413
  },
1209
1414
  deleteAgent: async (agentId) => {
1210
- await deleteAgent(agentId);
1415
+ await api.delete(agentId);
1416
+ },
1417
+ cloneAgent: async (agentId, payload) => {
1418
+ const cloned = await api.clone(agentId, payload);
1419
+ return wrapAgent(cloned);
1211
1420
  }
1212
1421
  });
1422
+ const wrapList = async (opts) => {
1423
+ const result = await fetchPage(opts);
1424
+ const items = Array.isArray(result.data) ? result.data : [];
1425
+ return { ...result, data: items.map(wrapAgent) };
1426
+ };
1213
1427
  return {
1214
- ...baseApi,
1428
+ ...api,
1215
1429
  async list(options = {}) {
1216
- const normalizedOptions = { ...options ?? {} };
1217
- const applyWrap = async (opts) => {
1218
- const result = await fetchAgentsPage(opts);
1219
- const items = Array.isArray(result.data) ? result.data : [];
1220
- return {
1221
- ...result,
1222
- data: items.map((summary) => wrapAgent(summary))
1223
- };
1224
- };
1225
- const initial = await applyWrap(normalizedOptions);
1226
- return attachPaginator(initial, applyWrap, normalizedOptions);
1430
+ const opts = { ...options ?? {} };
1431
+ const initial = await wrapList(opts);
1432
+ return attachPaginator(initial, wrapList, opts);
1227
1433
  },
1228
- async get(agentId) {
1229
- const detail = await getAgentDetail(agentId);
1230
- return wrapAgent(detail);
1434
+ async get(agentId, callbacks) {
1435
+ return wrapAgent(await api.get(agentId, callbacks));
1231
1436
  },
1232
- async create(payload) {
1233
- const detail = await createAgent(payload);
1234
- return wrapAgent(detail);
1437
+ async create(payload, callbacks) {
1438
+ return wrapAgent(await api.create(payload, callbacks));
1439
+ },
1440
+ async clone(agentId, payload) {
1441
+ return wrapAgent(await api.clone(agentId, payload));
1235
1442
  },
1236
- async forkFromTemplate(payload) {
1237
- const detail = await forkAgentFromTemplate(payload);
1238
- return wrapAgent(detail);
1443
+ async forkFromTemplate(payload, callbacks) {
1444
+ return wrapAgent(await api.forkFromTemplate(payload, callbacks));
1239
1445
  },
1240
1446
  async update(agentId, payload) {
1241
- const detail = await updateAgent(agentId, payload);
1242
- return wrapAgent(detail);
1447
+ return wrapAgent(await api.update(agentId, payload));
1243
1448
  }
1244
1449
  };
1245
1450
  }
@@ -1275,6 +1480,82 @@ function createApiKeysApi(cfg) {
1275
1480
  };
1276
1481
  }
1277
1482
 
1483
+ // src/api/billing.ts
1484
+ function createBillingApi(cfg) {
1485
+ const { base, doFetch } = createHttp(cfg);
1486
+ return {
1487
+ async getBalance() {
1488
+ const res = await doFetch(`${base}/billing/balance`, {
1489
+ method: "GET"
1490
+ });
1491
+ return res.json();
1492
+ }
1493
+ };
1494
+ }
1495
+
1496
+ // src/api/calls.ts
1497
+ function createCallsApi(cfg) {
1498
+ const { base, doFetch } = createHttp(cfg);
1499
+ const fetchCallsPage = async (options = {}) => {
1500
+ const {
1501
+ agentBatchId,
1502
+ agentId,
1503
+ executionId,
1504
+ recordedAfter,
1505
+ recordedBefore,
1506
+ durationBucket,
1507
+ goalStatus,
1508
+ query: searchQuery,
1509
+ ...listOptions
1510
+ } = options ?? {};
1511
+ const query = serializeListOptions(listOptions, {
1512
+ agentBatchId,
1513
+ agentId,
1514
+ executionId,
1515
+ recordedAfter,
1516
+ recordedBefore,
1517
+ durationBucket,
1518
+ goalStatus,
1519
+ query: searchQuery
1520
+ });
1521
+ const res = await doFetch(`${base}/calls`, {
1522
+ method: "GET",
1523
+ query
1524
+ });
1525
+ return res.json();
1526
+ };
1527
+ return {
1528
+ async list(options = {}) {
1529
+ const normalizedOptions = { ...options ?? {} };
1530
+ const response = await fetchCallsPage(normalizedOptions);
1531
+ return attachPaginator(response, fetchCallsPage, normalizedOptions);
1532
+ },
1533
+ async stream(options) {
1534
+ const query = buildSearchParams([
1535
+ ["after", options.after],
1536
+ ["limit", options.limit],
1537
+ ["agentBatchId", options.agentBatchId],
1538
+ ["agentId", options.agentId],
1539
+ ["executionId", options.executionId],
1540
+ ["durationBucket", options.durationBucket],
1541
+ ["goalStatus", options.goalStatus],
1542
+ ["query", options.query]
1543
+ ]);
1544
+ const res = await doFetch(`${base}/calls/stream`, {
1545
+ method: "GET",
1546
+ query
1547
+ });
1548
+ return res.json();
1549
+ },
1550
+ async get(callId) {
1551
+ const res = await doFetch(`${base}/calls/${callId}`, {
1552
+ method: "GET"
1553
+ });
1554
+ return res.json();
1555
+ }
1556
+ };
1557
+ }
1558
+
1278
1559
  // src/api/campaigns.ts
1279
1560
  function createCampaignsApi(cfg) {
1280
1561
  const { base, doFetch } = createHttp(cfg);
@@ -1290,10 +1571,7 @@ function createCampaignsApi(cfg) {
1290
1571
  return res.json();
1291
1572
  };
1292
1573
  const fetchExecutionsPage = async (campaignId, options = {}) => {
1293
- const query = serializeListOptions({
1294
- page: options.page,
1295
- limit: options.limit
1296
- });
1574
+ const query = serializeListOptions(options ?? {});
1297
1575
  const res = await doFetch(`${base}/campaigns/${campaignId}/executions`, {
1298
1576
  method: "GET",
1299
1577
  query
@@ -1347,6 +1625,51 @@ function createCampaignsApi(cfg) {
1347
1625
  const response = await fetchExecutionsPage(campaignId, normalizedOptions);
1348
1626
  const fetchPage = (opts) => fetchExecutionsPage(campaignId, opts);
1349
1627
  return attachPaginator(response, fetchPage, normalizedOptions);
1628
+ },
1629
+ /**
1630
+ * Pausa una campaña en curso. Los reintentos ya programados se
1631
+ * reagendan al ejecutarse (sin consumir intentos) hasta `resume()`.
1632
+ */
1633
+ async pause(campaignId) {
1634
+ const res = await doFetch(`${base}/campaigns/${campaignId}/pause`, {
1635
+ method: "POST"
1636
+ });
1637
+ return res.json();
1638
+ },
1639
+ /**
1640
+ * Reanuda una campaña pausada. Re-encola los reintentos pendientes
1641
+ * según su `nextRetryAt`.
1642
+ */
1643
+ async resume(campaignId) {
1644
+ const res = await doFetch(`${base}/campaigns/${campaignId}/resume`, {
1645
+ method: "POST"
1646
+ });
1647
+ return res.json();
1648
+ },
1649
+ /**
1650
+ * Cancela definitivamente una campaña no terminal. Los reintentos
1651
+ * pendientes se descartan — operación destructiva.
1652
+ */
1653
+ async cancel(campaignId) {
1654
+ const res = await doFetch(`${base}/campaigns/${campaignId}/cancel`, {
1655
+ method: "POST"
1656
+ });
1657
+ return res.json();
1658
+ },
1659
+ /**
1660
+ * Descarga el CSV con todas las ejecuciones de la campaña. Devuelve un
1661
+ * `Blob` con `text/csv; charset=utf-8` (BOM UTF-8 incluido). Para
1662
+ * campañas > 20,000 filas la API responde 413.
1663
+ */
1664
+ async exportExecutions(campaignId) {
1665
+ const res = await doFetch(
1666
+ `${base}/campaigns/${campaignId}/executions/export`,
1667
+ {
1668
+ method: "GET",
1669
+ headers: { Accept: "text/csv" }
1670
+ }
1671
+ );
1672
+ return res.blob();
1350
1673
  }
1351
1674
  };
1352
1675
  }
@@ -1427,6 +1750,70 @@ function createCatalogsApi(cfg) {
1427
1750
  };
1428
1751
  }
1429
1752
 
1753
+ // src/api/documents.ts
1754
+ function createDocumentsApi(cfg) {
1755
+ const { base, doFetch, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1756
+ const jsonHeaders = { "content-type": "application/json" };
1757
+ const requireWorkspace = () => {
1758
+ const workspaceId = resolveWorkspaceId();
1759
+ if (typeof workspaceId === "string" && workspaceId.trim().length > 0) {
1760
+ return;
1761
+ }
1762
+ const apiKey = resolveApiKey();
1763
+ if (typeof apiKey === "string" && apiKey.trim().length > 0) {
1764
+ return;
1765
+ }
1766
+ throw new WorkspaceNotSelectedError();
1767
+ };
1768
+ return {
1769
+ async create(files) {
1770
+ requireWorkspace();
1771
+ const res = await doFetch(`${base}/documents`, {
1772
+ method: "POST",
1773
+ body: JSON.stringify({ files }),
1774
+ headers: jsonHeaders
1775
+ });
1776
+ return res.json();
1777
+ }
1778
+ };
1779
+ }
1780
+
1781
+ // src/api/sip-trunks.ts
1782
+ function createSipTrunksApi(cfg) {
1783
+ const { base, doFetch } = createHttp(cfg);
1784
+ return {
1785
+ async list() {
1786
+ const res = await doFetch(`${base}/sip/trunks`, { method: "GET" });
1787
+ return res.json();
1788
+ },
1789
+ async get(trunkId) {
1790
+ const res = await doFetch(`${base}/sip/trunks/${trunkId}`, {
1791
+ method: "GET"
1792
+ });
1793
+ return res.json();
1794
+ },
1795
+ async create(body) {
1796
+ const res = await doFetch(`${base}/sip/trunks`, {
1797
+ method: "POST",
1798
+ headers: { "Content-Type": "application/json" },
1799
+ body: JSON.stringify(body)
1800
+ });
1801
+ return res.json();
1802
+ },
1803
+ async update(trunkId, body) {
1804
+ const res = await doFetch(`${base}/sip/trunks/${trunkId}`, {
1805
+ method: "PATCH",
1806
+ headers: { "Content-Type": "application/json" },
1807
+ body: JSON.stringify(body)
1808
+ });
1809
+ return res.json();
1810
+ },
1811
+ async delete(trunkId) {
1812
+ await doFetch(`${base}/sip/trunks/${trunkId}`, { method: "DELETE" });
1813
+ }
1814
+ };
1815
+ }
1816
+
1430
1817
  // src/api/tools.ts
1431
1818
  var IDEMPOTENCY_HEADER = "Idempotency-Key";
1432
1819
  var generateIdempotencyKey = (explicit) => {
@@ -1616,6 +2003,74 @@ function createToolsApi(cfg) {
1616
2003
  };
1617
2004
  }
1618
2005
 
2006
+ // src/api/usage.ts
2007
+ import { Query } from "@getsupervisor/api-query-builder";
2008
+ function createUsageApi(cfg) {
2009
+ const { base, doFetch } = createHttp(cfg);
2010
+ const fetchUsageAgentsPage = async (options = {}) => {
2011
+ const { resource, from, to, ...listOptions } = options;
2012
+ const query = serializeListOptions({
2013
+ ...listOptions,
2014
+ filter: resolveFilter(
2015
+ buildUsageFilter(resource, from, to),
2016
+ listOptions.filter
2017
+ )
2018
+ });
2019
+ const res = await doFetch(`${base}/usage/agents`, {
2020
+ method: "GET",
2021
+ query
2022
+ });
2023
+ return res.json();
2024
+ };
2025
+ return {
2026
+ async agents(options = {}) {
2027
+ const normalizedOptions = {
2028
+ ...options
2029
+ };
2030
+ const response = await fetchUsageAgentsPage(normalizedOptions);
2031
+ return attachPaginator(response, fetchUsageAgentsPage, normalizedOptions);
2032
+ }
2033
+ };
2034
+ }
2035
+ function resolveFilter(usageFilter, fallback) {
2036
+ if (usageFilter) {
2037
+ return usageFilter;
2038
+ }
2039
+ return fallback;
2040
+ }
2041
+ function appendCondition(query, factory) {
2042
+ if (query) {
2043
+ query.and(factory);
2044
+ return query;
2045
+ }
2046
+ return new Query(factory);
2047
+ }
2048
+ var DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
2049
+ function toISOString(value) {
2050
+ const input = DATE_ONLY.test(value) ? `${value}T00:00:00` : value;
2051
+ const date = new Date(input);
2052
+ if (Number.isNaN(date.getTime())) {
2053
+ return value;
2054
+ }
2055
+ return date.toISOString();
2056
+ }
2057
+ function buildUsageFilter(resource, from, to) {
2058
+ let query;
2059
+ if (resource) {
2060
+ query = appendCondition(query, (qb) => qb.eq("resource", resource));
2061
+ }
2062
+ if (from) {
2063
+ query = appendCondition(
2064
+ query,
2065
+ (qb) => qb.mte("createdAt", toISOString(from))
2066
+ );
2067
+ }
2068
+ if (to) {
2069
+ query = appendCondition(query, (qb) => qb.lt("createdAt", toISOString(to)));
2070
+ }
2071
+ return query;
2072
+ }
2073
+
1619
2074
  // src/utils/catalog-voices.ts
1620
2075
  var FALLBACK_LOCALE = "und";
1621
2076
  var FALLBACK_PROVIDER = "catalog";
@@ -1674,9 +2129,9 @@ function pickGender(value) {
1674
2129
  }
1675
2130
 
1676
2131
  // src/utils/catalog-filter.ts
1677
- import { Query } from "@getsupervisor/api-query-builder";
2132
+ import { Query as Query2 } from "@getsupervisor/api-query-builder";
1678
2133
  function createCatalogTypeQuery(type) {
1679
- return new Query((qb) => qb.eq("type", type));
2134
+ return new Query2((qb) => qb.eq("type", type));
1680
2135
  }
1681
2136
  function ensureCatalogTypeFilter(filter, type) {
1682
2137
  const requiredQuery = createCatalogTypeQuery(type);
@@ -1912,6 +2367,35 @@ function createWebhooksApi(cfg) {
1912
2367
  `${base}/webhooks/${webhookId}/subscriptions/${subscriptionId}`,
1913
2368
  { method: "DELETE" }
1914
2369
  );
2370
+ },
2371
+ // Deliveries
2372
+ async listDeliveries(webhookId, options = {}) {
2373
+ const fetchPage = async (opts) => {
2374
+ const query = serializeListOptions({
2375
+ page: opts.page,
2376
+ limit: opts.limit,
2377
+ sort: opts.sort,
2378
+ fields: opts.fields,
2379
+ include: opts.include,
2380
+ search: opts.search,
2381
+ filter: opts.filter
2382
+ });
2383
+ const res = await doFetch(`${base}/webhooks/${webhookId}/deliveries`, {
2384
+ method: "GET",
2385
+ query
2386
+ });
2387
+ return res.json();
2388
+ };
2389
+ const normalized = { ...options ?? {} };
2390
+ const response = await fetchPage(normalized);
2391
+ return attachPaginator(response, fetchPage, normalized);
2392
+ },
2393
+ async getDelivery(webhookId, deliveryId) {
2394
+ const res = await doFetch(
2395
+ `${base}/webhooks/${webhookId}/deliveries/${deliveryId}`,
2396
+ { method: "GET" }
2397
+ );
2398
+ return res.json();
1915
2399
  }
1916
2400
  };
1917
2401
  }
@@ -1975,8 +2459,8 @@ function createWorkspacesApi(cfg) {
1975
2459
  const fetchPage = (options) => fetchPhonesPage(workspaceId, options);
1976
2460
  return attachPaginator(response, fetchPage, normalizedOptions);
1977
2461
  },
1978
- async enable(workspaceId, payload) {
1979
- const res = await doFetch(`${base}/workspaces/${workspaceId}/enable`, {
2462
+ async enable(payload) {
2463
+ const res = await doFetch(`${base}/workspaces/enable`, {
1980
2464
  method: "POST",
1981
2465
  headers: jsonHeaders,
1982
2466
  body: JSON.stringify(payload)
@@ -2022,6 +2506,7 @@ function createClient(initialCfg) {
2022
2506
  const stagesApi = createAgentStagesApi(runtimeCfg);
2023
2507
  const voicesApi = createVoicesApi(runtimeCfg);
2024
2508
  const apiKeysApi = createApiKeysApi(runtimeCfg);
2509
+ const callsApi = createCallsApi(runtimeCfg);
2025
2510
  const catalogsApi = createCatalogsApi(runtimeCfg);
2026
2511
  const catalogTemplatesApi = createCatalogTemplatesApi(runtimeCfg);
2027
2512
  const webhooksApi = createWebhooksApi(runtimeCfg);
@@ -2090,9 +2575,14 @@ function createClient(initialCfg) {
2090
2575
  ...catalogsApi,
2091
2576
  templates: catalogTemplatesApi
2092
2577
  },
2578
+ documents: createDocumentsApi(runtimeCfg),
2093
2579
  campaigns: createCampaignsApi(runtimeCfg),
2094
2580
  voices: voicesApi,
2095
2581
  apiKeys: apiKeysApi,
2582
+ billing: createBillingApi(runtimeCfg),
2583
+ calls: callsApi,
2584
+ usage: createUsageApi(runtimeCfg),
2585
+ sip: createSipTrunksApi(runtimeCfg),
2096
2586
  webhooks: webhooksApi
2097
2587
  };
2098
2588
  return {
@@ -2175,12 +2665,17 @@ export {
2175
2665
  createAgentVersionsApi,
2176
2666
  createAgentsApi,
2177
2667
  createApiKeysApi,
2668
+ createBillingApi,
2669
+ createCallsApi,
2178
2670
  createCampaignsApi,
2179
2671
  createCatalogTemplatesApi,
2180
2672
  createCatalogsApi,
2181
2673
  createClient,
2674
+ createDocumentsApi,
2182
2675
  createHttp,
2676
+ createSipTrunksApi,
2183
2677
  createToolsApi,
2678
+ createUsageApi,
2184
2679
  createVoicesApi,
2185
2680
  createWebhooksApi,
2186
2681
  createWorkspacesApi,