@mitralab.io/sdk-core 0.3.9 → 0.3.11
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/CHANGELOG.md +100 -0
- package/README.md +46 -16
- package/contracts/README.md +10 -0
- package/contracts/manifest.json +11 -1
- package/contracts/v0.3.10/sdk-parity.json +2902 -0
- package/contracts/v0.3.11/sdk-parity.json +2902 -0
- package/dist/index.cjs +463 -28
- package/dist/index.d.cts +450 -2
- package/dist/index.d.ts +450 -2
- package/dist/index.js +460 -28
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -22,6 +22,7 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
API_KEY_EXCHANGE_PATH: () => API_KEY_EXCHANGE_PATH,
|
|
24
24
|
AgentTaskTurnError: () => AgentTaskTurnError,
|
|
25
|
+
FileTransferError: () => FileTransferError,
|
|
25
26
|
SdkCoreConfigurationError: () => SdkCoreConfigurationError,
|
|
26
27
|
SdkCoreResponseError: () => SdkCoreResponseError,
|
|
27
28
|
appAccessTokenPath: () => appAccessTokenPath,
|
|
@@ -37,6 +38,7 @@ __export(index_exports, {
|
|
|
37
38
|
createCustomQueriesModule: () => createCustomQueriesModule,
|
|
38
39
|
createDataSourcesModule: () => createDataSourcesModule,
|
|
39
40
|
createEntitiesModule: () => createEntitiesModule,
|
|
41
|
+
createFilesModule: () => createFilesModule,
|
|
40
42
|
createFunctionsAdminModule: () => createFunctionsAdminModule,
|
|
41
43
|
createFunctionsModule: () => createFunctionsModule,
|
|
42
44
|
createImportsModule: () => createImportsModule,
|
|
@@ -61,6 +63,7 @@ __export(index_exports, {
|
|
|
61
63
|
expectObjectArray: () => expectObjectArray,
|
|
62
64
|
expectPage: () => expectPage,
|
|
63
65
|
expectStringArray: () => expectStringArray,
|
|
66
|
+
isAgentTurnEvent: () => isAgentTurnEvent,
|
|
64
67
|
isWorkspaceSession: () => isWorkspaceSession,
|
|
65
68
|
readTokenAppId: () => readTokenAppId,
|
|
66
69
|
readTokenExpiry: () => readTokenExpiry,
|
|
@@ -746,6 +749,55 @@ function expectImportExecution(value, context, errors = defaultSdkCoreErrorFacto
|
|
|
746
749
|
if (!isNullableString(execution.errorMessage)) invalidField(context, "errorMessage", errors);
|
|
747
750
|
return execution;
|
|
748
751
|
}
|
|
752
|
+
function expectFileMetadata(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
753
|
+
const file = expectObject(value, context, errors);
|
|
754
|
+
for (const field of ["id", "name", "contentType", "createdAt"]) {
|
|
755
|
+
if (typeof file[field] !== "string") invalidField(context, field, errors);
|
|
756
|
+
}
|
|
757
|
+
if (!isInteger(file.sizeBytes)) invalidField(context, "sizeBytes", errors);
|
|
758
|
+
if (!isOneOf(file.kind, ["TEMPORARY", "PERMANENT"])) {
|
|
759
|
+
invalidField(context, "kind", errors);
|
|
760
|
+
}
|
|
761
|
+
if (!isOneOf(file.visibility, ["PRIVATE", "RESTRICTED", "APP"])) {
|
|
762
|
+
invalidField(context, "visibility", errors);
|
|
763
|
+
}
|
|
764
|
+
if (!isOneOf(file.status, ["PENDING", "READY", "DELETED"])) {
|
|
765
|
+
invalidField(context, "status", errors);
|
|
766
|
+
}
|
|
767
|
+
if (!isOneOf(file.createdBy, ["USER", "FUNCTION"])) {
|
|
768
|
+
invalidField(context, "createdBy", errors);
|
|
769
|
+
}
|
|
770
|
+
for (const field of ["ownerUserId", "confirmedAt", "expiresAt"]) {
|
|
771
|
+
if (!isNullableString(file[field])) invalidField(context, field, errors);
|
|
772
|
+
}
|
|
773
|
+
return file;
|
|
774
|
+
}
|
|
775
|
+
function expectFileUpload(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
776
|
+
const upload = expectObject(value, context, errors);
|
|
777
|
+
for (const field of ["fileId", "uploadUrl", "uploadMethod", "expiresAt"]) {
|
|
778
|
+
if (typeof upload[field] !== "string" || !upload[field]) invalidField(context, field, errors);
|
|
779
|
+
}
|
|
780
|
+
if (!isStringRecord(upload.uploadHeaders)) invalidField(context, "uploadHeaders", errors);
|
|
781
|
+
return upload;
|
|
782
|
+
}
|
|
783
|
+
function expectFileReadUrl(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
784
|
+
const read = expectObject(value, context, errors);
|
|
785
|
+
if (typeof read.url !== "string" || !read.url) invalidField(context, "url", errors);
|
|
786
|
+
if (typeof read.expiresAt !== "string") invalidField(context, "expiresAt", errors);
|
|
787
|
+
return read;
|
|
788
|
+
}
|
|
789
|
+
function expectFileReadUrls(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
790
|
+
const response = expectObject(value, context, errors);
|
|
791
|
+
expectObjectArray(response.results, `${context} results`, errors).forEach(
|
|
792
|
+
(item, position) => {
|
|
793
|
+
const itemContext = `${context} result ${position}`;
|
|
794
|
+
if (typeof item.fileId !== "string") invalidField(itemContext, "fileId", errors);
|
|
795
|
+
if (typeof item.errorCode === "string") return;
|
|
796
|
+
expectFileReadUrl(item, itemContext, errors);
|
|
797
|
+
}
|
|
798
|
+
);
|
|
799
|
+
return response;
|
|
800
|
+
}
|
|
749
801
|
function expectMessageAccepted(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
750
802
|
const response = expectObject(value, context, errors);
|
|
751
803
|
if (typeof response.messageId !== "string") invalidField(context, "messageId", errors);
|
|
@@ -1004,7 +1056,40 @@ function expectAgentTask(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
|
1004
1056
|
if (typeof task.archived !== "boolean") invalidField(context, "archived", errors);
|
|
1005
1057
|
if (!isNullableString(task.createdAt)) invalidField(context, "createdAt", errors);
|
|
1006
1058
|
if (typeof task.updatedAt !== "string") invalidField(context, "updatedAt", errors);
|
|
1007
|
-
|
|
1059
|
+
const { turn: rawTurn, instructions: rawInstructions, folderKey: rawFolderKey, ...rest } = task;
|
|
1060
|
+
const turn = rawTurn === null ? null : agentTaskTurnOf(rawTurn);
|
|
1061
|
+
const instructions = typeof rawInstructions === "string" ? rawInstructions : void 0;
|
|
1062
|
+
const folderKey = typeof rawFolderKey === "string" ? rawFolderKey : void 0;
|
|
1063
|
+
return {
|
|
1064
|
+
...rest,
|
|
1065
|
+
...turn !== void 0 ? { turn } : {},
|
|
1066
|
+
...instructions !== void 0 ? { instructions } : {},
|
|
1067
|
+
...folderKey !== void 0 ? { folderKey } : {}
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
function agentTaskTurnOf(value) {
|
|
1071
|
+
if (!isObject(value)) return void 0;
|
|
1072
|
+
if (typeof value.state !== "string") return void 0;
|
|
1073
|
+
if (!isNullableString(value.currentTurnId) || !isCount(value.queuedCount)) return void 0;
|
|
1074
|
+
if (value.lastTurn === null) return value;
|
|
1075
|
+
const lastTurn = value.lastTurn;
|
|
1076
|
+
if (!isObject(lastTurn)) return void 0;
|
|
1077
|
+
if (typeof lastTurn.turnId !== "string" || typeof lastTurn.status !== "string") return void 0;
|
|
1078
|
+
if (!isStringArray(lastTurn.messageIds)) return void 0;
|
|
1079
|
+
const nullableTexts = ["finishReason", "errorCode", "startedAt", "endedAt"];
|
|
1080
|
+
if (!nullableTexts.every((field) => isNullableString(lastTurn[field]))) return void 0;
|
|
1081
|
+
return value;
|
|
1082
|
+
}
|
|
1083
|
+
function expectAgentTaskTurnStates(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
1084
|
+
return expectObjectArray(value, context, errors).map((state, position) => {
|
|
1085
|
+
if (typeof state.taskId !== "string") {
|
|
1086
|
+
invalidField(`${context} item ${position}`, "taskId", errors);
|
|
1087
|
+
}
|
|
1088
|
+
return {
|
|
1089
|
+
...state,
|
|
1090
|
+
turn: agentTaskTurnOf(state.turn) ?? null
|
|
1091
|
+
};
|
|
1092
|
+
});
|
|
1008
1093
|
}
|
|
1009
1094
|
function expectAgentTaskChannel(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
1010
1095
|
if (value === void 0 || value === null || value === "") return null;
|
|
@@ -1078,6 +1163,22 @@ function agentTurnUsageOf(value) {
|
|
|
1078
1163
|
}
|
|
1079
1164
|
return usage;
|
|
1080
1165
|
}
|
|
1166
|
+
function agentTurnCompactionsOf(value) {
|
|
1167
|
+
if (!Array.isArray(value)) return [];
|
|
1168
|
+
return value.flatMap((item) => {
|
|
1169
|
+
const compaction = agentTurnCompactionOf(item);
|
|
1170
|
+
return compaction ? [compaction] : [];
|
|
1171
|
+
});
|
|
1172
|
+
}
|
|
1173
|
+
function agentTurnCompactionOf(value) {
|
|
1174
|
+
if (!isObject(value) || typeof value.trigger !== "string") return void 0;
|
|
1175
|
+
return {
|
|
1176
|
+
trigger: value.trigger,
|
|
1177
|
+
tokensBefore: isCount(value.tokensBefore) ? value.tokensBefore : null,
|
|
1178
|
+
tokensAfter: isCount(value.tokensAfter) ? value.tokensAfter : null,
|
|
1179
|
+
at: typeof value.at === "string" ? value.at : null
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1081
1182
|
function agentTurnUsageRequestOf(value) {
|
|
1082
1183
|
if (!isObject(value)) return void 0;
|
|
1083
1184
|
const request = {};
|
|
@@ -1716,6 +1817,8 @@ function createAgentsModule(functionsTransport, copilotTransport, errors = defau
|
|
|
1716
1817
|
}
|
|
1717
1818
|
|
|
1718
1819
|
// src/modules/agentTasks.ts
|
|
1820
|
+
var TURN_STATES_BATCH_SIZE = 100;
|
|
1821
|
+
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
1719
1822
|
function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory) {
|
|
1720
1823
|
const path = (id) => `/api/v1/tasks/${encodePathSegment(id, "task id", errors)}`;
|
|
1721
1824
|
return {
|
|
@@ -1729,12 +1832,18 @@ function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory)
|
|
|
1729
1832
|
search: options.search,
|
|
1730
1833
|
userId: options.userId
|
|
1731
1834
|
};
|
|
1732
|
-
|
|
1835
|
+
const context = "Agent task page response";
|
|
1836
|
+
const page = expectPage(
|
|
1733
1837
|
await transport.request("/api/v1/tasks", { method: "GET", params }),
|
|
1734
|
-
|
|
1735
|
-
errors
|
|
1736
|
-
expectAgentTask
|
|
1838
|
+
context,
|
|
1839
|
+
errors
|
|
1737
1840
|
);
|
|
1841
|
+
return {
|
|
1842
|
+
...page,
|
|
1843
|
+
content: page.content.map(
|
|
1844
|
+
(task, position) => expectAgentTask(task, `${context} item ${position}`, errors)
|
|
1845
|
+
)
|
|
1846
|
+
};
|
|
1738
1847
|
},
|
|
1739
1848
|
async get(id) {
|
|
1740
1849
|
return expectAgentTask(
|
|
@@ -1757,6 +1866,16 @@ function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory)
|
|
|
1757
1866
|
errors
|
|
1758
1867
|
);
|
|
1759
1868
|
},
|
|
1869
|
+
async setInstructions(id, instructions) {
|
|
1870
|
+
return expectAgentTask(
|
|
1871
|
+
await transport.request(`${path(id)}/instructions`, {
|
|
1872
|
+
method: "PUT",
|
|
1873
|
+
body: { instructions }
|
|
1874
|
+
}),
|
|
1875
|
+
"Agent task instructions response",
|
|
1876
|
+
errors
|
|
1877
|
+
);
|
|
1878
|
+
},
|
|
1760
1879
|
async archive(id) {
|
|
1761
1880
|
expectEmpty(
|
|
1762
1881
|
await transport.request(`${path(id)}/archive`, { method: "PATCH" }),
|
|
@@ -1801,6 +1920,26 @@ function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory)
|
|
|
1801
1920
|
"Agent task channel response",
|
|
1802
1921
|
errors
|
|
1803
1922
|
);
|
|
1923
|
+
},
|
|
1924
|
+
async turnStates(ids) {
|
|
1925
|
+
const unique = [...new Set(ids.map((id) => id.toLowerCase()))].filter((id) => UUID.test(id));
|
|
1926
|
+
const states = [];
|
|
1927
|
+
for (let start = 0; start < unique.length; start += TURN_STATES_BATCH_SIZE) {
|
|
1928
|
+
const taskIds = unique.slice(start, start + TURN_STATES_BATCH_SIZE);
|
|
1929
|
+
states.push(
|
|
1930
|
+
...expectAgentTaskTurnStates(
|
|
1931
|
+
await transport.request("/api/v1/tasks/turn-states", {
|
|
1932
|
+
method: "GET",
|
|
1933
|
+
// Joined here because a transport may send an array as repeated keys; the Copilot
|
|
1934
|
+
// reads one comma-separated value.
|
|
1935
|
+
params: { taskIds: taskIds.join(",") }
|
|
1936
|
+
}),
|
|
1937
|
+
"Agent task turn states response",
|
|
1938
|
+
errors
|
|
1939
|
+
)
|
|
1940
|
+
);
|
|
1941
|
+
}
|
|
1942
|
+
return states;
|
|
1804
1943
|
}
|
|
1805
1944
|
};
|
|
1806
1945
|
}
|
|
@@ -2371,6 +2510,248 @@ function createEntitiesModule(transport, errors = defaultSdkCoreErrorFactory) {
|
|
|
2371
2510
|
});
|
|
2372
2511
|
}
|
|
2373
2512
|
|
|
2513
|
+
// src/fileTransferError.ts
|
|
2514
|
+
var FileTransferError = class extends Error {
|
|
2515
|
+
fileId;
|
|
2516
|
+
step;
|
|
2517
|
+
/**
|
|
2518
|
+
* HTTP status storage answered in `send` and `download`; null when storage gave no status or the
|
|
2519
|
+
* content failed to arrive, and always in `confirm`.
|
|
2520
|
+
*/
|
|
2521
|
+
status;
|
|
2522
|
+
/**
|
|
2523
|
+
* True when the `PENDING` file a failed `send` left behind was deleted. False when that delete
|
|
2524
|
+
* failed too, and always in `confirm` and `download`, where nothing is deleted.
|
|
2525
|
+
*/
|
|
2526
|
+
cleanedUp;
|
|
2527
|
+
constructor(message, details) {
|
|
2528
|
+
super(message, details.cause === void 0 ? void 0 : { cause: details.cause });
|
|
2529
|
+
this.name = "FileTransferError";
|
|
2530
|
+
this.fileId = details.fileId;
|
|
2531
|
+
this.step = details.step;
|
|
2532
|
+
this.status = details.status ?? null;
|
|
2533
|
+
this.cleanedUp = details.cleanedUp ?? false;
|
|
2534
|
+
}
|
|
2535
|
+
};
|
|
2536
|
+
|
|
2537
|
+
// src/modules/files.ts
|
|
2538
|
+
var MAX_READ_URLS = 100;
|
|
2539
|
+
var UNTYPED_CONTENT = "application/octet-stream";
|
|
2540
|
+
var REDACTED = "<redacted>";
|
|
2541
|
+
var MAX_CAUSE_DEPTH = 5;
|
|
2542
|
+
var MIN_SECRET_LENGTH = 16;
|
|
2543
|
+
var UNREADABLE_STORAGE_ERROR = "storage failed with an error the Core cannot show";
|
|
2544
|
+
function createFilesModule(transport, options = {}, errors = defaultSdkCoreErrorFactory) {
|
|
2545
|
+
const path = (fileId) => `/api/v1/files/${encodePathSegment(fileId, "file id", errors)}`;
|
|
2546
|
+
const storageFetch = () => {
|
|
2547
|
+
if (options.fetch) return options.fetch;
|
|
2548
|
+
const global = globalThis.fetch;
|
|
2549
|
+
if (typeof global !== "function") {
|
|
2550
|
+
configurationError("fetch is not available: pass files.fetch to the Core", errors);
|
|
2551
|
+
}
|
|
2552
|
+
return global;
|
|
2553
|
+
};
|
|
2554
|
+
const requestUpload = async (input) => expectFileUpload(
|
|
2555
|
+
await transport.request("/api/v1/files", { method: "POST", body: input }),
|
|
2556
|
+
"File upload response",
|
|
2557
|
+
errors
|
|
2558
|
+
);
|
|
2559
|
+
const confirmUpload = async (fileId) => expectFileMetadata(
|
|
2560
|
+
await transport.request(`${path(fileId)}/confirm`, { method: "POST" }),
|
|
2561
|
+
"Confirm file upload response",
|
|
2562
|
+
errors
|
|
2563
|
+
);
|
|
2564
|
+
const getUrl = async (fileId, { expiresInSeconds } = {}) => expectFileReadUrl(
|
|
2565
|
+
await transport.request(`${path(fileId)}/read-url`, {
|
|
2566
|
+
method: "POST",
|
|
2567
|
+
...expiresInSeconds === void 0 ? {} : { body: { expiresInSeconds } }
|
|
2568
|
+
}),
|
|
2569
|
+
"File read URL response",
|
|
2570
|
+
errors
|
|
2571
|
+
);
|
|
2572
|
+
const deleteFile = async (fileId) => {
|
|
2573
|
+
expectEmpty(
|
|
2574
|
+
await transport.request(path(fileId), { method: "DELETE" }),
|
|
2575
|
+
"Delete file response",
|
|
2576
|
+
errors
|
|
2577
|
+
);
|
|
2578
|
+
};
|
|
2579
|
+
const sendContent = async (fetch, upload, content, signal) => {
|
|
2580
|
+
const failure = await putContent(fetch, upload, content, signal);
|
|
2581
|
+
if (failure === null) return;
|
|
2582
|
+
const cleanedUp = await deleteFile(upload.fileId).then(
|
|
2583
|
+
() => true,
|
|
2584
|
+
() => false
|
|
2585
|
+
);
|
|
2586
|
+
if (failure.aborted && signal !== void 0) throw abortReason(signal);
|
|
2587
|
+
throw new FileTransferError(`File ${upload.fileId} was not sent: ${failure.reason}`, {
|
|
2588
|
+
fileId: upload.fileId,
|
|
2589
|
+
step: "send",
|
|
2590
|
+
status: failure.status,
|
|
2591
|
+
cause: "error" in failure ? storageCause(failure.error, upload.uploadUrl) : void 0,
|
|
2592
|
+
cleanedUp
|
|
2593
|
+
});
|
|
2594
|
+
};
|
|
2595
|
+
const confirmSent = async (fileId) => {
|
|
2596
|
+
try {
|
|
2597
|
+
return await confirmUpload(fileId);
|
|
2598
|
+
} catch (error) {
|
|
2599
|
+
throw new FileTransferError(
|
|
2600
|
+
`File ${fileId} was sent but not confirmed: ${messageOf(error)}`,
|
|
2601
|
+
{
|
|
2602
|
+
fileId,
|
|
2603
|
+
step: "confirm",
|
|
2604
|
+
cause: error
|
|
2605
|
+
}
|
|
2606
|
+
);
|
|
2607
|
+
}
|
|
2608
|
+
};
|
|
2609
|
+
return {
|
|
2610
|
+
requestUpload,
|
|
2611
|
+
confirmUpload,
|
|
2612
|
+
async upload(content, uploadOptions) {
|
|
2613
|
+
const fetch = storageFetch();
|
|
2614
|
+
const input = uploadInput(content, uploadOptions, errors);
|
|
2615
|
+
const { signal } = uploadOptions;
|
|
2616
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
2617
|
+
const upload = await requestUpload(input);
|
|
2618
|
+
await sendContent(fetch, upload, content, signal);
|
|
2619
|
+
return confirmSent(upload.fileId);
|
|
2620
|
+
},
|
|
2621
|
+
getUrl,
|
|
2622
|
+
async getUrls(fileIds, { expiresInSeconds } = {}) {
|
|
2623
|
+
requireBatchSize(fileIds, "fileIds", MAX_READ_URLS, errors);
|
|
2624
|
+
return expectFileReadUrls(
|
|
2625
|
+
await transport.request("/api/v1/files/read-urls", {
|
|
2626
|
+
method: "POST",
|
|
2627
|
+
body: { fileIds, ...expiresInSeconds === void 0 ? {} : { expiresInSeconds } }
|
|
2628
|
+
}),
|
|
2629
|
+
"File read URLs response",
|
|
2630
|
+
errors
|
|
2631
|
+
);
|
|
2632
|
+
},
|
|
2633
|
+
async download(fileId, { signal } = {}) {
|
|
2634
|
+
const fetch = storageFetch();
|
|
2635
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
2636
|
+
const { url } = await getUrl(fileId);
|
|
2637
|
+
return fetchContent(fetch, fileId, url, signal);
|
|
2638
|
+
},
|
|
2639
|
+
async get(fileId) {
|
|
2640
|
+
return expectFileMetadata(
|
|
2641
|
+
await transport.request(path(fileId), { method: "GET" }),
|
|
2642
|
+
"File response",
|
|
2643
|
+
errors
|
|
2644
|
+
);
|
|
2645
|
+
},
|
|
2646
|
+
delete: deleteFile
|
|
2647
|
+
};
|
|
2648
|
+
}
|
|
2649
|
+
function uploadInput(content, options, errors) {
|
|
2650
|
+
const name = options.name ?? fileNameOf(content);
|
|
2651
|
+
if (!name) configurationError("name is required when the content is not a named File", errors);
|
|
2652
|
+
const contentType = options.contentType || content.type || UNTYPED_CONTENT;
|
|
2653
|
+
return {
|
|
2654
|
+
name,
|
|
2655
|
+
contentType,
|
|
2656
|
+
sizeBytes: content.size,
|
|
2657
|
+
kind: options.kind,
|
|
2658
|
+
visibility: options.visibility,
|
|
2659
|
+
...options.appOwned === void 0 ? {} : { appOwned: options.appOwned }
|
|
2660
|
+
};
|
|
2661
|
+
}
|
|
2662
|
+
function fileNameOf(content) {
|
|
2663
|
+
const name = content.name;
|
|
2664
|
+
return typeof name === "string" ? name : void 0;
|
|
2665
|
+
}
|
|
2666
|
+
async function putContent(fetch, upload, content, signal) {
|
|
2667
|
+
try {
|
|
2668
|
+
const response = await fetch(upload.uploadUrl, {
|
|
2669
|
+
method: upload.uploadMethod,
|
|
2670
|
+
headers: upload.uploadHeaders,
|
|
2671
|
+
body: content,
|
|
2672
|
+
redirect: "error",
|
|
2673
|
+
...signal === void 0 ? {} : { signal }
|
|
2674
|
+
});
|
|
2675
|
+
discardBody(response);
|
|
2676
|
+
if (response.ok) return null;
|
|
2677
|
+
return {
|
|
2678
|
+
reason: `storage answered ${response.status}`,
|
|
2679
|
+
status: response.status,
|
|
2680
|
+
aborted: false
|
|
2681
|
+
};
|
|
2682
|
+
} catch (error) {
|
|
2683
|
+
return {
|
|
2684
|
+
reason: "the transfer to storage failed",
|
|
2685
|
+
status: null,
|
|
2686
|
+
aborted: signal?.aborted === true,
|
|
2687
|
+
error
|
|
2688
|
+
};
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
async function fetchContent(fetch, fileId, url, signal) {
|
|
2692
|
+
let response;
|
|
2693
|
+
try {
|
|
2694
|
+
response = await fetch(url, {
|
|
2695
|
+
method: "GET",
|
|
2696
|
+
redirect: "error",
|
|
2697
|
+
...signal === void 0 ? {} : { signal }
|
|
2698
|
+
});
|
|
2699
|
+
if (response.ok) return await response.blob();
|
|
2700
|
+
} catch (error) {
|
|
2701
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
2702
|
+
throw new FileTransferError(
|
|
2703
|
+
`File ${fileId} was not downloaded: the transfer from storage failed`,
|
|
2704
|
+
{ fileId, step: "download", cause: storageCause(error, url) }
|
|
2705
|
+
);
|
|
2706
|
+
}
|
|
2707
|
+
discardBody(response);
|
|
2708
|
+
throw new FileTransferError(
|
|
2709
|
+
`File ${fileId} was not downloaded: storage answered ${response.status}`,
|
|
2710
|
+
{ fileId, step: "download", status: response.status }
|
|
2711
|
+
);
|
|
2712
|
+
}
|
|
2713
|
+
function storageCause(thrown, link) {
|
|
2714
|
+
const secrets = linkSecrets(link);
|
|
2715
|
+
const redact = (text) => secrets.reduce((result, secret) => result.replaceAll(secret, REDACTED), text);
|
|
2716
|
+
const rebuild = (value, depth) => {
|
|
2717
|
+
if (!(value instanceof Error)) {
|
|
2718
|
+
const opaque = typeof value === "function" || typeof value === "object" && value !== null;
|
|
2719
|
+
return new Error(opaque ? UNREADABLE_STORAGE_ERROR : redact(String(value)));
|
|
2720
|
+
}
|
|
2721
|
+
const below = depth < MAX_CAUSE_DEPTH && value.cause != null ? rebuild(value.cause, depth + 1) : void 0;
|
|
2722
|
+
const error = new Error(
|
|
2723
|
+
redact(String(value.message)),
|
|
2724
|
+
below === void 0 ? void 0 : { cause: below }
|
|
2725
|
+
);
|
|
2726
|
+
error.name = redact(String(value.name));
|
|
2727
|
+
if (typeof value.stack === "string") error.stack = redact(value.stack);
|
|
2728
|
+
const { code } = value;
|
|
2729
|
+
return typeof code === "string" ? Object.assign(error, { code: redact(code) }) : error;
|
|
2730
|
+
};
|
|
2731
|
+
try {
|
|
2732
|
+
return rebuild(thrown, 1);
|
|
2733
|
+
} catch {
|
|
2734
|
+
return new Error(UNREADABLE_STORAGE_ERROR);
|
|
2735
|
+
}
|
|
2736
|
+
}
|
|
2737
|
+
function linkSecrets(link) {
|
|
2738
|
+
const start = link.indexOf("?");
|
|
2739
|
+
const query = start < 0 ? "" : link.slice(start + 1);
|
|
2740
|
+
const raw = query.split("&").map((pair) => pair.slice(pair.indexOf("=") + 1));
|
|
2741
|
+
const decoded = [...new URLSearchParams(query).values()];
|
|
2742
|
+
const values = [...raw, ...decoded].filter((value) => value.length >= MIN_SECRET_LENGTH);
|
|
2743
|
+
return [link, query, ...values].filter((secret) => secret.length > 0);
|
|
2744
|
+
}
|
|
2745
|
+
function discardBody(response) {
|
|
2746
|
+
void Promise.resolve().then(() => response.body?.cancel()).catch(() => void 0);
|
|
2747
|
+
}
|
|
2748
|
+
function abortReason(signal) {
|
|
2749
|
+
return signal.reason ?? new DOMException("This operation was aborted", "AbortError");
|
|
2750
|
+
}
|
|
2751
|
+
function messageOf(error) {
|
|
2752
|
+
return error instanceof Error ? error.message : String(error);
|
|
2753
|
+
}
|
|
2754
|
+
|
|
2374
2755
|
// src/modules/functions.ts
|
|
2375
2756
|
var DefaultFunctionsModule = class {
|
|
2376
2757
|
constructor(transport, errors, options) {
|
|
@@ -3380,6 +3761,7 @@ function createSdkCore(options) {
|
|
|
3380
3761
|
customQueries: createCustomQueriesModule(options.transports.dataManager, errors),
|
|
3381
3762
|
dataSources: createDataSourcesModule(options.transports.dataManager, errors),
|
|
3382
3763
|
entities: createEntitiesModule(options.transports.dataManager, errors),
|
|
3764
|
+
files: createFilesModule(options.transports.dataManager, options.files, errors),
|
|
3383
3765
|
functions: createFunctionsModule(options.transports.functions, options.functions, errors),
|
|
3384
3766
|
functionsAdmin,
|
|
3385
3767
|
imports: createImportsModule(options.transports.dataManager, errors),
|
|
@@ -3407,10 +3789,13 @@ function unavailableTransport(name, errors) {
|
|
|
3407
3789
|
// src/agentTurnError.ts
|
|
3408
3790
|
var AgentTaskTurnError = class extends Error {
|
|
3409
3791
|
code;
|
|
3410
|
-
|
|
3792
|
+
/** The compactions of a turn the box closed with an error; empty when it reported none. */
|
|
3793
|
+
compactions;
|
|
3794
|
+
constructor(message, code, compactions = []) {
|
|
3411
3795
|
super(message);
|
|
3412
3796
|
this.name = "AgentTaskTurnError";
|
|
3413
3797
|
this.code = code;
|
|
3798
|
+
this.compactions = compactions;
|
|
3414
3799
|
}
|
|
3415
3800
|
};
|
|
3416
3801
|
|
|
@@ -4562,6 +4947,7 @@ var CoreAgentTaskSession = class {
|
|
|
4562
4947
|
recoveryGeneration = 0;
|
|
4563
4948
|
recoveredTerminalReason;
|
|
4564
4949
|
recoveredTerminalUsage;
|
|
4950
|
+
recoveredTerminalCompactions;
|
|
4565
4951
|
// Polls that found no closing row although a terminal with usage was heard.
|
|
4566
4952
|
endedPollsWithoutRow = 0;
|
|
4567
4953
|
// The turn ids the running turn went by, and the highest box sequence seen in it. The Copilot
|
|
@@ -4678,7 +5064,7 @@ var CoreAgentTaskSession = class {
|
|
|
4678
5064
|
const error = new AgentTaskTurnError(
|
|
4679
5065
|
"Agent turn cancellation was not acknowledged before the safety timeout."
|
|
4680
5066
|
);
|
|
4681
|
-
this.emit("error", { error: error.message });
|
|
5067
|
+
this.emit("error", { error: error.message, compactions: [] });
|
|
4682
5068
|
this.failTurn(error);
|
|
4683
5069
|
}
|
|
4684
5070
|
}, CANCEL_SAFETY_MS);
|
|
@@ -4985,6 +5371,7 @@ var CoreAgentTaskSession = class {
|
|
|
4985
5371
|
this.recoveryUsed = false;
|
|
4986
5372
|
this.recoveredTerminalReason = void 0;
|
|
4987
5373
|
this.recoveredTerminalUsage = void 0;
|
|
5374
|
+
this.recoveredTerminalCompactions = void 0;
|
|
4988
5375
|
this.recoveryGeneration += 1;
|
|
4989
5376
|
this.activeWaiter = waiter;
|
|
4990
5377
|
this.setStatus("streaming");
|
|
@@ -5162,6 +5549,7 @@ var CoreAgentTaskSession = class {
|
|
|
5162
5549
|
this.recoveryUsed = false;
|
|
5163
5550
|
this.recoveredTerminalReason = void 0;
|
|
5164
5551
|
this.recoveredTerminalUsage = void 0;
|
|
5552
|
+
this.recoveredTerminalCompactions = void 0;
|
|
5165
5553
|
this.recoveryGeneration += 1;
|
|
5166
5554
|
this.turnBaselineIds = new Set(this._history.map((item) => item.id));
|
|
5167
5555
|
void this.captureTurnBaseline().catch(() => void 0);
|
|
@@ -5296,19 +5684,28 @@ var CoreAgentTaskSession = class {
|
|
|
5296
5684
|
);
|
|
5297
5685
|
if (!newest) return false;
|
|
5298
5686
|
this._content = newest.content;
|
|
5299
|
-
this.finishTurn(reason, this.recoveredTerminalUsage);
|
|
5687
|
+
this.finishTurn(reason, this.recoveredTerminalUsage, this.recoveredTerminalEnd());
|
|
5300
5688
|
return true;
|
|
5301
5689
|
}
|
|
5302
5690
|
if (recovered.type === "ERROR") {
|
|
5303
5691
|
const error = new AgentTaskTurnError(recovered.content);
|
|
5304
|
-
this.emit("error", { error: error.message });
|
|
5692
|
+
this.emit("error", { error: error.message, compactions: [] });
|
|
5305
5693
|
this.failTurn(error);
|
|
5306
5694
|
return true;
|
|
5307
5695
|
}
|
|
5308
5696
|
this._content = recovered.content;
|
|
5309
|
-
this.finishTurn(
|
|
5697
|
+
this.finishTurn(
|
|
5698
|
+
reason,
|
|
5699
|
+
recovered.usage ?? this.recoveredTerminalUsage,
|
|
5700
|
+
this.recoveredTerminalEnd()
|
|
5701
|
+
);
|
|
5310
5702
|
return true;
|
|
5311
5703
|
}
|
|
5704
|
+
// The history rows carry no compactions: only a terminal heard live has the turn's list.
|
|
5705
|
+
recoveredTerminalEnd() {
|
|
5706
|
+
const compactions = this.recoveredTerminalCompactions;
|
|
5707
|
+
return compactions ? { compactions } : {};
|
|
5708
|
+
}
|
|
5312
5709
|
closesThisTurn(message) {
|
|
5313
5710
|
if (!message.usage) return false;
|
|
5314
5711
|
const prompt = message.usage.requestMessageId;
|
|
@@ -5469,9 +5866,11 @@ var CoreAgentTaskSession = class {
|
|
|
5469
5866
|
const finished = typeof lifecycle2?.turnId === "string" ? lifecycle2.turnId : void 0;
|
|
5470
5867
|
if (finished !== void 0 && this.finishedTurnIds.includes(finished)) break;
|
|
5471
5868
|
if (finished === void 0 && lifecycle2 && this.turnIds.size > 0) break;
|
|
5869
|
+
const compactions = agentTurnCompactionsOf(payload?.compactions);
|
|
5472
5870
|
const end = {
|
|
5473
5871
|
...sequenceOf(event),
|
|
5474
|
-
...finished === void 0 ? {} : { turnId: finished }
|
|
5872
|
+
...finished === void 0 ? {} : { turnId: finished },
|
|
5873
|
+
compactions
|
|
5475
5874
|
};
|
|
5476
5875
|
if (reason === "interrupted" || lifecycle2?.interruptTerminal === true) {
|
|
5477
5876
|
this.rememberFinishedTurn(lifecycle2);
|
|
@@ -5483,6 +5882,7 @@ var CoreAgentTaskSession = class {
|
|
|
5483
5882
|
if (this.recoveryUsed) {
|
|
5484
5883
|
this.recoveredTerminalReason = reason;
|
|
5485
5884
|
this.recoveredTerminalUsage = usage;
|
|
5885
|
+
this.recoveredTerminalCompactions = compactions;
|
|
5486
5886
|
if (!this.recoveryPromise) {
|
|
5487
5887
|
const generation = this.recoveryGeneration;
|
|
5488
5888
|
this.recoveryPromise = this.recoverTurn(generation).finally(() => {
|
|
@@ -5498,24 +5898,20 @@ var CoreAgentTaskSession = class {
|
|
|
5498
5898
|
case "error": {
|
|
5499
5899
|
const code = typeof payload?.code === "string" ? payload.code : void 0;
|
|
5500
5900
|
const message = typeof payload?.message === "string" ? payload.message : "Agent returned an error.";
|
|
5501
|
-
|
|
5502
|
-
|
|
5503
|
-
|
|
5504
|
-
|
|
5505
|
-
|
|
5506
|
-
|
|
5507
|
-
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
if (steered) steered.refusal = new AgentTaskTurnError(message, code);
|
|
5511
|
-
break;
|
|
5512
|
-
}
|
|
5513
|
-
} else if (!payload?.lifecycle && !this.dispatching && this.pendingSteers > 0) break;
|
|
5514
|
-
if (this._status === "streaming" || this._status === "cancelled") {
|
|
5515
|
-
this.failTurn(new AgentTaskTurnError(message, code));
|
|
5516
|
-
}
|
|
5901
|
+
const compactions = agentTurnCompactionsOf(payload?.compactions);
|
|
5902
|
+
const endsTurn = this.boxErrorEndsTurn(payload, message, code);
|
|
5903
|
+
this.emit("error", {
|
|
5904
|
+
...code ? { code } : {},
|
|
5905
|
+
error: message,
|
|
5906
|
+
...sequenceOf(event),
|
|
5907
|
+
...endsTurn ? { compactions } : {}
|
|
5908
|
+
});
|
|
5909
|
+
if (endsTurn) this.failTurn(new AgentTaskTurnError(message, code, compactions));
|
|
5517
5910
|
break;
|
|
5518
5911
|
}
|
|
5912
|
+
case "contextCompacted":
|
|
5913
|
+
if (!this.isFinishedTurnFrame(lifecycle)) this.emitCompaction(event, payload);
|
|
5914
|
+
break;
|
|
5519
5915
|
case "approval":
|
|
5520
5916
|
case "user_input_request":
|
|
5521
5917
|
this.liveRequest(event, payload);
|
|
@@ -5659,6 +6055,22 @@ var CoreAgentTaskSession = class {
|
|
|
5659
6055
|
this.turnIds.add(turnId);
|
|
5660
6056
|
return false;
|
|
5661
6057
|
}
|
|
6058
|
+
// A refused steer is handed its refusal here, for the send that waits on it.
|
|
6059
|
+
boxErrorEndsTurn(payload, message, code) {
|
|
6060
|
+
if (!payload?.lifecycle && payload?.requestId !== void 0) return false;
|
|
6061
|
+
if (typeof payload?.command === "string") return false;
|
|
6062
|
+
const refused = payload?.lifecycle ? void 0 : payload?.clientMessageId;
|
|
6063
|
+
if (typeof refused === "string") {
|
|
6064
|
+
if (refused !== this.openingMessageId) {
|
|
6065
|
+
const steered = [...this.steers, ...this.strayedSteers].find(
|
|
6066
|
+
(pending) => pending.clientMessageId === refused
|
|
6067
|
+
);
|
|
6068
|
+
if (steered) steered.refusal = new AgentTaskTurnError(message, code);
|
|
6069
|
+
return false;
|
|
6070
|
+
}
|
|
6071
|
+
} else if (!payload?.lifecycle && !this.dispatching && this.pendingSteers > 0) return false;
|
|
6072
|
+
return this._status === "streaming" || this._status === "cancelled";
|
|
6073
|
+
}
|
|
5662
6074
|
isFinishedTurnFrame(lifecycle) {
|
|
5663
6075
|
const turnId = lifecycle?.turnId;
|
|
5664
6076
|
return typeof turnId === "string" && this.finishedTurnIds.includes(turnId);
|
|
@@ -5886,10 +6298,22 @@ var CoreAgentTaskSession = class {
|
|
|
5886
6298
|
case "user_input_request":
|
|
5887
6299
|
this.liveRequest(event, payload, parentToolId);
|
|
5888
6300
|
break;
|
|
6301
|
+
case "contextCompacted":
|
|
6302
|
+
this.emitCompaction(event, payload, parentToolId);
|
|
6303
|
+
break;
|
|
5889
6304
|
default:
|
|
5890
6305
|
break;
|
|
5891
6306
|
}
|
|
5892
6307
|
}
|
|
6308
|
+
emitCompaction(event, payload, parentToolId) {
|
|
6309
|
+
const compaction = agentTurnCompactionOf(payload);
|
|
6310
|
+
if (!compaction) return;
|
|
6311
|
+
this.emit("contextCompacted", {
|
|
6312
|
+
...compaction,
|
|
6313
|
+
...pieceIdentityOf(event, payload),
|
|
6314
|
+
...parentToolIdOf(parentToolId)
|
|
6315
|
+
});
|
|
6316
|
+
}
|
|
5893
6317
|
applySubagent(event, payload) {
|
|
5894
6318
|
const subagentId = payload?.subagentId;
|
|
5895
6319
|
const toolId = payload?.toolId;
|
|
@@ -5953,7 +6377,8 @@ var CoreAgentTaskSession = class {
|
|
|
5953
6377
|
content: this._content,
|
|
5954
6378
|
reason,
|
|
5955
6379
|
...usage ? { usage } : {},
|
|
5956
|
-
...end
|
|
6380
|
+
...end,
|
|
6381
|
+
compactions: end.compactions ?? []
|
|
5957
6382
|
};
|
|
5958
6383
|
this.emit("turnEnd", result);
|
|
5959
6384
|
this.activeWaiter?.resolve(result);
|
|
@@ -6073,6 +6498,13 @@ var CoreAgentTaskSession = class {
|
|
|
6073
6498
|
}
|
|
6074
6499
|
};
|
|
6075
6500
|
|
|
6501
|
+
// src/agentTurnEvent.ts
|
|
6502
|
+
function isAgentTurnEvent(value) {
|
|
6503
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
6504
|
+
const event = value;
|
|
6505
|
+
return (event.type === "STARTED" || event.type === "ENDED") && typeof event.eventId === "string" && typeof event.taskId === "string" && typeof event.turnId === "string" && typeof event.occurredAt === "string";
|
|
6506
|
+
}
|
|
6507
|
+
|
|
6076
6508
|
// src/apiKeySession.ts
|
|
6077
6509
|
var API_KEY_EXCHANGE_PATH = "/api/v1/auth/exchange";
|
|
6078
6510
|
function appAccessTokenPath(appId) {
|
|
@@ -6143,6 +6575,7 @@ async function resolveApiKeyToken(call, appId, apiKey, errors = defaultSdkCoreEr
|
|
|
6143
6575
|
0 && (module.exports = {
|
|
6144
6576
|
API_KEY_EXCHANGE_PATH,
|
|
6145
6577
|
AgentTaskTurnError,
|
|
6578
|
+
FileTransferError,
|
|
6146
6579
|
SdkCoreConfigurationError,
|
|
6147
6580
|
SdkCoreResponseError,
|
|
6148
6581
|
appAccessTokenPath,
|
|
@@ -6158,6 +6591,7 @@ async function resolveApiKeyToken(call, appId, apiKey, errors = defaultSdkCoreEr
|
|
|
6158
6591
|
createCustomQueriesModule,
|
|
6159
6592
|
createDataSourcesModule,
|
|
6160
6593
|
createEntitiesModule,
|
|
6594
|
+
createFilesModule,
|
|
6161
6595
|
createFunctionsAdminModule,
|
|
6162
6596
|
createFunctionsModule,
|
|
6163
6597
|
createImportsModule,
|
|
@@ -6182,6 +6616,7 @@ async function resolveApiKeyToken(call, appId, apiKey, errors = defaultSdkCoreEr
|
|
|
6182
6616
|
expectObjectArray,
|
|
6183
6617
|
expectPage,
|
|
6184
6618
|
expectStringArray,
|
|
6619
|
+
isAgentTurnEvent,
|
|
6185
6620
|
isWorkspaceSession,
|
|
6186
6621
|
readTokenAppId,
|
|
6187
6622
|
readTokenExpiry,
|