@autopilot-agent/cli 0.1.0 → 0.3.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 +30 -153
- package/dist/activity-animation.d.ts +6 -0
- package/dist/activity-animation.js +15 -18
- package/dist/composition-root.d.ts +31 -1
- package/dist/composition-root.js +124 -3
- package/dist/context-report.d.ts +9 -4
- package/dist/context-report.js +16 -5
- package/dist/controller.d.ts +34 -4
- package/dist/controller.js +494 -90
- package/dist/design-tokens.d.ts +4 -0
- package/dist/design-tokens.js +4 -0
- package/dist/error-diagnostics.d.ts +2 -0
- package/dist/error-diagnostics.js +3 -1
- package/dist/event-projector.d.ts +16 -2
- package/dist/event-projector.js +78 -7
- package/dist/index.d.ts +1 -1
- package/dist/main.js +142 -7
- package/dist/parser.js +511 -4
- package/dist/renderer.js +19 -3
- package/dist/session-summary.d.ts +7 -0
- package/dist/session-summary.js +25 -0
- package/dist/session-title.d.ts +5 -0
- package/dist/session-title.js +10 -0
- package/dist/termination-format.js +1 -0
- package/dist/thread-commands.d.ts +37 -0
- package/dist/thread-commands.js +233 -0
- package/dist/thread-host.d.ts +31 -0
- package/dist/thread-host.js +76 -0
- package/dist/timeline-lines.js +36 -28
- package/dist/tui/app.d.ts +22 -0
- package/dist/tui/app.js +119 -44
- package/dist/tui/approval.d.ts +5 -1
- package/dist/tui/approval.js +20 -7
- package/dist/tui/constants.d.ts +1 -1
- package/dist/tui/constants.js +1 -1
- package/dist/tui/format.d.ts +1 -1
- package/dist/tui/format.js +23 -11
- package/dist/tui/hooks/use-prompt-input.d.ts +52 -0
- package/dist/tui/hooks/use-prompt-input.js +148 -20
- package/dist/tui/hooks/use-status-view.d.ts +2 -2
- package/dist/tui/hooks/use-status-view.js +38 -11
- package/dist/tui/inline-output.d.ts +17 -0
- package/dist/tui/inline-output.js +31 -0
- package/dist/tui/input/clipboard-image.d.ts +17 -0
- package/dist/tui/input/clipboard-image.js +117 -0
- package/dist/tui/input-area.d.ts +8 -0
- package/dist/tui/input-area.js +18 -3
- package/dist/tui/lifecycle.js +2 -5
- package/dist/tui/markdown/diff.d.ts +1 -1
- package/dist/tui/markdown/diff.js +28 -1
- package/dist/tui/performance-diagnostics.js +2 -0
- package/dist/tui/prompt-input.d.ts +1 -0
- package/dist/tui/prompt-input.js +5 -3
- package/dist/tui/render-mode.d.ts +4 -4
- package/dist/tui/render-mode.js +5 -11
- package/dist/tui/run.js +37 -49
- package/dist/tui/session-manager.d.ts +115 -0
- package/dist/tui/session-manager.js +383 -0
- package/dist/tui/session-workspace.d.ts +9 -0
- package/dist/tui/session-workspace.js +383 -0
- package/dist/tui/status-line.d.ts +3 -0
- package/dist/tui/status-line.js +12 -6
- package/dist/tui/store.js +111 -53
- package/dist/tui/timeline/live.js +9 -8
- package/dist/tui/timeline/presentation-timeline.d.ts +2 -1
- package/dist/tui/timeline/presentation-timeline.js +34 -15
- package/dist/tui/timeline/progress.js +3 -2
- package/dist/tui/timeline/static-timeline.js +17 -8
- package/dist/tui/timeline/turn-items.d.ts +12 -0
- package/dist/tui/timeline/turn-items.js +28 -0
- package/dist/tui/timeline/turn.js +8 -7
- package/dist/tui/transcript.d.ts +2 -2
- package/dist/tui/transcript.js +21 -3
- package/dist/tui/trust-dialog.d.ts +3 -0
- package/dist/tui/trust-dialog.js +23 -2
- package/dist/tui/types.d.ts +23 -1
- package/dist/tui/types.js +4 -1
- package/dist/tui/viewport/sources.d.ts +2 -2
- package/dist/tui/viewport/sources.js +75 -75
- package/dist/tui/viewport/viewport.js +5 -1
- package/dist/types.d.ts +165 -2
- package/package.json +4 -4
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** CLI会话标题:沿Web短文本范式,不增加模型采样,普通界面不以机器ID作标题。 */
|
|
2
|
+
/** 从已有用户输入/成功回答得到20字短标题,清除终端控制字符和换行。 */
|
|
3
|
+
export function createCliSessionTitle(content) {
|
|
4
|
+
return content.replace(/[\x00-\x1f\x7f]/gu, " ").replace(/\s+/gu, " ").trim().slice(0, 20) || "Agent 对话";
|
|
5
|
+
}
|
|
6
|
+
/** 历史空标题与旧机器ID标题使用安全占位;手工标题仅做显示净化。 */
|
|
7
|
+
export function getCliSessionDisplayTitle(title, sessionId) {
|
|
8
|
+
const clean = title?.replace(/[\x00-\x1f\x7f]/gu, " ").trim();
|
|
9
|
+
return !clean || clean === sessionId || /^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$/iu.test(clean) ? "新对话" : clean;
|
|
10
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* thread-commands.ts — CLI 协作命令到 Core ThreadFacade 的薄映射
|
|
3
|
+
*
|
|
4
|
+
* 只做正文/文件读取、参数整形与输出行生成;组归属、幂等、容量和交付事实都在 Core。
|
|
5
|
+
* 短命令不启动 Runner,也不猜测其它进程是否在线:回执只表示消息已接受,
|
|
6
|
+
* 真正执行仍由在线宿主 Runner 与 Session lease 决定。
|
|
7
|
+
*/
|
|
8
|
+
import type { ThreadFacade } from "@autopilot-agent/core";
|
|
9
|
+
import type { CliExitCode, CliThreadCommand } from "./types.js";
|
|
10
|
+
/** 命令输入不合法;Main 映射为 usage 退出码,不进入 Runtime。 */
|
|
11
|
+
export declare class CliThreadInputError extends Error {
|
|
12
|
+
constructor(message: string);
|
|
13
|
+
}
|
|
14
|
+
/** 命令执行所需的宿主边界能力;测试可注入替身。 */
|
|
15
|
+
export interface CliThreadCommandScope {
|
|
16
|
+
threads: ThreadFacade;
|
|
17
|
+
/** 读取 UTF-8 文本;失败时由命令层收敛为固定输入错误。 */
|
|
18
|
+
readFile(path: string): Promise<string>;
|
|
19
|
+
/** 读取全部 stdin;调用方必须已确认用户显式要求从 stdin 读取。 */
|
|
20
|
+
readStdin(): Promise<string>;
|
|
21
|
+
/** watch 的逐行输出;只输出安全事件 JSON。 */
|
|
22
|
+
write(line: string): void;
|
|
23
|
+
/** watch 与 wait 的中止信号;中止只结束观察,不停止目标。 */
|
|
24
|
+
signal: AbortSignal;
|
|
25
|
+
/** watch 轮询间隔;缺省 500ms。 */
|
|
26
|
+
pollIntervalMs?: number;
|
|
27
|
+
}
|
|
28
|
+
/** 命令执行结果;result 由 Main 统一序列化为 JSON。 */
|
|
29
|
+
export interface CliThreadCommandResult {
|
|
30
|
+
exitCode: CliExitCode;
|
|
31
|
+
result?: unknown;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 执行一条协作命令。
|
|
35
|
+
* 参数错误抛 CliThreadInputError;领域错误由 Core 抛出,Main 统一映射退出码。
|
|
36
|
+
*/
|
|
37
|
+
export declare function executeThreadCommand(command: CliThreadCommand, scope: CliThreadCommandScope): Promise<CliThreadCommandResult>;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* thread-commands.ts — CLI 协作命令到 Core ThreadFacade 的薄映射
|
|
3
|
+
*
|
|
4
|
+
* 只做正文/文件读取、参数整形与输出行生成;组归属、幂等、容量和交付事实都在 Core。
|
|
5
|
+
* 短命令不启动 Runner,也不猜测其它进程是否在线:回执只表示消息已接受,
|
|
6
|
+
* 真正执行仍由在线宿主 Runner 与 Session lease 决定。
|
|
7
|
+
*/
|
|
8
|
+
/** 命令输入不合法;Main 映射为 usage 退出码,不进入 Runtime。 */
|
|
9
|
+
export class CliThreadInputError extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "CliThreadInputError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** 协作协议版本固定为 1;与 Web/工具共用同一冻结契约。 */
|
|
16
|
+
const THREAD_PROTOCOL_VERSION = 1;
|
|
17
|
+
/** watch 的默认轮询间隔;只影响本地观察节奏,不改变持久事实。 */
|
|
18
|
+
const DEFAULT_WATCH_POLL_MS = 500;
|
|
19
|
+
/** 读取协作正文;空正文在投递前就拒绝,避免把空任务写给目标会话。 */
|
|
20
|
+
async function readContent(command, scope) {
|
|
21
|
+
let content;
|
|
22
|
+
try {
|
|
23
|
+
content = command.contentSource.kind === "file"
|
|
24
|
+
? await scope.readFile(command.contentSource.path)
|
|
25
|
+
: await scope.readStdin();
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
throw new CliThreadInputError("协作消息正文读取失败");
|
|
29
|
+
}
|
|
30
|
+
if (!content.trim())
|
|
31
|
+
throw new CliThreadInputError("协作消息正文不能为空");
|
|
32
|
+
return content;
|
|
33
|
+
}
|
|
34
|
+
/** 读取显式交接摘要;标题由参数给出,正文只从受控文件读取。 */
|
|
35
|
+
async function readHandoff(command, scope) {
|
|
36
|
+
if (!command.handoff)
|
|
37
|
+
return undefined;
|
|
38
|
+
let markdown;
|
|
39
|
+
try {
|
|
40
|
+
markdown = await scope.readFile(command.handoff.markdownFile);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
throw new CliThreadInputError("交接文档读取失败");
|
|
44
|
+
}
|
|
45
|
+
if (!markdown.trim())
|
|
46
|
+
throw new CliThreadInputError("交接文档不能为空");
|
|
47
|
+
return { title: command.handoff.title, markdown };
|
|
48
|
+
}
|
|
49
|
+
/** 读取共享板写入值;只接受合法 JSON,不把裸文本静默当字符串。 */
|
|
50
|
+
async function readStateValue(value, scope) {
|
|
51
|
+
let json;
|
|
52
|
+
if (value.kind === "inline") {
|
|
53
|
+
json = value.json;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
try {
|
|
57
|
+
json = await scope.readFile(value.path);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
throw new CliThreadInputError("共享板取值读取失败");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(json);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
throw new CliThreadInputError("共享板取值需要合法 JSON");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** 可中止的本地等待;中止只结束观察节奏,不影响任何目标执行。 */
|
|
71
|
+
function waitForPoll(ms, signal) {
|
|
72
|
+
if (signal.aborted)
|
|
73
|
+
return Promise.resolve();
|
|
74
|
+
return new Promise(function schedule(resolve) {
|
|
75
|
+
const timer = setTimeout(finish, ms);
|
|
76
|
+
/** 清理定时器与监听,避免中止后残留回调。 */
|
|
77
|
+
function finish() {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
signal.removeEventListener("abort", finish);
|
|
80
|
+
resolve();
|
|
81
|
+
}
|
|
82
|
+
signal.addEventListener("abort", finish, { once: true });
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/** 持续观察组事件;每行一条 JSON,Ctrl+C 只结束观察。 */
|
|
86
|
+
async function watchEvents(command, scope) {
|
|
87
|
+
let cursor = command.cursor;
|
|
88
|
+
while (!scope.signal.aborted) {
|
|
89
|
+
const page = await scope.threads.events({
|
|
90
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
91
|
+
groupId: command.groupId,
|
|
92
|
+
...(cursor ? { cursor } : {}),
|
|
93
|
+
...(command.limit !== undefined ? { limit: command.limit } : {}),
|
|
94
|
+
});
|
|
95
|
+
for (const event of page.items)
|
|
96
|
+
scope.write(JSON.stringify(event));
|
|
97
|
+
cursor = page.cursor;
|
|
98
|
+
if (scope.signal.aborted)
|
|
99
|
+
break;
|
|
100
|
+
// 还有下一页时立即续读,不额外等待。
|
|
101
|
+
if (!page.nextCursor)
|
|
102
|
+
await waitForPoll(scope.pollIntervalMs ?? DEFAULT_WATCH_POLL_MS, scope.signal);
|
|
103
|
+
}
|
|
104
|
+
return { exitCode: 0 };
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 执行一条协作命令。
|
|
108
|
+
* 参数错误抛 CliThreadInputError;领域错误由 Core 抛出,Main 统一映射退出码。
|
|
109
|
+
*/
|
|
110
|
+
export async function executeThreadCommand(command, scope) {
|
|
111
|
+
if (command.type === "group_of")
|
|
112
|
+
return { exitCode: 0, result: await scope.threads.groupOf({ protocolVersion: THREAD_PROTOCOL_VERSION, threadId: command.threadId }) };
|
|
113
|
+
if (command.type === "group_enable") {
|
|
114
|
+
return { exitCode: 0, result: await scope.threads.enable({ protocolVersion: THREAD_PROTOCOL_VERSION, requestId: command.requestId, rootThreadId: command.rootThreadId }) };
|
|
115
|
+
}
|
|
116
|
+
if (command.type === "group_disable") {
|
|
117
|
+
return { exitCode: 0, result: await scope.threads.disable({ protocolVersion: THREAD_PROTOCOL_VERSION, requestId: command.requestId, groupId: command.groupId }) };
|
|
118
|
+
}
|
|
119
|
+
if (command.type === "create") {
|
|
120
|
+
return {
|
|
121
|
+
exitCode: 0,
|
|
122
|
+
result: await scope.threads.create({
|
|
123
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
124
|
+
requestId: command.requestId,
|
|
125
|
+
groupId: command.groupId,
|
|
126
|
+
parentThreadId: command.parentThreadId,
|
|
127
|
+
...(command.title !== undefined ? { title: command.title } : {}),
|
|
128
|
+
...(command.roleLabel !== undefined ? { roleLabel: command.roleLabel } : {}),
|
|
129
|
+
...(command.modelRef ? { modelRef: command.modelRef } : {}),
|
|
130
|
+
...(command.reasoningEffort !== undefined ? { reasoningEffort: command.reasoningEffort } : {}),
|
|
131
|
+
}),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
if (command.type === "bind_host") {
|
|
135
|
+
return { exitCode: 0, result: await scope.threads.bindExecutionHost({ protocolVersion: THREAD_PROTOCOL_VERSION, groupId: command.groupId, threadId: command.threadId, expectedRevision: command.expectedRevision, executionHost: command.executionHost }) };
|
|
136
|
+
}
|
|
137
|
+
if (command.type === "associate") {
|
|
138
|
+
return { exitCode: 0, result: await scope.threads.associate({ protocolVersion: THREAD_PROTOCOL_VERSION, groupId: command.groupId, threadId: command.threadId, requestId: command.requestId }) };
|
|
139
|
+
}
|
|
140
|
+
if (command.type === "list") {
|
|
141
|
+
return {
|
|
142
|
+
exitCode: 0,
|
|
143
|
+
result: await scope.threads.list({
|
|
144
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
145
|
+
groupId: command.groupId,
|
|
146
|
+
...(command.cursor ? { cursor: command.cursor } : {}),
|
|
147
|
+
...(command.limit !== undefined ? { limit: command.limit } : {}),
|
|
148
|
+
}),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (command.type === "read") {
|
|
152
|
+
return {
|
|
153
|
+
exitCode: 0,
|
|
154
|
+
result: await scope.threads.read({
|
|
155
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
156
|
+
groupId: command.groupId,
|
|
157
|
+
threadId: command.threadId,
|
|
158
|
+
...(command.cursor ? { cursor: command.cursor } : {}),
|
|
159
|
+
...(command.limit !== undefined ? { limit: command.limit } : {}),
|
|
160
|
+
}),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (command.type === "send") {
|
|
164
|
+
const content = await readContent(command, scope);
|
|
165
|
+
const handoff = await readHandoff(command, scope);
|
|
166
|
+
return {
|
|
167
|
+
exitCode: 0,
|
|
168
|
+
result: await scope.threads.send({
|
|
169
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
170
|
+
requestId: command.requestId,
|
|
171
|
+
groupId: command.groupId,
|
|
172
|
+
threadId: command.threadId,
|
|
173
|
+
mode: command.mode,
|
|
174
|
+
content,
|
|
175
|
+
...(command.replyTo !== undefined ? { replyTo: command.replyTo } : {}),
|
|
176
|
+
...(command.expectedTurnId !== undefined ? { expectedTurnId: command.expectedTurnId } : {}),
|
|
177
|
+
...(handoff ? { handoff } : {}),
|
|
178
|
+
}),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
if (command.type === "wait") {
|
|
182
|
+
return {
|
|
183
|
+
exitCode: 0,
|
|
184
|
+
result: await scope.threads.wait({
|
|
185
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
186
|
+
groupId: command.groupId,
|
|
187
|
+
targets: command.targets,
|
|
188
|
+
timeoutMs: command.timeoutMs,
|
|
189
|
+
signal: scope.signal,
|
|
190
|
+
}),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
if (command.type === "state_read") {
|
|
194
|
+
return { exitCode: 0, result: await scope.threads.readState({ protocolVersion: THREAD_PROTOCOL_VERSION, groupId: command.groupId }) };
|
|
195
|
+
}
|
|
196
|
+
if (command.type === "state_set") {
|
|
197
|
+
return {
|
|
198
|
+
exitCode: 0,
|
|
199
|
+
result: await scope.threads.updateState({
|
|
200
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
201
|
+
requestId: command.requestId,
|
|
202
|
+
groupId: command.groupId,
|
|
203
|
+
expectedRevision: command.expectedRevision,
|
|
204
|
+
operations: [{ op: "set", key: command.key, value: await readStateValue(command.value, scope) }],
|
|
205
|
+
}),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (command.type === "state_remove") {
|
|
209
|
+
return {
|
|
210
|
+
exitCode: 0,
|
|
211
|
+
result: await scope.threads.updateState({
|
|
212
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
213
|
+
requestId: command.requestId,
|
|
214
|
+
groupId: command.groupId,
|
|
215
|
+
expectedRevision: command.expectedRevision,
|
|
216
|
+
operations: [{ op: "remove", key: command.key }],
|
|
217
|
+
}),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
if (command.type === "interrupt") {
|
|
221
|
+
return {
|
|
222
|
+
exitCode: 0,
|
|
223
|
+
result: await scope.threads.interrupt({
|
|
224
|
+
protocolVersion: THREAD_PROTOCOL_VERSION,
|
|
225
|
+
requestId: command.requestId,
|
|
226
|
+
groupId: command.groupId,
|
|
227
|
+
threadId: command.threadId,
|
|
228
|
+
expectedTurnId: command.expectedTurnId,
|
|
229
|
+
}),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
return watchEvents(command, scope);
|
|
233
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* thread-host.ts — CLI 常驻协作宿主
|
|
3
|
+
*
|
|
4
|
+
* 只由常驻 `ap` 会话装配:发现已启用组后启动 Node Runner,用既有 turns 驱动目标会话;
|
|
5
|
+
* 短命令与 watch 不启动本宿主。退出时停止本进程持有的驱动并报告未确认项,
|
|
6
|
+
* 不假称目标已完成,也不接管其他 owner 的 Turn。
|
|
7
|
+
*/
|
|
8
|
+
import { type NodeThreadRunner, type NodeThreadRunnerStopReport } from "@autopilot-agent/adapters";
|
|
9
|
+
import type { AgentRuntime, RuntimePorts } from "@autopilot-agent/core";
|
|
10
|
+
/** 创建常驻宿主所需的受信事实;全部来自 Composition Root 与当前 Runtime。 */
|
|
11
|
+
export interface CreateCliThreadHostOptions {
|
|
12
|
+
workspaceId: string;
|
|
13
|
+
stateRootId: string;
|
|
14
|
+
runtime: AgentRuntime;
|
|
15
|
+
ports: RuntimePorts;
|
|
16
|
+
/** 组发现间隔;未启用协作的工作区不做执行轮询。 */
|
|
17
|
+
discoverIntervalMs?: number;
|
|
18
|
+
/** 单轮调度异常的观测回调;不改变持久事实。 */
|
|
19
|
+
onError?: import("@autopilot-agent/adapters").NodeThreadRunnerOptions["onError"];
|
|
20
|
+
}
|
|
21
|
+
/** 常驻宿主;是否在驱动由真实启动状态决定,不由调用方猜测。 */
|
|
22
|
+
export interface CliThreadHost {
|
|
23
|
+
/** 开始发现并驱动;重复调用无副作用。 */
|
|
24
|
+
start(): void;
|
|
25
|
+
/** 停止发现与驱动,返回停机报告;从未启动时返回 undefined。 */
|
|
26
|
+
stop(): Promise<NodeThreadRunnerStopReport | undefined>;
|
|
27
|
+
registerExecution: NodeThreadRunner["registerExecution"];
|
|
28
|
+
isRunning(): boolean;
|
|
29
|
+
}
|
|
30
|
+
/** 创建 CLI 常驻协作宿主;构造只装配,不启动轮询。 */
|
|
31
|
+
export declare function createCliThreadHost(options: CreateCliThreadHostOptions): CliThreadHost;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* thread-host.ts — CLI 常驻协作宿主
|
|
3
|
+
*
|
|
4
|
+
* 只由常驻 `ap` 会话装配:发现已启用组后启动 Node Runner,用既有 turns 驱动目标会话;
|
|
5
|
+
* 短命令与 watch 不启动本宿主。退出时停止本进程持有的驱动并报告未确认项,
|
|
6
|
+
* 不假称目标已完成,也不接管其他 owner 的 Turn。
|
|
7
|
+
*/
|
|
8
|
+
import { createNodeThreadRunner } from "@autopilot-agent/adapters";
|
|
9
|
+
import { createThreadDeliveryService } from "@autopilot-agent/core";
|
|
10
|
+
/** 创建 CLI 常驻协作宿主;构造只装配,不启动轮询。 */
|
|
11
|
+
export function createCliThreadHost(options) {
|
|
12
|
+
const delivery = createThreadDeliveryService(options.ports, options.ports.threadStore, {
|
|
13
|
+
workspaceId: options.workspaceId,
|
|
14
|
+
executionHost: "cli-work-v1",
|
|
15
|
+
stateRootId: options.stateRootId,
|
|
16
|
+
// 同一进程重启视为新 owner,旧领取由 Runner 首轮 reconcile 收敛。
|
|
17
|
+
ownerId: `cli:${options.workspaceId}:${process.pid}`,
|
|
18
|
+
});
|
|
19
|
+
const runner = createNodeThreadRunner({
|
|
20
|
+
delivery,
|
|
21
|
+
runtime: options.runtime,
|
|
22
|
+
ports: options.ports,
|
|
23
|
+
...(options.onError ? { onError: options.onError } : {}),
|
|
24
|
+
});
|
|
25
|
+
let started = false;
|
|
26
|
+
let discovering = false;
|
|
27
|
+
let discovery;
|
|
28
|
+
/** 发现已启用组后接管驱动;没有组时保持轻量复查,不做执行轮询。 */
|
|
29
|
+
async function discover() {
|
|
30
|
+
if (started || discovering)
|
|
31
|
+
return;
|
|
32
|
+
discovering = true;
|
|
33
|
+
try {
|
|
34
|
+
const groups = await options.ports.threadStore.listGroups();
|
|
35
|
+
if (groups.length === 0)
|
|
36
|
+
return;
|
|
37
|
+
started = true;
|
|
38
|
+
if (discovery) {
|
|
39
|
+
clearInterval(discovery);
|
|
40
|
+
discovery = undefined;
|
|
41
|
+
}
|
|
42
|
+
await runner.start();
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
discovering = false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
start: function start() {
|
|
50
|
+
if (discovery || started)
|
|
51
|
+
return;
|
|
52
|
+
/** 单次发现失败不阻断会话;保持复查,异常只交给宿主观测。 */
|
|
53
|
+
function attempt() {
|
|
54
|
+
void discover().catch(function report(error) { options.onError?.(error); });
|
|
55
|
+
}
|
|
56
|
+
attempt();
|
|
57
|
+
discovery = setInterval(attempt, options.discoverIntervalMs ?? 5_000);
|
|
58
|
+
// 发现定时器不应单独阻止进程退出。
|
|
59
|
+
discovery.unref?.();
|
|
60
|
+
},
|
|
61
|
+
stop: async function stop() {
|
|
62
|
+
if (discovery) {
|
|
63
|
+
clearInterval(discovery);
|
|
64
|
+
discovery = undefined;
|
|
65
|
+
}
|
|
66
|
+
if (!started)
|
|
67
|
+
return undefined;
|
|
68
|
+
started = false;
|
|
69
|
+
return runner.stop();
|
|
70
|
+
},
|
|
71
|
+
registerExecution: runner.registerExecution,
|
|
72
|
+
isRunning: function isRunning() {
|
|
73
|
+
return started;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
package/dist/timeline-lines.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/** TUI 时间线视觉行模型;统一文字换行、样式片段与源文本坐标,暂不接入渲染。 */
|
|
2
2
|
import stringWidth from "string-width";
|
|
3
|
-
import wrapAnsi from "wrap-ansi";
|
|
4
3
|
/** 将一个样式范围裁剪到当前视觉行,保留行内坐标。 */
|
|
5
4
|
function clipTimelineSpans(spans, sourceStart, sourceEnd) {
|
|
6
5
|
return spans.flatMap(function clipSpan(span) {
|
|
@@ -9,39 +8,48 @@ function clipTimelineSpans(spans, sourceStart, sourceEnd) {
|
|
|
9
8
|
return start < end ? [{ ...span, start: start - sourceStart, end: end - sourceStart }] : [];
|
|
10
9
|
});
|
|
11
10
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
15
|
-
for (const segment of segmenter.segment(source)) {
|
|
16
|
-
const end = segment.index + segment.segment.length;
|
|
17
|
-
if (segment.index < offset && offset < end)
|
|
18
|
-
return end;
|
|
19
|
-
}
|
|
20
|
-
return offset;
|
|
21
|
-
}
|
|
22
|
-
/** 复用现有 wrap-ansi 规则生成视觉行,同时把被截断的 grapheme 收拢到同一行。 */
|
|
11
|
+
const timelineSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
12
|
+
/** 一次按词与grapheme顺序换行;ANSI为零宽原子,源坐标不含合成控制序列。 */
|
|
23
13
|
function wrapTimelineSource(source, columns) {
|
|
24
14
|
const lines = [];
|
|
15
|
+
let lineStart = 0;
|
|
25
16
|
let offset = 0;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
17
|
+
let width = 0;
|
|
18
|
+
/** 只切源字符串一次,不从头扫描已处理的grapheme。 */
|
|
19
|
+
function emit(end) {
|
|
20
|
+
lines.push({ text: source.slice(lineStart, end), sourceStart: lineStart, sourceEnd: end });
|
|
21
|
+
lineStart = end;
|
|
22
|
+
width = 0;
|
|
23
|
+
}
|
|
24
|
+
const tokens = source.matchAll(/\n| |[^ \n]+/gu);
|
|
25
|
+
for (const token of tokens) {
|
|
26
|
+
const word = token[0];
|
|
27
|
+
if (word === "\n") {
|
|
28
|
+
emit(offset);
|
|
29
|
+
offset++;
|
|
30
|
+
lineStart = offset;
|
|
33
31
|
continue;
|
|
34
32
|
}
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
33
|
+
const wordWidth = stringWidth(word);
|
|
34
|
+
if (word === " " && width >= columns)
|
|
35
|
+
emit(offset);
|
|
36
|
+
else if (word !== " " && width > 0 && (wordWidth <= columns ? width + wordWidth > columns : Math.floor((wordWidth - 1) / columns) < Math.floor((wordWidth - (columns - width) - 1) / columns) + 1))
|
|
37
|
+
emit(offset);
|
|
38
|
+
const segments = timelineSegmenter.segment(word);
|
|
39
|
+
let local = 0;
|
|
40
|
+
while (local < word.length) {
|
|
41
|
+
const escape = word.charCodeAt(local) === 27 ? /^(?:\u001b\[[0-?]*[ -/]*[@-~]|\u001b\][^\u0007]*(?:\u0007|\u001b\\))/u.exec(word.slice(local))?.[0] : undefined;
|
|
42
|
+
const character = escape ?? (word.charCodeAt(local) >= 32 && word.charCodeAt(local) < 127 && (local + 1 === word.length || word.charCodeAt(local + 1) < 127) ? word[local] : segments.containing(local).segment);
|
|
43
|
+
const characterWidth = escape ? 0 : stringWidth(character);
|
|
44
|
+
if (width > 0 && width + characterWidth > columns)
|
|
45
|
+
emit(offset);
|
|
46
|
+
width += characterWidth;
|
|
47
|
+
local += character.length;
|
|
48
|
+
offset += character.length;
|
|
49
|
+
}
|
|
42
50
|
}
|
|
43
|
-
if (source.endsWith("\n"))
|
|
44
|
-
|
|
51
|
+
if (offset > lineStart || source.endsWith("\n"))
|
|
52
|
+
emit(offset);
|
|
45
53
|
return lines;
|
|
46
54
|
}
|
|
47
55
|
/** 将安全时间线文本按当前终端列宽转换为统一的视觉行。 */
|
package/dist/tui/app.d.ts
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
1
|
/** 瘦身后的 Ink 应用组件:编排 store 订阅、dispatchAction、动画时钟与渲染。 */
|
|
2
2
|
import { type ReactNode } from "react";
|
|
3
|
+
import { type Key } from "ink";
|
|
3
4
|
import type { CliController, CliControllerResult } from "../controller.js";
|
|
4
5
|
import type { CliExitCode } from "../types.js";
|
|
6
|
+
import type { CliTuiDraftRef } from "./hooks/use-prompt-input.js";
|
|
5
7
|
import type { CliTuiInitialOperation, CliTuiStore, TuiTerminalSizeSource, TuiMouseInputSink, TuiRenderMode } from "./types.js";
|
|
6
8
|
/** Ink 应用组件;useInput 只把用户动作交给 Controller Action。 */
|
|
7
9
|
export declare function CliTuiApp(props: {
|
|
8
10
|
store: CliTuiStore;
|
|
11
|
+
inlineOutputKey?: string;
|
|
12
|
+
sessionTitle?: string;
|
|
13
|
+
interactionLabel?: string;
|
|
14
|
+
/** 保活后台子树时禁止所有输入处理器与鼠标绑定;不依赖display:none隔离副作用。 */
|
|
15
|
+
visible?: boolean;
|
|
16
|
+
inputActive?: boolean;
|
|
17
|
+
viewState?: {
|
|
18
|
+
draftRef: CliTuiDraftRef;
|
|
19
|
+
initialStarted: boolean;
|
|
20
|
+
lastSubmittedPrompt: {
|
|
21
|
+
current: string | undefined;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** 每次事件再核对实时会话/列表模式,避免React尚未commit时旧处理器消费输入。 */
|
|
25
|
+
canReceiveInput?(value?: string, key?: Key): boolean;
|
|
26
|
+
bindInputHandler?(handler: ((value: string, key: Key) => void) | undefined): void;
|
|
27
|
+
onDraftAttachmentsChange?(names: string[]): void;
|
|
28
|
+
escapeClosesPanel?: boolean;
|
|
29
|
+
availableColumns?: number;
|
|
30
|
+
availableRows?: number;
|
|
9
31
|
controller: CliController;
|
|
10
32
|
initial?: CliTuiInitialOperation;
|
|
11
33
|
closeWhenIdle: boolean;
|