(model, prompt, options)
| 69 | |
| 70 | const codexAgent: Agent.Definition<(typeof models)[number]> = { |
| 71 | async run(model, prompt, options) { |
| 72 | options.logger.log( |
| 73 | `codex-sdk --model ${model} --sandbox ${DEFAULT_SANDBOX} ${prompt}`, |
| 74 | ); |
| 75 | |
| 76 | const key = sessionKey(model, options.cwd); |
| 77 | const thread = getOrCreateThread(model, options.cwd); |
| 78 | |
| 79 | const actions: string[] = []; |
| 80 | let usage: Usage; |
| 81 | let cost = 0; |
| 82 | try { |
| 83 | const pricingKey = model; |
| 84 | const pricing = openai.models[pricingKey]?.cost; |
| 85 | const turn = await thread.run(prompt); |
| 86 | assert(turn.usage, "The agent did not emit the usage information."); |
| 87 | usage = turn.usage; |
| 88 | const billableInput = |
| 89 | (usage.input_tokens ?? 0) - (usage.cached_input_tokens ?? 0); |
| 90 | const cachedInput = usage.cached_input_tokens ?? 0; |
| 91 | const output = usage.output_tokens ?? 0; |
| 92 | cost = |
| 93 | (billableInput * pricing.input + |
| 94 | output * pricing.output + |
| 95 | cachedInput * pricing.cache_read) / |
| 96 | 1_000_000; |
| 97 | |
| 98 | actions.push(...turn.items.map((item) => JSON.stringify(item))); |
| 99 | logTurnItems(turn.items, options); |
| 100 | } catch (error) { |
| 101 | threadCache.delete(key); |
| 102 | throw error; |
| 103 | } |
| 104 | |
| 105 | return { |
| 106 | actions, |
| 107 | usage: { |
| 108 | input: usage.input_tokens, |
| 109 | output: usage.output_tokens, |
| 110 | cost, |
| 111 | }, |
| 112 | }; |
| 113 | }, |
| 114 | }; |
| 115 | |
| 116 | export default codexAgent; |
nothing calls this directly
no test coverage detected