agents 0.0.0-579b228 → 0.0.0-57c0db2
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/README.md +234 -6
- package/dist/_esm-LV5FJ3HK.js +3922 -0
- package/dist/_esm-LV5FJ3HK.js.map +1 -0
- package/dist/ai-chat-agent.d.ts +12 -9
- package/dist/ai-chat-agent.js +151 -59
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.d.ts +152 -0
- package/dist/ai-chat-v5-migration.js +20 -0
- package/dist/ai-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +68 -71
- package/dist/ai-react.js +178 -37
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +37 -19
- package/dist/ai-types.js +7 -0
- package/dist/ccip-CMBYN64O.js +15 -0
- package/dist/ccip-CMBYN64O.js.map +1 -0
- package/dist/chunk-5Y6BEZDY.js +276 -0
- package/dist/chunk-5Y6BEZDY.js.map +1 -0
- package/dist/chunk-BER7KXUJ.js +18 -0
- package/dist/chunk-BER7KXUJ.js.map +1 -0
- package/dist/{chunk-CKHRUXM5.js → chunk-HS7VEROK.js} +120 -90
- package/dist/chunk-HS7VEROK.js.map +1 -0
- package/dist/chunk-JJBFIGUC.js +5202 -0
- package/dist/chunk-JJBFIGUC.js.map +1 -0
- package/dist/{chunk-PVQZBKN7.js → chunk-LL2AFX7V.js} +5 -2
- package/dist/chunk-LL2AFX7V.js.map +1 -0
- package/dist/chunk-PR4QN5HX.js +43 -0
- package/dist/chunk-PR4QN5HX.js.map +1 -0
- package/dist/{chunk-KUH345EY.js → chunk-QEVM4BVL.js} +5 -5
- package/dist/chunk-QEVM4BVL.js.map +1 -0
- package/dist/{chunk-MW5BQ2FW.js → chunk-SKACXF37.js} +164 -21
- package/dist/chunk-SKACXF37.js.map +1 -0
- package/dist/chunk-TYAY6AU6.js +159 -0
- package/dist/chunk-TYAY6AU6.js.map +1 -0
- package/dist/chunk-UJVEAURM.js +150 -0
- package/dist/chunk-UJVEAURM.js.map +1 -0
- package/dist/client-CvaJdLQA.d.ts +5015 -0
- package/dist/client.js +3 -1
- package/dist/index.d.ts +557 -32
- package/dist/index.js +8 -4
- package/dist/mcp/client.d.ts +9 -1053
- package/dist/mcp/client.js +2 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -0
- package/dist/mcp/do-oauth-client-provider.js +2 -1
- package/dist/mcp/index.d.ts +64 -56
- package/dist/mcp/index.js +954 -638
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +31 -0
- package/dist/mcp/x402.js +3195 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/observability/index.d.ts +46 -12
- package/dist/observability/index.js +6 -4
- package/dist/react.d.ts +7 -3
- package/dist/react.js +8 -5
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +83 -9
- package/dist/schedule.js +17 -2
- package/dist/schedule.js.map +1 -1
- package/dist/secp256k1-M22GZP2U.js +2193 -0
- package/dist/secp256k1-M22GZP2U.js.map +1 -0
- package/package.json +24 -8
- package/src/index.ts +161 -102
- package/dist/chunk-CKHRUXM5.js.map +0 -1
- package/dist/chunk-KUH345EY.js.map +0 -1
- package/dist/chunk-MW5BQ2FW.js.map +0 -1
- package/dist/chunk-PVQZBKN7.js.map +0 -1
- package/dist/index-BIJvkfYt.d.ts +0 -614
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { env } from "cloudflare:workers";
|
|
1
2
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
3
|
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
3
4
|
import type { SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
@@ -25,6 +26,7 @@ import { MCPClientManager } from "./mcp/client";
|
|
|
25
26
|
// import type { MCPClientConnection } from "./mcp/client-connection";
|
|
26
27
|
import { DurableObjectOAuthClientProvider } from "./mcp/do-oauth-client-provider";
|
|
27
28
|
import { genericObservability, type Observability } from "./observability";
|
|
29
|
+
import { MessageType } from "./ai-types";
|
|
28
30
|
|
|
29
31
|
export type { Connection, ConnectionContext, WSMessage } from "partyserver";
|
|
30
32
|
|
|
@@ -42,7 +44,7 @@ export type RPCRequest = {
|
|
|
42
44
|
* State update message from client
|
|
43
45
|
*/
|
|
44
46
|
export type StateUpdateMessage = {
|
|
45
|
-
type:
|
|
47
|
+
type: MessageType.CF_AGENT_STATE;
|
|
46
48
|
state: unknown;
|
|
47
49
|
};
|
|
48
50
|
|
|
@@ -50,7 +52,7 @@ export type StateUpdateMessage = {
|
|
|
50
52
|
* RPC response message to client
|
|
51
53
|
*/
|
|
52
54
|
export type RPCResponse = {
|
|
53
|
-
type:
|
|
55
|
+
type: MessageType.RPC;
|
|
54
56
|
id: string;
|
|
55
57
|
} & (
|
|
56
58
|
| {
|
|
@@ -77,7 +79,7 @@ function isRPCRequest(msg: unknown): msg is RPCRequest {
|
|
|
77
79
|
typeof msg === "object" &&
|
|
78
80
|
msg !== null &&
|
|
79
81
|
"type" in msg &&
|
|
80
|
-
msg.type ===
|
|
82
|
+
msg.type === MessageType.RPC &&
|
|
81
83
|
"id" in msg &&
|
|
82
84
|
typeof msg.id === "string" &&
|
|
83
85
|
"method" in msg &&
|
|
@@ -95,7 +97,7 @@ function isStateUpdateMessage(msg: unknown): msg is StateUpdateMessage {
|
|
|
95
97
|
typeof msg === "object" &&
|
|
96
98
|
msg !== null &&
|
|
97
99
|
"type" in msg &&
|
|
98
|
-
msg.type ===
|
|
100
|
+
msg.type === MessageType.CF_AGENT_STATE &&
|
|
99
101
|
"state" in msg
|
|
100
102
|
);
|
|
101
103
|
}
|
|
@@ -116,7 +118,7 @@ const callableMetadata = new Map<Function, CallableMetadata>();
|
|
|
116
118
|
* Decorator that marks a method as callable by clients
|
|
117
119
|
* @param metadata Optional metadata about the callable method
|
|
118
120
|
*/
|
|
119
|
-
export function
|
|
121
|
+
export function callable(metadata: CallableMetadata = {}) {
|
|
120
122
|
return function callableDecorator<This, Args extends unknown[], Return>(
|
|
121
123
|
target: (this: This, ...args: Args) => Return,
|
|
122
124
|
// biome-ignore lint/correctness/noUnusedFunctionParameters: later
|
|
@@ -130,6 +132,23 @@ export function unstable_callable(metadata: CallableMetadata = {}) {
|
|
|
130
132
|
};
|
|
131
133
|
}
|
|
132
134
|
|
|
135
|
+
let didWarnAboutUnstableCallable = false;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Decorator that marks a method as callable by clients
|
|
139
|
+
* @deprecated this has been renamed to callable, and unstable_callable will be removed in the next major version
|
|
140
|
+
* @param metadata Optional metadata about the callable method
|
|
141
|
+
*/
|
|
142
|
+
export const unstable_callable = (metadata: CallableMetadata = {}) => {
|
|
143
|
+
if (!didWarnAboutUnstableCallable) {
|
|
144
|
+
didWarnAboutUnstableCallable = true;
|
|
145
|
+
console.warn(
|
|
146
|
+
"unstable_callable is deprecated, use callable instead. unstable_callable will be removed in the next major version."
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
callable(metadata);
|
|
150
|
+
};
|
|
151
|
+
|
|
133
152
|
export type QueueItem<T = string> = {
|
|
134
153
|
id: string;
|
|
135
154
|
payload: T;
|
|
@@ -182,7 +201,7 @@ function getNextCronTime(cron: string) {
|
|
|
182
201
|
* MCP Server state update message from server -> Client
|
|
183
202
|
*/
|
|
184
203
|
export type MCPServerMessage = {
|
|
185
|
-
type:
|
|
204
|
+
type: MessageType.CF_AGENT_MCP_SERVERS;
|
|
186
205
|
mcp: MCPServersState;
|
|
187
206
|
};
|
|
188
207
|
|
|
@@ -271,7 +290,13 @@ function withAgentContext<T extends (...args: any[]) => any>(
|
|
|
271
290
|
method: T
|
|
272
291
|
): (this: Agent<unknown, unknown>, ...args: Parameters<T>) => ReturnType<T> {
|
|
273
292
|
return function (...args: Parameters<T>): ReturnType<T> {
|
|
274
|
-
const { connection, request, email } = getCurrentAgent();
|
|
293
|
+
const { connection, request, email, agent } = getCurrentAgent();
|
|
294
|
+
|
|
295
|
+
if (agent === this) {
|
|
296
|
+
// already wrapped, so we can just call the method
|
|
297
|
+
return method.apply(this, args);
|
|
298
|
+
}
|
|
299
|
+
// not wrapped, so we need to wrap it
|
|
275
300
|
return agentContext.run({ agent: this, connection, request, email }, () => {
|
|
276
301
|
return method.apply(this, args);
|
|
277
302
|
});
|
|
@@ -283,7 +308,11 @@ function withAgentContext<T extends (...args: any[]) => any>(
|
|
|
283
308
|
* @template Env Environment type containing bindings
|
|
284
309
|
* @template State State type to store within the Agent
|
|
285
310
|
*/
|
|
286
|
-
export class Agent<
|
|
311
|
+
export class Agent<
|
|
312
|
+
Env = typeof env,
|
|
313
|
+
State = unknown,
|
|
314
|
+
Props extends Record<string, unknown> = Record<string, unknown>
|
|
315
|
+
> extends Server<Env, Props> {
|
|
287
316
|
private _state = DEFAULT_STATE as State;
|
|
288
317
|
|
|
289
318
|
private _ParentClass: typeof Agent<Env, State> =
|
|
@@ -382,8 +411,11 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
382
411
|
constructor(ctx: AgentContext, env: Env) {
|
|
383
412
|
super(ctx, env);
|
|
384
413
|
|
|
385
|
-
|
|
386
|
-
|
|
414
|
+
if (!wrappedClasses.has(this.constructor)) {
|
|
415
|
+
// Auto-wrap custom methods with agent context
|
|
416
|
+
this._autoWrapCustomMethods();
|
|
417
|
+
wrappedClasses.add(this.constructor);
|
|
418
|
+
}
|
|
387
419
|
|
|
388
420
|
this.sql`
|
|
389
421
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
@@ -446,7 +478,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
446
478
|
this.broadcast(
|
|
447
479
|
JSON.stringify({
|
|
448
480
|
mcp: this.getMcpServers(),
|
|
449
|
-
type:
|
|
481
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
450
482
|
})
|
|
451
483
|
);
|
|
452
484
|
|
|
@@ -515,10 +547,8 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
515
547
|
displayMessage: `RPC call to ${method}`,
|
|
516
548
|
id: nanoid(),
|
|
517
549
|
payload: {
|
|
518
|
-
args,
|
|
519
550
|
method,
|
|
520
|
-
streaming: metadata?.streaming
|
|
521
|
-
success: true
|
|
551
|
+
streaming: metadata?.streaming
|
|
522
552
|
},
|
|
523
553
|
timestamp: Date.now(),
|
|
524
554
|
type: "rpc"
|
|
@@ -531,7 +561,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
531
561
|
id,
|
|
532
562
|
result,
|
|
533
563
|
success: true,
|
|
534
|
-
type:
|
|
564
|
+
type: MessageType.RPC
|
|
535
565
|
};
|
|
536
566
|
connection.send(JSON.stringify(response));
|
|
537
567
|
} catch (e) {
|
|
@@ -541,7 +571,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
541
571
|
e instanceof Error ? e.message : "Unknown error occurred",
|
|
542
572
|
id: parsed.id,
|
|
543
573
|
success: false,
|
|
544
|
-
type:
|
|
574
|
+
type: MessageType.RPC
|
|
545
575
|
};
|
|
546
576
|
connection.send(JSON.stringify(response));
|
|
547
577
|
console.error("RPC error:", e);
|
|
@@ -560,44 +590,42 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
560
590
|
// must fix this
|
|
561
591
|
return agentContext.run(
|
|
562
592
|
{ agent: this, connection, request: ctx.request, email: undefined },
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
if (this.state) {
|
|
566
|
-
connection.send(
|
|
567
|
-
JSON.stringify({
|
|
568
|
-
state: this.state,
|
|
569
|
-
type: "cf_agent_state"
|
|
570
|
-
})
|
|
571
|
-
);
|
|
572
|
-
}
|
|
573
|
-
|
|
593
|
+
() => {
|
|
594
|
+
if (this.state) {
|
|
574
595
|
connection.send(
|
|
575
596
|
JSON.stringify({
|
|
576
|
-
|
|
577
|
-
type:
|
|
597
|
+
state: this.state,
|
|
598
|
+
type: MessageType.CF_AGENT_STATE
|
|
578
599
|
})
|
|
579
600
|
);
|
|
601
|
+
}
|
|
580
602
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
603
|
+
connection.send(
|
|
604
|
+
JSON.stringify({
|
|
605
|
+
mcp: this.getMcpServers(),
|
|
606
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
607
|
+
})
|
|
608
|
+
);
|
|
609
|
+
|
|
610
|
+
this.observability?.emit(
|
|
611
|
+
{
|
|
612
|
+
displayMessage: "Connection established",
|
|
613
|
+
id: nanoid(),
|
|
614
|
+
payload: {
|
|
615
|
+
connectionId: connection.id
|
|
590
616
|
},
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
617
|
+
timestamp: Date.now(),
|
|
618
|
+
type: "connect"
|
|
619
|
+
},
|
|
620
|
+
this.ctx
|
|
621
|
+
);
|
|
622
|
+
return this._tryCatch(() => _onConnect(connection, ctx));
|
|
595
623
|
}
|
|
596
624
|
);
|
|
597
625
|
};
|
|
598
626
|
|
|
599
627
|
const _onStart = this.onStart.bind(this);
|
|
600
|
-
this.onStart = async () => {
|
|
628
|
+
this.onStart = async (props?: Props) => {
|
|
601
629
|
return agentContext.run(
|
|
602
630
|
{
|
|
603
631
|
agent: this,
|
|
@@ -606,15 +634,22 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
606
634
|
email: undefined
|
|
607
635
|
},
|
|
608
636
|
async () => {
|
|
609
|
-
|
|
637
|
+
await this._tryCatch(() => {
|
|
638
|
+
const servers = this.sql<MCPServerRow>`
|
|
610
639
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
611
640
|
`;
|
|
612
641
|
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
642
|
+
this.broadcast(
|
|
643
|
+
JSON.stringify({
|
|
644
|
+
mcp: this.getMcpServers(),
|
|
645
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
646
|
+
})
|
|
647
|
+
);
|
|
648
|
+
|
|
649
|
+
// from DO storage, reconnect to all servers not currently in the oauth flow using our saved auth information
|
|
650
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
651
|
+
servers.forEach((server) => {
|
|
652
|
+
this._connectToMcpServerInternal(
|
|
618
653
|
server.name,
|
|
619
654
|
server.server_url,
|
|
620
655
|
server.callback_url,
|
|
@@ -625,18 +660,33 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
625
660
|
id: server.id,
|
|
626
661
|
oauthClientId: server.client_id ?? undefined
|
|
627
662
|
}
|
|
628
|
-
)
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
663
|
+
)
|
|
664
|
+
.then(() => {
|
|
665
|
+
// Broadcast updated MCP servers state after each server connects
|
|
666
|
+
this.broadcast(
|
|
667
|
+
JSON.stringify({
|
|
668
|
+
mcp: this.getMcpServers(),
|
|
669
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
670
|
+
})
|
|
671
|
+
);
|
|
672
|
+
})
|
|
673
|
+
.catch((error) => {
|
|
674
|
+
console.error(
|
|
675
|
+
`Error connecting to MCP server: ${server.name} (${server.server_url})`,
|
|
676
|
+
error
|
|
677
|
+
);
|
|
678
|
+
// Still broadcast even if connection fails, so clients know about the failure
|
|
679
|
+
this.broadcast(
|
|
680
|
+
JSON.stringify({
|
|
681
|
+
mcp: this.getMcpServers(),
|
|
682
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
683
|
+
})
|
|
684
|
+
);
|
|
685
|
+
});
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
return _onStart(props);
|
|
689
|
+
});
|
|
640
690
|
}
|
|
641
691
|
);
|
|
642
692
|
};
|
|
@@ -646,7 +696,6 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
646
696
|
state: State,
|
|
647
697
|
source: Connection | "server" = "server"
|
|
648
698
|
) {
|
|
649
|
-
const previousState = this._state;
|
|
650
699
|
this._state = state;
|
|
651
700
|
this.sql`
|
|
652
701
|
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
@@ -659,7 +708,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
659
708
|
this.broadcast(
|
|
660
709
|
JSON.stringify({
|
|
661
710
|
state: state,
|
|
662
|
-
type:
|
|
711
|
+
type: MessageType.CF_AGENT_STATE
|
|
663
712
|
}),
|
|
664
713
|
source !== "server" ? [source.id] : []
|
|
665
714
|
);
|
|
@@ -672,10 +721,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
672
721
|
{
|
|
673
722
|
displayMessage: "State updated",
|
|
674
723
|
id: nanoid(),
|
|
675
|
-
payload: {
|
|
676
|
-
previousState,
|
|
677
|
-
state
|
|
678
|
-
},
|
|
724
|
+
payload: {},
|
|
679
725
|
timestamp: Date.now(),
|
|
680
726
|
type: "state:update"
|
|
681
727
|
},
|
|
@@ -815,41 +861,37 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
815
861
|
while (proto && proto !== Object.prototype && depth < 10) {
|
|
816
862
|
const methodNames = Object.getOwnPropertyNames(proto);
|
|
817
863
|
for (const methodName of methodNames) {
|
|
818
|
-
|
|
864
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
865
|
+
|
|
866
|
+
// Skip if it's a private method, a base method, a getter, or not a function,
|
|
819
867
|
if (
|
|
820
868
|
baseMethods.has(methodName) ||
|
|
821
869
|
methodName.startsWith("_") ||
|
|
822
|
-
|
|
870
|
+
!descriptor ||
|
|
871
|
+
!!descriptor.get ||
|
|
872
|
+
typeof descriptor.value !== "function"
|
|
823
873
|
) {
|
|
824
874
|
continue;
|
|
825
875
|
}
|
|
826
|
-
// If the method doesn't exist in base prototypes, it's a custom method
|
|
827
|
-
if (!baseMethods.has(methodName)) {
|
|
828
|
-
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
829
|
-
if (descriptor && typeof descriptor.value === "function") {
|
|
830
|
-
// Wrap the custom method with context
|
|
831
|
-
|
|
832
|
-
const wrappedFunction = withAgentContext(
|
|
833
|
-
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
834
|
-
this[methodName as keyof this] as (...args: any[]) => any
|
|
835
|
-
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
836
|
-
) as any;
|
|
837
|
-
|
|
838
|
-
// if the method is callable, copy the metadata from the original method
|
|
839
|
-
if (this._isCallable(methodName)) {
|
|
840
|
-
callableMetadata.set(
|
|
841
|
-
wrappedFunction,
|
|
842
|
-
callableMetadata.get(
|
|
843
|
-
this[methodName as keyof this] as Function
|
|
844
|
-
)!
|
|
845
|
-
);
|
|
846
|
-
}
|
|
847
876
|
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
877
|
+
// Now, methodName is confirmed to be a custom method/function
|
|
878
|
+
// Wrap the custom method with context
|
|
879
|
+
const wrappedFunction = withAgentContext(
|
|
880
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
881
|
+
this[methodName as keyof this] as (...args: any[]) => any
|
|
882
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
883
|
+
) as any;
|
|
884
|
+
|
|
885
|
+
// if the method is callable, copy the metadata from the original method
|
|
886
|
+
if (this._isCallable(methodName)) {
|
|
887
|
+
callableMetadata.set(
|
|
888
|
+
wrappedFunction,
|
|
889
|
+
callableMetadata.get(this[methodName as keyof this] as Function)!
|
|
890
|
+
);
|
|
852
891
|
}
|
|
892
|
+
|
|
893
|
+
// set the wrapped function on the prototype
|
|
894
|
+
this.constructor.prototype[methodName as keyof this] = wrappedFunction;
|
|
853
895
|
}
|
|
854
896
|
|
|
855
897
|
proto = Object.getPrototypeOf(proto);
|
|
@@ -1036,7 +1078,10 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
1036
1078
|
{
|
|
1037
1079
|
displayMessage: `Schedule ${schedule.id} created`,
|
|
1038
1080
|
id: nanoid(),
|
|
1039
|
-
payload:
|
|
1081
|
+
payload: {
|
|
1082
|
+
callback: callback as string,
|
|
1083
|
+
id: id
|
|
1084
|
+
},
|
|
1040
1085
|
timestamp: Date.now(),
|
|
1041
1086
|
type: "schedule:create"
|
|
1042
1087
|
},
|
|
@@ -1206,7 +1251,10 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
1206
1251
|
{
|
|
1207
1252
|
displayMessage: `Schedule ${id} cancelled`,
|
|
1208
1253
|
id: nanoid(),
|
|
1209
|
-
payload:
|
|
1254
|
+
payload: {
|
|
1255
|
+
callback: schedule.callback,
|
|
1256
|
+
id: schedule.id
|
|
1257
|
+
},
|
|
1210
1258
|
timestamp: Date.now(),
|
|
1211
1259
|
type: "schedule:cancel"
|
|
1212
1260
|
},
|
|
@@ -1271,7 +1319,10 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
1271
1319
|
{
|
|
1272
1320
|
displayMessage: `Schedule ${row.id} executed`,
|
|
1273
1321
|
id: nanoid(),
|
|
1274
|
-
payload:
|
|
1322
|
+
payload: {
|
|
1323
|
+
callback: row.callback,
|
|
1324
|
+
id: row.id
|
|
1325
|
+
},
|
|
1275
1326
|
timestamp: Date.now(),
|
|
1276
1327
|
type: "schedule:execute"
|
|
1277
1328
|
},
|
|
@@ -1391,7 +1442,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
1391
1442
|
this.broadcast(
|
|
1392
1443
|
JSON.stringify({
|
|
1393
1444
|
mcp: this.getMcpServers(),
|
|
1394
|
-
type:
|
|
1445
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
1395
1446
|
})
|
|
1396
1447
|
);
|
|
1397
1448
|
|
|
@@ -1480,7 +1531,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
1480
1531
|
this.broadcast(
|
|
1481
1532
|
JSON.stringify({
|
|
1482
1533
|
mcp: this.getMcpServers(),
|
|
1483
|
-
type:
|
|
1534
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
1484
1535
|
})
|
|
1485
1536
|
);
|
|
1486
1537
|
}
|
|
@@ -1516,6 +1567,9 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
1516
1567
|
}
|
|
1517
1568
|
}
|
|
1518
1569
|
|
|
1570
|
+
// A set of classes that have been wrapped with agent context
|
|
1571
|
+
const wrappedClasses = new Set<typeof Agent.prototype.constructor>();
|
|
1572
|
+
|
|
1519
1573
|
/**
|
|
1520
1574
|
* Namespace for creating Agent instances
|
|
1521
1575
|
* @template Agentic Type of the Agent class
|
|
@@ -1831,12 +1885,17 @@ export type EmailSendOptions = {
|
|
|
1831
1885
|
* @param options Options for Agent creation
|
|
1832
1886
|
* @returns Promise resolving to an Agent instance stub
|
|
1833
1887
|
*/
|
|
1834
|
-
export async function getAgentByName<
|
|
1888
|
+
export async function getAgentByName<
|
|
1889
|
+
Env,
|
|
1890
|
+
T extends Agent<Env>,
|
|
1891
|
+
Props extends Record<string, unknown> = Record<string, unknown>
|
|
1892
|
+
>(
|
|
1835
1893
|
namespace: AgentNamespace<T>,
|
|
1836
1894
|
name: string,
|
|
1837
1895
|
options?: {
|
|
1838
1896
|
jurisdiction?: DurableObjectJurisdiction;
|
|
1839
1897
|
locationHint?: DurableObjectLocationHint;
|
|
1898
|
+
props?: Props;
|
|
1840
1899
|
}
|
|
1841
1900
|
) {
|
|
1842
1901
|
return getServerByName<Env, T>(namespace, name, options);
|
|
@@ -1868,7 +1927,7 @@ export class StreamingResponse {
|
|
|
1868
1927
|
id: this._id,
|
|
1869
1928
|
result: chunk,
|
|
1870
1929
|
success: true,
|
|
1871
|
-
type:
|
|
1930
|
+
type: MessageType.RPC
|
|
1872
1931
|
};
|
|
1873
1932
|
this._connection.send(JSON.stringify(response));
|
|
1874
1933
|
}
|
|
@@ -1887,7 +1946,7 @@ export class StreamingResponse {
|
|
|
1887
1946
|
id: this._id,
|
|
1888
1947
|
result: finalChunk,
|
|
1889
1948
|
success: true,
|
|
1890
|
-
type:
|
|
1949
|
+
type: MessageType.RPC
|
|
1891
1950
|
};
|
|
1892
1951
|
this._connection.send(JSON.stringify(response));
|
|
1893
1952
|
}
|