thatgfsj-code 2.2.9 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -67
- package/dist/app/index.d.ts +28 -5
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +76 -10
- package/dist/app/index.js.map +1 -1
- package/dist/cache/fingerprint.d.ts +49 -0
- package/dist/cache/fingerprint.d.ts.map +1 -0
- package/dist/cache/fingerprint.js +100 -0
- package/dist/cache/fingerprint.js.map +1 -0
- package/dist/cache/index.d.ts +15 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +15 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/smartModel.d.ts +40 -0
- package/dist/cache/smartModel.d.ts.map +1 -0
- package/dist/cache/smartModel.js +43 -0
- package/dist/cache/smartModel.js.map +1 -0
- package/dist/cache/stats.d.ts +93 -0
- package/dist/cache/stats.d.ts.map +1 -0
- package/dist/cache/stats.js +155 -0
- package/dist/cache/stats.js.map +1 -0
- package/dist/cache/volatile.d.ts +32 -0
- package/dist/cache/volatile.d.ts.map +1 -0
- package/dist/cache/volatile.js +44 -0
- package/dist/cache/volatile.js.map +1 -0
- package/dist/cmd/index.js +49 -57
- package/dist/cmd/index.js.map +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -1
- package/dist/config/types.d.ts +14 -0
- package/dist/config/types.d.ts.map +1 -1
- package/dist/llm/anthropic.d.ts +55 -5
- package/dist/llm/anthropic.d.ts.map +1 -1
- package/dist/llm/anthropic.js +172 -26
- package/dist/llm/anthropic.js.map +1 -1
- package/dist/llm/index.d.ts +26 -8
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +83 -15
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/openai.d.ts +28 -8
- package/dist/llm/openai.d.ts.map +1 -1
- package/dist/llm/openai.js +58 -8
- package/dist/llm/openai.js.map +1 -1
- package/dist/llm/provider.d.ts +40 -8
- package/dist/llm/provider.d.ts.map +1 -1
- package/dist/llm/provider.js +8 -1
- package/dist/llm/provider.js.map +1 -1
- package/dist/prompts/index.d.ts +32 -0
- package/dist/prompts/index.d.ts.map +1 -1
- package/dist/prompts/index.js +33 -11
- package/dist/prompts/index.js.map +1 -1
- package/dist/session/index.d.ts +32 -3
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +51 -36
- package/dist/session/index.js.map +1 -1
- package/dist/skills/brainstorming.js +33 -33
- package/dist/skills/code-review.js +35 -35
- package/dist/skills/executing-plans.js +18 -18
- package/dist/skills/frontend-design.js +25 -25
- package/dist/skills/git-workflow.js +26 -26
- package/dist/skills/improve-architecture.js +28 -28
- package/dist/skills/neuroweave.js +37 -37
- package/dist/skills/playwright.js +62 -62
- package/dist/skills/prototype.js +20 -20
- package/dist/skills/subagent.js +19 -19
- package/dist/skills/supabase.js +34 -34
- package/dist/skills/systematic-debugging.js +34 -34
- package/dist/skills/tdd.js +29 -29
- package/dist/skills/triage.js +25 -25
- package/dist/skills/verification.js +22 -22
- package/dist/skills/writing-plans.js +32 -32
- package/dist/tools/nwt.js +16 -16
- package/dist/tui/app.d.ts.map +1 -1
- package/dist/tui/app.js +9 -2
- package/dist/tui/app.js.map +1 -1
- package/dist/tui/components/Header.d.ts +8 -0
- package/dist/tui/components/Header.d.ts.map +1 -1
- package/dist/tui/components/Header.js +17 -4
- package/dist/tui/components/Header.js.map +1 -1
- package/dist/tui/components/InitWizard.d.ts +12 -1
- package/dist/tui/components/InitWizard.d.ts.map +1 -1
- package/dist/tui/components/InitWizard.js +58 -3
- package/dist/tui/components/InitWizard.js.map +1 -1
- package/dist/tui/components/ModelSelector.d.ts +3 -1
- package/dist/tui/components/ModelSelector.d.ts.map +1 -1
- package/dist/tui/components/ModelSelector.js +24 -7
- package/dist/tui/components/ModelSelector.js.map +1 -1
- package/dist/tui/components/ToolCall.d.ts.map +1 -1
- package/dist/tui/components/ToolCall.js +1 -6
- package/dist/tui/components/ToolCall.js.map +1 -1
- package/dist/tui/components/UserInput.d.ts.map +1 -1
- package/dist/tui/components/UserInput.js +20 -0
- package/dist/tui/components/UserInput.js.map +1 -1
- package/dist/tui/hooks/useChat.d.ts +22 -0
- package/dist/tui/hooks/useChat.d.ts.map +1 -1
- package/dist/tui/hooks/useChat.js +82 -46
- package/dist/tui/hooks/useChat.js.map +1 -1
- package/dist/tui/hooks/useCommands.d.ts.map +1 -1
- package/dist/tui/hooks/useCommands.js +57 -0
- package/dist/tui/hooks/useCommands.js.map +1 -1
- package/dist/tui/welcome.d.ts.map +1 -1
- package/dist/tui/welcome.js +2 -3
- package/dist/tui/welcome.js.map +1 -1
- package/dist/types.d.ts +79 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -1
- package/dist/utils/stableStringify.d.ts +21 -0
- package/dist/utils/stableStringify.d.ts.map +1 -0
- package/dist/utils/stableStringify.js +53 -0
- package/dist/utils/stableStringify.js.map +1 -0
- package/dist/utils/thinking.d.ts.map +1 -1
- package/dist/utils/thinking.js +0 -3
- package/dist/utils/thinking.js.map +1 -1
- package/package.json +7 -6
- package/src/app/index.ts +189 -124
- package/src/cache/fingerprint.ts +101 -0
- package/src/cache/index.ts +15 -0
- package/src/cache/smartModel.ts +52 -0
- package/src/cache/stats.ts +199 -0
- package/src/cache/volatile.ts +47 -0
- package/src/cmd/index.tsx +288 -292
- package/src/config/index.ts +156 -148
- package/src/config/providers.ts +234 -234
- package/src/config/types.ts +67 -53
- package/src/hooks/index.ts +111 -111
- package/src/llm/anthropic.ts +388 -243
- package/src/llm/gemini.ts +169 -169
- package/src/llm/index.ts +265 -200
- package/src/llm/openai.ts +243 -196
- package/src/llm/provider.ts +66 -34
- package/src/prompts/index.ts +260 -220
- package/src/session/compactor.ts +103 -103
- package/src/session/index.ts +181 -168
- package/src/session/message.ts +42 -42
- package/src/skills/brainstorming.ts +43 -43
- package/src/skills/code-review.ts +45 -45
- package/src/skills/executing-plans.ts +27 -27
- package/src/skills/frontend-design.ts +35 -35
- package/src/skills/git-workflow.ts +36 -36
- package/src/skills/improve-architecture.ts +38 -38
- package/src/skills/index.ts +136 -136
- package/src/skills/neuroweave.ts +47 -47
- package/src/skills/playwright.ts +72 -72
- package/src/skills/prototype.ts +30 -30
- package/src/skills/subagent.ts +28 -28
- package/src/skills/supabase.ts +44 -44
- package/src/skills/systematic-debugging.ts +44 -44
- package/src/skills/tdd.ts +39 -39
- package/src/skills/triage.ts +35 -35
- package/src/skills/verification.ts +31 -31
- package/src/skills/writing-plans.ts +42 -42
- package/src/tools/nwt.ts +598 -598
- package/src/tools/types.ts +122 -122
- package/src/tui/app.tsx +200 -186
- package/src/tui/components/ChatList.tsx +41 -41
- package/src/tui/components/ChatMessage.tsx +54 -54
- package/src/tui/components/Header.tsx +56 -29
- package/src/tui/components/InitWizard.tsx +217 -132
- package/src/tui/components/Markdown.tsx +35 -35
- package/src/tui/components/ModelSelector.tsx +107 -87
- package/src/tui/components/StatusBar.tsx +30 -30
- package/src/tui/components/Thinking.tsx +17 -17
- package/src/tui/components/ToolCall.tsx +102 -107
- package/src/tui/components/UserInput.tsx +151 -131
- package/src/tui/hooks/useChat.ts +287 -238
- package/src/tui/hooks/useCommands.ts +234 -176
- package/src/tui/index.ts +6 -6
- package/src/tui/welcome.ts +177 -178
- package/src/types.ts +104 -42
- package/src/utils/diff.ts +71 -71
- package/src/utils/project.ts +99 -99
- package/src/utils/stableStringify.ts +47 -0
- package/src/utils/thinking.ts +118 -121
- package/tests/cache/fingerprint.test.ts +75 -0
- package/tests/cache/providerCatalog.test.ts +31 -0
- package/tests/cache/stableStringify.test.ts +43 -0
- package/tests/cache/stats.test.ts +146 -0
- package/tests/cache/volatile.test.ts +75 -0
- package/tests/smoke-pollution.mjs +78 -0
- package/tests/smoke-thinking.mjs +110 -0
- package/tests/smoke-tool-stream.mjs +69 -0
- package/tests/smoke-tool.mjs +117 -0
- package/.nwt/meta.json +0 -5
- package/dist/version.d.ts +0 -16
- package/dist/version.d.ts.map +0 -1
- package/dist/version.js +0 -16
- package/dist/version.js.map +0 -1
- package/src/version.ts +0 -16
package/dist/llm/index.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* LLM Service - Factory for creating providers
|
|
3
3
|
* Supports all providers + custom relay stations (中转站)
|
|
4
|
+
*
|
|
5
|
+
* v3.0.0+: streaming protocol migrated to structured StreamChunk
|
|
6
|
+
* - chatStream yields { type: 'text' | 'tool_calls' | 'thinking' | 'usage' }
|
|
7
|
+
* - Tool call execution is now part of the agent loop; consumers see
|
|
8
|
+
* { type: 'tool_calls', toolCalls } for dispatch and { type: 'text', content }
|
|
9
|
+
* for model output. The legacy @@TOOL@@ sentinel-string protocol is GONE.
|
|
10
|
+
* - Usage / cache stats are emitted as the final { type: 'usage' } chunk,
|
|
11
|
+
* which the TUI cache stats store consumes.
|
|
4
12
|
*/
|
|
5
13
|
import { PROVIDERS } from '../config/providers.js';
|
|
6
14
|
import { OpenAIProvider } from './openai.js';
|
|
@@ -23,6 +31,9 @@ export class LLMService {
|
|
|
23
31
|
baseUrl: config.baseUrl || providerConfig.baseUrl,
|
|
24
32
|
temperature: config.temperature ?? 0.7,
|
|
25
33
|
maxTokens: config.maxTokens ?? 4096,
|
|
34
|
+
// v3.0.0: forward cache policy. Anthropic reads this to decide
|
|
35
|
+
// whether to attach cache_control markers; other providers ignore it.
|
|
36
|
+
cache: config.cache ?? { enabled: true, ttl: '5m', strategy: 'auto' },
|
|
26
37
|
};
|
|
27
38
|
const format = providerConfig.format;
|
|
28
39
|
let provider;
|
|
@@ -52,10 +63,18 @@ export class LLMService {
|
|
|
52
63
|
return this.provider.chat(messages, options, toolsArray.length > 0 ? toolsArray : undefined);
|
|
53
64
|
}
|
|
54
65
|
/**
|
|
55
|
-
* Streaming chat with agent loop (tool call support)
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
66
|
+
* Streaming chat with agent loop (tool call support).
|
|
67
|
+
*
|
|
68
|
+
* Structured chunk protocol (replaces the @@TOOL@@ sentinel strings):
|
|
69
|
+
* { type: 'text', content: string } model output, accumulate + render
|
|
70
|
+
* { type: 'tool_calls', toolCalls: ToolCall[] } dispatch tools, results will
|
|
71
|
+
* be folded into the next request
|
|
72
|
+
* { type: 'thinking', content: string } reasoning text (consumers may
|
|
73
|
+
* display in debug mode)
|
|
74
|
+
* { type: 'usage', usage: Usage } cache hit/miss + token counts;
|
|
75
|
+
* emitted on the final round
|
|
76
|
+
*
|
|
77
|
+
* Returns the final ChatResponse of the agent loop when done.
|
|
59
78
|
*/
|
|
60
79
|
async *chatStream(messages, options) {
|
|
61
80
|
if (!this.hasApiKey())
|
|
@@ -63,44 +82,74 @@ export class LLMService {
|
|
|
63
82
|
const maxIterations = options?.maxIterations ?? 10;
|
|
64
83
|
let currentMessages = [...messages];
|
|
65
84
|
let iterations = 0;
|
|
85
|
+
let lastUsage;
|
|
66
86
|
while (iterations < maxIterations) {
|
|
67
87
|
iterations++;
|
|
68
88
|
const toolsArray = [...this.tools.values()];
|
|
69
89
|
const hasTools = toolsArray.length > 0;
|
|
70
90
|
let fullContent = '';
|
|
71
91
|
let detectedToolCalls;
|
|
92
|
+
// Forward stream chunks from the provider. We collect text internally for
|
|
93
|
+
// tool-call persistence but always re-emit the original chunks unchanged.
|
|
72
94
|
const stream = this.provider.chatStream(currentMessages, options, hasTools ? toolsArray : undefined);
|
|
73
95
|
for await (const chunk of stream) {
|
|
74
96
|
if (chunk.type === 'text' && chunk.content) {
|
|
75
97
|
fullContent += chunk.content;
|
|
76
|
-
yield chunk
|
|
98
|
+
yield chunk;
|
|
77
99
|
}
|
|
78
100
|
else if (chunk.type === 'tool_calls' && chunk.toolCalls) {
|
|
79
101
|
detectedToolCalls = chunk.toolCalls;
|
|
102
|
+
// Don't yield the raw tool_calls chunk here — we emit one combined
|
|
103
|
+
// chunk after persisting the assistant message so consumers don't
|
|
104
|
+
// double-render.
|
|
105
|
+
}
|
|
106
|
+
else if (chunk.type === 'thinking') {
|
|
107
|
+
yield chunk;
|
|
108
|
+
}
|
|
109
|
+
else if (chunk.type === 'usage') {
|
|
110
|
+
lastUsage = chunk.usage;
|
|
111
|
+
yield chunk;
|
|
80
112
|
}
|
|
81
113
|
}
|
|
82
114
|
// If we got tool calls, execute them and loop
|
|
83
115
|
if (detectedToolCalls && detectedToolCalls.length > 0) {
|
|
84
|
-
// Add assistant message with tool calls
|
|
116
|
+
// Add assistant message with tool calls (append-only, preserves prefix cache)
|
|
85
117
|
currentMessages.push({
|
|
86
118
|
role: 'assistant',
|
|
87
119
|
content: fullContent || '',
|
|
88
120
|
tool_calls: detectedToolCalls,
|
|
89
121
|
});
|
|
90
|
-
// Execute each tool
|
|
122
|
+
// Execute each tool and emit a single structured tool_calls chunk
|
|
123
|
+
// describing the dispatch plan. Per-tool results are appended to
|
|
124
|
+
// currentMessages but NOT yielded as additional chunks — consumers
|
|
125
|
+
// that want to display result text can introspect tool_calls[*].result
|
|
126
|
+
// on a synthesized combined chunk, OR we re-emit one tool_calls chunk
|
|
127
|
+
// at the end with results attached. We choose the simpler approach:
|
|
128
|
+
// emit ONE tool_calls chunk per iteration with all the calls; results
|
|
129
|
+
// are surfaced through the next assistant turn's text content.
|
|
130
|
+
//
|
|
131
|
+
// v3.0.0 Tool-call Repair (Reasonix P2): on tool failure we APPEND
|
|
132
|
+
// a `[TOOL_REPAIR]` system message rather than mutating the existing
|
|
133
|
+
// tool_call message. This preserves the upstream cache prefix —
|
|
134
|
+
// re-writing an earlier message would shift the prefix by N bytes
|
|
135
|
+
// and bust the cache for every subsequent round.
|
|
91
136
|
for (const toolCall of detectedToolCalls) {
|
|
92
137
|
const tool = this.tools.get(toolCall.function.name);
|
|
93
|
-
// Yield structured tool call as JSON line
|
|
94
|
-
yield `\n@@TOOL@@${JSON.stringify({ action: 'call', name: toolCall.function.name, args: toolCall.function.arguments })}\n`;
|
|
95
138
|
if (!tool) {
|
|
96
139
|
const errMsg = `Tool "${toolCall.function.name}" not found`;
|
|
140
|
+
// Repair message — explain the failure and tell the model to
|
|
141
|
+
// try a different tool. Cache-safe because we are APPENDING,
|
|
142
|
+
// never modifying existing messages.
|
|
143
|
+
currentMessages.push({
|
|
144
|
+
role: 'system',
|
|
145
|
+
content: `[TOOL_REPAIR] Previous tool_call "${toolCall.function.name}" (id=${toolCall.id}) failed: ${errMsg}. Available tools: ${[...this.tools.keys()].join(', ')}.`,
|
|
146
|
+
});
|
|
97
147
|
currentMessages.push({
|
|
98
148
|
role: 'tool',
|
|
99
149
|
content: errMsg,
|
|
100
150
|
tool_call_id: toolCall.id,
|
|
101
151
|
name: toolCall.function.name,
|
|
102
152
|
});
|
|
103
|
-
yield `@@TOOL@@${JSON.stringify({ action: 'result', error: errMsg })}\n`;
|
|
104
153
|
continue;
|
|
105
154
|
}
|
|
106
155
|
try {
|
|
@@ -113,7 +162,13 @@ export class LLMService {
|
|
|
113
162
|
tool_call_id: toolCall.id,
|
|
114
163
|
name: toolCall.function.name,
|
|
115
164
|
});
|
|
116
|
-
|
|
165
|
+
if (!result.success) {
|
|
166
|
+
// Soft failure: tool returned success=false. Same repair pattern.
|
|
167
|
+
currentMessages.push({
|
|
168
|
+
role: 'system',
|
|
169
|
+
content: `[TOOL_REPAIR] Tool "${toolCall.function.name}" returned success=false: ${output}. Consider correcting the arguments and retrying.`,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
117
172
|
}
|
|
118
173
|
catch (error) {
|
|
119
174
|
const errMsg = `Error: ${error.message}`;
|
|
@@ -123,14 +178,27 @@ export class LLMService {
|
|
|
123
178
|
tool_call_id: toolCall.id,
|
|
124
179
|
name: toolCall.function.name,
|
|
125
180
|
});
|
|
126
|
-
|
|
181
|
+
// Hard failure: tool.execute threw. Repair message so the model
|
|
182
|
+
// can see the failure next round and adjust (e.g. fix a path
|
|
183
|
+
// typo, retry without the optional arg).
|
|
184
|
+
currentMessages.push({
|
|
185
|
+
role: 'system',
|
|
186
|
+
content: `[TOOL_REPAIR] Tool "${toolCall.function.name}" threw an exception: ${errMsg}. Inspect the arguments and retry with a corrected call.`,
|
|
187
|
+
});
|
|
127
188
|
}
|
|
128
189
|
}
|
|
129
|
-
|
|
190
|
+
// Emit one tool_calls chunk for this iteration. TUI renders this as a
|
|
191
|
+
// "tool invoked" entry. Per-tool outputs are summarized on the next
|
|
192
|
+
// assistant turn.
|
|
193
|
+
yield { type: 'tool_calls', toolCalls: detectedToolCalls };
|
|
130
194
|
continue;
|
|
131
195
|
}
|
|
132
|
-
// No tool calls - done
|
|
133
|
-
return {
|
|
196
|
+
// No tool calls - done. Return final response (with usage if we have it).
|
|
197
|
+
return {
|
|
198
|
+
content: fullContent,
|
|
199
|
+
role: 'assistant',
|
|
200
|
+
usage: lastUsage,
|
|
201
|
+
};
|
|
134
202
|
}
|
|
135
203
|
return { content: '[Agent loop exceeded maximum iterations]', role: 'assistant' };
|
|
136
204
|
}
|
package/dist/llm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAOH,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,OAAO,UAAU;IACb,QAAQ,CAAc;IACtB,KAAK,GAAsB,IAAI,GAAG,EAAE,CAAC;IACrC,MAAM,CAAS;IAEvB,YAAY,QAAqB,EAAE,MAAc;QAC/C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CA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,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/llm/openai.d.ts
CHANGED
|
@@ -2,15 +2,20 @@
|
|
|
2
2
|
* OpenAI-compatible Provider
|
|
3
3
|
* Works with: OpenAI, SiliconFlow, DeepSeek, Kimi, Zhipu, MiniMax, Baichuan, Stepfun, Doubao, Ollama, ERNIE
|
|
4
4
|
* Also works with any OpenAI-compatible relay station (中转站)
|
|
5
|
+
*
|
|
6
|
+
* v3.0.0+: caching-friendly wire format
|
|
7
|
+
* - StreamChunk is now imported from ../types (single source of truth)
|
|
8
|
+
* - buildRequest uses stableStringify so that the bytes sent are byte-equal
|
|
9
|
+
* between requests with the same logical payload (required for DeepSeek
|
|
10
|
+
* automatic prefix-cache hit)
|
|
11
|
+
* - cache_control on individual ChatMessage is forwarded (Anthropic-style
|
|
12
|
+
* markers are no-ops on OpenAI-compatible APIs but harmless)
|
|
13
|
+
* - chatStream yields a final { type: 'usage' } chunk if the upstream
|
|
14
|
+
* returned usage info (stream_options.include_usage already requested)
|
|
5
15
|
*/
|
|
6
|
-
import type { ChatMessage, ChatResponse, ChatOptions,
|
|
16
|
+
import type { ChatMessage, ChatResponse, ChatOptions, StreamChunk } from '../types.js';
|
|
7
17
|
import type { Tool } from '../tools/types.js';
|
|
8
18
|
import type { LLMProvider, ProviderConfig } from './provider.js';
|
|
9
|
-
export interface StreamChunk {
|
|
10
|
-
type: 'text' | 'tool_calls';
|
|
11
|
-
content?: string;
|
|
12
|
-
toolCalls?: ToolCall[];
|
|
13
|
-
}
|
|
14
19
|
export declare class OpenAIProvider implements LLMProvider {
|
|
15
20
|
readonly name = "openai";
|
|
16
21
|
protected config: ProviderConfig;
|
|
@@ -19,12 +24,27 @@ export declare class OpenAIProvider implements LLMProvider {
|
|
|
19
24
|
chat(messages: ChatMessage[], options?: ChatOptions, tools?: Tool[]): Promise<ChatResponse>;
|
|
20
25
|
chatStream(messages: ChatMessage[], options?: ChatOptions, tools?: Tool[]): AsyncGenerator<StreamChunk, ChatResponse>;
|
|
21
26
|
/**
|
|
22
|
-
* Build the request body for OpenAI-compatible API
|
|
27
|
+
* Build the request body for OpenAI-compatible API.
|
|
28
|
+
*
|
|
29
|
+
* Why stableStringify matters: DeepSeek (and most OpenAI-compatible APIs)
|
|
30
|
+
* perform automatic prefix cache lookup by byte-level hash of the request
|
|
31
|
+
* payload. If the JSON we send today differs from yesterday's by even one
|
|
32
|
+
* reordered key, the cache misses. Insertion-order stability is good enough
|
|
33
|
+
* when the same code path runs every time, but we now use stableStringify
|
|
34
|
+
* as a belt-and-suspenders guarantee against accidental key reordering from
|
|
35
|
+
* future refactors (spread / Object.fromEntries / map merging).
|
|
23
36
|
*/
|
|
24
37
|
protected buildRequest(messages: ChatMessage[], stream: boolean, options?: ChatOptions, tools?: Tool[]): any;
|
|
25
38
|
/**
|
|
26
|
-
* Execute the HTTP request
|
|
39
|
+
* Execute the HTTP request. Serializes via stableStringify so the byte
|
|
40
|
+
* sequence is deterministic across requests.
|
|
27
41
|
*/
|
|
28
42
|
protected doRequest(body: any): Promise<Response>;
|
|
43
|
+
/**
|
|
44
|
+
* Map upstream usage JSON to the canonical Usage shape. Different providers
|
|
45
|
+
* attach different cache-related fields; we pass everything through so the
|
|
46
|
+
* cache stats layer can interpret per-provider.
|
|
47
|
+
*/
|
|
48
|
+
protected normalizeUsage(raw: any): ChatResponse['usage'];
|
|
29
49
|
}
|
|
30
50
|
//# sourceMappingURL=openai.d.ts.map
|
package/dist/llm/openai.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA;;;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un, Doubao, Ollama, ERNIE
|
|
4
4
|
* Also works with any OpenAI-compatible relay station (中转站)
|
|
5
|
+
*
|
|
6
|
+
* v3.0.0+: caching-friendly wire format
|
|
7
|
+
* - StreamChunk is now imported from ../types (single source of truth)
|
|
8
|
+
* - buildRequest uses stableStringify so that the bytes sent are byte-equal
|
|
9
|
+
* between requests with the same logical payload (required for DeepSeek
|
|
10
|
+
* automatic prefix-cache hit)
|
|
11
|
+
* - cache_control on individual ChatMessage is forwarded (Anthropic-style
|
|
12
|
+
* markers are no-ops on OpenAI-compatible APIs but harmless)
|
|
13
|
+
* - chatStream yields a final { type: 'usage' } chunk if the upstream
|
|
14
|
+
* returned usage info (stream_options.include_usage already requested)
|
|
5
15
|
*/
|
|
16
|
+
import { stableStringify } from '../utils/stableStringify.js';
|
|
6
17
|
export class OpenAIProvider {
|
|
7
18
|
name = 'openai';
|
|
8
19
|
config;
|
|
@@ -31,11 +42,7 @@ export class OpenAIProvider {
|
|
|
31
42
|
return {
|
|
32
43
|
content: choice?.message?.content || '',
|
|
33
44
|
role: 'assistant',
|
|
34
|
-
usage: data.usage ?
|
|
35
|
-
prompt_tokens: data.usage.prompt_tokens || 0,
|
|
36
|
-
completion_tokens: data.usage.completion_tokens || 0,
|
|
37
|
-
total_tokens: data.usage.total_tokens || 0,
|
|
38
|
-
} : undefined,
|
|
45
|
+
usage: data.usage ? this.normalizeUsage(data.usage) : undefined,
|
|
39
46
|
tool_calls: choice?.message?.tool_calls,
|
|
40
47
|
};
|
|
41
48
|
}
|
|
@@ -52,6 +59,9 @@ export class OpenAIProvider {
|
|
|
52
59
|
let buffer = '';
|
|
53
60
|
// Accumulate streaming tool call chunks
|
|
54
61
|
const toolCallBuffers = new Map();
|
|
62
|
+
// Some providers attach usage only on the last chunk (DeepSeek / OpenAI with
|
|
63
|
+
// stream_options.include_usage). We capture it here and yield at the end.
|
|
64
|
+
let capturedUsage;
|
|
55
65
|
try {
|
|
56
66
|
while (true) {
|
|
57
67
|
const { done, value } = await reader.read();
|
|
@@ -92,6 +102,10 @@ export class OpenAIProvider {
|
|
|
92
102
|
buf.arguments += tc.function.arguments;
|
|
93
103
|
}
|
|
94
104
|
}
|
|
105
|
+
// DeepSeek/OpenAI stream-end usage (only present on the last chunk)
|
|
106
|
+
if (data.usage) {
|
|
107
|
+
capturedUsage = this.normalizeUsage(data.usage);
|
|
108
|
+
}
|
|
95
109
|
}
|
|
96
110
|
catch {
|
|
97
111
|
// Skip invalid JSON lines
|
|
@@ -116,24 +130,41 @@ export class OpenAIProvider {
|
|
|
116
130
|
if (toolCalls.length > 0) {
|
|
117
131
|
yield { type: 'tool_calls', toolCalls };
|
|
118
132
|
}
|
|
133
|
+
// Yield captured usage so TUI / cache stats can observe cache hits
|
|
134
|
+
if (capturedUsage) {
|
|
135
|
+
yield { type: 'usage', usage: capturedUsage };
|
|
136
|
+
}
|
|
119
137
|
return {
|
|
120
138
|
content: fullContent,
|
|
121
139
|
role: 'assistant',
|
|
140
|
+
usage: capturedUsage,
|
|
122
141
|
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
123
142
|
};
|
|
124
143
|
}
|
|
125
144
|
/**
|
|
126
|
-
* Build the request body for OpenAI-compatible API
|
|
145
|
+
* Build the request body for OpenAI-compatible API.
|
|
146
|
+
*
|
|
147
|
+
* Why stableStringify matters: DeepSeek (and most OpenAI-compatible APIs)
|
|
148
|
+
* perform automatic prefix cache lookup by byte-level hash of the request
|
|
149
|
+
* payload. If the JSON we send today differs from yesterday's by even one
|
|
150
|
+
* reordered key, the cache misses. Insertion-order stability is good enough
|
|
151
|
+
* when the same code path runs every time, but we now use stableStringify
|
|
152
|
+
* as a belt-and-suspenders guarantee against accidental key reordering from
|
|
153
|
+
* future refactors (spread / Object.fromEntries / map merging).
|
|
127
154
|
*/
|
|
128
155
|
buildRequest(messages, stream, options, tools) {
|
|
129
156
|
const body = {
|
|
130
157
|
model: this.config.model,
|
|
131
158
|
messages: messages.map(m => ({
|
|
132
159
|
role: m.role,
|
|
160
|
+
// content is string | ContentBlock[]. OpenAI wire format accepts both:
|
|
161
|
+
// - string for plain text messages
|
|
162
|
+
// - array of {type,text} or {type,image_url} blocks for multimodal
|
|
133
163
|
content: m.content,
|
|
134
164
|
...(m.name && { name: m.name }),
|
|
135
165
|
...(m.tool_call_id && { tool_call_id: m.tool_call_id }),
|
|
136
166
|
...(m.tool_calls && { tool_calls: m.tool_calls }),
|
|
167
|
+
...(m.cache_control && { cache_control: m.cache_control }),
|
|
137
168
|
})),
|
|
138
169
|
temperature: options?.temperature ?? this.config.temperature,
|
|
139
170
|
max_tokens: options?.maxTokens ?? this.config.maxTokens,
|
|
@@ -147,7 +178,8 @@ export class OpenAIProvider {
|
|
|
147
178
|
return body;
|
|
148
179
|
}
|
|
149
180
|
/**
|
|
150
|
-
* Execute the HTTP request
|
|
181
|
+
* Execute the HTTP request. Serializes via stableStringify so the byte
|
|
182
|
+
* sequence is deterministic across requests.
|
|
151
183
|
*/
|
|
152
184
|
async doRequest(body) {
|
|
153
185
|
const url = `${this.config.baseUrl}/chat/completions`;
|
|
@@ -160,7 +192,7 @@ export class OpenAIProvider {
|
|
|
160
192
|
'Content-Type': 'application/json',
|
|
161
193
|
'Authorization': `Bearer ${this.config.apiKey}`,
|
|
162
194
|
},
|
|
163
|
-
body:
|
|
195
|
+
body: stableStringify(body),
|
|
164
196
|
signal: controller.signal,
|
|
165
197
|
});
|
|
166
198
|
return response;
|
|
@@ -169,5 +201,23 @@ export class OpenAIProvider {
|
|
|
169
201
|
clearTimeout(timeout);
|
|
170
202
|
}
|
|
171
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Map upstream usage JSON to the canonical Usage shape. Different providers
|
|
206
|
+
* attach different cache-related fields; we pass everything through so the
|
|
207
|
+
* cache stats layer can interpret per-provider.
|
|
208
|
+
*/
|
|
209
|
+
normalizeUsage(raw) {
|
|
210
|
+
return {
|
|
211
|
+
prompt_tokens: raw.prompt_tokens || 0,
|
|
212
|
+
completion_tokens: raw.completion_tokens || 0,
|
|
213
|
+
total_tokens: raw.total_tokens || 0,
|
|
214
|
+
// DeepSeek automatic prefix cache fields
|
|
215
|
+
prompt_cache_hit_tokens: raw.prompt_cache_hit_tokens,
|
|
216
|
+
prompt_cache_miss_tokens: raw.prompt_cache_miss_tokens,
|
|
217
|
+
// Anthropic prompt cache fields (passed through if relay forwards them)
|
|
218
|
+
cache_creation_input_tokens: raw.cache_creation_input_tokens,
|
|
219
|
+
cache_read_input_tokens: raw.cache_read_input_tokens,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
172
222
|
}
|
|
173
223
|
//# sourceMappingURL=openai.js.map
|
package/dist/llm/openai.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D,MAAM,OAAO,cAAc;IAChB,IAAI,GAAG,QAAQ,CAAC;IACf,MAAM,CAAiB;IAEjC,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,UAAU,CAAC,KAAa;QACtB,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACxB,IAAI,EAAE,UAAmB;YACzB,QAAQ,EAAE;gBACR,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,UAAU,EAAE,IAAI,CAAC,WAAW,IAAI;oBAC9B,IAAI,EAAE,QAAQ;oBACd,UAAU,EAAE,MAAM,CAAC,WAAW,CAC5B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,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AC,OAAO,mBAAmB,CAAC;QACtD,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;QACzC,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC,cAAc;QAE3E,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;gBAChC,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE;oBACP,cAAc,EAAE,kBAAkB;oBAClC,eAAe,EAAE,UAAU,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;iBAChD;gBACD,IAAI,EAAE,eAAe,CAAC,IAAI,CAAC;gBAC3B,MAAM,EAAE,UAAU,CAAC,MAAM;aAC1B,CAAC,CAAC;YACH,OAAO,QAAQ,CAAC;QAClB,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IAED;;;;OAIG;IACO,cAAc,CAAC,GAAQ;QAC/B,OAAO;YACL,aAAa,EAAE,GAAG,CAAC,aAAa,IAAI,CAAC;YACrC,iBAAiB,EAAE,GAAG,CAAC,iBAAiB,IAAI,CAAC;YAC7C,YAAY,EAAE,GAAG,CAAC,YAAY,IAAI,CAAC;YACnC,yCAAyC;YACzC,uBAAuB,EAAE,GAAG,CAAC,uBAAuB;YACpD,wBAAwB,EAAE,GAAG,CAAC,wBAAwB;YACtD,wEAAwE;YACxE,2BAA2B,EAAE,GAAG,CAAC,2BAA2B;YAC5D,uBAAuB,EAAE,GAAG,CAAC,uBAAuB;SACrD,CAAC;IACJ,CAAC;CACF"}
|
package/dist/llm/provider.d.ts
CHANGED
|
@@ -1,19 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Abstract LLM Provider interface
|
|
3
|
-
* All providers implement this interface
|
|
3
|
+
* All providers implement this interface.
|
|
4
|
+
*
|
|
5
|
+
* v3.0.0+: StreamChunk is now imported from the shared types module so that
|
|
6
|
+
* TUI / session / cache layers can consume it uniformly. Providers no longer
|
|
7
|
+
* carry their own definition; see src/types.ts for the union (text/tool_calls/
|
|
8
|
+
* thinking/usage). The previous local 'text' | 'tool_calls' shape was kept as
|
|
9
|
+
* a structural type alias for backward compatibility with existing call sites
|
|
10
|
+
* that destructure by .type only.
|
|
4
11
|
*/
|
|
5
|
-
import type { ChatMessage, ChatResponse, ChatOptions,
|
|
12
|
+
import type { ChatMessage, ChatResponse, ChatOptions, StreamChunk as SharedStreamChunk } from '../types.js';
|
|
6
13
|
import type { Tool } from '../tools/types.js';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Re-export of the structured streaming chunk. Now structurally compatible
|
|
16
|
+
* with the old { type, content?, toolCalls? } shape — callers that previously
|
|
17
|
+
* destructured chunk.type and chunk.content / chunk.toolCalls still work, and
|
|
18
|
+
* new variants ('thinking', 'usage') are opt-in.
|
|
19
|
+
*/
|
|
20
|
+
export type StreamChunk = SharedStreamChunk;
|
|
12
21
|
export interface LLMProvider {
|
|
13
22
|
readonly name: string;
|
|
14
23
|
/** Non-streaming chat with optional tools */
|
|
15
24
|
chat(messages: ChatMessage[], options?: ChatOptions, tools?: Tool[]): Promise<ChatResponse>;
|
|
16
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Streaming chat with optional tools.
|
|
27
|
+
* Yields structured StreamChunks:
|
|
28
|
+
* { type: 'text', content: string }
|
|
29
|
+
* { type: 'tool_calls', toolCalls: ToolCall[] }
|
|
30
|
+
* { type: 'thinking', content: string }
|
|
31
|
+
* { type: 'usage', usage: Usage }
|
|
32
|
+
* Returns the final ChatResponse (with usage) when the stream completes.
|
|
33
|
+
*/
|
|
17
34
|
chatStream(messages: ChatMessage[], options?: ChatOptions, tools?: Tool[]): AsyncGenerator<StreamChunk, ChatResponse>;
|
|
18
35
|
/** Convert Tool[] to provider-specific format */
|
|
19
36
|
buildTools(tools: Tool[]): any[];
|
|
@@ -24,5 +41,20 @@ export interface ProviderConfig {
|
|
|
24
41
|
baseUrl: string;
|
|
25
42
|
temperature: number;
|
|
26
43
|
maxTokens: number;
|
|
44
|
+
/**
|
|
45
|
+
* Optional cache control policy. When omitted, providers fall back to their
|
|
46
|
+
* default behavior (Anthropic: explicit cache_control markers; OpenAI / Gemini
|
|
47
|
+
* / DeepSeek: automatic prefix cache, no markers needed).
|
|
48
|
+
*/
|
|
49
|
+
cache?: {
|
|
50
|
+
enabled: boolean;
|
|
51
|
+
ttl?: '5m' | '1h';
|
|
52
|
+
/**
|
|
53
|
+
* 'auto' — provider default (Anthropic: explicit, OpenAI/DeepSeek: auto)
|
|
54
|
+
* 'manual' — always emit cache_control markers regardless of provider
|
|
55
|
+
* 'off' — never emit markers (force disable, useful for benchmarking)
|
|
56
|
+
*/
|
|
57
|
+
strategy?: 'auto' | 'manual' | 'off';
|
|
58
|
+
};
|
|
27
59
|
}
|
|
28
60
|
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/llm/provider.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/llm/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,IAAI,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAC5G,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG,iBAAiB,CAAC;AAE5C,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,6CAA6C;IAC7C,IAAI,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAE5F;;;;;;;;OAQG;IACH,UAAU,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,cAAc,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;IAEtH,iDAAiD;IACjD,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,GAAG,EAAE,CAAC;CAClC;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,KAAK,CAAC,EAAE;QACN,OAAO,EAAE,OAAO,CAAC;QACjB,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;QAClB;;;;WAIG;QACH,QAAQ,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAC;KACtC,CAAC;CACH"}
|
package/dist/llm/provider.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Abstract LLM Provider interface
|
|
3
|
-
* All providers implement this interface
|
|
3
|
+
* All providers implement this interface.
|
|
4
|
+
*
|
|
5
|
+
* v3.0.0+: StreamChunk is now imported from the shared types module so that
|
|
6
|
+
* TUI / session / cache layers can consume it uniformly. Providers no longer
|
|
7
|
+
* carry their own definition; see src/types.ts for the union (text/tool_calls/
|
|
8
|
+
* thinking/usage). The previous local 'text' | 'tool_calls' shape was kept as
|
|
9
|
+
* a structural type alias for backward compatibility with existing call sites
|
|
10
|
+
* that destructure by .type only.
|
|
4
11
|
*/
|
|
5
12
|
export {};
|
|
6
13
|
//# sourceMappingURL=provider.js.map
|
package/dist/llm/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/llm/provider.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/llm/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG"}
|
package/dist/prompts/index.d.ts
CHANGED
|
@@ -10,10 +10,42 @@ export interface SystemPromptConfig {
|
|
|
10
10
|
date?: Date;
|
|
11
11
|
skillsPrompt?: string;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* v3.0.0: a single named fragment of the system prompt. The provider layer
|
|
15
|
+
* attaches cache_control markers to volatile=false segments (Anthropic) and
|
|
16
|
+
* the fingerprint module hashes only the non-volatile portion.
|
|
17
|
+
*/
|
|
18
|
+
export interface SystemSegment {
|
|
19
|
+
/** Stable identifier, used in fingerprints and logging. */
|
|
20
|
+
name: string;
|
|
21
|
+
/** Rendered text content. May be empty string if the section was a no-op. */
|
|
22
|
+
content: string;
|
|
23
|
+
/**
|
|
24
|
+
* volatile=false: content never changes between requests within a session.
|
|
25
|
+
* volatile=true: content may change (NWT events, current time).
|
|
26
|
+
*/
|
|
27
|
+
volatile: boolean;
|
|
28
|
+
}
|
|
13
29
|
export declare class SystemPromptBuilder {
|
|
14
30
|
private config;
|
|
15
31
|
constructor(config?: SystemPromptConfig);
|
|
16
32
|
build(): string;
|
|
33
|
+
/**
|
|
34
|
+
* v3.0.0: build the system prompt as named segments instead of a flat
|
|
35
|
+
* string. The provider layer (Anthropic) can attach cache_control markers
|
|
36
|
+
* to the segments it caches, and the fingerprint module can hash the
|
|
37
|
+
* static portion to detect when the immutable prefix changed.
|
|
38
|
+
*
|
|
39
|
+
* Ordering rule (Reasonix-style): immutable prefix first, volatile tail
|
|
40
|
+
* last. NWT history and Date move to the tail because both can change
|
|
41
|
+
* between rounds (Date: every build; NWT: when a new event is logged).
|
|
42
|
+
* Keeping them out of the prefix means the upstream cache keeps its
|
|
43
|
+
* prefix checkpoint stable across those changes.
|
|
44
|
+
*
|
|
45
|
+
* Segments with empty content are filtered out — they neither contribute
|
|
46
|
+
* to the string output nor to any cache fingerprint.
|
|
47
|
+
*/
|
|
48
|
+
buildSegments(): SystemSegment[];
|
|
17
49
|
private buildIdentity;
|
|
18
50
|
private buildToolInstructions;
|
|
19
51
|
private buildEnvironment;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/prompts/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;IACf,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,cAAc,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,KAAK,CAAC;IAC3C,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,MAAM,CAA+B;gBAEjC,MAAM,GAAE,kBAAuB;IAW3C,KAAK,IAAI,MAAM;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/prompts/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;IACf,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,cAAc,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,KAAK,CAAC;IAC3C,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;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