ycodium-acp-executor 0.2.5542 → 0.2.5751
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 +5 -4
- package/dist/_private/ycodium-jsonl-stream/appendedJsonl.d.ts +47 -0
- package/dist/_private/ycodium-jsonl-stream/appendedJsonl.js +172 -0
- package/dist/_private/ycodium-jsonl-stream/claudeSessions.d.ts +63 -0
- package/dist/_private/ycodium-jsonl-stream/claudeSessions.js +220 -0
- package/dist/_private/ycodium-jsonl-stream/codexSessions.d.ts +41 -0
- package/dist/_private/ycodium-jsonl-stream/codexSessions.js +197 -0
- package/dist/_private/ycodium-jsonl-stream/history.d.ts +6 -0
- package/dist/_private/ycodium-jsonl-stream/history.js +389 -0
- package/dist/_private/ycodium-jsonl-stream/historyErrors.d.ts +6 -0
- package/dist/_private/ycodium-jsonl-stream/historyErrors.js +6 -0
- package/dist/_private/ycodium-jsonl-stream/historyFile.d.ts +19 -0
- package/dist/_private/ycodium-jsonl-stream/historyFile.js +117 -0
- package/dist/_private/ycodium-jsonl-stream/historyRecord.d.ts +31 -0
- package/dist/_private/ycodium-jsonl-stream/historyRecord.js +144 -0
- package/dist/_private/ycodium-jsonl-stream/historyWaiting.d.ts +8 -0
- package/dist/_private/ycodium-jsonl-stream/historyWaiting.js +142 -0
- package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.js +18 -0
- package/dist/_private/ycodium-jsonl-stream/jsonlStream.d.ts +79 -0
- package/dist/_private/ycodium-jsonl-stream/jsonlStream.js +603 -0
- package/dist/_private/ycodium-jsonl-stream/scanCache.d.ts +69 -0
- package/dist/_private/ycodium-jsonl-stream/scanCache.js +106 -0
- package/dist/_private/ycodium-jsonl-stream/sessionDirectory.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/sessionDirectory.js +32 -0
- package/dist/_private/ycodium-jsonl-stream/sessionTranscript.d.ts +3 -1
- package/dist/_private/ycodium-jsonl-stream/sessionTranscript.js +13 -0
- package/dist/_private/ycodium-jsonl-stream/usageFields.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/usageFields.js +11 -0
- package/dist/_private/ycodium-workspace-path/directoryScope.d.ts +5 -0
- package/dist/_private/ycodium-workspace-path/directoryScope.js +28 -0
- package/dist/_private/ycodium-workspace-path/workspacePath.d.ts +19 -0
- package/dist/_private/ycodium-workspace-path/workspacePath.js +90 -0
- package/dist/adapter-support.d.ts.map +1 -1
- package/dist/adapter-support.js.map +1 -1
- package/dist/apply-effort.d.ts.map +1 -1
- package/dist/apply-effort.js.map +1 -1
- package/dist/approvals.d.ts.map +1 -1
- package/dist/approvals.js.map +1 -1
- package/dist/artifacts.d.ts.map +1 -1
- package/dist/artifacts.js.map +1 -1
- package/dist/card-defaults.d.ts.map +1 -1
- package/dist/card-defaults.js.map +1 -1
- package/dist/config-option-wiring.d.ts.map +1 -1
- package/dist/config-option-wiring.js.map +1 -1
- package/dist/config-options.d.ts.map +1 -1
- package/dist/config-options.js.map +1 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/event-stream.d.ts.map +1 -1
- package/dist/event-stream.js.map +1 -1
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js.map +1 -1
- package/dist/executor/child-sessions.d.ts.map +1 -1
- package/dist/executor/child-sessions.js.map +1 -1
- package/dist/executor/lifecycle.d.ts.map +1 -1
- package/dist/executor/lifecycle.js.map +1 -1
- package/dist/executor/prompt-results.d.ts.map +1 -1
- package/dist/executor/prompt-results.js.map +1 -1
- package/dist/executor/prompt.d.ts.map +1 -1
- package/dist/executor/prompt.js.map +1 -1
- package/dist/executor/requests.d.ts.map +1 -1
- package/dist/executor/requests.js.map +1 -1
- package/dist/executor/runtime.d.ts.map +1 -1
- package/dist/executor/runtime.js.map +1 -1
- package/dist/executor/session.d.ts.map +1 -1
- package/dist/executor/session.js +17 -1
- package/dist/executor/session.js.map +1 -1
- package/dist/executor/settlement.d.ts.map +1 -1
- package/dist/executor/settlement.js.map +1 -1
- package/dist/executor/subagent-sessions.d.ts.map +1 -1
- package/dist/executor/subagent-sessions.js.map +1 -1
- package/dist/executor/support.d.ts.map +1 -1
- package/dist/executor/support.js.map +1 -1
- package/dist/executor/updates.d.ts.map +1 -1
- package/dist/executor/updates.js +11 -0
- package/dist/executor/updates.js.map +1 -1
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js.map +1 -1
- package/dist/failureCategory.d.ts.map +1 -1
- package/dist/failureCategory.js.map +1 -1
- package/dist/handshake.d.ts.map +1 -1
- package/dist/handshake.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/instance-kit.d.ts.map +1 -1
- package/dist/instance-kit.js.map +1 -1
- package/dist/jsonrpc.d.ts.map +1 -1
- package/dist/jsonrpc.js.map +1 -1
- package/dist/line-buffer.d.ts.map +1 -1
- package/dist/line-buffer.js.map +1 -1
- package/dist/mcp-servers.d.ts +26 -4
- package/dist/mcp-servers.d.ts.map +1 -1
- package/dist/mcp-servers.js +25 -4
- package/dist/mcp-servers.js.map +1 -1
- package/dist/model-selection.d.ts.map +1 -1
- package/dist/model-selection.js.map +1 -1
- package/dist/model-send.d.ts.map +1 -1
- package/dist/model-send.js.map +1 -1
- package/dist/mutex.d.ts.map +1 -1
- package/dist/mutex.js.map +1 -1
- package/dist/native-session-transcript.d.ts.map +1 -1
- package/dist/native-session-transcript.js.map +1 -1
- package/dist/native-sessions.d.ts.map +1 -1
- package/dist/native-sessions.js.map +1 -1
- package/dist/openCommands.d.ts.map +1 -1
- package/dist/openCommands.js.map +1 -1
- package/dist/permission-statement.d.ts.map +1 -1
- package/dist/permission-statement.js.map +1 -1
- package/dist/probe-process.d.ts +9 -5
- package/dist/probe-process.d.ts.map +1 -1
- package/dist/probe-process.js +4 -0
- package/dist/probe-process.js.map +1 -1
- package/dist/probe-session.d.ts.map +1 -1
- package/dist/probe-session.js.map +1 -1
- package/dist/probe.d.ts.map +1 -1
- package/dist/probe.js.map +1 -1
- package/dist/prompt-dispatch.d.ts.map +1 -1
- package/dist/prompt-dispatch.js.map +1 -1
- package/dist/protocol-log.d.ts.map +1 -1
- package/dist/protocol-log.js.map +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/refreshDeadline.d.ts.map +1 -1
- package/dist/refreshDeadline.js.map +1 -1
- package/dist/runtime-mode.d.ts.map +1 -1
- package/dist/runtime-mode.js.map +1 -1
- package/dist/runtime-model.d.ts +4 -0
- package/dist/runtime-model.d.ts.map +1 -1
- package/dist/runtime-model.js +13 -0
- package/dist/runtime-model.js.map +1 -1
- package/dist/session-cleanup.d.ts.map +1 -1
- package/dist/session-cleanup.js.map +1 -1
- package/dist/session-registry.d.ts.map +1 -1
- package/dist/session-registry.js.map +1 -1
- package/dist/session-update.d.ts.map +1 -1
- package/dist/session-update.js.map +1 -1
- package/dist/sessionProcess.d.ts.map +1 -1
- package/dist/sessionProcess.js.map +1 -1
- package/dist/source/_private/ycodium-executor-common/catalogModel.ts +40 -0
- package/dist/source/_private/ycodium-executor-common/eventTypes.ts +97 -0
- package/dist/source/_private/ycodium-executor-common/modelSelection.ts +24 -0
- package/dist/source/_private/ycodium-executor-common/nodeStreams.ts +212 -0
- package/dist/source/_private/ycodium-executor-common/raceAbort.ts +25 -0
- package/dist/source/_private/ycodium-executor-common/reasoningLevelFallback.ts +28 -0
- package/dist/source/_private/ycodium-executor-common/stderrTail.ts +27 -0
- package/dist/source/_private/ycodium-executor-common/toolPresentation.ts +268 -0
- package/dist/source/_private/ycodium-jsonl-stream/appendedJsonl.ts +222 -0
- package/dist/source/_private/ycodium-jsonl-stream/claudeSessions.ts +263 -0
- package/dist/source/_private/ycodium-jsonl-stream/codexSessions.ts +254 -0
- package/dist/source/_private/ycodium-jsonl-stream/history.ts +492 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyErrors.ts +6 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyFile.ts +139 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyRecord.ts +180 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyWaiting.ts +140 -0
- package/dist/source/_private/ycodium-jsonl-stream/jsonObjectLine.ts +15 -0
- package/dist/source/_private/ycodium-jsonl-stream/jsonlStream.ts +650 -0
- package/dist/source/_private/ycodium-jsonl-stream/scanCache.ts +163 -0
- package/dist/source/_private/ycodium-jsonl-stream/sessionDirectory.ts +39 -0
- package/dist/source/_private/ycodium-jsonl-stream/sessionTranscript.ts +136 -0
- package/dist/source/_private/ycodium-jsonl-stream/usageFields.ts +13 -0
- package/dist/source/_private/ycodium-workspace-path/directoryScope.ts +34 -0
- package/dist/source/_private/ycodium-workspace-path/workspacePath.ts +110 -0
- package/dist/source/adapter-support.ts +115 -0
- package/dist/source/apply-effort.ts +66 -0
- package/dist/source/approvals.ts +94 -0
- package/dist/source/artifacts.ts +88 -0
- package/dist/source/card-defaults.ts +25 -0
- package/dist/source/config-option-wiring.ts +119 -0
- package/dist/source/config-options.ts +270 -0
- package/dist/source/connection.ts +553 -0
- package/dist/source/errors.ts +207 -0
- package/dist/source/event-stream.ts +91 -0
- package/dist/source/events.ts +341 -0
- package/dist/source/executor/child-sessions.ts +87 -0
- package/dist/source/executor/lifecycle.ts +282 -0
- package/dist/source/executor/prompt-results.ts +123 -0
- package/dist/source/executor/prompt.ts +606 -0
- package/dist/source/executor/requests.ts +323 -0
- package/dist/source/executor/runtime.ts +137 -0
- package/dist/source/executor/session.ts +553 -0
- package/dist/source/executor/settlement.ts +92 -0
- package/dist/source/executor/subagent-sessions.ts +183 -0
- package/dist/source/executor/support.ts +129 -0
- package/dist/source/executor/updates.ts +427 -0
- package/dist/source/executor.ts +537 -0
- package/dist/source/failureCategory.ts +74 -0
- package/dist/source/handshake.ts +66 -0
- package/dist/source/index.ts +210 -0
- package/dist/source/instance-kit.ts +478 -0
- package/dist/source/jsonrpc.ts +59 -0
- package/dist/source/line-buffer.ts +54 -0
- package/dist/source/mcp-servers.ts +83 -0
- package/dist/source/model-selection.ts +138 -0
- package/dist/source/model-send.ts +80 -0
- package/dist/source/mutex.ts +47 -0
- package/dist/source/native-session-transcript.ts +167 -0
- package/dist/source/native-sessions.ts +271 -0
- package/dist/source/openCommands.ts +39 -0
- package/dist/source/permission-statement.ts +49 -0
- package/dist/source/probe-process.ts +95 -0
- package/dist/source/probe-session.ts +81 -0
- package/dist/source/probe.ts +261 -0
- package/dist/source/prompt-dispatch.ts +42 -0
- package/dist/source/protocol-log.ts +143 -0
- package/dist/source/protocol.ts +36 -0
- package/dist/source/refreshDeadline.ts +2 -0
- package/dist/source/runtime-mode.ts +125 -0
- package/dist/source/runtime-model.ts +598 -0
- package/dist/source/session-cleanup.ts +82 -0
- package/dist/source/session-registry.ts +62 -0
- package/dist/source/session-update.ts +88 -0
- package/dist/source/sessionProcess.ts +29 -0
- package/dist/source/start-abort.ts +19 -0
- package/dist/source/thread-operations.ts +145 -0
- package/dist/source/tool-call-state.ts +322 -0
- package/dist/source/tool-presentation.ts +20 -0
- package/dist/source/tool-subagents.ts +491 -0
- package/dist/source/turn-usage.ts +78 -0
- package/dist/source/types.ts +146 -0
- package/dist/source/user-input-pending.ts +31 -0
- package/dist/source/util.ts +38 -0
- package/dist/source/vendor-hooks.ts +394 -0
- package/dist/start-abort.d.ts.map +1 -1
- package/dist/start-abort.js.map +1 -1
- package/dist/thread-operations.d.ts.map +1 -1
- package/dist/thread-operations.js.map +1 -1
- package/dist/tool-call-state.d.ts.map +1 -1
- package/dist/tool-call-state.js.map +1 -1
- package/dist/tool-presentation.d.ts.map +1 -1
- package/dist/tool-presentation.js.map +1 -1
- package/dist/tool-subagents.d.ts +1 -0
- package/dist/tool-subagents.d.ts.map +1 -1
- package/dist/tool-subagents.js +2 -0
- package/dist/tool-subagents.js.map +1 -1
- package/dist/turn-usage.d.ts.map +1 -1
- package/dist/turn-usage.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/user-input-pending.d.ts.map +1 -1
- package/dist/user-input-pending.js.map +1 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js.map +1 -1
- package/dist/vendor-hooks.d.ts +11 -3
- package/dist/vendor-hooks.d.ts.map +1 -1
- package/dist/vendor-hooks.js.map +1 -1
- package/package.json +5 -2
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// @effect-diagnostics globalTimers:off
|
|
2
|
+
/**
|
|
3
|
+
* 探测用的一次性 ACP 握手:连上、`initialize`、把连接交给调用方问一句,然后收工。
|
|
4
|
+
*
|
|
5
|
+
* **为什么不用 `createAcpExecutor`**:那把状态机是为「一条真会话」造的——要 threadId、
|
|
6
|
+
* 发事件、管审批与轮次收口。探测只要目录,多出来的生命周期全是负担。这里只共享
|
|
7
|
+
* 分帧(`createAcpConnection`)与 `initialize` 载荷(`acpInitializePayload`),
|
|
8
|
+
* 两处都不许各写一份。
|
|
9
|
+
*
|
|
10
|
+
* **调用方管进程**:spawn 与 kill 留在各插件手里(宿主的 `host.process.spawn` 只有
|
|
11
|
+
* 它们够得着)。这里只保证一件事——任何一步失败或超时都抛出去,让调用方的 `finally`
|
|
12
|
+
* 有机会杀进程;绝不把挂住的 promise 留给状态刷新。例外是 `--version`、`kimi provider
|
|
13
|
+
* list` 那种一次性命令({@link collectProbeProcess}):spawn 仍由调用方做,跑完就地杀掉。
|
|
14
|
+
*
|
|
15
|
+
* agent 反过来问客户端的事(权限、读文件)一律按 `method not found` 回:不挂
|
|
16
|
+
* `onRequest`,协议库就是这个默认。探测替用户批准任何东西都是错的。
|
|
17
|
+
*/
|
|
18
|
+
import type * as NodeStream from "node:stream";
|
|
19
|
+
|
|
20
|
+
import { createAcpConnection, type AcpConnection } from "./protocol.ts";
|
|
21
|
+
|
|
22
|
+
import { acpCapabilityOverrides, acpInitializePayload, type AcpClientInfo } from "./handshake.ts";
|
|
23
|
+
import { asTrimmedString, isRecord } from "./util.ts";
|
|
24
|
+
|
|
25
|
+
export type AcpProbeHandshake = {
|
|
26
|
+
/** `initialize` 的原样回包。厂商把目录挂在哪一位各家不同,由调用方自己认。 */
|
|
27
|
+
readonly initializeResult: unknown;
|
|
28
|
+
/** 再问一句(`session/list`、`session/new`…)。同一个超时口径。 */
|
|
29
|
+
readonly request: (method: string, params?: unknown) => Promise<unknown>;
|
|
30
|
+
readonly close: () => Promise<void>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** 超时后 promise 就地作废;挂着的请求由调用方杀进程时一起收掉。 */
|
|
34
|
+
function withProbeTimeout<T>(
|
|
35
|
+
promise: Promise<T>,
|
|
36
|
+
timeoutMs: number,
|
|
37
|
+
what: string,
|
|
38
|
+
signal?: AbortSignal,
|
|
39
|
+
): Promise<T> {
|
|
40
|
+
signal?.throwIfAborted();
|
|
41
|
+
return new Promise<T>((resolve, reject) => {
|
|
42
|
+
const onAbort = () => {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
reject(signal?.reason ?? new Error("aborted"));
|
|
45
|
+
};
|
|
46
|
+
const timer = setTimeout(() => {
|
|
47
|
+
signal?.removeEventListener("abort", onAbort);
|
|
48
|
+
reject(new Error(`${what} timed out after ${timeoutMs}ms`));
|
|
49
|
+
}, timeoutMs);
|
|
50
|
+
timer.unref?.();
|
|
51
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
52
|
+
promise.then(
|
|
53
|
+
(value) => {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
signal?.removeEventListener("abort", onAbort);
|
|
56
|
+
resolve(value);
|
|
57
|
+
},
|
|
58
|
+
(error: unknown) => {
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
signal?.removeEventListener("abort", onAbort);
|
|
61
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function openAcpProbeHandshake(input: {
|
|
68
|
+
readonly readable: NodeStream.Readable;
|
|
69
|
+
readonly writable: NodeStream.Writable;
|
|
70
|
+
readonly clientInfo: AcpClientInfo;
|
|
71
|
+
readonly requestTimeoutMs: number;
|
|
72
|
+
readonly signal?: AbortSignal;
|
|
73
|
+
/** 与 `AcpExecutorOptions.subagentSessions` 同义;探测与会话必须传同一个值。 */
|
|
74
|
+
readonly subagentSessions?: boolean;
|
|
75
|
+
}): Promise<AcpProbeHandshake> {
|
|
76
|
+
const connection: AcpConnection = createAcpConnection({
|
|
77
|
+
readable: input.readable,
|
|
78
|
+
writable: input.writable,
|
|
79
|
+
onNotification: () => undefined,
|
|
80
|
+
});
|
|
81
|
+
try {
|
|
82
|
+
const initializeResult = await withProbeTimeout(
|
|
83
|
+
connection.request(
|
|
84
|
+
"initialize",
|
|
85
|
+
acpInitializePayload(
|
|
86
|
+
input.clientInfo,
|
|
87
|
+
acpCapabilityOverrides({ subagentSessions: input.subagentSessions }),
|
|
88
|
+
),
|
|
89
|
+
),
|
|
90
|
+
input.requestTimeoutMs,
|
|
91
|
+
"ACP probe request 'initialize'",
|
|
92
|
+
input.signal,
|
|
93
|
+
);
|
|
94
|
+
return {
|
|
95
|
+
initializeResult,
|
|
96
|
+
request: (method, params) =>
|
|
97
|
+
withProbeTimeout(
|
|
98
|
+
connection.request(method, params),
|
|
99
|
+
input.requestTimeoutMs,
|
|
100
|
+
`ACP probe request '${method}'`,
|
|
101
|
+
input.signal,
|
|
102
|
+
),
|
|
103
|
+
close: () => connection.close(),
|
|
104
|
+
};
|
|
105
|
+
} catch (error) {
|
|
106
|
+
await connection.close().catch(() => undefined);
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* agent 在 `initialize` 里自报的版本(`agentInfo.version`)。没报或空串就是 null,不猜。
|
|
113
|
+
*
|
|
114
|
+
* 只有报的就是 CLI 自己版本的 agent 才能用它:dsh 的 ACP 层把这一位写死成 `0.0.1`,
|
|
115
|
+
* 那一家改跑 `--version`({@link probeCliVersion})。
|
|
116
|
+
*/
|
|
117
|
+
export function acpAgentVersion(initializeResult: unknown): string | null {
|
|
118
|
+
if (!isRecord(initializeResult)) return null;
|
|
119
|
+
const agentInfo = initializeResult["agentInfo"];
|
|
120
|
+
return isRecord(agentInfo) ? (asTrimmedString(agentInfo["version"]) ?? null) : null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* `--version` 输出里的版本号:三段数字连同预发布段。dsh 报 `0.1.5-rc.2`,截成 `0.1.5`
|
|
125
|
+
* 会让「有更新」拿正式版去比。前面紧挨数字或点的不认,免得从更长的数串中间截一段。
|
|
126
|
+
*/
|
|
127
|
+
const CLI_VERSION_PATTERN = /(?<![\d.])(\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?)/;
|
|
128
|
+
|
|
129
|
+
export function parseCliVersion(output: string): string | null {
|
|
130
|
+
return CLI_VERSION_PATTERN.exec(output)?.[1] ?? null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 一次性探测命令用得着的那几样;宿主 `host.process.spawn` 回来的那只手在结构上就满足它。 */
|
|
134
|
+
export type ProbeProcess = {
|
|
135
|
+
readonly stdout: AsyncIterable<Uint8Array>;
|
|
136
|
+
readonly stderr: AsyncIterable<Uint8Array>;
|
|
137
|
+
readonly exited: Promise<number | null>;
|
|
138
|
+
readonly closeStdin: () => Promise<void>;
|
|
139
|
+
readonly kill: (signal?: "SIGTERM" | "SIGKILL") => Promise<void>;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** 一次性命令跑完的样子。`stdout` 超出封顶就是 undefined:半截输出不能当答案。 */
|
|
143
|
+
export type ProbeProcessRun = {
|
|
144
|
+
readonly stdout: string | undefined;
|
|
145
|
+
readonly stderr: string;
|
|
146
|
+
readonly exitCode: number | null;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
async function collectCappedText(
|
|
150
|
+
stream: AsyncIterable<Uint8Array>,
|
|
151
|
+
maxBytes: number,
|
|
152
|
+
): Promise<{ readonly text: string; readonly overflowed: boolean }> {
|
|
153
|
+
const decoder = new TextDecoder();
|
|
154
|
+
let text = "";
|
|
155
|
+
let total = 0;
|
|
156
|
+
for await (const chunk of stream) {
|
|
157
|
+
// 超出封顶也要读到底:不读的话子进程写满管道就卡住,等不到它退出。
|
|
158
|
+
total += chunk.byteLength;
|
|
159
|
+
if (total <= maxBytes) text += decoder.decode(chunk, { stream: true });
|
|
160
|
+
}
|
|
161
|
+
return { text: text + decoder.decode(), overflowed: total > maxBytes };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 跑完一个已经起来的一次性命令:同时收 stdout/stderr(各自封顶)、关 stdin、等退出,整段
|
|
166
|
+
* 一个超时,收尾一律终止并等待输出与退出。超时抛 `<what> timed out after <ms>ms`,由调用方按失败报;
|
|
167
|
+
* 「起不来」在 spawn 那一步,由调用方自己分。
|
|
168
|
+
*/
|
|
169
|
+
export async function collectProbeProcess(
|
|
170
|
+
child: ProbeProcess,
|
|
171
|
+
input: {
|
|
172
|
+
readonly timeoutMs: number;
|
|
173
|
+
readonly maxOutputBytes: number;
|
|
174
|
+
readonly what: string;
|
|
175
|
+
readonly signal?: AbortSignal;
|
|
176
|
+
readonly cleanupTimeoutMs?: number;
|
|
177
|
+
},
|
|
178
|
+
): Promise<ProbeProcessRun> {
|
|
179
|
+
// 同时消费两路输出,输入关闭也算在工作期限内,避免卡在关闭 stdin 的阶段。
|
|
180
|
+
const stdinClosed = Promise.resolve().then(() => child.closeStdin());
|
|
181
|
+
const stdout = collectCappedText(child.stdout, input.maxOutputBytes);
|
|
182
|
+
const stderr = collectCappedText(child.stderr, input.maxOutputBytes);
|
|
183
|
+
const tasks = [stdinClosed, stdout, stderr, child.exited] as const;
|
|
184
|
+
let result: ProbeProcessRun | undefined;
|
|
185
|
+
let bodyFailed = false;
|
|
186
|
+
let bodyError: unknown;
|
|
187
|
+
try {
|
|
188
|
+
const [, out, err, exitCode] = await withProbeTimeout(
|
|
189
|
+
Promise.all(tasks),
|
|
190
|
+
input.timeoutMs,
|
|
191
|
+
input.what,
|
|
192
|
+
input.signal,
|
|
193
|
+
);
|
|
194
|
+
result = { stdout: out.overflowed ? undefined : out.text, stderr: err.text, exitCode };
|
|
195
|
+
} catch (error) {
|
|
196
|
+
bodyFailed = true;
|
|
197
|
+
bodyError = error;
|
|
198
|
+
}
|
|
199
|
+
const term = Promise.resolve().then(() => child.kill("SIGTERM"));
|
|
200
|
+
void term.catch(() => undefined);
|
|
201
|
+
const cleanup = (async () => {
|
|
202
|
+
try {
|
|
203
|
+
await withProbeTimeout(child.exited, 250, `${input.what} termination grace`);
|
|
204
|
+
} catch {
|
|
205
|
+
await child.kill("SIGKILL");
|
|
206
|
+
}
|
|
207
|
+
const settled = await Promise.allSettled([term, ...tasks]);
|
|
208
|
+
// 工作错误由原通道保留;终止失败额外报告,不能伪装成已回收。
|
|
209
|
+
const termination = settled[0];
|
|
210
|
+
if (termination?.status === "rejected") throw termination.reason;
|
|
211
|
+
})();
|
|
212
|
+
try {
|
|
213
|
+
await withProbeTimeout(cleanup, input.cleanupTimeoutMs ?? 5_000, `${input.what} cleanup`);
|
|
214
|
+
} catch (cleanupError) {
|
|
215
|
+
// oxlint-disable-next-line preserve-caught-error -- AggregateError 的 cause 是第三个参数,下方保留原始清理错误。
|
|
216
|
+
throw new AggregateError(
|
|
217
|
+
bodyFailed ? [bodyError, cleanupError] : [cleanupError],
|
|
218
|
+
`${input.what} cleanup failed`,
|
|
219
|
+
{ cause: cleanupError },
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
if (bodyFailed) throw bodyError;
|
|
223
|
+
return result!;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** 版本输出正常就一行;封顶只防 CLI 抽风刷屏把内存吃掉。 */
|
|
227
|
+
const MAX_VERSION_OUTPUT_BYTES = 64 * 1024;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 跑一次 `<cli> --version` 读版本号。起不来、超时、非零退出、认不出一律 null:版本读不到
|
|
231
|
+
* 不牵连就绪,调用方照报 null。宿主因进程满额拒绝起这个辅助进程同样只是 null,不往上抛——
|
|
232
|
+
* 就绪那条主进程被拒才由调用方抛出。先认 stdout,没有才认 stderr——stderr 常混着运行时
|
|
233
|
+
* 告警,那里的版本号未必是这只 CLI 的。
|
|
234
|
+
*/
|
|
235
|
+
export async function probeCliVersion(input: {
|
|
236
|
+
readonly spawn: () => Promise<ProbeProcess>;
|
|
237
|
+
readonly timeoutMs: number;
|
|
238
|
+
readonly signal?: AbortSignal;
|
|
239
|
+
}): Promise<string | null> {
|
|
240
|
+
input.signal?.throwIfAborted();
|
|
241
|
+
let child: ProbeProcess;
|
|
242
|
+
try {
|
|
243
|
+
child = await input.spawn();
|
|
244
|
+
} catch (error) {
|
|
245
|
+
if (input.signal?.aborted) throw error;
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
const run = await collectProbeProcess(child, {
|
|
250
|
+
timeoutMs: input.timeoutMs,
|
|
251
|
+
maxOutputBytes: MAX_VERSION_OUTPUT_BYTES,
|
|
252
|
+
what: "--version",
|
|
253
|
+
...(input.signal === undefined ? {} : { signal: input.signal }),
|
|
254
|
+
});
|
|
255
|
+
if (run.exitCode !== 0) return null;
|
|
256
|
+
return parseCliVersion(run.stdout ?? "") ?? parseCliVersion(run.stderr);
|
|
257
|
+
} catch (error) {
|
|
258
|
+
if (error instanceof AggregateError || input.signal?.aborted) throw error;
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AcpExecutorTurnEndedError } from "./errors.ts";
|
|
2
|
+
import type { AcpExecutor } from "./executor.ts";
|
|
3
|
+
import type { AcpPromptInput } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
export type AcpDispatchedPrompt = {
|
|
6
|
+
readonly turnId: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 发一轮 prompt,在 `session/prompt` 写进对端 stdin 时就返回,不等整轮跑完:插件的 `sendTurn`
|
|
11
|
+
* 据此交出投递确认(规则 13 的判据),整轮结果走 `turn.completed`。写入之前就失败(会话已关、
|
|
12
|
+
* 换模型失败)照旧 reject;插话未写出就被取消,必须拒收,不能确认投递。普通 prompt 保留取消结果。
|
|
13
|
+
*
|
|
14
|
+
* 会话忙闲以 `executor.status` 为准;写入之后的失败由执行器以 failed 收尾并记日志。
|
|
15
|
+
*/
|
|
16
|
+
export const dispatchAcpPrompt = (
|
|
17
|
+
executor: Pick<AcpExecutor, "prompt">,
|
|
18
|
+
input: Omit<AcpPromptInput, "onDispatched">,
|
|
19
|
+
): Promise<AcpDispatchedPrompt> => {
|
|
20
|
+
const { promise: dispatched, resolve } = Promise.withResolvers<string>();
|
|
21
|
+
let written = false;
|
|
22
|
+
const finished = executor.prompt({
|
|
23
|
+
...input,
|
|
24
|
+
onDispatched: (turnId) => {
|
|
25
|
+
written = true;
|
|
26
|
+
resolve(turnId);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
return Promise.race([
|
|
30
|
+
dispatched.then((turnId) => ({ turnId })),
|
|
31
|
+
finished.then((result) => {
|
|
32
|
+
if (
|
|
33
|
+
!written &&
|
|
34
|
+
input.expectedActiveTurnId !== undefined &&
|
|
35
|
+
result.stopReason === "cancelled"
|
|
36
|
+
) {
|
|
37
|
+
throw new AcpExecutorTurnEndedError();
|
|
38
|
+
}
|
|
39
|
+
return { turnId: result.turnId };
|
|
40
|
+
}),
|
|
41
|
+
]);
|
|
42
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// @effect-diagnostics nodeBuiltinImport:off
|
|
2
|
+
// @effect-diagnostics globalDate:off
|
|
3
|
+
/**
|
|
4
|
+
* ACP 原始帧的可选落盘。
|
|
5
|
+
*
|
|
6
|
+
* 线上排查「界面没有回复、但 provider 说成功了」这类问题时,canonical 事件日志不够:
|
|
7
|
+
* 它写在解析与过滤之后,证明不了 stdio 上到底有没有帧。这个 sink 把连接层看到的原始
|
|
8
|
+
* 方向、阶段与载荷逐行写成 NDJSON,用来跨进程边界定位。
|
|
9
|
+
*
|
|
10
|
+
* 默认关闭:设 `YCODIUM_ACP_PROTOCOL_LOG=1` 才写,目录默认在系统临时目录下(可用
|
|
11
|
+
* `YCODIUM_ACP_PROTOCOL_LOG_DIR` 改)。载荷可能包含用户内容,只在排查时开。
|
|
12
|
+
*
|
|
13
|
+
* @module protocol-log
|
|
14
|
+
*/
|
|
15
|
+
import * as NodeFS from "node:fs";
|
|
16
|
+
import * as NodeOS from "node:os";
|
|
17
|
+
import * as NodePath from "node:path";
|
|
18
|
+
import * as NodeStream from "node:stream";
|
|
19
|
+
|
|
20
|
+
import type { AcpProtocolLogEvent } from "./connection.ts";
|
|
21
|
+
|
|
22
|
+
const DEFAULT_MAX_BYTES = 8 * 1024 * 1024;
|
|
23
|
+
const UNSAFE_FILE_CHARS = /[^A-Za-z0-9._-]+/gu;
|
|
24
|
+
|
|
25
|
+
export type AcpProtocolLogSink = {
|
|
26
|
+
readonly path: string;
|
|
27
|
+
readonly write: (event: AcpProtocolLogEvent) => void;
|
|
28
|
+
readonly close: () => Promise<void>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function safeFileName(value: string): string {
|
|
32
|
+
return value.replaceAll(UNSAFE_FILE_CHARS, "_");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 每个进程每次会话一个文件,写到上限后停机(不再写,保留已写的部分)。
|
|
37
|
+
* 用流写入是热路径:ACP 每个 token 都会经过这里,同步写会把每帧都变成一次系统调用。
|
|
38
|
+
*/
|
|
39
|
+
export function createAcpProtocolLogFile(input: {
|
|
40
|
+
readonly directory: string;
|
|
41
|
+
readonly provider: string;
|
|
42
|
+
readonly threadId: string;
|
|
43
|
+
readonly maxBytes?: number;
|
|
44
|
+
/** 测试用:换掉文件流,验证 close() 等的是 close 事件而不是 end()。 */
|
|
45
|
+
readonly openStream?: (path: string) => NodeStream.Writable;
|
|
46
|
+
}): AcpProtocolLogSink {
|
|
47
|
+
const maxBytes = input.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
48
|
+
NodeFS.mkdirSync(input.directory, { recursive: true });
|
|
49
|
+
const filePath = NodePath.join(
|
|
50
|
+
input.directory,
|
|
51
|
+
`${safeFileName(input.provider)}-${safeFileName(input.threadId)}-${Date.now().toString(36)}.ndjson`,
|
|
52
|
+
);
|
|
53
|
+
const stream = (input.openStream ?? ((path) => NodeFS.createWriteStream(path, { flags: "a" })))(
|
|
54
|
+
filePath,
|
|
55
|
+
);
|
|
56
|
+
let bytes = 0;
|
|
57
|
+
let stopped = false;
|
|
58
|
+
const closed = Promise.withResolvers<void>();
|
|
59
|
+
let closing = false;
|
|
60
|
+
stream.once("close", () => closed.resolve());
|
|
61
|
+
// 磁盘满、目录被删:调试记录不能把宿主带崩,停写并释放句柄。close() 等的是真正的
|
|
62
|
+
// close 事件,不是 error:解析上的错误不等于句柄已经交还给操作系统。
|
|
63
|
+
stream.on("error", () => {
|
|
64
|
+
stopped = true;
|
|
65
|
+
stream.destroy();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const writeTruncated = (): void => {
|
|
69
|
+
stopped = true;
|
|
70
|
+
const marker = `${JSON.stringify({ at: new Date().toISOString(), stage: "truncated" })}\n`;
|
|
71
|
+
// 上限覆盖最终文件:截断标记自己也算字节,放不下就不写。
|
|
72
|
+
if (bytes + Buffer.byteLength(marker) <= maxBytes) stream.write(marker);
|
|
73
|
+
stream.end();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const writeLine = (value: unknown): void => {
|
|
77
|
+
if (stopped) return;
|
|
78
|
+
const line = `${JSON.stringify(value)}\n`;
|
|
79
|
+
const lineBytes = Buffer.byteLength(line);
|
|
80
|
+
if (bytes + lineBytes > maxBytes) {
|
|
81
|
+
writeTruncated();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
bytes += lineBytes;
|
|
85
|
+
stream.write(line);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
path: filePath,
|
|
90
|
+
write: (event) =>
|
|
91
|
+
writeLine({
|
|
92
|
+
at: new Date().toISOString(),
|
|
93
|
+
direction: event.direction,
|
|
94
|
+
stage: event.stage,
|
|
95
|
+
payload: event.payload,
|
|
96
|
+
}),
|
|
97
|
+
close: async () => {
|
|
98
|
+
if (closing) return closed.promise;
|
|
99
|
+
closing = true;
|
|
100
|
+
stopped = true;
|
|
101
|
+
stream.end();
|
|
102
|
+
await closed.promise;
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 按环境变量决定开不开。返回 undefined 表示这次会话不记录原始帧。
|
|
109
|
+
* 路径只在开启时打一条 info:排查的人要能从日志里找到文件。
|
|
110
|
+
*/
|
|
111
|
+
export function createAcpProtocolLogFromEnv(input: {
|
|
112
|
+
readonly provider: string;
|
|
113
|
+
readonly threadId: string;
|
|
114
|
+
readonly log?: {
|
|
115
|
+
readonly info: (message: string, fields?: Readonly<Record<string, unknown>>) => void;
|
|
116
|
+
readonly warn?: (message: string, fields?: Readonly<Record<string, unknown>>) => void;
|
|
117
|
+
};
|
|
118
|
+
}): AcpProtocolLogSink | undefined {
|
|
119
|
+
const flag = process.env.YCODIUM_ACP_PROTOCOL_LOG;
|
|
120
|
+
if (flag !== "1" && flag?.toLowerCase() !== "true") return undefined;
|
|
121
|
+
const configured = process.env.YCODIUM_ACP_PROTOCOL_LOG_DIR?.trim();
|
|
122
|
+
const directory =
|
|
123
|
+
configured && configured.length > 0
|
|
124
|
+
? configured
|
|
125
|
+
: NodePath.join(NodeOS.tmpdir(), "ycodium-acp-protocol");
|
|
126
|
+
try {
|
|
127
|
+
const sink = createAcpProtocolLogFile({
|
|
128
|
+
directory,
|
|
129
|
+
provider: input.provider,
|
|
130
|
+
threadId: input.threadId,
|
|
131
|
+
});
|
|
132
|
+
input.log?.info("acp protocol frames are being recorded", { path: sink.path });
|
|
133
|
+
return sink;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
// 目录不可建、权限不足:这是可选诊断,不能把会话启动带下去。调用方这时可能已经
|
|
136
|
+
// 起好了子进程,异常穿出去就没人回收它。
|
|
137
|
+
input.log?.warn?.("acp protocol frame log could not be opened", {
|
|
138
|
+
directory,
|
|
139
|
+
error: error instanceof Error ? error.message : String(error),
|
|
140
|
+
});
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// 内部出口:线上分帧与 SDK 再导出。只从 SDK 根入口取,不许深引 SDK 私有模块。
|
|
2
|
+
export { createAcpConnection } from "./connection.ts";
|
|
3
|
+
export type {
|
|
4
|
+
AcpConnection,
|
|
5
|
+
AcpConnectionOptions,
|
|
6
|
+
AcpIncomingNotification,
|
|
7
|
+
AcpProtocolLogEvent,
|
|
8
|
+
} from "./connection.ts";
|
|
9
|
+
export { decodeSessionUpdate, isSessionUpdateEnvelope } from "./session-update.ts";
|
|
10
|
+
export type { SessionUpdateDecode, SessionUpdateEnvelope } from "./session-update.ts";
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
AGENT_METHODS,
|
|
14
|
+
CLIENT_METHODS,
|
|
15
|
+
PROTOCOL_VERSION,
|
|
16
|
+
RequestError,
|
|
17
|
+
methods,
|
|
18
|
+
ndJsonStream,
|
|
19
|
+
} from "@agentclientprotocol/sdk";
|
|
20
|
+
|
|
21
|
+
export type {
|
|
22
|
+
AgentRequestMethod,
|
|
23
|
+
AgentRequestParamsByMethod,
|
|
24
|
+
AgentRequestResponsesByMethod,
|
|
25
|
+
CancelNotification,
|
|
26
|
+
InitializeRequest,
|
|
27
|
+
InitializeResponse,
|
|
28
|
+
LoadSessionRequest,
|
|
29
|
+
LoadSessionResponse,
|
|
30
|
+
NewSessionRequest,
|
|
31
|
+
NewSessionResponse,
|
|
32
|
+
PromptRequest,
|
|
33
|
+
PromptResponse,
|
|
34
|
+
SessionNotification,
|
|
35
|
+
SessionUpdate,
|
|
36
|
+
} from "@agentclientprotocol/sdk";
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 读出这只手自报的审批档,原样交给界面。
|
|
3
|
+
*
|
|
4
|
+
* **不翻译成宿主的四档。** 契约里 `ServerProvider.nativeModes` 就是为这件事留的位置
|
|
5
|
+
* (注释:「缺席 = 该 provider 不报原生模式,界面沿用四档 RuntimeMode 开关」),
|
|
6
|
+
* `composerNativeMode.ts` 早就会画它——只是服务端从来没人往里填,所以 composer 一直
|
|
7
|
+
* 退回那四档。把 agent 自己报的原样送上去,用户看到的就是这只手真有的那几档。
|
|
8
|
+
*
|
|
9
|
+
* 曾经想过写一张「`default`→逐条确认、`yolo`→全放开」的别名表:那是硬编码,且各家档位
|
|
10
|
+
* 的语义本来就对不齐(grok 的 `acceptEdits` 实测等同 `default`,照样问)。翻译一次就
|
|
11
|
+
* 多一处会漂的事实,不如不翻。
|
|
12
|
+
*
|
|
13
|
+
* 档位 id 原样发回去,所以也不存在「发了个它没报过的 id 被拒」(kimi 实测 `-32602`)。
|
|
14
|
+
*
|
|
15
|
+
* @module runtime-mode
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** 宿主的四档。与 `@ycodium-ai/plugin-api` 的 `ExecutorRuntimeMode` 同形,这个包不依赖它。 */
|
|
19
|
+
export type AcpRuntimeMode = "approval-required" | "auto-accept-edits" | "auto" | "full-access";
|
|
20
|
+
|
|
21
|
+
/** agent 自报的一档。字段与宿主 `ServerProviderNativeMode` 同形,界面原样画。 */
|
|
22
|
+
export type AcpAvailableMode = {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
/** 给人看的名字。agent 没给就退回 id——界面上空着一行比显示 id 更糟。 */
|
|
25
|
+
readonly name: string;
|
|
26
|
+
/** agent 给这一档写的说明(claude-agent-acp 每档都有,如「Always ask before making changes」)。 */
|
|
27
|
+
readonly description?: string;
|
|
28
|
+
/** `_meta.kind`(`standard` / `plan` / `auto_review` / `full_access`)。原样带上,不据此翻译。 */
|
|
29
|
+
readonly kind?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
33
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function asString(value: unknown): string | undefined {
|
|
37
|
+
return typeof value === "string" ? value : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 从 `session/new` 回包里把 agent 自报的档位清单读出来。
|
|
42
|
+
*
|
|
43
|
+
* 两处都认:`modes.availableModes`(ACP 的会话模式那一位)与 `configOptions` 里
|
|
44
|
+
* `category: "mode"` 的那一项——`claude-agent-acp` 两处都报,kimi 报前者。
|
|
45
|
+
*/
|
|
46
|
+
export function acpAvailableModes(setup: unknown): readonly AcpAvailableMode[] {
|
|
47
|
+
if (!isRecord(setup)) return [];
|
|
48
|
+
const out: AcpAvailableMode[] = [];
|
|
49
|
+
const seen = new Set<string>();
|
|
50
|
+
const push = (entry: unknown): void => {
|
|
51
|
+
if (!isRecord(entry)) return;
|
|
52
|
+
const id = (asString(entry["id"]) ?? asString(entry["value"]))?.trim();
|
|
53
|
+
if (!id || seen.has(id)) return;
|
|
54
|
+
seen.add(id);
|
|
55
|
+
const meta = entry["_meta"];
|
|
56
|
+
const kind = isRecord(meta) ? asString(meta["kind"])?.trim().toLowerCase() : undefined;
|
|
57
|
+
const name = asString(entry["name"])?.trim();
|
|
58
|
+
const description = asString(entry["description"])?.trim();
|
|
59
|
+
out.push({
|
|
60
|
+
id,
|
|
61
|
+
name: name || id,
|
|
62
|
+
...(description ? { description } : {}),
|
|
63
|
+
...(kind ? { kind } : {}),
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const modes = setup["modes"];
|
|
68
|
+
if (isRecord(modes) && Array.isArray(modes["availableModes"])) {
|
|
69
|
+
for (const entry of modes["availableModes"]) push(entry);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const configOptions = setup["configOptions"];
|
|
73
|
+
if (Array.isArray(configOptions)) {
|
|
74
|
+
for (const option of configOptions) {
|
|
75
|
+
if (!isRecord(option)) continue;
|
|
76
|
+
if (asString(option["category"])?.trim().toLowerCase() !== "mode") continue;
|
|
77
|
+
if (!Array.isArray(option["options"])) continue;
|
|
78
|
+
for (const entry of option["options"]) push(entry);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** `configOptions` 里那一项的 id;走 `session/set_config_option` 时要用它。缺席就该走 `session/set_mode`。 */
|
|
85
|
+
export function acpModeConfigId(setup: unknown): string | undefined {
|
|
86
|
+
if (!isRecord(setup)) return undefined;
|
|
87
|
+
const configOptions = setup["configOptions"];
|
|
88
|
+
if (!Array.isArray(configOptions)) return undefined;
|
|
89
|
+
for (const option of configOptions) {
|
|
90
|
+
if (!isRecord(option)) continue;
|
|
91
|
+
if (asString(option["category"])?.trim().toLowerCase() !== "mode") continue;
|
|
92
|
+
const id = asString(option["id"])?.trim();
|
|
93
|
+
if (id) return id;
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* agent 报的「当前在哪一档」。
|
|
100
|
+
*
|
|
101
|
+
* **必须跟档位清单一起报**:座位那段(契约的 `resolveProviderNativeMode`)
|
|
102
|
+
* 在清单里找不到当前档时**整个返回 null**——只报清单不报当前档,新会话上那一格一个字
|
|
103
|
+
* 都画不出来,看着跟「这只手没有档位」一模一样。
|
|
104
|
+
*
|
|
105
|
+
* 两处都认,与 {@link acpAvailableModes} 同一口径:`modes.currentModeId` 与
|
|
106
|
+
* `configOptions` 里 `category: "mode"` 那一项的 `currentValue`。
|
|
107
|
+
*/
|
|
108
|
+
export function acpCurrentModeId(setup: unknown): string | undefined {
|
|
109
|
+
if (!isRecord(setup)) return undefined;
|
|
110
|
+
const modes = setup["modes"];
|
|
111
|
+
if (isRecord(modes)) {
|
|
112
|
+
const current = asString(modes["currentModeId"])?.trim();
|
|
113
|
+
if (current) return current;
|
|
114
|
+
}
|
|
115
|
+
const configOptions = setup["configOptions"];
|
|
116
|
+
if (Array.isArray(configOptions)) {
|
|
117
|
+
for (const option of configOptions) {
|
|
118
|
+
if (!isRecord(option)) continue;
|
|
119
|
+
if (asString(option["category"])?.trim().toLowerCase() !== "mode") continue;
|
|
120
|
+
const current = asString(option["currentValue"])?.trim();
|
|
121
|
+
if (current) return current;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|