@reinsjs/core 0.13.0 → 0.14.1
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 +18 -0
- package/README.md +1 -0
- package/dist/{chunk-3QMUURRG.js → chunk-DFXHUJV4.js} +2 -1
- package/dist/{chunk-3QMUURRG.js.map → chunk-DFXHUJV4.js.map} +1 -1
- package/dist/{core-h3S4jFh-.d.ts → core-BhmO9N-j.d.ts} +51 -1
- package/dist/index.d.ts +112 -5
- package/dist/index.js +297 -77
- 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/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
createEvent,
|
|
9
9
|
renderToolReference,
|
|
10
10
|
uuidv7
|
|
11
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-DFXHUJV4.js";
|
|
12
12
|
|
|
13
13
|
// src/events/core.ts
|
|
14
14
|
function contextTokensOf(usage) {
|
|
@@ -35,7 +35,8 @@ var DEFAULT_MODEL_INVISIBLE_TYPES = /* @__PURE__ */ new Set([
|
|
|
35
35
|
"core.handoff",
|
|
36
36
|
"core.error",
|
|
37
37
|
"core.tools_bound",
|
|
38
|
-
"core.tool_started"
|
|
38
|
+
"core.tool_started",
|
|
39
|
+
"core.run_ended"
|
|
39
40
|
]);
|
|
40
41
|
function visibilityFilter(opts = {}) {
|
|
41
42
|
const invisible = opts.invisibleTypes ? new Set(opts.invisibleTypes) : DEFAULT_MODEL_INVISIBLE_TYPES;
|
|
@@ -813,6 +814,159 @@ async function readTimeline(log, sessionId, opts) {
|
|
|
813
814
|
return out;
|
|
814
815
|
}
|
|
815
816
|
|
|
817
|
+
// src/loop/tools.ts
|
|
818
|
+
function toolResultTrust(tool, isError) {
|
|
819
|
+
if (!tool || tool.resultTrust === void 0 || isError) return void 0;
|
|
820
|
+
return tool.resultTrust;
|
|
821
|
+
}
|
|
822
|
+
function isClientTool(tool) {
|
|
823
|
+
return !tool.execute || tool.side === "client";
|
|
824
|
+
}
|
|
825
|
+
function defineTool(tool) {
|
|
826
|
+
return tool;
|
|
827
|
+
}
|
|
828
|
+
function toolSpecOf(tool) {
|
|
829
|
+
return { name: tool.name, description: tool.description, inputSchema: tool.inputSchema };
|
|
830
|
+
}
|
|
831
|
+
function deferredToolSpecOf(tool, deferred) {
|
|
832
|
+
return deferred ? { ...toolSpecOf(tool), deferLoading: true } : toolSpecOf(tool);
|
|
833
|
+
}
|
|
834
|
+
function isDuration(x) {
|
|
835
|
+
return x === void 0 || typeof x === "number" && Number.isFinite(x) && x > 0;
|
|
836
|
+
}
|
|
837
|
+
function isContentPart(x) {
|
|
838
|
+
if (typeof x !== "object" || x === null) return false;
|
|
839
|
+
const p = x;
|
|
840
|
+
switch (p.type) {
|
|
841
|
+
case "text":
|
|
842
|
+
return typeof p.text === "string";
|
|
843
|
+
case "image":
|
|
844
|
+
return typeof p.mime === "string" && typeof p.data === "string";
|
|
845
|
+
case "video":
|
|
846
|
+
case "audio":
|
|
847
|
+
return typeof p.mime === "string" && p.mime.startsWith(`${p.type}/`) && typeof p.data === "string" && isDuration(p.durationSec);
|
|
848
|
+
case "tool_reference":
|
|
849
|
+
return typeof p.name === "string" && typeof p.description === "string" && typeof p.inputSchema === "object" && p.inputSchema !== null && !Array.isArray(p.inputSchema);
|
|
850
|
+
default:
|
|
851
|
+
return false;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
function isToolResult(x) {
|
|
855
|
+
if (typeof x !== "object" || x === null) return false;
|
|
856
|
+
const r = x;
|
|
857
|
+
return Array.isArray(r.content) && r.content.every(isContentPart) && (r.isError === void 0 || typeof r.isError === "boolean");
|
|
858
|
+
}
|
|
859
|
+
function normalizeToolOutput(output) {
|
|
860
|
+
if (typeof output === "string") return { content: [{ type: "text", text: output }] };
|
|
861
|
+
if (output === void 0 || output === null) return { content: [{ type: "text", text: "" }] };
|
|
862
|
+
if (Array.isArray(output) && output.every(isContentPart)) return { content: output };
|
|
863
|
+
if (isToolResult(output)) return output;
|
|
864
|
+
const json = JSON.stringify(output);
|
|
865
|
+
return { content: [{ type: "text", text: typeof json === "string" ? json : String(output) }] };
|
|
866
|
+
}
|
|
867
|
+
function errorMessageOf(err) {
|
|
868
|
+
if (err instanceof Error) return err.message;
|
|
869
|
+
return typeof err === "string" ? err : JSON.stringify(err) ?? String(err);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
// src/loop/run-status.ts
|
|
873
|
+
var RUN_MARKER_TYPES = ["core.tools_bound", "core.run_paused", "core.run_ended"];
|
|
874
|
+
var MARKER_SET = new Set(RUN_MARKER_TYPES);
|
|
875
|
+
function lastRunMarker(events) {
|
|
876
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
877
|
+
const e = events[i];
|
|
878
|
+
if (e !== void 0 && MARKER_SET.has(e.type)) return e;
|
|
879
|
+
}
|
|
880
|
+
return void 0;
|
|
881
|
+
}
|
|
882
|
+
function awaitingOf(input) {
|
|
883
|
+
const requestedAt = /* @__PURE__ */ new Map();
|
|
884
|
+
const decided = /* @__PURE__ */ new Set();
|
|
885
|
+
for (const raw of input.timeline) {
|
|
886
|
+
const id = raw.payload?.toolCallId;
|
|
887
|
+
if (typeof id !== "string") continue;
|
|
888
|
+
if (raw.type === "core.approval_request" && !requestedAt.has(id)) requestedAt.set(id, raw.at);
|
|
889
|
+
if (raw.type === "core.approval_decision") decided.add(id);
|
|
890
|
+
}
|
|
891
|
+
const awaiting = [];
|
|
892
|
+
for (const call of input.pending) {
|
|
893
|
+
const { toolCallId, name } = call.payload;
|
|
894
|
+
const at = requestedAt.get(toolCallId);
|
|
895
|
+
if (at !== void 0 && !decided.has(toolCallId)) {
|
|
896
|
+
awaiting.push({ kind: "approval", toolCallId, name, requestedAt: at });
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
const tool = input.tools.find((t) => t.name === name);
|
|
900
|
+
if (tool !== void 0 && isClientTool(tool)) {
|
|
901
|
+
awaiting.push({ kind: "client_tool", toolCallId, name });
|
|
902
|
+
continue;
|
|
903
|
+
}
|
|
904
|
+
const sub = input.interruptions.find((i) => i.kind === "subagent" && i.toolCallId === toolCallId);
|
|
905
|
+
if (sub?.kind === "subagent") {
|
|
906
|
+
awaiting.push({
|
|
907
|
+
kind: "subagent",
|
|
908
|
+
toolCallId,
|
|
909
|
+
name,
|
|
910
|
+
childSessionId: sub.childSessionId,
|
|
911
|
+
reason: sub.reason
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
return awaiting;
|
|
916
|
+
}
|
|
917
|
+
var PAUSE_REASONS = /* @__PURE__ */ new Set(["approval", "budget", "host"]);
|
|
918
|
+
var isNonEmptyString = (x) => typeof x === "string" && x !== "";
|
|
919
|
+
function isAwaitingCall(x) {
|
|
920
|
+
if (typeof x !== "object" || x === null) return false;
|
|
921
|
+
const a = x;
|
|
922
|
+
if (!isNonEmptyString(a.toolCallId) || typeof a.name !== "string") return false;
|
|
923
|
+
if (a.kind === "approval") return typeof a.requestedAt === "number" && Number.isFinite(a.requestedAt);
|
|
924
|
+
if (a.kind === "client_tool") return true;
|
|
925
|
+
if (a.kind === "subagent")
|
|
926
|
+
return isNonEmptyString(a.childSessionId) && typeof a.reason === "string" && PAUSE_REASONS.has(a.reason);
|
|
927
|
+
return false;
|
|
928
|
+
}
|
|
929
|
+
function runStatusOf(input) {
|
|
930
|
+
if (input.held) return { state: "running" };
|
|
931
|
+
if (input.lastMarker === void 0) return { state: "idle" };
|
|
932
|
+
let marker;
|
|
933
|
+
try {
|
|
934
|
+
marker = (input.registry ?? createCoreRegistry()).read(input.lastMarker);
|
|
935
|
+
} catch {
|
|
936
|
+
return { state: "unknown" };
|
|
937
|
+
}
|
|
938
|
+
const p = typeof marker.payload === "object" && marker.payload !== null ? marker.payload : {};
|
|
939
|
+
const { seq, at } = marker;
|
|
940
|
+
switch (marker.type) {
|
|
941
|
+
case "core.tools_bound":
|
|
942
|
+
return p.marksEnd === true ? { state: "interrupted", seq, startedAt: at } : { state: "unknown" };
|
|
943
|
+
case "core.run_paused": {
|
|
944
|
+
if (typeof p.reason !== "string" || !PAUSE_REASONS.has(p.reason)) return { state: "unknown" };
|
|
945
|
+
if (p.stopped !== void 0 && p.stopped !== true) return { state: "unknown" };
|
|
946
|
+
if (p.awaiting !== void 0 && !(Array.isArray(p.awaiting) && p.awaiting.every(isAwaitingCall)))
|
|
947
|
+
return { state: "unknown" };
|
|
948
|
+
return {
|
|
949
|
+
state: "paused",
|
|
950
|
+
seq,
|
|
951
|
+
endedAt: at,
|
|
952
|
+
reason: p.reason,
|
|
953
|
+
stopped: p.stopped === true,
|
|
954
|
+
awaiting: p.awaiting
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
case "core.run_ended": {
|
|
958
|
+
if (p.status === "done") return { state: "done", seq, endedAt: at };
|
|
959
|
+
if (p.status === "handoff" && isNonEmptyString(p.toSessionId))
|
|
960
|
+
return { state: "handoff", seq, endedAt: at, toSessionId: p.toSessionId };
|
|
961
|
+
if (p.status === "error" && typeof p.category === "string")
|
|
962
|
+
return { state: "error", seq, endedAt: at, category: p.category };
|
|
963
|
+
return { state: "unknown" };
|
|
964
|
+
}
|
|
965
|
+
default:
|
|
966
|
+
return { state: "unknown" };
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
816
970
|
// src/loop/state.ts
|
|
817
971
|
function pendingToolCalls(timeline) {
|
|
818
972
|
const answered = /* @__PURE__ */ new Set();
|
|
@@ -1058,58 +1212,6 @@ function isSubagentPause(value) {
|
|
|
1058
1212
|
return typeof value === "object" && value !== null && value[SUBAGENT_PAUSE] === true;
|
|
1059
1213
|
}
|
|
1060
1214
|
|
|
1061
|
-
// src/loop/tools.ts
|
|
1062
|
-
function toolResultTrust(tool, isError) {
|
|
1063
|
-
if (!tool || tool.resultTrust === void 0 || isError) return void 0;
|
|
1064
|
-
return tool.resultTrust;
|
|
1065
|
-
}
|
|
1066
|
-
function defineTool(tool) {
|
|
1067
|
-
return tool;
|
|
1068
|
-
}
|
|
1069
|
-
function toolSpecOf(tool) {
|
|
1070
|
-
return { name: tool.name, description: tool.description, inputSchema: tool.inputSchema };
|
|
1071
|
-
}
|
|
1072
|
-
function deferredToolSpecOf(tool, deferred) {
|
|
1073
|
-
return deferred ? { ...toolSpecOf(tool), deferLoading: true } : toolSpecOf(tool);
|
|
1074
|
-
}
|
|
1075
|
-
function isDuration(x) {
|
|
1076
|
-
return x === void 0 || typeof x === "number" && Number.isFinite(x) && x > 0;
|
|
1077
|
-
}
|
|
1078
|
-
function isContentPart(x) {
|
|
1079
|
-
if (typeof x !== "object" || x === null) return false;
|
|
1080
|
-
const p = x;
|
|
1081
|
-
switch (p.type) {
|
|
1082
|
-
case "text":
|
|
1083
|
-
return typeof p.text === "string";
|
|
1084
|
-
case "image":
|
|
1085
|
-
return typeof p.mime === "string" && typeof p.data === "string";
|
|
1086
|
-
case "video":
|
|
1087
|
-
case "audio":
|
|
1088
|
-
return typeof p.mime === "string" && p.mime.startsWith(`${p.type}/`) && typeof p.data === "string" && isDuration(p.durationSec);
|
|
1089
|
-
case "tool_reference":
|
|
1090
|
-
return typeof p.name === "string" && typeof p.description === "string" && typeof p.inputSchema === "object" && p.inputSchema !== null && !Array.isArray(p.inputSchema);
|
|
1091
|
-
default:
|
|
1092
|
-
return false;
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
function isToolResult(x) {
|
|
1096
|
-
if (typeof x !== "object" || x === null) return false;
|
|
1097
|
-
const r = x;
|
|
1098
|
-
return Array.isArray(r.content) && r.content.every(isContentPart) && (r.isError === void 0 || typeof r.isError === "boolean");
|
|
1099
|
-
}
|
|
1100
|
-
function normalizeToolOutput(output) {
|
|
1101
|
-
if (typeof output === "string") return { content: [{ type: "text", text: output }] };
|
|
1102
|
-
if (output === void 0 || output === null) return { content: [{ type: "text", text: "" }] };
|
|
1103
|
-
if (Array.isArray(output) && output.every(isContentPart)) return { content: output };
|
|
1104
|
-
if (isToolResult(output)) return output;
|
|
1105
|
-
const json = JSON.stringify(output);
|
|
1106
|
-
return { content: [{ type: "text", text: typeof json === "string" ? json : String(output) }] };
|
|
1107
|
-
}
|
|
1108
|
-
function errorMessageOf(err) {
|
|
1109
|
-
if (err instanceof Error) return err.message;
|
|
1110
|
-
return typeof err === "string" ? err : JSON.stringify(err) ?? String(err);
|
|
1111
|
-
}
|
|
1112
|
-
|
|
1113
1215
|
// src/loop/tools-bound.ts
|
|
1114
1216
|
function lastToolsBound(timeline) {
|
|
1115
1217
|
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
@@ -1126,26 +1228,117 @@ function diffToolNames(previous, next) {
|
|
|
1126
1228
|
if (added.length === 0 && removed.length === 0) return void 0;
|
|
1127
1229
|
return { added, removed };
|
|
1128
1230
|
}
|
|
1129
|
-
|
|
1231
|
+
var TOOL_CHANGE_LIST_MAX = 20;
|
|
1232
|
+
function renderToolChangeNote(diff, tiers = []) {
|
|
1130
1233
|
const lines = ["Your available tools changed since the previous run."];
|
|
1131
|
-
|
|
1132
|
-
if (
|
|
1133
|
-
lines.push(`
|
|
1234
|
+
const { added, removed } = diff;
|
|
1235
|
+
if (added.length > TOOL_CHANGE_LIST_MAX)
|
|
1236
|
+
lines.push(`Added ${added.length} tools, including ${sampleNames(added, tiers)}.`);
|
|
1237
|
+
else if (added.length > 0) lines.push(`Added: ${added.join(", ")}.`);
|
|
1238
|
+
if (removed.length > TOOL_CHANGE_LIST_MAX)
|
|
1239
|
+
lines.push(
|
|
1240
|
+
`Removed ${removed.length} tools (no longer callable, even if earlier turns used them), including ${sampleNames(removed, tiers)}.`
|
|
1241
|
+
);
|
|
1242
|
+
else if (removed.length > 0)
|
|
1243
|
+
lines.push(`Removed (no longer callable, even if earlier turns used them): ${removed.join(", ")}.`);
|
|
1134
1244
|
return lines.join(" ");
|
|
1135
1245
|
}
|
|
1246
|
+
function sampleNames(names, tiers) {
|
|
1247
|
+
const rank = (n) => {
|
|
1248
|
+
const i = tiers.findIndex((t) => t.has(n));
|
|
1249
|
+
return i < 0 ? tiers.length : i;
|
|
1250
|
+
};
|
|
1251
|
+
const picked = names.map((n, i) => ({ n, i, r: rank(n) })).sort((a, b) => a.r - b.r || a.i - b.i).slice(0, TOOL_CHANGE_LIST_MAX).map((x) => x.n);
|
|
1252
|
+
return `${picked.join(", ")}, and ${names.length - picked.length} more`;
|
|
1253
|
+
}
|
|
1254
|
+
function toolMentionTiers(timeline, candidates) {
|
|
1255
|
+
const wanted = new Set(candidates);
|
|
1256
|
+
const called = /* @__PURE__ */ new Set();
|
|
1257
|
+
const mentioned = /* @__PURE__ */ new Set();
|
|
1258
|
+
const call = (name) => {
|
|
1259
|
+
if (wanted.has(name)) called.add(name);
|
|
1260
|
+
};
|
|
1261
|
+
const scan = (text) => {
|
|
1262
|
+
TOOL_WORD_RE.lastIndex = 0;
|
|
1263
|
+
for (let m = TOOL_WORD_RE.exec(text); m !== null; m = TOOL_WORD_RE.exec(text))
|
|
1264
|
+
if (wanted.has(m[0])) mentioned.add(m[0]);
|
|
1265
|
+
};
|
|
1266
|
+
const scanValue = (v) => {
|
|
1267
|
+
if (typeof v === "string") scan(v);
|
|
1268
|
+
else if (Array.isArray(v)) for (const x of v) scanValue(x);
|
|
1269
|
+
else if (typeof v === "object" && v !== null) for (const x of Object.values(v)) scanValue(x);
|
|
1270
|
+
};
|
|
1271
|
+
const scanParts = (parts) => {
|
|
1272
|
+
for (const part of parts) {
|
|
1273
|
+
if (part.type === "text") scan(part.text);
|
|
1274
|
+
else if (part.type === "tool_reference") call(part.name);
|
|
1275
|
+
}
|
|
1276
|
+
};
|
|
1277
|
+
for (const e of timeline) {
|
|
1278
|
+
switch (e.type) {
|
|
1279
|
+
case "core.user_message":
|
|
1280
|
+
scanParts(e.payload.content);
|
|
1281
|
+
break;
|
|
1282
|
+
case "core.model_text":
|
|
1283
|
+
scan(e.payload.text);
|
|
1284
|
+
break;
|
|
1285
|
+
case "core.model_thinking":
|
|
1286
|
+
scan(e.payload.text);
|
|
1287
|
+
break;
|
|
1288
|
+
case "core.compaction":
|
|
1289
|
+
scan(e.payload.summary);
|
|
1290
|
+
break;
|
|
1291
|
+
case "core.system_note": {
|
|
1292
|
+
const p = e.payload;
|
|
1293
|
+
if (p.meta?.toolsChanged === void 0) scan(p.text);
|
|
1294
|
+
break;
|
|
1295
|
+
}
|
|
1296
|
+
case "core.tool_call": {
|
|
1297
|
+
const p = e.payload;
|
|
1298
|
+
call(p.name);
|
|
1299
|
+
if (typeof p.args === "string") {
|
|
1300
|
+
try {
|
|
1301
|
+
scanValue(JSON.parse(p.args));
|
|
1302
|
+
} catch {
|
|
1303
|
+
scan(p.args);
|
|
1304
|
+
}
|
|
1305
|
+
} else scanValue(p.args);
|
|
1306
|
+
break;
|
|
1307
|
+
}
|
|
1308
|
+
case "core.tool_result": {
|
|
1309
|
+
const p = e.payload;
|
|
1310
|
+
call(p.name);
|
|
1311
|
+
scanParts(p.content);
|
|
1312
|
+
break;
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
for (const n of called) mentioned.delete(n);
|
|
1317
|
+
return [called, mentioned];
|
|
1318
|
+
}
|
|
1319
|
+
var TOOL_WORD_RE = /[A-Za-z0-9_-]+/g;
|
|
1136
1320
|
function toolsBoundDrafts(input) {
|
|
1137
1321
|
const toolNames = [...input.toolNames].sort();
|
|
1138
1322
|
const drafts = [
|
|
1139
|
-
{
|
|
1323
|
+
{
|
|
1324
|
+
type: "core.tools_bound",
|
|
1325
|
+
actor: "system",
|
|
1326
|
+
payload: {
|
|
1327
|
+
toolNames,
|
|
1328
|
+
configHash: input.configHash,
|
|
1329
|
+
...input.marksEnd ? { marksEnd: true } : {}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1140
1332
|
];
|
|
1141
1333
|
const previous = lastToolsBound(input.timeline);
|
|
1142
1334
|
if (!input.announce || previous === void 0) return drafts;
|
|
1143
1335
|
const diff = diffToolNames(previous.payload.toolNames, toolNames);
|
|
1144
1336
|
if (!diff) return drafts;
|
|
1337
|
+
const tiers = diff.added.length > TOOL_CHANGE_LIST_MAX || diff.removed.length > TOOL_CHANGE_LIST_MAX ? toolMentionTiers(input.timeline, [...diff.added, ...diff.removed]) : [];
|
|
1145
1338
|
drafts.push({
|
|
1146
1339
|
type: "core.system_note",
|
|
1147
1340
|
actor: "host",
|
|
1148
|
-
payload: { kind: "host", text: renderToolChangeNote(diff), meta: { toolsChanged: diff } }
|
|
1341
|
+
payload: { kind: "host", text: renderToolChangeNote(diff, tiers), meta: { toolsChanged: diff } }
|
|
1149
1342
|
});
|
|
1150
1343
|
return drafts;
|
|
1151
1344
|
}
|
|
@@ -1316,7 +1509,9 @@ async function* runLoop(cfg) {
|
|
|
1316
1509
|
const reason = texts ? "host" : requested;
|
|
1317
1510
|
const interruptions = texts ? [{ kind: "host", note: texts.note }] : requestedInterruptions;
|
|
1318
1511
|
const settled = texts ? await settleStopped(texts) : [];
|
|
1319
|
-
const
|
|
1512
|
+
const timeline = await readTimeline(log, sessionId, { registry });
|
|
1513
|
+
const pending = pendingToolCalls(timeline);
|
|
1514
|
+
const awaiting = awaitingOf({ timeline, pending, tools: baseTools, interruptions });
|
|
1320
1515
|
const state = await serializeRunState({
|
|
1321
1516
|
sessionId,
|
|
1322
1517
|
lastSeq: lastSeq + 1,
|
|
@@ -1328,7 +1523,13 @@ async function* runLoop(cfg) {
|
|
|
1328
1523
|
});
|
|
1329
1524
|
const events = [
|
|
1330
1525
|
...settled,
|
|
1331
|
-
...await append([
|
|
1526
|
+
...await append([
|
|
1527
|
+
{
|
|
1528
|
+
type: "core.run_paused",
|
|
1529
|
+
actor: "system",
|
|
1530
|
+
payload: { reason, state, awaiting, ...texts ? { stopped: true } : {} }
|
|
1531
|
+
}
|
|
1532
|
+
])
|
|
1332
1533
|
];
|
|
1333
1534
|
return { events, result: { status: "paused", sessionId, lastSeq, reason, interruptions, state } };
|
|
1334
1535
|
};
|
|
@@ -1337,7 +1538,10 @@ async function* runLoop(cfg) {
|
|
|
1337
1538
|
ctx.budget.tokensSpent = tokensSpent;
|
|
1338
1539
|
};
|
|
1339
1540
|
const fail = async (payload) => {
|
|
1340
|
-
const events = await append([
|
|
1541
|
+
const events = await append([
|
|
1542
|
+
{ type: "core.error", actor: "system", payload },
|
|
1543
|
+
{ type: "core.run_ended", actor: "system", payload: { status: "error", category: payload.category } }
|
|
1544
|
+
]);
|
|
1341
1545
|
const error = events[0];
|
|
1342
1546
|
return { events, result: { status: "error", sessionId, lastSeq, error } };
|
|
1343
1547
|
};
|
|
@@ -1347,7 +1551,8 @@ async function* runLoop(cfg) {
|
|
|
1347
1551
|
const forwardedDecisions = (cfg.decisions ?? []).filter(
|
|
1348
1552
|
(d) => d.sessionId !== void 0 && d.sessionId !== sessionId
|
|
1349
1553
|
);
|
|
1350
|
-
|
|
1554
|
+
const resuming = cfg.resume !== void 0 || ownDecisions.length > 0;
|
|
1555
|
+
if (resuming) {
|
|
1351
1556
|
const timeline = await readTimeline(log, sessionId, { registry });
|
|
1352
1557
|
if (cfg.resume !== void 0) {
|
|
1353
1558
|
await validateResume({
|
|
@@ -1374,6 +1579,20 @@ async function* runLoop(cfg) {
|
|
|
1374
1579
|
);
|
|
1375
1580
|
}
|
|
1376
1581
|
}
|
|
1582
|
+
}
|
|
1583
|
+
if (!cfg.signal?.aborted) {
|
|
1584
|
+
yield* await append(
|
|
1585
|
+
toolsBoundDrafts({
|
|
1586
|
+
timeline: await readTimeline(log, sessionId, { registry }),
|
|
1587
|
+
toolNames: baseTools.map((t) => t.name),
|
|
1588
|
+
configHash,
|
|
1589
|
+
announce: cfg.announceToolChanges ?? true,
|
|
1590
|
+
marksEnd: true
|
|
1591
|
+
})
|
|
1592
|
+
);
|
|
1593
|
+
}
|
|
1594
|
+
if (resuming) {
|
|
1595
|
+
const decisions = ownDecisions;
|
|
1377
1596
|
if (cfg.resume !== void 0) {
|
|
1378
1597
|
const by = decisions[0]?.by;
|
|
1379
1598
|
yield* await append([
|
|
@@ -1393,16 +1612,6 @@ async function* runLoop(cfg) {
|
|
|
1393
1612
|
}))
|
|
1394
1613
|
);
|
|
1395
1614
|
}
|
|
1396
|
-
if (!cfg.signal?.aborted) {
|
|
1397
|
-
yield* await append(
|
|
1398
|
-
toolsBoundDrafts({
|
|
1399
|
-
timeline: await readTimeline(log, sessionId, { registry }),
|
|
1400
|
-
toolNames: baseTools.map((t) => t.name),
|
|
1401
|
-
configHash,
|
|
1402
|
-
announce: cfg.announceToolChanges ?? true
|
|
1403
|
-
})
|
|
1404
|
-
);
|
|
1405
|
-
}
|
|
1406
1615
|
yield* await append(input.map((d) => withResultTrust(d, baseTools)));
|
|
1407
1616
|
while (true) {
|
|
1408
1617
|
if (cfg.signal?.aborted) {
|
|
@@ -1716,7 +1925,10 @@ async function* runLoop(cfg) {
|
|
|
1716
1925
|
}
|
|
1717
1926
|
yield* await flush();
|
|
1718
1927
|
if (decision === "continue") return void 0;
|
|
1719
|
-
if (decision === "stop")
|
|
1928
|
+
if (decision === "stop") {
|
|
1929
|
+
yield* await append([{ type: "core.run_ended", actor: "system", payload: { status: "done" } }]);
|
|
1930
|
+
return { status: "done", sessionId, lastSeq };
|
|
1931
|
+
}
|
|
1720
1932
|
if ("pause" in decision) {
|
|
1721
1933
|
const note = decision.pause.note ?? `a socket requested a pause (${decision.pause.reason})`;
|
|
1722
1934
|
const kind = decision.pause.reason;
|
|
@@ -1755,6 +1967,9 @@ async function* runLoop(cfg) {
|
|
|
1755
1967
|
);
|
|
1756
1968
|
await log.append(openingEvents);
|
|
1757
1969
|
yield* openingEvents;
|
|
1970
|
+
yield* await append([
|
|
1971
|
+
{ type: "core.run_ended", actor: "system", payload: { status: "handoff", toSessionId } }
|
|
1972
|
+
]);
|
|
1758
1973
|
await cfg.onHandoff?.(sessionId, toSessionId);
|
|
1759
1974
|
return { status: "handoff", sessionId, lastSeq, toSessionId };
|
|
1760
1975
|
}
|
|
@@ -1884,7 +2099,7 @@ async function* executeToolCalls(ctx, calls, timeline, deps) {
|
|
|
1884
2099
|
batch.held.push(toolCallId);
|
|
1885
2100
|
continue;
|
|
1886
2101
|
}
|
|
1887
|
-
if (
|
|
2102
|
+
if (isClientTool(tool) || !tool.execute) {
|
|
1888
2103
|
summary.interruptions.push({ kind: "client_tool", toolCallId, call: call.payload });
|
|
1889
2104
|
batch.held.push(toolCallId);
|
|
1890
2105
|
continue;
|
|
@@ -2211,7 +2426,7 @@ function memoryStore() {
|
|
|
2211
2426
|
}
|
|
2212
2427
|
|
|
2213
2428
|
// src/index.ts
|
|
2214
|
-
var REINS_VERSION = "0.
|
|
2429
|
+
var REINS_VERSION = "0.14.1";
|
|
2215
2430
|
export {
|
|
2216
2431
|
BUILTIN_APPROVAL_POLICY,
|
|
2217
2432
|
CORE_SCHEMAS,
|
|
@@ -2234,6 +2449,7 @@ export {
|
|
|
2234
2449
|
LoweringError,
|
|
2235
2450
|
OUTCOME_UNKNOWN_TEXT,
|
|
2236
2451
|
REINS_VERSION,
|
|
2452
|
+
RUN_MARKER_TYPES,
|
|
2237
2453
|
RunStateError,
|
|
2238
2454
|
RunStop,
|
|
2239
2455
|
STOPPED_IN_PROGRESS_TEXT,
|
|
@@ -2244,6 +2460,7 @@ export {
|
|
|
2244
2460
|
UNTRUSTED_CLOSE_TAG,
|
|
2245
2461
|
UNTRUSTED_TAG,
|
|
2246
2462
|
assertRunStateShape,
|
|
2463
|
+
awaitingOf,
|
|
2247
2464
|
backoffDelayMs,
|
|
2248
2465
|
budgetTruncate,
|
|
2249
2466
|
computeConfigHash,
|
|
@@ -2273,6 +2490,7 @@ export {
|
|
|
2273
2490
|
foldedToolResults,
|
|
2274
2491
|
forkSession,
|
|
2275
2492
|
inputDrafts,
|
|
2493
|
+
isClientTool,
|
|
2276
2494
|
isCompaction,
|
|
2277
2495
|
isContentPart,
|
|
2278
2496
|
isModelOutput,
|
|
@@ -2283,6 +2501,7 @@ export {
|
|
|
2283
2501
|
isSubagentPause,
|
|
2284
2502
|
isTransientFailure,
|
|
2285
2503
|
keptBy,
|
|
2504
|
+
lastRunMarker,
|
|
2286
2505
|
lastToolsBound,
|
|
2287
2506
|
lossesOf,
|
|
2288
2507
|
markUntrusted,
|
|
@@ -2305,6 +2524,7 @@ export {
|
|
|
2305
2524
|
resolveSocketContributions,
|
|
2306
2525
|
roughTokenEstimate,
|
|
2307
2526
|
runLoop,
|
|
2527
|
+
runStatusOf,
|
|
2308
2528
|
serializeRunState,
|
|
2309
2529
|
signRunState,
|
|
2310
2530
|
splitTurns,
|