@getsupervisor/agents-studio-sdk 1.41.0 → 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
@@ -9,6 +9,14 @@ var HttpError = class extends Error {
9
9
  this.name = "HttpError";
10
10
  }
11
11
  };
12
+ function isApiErrorBody(body) {
13
+ if (!body || typeof body !== "object") return false;
14
+ const candidate = body;
15
+ return typeof candidate.code === "string" && typeof candidate.message === "string";
16
+ }
17
+ function isApiHttpError(err) {
18
+ return err instanceof HttpError && isApiErrorBody(err.body);
19
+ }
12
20
  var TimeoutError = class extends Error {
13
21
  constructor(ms, url) {
14
22
  super(`Timeout after ${ms}ms`);
@@ -25,6 +33,12 @@ var NetworkError = class extends Error {
25
33
  this.name = "NetworkError";
26
34
  }
27
35
  };
36
+ var WorkspaceNotSelectedError = class extends Error {
37
+ constructor() {
38
+ super("Workspace is not selected");
39
+ this.name = "WorkspaceNotSelectedError";
40
+ }
41
+ };
28
42
 
29
43
  // src/http.ts
30
44
  function toQueryString(query) {
@@ -165,7 +179,8 @@ function createHttp(cfg) {
165
179
  }
166
180
  return res;
167
181
  } catch (e) {
168
- if (e.name === "AbortError") throw new TimeoutError(timeout, targetUrl);
182
+ if (e?.name === "AbortError")
183
+ throw new TimeoutError(timeout, targetUrl);
169
184
  if (e instanceof HttpError) throw e;
170
185
  throw new NetworkError(e, targetUrl);
171
186
  }
@@ -186,6 +201,11 @@ function createHttp(cfg) {
186
201
  }
187
202
 
188
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
+ }
189
209
  function serializeListOptions(options = {}, extra = {}) {
190
210
  const params = new URLSearchParams();
191
211
  appendParam(params, "page", options.page);
@@ -871,6 +891,117 @@ function createAgentVersionsApi(cfg) {
871
891
  };
872
892
  }
873
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
+
874
1005
  // src/entities/agent.ts
875
1006
  var bindAgentStageTriggers = (api, agentId, blueprintId, stageId) => ({
876
1007
  list(opts) {
@@ -1057,7 +1188,8 @@ var createAgentEntity = (dto, options) => {
1057
1188
  stageTriggersApi,
1058
1189
  reload,
1059
1190
  updateAgent,
1060
- deleteAgent
1191
+ deleteAgent,
1192
+ cloneAgent
1061
1193
  } = options;
1062
1194
  const schedulesHelper = bindAgentSchedules(
1063
1195
  scheduleApi,
@@ -1076,6 +1208,9 @@ var createAgentEntity = (dto, options) => {
1076
1208
  async save(patch) {
1077
1209
  return updateAgent(dto.agentId, patch);
1078
1210
  },
1211
+ async clone(payload) {
1212
+ return cloneAgent(dto.agentId, payload);
1213
+ },
1079
1214
  async delete() {
1080
1215
  await deleteAgent(dto.agentId);
1081
1216
  },
@@ -1087,128 +1222,229 @@ var createAgentEntity = (dto, options) => {
1087
1222
  };
1088
1223
 
1089
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
+ }
1090
1254
  function createAgentsApi(cfg, relatedApis) {
1091
- const { base, doFetch } = createHttp(cfg);
1255
+ const { base, doFetch, buildHeaders, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1092
1256
  const jsonHeaders = { "content-type": "application/json" };
1093
- const fetchAgentsPage = async (options = {}) => {
1094
- const sanitizedOptions = {
1095
- page: options.page,
1096
- limit: options.limit,
1097
- filter: options.filter
1098
- };
1099
- const query = serializeListOptions(sanitizedOptions);
1100
- const res = await doFetch(`${base}/agents`, {
1101
- method: "GET",
1102
- query
1103
- });
1104
- return res.json();
1105
- };
1106
- const listAgents = async (options = {}) => {
1107
- const normalizedOptions = { ...options ?? {} };
1108
- const response = await fetchAgentsPage(normalizedOptions);
1109
- return attachPaginator(response, fetchAgentsPage, normalizedOptions);
1110
- };
1111
- const getAgentDetail = async (agentId) => {
1112
- const res = await doFetch(`${base}/agents/${agentId}`, {
1113
- method: "GET"
1114
- });
1115
- return res.json();
1257
+ const requireWorkspace = () => {
1258
+ const workspaceId = resolveWorkspaceId();
1259
+ if (typeof workspaceId === "string" && workspaceId.trim().length > 0) {
1260
+ return;
1261
+ }
1262
+ const apiKey = resolveApiKey();
1263
+ if (typeof apiKey === "string" && apiKey.trim().length > 0) {
1264
+ return;
1265
+ }
1266
+ throw new WorkspaceNotSelectedError();
1116
1267
  };
1117
- const createAgent = async (payload) => {
1118
- const res = await doFetch(`${base}/agents`, {
1268
+ const postJson = async (url, payload) => {
1269
+ requireWorkspace();
1270
+ const res = await doFetch(url, {
1119
1271
  method: "POST",
1120
1272
  body: JSON.stringify(payload),
1121
1273
  headers: jsonHeaders
1122
1274
  });
1123
1275
  return res.json();
1124
1276
  };
1125
- const forkAgentFromTemplate = async (payload) => {
1126
- const res = await doFetch(`${base}/agents/from-template`, {
1277
+ const postWithSse = async (url, payload, callbacks) => {
1278
+ requireWorkspace();
1279
+ const headers = buildHeaders({ "content-type": "application/json" });
1280
+ const fx = cfg.fetchImpl ?? fetch;
1281
+ const res = await fx(url, {
1127
1282
  method: "POST",
1128
1283
  body: JSON.stringify(payload),
1129
- headers: jsonHeaders
1284
+ headers
1130
1285
  });
1131
- return res.json();
1132
- };
1133
- const updateAgent = async (agentId, payload) => {
1134
- const res = await doFetch(`${base}/agents/${agentId}`, {
1135
- method: "PATCH",
1136
- body: JSON.stringify(payload),
1137
- headers: jsonHeaders
1138
- });
1139
- 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
+ );
1140
1301
  };
1141
- const resolveAgentId = (agent) => {
1142
- return typeof agent === "string" ? agent : agent.agentId;
1302
+ const getWithSse = async (url, callbacks) => {
1303
+ requireWorkspace();
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
+ );
1143
1322
  };
1144
- const deleteAgent = async (agent) => {
1145
- const agentId = resolveAgentId(agent);
1146
- await doFetch(`${base}/agents/${agentId}`, {
1147
- method: "DELETE"
1323
+ const fetchPage = async (options = {}) => {
1324
+ requireWorkspace();
1325
+ const query = serializeListOptions({
1326
+ page: options.page,
1327
+ limit: options.limit,
1328
+ filter: options.filter
1148
1329
  });
1330
+ const res = await doFetch(`${base}/agents`, { method: "GET", query });
1331
+ return res.json();
1149
1332
  };
1150
1333
  const baseApi = {
1151
- list: listAgents,
1152
- get: getAgentDetail,
1153
- create: createAgent,
1154
- forkFromTemplate: forkAgentFromTemplate,
1155
- update: updateAgent,
1156
- 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
+ }
1157
1397
  };
1158
1398
  if (!relatedApis) {
1159
1399
  return baseApi;
1160
1400
  }
1401
+ return withEntityWrapping(baseApi, fetchPage, relatedApis);
1402
+ }
1403
+ function withEntityWrapping(api, fetchPage, deps) {
1161
1404
  const wrapAgent = (detail) => createAgentEntity(detail, {
1162
- tagsApi: relatedApis.tagsApi,
1163
- phonesApi: relatedApis.phonesApi,
1164
- scheduleApi: relatedApis.scheduleApi,
1165
- scheduleExceptionsApi: relatedApis.scheduleExceptionsApi,
1166
- versionsApi: relatedApis.versionsApi,
1167
- blueprintsApi: relatedApis.blueprintsApi,
1168
- stagesApi: relatedApis.stagesApi,
1169
- stageTriggersApi: relatedApis.stageTriggersApi,
1405
+ ...deps,
1170
1406
  reload: async (agentId) => {
1171
- const latest = await getAgentDetail(agentId);
1407
+ const latest = await api.get(agentId);
1172
1408
  return wrapAgent(latest);
1173
1409
  },
1174
1410
  updateAgent: async (agentId, payload) => {
1175
- const updated = await updateAgent(agentId, payload);
1411
+ const updated = await api.update(agentId, payload);
1176
1412
  return wrapAgent(updated);
1177
1413
  },
1178
1414
  deleteAgent: async (agentId) => {
1179
- 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);
1180
1420
  }
1181
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
+ };
1182
1427
  return {
1183
- ...baseApi,
1428
+ ...api,
1184
1429
  async list(options = {}) {
1185
- const normalizedOptions = { ...options ?? {} };
1186
- const applyWrap = async (opts) => {
1187
- const result = await fetchAgentsPage(opts);
1188
- const items = Array.isArray(result.data) ? result.data : [];
1189
- return {
1190
- ...result,
1191
- data: items.map((summary) => wrapAgent(summary))
1192
- };
1193
- };
1194
- const initial = await applyWrap(normalizedOptions);
1195
- return attachPaginator(initial, applyWrap, normalizedOptions);
1430
+ const opts = { ...options ?? {} };
1431
+ const initial = await wrapList(opts);
1432
+ return attachPaginator(initial, wrapList, opts);
1196
1433
  },
1197
- async get(agentId) {
1198
- const detail = await getAgentDetail(agentId);
1199
- return wrapAgent(detail);
1434
+ async get(agentId, callbacks) {
1435
+ return wrapAgent(await api.get(agentId, callbacks));
1200
1436
  },
1201
- async create(payload) {
1202
- const detail = await createAgent(payload);
1203
- return wrapAgent(detail);
1437
+ async create(payload, callbacks) {
1438
+ return wrapAgent(await api.create(payload, callbacks));
1204
1439
  },
1205
- async forkFromTemplate(payload) {
1206
- const detail = await forkAgentFromTemplate(payload);
1207
- return wrapAgent(detail);
1440
+ async clone(agentId, payload) {
1441
+ return wrapAgent(await api.clone(agentId, payload));
1442
+ },
1443
+ async forkFromTemplate(payload, callbacks) {
1444
+ return wrapAgent(await api.forkFromTemplate(payload, callbacks));
1208
1445
  },
1209
1446
  async update(agentId, payload) {
1210
- const detail = await updateAgent(agentId, payload);
1211
- return wrapAgent(detail);
1447
+ return wrapAgent(await api.update(agentId, payload));
1212
1448
  }
1213
1449
  };
1214
1450
  }
@@ -1244,6 +1480,82 @@ function createApiKeysApi(cfg) {
1244
1480
  };
1245
1481
  }
1246
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
+
1247
1559
  // src/api/campaigns.ts
1248
1560
  function createCampaignsApi(cfg) {
1249
1561
  const { base, doFetch } = createHttp(cfg);
@@ -1259,10 +1571,7 @@ function createCampaignsApi(cfg) {
1259
1571
  return res.json();
1260
1572
  };
1261
1573
  const fetchExecutionsPage = async (campaignId, options = {}) => {
1262
- const query = serializeListOptions({
1263
- page: options.page,
1264
- limit: options.limit
1265
- });
1574
+ const query = serializeListOptions(options ?? {});
1266
1575
  const res = await doFetch(`${base}/campaigns/${campaignId}/executions`, {
1267
1576
  method: "GET",
1268
1577
  query
@@ -1316,6 +1625,51 @@ function createCampaignsApi(cfg) {
1316
1625
  const response = await fetchExecutionsPage(campaignId, normalizedOptions);
1317
1626
  const fetchPage = (opts) => fetchExecutionsPage(campaignId, opts);
1318
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();
1319
1673
  }
1320
1674
  };
1321
1675
  }
@@ -1396,6 +1750,70 @@ function createCatalogsApi(cfg) {
1396
1750
  };
1397
1751
  }
1398
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
+
1399
1817
  // src/api/tools.ts
1400
1818
  var IDEMPOTENCY_HEADER = "Idempotency-Key";
1401
1819
  var generateIdempotencyKey = (explicit) => {
@@ -1464,7 +1882,15 @@ function createToolsApi(cfg) {
1464
1882
  });
1465
1883
  return res.json();
1466
1884
  };
1467
- return {
1885
+ const fetchToolConnectionsPage = async (options = {}) => {
1886
+ const query = serializeListOptions(options ?? {});
1887
+ const res = await doFetch(`${base}/tools/connections`, {
1888
+ method: "GET",
1889
+ query
1890
+ });
1891
+ return res.json();
1892
+ };
1893
+ const api = {
1468
1894
  async list(options = {}) {
1469
1895
  const normalizedOptions = { ...options ?? {} };
1470
1896
  const response = await fetchToolsPage(normalizedOptions);
@@ -1478,6 +1904,17 @@ function createToolsApi(cfg) {
1478
1904
  const response = await fetchPage(normalizedOptions);
1479
1905
  return attachPaginator(response, fetchPage, normalizedOptions);
1480
1906
  },
1907
+ async listConnections(options = {}) {
1908
+ const normalizedOptions = {
1909
+ ...options ?? {}
1910
+ };
1911
+ const response = await fetchToolConnectionsPage(normalizedOptions);
1912
+ return attachPaginator(
1913
+ response,
1914
+ fetchToolConnectionsPage,
1915
+ normalizedOptions
1916
+ );
1917
+ },
1481
1918
  async uploadResource(toolId, payload) {
1482
1919
  const formData = toFormData(payload);
1483
1920
  const res = await doFetch(`${base}/tools/${toolId}/resources`, {
@@ -1523,8 +1960,115 @@ function createToolsApi(cfg) {
1523
1960
  body: JSON.stringify(payload)
1524
1961
  });
1525
1962
  return res.json();
1963
+ },
1964
+ async createConnection(payload, options = {}) {
1965
+ const idempotencyKey = generateIdempotencyKey(options.idempotencyKey);
1966
+ const res = await doFetch(`${base}/tools/connections`, {
1967
+ method: "POST",
1968
+ headers: {
1969
+ ...jsonHeaders,
1970
+ [IDEMPOTENCY_HEADER]: idempotencyKey
1971
+ },
1972
+ body: JSON.stringify(payload)
1973
+ });
1974
+ return res.json();
1975
+ },
1976
+ async executeConnection(toolAgentConnectionId, payload, options = {}) {
1977
+ const idempotencyKey = generateIdempotencyKey(options.idempotencyKey);
1978
+ const res = await doFetch(
1979
+ `${base}/tools/connections/${toolAgentConnectionId}/execute`,
1980
+ {
1981
+ method: "POST",
1982
+ headers: {
1983
+ ...jsonHeaders,
1984
+ [IDEMPOTENCY_HEADER]: idempotencyKey
1985
+ },
1986
+ body: JSON.stringify(payload)
1987
+ }
1988
+ );
1989
+ return res.json();
1526
1990
  }
1527
1991
  };
1992
+ const connections = {
1993
+ connect: api.connect,
1994
+ create: api.createConnection,
1995
+ execute: api.executeConnection,
1996
+ list: api.listConnections,
1997
+ createConnection: api.createConnection,
1998
+ executeConnection: api.executeConnection
1999
+ };
2000
+ return {
2001
+ ...api,
2002
+ connections
2003
+ };
2004
+ }
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;
1528
2072
  }
1529
2073
 
1530
2074
  // src/utils/catalog-voices.ts
@@ -1585,9 +2129,9 @@ function pickGender(value) {
1585
2129
  }
1586
2130
 
1587
2131
  // src/utils/catalog-filter.ts
1588
- import { Query } from "@getsupervisor/api-query-builder";
2132
+ import { Query as Query2 } from "@getsupervisor/api-query-builder";
1589
2133
  function createCatalogTypeQuery(type) {
1590
- return new Query((qb) => qb.eq("type", type));
2134
+ return new Query2((qb) => qb.eq("type", type));
1591
2135
  }
1592
2136
  function ensureCatalogTypeFilter(filter, type) {
1593
2137
  const requiredQuery = createCatalogTypeQuery(type);
@@ -1823,6 +2367,35 @@ function createWebhooksApi(cfg) {
1823
2367
  `${base}/webhooks/${webhookId}/subscriptions/${subscriptionId}`,
1824
2368
  { method: "DELETE" }
1825
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();
1826
2399
  }
1827
2400
  };
1828
2401
  }
@@ -1831,6 +2404,26 @@ function createWebhooksApi(cfg) {
1831
2404
  function createWorkspacesApi(cfg) {
1832
2405
  const { base, doFetch } = createHttp(cfg);
1833
2406
  const jsonHeaders = { "content-type": "application/json" };
2407
+ const fetchWorkspacesPage = async (options = {}) => {
2408
+ const normalized = { ...options ?? {} };
2409
+ const query = serializeListOptions({
2410
+ page: normalized.page,
2411
+ limit: normalized.limit,
2412
+ sort: normalized.sort,
2413
+ fields: normalized.fields,
2414
+ include: normalized.include,
2415
+ search: normalized.search,
2416
+ filter: normalized.filter,
2417
+ or: normalized.or
2418
+ });
2419
+ const headers = normalized.refreshCache ? { "X-Cache-Refresh": "true" } : void 0;
2420
+ const res = await doFetch(`${base}/workspaces`, {
2421
+ method: "GET",
2422
+ query,
2423
+ headers
2424
+ });
2425
+ return res.json();
2426
+ };
1834
2427
  const fetchPhonesPage = async (workspaceId, opts = {}) => {
1835
2428
  const { channel } = opts ?? {};
1836
2429
  const query = serializeListOptions(
@@ -1853,6 +2446,11 @@ function createWorkspacesApi(cfg) {
1853
2446
  return res.json();
1854
2447
  };
1855
2448
  return {
2449
+ async list(options = {}) {
2450
+ const normalizedOptions = { ...options ?? {} };
2451
+ const response = await fetchWorkspacesPage(normalizedOptions);
2452
+ return attachPaginator(response, fetchWorkspacesPage, normalizedOptions);
2453
+ },
1856
2454
  async listPhones(workspaceId, opts = {}) {
1857
2455
  const normalizedOptions = {
1858
2456
  ...opts ?? {}
@@ -1861,8 +2459,8 @@ function createWorkspacesApi(cfg) {
1861
2459
  const fetchPage = (options) => fetchPhonesPage(workspaceId, options);
1862
2460
  return attachPaginator(response, fetchPage, normalizedOptions);
1863
2461
  },
1864
- async enable(workspaceId, payload) {
1865
- const res = await doFetch(`${base}/workspaces/${workspaceId}/enable`, {
2462
+ async enable(payload) {
2463
+ const res = await doFetch(`${base}/workspaces/enable`, {
1866
2464
  method: "POST",
1867
2465
  headers: jsonHeaders,
1868
2466
  body: JSON.stringify(payload)
@@ -1908,6 +2506,7 @@ function createClient(initialCfg) {
1908
2506
  const stagesApi = createAgentStagesApi(runtimeCfg);
1909
2507
  const voicesApi = createVoicesApi(runtimeCfg);
1910
2508
  const apiKeysApi = createApiKeysApi(runtimeCfg);
2509
+ const callsApi = createCallsApi(runtimeCfg);
1911
2510
  const catalogsApi = createCatalogsApi(runtimeCfg);
1912
2511
  const catalogTemplatesApi = createCatalogTemplatesApi(runtimeCfg);
1913
2512
  const webhooksApi = createWebhooksApi(runtimeCfg);
@@ -1976,9 +2575,14 @@ function createClient(initialCfg) {
1976
2575
  ...catalogsApi,
1977
2576
  templates: catalogTemplatesApi
1978
2577
  },
2578
+ documents: createDocumentsApi(runtimeCfg),
1979
2579
  campaigns: createCampaignsApi(runtimeCfg),
1980
2580
  voices: voicesApi,
1981
2581
  apiKeys: apiKeysApi,
2582
+ billing: createBillingApi(runtimeCfg),
2583
+ calls: callsApi,
2584
+ usage: createUsageApi(runtimeCfg),
2585
+ sip: createSipTrunksApi(runtimeCfg),
1982
2586
  webhooks: webhooksApi
1983
2587
  };
1984
2588
  return {
@@ -2040,6 +2644,7 @@ export {
2040
2644
  HttpError,
2041
2645
  NetworkError,
2042
2646
  TimeoutError,
2647
+ WorkspaceNotSelectedError,
2043
2648
  bindAgentBlueprints,
2044
2649
  bindAgentPhones,
2045
2650
  bindAgentSchedule,
@@ -2060,14 +2665,21 @@ export {
2060
2665
  createAgentVersionsApi,
2061
2666
  createAgentsApi,
2062
2667
  createApiKeysApi,
2668
+ createBillingApi,
2669
+ createCallsApi,
2063
2670
  createCampaignsApi,
2064
2671
  createCatalogTemplatesApi,
2065
2672
  createCatalogsApi,
2066
2673
  createClient,
2674
+ createDocumentsApi,
2067
2675
  createHttp,
2676
+ createSipTrunksApi,
2068
2677
  createToolsApi,
2678
+ createUsageApi,
2069
2679
  createVoicesApi,
2070
2680
  createWebhooksApi,
2071
- createWorkspacesApi
2681
+ createWorkspacesApi,
2682
+ isApiErrorBody,
2683
+ isApiHttpError
2072
2684
  };
2073
2685
  //# sourceMappingURL=index.js.map