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.
Files changed (184) hide show
  1. package/CHANGELOG.md +192 -0
  2. package/DEVELOPMENT.md +286 -0
  3. package/dist/app/index.d.ts +40 -3
  4. package/dist/app/index.d.ts.map +1 -1
  5. package/dist/app/index.js +87 -12
  6. package/dist/app/index.js.map +1 -1
  7. package/dist/cache/fingerprint.d.ts +49 -0
  8. package/dist/cache/fingerprint.d.ts.map +1 -0
  9. package/dist/cache/fingerprint.js +100 -0
  10. package/dist/cache/fingerprint.js.map +1 -0
  11. package/dist/cache/index.d.ts +15 -0
  12. package/dist/cache/index.d.ts.map +1 -0
  13. package/dist/cache/index.js +15 -0
  14. package/dist/cache/index.js.map +1 -0
  15. package/dist/cache/smartModel.d.ts +40 -0
  16. package/dist/cache/smartModel.d.ts.map +1 -0
  17. package/dist/cache/smartModel.js +43 -0
  18. package/dist/cache/smartModel.js.map +1 -0
  19. package/dist/cache/stats.d.ts +93 -0
  20. package/dist/cache/stats.d.ts.map +1 -0
  21. package/dist/cache/stats.js +155 -0
  22. package/dist/cache/stats.js.map +1 -0
  23. package/dist/cache/volatile.d.ts +32 -0
  24. package/dist/cache/volatile.d.ts.map +1 -0
  25. package/dist/cache/volatile.js +44 -0
  26. package/dist/cache/volatile.js.map +1 -0
  27. package/dist/cmd/index.d.ts +21 -0
  28. package/dist/cmd/index.d.ts.map +1 -1
  29. package/dist/cmd/index.js +155 -31
  30. package/dist/cmd/index.js.map +1 -1
  31. package/dist/config/index.d.ts.map +1 -1
  32. package/dist/config/index.js +8 -0
  33. package/dist/config/index.js.map +1 -1
  34. package/dist/config/types.d.ts +14 -0
  35. package/dist/config/types.d.ts.map +1 -1
  36. package/dist/llm/anthropic.d.ts +55 -5
  37. package/dist/llm/anthropic.d.ts.map +1 -1
  38. package/dist/llm/anthropic.js +172 -26
  39. package/dist/llm/anthropic.js.map +1 -1
  40. package/dist/llm/index.d.ts +26 -8
  41. package/dist/llm/index.d.ts.map +1 -1
  42. package/dist/llm/index.js +83 -15
  43. package/dist/llm/index.js.map +1 -1
  44. package/dist/llm/openai.d.ts +28 -8
  45. package/dist/llm/openai.d.ts.map +1 -1
  46. package/dist/llm/openai.js +58 -8
  47. package/dist/llm/openai.js.map +1 -1
  48. package/dist/llm/provider.d.ts +40 -8
  49. package/dist/llm/provider.d.ts.map +1 -1
  50. package/dist/llm/provider.js +8 -1
  51. package/dist/llm/provider.js.map +1 -1
  52. package/dist/prompts/index.d.ts +32 -0
  53. package/dist/prompts/index.d.ts.map +1 -1
  54. package/dist/prompts/index.js +33 -11
  55. package/dist/prompts/index.js.map +1 -1
  56. package/dist/session/index.d.ts +43 -3
  57. package/dist/session/index.d.ts.map +1 -1
  58. package/dist/session/index.js +114 -17
  59. package/dist/session/index.js.map +1 -1
  60. package/dist/skills/brainstorming.js +33 -33
  61. package/dist/skills/code-review.js +35 -35
  62. package/dist/skills/executing-plans.js +18 -18
  63. package/dist/skills/frontend-design.js +25 -25
  64. package/dist/skills/git-workflow.js +26 -26
  65. package/dist/skills/improve-architecture.js +28 -28
  66. package/dist/skills/neuroweave.js +37 -37
  67. package/dist/skills/playwright.js +62 -62
  68. package/dist/skills/prototype.js +20 -20
  69. package/dist/skills/subagent.js +19 -19
  70. package/dist/skills/supabase.js +34 -34
  71. package/dist/skills/systematic-debugging.js +34 -34
  72. package/dist/skills/tdd.js +29 -29
  73. package/dist/skills/triage.js +25 -25
  74. package/dist/skills/verification.js +22 -22
  75. package/dist/skills/writing-plans.js +32 -32
  76. package/dist/tools/nwt.js +16 -16
  77. package/dist/tui/app.d.ts.map +1 -1
  78. package/dist/tui/app.js +9 -2
  79. package/dist/tui/app.js.map +1 -1
  80. package/dist/tui/components/ChatList.d.ts.map +1 -1
  81. package/dist/tui/components/ChatList.js +4 -3
  82. package/dist/tui/components/ChatList.js.map +1 -1
  83. package/dist/tui/components/Header.d.ts +8 -0
  84. package/dist/tui/components/Header.d.ts.map +1 -1
  85. package/dist/tui/components/Header.js +17 -3
  86. package/dist/tui/components/Header.js.map +1 -1
  87. package/dist/tui/components/InitWizard.d.ts +12 -1
  88. package/dist/tui/components/InitWizard.d.ts.map +1 -1
  89. package/dist/tui/components/InitWizard.js +58 -3
  90. package/dist/tui/components/InitWizard.js.map +1 -1
  91. package/dist/tui/components/ToolCall.d.ts +1 -1
  92. package/dist/tui/components/ToolCall.d.ts.map +1 -1
  93. package/dist/tui/components/ToolCall.js +4 -4
  94. package/dist/tui/components/ToolCall.js.map +1 -1
  95. package/dist/tui/hooks/useChat.d.ts +22 -0
  96. package/dist/tui/hooks/useChat.d.ts.map +1 -1
  97. package/dist/tui/hooks/useChat.js +127 -50
  98. package/dist/tui/hooks/useChat.js.map +1 -1
  99. package/dist/tui/hooks/useCommands.d.ts.map +1 -1
  100. package/dist/tui/hooks/useCommands.js +78 -0
  101. package/dist/tui/hooks/useCommands.js.map +1 -1
  102. package/dist/types.d.ts +79 -6
  103. package/dist/types.d.ts.map +1 -1
  104. package/dist/types.js +7 -0
  105. package/dist/types.js.map +1 -1
  106. package/dist/utils/stableStringify.d.ts +21 -0
  107. package/dist/utils/stableStringify.d.ts.map +1 -0
  108. package/dist/utils/stableStringify.js +53 -0
  109. package/dist/utils/stableStringify.js.map +1 -0
  110. package/dist/utils/thinking.d.ts +59 -0
  111. package/dist/utils/thinking.d.ts.map +1 -0
  112. package/dist/utils/thinking.js +104 -0
  113. package/dist/utils/thinking.js.map +1 -0
  114. package/install.bat +63 -0
  115. package/install.ps1 +238 -0
  116. package/install.sh +113 -0
  117. package/package.json +7 -4
  118. package/src/app/index.ts +180 -108
  119. package/src/cache/fingerprint.ts +101 -0
  120. package/src/cache/index.ts +15 -0
  121. package/src/cache/smartModel.ts +52 -0
  122. package/src/cache/stats.ts +199 -0
  123. package/src/cache/volatile.ts +47 -0
  124. package/src/cmd/index.tsx +288 -160
  125. package/src/config/index.ts +156 -148
  126. package/src/config/providers.ts +234 -234
  127. package/src/config/types.ts +67 -53
  128. package/src/hooks/index.ts +111 -111
  129. package/src/llm/anthropic.ts +388 -243
  130. package/src/llm/gemini.ts +169 -169
  131. package/src/llm/index.ts +265 -200
  132. package/src/llm/openai.ts +243 -196
  133. package/src/llm/provider.ts +66 -34
  134. package/src/prompts/index.ts +260 -220
  135. package/src/session/compactor.ts +103 -103
  136. package/src/session/index.ts +181 -87
  137. package/src/session/message.ts +42 -42
  138. package/src/skills/brainstorming.ts +43 -43
  139. package/src/skills/code-review.ts +45 -45
  140. package/src/skills/executing-plans.ts +27 -27
  141. package/src/skills/frontend-design.ts +35 -35
  142. package/src/skills/git-workflow.ts +36 -36
  143. package/src/skills/improve-architecture.ts +38 -38
  144. package/src/skills/index.ts +136 -136
  145. package/src/skills/neuroweave.ts +47 -47
  146. package/src/skills/playwright.ts +72 -72
  147. package/src/skills/prototype.ts +30 -30
  148. package/src/skills/subagent.ts +28 -28
  149. package/src/skills/supabase.ts +44 -44
  150. package/src/skills/systematic-debugging.ts +44 -44
  151. package/src/skills/tdd.ts +39 -39
  152. package/src/skills/triage.ts +35 -35
  153. package/src/skills/verification.ts +31 -31
  154. package/src/skills/writing-plans.ts +42 -42
  155. package/src/tools/nwt.ts +598 -598
  156. package/src/tools/types.ts +122 -122
  157. package/src/tui/app.tsx +199 -186
  158. package/src/tui/components/ChatList.tsx +41 -32
  159. package/src/tui/components/ChatMessage.tsx +54 -54
  160. package/src/tui/components/Header.tsx +56 -28
  161. package/src/tui/components/InitWizard.tsx +217 -132
  162. package/src/tui/components/Markdown.tsx +35 -35
  163. package/src/tui/components/ModelSelector.tsx +87 -87
  164. package/src/tui/components/StatusBar.tsx +30 -30
  165. package/src/tui/components/Thinking.tsx +17 -17
  166. package/src/tui/components/ToolCall.tsx +102 -71
  167. package/src/tui/components/UserInput.tsx +131 -131
  168. package/src/tui/hooks/useChat.ts +287 -194
  169. package/src/tui/hooks/useCommands.ts +234 -154
  170. package/src/tui/index.ts +6 -6
  171. package/src/tui/welcome.ts +177 -177
  172. package/src/types.ts +104 -42
  173. package/src/utils/diff.ts +71 -71
  174. package/src/utils/project.ts +99 -99
  175. package/src/utils/stableStringify.ts +47 -0
  176. package/src/utils/thinking.ts +119 -0
  177. package/tests/cache/fingerprint.test.ts +75 -0
  178. package/tests/cache/stableStringify.test.ts +43 -0
  179. package/tests/cache/stats.test.ts +146 -0
  180. package/tests/cache/volatile.test.ts +75 -0
  181. package/tests/smoke-pollution.mjs +78 -0
  182. package/tests/smoke-thinking.mjs +110 -0
  183. package/tests/smoke-tool-stream.mjs +69 -0
  184. package/tests/smoke-tool.mjs +117 -0
@@ -1,194 +1,287 @@
1
- import { useState, useCallback, useRef } from 'react';
2
- import type { MessageData } from '../components/ChatMessage.js';
3
- import type { ToolCallData } from '../components/ToolCall.js';
4
- import type { App } from '../../app/index.js';
5
-
6
- interface ChatState {
7
- messages: MessageData[];
8
- isThinking: boolean;
9
- streaming: string;
10
- streamingToolCalls: ToolCallData[];
11
- queuedMessage: string | null;
12
- }
13
-
14
- export function useChat(app: App) {
15
- const [state, setState] = useState<ChatState>({
16
- messages: [],
17
- isThinking: false,
18
- streaming: '',
19
- streamingToolCalls: [],
20
- queuedMessage: null,
21
- });
22
- const processingRef = useRef(false);
23
- const queuedRef = useRef<string | null>(null);
24
- const abortRef = useRef(false);
25
-
26
- const processStream = async (input: string) => {
27
- abortRef.current = false;
28
- setState(prev => ({
29
- ...prev,
30
- messages: [...prev.messages, { role: 'user', content: input }],
31
- isThinking: true,
32
- streaming: '',
33
- streamingToolCalls: [],
34
- queuedMessage: null,
35
- }));
36
-
37
- app.session.addMessage('user', input);
38
-
39
- const stream = app.streamResponse();
40
- let fullContent = '';
41
- let currentToolCalls: ToolCallData[] = [];
42
- let lastUpdateTime = 0;
43
- const THROTTLE_MS = 50;
44
-
45
- try {
46
- for await (const chunk of stream) {
47
- // Check abort
48
- if (abortRef.current) {
49
- break;
50
- }
51
-
52
- if (chunk.includes('@@TOOL@@')) {
53
- const parts = chunk.split('\n');
54
- for (const part of parts) {
55
- if (part.startsWith('@@TOOL@@')) {
56
- try {
57
- const data = JSON.parse(part.slice(8));
58
- if (data.action === 'call') {
59
- currentToolCalls.push({ name: data.name, args: data.args || '' });
60
- setState(prev => ({
61
- ...prev,
62
- isThinking: false,
63
- streamingToolCalls: [...currentToolCalls],
64
- }));
65
- } else if (data.action === 'result') {
66
- const lastIdx = currentToolCalls.length - 1;
67
- if (lastIdx >= 0) {
68
- currentToolCalls[lastIdx] = {
69
- ...currentToolCalls[lastIdx],
70
- result: data.output || data.error || '',
71
- isError: !!data.error,
72
- };
73
- }
74
- setState(prev => ({
75
- ...prev,
76
- streamingToolCalls: [...currentToolCalls],
77
- isThinking: true,
78
- }));
79
- }
80
- } catch {
81
- fullContent += part;
82
- }
83
- } else if (part) {
84
- fullContent += part;
85
- }
86
- }
87
- } else {
88
- fullContent += chunk;
89
- }
90
-
91
- const now = Date.now();
92
- if (now - lastUpdateTime >= THROTTLE_MS) {
93
- lastUpdateTime = now;
94
- setState(prev => ({ ...prev, isThinking: false, streaming: fullContent }));
95
- }
96
- }
97
-
98
- // Save response (even if aborted, save what we have)
99
- if (fullContent.trim() || currentToolCalls.length > 0) {
100
- const suffix = abortRef.current ? '\n\n[已中断]' : '';
101
- app.session.addMessage('assistant', fullContent + suffix);
102
- }
103
-
104
- setState(prev => ({
105
- ...prev,
106
- messages: [
107
- ...prev.messages,
108
- ...(fullContent.trim() || currentToolCalls.length > 0
109
- ? [{
110
- role: 'assistant' as const,
111
- content: fullContent + (abortRef.current ? '\n\n[已中断]' : ''),
112
- toolCalls: currentToolCalls.length > 0 ? currentToolCalls : undefined,
113
- }]
114
- : []),
115
- ],
116
- streaming: '',
117
- streamingToolCalls: [],
118
- isThinking: false,
119
- }));
120
-
121
- app.session.truncate();
122
- } catch (error: any) {
123
- if (abortRef.current) {
124
- setState(prev => ({
125
- ...prev,
126
- messages: [
127
- ...prev.messages,
128
- ...(fullContent.trim()
129
- ? [{ role: 'assistant' as const, content: fullContent + '\n\n[已中断]' }]
130
- : [{ role: 'assistant' as const, content: '[已中断]' }]),
131
- ],
132
- streaming: '',
133
- streamingToolCalls: [],
134
- isThinking: false,
135
- }));
136
- } else {
137
- const msg = error.message || String(error);
138
- let errorMsg = `Error: ${msg}`;
139
-
140
- if (msg.includes('401') || msg.includes('403') || msg.includes('Unauthorized')) {
141
- errorMsg = `❌ API key invalid. Run \`gfcode init\` to reconfigure.`;
142
- } else if (msg.includes('429') || msg.includes('rate limit') || msg.includes('quota')) {
143
- errorMsg = `❌ Rate limit exceeded. Wait or run \`gfcode init\` to switch provider.`;
144
- } else if (msg.includes('ECONNREFUSED') || msg.includes('ENOTFOUND')) {
145
- errorMsg = `❌ Cannot connect. Check network or run \`gfcode init\`.`;
146
- } else if (msg.includes('abort') || msg.includes('AbortError')) {
147
- errorMsg = `[已中断]`;
148
- }
149
-
150
- setState(prev => ({
151
- ...prev,
152
- messages: [
153
- ...prev.messages,
154
- ...(fullContent.trim()
155
- ? [{ role: 'assistant' as const, content: fullContent }]
156
- : []),
157
- { role: 'assistant', content: errorMsg },
158
- ],
159
- streaming: '',
160
- streamingToolCalls: [],
161
- isThinking: false,
162
- }));
163
- }
164
- }
165
-
166
- // Process queued message
167
- if (!abortRef.current && queuedRef.current) {
168
- const next = queuedRef.current;
169
- queuedRef.current = null;
170
- await processStream(next);
171
- } else {
172
- processingRef.current = false;
173
- }
174
- };
175
-
176
- const sendMessage = useCallback((input: string) => {
177
- if (processingRef.current) {
178
- queuedRef.current = input;
179
- setState(prev => ({ ...prev, queuedMessage: input }));
180
- return;
181
- }
182
-
183
- processingRef.current = true;
184
- processStream(input);
185
- }, [app]);
186
-
187
- const cancel = useCallback(() => {
188
- abortRef.current = true;
189
- queuedRef.current = null;
190
- setState(prev => ({ ...prev, queuedMessage: null, isThinking: false }));
191
- }, []);
192
-
193
- return { ...state, sendMessage, cancel };
194
- }
1
+ import { useState, useCallback, useRef } from 'react';
2
+ import type { MessageData } from '../components/ChatMessage.js';
3
+ import type { ToolCallData } from '../components/ToolCall.js';
4
+ import type { App } from '../../app/index.js';
5
+ import { compressThinking, splitThinking, summarizeThinking } from '../../utils/thinking.js';
6
+ import type { StreamChunk, Usage } from '../../types.js';
7
+
8
+ interface ChatState {
9
+ messages: MessageData[];
10
+ isThinking: boolean;
11
+ streaming: string;
12
+ streamingToolCalls: ToolCallData[];
13
+ queuedMessage: string | null;
14
+ /**
15
+ * v3.0.0: Latest cache usage emitted by the provider, surfaced to the TUI
16
+ * Header. Null when no usage data was returned (e.g. provider does not
17
+ * support cache stats or streaming without stream_options.include_usage).
18
+ */
19
+ lastUsage: Usage | null;
20
+ }
21
+
22
+ /**
23
+ * Hook for managing chat state and the streaming response lifecycle.
24
+ *
25
+ * v3.0.0: streamResponse yields structured StreamChunks (text / tool_calls /
26
+ * thinking / usage). The previous @@TOOL@@ sentinel-string parsing is gone.
27
+ *
28
+ * Persisted message order matches the source chunks exactly:
29
+ * - text chunks → accumulated into fullContent (later compressed via
30
+ * compressThinking before persistence)
31
+ * - tool_calls chunks → results are summarized into a `[tool: name → result]`
32
+ * suffix on the assistant message (same belt-and-suspenders behavior as
33
+ * v2.2.6)
34
+ * - usage chunks → surfaced via state.lastUsage so the TUI Header can render
35
+ * cache hit-rate (consumed by app.tsx via /cache command in M3)
36
+ */
37
+ export function useChat(app: App) {
38
+ const [state, setState] = useState<ChatState>({
39
+ messages: [],
40
+ isThinking: false,
41
+ streaming: '',
42
+ streamingToolCalls: [],
43
+ queuedMessage: null,
44
+ lastUsage: null,
45
+ });
46
+ const processingRef = useRef(false);
47
+ const queuedRef = useRef<string | null>(null);
48
+ const abortRef = useRef(false);
49
+
50
+ const processStream = async (input: string) => {
51
+ abortRef.current = false;
52
+ setState(prev => ({
53
+ ...prev,
54
+ messages: [...prev.messages, { role: 'user', content: input }],
55
+ isThinking: true,
56
+ streaming: '',
57
+ streamingToolCalls: [],
58
+ queuedMessage: null,
59
+ }));
60
+
61
+ app.session.addMessage('user', input);
62
+
63
+ const stream = app.streamResponse();
64
+ let fullContent = '';
65
+ let currentToolCalls: ToolCallData[] = [];
66
+ // Latest usage from this round; surfaced via state.lastUsage.
67
+ let lastUsage: Usage | null = null;
68
+ let lastUpdateTime = 0;
69
+ const THROTTLE_MS = 50;
70
+
71
+ /**
72
+ * Flush streaming state to React. Throttled to ~20fps so the terminal
73
+ * doesn't flicker on long completions (combined with <Static> below the
74
+ * completion line stays put while the streaming line updates in place).
75
+ */
76
+ const flushStreaming = () => {
77
+ const now = Date.now();
78
+ if (now - lastUpdateTime < THROTTLE_MS) return;
79
+ lastUpdateTime = now;
80
+ setState(prev => ({ ...prev, isThinking: false, streaming: fullContent }));
81
+ };
82
+
83
+ try {
84
+ for await (const chunk of stream as AsyncIterable<StreamChunk>) {
85
+ // Check abort
86
+ if (abortRef.current) {
87
+ break;
88
+ }
89
+
90
+ switch (chunk.type) {
91
+ case 'text':
92
+ if (chunk.content) {
93
+ fullContent += chunk.content;
94
+ flushStreaming();
95
+ }
96
+ break;
97
+
98
+ case 'thinking':
99
+ // Reasoning text. We do not append it to fullContent (it is
100
+ // stripped from persistence in compressThinking), but we surface
101
+ // it via the streaming display when app.showThinking is on.
102
+ if (app.showThinking && chunk.content) {
103
+ fullContent += chunk.content;
104
+ flushStreaming();
105
+ }
106
+ break;
107
+
108
+ case 'tool_calls':
109
+ if (chunk.toolCalls && chunk.toolCalls.length > 0) {
110
+ // Replace rather than append — LLMService emits one combined
111
+ // tool_calls chunk per iteration. This is the dispatch plan.
112
+ currentToolCalls = chunk.toolCalls.map(tc => ({
113
+ name: tc.function.name,
114
+ args: tc.function.arguments,
115
+ // Results are appended by the *next* text chunk via the
116
+ // `[tool: name → result]` suffix we add on persistence. The
117
+ // streaming ToolCall panel below renders a "running" state
118
+ // until the next iteration's tool_calls chunk comes back
119
+ // with `result` (we look up by name).
120
+ result: undefined,
121
+ isError: false,
122
+ }));
123
+ setState(prev => ({
124
+ ...prev,
125
+ isThinking: false,
126
+ streamingToolCalls: [...currentToolCalls],
127
+ }));
128
+ }
129
+ break;
130
+
131
+ case 'usage':
132
+ lastUsage = chunk.usage;
133
+ setState(prev => ({ ...prev, lastUsage: chunk.usage }));
134
+ break;
135
+ }
136
+ }
137
+
138
+ // v2.2.4 (port from v2.1.0): DO NOT persist truncated assistant
139
+ // messages. The previous code literally wrote `'\n\n[已中断]'`
140
+ // as a suffix and persisted it — which is what created the
141
+ // hallucination loop where the next turn's LLM echoed the
142
+ // marker back. The fix is two-pronged:
143
+ // 1. Never persist when the stream was aborted (here).
144
+ // 2. SessionManager.addMessageSafe drops messages that match
145
+ // the pollution filter as a belt-and-suspenders check
146
+ // for cases where we somehow persist a polluted message.
147
+ const wasAborted = abortRef.current;
148
+ const shouldPersist = !wasAborted &&
149
+ (fullContent.trim() || currentToolCalls.length > 0);
150
+
151
+ if (shouldPersist) {
152
+ // v2.2.5: strip blocks from the persisted message
153
+ // when compression is enabled. Same rationale as in
154
+ // cmd/index.tsx — keeps history compact, avoids re-feeding
155
+ // reasoning into the next turn's context window.
156
+ const toPersist = compressThinking(fullContent, app.showThinking);
157
+ app.session.addMessageSafe('assistant', toPersist);
158
+ }
159
+
160
+ // v2.2.5: build a displayable version. When thinking is hidden
161
+ // we still want the user to see a one-line indicator of how
162
+ // much reasoning the model did, plus the conclusion.
163
+ const split = splitThinking(fullContent);
164
+ const displayContent = app.showThinking
165
+ ? fullContent
166
+ : (split.thinking
167
+ ? `${summarizeThinking(split)}\n${split.conclusion}`
168
+ : fullContent);
169
+
170
+ // v2.2.6 (tool-result belt-and-suspenders): if any tool call
171
+ // returned text, also append a compact "[tool: name → result]"
172
+ // summary to the persisted/displayed content. This guarantees
173
+ // the user sees the tool output regardless of whether the Ink
174
+ // <ToolCall/> component renders it correctly. Past sessions
175
+ // have had cases where the streaming ToolCall rendering failed
176
+ // silently (e.g. result was empty string, wrap=truncate cut
177
+ // long output off-screen) and the user had no idea what the
178
+ // tool actually returned.
179
+ //
180
+ // v3.0.0: since LLMService now folds tool results into
181
+ // currentMessages without surfacing them as separate chunks, we no
182
+ // longer have a `result` field on each ToolCallData here. The
183
+ // result summary is built by reading back from
184
+ // app.session.getMessages() — the latest `tool` role message per
185
+ // tool call id. To keep this lightweight we fall back to a brief
186
+ // "[tool: name → see message]" suffix when the result is not
187
+ // available in our local streaming state.
188
+ const toolSummary = currentToolCalls.length > 0
189
+ ? '\n\n' + currentToolCalls.map((tc) => {
190
+ const r = tc.result !== undefined ? tc.result : '(see tool result above)';
191
+ const short = r.length > 200 ? r.slice(0, 197) + '...' : r;
192
+ return `[tool: ${tc.name} → ${short}]`;
193
+ }).join('\n')
194
+ : '';
195
+
196
+ setState(prev => ({
197
+ ...prev,
198
+ messages: [
199
+ ...prev.messages,
200
+ ...(shouldPersist
201
+ ? [{
202
+ role: 'assistant' as const,
203
+ content: displayContent + toolSummary,
204
+ toolCalls: currentToolCalls.length > 0 ? currentToolCalls : undefined,
205
+ }]
206
+ : []),
207
+ ],
208
+ streaming: '',
209
+ streamingToolCalls: [],
210
+ isThinking: false,
211
+ lastUsage,
212
+ }));
213
+
214
+ app.session.truncate();
215
+ } catch (error: any) {
216
+ if (abortRef.current) {
217
+ setState(prev => ({
218
+ ...prev,
219
+ messages: [
220
+ ...prev.messages,
221
+ ...(fullContent.trim()
222
+ ? [{ role: 'assistant' as const, content: fullContent + '\n\n[已中断]' }]
223
+ : [{ role: 'assistant' as const, content: '[已中断]' }]),
224
+ ],
225
+ streaming: '',
226
+ streamingToolCalls: [],
227
+ isThinking: false,
228
+ }));
229
+ } else {
230
+ const msg = error.message || String(error);
231
+ let errorMsg = `Error: ${msg}`;
232
+
233
+ if (msg.includes('401') || msg.includes('403') || msg.includes('Unauthorized')) {
234
+ errorMsg = `❌ API key invalid. Run \`gfcode init\` to reconfigure.`;
235
+ } else if (msg.includes('429') || msg.includes('rate limit') || msg.includes('quota')) {
236
+ errorMsg = `❌ Rate limit exceeded. Wait or run \`gfcode init\` to switch provider.`;
237
+ } else if (msg.includes('ECONNREFUSED') || msg.includes('ENOTFOUND')) {
238
+ errorMsg = `❌ Cannot connect. Check network or run \`gfcode init\`.`;
239
+ } else if (msg.includes('abort') || msg.includes('AbortError')) {
240
+ errorMsg = `[已中断]`;
241
+ }
242
+
243
+ setState(prev => ({
244
+ ...prev,
245
+ messages: [
246
+ ...prev.messages,
247
+ ...(fullContent.trim()
248
+ ? [{ role: 'assistant' as const, content: fullContent }]
249
+ : []),
250
+ { role: 'assistant', content: errorMsg },
251
+ ],
252
+ streaming: '',
253
+ streamingToolCalls: [],
254
+ isThinking: false,
255
+ }));
256
+ }
257
+ }
258
+
259
+ // Process queued message
260
+ if (!abortRef.current && queuedRef.current) {
261
+ const next = queuedRef.current;
262
+ queuedRef.current = null;
263
+ await processStream(next);
264
+ } else {
265
+ processingRef.current = false;
266
+ }
267
+ };
268
+
269
+ const sendMessage = useCallback((input: string) => {
270
+ if (processingRef.current) {
271
+ queuedRef.current = input;
272
+ setState(prev => ({ ...prev, queuedMessage: input }));
273
+ return;
274
+ }
275
+
276
+ processingRef.current = true;
277
+ processStream(input);
278
+ }, [app]);
279
+
280
+ const cancel = useCallback(() => {
281
+ abortRef.current = true;
282
+ queuedRef.current = null;
283
+ setState(prev => ({ ...prev, queuedMessage: null, isThinking: false }));
284
+ }, []);
285
+
286
+ return { ...state, sendMessage, cancel };
287
+ }