agentfootprint 8.0.0 → 8.2.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/AGENTS.md +14 -9
- package/README.md +11 -7
- package/ai-instructions/claude-code/SKILL.md +1 -1
- package/ai-instructions/clinerules +1 -1
- package/ai-instructions/copilot-instructions.md +1 -1
- package/ai-instructions/cursor/agentfootprint.md +1 -1
- package/ai-instructions/kiro/agentfootprint.md +1 -1
- package/ai-instructions/windsurfrules +1 -1
- package/dist/adapters/llm/OllamaProvider.js +628 -0
- package/dist/adapters/llm/OllamaProvider.js.map +1 -0
- package/dist/adapters/llm/OpenAIProvider.js +12 -21
- package/dist/adapters/llm/OpenAIProvider.js.map +1 -1
- package/dist/adapters/llm/createProvider.js +35 -6
- package/dist/adapters/llm/createProvider.js.map +1 -1
- package/dist/core/Agent.js +44 -1
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/agent/stages/seed.js +10 -0
- package/dist/core/agent/stages/seed.js.map +1 -1
- package/dist/core/agent/stages/window.js +21 -0
- package/dist/core/agent/stages/window.js.map +1 -1
- package/dist/core/agent/window/errors.js +4 -3
- package/dist/core/agent/window/errors.js.map +1 -1
- package/dist/core/agent/window/folded.js +98 -0
- package/dist/core/agent/window/folded.js.map +1 -0
- package/dist/core/agent/window/index.js +9 -1
- package/dist/core/agent/window/index.js.map +1 -1
- package/dist/core/agent/window/options.js +18 -2
- package/dist/core/agent/window/options.js.map +1 -1
- package/dist/core/agent/window/strategies/slidingWindow.js +2 -2
- package/dist/core/agent/window/strategies/summarizeOldest.js +25 -2
- package/dist/core/agent/window/strategies/summarizeOldest.js.map +1 -1
- package/dist/core/agent/window/summarize.js +27 -1
- package/dist/core/agent/window/summarize.js.map +1 -1
- package/dist/core/agent/window/types.js +6 -0
- package/dist/core/agent/window/types.js.map +1 -1
- package/dist/core/runCheckpoint.js +9 -1
- package/dist/core/runCheckpoint.js.map +1 -1
- package/dist/esm/adapters/llm/OllamaProvider.d.ts +182 -0
- package/dist/esm/adapters/llm/OllamaProvider.js +622 -0
- package/dist/esm/adapters/llm/OllamaProvider.js.map +1 -0
- package/dist/esm/adapters/llm/OpenAIProvider.d.ts +0 -12
- package/dist/esm/adapters/llm/OpenAIProvider.js +11 -19
- package/dist/esm/adapters/llm/OpenAIProvider.js.map +1 -1
- package/dist/esm/adapters/llm/createProvider.d.ts +25 -8
- package/dist/esm/adapters/llm/createProvider.js +35 -6
- package/dist/esm/adapters/llm/createProvider.js.map +1 -1
- package/dist/esm/core/Agent.d.ts +17 -0
- package/dist/esm/core/Agent.js +44 -1
- package/dist/esm/core/Agent.js.map +1 -1
- package/dist/esm/core/agent/stages/seed.d.ts +11 -0
- package/dist/esm/core/agent/stages/seed.js +10 -0
- package/dist/esm/core/agent/stages/seed.js.map +1 -1
- package/dist/esm/core/agent/stages/window.d.ts +13 -0
- package/dist/esm/core/agent/stages/window.js +21 -0
- package/dist/esm/core/agent/stages/window.js.map +1 -1
- package/dist/esm/core/agent/types.d.ts +15 -1
- package/dist/esm/core/agent/window/errors.js +4 -3
- package/dist/esm/core/agent/window/errors.js.map +1 -1
- package/dist/esm/core/agent/window/folded.d.ts +90 -0
- package/dist/esm/core/agent/window/folded.js +92 -0
- package/dist/esm/core/agent/window/folded.js.map +1 -0
- package/dist/esm/core/agent/window/index.d.ts +7 -1
- package/dist/esm/core/agent/window/index.js +6 -0
- package/dist/esm/core/agent/window/index.js.map +1 -1
- package/dist/esm/core/agent/window/options.d.ts +7 -1
- package/dist/esm/core/agent/window/options.js +17 -1
- package/dist/esm/core/agent/window/options.js.map +1 -1
- package/dist/esm/core/agent/window/strategies/slidingWindow.d.ts +2 -2
- package/dist/esm/core/agent/window/strategies/slidingWindow.js +2 -2
- package/dist/esm/core/agent/window/strategies/summarizeOldest.d.ts +4 -2
- package/dist/esm/core/agent/window/strategies/summarizeOldest.js +25 -2
- package/dist/esm/core/agent/window/strategies/summarizeOldest.js.map +1 -1
- package/dist/esm/core/agent/window/strategy.d.ts +26 -1
- package/dist/esm/core/agent/window/summarize.d.ts +11 -0
- package/dist/esm/core/agent/window/summarize.js +27 -1
- package/dist/esm/core/agent/window/summarize.js.map +1 -1
- package/dist/esm/core/agent/window/types.d.ts +106 -1
- package/dist/esm/core/agent/window/types.js +6 -0
- package/dist/esm/core/agent/window/types.js.map +1 -1
- package/dist/esm/core/runCheckpoint.d.ts +32 -1
- package/dist/esm/core/runCheckpoint.js +9 -1
- package/dist/esm/core/runCheckpoint.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +6 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/providers.d.ts +13 -5
- package/dist/esm/providers.js +13 -5
- package/dist/esm/providers.js.map +1 -1
- package/dist/esm/thinking/OllamaThinkingHandler.d.ts +60 -0
- package/dist/esm/thinking/OllamaThinkingHandler.js +107 -0
- package/dist/esm/thinking/OllamaThinkingHandler.js.map +1 -0
- package/dist/esm/thinking/index.d.ts +1 -0
- package/dist/esm/thinking/index.js +1 -0
- package/dist/esm/thinking/index.js.map +1 -1
- package/dist/esm/thinking/registry.js +2 -0
- package/dist/esm/thinking/registry.js.map +1 -1
- package/dist/index.js +9 -2
- package/dist/index.js.map +1 -1
- package/dist/providers.js +16 -6
- package/dist/providers.js.map +1 -1
- package/dist/thinking/OllamaThinkingHandler.js +111 -0
- package/dist/thinking/OllamaThinkingHandler.js.map +1 -0
- package/dist/thinking/index.js +4 -1
- package/dist/thinking/index.js.map +1 -1
- package/dist/thinking/registry.js +2 -0
- package/dist/thinking/registry.js.map +1 -1
- package/dist/types/adapters/llm/OllamaProvider.d.ts +183 -0
- package/dist/types/adapters/llm/OllamaProvider.d.ts.map +1 -0
- package/dist/types/adapters/llm/OpenAIProvider.d.ts +0 -12
- package/dist/types/adapters/llm/OpenAIProvider.d.ts.map +1 -1
- package/dist/types/adapters/llm/createProvider.d.ts +25 -8
- package/dist/types/adapters/llm/createProvider.d.ts.map +1 -1
- package/dist/types/core/Agent.d.ts +17 -0
- package/dist/types/core/Agent.d.ts.map +1 -1
- package/dist/types/core/agent/stages/seed.d.ts +11 -0
- package/dist/types/core/agent/stages/seed.d.ts.map +1 -1
- package/dist/types/core/agent/stages/window.d.ts +13 -0
- package/dist/types/core/agent/stages/window.d.ts.map +1 -1
- package/dist/types/core/agent/types.d.ts +15 -1
- package/dist/types/core/agent/types.d.ts.map +1 -1
- package/dist/types/core/agent/window/errors.d.ts.map +1 -1
- package/dist/types/core/agent/window/folded.d.ts +91 -0
- package/dist/types/core/agent/window/folded.d.ts.map +1 -0
- package/dist/types/core/agent/window/index.d.ts +7 -1
- package/dist/types/core/agent/window/index.d.ts.map +1 -1
- package/dist/types/core/agent/window/options.d.ts +7 -1
- package/dist/types/core/agent/window/options.d.ts.map +1 -1
- package/dist/types/core/agent/window/strategies/slidingWindow.d.ts +2 -2
- package/dist/types/core/agent/window/strategies/summarizeOldest.d.ts +4 -2
- package/dist/types/core/agent/window/strategies/summarizeOldest.d.ts.map +1 -1
- package/dist/types/core/agent/window/strategy.d.ts +26 -1
- package/dist/types/core/agent/window/strategy.d.ts.map +1 -1
- package/dist/types/core/agent/window/summarize.d.ts +11 -0
- package/dist/types/core/agent/window/summarize.d.ts.map +1 -1
- package/dist/types/core/agent/window/types.d.ts +106 -1
- package/dist/types/core/agent/window/types.d.ts.map +1 -1
- package/dist/types/core/runCheckpoint.d.ts +32 -1
- package/dist/types/core/runCheckpoint.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/providers.d.ts +13 -5
- package/dist/types/providers.d.ts.map +1 -1
- package/dist/types/thinking/OllamaThinkingHandler.d.ts +61 -0
- package/dist/types/thinking/OllamaThinkingHandler.d.ts.map +1 -0
- package/dist/types/thinking/index.d.ts +1 -0
- package/dist/types/thinking/index.d.ts.map +1 -1
- package/dist/types/thinking/registry.d.ts.map +1 -1
- package/package.json +3 -1
|
@@ -17,9 +17,15 @@
|
|
|
17
17
|
*
|
|
18
18
|
* The frame is authored. The summary is data. Neither can become
|
|
19
19
|
* the other.
|
|
20
|
+
*
|
|
21
|
+
* A third rule joined them in 8.2: the frame may only claim what is
|
|
22
|
+
* true. It names where the folded messages went, and it reads that
|
|
23
|
+
* from the resolved retention policy rather than asserting a
|
|
24
|
+
* constant — see {@link retentionSentence}.
|
|
20
25
|
* Emits: N/A (the stage emits; this file only builds and calls).
|
|
21
26
|
*/
|
|
22
27
|
import type { LLMMessage, LLMProvider, LLMResponse } from '../../../adapters/types.js';
|
|
28
|
+
import type { CompactionRetention } from './types.js';
|
|
23
29
|
/** Opening of the authored label. Stable — tests and readers match on it. */
|
|
24
30
|
export declare const COMPACTED_FRAME_PREFIX = "[compacted history";
|
|
25
31
|
/**
|
|
@@ -39,11 +45,16 @@ export declare function renderTranscript(messages: readonly LLMMessage[]): strin
|
|
|
39
45
|
* The label is written by this function and always comes first. `summary` is
|
|
40
46
|
* appended to it verbatim: the library never edits model output, and it never
|
|
41
47
|
* lets model output speak in the library's voice either.
|
|
48
|
+
*
|
|
49
|
+
* The label states the retention policy, and states it truthfully — see
|
|
50
|
+
* {@link retentionSentence}. `COMPACTED_FRAME_PREFIX` is unchanged: it is what
|
|
51
|
+
* every reader and every test matches on, and it stays put.
|
|
42
52
|
*/
|
|
43
53
|
export declare function buildSummaryMessage(summary: string, facts: {
|
|
44
54
|
readonly foldedMessageCount: number;
|
|
45
55
|
readonly iteration: number;
|
|
46
56
|
readonly model: string;
|
|
57
|
+
readonly retain: CompactionRetention;
|
|
47
58
|
}): LLMMessage;
|
|
48
59
|
/** True when this message is a frame a previous fold wrote. */
|
|
49
60
|
export declare function isCompactedSummary(msg: LLMMessage | undefined): boolean;
|
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
*
|
|
18
18
|
* The frame is authored. The summary is data. Neither can become
|
|
19
19
|
* the other.
|
|
20
|
+
*
|
|
21
|
+
* A third rule joined them in 8.2: the frame may only claim what is
|
|
22
|
+
* true. It names where the folded messages went, and it reads that
|
|
23
|
+
* from the resolved retention policy rather than asserting a
|
|
24
|
+
* constant — see {@link retentionSentence}.
|
|
20
25
|
* Emits: N/A (the stage emits; this file only builds and calls).
|
|
21
26
|
*/
|
|
22
27
|
/** Opening of the authored label. Stable — tests and readers match on it. */
|
|
@@ -56,6 +61,23 @@ function renderMessage(msg) {
|
|
|
56
61
|
export function renderTranscript(messages) {
|
|
57
62
|
return [TRANSCRIPT_OPEN, ...messages.map(renderMessage), TRANSCRIPT_CLOSE].join('\n');
|
|
58
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Where the folded messages went, said in the frame itself.
|
|
66
|
+
*
|
|
67
|
+
* Through 8.1 this sentence was a constant: *"The folded messages are retained
|
|
68
|
+
* verbatim in this run's commit log."* True inside the process, and false the
|
|
69
|
+
* moment the run ended — which is exactly when a standing agent reads it back
|
|
70
|
+
* out of storage. A library asserting something false inside the model's own
|
|
71
|
+
* context is worse than saying nothing, so the sentence is now written FROM
|
|
72
|
+
* the resolved retention policy and can only say what actually happened.
|
|
73
|
+
*/
|
|
74
|
+
function retentionSentence(retain) {
|
|
75
|
+
return retain === 'conversation'
|
|
76
|
+
? `The folded messages are retained verbatim with this conversation and can be produced ` +
|
|
77
|
+
`on request.`
|
|
78
|
+
: `The folded messages were not retained beyond the run that folded them; only this ` +
|
|
79
|
+
`summary carries them forward.`;
|
|
80
|
+
}
|
|
59
81
|
/**
|
|
60
82
|
* Build the message that replaces the folded span IN THE WINDOW.
|
|
61
83
|
*
|
|
@@ -65,12 +87,16 @@ export function renderTranscript(messages) {
|
|
|
65
87
|
* The label is written by this function and always comes first. `summary` is
|
|
66
88
|
* appended to it verbatim: the library never edits model output, and it never
|
|
67
89
|
* lets model output speak in the library's voice either.
|
|
90
|
+
*
|
|
91
|
+
* The label states the retention policy, and states it truthfully — see
|
|
92
|
+
* {@link retentionSentence}. `COMPACTED_FRAME_PREFIX` is unchanged: it is what
|
|
93
|
+
* every reader and every test matches on, and it stays put.
|
|
68
94
|
*/
|
|
69
95
|
export function buildSummaryMessage(summary, facts) {
|
|
70
96
|
const label = `${COMPACTED_FRAME_PREFIX} — ${facts.foldedMessageCount} earlier message(s) were folded ` +
|
|
71
97
|
`out of this window at iteration ${facts.iteration}. The text after this line is a SUMMARY ` +
|
|
72
98
|
`written by ${facts.model}; it is a claim about the conversation, not the conversation. ` +
|
|
73
|
-
|
|
99
|
+
`${retentionSentence(facts.retain)}]`;
|
|
74
100
|
return { role: 'user', content: `${label}\n\n${summary}` };
|
|
75
101
|
}
|
|
76
102
|
/** True when this message is a frame a previous fold wrote. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summarize.js","sourceRoot":"","sources":["../../../../../src/core/agent/window/summarize.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"summarize.js","sourceRoot":"","sources":["../../../../../src/core/agent/window/summarize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAKH,6EAA6E;AAC7E,MAAM,CAAC,MAAM,sBAAsB,GAAG,oBAAoB,CAAC;AAE3D,kFAAkF;AAClF,MAAM,eAAe,GAAG,kBAAkB,CAAC;AAC3C,MAAM,gBAAgB,GAAG,sBAAsB,CAAC;AAEhD;;;;GAIG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG;IACtC,wFAAwF;IACxF,kCAAkC;IAClC,EAAE;IACF,kCAAkC,eAAe,QAAQ,gBAAgB,sBAAsB;IAC/F,8FAA8F;IAC9F,6FAA6F;IAC7F,EAAE;IACF,4FAA4F;IAC5F,6FAA6F;IAC7F,gGAAgG;IAChG,yEAAyE;CAC1E,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEb,+EAA+E;AAC/E,SAAS,aAAa,CAAC,GAAe;IACpC,MAAM,KAAK,GAAG,CAAC,GAAG,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC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AAE,CAAC;AAC7D,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,kBAAkB,CAAC,GAA2B;IAC5D,OAAO,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,sBAAsB,CAAC,CAAC;AACpG,CAAC;AAOD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAqB,EACrB,KAAa,EACb,IAA2B,EAC3B,MAA+B;IAE/B,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAC;QACvC,YAAY,EAAE,wBAAwB;QACtC,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7D,KAAK;QACL,GAAG,CAAC,MAAM,KAAK,SAAS,IAAI,EAAE,MAAM,EAAE,CAAC;KACxC,CAAC,CAAC;IACH,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC;AAC3D,CAAC"}
|
|
@@ -17,8 +17,14 @@
|
|
|
17
17
|
* filed as a claim — its own recorded step, naming every `runtimeStageId` it
|
|
18
18
|
* folded. A drop is an ABSENCE, and it is filed the same way: the record and
|
|
19
19
|
* the eviction events name what left, by id.
|
|
20
|
+
*
|
|
21
|
+
* That law holds for as long as the PROCESS holds — a commit log is memory.
|
|
22
|
+
* A conversation outlives its process, so the durable half of the same law
|
|
23
|
+
* lives on the conversation checkpoint: `.compaction({ retain })` carries the
|
|
24
|
+
* folded originals there, beside the summary that stands for them. See
|
|
25
|
+
* {@link CompactionRetention} and {@link FoldedSpan}.
|
|
20
26
|
*/
|
|
21
|
-
import type { LLMProvider } from '../../../adapters/types.js';
|
|
27
|
+
import type { LLMMessage, LLMProvider } from '../../../adapters/types.js';
|
|
22
28
|
/**
|
|
23
29
|
* Why a turn refused to leave the window. Every one of these is NAMED in the
|
|
24
30
|
* commit — a removal that took less than it could have has to say why, or the
|
|
@@ -169,6 +175,87 @@ export interface TokenBudgetRecord extends WindowRecord {
|
|
|
169
175
|
/** How many recent turns were off-limits to this visit. */
|
|
170
176
|
readonly keepRecentTurns: number;
|
|
171
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* What becomes of the messages a fold removes from the window.
|
|
180
|
+
*
|
|
181
|
+
* The commit log keeps them for as long as the PROCESS keeps them, and that
|
|
182
|
+
* is the whole of what 8.1 offered. A standing agent outlives its process, so
|
|
183
|
+
* "the folded turns are still in the commit log" stops being true the moment
|
|
184
|
+
* the run ends — and the summary sitting in the restored conversation went on
|
|
185
|
+
* claiming otherwise. Retention is the fix: the originals ride with the
|
|
186
|
+
* CONVERSATION, which is the thing that actually survives.
|
|
187
|
+
*
|
|
188
|
+
* The trade is stated rather than hidden: **compaction shrinks the wire, not
|
|
189
|
+
* the record.** A stored session grows as it folds. That is the right way
|
|
190
|
+
* round — the model's context window is scarce and a session row is not.
|
|
191
|
+
*/
|
|
192
|
+
export type CompactionRetention =
|
|
193
|
+
/**
|
|
194
|
+
* The folded messages ride with the conversation checkpoint, so a restart —
|
|
195
|
+
* a new process, a new machine, a deploy — can still produce them verbatim.
|
|
196
|
+
* The default: originals are never destroyed unless you say so.
|
|
197
|
+
*/
|
|
198
|
+
'conversation'
|
|
199
|
+
/**
|
|
200
|
+
* The folded messages are NOT carried forward. The span is still recorded,
|
|
201
|
+
* naming what left and how much of it, because a discard is an absence and
|
|
202
|
+
* this family files absences the same way it files claims. Choose it when
|
|
203
|
+
* the conversation is the only thing you want to keep and the storage cost
|
|
204
|
+
* of the transcript is not worth paying.
|
|
205
|
+
*/
|
|
206
|
+
| 'discard';
|
|
207
|
+
/**
|
|
208
|
+
* One fold, as it survives the process: the summary's fingerprint, what it
|
|
209
|
+
* stands for, and — under `retain: 'conversation'` — the messages themselves.
|
|
210
|
+
*
|
|
211
|
+
* These accumulate on the conversation checkpoint across every turn and every
|
|
212
|
+
* restart, so a standing agent that folded week one in April can still produce
|
|
213
|
+
* week one in July.
|
|
214
|
+
*
|
|
215
|
+
* ## Joining a span to its summary
|
|
216
|
+
*
|
|
217
|
+
* By CONTENT FINGERPRINT, never by index: a later fold can swallow an earlier
|
|
218
|
+
* summary, and every index in the window moves when it does. {@link
|
|
219
|
+
* foldedSpanFor} does the join for you.
|
|
220
|
+
*/
|
|
221
|
+
export interface FoldedSpan {
|
|
222
|
+
/**
|
|
223
|
+
* Fingerprint of the summary message this span was folded into — the join
|
|
224
|
+
* key back to the message sitting in `history`.
|
|
225
|
+
*
|
|
226
|
+
* It is a hash of the message's full content (authored frame included), so
|
|
227
|
+
* it costs no extra bytes on the wire and cannot be forged by a summary that
|
|
228
|
+
* merely copies the frame's opening words: different content, different
|
|
229
|
+
* fingerprint, no match.
|
|
230
|
+
*/
|
|
231
|
+
readonly summaryFingerprint: string;
|
|
232
|
+
/**
|
|
233
|
+
* The run whose commit log held these messages. Diagnostic, and the honest
|
|
234
|
+
* answer to "where else could I have found this?" — that log is gone with
|
|
235
|
+
* the process, which is why the messages are here.
|
|
236
|
+
*/
|
|
237
|
+
readonly runId: string;
|
|
238
|
+
/** ReAct iteration the fold happened at, in that run. */
|
|
239
|
+
readonly iteration: number;
|
|
240
|
+
/** Wall clock of the fold. */
|
|
241
|
+
readonly foldedAtMs: number;
|
|
242
|
+
/** The model that wrote the summary — a claim's author is part of the claim. */
|
|
243
|
+
readonly model: string;
|
|
244
|
+
/** How many messages the summary stands for. Always recorded, both policies. */
|
|
245
|
+
readonly messageCount: number;
|
|
246
|
+
/** `runtimeStageId`s of the stages that appended those messages. */
|
|
247
|
+
readonly removedStageIds: readonly string[];
|
|
248
|
+
/** Which policy this fold ran under. */
|
|
249
|
+
readonly retained: CompactionRetention;
|
|
250
|
+
/**
|
|
251
|
+
* The folded messages, verbatim and in order. Present exactly when
|
|
252
|
+
* `retained` is `'conversation'`.
|
|
253
|
+
*
|
|
254
|
+
* Absent under `'discard'` — and absent is the honest shape there, rather
|
|
255
|
+
* than an empty array that reads like "there were none".
|
|
256
|
+
*/
|
|
257
|
+
readonly messages?: readonly LLMMessage[];
|
|
258
|
+
}
|
|
172
259
|
/**
|
|
173
260
|
* What `.compaction({...})` — and `summarizeOldest({...})` — accepts.
|
|
174
261
|
*
|
|
@@ -210,6 +297,23 @@ export interface CompactionOptions {
|
|
|
210
297
|
* `summarizer: anthropic()` alone works; name a cheap model to spend less.
|
|
211
298
|
*/
|
|
212
299
|
readonly model?: string;
|
|
300
|
+
/**
|
|
301
|
+
* What happens to the messages a fold removes. Default `'conversation'` —
|
|
302
|
+
* they ride with the conversation checkpoint and survive the process.
|
|
303
|
+
*
|
|
304
|
+
* Pass `'discard'` to opt out. Nothing is ever destroyed silently: the only
|
|
305
|
+
* way to lose the originals is to name this.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```ts
|
|
309
|
+
* .compaction({
|
|
310
|
+
* thresholdTokens: 120_000,
|
|
311
|
+
* summarizer: anthropic(),
|
|
312
|
+
* retain: 'conversation', // the default, spelled out
|
|
313
|
+
* })
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
readonly retain?: CompactionRetention;
|
|
213
317
|
}
|
|
214
318
|
/** Resolved form — defaults applied at build time, validated once. */
|
|
215
319
|
export interface ResolvedCompaction {
|
|
@@ -217,6 +321,7 @@ export interface ResolvedCompaction {
|
|
|
217
321
|
readonly keepRecentTurns: number;
|
|
218
322
|
readonly summarizer: LLMProvider;
|
|
219
323
|
readonly model: string | undefined;
|
|
324
|
+
readonly retain: CompactionRetention;
|
|
220
325
|
}
|
|
221
326
|
/**
|
|
222
327
|
* What `slidingWindow({...})` accepts.
|
|
@@ -17,6 +17,12 @@
|
|
|
17
17
|
* filed as a claim — its own recorded step, naming every `runtimeStageId` it
|
|
18
18
|
* folded. A drop is an ABSENCE, and it is filed the same way: the record and
|
|
19
19
|
* the eviction events name what left, by id.
|
|
20
|
+
*
|
|
21
|
+
* That law holds for as long as the PROCESS holds — a commit log is memory.
|
|
22
|
+
* A conversation outlives its process, so the durable half of the same law
|
|
23
|
+
* lives on the conversation checkpoint: `.compaction({ retain })` carries the
|
|
24
|
+
* folded originals there, beside the summary that stands for them. See
|
|
25
|
+
* {@link CompactionRetention} and {@link FoldedSpan}.
|
|
20
26
|
*/
|
|
21
27
|
export {};
|
|
22
28
|
//# sourceMappingURL=types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/core/agent/window/types.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/core/agent/window/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG"}
|
|
@@ -54,6 +54,7 @@
|
|
|
54
54
|
* DFS stage).
|
|
55
55
|
*/
|
|
56
56
|
import type { LLMMessage } from '../adapters/types.js';
|
|
57
|
+
import type { FoldedSpan } from './agent/window/types.js';
|
|
57
58
|
/**
|
|
58
59
|
* JSON-serializable checkpoint of an in-progress agent run. Persist
|
|
59
60
|
* to ANY durable store (Redis / Postgres / S3 / disk / queue) and
|
|
@@ -85,6 +86,29 @@ export interface AgentRunCheckpoint {
|
|
|
85
86
|
};
|
|
86
87
|
/** Wall-clock when the checkpoint was captured. Diagnostic only. */
|
|
87
88
|
readonly checkpointedAt: number;
|
|
89
|
+
/**
|
|
90
|
+
* Every span this conversation folded into a summary, oldest first — what
|
|
91
|
+
* makes a compacted conversation still a provable one after the process
|
|
92
|
+
* that compacted it is gone.
|
|
93
|
+
*
|
|
94
|
+
* Written by `.compaction()`; absent on a conversation that never folded,
|
|
95
|
+
* and absent on one stored by a runtime older than 8.2. Under the default
|
|
96
|
+
* `retain: 'conversation'` each span carries the folded messages verbatim;
|
|
97
|
+
* under `retain: 'discard'` the span is still here, naming what left, and
|
|
98
|
+
* only `messages` is absent.
|
|
99
|
+
*
|
|
100
|
+
* Join a summary in {@link history} to its span with `foldedSpanFor(...)` —
|
|
101
|
+
* by content fingerprint, never by index, because a later fold moves every
|
|
102
|
+
* index after it.
|
|
103
|
+
*
|
|
104
|
+
* **Version 1 still, deliberately.** An optional field is not a format
|
|
105
|
+
* change: a runtime that has never heard of `folded` reads this checkpoint,
|
|
106
|
+
* ignores it, and continues the conversation correctly — the summary is an
|
|
107
|
+
* ordinary message in `history` either way. Bumping the version would make
|
|
108
|
+
* an older deployment REFUSE a session it can serve perfectly well, which is
|
|
109
|
+
* the opposite of what the version field is for.
|
|
110
|
+
*/
|
|
111
|
+
readonly folded?: readonly FoldedSpan[];
|
|
88
112
|
/** Where the failure happened. Diagnostic — surfaces in oncall
|
|
89
113
|
* triage so you can tell "LLM 500 mid-iteration" from "tool
|
|
90
114
|
* threw" from "validation kept failing". */
|
|
@@ -159,7 +183,14 @@ export declare function buildCheckpoint(tracker: RunCheckpointTracker, failurePo
|
|
|
159
183
|
phase: AgentRunCheckpoint['failurePoint'] extends infer F ? F extends {
|
|
160
184
|
phase: infer P;
|
|
161
185
|
} ? P : never : never;
|
|
162
|
-
}
|
|
186
|
+
},
|
|
187
|
+
/**
|
|
188
|
+
* Spans this run folded, read from committed state by the caller. Passed in
|
|
189
|
+
* rather than tracked, because the tracker follows `history` through events
|
|
190
|
+
* and a fold's span is committed state — and because the same reader then
|
|
191
|
+
* serves both checkpoint carriers, so neither can lose what the other keeps.
|
|
192
|
+
*/
|
|
193
|
+
folded?: readonly FoldedSpan[]): AgentRunCheckpoint;
|
|
163
194
|
/**
|
|
164
195
|
* Validate a checkpoint at deserialization time. Catches forward-
|
|
165
196
|
* incompatible payloads (someone tries to resume a v3 checkpoint on
|
|
@@ -103,7 +103,14 @@ export class RunCheckpointError extends Error {
|
|
|
103
103
|
*
|
|
104
104
|
* @internal
|
|
105
105
|
*/
|
|
106
|
-
export function buildCheckpoint(tracker, failurePoint
|
|
106
|
+
export function buildCheckpoint(tracker, failurePoint,
|
|
107
|
+
/**
|
|
108
|
+
* Spans this run folded, read from committed state by the caller. Passed in
|
|
109
|
+
* rather than tracked, because the tracker follows `history` through events
|
|
110
|
+
* and a fold's span is committed state — and because the same reader then
|
|
111
|
+
* serves both checkpoint carriers, so neither can lose what the other keeps.
|
|
112
|
+
*/
|
|
113
|
+
folded) {
|
|
107
114
|
return {
|
|
108
115
|
version: 1,
|
|
109
116
|
runId: tracker.runId,
|
|
@@ -112,6 +119,7 @@ export function buildCheckpoint(tracker, failurePoint) {
|
|
|
112
119
|
originalInput: tracker.originalInput,
|
|
113
120
|
checkpointedAt: Date.now(),
|
|
114
121
|
...(failurePoint && { failurePoint }),
|
|
122
|
+
...(folded !== undefined && folded.length > 0 && { folded }),
|
|
115
123
|
};
|
|
116
124
|
}
|
|
117
125
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runCheckpoint.js","sourceRoot":"","sources":["../../../src/core/runCheckpoint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;
|
|
1
|
+
{"version":3,"file":"runCheckpoint.js","sourceRoot":"","sources":["../../../src/core/runCheckpoint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AAoEH;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAClC,IAAI,GAAG,oBAA6B,CAAC;IAC9C;;6BAEyB;IACP,KAAK,CAAQ;IAC/B;mEAC+D;IACtD,UAAU,CAAqB;IAExC,YAAY,KAAY,EAAE,UAA8B;QACtD,MAAM,KAAK,GAAG,UAAU,CAAC,YAAY,EAAE,KAAK,IAAI,SAAS,CAAC;QAC1D,KAAK,CACH,mCAAmC,UAAU,CAAC,YAAY,EAAE,SAAS,IAAI,GAAG,KA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put, type LLMCallOptions, type L
|
|
|
31
31
|
export { type MessageApiChartDeps } from './core/agent/buildMessageApiChart.js';
|
|
32
32
|
export { buildAgentMessageApiChart, type AgentMessageApiChartDeps, } from './core/agent/buildAgentMessageApiChart.js';
|
|
33
33
|
export { Agent, AgentBuilder, type AgentInput, type AgentOptions, type AgentOutput, type ObserverDeliveryOptions, } from './core/Agent.js';
|
|
34
|
-
export { CompactionUnmeasurableError, COMPACTED_FRAME_PREFIX, DROP_NOTICE_PREFIX, isCompactedSummary, isDropNotice, slidingWindow, summarizeOldest, tokenBudget, type CompactionOptions, type CompactionRecord, type FoldRefusal, type FoldRefusalReason, type RemovalFacts, type RemovalPlan, type SlidingWindowOptions, type SlidingWindowRecord, type TokenBudgetOptions, type TokenBudgetRecord, type Turn, type WindowEviction, type WindowRecord, type WindowRefusal, type WindowRefusalReason, type WindowStrategy, type WindowStrategyInput, type WindowStrategyResult, } from './core/agent/window/index.js';
|
|
34
|
+
export { CompactionUnmeasurableError, COMPACTED_FRAME_PREFIX, DROP_NOTICE_PREFIX, foldedMessages, foldedSpanFor, isCompactedSummary, isDropNotice, slidingWindow, summarizeOldest, tokenBudget, type CompactionOptions, type CompactionRecord, type CompactionRetention, type FoldedConversation, type FoldedSpan, type FoldRefusal, type FoldRefusalReason, type RemovalFacts, type RemovalPlan, type SlidingWindowOptions, type SlidingWindowRecord, type TokenBudgetOptions, type TokenBudgetRecord, type Turn, type WindowEviction, type WindowRecord, type WindowRefusal, type WindowRefusalReason, type WindowStrategy, type WindowStrategyInput, type WindowStrategyResult, } from './core/agent/window/index.js';
|
|
35
35
|
export type { SelfExplainOptions } from './lib/trace-toolpack/selfExplain.js';
|
|
36
36
|
export type { ObserverDrainResult, ObserverStats } from 'footprintjs';
|
|
37
37
|
export type { ToolArgValidationMode } from './core/agent/toolArgsValidation.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -112,7 +112,12 @@ export { Agent, AgentBuilder, } from './core/Agent.js';
|
|
|
112
112
|
// summarizeOldest fold the oldest span into a summary (what `.compaction()` is)
|
|
113
113
|
// slidingWindow keep the last N turns, drop older ones
|
|
114
114
|
// tokenBudget counted-token trigger, drop instead of summarize
|
|
115
|
-
|
|
115
|
+
//
|
|
116
|
+
// A commit log lives as long as the process; a conversation lives longer. So
|
|
117
|
+
// `.compaction({ retain })` carries the folded originals on the conversation
|
|
118
|
+
// checkpoint too, and `foldedSpanFor(conversation, message)` is the door back
|
|
119
|
+
// to them after a restart.
|
|
120
|
+
export { CompactionUnmeasurableError, COMPACTED_FRAME_PREFIX, DROP_NOTICE_PREFIX, foldedMessages, foldedSpanFor, isCompactedSummary, isDropNotice, slidingWindow, summarizeOldest, tokenBudget, } from './core/agent/window/index.js';
|
|
116
121
|
export { OutputSchemaError, applyOutputSchema, } from './core/outputSchema.js';
|
|
117
122
|
// 7.26 — the loop's side of the output contract. `OutputAttempt` is the row
|
|
118
123
|
// shape `snapshot.sharedState.outputAttempts` carries; the frame prefix and
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,oEAAoE;AACpE,8DAA8D;AAC9D,uEAAuE;AACvE,kEAAkE;AAClE,sEAAsE;AACtE,sDAAsD;AACtD,OAAO,8CAA8C,CAAC;AACtD,OAAO,2CAA2C,CAAC;AACnD,OAAO,4CAA4C,CAAC;AAsCpD,6BAA6B;AAC7B,cAAc,qBAAqB,CAAC;AAEpC,yEAAyE;AACzE,uDAAuD;AACvD,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,cAAc;AAEd,yEAAyE;AACzE,wEAAwE;AACxE,yEAAyE;AACzE,gBAAgB;AAChB,SAAS;AAET,2EAA2E;AAC3E,6EAA6E;AAC7E,6EAA6E;AAC7E,6DAA6D;AAC7D,YAAY,GAGb,MAAM,kBAAkB,CAAC;AAC1B,oEAAoE;AACpE,yEAAyE;AACzE,wE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C7E,OAAO,EACL,eAAe,GAIhB,MAAM,qCAAqC,CAAC;AAY7C,qEAAqE;AACrE,kEAAkE;AAClE,kEAAkE;AAClE,OAAO,EACL,0BAA0B;AAC1B,kEAAkE;AAClE,mEAAmE;AACnE,oDAAoD;AACpD,gBAAgB,EAChB,qBAAqB,EACrB,gBAAgB,EAChB,mBAAmB,GAGpB,MAAM,6DAA6D,CAAC;AAErE,gEAAgE;AAChE,sEAAsE;AACtE,uDAAuD;AACvD,sEAAsE;AACtE,+DAA+D;AAC/D,gEAAgE;AAChE,wEAAwE;AACxE,mCAAmC;AAEnC,qBAAqB;AACrB,OAAO,EACL,OAAO,EACP,cAAc,GAIf,MAAM,mBAAmB,CAAC;AAO3B,2EAA2E;AAC3E,gFAAgF;AAChF,4EAA4E;AAC5E,2EAA2E;AAC3E,OAAO,EACL,yBAAyB,GAE1B,MAAM,2CAA2C,CAAC;AACnD,OAAO,EACL,KAAK,EACL,YAAY,GAKb,MAAM,iBAAiB,CAAC;AACzB,8EAA8E;AAC9E,8EAA8E;AAC9E,+EAA+E;AAC/E,oEAAoE;AACpE,EAAE;AACF,iEAAiE;AACjE,oFAAoF;AACpF,4DAA4D;AAC5D,sEAAsE;AACtE,OAAO,EACL,2BAA2B,EAC3B,sBAAsB,EACtB,kBAAkB,EAClB,kBAAkB,EAClB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,WAAW,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,oEAAoE;AACpE,8DAA8D;AAC9D,uEAAuE;AACvE,kEAAkE;AAClE,sEAAsE;AACtE,sDAAsD;AACtD,OAAO,8CAA8C,CAAC;AACtD,OAAO,2CAA2C,CAAC;AACnD,OAAO,4CAA4C,CAAC;AAsCpD,6BAA6B;AAC7B,cAAc,qBAAqB,CAAC;AAEpC,yEAAyE;AACzE,uDAAuD;AACvD,OAAO,EACL,cAAc,EACd,mBAAmB,E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;AAE/D,oEAAoE;AACpE,gEAAgE;AAEhE,qEAAqE;AACrE,0DAA0D;AAC1D,cAAc,qBAAqB,CAAC;AAEpC,uEAAuE;AACvE,uEAAuE;AACvE,oEAAoE;AACpE,sEAAsE;AACtE,8CAA8C;AAE9C,wEAAwE;AACxE,sEAAsE;AACtE,sEAAsE;AACtE,OAAO,EACL,SAAS,EAET,cAAc,GAGf,MAAM,oBAAoB,CAAC;AAE5B,qEAAqE;AACrE,wEAAwE;AACxE,uEAAuE;AACvE,oEAAoE;AACpE,8DAA8D;AAC9D,wEAAwE;AACxE,gDAAgD;AAChD,wEAAwE;AACxE,gEAAgE;AAEhE,wEAAwE;AACxE,wEAAwE;AACxE,uDAAuD;AAEvD,mEAAmE;AACnE,oEAAoE;AACpE,sDAAsD"}
|
package/dist/esm/providers.d.ts
CHANGED
|
@@ -14,18 +14,26 @@
|
|
|
14
14
|
*
|
|
15
15
|
* What's here today:
|
|
16
16
|
* • `mock` / `MockProvider` — deterministic + realistic-mode mock
|
|
17
|
+
* • `ollama` / `OllamaProvider` — local models, free, no API key
|
|
17
18
|
* • `anthropic` / `AnthropicProvider` — real provider (Claude)
|
|
18
19
|
* • `openai` / `OpenAIProvider` — real provider (GPT)
|
|
20
|
+
* • `bedrock`, `azureOpenai`, and the `browser*` variants
|
|
21
|
+
*
|
|
22
|
+
* The ladder these are FOR: `mock()` while you shape the logic →
|
|
23
|
+
* `ollama('<model>')` for a real model that costs nothing and needs no
|
|
24
|
+
* key → a paid API in production. The agent code above them does not
|
|
25
|
+
* change between the three; that is the whole point of the port.
|
|
19
26
|
*
|
|
20
27
|
* Bring your own (BYO):
|
|
21
|
-
* For
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
28
|
+
* For Cohere / on-prem / fine-tuned models, implement the
|
|
29
|
+
* `LLMProvider` interface (see `LLMProvider` exported from the main
|
|
30
|
+
* barrel) — `complete()` is required, `stream()` is optional. The
|
|
31
|
+
* `MockProvider` source is the canonical reference.
|
|
25
32
|
*/
|
|
26
33
|
export { MockProvider, mock, type MockProviderOptions, type MockReply, type LatencyMs, } from './adapters/llm/MockProvider.js';
|
|
27
34
|
export { anthropic, AnthropicProvider, type AnthropicProviderOptions, } from './adapters/llm/AnthropicProvider.js';
|
|
28
|
-
export { openai, OpenAIProvider,
|
|
35
|
+
export { openai, OpenAIProvider, azureOpenai, type OpenAIProviderOptions, type AzureOpenAIProviderOptions, } from './adapters/llm/OpenAIProvider.js';
|
|
36
|
+
export { ollama, OllamaProvider, OllamaUnavailableError, type OllamaProviderOptions, type ThinkLevel, } from './adapters/llm/OllamaProvider.js';
|
|
29
37
|
export { bedrock, BedrockProvider, type BedrockProviderOptions, } from './adapters/llm/BedrockProvider.js';
|
|
30
38
|
export { browserAnthropic, BrowserAnthropicProvider, type BrowserAnthropicProviderOptions, } from './adapters/llm/BrowserAnthropicProvider.js';
|
|
31
39
|
export { browserOpenai, BrowserOpenAIProvider, type BrowserOpenAIProviderOptions, browserAzureOpenai, BrowserAzureOpenAIProvider, type BrowserAzureOpenAIProviderOptions, } from './adapters/llm/BrowserOpenAIProvider.js';
|
package/dist/esm/providers.js
CHANGED
|
@@ -14,18 +14,26 @@
|
|
|
14
14
|
*
|
|
15
15
|
* What's here today:
|
|
16
16
|
* • `mock` / `MockProvider` — deterministic + realistic-mode mock
|
|
17
|
+
* • `ollama` / `OllamaProvider` — local models, free, no API key
|
|
17
18
|
* • `anthropic` / `AnthropicProvider` — real provider (Claude)
|
|
18
19
|
* • `openai` / `OpenAIProvider` — real provider (GPT)
|
|
20
|
+
* • `bedrock`, `azureOpenai`, and the `browser*` variants
|
|
21
|
+
*
|
|
22
|
+
* The ladder these are FOR: `mock()` while you shape the logic →
|
|
23
|
+
* `ollama('<model>')` for a real model that costs nothing and needs no
|
|
24
|
+
* key → a paid API in production. The agent code above them does not
|
|
25
|
+
* change between the three; that is the whole point of the port.
|
|
19
26
|
*
|
|
20
27
|
* Bring your own (BYO):
|
|
21
|
-
* For
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
28
|
+
* For Cohere / on-prem / fine-tuned models, implement the
|
|
29
|
+
* `LLMProvider` interface (see `LLMProvider` exported from the main
|
|
30
|
+
* barrel) — `complete()` is required, `stream()` is optional. The
|
|
31
|
+
* `MockProvider` source is the canonical reference.
|
|
25
32
|
*/
|
|
26
33
|
export { MockProvider, mock, } from './adapters/llm/MockProvider.js';
|
|
27
34
|
export { anthropic, AnthropicProvider, } from './adapters/llm/AnthropicProvider.js';
|
|
28
|
-
export { openai, OpenAIProvider,
|
|
35
|
+
export { openai, OpenAIProvider, azureOpenai, } from './adapters/llm/OpenAIProvider.js';
|
|
36
|
+
export { ollama, OllamaProvider, OllamaUnavailableError, } from './adapters/llm/OllamaProvider.js';
|
|
29
37
|
export { bedrock, BedrockProvider, } from './adapters/llm/BedrockProvider.js';
|
|
30
38
|
export { browserAnthropic, BrowserAnthropicProvider, } from './adapters/llm/BrowserAnthropicProvider.js';
|
|
31
39
|
export { browserOpenai, BrowserOpenAIProvider, browserAzureOpenai, BrowserAzureOpenAIProvider, } from './adapters/llm/BrowserOpenAIProvider.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"providers.js","sourceRoot":"","sources":["../../src/providers.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"providers.js","sourceRoot":"","sources":["../../src/providers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,EACL,YAAY,EACZ,IAAI,GAIL,MAAM,gCAAgC,CAAC;AAExC,OAAO,EACL,SAAS,EACT,iBAAiB,GAElB,MAAM,qCAAqC,CAAC;AAE7C,OAAO,EACL,MAAM,EACN,cAAc,EACd,WAAW,GAGZ,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,MAAM,EACN,cAAc,EACd,sBAAsB,GAGvB,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,OAAO,EACP,eAAe,GAEhB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,gBAAgB,EAChB,wBAAwB,GAEzB,MAAM,4CAA4C,CAAC;AAEpD,OAAO,EACL,aAAa,EACb,qBAAqB,EAErB,kBAAkB,EAClB,0BAA0B,GAE3B,MAAM,yCAAyC,CAAC;AAEjD,OAAO,EACL,cAAc,GAGf,MAAM,kCAAkC,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OllamaThinkingHandler — normalizes a local reasoning model's thinking
|
|
3
|
+
* into the framework's `ThinkingBlock[]` contract.
|
|
4
|
+
*
|
|
5
|
+
* Local reasoning models (deepseek-r1, qwen3, gpt-oss, …) reason in the
|
|
6
|
+
* open, and where that reasoning LANDS depends on how they were asked:
|
|
7
|
+
*
|
|
8
|
+
* 1. ASKED (`ollama('qwen3', { think: true })`, or the agent's
|
|
9
|
+
* `.thinking({ budget })`) — Ollama lifts the reasoning out of the
|
|
10
|
+
* answer into `message.thinking`. The adapter forwards it as
|
|
11
|
+
* `{ kind: 'field', thinking }`. This is the good path.
|
|
12
|
+
*
|
|
13
|
+
* 2. NOT ASKED — the same model writes `<think>…</think>` straight into
|
|
14
|
+
* the answer text. The adapter recognizes the shape and forwards the
|
|
15
|
+
* whole answer as `{ kind: 'inline', content }`.
|
|
16
|
+
*
|
|
17
|
+
* **The library recognizes the inline shape; it never rewrites the
|
|
18
|
+
* answer.** Blocks produced from case 2 describe text that is STILL
|
|
19
|
+
* present, verbatim, in `LLMResponse.content` — because silently editing
|
|
20
|
+
* a model's answer is a meaning change, and that is the application's
|
|
21
|
+
* decision to make, not this library's. If you want the reasoning out of
|
|
22
|
+
* the answer, ask for it: turn `think` on, and the model stops putting it
|
|
23
|
+
* there.
|
|
24
|
+
*
|
|
25
|
+
* **No signature** — nothing on this wire is signed, so there is no
|
|
26
|
+
* round-trip integrity invariant (unlike Anthropic). `signature` stays
|
|
27
|
+
* undefined.
|
|
28
|
+
*
|
|
29
|
+
* **No `summary` flag** — this is raw reasoning, not a structured summary
|
|
30
|
+
* (unlike OpenAI's `reasoning_summary`).
|
|
31
|
+
*
|
|
32
|
+
* **`parseChunk`** — the adapter already emits `LLMChunk.thinkingDelta`
|
|
33
|
+
* directly while streaming (Ollama sends `message.thinking` deltas frame
|
|
34
|
+
* by frame), so the framework does not need this to see live reasoning.
|
|
35
|
+
* It is implemented anyway for consumers driving the handler themselves.
|
|
36
|
+
*/
|
|
37
|
+
import type { ThinkingHandler } from './types.js';
|
|
38
|
+
/**
|
|
39
|
+
* What `OllamaProvider` puts on `LLMResponse.rawThinking`.
|
|
40
|
+
*
|
|
41
|
+
* Tagged rather than a bare string so the handler never has to GUESS which
|
|
42
|
+
* of the two situations it is looking at — and so a consumer reading the
|
|
43
|
+
* raw value can tell whether the reasoning is also sitting in the answer.
|
|
44
|
+
*/
|
|
45
|
+
export type OllamaRawThinking = {
|
|
46
|
+
readonly kind: 'field';
|
|
47
|
+
readonly thinking: string;
|
|
48
|
+
} | {
|
|
49
|
+
readonly kind: 'inline';
|
|
50
|
+
readonly content: string;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Pull the reasoning out of text that carries `<think>` tags.
|
|
54
|
+
*
|
|
55
|
+
* All tag PARSING lives here, in the handler — the adapter only notices
|
|
56
|
+
* that a `<think` substring is present and tags the payload accordingly.
|
|
57
|
+
* Exported so a consumer driving normalization by hand can reuse it.
|
|
58
|
+
*/
|
|
59
|
+
export declare function extractInlineThinking(text: string): readonly string[];
|
|
60
|
+
export declare const ollamaThinkingHandler: ThinkingHandler;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OllamaThinkingHandler — normalizes a local reasoning model's thinking
|
|
3
|
+
* into the framework's `ThinkingBlock[]` contract.
|
|
4
|
+
*
|
|
5
|
+
* Local reasoning models (deepseek-r1, qwen3, gpt-oss, …) reason in the
|
|
6
|
+
* open, and where that reasoning LANDS depends on how they were asked:
|
|
7
|
+
*
|
|
8
|
+
* 1. ASKED (`ollama('qwen3', { think: true })`, or the agent's
|
|
9
|
+
* `.thinking({ budget })`) — Ollama lifts the reasoning out of the
|
|
10
|
+
* answer into `message.thinking`. The adapter forwards it as
|
|
11
|
+
* `{ kind: 'field', thinking }`. This is the good path.
|
|
12
|
+
*
|
|
13
|
+
* 2. NOT ASKED — the same model writes `<think>…</think>` straight into
|
|
14
|
+
* the answer text. The adapter recognizes the shape and forwards the
|
|
15
|
+
* whole answer as `{ kind: 'inline', content }`.
|
|
16
|
+
*
|
|
17
|
+
* **The library recognizes the inline shape; it never rewrites the
|
|
18
|
+
* answer.** Blocks produced from case 2 describe text that is STILL
|
|
19
|
+
* present, verbatim, in `LLMResponse.content` — because silently editing
|
|
20
|
+
* a model's answer is a meaning change, and that is the application's
|
|
21
|
+
* decision to make, not this library's. If you want the reasoning out of
|
|
22
|
+
* the answer, ask for it: turn `think` on, and the model stops putting it
|
|
23
|
+
* there.
|
|
24
|
+
*
|
|
25
|
+
* **No signature** — nothing on this wire is signed, so there is no
|
|
26
|
+
* round-trip integrity invariant (unlike Anthropic). `signature` stays
|
|
27
|
+
* undefined.
|
|
28
|
+
*
|
|
29
|
+
* **No `summary` flag** — this is raw reasoning, not a structured summary
|
|
30
|
+
* (unlike OpenAI's `reasoning_summary`).
|
|
31
|
+
*
|
|
32
|
+
* **`parseChunk`** — the adapter already emits `LLMChunk.thinkingDelta`
|
|
33
|
+
* directly while streaming (Ollama sends `message.thinking` deltas frame
|
|
34
|
+
* by frame), so the framework does not need this to see live reasoning.
|
|
35
|
+
* It is implemented anyway for consumers driving the handler themselves.
|
|
36
|
+
*/
|
|
37
|
+
/** `<think>…</think>`, including an unclosed one from a truncated answer. */
|
|
38
|
+
const THINK_TAG = /<think>([\s\S]*?)(?:<\/think>|$)/gi;
|
|
39
|
+
/**
|
|
40
|
+
* Pull the reasoning out of text that carries `<think>` tags.
|
|
41
|
+
*
|
|
42
|
+
* All tag PARSING lives here, in the handler — the adapter only notices
|
|
43
|
+
* that a `<think` substring is present and tags the payload accordingly.
|
|
44
|
+
* Exported so a consumer driving normalization by hand can reuse it.
|
|
45
|
+
*/
|
|
46
|
+
export function extractInlineThinking(text) {
|
|
47
|
+
if (!text.includes('<think'))
|
|
48
|
+
return [];
|
|
49
|
+
const out = [];
|
|
50
|
+
// `matchAll` needs the /g flag and a fresh lastIndex per call.
|
|
51
|
+
THINK_TAG.lastIndex = 0;
|
|
52
|
+
for (const match of text.matchAll(THINK_TAG)) {
|
|
53
|
+
const inner = (match[1] ?? '').trim();
|
|
54
|
+
if (inner.length > 0)
|
|
55
|
+
out.push(inner);
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
function isRawThinking(raw) {
|
|
60
|
+
return typeof raw === 'object' && raw !== null && 'kind' in raw;
|
|
61
|
+
}
|
|
62
|
+
export const ollamaThinkingHandler = {
|
|
63
|
+
id: 'ollama',
|
|
64
|
+
providerNames: ['ollama'],
|
|
65
|
+
normalize(raw) {
|
|
66
|
+
if (raw === undefined || raw === null)
|
|
67
|
+
return [];
|
|
68
|
+
// Bare string — a hand-fed value, or a consumer-authored adapter that
|
|
69
|
+
// passes `message.thinking` straight through. Treat it as reasoning,
|
|
70
|
+
// but still honor tags if they are in there.
|
|
71
|
+
if (typeof raw === 'string')
|
|
72
|
+
return blocksFromText(raw);
|
|
73
|
+
if (isRawThinking(raw)) {
|
|
74
|
+
if (raw.kind === 'field') {
|
|
75
|
+
return typeof raw.thinking === 'string' ? blocksFromText(raw.thinking) : [];
|
|
76
|
+
}
|
|
77
|
+
if (raw.kind === 'inline') {
|
|
78
|
+
if (typeof raw.content !== 'string')
|
|
79
|
+
return [];
|
|
80
|
+
return extractInlineThinking(raw.content).map(toBlock);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Unknown shape — empty rather than throw. The framework catches throws
|
|
84
|
+
// and emits parse_failed; graceful empty is the right answer for a
|
|
85
|
+
// wire that may grow fields we have not seen yet.
|
|
86
|
+
return [];
|
|
87
|
+
},
|
|
88
|
+
parseChunk(chunk) {
|
|
89
|
+
if (typeof chunk !== 'object' || chunk === null)
|
|
90
|
+
return {};
|
|
91
|
+
const message = chunk.message;
|
|
92
|
+
const thinking = message?.thinking;
|
|
93
|
+
return typeof thinking === 'string' && thinking.length > 0 ? { thinkingDelta: thinking } : {};
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
/** Text → blocks. Tagged text yields one block per tag; plain text one block. */
|
|
97
|
+
function blocksFromText(text) {
|
|
98
|
+
const tagged = extractInlineThinking(text);
|
|
99
|
+
if (tagged.length > 0)
|
|
100
|
+
return tagged.map(toBlock);
|
|
101
|
+
const trimmed = text.trim();
|
|
102
|
+
return trimmed.length > 0 ? [toBlock(trimmed)] : [];
|
|
103
|
+
}
|
|
104
|
+
function toBlock(content) {
|
|
105
|
+
return { type: 'thinking', content };
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=OllamaThinkingHandler.js.map
|