agents 0.0.0-25aeaf2 → 0.0.0-2662748

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 (40) hide show
  1. package/dist/ai-chat-agent.d.ts +28 -2
  2. package/dist/ai-chat-agent.js +99 -105
  3. package/dist/ai-chat-agent.js.map +1 -1
  4. package/dist/ai-react.d.ts +2 -1
  5. package/dist/ai-react.js +0 -2
  6. package/dist/ai-react.js.map +1 -1
  7. package/dist/{chunk-D6UOOELW.js → chunk-BZXOAZUX.js} +11 -11
  8. package/dist/chunk-BZXOAZUX.js.map +1 -0
  9. package/dist/{chunk-25YDMV4H.js → chunk-OYJXQRRH.js} +13 -12
  10. package/dist/chunk-OYJXQRRH.js.map +1 -0
  11. package/dist/{chunk-YFPCCSZO.js → chunk-P3RZJ72N.js} +183 -187
  12. package/dist/chunk-P3RZJ72N.js.map +1 -0
  13. package/dist/{chunk-RN4SNE73.js → chunk-VCSB47AK.js} +10 -27
  14. package/dist/chunk-VCSB47AK.js.map +1 -0
  15. package/dist/client.d.ts +8 -1
  16. package/dist/client.js +1 -2
  17. package/dist/index.d.ts +47 -3
  18. package/dist/index.js +4 -5
  19. package/dist/mcp/client.d.ts +6 -6
  20. package/dist/mcp/client.js +1 -2
  21. package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
  22. package/dist/mcp/do-oauth-client-provider.js +1 -2
  23. package/dist/mcp/index.d.ts +10 -1
  24. package/dist/mcp/index.js +90 -126
  25. package/dist/mcp/index.js.map +1 -1
  26. package/dist/react.d.ts +73 -7
  27. package/dist/react.js +10 -2
  28. package/dist/react.js.map +1 -1
  29. package/dist/schedule.js +0 -2
  30. package/dist/schedule.js.map +1 -1
  31. package/dist/serializable.d.ts +32 -0
  32. package/dist/serializable.js +1 -0
  33. package/package.json +8 -8
  34. package/src/index.ts +86 -78
  35. package/dist/chunk-25YDMV4H.js.map +0 -1
  36. package/dist/chunk-D6UOOELW.js.map +0 -1
  37. package/dist/chunk-HMLY7DHA.js +0 -16
  38. package/dist/chunk-RN4SNE73.js.map +0 -1
  39. package/dist/chunk-YFPCCSZO.js.map +0 -1
  40. /package/dist/{chunk-HMLY7DHA.js.map → serializable.js.map} +0 -0
package/dist/mcp/index.js CHANGED
@@ -1,15 +1,9 @@
1
1
  import {
2
2
  Agent
3
- } from "../chunk-YFPCCSZO.js";
4
- import "../chunk-D6UOOELW.js";
5
- import "../chunk-RN4SNE73.js";
6
- import "../chunk-25YDMV4H.js";
7
- import {
8
- __privateAdd,
9
- __privateGet,
10
- __privateMethod,
11
- __privateSet
12
- } from "../chunk-HMLY7DHA.js";
3
+ } from "../chunk-P3RZJ72N.js";
4
+ import "../chunk-OYJXQRRH.js";
5
+ import "../chunk-BZXOAZUX.js";
6
+ import "../chunk-VCSB47AK.js";
13
7
 
14
8
  // src/mcp/index.ts
15
9
  import { DurableObject } from "cloudflare:workers";
@@ -32,30 +26,31 @@ function corsHeaders(request, corsOptions = {}) {
32
26
  "Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id"
33
27
  };
34
28
  }
29
+ function isDurableObjectNamespace(namespace) {
30
+ return typeof namespace === "object" && namespace !== null && "newUniqueId" in namespace && typeof namespace.newUniqueId === "function" && "idFromName" in namespace && typeof namespace.idFromName === "function";
31
+ }
35
32
  function handleCORS(request, corsOptions) {
36
33
  if (request.method === "OPTIONS") {
37
34
  return new Response(null, { headers: corsHeaders(request, corsOptions) });
38
35
  }
39
36
  return null;
40
37
  }
41
- var _getWebSocket, _started;
42
38
  var McpSSETransport = class {
43
39
  constructor(getWebSocket) {
44
- __privateAdd(this, _getWebSocket);
45
- __privateAdd(this, _started, false);
46
- __privateSet(this, _getWebSocket, getWebSocket);
40
+ this._started = false;
41
+ this._getWebSocket = getWebSocket;
47
42
  }
48
43
  async start() {
49
- if (__privateGet(this, _started)) {
44
+ if (this._started) {
50
45
  throw new Error("Transport already started");
51
46
  }
52
- __privateSet(this, _started, true);
47
+ this._started = true;
53
48
  }
54
49
  async send(message) {
55
- if (!__privateGet(this, _started)) {
50
+ if (!this._started) {
56
51
  throw new Error("Transport not started");
57
52
  }
58
- const websocket = __privateGet(this, _getWebSocket).call(this);
53
+ const websocket = this._getWebSocket();
59
54
  if (!websocket) {
60
55
  throw new Error("WebSocket not connected");
61
56
  }
@@ -70,52 +65,40 @@ var McpSSETransport = class {
70
65
  this.onclose?.();
71
66
  }
72
67
  };
73
- _getWebSocket = new WeakMap();
74
- _started = new WeakMap();
75
- var _getWebSocketForGetRequest, _getWebSocketForMessageID, _notifyResponseIdSent, _started2;
76
68
  var McpStreamableHttpTransport = class {
77
69
  constructor(getWebSocketForMessageID, notifyResponseIdSent) {
78
- // TODO: If there is an open connection to send server-initiated messages
79
- // back, we should use that connection
80
- __privateAdd(this, _getWebSocketForGetRequest);
81
- // Get the appropriate websocket connection for a given message id
82
- __privateAdd(this, _getWebSocketForMessageID);
83
- // Notify the server that a response has been sent for a given message id
84
- // so that it may clean up it's mapping of message ids to connections
85
- // once they are no longer needed
86
- __privateAdd(this, _notifyResponseIdSent);
87
- __privateAdd(this, _started2, false);
88
- __privateSet(this, _getWebSocketForMessageID, getWebSocketForMessageID);
89
- __privateSet(this, _notifyResponseIdSent, notifyResponseIdSent);
90
- __privateSet(this, _getWebSocketForGetRequest, () => null);
70
+ this._started = false;
71
+ this._getWebSocketForMessageID = getWebSocketForMessageID;
72
+ this._notifyResponseIdSent = notifyResponseIdSent;
73
+ this._getWebSocketForGetRequest = () => null;
91
74
  }
92
75
  async start() {
93
- if (__privateGet(this, _started2)) {
76
+ if (this._started) {
94
77
  throw new Error("Transport already started");
95
78
  }
96
- __privateSet(this, _started2, true);
79
+ this._started = true;
97
80
  }
98
81
  async send(message) {
99
- if (!__privateGet(this, _started2)) {
82
+ if (!this._started) {
100
83
  throw new Error("Transport not started");
101
84
  }
102
85
  let websocket = null;
103
86
  if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
104
- websocket = __privateGet(this, _getWebSocketForMessageID).call(this, message.id.toString());
87
+ websocket = this._getWebSocketForMessageID(message.id.toString());
105
88
  if (!websocket) {
106
89
  throw new Error(
107
90
  `Could not find WebSocket for message id: ${message.id}`
108
91
  );
109
92
  }
110
93
  } else if (isJSONRPCRequest(message)) {
111
- websocket = __privateGet(this, _getWebSocketForGetRequest).call(this);
94
+ websocket = this._getWebSocketForGetRequest();
112
95
  } else if (isJSONRPCNotification(message)) {
113
96
  websocket = null;
114
97
  }
115
98
  try {
116
99
  websocket?.send(JSON.stringify(message));
117
100
  if (isJSONRPCResponse(message)) {
118
- __privateGet(this, _notifyResponseIdSent).call(this, message.id.toString());
101
+ this._notifyResponseIdSent(message.id.toString());
119
102
  }
120
103
  } catch (error) {
121
104
  this.onerror?.(error);
@@ -126,28 +109,16 @@ var McpStreamableHttpTransport = class {
126
109
  this.onclose?.();
127
110
  }
128
111
  };
129
- _getWebSocketForGetRequest = new WeakMap();
130
- _getWebSocketForMessageID = new WeakMap();
131
- _notifyResponseIdSent = new WeakMap();
132
- _started2 = new WeakMap();
133
- var _status, _transport, _transportType, _requestIdToConnectionId, _agent, _McpAgent_instances, initialize_fn;
134
- var _McpAgent = class _McpAgent extends DurableObject {
112
+ var McpAgent = class _McpAgent extends DurableObject {
135
113
  constructor(ctx, env) {
136
114
  var _a;
137
115
  super(ctx, env);
138
- __privateAdd(this, _McpAgent_instances);
139
- __privateAdd(this, _status, "zero");
140
- __privateAdd(this, _transport);
141
- __privateAdd(this, _transportType, "unset");
142
- __privateAdd(this, _requestIdToConnectionId, /* @__PURE__ */ new Map());
143
- /**
144
- * Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
145
- * to the outer class: initialState/state/setState/onStateUpdate/sql
146
- */
147
- __privateAdd(this, _agent);
116
+ this._status = "zero";
117
+ this._transportType = "unset";
118
+ this._requestIdToConnectionId = /* @__PURE__ */ new Map();
148
119
  this.initRun = false;
149
120
  const self = this;
150
- __privateSet(this, _agent, new (_a = class extends Agent {
121
+ this._agent = new (_a = class extends Agent {
151
122
  onStateUpdate(state, source) {
152
123
  return self.onStateUpdate(state, source);
153
124
  }
@@ -156,26 +127,26 @@ var _McpAgent = class _McpAgent extends DurableObject {
156
127
  }
157
128
  }, _a.options = {
158
129
  hibernate: true
159
- }, _a)(ctx, env));
130
+ }, _a)(ctx, env);
160
131
  }
161
132
  get mcp() {
162
- return __privateGet(this, _agent).mcp;
133
+ return this._agent.mcp;
163
134
  }
164
135
  get state() {
165
- return __privateGet(this, _agent).state;
136
+ return this._agent.state;
166
137
  }
167
138
  sql(strings, ...values) {
168
- return __privateGet(this, _agent).sql(strings, ...values);
139
+ return this._agent.sql(strings, ...values);
169
140
  }
170
141
  setState(state) {
171
- return __privateGet(this, _agent).setState(state);
142
+ return this._agent.setState(state);
172
143
  }
173
144
  onStateUpdate(state, source) {
174
145
  }
175
146
  async onStart() {
176
147
  var _a;
177
148
  const self = this;
178
- __privateSet(this, _agent, new (_a = class extends Agent {
149
+ this._agent = new (_a = class extends Agent {
179
150
  constructor() {
180
151
  super(...arguments);
181
152
  this.initialState = self.initialState;
@@ -188,22 +159,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
188
159
  }
189
160
  }, _a.options = {
190
161
  hibernate: true
191
- }, _a)(this.ctx, this.env));
162
+ }, _a)(this.ctx, this.env);
192
163
  this.props = await this.ctx.storage.get("props");
193
- __privateSet(this, _transportType, await this.ctx.storage.get(
164
+ this._transportType = await this.ctx.storage.get(
194
165
  "transportType"
195
- ));
166
+ );
196
167
  await this._init(this.props);
197
168
  const server = await this.server;
198
- if (__privateGet(this, _transportType) === "sse") {
199
- __privateSet(this, _transport, new McpSSETransport(() => this.getWebSocket()));
200
- await server.connect(__privateGet(this, _transport));
201
- } else if (__privateGet(this, _transportType) === "streamable-http") {
202
- __privateSet(this, _transport, new McpStreamableHttpTransport(
169
+ if (this._transportType === "sse") {
170
+ this._transport = new McpSSETransport(() => this.getWebSocket());
171
+ await server.connect(this._transport);
172
+ } else if (this._transportType === "streamable-http") {
173
+ this._transport = new McpStreamableHttpTransport(
203
174
  (id) => this.getWebSocketForResponseID(id),
204
- (id) => __privateGet(this, _requestIdToConnectionId).delete(id)
205
- ));
206
- await server.connect(__privateGet(this, _transport));
175
+ (id) => this._requestIdToConnectionId.delete(id)
176
+ );
177
+ await server.connect(this._transport);
207
178
  }
208
179
  }
209
180
  async _init(props) {
@@ -223,10 +194,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
223
194
  async isInitialized() {
224
195
  return await this.ctx.storage.get("initialized") === true;
225
196
  }
197
+ async _initialize() {
198
+ await this.ctx.blockConcurrencyWhile(async () => {
199
+ this._status = "starting";
200
+ await this.onStart();
201
+ this._status = "started";
202
+ });
203
+ }
226
204
  // Allow the worker to fetch a websocket connection to the agent
227
205
  async fetch(request) {
228
- if (__privateGet(this, _status) !== "started") {
229
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
206
+ if (this._status !== "started") {
207
+ await this._initialize();
230
208
  }
231
209
  if (request.headers.get("Upgrade") !== "websocket") {
232
210
  return new Response("Expected WebSocket Upgrade request", {
@@ -243,24 +221,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
243
221
  return new Response("Websocket already connected", { status: 400 });
244
222
  }
245
223
  await this.ctx.storage.put("transportType", "sse");
246
- __privateSet(this, _transportType, "sse");
247
- if (!__privateGet(this, _transport)) {
248
- __privateSet(this, _transport, new McpSSETransport(() => this.getWebSocket()));
249
- await server.connect(__privateGet(this, _transport));
224
+ this._transportType = "sse";
225
+ if (!this._transport) {
226
+ this._transport = new McpSSETransport(() => this.getWebSocket());
227
+ await server.connect(this._transport);
250
228
  }
251
- return __privateGet(this, _agent).fetch(request);
229
+ return this._agent.fetch(request);
252
230
  }
253
231
  case "/streamable-http": {
254
- if (!__privateGet(this, _transport)) {
255
- __privateSet(this, _transport, new McpStreamableHttpTransport(
232
+ if (!this._transport) {
233
+ this._transport = new McpStreamableHttpTransport(
256
234
  (id) => this.getWebSocketForResponseID(id),
257
- (id) => __privateGet(this, _requestIdToConnectionId).delete(id)
258
- ));
259
- await server.connect(__privateGet(this, _transport));
235
+ (id) => this._requestIdToConnectionId.delete(id)
236
+ );
237
+ await server.connect(this._transport);
260
238
  }
261
239
  await this.ctx.storage.put("transportType", "streamable-http");
262
- __privateSet(this, _transportType, "streamable-http");
263
- return __privateGet(this, _agent).fetch(request);
240
+ this._transportType = "streamable-http";
241
+ return this._agent.fetch(request);
264
242
  }
265
243
  default:
266
244
  return new Response(
@@ -279,19 +257,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
279
257
  return websockets[0];
280
258
  }
281
259
  getWebSocketForResponseID(id) {
282
- const connectionId = __privateGet(this, _requestIdToConnectionId).get(id);
260
+ const connectionId = this._requestIdToConnectionId.get(id);
283
261
  if (connectionId === void 0) {
284
262
  return null;
285
263
  }
286
- return __privateGet(this, _agent).getConnection(connectionId) ?? null;
264
+ return this._agent.getConnection(connectionId) ?? null;
287
265
  }
288
266
  // All messages received here. This is currently never called
289
267
  async onMessage(connection, event) {
290
- if (__privateGet(this, _transportType) !== "streamable-http") {
268
+ if (this._transportType !== "streamable-http") {
291
269
  const err = new Error(
292
270
  "Internal Server Error: Expected streamable-http protocol"
293
271
  );
294
- __privateGet(this, _transport)?.onerror?.(err);
272
+ this._transport?.onerror?.(err);
295
273
  return;
296
274
  }
297
275
  let message;
@@ -299,21 +277,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
299
277
  const data = typeof event === "string" ? event : new TextDecoder().decode(event);
300
278
  message = JSONRPCMessageSchema.parse(JSON.parse(data));
301
279
  } catch (error) {
302
- __privateGet(this, _transport)?.onerror?.(error);
280
+ this._transport?.onerror?.(error);
303
281
  return;
304
282
  }
305
283
  if (isJSONRPCRequest(message)) {
306
- __privateGet(this, _requestIdToConnectionId).set(message.id.toString(), connection.id);
284
+ this._requestIdToConnectionId.set(message.id.toString(), connection.id);
307
285
  }
308
- __privateGet(this, _transport)?.onmessage?.(message);
286
+ this._transport?.onmessage?.(message);
309
287
  }
310
288
  // All messages received over SSE after the initial connection has been established
311
289
  // will be passed here
312
290
  async onSSEMcpMessage(sessionId, request) {
313
- if (__privateGet(this, _status) !== "started") {
314
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
291
+ if (this._status !== "started") {
292
+ await this._initialize();
315
293
  }
316
- if (__privateGet(this, _transportType) !== "sse") {
294
+ if (this._transportType !== "sse") {
317
295
  return new Error("Internal Server Error: Expected SSE protocol");
318
296
  }
319
297
  try {
@@ -322,36 +300,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
322
300
  try {
323
301
  parsedMessage = JSONRPCMessageSchema.parse(message);
324
302
  } catch (error) {
325
- __privateGet(this, _transport)?.onerror?.(error);
303
+ this._transport?.onerror?.(error);
326
304
  throw error;
327
305
  }
328
- __privateGet(this, _transport)?.onmessage?.(parsedMessage);
306
+ this._transport?.onmessage?.(parsedMessage);
329
307
  return null;
330
308
  } catch (error) {
331
309
  console.error("Error forwarding message to SSE:", error);
332
- __privateGet(this, _transport)?.onerror?.(error);
310
+ this._transport?.onerror?.(error);
333
311
  return error;
334
312
  }
335
313
  }
336
314
  // Delegate all websocket events to the underlying agent
337
315
  async webSocketMessage(ws, event) {
338
- if (__privateGet(this, _status) !== "started") {
339
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
316
+ if (this._status !== "started") {
317
+ await this._initialize();
340
318
  }
341
- return await __privateGet(this, _agent).webSocketMessage(ws, event);
319
+ return await this._agent.webSocketMessage(ws, event);
342
320
  }
343
321
  // WebSocket event handlers for hibernation support
344
322
  async webSocketError(ws, error) {
345
- if (__privateGet(this, _status) !== "started") {
346
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
323
+ if (this._status !== "started") {
324
+ await this._initialize();
347
325
  }
348
- return await __privateGet(this, _agent).webSocketError(ws, error);
326
+ return await this._agent.webSocketError(ws, error);
349
327
  }
350
328
  async webSocketClose(ws, code, reason, wasClean) {
351
- if (__privateGet(this, _status) !== "started") {
352
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
329
+ if (this._status !== "started") {
330
+ await this._initialize();
353
331
  }
354
- return await __privateGet(this, _agent).webSocketClose(ws, code, reason, wasClean);
332
+ return await this._agent.webSocketClose(ws, code, reason, wasClean);
355
333
  }
356
334
  static mount(path, {
357
335
  binding = "MCP_OBJECT",
@@ -381,7 +359,7 @@ var _McpAgent = class _McpAgent extends DurableObject {
381
359
  );
382
360
  return new Response("Invalid binding", { status: 500 });
383
361
  }
384
- if (bindingValue.toString() !== "[object DurableObjectNamespace]") {
362
+ if (!isDurableObjectNamespace(bindingValue)) {
385
363
  return new Response("Invalid binding", { status: 500 });
386
364
  }
387
365
  const namespace = bindingValue;
@@ -546,7 +524,7 @@ data: ${JSON.stringify(result.data)}
546
524
  );
547
525
  return new Response("Invalid binding", { status: 500 });
548
526
  }
549
- if (bindingValue.toString() !== "[object DurableObjectNamespace]") {
527
+ if (!isDurableObjectNamespace(bindingValue)) {
550
528
  return new Response("Invalid binding", { status: 500 });
551
529
  }
552
530
  const namespace = bindingValue;
@@ -798,20 +776,6 @@ data: ${JSON.stringify(result.data)}
798
776
  };
799
777
  }
800
778
  };
801
- _status = new WeakMap();
802
- _transport = new WeakMap();
803
- _transportType = new WeakMap();
804
- _requestIdToConnectionId = new WeakMap();
805
- _agent = new WeakMap();
806
- _McpAgent_instances = new WeakSet();
807
- initialize_fn = async function() {
808
- await this.ctx.blockConcurrencyWhile(async () => {
809
- __privateSet(this, _status, "starting");
810
- await this.onStart();
811
- __privateSet(this, _status, "started");
812
- });
813
- };
814
- var McpAgent = _McpAgent;
815
779
  export {
816
780
  McpAgent
817
781
  };