Skip to content

Commit f375c70

Browse files
author
Frank
committed
refactor
1 parent 6dae7c8 commit f375c70

File tree

10 files changed

+212
-518
lines changed

10 files changed

+212
-518
lines changed

agents/claude-code.ts

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,32 @@ function sessionKey(cwd: string, model: string): string {
2020
return `${cwd}::${model}`;
2121
}
2222

23-
function formatCommand(command: string, args: string[]): string {
24-
if (args.length === 0) {
25-
return command;
26-
}
27-
28-
const rendered = args.map((arg) =>
29-
/[\s"']/.test(arg) ? JSON.stringify(arg) : arg,
30-
);
31-
32-
return `${command} ${rendered.join(" ")}`;
33-
}
34-
35-
function writeLog(
36-
output: NodeJS.WriteStream,
37-
message: string,
38-
logger?: Logger.Instance,
39-
): void {
40-
output.write(`${logger?.format(message) ?? message}\n`);
41-
}
42-
4323
function logJson(value: unknown, options: Agent.RunOptions): void {
24+
let message: string;
4425
try {
45-
writeLog(process.stdout, JSON.stringify(value), options.logger);
26+
message = JSON.stringify(value);
4627
} catch (error) {
47-
const reason = error instanceof Error ? error.message : String(error);
48-
writeLog(
49-
process.stdout,
50-
JSON.stringify({ error: "serialization_failed", reason }),
51-
options.logger,
52-
);
28+
message = JSON.stringify({
29+
error: "serialization_failed",
30+
reason: error instanceof Error ? error.message : String(error),
31+
});
5332
}
33+
34+
process.stdout.write(`${options.logger.format(message)}\n`);
5435
}
5536

5637
function logError(value: unknown, options: Agent.RunOptions): void {
38+
let message: string;
5739
try {
58-
writeLog(process.stderr, JSON.stringify(value), options.logger);
40+
message = JSON.stringify(value);
5941
} catch (error) {
60-
const reason = error instanceof Error ? error.message : String(error);
61-
writeLog(
62-
process.stderr,
63-
JSON.stringify({ error: "serialization_failed", reason }),
64-
options.logger,
65-
);
42+
message = JSON.stringify({
43+
error: "serialization_failed",
44+
reason: error instanceof Error ? error.message : String(error),
45+
});
6646
}
47+
48+
process.stderr.write(`${options.logger.format(message)}\n`);
6749
}
6850

6951
function serializeError(error: unknown): Record<string, unknown> {
@@ -79,18 +61,7 @@ function serializeError(error: unknown): Record<string, unknown> {
7961

8062
const claudeCodeAgent: Agent.Definition = {
8163
async run(model, prompt, cwd, options) {
82-
assert(
83-
typeof prompt === "string",
84-
"Claude Code agent requires a prompt string.",
85-
);
86-
87-
const displayCommand = formatCommand("claude-agent-sdk", [
88-
"--model",
89-
model,
90-
prompt,
91-
]);
92-
93-
options.logger.log(displayCommand);
64+
options.logger.log(`claude-agent-sdk --model ${model} ${prompt}`);
9465

9566
const cacheKey = sessionKey(cwd, model);
9667
const existingSessionID = sessionCache.get(cacheKey);
@@ -142,7 +113,7 @@ const claudeCodeAgent: Agent.Definition = {
142113
throw error;
143114
}
144115

145-
return { command: displayCommand, actions, usage };
116+
return { actions, usage };
146117
},
147118
};
148119

agents/codex.ts

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import process from "node:process";
44
import {
55
Codex,
66
Usage,
7-
type CommandExecutionItem,
87
type SandboxMode,
98
type Thread,
109
type ThreadItem,
1110
} from "@openai/codex-sdk";
1211

1312
import type { Agent } from "~/agents/index.js";
14-
import { Logger } from "~/lib/logger.js";
1513

1614
const 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-
5931
function 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

9570
const 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,

agents/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export namespace Agent {
3131
}
3232

3333
export interface RunResult {
34-
command: string;
3534
actions: string[];
3635
usage: {
3736
input: number;

0 commit comments

Comments
 (0)