@@ -1,38 +1,29 @@
1-import {
2- type Context,
3- completeSimple,
4- streamSimple,
5- type Tool,
6-} from "@mariozechner/pi-ai";
7-import { TASK_PROMPT } from "./agent";
1+import type { Message } from "@mariozechner/pi-ai";
2+import { streamAgent, TASK_PROMPT } from "./agent";
83 import { getApiKey } from "./oauth";
94 import { bash, runBashTool } from "./tool-bash";
10-import type { CliOptions } from "./types";
5+import type { CliOptions, ToolAndRunner } from "./types";
116
127 export async function streamHeadless(
138 options: CliOptions,
149 leave: (s: string) => void,
1510 ) {
1611 const apiKey = await getApiKey(options);
17- const tools: Tool[] = [bash];
18- const context: Context = {
19- systemPrompt: TASK_PROMPT,
20- messages: [
21- { content: options.prompt ?? "", role: "user", timestamp: Date.now() },
22- ],
23- tools,
24- };
12+ const tools: ToolAndRunner[] = [{ tool: bash, runner: runBashTool }];
13+ const messages: Message[] = [
14+ { role: "user", content: options.prompt || "", timestamp: Date.now() },
15+ ];
2516
2617 console.log("mini-coder headless");
2718 console.log("-------------------");
2819
29- while (true) {
30- const s = streamSimple(options.model, context, {
31- apiKey,
32- reasoning: options.effort,
33- });
34-
35- for await (const ev of s) {
20+ await streamAgent(
21+ apiKey,
22+ tools,
23+ TASK_PROMPT,
24+ messages,
25+ options,
26+ (ev) => {
3627 switch (ev.type) {
3728 case "text_start":
3829 console.log("> Answering...");
@@ -49,51 +40,28 @@ export async function streamHeadless(
4940 );
5041 break;
5142 case "error":
52- console.log(`Error ${ev.error}`);
43+ console.log(`Error ${ev.reason}\n${ev.error.content}`);
5344 break;
5445 }
55- }
56-
57- const finalMessage = await s.result();
58- context.messages.push(finalMessage);
59- const finalContent = finalMessage.content
60- .map((m) => m.type === "text" && m.text)
61- .filter((m) => Boolean(m));
62-
63- if (finalContent.length > 0) console.log(finalContent.join("\n"));
46+ },
47+ undefined,
48+ (msg, dur) => {
49+ const text = msg.content
50+ .filter((c) => c.type === "text")
51+ .map((c) => c.text)
52+ .join("\n");
53+ console.log(`\n${text}`);
54+ console.log(
55+ `\nTotal tokens: ${msg.usage.input} in, ${msg.usage.output} out`,
56+ );
57+ console.log(`Cost: $${msg.usage.cost.total.toFixed(4)}`);
6458
65- const toolCalls = finalMessage.content.filter((b) => b.type === "toolCall");
66- for (const call of toolCalls) {
67- const result = await runBashTool(call.arguments.command);
68-
69- context.messages.push({
70- role: "toolResult",
71- toolCallId: call.id,
72- toolName: call.name,
73- content: [{ type: "text", text: result }],
74- isError: false,
75- timestamp: Date.now(),
76- });
77- }
78-
79- if (toolCalls.length > 0) {
80- // TODO: Investigate why we get an error: `No output for tool call id XXXX...` when
81- // we add { apiKey } in this call. And how does it work without it?
82- const cont = await completeSimple(options.model, context, {
83- reasoning: options.effort,
84- });
85- context.messages.push(cont);
86- }
87-
88- console.log(
89- `Total tokens: ${finalMessage.usage.input} in, ${finalMessage.usage.output} out`,
90- );
91- console.log(`Cost: $${finalMessage.usage.cost.total.toFixed(4)}`);
59+ if (["stop", "error", "aborted"].includes(msg.stopReason)) {
60+ console.log(`Reason for stopping: "${msg.stopReason}"`);
61+ leave(`Done. Took ${dur / 1000}s`);
62+ }
63+ },
64+ );
9265
93- if (["stop", "error", "aborted"].includes(finalMessage.stopReason)) {
94- console.log(`Reason for stopping: "${finalMessage.stopReason}"`);
95- const dur = Date.now() - context.messages[0].timestamp;
96- leave(`Done. Took ${dur / 1000}s`);
97- }
98- }
66+ leave("Done.");
9967 }