Claude & Codex Integration
Relevant source files
The following files were used as context for generating this wiki page:
The Claude and Codex integrations represent two primary AI engine backends within the Morty autonomous coding loop. While Claude (specifically Claude Code) serves as the default engine, Codex provides a specialized alternative for task execution. These integrations allow Morty to translate high-level tasks into actionable code changes by interfacing with their respective Command Line Interfaces (CLIs).
Both engines are abstracted through a consistent interface that handles prompt construction, execution via shell commands, and result parsing. This modularity ensures that the core logic of the autonomous loop—including testing, linting, and git management—remains independent of the specific AI provider used.
Sources: morty.sh:17-18, cli/src/cli/args.ts:51-60, cli/src/engines/claude.ts:11, cli/src/engines/codex.ts:10
Engine Architecture and Execution Flow
The integration architecture follows a provider-consumer pattern where the Morty core (either the Bash script or the TypeScript CLI) invokes specific engine classes or functions to communicate with external AI CLIs.
Core Execution Logic
The following diagram illustrates how Morty selects and executes a task through either the Claude or Codex engine.
The diagram shows the branching logic for engine selection and the distinct CLI flags used for each.
Sources: cli/src/engines/claude.ts:15-20, cli/src/engines/codex.ts:15-22, morty.sh:317-353
Claude Integration (Claude Code)
Claude Code is the default engine for Morty. It is designed for high-interactivity and detailed progress tracking through stream-json output.
Key Implementation Details
The integration utilizes the claude CLI with specific flags to ensure autonomous operation.
- Permissions: Uses
--dangerously-skip-permissionsto allow the AI to perform file operations without manual user approval. - Output Handling: Requests
stream-jsonto allow Morty to monitor the "step" of the AI (e.g., Thinking, Implementing, Testing). - Token Tracking: Claude provides explicit usage data for both input and output tokens.
| Feature | implementation |
|---|---|
| CLI Command | claude |
| Default Model | Anthropic Opus (overridable) |
| Output Format | stream-json |
| Risk Management | --dangerously-skip-permissions |
Sources: cli/src/engines/claude.ts:12-25, morty.sh:348-353
Streaming and Step Detection
The Claude integration supports real-time progress updates by parsing lines from the CLI output to determine the current activity of the agent.
Morty monitors the stream-json output to provide feedback to the user while the task is in progress.
Sources: cli/src/engines/claude.ts:50-70, morty.sh:815-850
Codex Integration
The Codex integration is optimized for task execution via a standard execution flow, often relying on temporary files to retrieve long-form responses.
Key Implementation Details
- Execution Command: Uses
codex execwith the--full-autoflag. - Message Retrieval: Unlike Claude's stream, Codex often writes the final response to a file specified by the
--output-last-messageflag. Morty generates a unique temporary filename for this purpose and cleans it up after reading. - Response Cleaning: The integration automatically strips generic prefixes like "Task completed successfully." to provide clean output.
Sources: cli/src/engines/codex.ts:15-35, morty.sh:343-347
Codex Data Flow
This flowchart details the lifecycle of a Codex request, specifically the handling of the output message file.
Sources: cli/src/engines/codex.ts:14-60
Configuration and Selection
Users can switch between these engines via CLI flags or configuration files.
CLI Options
The following flags determine which engine is initialized:
--claude: Explicitly selects the Claude engine (Default).--codex: Selects the Codex engine.--sonnet: A shortcut for--claude --model sonnet.--model <name>: Overrides the default model for the selected engine (e.g.,opusfor Claude).
Sources: cli/src/cli/args.ts:25-30, morty.sh:455-465
Engine Parameter Mapping
| Parameter | Claude Value | Codex Value |
|---|---|---|
| CLI Command | claude | codex |
| Autonomy Flag | --dangerously-skip-permissions | --full-auto |
| Format Flag | --output-format stream-json | --json |
| Token Usage | Available in JSON | Not exposed (returns 0) |
Sources: cli/src/engines/claude.ts:16-18, cli/src/engines/codex.ts:17-18, morty.sh:885-910
Summary
The Claude and Codex integrations provide Morty with flexible AI backends. Claude is utilized for its rich streaming data and step-by-step progress monitoring, while Codex offers a straightforward execution model. Both integrations share a common goal of providing the autonomous loop with the capability to modify code, run tests, and report progress while abstracting away the underlying CLI complexities.
Sources: cli/src/engines/claude.ts:1-5, cli/src/engines/codex.ts:1-5, morty.sh:1-10