@openrouter/agent 0.7.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/README.md +728 -19
  2. package/esm/index.d.ts +24 -6
  3. package/esm/index.d.ts.map +1 -1
  4. package/esm/index.js +20 -5
  5. package/esm/index.js.map +1 -1
  6. package/esm/inner-loop/call-model.d.ts +8 -5
  7. package/esm/inner-loop/call-model.d.ts.map +1 -1
  8. package/esm/inner-loop/call-model.js +49 -40
  9. package/esm/inner-loop/call-model.js.map +1 -1
  10. package/esm/inner-loop/resume-tool-results.d.ts +128 -0
  11. package/esm/inner-loop/resume-tool-results.d.ts.map +1 -0
  12. package/esm/inner-loop/resume-tool-results.js +244 -0
  13. package/esm/inner-loop/resume-tool-results.js.map +1 -0
  14. package/esm/lib/agent-tool.d.ts +92 -0
  15. package/esm/lib/agent-tool.d.ts.map +1 -0
  16. package/esm/lib/agent-tool.js +211 -0
  17. package/esm/lib/agent-tool.js.map +1 -0
  18. package/esm/lib/async-params.d.ts +107 -6
  19. package/esm/lib/async-params.d.ts.map +1 -1
  20. package/esm/lib/async-params.js +8 -1
  21. package/esm/lib/async-params.js.map +1 -1
  22. package/esm/lib/async-tool-registry.d.ts +134 -0
  23. package/esm/lib/async-tool-registry.d.ts.map +1 -0
  24. package/esm/lib/async-tool-registry.js +352 -0
  25. package/esm/lib/async-tool-registry.js.map +1 -0
  26. package/esm/lib/async-tools.d.ts +23 -0
  27. package/esm/lib/async-tools.d.ts.map +1 -0
  28. package/esm/lib/async-tools.js +19 -0
  29. package/esm/lib/async-tools.js.map +1 -0
  30. package/esm/lib/conversation-state.d.ts +45 -1
  31. package/esm/lib/conversation-state.d.ts.map +1 -1
  32. package/esm/lib/conversation-state.js +119 -0
  33. package/esm/lib/conversation-state.js.map +1 -1
  34. package/esm/lib/doom-loop.d.ts +533 -0
  35. package/esm/lib/doom-loop.d.ts.map +1 -0
  36. package/esm/lib/doom-loop.js +1221 -0
  37. package/esm/lib/doom-loop.js.map +1 -0
  38. package/esm/lib/hooks-emit.d.ts +50 -0
  39. package/esm/lib/hooks-emit.d.ts.map +1 -0
  40. package/esm/lib/hooks-emit.js +307 -0
  41. package/esm/lib/hooks-emit.js.map +1 -0
  42. package/esm/lib/hooks-manager.d.ts +141 -0
  43. package/esm/lib/hooks-manager.d.ts.map +1 -0
  44. package/esm/lib/hooks-manager.js +282 -0
  45. package/esm/lib/hooks-manager.js.map +1 -0
  46. package/esm/lib/hooks-matchers.d.ts +12 -0
  47. package/esm/lib/hooks-matchers.d.ts.map +1 -0
  48. package/esm/lib/hooks-matchers.js +26 -0
  49. package/esm/lib/hooks-matchers.js.map +1 -0
  50. package/esm/lib/hooks-resolve.d.ts +17 -0
  51. package/esm/lib/hooks-resolve.d.ts.map +1 -0
  52. package/esm/lib/hooks-resolve.js +42 -0
  53. package/esm/lib/hooks-resolve.js.map +1 -0
  54. package/esm/lib/hooks-schemas.d.ts +221 -0
  55. package/esm/lib/hooks-schemas.d.ts.map +1 -0
  56. package/esm/lib/hooks-schemas.js +238 -0
  57. package/esm/lib/hooks-schemas.js.map +1 -0
  58. package/esm/lib/hooks-types.d.ts +178 -0
  59. package/esm/lib/hooks-types.d.ts.map +1 -0
  60. package/esm/lib/hooks-types.js +45 -0
  61. package/esm/lib/hooks-types.js.map +1 -0
  62. package/esm/lib/model-result.d.ts +848 -12
  63. package/esm/lib/model-result.d.ts.map +1 -1
  64. package/esm/lib/model-result.js +4055 -410
  65. package/esm/lib/model-result.js.map +1 -1
  66. package/esm/lib/reusable-stream.d.ts +15 -0
  67. package/esm/lib/reusable-stream.d.ts.map +1 -1
  68. package/esm/lib/reusable-stream.js +25 -0
  69. package/esm/lib/reusable-stream.js.map +1 -1
  70. package/esm/lib/stream-transformers.d.ts +20 -0
  71. package/esm/lib/stream-transformers.d.ts.map +1 -1
  72. package/esm/lib/stream-transformers.js +45 -0
  73. package/esm/lib/stream-transformers.js.map +1 -1
  74. package/esm/lib/tool-check.d.ts +76 -0
  75. package/esm/lib/tool-check.d.ts.map +1 -0
  76. package/esm/lib/tool-check.js +241 -0
  77. package/esm/lib/tool-check.js.map +1 -0
  78. package/esm/lib/tool-concurrency.d.ts +42 -0
  79. package/esm/lib/tool-concurrency.d.ts.map +1 -0
  80. package/esm/lib/tool-concurrency.js +80 -0
  81. package/esm/lib/tool-concurrency.js.map +1 -0
  82. package/esm/lib/tool-context.d.ts +45 -1
  83. package/esm/lib/tool-context.d.ts.map +1 -1
  84. package/esm/lib/tool-context.js +60 -1
  85. package/esm/lib/tool-context.js.map +1 -1
  86. package/esm/lib/tool-executor.d.ts +52 -9
  87. package/esm/lib/tool-executor.d.ts.map +1 -1
  88. package/esm/lib/tool-executor.js +263 -21
  89. package/esm/lib/tool-executor.js.map +1 -1
  90. package/esm/lib/tool-orchestrator.d.ts.map +1 -1
  91. package/esm/lib/tool-orchestrator.js +87 -47
  92. package/esm/lib/tool-orchestrator.js.map +1 -1
  93. package/esm/lib/tool-task.d.ts +157 -0
  94. package/esm/lib/tool-task.d.ts.map +1 -0
  95. package/esm/lib/tool-task.js +206 -0
  96. package/esm/lib/tool-task.js.map +1 -0
  97. package/esm/lib/tool-types.d.ts +609 -58
  98. package/esm/lib/tool-types.d.ts.map +1 -1
  99. package/esm/lib/tool-types.js +72 -10
  100. package/esm/lib/tool-types.js.map +1 -1
  101. package/esm/lib/tool.d.ts +217 -25
  102. package/esm/lib/tool.d.ts.map +1 -1
  103. package/esm/lib/tool.js +210 -1
  104. package/esm/lib/tool.js.map +1 -1
  105. package/package.json +35 -3
@@ -1,13 +1,41 @@
1
1
  import type * as models from '@openrouter/sdk/models';
2
2
  import type { StreamEvents } from '@openrouter/sdk/models';
3
3
  import type { $ZodObject, $ZodShape, $ZodType, infer as zodInfer } from 'zod/v4/core';
4
- import type { ModelResult } from './model-result.js';
4
+ import type { DoomLoopSerializedState } from './doom-loop.js';
5
+ import type { TaskLogLimits, ToolTaskMode, ToolTaskStatus } from './tool-task.js';
5
6
  /**
6
7
  * Tool type enum for enhanced tools
7
8
  */
8
9
  export declare enum ToolType {
9
10
  Function = "function"
10
11
  }
12
+ /**
13
+ * Narrow façade over a running task, handed to check-in `execute` handlers
14
+ * via `turnContext.task`. Lets a custom check read progress, steer, or
15
+ * cancel without exposing the full registry.
16
+ */
17
+ export interface ToolTaskHandle {
18
+ readonly taskId: string;
19
+ readonly toolName: string;
20
+ readonly mode: ToolTaskMode;
21
+ /** Current lifecycle status. */
22
+ status(): ToolTaskStatus;
23
+ /** The `status` check-view summary. */
24
+ statusView(): Record<string, unknown>;
25
+ /** Last `n` log entries (oldest first). */
26
+ tailLogs(n: number): Array<{
27
+ seq: number;
28
+ at: number;
29
+ data: unknown;
30
+ kind: string;
31
+ }>;
32
+ /** Rendered transcript, truncated to `maxChars` keeping the tail. */
33
+ transcript(maxChars?: number): string;
34
+ /** Queue a steering message for the run body (`ctx.onMessage`). */
35
+ send(message: unknown): void;
36
+ /** Cancel the task. Returns true when a working task was cancelled. */
37
+ cancel(reason?: string): boolean;
38
+ }
11
39
  /**
12
40
  * Turn context passed to tool execute functions and async parameter resolution
13
41
  * Contains information about the current conversation state
@@ -19,16 +47,46 @@ export interface TurnContext {
19
47
  numberOfTurns: number;
20
48
  /** The full request being sent to the API (only available during tool execution) */
21
49
  turnRequest?: models.ResponsesRequest;
50
+ /**
51
+ * CHECK CALLS ONLY: lifecycle status of the task the model is checking on.
52
+ * Absent on every other path.
53
+ */
54
+ toolCallStatus?: ToolTaskStatus;
55
+ /**
56
+ * CHECK CALLS ONLY: the data of every retained yield/log from the task's
57
+ * `run` so far, oldest first. Absent on every other path.
58
+ */
59
+ accumulatedYieldedEvents?: unknown[];
60
+ /**
61
+ * CHECK CALLS ONLY: façade over the task being checked (read progress,
62
+ * steer via `send`, cancel). Absent on every other path.
63
+ */
64
+ task?: ToolTaskHandle;
22
65
  }
23
66
  /**
24
- * Extract context schema type from a tool definition
25
- * Returns the inferred type of the tool's contextSchema, or empty object if none
67
+ * Extract context schema type from a tool definition.
68
+ * Returns the inferred type of the tool's `contextSchema`, or
69
+ * `Record<string, never>` when the tool has no (required) contextSchema.
70
+ *
71
+ * Note: optional `contextSchema?: ...` does not match; tools produced by
72
+ * `tool()` only mark `contextSchema` as required when one was provided, so
73
+ * tools without a schema keep `Record<string, never>` map slots.
26
74
  */
27
75
  export type InferToolContext<T> = T extends {
28
76
  function: {
29
- contextSchema: infer S;
77
+ readonly contextSchema?: infer S;
30
78
  };
31
- } ? S extends $ZodType ? zodInfer<S> : Record<string, never> : Record<string, never>;
79
+ } ? [
80
+ S
81
+ ] extends [
82
+ $ZodObject<$ZodShape>
83
+ ] ? $ZodObject<$ZodShape> extends S ? Record<string, never> : zodInfer<S> extends Record<string, unknown> ? zodInfer<S> : zodInfer<S> & Record<string, unknown> : Record<string, never> : Record<string, never>;
84
+ /**
85
+ * Resolve execute-context shape from a contextSchema generic.
86
+ * - Wide/default `$ZodObject<$ZodShape>` (no schema provided) → `Record<string, unknown>`
87
+ * - Concrete schema → its Zod-inferred shape
88
+ */
89
+ export type ContextFromSchema<TCtx extends $ZodObject<$ZodShape>> = $ZodObject<$ZodShape> extends TCtx ? Record<string, unknown> : zodInfer<TCtx> extends Record<string, unknown> ? zodInfer<TCtx> : zodInfer<TCtx> & Record<string, unknown>;
32
90
  /**
33
91
  * Extract tool name from a tool definition
34
92
  */
@@ -56,6 +114,90 @@ export type ToolExecuteContext<TName extends string = string, TContext extends R
56
114
  shared: Readonly<TShared>;
57
115
  /** Mutate the shared context in the store (persists across turns) */
58
116
  setSharedContext(partial: Partial<TShared>): void;
117
+ /**
118
+ * Abort signal for this tool call. Fires when the run is aborted
119
+ * (`callModel`'s `signal` option or `ModelResult.cancel()`), when the
120
+ * tool's `timeoutMs` (or the run-level `toolTimeoutMs`) elapses, or when
121
+ * a background task is cancelled via `cancelTask`. Cooperative: tool
122
+ * bodies should pass it to their own I/O (fetch, child processes, ...).
123
+ * Always present — a never-aborting signal is supplied when no
124
+ * cancellation sources exist.
125
+ */
126
+ readonly signal: AbortSignal;
127
+ /** The id of the tool call being executed (for correlating external work). */
128
+ readonly callId?: string;
129
+ /**
130
+ * The conversation id when a `StateAccessor` is configured. Deferred
131
+ * tools typically hand this to the external system so its webhook can
132
+ * locate the conversation to resume.
133
+ */
134
+ readonly conversationId?: string;
135
+ };
136
+ declare const DEFERRED_BRAND: unique symbol;
137
+ /**
138
+ * Returned by `ctx.defer()`. Returning it from `run` parks the call on a
139
+ * durable external task: the model gets a pending placeholder and the run
140
+ * pauses (`status: 'awaiting_async_tools'`) until the task is resolved via
141
+ * the tool's `.resolve()` / `.fail()` / `.cancel()` methods or
142
+ * `resumeToolResults()` — possibly from another process.
143
+ *
144
+ * Branded with TOutput so a handle from one tool cannot be returned from
145
+ * another whose outputSchema differs.
146
+ *
147
+ * Two distinct markers, deliberately:
148
+ * - `[DEFERRED_BRAND]` is TYPE-ONLY (`declare const` — no runtime value
149
+ * exists) and carries the TOutput brand for compile-time safety.
150
+ * - `__deferred` is the RUNTIME marker {@link isDeferredHandle} checks.
151
+ * `ctx.defer()` (the sole creator, in model-result.ts) always sets it.
152
+ * Changing either without the other breaks deferred-handle detection —
153
+ * keep them in sync.
154
+ */
155
+ export interface DeferredHandle<TOutput = unknown> {
156
+ readonly [DEFERRED_BRAND]: TOutput;
157
+ /** Runtime marker set by `ctx.defer()`; checked by {@link isDeferredHandle}. */
158
+ readonly __deferred: true;
159
+ readonly taskId: string;
160
+ readonly pollAfterMs?: number;
161
+ readonly expiresAt?: number;
162
+ readonly ack?: string | Record<string, unknown>;
163
+ }
164
+ /** Runtime guard for {@link DeferredHandle} (structural — brand is type-only). */
165
+ export declare function isDeferredHandle(value: unknown): value is DeferredHandle;
166
+ /** Options for `ctx.defer()`. */
167
+ export type DeferOptions = {
168
+ /** Poll-interval hint surfaced in the placeholder and to external pollers. */
169
+ pollAfterMs?: number;
170
+ /** Unix ms after which the task is considered expired. */
171
+ expiresAt?: number;
172
+ /** Model-facing note merged into the placeholder (overrides the tool-level `ack`). */
173
+ ack?: string | Record<string, unknown>;
174
+ };
175
+ /**
176
+ * Context passed to a unified tool's `run`. Extends the base execute
177
+ * context with the async-task affordances: deferral, logging, and the
178
+ * steering inbox.
179
+ */
180
+ export type ToolRunContext<TName extends string = string, TContext extends Record<string, unknown> = Record<string, unknown>, TOutput = unknown, TShared extends Record<string, unknown> = Record<string, unknown>> = ToolExecuteContext<TName, TContext, TShared> & {
181
+ /**
182
+ * Park this call on a durable external task and return the handle:
183
+ * `return ctx.defer(ticket.id)`. Only valid on `lifecycle: 'deferred'`
184
+ * tools — throws elsewhere.
185
+ */
186
+ defer(taskId: string, options?: DeferOptions): DeferredHandle<TOutput>;
187
+ /**
188
+ * Append a log entry without yielding — progress for non-generator `run`
189
+ * bodies. Same pipeline as a yield: validated against `eventSchema` when
190
+ * declared, appended to the task log, surfaced as `tool.preliminary_result`.
191
+ */
192
+ log(entry: unknown): void;
193
+ /**
194
+ * Register a steering handler. Messages sent via a check call's
195
+ * `turnContext.task.send(...)` or `ModelResult.sendToTask()` are delivered
196
+ * here (queued until registration).
197
+ */
198
+ onMessage(handler: (message: unknown) => void): void;
199
+ /** The task's id (background/agent: engine-generated, present once escaped). */
200
+ readonly taskId?: string;
59
201
  };
60
202
  /**
61
203
  * Context map keyed by tool name for callModel's `context` option.
@@ -104,14 +246,57 @@ export type NextTurnParamsContext = {
104
246
  * Each function receives the tool's input params and current request context
105
247
  */
106
248
  export type NextTurnParamsFunctions<TInput> = {
107
- [K in keyof NextTurnParamsContext]?: (params: TInput, context: NextTurnParamsContext) => NextTurnParamsContext[K] | Promise<NextTurnParamsContext[K]>;
249
+ [K in keyof NextTurnParamsContext]?: {
250
+ bivarianceHack(params: TInput, context: NextTurnParamsContext): NextTurnParamsContext[K] | Promise<NextTurnParamsContext[K]>;
251
+ }['bivarianceHack'];
108
252
  };
109
253
  /**
110
254
  * Tool-level approval check function type
111
255
  * Receives the tool's input params and turn context
112
256
  * Returns true if approval is required, false otherwise
113
257
  */
114
- export type ToolApprovalCheck<TInput> = (params: TInput, context: TurnContext) => boolean | Promise<boolean>;
258
+ export type ToolApprovalCheck<TInput> = {
259
+ bivarianceHack(params: TInput, context: TurnContext): boolean | Promise<boolean>;
260
+ }['bivarianceHack'];
261
+ /**
262
+ * Function form of {@link ToolLoopKey}: computes the key material that
263
+ * identifies a call of this tool for doom-loop detection.
264
+ *
265
+ * The returned value is canonicalized (RFC 8785) and hashed by the engine —
266
+ * tools declare *what identifies a call*, the engine owns *how it is
267
+ * fingerprinted*. Two calls whose key material canonicalizes identically
268
+ * count as the same call.
269
+ *
270
+ * - Return a focused subset for precise identity: a web-search tool returns
271
+ * its normalized query; a bash tool returns `{ command, cwd, env }`.
272
+ * - Return `null` to exempt THIS call from detection (e.g. a legitimate
273
+ * repeat the tool can recognize from its arguments).
274
+ * - Returning `undefined` (a bare `return;`) is treated as a bug: the
275
+ * engine warns and falls back to the full-arguments identity.
276
+ *
277
+ * Must be pure and deterministic. A throwing `loopKey` falls back to the
278
+ * full-arguments fingerprint (detection must never take down a run).
279
+ */
280
+ export type ToolLoopKeyFn<TInput> = {
281
+ bivarianceHack(params: TInput): unknown;
282
+ }['bivarianceHack'];
283
+ /**
284
+ * Doom-loop identity declaration for a tool (see the `doomLoop` option on
285
+ * `callModel`). Computed like every other tool hook — a plain function over
286
+ * the call's validated arguments:
287
+ *
288
+ * - function — computes key material per call ({@link ToolLoopKeyFn}), e.g.
289
+ * `({ command, cwd }) => ({ command, cwd })` for a bash tool. Returning
290
+ * `null` exempts individual calls (a legitimate repeat the tool can
291
+ * recognize from its arguments).
292
+ * - field-name array — the declarative subset identifying a call, e.g.
293
+ * `['command', 'cwd']` for a bash tool. Data, not code: serializable, so
294
+ * it survives tool caches and can be advertised over the MCP wire.
295
+ * - `false` — this tool is statically exempt (repetition is its job, e.g. a
296
+ * status poller).
297
+ * - absent — the full validated arguments object is the identity.
298
+ */
299
+ export type ToolLoopKey<TInput> = ToolLoopKeyFn<TInput> | readonly string[] | false;
115
300
  /**
116
301
  * Content item types for tool output to model.
117
302
  * These match the Responses API format for multimodal tool outputs.
@@ -142,35 +327,89 @@ export type ToModelOutputResult = {
142
327
  * @template TInput - The tool's input type
143
328
  * @template TOutput - The tool's output type
144
329
  */
145
- export type ToModelOutputFunction<TInput, TOutput> = (params: {
146
- output: TOutput;
147
- input: TInput;
148
- }) => ToModelOutputResult | Promise<ToModelOutputResult>;
330
+ export type ToModelOutputFunction<TInput, TOutput> = {
331
+ bivarianceHack(params: {
332
+ output: TOutput;
333
+ input: TInput;
334
+ }): ToModelOutputResult | Promise<ToModelOutputResult>;
335
+ }['bivarianceHack'];
149
336
  /**
150
337
  * Base tool function interface with inputSchema
151
338
  * @template TInput - Zod schema for tool input
339
+ * @template TCtx - Zod schema for tool context (optional; default = erased wide type)
152
340
  */
153
- export interface BaseToolFunction<TInput extends $ZodObject<$ZodShape>> {
341
+ export interface BaseToolFunction<TInput extends $ZodObject<$ZodShape>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> {
154
342
  name: string;
155
343
  description?: string;
156
344
  inputSchema: TInput;
157
- /** Zod schema declaring the context data this tool needs */
158
- contextSchema?: $ZodObject<$ZodShape>;
345
+ /**
346
+ * Whether providers should enforce strict schema adherence when generating
347
+ * this tool's call arguments (OpenAI structured-outputs style). Serialized
348
+ * onto the wire tool definition; `null`/absent leaves provider default.
349
+ *
350
+ * OpenAI-style strict mode requires every declared object property to be
351
+ * listed in `required`. Use Zod `.nullable()` for values that may be absent
352
+ * conceptually; `.optional()` produces a schema those providers may reject.
353
+ * The SDK forwards the caller's schema unchanged.
354
+ */
355
+ strict?: boolean | null;
356
+ /**
357
+ * Zod schema declaring the context data this tool needs.
358
+ * `readonly` keeps TCtx covariant so tools carrying a concrete schema stay
359
+ * assignable to the wide `Tool` union.
360
+ */
361
+ readonly contextSchema?: TCtx;
159
362
  nextTurnParams?: NextTurnParamsFunctions<zodInfer<TInput>>;
160
363
  /**
161
364
  * Whether this tool requires human approval before execution
162
365
  * Can be a boolean or an async function that receives the tool's input params and context
163
366
  */
164
367
  requireApproval?: boolean | ToolApprovalCheck<zodInfer<TInput>>;
368
+ /**
369
+ * Doom-loop identity for this tool's calls — see {@link ToolLoopKey}.
370
+ * A computed function over the call's arguments, a field-name array
371
+ * (serializable — used by MCP tool caches), or `false` (exempt).
372
+ * Absent: the full validated arguments object is the identity.
373
+ */
374
+ loopKey?: ToolLoopKey<zodInfer<TInput>>;
375
+ /**
376
+ * Deadline for one execution of this tool, in milliseconds. When it
377
+ * elapses the round stops waiting: the model receives a
378
+ * `{ error, code: 'tool_timeout' }` output immediately and the tool's
379
+ * `ctx.signal` is aborted. The timeout bounds the round's WAIT, not the
380
+ * tool body's execution — a body that ignores its signal keeps running
381
+ * detached (its result is discarded). Overrides the run-level
382
+ * `toolTimeoutMs` default.
383
+ */
384
+ timeoutMs?: number;
385
+ /**
386
+ * Maximum simultaneous in-flight executions of THIS tool across the run
387
+ * — round-synchronous and background alike (background bodies re-acquire
388
+ * the gate for their full duration). Typically encodes an external
389
+ * constraint (one DB connection, a rate-limited API key). Excess calls
390
+ * queue FIFO. Queue wait does NOT count against `timeoutMs` for
391
+ * round-synchronous calls (the deadline starts once the slot is held);
392
+ * background tasks queue against their own registry-tracked timeout.
393
+ * Unbounded when absent.
394
+ */
395
+ maxConcurrency?: number;
165
396
  }
166
397
  /**
167
398
  * Regular tool with synchronous or asynchronous execute function and optional outputSchema
168
399
  * @template TContext - Shape of the tool's context (inferred from contextSchema)
169
400
  * @template TName - The tool's literal name string
170
401
  */
171
- export interface ToolFunctionWithExecute<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string> extends BaseToolFunction<TInput> {
402
+ export interface ToolFunctionWithExecute<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> extends BaseToolFunction<TInput, TCtx> {
172
403
  outputSchema?: TOutput;
173
- execute: (params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>) => Promise<zodInfer<TOutput>> | zodInfer<TOutput>;
404
+ /**
405
+ * Absent on regular tools. Declared as `undefined`-only so
406
+ * `UnifiedToolFunction` (which requires `run` and `lifecycle`) is
407
+ * structurally DISJOINT from this interface — type-guard narrowing must
408
+ * not keep unified tools in the regular-execute union.
409
+ */
410
+ readonly run?: undefined;
411
+ readonly lifecycle?: undefined;
412
+ execute(params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>): Promise<zodInfer<TOutput>> | zodInfer<TOutput>;
174
413
  /** Convert tool execution output to model-facing output */
175
414
  toModelOutput?: ToModelOutputFunction<zodInfer<TInput>, zodInfer<TOutput>>;
176
415
  }
@@ -196,17 +435,17 @@ export interface ToolFunctionWithExecute<TInput extends $ZodObject<$ZodShape>, T
196
435
  * }
197
436
  * ```
198
437
  */
199
- export interface ToolFunctionWithGenerator<TInput extends $ZodObject<$ZodShape>, TEvent extends $ZodType = $ZodType<unknown>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string> extends BaseToolFunction<TInput> {
438
+ export interface ToolFunctionWithGenerator<TInput extends $ZodObject<$ZodShape>, TEvent extends $ZodType = $ZodType<unknown>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> extends BaseToolFunction<TInput, TCtx> {
200
439
  eventSchema: TEvent;
201
440
  outputSchema: TOutput;
202
- execute: (params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>) => AsyncGenerator<zodInfer<TEvent> | zodInfer<TOutput>, zodInfer<TOutput> | undefined>;
441
+ execute(params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>): AsyncGenerator<zodInfer<TEvent> | zodInfer<TOutput>, zodInfer<TOutput> | undefined>;
203
442
  /** Convert tool execution output to model-facing output */
204
443
  toModelOutput?: ToModelOutputFunction<zodInfer<TInput>, zodInfer<TOutput>>;
205
444
  }
206
445
  /**
207
446
  * Manual tool without execute function - requires manual handling by developer
208
447
  */
209
- export interface ManualToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>> extends BaseToolFunction<TInput> {
448
+ export interface ManualToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> extends BaseToolFunction<TInput, TCtx> {
210
449
  outputSchema?: TOutput;
211
450
  }
212
451
  /**
@@ -221,7 +460,7 @@ export interface ManualToolFunction<TInput extends $ZodObject<$ZodShape>, TOutpu
221
460
  * It receives the caller-supplied raw result and returns the value sent to the model.
222
461
  * Throwing surfaces as a tool error to the model.
223
462
  */
224
- export interface HITLToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string> extends BaseToolFunction<TInput> {
463
+ export interface HITLToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> extends BaseToolFunction<TInput, TCtx> {
225
464
  /**
226
465
  * Required for HITL tools. Used to validate both the `onToolCalled` return
227
466
  * value (when non-null) and the caller-supplied response that comes back via
@@ -229,43 +468,160 @@ export interface HITLToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput
229
468
  * `onResponseReceived` or passed through directly when no hook is defined.
230
469
  */
231
470
  outputSchema: TOutput;
232
- onToolCalled: (params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>) => Promise<zodInfer<TOutput> | null> | zodInfer<TOutput> | null;
233
- onResponseReceived?: (rawResult: unknown, context?: ToolExecuteContext<TName, TContext>) => Promise<zodInfer<TOutput>> | zodInfer<TOutput>;
471
+ onToolCalled(params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>): Promise<zodInfer<TOutput> | null> | zodInfer<TOutput> | null;
472
+ onResponseReceived?(rawResult: unknown, context?: ToolExecuteContext<TName, TContext>): Promise<zodInfer<TOutput>> | zodInfer<TOutput>;
473
+ toModelOutput?: ToModelOutputFunction<zodInfer<TInput>, zodInfer<TOutput>>;
474
+ }
475
+ /**
476
+ * Model-facing acknowledgement for an async tool's placeholder output.
477
+ * A string becomes the placeholder's `note`; an object is merged into the
478
+ * placeholder payload; a function computes either from the call's input.
479
+ */
480
+ export type AsyncToolAck<TInput> = string | Record<string, unknown> | {
481
+ bivarianceHack(input: TInput): string | Record<string, unknown>;
482
+ }['bivarianceHack'];
483
+ /** How a unified tool's execution relates to the tool round. */
484
+ export type ToolLifecycle = 'sync' | 'background' | 'deferred';
485
+ /**
486
+ * Check-in configuration for a long-running tool. The model interacts with
487
+ * running tasks through the SINGLE universal `task` tool (`action:
488
+ * 'check' | 'steer' | 'result' | 'cancel'`, addressed by `taskId`); the
489
+ * engine dispatches those calls to the owning tool's config here — so the
490
+ * wire surface stays constant while the implementation stays tool-specific.
491
+ *
492
+ * - `true` — explicit opt-in to the SDK default handling (long-running
493
+ * tools get it even without this).
494
+ * - `schema` — validates the task tool's free-form `params` object when the
495
+ * model passes one to this tool's custom handler.
496
+ * - `execute` — custom check handler, replacing the SDK default for
497
+ * `action: 'check'` calls targeting this tool's tasks. Receives the
498
+ * validated `params` and a TurnContext populated with `toolCallStatus`,
499
+ * `accumulatedYieldedEvents`, and the `task` handle. Its return value is
500
+ * the tool output the model sees.
501
+ */
502
+ export type ToolCheckConfig<TCheckParams = Record<string, unknown>> = true | {
503
+ /**
504
+ * Validation schema for the model-supplied `params` on check calls.
505
+ * SECURITY: `params` is MODEL INPUT (and models can be steered by
506
+ * injected tool/web content). Without a schema the handler receives
507
+ * the raw record — declare one whenever `execute` forwards params
508
+ * into steering/cancel or any other side effect, exactly as you
509
+ * would validate a tool's inputSchema.
510
+ */
511
+ schema?: $ZodObject<$ZodShape>;
512
+ execute?: {
513
+ bivarianceHack(params: TCheckParams, turnContext: TurnContext): unknown | Promise<unknown>;
514
+ }['bivarianceHack'];
515
+ };
516
+ /**
517
+ * The unified `run`-based tool kind. One shape covers every lifecycle:
518
+ *
519
+ * - `'sync'` (default): `run` is awaited in the round, exactly like `execute`.
520
+ * - `'background'`: the loop does not wait. Work settling within `graceMs`
521
+ * behaves like a sync call; otherwise the model receives a pending
522
+ * placeholder, the loop continues, and the return value is injected as a
523
+ * `tool_task_result` envelope when it settles.
524
+ * - `'deferred'`: `run` may return `ctx.defer(taskId)` to park the call on a
525
+ * durable external task — the run pauses (`awaiting_async_tools`) until
526
+ * the task is resolved via `.resolve()`/`.fail()`/`.cancel()` or
527
+ * `resumeToolResults()`, possibly from another process. Returning a plain
528
+ * value resolves immediately (typed fast path).
529
+ *
530
+ * `run` is an async function or an async generator. Generator yields become
531
+ * the task's LOG entries (feeding check-in views and
532
+ * `tool.preliminary_result` events); the generator's RETURN value is the
533
+ * final result, validated against `outputSchema`. (This is stricter than
534
+ * legacy `execute` generators, which accept a final yield as the result.)
535
+ */
536
+ export interface UnifiedToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TEvent extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> extends BaseToolFunction<TInput, TCtx> {
537
+ /** Discriminator against every legacy kind. */
538
+ readonly lifecycle: ToolLifecycle;
539
+ /**
540
+ * Required for 'background' | 'deferred' (results settle after the round,
541
+ * possibly in another process). Optional for 'sync' (inferred from run).
542
+ */
543
+ outputSchema?: TOutput;
544
+ /** Validates `run` yields / `ctx.log()` entries when declared. */
545
+ eventSchema?: TEvent;
546
+ /** Model-facing acknowledgement merged into the pending placeholder. */
547
+ ack?: AsyncToolAck<zodInfer<TInput>>;
548
+ /**
549
+ * Background only: hold the round this long (ms) before emitting a
550
+ * placeholder. Work settling in-window produces a plain synchronous
551
+ * output. Default 250; `0` always placeholders.
552
+ */
553
+ graceMs?: number;
554
+ /** Deferred only: default poll-interval hint for tasks from this tool. */
555
+ pollAfterMs?: number;
556
+ /** Check-in configuration — see {@link ToolCheckConfig}. */
557
+ check?: ToolCheckConfig;
558
+ /** Set by `tool.agent()`: marks the run as a child-conversation driver. */
559
+ readonly kind?: 'agent';
560
+ /** Per-task log ring-buffer overrides. */
561
+ logLimits?: Partial<TaskLogLimits>;
562
+ run(params: zodInfer<TInput>, context?: ToolRunContext<TName, TContext, zodInfer<TOutput>>): Promise<zodInfer<TOutput> | DeferredHandle<zodInfer<TOutput>>> | zodInfer<TOutput> | DeferredHandle<zodInfer<TOutput>> | AsyncGenerator<zodInfer<TEvent>, zodInfer<TOutput> | DeferredHandle<zodInfer<TOutput>>>;
563
+ /**
564
+ * Convert tool execution output to model-facing output.
565
+ *
566
+ * ROUND-SYNCHRONOUS RESULTS ONLY: applies to `lifecycle: 'sync'` results
567
+ * and background work that settles inside its grace window — the paths
568
+ * that produce a `function_call_output`. Late-delivered results
569
+ * (background past the grace window, deferred completions) arrive as a
570
+ * `tool_task_result` JSON envelope in a user-role message, which cannot
571
+ * carry this mapper's content-item arrays; they deliver the validated
572
+ * output verbatim.
573
+ */
234
574
  toModelOutput?: ToModelOutputFunction<zodInfer<TInput>, zodInfer<TOutput>>;
575
+ /** Absent on unified tools — keeps them disjoint from legacy kinds. */
576
+ readonly execute?: undefined;
577
+ readonly onToolCalled?: undefined;
235
578
  }
236
579
  /**
237
580
  * Tool with execute function (regular or generator)
581
+ * @template TCtx - The concrete contextSchema type when one was provided to `tool()`
238
582
  */
239
- export type ToolWithExecute<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>> = {
583
+ export type ToolWithExecute<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> = {
240
584
  type: ToolType.Function;
241
- function: ToolFunctionWithExecute<TInput, TOutput, TContext>;
585
+ function: ToolFunctionWithExecute<TInput, TOutput, TContext, string, TCtx>;
242
586
  };
243
587
  /**
244
588
  * Tool with generator execute function
589
+ * @template TCtx - The concrete contextSchema type when one was provided to `tool()`
245
590
  */
246
- export type ToolWithGenerator<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TEvent extends $ZodType = $ZodType<unknown>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>> = {
591
+ export type ToolWithGenerator<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TEvent extends $ZodType = $ZodType<unknown>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> = {
247
592
  type: ToolType.Function;
248
- function: ToolFunctionWithGenerator<TInput, TEvent, TOutput, TContext>;
593
+ function: ToolFunctionWithGenerator<TInput, TEvent, TOutput, TContext, string, TCtx>;
249
594
  };
250
595
  /**
251
596
  * Tool without execute function (manual handling)
597
+ * @template TCtx - The concrete contextSchema type when one was provided to `tool()`
252
598
  */
253
- export type ManualTool<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>> = {
599
+ export type ManualTool<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> = {
254
600
  type: ToolType.Function;
255
- function: ManualToolFunction<TInput, TOutput>;
601
+ function: ManualToolFunction<TInput, TOutput, TCtx>;
256
602
  };
257
603
  /**
258
604
  * Human-in-the-loop tool (with onToolCalled / onResponseReceived hooks)
605
+ * @template TCtx - The concrete contextSchema type when one was provided to `tool()`
259
606
  */
260
- export type HITLTool<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>> = {
607
+ export type HITLTool<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> = {
261
608
  type: ToolType.Function;
262
- function: HITLToolFunction<TInput, TOutput, TContext>;
609
+ function: HITLToolFunction<TInput, TOutput, TContext, string, TCtx>;
263
610
  };
264
611
  /**
265
- * Union type of all client-executed tool shapes (function, generator, manual, HITL).
266
- * These run in the user's process via the agent SDK's tool execution loop.
612
+ * Unified tool wrapper (`tool()` with `run`)
613
+ * @template TCtx - The concrete contextSchema type when one was provided
267
614
  */
268
- export type ClientTool = ToolWithExecute<$ZodObject<$ZodShape>, $ZodType<unknown>> | ToolWithGenerator<$ZodObject<$ZodShape>, $ZodType<unknown>, $ZodType<unknown>> | ManualTool<$ZodObject<$ZodShape>, $ZodType<unknown>> | HITLTool<$ZodObject<$ZodShape>, $ZodType<unknown>>;
615
+ export type UnifiedTool<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TEvent extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>> = {
616
+ type: ToolType.Function;
617
+ function: UnifiedToolFunction<TInput, TOutput, TEvent, TContext, string, TCtx>;
618
+ };
619
+ /**
620
+ * Union type of all client-executed tool shapes (function, generator, manual,
621
+ * HITL, unified `run`). These run in the user's process via the agent SDK's
622
+ * tool execution loop.
623
+ */
624
+ export type ClientTool = ToolWithExecute<$ZodObject<$ZodShape>, $ZodType<unknown>> | ToolWithGenerator<$ZodObject<$ZodShape>, $ZodType<unknown>, $ZodType<unknown>> | ManualTool<$ZodObject<$ZodShape>, $ZodType<unknown>> | HITLTool<$ZodObject<$ZodShape>, $ZodType<unknown>> | UnifiedTool<$ZodObject<$ZodShape>, $ZodType<unknown>, $ZodType<unknown>>;
269
625
  /**
270
626
  * Config payload for an OpenRouter server-executed tool. Derived directly
271
627
  * from the SDK's request-tool union so new server tools flow through with
@@ -410,16 +766,52 @@ export type InferToolEventsUnion<T extends readonly Tool[]> = {
410
766
  * branch structurally, so `tool._brand` is reachable without a cast.
411
767
  */
412
768
  export declare function isServerTool(tool: Tool): tool is ServerTool;
769
+ /**
770
+ * A client tool additionally branded as originating from an MCP server. The
771
+ * `_mcp` marker is purely informational: it does NOT change how the tool is
772
+ * executed (MCP tools keep their local `execute` fn and run through the normal
773
+ * client-tool path) or serialized (they go on the wire as `type: 'function'`).
774
+ * It exists only so result types can discriminate MCP results — whose output
775
+ * schema is `unknown` at compile time — from precisely-typed client tools,
776
+ * preventing a single MCP tool from collapsing the whole result union.
777
+ */
778
+ export type McpBranded<T extends Tool = Tool> = T & {
779
+ readonly _mcp: true;
780
+ };
781
+ /**
782
+ * Type guard: true if the tool carries the additive MCP brand (see
783
+ * {@link McpBranded}). Structural check on `_mcp`, so no cast is needed.
784
+ */
785
+ export declare function isMcpTool(tool: Tool): tool is McpBranded;
413
786
  /**
414
787
  * Type guard: true if the tool is a client-executed tool (function, generator, or manual).
415
788
  */
416
789
  export declare function isClientTool(tool: Tool): tool is ClientTool;
417
790
  /**
418
- * Type guard to check if a tool has an execute function
791
+ * Type guard to check if a tool is a unified `run`-based tool
792
+ */
793
+ export declare function isUnifiedTool(tool: Tool): tool is UnifiedTool;
794
+ /**
795
+ * True when the tool can outlive its round: a unified tool with a
796
+ * non-'sync' lifecycle. Drives check-schema generation and placeholder
797
+ * wording.
798
+ */
799
+ export declare function isLongRunningTool(tool: Tool): boolean;
800
+ /**
801
+ * True when the tool is an agent tool (`tool.agent()`): a unified tool
802
+ * whose run drives a child conversation.
803
+ */
804
+ export declare function isAgentTool(tool: Tool): boolean;
805
+ /**
806
+ * Type guard to check if a tool has an execute function (regular or
807
+ * generator). Unified `run` tools are excluded — they have their own
808
+ * dispatch and this guard's predicate would misclassify them.
419
809
  */
420
810
  export declare function hasExecuteFunction(tool: Tool): tool is ToolWithExecute | ToolWithGenerator;
421
811
  /**
422
- * Type guard to check if a tool uses a generator (has eventSchema)
812
+ * Type guard to check if a tool uses a generator (has eventSchema).
813
+ * Unified tools may declare an `eventSchema` for run yields but are not
814
+ * legacy generator tools.
423
815
  */
424
816
  export declare function isGeneratorTool(tool: Tool): tool is ToolWithGenerator;
425
817
  /**
@@ -427,7 +819,7 @@ export declare function isGeneratorTool(tool: Tool): tool is ToolWithGenerator;
427
819
  */
428
820
  export declare function isRegularExecuteTool(tool: Tool): tool is ToolWithExecute;
429
821
  /**
430
- * Type guard to check if a tool is a manual tool (no execute and no onToolCalled)
822
+ * Type guard to check if a tool is a manual tool (no execute, no onToolCalled, no run)
431
823
  */
432
824
  export declare function isManualTool(tool: Tool): tool is ManualTool;
433
825
  /**
@@ -435,11 +827,12 @@ export declare function isManualTool(tool: Tool): tool is ManualTool;
435
827
  */
436
828
  export declare function isHITLTool(tool: Tool): tool is HITLTool;
437
829
  /**
438
- * Type guard: true if the tool can be auto-resolved within a turn — either
439
- * through a client execute/generator function or through a HITL onToolCalled hook.
440
- * Returns false for manual tools (which always pause) and server tools.
830
+ * Type guard: true if the tool can be auto-resolved within a turn — through a
831
+ * client execute/generator function, a HITL onToolCalled hook, or a unified
832
+ * `run` (which always produces at least a placeholder output). Returns false
833
+ * for manual tools (which always pause) and server tools.
441
834
  */
442
- export declare function isAutoResolvableTool(tool: Tool): tool is ToolWithExecute | ToolWithGenerator | HITLTool;
835
+ export declare function isAutoResolvableTool(tool: Tool): tool is ToolWithExecute | ToolWithGenerator | HITLTool | UnifiedTool;
443
836
  /**
444
837
  * Parsed tool call from API response
445
838
  * @template T - The tool type to infer argument types from
@@ -454,16 +847,53 @@ export interface ParsedToolCall<T extends Tool> {
454
847
  arguments: InferToolInput<T>;
455
848
  }
456
849
  /**
457
- * Result of tool execution
850
+ * Result of tool execution.
851
+ *
852
+ * The `_mcp` brand is tested BEFORE the execute/generator branch: an MCP tool
853
+ * is structurally also a `ToolWithExecute`, so checking the brand last would let
854
+ * its `unknown` output flow through and collapse the result union. Brand-first
855
+ * isolates MCP results as `unknown` under `source: 'mcp'` while every other tool
856
+ * keeps its precise, schema-derived result under `source: 'client'`. Consumers
857
+ * narrow on `source` (narrowing on the `toolName` literal alone does not exclude
858
+ * the MCP branch, whose `toolName` is `string`).
859
+ *
458
860
  * @template T - The tool type to infer result types from
459
861
  */
460
862
  export interface ToolExecutionResult<T extends Tool> {
461
863
  toolCallId: string;
462
- toolName: string;
463
- result: T extends ToolWithExecute<$ZodObject<$ZodShape>, infer O> | ToolWithGenerator<$ZodObject<$ZodShape>, $ZodType<unknown>, infer O> ? zodInfer<O> : unknown;
864
+ toolName: T extends {
865
+ function: {
866
+ name: infer N extends string;
867
+ };
868
+ } ? N : string;
869
+ source: ToolSource<T>;
870
+ result: T extends {
871
+ readonly _mcp: true;
872
+ } ? unknown : [
873
+ Tool
874
+ ] extends [
875
+ T
876
+ ] ? unknown : T extends ToolWithExecute<$ZodObject<$ZodShape>, infer O> | ToolWithGenerator<$ZodObject<$ZodShape>, $ZodType<unknown>, infer O> ? zodInfer<O> : unknown;
464
877
  preliminaryResults?: T extends ToolWithGenerator<$ZodObject<$ZodShape>, infer E> ? zodInfer<E>[] : undefined;
465
878
  error?: Error;
466
879
  }
880
+ /**
881
+ * The `source` discriminant for a tool result. A specific MCP-branded tool is
882
+ * `'mcp'`; a specific client tool is `'client'`; the wide `Tool` (used by the
883
+ * internal executor before the concrete tool type is known) is the full union,
884
+ * so a runtime-computed `'client' | 'mcp'` assigns into it.
885
+ */
886
+ export type ToolSource<T extends Tool> = [
887
+ Tool
888
+ ] extends [
889
+ T
890
+ ] ? 'client' | 'mcp' : [
891
+ T
892
+ ] extends [
893
+ {
894
+ readonly _mcp: true;
895
+ }
896
+ ] ? 'mcp' : 'client';
467
897
  /**
468
898
  * Warning from step execution
469
899
  */
@@ -521,17 +951,6 @@ export type StopCondition<TTools extends readonly Tool[] = readonly Tool[]> = (o
521
951
  * Can be a single condition or array of conditions
522
952
  */
523
953
  export type StopWhen<TTools extends readonly Tool[] = readonly Tool[]> = StopCondition<TTools> | ReadonlyArray<StopCondition<TTools>>;
524
- /**
525
- * Result of executeTools operation
526
- */
527
- export interface ExecuteToolsResult<TTools extends readonly Tool[]> {
528
- finalResponse: ModelResult<TTools>;
529
- allResponses: ModelResult<TTools>[];
530
- toolResults: Map<string, {
531
- result: unknown;
532
- preliminaryResults?: unknown[];
533
- }>;
534
- }
535
954
  /**
536
955
  * Standard tool format for OpenRouter API (JSON Schema based)
537
956
  * Matches ResponsesRequestToolFunction structure
@@ -564,6 +983,13 @@ export type ToolPreliminaryResultEvent<TEvent = unknown> = {
564
983
  export type ToolResultEvent<TResult = unknown, TPreliminaryResults = unknown> = {
565
984
  type: 'tool.result';
566
985
  toolCallId: string;
986
+ /**
987
+ * Origin of the tool: `'mcp'` for tools wrapped from a remote MCP server
988
+ * (whose `result` is `unknown`), `'client'` for locally-defined tools. Lets
989
+ * consumers discriminate so an MCP result's `unknown` doesn't force callers to
990
+ * treat every tool result as untyped.
991
+ */
992
+ source: 'client' | 'mcp';
567
993
  result: TResult;
568
994
  timestamp: number;
569
995
  preliminaryResults?: TPreliminaryResults[];
@@ -578,6 +1004,38 @@ export type ToolCallOutputEvent = {
578
1004
  output: models.FunctionCallOutputItem;
579
1005
  timestamp: number;
580
1006
  };
1007
+ /**
1008
+ * Emitted when an async tool call escapes the round: a background tool's
1009
+ * execute outlived its grace window, or a deferred tool's start returned a
1010
+ * task handle. The model has received a pending placeholder output.
1011
+ */
1012
+ export type ToolAsyncStartedEvent = {
1013
+ type: 'tool.async_started';
1014
+ toolCallId: string;
1015
+ toolName: string;
1016
+ taskId: string;
1017
+ mode: ToolTaskMode;
1018
+ /** The model-facing acknowledgement carried in the placeholder, if any. */
1019
+ ack?: unknown;
1020
+ timestamp: number;
1021
+ };
1022
+ /**
1023
+ * Emitted when an async tool task settles — completed, failed, cancelled,
1024
+ * timed out, or expired. `delivery` reports how (or whether) the outcome
1025
+ * reached the model: `'injected'` into this run's conversation,
1026
+ * `'pending_resume'` recorded on state for the next run, or `'dropped'`
1027
+ * (run ended under `onRunEnd: 'detach'`).
1028
+ */
1029
+ export type ToolAsyncSettledEvent<TResult = unknown> = {
1030
+ type: 'tool.async_settled';
1031
+ toolCallId: string;
1032
+ taskId: string;
1033
+ status: 'completed' | 'failed' | 'cancelled' | 'timed_out' | 'expired';
1034
+ result?: TResult;
1035
+ error?: string;
1036
+ delivery: 'injected' | 'pending_resume' | 'dropped';
1037
+ timestamp: number;
1038
+ };
581
1039
  /**
582
1040
  * Turn start event emitted at the beginning of each API turn
583
1041
  * Turn 0 is the initial request, subsequent turns follow tool execution
@@ -602,7 +1060,15 @@ export type TurnEndEvent = {
602
1060
  * @template TEvent - The event type from generator tools
603
1061
  * @template TResult - The result type from tool execution
604
1062
  */
605
- export type ResponseStreamEvent<TEvent = unknown, TResult = unknown> = StreamEvents | ToolPreliminaryResultEvent<TEvent> | ToolResultEvent<TResult, TEvent> | ToolCallOutputEvent | TurnStartEvent | TurnEndEvent;
1063
+ export type ResponseStreamEvent<TEvent = unknown, TResult = unknown> = StreamEvents | ToolPreliminaryResultEvent<TEvent> | ToolResultEvent<TResult, TEvent> | ToolCallOutputEvent | ToolAsyncStartedEvent | ToolAsyncSettledEvent<TResult> | TurnStartEvent | TurnEndEvent;
1064
+ /**
1065
+ * Type guard to check if an event is a tool async started event
1066
+ */
1067
+ export declare function isToolAsyncStartedEvent(event: ResponseStreamEvent): event is ToolAsyncStartedEvent;
1068
+ /**
1069
+ * Type guard to check if an event is a tool async settled event
1070
+ */
1071
+ export declare function isToolAsyncSettledEvent<TResult = unknown>(event: ResponseStreamEvent<unknown, TResult>): event is ToolAsyncSettledEvent<TResult>;
606
1072
  /**
607
1073
  * Type guard to check if an event is a tool preliminary result event
608
1074
  */
@@ -698,19 +1164,86 @@ export interface PartialResponse<TTools extends readonly Tool[] = readonly Tool[
698
1164
  * - `awaiting_hitl`: one or more HITL tools returned `null` from `onToolCalled`,
699
1165
  * pausing execution so the caller can supply outputs for the paused calls
700
1166
  * before resuming
701
- */
702
- export type ConversationStatus = 'complete' | 'interrupted' | 'awaiting_approval' | 'awaiting_hitl' | 'in_progress';
1167
+ * - `awaiting_client_tools`: one or more manual (`execute: false` / no execute
1168
+ * fn) tool calls are unresolved; the loop stopped so the caller can execute
1169
+ * them client-side and continue. Distinct from `awaiting_hitl` — HITL tools
1170
+ * have an `onToolCalled` hook; manual tools do not.
1171
+ * - `awaiting_async_tools`: one or more deferred tools (`tool.deferred`)
1172
+ * started a durable external task; the loop paused until the task is
1173
+ * resolved via the tool's `.resolve()` / `.fail()` / `.cancel()` methods
1174
+ * (or `resumeToolResults()`), possibly from a different process. Distinct
1175
+ * from `awaiting_client_tools` — the tool DID execute (its `start` ran and
1176
+ * a placeholder output was persisted); only its final result is pending.
1177
+ */
1178
+ export type ConversationStatus = 'complete' | 'interrupted' | 'awaiting_approval' | 'awaiting_hitl' | 'awaiting_client_tools' | 'awaiting_async_tools' | 'in_progress';
1179
+ export type { ToolTaskStatus } from './tool-task.js';
1180
+ /**
1181
+ * A pending (or settled) async tool task tracked on
1182
+ * {@link ConversationState.pendingAsyncTools}. One entry per background /
1183
+ * deferred call that produced a placeholder output.
1184
+ */
1185
+ export interface PendingAsyncTool {
1186
+ /** The originating `function_call`'s call id. */
1187
+ callId: string;
1188
+ /** Durable task id (deferred: caller-supplied; background/agent: generated). */
1189
+ taskId: string;
1190
+ /** The tool's name. */
1191
+ name: string;
1192
+ /** How the task escapes the round. */
1193
+ mode: ToolTaskMode;
1194
+ /** Current lifecycle status (MCP-Tasks-compatible vocabulary). */
1195
+ status: ToolTaskStatus;
1196
+ /** Unix ms when the task started. */
1197
+ startedAt: number;
1198
+ /** Unix ms after which the task is considered expired. */
1199
+ expiresAt?: number;
1200
+ /** Poll-interval hint surfaced to the model and external pollers. */
1201
+ pollAfterMs?: number;
1202
+ /**
1203
+ * Set on a background task left running when the run ended under
1204
+ * `onRunEnd: 'detach'` — its result will never be delivered.
1205
+ */
1206
+ orphaned?: boolean;
1207
+ /**
1208
+ * The most recent log entry, truncated (~200 chars) — the one piece of
1209
+ * progress that survives a process restart. Additive within
1210
+ * ConversationState version 1.
1211
+ */
1212
+ lastLog?: {
1213
+ at: number;
1214
+ text: string;
1215
+ };
1216
+ }
703
1217
  /**
704
1218
  * State for multi-turn conversations with persistence and approval gates
705
1219
  * @template TTools - The tools array type for proper type inference
706
1220
  */
707
1221
  export interface ConversationState<TTools extends readonly Tool[] = readonly Tool[]> {
1222
+ /**
1223
+ * Serialization-contract version for this state blob.
1224
+ *
1225
+ * Optional so legacy (pre-version-field) states remain assignable. Absence is
1226
+ * treated as version `1` by {@link deserializeConversationState}. Consumers
1227
+ * should treat serialized JSON as opaque: additive fields within a major
1228
+ * version, migrations applied inside `deserializeConversationState` on bump.
1229
+ */
1230
+ version?: number;
708
1231
  /** Unique identifier for this conversation */
709
1232
  id: string;
710
1233
  /** Full message history */
711
1234
  messages: models.InputsUnion;
712
1235
  /** Previous response ID for chaining (OpenRouter server-side optimization) */
713
1236
  previousResponseId?: string;
1237
+ /**
1238
+ * RFC 8785 canonical key of the forced `toolChoice` most recently consumed
1239
+ * by the active logical run. Persisted across approval, HITL, client-tool,
1240
+ * and async-tool pauses so an unchanged forced choice stays relaxed after
1241
+ * resume while a newly resolved forced value can re-arm.
1242
+ *
1243
+ * Cleared after an unforced caller choice is successfully dispatched or the
1244
+ * run reaches a terminal exit.
1245
+ */
1246
+ consumedForcedToolChoiceKey?: string;
714
1247
  /** Tool calls awaiting human approval */
715
1248
  pendingToolCalls?: Array<ParsedToolCall<TTools[number]>>;
716
1249
  /** Tool results executed but not yet sent to the model */
@@ -719,6 +1252,25 @@ export interface ConversationState<TTools extends readonly Tool[] = readonly Too
719
1252
  partialResponse?: PartialResponse<TTools>;
720
1253
  /** Signal from a new request to interrupt this conversation */
721
1254
  interruptedBy?: string;
1255
+ /**
1256
+ * Doom-loop detector state (see the `doomLoop` option on `callModel`).
1257
+ * Bounded plain JSON, persisted so repetition streaks survive
1258
+ * serialize → resume. Absent when detection is off. Additive within
1259
+ * ConversationState version 1.
1260
+ */
1261
+ doomLoop?: DoomLoopSerializedState;
1262
+ /**
1263
+ * Async tool tasks (background / deferred) whose placeholder output was
1264
+ * sent to the model but whose real result has not been delivered yet.
1265
+ * Additive within ConversationState version 1.
1266
+ */
1267
+ pendingAsyncTools?: PendingAsyncTool[];
1268
+ /**
1269
+ * Call ids whose async result has already been delivered — the
1270
+ * at-most-once guard against replayed webhook resolutions. Additive
1271
+ * within ConversationState version 1.
1272
+ */
1273
+ settledAsyncCallIds?: string[];
722
1274
  /** Current status of the conversation */
723
1275
  status: ConversationStatus;
724
1276
  /** Creation timestamp (Unix ms) */
@@ -771,5 +1323,4 @@ export declare function toolHasApprovalConfigured(tool: Tool): boolean;
771
1323
  * Type guard to check if any tools in array have approval configured at runtime
772
1324
  */
773
1325
  export declare function hasApprovalRequiredTools(tools: readonly Tool[]): boolean;
774
- export {};
775
1326
  //# sourceMappingURL=tool-types.d.ts.map