unified-agent-api-codex 0.3.5

Async wrapper around the Codex CLI for programmatic prompting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use super::*;

#[cfg(unix)]
#[tokio::test]
async fn app_server_codegen_maps_overrides_and_prettier() {
    let dir = tempfile::tempdir().unwrap();
    let script_path = write_fake_codex(
        dir.path(),
        r#"#!/usr/bin/env bash
echo "$PWD"
printf "%s\n" "$@"
"#,
    );

    let workdir = dir.path().join("workdir");
    std_fs::create_dir_all(&workdir).unwrap();
    let out_dir = dir.path().join("out/ts");
    let prettier = dir.path().join("bin/prettier.js");

    let client = CodexClient::builder()
        .binary(&script_path)
        .mirror_stdout(false)
        .quiet(true)
        .working_dir(&workdir)
        .approval_policy(ApprovalPolicy::OnRequest)
        .search(true)
        .build();

    let result = client
        .generate_app_server_bindings(
            AppServerCodegenRequest::typescript(&out_dir)
                .prettier(&prettier)
                .profile("dev")
                .config_override("features.codegen", "true"),
        )
        .await
        .unwrap();

    let mut lines = result.stdout.lines();
    let pwd = lines.next().unwrap();
    let pwd = std_fs::canonicalize(Path::new(pwd)).unwrap();
    let workdir = std_fs::canonicalize(&workdir).unwrap();
    assert_eq!(pwd, workdir);

    let args: Vec<_> = lines.map(str::to_string).collect();
    assert_eq!(
        args,
        vec![
            "--config",
            "features.codegen=true",
            "--profile",
            "dev",
            "--ask-for-approval",
            "on-request",
            "--search",
            "app-server",
            "generate-ts",
            "--out",
            out_dir.to_string_lossy().as_ref(),
            "--prettier",
            prettier.to_string_lossy().as_ref(),
        ]
    );
    assert!(out_dir.is_dir());
    assert_eq!(result.out_dir, out_dir);
    assert!(result.status.success());
}

#[cfg(unix)]
#[tokio::test]
async fn app_server_codegen_surfaces_non_zero_exit() {
    let dir = tempfile::tempdir().unwrap();
    let script_path = write_fake_codex(
        dir.path(),
        r#"#!/usr/bin/env bash
echo "ts error"
echo "bad format" 1>&2
exit 5
"#,
    );

    let client = CodexClient::builder()
        .binary(&script_path)
        .mirror_stdout(false)
        .quiet(true)
        .build();

    let out_dir = dir.path().join("schema");
    let err = client
        .generate_app_server_bindings(AppServerCodegenRequest::json_schema(&out_dir))
        .await
        .unwrap_err();

    match err {
        CodexError::NonZeroExit { status, stderr } => {
            assert_eq!(status.code(), Some(5));
            assert!(stderr.contains("bad format"));
        }
        other => panic!("expected NonZeroExit, got {other:?}"),
    }
    assert!(out_dir.is_dir());
}

#[cfg(unix)]
#[tokio::test]
async fn app_server_proxy_maps_sock_and_overrides() {
    let dir = tempfile::tempdir().unwrap();
    let socket_path = dir.path().join("proxy.sock");
    let script_path = write_fake_codex(
        dir.path(),
        r#"#!/usr/bin/env bash
echo "$PWD"
printf "%s\n" "$@"
"#,
    );

    let client_workdir = dir.path().join("client-workdir");
    let request_workdir = dir.path().join("proxy-workdir");
    std_fs::create_dir_all(&client_workdir).unwrap();
    std_fs::create_dir_all(&request_workdir).unwrap();

    let client = CodexClient::builder()
        .binary(&script_path)
        .mirror_stdout(false)
        .quiet(true)
        .working_dir(&client_workdir)
        .approval_policy(ApprovalPolicy::OnRequest)
        .build();

    let mut child = client
        .start_app_server_proxy(
            AppServerProxyRequest::new()
                .socket_path(&socket_path)
                .working_dir(&request_workdir)
                .profile("dev")
                .config_override("features.proxy", "true")
                .search(true),
        )
        .unwrap();

    let stdout = child.stdout.take().unwrap();
    let mut lines = BufReader::new(stdout).lines();

    let pwd = lines.next_line().await.unwrap().unwrap();
    let pwd = std_fs::canonicalize(Path::new(&pwd)).unwrap();
    let request_workdir = std_fs::canonicalize(&request_workdir).unwrap();
    assert_eq!(pwd, request_workdir);

    let mut args = Vec::new();
    while let Some(line) = lines.next_line().await.unwrap() {
        args.push(line);
    }
    assert_eq!(
        args,
        vec![
            "--config",
            "features.proxy=true",
            "--profile",
            "dev",
            "--ask-for-approval",
            "on-request",
            "--search",
            "app-server",
            "proxy",
            "--sock",
            socket_path.to_string_lossy().as_ref(),
        ]
    );

    let status = child.wait().await.unwrap();
    assert!(status.success());
}

#[cfg(unix)]
#[tokio::test]
async fn responses_api_proxy_maps_flags_and_parses_server_info() {
    let dir = tempfile::tempdir().unwrap();
    let server_info = dir.path().join("server-info.json");
    let script_path = write_fake_codex(
        dir.path(),
        r#"#!/usr/bin/env bash
echo "$PWD"
printf "%s\n" "$@"
info_path=""
while [[ $# -gt 0 ]]; do
  if [[ $1 == "--server-info" ]]; then
info_path=$2
  fi
  shift
done
read -r key || exit 1
echo "key:${key}"
if [[ -n "$info_path" ]]; then
  printf '{"port":4567,"pid":1234}\n' > "$info_path"
fi
"#,
    );

    let workdir = dir.path().join("responses-workdir");
    std_fs::create_dir_all(&workdir).unwrap();

    let client = CodexClient::builder()
        .binary(&script_path)
        .mirror_stdout(false)
        .quiet(true)
        .working_dir(&workdir)
        .build();

    let mut proxy = client
        .start_responses_api_proxy(
            ResponsesApiProxyRequest::new("sk-test-123")
                .port(8080)
                .server_info(&server_info)
                .http_shutdown(true)
                .upstream_url("https://example.com/v1/responses"),
        )
        .await
        .unwrap();

    assert_eq!(
        proxy.server_info_path.as_deref(),
        Some(server_info.as_path())
    );

    let stdout = proxy.child.stdout.take().unwrap();
    let mut lines = BufReader::new(stdout).lines();

    let pwd = lines.next_line().await.unwrap().unwrap();
    let pwd = std_fs::canonicalize(Path::new(&pwd)).unwrap();
    let workdir = std_fs::canonicalize(&workdir).unwrap();
    assert_eq!(pwd, workdir);

    let mut args = Vec::new();
    for _ in 0..8 {
        args.push(lines.next_line().await.unwrap().unwrap());
    }
    assert_eq!(
        args,
        vec![
            "responses-api-proxy",
            "--port",
            "8080",
            "--server-info",
            server_info.to_string_lossy().as_ref(),
            "--http-shutdown",
            "--upstream-url",
            "https://example.com/v1/responses",
        ]
    );

    let api_key_line = lines.next_line().await.unwrap().unwrap();
    assert_eq!(api_key_line, "key:sk-test-123");

    let info = proxy.read_server_info().await.unwrap().unwrap();
    assert_eq!(info.port, 4567);
    assert_eq!(info.pid, 1234);

    let status = proxy.child.wait().await.unwrap();
    assert!(status.success());
}

#[tokio::test]
async fn responses_api_proxy_rejects_empty_api_key() {
    let client = CodexClient::builder().build();
    let err = client
        .start_responses_api_proxy(ResponsesApiProxyRequest::new("  "))
        .await
        .unwrap_err();
    assert!(matches!(err, CodexError::EmptyApiKey));
}

#[cfg(unix)]
#[tokio::test]
async fn exec_server_maps_overrides_and_request_workdir() {
    let dir = tempfile::tempdir().unwrap();
    let script_path = write_fake_codex(
        dir.path(),
        r#"#!/usr/bin/env bash
echo "$PWD"
printf "%s\n" "$@"
"#,
    );

    let request_workdir = dir.path().join("exec-server-workdir");
    std_fs::create_dir_all(&request_workdir).unwrap();

    let client = CodexClient::builder()
        .binary(&script_path)
        .mirror_stdout(false)
        .quiet(true)
        .approval_policy(ApprovalPolicy::OnRequest)
        .build();

    let mut child = client
        .start_exec_server(
            ExecServerRequest::new()
                .working_dir(&request_workdir)
                .profile("dev")
                .config_override("exec.mode", "server")
                .search(true),
        )
        .unwrap();

    let stdout = child.stdout.take().unwrap();
    let mut lines = BufReader::new(stdout).lines();

    let pwd = lines.next_line().await.unwrap().unwrap();
    let pwd = std_fs::canonicalize(Path::new(&pwd)).unwrap();
    let request_workdir = std_fs::canonicalize(&request_workdir).unwrap();
    assert_eq!(pwd, request_workdir);

    let mut args = Vec::new();
    while let Some(line) = lines.next_line().await.unwrap() {
        args.push(line);
    }
    assert_eq!(
        args,
        vec![
            "--config",
            "exec.mode=server",
            "--profile",
            "dev",
            "--ask-for-approval",
            "on-request",
            "--search",
            "exec-server",
        ]
    );

    let status = child.wait().await.unwrap();
    assert!(status.success());
}

#[cfg(unix)]
#[tokio::test]
async fn stdio_to_uds_maps_args_and_pipes_stdio() {
    let dir = tempfile::tempdir().unwrap();
    let socket_path = dir.path().join("bridge.sock");
    let script_path = write_fake_codex(
        dir.path(),
        r#"#!/usr/bin/env bash
echo "$PWD"
printf "%s\n" "$@"
while read -r line; do
  echo "relay:${line}"
done
"#,
    );

    let workdir = dir.path().join("uds-workdir");
    std_fs::create_dir_all(&workdir).unwrap();

    let client = CodexClient::builder()
        .binary(&script_path)
        .mirror_stdout(false)
        .quiet(true)
        .working_dir(&workdir)
        .build();

    let request = StdioToUdsRequest::new(&socket_path).working_dir(&workdir);
    let mut child = match client.stdio_to_uds(request.clone()) {
        Ok(child) => child,
        Err(CodexError::Spawn { source, .. }) if source.raw_os_error() == Some(26) => {
            time::sleep(Duration::from_millis(25)).await;
            client.stdio_to_uds(request).unwrap()
        }
        Err(other) => panic!("unexpected spawn error: {other:?}"),
    };

    let stdout = child.stdout.take().unwrap();
    let mut lines = BufReader::new(stdout).lines();

    let pwd = lines.next_line().await.unwrap().unwrap();
    let pwd = std_fs::canonicalize(Path::new(&pwd)).unwrap();
    let workdir = std_fs::canonicalize(&workdir).unwrap();
    assert_eq!(pwd, workdir);

    let arg_one = lines.next_line().await.unwrap().unwrap();
    let arg_two = lines.next_line().await.unwrap().unwrap();
    assert_eq!(arg_one, "stdio-to-uds");
    assert_eq!(arg_two, socket_path.to_string_lossy().as_ref());

    let mut stdin = child.stdin.take().unwrap();
    stdin.write_all(b"ping\n").await.unwrap();
    stdin.shutdown().await.unwrap();
    drop(stdin);

    let echoed = lines.next_line().await.unwrap().unwrap();
    assert_eq!(echoed, "relay:ping");

    let status = time::timeout(Duration::from_secs(5), child.wait())
        .await
        .expect("stdio-to-uds wait timed out")
        .unwrap();
    assert!(status.success());
}

#[tokio::test]
async fn stdio_to_uds_rejects_empty_socket_path() {
    let client = CodexClient::builder().build();
    let err = client
        .stdio_to_uds(StdioToUdsRequest::new(PathBuf::new()))
        .unwrap_err();
    assert!(matches!(err, CodexError::EmptySocketPath));
}