agents 0.0.0-504a24f → 0.0.0-5320b13
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 +157 -27
- package/dist/ai-chat-agent.d.ts +11 -7
- package/dist/ai-chat-agent.js +168 -47
- 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 +61 -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-767EASBA.js → chunk-LL2AFX7V.js} +5 -2
- package/dist/chunk-LL2AFX7V.js.map +1 -0
- package/dist/{chunk-E3LCYPCB.js → chunk-MH46VMM4.js} +163 -20
- package/dist/chunk-MH46VMM4.js.map +1 -0
- package/dist/{chunk-NKZZ66QY.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-CGWTDCBQ.js → chunk-YDUDMOL6.js} +607 -102
- package/dist/chunk-YDUDMOL6.js.map +1 -0
- package/dist/client-CvaJdLQA.d.ts +5015 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +2 -1
- package/dist/index.d.ts +172 -20
- package/dist/index.js +13 -4
- package/dist/mcp/client.d.ts +9 -781
- 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 +68 -55
- package/dist/mcp/index.js +854 -608
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +46 -0
- package/dist/observability/index.js +11 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +9 -5
- 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 +25 -9
- package/src/index.ts +830 -140
- package/dist/chunk-767EASBA.js.map +0 -1
- package/dist/chunk-CGWTDCBQ.js.map +0 -1
- package/dist/chunk-E3LCYPCB.js.map +0 -1
- package/dist/chunk-NKZZ66QY.js.map +0 -1
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MCPClientManager
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-MH46VMM4.js";
|
|
4
4
|
import {
|
|
5
5
|
DurableObjectOAuthClientProvider
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-LL2AFX7V.js";
|
|
7
7
|
import {
|
|
8
8
|
camelCaseToKebabCase
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-QEVM4BVL.js";
|
|
10
10
|
|
|
11
11
|
// src/index.ts
|
|
12
12
|
import { AsyncLocalStorage } from "async_hooks";
|
|
13
13
|
import { parseCronExpression } from "cron-schedule";
|
|
14
14
|
import { nanoid } from "nanoid";
|
|
15
|
+
import { EmailMessage } from "cloudflare:email";
|
|
15
16
|
import {
|
|
17
|
+
Server,
|
|
16
18
|
getServerByName,
|
|
17
|
-
routePartykitRequest
|
|
18
|
-
Server
|
|
19
|
+
routePartykitRequest
|
|
19
20
|
} from "partyserver";
|
|
20
21
|
function isRPCRequest(msg) {
|
|
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
|
+
return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "rpc" /* RPC */ && "id" in msg && typeof msg.id === "string" && "method" in msg && typeof msg.method === "string" && "args" in msg && Array.isArray(msg.args);
|
|
22
23
|
}
|
|
23
24
|
function isStateUpdateMessage(msg) {
|
|
24
|
-
return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "cf_agent_state" && "state" in msg;
|
|
25
|
+
return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "cf_agent_state" /* CF_AGENT_STATE */ && "state" in msg;
|
|
25
26
|
}
|
|
26
27
|
var callableMetadata = /* @__PURE__ */ new Map();
|
|
27
|
-
function
|
|
28
|
+
function callable(metadata = {}) {
|
|
28
29
|
return function callableDecorator(target, context) {
|
|
29
30
|
if (!callableMetadata.has(target)) {
|
|
30
31
|
callableMetadata.set(target, metadata);
|
|
@@ -32,6 +33,16 @@ function unstable_callable(metadata = {}) {
|
|
|
32
33
|
return target;
|
|
33
34
|
};
|
|
34
35
|
}
|
|
36
|
+
var didWarnAboutUnstableCallable = false;
|
|
37
|
+
var unstable_callable = (metadata = {}) => {
|
|
38
|
+
if (!didWarnAboutUnstableCallable) {
|
|
39
|
+
didWarnAboutUnstableCallable = true;
|
|
40
|
+
console.warn(
|
|
41
|
+
"unstable_callable is deprecated, use callable instead. unstable_callable will be removed in the next major version."
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
callable(metadata);
|
|
45
|
+
};
|
|
35
46
|
function getNextCronTime(cron) {
|
|
36
47
|
const interval = parseCronExpression(cron);
|
|
37
48
|
return interval.getNextDate();
|
|
@@ -46,12 +57,24 @@ function getCurrentAgent() {
|
|
|
46
57
|
return {
|
|
47
58
|
agent: void 0,
|
|
48
59
|
connection: void 0,
|
|
49
|
-
request: void 0
|
|
60
|
+
request: void 0,
|
|
61
|
+
email: void 0
|
|
50
62
|
};
|
|
51
63
|
}
|
|
52
64
|
return store;
|
|
53
65
|
}
|
|
54
|
-
|
|
66
|
+
function withAgentContext(method) {
|
|
67
|
+
return function(...args) {
|
|
68
|
+
const { connection, request, email, agent } = getCurrentAgent();
|
|
69
|
+
if (agent === this) {
|
|
70
|
+
return method.apply(this, args);
|
|
71
|
+
}
|
|
72
|
+
return agentContext.run({ agent: this, connection, request, email }, () => {
|
|
73
|
+
return method.apply(this, args);
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
var _Agent = class _Agent extends Server {
|
|
55
78
|
constructor(ctx, env) {
|
|
56
79
|
super(ctx, env);
|
|
57
80
|
this._state = DEFAULT_STATE;
|
|
@@ -62,6 +85,11 @@ var Agent = class extends Server {
|
|
|
62
85
|
* Override to provide default state values
|
|
63
86
|
*/
|
|
64
87
|
this.initialState = DEFAULT_STATE;
|
|
88
|
+
/**
|
|
89
|
+
* The observability implementation to use for the Agent
|
|
90
|
+
*/
|
|
91
|
+
this.observability = genericObservability;
|
|
92
|
+
this._flushingQueue = false;
|
|
65
93
|
/**
|
|
66
94
|
* Method called when an alarm fires.
|
|
67
95
|
* Executes any scheduled tasks that are due.
|
|
@@ -75,42 +103,71 @@ var Agent = class extends Server {
|
|
|
75
103
|
const result = this.sql`
|
|
76
104
|
SELECT * FROM cf_agents_schedules WHERE time <= ${now}
|
|
77
105
|
`;
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
}
|
|
106
|
+
if (result && Array.isArray(result)) {
|
|
107
|
+
for (const row of result) {
|
|
108
|
+
const callback = this[row.callback];
|
|
109
|
+
if (!callback) {
|
|
110
|
+
console.error(`callback ${row.callback} not found`);
|
|
111
|
+
continue;
|
|
92
112
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
113
|
+
await agentContext.run(
|
|
114
|
+
{
|
|
115
|
+
agent: this,
|
|
116
|
+
connection: void 0,
|
|
117
|
+
request: void 0,
|
|
118
|
+
email: void 0
|
|
119
|
+
},
|
|
120
|
+
async () => {
|
|
121
|
+
try {
|
|
122
|
+
this.observability?.emit(
|
|
123
|
+
{
|
|
124
|
+
displayMessage: `Schedule ${row.id} executed`,
|
|
125
|
+
id: nanoid(),
|
|
126
|
+
payload: {
|
|
127
|
+
callback: row.callback,
|
|
128
|
+
id: row.id
|
|
129
|
+
},
|
|
130
|
+
timestamp: Date.now(),
|
|
131
|
+
type: "schedule:execute"
|
|
132
|
+
},
|
|
133
|
+
this.ctx
|
|
134
|
+
);
|
|
135
|
+
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
console.error(`error executing callback "${row.callback}"`, e);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
if (row.type === "cron") {
|
|
142
|
+
const nextExecutionTime = getNextCronTime(row.cron);
|
|
143
|
+
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
|
|
144
|
+
this.sql`
|
|
98
145
|
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
99
146
|
`;
|
|
100
|
-
|
|
101
|
-
|
|
147
|
+
} else {
|
|
148
|
+
this.sql`
|
|
102
149
|
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
103
150
|
`;
|
|
151
|
+
}
|
|
104
152
|
}
|
|
105
153
|
}
|
|
106
154
|
await this._scheduleNextAlarm();
|
|
107
155
|
};
|
|
156
|
+
this._autoWrapCustomMethods();
|
|
108
157
|
this.sql`
|
|
109
158
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
110
159
|
id TEXT PRIMARY KEY NOT NULL,
|
|
111
160
|
state TEXT
|
|
112
161
|
)
|
|
113
162
|
`;
|
|
163
|
+
this.sql`
|
|
164
|
+
CREATE TABLE IF NOT EXISTS cf_agents_queues (
|
|
165
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
166
|
+
payload TEXT,
|
|
167
|
+
callback TEXT,
|
|
168
|
+
created_at INTEGER DEFAULT (unixepoch())
|
|
169
|
+
)
|
|
170
|
+
`;
|
|
114
171
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
115
172
|
return this._tryCatch(async () => {
|
|
116
173
|
this.sql`
|
|
@@ -142,14 +199,14 @@ var Agent = class extends Server {
|
|
|
142
199
|
const _onRequest = this.onRequest.bind(this);
|
|
143
200
|
this.onRequest = (request) => {
|
|
144
201
|
return agentContext.run(
|
|
145
|
-
{ agent: this, connection: void 0, request },
|
|
202
|
+
{ agent: this, connection: void 0, request, email: void 0 },
|
|
146
203
|
async () => {
|
|
147
204
|
if (this.mcp.isCallbackRequest(request)) {
|
|
148
205
|
await this.mcp.handleCallbackRequest(request);
|
|
149
206
|
this.broadcast(
|
|
150
207
|
JSON.stringify({
|
|
151
208
|
mcp: this.getMcpServers(),
|
|
152
|
-
type: "cf_agent_mcp_servers"
|
|
209
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
153
210
|
})
|
|
154
211
|
);
|
|
155
212
|
return new Response("<script>window.close();</script>", {
|
|
@@ -164,7 +221,7 @@ var Agent = class extends Server {
|
|
|
164
221
|
const _onMessage = this.onMessage.bind(this);
|
|
165
222
|
this.onMessage = async (connection, message) => {
|
|
166
223
|
return agentContext.run(
|
|
167
|
-
{ agent: this, connection, request: void 0 },
|
|
224
|
+
{ agent: this, connection, request: void 0, email: void 0 },
|
|
168
225
|
async () => {
|
|
169
226
|
if (typeof message !== "string") {
|
|
170
227
|
return this._tryCatch(() => _onMessage(connection, message));
|
|
@@ -196,12 +253,25 @@ var Agent = class extends Server {
|
|
|
196
253
|
return;
|
|
197
254
|
}
|
|
198
255
|
const result = await methodFn.apply(this, args);
|
|
256
|
+
this.observability?.emit(
|
|
257
|
+
{
|
|
258
|
+
displayMessage: `RPC call to ${method}`,
|
|
259
|
+
id: nanoid(),
|
|
260
|
+
payload: {
|
|
261
|
+
method,
|
|
262
|
+
streaming: metadata?.streaming
|
|
263
|
+
},
|
|
264
|
+
timestamp: Date.now(),
|
|
265
|
+
type: "rpc"
|
|
266
|
+
},
|
|
267
|
+
this.ctx
|
|
268
|
+
);
|
|
199
269
|
const response = {
|
|
200
270
|
done: true,
|
|
201
271
|
id,
|
|
202
272
|
result,
|
|
203
273
|
success: true,
|
|
204
|
-
type: "rpc"
|
|
274
|
+
type: "rpc" /* RPC */
|
|
205
275
|
};
|
|
206
276
|
connection.send(JSON.stringify(response));
|
|
207
277
|
} catch (e) {
|
|
@@ -209,7 +279,7 @@ var Agent = class extends Server {
|
|
|
209
279
|
error: e instanceof Error ? e.message : "Unknown error occurred",
|
|
210
280
|
id: parsed.id,
|
|
211
281
|
success: false,
|
|
212
|
-
type: "rpc"
|
|
282
|
+
type: "rpc" /* RPC */
|
|
213
283
|
};
|
|
214
284
|
connection.send(JSON.stringify(response));
|
|
215
285
|
console.error("RPC error:", e);
|
|
@@ -223,58 +293,92 @@ var Agent = class extends Server {
|
|
|
223
293
|
const _onConnect = this.onConnect.bind(this);
|
|
224
294
|
this.onConnect = (connection, ctx2) => {
|
|
225
295
|
return agentContext.run(
|
|
226
|
-
{ agent: this, connection, request: ctx2.request },
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (this.state) {
|
|
230
|
-
connection.send(
|
|
231
|
-
JSON.stringify({
|
|
232
|
-
state: this.state,
|
|
233
|
-
type: "cf_agent_state"
|
|
234
|
-
})
|
|
235
|
-
);
|
|
236
|
-
}
|
|
296
|
+
{ agent: this, connection, request: ctx2.request, email: void 0 },
|
|
297
|
+
() => {
|
|
298
|
+
if (this.state) {
|
|
237
299
|
connection.send(
|
|
238
300
|
JSON.stringify({
|
|
239
|
-
|
|
240
|
-
type: "
|
|
301
|
+
state: this.state,
|
|
302
|
+
type: "cf_agent_state" /* CF_AGENT_STATE */
|
|
241
303
|
})
|
|
242
304
|
);
|
|
243
|
-
|
|
244
|
-
|
|
305
|
+
}
|
|
306
|
+
connection.send(
|
|
307
|
+
JSON.stringify({
|
|
308
|
+
mcp: this.getMcpServers(),
|
|
309
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
310
|
+
})
|
|
311
|
+
);
|
|
312
|
+
this.observability?.emit(
|
|
313
|
+
{
|
|
314
|
+
displayMessage: "Connection established",
|
|
315
|
+
id: nanoid(),
|
|
316
|
+
payload: {
|
|
317
|
+
connectionId: connection.id
|
|
318
|
+
},
|
|
319
|
+
timestamp: Date.now(),
|
|
320
|
+
type: "connect"
|
|
321
|
+
},
|
|
322
|
+
this.ctx
|
|
323
|
+
);
|
|
324
|
+
return this._tryCatch(() => _onConnect(connection, ctx2));
|
|
245
325
|
}
|
|
246
326
|
);
|
|
247
327
|
};
|
|
248
328
|
const _onStart = this.onStart.bind(this);
|
|
249
|
-
this.onStart = async () => {
|
|
329
|
+
this.onStart = async (props) => {
|
|
250
330
|
return agentContext.run(
|
|
251
|
-
{
|
|
331
|
+
{
|
|
332
|
+
agent: this,
|
|
333
|
+
connection: void 0,
|
|
334
|
+
request: void 0,
|
|
335
|
+
email: void 0
|
|
336
|
+
},
|
|
252
337
|
async () => {
|
|
253
|
-
|
|
338
|
+
await this._tryCatch(() => {
|
|
339
|
+
const servers = this.sql`
|
|
254
340
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
255
341
|
`;
|
|
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
342
|
this.broadcast(
|
|
271
343
|
JSON.stringify({
|
|
272
|
-
|
|
273
|
-
|
|
344
|
+
mcp: this.getMcpServers(),
|
|
345
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
274
346
|
})
|
|
275
347
|
);
|
|
348
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
349
|
+
servers.forEach((server) => {
|
|
350
|
+
this._connectToMcpServerInternal(
|
|
351
|
+
server.name,
|
|
352
|
+
server.server_url,
|
|
353
|
+
server.callback_url,
|
|
354
|
+
server.server_options ? JSON.parse(server.server_options) : void 0,
|
|
355
|
+
{
|
|
356
|
+
id: server.id,
|
|
357
|
+
oauthClientId: server.client_id ?? void 0
|
|
358
|
+
}
|
|
359
|
+
).then(() => {
|
|
360
|
+
this.broadcast(
|
|
361
|
+
JSON.stringify({
|
|
362
|
+
mcp: this.getMcpServers(),
|
|
363
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
364
|
+
})
|
|
365
|
+
);
|
|
366
|
+
}).catch((error) => {
|
|
367
|
+
console.error(
|
|
368
|
+
`Error connecting to MCP server: ${server.name} (${server.server_url})`,
|
|
369
|
+
error
|
|
370
|
+
);
|
|
371
|
+
this.broadcast(
|
|
372
|
+
JSON.stringify({
|
|
373
|
+
mcp: this.getMcpServers(),
|
|
374
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
375
|
+
})
|
|
376
|
+
);
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
return _onStart(props);
|
|
276
381
|
});
|
|
277
|
-
await this._tryCatch(() => _onStart());
|
|
278
382
|
}
|
|
279
383
|
);
|
|
280
384
|
};
|
|
@@ -337,15 +441,25 @@ var Agent = class extends Server {
|
|
|
337
441
|
this.broadcast(
|
|
338
442
|
JSON.stringify({
|
|
339
443
|
state,
|
|
340
|
-
type: "cf_agent_state"
|
|
444
|
+
type: "cf_agent_state" /* CF_AGENT_STATE */
|
|
341
445
|
}),
|
|
342
446
|
source !== "server" ? [source.id] : []
|
|
343
447
|
);
|
|
344
448
|
return this._tryCatch(() => {
|
|
345
|
-
const { connection, request } = agentContext.getStore() || {};
|
|
449
|
+
const { connection, request, email } = agentContext.getStore() || {};
|
|
346
450
|
return agentContext.run(
|
|
347
|
-
{ agent: this, connection, request },
|
|
451
|
+
{ agent: this, connection, request, email },
|
|
348
452
|
async () => {
|
|
453
|
+
this.observability?.emit(
|
|
454
|
+
{
|
|
455
|
+
displayMessage: "State updated",
|
|
456
|
+
id: nanoid(),
|
|
457
|
+
payload: {},
|
|
458
|
+
timestamp: Date.now(),
|
|
459
|
+
type: "state:update"
|
|
460
|
+
},
|
|
461
|
+
this.ctx
|
|
462
|
+
);
|
|
349
463
|
return this.onStateUpdate(state, source);
|
|
350
464
|
}
|
|
351
465
|
);
|
|
@@ -367,18 +481,67 @@ var Agent = class extends Server {
|
|
|
367
481
|
onStateUpdate(state, source) {
|
|
368
482
|
}
|
|
369
483
|
/**
|
|
370
|
-
* Called when the Agent receives an email
|
|
484
|
+
* Called when the Agent receives an email via routeAgentEmail()
|
|
485
|
+
* Override this method to handle incoming emails
|
|
371
486
|
* @param email Email message to process
|
|
372
487
|
*/
|
|
373
|
-
|
|
374
|
-
onEmail(email) {
|
|
488
|
+
async _onEmail(email) {
|
|
375
489
|
return agentContext.run(
|
|
376
|
-
{ agent: this, connection: void 0, request: void 0 },
|
|
490
|
+
{ agent: this, connection: void 0, request: void 0, email },
|
|
377
491
|
async () => {
|
|
378
|
-
|
|
492
|
+
if ("onEmail" in this && typeof this.onEmail === "function") {
|
|
493
|
+
return this._tryCatch(
|
|
494
|
+
() => this.onEmail(email)
|
|
495
|
+
);
|
|
496
|
+
} else {
|
|
497
|
+
console.log("Received email from:", email.from, "to:", email.to);
|
|
498
|
+
console.log("Subject:", email.headers.get("subject"));
|
|
499
|
+
console.log(
|
|
500
|
+
"Implement onEmail(email: AgentEmail): Promise<void> in your agent to process emails"
|
|
501
|
+
);
|
|
502
|
+
}
|
|
379
503
|
}
|
|
380
504
|
);
|
|
381
505
|
}
|
|
506
|
+
/**
|
|
507
|
+
* Reply to an email
|
|
508
|
+
* @param email The email to reply to
|
|
509
|
+
* @param options Options for the reply
|
|
510
|
+
* @returns void
|
|
511
|
+
*/
|
|
512
|
+
async replyToEmail(email, options) {
|
|
513
|
+
return this._tryCatch(async () => {
|
|
514
|
+
const agentName = camelCaseToKebabCase(this._ParentClass.name);
|
|
515
|
+
const agentId = this.name;
|
|
516
|
+
const { createMimeMessage } = await import("mimetext");
|
|
517
|
+
const msg = createMimeMessage();
|
|
518
|
+
msg.setSender({ addr: email.to, name: options.fromName });
|
|
519
|
+
msg.setRecipient(email.from);
|
|
520
|
+
msg.setSubject(
|
|
521
|
+
options.subject || `Re: ${email.headers.get("subject")}` || "No subject"
|
|
522
|
+
);
|
|
523
|
+
msg.addMessage({
|
|
524
|
+
contentType: options.contentType || "text/plain",
|
|
525
|
+
data: options.body
|
|
526
|
+
});
|
|
527
|
+
const domain = email.from.split("@")[1];
|
|
528
|
+
const messageId = `<${agentId}@${domain}>`;
|
|
529
|
+
msg.setHeader("In-Reply-To", email.headers.get("Message-ID"));
|
|
530
|
+
msg.setHeader("Message-ID", messageId);
|
|
531
|
+
msg.setHeader("X-Agent-Name", agentName);
|
|
532
|
+
msg.setHeader("X-Agent-ID", agentId);
|
|
533
|
+
if (options.headers) {
|
|
534
|
+
for (const [key, value] of Object.entries(options.headers)) {
|
|
535
|
+
msg.setHeader(key, value);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
await email.reply({
|
|
539
|
+
from: email.to,
|
|
540
|
+
raw: msg.asRaw(),
|
|
541
|
+
to: email.from
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
}
|
|
382
545
|
async _tryCatch(fn) {
|
|
383
546
|
try {
|
|
384
547
|
return await fn();
|
|
@@ -386,6 +549,49 @@ var Agent = class extends Server {
|
|
|
386
549
|
throw this.onError(e);
|
|
387
550
|
}
|
|
388
551
|
}
|
|
552
|
+
/**
|
|
553
|
+
* Automatically wrap custom methods with agent context
|
|
554
|
+
* This ensures getCurrentAgent() works in all custom methods without decorators
|
|
555
|
+
*/
|
|
556
|
+
_autoWrapCustomMethods() {
|
|
557
|
+
const basePrototypes = [_Agent.prototype, Server.prototype];
|
|
558
|
+
const baseMethods = /* @__PURE__ */ new Set();
|
|
559
|
+
for (const baseProto of basePrototypes) {
|
|
560
|
+
let proto2 = baseProto;
|
|
561
|
+
while (proto2 && proto2 !== Object.prototype) {
|
|
562
|
+
const methodNames = Object.getOwnPropertyNames(proto2);
|
|
563
|
+
for (const methodName of methodNames) {
|
|
564
|
+
baseMethods.add(methodName);
|
|
565
|
+
}
|
|
566
|
+
proto2 = Object.getPrototypeOf(proto2);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
let proto = Object.getPrototypeOf(this);
|
|
570
|
+
let depth = 0;
|
|
571
|
+
while (proto && proto !== Object.prototype && depth < 10) {
|
|
572
|
+
const methodNames = Object.getOwnPropertyNames(proto);
|
|
573
|
+
for (const methodName of methodNames) {
|
|
574
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
575
|
+
if (baseMethods.has(methodName) || methodName.startsWith("_") || !descriptor || !!descriptor.get || typeof descriptor.value !== "function") {
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
const wrappedFunction = withAgentContext(
|
|
579
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
580
|
+
this[methodName]
|
|
581
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
582
|
+
);
|
|
583
|
+
if (this._isCallable(methodName)) {
|
|
584
|
+
callableMetadata.set(
|
|
585
|
+
wrappedFunction,
|
|
586
|
+
callableMetadata.get(this[methodName])
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
this.constructor.prototype[methodName] = wrappedFunction;
|
|
590
|
+
}
|
|
591
|
+
proto = Object.getPrototypeOf(proto);
|
|
592
|
+
depth++;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
389
595
|
onError(connectionOrError, error) {
|
|
390
596
|
let theError;
|
|
391
597
|
if (connectionOrError && error) {
|
|
@@ -411,6 +617,108 @@ var Agent = class extends Server {
|
|
|
411
617
|
render() {
|
|
412
618
|
throw new Error("Not implemented");
|
|
413
619
|
}
|
|
620
|
+
/**
|
|
621
|
+
* Queue a task to be executed in the future
|
|
622
|
+
* @param payload Payload to pass to the callback
|
|
623
|
+
* @param callback Name of the method to call
|
|
624
|
+
* @returns The ID of the queued task
|
|
625
|
+
*/
|
|
626
|
+
async queue(callback, payload) {
|
|
627
|
+
const id = nanoid(9);
|
|
628
|
+
if (typeof callback !== "string") {
|
|
629
|
+
throw new Error("Callback must be a string");
|
|
630
|
+
}
|
|
631
|
+
if (typeof this[callback] !== "function") {
|
|
632
|
+
throw new Error(`this.${callback} is not a function`);
|
|
633
|
+
}
|
|
634
|
+
this.sql`
|
|
635
|
+
INSERT OR REPLACE INTO cf_agents_queues (id, payload, callback)
|
|
636
|
+
VALUES (${id}, ${JSON.stringify(payload)}, ${callback})
|
|
637
|
+
`;
|
|
638
|
+
void this._flushQueue().catch((e) => {
|
|
639
|
+
console.error("Error flushing queue:", e);
|
|
640
|
+
});
|
|
641
|
+
return id;
|
|
642
|
+
}
|
|
643
|
+
async _flushQueue() {
|
|
644
|
+
if (this._flushingQueue) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
this._flushingQueue = true;
|
|
648
|
+
while (true) {
|
|
649
|
+
const result = this.sql`
|
|
650
|
+
SELECT * FROM cf_agents_queues
|
|
651
|
+
ORDER BY created_at ASC
|
|
652
|
+
`;
|
|
653
|
+
if (!result || result.length === 0) {
|
|
654
|
+
break;
|
|
655
|
+
}
|
|
656
|
+
for (const row of result || []) {
|
|
657
|
+
const callback = this[row.callback];
|
|
658
|
+
if (!callback) {
|
|
659
|
+
console.error(`callback ${row.callback} not found`);
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
const { connection, request, email } = agentContext.getStore() || {};
|
|
663
|
+
await agentContext.run(
|
|
664
|
+
{
|
|
665
|
+
agent: this,
|
|
666
|
+
connection,
|
|
667
|
+
request,
|
|
668
|
+
email
|
|
669
|
+
},
|
|
670
|
+
async () => {
|
|
671
|
+
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
672
|
+
await this.dequeue(row.id);
|
|
673
|
+
}
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
this._flushingQueue = false;
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Dequeue a task by ID
|
|
681
|
+
* @param id ID of the task to dequeue
|
|
682
|
+
*/
|
|
683
|
+
async dequeue(id) {
|
|
684
|
+
this.sql`DELETE FROM cf_agents_queues WHERE id = ${id}`;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Dequeue all tasks
|
|
688
|
+
*/
|
|
689
|
+
async dequeueAll() {
|
|
690
|
+
this.sql`DELETE FROM cf_agents_queues`;
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Dequeue all tasks by callback
|
|
694
|
+
* @param callback Name of the callback to dequeue
|
|
695
|
+
*/
|
|
696
|
+
async dequeueAllByCallback(callback) {
|
|
697
|
+
this.sql`DELETE FROM cf_agents_queues WHERE callback = ${callback}`;
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Get a queued task by ID
|
|
701
|
+
* @param id ID of the task to get
|
|
702
|
+
* @returns The task or undefined if not found
|
|
703
|
+
*/
|
|
704
|
+
async getQueue(id) {
|
|
705
|
+
const result = this.sql`
|
|
706
|
+
SELECT * FROM cf_agents_queues WHERE id = ${id}
|
|
707
|
+
`;
|
|
708
|
+
return result ? { ...result[0], payload: JSON.parse(result[0].payload) } : void 0;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Get all queues by key and value
|
|
712
|
+
* @param key Key to filter by
|
|
713
|
+
* @param value Value to filter by
|
|
714
|
+
* @returns Array of matching QueueItem objects
|
|
715
|
+
*/
|
|
716
|
+
async getQueues(key, value) {
|
|
717
|
+
const result = this.sql`
|
|
718
|
+
SELECT * FROM cf_agents_queues
|
|
719
|
+
`;
|
|
720
|
+
return result.filter((row) => JSON.parse(row.payload)[key] === value);
|
|
721
|
+
}
|
|
414
722
|
/**
|
|
415
723
|
* Schedule a task to be executed in the future
|
|
416
724
|
* @template T Type of the payload data
|
|
@@ -421,6 +729,19 @@ var Agent = class extends Server {
|
|
|
421
729
|
*/
|
|
422
730
|
async schedule(when, callback, payload) {
|
|
423
731
|
const id = nanoid(9);
|
|
732
|
+
const emitScheduleCreate = (schedule) => this.observability?.emit(
|
|
733
|
+
{
|
|
734
|
+
displayMessage: `Schedule ${schedule.id} created`,
|
|
735
|
+
id: nanoid(),
|
|
736
|
+
payload: {
|
|
737
|
+
callback,
|
|
738
|
+
id
|
|
739
|
+
},
|
|
740
|
+
timestamp: Date.now(),
|
|
741
|
+
type: "schedule:create"
|
|
742
|
+
},
|
|
743
|
+
this.ctx
|
|
744
|
+
);
|
|
424
745
|
if (typeof callback !== "string") {
|
|
425
746
|
throw new Error("Callback must be a string");
|
|
426
747
|
}
|
|
@@ -436,13 +757,15 @@ var Agent = class extends Server {
|
|
|
436
757
|
)}, 'scheduled', ${timestamp})
|
|
437
758
|
`;
|
|
438
759
|
await this._scheduleNextAlarm();
|
|
439
|
-
|
|
760
|
+
const schedule = {
|
|
440
761
|
callback,
|
|
441
762
|
id,
|
|
442
763
|
payload,
|
|
443
764
|
time: timestamp,
|
|
444
765
|
type: "scheduled"
|
|
445
766
|
};
|
|
767
|
+
emitScheduleCreate(schedule);
|
|
768
|
+
return schedule;
|
|
446
769
|
}
|
|
447
770
|
if (typeof when === "number") {
|
|
448
771
|
const time = new Date(Date.now() + when * 1e3);
|
|
@@ -454,7 +777,7 @@ var Agent = class extends Server {
|
|
|
454
777
|
)}, 'delayed', ${when}, ${timestamp})
|
|
455
778
|
`;
|
|
456
779
|
await this._scheduleNextAlarm();
|
|
457
|
-
|
|
780
|
+
const schedule = {
|
|
458
781
|
callback,
|
|
459
782
|
delayInSeconds: when,
|
|
460
783
|
id,
|
|
@@ -462,6 +785,8 @@ var Agent = class extends Server {
|
|
|
462
785
|
time: timestamp,
|
|
463
786
|
type: "delayed"
|
|
464
787
|
};
|
|
788
|
+
emitScheduleCreate(schedule);
|
|
789
|
+
return schedule;
|
|
465
790
|
}
|
|
466
791
|
if (typeof when === "string") {
|
|
467
792
|
const nextExecutionTime = getNextCronTime(when);
|
|
@@ -473,7 +798,7 @@ var Agent = class extends Server {
|
|
|
473
798
|
)}, 'cron', ${when}, ${timestamp})
|
|
474
799
|
`;
|
|
475
800
|
await this._scheduleNextAlarm();
|
|
476
|
-
|
|
801
|
+
const schedule = {
|
|
477
802
|
callback,
|
|
478
803
|
cron: when,
|
|
479
804
|
id,
|
|
@@ -481,6 +806,8 @@ var Agent = class extends Server {
|
|
|
481
806
|
time: timestamp,
|
|
482
807
|
type: "cron"
|
|
483
808
|
};
|
|
809
|
+
emitScheduleCreate(schedule);
|
|
810
|
+
return schedule;
|
|
484
811
|
}
|
|
485
812
|
throw new Error("Invalid schedule type");
|
|
486
813
|
}
|
|
@@ -538,15 +865,31 @@ var Agent = class extends Server {
|
|
|
538
865
|
* @returns true if the task was cancelled, false otherwise
|
|
539
866
|
*/
|
|
540
867
|
async cancelSchedule(id) {
|
|
868
|
+
const schedule = await this.getSchedule(id);
|
|
869
|
+
if (schedule) {
|
|
870
|
+
this.observability?.emit(
|
|
871
|
+
{
|
|
872
|
+
displayMessage: `Schedule ${id} cancelled`,
|
|
873
|
+
id: nanoid(),
|
|
874
|
+
payload: {
|
|
875
|
+
callback: schedule.callback,
|
|
876
|
+
id: schedule.id
|
|
877
|
+
},
|
|
878
|
+
timestamp: Date.now(),
|
|
879
|
+
type: "schedule:cancel"
|
|
880
|
+
},
|
|
881
|
+
this.ctx
|
|
882
|
+
);
|
|
883
|
+
}
|
|
541
884
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
542
885
|
await this._scheduleNextAlarm();
|
|
543
886
|
return true;
|
|
544
887
|
}
|
|
545
888
|
async _scheduleNextAlarm() {
|
|
546
889
|
const result = this.sql`
|
|
547
|
-
SELECT time FROM cf_agents_schedules
|
|
890
|
+
SELECT time FROM cf_agents_schedules
|
|
548
891
|
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
549
|
-
ORDER BY time ASC
|
|
892
|
+
ORDER BY time ASC
|
|
550
893
|
LIMIT 1
|
|
551
894
|
`;
|
|
552
895
|
if (!result) return;
|
|
@@ -562,9 +905,20 @@ var Agent = class extends Server {
|
|
|
562
905
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
563
906
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
564
907
|
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
908
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_queues`;
|
|
565
909
|
await this.ctx.storage.deleteAlarm();
|
|
566
910
|
await this.ctx.storage.deleteAll();
|
|
567
911
|
this.ctx.abort("destroyed");
|
|
912
|
+
this.observability?.emit(
|
|
913
|
+
{
|
|
914
|
+
displayMessage: "Agent destroyed",
|
|
915
|
+
id: nanoid(),
|
|
916
|
+
payload: {},
|
|
917
|
+
timestamp: Date.now(),
|
|
918
|
+
type: "destroy"
|
|
919
|
+
},
|
|
920
|
+
this.ctx
|
|
921
|
+
);
|
|
568
922
|
}
|
|
569
923
|
/**
|
|
570
924
|
* Get all methods marked as callable on this Agent
|
|
@@ -606,7 +960,7 @@ var Agent = class extends Server {
|
|
|
606
960
|
this.broadcast(
|
|
607
961
|
JSON.stringify({
|
|
608
962
|
mcp: this.getMcpServers(),
|
|
609
|
-
type: "cf_agent_mcp_servers"
|
|
963
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
610
964
|
})
|
|
611
965
|
);
|
|
612
966
|
return result;
|
|
@@ -659,7 +1013,7 @@ var Agent = class extends Server {
|
|
|
659
1013
|
this.broadcast(
|
|
660
1014
|
JSON.stringify({
|
|
661
1015
|
mcp: this.getMcpServers(),
|
|
662
|
-
type: "cf_agent_mcp_servers"
|
|
1016
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
663
1017
|
})
|
|
664
1018
|
);
|
|
665
1019
|
}
|
|
@@ -673,17 +1027,19 @@ var Agent = class extends Server {
|
|
|
673
1027
|
const servers = this.sql`
|
|
674
1028
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
675
1029
|
`;
|
|
676
|
-
|
|
677
|
-
const
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
1030
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
1031
|
+
for (const server of servers) {
|
|
1032
|
+
const serverConn = this.mcp.mcpConnections[server.id];
|
|
1033
|
+
mcpState.servers[server.id] = {
|
|
1034
|
+
auth_url: server.auth_url,
|
|
1035
|
+
capabilities: serverConn?.serverCapabilities ?? null,
|
|
1036
|
+
instructions: serverConn?.instructions ?? null,
|
|
1037
|
+
name: server.name,
|
|
1038
|
+
server_url: server.server_url,
|
|
1039
|
+
// mark as "authenticating" because the server isn't automatically connected, so it's pending authenticating
|
|
1040
|
+
state: serverConn?.connectionState ?? "authenticating"
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
687
1043
|
}
|
|
688
1044
|
return mcpState;
|
|
689
1045
|
}
|
|
@@ -691,11 +1047,12 @@ var Agent = class extends Server {
|
|
|
691
1047
|
/**
|
|
692
1048
|
* Agent configuration options
|
|
693
1049
|
*/
|
|
694
|
-
|
|
1050
|
+
_Agent.options = {
|
|
695
1051
|
/** Whether the Agent should hibernate when inactive */
|
|
696
1052
|
hibernate: true
|
|
697
1053
|
// default to hibernate
|
|
698
1054
|
};
|
|
1055
|
+
var Agent = _Agent;
|
|
699
1056
|
async function routeAgentRequest(request, env, options) {
|
|
700
1057
|
const corsHeaders = options?.cors === true ? {
|
|
701
1058
|
"Access-Control-Allow-Credentials": "true",
|
|
@@ -731,7 +1088,126 @@ async function routeAgentRequest(request, env, options) {
|
|
|
731
1088
|
}
|
|
732
1089
|
return response;
|
|
733
1090
|
}
|
|
734
|
-
|
|
1091
|
+
function createHeaderBasedEmailResolver() {
|
|
1092
|
+
return async (email, _env) => {
|
|
1093
|
+
const messageId = email.headers.get("message-id");
|
|
1094
|
+
if (messageId) {
|
|
1095
|
+
const messageIdMatch = messageId.match(/<([^@]+)@([^>]+)>/);
|
|
1096
|
+
if (messageIdMatch) {
|
|
1097
|
+
const [, agentId2, domain] = messageIdMatch;
|
|
1098
|
+
const agentName2 = domain.split(".")[0];
|
|
1099
|
+
return { agentName: agentName2, agentId: agentId2 };
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
const references = email.headers.get("references");
|
|
1103
|
+
if (references) {
|
|
1104
|
+
const referencesMatch = references.match(
|
|
1105
|
+
/<([A-Za-z0-9+/]{43}=)@([^>]+)>/
|
|
1106
|
+
);
|
|
1107
|
+
if (referencesMatch) {
|
|
1108
|
+
const [, base64Id, domain] = referencesMatch;
|
|
1109
|
+
const agentId2 = Buffer.from(base64Id, "base64").toString("hex");
|
|
1110
|
+
const agentName2 = domain.split(".")[0];
|
|
1111
|
+
return { agentName: agentName2, agentId: agentId2 };
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
const agentName = email.headers.get("x-agent-name");
|
|
1115
|
+
const agentId = email.headers.get("x-agent-id");
|
|
1116
|
+
if (agentName && agentId) {
|
|
1117
|
+
return { agentName, agentId };
|
|
1118
|
+
}
|
|
1119
|
+
return null;
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
1122
|
+
function createAddressBasedEmailResolver(defaultAgentName) {
|
|
1123
|
+
return async (email, _env) => {
|
|
1124
|
+
const emailMatch = email.to.match(/^([^+@]+)(?:\+([^@]+))?@(.+)$/);
|
|
1125
|
+
if (!emailMatch) {
|
|
1126
|
+
return null;
|
|
1127
|
+
}
|
|
1128
|
+
const [, localPart, subAddress] = emailMatch;
|
|
1129
|
+
if (subAddress) {
|
|
1130
|
+
return {
|
|
1131
|
+
agentName: localPart,
|
|
1132
|
+
agentId: subAddress
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
return {
|
|
1136
|
+
agentName: defaultAgentName,
|
|
1137
|
+
agentId: localPart
|
|
1138
|
+
};
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
function createCatchAllEmailResolver(agentName, agentId) {
|
|
1142
|
+
return async () => ({ agentName, agentId });
|
|
1143
|
+
}
|
|
1144
|
+
var agentMapCache = /* @__PURE__ */ new WeakMap();
|
|
1145
|
+
async function routeAgentEmail(email, env, options) {
|
|
1146
|
+
const routingInfo = await options.resolver(email, env);
|
|
1147
|
+
if (!routingInfo) {
|
|
1148
|
+
console.warn("No routing information found for email, dropping message");
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
if (!agentMapCache.has(env)) {
|
|
1152
|
+
const map = {};
|
|
1153
|
+
for (const [key, value] of Object.entries(env)) {
|
|
1154
|
+
if (value && typeof value === "object" && "idFromName" in value && typeof value.idFromName === "function") {
|
|
1155
|
+
map[key] = value;
|
|
1156
|
+
map[camelCaseToKebabCase(key)] = value;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
agentMapCache.set(env, map);
|
|
1160
|
+
}
|
|
1161
|
+
const agentMap = agentMapCache.get(env);
|
|
1162
|
+
const namespace = agentMap[routingInfo.agentName];
|
|
1163
|
+
if (!namespace) {
|
|
1164
|
+
const availableAgents = Object.keys(agentMap).filter((key) => !key.includes("-")).join(", ");
|
|
1165
|
+
throw new Error(
|
|
1166
|
+
`Agent namespace '${routingInfo.agentName}' not found in environment. Available agents: ${availableAgents}`
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
const agent = await getAgentByName(
|
|
1170
|
+
namespace,
|
|
1171
|
+
routingInfo.agentId
|
|
1172
|
+
);
|
|
1173
|
+
const serialisableEmail = {
|
|
1174
|
+
getRaw: async () => {
|
|
1175
|
+
const reader = email.raw.getReader();
|
|
1176
|
+
const chunks = [];
|
|
1177
|
+
let done = false;
|
|
1178
|
+
while (!done) {
|
|
1179
|
+
const { value, done: readerDone } = await reader.read();
|
|
1180
|
+
done = readerDone;
|
|
1181
|
+
if (value) {
|
|
1182
|
+
chunks.push(value);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
1186
|
+
const combined = new Uint8Array(totalLength);
|
|
1187
|
+
let offset = 0;
|
|
1188
|
+
for (const chunk of chunks) {
|
|
1189
|
+
combined.set(chunk, offset);
|
|
1190
|
+
offset += chunk.length;
|
|
1191
|
+
}
|
|
1192
|
+
return combined;
|
|
1193
|
+
},
|
|
1194
|
+
headers: email.headers,
|
|
1195
|
+
rawSize: email.rawSize,
|
|
1196
|
+
setReject: (reason) => {
|
|
1197
|
+
email.setReject(reason);
|
|
1198
|
+
},
|
|
1199
|
+
forward: (rcptTo, headers) => {
|
|
1200
|
+
return email.forward(rcptTo, headers);
|
|
1201
|
+
},
|
|
1202
|
+
reply: (options2) => {
|
|
1203
|
+
return email.reply(
|
|
1204
|
+
new EmailMessage(options2.from, options2.to, options2.raw)
|
|
1205
|
+
);
|
|
1206
|
+
},
|
|
1207
|
+
from: email.from,
|
|
1208
|
+
to: email.to
|
|
1209
|
+
};
|
|
1210
|
+
await agent._onEmail(serialisableEmail);
|
|
735
1211
|
}
|
|
736
1212
|
async function getAgentByName(namespace, name, options) {
|
|
737
1213
|
return getServerByName(namespace, name, options);
|
|
@@ -755,7 +1231,7 @@ var StreamingResponse = class {
|
|
|
755
1231
|
id: this._id,
|
|
756
1232
|
result: chunk,
|
|
757
1233
|
success: true,
|
|
758
|
-
type: "rpc"
|
|
1234
|
+
type: "rpc" /* RPC */
|
|
759
1235
|
};
|
|
760
1236
|
this._connection.send(JSON.stringify(response));
|
|
761
1237
|
}
|
|
@@ -773,19 +1249,48 @@ var StreamingResponse = class {
|
|
|
773
1249
|
id: this._id,
|
|
774
1250
|
result: finalChunk,
|
|
775
1251
|
success: true,
|
|
776
|
-
type: "rpc"
|
|
1252
|
+
type: "rpc" /* RPC */
|
|
777
1253
|
};
|
|
778
1254
|
this._connection.send(JSON.stringify(response));
|
|
779
1255
|
}
|
|
780
1256
|
};
|
|
781
1257
|
|
|
1258
|
+
// src/observability/index.ts
|
|
1259
|
+
var genericObservability = {
|
|
1260
|
+
emit(event) {
|
|
1261
|
+
if (isLocalMode()) {
|
|
1262
|
+
console.log(event.displayMessage);
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
console.log(event);
|
|
1266
|
+
}
|
|
1267
|
+
};
|
|
1268
|
+
var localMode = false;
|
|
1269
|
+
function isLocalMode() {
|
|
1270
|
+
if (localMode) {
|
|
1271
|
+
return true;
|
|
1272
|
+
}
|
|
1273
|
+
const { request } = getCurrentAgent();
|
|
1274
|
+
if (!request) {
|
|
1275
|
+
return false;
|
|
1276
|
+
}
|
|
1277
|
+
const url = new URL(request.url);
|
|
1278
|
+
localMode = url.hostname === "localhost";
|
|
1279
|
+
return localMode;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
782
1282
|
export {
|
|
1283
|
+
genericObservability,
|
|
1284
|
+
callable,
|
|
783
1285
|
unstable_callable,
|
|
784
1286
|
getCurrentAgent,
|
|
785
1287
|
Agent,
|
|
786
1288
|
routeAgentRequest,
|
|
1289
|
+
createHeaderBasedEmailResolver,
|
|
1290
|
+
createAddressBasedEmailResolver,
|
|
1291
|
+
createCatchAllEmailResolver,
|
|
787
1292
|
routeAgentEmail,
|
|
788
1293
|
getAgentByName,
|
|
789
1294
|
StreamingResponse
|
|
790
1295
|
};
|
|
791
|
-
//# sourceMappingURL=chunk-
|
|
1296
|
+
//# sourceMappingURL=chunk-YDUDMOL6.js.map
|