thatgfsj-code 3.0.4 → 3.0.6
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/README.md +64 -21
- package/dist/app/index.d.ts +93 -22
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +232 -40
- package/dist/app/index.js.map +1 -1
- package/dist/cmd/index.d.ts +12 -20
- package/dist/cmd/index.d.ts.map +1 -1
- package/dist/cmd/index.js +192 -146
- package/dist/cmd/index.js.map +1 -1
- package/dist/config/index.d.ts +4 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +5 -0
- package/dist/config/index.js.map +1 -1
- package/dist/config/types.d.ts +5 -0
- package/dist/config/types.d.ts.map +1 -1
- package/dist/llm/anthropic.d.ts +1 -1
- package/dist/llm/anthropic.d.ts.map +1 -1
- package/dist/llm/anthropic.js +116 -84
- package/dist/llm/anthropic.js.map +1 -1
- package/dist/llm/gemini.d.ts +27 -3
- package/dist/llm/gemini.d.ts.map +1 -1
- package/dist/llm/gemini.js +196 -73
- package/dist/llm/gemini.js.map +1 -1
- package/dist/llm/index.d.ts +20 -1
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +66 -6
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/openai.d.ts +5 -1
- package/dist/llm/openai.d.ts.map +1 -1
- package/dist/llm/openai.js +96 -56
- package/dist/llm/openai.js.map +1 -1
- package/dist/mcp/client.d.ts +84 -34
- package/dist/mcp/client.d.ts.map +1 -1
- package/dist/mcp/client.js +291 -126
- package/dist/mcp/client.js.map +1 -1
- package/dist/session/compactor.d.ts +27 -7
- package/dist/session/compactor.d.ts.map +1 -1
- package/dist/session/compactor.js +78 -23
- package/dist/session/compactor.js.map +1 -1
- package/dist/session/index.d.ts +96 -33
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +278 -60
- package/dist/session/index.js.map +1 -1
- package/dist/tools/file.d.ts +10 -2
- package/dist/tools/file.d.ts.map +1 -1
- package/dist/tools/file.js +54 -5
- package/dist/tools/file.js.map +1 -1
- package/dist/tools/git.d.ts +16 -2
- package/dist/tools/git.d.ts.map +1 -1
- package/dist/tools/git.js +61 -18
- package/dist/tools/git.js.map +1 -1
- package/dist/tools/nwt.d.ts.map +1 -1
- package/dist/tools/nwt.js +8 -1
- package/dist/tools/nwt.js.map +1 -1
- package/dist/tools/search.d.ts +8 -1
- package/dist/tools/search.d.ts.map +1 -1
- package/dist/tools/search.js +105 -34
- package/dist/tools/search.js.map +1 -1
- package/dist/tools/shell.d.ts +0 -4
- package/dist/tools/shell.d.ts.map +1 -1
- package/dist/tools/shell.js +11 -26
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/types.d.ts +7 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/types.js.map +1 -1
- package/dist/tui/app.d.ts.map +1 -1
- package/dist/tui/app.js +90 -18
- package/dist/tui/app.js.map +1 -1
- package/dist/tui/components/ChatMessage.d.ts.map +1 -1
- package/dist/tui/components/ChatMessage.js +9 -2
- package/dist/tui/components/ChatMessage.js.map +1 -1
- package/dist/tui/components/ConfirmPrompt.d.ts +21 -0
- package/dist/tui/components/ConfirmPrompt.d.ts.map +1 -0
- package/dist/tui/components/ConfirmPrompt.js +45 -0
- package/dist/tui/components/ConfirmPrompt.js.map +1 -0
- package/dist/tui/components/Header.d.ts +10 -5
- package/dist/tui/components/Header.d.ts.map +1 -1
- package/dist/tui/components/Header.js +13 -5
- package/dist/tui/components/Header.js.map +1 -1
- package/dist/tui/components/StatusBar.d.ts +7 -0
- package/dist/tui/components/StatusBar.d.ts.map +1 -1
- package/dist/tui/components/StatusBar.js +8 -3
- package/dist/tui/components/StatusBar.js.map +1 -1
- package/dist/tui/components/Thinking.d.ts +5 -0
- package/dist/tui/components/Thinking.d.ts.map +1 -1
- package/dist/tui/components/Thinking.js +24 -2
- package/dist/tui/components/Thinking.js.map +1 -1
- package/dist/tui/components/ToolCall.d.ts +6 -1
- package/dist/tui/components/ToolCall.d.ts.map +1 -1
- package/dist/tui/components/ToolCall.js +50 -22
- package/dist/tui/components/ToolCall.js.map +1 -1
- package/dist/tui/components/UserInput.d.ts +5 -0
- package/dist/tui/components/UserInput.d.ts.map +1 -1
- package/dist/tui/components/UserInput.js +9 -13
- package/dist/tui/components/UserInput.js.map +1 -1
- package/dist/tui/hooks/useChat.d.ts +1 -0
- package/dist/tui/hooks/useChat.d.ts.map +1 -1
- package/dist/tui/hooks/useChat.js +36 -16
- package/dist/tui/hooks/useChat.js.map +1 -1
- package/dist/tui/hooks/useCommands.d.ts +7 -1
- package/dist/tui/hooks/useCommands.d.ts.map +1 -1
- package/dist/tui/hooks/useCommands.js +77 -41
- package/dist/tui/hooks/useCommands.js.map +1 -1
- package/dist/tui/theme.d.ts +34 -0
- package/dist/tui/theme.d.ts.map +1 -0
- package/dist/tui/theme.js +34 -0
- package/dist/tui/theme.js.map +1 -0
- package/dist/tui/welcome.d.ts.map +1 -1
- package/dist/tui/welcome.js +9 -7
- package/dist/tui/welcome.js.map +1 -1
- package/dist/types.d.ts +16 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/diff.d.ts +5 -0
- package/dist/utils/diff.d.ts.map +1 -1
- package/dist/utils/diff.js +15 -0
- package/dist/utils/diff.js.map +1 -1
- package/dist/version.d.ts +12 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +29 -0
- package/dist/version.js.map +1 -0
- package/package.json +19 -7
- package/CHANGELOG.md +0 -192
- package/DEVELOPMENT.md +0 -286
- package/ROADMAP.md +0 -91
- package/docs/API_KEY_GUIDE.md +0 -236
- package/docs/FAQ.md +0 -182
- package/install.bat +0 -63
- package/install.ps1 +0 -238
- package/install.sh +0 -113
- package/src/app/index.ts +0 -197
- package/src/cache/fingerprint.ts +0 -101
- package/src/cache/index.ts +0 -21
- package/src/cache/smartModel.ts +0 -133
- package/src/cache/stats.ts +0 -199
- package/src/cache/volatile.ts +0 -47
- package/src/cmd/index.tsx +0 -288
- package/src/config/index.ts +0 -156
- package/src/config/providers.ts +0 -234
- package/src/config/types.ts +0 -71
- package/src/hooks/index.ts +0 -111
- package/src/llm/anthropic.ts +0 -413
- package/src/llm/gemini.ts +0 -169
- package/src/llm/index.ts +0 -303
- package/src/llm/openai.ts +0 -243
- package/src/llm/provider.ts +0 -71
- package/src/mcp/client.ts +0 -330
- package/src/prompts/index.ts +0 -260
- package/src/session/compactor.ts +0 -103
- package/src/session/index.ts +0 -181
- package/src/session/message.ts +0 -42
- package/src/skills/brainstorming.ts +0 -43
- package/src/skills/code-review.ts +0 -45
- package/src/skills/executing-plans.ts +0 -27
- package/src/skills/frontend-design.ts +0 -35
- package/src/skills/git-workflow.ts +0 -36
- package/src/skills/improve-architecture.ts +0 -38
- package/src/skills/index.ts +0 -136
- package/src/skills/neuroweave.ts +0 -47
- package/src/skills/playwright.ts +0 -72
- package/src/skills/prototype.ts +0 -30
- package/src/skills/subagent.ts +0 -28
- package/src/skills/supabase.ts +0 -44
- package/src/skills/systematic-debugging.ts +0 -44
- package/src/skills/tdd.ts +0 -39
- package/src/skills/triage.ts +0 -35
- package/src/skills/verification.ts +0 -31
- package/src/skills/writing-plans.ts +0 -42
- package/src/tools/file.ts +0 -143
- package/src/tools/git.ts +0 -132
- package/src/tools/index.ts +0 -111
- package/src/tools/nwt.ts +0 -598
- package/src/tools/search.ts +0 -263
- package/src/tools/shell.ts +0 -136
- package/src/tools/types.ts +0 -122
- package/src/tui/app.tsx +0 -159
- package/src/tui/components/ChatList.tsx +0 -41
- package/src/tui/components/ChatMessage.tsx +0 -54
- package/src/tui/components/Header.tsx +0 -66
- package/src/tui/components/InitWizard.tsx +0 -247
- package/src/tui/components/Markdown.tsx +0 -35
- package/src/tui/components/ModelSelector.tsx +0 -107
- package/src/tui/components/StatusBar.tsx +0 -30
- package/src/tui/components/Thinking.tsx +0 -17
- package/src/tui/components/ToolCall.tsx +0 -102
- package/src/tui/components/UserInput.tsx +0 -151
- package/src/tui/hooks/useChat.ts +0 -287
- package/src/tui/hooks/useCommands.ts +0 -271
- package/src/tui/index.ts +0 -6
- package/src/tui/welcome.ts +0 -177
- package/src/types.ts +0 -104
- package/src/utils/diff.ts +0 -71
- package/src/utils/project.ts +0 -99
- package/src/utils/stableStringify.ts +0 -47
- package/src/utils/thinking.ts +0 -119
- package/tests/cache/fingerprint.test.ts +0 -75
- package/tests/cache/providerCatalog.test.ts +0 -31
- package/tests/cache/smartModel.test.ts +0 -100
- package/tests/cache/stableStringify.test.ts +0 -43
- package/tests/cache/stats.test.ts +0 -146
- package/tests/cache/volatile.test.ts +0 -75
- package/tests/smoke-pollution.mjs +0 -78
- package/tests/smoke-thinking.mjs +0 -110
- package/tests/smoke-tool-stream.mjs +0 -69
- package/tests/smoke-tool.mjs +0 -117
- package/tsconfig.json +0 -21
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
/** @jsxImportSource react */
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import { Box, Text } from 'ink';
|
|
4
|
-
|
|
5
|
-
interface ToolCallData {
|
|
6
|
-
name: string;
|
|
7
|
-
args: string;
|
|
8
|
-
result?: string;
|
|
9
|
-
isError?: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface Props {
|
|
13
|
-
tool: ToolCallData;
|
|
14
|
-
width?: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function formatToolName(name: string, args: string): string {
|
|
18
|
-
try {
|
|
19
|
-
const obj = JSON.parse(args);
|
|
20
|
-
switch (name) {
|
|
21
|
-
case 'file': {
|
|
22
|
-
const action = obj.action || '';
|
|
23
|
-
const path = (obj.path || '').replace(/.*[/\\]/, '');
|
|
24
|
-
return `${action} ${path || obj.path || ''}`;
|
|
25
|
-
}
|
|
26
|
-
case 'shell': return obj.command?.slice(0, 60) || '';
|
|
27
|
-
case 'git': return obj.command || '';
|
|
28
|
-
case 'search': return obj.query || obj.pattern || '';
|
|
29
|
-
case 'nwt': return obj.action || '';
|
|
30
|
-
default: return Object.keys(obj).slice(0, 2).join(', ');
|
|
31
|
-
}
|
|
32
|
-
} catch {
|
|
33
|
-
return '';
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function truncateOutput(output: string, maxLines = 8): { text: string; truncated: boolean } {
|
|
38
|
-
const lines = output.split('\n');
|
|
39
|
-
if (lines.length <= maxLines) return { text: output, truncated: false };
|
|
40
|
-
return { text: lines.slice(0, maxLines).join('\n'), truncated: true };
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function ToolCall({ tool, width }: Props) {
|
|
44
|
-
const label = formatToolName(tool.name, tool.args);
|
|
45
|
-
const result = tool.result !== undefined ? truncateOutput(tool.result) : null;
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
<Box flexDirection="column" marginBottom={0} paddingLeft={1}>
|
|
49
|
-
{/* Tool header */}
|
|
50
|
-
<Box>
|
|
51
|
-
<Text color="#06B6D4" bold>⚙ </Text>
|
|
52
|
-
<Text color="#06B6D4">{tool.name}</Text>
|
|
53
|
-
{label && <Text color="#64748B"> {label}</Text>}
|
|
54
|
-
</Box>
|
|
55
|
-
|
|
56
|
-
{/* Result — v2.2.6 fix:
|
|
57
|
-
- Use `!== undefined` (not truthy) so empty string results
|
|
58
|
-
still render (truthy check dropped them, making it look
|
|
59
|
-
like the tool result was missing).
|
|
60
|
-
- Switch wrap from "truncate" to "wrap" so long lines don't
|
|
61
|
-
silently disappear off-screen.
|
|
62
|
-
- Always render the Box even when result is empty (just
|
|
63
|
-
with a "(no output)" marker), so the visual frame stays
|
|
64
|
-
consistent.
|
|
65
|
-
- If truncated, show explicit "(+N more lines)" indicator
|
|
66
|
-
so users know the result was clipped. */}
|
|
67
|
-
{result !== null && (
|
|
68
|
-
<Box paddingLeft={2} flexDirection="column">
|
|
69
|
-
{result.text.length === 0 ? (
|
|
70
|
-
<Text color="#94A3B8" dimColor>(no output)</Text>
|
|
71
|
-
) : (
|
|
72
|
-
<>
|
|
73
|
-
{result.text.split('\n').map((line, i) => (
|
|
74
|
-
<Text
|
|
75
|
-
key={i}
|
|
76
|
-
color={tool.isError ? '#EF4444' : '#64748B'}
|
|
77
|
-
wrap="wrap"
|
|
78
|
-
>
|
|
79
|
-
{line || ' '}
|
|
80
|
-
</Text>
|
|
81
|
-
))}
|
|
82
|
-
{result.truncated && (
|
|
83
|
-
<Text color="#94A3B8" dimColor>
|
|
84
|
-
{' '}(+{tool.result!.split('\n').length - 8} more lines)
|
|
85
|
-
</Text>
|
|
86
|
-
)}
|
|
87
|
-
</>
|
|
88
|
-
)}
|
|
89
|
-
</Box>
|
|
90
|
-
)}
|
|
91
|
-
{/* v2.2.6: if result is undefined (still running), show a
|
|
92
|
-
pending indicator so the user knows the tool is in flight. */}
|
|
93
|
-
{result === null && tool.result === undefined && (
|
|
94
|
-
<Box paddingLeft={2}>
|
|
95
|
-
<Text color="#94A3B8" dimColor> ⏳ running...</Text>
|
|
96
|
-
</Box>
|
|
97
|
-
)}
|
|
98
|
-
</Box>
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export type { ToolCallData };
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
/** @jsxImportSource react */
|
|
2
|
-
import React, { useState } from 'react';
|
|
3
|
-
import { Box, Text, useInput, useApp, useFocus } from 'ink';
|
|
4
|
-
import { COMMAND_LIST } from '../hooks/useCommands.js';
|
|
5
|
-
|
|
6
|
-
interface Props {
|
|
7
|
-
onSubmit: (input: string) => void;
|
|
8
|
-
onCancel: () => void;
|
|
9
|
-
disabled?: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function UserInput({ onSubmit, onCancel, disabled }: Props) {
|
|
13
|
-
const [value, setValue] = useState('');
|
|
14
|
-
const [history, setHistory] = useState<string[]>([]);
|
|
15
|
-
const [historyIdx, setHistoryIdx] = useState(-1);
|
|
16
|
-
const [selectedCmd, setSelectedCmd] = useState(0);
|
|
17
|
-
const { exit } = useApp();
|
|
18
|
-
useFocus({ autoFocus: true });
|
|
19
|
-
|
|
20
|
-
const showCommands = value.startsWith('/') && value.length > 0 && !value.includes(' ');
|
|
21
|
-
const filteredCommands = showCommands
|
|
22
|
-
? COMMAND_LIST.filter(c => c.name.startsWith(value))
|
|
23
|
-
: [];
|
|
24
|
-
|
|
25
|
-
useInput((input, key) => {
|
|
26
|
-
// ESC - cancel/clear (always works, even when disabled)
|
|
27
|
-
if (key.escape) {
|
|
28
|
-
setValue('');
|
|
29
|
-
setSelectedCmd(0);
|
|
30
|
-
setHistoryIdx(-1);
|
|
31
|
-
onCancel();
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (disabled) return;
|
|
36
|
-
|
|
37
|
-
// Command selector navigation
|
|
38
|
-
if (showCommands && filteredCommands.length > 0) {
|
|
39
|
-
if (key.upArrow) {
|
|
40
|
-
setSelectedCmd(prev => Math.max(0, prev - 1));
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
if (key.downArrow) {
|
|
44
|
-
setSelectedCmd(prev => Math.min(filteredCommands.length - 1, prev + 1));
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
if (key.tab || key.return) {
|
|
48
|
-
const selected = filteredCommands[selectedCmd] || filteredCommands[0];
|
|
49
|
-
// v3.0.0 bug fix: previously we set `value = selected.name + ' '` and
|
|
50
|
-
// returned, which forced the user to press Enter a SECOND time to
|
|
51
|
-
// actually execute the command. For single-word commands like
|
|
52
|
-
// /模型 /新建 /压缩 /帮助 /技能 etc. that meant: "press Enter twice
|
|
53
|
-
// to switch model", which is confusing and a reproduce-and-exit
|
|
54
|
-
// vector if the user panics and Ctrl+C's in the middle.
|
|
55
|
-
//
|
|
56
|
-
// Fix: when Enter is pressed on a command-list item, EXECUTE the
|
|
57
|
-
// command directly. If the user wants to append args (e.g. /模型
|
|
58
|
-
// <name>), they can type them after the slash. Tab still writes
|
|
59
|
-
// the name into the input so the user can append text.
|
|
60
|
-
if (key.return) {
|
|
61
|
-
setSelectedCmd(0);
|
|
62
|
-
setValue('');
|
|
63
|
-
setHistory(prev => [...prev, selected.name]);
|
|
64
|
-
setHistoryIdx(-1);
|
|
65
|
-
onSubmit(selected.name);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
// Tab: write the name into the input so the user can append args.
|
|
69
|
-
setValue(selected.name + ' ');
|
|
70
|
-
setSelectedCmd(0);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (key.return) {
|
|
76
|
-
const trimmed = value.trim();
|
|
77
|
-
if (trimmed) {
|
|
78
|
-
if (trimmed === 'exit' || trimmed === 'quit') {
|
|
79
|
-
exit();
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
setHistory(prev => [...prev, trimmed]);
|
|
83
|
-
setHistoryIdx(-1);
|
|
84
|
-
onSubmit(trimmed);
|
|
85
|
-
setValue('');
|
|
86
|
-
setSelectedCmd(0);
|
|
87
|
-
}
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (key.upArrow && history.length > 0) {
|
|
92
|
-
const newIdx = historyIdx === -1 ? history.length - 1 : Math.max(0, historyIdx - 1);
|
|
93
|
-
setHistoryIdx(newIdx);
|
|
94
|
-
setValue(history[newIdx]);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (key.downArrow && historyIdx >= 0) {
|
|
99
|
-
const newIdx = historyIdx + 1;
|
|
100
|
-
if (newIdx >= history.length) {
|
|
101
|
-
setHistoryIdx(-1);
|
|
102
|
-
setValue('');
|
|
103
|
-
} else {
|
|
104
|
-
setHistoryIdx(newIdx);
|
|
105
|
-
setValue(history[newIdx]);
|
|
106
|
-
}
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (key.backspace || key.delete) {
|
|
111
|
-
setValue(v => v.slice(0, -1));
|
|
112
|
-
setSelectedCmd(0);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (key.ctrl && input === 'c') {
|
|
117
|
-
exit();
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (input && !key.ctrl && !key.meta) {
|
|
122
|
-
setValue(v => v + input);
|
|
123
|
-
setSelectedCmd(0);
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
return (
|
|
128
|
-
<Box flexDirection="column">
|
|
129
|
-
{showCommands && filteredCommands.length > 0 && (
|
|
130
|
-
<Box flexDirection="column" paddingLeft={2} marginBottom={0}>
|
|
131
|
-
{filteredCommands.map((cmd, i) => (
|
|
132
|
-
<Box key={cmd.name}>
|
|
133
|
-
<Text color={i === selectedCmd ? '#06B6D4' : '#64748B'}>
|
|
134
|
-
{i === selectedCmd ? '▸ ' : ' '}
|
|
135
|
-
</Text>
|
|
136
|
-
<Text color={i === selectedCmd ? '#06B6D4' : '#94A3B8'} bold={i === selectedCmd}>
|
|
137
|
-
{cmd.name}
|
|
138
|
-
</Text>
|
|
139
|
-
<Text color="#64748B"> {cmd.desc}</Text>
|
|
140
|
-
</Box>
|
|
141
|
-
))}
|
|
142
|
-
</Box>
|
|
143
|
-
)}
|
|
144
|
-
<Box paddingY={0}>
|
|
145
|
-
<Text color="#06B6D4" bold>{disabled ? ' ' : '❯ '}</Text>
|
|
146
|
-
<Text>{value}</Text>
|
|
147
|
-
{!disabled && <Text color="#06B6D4">█</Text>}
|
|
148
|
-
</Box>
|
|
149
|
-
</Box>
|
|
150
|
-
);
|
|
151
|
-
}
|
package/src/tui/hooks/useChat.ts
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
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
|
-
}
|