agents 0.0.0-f59e6a2 → 0.0.0-f5ccde3
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 +257 -27
- package/dist/ai-chat-agent.d.ts +122 -21
- package/dist/ai-chat-agent.js +798 -226
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration-BSiGZmYU.js +155 -0
- package/dist/ai-chat-v5-migration-BSiGZmYU.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +155 -0
- package/dist/ai-chat-v5-migration.js +3 -0
- package/dist/ai-react.d.ts +78 -72
- package/dist/ai-react.js +411 -200
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types-81H_-Uxh.d.ts +103 -0
- package/dist/ai-types-CrMqkwc_.js +24 -0
- package/dist/ai-types-CrMqkwc_.js.map +1 -0
- package/dist/ai-types.d.ts +6 -74
- package/dist/ai-types.js +3 -1
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +27 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-B3SR12TQ.js +117 -0
- package/dist/client-B3SR12TQ.js.map +1 -0
- package/dist/client-BAQA84dr.d.ts +104 -0
- package/dist/client-BZq9qau2.js +1093 -0
- package/dist/client-BZq9qau2.js.map +1 -0
- package/dist/client-CsaP9Irq.d.ts +1528 -0
- package/dist/client.d.ts +12 -79
- package/dist/client.js +3 -130
- package/dist/codemode/ai.d.ts +27 -0
- package/dist/codemode/ai.js +151 -0
- package/dist/codemode/ai.js.map +1 -0
- package/dist/do-oauth-client-provider-C2CHH5x-.d.ts +55 -0
- package/dist/do-oauth-client-provider-CwqK5SXm.js +94 -0
- package/dist/do-oauth-client-provider-CwqK5SXm.js.map +1 -0
- package/dist/index-BUle9RiP.d.ts +58 -0
- package/dist/index-Bx5KK3VJ.d.ts +587 -0
- package/dist/index.d.ts +63 -306
- package/dist/index.js +7 -21
- package/dist/mcp/client.d.ts +4 -768
- package/dist/mcp/client.js +4 -10
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -41
- package/dist/mcp/do-oauth-client-provider.js +2 -106
- package/dist/mcp/index.d.ts +190 -71
- package/dist/mcp/index.js +1436 -797
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +34 -0
- package/dist/mcp/x402.js +198 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/mcp-BwPscEiF.d.ts +61 -0
- package/dist/observability/index.d.ts +3 -0
- package/dist/observability/index.js +7 -0
- package/dist/react-CbwD4fBf.d.ts +113 -0
- package/dist/react.d.ts +10 -39
- package/dist/react.js +183 -98
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +89 -12
- package/dist/schedule.js +46 -23
- package/dist/schedule.js.map +1 -1
- package/dist/serializable-faDkMCai.d.ts +39 -0
- package/dist/serializable.d.ts +7 -0
- package/dist/serializable.js +1 -0
- package/dist/src-D_KKH_4c.js +1184 -0
- package/dist/src-D_KKH_4c.js.map +1 -0
- package/package.json +125 -68
- package/dist/ai-types.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-HMLY7DHA.js.map +0 -1
- package/dist/chunk-WNICV3OI.js +0 -436
- package/dist/chunk-WNICV3OI.js.map +0 -1
- package/dist/chunk-ZRZEISHY.js +0 -597
- package/dist/chunk-ZRZEISHY.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/mcp/client.js.map +0 -1
- package/dist/mcp/do-oauth-client-provider.js.map +0 -1
- package/src/index.ts +0 -918
package/dist/client.d.ts
CHANGED
|
@@ -1,84 +1,17 @@
|
|
|
1
|
+
import "./serializable-faDkMCai.js";
|
|
1
2
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
3
|
+
a as agentFetch,
|
|
4
|
+
i as StreamOptions,
|
|
5
|
+
n as AgentClientFetchOptions,
|
|
6
|
+
o as camelCaseToKebabCase,
|
|
7
|
+
r as AgentClientOptions,
|
|
8
|
+
t as AgentClient
|
|
9
|
+
} from "./client-BAQA84dr.js";
|
|
78
10
|
export {
|
|
79
11
|
AgentClient,
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
12
|
+
AgentClientFetchOptions,
|
|
13
|
+
AgentClientOptions,
|
|
14
|
+
StreamOptions,
|
|
83
15
|
agentFetch,
|
|
16
|
+
camelCaseToKebabCase
|
|
84
17
|
};
|
package/dist/client.js
CHANGED
|
@@ -1,131 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
__privateGet,
|
|
4
|
-
__privateSet
|
|
5
|
-
} from "./chunk-HMLY7DHA.js";
|
|
1
|
+
import "./ai-types-CrMqkwc_.js";
|
|
2
|
+
import { n as agentFetch, r as camelCaseToKebabCase, t as AgentClient } from "./client-B3SR12TQ.js";
|
|
6
3
|
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
PartySocket
|
|
10
|
-
} from "partysocket";
|
|
11
|
-
function camelCaseToKebabCase(str) {
|
|
12
|
-
if (str === str.toUpperCase() && str !== str.toLowerCase()) {
|
|
13
|
-
return str.toLowerCase().replace(/_/g, "-");
|
|
14
|
-
}
|
|
15
|
-
let kebabified = str.replace(
|
|
16
|
-
/[A-Z]/g,
|
|
17
|
-
(letter) => `-${letter.toLowerCase()}`
|
|
18
|
-
);
|
|
19
|
-
kebabified = kebabified.startsWith("-") ? kebabified.slice(1) : kebabified;
|
|
20
|
-
return kebabified.replace(/_/g, "-").replace(/-$/, "");
|
|
21
|
-
}
|
|
22
|
-
var _options, _pendingCalls;
|
|
23
|
-
var AgentClient = class extends PartySocket {
|
|
24
|
-
constructor(options) {
|
|
25
|
-
const agentNamespace = camelCaseToKebabCase(options.agent);
|
|
26
|
-
super({
|
|
27
|
-
prefix: "agents",
|
|
28
|
-
party: agentNamespace,
|
|
29
|
-
room: options.name || "default",
|
|
30
|
-
...options
|
|
31
|
-
});
|
|
32
|
-
__privateAdd(this, _options);
|
|
33
|
-
__privateAdd(this, _pendingCalls, /* @__PURE__ */ new Map());
|
|
34
|
-
this.agent = agentNamespace;
|
|
35
|
-
this.name = options.name || "default";
|
|
36
|
-
__privateSet(this, _options, options);
|
|
37
|
-
this.addEventListener("message", (event) => {
|
|
38
|
-
if (typeof event.data === "string") {
|
|
39
|
-
let parsedMessage;
|
|
40
|
-
try {
|
|
41
|
-
parsedMessage = JSON.parse(event.data);
|
|
42
|
-
} catch (error) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
if (parsedMessage.type === "cf_agent_state") {
|
|
46
|
-
__privateGet(this, _options).onStateUpdate?.(parsedMessage.state, "server");
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
if (parsedMessage.type === "rpc") {
|
|
50
|
-
const response = parsedMessage;
|
|
51
|
-
const pending = __privateGet(this, _pendingCalls).get(response.id);
|
|
52
|
-
if (!pending) return;
|
|
53
|
-
if (!response.success) {
|
|
54
|
-
pending.reject(new Error(response.error));
|
|
55
|
-
__privateGet(this, _pendingCalls).delete(response.id);
|
|
56
|
-
pending.stream?.onError?.(response.error);
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
if ("done" in response) {
|
|
60
|
-
if (response.done) {
|
|
61
|
-
pending.resolve(response.result);
|
|
62
|
-
__privateGet(this, _pendingCalls).delete(response.id);
|
|
63
|
-
pending.stream?.onDone?.(response.result);
|
|
64
|
-
} else {
|
|
65
|
-
pending.stream?.onChunk?.(response.result);
|
|
66
|
-
}
|
|
67
|
-
} else {
|
|
68
|
-
pending.resolve(response.result);
|
|
69
|
-
__privateGet(this, _pendingCalls).delete(response.id);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* @deprecated Use agentFetch instead
|
|
77
|
-
*/
|
|
78
|
-
static fetch(_opts) {
|
|
79
|
-
throw new Error(
|
|
80
|
-
"AgentClient.fetch is not implemented, use agentFetch instead"
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
setState(state) {
|
|
84
|
-
this.send(JSON.stringify({ type: "cf_agent_state", state }));
|
|
85
|
-
__privateGet(this, _options).onStateUpdate?.(state, "client");
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Call a method on the Agent
|
|
89
|
-
* @param method Name of the method to call
|
|
90
|
-
* @param args Arguments to pass to the method
|
|
91
|
-
* @param streamOptions Options for handling streaming responses
|
|
92
|
-
* @returns Promise that resolves with the method's return value
|
|
93
|
-
*/
|
|
94
|
-
async call(method, args = [], streamOptions) {
|
|
95
|
-
return new Promise((resolve, reject) => {
|
|
96
|
-
const id = Math.random().toString(36).slice(2);
|
|
97
|
-
__privateGet(this, _pendingCalls).set(id, {
|
|
98
|
-
resolve: (value) => resolve(value),
|
|
99
|
-
reject,
|
|
100
|
-
stream: streamOptions,
|
|
101
|
-
type: null
|
|
102
|
-
});
|
|
103
|
-
const request = {
|
|
104
|
-
type: "rpc",
|
|
105
|
-
id,
|
|
106
|
-
method,
|
|
107
|
-
args
|
|
108
|
-
};
|
|
109
|
-
this.send(JSON.stringify(request));
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
_options = new WeakMap();
|
|
114
|
-
_pendingCalls = new WeakMap();
|
|
115
|
-
function agentFetch(opts, init) {
|
|
116
|
-
const agentNamespace = camelCaseToKebabCase(opts.agent);
|
|
117
|
-
return PartySocket.fetch(
|
|
118
|
-
{
|
|
119
|
-
prefix: "agents",
|
|
120
|
-
party: agentNamespace,
|
|
121
|
-
room: opts.name || "default",
|
|
122
|
-
...opts
|
|
123
|
-
},
|
|
124
|
-
init
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
export {
|
|
128
|
-
AgentClient,
|
|
129
|
-
agentFetch
|
|
130
|
-
};
|
|
131
|
-
//# sourceMappingURL=client.js.map
|
|
4
|
+
export { AgentClient, agentFetch, camelCaseToKebabCase };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ToolSet } from "ai";
|
|
2
|
+
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
3
|
+
|
|
4
|
+
//#region src/codemode/ai.d.ts
|
|
5
|
+
declare class CodeModeProxy extends WorkerEntrypoint<Cloudflare.Env, {
|
|
6
|
+
binding: string;
|
|
7
|
+
name: string;
|
|
8
|
+
callback: string;
|
|
9
|
+
}> {
|
|
10
|
+
callFunction(options: {
|
|
11
|
+
functionName: string;
|
|
12
|
+
args: unknown[];
|
|
13
|
+
}): Promise<any>;
|
|
14
|
+
}
|
|
15
|
+
declare function experimental_codemode(options: {
|
|
16
|
+
tools: ToolSet;
|
|
17
|
+
prompt: string;
|
|
18
|
+
globalOutbound: Fetcher;
|
|
19
|
+
loader: WorkerLoader;
|
|
20
|
+
proxy: Fetcher<CodeModeProxy>;
|
|
21
|
+
}): Promise<{
|
|
22
|
+
prompt: string;
|
|
23
|
+
tools: ToolSet;
|
|
24
|
+
}>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { CodeModeProxy, experimental_codemode };
|
|
27
|
+
//# sourceMappingURL=ai.d.ts.map
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import "../ai-types-CrMqkwc_.js";
|
|
2
|
+
import "../client-B3SR12TQ.js";
|
|
3
|
+
import "../client-BZq9qau2.js";
|
|
4
|
+
import "../do-oauth-client-provider-CwqK5SXm.js";
|
|
5
|
+
import { s as getAgentByName } from "../src-D_KKH_4c.js";
|
|
6
|
+
import { generateObject, tool } from "ai";
|
|
7
|
+
import { openai } from "@ai-sdk/openai";
|
|
8
|
+
import { z } from "zod/v3";
|
|
9
|
+
import { compile } from "json-schema-to-typescript";
|
|
10
|
+
import { createTypeAlias, printNode, zodToTs } from "zod-to-ts";
|
|
11
|
+
import { WorkerEntrypoint, env } from "cloudflare:workers";
|
|
12
|
+
|
|
13
|
+
//#region src/codemode/ai.ts
|
|
14
|
+
function toCamelCase(str) {
|
|
15
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
|
|
16
|
+
}
|
|
17
|
+
var CodeModeProxy = class extends WorkerEntrypoint {
|
|
18
|
+
async callFunction(options) {
|
|
19
|
+
return (await getAgentByName(env[this.ctx.props.binding], this.ctx.props.name))[this.ctx.props.callback](options.functionName, options.args);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
async function experimental_codemode(options) {
|
|
23
|
+
const generatedTypes = await generateTypes(options.tools);
|
|
24
|
+
return {
|
|
25
|
+
prompt: `You are a helpful assistant. You have access to the "codemode" tool that can do different things:
|
|
26
|
+
|
|
27
|
+
${getToolDescriptions(options.tools)}
|
|
28
|
+
|
|
29
|
+
If the user asks to do anything that be achieveable by the codemode tool, then simply pass over control to it by giving it a simple function description. Don't be too verbose.
|
|
30
|
+
|
|
31
|
+
`,
|
|
32
|
+
tools: { codemode: tool({
|
|
33
|
+
description: "codemode: a tool that can generate code to achieve a goal",
|
|
34
|
+
inputSchema: z.object({ functionDescription: z.string() }),
|
|
35
|
+
outputSchema: z.object({
|
|
36
|
+
code: z.string(),
|
|
37
|
+
result: z.any()
|
|
38
|
+
}),
|
|
39
|
+
execute: async ({ functionDescription }) => {
|
|
40
|
+
try {
|
|
41
|
+
const response = await generateObject({
|
|
42
|
+
model: openai("gpt-4.1"),
|
|
43
|
+
schema: z.object({ code: z.string() }),
|
|
44
|
+
prompt: `You are a code generating machine.
|
|
45
|
+
|
|
46
|
+
In addition to regular javascript, you can also use the following functions:
|
|
47
|
+
|
|
48
|
+
${generatedTypes}
|
|
49
|
+
|
|
50
|
+
Respond only with the code, nothing else. Output javascript code.
|
|
51
|
+
|
|
52
|
+
Generate an async function that achieves the goal. This async function doesn't accept any arguments.
|
|
53
|
+
|
|
54
|
+
Here is user input: ${functionDescription}`
|
|
55
|
+
});
|
|
56
|
+
const result = await createEvaluator(response.object.code, {
|
|
57
|
+
proxy: options.proxy,
|
|
58
|
+
loader: options.loader
|
|
59
|
+
})();
|
|
60
|
+
return {
|
|
61
|
+
code: response.object.code,
|
|
62
|
+
result
|
|
63
|
+
};
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error("error", error);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}) }
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function createEvaluator(code, options) {
|
|
73
|
+
return async () => {
|
|
74
|
+
return await options.loader.get(`code-${Math.random()}`, () => {
|
|
75
|
+
return {
|
|
76
|
+
compatibilityDate: "2025-06-01",
|
|
77
|
+
compatibilityFlags: ["nodejs_compat"],
|
|
78
|
+
mainModule: "foo.js",
|
|
79
|
+
modules: { "foo.js": `
|
|
80
|
+
import { env, WorkerEntrypoint } from "cloudflare:workers";
|
|
81
|
+
|
|
82
|
+
export default class CodeModeWorker extends WorkerEntrypoint {
|
|
83
|
+
async evaluate() {
|
|
84
|
+
try {
|
|
85
|
+
const { CodeModeProxy } = env;
|
|
86
|
+
const codemode = new Proxy(
|
|
87
|
+
{},
|
|
88
|
+
{
|
|
89
|
+
get: (target, prop) => {
|
|
90
|
+
return (args) => {
|
|
91
|
+
return CodeModeProxy.callFunction({
|
|
92
|
+
functionName: prop,
|
|
93
|
+
args: args,
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return await ${code}();
|
|
101
|
+
} catch (err) {
|
|
102
|
+
return {
|
|
103
|
+
err: err.message,
|
|
104
|
+
stack: err.stack
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
` },
|
|
111
|
+
env: { CodeModeProxy: options.proxy },
|
|
112
|
+
globalOutbound: null
|
|
113
|
+
};
|
|
114
|
+
}).getEntrypoint().evaluate();
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
async function generateTypes(tools) {
|
|
118
|
+
let availableTools = "";
|
|
119
|
+
let availableTypes = "";
|
|
120
|
+
for (const [toolName, tool$1] of Object.entries(tools)) {
|
|
121
|
+
const inputJsonType = tool$1.inputSchema.jsonSchema ? await compile(tool$1.inputSchema.jsonSchema, `${toCamelCase(toolName)}Input`, {
|
|
122
|
+
format: false,
|
|
123
|
+
bannerComment: " "
|
|
124
|
+
}) : printNode(createTypeAlias(zodToTs(tool$1.inputSchema, `${toCamelCase(toolName)}Input`).node, `${toCamelCase(toolName)}Input`));
|
|
125
|
+
const outputJsonType = tool$1.outputSchema?.jsonSchema ? await compile(tool$1.outputSchema?.jsonSchema, `${toCamelCase(toolName)}Output`, {
|
|
126
|
+
format: false,
|
|
127
|
+
bannerComment: " "
|
|
128
|
+
}) : tool$1.outputSchema ? printNode(createTypeAlias(zodToTs(tool$1.outputSchema, `${toCamelCase(toolName)}Output`).node, `${toCamelCase(toolName)}Output`)) : `interface ${toCamelCase(toolName)}Output { [key: string]: any }`;
|
|
129
|
+
const InputType = inputJsonType.trim().replace("export interface", "interface");
|
|
130
|
+
const OutputType = outputJsonType.trim().replace("export interface", "interface");
|
|
131
|
+
availableTypes += `\n${InputType}`;
|
|
132
|
+
availableTypes += `\n${OutputType}`;
|
|
133
|
+
availableTools += `\n\t/*\n\t${tool$1.description?.trim()}\n\t*/`;
|
|
134
|
+
availableTools += `\n\t${toolName}: (input: ${toCamelCase(toolName)}Input) => Promise<${toCamelCase(toolName)}Output>;`;
|
|
135
|
+
availableTools += "\n";
|
|
136
|
+
}
|
|
137
|
+
availableTools = `\ndeclare const codemode: {${availableTools}}`;
|
|
138
|
+
return `
|
|
139
|
+
${availableTypes}
|
|
140
|
+
${availableTools}
|
|
141
|
+
`;
|
|
142
|
+
}
|
|
143
|
+
function getToolDescriptions(tools) {
|
|
144
|
+
return Object.entries(tools).map(([_toolName, tool$1]) => {
|
|
145
|
+
return `\n- ${tool$1.description?.trim()}`;
|
|
146
|
+
}).join("");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
export { CodeModeProxy, experimental_codemode };
|
|
151
|
+
//# sourceMappingURL=ai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai.js","names":["tool","compileJsonSchemaToTs","printNodeZodToTs"],"sources":["../../src/codemode/ai.ts"],"sourcesContent":["import { generateObject, tool, type ToolSet } from \"ai\";\nimport { openai } from \"@ai-sdk/openai\";\nimport { z } from \"zod/v3\";\nimport { compile as compileJsonSchemaToTs } from \"json-schema-to-typescript\";\nimport {\n zodToTs,\n printNode as printNodeZodToTs,\n createTypeAlias\n} from \"zod-to-ts\";\nimport { getAgentByName } from \"..\";\nimport { env, WorkerEntrypoint } from \"cloudflare:workers\";\n\nfunction toCamelCase(str: string) {\n return str\n .replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())\n .replace(/^[a-z]/, (letter) => letter.toUpperCase());\n}\n\nexport class CodeModeProxy extends WorkerEntrypoint<\n Cloudflare.Env,\n {\n binding: string;\n name: string;\n callback: string;\n }\n> {\n async callFunction(options: { functionName: string; args: unknown[] }) {\n const stub = (await getAgentByName(\n // @ts-expect-error\n env[this.ctx.props.binding] as AgentNamespace<T>,\n this.ctx.props.name\n )) as DurableObjectStub;\n // @ts-expect-error\n return stub[this.ctx.props.callback](options.functionName, options.args);\n }\n}\n\nexport async function experimental_codemode(options: {\n tools: ToolSet;\n prompt: string;\n globalOutbound: Fetcher;\n loader: WorkerLoader;\n proxy: Fetcher<CodeModeProxy>;\n}): Promise<{\n prompt: string;\n tools: ToolSet;\n}> {\n const generatedTypes = await generateTypes(options.tools);\n const prompt = `You are a helpful assistant. You have access to the \"codemode\" tool that can do different things: \n \n ${getToolDescriptions(options.tools)} \n \n If the user asks to do anything that be achieveable by the codemode tool, then simply pass over control to it by giving it a simple function description. Don't be too verbose.\n \n `;\n\n const codemodeTool = tool({\n description: \"codemode: a tool that can generate code to achieve a goal\",\n inputSchema: z.object({\n functionDescription: z.string()\n }),\n outputSchema: z.object({\n code: z.string(),\n result: z.any()\n }),\n execute: async ({ functionDescription }) => {\n try {\n const response = await generateObject({\n model: openai(\"gpt-4.1\"),\n schema: z.object({\n code: z.string()\n }),\n prompt: `You are a code generating machine.\n\n In addition to regular javascript, you can also use the following functions:\n\n ${generatedTypes} \n\n Respond only with the code, nothing else. Output javascript code.\n\n Generate an async function that achieves the goal. This async function doesn't accept any arguments.\n\n Here is user input: ${functionDescription}` // insert ts types for the tools here\n });\n\n // console.log(\"args\", response.object.args);\n const evaluator = createEvaluator(response.object.code, {\n proxy: options.proxy,\n loader: options.loader\n });\n const result = await evaluator();\n return { code: response.object.code, result: result };\n } catch (error) {\n console.error(\"error\", error);\n throw error;\n // return { code: \"\", result: error };\n }\n }\n });\n\n return { prompt, tools: { codemode: codemodeTool } };\n}\n\nfunction createEvaluator(\n code: string,\n options: {\n loader: WorkerLoader;\n proxy: Fetcher<CodeModeProxy>;\n }\n) {\n return async () => {\n const worker = options.loader.get(`code-${Math.random()}`, () => {\n return {\n compatibilityDate: \"2025-06-01\",\n compatibilityFlags: [\"nodejs_compat\"],\n mainModule: \"foo.js\",\n modules: {\n \"foo.js\": `\nimport { env, WorkerEntrypoint } from \"cloudflare:workers\";\n\nexport default class CodeModeWorker extends WorkerEntrypoint {\n async evaluate() {\n try {\n const { CodeModeProxy } = env;\n const codemode = new Proxy(\n {},\n {\n get: (target, prop) => {\n return (args) => {\n return CodeModeProxy.callFunction({\n functionName: prop,\n args: args, \n });\n };\n }\n }\n );\n\n return await ${code}();\n } catch (err) {\n return {\n err: err.message,\n stack: err.stack\n };\n }\n }\n}\n \n `\n },\n env: {\n // insert keys and bindings to tools/ts functions here\n CodeModeProxy: options.proxy\n },\n globalOutbound: null\n };\n });\n\n // @ts-expect-error TODO: fix this\n return await worker.getEntrypoint().evaluate();\n };\n}\n\nasync function generateTypes(tools: ToolSet) {\n let availableTools = \"\";\n let availableTypes = \"\";\n\n for (const [toolName, tool] of Object.entries(tools)) {\n // @ts-expect-error TODO: fix this\n const inputJsonType = tool.inputSchema.jsonSchema\n ? await compileJsonSchemaToTs(\n // @ts-expect-error TODO: fix this\n tool.inputSchema.jsonSchema,\n `${toCamelCase(toolName)}Input`,\n {\n format: false,\n bannerComment: \" \"\n }\n )\n : printNodeZodToTs(\n createTypeAlias(\n zodToTs(\n // @ts-expect-error TODO: fix this\n tool.inputSchema,\n `${toCamelCase(toolName)}Input`\n ).node,\n `${toCamelCase(toolName)}Input`\n )\n );\n\n const outputJsonType =\n // @ts-expect-error TODO: fix this\n tool.outputSchema?.jsonSchema\n ? await compileJsonSchemaToTs(\n // @ts-expect-error TODO: fix this\n tool.outputSchema?.jsonSchema,\n `${toCamelCase(toolName)}Output`,\n {\n format: false,\n bannerComment: \" \"\n }\n )\n : tool.outputSchema\n ? printNodeZodToTs(\n createTypeAlias(\n zodToTs(\n // @ts-expect-error TODO: fix this\n tool.outputSchema,\n `${toCamelCase(toolName)}Output`\n ).node,\n `${toCamelCase(toolName)}Output`\n )\n )\n : `interface ${toCamelCase(toolName)}Output { [key: string]: any }`;\n\n const InputType = inputJsonType\n .trim()\n .replace(\"export interface\", \"interface\");\n\n const OutputType = outputJsonType\n .trim()\n .replace(\"export interface\", \"interface\");\n\n availableTypes += `\\n${InputType}`;\n availableTypes += `\\n${OutputType}`;\n availableTools += `\\n\\t/*\\n\\t${tool.description?.trim()}\\n\\t*/`;\n availableTools += `\\n\\t${toolName}: (input: ${toCamelCase(toolName)}Input) => Promise<${toCamelCase(toolName)}Output>;`;\n availableTools += \"\\n\";\n }\n\n availableTools = `\\ndeclare const codemode: {${availableTools}}`;\n\n return `\n${availableTypes}\n${availableTools}\n `;\n}\n\nfunction getToolDescriptions(tools: ToolSet) {\n return Object.entries(tools)\n .map(([_toolName, tool]) => {\n return `\\n- ${tool.description?.trim()}`;\n })\n .join(\"\");\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js";
|
|
2
|
+
import {
|
|
3
|
+
OAuthClientInformation,
|
|
4
|
+
OAuthClientInformationFull,
|
|
5
|
+
OAuthClientMetadata,
|
|
6
|
+
OAuthTokens
|
|
7
|
+
} from "@modelcontextprotocol/sdk/shared/auth.js";
|
|
8
|
+
|
|
9
|
+
//#region src/mcp/do-oauth-client-provider.d.ts
|
|
10
|
+
interface AgentsOAuthProvider extends OAuthClientProvider {
|
|
11
|
+
authUrl: string | undefined;
|
|
12
|
+
clientId: string | undefined;
|
|
13
|
+
serverId: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
declare class DurableObjectOAuthClientProvider implements AgentsOAuthProvider {
|
|
16
|
+
storage: DurableObjectStorage;
|
|
17
|
+
clientName: string;
|
|
18
|
+
baseRedirectUrl: string;
|
|
19
|
+
private _authUrl_;
|
|
20
|
+
private _serverId_;
|
|
21
|
+
private _clientId_;
|
|
22
|
+
constructor(
|
|
23
|
+
storage: DurableObjectStorage,
|
|
24
|
+
clientName: string,
|
|
25
|
+
baseRedirectUrl: string
|
|
26
|
+
);
|
|
27
|
+
get clientMetadata(): OAuthClientMetadata;
|
|
28
|
+
get clientUri(): string;
|
|
29
|
+
get redirectUrl(): string;
|
|
30
|
+
get clientId(): string;
|
|
31
|
+
set clientId(clientId_: string);
|
|
32
|
+
get serverId(): string;
|
|
33
|
+
set serverId(serverId_: string);
|
|
34
|
+
keyPrefix(clientId: string): string;
|
|
35
|
+
clientInfoKey(clientId: string): string;
|
|
36
|
+
clientInformation(): Promise<OAuthClientInformation | undefined>;
|
|
37
|
+
saveClientInformation(
|
|
38
|
+
clientInformation: OAuthClientInformationFull
|
|
39
|
+
): Promise<void>;
|
|
40
|
+
tokenKey(clientId: string): string;
|
|
41
|
+
tokens(): Promise<OAuthTokens | undefined>;
|
|
42
|
+
saveTokens(tokens: OAuthTokens): Promise<void>;
|
|
43
|
+
get authUrl(): string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Because this operates on the server side (but we need browser auth), we send this url back to the user
|
|
46
|
+
* and require user interact to initiate the redirect flow
|
|
47
|
+
*/
|
|
48
|
+
redirectToAuthorization(authUrl: URL): Promise<void>;
|
|
49
|
+
codeVerifierKey(clientId: string): string;
|
|
50
|
+
saveCodeVerifier(verifier: string): Promise<void>;
|
|
51
|
+
codeVerifier(): Promise<string>;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { DurableObjectOAuthClientProvider as n, AgentsOAuthProvider as t };
|
|
55
|
+
//# sourceMappingURL=do-oauth-client-provider-C2CHH5x-.d.ts.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { nanoid } from "nanoid";
|
|
2
|
+
|
|
3
|
+
//#region src/mcp/do-oauth-client-provider.ts
|
|
4
|
+
var DurableObjectOAuthClientProvider = class {
|
|
5
|
+
constructor(storage, clientName, baseRedirectUrl) {
|
|
6
|
+
this.storage = storage;
|
|
7
|
+
this.clientName = clientName;
|
|
8
|
+
this.baseRedirectUrl = baseRedirectUrl;
|
|
9
|
+
if (!storage) throw new Error("DurableObjectOAuthClientProvider requires a valid DurableObjectStorage instance");
|
|
10
|
+
}
|
|
11
|
+
get clientMetadata() {
|
|
12
|
+
return {
|
|
13
|
+
client_name: this.clientName,
|
|
14
|
+
client_uri: this.clientUri,
|
|
15
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
16
|
+
redirect_uris: [this.redirectUrl],
|
|
17
|
+
response_types: ["code"],
|
|
18
|
+
token_endpoint_auth_method: "none"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
get clientUri() {
|
|
22
|
+
return new URL(this.redirectUrl).origin;
|
|
23
|
+
}
|
|
24
|
+
get redirectUrl() {
|
|
25
|
+
return `${this.baseRedirectUrl}/${this.serverId}`;
|
|
26
|
+
}
|
|
27
|
+
get clientId() {
|
|
28
|
+
if (!this._clientId_) throw new Error("Trying to access clientId before it was set");
|
|
29
|
+
return this._clientId_;
|
|
30
|
+
}
|
|
31
|
+
set clientId(clientId_) {
|
|
32
|
+
this._clientId_ = clientId_;
|
|
33
|
+
}
|
|
34
|
+
get serverId() {
|
|
35
|
+
if (!this._serverId_) throw new Error("Trying to access serverId before it was set");
|
|
36
|
+
return this._serverId_;
|
|
37
|
+
}
|
|
38
|
+
set serverId(serverId_) {
|
|
39
|
+
this._serverId_ = serverId_;
|
|
40
|
+
}
|
|
41
|
+
keyPrefix(clientId) {
|
|
42
|
+
return `/${this.clientName}/${this.serverId}/${clientId}`;
|
|
43
|
+
}
|
|
44
|
+
clientInfoKey(clientId) {
|
|
45
|
+
return `${this.keyPrefix(clientId)}/client_info/`;
|
|
46
|
+
}
|
|
47
|
+
async clientInformation() {
|
|
48
|
+
if (!this._clientId_) return;
|
|
49
|
+
return await this.storage.get(this.clientInfoKey(this.clientId)) ?? void 0;
|
|
50
|
+
}
|
|
51
|
+
async saveClientInformation(clientInformation) {
|
|
52
|
+
await this.storage.put(this.clientInfoKey(clientInformation.client_id), clientInformation);
|
|
53
|
+
this.clientId = clientInformation.client_id;
|
|
54
|
+
}
|
|
55
|
+
tokenKey(clientId) {
|
|
56
|
+
return `${this.keyPrefix(clientId)}/token`;
|
|
57
|
+
}
|
|
58
|
+
async tokens() {
|
|
59
|
+
if (!this._clientId_) return;
|
|
60
|
+
return await this.storage.get(this.tokenKey(this.clientId)) ?? void 0;
|
|
61
|
+
}
|
|
62
|
+
async saveTokens(tokens) {
|
|
63
|
+
await this.storage.put(this.tokenKey(this.clientId), tokens);
|
|
64
|
+
}
|
|
65
|
+
get authUrl() {
|
|
66
|
+
return this._authUrl_;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Because this operates on the server side (but we need browser auth), we send this url back to the user
|
|
70
|
+
* and require user interact to initiate the redirect flow
|
|
71
|
+
*/
|
|
72
|
+
async redirectToAuthorization(authUrl) {
|
|
73
|
+
const stateToken = nanoid();
|
|
74
|
+
authUrl.searchParams.set("state", stateToken);
|
|
75
|
+
this._authUrl_ = authUrl.toString();
|
|
76
|
+
}
|
|
77
|
+
codeVerifierKey(clientId) {
|
|
78
|
+
return `${this.keyPrefix(clientId)}/code_verifier`;
|
|
79
|
+
}
|
|
80
|
+
async saveCodeVerifier(verifier) {
|
|
81
|
+
const key = this.codeVerifierKey(this.clientId);
|
|
82
|
+
if (await this.storage.get(key)) return;
|
|
83
|
+
await this.storage.put(key, verifier);
|
|
84
|
+
}
|
|
85
|
+
async codeVerifier() {
|
|
86
|
+
const codeVerifier = await this.storage.get(this.codeVerifierKey(this.clientId));
|
|
87
|
+
if (!codeVerifier) throw new Error("No code verifier found");
|
|
88
|
+
return codeVerifier;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
export { DurableObjectOAuthClientProvider as t };
|
|
94
|
+
//# sourceMappingURL=do-oauth-client-provider-CwqK5SXm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"do-oauth-client-provider-CwqK5SXm.js","names":["storage: DurableObjectStorage","clientName: string","baseRedirectUrl: string"],"sources":["../src/mcp/do-oauth-client-provider.ts"],"sourcesContent":["import type { OAuthClientProvider } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport type {\n OAuthClientInformation,\n OAuthClientInformationFull,\n OAuthClientMetadata,\n OAuthTokens\n} from \"@modelcontextprotocol/sdk/shared/auth.js\";\nimport { nanoid } from \"nanoid\";\n\n// A slight extension to the standard OAuthClientProvider interface because `redirectToAuthorization` doesn't give us the interface we need\n// This allows us to track authentication for a specific server and associated dynamic client registration\nexport interface AgentsOAuthProvider extends OAuthClientProvider {\n authUrl: string | undefined;\n clientId: string | undefined;\n serverId: string | undefined;\n}\n\nexport class DurableObjectOAuthClientProvider implements AgentsOAuthProvider {\n private _authUrl_: string | undefined;\n private _serverId_: string | undefined;\n private _clientId_: string | undefined;\n\n constructor(\n public storage: DurableObjectStorage,\n public clientName: string,\n public baseRedirectUrl: string\n ) {\n if (!storage) {\n throw new Error(\n \"DurableObjectOAuthClientProvider requires a valid DurableObjectStorage instance\"\n );\n }\n }\n\n get clientMetadata(): OAuthClientMetadata {\n return {\n client_name: this.clientName,\n client_uri: this.clientUri,\n grant_types: [\"authorization_code\", \"refresh_token\"],\n redirect_uris: [this.redirectUrl],\n response_types: [\"code\"],\n token_endpoint_auth_method: \"none\"\n };\n }\n\n get clientUri() {\n return new URL(this.redirectUrl).origin;\n }\n\n get redirectUrl() {\n return `${this.baseRedirectUrl}/${this.serverId}`;\n }\n\n get clientId() {\n if (!this._clientId_) {\n throw new Error(\"Trying to access clientId before it was set\");\n }\n return this._clientId_;\n }\n\n set clientId(clientId_: string) {\n this._clientId_ = clientId_;\n }\n\n get serverId() {\n if (!this._serverId_) {\n throw new Error(\"Trying to access serverId before it was set\");\n }\n return this._serverId_;\n }\n\n set serverId(serverId_: string) {\n this._serverId_ = serverId_;\n }\n\n keyPrefix(clientId: string) {\n return `/${this.clientName}/${this.serverId}/${clientId}`;\n }\n\n clientInfoKey(clientId: string) {\n return `${this.keyPrefix(clientId)}/client_info/`;\n }\n\n async clientInformation(): Promise<OAuthClientInformation | undefined> {\n if (!this._clientId_) {\n return undefined;\n }\n return (\n (await this.storage.get<OAuthClientInformation>(\n this.clientInfoKey(this.clientId)\n )) ?? undefined\n );\n }\n\n async saveClientInformation(\n clientInformation: OAuthClientInformationFull\n ): Promise<void> {\n await this.storage.put(\n this.clientInfoKey(clientInformation.client_id),\n clientInformation\n );\n this.clientId = clientInformation.client_id;\n }\n\n tokenKey(clientId: string) {\n return `${this.keyPrefix(clientId)}/token`;\n }\n\n async tokens(): Promise<OAuthTokens | undefined> {\n if (!this._clientId_) {\n return undefined;\n }\n return (\n (await this.storage.get<OAuthTokens>(this.tokenKey(this.clientId))) ??\n undefined\n );\n }\n\n async saveTokens(tokens: OAuthTokens): Promise<void> {\n await this.storage.put(this.tokenKey(this.clientId), tokens);\n }\n\n get authUrl() {\n return this._authUrl_;\n }\n\n /**\n * Because this operates on the server side (but we need browser auth), we send this url back to the user\n * and require user interact to initiate the redirect flow\n */\n async redirectToAuthorization(authUrl: URL): Promise<void> {\n // Generate secure random token for state parameter\n const stateToken = nanoid();\n authUrl.searchParams.set(\"state\", stateToken);\n this._authUrl_ = authUrl.toString();\n }\n\n codeVerifierKey(clientId: string) {\n return `${this.keyPrefix(clientId)}/code_verifier`;\n }\n\n async saveCodeVerifier(verifier: string): Promise<void> {\n const key = this.codeVerifierKey(this.clientId);\n\n // Don't overwrite existing verifier to preserve first PKCE verifier\n const existing = await this.storage.get<string>(key);\n if (existing) {\n return;\n }\n\n await this.storage.put(key, verifier);\n }\n\n async codeVerifier(): Promise<string> {\n const codeVerifier = await this.storage.get<string>(\n this.codeVerifierKey(this.clientId)\n );\n if (!codeVerifier) {\n throw new Error(\"No code verifier found\");\n }\n return codeVerifier;\n }\n}\n"],"mappings":";;;AAiBA,IAAa,mCAAb,MAA6E;CAK3E,YACE,AAAOA,SACP,AAAOC,YACP,AAAOC,iBACP;EAHO;EACA;EACA;AAEP,MAAI,CAAC,QACH,OAAM,IAAI,MACR,kFACD;;CAIL,IAAI,iBAAsC;AACxC,SAAO;GACL,aAAa,KAAK;GAClB,YAAY,KAAK;GACjB,aAAa,CAAC,sBAAsB,gBAAgB;GACpD,eAAe,CAAC,KAAK,YAAY;GACjC,gBAAgB,CAAC,OAAO;GACxB,4BAA4B;GAC7B;;CAGH,IAAI,YAAY;AACd,SAAO,IAAI,IAAI,KAAK,YAAY,CAAC;;CAGnC,IAAI,cAAc;AAChB,SAAO,GAAG,KAAK,gBAAgB,GAAG,KAAK;;CAGzC,IAAI,WAAW;AACb,MAAI,CAAC,KAAK,WACR,OAAM,IAAI,MAAM,8CAA8C;AAEhE,SAAO,KAAK;;CAGd,IAAI,SAAS,WAAmB;AAC9B,OAAK,aAAa;;CAGpB,IAAI,WAAW;AACb,MAAI,CAAC,KAAK,WACR,OAAM,IAAI,MAAM,8CAA8C;AAEhE,SAAO,KAAK;;CAGd,IAAI,SAAS,WAAmB;AAC9B,OAAK,aAAa;;CAGpB,UAAU,UAAkB;AAC1B,SAAO,IAAI,KAAK,WAAW,GAAG,KAAK,SAAS,GAAG;;CAGjD,cAAc,UAAkB;AAC9B,SAAO,GAAG,KAAK,UAAU,SAAS,CAAC;;CAGrC,MAAM,oBAAiE;AACrE,MAAI,CAAC,KAAK,WACR;AAEF,SACG,MAAM,KAAK,QAAQ,IAClB,KAAK,cAAc,KAAK,SAAS,CAClC,IAAK;;CAIV,MAAM,sBACJ,mBACe;AACf,QAAM,KAAK,QAAQ,IACjB,KAAK,cAAc,kBAAkB,UAAU,EAC/C,kBACD;AACD,OAAK,WAAW,kBAAkB;;CAGpC,SAAS,UAAkB;AACzB,SAAO,GAAG,KAAK,UAAU,SAAS,CAAC;;CAGrC,MAAM,SAA2C;AAC/C,MAAI,CAAC,KAAK,WACR;AAEF,SACG,MAAM,KAAK,QAAQ,IAAiB,KAAK,SAAS,KAAK,SAAS,CAAC,IAClE;;CAIJ,MAAM,WAAW,QAAoC;AACnD,QAAM,KAAK,QAAQ,IAAI,KAAK,SAAS,KAAK,SAAS,EAAE,OAAO;;CAG9D,IAAI,UAAU;AACZ,SAAO,KAAK;;;;;;CAOd,MAAM,wBAAwB,SAA6B;EAEzD,MAAM,aAAa,QAAQ;AAC3B,UAAQ,aAAa,IAAI,SAAS,WAAW;AAC7C,OAAK,YAAY,QAAQ,UAAU;;CAGrC,gBAAgB,UAAkB;AAChC,SAAO,GAAG,KAAK,UAAU,SAAS,CAAC;;CAGrC,MAAM,iBAAiB,UAAiC;EACtD,MAAM,MAAM,KAAK,gBAAgB,KAAK,SAAS;AAI/C,MADiB,MAAM,KAAK,QAAQ,IAAY,IAAI,CAElD;AAGF,QAAM,KAAK,QAAQ,IAAI,KAAK,SAAS;;CAGvC,MAAM,eAAgC;EACpC,MAAM,eAAe,MAAM,KAAK,QAAQ,IACtC,KAAK,gBAAgB,KAAK,SAAS,CACpC;AACD,MAAI,CAAC,aACH,OAAM,IAAI,MAAM,yBAAyB;AAE3C,SAAO"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { n as BaseEvent, t as MCPObservabilityEvent } from "./mcp-BwPscEiF.js";
|
|
2
|
+
|
|
3
|
+
//#region src/observability/agent.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Agent-specific observability events
|
|
7
|
+
* These track the lifecycle and operations of an Agent
|
|
8
|
+
*/
|
|
9
|
+
type AgentObservabilityEvent =
|
|
10
|
+
| BaseEvent<"state:update", {}>
|
|
11
|
+
| BaseEvent<
|
|
12
|
+
"rpc",
|
|
13
|
+
{
|
|
14
|
+
method: string;
|
|
15
|
+
streaming?: boolean;
|
|
16
|
+
}
|
|
17
|
+
>
|
|
18
|
+
| BaseEvent<"message:request" | "message:response", {}>
|
|
19
|
+
| BaseEvent<"message:clear">
|
|
20
|
+
| BaseEvent<
|
|
21
|
+
"schedule:create" | "schedule:execute" | "schedule:cancel",
|
|
22
|
+
{
|
|
23
|
+
callback: string;
|
|
24
|
+
id: string;
|
|
25
|
+
}
|
|
26
|
+
>
|
|
27
|
+
| BaseEvent<"destroy">
|
|
28
|
+
| BaseEvent<
|
|
29
|
+
"connect",
|
|
30
|
+
{
|
|
31
|
+
connectionId: string;
|
|
32
|
+
}
|
|
33
|
+
>;
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/observability/index.d.ts
|
|
36
|
+
/**
|
|
37
|
+
* Union of all observability event types from different domains
|
|
38
|
+
*/
|
|
39
|
+
type ObservabilityEvent = AgentObservabilityEvent | MCPObservabilityEvent;
|
|
40
|
+
interface Observability {
|
|
41
|
+
/**
|
|
42
|
+
* Emit an event for the Agent's observability implementation to handle.
|
|
43
|
+
* @param event - The event to emit
|
|
44
|
+
* @param ctx - The execution context of the invocation (optional)
|
|
45
|
+
*/
|
|
46
|
+
emit(event: ObservabilityEvent, ctx?: DurableObjectState): void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A generic observability implementation that logs events to the console.
|
|
50
|
+
*/
|
|
51
|
+
declare const genericObservability: Observability;
|
|
52
|
+
//#endregion
|
|
53
|
+
export {
|
|
54
|
+
ObservabilityEvent as n,
|
|
55
|
+
genericObservability as r,
|
|
56
|
+
Observability as t
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=index-BUle9RiP.d.ts.map
|