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