lemma-agent 0.0.0-stage → 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 +121 -2
- package/bin/lemma.mjs +42 -0
- package/dist/acceptance.js +63 -0
- package/dist/admin.js +1482 -0
- package/dist/agents.js +245 -0
- package/dist/api-member.js +537 -0
- package/dist/app.js +161 -0
- package/dist/board.js +185 -0
- package/dist/business-tools.js +153 -0
- package/dist/change-ops.js +91 -0
- package/dist/changes.js +46 -0
- package/dist/cli-runner.js +106 -0
- package/dist/code-tools.js +396 -0
- package/dist/coding-benchmark-fixtures.js +1049 -0
- package/dist/coding-benchmark-suite.js +1252 -0
- package/dist/collaboration-benchmark.js +349 -0
- package/dist/config.js +392 -0
- package/dist/context-benchmark.js +91 -0
- package/dist/conventions.js +282 -0
- package/dist/conversation.js +39 -0
- package/dist/credentials.js +44 -0
- package/dist/deliverables.js +43 -0
- package/dist/digest.js +129 -0
- package/dist/dispatch.js +801 -0
- package/dist/execution-settings.js +24 -0
- package/dist/git-workbench.js +800 -0
- package/dist/git.js +149 -0
- package/dist/integration-git.js +108 -0
- package/dist/integrations.js +557 -0
- package/dist/jobs.js +100 -0
- package/dist/lead.js +263 -0
- package/dist/learning.js +36 -0
- package/dist/main.js +1402 -0
- package/dist/mcp-settings.js +45 -0
- package/dist/mcp-tools.js +102 -0
- package/dist/member-settings.js +245 -0
- package/dist/model-selection.js +48 -0
- package/dist/models.js +145 -0
- package/dist/native-selectors.js +45 -0
- package/dist/native-terminal-components.js +100 -0
- package/dist/ood-benchmark.js +660 -0
- package/dist/operations.js +21 -0
- package/dist/printer.js +113 -0
- package/dist/repair.js +102 -0
- package/dist/resources.js +31 -0
- package/dist/result-summary.js +8 -0
- package/dist/results.js +41 -0
- package/dist/scope.js +114 -0
- package/dist/sessions.js +95 -0
- package/dist/settings.js +30 -0
- package/dist/task-commands.js +37 -0
- package/dist/task-lifecycle.js +49 -0
- package/dist/task-readiness.js +22 -0
- package/dist/task-recovery.js +116 -0
- package/dist/task-review.js +70 -0
- package/dist/task-workflow.js +424 -0
- package/dist/terminal-layout.js +27 -0
- package/dist/terminal-state.js +145 -0
- package/dist/terminal.js +800 -0
- package/dist/tool-settings.js +111 -0
- package/dist/tools.js +1043 -0
- package/dist/ux-telemetry.js +127 -0
- package/dist/view.js +114 -0
- package/package.json +79 -6
- package/web/index.html +136 -0
- package/web/js/conversations.js +312 -0
- package/web/js/dag.js +495 -0
- package/web/js/drawer.js +139 -0
- package/web/js/editors.js +424 -0
- package/web/js/gitlens.js +844 -0
- package/web/js/main.js +152 -0
- package/web/js/sankey.js +476 -0
- package/web/js/store.js +291 -0
- package/web/js/ticket.js +151 -0
- package/web/js/timeline.js +183 -0
- package/web/js/topology.js +454 -0
- package/web/js/util.js +232 -0
- package/web/js/views.js +1496 -0
- package/web/style.css +5299 -0
package/dist/terminal.js
ADDED
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
// Pi TUI renders committed Durable views; runtime owns dispatch and persistence.
|
|
2
|
+
import { CombinedAutocompleteProvider, Container, Editor, Input, getKeybindings, matchesKey, ProcessTerminal, ScrollView, SelectList, Text, truncateToWidth, visibleWidth, TuiAltScreen, VStack, } from "@earendil-works/pi-tui";
|
|
3
|
+
import { colors as c } from "./printer.js";
|
|
4
|
+
import { CustomEditor, ModelSelectorComponent } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { nativeSessions, nativeConversationTree } from "./native-selectors.js";
|
|
6
|
+
import { AssistantMessageComponent, UserMessageComponent, ToolExecutionComponent, getMarkdownTheme, getSelectListTheme, nativeToolRenderers, safeNativeComponent, assistantMessage, cleanText, } from "./native-terminal-components.js";
|
|
7
|
+
import { conversationWelcome, terminalLabel, terminalRow } from "./terminal-layout.js";
|
|
8
|
+
export const TERMINAL_COMMANDS = [
|
|
9
|
+
["agents", "成员和模型"],
|
|
10
|
+
["agent", "add / edit / show / enable / disable / model / key 成员配置"],
|
|
11
|
+
["setup", "引导配置总控模型、接口与凭据"],
|
|
12
|
+
["tool", "list / add / edit / key / remove / test 成员业务工具"],
|
|
13
|
+
["limits", "查看并发和预算;edit 引导修改限制"],
|
|
14
|
+
["progress", "运行、排队与预留;定位等待原因"],
|
|
15
|
+
["mcp", "add / refresh / remove / list 成员 MCP 只读工具"],
|
|
16
|
+
["help", "终端操作与成员配置命令"],
|
|
17
|
+
["history", "加载当前对话更早的记录,不调用模型"],
|
|
18
|
+
["thinking", "展开或收起模型返回的思考记录"],
|
|
19
|
+
["web", "后台网页"],
|
|
20
|
+
["usage", "用量"],
|
|
21
|
+
["goal", "项目目标"],
|
|
22
|
+
["tasks", "分工表"],
|
|
23
|
+
["ready", "查看就绪队列、依赖等待原因和自动派发授权"],
|
|
24
|
+
["workflow", "status / pause / resume 自动协作状态和暂停恢复"],
|
|
25
|
+
["task", "inspect / resume / supply / reassign / cancel 受阻任务恢复"],
|
|
26
|
+
["result", "按 result_id 分页查看完整成员结果,不调用模型"],
|
|
27
|
+
["operations", "查看业务操作;resolve 人工核对未知远端状态"],
|
|
28
|
+
["rules", "共同规则"],
|
|
29
|
+
["jobs", "后台任务"],
|
|
30
|
+
["queue", "排到下一轮"],
|
|
31
|
+
["continue", "续跑"],
|
|
32
|
+
["compact", "压缩上下文"],
|
|
33
|
+
["abort", "中止本轮;all 连后台一起停"],
|
|
34
|
+
["changes", "待合并改动"],
|
|
35
|
+
["integrate", "create / list / show / cancel 多分支联合集成与检查"],
|
|
36
|
+
["diff", "查看改动"],
|
|
37
|
+
["apply", "应用改动"],
|
|
38
|
+
["drop", "丢弃改动"],
|
|
39
|
+
["clean", "清理工作副本"],
|
|
40
|
+
["new", "新上下文"],
|
|
41
|
+
["exit", "退出"],
|
|
42
|
+
["quit", "退出"],
|
|
43
|
+
["sessions", "列出隔离会话"],
|
|
44
|
+
["session", "new / use / rename 会话"],
|
|
45
|
+
["conversations", "搜索会话与子对话"],
|
|
46
|
+
["taskgraph", "原生任务面板"],
|
|
47
|
+
["tree", "原生对话关系树"],
|
|
48
|
+
["model", "原生总控模型选择"],
|
|
49
|
+
["main", "返回总控"],
|
|
50
|
+
["members", "成员上下文"],
|
|
51
|
+
["learn", "已验证的修复记录"],
|
|
52
|
+
["repair", "自动修复状态"],
|
|
53
|
+
["conversation", "总控对话 ID 与隔离边界"],
|
|
54
|
+
["id", "当前对话 ID"],
|
|
55
|
+
].map(([name, description]) => ({ name: name, description: description }));
|
|
56
|
+
/** Untrusted output cannot change cursor position, title or terminal modes. */
|
|
57
|
+
export const terminalText = cleanText;
|
|
58
|
+
const plain = (text, limit = 20_000) => terminalText(String(text ?? "")).slice(0, limit);
|
|
59
|
+
const selectTheme = getSelectListTheme();
|
|
60
|
+
/** Reuse native editing/paste behavior; credentials never enter the rendered screen. */
|
|
61
|
+
class SettingsInput extends Input {
|
|
62
|
+
secret = false;
|
|
63
|
+
render(width) {
|
|
64
|
+
return this.secret ? ["•".repeat(Math.min(Math.max(0, width - 2), this.getValue().length)) + "▏"] : super.render(width);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
class Line {
|
|
68
|
+
value;
|
|
69
|
+
constructor(value) {
|
|
70
|
+
this.value = value;
|
|
71
|
+
}
|
|
72
|
+
render(width) {
|
|
73
|
+
return [truncateToWidth(this.value(width), Math.max(1, width))];
|
|
74
|
+
}
|
|
75
|
+
invalidate() { }
|
|
76
|
+
}
|
|
77
|
+
/** Observe native scroll boundaries without intercepting its keyboard, mouse or selection input. */
|
|
78
|
+
class HistoryScrollView extends ScrollView {
|
|
79
|
+
onReachStart;
|
|
80
|
+
constructor(component, onReachStart) {
|
|
81
|
+
super(component, { follow: "end", primary: true, scrollbar: "auto" });
|
|
82
|
+
this.onReachStart = onReachStart;
|
|
83
|
+
}
|
|
84
|
+
updateLayout(height, viewport, requestRender) {
|
|
85
|
+
super.updateLayout(height, viewport, requestRender);
|
|
86
|
+
if (!this.isFollowingEnd && this.scrollTop <= this.viewportHeight)
|
|
87
|
+
this.onReachStart();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Isolate input history without replacing Editor or using private framework fields. */
|
|
91
|
+
class ConversationEditor extends CustomEditor {
|
|
92
|
+
caption = () => "输入消息";
|
|
93
|
+
renderTopBorder(width, hiddenLineCount) {
|
|
94
|
+
if (hiddenLineCount > 0 || width < 24)
|
|
95
|
+
return super.renderTopBorder(width, hiddenLineCount);
|
|
96
|
+
const label = truncateToWidth(` ${this.caption()} `, Math.max(1, width - 4));
|
|
97
|
+
return this.borderColor("──" + label + "─".repeat(Math.max(0, width - visibleWidth(label) - 2)));
|
|
98
|
+
}
|
|
99
|
+
histories = new Map();
|
|
100
|
+
key = "";
|
|
101
|
+
browsing = -1;
|
|
102
|
+
draft = "";
|
|
103
|
+
killedText = "";
|
|
104
|
+
bindHistory(key) {
|
|
105
|
+
this.key = key;
|
|
106
|
+
this.browsing = -1;
|
|
107
|
+
this.draft = "";
|
|
108
|
+
this.killedText = "";
|
|
109
|
+
// A blank native submit resets undo, paste and navigation state without sending input.
|
|
110
|
+
const submit = this.onSubmit, change = this.onChange;
|
|
111
|
+
this.onSubmit = undefined;
|
|
112
|
+
this.onChange = undefined;
|
|
113
|
+
try {
|
|
114
|
+
super.setText("");
|
|
115
|
+
super.handleInput("\r");
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
this.onSubmit = submit;
|
|
119
|
+
this.onChange = change;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
addToHistory(text) {
|
|
123
|
+
const values = this.histories.get(this.key) ?? [];
|
|
124
|
+
if (text.trim() && values.at(-1) !== text)
|
|
125
|
+
values.push(text);
|
|
126
|
+
this.histories.set(this.key, values.slice(-100));
|
|
127
|
+
this.browsing = -1;
|
|
128
|
+
}
|
|
129
|
+
handleInput(data) {
|
|
130
|
+
const keys = getKeybindings();
|
|
131
|
+
// The public Editor has no kill-ring reset; keep yank in this binding too.
|
|
132
|
+
if (keys.matches(data, "tui.editor.yank")) {
|
|
133
|
+
if (this.killedText)
|
|
134
|
+
this.insertTextAtCursor(this.killedText);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (keys.matches(data, "tui.editor.yankPop"))
|
|
138
|
+
return;
|
|
139
|
+
const history = this.histories.get(this.key) ?? [];
|
|
140
|
+
if (matchesKey(data, "up") && history.length && (this.browsing >= 0 || !this.getText())) {
|
|
141
|
+
if (this.browsing < 0)
|
|
142
|
+
this.draft = this.getExpandedText();
|
|
143
|
+
this.browsing = Math.min(history.length - 1, this.browsing + 1);
|
|
144
|
+
super.setText(history[history.length - 1 - this.browsing]);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (matchesKey(data, "down") && this.browsing >= 0) {
|
|
148
|
+
this.browsing--;
|
|
149
|
+
super.setText(this.browsing < 0 ? this.draft : history[history.length - 1 - this.browsing]);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!matchesKey(data, "up") && !matchesKey(data, "down"))
|
|
153
|
+
this.browsing = -1;
|
|
154
|
+
const cutting = [
|
|
155
|
+
"tui.editor.deleteWordBackward",
|
|
156
|
+
"tui.editor.deleteWordForward",
|
|
157
|
+
"tui.editor.deleteToLineStart",
|
|
158
|
+
"tui.editor.deleteToLineEnd",
|
|
159
|
+
].some((action) => keys.matches(data, action));
|
|
160
|
+
const before = cutting ? this.getExpandedText() : "";
|
|
161
|
+
super.handleInput(data);
|
|
162
|
+
if (cutting) {
|
|
163
|
+
const after = this.getExpandedText();
|
|
164
|
+
let left = 0;
|
|
165
|
+
while (left < after.length && before[left] === after[left])
|
|
166
|
+
left++;
|
|
167
|
+
let right = 0;
|
|
168
|
+
while (right < after.length - left && before[before.length - right - 1] === after[after.length - right - 1])
|
|
169
|
+
right++;
|
|
170
|
+
this.killedText = before.slice(left, before.length - right);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const TOOL_STATUS = {
|
|
175
|
+
running: "执行中",
|
|
176
|
+
pending: "排队",
|
|
177
|
+
waiting: "等待中",
|
|
178
|
+
sleeping: "等待中",
|
|
179
|
+
done: "完成",
|
|
180
|
+
error: "失败",
|
|
181
|
+
aborted: "已中止",
|
|
182
|
+
interrupted: "已中断",
|
|
183
|
+
completing: "收尾中",
|
|
184
|
+
open: "等待结果",
|
|
185
|
+
};
|
|
186
|
+
export class AgentTerminal {
|
|
187
|
+
tui;
|
|
188
|
+
editor;
|
|
189
|
+
binding;
|
|
190
|
+
options;
|
|
191
|
+
transcript = new Container();
|
|
192
|
+
notices = new Container();
|
|
193
|
+
partial = safeNativeComponent(new AssistantMessageComponent(undefined, false));
|
|
194
|
+
liveTools = new Container();
|
|
195
|
+
taskPanel = new Text("", 1, 0);
|
|
196
|
+
scroll;
|
|
197
|
+
status = { busy: false, jobs: [], spent: 0, budget: null, pending: 0 };
|
|
198
|
+
blocks = [];
|
|
199
|
+
cards = new Map();
|
|
200
|
+
liveCards = new Map();
|
|
201
|
+
renderedItems = new WeakMap();
|
|
202
|
+
renderedTools = new WeakMap();
|
|
203
|
+
view;
|
|
204
|
+
tasks = [];
|
|
205
|
+
tasksVisible = false;
|
|
206
|
+
toolsExpanded = false;
|
|
207
|
+
thinkingHidden = false;
|
|
208
|
+
picker;
|
|
209
|
+
disposePicker;
|
|
210
|
+
size = 0;
|
|
211
|
+
stopped = false;
|
|
212
|
+
started = false;
|
|
213
|
+
lastUser;
|
|
214
|
+
unread = 0;
|
|
215
|
+
seenEntries = new Set();
|
|
216
|
+
newestEntry = -Infinity;
|
|
217
|
+
messageFrames = new Map();
|
|
218
|
+
failure = new Text("", 1, 0);
|
|
219
|
+
historyRequest;
|
|
220
|
+
historyAutoBlocked = false;
|
|
221
|
+
constructor(options) {
|
|
222
|
+
this.options = options;
|
|
223
|
+
this.binding = { ...options };
|
|
224
|
+
this.tui = new TuiAltScreen(options.terminal ?? new ProcessTerminal(), true, undefined, { mouse: true, wheelScrollLines: "auto" });
|
|
225
|
+
this.editor = new ConversationEditor(this.tui, { borderColor: c.cyan, selectList: selectTheme }, getKeybindings(), { paddingX: 1, autocompleteMaxVisible: 5 });
|
|
226
|
+
this.editor.onAction("app.model.select", () => options.onModels?.());
|
|
227
|
+
this.editor.caption = () => this.readOnly ? "只读浏览 · /main 返回总控" : this.status.busy ? "补充本轮要求 · Ctrl+C 中止" : "输入消息 · / 查看命令";
|
|
228
|
+
this.editor.onSubmit = (text) => {
|
|
229
|
+
if (this.stopped || !text.trim())
|
|
230
|
+
return;
|
|
231
|
+
this.editor.addToHistory(text);
|
|
232
|
+
this.scroll.scrollToEnd();
|
|
233
|
+
this.unread = 0;
|
|
234
|
+
options.onSubmit(text);
|
|
235
|
+
};
|
|
236
|
+
const document = new Container();
|
|
237
|
+
document.addChild(new Line(() => c.dim(this.view?.history?.loading ? " 正在加载更早记录…" : this.view?.history?.hasEarlier ? " 向上滚动加载更早记录 · /history" : this.view?.items.length ? " 已到对话开头" : "")));
|
|
238
|
+
document.addChild({ render: (width) => !this.view?.items.length && !this.status.busy && !this.view?.live.partial && !this.view?.live.runFailure && !(this.view?.tools ?? this.view?.live.tools)?.length ? conversationWelcome(width, this.readOnly) : [], invalidate() { } });
|
|
239
|
+
document.addChild(this.transcript);
|
|
240
|
+
document.addChild({ render: (width) => this.view?.live.partial || this.view?.partialMessage?.content.some((block) => block.type === "thinking") ? [truncateToWidth(c.dim(this.readOnly ? " 成员 · 正在回答" : " Lemma · 正在回答"), Math.max(1, width))] : [], invalidate() { } });
|
|
241
|
+
document.addChild(this.partial);
|
|
242
|
+
document.addChild(this.liveTools);
|
|
243
|
+
document.addChild(this.notices);
|
|
244
|
+
document.addChild(this.failure);
|
|
245
|
+
this.scroll = new HistoryScrollView(document, () => {
|
|
246
|
+
if (!this.historyAutoBlocked && !this.historyRequest && this.view?.history?.hasEarlier && !this.view.history.loading)
|
|
247
|
+
queueMicrotask(() => { if (!this.stopped && !this.historyAutoBlocked && !this.scroll.isFollowingEnd && this.scroll.scrollTop <= this.scroll.viewportHeight)
|
|
248
|
+
void this.loadEarlier(); });
|
|
249
|
+
});
|
|
250
|
+
this.tui.setLayoutRoot(new VStack([
|
|
251
|
+
{
|
|
252
|
+
component: new Line((width) => {
|
|
253
|
+
const b = this.binding;
|
|
254
|
+
return terminalRow(c.cyan(` Lemma${b.session ? ` · ${terminalLabel(b.session)}` : ""}${b.conversationId ? ` · ${terminalLabel(b.conversationLabel ?? "总控 CID")}:${terminalLabel(b.conversationId)}` : ""}`), c.dim(terminalLabel(b.model)), width);
|
|
255
|
+
}),
|
|
256
|
+
shrink: 0,
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
component: new Line(() => c.dim(` ${terminalLabel(this.binding.workdir, 500)}${this.binding.web ? ` 后台网页:${terminalLabel(this.binding.web, 200)}` : ""}`)),
|
|
260
|
+
shrink: 0,
|
|
261
|
+
visible: ({ height }) => height >= 10,
|
|
262
|
+
},
|
|
263
|
+
{ component: this.scroll, basis: 0, grow: 1, minSize: 1 },
|
|
264
|
+
{
|
|
265
|
+
component: this.taskPanel,
|
|
266
|
+
maxSize: 7,
|
|
267
|
+
shrink: 1,
|
|
268
|
+
visible: ({ height }) => this.tasksVisible && height >= 14,
|
|
269
|
+
},
|
|
270
|
+
{ component: new Line((width) => this.statusLine(width)), shrink: 0 },
|
|
271
|
+
{ component: this.editor, maxSize: 9, minSize: 3 },
|
|
272
|
+
{
|
|
273
|
+
component: new Line((width) => this.helpLine(width)),
|
|
274
|
+
shrink: 0,
|
|
275
|
+
visible: ({ height }) => height >= 10,
|
|
276
|
+
},
|
|
277
|
+
]));
|
|
278
|
+
this.bind(options);
|
|
279
|
+
this.tui.setFocus(this.editor);
|
|
280
|
+
this.tui.addInputListener((data) => {
|
|
281
|
+
if (this.picker) {
|
|
282
|
+
if (matchesKey(data, "ctrl+c") || matchesKey(data, "ctrl+k")) {
|
|
283
|
+
this.closePicker();
|
|
284
|
+
return { consume: true };
|
|
285
|
+
}
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
if (matchesKey(data, "ctrl+k") && options.onConversations) {
|
|
289
|
+
options.onConversations();
|
|
290
|
+
return { consume: true };
|
|
291
|
+
}
|
|
292
|
+
if (matchesKey(data, "alt+end")) {
|
|
293
|
+
this.scroll.scrollToEnd();
|
|
294
|
+
this.unread = 0;
|
|
295
|
+
this.tui.requestRender();
|
|
296
|
+
return { consume: true };
|
|
297
|
+
}
|
|
298
|
+
if (matchesKey(data, "alt+c") && options.onTree) {
|
|
299
|
+
options.onTree();
|
|
300
|
+
return { consume: true };
|
|
301
|
+
}
|
|
302
|
+
if (matchesKey(data, "ctrl+t")) {
|
|
303
|
+
this.toggleTasks();
|
|
304
|
+
return { consume: true };
|
|
305
|
+
}
|
|
306
|
+
if (matchesKey(data, "ctrl+e")) {
|
|
307
|
+
this.toolsExpanded = !this.toolsExpanded;
|
|
308
|
+
if (this.view)
|
|
309
|
+
this.applyConversation(this.view);
|
|
310
|
+
return { consume: true };
|
|
311
|
+
}
|
|
312
|
+
if (matchesKey(data, "alt+t")) {
|
|
313
|
+
this.toggleThinking();
|
|
314
|
+
return { consume: true };
|
|
315
|
+
}
|
|
316
|
+
if (matchesKey(data, "ctrl+o") && options.onSessionNext) {
|
|
317
|
+
options.onSessionNext();
|
|
318
|
+
return { consume: true };
|
|
319
|
+
}
|
|
320
|
+
if (matchesKey(data, "ctrl+n") && options.onSessionNew) {
|
|
321
|
+
options.onSessionNew();
|
|
322
|
+
return { consume: true };
|
|
323
|
+
}
|
|
324
|
+
if (matchesKey(data, "ctrl+c")) {
|
|
325
|
+
options.onInterrupt();
|
|
326
|
+
return { consume: true };
|
|
327
|
+
}
|
|
328
|
+
if (matchesKey(data, "ctrl+d") && !this.editor.getExpandedText()) {
|
|
329
|
+
options.onExit();
|
|
330
|
+
return { consume: true };
|
|
331
|
+
}
|
|
332
|
+
return undefined;
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
bind(binding, draft = "") {
|
|
336
|
+
if (this.stopped)
|
|
337
|
+
return;
|
|
338
|
+
this.closePicker();
|
|
339
|
+
this.binding = { ...binding };
|
|
340
|
+
this.historyRequest = undefined;
|
|
341
|
+
this.historyAutoBlocked = false;
|
|
342
|
+
this.tui.resetTextSelection();
|
|
343
|
+
this.transcript.clear();
|
|
344
|
+
this.notices.clear();
|
|
345
|
+
this.partial.updateContent(assistantMessage(""));
|
|
346
|
+
this.liveTools.clear();
|
|
347
|
+
this.cards.clear();
|
|
348
|
+
this.liveCards.clear();
|
|
349
|
+
this.blocks = [];
|
|
350
|
+
this.size = 0;
|
|
351
|
+
this.view = undefined;
|
|
352
|
+
this.lastUser = undefined;
|
|
353
|
+
this.unread = 0;
|
|
354
|
+
this.seenEntries.clear();
|
|
355
|
+
this.newestEntry = -Infinity;
|
|
356
|
+
this.messageFrames.clear();
|
|
357
|
+
this.failure.setText("");
|
|
358
|
+
this.tasks = [];
|
|
359
|
+
this.status = { busy: false, jobs: [], spent: 0, budget: null, pending: 0 };
|
|
360
|
+
this.editor.bindHistory(`${binding.session ?? ""}:${binding.conversationId ?? ""}`);
|
|
361
|
+
this.editor.setAutocompleteProvider(new CombinedAutocompleteProvider(TERMINAL_COMMANDS, binding.workdir));
|
|
362
|
+
this.editor.setText(draft);
|
|
363
|
+
this.renderTasks();
|
|
364
|
+
this.scroll.scrollToEnd();
|
|
365
|
+
this.tui.requestRender();
|
|
366
|
+
}
|
|
367
|
+
get readOnly() { return this.binding.conversationLabel?.includes("只读") ?? false; }
|
|
368
|
+
helpLine(width) {
|
|
369
|
+
if (!this.scroll.isFollowingEnd)
|
|
370
|
+
return c.yellow(` 正在翻阅${this.unread ? ` · ${this.unread} 条新内容` : ""} · Alt+End 回到最新`);
|
|
371
|
+
if (this.readOnly)
|
|
372
|
+
return c.dim(" 只读 · /main 返回总控 · PgUp/PgDn 翻阅 · Ctrl+K 会话");
|
|
373
|
+
return c.dim(width < 54 ? " 滚轮翻阅 · Alt+T 思考 · /help" : width < 100 ? " Enter 发送 · 滚轮/PgUp 翻阅 · Alt+T 思考 · /help" : " Enter 发送 · Ctrl+J 换行 · 滚轮/PgUp/PgDn 翻阅 · Alt+T 思考 · Ctrl+K 会话 · /help");
|
|
374
|
+
}
|
|
375
|
+
toggleThinking() {
|
|
376
|
+
this.thinkingHidden = !this.thinkingHidden;
|
|
377
|
+
this.partial.setHideThinkingBlock(this.thinkingHidden);
|
|
378
|
+
for (const card of this.cards.values())
|
|
379
|
+
if (card instanceof AssistantMessageComponent)
|
|
380
|
+
card.setHideThinkingBlock(this.thinkingHidden);
|
|
381
|
+
this.tui.flash(`思考记录:${this.thinkingHidden ? "已折叠" : "已展开"} · Alt+T 切换`);
|
|
382
|
+
this.tui.requestRender();
|
|
383
|
+
}
|
|
384
|
+
async loadEarlier() {
|
|
385
|
+
const history = this.view?.history;
|
|
386
|
+
if (this.stopped || this.historyRequest || history?.loading || !history?.hasEarlier)
|
|
387
|
+
return;
|
|
388
|
+
const request = this.historyRequest = {};
|
|
389
|
+
this.historyAutoBlocked = false;
|
|
390
|
+
const first = this.transcript.children[0];
|
|
391
|
+
try {
|
|
392
|
+
await history.loadEarlier();
|
|
393
|
+
if (this.stopped || this.historyRequest !== request)
|
|
394
|
+
return;
|
|
395
|
+
if (first && !this.scroll.isFollowingEnd) {
|
|
396
|
+
const index = this.transcript.children.indexOf(first);
|
|
397
|
+
if (index > 0) {
|
|
398
|
+
const width = this.scroll.getContentWidth(this.tui.terminal.columns);
|
|
399
|
+
const added = this.transcript.children.slice(0, index).reduce((sum, child) => sum + child.render(width).length, 0);
|
|
400
|
+
const top = this.scroll.scrollTop;
|
|
401
|
+
this.tui.renderNow();
|
|
402
|
+
this.scroll.scrollTo(top + added, { disableFollow: true });
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
if (!this.stopped && this.historyRequest === request) {
|
|
408
|
+
this.historyAutoBlocked = true;
|
|
409
|
+
this.tui.flash(`历史加载失败,可 /history 重试:${plain(error, 120)}`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
finally {
|
|
413
|
+
if (this.historyRequest === request)
|
|
414
|
+
this.historyRequest = undefined;
|
|
415
|
+
}
|
|
416
|
+
this.tui.requestRender();
|
|
417
|
+
}
|
|
418
|
+
statusLine(width) {
|
|
419
|
+
const s = this.status, label = this.binding.conversationLabel?.includes("只读") ? "子对话" : "总调度", state = this.view?.live.retry ? c.yellow(`${label}重试等待`) : s.busy ? c.yellow(`${label}运行中`) : this.readOnly ? c.dim("子对话只读") : this.view?.live.runFailure ? c.red("本轮未完成") : c.green(`${label}空闲`), budget = `$${s.spent.toFixed(3)}${s.budget === null ? "" : ` / $${s.budget.toFixed(2)}`}`;
|
|
420
|
+
const tools = (this.view?.tools ?? this.view?.live.tools ?? []).filter((tool) => !["done", "error", "aborted", "interrupted"].includes(tool.status)), queued = tools.filter((tool) => ["waiting", "sleeping", "pending"].includes(tool.status)).length;
|
|
421
|
+
const inbox = this.view?.live.inbox.length ?? 0;
|
|
422
|
+
const activity = `${tools.length ? ` · 工具 ${tools.length}${queued ? `(等待 ${queued})` : ""}` : ""}${inbox ? ` · 待处理消息 ${inbox}` : ""}${s.jobs.length ? ` · 后台 ${s.jobs.map((job) => terminalLabel(job)).join(" / ")}` : ""}`;
|
|
423
|
+
return terminalRow(` ${state}${activity}`, `成员 ${budget} · 待合并 ${s.pending}`, width);
|
|
424
|
+
}
|
|
425
|
+
start() {
|
|
426
|
+
if (this.started || this.stopped)
|
|
427
|
+
return;
|
|
428
|
+
this.started = true;
|
|
429
|
+
this.tui.start();
|
|
430
|
+
}
|
|
431
|
+
write(text) {
|
|
432
|
+
if (this.stopped)
|
|
433
|
+
return;
|
|
434
|
+
const safe = terminalText(text).slice(-20_000);
|
|
435
|
+
if (!safe.trim())
|
|
436
|
+
return;
|
|
437
|
+
const component = new Text(`${safe}\n`, 1, 0);
|
|
438
|
+
this.blocks.push({ component, size: safe.length });
|
|
439
|
+
this.size += safe.length;
|
|
440
|
+
this.notices.addChild(component);
|
|
441
|
+
if (!this.scroll.isFollowingEnd)
|
|
442
|
+
this.unread++;
|
|
443
|
+
while (this.blocks.length > 60 || this.size > 30_000) {
|
|
444
|
+
const first = this.blocks.shift();
|
|
445
|
+
this.size -= first.size;
|
|
446
|
+
this.notices.removeChild(first.component);
|
|
447
|
+
}
|
|
448
|
+
this.tui.requestRender();
|
|
449
|
+
}
|
|
450
|
+
setPartial(text, message) {
|
|
451
|
+
if (this.stopped)
|
|
452
|
+
return;
|
|
453
|
+
this.partial.updateContent(assistantMessage(text, message), true);
|
|
454
|
+
this.tui.requestRender();
|
|
455
|
+
}
|
|
456
|
+
setStatus(status) {
|
|
457
|
+
if (this.stopped)
|
|
458
|
+
return;
|
|
459
|
+
this.status = status;
|
|
460
|
+
this.tui.requestRender();
|
|
461
|
+
}
|
|
462
|
+
setModel(model) { this.binding.model = model; this.tui.requestRender(); }
|
|
463
|
+
applyConversation(view) {
|
|
464
|
+
if (this.stopped)
|
|
465
|
+
return;
|
|
466
|
+
const retained = view.items;
|
|
467
|
+
const newest = this.newestEntry;
|
|
468
|
+
this.view = { ...view, items: retained };
|
|
469
|
+
const fresh = retained.filter((item) => item.id > newest && !this.seenEntries.has(item.id));
|
|
470
|
+
for (const item of fresh)
|
|
471
|
+
this.newestEntry = Math.max(this.newestEntry, item.id);
|
|
472
|
+
if (!this.scroll.isFollowingEnd)
|
|
473
|
+
this.unread += fresh.length;
|
|
474
|
+
else
|
|
475
|
+
this.unread = 0;
|
|
476
|
+
this.seenEntries = new Set(retained.map((item) => item.id));
|
|
477
|
+
const failure = view.live.runFailure;
|
|
478
|
+
const next = failure && ["interrupted", "turn_limit", "aborted"].includes(failure.reason) ? "/continue 续跑 · /tasks 查看成果" : failure?.reason === "model_error" || failure?.reason === "no_model" ? "/model 检查模型 · /help 查看操作" : "/tasks 查看成果 · /help 查看操作";
|
|
479
|
+
this.failure.setText(failure && !view.live.busy ? c.red(`本轮未完成 · ${plain(failure.reason, 80)}`) + (failure.detail ? `\n${plain(failure.detail, 220)}` : "") + c.dim(`\n${next} · 记录已保留`) : "");
|
|
480
|
+
const lastUser = retained.findLast((item) => item.type === "user")?.id;
|
|
481
|
+
if (lastUser !== undefined && lastUser !== this.lastUser) {
|
|
482
|
+
// New committed input starts the next interaction, without old command output below it.
|
|
483
|
+
this.notices.clear();
|
|
484
|
+
this.blocks = [];
|
|
485
|
+
this.size = 0;
|
|
486
|
+
this.lastUser = lastUser;
|
|
487
|
+
}
|
|
488
|
+
// Providers may reuse call IDs in later runs: pair results with the preceding call entry.
|
|
489
|
+
const results = new Map(), pending = new Map(), pairedResults = new Set();
|
|
490
|
+
for (const item of retained) {
|
|
491
|
+
if (item.type === "assistant")
|
|
492
|
+
for (const call of item.toolCalls)
|
|
493
|
+
pending.set(call.id, `tool:${item.id}:${call.id}`);
|
|
494
|
+
if (item.type === "tool") {
|
|
495
|
+
const key = pending.get(item.callId);
|
|
496
|
+
if (key) {
|
|
497
|
+
results.set(key, item);
|
|
498
|
+
pairedResults.add(item.id);
|
|
499
|
+
pending.delete(item.callId);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
const activeTools = view.tools ?? view.live.tools;
|
|
504
|
+
const live = new Map(activeTools.map((tool) => [tool.callId, tool]));
|
|
505
|
+
const emitted = new Set(), used = new Set();
|
|
506
|
+
const children = [];
|
|
507
|
+
const message = (item) => {
|
|
508
|
+
const key = `entry:${item.id}`;
|
|
509
|
+
let card = this.cards.get(key);
|
|
510
|
+
if (item.type === "user") {
|
|
511
|
+
if (!(card instanceof UserMessageComponent))
|
|
512
|
+
card = safeNativeComponent(new UserMessageComponent(terminalText(item.text), getMarkdownTheme(), 1));
|
|
513
|
+
}
|
|
514
|
+
else if (item.type === "assistant") {
|
|
515
|
+
if (!(card instanceof AssistantMessageComponent))
|
|
516
|
+
card = safeNativeComponent(new AssistantMessageComponent(undefined, this.thinkingHidden));
|
|
517
|
+
if (this.renderedItems.get(card) !== item)
|
|
518
|
+
card.updateContent(assistantMessage(item.text, item.message?.role === "assistant" ? item.message : undefined, item.error));
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
if (!(card instanceof Text))
|
|
522
|
+
card = new Text("", 1, 0);
|
|
523
|
+
card.setText(item.type === "job" ? `${c.yellow(`后台结果 ${plain(item.job)} · ${plain(item.agent)} · ${TOOL_STATUS[item.status] ?? plain(item.status)}`)}\n${plain(item.text)}` : item.type === "divider" ? c.dim(`── ${plain(item.label)} ──`) : "");
|
|
524
|
+
}
|
|
525
|
+
this.cards.set(key, card);
|
|
526
|
+
this.renderedItems.set(card, item);
|
|
527
|
+
used.add(key);
|
|
528
|
+
if (item.type === "user" || (item.type === "assistant" && (item.text || item.error || item.message?.role === "assistant" && item.message.content.some((block) => block.type === "thinking")))) {
|
|
529
|
+
let frame = this.messageFrames.get(key);
|
|
530
|
+
if (!frame) {
|
|
531
|
+
frame = new Container();
|
|
532
|
+
frame.addChild(new Text(item.type === "user" ? c.cyan(this.readOnly ? "收到的任务" : "你") : c.dim(this.readOnly ? "成员" : "Lemma"), 1, 0));
|
|
533
|
+
frame.addChild(card);
|
|
534
|
+
this.messageFrames.set(key, frame);
|
|
535
|
+
}
|
|
536
|
+
children.push(frame);
|
|
537
|
+
}
|
|
538
|
+
else
|
|
539
|
+
children.push(card);
|
|
540
|
+
};
|
|
541
|
+
const updateTool = (card, result, active) => {
|
|
542
|
+
const signature = JSON.stringify([result, active, this.toolsExpanded]);
|
|
543
|
+
if (this.renderedTools.get(card) === signature)
|
|
544
|
+
return;
|
|
545
|
+
this.renderedTools.set(card, signature);
|
|
546
|
+
if (active?.args)
|
|
547
|
+
card.updateArgs(active.args);
|
|
548
|
+
if (active?.argsComplete !== false)
|
|
549
|
+
card.setArgsComplete();
|
|
550
|
+
if (result || active) {
|
|
551
|
+
if (result || active?.status !== "pending")
|
|
552
|
+
card.markExecutionStarted();
|
|
553
|
+
const raw = result?.message?.role === "toolResult" ? result.message : undefined;
|
|
554
|
+
card.updateResult({
|
|
555
|
+
content: raw?.content ?? [{ type: "text", text: terminalText(result?.text ?? active?.output ?? "") }],
|
|
556
|
+
details: result?.details ?? active?.details,
|
|
557
|
+
isError: result?.isError ?? active?.isError ?? ["error", "aborted", "interrupted"].includes(active?.status ?? ""),
|
|
558
|
+
}, !result && !!active && !["done", "error", "aborted", "interrupted"].includes(active.status));
|
|
559
|
+
}
|
|
560
|
+
else
|
|
561
|
+
card.updateResult({ content: [{ type: "text", text: "执行已中断,等待续跑" }], isError: true });
|
|
562
|
+
card.setExpanded(this.toolsExpanded);
|
|
563
|
+
};
|
|
564
|
+
const tool = (key, callId, name, args, result) => {
|
|
565
|
+
let card = this.cards.get(key);
|
|
566
|
+
if (!(card instanceof ToolExecutionComponent)) {
|
|
567
|
+
card = safeNativeComponent(new ToolExecutionComponent(plain(name, 100), callId, args, { showImages: false }, nativeToolRenderers(name, this.binding.workdir), this.tui, this.binding.workdir));
|
|
568
|
+
this.cards.set(key, card);
|
|
569
|
+
}
|
|
570
|
+
const active = pending.get(callId) === key ? live.get(callId) : undefined;
|
|
571
|
+
updateTool(card, result, active);
|
|
572
|
+
used.add(key);
|
|
573
|
+
if (active)
|
|
574
|
+
emitted.add(callId);
|
|
575
|
+
children.push(card);
|
|
576
|
+
};
|
|
577
|
+
for (const item of retained) {
|
|
578
|
+
if (item.type === "user" || item.type === "assistant" || item.type === "job" || item.type === "divider")
|
|
579
|
+
message(item);
|
|
580
|
+
if (item.type === "assistant") {
|
|
581
|
+
for (const call of item.toolCalls) {
|
|
582
|
+
const key = `tool:${item.id}:${call.id}`;
|
|
583
|
+
tool(key, call.id, call.name, call.args, results.get(key));
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
if (item.type === "tool" && !pairedResults.has(item.id))
|
|
587
|
+
tool(`result:${item.id}`, item.callId, item.name, {}, item);
|
|
588
|
+
}
|
|
589
|
+
for (const key of this.cards.keys())
|
|
590
|
+
if (!used.has(key))
|
|
591
|
+
this.cards.delete(key);
|
|
592
|
+
for (const key of this.messageFrames.keys())
|
|
593
|
+
if (!used.has(key))
|
|
594
|
+
this.messageFrames.delete(key);
|
|
595
|
+
if (children.length !== this.transcript.children.length ||
|
|
596
|
+
children.some((child, index) => this.transcript.children[index] !== child)) {
|
|
597
|
+
this.transcript.clear();
|
|
598
|
+
for (const child of children)
|
|
599
|
+
this.transcript.addChild(child);
|
|
600
|
+
}
|
|
601
|
+
const liveChildren = [], liveUsed = new Set();
|
|
602
|
+
for (const active of activeTools) {
|
|
603
|
+
if (emitted.has(active.callId))
|
|
604
|
+
continue;
|
|
605
|
+
let card = this.liveCards.get(active.callId);
|
|
606
|
+
if (!card) {
|
|
607
|
+
card = safeNativeComponent(new ToolExecutionComponent(plain(active.name, 100), active.callId, active.args ?? {}, { showImages: false }, nativeToolRenderers(active.name, this.binding.workdir), this.tui, this.binding.workdir));
|
|
608
|
+
this.liveCards.set(active.callId, card);
|
|
609
|
+
}
|
|
610
|
+
updateTool(card, undefined, active);
|
|
611
|
+
liveChildren.push(card);
|
|
612
|
+
liveUsed.add(active.callId);
|
|
613
|
+
}
|
|
614
|
+
for (const key of this.liveCards.keys())
|
|
615
|
+
if (!liveUsed.has(key))
|
|
616
|
+
this.liveCards.delete(key);
|
|
617
|
+
if (liveChildren.length !== this.liveTools.children.length ||
|
|
618
|
+
liveChildren.some((child, index) => this.liveTools.children[index] !== child)) {
|
|
619
|
+
this.liveTools.clear();
|
|
620
|
+
for (const child of liveChildren)
|
|
621
|
+
this.liveTools.addChild(child);
|
|
622
|
+
}
|
|
623
|
+
this.setPartial(view.live.partial, view.partialMessage);
|
|
624
|
+
this.tui.requestRender();
|
|
625
|
+
}
|
|
626
|
+
applyTasks(tasks) {
|
|
627
|
+
if (this.stopped)
|
|
628
|
+
return;
|
|
629
|
+
this.tasks = tasks;
|
|
630
|
+
this.renderTasks();
|
|
631
|
+
this.tui.requestRender();
|
|
632
|
+
}
|
|
633
|
+
renderTasks() {
|
|
634
|
+
const lines = [c.cyan(`原生任务 · ${this.tasks.length} 项 · Ctrl+T 收起`)];
|
|
635
|
+
for (const task of this.tasks.slice(0, 5))
|
|
636
|
+
lines.push(`${plain(task.id, 14)} · ${TOOL_STATUS[task.status] ?? plain(task.status, 30)} · ${plain(task.label ?? task.kind, 100)} · 对话 ${plain(task.conversationId, 20)}${task.owner ? ` ← ${plain(task.owner, 12)}` : ""}`);
|
|
637
|
+
if (!this.tasks.length)
|
|
638
|
+
lines.push(c.dim("当前没有活动任务"));
|
|
639
|
+
else if (this.tasks.length > 5)
|
|
640
|
+
lines.push(c.dim(`另有 ${this.tasks.length - 5} 项活动任务`));
|
|
641
|
+
this.taskPanel.setText(lines.join("\n"));
|
|
642
|
+
}
|
|
643
|
+
toggleTasks() {
|
|
644
|
+
if (this.stopped)
|
|
645
|
+
return;
|
|
646
|
+
this.tasksVisible = !this.tasksVisible;
|
|
647
|
+
this.renderTasks();
|
|
648
|
+
this.tui.requestRender();
|
|
649
|
+
}
|
|
650
|
+
showConversations(items) {
|
|
651
|
+
if (this.stopped)
|
|
652
|
+
return;
|
|
653
|
+
this.closePicker();
|
|
654
|
+
const picker = nativeSessions(items, (id) => { this.closePicker(); this.options.onConversationSelect?.(id); }, () => this.closePicker(), () => this.tui.requestRender());
|
|
655
|
+
picker.addChild(new Text(c.dim("切换会话 / 查看子对话 · 仅导航,旧记录保留"), 1, 0));
|
|
656
|
+
this.picker = this.tui.showOverlay(picker, { width: "88%", maxHeight: "85%", anchor: "center", margin: 1 });
|
|
657
|
+
this.disposePicker = () => picker.getSessionList().onCancel?.();
|
|
658
|
+
this.tui.requestRender();
|
|
659
|
+
}
|
|
660
|
+
showConversationTree(items) {
|
|
661
|
+
if (this.stopped)
|
|
662
|
+
return;
|
|
663
|
+
this.closePicker();
|
|
664
|
+
const picker = nativeConversationTree(items, this.tui.terminal.rows, (id) => { this.closePicker(); this.options.onConversationSelect?.(id); }, () => this.closePicker());
|
|
665
|
+
picker.getTreeList().onLabelEdit = undefined;
|
|
666
|
+
this.picker = this.tui.showOverlay(picker, { width: "92%", maxHeight: "90%", anchor: "center", margin: 1 });
|
|
667
|
+
this.tui.requestRender();
|
|
668
|
+
}
|
|
669
|
+
showModels(runtime, current, choices, select) {
|
|
670
|
+
if (this.stopped)
|
|
671
|
+
return;
|
|
672
|
+
this.closePicker();
|
|
673
|
+
// Upstream types the catalog as ModelRuntime; the version-pinned selector calls only NativeModelCatalog's four methods.
|
|
674
|
+
const picker = safeNativeComponent(new ModelSelectorComponent(this.tui, current, runtime, choices.map((model) => ({ model })), (model) => { this.closePicker(); select(model); }, () => this.closePicker()), true);
|
|
675
|
+
this.picker = this.tui.showOverlay(picker, { width: "92%", maxHeight: "90%", anchor: "center", margin: 1 });
|
|
676
|
+
this.disposePicker = () => picker.dispose();
|
|
677
|
+
this.tui.requestRender();
|
|
678
|
+
}
|
|
679
|
+
question(options) {
|
|
680
|
+
if (this.stopped)
|
|
681
|
+
return Promise.reject(new Error("终端已关闭。"));
|
|
682
|
+
this.closePicker();
|
|
683
|
+
return new Promise((resolve, reject) => {
|
|
684
|
+
const input = new SettingsInput({ placeholder: options.value ? `留空保留:${plain(options.value, 180)}` : "请输入" });
|
|
685
|
+
input.secret = options.secret === true;
|
|
686
|
+
const panel = new Container();
|
|
687
|
+
panel.addChild(new Text(c.cyan(plain(options.title, 300)), 1, 1));
|
|
688
|
+
panel.addChild(input);
|
|
689
|
+
panel.addChild(new Text(c.dim("Enter 确认 · Esc / Ctrl+C 取消(不会发送给模型)"), 1, 1));
|
|
690
|
+
let settled = false;
|
|
691
|
+
input.onSubmit = (value) => { settled = true; input.setValue(""); this.closePicker(); resolve(value); };
|
|
692
|
+
input.onEscape = () => this.closePicker();
|
|
693
|
+
this.picker = this.tui.showOverlay(panel, { width: "88%", maxHeight: "85%", anchor: "center", margin: 1 });
|
|
694
|
+
this.disposePicker = () => { input.setValue(""); if (!settled)
|
|
695
|
+
reject(new Error("配置已取消,未保存。")); };
|
|
696
|
+
this.tui.setFocus(input);
|
|
697
|
+
this.tui.requestRender();
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
pickSelectOverlay(options) {
|
|
701
|
+
if (this.stopped || !options.items.length)
|
|
702
|
+
return Promise.resolve(undefined);
|
|
703
|
+
this.closePicker();
|
|
704
|
+
return new Promise((resolve) => {
|
|
705
|
+
const panel = new Container();
|
|
706
|
+
panel.addChild(new Text(c.cyan(plain(options.title, 200)), 1, 1));
|
|
707
|
+
for (const line of options.lines ?? [])
|
|
708
|
+
panel.addChild(new Text(plain(line, 260), 1, 0));
|
|
709
|
+
const list = new SelectList(options.items, Math.min(8, options.items.length), selectTheme, { minPrimaryColumnWidth: 24, maxPrimaryColumnWidth: 38 });
|
|
710
|
+
let settled = false;
|
|
711
|
+
const finish = (value) => {
|
|
712
|
+
if (settled)
|
|
713
|
+
return;
|
|
714
|
+
settled = true;
|
|
715
|
+
resolve(value);
|
|
716
|
+
};
|
|
717
|
+
list.onSelect = (item) => {
|
|
718
|
+
settled = true;
|
|
719
|
+
this.closePicker();
|
|
720
|
+
resolve(item.value);
|
|
721
|
+
};
|
|
722
|
+
list.onCancel = () => this.closePicker();
|
|
723
|
+
list.onSelectionChange = () => this.tui.requestRender();
|
|
724
|
+
panel.addChild(list);
|
|
725
|
+
panel.addChild(new Text(c.dim(options.hint ?? "↑↓ 选择 · Enter 确认 · Esc / Ctrl+C 取消"), 1, 1));
|
|
726
|
+
this.picker = this.tui.showOverlay(panel, { width: "90%", maxHeight: "88%", anchor: "center", margin: 1 });
|
|
727
|
+
this.disposePicker = () => finish(undefined);
|
|
728
|
+
this.tui.setFocus(list);
|
|
729
|
+
this.tui.requestRender();
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
async confirmApply(req) {
|
|
733
|
+
if (this.stopped)
|
|
734
|
+
return { approvedIds: [], deferred: true, reason: "终端已关闭。" };
|
|
735
|
+
const lines = [];
|
|
736
|
+
if (req.summary?.trim())
|
|
737
|
+
lines.push(c.yellow(plain(req.summary.trim(), 240)));
|
|
738
|
+
for (const item of req.items) {
|
|
739
|
+
const repoName = item.root.replace(/[\\/]+$/, "").split(/[\\/]/).pop() || item.root;
|
|
740
|
+
lines.push(`• #${item.id} (${item.isIntegration ? "联合集成" : item.agent}) · ${repoName} · ${item.filesCount} 文件 +${item.add} −${item.del} · ${plain(item.task, 64)}`);
|
|
741
|
+
}
|
|
742
|
+
const choices = [
|
|
743
|
+
...(req.items.length > 1
|
|
744
|
+
? [{ value: "all", label: `同意全部合并 (${req.items.length} 项)`, description: "依次应用到对应工作区(不自动 commit)" }]
|
|
745
|
+
: [{ value: `one:${req.items[0].id}`, label: `同意合并 #${req.items[0].id}`, description: `${req.items[0].filesCount} 文件 +${req.items[0].add} −${req.items[0].del}` }]),
|
|
746
|
+
...(req.items.length > 1
|
|
747
|
+
? req.items.map((item) => ({ value: `one:${item.id}`, label: `仅合并 #${item.id}`, description: `${item.filesCount} 文件 +${item.add} −${item.del} · ${plain(item.task, 50)}` }))
|
|
748
|
+
: []),
|
|
749
|
+
{ value: "reject", label: "暂不合并", description: "保留在待合并队列,稍后用 /apply 或网页处理" },
|
|
750
|
+
];
|
|
751
|
+
const picked = await this.pickSelectOverlay({
|
|
752
|
+
title: "⚑ 请求确认合并改动到工作区",
|
|
753
|
+
lines,
|
|
754
|
+
items: choices,
|
|
755
|
+
hint: "Enter 按所选执行 · Esc / Ctrl+C 暂不合并",
|
|
756
|
+
});
|
|
757
|
+
if (!picked || picked === "reject")
|
|
758
|
+
return { approvedIds: [] };
|
|
759
|
+
if (picked === "all")
|
|
760
|
+
return { approvedIds: req.items.map((x) => x.id) };
|
|
761
|
+
if (picked.startsWith("one:"))
|
|
762
|
+
return { approvedIds: [picked.slice(4)] };
|
|
763
|
+
return { approvedIds: [] };
|
|
764
|
+
}
|
|
765
|
+
async selectPendingChange(mode, items) {
|
|
766
|
+
if (!items.length)
|
|
767
|
+
return undefined;
|
|
768
|
+
const verb = mode === "/apply" ? "合并到工作区" : "丢弃改动并删除 worktree";
|
|
769
|
+
const choices = [
|
|
770
|
+
...(items.length > 1 ? [{ value: "all", label: `全部 (${items.length} 项)`, description: verb }] : []),
|
|
771
|
+
...items.map((item) => ({
|
|
772
|
+
value: item.id,
|
|
773
|
+
label: `#${item.id} (${item.agent})`,
|
|
774
|
+
description: `${item.filesCount} 文件 +${item.add} −${item.del} · ${plain(item.task, 55)}`,
|
|
775
|
+
})),
|
|
776
|
+
];
|
|
777
|
+
return this.pickSelectOverlay({
|
|
778
|
+
title: mode === "/apply" ? "选择要合并的改动 (/apply)" : "选择要丢弃的改动 (/drop)",
|
|
779
|
+
items: choices,
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
closePicker() {
|
|
783
|
+
if (!this.picker)
|
|
784
|
+
return;
|
|
785
|
+
const picker = this.picker, dispose = this.disposePicker;
|
|
786
|
+
this.picker = undefined;
|
|
787
|
+
this.disposePicker = undefined;
|
|
788
|
+
picker.hide();
|
|
789
|
+
dispose?.();
|
|
790
|
+
this.tui.setFocus(this.editor);
|
|
791
|
+
}
|
|
792
|
+
stop() {
|
|
793
|
+
if (this.stopped)
|
|
794
|
+
return;
|
|
795
|
+
this.closePicker();
|
|
796
|
+
this.stopped = true;
|
|
797
|
+
if (this.started)
|
|
798
|
+
this.tui.stop();
|
|
799
|
+
}
|
|
800
|
+
}
|