@getsupervisor/agents-studio-sdk 1.41.2-patch.14 → 1.41.2-patch.15
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/README.md +39 -0
- package/dist/index.cjs +258 -118
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +168 -12
- package/dist/index.d.ts +168 -12
- package/dist/index.js +257 -118
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -890,6 +890,102 @@ 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
|
+
|
|
893
989
|
// src/entities/agent.ts
|
|
894
990
|
var bindAgentStageTriggers = (api, agentId, blueprintId, stageId) => ({
|
|
895
991
|
list(opts) {
|
|
@@ -1110,8 +1206,36 @@ var createAgentEntity = (dto, options) => {
|
|
|
1110
1206
|
};
|
|
1111
1207
|
|
|
1112
1208
|
// 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
|
+
}
|
|
1113
1236
|
function createAgentsApi(cfg, relatedApis) {
|
|
1114
|
-
const { base, doFetch, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
|
|
1237
|
+
const { base, doFetch, buildHeaders, resolveWorkspaceId, resolveApiKey } = createHttp(cfg);
|
|
1238
|
+
const jsonHeaders = { "content-type": "application/json" };
|
|
1115
1239
|
const requireWorkspace = () => {
|
|
1116
1240
|
const workspaceId = resolveWorkspaceId();
|
|
1117
1241
|
if (typeof workspaceId === "string" && workspaceId.trim().length > 0) {
|
|
@@ -1123,167 +1247,144 @@ function createAgentsApi(cfg, relatedApis) {
|
|
|
1123
1247
|
}
|
|
1124
1248
|
throw new WorkspaceNotSelectedError();
|
|
1125
1249
|
};
|
|
1126
|
-
const
|
|
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 = {}) => {
|
|
1144
|
-
requireWorkspace();
|
|
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) => {
|
|
1250
|
+
const postJson = async (url, payload) => {
|
|
1163
1251
|
requireWorkspace();
|
|
1164
|
-
const res = await doFetch(
|
|
1165
|
-
method: "GET"
|
|
1166
|
-
});
|
|
1167
|
-
return res.json();
|
|
1168
|
-
};
|
|
1169
|
-
const createAgent = async (payload) => {
|
|
1170
|
-
requireWorkspace();
|
|
1171
|
-
const res = await doFetch(`${base}/agents`, {
|
|
1252
|
+
const res = await doFetch(url, {
|
|
1172
1253
|
method: "POST",
|
|
1173
1254
|
body: JSON.stringify(payload),
|
|
1174
1255
|
headers: jsonHeaders
|
|
1175
1256
|
});
|
|
1176
1257
|
return res.json();
|
|
1177
1258
|
};
|
|
1178
|
-
const
|
|
1259
|
+
const postWithSse = async (url, payload, callbacks) => {
|
|
1179
1260
|
requireWorkspace();
|
|
1180
|
-
const
|
|
1261
|
+
const headers = buildHeaders({ "content-type": "application/json" });
|
|
1262
|
+
const fx = cfg.fetchImpl ?? fetch;
|
|
1263
|
+
const res = await fx(url, {
|
|
1181
1264
|
method: "POST",
|
|
1182
1265
|
body: JSON.stringify(payload),
|
|
1183
|
-
headers
|
|
1184
|
-
});
|
|
1185
|
-
return res.json();
|
|
1186
|
-
};
|
|
1187
|
-
const cloneAgent = async (agentId, payload) => {
|
|
1188
|
-
requireWorkspace();
|
|
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
|
|
1266
|
+
headers
|
|
1194
1267
|
});
|
|
1195
|
-
|
|
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
|
+
);
|
|
1196
1279
|
};
|
|
1197
|
-
const
|
|
1280
|
+
const fetchPage = async (options = {}) => {
|
|
1198
1281
|
requireWorkspace();
|
|
1199
|
-
const
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1282
|
+
const query = serializeListOptions({
|
|
1283
|
+
page: options.page,
|
|
1284
|
+
limit: options.limit,
|
|
1285
|
+
filter: options.filter
|
|
1203
1286
|
});
|
|
1287
|
+
const res = await doFetch(`${base}/agents`, { method: "GET", query });
|
|
1204
1288
|
return res.json();
|
|
1205
1289
|
};
|
|
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
|
-
};
|
|
1216
1290
|
const baseApi = {
|
|
1217
|
-
list
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
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
|
+
}
|
|
1224
1337
|
};
|
|
1225
1338
|
if (!relatedApis) {
|
|
1226
1339
|
return baseApi;
|
|
1227
1340
|
}
|
|
1341
|
+
return withEntityWrapping(baseApi, fetchPage, relatedApis);
|
|
1342
|
+
}
|
|
1343
|
+
function withEntityWrapping(api, fetchPage, deps) {
|
|
1228
1344
|
const wrapAgent = (detail) => createAgentEntity(detail, {
|
|
1229
|
-
|
|
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,
|
|
1345
|
+
...deps,
|
|
1237
1346
|
reload: async (agentId) => {
|
|
1238
|
-
const latest = await
|
|
1347
|
+
const latest = await api.get(agentId);
|
|
1239
1348
|
return wrapAgent(latest);
|
|
1240
1349
|
},
|
|
1241
1350
|
updateAgent: async (agentId, payload) => {
|
|
1242
|
-
const updated = await
|
|
1351
|
+
const updated = await api.update(agentId, payload);
|
|
1243
1352
|
return wrapAgent(updated);
|
|
1244
1353
|
},
|
|
1245
1354
|
deleteAgent: async (agentId) => {
|
|
1246
|
-
await
|
|
1355
|
+
await api.delete(agentId);
|
|
1247
1356
|
},
|
|
1248
1357
|
cloneAgent: async (agentId, payload) => {
|
|
1249
|
-
const cloned = await
|
|
1358
|
+
const cloned = await api.clone(agentId, payload);
|
|
1250
1359
|
return wrapAgent(cloned);
|
|
1251
1360
|
}
|
|
1252
1361
|
});
|
|
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
|
+
};
|
|
1253
1367
|
return {
|
|
1254
|
-
...
|
|
1368
|
+
...api,
|
|
1255
1369
|
async list(options = {}) {
|
|
1256
|
-
const
|
|
1257
|
-
const
|
|
1258
|
-
|
|
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);
|
|
1370
|
+
const opts = { ...options ?? {} };
|
|
1371
|
+
const initial = await wrapList(opts);
|
|
1372
|
+
return attachPaginator(initial, wrapList, opts);
|
|
1267
1373
|
},
|
|
1268
1374
|
async get(agentId) {
|
|
1269
|
-
|
|
1270
|
-
return wrapAgent(detail);
|
|
1375
|
+
return wrapAgent(await api.get(agentId));
|
|
1271
1376
|
},
|
|
1272
|
-
async create(payload) {
|
|
1273
|
-
|
|
1274
|
-
return wrapAgent(detail);
|
|
1377
|
+
async create(payload, callbacks) {
|
|
1378
|
+
return wrapAgent(await api.create(payload, callbacks));
|
|
1275
1379
|
},
|
|
1276
1380
|
async clone(agentId, payload) {
|
|
1277
|
-
|
|
1278
|
-
return wrapAgent(detail);
|
|
1381
|
+
return wrapAgent(await api.clone(agentId, payload));
|
|
1279
1382
|
},
|
|
1280
|
-
async forkFromTemplate(payload) {
|
|
1281
|
-
|
|
1282
|
-
return wrapAgent(detail);
|
|
1383
|
+
async forkFromTemplate(payload, callbacks) {
|
|
1384
|
+
return wrapAgent(await api.forkFromTemplate(payload, callbacks));
|
|
1283
1385
|
},
|
|
1284
1386
|
async update(agentId, payload) {
|
|
1285
|
-
|
|
1286
|
-
return wrapAgent(detail);
|
|
1387
|
+
return wrapAgent(await api.update(agentId, payload));
|
|
1287
1388
|
}
|
|
1288
1389
|
};
|
|
1289
1390
|
}
|
|
@@ -1547,6 +1648,42 @@ function createCatalogsApi(cfg) {
|
|
|
1547
1648
|
};
|
|
1548
1649
|
}
|
|
1549
1650
|
|
|
1651
|
+
// src/api/sip-trunks.ts
|
|
1652
|
+
function createSipTrunksApi(cfg) {
|
|
1653
|
+
const { base, doFetch } = createHttp(cfg);
|
|
1654
|
+
return {
|
|
1655
|
+
async list() {
|
|
1656
|
+
const res = await doFetch(`${base}/sip/trunks`, { method: "GET" });
|
|
1657
|
+
return res.json();
|
|
1658
|
+
},
|
|
1659
|
+
async get(trunkId) {
|
|
1660
|
+
const res = await doFetch(`${base}/sip/trunks/${trunkId}`, {
|
|
1661
|
+
method: "GET"
|
|
1662
|
+
});
|
|
1663
|
+
return res.json();
|
|
1664
|
+
},
|
|
1665
|
+
async create(body) {
|
|
1666
|
+
const res = await doFetch(`${base}/sip/trunks`, {
|
|
1667
|
+
method: "POST",
|
|
1668
|
+
headers: { "Content-Type": "application/json" },
|
|
1669
|
+
body: JSON.stringify(body)
|
|
1670
|
+
});
|
|
1671
|
+
return res.json();
|
|
1672
|
+
},
|
|
1673
|
+
async update(trunkId, body) {
|
|
1674
|
+
const res = await doFetch(`${base}/sip/trunks/${trunkId}`, {
|
|
1675
|
+
method: "PATCH",
|
|
1676
|
+
headers: { "Content-Type": "application/json" },
|
|
1677
|
+
body: JSON.stringify(body)
|
|
1678
|
+
});
|
|
1679
|
+
return res.json();
|
|
1680
|
+
},
|
|
1681
|
+
async delete(trunkId) {
|
|
1682
|
+
await doFetch(`${base}/sip/trunks/${trunkId}`, { method: "DELETE" });
|
|
1683
|
+
}
|
|
1684
|
+
};
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1550
1687
|
// src/api/tools.ts
|
|
1551
1688
|
var IDEMPOTENCY_HEADER = "Idempotency-Key";
|
|
1552
1689
|
var generateIdempotencyKey = (explicit) => {
|
|
@@ -2314,6 +2451,7 @@ function createClient(initialCfg) {
|
|
|
2314
2451
|
billing: createBillingApi(runtimeCfg),
|
|
2315
2452
|
calls: callsApi,
|
|
2316
2453
|
usage: createUsageApi(runtimeCfg),
|
|
2454
|
+
sip: createSipTrunksApi(runtimeCfg),
|
|
2317
2455
|
webhooks: webhooksApi
|
|
2318
2456
|
};
|
|
2319
2457
|
return {
|
|
@@ -2403,6 +2541,7 @@ export {
|
|
|
2403
2541
|
createCatalogsApi,
|
|
2404
2542
|
createClient,
|
|
2405
2543
|
createHttp,
|
|
2544
|
+
createSipTrunksApi,
|
|
2406
2545
|
createToolsApi,
|
|
2407
2546
|
createUsageApi,
|
|
2408
2547
|
createVoicesApi,
|