@openrouter/agent 0.8.0 → 0.10.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.
- package/README.md +732 -7
- package/THIRD_PARTY_NOTICES.md +25 -0
- package/esm/index.d.ts +20 -5
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +10 -3
- package/esm/index.js.map +1 -1
- package/esm/inner-loop/call-model.d.ts.map +1 -1
- package/esm/inner-loop/call-model.js +63 -46
- package/esm/inner-loop/call-model.js.map +1 -1
- package/esm/inner-loop/resume-tool-results.d.ts +128 -0
- package/esm/inner-loop/resume-tool-results.d.ts.map +1 -0
- package/esm/inner-loop/resume-tool-results.js +244 -0
- package/esm/inner-loop/resume-tool-results.js.map +1 -0
- package/esm/lib/agent-tool.d.ts +92 -0
- package/esm/lib/agent-tool.d.ts.map +1 -0
- package/esm/lib/agent-tool.js +211 -0
- package/esm/lib/agent-tool.js.map +1 -0
- package/esm/lib/async-params.d.ts +96 -0
- package/esm/lib/async-params.d.ts.map +1 -1
- package/esm/lib/async-params.js +30 -1
- package/esm/lib/async-params.js.map +1 -1
- package/esm/lib/async-tool-registry.d.ts +134 -0
- package/esm/lib/async-tool-registry.d.ts.map +1 -0
- package/esm/lib/async-tool-registry.js +352 -0
- package/esm/lib/async-tool-registry.js.map +1 -0
- package/esm/lib/async-tools.d.ts +23 -0
- package/esm/lib/async-tools.d.ts.map +1 -0
- package/esm/lib/async-tools.js +19 -0
- package/esm/lib/async-tools.js.map +1 -0
- package/esm/lib/conversation-state.d.ts +1 -0
- package/esm/lib/conversation-state.d.ts.map +1 -1
- package/esm/lib/conversation-state.js +8 -0
- package/esm/lib/conversation-state.js.map +1 -1
- package/esm/lib/doom-loop.d.ts +533 -0
- package/esm/lib/doom-loop.d.ts.map +1 -0
- package/esm/lib/doom-loop.js +1221 -0
- package/esm/lib/doom-loop.js.map +1 -0
- package/esm/lib/hooks-schemas.d.ts +33 -0
- package/esm/lib/hooks-schemas.d.ts.map +1 -1
- package/esm/lib/hooks-schemas.js +54 -0
- package/esm/lib/hooks-schemas.js.map +1 -1
- package/esm/lib/hooks-types.d.ts +6 -2
- package/esm/lib/hooks-types.d.ts.map +1 -1
- package/esm/lib/hooks-types.js.map +1 -1
- package/esm/lib/model-result.d.ts +648 -12
- package/esm/lib/model-result.d.ts.map +1 -1
- package/esm/lib/model-result.js +2784 -164
- package/esm/lib/model-result.js.map +1 -1
- package/esm/lib/next-turn-params.d.ts.map +1 -1
- package/esm/lib/next-turn-params.js +3 -1
- package/esm/lib/next-turn-params.js.map +1 -1
- package/esm/lib/next-turn-params.test.js +72 -0
- package/esm/lib/next-turn-params.test.js.map +1 -1
- package/esm/lib/reusable-stream.d.ts +9 -0
- package/esm/lib/reusable-stream.d.ts.map +1 -1
- package/esm/lib/reusable-stream.js +17 -0
- package/esm/lib/reusable-stream.js.map +1 -1
- package/esm/lib/stream-transformers.d.ts +21 -1
- package/esm/lib/stream-transformers.d.ts.map +1 -1
- package/esm/lib/stream-transformers.js +45 -0
- package/esm/lib/stream-transformers.js.map +1 -1
- package/esm/lib/tool-check.d.ts +76 -0
- package/esm/lib/tool-check.d.ts.map +1 -0
- package/esm/lib/tool-check.js +241 -0
- package/esm/lib/tool-check.js.map +1 -0
- package/esm/lib/tool-concurrency.d.ts +42 -0
- package/esm/lib/tool-concurrency.d.ts.map +1 -0
- package/esm/lib/tool-concurrency.js +80 -0
- package/esm/lib/tool-concurrency.js.map +1 -0
- package/esm/lib/tool-context.d.ts +45 -1
- package/esm/lib/tool-context.d.ts.map +1 -1
- package/esm/lib/tool-context.js +60 -1
- package/esm/lib/tool-context.js.map +1 -1
- package/esm/lib/tool-executor.d.ts +52 -9
- package/esm/lib/tool-executor.d.ts.map +1 -1
- package/esm/lib/tool-executor.js +253 -21
- package/esm/lib/tool-executor.js.map +1 -1
- package/esm/lib/tool-orchestrator.d.ts.map +1 -1
- package/esm/lib/tool-orchestrator.js +87 -50
- package/esm/lib/tool-orchestrator.js.map +1 -1
- package/esm/lib/tool-set-types.d.ts +298 -0
- package/esm/lib/tool-set-types.d.ts.map +1 -0
- package/esm/lib/tool-set-types.js +2 -0
- package/esm/lib/tool-set-types.js.map +1 -0
- package/esm/lib/tool-set.d.ts +157 -0
- package/esm/lib/tool-set.d.ts.map +1 -0
- package/esm/lib/tool-set.js +473 -0
- package/esm/lib/tool-set.js.map +1 -0
- package/esm/lib/tool-task.d.ts +157 -0
- package/esm/lib/tool-task.d.ts.map +1 -0
- package/esm/lib/tool-task.js +206 -0
- package/esm/lib/tool-task.js.map +1 -0
- package/esm/lib/tool-types.d.ts +667 -38
- package/esm/lib/tool-types.d.ts.map +1 -1
- package/esm/lib/tool-types.js +59 -10
- package/esm/lib/tool-types.js.map +1 -1
- package/esm/lib/tool.d.ts +227 -19
- package/esm/lib/tool.d.ts.map +1 -1
- package/esm/lib/tool.js +206 -6
- package/esm/lib/tool.js.map +1 -1
- package/esm/mcp/auth/auth-resolver.d.ts +14 -0
- package/esm/mcp/auth/auth-resolver.d.ts.map +1 -0
- package/esm/mcp/auth/auth-resolver.js +27 -0
- package/esm/mcp/auth/auth-resolver.js.map +1 -0
- package/esm/mcp/auth/auth-types.d.ts +33 -0
- package/esm/mcp/auth/auth-types.d.ts.map +1 -0
- package/esm/mcp/auth/auth-types.js +4 -0
- package/esm/mcp/auth/auth-types.js.map +1 -0
- package/esm/mcp/build-tools.d.ts +29 -0
- package/esm/mcp/build-tools.d.ts.map +1 -0
- package/esm/mcp/build-tools.js +80 -0
- package/esm/mcp/build-tools.js.map +1 -0
- package/esm/mcp/cache/cache-store.d.ts +24 -0
- package/esm/mcp/cache/cache-store.d.ts.map +1 -0
- package/esm/mcp/cache/cache-store.js +20 -0
- package/esm/mcp/cache/cache-store.js.map +1 -0
- package/esm/mcp/cache/cache-types.d.ts +71 -0
- package/esm/mcp/cache/cache-types.d.ts.map +1 -0
- package/esm/mcp/cache/cache-types.js +49 -0
- package/esm/mcp/cache/cache-types.js.map +1 -0
- package/esm/mcp/cache/serialize.d.ts +25 -0
- package/esm/mcp/cache/serialize.d.ts.map +1 -0
- package/esm/mcp/cache/serialize.js +96 -0
- package/esm/mcp/cache/serialize.js.map +1 -0
- package/esm/mcp/close-quietly.d.ts +24 -0
- package/esm/mcp/close-quietly.d.ts.map +1 -0
- package/esm/mcp/close-quietly.js +30 -0
- package/esm/mcp/close-quietly.js.map +1 -0
- package/esm/mcp/create-mcp-tools.d.ts +13 -0
- package/esm/mcp/create-mcp-tools.d.ts.map +1 -0
- package/esm/mcp/create-mcp-tools.js +87 -0
- package/esm/mcp/create-mcp-tools.js.map +1 -0
- package/esm/mcp/elicitation.d.ts +11 -0
- package/esm/mcp/elicitation.d.ts.map +1 -0
- package/esm/mcp/elicitation.js +51 -0
- package/esm/mcp/elicitation.js.map +1 -0
- package/esm/mcp/errors.d.ts +91 -0
- package/esm/mcp/errors.d.ts.map +1 -0
- package/esm/mcp/errors.js +91 -0
- package/esm/mcp/errors.js.map +1 -0
- package/esm/mcp/handle.d.ts +66 -0
- package/esm/mcp/handle.d.ts.map +1 -0
- package/esm/mcp/handle.js +323 -0
- package/esm/mcp/handle.js.map +1 -0
- package/esm/mcp/index.d.ts +13 -0
- package/esm/mcp/index.d.ts.map +1 -0
- package/esm/mcp/index.js +11 -0
- package/esm/mcp/index.js.map +1 -0
- package/esm/mcp/mcp-connection.d.ts +150 -0
- package/esm/mcp/mcp-connection.d.ts.map +1 -0
- package/esm/mcp/mcp-connection.js +478 -0
- package/esm/mcp/mcp-connection.js.map +1 -0
- package/esm/mcp/mcp-sdk.d.ts +10 -0
- package/esm/mcp/mcp-sdk.d.ts.map +1 -0
- package/esm/mcp/mcp-sdk.js +37 -0
- package/esm/mcp/mcp-sdk.js.map +1 -0
- package/esm/mcp/rehydrate.d.ts +76 -0
- package/esm/mcp/rehydrate.d.ts.map +1 -0
- package/esm/mcp/rehydrate.js +505 -0
- package/esm/mcp/rehydrate.js.map +1 -0
- package/esm/mcp/resource-tools.d.ts +17 -0
- package/esm/mcp/resource-tools.d.ts.map +1 -0
- package/esm/mcp/resource-tools.js +155 -0
- package/esm/mcp/resource-tools.js.map +1 -0
- package/esm/mcp/result-mapper.d.ts +19 -0
- package/esm/mcp/result-mapper.d.ts.map +1 -0
- package/esm/mcp/result-mapper.js +40 -0
- package/esm/mcp/result-mapper.js.map +1 -0
- package/esm/mcp/schema/json-schema-guards.d.ts +14 -0
- package/esm/mcp/schema/json-schema-guards.d.ts.map +1 -0
- package/esm/mcp/schema/json-schema-guards.js +19 -0
- package/esm/mcp/schema/json-schema-guards.js.map +1 -0
- package/esm/mcp/schema/json-schema-to-zod.d.ts +20 -0
- package/esm/mcp/schema/json-schema-to-zod.d.ts.map +1 -0
- package/esm/mcp/schema/json-schema-to-zod.js +72 -0
- package/esm/mcp/schema/json-schema-to-zod.js.map +1 -0
- package/esm/mcp/tool-wrapper.d.ts +41 -0
- package/esm/mcp/tool-wrapper.d.ts.map +1 -0
- package/esm/mcp/tool-wrapper.js +131 -0
- package/esm/mcp/tool-wrapper.js.map +1 -0
- package/esm/mcp/transport-types.d.ts +45 -0
- package/esm/mcp/transport-types.d.ts.map +1 -0
- package/esm/mcp/transport-types.js +2 -0
- package/esm/mcp/transport-types.js.map +1 -0
- package/esm/mcp/types.d.ts +119 -0
- package/esm/mcp/types.d.ts.map +1 -0
- package/esm/mcp/types.js +2 -0
- package/esm/mcp/types.js.map +1 -0
- package/esm/mcp/version.d.ts +3 -0
- package/esm/mcp/version.d.ts.map +1 -0
- package/esm/mcp/version.js +5 -0
- package/esm/mcp/version.js.map +1 -0
- package/package.json +70 -5
package/esm/lib/tool-types.d.ts
CHANGED
|
@@ -1,12 +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 { DoomLoopSerializedState } from './doom-loop.js';
|
|
5
|
+
import type { TaskLogLimits, ToolTaskMode, ToolTaskStatus } from './tool-task.js';
|
|
4
6
|
/**
|
|
5
7
|
* Tool type enum for enhanced tools
|
|
6
8
|
*/
|
|
7
9
|
export declare enum ToolType {
|
|
8
10
|
Function = "function"
|
|
9
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
|
+
}
|
|
10
39
|
/**
|
|
11
40
|
* Turn context passed to tool execute functions and async parameter resolution
|
|
12
41
|
* Contains information about the current conversation state
|
|
@@ -18,6 +47,21 @@ export interface TurnContext {
|
|
|
18
47
|
numberOfTurns: number;
|
|
19
48
|
/** The full request being sent to the API (only available during tool execution) */
|
|
20
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;
|
|
21
65
|
}
|
|
22
66
|
/**
|
|
23
67
|
* Extract context schema type from a tool definition.
|
|
@@ -44,9 +88,10 @@ export type InferToolContext<T> = T extends {
|
|
|
44
88
|
*/
|
|
45
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>;
|
|
46
90
|
/**
|
|
47
|
-
* Extract tool name from a tool definition
|
|
91
|
+
* Extract tool name from a tool definition.
|
|
92
|
+
* Preserves literal names when present; falls back to `string`.
|
|
48
93
|
*/
|
|
49
|
-
type InferToolName<T> = T extends {
|
|
94
|
+
export type InferToolName<T> = T extends {
|
|
50
95
|
function: {
|
|
51
96
|
name: infer N extends string;
|
|
52
97
|
};
|
|
@@ -70,6 +115,90 @@ export type ToolExecuteContext<TName extends string = string, TContext extends R
|
|
|
70
115
|
shared: Readonly<TShared>;
|
|
71
116
|
/** Mutate the shared context in the store (persists across turns) */
|
|
72
117
|
setSharedContext(partial: Partial<TShared>): void;
|
|
118
|
+
/**
|
|
119
|
+
* Abort signal for this tool call. Fires when the run is aborted
|
|
120
|
+
* (`callModel`'s `signal` option or `ModelResult.cancel()`), when the
|
|
121
|
+
* tool's `timeoutMs` (or the run-level `toolTimeoutMs`) elapses, or when
|
|
122
|
+
* a background task is cancelled via `cancelTask`. Cooperative: tool
|
|
123
|
+
* bodies should pass it to their own I/O (fetch, child processes, ...).
|
|
124
|
+
* Always present — a never-aborting signal is supplied when no
|
|
125
|
+
* cancellation sources exist.
|
|
126
|
+
*/
|
|
127
|
+
readonly signal: AbortSignal;
|
|
128
|
+
/** The id of the tool call being executed (for correlating external work). */
|
|
129
|
+
readonly callId?: string;
|
|
130
|
+
/**
|
|
131
|
+
* The conversation id when a `StateAccessor` is configured. Deferred
|
|
132
|
+
* tools typically hand this to the external system so its webhook can
|
|
133
|
+
* locate the conversation to resume.
|
|
134
|
+
*/
|
|
135
|
+
readonly conversationId?: string;
|
|
136
|
+
};
|
|
137
|
+
declare const DEFERRED_BRAND: unique symbol;
|
|
138
|
+
/**
|
|
139
|
+
* Returned by `ctx.defer()`. Returning it from `run` parks the call on a
|
|
140
|
+
* durable external task: the model gets a pending placeholder and the run
|
|
141
|
+
* pauses (`status: 'awaiting_async_tools'`) until the task is resolved via
|
|
142
|
+
* the tool's `.resolve()` / `.fail()` / `.cancel()` methods or
|
|
143
|
+
* `resumeToolResults()` — possibly from another process.
|
|
144
|
+
*
|
|
145
|
+
* Branded with TOutput so a handle from one tool cannot be returned from
|
|
146
|
+
* another whose outputSchema differs.
|
|
147
|
+
*
|
|
148
|
+
* Two distinct markers, deliberately:
|
|
149
|
+
* - `[DEFERRED_BRAND]` is TYPE-ONLY (`declare const` — no runtime value
|
|
150
|
+
* exists) and carries the TOutput brand for compile-time safety.
|
|
151
|
+
* - `__deferred` is the RUNTIME marker {@link isDeferredHandle} checks.
|
|
152
|
+
* `ctx.defer()` (the sole creator, in model-result.ts) always sets it.
|
|
153
|
+
* Changing either without the other breaks deferred-handle detection —
|
|
154
|
+
* keep them in sync.
|
|
155
|
+
*/
|
|
156
|
+
export interface DeferredHandle<TOutput = unknown> {
|
|
157
|
+
readonly [DEFERRED_BRAND]: TOutput;
|
|
158
|
+
/** Runtime marker set by `ctx.defer()`; checked by {@link isDeferredHandle}. */
|
|
159
|
+
readonly __deferred: true;
|
|
160
|
+
readonly taskId: string;
|
|
161
|
+
readonly pollAfterMs?: number;
|
|
162
|
+
readonly expiresAt?: number;
|
|
163
|
+
readonly ack?: string | Record<string, unknown>;
|
|
164
|
+
}
|
|
165
|
+
/** Runtime guard for {@link DeferredHandle} (structural — brand is type-only). */
|
|
166
|
+
export declare function isDeferredHandle(value: unknown): value is DeferredHandle;
|
|
167
|
+
/** Options for `ctx.defer()`. */
|
|
168
|
+
export type DeferOptions = {
|
|
169
|
+
/** Poll-interval hint surfaced in the placeholder and to external pollers. */
|
|
170
|
+
pollAfterMs?: number;
|
|
171
|
+
/** Unix ms after which the task is considered expired. */
|
|
172
|
+
expiresAt?: number;
|
|
173
|
+
/** Model-facing note merged into the placeholder (overrides the tool-level `ack`). */
|
|
174
|
+
ack?: string | Record<string, unknown>;
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* Context passed to a unified tool's `run`. Extends the base execute
|
|
178
|
+
* context with the async-task affordances: deferral, logging, and the
|
|
179
|
+
* steering inbox.
|
|
180
|
+
*/
|
|
181
|
+
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> & {
|
|
182
|
+
/**
|
|
183
|
+
* Park this call on a durable external task and return the handle:
|
|
184
|
+
* `return ctx.defer(ticket.id)`. Only valid on `lifecycle: 'deferred'`
|
|
185
|
+
* tools — throws elsewhere.
|
|
186
|
+
*/
|
|
187
|
+
defer(taskId: string, options?: DeferOptions): DeferredHandle<TOutput>;
|
|
188
|
+
/**
|
|
189
|
+
* Append a log entry without yielding — progress for non-generator `run`
|
|
190
|
+
* bodies. Same pipeline as a yield: validated against `eventSchema` when
|
|
191
|
+
* declared, appended to the task log, surfaced as `tool.preliminary_result`.
|
|
192
|
+
*/
|
|
193
|
+
log(entry: unknown): void;
|
|
194
|
+
/**
|
|
195
|
+
* Register a steering handler. Messages sent via a check call's
|
|
196
|
+
* `turnContext.task.send(...)` or `ModelResult.sendToTask()` are delivered
|
|
197
|
+
* here (queued until registration).
|
|
198
|
+
*/
|
|
199
|
+
onMessage(handler: (message: unknown) => void): void;
|
|
200
|
+
/** The task's id (background/agent: engine-generated, present once escaped). */
|
|
201
|
+
readonly taskId?: string;
|
|
73
202
|
};
|
|
74
203
|
/**
|
|
75
204
|
* Context map keyed by tool name for callModel's `context` option.
|
|
@@ -98,6 +227,17 @@ export declare const SHARED_CONTEXT_KEY: "shared";
|
|
|
98
227
|
export type NextTurnParamsContext = {
|
|
99
228
|
/** Current input (messages) */
|
|
100
229
|
input: models.InputsUnion;
|
|
230
|
+
/**
|
|
231
|
+
* Current tool choice.
|
|
232
|
+
*
|
|
233
|
+
* Returning a new value changes which tools the model may call next turn
|
|
234
|
+
* without touching the `tools` array — the hook a tool-search tool uses to
|
|
235
|
+
* widen `{ type: 'allowed_tools', tools: [...] }` once it has found what it
|
|
236
|
+
* was looking for. Leaving `tools` alone is the point: rewriting it would
|
|
237
|
+
* invalidate the provider's prompt-cache prefix, which is usually the reason
|
|
238
|
+
* the caller is withholding tools in the first place.
|
|
239
|
+
*/
|
|
240
|
+
toolChoice: models.ResponsesRequest['toolChoice'];
|
|
101
241
|
/** Current model selection */
|
|
102
242
|
model: string;
|
|
103
243
|
/** Current models array */
|
|
@@ -130,6 +270,45 @@ export type NextTurnParamsFunctions<TInput> = {
|
|
|
130
270
|
export type ToolApprovalCheck<TInput> = {
|
|
131
271
|
bivarianceHack(params: TInput, context: TurnContext): boolean | Promise<boolean>;
|
|
132
272
|
}['bivarianceHack'];
|
|
273
|
+
/**
|
|
274
|
+
* Function form of {@link ToolLoopKey}: computes the key material that
|
|
275
|
+
* identifies a call of this tool for doom-loop detection.
|
|
276
|
+
*
|
|
277
|
+
* The returned value is canonicalized (RFC 8785) and hashed by the engine —
|
|
278
|
+
* tools declare *what identifies a call*, the engine owns *how it is
|
|
279
|
+
* fingerprinted*. Two calls whose key material canonicalizes identically
|
|
280
|
+
* count as the same call.
|
|
281
|
+
*
|
|
282
|
+
* - Return a focused subset for precise identity: a web-search tool returns
|
|
283
|
+
* its normalized query; a bash tool returns `{ command, cwd, env }`.
|
|
284
|
+
* - Return `null` to exempt THIS call from detection (e.g. a legitimate
|
|
285
|
+
* repeat the tool can recognize from its arguments).
|
|
286
|
+
* - Returning `undefined` (a bare `return;`) is treated as a bug: the
|
|
287
|
+
* engine warns and falls back to the full-arguments identity.
|
|
288
|
+
*
|
|
289
|
+
* Must be pure and deterministic. A throwing `loopKey` falls back to the
|
|
290
|
+
* full-arguments fingerprint (detection must never take down a run).
|
|
291
|
+
*/
|
|
292
|
+
export type ToolLoopKeyFn<TInput> = {
|
|
293
|
+
bivarianceHack(params: TInput): unknown;
|
|
294
|
+
}['bivarianceHack'];
|
|
295
|
+
/**
|
|
296
|
+
* Doom-loop identity declaration for a tool (see the `doomLoop` option on
|
|
297
|
+
* `callModel`). Computed like every other tool hook — a plain function over
|
|
298
|
+
* the call's validated arguments:
|
|
299
|
+
*
|
|
300
|
+
* - function — computes key material per call ({@link ToolLoopKeyFn}), e.g.
|
|
301
|
+
* `({ command, cwd }) => ({ command, cwd })` for a bash tool. Returning
|
|
302
|
+
* `null` exempts individual calls (a legitimate repeat the tool can
|
|
303
|
+
* recognize from its arguments).
|
|
304
|
+
* - field-name array — the declarative subset identifying a call, e.g.
|
|
305
|
+
* `['command', 'cwd']` for a bash tool. Data, not code: serializable, so
|
|
306
|
+
* it survives tool caches and can be advertised over the MCP wire.
|
|
307
|
+
* - `false` — this tool is statically exempt (repetition is its job, e.g. a
|
|
308
|
+
* status poller).
|
|
309
|
+
* - absent — the full validated arguments object is the identity.
|
|
310
|
+
*/
|
|
311
|
+
export type ToolLoopKey<TInput> = ToolLoopKeyFn<TInput> | readonly string[] | false;
|
|
133
312
|
/**
|
|
134
313
|
* Content item types for tool output to model.
|
|
135
314
|
* These match the Responses API format for multimodal tool outputs.
|
|
@@ -170,11 +349,23 @@ export type ToModelOutputFunction<TInput, TOutput> = {
|
|
|
170
349
|
* Base tool function interface with inputSchema
|
|
171
350
|
* @template TInput - Zod schema for tool input
|
|
172
351
|
* @template TCtx - Zod schema for tool context (optional; default = erased wide type)
|
|
352
|
+
* @template TName - Literal tool name (default `string` keeps wide assignability)
|
|
173
353
|
*/
|
|
174
|
-
export interface BaseToolFunction<TInput extends $ZodObject<$ZodShape>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape
|
|
175
|
-
name:
|
|
354
|
+
export interface BaseToolFunction<TInput extends $ZodObject<$ZodShape>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TName extends string = string> {
|
|
355
|
+
name: TName;
|
|
176
356
|
description?: string;
|
|
177
357
|
inputSchema: TInput;
|
|
358
|
+
/**
|
|
359
|
+
* Whether providers should enforce strict schema adherence when generating
|
|
360
|
+
* this tool's call arguments (OpenAI structured-outputs style). Serialized
|
|
361
|
+
* onto the wire tool definition; `null`/absent leaves provider default.
|
|
362
|
+
*
|
|
363
|
+
* OpenAI-style strict mode requires every declared object property to be
|
|
364
|
+
* listed in `required`. Use Zod `.nullable()` for values that may be absent
|
|
365
|
+
* conceptually; `.optional()` produces a schema those providers may reject.
|
|
366
|
+
* The SDK forwards the caller's schema unchanged.
|
|
367
|
+
*/
|
|
368
|
+
strict?: boolean | null;
|
|
178
369
|
/**
|
|
179
370
|
* Zod schema declaring the context data this tool needs.
|
|
180
371
|
* `readonly` keeps TCtx covariant so tools carrying a concrete schema stay
|
|
@@ -187,14 +378,50 @@ export interface BaseToolFunction<TInput extends $ZodObject<$ZodShape>, TCtx ext
|
|
|
187
378
|
* Can be a boolean or an async function that receives the tool's input params and context
|
|
188
379
|
*/
|
|
189
380
|
requireApproval?: boolean | ToolApprovalCheck<zodInfer<TInput>>;
|
|
381
|
+
/**
|
|
382
|
+
* Doom-loop identity for this tool's calls — see {@link ToolLoopKey}.
|
|
383
|
+
* A computed function over the call's arguments, a field-name array
|
|
384
|
+
* (serializable — used by MCP tool caches), or `false` (exempt).
|
|
385
|
+
* Absent: the full validated arguments object is the identity.
|
|
386
|
+
*/
|
|
387
|
+
loopKey?: ToolLoopKey<zodInfer<TInput>>;
|
|
388
|
+
/**
|
|
389
|
+
* Deadline for one execution of this tool, in milliseconds. When it
|
|
390
|
+
* elapses the round stops waiting: the model receives a
|
|
391
|
+
* `{ error, code: 'tool_timeout' }` output immediately and the tool's
|
|
392
|
+
* `ctx.signal` is aborted. The timeout bounds the round's WAIT, not the
|
|
393
|
+
* tool body's execution — a body that ignores its signal keeps running
|
|
394
|
+
* detached (its result is discarded). Overrides the run-level
|
|
395
|
+
* `toolTimeoutMs` default.
|
|
396
|
+
*/
|
|
397
|
+
timeoutMs?: number;
|
|
398
|
+
/**
|
|
399
|
+
* Maximum simultaneous in-flight executions of THIS tool across the run
|
|
400
|
+
* — round-synchronous and background alike (background bodies re-acquire
|
|
401
|
+
* the gate for their full duration). Typically encodes an external
|
|
402
|
+
* constraint (one DB connection, a rate-limited API key). Excess calls
|
|
403
|
+
* queue FIFO. Queue wait does NOT count against `timeoutMs` for
|
|
404
|
+
* round-synchronous calls (the deadline starts once the slot is held);
|
|
405
|
+
* background tasks queue against their own registry-tracked timeout.
|
|
406
|
+
* Unbounded when absent.
|
|
407
|
+
*/
|
|
408
|
+
maxConcurrency?: number;
|
|
190
409
|
}
|
|
191
410
|
/**
|
|
192
411
|
* Regular tool with synchronous or asynchronous execute function and optional outputSchema
|
|
193
412
|
* @template TContext - Shape of the tool's context (inferred from contextSchema)
|
|
194
413
|
* @template TName - The tool's literal name string
|
|
195
414
|
*/
|
|
196
|
-
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> {
|
|
415
|
+
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, TName> {
|
|
197
416
|
outputSchema?: TOutput;
|
|
417
|
+
/**
|
|
418
|
+
* Absent on regular tools. Declared as `undefined`-only so
|
|
419
|
+
* `UnifiedToolFunction` (which requires `run` and `lifecycle`) is
|
|
420
|
+
* structurally DISJOINT from this interface — type-guard narrowing must
|
|
421
|
+
* not keep unified tools in the regular-execute union.
|
|
422
|
+
*/
|
|
423
|
+
readonly run?: undefined;
|
|
424
|
+
readonly lifecycle?: undefined;
|
|
198
425
|
execute(params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>): Promise<zodInfer<TOutput>> | zodInfer<TOutput>;
|
|
199
426
|
/** Convert tool execution output to model-facing output */
|
|
200
427
|
toModelOutput?: ToModelOutputFunction<zodInfer<TInput>, zodInfer<TOutput>>;
|
|
@@ -221,7 +448,7 @@ export interface ToolFunctionWithExecute<TInput extends $ZodObject<$ZodShape>, T
|
|
|
221
448
|
* }
|
|
222
449
|
* ```
|
|
223
450
|
*/
|
|
224
|
-
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> {
|
|
451
|
+
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, TName> {
|
|
225
452
|
eventSchema: TEvent;
|
|
226
453
|
outputSchema: TOutput;
|
|
227
454
|
execute(params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>): AsyncGenerator<zodInfer<TEvent> | zodInfer<TOutput>, zodInfer<TOutput> | undefined>;
|
|
@@ -231,7 +458,7 @@ export interface ToolFunctionWithGenerator<TInput extends $ZodObject<$ZodShape>,
|
|
|
231
458
|
/**
|
|
232
459
|
* Manual tool without execute function - requires manual handling by developer
|
|
233
460
|
*/
|
|
234
|
-
export interface ManualToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape
|
|
461
|
+
export interface ManualToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TName extends string = string> extends BaseToolFunction<TInput, TCtx, TName> {
|
|
235
462
|
outputSchema?: TOutput;
|
|
236
463
|
}
|
|
237
464
|
/**
|
|
@@ -246,7 +473,7 @@ export interface ManualToolFunction<TInput extends $ZodObject<$ZodShape>, TOutpu
|
|
|
246
473
|
* It receives the caller-supplied raw result and returns the value sent to the model.
|
|
247
474
|
* Throwing surfaces as a tool error to the model.
|
|
248
475
|
*/
|
|
249
|
-
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> {
|
|
476
|
+
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, TName> {
|
|
250
477
|
/**
|
|
251
478
|
* Required for HITL tools. Used to validate both the `onToolCalled` return
|
|
252
479
|
* value (when non-null) and the caller-supplied response that comes back via
|
|
@@ -258,43 +485,156 @@ export interface HITLToolFunction<TInput extends $ZodObject<$ZodShape>, TOutput
|
|
|
258
485
|
onResponseReceived?(rawResult: unknown, context?: ToolExecuteContext<TName, TContext>): Promise<zodInfer<TOutput>> | zodInfer<TOutput>;
|
|
259
486
|
toModelOutput?: ToModelOutputFunction<zodInfer<TInput>, zodInfer<TOutput>>;
|
|
260
487
|
}
|
|
488
|
+
/**
|
|
489
|
+
* Model-facing acknowledgement for an async tool's placeholder output.
|
|
490
|
+
* A string becomes the placeholder's `note`; an object is merged into the
|
|
491
|
+
* placeholder payload; a function computes either from the call's input.
|
|
492
|
+
*/
|
|
493
|
+
export type AsyncToolAck<TInput> = string | Record<string, unknown> | {
|
|
494
|
+
bivarianceHack(input: TInput): string | Record<string, unknown>;
|
|
495
|
+
}['bivarianceHack'];
|
|
496
|
+
/** How a unified tool's execution relates to the tool round. */
|
|
497
|
+
export type ToolLifecycle = 'sync' | 'background' | 'deferred';
|
|
498
|
+
/**
|
|
499
|
+
* Check-in configuration for a long-running tool. The model interacts with
|
|
500
|
+
* running tasks through the SINGLE universal `task` tool (`action:
|
|
501
|
+
* 'check' | 'steer' | 'result' | 'cancel'`, addressed by `taskId`); the
|
|
502
|
+
* engine dispatches those calls to the owning tool's config here — so the
|
|
503
|
+
* wire surface stays constant while the implementation stays tool-specific.
|
|
504
|
+
*
|
|
505
|
+
* - `true` — explicit opt-in to the SDK default handling (long-running
|
|
506
|
+
* tools get it even without this).
|
|
507
|
+
* - `schema` — validates the task tool's free-form `params` object when the
|
|
508
|
+
* model passes one to this tool's custom handler.
|
|
509
|
+
* - `execute` — custom check handler, replacing the SDK default for
|
|
510
|
+
* `action: 'check'` calls targeting this tool's tasks. Receives the
|
|
511
|
+
* validated `params` and a TurnContext populated with `toolCallStatus`,
|
|
512
|
+
* `accumulatedYieldedEvents`, and the `task` handle. Its return value is
|
|
513
|
+
* the tool output the model sees.
|
|
514
|
+
*/
|
|
515
|
+
export type ToolCheckConfig<TCheckParams = Record<string, unknown>> = true | {
|
|
516
|
+
/**
|
|
517
|
+
* Validation schema for the model-supplied `params` on check calls.
|
|
518
|
+
* SECURITY: `params` is MODEL INPUT (and models can be steered by
|
|
519
|
+
* injected tool/web content). Without a schema the handler receives
|
|
520
|
+
* the raw record — declare one whenever `execute` forwards params
|
|
521
|
+
* into steering/cancel or any other side effect, exactly as you
|
|
522
|
+
* would validate a tool's inputSchema.
|
|
523
|
+
*/
|
|
524
|
+
schema?: $ZodObject<$ZodShape>;
|
|
525
|
+
execute?: {
|
|
526
|
+
bivarianceHack(params: TCheckParams, turnContext: TurnContext): unknown | Promise<unknown>;
|
|
527
|
+
}['bivarianceHack'];
|
|
528
|
+
};
|
|
529
|
+
/**
|
|
530
|
+
* The unified `run`-based tool kind. One shape covers every lifecycle:
|
|
531
|
+
*
|
|
532
|
+
* - `'sync'` (default): `run` is awaited in the round, exactly like `execute`.
|
|
533
|
+
* - `'background'`: the loop does not wait. Work settling within `graceMs`
|
|
534
|
+
* behaves like a sync call; otherwise the model receives a pending
|
|
535
|
+
* placeholder, the loop continues, and the return value is injected as a
|
|
536
|
+
* `tool_task_result` envelope when it settles.
|
|
537
|
+
* - `'deferred'`: `run` may return `ctx.defer(taskId)` to park the call on a
|
|
538
|
+
* durable external task — the run pauses (`awaiting_async_tools`) until
|
|
539
|
+
* the task is resolved via `.resolve()`/`.fail()`/`.cancel()` or
|
|
540
|
+
* `resumeToolResults()`, possibly from another process. Returning a plain
|
|
541
|
+
* value resolves immediately (typed fast path).
|
|
542
|
+
*
|
|
543
|
+
* `run` is an async function or an async generator. Generator yields become
|
|
544
|
+
* the task's LOG entries (feeding check-in views and
|
|
545
|
+
* `tool.preliminary_result` events); the generator's RETURN value is the
|
|
546
|
+
* final result, validated against `outputSchema`. (This is stricter than
|
|
547
|
+
* legacy `execute` generators, which accept a final yield as the result.)
|
|
548
|
+
*/
|
|
549
|
+
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, TName> {
|
|
550
|
+
/** Discriminator against every legacy kind. */
|
|
551
|
+
readonly lifecycle: ToolLifecycle;
|
|
552
|
+
/**
|
|
553
|
+
* Required for 'background' | 'deferred' (results settle after the round,
|
|
554
|
+
* possibly in another process). Optional for 'sync' (inferred from run).
|
|
555
|
+
*/
|
|
556
|
+
outputSchema?: TOutput;
|
|
557
|
+
/** Validates `run` yields / `ctx.log()` entries when declared. */
|
|
558
|
+
eventSchema?: TEvent;
|
|
559
|
+
/** Model-facing acknowledgement merged into the pending placeholder. */
|
|
560
|
+
ack?: AsyncToolAck<zodInfer<TInput>>;
|
|
561
|
+
/**
|
|
562
|
+
* Background only: hold the round this long (ms) before emitting a
|
|
563
|
+
* placeholder. Work settling in-window produces a plain synchronous
|
|
564
|
+
* output. Default 250; `0` always placeholders.
|
|
565
|
+
*/
|
|
566
|
+
graceMs?: number;
|
|
567
|
+
/** Deferred only: default poll-interval hint for tasks from this tool. */
|
|
568
|
+
pollAfterMs?: number;
|
|
569
|
+
/** Check-in configuration — see {@link ToolCheckConfig}. */
|
|
570
|
+
check?: ToolCheckConfig;
|
|
571
|
+
/** Set by `tool.agent()`: marks the run as a child-conversation driver. */
|
|
572
|
+
readonly kind?: 'agent';
|
|
573
|
+
/** Per-task log ring-buffer overrides. */
|
|
574
|
+
logLimits?: Partial<TaskLogLimits>;
|
|
575
|
+
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>>>;
|
|
576
|
+
/**
|
|
577
|
+
* Convert tool execution output to model-facing output.
|
|
578
|
+
*
|
|
579
|
+
* ROUND-SYNCHRONOUS RESULTS ONLY: applies to `lifecycle: 'sync'` results
|
|
580
|
+
* and background work that settles inside its grace window — the paths
|
|
581
|
+
* that produce a `function_call_output`. Late-delivered results
|
|
582
|
+
* (background past the grace window, deferred completions) arrive as a
|
|
583
|
+
* `tool_task_result` JSON envelope in a user-role message, which cannot
|
|
584
|
+
* carry this mapper's content-item arrays; they deliver the validated
|
|
585
|
+
* output verbatim.
|
|
586
|
+
*/
|
|
587
|
+
toModelOutput?: ToModelOutputFunction<zodInfer<TInput>, zodInfer<TOutput>>;
|
|
588
|
+
/** Absent on unified tools — keeps them disjoint from legacy kinds. */
|
|
589
|
+
readonly execute?: undefined;
|
|
590
|
+
readonly onToolCalled?: undefined;
|
|
591
|
+
}
|
|
261
592
|
/**
|
|
262
593
|
* Tool with execute function (regular or generator)
|
|
263
594
|
* @template TCtx - The concrete contextSchema type when one was provided to `tool()`
|
|
264
595
|
*/
|
|
265
|
-
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
|
|
596
|
+
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>, TName extends string = string> = {
|
|
266
597
|
type: ToolType.Function;
|
|
267
|
-
function: ToolFunctionWithExecute<TInput, TOutput, TContext,
|
|
598
|
+
function: ToolFunctionWithExecute<TInput, TOutput, TContext, TName, TCtx>;
|
|
268
599
|
};
|
|
269
600
|
/**
|
|
270
601
|
* Tool with generator execute function
|
|
271
602
|
* @template TCtx - The concrete contextSchema type when one was provided to `tool()`
|
|
272
603
|
*/
|
|
273
|
-
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
|
|
604
|
+
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>, TName extends string = string> = {
|
|
274
605
|
type: ToolType.Function;
|
|
275
|
-
function: ToolFunctionWithGenerator<TInput, TEvent, TOutput, TContext,
|
|
606
|
+
function: ToolFunctionWithGenerator<TInput, TEvent, TOutput, TContext, TName, TCtx>;
|
|
276
607
|
};
|
|
277
608
|
/**
|
|
278
609
|
* Tool without execute function (manual handling)
|
|
279
610
|
* @template TCtx - The concrete contextSchema type when one was provided to `tool()`
|
|
280
611
|
*/
|
|
281
|
-
export type ManualTool<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape
|
|
612
|
+
export type ManualTool<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TCtx extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TName extends string = string> = {
|
|
282
613
|
type: ToolType.Function;
|
|
283
|
-
function: ManualToolFunction<TInput, TOutput, TCtx>;
|
|
614
|
+
function: ManualToolFunction<TInput, TOutput, TCtx, TName>;
|
|
284
615
|
};
|
|
285
616
|
/**
|
|
286
617
|
* Human-in-the-loop tool (with onToolCalled / onResponseReceived hooks)
|
|
287
618
|
* @template TCtx - The concrete contextSchema type when one was provided to `tool()`
|
|
288
619
|
*/
|
|
289
|
-
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
|
|
620
|
+
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>, TName extends string = string> = {
|
|
290
621
|
type: ToolType.Function;
|
|
291
|
-
function: HITLToolFunction<TInput, TOutput, TContext,
|
|
622
|
+
function: HITLToolFunction<TInput, TOutput, TContext, TName, TCtx>;
|
|
292
623
|
};
|
|
293
624
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
625
|
+
* Unified tool wrapper (`tool()` with `run`)
|
|
626
|
+
* @template TCtx - The concrete contextSchema type when one was provided
|
|
296
627
|
*/
|
|
297
|
-
export type
|
|
628
|
+
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>, TName extends string = string> = {
|
|
629
|
+
type: ToolType.Function;
|
|
630
|
+
function: UnifiedToolFunction<TInput, TOutput, TEvent, TContext, TName, TCtx>;
|
|
631
|
+
};
|
|
632
|
+
/**
|
|
633
|
+
* Union type of all client-executed tool shapes (function, generator, manual,
|
|
634
|
+
* HITL, unified `run`). These run in the user's process via the agent SDK's
|
|
635
|
+
* tool execution loop.
|
|
636
|
+
*/
|
|
637
|
+
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>>;
|
|
298
638
|
/**
|
|
299
639
|
* Config payload for an OpenRouter server-executed tool. Derived directly
|
|
300
640
|
* from the SDK's request-tool union so new server tools flow through with
|
|
@@ -323,6 +663,16 @@ export type ServerToolType = ServerToolConfig['type'];
|
|
|
323
663
|
export interface ServerToolBase {
|
|
324
664
|
readonly _brand: 'server-tool';
|
|
325
665
|
readonly config: ServerToolConfig;
|
|
666
|
+
/**
|
|
667
|
+
* Stable tool-set identity used by `@openrouter/agent-tool-set` activation.
|
|
668
|
+
* Defaults to `server:${config.type}` when constructed via {@link serverTool}.
|
|
669
|
+
*
|
|
670
|
+
* Optional here for source compatibility with legacy hand-constructed
|
|
671
|
+
* `ServerToolBase` values that predate this field. `ServerTool<T, TId>`
|
|
672
|
+
* (the type returned by {@link serverTool}) still requires it as a
|
|
673
|
+
* literal `TId` via interface narrowing below.
|
|
674
|
+
*/
|
|
675
|
+
readonly id?: string;
|
|
326
676
|
}
|
|
327
677
|
/**
|
|
328
678
|
* A server-executed tool. OpenRouter runs the tool and returns an output
|
|
@@ -333,11 +683,13 @@ export interface ServerToolBase {
|
|
|
333
683
|
* (and hence to `Tool`) regardless of `T`.
|
|
334
684
|
*
|
|
335
685
|
* @template T The specific server-tool type literal (narrows `config`).
|
|
686
|
+
* @template TId Stable tool-set ID (defaults to `server:${T}`).
|
|
336
687
|
*/
|
|
337
|
-
export interface ServerTool<T extends ServerToolType = ServerToolType
|
|
688
|
+
export interface ServerTool<T extends ServerToolType = ServerToolType, TId extends string = `server:${T}`> extends ServerToolBase {
|
|
338
689
|
readonly config: Extract<ServerToolConfig, {
|
|
339
690
|
type: T;
|
|
340
691
|
}>;
|
|
692
|
+
readonly id: TId;
|
|
341
693
|
}
|
|
342
694
|
/**
|
|
343
695
|
* Union of every tool kind accepted by `callModel({ tools: [...] })`:
|
|
@@ -438,7 +790,7 @@ export type InferToolEventsUnion<T extends readonly Tool[]> = {
|
|
|
438
790
|
* `ClientTool` lacks. `'_brand' in tool` narrows the union to the server
|
|
439
791
|
* branch structurally, so `tool._brand` is reachable without a cast.
|
|
440
792
|
*/
|
|
441
|
-
export declare function isServerTool(tool: Tool): tool is
|
|
793
|
+
export declare function isServerTool(tool: Tool): tool is ServerToolBase;
|
|
442
794
|
/**
|
|
443
795
|
* A client tool additionally branded as originating from an MCP server. The
|
|
444
796
|
* `_mcp` marker is purely informational: it does NOT change how the tool is
|
|
@@ -461,11 +813,30 @@ export declare function isMcpTool(tool: Tool): tool is McpBranded;
|
|
|
461
813
|
*/
|
|
462
814
|
export declare function isClientTool(tool: Tool): tool is ClientTool;
|
|
463
815
|
/**
|
|
464
|
-
* Type guard to check if a tool
|
|
816
|
+
* Type guard to check if a tool is a unified `run`-based tool
|
|
817
|
+
*/
|
|
818
|
+
export declare function isUnifiedTool(tool: Tool): tool is UnifiedTool;
|
|
819
|
+
/**
|
|
820
|
+
* True when the tool can outlive its round: a unified tool with a
|
|
821
|
+
* non-'sync' lifecycle. Drives check-schema generation and placeholder
|
|
822
|
+
* wording.
|
|
823
|
+
*/
|
|
824
|
+
export declare function isLongRunningTool(tool: Tool): boolean;
|
|
825
|
+
/**
|
|
826
|
+
* True when the tool is an agent tool (`tool.agent()`): a unified tool
|
|
827
|
+
* whose run drives a child conversation.
|
|
828
|
+
*/
|
|
829
|
+
export declare function isAgentTool(tool: Tool): boolean;
|
|
830
|
+
/**
|
|
831
|
+
* Type guard to check if a tool has an execute function (regular or
|
|
832
|
+
* generator). Unified `run` tools are excluded — they have their own
|
|
833
|
+
* dispatch and this guard's predicate would misclassify them.
|
|
465
834
|
*/
|
|
466
835
|
export declare function hasExecuteFunction(tool: Tool): tool is ToolWithExecute | ToolWithGenerator;
|
|
467
836
|
/**
|
|
468
|
-
* Type guard to check if a tool uses a generator (has eventSchema)
|
|
837
|
+
* Type guard to check if a tool uses a generator (has eventSchema).
|
|
838
|
+
* Unified tools may declare an `eventSchema` for run yields but are not
|
|
839
|
+
* legacy generator tools.
|
|
469
840
|
*/
|
|
470
841
|
export declare function isGeneratorTool(tool: Tool): tool is ToolWithGenerator;
|
|
471
842
|
/**
|
|
@@ -473,7 +844,7 @@ export declare function isGeneratorTool(tool: Tool): tool is ToolWithGenerator;
|
|
|
473
844
|
*/
|
|
474
845
|
export declare function isRegularExecuteTool(tool: Tool): tool is ToolWithExecute;
|
|
475
846
|
/**
|
|
476
|
-
* Type guard to check if a tool is a manual tool (no execute
|
|
847
|
+
* Type guard to check if a tool is a manual tool (no execute, no onToolCalled, no run)
|
|
477
848
|
*/
|
|
478
849
|
export declare function isManualTool(tool: Tool): tool is ManualTool;
|
|
479
850
|
/**
|
|
@@ -481,11 +852,12 @@ export declare function isManualTool(tool: Tool): tool is ManualTool;
|
|
|
481
852
|
*/
|
|
482
853
|
export declare function isHITLTool(tool: Tool): tool is HITLTool;
|
|
483
854
|
/**
|
|
484
|
-
* Type guard: true if the tool can be auto-resolved within a turn —
|
|
485
|
-
*
|
|
486
|
-
*
|
|
855
|
+
* Type guard: true if the tool can be auto-resolved within a turn — through a
|
|
856
|
+
* client execute/generator function, a HITL onToolCalled hook, or a unified
|
|
857
|
+
* `run` (which always produces at least a placeholder output). Returns false
|
|
858
|
+
* for manual tools (which always pause) and server tools.
|
|
487
859
|
*/
|
|
488
|
-
export declare function isAutoResolvableTool(tool: Tool): tool is ToolWithExecute | ToolWithGenerator | HITLTool;
|
|
860
|
+
export declare function isAutoResolvableTool(tool: Tool): tool is ToolWithExecute | ToolWithGenerator | HITLTool | UnifiedTool;
|
|
489
861
|
/**
|
|
490
862
|
* Parsed tool call from API response
|
|
491
863
|
* @template T - The tool type to infer argument types from
|
|
@@ -620,10 +992,18 @@ export interface APITool {
|
|
|
620
992
|
/**
|
|
621
993
|
* Tool preliminary result event emitted during generator tool execution
|
|
622
994
|
* @template TEvent - The event type from the tool's eventSchema
|
|
995
|
+
* @template TName - The tool's name (literal when known)
|
|
623
996
|
*/
|
|
624
|
-
export type ToolPreliminaryResultEvent<TEvent = unknown> = {
|
|
997
|
+
export type ToolPreliminaryResultEvent<TEvent = unknown, TName extends string = string> = {
|
|
625
998
|
type: 'tool.preliminary_result';
|
|
626
999
|
toolCallId: string;
|
|
1000
|
+
/**
|
|
1001
|
+
* Name of the tool that produced this preliminary result.
|
|
1002
|
+
* Optional for source compatibility with legacy hand-constructed events
|
|
1003
|
+
* that predate this field; {@link CorrelatedToolPreliminaryResultEvent}
|
|
1004
|
+
* re-requires it as a literal for a concrete tool.
|
|
1005
|
+
*/
|
|
1006
|
+
toolName?: TName;
|
|
627
1007
|
result: TEvent;
|
|
628
1008
|
timestamp: number;
|
|
629
1009
|
};
|
|
@@ -632,10 +1012,18 @@ export type ToolPreliminaryResultEvent<TEvent = unknown> = {
|
|
|
632
1012
|
* Contains the final result and any preliminary results that were emitted
|
|
633
1013
|
* @template TResult - The result type from the tool's outputSchema
|
|
634
1014
|
* @template TPreliminaryResults - The event type from generator tools' eventSchema
|
|
1015
|
+
* @template TName - The tool's name (literal when known)
|
|
635
1016
|
*/
|
|
636
|
-
export type ToolResultEvent<TResult = unknown, TPreliminaryResults = unknown> = {
|
|
1017
|
+
export type ToolResultEvent<TResult = unknown, TPreliminaryResults = unknown, TName extends string = string> = {
|
|
637
1018
|
type: 'tool.result';
|
|
638
1019
|
toolCallId: string;
|
|
1020
|
+
/**
|
|
1021
|
+
* Name of the tool that produced this result.
|
|
1022
|
+
* Optional for source compatibility with legacy hand-constructed events
|
|
1023
|
+
* that predate this field; {@link CorrelatedToolResultEvent} re-requires
|
|
1024
|
+
* it as a literal for a concrete tool.
|
|
1025
|
+
*/
|
|
1026
|
+
toolName?: TName;
|
|
639
1027
|
/**
|
|
640
1028
|
* Origin of the tool: `'mcp'` for tools wrapped from a remote MCP server
|
|
641
1029
|
* (whose `result` is `unknown`), `'client'` for locally-defined tools. Lets
|
|
@@ -647,6 +1035,106 @@ export type ToolResultEvent<TResult = unknown, TPreliminaryResults = unknown> =
|
|
|
647
1035
|
timestamp: number;
|
|
648
1036
|
preliminaryResults?: TPreliminaryResults[];
|
|
649
1037
|
};
|
|
1038
|
+
/**
|
|
1039
|
+
* Name-correlated preliminary result event for one concrete tool.
|
|
1040
|
+
* Narrowing on `toolName` recovers this tool's event payload type.
|
|
1041
|
+
*
|
|
1042
|
+
* `toolName` is optional on the underlying {@link ToolPreliminaryResultEvent}
|
|
1043
|
+
* base (for legacy source compatibility), so it's overridden back to a
|
|
1044
|
+
* required literal here via `Omit<...> & {...}` — parameterizing the base
|
|
1045
|
+
* type alone does not re-require an optional field.
|
|
1046
|
+
*/
|
|
1047
|
+
export type CorrelatedToolPreliminaryResultEvent<T extends Tool> = Omit<ToolPreliminaryResultEvent<InferToolEvent<T>, InferToolName<T>>, 'toolName'> & {
|
|
1048
|
+
toolName: InferToolName<T>;
|
|
1049
|
+
};
|
|
1050
|
+
/**
|
|
1051
|
+
* Name-correlated final result event for one concrete tool.
|
|
1052
|
+
* Narrowing on `toolName` recovers this tool's result (and preliminary) types.
|
|
1053
|
+
*
|
|
1054
|
+
* `toolName` is optional on the underlying {@link ToolResultEvent} base (for
|
|
1055
|
+
* legacy source compatibility), so it's overridden back to a required
|
|
1056
|
+
* literal here via `Omit<...> & {...}` — parameterizing the base type alone
|
|
1057
|
+
* does not re-require an optional field.
|
|
1058
|
+
*
|
|
1059
|
+
* `result` unions in `{ error: string }` for concrete tools: at runtime,
|
|
1060
|
+
* `ModelResult` broadcasts this exact shape under the same `tool.result`
|
|
1061
|
+
* type and `toolName` for parse failures, thrown/rejected executions, and
|
|
1062
|
+
* tool-reported execution errors (see `broadcastToolResult` call sites in
|
|
1063
|
+
* `model-result.ts`). Without this, narrowing by `toolName` would let a
|
|
1064
|
+
* consumer safely (but incorrectly) access success-only output fields on an
|
|
1065
|
+
* error payload. The `_mcp: true` branch and the generic `readonly Tool[]`
|
|
1066
|
+
* fallback branch are left as `unknown`, which already structurally permits
|
|
1067
|
+
* `{ error: string }` — only the concrete-tool success branch needs the
|
|
1068
|
+
* explicit union.
|
|
1069
|
+
*/
|
|
1070
|
+
export type CorrelatedToolResultEvent<T extends Tool> = Omit<ToolResultEvent<T extends {
|
|
1071
|
+
readonly _mcp: true;
|
|
1072
|
+
} ? unknown : [
|
|
1073
|
+
Tool
|
|
1074
|
+
] extends [
|
|
1075
|
+
T
|
|
1076
|
+
] ? unknown : (T extends ToolWithExecute<$ZodObject<$ZodShape>, infer O> | ToolWithGenerator<$ZodObject<$ZodShape>, $ZodType<unknown>, infer O> | HITLTool<$ZodObject<$ZodShape>, infer O> ? zodInfer<O> : InferToolOutput<T>) | {
|
|
1077
|
+
error: string;
|
|
1078
|
+
}, T extends ToolWithGenerator<$ZodObject<$ZodShape>, infer E> ? zodInfer<E> : never, InferToolName<T>>, 'toolName'> & {
|
|
1079
|
+
toolName: InferToolName<T>;
|
|
1080
|
+
source: ToolSource<T>;
|
|
1081
|
+
};
|
|
1082
|
+
/**
|
|
1083
|
+
* Widest backward-compatible shape for {@link CorrelatedToolEventUnion} when
|
|
1084
|
+
* `T` is the generic `readonly Tool[]` (e.g. a tool handle from
|
|
1085
|
+
* `@openrouter/mcp`, whose concrete tuple isn't known at the type level).
|
|
1086
|
+
* Mirrors the pre-existing {@link ToolPreliminaryResultEvent} /
|
|
1087
|
+
* {@link ToolResultEvent} default shapes.
|
|
1088
|
+
*/
|
|
1089
|
+
type WidestCorrelatedToolEvent = ToolPreliminaryResultEvent | ToolResultEvent;
|
|
1090
|
+
/**
|
|
1091
|
+
* Final result emitted by the engine-injected `task` tool used to inspect,
|
|
1092
|
+
* steer, fetch, or cancel long-running tasks. The payload is `unknown`
|
|
1093
|
+
* because custom check handlers and completed task results are user-defined.
|
|
1094
|
+
*/
|
|
1095
|
+
export type BuiltinTaskToolEvent = Omit<ToolResultEvent<unknown, never, 'task'>, 'toolName'> & {
|
|
1096
|
+
toolName: 'task';
|
|
1097
|
+
source: 'client';
|
|
1098
|
+
};
|
|
1099
|
+
/**
|
|
1100
|
+
* Discriminated union of name-correlated tool events across a tools tuple.
|
|
1101
|
+
* Checking `event.toolName === 'my_tool'` narrows `result` to that tool's output.
|
|
1102
|
+
*
|
|
1103
|
+
* For the generic `readonly Tool[]` case, falls back to the widest
|
|
1104
|
+
* backward-compatible shape instead of collapsing to `never`: the mapped-type
|
|
1105
|
+
* check `T[K] extends ClientTool` is a non-distributive check on the indexed
|
|
1106
|
+
* access `T[K]` (only a *naked* type parameter distributes over a union), so
|
|
1107
|
+
* when `T[K]` resolves to the full `Tool` union (`ClientTool | ServerToolBase`)
|
|
1108
|
+
* the check fails as a monolithic comparison rather than narrowing per-member.
|
|
1109
|
+
*/
|
|
1110
|
+
export type CorrelatedToolEventUnion<T extends readonly Tool[]> = BuiltinTaskToolEvent | (readonly Tool[] extends T ? WidestCorrelatedToolEvent : {
|
|
1111
|
+
[K in keyof T]: T[K] extends ClientTool ? CorrelatedToolPreliminaryResultEvent<T[K]> | CorrelatedToolResultEvent<T[K]> : never;
|
|
1112
|
+
}[number]);
|
|
1113
|
+
/**
|
|
1114
|
+
* Widest backward-compatible shape for {@link CorrelatedToolStreamPreliminaryUnion}
|
|
1115
|
+
* when `T` is the generic `readonly Tool[]`. Mirrors the pre-existing
|
|
1116
|
+
* {@link ToolStreamEvent} preliminary-result shape.
|
|
1117
|
+
*/
|
|
1118
|
+
type WidestCorrelatedToolStreamPreliminary = {
|
|
1119
|
+
type: 'preliminary_result';
|
|
1120
|
+
toolCallId: string;
|
|
1121
|
+
toolName: string;
|
|
1122
|
+
result: unknown;
|
|
1123
|
+
};
|
|
1124
|
+
/**
|
|
1125
|
+
* Discriminated union of name-correlated preliminary stream events
|
|
1126
|
+
* (legacy `getToolStream` shape) across a tools tuple. Falls back to the
|
|
1127
|
+
* widest backward-compatible shape for the generic `readonly Tool[]` case;
|
|
1128
|
+
* see {@link CorrelatedToolEventUnion} for why the naive mapped check collapses.
|
|
1129
|
+
*/
|
|
1130
|
+
export type CorrelatedToolStreamPreliminaryUnion<T extends readonly Tool[]> = readonly Tool[] extends T ? WidestCorrelatedToolStreamPreliminary : {
|
|
1131
|
+
[K in keyof T]: T[K] extends ClientTool ? {
|
|
1132
|
+
type: 'preliminary_result';
|
|
1133
|
+
toolCallId: string;
|
|
1134
|
+
toolName: InferToolName<T[K]>;
|
|
1135
|
+
result: InferToolEvent<T[K]>;
|
|
1136
|
+
} : never;
|
|
1137
|
+
}[number];
|
|
650
1138
|
/**
|
|
651
1139
|
* Tool call output event carrying the fully-formed FunctionCallOutputItem.
|
|
652
1140
|
* Broadcast by executeToolRound so passive consumers (getItemsStream) can yield
|
|
@@ -657,6 +1145,38 @@ export type ToolCallOutputEvent = {
|
|
|
657
1145
|
output: models.FunctionCallOutputItem;
|
|
658
1146
|
timestamp: number;
|
|
659
1147
|
};
|
|
1148
|
+
/**
|
|
1149
|
+
* Emitted when an async tool call escapes the round: a background tool's
|
|
1150
|
+
* execute outlived its grace window, or a deferred tool's start returned a
|
|
1151
|
+
* task handle. The model has received a pending placeholder output.
|
|
1152
|
+
*/
|
|
1153
|
+
export type ToolAsyncStartedEvent = {
|
|
1154
|
+
type: 'tool.async_started';
|
|
1155
|
+
toolCallId: string;
|
|
1156
|
+
toolName: string;
|
|
1157
|
+
taskId: string;
|
|
1158
|
+
mode: ToolTaskMode;
|
|
1159
|
+
/** The model-facing acknowledgement carried in the placeholder, if any. */
|
|
1160
|
+
ack?: unknown;
|
|
1161
|
+
timestamp: number;
|
|
1162
|
+
};
|
|
1163
|
+
/**
|
|
1164
|
+
* Emitted when an async tool task settles — completed, failed, cancelled,
|
|
1165
|
+
* timed out, or expired. `delivery` reports how (or whether) the outcome
|
|
1166
|
+
* reached the model: `'injected'` into this run's conversation,
|
|
1167
|
+
* `'pending_resume'` recorded on state for the next run, or `'dropped'`
|
|
1168
|
+
* (run ended under `onRunEnd: 'detach'`).
|
|
1169
|
+
*/
|
|
1170
|
+
export type ToolAsyncSettledEvent<TResult = unknown> = {
|
|
1171
|
+
type: 'tool.async_settled';
|
|
1172
|
+
toolCallId: string;
|
|
1173
|
+
taskId: string;
|
|
1174
|
+
status: 'completed' | 'failed' | 'cancelled' | 'timed_out' | 'expired';
|
|
1175
|
+
result?: TResult;
|
|
1176
|
+
error?: string;
|
|
1177
|
+
delivery: 'injected' | 'pending_resume' | 'dropped';
|
|
1178
|
+
timestamp: number;
|
|
1179
|
+
};
|
|
660
1180
|
/**
|
|
661
1181
|
* Turn start event emitted at the beginning of each API turn
|
|
662
1182
|
* Turn 0 is the initial request, subsequent turns follow tool execution
|
|
@@ -680,16 +1200,32 @@ export type TurnEndEvent = {
|
|
|
680
1200
|
* and turn delimiter events for multi-turn streaming
|
|
681
1201
|
* @template TEvent - The event type from generator tools
|
|
682
1202
|
* @template TResult - The result type from tool execution
|
|
1203
|
+
* @template TName - Tool name (literal when known)
|
|
1204
|
+
*/
|
|
1205
|
+
export type ResponseStreamEvent<TEvent = unknown, TResult = unknown, TName extends string = string> = StreamEvents | ToolPreliminaryResultEvent<TEvent, TName> | ToolResultEvent<TResult, TEvent, TName> | ToolCallOutputEvent | ToolAsyncStartedEvent | ToolAsyncSettledEvent<TResult> | TurnStartEvent | TurnEndEvent;
|
|
1206
|
+
/**
|
|
1207
|
+
* Name-correlated stream events for a concrete tools tuple.
|
|
1208
|
+
* Prefer this (or {@link ModelResult.getFullResponsesStream}) when callers need
|
|
1209
|
+
* `event.toolName` narrowing; the default {@link ResponseStreamEvent} keeps a
|
|
1210
|
+
* wide, backward-compatible shape.
|
|
1211
|
+
*/
|
|
1212
|
+
export type CorrelatedResponseStreamEvent<TTools extends readonly Tool[]> = StreamEvents | CorrelatedToolEventUnion<TTools> | ToolCallOutputEvent | ToolAsyncStartedEvent | ToolAsyncSettledEvent<InferToolOutputsUnion<TTools>> | TurnStartEvent | TurnEndEvent;
|
|
1213
|
+
/**
|
|
1214
|
+
* Type guard to check if an event is a tool async started event
|
|
1215
|
+
*/
|
|
1216
|
+
export declare function isToolAsyncStartedEvent(event: ResponseStreamEvent): event is ToolAsyncStartedEvent;
|
|
1217
|
+
/**
|
|
1218
|
+
* Type guard to check if an event is a tool async settled event
|
|
683
1219
|
*/
|
|
684
|
-
export
|
|
1220
|
+
export declare function isToolAsyncSettledEvent<TResult = unknown>(event: ResponseStreamEvent<unknown, TResult>): event is ToolAsyncSettledEvent<TResult>;
|
|
685
1221
|
/**
|
|
686
1222
|
* Type guard to check if an event is a tool preliminary result event
|
|
687
1223
|
*/
|
|
688
|
-
export declare function isToolPreliminaryResultEvent<TEvent = unknown>(event: ResponseStreamEvent<TEvent>): event is ToolPreliminaryResultEvent<TEvent>;
|
|
1224
|
+
export declare function isToolPreliminaryResultEvent<TEvent = unknown, TName extends string = string>(event: ResponseStreamEvent<TEvent, unknown, TName>): event is ToolPreliminaryResultEvent<TEvent, TName>;
|
|
689
1225
|
/**
|
|
690
1226
|
* Type guard to check if an event is a tool result event
|
|
691
1227
|
*/
|
|
692
|
-
export declare function isToolResultEvent<TResult = unknown, TPreliminaryResults = unknown>(event: ResponseStreamEvent<TPreliminaryResults, TResult>): event is ToolResultEvent<TResult, TPreliminaryResults>;
|
|
1228
|
+
export declare function isToolResultEvent<TResult = unknown, TPreliminaryResults = unknown, TName extends string = string>(event: ResponseStreamEvent<TPreliminaryResults, TResult, TName>): event is ToolResultEvent<TResult, TPreliminaryResults, TName>;
|
|
693
1229
|
/**
|
|
694
1230
|
* Type guard to check if an event is a tool call output event
|
|
695
1231
|
*/
|
|
@@ -706,21 +1242,37 @@ export declare function isTurnEndEvent(event: ResponseStreamEvent): event is Tur
|
|
|
706
1242
|
* Tool stream event types for getToolStream
|
|
707
1243
|
* Includes both argument deltas and preliminary results
|
|
708
1244
|
* @template TEvent - The event type from generator tools
|
|
1245
|
+
* @template TName - Tool name (literal when known)
|
|
709
1246
|
*/
|
|
710
|
-
export type ToolStreamEvent<TEvent = unknown> = {
|
|
1247
|
+
export type ToolStreamEvent<TEvent = unknown, TName extends string = string> = {
|
|
711
1248
|
type: 'delta';
|
|
712
1249
|
content: string;
|
|
713
1250
|
} | {
|
|
714
1251
|
type: 'preliminary_result';
|
|
715
1252
|
toolCallId: string;
|
|
1253
|
+
/**
|
|
1254
|
+
* Optional for source compatibility with legacy hand-constructed
|
|
1255
|
+
* events; {@link CorrelatedToolStreamEvent} re-requires it as a
|
|
1256
|
+
* literal for a concrete tool.
|
|
1257
|
+
*/
|
|
1258
|
+
toolName?: TName;
|
|
716
1259
|
result: TEvent;
|
|
717
1260
|
};
|
|
1261
|
+
/**
|
|
1262
|
+
* Name-correlated tool stream events for a concrete tools tuple.
|
|
1263
|
+
* Checking `event.toolName` on a `preliminary_result` narrows `result`.
|
|
1264
|
+
*/
|
|
1265
|
+
export type CorrelatedToolStreamEvent<TTools extends readonly Tool[]> = {
|
|
1266
|
+
type: 'delta';
|
|
1267
|
+
content: string;
|
|
1268
|
+
} | CorrelatedToolStreamPreliminaryUnion<TTools>;
|
|
718
1269
|
/**
|
|
719
1270
|
* Chat stream event types for getFullChatStream
|
|
720
1271
|
* Includes content deltas, completion events, and tool preliminary results
|
|
721
1272
|
* @template TEvent - The event type from generator tools
|
|
1273
|
+
* @template TName - Tool name (literal when known)
|
|
722
1274
|
*/
|
|
723
|
-
export type ChatStreamEvent<TEvent = unknown> = {
|
|
1275
|
+
export type ChatStreamEvent<TEvent = unknown, TName extends string = string> = {
|
|
724
1276
|
type: 'content.delta';
|
|
725
1277
|
delta: string;
|
|
726
1278
|
} | {
|
|
@@ -729,6 +1281,11 @@ export type ChatStreamEvent<TEvent = unknown> = {
|
|
|
729
1281
|
} | {
|
|
730
1282
|
type: 'tool.preliminary_result';
|
|
731
1283
|
toolCallId: string;
|
|
1284
|
+
/**
|
|
1285
|
+
* Optional for source compatibility with legacy hand-constructed
|
|
1286
|
+
* events that predate this field.
|
|
1287
|
+
*/
|
|
1288
|
+
toolName?: TName;
|
|
732
1289
|
result: TEvent;
|
|
733
1290
|
} | {
|
|
734
1291
|
type: string;
|
|
@@ -781,8 +1338,52 @@ export interface PartialResponse<TTools extends readonly Tool[] = readonly Tool[
|
|
|
781
1338
|
* fn) tool calls are unresolved; the loop stopped so the caller can execute
|
|
782
1339
|
* them client-side and continue. Distinct from `awaiting_hitl` — HITL tools
|
|
783
1340
|
* have an `onToolCalled` hook; manual tools do not.
|
|
784
|
-
|
|
785
|
-
|
|
1341
|
+
* - `awaiting_async_tools`: one or more deferred tools (`tool.deferred`)
|
|
1342
|
+
* started a durable external task; the loop paused until the task is
|
|
1343
|
+
* resolved via the tool's `.resolve()` / `.fail()` / `.cancel()` methods
|
|
1344
|
+
* (or `resumeToolResults()`), possibly from a different process. Distinct
|
|
1345
|
+
* from `awaiting_client_tools` — the tool DID execute (its `start` ran and
|
|
1346
|
+
* a placeholder output was persisted); only its final result is pending.
|
|
1347
|
+
*/
|
|
1348
|
+
export type ConversationStatus = 'complete' | 'interrupted' | 'awaiting_approval' | 'awaiting_hitl' | 'awaiting_client_tools' | 'awaiting_async_tools' | 'in_progress';
|
|
1349
|
+
export type { ToolTaskStatus } from './tool-task.js';
|
|
1350
|
+
/**
|
|
1351
|
+
* A pending (or settled) async tool task tracked on
|
|
1352
|
+
* {@link ConversationState.pendingAsyncTools}. One entry per background /
|
|
1353
|
+
* deferred call that produced a placeholder output.
|
|
1354
|
+
*/
|
|
1355
|
+
export interface PendingAsyncTool {
|
|
1356
|
+
/** The originating `function_call`'s call id. */
|
|
1357
|
+
callId: string;
|
|
1358
|
+
/** Durable task id (deferred: caller-supplied; background/agent: generated). */
|
|
1359
|
+
taskId: string;
|
|
1360
|
+
/** The tool's name. */
|
|
1361
|
+
name: string;
|
|
1362
|
+
/** How the task escapes the round. */
|
|
1363
|
+
mode: ToolTaskMode;
|
|
1364
|
+
/** Current lifecycle status (MCP-Tasks-compatible vocabulary). */
|
|
1365
|
+
status: ToolTaskStatus;
|
|
1366
|
+
/** Unix ms when the task started. */
|
|
1367
|
+
startedAt: number;
|
|
1368
|
+
/** Unix ms after which the task is considered expired. */
|
|
1369
|
+
expiresAt?: number;
|
|
1370
|
+
/** Poll-interval hint surfaced to the model and external pollers. */
|
|
1371
|
+
pollAfterMs?: number;
|
|
1372
|
+
/**
|
|
1373
|
+
* Set on a background task left running when the run ended under
|
|
1374
|
+
* `onRunEnd: 'detach'` — its result will never be delivered.
|
|
1375
|
+
*/
|
|
1376
|
+
orphaned?: boolean;
|
|
1377
|
+
/**
|
|
1378
|
+
* The most recent log entry, truncated (~200 chars) — the one piece of
|
|
1379
|
+
* progress that survives a process restart. Additive within
|
|
1380
|
+
* ConversationState version 1.
|
|
1381
|
+
*/
|
|
1382
|
+
lastLog?: {
|
|
1383
|
+
at: number;
|
|
1384
|
+
text: string;
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
786
1387
|
/**
|
|
787
1388
|
* State for multi-turn conversations with persistence and approval gates
|
|
788
1389
|
* @template TTools - The tools array type for proper type inference
|
|
@@ -803,6 +1404,16 @@ export interface ConversationState<TTools extends readonly Tool[] = readonly Too
|
|
|
803
1404
|
messages: models.InputsUnion;
|
|
804
1405
|
/** Previous response ID for chaining (OpenRouter server-side optimization) */
|
|
805
1406
|
previousResponseId?: string;
|
|
1407
|
+
/**
|
|
1408
|
+
* RFC 8785 canonical key of the forced `toolChoice` most recently consumed
|
|
1409
|
+
* by the active logical run. Persisted across approval, HITL, client-tool,
|
|
1410
|
+
* and async-tool pauses so an unchanged forced choice stays relaxed after
|
|
1411
|
+
* resume while a newly resolved forced value can re-arm.
|
|
1412
|
+
*
|
|
1413
|
+
* Cleared after an unforced caller choice is successfully dispatched or the
|
|
1414
|
+
* run reaches a terminal exit.
|
|
1415
|
+
*/
|
|
1416
|
+
consumedForcedToolChoiceKey?: string;
|
|
806
1417
|
/** Tool calls awaiting human approval */
|
|
807
1418
|
pendingToolCalls?: Array<ParsedToolCall<TTools[number]>>;
|
|
808
1419
|
/** Tool results executed but not yet sent to the model */
|
|
@@ -811,6 +1422,25 @@ export interface ConversationState<TTools extends readonly Tool[] = readonly Too
|
|
|
811
1422
|
partialResponse?: PartialResponse<TTools>;
|
|
812
1423
|
/** Signal from a new request to interrupt this conversation */
|
|
813
1424
|
interruptedBy?: string;
|
|
1425
|
+
/**
|
|
1426
|
+
* Doom-loop detector state (see the `doomLoop` option on `callModel`).
|
|
1427
|
+
* Bounded plain JSON, persisted so repetition streaks survive
|
|
1428
|
+
* serialize → resume. Absent when detection is off. Additive within
|
|
1429
|
+
* ConversationState version 1.
|
|
1430
|
+
*/
|
|
1431
|
+
doomLoop?: DoomLoopSerializedState;
|
|
1432
|
+
/**
|
|
1433
|
+
* Async tool tasks (background / deferred) whose placeholder output was
|
|
1434
|
+
* sent to the model but whose real result has not been delivered yet.
|
|
1435
|
+
* Additive within ConversationState version 1.
|
|
1436
|
+
*/
|
|
1437
|
+
pendingAsyncTools?: PendingAsyncTool[];
|
|
1438
|
+
/**
|
|
1439
|
+
* Call ids whose async result has already been delivered — the
|
|
1440
|
+
* at-most-once guard against replayed webhook resolutions. Additive
|
|
1441
|
+
* within ConversationState version 1.
|
|
1442
|
+
*/
|
|
1443
|
+
settledAsyncCallIds?: string[];
|
|
814
1444
|
/** Current status of the conversation */
|
|
815
1445
|
status: ConversationStatus;
|
|
816
1446
|
/** Creation timestamp (Unix ms) */
|
|
@@ -863,5 +1493,4 @@ export declare function toolHasApprovalConfigured(tool: Tool): boolean;
|
|
|
863
1493
|
* Type guard to check if any tools in array have approval configured at runtime
|
|
864
1494
|
*/
|
|
865
1495
|
export declare function hasApprovalRequiredTools(tools: readonly Tool[]): boolean;
|
|
866
|
-
export {};
|
|
867
1496
|
//# sourceMappingURL=tool-types.d.ts.map
|