Overview
Manages Codex agent conversation turns by starting, streaming, and cleaning up Codex CLI turn sessions with timeout management and tool result handling.
Description
The Codex Turn Manager orchestrates the full lifecycle of a Codex CLI conversation turn. It starts threads (20s timeout), starts turns (20s timeout), streams `CodexTurnStreamEvent`s from the Codex app server, handles tool result requests, errors, and summary truncation (500 chars). Manages cleanup with a 3s timeout and tracks terminal state to prevent double-completion.
Usage
- Codex agent plugin: Used by `codexAgentPlugin` to handle HTTP streaming turn requests.
- Tool result settlement: Resolves tool call requests from the Codex CLI back to the editor.
Code Reference
Source Location
- Repository: OpenChatCut
- File: server/codex/turn-manager.ts
- Lines: 1-411
Signature
export interface TurnSession {
readonly requestId: string;
readonly client: CodexAppServerClient;
readonly toolNames: ReadonlySet<string>;
readonly emit: StreamSink;
readonly completion: Promise<void>;
readonly finish: (event: Extract<CodexTurnStreamEvent, { type: 'done' | 'error' }>) => boolean;
threadId: string | null;
turnId: string | null;
clientAlive: boolean;
terminal: boolean;
rejectedToolCalls: number;
}
export const codexTurnManager: {
startTurn(request: CodexTurnRequest, tools: CodexAgentToolSpec[], emit: StreamSink): Promise<TurnSession>;
settleToolResult(session: TurnSession, result: CodexToolResultRequest): Promise<boolean>;
};
Import
import { codexTurnManager } from '../codex/turn-manager.ts';
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| request |
CodexTurnRequest |
Yes |
Turn request with prompt and context
|
| tools |
CodexAgentToolSpec[] |
Yes |
Available tool specifications
|
| emit |
StreamSink |
Yes |
Callback for streaming turn events
|
| result |
CodexToolResultRequest |
No |
Tool result for settling pending tool calls
|
Outputs
| Name |
Type |
Description
|
| startTurn() returns |
Promise<TurnSession> |
Active turn session with completion promise
|
| settleToolResult() returns |
Promise<boolean> |
Whether the tool result was accepted
|
| stream events |
CodexTurnStreamEvent |
Streamed via emit callback (text, tool_request, done, error)
|
Usage Examples
Streaming a Codex Turn
import { codexTurnManager } from '../codex/turn-manager.ts';
const session = await codexTurnManager.startTurn(
{ prompt: 'Split clip at 5 seconds', threadId: null },
toolSpecs,
(event) => {
if (event.type === 'text') console.log(event.text);
if (event.type === 'done') console.log('Turn complete');
}
);
await session.completion;
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.