Skip to content

Commit 313a523

Browse files
committed
wip
1 parent 0781b46 commit 313a523

6 files changed

Lines changed: 105 additions & 478 deletions

File tree

agents/claude-code.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ const claudeCodeAgent: AgentDefinition = {
106106
const cacheKey = sessionKey(cwd, model);
107107
const existingSessionID = sessionCache.get(cacheKey);
108108

109+
const usage = {
110+
input: 0,
111+
output: 0,
112+
};
113+
109114
try {
110115
const result = query({
111116
prompt,
@@ -122,6 +127,13 @@ const claudeCodeAgent: AgentDefinition = {
122127
for await (const message of result) {
123128
// Extract and cache session ID from messages
124129
sessionCache.set(cacheKey, message.session_id);
130+
131+
// Accumulate token usage if available
132+
if (message.usage) {
133+
usage.input += message.usage.input_tokens || 0;
134+
usage.output += message.usage.output_tokens || 0;
135+
}
136+
125137
logJson(message, options);
126138
}
127139
} catch (error) {
@@ -137,7 +149,7 @@ const claudeCodeAgent: AgentDefinition = {
137149
throw error;
138150
}
139151

140-
return { command: displayCommand };
152+
return { command: displayCommand, usage };
141153
},
142154
};
143155

agents/codex.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import process from "node:process";
33

44
import {
55
Codex,
6+
Usage,
67
type CommandExecutionItem,
78
type SandboxMode,
89
type Thread,
@@ -122,15 +123,27 @@ const codexAgent: AgentDefinition = {
122123
const key = sessionKey(model, cwd);
123124
const thread = getOrCreateThread(model, cwd);
124125

126+
const actions: string[] = [];
127+
let usage: Usage;
125128
try {
126129
const turn = await thread.run(prompt);
130+
assert(turn.usage, "The agent did not emit the usage information.");
131+
usage = turn.usage;
132+
actions.push(...turn.items.map((item) => JSON.stringify(item)));
127133
logTurnItems(turn.items, options);
128134
} catch (error) {
129135
threadCache.delete(key);
130136
throw error;
131137
}
132138

133-
return { command: displayCommand };
139+
return {
140+
command: displayCommand,
141+
actions,
142+
usage: {
143+
input: usage.input_tokens,
144+
output: usage.output_tokens,
145+
},
146+
};
134147
},
135148
};
136149

agents/opencode.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,51 +71,31 @@ function writeLog(
7171
}
7272
}
7373

74-
function logJson(
75-
value: unknown,
76-
options: AgentRunOptions | undefined,
77-
logs?: string[],
78-
): void {
74+
function logJson(value: unknown, options: AgentRunOptions | undefined): void {
7975
try {
8076
const message = JSON.stringify(value);
8177
writeLog(process.stdout, message, options?.logPrefix);
82-
if (options?.captureLogs && logs) {
83-
logs.push(message);
84-
}
8578
} catch (error) {
8679
const reason = error instanceof Error ? error.message : String(error);
8780
const errorMessage = JSON.stringify({
8881
error: "serialization_failed",
8982
reason,
9083
});
9184
writeLog(process.stdout, errorMessage, options?.logPrefix);
92-
if (options?.captureLogs && logs) {
93-
logs.push(errorMessage);
94-
}
9585
}
9686
}
9787

98-
function logError(
99-
value: unknown,
100-
options: AgentRunOptions | undefined,
101-
logs?: string[],
102-
): void {
88+
function logError(value: unknown, options: AgentRunOptions | undefined): void {
10389
try {
10490
const message = JSON.stringify(value);
10591
writeLog(process.stderr, message, options?.logPrefix);
106-
if (options?.captureLogs && logs) {
107-
logs.push(`ERROR: ${message}`);
108-
}
10992
} catch (error) {
11093
const reason = error instanceof Error ? error.message : String(error);
11194
const errorMessage = JSON.stringify({
11295
error: "serialization_failed",
11396
reason,
11497
});
11598
writeLog(process.stderr, errorMessage, options?.logPrefix);
116-
if (options?.captureLogs && logs) {
117-
logs.push(`ERROR: ${errorMessage}`);
118-
}
11999
}
120100
}
121101

@@ -179,7 +159,6 @@ const opencodeAgent: AgentDefinition = {
179159
options?.onStart?.(displayCommand);
180160

181161
const cacheKey = sessionKey(cwd, model);
182-
const logs: string[] = options?.captureLogs ? [] : undefined as any;
183162

184163
let sessionID = sessionCache.get(cacheKey);
185164
if (!sessionID) {
@@ -191,6 +170,10 @@ const opencodeAgent: AgentDefinition = {
191170
sessionCache.set(cacheKey, sessionID);
192171
}
193172

173+
const usage = {
174+
input: 0,
175+
output: 0,
176+
};
194177
try {
195178
const [providerID, modelID] = model.split("/");
196179
const { data } = await opencode.client.session.prompt({
@@ -206,7 +189,10 @@ const opencodeAgent: AgentDefinition = {
206189
throwOnError: true,
207190
});
208191

209-
logPromptResult(data, options, logs);
192+
usage.input = data.info.tokens.input;
193+
usage.output = data.info.tokens.output;
194+
195+
logPromptResult(data, options);
210196
} catch (error) {
211197
sessionCache.delete(cacheKey);
212198
logError(
@@ -215,12 +201,11 @@ const opencodeAgent: AgentDefinition = {
215201
details: serializeError(error),
216202
},
217203
options,
218-
logs,
219204
);
220205
throw error;
221206
}
222207

223-
return { command: displayCommand, sessionID, logs };
208+
return { command: displayCommand, usage };
224209
},
225210
};
226211

0 commit comments

Comments
 (0)