@orbit-build/cli 0.1.0 → 0.1.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/dist/index.js +178 -65
- package/package.json +22 -9
- package/dist/FullscreenTui-BNSSKSLN.js +0 -14
- package/dist/chunk-QYRH36M4.js +0 -1919
- package/src/commands/LSPServer.ts +0 -231
- package/src/commands/config.ts +0 -8
- package/src/commands/customCommands.test.ts +0 -62
- package/src/commands/customCommands.ts +0 -127
- package/src/commands/doctor.ts +0 -63
- package/src/commands/exec.test.ts +0 -112
- package/src/commands/init.ts +0 -26
- package/src/commands/login.ts +0 -120
- package/src/commands/run.test.ts +0 -290
- package/src/commands/run.ts +0 -196
- package/src/index.ts +0 -97
- package/src/runtime/CommandRouter.test.ts +0 -107
- package/src/runtime/CommandRouter.ts +0 -2025
- package/src/runtime/ReplController.test.ts +0 -36
- package/src/runtime/ReplController.ts +0 -539
- package/src/tui/FullscreenTui.ts +0 -2548
- package/tsconfig.json +0 -8
- package/verify_agent_loop.js +0 -91
|
@@ -1,2025 +0,0 @@
|
|
|
1
|
-
import { AgentLoop, UserInteraction } from "@orbit-build/core";
|
|
2
|
-
import { FullscreenTui } from "../tui/FullscreenTui.js";
|
|
3
|
-
import { ConfigSchema } from "@orbit-build/config";
|
|
4
|
-
import { Prompt } from "@orbit-build/tui";
|
|
5
|
-
import picocolors from "picocolors";
|
|
6
|
-
import glob from "fast-glob";
|
|
7
|
-
import { existsSync, readFileSync, readdirSync } from "fs";
|
|
8
|
-
import { join, dirname, resolve } from "path";
|
|
9
|
-
import { homedir } from "os";
|
|
10
|
-
import { PermissionEngine } from "@orbit-build/permissions";
|
|
11
|
-
import { expandCustomCommand, loadCustomCommands } from "../commands/customCommands.js";
|
|
12
|
-
import { createRequire } from "module";
|
|
13
|
-
|
|
14
|
-
const require = createRequire(import.meta.url);
|
|
15
|
-
|
|
16
|
-
export const BUILTIN_SLASH_COMMANDS = [
|
|
17
|
-
"/help",
|
|
18
|
-
"/status",
|
|
19
|
-
"/config",
|
|
20
|
-
"/model",
|
|
21
|
-
"/chat",
|
|
22
|
-
"/commit",
|
|
23
|
-
"/exit",
|
|
24
|
-
"/quit",
|
|
25
|
-
"/rollback",
|
|
26
|
-
"/clear",
|
|
27
|
-
"/add",
|
|
28
|
-
"/drop",
|
|
29
|
-
"/mode",
|
|
30
|
-
"/copy",
|
|
31
|
-
] as const;
|
|
32
|
-
|
|
33
|
-
export class CommandRouter {
|
|
34
|
-
constructor(
|
|
35
|
-
private cwd: string,
|
|
36
|
-
private config: any,
|
|
37
|
-
private providerInstance: any,
|
|
38
|
-
private setProviderInstance: (newProvider: any) => void,
|
|
39
|
-
private loop: AgentLoop,
|
|
40
|
-
private tui: FullscreenTui,
|
|
41
|
-
private useFullscreenTui: boolean,
|
|
42
|
-
private getCandidates: () => any,
|
|
43
|
-
private setCandidates: (candidates: any) => void,
|
|
44
|
-
private getLocalState: () => any,
|
|
45
|
-
private saveLocalState: (state: any) => void,
|
|
46
|
-
private tuiInteraction: UserInteraction,
|
|
47
|
-
private multi?: boolean
|
|
48
|
-
) {}
|
|
49
|
-
|
|
50
|
-
private printOutput(text: string, raw = false) {
|
|
51
|
-
if (this.tui && this.tui.isActive) {
|
|
52
|
-
this.tui.addSystemMessage(text, raw);
|
|
53
|
-
} else {
|
|
54
|
-
console.log(text);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public async route(input: string): Promise<{ shouldExit: boolean; processed: boolean }> {
|
|
59
|
-
let trimmed = input.trim();
|
|
60
|
-
if (!trimmed) return { shouldExit: false, processed: false };
|
|
61
|
-
|
|
62
|
-
const useFullscreenTui = this.useFullscreenTui;
|
|
63
|
-
const tui = this.tui;
|
|
64
|
-
const config = this.config;
|
|
65
|
-
const loop = this.loop;
|
|
66
|
-
const cwd = this.cwd;
|
|
67
|
-
|
|
68
|
-
if (trimmed.startsWith("/")) {
|
|
69
|
-
const commandName = trimmed.slice(1).split(/\s+/, 1)[0].toLowerCase();
|
|
70
|
-
const customCommand = loadCustomCommands(
|
|
71
|
-
cwd,
|
|
72
|
-
BUILTIN_SLASH_COMMANDS,
|
|
73
|
-
).find((candidate) => candidate.name === commandName);
|
|
74
|
-
if (customCommand) {
|
|
75
|
-
const rawArguments = trimmed.slice(commandName.length + 1).trim();
|
|
76
|
-
trimmed = expandCustomCommand(customCommand, rawArguments);
|
|
77
|
-
tui.addLog(
|
|
78
|
-
`${config.language === "zh" ? "已展开自定义命令" : "Expanded custom command"} /${customCommand.name}`,
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (trimmed.startsWith("!") || trimmed.startsWith("/run")) {
|
|
84
|
-
const wasActive = useFullscreenTui && tui.isActive;
|
|
85
|
-
if (wasActive) tui.stop();
|
|
86
|
-
|
|
87
|
-
let shellCmd = "";
|
|
88
|
-
if (trimmed.startsWith("!")) {
|
|
89
|
-
shellCmd = trimmed.substring(1).trim();
|
|
90
|
-
} else {
|
|
91
|
-
shellCmd = trimmed.substring(4).trim();
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const isZh = config.language === "zh";
|
|
95
|
-
if (!shellCmd) {
|
|
96
|
-
console.log(
|
|
97
|
-
isZh
|
|
98
|
-
? picocolors.yellow(
|
|
99
|
-
"用法: !<shell_command> 或 /run <shell_command>",
|
|
100
|
-
)
|
|
101
|
-
: picocolors.yellow(
|
|
102
|
-
"Usage: !<shell_command> or /run <shell_command>",
|
|
103
|
-
),
|
|
104
|
-
);
|
|
105
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
106
|
-
return { shouldExit: false, processed: true };
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const permissionEngine = new PermissionEngine(config);
|
|
110
|
-
const decision = permissionEngine.evaluate(
|
|
111
|
-
"bash",
|
|
112
|
-
{ command: shellCmd },
|
|
113
|
-
"execute",
|
|
114
|
-
);
|
|
115
|
-
if (decision.action === "deny") {
|
|
116
|
-
console.log(
|
|
117
|
-
picocolors.red(
|
|
118
|
-
isZh
|
|
119
|
-
? `✖ 命令已被安全策略阻止: ${decision.reason}`
|
|
120
|
-
: `✖ Command blocked by safety policy: ${decision.reason}`,
|
|
121
|
-
),
|
|
122
|
-
);
|
|
123
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
124
|
-
return { shouldExit: false, processed: true };
|
|
125
|
-
}
|
|
126
|
-
if (decision.action === "ask") {
|
|
127
|
-
const approved = await Prompt.askApproval(
|
|
128
|
-
isZh
|
|
129
|
-
? `命令需要 ${decision.risk} 权限:${shellCmd}`
|
|
130
|
-
: `Command requires ${decision.risk} permission: ${shellCmd}`,
|
|
131
|
-
);
|
|
132
|
-
if (!approved) {
|
|
133
|
-
console.log(
|
|
134
|
-
isZh ? "已取消命令执行。" : "Command execution cancelled.",
|
|
135
|
-
);
|
|
136
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
137
|
-
return { shouldExit: false, processed: true };
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
console.log(
|
|
142
|
-
isZh
|
|
143
|
-
? picocolors.cyan(`\n正在执行 Shell 命令: ${shellCmd}...`)
|
|
144
|
-
: picocolors.cyan(`\nRunning shell command: ${shellCmd}...`),
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
try {
|
|
148
|
-
const { spawnSync } = await import("child_process");
|
|
149
|
-
const result = spawnSync(shellCmd, {
|
|
150
|
-
cwd,
|
|
151
|
-
stdio: "inherit",
|
|
152
|
-
shell: true,
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
if (result.status === 0) {
|
|
156
|
-
console.log(
|
|
157
|
-
isZh
|
|
158
|
-
? picocolors.green(`\n✔ 命令执行成功。`)
|
|
159
|
-
: picocolors.green(`\n✔ Command completed successfully.`),
|
|
160
|
-
);
|
|
161
|
-
} else {
|
|
162
|
-
console.log(
|
|
163
|
-
isZh
|
|
164
|
-
? picocolors.red(`\n✖ 命令执行失败,退出代码: ${result.status}`)
|
|
165
|
-
: picocolors.red(
|
|
166
|
-
`\n✖ Command failed with exit code ${result.status}`,
|
|
167
|
-
),
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
await Prompt.askText(
|
|
172
|
-
isZh
|
|
173
|
-
? "按 Enter 键返回 Orbit..."
|
|
174
|
-
: "Press Enter to return to Orbit...",
|
|
175
|
-
);
|
|
176
|
-
} catch (err: any) {
|
|
177
|
-
console.log(
|
|
178
|
-
isZh
|
|
179
|
-
? picocolors.red(`无法执行命令: ${err.message}`)
|
|
180
|
-
: picocolors.red(`Failed to execute command: ${err.message}`),
|
|
181
|
-
);
|
|
182
|
-
} finally {
|
|
183
|
-
tui.syncFromLoop(loop);
|
|
184
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
185
|
-
}
|
|
186
|
-
return { shouldExit: false, processed: true };
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (trimmed.startsWith("/")) {
|
|
190
|
-
const parts = trimmed.split(" ");
|
|
191
|
-
const command = parts[0].toLowerCase();
|
|
192
|
-
|
|
193
|
-
if (command === "/exit" || command === "/quit") {
|
|
194
|
-
console.log(
|
|
195
|
-
picocolors.yellow("Exiting Orbit Interactive Shell. Goodbye!"),
|
|
196
|
-
);
|
|
197
|
-
return { shouldExit: true, processed: true };
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (command === "/help") {
|
|
201
|
-
const isZh = config.language === "zh";
|
|
202
|
-
let helpText = "";
|
|
203
|
-
|
|
204
|
-
if (isZh) {
|
|
205
|
-
helpText = [
|
|
206
|
-
"",
|
|
207
|
-
picocolors.bold(picocolors.cyan("● Orbit 交互式终端指令指南")),
|
|
208
|
-
"",
|
|
209
|
-
picocolors.bold(picocolors.yellow(" [ 上下文管理 (Context) ]")),
|
|
210
|
-
` ${picocolors.green("/add")} ${picocolors.cyan("<file>")} - 添加文件/目录至当前上下文 (使用 -r 设为只读)`,
|
|
211
|
-
` ${picocolors.green("/drop")} ${picocolors.cyan("<file>")} - 从活动上下文中移除指定文件或通配符模式`,
|
|
212
|
-
` ${picocolors.green("/clear")} - 清空终端屏幕和滚动缓冲`,
|
|
213
|
-
"",
|
|
214
|
-
picocolors.bold(picocolors.yellow(" [ 会话与历史 (Session) ]")),
|
|
215
|
-
` ${picocolors.green("/chat")} ${picocolors.cyan("[action]")} - 管理对话会话 (子命令: list/ls, new, delete/rm, switch)`,
|
|
216
|
-
` ${picocolors.green("/rollback")} - 回滚最近的文件修改检查点`,
|
|
217
|
-
` ${picocolors.green("/copy")} - 拷贝 AI 的最新回复至系统剪贴板`,
|
|
218
|
-
"",
|
|
219
|
-
picocolors.bold(picocolors.yellow(" [ 配置与状态 (Settings) ]")),
|
|
220
|
-
` ${picocolors.green("/status")} - 诊断并展示当前会话、模型、Token 消耗和限额`,
|
|
221
|
-
` ${picocolors.green("/config")} ${picocolors.cyan("[k=v]")} - 查看或交互式/直接修改配置参数`,
|
|
222
|
-
` ${picocolors.green("/model")} ${picocolors.cyan("[name]")} - 动态查询或切换正在使用的 AI 语言大模型`,
|
|
223
|
-
` ${picocolors.green("/mode")} ${picocolors.cyan("[mode]")} - 切换安全确认模式 (strict, normal, auto, plan)`,
|
|
224
|
-
"",
|
|
225
|
-
picocolors.bold(picocolors.yellow(" [ Git 提交 (Git) ]")),
|
|
226
|
-
` ${picocolors.green("/commit")} ${picocolors.cyan("[msg]")} - 暂存工作区修改并生成提交 (空消息时使用 LLM 自动生成)`,
|
|
227
|
-
"",
|
|
228
|
-
picocolors.bold(picocolors.yellow(" [ 系统控制 (System) ]")),
|
|
229
|
-
` ${picocolors.green("/help")} - 显示此帮助信息`,
|
|
230
|
-
` ${picocolors.green("/exit")} / ${picocolors.green("/quit")} - 安全退出交互式终端`,
|
|
231
|
-
"",
|
|
232
|
-
picocolors.bold(picocolors.cyan(" [ Shell 命令直接运行 ]")),
|
|
233
|
-
` ${picocolors.green("!<cmd>")} - 直接执行系统原生 Shell 命令 (例如: !git diff)`,
|
|
234
|
-
"",
|
|
235
|
-
].join("\n");
|
|
236
|
-
} else {
|
|
237
|
-
helpText = [
|
|
238
|
-
"",
|
|
239
|
-
picocolors.bold(picocolors.cyan("● Orbit Interactive Shell Commands Guide")),
|
|
240
|
-
"",
|
|
241
|
-
picocolors.bold(picocolors.yellow(" [ Context Management ]")),
|
|
242
|
-
` ${picocolors.green("/add")} ${picocolors.cyan("<file>")} - Add files/dirs to prompt context (use -r for read-only)`,
|
|
243
|
-
` ${picocolors.green("/drop")} ${picocolors.cyan("<file>")} - Remove files/patterns from active prompt context`,
|
|
244
|
-
` ${picocolors.green("/clear")} - Clear the terminal screen and scrollback buffer`,
|
|
245
|
-
"",
|
|
246
|
-
picocolors.bold(picocolors.yellow(" [ Session & History ]")),
|
|
247
|
-
` ${picocolors.green("/chat")} ${picocolors.cyan("[action]")} - Manage sessions (subcommands: list/ls, new, delete/rm, switch)`,
|
|
248
|
-
` ${picocolors.green("/rollback")} - Revert the last file edits checkpoint`,
|
|
249
|
-
` ${picocolors.green("/copy")} - Copy last assistant response to system clipboard`,
|
|
250
|
-
"",
|
|
251
|
-
picocolors.bold(picocolors.yellow(" [ Configuration & Status ]")),
|
|
252
|
-
` ${picocolors.green("/status")} - Display active session info, token usage, and budget limits`,
|
|
253
|
-
` ${picocolors.green("/config")} ${picocolors.cyan("[k=v]")} - View or modify configurations interactively or via key=value`,
|
|
254
|
-
` ${picocolors.green("/model")} ${picocolors.cyan("[name]")} - Query or dynamically swap the active AI model`,
|
|
255
|
-
` ${picocolors.green("/mode")} ${picocolors.cyan("[mode]")} - Switch permission safety mode (strict, normal, auto, plan)`,
|
|
256
|
-
"",
|
|
257
|
-
picocolors.bold(picocolors.yellow(" [ Git Operations ]")),
|
|
258
|
-
` ${picocolors.green("/commit")} ${picocolors.cyan("[msg]")} - Stage all changes and commit (generates message via LLM if empty)`,
|
|
259
|
-
"",
|
|
260
|
-
picocolors.bold(picocolors.yellow(" [ System Commands ]")),
|
|
261
|
-
` ${picocolors.green("/help")} - Show this commands guide`,
|
|
262
|
-
` ${picocolors.green("/exit")} / ${picocolors.green("/quit")} - Terminate the interactive session`,
|
|
263
|
-
"",
|
|
264
|
-
picocolors.bold(picocolors.cyan(" [ Direct Shell Execution ]")),
|
|
265
|
-
` ${picocolors.green("!<cmd>")} - Run a raw shell command directly on the host machine (e.g. !git status)`,
|
|
266
|
-
"",
|
|
267
|
-
].join("\n");
|
|
268
|
-
}
|
|
269
|
-
this.printOutput(helpText);
|
|
270
|
-
return { shouldExit: false, processed: true };
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
if (command === "/rollback") {
|
|
276
|
-
const isZh = config.language === "zh";
|
|
277
|
-
const args = parts.slice(1).join(" ").trim();
|
|
278
|
-
|
|
279
|
-
if (args === "all" || args === "--all") {
|
|
280
|
-
await loop.rollbackLastCheckpoint();
|
|
281
|
-
return { shouldExit: false, processed: true };
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const { execSync } = await import("child_process");
|
|
285
|
-
let statusOut = "";
|
|
286
|
-
try {
|
|
287
|
-
statusOut = execSync("git status --porcelain", {
|
|
288
|
-
cwd,
|
|
289
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
290
|
-
}).toString();
|
|
291
|
-
} catch {
|
|
292
|
-
await loop.rollbackLastCheckpoint();
|
|
293
|
-
return { shouldExit: false, processed: true };
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const lines = statusOut
|
|
297
|
-
.split("\n")
|
|
298
|
-
.map((l) => l.trim())
|
|
299
|
-
.filter(Boolean);
|
|
300
|
-
if (lines.length === 0) {
|
|
301
|
-
console.log(
|
|
302
|
-
isZh
|
|
303
|
-
? picocolors.yellow(
|
|
304
|
-
"当前工作区没有检测到任何未提交的代码变更。",
|
|
305
|
-
)
|
|
306
|
-
: picocolors.yellow(
|
|
307
|
-
"No uncommitted changes detected in the workspace.",
|
|
308
|
-
),
|
|
309
|
-
);
|
|
310
|
-
return { shouldExit: false, processed: true };
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
const modifiedFiles = lines.map((line) => {
|
|
314
|
-
const filepath = line
|
|
315
|
-
.substring(3)
|
|
316
|
-
.trim()
|
|
317
|
-
.replace(/^["']|["']$/g, "");
|
|
318
|
-
if (filepath.includes(" -> ")) {
|
|
319
|
-
const parts = filepath.split(" -> ");
|
|
320
|
-
return parts[parts.length - 1].trim().replace(/^["']|["']$/g, "");
|
|
321
|
-
}
|
|
322
|
-
return filepath;
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
const wasActive = useFullscreenTui && tui.isActive;
|
|
326
|
-
if (wasActive) tui.stop();
|
|
327
|
-
|
|
328
|
-
try {
|
|
329
|
-
const options = [
|
|
330
|
-
{
|
|
331
|
-
value: "all",
|
|
332
|
-
label: isZh
|
|
333
|
-
? "【全部回滚】 撤销所有变更"
|
|
334
|
-
: "[Rollback All] Discard all changes",
|
|
335
|
-
},
|
|
336
|
-
...modifiedFiles.map((f) => ({ value: f, label: f })),
|
|
337
|
-
];
|
|
338
|
-
|
|
339
|
-
const selected = await Prompt.askMultiSelect(
|
|
340
|
-
isZh
|
|
341
|
-
? "选择要回滚(撤销变更)的文件:"
|
|
342
|
-
: "Select files to rollback (discard changes):",
|
|
343
|
-
options,
|
|
344
|
-
);
|
|
345
|
-
|
|
346
|
-
if (selected && selected.length > 0) {
|
|
347
|
-
if (selected.includes("all")) {
|
|
348
|
-
await loop.rollbackLastCheckpoint();
|
|
349
|
-
} else {
|
|
350
|
-
for (const file of selected) {
|
|
351
|
-
const rolledBack = (loop as any).rollbackFileToCheckpoint(
|
|
352
|
-
file,
|
|
353
|
-
);
|
|
354
|
-
if (!rolledBack) {
|
|
355
|
-
try {
|
|
356
|
-
execSync(`git checkout -- "${file}"`, {
|
|
357
|
-
cwd,
|
|
358
|
-
stdio: "ignore",
|
|
359
|
-
});
|
|
360
|
-
} catch {
|
|
361
|
-
try {
|
|
362
|
-
const fullP = resolve(cwd, file);
|
|
363
|
-
if (existsSync(fullP)) {
|
|
364
|
-
import("fs").then(fs => fs.unlinkSync(fullP));
|
|
365
|
-
}
|
|
366
|
-
} catch {}
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
console.log(
|
|
371
|
-
isZh
|
|
372
|
-
? picocolors.green(
|
|
373
|
-
`✔ 成功回滚以下文件的变更: ${selected.join(", ")}`,
|
|
374
|
-
)
|
|
375
|
-
: picocolors.green(
|
|
376
|
-
`✔ Successfully rolled back changes for: ${selected.join(", ")}`,
|
|
377
|
-
),
|
|
378
|
-
);
|
|
379
|
-
}
|
|
380
|
-
} else {
|
|
381
|
-
console.log(
|
|
382
|
-
isZh
|
|
383
|
-
? picocolors.yellow("未选择任何文件。")
|
|
384
|
-
: picocolors.yellow("No files selected."),
|
|
385
|
-
);
|
|
386
|
-
}
|
|
387
|
-
} catch (err: any) {
|
|
388
|
-
console.log(
|
|
389
|
-
isZh
|
|
390
|
-
? picocolors.red(`回滚操作失败: ${err.message}`)
|
|
391
|
-
: picocolors.red(`Rollback operation failed: ${err.message}`),
|
|
392
|
-
);
|
|
393
|
-
} finally {
|
|
394
|
-
tui.syncFromLoop(loop);
|
|
395
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
396
|
-
}
|
|
397
|
-
return { shouldExit: false, processed: true };
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
if (command === "/status") {
|
|
403
|
-
const activeConfig = loop.getConfig();
|
|
404
|
-
const activeModel = loop.getModelOverride() || activeConfig.models.default;
|
|
405
|
-
const budgetLimit = activeConfig.budgetLimit;
|
|
406
|
-
const currentCost = loop.getSessionCost();
|
|
407
|
-
const mode = activeConfig.permissions.mode;
|
|
408
|
-
|
|
409
|
-
const statusText = [
|
|
410
|
-
picocolors.bold(picocolors.cyan("\n=== Orbit Session Status ===")),
|
|
411
|
-
` 🆔 Session ID: ${picocolors.green(loop.getSessionId())}`,
|
|
412
|
-
` 🔌 Provider: ${picocolors.green(this.providerInstance.id)} (${this.providerInstance.baseUrl || "Default URL"})`,
|
|
413
|
-
` 🤖 Active Model: ${picocolors.green(activeModel)}`,
|
|
414
|
-
` 💰 Session Cost: $${currentCost.toFixed(4)} / $${budgetLimit.toFixed(2)} (Limit)`,
|
|
415
|
-
` 🛡️ Security Mode: ${picocolors.green(mode.toUpperCase())}`,
|
|
416
|
-
picocolors.cyan("============================\n"),
|
|
417
|
-
].join("\n");
|
|
418
|
-
this.printOutput(statusText);
|
|
419
|
-
return { shouldExit: false, processed: true };
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
if (command === "/config") {
|
|
423
|
-
const configArg = parts.slice(1).join(" ").trim();
|
|
424
|
-
const activeConfig = loop.getConfig();
|
|
425
|
-
|
|
426
|
-
if (configArg) {
|
|
427
|
-
const eqIndex = configArg.indexOf("=");
|
|
428
|
-
if (eqIndex === -1) {
|
|
429
|
-
this.printOutput(
|
|
430
|
-
picocolors.yellow(
|
|
431
|
-
"Usage: /config <key>=<value> or just /config for interactive menu.",
|
|
432
|
-
),
|
|
433
|
-
);
|
|
434
|
-
return { shouldExit: false, processed: true };
|
|
435
|
-
}
|
|
436
|
-
const key = configArg.slice(0, eqIndex).trim();
|
|
437
|
-
const rawVal = configArg.slice(eqIndex + 1).trim();
|
|
438
|
-
|
|
439
|
-
const currentVal = this.getNestedProperty(activeConfig, key);
|
|
440
|
-
if (currentVal === undefined) {
|
|
441
|
-
this.printOutput(
|
|
442
|
-
picocolors.red(`Error: Unknown configuration key "${key}".`),
|
|
443
|
-
);
|
|
444
|
-
return { shouldExit: false, processed: true };
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
let parsedVal: any = rawVal;
|
|
448
|
-
if (typeof currentVal === "boolean") {
|
|
449
|
-
const lowerVal = rawVal.toLowerCase();
|
|
450
|
-
if (lowerVal === "true" || lowerVal === "1") parsedVal = true;
|
|
451
|
-
else if (lowerVal === "false" || lowerVal === "0")
|
|
452
|
-
parsedVal = false;
|
|
453
|
-
else {
|
|
454
|
-
this.printOutput(
|
|
455
|
-
picocolors.red(
|
|
456
|
-
`Error: Key "${key}" expects a boolean value (true/false).`,
|
|
457
|
-
),
|
|
458
|
-
);
|
|
459
|
-
return { shouldExit: false, processed: true };
|
|
460
|
-
}
|
|
461
|
-
} else if (typeof currentVal === "number") {
|
|
462
|
-
const num = Number(rawVal);
|
|
463
|
-
if (isNaN(num)) {
|
|
464
|
-
this.printOutput(
|
|
465
|
-
picocolors.red(
|
|
466
|
-
`Error: Key "${key}" expects a numeric value.`,
|
|
467
|
-
),
|
|
468
|
-
);
|
|
469
|
-
return { shouldExit: false, processed: true };
|
|
470
|
-
}
|
|
471
|
-
parsedVal = num;
|
|
472
|
-
} else if (Array.isArray(currentVal)) {
|
|
473
|
-
parsedVal = rawVal
|
|
474
|
-
.split(",")
|
|
475
|
-
.map((s) => s.trim())
|
|
476
|
-
.filter(Boolean);
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
const testConfig = JSON.parse(JSON.stringify(activeConfig));
|
|
480
|
-
this.setNestedProperty(testConfig, key, parsedVal);
|
|
481
|
-
|
|
482
|
-
const parseResult = ConfigSchema.safeParse(testConfig);
|
|
483
|
-
if (!parseResult.success) {
|
|
484
|
-
this.printOutput(
|
|
485
|
-
picocolors.red(
|
|
486
|
-
`Configuration validation failed: ${parseResult.error.message}`,
|
|
487
|
-
),
|
|
488
|
-
);
|
|
489
|
-
return { shouldExit: false, processed: true };
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
this.setNestedProperty(activeConfig, key, parsedVal);
|
|
493
|
-
this.printOutput(
|
|
494
|
-
picocolors.green(`✔ Updated "${key}" to: ${parsedVal}`),
|
|
495
|
-
);
|
|
496
|
-
return { shouldExit: false, processed: true };
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
const wasActive = useFullscreenTui && tui.isActive;
|
|
500
|
-
if (wasActive) tui.stop();
|
|
501
|
-
try {
|
|
502
|
-
while (true) {
|
|
503
|
-
const currentMode = activeConfig.permissions.mode;
|
|
504
|
-
const currentBudget = activeConfig.budgetLimit;
|
|
505
|
-
const currentAllowRead = activeConfig.permissions.allowRead;
|
|
506
|
-
const currentApprovalWrite =
|
|
507
|
-
activeConfig.permissions.requireApprovalForWrite;
|
|
508
|
-
const currentApprovalBash =
|
|
509
|
-
activeConfig.permissions.requireApprovalForBash;
|
|
510
|
-
const currentBlockDangerous =
|
|
511
|
-
activeConfig.permissions.blockDangerousCommands;
|
|
512
|
-
const currentProtectSecrets =
|
|
513
|
-
activeConfig.permissions.protectSecrets;
|
|
514
|
-
const currentBashEnabled = activeConfig.tools.bash.enabled;
|
|
515
|
-
const currentSearchEnabled = activeConfig.tools.webSearch.enabled;
|
|
516
|
-
const currentMcpEnabled = activeConfig.tools.mcp.enabled;
|
|
517
|
-
const currentEditor = activeConfig.editor;
|
|
518
|
-
const currentAutoCommit = activeConfig.autoCommit;
|
|
519
|
-
const currentProtectedPaths =
|
|
520
|
-
activeConfig.permissions.protectedPaths;
|
|
521
|
-
const currentIgnore = activeConfig.context.ignore;
|
|
522
|
-
|
|
523
|
-
const choice = await Prompt.askSelect(
|
|
524
|
-
"Select a configuration key to modify:",
|
|
525
|
-
[
|
|
526
|
-
{
|
|
527
|
-
value: "permissions.mode",
|
|
528
|
-
label: `🛡️ permissions.mode (current: ${currentMode})`,
|
|
529
|
-
},
|
|
530
|
-
{
|
|
531
|
-
value: "budgetLimit",
|
|
532
|
-
label: `💰 budgetLimit (current: $${currentBudget})`,
|
|
533
|
-
},
|
|
534
|
-
{
|
|
535
|
-
value: "permissions.allowRead",
|
|
536
|
-
label: `📄 permissions.allowRead (current: ${currentAllowRead})`,
|
|
537
|
-
},
|
|
538
|
-
{
|
|
539
|
-
value: "permissions.requireApprovalForWrite",
|
|
540
|
-
label: `✏️ permissions.requireApprovalForWrite (current: ${currentApprovalWrite})`,
|
|
541
|
-
},
|
|
542
|
-
{
|
|
543
|
-
value: "permissions.requireApprovalForBash",
|
|
544
|
-
label: `🐚 permissions.requireApprovalForBash (current: ${currentApprovalBash})`,
|
|
545
|
-
},
|
|
546
|
-
{
|
|
547
|
-
value: "permissions.blockDangerousCommands",
|
|
548
|
-
label: `🚫 permissions.blockDangerousCommands (current: ${currentBlockDangerous})`,
|
|
549
|
-
},
|
|
550
|
-
{
|
|
551
|
-
value: "permissions.protectSecrets",
|
|
552
|
-
label: `🔑 permissions.protectSecrets (current: ${currentProtectSecrets})`,
|
|
553
|
-
},
|
|
554
|
-
{
|
|
555
|
-
value: "tools.bash.enabled",
|
|
556
|
-
label: `💻 tools.bash.enabled (current: ${currentBashEnabled})`,
|
|
557
|
-
},
|
|
558
|
-
{
|
|
559
|
-
value: "tools.webSearch.enabled",
|
|
560
|
-
label: `🌐 tools.webSearch.enabled (current: ${currentSearchEnabled})`,
|
|
561
|
-
},
|
|
562
|
-
{
|
|
563
|
-
value: "tools.mcp.enabled",
|
|
564
|
-
label: `🔌 tools.mcp.enabled (current: ${currentMcpEnabled})`,
|
|
565
|
-
},
|
|
566
|
-
{
|
|
567
|
-
value: "permissions.protectedPaths",
|
|
568
|
-
label: `🔒 permissions.protectedPaths (current: ${currentProtectedPaths.join(", ")})`,
|
|
569
|
-
},
|
|
570
|
-
{
|
|
571
|
-
value: "context.ignore",
|
|
572
|
-
label: `🗂️ context.ignore (current: ${currentIgnore.join(", ")})`,
|
|
573
|
-
},
|
|
574
|
-
{
|
|
575
|
-
value: "editor",
|
|
576
|
-
label: `📝 editor (current: ${currentEditor})`,
|
|
577
|
-
},
|
|
578
|
-
{
|
|
579
|
-
value: "autoCommit",
|
|
580
|
-
label: `🚀 autoCommit (current: ${currentAutoCommit})`,
|
|
581
|
-
},
|
|
582
|
-
{ value: "exit", label: "❌ Exit Menu" },
|
|
583
|
-
],
|
|
584
|
-
);
|
|
585
|
-
|
|
586
|
-
if (choice === null || choice === "exit" || choice === "") {
|
|
587
|
-
break;
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
const currentVal = this.getNestedProperty(activeConfig, choice);
|
|
591
|
-
if (typeof currentVal === "boolean") {
|
|
592
|
-
const nextVal = await Prompt.askSelect(`Set ${choice} to:`, [
|
|
593
|
-
{ value: "true", label: "true" },
|
|
594
|
-
{ value: "false", label: "false" },
|
|
595
|
-
]);
|
|
596
|
-
if (nextVal !== null && nextVal !== "") {
|
|
597
|
-
const boolVal = nextVal === "true";
|
|
598
|
-
const testConfig = JSON.parse(JSON.stringify(activeConfig));
|
|
599
|
-
this.setNestedProperty(testConfig, choice, boolVal);
|
|
600
|
-
const parseResult = ConfigSchema.safeParse(testConfig);
|
|
601
|
-
if (parseResult.success) {
|
|
602
|
-
this.setNestedProperty(activeConfig, choice, boolVal);
|
|
603
|
-
console.log(
|
|
604
|
-
picocolors.green(`✔ Updated "${choice}" to: ${boolVal}`),
|
|
605
|
-
);
|
|
606
|
-
} else {
|
|
607
|
-
console.log(
|
|
608
|
-
picocolors.red(
|
|
609
|
-
`Validation error: ${parseResult.error.message}`,
|
|
610
|
-
),
|
|
611
|
-
);
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
} else if (choice === "permissions.mode") {
|
|
615
|
-
const nextVal = await Prompt.askSelect(
|
|
616
|
-
"Set permissions.mode to:",
|
|
617
|
-
[
|
|
618
|
-
{
|
|
619
|
-
value: "strict",
|
|
620
|
-
label:
|
|
621
|
-
"strict (High security, ask for write/exec, block dangerous)",
|
|
622
|
-
},
|
|
623
|
-
{
|
|
624
|
-
value: "normal",
|
|
625
|
-
label: "normal (Standard safety, ask for all write/exec)",
|
|
626
|
-
},
|
|
627
|
-
{
|
|
628
|
-
value: "auto",
|
|
629
|
-
label:
|
|
630
|
-
"auto (Allow write/exec automatically, block dangerous)",
|
|
631
|
-
},
|
|
632
|
-
{
|
|
633
|
-
value: "plan",
|
|
634
|
-
label: "plan (Interactive planning mode - read-only)",
|
|
635
|
-
},
|
|
636
|
-
],
|
|
637
|
-
);
|
|
638
|
-
if (nextVal !== null && nextVal !== "") {
|
|
639
|
-
const testConfig = JSON.parse(JSON.stringify(activeConfig));
|
|
640
|
-
this.setNestedProperty(testConfig, choice, nextVal);
|
|
641
|
-
const parseResult = ConfigSchema.safeParse(testConfig);
|
|
642
|
-
if (parseResult.success) {
|
|
643
|
-
this.setNestedProperty(activeConfig, choice, nextVal);
|
|
644
|
-
console.log(
|
|
645
|
-
picocolors.green(`✔ Updated "${choice}" to: ${nextVal}`),
|
|
646
|
-
);
|
|
647
|
-
} else {
|
|
648
|
-
console.log(
|
|
649
|
-
picocolors.red(
|
|
650
|
-
`Validation error: ${parseResult.error.message}`,
|
|
651
|
-
),
|
|
652
|
-
);
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
} else if (choice === "budgetLimit") {
|
|
656
|
-
const nextValStr = await Prompt.askText(
|
|
657
|
-
`Enter new budget limit (number):`,
|
|
658
|
-
String(currentVal),
|
|
659
|
-
);
|
|
660
|
-
if (nextValStr !== null && nextValStr !== "") {
|
|
661
|
-
const numVal = Number(nextValStr);
|
|
662
|
-
if (isNaN(numVal)) {
|
|
663
|
-
console.log(
|
|
664
|
-
picocolors.red(
|
|
665
|
-
"Error: budgetLimit must be a valid number.",
|
|
666
|
-
),
|
|
667
|
-
);
|
|
668
|
-
} else {
|
|
669
|
-
const testConfig = JSON.parse(JSON.stringify(activeConfig));
|
|
670
|
-
this.setNestedProperty(testConfig, choice, numVal);
|
|
671
|
-
const parseResult = ConfigSchema.safeParse(testConfig);
|
|
672
|
-
if (parseResult.success) {
|
|
673
|
-
this.setNestedProperty(activeConfig, choice, numVal);
|
|
674
|
-
console.log(
|
|
675
|
-
picocolors.green(`✔ Updated "${choice}" to: ${numVal}`),
|
|
676
|
-
);
|
|
677
|
-
} else {
|
|
678
|
-
console.log(
|
|
679
|
-
picocolors.red(
|
|
680
|
-
`Validation error: ${parseResult.error.message}`,
|
|
681
|
-
),
|
|
682
|
-
);
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
} else if (Array.isArray(currentVal)) {
|
|
687
|
-
const nextValStr = await Prompt.askText(
|
|
688
|
-
`Enter comma-separated values for ${choice}:`,
|
|
689
|
-
currentVal.join(", "),
|
|
690
|
-
);
|
|
691
|
-
if (nextValStr !== null && nextValStr !== "") {
|
|
692
|
-
const arrVal = nextValStr
|
|
693
|
-
.split(",")
|
|
694
|
-
.map((s) => s.trim())
|
|
695
|
-
.filter(Boolean);
|
|
696
|
-
const testConfig = JSON.parse(JSON.stringify(activeConfig));
|
|
697
|
-
this.setNestedProperty(testConfig, choice, arrVal);
|
|
698
|
-
const parseResult = ConfigSchema.safeParse(testConfig);
|
|
699
|
-
if (parseResult.success) {
|
|
700
|
-
this.setNestedProperty(activeConfig, choice, arrVal);
|
|
701
|
-
console.log(
|
|
702
|
-
picocolors.green(
|
|
703
|
-
`✔ Updated "${choice}" to: [${arrVal.join(", ")}]`,
|
|
704
|
-
),
|
|
705
|
-
);
|
|
706
|
-
} else {
|
|
707
|
-
console.log(
|
|
708
|
-
picocolors.red(
|
|
709
|
-
`Validation error: ${parseResult.error.message}`,
|
|
710
|
-
),
|
|
711
|
-
);
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
} else if (
|
|
715
|
-
typeof currentVal === "string" &&
|
|
716
|
-
choice !== "permissions.mode"
|
|
717
|
-
) {
|
|
718
|
-
const nextValStr = await Prompt.askText(
|
|
719
|
-
`Enter value for ${choice}:`,
|
|
720
|
-
String(currentVal),
|
|
721
|
-
);
|
|
722
|
-
if (nextValStr !== null && nextValStr !== "") {
|
|
723
|
-
const testConfig = JSON.parse(JSON.stringify(activeConfig));
|
|
724
|
-
this.setNestedProperty(testConfig, choice, nextValStr);
|
|
725
|
-
const parseResult = ConfigSchema.safeParse(testConfig);
|
|
726
|
-
if (parseResult.success) {
|
|
727
|
-
this.setNestedProperty(activeConfig, choice, nextValStr);
|
|
728
|
-
console.log(
|
|
729
|
-
picocolors.green(
|
|
730
|
-
`✔ Updated "${choice}" to: ${nextValStr}`,
|
|
731
|
-
),
|
|
732
|
-
);
|
|
733
|
-
} else {
|
|
734
|
-
console.log(
|
|
735
|
-
picocolors.red(
|
|
736
|
-
`Validation error: ${parseResult.error.message}`,
|
|
737
|
-
),
|
|
738
|
-
);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
} finally {
|
|
744
|
-
if (wasActive && !tui.isActive) {
|
|
745
|
-
tui.start(config.budgetLimit);
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
return { shouldExit: false, processed: true };
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
if (command === "/model") {
|
|
752
|
-
const modelArg = parts.slice(1).join(" ").trim();
|
|
753
|
-
const activeConfig = loop.getConfig();
|
|
754
|
-
if (!modelArg) {
|
|
755
|
-
const wasActive = useFullscreenTui && tui.isActive;
|
|
756
|
-
if (wasActive) tui.stop();
|
|
757
|
-
try {
|
|
758
|
-
const activeModel =
|
|
759
|
-
loop.getModelOverride() || activeConfig.models.default;
|
|
760
|
-
let modelOptions: Array<{ value: string; label: string }> = [];
|
|
761
|
-
const providerId = this.providerInstance.id;
|
|
762
|
-
|
|
763
|
-
if (providerId === "anthropic") {
|
|
764
|
-
modelOptions = [
|
|
765
|
-
{
|
|
766
|
-
value: "claude-3-5-sonnet-latest",
|
|
767
|
-
label:
|
|
768
|
-
"claude-3-5-sonnet-latest (Claude 3.5 Sonnet - Recommended)",
|
|
769
|
-
},
|
|
770
|
-
{
|
|
771
|
-
value: "claude-3-5-haiku-latest",
|
|
772
|
-
label: "claude-3-5-haiku-latest (Claude 3.5 Haiku)",
|
|
773
|
-
},
|
|
774
|
-
{
|
|
775
|
-
value: "claude-3-opus-latest",
|
|
776
|
-
label: "claude-3-opus-latest (Claude 3 Opus)",
|
|
777
|
-
},
|
|
778
|
-
];
|
|
779
|
-
} else if (providerId === "openai") {
|
|
780
|
-
modelOptions = [
|
|
781
|
-
{ value: "gpt-4o", label: "gpt-4o (GPT-4o - Recommended)" },
|
|
782
|
-
{ value: "gpt-4o-mini", label: "gpt-4o-mini (GPT-4o mini)" },
|
|
783
|
-
{ value: "o1", label: "o1 (o1 Reasoner)" },
|
|
784
|
-
{
|
|
785
|
-
value: "o3-mini",
|
|
786
|
-
label: "o3-mini (o3-mini Fast Reasoner)",
|
|
787
|
-
},
|
|
788
|
-
];
|
|
789
|
-
} else if (
|
|
790
|
-
providerId === "deepseek-openai" ||
|
|
791
|
-
providerId === "deepseek-anthropic"
|
|
792
|
-
) {
|
|
793
|
-
modelOptions = [
|
|
794
|
-
{
|
|
795
|
-
value: "deepseek-v4-flash",
|
|
796
|
-
label:
|
|
797
|
-
"deepseek-v4-flash (DeepSeek-V4 / Fast & Flash - Recommended)",
|
|
798
|
-
},
|
|
799
|
-
{
|
|
800
|
-
value: "deepseek-v4-pro",
|
|
801
|
-
label: "deepseek-v4-pro (DeepSeek-V4 / Advanced & Pro)",
|
|
802
|
-
},
|
|
803
|
-
{
|
|
804
|
-
value: "deepseek-chat",
|
|
805
|
-
label: "deepseek-chat (DeepSeek-V3 / Chat)",
|
|
806
|
-
},
|
|
807
|
-
{
|
|
808
|
-
value: "deepseek-reasoner",
|
|
809
|
-
label: "deepseek-reasoner (DeepSeek-R1 / Reasoner)",
|
|
810
|
-
},
|
|
811
|
-
];
|
|
812
|
-
} else if (providerId === "ollama") {
|
|
813
|
-
modelOptions = [
|
|
814
|
-
{ value: "qwen2.5-coder:7b", label: "qwen2.5-coder:7b" },
|
|
815
|
-
{ value: "qwen2.5-coder:1.5b", label: "qwen2.5-coder:1.5b" },
|
|
816
|
-
{ value: "llama3", label: "llama3" },
|
|
817
|
-
];
|
|
818
|
-
} else {
|
|
819
|
-
modelOptions = [
|
|
820
|
-
{
|
|
821
|
-
value: "deepseek-v4-flash",
|
|
822
|
-
label: "deepseek-v4-flash (DeepSeek-V4 / Fast & Flash)",
|
|
823
|
-
},
|
|
824
|
-
{
|
|
825
|
-
value: "deepseek-v4-pro",
|
|
826
|
-
label: "deepseek-v4-pro (DeepSeek-V4 / Advanced & Pro)",
|
|
827
|
-
},
|
|
828
|
-
{ value: "gpt-4o", label: "gpt-4o (GPT-4o)" },
|
|
829
|
-
{
|
|
830
|
-
value: "claude-3-5-sonnet-latest",
|
|
831
|
-
label: "claude-3-5-sonnet-latest (Claude 3.5 Sonnet)",
|
|
832
|
-
},
|
|
833
|
-
];
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
modelOptions.push({
|
|
837
|
-
value: "custom",
|
|
838
|
-
label: "Custom model name...",
|
|
839
|
-
});
|
|
840
|
-
modelOptions.push({ value: "cancel", label: "Cancel" });
|
|
841
|
-
|
|
842
|
-
const selectedModel = await Prompt.askSelect(
|
|
843
|
-
`Current model: ${activeModel}. Select a model to switch:`,
|
|
844
|
-
modelOptions,
|
|
845
|
-
);
|
|
846
|
-
if (!selectedModel || selectedModel === "cancel") {
|
|
847
|
-
return { shouldExit: false, processed: true };
|
|
848
|
-
}
|
|
849
|
-
let finalModel = selectedModel;
|
|
850
|
-
if (selectedModel === "custom") {
|
|
851
|
-
const customModel = await Prompt.askText(
|
|
852
|
-
"Enter custom model name:",
|
|
853
|
-
);
|
|
854
|
-
if (customModel) {
|
|
855
|
-
finalModel = customModel;
|
|
856
|
-
loop.setModelOverride(customModel);
|
|
857
|
-
if (wasActive && !tui.isActive) {
|
|
858
|
-
tui.start(config.budgetLimit);
|
|
859
|
-
}
|
|
860
|
-
this.printOutput(
|
|
861
|
-
`Switched active model to: ${picocolors.green(customModel)}`,
|
|
862
|
-
);
|
|
863
|
-
} else {
|
|
864
|
-
return { shouldExit: false, processed: true };
|
|
865
|
-
}
|
|
866
|
-
} else {
|
|
867
|
-
loop.setModelOverride(selectedModel);
|
|
868
|
-
if (wasActive && !tui.isActive) {
|
|
869
|
-
tui.start(config.budgetLimit);
|
|
870
|
-
}
|
|
871
|
-
this.printOutput(
|
|
872
|
-
`Switched active model to: ${picocolors.green(selectedModel)}`,
|
|
873
|
-
);
|
|
874
|
-
}
|
|
875
|
-
this.saveLocalState({ lastModel: finalModel });
|
|
876
|
-
} finally {
|
|
877
|
-
if (wasActive && !tui.isActive) {
|
|
878
|
-
tui.start(config.budgetLimit);
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
return { shouldExit: false, processed: true };
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
loop.setModelOverride(modelArg);
|
|
885
|
-
this.printOutput(
|
|
886
|
-
`Switched active model to: ${picocolors.green(modelArg)}`,
|
|
887
|
-
);
|
|
888
|
-
this.saveLocalState({ lastModel: modelArg });
|
|
889
|
-
return { shouldExit: false, processed: true };
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
if (command === "/commit") {
|
|
893
|
-
const commitMsg = parts.slice(1).join(" ").trim();
|
|
894
|
-
const isZh = config.language === "zh";
|
|
895
|
-
const { execSync } = await import("child_process");
|
|
896
|
-
try {
|
|
897
|
-
let diff = execSync("git diff --cached", { cwd }).toString().trim();
|
|
898
|
-
if (!diff) {
|
|
899
|
-
const unstaged = execSync("git status --porcelain", { cwd })
|
|
900
|
-
.toString()
|
|
901
|
-
.trim();
|
|
902
|
-
if (!unstaged) {
|
|
903
|
-
console.log(
|
|
904
|
-
picocolors.yellow(
|
|
905
|
-
isZh
|
|
906
|
-
? "工作区干净,没有检测到任何已暂存或未暂存的更改。"
|
|
907
|
-
: "Workspace clean. No staged or unstaged changes found to commit.",
|
|
908
|
-
),
|
|
909
|
-
);
|
|
910
|
-
return { shouldExit: false, processed: true };
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
const wasActive = useFullscreenTui && tui.isActive;
|
|
914
|
-
if (wasActive) tui.stop();
|
|
915
|
-
|
|
916
|
-
const autoStage = await Prompt.askApproval(
|
|
917
|
-
isZh
|
|
918
|
-
? "未检测到已暂存的修改,是否自动暂存工作区中的所有变更并生成提交?"
|
|
919
|
-
: "No staged changes found. Automatically stage all local changes and create a commit?",
|
|
920
|
-
);
|
|
921
|
-
|
|
922
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
923
|
-
|
|
924
|
-
if (!autoStage) {
|
|
925
|
-
console.log(
|
|
926
|
-
picocolors.yellow(
|
|
927
|
-
isZh
|
|
928
|
-
? "操作已取消。请先运行 'git add' 暂存你的修改。"
|
|
929
|
-
: "Operation cancelled. Please run 'git add' to stage your changes first.",
|
|
930
|
-
),
|
|
931
|
-
);
|
|
932
|
-
return { shouldExit: false, processed: true };
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
console.log(
|
|
936
|
-
isZh ? "正在暂存所有变更..." : "Staging all changes...",
|
|
937
|
-
);
|
|
938
|
-
execSync("git add -A", { cwd });
|
|
939
|
-
diff = execSync("git diff --cached", { cwd }).toString().trim();
|
|
940
|
-
if (!diff) {
|
|
941
|
-
console.log(
|
|
942
|
-
picocolors.red(
|
|
943
|
-
isZh
|
|
944
|
-
? "✖ 暂存失败或暂存后仍无变更。"
|
|
945
|
-
: "✖ Staging failed or resulted in no diff.",
|
|
946
|
-
),
|
|
947
|
-
);
|
|
948
|
-
return { shouldExit: false, processed: true };
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
let finalMsg = commitMsg;
|
|
953
|
-
if (!finalMsg) {
|
|
954
|
-
console.log("Generating commit message via LLM...");
|
|
955
|
-
const fastModel = config.models.fast || config.models.default;
|
|
956
|
-
const stream = this.providerInstance.chat({
|
|
957
|
-
model: fastModel,
|
|
958
|
-
messages: [
|
|
959
|
-
{
|
|
960
|
-
id: `msg_commit_cmd_${Date.now()}`,
|
|
961
|
-
role: "user",
|
|
962
|
-
createdAt: new Date().toISOString(),
|
|
963
|
-
content: [
|
|
964
|
-
{
|
|
965
|
-
type: "text",
|
|
966
|
-
text: `Generate a concise, high-quality conventional git commit message (e.g. feat(cli): add autocomplete) for the following git diff. Output ONLY the commit message, no formatting, no markdown, no quotes, just the text:\n\n${diff.substring(0, 20000)}`,
|
|
967
|
-
},
|
|
968
|
-
],
|
|
969
|
-
},
|
|
970
|
-
],
|
|
971
|
-
tools: [],
|
|
972
|
-
});
|
|
973
|
-
|
|
974
|
-
let generatedMessage = "";
|
|
975
|
-
for await (const event of stream) {
|
|
976
|
-
if (event.type === "text_delta") {
|
|
977
|
-
generatedMessage += event.text;
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
finalMsg = generatedMessage.trim().replace(/^["']|["']$/g, "");
|
|
981
|
-
if (!finalMsg) {
|
|
982
|
-
finalMsg = "chore: auto-commit";
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
console.log(
|
|
987
|
-
`Committing changes with message: "${picocolors.green(finalMsg)}"`,
|
|
988
|
-
);
|
|
989
|
-
const commitCmd = `git commit -m ${JSON.stringify(finalMsg)}`;
|
|
990
|
-
execSync(commitCmd, { cwd });
|
|
991
|
-
console.log(picocolors.green("✔ Git commit created successfully."));
|
|
992
|
-
} catch (err: any) {
|
|
993
|
-
console.log(picocolors.red(`✖ Commit failed: ${err.message}`));
|
|
994
|
-
}
|
|
995
|
-
return { shouldExit: false, processed: true };
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
if (command === "/add") {
|
|
1001
|
-
let fileArg = parts.slice(1).join(" ").trim();
|
|
1002
|
-
const isZh = config.language === "zh";
|
|
1003
|
-
const candidates = this.getCandidates();
|
|
1004
|
-
|
|
1005
|
-
let readOnly = false;
|
|
1006
|
-
if (
|
|
1007
|
-
fileArg.startsWith("--read-only ") ||
|
|
1008
|
-
fileArg.startsWith("--readonly ") ||
|
|
1009
|
-
fileArg.startsWith("-r ")
|
|
1010
|
-
) {
|
|
1011
|
-
readOnly = true;
|
|
1012
|
-
fileArg = fileArg.replace(/^(--read-only|--readonly|-r)\s+/, "").trim();
|
|
1013
|
-
} else if (
|
|
1014
|
-
fileArg === "--read-only" ||
|
|
1015
|
-
fileArg === "--readonly" ||
|
|
1016
|
-
fileArg === "-r"
|
|
1017
|
-
) {
|
|
1018
|
-
readOnly = true;
|
|
1019
|
-
fileArg = "";
|
|
1020
|
-
}
|
|
1021
|
-
|
|
1022
|
-
if (!fileArg) {
|
|
1023
|
-
const wasActive = useFullscreenTui && tui.isActive;
|
|
1024
|
-
if (wasActive) tui.stop();
|
|
1025
|
-
|
|
1026
|
-
try {
|
|
1027
|
-
if (
|
|
1028
|
-
!candidates ||
|
|
1029
|
-
!candidates.files ||
|
|
1030
|
-
candidates.files.length === 0
|
|
1031
|
-
) {
|
|
1032
|
-
console.log(
|
|
1033
|
-
isZh
|
|
1034
|
-
? picocolors.yellow("工作区未找到可添加的文件。")
|
|
1035
|
-
: picocolors.yellow(
|
|
1036
|
-
"No files found in the workspace to add.",
|
|
1037
|
-
),
|
|
1038
|
-
);
|
|
1039
|
-
} else {
|
|
1040
|
-
const filterQuery = await Prompt.askText(
|
|
1041
|
-
isZh
|
|
1042
|
-
? "输入文件名过滤词(支持模糊匹配,直接回车列出所有):"
|
|
1043
|
-
: "Enter filename filter query (fuzzy, press Enter for all):",
|
|
1044
|
-
);
|
|
1045
|
-
|
|
1046
|
-
if (filterQuery === null) {
|
|
1047
|
-
console.log(
|
|
1048
|
-
isZh
|
|
1049
|
-
? picocolors.yellow("操作已取消。")
|
|
1050
|
-
: picocolors.yellow("Operation cancelled."),
|
|
1051
|
-
);
|
|
1052
|
-
return { shouldExit: false, processed: true };
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
let filtered = candidates.files;
|
|
1056
|
-
if (filterQuery.trim()) {
|
|
1057
|
-
const q = filterQuery.trim().toLowerCase();
|
|
1058
|
-
filtered = candidates.files.filter((f: string) =>
|
|
1059
|
-
f.toLowerCase().includes(q),
|
|
1060
|
-
);
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
if (filtered.length === 0) {
|
|
1064
|
-
console.log(
|
|
1065
|
-
isZh
|
|
1066
|
-
? picocolors.yellow("未找到匹配过滤词的文件。")
|
|
1067
|
-
: picocolors.yellow("No matching files found."),
|
|
1068
|
-
);
|
|
1069
|
-
} else {
|
|
1070
|
-
const options = filtered.map((f: string) => ({ value: f, label: f }));
|
|
1071
|
-
const selected = await Prompt.askMultiSelect(
|
|
1072
|
-
isZh
|
|
1073
|
-
? readOnly
|
|
1074
|
-
? "选择要添加到上下文的只读参考文件:"
|
|
1075
|
-
: "选择要添加到上下文的文件:"
|
|
1076
|
-
: readOnly
|
|
1077
|
-
? "Select files to add as read-only reference context:"
|
|
1078
|
-
: "Select files to add to the context:",
|
|
1079
|
-
options,
|
|
1080
|
-
);
|
|
1081
|
-
if (selected && selected.length > 0) {
|
|
1082
|
-
for (const f of selected) {
|
|
1083
|
-
if (readOnly) {
|
|
1084
|
-
loop.addReadOnlyFilePublic(
|
|
1085
|
-
f,
|
|
1086
|
-
"Manually added via interactive /add --read-only",
|
|
1087
|
-
);
|
|
1088
|
-
} else {
|
|
1089
|
-
loop.addRelevantFilePublic(
|
|
1090
|
-
f,
|
|
1091
|
-
"Manually added via interactive /add",
|
|
1092
|
-
);
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
console.log(
|
|
1096
|
-
isZh
|
|
1097
|
-
? readOnly
|
|
1098
|
-
? picocolors.green(
|
|
1099
|
-
`✔ 成功添加 ${selected.length} 个只读文件到上下文。`,
|
|
1100
|
-
)
|
|
1101
|
-
: picocolors.green(
|
|
1102
|
-
`✔ 成功添加 ${selected.length} 个文件到上下文。`,
|
|
1103
|
-
)
|
|
1104
|
-
: readOnly
|
|
1105
|
-
? picocolors.green(
|
|
1106
|
-
`✔ Added ${selected.length} read-only file(s) to active context.`,
|
|
1107
|
-
)
|
|
1108
|
-
: picocolors.green(
|
|
1109
|
-
`✔ Added ${selected.length} file(s) to active context.`,
|
|
1110
|
-
),
|
|
1111
|
-
);
|
|
1112
|
-
} else {
|
|
1113
|
-
console.log(
|
|
1114
|
-
isZh
|
|
1115
|
-
? picocolors.yellow("未选择任何文件。")
|
|
1116
|
-
: picocolors.yellow("No files selected."),
|
|
1117
|
-
);
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
} catch (err: any) {
|
|
1122
|
-
console.log(
|
|
1123
|
-
isZh
|
|
1124
|
-
? picocolors.red(`选择文件失败: ${err.message}`)
|
|
1125
|
-
: picocolors.red(`Failed to select files: ${err.message}`),
|
|
1126
|
-
);
|
|
1127
|
-
} finally {
|
|
1128
|
-
tui.syncFromLoop(loop);
|
|
1129
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
1130
|
-
}
|
|
1131
|
-
return { shouldExit: false, processed: true };
|
|
1132
|
-
}
|
|
1133
|
-
|
|
1134
|
-
const { isAbsolute, relative, resolve: pathResolve } = await import("path");
|
|
1135
|
-
const { statSync } = await import("fs");
|
|
1136
|
-
const absPath = isAbsolute(fileArg) ? fileArg : pathResolve(cwd, fileArg);
|
|
1137
|
-
const relPath = relative(cwd, absPath).replace(/\\/g, "/");
|
|
1138
|
-
|
|
1139
|
-
if (!existsSync(absPath)) {
|
|
1140
|
-
const matched = (candidates?.files || []).filter(
|
|
1141
|
-
(f: string) =>
|
|
1142
|
-
f.toLowerCase().includes(fileArg.toLowerCase()) ||
|
|
1143
|
-
f.toLowerCase().endsWith("/" + fileArg.toLowerCase()),
|
|
1144
|
-
);
|
|
1145
|
-
if (matched.length === 1) {
|
|
1146
|
-
if (readOnly) {
|
|
1147
|
-
loop.addReadOnlyFilePublic(
|
|
1148
|
-
matched[0],
|
|
1149
|
-
"Fuzzy matched via /add --read-only",
|
|
1150
|
-
);
|
|
1151
|
-
console.log(
|
|
1152
|
-
isZh
|
|
1153
|
-
? picocolors.green(`✔ 自动匹配并添加只读文件: ${matched[0]}`)
|
|
1154
|
-
: picocolors.green(
|
|
1155
|
-
`✔ Auto-matched and added read-only file: ${matched[0]}`,
|
|
1156
|
-
),
|
|
1157
|
-
);
|
|
1158
|
-
} else {
|
|
1159
|
-
loop.addRelevantFilePublic(matched[0], "Fuzzy matched via /add");
|
|
1160
|
-
console.log(
|
|
1161
|
-
isZh
|
|
1162
|
-
? picocolors.green(`✔ 自动匹配并添加文件: ${matched[0]}`)
|
|
1163
|
-
: picocolors.green(
|
|
1164
|
-
`✔ Auto-matched and added file: ${matched[0]}`,
|
|
1165
|
-
),
|
|
1166
|
-
);
|
|
1167
|
-
}
|
|
1168
|
-
tui.syncFromLoop(loop);
|
|
1169
|
-
return { shouldExit: false, processed: true };
|
|
1170
|
-
} else if (matched.length > 1) {
|
|
1171
|
-
console.log(
|
|
1172
|
-
isZh
|
|
1173
|
-
? picocolors.yellow(
|
|
1174
|
-
`找到多个匹配文件,请精确输入路径或使用无参交互选择:\n${matched.map((m: string) => ` • ${m}`).join("\n")}`,
|
|
1175
|
-
)
|
|
1176
|
-
: picocolors.yellow(
|
|
1177
|
-
`Multiple matches found, please specify or use interactive select:\n${matched.map((m: string) => ` • ${m}`).join("\n")}`,
|
|
1178
|
-
),
|
|
1179
|
-
);
|
|
1180
|
-
return { shouldExit: false, processed: true };
|
|
1181
|
-
}
|
|
1182
|
-
console.log(
|
|
1183
|
-
isZh
|
|
1184
|
-
? picocolors.red(`文件不存在: ${fileArg}`)
|
|
1185
|
-
: picocolors.red(`File does not exist: ${fileArg}`),
|
|
1186
|
-
);
|
|
1187
|
-
return { shouldExit: false, processed: true };
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
try {
|
|
1191
|
-
const stat = statSync(absPath);
|
|
1192
|
-
if (stat.isDirectory()) {
|
|
1193
|
-
const files = await glob("**/*", {
|
|
1194
|
-
cwd: absPath,
|
|
1195
|
-
onlyFiles: true,
|
|
1196
|
-
suppressErrors: true,
|
|
1197
|
-
});
|
|
1198
|
-
for (const f of files) {
|
|
1199
|
-
const subRelPath = join(relPath, f).replace(/\\/g, "/");
|
|
1200
|
-
if (readOnly) {
|
|
1201
|
-
loop.addReadOnlyFilePublic(
|
|
1202
|
-
subRelPath,
|
|
1203
|
-
"Manually added directory via /add --read-only",
|
|
1204
|
-
);
|
|
1205
|
-
} else {
|
|
1206
|
-
loop.addRelevantFilePublic(
|
|
1207
|
-
subRelPath,
|
|
1208
|
-
"Manually added directory via /add",
|
|
1209
|
-
);
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
console.log(
|
|
1213
|
-
isZh
|
|
1214
|
-
? readOnly
|
|
1215
|
-
? picocolors.green(
|
|
1216
|
-
`✔ 成功添加目录 ${relPath} 下的所有只读文件到上下文。`,
|
|
1217
|
-
)
|
|
1218
|
-
: picocolors.green(
|
|
1219
|
-
`✔ 成功添加目录 ${relPath} 下的所有文件到上下文。`,
|
|
1220
|
-
)
|
|
1221
|
-
: readOnly
|
|
1222
|
-
? picocolors.green(
|
|
1223
|
-
`✔ Added all files in directory ${relPath} as read-only to active context.`,
|
|
1224
|
-
)
|
|
1225
|
-
: picocolors.green(
|
|
1226
|
-
`✔ Added all files in directory ${relPath} to active context.`,
|
|
1227
|
-
),
|
|
1228
|
-
);
|
|
1229
|
-
} else {
|
|
1230
|
-
if (readOnly) {
|
|
1231
|
-
loop.addReadOnlyFilePublic(
|
|
1232
|
-
relPath,
|
|
1233
|
-
"Manually added file via /add --read-only",
|
|
1234
|
-
);
|
|
1235
|
-
console.log(
|
|
1236
|
-
isZh
|
|
1237
|
-
? picocolors.green(`✔ 已将只读文件 ${relPath} 添加到上下文。`)
|
|
1238
|
-
: picocolors.green(
|
|
1239
|
-
`✔ Added read-only file ${relPath} to active context.`,
|
|
1240
|
-
),
|
|
1241
|
-
);
|
|
1242
|
-
} else {
|
|
1243
|
-
loop.addRelevantFilePublic(
|
|
1244
|
-
relPath,
|
|
1245
|
-
"Manually added file via /add",
|
|
1246
|
-
);
|
|
1247
|
-
console.log(
|
|
1248
|
-
isZh
|
|
1249
|
-
? picocolors.green(`✔ 已将 ${relPath} 添加到上下文。`)
|
|
1250
|
-
: picocolors.green(`✔ Added ${relPath} to active context.`),
|
|
1251
|
-
);
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
tui.syncFromLoop(loop);
|
|
1255
|
-
} catch (err: any) {
|
|
1256
|
-
console.log(
|
|
1257
|
-
isZh
|
|
1258
|
-
? picocolors.red(`添加失败: ${err.message}`)
|
|
1259
|
-
: picocolors.red(`Failed to add: ${err.message}`),
|
|
1260
|
-
);
|
|
1261
|
-
}
|
|
1262
|
-
return { shouldExit: false, processed: true };
|
|
1263
|
-
}
|
|
1264
|
-
|
|
1265
|
-
if (command === "/drop") {
|
|
1266
|
-
const fileArg = parts.slice(1).join(" ").trim();
|
|
1267
|
-
const isZh = config.language === "zh";
|
|
1268
|
-
if (!fileArg) {
|
|
1269
|
-
const wasActive = useFullscreenTui && tui.isActive;
|
|
1270
|
-
if (wasActive) tui.stop();
|
|
1271
|
-
|
|
1272
|
-
try {
|
|
1273
|
-
const activeFiles = loop.getRelevantFiles();
|
|
1274
|
-
if (activeFiles.length === 0) {
|
|
1275
|
-
console.log(
|
|
1276
|
-
isZh
|
|
1277
|
-
? picocolors.yellow("当前活动上下文为空,无可移除的文件。")
|
|
1278
|
-
: picocolors.yellow(
|
|
1279
|
-
"Active context is empty, no files to remove.",
|
|
1280
|
-
),
|
|
1281
|
-
);
|
|
1282
|
-
} else {
|
|
1283
|
-
const options = activeFiles.map((f) => ({
|
|
1284
|
-
value: f.path,
|
|
1285
|
-
label: f.path,
|
|
1286
|
-
}));
|
|
1287
|
-
const selected = await Prompt.askMultiSelect(
|
|
1288
|
-
isZh
|
|
1289
|
-
? "选择要从上下文中移除的文件:"
|
|
1290
|
-
: "Select files to remove from the context:",
|
|
1291
|
-
options,
|
|
1292
|
-
);
|
|
1293
|
-
if (selected && selected.length > 0) {
|
|
1294
|
-
for (const f of selected) {
|
|
1295
|
-
loop.removeRelevantFilePublic(f);
|
|
1296
|
-
}
|
|
1297
|
-
console.log(
|
|
1298
|
-
isZh
|
|
1299
|
-
? picocolors.green(
|
|
1300
|
-
`✔ 成功从上下文中移除 ${selected.length} 个文件。`,
|
|
1301
|
-
)
|
|
1302
|
-
: picocolors.green(
|
|
1303
|
-
`✔ Removed ${selected.length} file(s) from active context.`,
|
|
1304
|
-
),
|
|
1305
|
-
);
|
|
1306
|
-
} else {
|
|
1307
|
-
console.log(
|
|
1308
|
-
isZh
|
|
1309
|
-
? picocolors.yellow("未选择任何文件。")
|
|
1310
|
-
: picocolors.yellow("No files selected."),
|
|
1311
|
-
);
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
} catch (err: any) {
|
|
1315
|
-
console.log(
|
|
1316
|
-
isZh
|
|
1317
|
-
? picocolors.red(`移除文件失败: ${err.message}`)
|
|
1318
|
-
: picocolors.red(`Failed to remove files: ${err.message}`),
|
|
1319
|
-
);
|
|
1320
|
-
} finally {
|
|
1321
|
-
tui.syncFromLoop(loop);
|
|
1322
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
1323
|
-
}
|
|
1324
|
-
return { shouldExit: false, processed: true };
|
|
1325
|
-
}
|
|
1326
|
-
|
|
1327
|
-
if (fileArg === "all" || fileArg === "*") {
|
|
1328
|
-
loop.clearRelevantFilesPublic();
|
|
1329
|
-
tui.syncFromLoop(loop);
|
|
1330
|
-
console.log(
|
|
1331
|
-
isZh
|
|
1332
|
-
? picocolors.green(`✔ 已从上下文中清空所有文件。`)
|
|
1333
|
-
: picocolors.green(`✔ Cleared all files from active context.`),
|
|
1334
|
-
);
|
|
1335
|
-
return { shouldExit: false, processed: true };
|
|
1336
|
-
}
|
|
1337
|
-
|
|
1338
|
-
const { isAbsolute, relative, resolve: pathResolve } = await import("path");
|
|
1339
|
-
const absPath = isAbsolute(fileArg) ? fileArg : pathResolve(cwd, fileArg);
|
|
1340
|
-
const relPath = relative(cwd, absPath).replace(/\\/g, "/");
|
|
1341
|
-
|
|
1342
|
-
const beforeCount = loop.getRelevantFiles().length;
|
|
1343
|
-
loop.removeRelevantFilePublic(relPath);
|
|
1344
|
-
|
|
1345
|
-
// Glob/regex fallback for dropping files by pattern
|
|
1346
|
-
const activeFiles = loop.getRelevantFiles().map((f) => f.path);
|
|
1347
|
-
const escaped = fileArg.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
1348
|
-
const globRegexStr = escaped.replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
1349
|
-
const rx = new RegExp("^" + globRegexStr + "$", "i");
|
|
1350
|
-
|
|
1351
|
-
for (const f of activeFiles) {
|
|
1352
|
-
if (rx.test(f) || f.startsWith(relPath)) {
|
|
1353
|
-
loop.removeRelevantFilePublic(f);
|
|
1354
|
-
}
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
tui.syncFromLoop(loop);
|
|
1358
|
-
const afterCount = loop.getRelevantFiles().length;
|
|
1359
|
-
const droppedCount = beforeCount - afterCount;
|
|
1360
|
-
if (droppedCount > 0) {
|
|
1361
|
-
console.log(
|
|
1362
|
-
isZh
|
|
1363
|
-
? picocolors.green(
|
|
1364
|
-
`✔ 从上下文中成功移除 ${droppedCount} 个文件。`,
|
|
1365
|
-
)
|
|
1366
|
-
: picocolors.green(
|
|
1367
|
-
`✔ Removed ${droppedCount} file(s) from active context.`,
|
|
1368
|
-
),
|
|
1369
|
-
);
|
|
1370
|
-
} else {
|
|
1371
|
-
console.log(
|
|
1372
|
-
isZh
|
|
1373
|
-
? picocolors.yellow(`上下文中未找到匹配 "${fileArg}" 的文件。`)
|
|
1374
|
-
: picocolors.yellow(
|
|
1375
|
-
`No files matching "${fileArg}" were found in active context.`,
|
|
1376
|
-
),
|
|
1377
|
-
);
|
|
1378
|
-
}
|
|
1379
|
-
return { shouldExit: false, processed: true };
|
|
1380
|
-
}
|
|
1381
|
-
|
|
1382
|
-
if (command === "/clear") {
|
|
1383
|
-
console.clear();
|
|
1384
|
-
return { shouldExit: false, processed: true };
|
|
1385
|
-
}
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
if (command === "/chat") {
|
|
1390
|
-
const wasActive = useFullscreenTui && tui.isActive;
|
|
1391
|
-
let stoppedTui = false;
|
|
1392
|
-
const stopTuiIfNeeded = () => {
|
|
1393
|
-
if (wasActive && !stoppedTui) {
|
|
1394
|
-
tui.stop();
|
|
1395
|
-
stoppedTui = true;
|
|
1396
|
-
}
|
|
1397
|
-
};
|
|
1398
|
-
const restoreTuiAndPrint = (msg: string) => {
|
|
1399
|
-
if (wasActive && stoppedTui && !tui.isActive) {
|
|
1400
|
-
tui.start(config.budgetLimit);
|
|
1401
|
-
stoppedTui = false;
|
|
1402
|
-
}
|
|
1403
|
-
this.printOutput(msg);
|
|
1404
|
-
};
|
|
1405
|
-
|
|
1406
|
-
try {
|
|
1407
|
-
const subCommand = parts[1]?.toLowerCase();
|
|
1408
|
-
const arg = parts.slice(2).join(" ").trim();
|
|
1409
|
-
|
|
1410
|
-
const sessions = loop.getSessions();
|
|
1411
|
-
|
|
1412
|
-
// Function to delete session and adjust active session if needed
|
|
1413
|
-
const handleDelete = (idToDelete: string) => {
|
|
1414
|
-
loop.deleteSession(idToDelete);
|
|
1415
|
-
restoreTuiAndPrint(
|
|
1416
|
-
picocolors.green(
|
|
1417
|
-
`✔ Session ${idToDelete} deleted successfully.`,
|
|
1418
|
-
),
|
|
1419
|
-
);
|
|
1420
|
-
|
|
1421
|
-
// If deleted the current session, switch to the most recent one remaining, or a new one
|
|
1422
|
-
const activeSession =
|
|
1423
|
-
(loop as any).state?.sessionId ||
|
|
1424
|
-
(loop as any).sessionManager.getActiveSession()?.id;
|
|
1425
|
-
if (activeSession === idToDelete) {
|
|
1426
|
-
const remaining = loop.getSessions();
|
|
1427
|
-
if (remaining.length > 0) {
|
|
1428
|
-
const targetSession = remaining[0];
|
|
1429
|
-
const success = loop.resumeSession(targetSession.id);
|
|
1430
|
-
if (success) {
|
|
1431
|
-
tui.loadHistory(loop.getHistory());
|
|
1432
|
-
restoreTuiAndPrint(
|
|
1433
|
-
picocolors.green(
|
|
1434
|
-
`✔ Automatically switched to session: ${targetSession.id}`,
|
|
1435
|
-
),
|
|
1436
|
-
);
|
|
1437
|
-
this.saveLocalState({
|
|
1438
|
-
lastSessionId: targetSession.id,
|
|
1439
|
-
lastModel:
|
|
1440
|
-
loop.getModelOverride() || config.models.default,
|
|
1441
|
-
});
|
|
1442
|
-
}
|
|
1443
|
-
} else {
|
|
1444
|
-
// No sessions left, start a new one
|
|
1445
|
-
const activeModel =
|
|
1446
|
-
loop.getModelOverride() || config.models.default;
|
|
1447
|
-
const newSessionId = loop.startNewSession(
|
|
1448
|
-
this.providerInstance.id,
|
|
1449
|
-
activeModel,
|
|
1450
|
-
);
|
|
1451
|
-
tui.loadHistory([]);
|
|
1452
|
-
restoreTuiAndPrint(
|
|
1453
|
-
picocolors.green(
|
|
1454
|
-
`✔ Automatically started new session: ${newSessionId}`,
|
|
1455
|
-
),
|
|
1456
|
-
);
|
|
1457
|
-
this.saveLocalState({
|
|
1458
|
-
lastSessionId: newSessionId,
|
|
1459
|
-
lastModel: activeModel,
|
|
1460
|
-
});
|
|
1461
|
-
}
|
|
1462
|
-
}
|
|
1463
|
-
};
|
|
1464
|
-
|
|
1465
|
-
// CLI subcommand: /chat list / ls
|
|
1466
|
-
if (subCommand === "list" || subCommand === "ls") {
|
|
1467
|
-
if (sessions.length === 0) {
|
|
1468
|
-
restoreTuiAndPrint(
|
|
1469
|
-
picocolors.yellow("No active or saved sessions found."),
|
|
1470
|
-
);
|
|
1471
|
-
} else {
|
|
1472
|
-
let listMsg = picocolors.bold(
|
|
1473
|
-
picocolors.cyan("\n=== Orbit Saved Sessions ===\n\n"),
|
|
1474
|
-
);
|
|
1475
|
-
const activeSessionId =
|
|
1476
|
-
(loop as any).state?.sessionId ||
|
|
1477
|
-
(loop as any).sessionManager.getActiveSession()?.id;
|
|
1478
|
-
sessions.forEach((s: any, idx: number) => {
|
|
1479
|
-
const formattedDate = new Date(s.createdAt).toLocaleString();
|
|
1480
|
-
const isActive = s.id === activeSessionId;
|
|
1481
|
-
const prefixStr = isActive
|
|
1482
|
-
? picocolors.green("● (active)")
|
|
1483
|
-
: " ";
|
|
1484
|
-
listMsg += ` ${prefixStr} [${idx + 1}] ${picocolors.blue(s.id)} - ${s.title || "Untitled"} (${formattedDate}) [${s.model}]\n`;
|
|
1485
|
-
});
|
|
1486
|
-
listMsg += picocolors.cyan("============================\n");
|
|
1487
|
-
restoreTuiAndPrint(listMsg);
|
|
1488
|
-
}
|
|
1489
|
-
return { shouldExit: false, processed: true };
|
|
1490
|
-
}
|
|
1491
|
-
|
|
1492
|
-
// CLI subcommand: /chat delete / rm / del
|
|
1493
|
-
if (
|
|
1494
|
-
subCommand === "delete" ||
|
|
1495
|
-
subCommand === "rm" ||
|
|
1496
|
-
subCommand === "del"
|
|
1497
|
-
) {
|
|
1498
|
-
let idToDelete = arg;
|
|
1499
|
-
if (!idToDelete) {
|
|
1500
|
-
if (sessions.length === 0) {
|
|
1501
|
-
restoreTuiAndPrint(
|
|
1502
|
-
picocolors.yellow(
|
|
1503
|
-
"No active or saved sessions found to delete.",
|
|
1504
|
-
),
|
|
1505
|
-
);
|
|
1506
|
-
return { shouldExit: false, processed: true };
|
|
1507
|
-
}
|
|
1508
|
-
const deleteOptions = sessions.map((s: any) => {
|
|
1509
|
-
const formattedDate = new Date(s.createdAt).toLocaleString();
|
|
1510
|
-
return {
|
|
1511
|
-
value: s.id,
|
|
1512
|
-
label: `${s.id} - ${s.title || "Untitled"} (${formattedDate}) [${s.model}]`,
|
|
1513
|
-
};
|
|
1514
|
-
});
|
|
1515
|
-
deleteOptions.push({
|
|
1516
|
-
value: "cancel",
|
|
1517
|
-
label: "Cancel",
|
|
1518
|
-
});
|
|
1519
|
-
stopTuiIfNeeded();
|
|
1520
|
-
idToDelete = await Prompt.askSelect(
|
|
1521
|
-
"Choose a session to delete:",
|
|
1522
|
-
deleteOptions,
|
|
1523
|
-
);
|
|
1524
|
-
} else {
|
|
1525
|
-
// Check if index was provided instead of full id
|
|
1526
|
-
const idx = parseInt(idToDelete, 10);
|
|
1527
|
-
if (!isNaN(idx) && idx >= 1 && idx <= sessions.length) {
|
|
1528
|
-
idToDelete = sessions[idx - 1].id;
|
|
1529
|
-
} else {
|
|
1530
|
-
// check if it's a valid session ID
|
|
1531
|
-
const found = sessions.find((s: any) => s.id === idToDelete);
|
|
1532
|
-
if (!found) {
|
|
1533
|
-
restoreTuiAndPrint(
|
|
1534
|
-
picocolors.red(`✖ Session not found: ${idToDelete}`),
|
|
1535
|
-
);
|
|
1536
|
-
return { shouldExit: false, processed: true };
|
|
1537
|
-
}
|
|
1538
|
-
}
|
|
1539
|
-
}
|
|
1540
|
-
|
|
1541
|
-
if (!idToDelete || idToDelete === "cancel") {
|
|
1542
|
-
return { shouldExit: false, processed: true };
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
// confirm deletion only if no arg was specified
|
|
1546
|
-
let confirm = "yes";
|
|
1547
|
-
if (!arg) {
|
|
1548
|
-
stopTuiIfNeeded();
|
|
1549
|
-
confirm = await Prompt.askSelect(
|
|
1550
|
-
`Are you sure you want to delete session ${idToDelete}?`,
|
|
1551
|
-
[
|
|
1552
|
-
{ value: "yes", label: "Yes, delete it" },
|
|
1553
|
-
{ value: "no", label: "No, cancel" },
|
|
1554
|
-
],
|
|
1555
|
-
);
|
|
1556
|
-
}
|
|
1557
|
-
|
|
1558
|
-
if (confirm === "yes") {
|
|
1559
|
-
handleDelete(idToDelete);
|
|
1560
|
-
}
|
|
1561
|
-
return { shouldExit: false, processed: true };
|
|
1562
|
-
}
|
|
1563
|
-
|
|
1564
|
-
// CLI subcommand: /chat new / create
|
|
1565
|
-
if (subCommand === "new" || subCommand === "create") {
|
|
1566
|
-
const activeModel =
|
|
1567
|
-
loop.getModelOverride() || config.models.default;
|
|
1568
|
-
const newSessionId = loop.startNewSession(
|
|
1569
|
-
this.providerInstance.id,
|
|
1570
|
-
activeModel,
|
|
1571
|
-
);
|
|
1572
|
-
tui.loadHistory([]);
|
|
1573
|
-
restoreTuiAndPrint(
|
|
1574
|
-
picocolors.green(`✔ Started new session: ${newSessionId}`),
|
|
1575
|
-
);
|
|
1576
|
-
|
|
1577
|
-
this.saveLocalState({
|
|
1578
|
-
lastSessionId: newSessionId,
|
|
1579
|
-
lastModel: activeModel,
|
|
1580
|
-
});
|
|
1581
|
-
return { shouldExit: false, processed: true };
|
|
1582
|
-
}
|
|
1583
|
-
|
|
1584
|
-
// CLI subcommand: /chat switch / load
|
|
1585
|
-
if (
|
|
1586
|
-
subCommand === "switch" ||
|
|
1587
|
-
subCommand === "load" ||
|
|
1588
|
-
(subCommand &&
|
|
1589
|
-
(sessions.some((s: any) => s.id === subCommand) ||
|
|
1590
|
-
!isNaN(parseInt(subCommand, 10))))
|
|
1591
|
-
) {
|
|
1592
|
-
let targetId =
|
|
1593
|
-
subCommand === "switch" || subCommand === "load"
|
|
1594
|
-
? arg
|
|
1595
|
-
: subCommand;
|
|
1596
|
-
if (!targetId) {
|
|
1597
|
-
restoreTuiAndPrint(
|
|
1598
|
-
picocolors.yellow("Usage: /chat switch <session_id | index>"),
|
|
1599
|
-
);
|
|
1600
|
-
return { shouldExit: false, processed: true };
|
|
1601
|
-
}
|
|
1602
|
-
const idx = parseInt(targetId, 10);
|
|
1603
|
-
if (!isNaN(idx) && idx >= 1 && idx <= sessions.length) {
|
|
1604
|
-
targetId = sessions[idx - 1].id;
|
|
1605
|
-
}
|
|
1606
|
-
const found = sessions.find((s: any) => s.id === targetId);
|
|
1607
|
-
if (!found) {
|
|
1608
|
-
restoreTuiAndPrint(
|
|
1609
|
-
picocolors.red(`✖ Session not found: ${targetId}`),
|
|
1610
|
-
);
|
|
1611
|
-
return { shouldExit: false, processed: true };
|
|
1612
|
-
}
|
|
1613
|
-
|
|
1614
|
-
const success = loop.resumeSession(targetId);
|
|
1615
|
-
if (success) {
|
|
1616
|
-
tui.loadHistory(loop.getHistory());
|
|
1617
|
-
restoreTuiAndPrint(
|
|
1618
|
-
picocolors.green(`✔ Switched to session: ${targetId}`),
|
|
1619
|
-
);
|
|
1620
|
-
|
|
1621
|
-
this.saveLocalState({
|
|
1622
|
-
lastSessionId: targetId,
|
|
1623
|
-
lastModel: loop.getModelOverride() || config.models.default,
|
|
1624
|
-
});
|
|
1625
|
-
} else {
|
|
1626
|
-
restoreTuiAndPrint(
|
|
1627
|
-
picocolors.red(`✖ Failed to resume session: ${targetId}`),
|
|
1628
|
-
);
|
|
1629
|
-
}
|
|
1630
|
-
return { shouldExit: false, processed: true };
|
|
1631
|
-
}
|
|
1632
|
-
|
|
1633
|
-
// Fallback: If no subcommands, show the interactive select menu
|
|
1634
|
-
if (sessions.length === 0) {
|
|
1635
|
-
// start a new session since none exist
|
|
1636
|
-
const activeModel =
|
|
1637
|
-
loop.getModelOverride() || config.models.default;
|
|
1638
|
-
const newSessionId = loop.startNewSession(
|
|
1639
|
-
this.providerInstance.id,
|
|
1640
|
-
activeModel,
|
|
1641
|
-
);
|
|
1642
|
-
tui.loadHistory([]);
|
|
1643
|
-
restoreTuiAndPrint(
|
|
1644
|
-
picocolors.green(`✔ Started new session: ${newSessionId}`),
|
|
1645
|
-
);
|
|
1646
|
-
this.saveLocalState({
|
|
1647
|
-
lastSessionId: newSessionId,
|
|
1648
|
-
lastModel: activeModel,
|
|
1649
|
-
});
|
|
1650
|
-
return { shouldExit: false, processed: true };
|
|
1651
|
-
}
|
|
1652
|
-
|
|
1653
|
-
const sessionOptions = sessions.map((s: any) => {
|
|
1654
|
-
const formattedDate = new Date(s.createdAt).toLocaleString();
|
|
1655
|
-
return {
|
|
1656
|
-
value: s.id,
|
|
1657
|
-
label: `${s.id} - ${s.title || "Untitled"} (${formattedDate}) [${s.model}]`,
|
|
1658
|
-
};
|
|
1659
|
-
});
|
|
1660
|
-
|
|
1661
|
-
sessionOptions.unshift({
|
|
1662
|
-
value: "new",
|
|
1663
|
-
label: picocolors.green("+ Start a new session"),
|
|
1664
|
-
});
|
|
1665
|
-
sessionOptions.unshift({
|
|
1666
|
-
value: "delete_menu",
|
|
1667
|
-
label: picocolors.red("- Delete a session..."),
|
|
1668
|
-
});
|
|
1669
|
-
sessionOptions.push({
|
|
1670
|
-
value: "cancel",
|
|
1671
|
-
label: "Cancel",
|
|
1672
|
-
});
|
|
1673
|
-
|
|
1674
|
-
const selectedSessionId = await Prompt.askSelect(
|
|
1675
|
-
"Choose a session to load:",
|
|
1676
|
-
sessionOptions,
|
|
1677
|
-
);
|
|
1678
|
-
|
|
1679
|
-
if (!selectedSessionId || selectedSessionId === "cancel") {
|
|
1680
|
-
return { shouldExit: false, processed: true };
|
|
1681
|
-
}
|
|
1682
|
-
|
|
1683
|
-
if (selectedSessionId === "delete_menu") {
|
|
1684
|
-
const deleteOptions = sessions.map((s: any) => {
|
|
1685
|
-
const formattedDate = new Date(s.createdAt).toLocaleString();
|
|
1686
|
-
return {
|
|
1687
|
-
value: s.id,
|
|
1688
|
-
label: `${s.id} - ${s.title || "Untitled"} (${formattedDate}) [${s.model}]`,
|
|
1689
|
-
};
|
|
1690
|
-
});
|
|
1691
|
-
deleteOptions.push({
|
|
1692
|
-
value: "cancel",
|
|
1693
|
-
label: "Cancel",
|
|
1694
|
-
});
|
|
1695
|
-
const idToDelete = await Prompt.askSelect(
|
|
1696
|
-
"Choose a session to delete:",
|
|
1697
|
-
deleteOptions,
|
|
1698
|
-
);
|
|
1699
|
-
|
|
1700
|
-
if (idToDelete && idToDelete !== "cancel") {
|
|
1701
|
-
const confirm = await Prompt.askSelect(
|
|
1702
|
-
`Are you sure you want to delete session ${idToDelete}?`,
|
|
1703
|
-
[
|
|
1704
|
-
{ value: "yes", label: "Yes, delete it" },
|
|
1705
|
-
{ value: "no", label: "No, cancel" },
|
|
1706
|
-
],
|
|
1707
|
-
);
|
|
1708
|
-
|
|
1709
|
-
if (confirm === "yes") {
|
|
1710
|
-
handleDelete(idToDelete);
|
|
1711
|
-
}
|
|
1712
|
-
}
|
|
1713
|
-
} else if (selectedSessionId === "new") {
|
|
1714
|
-
const activeModel =
|
|
1715
|
-
loop.getModelOverride() || config.models.default;
|
|
1716
|
-
const newSessionId = loop.startNewSession(
|
|
1717
|
-
this.providerInstance.id,
|
|
1718
|
-
activeModel,
|
|
1719
|
-
);
|
|
1720
|
-
tui.loadHistory([]);
|
|
1721
|
-
console.log(
|
|
1722
|
-
picocolors.green(`✔ Started new session: ${newSessionId}`),
|
|
1723
|
-
);
|
|
1724
|
-
|
|
1725
|
-
this.saveLocalState({
|
|
1726
|
-
lastSessionId: newSessionId,
|
|
1727
|
-
lastModel: activeModel,
|
|
1728
|
-
});
|
|
1729
|
-
} else {
|
|
1730
|
-
const success = loop.resumeSession(selectedSessionId);
|
|
1731
|
-
if (success) {
|
|
1732
|
-
tui.loadHistory(loop.getHistory());
|
|
1733
|
-
console.log(
|
|
1734
|
-
picocolors.green(
|
|
1735
|
-
`✔ Switched to session: ${selectedSessionId}`,
|
|
1736
|
-
),
|
|
1737
|
-
);
|
|
1738
|
-
|
|
1739
|
-
this.saveLocalState({
|
|
1740
|
-
lastSessionId: selectedSessionId,
|
|
1741
|
-
lastModel: loop.getModelOverride() || config.models.default,
|
|
1742
|
-
});
|
|
1743
|
-
} else {
|
|
1744
|
-
console.log(
|
|
1745
|
-
picocolors.red(
|
|
1746
|
-
`Failed to resume session: ${selectedSessionId}`,
|
|
1747
|
-
),
|
|
1748
|
-
);
|
|
1749
|
-
}
|
|
1750
|
-
}
|
|
1751
|
-
} finally {
|
|
1752
|
-
try {
|
|
1753
|
-
tui.setCandidates(await getAutocompleteCandidates(cwd, config));
|
|
1754
|
-
} catch {}
|
|
1755
|
-
if (wasActive) tui.start(config.budgetLimit);
|
|
1756
|
-
}
|
|
1757
|
-
return { shouldExit: false, processed: true };
|
|
1758
|
-
}
|
|
1759
|
-
|
|
1760
|
-
if (command === "/mode") {
|
|
1761
|
-
const isZh = config.language === "zh";
|
|
1762
|
-
const targetMode = parts.slice(1).join(" ").trim().toLowerCase();
|
|
1763
|
-
|
|
1764
|
-
if (!targetMode) {
|
|
1765
|
-
console.log(
|
|
1766
|
-
isZh
|
|
1767
|
-
? picocolors.yellow("用法: /mode <strict|normal|auto|plan>")
|
|
1768
|
-
: picocolors.yellow("Usage: /mode <strict|normal|auto|plan>"),
|
|
1769
|
-
);
|
|
1770
|
-
return { shouldExit: false, processed: true };
|
|
1771
|
-
}
|
|
1772
|
-
|
|
1773
|
-
const validModes = ["strict", "normal", "auto", "plan"];
|
|
1774
|
-
if (!validModes.includes(targetMode)) {
|
|
1775
|
-
console.log(
|
|
1776
|
-
isZh
|
|
1777
|
-
? picocolors.red(
|
|
1778
|
-
`✖ 无效的安全模式: ${targetMode}。可选模式: ${validModes.join(", ")}`,
|
|
1779
|
-
)
|
|
1780
|
-
: picocolors.red(
|
|
1781
|
-
`✖ Invalid security mode: ${targetMode}. Valid modes: ${validModes.join(", ")}`,
|
|
1782
|
-
),
|
|
1783
|
-
);
|
|
1784
|
-
return { shouldExit: false, processed: true };
|
|
1785
|
-
}
|
|
1786
|
-
|
|
1787
|
-
loop.getConfig().permissions.mode = targetMode as any;
|
|
1788
|
-
console.log(
|
|
1789
|
-
isZh
|
|
1790
|
-
? picocolors.green(
|
|
1791
|
-
`✔ 已切换安全模式至: ${targetMode.toUpperCase()}`,
|
|
1792
|
-
)
|
|
1793
|
-
: picocolors.green(
|
|
1794
|
-
`✔ Switched security mode to: ${targetMode.toUpperCase()}`,
|
|
1795
|
-
),
|
|
1796
|
-
);
|
|
1797
|
-
tui.syncFromLoop(loop);
|
|
1798
|
-
return { shouldExit: false, processed: true };
|
|
1799
|
-
}
|
|
1800
|
-
|
|
1801
|
-
if (command === "/copy") {
|
|
1802
|
-
const isZh = config.language === "zh";
|
|
1803
|
-
const history = loop.getHistory();
|
|
1804
|
-
const lastAssistantMsg = [...history]
|
|
1805
|
-
.reverse()
|
|
1806
|
-
.find((msg) => msg.role === "assistant");
|
|
1807
|
-
|
|
1808
|
-
if (!lastAssistantMsg) {
|
|
1809
|
-
console.log(
|
|
1810
|
-
isZh
|
|
1811
|
-
? picocolors.yellow("没有找到 AI 的最近回复。")
|
|
1812
|
-
: picocolors.yellow(
|
|
1813
|
-
"No recent assistant response found to copy.",
|
|
1814
|
-
),
|
|
1815
|
-
);
|
|
1816
|
-
return { shouldExit: false, processed: true };
|
|
1817
|
-
}
|
|
1818
|
-
|
|
1819
|
-
let textToCopy = "";
|
|
1820
|
-
if (typeof lastAssistantMsg.content === "string") {
|
|
1821
|
-
textToCopy = lastAssistantMsg.content;
|
|
1822
|
-
} else if (Array.isArray(lastAssistantMsg.content)) {
|
|
1823
|
-
textToCopy = lastAssistantMsg.content
|
|
1824
|
-
.map((c: any) => (c.type === "text" ? c.text : ""))
|
|
1825
|
-
.join("");
|
|
1826
|
-
}
|
|
1827
|
-
|
|
1828
|
-
if (!textToCopy) {
|
|
1829
|
-
console.log(
|
|
1830
|
-
isZh
|
|
1831
|
-
? picocolors.yellow("AI 的最近回复内容为空。")
|
|
1832
|
-
: picocolors.yellow("Recent assistant response is empty."),
|
|
1833
|
-
);
|
|
1834
|
-
return { shouldExit: false, processed: true };
|
|
1835
|
-
}
|
|
1836
|
-
|
|
1837
|
-
const copied = this.copyToClipboard(textToCopy);
|
|
1838
|
-
if (copied) {
|
|
1839
|
-
console.log(
|
|
1840
|
-
isZh
|
|
1841
|
-
? picocolors.green("✔ 已成功复制 AI 最近回复到剪贴板!")
|
|
1842
|
-
: picocolors.green(
|
|
1843
|
-
"✔ Successfully copied recent AI response to clipboard!",
|
|
1844
|
-
),
|
|
1845
|
-
);
|
|
1846
|
-
} else {
|
|
1847
|
-
console.log(
|
|
1848
|
-
isZh
|
|
1849
|
-
? picocolors.red(
|
|
1850
|
-
"✖ 复制到剪贴板失败,系统未配置剪贴板工具(如 pbcopy/clip/xclip)。",
|
|
1851
|
-
)
|
|
1852
|
-
: picocolors.red(
|
|
1853
|
-
"✖ Failed to copy to clipboard. Ensure pbcopy/clip/xclip is installed.",
|
|
1854
|
-
),
|
|
1855
|
-
);
|
|
1856
|
-
}
|
|
1857
|
-
return { shouldExit: false, processed: true };
|
|
1858
|
-
}
|
|
1859
|
-
|
|
1860
|
-
this.printOutput(
|
|
1861
|
-
picocolors.red(
|
|
1862
|
-
`Unknown command: ${trimmed}. Type /help for available commands.`,
|
|
1863
|
-
),
|
|
1864
|
-
);
|
|
1865
|
-
return { shouldExit: false, processed: true };
|
|
1866
|
-
}
|
|
1867
|
-
|
|
1868
|
-
return { shouldExit: false, processed: false };
|
|
1869
|
-
}
|
|
1870
|
-
|
|
1871
|
-
private getNestedProperty(obj: any, path: string): any {
|
|
1872
|
-
const parts = path.split(".");
|
|
1873
|
-
let current = obj;
|
|
1874
|
-
for (const part of parts) {
|
|
1875
|
-
if (current == null || typeof current !== "object") {
|
|
1876
|
-
return undefined;
|
|
1877
|
-
}
|
|
1878
|
-
current = current[part];
|
|
1879
|
-
}
|
|
1880
|
-
return current;
|
|
1881
|
-
}
|
|
1882
|
-
|
|
1883
|
-
private setNestedProperty(obj: any, path: string, value: any): void {
|
|
1884
|
-
const parts = path.split(".");
|
|
1885
|
-
let current = obj;
|
|
1886
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
1887
|
-
const part = parts[i];
|
|
1888
|
-
if (
|
|
1889
|
-
!(part in current) ||
|
|
1890
|
-
current[part] == null ||
|
|
1891
|
-
typeof current[part] !== "object"
|
|
1892
|
-
) {
|
|
1893
|
-
current[part] = {};
|
|
1894
|
-
}
|
|
1895
|
-
current = current[part];
|
|
1896
|
-
}
|
|
1897
|
-
current[parts[parts.length - 1]] = value;
|
|
1898
|
-
}
|
|
1899
|
-
|
|
1900
|
-
private copyToClipboard(text: string): boolean {
|
|
1901
|
-
const { execSync } = require("child_process");
|
|
1902
|
-
try {
|
|
1903
|
-
if (process.platform === "win32") {
|
|
1904
|
-
execSync("clip", { input: text });
|
|
1905
|
-
return true;
|
|
1906
|
-
} else if (process.platform === "darwin") {
|
|
1907
|
-
execSync("pbcopy", { input: text });
|
|
1908
|
-
return true;
|
|
1909
|
-
} else {
|
|
1910
|
-
try {
|
|
1911
|
-
execSync("xclip -selection clipboard", { input: text });
|
|
1912
|
-
return true;
|
|
1913
|
-
} catch {
|
|
1914
|
-
try {
|
|
1915
|
-
execSync("xsel -ib", { input: text });
|
|
1916
|
-
return true;
|
|
1917
|
-
} catch {
|
|
1918
|
-
try {
|
|
1919
|
-
execSync("wl-copy", { input: text });
|
|
1920
|
-
return true;
|
|
1921
|
-
} catch {
|
|
1922
|
-
return false;
|
|
1923
|
-
}
|
|
1924
|
-
}
|
|
1925
|
-
}
|
|
1926
|
-
}
|
|
1927
|
-
} catch {
|
|
1928
|
-
return false;
|
|
1929
|
-
}
|
|
1930
|
-
}
|
|
1931
|
-
}
|
|
1932
|
-
|
|
1933
|
-
export async function getAutocompleteCandidates(
|
|
1934
|
-
cwd: string,
|
|
1935
|
-
config: any,
|
|
1936
|
-
): Promise<{
|
|
1937
|
-
commands: string[];
|
|
1938
|
-
files: string[];
|
|
1939
|
-
symbols: string[];
|
|
1940
|
-
sessions: string[];
|
|
1941
|
-
}> {
|
|
1942
|
-
const customCommands = loadCustomCommands(cwd, BUILTIN_SLASH_COMMANDS);
|
|
1943
|
-
const commands = [
|
|
1944
|
-
...BUILTIN_SLASH_COMMANDS,
|
|
1945
|
-
...customCommands.map((command) => `/${command.name}`),
|
|
1946
|
-
];
|
|
1947
|
-
const files: string[] = [];
|
|
1948
|
-
const symbols: string[] = [];
|
|
1949
|
-
const sessions: string[] = [];
|
|
1950
|
-
|
|
1951
|
-
const normCwd = resolve(cwd).toLowerCase().replace(/\\/g, "/");
|
|
1952
|
-
const normHome = resolve(homedir()).toLowerCase().replace(/\\/g, "/");
|
|
1953
|
-
const isHomeOrRoot =
|
|
1954
|
-
normCwd === normHome ||
|
|
1955
|
-
normCwd === "/" ||
|
|
1956
|
-
/^[a-zA-Z]:\/$/.test(normCwd) ||
|
|
1957
|
-
dirname(normCwd) === normCwd;
|
|
1958
|
-
|
|
1959
|
-
if (isHomeOrRoot) {
|
|
1960
|
-
return {
|
|
1961
|
-
commands,
|
|
1962
|
-
files,
|
|
1963
|
-
symbols,
|
|
1964
|
-
sessions,
|
|
1965
|
-
};
|
|
1966
|
-
}
|
|
1967
|
-
|
|
1968
|
-
try {
|
|
1969
|
-
const ignorePatterns = config.context?.ignore || [];
|
|
1970
|
-
const globbedFiles = await glob("**/*", {
|
|
1971
|
-
cwd,
|
|
1972
|
-
ignore: ignorePatterns,
|
|
1973
|
-
onlyFiles: true,
|
|
1974
|
-
dot: true,
|
|
1975
|
-
suppressErrors: true,
|
|
1976
|
-
});
|
|
1977
|
-
files.push(...globbedFiles);
|
|
1978
|
-
} catch {
|
|
1979
|
-
// Ignored
|
|
1980
|
-
}
|
|
1981
|
-
|
|
1982
|
-
try {
|
|
1983
|
-
const indexPath = join(cwd, ".orbit", "symbols.json");
|
|
1984
|
-
if (existsSync(indexPath)) {
|
|
1985
|
-
const raw = readFileSync(indexPath, "utf8");
|
|
1986
|
-
const index = JSON.parse(raw);
|
|
1987
|
-
if (index.files && typeof index.files === "object") {
|
|
1988
|
-
for (const fileData of Object.values(index.files)) {
|
|
1989
|
-
const data = fileData as any;
|
|
1990
|
-
if (data && Array.isArray(data.symbols)) {
|
|
1991
|
-
for (const sym of data.symbols) {
|
|
1992
|
-
if (sym.name) {
|
|
1993
|
-
symbols.push(sym.name);
|
|
1994
|
-
}
|
|
1995
|
-
}
|
|
1996
|
-
}
|
|
1997
|
-
}
|
|
1998
|
-
}
|
|
1999
|
-
}
|
|
2000
|
-
} catch {
|
|
2001
|
-
// Ignored
|
|
2002
|
-
}
|
|
2003
|
-
|
|
2004
|
-
try {
|
|
2005
|
-
const sessionDir = join(cwd, ".orbit", "sessions");
|
|
2006
|
-
if (existsSync(sessionDir)) {
|
|
2007
|
-
const dirs = readdirSync(sessionDir);
|
|
2008
|
-
for (const dir of dirs) {
|
|
2009
|
-
const sessionFile = join(sessionDir, dir, "session.json");
|
|
2010
|
-
if (existsSync(sessionFile)) {
|
|
2011
|
-
sessions.push(dir);
|
|
2012
|
-
}
|
|
2013
|
-
}
|
|
2014
|
-
}
|
|
2015
|
-
} catch {
|
|
2016
|
-
// Ignored
|
|
2017
|
-
}
|
|
2018
|
-
|
|
2019
|
-
return {
|
|
2020
|
-
commands,
|
|
2021
|
-
files,
|
|
2022
|
-
symbols: Array.from(new Set(symbols)),
|
|
2023
|
-
sessions,
|
|
2024
|
-
};
|
|
2025
|
-
}
|