@getsupervisor/agents-studio-sdk 1.41.2-patch.15 → 1.41.2-patch.17

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
@@ -953,102 +953,6 @@ function createAgentVersionsApi(cfg) {
953
953
  };
954
954
  }
955
955
 
956
- // src/utils/sse-stream.ts
957
- function extractField(line, prefix) {
958
- if (!line.startsWith(prefix)) {
959
- return null;
960
- }
961
- return line.slice(prefix.length);
962
- }
963
- function parseSseMessage(raw) {
964
- let type = "";
965
- let data = "";
966
- for (const line of raw.split("\n")) {
967
- type = extractField(line, "event: ") ?? type;
968
- data = extractField(line, "data: ") ?? data;
969
- }
970
- if (!type || !data) {
971
- return null;
972
- }
973
- return { type, data };
974
- }
975
- function dispatchSseEvent(event, handlers) {
976
- const handler = handlers[event.type];
977
- if (!handler) {
978
- return void 0;
979
- }
980
- try {
981
- const parsed = JSON.parse(event.data);
982
- handler(parsed);
983
- return parsed;
984
- } catch {
985
- return void 0;
986
- }
987
- }
988
- function processRawPart(raw, handlers, resolveEvent) {
989
- const trimmed = raw.trim();
990
- if (!trimmed) {
991
- return void 0;
992
- }
993
- const event = parseSseMessage(trimmed);
994
- if (!event) {
995
- return void 0;
996
- }
997
- const result = dispatchSseEvent(event, handlers);
998
- if (event.type !== resolveEvent) {
999
- return void 0;
1000
- }
1001
- return result;
1002
- }
1003
- function flushRemainingBuffer(buffer) {
1004
- if (!buffer.trim()) {
1005
- return "";
1006
- }
1007
- return buffer + "\n\n";
1008
- }
1009
- function consumeSseStream(response, handlers, resolveEvent) {
1010
- if (!response.body) {
1011
- throw new Error("No response body for SSE stream");
1012
- }
1013
- return new Promise((resolve, reject) => {
1014
- let resolved;
1015
- let buffer = "";
1016
- const reader = response.body.getReader();
1017
- const decoder = new TextDecoder();
1018
- const processBuffer = () => {
1019
- const parts = buffer.split("\n\n");
1020
- buffer = parts.pop() ?? "";
1021
- for (const part of parts) {
1022
- const result = processRawPart(part, handlers, resolveEvent);
1023
- if (result) {
1024
- resolved = result;
1025
- }
1026
- }
1027
- };
1028
- const finalize = () => {
1029
- buffer = flushRemainingBuffer(buffer);
1030
- processBuffer();
1031
- if (resolved) {
1032
- resolve(resolved);
1033
- return;
1034
- }
1035
- reject(new Error(`SSE stream ended without '${resolveEvent}' event`));
1036
- };
1037
- const read = () => {
1038
- reader.read().then(({ done, value }) => {
1039
- if (done) {
1040
- finalize();
1041
- return;
1042
- }
1043
- buffer += decoder.decode(value, { stream: true });
1044
- processBuffer();
1045
- read();
1046
- }).catch(reject);
1047
- };
1048
- read();
1049
- });
1050
- }
1051
-
1052
956
  // src/entities/agent.ts
1053
957
  var bindAgentStageTriggers = (api, agentId, blueprintId, stageId) => ({
1054
958
  list(opts) {
@@ -1269,36 +1173,8 @@ var createAgentEntity = (dto, options) => {
1269
1173
  };
1270
1174
 
1271
1175
  // src/api/agents.ts
1272
- function normalizeCloneSelection(selection) {
1273
- if (!selection) {
1274
- return [];
1275
- }
1276
- if (Array.isArray(selection)) {
1277
- return selection;
1278
- }
1279
- return Object.entries(selection).filter(([, enabled]) => enabled).map(([component]) => component);
1280
- }
1281
- function buildCloneRequest(payload) {
1282
- const { clone, ...rest } = payload;
1283
- return { ...rest, clone: normalizeCloneSelection(clone) };
1284
- }
1285
- function buildSseHandlers(callbacks) {
1286
- return {
1287
- "agent-created": (data) => callbacks.onAgentCreated?.(data),
1288
- "runtime-progress": (data) => callbacks.onProgress?.(data),
1289
- "runtime-complete": (data) => callbacks.onComplete?.(data),
1290
- "runtime-error": (data) => callbacks.onError?.(data)
1291
- };
1292
- }
1293
- function resolveAgentId(agent) {
1294
- if (typeof agent === "string") {
1295
- return agent;
1296
- }
1297
- return agent.agentId;
1298
- }
1299
1176
  function createAgentsApi(cfg, relatedApis) {
1300
- const { base, doFetch, buildHeaders, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1301
- const jsonHeaders = { "content-type": "application/json" };
1177
+ const { base, doFetch, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1302
1178
  const requireWorkspace = () => {
1303
1179
  const workspaceId = resolveWorkspaceId();
1304
1180
  if (typeof workspaceId === "string" && workspaceId.trim().length > 0) {
@@ -1310,144 +1186,167 @@ function createAgentsApi(cfg, relatedApis) {
1310
1186
  }
1311
1187
  throw new WorkspaceNotSelectedError();
1312
1188
  };
1313
- const postJson = async (url, payload) => {
1189
+ const jsonHeaders = { "content-type": "application/json" };
1190
+ const normalizeCloneSelection = (selection) => {
1191
+ if (!selection) {
1192
+ return [];
1193
+ }
1194
+ if (Array.isArray(selection)) {
1195
+ return selection;
1196
+ }
1197
+ return Object.entries(selection).filter(([, enabled]) => enabled).map(([component]) => component);
1198
+ };
1199
+ const buildCloneAgentRequest = (payload) => {
1200
+ const { clone, ...rest } = payload;
1201
+ return {
1202
+ ...rest,
1203
+ clone: normalizeCloneSelection(clone)
1204
+ };
1205
+ };
1206
+ const fetchAgentsPage = async (options = {}) => {
1314
1207
  requireWorkspace();
1315
- const res = await doFetch(url, {
1208
+ const sanitizedOptions = {
1209
+ page: options.page,
1210
+ limit: options.limit,
1211
+ filter: options.filter
1212
+ };
1213
+ const query = serializeListOptions(sanitizedOptions);
1214
+ const res = await doFetch(`${base}/agents`, {
1215
+ method: "GET",
1216
+ query
1217
+ });
1218
+ return res.json();
1219
+ };
1220
+ const listAgents = async (options = {}) => {
1221
+ const normalizedOptions = { ...options ?? {} };
1222
+ const response = await fetchAgentsPage(normalizedOptions);
1223
+ return attachPaginator(response, fetchAgentsPage, normalizedOptions);
1224
+ };
1225
+ const getAgentDetail = async (agentId) => {
1226
+ requireWorkspace();
1227
+ const res = await doFetch(`${base}/agents/${agentId}`, {
1228
+ method: "GET"
1229
+ });
1230
+ return res.json();
1231
+ };
1232
+ const createAgent = async (payload) => {
1233
+ requireWorkspace();
1234
+ const res = await doFetch(`${base}/agents`, {
1316
1235
  method: "POST",
1317
1236
  body: JSON.stringify(payload),
1318
1237
  headers: jsonHeaders
1319
1238
  });
1320
1239
  return res.json();
1321
1240
  };
1322
- const postWithSse = async (url, payload, callbacks) => {
1241
+ const forkAgentFromTemplate = async (payload) => {
1323
1242
  requireWorkspace();
1324
- const headers = buildHeaders({ "content-type": "application/json" });
1325
- const fx = cfg.fetchImpl ?? fetch;
1326
- const res = await fx(url, {
1243
+ const res = await doFetch(`${base}/agents/from-template`, {
1327
1244
  method: "POST",
1328
1245
  body: JSON.stringify(payload),
1329
- headers
1246
+ headers: jsonHeaders
1330
1247
  });
1331
- const contentType = res.headers.get("content-type") ?? "";
1332
- if (contentType.includes("application/json")) {
1333
- const agent = await res.json();
1334
- callbacks.onAgentCreated?.(agent);
1335
- return agent;
1336
- }
1337
- return consumeSseStream(
1338
- res,
1339
- buildSseHandlers(callbacks),
1340
- "agent-created"
1341
- );
1248
+ return res.json();
1342
1249
  };
1343
- const fetchPage = async (options = {}) => {
1250
+ const cloneAgent = async (agentId, payload) => {
1344
1251
  requireWorkspace();
1345
- const query = serializeListOptions({
1346
- page: options.page,
1347
- limit: options.limit,
1348
- filter: options.filter
1252
+ const requestPayload = buildCloneAgentRequest(payload);
1253
+ const res = await doFetch(`${base}/agents/${agentId}/clone`, {
1254
+ method: "POST",
1255
+ body: JSON.stringify(requestPayload),
1256
+ headers: jsonHeaders
1349
1257
  });
1350
- const res = await doFetch(`${base}/agents`, { method: "GET", query });
1351
1258
  return res.json();
1352
1259
  };
1260
+ const updateAgent = async (agentId, payload) => {
1261
+ requireWorkspace();
1262
+ const res = await doFetch(`${base}/agents/${agentId}`, {
1263
+ method: "PATCH",
1264
+ body: JSON.stringify(payload),
1265
+ headers: jsonHeaders
1266
+ });
1267
+ return res.json();
1268
+ };
1269
+ const resolveAgentId = (agent) => {
1270
+ return typeof agent === "string" ? agent : agent.agentId;
1271
+ };
1272
+ const deleteAgent = async (agent) => {
1273
+ requireWorkspace();
1274
+ const agentId = resolveAgentId(agent);
1275
+ await doFetch(`${base}/agents/${agentId}`, {
1276
+ method: "DELETE"
1277
+ });
1278
+ };
1353
1279
  const baseApi = {
1354
- async list(options = {}) {
1355
- const opts = { ...options ?? {} };
1356
- const response = await fetchPage(opts);
1357
- return attachPaginator(response, fetchPage, opts);
1358
- },
1359
- async get(agentId) {
1360
- requireWorkspace();
1361
- const res = await doFetch(`${base}/agents/${agentId}`, { method: "GET" });
1362
- return res.json();
1363
- },
1364
- async create(payload, callbacks) {
1365
- if (callbacks) {
1366
- return postWithSse(`${base}/agents`, payload, callbacks);
1367
- }
1368
- return postJson(`${base}/agents`, payload);
1369
- },
1370
- async clone(agentId, payload) {
1371
- return postJson(
1372
- `${base}/agents/${agentId}/clone`,
1373
- buildCloneRequest(payload)
1374
- );
1375
- },
1376
- async forkFromTemplate(payload, callbacks) {
1377
- if (callbacks) {
1378
- return postWithSse(`${base}/agents/from-template`, payload, callbacks);
1379
- }
1380
- return postJson(`${base}/agents/from-template`, payload);
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 presignDocuments(files) {
1392
- return postJson(`${base}/documents`, { files });
1393
- },
1394
- async delete(agent) {
1395
- requireWorkspace();
1396
- await doFetch(`${base}/agents/${resolveAgentId(agent)}`, {
1397
- method: "DELETE"
1398
- });
1399
- }
1280
+ list: listAgents,
1281
+ get: getAgentDetail,
1282
+ create: createAgent,
1283
+ clone: cloneAgent,
1284
+ forkFromTemplate: forkAgentFromTemplate,
1285
+ update: updateAgent,
1286
+ delete: deleteAgent
1400
1287
  };
1401
1288
  if (!relatedApis) {
1402
1289
  return baseApi;
1403
1290
  }
1404
- return withEntityWrapping(baseApi, fetchPage, relatedApis);
1405
- }
1406
- function withEntityWrapping(api, fetchPage, deps) {
1407
1291
  const wrapAgent = (detail) => createAgentEntity(detail, {
1408
- ...deps,
1292
+ tagsApi: relatedApis.tagsApi,
1293
+ phonesApi: relatedApis.phonesApi,
1294
+ scheduleApi: relatedApis.scheduleApi,
1295
+ scheduleExceptionsApi: relatedApis.scheduleExceptionsApi,
1296
+ versionsApi: relatedApis.versionsApi,
1297
+ blueprintsApi: relatedApis.blueprintsApi,
1298
+ stagesApi: relatedApis.stagesApi,
1299
+ stageTriggersApi: relatedApis.stageTriggersApi,
1409
1300
  reload: async (agentId) => {
1410
- const latest = await api.get(agentId);
1301
+ const latest = await getAgentDetail(agentId);
1411
1302
  return wrapAgent(latest);
1412
1303
  },
1413
1304
  updateAgent: async (agentId, payload) => {
1414
- const updated = await api.update(agentId, payload);
1305
+ const updated = await updateAgent(agentId, payload);
1415
1306
  return wrapAgent(updated);
1416
1307
  },
1417
1308
  deleteAgent: async (agentId) => {
1418
- await api.delete(agentId);
1309
+ await deleteAgent(agentId);
1419
1310
  },
1420
1311
  cloneAgent: async (agentId, payload) => {
1421
- const cloned = await api.clone(agentId, payload);
1312
+ const cloned = await cloneAgent(agentId, payload);
1422
1313
  return wrapAgent(cloned);
1423
1314
  }
1424
1315
  });
1425
- const wrapList = async (opts) => {
1426
- const result = await fetchPage(opts);
1427
- const items = Array.isArray(result.data) ? result.data : [];
1428
- return { ...result, data: items.map(wrapAgent) };
1429
- };
1430
1316
  return {
1431
- ...api,
1317
+ ...baseApi,
1432
1318
  async list(options = {}) {
1433
- const opts = { ...options ?? {} };
1434
- const initial = await wrapList(opts);
1435
- return attachPaginator(initial, wrapList, opts);
1319
+ const normalizedOptions = { ...options ?? {} };
1320
+ const applyWrap = async (opts) => {
1321
+ const result = await fetchAgentsPage(opts);
1322
+ const items = Array.isArray(result.data) ? result.data : [];
1323
+ return {
1324
+ ...result,
1325
+ data: items.map((summary) => wrapAgent(summary))
1326
+ };
1327
+ };
1328
+ const initial = await applyWrap(normalizedOptions);
1329
+ return attachPaginator(initial, applyWrap, normalizedOptions);
1436
1330
  },
1437
1331
  async get(agentId) {
1438
- return wrapAgent(await api.get(agentId));
1332
+ const detail = await getAgentDetail(agentId);
1333
+ return wrapAgent(detail);
1439
1334
  },
1440
- async create(payload, callbacks) {
1441
- return wrapAgent(await api.create(payload, callbacks));
1335
+ async create(payload) {
1336
+ const detail = await createAgent(payload);
1337
+ return wrapAgent(detail);
1442
1338
  },
1443
1339
  async clone(agentId, payload) {
1444
- return wrapAgent(await api.clone(agentId, payload));
1340
+ const detail = await cloneAgent(agentId, payload);
1341
+ return wrapAgent(detail);
1445
1342
  },
1446
- async forkFromTemplate(payload, callbacks) {
1447
- return wrapAgent(await api.forkFromTemplate(payload, callbacks));
1343
+ async forkFromTemplate(payload) {
1344
+ const detail = await forkAgentFromTemplate(payload);
1345
+ return wrapAgent(detail);
1448
1346
  },
1449
1347
  async update(agentId, payload) {
1450
- return wrapAgent(await api.update(agentId, payload));
1348
+ const detail = await updateAgent(agentId, payload);
1349
+ return wrapAgent(detail);
1451
1350
  }
1452
1351
  };
1453
1352
  }
@@ -1574,10 +1473,7 @@ function createCampaignsApi(cfg) {
1574
1473
  return res.json();
1575
1474
  };
1576
1475
  const fetchExecutionsPage = async (campaignId, options = {}) => {
1577
- const query = serializeListOptions({
1578
- page: options.page,
1579
- limit: options.limit
1580
- });
1476
+ const query = serializeListOptions(options ?? {});
1581
1477
  const res = await doFetch(`${base}/campaigns/${campaignId}/executions`, {
1582
1478
  method: "GET",
1583
1479
  query
@@ -1631,6 +1527,51 @@ function createCampaignsApi(cfg) {
1631
1527
  const response = await fetchExecutionsPage(campaignId, normalizedOptions);
1632
1528
  const fetchPage = (opts) => fetchExecutionsPage(campaignId, opts);
1633
1529
  return attachPaginator(response, fetchPage, normalizedOptions);
1530
+ },
1531
+ /**
1532
+ * Pausa una campaña en curso. Los reintentos ya programados se
1533
+ * reagendan al ejecutarse (sin consumir intentos) hasta `resume()`.
1534
+ */
1535
+ async pause(campaignId) {
1536
+ const res = await doFetch(`${base}/campaigns/${campaignId}/pause`, {
1537
+ method: "POST"
1538
+ });
1539
+ return res.json();
1540
+ },
1541
+ /**
1542
+ * Reanuda una campaña pausada. Re-encola los reintentos pendientes
1543
+ * según su `nextRetryAt`.
1544
+ */
1545
+ async resume(campaignId) {
1546
+ const res = await doFetch(`${base}/campaigns/${campaignId}/resume`, {
1547
+ method: "POST"
1548
+ });
1549
+ return res.json();
1550
+ },
1551
+ /**
1552
+ * Cancela definitivamente una campaña no terminal. Los reintentos
1553
+ * pendientes se descartan — operación destructiva.
1554
+ */
1555
+ async cancel(campaignId) {
1556
+ const res = await doFetch(`${base}/campaigns/${campaignId}/cancel`, {
1557
+ method: "POST"
1558
+ });
1559
+ return res.json();
1560
+ },
1561
+ /**
1562
+ * Descarga el CSV con todas las ejecuciones de la campaña. Devuelve un
1563
+ * `Blob` con `text/csv; charset=utf-8` (BOM UTF-8 incluido). Para
1564
+ * campañas > 20,000 filas la API responde 413.
1565
+ */
1566
+ async exportExecutions(campaignId) {
1567
+ const res = await doFetch(
1568
+ `${base}/campaigns/${campaignId}/executions/export`,
1569
+ {
1570
+ method: "GET",
1571
+ headers: { Accept: "text/csv" }
1572
+ }
1573
+ );
1574
+ return res.blob();
1634
1575
  }
1635
1576
  };
1636
1577
  }