@@ -1,9 +1,28 @@
1-import { Context, stream } from "@mariozechner/pi-ai";
1+import { complete, Context, stream, Tool, Type } from "@mariozechner/pi-ai";
22 import { getApiKey } from "./oauth";
33 import type { CliOptions } from "./types";
4+import { $ } from "bun";
45
5-export async function streamHeadless(options: CliOptions) {
6+export async function streamHeadless(
7+ options: CliOptions,
8+ leave: (s: string) => void,
9+) {
610 const apiKey = await getApiKey(options);
11+
12+ const tools: Tool[] = [
13+ {
14+ name: "shell",
15+ description:
16+ "This is your command line, and uses the installed shell in your user's environment.",
17+ parameters: Type.Object({
18+ command: Type.String({
19+ description:
20+ "The command you want to run, ex: `ls`, `fd`, `find`, `grep`, `rg`, `cat`, `sed`, `bun`, etc",
21+ }),
22+ }),
23+ },
24+ ];
25+
726 const context: Context = {
827 // TODO: Centralize prompts
928 systemPrompt:
@@ -11,31 +30,69 @@ export async function streamHeadless(options: CliOptions) {
1130 messages: [
1231 { content: options.prompt ?? "", role: "user", timestamp: Date.now() },
1332 ],
14- tools: [],
33+ tools,
1534 };
1635
17- const s = stream(options.model, context, { apiKey });
18- for await (const ev of s) {
19- switch (ev.type) {
20- case "start":
21- console.log("Starting headless mode");
22- break;
23- case "text_start":
24- console.log("Answering...")
25- break;
26- case "thinking_start":
27- console.log("Thinking... or trying to...")
28- break;
29- case "done":
30- console.log("Done.")
31- break;
32- case "error":
33- console.log(`Error ${ev.error}`)
34- break;
36+ while (true) {
37+ const s = stream(options.model, context, {
38+ apiKey,
39+ reasoning: options.effort,
40+ });
41+ for await (const ev of s) {
42+ switch (ev.type) {
43+ case "text_start":
44+ console.log("mini-coder headless");
45+ console.log("-------------------");
46+ console.log("Answering...");
47+ break;
48+ case "thinking_start":
49+ console.log("Thinking...");
50+ break;
51+ case "toolcall_start":
52+ console.log(`Calling tool...`);
53+ break;
54+ case "toolcall_end":
55+ console.log(`${ev.toolCall.name}:\n${ev.toolCall.arguments}`);
56+ break;
57+ case "error":
58+ console.log(`Error ${ev.error}`);
59+ break;
60+ }
61+ }
62+
63+ const finalMessage = await s.result();
64+ context.messages.push(finalMessage);
65+ console.log(
66+ finalMessage.content.map((m) => m.type === "text" && m.text).join("\n"),
67+ );
68+
69+ const toolCalls = finalMessage.content.filter((b) => b.type === "toolCall");
70+ for (const call of toolCalls) {
71+ const result = (await $`${call.arguments}`).text();
72+ context.messages.push({
73+ role: "toolResult",
74+ toolCallId: call.id,
75+ toolName: call.name,
76+ content: [{ type: "text", text: result }],
77+ isError: false,
78+ timestamp: Date.now(),
79+ });
3580 }
36- }
3781
38- const finalMessage = await s.result();
39- context.messages.push(finalMessage)
40- console.log(finalMessage.content)
82+ if (toolCalls.length > 0) {
83+ const cont = await complete(options.model, context);
84+ context.messages.push(cont);
85+ }
86+
87+ console.log(
88+ `Total tokens: ${finalMessage.usage.input} in, ${finalMessage.usage.output} out`,
89+ );
90+ console.log(`Cost: $${finalMessage.usage.cost.total.toFixed(4)}`);
91+
92+ if (["stop", "error", "aborted"].includes(finalMessage.stopReason)) {
93+ console.log(`Reason for stopping: "${finalMessage.stopReason}"`)
94+ const dur = Date.now() - context.messages[0].timestamp
95+ leave(`Done. Took ${dur / 1000}s`);
96+ }
97+ }
4198 }