@gajae-code/agent-core 0.1.1
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/CHANGELOG.md +482 -0
- package/README.md +473 -0
- package/dist/types/agent-loop.d.ts +55 -0
- package/dist/types/agent.d.ts +334 -0
- package/dist/types/append-only-context.d.ts +113 -0
- package/dist/types/compaction/branch-summarization.d.ts +94 -0
- package/dist/types/compaction/compaction.d.ts +166 -0
- package/dist/types/compaction/entries.d.ts +103 -0
- package/dist/types/compaction/errors.d.ts +26 -0
- package/dist/types/compaction/index.d.ts +11 -0
- package/dist/types/compaction/messages.d.ts +61 -0
- package/dist/types/compaction/openai.d.ts +58 -0
- package/dist/types/compaction/pruning.d.ts +18 -0
- package/dist/types/compaction/utils.d.ts +32 -0
- package/dist/types/compaction.d.ts +1 -0
- package/dist/types/harmony-leak.d.ts +99 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/proxy.d.ts +84 -0
- package/dist/types/run-collector.d.ts +196 -0
- package/dist/types/telemetry.d.ts +588 -0
- package/dist/types/thinking.d.ts +17 -0
- package/dist/types/types.d.ts +407 -0
- package/package.json +75 -0
- package/src/agent-loop.ts +1279 -0
- package/src/agent.ts +1399 -0
- package/src/append-only-context.ts +297 -0
- package/src/compaction/branch-summarization.ts +339 -0
- package/src/compaction/compaction.ts +1065 -0
- package/src/compaction/entries.ts +133 -0
- package/src/compaction/errors.ts +31 -0
- package/src/compaction/index.ts +12 -0
- package/src/compaction/messages.ts +212 -0
- package/src/compaction/openai.ts +552 -0
- package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
- package/src/compaction/prompts/branch-summary-context.md +5 -0
- package/src/compaction/prompts/branch-summary-preamble.md +2 -0
- package/src/compaction/prompts/branch-summary.md +30 -0
- package/src/compaction/prompts/compaction-short-summary.md +9 -0
- package/src/compaction/prompts/compaction-summary-context.md +5 -0
- package/src/compaction/prompts/compaction-summary.md +38 -0
- package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
- package/src/compaction/prompts/compaction-update-summary.md +45 -0
- package/src/compaction/prompts/file-operations.md +10 -0
- package/src/compaction/prompts/handoff-document.md +49 -0
- package/src/compaction/prompts/summarization-system.md +3 -0
- package/src/compaction/pruning.ts +92 -0
- package/src/compaction/utils.ts +185 -0
- package/src/compaction.ts +1 -0
- package/src/harmony-leak.ts +427 -0
- package/src/index.ts +19 -0
- package/src/proxy.ts +326 -0
- package/src/run-collector.ts +631 -0
- package/src/telemetry.ts +2018 -0
- package/src/thinking.ts +19 -0
- package/src/types.ts +467 -0
package/README.md
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
# @gajae-code/agent-core
|
|
2
|
+
|
|
3
|
+
Stateful agent with tool execution and event streaming. Built on `@gajae-code/ai`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @gajae-code/agent-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Agent } from "@gajae-code/agent-core";
|
|
15
|
+
import { getModel } from "@gajae-code/ai";
|
|
16
|
+
|
|
17
|
+
const agent = new Agent({
|
|
18
|
+
initialState: {
|
|
19
|
+
systemPrompt: ["You are a helpful assistant."],
|
|
20
|
+
model: getModel("anthropic", "anthropic-model-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,
|
|
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;
|
|
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
|
+
## Steering & Follow-up
|
|
228
|
+
|
|
229
|
+
Queue messages to inject during tool execution (steering) or after the agent would otherwise stop (follow-up):
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
agent.setSteeringMode("one-at-a-time");
|
|
233
|
+
agent.setInterruptMode("immediate");
|
|
234
|
+
|
|
235
|
+
// While agent is running tools
|
|
236
|
+
agent.steer({
|
|
237
|
+
role: "user",
|
|
238
|
+
content: "Stop! Do this instead.",
|
|
239
|
+
timestamp: Date.now(),
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// Queue a follow-up to run after the current turn completes
|
|
243
|
+
agent.followUp({
|
|
244
|
+
role: "user",
|
|
245
|
+
content: "After that, summarize the changes.",
|
|
246
|
+
timestamp: Date.now(),
|
|
247
|
+
});
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Steering messages are checked after each tool call by default. Set `interruptMode` to `"wait"` to defer
|
|
251
|
+
steering until the current turn completes.
|
|
252
|
+
|
|
253
|
+
## Custom Message Types
|
|
254
|
+
|
|
255
|
+
Extend `AgentMessage` via declaration merging:
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
declare module "@gajae-code/agent-core" {
|
|
259
|
+
interface CustomAgentMessages {
|
|
260
|
+
notification: { role: "notification"; text: string; timestamp: number };
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Now valid
|
|
265
|
+
const msg: AgentMessage = { role: "notification", text: "Info", timestamp: Date.now() };
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Handle custom types in `convertToLlm`:
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
const agent = new Agent({
|
|
272
|
+
convertToLlm: (messages) =>
|
|
273
|
+
messages.flatMap((m) => {
|
|
274
|
+
if (m.role === "notification") return []; // Filter out
|
|
275
|
+
return [m];
|
|
276
|
+
}),
|
|
277
|
+
});
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Tools
|
|
281
|
+
|
|
282
|
+
Define tools using `AgentTool` with a Zod parameter schema (via `z` from `@gajae-code/ai`).
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import { z } from "@gajae-code/ai";
|
|
286
|
+
|
|
287
|
+
const readFileTool: AgentTool = {
|
|
288
|
+
name: "read_file",
|
|
289
|
+
label: "Read File", // For UI display
|
|
290
|
+
description: "Read a file's contents",
|
|
291
|
+
parameters: z.object({
|
|
292
|
+
path: z.string().describe("File path"),
|
|
293
|
+
}),
|
|
294
|
+
execute: async (toolCallId, params, signal, onUpdate, context) => {
|
|
295
|
+
const content = await fs.readFile(params.path, "utf-8");
|
|
296
|
+
|
|
297
|
+
// Optional: stream progress
|
|
298
|
+
onUpdate?.({ content: [{ type: "text", text: "Reading..." }], details: {} });
|
|
299
|
+
|
|
300
|
+
return {
|
|
301
|
+
content: [{ type: "text", text: content }],
|
|
302
|
+
details: { path: params.path, size: content.length },
|
|
303
|
+
};
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
agent.setTools([readFileTool]);
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Error Handling
|
|
311
|
+
|
|
312
|
+
**Throw an error** when a tool fails. Do not return error messages as content.
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
execute: async (toolCallId, params, signal, onUpdate) => {
|
|
316
|
+
if (!fs.existsSync(params.path)) {
|
|
317
|
+
throw new Error(`File not found: ${params.path}`);
|
|
318
|
+
}
|
|
319
|
+
// Return content only on success
|
|
320
|
+
return { content: [{ type: "text", text: "..." }] };
|
|
321
|
+
};
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Thrown errors are caught by the agent and reported to the LLM as tool errors with `isError: true`.
|
|
325
|
+
|
|
326
|
+
## Proxy Usage
|
|
327
|
+
|
|
328
|
+
For browser apps that proxy through a backend:
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
331
|
+
import { Agent, streamProxy } from "@gajae-code/agent-core";
|
|
332
|
+
|
|
333
|
+
const agent = new Agent({
|
|
334
|
+
streamFn: (model, context, options) =>
|
|
335
|
+
streamProxy(model, context, {
|
|
336
|
+
...options,
|
|
337
|
+
authToken: "...",
|
|
338
|
+
proxyUrl: "https://your-server.com",
|
|
339
|
+
}),
|
|
340
|
+
});
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## Low-Level API
|
|
344
|
+
|
|
345
|
+
For direct control without the Agent class:
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
import { agentLoop, agentLoopContinue } from "@gajae-code/agent-core";
|
|
349
|
+
|
|
350
|
+
const context: AgentContext = {
|
|
351
|
+
systemPrompt: ["You are helpful."],
|
|
352
|
+
messages: [],
|
|
353
|
+
tools: [],
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const config: AgentLoopConfig = {
|
|
357
|
+
model: getModel("openai", "gpt-4o"),
|
|
358
|
+
convertToLlm: (msgs) => msgs.filter((m) => ["user", "assistant", "toolResult"].includes(m.role)),
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const userMessage = { role: "user", content: "Hello", timestamp: Date.now() };
|
|
362
|
+
|
|
363
|
+
for await (const event of agentLoop([userMessage], context, config)) {
|
|
364
|
+
console.log(event.type);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Continue from existing context
|
|
368
|
+
for await (const event of agentLoopContinue(context, config)) {
|
|
369
|
+
console.log(event.type);
|
|
370
|
+
}
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## Run-level telemetry
|
|
374
|
+
Every `invoke_agent` produces two values alongside the OTEL spans:
|
|
375
|
+
|
|
376
|
+
- **`AgentRunSummary`** — chat / tool / usage / cost / error counters bucketed
|
|
377
|
+
by status, with per-tool-name breakdowns. Pure aggregation, safe to
|
|
378
|
+
persist, diff, or assert.
|
|
379
|
+
- **`AgentRunCoverage`** — sorted+deduped `toolsAvailable` / `toolsInvoked` /
|
|
380
|
+
`toolsUnused` / `modelsUsed` / `providersUsed` arrays. Stable for snapshot
|
|
381
|
+
tests.
|
|
382
|
+
|
|
383
|
+
Three delivery channels (use whichever fits):
|
|
384
|
+
|
|
385
|
+
### `agent_end` event (additive)
|
|
386
|
+
|
|
387
|
+
```typescript
|
|
388
|
+
for await (const event of agentLoop([userMessage], context, {
|
|
389
|
+
...config,
|
|
390
|
+
telemetry: {},
|
|
391
|
+
})) {
|
|
392
|
+
if (event.type === "agent_end" && event.telemetry) {
|
|
393
|
+
console.log("tokens:", event.telemetry.usage.totalTokens);
|
|
394
|
+
console.log("unused tools:", event.coverage?.toolsUnused);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
The `messages` field is unchanged. Consumers that ignore `telemetry`/
|
|
400
|
+
`coverage` continue to work.
|
|
401
|
+
|
|
402
|
+
### `onRunEnd` hook (non-fatal)
|
|
403
|
+
|
|
404
|
+
```typescript
|
|
405
|
+
const stream = agentLoop([userMessage], context, {
|
|
406
|
+
...config,
|
|
407
|
+
telemetry: {
|
|
408
|
+
onRunEnd: (summary, coverage) => {
|
|
409
|
+
await persistRunSummary(summary, coverage);
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
Exceptions thrown from `onRunEnd` are caught and logged via `console.warn`;
|
|
416
|
+
a misbehaving telemetry consumer can **never** turn a successful agent run
|
|
417
|
+
into a failed one.
|
|
418
|
+
|
|
419
|
+
### `agentLoopDetailed` (typed `detailed()` result)
|
|
420
|
+
|
|
421
|
+
Convenience wrapper that preserves the existing stream API and exposes the
|
|
422
|
+
rollup as a typed value:
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
const { stream, detailed } = agentLoopDetailed([userMessage], context, {
|
|
426
|
+
...config,
|
|
427
|
+
telemetry: {}, // required to populate telemetry/coverage
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
for await (const event of stream) {
|
|
431
|
+
// existing event handling
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const { messages, telemetry, coverage } = await detailed();
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
`stream.result()` still resolves to `AgentMessage[]` — no breaking change.
|
|
438
|
+
|
|
439
|
+
### Multi-run aggregation
|
|
440
|
+
|
|
441
|
+
Callers that drive the loop multiple times (verify pass, benchmark harness)
|
|
442
|
+
fold N summaries with `aggregateAgentRunSummaries` / `aggregateAgentRunCoverage`:
|
|
443
|
+
|
|
444
|
+
```typescript
|
|
445
|
+
import {
|
|
446
|
+
aggregateAgentRunSummaries,
|
|
447
|
+
aggregateAgentRunCoverage,
|
|
448
|
+
} from "@gajae-code/agent-core";
|
|
449
|
+
|
|
450
|
+
const summaries: AgentRunSummary[] = [];
|
|
451
|
+
const coverages: AgentRunCoverage[] = [];
|
|
452
|
+
for (const target of targets) {
|
|
453
|
+
const { detailed } = agentLoopDetailed(/* ... */);
|
|
454
|
+
const result = await detailed();
|
|
455
|
+
if (result.telemetry) summaries.push(result.telemetry);
|
|
456
|
+
if (result.coverage) coverages.push(result.coverage);
|
|
457
|
+
}
|
|
458
|
+
const runSummary = aggregateAgentRunSummaries(summaries);
|
|
459
|
+
const runCoverage = aggregateAgentRunCoverage(coverages);
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### Tool status reporting
|
|
463
|
+
|
|
464
|
+
`execute_tool` spans carry `pi.gen_ai.tool.status` ∈
|
|
465
|
+
`"ok" | "error" | "skipped" | "blocked" | "timeout" | "aborted"`.
|
|
466
|
+
`beforeToolCall` blocks throw a distinguishable `ToolCallBlockedError`
|
|
467
|
+
internally; the catch path reports `status: "blocked"` instead of conflating
|
|
468
|
+
with generic tool errors. Pre-run interrupts and tail-sweep skips are
|
|
469
|
+
recorded as `"skipped"` even though they never start a span.
|
|
470
|
+
|
|
471
|
+
## License
|
|
472
|
+
|
|
473
|
+
MIT
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent loop that works with AgentMessage throughout.
|
|
3
|
+
* Transforms to Message[] only at the LLM call boundary.
|
|
4
|
+
*/
|
|
5
|
+
import { type Context, EventStream } from "@gajae-code/ai";
|
|
6
|
+
import { type AgentRunCoverage, type AgentRunSummary } from "./run-collector";
|
|
7
|
+
import type { AgentContext, AgentEvent, AgentLoopConfig, AgentMessage, StreamFn } from "./types";
|
|
8
|
+
/**
|
|
9
|
+
* Start an agent loop with a new prompt message.
|
|
10
|
+
* The prompt is added to the context and events are emitted for it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function agentLoop(prompts: AgentMessage[], context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn): EventStream<AgentEvent, AgentMessage[]>;
|
|
13
|
+
/**
|
|
14
|
+
* Continue an agent loop from the current context without adding a new message.
|
|
15
|
+
* Used for retries - context already has user message or tool results.
|
|
16
|
+
*
|
|
17
|
+
* **Important:** The last message in context must convert to a `user` or `toolResult` message
|
|
18
|
+
* via `convertToLlm`. If it doesn't, the LLM provider will reject the request.
|
|
19
|
+
* This cannot be validated here since `convertToLlm` is only called once per turn.
|
|
20
|
+
*/
|
|
21
|
+
export declare function agentLoopContinue(context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn): EventStream<AgentEvent, AgentMessage[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Detailed-result handle returned by {@link agentLoopDetailed}. Adds the
|
|
24
|
+
* run-level telemetry/coverage rollup to the existing `AgentMessage[]`
|
|
25
|
+
* payload without changing the resolved type of `stream.result()`.
|
|
26
|
+
*/
|
|
27
|
+
export interface AgentLoopDetailedResult {
|
|
28
|
+
readonly messages: AgentMessage[];
|
|
29
|
+
readonly telemetry: AgentRunSummary | undefined;
|
|
30
|
+
readonly coverage: AgentRunCoverage | undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Convenience wrapper over {@link agentLoop} that exposes the run-level
|
|
34
|
+
* summary + coverage alongside the messages. The returned `stream` is the
|
|
35
|
+
* same `EventStream` callers already consume; `detailed()` awaits the
|
|
36
|
+
* stream's `agent_end` event and returns the additive fields.
|
|
37
|
+
*
|
|
38
|
+
* Existing `stream.result()` semantics are preserved — it still resolves to
|
|
39
|
+
* `AgentMessage[]`. Use {@link agentLoopDetailed} when you need the rollup;
|
|
40
|
+
* use {@link agentLoop} when you do not.
|
|
41
|
+
*/
|
|
42
|
+
export declare function agentLoopDetailed(prompts: AgentMessage[], context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn): {
|
|
43
|
+
readonly stream: EventStream<AgentEvent, AgentMessage[]>;
|
|
44
|
+
readonly detailed: () => Promise<AgentLoopDetailedResult>;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Like {@link agentLoopDetailed} but built on top of
|
|
48
|
+
* {@link agentLoopContinue}.
|
|
49
|
+
*/
|
|
50
|
+
export declare function agentLoopContinueDetailed(context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn): {
|
|
51
|
+
readonly stream: EventStream<AgentEvent, AgentMessage[]>;
|
|
52
|
+
readonly detailed: () => Promise<AgentLoopDetailedResult>;
|
|
53
|
+
};
|
|
54
|
+
export declare const INTENT_FIELD = "_i";
|
|
55
|
+
export declare function normalizeTools(tools: AgentContext["tools"], injectIntent: boolean): Context["tools"];
|