@oh-my-pi/pi-agent-core 1.337.0
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 +370 -0
- package/package.json +40 -0
- package/src/agent-loop.ts +409 -0
- package/src/agent.ts +386 -0
- package/src/index.ts +8 -0
- package/src/proxy.ts +339 -0
- package/src/types.ts +210 -0
package/README.md
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
# @oh-my-pi/pi-agent
|
|
2
|
+
|
|
3
|
+
Stateful agent with tool execution and event streaming. Built on `@oh-my-pi/pi-ai`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @oh-my-pi/pi-agent
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Agent } from "@oh-my-pi/pi-agent";
|
|
15
|
+
import { getModel } from "@oh-my-pi/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
|
+
|
|
40
|
+
- Standard LLM messages (`user`, `assistant`, `toolResult`)
|
|
41
|
+
- Custom app-specific message types via declaration merging
|
|
42
|
+
|
|
43
|
+
LLMs only understand `user`, `assistant`, and `toolResult`. The `convertToLlm` function bridges this gap by filtering and transforming messages before each LLM call.
|
|
44
|
+
|
|
45
|
+
### Message Flow
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
AgentMessage[] → transformContext() → AgentMessage[] → convertToLlm() → Message[] → LLM
|
|
49
|
+
(optional) (required)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
1. **transformContext**: Prune old messages, inject external context
|
|
53
|
+
2. **convertToLlm**: Filter out UI-only messages, convert custom types to LLM format
|
|
54
|
+
|
|
55
|
+
## Event Flow
|
|
56
|
+
|
|
57
|
+
The agent emits events for UI updates. Understanding the event sequence helps build responsive interfaces.
|
|
58
|
+
|
|
59
|
+
### prompt() Event Sequence
|
|
60
|
+
|
|
61
|
+
When you call `prompt("Hello")`:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
prompt("Hello")
|
|
65
|
+
├─ agent_start
|
|
66
|
+
├─ turn_start
|
|
67
|
+
├─ message_start { message: userMessage } // Your prompt
|
|
68
|
+
├─ message_end { message: userMessage }
|
|
69
|
+
├─ message_start { message: assistantMessage } // LLM starts responding
|
|
70
|
+
├─ message_update { message: partial... } // Streaming chunks
|
|
71
|
+
├─ message_update { message: partial... }
|
|
72
|
+
├─ message_end { message: assistantMessage } // Complete response
|
|
73
|
+
├─ turn_end { message, toolResults: [] }
|
|
74
|
+
└─ agent_end { messages: [...] }
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### With Tool Calls
|
|
78
|
+
|
|
79
|
+
If the assistant calls tools, the loop continues:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
prompt("Read config.json")
|
|
83
|
+
├─ agent_start
|
|
84
|
+
├─ turn_start
|
|
85
|
+
├─ message_start/end { userMessage }
|
|
86
|
+
├─ message_start { assistantMessage with toolCall }
|
|
87
|
+
├─ message_update...
|
|
88
|
+
├─ message_end { assistantMessage }
|
|
89
|
+
├─ tool_execution_start { toolCallId, toolName, args }
|
|
90
|
+
├─ tool_execution_update { partialResult } // If tool streams
|
|
91
|
+
├─ tool_execution_end { toolCallId, result }
|
|
92
|
+
├─ message_start/end { toolResultMessage }
|
|
93
|
+
├─ turn_end { message, toolResults: [toolResult] }
|
|
94
|
+
│
|
|
95
|
+
├─ turn_start // Next turn
|
|
96
|
+
├─ message_start { assistantMessage } // LLM responds to tool result
|
|
97
|
+
├─ message_update...
|
|
98
|
+
├─ message_end
|
|
99
|
+
├─ turn_end
|
|
100
|
+
└─ agent_end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### continue() Event Sequence
|
|
104
|
+
|
|
105
|
+
`continue()` resumes from existing context without adding a new message. Use it for retries after errors.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
// After an error, retry from current state
|
|
109
|
+
await agent.continue();
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The last message in context must be `user` or `toolResult` (not `assistant`).
|
|
113
|
+
|
|
114
|
+
### Event Types
|
|
115
|
+
|
|
116
|
+
| Event | Description |
|
|
117
|
+
| ----------------------- | --------------------------------------------------------------- |
|
|
118
|
+
| `agent_start` | Agent begins processing |
|
|
119
|
+
| `agent_end` | Agent completes with all new messages |
|
|
120
|
+
| `turn_start` | New turn begins (one LLM call + tool executions) |
|
|
121
|
+
| `turn_end` | Turn completes with assistant message and tool results |
|
|
122
|
+
| `message_start` | Any message begins (user, assistant, toolResult) |
|
|
123
|
+
| `message_update` | **Assistant only.** Includes `assistantMessageEvent` with delta |
|
|
124
|
+
| `message_end` | Message completes |
|
|
125
|
+
| `tool_execution_start` | Tool begins |
|
|
126
|
+
| `tool_execution_update` | Tool streams progress |
|
|
127
|
+
| `tool_execution_end` | Tool completes |
|
|
128
|
+
|
|
129
|
+
## Agent Options
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
const agent = new Agent({
|
|
133
|
+
// Initial state
|
|
134
|
+
initialState: {
|
|
135
|
+
systemPrompt: string,
|
|
136
|
+
model: Model<any>,
|
|
137
|
+
thinkingLevel: "off" | "minimal" | "low" | "medium" | "high" | "xhigh",
|
|
138
|
+
tools: AgentTool<any>[],
|
|
139
|
+
messages: AgentMessage[],
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
// Convert AgentMessage[] to LLM Message[] (required for custom message types)
|
|
143
|
+
convertToLlm: (messages) => messages.filter(...),
|
|
144
|
+
|
|
145
|
+
// Transform context before convertToLlm (for pruning, compaction)
|
|
146
|
+
transformContext: async (messages, signal) => pruneOldMessages(messages),
|
|
147
|
+
|
|
148
|
+
// How to handle queued messages: "one-at-a-time" (default) or "all"
|
|
149
|
+
queueMode: "one-at-a-time",
|
|
150
|
+
|
|
151
|
+
// Custom stream function (for proxy backends)
|
|
152
|
+
streamFn: streamProxy,
|
|
153
|
+
|
|
154
|
+
// Dynamic API key resolution (for expiring OAuth tokens)
|
|
155
|
+
getApiKey: async (provider) => refreshToken(),
|
|
156
|
+
|
|
157
|
+
// Tool execution context (late-bound UI/session access)
|
|
158
|
+
getToolContext: () => ({ /* app-defined */ }),
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Agent State
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
interface AgentState {
|
|
166
|
+
systemPrompt: string;
|
|
167
|
+
model: Model<any>;
|
|
168
|
+
thinkingLevel: ThinkingLevel;
|
|
169
|
+
tools: AgentTool<any>[];
|
|
170
|
+
messages: AgentMessage[];
|
|
171
|
+
isStreaming: boolean;
|
|
172
|
+
streamMessage: AgentMessage | null; // Current partial during streaming
|
|
173
|
+
pendingToolCalls: Set<string>;
|
|
174
|
+
error?: string;
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Access via `agent.state`. During streaming, `streamMessage` contains the partial assistant message.
|
|
179
|
+
|
|
180
|
+
## Methods
|
|
181
|
+
|
|
182
|
+
### Prompting
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
// Text prompt
|
|
186
|
+
await agent.prompt("Hello");
|
|
187
|
+
|
|
188
|
+
// With images
|
|
189
|
+
await agent.prompt("What's in this image?", [{ type: "image", data: base64Data, mimeType: "image/jpeg" }]);
|
|
190
|
+
|
|
191
|
+
// AgentMessage directly
|
|
192
|
+
await agent.prompt({ role: "user", content: "Hello", timestamp: Date.now() });
|
|
193
|
+
|
|
194
|
+
// Continue from current context (last message must be user or toolResult)
|
|
195
|
+
await agent.continue();
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### State Management
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
agent.setSystemPrompt("New prompt");
|
|
202
|
+
agent.setModel(getModel("openai", "gpt-4o"));
|
|
203
|
+
agent.setThinkingLevel("medium");
|
|
204
|
+
agent.setTools([myTool]);
|
|
205
|
+
agent.replaceMessages(newMessages);
|
|
206
|
+
agent.appendMessage(message);
|
|
207
|
+
agent.clearMessages();
|
|
208
|
+
agent.reset(); // Clear everything
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Control
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
agent.abort(); // Cancel current operation
|
|
215
|
+
await agent.waitForIdle(); // Wait for completion
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Events
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
const unsubscribe = agent.subscribe((event) => {
|
|
222
|
+
console.log(event.type);
|
|
223
|
+
});
|
|
224
|
+
unsubscribe();
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Message Queue
|
|
228
|
+
|
|
229
|
+
Queue messages to inject during tool execution (for user interruptions):
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
agent.setQueueMode("one-at-a-time");
|
|
233
|
+
|
|
234
|
+
// While agent is running tools
|
|
235
|
+
agent.queueMessage({
|
|
236
|
+
role: "user",
|
|
237
|
+
content: "Stop! Do this instead.",
|
|
238
|
+
timestamp: Date.now(),
|
|
239
|
+
});
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
When queued messages are detected after a tool completes:
|
|
243
|
+
|
|
244
|
+
1. Remaining tools are skipped with error results
|
|
245
|
+
2. Queued message is injected
|
|
246
|
+
3. LLM responds to the interruption
|
|
247
|
+
|
|
248
|
+
## Custom Message Types
|
|
249
|
+
|
|
250
|
+
Extend `AgentMessage` via declaration merging:
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
declare module "@oh-my-pi/pi-agent" {
|
|
254
|
+
interface CustomAgentMessages {
|
|
255
|
+
notification: { role: "notification"; text: string; timestamp: number };
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Now valid
|
|
260
|
+
const msg: AgentMessage = { role: "notification", text: "Info", timestamp: Date.now() };
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Handle custom types in `convertToLlm`:
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
const agent = new Agent({
|
|
267
|
+
convertToLlm: (messages) =>
|
|
268
|
+
messages.flatMap((m) => {
|
|
269
|
+
if (m.role === "notification") return []; // Filter out
|
|
270
|
+
return [m];
|
|
271
|
+
}),
|
|
272
|
+
});
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Tools
|
|
276
|
+
|
|
277
|
+
Define tools using `AgentTool`:
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
import { Type } from "@sinclair/typebox";
|
|
281
|
+
|
|
282
|
+
const readFileTool: AgentTool = {
|
|
283
|
+
name: "read_file",
|
|
284
|
+
label: "Read File", // For UI display
|
|
285
|
+
description: "Read a file's contents",
|
|
286
|
+
parameters: Type.Object({
|
|
287
|
+
path: Type.String({ description: "File path" }),
|
|
288
|
+
}),
|
|
289
|
+
execute: async (toolCallId, params, signal, onUpdate, context) => {
|
|
290
|
+
const content = await fs.readFile(params.path, "utf-8");
|
|
291
|
+
|
|
292
|
+
// Optional: stream progress
|
|
293
|
+
onUpdate?.({ content: [{ type: "text", text: "Reading..." }], details: {} });
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
content: [{ type: "text", text: content }],
|
|
297
|
+
details: { path: params.path, size: content.length },
|
|
298
|
+
};
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
agent.setTools([readFileTool]);
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### Error Handling
|
|
306
|
+
|
|
307
|
+
**Throw an error** when a tool fails. Do not return error messages as content.
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
execute: async (toolCallId, params, signal, onUpdate) => {
|
|
311
|
+
if (!fs.existsSync(params.path)) {
|
|
312
|
+
throw new Error(`File not found: ${params.path}`);
|
|
313
|
+
}
|
|
314
|
+
// Return content only on success
|
|
315
|
+
return { content: [{ type: "text", text: "..." }] };
|
|
316
|
+
};
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Thrown errors are caught by the agent and reported to the LLM as tool errors with `isError: true`.
|
|
320
|
+
|
|
321
|
+
## Proxy Usage
|
|
322
|
+
|
|
323
|
+
For browser apps that proxy through a backend:
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
import { Agent, streamProxy } from "@oh-my-pi/pi-agent";
|
|
327
|
+
|
|
328
|
+
const agent = new Agent({
|
|
329
|
+
streamFn: (model, context, options) =>
|
|
330
|
+
streamProxy(model, context, {
|
|
331
|
+
...options,
|
|
332
|
+
authToken: "...",
|
|
333
|
+
proxyUrl: "https://your-server.com",
|
|
334
|
+
}),
|
|
335
|
+
});
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Low-Level API
|
|
339
|
+
|
|
340
|
+
For direct control without the Agent class:
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
import { agentLoop, agentLoopContinue } from "@oh-my-pi/pi-agent";
|
|
344
|
+
|
|
345
|
+
const context: AgentContext = {
|
|
346
|
+
systemPrompt: "You are helpful.",
|
|
347
|
+
messages: [],
|
|
348
|
+
tools: [],
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const config: AgentLoopConfig = {
|
|
352
|
+
model: getModel("openai", "gpt-4o"),
|
|
353
|
+
convertToLlm: (msgs) => msgs.filter((m) => ["user", "assistant", "toolResult"].includes(m.role)),
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const userMessage = { role: "user", content: "Hello", timestamp: Date.now() };
|
|
357
|
+
|
|
358
|
+
for await (const event of agentLoop([userMessage], context, config)) {
|
|
359
|
+
console.log(event.type);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Continue from existing context
|
|
363
|
+
for await (const event of agentLoopContinue(context, config)) {
|
|
364
|
+
console.log(event.type);
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
## License
|
|
369
|
+
|
|
370
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oh-my-pi/pi-agent-core",
|
|
3
|
+
"version": "1.337.0",
|
|
4
|
+
"description": "General-purpose agent with transport abstraction, state management, and attachment support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "vitest --run"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@oh-my-pi/pi-ai": "workspace:*",
|
|
17
|
+
"@oh-my-pi/pi-tui": "workspace:*"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"ai",
|
|
21
|
+
"agent",
|
|
22
|
+
"llm",
|
|
23
|
+
"transport",
|
|
24
|
+
"state-management"
|
|
25
|
+
],
|
|
26
|
+
"author": "Mario Zechner",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/can1357/oh-my-pi.git",
|
|
31
|
+
"directory": "packages/agent"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"bun": ">=1.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^24.3.0",
|
|
38
|
+
"vitest": "^3.2.4"
|
|
39
|
+
}
|
|
40
|
+
}
|