@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.js CHANGED
@@ -890,102 +890,6 @@ function createAgentVersionsApi(cfg) {
890
890
  };
891
891
  }
892
892
 
893
- // src/utils/sse-stream.ts
894
- function extractField(line, prefix) {
895
- if (!line.startsWith(prefix)) {
896
- return null;
897
- }
898
- return line.slice(prefix.length);
899
- }
900
- function parseSseMessage(raw) {
901
- let type = "";
902
- let data = "";
903
- for (const line of raw.split("\n")) {
904
- type = extractField(line, "event: ") ?? type;
905
- data = extractField(line, "data: ") ?? data;
906
- }
907
- if (!type || !data) {
908
- return null;
909
- }
910
- return { type, data };
911
- }
912
- function dispatchSseEvent(event, handlers) {
913
- const handler = handlers[event.type];
914
- if (!handler) {
915
- return void 0;
916
- }
917
- try {
918
- const parsed = JSON.parse(event.data);
919
- handler(parsed);
920
- return parsed;
921
- } catch {
922
- return void 0;
923
- }
924
- }
925
- function processRawPart(raw, handlers, resolveEvent) {
926
- const trimmed = raw.trim();
927
- if (!trimmed) {
928
- return void 0;
929
- }
930
- const event = parseSseMessage(trimmed);
931
- if (!event) {
932
- return void 0;
933
- }
934
- const result = dispatchSseEvent(event, handlers);
935
- if (event.type !== resolveEvent) {
936
- return void 0;
937
- }
938
- return result;
939
- }
940
- function flushRemainingBuffer(buffer) {
941
- if (!buffer.trim()) {
942
- return "";
943
- }
944
- return buffer + "\n\n";
945
- }
946
- function consumeSseStream(response, handlers, resolveEvent) {
947
- if (!response.body) {
948
- throw new Error("No response body for SSE stream");
949
- }
950
- return new Promise((resolve, reject) => {
951
- let resolved;
952
- let buffer = "";
953
- const reader = response.body.getReader();
954
- const decoder = new TextDecoder();
955
- const processBuffer = () => {
956
- const parts = buffer.split("\n\n");
957
- buffer = parts.pop() ?? "";
958
- for (const part of parts) {
959
- const result = processRawPart(part, handlers, resolveEvent);
960
- if (result) {
961
- resolved = result;
962
- }
963
- }
964
- };
965
- const finalize = () => {
966
- buffer = flushRemainingBuffer(buffer);
967
- processBuffer();
968
- if (resolved) {
969
- resolve(resolved);
970
- return;
971
- }
972
- reject(new Error(`SSE stream ended without '${resolveEvent}' event`));
973
- };
974
- const read = () => {
975
- reader.read().then(({ done, value }) => {
976
- if (done) {
977
- finalize();
978
- return;
979
- }
980
- buffer += decoder.decode(value, { stream: true });
981
- processBuffer();
982
- read();
983
- }).catch(reject);
984
- };
985
- read();
986
- });
987
- }
988
-
989
893
  // src/entities/agent.ts
990
894
  var bindAgentStageTriggers = (api, agentId, blueprintId, stageId) => ({
991
895
  list(opts) {
@@ -1206,36 +1110,8 @@ var createAgentEntity = (dto, options) => {
1206
1110
  };
1207
1111
 
1208
1112
  // src/api/agents.ts
1209
- function normalizeCloneSelection(selection) {
1210
- if (!selection) {
1211
- return [];
1212
- }
1213
- if (Array.isArray(selection)) {
1214
- return selection;
1215
- }
1216
- return Object.entries(selection).filter(([, enabled]) => enabled).map(([component]) => component);
1217
- }
1218
- function buildCloneRequest(payload) {
1219
- const { clone, ...rest } = payload;
1220
- return { ...rest, clone: normalizeCloneSelection(clone) };
1221
- }
1222
- function buildSseHandlers(callbacks) {
1223
- return {
1224
- "agent-created": (data) => callbacks.onAgentCreated?.(data),
1225
- "runtime-progress": (data) => callbacks.onProgress?.(data),
1226
- "runtime-complete": (data) => callbacks.onComplete?.(data),
1227
- "runtime-error": (data) => callbacks.onError?.(data)
1228
- };
1229
- }
1230
- function resolveAgentId(agent) {
1231
- if (typeof agent === "string") {
1232
- return agent;
1233
- }
1234
- return agent.agentId;
1235
- }
1236
1113
  function createAgentsApi(cfg, relatedApis) {
1237
- const { base, doFetch, buildHeaders, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1238
- const jsonHeaders = { "content-type": "application/json" };
1114
+ const { base, doFetch, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
1239
1115
  const requireWorkspace = () => {
1240
1116
  const workspaceId = resolveWorkspaceId();
1241
1117
  if (typeof workspaceId === "string" && workspaceId.trim().length > 0) {
@@ -1247,144 +1123,167 @@ function createAgentsApi(cfg, relatedApis) {
1247
1123
  }
1248
1124
  throw new WorkspaceNotSelectedError();
1249
1125
  };
1250
- const postJson = async (url, payload) => {
1126
+ const jsonHeaders = { "content-type": "application/json" };
1127
+ const normalizeCloneSelection = (selection) => {
1128
+ if (!selection) {
1129
+ return [];
1130
+ }
1131
+ if (Array.isArray(selection)) {
1132
+ return selection;
1133
+ }
1134
+ return Object.entries(selection).filter(([, enabled]) => enabled).map(([component]) => component);
1135
+ };
1136
+ const buildCloneAgentRequest = (payload) => {
1137
+ const { clone, ...rest } = payload;
1138
+ return {
1139
+ ...rest,
1140
+ clone: normalizeCloneSelection(clone)
1141
+ };
1142
+ };
1143
+ const fetchAgentsPage = async (options = {}) => {
1251
1144
  requireWorkspace();
1252
- const res = await doFetch(url, {
1145
+ const sanitizedOptions = {
1146
+ page: options.page,
1147
+ limit: options.limit,
1148
+ filter: options.filter
1149
+ };
1150
+ const query = serializeListOptions(sanitizedOptions);
1151
+ const res = await doFetch(`${base}/agents`, {
1152
+ method: "GET",
1153
+ query
1154
+ });
1155
+ return res.json();
1156
+ };
1157
+ const listAgents = async (options = {}) => {
1158
+ const normalizedOptions = { ...options ?? {} };
1159
+ const response = await fetchAgentsPage(normalizedOptions);
1160
+ return attachPaginator(response, fetchAgentsPage, normalizedOptions);
1161
+ };
1162
+ const getAgentDetail = async (agentId) => {
1163
+ requireWorkspace();
1164
+ const res = await doFetch(`${base}/agents/${agentId}`, {
1165
+ method: "GET"
1166
+ });
1167
+ return res.json();
1168
+ };
1169
+ const createAgent = async (payload) => {
1170
+ requireWorkspace();
1171
+ const res = await doFetch(`${base}/agents`, {
1253
1172
  method: "POST",
1254
1173
  body: JSON.stringify(payload),
1255
1174
  headers: jsonHeaders
1256
1175
  });
1257
1176
  return res.json();
1258
1177
  };
1259
- const postWithSse = async (url, payload, callbacks) => {
1178
+ const forkAgentFromTemplate = async (payload) => {
1260
1179
  requireWorkspace();
1261
- const headers = buildHeaders({ "content-type": "application/json" });
1262
- const fx = cfg.fetchImpl ?? fetch;
1263
- const res = await fx(url, {
1180
+ const res = await doFetch(`${base}/agents/from-template`, {
1264
1181
  method: "POST",
1265
1182
  body: JSON.stringify(payload),
1266
- headers
1183
+ headers: jsonHeaders
1267
1184
  });
1268
- const contentType = res.headers.get("content-type") ?? "";
1269
- if (contentType.includes("application/json")) {
1270
- const agent = await res.json();
1271
- callbacks.onAgentCreated?.(agent);
1272
- return agent;
1273
- }
1274
- return consumeSseStream(
1275
- res,
1276
- buildSseHandlers(callbacks),
1277
- "agent-created"
1278
- );
1185
+ return res.json();
1279
1186
  };
1280
- const fetchPage = async (options = {}) => {
1187
+ const cloneAgent = async (agentId, payload) => {
1281
1188
  requireWorkspace();
1282
- const query = serializeListOptions({
1283
- page: options.page,
1284
- limit: options.limit,
1285
- filter: options.filter
1189
+ const requestPayload = buildCloneAgentRequest(payload);
1190
+ const res = await doFetch(`${base}/agents/${agentId}/clone`, {
1191
+ method: "POST",
1192
+ body: JSON.stringify(requestPayload),
1193
+ headers: jsonHeaders
1286
1194
  });
1287
- const res = await doFetch(`${base}/agents`, { method: "GET", query });
1288
1195
  return res.json();
1289
1196
  };
1197
+ const updateAgent = async (agentId, payload) => {
1198
+ requireWorkspace();
1199
+ const res = await doFetch(`${base}/agents/${agentId}`, {
1200
+ method: "PATCH",
1201
+ body: JSON.stringify(payload),
1202
+ headers: jsonHeaders
1203
+ });
1204
+ return res.json();
1205
+ };
1206
+ const resolveAgentId = (agent) => {
1207
+ return typeof agent === "string" ? agent : agent.agentId;
1208
+ };
1209
+ const deleteAgent = async (agent) => {
1210
+ requireWorkspace();
1211
+ const agentId = resolveAgentId(agent);
1212
+ await doFetch(`${base}/agents/${agentId}`, {
1213
+ method: "DELETE"
1214
+ });
1215
+ };
1290
1216
  const baseApi = {
1291
- async list(options = {}) {
1292
- const opts = { ...options ?? {} };
1293
- const response = await fetchPage(opts);
1294
- return attachPaginator(response, fetchPage, opts);
1295
- },
1296
- async get(agentId) {
1297
- requireWorkspace();
1298
- const res = await doFetch(`${base}/agents/${agentId}`, { method: "GET" });
1299
- return res.json();
1300
- },
1301
- async create(payload, callbacks) {
1302
- if (callbacks) {
1303
- return postWithSse(`${base}/agents`, payload, callbacks);
1304
- }
1305
- return postJson(`${base}/agents`, payload);
1306
- },
1307
- async clone(agentId, payload) {
1308
- return postJson(
1309
- `${base}/agents/${agentId}/clone`,
1310
- buildCloneRequest(payload)
1311
- );
1312
- },
1313
- async forkFromTemplate(payload, callbacks) {
1314
- if (callbacks) {
1315
- return postWithSse(`${base}/agents/from-template`, payload, callbacks);
1316
- }
1317
- return postJson(`${base}/agents/from-template`, payload);
1318
- },
1319
- async update(agentId, payload) {
1320
- requireWorkspace();
1321
- const res = await doFetch(`${base}/agents/${agentId}`, {
1322
- method: "PATCH",
1323
- body: JSON.stringify(payload),
1324
- headers: jsonHeaders
1325
- });
1326
- return res.json();
1327
- },
1328
- async presignDocuments(files) {
1329
- return postJson(`${base}/documents`, { files });
1330
- },
1331
- async delete(agent) {
1332
- requireWorkspace();
1333
- await doFetch(`${base}/agents/${resolveAgentId(agent)}`, {
1334
- method: "DELETE"
1335
- });
1336
- }
1217
+ list: listAgents,
1218
+ get: getAgentDetail,
1219
+ create: createAgent,
1220
+ clone: cloneAgent,
1221
+ forkFromTemplate: forkAgentFromTemplate,
1222
+ update: updateAgent,
1223
+ delete: deleteAgent
1337
1224
  };
1338
1225
  if (!relatedApis) {
1339
1226
  return baseApi;
1340
1227
  }
1341
- return withEntityWrapping(baseApi, fetchPage, relatedApis);
1342
- }
1343
- function withEntityWrapping(api, fetchPage, deps) {
1344
1228
  const wrapAgent = (detail) => createAgentEntity(detail, {
1345
- ...deps,
1229
+ tagsApi: relatedApis.tagsApi,
1230
+ phonesApi: relatedApis.phonesApi,
1231
+ scheduleApi: relatedApis.scheduleApi,
1232
+ scheduleExceptionsApi: relatedApis.scheduleExceptionsApi,
1233
+ versionsApi: relatedApis.versionsApi,
1234
+ blueprintsApi: relatedApis.blueprintsApi,
1235
+ stagesApi: relatedApis.stagesApi,
1236
+ stageTriggersApi: relatedApis.stageTriggersApi,
1346
1237
  reload: async (agentId) => {
1347
- const latest = await api.get(agentId);
1238
+ const latest = await getAgentDetail(agentId);
1348
1239
  return wrapAgent(latest);
1349
1240
  },
1350
1241
  updateAgent: async (agentId, payload) => {
1351
- const updated = await api.update(agentId, payload);
1242
+ const updated = await updateAgent(agentId, payload);
1352
1243
  return wrapAgent(updated);
1353
1244
  },
1354
1245
  deleteAgent: async (agentId) => {
1355
- await api.delete(agentId);
1246
+ await deleteAgent(agentId);
1356
1247
  },
1357
1248
  cloneAgent: async (agentId, payload) => {
1358
- const cloned = await api.clone(agentId, payload);
1249
+ const cloned = await cloneAgent(agentId, payload);
1359
1250
  return wrapAgent(cloned);
1360
1251
  }
1361
1252
  });
1362
- const wrapList = async (opts) => {
1363
- const result = await fetchPage(opts);
1364
- const items = Array.isArray(result.data) ? result.data : [];
1365
- return { ...result, data: items.map(wrapAgent) };
1366
- };
1367
1253
  return {
1368
- ...api,
1254
+ ...baseApi,
1369
1255
  async list(options = {}) {
1370
- const opts = { ...options ?? {} };
1371
- const initial = await wrapList(opts);
1372
- return attachPaginator(initial, wrapList, opts);
1256
+ const normalizedOptions = { ...options ?? {} };
1257
+ const applyWrap = async (opts) => {
1258
+ const result = await fetchAgentsPage(opts);
1259
+ const items = Array.isArray(result.data) ? result.data : [];
1260
+ return {
1261
+ ...result,
1262
+ data: items.map((summary) => wrapAgent(summary))
1263
+ };
1264
+ };
1265
+ const initial = await applyWrap(normalizedOptions);
1266
+ return attachPaginator(initial, applyWrap, normalizedOptions);
1373
1267
  },
1374
1268
  async get(agentId) {
1375
- return wrapAgent(await api.get(agentId));
1269
+ const detail = await getAgentDetail(agentId);
1270
+ return wrapAgent(detail);
1376
1271
  },
1377
- async create(payload, callbacks) {
1378
- return wrapAgent(await api.create(payload, callbacks));
1272
+ async create(payload) {
1273
+ const detail = await createAgent(payload);
1274
+ return wrapAgent(detail);
1379
1275
  },
1380
1276
  async clone(agentId, payload) {
1381
- return wrapAgent(await api.clone(agentId, payload));
1277
+ const detail = await cloneAgent(agentId, payload);
1278
+ return wrapAgent(detail);
1382
1279
  },
1383
- async forkFromTemplate(payload, callbacks) {
1384
- return wrapAgent(await api.forkFromTemplate(payload, callbacks));
1280
+ async forkFromTemplate(payload) {
1281
+ const detail = await forkAgentFromTemplate(payload);
1282
+ return wrapAgent(detail);
1385
1283
  },
1386
1284
  async update(agentId, payload) {
1387
- return wrapAgent(await api.update(agentId, payload));
1285
+ const detail = await updateAgent(agentId, payload);
1286
+ return wrapAgent(detail);
1388
1287
  }
1389
1288
  };
1390
1289
  }
@@ -1511,10 +1410,7 @@ function createCampaignsApi(cfg) {
1511
1410
  return res.json();
1512
1411
  };
1513
1412
  const fetchExecutionsPage = async (campaignId, options = {}) => {
1514
- const query = serializeListOptions({
1515
- page: options.page,
1516
- limit: options.limit
1517
- });
1413
+ const query = serializeListOptions(options ?? {});
1518
1414
  const res = await doFetch(`${base}/campaigns/${campaignId}/executions`, {
1519
1415
  method: "GET",
1520
1416
  query
@@ -1568,6 +1464,51 @@ function createCampaignsApi(cfg) {
1568
1464
  const response = await fetchExecutionsPage(campaignId, normalizedOptions);
1569
1465
  const fetchPage = (opts) => fetchExecutionsPage(campaignId, opts);
1570
1466
  return attachPaginator(response, fetchPage, normalizedOptions);
1467
+ },
1468
+ /**
1469
+ * Pausa una campaña en curso. Los reintentos ya programados se
1470
+ * reagendan al ejecutarse (sin consumir intentos) hasta `resume()`.
1471
+ */
1472
+ async pause(campaignId) {
1473
+ const res = await doFetch(`${base}/campaigns/${campaignId}/pause`, {
1474
+ method: "POST"
1475
+ });
1476
+ return res.json();
1477
+ },
1478
+ /**
1479
+ * Reanuda una campaña pausada. Re-encola los reintentos pendientes
1480
+ * según su `nextRetryAt`.
1481
+ */
1482
+ async resume(campaignId) {
1483
+ const res = await doFetch(`${base}/campaigns/${campaignId}/resume`, {
1484
+ method: "POST"
1485
+ });
1486
+ return res.json();
1487
+ },
1488
+ /**
1489
+ * Cancela definitivamente una campaña no terminal. Los reintentos
1490
+ * pendientes se descartan — operación destructiva.
1491
+ */
1492
+ async cancel(campaignId) {
1493
+ const res = await doFetch(`${base}/campaigns/${campaignId}/cancel`, {
1494
+ method: "POST"
1495
+ });
1496
+ return res.json();
1497
+ },
1498
+ /**
1499
+ * Descarga el CSV con todas las ejecuciones de la campaña. Devuelve un
1500
+ * `Blob` con `text/csv; charset=utf-8` (BOM UTF-8 incluido). Para
1501
+ * campañas > 20,000 filas la API responde 413.
1502
+ */
1503
+ async exportExecutions(campaignId) {
1504
+ const res = await doFetch(
1505
+ `${base}/campaigns/${campaignId}/executions/export`,
1506
+ {
1507
+ method: "GET",
1508
+ headers: { Accept: "text/csv" }
1509
+ }
1510
+ );
1511
+ return res.blob();
1571
1512
  }
1572
1513
  };
1573
1514
  }