ycodium-acp-executor 0.2.5634 → 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 +2 -1
- 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/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.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 +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- 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.map +1 -1
- 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.map +1 -1
- 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 +9 -0
- 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,323 @@
|
|
|
1
|
+
// @effect-diagnostics nodeBuiltinImport:off
|
|
2
|
+
// @effect-diagnostics globalDate:off
|
|
3
|
+
// @effect-diagnostics globalTimers:off
|
|
4
|
+
import {
|
|
5
|
+
acpApprovalOptions,
|
|
6
|
+
isAcpRequestPermissionParams,
|
|
7
|
+
selectAcpPermissionOptionId,
|
|
8
|
+
selectAutoApprovedAcpPermissionOption,
|
|
9
|
+
} from "../adapter-support.ts";
|
|
10
|
+
import {
|
|
11
|
+
approvalOutcomeForDecision,
|
|
12
|
+
type PendingApproval,
|
|
13
|
+
type PermissionOutcome,
|
|
14
|
+
} from "../approvals.ts";
|
|
15
|
+
import { AcpExecutorRequestError } from "../errors.ts";
|
|
16
|
+
import {
|
|
17
|
+
fileChangesFromAcpPermission,
|
|
18
|
+
makeAcpRequestOpenedEvent,
|
|
19
|
+
makeAcpRequestResolvedEvent,
|
|
20
|
+
} from "../events.ts";
|
|
21
|
+
import { CLIENT_METHODS, RequestError } from "../protocol.ts";
|
|
22
|
+
import { parsePermissionRequest } from "../runtime-model.ts";
|
|
23
|
+
import { defaultPermissionStatement } from "../permission-statement.ts";
|
|
24
|
+
import type { AcpApprovalDecision, AcpRequestPermissionParams } from "../types.ts";
|
|
25
|
+
import { userInputAnswerOptionId, type PendingUserInput } from "../user-input-pending.ts";
|
|
26
|
+
import { isRecord } from "../util.ts";
|
|
27
|
+
import {
|
|
28
|
+
type AcpExtensionContext,
|
|
29
|
+
type AcpUserInputQuestion,
|
|
30
|
+
type AcpUserInputResolution as UserInputResolution,
|
|
31
|
+
} from "../vendor-hooks.ts";
|
|
32
|
+
import type { createExecutorRuntime } from "./runtime.ts";
|
|
33
|
+
import type { AcpExecutorSupport } from "./support.ts";
|
|
34
|
+
|
|
35
|
+
export function createExecutorRequests({
|
|
36
|
+
emit,
|
|
37
|
+
stamp,
|
|
38
|
+
options,
|
|
39
|
+
approvals,
|
|
40
|
+
pendingUserInputs,
|
|
41
|
+
failIfDisposed,
|
|
42
|
+
randomId,
|
|
43
|
+
hooks,
|
|
44
|
+
shared,
|
|
45
|
+
toolCalls,
|
|
46
|
+
}: Pick<
|
|
47
|
+
AcpExecutorSupport,
|
|
48
|
+
"options" | "approvals" | "pendingUserInputs" | "randomId" | "hooks" | "shared" | "toolCalls"
|
|
49
|
+
> &
|
|
50
|
+
Pick<ReturnType<typeof createExecutorRuntime>, "emit" | "stamp" | "failIfDisposed">) {
|
|
51
|
+
const emitResolved = (
|
|
52
|
+
pending: PendingApproval,
|
|
53
|
+
decision: AcpApprovalDecision,
|
|
54
|
+
reason?: string,
|
|
55
|
+
): void => {
|
|
56
|
+
emit(
|
|
57
|
+
makeAcpRequestResolvedEvent({
|
|
58
|
+
stamp: stamp(),
|
|
59
|
+
provider: options.provider,
|
|
60
|
+
threadId: options.threadId,
|
|
61
|
+
turnId: pending.turnId,
|
|
62
|
+
requestId: pending.requestId,
|
|
63
|
+
permissionRequest: pending.permissionRequest,
|
|
64
|
+
decision,
|
|
65
|
+
...(reason !== undefined ? { reason } : {}),
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const settleApproval = (
|
|
71
|
+
pending: PendingApproval,
|
|
72
|
+
outcome: ReturnType<typeof approvalOutcomeForDecision>,
|
|
73
|
+
decision: AcpApprovalDecision,
|
|
74
|
+
reason?: string,
|
|
75
|
+
): boolean => {
|
|
76
|
+
if (!approvals.settle(pending, outcome)) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
emitResolved(pending, decision, reason);
|
|
80
|
+
return true;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const settleAllApprovals = (decision: AcpApprovalDecision): void => {
|
|
84
|
+
for (const entry of approvals.settleAll({ outcome: "cancelled" })) {
|
|
85
|
+
emitResolved(entry, decision);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const emitUserInputResolved = (
|
|
90
|
+
pending: PendingUserInput,
|
|
91
|
+
answers: Record<string, unknown>,
|
|
92
|
+
): void => {
|
|
93
|
+
emit({
|
|
94
|
+
type: "user-input.resolved",
|
|
95
|
+
...stamp(),
|
|
96
|
+
provider: options.provider,
|
|
97
|
+
threadId: options.threadId,
|
|
98
|
+
...(pending.turnId !== undefined ? { turnId: pending.turnId } : {}),
|
|
99
|
+
requestId: pending.requestId,
|
|
100
|
+
payload: { answers },
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const settleAllUserInputs = (): void => {
|
|
105
|
+
for (const pending of pendingUserInputs.values()) {
|
|
106
|
+
if (pending.settled) continue;
|
|
107
|
+
pending.settled = true;
|
|
108
|
+
pending.resolve({ kind: "cancelled" });
|
|
109
|
+
}
|
|
110
|
+
pendingUserInputs.clear();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 把一组问题交给用户,等回答。权限那条(Kimi 借 permission 传提问)与厂商扩展那条
|
|
115
|
+
* (Grok 的 `_x.ai/ask_user_question`)共用这一份等待表——两边各自登记会让
|
|
116
|
+
* `settleAllUserInputs` 漏掉一半,CLI 崩溃时那一半永远不 settle。
|
|
117
|
+
*/
|
|
118
|
+
const askUserInput = async (input: {
|
|
119
|
+
readonly questions: ReadonlyArray<AcpUserInputQuestion>;
|
|
120
|
+
readonly method: string;
|
|
121
|
+
readonly payload: unknown;
|
|
122
|
+
}): Promise<UserInputResolution> => {
|
|
123
|
+
// dispose 之后迟到的请求不得再登记:清理已经跑完,塞进去就再也 settle 不了。
|
|
124
|
+
failIfDisposed();
|
|
125
|
+
|
|
126
|
+
const requestId = randomId();
|
|
127
|
+
const { promise: settlement, resolve: settle } = Promise.withResolvers<UserInputResolution>();
|
|
128
|
+
const pending: PendingUserInput = {
|
|
129
|
+
requestId,
|
|
130
|
+
questions: input.questions,
|
|
131
|
+
turnId: shared.activeTurnId,
|
|
132
|
+
settled: false,
|
|
133
|
+
resolve: settle,
|
|
134
|
+
};
|
|
135
|
+
pendingUserInputs.set(requestId, pending);
|
|
136
|
+
emit({
|
|
137
|
+
type: "user-input.requested",
|
|
138
|
+
...stamp(),
|
|
139
|
+
provider: options.provider,
|
|
140
|
+
threadId: options.threadId,
|
|
141
|
+
...(shared.activeTurnId !== undefined ? { turnId: shared.activeTurnId } : {}),
|
|
142
|
+
requestId,
|
|
143
|
+
payload: { questions: input.questions },
|
|
144
|
+
raw: {
|
|
145
|
+
source: "acp.jsonrpc",
|
|
146
|
+
method: input.method,
|
|
147
|
+
payload: input.payload,
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
const resolution = await settlement;
|
|
151
|
+
pendingUserInputs.delete(requestId);
|
|
152
|
+
emitUserInputResolved(pending, resolution.kind === "answered" ? resolution.answers : {});
|
|
153
|
+
return resolution;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/** 每次扩展请求现造一份,把真实方法名与原始载荷带进 `raw`,排障时看得出是谁问的。 */
|
|
157
|
+
const makeExtensionContext = (method: string, params: unknown): AcpExtensionContext => ({
|
|
158
|
+
askUserInput: (questions) => askUserInput({ questions, method, payload: params }),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
/** 厂商钩子认出的提问/计划走 `user-input.*`,返回 undefined 表示没接管。 */
|
|
162
|
+
const handlePermissionAsUserInput = async (
|
|
163
|
+
params: AcpRequestPermissionParams,
|
|
164
|
+
): Promise<unknown | undefined> => {
|
|
165
|
+
if (hooks.permissionUserInput === undefined) return undefined;
|
|
166
|
+
let question: AcpUserInputQuestion | undefined;
|
|
167
|
+
try {
|
|
168
|
+
question = hooks.permissionUserInput(params);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
// 认出来却形状不全的交互绝不能降格成批准/拒绝两档——回协议错误。
|
|
171
|
+
throw RequestError.invalidParams({
|
|
172
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
if (question === undefined) return undefined;
|
|
176
|
+
|
|
177
|
+
const resolution = await askUserInput({
|
|
178
|
+
questions: [question],
|
|
179
|
+
method: CLIENT_METHODS.session_request_permission,
|
|
180
|
+
payload: params,
|
|
181
|
+
});
|
|
182
|
+
const optionId =
|
|
183
|
+
resolution.kind === "answered"
|
|
184
|
+
? userInputAnswerOptionId(question, resolution.answers)
|
|
185
|
+
: undefined;
|
|
186
|
+
return {
|
|
187
|
+
outcome:
|
|
188
|
+
optionId !== undefined ? { outcome: "selected", optionId } : { outcome: "cancelled" },
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const handlePermissionRequest = async (params: unknown): Promise<unknown> => {
|
|
193
|
+
if (!isAcpRequestPermissionParams(params)) {
|
|
194
|
+
throw RequestError.invalidParams({ detail: "Malformed session/request_permission" });
|
|
195
|
+
}
|
|
196
|
+
const userInputOutcome = await handlePermissionAsUserInput(params);
|
|
197
|
+
if (userInputOutcome !== undefined) return userInputOutcome;
|
|
198
|
+
const tracked = toolCalls.get(params.toolCall.toolCallId);
|
|
199
|
+
const parsed = parsePermissionRequest(params, tracked);
|
|
200
|
+
const overriddenKind = hooks.permissionRequestKind?.({
|
|
201
|
+
title: params.toolCall.title ?? tracked?.rawTitle,
|
|
202
|
+
kind: parsed.kind,
|
|
203
|
+
rawInput: parsed.toolCall?.data["rawInput"],
|
|
204
|
+
});
|
|
205
|
+
const permissionRequest =
|
|
206
|
+
overriddenKind === undefined ? parsed : { ...parsed, kind: overriddenKind };
|
|
207
|
+
if (options.autoApprove?.(permissionRequest) === true) {
|
|
208
|
+
// 选项表认不出唯一的「允许」项就照常弹给用户,不替它猜。
|
|
209
|
+
const optionId = selectAutoApprovedAcpPermissionOption(params);
|
|
210
|
+
if (optionId !== undefined) {
|
|
211
|
+
return { outcome: { outcome: "selected", optionId } };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const permissionContext = {
|
|
215
|
+
request: params,
|
|
216
|
+
permissionRequest,
|
|
217
|
+
previousToolCall: tracked?.state,
|
|
218
|
+
};
|
|
219
|
+
const statement =
|
|
220
|
+
hooks.permissionStatement?.(permissionContext) ??
|
|
221
|
+
defaultPermissionStatement(permissionRequest);
|
|
222
|
+
const fileChanges =
|
|
223
|
+
hooks.permissionFileChanges?.(permissionContext) ??
|
|
224
|
+
fileChangesFromAcpPermission(permissionRequest);
|
|
225
|
+
const requestId = randomId();
|
|
226
|
+
const outcome = await new Promise<PermissionOutcome>((resolve) => {
|
|
227
|
+
const pending: PendingApproval = {
|
|
228
|
+
requestId,
|
|
229
|
+
request: params,
|
|
230
|
+
permissionRequest,
|
|
231
|
+
turnId: shared.activeTurnId,
|
|
232
|
+
settled: false,
|
|
233
|
+
resolve,
|
|
234
|
+
};
|
|
235
|
+
approvals.add(pending);
|
|
236
|
+
emit(
|
|
237
|
+
makeAcpRequestOpenedEvent({
|
|
238
|
+
stamp: stamp(),
|
|
239
|
+
provider: options.provider,
|
|
240
|
+
threadId: options.threadId,
|
|
241
|
+
turnId: shared.activeTurnId,
|
|
242
|
+
requestId,
|
|
243
|
+
permissionRequest,
|
|
244
|
+
detail: statement.command ?? permissionRequest.detail,
|
|
245
|
+
args: permissionRequest.toolCall?.data["rawInput"] ?? params.toolCall.rawInput,
|
|
246
|
+
statement,
|
|
247
|
+
options: acpApprovalOptions(params),
|
|
248
|
+
fileChanges,
|
|
249
|
+
source: "acp.jsonrpc",
|
|
250
|
+
method: "session/request_permission",
|
|
251
|
+
rawPayload: params,
|
|
252
|
+
}),
|
|
253
|
+
);
|
|
254
|
+
});
|
|
255
|
+
return { outcome };
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const respondToRequest = async (
|
|
259
|
+
requestId: string,
|
|
260
|
+
decision: AcpApprovalDecision,
|
|
261
|
+
reason?: string,
|
|
262
|
+
): Promise<void> => {
|
|
263
|
+
failIfDisposed();
|
|
264
|
+
const apply = (): void => {
|
|
265
|
+
const pending = approvals.get(requestId);
|
|
266
|
+
if (!pending || pending.requestId !== requestId) {
|
|
267
|
+
throw new AcpExecutorRequestError(
|
|
268
|
+
"session/request_permission",
|
|
269
|
+
`Unknown pending approval request: ${requestId}`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
if (pending.settled) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const selectedOptionId =
|
|
276
|
+
decision === "cancel" ? undefined : selectAcpPermissionOptionId(pending.request, decision);
|
|
277
|
+
if (decision !== "cancel" && selectedOptionId === undefined) {
|
|
278
|
+
throw new AcpExecutorRequestError(
|
|
279
|
+
"session/request_permission",
|
|
280
|
+
`Unsupported approval decision for this request: ${decision}`,
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
settleApproval(
|
|
284
|
+
pending,
|
|
285
|
+
approvalOutcomeForDecision(decision, selectedOptionId),
|
|
286
|
+
decision,
|
|
287
|
+
reason,
|
|
288
|
+
);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// 审批答复同样不走 thread 锁,理由同 `cancel`。
|
|
292
|
+
apply();
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const respondToUserInput = async (requestId: string, answers: unknown): Promise<void> => {
|
|
296
|
+
failIfDisposed();
|
|
297
|
+
const pending = pendingUserInputs.get(requestId);
|
|
298
|
+
if (pending === undefined) {
|
|
299
|
+
throw new AcpExecutorRequestError(
|
|
300
|
+
"session/request_permission",
|
|
301
|
+
`Unknown pending user-input request: ${requestId}`,
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
if (pending.settled) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
pending.settled = true;
|
|
308
|
+
pendingUserInputs.delete(requestId);
|
|
309
|
+
pending.resolve({
|
|
310
|
+
kind: "answered",
|
|
311
|
+
answers: isRecord(answers) ? answers : {},
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
return {
|
|
316
|
+
settleAllApprovals,
|
|
317
|
+
settleAllUserInputs,
|
|
318
|
+
makeExtensionContext,
|
|
319
|
+
handlePermissionRequest,
|
|
320
|
+
respondToRequest,
|
|
321
|
+
respondToUserInput,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// @effect-diagnostics nodeBuiltinImport:off
|
|
2
|
+
// @effect-diagnostics globalDate:off
|
|
3
|
+
// @effect-diagnostics globalTimers:off
|
|
4
|
+
import { makeArtifactEmitter } from "../artifacts.ts";
|
|
5
|
+
import { AcpExecutorDisposedError } from "../errors.ts";
|
|
6
|
+
import { makeAcpAssistantItemEvent } from "../events.ts";
|
|
7
|
+
import type { AcpEventStamp, ExecutorEvent } from "../types.ts";
|
|
8
|
+
import type { AcpExecutorSupport } from "./support.ts";
|
|
9
|
+
|
|
10
|
+
export function createExecutorRuntime({
|
|
11
|
+
randomId,
|
|
12
|
+
nowIso,
|
|
13
|
+
eventStream,
|
|
14
|
+
options,
|
|
15
|
+
assistantItemRuntimeId,
|
|
16
|
+
promptMutex,
|
|
17
|
+
shared,
|
|
18
|
+
}: Pick<
|
|
19
|
+
AcpExecutorSupport,
|
|
20
|
+
| "randomId"
|
|
21
|
+
| "nowIso"
|
|
22
|
+
| "eventStream"
|
|
23
|
+
| "options"
|
|
24
|
+
| "assistantItemRuntimeId"
|
|
25
|
+
| "promptMutex"
|
|
26
|
+
| "shared"
|
|
27
|
+
>) {
|
|
28
|
+
const stamp = (): AcpEventStamp => ({
|
|
29
|
+
eventId: randomId(),
|
|
30
|
+
createdAt: nowIso(),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// 代次失效后不许回写状态;正在关闭时,已登记结果的收尾仍属于当前代次。
|
|
34
|
+
const isLive = (expectedGeneration: number): boolean =>
|
|
35
|
+
!shared.disposed && shared.generation === expectedGeneration;
|
|
36
|
+
|
|
37
|
+
const publish = (event: ExecutorEvent): void => {
|
|
38
|
+
if (shared.disposed) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
eventStream.publish(event);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const emit: (event: ExecutorEvent) => void = publish;
|
|
45
|
+
|
|
46
|
+
const emitArtifacts = makeArtifactEmitter({
|
|
47
|
+
emit,
|
|
48
|
+
stamp,
|
|
49
|
+
options,
|
|
50
|
+
turnId: () => shared.activeTurnId,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const failIfDisposed = (): void => {
|
|
54
|
+
if (shared.disposed || shared.disposing) {
|
|
55
|
+
throw new AcpExecutorDisposedError();
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const closeActiveAssistantSegment = (turnId: string | undefined): void => {
|
|
60
|
+
if (!shared.assistantSegment.activeItemId) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const itemId = shared.assistantSegment.activeItemId;
|
|
64
|
+
shared.assistantSegment = { nextSegmentIndex: shared.assistantSegment.nextSegmentIndex };
|
|
65
|
+
emit(
|
|
66
|
+
makeAcpAssistantItemEvent({
|
|
67
|
+
stamp: stamp(),
|
|
68
|
+
provider: options.provider,
|
|
69
|
+
threadId: options.threadId,
|
|
70
|
+
turnId,
|
|
71
|
+
itemId,
|
|
72
|
+
lifecycle: "item.completed",
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const ensureActiveAssistantSegment = (
|
|
78
|
+
acpSessionId: string,
|
|
79
|
+
turnId: string | undefined,
|
|
80
|
+
): string => {
|
|
81
|
+
if (shared.assistantSegment.activeItemId) {
|
|
82
|
+
return shared.assistantSegment.activeItemId;
|
|
83
|
+
}
|
|
84
|
+
const itemId = `assistant:${acpSessionId}:runtime:${assistantItemRuntimeId}:segment:${shared.assistantSegment.nextSegmentIndex}`;
|
|
85
|
+
shared.assistantSegment = {
|
|
86
|
+
nextSegmentIndex: shared.assistantSegment.nextSegmentIndex + 1,
|
|
87
|
+
activeItemId: itemId,
|
|
88
|
+
};
|
|
89
|
+
emit(
|
|
90
|
+
makeAcpAssistantItemEvent({
|
|
91
|
+
stamp: stamp(),
|
|
92
|
+
provider: options.provider,
|
|
93
|
+
threadId: options.threadId,
|
|
94
|
+
turnId,
|
|
95
|
+
itemId,
|
|
96
|
+
lifecycle: "item.started",
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
return itemId;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const notifyPromptSettled = (): void => {
|
|
103
|
+
const waiters = shared.promptWaiters.splice(0);
|
|
104
|
+
for (const waiter of waiters) {
|
|
105
|
+
waiter();
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const waitForPromptIdle = async (timeoutMs: number): Promise<boolean> => {
|
|
110
|
+
if (!promptMutex.isLocked) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return await new Promise<boolean>((resolve) => {
|
|
114
|
+
const timer = setTimeout(() => {
|
|
115
|
+
shared.promptWaiters = shared.promptWaiters.filter((waiter) => waiter !== onSettle);
|
|
116
|
+
resolve(false);
|
|
117
|
+
}, timeoutMs);
|
|
118
|
+
const onSettle = () => {
|
|
119
|
+
clearTimeout(timer);
|
|
120
|
+
resolve(true);
|
|
121
|
+
};
|
|
122
|
+
shared.promptWaiters.push(onSettle);
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
stamp,
|
|
128
|
+
isLive,
|
|
129
|
+
emit,
|
|
130
|
+
emitArtifacts,
|
|
131
|
+
failIfDisposed,
|
|
132
|
+
closeActiveAssistantSegment,
|
|
133
|
+
ensureActiveAssistantSegment,
|
|
134
|
+
notifyPromptSettled,
|
|
135
|
+
waitForPromptIdle,
|
|
136
|
+
};
|
|
137
|
+
}
|