@reinsjs/brain 0.5.0 → 0.7.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 +12 -0
- package/README.md +23 -3
- package/dist/index.d.ts +42 -13
- package/dist/index.js +292 -228
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -267,7 +267,7 @@ function budget(opts) {
|
|
|
267
267
|
|
|
268
268
|
// src/compact/compact.ts
|
|
269
269
|
import {
|
|
270
|
-
isCompaction as
|
|
270
|
+
isCompaction as isCompaction4
|
|
271
271
|
} from "@reinsjs/core";
|
|
272
272
|
|
|
273
273
|
// src/perception/tiers.ts
|
|
@@ -632,8 +632,44 @@ function recallTool() {
|
|
|
632
632
|
// src/compact/request.ts
|
|
633
633
|
import {
|
|
634
634
|
contextOverheadOf,
|
|
635
|
-
isCompaction as
|
|
635
|
+
isCompaction as isCompaction3
|
|
636
636
|
} from "@reinsjs/core";
|
|
637
|
+
|
|
638
|
+
// src/shared/segment.ts
|
|
639
|
+
import { isCompaction as isCompaction2 } from "@reinsjs/core";
|
|
640
|
+
var isModelAction = (e) => e.type === "core.model_text" || e.type === "core.tool_call";
|
|
641
|
+
function modelActedSinceLastCompaction(timeline) {
|
|
642
|
+
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
643
|
+
const e = timeline[i];
|
|
644
|
+
if (!e) continue;
|
|
645
|
+
if (isCompaction2(e)) return false;
|
|
646
|
+
if (isModelAction(e)) return true;
|
|
647
|
+
}
|
|
648
|
+
return true;
|
|
649
|
+
}
|
|
650
|
+
function atSegmentStart(timeline) {
|
|
651
|
+
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
652
|
+
const e = timeline[i];
|
|
653
|
+
if (!e) continue;
|
|
654
|
+
if (isCompaction2(e)) return true;
|
|
655
|
+
if (isModelAction(e)) return false;
|
|
656
|
+
}
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
function currentSegment(timeline) {
|
|
660
|
+
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
661
|
+
const e = timeline[i];
|
|
662
|
+
if (e && isCompaction2(e)) return timeline.slice(i + 1);
|
|
663
|
+
}
|
|
664
|
+
return timeline;
|
|
665
|
+
}
|
|
666
|
+
function timelineSoFar(ctx) {
|
|
667
|
+
const end = ctx.timeline.at(-1)?.seq ?? 0;
|
|
668
|
+
const fresh = ctx.events.filter((e) => e.seq > end).sort((a, b) => a.seq - b.seq);
|
|
669
|
+
return [...ctx.timeline, ...fresh];
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// src/compact/request.ts
|
|
637
673
|
var DEFAULT_FORCE_AT = [0.75, 0.8];
|
|
638
674
|
function renderCompactRequest(info) {
|
|
639
675
|
if (info.attempt > 1) {
|
|
@@ -652,25 +688,11 @@ function compactRequestsSinceLastCompaction(timeline) {
|
|
|
652
688
|
let n = 0;
|
|
653
689
|
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
654
690
|
const e = timeline[i];
|
|
655
|
-
if (!e ||
|
|
691
|
+
if (!e || isCompaction3(e)) break;
|
|
656
692
|
if (isCompactRequest(e)) n++;
|
|
657
693
|
}
|
|
658
694
|
return n;
|
|
659
695
|
}
|
|
660
|
-
function modelActedSinceLastCompaction(timeline) {
|
|
661
|
-
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
662
|
-
const e = timeline[i];
|
|
663
|
-
if (!e) continue;
|
|
664
|
-
if (isCompaction2(e)) return false;
|
|
665
|
-
if (e.type === "core.model_text" || e.type === "core.tool_call") return true;
|
|
666
|
-
}
|
|
667
|
-
return true;
|
|
668
|
-
}
|
|
669
|
-
function timelineSoFar(ctx) {
|
|
670
|
-
const end = ctx.timeline.at(-1)?.seq ?? 0;
|
|
671
|
-
const fresh = ctx.events.filter((e) => e.seq > end).sort((a, b) => a.seq - b.seq);
|
|
672
|
-
return [...ctx.timeline, ...fresh];
|
|
673
|
-
}
|
|
674
696
|
function contextUsageRatio(ctx) {
|
|
675
697
|
return (ctx.budget.used + contextOverheadOf(ctx.timeline)) / ctx.budget.contextLimit;
|
|
676
698
|
}
|
|
@@ -815,7 +837,7 @@ function compact(opts = {}) {
|
|
|
815
837
|
const record = records.get(ctx);
|
|
816
838
|
if (!record?.hadToolCall) return void 0;
|
|
817
839
|
const snapshotEnd = ctx.timeline.at(-1)?.seq ?? 0;
|
|
818
|
-
const thresholdThisTurn = ctx.events.filter((e) =>
|
|
840
|
+
const thresholdThisTurn = ctx.events.filter((e) => isCompaction4(e) && e.seq > snapshotEnd).length;
|
|
819
841
|
const thisTurn = record.compactions + thresholdThisTurn;
|
|
820
842
|
if (thisTurn === 0) return void 0;
|
|
821
843
|
const total = thisTurn + trailingCompactionRun(ctx.timeline);
|
|
@@ -835,12 +857,255 @@ function compact(opts = {}) {
|
|
|
835
857
|
return socket;
|
|
836
858
|
}
|
|
837
859
|
function isModelCompaction(e) {
|
|
838
|
-
return
|
|
860
|
+
return isCompaction4(e) && e.payload.decidedBy === "model";
|
|
839
861
|
}
|
|
840
862
|
|
|
841
863
|
// src/handoff/handoff.ts
|
|
842
864
|
import { isModelOutput, supersededIds as supersededIds2 } from "@reinsjs/core";
|
|
843
865
|
|
|
866
|
+
// src/pins/pins.ts
|
|
867
|
+
import {
|
|
868
|
+
covers,
|
|
869
|
+
isCompaction as isCompaction5
|
|
870
|
+
} from "@reinsjs/core";
|
|
871
|
+
var PINS_SOCKET_NAME = "pins";
|
|
872
|
+
var DEFAULT_MAX_PIN_TEXT_LENGTH = 500;
|
|
873
|
+
var DEFAULT_PIN_OVERSHOOT_TOLERANCE = 0.2;
|
|
874
|
+
function pinInputSchema(maxTextLength) {
|
|
875
|
+
return {
|
|
876
|
+
type: "object",
|
|
877
|
+
properties: {
|
|
878
|
+
text: {
|
|
879
|
+
type: "string",
|
|
880
|
+
maxLength: maxTextLength,
|
|
881
|
+
description: `The note to pin, one or two sentences (at most ${maxTextLength} characters). It will be shown verbatim after every future summary.`
|
|
882
|
+
},
|
|
883
|
+
replaces: {
|
|
884
|
+
type: "string",
|
|
885
|
+
description: "Exact text of one of your earlier pinned notes that this one supersedes. Omit to add a new note."
|
|
886
|
+
}
|
|
887
|
+
},
|
|
888
|
+
required: ["text"],
|
|
889
|
+
additionalProperties: false
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
var PIN_INPUT_SCHEMA = {
|
|
893
|
+
type: "object",
|
|
894
|
+
properties: {
|
|
895
|
+
text: {
|
|
896
|
+
type: "string",
|
|
897
|
+
description: "The note to pin, one or two sentences. It will be shown verbatim after every future summary."
|
|
898
|
+
},
|
|
899
|
+
replaces: {
|
|
900
|
+
type: "string",
|
|
901
|
+
description: "Exact text of one of your earlier pinned notes that this one supersedes. Omit to add a new note."
|
|
902
|
+
}
|
|
903
|
+
},
|
|
904
|
+
required: ["text"],
|
|
905
|
+
additionalProperties: false
|
|
906
|
+
};
|
|
907
|
+
function parsePinArgs(raw, maxTextLength = DEFAULT_MAX_PIN_TEXT_LENGTH, overshootTolerance = DEFAULT_PIN_OVERSHOOT_TOLERANCE) {
|
|
908
|
+
if (typeof raw !== "object" || raw === null) throw new RangeError(`${PIN_TOOL_NAME} expects an object`);
|
|
909
|
+
const o = raw;
|
|
910
|
+
if (typeof o.text !== "string" || o.text.trim().length === 0) {
|
|
911
|
+
throw new RangeError("`text` must be a non-empty string");
|
|
912
|
+
}
|
|
913
|
+
const text = o.text.trim();
|
|
914
|
+
if (text.length > Math.ceil(maxTextLength * (1 + overshootTolerance))) {
|
|
915
|
+
throw new RangeError(
|
|
916
|
+
`\`text\` is ${text.length} characters; pins are limited to ${maxTextLength}. Split it across several notes or put it in your compact summary instead.`
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
if (o.replaces !== void 0 && (typeof o.replaces !== "string" || o.replaces.trim().length === 0)) {
|
|
920
|
+
throw new RangeError("`replaces` must be a non-empty string when given");
|
|
921
|
+
}
|
|
922
|
+
return typeof o.replaces === "string" ? { text, replaces: o.replaces.trim() } : { text };
|
|
923
|
+
}
|
|
924
|
+
function isPinNoteEvent(e) {
|
|
925
|
+
return e.type === "core.system_note" && e.payload.kind === "pin";
|
|
926
|
+
}
|
|
927
|
+
function pinMetaOf(e) {
|
|
928
|
+
const m = e.payload.meta?.pin;
|
|
929
|
+
if (typeof m !== "object" || m === null) return void 0;
|
|
930
|
+
const source = m.source;
|
|
931
|
+
if (source !== "host" && source !== "model") return void 0;
|
|
932
|
+
const spec = m.spec;
|
|
933
|
+
const out = typeof spec === "string" ? { source, spec } : { source };
|
|
934
|
+
if (m.snapshot === true) out.snapshot = true;
|
|
935
|
+
return out;
|
|
936
|
+
}
|
|
937
|
+
function isSnapshotPin(e) {
|
|
938
|
+
return isPinNoteEvent(e) && pinMetaOf(e)?.snapshot === true;
|
|
939
|
+
}
|
|
940
|
+
function lastHostPin(events, spec) {
|
|
941
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
942
|
+
const e = events[i];
|
|
943
|
+
if (!isPinNoteEvent(e)) continue;
|
|
944
|
+
const meta = pinMetaOf(e);
|
|
945
|
+
if (meta?.source === "host" && meta.spec === spec) return e;
|
|
946
|
+
}
|
|
947
|
+
return void 0;
|
|
948
|
+
}
|
|
949
|
+
function findModelPin(events, text) {
|
|
950
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
951
|
+
const e = events[i];
|
|
952
|
+
if (isPinNoteEvent(e) && pinMetaOf(e)?.source === "model" && e.payload.text === text) return e;
|
|
953
|
+
}
|
|
954
|
+
return void 0;
|
|
955
|
+
}
|
|
956
|
+
function normalizeSpecs(specs) {
|
|
957
|
+
const out = [];
|
|
958
|
+
const names = /* @__PURE__ */ new Set();
|
|
959
|
+
for (const spec of specs) {
|
|
960
|
+
const n = typeof spec === "string" ? { name: spec, text: spec } : "text" in spec ? { name: spec.name, text: spec.text } : "extract" in spec ? { name: spec.name, extract: spec.extract } : { name: spec.name, snapshot: spec.snapshot };
|
|
961
|
+
if (typeof spec === "object" && "extract" in spec && "snapshot" in spec) {
|
|
962
|
+
throw new RangeError(
|
|
963
|
+
`pins: spec ${JSON.stringify(n.name)} must have either extract or snapshot, not both`
|
|
964
|
+
);
|
|
965
|
+
}
|
|
966
|
+
if (n.name.trim().length === 0) throw new RangeError("pins: a spec name must not be empty");
|
|
967
|
+
if (names.has(n.name)) throw new RangeError(`pins: duplicate spec name ${JSON.stringify(n.name)}`);
|
|
968
|
+
if (n.text !== void 0 && n.text.trim().length === 0) {
|
|
969
|
+
throw new RangeError(`pins: the text of static pin ${JSON.stringify(n.name)} must not be empty`);
|
|
970
|
+
}
|
|
971
|
+
if (n.extract !== void 0 && typeof n.extract !== "function") {
|
|
972
|
+
throw new RangeError(`pins: extract of spec ${JSON.stringify(n.name)} must be a function`);
|
|
973
|
+
}
|
|
974
|
+
if ("snapshot" in n && typeof n.snapshot !== "function") {
|
|
975
|
+
throw new RangeError(`pins: snapshot of spec ${JSON.stringify(n.name)} must be a function`);
|
|
976
|
+
}
|
|
977
|
+
names.add(n.name);
|
|
978
|
+
out.push(n);
|
|
979
|
+
}
|
|
980
|
+
return out;
|
|
981
|
+
}
|
|
982
|
+
function pinDraft(text, meta, actor, extra) {
|
|
983
|
+
const payload = { kind: "pin", text, meta: { pin: meta } };
|
|
984
|
+
if (extra.supersedes !== void 0) payload.supersedes = [extra.supersedes];
|
|
985
|
+
const draft = {
|
|
986
|
+
type: "core.system_note",
|
|
987
|
+
actor,
|
|
988
|
+
payload,
|
|
989
|
+
provenance: extra.provenanceRef !== void 0 ? { source: PINS_SOCKET_NAME, ref: extra.provenanceRef } : { source: PINS_SOCKET_NAME }
|
|
990
|
+
};
|
|
991
|
+
if (extra.parentId !== void 0) draft.parentId = extra.parentId;
|
|
992
|
+
return draft;
|
|
993
|
+
}
|
|
994
|
+
async function refreshSnapshots(ctx, specs) {
|
|
995
|
+
const timeline = timelineSoFar(ctx);
|
|
996
|
+
const segmentStart = atSegmentStart(timeline);
|
|
997
|
+
const segment = currentSegment(timeline);
|
|
998
|
+
const compactions = ctx.events.filter(isCompaction5);
|
|
999
|
+
const drop = /* @__PURE__ */ new Set();
|
|
1000
|
+
for (const spec of specs) {
|
|
1001
|
+
const due = segmentStart && !lastHostPin(segment, spec.name) || !lastHostPin(timeline, spec.name);
|
|
1002
|
+
if (!due || !spec.snapshot) continue;
|
|
1003
|
+
const text = (await spec.snapshot(ctx))?.trim();
|
|
1004
|
+
if (!text) continue;
|
|
1005
|
+
const visible = lastHostPin(ctx.events, spec.name);
|
|
1006
|
+
if (visible?.payload.text === text) continue;
|
|
1007
|
+
const previous = lastHostPin(timeline, spec.name);
|
|
1008
|
+
const extra = { provenanceRef: spec.name };
|
|
1009
|
+
if (previous) extra.supersedes = previous.id;
|
|
1010
|
+
ctx.emit(pinDraft(text, { source: "host", spec: spec.name, snapshot: true }, "system", extra));
|
|
1011
|
+
if (visible && compactions.some((c) => covers(c, visible))) drop.add(visible.id);
|
|
1012
|
+
}
|
|
1013
|
+
if (drop.size === 0) return void 0;
|
|
1014
|
+
return { events: ctx.events.filter((e) => !drop.has(e.id)) };
|
|
1015
|
+
}
|
|
1016
|
+
function replaceResult2(result, text, isError) {
|
|
1017
|
+
return { ...result, payload: { ...result.payload, content: [{ type: "text", text }], isError } };
|
|
1018
|
+
}
|
|
1019
|
+
var PLACEHOLDER2 = `The ${PIN_TOOL_NAME} tool is present but its Socket is not installed; nothing was pinned.`;
|
|
1020
|
+
function pins(opts = {}) {
|
|
1021
|
+
const specs = normalizeSpecs(opts.pins ?? []);
|
|
1022
|
+
const withTool = opts.tool ?? true;
|
|
1023
|
+
const maxTextLength = opts.maxTextLength ?? DEFAULT_MAX_PIN_TEXT_LENGTH;
|
|
1024
|
+
if (!Number.isInteger(maxTextLength) || maxTextLength < 1) {
|
|
1025
|
+
throw new RangeError(`pins.maxTextLength must be an integer >= 1, got ${String(maxTextLength)}`);
|
|
1026
|
+
}
|
|
1027
|
+
const tolerance = opts.overshootTolerance ?? DEFAULT_PIN_OVERSHOOT_TOLERANCE;
|
|
1028
|
+
if (!(tolerance >= 0 && tolerance < 1)) {
|
|
1029
|
+
throw new RangeError(`pins.overshootTolerance must be within [0, 1), got ${String(tolerance)}`);
|
|
1030
|
+
}
|
|
1031
|
+
if (specs.length === 0 && !withTool) {
|
|
1032
|
+
throw new RangeError("pins: with neither host pins nor a model-facing tool this socket does nothing");
|
|
1033
|
+
}
|
|
1034
|
+
const tool = {
|
|
1035
|
+
name: PIN_TOOL_NAME,
|
|
1036
|
+
description: pinToolDescription(maxTextLength),
|
|
1037
|
+
inputSchema: pinInputSchema(maxTextLength),
|
|
1038
|
+
validate: (raw) => parsePinArgs(raw, maxTextLength, tolerance),
|
|
1039
|
+
risk: "low",
|
|
1040
|
+
execute: () => PLACEHOLDER2
|
|
1041
|
+
};
|
|
1042
|
+
const snapshots = specs.filter((s) => s.snapshot !== void 0);
|
|
1043
|
+
const socket = {
|
|
1044
|
+
name: PINS_SOCKET_NAME,
|
|
1045
|
+
beforeModel(ctx) {
|
|
1046
|
+
for (const spec of specs) {
|
|
1047
|
+
if (spec.snapshot) continue;
|
|
1048
|
+
const raw = spec.text ?? spec.extract?.(ctx);
|
|
1049
|
+
const text = raw?.trim();
|
|
1050
|
+
if (!text) continue;
|
|
1051
|
+
const visible = lastHostPin(ctx.events, spec.name);
|
|
1052
|
+
if (visible?.payload.text === text) continue;
|
|
1053
|
+
const previous = lastHostPin(ctx.timeline, spec.name);
|
|
1054
|
+
const extra = { provenanceRef: spec.name };
|
|
1055
|
+
if (previous) extra.supersedes = previous.id;
|
|
1056
|
+
ctx.emit(pinDraft(text, { source: "host", spec: spec.name }, "system", extra));
|
|
1057
|
+
}
|
|
1058
|
+
return snapshots.length > 0 ? refreshSnapshots(ctx, snapshots) : void 0;
|
|
1059
|
+
},
|
|
1060
|
+
afterTool(ctx, call, result) {
|
|
1061
|
+
if (!withTool || call.payload.name !== PIN_TOOL_NAME || result.payload.isError) return void 0;
|
|
1062
|
+
let args;
|
|
1063
|
+
try {
|
|
1064
|
+
args = parsePinArgs(call.payload.args, maxTextLength, tolerance);
|
|
1065
|
+
} catch (err) {
|
|
1066
|
+
return replaceResult2(result, err instanceof Error ? err.message : String(err), true);
|
|
1067
|
+
}
|
|
1068
|
+
let replaced;
|
|
1069
|
+
if (args.replaces !== void 0) {
|
|
1070
|
+
replaced = findModelPin(ctx.events, args.replaces);
|
|
1071
|
+
if (!replaced) {
|
|
1072
|
+
const hostHit = ctx.events.find(
|
|
1073
|
+
(e) => isPinNoteEvent(e) && e.payload.text === args.replaces && pinMetaOf(e)?.source === "host"
|
|
1074
|
+
);
|
|
1075
|
+
return replaceResult2(
|
|
1076
|
+
result,
|
|
1077
|
+
hostHit ? "That note was pinned by the host and cannot be replaced." : "No note pinned by you matches `replaces` exactly. Check the visible pinned notes and pass the text verbatim.",
|
|
1078
|
+
true
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
if (replaced.payload.text === args.text) {
|
|
1082
|
+
return replaceResult2(
|
|
1083
|
+
result,
|
|
1084
|
+
"That note is already pinned with the same text; nothing changed.",
|
|
1085
|
+
false
|
|
1086
|
+
);
|
|
1087
|
+
}
|
|
1088
|
+
} else if (findModelPin(ctx.events, args.text)) {
|
|
1089
|
+
return replaceResult2(result, "Already pinned; nothing changed.", false);
|
|
1090
|
+
}
|
|
1091
|
+
const extra = {
|
|
1092
|
+
parentId: call.id,
|
|
1093
|
+
provenanceRef: call.payload.toolCallId
|
|
1094
|
+
};
|
|
1095
|
+
if (replaced) extra.supersedes = replaced.id;
|
|
1096
|
+
ctx.emit(pinDraft(args.text, { source: "model" }, "model", extra));
|
|
1097
|
+
return replaceResult2(
|
|
1098
|
+
result,
|
|
1099
|
+
replaced ? "Pinned, replacing the earlier note. The old note will not survive the next compaction." : "Pinned. This note will survive compaction verbatim until you replace it.",
|
|
1100
|
+
false
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
if (withTool) socket.tools = [tool];
|
|
1105
|
+
if (withTool && opts.rules !== false) socket.systemPrompt = opts.rules ?? PIN_RULES;
|
|
1106
|
+
return socket;
|
|
1107
|
+
}
|
|
1108
|
+
|
|
844
1109
|
// src/handoff/rules.ts
|
|
845
1110
|
var HANDOFF_TOOL_NAME = "handoff";
|
|
846
1111
|
var HANDOFF_TOOL_DESCRIPTION = [
|
|
@@ -923,7 +1188,7 @@ function carriedPins(ctx) {
|
|
|
923
1188
|
const superseded = supersededIds2(ctx.timeline);
|
|
924
1189
|
const out = [];
|
|
925
1190
|
for (const e of ctx.events) {
|
|
926
|
-
if (!isPin(e) || superseded.has(e.id)) continue;
|
|
1191
|
+
if (!isPin(e) || superseded.has(e.id) || isSnapshotPin(e)) continue;
|
|
927
1192
|
const payload = { kind: "pin", text: e.payload.text };
|
|
928
1193
|
if (e.payload.meta !== void 0) payload.meta = e.payload.meta;
|
|
929
1194
|
out.push({
|
|
@@ -969,10 +1234,10 @@ function unfinishedHandoffArgs(timeline) {
|
|
|
969
1234
|
return void 0;
|
|
970
1235
|
}
|
|
971
1236
|
}
|
|
972
|
-
function
|
|
1237
|
+
function replaceResult3(result, text, isError) {
|
|
973
1238
|
return { ...result, payload: { ...result.payload, content: [{ type: "text", text }], isError } };
|
|
974
1239
|
}
|
|
975
|
-
var
|
|
1240
|
+
var PLACEHOLDER3 = `The ${HANDOFF_TOOL_NAME} tool is present but its Socket is not installed; no handoff will happen.`;
|
|
976
1241
|
function handoff(opts = {}) {
|
|
977
1242
|
const carryPins = opts.carryPins ?? true;
|
|
978
1243
|
const intents = /* @__PURE__ */ new WeakMap();
|
|
@@ -982,7 +1247,7 @@ function handoff(opts = {}) {
|
|
|
982
1247
|
inputSchema: HANDOFF_INPUT_SCHEMA,
|
|
983
1248
|
validate: parseHandoffArgs,
|
|
984
1249
|
risk: "low",
|
|
985
|
-
execute: () =>
|
|
1250
|
+
execute: () => PLACEHOLDER3
|
|
986
1251
|
};
|
|
987
1252
|
const socket = {
|
|
988
1253
|
name: HANDOFF_SOCKET_NAME,
|
|
@@ -990,7 +1255,7 @@ function handoff(opts = {}) {
|
|
|
990
1255
|
afterTool(ctx, call, result) {
|
|
991
1256
|
if (call.payload.name !== HANDOFF_TOOL_NAME || result.payload.isError) return void 0;
|
|
992
1257
|
if (intents.has(ctx)) {
|
|
993
|
-
return
|
|
1258
|
+
return replaceResult3(
|
|
994
1259
|
result,
|
|
995
1260
|
"A handoff is already scheduled for the end of this turn; this call changed nothing.",
|
|
996
1261
|
true
|
|
@@ -1000,7 +1265,7 @@ function handoff(opts = {}) {
|
|
|
1000
1265
|
try {
|
|
1001
1266
|
args = parseHandoffArgs(call.payload.args);
|
|
1002
1267
|
} catch (err) {
|
|
1003
|
-
return
|
|
1268
|
+
return replaceResult3(result, err instanceof Error ? err.message : String(err), true);
|
|
1004
1269
|
}
|
|
1005
1270
|
const { intent, pins: pins2, triggerMessage } = intentOf(ctx, args);
|
|
1006
1271
|
intents.set(ctx, intent);
|
|
@@ -1009,7 +1274,7 @@ function handoff(opts = {}) {
|
|
|
1009
1274
|
`${pins2.length} pinned note${pins2.length === 1 ? "" : "s"}`,
|
|
1010
1275
|
triggerMessage !== void 0 ? "the message to act on" : "no message to act on (the host will supply one)"
|
|
1011
1276
|
].join(", ");
|
|
1012
|
-
return
|
|
1277
|
+
return replaceResult3(
|
|
1013
1278
|
result,
|
|
1014
1279
|
`Handoff scheduled: this session ends when this turn ends, and a new session starts with your summary, ${carried}. Finish any remaining tool calls of this turn; do not start new work.`,
|
|
1015
1280
|
false
|
|
@@ -1952,208 +2217,6 @@ function perception(opts = {}) {
|
|
|
1952
2217
|
};
|
|
1953
2218
|
}
|
|
1954
2219
|
|
|
1955
|
-
// src/pins/pins.ts
|
|
1956
|
-
var PINS_SOCKET_NAME = "pins";
|
|
1957
|
-
var DEFAULT_MAX_PIN_TEXT_LENGTH = 500;
|
|
1958
|
-
var DEFAULT_PIN_OVERSHOOT_TOLERANCE = 0.2;
|
|
1959
|
-
function pinInputSchema(maxTextLength) {
|
|
1960
|
-
return {
|
|
1961
|
-
type: "object",
|
|
1962
|
-
properties: {
|
|
1963
|
-
text: {
|
|
1964
|
-
type: "string",
|
|
1965
|
-
maxLength: maxTextLength,
|
|
1966
|
-
description: `The note to pin, one or two sentences (at most ${maxTextLength} characters). It will be shown verbatim after every future summary.`
|
|
1967
|
-
},
|
|
1968
|
-
replaces: {
|
|
1969
|
-
type: "string",
|
|
1970
|
-
description: "Exact text of one of your earlier pinned notes that this one supersedes. Omit to add a new note."
|
|
1971
|
-
}
|
|
1972
|
-
},
|
|
1973
|
-
required: ["text"],
|
|
1974
|
-
additionalProperties: false
|
|
1975
|
-
};
|
|
1976
|
-
}
|
|
1977
|
-
var PIN_INPUT_SCHEMA = {
|
|
1978
|
-
type: "object",
|
|
1979
|
-
properties: {
|
|
1980
|
-
text: {
|
|
1981
|
-
type: "string",
|
|
1982
|
-
description: "The note to pin, one or two sentences. It will be shown verbatim after every future summary."
|
|
1983
|
-
},
|
|
1984
|
-
replaces: {
|
|
1985
|
-
type: "string",
|
|
1986
|
-
description: "Exact text of one of your earlier pinned notes that this one supersedes. Omit to add a new note."
|
|
1987
|
-
}
|
|
1988
|
-
},
|
|
1989
|
-
required: ["text"],
|
|
1990
|
-
additionalProperties: false
|
|
1991
|
-
};
|
|
1992
|
-
function parsePinArgs(raw, maxTextLength = DEFAULT_MAX_PIN_TEXT_LENGTH, overshootTolerance = DEFAULT_PIN_OVERSHOOT_TOLERANCE) {
|
|
1993
|
-
if (typeof raw !== "object" || raw === null) throw new RangeError(`${PIN_TOOL_NAME} expects an object`);
|
|
1994
|
-
const o = raw;
|
|
1995
|
-
if (typeof o.text !== "string" || o.text.trim().length === 0) {
|
|
1996
|
-
throw new RangeError("`text` must be a non-empty string");
|
|
1997
|
-
}
|
|
1998
|
-
const text = o.text.trim();
|
|
1999
|
-
if (text.length > Math.ceil(maxTextLength * (1 + overshootTolerance))) {
|
|
2000
|
-
throw new RangeError(
|
|
2001
|
-
`\`text\` is ${text.length} characters; pins are limited to ${maxTextLength}. Split it across several notes or put it in your compact summary instead.`
|
|
2002
|
-
);
|
|
2003
|
-
}
|
|
2004
|
-
if (o.replaces !== void 0 && (typeof o.replaces !== "string" || o.replaces.trim().length === 0)) {
|
|
2005
|
-
throw new RangeError("`replaces` must be a non-empty string when given");
|
|
2006
|
-
}
|
|
2007
|
-
return typeof o.replaces === "string" ? { text, replaces: o.replaces.trim() } : { text };
|
|
2008
|
-
}
|
|
2009
|
-
function isPinNoteEvent(e) {
|
|
2010
|
-
return e.type === "core.system_note" && e.payload.kind === "pin";
|
|
2011
|
-
}
|
|
2012
|
-
function pinMetaOf(e) {
|
|
2013
|
-
const m = e.payload.meta?.pin;
|
|
2014
|
-
if (typeof m !== "object" || m === null) return void 0;
|
|
2015
|
-
const source = m.source;
|
|
2016
|
-
if (source !== "host" && source !== "model") return void 0;
|
|
2017
|
-
const spec = m.spec;
|
|
2018
|
-
return typeof spec === "string" ? { source, spec } : { source };
|
|
2019
|
-
}
|
|
2020
|
-
function lastHostPin(events, spec) {
|
|
2021
|
-
for (let i = events.length - 1; i >= 0; i--) {
|
|
2022
|
-
const e = events[i];
|
|
2023
|
-
if (!isPinNoteEvent(e)) continue;
|
|
2024
|
-
const meta = pinMetaOf(e);
|
|
2025
|
-
if (meta?.source === "host" && meta.spec === spec) return e;
|
|
2026
|
-
}
|
|
2027
|
-
return void 0;
|
|
2028
|
-
}
|
|
2029
|
-
function findModelPin(events, text) {
|
|
2030
|
-
for (let i = events.length - 1; i >= 0; i--) {
|
|
2031
|
-
const e = events[i];
|
|
2032
|
-
if (isPinNoteEvent(e) && pinMetaOf(e)?.source === "model" && e.payload.text === text) return e;
|
|
2033
|
-
}
|
|
2034
|
-
return void 0;
|
|
2035
|
-
}
|
|
2036
|
-
function normalizeSpecs(specs) {
|
|
2037
|
-
const out = [];
|
|
2038
|
-
const names = /* @__PURE__ */ new Set();
|
|
2039
|
-
for (const spec of specs) {
|
|
2040
|
-
const n = typeof spec === "string" ? { name: spec, text: spec } : "text" in spec ? { name: spec.name, text: spec.text } : { name: spec.name, extract: spec.extract };
|
|
2041
|
-
if (n.name.trim().length === 0) throw new RangeError("pins: a spec name must not be empty");
|
|
2042
|
-
if (names.has(n.name)) throw new RangeError(`pins: duplicate spec name ${JSON.stringify(n.name)}`);
|
|
2043
|
-
if (n.text !== void 0 && n.text.trim().length === 0) {
|
|
2044
|
-
throw new RangeError(`pins: the text of static pin ${JSON.stringify(n.name)} must not be empty`);
|
|
2045
|
-
}
|
|
2046
|
-
if (n.extract !== void 0 && typeof n.extract !== "function") {
|
|
2047
|
-
throw new RangeError(`pins: extract of spec ${JSON.stringify(n.name)} must be a function`);
|
|
2048
|
-
}
|
|
2049
|
-
names.add(n.name);
|
|
2050
|
-
out.push(n);
|
|
2051
|
-
}
|
|
2052
|
-
return out;
|
|
2053
|
-
}
|
|
2054
|
-
function pinDraft(text, meta, actor, extra) {
|
|
2055
|
-
const payload = { kind: "pin", text, meta: { pin: meta } };
|
|
2056
|
-
if (extra.supersedes !== void 0) payload.supersedes = [extra.supersedes];
|
|
2057
|
-
const draft = {
|
|
2058
|
-
type: "core.system_note",
|
|
2059
|
-
actor,
|
|
2060
|
-
payload,
|
|
2061
|
-
provenance: extra.provenanceRef !== void 0 ? { source: PINS_SOCKET_NAME, ref: extra.provenanceRef } : { source: PINS_SOCKET_NAME }
|
|
2062
|
-
};
|
|
2063
|
-
if (extra.parentId !== void 0) draft.parentId = extra.parentId;
|
|
2064
|
-
return draft;
|
|
2065
|
-
}
|
|
2066
|
-
function replaceResult3(result, text, isError) {
|
|
2067
|
-
return { ...result, payload: { ...result.payload, content: [{ type: "text", text }], isError } };
|
|
2068
|
-
}
|
|
2069
|
-
var PLACEHOLDER3 = `The ${PIN_TOOL_NAME} tool is present but its Socket is not installed; nothing was pinned.`;
|
|
2070
|
-
function pins(opts = {}) {
|
|
2071
|
-
const specs = normalizeSpecs(opts.pins ?? []);
|
|
2072
|
-
const withTool = opts.tool ?? true;
|
|
2073
|
-
const maxTextLength = opts.maxTextLength ?? DEFAULT_MAX_PIN_TEXT_LENGTH;
|
|
2074
|
-
if (!Number.isInteger(maxTextLength) || maxTextLength < 1) {
|
|
2075
|
-
throw new RangeError(`pins.maxTextLength must be an integer >= 1, got ${String(maxTextLength)}`);
|
|
2076
|
-
}
|
|
2077
|
-
const tolerance = opts.overshootTolerance ?? DEFAULT_PIN_OVERSHOOT_TOLERANCE;
|
|
2078
|
-
if (!(tolerance >= 0 && tolerance < 1)) {
|
|
2079
|
-
throw new RangeError(`pins.overshootTolerance must be within [0, 1), got ${String(tolerance)}`);
|
|
2080
|
-
}
|
|
2081
|
-
if (specs.length === 0 && !withTool) {
|
|
2082
|
-
throw new RangeError("pins: with neither host pins nor a model-facing tool this socket does nothing");
|
|
2083
|
-
}
|
|
2084
|
-
const tool = {
|
|
2085
|
-
name: PIN_TOOL_NAME,
|
|
2086
|
-
description: pinToolDescription(maxTextLength),
|
|
2087
|
-
inputSchema: pinInputSchema(maxTextLength),
|
|
2088
|
-
validate: (raw) => parsePinArgs(raw, maxTextLength, tolerance),
|
|
2089
|
-
risk: "low",
|
|
2090
|
-
execute: () => PLACEHOLDER3
|
|
2091
|
-
};
|
|
2092
|
-
const socket = {
|
|
2093
|
-
name: PINS_SOCKET_NAME,
|
|
2094
|
-
beforeModel(ctx) {
|
|
2095
|
-
for (const spec of specs) {
|
|
2096
|
-
const raw = spec.text ?? spec.extract?.(ctx);
|
|
2097
|
-
const text = raw?.trim();
|
|
2098
|
-
if (!text) continue;
|
|
2099
|
-
const visible = lastHostPin(ctx.events, spec.name);
|
|
2100
|
-
if (visible?.payload.text === text) continue;
|
|
2101
|
-
const previous = lastHostPin(ctx.timeline, spec.name);
|
|
2102
|
-
const extra = { provenanceRef: spec.name };
|
|
2103
|
-
if (previous) extra.supersedes = previous.id;
|
|
2104
|
-
ctx.emit(pinDraft(text, { source: "host", spec: spec.name }, "system", extra));
|
|
2105
|
-
}
|
|
2106
|
-
return void 0;
|
|
2107
|
-
},
|
|
2108
|
-
afterTool(ctx, call, result) {
|
|
2109
|
-
if (!withTool || call.payload.name !== PIN_TOOL_NAME || result.payload.isError) return void 0;
|
|
2110
|
-
let args;
|
|
2111
|
-
try {
|
|
2112
|
-
args = parsePinArgs(call.payload.args, maxTextLength, tolerance);
|
|
2113
|
-
} catch (err) {
|
|
2114
|
-
return replaceResult3(result, err instanceof Error ? err.message : String(err), true);
|
|
2115
|
-
}
|
|
2116
|
-
let replaced;
|
|
2117
|
-
if (args.replaces !== void 0) {
|
|
2118
|
-
replaced = findModelPin(ctx.events, args.replaces);
|
|
2119
|
-
if (!replaced) {
|
|
2120
|
-
const hostHit = ctx.events.find(
|
|
2121
|
-
(e) => isPinNoteEvent(e) && e.payload.text === args.replaces && pinMetaOf(e)?.source === "host"
|
|
2122
|
-
);
|
|
2123
|
-
return replaceResult3(
|
|
2124
|
-
result,
|
|
2125
|
-
hostHit ? "That note was pinned by the host and cannot be replaced." : "No note pinned by you matches `replaces` exactly. Check the visible pinned notes and pass the text verbatim.",
|
|
2126
|
-
true
|
|
2127
|
-
);
|
|
2128
|
-
}
|
|
2129
|
-
if (replaced.payload.text === args.text) {
|
|
2130
|
-
return replaceResult3(
|
|
2131
|
-
result,
|
|
2132
|
-
"That note is already pinned with the same text; nothing changed.",
|
|
2133
|
-
false
|
|
2134
|
-
);
|
|
2135
|
-
}
|
|
2136
|
-
} else if (findModelPin(ctx.events, args.text)) {
|
|
2137
|
-
return replaceResult3(result, "Already pinned; nothing changed.", false);
|
|
2138
|
-
}
|
|
2139
|
-
const extra = {
|
|
2140
|
-
parentId: call.id,
|
|
2141
|
-
provenanceRef: call.payload.toolCallId
|
|
2142
|
-
};
|
|
2143
|
-
if (replaced) extra.supersedes = replaced.id;
|
|
2144
|
-
ctx.emit(pinDraft(args.text, { source: "model" }, "model", extra));
|
|
2145
|
-
return replaceResult3(
|
|
2146
|
-
result,
|
|
2147
|
-
replaced ? "Pinned, replacing the earlier note. The old note will not survive the next compaction." : "Pinned. This note will survive compaction verbatim until you replace it.",
|
|
2148
|
-
false
|
|
2149
|
-
);
|
|
2150
|
-
}
|
|
2151
|
-
};
|
|
2152
|
-
if (withTool) socket.tools = [tool];
|
|
2153
|
-
if (withTool && opts.rules !== false) socket.systemPrompt = opts.rules ?? PIN_RULES;
|
|
2154
|
-
return socket;
|
|
2155
|
-
}
|
|
2156
|
-
|
|
2157
2220
|
// src/skills/frontmatter.ts
|
|
2158
2221
|
var MAX_SKILL_DESCRIPTION_CHARS = 1024;
|
|
2159
2222
|
function unquote(v) {
|
|
@@ -2860,6 +2923,7 @@ export {
|
|
|
2860
2923
|
isCompactRequest,
|
|
2861
2924
|
isModelCompaction,
|
|
2862
2925
|
isPinNoteEvent,
|
|
2926
|
+
isSnapshotPin,
|
|
2863
2927
|
isTextMime,
|
|
2864
2928
|
isUnder,
|
|
2865
2929
|
lastHostPin,
|