agents 0.0.0-c3e8618 → 0.0.0-c4c9271

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.
Files changed (43) hide show
  1. package/dist/ai-chat-agent.d.ts +73 -23
  2. package/dist/ai-chat-agent.js +230 -0
  3. package/dist/ai-chat-agent.js.map +1 -0
  4. package/dist/ai-react.d.ts +85 -44
  5. package/dist/ai-react.js +203 -0
  6. package/dist/ai-react.js.map +1 -0
  7. package/dist/ai-types.d.ts +65 -40
  8. package/dist/ai-types.js +1 -0
  9. package/dist/ai-types.js.map +1 -0
  10. package/dist/chunk-6RPGDIE2.js +786 -0
  11. package/dist/chunk-6RPGDIE2.js.map +1 -0
  12. package/dist/chunk-BZXOAZUX.js +106 -0
  13. package/dist/chunk-BZXOAZUX.js.map +1 -0
  14. package/dist/chunk-OYJXQRRH.js +465 -0
  15. package/dist/chunk-OYJXQRRH.js.map +1 -0
  16. package/dist/chunk-VCSB47AK.js +116 -0
  17. package/dist/chunk-VCSB47AK.js.map +1 -0
  18. package/dist/client.d.ts +71 -37
  19. package/dist/client.js +11 -0
  20. package/dist/client.js.map +1 -0
  21. package/dist/index.d.ts +331 -179
  22. package/dist/index.js +22 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/mcp/client.d.ts +142 -34
  25. package/dist/mcp/client.js +9 -0
  26. package/dist/mcp/client.js.map +1 -0
  27. package/dist/mcp/do-oauth-client-provider.d.ts +41 -0
  28. package/dist/mcp/do-oauth-client-provider.js +7 -0
  29. package/dist/mcp/do-oauth-client-provider.js.map +1 -0
  30. package/dist/mcp/index.d.ts +50 -7
  31. package/dist/mcp/index.js +782 -0
  32. package/dist/mcp/index.js.map +1 -0
  33. package/dist/react.d.ts +104 -15
  34. package/dist/react.js +116 -0
  35. package/dist/react.js.map +1 -0
  36. package/dist/schedule.d.ts +30 -20
  37. package/dist/schedule.js +71 -0
  38. package/dist/schedule.js.map +1 -0
  39. package/dist/serializable.d.ts +32 -0
  40. package/dist/serializable.js +1 -0
  41. package/dist/serializable.js.map +1 -0
  42. package/package.json +28 -5
  43. package/src/index.ts +396 -60
@@ -1,31 +1,81 @@
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 "@modelcontextprotocol/sdk/types.js";
5
+ import "./mcp/client.js";
6
+ import "zod";
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
+ /**
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;
27
+ /** Array of chat messages for the current conversation */
28
+ messages: Message[];
29
+ constructor(ctx: AgentContext, env: Env);
30
+ private _broadcastChatMessage;
31
+ onMessage(connection: Connection, message: WSMessage): Promise<void>;
32
+ onRequest(request: Request): Promise<Response>;
33
+ private _tryCatchChat;
34
+ /**
35
+ * Handle incoming chat messages and generate a response
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
38
+ * @returns Response to send to the client or undefined
39
+ */
40
+ onChatMessage(
41
+ onFinish: StreamTextOnFinishCallback<ToolSet>,
42
+ options?: {
43
+ abortSignal: AbortSignal | undefined;
44
+ }
45
+ ): Promise<Response | undefined>;
46
+ /**
47
+ * Save messages on the server side and trigger AI response
48
+ * @param messages Chat messages to save
49
+ */
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>;
29
79
  }
30
80
 
31
81
  export { AIChatAgent };
@@ -0,0 +1,230 @@
1
+ import {
2
+ Agent
3
+ } from "./chunk-6RPGDIE2.js";
4
+ import "./chunk-OYJXQRRH.js";
5
+ import "./chunk-BZXOAZUX.js";
6
+ import "./chunk-VCSB47AK.js";
7
+
8
+ // src/ai-chat-agent.ts
9
+ import { appendResponseMessages } from "ai";
10
+ var decoder = new TextDecoder();
11
+ var AIChatAgent = class extends Agent {
12
+ constructor(ctx, env) {
13
+ super(ctx, env);
14
+ this.sql`create table if not exists cf_ai_chat_agent_messages (
15
+ id text primary key,
16
+ message text not null,
17
+ created_at datetime default current_timestamp
18
+ )`;
19
+ this.messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
20
+ return JSON.parse(row.message);
21
+ });
22
+ this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
23
+ }
24
+ _broadcastChatMessage(message, exclude) {
25
+ this.broadcast(JSON.stringify(message), exclude);
26
+ }
27
+ async onMessage(connection, message) {
28
+ if (typeof message === "string") {
29
+ let data;
30
+ try {
31
+ data = JSON.parse(message);
32
+ } catch (error) {
33
+ return;
34
+ }
35
+ if (data.type === "cf_agent_use_chat_request" && data.init.method === "POST") {
36
+ const {
37
+ method,
38
+ keepalive,
39
+ headers,
40
+ body,
41
+ // we're reading this
42
+ redirect,
43
+ integrity,
44
+ credentials,
45
+ mode,
46
+ referrer,
47
+ referrerPolicy,
48
+ window
49
+ // dispatcher,
50
+ // duplex
51
+ } = data.init;
52
+ const { messages } = JSON.parse(body);
53
+ this._broadcastChatMessage(
54
+ {
55
+ type: "cf_agent_chat_messages",
56
+ messages
57
+ },
58
+ [connection.id]
59
+ );
60
+ await this.persistMessages(messages, [connection.id]);
61
+ const chatMessageId = data.id;
62
+ const abortSignal = this._getAbortSignal(chatMessageId);
63
+ return this._tryCatchChat(async () => {
64
+ const response = await this.onChatMessage(
65
+ async ({ response: response2 }) => {
66
+ const finalMessages = appendResponseMessages({
67
+ messages,
68
+ responseMessages: response2.messages
69
+ });
70
+ await this.persistMessages(finalMessages, [connection.id]);
71
+ this._removeAbortController(chatMessageId);
72
+ },
73
+ abortSignal ? { abortSignal } : void 0
74
+ );
75
+ if (response) {
76
+ await this._reply(data.id, response);
77
+ }
78
+ });
79
+ }
80
+ if (data.type === "cf_agent_chat_clear") {
81
+ this._destroyAbortControllers();
82
+ this.sql`delete from cf_ai_chat_agent_messages`;
83
+ this.messages = [];
84
+ this._broadcastChatMessage(
85
+ {
86
+ type: "cf_agent_chat_clear"
87
+ },
88
+ [connection.id]
89
+ );
90
+ } else if (data.type === "cf_agent_chat_messages") {
91
+ await this.persistMessages(data.messages, [connection.id]);
92
+ } else if (data.type === "cf_agent_chat_request_cancel") {
93
+ this._cancelChatRequest(data.id);
94
+ }
95
+ }
96
+ }
97
+ async onRequest(request) {
98
+ return this._tryCatchChat(() => {
99
+ const url = new URL(request.url);
100
+ if (url.pathname.endsWith("/get-messages")) {
101
+ const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
102
+ return JSON.parse(row.message);
103
+ });
104
+ return Response.json(messages);
105
+ }
106
+ return super.onRequest(request);
107
+ });
108
+ }
109
+ async _tryCatchChat(fn) {
110
+ try {
111
+ return await fn();
112
+ } catch (e) {
113
+ throw this.onError(e);
114
+ }
115
+ }
116
+ /**
117
+ * Handle incoming chat messages and generate a response
118
+ * @param onFinish Callback to be called when the response is finished
119
+ * @param options.signal A signal to pass to any child requests which can be used to cancel them
120
+ * @returns Response to send to the client or undefined
121
+ */
122
+ async onChatMessage(onFinish, options) {
123
+ throw new Error(
124
+ "recieved a chat message, override onChatMessage and return a Response to send to the client"
125
+ );
126
+ }
127
+ /**
128
+ * Save messages on the server side and trigger AI response
129
+ * @param messages Chat messages to save
130
+ */
131
+ async saveMessages(messages) {
132
+ await this.persistMessages(messages);
133
+ const response = await this.onChatMessage(async ({ response: response2 }) => {
134
+ const finalMessages = appendResponseMessages({
135
+ messages,
136
+ responseMessages: response2.messages
137
+ });
138
+ await this.persistMessages(finalMessages, []);
139
+ });
140
+ if (response) {
141
+ for await (const chunk of response.body) {
142
+ decoder.decode(chunk);
143
+ }
144
+ response.body?.cancel();
145
+ }
146
+ }
147
+ async persistMessages(messages, excludeBroadcastIds = []) {
148
+ this.sql`delete from cf_ai_chat_agent_messages`;
149
+ for (const message of messages) {
150
+ this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
151
+ }
152
+ this.messages = messages;
153
+ this._broadcastChatMessage(
154
+ {
155
+ type: "cf_agent_chat_messages",
156
+ messages
157
+ },
158
+ excludeBroadcastIds
159
+ );
160
+ }
161
+ async _reply(id, response) {
162
+ return this._tryCatchChat(async () => {
163
+ for await (const chunk of response.body) {
164
+ const body = decoder.decode(chunk);
165
+ this._broadcastChatMessage({
166
+ id,
167
+ type: "cf_agent_use_chat_response",
168
+ body,
169
+ done: false
170
+ });
171
+ }
172
+ this._broadcastChatMessage({
173
+ id,
174
+ type: "cf_agent_use_chat_response",
175
+ body: "",
176
+ done: true
177
+ });
178
+ });
179
+ }
180
+ /**
181
+ * For the given message id, look up its associated AbortController
182
+ * If the AbortController does not exist, create and store one in memory
183
+ *
184
+ * returns the AbortSignal associated with the AbortController
185
+ */
186
+ _getAbortSignal(id) {
187
+ if (typeof id !== "string") {
188
+ return void 0;
189
+ }
190
+ if (!this._chatMessageAbortControllers.has(id)) {
191
+ this._chatMessageAbortControllers.set(id, new AbortController());
192
+ }
193
+ return this._chatMessageAbortControllers.get(id)?.signal;
194
+ }
195
+ /**
196
+ * Remove an abort controller from the cache of pending message responses
197
+ */
198
+ _removeAbortController(id) {
199
+ this._chatMessageAbortControllers.delete(id);
200
+ }
201
+ /**
202
+ * Propagate an abort signal for any requests associated with the given message id
203
+ */
204
+ _cancelChatRequest(id) {
205
+ if (this._chatMessageAbortControllers.has(id)) {
206
+ const abortController = this._chatMessageAbortControllers.get(id);
207
+ abortController?.abort();
208
+ }
209
+ }
210
+ /**
211
+ * Abort all pending requests and clear the cache of AbortControllers
212
+ */
213
+ _destroyAbortControllers() {
214
+ for (const controller of this._chatMessageAbortControllers.values()) {
215
+ controller?.abort();
216
+ }
217
+ this._chatMessageAbortControllers.clear();
218
+ }
219
+ /**
220
+ * When the DO is destroyed, cancel all pending requests
221
+ */
222
+ async destroy() {
223
+ this._destroyAbortControllers();
224
+ await super.destroy();
225
+ }
226
+ };
227
+ export {
228
+ AIChatAgent
229
+ };
230
+ //# 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 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 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._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 }\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 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 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 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 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":";;;;;;;;AAMA,SAAS,8BAA8B;AAGvC,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,IAAI;AAAA,EAC9C;AAAA,EAEQ,sBAAsB,SAA0B,SAAoB;AAC1E,SAAK,UAAU,KAAK,UAAU,OAAO,GAAG,OAAO;AAAA,EACjD;AAAA,EAEA,MAAe,UAAU,YAAwB,SAAoB;AACnE,QAAI,OAAO,YAAY,UAAU;AAC/B,UAAI;AACJ,UAAI;AACF,eAAO,KAAK,MAAM,OAAO;AAAA,MAC3B,SAAS,OAAO;AAGd;AAAA,MACF;AACA,UACE,KAAK,SAAS,+BACd,KAAK,KAAK,WAAW,QACrB;AACA,cAAM;AAAA,UACJ;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA;AAAA;AAAA,QAGF,IAAI,KAAK;AACT,cAAM,EAAE,SAAS,IAAI,KAAK,MAAM,IAAc;AA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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 "./index.js";
8
+ import "partyserver";
9
+ import "@modelcontextprotocol/sdk/types.js";
10
+ import "./mcp/client.js";
11
+ import "zod";
12
+ import "@modelcontextprotocol/sdk/client/index.js";
13
+ import "@modelcontextprotocol/sdk/client/sse.js";
14
+ import "./mcp/do-oauth-client-provider.js";
15
+ import "@modelcontextprotocol/sdk/client/auth.js";
16
+ import "@modelcontextprotocol/sdk/shared/auth.js";
17
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
18
+ import "./client.js";
19
+ import "./serializable.js";
8
20
 
9
21
  type GetInitialMessagesOptions = {
10
- agent: string;
11
- name: string;
12
- url: string;
22
+ agent: string;
23
+ name: string;
24
+ url: string;
13
25
  };
14
26
  /**
15
27
  * Options for the useAgentChat hook
16
28
  */
17
- type UseAgentChatOptions<State> = Omit<Parameters<typeof useChat>[0] & {
29
+ type UseAgentChatOptions<State> = Omit<
30
+ Parameters<typeof useChat>[0] & {
18
31
  /** Agent connection from useAgent */
19
32
  agent: ReturnType<typeof useAgent<State>>;
20
- getInitialMessages?: undefined | null | ((options: GetInitialMessagesOptions) => Promise<Message[]>);
21
- }, "fetch">;
33
+ getInitialMessages?:
34
+ | undefined
35
+ | null
36
+ | ((options: GetInitialMessagesOptions) => Promise<Message[]>);
37
+ },
38
+ "fetch"
39
+ >;
22
40
  /**
23
41
  * React hook for building AI chat interfaces using an Agent
24
42
  * @param options Chat options including the agent connection
25
43
  * @returns Chat interface controls and state with added clearHistory method
26
44
  */
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;
45
+ declare function useAgentChat<State = unknown>(
46
+ options: UseAgentChatOptions<State>
47
+ ): {
48
+ /**
49
+ * Set the chat messages and synchronize with the Agent
50
+ * @param messages New messages to set
51
+ */
52
+ setMessages: (messages: Message[]) => void;
53
+ /**
54
+ * Clear chat history on both client and Agent
55
+ */
56
+ clearHistory: () => void;
57
+ messages: ai.UIMessage[];
58
+ error: undefined | Error;
59
+ append: (
60
+ message: Message | ai.CreateMessage,
61
+ chatRequestOptions?: ai.ChatRequestOptions
62
+ ) => Promise<string | null | undefined>;
63
+ reload: (
64
+ chatRequestOptions?: ai.ChatRequestOptions
65
+ ) => Promise<string | null | undefined>;
66
+ stop: () => void;
67
+ experimental_resume: () => void;
68
+ input: string;
69
+ setInput: React.Dispatch<React.SetStateAction<string>>;
70
+ handleInputChange: (
71
+ e:
72
+ | React.ChangeEvent<HTMLInputElement>
73
+ | React.ChangeEvent<HTMLTextAreaElement>
74
+ ) => void;
75
+ handleSubmit: (
76
+ event?: {
77
+ preventDefault?: () => void;
78
+ },
79
+ chatRequestOptions?: ai.ChatRequestOptions
80
+ ) => void;
81
+ metadata?: Object;
82
+ isLoading: boolean;
83
+ status: "submitted" | "streaming" | "ready" | "error";
84
+ data?: ai.JSONValue[];
85
+ setData: (
86
+ data:
87
+ | ai.JSONValue[]
88
+ | undefined
89
+ | ((data: ai.JSONValue[] | undefined) => ai.JSONValue[] | undefined)
90
+ ) => void;
91
+ id: string;
92
+ addToolResult: ({
93
+ toolCallId,
94
+ result,
95
+ }: {
96
+ toolCallId: string;
97
+ result: any;
98
+ }) => void;
58
99
  };
59
100
 
60
101
  export { useAgentChat };