agents 0.0.101 → 0.0.103
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 +2 -1
- package/dist/ai-chat-agent.js +31 -1
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +1 -1
- package/dist/{chunk-4CIGD73X.js → chunk-JFRK72K3.js} +123 -4
- package/dist/chunk-JFRK72K3.js.map +1 -0
- package/dist/index-CITGJflw.d.ts +486 -0
- package/dist/index.d.ts +23 -404
- package/dist/index.js +1 -1
- package/dist/mcp/index.js +1 -1
- package/dist/observability/index.d.ts +12 -0
- package/dist/observability/index.js +10 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +1 -1
- package/dist/schedule.d.ts +4 -4
- package/package.json +6 -1
- package/src/index.ts +111 -3
- package/dist/chunk-4CIGD73X.js.map +0 -1
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import {
|
|
3
|
+
ServerCapabilities,
|
|
4
|
+
Tool,
|
|
5
|
+
Prompt,
|
|
6
|
+
Resource,
|
|
7
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
8
|
+
import { Server, Connection, PartyServerOptions } from "partyserver";
|
|
9
|
+
import { MCPClientManager } from "./mcp/client.js";
|
|
10
|
+
import { Message } from "ai";
|
|
11
|
+
|
|
12
|
+
type BaseEvent<
|
|
13
|
+
T extends string,
|
|
14
|
+
Payload extends Record<string, unknown> = {},
|
|
15
|
+
> = {
|
|
16
|
+
type: T;
|
|
17
|
+
/**
|
|
18
|
+
* The unique identifier for the event
|
|
19
|
+
*/
|
|
20
|
+
id: string;
|
|
21
|
+
/**
|
|
22
|
+
* The message to display in the logs for this event, should the implementation choose to display
|
|
23
|
+
* a human-readable message.
|
|
24
|
+
*/
|
|
25
|
+
displayMessage: string;
|
|
26
|
+
/**
|
|
27
|
+
* The payload of the event
|
|
28
|
+
*/
|
|
29
|
+
payload: Payload;
|
|
30
|
+
/**
|
|
31
|
+
* The timestamp of the event in milliseconds since epoch
|
|
32
|
+
*/
|
|
33
|
+
timestamp: number;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* The type of events that can be emitted by an Agent
|
|
37
|
+
*/
|
|
38
|
+
type ObservabilityEvent =
|
|
39
|
+
| BaseEvent<
|
|
40
|
+
"state:update",
|
|
41
|
+
{
|
|
42
|
+
state: unknown;
|
|
43
|
+
previousState: unknown;
|
|
44
|
+
}
|
|
45
|
+
>
|
|
46
|
+
| BaseEvent<
|
|
47
|
+
"rpc",
|
|
48
|
+
{
|
|
49
|
+
method: string;
|
|
50
|
+
args: unknown[];
|
|
51
|
+
streaming?: boolean;
|
|
52
|
+
success: boolean;
|
|
53
|
+
}
|
|
54
|
+
>
|
|
55
|
+
| BaseEvent<
|
|
56
|
+
"message:request" | "message:response",
|
|
57
|
+
{
|
|
58
|
+
message: Message[];
|
|
59
|
+
}
|
|
60
|
+
>
|
|
61
|
+
| BaseEvent<"message:clear">
|
|
62
|
+
| BaseEvent<
|
|
63
|
+
"schedule:create" | "schedule:execute" | "schedule:cancel",
|
|
64
|
+
Schedule<unknown>
|
|
65
|
+
>
|
|
66
|
+
| BaseEvent<"destroy">
|
|
67
|
+
| BaseEvent<
|
|
68
|
+
"connect",
|
|
69
|
+
{
|
|
70
|
+
connectionId: string;
|
|
71
|
+
}
|
|
72
|
+
>;
|
|
73
|
+
interface Observability {
|
|
74
|
+
/**
|
|
75
|
+
* Emit an event for the Agent's observability implementation to handle.
|
|
76
|
+
* @param event - The event to emit
|
|
77
|
+
* @param ctx - The execution context of the invocation
|
|
78
|
+
*/
|
|
79
|
+
emit(event: ObservabilityEvent, ctx: DurableObjectState): void;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A generic observability implementation that logs events to the console.
|
|
83
|
+
*/
|
|
84
|
+
declare const genericObservability: Observability;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* RPC request message from client
|
|
88
|
+
*/
|
|
89
|
+
type RPCRequest = {
|
|
90
|
+
type: "rpc";
|
|
91
|
+
id: string;
|
|
92
|
+
method: string;
|
|
93
|
+
args: unknown[];
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* State update message from client
|
|
97
|
+
*/
|
|
98
|
+
type StateUpdateMessage = {
|
|
99
|
+
type: "cf_agent_state";
|
|
100
|
+
state: unknown;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* RPC response message to client
|
|
104
|
+
*/
|
|
105
|
+
type RPCResponse = {
|
|
106
|
+
type: "rpc";
|
|
107
|
+
id: string;
|
|
108
|
+
} & (
|
|
109
|
+
| {
|
|
110
|
+
success: true;
|
|
111
|
+
result: unknown;
|
|
112
|
+
done?: false;
|
|
113
|
+
}
|
|
114
|
+
| {
|
|
115
|
+
success: true;
|
|
116
|
+
result: unknown;
|
|
117
|
+
done: true;
|
|
118
|
+
}
|
|
119
|
+
| {
|
|
120
|
+
success: false;
|
|
121
|
+
error: string;
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
/**
|
|
125
|
+
* Metadata for a callable method
|
|
126
|
+
*/
|
|
127
|
+
type CallableMetadata = {
|
|
128
|
+
/** Optional description of what the method does */
|
|
129
|
+
description?: string;
|
|
130
|
+
/** Whether the method supports streaming responses */
|
|
131
|
+
streaming?: boolean;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Decorator that marks a method as callable by clients
|
|
135
|
+
* @param metadata Optional metadata about the callable method
|
|
136
|
+
*/
|
|
137
|
+
declare function unstable_callable(
|
|
138
|
+
metadata?: CallableMetadata
|
|
139
|
+
): <This, Args extends unknown[], Return>(
|
|
140
|
+
target: (this: This, ...args: Args) => Return,
|
|
141
|
+
context: ClassMethodDecoratorContext
|
|
142
|
+
) => (this: This, ...args: Args) => Return;
|
|
143
|
+
/**
|
|
144
|
+
* Represents a scheduled task within an Agent
|
|
145
|
+
* @template T Type of the payload data
|
|
146
|
+
*/
|
|
147
|
+
type Schedule<T = string> = {
|
|
148
|
+
/** Unique identifier for the schedule */
|
|
149
|
+
id: string;
|
|
150
|
+
/** Name of the method to be called */
|
|
151
|
+
callback: string;
|
|
152
|
+
/** Data to be passed to the callback */
|
|
153
|
+
payload: T;
|
|
154
|
+
} & (
|
|
155
|
+
| {
|
|
156
|
+
/** Type of schedule for one-time execution at a specific time */
|
|
157
|
+
type: "scheduled";
|
|
158
|
+
/** Timestamp when the task should execute */
|
|
159
|
+
time: number;
|
|
160
|
+
}
|
|
161
|
+
| {
|
|
162
|
+
/** Type of schedule for delayed execution */
|
|
163
|
+
type: "delayed";
|
|
164
|
+
/** Timestamp when the task should execute */
|
|
165
|
+
time: number;
|
|
166
|
+
/** Number of seconds to delay execution */
|
|
167
|
+
delayInSeconds: number;
|
|
168
|
+
}
|
|
169
|
+
| {
|
|
170
|
+
/** Type of schedule for recurring execution based on cron expression */
|
|
171
|
+
type: "cron";
|
|
172
|
+
/** Timestamp for the next execution */
|
|
173
|
+
time: number;
|
|
174
|
+
/** Cron expression defining the schedule */
|
|
175
|
+
cron: string;
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
/**
|
|
179
|
+
* MCP Server state update message from server -> Client
|
|
180
|
+
*/
|
|
181
|
+
type MCPServerMessage = {
|
|
182
|
+
type: "cf_agent_mcp_servers";
|
|
183
|
+
mcp: MCPServersState;
|
|
184
|
+
};
|
|
185
|
+
type MCPServersState = {
|
|
186
|
+
servers: {
|
|
187
|
+
[id: string]: MCPServer;
|
|
188
|
+
};
|
|
189
|
+
tools: Tool[];
|
|
190
|
+
prompts: Prompt[];
|
|
191
|
+
resources: Resource[];
|
|
192
|
+
};
|
|
193
|
+
type MCPServer = {
|
|
194
|
+
name: string;
|
|
195
|
+
server_url: string;
|
|
196
|
+
auth_url: string | null;
|
|
197
|
+
state: "authenticating" | "connecting" | "ready" | "discovering" | "failed";
|
|
198
|
+
instructions: string | null;
|
|
199
|
+
capabilities: ServerCapabilities | null;
|
|
200
|
+
};
|
|
201
|
+
declare function getCurrentAgent<
|
|
202
|
+
T extends Agent<unknown, unknown> = Agent<unknown, unknown>,
|
|
203
|
+
>(): {
|
|
204
|
+
agent: T | undefined;
|
|
205
|
+
connection: Connection | undefined;
|
|
206
|
+
request: Request<unknown, CfProperties<unknown>> | undefined;
|
|
207
|
+
};
|
|
208
|
+
/**
|
|
209
|
+
* Base class for creating Agent implementations
|
|
210
|
+
* @template Env Environment type containing bindings
|
|
211
|
+
* @template State State type to store within the Agent
|
|
212
|
+
*/
|
|
213
|
+
declare class Agent<Env, State = unknown> extends Server<Env> {
|
|
214
|
+
private _state;
|
|
215
|
+
private _ParentClass;
|
|
216
|
+
mcp: MCPClientManager;
|
|
217
|
+
/**
|
|
218
|
+
* Initial state for the Agent
|
|
219
|
+
* Override to provide default state values
|
|
220
|
+
*/
|
|
221
|
+
initialState: State;
|
|
222
|
+
/**
|
|
223
|
+
* Current state of the Agent
|
|
224
|
+
*/
|
|
225
|
+
get state(): State;
|
|
226
|
+
/**
|
|
227
|
+
* Agent configuration options
|
|
228
|
+
*/
|
|
229
|
+
static options: {
|
|
230
|
+
/** Whether the Agent should hibernate when inactive */
|
|
231
|
+
hibernate: boolean;
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* The observability implementation to use for the Agent
|
|
235
|
+
*/
|
|
236
|
+
observability?: Observability;
|
|
237
|
+
/**
|
|
238
|
+
* Execute SQL queries against the Agent's database
|
|
239
|
+
* @template T Type of the returned rows
|
|
240
|
+
* @param strings SQL query template strings
|
|
241
|
+
* @param values Values to be inserted into the query
|
|
242
|
+
* @returns Array of query results
|
|
243
|
+
*/
|
|
244
|
+
sql<T = Record<string, string | number | boolean | null>>(
|
|
245
|
+
strings: TemplateStringsArray,
|
|
246
|
+
...values: (string | number | boolean | null)[]
|
|
247
|
+
): T[];
|
|
248
|
+
constructor(ctx: AgentContext, env: Env);
|
|
249
|
+
private _setStateInternal;
|
|
250
|
+
/**
|
|
251
|
+
* Update the Agent's state
|
|
252
|
+
* @param state New state to set
|
|
253
|
+
*/
|
|
254
|
+
setState(state: State): void;
|
|
255
|
+
/**
|
|
256
|
+
* Called when the Agent's state is updated
|
|
257
|
+
* @param state Updated state
|
|
258
|
+
* @param source Source of the state update ("server" or a client connection)
|
|
259
|
+
*/
|
|
260
|
+
onStateUpdate(state: State | undefined, source: Connection | "server"): void;
|
|
261
|
+
/**
|
|
262
|
+
* Called when the Agent receives an email
|
|
263
|
+
* @param email Email message to process
|
|
264
|
+
*/
|
|
265
|
+
onEmail(email: ForwardableEmailMessage): Promise<void>;
|
|
266
|
+
private _tryCatch;
|
|
267
|
+
onError(connection: Connection, error: unknown): void | Promise<void>;
|
|
268
|
+
onError(error: unknown): void | Promise<void>;
|
|
269
|
+
/**
|
|
270
|
+
* Render content (not implemented in base class)
|
|
271
|
+
*/
|
|
272
|
+
render(): void;
|
|
273
|
+
/**
|
|
274
|
+
* Schedule a task to be executed in the future
|
|
275
|
+
* @template T Type of the payload data
|
|
276
|
+
* @param when When to execute the task (Date, seconds delay, or cron expression)
|
|
277
|
+
* @param callback Name of the method to call
|
|
278
|
+
* @param payload Data to pass to the callback
|
|
279
|
+
* @returns Schedule object representing the scheduled task
|
|
280
|
+
*/
|
|
281
|
+
schedule<T = string>(
|
|
282
|
+
when: Date | string | number,
|
|
283
|
+
callback: keyof this,
|
|
284
|
+
payload?: T
|
|
285
|
+
): Promise<Schedule<T>>;
|
|
286
|
+
/**
|
|
287
|
+
* Get a scheduled task by ID
|
|
288
|
+
* @template T Type of the payload data
|
|
289
|
+
* @param id ID of the scheduled task
|
|
290
|
+
* @returns The Schedule object or undefined if not found
|
|
291
|
+
*/
|
|
292
|
+
getSchedule<T = string>(id: string): Promise<Schedule<T> | undefined>;
|
|
293
|
+
/**
|
|
294
|
+
* Get scheduled tasks matching the given criteria
|
|
295
|
+
* @template T Type of the payload data
|
|
296
|
+
* @param criteria Criteria to filter schedules
|
|
297
|
+
* @returns Array of matching Schedule objects
|
|
298
|
+
*/
|
|
299
|
+
getSchedules<T = string>(criteria?: {
|
|
300
|
+
id?: string;
|
|
301
|
+
type?: "scheduled" | "delayed" | "cron";
|
|
302
|
+
timeRange?: {
|
|
303
|
+
start?: Date;
|
|
304
|
+
end?: Date;
|
|
305
|
+
};
|
|
306
|
+
}): Schedule<T>[];
|
|
307
|
+
/**
|
|
308
|
+
* Cancel a scheduled task
|
|
309
|
+
* @param id ID of the task to cancel
|
|
310
|
+
* @returns true if the task was cancelled, false otherwise
|
|
311
|
+
*/
|
|
312
|
+
cancelSchedule(id: string): Promise<boolean>;
|
|
313
|
+
private _scheduleNextAlarm;
|
|
314
|
+
/**
|
|
315
|
+
* Method called when an alarm fires.
|
|
316
|
+
* Executes any scheduled tasks that are due.
|
|
317
|
+
*
|
|
318
|
+
* @remarks
|
|
319
|
+
* To schedule a task, please use the `this.schedule` method instead.
|
|
320
|
+
* See {@link https://developers.cloudflare.com/agents/api-reference/schedule-tasks/}
|
|
321
|
+
*/
|
|
322
|
+
readonly alarm: () => Promise<void>;
|
|
323
|
+
/**
|
|
324
|
+
* Destroy the Agent, removing all state and scheduled tasks
|
|
325
|
+
*/
|
|
326
|
+
destroy(): Promise<void>;
|
|
327
|
+
/**
|
|
328
|
+
* Get all methods marked as callable on this Agent
|
|
329
|
+
* @returns A map of method names to their metadata
|
|
330
|
+
*/
|
|
331
|
+
private _isCallable;
|
|
332
|
+
/**
|
|
333
|
+
* Connect to a new MCP Server
|
|
334
|
+
*
|
|
335
|
+
* @param url MCP Server SSE URL
|
|
336
|
+
* @param callbackHost Base host for the agent, used for the redirect URI.
|
|
337
|
+
* @param agentsPrefix agents routing prefix if not using `agents`
|
|
338
|
+
* @param options MCP client and transport (header) options
|
|
339
|
+
* @returns authUrl
|
|
340
|
+
*/
|
|
341
|
+
addMcpServer(
|
|
342
|
+
serverName: string,
|
|
343
|
+
url: string,
|
|
344
|
+
callbackHost: string,
|
|
345
|
+
agentsPrefix?: string,
|
|
346
|
+
options?: {
|
|
347
|
+
client?: ConstructorParameters<typeof Client>[1];
|
|
348
|
+
transport?: {
|
|
349
|
+
headers: HeadersInit;
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
): Promise<{
|
|
353
|
+
id: string;
|
|
354
|
+
authUrl: string | undefined;
|
|
355
|
+
}>;
|
|
356
|
+
_connectToMcpServerInternal(
|
|
357
|
+
_serverName: string,
|
|
358
|
+
url: string,
|
|
359
|
+
callbackUrl: string,
|
|
360
|
+
options?: {
|
|
361
|
+
client?: ConstructorParameters<typeof Client>[1];
|
|
362
|
+
/**
|
|
363
|
+
* We don't expose the normal set of transport options because:
|
|
364
|
+
* 1) we can't serialize things like the auth provider or a fetch function into the DB for reconnection purposes
|
|
365
|
+
* 2) We probably want these options to be agnostic to the transport type (SSE vs Streamable)
|
|
366
|
+
*
|
|
367
|
+
* This has the limitation that you can't override fetch, but I think headers should handle nearly all cases needed (i.e. non-standard bearer auth).
|
|
368
|
+
*/
|
|
369
|
+
transport?: {
|
|
370
|
+
headers?: HeadersInit;
|
|
371
|
+
};
|
|
372
|
+
},
|
|
373
|
+
reconnect?: {
|
|
374
|
+
id: string;
|
|
375
|
+
oauthClientId?: string;
|
|
376
|
+
}
|
|
377
|
+
): Promise<{
|
|
378
|
+
id: string;
|
|
379
|
+
authUrl: string | undefined;
|
|
380
|
+
clientId: string | undefined;
|
|
381
|
+
}>;
|
|
382
|
+
removeMcpServer(id: string): Promise<void>;
|
|
383
|
+
getMcpServers(): MCPServersState;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Namespace for creating Agent instances
|
|
387
|
+
* @template Agentic Type of the Agent class
|
|
388
|
+
*/
|
|
389
|
+
type AgentNamespace<Agentic extends Agent<unknown>> =
|
|
390
|
+
DurableObjectNamespace<Agentic>;
|
|
391
|
+
/**
|
|
392
|
+
* Agent's durable context
|
|
393
|
+
*/
|
|
394
|
+
type AgentContext = DurableObjectState;
|
|
395
|
+
/**
|
|
396
|
+
* Configuration options for Agent routing
|
|
397
|
+
*/
|
|
398
|
+
type AgentOptions<Env> = PartyServerOptions<Env> & {
|
|
399
|
+
/**
|
|
400
|
+
* Whether to enable CORS for the Agent
|
|
401
|
+
*/
|
|
402
|
+
cors?: boolean | HeadersInit | undefined;
|
|
403
|
+
};
|
|
404
|
+
/**
|
|
405
|
+
* Route a request to the appropriate Agent
|
|
406
|
+
* @param request Request to route
|
|
407
|
+
* @param env Environment containing Agent bindings
|
|
408
|
+
* @param options Routing options
|
|
409
|
+
* @returns Response from the Agent or undefined if no route matched
|
|
410
|
+
*/
|
|
411
|
+
declare function routeAgentRequest<Env>(
|
|
412
|
+
request: Request,
|
|
413
|
+
env: Env,
|
|
414
|
+
options?: AgentOptions<Env>
|
|
415
|
+
): Promise<Response | null>;
|
|
416
|
+
/**
|
|
417
|
+
* Route an email to the appropriate Agent
|
|
418
|
+
* @param email Email message to route
|
|
419
|
+
* @param env Environment containing Agent bindings
|
|
420
|
+
* @param options Routing options
|
|
421
|
+
*/
|
|
422
|
+
declare function routeAgentEmail<Env>(
|
|
423
|
+
_email: ForwardableEmailMessage,
|
|
424
|
+
_env: Env,
|
|
425
|
+
_options?: AgentOptions<Env>
|
|
426
|
+
): Promise<void>;
|
|
427
|
+
/**
|
|
428
|
+
* Get or create an Agent by name
|
|
429
|
+
* @template Env Environment type containing bindings
|
|
430
|
+
* @template T Type of the Agent class
|
|
431
|
+
* @param namespace Agent namespace
|
|
432
|
+
* @param name Name of the Agent instance
|
|
433
|
+
* @param options Options for Agent creation
|
|
434
|
+
* @returns Promise resolving to an Agent instance stub
|
|
435
|
+
*/
|
|
436
|
+
declare function getAgentByName<Env, T extends Agent<Env>>(
|
|
437
|
+
namespace: AgentNamespace<T>,
|
|
438
|
+
name: string,
|
|
439
|
+
options?: {
|
|
440
|
+
jurisdiction?: DurableObjectJurisdiction;
|
|
441
|
+
locationHint?: DurableObjectLocationHint;
|
|
442
|
+
}
|
|
443
|
+
): Promise<DurableObjectStub<T>>;
|
|
444
|
+
/**
|
|
445
|
+
* A wrapper for streaming responses in callable methods
|
|
446
|
+
*/
|
|
447
|
+
declare class StreamingResponse {
|
|
448
|
+
private _connection;
|
|
449
|
+
private _id;
|
|
450
|
+
private _closed;
|
|
451
|
+
constructor(connection: Connection, id: string);
|
|
452
|
+
/**
|
|
453
|
+
* Send a chunk of data to the client
|
|
454
|
+
* @param chunk The data to send
|
|
455
|
+
*/
|
|
456
|
+
send(chunk: unknown): void;
|
|
457
|
+
/**
|
|
458
|
+
* End the stream and send the final chunk (if any)
|
|
459
|
+
* @param finalChunk Optional final chunk of data to send
|
|
460
|
+
*/
|
|
461
|
+
end(finalChunk?: unknown): void;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export {
|
|
465
|
+
Agent as A,
|
|
466
|
+
type CallableMetadata as C,
|
|
467
|
+
type MCPServersState as M,
|
|
468
|
+
type ObservabilityEvent as O,
|
|
469
|
+
type RPCRequest as R,
|
|
470
|
+
type StateUpdateMessage as S,
|
|
471
|
+
type AgentContext as a,
|
|
472
|
+
type Observability as b,
|
|
473
|
+
type RPCResponse as c,
|
|
474
|
+
type Schedule as d,
|
|
475
|
+
type MCPServerMessage as e,
|
|
476
|
+
type MCPServer as f,
|
|
477
|
+
genericObservability as g,
|
|
478
|
+
getCurrentAgent as h,
|
|
479
|
+
type AgentNamespace as i,
|
|
480
|
+
type AgentOptions as j,
|
|
481
|
+
routeAgentEmail as k,
|
|
482
|
+
getAgentByName as l,
|
|
483
|
+
StreamingResponse as m,
|
|
484
|
+
routeAgentRequest as r,
|
|
485
|
+
unstable_callable as u,
|
|
486
|
+
};
|