xonecas.io
64875
Mc

mini-coder / files

mini-coder / src / agent.ts

7.0 KB
1import {
2  type Api,
3  type AssistantMessage,
4  type JsonObject,
5  type Message,
6  type Model,
7  type ModelThinkingLevel,
8  type Models,
9  type Tool,
10  type ToolCall,
11  type ToolResultMessage,
12  type UserMessage,
13  validateToolCall,
14} from "@earendil-works/pi-ai";
15import type { Session } from "./session.ts";
16import type { ToolName } from "./config.ts";
17import { acceptsImages, executeTool, type FileDiff, type ToolResult } from "./tools/index.ts";
18
19export type Phase = "preparing" | "waitingModel" | "streaming" | "runningTool" | "pausing" | "idle";
20
21export type AgentEvent =
22  | { type: "phase"; phase: Phase; detail?: string }
23  | { type: "text"; delta: string }
24  | { type: "reasoning"; delta: string }
25  | { type: "toolCallStart"; name: string }
26  | { type: "toolCall"; name: string; arguments: JsonObject }
27  | { type: "toolOutput"; chunk: string }
28  | { type: "toolResult"; name: string; text: string; isError: boolean; diffs?: FileDiff[] }
29  | { type: "message"; message: AssistantMessage }
30  | { type: "noModel" }
31  | { type: "error"; message: string }
32  | { type: "cancelled" }
33  | { type: "complete" };
34
35interface Interaction {
36  isPauseRequested(): boolean;
37  clearPause(): void;
38  requestSteering(): Promise<string>;
39}
40
41export const NO_INTERACTION: Interaction = {
42  isPauseRequested: () => false,
43  clearPause: () => {},
44  requestSteering: async () => "",
45};
46
47/** What one turn needs, shared by both projections: the TUI and `--print`. */
48export interface AgentOptions {
49  models: Models;
50  /** The selected model, or null until one is configured or chosen in the TUI. */
51  model: Model<Api> | null;
52  systemPrompt: string;
53  tools: Tool[];
54  /** The configured tool names, kept so a model switch can rebuild `tools`. */
55  toolNames: ToolName[];
56  thinkingEffort: ModelThinkingLevel;
57  session: Session;
58}
59
60interface AgentRun extends AgentOptions {
61  messages: Message[];
62  signal: AbortSignal;
63  interaction: Interaction;
64  onEvent: (event: AgentEvent) => void;
65}
66
67export async function runAgentTurn(run: AgentRun): Promise<void> {
68  const { messages, session, signal, interaction, onEvent } = run;
69
70  const model = run.model;
71  if (model === null) {
72    onEvent({ type: "noModel" });
73    return;
74  }
75
76  const cancelled = (): void => onEvent({ type: "cancelled" });
77  const failed = (message: string): void => onEvent({ type: "error", message });
78
79  /** Appends a user message the model reads at the next step boundary. */
80  const steer = (content: string): void => {
81    if (content === "") return;
82    const message: UserMessage = { role: "user", content, timestamp: Date.now() };
83    messages.push(message);
84    session.appendMessage(message);
85  };
86
87  /**
88   * Returns the steering typed while paused, or ""; the caller decides where
89   * it lands, because a user message between an assistant turn and its tool
90   * results is rejected by the provider. Null once the turn has been aborted;
91   * every step boundary is a cancellation point.
92   */
93  const pauseStep = async (): Promise<string | null> => {
94    if (signal.aborted) return null;
95    if (interaction.isPauseRequested()) {
96      interaction.clearPause();
97      onEvent({ type: "phase", phase: "pausing" });
98      const steering = await interaction.requestSteering();
99      onEvent({ type: "phase", phase: "idle" });
100      if (signal.aborted) return null;
101      return steering;
102    }
103    return "";
104  };
105
106  for (;;) {
107    const steering = await pauseStep();
108    if (steering === null) {
109      cancelled();
110      return;
111    }
112    steer(steering);
113
114    onEvent({ type: "phase", phase: "preparing" });
115    session.appendRequest({
116      provider: model.provider,
117      model: model.id,
118      api: model.api,
119      thinkingEffort: run.thinkingEffort,
120      systemPrompt: run.systemPrompt,
121      tools: run.tools,
122    });
123
124    onEvent({ type: "phase", phase: "waitingModel" });
125    const context = { systemPrompt: run.systemPrompt, tools: run.tools, messages };
126    let stream;
127    try {
128      stream = run.models.streamSimple(model, context, {
129        reasoning: run.thinkingEffort === "off" ? undefined : run.thinkingEffort,
130        signal,
131        sessionId: session.id ?? undefined,
132      });
133    } catch (error) {
134      failed((error as Error).message);
135      return;
136    }
137
138    onEvent({ type: "phase", phase: "streaming" });
139    try {
140      for await (const event of stream) {
141        if (event.type === "text_delta") onEvent({ type: "text", delta: event.delta });
142        else if (event.type === "thinking_delta") onEvent({ type: "reasoning", delta: event.delta });
143        // The tool's name is known as soon as the call starts being written.
144        else if (event.type === "toolcall_start") {
145          const block = event.partial.content[event.contentIndex];
146          if (block.type === "toolCall") onEvent({ type: "toolCallStart", name: block.name });
147        } else if (event.type === "toolcall_end") {
148          onEvent({
149            type: "toolCall",
150            name: event.toolCall.name,
151            arguments: event.toolCall.arguments,
152          });
153        }
154      }
155    } catch (error) {
156      failed((error as Error).message);
157      return;
158    }
159
160    const assistant = await stream.result();
161    messages.push(assistant);
162    session.appendMessage(assistant);
163    onEvent({ type: "message", message: assistant });
164
165    if (assistant.stopReason === "aborted") {
166      cancelled();
167      return;
168    }
169    if (assistant.stopReason === "error") {
170      failed(assistant.errorMessage ?? "provider error");
171      return;
172    }
173
174    const toolCalls = assistant.content.filter((block): block is ToolCall => block.type === "toolCall");
175    if (toolCalls.length === 0) {
176      onEvent({ type: "complete" });
177      return;
178    }
179
180    // Steering is held until every result of this assistant turn has landed:
181    // the provider rejects a user message between an assistant turn and its
182    // tool results.
183    const held: string[] = [];
184    for (const call of toolCalls) {
185      const steering = await pauseStep();
186      if (steering === null) {
187        cancelled();
188        return;
189      }
190      if (steering !== "") held.push(steering);
191      onEvent({ type: "phase", phase: "runningTool", detail: call.name });
192
193      let result: ToolResult;
194      try {
195        result = await executeTool(call.name, validateToolCall(run.tools, call), {
196          signal,
197          supportsImages: acceptsImages(model),
198          onOutput: (chunk) => onEvent({ type: "toolOutput", chunk }),
199        });
200      } catch (error) {
201        result = { text: (error as Error).message, isError: true };
202      }
203
204      const toolMessage: ToolResultMessage = {
205        role: "toolResult",
206        toolCallId: call.id,
207        toolName: call.name,
208        content: [{ type: "text", text: result.text }, ...(result.images ?? [])],
209        isError: result.isError,
210        timestamp: Date.now(),
211      };
212      messages.push(toolMessage);
213      session.appendMessage(toolMessage);
214      onEvent({
215        type: "toolResult",
216        name: call.name,
217        text: result.text,
218        isError: result.isError,
219        diffs: result.diffs,
220      });
221    }
222    steer(held.join("\n"));
223  }
224}