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        provisioner: None,
28    }
29}
30
31fn descriptor() -> WorkerDescriptor {
32    WorkerDescriptor {
33        name: "codex".to_string(),
34        family: WorkerFamily::Harness,
35        capabilities: vec![
36            Capability::CodeEdit,
37            Capability::RepoAware,
38            Capability::Reasoning,
39            Capability::ToolRun,
40            Capability::Writing,
41        ],
42        auth_mode: AuthMode::Subscription,
43        quota: QuotaModel {
44            capacity: 150_000.0,
45            refill_per_minute: 15_000.0,
46        },
47        latency: LatencyProfile::Medium,
48        transport: Transport::SubprocessJson,
49        models: Vec::new(),
50    }
51}
52
53fn build_args(prompt: &str, model: Option<&str>) -> Vec<String> {
54    let mut args = vec!["exec".into()];
55    if let Some(m) = model {
56        args.push("--model".into());
57        args.push(m.into());
58    }
59    args.push(prompt.into());
60    args
61}
62
63/// `codex exec` is line/stream oriented rather than a single JSON object, so we
64/// take the captured stdout as the result text.
65fn parse(out: &SubprocessOutput) -> ParsedHarness {
66    ParsedHarness::raw(&out.stdout)
67}
68
69fn line_mapper(line: &str) -> Vec<WorkerEvent> {
70    let t = line.trim();
71    if t.is_empty() {
72        Vec::new()
73    } else {
74        vec![WorkerEvent::Message(t.to_string())]
75    }
76}