thatgfsj-code 1.0.3 → 3.0.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 +192 -0
- package/DEVELOPMENT.md +286 -0
- package/dist/app/index.d.ts +40 -3
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +87 -12
- 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.d.ts +21 -0
- package/dist/cmd/index.d.ts.map +1 -1
- package/dist/cmd/index.js +155 -31
- 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 +43 -3
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +114 -17
- 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/ChatList.d.ts.map +1 -1
- package/dist/tui/components/ChatList.js +4 -3
- package/dist/tui/components/ChatList.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 -3
- 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/ToolCall.d.ts +1 -1
- package/dist/tui/components/ToolCall.d.ts.map +1 -1
- package/dist/tui/components/ToolCall.js +4 -4
- package/dist/tui/components/ToolCall.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 +127 -50
- 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 +78 -0
- package/dist/tui/hooks/useCommands.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 +59 -0
- package/dist/utils/thinking.d.ts.map +1 -0
- package/dist/utils/thinking.js +104 -0
- package/dist/utils/thinking.js.map +1 -0
- package/install.bat +63 -0
- package/install.ps1 +238 -0
- package/install.sh +113 -0
- package/package.json +7 -4
- package/src/app/index.ts +180 -108
- 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 -160
- 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 -87
- 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 +199 -186
- package/src/tui/components/ChatList.tsx +41 -32
- package/src/tui/components/ChatMessage.tsx +54 -54
- package/src/tui/components/Header.tsx +56 -28
- package/src/tui/components/InitWizard.tsx +217 -132
- package/src/tui/components/Markdown.tsx +35 -35
- package/src/tui/components/ModelSelector.tsx +87 -87
- package/src/tui/components/StatusBar.tsx +30 -30
- package/src/tui/components/Thinking.tsx +17 -17
- package/src/tui/components/ToolCall.tsx +102 -71
- package/src/tui/components/UserInput.tsx +131 -131
- package/src/tui/hooks/useChat.ts +287 -194
- package/src/tui/hooks/useCommands.ts +234 -154
- package/src/tui/index.ts +6 -6
- package/src/tui/welcome.ts +177 -177
- 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 +119 -0
- package/tests/cache/fingerprint.test.ts +75 -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/src/app/index.ts
CHANGED
|
@@ -1,108 +1,180 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* App - Core application singleton
|
|
3
|
-
* Simplified: directly uses LLMService (which has built-in agent loop)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
llm
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
session
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
1
|
+
/**
|
|
2
|
+
* App - Core application singleton
|
|
3
|
+
* Simplified: directly uses LLMService (which has built-in agent loop)
|
|
4
|
+
*
|
|
5
|
+
* v3.0.0+: streamResponse yields structured StreamChunk
|
|
6
|
+
* - runPrompt streams { type: 'text' } chunks to stdout and captures the
|
|
7
|
+
* final usage for cache stats recording.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { ConfigManager } from '../config/index.js';
|
|
11
|
+
import { LLMService } from '../llm/index.js';
|
|
12
|
+
import { SessionManager } from '../session/index.js';
|
|
13
|
+
import { ToolRegistry } from '../tools/index.js';
|
|
14
|
+
import { HookManager } from '../hooks/index.js';
|
|
15
|
+
import { SystemPromptBuilder } from '../prompts/index.js';
|
|
16
|
+
import { SkillRegistry } from '../skills/index.js';
|
|
17
|
+
import { CacheStatsStore } from '../cache/stats.js';
|
|
18
|
+
import { compressThinking } from '../utils/thinking.js';
|
|
19
|
+
import type { ChatMessage, ChatResponse, StreamChunk, Usage } from '../types.js';
|
|
20
|
+
|
|
21
|
+
export class App {
|
|
22
|
+
config: ConfigManager;
|
|
23
|
+
llm: LLMService;
|
|
24
|
+
session: SessionManager;
|
|
25
|
+
tools: ToolRegistry;
|
|
26
|
+
hooks: HookManager;
|
|
27
|
+
prompts: SystemPromptBuilder;
|
|
28
|
+
skills: SkillRegistry;
|
|
29
|
+
/**
|
|
30
|
+
* v3.0.0: persistent cache stats store. The single source of truth for
|
|
31
|
+
* cache hit-rate and estimated savings, surfaced through the TUI Header
|
|
32
|
+
* and the /cache command.
|
|
33
|
+
*/
|
|
34
|
+
cacheStats: CacheStatsStore;
|
|
35
|
+
/**
|
|
36
|
+
* v2.2.5 (product 0.4.2): toggle block compression. Default
|
|
37
|
+
* true. Toggled by `--show-thinking` on the CLI or `/thinking on|off`
|
|
38
|
+
* in the REPL.
|
|
39
|
+
*/
|
|
40
|
+
showThinking: boolean = false;
|
|
41
|
+
|
|
42
|
+
private constructor(
|
|
43
|
+
config: ConfigManager,
|
|
44
|
+
llm: LLMService,
|
|
45
|
+
session: SessionManager,
|
|
46
|
+
tools: ToolRegistry,
|
|
47
|
+
hooks: HookManager,
|
|
48
|
+
prompts: SystemPromptBuilder,
|
|
49
|
+
skills: SkillRegistry,
|
|
50
|
+
cacheStats: CacheStatsStore,
|
|
51
|
+
) {
|
|
52
|
+
this.config = config;
|
|
53
|
+
this.llm = llm;
|
|
54
|
+
this.session = session;
|
|
55
|
+
this.tools = tools;
|
|
56
|
+
this.hooks = hooks;
|
|
57
|
+
this.prompts = prompts;
|
|
58
|
+
this.skills = skills;
|
|
59
|
+
this.cacheStats = cacheStats;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static async create(): Promise<App> {
|
|
63
|
+
const config = await ConfigManager.load();
|
|
64
|
+
const aiConfig = config.getAIConfig();
|
|
65
|
+
|
|
66
|
+
const llm = LLMService.fromConfig(aiConfig);
|
|
67
|
+
const cacheStats = new CacheStatsStore();
|
|
68
|
+
const session = new SessionManager(config.get().contextLength || 50);
|
|
69
|
+
// v3.0.0: do not mutate messages when context grows — instead, surface
|
|
70
|
+
// a "consider /new" toast via onSuggestNewSession. The TUI wires this
|
|
71
|
+
// up in app.tsx; in CLI single-prompt mode it's a no-op (one-shot).
|
|
72
|
+
session.onSuggestNewSession = (info) => {
|
|
73
|
+
console.warn(
|
|
74
|
+
`\n ⚠️ 上下文较长(${info.currentLength}/${info.max})。` +
|
|
75
|
+
`建议调 /new 开新会话(NWT 已自动归档历史)\n`,
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
const tools = new ToolRegistry();
|
|
79
|
+
const hooks = new HookManager();
|
|
80
|
+
const skills = new SkillRegistry();
|
|
81
|
+
|
|
82
|
+
// Register tools with LLM service
|
|
83
|
+
llm.registerTools(tools.list());
|
|
84
|
+
|
|
85
|
+
// Auto-init NWT timeline
|
|
86
|
+
const nwtTool = tools.get('nwt');
|
|
87
|
+
if (nwtTool) {
|
|
88
|
+
await nwtTool.execute({ action: 'init' });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Build system prompt with active skills
|
|
92
|
+
const prompts = new SystemPromptBuilder({
|
|
93
|
+
cwd: process.cwd(),
|
|
94
|
+
tools: tools.list(),
|
|
95
|
+
permissionMode: 'ask',
|
|
96
|
+
skillsPrompt: skills.getActivePrompts(),
|
|
97
|
+
});
|
|
98
|
+
session.addMessage('system', prompts.build());
|
|
99
|
+
|
|
100
|
+
return new App(config, llm, session, tools, hooks, prompts, skills, cacheStats);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Stream a response for the current session messages.
|
|
105
|
+
* The LLMService handles the full agent loop internally.
|
|
106
|
+
*
|
|
107
|
+
* Yields structured StreamChunks. Returns the final ChatResponse (with usage
|
|
108
|
+
* if the provider reported it) so the caller can record cache stats.
|
|
109
|
+
*
|
|
110
|
+
* Implementation note: we drain the inner stream manually so the final
|
|
111
|
+
* ChatResponse returned by LLMService.chatStream is propagated as this
|
|
112
|
+
* generator's return value. Using yield* doesn't carry the return value
|
|
113
|
+
* through TS's AsyncGenerator<T, R> type inference in this version of
|
|
114
|
+
* TypeScript, so we wrap with an inner for-await and explicit return.
|
|
115
|
+
*/
|
|
116
|
+
async *streamResponse(messages?: ChatMessage[]): AsyncGenerator<StreamChunk, ChatResponse> {
|
|
117
|
+
const msgs = messages || this.session.getMessages();
|
|
118
|
+
const inner = this.llm.chatStream(msgs);
|
|
119
|
+
let next = await inner.next();
|
|
120
|
+
while (!next.done) {
|
|
121
|
+
// Forward chunks unchanged, but capture usage into the persistent
|
|
122
|
+
// cache stats store so the TUI Header / /cache command can read it.
|
|
123
|
+
if (next.value && next.value.type === 'usage') {
|
|
124
|
+
try { this.cacheStats.record(next.value.usage); } catch { /* best-effort */ }
|
|
125
|
+
}
|
|
126
|
+
yield next.value;
|
|
127
|
+
next = await inner.next();
|
|
128
|
+
}
|
|
129
|
+
// The generator's return value (ChatResponse with usage) is propagated
|
|
130
|
+
// to callers via `for await ... await streamResponse.next()` semantics.
|
|
131
|
+
return next.value;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Run a single prompt (non-interactive mode)
|
|
136
|
+
*
|
|
137
|
+
* v2.2.4 (port from v2.1.0): persistence of the assistant message
|
|
138
|
+
* uses addMessageSafe, which drops the message if it contains
|
|
139
|
+
* pollution markers like "[已中断]".
|
|
140
|
+
*
|
|
141
|
+
* v2.2.5 (product 0.4.2): persistence also strips blocks
|
|
142
|
+
* (and similar reasoning delimiters) when showThinking is false,
|
|
143
|
+
* so the conversation log stays compact.
|
|
144
|
+
*
|
|
145
|
+
* v3.0.0: yields structured StreamChunks; final usage is captured
|
|
146
|
+
* into onUsage callback for cache stats persistence.
|
|
147
|
+
*/
|
|
148
|
+
async runPrompt(prompt: string, onUsage?: (usage: Usage) => void): Promise<string> {
|
|
149
|
+
this.session.addMessage('user', prompt);
|
|
150
|
+
|
|
151
|
+
let fullResponse = '';
|
|
152
|
+
let finalUsage: ChatResponse['usage'] | undefined;
|
|
153
|
+
try {
|
|
154
|
+
for await (const chunk of this.streamResponse()) {
|
|
155
|
+
if (chunk.type === 'text' && chunk.content) {
|
|
156
|
+
process.stdout.write(chunk.content);
|
|
157
|
+
fullResponse += chunk.content;
|
|
158
|
+
} else if (chunk.type === 'usage') {
|
|
159
|
+
finalUsage = chunk.usage;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
} catch (err) {
|
|
163
|
+
// Re-throw without persisting partial response. Persisting
|
|
164
|
+
// truncated output here was the source of the [已中断] loop in
|
|
165
|
+
// v2.2.3.
|
|
166
|
+
throw err;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
console.log();
|
|
170
|
+
// v2.2.5: compress blocks before persisting.
|
|
171
|
+
const toPersist = compressThinking(fullResponse, this.showThinking);
|
|
172
|
+
this.session.addMessageSafe('assistant', toPersist);
|
|
173
|
+
|
|
174
|
+
// v3.0.0: forward usage to caller (CLI single-shot mode records stats too)
|
|
175
|
+
if (finalUsage && onUsage) {
|
|
176
|
+
try { onUsage(finalUsage); } catch { /* best-effort */ }
|
|
177
|
+
}
|
|
178
|
+
return fullResponse;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache fingerprints — small, stable hashes used to detect when the
|
|
3
|
+
* "cacheable prefix" of an LLM request changes between rounds.
|
|
4
|
+
*
|
|
5
|
+
* Why this exists:
|
|
6
|
+
* Anthropic prompt cache and DeepSeek automatic prefix cache both break
|
|
7
|
+
* when the request body changes upstream of the cache breakpoint. To
|
|
8
|
+
* debug "why is my hit-rate 0%", we want a quick fingerprint of the
|
|
9
|
+
* things that *should* be cacheable (tools + system prefix) so we can
|
|
10
|
+
* log it on every request and compare across rounds.
|
|
11
|
+
*
|
|
12
|
+
* The fingerprint itself is NOT used to decide anything (the upstream
|
|
13
|
+
* provider does the actual cache lookup). It's purely a debug /
|
|
14
|
+
* observability tool — same role as `reasoning_content` in the message
|
|
15
|
+
* schema: captured for transparency, not for control flow.
|
|
16
|
+
*
|
|
17
|
+
* Implementation:
|
|
18
|
+
* - Uses stableStringify (sibling module) so logically equal objects
|
|
19
|
+
* always produce the same JSON.
|
|
20
|
+
* - Hashes with sha256 (Node built-in `crypto`), truncated to 16 chars.
|
|
21
|
+
* 16 hex chars = 64 bits, which is plenty for collision-resistance
|
|
22
|
+
* across a single user's session.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { createHash } from 'crypto';
|
|
26
|
+
import type { Tool } from '../tools/types.js';
|
|
27
|
+
import type { SystemSegment } from '../prompts/index.js';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Hash a stable JSON representation of `value` and return a 16-char hex prefix.
|
|
31
|
+
* Recursively normalizes object keys (sorted) so insertion order does not
|
|
32
|
+
* affect the output.
|
|
33
|
+
*/
|
|
34
|
+
export function fingerprint(value: unknown): string {
|
|
35
|
+
// stableStringify is imported lazily to avoid a circular dep — fingerprint
|
|
36
|
+
// is consumed by both providers (which sit above utils/) and the cache
|
|
37
|
+
// stats store (which sits next to fingerprint). Top-level import works
|
|
38
|
+
// because utils has no other consumers besides fingerprint, so we just
|
|
39
|
+
// import statically.
|
|
40
|
+
return createHash('sha256')
|
|
41
|
+
.update(stableStringifyLocal(value))
|
|
42
|
+
.digest('hex')
|
|
43
|
+
.slice(0, 16);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Compute a fingerprint of the registered tool schemas.
|
|
48
|
+
*
|
|
49
|
+
* We deliberately pick only `name`, `description`, and `inputSchema` — the
|
|
50
|
+
* three fields that matter for the upstream prompt cache. Other Tool
|
|
51
|
+
* metadata (version strings, descriptions of internal handlers, etc) is
|
|
52
|
+
* ignored because it never reaches the wire.
|
|
53
|
+
*/
|
|
54
|
+
export function fingerprintTools(tools: Tool[]): string {
|
|
55
|
+
const minimal = tools.map(t => ({
|
|
56
|
+
name: t.name,
|
|
57
|
+
description: t.description,
|
|
58
|
+
inputSchema: t.inputSchema,
|
|
59
|
+
}));
|
|
60
|
+
return fingerprint({ tools: minimal });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Compute a fingerprint of the system prompt's *immutable* portion.
|
|
65
|
+
*
|
|
66
|
+
* Volatile segments (NWT history, current time) are deliberately excluded
|
|
67
|
+
* because they change between rounds and would defeat the fingerprint's
|
|
68
|
+
* purpose as a "did the cacheable prefix change?" signal.
|
|
69
|
+
*/
|
|
70
|
+
export function fingerprintSystemPrefix(segments: SystemSegment[]): string {
|
|
71
|
+
const immutable = segments.filter(s => !s.volatile);
|
|
72
|
+
return fingerprint({ system: immutable.map(s => ({ name: s.name, content: s.content })) });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// -- Local copy of stableStringify to avoid a circular dep. This must stay
|
|
76
|
+
// byte-for-byte identical to src/utils/stableStringify.ts. If you change
|
|
77
|
+
// one, change both.
|
|
78
|
+
|
|
79
|
+
function stableStringifyLocal(value: unknown): string {
|
|
80
|
+
if (value === null) return 'null';
|
|
81
|
+
if (value === undefined) return 'null';
|
|
82
|
+
const t = typeof value;
|
|
83
|
+
if (t === 'string') return JSON.stringify(value);
|
|
84
|
+
if (t === 'number') return Number.isFinite(value as number) ? String(value) : 'null';
|
|
85
|
+
if (t === 'boolean') return value ? 'true' : 'false';
|
|
86
|
+
if (Array.isArray(value)) {
|
|
87
|
+
return '[' + value.map((v) => stableStringifyLocal(v)).join(',') + ']';
|
|
88
|
+
}
|
|
89
|
+
if (t === 'object') {
|
|
90
|
+
const obj = value as Record<string, unknown>;
|
|
91
|
+
const keys = Object.keys(obj).sort();
|
|
92
|
+
const parts: string[] = [];
|
|
93
|
+
for (const k of keys) {
|
|
94
|
+
const v = obj[k];
|
|
95
|
+
if (v === undefined) continue;
|
|
96
|
+
parts.push(JSON.stringify(k) + ':' + stableStringifyLocal(v));
|
|
97
|
+
}
|
|
98
|
+
return '{' + parts.join(',') + '}';
|
|
99
|
+
}
|
|
100
|
+
return 'null';
|
|
101
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache subsystem barrel. Re-exports the public surface used by the TUI,
|
|
3
|
+
* the App singleton, and the LLM service.
|
|
4
|
+
*
|
|
5
|
+
* Modules:
|
|
6
|
+
* - fingerprint.ts: stable hashes for tools + immutable system prefix
|
|
7
|
+
* - stats.ts: CacheStatsStore — per-round hit/miss + cost savings
|
|
8
|
+
* - volatile.ts: VolatileScratch — per-round scratch that never reaches the API
|
|
9
|
+
* - smartModel.ts: smart-model routing hook (simple → mini, complex → main)
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export { fingerprint, fingerprintTools, fingerprintSystemPrefix } from './fingerprint.js';
|
|
13
|
+
export { CacheStatsStore, estimateSavingsCNY, type CacheStats, type CacheSnapshot } from './stats.js';
|
|
14
|
+
export { VolatileScratch } from './volatile.js';
|
|
15
|
+
export { shouldDowngrade, type SmartModelDecision } from './smartModel.js';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smart-model routing (P3 of the Reasonix plan).
|
|
3
|
+
*
|
|
4
|
+
* Idea: classify the user's input as "simple" or "complex" and route
|
|
5
|
+
* simple queries to a cheaper / faster model while keeping the main model
|
|
6
|
+
* for complex work. The classification is intentionally trivial — prompt
|
|
7
|
+
* length + presence of recent tool calls — because the cost of a
|
|
8
|
+
* misclassification (a slightly worse answer on a simple query) is much
|
|
9
|
+
* lower than the cost of running a frontier model on every greeting.
|
|
10
|
+
*
|
|
11
|
+
* Trade-off: each routing decision adds one model swap (different
|
|
12
|
+
* `model` field on the wire). If the cache prefix is keyed on the model
|
|
13
|
+
* string, the cache miss rate can spike on every simple query. To keep
|
|
14
|
+
* the prefix stable, callers should pass a *family* (e.g. "anthropic" or
|
|
15
|
+
* "deepseek") rather than a specific model name when forwarding the
|
|
16
|
+
* prompt to the downstream provider — that's outside the scope of this
|
|
17
|
+
* hook; this module only answers the question "should I downgrade?".
|
|
18
|
+
*
|
|
19
|
+
* v3.0.0: shipped as a stub. Real decision logic is conservative
|
|
20
|
+
* (downgrade only when prompt is short AND no recent tool activity).
|
|
21
|
+
* Future iterations can add heuristic or classifier-based routing.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import type { ChatMessage } from '../types.js';
|
|
25
|
+
|
|
26
|
+
export interface SmartModelDecision {
|
|
27
|
+
/** Should we route this turn to a cheaper model? */
|
|
28
|
+
downgrade: boolean;
|
|
29
|
+
/** Why we made this call — useful for `/cache` debug output. */
|
|
30
|
+
reason: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Heuristic: downgrade when
|
|
35
|
+
* 1. The latest user message is short (< 200 chars), AND
|
|
36
|
+
* 2. The last 3 messages contain no assistant tool_calls.
|
|
37
|
+
*
|
|
38
|
+
* This covers greetings, one-line questions, and simple follow-ups
|
|
39
|
+
* ("thanks", "ok", "explain this one line") while routing anything
|
|
40
|
+
* that triggered a tool to the main model.
|
|
41
|
+
*/
|
|
42
|
+
export function shouldDowngrade(messages: ChatMessage[], lastUserInput: string): SmartModelDecision {
|
|
43
|
+
if (lastUserInput.length > 200) {
|
|
44
|
+
return { downgrade: false, reason: 'prompt-too-long' };
|
|
45
|
+
}
|
|
46
|
+
const recent = messages.slice(-3);
|
|
47
|
+
const hasRecentToolCalls = recent.some(m => m.tool_calls && m.tool_calls.length > 0);
|
|
48
|
+
if (hasRecentToolCalls) {
|
|
49
|
+
return { downgrade: false, reason: 'recent-tool-call' };
|
|
50
|
+
}
|
|
51
|
+
return { downgrade: true, reason: 'short-no-tools' };
|
|
52
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache statistics store.
|
|
3
|
+
*
|
|
4
|
+
* Tracks per-round cache hit/miss token counts across the user's local
|
|
5
|
+
* sessions and persists them to `~/.thatgfsj/cache-stats.json`. The store
|
|
6
|
+
* is consumed by:
|
|
7
|
+
* - the TUI Header (`⚡ 命中率 87%`)
|
|
8
|
+
* - the /cache CLI command (detailed breakdown)
|
|
9
|
+
* - the /cache reset command (clears the file)
|
|
10
|
+
*
|
|
11
|
+
* Provider-agnostic. Anthropic reports cache_creation_input_tokens /
|
|
12
|
+
* cache_read_input_tokens; DeepSeek reports prompt_cache_hit_tokens /
|
|
13
|
+
* prompt_cache_miss_tokens; OpenAI does not surface any cache stats at
|
|
14
|
+
* all (their automatic cache is invisible to the client). We normalize
|
|
15
|
+
* both shapes into a single internal model.
|
|
16
|
+
*
|
|
17
|
+
* Cost estimation is intentionally simplified:
|
|
18
|
+
* - cache_read = 10% of normal input price
|
|
19
|
+
* - cache_creation = 125% of normal input price (Anthropic charges extra
|
|
20
|
+
* on first write)
|
|
21
|
+
* - We compare against the baseline "what would this have cost without
|
|
22
|
+
* caching" and report the delta as estimated savings.
|
|
23
|
+
*
|
|
24
|
+
* Prices are in CNY per million tokens. Defaults reflect Anthropic Claude
|
|
25
|
+
* Sonnet on the official API; users on cheaper providers may see savings
|
|
26
|
+
* proportional to their actual price floor. The numbers are *estimates*
|
|
27
|
+
* — the goal is to give the user a sense of order of magnitude, not a
|
|
28
|
+
* billable invoice.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
32
|
+
import { dirname, join } from 'path';
|
|
33
|
+
import { homedir } from 'os';
|
|
34
|
+
import type { Usage } from '../types.js';
|
|
35
|
+
|
|
36
|
+
/** Per-million-token price in CNY. Adjust if your provider differs significantly. */
|
|
37
|
+
const PRICE = {
|
|
38
|
+
normalInput: 3,
|
|
39
|
+
cacheRead: 0.3,
|
|
40
|
+
cacheCreation: 3.75,
|
|
41
|
+
output: 15,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Maximum number of recent rounds kept in the history array (rolling window). */
|
|
45
|
+
const HISTORY_LIMIT = 50;
|
|
46
|
+
|
|
47
|
+
export interface CacheStats {
|
|
48
|
+
/** Total tokens served from cache (Anthropic cache_read / DeepSeek hit). */
|
|
49
|
+
totalReadTokens: number;
|
|
50
|
+
/** Total tokens written into cache (Anthropic cache_creation / DeepSeek miss prefix written). */
|
|
51
|
+
totalCreationTokens: number;
|
|
52
|
+
/** Total input tokens (prompt_tokens). */
|
|
53
|
+
totalInputTokens: number;
|
|
54
|
+
/** Total output tokens. */
|
|
55
|
+
totalOutputTokens: number;
|
|
56
|
+
/** Number of completed rounds. */
|
|
57
|
+
totalRequests: number;
|
|
58
|
+
/** Last N round snapshots (oldest first). */
|
|
59
|
+
history: Array<{
|
|
60
|
+
ts: number;
|
|
61
|
+
read: number;
|
|
62
|
+
creation: number;
|
|
63
|
+
input: number;
|
|
64
|
+
output: number;
|
|
65
|
+
hitRate: number;
|
|
66
|
+
}>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface CacheSnapshot extends CacheStats {
|
|
70
|
+
/** 0..1 ratio of cached tokens to total input tokens. */
|
|
71
|
+
hitRate: number;
|
|
72
|
+
/** Estimated CNY saved vs uncached baseline. */
|
|
73
|
+
estimatedSavingsCNY: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const EMPTY_STATS: CacheStats = {
|
|
77
|
+
totalReadTokens: 0,
|
|
78
|
+
totalCreationTokens: 0,
|
|
79
|
+
totalInputTokens: 0,
|
|
80
|
+
totalOutputTokens: 0,
|
|
81
|
+
totalRequests: 0,
|
|
82
|
+
history: [],
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Estimate the CNY saved by cache_read vs a hypothetical uncached baseline.
|
|
87
|
+
* Negative values are clamped to zero (e.g. a relay station that returned
|
|
88
|
+
* bogus cache stats).
|
|
89
|
+
*/
|
|
90
|
+
export function estimateSavingsCNY(stats: CacheStats): number {
|
|
91
|
+
const baselineCost = (stats.totalReadTokens / 1e6) * PRICE.normalInput;
|
|
92
|
+
const cachedCost = (stats.totalReadTokens / 1e6) * PRICE.cacheRead;
|
|
93
|
+
return Math.max(0, baselineCost - cachedCost);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Persists cache stats to disk. Safe to call from a hot path — uses atomic
|
|
98
|
+
* write (write to .tmp, rename) to avoid corruption on crash.
|
|
99
|
+
*/
|
|
100
|
+
export class CacheStatsStore {
|
|
101
|
+
private stats: CacheStats;
|
|
102
|
+
private path: string;
|
|
103
|
+
|
|
104
|
+
constructor(path?: string) {
|
|
105
|
+
this.path = path ?? join(homedir(), '.thatgfsj', 'cache-stats.json');
|
|
106
|
+
this.stats = this.load();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Record a single round's usage. Provider-agnostic: accepts both Anthropic
|
|
111
|
+
* (cache_creation_input_tokens / cache_read_input_tokens) and DeepSeek
|
|
112
|
+
* (prompt_cache_hit_tokens / prompt_cache_miss_tokens) shapes. If a
|
|
113
|
+
* provider returned neither (e.g. OpenAI), the call is a no-op apart
|
|
114
|
+
* from updating totalRequest count.
|
|
115
|
+
*/
|
|
116
|
+
record(usage: Usage): void {
|
|
117
|
+
const read = usage.cache_read_input_tokens ?? usage.prompt_cache_hit_tokens ?? 0;
|
|
118
|
+
const creation = usage.cache_creation_input_tokens
|
|
119
|
+
?? (usage.prompt_cache_miss_tokens ?? 0);
|
|
120
|
+
const input = usage.prompt_tokens ?? 0;
|
|
121
|
+
const output = usage.completion_tokens ?? 0;
|
|
122
|
+
|
|
123
|
+
// Avoid double-counting: if the provider reported both cache_creation
|
|
124
|
+
// and prompt_cache_miss_tokens we'd otherwise sum them. The ?? in the
|
|
125
|
+
// chain above only uses prompt_cache_miss_tokens when
|
|
126
|
+
// cache_creation_input_tokens is undefined, which is what we want.
|
|
127
|
+
|
|
128
|
+
this.stats.totalReadTokens += read;
|
|
129
|
+
this.stats.totalCreationTokens += creation;
|
|
130
|
+
this.stats.totalInputTokens += input;
|
|
131
|
+
this.stats.totalOutputTokens += output;
|
|
132
|
+
this.stats.totalRequests += 1;
|
|
133
|
+
|
|
134
|
+
// Per-round snapshot (for /cache command history view).
|
|
135
|
+
const total = read + (input - read);
|
|
136
|
+
const hitRate = total > 0 ? read / total : 0;
|
|
137
|
+
this.stats.history.push({ ts: Date.now(), read, creation, input, output, hitRate });
|
|
138
|
+
if (this.stats.history.length > HISTORY_LIMIT) {
|
|
139
|
+
this.stats.history.splice(0, this.stats.history.length - HISTORY_LIMIT);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.save();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Reset the stats. Used by `/cache reset`.
|
|
147
|
+
*/
|
|
148
|
+
reset(): void {
|
|
149
|
+
this.stats = { ...EMPTY_STATS, history: [] };
|
|
150
|
+
this.save();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Read-only snapshot for rendering (TUI Header, /cache command).
|
|
155
|
+
*/
|
|
156
|
+
snapshot(): CacheSnapshot {
|
|
157
|
+
const input = this.stats.totalInputTokens;
|
|
158
|
+
const read = this.stats.totalReadTokens;
|
|
159
|
+
const hitRate = input > 0 ? read / input : 0;
|
|
160
|
+
return {
|
|
161
|
+
...this.stats,
|
|
162
|
+
hitRate,
|
|
163
|
+
estimatedSavingsCNY: estimateSavingsCNY(this.stats),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Direct accessor for tests. */
|
|
168
|
+
get raw(): CacheStats { return this.stats; }
|
|
169
|
+
|
|
170
|
+
// -- I/O --
|
|
171
|
+
|
|
172
|
+
private load(): CacheStats {
|
|
173
|
+
try {
|
|
174
|
+
if (!existsSync(this.path)) return { ...EMPTY_STATS, history: [] };
|
|
175
|
+
const txt = readFileSync(this.path, 'utf-8');
|
|
176
|
+
const parsed = JSON.parse(txt);
|
|
177
|
+
// Tolerate partial / older shapes.
|
|
178
|
+
return {
|
|
179
|
+
totalReadTokens: parsed.totalReadTokens ?? 0,
|
|
180
|
+
totalCreationTokens: parsed.totalCreationTokens ?? 0,
|
|
181
|
+
totalInputTokens: parsed.totalInputTokens ?? 0,
|
|
182
|
+
totalOutputTokens: parsed.totalOutputTokens ?? 0,
|
|
183
|
+
totalRequests: parsed.totalRequests ?? 0,
|
|
184
|
+
history: Array.isArray(parsed.history) ? parsed.history : [],
|
|
185
|
+
};
|
|
186
|
+
} catch {
|
|
187
|
+
return { ...EMPTY_STATS, history: [] };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private save(): void {
|
|
192
|
+
try {
|
|
193
|
+
mkdirSync(dirname(this.path), { recursive: true });
|
|
194
|
+
writeFileSync(this.path, JSON.stringify(this.stats, null, 2), 'utf-8');
|
|
195
|
+
} catch {
|
|
196
|
+
// best-effort persistence; do not crash the chat loop on disk errors
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|