deeper-cli 1.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/README.md +254 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +12067 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/index.d.ts +415 -0
- package/dist/index.js +1599 -0
- package/dist/index.js.map +1 -0
- package/docs/superpowers/plans/2026-05-14-deepercode-implementation.md +24 -0
- package/docs/superpowers/plans/2026-05-14-deepercode-plan.md +1248 -0
- package/docs/superpowers/specs/2026-05-14-deepercode-design.md +560 -0
- package/package.json +60 -0
- package/src/cli/bootstrap.ts +69 -0
- package/src/cli/chat-repl.ts +932 -0
- package/src/cli/commands/chat.ts +39 -0
- package/src/cli/commands/chat.tsx +39 -0
- package/src/cli/commands/config.ts +133 -0
- package/src/cli/commands/mcp.ts +172 -0
- package/src/cli/commands/run.ts +147 -0
- package/src/cli/commands/skill.ts +152 -0
- package/src/cli/index.ts +184 -0
- package/src/core/bugscan.ts +145 -0
- package/src/core/config.ts +285 -0
- package/src/core/constants.ts +49 -0
- package/src/core/eventbus.ts +202 -0
- package/src/core/logger.ts +109 -0
- package/src/core/storage.ts +96 -0
- package/src/index.ts +26 -0
- package/src/mcp/ConfigLoader.ts +74 -0
- package/src/mcp/MCPClient.ts +326 -0
- package/src/mcp/ResourceAdapter.ts +58 -0
- package/src/mcp/SSETransport.ts +133 -0
- package/src/mcp/StdioTransport.ts +116 -0
- package/src/mcp/ToolAdapter.ts +71 -0
- package/src/mcp/types.ts +58 -0
- package/src/memory/xmemory.ts +275 -0
- package/src/model/DeepSeekClient.ts +292 -0
- package/src/model/MessageBuilder.ts +155 -0
- package/src/model/RetryManager.ts +82 -0
- package/src/model/StreamHandler.ts +158 -0
- package/src/model/types.ts +86 -0
- package/src/skills/SkillCreator.ts +153 -0
- package/src/skills/SkillEngine.ts +158 -0
- package/src/skills/SkillExecutor.ts +107 -0
- package/src/skills/SkillLoader.ts +182 -0
- package/src/skills/SkillRegistry.ts +73 -0
- package/src/skills/SkillTrigger.ts +82 -0
- package/src/skills/types.ts +28 -0
- package/src/tools/ToolExecutor.ts +103 -0
- package/src/tools/ToolRegistry.ts +71 -0
- package/src/tools/ToolValidator.ts +103 -0
- package/src/tools/builtin/ai/context_summarize.ts +76 -0
- package/src/tools/builtin/ai/memory_store.ts +86 -0
- package/src/tools/builtin/ai/prompt_template.ts +71 -0
- package/src/tools/builtin/ai/skill_create.ts +53 -0
- package/src/tools/builtin/ai/subagent.ts +39 -0
- package/src/tools/builtin/ai/todo_manager.ts +157 -0
- package/src/tools/builtin/ai/token_count.ts +196 -0
- package/src/tools/builtin/ai/tool_create.ts +52 -0
- package/src/tools/builtin/code/analyze_deps.ts +72 -0
- package/src/tools/builtin/code/bug_scan.ts +80 -0
- package/src/tools/builtin/code/code_metrics.ts +111 -0
- package/src/tools/builtin/code/extract_function.ts +86 -0
- package/src/tools/builtin/code/format_code.ts +57 -0
- package/src/tools/builtin/code/generate_code.ts +75 -0
- package/src/tools/builtin/code/import_organizer.ts +82 -0
- package/src/tools/builtin/code/lint_code.ts +48 -0
- package/src/tools/builtin/code/parse_ast.ts +86 -0
- package/src/tools/builtin/code/refactor_code.ts +63 -0
- package/src/tools/builtin/code/type_check.ts +48 -0
- package/src/tools/builtin/data/chart_generate.ts +62 -0
- package/src/tools/builtin/data/csv_parse.ts +56 -0
- package/src/tools/builtin/data/data_diff.ts +79 -0
- package/src/tools/builtin/data/data_transform.ts +74 -0
- package/src/tools/builtin/data/data_validate.ts +75 -0
- package/src/tools/builtin/data/json_parse.ts +71 -0
- package/src/tools/builtin/data/template_render.ts +58 -0
- package/src/tools/builtin/data/toml_parse.ts +42 -0
- package/src/tools/builtin/data/xml_parse.ts +79 -0
- package/src/tools/builtin/data/yaml_parse.ts +42 -0
- package/src/tools/builtin/database/db_backup.ts +53 -0
- package/src/tools/builtin/database/db_restore.ts +51 -0
- package/src/tools/builtin/database/db_schema.ts +66 -0
- package/src/tools/builtin/database/nosql_query.ts +50 -0
- package/src/tools/builtin/database/orm_generate.ts +66 -0
- package/src/tools/builtin/database/redis_command.ts +46 -0
- package/src/tools/builtin/database/sql_migrate.ts +55 -0
- package/src/tools/builtin/database/sql_query.ts +60 -0
- package/src/tools/builtin/filesystem/batch_read.ts +56 -0
- package/src/tools/builtin/filesystem/batch_write.ts +67 -0
- package/src/tools/builtin/filesystem/copy_file.ts +36 -0
- package/src/tools/builtin/filesystem/create_dir.ts +30 -0
- package/src/tools/builtin/filesystem/delete_file.ts +30 -0
- package/src/tools/builtin/filesystem/diff_files.ts +47 -0
- package/src/tools/builtin/filesystem/edit_file.ts +47 -0
- package/src/tools/builtin/filesystem/file_info.ts +52 -0
- package/src/tools/builtin/filesystem/glob_find.ts +44 -0
- package/src/tools/builtin/filesystem/list_dir.ts +51 -0
- package/src/tools/builtin/filesystem/merge_files.ts +44 -0
- package/src/tools/builtin/filesystem/move_file.ts +37 -0
- package/src/tools/builtin/filesystem/read_file.ts +55 -0
- package/src/tools/builtin/filesystem/watch_file.ts +33 -0
- package/src/tools/builtin/filesystem/write_file.ts +45 -0
- package/src/tools/builtin/index.ts +244 -0
- package/src/tools/builtin/network/api_call.ts +79 -0
- package/src/tools/builtin/network/browser_action.ts +54 -0
- package/src/tools/builtin/network/check_url.ts +59 -0
- package/src/tools/builtin/network/download_file.ts +64 -0
- package/src/tools/builtin/network/graphql_query.ts +46 -0
- package/src/tools/builtin/network/http_request.ts +61 -0
- package/src/tools/builtin/network/parse_html.ts +101 -0
- package/src/tools/builtin/network/proxy_request.ts +53 -0
- package/src/tools/builtin/network/screenshot_page.ts +58 -0
- package/src/tools/builtin/network/web_fetch.ts +70 -0
- package/src/tools/builtin/network/web_search.ts +128 -0
- package/src/tools/builtin/network/websocket_connect.ts +70 -0
- package/src/tools/builtin/project/build_project.ts +68 -0
- package/src/tools/builtin/project/config_manage.ts +99 -0
- package/src/tools/builtin/project/coverage_report.ts +59 -0
- package/src/tools/builtin/project/docker_manage.ts +90 -0
- package/src/tools/builtin/project/env_manage.ts +88 -0
- package/src/tools/builtin/project/npm_manage.ts +71 -0
- package/src/tools/builtin/project/project_init.ts +59 -0
- package/src/tools/builtin/project/run_test.ts +74 -0
- package/src/tools/builtin/search/codebase_search.ts +76 -0
- package/src/tools/builtin/search/find_definition.ts +84 -0
- package/src/tools/builtin/search/find_references.ts +75 -0
- package/src/tools/builtin/search/fuzzy_find.ts +75 -0
- package/src/tools/builtin/search/grep_search.ts +90 -0
- package/src/tools/builtin/search/regex_find.ts +91 -0
- package/src/tools/builtin/search/search_docs.ts +51 -0
- package/src/tools/builtin/search/search_package.ts +50 -0
- package/src/tools/builtin/search/symbol_search.ts +82 -0
- package/src/tools/builtin/search/text_search.ts +63 -0
- package/src/tools/builtin/security/decrypt_file.ts +54 -0
- package/src/tools/builtin/security/encrypt_file.ts +52 -0
- package/src/tools/builtin/security/hash_generate.ts +48 -0
- package/src/tools/builtin/security/jwt_decode.ts +53 -0
- package/src/tools/builtin/security/secret_scan.ts +82 -0
- package/src/tools/builtin/security/vulnerability_check.ts +71 -0
- package/src/tools/builtin/shell/background_terminal.ts +38 -0
- package/src/tools/builtin/shell/check_status.ts +48 -0
- package/src/tools/builtin/shell/interactive_terminal.ts +31 -0
- package/src/tools/builtin/shell/kill_terminal.ts +29 -0
- package/src/tools/builtin/shell/list_terminals.ts +61 -0
- package/src/tools/builtin/shell/pipe_commands.ts +55 -0
- package/src/tools/builtin/shell/process-pool.ts +150 -0
- package/src/tools/builtin/shell/run_async.ts +73 -0
- package/src/tools/builtin/shell/run_command.ts +60 -0
- package/src/tools/builtin/shell/send_ctrl_keys.ts +43 -0
- package/src/tools/builtin/shell/send_keys.ts +36 -0
- package/src/tools/builtin/shell/send_text.ts +35 -0
- package/src/tools/builtin/shell/shell_script.ts +65 -0
- package/src/tools/builtin/shell/stop_command.ts +40 -0
- package/src/tools/builtin/shell/terminal_resize.ts +31 -0
- package/src/tools/builtin/shell/terminal_screenshot.ts +28 -0
- package/src/tools/builtin/system/log_viewer.ts +89 -0
- package/src/tools/builtin/system/notify_user.ts +55 -0
- package/src/tools/builtin/system/process_list.ts +66 -0
- package/src/tools/builtin/system/resource_monitor.ts +66 -0
- package/src/tools/builtin/system/system_info.ts +41 -0
- package/src/tools/tool-types.ts +97 -0
- package/src/ui/AgentTree.tsx +98 -0
- package/src/ui/App.tsx +46 -0
- package/src/ui/ChatView.tsx +278 -0
- package/src/ui/ConfirmDialog.tsx +68 -0
- package/src/ui/DiffView.tsx +64 -0
- package/src/ui/FilePreview.tsx +59 -0
- package/src/ui/InputBox.tsx +267 -0
- package/src/ui/MessageBubble.tsx +30 -0
- package/src/ui/Spinner.tsx +35 -0
- package/src/ui/StatusBar.tsx +41 -0
- package/src/ui/ToolCallCard.tsx +73 -0
- package/src/ui/ansi.ts +50 -0
- package/src/ui/markdown.ts +238 -0
- package/src/ui/themes/dark.ts +4 -0
- package/src/ui/themes/default.ts +25 -0
- package/src/ui/themes/light.ts +14 -0
- package/tests/unit/BuiltinTools.test.ts +129 -0
- package/tests/unit/BuiltinToolsIntegration.test.ts +111 -0
- package/tests/unit/FilesystemTools.test.ts +211 -0
- package/tests/unit/SkillLoader.test.ts +141 -0
- package/tests/unit/SkillRegistry.test.ts +113 -0
- package/tests/unit/ToolExecutor.test.ts +160 -0
- package/tests/unit/ToolRegistry.test.ts +103 -0
- package/tests/unit/ToolValidator.test.ts +137 -0
- package/tsconfig.json +28 -0
- package/tsup.config.ts +17 -0
- package/vitest.config.ts +20 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { useEffect, useState, useRef } from 'react';
|
|
2
|
+
import { Text, Box, useStdin } from 'ink';
|
|
3
|
+
import { MessageBubble } from './MessageBubble.tsx';
|
|
4
|
+
import { defaultTheme } from './themes/default.ts';
|
|
5
|
+
import { eventbus } from '../core/eventbus.ts';
|
|
6
|
+
import type { ChatMessage } from '../model/types.ts';
|
|
7
|
+
import readline from 'node:readline';
|
|
8
|
+
|
|
9
|
+
interface ChatViewProps {
|
|
10
|
+
initialMessages?: ChatMessage[];
|
|
11
|
+
apiKey?: string;
|
|
12
|
+
model?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const HISTORY_MAX = 100;
|
|
16
|
+
const history: string[] = [];
|
|
17
|
+
|
|
18
|
+
const SLASH_RESULTS: Record<string, string> = {
|
|
19
|
+
'/help': '命令: /help /clear /quit /config /model /tools /export /theme',
|
|
20
|
+
'/clear': '__CLEAR__',
|
|
21
|
+
'/quit': '__QUIT__',
|
|
22
|
+
'/config': '打开配置: deeper config list | deeper config set <key> <value>',
|
|
23
|
+
'/model': '模型: deepseek-v4-pro / v4-flash',
|
|
24
|
+
'/tools': '105 个内置工具已就绪',
|
|
25
|
+
'/export': '对话导出功能开发中',
|
|
26
|
+
'/theme': '主题: dark | 切换: deeper config set theme light',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function ChatView({ initialMessages = [], apiKey, model }: ChatViewProps) {
|
|
30
|
+
const [messages, setMessages] = useState<ChatMessage[]>(initialMessages);
|
|
31
|
+
const [thinking, setThinking] = useState(false);
|
|
32
|
+
const keyRef = useRef(apiKey);
|
|
33
|
+
const modelRef = useRef(model || 'deepseek-v4-pro');
|
|
34
|
+
keyRef.current = apiKey;
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const onMsg = (p: { id: string; role: 'user' | 'assistant' | 'system'; content: string; timestamp: number }) => {
|
|
38
|
+
setMessages(prev => [...prev, { ...p }]);
|
|
39
|
+
};
|
|
40
|
+
const onClear = () => { setMessages([]); setThinking(false); };
|
|
41
|
+
eventbus.onMessageReceived(onMsg);
|
|
42
|
+
eventbus.onAppClear(onClear);
|
|
43
|
+
return () => { eventbus.removeAllListeners(); };
|
|
44
|
+
}, []);
|
|
45
|
+
|
|
46
|
+
const handleSubmit = (content: string) => {
|
|
47
|
+
const trimmed = content.trim();
|
|
48
|
+
if (!trimmed) return;
|
|
49
|
+
|
|
50
|
+
if (SLASH_RESULTS[trimmed]) {
|
|
51
|
+
const r = SLASH_RESULTS[trimmed];
|
|
52
|
+
if (r === '__QUIT__') { eventbus.emitAppQuit(); return; }
|
|
53
|
+
if (r === '__CLEAR__') { setMessages([]); return; }
|
|
54
|
+
setMessages(prev => [...prev, { id: `s-${Date.now()}`, role: 'system', content: r, timestamp: Date.now() }]);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
history.push(trimmed);
|
|
59
|
+
if (history.length > HISTORY_MAX) history.shift();
|
|
60
|
+
|
|
61
|
+
const um: ChatMessage = { id: `u-${Date.now()}`, role: 'user', content: trimmed, timestamp: Date.now() };
|
|
62
|
+
setMessages(prev => [...prev, um]);
|
|
63
|
+
eventbus.emitMessageSend({ content: trimmed });
|
|
64
|
+
|
|
65
|
+
const currentKey = keyRef.current || '';
|
|
66
|
+
if (!currentKey) {
|
|
67
|
+
setMessages(prev => [...prev, {
|
|
68
|
+
id: `a-${Date.now()}`, role: 'system',
|
|
69
|
+
content: '⚠ 未配置 API Key。\n\n请运行以下命令配置:\n deeper config set apiKey "sk-你的DeepSeek密钥"\n\n获取密钥: https://platform.deepseek.com',
|
|
70
|
+
timestamp: Date.now(),
|
|
71
|
+
}]);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setThinking(true);
|
|
76
|
+
callDeepSeek(trimmed, currentKey, modelRef.current, messages)
|
|
77
|
+
.then(responseText => {
|
|
78
|
+
setThinking(false);
|
|
79
|
+
setMessages(prev => [...prev, {
|
|
80
|
+
id: `a-${Date.now()}`, role: 'assistant',
|
|
81
|
+
content: responseText,
|
|
82
|
+
timestamp: Date.now(),
|
|
83
|
+
}]);
|
|
84
|
+
})
|
|
85
|
+
.catch(err => {
|
|
86
|
+
setThinking(false);
|
|
87
|
+
setMessages(prev => [...prev, {
|
|
88
|
+
id: `a-${Date.now()}`, role: 'system',
|
|
89
|
+
content: `❌ API 请求失败: ${err instanceof Error ? err.message : String(err)}`,
|
|
90
|
+
timestamp: Date.now(),
|
|
91
|
+
}]);
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<Box flexDirection="column">
|
|
97
|
+
<Box flexDirection="column" paddingX={1}>
|
|
98
|
+
{messages.length === 0 && !thinking ? (
|
|
99
|
+
<Box flexDirection="column" paddingY={1}>
|
|
100
|
+
<Box>
|
|
101
|
+
<Text bold color={defaultTheme.primary}>DeeperCode</Text>
|
|
102
|
+
<Text dimColor> — 一句话生成完整项目</Text>
|
|
103
|
+
</Box>
|
|
104
|
+
<Box marginTop={1}>
|
|
105
|
+
<Text dimColor>输入你的任务或 /help 查看命令</Text>
|
|
106
|
+
</Box>
|
|
107
|
+
</Box>
|
|
108
|
+
) : null}
|
|
109
|
+
|
|
110
|
+
{messages.map((m) => (
|
|
111
|
+
<MessageBubble key={m.id} role={m.role} content={m.content} timestamp={m.timestamp} />
|
|
112
|
+
))}
|
|
113
|
+
|
|
114
|
+
{thinking ? (
|
|
115
|
+
<Box marginY={1}><Text dimColor>思考中...</Text></Box>
|
|
116
|
+
) : null}
|
|
117
|
+
</Box>
|
|
118
|
+
|
|
119
|
+
<InputLine onSubmit={handleSubmit} />
|
|
120
|
+
</Box>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function callDeepSeek(
|
|
125
|
+
prompt: string,
|
|
126
|
+
apiKey: string,
|
|
127
|
+
model: string,
|
|
128
|
+
history: ChatMessage[],
|
|
129
|
+
): Promise<string> {
|
|
130
|
+
const systemMsg = {
|
|
131
|
+
role: 'system',
|
|
132
|
+
content: '你是 DeeperCode AI 编程助手,基于 DeepSeek-V4-Pro。你擅长编写代码、调试、项目构建和技术问题解答。请用中文回复,代码保持原样。',
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const recentHistory = history.slice(-20).map(m => ({
|
|
136
|
+
role: m.role === 'system' ? 'system' : m.role,
|
|
137
|
+
content: m.content || '',
|
|
138
|
+
}));
|
|
139
|
+
|
|
140
|
+
const messages = [systemMsg, ...recentHistory, { role: 'user', content: prompt }];
|
|
141
|
+
|
|
142
|
+
const response = await fetch('https://api.deepseek.com/v1/chat/completions', {
|
|
143
|
+
method: 'POST',
|
|
144
|
+
headers: {
|
|
145
|
+
'Content-Type': 'application/json',
|
|
146
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
147
|
+
},
|
|
148
|
+
body: JSON.stringify({
|
|
149
|
+
model,
|
|
150
|
+
messages,
|
|
151
|
+
stream: false,
|
|
152
|
+
max_tokens: 4096,
|
|
153
|
+
temperature: 0.7,
|
|
154
|
+
}),
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
if (!response.ok) {
|
|
158
|
+
const errText = await response.text().catch(() => '');
|
|
159
|
+
if (response.status === 401) throw new Error('API Key 无效或已过期,请检查配置');
|
|
160
|
+
throw new Error(`HTTP ${response.status}: ${errText.slice(0, 200)}`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const data = await response.json() as any;
|
|
164
|
+
return data.choices?.[0]?.message?.content || '(空响应)';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function InputLine({ onSubmit }: { onSubmit: (v: string) => void }) {
|
|
168
|
+
const { stdin } = useStdin();
|
|
169
|
+
const [buf, setBuf] = useState('');
|
|
170
|
+
const bufRef = useRef('');
|
|
171
|
+
bufRef.current = buf;
|
|
172
|
+
const histIdx = useRef(-1);
|
|
173
|
+
const cursorRef = useRef(0);
|
|
174
|
+
|
|
175
|
+
useEffect(() => {
|
|
176
|
+
if (!stdin) return;
|
|
177
|
+
|
|
178
|
+
readline.emitKeypressEvents(stdin as unknown as NodeJS.ReadableStream);
|
|
179
|
+
|
|
180
|
+
const onKP = (str: string | undefined, key: readline.Key) => {
|
|
181
|
+
if (key.ctrl && key.name === 'c') {
|
|
182
|
+
eventbus.emitAppQuit();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (key.name === 'return' || key.name === 'enter') {
|
|
187
|
+
if (bufRef.current.trim()) {
|
|
188
|
+
onSubmit(bufRef.current);
|
|
189
|
+
}
|
|
190
|
+
bufRef.current = '';
|
|
191
|
+
cursorRef.current = 0;
|
|
192
|
+
setBuf('');
|
|
193
|
+
histIdx.current = -1;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (key.name === 'backspace') {
|
|
198
|
+
if (cursorRef.current > 0) {
|
|
199
|
+
const prev = bufRef.current;
|
|
200
|
+
bufRef.current = prev.slice(0, cursorRef.current - 1) + prev.slice(cursorRef.current);
|
|
201
|
+
cursorRef.current = Math.max(0, cursorRef.current - 1);
|
|
202
|
+
setBuf(bufRef.current);
|
|
203
|
+
}
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (key.name === 'delete') {
|
|
208
|
+
if (cursorRef.current < bufRef.current.length) {
|
|
209
|
+
const prev = bufRef.current;
|
|
210
|
+
bufRef.current = prev.slice(0, cursorRef.current) + prev.slice(cursorRef.current + 1);
|
|
211
|
+
setBuf(bufRef.current);
|
|
212
|
+
}
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (key.name === 'left') {
|
|
217
|
+
cursorRef.current = Math.max(0, cursorRef.current - 1);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (key.name === 'right') {
|
|
221
|
+
cursorRef.current = Math.min(bufRef.current.length, cursorRef.current + 1);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (key.name === 'home') { cursorRef.current = 0; return; }
|
|
225
|
+
if (key.name === 'end') { cursorRef.current = bufRef.current.length; return; }
|
|
226
|
+
|
|
227
|
+
if (key.name === 'up') {
|
|
228
|
+
if (history.length === 0) return;
|
|
229
|
+
const idx = histIdx.current === -1 ? history.length - 1 : Math.max(0, histIdx.current - 1);
|
|
230
|
+
histIdx.current = idx;
|
|
231
|
+
bufRef.current = history[idx];
|
|
232
|
+
cursorRef.current = bufRef.current.length;
|
|
233
|
+
setBuf(bufRef.current);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (key.name === 'down') {
|
|
238
|
+
if (histIdx.current === -1) return;
|
|
239
|
+
const idx = histIdx.current + 1;
|
|
240
|
+
if (idx >= history.length) {
|
|
241
|
+
histIdx.current = -1;
|
|
242
|
+
bufRef.current = '';
|
|
243
|
+
cursorRef.current = 0;
|
|
244
|
+
} else {
|
|
245
|
+
histIdx.current = idx;
|
|
246
|
+
bufRef.current = history[idx];
|
|
247
|
+
cursorRef.current = bufRef.current.length;
|
|
248
|
+
}
|
|
249
|
+
setBuf(bufRef.current);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (str) {
|
|
254
|
+
const prev = bufRef.current;
|
|
255
|
+
bufRef.current = prev.slice(0, cursorRef.current) + str + prev.slice(cursorRef.current);
|
|
256
|
+
cursorRef.current += str.length;
|
|
257
|
+
setBuf(bufRef.current);
|
|
258
|
+
histIdx.current = -1;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
(stdin as any).on('keypress', onKP);
|
|
263
|
+
return () => { (stdin as any).removeListener('keypress', onKP); };
|
|
264
|
+
}, []);
|
|
265
|
+
|
|
266
|
+
const before = buf.slice(0, cursorRef.current);
|
|
267
|
+
const at = buf[cursorRef.current] || ' ';
|
|
268
|
+
const after = buf.slice(cursorRef.current + 1);
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<Box paddingX={1}>
|
|
272
|
+
<Text color={defaultTheme.primary} bold>❯ </Text>
|
|
273
|
+
<Text>{before}</Text>
|
|
274
|
+
<Text inverse>{at}</Text>
|
|
275
|
+
<Text>{after}</Text>
|
|
276
|
+
</Box>
|
|
277
|
+
);
|
|
278
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import { Text, Box, useInput } from 'ink';
|
|
3
|
+
import { defaultTheme } from './themes/default.ts';
|
|
4
|
+
|
|
5
|
+
interface ConfirmDialogProps {
|
|
6
|
+
message: string;
|
|
7
|
+
detail?: string;
|
|
8
|
+
onConfirm: () => void;
|
|
9
|
+
onReject: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function ConfirmDialog({ message, detail, onConfirm, onReject }: ConfirmDialogProps) {
|
|
13
|
+
const [selected, setSelected] = useState<'confirm' | 'reject'>('reject');
|
|
14
|
+
|
|
15
|
+
useInput((input, key) => {
|
|
16
|
+
if (key.leftArrow || key.rightArrow) {
|
|
17
|
+
setSelected((prev) => (prev === 'confirm' ? 'reject' : 'confirm'));
|
|
18
|
+
}
|
|
19
|
+
if (key.return) {
|
|
20
|
+
if (selected === 'confirm') {
|
|
21
|
+
onConfirm();
|
|
22
|
+
} else {
|
|
23
|
+
onReject();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (input === 'y' || input === 'Y') {
|
|
27
|
+
onConfirm();
|
|
28
|
+
}
|
|
29
|
+
if (input === 'n' || input === 'N') {
|
|
30
|
+
onReject();
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
setSelected('reject');
|
|
36
|
+
}, [message]);
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<Box flexDirection="column" borderStyle="double" borderColor={defaultTheme.warning} paddingX={2} paddingY={1}>
|
|
40
|
+
<Box>
|
|
41
|
+
<Text bold color={defaultTheme.warning}>⚠ {message}</Text>
|
|
42
|
+
</Box>
|
|
43
|
+
{detail ? (
|
|
44
|
+
<Box marginTop={0}>
|
|
45
|
+
<Text dimColor>{detail}</Text>
|
|
46
|
+
</Box>
|
|
47
|
+
) : null}
|
|
48
|
+
<Box marginTop={1}>
|
|
49
|
+
<Box marginRight={2}>
|
|
50
|
+
<Text
|
|
51
|
+
color={selected === 'confirm' ? defaultTheme.success : defaultTheme.dimText}
|
|
52
|
+
bold={selected === 'confirm'}
|
|
53
|
+
>
|
|
54
|
+
{selected === 'confirm' ? '▶ ' : ' '}[Y] 确认
|
|
55
|
+
</Text>
|
|
56
|
+
</Box>
|
|
57
|
+
<Box>
|
|
58
|
+
<Text
|
|
59
|
+
color={selected === 'reject' ? defaultTheme.error : defaultTheme.dimText}
|
|
60
|
+
bold={selected === 'reject'}
|
|
61
|
+
>
|
|
62
|
+
{selected === 'reject' ? '▶ ' : ' '}[N] 取消
|
|
63
|
+
</Text>
|
|
64
|
+
</Box>
|
|
65
|
+
</Box>
|
|
66
|
+
</Box>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { Text, Box } from 'ink';
|
|
3
|
+
import { diffLines } from 'diff';
|
|
4
|
+
import { defaultTheme } from './themes/default.ts';
|
|
5
|
+
|
|
6
|
+
interface DiffViewProps {
|
|
7
|
+
filePath: string;
|
|
8
|
+
oldContent: string;
|
|
9
|
+
newContent: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function DiffView({ filePath, oldContent, newContent }: DiffViewProps) {
|
|
13
|
+
const changes = useMemo(() => {
|
|
14
|
+
return diffLines(oldContent || '', newContent || '', {
|
|
15
|
+
ignoreWhitespace: false,
|
|
16
|
+
});
|
|
17
|
+
}, [oldContent, newContent]);
|
|
18
|
+
|
|
19
|
+
const addedCount = changes.filter((c) => c.added).length;
|
|
20
|
+
const removedCount = changes.filter((c) => c.removed).length;
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<Box flexDirection="column" borderStyle="round" borderColor={defaultTheme.border} paddingX={1}>
|
|
24
|
+
<Box>
|
|
25
|
+
<Text bold>📄 </Text>
|
|
26
|
+
<Text bold color={defaultTheme.primary}>{filePath}</Text>
|
|
27
|
+
<Text dimColor>
|
|
28
|
+
{' '}
|
|
29
|
+
<Text color={defaultTheme.success}>+{addedCount}</Text>
|
|
30
|
+
{' / '}
|
|
31
|
+
<Text color={defaultTheme.error}>-{removedCount}</Text>
|
|
32
|
+
</Text>
|
|
33
|
+
</Box>
|
|
34
|
+
|
|
35
|
+
<Box flexDirection="column" marginTop={0}>
|
|
36
|
+
{changes.slice(0, 50).map((change, i) => {
|
|
37
|
+
if (change.added) {
|
|
38
|
+
return (
|
|
39
|
+
<Box key={i}>
|
|
40
|
+
<Text color={defaultTheme.success}>+ {change.value.trimEnd()}</Text>
|
|
41
|
+
</Box>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
if (change.removed) {
|
|
45
|
+
return (
|
|
46
|
+
<Box key={i}>
|
|
47
|
+
<Text color={defaultTheme.error}>- {change.value.trimEnd()}</Text>
|
|
48
|
+
</Box>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return (
|
|
52
|
+
<Box key={i}>
|
|
53
|
+
<Text dimColor> {change.value.trimEnd()}</Text>
|
|
54
|
+
</Box>
|
|
55
|
+
);
|
|
56
|
+
})}
|
|
57
|
+
</Box>
|
|
58
|
+
|
|
59
|
+
{changes.length > 50 ? (
|
|
60
|
+
<Text dimColor>... 还有 {changes.length - 50} 处变更未显示</Text>
|
|
61
|
+
) : null}
|
|
62
|
+
</Box>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Text, Box } from 'ink';
|
|
2
|
+
import { defaultTheme } from './themes/default.ts';
|
|
3
|
+
|
|
4
|
+
interface FilePreviewProps {
|
|
5
|
+
filePath: string;
|
|
6
|
+
content?: string;
|
|
7
|
+
language?: string;
|
|
8
|
+
lineCount?: number;
|
|
9
|
+
size?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function formatSize(bytes: number): string {
|
|
13
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
14
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
15
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function detectLang(path: string): string {
|
|
19
|
+
const ext = path.split('.').pop()?.toLowerCase();
|
|
20
|
+
const map: Record<string, string> = {
|
|
21
|
+
ts: 'TypeScript', tsx: 'TSX', js: 'JavaScript', jsx: 'JSX',
|
|
22
|
+
py: 'Python', rs: 'Rust', go: 'Go', java: 'Java',
|
|
23
|
+
json: 'JSON', yaml: 'YAML', yml: 'YAML', toml: 'TOML',
|
|
24
|
+
md: 'Markdown', html: 'HTML', css: 'CSS', scss: 'SCSS',
|
|
25
|
+
sql: 'SQL', sh: 'Shell', bat: 'Batch', ps1: 'PowerShell',
|
|
26
|
+
};
|
|
27
|
+
return map[ext || ''] || ext?.toUpperCase() || 'text';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function FilePreview({ filePath, content, language, lineCount, size }: FilePreviewProps) {
|
|
31
|
+
const lang = language || detectLang(filePath);
|
|
32
|
+
const lines = content ? content.split('\n').slice(0, 10) : [];
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<Box flexDirection="column" borderStyle="round" borderColor={defaultTheme.border} paddingX={1}>
|
|
36
|
+
<Box>
|
|
37
|
+
<Text bold>📄 </Text>
|
|
38
|
+
<Text bold color={defaultTheme.primary}>{filePath}</Text>
|
|
39
|
+
<Text dimColor> ({lang})</Text>
|
|
40
|
+
{size !== undefined ? <Text dimColor> - {formatSize(size)}</Text> : null}
|
|
41
|
+
{lineCount !== undefined ? <Text dimColor> - {lineCount} 行</Text> : null}
|
|
42
|
+
</Box>
|
|
43
|
+
|
|
44
|
+
{content ? (
|
|
45
|
+
<Box flexDirection="column" borderStyle="single" borderColor={defaultTheme.border} paddingX={1} marginTop={0}>
|
|
46
|
+
{lines.map((line, i) => (
|
|
47
|
+
<Box key={i}>
|
|
48
|
+
<Text dimColor>{(i + 1).toString().padStart(3, ' ')} │ </Text>
|
|
49
|
+
<Text>{line.length > 100 ? line.slice(0, 100) + '...' : line}</Text>
|
|
50
|
+
</Box>
|
|
51
|
+
))}
|
|
52
|
+
{content.split('\n').length > 10 ? (
|
|
53
|
+
<Text dimColor>... 还有 {content.split('\n').length - 10} 行</Text>
|
|
54
|
+
) : null}
|
|
55
|
+
</Box>
|
|
56
|
+
) : null}
|
|
57
|
+
</Box>
|
|
58
|
+
);
|
|
59
|
+
}
|