@cloudflare/ai-chat 0.4.6 → 0.5.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 +12 -12
- package/dist/index.d.ts +37 -75
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/react.d.ts +25 -4
- package/dist/react.js +88 -21
- package/dist/react.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ export class ChatAgent extends AIChatAgent {
|
|
|
22
22
|
const workersai = createWorkersAI({ binding: this.env.AI });
|
|
23
23
|
|
|
24
24
|
const result = streamText({
|
|
25
|
-
model: workersai("@cf/moonshotai/kimi-k2.
|
|
25
|
+
model: workersai("@cf/moonshotai/kimi-k2.6"),
|
|
26
26
|
messages: await convertToModelMessages(this.messages)
|
|
27
27
|
});
|
|
28
28
|
|
|
@@ -110,7 +110,7 @@ export class ChatAgent extends AIChatAgent {
|
|
|
110
110
|
const workersai = createWorkersAI({ binding: this.env.AI });
|
|
111
111
|
|
|
112
112
|
const result = streamText({
|
|
113
|
-
model: workersai("@cf/moonshotai/kimi-k2.
|
|
113
|
+
model: workersai("@cf/moonshotai/kimi-k2.6"),
|
|
114
114
|
messages: await convertToModelMessages(this.messages),
|
|
115
115
|
tools: {
|
|
116
116
|
getWeather: tool({
|
|
@@ -289,7 +289,7 @@ export class ChatAgent extends AIChatAgent {
|
|
|
289
289
|
const workersai = createWorkersAI({ binding: this.env.AI });
|
|
290
290
|
|
|
291
291
|
const result = streamText({
|
|
292
|
-
model: workersai("@cf/moonshotai/kimi-k2.
|
|
292
|
+
model: workersai("@cf/moonshotai/kimi-k2.6"),
|
|
293
293
|
messages: pruneMessages({
|
|
294
294
|
messages: await convertToModelMessages(this.messages),
|
|
295
295
|
reasoning: "before-last-message",
|
|
@@ -333,13 +333,13 @@ async onChatMessage(onFinish, options) {
|
|
|
333
333
|
|
|
334
334
|
## API Reference
|
|
335
335
|
|
|
336
|
-
### `AIChatAgent<Env, State>`
|
|
336
|
+
### `AIChatAgent<Env, State, Props>`
|
|
337
337
|
|
|
338
338
|
Extends `Agent` from the `agents` package.
|
|
339
339
|
|
|
340
340
|
| Property / Method | Type | Description |
|
|
341
341
|
| ------------------------------------ | ----------------------------- | ------------------------------------------------------------------------------------------------- |
|
|
342
|
-
| `messages` | `
|
|
342
|
+
| `messages` | `ChatMessage[]` | Current conversation messages (loaded from SQLite) |
|
|
343
343
|
| `maxPersistedMessages` | `number \| undefined` | Max messages to keep in SQLite. Default: unlimited |
|
|
344
344
|
| `messageConcurrency` | `MessageConcurrency` | Concurrency strategy for `sendMessage()` submits. Default: `"queue"` |
|
|
345
345
|
| `onChatMessage(onFinish?, options?)` | Override | Handle incoming chat messages. Return a `Response`. `onFinish` is optional. |
|
|
@@ -364,13 +364,13 @@ React hook for chat interactions. Wraps the AI SDK's `useChat` with WebSocket tr
|
|
|
364
364
|
| `resume` | `boolean` | Enable stream resumption. Default: `true` |
|
|
365
365
|
| `body` | `object \| () => object` | Custom data sent with every request (see below) |
|
|
366
366
|
| `prepareSendMessagesRequest` | `(options) => { body?, headers? }` | Advanced per-request customization |
|
|
367
|
-
| `getInitialMessages` | `(options) => Promise<
|
|
367
|
+
| `getInitialMessages` | `(options) => Promise<ChatMessage[]>` | Custom initial message loader |
|
|
368
368
|
|
|
369
369
|
**Returns:**
|
|
370
370
|
|
|
371
371
|
| Property | Type | Description |
|
|
372
372
|
| ------------------------- | ---------------------------------- | ------------------------------------------------------- |
|
|
373
|
-
| `messages` | `
|
|
373
|
+
| `messages` | `ChatMessage[]` | Chat messages |
|
|
374
374
|
| `sendMessage` | `(message) => void` | Send a message |
|
|
375
375
|
| `clearHistory` | `() => void` | Clear conversation |
|
|
376
376
|
| `addToolOutput` | `({ toolCallId, output }) => void` | Provide tool output |
|
|
@@ -380,11 +380,11 @@ React hook for chat interactions. Wraps the AI SDK's `useChat` with WebSocket tr
|
|
|
380
380
|
|
|
381
381
|
### Exports
|
|
382
382
|
|
|
383
|
-
| Import path | What it provides
|
|
384
|
-
| --------------------------- |
|
|
385
|
-
| `@cloudflare/ai-chat` | `AIChatAgent`, `createToolsFromClientSchemas`
|
|
386
|
-
| `@cloudflare/ai-chat/react` | `useAgentChat`
|
|
387
|
-
| `@cloudflare/ai-chat/types` | `MessageType`, `OutgoingMessage`, `IncomingMessage`
|
|
383
|
+
| Import path | What it provides |
|
|
384
|
+
| --------------------------- | ------------------------------------------------------------ |
|
|
385
|
+
| `@cloudflare/ai-chat` | `AIChatAgent`, `ChatMessage`, `createToolsFromClientSchemas` |
|
|
386
|
+
| `@cloudflare/ai-chat/react` | `useAgentChat` |
|
|
387
|
+
| `@cloudflare/ai-chat/types` | `MessageType`, `OutgoingMessage`, `IncomingMessage` |
|
|
388
388
|
|
|
389
389
|
## Examples
|
|
390
390
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,66 +1,24 @@
|
|
|
1
1
|
import { Agent, AgentContext, FiberRecoveryContext } from "agents";
|
|
2
2
|
import {
|
|
3
|
+
ChatRecoveryContext,
|
|
4
|
+
ChatRecoveryContext as ChatRecoveryContext$1,
|
|
5
|
+
ChatRecoveryOptions,
|
|
6
|
+
ChatRecoveryOptions as ChatRecoveryOptions$1,
|
|
7
|
+
ChatResponseResult,
|
|
8
|
+
ChatResponseResult as ChatResponseResult$1,
|
|
3
9
|
ClientToolSchema,
|
|
4
10
|
ClientToolSchema as ClientToolSchema$1,
|
|
5
|
-
|
|
11
|
+
MessageConcurrency,
|
|
12
|
+
MessageConcurrency as MessageConcurrency$1,
|
|
6
13
|
ResumableStream,
|
|
14
|
+
SaveMessagesResult,
|
|
15
|
+
SaveMessagesResult as SaveMessagesResult$1,
|
|
7
16
|
createToolsFromClientSchemas
|
|
8
17
|
} from "agents/chat";
|
|
9
18
|
import { StreamTextOnFinishCallback, ToolSet, UIMessage } from "ai";
|
|
10
19
|
|
|
11
20
|
//#region src/index.d.ts
|
|
12
|
-
|
|
13
|
-
* Context passed to `onChatRecovery` when an interrupted chat stream
|
|
14
|
-
* is detected after DO restart.
|
|
15
|
-
*/
|
|
16
|
-
type ChatRecoveryContext = {
|
|
17
|
-
/** Stream ID from the interrupted stream. */ streamId: string /** Request ID from the interrupted stream. */;
|
|
18
|
-
requestId: string /** Partial text extracted from stored chunks. */;
|
|
19
|
-
partialText: string /** Partial message parts reconstructed from chunks. */;
|
|
20
|
-
partialParts: MessagePart[] /** Checkpoint data from `this.stash()` during the interrupted stream. */;
|
|
21
|
-
recoveryData: unknown | null /** Current persisted messages. */;
|
|
22
|
-
messages: UIMessage[] /** Custom body from the last chat request. */;
|
|
23
|
-
lastBody?: Record<
|
|
24
|
-
string,
|
|
25
|
-
unknown
|
|
26
|
-
> /** Client tool schemas from the last chat request. */;
|
|
27
|
-
lastClientTools?: ClientToolSchema$1[];
|
|
28
|
-
/**
|
|
29
|
-
* Epoch milliseconds when the underlying fiber was started. Compare against
|
|
30
|
-
* `Date.now()` to suppress continuations for turns that have been orphaned
|
|
31
|
-
* too long to safely replay.
|
|
32
|
-
*/
|
|
33
|
-
createdAt: number;
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Options returned from `onChatRecovery` to control recovery behavior.
|
|
37
|
-
*/
|
|
38
|
-
type ChatRecoveryOptions = {
|
|
39
|
-
/** Save the partial response from stored chunks. Default: true. */ persist?: boolean /** Schedule a continuation via continueLastTurn(). Default: true. */;
|
|
40
|
-
continue?: boolean;
|
|
41
|
-
};
|
|
42
|
-
type MessageConcurrency =
|
|
43
|
-
| "queue"
|
|
44
|
-
| "latest"
|
|
45
|
-
| "merge"
|
|
46
|
-
| "drop"
|
|
47
|
-
| {
|
|
48
|
-
strategy: "debounce";
|
|
49
|
-
debounceMs?: number;
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* Result passed to the `onChatResponse` lifecycle hook after a chat turn completes.
|
|
53
|
-
*/
|
|
54
|
-
type ChatResponseResult = {
|
|
55
|
-
/** The finalized assistant message from this turn. */ message: UIMessage /** The request ID associated with this turn. */;
|
|
56
|
-
requestId: string /** Whether this turn was a continuation of a previous assistant turn. */;
|
|
57
|
-
continuation: boolean /** How the turn ended. */;
|
|
58
|
-
status:
|
|
59
|
-
| "completed"
|
|
60
|
-
| "error"
|
|
61
|
-
| "aborted" /** Error message when `status` is `"error"`. */;
|
|
62
|
-
error?: string;
|
|
63
|
-
};
|
|
21
|
+
type ChatMessage = UIMessage;
|
|
64
22
|
/**
|
|
65
23
|
* Options passed to the onChatMessage handler.
|
|
66
24
|
*/
|
|
@@ -110,21 +68,15 @@ type OnChatMessageOptions = {
|
|
|
110
68
|
*/
|
|
111
69
|
continuation?: boolean;
|
|
112
70
|
};
|
|
113
|
-
/**
|
|
114
|
-
* Result returned by `saveMessages()`.
|
|
115
|
-
*/
|
|
116
|
-
type SaveMessagesResult = {
|
|
117
|
-
/** Server-generated request ID for the chat turn. */ requestId: string /** Whether the turn ran or was skipped (e.g. because the chat was cleared). */;
|
|
118
|
-
status: "completed" | "skipped";
|
|
119
|
-
};
|
|
120
71
|
/**
|
|
121
72
|
* Extension of Agent with built-in chat capabilities
|
|
122
73
|
* @template Env Environment type containing bindings
|
|
123
74
|
*/
|
|
124
75
|
declare class AIChatAgent<
|
|
125
76
|
Env extends Cloudflare.Env = Cloudflare.Env,
|
|
126
|
-
State = unknown
|
|
127
|
-
|
|
77
|
+
State = unknown,
|
|
78
|
+
Props extends Record<string, unknown> = Record<string, unknown>
|
|
79
|
+
> extends Agent<Env, State, Props> {
|
|
128
80
|
/**
|
|
129
81
|
* Registry of per-request AbortControllers.
|
|
130
82
|
* Used to propagate cancellation signals for any external calls made by the agent.
|
|
@@ -268,7 +220,7 @@ declare class AIChatAgent<
|
|
|
268
220
|
* programmatic `saveMessages()` calls keep their existing serialized
|
|
269
221
|
* behavior.
|
|
270
222
|
*/
|
|
271
|
-
messageConcurrency: MessageConcurrency;
|
|
223
|
+
messageConcurrency: MessageConcurrency$1;
|
|
272
224
|
/**
|
|
273
225
|
* When enabled, waits for all MCP server connections to be ready before
|
|
274
226
|
* calling `onChatMessage`. This prevents the race condition where
|
|
@@ -301,7 +253,14 @@ declare class AIChatAgent<
|
|
|
301
253
|
| {
|
|
302
254
|
timeout: number;
|
|
303
255
|
};
|
|
304
|
-
/**
|
|
256
|
+
/**
|
|
257
|
+
* Array of chat messages for the current conversation.
|
|
258
|
+
*
|
|
259
|
+
* Public and mutable for backwards compatibility. Framework code
|
|
260
|
+
* generally writes through `saveMessages` / `persistMessages`, but
|
|
261
|
+
* existing subclasses may still assign `this.messages = [...]` or
|
|
262
|
+
* mutate the array directly.
|
|
263
|
+
*/
|
|
305
264
|
messages: UIMessage[];
|
|
306
265
|
constructor(ctx: AgentContext, env: Env);
|
|
307
266
|
private _flushAwaitingStreamStartConnections;
|
|
@@ -460,7 +419,7 @@ declare class AIChatAgent<
|
|
|
460
419
|
* }
|
|
461
420
|
* ```
|
|
462
421
|
*/
|
|
463
|
-
protected onChatResponse(_result: ChatResponseResult): void | Promise<void>;
|
|
422
|
+
protected onChatResponse(_result: ChatResponseResult$1): void | Promise<void>;
|
|
464
423
|
/**
|
|
465
424
|
* Override this method to apply custom transformations to messages before
|
|
466
425
|
* they are persisted to storage. This hook runs **after** the built-in
|
|
@@ -515,8 +474,10 @@ declare class AIChatAgent<
|
|
|
515
474
|
saveMessages(
|
|
516
475
|
messages:
|
|
517
476
|
| UIMessage[]
|
|
518
|
-
| ((
|
|
519
|
-
|
|
477
|
+
| ((
|
|
478
|
+
currentMessages: readonly UIMessage[]
|
|
479
|
+
) => UIMessage[] | Promise<UIMessage[]>)
|
|
480
|
+
): Promise<SaveMessagesResult$1>;
|
|
520
481
|
/**
|
|
521
482
|
* Trigger a continuation of the last assistant message without inserting
|
|
522
483
|
* a new user message. The LLM sees the full conversation (including the
|
|
@@ -531,7 +492,7 @@ declare class AIChatAgent<
|
|
|
531
492
|
*/
|
|
532
493
|
protected continueLastTurn(
|
|
533
494
|
body?: Record<string, unknown>
|
|
534
|
-
): Promise<SaveMessagesResult>;
|
|
495
|
+
): Promise<SaveMessagesResult$1>;
|
|
535
496
|
/**
|
|
536
497
|
* Context passed to `onChatRecovery` when an interrupted chat stream
|
|
537
498
|
* is detected after DO restart.
|
|
@@ -557,8 +518,8 @@ declare class AIChatAgent<
|
|
|
557
518
|
* during streaming (e.g., OpenAI `responseId`).
|
|
558
519
|
*/
|
|
559
520
|
protected onChatRecovery(
|
|
560
|
-
_ctx: ChatRecoveryContext
|
|
561
|
-
): Promise<ChatRecoveryOptions>;
|
|
521
|
+
_ctx: ChatRecoveryContext$1
|
|
522
|
+
): Promise<ChatRecoveryOptions$1>;
|
|
562
523
|
_chatRecoveryContinue(data?: { targetAssistantId?: string }): Promise<void>;
|
|
563
524
|
/**
|
|
564
525
|
* Extract partial text and parts from stored stream chunks.
|
|
@@ -714,13 +675,14 @@ declare class AIChatAgent<
|
|
|
714
675
|
//#endregion
|
|
715
676
|
export {
|
|
716
677
|
AIChatAgent,
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
678
|
+
ChatMessage,
|
|
679
|
+
type ChatRecoveryContext,
|
|
680
|
+
type ChatRecoveryOptions,
|
|
681
|
+
type ChatResponseResult,
|
|
720
682
|
type ClientToolSchema,
|
|
721
|
-
MessageConcurrency,
|
|
683
|
+
type MessageConcurrency,
|
|
722
684
|
OnChatMessageOptions,
|
|
723
|
-
SaveMessagesResult,
|
|
685
|
+
type SaveMessagesResult,
|
|
724
686
|
createToolsFromClientSchemas
|
|
725
687
|
};
|
|
726
688
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -179,6 +179,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
179
179
|
this.maxPersistedMessages = void 0;
|
|
180
180
|
this.messageConcurrency = "queue";
|
|
181
181
|
this.waitForMcpConnections = { timeout: 1e4 };
|
|
182
|
+
this.messages = [];
|
|
182
183
|
this.sql`create table if not exists cf_ai_chat_agent_messages (
|
|
183
184
|
id text primary key,
|
|
184
185
|
message text not null,
|