@@ -1,11 +1,10 @@
11 import { basename } from "node:path";
2-import { cel, HStack, ProcessTerminal, Text, VStack } from "@cel-tui/core";
2+import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
33 import type { Message } from "@mariozechner/pi-ai";
4-import { streamAgent, TASK_PROMPT } from "./agent";
4+import { MAIN_PROMPT, streamAgent } from "./agent";
55 import { getApiKey } from "./oauth";
66 import { estimateTokens, secureRandomString } from "./shared";
7-import { bash, runBashTool } from "./tool-bash";
8-import { edit, runEditTool } from "./tool-edit";
7+import { runTaskTool, task } from "./tool-task";
98 import { ActivityPill, Spinner, TextPill, theme } from "./tui-components";
109 import { Conversation } from "./tui-conversation";
1110 import { Editor } from "./tui-editor";
@@ -15,28 +14,28 @@ import type { ToolAndRunner, TUIMessage, TUIState } from "./types";
1514 function clearOrAbort(state: TUIState) {
1615 // Are we mid stream? Abort it.
1716 if (state.streaming) {
18- state.abortController.abort()
17+ state.abortController?.abort();
1918 state.abortController = new AbortController();
2019 }
2120
2221 // Is the user clearing a state prompt?
2322 if (state.prompt?.length) {
24- state.prompt = ""
23+ state.prompt = "";
2524 }
2625 }
2726
2827 function ContextPill(state: TUIState) {
29- let text = ""
28+ let text = "";
3029 if (!state.context) {
31- text = "0%"
30+ text = "0%";
3231 } else {
33- const current = estimateTokens(JSON.stringify(state.context))
34- const max = state.options.model.maxTokens
35- const percent = Math.round((current/max)*100)
36- text = `~${percent}%`
32+ const current = estimateTokens(JSON.stringify(state.context));
33+ const max = state.options.model.contextWindow;
34+ const percent = Math.round((current / max) * 100);
35+ text = `~${percent}%`;
3736 }
3837
39- return TextPill(text, theme.bblack, theme.bwhite)
38+ return TextPill(text, theme.bblack, theme.bwhite);
4039 }
4140
4241 export function initTUI(state: TUIState, leave: (s: string) => void) {
@@ -68,9 +67,9 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
6867 clearInterval(baseFramerateIntervalId);
6968 cel.stop();
7069 leave("Done.");
71- } else if (key === 'escape') {
70+ } else if (key === "escape") {
7271 // Abort or clear prompt
73- clearOrAbort(state)
72+ clearOrAbort(state);
7473 }
7574 },
7675 },
@@ -95,10 +94,11 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
9594 (key: string) => {
9695 // onKeyPress
9796 if (key === "enter") {
98- if (state.prompt === ':q') {
99- clearInterval(baseFramerateIntervalId)
100- cel.stop()
101- leave("Done. I like vim too.")
97+ if (state.prompt === ":q") {
98+ clearInterval(baseFramerateIntervalId);
99+ cel.stop();
100+ leave("Done. I like vim too.");
101+ return false;
102102 }
103103 const submit = async () => {
104104 await streamTUI(state);
@@ -117,11 +117,18 @@ export async function streamTUI(state: TUIState) {
117117 if (state.streaming) return;
118118 state.streaming = true;
119119
120+ const abortController = new AbortController();
121+ state.abortController = abortController;
122+
120123 const lastTs = Date.now();
121124 const apiKey = await getApiKey(state.options);
122125 const tools: ToolAndRunner[] = [
123- { tool: bash, runner: runBashTool },
124- { tool: edit, runner: runEditTool },
126+ {
127+ tool: task,
128+ runner: async (args: Record<string, any>) => {
129+ return runTaskTool({...state.options, abortController}, args);
130+ },
131+ },
125132 ];
126133 const userPrompt = {
127134 role: "user",
@@ -140,11 +147,13 @@ export async function streamTUI(state: TUIState) {
140147 await streamAgent(
141148 apiKey,
142149 tools,
143- TASK_PROMPT,
150+ MAIN_PROMPT,
144151 messages,
145152 state.options,
146153 state.abortController,
147- (ev) => {
154+ (ev, context) => {
155+ state.context = context;
156+
148157 switch (ev.type) {
149158 case "text_start":
150159 state.activeState = "answering";
@@ -182,7 +191,7 @@ export async function streamTUI(state: TUIState) {
182191 }
183192
184193 case "toolcall_end": {
185- state.activeState = "idle";
194+ state.activeState = "waiting";
186195
187196 // Collect tool calls state
188197 const finishedToolCalls = ev.partial.content.filter(
@@ -209,7 +218,7 @@ export async function streamTUI(state: TUIState) {
209218 }
210219
211220 case "done":
212- state.activeState = "idle";
221+ state.activeState = "waiting";
213222 cel.render();
214223 break;
215224
@@ -219,7 +228,7 @@ export async function streamTUI(state: TUIState) {
219228 }
220229 },
221230
222- (tool) => {
231+ (tool, context) => {
223232 // Append the output to the existing TUI messages
224233 state.messages = state.messages.map((msg) => {
225234 if (tool.toolCallId === msg.id) {
@@ -230,6 +239,7 @@ export async function streamTUI(state: TUIState) {
230239 }
231240 return msg;
232241 });
242+ state.context = context;
233243 cel.render();
234244 },
235245
@@ -259,6 +269,7 @@ export async function streamTUI(state: TUIState) {
259269 }
260270
261271 state.streaming = false;
272+ state.activeState = "idle";
262273 state.context = context;
263274 cel.render();
264275 },