@@ -1,5 +1,12 @@
1-import { complete, Context, stream, Tool, Type } from "@mariozechner/pi-ai";
1+import {
2+ type Context,
3+ completeSimple,
4+ streamSimple,
5+ type Tool,
6+} from "@mariozechner/pi-ai";
7+import { TASK_PROMPT } from "./agent";
28 import { getApiKey } from "./oauth";
9+import { bash, runBashTool } from "./tool-bash";
310 import type { CliOptions } from "./types";
411
512 export async function streamHeadless(
@@ -7,25 +14,9 @@ export async function streamHeadless(
714 leave: (s: string) => void,
815 ) {
916 const apiKey = await getApiKey(options);
10-
11- const tools: Tool[] = [
12- {
13- name: "bash",
14- description:
15- "This is your command line, runs commands in your user's environment.",
16- parameters: Type.Object({
17- command: Type.String({
18- description:
19- "The command you want to run, ex: `ls`, `fd`, `find`, `grep`, `rg`, `cat`, `sed`, `bun`, etc",
20- }),
21- }),
22- },
23- ];
24-
17+ const tools: Tool[] = [bash];
2518 const context: Context = {
26- // TODO: Centralize prompts
27- systemPrompt:
28- "You are friendly coding assistant, answer user questions and complete given tasks with accuracy.",
19+ systemPrompt: TASK_PROMPT,
2920 messages: [
3021 { content: options.prompt ?? "", role: "user", timestamp: Date.now() },
3122 ],
@@ -36,7 +27,7 @@ export async function streamHeadless(
3627 console.log("-------------------");
3728
3829 while (true) {
39- const s = stream(options.model, context, {
30+ const s = streamSimple(options.model, context, {
4031 apiKey,
4132 reasoning: options.effort,
4233 });
@@ -44,17 +35,17 @@ export async function streamHeadless(
4435 for await (const ev of s) {
4536 switch (ev.type) {
4637 case "text_start":
47- console.log("Answering...");
38+ console.log("> Answering...");
4839 break;
4940 case "thinking_start":
50- console.log("Thinking...");
41+ console.log("> Thinking...");
5142 break;
5243 case "toolcall_start":
53- console.log(`Calling tool...`);
44+ console.log(`> Calling tool...`);
5445 break;
5546 case "toolcall_end":
5647 console.log(
57- `${ev.toolCall.name}:\n\t${ev.toolCall.arguments.command}`,
48+ `> ${ev.toolCall.name}: ${ev.toolCall.arguments.command}`,
5849 );
5950 break;
6051 case "error":
@@ -73,9 +64,7 @@ export async function streamHeadless(
7364
7465 const toolCalls = finalMessage.content.filter((b) => b.type === "toolCall");
7566 for (const call of toolCalls) {
76- const proc = Bun.spawn(["bash", "-c", call.arguments.command]);
77- await proc.exited;
78- const result = await proc.stdout.text();
67+ const result = await runBashTool(call.arguments.command);
7968
8069 context.messages.push({
8170 role: "toolResult",
@@ -88,7 +77,7 @@ export async function streamHeadless(
8877 }
8978
9079 if (toolCalls.length > 0) {
91- const cont = await complete(options.model, context);
80+ const cont = await completeSimple(options.model, context);
9281 context.messages.push(cont);
9382 }
9483