@@ -4,14 +4,12 @@ import process from "node:process";
44import {
55 Codex ,
66 Usage ,
7- type CommandExecutionItem ,
87 type SandboxMode ,
98 type Thread ,
109 type ThreadItem ,
1110} from "@openai/codex-sdk" ;
1211
1312import type { Agent } from "~/agents/index.js" ;
14- import { Logger } from "~/lib/logger.js" ;
1513
1614const DEFAULT_SANDBOX : SandboxMode = "workspace-write" ;
1715
@@ -30,46 +28,23 @@ function sessionKey(cwd: string, model: string): string {
3028 return `${ cwd } ::${ model } ` ;
3129}
3230
33- function formatCommand ( command : string , args : string [ ] ) : string {
34- if ( args . length === 0 ) {
35- return command ;
36- }
37-
38- const renderedArgs = args . map ( ( arg ) =>
39- / [ \s " ' ] / . test ( arg ) ? JSON . stringify ( arg ) : arg ,
40- ) ;
41-
42- return `${ command } ${ renderedArgs . join ( " " ) } ` ;
43- }
44-
45- function writeLog (
46- output : NodeJS . WriteStream ,
47- message : string ,
48- logger ?: Logger . Instance ,
49- ) : void {
50- output . write ( `${ logger ?. format ( message ) ?? message } \n` ) ;
51- }
52-
53- function isCommandExecutionItem (
54- item : ThreadItem ,
55- ) : item is CommandExecutionItem {
56- return item . type === "command_execution" ;
57- }
58-
5931function logTurnItems ( items : ThreadItem [ ] , options : Agent . RunOptions ) : void {
6032 for ( const item of items ) {
6133 try {
62- writeLog ( process . stdout , JSON . stringify ( item ) , options . logger ) ;
34+ process . stdout . write ( ` ${ options . logger . format ( JSON . stringify ( item ) ) } \n` ) ;
6335 } catch ( error ) {
64- const sanitizedItem = isCommandExecutionItem ( item )
65- ? { ...item , aggregated_output : "<omitted>" }
66- : item ;
67- writeLog ( process . stdout , JSON . stringify ( sanitizedItem ) , options . logger ) ;
36+ const sanitizedItem =
37+ item . type === "command_execution"
38+ ? { ...item , aggregated_output : "<omitted>" }
39+ : item ;
40+ process . stdout . write (
41+ `${ options . logger . format ( JSON . stringify ( sanitizedItem ) ) } \n` ,
42+ ) ;
6843 if ( error instanceof Error ) {
69- writeLog (
70- process . stderr ,
71- `Failed to serialize Codex item: ${ error . message } ` ,
72- options . logger ,
44+ process . stderr . write (
45+ ` ${ options . logger . format (
46+ `Failed to serialize Codex item: ${ error . message } ` ,
47+ ) } \n` ,
7348 ) ;
7449 }
7550 }
@@ -94,17 +69,9 @@ function getOrCreateThread(model: string, cwd: string): Thread {
9469
9570const codexAgent : Agent . Definition < ( typeof models ) [ number ] > = {
9671 async run ( model , prompt , cwd , options ) {
97- assert ( typeof prompt === "string" , "Codex agent requires a prompt string." ) ;
98-
99- const displayCommand = formatCommand ( "codex-sdk" , [
100- "--model" ,
101- model ,
102- "--sandbox" ,
103- DEFAULT_SANDBOX ,
104- prompt ,
105- ] ) ;
106-
107- options . logger . log ( displayCommand ) ;
72+ options . logger . log (
73+ `codex-sdk --model ${ model } --sandbox ${ DEFAULT_SANDBOX } ${ prompt } ` ,
74+ ) ;
10875
10976 const key = sessionKey ( model , cwd ) ;
11077 const thread = getOrCreateThread ( model , cwd ) ;
@@ -136,7 +103,6 @@ const codexAgent: Agent.Definition<(typeof models)[number]> = {
136103 }
137104
138105 return {
139- command : displayCommand ,
140106 actions,
141107 usage : {
142108 input : usage . input_tokens ,
0 commit comments