@getsupervisor/agents-studio-sdk 1.41.2 → 1.42.0

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);
@@ -1071,7 +1077,8 @@ var createAgentEntity = (dto, options) => {
1071
1077
  stageTriggersApi,
1072
1078
  reload,
1073
1079
  updateAgent,
1074
- deleteAgent
1080
+ deleteAgent,
1081
+ cloneAgent
1075
1082
  } = options;
1076
1083
  const schedulesHelper = bindAgentSchedules(
1077
1084
  scheduleApi,
@@ -1090,6 +1097,9 @@ var createAgentEntity = (dto, options) => {
1090
1097
  async save(patch) {
1091
1098
  return updateAgent(dto.agentId, patch);
1092
1099
  },
1100
+ async clone(payload) {
1101
+ return cloneAgent(dto.agentId, payload);
1102
+ },
1093
1103
  async delete() {
1094
1104
  await deleteAgent(dto.agentId);
1095
1105
  },
@@ -1115,6 +1125,22 @@ function createAgentsApi(cfg, relatedApis) {
1115
1125
  throw new WorkspaceNotSelectedError();
1116
1126
  };
1117
1127
  const jsonHeaders = { "content-type": "application/json" };
1128
+ const normalizeCloneSelection = (selection) => {
1129
+ if (!selection) {
1130
+ return [];
1131
+ }
1132
+ if (Array.isArray(selection)) {
1133
+ return selection;
1134
+ }
1135
+ return Object.entries(selection).filter(([, enabled]) => enabled).map(([component]) => component);
1136
+ };
1137
+ const buildCloneAgentRequest = (payload) => {
1138
+ const { clone, ...rest } = payload;
1139
+ return {
1140
+ ...rest,
1141
+ clone: normalizeCloneSelection(clone)
1142
+ };
1143
+ };
1118
1144
  const fetchAgentsPage = async (options = {}) => {
1119
1145
  requireWorkspace();
1120
1146
  const sanitizedOptions = {
@@ -1159,6 +1185,16 @@ function createAgentsApi(cfg, relatedApis) {
1159
1185
  });
1160
1186
  return res.json();
1161
1187
  };
1188
+ const cloneAgent = async (agentId, payload) => {
1189
+ requireWorkspace();
1190
+ const requestPayload = buildCloneAgentRequest(payload);
1191
+ const res = await doFetch(`${base}/agents/${agentId}/clone`, {
1192
+ method: "POST",
1193
+ body: JSON.stringify(requestPayload),
1194
+ headers: jsonHeaders
1195
+ });
1196
+ return res.json();
1197
+ };
1162
1198
  const updateAgent = async (agentId, payload) => {
1163
1199
  requireWorkspace();
1164
1200
  const res = await doFetch(`${base}/agents/${agentId}`, {
@@ -1182,6 +1218,7 @@ function createAgentsApi(cfg, relatedApis) {
1182
1218
  list: listAgents,
1183
1219
  get: getAgentDetail,
1184
1220
  create: createAgent,
1221
+ clone: cloneAgent,
1185
1222
  forkFromTemplate: forkAgentFromTemplate,
1186
1223
  update: updateAgent,
1187
1224
  delete: deleteAgent
@@ -1208,6 +1245,10 @@ function createAgentsApi(cfg, relatedApis) {
1208
1245
  },
1209
1246
  deleteAgent: async (agentId) => {
1210
1247
  await deleteAgent(agentId);
1248
+ },
1249
+ cloneAgent: async (agentId, payload) => {
1250
+ const cloned = await cloneAgent(agentId, payload);
1251
+ return wrapAgent(cloned);
1211
1252
  }
1212
1253
  });
1213
1254
  return {
@@ -1233,6 +1274,10 @@ function createAgentsApi(cfg, relatedApis) {
1233
1274
  const detail = await createAgent(payload);
1234
1275
  return wrapAgent(detail);
1235
1276
  },
1277
+ async clone(agentId, payload) {
1278
+ const detail = await cloneAgent(agentId, payload);
1279
+ return wrapAgent(detail);
1280
+ },
1236
1281
  async forkFromTemplate(payload) {
1237
1282
  const detail = await forkAgentFromTemplate(payload);
1238
1283
  return wrapAgent(detail);
@@ -1275,6 +1320,82 @@ function createApiKeysApi(cfg) {
1275
1320
  };
1276
1321
  }
1277
1322
 
1323
+ // src/api/billing.ts
1324
+ function createBillingApi(cfg) {
1325
+ const { base, doFetch } = createHttp(cfg);
1326
+ return {
1327
+ async getBalance() {
1328
+ const res = await doFetch(`${base}/billing/balance`, {
1329
+ method: "GET"
1330
+ });
1331
+ return res.json();
1332
+ }
1333
+ };
1334
+ }
1335
+
1336
+ // src/api/calls.ts
1337
+ function createCallsApi(cfg) {
1338
+ const { base, doFetch } = createHttp(cfg);
1339
+ const fetchCallsPage = async (options = {}) => {
1340
+ const {
1341
+ agentBatchId,
1342
+ agentId,
1343
+ executionId,
1344
+ recordedAfter,
1345
+ recordedBefore,
1346
+ durationBucket,
1347
+ goalStatus,
1348
+ query: searchQuery,
1349
+ ...listOptions
1350
+ } = options ?? {};
1351
+ const query = serializeListOptions(listOptions, {
1352
+ agentBatchId,
1353
+ agentId,
1354
+ executionId,
1355
+ recordedAfter,
1356
+ recordedBefore,
1357
+ durationBucket,
1358
+ goalStatus,
1359
+ query: searchQuery
1360
+ });
1361
+ const res = await doFetch(`${base}/calls`, {
1362
+ method: "GET",
1363
+ query
1364
+ });
1365
+ return res.json();
1366
+ };
1367
+ return {
1368
+ async list(options = {}) {
1369
+ const normalizedOptions = { ...options ?? {} };
1370
+ const response = await fetchCallsPage(normalizedOptions);
1371
+ return attachPaginator(response, fetchCallsPage, normalizedOptions);
1372
+ },
1373
+ async stream(options) {
1374
+ const query = buildSearchParams([
1375
+ ["after", options.after],
1376
+ ["limit", options.limit],
1377
+ ["agentBatchId", options.agentBatchId],
1378
+ ["agentId", options.agentId],
1379
+ ["executionId", options.executionId],
1380
+ ["durationBucket", options.durationBucket],
1381
+ ["goalStatus", options.goalStatus],
1382
+ ["query", options.query]
1383
+ ]);
1384
+ const res = await doFetch(`${base}/calls/stream`, {
1385
+ method: "GET",
1386
+ query
1387
+ });
1388
+ return res.json();
1389
+ },
1390
+ async get(callId) {
1391
+ const res = await doFetch(`${base}/calls/${callId}`, {
1392
+ method: "GET"
1393
+ });
1394
+ return res.json();
1395
+ }
1396
+ };
1397
+ }
1398
+
1278
1399
  // src/api/campaigns.ts
1279
1400
  function createCampaignsApi(cfg) {
1280
1401
  const { base, doFetch } = createHttp(cfg);
@@ -1290,10 +1411,7 @@ function createCampaignsApi(cfg) {
1290
1411
  return res.json();
1291
1412
  };
1292
1413
  const fetchExecutionsPage = async (campaignId, options = {}) => {
1293
- const query = serializeListOptions({
1294
- page: options.page,
1295
- limit: options.limit
1296
- });
1414
+ const query = serializeListOptions(options ?? {});
1297
1415
  const res = await doFetch(`${base}/campaigns/${campaignId}/executions`, {
1298
1416
  method: "GET",
1299
1417
  query
@@ -1347,6 +1465,51 @@ function createCampaignsApi(cfg) {
1347
1465
  const response = await fetchExecutionsPage(campaignId, normalizedOptions);
1348
1466
  const fetchPage = (opts) => fetchExecutionsPage(campaignId, opts);
1349
1467
  return attachPaginator(response, fetchPage, normalizedOptions);
1468
+ },
1469
+ /**
1470
+ * Pausa una campaña en curso. Los reintentos ya programados se
1471
+ * reagendan al ejecutarse (sin consumir intentos) hasta `resume()`.
1472
+ */
1473
+ async pause(campaignId) {
1474
+ const res = await doFetch(`${base}/campaigns/${campaignId}/pause`, {
1475
+ method: "POST"
1476
+ });
1477
+ return res.json();
1478
+ },
1479
+ /**
1480
+ * Reanuda una campaña pausada. Re-encola los reintentos pendientes
1481
+ * según su `nextRetryAt`.
1482
+ */
1483
+ async resume(campaignId) {
1484
+ const res = await doFetch(`${base}/campaigns/${campaignId}/resume`, {
1485
+ method: "POST"
1486
+ });
1487
+ return res.json();
1488
+ },
1489
+ /**
1490
+ * Cancela definitivamente una campaña no terminal. Los reintentos
1491
+ * pendientes se descartan — operación destructiva.
1492
+ */
1493
+ async cancel(campaignId) {
1494
+ const res = await doFetch(`${base}/campaigns/${campaignId}/cancel`, {
1495
+ method: "POST"
1496
+ });
1497
+ return res.json();
1498
+ },
1499
+ /**
1500
+ * Descarga el CSV con todas las ejecuciones de la campaña. Devuelve un
1501
+ * `Blob` con `text/csv; charset=utf-8` (BOM UTF-8 incluido). Para
1502
+ * campañas > 20,000 filas la API responde 413.
1503
+ */
1504
+ async exportExecutions(campaignId) {
1505
+ const res = await doFetch(
1506
+ `${base}/campaigns/${campaignId}/executions/export`,
1507
+ {
1508
+ method: "GET",
1509
+ headers: { Accept: "text/csv" }
1510
+ }
1511
+ );
1512
+ return res.blob();
1350
1513
  }
1351
1514
  };
1352
1515
  }
@@ -1427,6 +1590,42 @@ function createCatalogsApi(cfg) {
1427
1590
  };
1428
1591
  }
1429
1592
 
1593
+ // src/api/sip-trunks.ts
1594
+ function createSipTrunksApi(cfg) {
1595
+ const { base, doFetch } = createHttp(cfg);
1596
+ return {
1597
+ async list() {
1598
+ const res = await doFetch(`${base}/sip/trunks`, { method: "GET" });
1599
+ return res.json();
1600
+ },
1601
+ async get(trunkId) {
1602
+ const res = await doFetch(`${base}/sip/trunks/${trunkId}`, {
1603
+ method: "GET"
1604
+ });
1605
+ return res.json();
1606
+ },
1607
+ async create(body) {
1608
+ const res = await doFetch(`${base}/sip/trunks`, {
1609
+ method: "POST",
1610
+ headers: { "Content-Type": "application/json" },
1611
+ body: JSON.stringify(body)
1612
+ });
1613
+ return res.json();
1614
+ },
1615
+ async update(trunkId, body) {
1616
+ const res = await doFetch(`${base}/sip/trunks/${trunkId}`, {
1617
+ method: "PATCH",
1618
+ headers: { "Content-Type": "application/json" },
1619
+ body: JSON.stringify(body)
1620
+ });
1621
+ return res.json();
1622
+ },
1623
+ async delete(trunkId) {
1624
+ await doFetch(`${base}/sip/trunks/${trunkId}`, { method: "DELETE" });
1625
+ }
1626
+ };
1627
+ }
1628
+
1430
1629
  // src/api/tools.ts
1431
1630
  var IDEMPOTENCY_HEADER = "Idempotency-Key";
1432
1631
  var generateIdempotencyKey = (explicit) => {
@@ -1616,6 +1815,74 @@ function createToolsApi(cfg) {
1616
1815
  };
1617
1816
  }
1618
1817
 
1818
+ // src/api/usage.ts
1819
+ import { Query } from "@getsupervisor/api-query-builder";
1820
+ function createUsageApi(cfg) {
1821
+ const { base, doFetch } = createHttp(cfg);
1822
+ const fetchUsageAgentsPage = async (options = {}) => {
1823
+ const { resource, from, to, ...listOptions } = options;
1824
+ const query = serializeListOptions({
1825
+ ...listOptions,
1826
+ filter: resolveFilter(
1827
+ buildUsageFilter(resource, from, to),
1828
+ listOptions.filter
1829
+ )
1830
+ });
1831
+ const res = await doFetch(`${base}/usage/agents`, {
1832
+ method: "GET",
1833
+ query
1834
+ });
1835
+ return res.json();
1836
+ };
1837
+ return {
1838
+ async agents(options = {}) {
1839
+ const normalizedOptions = {
1840
+ ...options
1841
+ };
1842
+ const response = await fetchUsageAgentsPage(normalizedOptions);
1843
+ return attachPaginator(response, fetchUsageAgentsPage, normalizedOptions);
1844
+ }
1845
+ };
1846
+ }
1847
+ function resolveFilter(usageFilter, fallback) {
1848
+ if (usageFilter) {
1849
+ return usageFilter;
1850
+ }
1851
+ return fallback;
1852
+ }
1853
+ function appendCondition(query, factory) {
1854
+ if (query) {
1855
+ query.and(factory);
1856
+ return query;
1857
+ }
1858
+ return new Query(factory);
1859
+ }
1860
+ var DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
1861
+ function toISOString(value) {
1862
+ const input = DATE_ONLY.test(value) ? `${value}T00:00:00` : value;
1863
+ const date = new Date(input);
1864
+ if (Number.isNaN(date.getTime())) {
1865
+ return value;
1866
+ }
1867
+ return date.toISOString();
1868
+ }
1869
+ function buildUsageFilter(resource, from, to) {
1870
+ let query;
1871
+ if (resource) {
1872
+ query = appendCondition(query, (qb) => qb.eq("resource", resource));
1873
+ }
1874
+ if (from) {
1875
+ query = appendCondition(
1876
+ query,
1877
+ (qb) => qb.mte("createdAt", toISOString(from))
1878
+ );
1879
+ }
1880
+ if (to) {
1881
+ query = appendCondition(query, (qb) => qb.lt("createdAt", toISOString(to)));
1882
+ }
1883
+ return query;
1884
+ }
1885
+
1619
1886
  // src/utils/catalog-voices.ts
1620
1887
  var FALLBACK_LOCALE = "und";
1621
1888
  var FALLBACK_PROVIDER = "catalog";
@@ -1674,9 +1941,9 @@ function pickGender(value) {
1674
1941
  }
1675
1942
 
1676
1943
  // src/utils/catalog-filter.ts
1677
- import { Query } from "@getsupervisor/api-query-builder";
1944
+ import { Query as Query2 } from "@getsupervisor/api-query-builder";
1678
1945
  function createCatalogTypeQuery(type) {
1679
- return new Query((qb) => qb.eq("type", type));
1946
+ return new Query2((qb) => qb.eq("type", type));
1680
1947
  }
1681
1948
  function ensureCatalogTypeFilter(filter, type) {
1682
1949
  const requiredQuery = createCatalogTypeQuery(type);
@@ -1912,6 +2179,35 @@ function createWebhooksApi(cfg) {
1912
2179
  `${base}/webhooks/${webhookId}/subscriptions/${subscriptionId}`,
1913
2180
  { method: "DELETE" }
1914
2181
  );
2182
+ },
2183
+ // Deliveries
2184
+ async listDeliveries(webhookId, options = {}) {
2185
+ const fetchPage = async (opts) => {
2186
+ const query = serializeListOptions({
2187
+ page: opts.page,
2188
+ limit: opts.limit,
2189
+ sort: opts.sort,
2190
+ fields: opts.fields,
2191
+ include: opts.include,
2192
+ search: opts.search,
2193
+ filter: opts.filter
2194
+ });
2195
+ const res = await doFetch(`${base}/webhooks/${webhookId}/deliveries`, {
2196
+ method: "GET",
2197
+ query
2198
+ });
2199
+ return res.json();
2200
+ };
2201
+ const normalized = { ...options ?? {} };
2202
+ const response = await fetchPage(normalized);
2203
+ return attachPaginator(response, fetchPage, normalized);
2204
+ },
2205
+ async getDelivery(webhookId, deliveryId) {
2206
+ const res = await doFetch(
2207
+ `${base}/webhooks/${webhookId}/deliveries/${deliveryId}`,
2208
+ { method: "GET" }
2209
+ );
2210
+ return res.json();
1915
2211
  }
1916
2212
  };
1917
2213
  }
@@ -1975,8 +2271,8 @@ function createWorkspacesApi(cfg) {
1975
2271
  const fetchPage = (options) => fetchPhonesPage(workspaceId, options);
1976
2272
  return attachPaginator(response, fetchPage, normalizedOptions);
1977
2273
  },
1978
- async enable(workspaceId, payload) {
1979
- const res = await doFetch(`${base}/workspaces/${workspaceId}/enable`, {
2274
+ async enable(payload) {
2275
+ const res = await doFetch(`${base}/workspaces/enable`, {
1980
2276
  method: "POST",
1981
2277
  headers: jsonHeaders,
1982
2278
  body: JSON.stringify(payload)
@@ -2022,6 +2318,7 @@ function createClient(initialCfg) {
2022
2318
  const stagesApi = createAgentStagesApi(runtimeCfg);
2023
2319
  const voicesApi = createVoicesApi(runtimeCfg);
2024
2320
  const apiKeysApi = createApiKeysApi(runtimeCfg);
2321
+ const callsApi = createCallsApi(runtimeCfg);
2025
2322
  const catalogsApi = createCatalogsApi(runtimeCfg);
2026
2323
  const catalogTemplatesApi = createCatalogTemplatesApi(runtimeCfg);
2027
2324
  const webhooksApi = createWebhooksApi(runtimeCfg);
@@ -2093,6 +2390,10 @@ function createClient(initialCfg) {
2093
2390
  campaigns: createCampaignsApi(runtimeCfg),
2094
2391
  voices: voicesApi,
2095
2392
  apiKeys: apiKeysApi,
2393
+ billing: createBillingApi(runtimeCfg),
2394
+ calls: callsApi,
2395
+ usage: createUsageApi(runtimeCfg),
2396
+ sip: createSipTrunksApi(runtimeCfg),
2096
2397
  webhooks: webhooksApi
2097
2398
  };
2098
2399
  return {
@@ -2175,12 +2476,16 @@ export {
2175
2476
  createAgentVersionsApi,
2176
2477
  createAgentsApi,
2177
2478
  createApiKeysApi,
2479
+ createBillingApi,
2480
+ createCallsApi,
2178
2481
  createCampaignsApi,
2179
2482
  createCatalogTemplatesApi,
2180
2483
  createCatalogsApi,
2181
2484
  createClient,
2182
2485
  createHttp,
2486
+ createSipTrunksApi,
2183
2487
  createToolsApi,
2488
+ createUsageApi,
2184
2489
  createVoicesApi,
2185
2490
  createWebhooksApi,
2186
2491
  createWorkspacesApi,