@cloudflare/think 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -9
- package/dist/{classPrivateFieldGet2-C7fYS0JL.js → classPrivateFieldGet2-Bvcn7kEw.js} +5 -5
- package/dist/extensions/index.js +2 -2
- package/dist/{manager-CR9ZdLiu.js → manager-DDC3Gb0V.js} +3 -3
- package/dist/{manager-CR9ZdLiu.js.map → manager-DDC3Gb0V.js.map} +1 -1
- package/dist/think.d.ts +155 -103
- package/dist/think.js +162 -113
- 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/dist/tools/workspace.d.ts +11 -2
- package/dist/tools/workspace.js +5 -1
- package/dist/tools/workspace.js.map +1 -1
- package/package.json +2 -2
package/dist/think.d.ts
CHANGED
|
@@ -2,9 +2,14 @@ import {
|
|
|
2
2
|
i as ExtensionManifest,
|
|
3
3
|
t as ExtensionManager
|
|
4
4
|
} from "./manager-DCbSiSWT.js";
|
|
5
|
+
import { WorkspaceLike } from "./tools/workspace.js";
|
|
5
6
|
import {
|
|
6
7
|
LanguageModel,
|
|
7
8
|
ModelMessage,
|
|
9
|
+
PrepareStepFunction,
|
|
10
|
+
PrepareStepFunction as PrepareStepFunction$1,
|
|
11
|
+
PrepareStepResult,
|
|
12
|
+
PrepareStepResult as PrepareStepResult$1,
|
|
8
13
|
StepResult,
|
|
9
14
|
StreamTextOnChunkCallback,
|
|
10
15
|
StreamTextOnStepFinishCallback,
|
|
@@ -22,12 +27,25 @@ import {
|
|
|
22
27
|
FiberRecoveryContext,
|
|
23
28
|
FiberRecoveryContext as FiberRecoveryContext$1
|
|
24
29
|
} from "agents";
|
|
25
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
ChatRecoveryContext,
|
|
32
|
+
ChatRecoveryContext as ChatRecoveryContext$1,
|
|
33
|
+
ChatRecoveryOptions,
|
|
34
|
+
ChatRecoveryOptions as ChatRecoveryOptions$1,
|
|
35
|
+
ChatResponseResult,
|
|
36
|
+
ChatResponseResult as ChatResponseResult$1,
|
|
37
|
+
ClientToolSchema,
|
|
38
|
+
MessageConcurrency,
|
|
39
|
+
MessageConcurrency as MessageConcurrency$1,
|
|
40
|
+
ResumableStream,
|
|
41
|
+
SaveMessagesResult,
|
|
42
|
+
SaveMessagesResult as SaveMessagesResult$1
|
|
43
|
+
} from "agents/chat";
|
|
26
44
|
import {
|
|
27
45
|
Session,
|
|
28
46
|
Session as Session$1
|
|
29
47
|
} from "agents/experimental/memory/session";
|
|
30
|
-
import { Workspace
|
|
48
|
+
import { Workspace } from "@cloudflare/shell";
|
|
31
49
|
|
|
32
50
|
//#region src/think.d.ts
|
|
33
51
|
/**
|
|
@@ -55,40 +73,6 @@ interface ChatOptions {
|
|
|
55
73
|
signal?: AbortSignal;
|
|
56
74
|
tools?: ToolSet;
|
|
57
75
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Result returned by `saveMessages()` and `continueLastTurn()`.
|
|
60
|
-
*/
|
|
61
|
-
type SaveMessagesResult = {
|
|
62
|
-
requestId: string;
|
|
63
|
-
status: "completed" | "skipped";
|
|
64
|
-
};
|
|
65
|
-
/**
|
|
66
|
-
* Context passed to `onChatRecovery` when an interrupted chat stream
|
|
67
|
-
* is detected after DO restart.
|
|
68
|
-
*/
|
|
69
|
-
type ChatRecoveryContext = {
|
|
70
|
-
streamId: string;
|
|
71
|
-
requestId: string;
|
|
72
|
-
partialText: string;
|
|
73
|
-
partialParts: MessagePart[];
|
|
74
|
-
recoveryData: unknown | null;
|
|
75
|
-
messages: UIMessage[];
|
|
76
|
-
lastBody?: Record<string, unknown>;
|
|
77
|
-
lastClientTools?: ClientToolSchema[];
|
|
78
|
-
/**
|
|
79
|
-
* Epoch milliseconds when the underlying fiber was started. Compare against
|
|
80
|
-
* `Date.now()` to suppress continuations for turns that have been orphaned
|
|
81
|
-
* too long to safely replay.
|
|
82
|
-
*/
|
|
83
|
-
createdAt: number;
|
|
84
|
-
};
|
|
85
|
-
/**
|
|
86
|
-
* Options returned from `onChatRecovery` to control recovery behavior.
|
|
87
|
-
*/
|
|
88
|
-
type ChatRecoveryOptions = {
|
|
89
|
-
persist?: boolean;
|
|
90
|
-
continue?: boolean;
|
|
91
|
-
};
|
|
92
76
|
/**
|
|
93
77
|
* A chat turn request. Built automatically by each entry path
|
|
94
78
|
* (WebSocket, chat(), saveMessages, auto-continuation) and passed
|
|
@@ -144,7 +128,43 @@ interface TurnConfig {
|
|
|
144
128
|
maxSteps?: number;
|
|
145
129
|
/** Provider-specific options (AI SDK providerOptions). */
|
|
146
130
|
providerOptions?: Record<string, unknown>;
|
|
131
|
+
/**
|
|
132
|
+
* Optional structured-output specification (AI SDK `output`).
|
|
133
|
+
* Forwarded to `streamText` so the model's final response is parsed
|
|
134
|
+
* against the supplied schema. Use the AI SDK's `Output.object({ schema })`
|
|
135
|
+
* / `Output.text()` helpers. Combine with `activeTools: []` on the
|
|
136
|
+
* terminal turn if your provider strips tools when structured output
|
|
137
|
+
* is active (e.g. workers-ai-provider).
|
|
138
|
+
*/
|
|
139
|
+
output?: Parameters<typeof streamText>[0]["output"];
|
|
147
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Context passed to the `beforeStep` hook before each AI SDK step in
|
|
143
|
+
* the agentic loop. Backed by the AI SDK's `PrepareStepFunction<TOOLS>`
|
|
144
|
+
* parameter — exposes the previous `steps`, the zero-based `stepNumber`,
|
|
145
|
+
* the currently selected `model`, the `messages` about to be sent, and
|
|
146
|
+
* `experimental_context`.
|
|
147
|
+
*
|
|
148
|
+
* Pass an explicit `TOOLS` generic for typed previous tool calls / results.
|
|
149
|
+
*
|
|
150
|
+
* Limitations (AI SDK boundary, not Think):
|
|
151
|
+
* - No `abortSignal` is exposed in the context. If you do remote work
|
|
152
|
+
* inside `beforeStep`, it cannot be cancelled by turn-level abort.
|
|
153
|
+
* - `experimental_context` is typed `unknown`; users must narrow it.
|
|
154
|
+
* - `output` cannot be overridden per-step — set it at the turn level
|
|
155
|
+
* via `TurnConfig.output` (returned from `beforeTurn`).
|
|
156
|
+
*/
|
|
157
|
+
type PrepareStepContext<TOOLS extends ToolSet = ToolSet> = Parameters<
|
|
158
|
+
PrepareStepFunction$1<TOOLS>
|
|
159
|
+
>[0];
|
|
160
|
+
/**
|
|
161
|
+
* Configuration returned by `beforeStep` to override defaults for the
|
|
162
|
+
* current AI SDK step. This is the AI SDK's `PrepareStepResult<TOOLS>` —
|
|
163
|
+
* return only the fields you want to override (`model`, `toolChoice`,
|
|
164
|
+
* `activeTools`, `system`, `messages`, `experimental_context`,
|
|
165
|
+
* `providerOptions`).
|
|
166
|
+
*/
|
|
167
|
+
type StepConfig<TOOLS extends ToolSet = ToolSet> = PrepareStepResult$1<TOOLS>;
|
|
148
168
|
/**
|
|
149
169
|
* Context passed to the `beforeToolCall` hook **before** the tool's
|
|
150
170
|
* `execute` function runs.
|
|
@@ -290,41 +310,6 @@ interface ExtensionConfig {
|
|
|
290
310
|
/** JavaScript source code defining the extension's tools. */
|
|
291
311
|
source: string;
|
|
292
312
|
}
|
|
293
|
-
/**
|
|
294
|
-
* Controls how overlapping user submit requests behave while another
|
|
295
|
-
* chat turn is already active or queued.
|
|
296
|
-
*
|
|
297
|
-
* - `"queue"` (default) — queue every submit and process them in order.
|
|
298
|
-
* - `"latest"` — keep only the latest overlapping submit; superseded submits
|
|
299
|
-
* still persist their user messages, but do not start their own model turn.
|
|
300
|
-
* - `"merge"` — like latest, but all overlapping user messages remain in
|
|
301
|
-
* the conversation history. The model sees them all in one turn.
|
|
302
|
-
* - `"drop"` — ignore overlapping submits entirely (messages not persisted).
|
|
303
|
-
* - `{ strategy: "debounce" }` — trailing-edge latest with a quiet window.
|
|
304
|
-
*
|
|
305
|
-
* Only applies to `submit-message` requests. Regenerations, tool
|
|
306
|
-
* continuations, approvals, clears, `saveMessages`, and `continueLastTurn`
|
|
307
|
-
* keep their existing serialized behavior.
|
|
308
|
-
*/
|
|
309
|
-
type MessageConcurrency =
|
|
310
|
-
| "queue"
|
|
311
|
-
| "latest"
|
|
312
|
-
| "merge"
|
|
313
|
-
| "drop"
|
|
314
|
-
| {
|
|
315
|
-
strategy: "debounce";
|
|
316
|
-
debounceMs?: number;
|
|
317
|
-
};
|
|
318
|
-
/**
|
|
319
|
-
* Result passed to `onChatResponse` after a chat turn completes.
|
|
320
|
-
*/
|
|
321
|
-
type ChatResponseResult = {
|
|
322
|
-
message: UIMessage;
|
|
323
|
-
requestId: string;
|
|
324
|
-
continuation: boolean;
|
|
325
|
-
status: "completed" | "error" | "aborted";
|
|
326
|
-
error?: string;
|
|
327
|
-
};
|
|
328
313
|
/**
|
|
329
314
|
* An opinionated chat agent base class.
|
|
330
315
|
*
|
|
@@ -332,9 +317,11 @@ type ChatResponseResult = {
|
|
|
332
317
|
*/
|
|
333
318
|
declare class Think<
|
|
334
319
|
Env extends Cloudflare.Env = Cloudflare.Env,
|
|
335
|
-
|
|
336
|
-
|
|
320
|
+
State = unknown,
|
|
321
|
+
Props extends Record<string, unknown> = Record<string, unknown>
|
|
322
|
+
> extends Agent<Env, State, Props> {
|
|
337
323
|
#private;
|
|
324
|
+
private static readonly CONFIG_KEYS;
|
|
338
325
|
/**
|
|
339
326
|
* Wait for MCP server connections to be ready before the inference
|
|
340
327
|
* loop. MCP tools are auto-merged into the tool set.
|
|
@@ -353,7 +340,7 @@ declare class Think<
|
|
|
353
340
|
*
|
|
354
341
|
* @default "queue"
|
|
355
342
|
*/
|
|
356
|
-
messageConcurrency: MessageConcurrency;
|
|
343
|
+
messageConcurrency: MessageConcurrency$1;
|
|
357
344
|
/**
|
|
358
345
|
* When true, chat turns are wrapped in `runFiber` for durable execution.
|
|
359
346
|
* Enables `onChatRecovery` hook and `this.stash()` during streaming.
|
|
@@ -373,19 +360,27 @@ declare class Think<
|
|
|
373
360
|
*/
|
|
374
361
|
extensionManager?: ExtensionManager;
|
|
375
362
|
/**
|
|
376
|
-
* Workspace filesystem
|
|
377
|
-
*
|
|
363
|
+
* Workspace filesystem available in `getTools()` and lifecycle hooks.
|
|
364
|
+
* Defaults to a full `Workspace` backed by the DO's SQLite storage.
|
|
365
|
+
*
|
|
366
|
+
* Typed as `WorkspaceLike` rather than `Workspace` so subclasses can
|
|
367
|
+
* replace it with anything that satisfies the interface — e.g. a proxy
|
|
368
|
+
* that forwards to a shared workspace owned by a parent DO. Override as
|
|
369
|
+
* a class field to skip the default init entirely:
|
|
378
370
|
*
|
|
379
|
-
* Override to add R2 spillover for large files:
|
|
380
371
|
* ```typescript
|
|
372
|
+
* // Default init with R2 spillover for large files.
|
|
381
373
|
* override workspace = new Workspace({
|
|
382
374
|
* sql: this.ctx.storage.sql,
|
|
383
375
|
* r2: this.env.R2,
|
|
384
376
|
* name: () => this.name
|
|
385
377
|
* });
|
|
378
|
+
*
|
|
379
|
+
* // Or a custom WorkspaceLike — e.g. a parent-owned shared workspace.
|
|
380
|
+
* override workspace: WorkspaceLike = new SharedWorkspace(this);
|
|
386
381
|
* ```
|
|
387
382
|
*/
|
|
388
|
-
workspace:
|
|
383
|
+
workspace: WorkspaceLike;
|
|
389
384
|
constructor(ctx: DurableObjectState, env: Env);
|
|
390
385
|
/**
|
|
391
386
|
* Conversation history. Computed from the active session.
|
|
@@ -394,7 +389,7 @@ declare class Think<
|
|
|
394
389
|
get messages(): UIMessage[];
|
|
395
390
|
private _aborts;
|
|
396
391
|
private _turnQueue;
|
|
397
|
-
|
|
392
|
+
protected _resumableStream: ResumableStream;
|
|
398
393
|
private _pendingResumeConnections;
|
|
399
394
|
private _lastClientTools;
|
|
400
395
|
private _lastBody;
|
|
@@ -403,25 +398,38 @@ declare class Think<
|
|
|
403
398
|
private _insideResponseHook;
|
|
404
399
|
private _insideInferenceLoop;
|
|
405
400
|
private _pendingInteractionPromise;
|
|
406
|
-
private
|
|
407
|
-
private _latestOverlappingSubmitSequence;
|
|
408
|
-
private _activeDebounceTimer;
|
|
409
|
-
private _activeDebounceResolve;
|
|
401
|
+
private _submitConcurrency;
|
|
410
402
|
private static MESSAGE_DEBOUNCE_MS;
|
|
411
403
|
/**
|
|
412
|
-
* Persist
|
|
413
|
-
* Stored in
|
|
404
|
+
* Persist an arbitrary JSON-serializable configuration object for this
|
|
405
|
+
* agent instance. Stored in the Think-private `think_config` table —
|
|
406
|
+
* survives
|
|
407
|
+
* restarts and hibernation. Pass the config shape as a method generic
|
|
408
|
+
* for typed call sites:
|
|
409
|
+
*
|
|
410
|
+
* ```ts
|
|
411
|
+
* this.configure<MyConfig>({ modelTier: "fast" });
|
|
412
|
+
* ```
|
|
413
|
+
*
|
|
414
|
+
* Prefer `state` / `setState` from `Agent` when you want the value
|
|
415
|
+
* broadcast to connected clients. Use `configure` for private
|
|
416
|
+
* per-instance config that should stay server-side.
|
|
414
417
|
*/
|
|
415
|
-
configure(config:
|
|
418
|
+
configure<T = Record<string, unknown>>(config: T): void;
|
|
416
419
|
/**
|
|
417
420
|
* Read the persisted configuration, or null if never configured.
|
|
421
|
+
* Pass the config shape as a method generic for a typed result:
|
|
422
|
+
*
|
|
423
|
+
* ```ts
|
|
424
|
+
* const cfg = this.getConfig<MyConfig>();
|
|
425
|
+
* ```
|
|
418
426
|
*/
|
|
419
|
-
getConfig():
|
|
427
|
+
getConfig<T = Record<string, unknown>>(): T | null;
|
|
428
|
+
protected _migrateLegacyConfigToThinkTable(): void;
|
|
420
429
|
private _ensureConfigTable;
|
|
421
430
|
private _configSet;
|
|
422
431
|
private _configGet;
|
|
423
432
|
private _configDelete;
|
|
424
|
-
private _sessionId;
|
|
425
433
|
/**
|
|
426
434
|
* Return the language model to use for inference.
|
|
427
435
|
* Must be overridden by subclasses.
|
|
@@ -478,6 +486,42 @@ declare class Think<
|
|
|
478
486
|
* ```
|
|
479
487
|
*/
|
|
480
488
|
beforeTurn(_ctx: TurnContext): TurnConfig | void | Promise<TurnConfig | void>;
|
|
489
|
+
/**
|
|
490
|
+
* Called before each AI SDK step in the agentic loop. Backed by
|
|
491
|
+
* `streamText({ prepareStep })`.
|
|
492
|
+
*
|
|
493
|
+
* Return `void` to accept the current step defaults, or return a
|
|
494
|
+
* `StepConfig` to override the model, tool choice, active tools,
|
|
495
|
+
* system prompt, messages, experimental context, or provider options
|
|
496
|
+
* for this step. Use `beforeTurn` for turn-wide assembly and
|
|
497
|
+
* `beforeStep` when the decision depends on the step number or
|
|
498
|
+
* previous step results.
|
|
499
|
+
*
|
|
500
|
+
* @example Force search on the first step
|
|
501
|
+
* ```typescript
|
|
502
|
+
* beforeStep(ctx: PrepareStepContext) {
|
|
503
|
+
* if (ctx.stepNumber === 0) {
|
|
504
|
+
* return {
|
|
505
|
+
* activeTools: ["search"],
|
|
506
|
+
* toolChoice: { type: "tool", toolName: "search" }
|
|
507
|
+
* };
|
|
508
|
+
* }
|
|
509
|
+
* }
|
|
510
|
+
* ```
|
|
511
|
+
*
|
|
512
|
+
* @example Switch to a cheaper model after tool results land
|
|
513
|
+
* ```typescript
|
|
514
|
+
* beforeStep(ctx: PrepareStepContext) {
|
|
515
|
+
* // assumes a `fastSummaryModel` field on your Think subclass
|
|
516
|
+
* if (ctx.steps.some((s) => s.toolResults.length > 0)) {
|
|
517
|
+
* return { model: this.fastSummaryModel };
|
|
518
|
+
* }
|
|
519
|
+
* }
|
|
520
|
+
* ```
|
|
521
|
+
*/
|
|
522
|
+
beforeStep(
|
|
523
|
+
_ctx: PrepareStepContext
|
|
524
|
+
): StepConfig | void | Promise<StepConfig | void>;
|
|
481
525
|
/**
|
|
482
526
|
* Called **before** the tool's `execute` function runs. Think wraps
|
|
483
527
|
* every tool's `execute` so it can consult this hook and act on the
|
|
@@ -573,7 +617,7 @@ declare class Think<
|
|
|
573
617
|
*
|
|
574
618
|
* Override for logging, chaining, analytics, usage tracking.
|
|
575
619
|
*/
|
|
576
|
-
onChatResponse(_result: ChatResponseResult): void | Promise<void>;
|
|
620
|
+
onChatResponse(_result: ChatResponseResult$1): void | Promise<void>;
|
|
577
621
|
/**
|
|
578
622
|
* Handle an error that occurred during a chat turn.
|
|
579
623
|
* Override to customize error handling (e.g. logging, metrics).
|
|
@@ -727,7 +771,7 @@ declare class Think<
|
|
|
727
771
|
messages:
|
|
728
772
|
| UIMessage[]
|
|
729
773
|
| ((currentMessages: UIMessage[]) => UIMessage[] | Promise<UIMessage[]>)
|
|
730
|
-
): Promise<SaveMessagesResult>;
|
|
774
|
+
): Promise<SaveMessagesResult$1>;
|
|
731
775
|
/**
|
|
732
776
|
* Run a new LLM call following the last assistant message.
|
|
733
777
|
*
|
|
@@ -744,7 +788,7 @@ declare class Think<
|
|
|
744
788
|
*/
|
|
745
789
|
protected continueLastTurn(
|
|
746
790
|
body?: Record<string, unknown>
|
|
747
|
-
): Promise<SaveMessagesResult>;
|
|
791
|
+
): Promise<SaveMessagesResult$1>;
|
|
748
792
|
private _setupProtocolHandlers;
|
|
749
793
|
private _handleProtocolEvent;
|
|
750
794
|
private _handleStreamResumeRequest;
|
|
@@ -762,6 +806,13 @@ declare class Think<
|
|
|
762
806
|
private _handleClear;
|
|
763
807
|
private _streamResult;
|
|
764
808
|
private _persistAssistantMessage;
|
|
809
|
+
/**
|
|
810
|
+
* Persist an incoming message after reconciliation. For assistant
|
|
811
|
+
* messages, also resolve their ID against any server-side row that
|
|
812
|
+
* already owns the same `toolCallId` so we update the existing row
|
|
813
|
+
* instead of inserting an orphan duplicate.
|
|
814
|
+
*/
|
|
815
|
+
private _persistIncomingMessage;
|
|
765
816
|
private _persistClientTools;
|
|
766
817
|
private _restoreClientTools;
|
|
767
818
|
private _persistBody;
|
|
@@ -777,15 +828,11 @@ declare class Think<
|
|
|
777
828
|
ctx: FiberRecoveryContext$1
|
|
778
829
|
): Promise<boolean>;
|
|
779
830
|
protected onChatRecovery(
|
|
780
|
-
_ctx: ChatRecoveryContext
|
|
781
|
-
): Promise<ChatRecoveryOptions>;
|
|
831
|
+
_ctx: ChatRecoveryContext$1
|
|
832
|
+
): Promise<ChatRecoveryOptions$1>;
|
|
782
833
|
_chatRecoveryContinue(data?: { targetAssistantId?: string }): Promise<void>;
|
|
783
834
|
private _getPartialStreamText;
|
|
784
|
-
private _normalizeMessageConcurrency;
|
|
785
835
|
private _getSubmitConcurrencyDecision;
|
|
786
|
-
private _isSupersededSubmit;
|
|
787
|
-
private _waitForTimestamp;
|
|
788
|
-
private _cancelActiveDebounce;
|
|
789
836
|
private _completeSkippedRequest;
|
|
790
837
|
private _rollbackDroppedSubmit;
|
|
791
838
|
private _scheduleAutoContinuation;
|
|
@@ -801,17 +848,21 @@ declare class Think<
|
|
|
801
848
|
//#endregion
|
|
802
849
|
export {
|
|
803
850
|
ChatOptions,
|
|
804
|
-
ChatRecoveryContext,
|
|
805
|
-
ChatRecoveryOptions,
|
|
806
|
-
ChatResponseResult,
|
|
851
|
+
type ChatRecoveryContext,
|
|
852
|
+
type ChatRecoveryOptions,
|
|
853
|
+
type ChatResponseResult,
|
|
807
854
|
ChunkContext,
|
|
808
855
|
ChunkPart,
|
|
809
856
|
ExtensionConfig,
|
|
810
857
|
type FiberContext,
|
|
811
858
|
type FiberRecoveryContext,
|
|
812
|
-
MessageConcurrency,
|
|
813
|
-
|
|
859
|
+
type MessageConcurrency,
|
|
860
|
+
PrepareStepContext,
|
|
861
|
+
type PrepareStepFunction,
|
|
862
|
+
type PrepareStepResult,
|
|
863
|
+
type SaveMessagesResult,
|
|
814
864
|
Session,
|
|
865
|
+
StepConfig,
|
|
815
866
|
StepContext,
|
|
816
867
|
type StepResult,
|
|
817
868
|
StreamCallback,
|
|
@@ -826,6 +877,7 @@ export {
|
|
|
826
877
|
TurnInput,
|
|
827
878
|
type TypedToolCall,
|
|
828
879
|
type TypedToolResult,
|
|
829
|
-
Workspace
|
|
880
|
+
Workspace,
|
|
881
|
+
type WorkspaceLike
|
|
830
882
|
};
|
|
831
883
|
//# sourceMappingURL=think.d.ts.map
|