@mitralab.io/sdk-core 0.3.8 → 0.3.10
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 +80 -0
- package/README.md +42 -17
- package/contracts/README.md +9 -0
- package/contracts/manifest.json +11 -1
- package/contracts/v0.3.10/sdk-parity.json +2902 -0
- package/contracts/v0.3.9/sdk-parity.json +2902 -0
- package/dist/index.cjs +467 -12
- package/dist/index.d.cts +216 -1
- package/dist/index.d.ts +216 -1
- package/dist/index.js +465 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -673,6 +673,55 @@ function expectImportExecution(value, context, errors = defaultSdkCoreErrorFacto
|
|
|
673
673
|
if (!isNullableString(execution.errorMessage)) invalidField(context, "errorMessage", errors);
|
|
674
674
|
return execution;
|
|
675
675
|
}
|
|
676
|
+
function expectFileMetadata(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
677
|
+
const file = expectObject(value, context, errors);
|
|
678
|
+
for (const field of ["id", "name", "contentType", "createdAt"]) {
|
|
679
|
+
if (typeof file[field] !== "string") invalidField(context, field, errors);
|
|
680
|
+
}
|
|
681
|
+
if (!isInteger(file.sizeBytes)) invalidField(context, "sizeBytes", errors);
|
|
682
|
+
if (!isOneOf(file.kind, ["TEMPORARY", "PERMANENT"])) {
|
|
683
|
+
invalidField(context, "kind", errors);
|
|
684
|
+
}
|
|
685
|
+
if (!isOneOf(file.visibility, ["PRIVATE", "RESTRICTED", "APP"])) {
|
|
686
|
+
invalidField(context, "visibility", errors);
|
|
687
|
+
}
|
|
688
|
+
if (!isOneOf(file.status, ["PENDING", "READY", "DELETED"])) {
|
|
689
|
+
invalidField(context, "status", errors);
|
|
690
|
+
}
|
|
691
|
+
if (!isOneOf(file.createdBy, ["USER", "FUNCTION"])) {
|
|
692
|
+
invalidField(context, "createdBy", errors);
|
|
693
|
+
}
|
|
694
|
+
for (const field of ["ownerUserId", "confirmedAt", "expiresAt"]) {
|
|
695
|
+
if (!isNullableString(file[field])) invalidField(context, field, errors);
|
|
696
|
+
}
|
|
697
|
+
return file;
|
|
698
|
+
}
|
|
699
|
+
function expectFileUpload(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
700
|
+
const upload = expectObject(value, context, errors);
|
|
701
|
+
for (const field of ["fileId", "uploadUrl", "uploadMethod", "expiresAt"]) {
|
|
702
|
+
if (typeof upload[field] !== "string" || !upload[field]) invalidField(context, field, errors);
|
|
703
|
+
}
|
|
704
|
+
if (!isStringRecord(upload.uploadHeaders)) invalidField(context, "uploadHeaders", errors);
|
|
705
|
+
return upload;
|
|
706
|
+
}
|
|
707
|
+
function expectFileReadUrl(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
708
|
+
const read = expectObject(value, context, errors);
|
|
709
|
+
if (typeof read.url !== "string" || !read.url) invalidField(context, "url", errors);
|
|
710
|
+
if (typeof read.expiresAt !== "string") invalidField(context, "expiresAt", errors);
|
|
711
|
+
return read;
|
|
712
|
+
}
|
|
713
|
+
function expectFileReadUrls(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
714
|
+
const response = expectObject(value, context, errors);
|
|
715
|
+
expectObjectArray(response.results, `${context} results`, errors).forEach(
|
|
716
|
+
(item, position) => {
|
|
717
|
+
const itemContext = `${context} result ${position}`;
|
|
718
|
+
if (typeof item.fileId !== "string") invalidField(itemContext, "fileId", errors);
|
|
719
|
+
if (typeof item.errorCode === "string") return;
|
|
720
|
+
expectFileReadUrl(item, itemContext, errors);
|
|
721
|
+
}
|
|
722
|
+
);
|
|
723
|
+
return response;
|
|
724
|
+
}
|
|
676
725
|
function expectMessageAccepted(value, context, errors = defaultSdkCoreErrorFactory) {
|
|
677
726
|
const response = expectObject(value, context, errors);
|
|
678
727
|
if (typeof response.messageId !== "string") invalidField(context, "messageId", errors);
|
|
@@ -2298,6 +2347,248 @@ function createEntitiesModule(transport, errors = defaultSdkCoreErrorFactory) {
|
|
|
2298
2347
|
});
|
|
2299
2348
|
}
|
|
2300
2349
|
|
|
2350
|
+
// src/fileTransferError.ts
|
|
2351
|
+
var FileTransferError = class extends Error {
|
|
2352
|
+
fileId;
|
|
2353
|
+
step;
|
|
2354
|
+
/**
|
|
2355
|
+
* HTTP status storage answered in `send` and `download`; null when storage gave no status or the
|
|
2356
|
+
* content failed to arrive, and always in `confirm`.
|
|
2357
|
+
*/
|
|
2358
|
+
status;
|
|
2359
|
+
/**
|
|
2360
|
+
* True when the `PENDING` file a failed `send` left behind was deleted. False when that delete
|
|
2361
|
+
* failed too, and always in `confirm` and `download`, where nothing is deleted.
|
|
2362
|
+
*/
|
|
2363
|
+
cleanedUp;
|
|
2364
|
+
constructor(message, details) {
|
|
2365
|
+
super(message, details.cause === void 0 ? void 0 : { cause: details.cause });
|
|
2366
|
+
this.name = "FileTransferError";
|
|
2367
|
+
this.fileId = details.fileId;
|
|
2368
|
+
this.step = details.step;
|
|
2369
|
+
this.status = details.status ?? null;
|
|
2370
|
+
this.cleanedUp = details.cleanedUp ?? false;
|
|
2371
|
+
}
|
|
2372
|
+
};
|
|
2373
|
+
|
|
2374
|
+
// src/modules/files.ts
|
|
2375
|
+
var MAX_READ_URLS = 100;
|
|
2376
|
+
var UNTYPED_CONTENT = "application/octet-stream";
|
|
2377
|
+
var REDACTED = "<redacted>";
|
|
2378
|
+
var MAX_CAUSE_DEPTH = 5;
|
|
2379
|
+
var MIN_SECRET_LENGTH = 16;
|
|
2380
|
+
var UNREADABLE_STORAGE_ERROR = "storage failed with an error the Core cannot show";
|
|
2381
|
+
function createFilesModule(transport, options = {}, errors = defaultSdkCoreErrorFactory) {
|
|
2382
|
+
const path = (fileId) => `/api/v1/files/${encodePathSegment(fileId, "file id", errors)}`;
|
|
2383
|
+
const storageFetch = () => {
|
|
2384
|
+
if (options.fetch) return options.fetch;
|
|
2385
|
+
const global = globalThis.fetch;
|
|
2386
|
+
if (typeof global !== "function") {
|
|
2387
|
+
configurationError("fetch is not available: pass files.fetch to the Core", errors);
|
|
2388
|
+
}
|
|
2389
|
+
return global;
|
|
2390
|
+
};
|
|
2391
|
+
const requestUpload = async (input) => expectFileUpload(
|
|
2392
|
+
await transport.request("/api/v1/files", { method: "POST", body: input }),
|
|
2393
|
+
"File upload response",
|
|
2394
|
+
errors
|
|
2395
|
+
);
|
|
2396
|
+
const confirmUpload = async (fileId) => expectFileMetadata(
|
|
2397
|
+
await transport.request(`${path(fileId)}/confirm`, { method: "POST" }),
|
|
2398
|
+
"Confirm file upload response",
|
|
2399
|
+
errors
|
|
2400
|
+
);
|
|
2401
|
+
const getUrl = async (fileId, { expiresInSeconds } = {}) => expectFileReadUrl(
|
|
2402
|
+
await transport.request(`${path(fileId)}/read-url`, {
|
|
2403
|
+
method: "POST",
|
|
2404
|
+
...expiresInSeconds === void 0 ? {} : { body: { expiresInSeconds } }
|
|
2405
|
+
}),
|
|
2406
|
+
"File read URL response",
|
|
2407
|
+
errors
|
|
2408
|
+
);
|
|
2409
|
+
const deleteFile = async (fileId) => {
|
|
2410
|
+
expectEmpty(
|
|
2411
|
+
await transport.request(path(fileId), { method: "DELETE" }),
|
|
2412
|
+
"Delete file response",
|
|
2413
|
+
errors
|
|
2414
|
+
);
|
|
2415
|
+
};
|
|
2416
|
+
const sendContent = async (fetch, upload, content, signal) => {
|
|
2417
|
+
const failure = await putContent(fetch, upload, content, signal);
|
|
2418
|
+
if (failure === null) return;
|
|
2419
|
+
const cleanedUp = await deleteFile(upload.fileId).then(
|
|
2420
|
+
() => true,
|
|
2421
|
+
() => false
|
|
2422
|
+
);
|
|
2423
|
+
if (failure.aborted && signal !== void 0) throw abortReason(signal);
|
|
2424
|
+
throw new FileTransferError(`File ${upload.fileId} was not sent: ${failure.reason}`, {
|
|
2425
|
+
fileId: upload.fileId,
|
|
2426
|
+
step: "send",
|
|
2427
|
+
status: failure.status,
|
|
2428
|
+
cause: "error" in failure ? storageCause(failure.error, upload.uploadUrl) : void 0,
|
|
2429
|
+
cleanedUp
|
|
2430
|
+
});
|
|
2431
|
+
};
|
|
2432
|
+
const confirmSent = async (fileId) => {
|
|
2433
|
+
try {
|
|
2434
|
+
return await confirmUpload(fileId);
|
|
2435
|
+
} catch (error) {
|
|
2436
|
+
throw new FileTransferError(
|
|
2437
|
+
`File ${fileId} was sent but not confirmed: ${messageOf(error)}`,
|
|
2438
|
+
{
|
|
2439
|
+
fileId,
|
|
2440
|
+
step: "confirm",
|
|
2441
|
+
cause: error
|
|
2442
|
+
}
|
|
2443
|
+
);
|
|
2444
|
+
}
|
|
2445
|
+
};
|
|
2446
|
+
return {
|
|
2447
|
+
requestUpload,
|
|
2448
|
+
confirmUpload,
|
|
2449
|
+
async upload(content, uploadOptions) {
|
|
2450
|
+
const fetch = storageFetch();
|
|
2451
|
+
const input = uploadInput(content, uploadOptions, errors);
|
|
2452
|
+
const { signal } = uploadOptions;
|
|
2453
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
2454
|
+
const upload = await requestUpload(input);
|
|
2455
|
+
await sendContent(fetch, upload, content, signal);
|
|
2456
|
+
return confirmSent(upload.fileId);
|
|
2457
|
+
},
|
|
2458
|
+
getUrl,
|
|
2459
|
+
async getUrls(fileIds, { expiresInSeconds } = {}) {
|
|
2460
|
+
requireBatchSize(fileIds, "fileIds", MAX_READ_URLS, errors);
|
|
2461
|
+
return expectFileReadUrls(
|
|
2462
|
+
await transport.request("/api/v1/files/read-urls", {
|
|
2463
|
+
method: "POST",
|
|
2464
|
+
body: { fileIds, ...expiresInSeconds === void 0 ? {} : { expiresInSeconds } }
|
|
2465
|
+
}),
|
|
2466
|
+
"File read URLs response",
|
|
2467
|
+
errors
|
|
2468
|
+
);
|
|
2469
|
+
},
|
|
2470
|
+
async download(fileId, { signal } = {}) {
|
|
2471
|
+
const fetch = storageFetch();
|
|
2472
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
2473
|
+
const { url } = await getUrl(fileId);
|
|
2474
|
+
return fetchContent(fetch, fileId, url, signal);
|
|
2475
|
+
},
|
|
2476
|
+
async get(fileId) {
|
|
2477
|
+
return expectFileMetadata(
|
|
2478
|
+
await transport.request(path(fileId), { method: "GET" }),
|
|
2479
|
+
"File response",
|
|
2480
|
+
errors
|
|
2481
|
+
);
|
|
2482
|
+
},
|
|
2483
|
+
delete: deleteFile
|
|
2484
|
+
};
|
|
2485
|
+
}
|
|
2486
|
+
function uploadInput(content, options, errors) {
|
|
2487
|
+
const name = options.name ?? fileNameOf(content);
|
|
2488
|
+
if (!name) configurationError("name is required when the content is not a named File", errors);
|
|
2489
|
+
const contentType = options.contentType || content.type || UNTYPED_CONTENT;
|
|
2490
|
+
return {
|
|
2491
|
+
name,
|
|
2492
|
+
contentType,
|
|
2493
|
+
sizeBytes: content.size,
|
|
2494
|
+
kind: options.kind,
|
|
2495
|
+
visibility: options.visibility,
|
|
2496
|
+
...options.appOwned === void 0 ? {} : { appOwned: options.appOwned }
|
|
2497
|
+
};
|
|
2498
|
+
}
|
|
2499
|
+
function fileNameOf(content) {
|
|
2500
|
+
const name = content.name;
|
|
2501
|
+
return typeof name === "string" ? name : void 0;
|
|
2502
|
+
}
|
|
2503
|
+
async function putContent(fetch, upload, content, signal) {
|
|
2504
|
+
try {
|
|
2505
|
+
const response = await fetch(upload.uploadUrl, {
|
|
2506
|
+
method: upload.uploadMethod,
|
|
2507
|
+
headers: upload.uploadHeaders,
|
|
2508
|
+
body: content,
|
|
2509
|
+
redirect: "error",
|
|
2510
|
+
...signal === void 0 ? {} : { signal }
|
|
2511
|
+
});
|
|
2512
|
+
discardBody(response);
|
|
2513
|
+
if (response.ok) return null;
|
|
2514
|
+
return {
|
|
2515
|
+
reason: `storage answered ${response.status}`,
|
|
2516
|
+
status: response.status,
|
|
2517
|
+
aborted: false
|
|
2518
|
+
};
|
|
2519
|
+
} catch (error) {
|
|
2520
|
+
return {
|
|
2521
|
+
reason: "the transfer to storage failed",
|
|
2522
|
+
status: null,
|
|
2523
|
+
aborted: signal?.aborted === true,
|
|
2524
|
+
error
|
|
2525
|
+
};
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
async function fetchContent(fetch, fileId, url, signal) {
|
|
2529
|
+
let response;
|
|
2530
|
+
try {
|
|
2531
|
+
response = await fetch(url, {
|
|
2532
|
+
method: "GET",
|
|
2533
|
+
redirect: "error",
|
|
2534
|
+
...signal === void 0 ? {} : { signal }
|
|
2535
|
+
});
|
|
2536
|
+
if (response.ok) return await response.blob();
|
|
2537
|
+
} catch (error) {
|
|
2538
|
+
if (signal?.aborted) throw abortReason(signal);
|
|
2539
|
+
throw new FileTransferError(
|
|
2540
|
+
`File ${fileId} was not downloaded: the transfer from storage failed`,
|
|
2541
|
+
{ fileId, step: "download", cause: storageCause(error, url) }
|
|
2542
|
+
);
|
|
2543
|
+
}
|
|
2544
|
+
discardBody(response);
|
|
2545
|
+
throw new FileTransferError(
|
|
2546
|
+
`File ${fileId} was not downloaded: storage answered ${response.status}`,
|
|
2547
|
+
{ fileId, step: "download", status: response.status }
|
|
2548
|
+
);
|
|
2549
|
+
}
|
|
2550
|
+
function storageCause(thrown, link) {
|
|
2551
|
+
const secrets = linkSecrets(link);
|
|
2552
|
+
const redact = (text) => secrets.reduce((result, secret) => result.replaceAll(secret, REDACTED), text);
|
|
2553
|
+
const rebuild = (value, depth) => {
|
|
2554
|
+
if (!(value instanceof Error)) {
|
|
2555
|
+
const opaque = typeof value === "function" || typeof value === "object" && value !== null;
|
|
2556
|
+
return new Error(opaque ? UNREADABLE_STORAGE_ERROR : redact(String(value)));
|
|
2557
|
+
}
|
|
2558
|
+
const below = depth < MAX_CAUSE_DEPTH && value.cause != null ? rebuild(value.cause, depth + 1) : void 0;
|
|
2559
|
+
const error = new Error(
|
|
2560
|
+
redact(String(value.message)),
|
|
2561
|
+
below === void 0 ? void 0 : { cause: below }
|
|
2562
|
+
);
|
|
2563
|
+
error.name = redact(String(value.name));
|
|
2564
|
+
if (typeof value.stack === "string") error.stack = redact(value.stack);
|
|
2565
|
+
const { code } = value;
|
|
2566
|
+
return typeof code === "string" ? Object.assign(error, { code: redact(code) }) : error;
|
|
2567
|
+
};
|
|
2568
|
+
try {
|
|
2569
|
+
return rebuild(thrown, 1);
|
|
2570
|
+
} catch {
|
|
2571
|
+
return new Error(UNREADABLE_STORAGE_ERROR);
|
|
2572
|
+
}
|
|
2573
|
+
}
|
|
2574
|
+
function linkSecrets(link) {
|
|
2575
|
+
const start = link.indexOf("?");
|
|
2576
|
+
const query = start < 0 ? "" : link.slice(start + 1);
|
|
2577
|
+
const raw = query.split("&").map((pair) => pair.slice(pair.indexOf("=") + 1));
|
|
2578
|
+
const decoded = [...new URLSearchParams(query).values()];
|
|
2579
|
+
const values = [...raw, ...decoded].filter((value) => value.length >= MIN_SECRET_LENGTH);
|
|
2580
|
+
return [link, query, ...values].filter((secret) => secret.length > 0);
|
|
2581
|
+
}
|
|
2582
|
+
function discardBody(response) {
|
|
2583
|
+
void Promise.resolve().then(() => response.body?.cancel()).catch(() => void 0);
|
|
2584
|
+
}
|
|
2585
|
+
function abortReason(signal) {
|
|
2586
|
+
return signal.reason ?? new DOMException("This operation was aborted", "AbortError");
|
|
2587
|
+
}
|
|
2588
|
+
function messageOf(error) {
|
|
2589
|
+
return error instanceof Error ? error.message : String(error);
|
|
2590
|
+
}
|
|
2591
|
+
|
|
2301
2592
|
// src/modules/functions.ts
|
|
2302
2593
|
var DefaultFunctionsModule = class {
|
|
2303
2594
|
constructor(transport, errors, options) {
|
|
@@ -3307,6 +3598,7 @@ function createSdkCore(options) {
|
|
|
3307
3598
|
customQueries: createCustomQueriesModule(options.transports.dataManager, errors),
|
|
3308
3599
|
dataSources: createDataSourcesModule(options.transports.dataManager, errors),
|
|
3309
3600
|
entities: createEntitiesModule(options.transports.dataManager, errors),
|
|
3601
|
+
files: createFilesModule(options.transports.dataManager, options.files, errors),
|
|
3310
3602
|
functions: createFunctionsModule(options.transports.functions, options.functions, errors),
|
|
3311
3603
|
functionsAdmin,
|
|
3312
3604
|
imports: createImportsModule(options.transports.dataManager, errors),
|
|
@@ -4199,6 +4491,14 @@ function errorCode(error) {
|
|
|
4199
4491
|
const code = candidate.code ?? candidate.details?.code ?? candidate.details?.error_code;
|
|
4200
4492
|
return typeof code === "string" ? code : void 0;
|
|
4201
4493
|
}
|
|
4494
|
+
function refusalSentence(error) {
|
|
4495
|
+
if (!error || typeof error !== "object" || errorCode(error) === void 0) return void 0;
|
|
4496
|
+
const candidate = error;
|
|
4497
|
+
const refused = error instanceof AgentTaskTurnError || typeof candidate.status === "number" && candidate.status >= 400;
|
|
4498
|
+
if (!refused) return void 0;
|
|
4499
|
+
const sentence = asObject2(candidate.details)?.message ?? asObject2(candidate.body)?.message ?? errorMessage2(error);
|
|
4500
|
+
return typeof sentence === "string" && sentence.trim() ? sentence : void 0;
|
|
4501
|
+
}
|
|
4202
4502
|
function delay(milliseconds) {
|
|
4203
4503
|
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
4204
4504
|
}
|
|
@@ -4278,6 +4578,46 @@ function historyCursorOf(history) {
|
|
|
4278
4578
|
}
|
|
4279
4579
|
return { sequence, ...newest.boxLog === void 0 ? {} : { boxLog: newest.boxLog } };
|
|
4280
4580
|
}
|
|
4581
|
+
var SUBAGENT_END_STATUSES = /* @__PURE__ */ new Set([
|
|
4582
|
+
"completed",
|
|
4583
|
+
"failed",
|
|
4584
|
+
"cancelled",
|
|
4585
|
+
"interrupted"
|
|
4586
|
+
]);
|
|
4587
|
+
function samePiece(held, row) {
|
|
4588
|
+
if (held.id === row.id) return true;
|
|
4589
|
+
if (!held.id.startsWith("live:")) return false;
|
|
4590
|
+
if (held.kind === "user" && row.kind === "user" && held.clientMessageId !== void 0) {
|
|
4591
|
+
return held.clientMessageId === row.clientMessageId;
|
|
4592
|
+
}
|
|
4593
|
+
if (row.itemId === void 0) return false;
|
|
4594
|
+
if (held.kind !== row.kind || held.itemId !== row.itemId) return false;
|
|
4595
|
+
return held.kind !== "tool" || row.kind === "tool" && held.tool.phase === row.tool.phase;
|
|
4596
|
+
}
|
|
4597
|
+
function heldIsNewer(held, row) {
|
|
4598
|
+
const heldLast = held.lastSequence ?? held.sequence;
|
|
4599
|
+
const rowLast = row.lastSequence ?? row.sequence;
|
|
4600
|
+
if (heldLast === void 0 || rowLast === void 0) return false;
|
|
4601
|
+
if (held.boxLog !== void 0 && held.boxLog !== row.boxLog) return false;
|
|
4602
|
+
return heldLast > rowLast;
|
|
4603
|
+
}
|
|
4604
|
+
function heldHasRow(held, row) {
|
|
4605
|
+
if (held.kind !== "agent" && held.kind !== "reasoning") return true;
|
|
4606
|
+
return held.sequence !== void 0 && row.sequence !== void 0 && held.sequence <= row.sequence;
|
|
4607
|
+
}
|
|
4608
|
+
function oldestRowOf(history) {
|
|
4609
|
+
return history.find((item) => !item.id.startsWith("live:"));
|
|
4610
|
+
}
|
|
4611
|
+
function rowOrHeld(held, row, liveComparable) {
|
|
4612
|
+
if (!liveComparable || !heldIsNewer(held, row) || !heldHasRow(held, row)) return row;
|
|
4613
|
+
return {
|
|
4614
|
+
...held,
|
|
4615
|
+
id: row.id,
|
|
4616
|
+
...row.sequence === void 0 ? {} : { sequence: row.sequence },
|
|
4617
|
+
...held.lastSequence === void 0 && held.sequence === void 0 ? {} : { lastSequence: held.lastSequence ?? held.sequence },
|
|
4618
|
+
...row.boxLog === void 0 ? {} : { boxLog: row.boxLog }
|
|
4619
|
+
};
|
|
4620
|
+
}
|
|
4281
4621
|
function hasOlderRows(page) {
|
|
4282
4622
|
return page.page.totalPages > page.page.number + 1;
|
|
4283
4623
|
}
|
|
@@ -4489,6 +4829,9 @@ var CoreAgentTaskSession = class {
|
|
|
4489
4829
|
// Whether the box the channel follows counts in the log of the history's rows. A box whose hello
|
|
4490
4830
|
// names another log, or reports a log shorter than the history's, has sequences that start over.
|
|
4491
4831
|
rowsInChannelLog = true;
|
|
4832
|
+
// The log the hello of the channel named. A reread compares its rows with it every time: the
|
|
4833
|
+
// flag above starts over at each reread, and a page from the old log may come again.
|
|
4834
|
+
channelLog;
|
|
4492
4835
|
olderRead = null;
|
|
4493
4836
|
get taskId() {
|
|
4494
4837
|
return this._taskId;
|
|
@@ -4588,7 +4931,7 @@ var CoreAgentTaskSession = class {
|
|
|
4588
4931
|
include: "plan"
|
|
4589
4932
|
});
|
|
4590
4933
|
this.historyLimit = options.limit;
|
|
4591
|
-
this.
|
|
4934
|
+
await this.rereadHistory(page);
|
|
4592
4935
|
const history = this.history;
|
|
4593
4936
|
this.emit("historyLoaded", { history });
|
|
4594
4937
|
return history;
|
|
@@ -4607,7 +4950,7 @@ var CoreAgentTaskSession = class {
|
|
|
4607
4950
|
return this.olderRead.promise;
|
|
4608
4951
|
}
|
|
4609
4952
|
async readOlder(generation) {
|
|
4610
|
-
const oldest = this._history
|
|
4953
|
+
const oldest = oldestRowOf(this._history);
|
|
4611
4954
|
if (!this._taskId || !this._hasOlder || !oldest) return [];
|
|
4612
4955
|
let page;
|
|
4613
4956
|
try {
|
|
@@ -4623,17 +4966,110 @@ var CoreAgentTaskSession = class {
|
|
|
4623
4966
|
throw error;
|
|
4624
4967
|
}
|
|
4625
4968
|
if (generation !== this.historyGeneration || this.isClosed()) return [];
|
|
4626
|
-
const
|
|
4969
|
+
const rows = [...page.content].reverse().map(toAgentTimelineItem);
|
|
4970
|
+
const rowsLog = [...rows].reverse().find((item) => item.boxLog !== void 0)?.boxLog;
|
|
4971
|
+
const liveComparable = this.rowsInChannelLog && (this.channelLog === void 0 || rowsLog === void 0 || rowsLog === this.channelLog);
|
|
4972
|
+
const taken = /* @__PURE__ */ new Set();
|
|
4973
|
+
const older = rows.map((row) => {
|
|
4974
|
+
const held = this._history.find(
|
|
4975
|
+
(item) => !taken.has(item) && item.id.startsWith("live:") && samePiece(item, row)
|
|
4976
|
+
);
|
|
4977
|
+
if (held === void 0) return row;
|
|
4978
|
+
taken.add(held);
|
|
4979
|
+
return rowOrHeld(held, row, liveComparable);
|
|
4980
|
+
});
|
|
4627
4981
|
this._hasOlder = hasOlderRows(page);
|
|
4628
|
-
this._history = [...older, ...this._history];
|
|
4982
|
+
this._history = [...older, ...this._history.filter((item) => !taken.has(item))];
|
|
4629
4983
|
this.emit("historyLoaded", { history: this.history });
|
|
4630
4984
|
return older;
|
|
4631
4985
|
}
|
|
4986
|
+
// A page that starts at a piece held live, with live pieces before it that no row of the page
|
|
4987
|
+
// takes, skipped rows the Copilot wrote between two rereads (more than a page of them): kept as
|
|
4988
|
+
// they are, those pieces never get their rows, and `loadOlder` only reads before the oldest row.
|
|
4989
|
+
// The rows before the page are read as `loadOlder` reads them, until the page reaches a row the
|
|
4990
|
+
// history holds or the gap's pieces all have their rows. A read of a history since replaced
|
|
4991
|
+
// lands nowhere (#145).
|
|
4992
|
+
async rereadHistory(page) {
|
|
4993
|
+
const generation = this.historyGeneration;
|
|
4994
|
+
let current = page;
|
|
4995
|
+
let extended = false;
|
|
4996
|
+
while (this._taskId && hasOlderRows(current) && this.skipsRows(current, extended)) {
|
|
4997
|
+
const oldest = current.content[current.content.length - 1];
|
|
4998
|
+
if (!oldest) break;
|
|
4999
|
+
let older;
|
|
5000
|
+
try {
|
|
5001
|
+
older = await this.dependencies.tasks.listMessages(this._taskId, {
|
|
5002
|
+
before: oldest.id,
|
|
5003
|
+
...this.historyLimit !== void 0 ? { size: this.historyLimit } : {},
|
|
5004
|
+
sort: "createdAt,desc",
|
|
5005
|
+
kinds: "all",
|
|
5006
|
+
include: "plan"
|
|
5007
|
+
});
|
|
5008
|
+
} catch (error) {
|
|
5009
|
+
if (generation !== this.historyGeneration || this.isClosed()) return;
|
|
5010
|
+
throw error;
|
|
5011
|
+
}
|
|
5012
|
+
if (generation !== this.historyGeneration || this.isClosed()) return;
|
|
5013
|
+
current = { content: [...current.content, ...older.content], page: older.page };
|
|
5014
|
+
extended = true;
|
|
5015
|
+
}
|
|
5016
|
+
this.replaceHistory(current);
|
|
5017
|
+
}
|
|
5018
|
+
// Newest first, as the Copilot answers. Once rows before the page were read, a row the history
|
|
5019
|
+
// does not hold at all (one the session never heard) is still inside the gap.
|
|
5020
|
+
skipsRows(page, extended) {
|
|
5021
|
+
const rows = page.content.map(toAgentTimelineItem);
|
|
5022
|
+
const first = rows[rows.length - 1];
|
|
5023
|
+
if (!first) return false;
|
|
5024
|
+
const anchor = this._history.findIndex((item) => samePiece(item, first));
|
|
5025
|
+
if (anchor < 0) return extended;
|
|
5026
|
+
if (!this._history[anchor]?.id.startsWith("live:")) return false;
|
|
5027
|
+
for (let index = anchor - 1; index >= 0; index -= 1) {
|
|
5028
|
+
const held = this._history[index];
|
|
5029
|
+
if (!held?.id.startsWith("live:")) return false;
|
|
5030
|
+
if (!rows.some((row) => samePiece(held, row))) return true;
|
|
5031
|
+
}
|
|
5032
|
+
return false;
|
|
5033
|
+
}
|
|
5034
|
+
// The recovery rereads only the newest 100 rows while the turn runs: the older pages already
|
|
5035
|
+
// read stay, or what sits in them (the cards of subagents still running) vanishes until
|
|
5036
|
+
// `loadOlder` (#145). A page that does not reach the history it already has replaces it.
|
|
4632
5037
|
replaceHistory(page) {
|
|
4633
|
-
this.
|
|
5038
|
+
const oldest = oldestRowOf(this._history)?.id;
|
|
5039
|
+
const rows = [...page.content].reverse().map(toAgentTimelineItem);
|
|
5040
|
+
const rowsLog = [...rows].reverse().find((item) => item.boxLog !== void 0)?.boxLog;
|
|
5041
|
+
const liveComparable = this.rowsInChannelLog && (this.channelLog === void 0 || rowsLog === void 0 || rowsLog === this.channelLog);
|
|
4634
5042
|
this.rowsInChannelLog = true;
|
|
4635
|
-
|
|
4636
|
-
|
|
5043
|
+
const first = rows[0];
|
|
5044
|
+
const anchor = first ? this._history.findIndex((item) => samePiece(item, first)) : -1;
|
|
5045
|
+
const kept = anchor < 0 ? [] : this._history.slice(0, anchor).filter((item) => !rows.some((row) => samePiece(item, row)));
|
|
5046
|
+
const replaced = anchor < 0 ? this._history : this._history.slice(anchor);
|
|
5047
|
+
if (anchor < 0) this._hasOlder = hasOlderRows(page);
|
|
5048
|
+
const used = /* @__PURE__ */ new Set();
|
|
5049
|
+
const merged = rows.map((row) => {
|
|
5050
|
+
const held = this._history.find((item) => !used.has(item) && samePiece(item, row));
|
|
5051
|
+
if (held === void 0) return row;
|
|
5052
|
+
used.add(held);
|
|
5053
|
+
return rowOrHeld(held, row, liveComparable);
|
|
5054
|
+
});
|
|
5055
|
+
const cursor = liveComparable ? historyCursorOf(rows) : void 0;
|
|
5056
|
+
const unrecorded = cursor === void 0 ? [] : replaced.filter(
|
|
5057
|
+
(item) => !used.has(item) && item.id.startsWith("live:") && item.sequence !== void 0 && (item.kind === "plan" || item.sequence > cursor.sequence)
|
|
5058
|
+
);
|
|
5059
|
+
this._history = [...kept, ...merged, ...unrecorded];
|
|
5060
|
+
if (anchor < 0 || oldestRowOf(this._history)?.id !== oldest) this.historyGeneration += 1;
|
|
5061
|
+
if (cursor === void 0) return;
|
|
5062
|
+
for (const subagent of this.subagentsById.values()) {
|
|
5063
|
+
const row = this._history.find(
|
|
5064
|
+
(item) => item.kind === "tool" && item.tool.toolId === subagent.toolId
|
|
5065
|
+
);
|
|
5066
|
+
const rowStatus = row?.kind === "tool" ? row.subagent?.status : void 0;
|
|
5067
|
+
if (rowStatus !== void 0 && SUBAGENT_END_STATUSES.has(rowStatus)) continue;
|
|
5068
|
+
const applied = this.subagentSequences.get(subagent.subagentId);
|
|
5069
|
+
if (SUBAGENT_END_STATUSES.has(subagent.status) || applied !== void 0 && applied > cursor.sequence) {
|
|
5070
|
+
this.writeSubagentOnSpawn(subagent);
|
|
5071
|
+
}
|
|
5072
|
+
}
|
|
4637
5073
|
}
|
|
4638
5074
|
editQueueItem(id, text) {
|
|
4639
5075
|
this.requireOpen();
|
|
@@ -4732,7 +5168,7 @@ var CoreAgentTaskSession = class {
|
|
|
4732
5168
|
});
|
|
4733
5169
|
const dispatch = this.dispatchSend(prompt, options, clientMessageId, handedOver, waiter).catch((error) => {
|
|
4734
5170
|
waiter?.reject(error);
|
|
4735
|
-
this.
|
|
5171
|
+
this.emitSendError(error);
|
|
4736
5172
|
if (this._status === "streaming") {
|
|
4737
5173
|
this.settleSteers((steered) => steered.reject(error));
|
|
4738
5174
|
this.setStatus("idle");
|
|
@@ -4800,7 +5236,7 @@ var CoreAgentTaskSession = class {
|
|
|
4800
5236
|
taken = await admitted;
|
|
4801
5237
|
} catch (error) {
|
|
4802
5238
|
handedOver();
|
|
4803
|
-
this.
|
|
5239
|
+
this.emitSendError(error);
|
|
4804
5240
|
return fail(error);
|
|
4805
5241
|
}
|
|
4806
5242
|
if (!taken) {
|
|
@@ -4870,7 +5306,7 @@ var CoreAgentTaskSession = class {
|
|
|
4870
5306
|
}).catch((error) => {
|
|
4871
5307
|
this.forgetSteer(steered);
|
|
4872
5308
|
waiter?.reject(error);
|
|
4873
|
-
this.
|
|
5309
|
+
this.emitSendError(error);
|
|
4874
5310
|
}).finally(() => {
|
|
4875
5311
|
this.pendingSteers -= 1;
|
|
4876
5312
|
});
|
|
@@ -5062,7 +5498,7 @@ var CoreAgentTaskSession = class {
|
|
|
5062
5498
|
kinds: "all",
|
|
5063
5499
|
include: "plan"
|
|
5064
5500
|
});
|
|
5065
|
-
this.
|
|
5501
|
+
await this.rereadHistory(page);
|
|
5066
5502
|
this.emit("historyLoaded", { history: this.history });
|
|
5067
5503
|
const rows = page.content.filter(isChatRow);
|
|
5068
5504
|
const recovered = rows.find(
|
|
@@ -5216,6 +5652,7 @@ var CoreAgentTaskSession = class {
|
|
|
5216
5652
|
break;
|
|
5217
5653
|
case "hello": {
|
|
5218
5654
|
const lastSequence = payload?.lastSequence;
|
|
5655
|
+
if (typeof payload?.boxLog === "number") this.channelLog = payload.boxLog;
|
|
5219
5656
|
const history = historyCursorOf(this._history);
|
|
5220
5657
|
const rowsLog = [...this._history].reverse().find((item) => item.boxLog !== void 0)?.boxLog;
|
|
5221
5658
|
if (typeof payload?.boxLog === "number" && rowsLog !== void 0 && payload.boxLog !== rowsLog || typeof lastSequence === "number" && history !== void 0 && lastSequence < history.sequence) {
|
|
@@ -5692,6 +6129,12 @@ var CoreAgentTaskSession = class {
|
|
|
5692
6129
|
...typeof payload?.childThreadId === "string" ? { childThreadId: payload.childThreadId } : {}
|
|
5693
6130
|
};
|
|
5694
6131
|
this.subagentsById.set(subagentId, subagent);
|
|
6132
|
+
this.writeSubagentOnSpawn(subagent);
|
|
6133
|
+
this.emit("subagent", { ...subagent, ...sequenceOf(event) });
|
|
6134
|
+
}
|
|
6135
|
+
// The call that spawned it carries its last state, as the history records it.
|
|
6136
|
+
writeSubagentOnSpawn(subagent) {
|
|
6137
|
+
const { toolId, subagentId, status } = subagent;
|
|
5695
6138
|
const index = this._history.findIndex(
|
|
5696
6139
|
(item) => item.kind === "tool" && item.tool.toolId === toolId
|
|
5697
6140
|
);
|
|
@@ -5709,7 +6152,6 @@ var CoreAgentTaskSession = class {
|
|
|
5709
6152
|
}
|
|
5710
6153
|
};
|
|
5711
6154
|
}
|
|
5712
|
-
this.emit("subagent", { ...subagent, ...sequenceOf(event) });
|
|
5713
6155
|
}
|
|
5714
6156
|
finishTurn(reason, usage, end = {}) {
|
|
5715
6157
|
if (this._status !== "streaming" && this._status !== "cancelled") return;
|
|
@@ -5807,6 +6249,15 @@ var CoreAgentTaskSession = class {
|
|
|
5807
6249
|
error: `${prefix}: ${errorMessage2(error)}`
|
|
5808
6250
|
});
|
|
5809
6251
|
}
|
|
6252
|
+
emitSendError(error) {
|
|
6253
|
+
const sentence = refusalSentence(error);
|
|
6254
|
+
if (sentence === void 0) {
|
|
6255
|
+
this.emitError("Failed to send Agent prompt", error);
|
|
6256
|
+
return;
|
|
6257
|
+
}
|
|
6258
|
+
const code = errorCode(error);
|
|
6259
|
+
this.emit("error", { ...code ? { code } : {}, error: sentence });
|
|
6260
|
+
}
|
|
5810
6261
|
setStatus(status) {
|
|
5811
6262
|
if (this._status === status) return;
|
|
5812
6263
|
if (status !== "streaming" && status !== "cancelled") {
|
|
@@ -5910,6 +6361,7 @@ async function resolveApiKeyToken(call, appId, apiKey, errors = defaultSdkCoreEr
|
|
|
5910
6361
|
export {
|
|
5911
6362
|
API_KEY_EXCHANGE_PATH,
|
|
5912
6363
|
AgentTaskTurnError,
|
|
6364
|
+
FileTransferError,
|
|
5913
6365
|
SdkCoreConfigurationError,
|
|
5914
6366
|
SdkCoreResponseError,
|
|
5915
6367
|
appAccessTokenPath,
|
|
@@ -5925,6 +6377,7 @@ export {
|
|
|
5925
6377
|
createCustomQueriesModule,
|
|
5926
6378
|
createDataSourcesModule,
|
|
5927
6379
|
createEntitiesModule,
|
|
6380
|
+
createFilesModule,
|
|
5928
6381
|
createFunctionsAdminModule,
|
|
5929
6382
|
createFunctionsModule,
|
|
5930
6383
|
createImportsModule,
|