@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.cjs CHANGED
@@ -43,12 +43,17 @@ __export(index_exports, {
43
43
  createAgentVersionsApi: () => createAgentVersionsApi,
44
44
  createAgentsApi: () => createAgentsApi,
45
45
  createApiKeysApi: () => createApiKeysApi,
46
+ createBillingApi: () => createBillingApi,
47
+ createCallsApi: () => createCallsApi,
46
48
  createCampaignsApi: () => createCampaignsApi,
47
49
  createCatalogTemplatesApi: () => createCatalogTemplatesApi,
48
50
  createCatalogsApi: () => createCatalogsApi,
49
51
  createClient: () => createClient,
52
+ createDocumentsApi: () => createDocumentsApi,
50
53
  createHttp: () => createHttp,
54
+ createSipTrunksApi: () => createSipTrunksApi,
51
55
  createToolsApi: () => createToolsApi,
56
+ createUsageApi: () => createUsageApi,
52
57
  createVoicesApi: () => createVoicesApi,
53
58
  createWebhooksApi: () => createWebhooksApi,
54
59
  createWorkspacesApi: () => createWorkspacesApi,
@@ -238,7 +243,8 @@ function createHttp(cfg) {
238
243
  }
239
244
  return res;
240
245
  } catch (e) {
241
- if (e.name === "AbortError") throw new TimeoutError(timeout, targetUrl);
246
+ if (e?.name === "AbortError")
247
+ throw new TimeoutError(timeout, targetUrl);
242
248
  if (e instanceof HttpError) throw e;
243
249
  throw new NetworkError(e, targetUrl);
244
250
  }
@@ -259,6 +265,11 @@ function createHttp(cfg) {
259
265
  }
260
266
 
261
267
  // src/utils/query.ts
268
+ function buildSearchParams(entries) {
269
+ const searchParams = new URLSearchParams();
270
+ entries.forEach(([key, value]) => appendParam(searchParams, key, value));
271
+ return searchParams;
272
+ }
262
273
  function serializeListOptions(options = {}, extra = {}) {
263
274
  const params = new URLSearchParams();
264
275
  appendParam(params, "page", options.page);
@@ -944,6 +955,117 @@ function createAgentVersionsApi(cfg) {
944
955
  };
945
956
  }
946
957
 
958
+ // src/utils/sse-stream.ts
959
+ function extractField(line, prefix) {
960
+ if (!line.startsWith(prefix)) {
961
+ return null;
962
+ }
963
+ return line.slice(prefix.length);
964
+ }
965
+ function parseSseMessage(raw) {
966
+ let type = "";
967
+ let data = "";
968
+ for (const line of raw.split("\n")) {
969
+ type = extractField(line, "event: ") ?? type;
970
+ data = extractField(line, "data: ") ?? data;
971
+ }
972
+ if (!type || !data) {
973
+ return null;
974
+ }
975
+ return { type, data };
976
+ }
977
+ function dispatchSseEvent(event, handlers) {
978
+ const handler = handlers[event.type];
979
+ if (!handler) {
980
+ return;
981
+ }
982
+ try {
983
+ const parsed = JSON.parse(event.data);
984
+ handler(parsed);
985
+ return parsed;
986
+ } catch {
987
+ return;
988
+ }
989
+ }
990
+ function processRawPart(raw, handlers, resolveEvent) {
991
+ const trimmed = raw.trim();
992
+ if (!trimmed) {
993
+ return;
994
+ }
995
+ const event = parseSseMessage(trimmed);
996
+ if (!event) {
997
+ return;
998
+ }
999
+ const result = dispatchSseEvent(event, handlers);
1000
+ if (event.type !== resolveEvent) {
1001
+ return;
1002
+ }
1003
+ return result;
1004
+ }
1005
+ function flushRemainingBuffer(buffer) {
1006
+ if (!buffer.trim()) {
1007
+ return "";
1008
+ }
1009
+ return buffer + "\n\n";
1010
+ }
1011
+ function consumeSseStream(response, handlers, resolveEvent, options = {}) {
1012
+ if (!response.body) {
1013
+ throw new Error("No response body for SSE stream");
1014
+ }
1015
+ return new Promise((resolve, reject) => {
1016
+ let resolved;
1017
+ let settled = false;
1018
+ let buffer = "";
1019
+ const reader = response.body.getReader();
1020
+ const decoder = new TextDecoder();
1021
+ const processBuffer = () => {
1022
+ const parts = buffer.split("\n\n");
1023
+ buffer = parts.pop() ?? "";
1024
+ for (const part of parts) {
1025
+ const result = processRawPart(part, handlers, resolveEvent);
1026
+ if (result) {
1027
+ resolved = result;
1028
+ }
1029
+ }
1030
+ };
1031
+ const tryResolveImmediately = () => {
1032
+ if (!options.resolveImmediately || resolved === void 0 || settled) {
1033
+ return false;
1034
+ }
1035
+ settled = true;
1036
+ resolve(resolved);
1037
+ reader.cancel().catch(() => void 0);
1038
+ return true;
1039
+ };
1040
+ const finalize = () => {
1041
+ if (settled) return;
1042
+ buffer = flushRemainingBuffer(buffer);
1043
+ processBuffer();
1044
+ if (resolved) {
1045
+ resolve(resolved);
1046
+ return;
1047
+ }
1048
+ reject(new Error(`SSE stream ended without '${resolveEvent}' event`));
1049
+ };
1050
+ const read = () => {
1051
+ if (settled) return;
1052
+ reader.read().then(({ done, value }) => {
1053
+ if (done) {
1054
+ finalize();
1055
+ return;
1056
+ }
1057
+ buffer += decoder.decode(value, { stream: true });
1058
+ processBuffer();
1059
+ if (tryResolveImmediately()) {
1060
+ return;
1061
+ }
1062
+ read();
1063
+ }).catch(reject);
1064
+ };
1065
+ read();
1066
+ });
1067
+ }
1068
+
947
1069
  // src/entities/agent.ts
948
1070
  var bindAgentStageTriggers = (api, agentId, blueprintId, stageId) => ({
949
1071
  list(opts) {
@@ -1130,7 +1252,8 @@ var createAgentEntity = (dto, options) => {
1130
1252
  stageTriggersApi,
1131
1253
  reload,
1132
1254
  updateAgent,
1133
- deleteAgent
1255
+ deleteAgent,
1256
+ cloneAgent
1134
1257
  } = options;
1135
1258
  const schedulesHelper = bindAgentSchedules(
1136
1259
  scheduleApi,
@@ -1149,6 +1272,9 @@ var createAgentEntity = (dto, options) => {
1149
1272
  async save(patch) {
1150
1273
  return updateAgent(dto.agentId, patch);
1151
1274
  },
1275
+ async clone(payload) {
1276
+ return cloneAgent(dto.agentId, payload);
1277
+ },
1152
1278
  async delete() {
1153
1279
  await deleteAgent(dto.agentId);
1154
1280
  },
@@ -1160,8 +1286,38 @@ var createAgentEntity = (dto, options) => {
1160
1286
  };
1161
1287
 
1162
1288
  // src/api/agents.ts
1289
+ function normalizeCloneSelection(selection) {
1290
+ if (!selection) {
1291
+ return [];
1292
+ }
1293
+ if (Array.isArray(selection)) {
1294
+ return selection;
1295
+ }
1296
+ return Object.entries(selection).filter(([, enabled]) => enabled).map(([component]) => component);
1297
+ }
1298
+ function buildCloneRequest(payload) {
1299
+ const { clone, ...rest } = payload;
1300
+ return { ...rest, clone: normalizeCloneSelection(clone) };
1301
+ }
1302
+ function buildSseHandlers(callbacks) {
1303
+ const onAgent = (data) => callbacks.onAgentCreated?.(data);
1304
+ return {
1305
+ "agent-created": onAgent,
1306
+ agent: onAgent,
1307
+ "runtime-progress": (data) => callbacks.onProgress?.(data),
1308
+ "runtime-complete": (data) => callbacks.onComplete?.(data),
1309
+ "runtime-error": (data) => callbacks.onError?.(data)
1310
+ };
1311
+ }
1312
+ function resolveAgentId(agent) {
1313
+ if (typeof agent === "string") {
1314
+ return agent;
1315
+ }
1316
+ return agent.agentId;
1317
+ }
1163
1318
  function createAgentsApi(cfg, relatedApis) {
1164
- const { base, doFetch, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1319
+ const { base, doFetch, buildHeaders, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1320
+ const jsonHeaders = { "content-type": "application/json" };
1165
1321
  const requireWorkspace = () => {
1166
1322
  const workspaceId = resolveWorkspaceId();
1167
1323
  if (typeof workspaceId === "string" && workspaceId.trim().length > 0) {
@@ -1173,132 +1329,186 @@ function createAgentsApi(cfg, relatedApis) {
1173
1329
  }
1174
1330
  throw new WorkspaceNotSelectedError();
1175
1331
  };
1176
- const jsonHeaders = { "content-type": "application/json" };
1177
- const fetchAgentsPage = async (options = {}) => {
1332
+ const postJson = async (url, payload) => {
1178
1333
  requireWorkspace();
1179
- const sanitizedOptions = {
1180
- page: options.page,
1181
- limit: options.limit,
1182
- filter: options.filter
1183
- };
1184
- const query = serializeListOptions(sanitizedOptions);
1185
- const res = await doFetch(`${base}/agents`, {
1186
- method: "GET",
1187
- query
1188
- });
1189
- return res.json();
1190
- };
1191
- const listAgents = async (options = {}) => {
1192
- const normalizedOptions = { ...options ?? {} };
1193
- const response = await fetchAgentsPage(normalizedOptions);
1194
- return attachPaginator(response, fetchAgentsPage, normalizedOptions);
1195
- };
1196
- const getAgentDetail = async (agentId) => {
1197
- requireWorkspace();
1198
- const res = await doFetch(`${base}/agents/${agentId}`, {
1199
- method: "GET"
1200
- });
1201
- return res.json();
1202
- };
1203
- const createAgent = async (payload) => {
1204
- requireWorkspace();
1205
- const res = await doFetch(`${base}/agents`, {
1334
+ const res = await doFetch(url, {
1206
1335
  method: "POST",
1207
1336
  body: JSON.stringify(payload),
1208
1337
  headers: jsonHeaders
1209
1338
  });
1210
1339
  return res.json();
1211
1340
  };
1212
- const forkAgentFromTemplate = async (payload) => {
1341
+ const postWithSse = async (url, payload, callbacks) => {
1213
1342
  requireWorkspace();
1214
- const res = await doFetch(`${base}/agents/from-template`, {
1343
+ const headers = buildHeaders({ "content-type": "application/json" });
1344
+ const fx = cfg.fetchImpl ?? fetch;
1345
+ const res = await fx(url, {
1215
1346
  method: "POST",
1216
1347
  body: JSON.stringify(payload),
1217
- headers: jsonHeaders
1348
+ headers
1218
1349
  });
1219
- return res.json();
1350
+ if (!res.ok) {
1351
+ const body = await res.text();
1352
+ throw new Error(`POST ${url} failed with status ${res.status}: ${body}`);
1353
+ }
1354
+ const contentType = res.headers.get("content-type") ?? "";
1355
+ if (contentType.includes("application/json")) {
1356
+ const agent = await res.json();
1357
+ callbacks.onAgentCreated?.(agent);
1358
+ return agent;
1359
+ }
1360
+ return consumeSseStream(
1361
+ res,
1362
+ buildSseHandlers(callbacks),
1363
+ "agent-created"
1364
+ );
1220
1365
  };
1221
- const updateAgent = async (agentId, payload) => {
1366
+ const getWithSse = async (url, callbacks) => {
1222
1367
  requireWorkspace();
1223
- const res = await doFetch(`${base}/agents/${agentId}`, {
1224
- method: "PATCH",
1225
- body: JSON.stringify(payload),
1226
- headers: jsonHeaders
1227
- });
1228
- return res.json();
1229
- };
1230
- const resolveAgentId = (agent) => {
1231
- return typeof agent === "string" ? agent : agent.agentId;
1368
+ const headers = buildHeaders({});
1369
+ const fx = cfg.fetchImpl ?? fetch;
1370
+ const res = await fx(url, { method: "GET", headers });
1371
+ if (!res.ok) {
1372
+ const body = await res.text();
1373
+ throw new Error(`GET ${url} failed with status ${res.status}: ${body}`);
1374
+ }
1375
+ const contentType = res.headers.get("content-type") ?? "";
1376
+ if (contentType.includes("application/json")) {
1377
+ const agent = await res.json();
1378
+ callbacks.onAgentCreated?.(agent);
1379
+ return agent;
1380
+ }
1381
+ return consumeSseStream(
1382
+ res,
1383
+ buildSseHandlers(callbacks),
1384
+ "agent"
1385
+ );
1232
1386
  };
1233
- const deleteAgent = async (agent) => {
1387
+ const fetchPage = async (options = {}) => {
1234
1388
  requireWorkspace();
1235
- const agentId = resolveAgentId(agent);
1236
- await doFetch(`${base}/agents/${agentId}`, {
1237
- method: "DELETE"
1389
+ const query = serializeListOptions({
1390
+ page: options.page,
1391
+ limit: options.limit,
1392
+ filter: options.filter
1238
1393
  });
1394
+ const res = await doFetch(`${base}/agents`, { method: "GET", query });
1395
+ return res.json();
1239
1396
  };
1240
1397
  const baseApi = {
1241
- list: listAgents,
1242
- get: getAgentDetail,
1243
- create: createAgent,
1244
- forkFromTemplate: forkAgentFromTemplate,
1245
- update: updateAgent,
1246
- delete: deleteAgent
1398
+ async list(options = {}) {
1399
+ const opts = { ...options ?? {} };
1400
+ const response = await fetchPage(opts);
1401
+ return attachPaginator(response, fetchPage, opts);
1402
+ },
1403
+ async get(agentId, callbacks) {
1404
+ if (callbacks) {
1405
+ return getWithSse(`${base}/agents/${agentId}`, callbacks);
1406
+ }
1407
+ requireWorkspace();
1408
+ const res = await doFetch(`${base}/agents/${agentId}`, { method: "GET" });
1409
+ const contentType = res.headers.get("content-type") ?? "";
1410
+ if (contentType.includes("text/event-stream")) {
1411
+ return consumeSseStream(
1412
+ res,
1413
+ buildSseHandlers({}),
1414
+ "agent",
1415
+ {
1416
+ resolveImmediately: true
1417
+ }
1418
+ );
1419
+ }
1420
+ return res.json();
1421
+ },
1422
+ async create(payload, callbacks) {
1423
+ if (callbacks) {
1424
+ return postWithSse(`${base}/agents`, payload, callbacks);
1425
+ }
1426
+ return postJson(`${base}/agents`, payload);
1427
+ },
1428
+ async clone(agentId, payload) {
1429
+ return postJson(
1430
+ `${base}/agents/${agentId}/clone`,
1431
+ buildCloneRequest(payload)
1432
+ );
1433
+ },
1434
+ async forkFromTemplate(payload, callbacks) {
1435
+ if (callbacks) {
1436
+ return postWithSse(`${base}/agents/from-template`, payload, callbacks);
1437
+ }
1438
+ return postJson(`${base}/agents/from-template`, payload);
1439
+ },
1440
+ async rebuild(agentId, callbacks) {
1441
+ if (callbacks) {
1442
+ return postWithSse(`${base}/agents/${agentId}/rebuild`, {}, callbacks);
1443
+ }
1444
+ return postJson(`${base}/agents/${agentId}/rebuild`, {});
1445
+ },
1446
+ async update(agentId, payload) {
1447
+ requireWorkspace();
1448
+ const res = await doFetch(`${base}/agents/${agentId}`, {
1449
+ method: "PATCH",
1450
+ body: JSON.stringify(payload),
1451
+ headers: jsonHeaders
1452
+ });
1453
+ return res.json();
1454
+ },
1455
+ async delete(agent) {
1456
+ requireWorkspace();
1457
+ await doFetch(`${base}/agents/${resolveAgentId(agent)}`, {
1458
+ method: "DELETE"
1459
+ });
1460
+ }
1247
1461
  };
1248
1462
  if (!relatedApis) {
1249
1463
  return baseApi;
1250
1464
  }
1465
+ return withEntityWrapping(baseApi, fetchPage, relatedApis);
1466
+ }
1467
+ function withEntityWrapping(api, fetchPage, deps) {
1251
1468
  const wrapAgent = (detail) => createAgentEntity(detail, {
1252
- tagsApi: relatedApis.tagsApi,
1253
- phonesApi: relatedApis.phonesApi,
1254
- scheduleApi: relatedApis.scheduleApi,
1255
- scheduleExceptionsApi: relatedApis.scheduleExceptionsApi,
1256
- versionsApi: relatedApis.versionsApi,
1257
- blueprintsApi: relatedApis.blueprintsApi,
1258
- stagesApi: relatedApis.stagesApi,
1259
- stageTriggersApi: relatedApis.stageTriggersApi,
1469
+ ...deps,
1260
1470
  reload: async (agentId) => {
1261
- const latest = await getAgentDetail(agentId);
1471
+ const latest = await api.get(agentId);
1262
1472
  return wrapAgent(latest);
1263
1473
  },
1264
1474
  updateAgent: async (agentId, payload) => {
1265
- const updated = await updateAgent(agentId, payload);
1475
+ const updated = await api.update(agentId, payload);
1266
1476
  return wrapAgent(updated);
1267
1477
  },
1268
1478
  deleteAgent: async (agentId) => {
1269
- await deleteAgent(agentId);
1479
+ await api.delete(agentId);
1480
+ },
1481
+ cloneAgent: async (agentId, payload) => {
1482
+ const cloned = await api.clone(agentId, payload);
1483
+ return wrapAgent(cloned);
1270
1484
  }
1271
1485
  });
1486
+ const wrapList = async (opts) => {
1487
+ const result = await fetchPage(opts);
1488
+ const items = Array.isArray(result.data) ? result.data : [];
1489
+ return { ...result, data: items.map(wrapAgent) };
1490
+ };
1272
1491
  return {
1273
- ...baseApi,
1492
+ ...api,
1274
1493
  async list(options = {}) {
1275
- const normalizedOptions = { ...options ?? {} };
1276
- const applyWrap = async (opts) => {
1277
- const result = await fetchAgentsPage(opts);
1278
- const items = Array.isArray(result.data) ? result.data : [];
1279
- return {
1280
- ...result,
1281
- data: items.map((summary) => wrapAgent(summary))
1282
- };
1283
- };
1284
- const initial = await applyWrap(normalizedOptions);
1285
- return attachPaginator(initial, applyWrap, normalizedOptions);
1494
+ const opts = { ...options ?? {} };
1495
+ const initial = await wrapList(opts);
1496
+ return attachPaginator(initial, wrapList, opts);
1286
1497
  },
1287
- async get(agentId) {
1288
- const detail = await getAgentDetail(agentId);
1289
- return wrapAgent(detail);
1498
+ async get(agentId, callbacks) {
1499
+ return wrapAgent(await api.get(agentId, callbacks));
1290
1500
  },
1291
- async create(payload) {
1292
- const detail = await createAgent(payload);
1293
- return wrapAgent(detail);
1501
+ async create(payload, callbacks) {
1502
+ return wrapAgent(await api.create(payload, callbacks));
1503
+ },
1504
+ async clone(agentId, payload) {
1505
+ return wrapAgent(await api.clone(agentId, payload));
1294
1506
  },
1295
- async forkFromTemplate(payload) {
1296
- const detail = await forkAgentFromTemplate(payload);
1297
- return wrapAgent(detail);
1507
+ async forkFromTemplate(payload, callbacks) {
1508
+ return wrapAgent(await api.forkFromTemplate(payload, callbacks));
1298
1509
  },
1299
1510
  async update(agentId, payload) {
1300
- const detail = await updateAgent(agentId, payload);
1301
- return wrapAgent(detail);
1511
+ return wrapAgent(await api.update(agentId, payload));
1302
1512
  }
1303
1513
  };
1304
1514
  }
@@ -1334,6 +1544,82 @@ function createApiKeysApi(cfg) {
1334
1544
  };
1335
1545
  }
1336
1546
 
1547
+ // src/api/billing.ts
1548
+ function createBillingApi(cfg) {
1549
+ const { base, doFetch } = createHttp(cfg);
1550
+ return {
1551
+ async getBalance() {
1552
+ const res = await doFetch(`${base}/billing/balance`, {
1553
+ method: "GET"
1554
+ });
1555
+ return res.json();
1556
+ }
1557
+ };
1558
+ }
1559
+
1560
+ // src/api/calls.ts
1561
+ function createCallsApi(cfg) {
1562
+ const { base, doFetch } = createHttp(cfg);
1563
+ const fetchCallsPage = async (options = {}) => {
1564
+ const {
1565
+ agentBatchId,
1566
+ agentId,
1567
+ executionId,
1568
+ recordedAfter,
1569
+ recordedBefore,
1570
+ durationBucket,
1571
+ goalStatus,
1572
+ query: searchQuery,
1573
+ ...listOptions
1574
+ } = options ?? {};
1575
+ const query = serializeListOptions(listOptions, {
1576
+ agentBatchId,
1577
+ agentId,
1578
+ executionId,
1579
+ recordedAfter,
1580
+ recordedBefore,
1581
+ durationBucket,
1582
+ goalStatus,
1583
+ query: searchQuery
1584
+ });
1585
+ const res = await doFetch(`${base}/calls`, {
1586
+ method: "GET",
1587
+ query
1588
+ });
1589
+ return res.json();
1590
+ };
1591
+ return {
1592
+ async list(options = {}) {
1593
+ const normalizedOptions = { ...options ?? {} };
1594
+ const response = await fetchCallsPage(normalizedOptions);
1595
+ return attachPaginator(response, fetchCallsPage, normalizedOptions);
1596
+ },
1597
+ async stream(options) {
1598
+ const query = buildSearchParams([
1599
+ ["after", options.after],
1600
+ ["limit", options.limit],
1601
+ ["agentBatchId", options.agentBatchId],
1602
+ ["agentId", options.agentId],
1603
+ ["executionId", options.executionId],
1604
+ ["durationBucket", options.durationBucket],
1605
+ ["goalStatus", options.goalStatus],
1606
+ ["query", options.query]
1607
+ ]);
1608
+ const res = await doFetch(`${base}/calls/stream`, {
1609
+ method: "GET",
1610
+ query
1611
+ });
1612
+ return res.json();
1613
+ },
1614
+ async get(callId) {
1615
+ const res = await doFetch(`${base}/calls/${callId}`, {
1616
+ method: "GET"
1617
+ });
1618
+ return res.json();
1619
+ }
1620
+ };
1621
+ }
1622
+
1337
1623
  // src/api/campaigns.ts
1338
1624
  function createCampaignsApi(cfg) {
1339
1625
  const { base, doFetch } = createHttp(cfg);
@@ -1349,10 +1635,7 @@ function createCampaignsApi(cfg) {
1349
1635
  return res.json();
1350
1636
  };
1351
1637
  const fetchExecutionsPage = async (campaignId, options = {}) => {
1352
- const query = serializeListOptions({
1353
- page: options.page,
1354
- limit: options.limit
1355
- });
1638
+ const query = serializeListOptions(options ?? {});
1356
1639
  const res = await doFetch(`${base}/campaigns/${campaignId}/executions`, {
1357
1640
  method: "GET",
1358
1641
  query
@@ -1406,6 +1689,51 @@ function createCampaignsApi(cfg) {
1406
1689
  const response = await fetchExecutionsPage(campaignId, normalizedOptions);
1407
1690
  const fetchPage = (opts) => fetchExecutionsPage(campaignId, opts);
1408
1691
  return attachPaginator(response, fetchPage, normalizedOptions);
1692
+ },
1693
+ /**
1694
+ * Pausa una campaña en curso. Los reintentos ya programados se
1695
+ * reagendan al ejecutarse (sin consumir intentos) hasta `resume()`.
1696
+ */
1697
+ async pause(campaignId) {
1698
+ const res = await doFetch(`${base}/campaigns/${campaignId}/pause`, {
1699
+ method: "POST"
1700
+ });
1701
+ return res.json();
1702
+ },
1703
+ /**
1704
+ * Reanuda una campaña pausada. Re-encola los reintentos pendientes
1705
+ * según su `nextRetryAt`.
1706
+ */
1707
+ async resume(campaignId) {
1708
+ const res = await doFetch(`${base}/campaigns/${campaignId}/resume`, {
1709
+ method: "POST"
1710
+ });
1711
+ return res.json();
1712
+ },
1713
+ /**
1714
+ * Cancela definitivamente una campaña no terminal. Los reintentos
1715
+ * pendientes se descartan — operación destructiva.
1716
+ */
1717
+ async cancel(campaignId) {
1718
+ const res = await doFetch(`${base}/campaigns/${campaignId}/cancel`, {
1719
+ method: "POST"
1720
+ });
1721
+ return res.json();
1722
+ },
1723
+ /**
1724
+ * Descarga el CSV con todas las ejecuciones de la campaña. Devuelve un
1725
+ * `Blob` con `text/csv; charset=utf-8` (BOM UTF-8 incluido). Para
1726
+ * campañas > 20,000 filas la API responde 413.
1727
+ */
1728
+ async exportExecutions(campaignId) {
1729
+ const res = await doFetch(
1730
+ `${base}/campaigns/${campaignId}/executions/export`,
1731
+ {
1732
+ method: "GET",
1733
+ headers: { Accept: "text/csv" }
1734
+ }
1735
+ );
1736
+ return res.blob();
1409
1737
  }
1410
1738
  };
1411
1739
  }
@@ -1486,6 +1814,70 @@ function createCatalogsApi(cfg) {
1486
1814
  };
1487
1815
  }
1488
1816
 
1817
+ // src/api/documents.ts
1818
+ function createDocumentsApi(cfg) {
1819
+ const { base, doFetch, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1820
+ const jsonHeaders = { "content-type": "application/json" };
1821
+ const requireWorkspace = () => {
1822
+ const workspaceId = resolveWorkspaceId();
1823
+ if (typeof workspaceId === "string" && workspaceId.trim().length > 0) {
1824
+ return;
1825
+ }
1826
+ const apiKey = resolveApiKey();
1827
+ if (typeof apiKey === "string" && apiKey.trim().length > 0) {
1828
+ return;
1829
+ }
1830
+ throw new WorkspaceNotSelectedError();
1831
+ };
1832
+ return {
1833
+ async create(files) {
1834
+ requireWorkspace();
1835
+ const res = await doFetch(`${base}/documents`, {
1836
+ method: "POST",
1837
+ body: JSON.stringify({ files }),
1838
+ headers: jsonHeaders
1839
+ });
1840
+ return res.json();
1841
+ }
1842
+ };
1843
+ }
1844
+
1845
+ // src/api/sip-trunks.ts
1846
+ function createSipTrunksApi(cfg) {
1847
+ const { base, doFetch } = createHttp(cfg);
1848
+ return {
1849
+ async list() {
1850
+ const res = await doFetch(`${base}/sip/trunks`, { method: "GET" });
1851
+ return res.json();
1852
+ },
1853
+ async get(trunkId) {
1854
+ const res = await doFetch(`${base}/sip/trunks/${trunkId}`, {
1855
+ method: "GET"
1856
+ });
1857
+ return res.json();
1858
+ },
1859
+ async create(body) {
1860
+ const res = await doFetch(`${base}/sip/trunks`, {
1861
+ method: "POST",
1862
+ headers: { "Content-Type": "application/json" },
1863
+ body: JSON.stringify(body)
1864
+ });
1865
+ return res.json();
1866
+ },
1867
+ async update(trunkId, body) {
1868
+ const res = await doFetch(`${base}/sip/trunks/${trunkId}`, {
1869
+ method: "PATCH",
1870
+ headers: { "Content-Type": "application/json" },
1871
+ body: JSON.stringify(body)
1872
+ });
1873
+ return res.json();
1874
+ },
1875
+ async delete(trunkId) {
1876
+ await doFetch(`${base}/sip/trunks/${trunkId}`, { method: "DELETE" });
1877
+ }
1878
+ };
1879
+ }
1880
+
1489
1881
  // src/api/tools.ts
1490
1882
  var IDEMPOTENCY_HEADER = "Idempotency-Key";
1491
1883
  var generateIdempotencyKey = (explicit) => {
@@ -1675,6 +2067,74 @@ function createToolsApi(cfg) {
1675
2067
  };
1676
2068
  }
1677
2069
 
2070
+ // src/api/usage.ts
2071
+ var import_api_query_builder = require("@getsupervisor/api-query-builder");
2072
+ function createUsageApi(cfg) {
2073
+ const { base, doFetch } = createHttp(cfg);
2074
+ const fetchUsageAgentsPage = async (options = {}) => {
2075
+ const { resource, from, to, ...listOptions } = options;
2076
+ const query = serializeListOptions({
2077
+ ...listOptions,
2078
+ filter: resolveFilter(
2079
+ buildUsageFilter(resource, from, to),
2080
+ listOptions.filter
2081
+ )
2082
+ });
2083
+ const res = await doFetch(`${base}/usage/agents`, {
2084
+ method: "GET",
2085
+ query
2086
+ });
2087
+ return res.json();
2088
+ };
2089
+ return {
2090
+ async agents(options = {}) {
2091
+ const normalizedOptions = {
2092
+ ...options
2093
+ };
2094
+ const response = await fetchUsageAgentsPage(normalizedOptions);
2095
+ return attachPaginator(response, fetchUsageAgentsPage, normalizedOptions);
2096
+ }
2097
+ };
2098
+ }
2099
+ function resolveFilter(usageFilter, fallback) {
2100
+ if (usageFilter) {
2101
+ return usageFilter;
2102
+ }
2103
+ return fallback;
2104
+ }
2105
+ function appendCondition(query, factory) {
2106
+ if (query) {
2107
+ query.and(factory);
2108
+ return query;
2109
+ }
2110
+ return new import_api_query_builder.Query(factory);
2111
+ }
2112
+ var DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
2113
+ function toISOString(value) {
2114
+ const input = DATE_ONLY.test(value) ? `${value}T00:00:00` : value;
2115
+ const date = new Date(input);
2116
+ if (Number.isNaN(date.getTime())) {
2117
+ return value;
2118
+ }
2119
+ return date.toISOString();
2120
+ }
2121
+ function buildUsageFilter(resource, from, to) {
2122
+ let query;
2123
+ if (resource) {
2124
+ query = appendCondition(query, (qb) => qb.eq("resource", resource));
2125
+ }
2126
+ if (from) {
2127
+ query = appendCondition(
2128
+ query,
2129
+ (qb) => qb.mte("createdAt", toISOString(from))
2130
+ );
2131
+ }
2132
+ if (to) {
2133
+ query = appendCondition(query, (qb) => qb.lt("createdAt", toISOString(to)));
2134
+ }
2135
+ return query;
2136
+ }
2137
+
1678
2138
  // src/utils/catalog-voices.ts
1679
2139
  var FALLBACK_LOCALE = "und";
1680
2140
  var FALLBACK_PROVIDER = "catalog";
@@ -1733,9 +2193,9 @@ function pickGender(value) {
1733
2193
  }
1734
2194
 
1735
2195
  // src/utils/catalog-filter.ts
1736
- var import_api_query_builder = require("@getsupervisor/api-query-builder");
2196
+ var import_api_query_builder2 = require("@getsupervisor/api-query-builder");
1737
2197
  function createCatalogTypeQuery(type) {
1738
- return new import_api_query_builder.Query((qb) => qb.eq("type", type));
2198
+ return new import_api_query_builder2.Query((qb) => qb.eq("type", type));
1739
2199
  }
1740
2200
  function ensureCatalogTypeFilter(filter, type) {
1741
2201
  const requiredQuery = createCatalogTypeQuery(type);
@@ -1971,6 +2431,35 @@ function createWebhooksApi(cfg) {
1971
2431
  `${base}/webhooks/${webhookId}/subscriptions/${subscriptionId}`,
1972
2432
  { method: "DELETE" }
1973
2433
  );
2434
+ },
2435
+ // Deliveries
2436
+ async listDeliveries(webhookId, options = {}) {
2437
+ const fetchPage = async (opts) => {
2438
+ const query = serializeListOptions({
2439
+ page: opts.page,
2440
+ limit: opts.limit,
2441
+ sort: opts.sort,
2442
+ fields: opts.fields,
2443
+ include: opts.include,
2444
+ search: opts.search,
2445
+ filter: opts.filter
2446
+ });
2447
+ const res = await doFetch(`${base}/webhooks/${webhookId}/deliveries`, {
2448
+ method: "GET",
2449
+ query
2450
+ });
2451
+ return res.json();
2452
+ };
2453
+ const normalized = { ...options ?? {} };
2454
+ const response = await fetchPage(normalized);
2455
+ return attachPaginator(response, fetchPage, normalized);
2456
+ },
2457
+ async getDelivery(webhookId, deliveryId) {
2458
+ const res = await doFetch(
2459
+ `${base}/webhooks/${webhookId}/deliveries/${deliveryId}`,
2460
+ { method: "GET" }
2461
+ );
2462
+ return res.json();
1974
2463
  }
1975
2464
  };
1976
2465
  }
@@ -2034,8 +2523,8 @@ function createWorkspacesApi(cfg) {
2034
2523
  const fetchPage = (options) => fetchPhonesPage(workspaceId, options);
2035
2524
  return attachPaginator(response, fetchPage, normalizedOptions);
2036
2525
  },
2037
- async enable(workspaceId, payload) {
2038
- const res = await doFetch(`${base}/workspaces/${workspaceId}/enable`, {
2526
+ async enable(payload) {
2527
+ const res = await doFetch(`${base}/workspaces/enable`, {
2039
2528
  method: "POST",
2040
2529
  headers: jsonHeaders,
2041
2530
  body: JSON.stringify(payload)
@@ -2081,6 +2570,7 @@ function createClient(initialCfg) {
2081
2570
  const stagesApi = createAgentStagesApi(runtimeCfg);
2082
2571
  const voicesApi = createVoicesApi(runtimeCfg);
2083
2572
  const apiKeysApi = createApiKeysApi(runtimeCfg);
2573
+ const callsApi = createCallsApi(runtimeCfg);
2084
2574
  const catalogsApi = createCatalogsApi(runtimeCfg);
2085
2575
  const catalogTemplatesApi = createCatalogTemplatesApi(runtimeCfg);
2086
2576
  const webhooksApi = createWebhooksApi(runtimeCfg);
@@ -2149,9 +2639,14 @@ function createClient(initialCfg) {
2149
2639
  ...catalogsApi,
2150
2640
  templates: catalogTemplatesApi
2151
2641
  },
2642
+ documents: createDocumentsApi(runtimeCfg),
2152
2643
  campaigns: createCampaignsApi(runtimeCfg),
2153
2644
  voices: voicesApi,
2154
2645
  apiKeys: apiKeysApi,
2646
+ billing: createBillingApi(runtimeCfg),
2647
+ calls: callsApi,
2648
+ usage: createUsageApi(runtimeCfg),
2649
+ sip: createSipTrunksApi(runtimeCfg),
2155
2650
  webhooks: webhooksApi
2156
2651
  };
2157
2652
  return {
@@ -2235,12 +2730,17 @@ function createClient(initialCfg) {
2235
2730
  createAgentVersionsApi,
2236
2731
  createAgentsApi,
2237
2732
  createApiKeysApi,
2733
+ createBillingApi,
2734
+ createCallsApi,
2238
2735
  createCampaignsApi,
2239
2736
  createCatalogTemplatesApi,
2240
2737
  createCatalogsApi,
2241
2738
  createClient,
2739
+ createDocumentsApi,
2242
2740
  createHttp,
2741
+ createSipTrunksApi,
2243
2742
  createToolsApi,
2743
+ createUsageApi,
2244
2744
  createVoicesApi,
2245
2745
  createWebhooksApi,
2246
2746
  createWorkspacesApi,