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/src/llm/index.ts
CHANGED
|
@@ -1,200 +1,265 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LLM Service - Factory for creating providers
|
|
3
|
-
* Supports all providers + custom relay stations (中转站)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
import
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
options
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
1
|
+
/**
|
|
2
|
+
* LLM Service - Factory for creating providers
|
|
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.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import chalk from 'chalk';
|
|
15
|
+
import type { ChatMessage, ChatResponse, ChatOptions, ToolCall, StreamChunk } from '../types.js';
|
|
16
|
+
import type { Tool } from '../tools/types.js';
|
|
17
|
+
import type { LLMProvider } from './provider.js';
|
|
18
|
+
import type { AIConfig, Config, ProviderName } from '../config/types.js';
|
|
19
|
+
import { PROVIDERS } from '../config/providers.js';
|
|
20
|
+
import { OpenAIProvider } from './openai.js';
|
|
21
|
+
import { AnthropicProvider } from './anthropic.js';
|
|
22
|
+
import { GeminiProvider } from './gemini.js';
|
|
23
|
+
|
|
24
|
+
export class LLMService {
|
|
25
|
+
private provider: LLMProvider;
|
|
26
|
+
private tools: Map<string, Tool> = new Map();
|
|
27
|
+
private apiKey: string;
|
|
28
|
+
|
|
29
|
+
constructor(provider: LLMProvider, apiKey: string) {
|
|
30
|
+
this.provider = provider;
|
|
31
|
+
this.apiKey = apiKey;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static fromConfig(config: AIConfig & { cache?: Config['cache'] }): LLMService {
|
|
35
|
+
const providerName = config.provider || 'siliconflow';
|
|
36
|
+
const providerConfig = PROVIDERS[providerName];
|
|
37
|
+
|
|
38
|
+
const providerCfg = {
|
|
39
|
+
apiKey: config.apiKey || '',
|
|
40
|
+
model: config.model || providerConfig.defaultModel,
|
|
41
|
+
baseUrl: config.baseUrl || providerConfig.baseUrl,
|
|
42
|
+
temperature: config.temperature ?? 0.7,
|
|
43
|
+
maxTokens: config.maxTokens ?? 4096,
|
|
44
|
+
// v3.0.0: forward cache policy. Anthropic reads this to decide
|
|
45
|
+
// whether to attach cache_control markers; other providers ignore it.
|
|
46
|
+
cache: config.cache ?? { enabled: true, ttl: '5m', strategy: 'auto' as const },
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const format = providerConfig.format;
|
|
50
|
+
let provider: LLMProvider;
|
|
51
|
+
|
|
52
|
+
switch (format) {
|
|
53
|
+
case 'anthropic':
|
|
54
|
+
provider = new AnthropicProvider(providerCfg);
|
|
55
|
+
break;
|
|
56
|
+
case 'gemini':
|
|
57
|
+
provider = new GeminiProvider(providerCfg);
|
|
58
|
+
break;
|
|
59
|
+
default:
|
|
60
|
+
provider = new OpenAIProvider(providerCfg);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return new LLMService(provider, providerCfg.apiKey);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
registerTools(tools: Tool[]): void {
|
|
67
|
+
for (const tool of tools) {
|
|
68
|
+
this.tools.set(tool.name, tool);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getProviderName(): string { return this.provider.name; }
|
|
73
|
+
hasApiKey(): boolean { return !!this.apiKey; }
|
|
74
|
+
|
|
75
|
+
async chat(messages: ChatMessage[], options?: ChatOptions): Promise<ChatResponse> {
|
|
76
|
+
if (!this.hasApiKey()) throw new Error(this.getNoKeyMessage());
|
|
77
|
+
const toolsArray = [...this.tools.values()];
|
|
78
|
+
return this.provider.chat(messages, options, toolsArray.length > 0 ? toolsArray : undefined);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Streaming chat with agent loop (tool call support).
|
|
83
|
+
*
|
|
84
|
+
* Structured chunk protocol (replaces the @@TOOL@@ sentinel strings):
|
|
85
|
+
* { type: 'text', content: string } model output, accumulate + render
|
|
86
|
+
* { type: 'tool_calls', toolCalls: ToolCall[] } dispatch tools, results will
|
|
87
|
+
* be folded into the next request
|
|
88
|
+
* { type: 'thinking', content: string } reasoning text (consumers may
|
|
89
|
+
* display in debug mode)
|
|
90
|
+
* { type: 'usage', usage: Usage } cache hit/miss + token counts;
|
|
91
|
+
* emitted on the final round
|
|
92
|
+
*
|
|
93
|
+
* Returns the final ChatResponse of the agent loop when done.
|
|
94
|
+
*/
|
|
95
|
+
async *chatStream(
|
|
96
|
+
messages: ChatMessage[],
|
|
97
|
+
options?: ChatOptions & { maxIterations?: number }
|
|
98
|
+
): AsyncGenerator<StreamChunk, ChatResponse> {
|
|
99
|
+
if (!this.hasApiKey()) throw new Error(this.getNoKeyMessage());
|
|
100
|
+
|
|
101
|
+
const maxIterations = options?.maxIterations ?? 10;
|
|
102
|
+
let currentMessages = [...messages];
|
|
103
|
+
let iterations = 0;
|
|
104
|
+
let lastUsage: ChatResponse['usage'] | undefined;
|
|
105
|
+
|
|
106
|
+
while (iterations < maxIterations) {
|
|
107
|
+
iterations++;
|
|
108
|
+
const toolsArray = [...this.tools.values()];
|
|
109
|
+
const hasTools = toolsArray.length > 0;
|
|
110
|
+
|
|
111
|
+
let fullContent = '';
|
|
112
|
+
let detectedToolCalls: ToolCall[] | undefined;
|
|
113
|
+
|
|
114
|
+
// Forward stream chunks from the provider. We collect text internally for
|
|
115
|
+
// tool-call persistence but always re-emit the original chunks unchanged.
|
|
116
|
+
const stream = this.provider.chatStream(currentMessages, options, hasTools ? toolsArray : undefined);
|
|
117
|
+
|
|
118
|
+
for await (const chunk of stream) {
|
|
119
|
+
if (chunk.type === 'text' && chunk.content) {
|
|
120
|
+
fullContent += chunk.content;
|
|
121
|
+
yield chunk;
|
|
122
|
+
} else if (chunk.type === 'tool_calls' && chunk.toolCalls) {
|
|
123
|
+
detectedToolCalls = chunk.toolCalls;
|
|
124
|
+
// Don't yield the raw tool_calls chunk here — we emit one combined
|
|
125
|
+
// chunk after persisting the assistant message so consumers don't
|
|
126
|
+
// double-render.
|
|
127
|
+
} else if (chunk.type === 'thinking') {
|
|
128
|
+
yield chunk;
|
|
129
|
+
} else if (chunk.type === 'usage') {
|
|
130
|
+
lastUsage = chunk.usage;
|
|
131
|
+
yield chunk;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// If we got tool calls, execute them and loop
|
|
136
|
+
if (detectedToolCalls && detectedToolCalls.length > 0) {
|
|
137
|
+
// Add assistant message with tool calls (append-only, preserves prefix cache)
|
|
138
|
+
currentMessages.push({
|
|
139
|
+
role: 'assistant',
|
|
140
|
+
content: fullContent || '',
|
|
141
|
+
tool_calls: detectedToolCalls,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Execute each tool and emit a single structured tool_calls chunk
|
|
145
|
+
// describing the dispatch plan. Per-tool results are appended to
|
|
146
|
+
// currentMessages but NOT yielded as additional chunks — consumers
|
|
147
|
+
// that want to display result text can introspect tool_calls[*].result
|
|
148
|
+
// on a synthesized combined chunk, OR we re-emit one tool_calls chunk
|
|
149
|
+
// at the end with results attached. We choose the simpler approach:
|
|
150
|
+
// emit ONE tool_calls chunk per iteration with all the calls; results
|
|
151
|
+
// are surfaced through the next assistant turn's text content.
|
|
152
|
+
//
|
|
153
|
+
// v3.0.0 Tool-call Repair (Reasonix P2): on tool failure we APPEND
|
|
154
|
+
// a `[TOOL_REPAIR]` system message rather than mutating the existing
|
|
155
|
+
// tool_call message. This preserves the upstream cache prefix —
|
|
156
|
+
// re-writing an earlier message would shift the prefix by N bytes
|
|
157
|
+
// and bust the cache for every subsequent round.
|
|
158
|
+
for (const toolCall of detectedToolCalls) {
|
|
159
|
+
const tool = this.tools.get(toolCall.function.name);
|
|
160
|
+
|
|
161
|
+
if (!tool) {
|
|
162
|
+
const errMsg = `Tool "${toolCall.function.name}" not found`;
|
|
163
|
+
// Repair message — explain the failure and tell the model to
|
|
164
|
+
// try a different tool. Cache-safe because we are APPENDING,
|
|
165
|
+
// never modifying existing messages.
|
|
166
|
+
currentMessages.push({
|
|
167
|
+
role: 'system',
|
|
168
|
+
content: `[TOOL_REPAIR] Previous tool_call "${toolCall.function.name}" (id=${toolCall.id}) failed: ${errMsg}. Available tools: ${[...this.tools.keys()].join(', ')}.`,
|
|
169
|
+
});
|
|
170
|
+
currentMessages.push({
|
|
171
|
+
role: 'tool',
|
|
172
|
+
content: errMsg,
|
|
173
|
+
tool_call_id: toolCall.id,
|
|
174
|
+
name: toolCall.function.name,
|
|
175
|
+
});
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
const params = JSON.parse(toolCall.function.arguments || '{}');
|
|
181
|
+
const result = await tool.execute(params);
|
|
182
|
+
const output = result.success ? (result.output || JSON.stringify(result.data)) : (result.error || 'Tool failed');
|
|
183
|
+
|
|
184
|
+
currentMessages.push({
|
|
185
|
+
role: 'tool',
|
|
186
|
+
content: output,
|
|
187
|
+
tool_call_id: toolCall.id,
|
|
188
|
+
name: toolCall.function.name,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
if (!result.success) {
|
|
192
|
+
// Soft failure: tool returned success=false. Same repair pattern.
|
|
193
|
+
currentMessages.push({
|
|
194
|
+
role: 'system',
|
|
195
|
+
content: `[TOOL_REPAIR] Tool "${toolCall.function.name}" returned success=false: ${output}. Consider correcting the arguments and retrying.`,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
} catch (error: any) {
|
|
199
|
+
const errMsg = `Error: ${error.message}`;
|
|
200
|
+
currentMessages.push({
|
|
201
|
+
role: 'tool',
|
|
202
|
+
content: errMsg,
|
|
203
|
+
tool_call_id: toolCall.id,
|
|
204
|
+
name: toolCall.function.name,
|
|
205
|
+
});
|
|
206
|
+
// Hard failure: tool.execute threw. Repair message so the model
|
|
207
|
+
// can see the failure next round and adjust (e.g. fix a path
|
|
208
|
+
// typo, retry without the optional arg).
|
|
209
|
+
currentMessages.push({
|
|
210
|
+
role: 'system',
|
|
211
|
+
content: `[TOOL_REPAIR] Tool "${toolCall.function.name}" threw an exception: ${errMsg}. Inspect the arguments and retry with a corrected call.`,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Emit one tool_calls chunk for this iteration. TUI renders this as a
|
|
217
|
+
// "tool invoked" entry. Per-tool outputs are summarized on the next
|
|
218
|
+
// assistant turn.
|
|
219
|
+
yield { type: 'tool_calls', toolCalls: detectedToolCalls };
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// No tool calls - done. Return final response (with usage if we have it).
|
|
224
|
+
return {
|
|
225
|
+
content: fullContent,
|
|
226
|
+
role: 'assistant',
|
|
227
|
+
usage: lastUsage,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return { content: '[Agent loop exceeded maximum iterations]', role: 'assistant' };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private truncateArgs(args: string): string {
|
|
235
|
+
try {
|
|
236
|
+
const obj = JSON.parse(args || '{}');
|
|
237
|
+
const entries = Object.entries(obj);
|
|
238
|
+
if (entries.length === 0) return '';
|
|
239
|
+
return entries.map(([k, v]) => {
|
|
240
|
+
const val = typeof v === 'string' && v.length > 50 ? v.slice(0, 50) + '...' : v;
|
|
241
|
+
return `${k}: ${JSON.stringify(val)}`;
|
|
242
|
+
}).join(', ');
|
|
243
|
+
} catch {
|
|
244
|
+
return args.length > 80 ? args.slice(0, 80) + '...' : args;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private getNoKeyMessage(): string {
|
|
249
|
+
return [
|
|
250
|
+
'❌ 未配置 API Key,无法调用 AI。',
|
|
251
|
+
'',
|
|
252
|
+
'请先运行: gfcode init',
|
|
253
|
+
'',
|
|
254
|
+
'或设置环境变量:',
|
|
255
|
+
' export SILICONFLOW_API_KEY="sk-..."',
|
|
256
|
+
' export OPENAI_API_KEY="sk-..."',
|
|
257
|
+
' export DEEPSEEK_API_KEY="sk-..."',
|
|
258
|
+
].join('\n');
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export type { LLMProvider, ProviderConfig, StreamChunk } from './provider.js';
|
|
263
|
+
export { OpenAIProvider } from './openai.js';
|
|
264
|
+
export { AnthropicProvider } from './anthropic.js';
|
|
265
|
+
export { GeminiProvider } from './gemini.js';
|