agents 0.0.0-74a8c74 → 0.0.0-75614c2
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/ai-chat-agent.d.ts +50 -5
- package/dist/ai-chat-agent.js +118 -51
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +17 -4
- package/dist/ai-react.js +62 -46
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +5 -0
- package/dist/chunk-767EASBA.js +106 -0
- package/dist/chunk-767EASBA.js.map +1 -0
- package/dist/{chunk-SZEXGW6W.js → chunk-CGWTDCBQ.js} +381 -170
- package/dist/chunk-CGWTDCBQ.js.map +1 -0
- package/dist/chunk-E3LCYPCB.js +469 -0
- package/dist/chunk-E3LCYPCB.js.map +1 -0
- package/dist/chunk-NKZZ66QY.js +116 -0
- package/dist/chunk-NKZZ66QY.js.map +1 -0
- package/dist/client.d.ts +15 -1
- package/dist/client.js +6 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +124 -20
- package/dist/index.js +8 -7
- package/dist/mcp/client.d.ts +142 -34
- package/dist/mcp/client.js +3 -780
- package/dist/mcp/client.js.map +1 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +41 -0
- package/dist/mcp/do-oauth-client-provider.js +7 -0
- package/dist/mcp/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/index.d.ts +50 -7
- package/dist/mcp/index.js +719 -71
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +85 -5
- package/dist/react.js +50 -29
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +2 -2
- package/dist/schedule.js +4 -4
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/dist/serializable.js.map +1 -0
- package/package.json +76 -53
- package/src/index.ts +431 -93
- package/dist/chunk-EZ76ZGDB.js +0 -1721
- package/dist/chunk-EZ76ZGDB.js.map +0 -1
- package/dist/chunk-SZEXGW6W.js.map +0 -1
package/dist/ai-chat-agent.d.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import { Agent, AgentContext } from "./index.js";
|
|
2
1
|
import { Message, StreamTextOnFinishCallback, ToolSet } from "ai";
|
|
2
|
+
import { Agent, AgentContext } from "./index.js";
|
|
3
3
|
import { Connection, WSMessage } from "partyserver";
|
|
4
|
-
import "
|
|
5
|
-
import "
|
|
4
|
+
import "@modelcontextprotocol/sdk/client/index.js";
|
|
5
|
+
import "@modelcontextprotocol/sdk/types.js";
|
|
6
|
+
import "./mcp/client.js";
|
|
7
|
+
import "zod";
|
|
8
|
+
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
9
|
+
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
10
|
+
import "./mcp/do-oauth-client-provider.js";
|
|
11
|
+
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
12
|
+
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
6
13
|
|
|
7
14
|
/**
|
|
8
15
|
* Extension of Agent with built-in chat capabilities
|
|
@@ -12,25 +19,63 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
12
19
|
Env,
|
|
13
20
|
State
|
|
14
21
|
> {
|
|
15
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Map of message `id`s to `AbortController`s
|
|
24
|
+
* useful to propagate request cancellation signals for any external calls made by the agent
|
|
25
|
+
*/
|
|
26
|
+
private _chatMessageAbortControllers;
|
|
16
27
|
/** Array of chat messages for the current conversation */
|
|
17
28
|
messages: Message[];
|
|
18
29
|
constructor(ctx: AgentContext, env: Env);
|
|
30
|
+
private _broadcastChatMessage;
|
|
19
31
|
onMessage(connection: Connection, message: WSMessage): Promise<void>;
|
|
20
32
|
onRequest(request: Request): Promise<Response>;
|
|
33
|
+
private _tryCatchChat;
|
|
21
34
|
/**
|
|
22
35
|
* Handle incoming chat messages and generate a response
|
|
23
36
|
* @param onFinish Callback to be called when the response is finished
|
|
37
|
+
* @param options.signal A signal to pass to any child requests which can be used to cancel them
|
|
24
38
|
* @returns Response to send to the client or undefined
|
|
25
39
|
*/
|
|
26
40
|
onChatMessage(
|
|
27
|
-
onFinish: StreamTextOnFinishCallback<ToolSet
|
|
41
|
+
onFinish: StreamTextOnFinishCallback<ToolSet>,
|
|
42
|
+
options?: {
|
|
43
|
+
abortSignal: AbortSignal | undefined;
|
|
44
|
+
}
|
|
28
45
|
): Promise<Response | undefined>;
|
|
29
46
|
/**
|
|
30
47
|
* Save messages on the server side and trigger AI response
|
|
31
48
|
* @param messages Chat messages to save
|
|
32
49
|
*/
|
|
33
50
|
saveMessages(messages: Message[]): Promise<void>;
|
|
51
|
+
persistMessages(
|
|
52
|
+
messages: Message[],
|
|
53
|
+
excludeBroadcastIds?: string[]
|
|
54
|
+
): Promise<void>;
|
|
55
|
+
private _reply;
|
|
56
|
+
/**
|
|
57
|
+
* For the given message id, look up its associated AbortController
|
|
58
|
+
* If the AbortController does not exist, create and store one in memory
|
|
59
|
+
*
|
|
60
|
+
* returns the AbortSignal associated with the AbortController
|
|
61
|
+
*/
|
|
62
|
+
private _getAbortSignal;
|
|
63
|
+
/**
|
|
64
|
+
* Remove an abort controller from the cache of pending message responses
|
|
65
|
+
*/
|
|
66
|
+
private _removeAbortController;
|
|
67
|
+
/**
|
|
68
|
+
* Propagate an abort signal for any requests associated with the given message id
|
|
69
|
+
*/
|
|
70
|
+
private _cancelChatRequest;
|
|
71
|
+
/**
|
|
72
|
+
* Abort all pending requests and clear the cache of AbortControllers
|
|
73
|
+
*/
|
|
74
|
+
private _destroyAbortControllers;
|
|
75
|
+
/**
|
|
76
|
+
* When the DO is destroyed, cancel all pending requests
|
|
77
|
+
*/
|
|
78
|
+
destroy(): Promise<void>;
|
|
34
79
|
}
|
|
35
80
|
|
|
36
81
|
export { AIChatAgent };
|
package/dist/ai-chat-agent.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-CGWTDCBQ.js";
|
|
4
|
+
import "./chunk-E3LCYPCB.js";
|
|
5
|
+
import "./chunk-767EASBA.js";
|
|
6
|
+
import "./chunk-NKZZ66QY.js";
|
|
4
7
|
|
|
5
8
|
// src/ai-chat-agent.ts
|
|
6
9
|
import { appendResponseMessages } from "ai";
|
|
7
10
|
var decoder = new TextDecoder();
|
|
8
11
|
var AIChatAgent = class extends Agent {
|
|
9
|
-
/** Array of chat messages for the current conversation */
|
|
10
|
-
messages;
|
|
11
12
|
constructor(ctx, env) {
|
|
12
13
|
super(ctx, env);
|
|
13
14
|
this.sql`create table if not exists cf_ai_chat_agent_messages (
|
|
@@ -18,8 +19,9 @@ var AIChatAgent = class extends Agent {
|
|
|
18
19
|
this.messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
|
|
19
20
|
return JSON.parse(row.message);
|
|
20
21
|
});
|
|
22
|
+
this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
|
|
21
23
|
}
|
|
22
|
-
|
|
24
|
+
_broadcastChatMessage(message, exclude) {
|
|
23
25
|
this.broadcast(JSON.stringify(message), exclude);
|
|
24
26
|
}
|
|
25
27
|
async onMessage(connection, message) {
|
|
@@ -27,65 +29,83 @@ var AIChatAgent = class extends Agent {
|
|
|
27
29
|
let data;
|
|
28
30
|
try {
|
|
29
31
|
data = JSON.parse(message);
|
|
30
|
-
} catch (
|
|
32
|
+
} catch (_error) {
|
|
31
33
|
return;
|
|
32
34
|
}
|
|
33
35
|
if (data.type === "cf_agent_use_chat_request" && data.init.method === "POST") {
|
|
34
36
|
const {
|
|
35
|
-
method,
|
|
36
|
-
keepalive,
|
|
37
|
-
headers,
|
|
38
|
-
body
|
|
37
|
+
// method,
|
|
38
|
+
// keepalive,
|
|
39
|
+
// headers,
|
|
40
|
+
body
|
|
39
41
|
// we're reading this
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
credentials,
|
|
43
|
-
mode,
|
|
44
|
-
referrer,
|
|
45
|
-
referrerPolicy,
|
|
46
|
-
window
|
|
42
|
+
//
|
|
43
|
+
// // these might not exist?
|
|
47
44
|
// dispatcher,
|
|
48
45
|
// duplex
|
|
49
46
|
} = data.init;
|
|
50
47
|
const { messages } = JSON.parse(body);
|
|
51
|
-
this
|
|
48
|
+
this._broadcastChatMessage(
|
|
52
49
|
{
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
messages,
|
|
51
|
+
type: "cf_agent_chat_messages"
|
|
55
52
|
},
|
|
56
53
|
[connection.id]
|
|
57
54
|
);
|
|
58
|
-
await this
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
55
|
+
await this.persistMessages(messages, [connection.id]);
|
|
56
|
+
const chatMessageId = data.id;
|
|
57
|
+
const abortSignal = this._getAbortSignal(chatMessageId);
|
|
58
|
+
return this._tryCatchChat(async () => {
|
|
59
|
+
const response = await this.onChatMessage(
|
|
60
|
+
async ({ response: response2 }) => {
|
|
61
|
+
const finalMessages = appendResponseMessages({
|
|
62
|
+
messages,
|
|
63
|
+
responseMessages: response2.messages
|
|
64
|
+
});
|
|
65
|
+
await this.persistMessages(finalMessages, [connection.id]);
|
|
66
|
+
this._removeAbortController(chatMessageId);
|
|
67
|
+
},
|
|
68
|
+
abortSignal ? { abortSignal } : void 0
|
|
69
|
+
);
|
|
67
70
|
if (response) {
|
|
68
|
-
await this
|
|
71
|
+
await this._reply(data.id, response);
|
|
72
|
+
} else {
|
|
73
|
+
console.warn(
|
|
74
|
+
`[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`
|
|
75
|
+
);
|
|
76
|
+
this._broadcastChatMessage(
|
|
77
|
+
{
|
|
78
|
+
body: "No response was generated by the agent.",
|
|
79
|
+
done: true,
|
|
80
|
+
id: data.id,
|
|
81
|
+
type: "cf_agent_use_chat_response"
|
|
82
|
+
},
|
|
83
|
+
[connection.id]
|
|
84
|
+
);
|
|
69
85
|
}
|
|
70
86
|
});
|
|
71
87
|
}
|
|
72
88
|
if (data.type === "cf_agent_chat_clear") {
|
|
89
|
+
this._destroyAbortControllers();
|
|
73
90
|
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
74
91
|
this.messages = [];
|
|
75
|
-
this
|
|
92
|
+
this._broadcastChatMessage(
|
|
76
93
|
{
|
|
77
94
|
type: "cf_agent_chat_clear"
|
|
78
95
|
},
|
|
79
96
|
[connection.id]
|
|
80
97
|
);
|
|
81
98
|
} else if (data.type === "cf_agent_chat_messages") {
|
|
82
|
-
await this
|
|
99
|
+
await this.persistMessages(data.messages, [connection.id]);
|
|
100
|
+
} else if (data.type === "cf_agent_chat_request_cancel") {
|
|
101
|
+
this._cancelChatRequest(data.id);
|
|
83
102
|
}
|
|
84
103
|
}
|
|
85
104
|
}
|
|
86
105
|
async onRequest(request) {
|
|
87
|
-
return this
|
|
88
|
-
|
|
106
|
+
return this._tryCatchChat(() => {
|
|
107
|
+
const url = new URL(request.url);
|
|
108
|
+
if (url.pathname.endsWith("/get-messages")) {
|
|
89
109
|
const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
|
|
90
110
|
return JSON.parse(row.message);
|
|
91
111
|
});
|
|
@@ -94,7 +114,7 @@ var AIChatAgent = class extends Agent {
|
|
|
94
114
|
return super.onRequest(request);
|
|
95
115
|
});
|
|
96
116
|
}
|
|
97
|
-
async
|
|
117
|
+
async _tryCatchChat(fn) {
|
|
98
118
|
try {
|
|
99
119
|
return await fn();
|
|
100
120
|
} catch (e) {
|
|
@@ -104,9 +124,10 @@ var AIChatAgent = class extends Agent {
|
|
|
104
124
|
/**
|
|
105
125
|
* Handle incoming chat messages and generate a response
|
|
106
126
|
* @param onFinish Callback to be called when the response is finished
|
|
127
|
+
* @param options.signal A signal to pass to any child requests which can be used to cancel them
|
|
107
128
|
* @returns Response to send to the client or undefined
|
|
108
129
|
*/
|
|
109
|
-
async onChatMessage(onFinish) {
|
|
130
|
+
async onChatMessage(onFinish, options) {
|
|
110
131
|
throw new Error(
|
|
111
132
|
"recieved a chat message, override onChatMessage and return a Response to send to the client"
|
|
112
133
|
);
|
|
@@ -116,13 +137,13 @@ var AIChatAgent = class extends Agent {
|
|
|
116
137
|
* @param messages Chat messages to save
|
|
117
138
|
*/
|
|
118
139
|
async saveMessages(messages) {
|
|
119
|
-
await this
|
|
140
|
+
await this.persistMessages(messages);
|
|
120
141
|
const response = await this.onChatMessage(async ({ response: response2 }) => {
|
|
121
142
|
const finalMessages = appendResponseMessages({
|
|
122
143
|
messages,
|
|
123
144
|
responseMessages: response2.messages
|
|
124
145
|
});
|
|
125
|
-
await this
|
|
146
|
+
await this.persistMessages(finalMessages, []);
|
|
126
147
|
});
|
|
127
148
|
if (response) {
|
|
128
149
|
for await (const chunk of response.body) {
|
|
@@ -131,39 +152,85 @@ var AIChatAgent = class extends Agent {
|
|
|
131
152
|
response.body?.cancel();
|
|
132
153
|
}
|
|
133
154
|
}
|
|
134
|
-
async
|
|
155
|
+
async persistMessages(messages, excludeBroadcastIds = []) {
|
|
135
156
|
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
136
157
|
for (const message of messages) {
|
|
137
158
|
this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
|
|
138
159
|
}
|
|
139
160
|
this.messages = messages;
|
|
140
|
-
this
|
|
161
|
+
this._broadcastChatMessage(
|
|
141
162
|
{
|
|
142
|
-
|
|
143
|
-
|
|
163
|
+
messages,
|
|
164
|
+
type: "cf_agent_chat_messages"
|
|
144
165
|
},
|
|
145
166
|
excludeBroadcastIds
|
|
146
167
|
);
|
|
147
168
|
}
|
|
148
|
-
async
|
|
149
|
-
return this
|
|
169
|
+
async _reply(id, response) {
|
|
170
|
+
return this._tryCatchChat(async () => {
|
|
150
171
|
for await (const chunk of response.body) {
|
|
151
172
|
const body = decoder.decode(chunk);
|
|
152
|
-
this
|
|
153
|
-
id,
|
|
154
|
-
type: "cf_agent_use_chat_response",
|
|
173
|
+
this._broadcastChatMessage({
|
|
155
174
|
body,
|
|
156
|
-
done: false
|
|
175
|
+
done: false,
|
|
176
|
+
id,
|
|
177
|
+
type: "cf_agent_use_chat_response"
|
|
157
178
|
});
|
|
158
179
|
}
|
|
159
|
-
this
|
|
160
|
-
id,
|
|
161
|
-
type: "cf_agent_use_chat_response",
|
|
180
|
+
this._broadcastChatMessage({
|
|
162
181
|
body: "",
|
|
163
|
-
done: true
|
|
182
|
+
done: true,
|
|
183
|
+
id,
|
|
184
|
+
type: "cf_agent_use_chat_response"
|
|
164
185
|
});
|
|
165
186
|
});
|
|
166
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* For the given message id, look up its associated AbortController
|
|
190
|
+
* If the AbortController does not exist, create and store one in memory
|
|
191
|
+
*
|
|
192
|
+
* returns the AbortSignal associated with the AbortController
|
|
193
|
+
*/
|
|
194
|
+
_getAbortSignal(id) {
|
|
195
|
+
if (typeof id !== "string") {
|
|
196
|
+
return void 0;
|
|
197
|
+
}
|
|
198
|
+
if (!this._chatMessageAbortControllers.has(id)) {
|
|
199
|
+
this._chatMessageAbortControllers.set(id, new AbortController());
|
|
200
|
+
}
|
|
201
|
+
return this._chatMessageAbortControllers.get(id)?.signal;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Remove an abort controller from the cache of pending message responses
|
|
205
|
+
*/
|
|
206
|
+
_removeAbortController(id) {
|
|
207
|
+
this._chatMessageAbortControllers.delete(id);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Propagate an abort signal for any requests associated with the given message id
|
|
211
|
+
*/
|
|
212
|
+
_cancelChatRequest(id) {
|
|
213
|
+
if (this._chatMessageAbortControllers.has(id)) {
|
|
214
|
+
const abortController = this._chatMessageAbortControllers.get(id);
|
|
215
|
+
abortController?.abort();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Abort all pending requests and clear the cache of AbortControllers
|
|
220
|
+
*/
|
|
221
|
+
_destroyAbortControllers() {
|
|
222
|
+
for (const controller of this._chatMessageAbortControllers.values()) {
|
|
223
|
+
controller?.abort();
|
|
224
|
+
}
|
|
225
|
+
this._chatMessageAbortControllers.clear();
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* When the DO is destroyed, cancel all pending requests
|
|
229
|
+
*/
|
|
230
|
+
async destroy() {
|
|
231
|
+
this._destroyAbortControllers();
|
|
232
|
+
await super.destroy();
|
|
233
|
+
}
|
|
167
234
|
};
|
|
168
235
|
export {
|
|
169
236
|
AIChatAgent
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ai-chat-agent.ts"],"sourcesContent":["import { Agent, type AgentContext, type Connection, type WSMessage } from \"./\";\nimport type {\n Message as ChatMessage,\n StreamTextOnFinishCallback,\n ToolSet,\n} from \"ai\";\nimport { appendResponseMessages } from \"ai\";\nimport type { OutgoingMessage, IncomingMessage } from \"./ai-types\";\nconst decoder = new TextDecoder();\n\n/**\n * Extension of Agent with built-in chat capabilities\n * @template Env Environment type containing bindings\n */\nexport class AIChatAgent<Env = unknown, State = unknown> extends Agent<\n Env,\n State\n> {\n /** Array of chat messages for the current conversation */\n messages: ChatMessage[];\n constructor(ctx: AgentContext, env: Env) {\n super(ctx, env);\n this.sql`create table if not exists cf_ai_chat_agent_messages (\n id text primary key,\n message text not null,\n created_at datetime default current_timestamp\n )`;\n this.messages = (\n this.sql`select * from cf_ai_chat_agent_messages` || []\n ).map((row) => {\n return JSON.parse(row.message as string);\n });\n }\n\n #broadcastChatMessage(message: OutgoingMessage, exclude?: string[]) {\n this.broadcast(JSON.stringify(message), exclude);\n }\n\n override async onMessage(connection: Connection, message: WSMessage) {\n if (typeof message === \"string\") {\n let data: IncomingMessage;\n try {\n data = JSON.parse(message) as IncomingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (\n data.type === \"cf_agent_use_chat_request\" &&\n data.init.method === \"POST\"\n ) {\n const {\n method,\n keepalive,\n headers,\n body, // we're reading this\n redirect,\n integrity,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher,\n // duplex\n } = data.init;\n const { messages } = JSON.parse(body as string);\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_messages\",\n messages,\n },\n [connection.id]\n );\n await this.#persistMessages(messages, [connection.id]);\n return this.#tryCatch(async () => {\n const response = await this.onChatMessage(async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages,\n });\n\n await this.#persistMessages(finalMessages, [connection.id]);\n });\n if (response) {\n await this.#reply(data.id, response);\n }\n });\n }\n if (data.type === \"cf_agent_chat_clear\") {\n this.sql`delete from cf_ai_chat_agent_messages`;\n this.messages = [];\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_clear\",\n },\n [connection.id]\n );\n } else if (data.type === \"cf_agent_chat_messages\") {\n // replace the messages with the new ones\n await this.#persistMessages(data.messages, [connection.id]);\n }\n }\n }\n\n override async onRequest(request: Request): Promise<Response> {\n return this.#tryCatch(() => {\n if (request.url.endsWith(\"/get-messages\")) {\n const messages = (\n this.sql`select * from cf_ai_chat_agent_messages` || []\n ).map((row) => {\n return JSON.parse(row.message as string);\n });\n return Response.json(messages);\n }\n return super.onRequest(request);\n });\n }\n\n async #tryCatch<T>(fn: () => T | Promise<T>) {\n try {\n return await fn();\n } catch (e) {\n throw this.onError(e);\n }\n }\n\n /**\n * Handle incoming chat messages and generate a response\n * @param onFinish Callback to be called when the response is finished\n * @returns Response to send to the client or undefined\n */\n async onChatMessage(\n onFinish: StreamTextOnFinishCallback<ToolSet>\n ): Promise<Response | undefined> {\n throw new Error(\n \"recieved a chat message, override onChatMessage and return a Response to send to the client\"\n );\n }\n\n /**\n * Save messages on the server side and trigger AI response\n * @param messages Chat messages to save\n */\n async saveMessages(messages: ChatMessage[]) {\n await this.#persistMessages(messages);\n const response = await this.onChatMessage(async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages,\n });\n\n await this.#persistMessages(finalMessages, []);\n });\n if (response) {\n // we're just going to drain the body\n // @ts-ignore TODO: fix this type error\n for await (const chunk of response.body!) {\n decoder.decode(chunk);\n }\n response.body?.cancel();\n }\n }\n\n async #persistMessages(\n messages: ChatMessage[],\n excludeBroadcastIds: string[] = []\n ) {\n this.sql`delete from cf_ai_chat_agent_messages`;\n for (const message of messages) {\n this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${\n message.id\n },${JSON.stringify(message)})`;\n }\n this.messages = messages;\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_messages\",\n messages: messages,\n },\n excludeBroadcastIds\n );\n }\n\n async #reply(id: string, response: Response) {\n // now take chunks out from dataStreamResponse and send them to the client\n return this.#tryCatch(async () => {\n // @ts-expect-error TODO: fix this type error\n for await (const chunk of response.body!) {\n const body = decoder.decode(chunk);\n\n this.#broadcastChatMessage({\n id,\n type: \"cf_agent_use_chat_response\",\n body,\n done: false,\n });\n }\n\n this.#broadcastChatMessage({\n id,\n type: \"cf_agent_use_chat_response\",\n body: \"\",\n done: true,\n });\n });\n }\n}\n"],"mappings":";;;;;AAMA,SAAS,8BAA8B;AAEvC,IAAM,UAAU,IAAI,YAAY;AAMzB,IAAM,cAAN,cAA0D,MAG/D;AAAA;AAAA,EAEA;AAAA,EACA,YAAY,KAAmB,KAAU;AACvC,UAAM,KAAK,GAAG;AACd,SAAK;AAAA;AAAA;AAAA;AAAA;AAKL,SAAK,YACH,KAAK,gDAAgD,CAAC,GACtD,IAAI,CAAC,QAAQ;AACb,aAAO,KAAK,MAAM,IAAI,OAAiB;AAAA,IACzC,CAAC;AAAA,EACH;AAAA,EAEA,sBAAsB,SAA0B,SAAoB;AAClE,SAAK,UAAU,KAAK,UAAU,OAAO,GAAG,OAAO;AAAA,EACjD;AAAA,EAEA,MAAe,UAAU,YAAwB,SAAo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A,QACN;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,OAAO,IAAY,UAAoB;AAE3C,WAAO,KAAK,UAAU,YAAY;AAEhC,uBAAiB,SAAS,SAAS,MAAO;AACxC,cAAM,OAAO,QAAQ,OAAO,KAAK;AAEjC,aAAK,sBAAsB;AAAA,UACzB;AAAA,UACA,MAAM;AAAA,UACN;AAAA,UACA,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAEA,WAAK,sBAAsB;AAAA,QACzB;AAAA,QACA,MAAM;AAAA,QACN,MAAM;AAAA,QACN,MAAM;AAAA,MACR,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":["response"]}
|
|
1
|
+
{"version":3,"sources":["../src/ai-chat-agent.ts"],"sourcesContent":["import type {\n Message as ChatMessage,\n StreamTextOnFinishCallback,\n ToolSet,\n} from \"ai\";\nimport { appendResponseMessages } from \"ai\";\nimport { Agent, type AgentContext, type Connection, type WSMessage } from \"./\";\nimport type { IncomingMessage, OutgoingMessage } from \"./ai-types\";\n\nconst decoder = new TextDecoder();\n\n/**\n * Extension of Agent with built-in chat capabilities\n * @template Env Environment type containing bindings\n */\nexport class AIChatAgent<Env = unknown, State = unknown> extends Agent<\n Env,\n State\n> {\n /**\n * Map of message `id`s to `AbortController`s\n * useful to propagate request cancellation signals for any external calls made by the agent\n */\n private _chatMessageAbortControllers: Map<string, AbortController>;\n /** Array of chat messages for the current conversation */\n messages: ChatMessage[];\n constructor(ctx: AgentContext, env: Env) {\n super(ctx, env);\n this.sql`create table if not exists cf_ai_chat_agent_messages (\n id text primary key,\n message text not null,\n created_at datetime default current_timestamp\n )`;\n this.messages = (\n this.sql`select * from cf_ai_chat_agent_messages` || []\n ).map((row) => {\n return JSON.parse(row.message as string);\n });\n\n this._chatMessageAbortControllers = new Map();\n }\n\n private _broadcastChatMessage(message: OutgoingMessage, exclude?: string[]) {\n this.broadcast(JSON.stringify(message), exclude);\n }\n\n override async onMessage(connection: Connection, message: WSMessage) {\n if (typeof message === \"string\") {\n let data: IncomingMessage;\n try {\n data = JSON.parse(message) as IncomingMessage;\n } catch (_error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (\n data.type === \"cf_agent_use_chat_request\" &&\n data.init.method === \"POST\"\n ) {\n const {\n // method,\n // keepalive,\n // headers,\n body, // we're reading this\n //\n // // these might not exist?\n // dispatcher,\n // duplex\n } = data.init;\n const { messages } = JSON.parse(body as string);\n this._broadcastChatMessage(\n {\n messages,\n type: \"cf_agent_chat_messages\",\n },\n [connection.id]\n );\n await this.persistMessages(messages, [connection.id]);\n\n const chatMessageId = data.id;\n const abortSignal = this._getAbortSignal(chatMessageId);\n\n return this._tryCatchChat(async () => {\n const response = await this.onChatMessage(\n async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages,\n });\n\n await this.persistMessages(finalMessages, [connection.id]);\n this._removeAbortController(chatMessageId);\n },\n abortSignal ? { abortSignal } : undefined\n );\n\n if (response) {\n await this._reply(data.id, response);\n } else {\n // Log a warning for observability\n console.warn(\n `[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`\n );\n // Send a fallback message to the client\n this._broadcastChatMessage(\n {\n body: \"No response was generated by the agent.\",\n done: true,\n id: data.id,\n type: \"cf_agent_use_chat_response\",\n },\n [connection.id]\n );\n }\n });\n }\n if (data.type === \"cf_agent_chat_clear\") {\n this._destroyAbortControllers();\n this.sql`delete from cf_ai_chat_agent_messages`;\n this.messages = [];\n this._broadcastChatMessage(\n {\n type: \"cf_agent_chat_clear\",\n },\n [connection.id]\n );\n } else if (data.type === \"cf_agent_chat_messages\") {\n // replace the messages with the new ones\n await this.persistMessages(data.messages, [connection.id]);\n } else if (data.type === \"cf_agent_chat_request_cancel\") {\n // propagate an abort signal for the associated request\n this._cancelChatRequest(data.id);\n }\n }\n }\n\n override async onRequest(request: Request): Promise<Response> {\n return this._tryCatchChat(() => {\n const url = new URL(request.url);\n if (url.pathname.endsWith(\"/get-messages\")) {\n const messages = (\n this.sql`select * from cf_ai_chat_agent_messages` || []\n ).map((row) => {\n return JSON.parse(row.message as string);\n });\n return Response.json(messages);\n }\n return super.onRequest(request);\n });\n }\n\n private async _tryCatchChat<T>(fn: () => T | Promise<T>) {\n try {\n return await fn();\n } catch (e) {\n throw this.onError(e);\n }\n }\n\n /**\n * Handle incoming chat messages and generate a response\n * @param onFinish Callback to be called when the response is finished\n * @param options.signal A signal to pass to any child requests which can be used to cancel them\n * @returns Response to send to the client or undefined\n */\n async onChatMessage(\n // biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later\n onFinish: StreamTextOnFinishCallback<ToolSet>,\n // biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later\n options?: { abortSignal: AbortSignal | undefined }\n ): Promise<Response | undefined> {\n throw new Error(\n \"recieved a chat message, override onChatMessage and return a Response to send to the client\"\n );\n }\n\n /**\n * Save messages on the server side and trigger AI response\n * @param messages Chat messages to save\n */\n async saveMessages(messages: ChatMessage[]) {\n await this.persistMessages(messages);\n const response = await this.onChatMessage(async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages,\n });\n\n await this.persistMessages(finalMessages, []);\n });\n if (response) {\n // we're just going to drain the body\n // @ts-ignore TODO: fix this type error\n for await (const chunk of response.body!) {\n decoder.decode(chunk);\n }\n response.body?.cancel();\n }\n }\n\n async persistMessages(\n messages: ChatMessage[],\n excludeBroadcastIds: string[] = []\n ) {\n this.sql`delete from cf_ai_chat_agent_messages`;\n for (const message of messages) {\n this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${\n message.id\n },${JSON.stringify(message)})`;\n }\n this.messages = messages;\n this._broadcastChatMessage(\n {\n messages: messages,\n type: \"cf_agent_chat_messages\",\n },\n excludeBroadcastIds\n );\n }\n\n private async _reply(id: string, response: Response) {\n // now take chunks out from dataStreamResponse and send them to the client\n return this._tryCatchChat(async () => {\n // @ts-expect-error TODO: fix this type error\n for await (const chunk of response.body!) {\n const body = decoder.decode(chunk);\n\n this._broadcastChatMessage({\n body,\n done: false,\n id,\n type: \"cf_agent_use_chat_response\",\n });\n }\n\n this._broadcastChatMessage({\n body: \"\",\n done: true,\n id,\n type: \"cf_agent_use_chat_response\",\n });\n });\n }\n\n /**\n * For the given message id, look up its associated AbortController\n * If the AbortController does not exist, create and store one in memory\n *\n * returns the AbortSignal associated with the AbortController\n */\n private _getAbortSignal(id: string): AbortSignal | undefined {\n // Defensive check, since we're coercing message types at the moment\n if (typeof id !== \"string\") {\n return undefined;\n }\n\n if (!this._chatMessageAbortControllers.has(id)) {\n this._chatMessageAbortControllers.set(id, new AbortController());\n }\n\n return this._chatMessageAbortControllers.get(id)?.signal;\n }\n\n /**\n * Remove an abort controller from the cache of pending message responses\n */\n private _removeAbortController(id: string) {\n this._chatMessageAbortControllers.delete(id);\n }\n\n /**\n * Propagate an abort signal for any requests associated with the given message id\n */\n private _cancelChatRequest(id: string) {\n if (this._chatMessageAbortControllers.has(id)) {\n const abortController = this._chatMessageAbortControllers.get(id);\n abortController?.abort();\n }\n }\n\n /**\n * Abort all pending requests and clear the cache of AbortControllers\n */\n private _destroyAbortControllers() {\n for (const controller of this._chatMessageAbortControllers.values()) {\n controller?.abort();\n }\n this._chatMessageAbortControllers.clear();\n }\n\n /**\n * When the DO is destroyed, cancel all pending requests\n */\n async destroy() {\n this._destroyAbortControllers();\n await super.destroy();\n }\n}\n"],"mappings":";;;;;;;;AAKA,SAAS,8BAA8B;AAIvC,IAAM,UAAU,IAAI,YAAY;AAMzB,IAAM,cAAN,cAA0D,MAG/D;AAAA,EAQA,YAAY,KAAmB,KAAU;AACvC,UAAM,KAAK,GAAG;AACd,SAAK;AAAA;AAAA;AAAA;AAAA;AAKL,SAAK,YACH,KAAK,gDAAgD,CAAC,GACtD,IAAI,CAAC,QAAQ;AACb,aAAO,KAAK,MAAM,IAAI,OAAiB;AAAA,IACzC,CAAC;AAED,SAAK,+BAA+B,oBAAI,I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gBAAgB,CAAC;AAAA,IACjE;AAEA,WAAO,KAAK,6BAA6B,IAAI,EAAE,GAAG;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA,EAKQ,uBAAuB,IAAY;AACzC,SAAK,6BAA6B,OAAO,EAAE;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA,EAKQ,mBAAmB,IAAY;AACrC,QAAI,KAAK,6BAA6B,IAAI,EAAE,GAAG;AAC7C,YAAM,kBAAkB,KAAK,6BAA6B,IAAI,EAAE;AAChE,uBAAiB,MAAM;AAAA,IACzB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,2BAA2B;AACjC,eAAW,cAAc,KAAK,6BAA6B,OAAO,GAAG;AACnE,kBAAY,MAAM;AAAA,IACpB;AACA,SAAK,6BAA6B,MAAM;AAAA,EAC1C;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,UAAU;AACd,SAAK,yBAAyB;AAC9B,UAAM,MAAM,QAAQ;AAAA,EACtB;AACF;","names":["response"]}
|
package/dist/ai-react.d.ts
CHANGED
|
@@ -4,7 +4,19 @@ import { useChat } from "@ai-sdk/react";
|
|
|
4
4
|
import { useAgent } from "./react.js";
|
|
5
5
|
import "partysocket";
|
|
6
6
|
import "partysocket/react";
|
|
7
|
+
import "./index.js";
|
|
8
|
+
import "@modelcontextprotocol/sdk/client/index.js";
|
|
9
|
+
import "@modelcontextprotocol/sdk/types.js";
|
|
10
|
+
import "partyserver";
|
|
11
|
+
import "./mcp/client.js";
|
|
12
|
+
import "zod";
|
|
13
|
+
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
14
|
+
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
15
|
+
import "./mcp/do-oauth-client-provider.js";
|
|
16
|
+
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
17
|
+
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
7
18
|
import "./client.js";
|
|
19
|
+
import "./serializable.js";
|
|
8
20
|
|
|
9
21
|
type GetInitialMessagesOptions = {
|
|
10
22
|
agent: string;
|
|
@@ -33,15 +45,15 @@ type UseAgentChatOptions<State> = Omit<
|
|
|
33
45
|
declare function useAgentChat<State = unknown>(
|
|
34
46
|
options: UseAgentChatOptions<State>
|
|
35
47
|
): {
|
|
48
|
+
/**
|
|
49
|
+
* Clear chat history on both client and Agent
|
|
50
|
+
*/
|
|
51
|
+
clearHistory: () => void;
|
|
36
52
|
/**
|
|
37
53
|
* Set the chat messages and synchronize with the Agent
|
|
38
54
|
* @param messages New messages to set
|
|
39
55
|
*/
|
|
40
56
|
setMessages: (messages: Message[]) => void;
|
|
41
|
-
/**
|
|
42
|
-
* Clear chat history on both client and Agent
|
|
43
|
-
*/
|
|
44
|
-
clearHistory: () => void;
|
|
45
57
|
messages: ai.UIMessage[];
|
|
46
58
|
error: undefined | Error;
|
|
47
59
|
append: (
|
|
@@ -52,6 +64,7 @@ declare function useAgentChat<State = unknown>(
|
|
|
52
64
|
chatRequestOptions?: ai.ChatRequestOptions
|
|
53
65
|
) => Promise<string | null | undefined>;
|
|
54
66
|
stop: () => void;
|
|
67
|
+
experimental_resume: () => void;
|
|
55
68
|
input: string;
|
|
56
69
|
setInput: React.Dispatch<React.SetStateAction<string>>;
|
|
57
70
|
handleInputChange: (
|
package/dist/ai-react.js
CHANGED
|
@@ -1,40 +1,53 @@
|
|
|
1
1
|
// src/ai-react.tsx
|
|
2
2
|
import { useChat } from "@ai-sdk/react";
|
|
3
|
-
import {
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
import { use, useEffect } from "react";
|
|
4
5
|
var requestCache = /* @__PURE__ */ new Map();
|
|
5
6
|
function useAgentChat(options) {
|
|
6
7
|
const { agent, getInitialMessages, ...rest } = options;
|
|
7
|
-
const
|
|
8
|
+
const agentUrl = new URL(
|
|
9
|
+
`${// @ts-expect-error we're using a protected _url property that includes query params
|
|
10
|
+
(agent._url || agent._pkurl)?.replace("ws://", "http://").replace("wss://", "https://")}`
|
|
11
|
+
);
|
|
12
|
+
agentUrl.searchParams.delete("_pk");
|
|
13
|
+
const agentUrlString = agentUrl.toString();
|
|
8
14
|
async function defaultGetInitialMessagesFetch({
|
|
9
|
-
url
|
|
15
|
+
url
|
|
10
16
|
}) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
17
|
+
const getMessagesUrl = new URL(url);
|
|
18
|
+
getMessagesUrl.pathname += "/get-messages";
|
|
19
|
+
const response = await fetch(getMessagesUrl.toString(), {
|
|
20
|
+
credentials: options.credentials,
|
|
21
|
+
headers: options.headers
|
|
14
22
|
});
|
|
15
23
|
return response.json();
|
|
16
24
|
}
|
|
17
25
|
const getInitialMessagesFetch = getInitialMessages || defaultGetInitialMessagesFetch;
|
|
18
26
|
function doGetInitialMessages(getInitialMessagesOptions) {
|
|
19
|
-
if (requestCache.has(
|
|
20
|
-
return requestCache.get(
|
|
27
|
+
if (requestCache.has(agentUrlString)) {
|
|
28
|
+
return requestCache.get(agentUrlString);
|
|
21
29
|
}
|
|
22
30
|
const promise = getInitialMessagesFetch(getInitialMessagesOptions);
|
|
23
|
-
requestCache.set(
|
|
31
|
+
requestCache.set(agentUrlString, promise);
|
|
24
32
|
return promise;
|
|
25
33
|
}
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
) : rest.initialMessages;
|
|
34
|
+
const initialMessagesPromise = getInitialMessages === null ? null : doGetInitialMessages({
|
|
35
|
+
agent: agent.agent,
|
|
36
|
+
name: agent.name,
|
|
37
|
+
url: agentUrlString
|
|
38
|
+
});
|
|
39
|
+
const initialMessages = initialMessagesPromise ? use(initialMessagesPromise) : rest.initialMessages ?? [];
|
|
33
40
|
useEffect(() => {
|
|
41
|
+
if (!initialMessagesPromise) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
requestCache.set(agentUrlString, initialMessagesPromise);
|
|
34
45
|
return () => {
|
|
35
|
-
requestCache.
|
|
46
|
+
if (requestCache.get(agentUrlString) === initialMessagesPromise) {
|
|
47
|
+
requestCache.delete(agentUrlString);
|
|
48
|
+
}
|
|
36
49
|
};
|
|
37
|
-
}, [
|
|
50
|
+
}, [agentUrlString, initialMessagesPromise]);
|
|
38
51
|
async function aiFetch(request, options2 = {}) {
|
|
39
52
|
const {
|
|
40
53
|
method,
|
|
@@ -51,10 +64,17 @@ function useAgentChat(options) {
|
|
|
51
64
|
window
|
|
52
65
|
// dispatcher, duplex
|
|
53
66
|
} = options2;
|
|
54
|
-
const id =
|
|
67
|
+
const id = nanoid(8);
|
|
55
68
|
const abortController = new AbortController();
|
|
56
69
|
signal?.addEventListener("abort", () => {
|
|
70
|
+
agent.send(
|
|
71
|
+
JSON.stringify({
|
|
72
|
+
id,
|
|
73
|
+
type: "cf_agent_chat_request_cancel"
|
|
74
|
+
})
|
|
75
|
+
);
|
|
57
76
|
abortController.abort();
|
|
77
|
+
controller.close();
|
|
58
78
|
});
|
|
59
79
|
agent.addEventListener(
|
|
60
80
|
"message",
|
|
@@ -62,7 +82,7 @@ function useAgentChat(options) {
|
|
|
62
82
|
let data;
|
|
63
83
|
try {
|
|
64
84
|
data = JSON.parse(event.data);
|
|
65
|
-
} catch (
|
|
85
|
+
} catch (_error) {
|
|
66
86
|
return;
|
|
67
87
|
}
|
|
68
88
|
if (data.type === "cf_agent_use_chat_response") {
|
|
@@ -85,32 +105,32 @@ function useAgentChat(options) {
|
|
|
85
105
|
});
|
|
86
106
|
agent.send(
|
|
87
107
|
JSON.stringify({
|
|
88
|
-
type: "cf_agent_use_chat_request",
|
|
89
108
|
id,
|
|
90
|
-
url: request.toString(),
|
|
91
109
|
init: {
|
|
92
|
-
method,
|
|
93
|
-
keepalive,
|
|
94
|
-
headers,
|
|
95
110
|
body,
|
|
96
|
-
redirect,
|
|
97
|
-
integrity,
|
|
98
111
|
credentials,
|
|
112
|
+
headers,
|
|
113
|
+
integrity,
|
|
114
|
+
keepalive,
|
|
115
|
+
method,
|
|
99
116
|
mode,
|
|
117
|
+
redirect,
|
|
100
118
|
referrer,
|
|
101
119
|
referrerPolicy,
|
|
102
120
|
window
|
|
103
121
|
// dispatcher,
|
|
104
122
|
// duplex
|
|
105
|
-
}
|
|
123
|
+
},
|
|
124
|
+
type: "cf_agent_use_chat_request",
|
|
125
|
+
url: request.toString()
|
|
106
126
|
})
|
|
107
127
|
);
|
|
108
128
|
return new Response(stream);
|
|
109
129
|
}
|
|
110
130
|
const useChatHelpers = useChat({
|
|
131
|
+
fetch: aiFetch,
|
|
111
132
|
initialMessages,
|
|
112
133
|
sendExtraMessageFields: true,
|
|
113
|
-
fetch: aiFetch,
|
|
114
134
|
...rest
|
|
115
135
|
});
|
|
116
136
|
useEffect(() => {
|
|
@@ -121,7 +141,7 @@ function useAgentChat(options) {
|
|
|
121
141
|
let data;
|
|
122
142
|
try {
|
|
123
143
|
data = JSON.parse(event.data);
|
|
124
|
-
} catch (
|
|
144
|
+
} catch (_error) {
|
|
125
145
|
return;
|
|
126
146
|
}
|
|
127
147
|
if (data.type === "cf_agent_chat_clear") {
|
|
@@ -135,7 +155,7 @@ function useAgentChat(options) {
|
|
|
135
155
|
let data;
|
|
136
156
|
try {
|
|
137
157
|
data = JSON.parse(event.data);
|
|
138
|
-
} catch (
|
|
158
|
+
} catch (_error) {
|
|
139
159
|
return;
|
|
140
160
|
}
|
|
141
161
|
if (data.type === "cf_agent_chat_messages") {
|
|
@@ -148,34 +168,30 @@ function useAgentChat(options) {
|
|
|
148
168
|
agent.removeEventListener("message", onClearHistory);
|
|
149
169
|
agent.removeEventListener("message", onMessages);
|
|
150
170
|
};
|
|
151
|
-
}, [
|
|
152
|
-
agent.addEventListener,
|
|
153
|
-
agent.removeEventListener,
|
|
154
|
-
useChatHelpers.setMessages
|
|
155
|
-
]);
|
|
171
|
+
}, [agent, useChatHelpers.setMessages]);
|
|
156
172
|
return {
|
|
157
173
|
...useChatHelpers,
|
|
158
174
|
/**
|
|
159
|
-
*
|
|
160
|
-
* @param messages New messages to set
|
|
175
|
+
* Clear chat history on both client and Agent
|
|
161
176
|
*/
|
|
162
|
-
|
|
163
|
-
useChatHelpers.setMessages(
|
|
177
|
+
clearHistory: () => {
|
|
178
|
+
useChatHelpers.setMessages([]);
|
|
164
179
|
agent.send(
|
|
165
180
|
JSON.stringify({
|
|
166
|
-
type: "
|
|
167
|
-
messages
|
|
181
|
+
type: "cf_agent_chat_clear"
|
|
168
182
|
})
|
|
169
183
|
);
|
|
170
184
|
},
|
|
171
185
|
/**
|
|
172
|
-
*
|
|
186
|
+
* Set the chat messages and synchronize with the Agent
|
|
187
|
+
* @param messages New messages to set
|
|
173
188
|
*/
|
|
174
|
-
|
|
175
|
-
useChatHelpers.setMessages(
|
|
189
|
+
setMessages: (messages) => {
|
|
190
|
+
useChatHelpers.setMessages(messages);
|
|
176
191
|
agent.send(
|
|
177
192
|
JSON.stringify({
|
|
178
|
-
|
|
193
|
+
messages,
|
|
194
|
+
type: "cf_agent_chat_messages"
|
|
179
195
|
})
|
|
180
196
|
);
|
|
181
197
|
}
|