agents 0.0.2 → 0.0.38
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 +418 -18
- package/dist/ai-chat-agent.d.ts +35 -0
- package/dist/ai-chat-agent.js +170 -0
- package/dist/ai-chat-agent.js.map +1 -0
- package/dist/ai-react.d.ts +86 -0
- package/dist/ai-react.js +189 -0
- package/dist/ai-react.js.map +1 -0
- package/dist/ai-types.d.ts +69 -0
- package/dist/ai-types.js +1 -0
- package/dist/ai-types.js.map +1 -0
- package/dist/chunk-HMLY7DHA.js +16 -0
- package/dist/chunk-HMLY7DHA.js.map +1 -0
- package/dist/chunk-X6BBKLSC.js +568 -0
- package/dist/chunk-X6BBKLSC.js.map +1 -0
- package/dist/client.d.ts +84 -0
- package/dist/client.js +138 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +302 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/react.d.ts +39 -0
- package/dist/react.js +97 -0
- package/dist/react.js.map +1 -0
- package/dist/schedule.d.ts +53 -0
- package/dist/schedule.js +73 -0
- package/dist/schedule.js.map +1 -0
- package/package.json +57 -18
- package/src/index.ts +888 -0
- package/.npmignore +0 -1
- package/allagents.xml +0 -22170
- package/node-user-agents.js +0 -37
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PartySocketOptions,
|
|
3
|
+
PartyFetchOptions,
|
|
4
|
+
PartySocket,
|
|
5
|
+
} from "partysocket";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for creating an AgentClient
|
|
9
|
+
*/
|
|
10
|
+
type AgentClientOptions<State = unknown> = Omit<
|
|
11
|
+
PartySocketOptions,
|
|
12
|
+
"party" | "room"
|
|
13
|
+
> & {
|
|
14
|
+
/** Name of the agent to connect to */
|
|
15
|
+
agent: string;
|
|
16
|
+
/** Name of the specific Agent instance */
|
|
17
|
+
name?: string;
|
|
18
|
+
/** Called when the Agent's state is updated */
|
|
19
|
+
onStateUpdate?: (state: State, source: "server" | "client") => void;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Options for streaming RPC calls
|
|
23
|
+
*/
|
|
24
|
+
type StreamOptions = {
|
|
25
|
+
/** Called when a chunk of data is received */
|
|
26
|
+
onChunk?: (chunk: unknown) => void;
|
|
27
|
+
/** Called when the stream ends */
|
|
28
|
+
onDone?: (finalChunk: unknown) => void;
|
|
29
|
+
/** Called when an error occurs */
|
|
30
|
+
onError?: (error: string) => void;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Options for the agentFetch function
|
|
34
|
+
*/
|
|
35
|
+
type AgentClientFetchOptions = Omit<PartyFetchOptions, "party" | "room"> & {
|
|
36
|
+
/** Name of the agent to connect to */
|
|
37
|
+
agent: string;
|
|
38
|
+
/** Name of the specific Agent instance */
|
|
39
|
+
name?: string;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* WebSocket client for connecting to an Agent
|
|
43
|
+
*/
|
|
44
|
+
declare class AgentClient<State = unknown> extends PartySocket {
|
|
45
|
+
#private;
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated Use agentFetch instead
|
|
48
|
+
*/
|
|
49
|
+
static fetch(_opts: PartyFetchOptions): Promise<Response>;
|
|
50
|
+
agent: string;
|
|
51
|
+
name: string;
|
|
52
|
+
constructor(options: AgentClientOptions<State>);
|
|
53
|
+
setState(state: State): void;
|
|
54
|
+
/**
|
|
55
|
+
* Call a method on the Agent
|
|
56
|
+
* @param method Name of the method to call
|
|
57
|
+
* @param args Arguments to pass to the method
|
|
58
|
+
* @param streamOptions Options for handling streaming responses
|
|
59
|
+
* @returns Promise that resolves with the method's return value
|
|
60
|
+
*/
|
|
61
|
+
call<T = unknown>(
|
|
62
|
+
method: string,
|
|
63
|
+
args?: unknown[],
|
|
64
|
+
streamOptions?: StreamOptions
|
|
65
|
+
): Promise<T>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Make an HTTP request to an Agent
|
|
69
|
+
* @param opts Connection options
|
|
70
|
+
* @param init Request initialization options
|
|
71
|
+
* @returns Promise resolving to a Response
|
|
72
|
+
*/
|
|
73
|
+
declare function agentFetch(
|
|
74
|
+
opts: AgentClientFetchOptions,
|
|
75
|
+
init?: RequestInit
|
|
76
|
+
): Promise<Response>;
|
|
77
|
+
|
|
78
|
+
export {
|
|
79
|
+
AgentClient,
|
|
80
|
+
type AgentClientFetchOptions,
|
|
81
|
+
type AgentClientOptions,
|
|
82
|
+
type StreamOptions,
|
|
83
|
+
agentFetch,
|
|
84
|
+
};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__privateAdd,
|
|
3
|
+
__privateGet,
|
|
4
|
+
__privateSet
|
|
5
|
+
} from "./chunk-HMLY7DHA.js";
|
|
6
|
+
|
|
7
|
+
// src/client.ts
|
|
8
|
+
import {
|
|
9
|
+
PartySocket
|
|
10
|
+
} from "partysocket";
|
|
11
|
+
var _options, _pendingCalls;
|
|
12
|
+
var AgentClient = class extends PartySocket {
|
|
13
|
+
constructor(options) {
|
|
14
|
+
super({
|
|
15
|
+
prefix: "agents",
|
|
16
|
+
party: options.agent,
|
|
17
|
+
room: options.name || "default",
|
|
18
|
+
...options
|
|
19
|
+
});
|
|
20
|
+
__privateAdd(this, _options);
|
|
21
|
+
__privateAdd(this, _pendingCalls, /* @__PURE__ */ new Map());
|
|
22
|
+
this.agent = options.agent;
|
|
23
|
+
this.name = options.name || "default";
|
|
24
|
+
__privateSet(this, _options, options);
|
|
25
|
+
if (this.agent !== this.agent.toLowerCase()) {
|
|
26
|
+
console.warn(
|
|
27
|
+
`Agent name: ${this.agent} should probably be in lowercase. Received: ${this.agent}`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
if (this.name !== this.name.toLowerCase()) {
|
|
31
|
+
console.warn(
|
|
32
|
+
`Agent instance name: ${this.name} should probably be in lowercase. Received: ${this.name}`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
this.addEventListener("message", (event) => {
|
|
36
|
+
if (typeof event.data === "string") {
|
|
37
|
+
let parsedMessage;
|
|
38
|
+
try {
|
|
39
|
+
parsedMessage = JSON.parse(event.data);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (parsedMessage.type === "cf_agent_state") {
|
|
44
|
+
__privateGet(this, _options).onStateUpdate?.(parsedMessage.state, "server");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (parsedMessage.type === "rpc") {
|
|
48
|
+
const response = parsedMessage;
|
|
49
|
+
const pending = __privateGet(this, _pendingCalls).get(response.id);
|
|
50
|
+
if (!pending) return;
|
|
51
|
+
if (!response.success) {
|
|
52
|
+
pending.reject(new Error(response.error));
|
|
53
|
+
__privateGet(this, _pendingCalls).delete(response.id);
|
|
54
|
+
pending.stream?.onError?.(response.error);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if ("done" in response) {
|
|
58
|
+
if (response.done) {
|
|
59
|
+
pending.resolve(response.result);
|
|
60
|
+
__privateGet(this, _pendingCalls).delete(response.id);
|
|
61
|
+
pending.stream?.onDone?.(response.result);
|
|
62
|
+
} else {
|
|
63
|
+
pending.stream?.onChunk?.(response.result);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
pending.resolve(response.result);
|
|
67
|
+
__privateGet(this, _pendingCalls).delete(response.id);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @deprecated Use agentFetch instead
|
|
75
|
+
*/
|
|
76
|
+
static fetch(_opts) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
"AgentClient.fetch is not implemented, use agentFetch instead"
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
setState(state) {
|
|
82
|
+
this.send(JSON.stringify({ type: "cf_agent_state", state }));
|
|
83
|
+
__privateGet(this, _options).onStateUpdate?.(state, "client");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Call a method on the Agent
|
|
87
|
+
* @param method Name of the method to call
|
|
88
|
+
* @param args Arguments to pass to the method
|
|
89
|
+
* @param streamOptions Options for handling streaming responses
|
|
90
|
+
* @returns Promise that resolves with the method's return value
|
|
91
|
+
*/
|
|
92
|
+
async call(method, args = [], streamOptions) {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const id = Math.random().toString(36).slice(2);
|
|
95
|
+
__privateGet(this, _pendingCalls).set(id, {
|
|
96
|
+
resolve: (value) => resolve(value),
|
|
97
|
+
reject,
|
|
98
|
+
stream: streamOptions,
|
|
99
|
+
type: null
|
|
100
|
+
});
|
|
101
|
+
const request = {
|
|
102
|
+
type: "rpc",
|
|
103
|
+
id,
|
|
104
|
+
method,
|
|
105
|
+
args
|
|
106
|
+
};
|
|
107
|
+
this.send(JSON.stringify(request));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
_options = new WeakMap();
|
|
112
|
+
_pendingCalls = new WeakMap();
|
|
113
|
+
function agentFetch(opts, init) {
|
|
114
|
+
if (opts.agent !== opts.agent.toLowerCase()) {
|
|
115
|
+
console.warn(
|
|
116
|
+
`Agent name: ${opts.agent} should probably be in lowercase. Received: ${opts.agent}`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (opts.name && opts.name !== opts.name.toLowerCase()) {
|
|
120
|
+
console.warn(
|
|
121
|
+
`Agent instance name: ${opts.name} should probably be in lowercase. Received: ${opts.name}`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return PartySocket.fetch(
|
|
125
|
+
{
|
|
126
|
+
prefix: "agents",
|
|
127
|
+
party: opts.agent,
|
|
128
|
+
room: opts.name || "default",
|
|
129
|
+
...opts
|
|
130
|
+
},
|
|
131
|
+
init
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
export {
|
|
135
|
+
AgentClient,
|
|
136
|
+
agentFetch
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["import {\n PartySocket,\n type PartySocketOptions,\n type PartyFetchOptions,\n} from \"partysocket\";\nimport type { RPCRequest, RPCResponse } from \"./\";\n\n/**\n * Options for creating an AgentClient\n */\nexport type AgentClientOptions<State = unknown> = Omit<\n PartySocketOptions,\n \"party\" | \"room\"\n> & {\n /** Name of the agent to connect to */\n agent: string;\n /** Name of the specific Agent instance */\n name?: string;\n /** Called when the Agent's state is updated */\n onStateUpdate?: (state: State, source: \"server\" | \"client\") => void;\n};\n\n/**\n * Options for streaming RPC calls\n */\nexport type StreamOptions = {\n /** Called when a chunk of data is received */\n onChunk?: (chunk: unknown) => void;\n /** Called when the stream ends */\n onDone?: (finalChunk: unknown) => void;\n /** Called when an error occurs */\n onError?: (error: string) => void;\n};\n\n/**\n * Options for the agentFetch function\n */\nexport type AgentClientFetchOptions = Omit<\n PartyFetchOptions,\n \"party\" | \"room\"\n> & {\n /** Name of the agent to connect to */\n agent: string;\n /** Name of the specific Agent instance */\n name?: string;\n};\n\n/**\n * WebSocket client for connecting to an Agent\n */\nexport class AgentClient<State = unknown> extends PartySocket {\n /**\n * @deprecated Use agentFetch instead\n */\n static fetch(_opts: PartyFetchOptions): Promise<Response> {\n throw new Error(\n \"AgentClient.fetch is not implemented, use agentFetch instead\"\n );\n }\n agent: string;\n name: string;\n #options: AgentClientOptions<State>;\n #pendingCalls = new Map<\n string,\n {\n resolve: (value: unknown) => void;\n reject: (error: Error) => void;\n stream?: StreamOptions;\n type?: unknown;\n }\n >();\n\n constructor(options: AgentClientOptions<State>) {\n super({\n prefix: \"agents\",\n party: options.agent,\n room: options.name || \"default\",\n ...options,\n });\n this.agent = options.agent;\n this.name = options.name || \"default\";\n this.#options = options;\n\n // warn if agent or name isn't in lowercase\n if (this.agent !== this.agent.toLowerCase()) {\n console.warn(\n `Agent name: ${this.agent} should probably be in lowercase. Received: ${this.agent}`\n );\n }\n if (this.name !== this.name.toLowerCase()) {\n console.warn(\n `Agent instance name: ${this.name} should probably be in lowercase. Received: ${this.name}`\n );\n }\n\n this.addEventListener(\"message\", (event) => {\n if (typeof event.data === \"string\") {\n let parsedMessage: Record<string, unknown>;\n try {\n parsedMessage = JSON.parse(event.data);\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (parsedMessage.type === \"cf_agent_state\") {\n this.#options.onStateUpdate?.(parsedMessage.state as State, \"server\");\n return;\n }\n if (parsedMessage.type === \"rpc\") {\n const response = parsedMessage as RPCResponse;\n const pending = this.#pendingCalls.get(response.id);\n if (!pending) return;\n\n if (!response.success) {\n pending.reject(new Error(response.error));\n this.#pendingCalls.delete(response.id);\n pending.stream?.onError?.(response.error);\n return;\n }\n\n // Handle streaming responses\n if (\"done\" in response) {\n if (response.done) {\n pending.resolve(response.result);\n this.#pendingCalls.delete(response.id);\n pending.stream?.onDone?.(response.result);\n } else {\n pending.stream?.onChunk?.(response.result);\n }\n } else {\n // Non-streaming response\n pending.resolve(response.result);\n this.#pendingCalls.delete(response.id);\n }\n }\n }\n });\n }\n\n setState(state: State) {\n this.send(JSON.stringify({ type: \"cf_agent_state\", state }));\n this.#options.onStateUpdate?.(state, \"client\");\n }\n\n /**\n * Call a method on the Agent\n * @param method Name of the method to call\n * @param args Arguments to pass to the method\n * @param streamOptions Options for handling streaming responses\n * @returns Promise that resolves with the method's return value\n */\n async call<T = unknown>(\n method: string,\n args: unknown[] = [],\n streamOptions?: StreamOptions\n ): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n const id = Math.random().toString(36).slice(2);\n this.#pendingCalls.set(id, {\n resolve: (value: unknown) => resolve(value as T),\n reject,\n stream: streamOptions,\n type: null as T,\n });\n\n const request: RPCRequest = {\n type: \"rpc\",\n id,\n method,\n args,\n };\n\n this.send(JSON.stringify(request));\n });\n }\n}\n\n/**\n * Make an HTTP request to an Agent\n * @param opts Connection options\n * @param init Request initialization options\n * @returns Promise resolving to a Response\n */\nexport function agentFetch(opts: AgentClientFetchOptions, init?: RequestInit) {\n // warn if agent or name isn't in lowercase\n if (opts.agent !== opts.agent.toLowerCase()) {\n console.warn(\n `Agent name: ${opts.agent} should probably be in lowercase. Received: ${opts.agent}`\n );\n }\n if (opts.name && opts.name !== opts.name.toLowerCase()) {\n console.warn(\n `Agent instance name: ${opts.name} should probably be in lowercase. Received: ${opts.name}`\n );\n }\n\n return PartySocket.fetch(\n {\n prefix: \"agents\",\n party: opts.agent,\n room: opts.name || \"default\",\n ...opts,\n },\n init\n );\n}\n"],"mappings":"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","names":[]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { Server, Connection, PartyServerOptions } from "partyserver";
|
|
2
|
+
export { Connection, ConnectionContext, WSMessage } from "partyserver";
|
|
3
|
+
import { WorkflowEntrypoint as WorkflowEntrypoint$1 } from "cloudflare:workers";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* RPC request message from client
|
|
7
|
+
*/
|
|
8
|
+
type RPCRequest = {
|
|
9
|
+
type: "rpc";
|
|
10
|
+
id: string;
|
|
11
|
+
method: string;
|
|
12
|
+
args: unknown[];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* State update message from client
|
|
16
|
+
*/
|
|
17
|
+
type StateUpdateMessage = {
|
|
18
|
+
type: "cf_agent_state";
|
|
19
|
+
state: unknown;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* RPC response message to client
|
|
23
|
+
*/
|
|
24
|
+
type RPCResponse = {
|
|
25
|
+
type: "rpc";
|
|
26
|
+
id: string;
|
|
27
|
+
} & (
|
|
28
|
+
| {
|
|
29
|
+
success: true;
|
|
30
|
+
result: unknown;
|
|
31
|
+
done?: false;
|
|
32
|
+
}
|
|
33
|
+
| {
|
|
34
|
+
success: true;
|
|
35
|
+
result: unknown;
|
|
36
|
+
done: true;
|
|
37
|
+
}
|
|
38
|
+
| {
|
|
39
|
+
success: false;
|
|
40
|
+
error: string;
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
/**
|
|
44
|
+
* Metadata for a callable method
|
|
45
|
+
*/
|
|
46
|
+
type CallableMetadata = {
|
|
47
|
+
/** Optional description of what the method does */
|
|
48
|
+
description?: string;
|
|
49
|
+
/** Whether the method supports streaming responses */
|
|
50
|
+
streaming?: boolean;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Decorator that marks a method as callable by clients
|
|
54
|
+
* @param metadata Optional metadata about the callable method
|
|
55
|
+
*/
|
|
56
|
+
declare function unstable_callable(
|
|
57
|
+
metadata?: CallableMetadata
|
|
58
|
+
): <This, Args extends unknown[], Return>(
|
|
59
|
+
target: (this: This, ...args: Args) => Return,
|
|
60
|
+
context: ClassMethodDecoratorContext
|
|
61
|
+
) => (this: This, ...args: Args) => Return;
|
|
62
|
+
/**
|
|
63
|
+
* A class for creating workflow entry points that can be used with Cloudflare Workers
|
|
64
|
+
*/
|
|
65
|
+
declare class WorkflowEntrypoint extends WorkflowEntrypoint$1 {}
|
|
66
|
+
/**
|
|
67
|
+
* Represents a scheduled task within an Agent
|
|
68
|
+
* @template T Type of the payload data
|
|
69
|
+
*/
|
|
70
|
+
type Schedule<T = string> = {
|
|
71
|
+
/** Unique identifier for the schedule */
|
|
72
|
+
id: string;
|
|
73
|
+
/** Name of the method to be called */
|
|
74
|
+
callback: string;
|
|
75
|
+
/** Data to be passed to the callback */
|
|
76
|
+
payload: T;
|
|
77
|
+
} & (
|
|
78
|
+
| {
|
|
79
|
+
/** Type of schedule for one-time execution at a specific time */
|
|
80
|
+
type: "scheduled";
|
|
81
|
+
/** Timestamp when the task should execute */
|
|
82
|
+
time: number;
|
|
83
|
+
}
|
|
84
|
+
| {
|
|
85
|
+
/** Type of schedule for delayed execution */
|
|
86
|
+
type: "delayed";
|
|
87
|
+
/** Timestamp when the task should execute */
|
|
88
|
+
time: number;
|
|
89
|
+
/** Number of seconds to delay execution */
|
|
90
|
+
delayInSeconds: number;
|
|
91
|
+
}
|
|
92
|
+
| {
|
|
93
|
+
/** Type of schedule for recurring execution based on cron expression */
|
|
94
|
+
type: "cron";
|
|
95
|
+
/** Timestamp for the next execution */
|
|
96
|
+
time: number;
|
|
97
|
+
/** Cron expression defining the schedule */
|
|
98
|
+
cron: string;
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
/**
|
|
102
|
+
* Base class for creating Agent implementations
|
|
103
|
+
* @template Env Environment type containing bindings
|
|
104
|
+
* @template State State type to store within the Agent
|
|
105
|
+
*/
|
|
106
|
+
declare class Agent<Env, State = unknown> extends Server<Env> {
|
|
107
|
+
#private;
|
|
108
|
+
/**
|
|
109
|
+
* Initial state for the Agent
|
|
110
|
+
* Override to provide default state values
|
|
111
|
+
*/
|
|
112
|
+
initialState: State;
|
|
113
|
+
/**
|
|
114
|
+
* Current state of the Agent
|
|
115
|
+
*/
|
|
116
|
+
get state(): State;
|
|
117
|
+
/**
|
|
118
|
+
* Agent configuration options
|
|
119
|
+
*/
|
|
120
|
+
static options: {
|
|
121
|
+
/** Whether the Agent should hibernate when inactive */
|
|
122
|
+
hibernate: boolean;
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Execute SQL queries against the Agent's database
|
|
126
|
+
* @template T Type of the returned rows
|
|
127
|
+
* @param strings SQL query template strings
|
|
128
|
+
* @param values Values to be inserted into the query
|
|
129
|
+
* @returns Array of query results
|
|
130
|
+
*/
|
|
131
|
+
sql<T = Record<string, string | number | boolean | null>>(
|
|
132
|
+
strings: TemplateStringsArray,
|
|
133
|
+
...values: (string | number | boolean | null)[]
|
|
134
|
+
): T[];
|
|
135
|
+
constructor(ctx: AgentContext, env: Env);
|
|
136
|
+
/**
|
|
137
|
+
* Update the Agent's state
|
|
138
|
+
* @param state New state to set
|
|
139
|
+
*/
|
|
140
|
+
setState(state: State): void;
|
|
141
|
+
/**
|
|
142
|
+
* Called when the Agent's state is updated
|
|
143
|
+
* @param state Updated state
|
|
144
|
+
* @param source Source of the state update ("server" or a client connection)
|
|
145
|
+
*/
|
|
146
|
+
onStateUpdate(state: State | undefined, source: Connection | "server"): void;
|
|
147
|
+
/**
|
|
148
|
+
* Called when the Agent receives an email
|
|
149
|
+
* @param email Email message to process
|
|
150
|
+
*/
|
|
151
|
+
onEmail(email: ForwardableEmailMessage): void;
|
|
152
|
+
onError(connection: Connection, error: unknown): void | Promise<void>;
|
|
153
|
+
onError(error: unknown): void | Promise<void>;
|
|
154
|
+
/**
|
|
155
|
+
* Render content (not implemented in base class)
|
|
156
|
+
*/
|
|
157
|
+
render(): void;
|
|
158
|
+
/**
|
|
159
|
+
* Schedule a task to be executed in the future
|
|
160
|
+
* @template T Type of the payload data
|
|
161
|
+
* @param when When to execute the task (Date, seconds delay, or cron expression)
|
|
162
|
+
* @param callback Name of the method to call
|
|
163
|
+
* @param payload Data to pass to the callback
|
|
164
|
+
* @returns Schedule object representing the scheduled task
|
|
165
|
+
*/
|
|
166
|
+
schedule<T = string>(
|
|
167
|
+
when: Date | string | number,
|
|
168
|
+
callback: keyof this,
|
|
169
|
+
payload?: T
|
|
170
|
+
): Promise<Schedule<T>>;
|
|
171
|
+
/**
|
|
172
|
+
* Get a scheduled task by ID
|
|
173
|
+
* @template T Type of the payload data
|
|
174
|
+
* @param id ID of the scheduled task
|
|
175
|
+
* @returns The Schedule object or undefined if not found
|
|
176
|
+
*/
|
|
177
|
+
getSchedule<T = string>(id: string): Promise<Schedule<T> | undefined>;
|
|
178
|
+
/**
|
|
179
|
+
* Get scheduled tasks matching the given criteria
|
|
180
|
+
* @template T Type of the payload data
|
|
181
|
+
* @param criteria Criteria to filter schedules
|
|
182
|
+
* @returns Array of matching Schedule objects
|
|
183
|
+
*/
|
|
184
|
+
getSchedules<T = string>(criteria?: {
|
|
185
|
+
description?: string;
|
|
186
|
+
id?: string;
|
|
187
|
+
type?: "scheduled" | "delayed" | "cron";
|
|
188
|
+
timeRange?: {
|
|
189
|
+
start?: Date;
|
|
190
|
+
end?: Date;
|
|
191
|
+
};
|
|
192
|
+
}): Schedule<T>[];
|
|
193
|
+
/**
|
|
194
|
+
* Cancel a scheduled task
|
|
195
|
+
* @param id ID of the task to cancel
|
|
196
|
+
* @returns true if the task was cancelled, false otherwise
|
|
197
|
+
*/
|
|
198
|
+
cancelSchedule(id: string): Promise<boolean>;
|
|
199
|
+
/**
|
|
200
|
+
* Method called when an alarm fires
|
|
201
|
+
* Executes any scheduled tasks that are due
|
|
202
|
+
*/
|
|
203
|
+
alarm(): Promise<void>;
|
|
204
|
+
/**
|
|
205
|
+
* Destroy the Agent, removing all state and scheduled tasks
|
|
206
|
+
*/
|
|
207
|
+
destroy(): Promise<void>;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Namespace for creating Agent instances
|
|
211
|
+
* @template Agentic Type of the Agent class
|
|
212
|
+
*/
|
|
213
|
+
type AgentNamespace<Agentic extends Agent<unknown>> =
|
|
214
|
+
DurableObjectNamespace<Agentic>;
|
|
215
|
+
/**
|
|
216
|
+
* Agent's durable context
|
|
217
|
+
*/
|
|
218
|
+
type AgentContext = DurableObjectState;
|
|
219
|
+
/**
|
|
220
|
+
* Configuration options for Agent routing
|
|
221
|
+
*/
|
|
222
|
+
type AgentOptions<Env> = PartyServerOptions<Env> & {
|
|
223
|
+
/**
|
|
224
|
+
* Whether to enable CORS for the Agent
|
|
225
|
+
*/
|
|
226
|
+
cors?: boolean | HeadersInit | undefined;
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Route a request to the appropriate Agent
|
|
230
|
+
* @param request Request to route
|
|
231
|
+
* @param env Environment containing Agent bindings
|
|
232
|
+
* @param options Routing options
|
|
233
|
+
* @returns Response from the Agent or undefined if no route matched
|
|
234
|
+
*/
|
|
235
|
+
declare function routeAgentRequest<Env>(
|
|
236
|
+
request: Request,
|
|
237
|
+
env: Env,
|
|
238
|
+
options?: AgentOptions<Env>
|
|
239
|
+
): Promise<Response | null>;
|
|
240
|
+
/**
|
|
241
|
+
* Route an email to the appropriate Agent
|
|
242
|
+
* @param email Email message to route
|
|
243
|
+
* @param env Environment containing Agent bindings
|
|
244
|
+
* @param options Routing options
|
|
245
|
+
*/
|
|
246
|
+
declare function routeAgentEmail<Env>(
|
|
247
|
+
email: ForwardableEmailMessage,
|
|
248
|
+
env: Env,
|
|
249
|
+
options?: AgentOptions<Env>
|
|
250
|
+
): Promise<void>;
|
|
251
|
+
/**
|
|
252
|
+
* Get or create an Agent by name
|
|
253
|
+
* @template Env Environment type containing bindings
|
|
254
|
+
* @template T Type of the Agent class
|
|
255
|
+
* @param namespace Agent namespace
|
|
256
|
+
* @param name Name of the Agent instance
|
|
257
|
+
* @param options Options for Agent creation
|
|
258
|
+
* @returns Promise resolving to an Agent instance stub
|
|
259
|
+
*/
|
|
260
|
+
declare function getAgentByName<Env, T extends Agent<Env>>(
|
|
261
|
+
namespace: AgentNamespace<T>,
|
|
262
|
+
name: string,
|
|
263
|
+
options?: {
|
|
264
|
+
jurisdiction?: DurableObjectJurisdiction;
|
|
265
|
+
locationHint?: DurableObjectLocationHint;
|
|
266
|
+
}
|
|
267
|
+
): Promise<DurableObjectStub<T>>;
|
|
268
|
+
/**
|
|
269
|
+
* A wrapper for streaming responses in callable methods
|
|
270
|
+
*/
|
|
271
|
+
declare class StreamingResponse {
|
|
272
|
+
#private;
|
|
273
|
+
constructor(connection: Connection, id: string);
|
|
274
|
+
/**
|
|
275
|
+
* Send a chunk of data to the client
|
|
276
|
+
* @param chunk The data to send
|
|
277
|
+
*/
|
|
278
|
+
send(chunk: unknown): void;
|
|
279
|
+
/**
|
|
280
|
+
* End the stream and send the final chunk (if any)
|
|
281
|
+
* @param finalChunk Optional final chunk of data to send
|
|
282
|
+
*/
|
|
283
|
+
end(finalChunk?: unknown): void;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export {
|
|
287
|
+
Agent,
|
|
288
|
+
type AgentContext,
|
|
289
|
+
type AgentNamespace,
|
|
290
|
+
type AgentOptions,
|
|
291
|
+
type CallableMetadata,
|
|
292
|
+
type RPCRequest,
|
|
293
|
+
type RPCResponse,
|
|
294
|
+
type Schedule,
|
|
295
|
+
type StateUpdateMessage,
|
|
296
|
+
StreamingResponse,
|
|
297
|
+
WorkflowEntrypoint,
|
|
298
|
+
getAgentByName,
|
|
299
|
+
routeAgentEmail,
|
|
300
|
+
routeAgentRequest,
|
|
301
|
+
unstable_callable,
|
|
302
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Agent,
|
|
3
|
+
StreamingResponse,
|
|
4
|
+
WorkflowEntrypoint,
|
|
5
|
+
getAgentByName,
|
|
6
|
+
routeAgentEmail,
|
|
7
|
+
routeAgentRequest,
|
|
8
|
+
unstable_callable
|
|
9
|
+
} from "./chunk-X6BBKLSC.js";
|
|
10
|
+
import "./chunk-HMLY7DHA.js";
|
|
11
|
+
export {
|
|
12
|
+
Agent,
|
|
13
|
+
StreamingResponse,
|
|
14
|
+
WorkflowEntrypoint,
|
|
15
|
+
getAgentByName,
|
|
16
|
+
routeAgentEmail,
|
|
17
|
+
routeAgentRequest,
|
|
18
|
+
unstable_callable
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { PartySocket } from "partysocket";
|
|
2
|
+
import { usePartySocket } from "partysocket/react";
|
|
3
|
+
import { StreamOptions } from "./client.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for the useAgent hook
|
|
7
|
+
* @template State Type of the Agent's state
|
|
8
|
+
*/
|
|
9
|
+
type UseAgentOptions<State = unknown> = Omit<
|
|
10
|
+
Parameters<typeof usePartySocket>[0],
|
|
11
|
+
"party" | "room"
|
|
12
|
+
> & {
|
|
13
|
+
/** Name of the agent to connect to */
|
|
14
|
+
agent: string;
|
|
15
|
+
/** Name of the specific Agent instance */
|
|
16
|
+
name?: string;
|
|
17
|
+
/** Called when the Agent's state is updated */
|
|
18
|
+
onStateUpdate?: (state: State, source: "server" | "client") => void;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* React hook for connecting to an Agent
|
|
22
|
+
* @template State Type of the Agent's state
|
|
23
|
+
* @param options Connection options
|
|
24
|
+
* @returns WebSocket connection with setState and call methods
|
|
25
|
+
*/
|
|
26
|
+
declare function useAgent<State = unknown>(
|
|
27
|
+
options: UseAgentOptions<State>
|
|
28
|
+
): PartySocket & {
|
|
29
|
+
agent: string;
|
|
30
|
+
name: string;
|
|
31
|
+
setState: (state: State) => void;
|
|
32
|
+
call: <T = unknown>(
|
|
33
|
+
method: string,
|
|
34
|
+
args?: unknown[],
|
|
35
|
+
streamOptions?: StreamOptions
|
|
36
|
+
) => Promise<T>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { type UseAgentOptions, useAgent };
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import "./chunk-HMLY7DHA.js";
|
|
2
|
+
|
|
3
|
+
// src/react.tsx
|
|
4
|
+
import { usePartySocket } from "partysocket/react";
|
|
5
|
+
import { useCallback, useRef } from "react";
|
|
6
|
+
function useAgent(options) {
|
|
7
|
+
const pendingCallsRef = useRef(
|
|
8
|
+
/* @__PURE__ */ new Map()
|
|
9
|
+
);
|
|
10
|
+
const call = useCallback(
|
|
11
|
+
(method, args = [], streamOptions) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const id = Math.random().toString(36).slice(2);
|
|
14
|
+
pendingCallsRef.current.set(id, {
|
|
15
|
+
resolve,
|
|
16
|
+
reject,
|
|
17
|
+
stream: streamOptions
|
|
18
|
+
});
|
|
19
|
+
const request = {
|
|
20
|
+
type: "rpc",
|
|
21
|
+
id,
|
|
22
|
+
method,
|
|
23
|
+
args
|
|
24
|
+
};
|
|
25
|
+
agent.send(JSON.stringify(request));
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
[]
|
|
29
|
+
);
|
|
30
|
+
const agent = usePartySocket({
|
|
31
|
+
prefix: "agents",
|
|
32
|
+
party: options.agent,
|
|
33
|
+
room: options.name || "default",
|
|
34
|
+
...options,
|
|
35
|
+
onMessage: (message) => {
|
|
36
|
+
if (typeof message.data === "string") {
|
|
37
|
+
let parsedMessage;
|
|
38
|
+
try {
|
|
39
|
+
parsedMessage = JSON.parse(message.data);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
return options.onMessage?.(message);
|
|
42
|
+
}
|
|
43
|
+
if (parsedMessage.type === "cf_agent_state") {
|
|
44
|
+
options.onStateUpdate?.(parsedMessage.state, "server");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (parsedMessage.type === "rpc") {
|
|
48
|
+
const response = parsedMessage;
|
|
49
|
+
const pending = pendingCallsRef.current.get(response.id);
|
|
50
|
+
if (!pending) return;
|
|
51
|
+
if (!response.success) {
|
|
52
|
+
pending.reject(new Error(response.error));
|
|
53
|
+
pendingCallsRef.current.delete(response.id);
|
|
54
|
+
pending.stream?.onError?.(response.error);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if ("done" in response) {
|
|
58
|
+
if (response.done) {
|
|
59
|
+
pending.resolve(response.result);
|
|
60
|
+
pendingCallsRef.current.delete(response.id);
|
|
61
|
+
pending.stream?.onDone?.(response.result);
|
|
62
|
+
} else {
|
|
63
|
+
pending.stream?.onChunk?.(response.result);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
pending.resolve(response.result);
|
|
67
|
+
pendingCallsRef.current.delete(response.id);
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
options.onMessage?.(message);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
agent.setState = (state) => {
|
|
76
|
+
agent.send(JSON.stringify({ type: "cf_agent_state", state }));
|
|
77
|
+
options.onStateUpdate?.(state, "client");
|
|
78
|
+
};
|
|
79
|
+
agent.call = call;
|
|
80
|
+
agent.agent = options.agent;
|
|
81
|
+
agent.name = options.name || "default";
|
|
82
|
+
if (agent.agent !== agent.agent.toLowerCase()) {
|
|
83
|
+
console.warn(
|
|
84
|
+
`Agent name: ${agent.agent} should probably be in lowercase. Received: ${agent.agent}`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (agent.name !== agent.name.toLowerCase()) {
|
|
88
|
+
console.warn(
|
|
89
|
+
`Agent instance name: ${agent.name} should probably be in lowercase. Received: ${agent.name}`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return agent;
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
useAgent
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=react.js.map
|