@cloudflare/ai-chat 0.0.7 → 0.0.8
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/CHANGELOG.md +16 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +12 -7
- package/dist/index.js.map +1 -1
- package/dist/react.js +4 -1
- package/dist/react.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +29 -5
- package/src/react.tsx +11 -1
- package/src/tests/client-tools-continuation.test.ts +314 -0
- package/src/tests/custom-body.test.ts +186 -0
- package/src/tests/worker.ts +30 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @cloudflare/ai-chat
|
|
2
2
|
|
|
3
|
+
## 0.0.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#882](https://github.com/cloudflare/agents/pull/882) [`584cebe`](https://github.com/cloudflare/agents/commit/584cebe882f437a685b96b26b15200dc50ba70e1) Thanks [@alexanderjacobsen](https://github.com/alexanderjacobsen)! - Fix multi-step client tool calling: pass stored client tool schemas to `onChatMessage` during tool continuations so the LLM can call additional client tools after auto-continuation. Also add a re-trigger mechanism to the client-side tool resolution effect to handle tool calls arriving during active resolution.
|
|
8
|
+
|
|
9
|
+
- [#891](https://github.com/cloudflare/agents/pull/891) [`0723b99`](https://github.com/cloudflare/agents/commit/0723b9909f037d494e0c7db43e031c952578c82e) Thanks [@ask-bonk](https://github.com/apps/ask-bonk)! - Fix `getCurrentAgent()` returning `undefined` connection when used with `@cloudflare/ai-chat` and Vite SSR
|
|
10
|
+
|
|
11
|
+
Re-export `agentContext` as `__DO_NOT_USE_WILL_BREAK__agentContext` from the main `agents` entry point and update `@cloudflare/ai-chat` to import it from `agents` instead of the `agents/internal_context` subpath export. This prevents Vite SSR pre-bundling from creating two separate `AsyncLocalStorage` instances, which caused `getCurrentAgent().connection` to be `undefined` inside `onChatMessage` and tool `execute` functions.
|
|
12
|
+
|
|
13
|
+
The `agents/internal_context` subpath export has been removed from `package.json` and the deprecated `agentContext` alias has been removed from `internal_context.ts`. This was never a public API.
|
|
14
|
+
|
|
15
|
+
- [#886](https://github.com/cloudflare/agents/pull/886) [`4292f6b`](https://github.com/cloudflare/agents/commit/4292f6ba6d49201c88b09553452c3b243620f35b) Thanks [@whoiskatrin](https://github.com/whoiskatrin)! - Forward custom body fields from client requests to `onChatMessage` options
|
|
16
|
+
|
|
17
|
+
Custom data sent via `prepareSendMessagesRequest` or the AI SDK's `body` option in `sendMessage` is now available in the `onChatMessage` handler through `options.body`. This allows passing dynamic context (e.g., model selection, temperature, custom metadata) from the client to the server without workarounds.
|
|
18
|
+
|
|
3
19
|
## 0.0.7
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,14 @@ type OnChatMessageOptions = {
|
|
|
38
38
|
* in `useAgentChat` for client-side execution.
|
|
39
39
|
*/
|
|
40
40
|
clientTools?: ClientToolSchema[];
|
|
41
|
+
/**
|
|
42
|
+
* Custom body data sent from the client via `prepareSendMessagesRequest`
|
|
43
|
+
* or the AI SDK's `body` option in `sendMessage`.
|
|
44
|
+
*
|
|
45
|
+
* Contains all fields from the request body except `messages` and `clientTools`,
|
|
46
|
+
* which are handled separately.
|
|
47
|
+
*/
|
|
48
|
+
body?: Record<string, unknown>;
|
|
41
49
|
};
|
|
42
50
|
/**
|
|
43
51
|
* Converts client tool schemas to AI SDK tool format.
|
|
@@ -138,6 +146,12 @@ declare class AIChatAgent<
|
|
|
138
146
|
* @internal
|
|
139
147
|
*/
|
|
140
148
|
private _pendingResumeConnections;
|
|
149
|
+
/**
|
|
150
|
+
* Client tool schemas from the most recent chat request.
|
|
151
|
+
* Stored so they can be passed to onChatMessage during tool continuations.
|
|
152
|
+
* @internal
|
|
153
|
+
*/
|
|
154
|
+
private _lastClientTools;
|
|
141
155
|
/** Array of chat messages for the current conversation */
|
|
142
156
|
messages: UIMessage[];
|
|
143
157
|
constructor(ctx: AgentContext, env: Env);
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { MessageType } from "./types.js";
|
|
2
2
|
import { autoTransformMessages } from "./ai-chat-v5-migration.js";
|
|
3
3
|
import { jsonSchema, tool } from "ai";
|
|
4
|
-
import { Agent } from "agents";
|
|
4
|
+
import { Agent, __DO_NOT_USE_WILL_BREAK__agentContext } from "agents";
|
|
5
5
|
import { nanoid } from "nanoid";
|
|
6
|
-
import { agentContext } from "agents/internal_context";
|
|
7
6
|
|
|
8
7
|
//#region src/index.ts
|
|
9
8
|
/**
|
|
@@ -126,7 +125,8 @@ var AIChatAgent = class extends Agent {
|
|
|
126
125
|
}
|
|
127
126
|
if (data.type === MessageType.CF_AGENT_USE_CHAT_REQUEST && data.init.method === "POST") {
|
|
128
127
|
const { body } = data.init;
|
|
129
|
-
const { messages, clientTools } = JSON.parse(body);
|
|
128
|
+
const { messages, clientTools, ...customBody } = JSON.parse(body);
|
|
129
|
+
this._lastClientTools = clientTools?.length ? clientTools : void 0;
|
|
130
130
|
const transformedMessages = autoTransformMessages(messages);
|
|
131
131
|
this._broadcastChatMessage({
|
|
132
132
|
messages: transformedMessages,
|
|
@@ -143,7 +143,7 @@ var AIChatAgent = class extends Agent {
|
|
|
143
143
|
const chatMessageId = data.id;
|
|
144
144
|
const abortSignal = this._getAbortSignal(chatMessageId);
|
|
145
145
|
return this._tryCatchChat(async () => {
|
|
146
|
-
return
|
|
146
|
+
return __DO_NOT_USE_WILL_BREAK__agentContext.run({
|
|
147
147
|
agent: this,
|
|
148
148
|
connection,
|
|
149
149
|
request: void 0,
|
|
@@ -160,7 +160,8 @@ var AIChatAgent = class extends Agent {
|
|
|
160
160
|
}, this.ctx);
|
|
161
161
|
}, {
|
|
162
162
|
abortSignal,
|
|
163
|
-
clientTools
|
|
163
|
+
clientTools,
|
|
164
|
+
body: Object.keys(customBody).length > 0 ? customBody : void 0
|
|
164
165
|
});
|
|
165
166
|
if (response) await this._reply(data.id, response, [connection.id]);
|
|
166
167
|
else {
|
|
@@ -184,6 +185,7 @@ var AIChatAgent = class extends Agent {
|
|
|
184
185
|
this._activeRequestId = null;
|
|
185
186
|
this._streamChunkIndex = 0;
|
|
186
187
|
this._pendingResumeConnections.clear();
|
|
188
|
+
this._lastClientTools = void 0;
|
|
187
189
|
this.messages = [];
|
|
188
190
|
this._broadcastChatMessage({ type: MessageType.CF_AGENT_CHAT_CLEAR }, [connection.id]);
|
|
189
191
|
return;
|
|
@@ -214,7 +216,7 @@ var AIChatAgent = class extends Agent {
|
|
|
214
216
|
const continuationId = nanoid();
|
|
215
217
|
const abortSignal = this._getAbortSignal(continuationId);
|
|
216
218
|
this._tryCatchChat(async () => {
|
|
217
|
-
return
|
|
219
|
+
return __DO_NOT_USE_WILL_BREAK__agentContext.run({
|
|
218
220
|
agent: this,
|
|
219
221
|
connection,
|
|
220
222
|
request: void 0,
|
|
@@ -229,7 +231,10 @@ var AIChatAgent = class extends Agent {
|
|
|
229
231
|
timestamp: Date.now(),
|
|
230
232
|
type: "message:response"
|
|
231
233
|
}, this.ctx);
|
|
232
|
-
}, {
|
|
234
|
+
}, {
|
|
235
|
+
abortSignal,
|
|
236
|
+
clientTools: this._lastClientTools
|
|
237
|
+
});
|
|
233
238
|
if (response) await this._reply(continuationId, response, [], { continuation: true });
|
|
234
239
|
});
|
|
235
240
|
});
|