Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/CodexEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
fuzzyFileSearchToolCallId,
} from "./CodexToolCallMapper";
import { stripShellPrefix } from "./CommandUtils";
import {commandToolName, functionToolName} from "./ToolCallName";
import {createTerminalOutputMeta, type TerminalOutputMode} from "./TerminalOutputMode";
import {
createCodexMessagePhaseMeta,
Expand Down Expand Up @@ -765,10 +766,16 @@ export class CodexEventHandler {
private async completeItemEvent(event: ItemCompletedNotification): Promise<UpdateSessionEvent | null> {
switch (event.item.type) {
case "fileChange":
return {
sessionUpdate: "tool_call_update",
toolCallId: event.item.id,
status: event.item.status === "completed" ? "completed" : "failed",
}
case "dynamicToolCall":
return {
sessionUpdate: "tool_call_update",
toolCallId: event.item.id,
name: functionToolName(event.item.tool, event.item.namespace),
status: event.item.status === "completed" ? "completed" : "failed",
}
case "mcpToolCall":
Expand Down Expand Up @@ -1006,9 +1013,11 @@ export class CodexEventHandler {
}

private completeCommandExecutionEvent(item: ThreadItem & { "type": "commandExecution" }): UpdateSessionEvent {
const name = commandToolName(item.source);
const update: UpdateSessionEvent = {
sessionUpdate: "tool_call_update",
toolCallId: item.id,
...(name === undefined ? {} : {name}),
status: item.status === "completed" ? "completed" : "failed",
rawOutput: {
formatted_output: item.aggregatedOutput ?? "",
Expand Down
16 changes: 13 additions & 3 deletions src/CodexToolCallMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
type TerminalOutputMode,
} from "./TerminalOutputMode";
import {createContextCompactionMeta} from "./ContextCompactionMeta";
import {commandToolName, functionToolName} from "./ToolCallName";

type CodexItemStatus = CommandExecutionStatus | PatchApplyStatus | McpToolCallStatus | DynamicToolCallStatus | CollabAgentToolCallStatus;
type AcpToolCallStatus = "pending" | "in_progress" | "completed" | "failed";
Expand Down Expand Up @@ -81,14 +82,19 @@ export async function createFileChangeUpdate(
}

export async function createCommandExecutionUpdate(item: CommandExecutionItem): Promise<UpdateSessionEvent> {
const name = commandToolName(item.source);
const commandAction = item.commandActions.length === 1 ? item.commandActions[0] : undefined;
if (commandAction) {
return createCommandActionEvent(item.id, item.status, item.cwd, commandAction);
return {
...createCommandActionEvent(item.id, item.status, item.cwd, commandAction),
...(name === undefined ? {} : {name}),
};
}
const command = stripShellPrefix(item.command);
return createTerminalCommandEvent({
sessionUpdate: "tool_call",
toolCallId: item.id,
...(name === undefined ? {} : {name}),
kind: "execute",
title: command,
status: toAcpStatus(item.status),
Expand Down Expand Up @@ -157,7 +163,10 @@ export async function createMcpToolCallUpdate(
export async function createDynamicToolCallUpdate(
item: ThreadItem & { type: "dynamicToolCall" }
): Promise<UpdateSessionEvent> {
return createExecuteToolCallUpdate(item, item.tool, { arguments: item.arguments })
return {
...await createExecuteToolCallUpdate(item, item.tool, { arguments: item.arguments }),
name: functionToolName(item.tool, item.namespace),
};
}

export function createImageViewUpdate(
Expand All @@ -168,6 +177,7 @@ export function createImageViewUpdate(
sessionUpdate: "tool_call",
toolCallId: item.id,
kind: "read",
name: "view_image",
title: `View Image ${displayPath}`,
status: "completed",
content: [createContent({
Expand Down Expand Up @@ -269,7 +279,7 @@ export async function createExecuteToolCallUpdate(
title: string,
rawInput?: Record<string, JsonValue | string>,
rawOutput?: Record<string, JsonValue | string | null>,
): Promise<UpdateSessionEvent> {
): Promise<AcpToolCallEvent> {
return {
sessionUpdate: "tool_call",
toolCallId: item.id,
Expand Down
8 changes: 7 additions & 1 deletion src/ResponseItemHistoryFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { CommandAction, Thread, ThreadItem } from "./app-server/v2";
import { createCommandActionEvent } from "./CodexToolCallMapper";
import { createTerminalOutputMeta, type TerminalOutputMode } from "./TerminalOutputMode";
import { createAgentMessageChunk, createCodexMessagePhaseMeta } from "./ContentChunks";
import { functionToolName } from "./ToolCallName";

type JsonRecord = Record<string, unknown>;
type AcpToolCallEvent = Extract<UpdateSessionEvent, { sessionUpdate: "tool_call" }>;
Expand Down Expand Up @@ -368,6 +369,7 @@ function createFunctionCallUpdate(item: JsonRecord): LegacyFunctionCallUpdate |
if (!toolCallId || !name) {
return null;
}
const toolName = functionToolName(name, stringValue(item["namespace"]));

const isExecCommand = name === "exec_command";
const args = parseFunctionArguments(item["arguments"]);
Expand All @@ -376,7 +378,10 @@ function createFunctionCallUpdate(item: JsonRecord): LegacyFunctionCallUpdate |
const commandAction = command ? inferCommandAction(command, cwd) : null;
if (commandAction) {
return {
update: createCommandActionEvent(toolCallId, "inProgress", cwd, commandAction),
update: {
...createCommandActionEvent(toolCallId, "inProgress", cwd, commandAction),
name: toolName,
},
usesTerminal: false,
isExecCommand,
};
Expand All @@ -385,6 +390,7 @@ function createFunctionCallUpdate(item: JsonRecord): LegacyFunctionCallUpdate |
const update: AcpToolCallEvent = {
sessionUpdate: "tool_call",
toolCallId,
name: toolName,
kind: toolKindForFunctionCall(name),
title: titleForFunctionCall(name, args),
status: "in_progress",
Expand Down
19 changes: 19 additions & 0 deletions src/ToolCallName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {CommandExecutionSource} from "./app-server/v2";

/** Matches Codex's flattened ToolName at boundaries that require a single string. */
export function functionToolName(name: string, namespace?: string | null): string {
return `${namespace ?? ""}${name}`;
}

export function commandToolName(source: CommandExecutionSource): string | undefined {
switch (source) {
case "unifiedExecStartup":
return "exec_command";
case "unifiedExecInteraction":
return "write_stdin";
// These sources do not identify a unique model tool.
case "agent":
case "userShell":
return undefined;
}
}
41 changes: 41 additions & 0 deletions src/__tests__/CodexACPAgent/approval-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,46 @@ describe("Approval Events", () => {
}

describe("command approvals", () => {
it.each([
["unifiedExecStartup", "exec_command"],
["unifiedExecInteraction", "write_stdin"],
["agent", undefined],
["userShell", undefined],
] as const)("uses only a known tool name for %s command approvals", async (source, name) => {
const prompt = setupSessionWithPendingPrompt();
fixture.sendServerNotification({
method: "item/started",
params: {
threadId: sessionId,
turnId: "turn-1",
startedAtMs: 0,
item: {
type: "commandExecution",
id: "command-item",
pluginId: null,
scriptPath: null,
command: "npm test",
cwd: "/workspace",
processId: null,
source,
status: "inProgress",
commandActions: [],
aggregatedOutput: null,
exitCode: null,
durationMs: null,
},
},
});
await fixture.getCodexAcpClient().waitForSessionNotifications(sessionId);
fixture.clearAcpConnectionDump();
fixture.setPermissionResponse({outcome: {outcome: "selected", optionId: ApprovalOptionId.AllowOnce}});

await fixture.sendServerRequest("item/commandExecution/requestApproval", commandParams(["accept", "cancel"]));

expect(permissionRequest().toolCall.name).toBe(name);
await finish(prompt);
});

it("emits an autonomous ACP v1 snapshot and maps explicit reject to decline", async () => {
const prompt = setupSessionWithPendingPrompt();
fixture.setPermissionResponse({outcome: {outcome: "selected", optionId: ApprovalOptionId.Decline}});
Expand Down Expand Up @@ -653,6 +693,7 @@ describe("Approval Events", () => {
expect(permissionRequest()).toMatchObject({
toolCall: {
toolCallId: "permissions-item",
name: "request_permissions",
kind: "other",
status: "pending",
title: "Additional sandbox permissions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"update": {
"sessionUpdate": "tool_call_update",
"toolCallId": "dyn-tool-123",
"name": "list_apps",
"status": "completed"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"arguments": {
"includeDisabled": false
}
}
},
"name": "list_apps"
}
}
]
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/CodexACPAgent/data/load-session-history.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@
"arguments": {
"includeDisabled": false
}
}
},
"name": "list_apps"
}
}
]
Expand All @@ -339,6 +340,7 @@
"sessionUpdate": "tool_call",
"toolCallId": "item-image-view-1",
"kind": "read",
"name": "view_image",
"title": "View Image /test/project/input.png",
"status": "completed",
"content": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@
"toolCallId": "call-rg",
"status": "in_progress",
"kind": "search",
"title": "Search for 'Service' in src"
"title": "Search for 'Service' in src",
"name": "exec_command"
}
}
]
Expand Down Expand Up @@ -223,7 +224,8 @@
"toolCallId": "call-rg-failed",
"status": "in_progress",
"kind": "search",
"title": "Search for 'Missing' in src"
"title": "Search for 'Missing' in src",
"name": "exec_command"
}
}
]
Expand Down Expand Up @@ -259,7 +261,8 @@
{
"path": "/test/project/src/index.ts"
}
]
],
"name": "exec_command"
}
}
]
Expand Down Expand Up @@ -290,7 +293,8 @@
"toolCallId": "call-ls",
"status": "in_progress",
"kind": "read",
"title": "List files"
"title": "List files",
"name": "exec_command"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[
{
"sessionUpdate": "tool_call",
"toolCallId": "call-search",
"status": "in_progress",
"kind": "search",
"title": "Search for 'Needle' in src",
"name": "exec_command"
},
{
"sessionUpdate": "tool_call",
"toolCallId": "call-terminal",
"name": "exec_command",
"kind": "execute",
"title": "npm test",
"status": "in_progress",
"content": [
{
"type": "terminal",
"terminalId": "call-terminal"
}
],
"rawInput": {
"command": "npm test",
"cwd": "/workspace",
"arguments": {
"cmd": "npm test",
"workdir": "/workspace",
"yield_time_ms": 1000
}
},
"_meta": {
"terminal_info": {
"cwd": "/workspace",
"terminal_id": "call-terminal"
}
}
},
{
"sessionUpdate": "tool_call",
"toolCallId": "call-patch",
"name": "apply_patch",
"kind": "edit",
"title": "Apply patch",
"status": "in_progress",
"rawInput": {
"name": "apply_patch",
"arguments": {
"patch": "*** Begin Patch\n*** End Patch"
}
}
},
{
"sessionUpdate": "tool_call",
"toolCallId": "call-mcp",
"name": "mcp__docs__find_page",
"kind": "other",
"title": "mcp__docs__find_page",
"status": "in_progress",
"rawInput": {
"name": "mcp__docs__find_page",
"arguments": {
"query": "Tool names"
}
}
},
{
"sessionUpdate": "tool_call",
"toolCallId": "call-namespaced",
"name": "functions.read_file",
"kind": "other",
"title": "read_file",
"status": "in_progress",
"rawInput": {
"name": "read_file",
"arguments": {
"path": "README.md"
}
}
},
{
"sessionUpdate": "tool_call_update",
"toolCallId": "call-search",
"status": "completed",
"rawOutput": {
"output": "Chunk ID: search\nProcess exited with code 0\nOutput:\nsrc/index.ts\n"
}
}
]
Loading