@threadwell/agent-core 0.0.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.
@@ -0,0 +1,443 @@
1
+ import type { AssistantMessage, AssistantMessageEvent, ImageContent, Message, Model, SimpleStreamOptions, streamSimple, TextContent, Tool, ToolResultMessage } from "@threadwell/ai";
2
+ import type { Static, TSchema } from "typebox";
3
+ /**
4
+ * Stream function used by the agent loop.
5
+ *
6
+ * Contract:
7
+ * - Must not throw or return a rejected promise for request/model/runtime failures.
8
+ * - Must return an AssistantMessageEventStream.
9
+ * - Failures must be encoded in the returned stream via protocol events and a
10
+ * final AssistantMessage with stopReason "error" or "aborted" and errorMessage.
11
+ */
12
+ export type StreamFn = (...args: Parameters<typeof streamSimple>) => ReturnType<typeof streamSimple> | Promise<ReturnType<typeof streamSimple>>;
13
+ /**
14
+ * Configuration for how tool calls from a single assistant message are executed.
15
+ *
16
+ * - "sequential": each tool call is prepared, executed, and finalized before the next one starts.
17
+ * - "parallel": tool calls are prepared sequentially, then allowed tools execute concurrently.
18
+ * `tool_execution_end` is emitted in tool completion order after each tool is finalized,
19
+ * while tool-result message artifacts are emitted later in assistant source order.
20
+ */
21
+ export type ToolExecutionMode = "sequential" | "parallel";
22
+ /** A single tool call content block emitted by an assistant message. */
23
+ export type AgentToolCall = Extract<AssistantMessage["content"][number], {
24
+ type: "toolCall";
25
+ }>;
26
+ /**
27
+ * Result returned from `beforeToolCall`.
28
+ *
29
+ * Returning `{ block: true }` prevents the tool from executing. The loop emits an error tool result instead.
30
+ * `reason` becomes the text shown in that error result. If omitted, a default blocked message is used.
31
+ */
32
+ export interface BeforeToolCallResult {
33
+ block?: boolean;
34
+ reason?: string;
35
+ }
36
+ /**
37
+ * Partial override returned from `afterToolCall`.
38
+ *
39
+ * Merge semantics are field-by-field:
40
+ * - `content`: if provided, replaces the tool result content array in full
41
+ * - `details`: if provided, replaces the tool result details value in full
42
+ * - `isError`: if provided, replaces the tool result error flag
43
+ * - `terminate`: if provided, replaces the early-termination hint
44
+ *
45
+ * Omitted fields keep the original executed tool result values.
46
+ * There is no deep merge for `content` or `details`.
47
+ */
48
+ export interface AfterToolCallResult {
49
+ content?: (TextContent | ImageContent)[];
50
+ details?: unknown;
51
+ isError?: boolean;
52
+ /**
53
+ * Hint that the agent should stop after the current tool batch.
54
+ * Early termination only happens when every finalized tool result in the batch sets this to true.
55
+ */
56
+ terminate?: boolean;
57
+ }
58
+ /** Context passed to `beforeToolCall`. */
59
+ export interface BeforeToolCallContext {
60
+ /** The assistant message that requested the tool call. */
61
+ assistantMessage: AssistantMessage;
62
+ /** The raw tool call block from `assistantMessage.content`. */
63
+ toolCall: AgentToolCall;
64
+ /** Validated tool arguments for the target tool schema. */
65
+ args: unknown;
66
+ /** Current agent context at the time the tool call is prepared. */
67
+ context: AgentContext;
68
+ }
69
+ /** Context passed to `afterToolCall`. */
70
+ export interface AfterToolCallContext {
71
+ /** The assistant message that requested the tool call. */
72
+ assistantMessage: AssistantMessage;
73
+ /** The raw tool call block from `assistantMessage.content`. */
74
+ toolCall: AgentToolCall;
75
+ /** Validated tool arguments for the target tool schema. */
76
+ args: unknown;
77
+ /** The executed tool result before any `afterToolCall` overrides are applied. */
78
+ result: AgentToolResult<any>;
79
+ /** Whether the executed tool result is currently treated as an error. */
80
+ isError: boolean;
81
+ /** Current agent context at the time the tool call is finalized. */
82
+ context: AgentContext;
83
+ }
84
+ /** Context passed to `shouldStopAfterTurn`. */
85
+ export interface ShouldStopAfterTurnContext {
86
+ /** The assistant message that completed the turn. */
87
+ message: AssistantMessage;
88
+ /** Tool result messages passed to the preceding `turn_end` event. */
89
+ toolResults: ToolResultMessage[];
90
+ /** Current agent context after the turn's assistant message and tool results have been appended. */
91
+ context: AgentContext;
92
+ /** Messages that this loop invocation will return if it exits at this point. Prompt runs include the initial prompt messages; continuation runs do not include pre-existing context messages. */
93
+ newMessages: AgentMessage[];
94
+ }
95
+ /** Common context passed to agent loop lifecycle hooks. */
96
+ export interface AgentLoopLifecycleBaseContext {
97
+ /** Current mutable loop context. Hook implementations should treat it as read-only unless explicitly coordinating with the loop owner. */
98
+ context: AgentContext;
99
+ /** Messages produced by this loop invocation so far. */
100
+ newMessages: readonly AgentMessage[];
101
+ /** Active abort signal for the loop invocation. */
102
+ signal?: AbortSignal;
103
+ }
104
+ /** Context passed before the next model request is created. */
105
+ export type BeforeModelCallContext = AgentLoopLifecycleBaseContext;
106
+ /** Context passed after a final assistant message is available. */
107
+ export interface AfterModelCallContext extends AgentLoopLifecycleBaseContext {
108
+ /** Final assistant message returned by the model. */
109
+ message: AssistantMessage;
110
+ }
111
+ /** Context passed after a batch of tool calls has completed and tool-result messages have been appended. */
112
+ export interface AfterToolBatchContext extends AgentLoopLifecycleBaseContext {
113
+ /** Assistant message that requested the tool calls. */
114
+ message: AssistantMessage;
115
+ /** Tool-result messages generated by the completed batch. */
116
+ toolResults: readonly ToolResultMessage[];
117
+ /** Whether the loop will continue because at least one tool result did not request termination. */
118
+ hasMoreToolCalls: boolean;
119
+ }
120
+ /** Context passed after turn_end has been emitted. */
121
+ export interface AfterTurnEndContext extends AgentLoopLifecycleBaseContext {
122
+ /** Assistant message that completed the turn. */
123
+ message: AssistantMessage;
124
+ /** Tool-result messages passed to turn_end. */
125
+ toolResults: readonly ToolResultMessage[];
126
+ }
127
+ /** Reason the loop is about to emit agent_end. */
128
+ export type AgentLoopEndReason = "stop" | "error" | "aborted" | "shouldStop";
129
+ /** Context passed immediately before agent_end is emitted. */
130
+ export interface BeforeLoopEndContext extends AgentLoopLifecycleBaseContext {
131
+ /** Why the loop is ending. */
132
+ reason: AgentLoopEndReason;
133
+ /** Last assistant message, when the loop reached one. */
134
+ message?: AssistantMessage;
135
+ /** Tool results from the last completed turn, when available. */
136
+ toolResults?: readonly ToolResultMessage[];
137
+ }
138
+ export interface AgentContextProviderContext extends AgentLoopLifecycleBaseContext {
139
+ }
140
+ /** Provides additional model-call context without mutating the persisted transcript. */
141
+ export type AgentContextProvider = (context: AgentContextProviderContext) => AgentMessage[] | undefined | Promise<AgentMessage[] | undefined>;
142
+ /** Optional lifecycle hooks for low-level agent loop observers. */
143
+ export interface AgentLoopLifecycleHooks {
144
+ /** Called after pending messages are appended and before the next model request is prepared. */
145
+ beforeModelCall?: (context: BeforeModelCallContext) => Promise<void> | void;
146
+ /** Called after the final assistant message for a model call is available. */
147
+ afterModelCall?: (context: AfterModelCallContext) => Promise<void> | void;
148
+ /** Called after tool execution and after tool-result messages have been appended. */
149
+ afterToolBatch?: (context: AfterToolBatchContext) => Promise<void> | void;
150
+ /** Called after turn_end has been emitted. */
151
+ afterTurnEnd?: (context: AfterTurnEndContext) => Promise<void> | void;
152
+ /** Called immediately before agent_end is emitted. */
153
+ beforeLoopEnd?: (context: BeforeLoopEndContext) => Promise<void> | void;
154
+ }
155
+ export interface AgentLoopConfig extends SimpleStreamOptions {
156
+ model: Model<any>;
157
+ /**
158
+ * Optional low-level lifecycle hooks.
159
+ *
160
+ * Hooks are observational by contract: they must not throw and should not mutate transcript arrays.
161
+ * Use existing queue and context-transform APIs for message injection.
162
+ */
163
+ lifecycleHooks?: AgentLoopLifecycleHooks;
164
+ /**
165
+ * Optional providers for ephemeral model-call context.
166
+ *
167
+ * Provider messages are prepended to the transformed context before `convertToLlm` and are not
168
+ * written to the persisted transcript. Providers run in array order.
169
+ */
170
+ contextProviders?: AgentContextProvider[];
171
+ /**
172
+ * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
173
+ *
174
+ * Each AgentMessage must be converted to a UserMessage, AssistantMessage, or ToolResultMessage
175
+ * that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,
176
+ * status messages) should be filtered out.
177
+ *
178
+ * Contract: must not throw or reject. Return a safe fallback value instead.
179
+ * Throwing interrupts the low-level agent loop without producing a normal event sequence.
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * convertToLlm: (messages) => messages.flatMap(m => {
184
+ * if (m.role === "custom") {
185
+ * // Convert custom message to user message
186
+ * return [{ role: "user", content: m.content, timestamp: m.timestamp }];
187
+ * }
188
+ * if (m.role === "notification") {
189
+ * // Filter out UI-only messages
190
+ * return [];
191
+ * }
192
+ * // Pass through standard LLM messages
193
+ * return [m];
194
+ * })
195
+ * ```
196
+ */
197
+ convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
198
+ /**
199
+ * Optional transform applied to the context before `convertToLlm`.
200
+ *
201
+ * Use this for operations that work at the AgentMessage level:
202
+ * - Context window management (pruning old messages)
203
+ * - Injecting context from external sources
204
+ *
205
+ * Contract: must not throw or reject. Return the original messages or another
206
+ * safe fallback value instead.
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * transformContext: async (messages) => {
211
+ * if (estimateTokens(messages) > MAX_TOKENS) {
212
+ * return pruneOldMessages(messages);
213
+ * }
214
+ * return messages;
215
+ * }
216
+ * ```
217
+ */
218
+ transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
219
+ /**
220
+ * Resolves an API key dynamically for each LLM call.
221
+ *
222
+ * Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire
223
+ * during long-running tool execution phases.
224
+ *
225
+ * Contract: must not throw or reject. Return undefined when no key is available.
226
+ */
227
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
228
+ /**
229
+ * Called after each turn fully completes and `turn_end` has been emitted.
230
+ *
231
+ * If it returns true, the loop emits `agent_end` and exits before polling steering or follow-up queues,
232
+ * without starting another LLM call. The current assistant response and any tool executions finish normally.
233
+ *
234
+ * Use this to request a graceful stop after the current turn, e.g. before context gets too full.
235
+ *
236
+ * Contract: must not throw or reject. Throwing interrupts the low-level agent loop without producing a normal event sequence.
237
+ */
238
+ shouldStopAfterTurn?: (context: ShouldStopAfterTurnContext) => boolean | Promise<boolean>;
239
+ /**
240
+ * Returns steering messages to inject into the conversation mid-run.
241
+ *
242
+ * Called after the current assistant turn finishes executing its tool calls, unless `shouldStopAfterTurn` exits first.
243
+ * If messages are returned, they are added to the context before the next LLM call.
244
+ * Tool calls from the current assistant message are not skipped.
245
+ *
246
+ * Use this for "steering" the agent while it's working.
247
+ *
248
+ * Contract: must not throw or reject. Return [] when no steering messages are available.
249
+ */
250
+ getSteeringMessages?: () => Promise<AgentMessage[]>;
251
+ /**
252
+ * Returns follow-up messages to process after the agent would otherwise stop.
253
+ *
254
+ * Called when the agent has no more tool calls and no steering messages.
255
+ * If messages are returned, they're added to the context and the agent
256
+ * continues with another turn.
257
+ *
258
+ * Use this for follow-up messages that should wait until the agent finishes.
259
+ *
260
+ * Contract: must not throw or reject. Return [] when no follow-up messages are available.
261
+ */
262
+ getFollowUpMessages?: () => Promise<AgentMessage[]>;
263
+ /**
264
+ * Tool execution mode.
265
+ * - "sequential": execute tool calls one by one
266
+ * - "parallel": preflight tool calls sequentially, then execute allowed tools concurrently;
267
+ * emit `tool_execution_end` in tool completion order after each tool is finalized,
268
+ * then emit tool-result message artifacts later in assistant source order
269
+ *
270
+ * Default: "parallel"
271
+ */
272
+ toolExecution?: ToolExecutionMode;
273
+ /**
274
+ * Called before a tool is executed, after arguments have been validated.
275
+ *
276
+ * Return `{ block: true }` to prevent execution. The loop emits an error tool result instead.
277
+ * The hook receives the agent abort signal and is responsible for honoring it.
278
+ */
279
+ beforeToolCall?: (context: BeforeToolCallContext, signal?: AbortSignal) => Promise<BeforeToolCallResult | undefined>;
280
+ /**
281
+ * Called after a tool finishes executing, before `tool_execution_end` and tool-result message events are emitted.
282
+ *
283
+ * Return an `AfterToolCallResult` to override parts of the executed tool result:
284
+ * - `content` replaces the full content array
285
+ * - `details` replaces the full details payload
286
+ * - `isError` replaces the error flag
287
+ * - `terminate` replaces the early-termination hint
288
+ *
289
+ * Any omitted fields keep their original values. No deep merge is performed.
290
+ * The hook receives the agent abort signal and is responsible for honoring it.
291
+ */
292
+ afterToolCall?: (context: AfterToolCallContext, signal?: AbortSignal) => Promise<AfterToolCallResult | undefined>;
293
+ }
294
+ /**
295
+ * Thinking/reasoning level for models that support it.
296
+ * Note: "xhigh" is only supported by selected model families. Use model thinking-level metadata
297
+ * from @threadwell/ai to detect support for a concrete model.
298
+ */
299
+ export type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh";
300
+ /**
301
+ * Extensible interface for custom app messages.
302
+ * Apps can extend via declaration merging:
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * declare module "@mariozechner/agent" {
307
+ * interface CustomAgentMessages {
308
+ * artifact: ArtifactMessage;
309
+ * notification: NotificationMessage;
310
+ * }
311
+ * }
312
+ * ```
313
+ */
314
+ export interface CustomAgentMessages {
315
+ }
316
+ /**
317
+ * AgentMessage: Union of LLM messages + custom messages.
318
+ * This abstraction allows apps to add custom message types while maintaining
319
+ * type safety and compatibility with the base LLM messages.
320
+ */
321
+ export type AgentMessage = Message | CustomAgentMessages[keyof CustomAgentMessages];
322
+ /**
323
+ * Public agent state.
324
+ *
325
+ * `tools` and `messages` use accessor properties so implementations can copy
326
+ * assigned arrays before storing them.
327
+ */
328
+ export interface AgentState {
329
+ /** System prompt sent with each model request. */
330
+ systemPrompt: string;
331
+ /** Active model used for future turns. */
332
+ model: Model<any>;
333
+ /** Requested reasoning level for future turns. */
334
+ thinkingLevel: ThinkingLevel;
335
+ /** Available tools. Assigning a new array copies the top-level array. */
336
+ set tools(tools: AgentTool<any>[]);
337
+ get tools(): AgentTool<any>[];
338
+ /** Conversation transcript. Assigning a new array copies the top-level array. */
339
+ set messages(messages: AgentMessage[]);
340
+ get messages(): AgentMessage[];
341
+ /**
342
+ * True while the agent is processing a prompt or continuation.
343
+ *
344
+ * This remains true until awaited `agent_end` listeners settle.
345
+ */
346
+ readonly isStreaming: boolean;
347
+ /** Partial assistant message for the current streamed response, if any. */
348
+ readonly streamingMessage?: AgentMessage;
349
+ /** Tool call ids currently executing. */
350
+ readonly pendingToolCalls: ReadonlySet<string>;
351
+ /** Error message from the most recent failed or aborted assistant turn, if any. */
352
+ readonly errorMessage?: string;
353
+ }
354
+ /** Final or partial result produced by a tool. */
355
+ export interface AgentToolResult<T> {
356
+ /** Text or image content returned to the model. */
357
+ content: (TextContent | ImageContent)[];
358
+ /** Arbitrary structured details for logs or UI rendering. */
359
+ details: T;
360
+ /**
361
+ * Hint that the agent should stop after the current tool batch.
362
+ * Early termination only happens when every finalized tool result in the batch sets this to true.
363
+ */
364
+ terminate?: boolean;
365
+ }
366
+ /** Callback used by tools to stream partial execution updates. */
367
+ export type AgentToolUpdateCallback<T = any> = (partialResult: AgentToolResult<T>) => void;
368
+ /** Tool definition used by the agent runtime. */
369
+ export interface AgentTool<TParameters extends TSchema = TSchema, TDetails = any> extends Tool<TParameters> {
370
+ /** Human-readable label for UI display. */
371
+ label: string;
372
+ /**
373
+ * Optional compatibility shim for raw tool-call arguments before schema validation.
374
+ * Must return an object that matches `TParameters`.
375
+ */
376
+ prepareArguments?: (args: unknown) => Static<TParameters>;
377
+ /** Execute the tool call. Throw on failure instead of encoding errors in `content`. */
378
+ execute: (toolCallId: string, params: Static<TParameters>, signal?: AbortSignal, onUpdate?: AgentToolUpdateCallback<TDetails>) => Promise<AgentToolResult<TDetails>>;
379
+ /**
380
+ * Per-tool execution mode override.
381
+ * - "sequential": this tool must execute one at a time with other tool calls.
382
+ * - "parallel": this tool can execute concurrently with other tool calls.
383
+ *
384
+ * If omitted, the default execution mode applies.
385
+ */
386
+ executionMode?: ToolExecutionMode;
387
+ }
388
+ /** Context snapshot passed into the low-level agent loop. */
389
+ export interface AgentContext {
390
+ /** System prompt included with the request. */
391
+ systemPrompt: string;
392
+ /** Transcript visible to the model. */
393
+ messages: AgentMessage[];
394
+ /** Tools available for this run. */
395
+ tools?: AgentTool<any>[];
396
+ }
397
+ /**
398
+ * Events emitted by the Agent for UI updates.
399
+ *
400
+ * `agent_end` is the last event emitted for a run, but awaited `Agent.subscribe()`
401
+ * listeners for that event are still part of run settlement. The agent becomes
402
+ * idle only after those listeners finish.
403
+ */
404
+ export type AgentEvent = {
405
+ type: "agent_start";
406
+ } | {
407
+ type: "agent_end";
408
+ messages: AgentMessage[];
409
+ } | {
410
+ type: "turn_start";
411
+ } | {
412
+ type: "turn_end";
413
+ message: AgentMessage;
414
+ toolResults: ToolResultMessage[];
415
+ } | {
416
+ type: "message_start";
417
+ message: AgentMessage;
418
+ } | {
419
+ type: "message_update";
420
+ message: AgentMessage;
421
+ assistantMessageEvent: AssistantMessageEvent;
422
+ } | {
423
+ type: "message_end";
424
+ message: AgentMessage;
425
+ } | {
426
+ type: "tool_execution_start";
427
+ toolCallId: string;
428
+ toolName: string;
429
+ args: any;
430
+ } | {
431
+ type: "tool_execution_update";
432
+ toolCallId: string;
433
+ toolName: string;
434
+ args: any;
435
+ partialResult: any;
436
+ } | {
437
+ type: "tool_execution_end";
438
+ toolCallId: string;
439
+ toolName: string;
440
+ result: any;
441
+ isError: boolean;
442
+ };
443
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,gBAAgB,EAChB,qBAAqB,EACrB,YAAY,EACZ,OAAO,EACP,KAAK,EACL,mBAAmB,EACnB,YAAY,EACZ,WAAW,EACX,IAAI,EACJ,iBAAiB,EACjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAE/C;;;;;;;;GAQG;AACH,MAAM,MAAM,QAAQ,GAAG,CACtB,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,KACpC,UAAU,CAAC,OAAO,YAAY,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC;AAEhF;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG,YAAY,GAAG,UAAU,CAAC;AAE1D,wEAAwE;AACxE,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,gBA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AAG,CAAA;CAAE,GACjF;IAAE,IAAI,EAAE,uBAAuB,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,GAAG,CAAC;IAAC,aAAa,EAAE,GAAG,CAAA;CAAE,GACtG;IAAE,IAAI,EAAE,oBAAoB,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,GAAG,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC","sourcesContent":["import type {\n\tAssistantMessage,\n\tAssistantMessageEvent,\n\tImageContent,\n\tMessage,\n\tModel,\n\tSimpleStreamOptions,\n\tstreamSimple,\n\tTextContent,\n\tTool,\n\tToolResultMessage,\n} from \"@threadwell/ai\";\nimport type { Static, TSchema } from \"typebox\";\n\n/**\n * Stream function used by the agent loop.\n *\n * Contract:\n * - Must not throw or return a rejected promise for request/model/runtime failures.\n * - Must return an AssistantMessageEventStream.\n * - Failures must be encoded in the returned stream via protocol events and a\n * final AssistantMessage with stopReason \"error\" or \"aborted\" and errorMessage.\n */\nexport type StreamFn = (\n\t...args: Parameters<typeof streamSimple>\n) => ReturnType<typeof streamSimple> | Promise<ReturnType<typeof streamSimple>>;\n\n/**\n * Configuration for how tool calls from a single assistant message are executed.\n *\n * - \"sequential\": each tool call is prepared, executed, and finalized before the next one starts.\n * - \"parallel\": tool calls are prepared sequentially, then allowed tools execute concurrently.\n * `tool_execution_end` is emitted in tool completion order after each tool is finalized,\n * while tool-result message artifacts are emitted later in assistant source order.\n */\nexport type ToolExecutionMode = \"sequential\" | \"parallel\";\n\n/** A single tool call content block emitted by an assistant message. */\nexport type AgentToolCall = Extract<AssistantMessage[\"content\"][number], { type: \"toolCall\" }>;\n\n/**\n * Result returned from `beforeToolCall`.\n *\n * Returning `{ block: true }` prevents the tool from executing. The loop emits an error tool result instead.\n * `reason` becomes the text shown in that error result. If omitted, a default blocked message is used.\n */\nexport interface BeforeToolCallResult {\n\tblock?: boolean;\n\treason?: string;\n}\n\n/**\n * Partial override returned from `afterToolCall`.\n *\n * Merge semantics are field-by-field:\n * - `content`: if provided, replaces the tool result content array in full\n * - `details`: if provided, replaces the tool result details value in full\n * - `isError`: if provided, replaces the tool result error flag\n * - `terminate`: if provided, replaces the early-termination hint\n *\n * Omitted fields keep the original executed tool result values.\n * There is no deep merge for `content` or `details`.\n */\nexport interface AfterToolCallResult {\n\tcontent?: (TextContent | ImageContent)[];\n\tdetails?: unknown;\n\tisError?: boolean;\n\t/**\n\t * Hint that the agent should stop after the current tool batch.\n\t * Early termination only happens when every finalized tool result in the batch sets this to true.\n\t */\n\tterminate?: boolean;\n}\n\n/** Context passed to `beforeToolCall`. */\nexport interface BeforeToolCallContext {\n\t/** The assistant message that requested the tool call. */\n\tassistantMessage: AssistantMessage;\n\t/** The raw tool call block from `assistantMessage.content`. */\n\ttoolCall: AgentToolCall;\n\t/** Validated tool arguments for the target tool schema. */\n\targs: unknown;\n\t/** Current agent context at the time the tool call is prepared. */\n\tcontext: AgentContext;\n}\n\n/** Context passed to `afterToolCall`. */\nexport interface AfterToolCallContext {\n\t/** The assistant message that requested the tool call. */\n\tassistantMessage: AssistantMessage;\n\t/** The raw tool call block from `assistantMessage.content`. */\n\ttoolCall: AgentToolCall;\n\t/** Validated tool arguments for the target tool schema. */\n\targs: unknown;\n\t/** The executed tool result before any `afterToolCall` overrides are applied. */\n\tresult: AgentToolResult<any>;\n\t/** Whether the executed tool result is currently treated as an error. */\n\tisError: boolean;\n\t/** Current agent context at the time the tool call is finalized. */\n\tcontext: AgentContext;\n}\n\n/** Context passed to `shouldStopAfterTurn`. */\nexport interface ShouldStopAfterTurnContext {\n\t/** The assistant message that completed the turn. */\n\tmessage: AssistantMessage;\n\t/** Tool result messages passed to the preceding `turn_end` event. */\n\ttoolResults: ToolResultMessage[];\n\t/** Current agent context after the turn's assistant message and tool results have been appended. */\n\tcontext: AgentContext;\n\t/** Messages that this loop invocation will return if it exits at this point. Prompt runs include the initial prompt messages; continuation runs do not include pre-existing context messages. */\n\tnewMessages: AgentMessage[];\n}\n\n/** Common context passed to agent loop lifecycle hooks. */\nexport interface AgentLoopLifecycleBaseContext {\n\t/** Current mutable loop context. Hook implementations should treat it as read-only unless explicitly coordinating with the loop owner. */\n\tcontext: AgentContext;\n\t/** Messages produced by this loop invocation so far. */\n\tnewMessages: readonly AgentMessage[];\n\t/** Active abort signal for the loop invocation. */\n\tsignal?: AbortSignal;\n}\n\n/** Context passed before the next model request is created. */\nexport type BeforeModelCallContext = AgentLoopLifecycleBaseContext;\n\n/** Context passed after a final assistant message is available. */\nexport interface AfterModelCallContext extends AgentLoopLifecycleBaseContext {\n\t/** Final assistant message returned by the model. */\n\tmessage: AssistantMessage;\n}\n\n/** Context passed after a batch of tool calls has completed and tool-result messages have been appended. */\nexport interface AfterToolBatchContext extends AgentLoopLifecycleBaseContext {\n\t/** Assistant message that requested the tool calls. */\n\tmessage: AssistantMessage;\n\t/** Tool-result messages generated by the completed batch. */\n\ttoolResults: readonly ToolResultMessage[];\n\t/** Whether the loop will continue because at least one tool result did not request termination. */\n\thasMoreToolCalls: boolean;\n}\n\n/** Context passed after turn_end has been emitted. */\nexport interface AfterTurnEndContext extends AgentLoopLifecycleBaseContext {\n\t/** Assistant message that completed the turn. */\n\tmessage: AssistantMessage;\n\t/** Tool-result messages passed to turn_end. */\n\ttoolResults: readonly ToolResultMessage[];\n}\n\n/** Reason the loop is about to emit agent_end. */\nexport type AgentLoopEndReason = \"stop\" | \"error\" | \"aborted\" | \"shouldStop\";\n\n/** Context passed immediately before agent_end is emitted. */\nexport interface BeforeLoopEndContext extends AgentLoopLifecycleBaseContext {\n\t/** Why the loop is ending. */\n\treason: AgentLoopEndReason;\n\t/** Last assistant message, when the loop reached one. */\n\tmessage?: AssistantMessage;\n\t/** Tool results from the last completed turn, when available. */\n\ttoolResults?: readonly ToolResultMessage[];\n}\n\nexport interface AgentContextProviderContext extends AgentLoopLifecycleBaseContext {}\n\n/** Provides additional model-call context without mutating the persisted transcript. */\nexport type AgentContextProvider = (\n\tcontext: AgentContextProviderContext,\n) => AgentMessage[] | undefined | Promise<AgentMessage[] | undefined>;\n\n/** Optional lifecycle hooks for low-level agent loop observers. */\nexport interface AgentLoopLifecycleHooks {\n\t/** Called after pending messages are appended and before the next model request is prepared. */\n\tbeforeModelCall?: (context: BeforeModelCallContext) => Promise<void> | void;\n\t/** Called after the final assistant message for a model call is available. */\n\tafterModelCall?: (context: AfterModelCallContext) => Promise<void> | void;\n\t/** Called after tool execution and after tool-result messages have been appended. */\n\tafterToolBatch?: (context: AfterToolBatchContext) => Promise<void> | void;\n\t/** Called after turn_end has been emitted. */\n\tafterTurnEnd?: (context: AfterTurnEndContext) => Promise<void> | void;\n\t/** Called immediately before agent_end is emitted. */\n\tbeforeLoopEnd?: (context: BeforeLoopEndContext) => Promise<void> | void;\n}\n\nexport interface AgentLoopConfig extends SimpleStreamOptions {\n\tmodel: Model<any>;\n\n\t/**\n\t * Optional low-level lifecycle hooks.\n\t *\n\t * Hooks are observational by contract: they must not throw and should not mutate transcript arrays.\n\t * Use existing queue and context-transform APIs for message injection.\n\t */\n\tlifecycleHooks?: AgentLoopLifecycleHooks;\n\n\t/**\n\t * Optional providers for ephemeral model-call context.\n\t *\n\t * Provider messages are prepended to the transformed context before `convertToLlm` and are not\n\t * written to the persisted transcript. Providers run in array order.\n\t */\n\tcontextProviders?: AgentContextProvider[];\n\n\t/**\n\t * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.\n\t *\n\t * Each AgentMessage must be converted to a UserMessage, AssistantMessage, or ToolResultMessage\n\t * that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,\n\t * status messages) should be filtered out.\n\t *\n\t * Contract: must not throw or reject. Return a safe fallback value instead.\n\t * Throwing interrupts the low-level agent loop without producing a normal event sequence.\n\t *\n\t * @example\n\t * ```typescript\n\t * convertToLlm: (messages) => messages.flatMap(m => {\n\t * if (m.role === \"custom\") {\n\t * // Convert custom message to user message\n\t * return [{ role: \"user\", content: m.content, timestamp: m.timestamp }];\n\t * }\n\t * if (m.role === \"notification\") {\n\t * // Filter out UI-only messages\n\t * return [];\n\t * }\n\t * // Pass through standard LLM messages\n\t * return [m];\n\t * })\n\t * ```\n\t */\n\tconvertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;\n\n\t/**\n\t * Optional transform applied to the context before `convertToLlm`.\n\t *\n\t * Use this for operations that work at the AgentMessage level:\n\t * - Context window management (pruning old messages)\n\t * - Injecting context from external sources\n\t *\n\t * Contract: must not throw or reject. Return the original messages or another\n\t * safe fallback value instead.\n\t *\n\t * @example\n\t * ```typescript\n\t * transformContext: async (messages) => {\n\t * if (estimateTokens(messages) > MAX_TOKENS) {\n\t * return pruneOldMessages(messages);\n\t * }\n\t * return messages;\n\t * }\n\t * ```\n\t */\n\ttransformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;\n\n\t/**\n\t * Resolves an API key dynamically for each LLM call.\n\t *\n\t * Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire\n\t * during long-running tool execution phases.\n\t *\n\t * Contract: must not throw or reject. Return undefined when no key is available.\n\t */\n\tgetApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;\n\n\t/**\n\t * Called after each turn fully completes and `turn_end` has been emitted.\n\t *\n\t * If it returns true, the loop emits `agent_end` and exits before polling steering or follow-up queues,\n\t * without starting another LLM call. The current assistant response and any tool executions finish normally.\n\t *\n\t * Use this to request a graceful stop after the current turn, e.g. before context gets too full.\n\t *\n\t * Contract: must not throw or reject. Throwing interrupts the low-level agent loop without producing a normal event sequence.\n\t */\n\tshouldStopAfterTurn?: (context: ShouldStopAfterTurnContext) => boolean | Promise<boolean>;\n\n\t/**\n\t * Returns steering messages to inject into the conversation mid-run.\n\t *\n\t * Called after the current assistant turn finishes executing its tool calls, unless `shouldStopAfterTurn` exits first.\n\t * If messages are returned, they are added to the context before the next LLM call.\n\t * Tool calls from the current assistant message are not skipped.\n\t *\n\t * Use this for \"steering\" the agent while it's working.\n\t *\n\t * Contract: must not throw or reject. Return [] when no steering messages are available.\n\t */\n\tgetSteeringMessages?: () => Promise<AgentMessage[]>;\n\n\t/**\n\t * Returns follow-up messages to process after the agent would otherwise stop.\n\t *\n\t * Called when the agent has no more tool calls and no steering messages.\n\t * If messages are returned, they're added to the context and the agent\n\t * continues with another turn.\n\t *\n\t * Use this for follow-up messages that should wait until the agent finishes.\n\t *\n\t * Contract: must not throw or reject. Return [] when no follow-up messages are available.\n\t */\n\tgetFollowUpMessages?: () => Promise<AgentMessage[]>;\n\n\t/**\n\t * Tool execution mode.\n\t * - \"sequential\": execute tool calls one by one\n\t * - \"parallel\": preflight tool calls sequentially, then execute allowed tools concurrently;\n\t * emit `tool_execution_end` in tool completion order after each tool is finalized,\n\t * then emit tool-result message artifacts later in assistant source order\n\t *\n\t * Default: \"parallel\"\n\t */\n\ttoolExecution?: ToolExecutionMode;\n\n\t/**\n\t * Called before a tool is executed, after arguments have been validated.\n\t *\n\t * Return `{ block: true }` to prevent execution. The loop emits an error tool result instead.\n\t * The hook receives the agent abort signal and is responsible for honoring it.\n\t */\n\tbeforeToolCall?: (context: BeforeToolCallContext, signal?: AbortSignal) => Promise<BeforeToolCallResult | undefined>;\n\n\t/**\n\t * Called after a tool finishes executing, before `tool_execution_end` and tool-result message events are emitted.\n\t *\n\t * Return an `AfterToolCallResult` to override parts of the executed tool result:\n\t * - `content` replaces the full content array\n\t * - `details` replaces the full details payload\n\t * - `isError` replaces the error flag\n\t * - `terminate` replaces the early-termination hint\n\t *\n\t * Any omitted fields keep their original values. No deep merge is performed.\n\t * The hook receives the agent abort signal and is responsible for honoring it.\n\t */\n\tafterToolCall?: (context: AfterToolCallContext, signal?: AbortSignal) => Promise<AfterToolCallResult | undefined>;\n}\n\n/**\n * Thinking/reasoning level for models that support it.\n * Note: \"xhigh\" is only supported by selected model families. Use model thinking-level metadata\n * from @threadwell/ai to detect support for a concrete model.\n */\nexport type ThinkingLevel = \"off\" | \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\";\n\n/**\n * Extensible interface for custom app messages.\n * Apps can extend via declaration merging:\n *\n * @example\n * ```typescript\n * declare module \"@mariozechner/agent\" {\n * interface CustomAgentMessages {\n * artifact: ArtifactMessage;\n * notification: NotificationMessage;\n * }\n * }\n * ```\n */\nexport interface CustomAgentMessages {\n\t// Empty by default - apps extend via declaration merging\n}\n\n/**\n * AgentMessage: Union of LLM messages + custom messages.\n * This abstraction allows apps to add custom message types while maintaining\n * type safety and compatibility with the base LLM messages.\n */\nexport type AgentMessage = Message | CustomAgentMessages[keyof CustomAgentMessages];\n\n/**\n * Public agent state.\n *\n * `tools` and `messages` use accessor properties so implementations can copy\n * assigned arrays before storing them.\n */\nexport interface AgentState {\n\t/** System prompt sent with each model request. */\n\tsystemPrompt: string;\n\t/** Active model used for future turns. */\n\tmodel: Model<any>;\n\t/** Requested reasoning level for future turns. */\n\tthinkingLevel: ThinkingLevel;\n\t/** Available tools. Assigning a new array copies the top-level array. */\n\tset tools(tools: AgentTool<any>[]);\n\tget tools(): AgentTool<any>[];\n\t/** Conversation transcript. Assigning a new array copies the top-level array. */\n\tset messages(messages: AgentMessage[]);\n\tget messages(): AgentMessage[];\n\t/**\n\t * True while the agent is processing a prompt or continuation.\n\t *\n\t * This remains true until awaited `agent_end` listeners settle.\n\t */\n\treadonly isStreaming: boolean;\n\t/** Partial assistant message for the current streamed response, if any. */\n\treadonly streamingMessage?: AgentMessage;\n\t/** Tool call ids currently executing. */\n\treadonly pendingToolCalls: ReadonlySet<string>;\n\t/** Error message from the most recent failed or aborted assistant turn, if any. */\n\treadonly errorMessage?: string;\n}\n\n/** Final or partial result produced by a tool. */\nexport interface AgentToolResult<T> {\n\t/** Text or image content returned to the model. */\n\tcontent: (TextContent | ImageContent)[];\n\t/** Arbitrary structured details for logs or UI rendering. */\n\tdetails: T;\n\t/**\n\t * Hint that the agent should stop after the current tool batch.\n\t * Early termination only happens when every finalized tool result in the batch sets this to true.\n\t */\n\tterminate?: boolean;\n}\n\n/** Callback used by tools to stream partial execution updates. */\nexport type AgentToolUpdateCallback<T = any> = (partialResult: AgentToolResult<T>) => void;\n\n/** Tool definition used by the agent runtime. */\nexport interface AgentTool<TParameters extends TSchema = TSchema, TDetails = any> extends Tool<TParameters> {\n\t/** Human-readable label for UI display. */\n\tlabel: string;\n\t/**\n\t * Optional compatibility shim for raw tool-call arguments before schema validation.\n\t * Must return an object that matches `TParameters`.\n\t */\n\tprepareArguments?: (args: unknown) => Static<TParameters>;\n\t/** Execute the tool call. Throw on failure instead of encoding errors in `content`. */\n\texecute: (\n\t\ttoolCallId: string,\n\t\tparams: Static<TParameters>,\n\t\tsignal?: AbortSignal,\n\t\tonUpdate?: AgentToolUpdateCallback<TDetails>,\n\t) => Promise<AgentToolResult<TDetails>>;\n\t/**\n\t * Per-tool execution mode override.\n\t * - \"sequential\": this tool must execute one at a time with other tool calls.\n\t * - \"parallel\": this tool can execute concurrently with other tool calls.\n\t *\n\t * If omitted, the default execution mode applies.\n\t */\n\texecutionMode?: ToolExecutionMode;\n}\n\n/** Context snapshot passed into the low-level agent loop. */\nexport interface AgentContext {\n\t/** System prompt included with the request. */\n\tsystemPrompt: string;\n\t/** Transcript visible to the model. */\n\tmessages: AgentMessage[];\n\t/** Tools available for this run. */\n\ttools?: AgentTool<any>[];\n}\n\n/**\n * Events emitted by the Agent for UI updates.\n *\n * `agent_end` is the last event emitted for a run, but awaited `Agent.subscribe()`\n * listeners for that event are still part of run settlement. The agent becomes\n * idle only after those listeners finish.\n */\nexport type AgentEvent =\n\t// Agent lifecycle\n\t| { type: \"agent_start\" }\n\t| { type: \"agent_end\"; messages: AgentMessage[] }\n\t// Turn lifecycle - a turn is one assistant response + any tool calls/results\n\t| { type: \"turn_start\" }\n\t| { type: \"turn_end\"; message: AgentMessage; toolResults: ToolResultMessage[] }\n\t// Message lifecycle - emitted for user, assistant, and toolResult messages\n\t| { type: \"message_start\"; message: AgentMessage }\n\t// Only emitted for assistant messages during streaming\n\t| { type: \"message_update\"; message: AgentMessage; assistantMessageEvent: AssistantMessageEvent }\n\t| { type: \"message_end\"; message: AgentMessage }\n\t// Tool execution lifecycle\n\t| { type: \"tool_execution_start\"; toolCallId: string; toolName: string; args: any }\n\t| { type: \"tool_execution_update\"; toolCallId: string; toolName: string; args: any; partialResult: any }\n\t| { type: \"tool_execution_end\"; toolCallId: string; toolName: string; result: any; isError: boolean };\n"]}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n\tAssistantMessage,\n\tAssistantMessageEvent,\n\tImageContent,\n\tMessage,\n\tModel,\n\tSimpleStreamOptions,\n\tstreamSimple,\n\tTextContent,\n\tTool,\n\tToolResultMessage,\n} from \"@threadwell/ai\";\nimport type { Static, TSchema } from \"typebox\";\n\n/**\n * Stream function used by the agent loop.\n *\n * Contract:\n * - Must not throw or return a rejected promise for request/model/runtime failures.\n * - Must return an AssistantMessageEventStream.\n * - Failures must be encoded in the returned stream via protocol events and a\n * final AssistantMessage with stopReason \"error\" or \"aborted\" and errorMessage.\n */\nexport type StreamFn = (\n\t...args: Parameters<typeof streamSimple>\n) => ReturnType<typeof streamSimple> | Promise<ReturnType<typeof streamSimple>>;\n\n/**\n * Configuration for how tool calls from a single assistant message are executed.\n *\n * - \"sequential\": each tool call is prepared, executed, and finalized before the next one starts.\n * - \"parallel\": tool calls are prepared sequentially, then allowed tools execute concurrently.\n * `tool_execution_end` is emitted in tool completion order after each tool is finalized,\n * while tool-result message artifacts are emitted later in assistant source order.\n */\nexport type ToolExecutionMode = \"sequential\" | \"parallel\";\n\n/** A single tool call content block emitted by an assistant message. */\nexport type AgentToolCall = Extract<AssistantMessage[\"content\"][number], { type: \"toolCall\" }>;\n\n/**\n * Result returned from `beforeToolCall`.\n *\n * Returning `{ block: true }` prevents the tool from executing. The loop emits an error tool result instead.\n * `reason` becomes the text shown in that error result. If omitted, a default blocked message is used.\n */\nexport interface BeforeToolCallResult {\n\tblock?: boolean;\n\treason?: string;\n}\n\n/**\n * Partial override returned from `afterToolCall`.\n *\n * Merge semantics are field-by-field:\n * - `content`: if provided, replaces the tool result content array in full\n * - `details`: if provided, replaces the tool result details value in full\n * - `isError`: if provided, replaces the tool result error flag\n * - `terminate`: if provided, replaces the early-termination hint\n *\n * Omitted fields keep the original executed tool result values.\n * There is no deep merge for `content` or `details`.\n */\nexport interface AfterToolCallResult {\n\tcontent?: (TextContent | ImageContent)[];\n\tdetails?: unknown;\n\tisError?: boolean;\n\t/**\n\t * Hint that the agent should stop after the current tool batch.\n\t * Early termination only happens when every finalized tool result in the batch sets this to true.\n\t */\n\tterminate?: boolean;\n}\n\n/** Context passed to `beforeToolCall`. */\nexport interface BeforeToolCallContext {\n\t/** The assistant message that requested the tool call. */\n\tassistantMessage: AssistantMessage;\n\t/** The raw tool call block from `assistantMessage.content`. */\n\ttoolCall: AgentToolCall;\n\t/** Validated tool arguments for the target tool schema. */\n\targs: unknown;\n\t/** Current agent context at the time the tool call is prepared. */\n\tcontext: AgentContext;\n}\n\n/** Context passed to `afterToolCall`. */\nexport interface AfterToolCallContext {\n\t/** The assistant message that requested the tool call. */\n\tassistantMessage: AssistantMessage;\n\t/** The raw tool call block from `assistantMessage.content`. */\n\ttoolCall: AgentToolCall;\n\t/** Validated tool arguments for the target tool schema. */\n\targs: unknown;\n\t/** The executed tool result before any `afterToolCall` overrides are applied. */\n\tresult: AgentToolResult<any>;\n\t/** Whether the executed tool result is currently treated as an error. */\n\tisError: boolean;\n\t/** Current agent context at the time the tool call is finalized. */\n\tcontext: AgentContext;\n}\n\n/** Context passed to `shouldStopAfterTurn`. */\nexport interface ShouldStopAfterTurnContext {\n\t/** The assistant message that completed the turn. */\n\tmessage: AssistantMessage;\n\t/** Tool result messages passed to the preceding `turn_end` event. */\n\ttoolResults: ToolResultMessage[];\n\t/** Current agent context after the turn's assistant message and tool results have been appended. */\n\tcontext: AgentContext;\n\t/** Messages that this loop invocation will return if it exits at this point. Prompt runs include the initial prompt messages; continuation runs do not include pre-existing context messages. */\n\tnewMessages: AgentMessage[];\n}\n\n/** Common context passed to agent loop lifecycle hooks. */\nexport interface AgentLoopLifecycleBaseContext {\n\t/** Current mutable loop context. Hook implementations should treat it as read-only unless explicitly coordinating with the loop owner. */\n\tcontext: AgentContext;\n\t/** Messages produced by this loop invocation so far. */\n\tnewMessages: readonly AgentMessage[];\n\t/** Active abort signal for the loop invocation. */\n\tsignal?: AbortSignal;\n}\n\n/** Context passed before the next model request is created. */\nexport type BeforeModelCallContext = AgentLoopLifecycleBaseContext;\n\n/** Context passed after a final assistant message is available. */\nexport interface AfterModelCallContext extends AgentLoopLifecycleBaseContext {\n\t/** Final assistant message returned by the model. */\n\tmessage: AssistantMessage;\n}\n\n/** Context passed after a batch of tool calls has completed and tool-result messages have been appended. */\nexport interface AfterToolBatchContext extends AgentLoopLifecycleBaseContext {\n\t/** Assistant message that requested the tool calls. */\n\tmessage: AssistantMessage;\n\t/** Tool-result messages generated by the completed batch. */\n\ttoolResults: readonly ToolResultMessage[];\n\t/** Whether the loop will continue because at least one tool result did not request termination. */\n\thasMoreToolCalls: boolean;\n}\n\n/** Context passed after turn_end has been emitted. */\nexport interface AfterTurnEndContext extends AgentLoopLifecycleBaseContext {\n\t/** Assistant message that completed the turn. */\n\tmessage: AssistantMessage;\n\t/** Tool-result messages passed to turn_end. */\n\ttoolResults: readonly ToolResultMessage[];\n}\n\n/** Reason the loop is about to emit agent_end. */\nexport type AgentLoopEndReason = \"stop\" | \"error\" | \"aborted\" | \"shouldStop\";\n\n/** Context passed immediately before agent_end is emitted. */\nexport interface BeforeLoopEndContext extends AgentLoopLifecycleBaseContext {\n\t/** Why the loop is ending. */\n\treason: AgentLoopEndReason;\n\t/** Last assistant message, when the loop reached one. */\n\tmessage?: AssistantMessage;\n\t/** Tool results from the last completed turn, when available. */\n\ttoolResults?: readonly ToolResultMessage[];\n}\n\nexport interface AgentContextProviderContext extends AgentLoopLifecycleBaseContext {}\n\n/** Provides additional model-call context without mutating the persisted transcript. */\nexport type AgentContextProvider = (\n\tcontext: AgentContextProviderContext,\n) => AgentMessage[] | undefined | Promise<AgentMessage[] | undefined>;\n\n/** Optional lifecycle hooks for low-level agent loop observers. */\nexport interface AgentLoopLifecycleHooks {\n\t/** Called after pending messages are appended and before the next model request is prepared. */\n\tbeforeModelCall?: (context: BeforeModelCallContext) => Promise<void> | void;\n\t/** Called after the final assistant message for a model call is available. */\n\tafterModelCall?: (context: AfterModelCallContext) => Promise<void> | void;\n\t/** Called after tool execution and after tool-result messages have been appended. */\n\tafterToolBatch?: (context: AfterToolBatchContext) => Promise<void> | void;\n\t/** Called after turn_end has been emitted. */\n\tafterTurnEnd?: (context: AfterTurnEndContext) => Promise<void> | void;\n\t/** Called immediately before agent_end is emitted. */\n\tbeforeLoopEnd?: (context: BeforeLoopEndContext) => Promise<void> | void;\n}\n\nexport interface AgentLoopConfig extends SimpleStreamOptions {\n\tmodel: Model<any>;\n\n\t/**\n\t * Optional low-level lifecycle hooks.\n\t *\n\t * Hooks are observational by contract: they must not throw and should not mutate transcript arrays.\n\t * Use existing queue and context-transform APIs for message injection.\n\t */\n\tlifecycleHooks?: AgentLoopLifecycleHooks;\n\n\t/**\n\t * Optional providers for ephemeral model-call context.\n\t *\n\t * Provider messages are prepended to the transformed context before `convertToLlm` and are not\n\t * written to the persisted transcript. Providers run in array order.\n\t */\n\tcontextProviders?: AgentContextProvider[];\n\n\t/**\n\t * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.\n\t *\n\t * Each AgentMessage must be converted to a UserMessage, AssistantMessage, or ToolResultMessage\n\t * that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,\n\t * status messages) should be filtered out.\n\t *\n\t * Contract: must not throw or reject. Return a safe fallback value instead.\n\t * Throwing interrupts the low-level agent loop without producing a normal event sequence.\n\t *\n\t * @example\n\t * ```typescript\n\t * convertToLlm: (messages) => messages.flatMap(m => {\n\t * if (m.role === \"custom\") {\n\t * // Convert custom message to user message\n\t * return [{ role: \"user\", content: m.content, timestamp: m.timestamp }];\n\t * }\n\t * if (m.role === \"notification\") {\n\t * // Filter out UI-only messages\n\t * return [];\n\t * }\n\t * // Pass through standard LLM messages\n\t * return [m];\n\t * })\n\t * ```\n\t */\n\tconvertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;\n\n\t/**\n\t * Optional transform applied to the context before `convertToLlm`.\n\t *\n\t * Use this for operations that work at the AgentMessage level:\n\t * - Context window management (pruning old messages)\n\t * - Injecting context from external sources\n\t *\n\t * Contract: must not throw or reject. Return the original messages or another\n\t * safe fallback value instead.\n\t *\n\t * @example\n\t * ```typescript\n\t * transformContext: async (messages) => {\n\t * if (estimateTokens(messages) > MAX_TOKENS) {\n\t * return pruneOldMessages(messages);\n\t * }\n\t * return messages;\n\t * }\n\t * ```\n\t */\n\ttransformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;\n\n\t/**\n\t * Resolves an API key dynamically for each LLM call.\n\t *\n\t * Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire\n\t * during long-running tool execution phases.\n\t *\n\t * Contract: must not throw or reject. Return undefined when no key is available.\n\t */\n\tgetApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;\n\n\t/**\n\t * Called after each turn fully completes and `turn_end` has been emitted.\n\t *\n\t * If it returns true, the loop emits `agent_end` and exits before polling steering or follow-up queues,\n\t * without starting another LLM call. The current assistant response and any tool executions finish normally.\n\t *\n\t * Use this to request a graceful stop after the current turn, e.g. before context gets too full.\n\t *\n\t * Contract: must not throw or reject. Throwing interrupts the low-level agent loop without producing a normal event sequence.\n\t */\n\tshouldStopAfterTurn?: (context: ShouldStopAfterTurnContext) => boolean | Promise<boolean>;\n\n\t/**\n\t * Returns steering messages to inject into the conversation mid-run.\n\t *\n\t * Called after the current assistant turn finishes executing its tool calls, unless `shouldStopAfterTurn` exits first.\n\t * If messages are returned, they are added to the context before the next LLM call.\n\t * Tool calls from the current assistant message are not skipped.\n\t *\n\t * Use this for \"steering\" the agent while it's working.\n\t *\n\t * Contract: must not throw or reject. Return [] when no steering messages are available.\n\t */\n\tgetSteeringMessages?: () => Promise<AgentMessage[]>;\n\n\t/**\n\t * Returns follow-up messages to process after the agent would otherwise stop.\n\t *\n\t * Called when the agent has no more tool calls and no steering messages.\n\t * If messages are returned, they're added to the context and the agent\n\t * continues with another turn.\n\t *\n\t * Use this for follow-up messages that should wait until the agent finishes.\n\t *\n\t * Contract: must not throw or reject. Return [] when no follow-up messages are available.\n\t */\n\tgetFollowUpMessages?: () => Promise<AgentMessage[]>;\n\n\t/**\n\t * Tool execution mode.\n\t * - \"sequential\": execute tool calls one by one\n\t * - \"parallel\": preflight tool calls sequentially, then execute allowed tools concurrently;\n\t * emit `tool_execution_end` in tool completion order after each tool is finalized,\n\t * then emit tool-result message artifacts later in assistant source order\n\t *\n\t * Default: \"parallel\"\n\t */\n\ttoolExecution?: ToolExecutionMode;\n\n\t/**\n\t * Called before a tool is executed, after arguments have been validated.\n\t *\n\t * Return `{ block: true }` to prevent execution. The loop emits an error tool result instead.\n\t * The hook receives the agent abort signal and is responsible for honoring it.\n\t */\n\tbeforeToolCall?: (context: BeforeToolCallContext, signal?: AbortSignal) => Promise<BeforeToolCallResult | undefined>;\n\n\t/**\n\t * Called after a tool finishes executing, before `tool_execution_end` and tool-result message events are emitted.\n\t *\n\t * Return an `AfterToolCallResult` to override parts of the executed tool result:\n\t * - `content` replaces the full content array\n\t * - `details` replaces the full details payload\n\t * - `isError` replaces the error flag\n\t * - `terminate` replaces the early-termination hint\n\t *\n\t * Any omitted fields keep their original values. No deep merge is performed.\n\t * The hook receives the agent abort signal and is responsible for honoring it.\n\t */\n\tafterToolCall?: (context: AfterToolCallContext, signal?: AbortSignal) => Promise<AfterToolCallResult | undefined>;\n}\n\n/**\n * Thinking/reasoning level for models that support it.\n * Note: \"xhigh\" is only supported by selected model families. Use model thinking-level metadata\n * from @threadwell/ai to detect support for a concrete model.\n */\nexport type ThinkingLevel = \"off\" | \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\";\n\n/**\n * Extensible interface for custom app messages.\n * Apps can extend via declaration merging:\n *\n * @example\n * ```typescript\n * declare module \"@mariozechner/agent\" {\n * interface CustomAgentMessages {\n * artifact: ArtifactMessage;\n * notification: NotificationMessage;\n * }\n * }\n * ```\n */\nexport interface CustomAgentMessages {\n\t// Empty by default - apps extend via declaration merging\n}\n\n/**\n * AgentMessage: Union of LLM messages + custom messages.\n * This abstraction allows apps to add custom message types while maintaining\n * type safety and compatibility with the base LLM messages.\n */\nexport type AgentMessage = Message | CustomAgentMessages[keyof CustomAgentMessages];\n\n/**\n * Public agent state.\n *\n * `tools` and `messages` use accessor properties so implementations can copy\n * assigned arrays before storing them.\n */\nexport interface AgentState {\n\t/** System prompt sent with each model request. */\n\tsystemPrompt: string;\n\t/** Active model used for future turns. */\n\tmodel: Model<any>;\n\t/** Requested reasoning level for future turns. */\n\tthinkingLevel: ThinkingLevel;\n\t/** Available tools. Assigning a new array copies the top-level array. */\n\tset tools(tools: AgentTool<any>[]);\n\tget tools(): AgentTool<any>[];\n\t/** Conversation transcript. Assigning a new array copies the top-level array. */\n\tset messages(messages: AgentMessage[]);\n\tget messages(): AgentMessage[];\n\t/**\n\t * True while the agent is processing a prompt or continuation.\n\t *\n\t * This remains true until awaited `agent_end` listeners settle.\n\t */\n\treadonly isStreaming: boolean;\n\t/** Partial assistant message for the current streamed response, if any. */\n\treadonly streamingMessage?: AgentMessage;\n\t/** Tool call ids currently executing. */\n\treadonly pendingToolCalls: ReadonlySet<string>;\n\t/** Error message from the most recent failed or aborted assistant turn, if any. */\n\treadonly errorMessage?: string;\n}\n\n/** Final or partial result produced by a tool. */\nexport interface AgentToolResult<T> {\n\t/** Text or image content returned to the model. */\n\tcontent: (TextContent | ImageContent)[];\n\t/** Arbitrary structured details for logs or UI rendering. */\n\tdetails: T;\n\t/**\n\t * Hint that the agent should stop after the current tool batch.\n\t * Early termination only happens when every finalized tool result in the batch sets this to true.\n\t */\n\tterminate?: boolean;\n}\n\n/** Callback used by tools to stream partial execution updates. */\nexport type AgentToolUpdateCallback<T = any> = (partialResult: AgentToolResult<T>) => void;\n\n/** Tool definition used by the agent runtime. */\nexport interface AgentTool<TParameters extends TSchema = TSchema, TDetails = any> extends Tool<TParameters> {\n\t/** Human-readable label for UI display. */\n\tlabel: string;\n\t/**\n\t * Optional compatibility shim for raw tool-call arguments before schema validation.\n\t * Must return an object that matches `TParameters`.\n\t */\n\tprepareArguments?: (args: unknown) => Static<TParameters>;\n\t/** Execute the tool call. Throw on failure instead of encoding errors in `content`. */\n\texecute: (\n\t\ttoolCallId: string,\n\t\tparams: Static<TParameters>,\n\t\tsignal?: AbortSignal,\n\t\tonUpdate?: AgentToolUpdateCallback<TDetails>,\n\t) => Promise<AgentToolResult<TDetails>>;\n\t/**\n\t * Per-tool execution mode override.\n\t * - \"sequential\": this tool must execute one at a time with other tool calls.\n\t * - \"parallel\": this tool can execute concurrently with other tool calls.\n\t *\n\t * If omitted, the default execution mode applies.\n\t */\n\texecutionMode?: ToolExecutionMode;\n}\n\n/** Context snapshot passed into the low-level agent loop. */\nexport interface AgentContext {\n\t/** System prompt included with the request. */\n\tsystemPrompt: string;\n\t/** Transcript visible to the model. */\n\tmessages: AgentMessage[];\n\t/** Tools available for this run. */\n\ttools?: AgentTool<any>[];\n}\n\n/**\n * Events emitted by the Agent for UI updates.\n *\n * `agent_end` is the last event emitted for a run, but awaited `Agent.subscribe()`\n * listeners for that event are still part of run settlement. The agent becomes\n * idle only after those listeners finish.\n */\nexport type AgentEvent =\n\t// Agent lifecycle\n\t| { type: \"agent_start\" }\n\t| { type: \"agent_end\"; messages: AgentMessage[] }\n\t// Turn lifecycle - a turn is one assistant response + any tool calls/results\n\t| { type: \"turn_start\" }\n\t| { type: \"turn_end\"; message: AgentMessage; toolResults: ToolResultMessage[] }\n\t// Message lifecycle - emitted for user, assistant, and toolResult messages\n\t| { type: \"message_start\"; message: AgentMessage }\n\t// Only emitted for assistant messages during streaming\n\t| { type: \"message_update\"; message: AgentMessage; assistantMessageEvent: AssistantMessageEvent }\n\t| { type: \"message_end\"; message: AgentMessage }\n\t// Tool execution lifecycle\n\t| { type: \"tool_execution_start\"; toolCallId: string; toolName: string; args: any }\n\t| { type: \"tool_execution_update\"; toolCallId: string; toolName: string; args: any; partialResult: any }\n\t| { type: \"tool_execution_end\"; toolCallId: string; toolName: string; result: any; isError: boolean };\n"]}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@threadwell/agent-core",
3
+ "version": "0.0.1",
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
+ "@threadwell/ai": "^0.0.1",
21
+ "typebox": "^1.1.24"
22
+ },
23
+ "keywords": [
24
+ "ai",
25
+ "agent",
26
+ "llm",
27
+ "transport",
28
+ "state-management"
29
+ ],
30
+ "author": "Mario Zechner",
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/EngramResearch/threadwell.git",
35
+ "directory": "packages/agent"
36
+ },
37
+ "engines": {
38
+ "node": ">=20.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "^24.3.0",
42
+ "typescript": "^5.7.3",
43
+ "vitest": "^3.2.4"
44
+ }
45
+ }