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,537 @@
|
|
|
1
|
+
// @effect-diagnostics nodeBuiltinImport:off
|
|
2
|
+
// @effect-diagnostics globalDate:off
|
|
3
|
+
// @effect-diagnostics globalTimers:off
|
|
4
|
+
import * as NodeCrypto from "node:crypto";
|
|
5
|
+
import type * as NodeStream from "node:stream";
|
|
6
|
+
import { createApprovalGate } from "./approvals.ts";
|
|
7
|
+
import { UnboundedEventStream } from "./event-stream.ts";
|
|
8
|
+
import { type AcpToolCallTrackedState } from "./events.ts";
|
|
9
|
+
import { createChildSessionRegistry } from "./executor/child-sessions.ts";
|
|
10
|
+
import { createExecutorLifecycle } from "./executor/lifecycle.ts";
|
|
11
|
+
import { createExecutorPrompt } from "./executor/prompt.ts";
|
|
12
|
+
import { createPromptResults } from "./executor/prompt-results.ts";
|
|
13
|
+
import { createExecutorRequests } from "./executor/requests.ts";
|
|
14
|
+
import { createExecutorRuntime } from "./executor/runtime.ts";
|
|
15
|
+
import { createExecutorSession } from "./executor/session.ts";
|
|
16
|
+
import { createExecutorSettlement } from "./executor/settlement.ts";
|
|
17
|
+
import type { AcpExecutorState, CompactionRecord } from "./executor/support.ts";
|
|
18
|
+
import { createExecutorUpdates } from "./executor/updates.ts";
|
|
19
|
+
import { acpClosedConnectionCategory } from "./failureCategory.ts";
|
|
20
|
+
import { Mutex } from "./mutex.ts";
|
|
21
|
+
import { readOpenToolCalls, type AcpOpenToolCall } from "./openCommands.ts";
|
|
22
|
+
import { createAcpProtocolLogFromEnv } from "./protocol-log.ts";
|
|
23
|
+
import {
|
|
24
|
+
CLIENT_METHODS,
|
|
25
|
+
RequestError,
|
|
26
|
+
createAcpConnection,
|
|
27
|
+
type AcpConnection,
|
|
28
|
+
type AcpIncomingNotification,
|
|
29
|
+
type AcpProtocolLogEvent,
|
|
30
|
+
type InitializeResponse,
|
|
31
|
+
type PromptResponse,
|
|
32
|
+
type SessionNotification,
|
|
33
|
+
} from "./protocol.ts";
|
|
34
|
+
import { type AcpAvailableCommand, type AcpPermissionRequest } from "./runtime-model.ts";
|
|
35
|
+
import type {
|
|
36
|
+
AcpApprovalDecision,
|
|
37
|
+
AcpPromptInput,
|
|
38
|
+
AcpSessionStartResult,
|
|
39
|
+
AcpSessionStatus,
|
|
40
|
+
AcpTurnResult,
|
|
41
|
+
ExecutorEvent,
|
|
42
|
+
} from "./types.ts";
|
|
43
|
+
import { type PendingUserInput } from "./user-input-pending.ts";
|
|
44
|
+
import { defaultVendorHooks, type AcpVendorHooks } from "./vendor-hooks.ts";
|
|
45
|
+
|
|
46
|
+
const DEFAULT_CANCEL_GRACE_MS = 5_000;
|
|
47
|
+
|
|
48
|
+
export type AcpExecutorOptions = {
|
|
49
|
+
readonly readable: NodeStream.Readable;
|
|
50
|
+
readonly writable: NodeStream.Writable;
|
|
51
|
+
readonly cwd: string;
|
|
52
|
+
readonly threadId: string;
|
|
53
|
+
readonly provider: string;
|
|
54
|
+
readonly providerInstanceId?: string;
|
|
55
|
+
readonly clientInfo: { readonly name: string; readonly version: string };
|
|
56
|
+
readonly resumeSessionId?: string;
|
|
57
|
+
readonly resumeMethod?: "session/load" | "session/resume";
|
|
58
|
+
readonly mcpServers?: ReadonlyArray<{ readonly type?: string; readonly [key: string]: unknown }>;
|
|
59
|
+
/**
|
|
60
|
+
* `cwd` 之外 agent 可读写的工作区根(ACP 草案字段)。`session/new`、`session/load`、
|
|
61
|
+
* `session/resume` 都带上;缺省或空数组时整个字段不发,不支持它的 agent 不受影响。
|
|
62
|
+
*/
|
|
63
|
+
readonly additionalDirectories?: ReadonlyArray<string>;
|
|
64
|
+
readonly authMethodId?: string | ((initializeResult: InitializeResponse) => string | undefined);
|
|
65
|
+
readonly clientCapabilities?: {
|
|
66
|
+
readonly fs?: { readonly readTextFile?: boolean; readonly writeTextFile?: boolean };
|
|
67
|
+
readonly terminal?: boolean;
|
|
68
|
+
readonly [key: string]: unknown;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* 跟踪 ACP 子会话(草案 Subagent Sessions,尚未定稿)。为真时 `initialize` 声明 `subagents`,
|
|
72
|
+
* 并把父会话上的 `subagent_update` 翻成 `task.*`。默认关:关着时线上字节与行为不变。
|
|
73
|
+
*/
|
|
74
|
+
readonly subagentSessions?: boolean;
|
|
75
|
+
readonly hooks?: AcpVendorHooks;
|
|
76
|
+
/**
|
|
77
|
+
* agent 子进程 stderr 的末尾(已脱敏,见 plugin-api 的 `nodeStreamsFromHandle`)。连接断了时
|
|
78
|
+
* 把它附在错误里,启动期与会话中途都附:用户看到的才是 agent 自己说的原因,而不是一句
|
|
79
|
+
* 「ACP input stream ended」。
|
|
80
|
+
*/
|
|
81
|
+
readonly stderrTail?: () => string | Promise<string>;
|
|
82
|
+
/**
|
|
83
|
+
* agent 子进程的退出(宿主句柄的 `exited`)。会话中途连接断了时,失败原话里写上退出码。
|
|
84
|
+
* 不传就只写「连接断了」与 stderr 末尾。
|
|
85
|
+
*/
|
|
86
|
+
readonly exited?: Promise<number | null>;
|
|
87
|
+
/**
|
|
88
|
+
* 宿主替用户答掉的审批:返回 true 就直接选「允许」那一项,不弹给用户。只在输入区座位
|
|
89
|
+
* 画的是宿主四档时才传(「完全放开」得真的不问);座位画这只手自己的档时放不放行归
|
|
90
|
+
* CLI 那一档管,宿主再替它批就是座位之外的第二个答案。
|
|
91
|
+
*/
|
|
92
|
+
readonly autoApprove?: (request: AcpPermissionRequest) => boolean;
|
|
93
|
+
readonly requestedModelId?: string;
|
|
94
|
+
/**
|
|
95
|
+
* 请求的型号不在目录里,或当前型号未知时,写型号之前叫一次。
|
|
96
|
+
* 有厂商缓存的手在这里刷新缓存。抛错表示刷新失败,这一轮不发。
|
|
97
|
+
* 返回之后(没挂这个钩子也一样)直接在已开的会话上写这个型号,由 agent 判;建会话、恢复会话、运行中都不另开会话。
|
|
98
|
+
*/
|
|
99
|
+
readonly refreshCatalogForSend?: () => Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* 发送前刷新模型清单的期限,也是每次型号、档位写入等应答的期限。
|
|
102
|
+
* 缺省比 grok models 命令的 60 秒更长。测试传一个很短的值。
|
|
103
|
+
*/
|
|
104
|
+
readonly modelCatalogRefreshTimeoutMs?: number;
|
|
105
|
+
/**
|
|
106
|
+
* 建会话时选的思考档位,会话建好之后对齐一次(见 `alignRequestedEffort`)。之后每轮由
|
|
107
|
+
* `prompt` 的 `requestedEffort` 对齐。
|
|
108
|
+
*/
|
|
109
|
+
readonly requestedEffort?: string;
|
|
110
|
+
/**
|
|
111
|
+
* agent 宣告(或更新)它的斜杠命令时调用,给的是**完整清单**(ACP 的
|
|
112
|
+
* `available_commands_update` 是整份替换)。插件据此经 `host.reportSessionInventory`
|
|
113
|
+
* 把命令报给宿主,输入区的 `/` 菜单才有它们。回调要同步、不抛错。
|
|
114
|
+
*/
|
|
115
|
+
readonly onAvailableCommands?: (commands: ReadonlyArray<AcpAvailableCommand>) => void;
|
|
116
|
+
/** 与插件 `host.log` 同形;缺席就不记。 */
|
|
117
|
+
readonly log?: {
|
|
118
|
+
readonly info: (message: string, fields?: Readonly<Record<string, unknown>>) => void;
|
|
119
|
+
readonly warn?: (message: string, fields?: Readonly<Record<string, unknown>>) => void;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* 调试缝:连接层看到的每一帧原始/解码 ACP 消息。设了就不再看环境变量。
|
|
123
|
+
* 载荷是协议原文(可能含用户内容),只在排查时接。
|
|
124
|
+
*/
|
|
125
|
+
readonly protocolLog?: (event: AcpProtocolLogEvent) => void;
|
|
126
|
+
readonly cancelGraceMs?: number;
|
|
127
|
+
/** 结果收尾与进程退出后等入流关闭的各段上限;不罩进程终止。 */
|
|
128
|
+
readonly resultDrainTimeoutMs?: number;
|
|
129
|
+
readonly nowMs?: () => number;
|
|
130
|
+
readonly nowIso?: () => string;
|
|
131
|
+
readonly randomId?: () => string;
|
|
132
|
+
/**
|
|
133
|
+
* 测试缝:每次 RPC 成功之后、把结果写进会话状态之前调用。
|
|
134
|
+
* 用来构造「结果已收到,收尾尚未发布」与迟到续体。
|
|
135
|
+
*/
|
|
136
|
+
readonly beforeApplyResult?: () => Promise<void>;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export type AcpExecutor = {
|
|
140
|
+
readonly generation: number;
|
|
141
|
+
readonly sessionId: string | undefined;
|
|
142
|
+
readonly modelId: string | undefined;
|
|
143
|
+
/** `modelId` 是不是 agent 报的;缺席的型号当然不是。 */
|
|
144
|
+
readonly modelReported: boolean;
|
|
145
|
+
readonly status: AcpSessionStatus;
|
|
146
|
+
readonly events: AsyncIterable<ExecutorEvent>;
|
|
147
|
+
start(): Promise<AcpSessionStartResult>;
|
|
148
|
+
prompt(input: AcpPromptInput): Promise<AcpTurnResult>;
|
|
149
|
+
cancel(turnId?: string): Promise<void>;
|
|
150
|
+
respondToRequest(
|
|
151
|
+
requestId: string,
|
|
152
|
+
decision: AcpApprovalDecision,
|
|
153
|
+
reason?: string,
|
|
154
|
+
): Promise<void>;
|
|
155
|
+
respondToUserInput(requestId: string, answers: unknown): Promise<void>;
|
|
156
|
+
/**
|
|
157
|
+
* 切到这只手自报的某一档审批模式。`modeId` 是它自己报的 id,**一路不翻译**。
|
|
158
|
+
*
|
|
159
|
+
* 两条路按 agent 报的形状挑:报了 `configOptions` 里 `category: "mode"` 那一项就走
|
|
160
|
+
* `session/set_config_option`(claude-agent-acp),否则走 `session/set_mode`(kimi)。
|
|
161
|
+
*/
|
|
162
|
+
setNativeMode(modeId: string): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* 让 agent 执行它自己宣告过的压缩斜杠命令(Grok 的 `/compact`),把进度与结果走
|
|
165
|
+
* 压缩生命周期事件(`thread.state.changed`:compacting → compacted)报出去。
|
|
166
|
+
*
|
|
167
|
+
* 这条 `/compact` 不是用户消息:不开轮次,agent 为它吐的回答文本也不进时间线。
|
|
168
|
+
* agent 没宣告这条命令、或这一刻有轮次/另一次压缩在跑,都直接拒绝——
|
|
169
|
+
* 命令名没人认的话,文本会被当成普通提示词发给模型。
|
|
170
|
+
*/
|
|
171
|
+
compact(input: { readonly commandName: string }): Promise<void>;
|
|
172
|
+
/** 这一轮还在跑的工具调用。只出事实:哪条算命令、argv 什么样由插件判,不带 pid。 */
|
|
173
|
+
openToolCalls(): ReadonlyArray<AcpOpenToolCall>;
|
|
174
|
+
/**
|
|
175
|
+
* 工具调用型子 agent(`hooks.toolSubagents`)里还有没拿到结局的:在跑的,或一轮结束后状态未知的。
|
|
176
|
+
* 打断停不掉子 agent 的 agent 据此决定停止时要不要结束整个会话。
|
|
177
|
+
*/
|
|
178
|
+
toolSubagentsMayRun(): boolean;
|
|
179
|
+
/** 只有已确认的进程自行退出才传退出码;用户 stop / dispose 不传。 */
|
|
180
|
+
dispose(exitCode?: number | null): Promise<void>;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
function defaultNowIso(): string {
|
|
184
|
+
return new Date().toISOString();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function defaultRandomId(): string {
|
|
188
|
+
return NodeCrypto.randomUUID();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function createAcpExecutor(options: AcpExecutorOptions): AcpExecutor {
|
|
192
|
+
const hooks = { ...defaultVendorHooks, ...options.hooks };
|
|
193
|
+
|
|
194
|
+
const cancelGraceMs = options.cancelGraceMs ?? DEFAULT_CANCEL_GRACE_MS;
|
|
195
|
+
|
|
196
|
+
const nowIso = options.nowIso ?? defaultNowIso;
|
|
197
|
+
|
|
198
|
+
const randomId = options.randomId ?? defaultRandomId;
|
|
199
|
+
|
|
200
|
+
const eventStream = new UnboundedEventStream();
|
|
201
|
+
|
|
202
|
+
const threadMutex = new Mutex();
|
|
203
|
+
|
|
204
|
+
const promptMutex = new Mutex();
|
|
205
|
+
const shared: AcpExecutorState = {
|
|
206
|
+
generation: 0,
|
|
207
|
+
disposed: false,
|
|
208
|
+
disposing: false,
|
|
209
|
+
status: "idle",
|
|
210
|
+
sessionId: undefined,
|
|
211
|
+
modelSelectionState: {
|
|
212
|
+
strategy: "session/set_model",
|
|
213
|
+
sessionModelsAdvertised: false,
|
|
214
|
+
modelConfigId: undefined,
|
|
215
|
+
currentModelId: undefined,
|
|
216
|
+
advertisedModelValues: [],
|
|
217
|
+
},
|
|
218
|
+
initializeResult: undefined,
|
|
219
|
+
startResult: undefined,
|
|
220
|
+
startInFlight: undefined,
|
|
221
|
+
activeTurnId: undefined,
|
|
222
|
+
promptsInFlight: 0,
|
|
223
|
+
pendingSettlements: new Map(),
|
|
224
|
+
manualCompaction: undefined,
|
|
225
|
+
assistantSegment: { nextSegmentIndex: 0 },
|
|
226
|
+
modeState: undefined,
|
|
227
|
+
availableCommands: [],
|
|
228
|
+
promptWaiters: [],
|
|
229
|
+
abortLocalPrompt: undefined,
|
|
230
|
+
connectionClosed: false,
|
|
231
|
+
sessionLoadInFlight: false,
|
|
232
|
+
droppedReplayUpdates: 0,
|
|
233
|
+
ownSessionIds: new Set(),
|
|
234
|
+
childSessions: createChildSessionRegistry(),
|
|
235
|
+
sessionOpensInFlight: 0,
|
|
236
|
+
configOptionUpdates: new Map(),
|
|
237
|
+
localPromptWait: undefined,
|
|
238
|
+
configUpdateSeq: 0,
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const interruptedTurnIds = new Set<string>();
|
|
242
|
+
|
|
243
|
+
const approvals = createApprovalGate({
|
|
244
|
+
singleResolver: false,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
// 结构化用户输入(厂商钩子把 permission 认成提问/计划时)的挂起表,与 approvals 同理:答复 / 取消 / 断连三者竞争只完成一次。
|
|
248
|
+
const pendingUserInputs = new Map<string, PendingUserInput>();
|
|
249
|
+
|
|
250
|
+
const toolCalls = new Map<string, AcpToolCallTrackedState>();
|
|
251
|
+
|
|
252
|
+
/** agent 自己的 compactionId → 累积状态。有界:一次会话压缩不了几回,结束的随手清掉。 */
|
|
253
|
+
const compactions = new Map<string, CompactionRecord>();
|
|
254
|
+
|
|
255
|
+
const nowMs = (): number => options.nowMs?.() ?? Date.now();
|
|
256
|
+
|
|
257
|
+
const assistantItemRuntimeId = randomId();
|
|
258
|
+
const {
|
|
259
|
+
stamp,
|
|
260
|
+
isLive,
|
|
261
|
+
emit,
|
|
262
|
+
emitArtifacts,
|
|
263
|
+
failIfDisposed,
|
|
264
|
+
closeActiveAssistantSegment,
|
|
265
|
+
ensureActiveAssistantSegment,
|
|
266
|
+
notifyPromptSettled,
|
|
267
|
+
waitForPromptIdle,
|
|
268
|
+
} = createExecutorRuntime({
|
|
269
|
+
randomId,
|
|
270
|
+
nowIso,
|
|
271
|
+
eventStream,
|
|
272
|
+
options,
|
|
273
|
+
assistantItemRuntimeId,
|
|
274
|
+
promptMutex,
|
|
275
|
+
shared,
|
|
276
|
+
});
|
|
277
|
+
const {
|
|
278
|
+
emitCompactionState,
|
|
279
|
+
receiveSessionUpdate,
|
|
280
|
+
receiveExtensionNotification,
|
|
281
|
+
settleToolSubagents,
|
|
282
|
+
settleSessionEnded,
|
|
283
|
+
toolSubagentsMayRun,
|
|
284
|
+
} = createExecutorUpdates({
|
|
285
|
+
emit,
|
|
286
|
+
stamp,
|
|
287
|
+
options,
|
|
288
|
+
nowMs,
|
|
289
|
+
compactions,
|
|
290
|
+
randomId,
|
|
291
|
+
toolCalls,
|
|
292
|
+
hooks,
|
|
293
|
+
emitArtifacts,
|
|
294
|
+
closeActiveAssistantSegment,
|
|
295
|
+
ensureActiveAssistantSegment,
|
|
296
|
+
shared,
|
|
297
|
+
});
|
|
298
|
+
const {
|
|
299
|
+
settleAllApprovals,
|
|
300
|
+
settleAllUserInputs,
|
|
301
|
+
makeExtensionContext,
|
|
302
|
+
handlePermissionRequest,
|
|
303
|
+
respondToRequest,
|
|
304
|
+
respondToUserInput,
|
|
305
|
+
} = createExecutorRequests({
|
|
306
|
+
emit,
|
|
307
|
+
stamp,
|
|
308
|
+
options,
|
|
309
|
+
approvals,
|
|
310
|
+
pendingUserInputs,
|
|
311
|
+
failIfDisposed,
|
|
312
|
+
randomId,
|
|
313
|
+
hooks,
|
|
314
|
+
shared,
|
|
315
|
+
toolCalls,
|
|
316
|
+
});
|
|
317
|
+
const { rememberSettledPromptKey, abortPendingSettlement, settleFromNotification } =
|
|
318
|
+
createExecutorSettlement({ hooks, shared });
|
|
319
|
+
const promptResults = createPromptResults({ options, shared });
|
|
320
|
+
|
|
321
|
+
// 默认关闭的原始帧记录:环境变量开启时每会话写一个 NDJSON 文件;嵌入方可以用
|
|
322
|
+
// `protocolLog` 直接接走。关闭交给 dispose,别把句柄留到进程结束。
|
|
323
|
+
const protocolLogSink =
|
|
324
|
+
options.protocolLog === undefined
|
|
325
|
+
? createAcpProtocolLogFromEnv({
|
|
326
|
+
provider: options.provider,
|
|
327
|
+
threadId: options.threadId,
|
|
328
|
+
...(options.log === undefined ? {} : { log: options.log }),
|
|
329
|
+
})
|
|
330
|
+
: undefined;
|
|
331
|
+
const protocolLog = options.protocolLog ?? protocolLogSink?.write;
|
|
332
|
+
|
|
333
|
+
const connection: AcpConnection = createAcpConnection({
|
|
334
|
+
readable: options.readable,
|
|
335
|
+
writable: options.writable,
|
|
336
|
+
...(protocolLog === undefined ? {} : { logger: protocolLog }),
|
|
337
|
+
onInboundFailure: (error) => {
|
|
338
|
+
options.log?.warn?.("acp connection closed: an inbound request could not be answered", {
|
|
339
|
+
threadId: options.threadId,
|
|
340
|
+
error: error instanceof Error ? error.message : String(error),
|
|
341
|
+
});
|
|
342
|
+
},
|
|
343
|
+
// 窗口在应答帧到达的那一刻同步收掉:应答之后的通知是真输出,不能再丢。
|
|
344
|
+
onResponse: ({ method, promptId }, outcome) => {
|
|
345
|
+
if (method === "session/load") shared.sessionLoadInFlight = false;
|
|
346
|
+
if (promptId !== undefined) {
|
|
347
|
+
if ("result" in outcome) promptResults.receive(promptId, outcome.result as PromptResponse);
|
|
348
|
+
else promptResults.reject(promptId);
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
onNotification: (notification: AcpIncomingNotification) => {
|
|
352
|
+
if (notification.method === "session/update") {
|
|
353
|
+
const params = notification.params as SessionNotification;
|
|
354
|
+
const transformed = hooks.transformSessionUpdate
|
|
355
|
+
? hooks.transformSessionUpdate(params)
|
|
356
|
+
: params;
|
|
357
|
+
if (transformed !== undefined) receiveSessionUpdate(transformed);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (settleFromNotification(notification.method, notification.params)) return;
|
|
361
|
+
receiveExtensionNotification(notification.method, notification.params);
|
|
362
|
+
},
|
|
363
|
+
onRequest: async (method, params) => {
|
|
364
|
+
if (method === CLIENT_METHODS.session_request_permission) {
|
|
365
|
+
return handlePermissionRequest(params);
|
|
366
|
+
}
|
|
367
|
+
if (hooks.onExtensionRequest) {
|
|
368
|
+
// undefined = 这家不认这个方法。当成合法结果回去的话,只认自己那两个方法的
|
|
369
|
+
// 钩子会让其它未知方法静默收到 null——agent 以为调成功了。
|
|
370
|
+
const handled = await hooks.onExtensionRequest(
|
|
371
|
+
method,
|
|
372
|
+
params,
|
|
373
|
+
makeExtensionContext(method, params),
|
|
374
|
+
);
|
|
375
|
+
if (handled !== undefined) return handled;
|
|
376
|
+
}
|
|
377
|
+
throw RequestError.methodNotFound(method);
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
void connection.closed.then(
|
|
382
|
+
() => {
|
|
383
|
+
shared.connectionClosed = true;
|
|
384
|
+
if (shared.disposed) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
settleAllApprovals("cancel");
|
|
388
|
+
settleAllUserInputs();
|
|
389
|
+
abortPendingSettlement(
|
|
390
|
+
"connection closed",
|
|
391
|
+
acpClosedConnectionCategory(
|
|
392
|
+
connection.closeError(),
|
|
393
|
+
shared.startResult !== undefined,
|
|
394
|
+
shared.disposed,
|
|
395
|
+
),
|
|
396
|
+
);
|
|
397
|
+
shared.promptWaiters.splice(0).forEach((wake) => wake());
|
|
398
|
+
},
|
|
399
|
+
() => {
|
|
400
|
+
shared.connectionClosed = true;
|
|
401
|
+
},
|
|
402
|
+
);
|
|
403
|
+
|
|
404
|
+
// 入流一死就当场结算挂着的审批/用户输入/轮次等待,不能等 connection.closed:
|
|
405
|
+
// 协议的 shutdown 排在入站队列后面,而队列正卡在等这些 handler settle——
|
|
406
|
+
// 等 closed 才动手就是死锁。这里先结算,队列疏通后剩余缓冲帧照常投递。
|
|
407
|
+
const onInputDead = (): void => {
|
|
408
|
+
if (shared.disposed) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
settleAllApprovals("cancel");
|
|
412
|
+
settleAllUserInputs();
|
|
413
|
+
// 提前收口的等待不在这里中止:清掉审批 / 用户输入之后入站队列就通了,协议层的
|
|
414
|
+
// shutdown 随即 reject 那条 prompt RPC,竞速的另一边自己会赢(实测两版都 reject,
|
|
415
|
+
// 只是理由从 "input stream ended" 变成流上的那个 error)。
|
|
416
|
+
shared.promptWaiters.splice(0).forEach((wake) => wake());
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
options.readable.once("end", onInputDead);
|
|
420
|
+
|
|
421
|
+
options.readable.once("error", onInputDead);
|
|
422
|
+
const {
|
|
423
|
+
applyRequestedModel,
|
|
424
|
+
modelDefaultEffort,
|
|
425
|
+
writeConfigOptionReported,
|
|
426
|
+
start,
|
|
427
|
+
requireStarted,
|
|
428
|
+
setNativeMode,
|
|
429
|
+
} = createExecutorSession({
|
|
430
|
+
options,
|
|
431
|
+
hooks,
|
|
432
|
+
connection,
|
|
433
|
+
isLive,
|
|
434
|
+
emit,
|
|
435
|
+
stamp,
|
|
436
|
+
failIfDisposed,
|
|
437
|
+
shared,
|
|
438
|
+
});
|
|
439
|
+
const { sendPromptRpc, prompt } = createExecutorPrompt({
|
|
440
|
+
promptResults,
|
|
441
|
+
settleToolSubagents,
|
|
442
|
+
isLive,
|
|
443
|
+
closeActiveAssistantSegment,
|
|
444
|
+
emit,
|
|
445
|
+
stamp,
|
|
446
|
+
options,
|
|
447
|
+
interruptedTurnIds,
|
|
448
|
+
hooks,
|
|
449
|
+
randomId,
|
|
450
|
+
connection,
|
|
451
|
+
rememberSettledPromptKey,
|
|
452
|
+
failIfDisposed,
|
|
453
|
+
requireStarted,
|
|
454
|
+
threadMutex,
|
|
455
|
+
promptMutex,
|
|
456
|
+
applyRequestedModel,
|
|
457
|
+
modelDefaultEffort,
|
|
458
|
+
writeConfigOptionReported,
|
|
459
|
+
notifyPromptSettled,
|
|
460
|
+
shared,
|
|
461
|
+
});
|
|
462
|
+
const {
|
|
463
|
+
cancel,
|
|
464
|
+
compact,
|
|
465
|
+
dispose: disposeLifecycle,
|
|
466
|
+
} = createExecutorLifecycle({
|
|
467
|
+
promptResults,
|
|
468
|
+
settleToolSubagents,
|
|
469
|
+
settleSessionEnded,
|
|
470
|
+
failIfDisposed,
|
|
471
|
+
requireStarted,
|
|
472
|
+
interruptedTurnIds,
|
|
473
|
+
settleAllApprovals,
|
|
474
|
+
settleAllUserInputs,
|
|
475
|
+
abortPendingSettlement,
|
|
476
|
+
connection,
|
|
477
|
+
waitForPromptIdle,
|
|
478
|
+
cancelGraceMs,
|
|
479
|
+
isLive,
|
|
480
|
+
closeActiveAssistantSegment,
|
|
481
|
+
emit,
|
|
482
|
+
stamp,
|
|
483
|
+
options,
|
|
484
|
+
promptMutex,
|
|
485
|
+
randomId,
|
|
486
|
+
nowMs,
|
|
487
|
+
emitCompactionState,
|
|
488
|
+
sendPromptRpc,
|
|
489
|
+
compactions,
|
|
490
|
+
notifyPromptSettled,
|
|
491
|
+
eventStream,
|
|
492
|
+
onInputDead,
|
|
493
|
+
shared,
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
let disposeOnce: Promise<void> | undefined;
|
|
497
|
+
|
|
498
|
+
return {
|
|
499
|
+
get generation() {
|
|
500
|
+
return shared.generation;
|
|
501
|
+
},
|
|
502
|
+
get sessionId() {
|
|
503
|
+
return shared.sessionId;
|
|
504
|
+
},
|
|
505
|
+
get modelId() {
|
|
506
|
+
return shared.modelSelectionState.currentModelId;
|
|
507
|
+
},
|
|
508
|
+
get modelReported() {
|
|
509
|
+
return shared.modelSelectionState.currentModelReported === true;
|
|
510
|
+
},
|
|
511
|
+
get status() {
|
|
512
|
+
return shared.status;
|
|
513
|
+
},
|
|
514
|
+
events: eventStream.subscribe(),
|
|
515
|
+
start,
|
|
516
|
+
prompt,
|
|
517
|
+
cancel,
|
|
518
|
+
respondToRequest,
|
|
519
|
+
respondToUserInput,
|
|
520
|
+
setNativeMode,
|
|
521
|
+
compact,
|
|
522
|
+
openToolCalls: () => readOpenToolCalls(toolCalls, shared.activeTurnId),
|
|
523
|
+
toolSubagentsMayRun,
|
|
524
|
+
dispose: (exitCode) => {
|
|
525
|
+
// 同一轮清理只跑一次,调用方拿到的永远是同一个 Promise(与 disposeLifecycle 的
|
|
526
|
+
// 契约一致;调试用的文件句柄也算在这次清理里)。
|
|
527
|
+
disposeOnce ??= (async () => {
|
|
528
|
+
try {
|
|
529
|
+
await disposeLifecycle(exitCode);
|
|
530
|
+
} finally {
|
|
531
|
+
await protocolLogSink?.close();
|
|
532
|
+
}
|
|
533
|
+
})();
|
|
534
|
+
return disposeOnce;
|
|
535
|
+
},
|
|
536
|
+
};
|
|
537
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话失败类别。与 plugin-api 的 `ExecutorFailureCategory`、契约的 `ProviderFailureCategory` 同值。
|
|
3
|
+
* 本包发布到 npm,不能 import 那两处,所以手写一份。
|
|
4
|
+
*
|
|
5
|
+
* 只按结构化信息定,不按错误文字猜。拿不准就不带;本包自己不写 `unknown`。
|
|
6
|
+
*/
|
|
7
|
+
import { AcpConnectionClosedError } from "./errors.ts";
|
|
8
|
+
|
|
9
|
+
const ACP_FAILURE_CATEGORIES = [
|
|
10
|
+
"quota",
|
|
11
|
+
"rate-limit",
|
|
12
|
+
"context-limit",
|
|
13
|
+
"auth",
|
|
14
|
+
"model-unavailable",
|
|
15
|
+
"transport",
|
|
16
|
+
"unknown",
|
|
17
|
+
"process-exit",
|
|
18
|
+
] as const;
|
|
19
|
+
|
|
20
|
+
export type AcpFailureCategory = (typeof ACP_FAILURE_CATEGORIES)[number];
|
|
21
|
+
|
|
22
|
+
const isAcpFailureCategory = (value: unknown): value is AcpFailureCategory =>
|
|
23
|
+
ACP_FAILURE_CATEGORIES.some((category) => category === value);
|
|
24
|
+
|
|
25
|
+
/** 错误上已经带的类别。不在名单里就当没有。 */
|
|
26
|
+
export const readAcpFailureCategory = (error: unknown): AcpFailureCategory | undefined => {
|
|
27
|
+
if (typeof error !== "object" || error === null) return undefined;
|
|
28
|
+
const value = (error as { failureCategory?: unknown }).failureCategory;
|
|
29
|
+
return isAcpFailureCategory(value) ? value : undefined;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** 只在类别有值时挂到错误上。宿主按属性认。 */
|
|
33
|
+
export const withFailureCategory = <T extends object>(
|
|
34
|
+
error: T,
|
|
35
|
+
category: AcpFailureCategory | undefined,
|
|
36
|
+
): T => (category === undefined ? error : Object.assign(error, { failureCategory: category }));
|
|
37
|
+
|
|
38
|
+
/** 只在失败 payload 上放这个键。 */
|
|
39
|
+
export const failureCategoryField = (
|
|
40
|
+
category: AcpFailureCategory | undefined,
|
|
41
|
+
): { readonly failureCategory: AcpFailureCategory } | Record<string, never> =>
|
|
42
|
+
category === undefined ? {} : { failureCategory: category };
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 连接关了时的类别。会话还没建立、或宿主在 dispose,不带。对端输出到头(agent 退出或关了输出)
|
|
46
|
+
* 是 `process-exit`;本端入站处理失败、管道报错等别的关法仍是 `transport`,原话照旧。
|
|
47
|
+
* 有状态码回来的错误不走这里。
|
|
48
|
+
*/
|
|
49
|
+
export const acpClosedConnectionCategory = (
|
|
50
|
+
closeError: unknown,
|
|
51
|
+
sessionEstablished: boolean,
|
|
52
|
+
disposed: boolean,
|
|
53
|
+
): AcpFailureCategory | undefined => {
|
|
54
|
+
if (!sessionEstablished || disposed) return undefined;
|
|
55
|
+
return closeError instanceof AcpConnectionClosedError && closeError.reason === "input-ended"
|
|
56
|
+
? "process-exit"
|
|
57
|
+
: "transport";
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* `session/prompt` 失败时的类别。
|
|
62
|
+
* 钩子或连接关闭已经挂上的类别优先。JSON-RPC 的 `RequestError` 不表达这些类别,不带。
|
|
63
|
+
* 会话建立之后的 `AcpConnectionClosedError` 按关的原因分 `process-exit` 与 `transport`。
|
|
64
|
+
*/
|
|
65
|
+
export const acpPromptFailureCategory = (
|
|
66
|
+
error: unknown,
|
|
67
|
+
input: { readonly sessionEstablished: boolean; readonly disposed: boolean },
|
|
68
|
+
): AcpFailureCategory | undefined => {
|
|
69
|
+
const signaled = readAcpFailureCategory(error);
|
|
70
|
+
if (signaled !== undefined) return signaled;
|
|
71
|
+
return error instanceof AcpConnectionClosedError
|
|
72
|
+
? acpClosedConnectionCategory(error, input.sessionEstablished, input.disposed)
|
|
73
|
+
: undefined;
|
|
74
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 客户端在 `initialize` 里怎么自报身份。
|
|
3
|
+
*
|
|
4
|
+
* 会话路径与探测路径必须是同一份:agent 会按客户端能力决定播报什么(cursor 的
|
|
5
|
+
* parameterized model picker 就是要客户端先声明才给),两边各写一份就等于探测问的
|
|
6
|
+
* 与真会话拿的不是同一个问题。
|
|
7
|
+
*
|
|
8
|
+
* `overrides` 只认 `fs`、`terminal` 与 `subagents` 三栏——其余键今天也没被发出去,这里不静默放宽。
|
|
9
|
+
* `subagents`(ACP 子会话草案,尚未定稿)为真才声明;不声明的 agent 按协议不会发子会话更新。
|
|
10
|
+
*/
|
|
11
|
+
import { PROTOCOL_VERSION } from "./protocol.ts";
|
|
12
|
+
|
|
13
|
+
export type AcpClientInfo = {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly version: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type AcpClientCapabilityOverrides = {
|
|
19
|
+
readonly fs?: { readonly readTextFile?: boolean; readonly writeTextFile?: boolean };
|
|
20
|
+
readonly terminal?: boolean;
|
|
21
|
+
readonly subagents?: boolean;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type AcpInitializePayload = {
|
|
25
|
+
readonly protocolVersion: number;
|
|
26
|
+
readonly clientCapabilities: {
|
|
27
|
+
readonly fs: { readonly readTextFile: boolean; readonly writeTextFile: boolean };
|
|
28
|
+
readonly terminal: boolean;
|
|
29
|
+
readonly subagents?: Record<string, never>;
|
|
30
|
+
};
|
|
31
|
+
readonly clientInfo: AcpClientInfo;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function acpInitializePayload(
|
|
35
|
+
clientInfo: AcpClientInfo,
|
|
36
|
+
overrides?: AcpClientCapabilityOverrides,
|
|
37
|
+
): AcpInitializePayload {
|
|
38
|
+
return {
|
|
39
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
40
|
+
clientCapabilities: {
|
|
41
|
+
fs: {
|
|
42
|
+
readTextFile: false,
|
|
43
|
+
writeTextFile: false,
|
|
44
|
+
...overrides?.fs,
|
|
45
|
+
},
|
|
46
|
+
terminal: overrides?.terminal ?? false,
|
|
47
|
+
...(overrides?.subagents === true ? { subagents: {} } : {}),
|
|
48
|
+
},
|
|
49
|
+
clientInfo,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 执行器与探测共用:把 `subagentSessions` 并进客户端能力覆盖,两条路径问的是同一个问题。 */
|
|
54
|
+
export function acpCapabilityOverrides(input: {
|
|
55
|
+
readonly clientCapabilities?: AcpClientCapabilityOverrides | undefined;
|
|
56
|
+
readonly subagentSessions?: boolean | undefined;
|
|
57
|
+
}): AcpClientCapabilityOverrides | undefined {
|
|
58
|
+
// `subagents` 只由 subagentSessions 决定:覆盖项里带的一律丢弃,免得声明了能力而没有追踪器。
|
|
59
|
+
const rest: {
|
|
60
|
+
-readonly [K in keyof AcpClientCapabilityOverrides]: AcpClientCapabilityOverrides[K];
|
|
61
|
+
} = {
|
|
62
|
+
...input.clientCapabilities,
|
|
63
|
+
};
|
|
64
|
+
delete rest.subagents;
|
|
65
|
+
return input.subagentSessions === true ? { ...rest, subagents: true } : rest;
|
|
66
|
+
}
|