phi-code-agent 0.56.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,412 @@
1
+ # @mariozechner/pi-agent-core
2
+
3
+ Stateful agent with tool execution and event streaming. Built on `@mariozechner/pi-ai`.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @mariozechner/pi-agent-core
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { Agent } from "@mariozechner/pi-agent-core";
15
+ import { getModel } from "@mariozechner/pi-ai";
16
+
17
+ const agent = new Agent({
18
+ initialState: {
19
+ systemPrompt: "You are a helpful assistant.",
20
+ model: getModel("anthropic", "claude-sonnet-4-20250514"),
21
+ },
22
+ });
23
+
24
+ agent.subscribe((event) => {
25
+ if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
26
+ // Stream just the new text chunk
27
+ process.stdout.write(event.assistantMessageEvent.delta);
28
+ }
29
+ });
30
+
31
+ await agent.prompt("Hello!");
32
+ ```
33
+
34
+ ## Core Concepts
35
+
36
+ ### AgentMessage vs LLM Message
37
+
38
+ The agent works with `AgentMessage`, a flexible type that can include:
39
+ - Standard LLM messages (`user`, `assistant`, `toolResult`)
40
+ - Custom app-specific message types via declaration merging
41
+
42
+ LLMs only understand `user`, `assistant`, and `toolResult`. The `convertToLlm` function bridges this gap by filtering and transforming messages before each LLM call.
43
+
44
+ ### Message Flow
45
+
46
+ ```
47
+ AgentMessage[] → transformContext() → AgentMessage[] → convertToLlm() → Message[] → LLM
48
+ (optional) (required)
49
+ ```
50
+
51
+ 1. **transformContext**: Prune old messages, inject external context
52
+ 2. **convertToLlm**: Filter out UI-only messages, convert custom types to LLM format
53
+
54
+ ## Event Flow
55
+
56
+ The agent emits events for UI updates. Understanding the event sequence helps build responsive interfaces.
57
+
58
+ ### prompt() Event Sequence
59
+
60
+ When you call `prompt("Hello")`:
61
+
62
+ ```
63
+ prompt("Hello")
64
+ ├─ agent_start
65
+ ├─ turn_start
66
+ ├─ message_start { message: userMessage } // Your prompt
67
+ ├─ message_end { message: userMessage }
68
+ ├─ message_start { message: assistantMessage } // LLM starts responding
69
+ ├─ message_update { message: partial... } // Streaming chunks
70
+ ├─ message_update { message: partial... }
71
+ ├─ message_end { message: assistantMessage } // Complete response
72
+ ├─ turn_end { message, toolResults: [] }
73
+ └─ agent_end { messages: [...] }
74
+ ```
75
+
76
+ ### With Tool Calls
77
+
78
+ If the assistant calls tools, the loop continues:
79
+
80
+ ```
81
+ prompt("Read config.json")
82
+ ├─ agent_start
83
+ ├─ turn_start
84
+ ├─ message_start/end { userMessage }
85
+ ├─ message_start { assistantMessage with toolCall }
86
+ ├─ message_update...
87
+ ├─ message_end { assistantMessage }
88
+ ├─ tool_execution_start { toolCallId, toolName, args }
89
+ ├─ tool_execution_update { partialResult } // If tool streams
90
+ ├─ tool_execution_end { toolCallId, result }
91
+ ├─ message_start/end { toolResultMessage }
92
+ ├─ turn_end { message, toolResults: [toolResult] }
93
+
94
+ ├─ turn_start // Next turn
95
+ ├─ message_start { assistantMessage } // LLM responds to tool result
96
+ ├─ message_update...
97
+ ├─ message_end
98
+ ├─ turn_end
99
+ └─ agent_end
100
+ ```
101
+
102
+ ### continue() Event Sequence
103
+
104
+ `continue()` resumes from existing context without adding a new message. Use it for retries after errors.
105
+
106
+ ```typescript
107
+ // After an error, retry from current state
108
+ await agent.continue();
109
+ ```
110
+
111
+ The last message in context must be `user` or `toolResult` (not `assistant`).
112
+
113
+ ### Event Types
114
+
115
+ | Event | Description |
116
+ |-------|-------------|
117
+ | `agent_start` | Agent begins processing |
118
+ | `agent_end` | Agent completes with all new messages |
119
+ | `turn_start` | New turn begins (one LLM call + tool executions) |
120
+ | `turn_end` | Turn completes with assistant message and tool results |
121
+ | `message_start` | Any message begins (user, assistant, toolResult) |
122
+ | `message_update` | **Assistant only.** Includes `assistantMessageEvent` with delta |
123
+ | `message_end` | Message completes |
124
+ | `tool_execution_start` | Tool begins |
125
+ | `tool_execution_update` | Tool streams progress |
126
+ | `tool_execution_end` | Tool completes |
127
+
128
+ ## Agent Options
129
+
130
+ ```typescript
131
+ const agent = new Agent({
132
+ // Initial state
133
+ initialState: {
134
+ systemPrompt: string,
135
+ model: Model<any>,
136
+ thinkingLevel: "off" | "minimal" | "low" | "medium" | "high" | "xhigh",
137
+ tools: AgentTool<any>[],
138
+ messages: AgentMessage[],
139
+ },
140
+
141
+ // Convert AgentMessage[] to LLM Message[] (required for custom message types)
142
+ convertToLlm: (messages) => messages.filter(...),
143
+
144
+ // Transform context before convertToLlm (for pruning, compaction)
145
+ transformContext: async (messages, signal) => pruneOldMessages(messages),
146
+
147
+ // Steering mode: "one-at-a-time" (default) or "all"
148
+ steeringMode: "one-at-a-time",
149
+
150
+ // Follow-up mode: "one-at-a-time" (default) or "all"
151
+ followUpMode: "one-at-a-time",
152
+
153
+ // Custom stream function (for proxy backends)
154
+ streamFn: streamProxy,
155
+
156
+ // Session ID for provider caching
157
+ sessionId: "session-123",
158
+
159
+ // Dynamic API key resolution (for expiring OAuth tokens)
160
+ getApiKey: async (provider) => refreshToken(),
161
+
162
+ // Custom thinking budgets for token-based providers
163
+ thinkingBudgets: {
164
+ minimal: 128,
165
+ low: 512,
166
+ medium: 1024,
167
+ high: 2048,
168
+ },
169
+ });
170
+ ```
171
+
172
+ ## Agent State
173
+
174
+ ```typescript
175
+ interface AgentState {
176
+ systemPrompt: string;
177
+ model: Model<any>;
178
+ thinkingLevel: ThinkingLevel;
179
+ tools: AgentTool<any>[];
180
+ messages: AgentMessage[];
181
+ isStreaming: boolean;
182
+ streamMessage: AgentMessage | null; // Current partial during streaming
183
+ pendingToolCalls: Set<string>;
184
+ error?: string;
185
+ }
186
+ ```
187
+
188
+ Access via `agent.state`. During streaming, `streamMessage` contains the partial assistant message.
189
+
190
+ ## Methods
191
+
192
+ ### Prompting
193
+
194
+ ```typescript
195
+ // Text prompt
196
+ await agent.prompt("Hello");
197
+
198
+ // With images
199
+ await agent.prompt("What's in this image?", [
200
+ { type: "image", data: base64Data, mimeType: "image/jpeg" }
201
+ ]);
202
+
203
+ // AgentMessage directly
204
+ await agent.prompt({ role: "user", content: "Hello", timestamp: Date.now() });
205
+
206
+ // Continue from current context (last message must be user or toolResult)
207
+ await agent.continue();
208
+ ```
209
+
210
+ ### State Management
211
+
212
+ ```typescript
213
+ agent.setSystemPrompt("New prompt");
214
+ agent.setModel(getModel("openai", "gpt-4o"));
215
+ agent.setThinkingLevel("medium");
216
+ agent.setTools([myTool]);
217
+ agent.replaceMessages(newMessages);
218
+ agent.appendMessage(message);
219
+ agent.clearMessages();
220
+ agent.reset(); // Clear everything
221
+ ```
222
+
223
+ ### Session and Thinking Budgets
224
+
225
+ ```typescript
226
+ agent.sessionId = "session-123";
227
+
228
+ agent.thinkingBudgets = {
229
+ minimal: 128,
230
+ low: 512,
231
+ medium: 1024,
232
+ high: 2048,
233
+ };
234
+ ```
235
+
236
+ ### Control
237
+
238
+ ```typescript
239
+ agent.abort(); // Cancel current operation
240
+ await agent.waitForIdle(); // Wait for completion
241
+ ```
242
+
243
+ ### Events
244
+
245
+ ```typescript
246
+ const unsubscribe = agent.subscribe((event) => {
247
+ console.log(event.type);
248
+ });
249
+ unsubscribe();
250
+ ```
251
+
252
+ ## Steering and Follow-up
253
+
254
+ Steering messages let you interrupt the agent while tools are running. Follow-up messages let you queue work after the agent would otherwise stop.
255
+
256
+ ```typescript
257
+ agent.setSteeringMode("one-at-a-time");
258
+ agent.setFollowUpMode("one-at-a-time");
259
+
260
+ // While agent is running tools
261
+ agent.steer({
262
+ role: "user",
263
+ content: "Stop! Do this instead.",
264
+ timestamp: Date.now(),
265
+ });
266
+
267
+ // After the agent finishes its current work
268
+ agent.followUp({
269
+ role: "user",
270
+ content: "Also summarize the result.",
271
+ timestamp: Date.now(),
272
+ });
273
+
274
+ const steeringMode = agent.getSteeringMode();
275
+ const followUpMode = agent.getFollowUpMode();
276
+
277
+ agent.clearSteeringQueue();
278
+ agent.clearFollowUpQueue();
279
+ agent.clearAllQueues();
280
+ ```
281
+
282
+ Use clearSteeringQueue, clearFollowUpQueue, or clearAllQueues to drop queued messages.
283
+
284
+ When steering messages are detected after a tool completes:
285
+ 1. Remaining tools are skipped with error results
286
+ 2. Steering messages are injected
287
+ 3. LLM responds to the interruption
288
+
289
+ Follow-up messages are checked only when there are no more tool calls and no steering messages. If any are queued, they are injected and another turn runs.
290
+
291
+ ## Custom Message Types
292
+
293
+ Extend `AgentMessage` via declaration merging:
294
+
295
+ ```typescript
296
+ declare module "@mariozechner/pi-agent-core" {
297
+ interface CustomAgentMessages {
298
+ notification: { role: "notification"; text: string; timestamp: number };
299
+ }
300
+ }
301
+
302
+ // Now valid
303
+ const msg: AgentMessage = { role: "notification", text: "Info", timestamp: Date.now() };
304
+ ```
305
+
306
+ Handle custom types in `convertToLlm`:
307
+
308
+ ```typescript
309
+ const agent = new Agent({
310
+ convertToLlm: (messages) => messages.flatMap(m => {
311
+ if (m.role === "notification") return []; // Filter out
312
+ return [m];
313
+ }),
314
+ });
315
+ ```
316
+
317
+ ## Tools
318
+
319
+ Define tools using `AgentTool`:
320
+
321
+ ```typescript
322
+ import { Type } from "@sinclair/typebox";
323
+
324
+ const readFileTool: AgentTool = {
325
+ name: "read_file",
326
+ label: "Read File", // For UI display
327
+ description: "Read a file's contents",
328
+ parameters: Type.Object({
329
+ path: Type.String({ description: "File path" }),
330
+ }),
331
+ execute: async (toolCallId, params, signal, onUpdate) => {
332
+ const content = await fs.readFile(params.path, "utf-8");
333
+
334
+ // Optional: stream progress
335
+ onUpdate?.({ content: [{ type: "text", text: "Reading..." }], details: {} });
336
+
337
+ return {
338
+ content: [{ type: "text", text: content }],
339
+ details: { path: params.path, size: content.length },
340
+ };
341
+ },
342
+ };
343
+
344
+ agent.setTools([readFileTool]);
345
+ ```
346
+
347
+ ### Error Handling
348
+
349
+ **Throw an error** when a tool fails. Do not return error messages as content.
350
+
351
+ ```typescript
352
+ execute: async (toolCallId, params, signal, onUpdate) => {
353
+ if (!fs.existsSync(params.path)) {
354
+ throw new Error(`File not found: ${params.path}`);
355
+ }
356
+ // Return content only on success
357
+ return { content: [{ type: "text", text: "..." }] };
358
+ }
359
+ ```
360
+
361
+ Thrown errors are caught by the agent and reported to the LLM as tool errors with `isError: true`.
362
+
363
+ ## Proxy Usage
364
+
365
+ For browser apps that proxy through a backend:
366
+
367
+ ```typescript
368
+ import { Agent, streamProxy } from "@mariozechner/pi-agent-core";
369
+
370
+ const agent = new Agent({
371
+ streamFn: (model, context, options) =>
372
+ streamProxy(model, context, {
373
+ ...options,
374
+ authToken: "...",
375
+ proxyUrl: "https://your-server.com",
376
+ }),
377
+ });
378
+ ```
379
+
380
+ ## Low-Level API
381
+
382
+ For direct control without the Agent class:
383
+
384
+ ```typescript
385
+ import { agentLoop, agentLoopContinue } from "@mariozechner/pi-agent-core";
386
+
387
+ const context: AgentContext = {
388
+ systemPrompt: "You are helpful.",
389
+ messages: [],
390
+ tools: [],
391
+ };
392
+
393
+ const config: AgentLoopConfig = {
394
+ model: getModel("openai", "gpt-4o"),
395
+ convertToLlm: (msgs) => msgs.filter(m => ["user", "assistant", "toolResult"].includes(m.role)),
396
+ };
397
+
398
+ const userMessage = { role: "user", content: "Hello", timestamp: Date.now() };
399
+
400
+ for await (const event of agentLoop([userMessage], context, config)) {
401
+ console.log(event.type);
402
+ }
403
+
404
+ // Continue from existing context
405
+ for await (const event of agentLoopContinue(context, config)) {
406
+ console.log(event.type);
407
+ }
408
+ ```
409
+
410
+ ## License
411
+
412
+ MIT
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Agent loop that works with AgentMessage throughout.
3
+ * Transforms to Message[] only at the LLM call boundary.
4
+ */
5
+ import { EventStream } from "phi-code-ai";
6
+ import type { AgentContext, AgentEvent, AgentLoopConfig, AgentMessage, StreamFn } from "./types.js";
7
+ /**
8
+ * Start an agent loop with a new prompt message.
9
+ * The prompt is added to the context and events are emitted for it.
10
+ */
11
+ export declare function agentLoop(prompts: AgentMessage[], context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn): EventStream<AgentEvent, AgentMessage[]>;
12
+ /**
13
+ * Continue an agent loop from the current context without adding a new message.
14
+ * Used for retries - context already has user message or tool results.
15
+ *
16
+ * **Important:** The last message in context must convert to a `user` or `toolResult` message
17
+ * via `convertToLlm`. If it doesn't, the LLM provider will reject the request.
18
+ * This cannot be validated here since `convertToLlm` is only called once per turn.
19
+ */
20
+ export declare function agentLoopContinue(context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn): EventStream<AgentEvent, AgentMessage[]>;
21
+ //# sourceMappingURL=agent-loop.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-loop.d.ts","sourceRoot":"","sources":["../src/agent-loop.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAGN,WAAW,EAIX,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EACX,YAAY,EACZ,UAAU,EACV,eAAe,EACf,YAAY,EAGZ,QAAQ,EACR,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,wBAAgB,SAAS,CACxB,OAAO,EAAE,YAAY,EAAE,EACvB,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,eAAe,EACvB,MAAM,CAAC,EAAE,WAAW,EACpB,QAAQ,CAAC,EAAE,QAAQ,GACjB,WAAW,CAAC,UAAU,EAAE,YAAY,EAAE,CAAC,CAqBzC;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAChC,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,eAAe,EACvB,MAAM,CAAC,EAAE,WAAW,EACpB,QAAQ,CAAC,EAAE,QAAQ,GACjB,WAAW,CAAC,UAAU,EAAE,YAAY,EAAE,CAAC,CAsBzC"}