agents 0.0.0-c3e8618 → 0.0.0-c8f53b8

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.
@@ -1,31 +1,55 @@
1
- import { Agent, AgentContext } from './index.js';
2
- import { Message, StreamTextOnFinishCallback, ToolSet } from 'ai';
3
- import { Connection, WSMessage } from 'partyserver';
4
- import 'node:async_hooks';
5
- import 'cloudflare:workers';
1
+ import { Agent, AgentContext } from "./index.js";
2
+ import { Message, StreamTextOnFinishCallback, ToolSet } from "ai";
3
+ import { Connection, WSMessage } from "partyserver";
4
+ import "./mcp/client.js";
5
+ import "zod";
6
+ import "@modelcontextprotocol/sdk/types.js";
7
+ import "@modelcontextprotocol/sdk/client/index.js";
8
+ import "@modelcontextprotocol/sdk/client/sse.js";
9
+ import "./mcp/do-oauth-client-provider.js";
10
+ import "@modelcontextprotocol/sdk/client/auth.js";
11
+ import "@modelcontextprotocol/sdk/shared/auth.js";
12
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
6
13
 
7
14
  /**
8
15
  * Extension of Agent with built-in chat capabilities
9
16
  * @template Env Environment type containing bindings
10
17
  */
11
- declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<Env, State> {
12
- #private;
13
- /** Array of chat messages for the current conversation */
14
- messages: Message[];
15
- constructor(ctx: AgentContext, env: Env);
16
- onMessage(connection: Connection, message: WSMessage): Promise<void>;
17
- onRequest(request: Request): Promise<Response>;
18
- /**
19
- * Handle incoming chat messages and generate a response
20
- * @param onFinish Callback to be called when the response is finished
21
- * @returns Response to send to the client or undefined
22
- */
23
- onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>): Promise<Response | undefined>;
24
- /**
25
- * Save messages on the server side and trigger AI response
26
- * @param messages Chat messages to save
27
- */
28
- saveMessages(messages: Message[]): Promise<void>;
18
+ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
19
+ Env,
20
+ State
21
+ > {
22
+ #private;
23
+ /** Array of chat messages for the current conversation */
24
+ messages: Message[];
25
+ constructor(ctx: AgentContext, env: Env);
26
+ onMessage(connection: Connection, message: WSMessage): Promise<void>;
27
+ onRequest(request: Request): Promise<Response>;
28
+ /**
29
+ * Handle incoming chat messages and generate a response
30
+ * @param onFinish Callback to be called when the response is finished
31
+ * @param options.signal A signal to pass to any child requests which can be used to cancel them
32
+ * @returns Response to send to the client or undefined
33
+ */
34
+ onChatMessage(
35
+ onFinish: StreamTextOnFinishCallback<ToolSet>,
36
+ options?: {
37
+ abortSignal: AbortSignal | undefined;
38
+ }
39
+ ): Promise<Response | undefined>;
40
+ /**
41
+ * Save messages on the server side and trigger AI response
42
+ * @param messages Chat messages to save
43
+ */
44
+ saveMessages(messages: Message[]): Promise<void>;
45
+ persistMessages(
46
+ messages: Message[],
47
+ excludeBroadcastIds?: string[]
48
+ ): Promise<void>;
49
+ /**
50
+ * When the DO is destroyed, cancel all pending requests
51
+ */
52
+ destroy(): Promise<void>;
29
53
  }
30
54
 
31
55
  export { AIChatAgent };
@@ -0,0 +1,234 @@
1
+ import {
2
+ Agent
3
+ } from "./chunk-HD4VEHBA.js";
4
+ import "./chunk-Q5ZBHY4Z.js";
5
+ import {
6
+ __privateAdd,
7
+ __privateGet,
8
+ __privateMethod,
9
+ __privateSet
10
+ } from "./chunk-HMLY7DHA.js";
11
+
12
+ // src/ai-chat-agent.ts
13
+ import { appendResponseMessages } from "ai";
14
+ var decoder = new TextDecoder();
15
+ var _chatMessageAbortControllers, _AIChatAgent_instances, broadcastChatMessage_fn, tryCatch_fn, reply_fn, getAbortSignal_fn, removeAbortController_fn, cancelChatRequest_fn, destroyAbortControllers_fn;
16
+ var AIChatAgent = class extends Agent {
17
+ constructor(ctx, env) {
18
+ super(ctx, env);
19
+ __privateAdd(this, _AIChatAgent_instances);
20
+ /**
21
+ * Map of message `id`s to `AbortController`s
22
+ * useful to propagate request cancellation signals for any external calls made by the agent
23
+ */
24
+ __privateAdd(this, _chatMessageAbortControllers);
25
+ this.sql`create table if not exists cf_ai_chat_agent_messages (
26
+ id text primary key,
27
+ message text not null,
28
+ created_at datetime default current_timestamp
29
+ )`;
30
+ this.messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
31
+ return JSON.parse(row.message);
32
+ });
33
+ __privateSet(this, _chatMessageAbortControllers, /* @__PURE__ */ new Map());
34
+ }
35
+ async onMessage(connection, message) {
36
+ if (typeof message === "string") {
37
+ let data;
38
+ try {
39
+ data = JSON.parse(message);
40
+ } catch (error) {
41
+ return;
42
+ }
43
+ if (data.type === "cf_agent_use_chat_request" && data.init.method === "POST") {
44
+ const {
45
+ method,
46
+ keepalive,
47
+ headers,
48
+ body,
49
+ // we're reading this
50
+ redirect,
51
+ integrity,
52
+ credentials,
53
+ mode,
54
+ referrer,
55
+ referrerPolicy,
56
+ window
57
+ // dispatcher,
58
+ // duplex
59
+ } = data.init;
60
+ const { messages } = JSON.parse(body);
61
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
62
+ type: "cf_agent_chat_messages",
63
+ messages
64
+ }, [connection.id]);
65
+ await this.persistMessages(messages, [connection.id]);
66
+ const chatMessageId = data.id;
67
+ const abortSignal = __privateMethod(this, _AIChatAgent_instances, getAbortSignal_fn).call(this, chatMessageId);
68
+ return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, async () => {
69
+ const response = await this.onChatMessage(
70
+ async ({ response: response2 }) => {
71
+ const finalMessages = appendResponseMessages({
72
+ messages,
73
+ responseMessages: response2.messages
74
+ });
75
+ await this.persistMessages(finalMessages, [connection.id]);
76
+ __privateMethod(this, _AIChatAgent_instances, removeAbortController_fn).call(this, chatMessageId);
77
+ },
78
+ abortSignal ? { abortSignal } : void 0
79
+ );
80
+ if (response) {
81
+ await __privateMethod(this, _AIChatAgent_instances, reply_fn).call(this, data.id, response);
82
+ }
83
+ });
84
+ }
85
+ if (data.type === "cf_agent_chat_clear") {
86
+ __privateMethod(this, _AIChatAgent_instances, destroyAbortControllers_fn).call(this);
87
+ this.sql`delete from cf_ai_chat_agent_messages`;
88
+ this.messages = [];
89
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
90
+ type: "cf_agent_chat_clear"
91
+ }, [connection.id]);
92
+ } else if (data.type === "cf_agent_chat_messages") {
93
+ await this.persistMessages(data.messages, [connection.id]);
94
+ } else if (data.type === "cf_agent_chat_request_cancel") {
95
+ __privateMethod(this, _AIChatAgent_instances, cancelChatRequest_fn).call(this, data.id);
96
+ }
97
+ }
98
+ }
99
+ async onRequest(request) {
100
+ return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, () => {
101
+ const url = new URL(request.url);
102
+ if (url.pathname.endsWith("/get-messages")) {
103
+ const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
104
+ return JSON.parse(row.message);
105
+ });
106
+ return Response.json(messages);
107
+ }
108
+ return super.onRequest(request);
109
+ });
110
+ }
111
+ /**
112
+ * Handle incoming chat messages and generate a response
113
+ * @param onFinish Callback to be called when the response is finished
114
+ * @param options.signal A signal to pass to any child requests which can be used to cancel them
115
+ * @returns Response to send to the client or undefined
116
+ */
117
+ async onChatMessage(onFinish, options) {
118
+ throw new Error(
119
+ "recieved a chat message, override onChatMessage and return a Response to send to the client"
120
+ );
121
+ }
122
+ /**
123
+ * Save messages on the server side and trigger AI response
124
+ * @param messages Chat messages to save
125
+ */
126
+ async saveMessages(messages) {
127
+ await this.persistMessages(messages);
128
+ const response = await this.onChatMessage(async ({ response: response2 }) => {
129
+ const finalMessages = appendResponseMessages({
130
+ messages,
131
+ responseMessages: response2.messages
132
+ });
133
+ await this.persistMessages(finalMessages, []);
134
+ });
135
+ if (response) {
136
+ for await (const chunk of response.body) {
137
+ decoder.decode(chunk);
138
+ }
139
+ response.body?.cancel();
140
+ }
141
+ }
142
+ async persistMessages(messages, excludeBroadcastIds = []) {
143
+ this.sql`delete from cf_ai_chat_agent_messages`;
144
+ for (const message of messages) {
145
+ this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
146
+ }
147
+ this.messages = messages;
148
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
149
+ type: "cf_agent_chat_messages",
150
+ messages
151
+ }, excludeBroadcastIds);
152
+ }
153
+ /**
154
+ * When the DO is destroyed, cancel all pending requests
155
+ */
156
+ async destroy() {
157
+ __privateMethod(this, _AIChatAgent_instances, destroyAbortControllers_fn).call(this);
158
+ await super.destroy();
159
+ }
160
+ };
161
+ _chatMessageAbortControllers = new WeakMap();
162
+ _AIChatAgent_instances = new WeakSet();
163
+ broadcastChatMessage_fn = function(message, exclude) {
164
+ this.broadcast(JSON.stringify(message), exclude);
165
+ };
166
+ tryCatch_fn = async function(fn) {
167
+ try {
168
+ return await fn();
169
+ } catch (e) {
170
+ throw this.onError(e);
171
+ }
172
+ };
173
+ reply_fn = async function(id, response) {
174
+ return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, async () => {
175
+ for await (const chunk of response.body) {
176
+ const body = decoder.decode(chunk);
177
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
178
+ id,
179
+ type: "cf_agent_use_chat_response",
180
+ body,
181
+ done: false
182
+ });
183
+ }
184
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
185
+ id,
186
+ type: "cf_agent_use_chat_response",
187
+ body: "",
188
+ done: true
189
+ });
190
+ });
191
+ };
192
+ /**
193
+ * For the given message id, look up its associated AbortController
194
+ * If the AbortController does not exist, create and store one in memory
195
+ *
196
+ * returns the AbortSignal associated with the AbortController
197
+ */
198
+ getAbortSignal_fn = function(id) {
199
+ if (typeof id !== "string") {
200
+ return void 0;
201
+ }
202
+ if (!__privateGet(this, _chatMessageAbortControllers).has(id)) {
203
+ __privateGet(this, _chatMessageAbortControllers).set(id, new AbortController());
204
+ }
205
+ return __privateGet(this, _chatMessageAbortControllers).get(id)?.signal;
206
+ };
207
+ /**
208
+ * Remove an abort controller from the cache of pending message responses
209
+ */
210
+ removeAbortController_fn = function(id) {
211
+ __privateGet(this, _chatMessageAbortControllers).delete(id);
212
+ };
213
+ /**
214
+ * Propagate an abort signal for any requests associated with the given message id
215
+ */
216
+ cancelChatRequest_fn = function(id) {
217
+ if (__privateGet(this, _chatMessageAbortControllers).has(id)) {
218
+ const abortController = __privateGet(this, _chatMessageAbortControllers).get(id);
219
+ abortController?.abort();
220
+ }
221
+ };
222
+ /**
223
+ * Abort all pending requests and clear the cache of AbortControllers
224
+ */
225
+ destroyAbortControllers_fn = function() {
226
+ for (const controller of __privateGet(this, _chatMessageAbortControllers).values()) {
227
+ controller?.abort();
228
+ }
229
+ __privateGet(this, _chatMessageAbortControllers).clear();
230
+ };
231
+ export {
232
+ AIChatAgent
233
+ };
234
+ //# sourceMappingURL=ai-chat-agent.js.map
@@ -0,0 +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\";\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 #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 #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\n const chatMessageId = data.id;\n const abortSignal = this.#getAbortSignal(chatMessageId);\n\n return this.#tryCatch(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 }\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.#tryCatch(() => {\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 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 * @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 onFinish: StreamTextOnFinishCallback<ToolSet>,\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 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 /**\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 #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 #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 #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 #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":";;;;;;;;;;;;AAMA,SAAS,8BAA8B;AAGvC,IAAM,UAAU,IAAI,YAAY;AAThC;AAeO,IAAM,cAAN,cAA0D,MAG/D;AAAA,EAQA,YAAY,KAAmB,KAAU;AACvC,UAAM,KAAK,GAAG;AAZX;AAQL;AAAA;AAAA;AAAA;AAAA;AAKE,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,uBAAK,8BAA+B,oBAAI,IAAI;AAAA,EAC9C;AAAA,EAMA,MAAe,UAAU,YAAwB,SAAoB;AACnE,QAAI,OAAO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C;AAAA;AAAA;AAAA;AAKA,uBAAkB,SAAC,IAAY;AAC7B,MAAI,mBAAK,8BAA6B,IAAI,EAAE,GAAG;AAC7C,UAAM,kBAAkB,mBAAK,8BAA6B,IAAI,EAAE;AAChE,qBAAiB,MAAM;AAAA,EACzB;AACF;AAAA;AAAA;AAAA;AAKA,6BAAwB,WAAG;AACzB,aAAW,cAAc,mBAAK,8BAA6B,OAAO,GAAG;AACnE,gBAAY,MAAM;AAAA,EACpB;AACA,qBAAK,8BAA6B,MAAM;AAC1C;","names":["response"]}
@@ -1,60 +1,88 @@
1
- import * as ai from 'ai';
2
- import { Message } from 'ai';
3
- import { useChat } from '@ai-sdk/react';
4
- import { useAgent } from './react.js';
5
- import 'partysocket';
6
- import 'partysocket/react';
7
- import './client.js';
1
+ import * as ai from "ai";
2
+ import { Message } from "ai";
3
+ import { useChat } from "@ai-sdk/react";
4
+ import { useAgent } from "./react.js";
5
+ import "partysocket";
6
+ import "partysocket/react";
7
+ import "./client.js";
8
8
 
9
9
  type GetInitialMessagesOptions = {
10
- agent: string;
11
- name: string;
12
- url: string;
10
+ agent: string;
11
+ name: string;
12
+ url: string;
13
13
  };
14
14
  /**
15
15
  * Options for the useAgentChat hook
16
16
  */
17
- type UseAgentChatOptions<State> = Omit<Parameters<typeof useChat>[0] & {
17
+ type UseAgentChatOptions<State> = Omit<
18
+ Parameters<typeof useChat>[0] & {
18
19
  /** Agent connection from useAgent */
19
20
  agent: ReturnType<typeof useAgent<State>>;
20
- getInitialMessages?: undefined | null | ((options: GetInitialMessagesOptions) => Promise<Message[]>);
21
- }, "fetch">;
21
+ getInitialMessages?:
22
+ | undefined
23
+ | null
24
+ | ((options: GetInitialMessagesOptions) => Promise<Message[]>);
25
+ },
26
+ "fetch"
27
+ >;
22
28
  /**
23
29
  * React hook for building AI chat interfaces using an Agent
24
30
  * @param options Chat options including the agent connection
25
31
  * @returns Chat interface controls and state with added clearHistory method
26
32
  */
27
- declare function useAgentChat<State = unknown>(options: UseAgentChatOptions<State>): {
28
- /**
29
- * Set the chat messages and synchronize with the Agent
30
- * @param messages New messages to set
31
- */
32
- setMessages: (messages: Message[]) => void;
33
- /**
34
- * Clear chat history on both client and Agent
35
- */
36
- clearHistory: () => void;
37
- messages: ai.UIMessage[];
38
- error: undefined | Error;
39
- append: (message: Message | ai.CreateMessage, chatRequestOptions?: ai.ChatRequestOptions) => Promise<string | null | undefined>;
40
- reload: (chatRequestOptions?: ai.ChatRequestOptions) => Promise<string | null | undefined>;
41
- stop: () => void;
42
- input: string;
43
- setInput: React.Dispatch<React.SetStateAction<string>>;
44
- handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
45
- handleSubmit: (event?: {
46
- preventDefault?: () => void;
47
- }, chatRequestOptions?: ai.ChatRequestOptions) => void;
48
- metadata?: Object;
49
- isLoading: boolean;
50
- status: "submitted" | "streaming" | "ready" | "error";
51
- data?: ai.JSONValue[];
52
- setData: (data: ai.JSONValue[] | undefined | ((data: ai.JSONValue[] | undefined) => ai.JSONValue[] | undefined)) => void;
53
- id: string;
54
- addToolResult: ({ toolCallId, result, }: {
55
- toolCallId: string;
56
- result: any;
57
- }) => void;
33
+ declare function useAgentChat<State = unknown>(
34
+ options: UseAgentChatOptions<State>
35
+ ): {
36
+ /**
37
+ * Set the chat messages and synchronize with the Agent
38
+ * @param messages New messages to set
39
+ */
40
+ setMessages: (messages: Message[]) => void;
41
+ /**
42
+ * Clear chat history on both client and Agent
43
+ */
44
+ clearHistory: () => void;
45
+ messages: ai.UIMessage[];
46
+ error: undefined | Error;
47
+ append: (
48
+ message: Message | ai.CreateMessage,
49
+ chatRequestOptions?: ai.ChatRequestOptions
50
+ ) => Promise<string | null | undefined>;
51
+ reload: (
52
+ chatRequestOptions?: ai.ChatRequestOptions
53
+ ) => Promise<string | null | undefined>;
54
+ stop: () => void;
55
+ input: string;
56
+ setInput: React.Dispatch<React.SetStateAction<string>>;
57
+ handleInputChange: (
58
+ e:
59
+ | React.ChangeEvent<HTMLInputElement>
60
+ | React.ChangeEvent<HTMLTextAreaElement>
61
+ ) => void;
62
+ handleSubmit: (
63
+ event?: {
64
+ preventDefault?: () => void;
65
+ },
66
+ chatRequestOptions?: ai.ChatRequestOptions
67
+ ) => void;
68
+ metadata?: Object;
69
+ isLoading: boolean;
70
+ status: "submitted" | "streaming" | "ready" | "error";
71
+ data?: ai.JSONValue[];
72
+ setData: (
73
+ data:
74
+ | ai.JSONValue[]
75
+ | undefined
76
+ | ((data: ai.JSONValue[] | undefined) => ai.JSONValue[] | undefined)
77
+ ) => void;
78
+ id: string;
79
+ addToolResult: ({
80
+ toolCallId,
81
+ result,
82
+ }: {
83
+ toolCallId: string;
84
+ result: any;
85
+ }) => void;
58
86
  };
59
87
 
60
88
  export { useAgentChat };
@@ -0,0 +1,204 @@
1
+ import "./chunk-HMLY7DHA.js";
2
+
3
+ // src/ai-react.tsx
4
+ import { useChat } from "@ai-sdk/react";
5
+ import { use, useEffect } from "react";
6
+ import { nanoid } from "nanoid";
7
+ var requestCache = /* @__PURE__ */ new Map();
8
+ function useAgentChat(options) {
9
+ const { agent, getInitialMessages, ...rest } = options;
10
+ const agentUrl = new URL(
11
+ `${// @ts-expect-error we're using a protected _url property that includes query params
12
+ (agent._url || agent._pkurl)?.replace("ws://", "http://").replace("wss://", "https://")}`
13
+ );
14
+ agentUrl.searchParams.delete("_pk");
15
+ const agentUrlString = agentUrl.toString();
16
+ async function defaultGetInitialMessagesFetch({
17
+ url
18
+ }) {
19
+ const getMessagesUrl = new URL(url);
20
+ getMessagesUrl.pathname += "/get-messages";
21
+ const response = await fetch(getMessagesUrl.toString(), {
22
+ headers: options.headers,
23
+ credentials: options.credentials
24
+ });
25
+ return response.json();
26
+ }
27
+ const getInitialMessagesFetch = getInitialMessages || defaultGetInitialMessagesFetch;
28
+ function doGetInitialMessages(getInitialMessagesOptions) {
29
+ if (requestCache.has(agentUrlString)) {
30
+ return requestCache.get(agentUrlString);
31
+ }
32
+ const promise = getInitialMessagesFetch(getInitialMessagesOptions);
33
+ requestCache.set(agentUrlString, promise);
34
+ return promise;
35
+ }
36
+ const initialMessagesPromise = getInitialMessages === null ? null : doGetInitialMessages({
37
+ agent: agent.agent,
38
+ name: agent.name,
39
+ url: agentUrlString
40
+ });
41
+ const initialMessages = initialMessagesPromise ? use(initialMessagesPromise) : rest.initialMessages ?? [];
42
+ useEffect(() => {
43
+ if (!initialMessagesPromise) {
44
+ return;
45
+ }
46
+ requestCache.set(agentUrlString, initialMessagesPromise);
47
+ return () => {
48
+ if (requestCache.get(agentUrlString) === initialMessagesPromise) {
49
+ requestCache.delete(agentUrlString);
50
+ }
51
+ };
52
+ }, [agentUrlString, initialMessagesPromise]);
53
+ async function aiFetch(request, options2 = {}) {
54
+ const {
55
+ method,
56
+ keepalive,
57
+ headers,
58
+ body,
59
+ redirect,
60
+ integrity,
61
+ signal,
62
+ credentials,
63
+ mode,
64
+ referrer,
65
+ referrerPolicy,
66
+ window
67
+ // dispatcher, duplex
68
+ } = options2;
69
+ const id = nanoid(8);
70
+ const abortController = new AbortController();
71
+ signal?.addEventListener("abort", () => {
72
+ agent.send(
73
+ JSON.stringify({
74
+ type: "cf_agent_chat_request_cancel",
75
+ id
76
+ })
77
+ );
78
+ abortController.abort();
79
+ });
80
+ agent.addEventListener(
81
+ "message",
82
+ (event) => {
83
+ let data;
84
+ try {
85
+ data = JSON.parse(event.data);
86
+ } catch (error) {
87
+ return;
88
+ }
89
+ if (data.type === "cf_agent_use_chat_response") {
90
+ if (data.id === id) {
91
+ controller.enqueue(new TextEncoder().encode(data.body));
92
+ if (data.done) {
93
+ controller.close();
94
+ abortController.abort();
95
+ }
96
+ }
97
+ }
98
+ },
99
+ { signal: abortController.signal }
100
+ );
101
+ let controller;
102
+ const stream = new ReadableStream({
103
+ start(c) {
104
+ controller = c;
105
+ }
106
+ });
107
+ agent.send(
108
+ JSON.stringify({
109
+ type: "cf_agent_use_chat_request",
110
+ id,
111
+ url: request.toString(),
112
+ init: {
113
+ method,
114
+ keepalive,
115
+ headers,
116
+ body,
117
+ redirect,
118
+ integrity,
119
+ credentials,
120
+ mode,
121
+ referrer,
122
+ referrerPolicy,
123
+ window
124
+ // dispatcher,
125
+ // duplex
126
+ }
127
+ })
128
+ );
129
+ return new Response(stream);
130
+ }
131
+ const useChatHelpers = useChat({
132
+ initialMessages,
133
+ sendExtraMessageFields: true,
134
+ fetch: aiFetch,
135
+ ...rest
136
+ });
137
+ useEffect(() => {
138
+ function onClearHistory(event) {
139
+ if (typeof event.data !== "string") {
140
+ return;
141
+ }
142
+ let data;
143
+ try {
144
+ data = JSON.parse(event.data);
145
+ } catch (error) {
146
+ return;
147
+ }
148
+ if (data.type === "cf_agent_chat_clear") {
149
+ useChatHelpers.setMessages([]);
150
+ }
151
+ }
152
+ function onMessages(event) {
153
+ if (typeof event.data !== "string") {
154
+ return;
155
+ }
156
+ let data;
157
+ try {
158
+ data = JSON.parse(event.data);
159
+ } catch (error) {
160
+ return;
161
+ }
162
+ if (data.type === "cf_agent_chat_messages") {
163
+ useChatHelpers.setMessages(data.messages);
164
+ }
165
+ }
166
+ agent.addEventListener("message", onClearHistory);
167
+ agent.addEventListener("message", onMessages);
168
+ return () => {
169
+ agent.removeEventListener("message", onClearHistory);
170
+ agent.removeEventListener("message", onMessages);
171
+ };
172
+ }, [agent, useChatHelpers.setMessages]);
173
+ return {
174
+ ...useChatHelpers,
175
+ /**
176
+ * Set the chat messages and synchronize with the Agent
177
+ * @param messages New messages to set
178
+ */
179
+ setMessages: (messages) => {
180
+ useChatHelpers.setMessages(messages);
181
+ agent.send(
182
+ JSON.stringify({
183
+ type: "cf_agent_chat_messages",
184
+ messages
185
+ })
186
+ );
187
+ },
188
+ /**
189
+ * Clear chat history on both client and Agent
190
+ */
191
+ clearHistory: () => {
192
+ useChatHelpers.setMessages([]);
193
+ agent.send(
194
+ JSON.stringify({
195
+ type: "cf_agent_chat_clear"
196
+ })
197
+ );
198
+ }
199
+ };
200
+ }
201
+ export {
202
+ useAgentChat
203
+ };
204
+ //# sourceMappingURL=ai-react.js.map