@@ -1,68 +1,12 @@
1-import type {
2- AgentEvent,
3- AgentPlugin,
4- AgentProcess,
5- AgentResult,
6- AgentSessionOpts,
7-} from "@orc-code/types";
1+import type { AgentEvent, AgentPlugin, AgentProcess, AgentSessionOpts } from "@orc-code/types";
2+import { checkCommand, spawnAgentProcess } from "@orc-code/types";
83 import { parseLine } from "./parser.js";
94
10-function spawnAgent(args: string[], cwd?: string): AgentProcess {
11- const child = Bun.spawn(["claude", ...args], {
12- cwd: cwd ?? process.cwd(),
13- stdout: "pipe",
14- stderr: "pipe",
15- });
16-
17- let resolvedSessionId: string | null = null;
18-
19- async function* streamEvents(): AsyncIterable<AgentEvent> {
20- const reader = child.stdout.getReader();
21- const decoder = new TextDecoder();
22- let buffer = "";
23-
24- while (true) {
25- const { done, value } = await reader.read();
26- if (done) break;
27- buffer += decoder.decode(value, { stream: true });
28-
29- const lines = buffer.split("\n");
30- // keep the last incomplete line in the buffer
31- buffer = lines.pop() ?? "";
32-
33- for (const line of lines) {
34- const events = parseLine(line);
35- for (const event of events) {
36- if (event.type === "done") {
37- resolvedSessionId = event.sessionId;
38- }
39- yield event;
40- }
41- }
42- }
43-
44- // flush remaining buffer
45- if (buffer.trim()) {
46- const events = parseLine(buffer);
47- for (const event of events) {
48- if (event.type === "done") {
49- resolvedSessionId = event.sessionId;
50- }
51- yield event;
52- }
53- }
5+function extractSessionId(_raw: string, events: AgentEvent[]): string | null {
6+ for (const event of events) {
7+ if (event.type === "done") return event.sessionId;
548 }
55-
56- const result: Promise<AgentResult> = child.exited.then((exitCode) => ({
57- sessionId: resolvedSessionId,
58- exitCode,
59- }));
60-
61- return {
62- child,
63- events: streamEvents(),
64- result,
65- };
9+ return null;
6610 }
6711
6812 const plugin: AgentPlugin = {
@@ -70,25 +14,26 @@ const plugin: AgentPlugin = {
7014 command: "claude",
7115 opts: ["-p", "--output-format", "stream-json", "--verbose", "--dangerously-skip-permissions"],
7216
73- async isAvailable(): Promise<boolean> {
74- try {
75- const proc = Bun.spawn(["which", "claude"], {
76- stdout: "ignore",
77- stderr: "ignore",
78- });
79- const code = await proc.exited;
80- return code === 0;
81- } catch {
82- return false;
83- }
84- },
17+ isAvailable: () => checkCommand("claude"),
8518
8619 createSession(prompt: string, opts?: AgentSessionOpts): AgentProcess {
87- return spawnAgent([...this.opts, prompt], opts?.cwd);
20+ return spawnAgentProcess(
21+ "claude",
22+ [...this.opts, prompt],
23+ parseLine,
24+ extractSessionId,
25+ opts?.cwd,
26+ );
8827 },
8928
9029 resumeSession(sessionId: string, prompt: string, opts?: AgentSessionOpts): AgentProcess {
91- return spawnAgent([...this.opts, "--resume", sessionId, prompt], opts?.cwd);
30+ return spawnAgentProcess(
31+ "claude",
32+ [...this.opts, "--resume", sessionId, prompt],
33+ parseLine,
34+ extractSessionId,
35+ opts?.cwd,
36+ );
9237 },
9338 };
9439