agents 0.0.0-3bcb134 → 0.0.0-3e523ea
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 +30 -6
- package/dist/ai-chat-agent.d.ts +9 -8
- package/dist/ai-chat-agent.js +142 -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 +19 -0
- package/dist/ai-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +59 -70
- package/dist/ai-react.js +144 -37
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +36 -19
- package/dist/ai-types.js +6 -0
- package/dist/chunk-AVYJQSLW.js +17 -0
- package/dist/chunk-AVYJQSLW.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-JXN5WZFQ.js → chunk-PNF6ZMUA.js} +83 -74
- package/dist/chunk-PNF6ZMUA.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-UJVEAURM.js +150 -0
- package/dist/chunk-UJVEAURM.js.map +1 -0
- package/dist/{chunk-HY7ZLHJB.js → chunk-VYENMKFS.js} +18 -4
- package/dist/chunk-VYENMKFS.js.map +1 -0
- package/dist/{client-DgyzBU_8.d.ts → client-CpU7236R.d.ts} +6 -0
- package/dist/client.js +2 -1
- package/dist/index.d.ts +546 -32
- package/dist/index.js +7 -4
- package/dist/mcp/client.d.ts +1 -1
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -0
- package/dist/mcp/do-oauth-client-provider.js +1 -1
- package/dist/mcp/index.d.ts +2 -2
- package/dist/mcp/index.js +5 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +46 -14
- package/dist/observability/index.js +5 -4
- package/dist/react.d.ts +4 -2
- package/dist/react.js +7 -5
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +79 -5
- package/dist/schedule.js +15 -2
- package/dist/schedule.js.map +1 -1
- package/package.json +13 -5
- package/src/index.ts +103 -82
- package/dist/chunk-HY7ZLHJB.js.map +0 -1
- package/dist/chunk-JXN5WZFQ.js.map +0 -1
- package/dist/chunk-KUH345EY.js.map +0 -1
- package/dist/chunk-PVQZBKN7.js.map +0 -1
- package/dist/index-BCJclX6q.d.ts +0 -615
package/dist/schedule.d.ts
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Get the schedule prompt for a given event
|
|
5
|
+
* @param event - The event to get the schedule prompt for
|
|
6
|
+
* @returns The schedule prompt
|
|
7
|
+
*/
|
|
8
|
+
declare function getSchedulePrompt(event: { date: Date }): string;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated this has been renamed to getSchedulePrompt, and unstable_getSchedulePrompt will be removed in the next major version
|
|
11
|
+
* @param event - The event to get the schedule prompt for
|
|
12
|
+
* @returns The schedule prompt
|
|
13
|
+
*/
|
|
4
14
|
declare function unstable_getSchedulePrompt(event: { date: Date }): string;
|
|
5
|
-
|
|
15
|
+
/**
|
|
16
|
+
* The schema for the schedule prompt
|
|
17
|
+
*/
|
|
18
|
+
declare const scheduleSchema: z.ZodObject<
|
|
6
19
|
{
|
|
7
20
|
description: z.ZodString;
|
|
8
21
|
when: z.ZodObject<
|
|
@@ -17,14 +30,14 @@ declare const unstable_scheduleSchema: z.ZodObject<
|
|
|
17
30
|
{
|
|
18
31
|
type: "scheduled" | "delayed" | "cron" | "no-schedule";
|
|
19
32
|
date?: Date | undefined;
|
|
20
|
-
delayInSeconds?: number | undefined;
|
|
21
33
|
cron?: string | undefined;
|
|
34
|
+
delayInSeconds?: number | undefined;
|
|
22
35
|
},
|
|
23
36
|
{
|
|
24
37
|
type: "scheduled" | "delayed" | "cron" | "no-schedule";
|
|
25
38
|
date?: Date | undefined;
|
|
26
|
-
delayInSeconds?: number | undefined;
|
|
27
39
|
cron?: string | undefined;
|
|
40
|
+
delayInSeconds?: number | undefined;
|
|
28
41
|
}
|
|
29
42
|
>;
|
|
30
43
|
},
|
|
@@ -35,19 +48,80 @@ declare const unstable_scheduleSchema: z.ZodObject<
|
|
|
35
48
|
when: {
|
|
36
49
|
type: "scheduled" | "delayed" | "cron" | "no-schedule";
|
|
37
50
|
date?: Date | undefined;
|
|
51
|
+
cron?: string | undefined;
|
|
38
52
|
delayInSeconds?: number | undefined;
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
description: string;
|
|
57
|
+
when: {
|
|
58
|
+
type: "scheduled" | "delayed" | "cron" | "no-schedule";
|
|
59
|
+
date?: Date | undefined;
|
|
39
60
|
cron?: string | undefined;
|
|
61
|
+
delayInSeconds?: number | undefined;
|
|
40
62
|
};
|
|
63
|
+
}
|
|
64
|
+
>;
|
|
65
|
+
/**
|
|
66
|
+
* The type for the schedule prompt
|
|
67
|
+
*/
|
|
68
|
+
type Schedule = z.infer<typeof scheduleSchema>;
|
|
69
|
+
/**
|
|
70
|
+
* @deprecated this has been renamed to scheduleSchema, and unstable_scheduleSchema will be removed in the next major version
|
|
71
|
+
* @returns The schedule schema
|
|
72
|
+
*/
|
|
73
|
+
declare const unstable_scheduleSchema: z.ZodObject<
|
|
74
|
+
{
|
|
75
|
+
description: z.ZodString;
|
|
76
|
+
when: z.ZodObject<
|
|
77
|
+
{
|
|
78
|
+
cron: z.ZodOptional<z.ZodString>;
|
|
79
|
+
date: z.ZodOptional<z.ZodDate>;
|
|
80
|
+
delayInSeconds: z.ZodOptional<z.ZodNumber>;
|
|
81
|
+
type: z.ZodEnum<["scheduled", "delayed", "cron", "no-schedule"]>;
|
|
82
|
+
},
|
|
83
|
+
"strip",
|
|
84
|
+
z.ZodTypeAny,
|
|
85
|
+
{
|
|
86
|
+
type: "scheduled" | "delayed" | "cron" | "no-schedule";
|
|
87
|
+
date?: Date | undefined;
|
|
88
|
+
cron?: string | undefined;
|
|
89
|
+
delayInSeconds?: number | undefined;
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
type: "scheduled" | "delayed" | "cron" | "no-schedule";
|
|
93
|
+
date?: Date | undefined;
|
|
94
|
+
cron?: string | undefined;
|
|
95
|
+
delayInSeconds?: number | undefined;
|
|
96
|
+
}
|
|
97
|
+
>;
|
|
41
98
|
},
|
|
99
|
+
"strip",
|
|
100
|
+
z.ZodTypeAny,
|
|
42
101
|
{
|
|
43
102
|
description: string;
|
|
44
103
|
when: {
|
|
45
104
|
type: "scheduled" | "delayed" | "cron" | "no-schedule";
|
|
46
105
|
date?: Date | undefined;
|
|
106
|
+
cron?: string | undefined;
|
|
47
107
|
delayInSeconds?: number | undefined;
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
description: string;
|
|
112
|
+
when: {
|
|
113
|
+
type: "scheduled" | "delayed" | "cron" | "no-schedule";
|
|
114
|
+
date?: Date | undefined;
|
|
48
115
|
cron?: string | undefined;
|
|
116
|
+
delayInSeconds?: number | undefined;
|
|
49
117
|
};
|
|
50
118
|
}
|
|
51
119
|
>;
|
|
52
120
|
|
|
53
|
-
export {
|
|
121
|
+
export {
|
|
122
|
+
type Schedule,
|
|
123
|
+
getSchedulePrompt,
|
|
124
|
+
scheduleSchema,
|
|
125
|
+
unstable_getSchedulePrompt,
|
|
126
|
+
unstable_scheduleSchema
|
|
127
|
+
};
|
package/dist/schedule.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/schedule.ts
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
function
|
|
3
|
+
function getSchedulePrompt(event) {
|
|
4
4
|
return `
|
|
5
5
|
[Schedule Parser Component]
|
|
6
6
|
|
|
@@ -49,7 +49,17 @@ Example outputs:
|
|
|
49
49
|
[End Schedule Parser Component]
|
|
50
50
|
`;
|
|
51
51
|
}
|
|
52
|
-
var
|
|
52
|
+
var didWarnAboutUnstableGetSchedulePrompt = false;
|
|
53
|
+
function unstable_getSchedulePrompt(event) {
|
|
54
|
+
if (!didWarnAboutUnstableGetSchedulePrompt) {
|
|
55
|
+
didWarnAboutUnstableGetSchedulePrompt = true;
|
|
56
|
+
console.warn(
|
|
57
|
+
"unstable_getSchedulePrompt is deprecated, use getSchedulePrompt instead. unstable_getSchedulePrompt will be removed in the next major version."
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return getSchedulePrompt(event);
|
|
61
|
+
}
|
|
62
|
+
var scheduleSchema = z.object({
|
|
53
63
|
description: z.string().describe("A description of the task"),
|
|
54
64
|
when: z.object({
|
|
55
65
|
cron: z.string().optional().describe(
|
|
@@ -64,7 +74,10 @@ var unstable_scheduleSchema = z.object({
|
|
|
64
74
|
type: z.enum(["scheduled", "delayed", "cron", "no-schedule"]).describe("The type of scheduling details")
|
|
65
75
|
})
|
|
66
76
|
});
|
|
77
|
+
var unstable_scheduleSchema = scheduleSchema;
|
|
67
78
|
export {
|
|
79
|
+
getSchedulePrompt,
|
|
80
|
+
scheduleSchema,
|
|
68
81
|
unstable_getSchedulePrompt,
|
|
69
82
|
unstable_scheduleSchema
|
|
70
83
|
};
|
package/dist/schedule.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/schedule.ts"],"sourcesContent":["import { z } from \"zod\";\n\
|
|
1
|
+
{"version":3,"sources":["../src/schedule.ts"],"sourcesContent":["import { z } from \"zod\";\n\n/**\n * Get the schedule prompt for a given event\n * @param event - The event to get the schedule prompt for\n * @returns The schedule prompt\n */\nexport function getSchedulePrompt(event: { date: Date }) {\n return `\n[Schedule Parser Component]\n\nCurrent time: ${event.date.toUTCString()}\n\nThis component parses natural language scheduling requests into a structured format. It extracts:\n1. A clean task description (without timing information)\n2. Scheduling details in one of these formats:\n - scheduled: Specific date/time events\n - delayed: Relative time delays (in seconds)\n - cron: Recurring patterns\n - no-schedule: Tasks without timing\n\nRules:\n- Task descriptions should be clean and focused on the action\n- Use numbers (0-6) for days in cron patterns (0=Sunday)\n- For recurring tasks, use standard cron syntax\n- For relative times, convert to seconds\n- For specific dates, use the current time as reference\n\nExample outputs:\n{\n \"description\": \"meeting with team\",\n \"when\": {\n \"type\": \"scheduled\",\n \"date\": \"tomorrow at 14:00\"\n }\n}\n\n{\n \"description\": \"backup database\",\n \"when\": {\n \"type\": \"cron\",\n \"cron\": \"0 0 * * *\"\n }\n}\n\n{\n \"description\": \"send report\",\n \"when\": {\n \"type\": \"delayed\",\n \"delayInSeconds\": 1800\n }\n}\n\n[End Schedule Parser Component]\n`;\n}\n\nlet didWarnAboutUnstableGetSchedulePrompt = false;\n\n/**\n * @deprecated this has been renamed to getSchedulePrompt, and unstable_getSchedulePrompt will be removed in the next major version\n * @param event - The event to get the schedule prompt for\n * @returns The schedule prompt\n */\nexport function unstable_getSchedulePrompt(event: { date: Date }) {\n if (!didWarnAboutUnstableGetSchedulePrompt) {\n didWarnAboutUnstableGetSchedulePrompt = true;\n console.warn(\n \"unstable_getSchedulePrompt is deprecated, use getSchedulePrompt instead. unstable_getSchedulePrompt will be removed in the next major version.\"\n );\n }\n return getSchedulePrompt(event);\n}\n\n/**\n * The schema for the schedule prompt\n */\nexport const scheduleSchema = z.object({\n description: z.string().describe(\"A description of the task\"),\n when: z.object({\n cron: z\n .string()\n .optional()\n .describe(\n \"execute task on a recurring interval specified as cron syntax (only use if the type is cron)\"\n ),\n date: z.coerce\n .date()\n .optional()\n .describe(\n \"execute task at the specified date and time (only use if the type is scheduled)\"\n ),\n delayInSeconds: z\n .number()\n .optional()\n .describe(\n \"execute task after a delay in seconds (only use if the type is delayed)\"\n ),\n type: z\n .enum([\"scheduled\", \"delayed\", \"cron\", \"no-schedule\"])\n .describe(\"The type of scheduling details\")\n })\n});\n\n/**\n * The type for the schedule prompt\n */\nexport type Schedule = z.infer<typeof scheduleSchema>;\n\n/**\n * @deprecated this has been renamed to scheduleSchema, and unstable_scheduleSchema will be removed in the next major version\n * @returns The schedule schema\n */\nexport const unstable_scheduleSchema = scheduleSchema;\n"],"mappings":";AAAA,SAAS,SAAS;AAOX,SAAS,kBAAkB,OAAuB;AACvD,SAAO;AAAA;AAAA;AAAA,gBAGO,MAAM,KAAK,YAAY,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA4CxC;AAEA,IAAI,wCAAwC;AAOrC,SAAS,2BAA2B,OAAuB;AAChE,MAAI,CAAC,uCAAuC;AAC1C,4CAAwC;AACxC,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACA,SAAO,kBAAkB,KAAK;AAChC;AAKO,IAAM,iBAAiB,EAAE,OAAO;AAAA,EACrC,aAAa,EAAE,OAAO,EAAE,SAAS,2BAA2B;AAAA,EAC5D,MAAM,EAAE,OAAO;AAAA,IACb,MAAM,EACH,OAAO,EACP,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,MAAM,EAAE,OACL,KAAK,EACL,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,gBAAgB,EACb,OAAO,EACP,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,MAAM,EACH,KAAK,CAAC,aAAa,WAAW,QAAQ,aAAa,CAAC,EACpD,SAAS,gCAAgC;AAAA,EAC9C,CAAC;AACH,CAAC;AAWM,IAAM,0BAA0B;","names":[]}
|
package/package.json
CHANGED
|
@@ -4,14 +4,17 @@
|
|
|
4
4
|
"url": "https://github.com/cloudflare/agents/issues"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@modelcontextprotocol/sdk": "^1.17.
|
|
8
|
-
"ai": "
|
|
7
|
+
"@modelcontextprotocol/sdk": "^1.17.5",
|
|
8
|
+
"ai": "5.0.30",
|
|
9
9
|
"cron-schedule": "^5.0.4",
|
|
10
10
|
"mimetext": "^3.0.27",
|
|
11
11
|
"nanoid": "^5.1.5",
|
|
12
|
-
"partyserver": "^0.0.
|
|
12
|
+
"partyserver": "^0.0.73",
|
|
13
13
|
"partysocket": "1.1.5",
|
|
14
|
-
"zod": "^3.25.
|
|
14
|
+
"zod": "^3.25.76"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
15
18
|
},
|
|
16
19
|
"description": "A home for your AI agents",
|
|
17
20
|
"devDependencies": {
|
|
@@ -29,6 +32,11 @@
|
|
|
29
32
|
"import": "./dist/ai-chat-agent.js",
|
|
30
33
|
"require": "./dist/ai-chat-agent.js"
|
|
31
34
|
},
|
|
35
|
+
"./ai-chat-v5-migration": {
|
|
36
|
+
"types": "./dist/ai-chat-v5-migration.d.ts",
|
|
37
|
+
"import": "./dist/ai-chat-v5-migration.js",
|
|
38
|
+
"require": "./dist/ai-chat-v5-migration.js"
|
|
39
|
+
},
|
|
32
40
|
"./ai-react": {
|
|
33
41
|
"types": "./dist/ai-react.d.ts",
|
|
34
42
|
"import": "./dist/ai-react.js",
|
|
@@ -102,5 +110,5 @@
|
|
|
102
110
|
},
|
|
103
111
|
"type": "module",
|
|
104
112
|
"types": "dist/index.d.ts",
|
|
105
|
-
"version": "0.0.0-
|
|
113
|
+
"version": "0.0.0-3e523ea"
|
|
106
114
|
}
|
package/src/index.ts
CHANGED
|
@@ -26,6 +26,7 @@ import { MCPClientManager } from "./mcp/client";
|
|
|
26
26
|
// import type { MCPClientConnection } from "./mcp/client-connection";
|
|
27
27
|
import { DurableObjectOAuthClientProvider } from "./mcp/do-oauth-client-provider";
|
|
28
28
|
import { genericObservability, type Observability } from "./observability";
|
|
29
|
+
import { MessageType } from "./ai-types";
|
|
29
30
|
|
|
30
31
|
export type { Connection, ConnectionContext, WSMessage } from "partyserver";
|
|
31
32
|
|
|
@@ -43,7 +44,7 @@ export type RPCRequest = {
|
|
|
43
44
|
* State update message from client
|
|
44
45
|
*/
|
|
45
46
|
export type StateUpdateMessage = {
|
|
46
|
-
type:
|
|
47
|
+
type: MessageType.CF_AGENT_STATE;
|
|
47
48
|
state: unknown;
|
|
48
49
|
};
|
|
49
50
|
|
|
@@ -51,7 +52,7 @@ export type StateUpdateMessage = {
|
|
|
51
52
|
* RPC response message to client
|
|
52
53
|
*/
|
|
53
54
|
export type RPCResponse = {
|
|
54
|
-
type:
|
|
55
|
+
type: MessageType.RPC;
|
|
55
56
|
id: string;
|
|
56
57
|
} & (
|
|
57
58
|
| {
|
|
@@ -78,7 +79,7 @@ function isRPCRequest(msg: unknown): msg is RPCRequest {
|
|
|
78
79
|
typeof msg === "object" &&
|
|
79
80
|
msg !== null &&
|
|
80
81
|
"type" in msg &&
|
|
81
|
-
msg.type ===
|
|
82
|
+
msg.type === MessageType.RPC &&
|
|
82
83
|
"id" in msg &&
|
|
83
84
|
typeof msg.id === "string" &&
|
|
84
85
|
"method" in msg &&
|
|
@@ -96,7 +97,7 @@ function isStateUpdateMessage(msg: unknown): msg is StateUpdateMessage {
|
|
|
96
97
|
typeof msg === "object" &&
|
|
97
98
|
msg !== null &&
|
|
98
99
|
"type" in msg &&
|
|
99
|
-
msg.type ===
|
|
100
|
+
msg.type === MessageType.CF_AGENT_STATE &&
|
|
100
101
|
"state" in msg
|
|
101
102
|
);
|
|
102
103
|
}
|
|
@@ -117,7 +118,7 @@ const callableMetadata = new Map<Function, CallableMetadata>();
|
|
|
117
118
|
* Decorator that marks a method as callable by clients
|
|
118
119
|
* @param metadata Optional metadata about the callable method
|
|
119
120
|
*/
|
|
120
|
-
export function
|
|
121
|
+
export function callable(metadata: CallableMetadata = {}) {
|
|
121
122
|
return function callableDecorator<This, Args extends unknown[], Return>(
|
|
122
123
|
target: (this: This, ...args: Args) => Return,
|
|
123
124
|
// biome-ignore lint/correctness/noUnusedFunctionParameters: later
|
|
@@ -131,6 +132,23 @@ export function unstable_callable(metadata: CallableMetadata = {}) {
|
|
|
131
132
|
};
|
|
132
133
|
}
|
|
133
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
|
+
|
|
134
152
|
export type QueueItem<T = string> = {
|
|
135
153
|
id: string;
|
|
136
154
|
payload: T;
|
|
@@ -183,7 +201,7 @@ function getNextCronTime(cron: string) {
|
|
|
183
201
|
* MCP Server state update message from server -> Client
|
|
184
202
|
*/
|
|
185
203
|
export type MCPServerMessage = {
|
|
186
|
-
type:
|
|
204
|
+
type: MessageType.CF_AGENT_MCP_SERVERS;
|
|
187
205
|
mcp: MCPServersState;
|
|
188
206
|
};
|
|
189
207
|
|
|
@@ -272,7 +290,13 @@ function withAgentContext<T extends (...args: any[]) => any>(
|
|
|
272
290
|
method: T
|
|
273
291
|
): (this: Agent<unknown, unknown>, ...args: Parameters<T>) => ReturnType<T> {
|
|
274
292
|
return function (...args: Parameters<T>): ReturnType<T> {
|
|
275
|
-
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
|
|
276
300
|
return agentContext.run({ agent: this, connection, request, email }, () => {
|
|
277
301
|
return method.apply(this, args);
|
|
278
302
|
});
|
|
@@ -447,7 +471,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
447
471
|
this.broadcast(
|
|
448
472
|
JSON.stringify({
|
|
449
473
|
mcp: this.getMcpServers(),
|
|
450
|
-
type:
|
|
474
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
451
475
|
})
|
|
452
476
|
);
|
|
453
477
|
|
|
@@ -516,10 +540,8 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
516
540
|
displayMessage: `RPC call to ${method}`,
|
|
517
541
|
id: nanoid(),
|
|
518
542
|
payload: {
|
|
519
|
-
args,
|
|
520
543
|
method,
|
|
521
|
-
streaming: metadata?.streaming
|
|
522
|
-
success: true
|
|
544
|
+
streaming: metadata?.streaming
|
|
523
545
|
},
|
|
524
546
|
timestamp: Date.now(),
|
|
525
547
|
type: "rpc"
|
|
@@ -532,7 +554,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
532
554
|
id,
|
|
533
555
|
result,
|
|
534
556
|
success: true,
|
|
535
|
-
type:
|
|
557
|
+
type: MessageType.RPC
|
|
536
558
|
};
|
|
537
559
|
connection.send(JSON.stringify(response));
|
|
538
560
|
} catch (e) {
|
|
@@ -542,7 +564,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
542
564
|
e instanceof Error ? e.message : "Unknown error occurred",
|
|
543
565
|
id: parsed.id,
|
|
544
566
|
success: false,
|
|
545
|
-
type:
|
|
567
|
+
type: MessageType.RPC
|
|
546
568
|
};
|
|
547
569
|
connection.send(JSON.stringify(response));
|
|
548
570
|
console.error("RPC error:", e);
|
|
@@ -561,38 +583,36 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
561
583
|
// must fix this
|
|
562
584
|
return agentContext.run(
|
|
563
585
|
{ agent: this, connection, request: ctx.request, email: undefined },
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
if (this.state) {
|
|
567
|
-
connection.send(
|
|
568
|
-
JSON.stringify({
|
|
569
|
-
state: this.state,
|
|
570
|
-
type: "cf_agent_state"
|
|
571
|
-
})
|
|
572
|
-
);
|
|
573
|
-
}
|
|
574
|
-
|
|
586
|
+
() => {
|
|
587
|
+
if (this.state) {
|
|
575
588
|
connection.send(
|
|
576
589
|
JSON.stringify({
|
|
577
|
-
|
|
578
|
-
type:
|
|
590
|
+
state: this.state,
|
|
591
|
+
type: MessageType.CF_AGENT_STATE
|
|
579
592
|
})
|
|
580
593
|
);
|
|
594
|
+
}
|
|
581
595
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
596
|
+
connection.send(
|
|
597
|
+
JSON.stringify({
|
|
598
|
+
mcp: this.getMcpServers(),
|
|
599
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
600
|
+
})
|
|
601
|
+
);
|
|
602
|
+
|
|
603
|
+
this.observability?.emit(
|
|
604
|
+
{
|
|
605
|
+
displayMessage: "Connection established",
|
|
606
|
+
id: nanoid(),
|
|
607
|
+
payload: {
|
|
608
|
+
connectionId: connection.id
|
|
591
609
|
},
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
610
|
+
timestamp: Date.now(),
|
|
611
|
+
type: "connect"
|
|
612
|
+
},
|
|
613
|
+
this.ctx
|
|
614
|
+
);
|
|
615
|
+
return this._tryCatch(() => _onConnect(connection, ctx));
|
|
596
616
|
}
|
|
597
617
|
);
|
|
598
618
|
};
|
|
@@ -615,7 +635,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
615
635
|
this.broadcast(
|
|
616
636
|
JSON.stringify({
|
|
617
637
|
mcp: this.getMcpServers(),
|
|
618
|
-
type:
|
|
638
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
619
639
|
})
|
|
620
640
|
);
|
|
621
641
|
|
|
@@ -639,7 +659,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
639
659
|
this.broadcast(
|
|
640
660
|
JSON.stringify({
|
|
641
661
|
mcp: this.getMcpServers(),
|
|
642
|
-
type:
|
|
662
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
643
663
|
})
|
|
644
664
|
);
|
|
645
665
|
})
|
|
@@ -652,7 +672,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
652
672
|
this.broadcast(
|
|
653
673
|
JSON.stringify({
|
|
654
674
|
mcp: this.getMcpServers(),
|
|
655
|
-
type:
|
|
675
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
656
676
|
})
|
|
657
677
|
);
|
|
658
678
|
});
|
|
@@ -669,7 +689,6 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
669
689
|
state: State,
|
|
670
690
|
source: Connection | "server" = "server"
|
|
671
691
|
) {
|
|
672
|
-
const previousState = this._state;
|
|
673
692
|
this._state = state;
|
|
674
693
|
this.sql`
|
|
675
694
|
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
@@ -682,7 +701,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
682
701
|
this.broadcast(
|
|
683
702
|
JSON.stringify({
|
|
684
703
|
state: state,
|
|
685
|
-
type:
|
|
704
|
+
type: MessageType.CF_AGENT_STATE
|
|
686
705
|
}),
|
|
687
706
|
source !== "server" ? [source.id] : []
|
|
688
707
|
);
|
|
@@ -695,10 +714,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
695
714
|
{
|
|
696
715
|
displayMessage: "State updated",
|
|
697
716
|
id: nanoid(),
|
|
698
|
-
payload: {
|
|
699
|
-
previousState,
|
|
700
|
-
state
|
|
701
|
-
},
|
|
717
|
+
payload: {},
|
|
702
718
|
timestamp: Date.now(),
|
|
703
719
|
type: "state:update"
|
|
704
720
|
},
|
|
@@ -838,41 +854,37 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
838
854
|
while (proto && proto !== Object.prototype && depth < 10) {
|
|
839
855
|
const methodNames = Object.getOwnPropertyNames(proto);
|
|
840
856
|
for (const methodName of methodNames) {
|
|
841
|
-
|
|
857
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
858
|
+
|
|
859
|
+
// Skip if it's a private method, a base method, a getter, or not a function,
|
|
842
860
|
if (
|
|
843
861
|
baseMethods.has(methodName) ||
|
|
844
862
|
methodName.startsWith("_") ||
|
|
845
|
-
|
|
863
|
+
!descriptor ||
|
|
864
|
+
!!descriptor.get ||
|
|
865
|
+
typeof descriptor.value !== "function"
|
|
846
866
|
) {
|
|
847
867
|
continue;
|
|
848
868
|
}
|
|
849
|
-
// If the method doesn't exist in base prototypes, it's a custom method
|
|
850
|
-
if (!baseMethods.has(methodName)) {
|
|
851
|
-
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
852
|
-
if (descriptor && typeof descriptor.value === "function") {
|
|
853
|
-
// Wrap the custom method with context
|
|
854
|
-
|
|
855
|
-
const wrappedFunction = withAgentContext(
|
|
856
|
-
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
857
|
-
this[methodName as keyof this] as (...args: any[]) => any
|
|
858
|
-
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
859
|
-
) as any;
|
|
860
|
-
|
|
861
|
-
// if the method is callable, copy the metadata from the original method
|
|
862
|
-
if (this._isCallable(methodName)) {
|
|
863
|
-
callableMetadata.set(
|
|
864
|
-
wrappedFunction,
|
|
865
|
-
callableMetadata.get(
|
|
866
|
-
this[methodName as keyof this] as Function
|
|
867
|
-
)!
|
|
868
|
-
);
|
|
869
|
-
}
|
|
870
869
|
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
870
|
+
// Now, methodName is confirmed to be a custom method/function
|
|
871
|
+
// Wrap the custom method with context
|
|
872
|
+
const wrappedFunction = withAgentContext(
|
|
873
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
874
|
+
this[methodName as keyof this] as (...args: any[]) => any
|
|
875
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
876
|
+
) as any;
|
|
877
|
+
|
|
878
|
+
// if the method is callable, copy the metadata from the original method
|
|
879
|
+
if (this._isCallable(methodName)) {
|
|
880
|
+
callableMetadata.set(
|
|
881
|
+
wrappedFunction,
|
|
882
|
+
callableMetadata.get(this[methodName as keyof this] as Function)!
|
|
883
|
+
);
|
|
875
884
|
}
|
|
885
|
+
|
|
886
|
+
// set the wrapped function on the prototype
|
|
887
|
+
this.constructor.prototype[methodName as keyof this] = wrappedFunction;
|
|
876
888
|
}
|
|
877
889
|
|
|
878
890
|
proto = Object.getPrototypeOf(proto);
|
|
@@ -1059,7 +1071,10 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
1059
1071
|
{
|
|
1060
1072
|
displayMessage: `Schedule ${schedule.id} created`,
|
|
1061
1073
|
id: nanoid(),
|
|
1062
|
-
payload:
|
|
1074
|
+
payload: {
|
|
1075
|
+
callback: callback as string,
|
|
1076
|
+
id: id
|
|
1077
|
+
},
|
|
1063
1078
|
timestamp: Date.now(),
|
|
1064
1079
|
type: "schedule:create"
|
|
1065
1080
|
},
|
|
@@ -1229,7 +1244,10 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
1229
1244
|
{
|
|
1230
1245
|
displayMessage: `Schedule ${id} cancelled`,
|
|
1231
1246
|
id: nanoid(),
|
|
1232
|
-
payload:
|
|
1247
|
+
payload: {
|
|
1248
|
+
callback: schedule.callback,
|
|
1249
|
+
id: schedule.id
|
|
1250
|
+
},
|
|
1233
1251
|
timestamp: Date.now(),
|
|
1234
1252
|
type: "schedule:cancel"
|
|
1235
1253
|
},
|
|
@@ -1294,7 +1312,10 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
1294
1312
|
{
|
|
1295
1313
|
displayMessage: `Schedule ${row.id} executed`,
|
|
1296
1314
|
id: nanoid(),
|
|
1297
|
-
payload:
|
|
1315
|
+
payload: {
|
|
1316
|
+
callback: row.callback,
|
|
1317
|
+
id: row.id
|
|
1318
|
+
},
|
|
1298
1319
|
timestamp: Date.now(),
|
|
1299
1320
|
type: "schedule:execute"
|
|
1300
1321
|
},
|
|
@@ -1414,7 +1435,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
1414
1435
|
this.broadcast(
|
|
1415
1436
|
JSON.stringify({
|
|
1416
1437
|
mcp: this.getMcpServers(),
|
|
1417
|
-
type:
|
|
1438
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
1418
1439
|
})
|
|
1419
1440
|
);
|
|
1420
1441
|
|
|
@@ -1503,7 +1524,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
|
1503
1524
|
this.broadcast(
|
|
1504
1525
|
JSON.stringify({
|
|
1505
1526
|
mcp: this.getMcpServers(),
|
|
1506
|
-
type:
|
|
1527
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
1507
1528
|
})
|
|
1508
1529
|
);
|
|
1509
1530
|
}
|
|
@@ -1891,7 +1912,7 @@ export class StreamingResponse {
|
|
|
1891
1912
|
id: this._id,
|
|
1892
1913
|
result: chunk,
|
|
1893
1914
|
success: true,
|
|
1894
|
-
type:
|
|
1915
|
+
type: MessageType.RPC
|
|
1895
1916
|
};
|
|
1896
1917
|
this._connection.send(JSON.stringify(response));
|
|
1897
1918
|
}
|
|
@@ -1910,7 +1931,7 @@ export class StreamingResponse {
|
|
|
1910
1931
|
id: this._id,
|
|
1911
1932
|
result: finalChunk,
|
|
1912
1933
|
success: true,
|
|
1913
|
-
type:
|
|
1934
|
+
type: MessageType.RPC
|
|
1914
1935
|
};
|
|
1915
1936
|
this._connection.send(JSON.stringify(response));
|
|
1916
1937
|
}
|