@reinsjs/brain 0.4.0 → 0.6.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 +20 -0
- package/README.md +24 -4
- package/dist/index.d.ts +108 -5
- package/dist/index.js +420 -262
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -267,9 +267,47 @@ 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
|
+
// src/perception/tiers.ts
|
|
274
|
+
function assertAscending(name, bounds) {
|
|
275
|
+
for (let i = 0; i < bounds.length; i++) {
|
|
276
|
+
const cur = bounds[i];
|
|
277
|
+
const prev = bounds[i - 1];
|
|
278
|
+
if (cur === void 0 || !Number.isFinite(cur) || prev !== void 0 && cur <= prev) {
|
|
279
|
+
throw new RangeError(
|
|
280
|
+
`the tier bounds of ${name} must be strictly ascending finite numbers, got ${JSON.stringify(bounds)}`
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function rangeTier(value, bounds, fmt2) {
|
|
286
|
+
let level = 0;
|
|
287
|
+
while (level < bounds.length && value >= bounds[level]) level++;
|
|
288
|
+
const lo = bounds[level - 1];
|
|
289
|
+
const hi = bounds[level];
|
|
290
|
+
if (lo === void 0) return { level, label: `<${fmt2(hi)}` };
|
|
291
|
+
if (hi === void 0) return { level, label: `\u2265${fmt2(lo)}` };
|
|
292
|
+
return { level, label: `${fmt2(lo)}\u2013${fmt2(hi)}` };
|
|
293
|
+
}
|
|
294
|
+
function countTier(value, bounds) {
|
|
295
|
+
let level = 0;
|
|
296
|
+
while (level < bounds.length && value > bounds[level]) level++;
|
|
297
|
+
const lo = bounds[level - 1];
|
|
298
|
+
const hi = bounds[level];
|
|
299
|
+
if (lo === void 0) return { level, label: `\u2264${hi}` };
|
|
300
|
+
if (hi === void 0) return { level, label: `>${lo}` };
|
|
301
|
+
return { level, label: lo + 1 === hi ? `${hi}` : `${lo + 1}\u2013${hi}` };
|
|
302
|
+
}
|
|
303
|
+
var percent = (ratio) => `${Math.round(ratio * 100)}%`;
|
|
304
|
+
function compactNumber(n) {
|
|
305
|
+
const trim = (x) => String(Number(x.toFixed(1)));
|
|
306
|
+
if (n >= 1e6) return `${trim(n / 1e6)}M`;
|
|
307
|
+
if (n >= 1e3) return `${trim(n / 1e3)}k`;
|
|
308
|
+
return String(n);
|
|
309
|
+
}
|
|
310
|
+
|
|
273
311
|
// src/compact/plan.ts
|
|
274
312
|
import {
|
|
275
313
|
foldedToolResults,
|
|
@@ -332,6 +370,7 @@ var RECALL_TOOL_DESCRIPTION = [
|
|
|
332
370
|
"The original output is returned exactly as the tool produced it (large outputs are stored as blobs and read with fetch_blob instead).",
|
|
333
371
|
"Use it when you need a value that the summary only mentioned or left out; each recall costs a turn and stays in context afterwards."
|
|
334
372
|
].join(" ");
|
|
373
|
+
var COMPACT_REQUEST_RULE = "- If the window gets close to full and you have not compacted, the harness will ask you to. Compact then, before anything else: that is the point where your own summary still beats a mechanical one.";
|
|
335
374
|
var COMPACT_RULES = `## Managing your context window
|
|
336
375
|
You have a \`${COMPACT_TOOL_NAME}\` tool that folds earlier conversation into a summary you write. Status notes from the harness, if present, tell you how full the window is.
|
|
337
376
|
- Compact when a sub-task is finished or the trajectory has converged and the folded details are no longer needed verbatim. Do it at a natural pause, not while acting on something.
|
|
@@ -339,6 +378,7 @@ You have a \`${COMPACT_TOOL_NAME}\` tool that folds earlier conversation into a
|
|
|
339
378
|
- The summary replaces everything it folds, including earlier summaries. Carry forward the user's goals and constraints, decisions made and why, the current state, and what comes next. List each fact that must survive in \`keep\`. The most recent user message and pinned notes survive verbatim on their own; check the pins still say what you need.
|
|
340
379
|
- Keep exact values, not just conclusions: identifiers, status and enum fields you verified, numbers, dates, error codes. "Checked, all ENABLE" loses the field values themselves.
|
|
341
380
|
- Nothing folded is lost for good: under each summary the harness lists the folded tool results as \`seq N tool(arguments)\`, and \`${RECALL_TOOL_NAME}({ seq })\` brings one back verbatim. When asked about a detail your summary did not keep, recall the result that contained it instead of guessing or saying it is gone.
|
|
381
|
+
${COMPACT_REQUEST_RULE}
|
|
342
382
|
- If you never compact, the harness folds the oldest turns mechanically once the window nears its limit. Its summary is worse than yours, but it lists the folded tool results the same way.`;
|
|
343
383
|
|
|
344
384
|
// src/compact/plan.ts
|
|
@@ -589,6 +629,74 @@ function recallTool() {
|
|
|
589
629
|
};
|
|
590
630
|
}
|
|
591
631
|
|
|
632
|
+
// src/compact/request.ts
|
|
633
|
+
import {
|
|
634
|
+
contextOverheadOf,
|
|
635
|
+
isCompaction as isCompaction3
|
|
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
|
|
673
|
+
var DEFAULT_FORCE_AT = [0.75, 0.8];
|
|
674
|
+
function renderCompactRequest(info) {
|
|
675
|
+
if (info.attempt > 1) {
|
|
676
|
+
return `Context window is now ${info.usage} full and you were already asked to compact. This is the last request: your next action must be a \`${COMPACT_TOOL_NAME}\` call, before any reply or other tool. At ${info.autoFoldAt} the harness cuts the oldest turns mechanically and keeps only a rough list instead of your summary.`;
|
|
677
|
+
}
|
|
678
|
+
return `Context window is ${info.usage} full; the harness folds the oldest turns mechanically at ${info.autoFoldAt}. Before doing anything else, call \`${COMPACT_TOOL_NAME}\` now to fold the conversation so far into your own summary (use keepRecentTurns to keep the turns you are actively working from), then continue the task.`;
|
|
679
|
+
}
|
|
680
|
+
function isCompactRequest(e) {
|
|
681
|
+
const n = e;
|
|
682
|
+
return n.type === "core.system_note" && n.payload.meta?.compactRequest !== void 0;
|
|
683
|
+
}
|
|
684
|
+
function compactRequestedSinceLastCompaction(timeline) {
|
|
685
|
+
return compactRequestsSinceLastCompaction(timeline) > 0;
|
|
686
|
+
}
|
|
687
|
+
function compactRequestsSinceLastCompaction(timeline) {
|
|
688
|
+
let n = 0;
|
|
689
|
+
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
690
|
+
const e = timeline[i];
|
|
691
|
+
if (!e || isCompaction3(e)) break;
|
|
692
|
+
if (isCompactRequest(e)) n++;
|
|
693
|
+
}
|
|
694
|
+
return n;
|
|
695
|
+
}
|
|
696
|
+
function contextUsageRatio(ctx) {
|
|
697
|
+
return (ctx.budget.used + contextOverheadOf(ctx.timeline)) / ctx.budget.contextLimit;
|
|
698
|
+
}
|
|
699
|
+
|
|
592
700
|
// src/compact/compact.ts
|
|
593
701
|
var COMPACT_SOCKET_NAME = "compact";
|
|
594
702
|
var DEFAULT_MAX_CONSECUTIVE_COMPACTIONS = 3;
|
|
@@ -642,6 +750,15 @@ function compact(opts = {}) {
|
|
|
642
750
|
throw new RangeError(`compact.maxConsecutive must be an integer >= 1, got ${String(maxConsecutive)}`);
|
|
643
751
|
}
|
|
644
752
|
const withRecall = opts.recall ?? true;
|
|
753
|
+
const force = opts.force === false ? void 0 : opts.force ?? {};
|
|
754
|
+
const forceAt = typeof force?.at === "number" ? [force.at] : force?.at ?? DEFAULT_FORCE_AT;
|
|
755
|
+
if (force && (forceAt.length === 0 || forceAt.some((at, i) => !(at > 0 && at < 1) || i > 0 && at <= (forceAt[i - 1] ?? 0)))) {
|
|
756
|
+
throw new RangeError(
|
|
757
|
+
`compact.force.at must be a number in (0, 1) or a strictly ascending non-empty list of them, got ${JSON.stringify(force.at)}`
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
const directive = force?.directive ?? true;
|
|
761
|
+
const renderRequest = force?.render ?? renderCompactRequest;
|
|
645
762
|
const records = /* @__PURE__ */ new WeakMap();
|
|
646
763
|
const recordOf = (ctx) => {
|
|
647
764
|
let r = records.get(ctx);
|
|
@@ -662,6 +779,30 @@ function compact(opts = {}) {
|
|
|
662
779
|
const socket = {
|
|
663
780
|
name: COMPACT_SOCKET_NAME,
|
|
664
781
|
tools: withRecall ? [tool, recallTool()] : [tool],
|
|
782
|
+
beforeModel(ctx) {
|
|
783
|
+
if (!force) return void 0;
|
|
784
|
+
const timeline = timelineSoFar(ctx);
|
|
785
|
+
const used = compactRequestsSinceLastCompaction(timeline);
|
|
786
|
+
const at = forceAt[used];
|
|
787
|
+
const usage = contextUsageRatio(ctx);
|
|
788
|
+
if (at === void 0 || usage < at || !modelActedSinceLastCompaction(timeline)) return void 0;
|
|
789
|
+
const info = {
|
|
790
|
+
usage: percent(usage),
|
|
791
|
+
autoFoldAt: percent(ctx.budget.targetTokens / ctx.budget.contextLimit),
|
|
792
|
+
attempt: used + 1
|
|
793
|
+
};
|
|
794
|
+
ctx.emit({
|
|
795
|
+
type: "core.system_note",
|
|
796
|
+
actor: "system",
|
|
797
|
+
payload: {
|
|
798
|
+
kind: "budget",
|
|
799
|
+
text: renderRequest(info),
|
|
800
|
+
meta: { compactRequest: { ...info, at } },
|
|
801
|
+
...directive ? { directive: true } : {}
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
return void 0;
|
|
805
|
+
},
|
|
665
806
|
afterModel(ctx, events) {
|
|
666
807
|
recordOf(ctx).hadToolCall = events.some((e) => e.type === "core.tool_call");
|
|
667
808
|
},
|
|
@@ -678,7 +819,10 @@ function compact(opts = {}) {
|
|
|
678
819
|
return replaceResult(result, err instanceof Error ? err.message : String(err), true);
|
|
679
820
|
}
|
|
680
821
|
if (!plan.ok) return replaceResult(result, plan.reason, true);
|
|
681
|
-
const payload =
|
|
822
|
+
const payload = {
|
|
823
|
+
...plan.payload,
|
|
824
|
+
decidedBy: compactRequestedSinceLastCompaction(timelineSoFar(ctx)) ? "forced" : "model"
|
|
825
|
+
};
|
|
682
826
|
ctx.emit({
|
|
683
827
|
type: "core.compaction",
|
|
684
828
|
actor: "model",
|
|
@@ -693,7 +837,7 @@ function compact(opts = {}) {
|
|
|
693
837
|
const record = records.get(ctx);
|
|
694
838
|
if (!record?.hadToolCall) return void 0;
|
|
695
839
|
const snapshotEnd = ctx.timeline.at(-1)?.seq ?? 0;
|
|
696
|
-
const thresholdThisTurn = ctx.events.filter((e) =>
|
|
840
|
+
const thresholdThisTurn = ctx.events.filter((e) => isCompaction4(e) && e.seq > snapshotEnd).length;
|
|
697
841
|
const thisTurn = record.compactions + thresholdThisTurn;
|
|
698
842
|
if (thisTurn === 0) return void 0;
|
|
699
843
|
const total = thisTurn + trailingCompactionRun(ctx.timeline);
|
|
@@ -706,15 +850,261 @@ function compact(opts = {}) {
|
|
|
706
850
|
};
|
|
707
851
|
}
|
|
708
852
|
};
|
|
709
|
-
if (opts.rules !== false)
|
|
853
|
+
if (opts.rules !== false) {
|
|
854
|
+
socket.systemPrompt = opts.rules ?? (force ? COMPACT_RULES : COMPACT_RULES.replace(`${COMPACT_REQUEST_RULE}
|
|
855
|
+
`, ""));
|
|
856
|
+
}
|
|
857
|
+
return socket;
|
|
858
|
+
}
|
|
859
|
+
function isModelCompaction(e) {
|
|
860
|
+
return isCompaction4(e) && e.payload.decidedBy === "model";
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
// src/handoff/handoff.ts
|
|
864
|
+
import { isModelOutput, supersededIds as supersededIds2 } from "@reinsjs/core";
|
|
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;
|
|
710
1106
|
return socket;
|
|
711
1107
|
}
|
|
712
|
-
function isModelCompaction(e) {
|
|
713
|
-
return isCompaction2(e) && e.payload.decidedBy === "model";
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
// src/handoff/handoff.ts
|
|
717
|
-
import { isModelOutput, supersededIds as supersededIds2 } from "@reinsjs/core";
|
|
718
1108
|
|
|
719
1109
|
// src/handoff/rules.ts
|
|
720
1110
|
var HANDOFF_TOOL_NAME = "handoff";
|
|
@@ -798,7 +1188,7 @@ function carriedPins(ctx) {
|
|
|
798
1188
|
const superseded = supersededIds2(ctx.timeline);
|
|
799
1189
|
const out = [];
|
|
800
1190
|
for (const e of ctx.events) {
|
|
801
|
-
if (!isPin(e) || superseded.has(e.id)) continue;
|
|
1191
|
+
if (!isPin(e) || superseded.has(e.id) || isSnapshotPin(e)) continue;
|
|
802
1192
|
const payload = { kind: "pin", text: e.payload.text };
|
|
803
1193
|
if (e.payload.meta !== void 0) payload.meta = e.payload.meta;
|
|
804
1194
|
out.push({
|
|
@@ -844,10 +1234,10 @@ function unfinishedHandoffArgs(timeline) {
|
|
|
844
1234
|
return void 0;
|
|
845
1235
|
}
|
|
846
1236
|
}
|
|
847
|
-
function
|
|
1237
|
+
function replaceResult3(result, text, isError) {
|
|
848
1238
|
return { ...result, payload: { ...result.payload, content: [{ type: "text", text }], isError } };
|
|
849
1239
|
}
|
|
850
|
-
var
|
|
1240
|
+
var PLACEHOLDER3 = `The ${HANDOFF_TOOL_NAME} tool is present but its Socket is not installed; no handoff will happen.`;
|
|
851
1241
|
function handoff(opts = {}) {
|
|
852
1242
|
const carryPins = opts.carryPins ?? true;
|
|
853
1243
|
const intents = /* @__PURE__ */ new WeakMap();
|
|
@@ -857,7 +1247,7 @@ function handoff(opts = {}) {
|
|
|
857
1247
|
inputSchema: HANDOFF_INPUT_SCHEMA,
|
|
858
1248
|
validate: parseHandoffArgs,
|
|
859
1249
|
risk: "low",
|
|
860
|
-
execute: () =>
|
|
1250
|
+
execute: () => PLACEHOLDER3
|
|
861
1251
|
};
|
|
862
1252
|
const socket = {
|
|
863
1253
|
name: HANDOFF_SOCKET_NAME,
|
|
@@ -865,7 +1255,7 @@ function handoff(opts = {}) {
|
|
|
865
1255
|
afterTool(ctx, call, result) {
|
|
866
1256
|
if (call.payload.name !== HANDOFF_TOOL_NAME || result.payload.isError) return void 0;
|
|
867
1257
|
if (intents.has(ctx)) {
|
|
868
|
-
return
|
|
1258
|
+
return replaceResult3(
|
|
869
1259
|
result,
|
|
870
1260
|
"A handoff is already scheduled for the end of this turn; this call changed nothing.",
|
|
871
1261
|
true
|
|
@@ -875,7 +1265,7 @@ function handoff(opts = {}) {
|
|
|
875
1265
|
try {
|
|
876
1266
|
args = parseHandoffArgs(call.payload.args);
|
|
877
1267
|
} catch (err) {
|
|
878
|
-
return
|
|
1268
|
+
return replaceResult3(result, err instanceof Error ? err.message : String(err), true);
|
|
879
1269
|
}
|
|
880
1270
|
const { intent, pins: pins2, triggerMessage } = intentOf(ctx, args);
|
|
881
1271
|
intents.set(ctx, intent);
|
|
@@ -884,7 +1274,7 @@ function handoff(opts = {}) {
|
|
|
884
1274
|
`${pins2.length} pinned note${pins2.length === 1 ? "" : "s"}`,
|
|
885
1275
|
triggerMessage !== void 0 ? "the message to act on" : "no message to act on (the host will supply one)"
|
|
886
1276
|
].join(", ");
|
|
887
|
-
return
|
|
1277
|
+
return replaceResult3(
|
|
888
1278
|
result,
|
|
889
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.`,
|
|
890
1280
|
false
|
|
@@ -1687,47 +2077,7 @@ function memory(opts = {}) {
|
|
|
1687
2077
|
}
|
|
1688
2078
|
|
|
1689
2079
|
// src/perception/reading.ts
|
|
1690
|
-
import { contextOverheadOf } from "@reinsjs/core";
|
|
1691
|
-
|
|
1692
|
-
// src/perception/tiers.ts
|
|
1693
|
-
function assertAscending(name, bounds) {
|
|
1694
|
-
for (let i = 0; i < bounds.length; i++) {
|
|
1695
|
-
const cur = bounds[i];
|
|
1696
|
-
const prev = bounds[i - 1];
|
|
1697
|
-
if (cur === void 0 || !Number.isFinite(cur) || prev !== void 0 && cur <= prev) {
|
|
1698
|
-
throw new RangeError(
|
|
1699
|
-
`the tier bounds of ${name} must be strictly ascending finite numbers, got ${JSON.stringify(bounds)}`
|
|
1700
|
-
);
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
1703
|
-
}
|
|
1704
|
-
function rangeTier(value, bounds, fmt2) {
|
|
1705
|
-
let level = 0;
|
|
1706
|
-
while (level < bounds.length && value >= bounds[level]) level++;
|
|
1707
|
-
const lo = bounds[level - 1];
|
|
1708
|
-
const hi = bounds[level];
|
|
1709
|
-
if (lo === void 0) return { level, label: `<${fmt2(hi)}` };
|
|
1710
|
-
if (hi === void 0) return { level, label: `\u2265${fmt2(lo)}` };
|
|
1711
|
-
return { level, label: `${fmt2(lo)}\u2013${fmt2(hi)}` };
|
|
1712
|
-
}
|
|
1713
|
-
function countTier(value, bounds) {
|
|
1714
|
-
let level = 0;
|
|
1715
|
-
while (level < bounds.length && value > bounds[level]) level++;
|
|
1716
|
-
const lo = bounds[level - 1];
|
|
1717
|
-
const hi = bounds[level];
|
|
1718
|
-
if (lo === void 0) return { level, label: `\u2264${hi}` };
|
|
1719
|
-
if (hi === void 0) return { level, label: `>${lo}` };
|
|
1720
|
-
return { level, label: lo + 1 === hi ? `${hi}` : `${lo + 1}\u2013${hi}` };
|
|
1721
|
-
}
|
|
1722
|
-
var percent = (ratio) => `${Math.round(ratio * 100)}%`;
|
|
1723
|
-
function compactNumber(n) {
|
|
1724
|
-
const trim = (x) => String(Number(x.toFixed(1)));
|
|
1725
|
-
if (n >= 1e6) return `${trim(n / 1e6)}M`;
|
|
1726
|
-
if (n >= 1e3) return `${trim(n / 1e3)}k`;
|
|
1727
|
-
return String(n);
|
|
1728
|
-
}
|
|
1729
|
-
|
|
1730
|
-
// src/perception/reading.ts
|
|
2080
|
+
import { contextOverheadOf as contextOverheadOf2 } from "@reinsjs/core";
|
|
1731
2081
|
var ASSISTANT_TYPES2 = /* @__PURE__ */ new Set([
|
|
1732
2082
|
"core.model_thinking",
|
|
1733
2083
|
"core.model_text",
|
|
@@ -1772,7 +2122,7 @@ function remainingOf(budget2, contextTokens, limits) {
|
|
|
1772
2122
|
}
|
|
1773
2123
|
function readPerception(ctx, thresholds, limits, opts = {}) {
|
|
1774
2124
|
const { budget: budget2 } = ctx;
|
|
1775
|
-
const contextOverhead = opts.calibrate === false ? 0 :
|
|
2125
|
+
const contextOverhead = opts.calibrate === false ? 0 : contextOverheadOf2(ctx.timeline);
|
|
1776
2126
|
const contextTokens = budget2.used + contextOverhead;
|
|
1777
2127
|
const spilled = ctx.events.filter(
|
|
1778
2128
|
(e) => e.type === "core.tool_result" && e.payload.spilled
|
|
@@ -1867,208 +2217,6 @@ function perception(opts = {}) {
|
|
|
1867
2217
|
};
|
|
1868
2218
|
}
|
|
1869
2219
|
|
|
1870
|
-
// src/pins/pins.ts
|
|
1871
|
-
var PINS_SOCKET_NAME = "pins";
|
|
1872
|
-
var DEFAULT_MAX_PIN_TEXT_LENGTH = 500;
|
|
1873
|
-
var DEFAULT_PIN_OVERSHOOT_TOLERANCE = 0.2;
|
|
1874
|
-
function pinInputSchema(maxTextLength) {
|
|
1875
|
-
return {
|
|
1876
|
-
type: "object",
|
|
1877
|
-
properties: {
|
|
1878
|
-
text: {
|
|
1879
|
-
type: "string",
|
|
1880
|
-
maxLength: maxTextLength,
|
|
1881
|
-
description: `The note to pin, one or two sentences (at most ${maxTextLength} characters). It will be shown verbatim after every future summary.`
|
|
1882
|
-
},
|
|
1883
|
-
replaces: {
|
|
1884
|
-
type: "string",
|
|
1885
|
-
description: "Exact text of one of your earlier pinned notes that this one supersedes. Omit to add a new note."
|
|
1886
|
-
}
|
|
1887
|
-
},
|
|
1888
|
-
required: ["text"],
|
|
1889
|
-
additionalProperties: false
|
|
1890
|
-
};
|
|
1891
|
-
}
|
|
1892
|
-
var PIN_INPUT_SCHEMA = {
|
|
1893
|
-
type: "object",
|
|
1894
|
-
properties: {
|
|
1895
|
-
text: {
|
|
1896
|
-
type: "string",
|
|
1897
|
-
description: "The note to pin, one or two sentences. It will be shown verbatim after every future summary."
|
|
1898
|
-
},
|
|
1899
|
-
replaces: {
|
|
1900
|
-
type: "string",
|
|
1901
|
-
description: "Exact text of one of your earlier pinned notes that this one supersedes. Omit to add a new note."
|
|
1902
|
-
}
|
|
1903
|
-
},
|
|
1904
|
-
required: ["text"],
|
|
1905
|
-
additionalProperties: false
|
|
1906
|
-
};
|
|
1907
|
-
function parsePinArgs(raw, maxTextLength = DEFAULT_MAX_PIN_TEXT_LENGTH, overshootTolerance = DEFAULT_PIN_OVERSHOOT_TOLERANCE) {
|
|
1908
|
-
if (typeof raw !== "object" || raw === null) throw new RangeError(`${PIN_TOOL_NAME} expects an object`);
|
|
1909
|
-
const o = raw;
|
|
1910
|
-
if (typeof o.text !== "string" || o.text.trim().length === 0) {
|
|
1911
|
-
throw new RangeError("`text` must be a non-empty string");
|
|
1912
|
-
}
|
|
1913
|
-
const text = o.text.trim();
|
|
1914
|
-
if (text.length > Math.ceil(maxTextLength * (1 + overshootTolerance))) {
|
|
1915
|
-
throw new RangeError(
|
|
1916
|
-
`\`text\` is ${text.length} characters; pins are limited to ${maxTextLength}. Split it across several notes or put it in your compact summary instead.`
|
|
1917
|
-
);
|
|
1918
|
-
}
|
|
1919
|
-
if (o.replaces !== void 0 && (typeof o.replaces !== "string" || o.replaces.trim().length === 0)) {
|
|
1920
|
-
throw new RangeError("`replaces` must be a non-empty string when given");
|
|
1921
|
-
}
|
|
1922
|
-
return typeof o.replaces === "string" ? { text, replaces: o.replaces.trim() } : { text };
|
|
1923
|
-
}
|
|
1924
|
-
function isPinNoteEvent(e) {
|
|
1925
|
-
return e.type === "core.system_note" && e.payload.kind === "pin";
|
|
1926
|
-
}
|
|
1927
|
-
function pinMetaOf(e) {
|
|
1928
|
-
const m = e.payload.meta?.pin;
|
|
1929
|
-
if (typeof m !== "object" || m === null) return void 0;
|
|
1930
|
-
const source = m.source;
|
|
1931
|
-
if (source !== "host" && source !== "model") return void 0;
|
|
1932
|
-
const spec = m.spec;
|
|
1933
|
-
return typeof spec === "string" ? { source, spec } : { source };
|
|
1934
|
-
}
|
|
1935
|
-
function lastHostPin(events, spec) {
|
|
1936
|
-
for (let i = events.length - 1; i >= 0; i--) {
|
|
1937
|
-
const e = events[i];
|
|
1938
|
-
if (!isPinNoteEvent(e)) continue;
|
|
1939
|
-
const meta = pinMetaOf(e);
|
|
1940
|
-
if (meta?.source === "host" && meta.spec === spec) return e;
|
|
1941
|
-
}
|
|
1942
|
-
return void 0;
|
|
1943
|
-
}
|
|
1944
|
-
function findModelPin(events, text) {
|
|
1945
|
-
for (let i = events.length - 1; i >= 0; i--) {
|
|
1946
|
-
const e = events[i];
|
|
1947
|
-
if (isPinNoteEvent(e) && pinMetaOf(e)?.source === "model" && e.payload.text === text) return e;
|
|
1948
|
-
}
|
|
1949
|
-
return void 0;
|
|
1950
|
-
}
|
|
1951
|
-
function normalizeSpecs(specs) {
|
|
1952
|
-
const out = [];
|
|
1953
|
-
const names = /* @__PURE__ */ new Set();
|
|
1954
|
-
for (const spec of specs) {
|
|
1955
|
-
const n = typeof spec === "string" ? { name: spec, text: spec } : "text" in spec ? { name: spec.name, text: spec.text } : { name: spec.name, extract: spec.extract };
|
|
1956
|
-
if (n.name.trim().length === 0) throw new RangeError("pins: a spec name must not be empty");
|
|
1957
|
-
if (names.has(n.name)) throw new RangeError(`pins: duplicate spec name ${JSON.stringify(n.name)}`);
|
|
1958
|
-
if (n.text !== void 0 && n.text.trim().length === 0) {
|
|
1959
|
-
throw new RangeError(`pins: the text of static pin ${JSON.stringify(n.name)} must not be empty`);
|
|
1960
|
-
}
|
|
1961
|
-
if (n.extract !== void 0 && typeof n.extract !== "function") {
|
|
1962
|
-
throw new RangeError(`pins: extract of spec ${JSON.stringify(n.name)} must be a function`);
|
|
1963
|
-
}
|
|
1964
|
-
names.add(n.name);
|
|
1965
|
-
out.push(n);
|
|
1966
|
-
}
|
|
1967
|
-
return out;
|
|
1968
|
-
}
|
|
1969
|
-
function pinDraft(text, meta, actor, extra) {
|
|
1970
|
-
const payload = { kind: "pin", text, meta: { pin: meta } };
|
|
1971
|
-
if (extra.supersedes !== void 0) payload.supersedes = [extra.supersedes];
|
|
1972
|
-
const draft = {
|
|
1973
|
-
type: "core.system_note",
|
|
1974
|
-
actor,
|
|
1975
|
-
payload,
|
|
1976
|
-
provenance: extra.provenanceRef !== void 0 ? { source: PINS_SOCKET_NAME, ref: extra.provenanceRef } : { source: PINS_SOCKET_NAME }
|
|
1977
|
-
};
|
|
1978
|
-
if (extra.parentId !== void 0) draft.parentId = extra.parentId;
|
|
1979
|
-
return draft;
|
|
1980
|
-
}
|
|
1981
|
-
function replaceResult3(result, text, isError) {
|
|
1982
|
-
return { ...result, payload: { ...result.payload, content: [{ type: "text", text }], isError } };
|
|
1983
|
-
}
|
|
1984
|
-
var PLACEHOLDER3 = `The ${PIN_TOOL_NAME} tool is present but its Socket is not installed; nothing was pinned.`;
|
|
1985
|
-
function pins(opts = {}) {
|
|
1986
|
-
const specs = normalizeSpecs(opts.pins ?? []);
|
|
1987
|
-
const withTool = opts.tool ?? true;
|
|
1988
|
-
const maxTextLength = opts.maxTextLength ?? DEFAULT_MAX_PIN_TEXT_LENGTH;
|
|
1989
|
-
if (!Number.isInteger(maxTextLength) || maxTextLength < 1) {
|
|
1990
|
-
throw new RangeError(`pins.maxTextLength must be an integer >= 1, got ${String(maxTextLength)}`);
|
|
1991
|
-
}
|
|
1992
|
-
const tolerance = opts.overshootTolerance ?? DEFAULT_PIN_OVERSHOOT_TOLERANCE;
|
|
1993
|
-
if (!(tolerance >= 0 && tolerance < 1)) {
|
|
1994
|
-
throw new RangeError(`pins.overshootTolerance must be within [0, 1), got ${String(tolerance)}`);
|
|
1995
|
-
}
|
|
1996
|
-
if (specs.length === 0 && !withTool) {
|
|
1997
|
-
throw new RangeError("pins: with neither host pins nor a model-facing tool this socket does nothing");
|
|
1998
|
-
}
|
|
1999
|
-
const tool = {
|
|
2000
|
-
name: PIN_TOOL_NAME,
|
|
2001
|
-
description: pinToolDescription(maxTextLength),
|
|
2002
|
-
inputSchema: pinInputSchema(maxTextLength),
|
|
2003
|
-
validate: (raw) => parsePinArgs(raw, maxTextLength, tolerance),
|
|
2004
|
-
risk: "low",
|
|
2005
|
-
execute: () => PLACEHOLDER3
|
|
2006
|
-
};
|
|
2007
|
-
const socket = {
|
|
2008
|
-
name: PINS_SOCKET_NAME,
|
|
2009
|
-
beforeModel(ctx) {
|
|
2010
|
-
for (const spec of specs) {
|
|
2011
|
-
const raw = spec.text ?? spec.extract?.(ctx);
|
|
2012
|
-
const text = raw?.trim();
|
|
2013
|
-
if (!text) continue;
|
|
2014
|
-
const visible = lastHostPin(ctx.events, spec.name);
|
|
2015
|
-
if (visible?.payload.text === text) continue;
|
|
2016
|
-
const previous = lastHostPin(ctx.timeline, spec.name);
|
|
2017
|
-
const extra = { provenanceRef: spec.name };
|
|
2018
|
-
if (previous) extra.supersedes = previous.id;
|
|
2019
|
-
ctx.emit(pinDraft(text, { source: "host", spec: spec.name }, "system", extra));
|
|
2020
|
-
}
|
|
2021
|
-
return void 0;
|
|
2022
|
-
},
|
|
2023
|
-
afterTool(ctx, call, result) {
|
|
2024
|
-
if (!withTool || call.payload.name !== PIN_TOOL_NAME || result.payload.isError) return void 0;
|
|
2025
|
-
let args;
|
|
2026
|
-
try {
|
|
2027
|
-
args = parsePinArgs(call.payload.args, maxTextLength, tolerance);
|
|
2028
|
-
} catch (err) {
|
|
2029
|
-
return replaceResult3(result, err instanceof Error ? err.message : String(err), true);
|
|
2030
|
-
}
|
|
2031
|
-
let replaced;
|
|
2032
|
-
if (args.replaces !== void 0) {
|
|
2033
|
-
replaced = findModelPin(ctx.events, args.replaces);
|
|
2034
|
-
if (!replaced) {
|
|
2035
|
-
const hostHit = ctx.events.find(
|
|
2036
|
-
(e) => isPinNoteEvent(e) && e.payload.text === args.replaces && pinMetaOf(e)?.source === "host"
|
|
2037
|
-
);
|
|
2038
|
-
return replaceResult3(
|
|
2039
|
-
result,
|
|
2040
|
-
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.",
|
|
2041
|
-
true
|
|
2042
|
-
);
|
|
2043
|
-
}
|
|
2044
|
-
if (replaced.payload.text === args.text) {
|
|
2045
|
-
return replaceResult3(
|
|
2046
|
-
result,
|
|
2047
|
-
"That note is already pinned with the same text; nothing changed.",
|
|
2048
|
-
false
|
|
2049
|
-
);
|
|
2050
|
-
}
|
|
2051
|
-
} else if (findModelPin(ctx.events, args.text)) {
|
|
2052
|
-
return replaceResult3(result, "Already pinned; nothing changed.", false);
|
|
2053
|
-
}
|
|
2054
|
-
const extra = {
|
|
2055
|
-
parentId: call.id,
|
|
2056
|
-
provenanceRef: call.payload.toolCallId
|
|
2057
|
-
};
|
|
2058
|
-
if (replaced) extra.supersedes = replaced.id;
|
|
2059
|
-
ctx.emit(pinDraft(args.text, { source: "model" }, "model", extra));
|
|
2060
|
-
return replaceResult3(
|
|
2061
|
-
result,
|
|
2062
|
-
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.",
|
|
2063
|
-
false
|
|
2064
|
-
);
|
|
2065
|
-
}
|
|
2066
|
-
};
|
|
2067
|
-
if (withTool) socket.tools = [tool];
|
|
2068
|
-
if (withTool && opts.rules !== false) socket.systemPrompt = opts.rules ?? PIN_RULES;
|
|
2069
|
-
return socket;
|
|
2070
|
-
}
|
|
2071
|
-
|
|
2072
2220
|
// src/skills/frontmatter.ts
|
|
2073
2221
|
var MAX_SKILL_DESCRIPTION_CHARS = 1024;
|
|
2074
2222
|
function unquote(v) {
|
|
@@ -2675,10 +2823,12 @@ export {
|
|
|
2675
2823
|
APPROVAL_SOCKET_NAME,
|
|
2676
2824
|
BUDGET_SOCKET_NAME,
|
|
2677
2825
|
COMPACT_INPUT_SCHEMA,
|
|
2826
|
+
COMPACT_REQUEST_RULE,
|
|
2678
2827
|
COMPACT_RULES,
|
|
2679
2828
|
COMPACT_SOCKET_NAME,
|
|
2680
2829
|
COMPACT_TOOL_DESCRIPTION,
|
|
2681
2830
|
COMPACT_TOOL_NAME,
|
|
2831
|
+
DEFAULT_FORCE_AT,
|
|
2682
2832
|
DEFAULT_HANDOFF_REASON,
|
|
2683
2833
|
DEFAULT_LAZY_SUMMARY_CHARS,
|
|
2684
2834
|
DEFAULT_MAX_CONSECUTIVE_COMPACTIONS,
|
|
@@ -2750,8 +2900,11 @@ export {
|
|
|
2750
2900
|
clipEndByTokens,
|
|
2751
2901
|
compact,
|
|
2752
2902
|
compactNumber,
|
|
2903
|
+
compactRequestedSinceLastCompaction,
|
|
2904
|
+
compactRequestsSinceLastCompaction,
|
|
2753
2905
|
composeHandoffNote,
|
|
2754
|
-
contextOverheadOf,
|
|
2906
|
+
contextOverheadOf2 as contextOverheadOf,
|
|
2907
|
+
contextUsageRatio,
|
|
2755
2908
|
countLines,
|
|
2756
2909
|
countModelTurns,
|
|
2757
2910
|
countTier,
|
|
@@ -2767,8 +2920,10 @@ export {
|
|
|
2767
2920
|
handoff,
|
|
2768
2921
|
humanSize,
|
|
2769
2922
|
inlineSkills,
|
|
2923
|
+
isCompactRequest,
|
|
2770
2924
|
isModelCompaction,
|
|
2771
2925
|
isPinNoteEvent,
|
|
2926
|
+
isSnapshotPin,
|
|
2772
2927
|
isTextMime,
|
|
2773
2928
|
isUnder,
|
|
2774
2929
|
lastHostPin,
|
|
@@ -2779,6 +2934,7 @@ export {
|
|
|
2779
2934
|
loadSkillMenu,
|
|
2780
2935
|
measureText,
|
|
2781
2936
|
memory,
|
|
2937
|
+
modelActedSinceLastCompaction,
|
|
2782
2938
|
normalizeRule,
|
|
2783
2939
|
parseCompactArgs,
|
|
2784
2940
|
parseFetchBlobArgs,
|
|
@@ -2801,6 +2957,7 @@ export {
|
|
|
2801
2957
|
readPerception,
|
|
2802
2958
|
recallResult,
|
|
2803
2959
|
recallTool,
|
|
2960
|
+
renderCompactRequest,
|
|
2804
2961
|
renderCompactionSummary,
|
|
2805
2962
|
renderLazyToolMenu,
|
|
2806
2963
|
renderLoadedTool,
|
|
@@ -2814,6 +2971,7 @@ export {
|
|
|
2814
2971
|
spill,
|
|
2815
2972
|
sumSessionTokens,
|
|
2816
2973
|
summarizeTool,
|
|
2974
|
+
timelineSoFar,
|
|
2817
2975
|
toolFindResultBound,
|
|
2818
2976
|
toolReferenceOf,
|
|
2819
2977
|
trailingCompactionRun,
|