@reinsjs/core 0.14.1 → 0.16.0
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 +13 -0
- package/README.md +16 -1
- package/dist/{chunk-DFXHUJV4.js → chunk-7RHFFDGN.js} +7 -2
- package/dist/chunk-7RHFFDGN.js.map +1 -0
- package/dist/{core-BhmO9N-j.d.ts → core-BxGiBJ_p.d.ts} +58 -7
- package/dist/index.d.ts +181 -6
- package/dist/index.js +342 -39
- package/dist/index.js.map +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-DFXHUJV4.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
createEvent,
|
|
9
9
|
renderToolReference,
|
|
10
10
|
uuidv7
|
|
11
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-7RHFFDGN.js";
|
|
12
12
|
|
|
13
13
|
// src/events/core.ts
|
|
14
14
|
function contextTokensOf(usage) {
|
|
@@ -300,6 +300,48 @@ function renderFoldedToolResults(items, opts) {
|
|
|
300
300
|
}
|
|
301
301
|
return lines.join("\n");
|
|
302
302
|
}
|
|
303
|
+
function foldedUserMedia(removed) {
|
|
304
|
+
const out = [];
|
|
305
|
+
for (const e of removed) {
|
|
306
|
+
if (e.type !== "core.user_message") continue;
|
|
307
|
+
const media = [];
|
|
308
|
+
for (const p of e.payload.content) {
|
|
309
|
+
switch (p.type) {
|
|
310
|
+
case "image":
|
|
311
|
+
case "video":
|
|
312
|
+
case "audio":
|
|
313
|
+
media.push({ type: p.type, mime: p.mime });
|
|
314
|
+
break;
|
|
315
|
+
case "text":
|
|
316
|
+
case "tool_reference":
|
|
317
|
+
break;
|
|
318
|
+
default: {
|
|
319
|
+
const _exhaustive = p;
|
|
320
|
+
return _exhaustive;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (media.length > 0) out.push({ seq: e.seq, media });
|
|
325
|
+
}
|
|
326
|
+
return out.sort((a, b) => a.seq - b.seq);
|
|
327
|
+
}
|
|
328
|
+
function renderFoldedUserMedia(items, opts) {
|
|
329
|
+
if (items.length === 0) return "";
|
|
330
|
+
const maxItems = opts.maxItems ?? 80;
|
|
331
|
+
const lines = [opts.heading];
|
|
332
|
+
const shown = items.slice(0, maxItems);
|
|
333
|
+
for (const it of shown) {
|
|
334
|
+
const byType = /* @__PURE__ */ new Map();
|
|
335
|
+
for (const m of it.media) byType.set(m.type, [...byType.get(m.type) ?? [], m.mime]);
|
|
336
|
+
const desc = [...byType.entries()].map(([type, mimes]) => `${mimes.length} ${type}${mimes.length > 1 ? "s" : ""} (${mimes.join(", ")})`).join(", ");
|
|
337
|
+
lines.push(`- seq ${it.seq} user message \u2014 ${desc}`);
|
|
338
|
+
}
|
|
339
|
+
if (items.length > shown.length) {
|
|
340
|
+
const rest = items.slice(shown.length);
|
|
341
|
+
lines.push(`- \u2026and ${rest.length} more (seq ${rest[0]?.seq}\u2013${rest[rest.length - 1]?.seq})`);
|
|
342
|
+
}
|
|
343
|
+
return lines.join("\n");
|
|
344
|
+
}
|
|
303
345
|
|
|
304
346
|
// src/projection/truncate.ts
|
|
305
347
|
var ASSISTANT_TYPES = /* @__PURE__ */ new Set([
|
|
@@ -370,6 +412,10 @@ var defaultThresholdSummary = (removed, ctx) => {
|
|
|
370
412
|
heading: "Tool results that were removed (still in the session log, by seq):"
|
|
371
413
|
});
|
|
372
414
|
if (manifest) lines.push(manifest);
|
|
415
|
+
const media = renderFoldedUserMedia(foldedUserMedia(fresh), {
|
|
416
|
+
heading: "Images, video or audio in removed user messages (still in the session log, by seq):"
|
|
417
|
+
});
|
|
418
|
+
if (media) lines.push(media);
|
|
373
419
|
return lines.join("\n");
|
|
374
420
|
};
|
|
375
421
|
function turnStartsCleanly(turn) {
|
|
@@ -616,6 +662,164 @@ async function forkSession(log, opts) {
|
|
|
616
662
|
return { toSessionId };
|
|
617
663
|
}
|
|
618
664
|
|
|
665
|
+
// src/store/errors.ts
|
|
666
|
+
var StoreError = class extends Error {
|
|
667
|
+
constructor(code, message, context = {}) {
|
|
668
|
+
super(`[${code}] ${message}`);
|
|
669
|
+
this.code = code;
|
|
670
|
+
this.context = context;
|
|
671
|
+
this.name = "StoreError";
|
|
672
|
+
}
|
|
673
|
+
code;
|
|
674
|
+
context;
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
// src/loop/media-refs.ts
|
|
678
|
+
var IMAGE_REF_ID_MAX = 128;
|
|
679
|
+
function isImageRef(p) {
|
|
680
|
+
if (typeof p !== "object" || p === null) return false;
|
|
681
|
+
const r = p;
|
|
682
|
+
return r.type === "image" && typeof r.mime === "string" && r.mime.startsWith("image/") && typeof r.blobId === "string" && r.blobId.length > 0 && r.blobId.length <= IMAGE_REF_ID_MAX && r.data === void 0;
|
|
683
|
+
}
|
|
684
|
+
function imageRefsIn(parts) {
|
|
685
|
+
return parts.filter(isImageRef);
|
|
686
|
+
}
|
|
687
|
+
function contentOf(e) {
|
|
688
|
+
if (e.type === "core.user_message") return e.payload.content;
|
|
689
|
+
if (e.type === "core.tool_result") return e.payload.content;
|
|
690
|
+
return void 0;
|
|
691
|
+
}
|
|
692
|
+
function mediaRefsOf(timeline) {
|
|
693
|
+
const ids = /* @__PURE__ */ new Set();
|
|
694
|
+
for (const e of timeline) {
|
|
695
|
+
const parts = contentOf(e);
|
|
696
|
+
if (!Array.isArray(parts)) continue;
|
|
697
|
+
for (const p of imageRefsIn(parts)) ids.add(p.blobId);
|
|
698
|
+
}
|
|
699
|
+
return ids;
|
|
700
|
+
}
|
|
701
|
+
function blobUsableBy(sessionId, meta, id, timeline) {
|
|
702
|
+
return meta.sessionId === sessionId || mediaRefsOf(timeline).has(id);
|
|
703
|
+
}
|
|
704
|
+
function imageRefNotFound(id) {
|
|
705
|
+
return `Blob ${id} not found in this session`;
|
|
706
|
+
}
|
|
707
|
+
async function checkImageRefs(parts, ctx) {
|
|
708
|
+
for (const ref of imageRefsIn(parts)) {
|
|
709
|
+
if (ctx.blobs === void 0) return imageRefNotFound(ref.blobId);
|
|
710
|
+
let meta;
|
|
711
|
+
const cached = ctx.cache.get(ref.blobId);
|
|
712
|
+
if (cached !== void 0) meta = cached.meta;
|
|
713
|
+
else {
|
|
714
|
+
let got;
|
|
715
|
+
try {
|
|
716
|
+
got = await ctx.blobs.get(ref.blobId);
|
|
717
|
+
} catch (err) {
|
|
718
|
+
if (err instanceof StoreError && err.code === "not_found") return imageRefNotFound(ref.blobId);
|
|
719
|
+
throw err;
|
|
720
|
+
}
|
|
721
|
+
meta = got.meta;
|
|
722
|
+
ctx.cache.set(ref.blobId, { data: bytesToBase64(got.bytes), meta });
|
|
723
|
+
}
|
|
724
|
+
if (!blobUsableBy(ctx.sessionId, meta, ref.blobId, ctx.timeline)) return imageRefNotFound(ref.blobId);
|
|
725
|
+
if (meta.mime !== ref.mime) return imageRefNotFound(ref.blobId);
|
|
726
|
+
}
|
|
727
|
+
return void 0;
|
|
728
|
+
}
|
|
729
|
+
async function hydrateImageRefs(events, ctx) {
|
|
730
|
+
const out = [];
|
|
731
|
+
for (const e of events) {
|
|
732
|
+
const parts = contentOf(e);
|
|
733
|
+
if (!Array.isArray(parts) || !parts.some(isImageRef)) {
|
|
734
|
+
out.push(e);
|
|
735
|
+
continue;
|
|
736
|
+
}
|
|
737
|
+
const next = [];
|
|
738
|
+
for (const p of parts) {
|
|
739
|
+
if (!isImageRef(p)) {
|
|
740
|
+
next.push(p);
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
const data = await loadImage(p.blobId, ctx);
|
|
744
|
+
next.push(data === void 0 ? p : { type: "image", mime: p.mime, data });
|
|
745
|
+
}
|
|
746
|
+
out.push({ ...e, payload: { ...e.payload, content: next } });
|
|
747
|
+
}
|
|
748
|
+
return out;
|
|
749
|
+
}
|
|
750
|
+
async function loadImage(id, ctx) {
|
|
751
|
+
const cached = ctx.cache.get(id);
|
|
752
|
+
if (cached !== void 0) return cached.data;
|
|
753
|
+
if (ctx.blobs === void 0) return void 0;
|
|
754
|
+
try {
|
|
755
|
+
const { bytes, meta } = await ctx.blobs.get(id);
|
|
756
|
+
const data = bytesToBase64(bytes);
|
|
757
|
+
ctx.cache.set(id, { data, meta });
|
|
758
|
+
return data;
|
|
759
|
+
} catch (err) {
|
|
760
|
+
if (err instanceof StoreError && err.code === "not_found") return void 0;
|
|
761
|
+
throw err;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
var BASE64_CHUNK = 32768;
|
|
765
|
+
function bytesToBase64(bytes) {
|
|
766
|
+
let binary = "";
|
|
767
|
+
for (let i = 0; i < bytes.length; i += BASE64_CHUNK) {
|
|
768
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + BASE64_CHUNK));
|
|
769
|
+
}
|
|
770
|
+
return btoa(binary);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// src/loop/pause-code.ts
|
|
774
|
+
var PAUSE_CODE_RE = /^[a-z0-9][a-z0-9_.:-]{0,63}$/;
|
|
775
|
+
function isPauseCode(x) {
|
|
776
|
+
return typeof x === "string" && PAUSE_CODE_RE.test(x);
|
|
777
|
+
}
|
|
778
|
+
var MAX_TURNS_PAUSE_CODE = "reins.max_turns";
|
|
779
|
+
function describeValue(value) {
|
|
780
|
+
if (typeof value !== "string") return value === null ? "null" : typeof value;
|
|
781
|
+
return JSON.stringify(value.length > 80 ? `${value.slice(0, 80)}\u2026` : value);
|
|
782
|
+
}
|
|
783
|
+
function resolveSocketPause(pause) {
|
|
784
|
+
const problems = [];
|
|
785
|
+
let kind;
|
|
786
|
+
if (pause.reason === "budget" || pause.reason === "host") {
|
|
787
|
+
kind = pause.reason;
|
|
788
|
+
} else {
|
|
789
|
+
kind = "host";
|
|
790
|
+
problems.push(
|
|
791
|
+
`pause.reason must be "budget" or "host", got ${describeValue(pause.reason)}; paused with reason "host" instead.`
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
let code;
|
|
795
|
+
if (isPauseCode(pause.code)) {
|
|
796
|
+
code = pause.code;
|
|
797
|
+
} else if (pause.code !== void 0) {
|
|
798
|
+
problems.push(
|
|
799
|
+
`pause.code must match ${PAUSE_CODE_RE.source}, got ${describeValue(pause.code)}; the code was dropped.`
|
|
800
|
+
);
|
|
801
|
+
}
|
|
802
|
+
const note = pause.note ?? `a socket requested a pause (${kind})`;
|
|
803
|
+
const interruption = { kind, note, ...code !== void 0 ? { code } : {} };
|
|
804
|
+
if (problems.length === 0) return { interruption };
|
|
805
|
+
return {
|
|
806
|
+
interruption,
|
|
807
|
+
error: {
|
|
808
|
+
category: "input",
|
|
809
|
+
message: problems.join(" "),
|
|
810
|
+
retryable: false,
|
|
811
|
+
detail: { phase: "onTurnEnd" }
|
|
812
|
+
}
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
function pauseDetailOf(interruptions) {
|
|
816
|
+
for (const i of interruptions) {
|
|
817
|
+
if (i.kind !== "budget" && i.kind !== "host") continue;
|
|
818
|
+
return { ...i.code !== void 0 ? { code: i.code } : {}, note: i.note };
|
|
819
|
+
}
|
|
820
|
+
return {};
|
|
821
|
+
}
|
|
822
|
+
|
|
619
823
|
// src/lowering/errors.ts
|
|
620
824
|
var LoweringError = class extends Error {
|
|
621
825
|
constructor(code, message, context = {}) {
|
|
@@ -841,6 +1045,7 @@ function isContentPart(x) {
|
|
|
841
1045
|
case "text":
|
|
842
1046
|
return typeof p.text === "string";
|
|
843
1047
|
case "image":
|
|
1048
|
+
if (p.blobId !== void 0) return isImageRef(p);
|
|
844
1049
|
return typeof p.mime === "string" && typeof p.data === "string";
|
|
845
1050
|
case "video":
|
|
846
1051
|
case "audio":
|
|
@@ -945,13 +1150,15 @@ function runStatusOf(input) {
|
|
|
945
1150
|
if (p.stopped !== void 0 && p.stopped !== true) return { state: "unknown" };
|
|
946
1151
|
if (p.awaiting !== void 0 && !(Array.isArray(p.awaiting) && p.awaiting.every(isAwaitingCall)))
|
|
947
1152
|
return { state: "unknown" };
|
|
1153
|
+
if (p.code !== void 0 && !isPauseCode(p.code)) return { state: "unknown" };
|
|
948
1154
|
return {
|
|
949
1155
|
state: "paused",
|
|
950
1156
|
seq,
|
|
951
1157
|
endedAt: at,
|
|
952
1158
|
reason: p.reason,
|
|
953
1159
|
stopped: p.stopped === true,
|
|
954
|
-
awaiting: p.awaiting
|
|
1160
|
+
awaiting: p.awaiting,
|
|
1161
|
+
...p.code !== void 0 ? { code: p.code } : {}
|
|
955
1162
|
};
|
|
956
1163
|
}
|
|
957
1164
|
case "core.run_ended": {
|
|
@@ -1448,6 +1655,10 @@ function describe(value) {
|
|
|
1448
1655
|
function isInputArray(input) {
|
|
1449
1656
|
return Array.isArray(input);
|
|
1450
1657
|
}
|
|
1658
|
+
function draftParts(draft) {
|
|
1659
|
+
if (draft.type !== "core.user_message" && draft.type !== "core.tool_result") return [];
|
|
1660
|
+
return draft.payload.content;
|
|
1661
|
+
}
|
|
1451
1662
|
async function* runLoop(cfg) {
|
|
1452
1663
|
const { sessionId, log, lowering, model } = cfg;
|
|
1453
1664
|
const registry = cfg.registry ?? createCoreRegistry();
|
|
@@ -1468,7 +1679,18 @@ async function* runLoop(cfg) {
|
|
|
1468
1679
|
...baseSystemPrompt !== void 0 ? { systemPrompt: baseSystemPrompt } : {}
|
|
1469
1680
|
});
|
|
1470
1681
|
const startedAt = now();
|
|
1471
|
-
|
|
1682
|
+
const startTimeline = await readTimeline(log, sessionId, { registry });
|
|
1683
|
+
let lastSeq = startTimeline.at(-1)?.seq ?? 0;
|
|
1684
|
+
const imageCache = /* @__PURE__ */ new Map();
|
|
1685
|
+
for (const d of input) {
|
|
1686
|
+
const problem = await checkImageRefs(draftParts(d), {
|
|
1687
|
+
sessionId,
|
|
1688
|
+
blobs: cfg.blobs,
|
|
1689
|
+
timeline: startTimeline,
|
|
1690
|
+
cache: imageCache
|
|
1691
|
+
});
|
|
1692
|
+
if (problem !== void 0) throw new RangeError(problem);
|
|
1693
|
+
}
|
|
1472
1694
|
let turns = 0;
|
|
1473
1695
|
let tokensSpent = 0;
|
|
1474
1696
|
let toolCallsTotal = 0;
|
|
@@ -1527,7 +1749,15 @@ async function* runLoop(cfg) {
|
|
|
1527
1749
|
{
|
|
1528
1750
|
type: "core.run_paused",
|
|
1529
1751
|
actor: "system",
|
|
1530
|
-
|
|
1752
|
+
// 原因码与说明随暂停落库(AdRate 建议 16):取本次中断里 budget / host 那条,口径只走 pauseDetailOf。
|
|
1753
|
+
// 用户停止时 interruptions 已换成停止那条,Socket 给的码随之不写
|
|
1754
|
+
payload: {
|
|
1755
|
+
reason,
|
|
1756
|
+
state,
|
|
1757
|
+
awaiting,
|
|
1758
|
+
...texts ? { stopped: true } : {},
|
|
1759
|
+
...pauseDetailOf(interruptions)
|
|
1760
|
+
}
|
|
1531
1761
|
}
|
|
1532
1762
|
])
|
|
1533
1763
|
];
|
|
@@ -1632,36 +1862,58 @@ async function* runLoop(cfg) {
|
|
|
1632
1862
|
thrown = { value: err };
|
|
1633
1863
|
}
|
|
1634
1864
|
if (cfg.signal?.aborted) continue;
|
|
1865
|
+
const hostFailure = async (message) => append([
|
|
1866
|
+
{
|
|
1867
|
+
type: "core.error",
|
|
1868
|
+
actor: "system",
|
|
1869
|
+
payload: { category: "host", message, retryable: true, detail: { phase: "pullInput" } }
|
|
1870
|
+
}
|
|
1871
|
+
]);
|
|
1635
1872
|
if (thrown !== void 0) {
|
|
1636
|
-
const errors = await
|
|
1637
|
-
{
|
|
1638
|
-
type: "core.error",
|
|
1639
|
-
actor: "system",
|
|
1640
|
-
payload: {
|
|
1641
|
-
category: "host",
|
|
1642
|
-
message: `pullInput threw: ${describeThrown(thrown.value)}`,
|
|
1643
|
-
retryable: true,
|
|
1644
|
-
detail: { phase: "pullInput" }
|
|
1645
|
-
}
|
|
1646
|
-
}
|
|
1647
|
-
]);
|
|
1873
|
+
const errors = await hostFailure(`pullInput threw: ${describeThrown(thrown.value)}`);
|
|
1648
1874
|
yield* errors;
|
|
1649
1875
|
timeline = [...timeline, ...errors];
|
|
1650
1876
|
} else {
|
|
1651
1877
|
let pulled;
|
|
1878
|
+
let problem;
|
|
1879
|
+
let storageFailure;
|
|
1652
1880
|
try {
|
|
1653
1881
|
pulled = checkPulledDrafts(raw, registry);
|
|
1654
1882
|
} catch (err) {
|
|
1883
|
+
pulled = [];
|
|
1884
|
+
problem = describeThrown(err);
|
|
1885
|
+
}
|
|
1886
|
+
for (const d of pulled) {
|
|
1887
|
+
if (problem !== void 0 || storageFailure !== void 0) break;
|
|
1888
|
+
try {
|
|
1889
|
+
problem = await checkImageRefs(draftParts(d), {
|
|
1890
|
+
sessionId,
|
|
1891
|
+
blobs: cfg.blobs,
|
|
1892
|
+
timeline,
|
|
1893
|
+
cache: imageCache
|
|
1894
|
+
});
|
|
1895
|
+
} catch (err) {
|
|
1896
|
+
storageFailure = { value: err };
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
if (cfg.signal?.aborted) continue;
|
|
1900
|
+
if (problem !== void 0) {
|
|
1655
1901
|
const { events, result } = await fail({
|
|
1656
1902
|
category: "input",
|
|
1657
|
-
message:
|
|
1903
|
+
message: problem,
|
|
1658
1904
|
retryable: false,
|
|
1659
1905
|
detail: { phase: "pullInput" }
|
|
1660
1906
|
});
|
|
1661
1907
|
yield* events;
|
|
1662
1908
|
return result;
|
|
1663
1909
|
}
|
|
1664
|
-
if (
|
|
1910
|
+
if (storageFailure !== void 0) {
|
|
1911
|
+
const errors = await hostFailure(
|
|
1912
|
+
`reading a blob referenced by pulled input failed: ${describeThrown(storageFailure.value)}`
|
|
1913
|
+
);
|
|
1914
|
+
yield* errors;
|
|
1915
|
+
timeline = [...timeline, ...errors];
|
|
1916
|
+
} else if (pulled.length > 0) {
|
|
1665
1917
|
const written = await append(pulled);
|
|
1666
1918
|
yield* written;
|
|
1667
1919
|
timeline = [...timeline, ...written];
|
|
@@ -1731,7 +1983,8 @@ async function* runLoop(cfg) {
|
|
|
1731
1983
|
flush,
|
|
1732
1984
|
forwardedDecisions,
|
|
1733
1985
|
spend: spendInto(ctx),
|
|
1734
|
-
batch
|
|
1986
|
+
batch,
|
|
1987
|
+
imageCache
|
|
1735
1988
|
});
|
|
1736
1989
|
toolCallsTotal += settled.executed;
|
|
1737
1990
|
if (settled.interruptions.length > 0 || isStopped(cfg.signal)) {
|
|
@@ -1746,7 +1999,7 @@ async function* runLoop(cfg) {
|
|
|
1746
1999
|
}
|
|
1747
2000
|
if (turns >= maxTurns) {
|
|
1748
2001
|
const note = `reached the per-run turn limit of ${maxTurns}`;
|
|
1749
|
-
const { events, result } = await pause("budget", [{ kind: "budget", note }]);
|
|
2002
|
+
const { events, result } = await pause("budget", [{ kind: "budget", note, code: MAX_TURNS_PAUSE_CODE }]);
|
|
1750
2003
|
yield* events;
|
|
1751
2004
|
return result;
|
|
1752
2005
|
}
|
|
@@ -1774,12 +2027,14 @@ async function* runLoop(cfg) {
|
|
|
1774
2027
|
ctx.tools = tools;
|
|
1775
2028
|
let outcome;
|
|
1776
2029
|
const modelEvents = [];
|
|
2030
|
+
let hydrated;
|
|
1777
2031
|
for (let attempt = 1; ; attempt++) {
|
|
1778
2032
|
let failure;
|
|
1779
2033
|
let attemptOutput = 0;
|
|
1780
2034
|
try {
|
|
2035
|
+
hydrated ??= await hydrateImageRefs(visible, { blobs: cfg.blobs, cache: imageCache });
|
|
1781
2036
|
const request = lowering.toRequest({
|
|
1782
|
-
events:
|
|
2037
|
+
events: hydrated,
|
|
1783
2038
|
tools: tools.map((t) => deferredToolSpecOf(t, deferredTools.has(t.name))),
|
|
1784
2039
|
model,
|
|
1785
2040
|
...systemPrompt !== void 0 ? { systemPrompt } : {}
|
|
@@ -1888,7 +2143,8 @@ async function* runLoop(cfg) {
|
|
|
1888
2143
|
flush,
|
|
1889
2144
|
forwardedDecisions,
|
|
1890
2145
|
spend: spendInto(ctx),
|
|
1891
|
-
batch
|
|
2146
|
+
batch,
|
|
2147
|
+
imageCache
|
|
1892
2148
|
});
|
|
1893
2149
|
toolCallsTotal += settled.executed;
|
|
1894
2150
|
ctx.budget.toolCalls = toolCallsTotal;
|
|
@@ -1930,9 +2186,11 @@ async function* runLoop(cfg) {
|
|
|
1930
2186
|
return { status: "done", sessionId, lastSeq };
|
|
1931
2187
|
}
|
|
1932
2188
|
if ("pause" in decision) {
|
|
1933
|
-
const
|
|
1934
|
-
|
|
1935
|
-
|
|
2189
|
+
const { interruption, error } = resolveSocketPause(decision.pause);
|
|
2190
|
+
if (error !== void 0) {
|
|
2191
|
+
yield* await append([{ type: "core.error", actor: "system", payload: error }]);
|
|
2192
|
+
}
|
|
2193
|
+
const { events, result } = await pause(interruption.kind, [interruption]);
|
|
1936
2194
|
yield* events;
|
|
1937
2195
|
return result;
|
|
1938
2196
|
}
|
|
@@ -1999,7 +2257,7 @@ function withResultTrust(input, tools) {
|
|
|
1999
2257
|
return { ...input, ...withTrust(toolResultTrust(tool, payload?.isError === true)) };
|
|
2000
2258
|
}
|
|
2001
2259
|
async function* executeToolCalls(ctx, calls, timeline, deps) {
|
|
2002
|
-
const { cfg, sockets, flush, forwardedDecisions, spend, batch } = deps;
|
|
2260
|
+
const { cfg, sockets, flush, forwardedDecisions, spend, batch, imageCache } = deps;
|
|
2003
2261
|
const append = async (drafts) => {
|
|
2004
2262
|
const events = await deps.append(drafts);
|
|
2005
2263
|
for (const e of events) if (e.type === "core.tool_result") batch.results.push(e);
|
|
@@ -2148,11 +2406,53 @@ async function* executeToolCalls(ctx, calls, timeline, deps) {
|
|
|
2148
2406
|
const replaced = await s.afterTool?.(ctx, call, result);
|
|
2149
2407
|
if (replaced) result = replaced;
|
|
2150
2408
|
}
|
|
2409
|
+
const refs = await checkToolResultRefs(result.payload.content, {
|
|
2410
|
+
sessionId: ctx.session.id,
|
|
2411
|
+
blobs: ctx.blobs,
|
|
2412
|
+
timeline,
|
|
2413
|
+
cache: imageCache
|
|
2414
|
+
});
|
|
2415
|
+
if (refs.content !== result.payload.content)
|
|
2416
|
+
result = { ...result, payload: { ...result.payload, content: refs.content } };
|
|
2417
|
+
for (const failure of refs.storageFailures) ctx.emit(failure);
|
|
2151
2418
|
yield* await settle(result);
|
|
2152
2419
|
if (cfg.signal?.aborted) break;
|
|
2153
2420
|
}
|
|
2154
2421
|
return summary;
|
|
2155
2422
|
}
|
|
2423
|
+
async function checkToolResultRefs(parts, ctx) {
|
|
2424
|
+
const storageFailures = [];
|
|
2425
|
+
let changed = false;
|
|
2426
|
+
const content = [];
|
|
2427
|
+
for (const p of parts) {
|
|
2428
|
+
if (!isImageRef(p)) {
|
|
2429
|
+
content.push(p);
|
|
2430
|
+
continue;
|
|
2431
|
+
}
|
|
2432
|
+
let text;
|
|
2433
|
+
try {
|
|
2434
|
+
text = await checkImageRefs([p], ctx);
|
|
2435
|
+
} catch (err) {
|
|
2436
|
+
text = `Could not read blob ${p.blobId}: storage error`;
|
|
2437
|
+
storageFailures.push({
|
|
2438
|
+
type: "core.error",
|
|
2439
|
+
actor: "system",
|
|
2440
|
+
payload: {
|
|
2441
|
+
category: "host",
|
|
2442
|
+
message: `reading blob ${p.blobId} referenced by a tool result failed: ${describeThrown(err)}`,
|
|
2443
|
+
retryable: true,
|
|
2444
|
+
detail: { phase: "toolResult" }
|
|
2445
|
+
}
|
|
2446
|
+
});
|
|
2447
|
+
}
|
|
2448
|
+
if (text === void 0) content.push(p);
|
|
2449
|
+
else {
|
|
2450
|
+
changed = true;
|
|
2451
|
+
content.push({ type: "text", text });
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
return { content: changed ? content : parts, storageFailures };
|
|
2455
|
+
}
|
|
2156
2456
|
|
|
2157
2457
|
// src/lowering/tool-arguments.ts
|
|
2158
2458
|
function unwrapToolArguments(parsed) {
|
|
@@ -2279,18 +2579,6 @@ function needsUntrustedMark(e) {
|
|
|
2279
2579
|
return e.trust === "untrusted";
|
|
2280
2580
|
}
|
|
2281
2581
|
|
|
2282
|
-
// src/store/errors.ts
|
|
2283
|
-
var StoreError = class extends Error {
|
|
2284
|
-
constructor(code, message, context = {}) {
|
|
2285
|
-
super(`[${code}] ${message}`);
|
|
2286
|
-
this.code = code;
|
|
2287
|
-
this.context = context;
|
|
2288
|
-
this.name = "StoreError";
|
|
2289
|
-
}
|
|
2290
|
-
code;
|
|
2291
|
-
context;
|
|
2292
|
-
};
|
|
2293
|
-
|
|
2294
2582
|
// src/store/in-memory.ts
|
|
2295
2583
|
var InMemoryEventLog = class {
|
|
2296
2584
|
sessions = /* @__PURE__ */ new Map();
|
|
@@ -2426,7 +2714,7 @@ function memoryStore() {
|
|
|
2426
2714
|
}
|
|
2427
2715
|
|
|
2428
2716
|
// src/index.ts
|
|
2429
|
-
var REINS_VERSION = "0.
|
|
2717
|
+
var REINS_VERSION = "0.16.0";
|
|
2430
2718
|
export {
|
|
2431
2719
|
BUILTIN_APPROVAL_POLICY,
|
|
2432
2720
|
CORE_SCHEMAS,
|
|
@@ -2439,6 +2727,7 @@ export {
|
|
|
2439
2727
|
DEFAULT_RETRY_MAX_DELAY_MS,
|
|
2440
2728
|
DEFAULT_TRUST,
|
|
2441
2729
|
EventSchemaRegistry,
|
|
2730
|
+
IMAGE_REF_ID_MAX,
|
|
2442
2731
|
INPUT_DRAFT_TYPES,
|
|
2443
2732
|
INTERRUPTED_IN_PROGRESS_TEXT,
|
|
2444
2733
|
INTERRUPTED_NOT_EXECUTED_TEXT,
|
|
@@ -2447,7 +2736,9 @@ export {
|
|
|
2447
2736
|
InMemoryMemoryStore,
|
|
2448
2737
|
InMemoryRunLease,
|
|
2449
2738
|
LoweringError,
|
|
2739
|
+
MAX_TURNS_PAUSE_CODE,
|
|
2450
2740
|
OUTCOME_UNKNOWN_TEXT,
|
|
2741
|
+
PAUSE_CODE_RE,
|
|
2451
2742
|
REINS_VERSION,
|
|
2452
2743
|
RUN_MARKER_TYPES,
|
|
2453
2744
|
RunStateError,
|
|
@@ -2462,7 +2753,10 @@ export {
|
|
|
2462
2753
|
assertRunStateShape,
|
|
2463
2754
|
awaitingOf,
|
|
2464
2755
|
backoffDelayMs,
|
|
2756
|
+
blobUsableBy,
|
|
2465
2757
|
budgetTruncate,
|
|
2758
|
+
bytesToBase64,
|
|
2759
|
+
checkImageRefs,
|
|
2466
2760
|
computeConfigHash,
|
|
2467
2761
|
computeModelDigest,
|
|
2468
2762
|
computePendingDigest,
|
|
@@ -2488,12 +2782,17 @@ export {
|
|
|
2488
2782
|
estimateTotal,
|
|
2489
2783
|
foldCompactions,
|
|
2490
2784
|
foldedToolResults,
|
|
2785
|
+
foldedUserMedia,
|
|
2491
2786
|
forkSession,
|
|
2787
|
+
hydrateImageRefs,
|
|
2788
|
+
imageRefNotFound,
|
|
2492
2789
|
inputDrafts,
|
|
2493
2790
|
isClientTool,
|
|
2494
2791
|
isCompaction,
|
|
2495
2792
|
isContentPart,
|
|
2793
|
+
isImageRef,
|
|
2496
2794
|
isModelOutput,
|
|
2795
|
+
isPauseCode,
|
|
2497
2796
|
isPinNote,
|
|
2498
2797
|
isReplayable,
|
|
2499
2798
|
isStopReason,
|
|
@@ -2506,10 +2805,12 @@ export {
|
|
|
2506
2805
|
lossesOf,
|
|
2507
2806
|
markUntrusted,
|
|
2508
2807
|
markUntrustedText,
|
|
2808
|
+
mediaRefsOf,
|
|
2509
2809
|
memoryStore,
|
|
2510
2810
|
needsUntrustedMark,
|
|
2511
2811
|
normalizeToolOutput,
|
|
2512
2812
|
outcomeUnknownText,
|
|
2813
|
+
pauseDetailOf,
|
|
2513
2814
|
pausedStateOf,
|
|
2514
2815
|
pendingToolCalls,
|
|
2515
2816
|
project,
|
|
@@ -2517,11 +2818,13 @@ export {
|
|
|
2517
2818
|
readTimeline,
|
|
2518
2819
|
reinjectPins,
|
|
2519
2820
|
renderFoldedToolResults,
|
|
2821
|
+
renderFoldedUserMedia,
|
|
2520
2822
|
renderToolChangeNote,
|
|
2521
2823
|
renderToolReference,
|
|
2522
2824
|
replayTurns,
|
|
2523
2825
|
resolveRetry,
|
|
2524
2826
|
resolveSocketContributions,
|
|
2827
|
+
resolveSocketPause,
|
|
2525
2828
|
roughTokenEstimate,
|
|
2526
2829
|
runLoop,
|
|
2527
2830
|
runStatusOf,
|