@cloudflare/ai-chat 0.1.8 → 0.1.9

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/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type { JSONSchema7, UIMessage } from \"ai\";\n\n/**\n * Enum for message types to improve type safety and maintainability\n */\nexport enum MessageType {\n CF_AGENT_CHAT_MESSAGES = \"cf_agent_chat_messages\",\n CF_AGENT_USE_CHAT_REQUEST = \"cf_agent_use_chat_request\",\n CF_AGENT_USE_CHAT_RESPONSE = \"cf_agent_use_chat_response\",\n CF_AGENT_CHAT_CLEAR = \"cf_agent_chat_clear\",\n CF_AGENT_CHAT_REQUEST_CANCEL = \"cf_agent_chat_request_cancel\",\n\n /** Sent by server when client connects and there's an active stream to resume */\n CF_AGENT_STREAM_RESUMING = \"cf_agent_stream_resuming\",\n /** Sent by client to acknowledge stream resuming notification and request chunks */\n CF_AGENT_STREAM_RESUME_ACK = \"cf_agent_stream_resume_ack\",\n /** Sent by client after message handler is ready, requesting stream resume check */\n CF_AGENT_STREAM_RESUME_REQUEST = \"cf_agent_stream_resume_request\",\n /** Sent by server when client requests resume but no active stream exists */\n CF_AGENT_STREAM_RESUME_NONE = \"cf_agent_stream_resume_none\",\n\n /** Client sends tool result to server (for client-side tools) */\n CF_AGENT_TOOL_RESULT = \"cf_agent_tool_result\",\n /** Server notifies client that a message was updated (e.g., tool result applied) */\n CF_AGENT_MESSAGE_UPDATED = \"cf_agent_message_updated\",\n /** Client sends tool approval response to server (for tools with needsApproval) */\n CF_AGENT_TOOL_APPROVAL = \"cf_agent_tool_approval\"\n}\n\n/**\n * Types of messages sent from the Agent to clients\n */\nexport type OutgoingMessage<ChatMessage extends UIMessage = UIMessage> =\n | {\n /** Indicates this message is a command to clear chat history */\n type: MessageType.CF_AGENT_CHAT_CLEAR;\n }\n | {\n /** Indicates this message contains updated chat messages */\n type: MessageType.CF_AGENT_CHAT_MESSAGES;\n /** Array of chat messages */\n messages: ChatMessage[];\n }\n | {\n /** Indicates this message is a response to a chat request */\n type: MessageType.CF_AGENT_USE_CHAT_RESPONSE;\n /** Unique ID of the request this response corresponds to */\n id: string;\n /** Content body of the response */\n body: string;\n /** Whether this is the final chunk of the response */\n done: boolean;\n /** Whether this response contains an error */\n error?: boolean;\n /** Whether this is a continuation (append to last assistant message) */\n continuation?: boolean;\n /** Whether this chunk is being replayed from storage (stream resumption) */\n replay?: boolean;\n /** Signals that replay of stored chunks is complete (stream is still active) */\n replayComplete?: boolean;\n }\n | {\n /** Indicates the server is resuming an active stream */\n type: MessageType.CF_AGENT_STREAM_RESUMING;\n /** The request ID of the stream being resumed */\n id: string;\n }\n | {\n /** Server notifies client that a message was updated (e.g., tool result applied) */\n type: MessageType.CF_AGENT_MESSAGE_UPDATED;\n /** The updated message */\n message: ChatMessage;\n }\n | {\n /** Server responds to resume request when no active stream exists */\n type: MessageType.CF_AGENT_STREAM_RESUME_NONE;\n };\n\n/**\n * Types of messages sent from clients to the Agent\n */\nexport type IncomingMessage<ChatMessage extends UIMessage = UIMessage> =\n | {\n /** Indicates this message is a command to clear chat history */\n type: MessageType.CF_AGENT_CHAT_CLEAR;\n }\n | {\n /** Indicates this message is a request to the chat API */\n type: MessageType.CF_AGENT_USE_CHAT_REQUEST;\n /** Unique ID for this request */\n id: string;\n /** Request initialization options */\n init: Pick<\n RequestInit,\n | \"method\"\n | \"keepalive\"\n | \"headers\"\n | \"body\"\n | \"redirect\"\n | \"integrity\"\n | \"credentials\"\n | \"mode\"\n | \"referrer\"\n | \"referrerPolicy\"\n | \"window\"\n >;\n }\n | {\n /** Indicates this message contains updated chat messages */\n type: MessageType.CF_AGENT_CHAT_MESSAGES;\n /** Array of chat messages */\n messages: ChatMessage[];\n }\n | {\n /** Indicates the user wants to stop generation of this message */\n type: MessageType.CF_AGENT_CHAT_REQUEST_CANCEL;\n id: string;\n }\n | {\n /** Client acknowledges stream resuming notification and is ready to receive chunks */\n type: MessageType.CF_AGENT_STREAM_RESUME_ACK;\n /** The request ID of the stream being resumed */\n id: string;\n }\n | {\n /** Client requests stream resume check after message handler is registered */\n type: MessageType.CF_AGENT_STREAM_RESUME_REQUEST;\n }\n | {\n /** Client sends tool result to server (for client-side tools) */\n type: MessageType.CF_AGENT_TOOL_RESULT;\n /** The tool call ID this result is for */\n toolCallId: string;\n /** The name of the tool */\n toolName: string;\n /** The output from the tool execution */\n output: unknown;\n /** Override the tool part state (e.g. \"output-error\" for custom denial) */\n state?: \"output-available\" | \"output-error\";\n /** Error message when state is \"output-error\" */\n errorText?: string;\n /** Whether server should auto-continue the conversation after applying result */\n autoContinue?: boolean;\n /** Client tool schemas for continuation (client is source of truth) */\n clientTools?: Array<{\n name: string;\n description?: string;\n parameters?: JSONSchema7;\n }>;\n }\n | {\n /** Client sends tool approval response to server (for tools with needsApproval) */\n type: MessageType.CF_AGENT_TOOL_APPROVAL;\n /** The tool call ID this approval is for */\n toolCallId: string;\n /** Whether the tool execution was approved */\n approved: boolean;\n /** Whether server should auto-continue the conversation after applying approval */\n autoContinue?: boolean;\n };\n"],"mappings":";;;;AAKA,IAAY,oDAAL;AACL;AACA;AACA;AACA;AACA;;AAGA;;AAEA;;AAEA;;AAEA;;AAGA;;AAEA;;AAEA"}
1
+ {"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type { JSONSchema7, UIMessage } from \"ai\";\n\n/**\n * Enum for message types to improve type safety and maintainability\n */\nexport enum MessageType {\n CF_AGENT_CHAT_MESSAGES = \"cf_agent_chat_messages\",\n CF_AGENT_USE_CHAT_REQUEST = \"cf_agent_use_chat_request\",\n CF_AGENT_USE_CHAT_RESPONSE = \"cf_agent_use_chat_response\",\n CF_AGENT_CHAT_CLEAR = \"cf_agent_chat_clear\",\n CF_AGENT_CHAT_REQUEST_CANCEL = \"cf_agent_chat_request_cancel\",\n\n /** Sent by server when client connects and there's an active stream to resume */\n CF_AGENT_STREAM_RESUMING = \"cf_agent_stream_resuming\",\n /** Sent by client to acknowledge stream resuming notification and request chunks */\n CF_AGENT_STREAM_RESUME_ACK = \"cf_agent_stream_resume_ack\",\n /** Sent by client after message handler is ready, requesting stream resume check */\n CF_AGENT_STREAM_RESUME_REQUEST = \"cf_agent_stream_resume_request\",\n /** Sent by server when client requests resume but no active stream exists */\n CF_AGENT_STREAM_RESUME_NONE = \"cf_agent_stream_resume_none\",\n\n /** Client sends tool result to server (for client-side tools) */\n CF_AGENT_TOOL_RESULT = \"cf_agent_tool_result\",\n /** Server notifies client that a message was updated (e.g., tool result applied) */\n CF_AGENT_MESSAGE_UPDATED = \"cf_agent_message_updated\",\n /** Client sends tool approval response to server (for tools with needsApproval) */\n CF_AGENT_TOOL_APPROVAL = \"cf_agent_tool_approval\"\n}\n\n/**\n * Types of messages sent from the Agent to clients\n */\nexport type OutgoingMessage<ChatMessage extends UIMessage = UIMessage> =\n | {\n /** Indicates this message is a command to clear chat history */\n type: MessageType.CF_AGENT_CHAT_CLEAR;\n }\n | {\n /** Indicates this message contains updated chat messages */\n type: MessageType.CF_AGENT_CHAT_MESSAGES;\n /** Array of chat messages */\n messages: ChatMessage[];\n }\n | {\n /** Indicates this message is a response to a chat request */\n type: MessageType.CF_AGENT_USE_CHAT_RESPONSE;\n /** Unique ID of the request this response corresponds to */\n id: string;\n /** Content body of the response */\n body: string;\n /** Whether this is the final chunk of the response */\n done: boolean;\n /** Whether this response contains an error */\n error?: boolean;\n /** Whether this is a continuation (append to last assistant message) */\n continuation?: boolean;\n /** Whether this chunk is being replayed from storage (stream resumption) */\n replay?: boolean;\n /** Signals that replay of stored chunks is complete (stream is still active) */\n replayComplete?: boolean;\n }\n | {\n /** Indicates the server is resuming an active stream */\n type: MessageType.CF_AGENT_STREAM_RESUMING;\n /** The request ID of the stream being resumed */\n id: string;\n }\n | {\n /** Server notifies client that a message was updated (e.g., tool result applied) */\n type: MessageType.CF_AGENT_MESSAGE_UPDATED;\n /** The updated message */\n message: ChatMessage;\n }\n | {\n /** Server responds to resume request when no active stream exists */\n type: MessageType.CF_AGENT_STREAM_RESUME_NONE;\n };\n\n/**\n * Types of messages sent from clients to the Agent\n */\nexport type IncomingMessage<ChatMessage extends UIMessage = UIMessage> =\n | {\n /** Indicates this message is a command to clear chat history */\n type: MessageType.CF_AGENT_CHAT_CLEAR;\n }\n | {\n /** Indicates this message is a request to the chat API */\n type: MessageType.CF_AGENT_USE_CHAT_REQUEST;\n /** Unique ID for this request */\n id: string;\n /** Request initialization options */\n init: Pick<\n RequestInit,\n | \"method\"\n | \"keepalive\"\n | \"headers\"\n | \"body\"\n | \"redirect\"\n | \"integrity\"\n | \"credentials\"\n | \"mode\"\n | \"referrer\"\n | \"referrerPolicy\"\n | \"window\"\n >;\n }\n | {\n /** Indicates this message contains updated chat messages */\n type: MessageType.CF_AGENT_CHAT_MESSAGES;\n /** Array of chat messages */\n messages: ChatMessage[];\n }\n | {\n /** Indicates the user wants to stop generation of this message */\n type: MessageType.CF_AGENT_CHAT_REQUEST_CANCEL;\n id: string;\n }\n | {\n /** Client acknowledges stream resuming notification and is ready to receive chunks */\n type: MessageType.CF_AGENT_STREAM_RESUME_ACK;\n /** The request ID of the stream being resumed */\n id: string;\n }\n | {\n /** Client requests stream resume check after message handler is registered */\n type: MessageType.CF_AGENT_STREAM_RESUME_REQUEST;\n }\n | {\n /** Client sends tool result to server (for client-side tools) */\n type: MessageType.CF_AGENT_TOOL_RESULT;\n /** The tool call ID this result is for */\n toolCallId: string;\n /** The name of the tool */\n toolName: string;\n /** The output from the tool execution */\n output: unknown;\n /** Override the tool part state (e.g. \"output-error\" for custom denial) */\n state?: \"output-available\" | \"output-error\";\n /** Error message when state is \"output-error\" */\n errorText?: string;\n /** Whether server should auto-continue the conversation after applying result */\n autoContinue?: boolean;\n /** Client tool schemas for continuation (client is source of truth) */\n clientTools?: Array<{\n name: string;\n description?: string;\n parameters?: JSONSchema7;\n }>;\n }\n | {\n /** Client sends tool approval response to server (for tools with needsApproval) */\n type: MessageType.CF_AGENT_TOOL_APPROVAL;\n /** The tool call ID this approval is for */\n toolCallId: string;\n /** Whether the tool execution was approved */\n approved: boolean;\n /** Whether server should auto-continue the conversation after applying approval */\n autoContinue?: boolean;\n };\n"],"mappings":";;;;AAKA,IAAY,cAAL,yBAAA,aAAA;AACL,aAAA,4BAAA;AACA,aAAA,+BAAA;AACA,aAAA,gCAAA;AACA,aAAA,yBAAA;AACA,aAAA,kCAAA;;AAGA,aAAA,8BAAA;;AAEA,aAAA,gCAAA;;AAEA,aAAA,oCAAA;;AAEA,aAAA,iCAAA;;AAGA,aAAA,0BAAA;;AAEA,aAAA,8BAAA;;AAEA,aAAA,4BAAA;;KACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/ai-chat",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Cloudflare Agents (x) AI SDK Chat",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -21,14 +21,14 @@
21
21
  "devDependencies": {
22
22
  "@playwright/test": "^1.58.2",
23
23
  "@types/react": "^19.2.14",
24
- "ai": "^6.0.111",
24
+ "ai": "^6.0.116",
25
25
  "react": "^19.2.4",
26
26
  "vitest-browser-react": "^1.0.1",
27
27
  "zod": "^4.3.6"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@ai-sdk/react": "^3.0.0",
31
- "agents": "^0.7.3",
31
+ "agents": "^0.7.6",
32
32
  "ai": "^6.0.0",
33
33
  "react": "^19.0.0",
34
34
  "zod": "^3.25.0 || ^4.0.0"
@@ -1,126 +0,0 @@
1
- import { Connection } from "agents";
2
-
3
- //#region src/resumable-stream.d.ts
4
- /**
5
- * Minimal SQL interface matching Agent's this.sql tagged template.
6
- * Allows ResumableStream to work with the Agent's SQLite without
7
- * depending on the full Agent class.
8
- */
9
- type SqlTaggedTemplate = {
10
- <T = Record<string, unknown>>(
11
- strings: TemplateStringsArray,
12
- ...values: (string | number | boolean | null)[]
13
- ): T[];
14
- };
15
- declare class ResumableStream {
16
- private sql;
17
- private _activeStreamId;
18
- private _activeRequestId;
19
- private _streamChunkIndex;
20
- /**
21
- * Whether the active stream was started in this instance (true) or
22
- * restored from SQLite after hibernation/restart (false). An orphaned
23
- * stream has no live LLM reader — the ReadableStream was lost when the
24
- * DO was evicted.
25
- */
26
- private _isLive;
27
- private _chunkBuffer;
28
- private _isFlushingChunks;
29
- private _lastCleanupTime;
30
- constructor(sql: SqlTaggedTemplate);
31
- get activeStreamId(): string | null;
32
- get activeRequestId(): string | null;
33
- hasActiveStream(): boolean;
34
- /**
35
- * Whether the active stream has a live LLM reader (started in this
36
- * instance) vs being restored from SQLite after hibernation (orphaned).
37
- */
38
- get isLive(): boolean;
39
- /**
40
- * Start tracking a new stream for resumable streaming.
41
- * Creates metadata entry in SQLite and sets up tracking state.
42
- * @param requestId - The unique ID of the chat request
43
- * @returns The generated stream ID
44
- */
45
- start(requestId: string): string;
46
- /**
47
- * Mark a stream as completed and flush any pending chunks.
48
- * @param streamId - The stream to mark as completed
49
- */
50
- complete(streamId: string): void;
51
- /**
52
- * Mark a stream as errored and clean up state.
53
- * @param streamId - The stream to mark as errored
54
- */
55
- markError(streamId: string): void;
56
- /** Maximum chunk body size before skipping storage (bytes). Prevents SQLite row limit crash. */
57
- private static CHUNK_MAX_BYTES;
58
- /**
59
- * Buffer a stream chunk for batch write to SQLite.
60
- * Chunks exceeding the row size limit are skipped to prevent crashes.
61
- * The chunk is still broadcast to live clients (caller handles that),
62
- * but will be missing from replay on reconnection.
63
- * @param streamId - The stream this chunk belongs to
64
- * @param body - The serialized chunk body
65
- */
66
- storeChunk(streamId: string, body: string): void;
67
- /**
68
- * Flush buffered chunks to SQLite in a single batch.
69
- * Uses a lock to prevent concurrent flush operations.
70
- */
71
- flushBuffer(): void;
72
- /**
73
- * Send stored stream chunks to a connection for replay.
74
- * Chunks are marked with replay: true so the client can batch-apply them.
75
- *
76
- * Three outcomes:
77
- * - **Live stream**: sends chunks + `replayComplete` — client flushes and
78
- * continues receiving live chunks from the LLM reader.
79
- * - **Orphaned stream** (restored from SQLite after hibernation, no reader):
80
- * sends chunks + `done` and completes the stream. The caller should
81
- * reconstruct and persist the partial message from the stored chunks.
82
- * - **Completed during replay** (defensive): sends chunks + `done`.
83
- *
84
- * @param connection - The WebSocket connection
85
- * @param requestId - The original request ID
86
- * @returns The stream ID if the stream was orphaned and finalized, null otherwise.
87
- * When non-null the caller should reconstruct the message from chunks.
88
- */
89
- replayChunks(connection: Connection, requestId: string): string | null;
90
- /**
91
- * Restore active stream state if the agent was restarted during streaming.
92
- * Validates stream freshness to avoid sending stale resume notifications.
93
- */
94
- restore(): void;
95
- /**
96
- * Clear all stream data (called on chat history clear).
97
- */
98
- clearAll(): void;
99
- /**
100
- * Drop all stream tables (called on destroy).
101
- */
102
- destroy(): void;
103
- private _maybeCleanupOldStreams;
104
- /** @internal For testing only */
105
- getStreamChunks(streamId: string): Array<{
106
- body: string;
107
- chunk_index: number;
108
- }>;
109
- /** @internal For testing only */
110
- getStreamMetadata(streamId: string): {
111
- status: string;
112
- request_id: string;
113
- } | null;
114
- /** @internal For testing only */
115
- getAllStreamMetadata(): Array<{
116
- id: string;
117
- status: string;
118
- request_id: string;
119
- created_at: number;
120
- }>;
121
- /** @internal For testing only */
122
- insertStaleStream(streamId: string, requestId: string, ageMs: number): void;
123
- }
124
- //#endregion
125
- export { ResumableStream as t };
126
- //# sourceMappingURL=resumable-stream-DTLAk9XC.d.ts.map