@hyperspaceng/neural-agent-core 0.60.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.
Files changed (2) hide show
  1. package/README.md +446 -0
  2. package/package.json +44 -0
package/README.md ADDED
@@ -0,0 +1,446 @@
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
+ Tool execution mode is configurable:
103
+
104
+ - `parallel` (default): preflight tool calls sequentially, execute allowed tools concurrently, emit final `tool_execution_end` and `toolResult` messages in assistant source order
105
+ - `sequential`: execute tool calls one by one, matching the historical behavior
106
+
107
+ The `beforeToolCall` hook runs after `tool_execution_start` and validated argument parsing. It can block execution. The `afterToolCall` hook runs after tool execution finishes and before `tool_execution_end` and final tool result message events are emitted.
108
+
109
+ When you use the `Agent` class, assistant `message_end` processing is treated as a barrier before tool preflight begins. That means `beforeToolCall` sees agent state that already includes the assistant message that requested the tool call.
110
+
111
+ ### continue() Event Sequence
112
+
113
+ `continue()` resumes from existing context without adding a new message. Use it for retries after errors.
114
+
115
+ ```typescript
116
+ // After an error, retry from current state
117
+ await agent.continue();
118
+ ```
119
+
120
+ The last message in context must be `user` or `toolResult` (not `assistant`).
121
+
122
+ ### Event Types
123
+
124
+ | Event | Description |
125
+ |-------|-------------|
126
+ | `agent_start` | Agent begins processing |
127
+ | `agent_end` | Agent completes with all new messages |
128
+ | `turn_start` | New turn begins (one LLM call + tool executions) |
129
+ | `turn_end` | Turn completes with assistant message and tool results |
130
+ | `message_start` | Any message begins (user, assistant, toolResult) |
131
+ | `message_update` | **Assistant only.** Includes `assistantMessageEvent` with delta |
132
+ | `message_end` | Message completes |
133
+ | `tool_execution_start` | Tool begins |
134
+ | `tool_execution_update` | Tool streams progress |
135
+ | `tool_execution_end` | Tool completes |
136
+
137
+ ## Agent Options
138
+
139
+ ```typescript
140
+ const agent = new Agent({
141
+ // Initial state
142
+ initialState: {
143
+ systemPrompt: string,
144
+ model: Model<any>,
145
+ thinkingLevel: "off" | "minimal" | "low" | "medium" | "high" | "xhigh",
146
+ tools: AgentTool<any>[],
147
+ messages: AgentMessage[],
148
+ },
149
+
150
+ // Convert AgentMessage[] to LLM Message[] (required for custom message types)
151
+ convertToLlm: (messages) => messages.filter(...),
152
+
153
+ // Transform context before convertToLlm (for pruning, compaction)
154
+ transformContext: async (messages, signal) => pruneOldMessages(messages),
155
+
156
+ // Steering mode: "one-at-a-time" (default) or "all"
157
+ steeringMode: "one-at-a-time",
158
+
159
+ // Follow-up mode: "one-at-a-time" (default) or "all"
160
+ followUpMode: "one-at-a-time",
161
+
162
+ // Custom stream function (for proxy backends)
163
+ streamFn: streamProxy,
164
+
165
+ // Session ID for provider caching
166
+ sessionId: "session-123",
167
+
168
+ // Dynamic API key resolution (for expiring OAuth tokens)
169
+ getApiKey: async (provider) => refreshToken(),
170
+
171
+ // Tool execution mode: "parallel" (default) or "sequential"
172
+ toolExecution: "parallel",
173
+
174
+ // Preflight each tool call after args are validated. Can block execution.
175
+ beforeToolCall: async ({ toolCall, args, context }) => {
176
+ if (toolCall.name === "bash") {
177
+ return { block: true, reason: "bash is disabled" };
178
+ }
179
+ },
180
+
181
+ // Postprocess each tool result before final tool events are emitted.
182
+ afterToolCall: async ({ toolCall, result, isError, context }) => {
183
+ if (!isError) {
184
+ return { details: { ...result.details, audited: true } };
185
+ }
186
+ },
187
+
188
+ // Custom thinking budgets for token-based providers
189
+ thinkingBudgets: {
190
+ minimal: 128,
191
+ low: 512,
192
+ medium: 1024,
193
+ high: 2048,
194
+ },
195
+ });
196
+ ```
197
+
198
+ ## Agent State
199
+
200
+ ```typescript
201
+ interface AgentState {
202
+ systemPrompt: string;
203
+ model: Model<any>;
204
+ thinkingLevel: ThinkingLevel;
205
+ tools: AgentTool<any>[];
206
+ messages: AgentMessage[];
207
+ isStreaming: boolean;
208
+ streamMessage: AgentMessage | null; // Current partial during streaming
209
+ pendingToolCalls: Set<string>;
210
+ error?: string;
211
+ }
212
+ ```
213
+
214
+ Access via `agent.state`. During streaming, `streamMessage` contains the partial assistant message.
215
+
216
+ ## Methods
217
+
218
+ ### Prompting
219
+
220
+ ```typescript
221
+ // Text prompt
222
+ await agent.prompt("Hello");
223
+
224
+ // With images
225
+ await agent.prompt("What's in this image?", [
226
+ { type: "image", data: base64Data, mimeType: "image/jpeg" }
227
+ ]);
228
+
229
+ // AgentMessage directly
230
+ await agent.prompt({ role: "user", content: "Hello", timestamp: Date.now() });
231
+
232
+ // Continue from current context (last message must be user or toolResult)
233
+ await agent.continue();
234
+ ```
235
+
236
+ ### State Management
237
+
238
+ ```typescript
239
+ agent.setSystemPrompt("New prompt");
240
+ agent.setModel(getModel("openai", "gpt-4o"));
241
+ agent.setThinkingLevel("medium");
242
+ agent.setTools([myTool]);
243
+ agent.setToolExecution("sequential");
244
+ agent.setBeforeToolCall(async ({ toolCall }) => undefined);
245
+ agent.setAfterToolCall(async ({ toolCall, result }) => undefined);
246
+ agent.replaceMessages(newMessages);
247
+ agent.appendMessage(message);
248
+ agent.clearMessages();
249
+ agent.reset(); // Clear everything
250
+ ```
251
+
252
+ ### Session and Thinking Budgets
253
+
254
+ ```typescript
255
+ agent.sessionId = "session-123";
256
+
257
+ agent.thinkingBudgets = {
258
+ minimal: 128,
259
+ low: 512,
260
+ medium: 1024,
261
+ high: 2048,
262
+ };
263
+ ```
264
+
265
+ ### Control
266
+
267
+ ```typescript
268
+ agent.abort(); // Cancel current operation
269
+ await agent.waitForIdle(); // Wait for completion
270
+ ```
271
+
272
+ ### Events
273
+
274
+ ```typescript
275
+ const unsubscribe = agent.subscribe((event) => {
276
+ console.log(event.type);
277
+ });
278
+ unsubscribe();
279
+ ```
280
+
281
+ ## Steering and Follow-up
282
+
283
+ Steering messages let you interrupt the agent while tools are running. Follow-up messages let you queue work after the agent would otherwise stop.
284
+
285
+ ```typescript
286
+ agent.setSteeringMode("one-at-a-time");
287
+ agent.setFollowUpMode("one-at-a-time");
288
+
289
+ // While agent is running tools
290
+ agent.steer({
291
+ role: "user",
292
+ content: "Stop! Do this instead.",
293
+ timestamp: Date.now(),
294
+ });
295
+
296
+ // After the agent finishes its current work
297
+ agent.followUp({
298
+ role: "user",
299
+ content: "Also summarize the result.",
300
+ timestamp: Date.now(),
301
+ });
302
+
303
+ const steeringMode = agent.getSteeringMode();
304
+ const followUpMode = agent.getFollowUpMode();
305
+
306
+ agent.clearSteeringQueue();
307
+ agent.clearFollowUpQueue();
308
+ agent.clearAllQueues();
309
+ ```
310
+
311
+ Use clearSteeringQueue, clearFollowUpQueue, or clearAllQueues to drop queued messages.
312
+
313
+ When steering messages are detected after a turn completes:
314
+ 1. All tool calls from the current assistant message have already finished
315
+ 2. Steering messages are injected
316
+ 3. The LLM responds on the next turn
317
+
318
+ 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.
319
+
320
+ ## Custom Message Types
321
+
322
+ Extend `AgentMessage` via declaration merging:
323
+
324
+ ```typescript
325
+ declare module "@mariozechner/pi-agent-core" {
326
+ interface CustomAgentMessages {
327
+ notification: { role: "notification"; text: string; timestamp: number };
328
+ }
329
+ }
330
+
331
+ // Now valid
332
+ const msg: AgentMessage = { role: "notification", text: "Info", timestamp: Date.now() };
333
+ ```
334
+
335
+ Handle custom types in `convertToLlm`:
336
+
337
+ ```typescript
338
+ const agent = new Agent({
339
+ convertToLlm: (messages) => messages.flatMap(m => {
340
+ if (m.role === "notification") return []; // Filter out
341
+ return [m];
342
+ }),
343
+ });
344
+ ```
345
+
346
+ ## Tools
347
+
348
+ Define tools using `AgentTool`:
349
+
350
+ ```typescript
351
+ import { Type } from "@sinclair/typebox";
352
+
353
+ const readFileTool: AgentTool = {
354
+ name: "read_file",
355
+ label: "Read File", // For UI display
356
+ description: "Read a file's contents",
357
+ parameters: Type.Object({
358
+ path: Type.String({ description: "File path" }),
359
+ }),
360
+ execute: async (toolCallId, params, signal, onUpdate) => {
361
+ const content = await fs.readFile(params.path, "utf-8");
362
+
363
+ // Optional: stream progress
364
+ onUpdate?.({ content: [{ type: "text", text: "Reading..." }], details: {} });
365
+
366
+ return {
367
+ content: [{ type: "text", text: content }],
368
+ details: { path: params.path, size: content.length },
369
+ };
370
+ },
371
+ };
372
+
373
+ agent.setTools([readFileTool]);
374
+ ```
375
+
376
+ ### Error Handling
377
+
378
+ **Throw an error** when a tool fails. Do not return error messages as content.
379
+
380
+ ```typescript
381
+ execute: async (toolCallId, params, signal, onUpdate) => {
382
+ if (!fs.existsSync(params.path)) {
383
+ throw new Error(`File not found: ${params.path}`);
384
+ }
385
+ // Return content only on success
386
+ return { content: [{ type: "text", text: "..." }] };
387
+ }
388
+ ```
389
+
390
+ Thrown errors are caught by the agent and reported to the LLM as tool errors with `isError: true`.
391
+
392
+ ## Proxy Usage
393
+
394
+ For browser apps that proxy through a backend:
395
+
396
+ ```typescript
397
+ import { Agent, streamProxy } from "@mariozechner/pi-agent-core";
398
+
399
+ const agent = new Agent({
400
+ streamFn: (model, context, options) =>
401
+ streamProxy(model, context, {
402
+ ...options,
403
+ authToken: "...",
404
+ proxyUrl: "https://your-server.com",
405
+ }),
406
+ });
407
+ ```
408
+
409
+ ## Low-Level API
410
+
411
+ For direct control without the Agent class:
412
+
413
+ ```typescript
414
+ import { agentLoop, agentLoopContinue } from "@mariozechner/pi-agent-core";
415
+
416
+ const context: AgentContext = {
417
+ systemPrompt: "You are helpful.",
418
+ messages: [],
419
+ tools: [],
420
+ };
421
+
422
+ const config: AgentLoopConfig = {
423
+ model: getModel("openai", "gpt-4o"),
424
+ convertToLlm: (msgs) => msgs.filter(m => ["user", "assistant", "toolResult"].includes(m.role)),
425
+ toolExecution: "parallel",
426
+ beforeToolCall: async ({ toolCall, args, context }) => undefined,
427
+ afterToolCall: async ({ toolCall, result, isError, context }) => undefined,
428
+ };
429
+
430
+ const userMessage = { role: "user", content: "Hello", timestamp: Date.now() };
431
+
432
+ for await (const event of agentLoop([userMessage], context, config)) {
433
+ console.log(event.type);
434
+ }
435
+
436
+ // Continue from existing context
437
+ for await (const event of agentLoopContinue(context, config)) {
438
+ console.log(event.type);
439
+ }
440
+ ```
441
+
442
+ These low-level streams are observational. They preserve event order, but they do not wait for your async event handling to settle before later producer phases continue. If you need message processing to act as a barrier before tool preflight, use the `Agent` class instead of raw `agentLoop()` or `agentLoopContinue()`.
443
+
444
+ ## License
445
+
446
+ MIT
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@hyperspaceng/neural-agent-core",
3
+ "version": "0.60.0",
4
+ "description": "General-purpose agent with transport abstraction, state management, and attachment support",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "scripts": {
13
+ "clean": "shx rm -rf dist",
14
+ "build": "tsgo -p tsconfig.build.json",
15
+ "dev": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
16
+ "test": "vitest --run",
17
+ "prepublishOnly": "npm run clean && npm run build"
18
+ },
19
+ "dependencies": {
20
+ "@hyperspaceng/neural-ai": "^0.60.0"
21
+ },
22
+ "keywords": [
23
+ "ai",
24
+ "agent",
25
+ "llm",
26
+ "transport",
27
+ "state-management"
28
+ ],
29
+ "author": "Hyperspace Technologies <hyperspace@hyperspace.ng>",
30
+ "license": "MIT",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/badlogic/pi-mono.git",
34
+ "directory": "packages/agent"
35
+ },
36
+ "engines": {
37
+ "node": ">=20.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^24.3.0",
41
+ "typescript": "^5.7.3",
42
+ "vitest": "^3.2.4"
43
+ }
44
+ }