@zvada/agent-server 0.3.23 → 0.3.24
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/CHANGELOG.md +34 -0
- package/README.md +14 -5
- package/dist/accounts/chatgpt.js +144 -0
- package/dist/accounts/check.js +71 -0
- package/dist/accounts/claude.js +80 -0
- package/dist/accounts/endpoint.js +104 -0
- package/dist/accounts/http.js +58 -0
- package/dist/accounts/index.js +9 -0
- package/dist/client/client.js +686 -0
- package/dist/client/connection.js +338 -0
- package/dist/client/history.js +49 -0
- package/dist/client/index.js +6 -0
- package/dist/client/listeners.js +19 -0
- package/dist/client/logins.js +92 -0
- package/dist/client/rpc.js +105 -0
- package/dist/client/streams.js +432 -0
- package/dist/client/transports.js +118 -0
- package/dist/client/turns.js +122 -0
- package/dist/core/agents/acp/acp-agent.js +425 -0
- package/dist/core/agents/acp/adapter.js +231 -0
- package/dist/core/agents/acp/client.js +222 -0
- package/dist/core/agents/acp/known-agents.js +57 -0
- package/dist/core/agents/acp/mappings.js +164 -0
- package/dist/core/agents/acp/requests.js +79 -0
- package/dist/core/agents/attachments.js +14 -0
- package/dist/core/agents/base.js +64 -0
- package/dist/core/agents/claude-code/adapter.js +474 -0
- package/dist/core/agents/claude-code/approvals.js +245 -0
- package/dist/core/agents/claude-code/claude-agent.js +319 -0
- package/dist/core/agents/claude-code/credentials.js +142 -0
- package/dist/core/agents/claude-code/events.js +11 -0
- package/dist/core/agents/claude-code/login.js +217 -0
- package/dist/core/agents/claude-code/mcp.js +181 -0
- package/dist/core/agents/claude-code/models.js +74 -0
- package/dist/core/agents/claude-code/options.js +240 -0
- package/dist/core/agents/claude-code/outcome.js +204 -0
- package/dist/core/agents/claude-code/results.js +58 -0
- package/dist/core/agents/claude-code/sdk-pair.js +59 -0
- package/dist/core/agents/claude-code/session.js +593 -0
- package/dist/core/agents/claude-code/spawn.js +125 -0
- package/dist/core/agents/claude-code/spend.js +118 -0
- package/dist/core/agents/claude-code/tools.js +131 -0
- package/dist/core/agents/claude-code/turn-usage.js +121 -0
- package/dist/core/agents/claude-code/turn.js +199 -0
- package/dist/core/agents/codex-app-server/adapter.js +463 -0
- package/dist/core/agents/codex-app-server/client.js +338 -0
- package/dist/core/agents/codex-app-server/codex-agent.js +294 -0
- package/dist/core/agents/codex-app-server/commands.js +184 -0
- package/dist/core/agents/codex-app-server/credentials.js +92 -0
- package/dist/core/agents/codex-app-server/effort.js +13 -0
- package/dist/core/agents/codex-app-server/events.js +18 -0
- package/dist/core/agents/codex-app-server/homes.js +118 -0
- package/dist/core/agents/codex-app-server/input.js +52 -0
- package/dist/core/agents/codex-app-server/items.js +307 -0
- package/dist/core/agents/codex-app-server/login.js +183 -0
- package/dist/core/agents/codex-app-server/mcp.js +323 -0
- package/dist/core/agents/codex-app-server/models.js +160 -0
- package/dist/core/agents/codex-app-server/options.js +155 -0
- package/dist/core/agents/codex-app-server/plan-approval.js +124 -0
- package/dist/core/agents/codex-app-server/protocol.js +38 -0
- package/dist/core/agents/codex-app-server/requests.js +250 -0
- package/dist/core/agents/codex-app-server/session.js +278 -0
- package/dist/core/agents/codex-app-server/subagents.js +260 -0
- package/dist/core/agents/codex-app-server/thread-settings.js +214 -0
- package/dist/core/agents/codex-app-server/turn-items.js +69 -0
- package/dist/core/agents/codex-app-server/turn.js +448 -0
- package/dist/core/agents/codex-app-server/user-login.js +78 -0
- package/dist/core/agents/config-fingerprint.js +10 -0
- package/dist/core/agents/credentials.js +71 -0
- package/dist/core/agents/diffs.js +27 -0
- package/dist/core/agents/elicitation.js +42 -0
- package/dist/core/agents/inherit-config.js +5 -0
- package/dist/core/agents/login.js +56 -0
- package/dist/core/agents/mcp-config.js +109 -0
- package/dist/core/agents/plan.js +6 -0
- package/dist/core/agents/process-pool.js +307 -0
- package/dist/core/agents/registry.js +49 -0
- package/dist/core/agents/scripted/adapter.js +86 -0
- package/dist/core/agents/scripted/mcp-call.js +74 -0
- package/dist/core/agents/scripted/scripted-agent.js +330 -0
- package/dist/core/agents/scripted/scripts.js +154 -0
- package/dist/core/agents/scripted/steps.js +87 -0
- package/dist/core/agents/session-agent.js +209 -0
- package/dist/core/agents/stderr-tail.js +71 -0
- package/dist/core/agents/thinking-levels.js +9 -0
- package/dist/core/agents/types.js +1 -0
- package/dist/core/diagnostics.js +23 -0
- package/dist/core/index.js +31 -0
- package/dist/core/presets.js +114 -0
- package/dist/core/processes/identity.js +91 -0
- package/dist/core/processes/lifeline.js +210 -0
- package/dist/core/processes/native-processes.js +299 -0
- package/dist/core/processes/process-table.js +163 -0
- package/dist/core/provision/cache-trust.js +121 -0
- package/dist/core/provision/cache-usage.js +61 -0
- package/dist/core/provision/checksums.js +22 -0
- package/dist/core/provision/extract.js +28 -0
- package/dist/core/provision/index.js +4 -0
- package/dist/core/provision/install-build.js +99 -0
- package/dist/core/provision/npm.js +316 -0
- package/dist/core/provision/pins.js +35 -0
- package/dist/core/provision/platform.js +65 -0
- package/dist/core/provision/provisioner.js +372 -0
- package/dist/core/provision/releases.js +68 -0
- package/dist/core/provision/tools.js +279 -0
- package/dist/core/proxy/anthropic-proxy.js +163 -0
- package/dist/core/proxy/api-key-store.js +30 -0
- package/dist/core/proxy/index.js +3 -0
- package/dist/core/proxy/loopback.js +86 -0
- package/dist/core/runtime/agent-runtime.js +434 -0
- package/dist/core/runtime/event-processor.js +379 -0
- package/dist/core/runtime/event-sink.js +65 -0
- package/dist/core/runtime/harness-clis.js +83 -0
- package/dist/core/runtime/harness-logins.js +313 -0
- package/dist/core/runtime/login-reads.js +71 -0
- package/dist/core/runtime/model-lists.js +160 -0
- package/dist/core/runtime/permission-policy.js +84 -0
- package/dist/core/runtime/request-broker.js +139 -0
- package/dist/core/runtime/steering.js +79 -0
- package/dist/core/runtime/turn-outcome.js +16 -0
- package/dist/core/runtime/turn.js +329 -0
- package/dist/core/utils/bounded.js +13 -0
- package/dist/core/utils/deadline.js +57 -0
- package/dist/core/utils/errors.js +176 -0
- package/dist/core/utils/is-record.js +4 -0
- package/dist/core/utils/loopback.js +11 -0
- package/dist/index.js +4 -0
- package/dist/operator/index.js +10 -0
- package/dist/operator/mcp.js +139 -0
- package/dist/operator/operator.js +499 -0
- package/dist/operator/render.js +408 -0
- package/dist/operator/view.js +35 -0
- package/dist/protocol/account.js +96 -0
- package/dist/protocol/async-queue.js +65 -0
- package/dist/protocol/auth.js +169 -0
- package/dist/protocol/canonical.js +18 -0
- package/dist/protocol/clis.js +63 -0
- package/dist/protocol/config.js +137 -0
- package/dist/protocol/conversation.js +231 -0
- package/dist/protocol/credentials.js +141 -0
- package/dist/protocol/errors.js +181 -0
- package/dist/protocol/execution.js +28 -0
- package/dist/protocol/factories.js +231 -0
- package/dist/protocol/guards.js +71 -0
- package/dist/protocol/harness.js +93 -0
- package/dist/protocol/history.js +201 -0
- package/dist/protocol/ids.js +37 -0
- package/dist/protocol/index.js +36 -0
- package/dist/protocol/lifecycle.js +562 -0
- package/dist/protocol/meta-keys.js +17 -0
- package/dist/protocol/meta.js +13 -0
- package/dist/protocol/model-catalog.js +223 -0
- package/dist/protocol/models.js +84 -0
- package/dist/protocol/part-input.js +94 -0
- package/dist/protocol/parts.js +166 -0
- package/dist/protocol/plan.js +31 -0
- package/dist/protocol/reduce.js +484 -0
- package/dist/protocol/selectors.js +136 -0
- package/dist/protocol/seq-cursor.js +37 -0
- package/dist/protocol/stop-reasons.js +41 -0
- package/dist/protocol/thinking.js +31 -0
- package/dist/protocol/time.js +21 -0
- package/dist/protocol/tokens.js +57 -0
- package/dist/protocol/tool-closure.js +70 -0
- package/dist/protocol/tool-state.js +153 -0
- package/dist/protocol/verify.js +467 -0
- package/dist/protocol/vocabulary.js +27 -0
- package/dist/protocol/wire-api.js +94 -0
- package/dist/protocol/wire.js +669 -0
- package/dist/server/acp/binding.js +251 -0
- package/dist/server/acp/session-records.js +53 -0
- package/dist/server/acp/translate.js +401 -0
- package/dist/server/agent-server.js +338 -0
- package/dist/server/bin.js +492 -0
- package/dist/server/feedback.js +216 -0
- package/dist/server/history.js +123 -0
- package/dist/server/index.js +8 -0
- package/dist/server/install.js +171 -0
- package/dist/server/open-parts.js +87 -0
- package/dist/server/routes.js +291 -0
- package/dist/server/secrets.js +19 -0
- package/dist/server/session-gate.js +54 -0
- package/dist/server/session-log.js +66 -0
- package/dist/server/session-store.js +243 -0
- package/dist/server/subscriptions.js +189 -0
- package/dist/server/transports.js +214 -0
- package/dist/server/wire-error.js +26 -0
- package/dist/sync/event-log.js +89 -0
- package/dist/sync/fit.js +202 -0
- package/dist/sync/history-reader.js +33 -0
- package/dist/sync/history-sqlite.js +281 -0
- package/dist/sync/history.js +163 -0
- package/dist/sync/index.js +5 -0
- package/dist/sync/journal-sqlite.js +316 -0
- package/dist/sync/journal.js +10 -0
- package/dist/sync/live-tails.js +85 -0
- package/dist/sync/node-sqlite.js +27 -0
- package/dist/sync/projection.js +32 -0
- package/dist/sync/recovery.js +117 -0
- package/dist/sync/retry.js +45 -0
- package/dist/sync/schema.js +73 -0
- package/dist/sync/sql.js +94 -0
- package/dist/sync/sqlite-file.js +31 -0
- package/dist/sync/sqlite.js +18 -0
- package/dist/sync/worker.js +212 -0
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +4 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/options.d.ts +24 -11
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/session.d.ts +8 -0
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +6 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts +12 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +4 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/process-pool.d.ts +8 -0
- package/dist/types/core/agents/process-pool.d.ts.map +1 -1
- package/dist/types/core/presets.d.ts +2 -2
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/server/bin.d.ts +3 -0
- package/dist/types/server/bin.d.ts.map +1 -0
- package/dist/types/server/transports.d.ts +2 -1
- package/dist/types/server/transports.d.ts.map +1 -1
- package/dist/types/sync/node-sqlite.d.ts +4 -1
- package/dist/types/sync/node-sqlite.d.ts.map +1 -1
- package/docs/consuming.md +30 -4
- package/docs/deploy.md +28 -0
- package/docs/harnesses.md +13 -1
- package/package.json +43 -18
- package/src/core/agents/claude-code/approvals.ts +5 -1
- package/src/core/agents/claude-code/claude-agent.ts +32 -5
- package/src/core/agents/claude-code/options.ts +24 -11
- package/src/core/agents/claude-code/session.ts +13 -0
- package/src/core/agents/codex-app-server/homes.ts +11 -2
- package/src/core/agents/codex-app-server/login.ts +30 -0
- package/src/core/agents/codex-app-server/requests.ts +7 -6
- package/src/core/agents/codex-app-server/session.ts +11 -2
- package/src/core/agents/process-pool.ts +30 -4
- package/src/core/presets.ts +3 -2
- package/src/server/bin.ts +25 -10
- package/src/server/transports.ts +6 -1
- package/src/sync/node-sqlite.ts +19 -3
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { onAbort } from "../../utils/deadline.js";
|
|
2
|
+
import { PLAN_OPTIONS, PLAN_TITLE, approvedPlanMode } from "../plan.js";
|
|
3
|
+
import { CODEX_PLAN_DECIDED } from "./events.js";
|
|
4
|
+
import { textInput } from "./input.js";
|
|
5
|
+
import { CODEX_TOOL_KINDS } from "./items.js";
|
|
6
|
+
import { mapSandbox } from "./options.js";
|
|
7
|
+
import { inCollaborationMode } from "./thread-settings.js";
|
|
8
|
+
/**
|
|
9
|
+
* Codex's built-in permission profile for the workspace-write sandbox (0.160).
|
|
10
|
+
* A turn switches a running thread to it, keeping the thread's writable roots.
|
|
11
|
+
*/
|
|
12
|
+
const WORKSPACE_PROFILE = ":workspace";
|
|
13
|
+
/** What Codex's TUI sends to implement the plan it showed (0.160). */
|
|
14
|
+
const IMPLEMENT_THE_PLAN = "Implement the plan.";
|
|
15
|
+
/** Codex exited while the user decided on its plan: nothing can carry the answer out. */
|
|
16
|
+
function exitedBeforeAnswer(client) {
|
|
17
|
+
return client.exitError ?? new Error("codex app-server exited before its plan was answered");
|
|
18
|
+
}
|
|
19
|
+
// @ref LLP 0001#d4--permission-requests-are-first-class-protocol — Codex's plan, put to the user like Claude's
|
|
20
|
+
/**
|
|
21
|
+
* Codex ends its Plan-mode turn with the plan and asks nothing; its TUI then
|
|
22
|
+
* asks whether to implement it, as the engine does here. The answer runs as
|
|
23
|
+
* Codex's next turn, within this one, which this returns; nothing when the
|
|
24
|
+
* turn ends here. A turn that does not go on once Codex's ends (a managed
|
|
25
|
+
* one), or has nobody to ask, presents the plan as written.
|
|
26
|
+
*/
|
|
27
|
+
export async function* askPlan(turn, native) {
|
|
28
|
+
const offered = native.plan;
|
|
29
|
+
const completed = native.outcome === "completed";
|
|
30
|
+
// Cancelled with a plan on the table: the turn was cancelled, not done.
|
|
31
|
+
if (offered && completed && turn.signal.aborted)
|
|
32
|
+
throw new Error("turn aborted");
|
|
33
|
+
if (!offered || !completed || turn.signal.aborted)
|
|
34
|
+
return undefined;
|
|
35
|
+
const decided = (decision) => ({
|
|
36
|
+
method: CODEX_PLAN_DECIDED,
|
|
37
|
+
params: { itemId: offered.itemId, ...decision },
|
|
38
|
+
});
|
|
39
|
+
const handler = turn.asksAboutPlans ? turn.scope.onPermissionRequest : undefined;
|
|
40
|
+
if (!handler) {
|
|
41
|
+
yield decided({ decision: "presented" });
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const { session } = turn;
|
|
45
|
+
const { client } = session;
|
|
46
|
+
// Steers sent while the user decides wait for Codex's next turn.
|
|
47
|
+
turn.steering.hold();
|
|
48
|
+
if (client.closed)
|
|
49
|
+
throw exitedBeforeAnswer(client);
|
|
50
|
+
// The question ends with the turn, or with Codex.
|
|
51
|
+
const asking = new AbortController();
|
|
52
|
+
const stopAsking = () => asking.abort();
|
|
53
|
+
const offAbort = onAbort(turn.signal, stopAsking);
|
|
54
|
+
const offClose = client.onClose(stopAsking);
|
|
55
|
+
let decision;
|
|
56
|
+
try {
|
|
57
|
+
decision = await handler({
|
|
58
|
+
toolCallId: offered.itemId,
|
|
59
|
+
toolName: "plan",
|
|
60
|
+
kind: CODEX_TOOL_KINDS.plan,
|
|
61
|
+
title: PLAN_TITLE,
|
|
62
|
+
rawInput: { plan: offered.text },
|
|
63
|
+
}, { signal: asking.signal, options: PLAN_OPTIONS });
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
offAbort();
|
|
67
|
+
offClose();
|
|
68
|
+
}
|
|
69
|
+
if (turn.signal.aborted)
|
|
70
|
+
throw new Error("turn aborted");
|
|
71
|
+
if (client.closed)
|
|
72
|
+
throw exitedBeforeAnswer(client);
|
|
73
|
+
// What finished while the question was open, before its answer.
|
|
74
|
+
yield* turn.commands.drainLate();
|
|
75
|
+
let input;
|
|
76
|
+
let sandbox = session.sandbox;
|
|
77
|
+
const params = { threadId: session.threadId };
|
|
78
|
+
if (decision.decision === "allow") {
|
|
79
|
+
turn.scope.mode = approvedPlanMode(decision.optionId);
|
|
80
|
+
yield decided({ decision: "approved", permissionMode: turn.scope.mode });
|
|
81
|
+
// An edited plan is the plan approved: Codex implements that one.
|
|
82
|
+
const edited = decision.updatedInput?.plan;
|
|
83
|
+
input =
|
|
84
|
+
typeof edited === "string" && edited !== offered.text
|
|
85
|
+
? `Implement this plan:\n\n${edited}`
|
|
86
|
+
: IMPLEMENT_THE_PLAN;
|
|
87
|
+
// Auto-accepted edits need the workspace-write sandbox; Codex switches a
|
|
88
|
+
// running thread to it for this turn and the next.
|
|
89
|
+
if (mapSandbox(turn.scope.mode) !== session.sandbox) {
|
|
90
|
+
params.permissions = WORKSPACE_PROFILE;
|
|
91
|
+
sandbox = mapSandbox(turn.scope.mode);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if (decision.decision === "cancel") {
|
|
95
|
+
// Nobody answered (or the client cancelled): the turn ends cancelled, as
|
|
96
|
+
// a Claude plan's does, not as a wish to keep planning. What the plan
|
|
97
|
+
// turn left running was left on purpose and keeps running: not
|
|
98
|
+
// cancelled, its outcome unknown.
|
|
99
|
+
yield* turn.endCancelled();
|
|
100
|
+
yield decided({ decision: "cancelled" });
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const message = decision.message;
|
|
105
|
+
yield decided({ decision: "kept_planning", ...(message && { message }) });
|
|
106
|
+
// Nothing said: the user tells the agent what to change in their next turn.
|
|
107
|
+
if (!message)
|
|
108
|
+
return undefined;
|
|
109
|
+
input = message;
|
|
110
|
+
}
|
|
111
|
+
// What finished while the answer streamed, before the next turn's events:
|
|
112
|
+
// shown before the abort's check, as nothing from that check to the next
|
|
113
|
+
// turn's start may pause for a cancel to slip past.
|
|
114
|
+
yield* turn.commands.drainLate();
|
|
115
|
+
// A cancel while the stream took the answer: nothing more runs.
|
|
116
|
+
if (turn.signal.aborted)
|
|
117
|
+
throw new Error("turn aborted");
|
|
118
|
+
const mode = inCollaborationMode(session.thread.settings, turn.scope.mode);
|
|
119
|
+
return {
|
|
120
|
+
params: { ...params, ...mode.params, input: [textInput(input)] },
|
|
121
|
+
settings: mode.next,
|
|
122
|
+
sandbox,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { isRecord } from "../../utils/is-record.js";
|
|
2
|
+
/** An item names its type and id; the rest is Codex's, as each type has it. */
|
|
3
|
+
function isItem(value) {
|
|
4
|
+
return isRecord(value) && typeof value.type === "string" && typeof value.id === "string";
|
|
5
|
+
}
|
|
6
|
+
/** The item a notification carries (`item/started`, `item/completed`), when it names its type and id. */
|
|
7
|
+
export function codexItem(n) {
|
|
8
|
+
const { item } = n.params;
|
|
9
|
+
return isItem(item) ? item : undefined;
|
|
10
|
+
}
|
|
11
|
+
/** The turn a notification names (`turn/started`, `turn/completed`). */
|
|
12
|
+
export function codexTurn(n) {
|
|
13
|
+
const { turn } = n.params;
|
|
14
|
+
if (!isRecord(turn))
|
|
15
|
+
return undefined;
|
|
16
|
+
const { error } = turn;
|
|
17
|
+
return {
|
|
18
|
+
...(typeof turn.id === "string" && { id: turn.id }),
|
|
19
|
+
...(typeof turn.status === "string" && { status: turn.status }),
|
|
20
|
+
...(isRecord(error) && {
|
|
21
|
+
error: {
|
|
22
|
+
...(typeof error.message === "string" && { message: error.message }),
|
|
23
|
+
codexErrorInfo: error.codexErrorInfo,
|
|
24
|
+
},
|
|
25
|
+
}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* An `error` Codex will not retry: its turn failed, and it ends that turn with
|
|
30
|
+
* `turn/completed` next. Seen on 0.160, some time later.
|
|
31
|
+
*/
|
|
32
|
+
export function failing(n) {
|
|
33
|
+
return n.method === "error" && n.params.willRetry !== true;
|
|
34
|
+
}
|
|
35
|
+
/** An `error` Codex retries what failed after: the turn goes on, and more is to come. */
|
|
36
|
+
export function retrying(n) {
|
|
37
|
+
return n.method === "error" && n.params.willRetry === true;
|
|
38
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { generateUUIDv7, } from "../../../protocol/index.js";
|
|
2
|
+
import { isRecord } from "../../utils/is-record.js";
|
|
3
|
+
import { NO_ANSWERS, inputReply, inputRequest, questionRequest, unanswered, } from "../elicitation.js";
|
|
4
|
+
import { refreshTokens } from "./credentials.js";
|
|
5
|
+
import { CODEX_TOOL_KINDS, codexChangeView, codexCommandLine } from "./items.js";
|
|
6
|
+
/** Whether the thread a request comes from is the session's, or one its turn's sub-agents run in. */
|
|
7
|
+
async function owns(session, turn, thread) {
|
|
8
|
+
if (!thread || thread === session.threadId)
|
|
9
|
+
return true;
|
|
10
|
+
return Boolean(typeof thread === "string" &&
|
|
11
|
+
turn.subagents &&
|
|
12
|
+
(await turn.subagents.owns(thread).catch(() => false)));
|
|
13
|
+
}
|
|
14
|
+
/** Ask the turn that has the session now, through its broker for `kind`. */
|
|
15
|
+
async function askCurrentTurn(session, params, kind, ask) {
|
|
16
|
+
const turn = session.turn;
|
|
17
|
+
const handler = turn?.[kind];
|
|
18
|
+
if (!turn || !handler || turn.signal.aborted || !(await owns(session, turn, params.threadId)))
|
|
19
|
+
return { refused: true };
|
|
20
|
+
// Ownership can take a thread/read round-trip: the turn may have ended.
|
|
21
|
+
if (session.turn !== turn || turn.signal.aborted)
|
|
22
|
+
return { refused: true };
|
|
23
|
+
const answer = await ask(handler, turn);
|
|
24
|
+
return session.turn !== turn || turn.signal.aborted ? { stale: true } : { answer };
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Answer Codex's requests (server → client) for the turn that has the session.
|
|
28
|
+
* A request it cannot answer gets a refusal in its own shape, never a hang.
|
|
29
|
+
*/
|
|
30
|
+
export function bindServerRequests(session) {
|
|
31
|
+
session.client.setRequestHandler(async (method, params) => {
|
|
32
|
+
switch (method) {
|
|
33
|
+
case "account/chatgptAuthTokens/refresh":
|
|
34
|
+
return refreshTokens(session, params);
|
|
35
|
+
case "mcpServer/elicitation/request":
|
|
36
|
+
return isMcpToolConsent(params)
|
|
37
|
+
? answerConsent(session, params)
|
|
38
|
+
: answerMcpElicitation(session, params);
|
|
39
|
+
case "item/tool/requestUserInput":
|
|
40
|
+
return answerQuestions(session, params);
|
|
41
|
+
case "item/commandExecution/requestApproval":
|
|
42
|
+
case "item/fileChange/requestApproval":
|
|
43
|
+
return answerApproval(session, method, params);
|
|
44
|
+
default:
|
|
45
|
+
// Other server requests have their own response shapes. Sending them
|
|
46
|
+
// to the approval broker can leave a turn waiting forever.
|
|
47
|
+
throw new Error(`Unsupported Codex server request: ${method}`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/** Codex asks consent for an MCP tool call as an MCP elicitation of this kind. */
|
|
52
|
+
function isMcpToolConsent(params) {
|
|
53
|
+
return isRecord(params._meta) && params._meta.codex_approval_kind === "mcp_tool_call";
|
|
54
|
+
}
|
|
55
|
+
/** An MCP elicitation's reply: Codex takes explicit nulls. */
|
|
56
|
+
const elicitationReply = (action, content = null) => ({
|
|
57
|
+
action,
|
|
58
|
+
content,
|
|
59
|
+
_meta: null,
|
|
60
|
+
});
|
|
61
|
+
/**
|
|
62
|
+
* Codex's consent for an MCP tool call is a tool approval like any other, so
|
|
63
|
+
* the engine broker answers it. It cannot carry edited arguments: what was
|
|
64
|
+
* approved is what runs, and an edited approval declines.
|
|
65
|
+
*/
|
|
66
|
+
async function answerConsent(session, params) {
|
|
67
|
+
const asked = await askCurrentTurn(session, params, "onPermissionRequest", (handler, turn) => handler(mcpConsentToolCall(params, turn.items)));
|
|
68
|
+
if (!("answer" in asked))
|
|
69
|
+
return elicitationReply("decline");
|
|
70
|
+
const decision = asked.answer;
|
|
71
|
+
if (decision.decision === "cancel")
|
|
72
|
+
return elicitationReply("cancel");
|
|
73
|
+
return decision.decision === "allow" && decision.updatedInput === undefined
|
|
74
|
+
? elicitationReply("accept")
|
|
75
|
+
: elicitationReply("decline");
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The consent names its server and arguments, not its call: link it to the
|
|
79
|
+
* started call it is for, so the approval points at that tool part.
|
|
80
|
+
*/
|
|
81
|
+
function mcpConsentToolCall(params, items) {
|
|
82
|
+
const meta = isRecord(params._meta) ? params._meta : {};
|
|
83
|
+
const serverName = typeof params.serverName === "string" ? params.serverName : undefined;
|
|
84
|
+
const server = serverName ?? "mcp";
|
|
85
|
+
const message = typeof params.message === "string" ? params.message : undefined;
|
|
86
|
+
const named = message?.match(/tool "([^"]+)"/)?.[1];
|
|
87
|
+
const call = items.consentedCall(server, typeof params.threadId === "string" ? params.threadId : undefined, named, JSON.stringify(meta.tool_params ?? {}));
|
|
88
|
+
const tool = call?.tool ?? named;
|
|
89
|
+
// The arguments are the tool's input, as a Claude MCP approval's are: a
|
|
90
|
+
// client previews them there.
|
|
91
|
+
const input = meta.tool_params ?? call?.arguments ?? {};
|
|
92
|
+
return {
|
|
93
|
+
toolCallId: call?.id ?? generateUUIDv7(),
|
|
94
|
+
toolName: `${server}.${tool ?? "tool"}`,
|
|
95
|
+
kind: CODEX_TOOL_KINDS.mcp,
|
|
96
|
+
// As Codex names them: the consent's server, and its linked call's tool or
|
|
97
|
+
// else the one its message names (the same either way); neither, no `mcp`.
|
|
98
|
+
...(serverName !== undefined && tool !== undefined && { mcp: { server: serverName, tool } }),
|
|
99
|
+
...(message && { title: message }),
|
|
100
|
+
rawInput: isRecord(input) ? input : { arguments: input },
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* An MCP server's own request for input (a form, or a URL to visit), asked
|
|
105
|
+
* through the engine's elicitation broker; declined when nobody can answer,
|
|
106
|
+
* so the server sees a refusal and not a protocol error.
|
|
107
|
+
*/
|
|
108
|
+
async function answerMcpElicitation(session, params) {
|
|
109
|
+
const mode = params.mode;
|
|
110
|
+
// OpenAI's own modes carry their own schemas: not ours to render.
|
|
111
|
+
if (mode !== "form" && mode !== "url")
|
|
112
|
+
return elicitationReply("decline");
|
|
113
|
+
const serverName = typeof params.serverName === "string" ? params.serverName : undefined;
|
|
114
|
+
const asked = await askCurrentTurn(session, params, "onElicitationRequest", (handler, turn) => handler(inputRequest({
|
|
115
|
+
mode,
|
|
116
|
+
message: typeof params.message === "string" ? params.message : "",
|
|
117
|
+
serverName,
|
|
118
|
+
toolCallId: turn.items.callOf(serverName)?.id,
|
|
119
|
+
requestedSchema: isRecord(params.requestedSchema) ? params.requestedSchema : undefined,
|
|
120
|
+
url: typeof params.url === "string" ? params.url : undefined,
|
|
121
|
+
elicitationId: typeof params.elicitationId === "string" ? params.elicitationId : undefined,
|
|
122
|
+
}), { signal: turn.signal }));
|
|
123
|
+
if ("refused" in asked)
|
|
124
|
+
return elicitationReply("decline");
|
|
125
|
+
if ("stale" in asked)
|
|
126
|
+
return elicitationReply("cancel");
|
|
127
|
+
const answer = inputReply(asked.answer);
|
|
128
|
+
return elicitationReply(answer.action, answer.content ?? null);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Codex's request_user_input (its plan mode asks this way), asked through
|
|
132
|
+
* the engine's elicitation broker. A refusal goes back as an error: Codex
|
|
133
|
+
* then asks in plain text instead of asking again.
|
|
134
|
+
*/
|
|
135
|
+
async function answerQuestions(session, params) {
|
|
136
|
+
const asked = (Array.isArray(params.questions) ? params.questions : []);
|
|
137
|
+
const questions = asked
|
|
138
|
+
.filter((q) => typeof q.id === "string" && typeof q.question === "string")
|
|
139
|
+
.map((q) => ({
|
|
140
|
+
id: q.id,
|
|
141
|
+
question: q.question,
|
|
142
|
+
...(q.header && { header: q.header }),
|
|
143
|
+
...(q.options?.length && {
|
|
144
|
+
options: q.options.map((o) => ({
|
|
145
|
+
label: o.label,
|
|
146
|
+
...(o.description && { description: o.description }),
|
|
147
|
+
})),
|
|
148
|
+
}),
|
|
149
|
+
...(q.isOther && { allowOther: true }),
|
|
150
|
+
...(q.isSecret && { secret: true }),
|
|
151
|
+
}));
|
|
152
|
+
const outcome = await askCurrentTurn(session, params, "onElicitationRequest", (handler, turn) => handler(questionRequest(questions, typeof params.itemId === "string" ? params.itemId : undefined), { signal: turn.signal }));
|
|
153
|
+
if ("refused" in outcome)
|
|
154
|
+
throw new Error(NO_ANSWERS);
|
|
155
|
+
if ("stale" in outcome)
|
|
156
|
+
throw new Error("The turn ended before the user answered.");
|
|
157
|
+
const answer = outcome.answer;
|
|
158
|
+
if (answer.action !== "accept")
|
|
159
|
+
throw new Error(unanswered(answer.action));
|
|
160
|
+
const content = answer.content ?? {};
|
|
161
|
+
return {
|
|
162
|
+
answers: Object.fromEntries(questions.map((q) => {
|
|
163
|
+
const value = content[q.id];
|
|
164
|
+
const answers = Array.isArray(value)
|
|
165
|
+
? value.map(String)
|
|
166
|
+
: value === undefined
|
|
167
|
+
? []
|
|
168
|
+
: [String(value)];
|
|
169
|
+
return [q.id, { answers }];
|
|
170
|
+
})),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* A command or file change Codex asks to run, through the engine broker.
|
|
175
|
+
* Codex's reply is a bare verdict, with no slot for an edited input: what was
|
|
176
|
+
* approved is what runs, so an edited approval declines (only claude-code's
|
|
177
|
+
* `canUseTool` can execute an edit).
|
|
178
|
+
*/
|
|
179
|
+
async function answerApproval(session, method, params) {
|
|
180
|
+
const decline = { decision: "decline" };
|
|
181
|
+
// A plan turn changes no files.
|
|
182
|
+
if (method === "item/fileChange/requestApproval" && session.turn?.mode === "plan")
|
|
183
|
+
return decline;
|
|
184
|
+
const always = codexAlways(method, params);
|
|
185
|
+
const asked = await askCurrentTurn(session, params, "onPermissionRequest", (handler, turn) => handler(approvalToolCall(method, params, turn.items), { always }));
|
|
186
|
+
if (!("answer" in asked))
|
|
187
|
+
return decline;
|
|
188
|
+
const decision = asked.answer;
|
|
189
|
+
if (decision.decision === "cancel")
|
|
190
|
+
return { decision: "cancel" };
|
|
191
|
+
if (decision.decision !== "allow" || decision.updatedInput !== undefined)
|
|
192
|
+
return decline;
|
|
193
|
+
return {
|
|
194
|
+
decision: decision.optionId === "allow_always" && always ? "acceptForSession" : "accept",
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/** Codex's approval request as the engine's PermissionToolCall. */
|
|
198
|
+
function approvalToolCall(method, params, items) {
|
|
199
|
+
const id = typeof params.itemId === "string" ? params.itemId : "approval";
|
|
200
|
+
// @ref LLP 0002#a-tool-call-says-what-it-acts-on — Codex's reason is why it asks, not the call's title
|
|
201
|
+
const reason = typeof params.reason === "string" ? params.reason : undefined;
|
|
202
|
+
if (method === "item/commandExecution/requestApproval") {
|
|
203
|
+
const command = typeof params.command === "string" ? params.command : "";
|
|
204
|
+
const line = codexCommandLine(command);
|
|
205
|
+
return {
|
|
206
|
+
toolCallId: id,
|
|
207
|
+
toolName: "shell",
|
|
208
|
+
kind: CODEX_TOOL_KINDS.shell,
|
|
209
|
+
...(line && { command: line }),
|
|
210
|
+
...(reason && { reason }),
|
|
211
|
+
rawInput: { command, ...(params.cwd ? { cwd: params.cwd } : {}) },
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
// A file-change request names its item, not the edits: those arrived on the
|
|
215
|
+
// item, which Codex starts before it asks. The approval shows them.
|
|
216
|
+
const changes = items.changesOf(id);
|
|
217
|
+
const { locations } = codexChangeView(changes);
|
|
218
|
+
const grantRoot = typeof params.grantRoot === "string" ? params.grantRoot : undefined;
|
|
219
|
+
return {
|
|
220
|
+
toolCallId: id,
|
|
221
|
+
toolName: "apply_patch",
|
|
222
|
+
kind: CODEX_TOOL_KINDS.apply_patch,
|
|
223
|
+
...(reason && { reason }),
|
|
224
|
+
...(locations.length && { locations }),
|
|
225
|
+
rawInput: { changes, ...(grantRoot && { grantRoot }) },
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* What "always" covers for a Codex approval: Codex keeps it itself, for the
|
|
230
|
+
* session (`acceptForSession`). For a plain command that is this exact
|
|
231
|
+
* command ("don't ask again for this command"); for a file change, these
|
|
232
|
+
* files. A network approval's "always" opens a host, and one with extra
|
|
233
|
+
* permissions or a write root grants those, so they offer none. Codex 0.160
|
|
234
|
+
* lists `acceptWithExecpolicyAmendment` for a command instead: a prefix rule
|
|
235
|
+
* it writes to the user's own rules, which an engine must not, so the session
|
|
236
|
+
* approval (which it honors though unlisted) is the one taken.
|
|
237
|
+
*/
|
|
238
|
+
export function codexAlways(method, params) {
|
|
239
|
+
if (method === "item/commandExecution/requestApproval") {
|
|
240
|
+
const plain = typeof params.command === "string" &&
|
|
241
|
+
params.command.length > 0 &&
|
|
242
|
+
(params.kind === undefined || params.kind === "command") &&
|
|
243
|
+
!params.networkApprovalContext &&
|
|
244
|
+
!params.additionalPermissions;
|
|
245
|
+
return plain ? { rule: params.command } : null;
|
|
246
|
+
}
|
|
247
|
+
if (method === "item/fileChange/requestApproval")
|
|
248
|
+
return params.grantRoot ? null : { rule: "changes to these files" };
|
|
249
|
+
return null;
|
|
250
|
+
}
|