@cloudflare/think 0.2.5 → 0.4.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 +90 -6
- package/dist/{classPrivateFieldGet2-fyJ3k1Ht.js → classPrivateFieldGet2-C7fYS0JL.js} +5 -5
- package/dist/extensions/index.js +2 -2
- package/dist/hook-proxy-5248W-ea.js +112 -0
- package/dist/hook-proxy-5248W-ea.js.map +1 -0
- package/dist/{manager-BTBhPS6g.js → manager-CR9ZdLiu.js} +8 -4
- package/dist/{manager-BTBhPS6g.js.map → manager-CR9ZdLiu.js.map} +1 -1
- package/dist/think.d.ts +275 -143
- package/dist/think.js +244 -53
- package/dist/think.js.map +1 -1
- package/dist/tools/browser.d.ts +1 -1
- package/dist/tools/browser.js +1 -1
- package/dist/tools/browser.js.map +1 -1
- package/dist/tools/extensions.js +1 -1
- package/package.json +2 -2
- package/dist/hook-proxy-f_DqtJ9u.js +0 -32
- package/dist/hook-proxy-f_DqtJ9u.js.map +0 -1
package/dist/think.d.ts
CHANGED
|
@@ -5,7 +5,14 @@ import {
|
|
|
5
5
|
import {
|
|
6
6
|
LanguageModel,
|
|
7
7
|
ModelMessage,
|
|
8
|
+
StepResult,
|
|
9
|
+
StreamTextOnChunkCallback,
|
|
10
|
+
StreamTextOnStepFinishCallback,
|
|
11
|
+
TextStreamPart,
|
|
8
12
|
ToolSet,
|
|
13
|
+
TypedToolCall,
|
|
14
|
+
TypedToolCall as TypedToolCall$1,
|
|
15
|
+
TypedToolResult,
|
|
9
16
|
UIMessage,
|
|
10
17
|
streamText
|
|
11
18
|
} from "ai";
|
|
@@ -15,7 +22,19 @@ import {
|
|
|
15
22
|
FiberRecoveryContext,
|
|
16
23
|
FiberRecoveryContext as FiberRecoveryContext$1
|
|
17
24
|
} from "agents";
|
|
18
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
ChatRecoveryContext,
|
|
27
|
+
ChatRecoveryContext as ChatRecoveryContext$1,
|
|
28
|
+
ChatRecoveryOptions,
|
|
29
|
+
ChatRecoveryOptions as ChatRecoveryOptions$1,
|
|
30
|
+
ChatResponseResult,
|
|
31
|
+
ChatResponseResult as ChatResponseResult$1,
|
|
32
|
+
ClientToolSchema,
|
|
33
|
+
MessageConcurrency,
|
|
34
|
+
MessageConcurrency as MessageConcurrency$1,
|
|
35
|
+
SaveMessagesResult,
|
|
36
|
+
SaveMessagesResult as SaveMessagesResult$1
|
|
37
|
+
} from "agents/chat";
|
|
19
38
|
import {
|
|
20
39
|
Session,
|
|
21
40
|
Session as Session$1
|
|
@@ -48,40 +67,6 @@ interface ChatOptions {
|
|
|
48
67
|
signal?: AbortSignal;
|
|
49
68
|
tools?: ToolSet;
|
|
50
69
|
}
|
|
51
|
-
/**
|
|
52
|
-
* Result returned by `saveMessages()` and `continueLastTurn()`.
|
|
53
|
-
*/
|
|
54
|
-
type SaveMessagesResult = {
|
|
55
|
-
requestId: string;
|
|
56
|
-
status: "completed" | "skipped";
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* Context passed to `onChatRecovery` when an interrupted chat stream
|
|
60
|
-
* is detected after DO restart.
|
|
61
|
-
*/
|
|
62
|
-
type ChatRecoveryContext = {
|
|
63
|
-
streamId: string;
|
|
64
|
-
requestId: string;
|
|
65
|
-
partialText: string;
|
|
66
|
-
partialParts: MessagePart[];
|
|
67
|
-
recoveryData: unknown | null;
|
|
68
|
-
messages: UIMessage[];
|
|
69
|
-
lastBody?: Record<string, unknown>;
|
|
70
|
-
lastClientTools?: ClientToolSchema[];
|
|
71
|
-
/**
|
|
72
|
-
* Epoch milliseconds when the underlying fiber was started. Compare against
|
|
73
|
-
* `Date.now()` to suppress continuations for turns that have been orphaned
|
|
74
|
-
* too long to safely replay.
|
|
75
|
-
*/
|
|
76
|
-
createdAt: number;
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* Options returned from `onChatRecovery` to control recovery behavior.
|
|
80
|
-
*/
|
|
81
|
-
type ChatRecoveryOptions = {
|
|
82
|
-
persist?: boolean;
|
|
83
|
-
continue?: boolean;
|
|
84
|
-
};
|
|
85
70
|
/**
|
|
86
71
|
* A chat turn request. Built automatically by each entry path
|
|
87
72
|
* (WebSocket, chat(), saveMessages, auto-continuation) and passed
|
|
@@ -139,74 +124,141 @@ interface TurnConfig {
|
|
|
139
124
|
providerOptions?: Record<string, unknown>;
|
|
140
125
|
}
|
|
141
126
|
/**
|
|
142
|
-
* Context passed to the `beforeToolCall` hook
|
|
143
|
-
*
|
|
127
|
+
* Context passed to the `beforeToolCall` hook **before** the tool's
|
|
128
|
+
* `execute` function runs.
|
|
129
|
+
*
|
|
130
|
+
* Backed by the AI SDK's `OnToolCallStartEvent` (the parameter of
|
|
131
|
+
* `experimental_onToolCallStart`). The full `TypedToolCall<TOOLS>`
|
|
132
|
+
* fields (`toolName`, `toolCallId`, `input`, `providerMetadata`, the
|
|
133
|
+
* dynamic/invalid/error discriminators) are spread at the top level for
|
|
134
|
+
* convenience, with the per-call event extras attached:
|
|
135
|
+
*
|
|
136
|
+
* - `stepNumber` — index of the current step
|
|
137
|
+
* - `messages` — conversation messages visible at tool execution time
|
|
138
|
+
* - `abortSignal` — signal that aborts if the turn is cancelled
|
|
139
|
+
*
|
|
140
|
+
* Pass an explicit `TOOLS` generic for full input typing:
|
|
141
|
+
*
|
|
142
|
+
* ```ts
|
|
143
|
+
* import type { ToolCallContext } from "@cloudflare/think";
|
|
144
|
+
* import type { tools } from "./my-tools";
|
|
145
|
+
*
|
|
146
|
+
* beforeToolCall(ctx: ToolCallContext<typeof tools>) {
|
|
147
|
+
* if (ctx.toolName === "search") {
|
|
148
|
+
* ctx.input.query; // typed
|
|
149
|
+
* }
|
|
150
|
+
* }
|
|
151
|
+
* ```
|
|
144
152
|
*/
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
153
|
+
type ToolCallContext<TOOLS extends ToolSet = ToolSet> =
|
|
154
|
+
TypedToolCall$1<TOOLS> & {
|
|
155
|
+
/** Zero-based index of the current step where this tool call occurs. */ readonly stepNumber:
|
|
156
|
+
| number
|
|
157
|
+
| undefined /** The conversation messages available at tool execution time. */;
|
|
158
|
+
readonly messages: ReadonlyArray<ModelMessage> /** Signal for cancelling the operation. */;
|
|
159
|
+
readonly abortSignal: AbortSignal | undefined;
|
|
160
|
+
};
|
|
151
161
|
/**
|
|
152
162
|
* Decision returned by `beforeToolCall` to control tool execution.
|
|
153
|
-
* Return void/undefined to allow execution with original
|
|
163
|
+
* Return void/undefined to allow execution with original input.
|
|
154
164
|
*
|
|
155
165
|
* Discriminated union — each action has a clear, non-overlapping meaning:
|
|
156
|
-
* - `allow` — execute the tool (optionally with modified
|
|
166
|
+
* - `allow` — execute the tool (optionally with modified input)
|
|
157
167
|
* - `block` — don't execute; return `reason` as the tool result so the model can adjust
|
|
158
|
-
* - `substitute` — don't execute; return `
|
|
168
|
+
* - `substitute` — don't execute; return `output` as the tool result (afterToolCall still fires)
|
|
159
169
|
*/
|
|
160
170
|
type ToolCallDecision =
|
|
161
171
|
| {
|
|
162
|
-
action: "allow" /** Modified
|
|
163
|
-
|
|
172
|
+
action: "allow" /** Modified input — tool executes with this instead of the original. */;
|
|
173
|
+
input?: Record<string, unknown>;
|
|
164
174
|
}
|
|
165
175
|
| {
|
|
166
176
|
action: "block" /** Returned as the tool result so the model can adjust. */;
|
|
167
177
|
reason?: string;
|
|
168
178
|
}
|
|
169
179
|
| {
|
|
170
|
-
action: "substitute" /** The substitute tool
|
|
171
|
-
|
|
172
|
-
|
|
180
|
+
action: "substitute" /** The substitute tool output — model sees this instead of real execution. */;
|
|
181
|
+
output: unknown /** Optional input attribution for the afterToolCall log. */;
|
|
182
|
+
input?: Record<string, unknown>;
|
|
173
183
|
};
|
|
174
184
|
/**
|
|
175
185
|
* Context passed to the `afterToolCall` hook after a tool executes.
|
|
186
|
+
*
|
|
187
|
+
* Backed by the AI SDK's `OnToolCallFinishEvent` (the parameter of
|
|
188
|
+
* `experimental_onToolCallFinish`). The full `TypedToolCall<TOOLS>`
|
|
189
|
+
* fields (`toolName`, `toolCallId`, `input`, …) are spread at the top
|
|
190
|
+
* level, plus the per-call event extras:
|
|
191
|
+
*
|
|
192
|
+
* - `stepNumber` — index of the current step
|
|
193
|
+
* - `messages` — conversation messages visible at tool execution time
|
|
194
|
+
* - `durationMs` — wall-clock execution time in milliseconds
|
|
195
|
+
* - `success`/`output`/`error` — discriminated outcome:
|
|
196
|
+
* - on success: `success: true`, `output: unknown`
|
|
197
|
+
* - on failure: `success: false`, `error: unknown`
|
|
198
|
+
*
|
|
199
|
+
* Pass an explicit `TOOLS` generic for full input typing:
|
|
200
|
+
*
|
|
201
|
+
* ```ts
|
|
202
|
+
* import type { ToolCallResultContext } from "@cloudflare/think";
|
|
203
|
+
* import type { tools } from "./my-tools";
|
|
204
|
+
*
|
|
205
|
+
* afterToolCall(ctx: ToolCallResultContext<typeof tools>) {
|
|
206
|
+
* if (ctx.success) {
|
|
207
|
+
* console.log(`${ctx.toolName} took ${ctx.durationMs}ms`, ctx.output);
|
|
208
|
+
* } else {
|
|
209
|
+
* console.error(`${ctx.toolName} failed:`, ctx.error);
|
|
210
|
+
* }
|
|
211
|
+
* }
|
|
212
|
+
* ```
|
|
176
213
|
*/
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
214
|
+
type ToolCallResultContext<TOOLS extends ToolSet = ToolSet> =
|
|
215
|
+
TypedToolCall$1<TOOLS> & {
|
|
216
|
+
readonly stepNumber: number | undefined;
|
|
217
|
+
readonly messages: ReadonlyArray<ModelMessage> /** Wall-clock execution time in milliseconds. */;
|
|
218
|
+
readonly durationMs: number;
|
|
219
|
+
} & (
|
|
220
|
+
| {
|
|
221
|
+
readonly success: true;
|
|
222
|
+
readonly output: unknown;
|
|
223
|
+
readonly error?: never;
|
|
224
|
+
}
|
|
225
|
+
| {
|
|
226
|
+
readonly success: false;
|
|
227
|
+
readonly output?: never;
|
|
228
|
+
readonly error: unknown;
|
|
229
|
+
}
|
|
230
|
+
);
|
|
185
231
|
/**
|
|
186
232
|
* Context passed to the `onStepFinish` hook after each step completes.
|
|
187
|
-
*
|
|
233
|
+
*
|
|
234
|
+
* This is the AI SDK's `StepResult<TOOLS>` (= `OnStepFinishEvent<TOOLS>`) —
|
|
235
|
+
* the full step record including `text`, `reasoning`, `toolCalls`,
|
|
236
|
+
* `toolResults`, `files`, `sources`, `usage` (with `cachedInputTokens`,
|
|
237
|
+
* `reasoningTokens`, `totalTokens`), `finishReason`, `warnings`, `request`,
|
|
238
|
+
* `response`, and `providerMetadata` (where provider-specific cache
|
|
239
|
+
* accounting like `cacheCreationInputTokens` lives).
|
|
240
|
+
*
|
|
241
|
+
* Pass an explicit `TOOLS` generic for typed `toolCalls`/`toolResults`.
|
|
188
242
|
*/
|
|
189
|
-
type StepContext =
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
| "continue"
|
|
193
|
-
| "tool-result" /** Text generated in this step. */;
|
|
194
|
-
text: string /** Tool calls made in this step. */;
|
|
195
|
-
toolCalls: unknown[] /** Tool results received in this step. */;
|
|
196
|
-
toolResults: unknown[] /** Why the step finished. */;
|
|
197
|
-
finishReason: string /** Token usage for this step. */;
|
|
198
|
-
usage: {
|
|
199
|
-
inputTokens: number;
|
|
200
|
-
outputTokens: number;
|
|
201
|
-
};
|
|
202
|
-
};
|
|
243
|
+
type StepContext<TOOLS extends ToolSet = ToolSet> = Parameters<
|
|
244
|
+
StreamTextOnStepFinishCallback<TOOLS>
|
|
245
|
+
>[0];
|
|
203
246
|
/**
|
|
204
247
|
* Context passed to the `onChunk` hook for each streaming chunk.
|
|
205
|
-
*
|
|
248
|
+
*
|
|
249
|
+
* This is the AI SDK's `StreamTextOnChunkCallback` event — `{ chunk }`
|
|
250
|
+
* where `chunk` is a discriminated union of `TextStreamPart` variants
|
|
251
|
+
* (text-delta, reasoning-delta, source, tool-call, tool-input-start,
|
|
252
|
+
* tool-input-delta, tool-result, raw).
|
|
206
253
|
*/
|
|
207
|
-
type ChunkContext =
|
|
208
|
-
|
|
209
|
-
|
|
254
|
+
type ChunkContext<TOOLS extends ToolSet = ToolSet> = Parameters<
|
|
255
|
+
StreamTextOnChunkCallback<TOOLS>
|
|
256
|
+
>[0];
|
|
257
|
+
/**
|
|
258
|
+
* @internal Re-export of the chunk variant union for consumers that need
|
|
259
|
+
* to narrow on `chunk.type` without importing `TextStreamPart` directly.
|
|
260
|
+
*/
|
|
261
|
+
type ChunkPart<TOOLS extends ToolSet = ToolSet> = ChunkContext<TOOLS>["chunk"];
|
|
210
262
|
/**
|
|
211
263
|
* Configuration for a sandboxed extension, returned by getExtensions().
|
|
212
264
|
*/
|
|
@@ -216,41 +268,6 @@ interface ExtensionConfig {
|
|
|
216
268
|
/** JavaScript source code defining the extension's tools. */
|
|
217
269
|
source: string;
|
|
218
270
|
}
|
|
219
|
-
/**
|
|
220
|
-
* Controls how overlapping user submit requests behave while another
|
|
221
|
-
* chat turn is already active or queued.
|
|
222
|
-
*
|
|
223
|
-
* - `"queue"` (default) — queue every submit and process them in order.
|
|
224
|
-
* - `"latest"` — keep only the latest overlapping submit; superseded submits
|
|
225
|
-
* still persist their user messages, but do not start their own model turn.
|
|
226
|
-
* - `"merge"` — like latest, but all overlapping user messages remain in
|
|
227
|
-
* the conversation history. The model sees them all in one turn.
|
|
228
|
-
* - `"drop"` — ignore overlapping submits entirely (messages not persisted).
|
|
229
|
-
* - `{ strategy: "debounce" }` — trailing-edge latest with a quiet window.
|
|
230
|
-
*
|
|
231
|
-
* Only applies to `submit-message` requests. Regenerations, tool
|
|
232
|
-
* continuations, approvals, clears, `saveMessages`, and `continueLastTurn`
|
|
233
|
-
* keep their existing serialized behavior.
|
|
234
|
-
*/
|
|
235
|
-
type MessageConcurrency =
|
|
236
|
-
| "queue"
|
|
237
|
-
| "latest"
|
|
238
|
-
| "merge"
|
|
239
|
-
| "drop"
|
|
240
|
-
| {
|
|
241
|
-
strategy: "debounce";
|
|
242
|
-
debounceMs?: number;
|
|
243
|
-
};
|
|
244
|
-
/**
|
|
245
|
-
* Result passed to `onChatResponse` after a chat turn completes.
|
|
246
|
-
*/
|
|
247
|
-
type ChatResponseResult = {
|
|
248
|
-
message: UIMessage;
|
|
249
|
-
requestId: string;
|
|
250
|
-
continuation: boolean;
|
|
251
|
-
status: "completed" | "error" | "aborted";
|
|
252
|
-
error?: string;
|
|
253
|
-
};
|
|
254
271
|
/**
|
|
255
272
|
* An opinionated chat agent base class.
|
|
256
273
|
*
|
|
@@ -258,8 +275,9 @@ type ChatResponseResult = {
|
|
|
258
275
|
*/
|
|
259
276
|
declare class Think<
|
|
260
277
|
Env extends Cloudflare.Env = Cloudflare.Env,
|
|
261
|
-
|
|
262
|
-
|
|
278
|
+
State = unknown,
|
|
279
|
+
Props extends Record<string, unknown> = Record<string, unknown>
|
|
280
|
+
> extends Agent<Env, State, Props> {
|
|
263
281
|
#private;
|
|
264
282
|
/**
|
|
265
283
|
* Wait for MCP server connections to be ready before the inference
|
|
@@ -279,7 +297,7 @@ declare class Think<
|
|
|
279
297
|
*
|
|
280
298
|
* @default "queue"
|
|
281
299
|
*/
|
|
282
|
-
messageConcurrency: MessageConcurrency;
|
|
300
|
+
messageConcurrency: MessageConcurrency$1;
|
|
283
301
|
/**
|
|
284
302
|
* When true, chat turns are wrapped in `runFiber` for durable execution.
|
|
285
303
|
* Enables `onChatRecovery` hook and `this.stash()` during streaming.
|
|
@@ -335,14 +353,29 @@ declare class Think<
|
|
|
335
353
|
private _activeDebounceResolve;
|
|
336
354
|
private static MESSAGE_DEBOUNCE_MS;
|
|
337
355
|
/**
|
|
338
|
-
* Persist
|
|
339
|
-
* Stored in
|
|
356
|
+
* Persist an arbitrary JSON-serializable configuration object for this
|
|
357
|
+
* agent instance. Stored in the assistant_config table — survives
|
|
358
|
+
* restarts and hibernation. Pass the config shape as a method generic
|
|
359
|
+
* for typed call sites:
|
|
360
|
+
*
|
|
361
|
+
* ```ts
|
|
362
|
+
* this.configure<MyConfig>({ modelTier: "fast" });
|
|
363
|
+
* ```
|
|
364
|
+
*
|
|
365
|
+
* Prefer `state` / `setState` from `Agent` when you want the value
|
|
366
|
+
* broadcast to connected clients. Use `configure` for private
|
|
367
|
+
* per-instance config that should stay server-side.
|
|
340
368
|
*/
|
|
341
|
-
configure(config:
|
|
369
|
+
configure<T = Record<string, unknown>>(config: T): void;
|
|
342
370
|
/**
|
|
343
371
|
* Read the persisted configuration, or null if never configured.
|
|
372
|
+
* Pass the config shape as a method generic for a typed result:
|
|
373
|
+
*
|
|
374
|
+
* ```ts
|
|
375
|
+
* const cfg = this.getConfig<MyConfig>();
|
|
376
|
+
* ```
|
|
344
377
|
*/
|
|
345
|
-
getConfig():
|
|
378
|
+
getConfig<T = Record<string, unknown>>(): T | null;
|
|
346
379
|
private _ensureConfigTable;
|
|
347
380
|
private _configSet;
|
|
348
381
|
private _configGet;
|
|
@@ -405,24 +438,49 @@ declare class Think<
|
|
|
405
438
|
*/
|
|
406
439
|
beforeTurn(_ctx: TurnContext): TurnConfig | void | Promise<TurnConfig | void>;
|
|
407
440
|
/**
|
|
408
|
-
* Called
|
|
409
|
-
*
|
|
441
|
+
* Called **before** the tool's `execute` function runs. Think wraps
|
|
442
|
+
* every tool's `execute` so it can consult this hook and act on the
|
|
443
|
+
* returned `ToolCallDecision`:
|
|
410
444
|
*
|
|
411
|
-
*
|
|
412
|
-
* `
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
445
|
+
* - `void` (or `{ action: "allow" }` with no `input`) — run the
|
|
446
|
+
* original `execute` with the original input.
|
|
447
|
+
* - `{ action: "allow", input }` — run the original `execute` with
|
|
448
|
+
* the substituted input.
|
|
449
|
+
* - `{ action: "block", reason }` — skip `execute`; the model sees
|
|
450
|
+
* `reason` as the tool's output.
|
|
451
|
+
* - `{ action: "substitute", output }` — skip `execute`; the model
|
|
452
|
+
* sees `output` as the tool's output.
|
|
416
453
|
*
|
|
417
|
-
* Only fires for server-side tools (tools with `execute`).
|
|
418
|
-
*
|
|
454
|
+
* Only fires for server-side tools (tools with `execute`). Client
|
|
455
|
+
* tools are handled on the client — Think can't intercept them.
|
|
419
456
|
*
|
|
420
|
-
*
|
|
457
|
+
* `afterToolCall` always fires after this hook (or after the original
|
|
458
|
+
* `execute` when `allow`). For `block`/`substitute`, the substituted
|
|
459
|
+
* value flows through `afterToolCall` as `success: true, output: ...`.
|
|
421
460
|
*
|
|
422
461
|
* @example Log tool calls
|
|
423
462
|
* ```typescript
|
|
424
463
|
* beforeToolCall(ctx: ToolCallContext) {
|
|
425
|
-
* console.log(`Tool called: ${ctx.toolName}`, ctx.
|
|
464
|
+
* console.log(`Tool called: ${ctx.toolName}`, ctx.input);
|
|
465
|
+
* }
|
|
466
|
+
* ```
|
|
467
|
+
*
|
|
468
|
+
* @example Block a tool the model shouldn't be calling here
|
|
469
|
+
* ```typescript
|
|
470
|
+
* beforeToolCall(ctx: ToolCallContext): ToolCallDecision | void {
|
|
471
|
+
* if (ctx.toolName === "delete" && this.isReadOnlyMode) {
|
|
472
|
+
* return { action: "block", reason: "delete is disabled in read-only mode" };
|
|
473
|
+
* }
|
|
474
|
+
* }
|
|
475
|
+
* ```
|
|
476
|
+
*
|
|
477
|
+
* @example Substitute a cached result
|
|
478
|
+
* ```typescript
|
|
479
|
+
* async beforeToolCall(ctx: ToolCallContext): Promise<ToolCallDecision | void> {
|
|
480
|
+
* if (ctx.toolName === "weather") {
|
|
481
|
+
* const cached = await this.cache.get(JSON.stringify(ctx.input));
|
|
482
|
+
* if (cached) return { action: "substitute", output: cached };
|
|
483
|
+
* }
|
|
426
484
|
* }
|
|
427
485
|
* ```
|
|
428
486
|
*/
|
|
@@ -430,10 +488,27 @@ declare class Think<
|
|
|
430
488
|
_ctx: ToolCallContext
|
|
431
489
|
): ToolCallDecision | void | Promise<ToolCallDecision | void>;
|
|
432
490
|
/**
|
|
433
|
-
* Called after a tool
|
|
434
|
-
*
|
|
491
|
+
* Called **after** a tool's outcome is known — for real executions, for
|
|
492
|
+
* `block` (carries the `reason` as `output`), and for `substitute`
|
|
493
|
+
* (carries the substituted `output`). Backed by the AI SDK's
|
|
494
|
+
* `experimental_onToolCallFinish`, so `durationMs` and the discriminated
|
|
495
|
+
* `success`/`output`/`error` outcome reflect what the model actually
|
|
496
|
+
* sees: a thrown error from the original `execute` becomes
|
|
497
|
+
* `success: false, error: ...`; everything else (including blocked /
|
|
498
|
+
* substituted calls) is `success: true, output: ...`.
|
|
435
499
|
*
|
|
436
500
|
* Override for logging, metrics, or result inspection.
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
* ```typescript
|
|
504
|
+
* afterToolCall(ctx: ToolCallResultContext) {
|
|
505
|
+
* if (ctx.success) {
|
|
506
|
+
* console.log(`${ctx.toolName} ok in ${ctx.durationMs}ms`);
|
|
507
|
+
* } else {
|
|
508
|
+
* console.error(`${ctx.toolName} failed:`, ctx.error);
|
|
509
|
+
* }
|
|
510
|
+
* }
|
|
511
|
+
* ```
|
|
437
512
|
*/
|
|
438
513
|
afterToolCall(_ctx: ToolCallResultContext): void | Promise<void>;
|
|
439
514
|
/**
|
|
@@ -457,7 +532,7 @@ declare class Think<
|
|
|
457
532
|
*
|
|
458
533
|
* Override for logging, chaining, analytics, usage tracking.
|
|
459
534
|
*/
|
|
460
|
-
onChatResponse(_result: ChatResponseResult): void | Promise<void>;
|
|
535
|
+
onChatResponse(_result: ChatResponseResult$1): void | Promise<void>;
|
|
461
536
|
/**
|
|
462
537
|
* Handle an error that occurred during a chat turn.
|
|
463
538
|
* Override to customize error handling (e.g. logging, metrics).
|
|
@@ -484,6 +559,58 @@ declare class Think<
|
|
|
484
559
|
* Extensions that don't subscribe to beforeTurn are skipped.
|
|
485
560
|
*/
|
|
486
561
|
private _pipelineExtensionBeforeTurn;
|
|
562
|
+
/**
|
|
563
|
+
* Dispatch an observation hook to all extensions that subscribe to it.
|
|
564
|
+
*
|
|
565
|
+
* Used by `_pipelineExtensionToolCallStart`, `_pipelineExtensionToolCallFinish`,
|
|
566
|
+
* `_pipelineExtensionStepFinish`, and `_pipelineExtensionChunk`. Unlike
|
|
567
|
+
* `beforeTurn`, these hooks are observation-only — extensions can't
|
|
568
|
+
* influence the turn — so we ignore return values, log errors, and
|
|
569
|
+
* apply a per-extension timeout.
|
|
570
|
+
*
|
|
571
|
+
* `onChunk` is high-frequency (per token) — extensions that subscribe
|
|
572
|
+
* to it pay an RPC cost per chunk and should be used sparingly.
|
|
573
|
+
*/
|
|
574
|
+
private _dispatchExtensionObservation;
|
|
575
|
+
/**
|
|
576
|
+
* Wrap each tool's `execute` function so the agent's `beforeToolCall`
|
|
577
|
+
* hook is consulted before the tool runs. The hook can return a
|
|
578
|
+
* `ToolCallDecision` to:
|
|
579
|
+
*
|
|
580
|
+
* - `allow` (default if `void` is returned) — run the original
|
|
581
|
+
* `execute`, optionally with a substituted `input`.
|
|
582
|
+
* - `block` — skip `execute` and return `reason` (or a default string)
|
|
583
|
+
* as the tool result. The model sees this as the tool's output.
|
|
584
|
+
* - `substitute` — skip `execute` and return `output` directly. The
|
|
585
|
+
* model sees this as the tool's output.
|
|
586
|
+
*
|
|
587
|
+
* The wrapped `execute` also dispatches the `beforeToolCall`
|
|
588
|
+
* observation snapshot to subscribed extensions. `afterToolCall` is
|
|
589
|
+
* still wired through the AI SDK's `experimental_onToolCallFinish`
|
|
590
|
+
* callback so we get accurate `durationMs` and proper success/error
|
|
591
|
+
* discrimination — `block` and `substitute` outcomes show up as
|
|
592
|
+
* `success: true` with the substituted output; uncaught throws from
|
|
593
|
+
* the original `execute` show up as `success: false` with the error.
|
|
594
|
+
*
|
|
595
|
+
* Tools without an `execute` (output-schema-only tools, client tools
|
|
596
|
+
* routed via `needsApproval`) are left untouched.
|
|
597
|
+
*
|
|
598
|
+
* **Streaming tools (AsyncIterable):** the AI SDK supports tools whose
|
|
599
|
+
* `execute` returns `AsyncIterable<output>` to emit preliminary
|
|
600
|
+
* results before a final value. This works whether the iterator is
|
|
601
|
+
* returned directly (sync function, `async function*`) or wrapped in
|
|
602
|
+
* a Promise (`async function execute(...) { return makeIter(); }`).
|
|
603
|
+
* Because the wrapper must `await beforeToolCall` first, preliminary
|
|
604
|
+
* chunks are collapsed — only the *final* yielded value reaches the
|
|
605
|
+
* model. If you need true preliminary streaming, override
|
|
606
|
+
* `getTools()` to provide such tools and avoid using `beforeToolCall`
|
|
607
|
+
* for them (or accept the collapse).
|
|
608
|
+
*/
|
|
609
|
+
private _wrapToolsWithDecision;
|
|
610
|
+
private _pipelineExtensionToolCallStart;
|
|
611
|
+
private _pipelineExtensionToolCallFinish;
|
|
612
|
+
private _pipelineExtensionStepFinish;
|
|
613
|
+
private _pipelineExtensionChunk;
|
|
487
614
|
_hostReadFile(path: string): Promise<string | null>;
|
|
488
615
|
_hostWriteFile(path: string, content: string): Promise<void>;
|
|
489
616
|
_hostDeleteFile(path: string): Promise<boolean>;
|
|
@@ -559,7 +686,7 @@ declare class Think<
|
|
|
559
686
|
messages:
|
|
560
687
|
| UIMessage[]
|
|
561
688
|
| ((currentMessages: UIMessage[]) => UIMessage[] | Promise<UIMessage[]>)
|
|
562
|
-
): Promise<SaveMessagesResult>;
|
|
689
|
+
): Promise<SaveMessagesResult$1>;
|
|
563
690
|
/**
|
|
564
691
|
* Run a new LLM call following the last assistant message.
|
|
565
692
|
*
|
|
@@ -576,7 +703,7 @@ declare class Think<
|
|
|
576
703
|
*/
|
|
577
704
|
protected continueLastTurn(
|
|
578
705
|
body?: Record<string, unknown>
|
|
579
|
-
): Promise<SaveMessagesResult>;
|
|
706
|
+
): Promise<SaveMessagesResult$1>;
|
|
580
707
|
private _setupProtocolHandlers;
|
|
581
708
|
private _handleProtocolEvent;
|
|
582
709
|
private _handleStreamResumeRequest;
|
|
@@ -609,8 +736,8 @@ declare class Think<
|
|
|
609
736
|
ctx: FiberRecoveryContext$1
|
|
610
737
|
): Promise<boolean>;
|
|
611
738
|
protected onChatRecovery(
|
|
612
|
-
_ctx: ChatRecoveryContext
|
|
613
|
-
): Promise<ChatRecoveryOptions>;
|
|
739
|
+
_ctx: ChatRecoveryContext$1
|
|
740
|
+
): Promise<ChatRecoveryOptions$1>;
|
|
614
741
|
_chatRecoveryContinue(data?: { targetAssistantId?: string }): Promise<void>;
|
|
615
742
|
private _getPartialStreamText;
|
|
616
743
|
private _normalizeMessageConcurrency;
|
|
@@ -633,19 +760,22 @@ declare class Think<
|
|
|
633
760
|
//#endregion
|
|
634
761
|
export {
|
|
635
762
|
ChatOptions,
|
|
636
|
-
ChatRecoveryContext,
|
|
637
|
-
ChatRecoveryOptions,
|
|
638
|
-
ChatResponseResult,
|
|
763
|
+
type ChatRecoveryContext,
|
|
764
|
+
type ChatRecoveryOptions,
|
|
765
|
+
type ChatResponseResult,
|
|
639
766
|
ChunkContext,
|
|
767
|
+
ChunkPart,
|
|
640
768
|
ExtensionConfig,
|
|
641
769
|
type FiberContext,
|
|
642
770
|
type FiberRecoveryContext,
|
|
643
|
-
MessageConcurrency,
|
|
644
|
-
SaveMessagesResult,
|
|
771
|
+
type MessageConcurrency,
|
|
772
|
+
type SaveMessagesResult,
|
|
645
773
|
Session,
|
|
646
774
|
StepContext,
|
|
775
|
+
type StepResult,
|
|
647
776
|
StreamCallback,
|
|
648
777
|
StreamableResult,
|
|
778
|
+
type TextStreamPart,
|
|
649
779
|
Think,
|
|
650
780
|
ToolCallContext,
|
|
651
781
|
ToolCallDecision,
|
|
@@ -653,6 +783,8 @@ export {
|
|
|
653
783
|
TurnConfig,
|
|
654
784
|
TurnContext,
|
|
655
785
|
TurnInput,
|
|
786
|
+
type TypedToolCall,
|
|
787
|
+
type TypedToolResult,
|
|
656
788
|
Workspace
|
|
657
789
|
};
|
|
658
790
|
//# sourceMappingURL=think.d.ts.map
|