Skip to main content

rinne_workers/adapters/
codex.rs

1//! Codex CLI adapter (`CONTEXT.md` ยง16).
2//!
3//! Drives `codex exec`, which honors the user's ChatGPT login or API key per
4//! their setup. `codex exec` streams progress to stdout and prints the final
5//! message; we capture stdout as the result and stream lines as events.
6
7use std::time::Duration;
8
9use rinne_core::worker::{
10    AuthMode, Capability, LatencyProfile, QuotaModel, Transport, WorkerDescriptor, WorkerEvent,
11    WorkerFamily,
12};
13
14use super::common::{HarnessAdapter, ParsedHarness};
15use crate::transport::subprocess::SubprocessOutput;
16
17pub fn worker() -> HarnessAdapter {
18    HarnessAdapter {
19        descriptor: descriptor(),
20        program: "codex".to_string(),
21        build_args,
22        plan_args: None,
23        parse,
24        line_mapper,
25        prompt_via_stdin: false,
26        default_timeout: Duration::from_secs(600),
27    }
28}
29
30fn descriptor() -> WorkerDescriptor {
31    WorkerDescriptor {
32        name: "codex".to_string(),
33        family: WorkerFamily::Harness,
34        capabilities: vec![
35            Capability::CodeEdit,
36            Capability::RepoAware,
37            Capability::Reasoning,
38            Capability::ToolRun,
39            Capability::Writing,
40        ],
41        auth_mode: AuthMode::Subscription,
42        quota: QuotaModel {
43            capacity: 150_000.0,
44            refill_per_minute: 15_000.0,
45        },
46        latency: LatencyProfile::Medium,
47        transport: Transport::SubprocessJson,
48        models: Vec::new(),
49    }
50}
51
52fn build_args(prompt: &str, model: Option<&str>) -> Vec<String> {
53    let mut args = vec!["exec".into()];
54    if let Some(m) = model {
55        args.push("--model".into());
56        args.push(m.into());
57    }
58    args.push(prompt.into());
59    args
60}
61
62/// `codex exec` is line/stream oriented rather than a single JSON object, so we
63/// take the captured stdout as the result text.
64fn parse(out: &SubprocessOutput) -> ParsedHarness {
65    ParsedHarness::raw(&out.stdout)
66}
67
68fn line_mapper(line: &str) -> Vec<WorkerEvent> {
69    let t = line.trim();
70    if t.is_empty() {
71        Vec::new()
72    } else {
73        vec![WorkerEvent::Message(t.to_string())]
74    }
75}