@greppleai/grip-agent-core 0.0.0-stage → 0.1.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.
Files changed (106) hide show
  1. package/README.md +489 -2
  2. package/dist/agent-loop.d.ts +24 -0
  3. package/dist/agent-loop.d.ts.map +1 -0
  4. package/dist/agent-loop.js +571 -0
  5. package/dist/agent-loop.js.map +1 -0
  6. package/dist/agent.d.ts +120 -0
  7. package/dist/agent.d.ts.map +1 -0
  8. package/dist/agent.js +414 -0
  9. package/dist/agent.js.map +1 -0
  10. package/dist/harness/agent-harness.d.ts +78 -0
  11. package/dist/harness/agent-harness.d.ts.map +1 -0
  12. package/dist/harness/agent-harness.js +894 -0
  13. package/dist/harness/agent-harness.js.map +1 -0
  14. package/dist/harness/compaction/branch-summarization.d.ts +48 -0
  15. package/dist/harness/compaction/branch-summarization.d.ts.map +1 -0
  16. package/dist/harness/compaction/branch-summarization.js +186 -0
  17. package/dist/harness/compaction/branch-summarization.js.map +1 -0
  18. package/dist/harness/compaction/compaction.d.ts +75 -0
  19. package/dist/harness/compaction/compaction.d.ts.map +1 -0
  20. package/dist/harness/compaction/compaction.js +505 -0
  21. package/dist/harness/compaction/compaction.js.map +1 -0
  22. package/dist/harness/compaction/utils.d.ts +26 -0
  23. package/dist/harness/compaction/utils.d.ts.map +1 -0
  24. package/dist/harness/compaction/utils.js +153 -0
  25. package/dist/harness/compaction/utils.js.map +1 -0
  26. package/dist/harness/env/nodejs.d.ts +36 -0
  27. package/dist/harness/env/nodejs.d.ts.map +1 -0
  28. package/dist/harness/env/nodejs.js +554 -0
  29. package/dist/harness/env/nodejs.js.map +1 -0
  30. package/dist/harness/messages.d.ts +51 -0
  31. package/dist/harness/messages.d.ts.map +1 -0
  32. package/dist/harness/messages.js +102 -0
  33. package/dist/harness/messages.js.map +1 -0
  34. package/dist/harness/prompt-templates.d.ts +56 -0
  35. package/dist/harness/prompt-templates.d.ts.map +1 -0
  36. package/dist/harness/prompt-templates.js +219 -0
  37. package/dist/harness/prompt-templates.js.map +1 -0
  38. package/dist/harness/session/jsonl-errors.d.ts +34 -0
  39. package/dist/harness/session/jsonl-errors.d.ts.map +1 -0
  40. package/dist/harness/session/jsonl-errors.js +35 -0
  41. package/dist/harness/session/jsonl-errors.js.map +1 -0
  42. package/dist/harness/session/jsonl-repo.d.ts +31 -0
  43. package/dist/harness/session/jsonl-repo.d.ts.map +1 -0
  44. package/dist/harness/session/jsonl-repo.js +199 -0
  45. package/dist/harness/session/jsonl-repo.js.map +1 -0
  46. package/dist/harness/session/jsonl-storage.d.ts +50 -0
  47. package/dist/harness/session/jsonl-storage.d.ts.map +1 -0
  48. package/dist/harness/session/jsonl-storage.js +696 -0
  49. package/dist/harness/session/jsonl-storage.js.map +1 -0
  50. package/dist/harness/session/memory-repo.d.ts +12 -0
  51. package/dist/harness/session/memory-repo.d.ts.map +1 -0
  52. package/dist/harness/session/memory-repo.js +42 -0
  53. package/dist/harness/session/memory-repo.js.map +1 -0
  54. package/dist/harness/session/memory-storage.d.ts +25 -0
  55. package/dist/harness/session/memory-storage.d.ts.map +1 -0
  56. package/dist/harness/session/memory-storage.js +116 -0
  57. package/dist/harness/session/memory-storage.js.map +1 -0
  58. package/dist/harness/session/repo-utils.d.ts +8 -0
  59. package/dist/harness/session/repo-utils.d.ts.map +1 -0
  60. package/dist/harness/session/repo-utils.js +39 -0
  61. package/dist/harness/session/repo-utils.js.map +1 -0
  62. package/dist/harness/session/session.d.ts +28 -0
  63. package/dist/harness/session/session.d.ts.map +1 -0
  64. package/dist/harness/session/session.js +210 -0
  65. package/dist/harness/session/session.js.map +1 -0
  66. package/dist/harness/session/uuid.d.ts +2 -0
  67. package/dist/harness/session/uuid.d.ts.map +1 -0
  68. package/dist/harness/session/uuid.js +50 -0
  69. package/dist/harness/session/uuid.js.map +1 -0
  70. package/dist/harness/skills.d.ts +54 -0
  71. package/dist/harness/skills.d.ts.map +1 -0
  72. package/dist/harness/skills.js +318 -0
  73. package/dist/harness/skills.js.map +1 -0
  74. package/dist/harness/system-prompt.d.ts +3 -0
  75. package/dist/harness/system-prompt.d.ts.map +1 -0
  76. package/dist/harness/system-prompt.js +30 -0
  77. package/dist/harness/system-prompt.js.map +1 -0
  78. package/dist/harness/types.d.ts +620 -0
  79. package/dist/harness/types.d.ts.map +1 -0
  80. package/dist/harness/types.js +94 -0
  81. package/dist/harness/types.js.map +1 -0
  82. package/dist/harness/utils/shell-output.d.ts +14 -0
  83. package/dist/harness/utils/shell-output.d.ts.map +1 -0
  84. package/dist/harness/utils/shell-output.js +135 -0
  85. package/dist/harness/utils/shell-output.js.map +1 -0
  86. package/dist/harness/utils/truncate.d.ts +73 -0
  87. package/dist/harness/utils/truncate.d.ts.map +1 -0
  88. package/dist/harness/utils/truncate.js +283 -0
  89. package/dist/harness/utils/truncate.js.map +1 -0
  90. package/dist/index.d.ts +23 -0
  91. package/dist/index.d.ts.map +1 -0
  92. package/dist/index.js +27 -0
  93. package/dist/index.js.map +1 -0
  94. package/dist/node.d.ts +3 -0
  95. package/dist/node.d.ts.map +1 -0
  96. package/dist/node.js +3 -0
  97. package/dist/node.js.map +1 -0
  98. package/dist/proxy.d.ts +69 -0
  99. package/dist/proxy.d.ts.map +1 -0
  100. package/dist/proxy.js +284 -0
  101. package/dist/proxy.js.map +1 -0
  102. package/dist/types.d.ts +402 -0
  103. package/dist/types.d.ts.map +1 -0
  104. package/dist/types.js +2 -0
  105. package/dist/types.js.map +1 -0
  106. package/package.json +59 -5
package/README.md CHANGED
@@ -1,3 +1,490 @@
1
- # Temporary Holding Version
1
+ # @greppleai/grip-agent-core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Stateful agent with tool execution and event streaming. Built on `@greppleai/grip-ai`.
4
+
5
+ ## Installation
6
+
7
+ When the new package is available in your registry:
8
+
9
+ ```bash
10
+ npm install --ignore-scripts @greppleai/grip-agent-core
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```typescript
16
+ import { Agent } from "@greppleai/grip-agent-core";
17
+ import { getModel } from "@greppleai/grip-ai";
18
+
19
+ const agent = new Agent({
20
+ initialState: {
21
+ systemPrompt: "You are a helpful assistant.",
22
+ model: getModel("anthropic", "claude-sonnet-4-20250514"),
23
+ },
24
+ });
25
+
26
+ agent.subscribe((event) => {
27
+ if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
28
+ // Stream just the new text chunk
29
+ process.stdout.write(event.assistantMessageEvent.delta);
30
+ }
31
+ });
32
+
33
+ await agent.prompt("Hello!");
34
+ ```
35
+
36
+ ## Core Concepts
37
+
38
+ ### AgentMessage vs LLM Message
39
+
40
+ The agent works with `AgentMessage`, a flexible type that can include:
41
+ - Standard LLM messages (`user`, `assistant`, `toolResult`)
42
+ - Custom app-specific message types via declaration merging
43
+
44
+ LLMs only understand `user`, `assistant`, and `toolResult`. The `convertToLlm` function bridges this gap by filtering and transforming messages before each LLM call.
45
+
46
+ ### Message Flow
47
+
48
+ ```
49
+ AgentMessage[] → transformContext() → AgentMessage[] → convertToLlm() → Message[] → LLM
50
+ (optional) (required)
51
+ ```
52
+
53
+ 1. **transformContext**: Prune old messages, inject external context
54
+ 2. **convertToLlm**: Filter out UI-only messages, convert custom types to LLM format
55
+
56
+ ## Event Flow
57
+
58
+ The agent emits events for UI updates. Understanding the event sequence helps build responsive interfaces.
59
+
60
+ ### prompt() Event Sequence
61
+
62
+ When you call `prompt("Hello")`:
63
+
64
+ ```
65
+ prompt("Hello")
66
+ ├─ agent_start
67
+ ├─ turn_start
68
+ ├─ message_start { message: userMessage } // Your prompt
69
+ ├─ message_end { message: userMessage }
70
+ ├─ message_start { message: assistantMessage } // LLM starts responding
71
+ ├─ message_update { message: partial... } // Streaming chunks
72
+ ├─ message_update { message: partial... }
73
+ ├─ message_end { message: assistantMessage } // Complete response
74
+ ├─ turn_end { message, toolResults: [] }
75
+ └─ agent_end { messages: [...] }
76
+ ```
77
+
78
+ ### With Tool Calls
79
+
80
+ If the assistant calls tools, the loop continues:
81
+
82
+ ```
83
+ prompt("Read config.json")
84
+ ├─ agent_start
85
+ ├─ turn_start
86
+ ├─ message_start/end { userMessage }
87
+ ├─ message_start { assistantMessage with toolCall }
88
+ ├─ message_update...
89
+ ├─ message_end { assistantMessage }
90
+ ├─ tool_execution_start { toolCallId, toolName, args }
91
+ ├─ tool_execution_update { partialResult } // If tool streams
92
+ ├─ tool_execution_end { toolCallId, result }
93
+ ├─ message_start/end { toolResultMessage }
94
+ ├─ turn_end { message, toolResults: [toolResult] }
95
+ │
96
+ ├─ turn_start // Next turn
97
+ ├─ message_start { assistantMessage } // LLM responds to tool result
98
+ ├─ message_update...
99
+ ├─ message_end
100
+ ├─ turn_end
101
+ └─ agent_end
102
+ ```
103
+
104
+ Tool execution mode is configurable:
105
+
106
+ - `parallel` (default): preflight tool calls sequentially, execute allowed tools concurrently, emit `tool_execution_end` as soon as each tool is finalized, then emit toolResult messages and `turn_end.toolResults` in assistant source order
107
+ - `sequential`: execute tool calls one by one, matching the historical behavior
108
+
109
+ In parallel mode, tool completion events follow tool completion order, but persisted toolResult messages still follow assistant source order.
110
+
111
+ The mode can be set globally via `toolExecution` in the agent config, or per-tool via `executionMode` on `AgentTool`. If any tool call in a batch targets a tool with `executionMode: "sequential"`, the entire batch executes sequentially regardless of the global setting.
112
+
113
+ The `beforeToolCall` hook runs after `tool_execution_start` and validated argument parsing. It can block execution and attach `terminate: true` to the blocked result. The `afterToolCall` hook runs after tool execution finishes and before `tool_execution_end` and final tool result message events are emitted.
114
+
115
+ Tools, blocked `beforeToolCall` results, and `afterToolCall` overrides can return `terminate: true` to hint that the automatic follow-up LLM call should be skipped. The loop only stops early when every finalized tool result in that batch sets `terminate: true`. Mixed batches continue normally.
116
+
117
+ Low-level loop callers can set `shouldStopAfterTurn` to stop gracefully after the current turn completes:
118
+
119
+ ```typescript
120
+ const stream = agentLoop(prompts, context, {
121
+ model,
122
+ convertToLlm,
123
+ shouldStopAfterTurn: async ({ message, toolResults, context, newMessages }) => {
124
+ return shouldCompactBeforeNextTurn(context.messages);
125
+ },
126
+ });
127
+ ```
128
+
129
+ `shouldStopAfterTurn` runs after `turn_end` is emitted and after the assistant response and any tool executions have completed normally. If it returns `true`, the loop emits `agent_end` and exits before polling steering or follow-up queues, and before starting another LLM call. It does not abort the provider stream, does not cancel running tools, and does not alter the assistant message stop reason.
130
+
131
+ 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.
132
+
133
+ ### continue() Event Sequence
134
+
135
+ `continue()` resumes from existing context without adding a new message. Use it for retries after errors.
136
+
137
+ ```typescript
138
+ // After an error, retry from current state
139
+ await agent.continue();
140
+ ```
141
+
142
+ The last message in context must be `user` or `toolResult` (not `assistant`).
143
+
144
+ ### Event Types
145
+
146
+ | Event | Description |
147
+ |-------|-------------|
148
+ | `agent_start` | Agent begins processing |
149
+ | `agent_end` | Final event for the run. Awaited subscribers for this event still count toward settlement |
150
+ | `turn_start` | New turn begins (one LLM call + tool executions) |
151
+ | `turn_end` | Turn completes with assistant message and tool results |
152
+ | `message_start` | Any message begins (user, assistant, toolResult) |
153
+ | `message_update` | **Assistant only.** Includes `assistantMessageEvent` with delta |
154
+ | `message_end` | Message completes |
155
+ | `tool_execution_start` | Tool begins |
156
+ | `tool_execution_update` | Tool streams progress |
157
+ | `tool_execution_end` | Tool completes |
158
+
159
+ `Agent.subscribe()` listeners are awaited in registration order. `agent_end` means no more loop events will be emitted, but `await agent.waitForIdle()` and `await agent.prompt(...)` only settle after awaited `agent_end` listeners finish.
160
+
161
+ ## Agent Options
162
+
163
+ ```typescript
164
+ const agent = new Agent({
165
+ // Initial state
166
+ initialState: {
167
+ systemPrompt: string,
168
+ model: Model<any>,
169
+ thinkingLevel: "off" | "minimal" | "low" | "medium" | "high" | "xhigh",
170
+ tools: AgentTool<any>[],
171
+ messages: AgentMessage[],
172
+ },
173
+
174
+ // Convert AgentMessage[] to LLM Message[] (required for custom message types)
175
+ convertToLlm: (messages) => messages.filter(...),
176
+
177
+ // Transform context before convertToLlm (for pruning, compaction)
178
+ transformContext: async (messages, signal) => pruneOldMessages(messages),
179
+
180
+ // Steering mode: "one-at-a-time" (default) or "all"
181
+ steeringMode: "one-at-a-time",
182
+
183
+ // Follow-up mode: "one-at-a-time" (default) or "all"
184
+ followUpMode: "one-at-a-time",
185
+
186
+ // Custom stream function (for proxy backends)
187
+ streamFn: streamProxy,
188
+
189
+ // Session ID for provider caching
190
+ sessionId: "session-123",
191
+
192
+ // Dynamic API key resolution (for expiring OAuth tokens)
193
+ getApiKey: async (provider) => refreshToken(),
194
+
195
+ // Tool execution mode: "parallel" (default) or "sequential"
196
+ toolExecution: "parallel",
197
+
198
+ // Preflight each tool call after args are validated. Can block execution.
199
+ beforeToolCall: async ({ toolCall, args, context }) => {
200
+ if (toolCall.name === "bash") {
201
+ return { block: true, reason: "bash is disabled", terminate: true };
202
+ }
203
+ },
204
+
205
+ // Postprocess each tool result before final tool events are emitted.
206
+ afterToolCall: async ({ toolCall, result, isError, context }) => {
207
+ if (toolCall.name === "notify_done" && !isError) {
208
+ return { terminate: true };
209
+ }
210
+ if (!isError) {
211
+ return { details: { ...result.details, audited: true } };
212
+ }
213
+ },
214
+
215
+ // Custom thinking budgets for token-based providers
216
+ thinkingBudgets: {
217
+ minimal: 128,
218
+ low: 512,
219
+ medium: 1024,
220
+ high: 2048,
221
+ },
222
+ });
223
+ ```
224
+
225
+ ## Agent State
226
+
227
+ ```typescript
228
+ interface AgentState {
229
+ systemPrompt: string;
230
+ model: Model<any>;
231
+ thinkingLevel: ThinkingLevel;
232
+ tools: AgentTool<any>[];
233
+ messages: AgentMessage[];
234
+ readonly isStreaming: boolean;
235
+ readonly streamingMessage?: AgentMessage;
236
+ readonly pendingToolCalls: ReadonlySet<string>;
237
+ readonly errorMessage?: string;
238
+ }
239
+ ```
240
+
241
+ Access state via `agent.state`.
242
+
243
+ Assigning `agent.state.tools = [...]` or `agent.state.messages = [...]` copies the top-level array before storing it. Mutating the returned array mutates the current agent state.
244
+
245
+ During streaming, `agent.state.streamingMessage` contains the current partial assistant message.
246
+
247
+ `agent.state.isStreaming` remains `true` until the run fully settles, including awaited `agent_end` subscribers.
248
+
249
+ ## Methods
250
+
251
+ ### Prompting
252
+
253
+ ```typescript
254
+ // Text prompt
255
+ await agent.prompt("Hello");
256
+
257
+ // With images
258
+ await agent.prompt("What's in this image?", [
259
+ { type: "image", data: base64Data, mimeType: "image/jpeg" }
260
+ ]);
261
+
262
+ // AgentMessage directly
263
+ await agent.prompt({ role: "user", content: "Hello", timestamp: Date.now() });
264
+
265
+ // Continue from current context (last message must be user or toolResult)
266
+ await agent.continue();
267
+ ```
268
+
269
+ ### State Management
270
+
271
+ ```typescript
272
+ agent.state.systemPrompt = "New prompt";
273
+ agent.state.model = getModel("openai", "gpt-4o");
274
+ agent.state.thinkingLevel = "medium";
275
+ agent.state.tools = [myTool];
276
+ agent.toolExecution = "sequential";
277
+ agent.beforeToolCall = async ({ toolCall }) => undefined;
278
+ agent.afterToolCall = async ({ toolCall, result }) => undefined;
279
+ agent.state.messages = newMessages; // top-level array is copied
280
+ agent.state.messages.push(message);
281
+ agent.reset();
282
+ ```
283
+
284
+ ### Session and Thinking Budgets
285
+
286
+ ```typescript
287
+ agent.sessionId = "session-123";
288
+
289
+ agent.thinkingBudgets = {
290
+ minimal: 128,
291
+ low: 512,
292
+ medium: 1024,
293
+ high: 2048,
294
+ };
295
+ ```
296
+
297
+ ### Control
298
+
299
+ ```typescript
300
+ agent.abort(); // Cancel current operation
301
+ await agent.waitForIdle(); // Wait for completion
302
+ ```
303
+
304
+ ### Events
305
+
306
+ ```typescript
307
+ const unsubscribe = agent.subscribe(async (event, signal) => {
308
+ if (event.type === "agent_end") {
309
+ // Final barrier work for the run
310
+ await flushSessionState(signal);
311
+ }
312
+ });
313
+ unsubscribe();
314
+ ```
315
+
316
+ ## Steering and Follow-up
317
+
318
+ Steering messages let you interrupt the agent while tools are running. Follow-up messages let you queue work after the agent would otherwise stop.
319
+
320
+ ```typescript
321
+ agent.steeringMode = "one-at-a-time";
322
+ agent.followUpMode = "one-at-a-time";
323
+
324
+ // While agent is running tools
325
+ agent.steer({
326
+ role: "user",
327
+ content: "Stop! Do this instead.",
328
+ timestamp: Date.now(),
329
+ });
330
+
331
+ // After the agent finishes its current work
332
+ agent.followUp({
333
+ role: "user",
334
+ content: "Also summarize the result.",
335
+ timestamp: Date.now(),
336
+ });
337
+
338
+ const steeringMode = agent.steeringMode;
339
+ const followUpMode = agent.followUpMode;
340
+
341
+ agent.clearSteeringQueue();
342
+ agent.clearFollowUpQueue();
343
+ agent.clearAllQueues();
344
+ ```
345
+
346
+ Use clearSteeringQueue, clearFollowUpQueue, or clearAllQueues to drop queued messages.
347
+
348
+ When steering messages are detected after a turn completes:
349
+ 1. All tool calls from the current assistant message have already finished
350
+ 2. Steering messages are injected
351
+ 3. The LLM responds on the next turn
352
+
353
+ 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.
354
+
355
+ ## Custom Message Types
356
+
357
+ Extend `AgentMessage` via declaration merging:
358
+
359
+ ```typescript
360
+ declare module "@greppleai/grip-agent-core" {
361
+ interface CustomAgentMessages {
362
+ notification: { role: "notification"; text: string; timestamp: number };
363
+ }
364
+ }
365
+
366
+ // Now valid
367
+ const msg: AgentMessage = { role: "notification", text: "Info", timestamp: Date.now() };
368
+ ```
369
+
370
+ Handle custom types in `convertToLlm`:
371
+
372
+ ```typescript
373
+ const agent = new Agent({
374
+ convertToLlm: (messages) => messages.flatMap(m => {
375
+ if (m.role === "notification") return []; // Filter out
376
+ return [m];
377
+ }),
378
+ });
379
+ ```
380
+
381
+ ## Tools
382
+
383
+ Define tools using `AgentTool`:
384
+
385
+ ```typescript
386
+ import { Type } from "typebox";
387
+
388
+ const readFileTool: AgentTool = {
389
+ name: "read_file",
390
+ label: "Read File", // For UI display
391
+ description: "Read a file's contents",
392
+ parameters: Type.Object({
393
+ path: Type.String({ description: "File path" }),
394
+ }),
395
+ // Override execution mode for this tool (optional).
396
+ // "sequential" forces the entire batch to run one at a time.
397
+ // "parallel" allows concurrent execution with other tool calls.
398
+ // If omitted, the global toolExecution config applies.
399
+ executionMode: "sequential",
400
+ execute: async (toolCallId, params, signal, onUpdate) => {
401
+ const content = await fs.readFile(params.path, "utf-8");
402
+
403
+ // Optional: stream progress
404
+ onUpdate?.({ content: [{ type: "text", text: "Reading..." }], details: {} });
405
+
406
+ // Optional: add `terminate: true` here to skip the automatic follow-up LLM call
407
+ // when every finalized tool result in the batch does the same.
408
+ return {
409
+ content: [{ type: "text", text: content }],
410
+ details: { path: params.path, size: content.length },
411
+ };
412
+ },
413
+ };
414
+
415
+ agent.state.tools = [readFileTool];
416
+ ```
417
+
418
+ ### Error Handling
419
+
420
+ **Throw an error** when a tool fails. Do not return error messages as content.
421
+
422
+ ```typescript
423
+ execute: async (toolCallId, params, signal, onUpdate) => {
424
+ if (!fs.existsSync(params.path)) {
425
+ throw new Error(`File not found: ${params.path}`);
426
+ }
427
+ // Return content only on success
428
+ return { content: [{ type: "text", text: "..." }] };
429
+ }
430
+ ```
431
+
432
+ Thrown errors are caught by the agent and reported to the LLM as tool errors with `isError: true`.
433
+
434
+ Return `terminate: true` from `execute()`, a blocked `beforeToolCall`, or `afterToolCall` to hint that the agent should stop after the current tool batch. This only takes effect when every finalized tool result in the batch is terminating. The hint is runtime-only; emitted `toolResult` transcript messages remain standard LLM tool results.
435
+
436
+ ## Proxy Usage
437
+
438
+ For browser apps that proxy through a backend:
439
+
440
+ ```typescript
441
+ import { Agent, streamProxy } from "@greppleai/grip-agent-core";
442
+
443
+ const agent = new Agent({
444
+ streamFn: (model, context, options) =>
445
+ streamProxy(model, context, {
446
+ ...options,
447
+ authToken: "...",
448
+ proxyUrl: "https://your-server.com",
449
+ }),
450
+ });
451
+ ```
452
+
453
+ ## Low-Level API
454
+
455
+ For direct control without the Agent class:
456
+
457
+ ```typescript
458
+ import { agentLoop, agentLoopContinue } from "@greppleai/grip-agent-core";
459
+
460
+ const context: AgentContext = {
461
+ systemPrompt: "You are helpful.",
462
+ messages: [],
463
+ tools: [],
464
+ };
465
+
466
+ const config: AgentLoopConfig = {
467
+ model: getModel("openai", "gpt-4o"),
468
+ convertToLlm: (msgs) => msgs.filter(m => ["user", "assistant", "toolResult"].includes(m.role)),
469
+ toolExecution: "parallel", // overridden by per-tool executionMode if set
470
+ beforeToolCall: async ({ toolCall, args, context }) => undefined,
471
+ afterToolCall: async ({ toolCall, result, isError, context }) => undefined,
472
+ };
473
+
474
+ const userMessage = { role: "user", content: "Hello", timestamp: Date.now() };
475
+
476
+ for await (const event of agentLoop([userMessage], context, config)) {
477
+ console.log(event.type);
478
+ }
479
+
480
+ // Continue from existing context
481
+ for await (const event of agentLoopContinue(context, config)) {
482
+ console.log(event.type);
483
+ }
484
+ ```
485
+
486
+ 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()`.
487
+
488
+ ## License
489
+
490
+ MIT
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Agent loop that works with AgentMessage throughout.
3
+ * Transforms to Message[] only at the LLM call boundary.
4
+ */
5
+ import { EventStream } from "@greppleai/grip-ai";
6
+ import type { AgentContext, AgentEvent, AgentLoopConfig, AgentMessage, StreamFn } from "./types.ts";
7
+ export type AgentEventSink = (event: AgentEvent) => Promise<void> | void;
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
+ export declare function runAgentLoop(prompts: AgentMessage[], context: AgentContext, config: AgentLoopConfig, emit: AgentEventSink, signal?: AbortSignal, streamFn?: StreamFn): Promise<AgentMessage[]>;
23
+ export declare function runAgentLoopContinue(context: AgentContext, config: AgentLoopConfig, emit: AgentEventSink, signal?: AbortSignal, streamFn?: StreamFn): Promise<AgentMessage[]>;
24
+ //# 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,EAIN,WAAW,EAIX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EACX,YAAY,EACZ,UAAU,EACV,eAAe,EAEf,YAAY,EAIZ,QAAQ,EACR,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,cAAc,GAAG,CAAC,KAAK,EAAE,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAEzE;;;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,CAUzC;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,CAkBzC;AAED,wBAAsB,YAAY,CACjC,OAAO,EAAE,YAAY,EAAE,EACvB,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,eAAe,EACvB,IAAI,EAAE,cAAc,EACpB,MAAM,CAAC,EAAE,WAAW,EACpB,QAAQ,CAAC,EAAE,QAAQ,GACjB,OAAO,CAAC,YAAY,EAAE,CAAC,CAgBzB;AAED,wBAAsB,oBAAoB,CACzC,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,eAAe,EACvB,IAAI,EAAE,cAAc,EACpB,MAAM,CAAC,EAAE,WAAW,EACpB,QAAQ,CAAC,EAAE,QAAQ,GACjB,OAAO,CAAC,YAAY,EAAE,CAAC,CAiBzB","sourcesContent":["/**\n * Agent loop that works with AgentMessage throughout.\n * Transforms to Message[] only at the LLM call boundary.\n */\n\nimport {\n\ttype AssistantMessage,\n\ttype AssistantMessageEventStream,\n\ttype Context,\n\tEventStream,\n\tstreamSimple,\n\ttype ToolResultMessage,\n\tvalidateToolArguments,\n} from \"@greppleai/grip-ai\";\nimport type {\n\tAgentContext,\n\tAgentEvent,\n\tAgentLoopConfig,\n\tAgentLoopTurnUpdate,\n\tAgentMessage,\n\tAgentTool,\n\tAgentToolCall,\n\tAgentToolResult,\n\tStreamFn,\n} from \"./types.ts\";\n\nexport type AgentEventSink = (event: AgentEvent) => Promise<void> | void;\n\n/**\n * Start an agent loop with a new prompt message.\n * The prompt is added to the context and events are emitted for it.\n */\nexport function agentLoop(\n\tprompts: AgentMessage[],\n\tcontext: AgentContext,\n\tconfig: AgentLoopConfig,\n\tsignal?: AbortSignal,\n\tstreamFn?: StreamFn,\n): EventStream<AgentEvent, AgentMessage[]> {\n\tconst stream = createAgentStream();\n\tconst state: DetachedAgentStreamState = { messages: [] };\n\n\tvoid runAgentLoop(prompts, context, config, createDetachedEventSink(stream, state), signal, streamFn).then(\n\t\t(messages) => stream.end(messages),\n\t\t(error: unknown) => terminateDetachedAgentStream(stream, state, config, error),\n\t);\n\n\treturn stream;\n}\n\n/**\n * Continue an agent loop from the current context without adding a new message.\n * Used for retries - context already has user message or tool results.\n *\n * **Important:** The last message in context must convert to a `user` or `toolResult` message\n * via `convertToLlm`. If it doesn't, the LLM provider will reject the request.\n * This cannot be validated here since `convertToLlm` is only called once per turn.\n */\nexport function agentLoopContinue(\n\tcontext: AgentContext,\n\tconfig: AgentLoopConfig,\n\tsignal?: AbortSignal,\n\tstreamFn?: StreamFn,\n): EventStream<AgentEvent, AgentMessage[]> {\n\tif (context.messages.length === 0) {\n\t\tthrow new Error(\"Cannot continue: no messages in context\");\n\t}\n\n\tif (context.messages[context.messages.length - 1].role === \"assistant\") {\n\t\tthrow new Error(\"Cannot continue from message role: assistant\");\n\t}\n\n\tconst stream = createAgentStream();\n\tconst state: DetachedAgentStreamState = { messages: [] };\n\n\tvoid runAgentLoopContinue(context, config, createDetachedEventSink(stream, state), signal, streamFn).then(\n\t\t(messages) => stream.end(messages),\n\t\t(error: unknown) => terminateDetachedAgentStream(stream, state, config, error),\n\t);\n\n\treturn stream;\n}\n\nexport async function runAgentLoop(\n\tprompts: AgentMessage[],\n\tcontext: AgentContext,\n\tconfig: AgentLoopConfig,\n\temit: AgentEventSink,\n\tsignal?: AbortSignal,\n\tstreamFn?: StreamFn,\n): Promise<AgentMessage[]> {\n\tconst newMessages: AgentMessage[] = [...prompts];\n\tconst currentContext: AgentContext = {\n\t\t...context,\n\t\tmessages: [...context.messages, ...prompts],\n\t};\n\n\tawait emit({ type: \"agent_start\" });\n\tawait emit({ type: \"turn_start\" });\n\tfor (const prompt of prompts) {\n\t\tawait emit({ type: \"message_start\", message: prompt });\n\t\tawait emit({ type: \"message_end\", message: prompt });\n\t}\n\n\tawait runLoop(currentContext, newMessages, config, signal, emit, streamFn);\n\treturn newMessages;\n}\n\nexport async function runAgentLoopContinue(\n\tcontext: AgentContext,\n\tconfig: AgentLoopConfig,\n\temit: AgentEventSink,\n\tsignal?: AbortSignal,\n\tstreamFn?: StreamFn,\n): Promise<AgentMessage[]> {\n\tif (context.messages.length === 0) {\n\t\tthrow new Error(\"Cannot continue: no messages in context\");\n\t}\n\n\tif (context.messages[context.messages.length - 1].role === \"assistant\") {\n\t\tthrow new Error(\"Cannot continue from message role: assistant\");\n\t}\n\n\tconst newMessages: AgentMessage[] = [];\n\tconst currentContext: AgentContext = { ...context, messages: [...context.messages] };\n\n\tawait emit({ type: \"agent_start\" });\n\tawait emit({ type: \"turn_start\" });\n\n\tawait runLoop(currentContext, newMessages, config, signal, emit, streamFn);\n\treturn newMessages;\n}\n\nfunction createAgentStream(): EventStream<AgentEvent, AgentMessage[]> {\n\treturn new EventStream<AgentEvent, AgentMessage[]>(\n\t\t(event: AgentEvent) => event.type === \"agent_end\",\n\t\t(event: AgentEvent) => (event.type === \"agent_end\" ? event.messages : []),\n\t);\n}\n\ninterface DetachedAgentStreamState {\n\tmessages: AgentMessage[];\n\tactiveAssistant?: AssistantMessage;\n\tturnOpen?: boolean;\n}\n\nfunction createDetachedEventSink(\n\tstream: EventStream<AgentEvent, AgentMessage[]>,\n\tstate: DetachedAgentStreamState,\n): AgentEventSink {\n\treturn (event) => {\n\t\tif (event.type === \"turn_start\") {\n\t\t\tstate.turnOpen = true;\n\t\t} else if (event.type === \"turn_end\") {\n\t\t\tstate.turnOpen = false;\n\t\t} else if (event.type === \"message_start\" && event.message.role === \"assistant\") {\n\t\t\tstate.activeAssistant = event.message;\n\t\t} else if (event.type === \"message_update\" && event.message.role === \"assistant\") {\n\t\t\tstate.activeAssistant = event.message;\n\t\t} else if (event.type === \"message_end\") {\n\t\t\tstate.messages.push(event.message);\n\t\t\tif (event.message.role === \"assistant\") state.activeAssistant = undefined;\n\t\t}\n\t\tstream.push(event);\n\t};\n}\n\nfunction terminateDetachedAgentStream(\n\tstream: EventStream<AgentEvent, AgentMessage[]>,\n\tstate: DetachedAgentStreamState,\n\tconfig: AgentLoopConfig,\n\terror: unknown,\n): void {\n\tconst errorMessage = error instanceof Error ? error.message : String(error);\n\tconst message: AssistantMessage = state.activeAssistant\n\t\t? { ...state.activeAssistant, stopReason: \"error\", errorMessage }\n\t\t: {\n\t\t\t\trole: \"assistant\",\n\t\t\t\tcontent: [],\n\t\t\t\tapi: config.model.api,\n\t\t\t\tprovider: config.model.provider,\n\t\t\t\tmodel: config.model.id,\n\t\t\t\tusage: {\n\t\t\t\t\tinput: 0,\n\t\t\t\t\toutput: 0,\n\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\ttotalTokens: 0,\n\t\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t\t},\n\t\t\t\tstopReason: \"error\",\n\t\t\t\terrorMessage,\n\t\t\t\ttimestamp: Date.now(),\n\t\t\t};\n\tif (!state.turnOpen) {\n\t\tstream.push({ type: \"turn_start\" });\n\t\tstate.turnOpen = true;\n\t}\n\tstate.messages.push(message);\n\tif (!state.activeAssistant) stream.push({ type: \"message_start\", message });\n\tstream.push({ type: \"message_end\", message });\n\tstream.push({ type: \"turn_end\", message, toolResults: [] });\n\tstate.turnOpen = false;\n\tstream.push({ type: \"agent_end\", messages: [...state.messages] });\n}\n\ninterface AgentLoopRuntimeState {\n\tcontext: AgentContext;\n\tconfig: AgentLoopConfig;\n}\n\ninterface AgentLoopExecution {\n\tnewMessages: AgentMessage[];\n\tsignal: AbortSignal | undefined;\n\temit: AgentEventSink;\n\tstreamFn: StreamFn | undefined;\n}\n\ninterface TurnSequenceResult {\n\tstate: AgentLoopRuntimeState;\n\tfirstTurn: boolean;\n\tterminated: boolean;\n}\n\ninterface AssistantTurnResult {\n\tterminated: boolean;\n\tmessage: AssistantMessage;\n\ttoolResults: ToolResultMessage[];\n\thasMoreToolCalls: boolean;\n}\n\nasync function emitSubsequentTurnStart(firstTurn: boolean, emit: AgentEventSink): Promise<void> {\n\tif (!firstTurn) await emit({ type: \"turn_start\" });\n}\n\nasync function injectPendingMessages(\n\tpendingMessages: AgentMessage[],\n\tcurrentContext: AgentContext,\n\tnewMessages: AgentMessage[],\n\temit: AgentEventSink,\n): Promise<void> {\n\tfor (const message of pendingMessages) {\n\t\tawait emit({ type: \"message_start\", message });\n\t\tawait emit({ type: \"message_end\", message });\n\t\tcurrentContext.messages.push(message);\n\t\tnewMessages.push(message);\n\t}\n}\n\nasync function executeAssistantTurn(\n\tcurrentContext: AgentContext,\n\tnewMessages: AgentMessage[],\n\tconfig: AgentLoopConfig,\n\tsignal: AbortSignal | undefined,\n\temit: AgentEventSink,\n\tstreamFn?: StreamFn,\n): Promise<AssistantTurnResult> {\n\tconst message = await streamAssistantResponse(currentContext, config, signal, emit, streamFn);\n\tnewMessages.push(message);\n\tif (message.stopReason === \"error\" || message.stopReason === \"aborted\") {\n\t\tawait emit({ type: \"turn_end\", message, toolResults: [] });\n\t\tawait emit({ type: \"agent_end\", messages: newMessages });\n\t\treturn { terminated: true, message, toolResults: [], hasMoreToolCalls: false };\n\t}\n\n\tconst toolCalls = message.content.filter((content) => content.type === \"toolCall\");\n\tconst toolResults: ToolResultMessage[] = [];\n\tlet hasMoreToolCalls = false;\n\tif (toolCalls.length > 0) {\n\t\tconst executedToolBatch = await executeToolCalls(currentContext, message, config, signal, emit);\n\t\ttoolResults.push(...executedToolBatch.messages);\n\t\thasMoreToolCalls = !executedToolBatch.terminate;\n\t\tfor (const result of toolResults) {\n\t\t\tcurrentContext.messages.push(result);\n\t\t\tnewMessages.push(result);\n\t\t}\n\t}\n\tawait emit({ type: \"turn_end\", message, toolResults });\n\treturn { terminated: false, message, toolResults, hasMoreToolCalls };\n}\n\nfunction applyNextTurnUpdate(\n\tcontext: AgentContext,\n\tconfig: AgentLoopConfig,\n\tupdate: AgentLoopTurnUpdate | undefined,\n): AgentLoopRuntimeState {\n\tif (!update) return { context, config };\n\tconst nextContext = update.context ? { ...update.context, messages: [...update.context.messages] } : context;\n\tconst reasoning =\n\t\tupdate.thinkingLevel === undefined\n\t\t\t? config.reasoning\n\t\t\t: update.thinkingLevel === \"off\"\n\t\t\t\t? undefined\n\t\t\t\t: update.thinkingLevel;\n\treturn {\n\t\tcontext: nextContext,\n\t\tconfig: { ...config, model: update.model ?? config.model, reasoning },\n\t};\n}\n\nasync function prepareFollowingTurn(\n\tturn: AssistantTurnResult,\n\tcontext: AgentContext,\n\tnewMessages: AgentMessage[],\n\tconfig: AgentLoopConfig,\n): Promise<AgentLoopRuntimeState> {\n\tconst update = await config.prepareNextTurn?.({\n\t\tmessage: turn.message,\n\t\ttoolResults: turn.toolResults,\n\t\tcontext,\n\t\tnewMessages,\n\t});\n\treturn applyNextTurnUpdate(context, config, update);\n}\n\nasync function shouldStopAfterCompletedTurn(\n\tturn: AssistantTurnResult,\n\tcontext: AgentContext,\n\tnewMessages: AgentMessage[],\n\tconfig: AgentLoopConfig,\n): Promise<boolean> {\n\treturn (\n\t\t(await config.shouldStopAfterTurn?.({\n\t\t\tmessage: turn.message,\n\t\t\ttoolResults: turn.toolResults,\n\t\t\tcontext,\n\t\t\tnewMessages,\n\t\t})) ?? false\n\t);\n}\n\nasync function runTurnSequence(\n\tinitialState: AgentLoopRuntimeState,\n\tinitialPendingMessages: AgentMessage[],\n\tfirstTurn: boolean,\n\texecution: AgentLoopExecution,\n): Promise<TurnSequenceResult> {\n\tlet state = initialState;\n\tlet pendingMessages = initialPendingMessages;\n\tlet hasMoreToolCalls = true;\n\twhile (hasMoreToolCalls || pendingMessages.length > 0) {\n\t\tawait emitSubsequentTurnStart(firstTurn, execution.emit);\n\t\tfirstTurn = false;\n\t\tawait injectPendingMessages(pendingMessages, state.context, execution.newMessages, execution.emit);\n\t\tpendingMessages = [];\n\n\t\tconst turn = await executeAssistantTurn(\n\t\t\tstate.context,\n\t\t\texecution.newMessages,\n\t\t\tstate.config,\n\t\t\texecution.signal,\n\t\t\texecution.emit,\n\t\t\texecution.streamFn,\n\t\t);\n\t\tif (turn.terminated) return { state, firstTurn, terminated: true };\n\t\thasMoreToolCalls = turn.hasMoreToolCalls;\n\t\tstate = await prepareFollowingTurn(turn, state.context, execution.newMessages, state.config);\n\t\tif (await shouldStopAfterCompletedTurn(turn, state.context, execution.newMessages, state.config)) {\n\t\t\tawait execution.emit({ type: \"agent_end\", messages: execution.newMessages });\n\t\t\treturn { state, firstTurn, terminated: true };\n\t\t}\n\t\tpendingMessages = (await state.config.getSteeringMessages?.()) || [];\n\t}\n\treturn { state, firstTurn, terminated: false };\n}\n\n/**\n * Main loop logic shared by agentLoop and agentLoopContinue.\n */\nasync function runLoop(\n\tinitialContext: AgentContext,\n\tnewMessages: AgentMessage[],\n\tinitialConfig: AgentLoopConfig,\n\tsignal: AbortSignal | undefined,\n\temit: AgentEventSink,\n\tstreamFn?: StreamFn,\n): Promise<void> {\n\tlet state: AgentLoopRuntimeState = { context: initialContext, config: initialConfig };\n\tlet firstTurn = true;\n\tlet pendingMessages: AgentMessage[] = (await state.config.getSteeringMessages?.()) || [];\n\tconst execution: AgentLoopExecution = { newMessages, signal, emit, streamFn };\n\n\twhile (true) {\n\t\tconst sequence = await runTurnSequence(state, pendingMessages, firstTurn, execution);\n\t\tif (sequence.terminated) return;\n\t\tstate = sequence.state;\n\t\tfirstTurn = sequence.firstTurn;\n\n\t\tconst followUpMessages = (await state.config.getFollowUpMessages?.()) || [];\n\t\tif (followUpMessages.length === 0) break;\n\t\tpendingMessages = followUpMessages;\n\t}\n\tawait emit({ type: \"agent_end\", messages: newMessages });\n}\n\nasync function finalizeAssistantResponse(\n\tresponse: AssistantMessageEventStream,\n\tcontext: AgentContext,\n\taddedPartial: boolean,\n\temit: AgentEventSink,\n): Promise<AssistantMessage> {\n\tconst finalMessage = await response.result();\n\tif (addedPartial) {\n\t\tcontext.messages[context.messages.length - 1] = finalMessage;\n\t} else {\n\t\tcontext.messages.push(finalMessage);\n\t\tawait emit({ type: \"message_start\", message: { ...finalMessage } });\n\t}\n\tawait emit({ type: \"message_end\", message: finalMessage });\n\treturn finalMessage;\n}\n\nasync function consumeAssistantResponse(\n\tresponse: AssistantMessageEventStream,\n\tcontext: AgentContext,\n\temit: AgentEventSink,\n): Promise<AssistantMessage> {\n\tlet partialMessage: AssistantMessage | null = null;\n\tlet addedPartial = false;\n\tfor await (const event of response) {\n\t\tswitch (event.type) {\n\t\t\tcase \"start\":\n\t\t\t\tpartialMessage = event.partial;\n\t\t\t\tcontext.messages.push(partialMessage);\n\t\t\t\taddedPartial = true;\n\t\t\t\tawait emit({ type: \"message_start\", message: { ...partialMessage } });\n\t\t\t\tbreak;\n\t\t\tcase \"text_start\":\n\t\t\tcase \"text_delta\":\n\t\t\tcase \"text_end\":\n\t\t\tcase \"thinking_start\":\n\t\t\tcase \"thinking_delta\":\n\t\t\tcase \"thinking_end\":\n\t\t\tcase \"toolcall_start\":\n\t\t\tcase \"toolcall_delta\":\n\t\t\tcase \"toolcall_end\":\n\t\t\t\tif (partialMessage) {\n\t\t\t\t\tpartialMessage = event.partial;\n\t\t\t\t\tcontext.messages[context.messages.length - 1] = partialMessage;\n\t\t\t\t\tawait emit({\n\t\t\t\t\t\ttype: \"message_update\",\n\t\t\t\t\t\tassistantMessageEvent: event,\n\t\t\t\t\t\tmessage: { ...partialMessage },\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase \"done\":\n\t\t\tcase \"error\":\n\t\t\t\treturn await finalizeAssistantResponse(response, context, addedPartial, emit);\n\t\t}\n\t}\n\treturn await finalizeAssistantResponse(response, context, addedPartial, emit);\n}\n\n/**\n * Stream an assistant response from the LLM.\n * This is where AgentMessage[] gets transformed to Message[] for the LLM.\n */\nasync function streamAssistantResponse(\n\tcontext: AgentContext,\n\tconfig: AgentLoopConfig,\n\tsignal: AbortSignal | undefined,\n\temit: AgentEventSink,\n\tstreamFn?: StreamFn,\n): Promise<AssistantMessage> {\n\t// Apply context transform if configured (AgentMessage[] → AgentMessage[])\n\tlet messages = context.messages;\n\tif (config.transformContext) {\n\t\tmessages = await config.transformContext(messages, signal);\n\t}\n\n\t// Convert to LLM-compatible messages (AgentMessage[] → Message[])\n\tconst llmMessages = await config.convertToLlm(messages);\n\n\t// Build LLM context\n\tconst llmContext: Context = {\n\t\tsystemPrompt: context.systemPrompt,\n\t\tmessages: llmMessages,\n\t\ttools: context.tools,\n\t};\n\n\tconst streamFunction = streamFn || streamSimple;\n\n\t// Resolve API key (important for expiring tokens)\n\tconst resolvedApiKey =\n\t\t(config.getApiKey ? await config.getApiKey(config.model.provider) : undefined) || config.apiKey;\n\n\tconst response = await streamFunction(config.model, llmContext, {\n\t\t...config,\n\t\tapiKey: resolvedApiKey,\n\t\tsignal,\n\t});\n\treturn await consumeAssistantResponse(response, context, emit);\n}\n\n/**\n * Execute tool calls from an assistant message.\n */\nasync function executeToolCalls(\n\tcurrentContext: AgentContext,\n\tassistantMessage: AssistantMessage,\n\tconfig: AgentLoopConfig,\n\tsignal: AbortSignal | undefined,\n\temit: AgentEventSink,\n): Promise<ExecutedToolCallBatch> {\n\tconst toolCalls = assistantMessage.content.filter((c) => c.type === \"toolCall\");\n\tconst toolsByName = new Map<string, AgentTool<any>>();\n\tfor (const tool of currentContext.tools ?? []) {\n\t\tif (!toolsByName.has(tool.name)) toolsByName.set(tool.name, tool);\n\t}\n\tconst hasSequentialToolCall = toolCalls.some(\n\t\t(toolCall) => toolsByName.get(toolCall.name)?.executionMode === \"sequential\",\n\t);\n\tif (config.toolExecution === \"sequential\" || hasSequentialToolCall) {\n\t\treturn executeToolCallsSequential(currentContext, assistantMessage, toolCalls, config, signal, emit);\n\t}\n\treturn executeToolCallsParallel(currentContext, assistantMessage, toolCalls, config, signal, emit);\n}\n\ntype ExecutedToolCallBatch = {\n\tmessages: ToolResultMessage[];\n\tterminate: boolean;\n};\n\nasync function executeToolCallsSequential(\n\tcurrentContext: AgentContext,\n\tassistantMessage: AssistantMessage,\n\ttoolCalls: AgentToolCall[],\n\tconfig: AgentLoopConfig,\n\tsignal: AbortSignal | undefined,\n\temit: AgentEventSink,\n): Promise<ExecutedToolCallBatch> {\n\tconst finalizedCalls: FinalizedToolCallOutcome[] = [];\n\tconst messages: ToolResultMessage[] = [];\n\n\tfor (const toolCall of toolCalls) {\n\t\tawait emit({\n\t\t\ttype: \"tool_execution_start\",\n\t\t\ttoolCallId: toolCall.id,\n\t\t\ttoolName: toolCall.name,\n\t\t\targs: toolCall.arguments,\n\t\t});\n\n\t\tconst preparation = await prepareToolCall(currentContext, assistantMessage, toolCall, config, signal);\n\t\tlet finalized: FinalizedToolCallOutcome;\n\t\tif (preparation.kind === \"immediate\") {\n\t\t\tfinalized = {\n\t\t\t\ttoolCall,\n\t\t\t\tresult: preparation.result,\n\t\t\t\tisError: preparation.isError,\n\t\t\t};\n\t\t} else {\n\t\t\tconst executed = await executePreparedToolCall(preparation, signal, emit);\n\t\t\tfinalized = await finalizeExecutedToolCall(\n\t\t\t\tcurrentContext,\n\t\t\t\tassistantMessage,\n\t\t\t\tpreparation,\n\t\t\t\texecuted,\n\t\t\t\tconfig,\n\t\t\t\tsignal,\n\t\t\t);\n\t\t}\n\n\t\tawait emitToolExecutionEnd(finalized, emit);\n\t\tconst toolResultMessage = createToolResultMessage(finalized);\n\t\tawait emitToolResultMessage(toolResultMessage, emit);\n\t\tfinalizedCalls.push(finalized);\n\t\tmessages.push(toolResultMessage);\n\n\t\tif (signal?.aborted) {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn {\n\t\tmessages,\n\t\tterminate: shouldTerminateToolBatch(finalizedCalls),\n\t};\n}\n\nasync function executeToolCallsParallel(\n\tcurrentContext: AgentContext,\n\tassistantMessage: AssistantMessage,\n\ttoolCalls: AgentToolCall[],\n\tconfig: AgentLoopConfig,\n\tsignal: AbortSignal | undefined,\n\temit: AgentEventSink,\n): Promise<ExecutedToolCallBatch> {\n\tconst finalizedCalls: FinalizedToolCallEntry[] = [];\n\n\tfor (const toolCall of toolCalls) {\n\t\tawait emit({\n\t\t\ttype: \"tool_execution_start\",\n\t\t\ttoolCallId: toolCall.id,\n\t\t\ttoolName: toolCall.name,\n\t\t\targs: toolCall.arguments,\n\t\t});\n\n\t\tconst preparation = await prepareToolCall(currentContext, assistantMessage, toolCall, config, signal);\n\t\tif (preparation.kind === \"immediate\") {\n\t\t\tconst finalized = {\n\t\t\t\ttoolCall,\n\t\t\t\tresult: preparation.result,\n\t\t\t\tisError: preparation.isError,\n\t\t\t} satisfies FinalizedToolCallOutcome;\n\t\t\tawait emitToolExecutionEnd(finalized, emit);\n\t\t\tfinalizedCalls.push(finalized);\n\t\t\tif (signal?.aborted) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tfinalizedCalls.push(async () => {\n\t\t\tconst executed = await executePreparedToolCall(preparation, signal, emit);\n\t\t\tconst finalized = await finalizeExecutedToolCall(\n\t\t\t\tcurrentContext,\n\t\t\t\tassistantMessage,\n\t\t\t\tpreparation,\n\t\t\t\texecuted,\n\t\t\t\tconfig,\n\t\t\t\tsignal,\n\t\t\t);\n\t\t\tawait emitToolExecutionEnd(finalized, emit);\n\t\t\treturn finalized;\n\t\t});\n\t\tif (signal?.aborted) {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tconst orderedFinalizedCalls = await Promise.all(\n\t\tfinalizedCalls.map((entry) => (typeof entry === \"function\" ? entry() : Promise.resolve(entry))),\n\t);\n\tconst messages: ToolResultMessage[] = [];\n\tfor (const finalized of orderedFinalizedCalls) {\n\t\tconst toolResultMessage = createToolResultMessage(finalized);\n\t\tawait emitToolResultMessage(toolResultMessage, emit);\n\t\tmessages.push(toolResultMessage);\n\t}\n\n\treturn {\n\t\tmessages,\n\t\tterminate: shouldTerminateToolBatch(orderedFinalizedCalls),\n\t};\n}\n\ntype PreparedToolCall = {\n\tkind: \"prepared\";\n\ttoolCall: AgentToolCall;\n\ttool: AgentTool<any>;\n\targs: unknown;\n};\n\ntype ImmediateToolCallOutcome = {\n\tkind: \"immediate\";\n\tresult: AgentToolResult<any>;\n\tisError: boolean;\n};\n\ntype ExecutedToolCallOutcome = {\n\tresult: AgentToolResult<any>;\n\tisError: boolean;\n};\n\ntype FinalizedToolCallOutcome = {\n\ttoolCall: AgentToolCall;\n\tresult: AgentToolResult<any>;\n\tisError: boolean;\n};\n\ntype FinalizedToolCallEntry = FinalizedToolCallOutcome | (() => Promise<FinalizedToolCallOutcome>);\n\nfunction shouldTerminateToolBatch(finalizedCalls: FinalizedToolCallOutcome[]): boolean {\n\treturn finalizedCalls.length > 0 && finalizedCalls.every((finalized) => finalized.result.terminate === true);\n}\n\nfunction prepareToolCallArguments(tool: AgentTool<any>, toolCall: AgentToolCall): AgentToolCall {\n\tif (!tool.prepareArguments) {\n\t\treturn toolCall;\n\t}\n\tconst preparedArguments = tool.prepareArguments(toolCall.arguments);\n\tif (preparedArguments === toolCall.arguments) {\n\t\treturn toolCall;\n\t}\n\treturn {\n\t\t...toolCall,\n\t\targuments: preparedArguments as Record<string, any>,\n\t};\n}\n\nasync function prepareToolCall(\n\tcurrentContext: AgentContext,\n\tassistantMessage: AssistantMessage,\n\ttoolCall: AgentToolCall,\n\tconfig: AgentLoopConfig,\n\tsignal: AbortSignal | undefined,\n): Promise<PreparedToolCall | ImmediateToolCallOutcome> {\n\tconst tool = currentContext.tools?.find((t) => t.name === toolCall.name);\n\tif (!tool) {\n\t\treturn {\n\t\t\tkind: \"immediate\",\n\t\t\tresult: createErrorToolResult(`Tool ${toolCall.name} not found`),\n\t\t\tisError: true,\n\t\t};\n\t}\n\n\ttry {\n\t\tconst preparedToolCall = prepareToolCallArguments(tool, toolCall);\n\t\tconst validatedArgs = validateToolArguments(tool, preparedToolCall);\n\t\tif (config.beforeToolCall) {\n\t\t\tconst beforeResult = await config.beforeToolCall(\n\t\t\t\t{\n\t\t\t\t\tassistantMessage,\n\t\t\t\t\ttoolCall,\n\t\t\t\t\targs: validatedArgs,\n\t\t\t\t\tcontext: currentContext,\n\t\t\t\t},\n\t\t\t\tsignal,\n\t\t\t);\n\t\t\tif (signal?.aborted) {\n\t\t\t\treturn {\n\t\t\t\t\tkind: \"immediate\",\n\t\t\t\t\tresult: createErrorToolResult(\"Operation aborted\"),\n\t\t\t\t\tisError: true,\n\t\t\t\t};\n\t\t\t}\n\t\t\tif (beforeResult?.block) {\n\t\t\t\tconst result = createErrorToolResult(beforeResult.reason || \"Tool execution was blocked\");\n\t\t\t\tif (beforeResult.terminate === true) {\n\t\t\t\t\tresult.terminate = true;\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tkind: \"immediate\",\n\t\t\t\t\tresult,\n\t\t\t\t\tisError: true,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\tif (signal?.aborted) {\n\t\t\treturn {\n\t\t\t\tkind: \"immediate\",\n\t\t\t\tresult: createErrorToolResult(\"Operation aborted\"),\n\t\t\t\tisError: true,\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\tkind: \"prepared\",\n\t\t\ttoolCall,\n\t\t\ttool,\n\t\t\targs: validatedArgs,\n\t\t};\n\t} catch (error) {\n\t\treturn {\n\t\t\tkind: \"immediate\",\n\t\t\tresult: createErrorToolResult(error instanceof Error ? error.message : String(error)),\n\t\t\tisError: true,\n\t\t};\n\t}\n}\n\nasync function executePreparedToolCall(\n\tprepared: PreparedToolCall,\n\tsignal: AbortSignal | undefined,\n\temit: AgentEventSink,\n): Promise<ExecutedToolCallOutcome> {\n\tlet updateEvents = Promise.resolve();\n\n\ttry {\n\t\tconst result = await prepared.tool.execute(\n\t\t\tprepared.toolCall.id,\n\t\t\tprepared.args as never,\n\t\t\tsignal,\n\t\t\t(partialResult) => {\n\t\t\t\tupdateEvents = updateEvents.then(() =>\n\t\t\t\t\temit({\n\t\t\t\t\t\ttype: \"tool_execution_update\",\n\t\t\t\t\t\ttoolCallId: prepared.toolCall.id,\n\t\t\t\t\t\ttoolName: prepared.toolCall.name,\n\t\t\t\t\t\targs: prepared.toolCall.arguments,\n\t\t\t\t\t\tpartialResult,\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t},\n\t\t);\n\t\tawait updateEvents;\n\t\treturn { result, isError: false };\n\t} catch (error) {\n\t\tawait updateEvents;\n\t\treturn {\n\t\t\tresult: createErrorToolResult(error instanceof Error ? error.message : String(error)),\n\t\t\tisError: true,\n\t\t};\n\t}\n}\n\nasync function finalizeExecutedToolCall(\n\tcurrentContext: AgentContext,\n\tassistantMessage: AssistantMessage,\n\tprepared: PreparedToolCall,\n\texecuted: ExecutedToolCallOutcome,\n\tconfig: AgentLoopConfig,\n\tsignal: AbortSignal | undefined,\n): Promise<FinalizedToolCallOutcome> {\n\tlet result = executed.result;\n\tlet isError = executed.isError;\n\n\tif (config.afterToolCall) {\n\t\ttry {\n\t\t\tconst afterResult = await config.afterToolCall(\n\t\t\t\t{\n\t\t\t\t\tassistantMessage,\n\t\t\t\t\ttoolCall: prepared.toolCall,\n\t\t\t\t\targs: prepared.args,\n\t\t\t\t\tresult,\n\t\t\t\t\tisError,\n\t\t\t\t\tcontext: currentContext,\n\t\t\t\t},\n\t\t\t\tsignal,\n\t\t\t);\n\t\t\tif (afterResult) {\n\t\t\t\tresult = {\n\t\t\t\t\tcontent: afterResult.content ?? result.content,\n\t\t\t\t\tdetails: afterResult.details ?? result.details,\n\t\t\t\t\tterminate: afterResult.terminate ?? result.terminate,\n\t\t\t\t};\n\t\t\t\tisError = afterResult.isError ?? isError;\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tresult = createErrorToolResult(error instanceof Error ? error.message : String(error));\n\t\t\tisError = true;\n\t\t}\n\t}\n\n\treturn {\n\t\ttoolCall: prepared.toolCall,\n\t\tresult,\n\t\tisError,\n\t};\n}\n\nfunction createErrorToolResult(message: string): AgentToolResult<any> {\n\treturn {\n\t\tcontent: [{ type: \"text\", text: message }],\n\t\tdetails: {},\n\t};\n}\n\nasync function emitToolExecutionEnd(finalized: FinalizedToolCallOutcome, emit: AgentEventSink): Promise<void> {\n\tawait emit({\n\t\ttype: \"tool_execution_end\",\n\t\ttoolCallId: finalized.toolCall.id,\n\t\ttoolName: finalized.toolCall.name,\n\t\tresult: finalized.result,\n\t\tisError: finalized.isError,\n\t});\n}\n\nfunction createToolResultMessage(finalized: FinalizedToolCallOutcome): ToolResultMessage {\n\treturn {\n\t\trole: \"toolResult\",\n\t\ttoolCallId: finalized.toolCall.id,\n\t\ttoolName: finalized.toolCall.name,\n\t\t// Untyped tools can omit content; never let nullish values enter history or provider payloads.\n\t\tcontent: finalized.result.content ?? [],\n\t\tdetails: finalized.result.details,\n\t\tisError: finalized.isError,\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nasync function emitToolResultMessage(toolResultMessage: ToolResultMessage, emit: AgentEventSink): Promise<void> {\n\tawait emit({ type: \"message_start\", message: toolResultMessage });\n\tawait emit({ type: \"message_end\", message: toolResultMessage });\n}\n"]}