agents 0.0.0-843745d → 0.0.0-85b0bb0
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/dist/ai-chat-agent.d.ts +31 -6
- package/dist/ai-chat-agent.js +119 -115
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +17 -4
- package/dist/ai-react.js +29 -29
- package/dist/ai-react.js.map +1 -1
- package/dist/{chunk-AV3OMRR4.js → chunk-4CIGD73X.js} +358 -164
- package/dist/chunk-4CIGD73X.js.map +1 -0
- package/dist/chunk-767EASBA.js +106 -0
- package/dist/chunk-767EASBA.js.map +1 -0
- package/dist/{chunk-YZNSS675.js → chunk-E3LCYPCB.js} +71 -37
- package/dist/chunk-E3LCYPCB.js.map +1 -0
- package/dist/chunk-NKZZ66QY.js +116 -0
- package/dist/chunk-NKZZ66QY.js.map +1 -0
- package/dist/client.d.ts +15 -1
- package/dist/client.js +6 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +117 -18
- package/dist/index.js +8 -7
- package/dist/mcp/client.d.ts +310 -23
- package/dist/mcp/client.js +1 -2
- package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
- package/dist/mcp/do-oauth-client-provider.js +3 -103
- package/dist/mcp/do-oauth-client-provider.js.map +1 -1
- package/dist/mcp/index.d.ts +22 -11
- package/dist/mcp/index.js +172 -175
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +85 -5
- package/dist/react.js +20 -8
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +2 -2
- package/dist/schedule.js +4 -6
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/package.json +70 -68
- package/src/index.ts +427 -88
- package/dist/chunk-AV3OMRR4.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-YZNSS675.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → serializable.js.map} +0 -0
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MCPClientManager
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-E3LCYPCB.js";
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "./chunk-
|
|
5
|
+
DurableObjectOAuthClientProvider
|
|
6
|
+
} from "./chunk-767EASBA.js";
|
|
7
|
+
import {
|
|
8
|
+
camelCaseToKebabCase
|
|
9
|
+
} from "./chunk-NKZZ66QY.js";
|
|
10
10
|
|
|
11
11
|
// src/index.ts
|
|
12
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
13
|
+
import { parseCronExpression } from "cron-schedule";
|
|
14
|
+
import { nanoid } from "nanoid";
|
|
12
15
|
import {
|
|
13
|
-
|
|
16
|
+
getServerByName,
|
|
14
17
|
routePartykitRequest,
|
|
15
|
-
|
|
18
|
+
Server
|
|
16
19
|
} from "partyserver";
|
|
17
|
-
import { parseCronExpression } from "cron-schedule";
|
|
18
|
-
import { nanoid } from "nanoid";
|
|
19
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
20
20
|
function isRPCRequest(msg) {
|
|
21
21
|
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
22
|
}
|
|
@@ -39,20 +39,72 @@ function getNextCronTime(cron) {
|
|
|
39
39
|
var STATE_ROW_ID = "cf_state_row_id";
|
|
40
40
|
var STATE_WAS_CHANGED = "cf_state_was_changed";
|
|
41
41
|
var DEFAULT_STATE = {};
|
|
42
|
-
var
|
|
43
|
-
|
|
42
|
+
var agentContext = new AsyncLocalStorage();
|
|
43
|
+
function getCurrentAgent() {
|
|
44
|
+
const store = agentContext.getStore();
|
|
45
|
+
if (!store) {
|
|
46
|
+
return {
|
|
47
|
+
agent: void 0,
|
|
48
|
+
connection: void 0,
|
|
49
|
+
request: void 0
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return store;
|
|
53
|
+
}
|
|
44
54
|
var Agent = class extends Server {
|
|
45
55
|
constructor(ctx, env) {
|
|
46
56
|
super(ctx, env);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
this.mcp = new MCPClientManager(__privateGet(this, _ParentClass).name, "0.0.1");
|
|
57
|
+
this._state = DEFAULT_STATE;
|
|
58
|
+
this._ParentClass = Object.getPrototypeOf(this).constructor;
|
|
59
|
+
this.mcp = new MCPClientManager(this._ParentClass.name, "0.0.1");
|
|
51
60
|
/**
|
|
52
61
|
* Initial state for the Agent
|
|
53
62
|
* Override to provide default state values
|
|
54
63
|
*/
|
|
55
64
|
this.initialState = DEFAULT_STATE;
|
|
65
|
+
/**
|
|
66
|
+
* Method called when an alarm fires.
|
|
67
|
+
* Executes any scheduled tasks that are due.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* To schedule a task, please use the `this.schedule` method instead.
|
|
71
|
+
* See {@link https://developers.cloudflare.com/agents/api-reference/schedule-tasks/}
|
|
72
|
+
*/
|
|
73
|
+
this.alarm = async () => {
|
|
74
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
75
|
+
const result = this.sql`
|
|
76
|
+
SELECT * FROM cf_agents_schedules WHERE time <= ${now}
|
|
77
|
+
`;
|
|
78
|
+
for (const row of result || []) {
|
|
79
|
+
const callback = this[row.callback];
|
|
80
|
+
if (!callback) {
|
|
81
|
+
console.error(`callback ${row.callback} not found`);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
await agentContext.run(
|
|
85
|
+
{ agent: this, connection: void 0, request: void 0 },
|
|
86
|
+
async () => {
|
|
87
|
+
try {
|
|
88
|
+
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
console.error(`error executing callback "${row.callback}"`, e);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
if (row.type === "cron") {
|
|
95
|
+
const nextExecutionTime = getNextCronTime(row.cron);
|
|
96
|
+
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
|
|
97
|
+
this.sql`
|
|
98
|
+
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
99
|
+
`;
|
|
100
|
+
} else {
|
|
101
|
+
this.sql`
|
|
102
|
+
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
103
|
+
`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
await this._scheduleNextAlarm();
|
|
107
|
+
};
|
|
56
108
|
this.sql`
|
|
57
109
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
58
110
|
id TEXT PRIMARY KEY NOT NULL,
|
|
@@ -60,7 +112,7 @@ var Agent = class extends Server {
|
|
|
60
112
|
)
|
|
61
113
|
`;
|
|
62
114
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
63
|
-
return
|
|
115
|
+
return this._tryCatch(async () => {
|
|
64
116
|
this.sql`
|
|
65
117
|
CREATE TABLE IF NOT EXISTS cf_agents_schedules (
|
|
66
118
|
id TEXT PRIMARY KEY NOT NULL DEFAULT (randomblob(9)),
|
|
@@ -76,22 +128,55 @@ var Agent = class extends Server {
|
|
|
76
128
|
await this.alarm();
|
|
77
129
|
});
|
|
78
130
|
});
|
|
131
|
+
this.sql`
|
|
132
|
+
CREATE TABLE IF NOT EXISTS cf_agents_mcp_servers (
|
|
133
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
134
|
+
name TEXT NOT NULL,
|
|
135
|
+
server_url TEXT NOT NULL,
|
|
136
|
+
callback_url TEXT NOT NULL,
|
|
137
|
+
client_id TEXT,
|
|
138
|
+
auth_url TEXT,
|
|
139
|
+
server_options TEXT
|
|
140
|
+
)
|
|
141
|
+
`;
|
|
142
|
+
const _onRequest = this.onRequest.bind(this);
|
|
143
|
+
this.onRequest = (request) => {
|
|
144
|
+
return agentContext.run(
|
|
145
|
+
{ agent: this, connection: void 0, request },
|
|
146
|
+
async () => {
|
|
147
|
+
if (this.mcp.isCallbackRequest(request)) {
|
|
148
|
+
await this.mcp.handleCallbackRequest(request);
|
|
149
|
+
this.broadcast(
|
|
150
|
+
JSON.stringify({
|
|
151
|
+
mcp: this.getMcpServers(),
|
|
152
|
+
type: "cf_agent_mcp_servers"
|
|
153
|
+
})
|
|
154
|
+
);
|
|
155
|
+
return new Response("<script>window.close();</script>", {
|
|
156
|
+
headers: { "content-type": "text/html" },
|
|
157
|
+
status: 200
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return this._tryCatch(() => _onRequest(request));
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
};
|
|
79
164
|
const _onMessage = this.onMessage.bind(this);
|
|
80
165
|
this.onMessage = async (connection, message) => {
|
|
81
|
-
return
|
|
166
|
+
return agentContext.run(
|
|
82
167
|
{ agent: this, connection, request: void 0 },
|
|
83
168
|
async () => {
|
|
84
169
|
if (typeof message !== "string") {
|
|
85
|
-
return
|
|
170
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
86
171
|
}
|
|
87
172
|
let parsed;
|
|
88
173
|
try {
|
|
89
174
|
parsed = JSON.parse(message);
|
|
90
|
-
} catch (
|
|
91
|
-
return
|
|
175
|
+
} catch (_e) {
|
|
176
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
92
177
|
}
|
|
93
178
|
if (isStateUpdateMessage(parsed)) {
|
|
94
|
-
|
|
179
|
+
this._setStateInternal(parsed.state, connection);
|
|
95
180
|
return;
|
|
96
181
|
}
|
|
97
182
|
if (isRPCRequest(parsed)) {
|
|
@@ -101,7 +186,7 @@ var Agent = class extends Server {
|
|
|
101
186
|
if (typeof methodFn !== "function") {
|
|
102
187
|
throw new Error(`Method ${method} does not exist`);
|
|
103
188
|
}
|
|
104
|
-
if (!
|
|
189
|
+
if (!this._isCallable(method)) {
|
|
105
190
|
throw new Error(`Method ${method} is not callable`);
|
|
106
191
|
}
|
|
107
192
|
const metadata = callableMetadata.get(methodFn);
|
|
@@ -112,55 +197,94 @@ var Agent = class extends Server {
|
|
|
112
197
|
}
|
|
113
198
|
const result = await methodFn.apply(this, args);
|
|
114
199
|
const response = {
|
|
115
|
-
|
|
200
|
+
done: true,
|
|
116
201
|
id,
|
|
117
|
-
success: true,
|
|
118
202
|
result,
|
|
119
|
-
|
|
203
|
+
success: true,
|
|
204
|
+
type: "rpc"
|
|
120
205
|
};
|
|
121
206
|
connection.send(JSON.stringify(response));
|
|
122
207
|
} catch (e) {
|
|
123
208
|
const response = {
|
|
124
|
-
|
|
209
|
+
error: e instanceof Error ? e.message : "Unknown error occurred",
|
|
125
210
|
id: parsed.id,
|
|
126
211
|
success: false,
|
|
127
|
-
|
|
212
|
+
type: "rpc"
|
|
128
213
|
};
|
|
129
214
|
connection.send(JSON.stringify(response));
|
|
130
215
|
console.error("RPC error:", e);
|
|
131
216
|
}
|
|
132
217
|
return;
|
|
133
218
|
}
|
|
134
|
-
return
|
|
219
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
135
220
|
}
|
|
136
221
|
);
|
|
137
222
|
};
|
|
138
223
|
const _onConnect = this.onConnect.bind(this);
|
|
139
224
|
this.onConnect = (connection, ctx2) => {
|
|
140
|
-
return
|
|
225
|
+
return agentContext.run(
|
|
141
226
|
{ agent: this, connection, request: ctx2.request },
|
|
142
227
|
async () => {
|
|
143
228
|
setTimeout(() => {
|
|
144
229
|
if (this.state) {
|
|
145
230
|
connection.send(
|
|
146
231
|
JSON.stringify({
|
|
147
|
-
|
|
148
|
-
|
|
232
|
+
state: this.state,
|
|
233
|
+
type: "cf_agent_state"
|
|
149
234
|
})
|
|
150
235
|
);
|
|
151
236
|
}
|
|
152
|
-
|
|
237
|
+
connection.send(
|
|
238
|
+
JSON.stringify({
|
|
239
|
+
mcp: this.getMcpServers(),
|
|
240
|
+
type: "cf_agent_mcp_servers"
|
|
241
|
+
})
|
|
242
|
+
);
|
|
243
|
+
return this._tryCatch(() => _onConnect(connection, ctx2));
|
|
153
244
|
}, 20);
|
|
154
245
|
}
|
|
155
246
|
);
|
|
156
247
|
};
|
|
248
|
+
const _onStart = this.onStart.bind(this);
|
|
249
|
+
this.onStart = async () => {
|
|
250
|
+
return agentContext.run(
|
|
251
|
+
{ agent: this, connection: void 0, request: void 0 },
|
|
252
|
+
async () => {
|
|
253
|
+
const servers = this.sql`
|
|
254
|
+
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
255
|
+
`;
|
|
256
|
+
Promise.allSettled(
|
|
257
|
+
servers.map((server) => {
|
|
258
|
+
return this._connectToMcpServerInternal(
|
|
259
|
+
server.name,
|
|
260
|
+
server.server_url,
|
|
261
|
+
server.callback_url,
|
|
262
|
+
server.server_options ? JSON.parse(server.server_options) : void 0,
|
|
263
|
+
{
|
|
264
|
+
id: server.id,
|
|
265
|
+
oauthClientId: server.client_id ?? void 0
|
|
266
|
+
}
|
|
267
|
+
);
|
|
268
|
+
})
|
|
269
|
+
).then((_results) => {
|
|
270
|
+
this.broadcast(
|
|
271
|
+
JSON.stringify({
|
|
272
|
+
mcp: this.getMcpServers(),
|
|
273
|
+
type: "cf_agent_mcp_servers"
|
|
274
|
+
})
|
|
275
|
+
);
|
|
276
|
+
});
|
|
277
|
+
await this._tryCatch(() => _onStart());
|
|
278
|
+
}
|
|
279
|
+
);
|
|
280
|
+
};
|
|
157
281
|
}
|
|
158
282
|
/**
|
|
159
283
|
* Current state of the Agent
|
|
160
284
|
*/
|
|
161
285
|
get state() {
|
|
162
|
-
if (
|
|
163
|
-
return
|
|
286
|
+
if (this._state !== DEFAULT_STATE) {
|
|
287
|
+
return this._state;
|
|
164
288
|
}
|
|
165
289
|
const wasChanged = this.sql`
|
|
166
290
|
SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
|
|
@@ -171,8 +295,8 @@ var Agent = class extends Server {
|
|
|
171
295
|
if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
|
|
172
296
|
result[0]?.state) {
|
|
173
297
|
const state = result[0]?.state;
|
|
174
|
-
|
|
175
|
-
return
|
|
298
|
+
this._state = JSON.parse(state);
|
|
299
|
+
return this._state;
|
|
176
300
|
}
|
|
177
301
|
if (this.initialState === DEFAULT_STATE) {
|
|
178
302
|
return void 0;
|
|
@@ -200,32 +324,68 @@ var Agent = class extends Server {
|
|
|
200
324
|
throw this.onError(e);
|
|
201
325
|
}
|
|
202
326
|
}
|
|
327
|
+
_setStateInternal(state, source = "server") {
|
|
328
|
+
this._state = state;
|
|
329
|
+
this.sql`
|
|
330
|
+
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
331
|
+
VALUES (${STATE_ROW_ID}, ${JSON.stringify(state)})
|
|
332
|
+
`;
|
|
333
|
+
this.sql`
|
|
334
|
+
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
335
|
+
VALUES (${STATE_WAS_CHANGED}, ${JSON.stringify(true)})
|
|
336
|
+
`;
|
|
337
|
+
this.broadcast(
|
|
338
|
+
JSON.stringify({
|
|
339
|
+
state,
|
|
340
|
+
type: "cf_agent_state"
|
|
341
|
+
}),
|
|
342
|
+
source !== "server" ? [source.id] : []
|
|
343
|
+
);
|
|
344
|
+
return this._tryCatch(() => {
|
|
345
|
+
const { connection, request } = agentContext.getStore() || {};
|
|
346
|
+
return agentContext.run(
|
|
347
|
+
{ agent: this, connection, request },
|
|
348
|
+
async () => {
|
|
349
|
+
return this.onStateUpdate(state, source);
|
|
350
|
+
}
|
|
351
|
+
);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
203
354
|
/**
|
|
204
355
|
* Update the Agent's state
|
|
205
356
|
* @param state New state to set
|
|
206
357
|
*/
|
|
207
358
|
setState(state) {
|
|
208
|
-
|
|
359
|
+
this._setStateInternal(state, "server");
|
|
209
360
|
}
|
|
210
361
|
/**
|
|
211
362
|
* Called when the Agent's state is updated
|
|
212
363
|
* @param state Updated state
|
|
213
364
|
* @param source Source of the state update ("server" or a client connection)
|
|
214
365
|
*/
|
|
366
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later
|
|
215
367
|
onStateUpdate(state, source) {
|
|
216
368
|
}
|
|
217
369
|
/**
|
|
218
370
|
* Called when the Agent receives an email
|
|
219
371
|
* @param email Email message to process
|
|
220
372
|
*/
|
|
373
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later
|
|
221
374
|
onEmail(email) {
|
|
222
|
-
return
|
|
375
|
+
return agentContext.run(
|
|
223
376
|
{ agent: this, connection: void 0, request: void 0 },
|
|
224
377
|
async () => {
|
|
225
378
|
console.error("onEmail not implemented");
|
|
226
379
|
}
|
|
227
380
|
);
|
|
228
381
|
}
|
|
382
|
+
async _tryCatch(fn) {
|
|
383
|
+
try {
|
|
384
|
+
return await fn();
|
|
385
|
+
} catch (e) {
|
|
386
|
+
throw this.onError(e);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
229
389
|
onError(connectionOrError, error) {
|
|
230
390
|
let theError;
|
|
231
391
|
if (connectionOrError && error) {
|
|
@@ -275,10 +435,10 @@ var Agent = class extends Server {
|
|
|
275
435
|
payload
|
|
276
436
|
)}, 'scheduled', ${timestamp})
|
|
277
437
|
`;
|
|
278
|
-
await
|
|
438
|
+
await this._scheduleNextAlarm();
|
|
279
439
|
return {
|
|
280
|
-
id,
|
|
281
440
|
callback,
|
|
441
|
+
id,
|
|
282
442
|
payload,
|
|
283
443
|
time: timestamp,
|
|
284
444
|
type: "scheduled"
|
|
@@ -293,12 +453,12 @@ var Agent = class extends Server {
|
|
|
293
453
|
payload
|
|
294
454
|
)}, 'delayed', ${when}, ${timestamp})
|
|
295
455
|
`;
|
|
296
|
-
await
|
|
456
|
+
await this._scheduleNextAlarm();
|
|
297
457
|
return {
|
|
298
|
-
id,
|
|
299
458
|
callback,
|
|
300
|
-
payload,
|
|
301
459
|
delayInSeconds: when,
|
|
460
|
+
id,
|
|
461
|
+
payload,
|
|
302
462
|
time: timestamp,
|
|
303
463
|
type: "delayed"
|
|
304
464
|
};
|
|
@@ -312,12 +472,12 @@ var Agent = class extends Server {
|
|
|
312
472
|
payload
|
|
313
473
|
)}, 'cron', ${when}, ${timestamp})
|
|
314
474
|
`;
|
|
315
|
-
await
|
|
475
|
+
await this._scheduleNextAlarm();
|
|
316
476
|
return {
|
|
317
|
-
id,
|
|
318
477
|
callback,
|
|
319
|
-
payload,
|
|
320
478
|
cron: when,
|
|
479
|
+
id,
|
|
480
|
+
payload,
|
|
321
481
|
time: timestamp,
|
|
322
482
|
type: "cron"
|
|
323
483
|
};
|
|
@@ -379,47 +539,21 @@ var Agent = class extends Server {
|
|
|
379
539
|
*/
|
|
380
540
|
async cancelSchedule(id) {
|
|
381
541
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
382
|
-
await
|
|
542
|
+
await this._scheduleNextAlarm();
|
|
383
543
|
return true;
|
|
384
544
|
}
|
|
385
|
-
|
|
386
|
-
* Method called when an alarm fires
|
|
387
|
-
* Executes any scheduled tasks that are due
|
|
388
|
-
*/
|
|
389
|
-
async alarm() {
|
|
390
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
545
|
+
async _scheduleNextAlarm() {
|
|
391
546
|
const result = this.sql`
|
|
392
|
-
SELECT
|
|
547
|
+
SELECT time FROM cf_agents_schedules
|
|
548
|
+
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
549
|
+
ORDER BY time ASC
|
|
550
|
+
LIMIT 1
|
|
393
551
|
`;
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
await unstable_context.run(
|
|
401
|
-
{ agent: this, connection: void 0, request: void 0 },
|
|
402
|
-
async () => {
|
|
403
|
-
try {
|
|
404
|
-
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
405
|
-
} catch (e) {
|
|
406
|
-
console.error(`error executing callback "${row.callback}"`, e);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
);
|
|
410
|
-
if (row.type === "cron") {
|
|
411
|
-
const nextExecutionTime = getNextCronTime(row.cron);
|
|
412
|
-
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
|
|
413
|
-
this.sql`
|
|
414
|
-
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
415
|
-
`;
|
|
416
|
-
} else {
|
|
417
|
-
this.sql`
|
|
418
|
-
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
419
|
-
`;
|
|
420
|
-
}
|
|
552
|
+
if (!result) return;
|
|
553
|
+
if (result.length > 0 && "time" in result[0]) {
|
|
554
|
+
const nextTime = result[0].time * 1e3;
|
|
555
|
+
await this.ctx.storage.setAlarm(nextTime);
|
|
421
556
|
}
|
|
422
|
-
await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
|
|
423
557
|
}
|
|
424
558
|
/**
|
|
425
559
|
* Destroy the Agent, removing all state and scheduled tasks
|
|
@@ -427,67 +561,133 @@ var Agent = class extends Server {
|
|
|
427
561
|
async destroy() {
|
|
428
562
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
429
563
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
564
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
430
565
|
await this.ctx.storage.deleteAlarm();
|
|
431
566
|
await this.ctx.storage.deleteAll();
|
|
567
|
+
this.ctx.abort("destroyed");
|
|
432
568
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
569
|
+
/**
|
|
570
|
+
* Get all methods marked as callable on this Agent
|
|
571
|
+
* @returns A map of method names to their metadata
|
|
572
|
+
*/
|
|
573
|
+
_isCallable(method) {
|
|
574
|
+
return callableMetadata.has(this[method]);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Connect to a new MCP Server
|
|
578
|
+
*
|
|
579
|
+
* @param url MCP Server SSE URL
|
|
580
|
+
* @param callbackHost Base host for the agent, used for the redirect URI.
|
|
581
|
+
* @param agentsPrefix agents routing prefix if not using `agents`
|
|
582
|
+
* @param options MCP client and transport (header) options
|
|
583
|
+
* @returns authUrl
|
|
584
|
+
*/
|
|
585
|
+
async addMcpServer(serverName, url, callbackHost, agentsPrefix = "agents", options) {
|
|
586
|
+
const callbackUrl = `${callbackHost}/${agentsPrefix}/${camelCaseToKebabCase(this._ParentClass.name)}/${this.name}/callback`;
|
|
587
|
+
const result = await this._connectToMcpServerInternal(
|
|
588
|
+
serverName,
|
|
589
|
+
url,
|
|
590
|
+
callbackUrl,
|
|
591
|
+
options
|
|
592
|
+
);
|
|
593
|
+
this.sql`
|
|
594
|
+
INSERT
|
|
595
|
+
OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
|
|
596
|
+
VALUES (
|
|
597
|
+
${result.id},
|
|
598
|
+
${serverName},
|
|
599
|
+
${url},
|
|
600
|
+
${result.clientId ?? null},
|
|
601
|
+
${result.authUrl ?? null},
|
|
602
|
+
${callbackUrl},
|
|
603
|
+
${options ? JSON.stringify(options) : null}
|
|
604
|
+
);
|
|
605
|
+
`;
|
|
606
|
+
this.broadcast(
|
|
607
|
+
JSON.stringify({
|
|
608
|
+
mcp: this.getMcpServers(),
|
|
609
|
+
type: "cf_agent_mcp_servers"
|
|
610
|
+
})
|
|
611
|
+
);
|
|
612
|
+
return result;
|
|
613
|
+
}
|
|
614
|
+
async _connectToMcpServerInternal(_serverName, url, callbackUrl, options, reconnect) {
|
|
615
|
+
const authProvider = new DurableObjectOAuthClientProvider(
|
|
616
|
+
this.ctx.storage,
|
|
617
|
+
this.name,
|
|
618
|
+
callbackUrl
|
|
619
|
+
);
|
|
620
|
+
if (reconnect) {
|
|
621
|
+
authProvider.serverId = reconnect.id;
|
|
622
|
+
if (reconnect.oauthClientId) {
|
|
623
|
+
authProvider.clientId = reconnect.oauthClientId;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
let headerTransportOpts = {};
|
|
627
|
+
if (options?.transport?.headers) {
|
|
628
|
+
headerTransportOpts = {
|
|
629
|
+
eventSourceInit: {
|
|
630
|
+
fetch: (url2, init) => fetch(url2, {
|
|
631
|
+
...init,
|
|
632
|
+
headers: options?.transport?.headers
|
|
633
|
+
})
|
|
634
|
+
},
|
|
635
|
+
requestInit: {
|
|
636
|
+
headers: options?.transport?.headers
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
const { id, authUrl, clientId } = await this.mcp.connect(url, {
|
|
641
|
+
client: options?.client,
|
|
642
|
+
reconnect,
|
|
643
|
+
transport: {
|
|
644
|
+
...headerTransportOpts,
|
|
645
|
+
authProvider
|
|
460
646
|
}
|
|
647
|
+
});
|
|
648
|
+
return {
|
|
649
|
+
authUrl,
|
|
650
|
+
clientId,
|
|
651
|
+
id
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
async removeMcpServer(id) {
|
|
655
|
+
this.mcp.closeConnection(id);
|
|
656
|
+
this.sql`
|
|
657
|
+
DELETE FROM cf_agents_mcp_servers WHERE id = ${id};
|
|
658
|
+
`;
|
|
659
|
+
this.broadcast(
|
|
660
|
+
JSON.stringify({
|
|
661
|
+
mcp: this.getMcpServers(),
|
|
662
|
+
type: "cf_agent_mcp_servers"
|
|
663
|
+
})
|
|
461
664
|
);
|
|
462
|
-
});
|
|
463
|
-
};
|
|
464
|
-
tryCatch_fn = async function(fn) {
|
|
465
|
-
try {
|
|
466
|
-
return await fn();
|
|
467
|
-
} catch (e) {
|
|
468
|
-
throw this.onError(e);
|
|
469
665
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
666
|
+
getMcpServers() {
|
|
667
|
+
const mcpState = {
|
|
668
|
+
prompts: this.mcp.listPrompts(),
|
|
669
|
+
resources: this.mcp.listResources(),
|
|
670
|
+
servers: {},
|
|
671
|
+
tools: this.mcp.listTools()
|
|
672
|
+
};
|
|
673
|
+
const servers = this.sql`
|
|
674
|
+
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
477
675
|
`;
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
676
|
+
for (const server of servers) {
|
|
677
|
+
const serverConn = this.mcp.mcpConnections[server.id];
|
|
678
|
+
mcpState.servers[server.id] = {
|
|
679
|
+
auth_url: server.auth_url,
|
|
680
|
+
capabilities: serverConn?.serverCapabilities ?? null,
|
|
681
|
+
instructions: serverConn?.instructions ?? null,
|
|
682
|
+
name: server.name,
|
|
683
|
+
server_url: server.server_url,
|
|
684
|
+
// mark as "authenticating" because the server isn't automatically connected, so it's pending authenticating
|
|
685
|
+
state: serverConn?.connectionState ?? "authenticating"
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
return mcpState;
|
|
482
689
|
}
|
|
483
690
|
};
|
|
484
|
-
/**
|
|
485
|
-
* Get all methods marked as callable on this Agent
|
|
486
|
-
* @returns A map of method names to their metadata
|
|
487
|
-
*/
|
|
488
|
-
isCallable_fn = function(method) {
|
|
489
|
-
return callableMetadata.has(this[method]);
|
|
490
|
-
};
|
|
491
691
|
/**
|
|
492
692
|
* Agent configuration options
|
|
493
693
|
*/
|
|
@@ -498,9 +698,9 @@ Agent.options = {
|
|
|
498
698
|
};
|
|
499
699
|
async function routeAgentRequest(request, env, options) {
|
|
500
700
|
const corsHeaders = options?.cors === true ? {
|
|
501
|
-
"Access-Control-Allow-Origin": "*",
|
|
502
|
-
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
|
|
503
701
|
"Access-Control-Allow-Credentials": "true",
|
|
702
|
+
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
|
|
703
|
+
"Access-Control-Allow-Origin": "*",
|
|
504
704
|
"Access-Control-Max-Age": "86400"
|
|
505
705
|
} : options?.cors;
|
|
506
706
|
if (request.method === "OPTIONS") {
|
|
@@ -531,67 +731,61 @@ async function routeAgentRequest(request, env, options) {
|
|
|
531
731
|
}
|
|
532
732
|
return response;
|
|
533
733
|
}
|
|
534
|
-
async function routeAgentEmail(
|
|
734
|
+
async function routeAgentEmail(_email, _env, _options) {
|
|
535
735
|
}
|
|
536
|
-
function getAgentByName(namespace, name, options) {
|
|
736
|
+
async function getAgentByName(namespace, name, options) {
|
|
537
737
|
return getServerByName(namespace, name, options);
|
|
538
738
|
}
|
|
539
|
-
var _connection, _id, _closed;
|
|
540
739
|
var StreamingResponse = class {
|
|
541
740
|
constructor(connection, id) {
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
__privateSet(this, _connection, connection);
|
|
546
|
-
__privateSet(this, _id, id);
|
|
741
|
+
this._closed = false;
|
|
742
|
+
this._connection = connection;
|
|
743
|
+
this._id = id;
|
|
547
744
|
}
|
|
548
745
|
/**
|
|
549
746
|
* Send a chunk of data to the client
|
|
550
747
|
* @param chunk The data to send
|
|
551
748
|
*/
|
|
552
749
|
send(chunk) {
|
|
553
|
-
if (
|
|
750
|
+
if (this._closed) {
|
|
554
751
|
throw new Error("StreamingResponse is already closed");
|
|
555
752
|
}
|
|
556
753
|
const response = {
|
|
557
|
-
|
|
558
|
-
id:
|
|
559
|
-
success: true,
|
|
754
|
+
done: false,
|
|
755
|
+
id: this._id,
|
|
560
756
|
result: chunk,
|
|
561
|
-
|
|
757
|
+
success: true,
|
|
758
|
+
type: "rpc"
|
|
562
759
|
};
|
|
563
|
-
|
|
760
|
+
this._connection.send(JSON.stringify(response));
|
|
564
761
|
}
|
|
565
762
|
/**
|
|
566
763
|
* End the stream and send the final chunk (if any)
|
|
567
764
|
* @param finalChunk Optional final chunk of data to send
|
|
568
765
|
*/
|
|
569
766
|
end(finalChunk) {
|
|
570
|
-
if (
|
|
767
|
+
if (this._closed) {
|
|
571
768
|
throw new Error("StreamingResponse is already closed");
|
|
572
769
|
}
|
|
573
|
-
|
|
770
|
+
this._closed = true;
|
|
574
771
|
const response = {
|
|
575
|
-
|
|
576
|
-
id:
|
|
577
|
-
success: true,
|
|
772
|
+
done: true,
|
|
773
|
+
id: this._id,
|
|
578
774
|
result: finalChunk,
|
|
579
|
-
|
|
775
|
+
success: true,
|
|
776
|
+
type: "rpc"
|
|
580
777
|
};
|
|
581
|
-
|
|
778
|
+
this._connection.send(JSON.stringify(response));
|
|
582
779
|
}
|
|
583
780
|
};
|
|
584
|
-
_connection = new WeakMap();
|
|
585
|
-
_id = new WeakMap();
|
|
586
|
-
_closed = new WeakMap();
|
|
587
781
|
|
|
588
782
|
export {
|
|
589
783
|
unstable_callable,
|
|
590
|
-
|
|
784
|
+
getCurrentAgent,
|
|
591
785
|
Agent,
|
|
592
786
|
routeAgentRequest,
|
|
593
787
|
routeAgentEmail,
|
|
594
788
|
getAgentByName,
|
|
595
789
|
StreamingResponse
|
|
596
790
|
};
|
|
597
|
-
//# sourceMappingURL=chunk-
|
|
791
|
+
//# sourceMappingURL=chunk-4CIGD73X.js.map
|