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