agents 0.8.7 → 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/dist/chat/index.d.ts +603 -0
- package/dist/chat/index.js +1285 -0
- package/dist/chat/index.js.map +1 -0
- package/dist/{client-BwgM3cRz.js → client-QBjFV5de.js} +161 -49
- package/dist/client-QBjFV5de.js.map +1 -0
- package/dist/client.d.ts +2 -2
- package/dist/{compaction-helpers-BFTBIzpK.js → compaction-helpers-BPE1_ziA.js} +1 -1
- package/dist/{compaction-helpers-BFTBIzpK.js.map → compaction-helpers-BPE1_ziA.js.map} +1 -1
- package/dist/{compaction-helpers-DkJreaDR.d.ts → compaction-helpers-CHNQeyRm.d.ts} +1 -1
- package/dist/{do-oauth-client-provider-C2jurFjW.d.ts → do-oauth-client-provider-31gqR33H.d.ts} +1 -1
- package/dist/{email-DwPlM0bQ.d.ts → email-Cql45SKP.d.ts} +1 -1
- package/dist/email.d.ts +2 -2
- package/dist/experimental/memory/session/index.d.ts +298 -73
- package/dist/experimental/memory/session/index.js +754 -66
- package/dist/experimental/memory/session/index.js.map +1 -1
- package/dist/experimental/memory/utils/index.d.ts +1 -1
- package/dist/experimental/memory/utils/index.js +1 -1
- package/dist/{index-C-6EMK-E.d.ts → index-BPkkIqMn.d.ts} +209 -76
- package/dist/{index-Ua2Nfvbm.d.ts → index-DDSX-g7W.d.ts} +11 -1
- package/dist/index.d.ts +30 -26
- package/dist/index.js +2 -3049
- package/dist/{internal_context-DT8RxmAN.d.ts → internal_context-DuQZFvWI.d.ts} +1 -1
- package/dist/internal_context.d.ts +1 -1
- package/dist/mcp/client.d.ts +2 -2
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +2 -2
- package/dist/observability/index.d.ts +1 -1
- package/dist/react.d.ts +3 -1
- package/dist/react.js +3 -0
- package/dist/react.js.map +1 -1
- package/dist/{retries-DXMQGhG3.d.ts → retries-B_CN5KM9.d.ts} +1 -1
- package/dist/retries.d.ts +1 -1
- package/dist/{serializable-8Jt1B04R.d.ts → serializable-DGdO8CDh.d.ts} +1 -1
- package/dist/serializable.d.ts +1 -1
- package/dist/src-B8NZxxsO.js +3217 -0
- package/dist/src-B8NZxxsO.js.map +1 -0
- package/dist/{types-C-m0II8i.d.ts → types-B9A8AU7B.d.ts} +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/{workflow-types-CZNXKj_D.d.ts → workflow-types-XmOkuI7A.d.ts} +1 -1
- package/dist/workflow-types.d.ts +1 -1
- package/dist/workflows.d.ts +2 -2
- package/dist/workflows.js +1 -1
- package/package.json +20 -18
- package/dist/client-BwgM3cRz.js.map +0 -1
- package/dist/experimental/forever.d.ts +0 -64
- package/dist/experimental/forever.js +0 -338
- package/dist/experimental/forever.js.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
import { JSONSchema7, Tool, ToolSet, UIMessage } from "ai";
|
|
2
|
+
import { Connection } from "agents";
|
|
3
|
+
|
|
4
|
+
//#region src/chat/message-builder.d.ts
|
|
5
|
+
/** The parts array type from UIMessage */
|
|
6
|
+
type MessageParts = UIMessage["parts"];
|
|
7
|
+
/** A single part from the UIMessage parts array */
|
|
8
|
+
type MessagePart = MessageParts[number];
|
|
9
|
+
/**
|
|
10
|
+
* Parsed chunk data from an AI SDK stream event.
|
|
11
|
+
* This is the JSON-parsed body of a CF_AGENT_USE_CHAT_RESPONSE message,
|
|
12
|
+
* or the `data:` payload of an SSE line.
|
|
13
|
+
*/
|
|
14
|
+
type StreamChunkData = {
|
|
15
|
+
type: string;
|
|
16
|
+
id?: string;
|
|
17
|
+
delta?: string;
|
|
18
|
+
text?: string;
|
|
19
|
+
mediaType?: string;
|
|
20
|
+
url?: string;
|
|
21
|
+
sourceId?: string;
|
|
22
|
+
title?: string;
|
|
23
|
+
filename?: string;
|
|
24
|
+
toolCallId?: string;
|
|
25
|
+
toolName?: string;
|
|
26
|
+
input?: unknown;
|
|
27
|
+
inputTextDelta?: string;
|
|
28
|
+
output?: unknown;
|
|
29
|
+
state?: string;
|
|
30
|
+
errorText?: string; /** When true, the output is preliminary (may be updated by a later chunk) */
|
|
31
|
+
preliminary?: boolean; /** Approval ID for tools with needsApproval */
|
|
32
|
+
approvalId?: string;
|
|
33
|
+
providerMetadata?: Record<string, unknown>; /** Whether the tool was executed by the provider (e.g. Gemini code execution) */
|
|
34
|
+
providerExecuted?: boolean; /** Payload for data-* parts (developer-defined typed JSON) */
|
|
35
|
+
data?: unknown; /** When true, data parts are ephemeral and not persisted to message.parts */
|
|
36
|
+
transient?: boolean; /** Message ID assigned by the server at stream start */
|
|
37
|
+
messageId?: string; /** Per-message metadata attached by start/finish/message-metadata chunks */
|
|
38
|
+
messageMetadata?: unknown;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Applies a stream chunk to a mutable parts array, building up the message
|
|
43
|
+
* incrementally. Returns true if the chunk was handled, false if it was
|
|
44
|
+
* an unrecognized type (caller may handle it with additional logic).
|
|
45
|
+
*
|
|
46
|
+
* Handles all common chunk types that both server and client need:
|
|
47
|
+
* - text-start / text-delta / text-end
|
|
48
|
+
* - reasoning-start / reasoning-delta / reasoning-end
|
|
49
|
+
* - file
|
|
50
|
+
* - source-url / source-document
|
|
51
|
+
* - tool-input-start / tool-input-delta / tool-input-available / tool-input-error
|
|
52
|
+
* - tool-output-available / tool-output-error
|
|
53
|
+
* - step-start (aliased from start-step)
|
|
54
|
+
* - data-* (developer-defined typed JSON blobs)
|
|
55
|
+
*
|
|
56
|
+
* @param parts - The mutable parts array to update
|
|
57
|
+
* @param chunk - The parsed stream chunk data
|
|
58
|
+
* @returns true if handled, false if the chunk type is not recognized
|
|
59
|
+
*/
|
|
60
|
+
declare function applyChunkToParts(parts: MessagePart[], chunk: StreamChunkData): boolean;
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/chat/sanitize.d.ts
|
|
63
|
+
/** Maximum serialized message size before compaction (bytes). 1.8MB with headroom below SQLite's 2MB limit. */
|
|
64
|
+
declare const ROW_MAX_BYTES = 1800000;
|
|
65
|
+
/** Measure UTF-8 byte length of a string. */
|
|
66
|
+
declare function byteLength(s: string): number;
|
|
67
|
+
/**
|
|
68
|
+
* Sanitize a message for persistence by removing ephemeral provider-specific
|
|
69
|
+
* data that should not be stored or sent back in subsequent requests.
|
|
70
|
+
*
|
|
71
|
+
* 1. Strips OpenAI ephemeral fields (itemId, reasoningEncryptedContent)
|
|
72
|
+
* 2. Filters truly empty reasoning parts (no text, no remaining providerMetadata)
|
|
73
|
+
*/
|
|
74
|
+
declare function sanitizeMessage(message: UIMessage): UIMessage;
|
|
75
|
+
/**
|
|
76
|
+
* Enforce SQLite row size limits by compacting tool outputs and text parts
|
|
77
|
+
* when a serialized message exceeds the safety threshold (1.8MB).
|
|
78
|
+
*
|
|
79
|
+
* Compaction strategy:
|
|
80
|
+
* 1. Compact tool outputs over 1KB (replace with summary)
|
|
81
|
+
* 2. If still too big, truncate text parts from oldest to newest
|
|
82
|
+
*/
|
|
83
|
+
declare function enforceRowSizeLimit(message: UIMessage): UIMessage;
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/chat/stream-accumulator.d.ts
|
|
86
|
+
interface StreamAccumulatorOptions {
|
|
87
|
+
messageId: string;
|
|
88
|
+
continuation?: boolean;
|
|
89
|
+
existingParts?: UIMessage["parts"];
|
|
90
|
+
existingMetadata?: Record<string, unknown>;
|
|
91
|
+
}
|
|
92
|
+
type ChunkAction = {
|
|
93
|
+
type: "start";
|
|
94
|
+
messageId?: string;
|
|
95
|
+
metadata?: Record<string, unknown>;
|
|
96
|
+
} | {
|
|
97
|
+
type: "finish";
|
|
98
|
+
finishReason?: string;
|
|
99
|
+
metadata?: Record<string, unknown>;
|
|
100
|
+
} | {
|
|
101
|
+
type: "message-metadata";
|
|
102
|
+
metadata: Record<string, unknown>;
|
|
103
|
+
} | {
|
|
104
|
+
type: "tool-approval-request";
|
|
105
|
+
toolCallId: string;
|
|
106
|
+
} | {
|
|
107
|
+
type: "cross-message-tool-update";
|
|
108
|
+
updateType: "output-available" | "output-error";
|
|
109
|
+
toolCallId: string;
|
|
110
|
+
output?: unknown;
|
|
111
|
+
errorText?: string;
|
|
112
|
+
preliminary?: boolean;
|
|
113
|
+
} | {
|
|
114
|
+
type: "error";
|
|
115
|
+
error: string;
|
|
116
|
+
};
|
|
117
|
+
interface ChunkResult {
|
|
118
|
+
handled: boolean;
|
|
119
|
+
action?: ChunkAction;
|
|
120
|
+
}
|
|
121
|
+
declare class StreamAccumulator {
|
|
122
|
+
messageId: string;
|
|
123
|
+
readonly parts: UIMessage["parts"];
|
|
124
|
+
metadata?: Record<string, unknown>;
|
|
125
|
+
private _isContinuation;
|
|
126
|
+
constructor(options: StreamAccumulatorOptions);
|
|
127
|
+
applyChunk(chunk: StreamChunkData): ChunkResult;
|
|
128
|
+
/** Snapshot the current state as a UIMessage. */
|
|
129
|
+
toMessage(): UIMessage;
|
|
130
|
+
/**
|
|
131
|
+
* Merge this accumulator's message into an existing message array.
|
|
132
|
+
* Handles continuation (walk backward for last assistant), replacement
|
|
133
|
+
* (update existing by messageId), or append (new message).
|
|
134
|
+
*/
|
|
135
|
+
mergeInto(messages: UIMessage[]): UIMessage[];
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/chat/turn-queue.d.ts
|
|
139
|
+
/**
|
|
140
|
+
* TurnQueue — serial async queue with generation-based invalidation.
|
|
141
|
+
*
|
|
142
|
+
* Serializes async work via a promise chain, tracks which request is
|
|
143
|
+
* currently active, and lets callers invalidate all queued work by
|
|
144
|
+
* advancing a generation counter.
|
|
145
|
+
*
|
|
146
|
+
* Used by @cloudflare/ai-chat (full concurrency policy spectrum) and
|
|
147
|
+
* @cloudflare/think (simple serial queue) to prevent overlapping
|
|
148
|
+
* chat turns.
|
|
149
|
+
*/
|
|
150
|
+
type TurnResult<T> = {
|
|
151
|
+
status: "completed";
|
|
152
|
+
value: T;
|
|
153
|
+
} | {
|
|
154
|
+
status: "stale";
|
|
155
|
+
};
|
|
156
|
+
interface EnqueueOptions {
|
|
157
|
+
/**
|
|
158
|
+
* Generation to bind this turn to. Defaults to the current generation
|
|
159
|
+
* at the time of the `enqueue` call. If the queue's generation has
|
|
160
|
+
* advanced past this value by the time the turn reaches the front,
|
|
161
|
+
* `fn` is not called and `{ status: "stale" }` is returned.
|
|
162
|
+
*/
|
|
163
|
+
generation?: number;
|
|
164
|
+
}
|
|
165
|
+
declare class TurnQueue {
|
|
166
|
+
private _queue;
|
|
167
|
+
private _generation;
|
|
168
|
+
private _activeRequestId;
|
|
169
|
+
private _countsByGeneration;
|
|
170
|
+
get generation(): number;
|
|
171
|
+
get activeRequestId(): string | null;
|
|
172
|
+
get isActive(): boolean;
|
|
173
|
+
enqueue<T>(requestId: string, fn: () => Promise<T>, options?: EnqueueOptions): Promise<TurnResult<T>>;
|
|
174
|
+
/**
|
|
175
|
+
* Advance the generation counter. All turns enqueued under older
|
|
176
|
+
* generations will be skipped when they reach the front of the queue.
|
|
177
|
+
*/
|
|
178
|
+
reset(): void;
|
|
179
|
+
/**
|
|
180
|
+
* Wait until the queue is fully drained (no pending or active turns).
|
|
181
|
+
*/
|
|
182
|
+
waitForIdle(): Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* Number of active + queued turns for a given generation.
|
|
185
|
+
* Defaults to the current generation.
|
|
186
|
+
*/
|
|
187
|
+
queuedCount(generation?: number): number;
|
|
188
|
+
private _decrementCount;
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/chat/broadcast-state.d.ts
|
|
192
|
+
type BroadcastStreamState = {
|
|
193
|
+
status: "idle";
|
|
194
|
+
} | {
|
|
195
|
+
status: "observing";
|
|
196
|
+
streamId: string;
|
|
197
|
+
accumulator: StreamAccumulator;
|
|
198
|
+
};
|
|
199
|
+
type BroadcastStreamEvent = {
|
|
200
|
+
type: "response";
|
|
201
|
+
streamId: string; /** Fallback message ID for a new accumulator (ignored if one exists for this stream). */
|
|
202
|
+
messageId: string;
|
|
203
|
+
chunkData?: unknown;
|
|
204
|
+
done?: boolean;
|
|
205
|
+
error?: boolean;
|
|
206
|
+
replay?: boolean;
|
|
207
|
+
replayComplete?: boolean;
|
|
208
|
+
continuation?: boolean; /** Required when continuation=true so the accumulator can pick up existing parts. */
|
|
209
|
+
currentMessages?: UIMessage[];
|
|
210
|
+
} | {
|
|
211
|
+
type: "resume-fallback";
|
|
212
|
+
streamId: string;
|
|
213
|
+
messageId: string;
|
|
214
|
+
} | {
|
|
215
|
+
type: "clear";
|
|
216
|
+
};
|
|
217
|
+
interface TransitionResult {
|
|
218
|
+
state: BroadcastStreamState;
|
|
219
|
+
messagesUpdate?: (prev: UIMessage[]) => UIMessage[];
|
|
220
|
+
isStreaming: boolean;
|
|
221
|
+
}
|
|
222
|
+
declare function transition(state: BroadcastStreamState, event: BroadcastStreamEvent): TransitionResult;
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/chat/resumable-stream.d.ts
|
|
225
|
+
/**
|
|
226
|
+
* Minimal SQL interface matching Agent's this.sql tagged template.
|
|
227
|
+
* Allows ResumableStream to work with the Agent's SQLite without
|
|
228
|
+
* depending on the full Agent class.
|
|
229
|
+
*/
|
|
230
|
+
type SqlTaggedTemplate = {
|
|
231
|
+
<T = Record<string, unknown>>(strings: TemplateStringsArray, ...values: (string | number | boolean | null)[]): T[];
|
|
232
|
+
};
|
|
233
|
+
declare class ResumableStream {
|
|
234
|
+
private sql;
|
|
235
|
+
private _activeStreamId;
|
|
236
|
+
private _activeRequestId;
|
|
237
|
+
private _streamChunkIndex;
|
|
238
|
+
/**
|
|
239
|
+
* Whether the active stream was started in this instance (true) or
|
|
240
|
+
* restored from SQLite after hibernation/restart (false). An orphaned
|
|
241
|
+
* stream has no live LLM reader — the ReadableStream was lost when the
|
|
242
|
+
* DO was evicted.
|
|
243
|
+
*/
|
|
244
|
+
private _isLive;
|
|
245
|
+
private _chunkBuffer;
|
|
246
|
+
private _isFlushingChunks;
|
|
247
|
+
private _lastCleanupTime;
|
|
248
|
+
constructor(sql: SqlTaggedTemplate);
|
|
249
|
+
get activeStreamId(): string | null;
|
|
250
|
+
get activeRequestId(): string | null;
|
|
251
|
+
hasActiveStream(): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Whether the active stream has a live LLM reader (started in this
|
|
254
|
+
* instance) vs being restored from SQLite after hibernation (orphaned).
|
|
255
|
+
*/
|
|
256
|
+
get isLive(): boolean;
|
|
257
|
+
/**
|
|
258
|
+
* Start tracking a new stream for resumable streaming.
|
|
259
|
+
* Creates metadata entry in SQLite and sets up tracking state.
|
|
260
|
+
* @param requestId - The unique ID of the chat request
|
|
261
|
+
* @returns The generated stream ID
|
|
262
|
+
*/
|
|
263
|
+
start(requestId: string): string;
|
|
264
|
+
/**
|
|
265
|
+
* Mark a stream as completed and flush any pending chunks.
|
|
266
|
+
* @param streamId - The stream to mark as completed
|
|
267
|
+
*/
|
|
268
|
+
complete(streamId: string): void;
|
|
269
|
+
/**
|
|
270
|
+
* Mark a stream as errored and clean up state.
|
|
271
|
+
* @param streamId - The stream to mark as errored
|
|
272
|
+
*/
|
|
273
|
+
markError(streamId: string): void;
|
|
274
|
+
/** Maximum chunk body size before skipping storage (bytes). Prevents SQLite row limit crash. */
|
|
275
|
+
private static CHUNK_MAX_BYTES;
|
|
276
|
+
/**
|
|
277
|
+
* Buffer a stream chunk for batch write to SQLite.
|
|
278
|
+
* Chunks exceeding the row size limit are skipped to prevent crashes.
|
|
279
|
+
* The chunk is still broadcast to live clients (caller handles that),
|
|
280
|
+
* but will be missing from replay on reconnection.
|
|
281
|
+
* @param streamId - The stream this chunk belongs to
|
|
282
|
+
* @param body - The serialized chunk body
|
|
283
|
+
*/
|
|
284
|
+
storeChunk(streamId: string, body: string): void;
|
|
285
|
+
/**
|
|
286
|
+
* Flush buffered chunks to SQLite in a single batch.
|
|
287
|
+
* Uses a lock to prevent concurrent flush operations.
|
|
288
|
+
*/
|
|
289
|
+
flushBuffer(): void;
|
|
290
|
+
/**
|
|
291
|
+
* Send stored stream chunks to a connection for replay.
|
|
292
|
+
* Chunks are marked with replay: true so the client can batch-apply them.
|
|
293
|
+
*
|
|
294
|
+
* Three outcomes:
|
|
295
|
+
* - **Live stream**: sends chunks + `replayComplete` — client flushes and
|
|
296
|
+
* continues receiving live chunks from the LLM reader.
|
|
297
|
+
* - **Orphaned stream** (restored from SQLite after hibernation, no reader):
|
|
298
|
+
* sends chunks + `done` and completes the stream. The caller should
|
|
299
|
+
* reconstruct and persist the partial message from the stored chunks.
|
|
300
|
+
* - **Completed during replay** (defensive): sends chunks + `done`.
|
|
301
|
+
*
|
|
302
|
+
* @param connection - The WebSocket connection
|
|
303
|
+
* @param requestId - The original request ID
|
|
304
|
+
* @returns The stream ID if the stream was orphaned and finalized, null otherwise.
|
|
305
|
+
* When non-null the caller should reconstruct the message from chunks.
|
|
306
|
+
*/
|
|
307
|
+
replayChunks(connection: Connection, requestId: string): string | null;
|
|
308
|
+
/**
|
|
309
|
+
* Restore active stream state if the agent was restarted during streaming.
|
|
310
|
+
* All streams are restored regardless of age — stale cleanup happens
|
|
311
|
+
* lazily in _maybeCleanupOldStreams after recovery has had its chance.
|
|
312
|
+
*/
|
|
313
|
+
restore(): void;
|
|
314
|
+
/**
|
|
315
|
+
* Clear all stream data (called on chat history clear).
|
|
316
|
+
*/
|
|
317
|
+
clearAll(): void;
|
|
318
|
+
/**
|
|
319
|
+
* Drop all stream tables (called on destroy).
|
|
320
|
+
*/
|
|
321
|
+
destroy(): void;
|
|
322
|
+
private _maybeCleanupOldStreams;
|
|
323
|
+
/** @internal For testing only */
|
|
324
|
+
getStreamChunks(streamId: string): Array<{
|
|
325
|
+
body: string;
|
|
326
|
+
chunk_index: number;
|
|
327
|
+
}>;
|
|
328
|
+
/** @internal For testing only */
|
|
329
|
+
getStreamMetadata(streamId: string): {
|
|
330
|
+
status: string;
|
|
331
|
+
request_id: string;
|
|
332
|
+
} | null;
|
|
333
|
+
/** @internal For testing only */
|
|
334
|
+
getAllStreamMetadata(): Array<{
|
|
335
|
+
id: string;
|
|
336
|
+
status: string;
|
|
337
|
+
request_id: string;
|
|
338
|
+
created_at: number;
|
|
339
|
+
}>;
|
|
340
|
+
/** @internal For testing only */
|
|
341
|
+
insertStaleStream(streamId: string, requestId: string, ageMs: number): void;
|
|
342
|
+
}
|
|
343
|
+
//#endregion
|
|
344
|
+
//#region src/chat/client-tools.d.ts
|
|
345
|
+
/**
|
|
346
|
+
* Wire-format tool schema sent from the client.
|
|
347
|
+
* Uses `parameters` (JSONSchema7) rather than AI SDK's `inputSchema`
|
|
348
|
+
* because Zod schemas cannot be serialized over the wire.
|
|
349
|
+
*/
|
|
350
|
+
type ClientToolSchema = {
|
|
351
|
+
/** Unique name for the tool */name: string; /** Human-readable description of what the tool does */
|
|
352
|
+
description?: Tool["description"]; /** JSON Schema defining the tool's input parameters */
|
|
353
|
+
parameters?: JSONSchema7;
|
|
354
|
+
};
|
|
355
|
+
/**
|
|
356
|
+
* Converts client tool schemas to AI SDK tool format.
|
|
357
|
+
*
|
|
358
|
+
* These tools have no `execute` function — when the AI model calls them,
|
|
359
|
+
* the tool call is sent back to the client for execution.
|
|
360
|
+
*
|
|
361
|
+
* @param clientTools - Array of tool schemas from the client
|
|
362
|
+
* @returns Record of AI SDK tools that can be spread into your tools object
|
|
363
|
+
*/
|
|
364
|
+
declare function createToolsFromClientSchemas(clientTools?: ClientToolSchema[]): ToolSet;
|
|
365
|
+
//#endregion
|
|
366
|
+
//#region src/chat/protocol.d.ts
|
|
367
|
+
/**
|
|
368
|
+
* Wire protocol message type constants for the cf_agent_chat_* protocol.
|
|
369
|
+
*
|
|
370
|
+
* These are the string values used on the wire between agent servers and
|
|
371
|
+
* clients. Both @cloudflare/ai-chat (via its MessageType enum) and
|
|
372
|
+
* @cloudflare/think use these values.
|
|
373
|
+
*/
|
|
374
|
+
declare const CHAT_MESSAGE_TYPES: {
|
|
375
|
+
readonly CHAT_MESSAGES: "cf_agent_chat_messages";
|
|
376
|
+
readonly USE_CHAT_REQUEST: "cf_agent_use_chat_request";
|
|
377
|
+
readonly USE_CHAT_RESPONSE: "cf_agent_use_chat_response";
|
|
378
|
+
readonly CHAT_CLEAR: "cf_agent_chat_clear";
|
|
379
|
+
readonly CHAT_REQUEST_CANCEL: "cf_agent_chat_request_cancel";
|
|
380
|
+
readonly STREAM_RESUMING: "cf_agent_stream_resuming";
|
|
381
|
+
readonly STREAM_RESUME_ACK: "cf_agent_stream_resume_ack";
|
|
382
|
+
readonly STREAM_RESUME_REQUEST: "cf_agent_stream_resume_request";
|
|
383
|
+
readonly STREAM_RESUME_NONE: "cf_agent_stream_resume_none";
|
|
384
|
+
readonly TOOL_RESULT: "cf_agent_tool_result";
|
|
385
|
+
readonly TOOL_APPROVAL: "cf_agent_tool_approval";
|
|
386
|
+
readonly MESSAGE_UPDATED: "cf_agent_message_updated";
|
|
387
|
+
};
|
|
388
|
+
//#endregion
|
|
389
|
+
//#region src/chat/continuation-state.d.ts
|
|
390
|
+
/**
|
|
391
|
+
* Minimal connection interface for sending WebSocket messages.
|
|
392
|
+
* Matches the Connection type from agents without importing it.
|
|
393
|
+
* Uses a permissive send signature so Connection (which extends
|
|
394
|
+
* WebSocket with its own send overload) is structurally assignable.
|
|
395
|
+
*/
|
|
396
|
+
interface ContinuationConnection {
|
|
397
|
+
readonly id: string;
|
|
398
|
+
send(message: string): void;
|
|
399
|
+
}
|
|
400
|
+
interface ContinuationPending {
|
|
401
|
+
connection: ContinuationConnection;
|
|
402
|
+
connectionId: string;
|
|
403
|
+
requestId: string;
|
|
404
|
+
clientTools?: ClientToolSchema[];
|
|
405
|
+
body?: Record<string, unknown>;
|
|
406
|
+
errorPrefix: string | null;
|
|
407
|
+
prerequisite: Promise<boolean> | null;
|
|
408
|
+
pastCoalesce: boolean;
|
|
409
|
+
}
|
|
410
|
+
interface ContinuationDeferred {
|
|
411
|
+
connection: ContinuationConnection;
|
|
412
|
+
connectionId: string;
|
|
413
|
+
clientTools?: ClientToolSchema[];
|
|
414
|
+
body?: Record<string, unknown>;
|
|
415
|
+
errorPrefix: string;
|
|
416
|
+
prerequisite: Promise<boolean> | null;
|
|
417
|
+
}
|
|
418
|
+
declare class ContinuationState {
|
|
419
|
+
pending: ContinuationPending | null;
|
|
420
|
+
deferred: ContinuationDeferred | null;
|
|
421
|
+
activeRequestId: string | null;
|
|
422
|
+
activeConnectionId: string | null;
|
|
423
|
+
awaitingConnections: Map<string, ContinuationConnection>;
|
|
424
|
+
/** Clear pending state and awaiting connections (without sending RESUME_NONE). */
|
|
425
|
+
clearPending(): void;
|
|
426
|
+
clearDeferred(): void;
|
|
427
|
+
clearAll(): void;
|
|
428
|
+
/**
|
|
429
|
+
* Send STREAM_RESUME_NONE to all connections waiting for a
|
|
430
|
+
* continuation stream to start, then clear the map.
|
|
431
|
+
*/
|
|
432
|
+
sendResumeNone(): void;
|
|
433
|
+
/**
|
|
434
|
+
* Flush awaiting connections by notifying each one via the provided
|
|
435
|
+
* callback (typically sends STREAM_RESUMING), then clear.
|
|
436
|
+
*/
|
|
437
|
+
flushAwaitingConnections(notify: (conn: ContinuationConnection) => void): void;
|
|
438
|
+
/**
|
|
439
|
+
* Transition pending → active. Called when the continuation stream
|
|
440
|
+
* actually starts. Moves request/connection IDs to active slots,
|
|
441
|
+
* clears pending fields.
|
|
442
|
+
*/
|
|
443
|
+
activatePending(): void;
|
|
444
|
+
/**
|
|
445
|
+
* Transition deferred → pending. Called when a continuation turn
|
|
446
|
+
* completes and there's a deferred follow-up waiting.
|
|
447
|
+
*
|
|
448
|
+
* Returns the new pending state (so the host can enqueue the turn),
|
|
449
|
+
* or null if there was nothing deferred.
|
|
450
|
+
*/
|
|
451
|
+
activateDeferred(generateRequestId: () => string): ContinuationPending | null;
|
|
452
|
+
}
|
|
453
|
+
//#endregion
|
|
454
|
+
//#region src/chat/abort-registry.d.ts
|
|
455
|
+
/**
|
|
456
|
+
* AbortRegistry — manages per-request AbortControllers.
|
|
457
|
+
*
|
|
458
|
+
* Shared between AIChatAgent and Think for chat turn cancellation.
|
|
459
|
+
* Each request gets its own AbortController keyed by request ID.
|
|
460
|
+
* Controllers are created lazily on first signal access.
|
|
461
|
+
*/
|
|
462
|
+
declare class AbortRegistry {
|
|
463
|
+
private controllers;
|
|
464
|
+
/**
|
|
465
|
+
* Get or create an AbortController for the given ID and return its signal.
|
|
466
|
+
* Creates the controller lazily on first access.
|
|
467
|
+
*/
|
|
468
|
+
getSignal(id: string): AbortSignal | undefined;
|
|
469
|
+
/**
|
|
470
|
+
* Get the signal for an existing controller without creating one.
|
|
471
|
+
* Returns undefined if no controller exists for this ID.
|
|
472
|
+
*/
|
|
473
|
+
getExistingSignal(id: string): AbortSignal | undefined;
|
|
474
|
+
/** Cancel a specific request by aborting its controller. */
|
|
475
|
+
cancel(id: string): void;
|
|
476
|
+
/** Remove a controller after the request completes. */
|
|
477
|
+
remove(id: string): void;
|
|
478
|
+
/** Abort all pending requests and clear the registry. */
|
|
479
|
+
destroyAll(): void;
|
|
480
|
+
/** Check if a controller exists for the given ID. */
|
|
481
|
+
has(id: string): boolean;
|
|
482
|
+
/** Number of tracked controllers. */
|
|
483
|
+
get size(): number;
|
|
484
|
+
}
|
|
485
|
+
//#endregion
|
|
486
|
+
//#region src/chat/tool-state.d.ts
|
|
487
|
+
/**
|
|
488
|
+
* Tool State — shared update builders and applicator for tool part state changes.
|
|
489
|
+
*
|
|
490
|
+
* Used by both AIChatAgent and Think to apply tool results and approvals
|
|
491
|
+
* to message parts. Each agent handles find-message, persist, and broadcast
|
|
492
|
+
* in their own way; this module provides the state matching and update logic.
|
|
493
|
+
*/
|
|
494
|
+
/**
|
|
495
|
+
* Describes an update to apply to a tool part.
|
|
496
|
+
*/
|
|
497
|
+
type ToolPartUpdate = {
|
|
498
|
+
toolCallId: string;
|
|
499
|
+
matchStates: string[];
|
|
500
|
+
apply: (part: Record<string, unknown>) => Record<string, unknown>;
|
|
501
|
+
};
|
|
502
|
+
/**
|
|
503
|
+
* Apply a tool part update to a parts array.
|
|
504
|
+
* Finds the first part matching `update.toolCallId` in one of `update.matchStates`,
|
|
505
|
+
* applies the update immutably, and returns the new parts array with the index.
|
|
506
|
+
*
|
|
507
|
+
* Returns `null` if no matching part was found.
|
|
508
|
+
*/
|
|
509
|
+
declare function applyToolUpdate(parts: Array<Record<string, unknown>>, update: ToolPartUpdate): {
|
|
510
|
+
parts: Array<Record<string, unknown>>;
|
|
511
|
+
index: number;
|
|
512
|
+
} | null;
|
|
513
|
+
/**
|
|
514
|
+
* Build an update descriptor for applying a tool result.
|
|
515
|
+
*
|
|
516
|
+
* Matches parts in `input-available`, `approval-requested`, or `approval-responded` state.
|
|
517
|
+
* Sets state to `output-available` (with output) or `output-error` (with errorText).
|
|
518
|
+
*/
|
|
519
|
+
declare function toolResultUpdate(toolCallId: string, output: unknown, overrideState?: "output-error", errorText?: string): ToolPartUpdate;
|
|
520
|
+
/**
|
|
521
|
+
* Build an update descriptor for applying a tool approval.
|
|
522
|
+
*
|
|
523
|
+
* Matches parts in `input-available` or `approval-requested` state.
|
|
524
|
+
* Sets state to `approval-responded` (if approved) or `output-denied` (if denied).
|
|
525
|
+
*/
|
|
526
|
+
declare function toolApprovalUpdate(toolCallId: string, approved: boolean): ToolPartUpdate;
|
|
527
|
+
//#endregion
|
|
528
|
+
//#region src/chat/parse-protocol.d.ts
|
|
529
|
+
/**
|
|
530
|
+
* Protocol Message Parser — typed parsing of cf_agent_chat_* WebSocket messages.
|
|
531
|
+
*
|
|
532
|
+
* Parses raw WebSocket messages into a discriminated union of protocol events.
|
|
533
|
+
* Both AIChatAgent and Think can use this instead of manual JSON.parse + type checking.
|
|
534
|
+
*/
|
|
535
|
+
/**
|
|
536
|
+
* Discriminated union of all incoming chat protocol events.
|
|
537
|
+
*
|
|
538
|
+
* Each agent handles the events it cares about and ignores the rest.
|
|
539
|
+
* Returns `null` for non-JSON messages or unrecognized types.
|
|
540
|
+
*/
|
|
541
|
+
type ChatProtocolEvent = {
|
|
542
|
+
type: "chat-request";
|
|
543
|
+
id: string;
|
|
544
|
+
init: {
|
|
545
|
+
method?: string;
|
|
546
|
+
body?: string;
|
|
547
|
+
[key: string]: unknown;
|
|
548
|
+
};
|
|
549
|
+
} | {
|
|
550
|
+
type: "clear";
|
|
551
|
+
} | {
|
|
552
|
+
type: "cancel";
|
|
553
|
+
id: string;
|
|
554
|
+
} | {
|
|
555
|
+
type: "tool-result";
|
|
556
|
+
toolCallId: string;
|
|
557
|
+
toolName: string;
|
|
558
|
+
output: unknown;
|
|
559
|
+
state?: string;
|
|
560
|
+
errorText?: string;
|
|
561
|
+
autoContinue?: boolean;
|
|
562
|
+
clientTools?: Array<{
|
|
563
|
+
name: string;
|
|
564
|
+
description?: string;
|
|
565
|
+
parameters?: unknown;
|
|
566
|
+
}>;
|
|
567
|
+
} | {
|
|
568
|
+
type: "tool-approval";
|
|
569
|
+
toolCallId: string;
|
|
570
|
+
approved: boolean;
|
|
571
|
+
autoContinue?: boolean;
|
|
572
|
+
} | {
|
|
573
|
+
type: "stream-resume-request";
|
|
574
|
+
} | {
|
|
575
|
+
type: "stream-resume-ack";
|
|
576
|
+
id: string;
|
|
577
|
+
} | {
|
|
578
|
+
type: "messages";
|
|
579
|
+
messages: unknown[];
|
|
580
|
+
};
|
|
581
|
+
/**
|
|
582
|
+
* Parse a raw WebSocket message string into a typed protocol event.
|
|
583
|
+
*
|
|
584
|
+
* Returns `null` if the message is not valid JSON or not a recognized
|
|
585
|
+
* protocol message type. Callers should fall through to the user's
|
|
586
|
+
* `onMessage` handler when `null` is returned.
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* ```typescript
|
|
590
|
+
* const event = parseProtocolMessage(rawMessage);
|
|
591
|
+
* if (!event) return userOnMessage(connection, rawMessage);
|
|
592
|
+
*
|
|
593
|
+
* switch (event.type) {
|
|
594
|
+
* case "chat-request": { ... }
|
|
595
|
+
* case "clear": { ... }
|
|
596
|
+
* case "tool-result": { ... }
|
|
597
|
+
* }
|
|
598
|
+
* ```
|
|
599
|
+
*/
|
|
600
|
+
declare function parseProtocolMessage(raw: string): ChatProtocolEvent | null;
|
|
601
|
+
//#endregion
|
|
602
|
+
export { AbortRegistry, type BroadcastStreamEvent, type BroadcastStreamState, type TransitionResult as BroadcastTransitionResult, CHAT_MESSAGE_TYPES, type ChatProtocolEvent, type ChunkAction, type ChunkResult, type ClientToolSchema, type ContinuationConnection, type ContinuationDeferred, type ContinuationPending, ContinuationState, type EnqueueOptions, type MessagePart, type MessageParts, ROW_MAX_BYTES, ResumableStream, type SqlTaggedTemplate, StreamAccumulator, type StreamAccumulatorOptions, type StreamChunkData, type ToolPartUpdate, TurnQueue, type TurnResult, applyChunkToParts, applyToolUpdate, transition as broadcastTransition, byteLength, createToolsFromClientSchemas, enforceRowSizeLimit, parseProtocolMessage, sanitizeMessage, toolApprovalUpdate, toolResultUpdate };
|
|
603
|
+
//# sourceMappingURL=index.d.ts.map
|