agents 0.0.0-f6c26e4 → 0.0.0-f7bd395

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 (50) hide show
  1. package/README.md +136 -6
  2. package/dist/ai-chat-agent.d.ts +12 -9
  3. package/dist/ai-chat-agent.js +142 -59
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/ai-chat-v5-migration.d.ts +152 -0
  6. package/dist/ai-chat-v5-migration.js +19 -0
  7. package/dist/ai-chat-v5-migration.js.map +1 -0
  8. package/dist/ai-react.d.ts +62 -71
  9. package/dist/ai-react.js +144 -37
  10. package/dist/ai-react.js.map +1 -1
  11. package/dist/ai-types.d.ts +36 -19
  12. package/dist/ai-types.js +6 -0
  13. package/dist/chunk-AVYJQSLW.js +17 -0
  14. package/dist/chunk-AVYJQSLW.js.map +1 -0
  15. package/dist/{chunk-5YIRLLUX.js → chunk-IJPBZOSS.js} +137 -105
  16. package/dist/chunk-IJPBZOSS.js.map +1 -0
  17. package/dist/{chunk-PVQZBKN7.js → chunk-LL2AFX7V.js} +5 -2
  18. package/dist/chunk-LL2AFX7V.js.map +1 -0
  19. package/dist/{chunk-KUH345EY.js → chunk-QEVM4BVL.js} +5 -5
  20. package/dist/chunk-QEVM4BVL.js.map +1 -0
  21. package/dist/chunk-UJVEAURM.js +150 -0
  22. package/dist/chunk-UJVEAURM.js.map +1 -0
  23. package/dist/{chunk-MW5BQ2FW.js → chunk-VYENMKFS.js} +163 -20
  24. package/dist/chunk-VYENMKFS.js.map +1 -0
  25. package/dist/client-CcIORE73.d.ts +4607 -0
  26. package/dist/client.js +2 -1
  27. package/dist/index.d.ts +557 -32
  28. package/dist/index.js +7 -4
  29. package/dist/mcp/client.d.ts +9 -1053
  30. package/dist/mcp/client.js +1 -1
  31. package/dist/mcp/do-oauth-client-provider.d.ts +1 -0
  32. package/dist/mcp/do-oauth-client-provider.js +1 -1
  33. package/dist/mcp/index.d.ts +66 -53
  34. package/dist/mcp/index.js +850 -604
  35. package/dist/mcp/index.js.map +1 -1
  36. package/dist/observability/index.d.ts +46 -12
  37. package/dist/observability/index.js +5 -4
  38. package/dist/react.d.ts +7 -3
  39. package/dist/react.js +7 -5
  40. package/dist/react.js.map +1 -1
  41. package/dist/schedule.d.ts +83 -9
  42. package/dist/schedule.js +15 -2
  43. package/dist/schedule.js.map +1 -1
  44. package/package.json +19 -8
  45. package/src/index.ts +192 -123
  46. package/dist/chunk-5YIRLLUX.js.map +0 -1
  47. package/dist/chunk-KUH345EY.js.map +0 -1
  48. package/dist/chunk-MW5BQ2FW.js.map +0 -1
  49. package/dist/chunk-PVQZBKN7.js.map +0 -1
  50. package/dist/index-BIJvkfYt.d.ts +0 -614
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  MCPClientManager
3
- } from "./chunk-MW5BQ2FW.js";
3
+ } from "./chunk-VYENMKFS.js";
4
4
  import {
5
5
  DurableObjectOAuthClientProvider
6
- } from "./chunk-PVQZBKN7.js";
6
+ } from "./chunk-LL2AFX7V.js";
7
7
  import {
8
8
  camelCaseToKebabCase
9
- } from "./chunk-KUH345EY.js";
9
+ } from "./chunk-QEVM4BVL.js";
10
10
 
11
11
  // src/index.ts
12
12
  import { AsyncLocalStorage } from "async_hooks";
@@ -19,13 +19,13 @@ import {
19
19
  routePartykitRequest
20
20
  } from "partyserver";
21
21
  function isRPCRequest(msg) {
22
- return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "rpc" && "id" in msg && typeof msg.id === "string" && "method" in msg && typeof msg.method === "string" && "args" in msg && Array.isArray(msg.args);
22
+ return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "rpc" /* RPC */ && "id" in msg && typeof msg.id === "string" && "method" in msg && typeof msg.method === "string" && "args" in msg && Array.isArray(msg.args);
23
23
  }
24
24
  function isStateUpdateMessage(msg) {
25
- return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "cf_agent_state" && "state" in msg;
25
+ return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "cf_agent_state" /* CF_AGENT_STATE */ && "state" in msg;
26
26
  }
27
27
  var callableMetadata = /* @__PURE__ */ new Map();
28
- function unstable_callable(metadata = {}) {
28
+ function callable(metadata = {}) {
29
29
  return function callableDecorator(target, context) {
30
30
  if (!callableMetadata.has(target)) {
31
31
  callableMetadata.set(target, metadata);
@@ -33,6 +33,16 @@ function unstable_callable(metadata = {}) {
33
33
  return target;
34
34
  };
35
35
  }
36
+ var didWarnAboutUnstableCallable = false;
37
+ var unstable_callable = (metadata = {}) => {
38
+ if (!didWarnAboutUnstableCallable) {
39
+ didWarnAboutUnstableCallable = true;
40
+ console.warn(
41
+ "unstable_callable is deprecated, use callable instead. unstable_callable will be removed in the next major version."
42
+ );
43
+ }
44
+ callable(metadata);
45
+ };
36
46
  function getNextCronTime(cron) {
37
47
  const interval = parseCronExpression(cron);
38
48
  return interval.getNextDate();
@@ -55,7 +65,10 @@ function getCurrentAgent() {
55
65
  }
56
66
  function withAgentContext(method) {
57
67
  return function(...args) {
58
- const { connection, request, email } = getCurrentAgent();
68
+ const { connection, request, email, agent } = getCurrentAgent();
69
+ if (agent === this) {
70
+ return method.apply(this, args);
71
+ }
59
72
  return agentContext.run({ agent: this, connection, request, email }, () => {
60
73
  return method.apply(this, args);
61
74
  });
@@ -110,7 +123,10 @@ var _Agent = class _Agent extends Server {
110
123
  {
111
124
  displayMessage: `Schedule ${row.id} executed`,
112
125
  id: nanoid(),
113
- payload: row,
126
+ payload: {
127
+ callback: row.callback,
128
+ id: row.id
129
+ },
114
130
  timestamp: Date.now(),
115
131
  type: "schedule:execute"
116
132
  },
@@ -190,7 +206,7 @@ var _Agent = class _Agent extends Server {
190
206
  this.broadcast(
191
207
  JSON.stringify({
192
208
  mcp: this.getMcpServers(),
193
- type: "cf_agent_mcp_servers"
209
+ type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
194
210
  })
195
211
  );
196
212
  return new Response("<script>window.close();</script>", {
@@ -242,10 +258,8 @@ var _Agent = class _Agent extends Server {
242
258
  displayMessage: `RPC call to ${method}`,
243
259
  id: nanoid(),
244
260
  payload: {
245
- args,
246
261
  method,
247
- streaming: metadata?.streaming,
248
- success: true
262
+ streaming: metadata?.streaming
249
263
  },
250
264
  timestamp: Date.now(),
251
265
  type: "rpc"
@@ -257,7 +271,7 @@ var _Agent = class _Agent extends Server {
257
271
  id,
258
272
  result,
259
273
  success: true,
260
- type: "rpc"
274
+ type: "rpc" /* RPC */
261
275
  };
262
276
  connection.send(JSON.stringify(response));
263
277
  } catch (e) {
@@ -265,7 +279,7 @@ var _Agent = class _Agent extends Server {
265
279
  error: e instanceof Error ? e.message : "Unknown error occurred",
266
280
  id: parsed.id,
267
281
  success: false,
268
- type: "rpc"
282
+ type: "rpc" /* RPC */
269
283
  };
270
284
  connection.send(JSON.stringify(response));
271
285
  console.error("RPC error:", e);
@@ -280,41 +294,39 @@ var _Agent = class _Agent extends Server {
280
294
  this.onConnect = (connection, ctx2) => {
281
295
  return agentContext.run(
282
296
  { agent: this, connection, request: ctx2.request, email: void 0 },
283
- async () => {
284
- setTimeout(() => {
285
- if (this.state) {
286
- connection.send(
287
- JSON.stringify({
288
- state: this.state,
289
- type: "cf_agent_state"
290
- })
291
- );
292
- }
297
+ () => {
298
+ if (this.state) {
293
299
  connection.send(
294
300
  JSON.stringify({
295
- mcp: this.getMcpServers(),
296
- type: "cf_agent_mcp_servers"
301
+ state: this.state,
302
+ type: "cf_agent_state" /* CF_AGENT_STATE */
297
303
  })
298
304
  );
299
- this.observability?.emit(
300
- {
301
- displayMessage: "Connection established",
302
- id: nanoid(),
303
- payload: {
304
- connectionId: connection.id
305
- },
306
- timestamp: Date.now(),
307
- type: "connect"
305
+ }
306
+ connection.send(
307
+ JSON.stringify({
308
+ mcp: this.getMcpServers(),
309
+ type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
310
+ })
311
+ );
312
+ this.observability?.emit(
313
+ {
314
+ displayMessage: "Connection established",
315
+ id: nanoid(),
316
+ payload: {
317
+ connectionId: connection.id
308
318
  },
309
- this.ctx
310
- );
311
- return this._tryCatch(() => _onConnect(connection, ctx2));
312
- }, 20);
319
+ timestamp: Date.now(),
320
+ type: "connect"
321
+ },
322
+ this.ctx
323
+ );
324
+ return this._tryCatch(() => _onConnect(connection, ctx2));
313
325
  }
314
326
  );
315
327
  };
316
328
  const _onStart = this.onStart.bind(this);
317
- this.onStart = async () => {
329
+ this.onStart = async (props) => {
318
330
  return agentContext.run(
319
331
  {
320
332
  agent: this,
@@ -323,13 +335,19 @@ var _Agent = class _Agent extends Server {
323
335
  email: void 0
324
336
  },
325
337
  async () => {
326
- const servers = this.sql`
338
+ await this._tryCatch(() => {
339
+ const servers = this.sql`
327
340
  SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
328
341
  `;
329
- if (servers && Array.isArray(servers) && servers.length > 0) {
330
- Promise.allSettled(
331
- servers.map((server) => {
332
- return this._connectToMcpServerInternal(
342
+ this.broadcast(
343
+ JSON.stringify({
344
+ mcp: this.getMcpServers(),
345
+ type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
346
+ })
347
+ );
348
+ if (servers && Array.isArray(servers) && servers.length > 0) {
349
+ servers.forEach((server) => {
350
+ this._connectToMcpServerInternal(
333
351
  server.name,
334
352
  server.server_url,
335
353
  server.callback_url,
@@ -338,18 +356,29 @@ var _Agent = class _Agent extends Server {
338
356
  id: server.id,
339
357
  oauthClientId: server.client_id ?? void 0
340
358
  }
341
- );
342
- })
343
- ).then((_results) => {
344
- this.broadcast(
345
- JSON.stringify({
346
- mcp: this.getMcpServers(),
347
- type: "cf_agent_mcp_servers"
348
- })
349
- );
350
- });
351
- }
352
- await this._tryCatch(() => _onStart());
359
+ ).then(() => {
360
+ this.broadcast(
361
+ JSON.stringify({
362
+ mcp: this.getMcpServers(),
363
+ type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
364
+ })
365
+ );
366
+ }).catch((error) => {
367
+ console.error(
368
+ `Error connecting to MCP server: ${server.name} (${server.server_url})`,
369
+ error
370
+ );
371
+ this.broadcast(
372
+ JSON.stringify({
373
+ mcp: this.getMcpServers(),
374
+ type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
375
+ })
376
+ );
377
+ });
378
+ });
379
+ }
380
+ return _onStart(props);
381
+ });
353
382
  }
354
383
  );
355
384
  };
@@ -400,7 +429,6 @@ var _Agent = class _Agent extends Server {
400
429
  }
401
430
  }
402
431
  _setStateInternal(state, source = "server") {
403
- const previousState = this._state;
404
432
  this._state = state;
405
433
  this.sql`
406
434
  INSERT OR REPLACE INTO cf_agents_state (id, state)
@@ -413,7 +441,7 @@ var _Agent = class _Agent extends Server {
413
441
  this.broadcast(
414
442
  JSON.stringify({
415
443
  state,
416
- type: "cf_agent_state"
444
+ type: "cf_agent_state" /* CF_AGENT_STATE */
417
445
  }),
418
446
  source !== "server" ? [source.id] : []
419
447
  );
@@ -426,10 +454,7 @@ var _Agent = class _Agent extends Server {
426
454
  {
427
455
  displayMessage: "State updated",
428
456
  id: nanoid(),
429
- payload: {
430
- previousState,
431
- state
432
- },
457
+ payload: {},
433
458
  timestamp: Date.now(),
434
459
  type: "state:update"
435
460
  },
@@ -546,28 +571,22 @@ var _Agent = class _Agent extends Server {
546
571
  while (proto && proto !== Object.prototype && depth < 10) {
547
572
  const methodNames = Object.getOwnPropertyNames(proto);
548
573
  for (const methodName of methodNames) {
549
- if (baseMethods.has(methodName) || methodName.startsWith("_") || typeof this[methodName] !== "function") {
574
+ const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
575
+ if (baseMethods.has(methodName) || methodName.startsWith("_") || !descriptor || !!descriptor.get || typeof descriptor.value !== "function") {
550
576
  continue;
551
577
  }
552
- if (!baseMethods.has(methodName)) {
553
- const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
554
- if (descriptor && typeof descriptor.value === "function") {
555
- const wrappedFunction = withAgentContext(
556
- // biome-ignore lint/suspicious/noExplicitAny: I can't typescript
557
- this[methodName]
558
- // biome-ignore lint/suspicious/noExplicitAny: I can't typescript
559
- );
560
- if (this._isCallable(methodName)) {
561
- callableMetadata.set(
562
- wrappedFunction,
563
- callableMetadata.get(
564
- this[methodName]
565
- )
566
- );
567
- }
568
- this.constructor.prototype[methodName] = wrappedFunction;
569
- }
578
+ const wrappedFunction = withAgentContext(
579
+ // biome-ignore lint/suspicious/noExplicitAny: I can't typescript
580
+ this[methodName]
581
+ // biome-ignore lint/suspicious/noExplicitAny: I can't typescript
582
+ );
583
+ if (this._isCallable(methodName)) {
584
+ callableMetadata.set(
585
+ wrappedFunction,
586
+ callableMetadata.get(this[methodName])
587
+ );
570
588
  }
589
+ this.constructor.prototype[methodName] = wrappedFunction;
571
590
  }
572
591
  proto = Object.getPrototypeOf(proto);
573
592
  depth++;
@@ -627,7 +646,6 @@ var _Agent = class _Agent extends Server {
627
646
  }
628
647
  this._flushingQueue = true;
629
648
  while (true) {
630
- const executed = [];
631
649
  const result = this.sql`
632
650
  SELECT * FROM cf_agents_queues
633
651
  ORDER BY created_at ASC
@@ -651,13 +669,10 @@ var _Agent = class _Agent extends Server {
651
669
  },
652
670
  async () => {
653
671
  await callback.bind(this)(JSON.parse(row.payload), row);
654
- executed.push(row.id);
672
+ await this.dequeue(row.id);
655
673
  }
656
674
  );
657
675
  }
658
- for (const id of executed) {
659
- await this.dequeue(id);
660
- }
661
676
  }
662
677
  this._flushingQueue = false;
663
678
  }
@@ -718,7 +733,10 @@ var _Agent = class _Agent extends Server {
718
733
  {
719
734
  displayMessage: `Schedule ${schedule.id} created`,
720
735
  id: nanoid(),
721
- payload: schedule,
736
+ payload: {
737
+ callback,
738
+ id
739
+ },
722
740
  timestamp: Date.now(),
723
741
  type: "schedule:create"
724
742
  },
@@ -853,7 +871,10 @@ var _Agent = class _Agent extends Server {
853
871
  {
854
872
  displayMessage: `Schedule ${id} cancelled`,
855
873
  id: nanoid(),
856
- payload: schedule,
874
+ payload: {
875
+ callback: schedule.callback,
876
+ id: schedule.id
877
+ },
857
878
  timestamp: Date.now(),
858
879
  type: "schedule:cancel"
859
880
  },
@@ -866,9 +887,9 @@ var _Agent = class _Agent extends Server {
866
887
  }
867
888
  async _scheduleNextAlarm() {
868
889
  const result = this.sql`
869
- SELECT time FROM cf_agents_schedules
890
+ SELECT time FROM cf_agents_schedules
870
891
  WHERE time > ${Math.floor(Date.now() / 1e3)}
871
- ORDER BY time ASC
892
+ ORDER BY time ASC
872
893
  LIMIT 1
873
894
  `;
874
895
  if (!result) return;
@@ -939,7 +960,7 @@ var _Agent = class _Agent extends Server {
939
960
  this.broadcast(
940
961
  JSON.stringify({
941
962
  mcp: this.getMcpServers(),
942
- type: "cf_agent_mcp_servers"
963
+ type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
943
964
  })
944
965
  );
945
966
  return result;
@@ -992,7 +1013,7 @@ var _Agent = class _Agent extends Server {
992
1013
  this.broadcast(
993
1014
  JSON.stringify({
994
1015
  mcp: this.getMcpServers(),
995
- type: "cf_agent_mcp_servers"
1016
+ type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
996
1017
  })
997
1018
  );
998
1019
  }
@@ -1120,25 +1141,35 @@ function createAddressBasedEmailResolver(defaultAgentName) {
1120
1141
  function createCatchAllEmailResolver(agentName, agentId) {
1121
1142
  return async () => ({ agentName, agentId });
1122
1143
  }
1144
+ var agentMapCache = /* @__PURE__ */ new WeakMap();
1123
1145
  async function routeAgentEmail(email, env, options) {
1124
1146
  const routingInfo = await options.resolver(email, env);
1125
1147
  if (!routingInfo) {
1126
1148
  console.warn("No routing information found for email, dropping message");
1127
1149
  return;
1128
1150
  }
1129
- const namespaceBinding = env[routingInfo.agentName];
1130
- if (!namespaceBinding) {
1131
- throw new Error(
1132
- `Agent namespace '${routingInfo.agentName}' not found in environment`
1133
- );
1151
+ if (!agentMapCache.has(env)) {
1152
+ const map = {};
1153
+ for (const [key, value] of Object.entries(env)) {
1154
+ if (value && typeof value === "object" && "idFromName" in value && typeof value.idFromName === "function") {
1155
+ map[key] = value;
1156
+ map[camelCaseToKebabCase(key)] = value;
1157
+ }
1158
+ }
1159
+ agentMapCache.set(env, map);
1134
1160
  }
1135
- if (typeof namespaceBinding !== "object" || !("idFromName" in namespaceBinding) || typeof namespaceBinding.idFromName !== "function") {
1161
+ const agentMap = agentMapCache.get(env);
1162
+ const namespace = agentMap[routingInfo.agentName];
1163
+ if (!namespace) {
1164
+ const availableAgents = Object.keys(agentMap).filter((key) => !key.includes("-")).join(", ");
1136
1165
  throw new Error(
1137
- `Environment binding '${routingInfo.agentName}' is not an AgentNamespace (found: ${typeof namespaceBinding})`
1166
+ `Agent namespace '${routingInfo.agentName}' not found in environment. Available agents: ${availableAgents}`
1138
1167
  );
1139
1168
  }
1140
- const namespace = namespaceBinding;
1141
- const agent = await getAgentByName(namespace, routingInfo.agentId);
1169
+ const agent = await getAgentByName(
1170
+ namespace,
1171
+ routingInfo.agentId
1172
+ );
1142
1173
  const serialisableEmail = {
1143
1174
  getRaw: async () => {
1144
1175
  const reader = email.raw.getReader();
@@ -1200,7 +1231,7 @@ var StreamingResponse = class {
1200
1231
  id: this._id,
1201
1232
  result: chunk,
1202
1233
  success: true,
1203
- type: "rpc"
1234
+ type: "rpc" /* RPC */
1204
1235
  };
1205
1236
  this._connection.send(JSON.stringify(response));
1206
1237
  }
@@ -1218,7 +1249,7 @@ var StreamingResponse = class {
1218
1249
  id: this._id,
1219
1250
  result: finalChunk,
1220
1251
  success: true,
1221
- type: "rpc"
1252
+ type: "rpc" /* RPC */
1222
1253
  };
1223
1254
  this._connection.send(JSON.stringify(response));
1224
1255
  }
@@ -1250,6 +1281,7 @@ function isLocalMode() {
1250
1281
 
1251
1282
  export {
1252
1283
  genericObservability,
1284
+ callable,
1253
1285
  unstable_callable,
1254
1286
  getCurrentAgent,
1255
1287
  Agent,
@@ -1261,4 +1293,4 @@ export {
1261
1293
  getAgentByName,
1262
1294
  StreamingResponse
1263
1295
  };
1264
- //# sourceMappingURL=chunk-5YIRLLUX.js.map
1296
+ //# sourceMappingURL=chunk-IJPBZOSS.js.map