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.
package/src/index.ts CHANGED
@@ -12,11 +12,10 @@ import { parseCronExpression } from "cron-schedule";
12
12
  import { nanoid } from "nanoid";
13
13
 
14
14
  import { AsyncLocalStorage } from "node:async_hooks";
15
+ import { MCPClientManager } from "./mcp/client";
15
16
 
16
17
  export type { Connection, WSMessage, ConnectionContext } from "partyserver";
17
18
 
18
- import { WorkflowEntrypoint as CFWorkflowEntrypoint } from "cloudflare:workers";
19
-
20
19
  /**
21
20
  * RPC request message from client
22
21
  */
@@ -119,11 +118,6 @@ export function unstable_callable(metadata: CallableMetadata = {}) {
119
118
  };
120
119
  }
121
120
 
122
- /**
123
- * A class for creating workflow entry points that can be used with Cloudflare Workers
124
- */
125
- export class WorkflowEntrypoint extends CFWorkflowEntrypoint {}
126
-
127
121
  /**
128
122
  * Represents a scheduled task within an Agent
129
123
  * @template T Type of the payload data
@@ -170,12 +164,36 @@ const STATE_WAS_CHANGED = "cf_state_was_changed";
170
164
 
171
165
  const DEFAULT_STATE = {} as unknown;
172
166
 
173
- export const unstable_context = new AsyncLocalStorage<{
167
+ const agentContext = new AsyncLocalStorage<{
174
168
  agent: Agent<unknown>;
175
169
  connection: Connection | undefined;
176
170
  request: Request | undefined;
177
171
  }>();
178
172
 
173
+ export function getCurrentAgent<
174
+ T extends Agent<unknown, unknown> = Agent<unknown, unknown>,
175
+ >(): {
176
+ agent: T | undefined;
177
+ connection: Connection | undefined;
178
+ request: Request<unknown, CfProperties<unknown>> | undefined;
179
+ } {
180
+ const store = agentContext.getStore() as
181
+ | {
182
+ agent: T;
183
+ connection: Connection | undefined;
184
+ request: Request<unknown, CfProperties<unknown>> | undefined;
185
+ }
186
+ | undefined;
187
+ if (!store) {
188
+ return {
189
+ agent: undefined,
190
+ connection: undefined,
191
+ request: undefined,
192
+ };
193
+ }
194
+ return store;
195
+ }
196
+
179
197
  /**
180
198
  * Base class for creating Agent implementations
181
199
  * @template Env Environment type containing bindings
@@ -184,6 +202,11 @@ export const unstable_context = new AsyncLocalStorage<{
184
202
  export class Agent<Env, State = unknown> extends Server<Env> {
185
203
  #state = DEFAULT_STATE as State;
186
204
 
205
+ #ParentClass: typeof Agent<Env, State> =
206
+ Object.getPrototypeOf(this).constructor;
207
+
208
+ mcp: MCPClientManager = new MCPClientManager(this.#ParentClass.name, "0.0.1");
209
+
187
210
  /**
188
211
  * Initial state for the Agent
189
212
  * Override to provide default state values
@@ -300,7 +323,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
300
323
 
301
324
  const _onMessage = this.onMessage.bind(this);
302
325
  this.onMessage = async (connection: Connection, message: WSMessage) => {
303
- return unstable_context.run(
326
+ return agentContext.run(
304
327
  { agent: this, connection, request: undefined },
305
328
  async () => {
306
329
  if (typeof message !== "string") {
@@ -378,7 +401,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
378
401
  this.onConnect = (connection: Connection, ctx: ConnectionContext) => {
379
402
  // TODO: This is a hack to ensure the state is sent after the connection is established
380
403
  // must fix this
381
- return unstable_context.run(
404
+ return agentContext.run(
382
405
  { agent: this, connection, request: ctx.request },
383
406
  async () => {
384
407
  setTimeout(() => {
@@ -415,8 +438,8 @@ export class Agent<Env, State = unknown> extends Server<Env> {
415
438
  source !== "server" ? [source.id] : []
416
439
  );
417
440
  return this.#tryCatch(() => {
418
- const { connection, request } = unstable_context.getStore() || {};
419
- return unstable_context.run(
441
+ const { connection, request } = agentContext.getStore() || {};
442
+ return agentContext.run(
420
443
  { agent: this, connection, request },
421
444
  async () => {
422
445
  return this.onStateUpdate(state, source);
@@ -447,7 +470,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
447
470
  * @param email Email message to process
448
471
  */
449
472
  onEmail(email: ForwardableEmailMessage) {
450
- return unstable_context.run(
473
+ return agentContext.run(
451
474
  { agent: this, connection: undefined, request: undefined },
452
475
  async () => {
453
476
  console.error("onEmail not implemented");
@@ -697,7 +720,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
697
720
  console.error(`callback ${row.callback} not found`);
698
721
  continue;
699
722
  }
700
- await unstable_context.run(
723
+ await agentContext.run(
701
724
  { agent: this, connection: undefined, request: undefined },
702
725
  async () => {
703
726
  try {
@@ -856,7 +879,7 @@ export async function routeAgentEmail<Env>(
856
879
  * @param options Options for Agent creation
857
880
  * @returns Promise resolving to an Agent instance stub
858
881
  */
859
- export function getAgentByName<Env, T extends Agent<Env>>(
882
+ export async function getAgentByName<Env, T extends Agent<Env>>(
860
883
  namespace: AgentNamespace<T>,
861
884
  name: string,
862
885
  options?: {