agents 0.0.0-1eac06e → 0.0.0-1f8c517
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 +32 -7
- package/dist/ai-chat-agent.d.ts +9 -8
- package/dist/ai-chat-agent.js +142 -59
- 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 +59 -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-PVQZBKN7.js → chunk-LL2AFX7V.js} +5 -2
- package/dist/chunk-LL2AFX7V.js.map +1 -0
- package/dist/{chunk-XJR75HO7.js → chunk-MH46VMM4.js} +91 -27
- package/dist/chunk-MH46VMM4.js.map +1 -0
- package/dist/{chunk-KUH345EY.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-2DJ6XAU6.js → chunk-YDUDMOL6.js} +115 -89
- package/dist/chunk-YDUDMOL6.js.map +1 -0
- package/dist/client-CvaJdLQA.d.ts +5015 -0
- package/dist/client.js +2 -1
- package/dist/index.d.ts +555 -32
- package/dist/index.js +7 -4
- package/dist/mcp/client.d.ts +9 -1057
- 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 +63 -88
- package/dist/mcp/index.js +813 -692
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +46 -14
- package/dist/observability/index.js +5 -4
- package/dist/react.d.ts +4 -2
- 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 +19 -8
- package/src/index.ts +152 -100
- package/dist/chunk-2DJ6XAU6.js.map +0 -1
- package/dist/chunk-KUH345EY.js.map +0 -1
- package/dist/chunk-PVQZBKN7.js.map +0 -1
- package/dist/chunk-XJR75HO7.js.map +0 -1
- package/dist/index-CLW1aEBr.d.ts +0 -615
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// src/ai-chat-v5-migration.ts
|
|
2
|
+
var STATE_MAP = {
|
|
3
|
+
"partial-call": "input-streaming",
|
|
4
|
+
call: "input-available",
|
|
5
|
+
result: "output-available",
|
|
6
|
+
error: "output-error"
|
|
7
|
+
};
|
|
8
|
+
function isUIMessage(message) {
|
|
9
|
+
return typeof message === "object" && message !== null && "parts" in message && Array.isArray(message.parts);
|
|
10
|
+
}
|
|
11
|
+
function isLegacyMessage(message) {
|
|
12
|
+
return typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.role === "string" && typeof message.content === "string";
|
|
13
|
+
}
|
|
14
|
+
function isCorruptArrayMessage(message) {
|
|
15
|
+
return typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.role === "string" && Array.isArray(message.content) && !("parts" in message);
|
|
16
|
+
}
|
|
17
|
+
function autoTransformMessage(message, index = 0) {
|
|
18
|
+
if (isUIMessage(message)) {
|
|
19
|
+
return message;
|
|
20
|
+
}
|
|
21
|
+
const parts = [];
|
|
22
|
+
if (message.reasoning) {
|
|
23
|
+
parts.push({
|
|
24
|
+
type: "reasoning",
|
|
25
|
+
text: message.reasoning
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (message.toolInvocations && Array.isArray(message.toolInvocations)) {
|
|
29
|
+
message.toolInvocations.forEach((inv) => {
|
|
30
|
+
if (typeof inv === "object" && inv !== null && "toolName" in inv) {
|
|
31
|
+
const invObj = inv;
|
|
32
|
+
parts.push({
|
|
33
|
+
type: `tool-${invObj.toolName}`,
|
|
34
|
+
toolCallId: invObj.toolCallId,
|
|
35
|
+
state: STATE_MAP[invObj.state] || "input-available",
|
|
36
|
+
input: invObj.args,
|
|
37
|
+
output: invObj.result !== void 0 ? invObj.result : null
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (message.parts && Array.isArray(message.parts)) {
|
|
43
|
+
message.parts.forEach((part) => {
|
|
44
|
+
if (typeof part === "object" && part !== null && "type" in part) {
|
|
45
|
+
const partObj = part;
|
|
46
|
+
if (partObj.type === "file") {
|
|
47
|
+
parts.push({
|
|
48
|
+
type: "file",
|
|
49
|
+
url: partObj.url || (partObj.data ? `data:${partObj.mimeType || partObj.mediaType};base64,${partObj.data}` : void 0),
|
|
50
|
+
mediaType: partObj.mediaType || partObj.mimeType,
|
|
51
|
+
filename: partObj.filename
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(message.content)) {
|
|
58
|
+
message.content.forEach((item) => {
|
|
59
|
+
if (typeof item === "object" && item !== null && "text" in item) {
|
|
60
|
+
const itemObj = item;
|
|
61
|
+
parts.push({
|
|
62
|
+
type: itemObj.type || "text",
|
|
63
|
+
text: itemObj.text || ""
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (!parts.length && message.content !== void 0) {
|
|
69
|
+
parts.push({
|
|
70
|
+
type: "text",
|
|
71
|
+
text: typeof message.content === "string" ? message.content : JSON.stringify(message.content)
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (!parts.length) {
|
|
75
|
+
parts.push({
|
|
76
|
+
type: "text",
|
|
77
|
+
text: typeof message === "string" ? message : JSON.stringify(message)
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
id: message.id || `msg-${index}`,
|
|
82
|
+
role: message.role === "data" ? "system" : message.role || "user",
|
|
83
|
+
parts
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function migrateToUIMessage(message) {
|
|
87
|
+
return autoTransformMessage(message);
|
|
88
|
+
}
|
|
89
|
+
function autoTransformMessages(messages) {
|
|
90
|
+
return messages.map((msg, i) => autoTransformMessage(msg, i));
|
|
91
|
+
}
|
|
92
|
+
function migrateMessagesToUIFormat(messages) {
|
|
93
|
+
return autoTransformMessages(messages);
|
|
94
|
+
}
|
|
95
|
+
function needsMigration(messages) {
|
|
96
|
+
return messages.some((message) => {
|
|
97
|
+
if (isUIMessage(message)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (isCorruptArrayMessage(message)) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
if (isLegacyMessage(message)) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function analyzeCorruption(messages) {
|
|
110
|
+
const stats = {
|
|
111
|
+
total: messages.length,
|
|
112
|
+
clean: 0,
|
|
113
|
+
legacyString: 0,
|
|
114
|
+
corruptArray: 0,
|
|
115
|
+
unknown: 0,
|
|
116
|
+
examples: {}
|
|
117
|
+
};
|
|
118
|
+
for (const message of messages) {
|
|
119
|
+
if (isUIMessage(message)) {
|
|
120
|
+
stats.clean++;
|
|
121
|
+
} else if (isCorruptArrayMessage(message)) {
|
|
122
|
+
stats.corruptArray++;
|
|
123
|
+
if (!stats.examples.corruptArray) {
|
|
124
|
+
stats.examples.corruptArray = message;
|
|
125
|
+
}
|
|
126
|
+
} else if (isLegacyMessage(message)) {
|
|
127
|
+
stats.legacyString++;
|
|
128
|
+
if (!stats.examples.legacyString) {
|
|
129
|
+
stats.examples.legacyString = message;
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
stats.unknown++;
|
|
133
|
+
if (!stats.examples.unknown) {
|
|
134
|
+
stats.examples.unknown = message;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return stats;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export {
|
|
142
|
+
isUIMessage,
|
|
143
|
+
autoTransformMessage,
|
|
144
|
+
migrateToUIMessage,
|
|
145
|
+
autoTransformMessages,
|
|
146
|
+
migrateMessagesToUIFormat,
|
|
147
|
+
needsMigration,
|
|
148
|
+
analyzeCorruption
|
|
149
|
+
};
|
|
150
|
+
//# sourceMappingURL=chunk-UJVEAURM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ai-chat-v5-migration.ts"],"sourcesContent":["import type { UIMessage } from \"ai\";\n\n/**\n * AI SDK v5 Migration following https://jhak.im/blog/ai-sdk-migration-handling-previously-saved-messages\n * Using exact types from the official AI SDK documentation\n */\n\n/**\n * AI SDK v5 Message Part types reference (from official AI SDK documentation)\n *\n * The migration logic below transforms legacy messages to match these official AI SDK v5 formats:\n * - TextUIPart: { type: \"text\", text: string, state?: \"streaming\" | \"done\" }\n * - ReasoningUIPart: { type: \"reasoning\", text: string, state?: \"streaming\" | \"done\", providerMetadata?: Record<string, unknown> }\n * - FileUIPart: { type: \"file\", mediaType: string, filename?: string, url: string }\n * - ToolUIPart: { type: `tool-${string}`, toolCallId: string, state: \"input-streaming\" | \"input-available\" | \"output-available\" | \"output-error\", input?: Record<string, unknown>, output?: unknown, errorText?: string, providerExecuted?: boolean }\n */\n\n/**\n * Tool invocation from v4 format\n */\ntype ToolInvocation = {\n toolCallId: string;\n toolName: string;\n args: Record<string, unknown>;\n result?: unknown;\n state: \"partial-call\" | \"call\" | \"result\" | \"error\";\n};\n\n/**\n * Legacy part from v4 format\n */\ntype LegacyPart = {\n type: string;\n text?: string;\n url?: string;\n data?: string;\n mimeType?: string;\n mediaType?: string;\n filename?: string;\n};\n\n/**\n * Legacy message format from AI SDK v4\n */\nexport type LegacyMessage = {\n id?: string;\n role: string;\n content: string;\n reasoning?: string;\n toolInvocations?: ToolInvocation[];\n parts?: LegacyPart[];\n [key: string]: unknown;\n};\n\n/**\n * Corrupt content item\n */\ntype CorruptContentItem = {\n type: string;\n text: string;\n};\n\n/**\n * Corrupted message format - has content as array instead of parts\n */\nexport type CorruptArrayMessage = {\n id?: string;\n role: string;\n content: CorruptContentItem[];\n reasoning?: string;\n toolInvocations?: ToolInvocation[];\n [key: string]: unknown;\n};\n\n/**\n * Union type for messages that could be in any format\n */\nexport type MigratableMessage = LegacyMessage | CorruptArrayMessage | UIMessage;\n\n/**\n * Tool call state mapping for v4 to v5 migration\n */\nconst STATE_MAP = {\n \"partial-call\": \"input-streaming\",\n call: \"input-available\",\n result: \"output-available\",\n error: \"output-error\"\n} as const;\n\n/**\n * Checks if a message is already in the UIMessage format (has parts array)\n */\nexport function isUIMessage(message: unknown): message is UIMessage {\n return (\n typeof message === \"object\" &&\n message !== null &&\n \"parts\" in message &&\n Array.isArray((message as { parts: unknown }).parts)\n );\n}\n\n/**\n * Type guard to check if a message is in legacy format (content as string)\n */\nfunction isLegacyMessage(message: unknown): message is LegacyMessage {\n return (\n typeof message === \"object\" &&\n message !== null &&\n \"role\" in message &&\n \"content\" in message &&\n typeof (message as { role: unknown }).role === \"string\" &&\n typeof (message as { content: unknown }).content === \"string\"\n );\n}\n\n/**\n * Type guard to check if a message has corrupted array content format\n * Detects: {role: \"user\", content: [{type: \"text\", text: \"...\"}]}\n */\nfunction isCorruptArrayMessage(\n message: unknown\n): message is CorruptArrayMessage {\n return (\n typeof message === \"object\" &&\n message !== null &&\n \"role\" in message &&\n \"content\" in message &&\n typeof (message as { role: unknown }).role === \"string\" &&\n Array.isArray((message as { content: unknown }).content) &&\n !(\"parts\" in message) // Ensure it's not already a UIMessage\n );\n}\n\n/**\n * Internal message part type for transformation\n */\ntype TransformMessagePart = {\n type: string;\n text?: string;\n toolCallId?: string;\n state?: string;\n input?: Record<string, unknown>;\n output?: unknown;\n url?: string;\n mediaType?: string;\n errorText?: string;\n filename?: string;\n};\n\n/**\n * Input message that could be in any format - using unknown for flexibility\n */\ntype InputMessage = {\n id?: string;\n role?: string;\n content?: unknown;\n reasoning?: string;\n toolInvocations?: unknown[];\n parts?: unknown[];\n [key: string]: unknown;\n};\n\n/**\n * Automatic message transformer following the blog post pattern\n * Handles comprehensive migration from AI SDK v4 to v5 format\n * @param message - Message in any legacy format\n * @param index - Index for ID generation fallback\n * @returns UIMessage in v5 format\n */\nexport function autoTransformMessage(\n message: InputMessage,\n index = 0\n): UIMessage {\n // Already in v5 format\n if (isUIMessage(message)) {\n return message;\n }\n\n const parts: TransformMessagePart[] = [];\n\n // Handle reasoning transformation\n if (message.reasoning) {\n parts.push({\n type: \"reasoning\",\n text: message.reasoning\n });\n }\n\n // Handle tool invocations transformation\n if (message.toolInvocations && Array.isArray(message.toolInvocations)) {\n message.toolInvocations.forEach((inv: unknown) => {\n if (typeof inv === \"object\" && inv !== null && \"toolName\" in inv) {\n const invObj = inv as ToolInvocation;\n parts.push({\n type: `tool-${invObj.toolName}`,\n toolCallId: invObj.toolCallId,\n state:\n STATE_MAP[invObj.state as keyof typeof STATE_MAP] ||\n \"input-available\",\n input: invObj.args,\n output: invObj.result !== undefined ? invObj.result : null\n });\n }\n });\n }\n\n // Handle file parts transformation\n if (message.parts && Array.isArray(message.parts)) {\n message.parts.forEach((part: unknown) => {\n if (typeof part === \"object\" && part !== null && \"type\" in part) {\n const partObj = part as LegacyPart;\n if (partObj.type === \"file\") {\n parts.push({\n type: \"file\",\n url:\n partObj.url ||\n (partObj.data\n ? `data:${partObj.mimeType || partObj.mediaType};base64,${partObj.data}`\n : undefined),\n mediaType: partObj.mediaType || partObj.mimeType,\n filename: partObj.filename\n });\n }\n }\n });\n }\n\n // Handle corrupt array format: {role: \"user\", content: [{type: \"text\", text: \"...\"}]}\n if (Array.isArray(message.content)) {\n message.content.forEach((item: unknown) => {\n if (typeof item === \"object\" && item !== null && \"text\" in item) {\n const itemObj = item as CorruptContentItem;\n parts.push({\n type: itemObj.type || \"text\",\n text: itemObj.text || \"\"\n });\n }\n });\n }\n\n // Fallback: convert plain content to text part\n if (!parts.length && message.content !== undefined) {\n parts.push({\n type: \"text\",\n text:\n typeof message.content === \"string\"\n ? message.content\n : JSON.stringify(message.content)\n });\n }\n\n // If still no parts, create a default text part\n if (!parts.length) {\n parts.push({\n type: \"text\",\n text: typeof message === \"string\" ? message : JSON.stringify(message)\n });\n }\n\n return {\n id: message.id || `msg-${index}`,\n role:\n message.role === \"data\"\n ? \"system\"\n : (message.role as \"user\" | \"assistant\" | \"system\") || \"user\",\n parts: parts as UIMessage[\"parts\"]\n };\n}\n\n/**\n * Legacy single message migration for backward compatibility\n */\nexport function migrateToUIMessage(message: MigratableMessage): UIMessage {\n return autoTransformMessage(message as InputMessage);\n}\n\n/**\n * Automatic message transformer for arrays following the blog post pattern\n * @param messages - Array of messages in any format\n * @returns Array of UIMessages in v5 format\n */\nexport function autoTransformMessages(messages: unknown[]): UIMessage[] {\n return messages.map((msg, i) => autoTransformMessage(msg as InputMessage, i));\n}\n\n/**\n * Migrates an array of messages to UIMessage format (legacy compatibility)\n * @param messages - Array of messages in old or new format\n * @returns Array of UIMessages in the new format\n */\nexport function migrateMessagesToUIFormat(\n messages: MigratableMessage[]\n): UIMessage[] {\n return autoTransformMessages(messages as InputMessage[]);\n}\n\n/**\n * Checks if any messages in an array need migration\n * @param messages - Array of messages to check\n * @returns true if any messages are not in proper UIMessage format\n */\nexport function needsMigration(messages: unknown[]): boolean {\n return messages.some((message) => {\n // If it's already a UIMessage, no migration needed\n if (isUIMessage(message)) {\n return false;\n }\n\n // Check for corrupt array format specifically\n if (isCorruptArrayMessage(message)) {\n return true;\n }\n\n // Check for legacy string format\n if (isLegacyMessage(message)) {\n return true;\n }\n\n // Any other format needs migration\n return true;\n });\n}\n\n/**\n * Analyzes the corruption types in a message array for debugging\n * @param messages - Array of messages to analyze\n * @returns Statistics about corruption types found\n */\nexport function analyzeCorruption(messages: unknown[]): {\n total: number;\n clean: number;\n legacyString: number;\n corruptArray: number;\n unknown: number;\n examples: {\n legacyString?: unknown;\n corruptArray?: unknown;\n unknown?: unknown;\n };\n} {\n const stats = {\n total: messages.length,\n clean: 0,\n legacyString: 0,\n corruptArray: 0,\n unknown: 0,\n examples: {} as {\n legacyString?: unknown;\n corruptArray?: unknown;\n unknown?: unknown;\n }\n };\n\n for (const message of messages) {\n if (isUIMessage(message)) {\n stats.clean++;\n } else if (isCorruptArrayMessage(message)) {\n stats.corruptArray++;\n if (!stats.examples.corruptArray) {\n stats.examples.corruptArray = message;\n }\n } else if (isLegacyMessage(message)) {\n stats.legacyString++;\n if (!stats.examples.legacyString) {\n stats.examples.legacyString = message;\n }\n } else {\n stats.unknown++;\n if (!stats.examples.unknown) {\n stats.examples.unknown = message;\n }\n }\n }\n\n return stats;\n}\n"],"mappings":";AAkFA,IAAM,YAAY;AAAA,EAChB,gBAAgB;AAAA,EAChB,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACT;AAKO,SAAS,YAAY,SAAwC;AAClE,SACE,OAAO,YAAY,YACnB,YAAY,QACZ,WAAW,WACX,MAAM,QAAS,QAA+B,KAAK;AAEvD;AAKA,SAAS,gBAAgB,SAA4C;AACnE,SACE,OAAO,YAAY,YACnB,YAAY,QACZ,UAAU,WACV,aAAa,WACb,OAAQ,QAA8B,SAAS,YAC/C,OAAQ,QAAiC,YAAY;AAEzD;AAMA,SAAS,sBACP,SACgC;AAChC,SACE,OAAO,YAAY,YACnB,YAAY,QACZ,UAAU,WACV,aAAa,WACb,OAAQ,QAA8B,SAAS,YAC/C,MAAM,QAAS,QAAiC,OAAO,KACvD,EAAE,WAAW;AAEjB;AAsCO,SAAS,qBACd,SACA,QAAQ,GACG;AAEX,MAAI,YAAY,OAAO,GAAG;AACxB,WAAO;AAAA,EACT;AAEA,QAAM,QAAgC,CAAC;AAGvC,MAAI,QAAQ,WAAW;AACrB,UAAM,KA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;AACxB,YAAM;AAAA,IACR,WAAW,sBAAsB,OAAO,GAAG;AACzC,YAAM;AACN,UAAI,CAAC,MAAM,SAAS,cAAc;AAChC,cAAM,SAAS,eAAe;AAAA,MAChC;AAAA,IACF,WAAW,gBAAgB,OAAO,GAAG;AACnC,YAAM;AACN,UAAI,CAAC,MAAM,SAAS,cAAc;AAChC,cAAM,SAAS,eAAe;AAAA,MAChC;AAAA,IACF,OAAO;AACL,YAAM;AACN,UAAI,CAAC,MAAM,SAAS,SAAS;AAC3B,cAAM,SAAS,UAAU;AAAA,MAC3B;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -1,12 +1,12 @@
|
|
|
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";
|
|
@@ -19,13 +19,13 @@ import {
|
|
|
19
19
|
routePartykitRequest
|
|
20
20
|
} from "partyserver";
|
|
21
21
|
function isRPCRequest(msg) {
|
|
22
|
-
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);
|
|
23
23
|
}
|
|
24
24
|
function isStateUpdateMessage(msg) {
|
|
25
|
-
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;
|
|
26
26
|
}
|
|
27
27
|
var callableMetadata = /* @__PURE__ */ new Map();
|
|
28
|
-
function
|
|
28
|
+
function callable(metadata = {}) {
|
|
29
29
|
return function callableDecorator(target, context) {
|
|
30
30
|
if (!callableMetadata.has(target)) {
|
|
31
31
|
callableMetadata.set(target, metadata);
|
|
@@ -33,6 +33,16 @@ function unstable_callable(metadata = {}) {
|
|
|
33
33
|
return target;
|
|
34
34
|
};
|
|
35
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
|
+
};
|
|
36
46
|
function getNextCronTime(cron) {
|
|
37
47
|
const interval = parseCronExpression(cron);
|
|
38
48
|
return interval.getNextDate();
|
|
@@ -55,7 +65,10 @@ function getCurrentAgent() {
|
|
|
55
65
|
}
|
|
56
66
|
function withAgentContext(method) {
|
|
57
67
|
return function(...args) {
|
|
58
|
-
const { connection, request, email } = getCurrentAgent();
|
|
68
|
+
const { connection, request, email, agent } = getCurrentAgent();
|
|
69
|
+
if (agent === this) {
|
|
70
|
+
return method.apply(this, args);
|
|
71
|
+
}
|
|
59
72
|
return agentContext.run({ agent: this, connection, request, email }, () => {
|
|
60
73
|
return method.apply(this, args);
|
|
61
74
|
});
|
|
@@ -110,7 +123,10 @@ var _Agent = class _Agent extends Server {
|
|
|
110
123
|
{
|
|
111
124
|
displayMessage: `Schedule ${row.id} executed`,
|
|
112
125
|
id: nanoid(),
|
|
113
|
-
payload:
|
|
126
|
+
payload: {
|
|
127
|
+
callback: row.callback,
|
|
128
|
+
id: row.id
|
|
129
|
+
},
|
|
114
130
|
timestamp: Date.now(),
|
|
115
131
|
type: "schedule:execute"
|
|
116
132
|
},
|
|
@@ -190,7 +206,7 @@ var _Agent = class _Agent extends Server {
|
|
|
190
206
|
this.broadcast(
|
|
191
207
|
JSON.stringify({
|
|
192
208
|
mcp: this.getMcpServers(),
|
|
193
|
-
type: "cf_agent_mcp_servers"
|
|
209
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
194
210
|
})
|
|
195
211
|
);
|
|
196
212
|
return new Response("<script>window.close();</script>", {
|
|
@@ -242,10 +258,8 @@ var _Agent = class _Agent extends Server {
|
|
|
242
258
|
displayMessage: `RPC call to ${method}`,
|
|
243
259
|
id: nanoid(),
|
|
244
260
|
payload: {
|
|
245
|
-
args,
|
|
246
261
|
method,
|
|
247
|
-
streaming: metadata?.streaming
|
|
248
|
-
success: true
|
|
262
|
+
streaming: metadata?.streaming
|
|
249
263
|
},
|
|
250
264
|
timestamp: Date.now(),
|
|
251
265
|
type: "rpc"
|
|
@@ -257,7 +271,7 @@ var _Agent = class _Agent extends Server {
|
|
|
257
271
|
id,
|
|
258
272
|
result,
|
|
259
273
|
success: true,
|
|
260
|
-
type: "rpc"
|
|
274
|
+
type: "rpc" /* RPC */
|
|
261
275
|
};
|
|
262
276
|
connection.send(JSON.stringify(response));
|
|
263
277
|
} catch (e) {
|
|
@@ -265,7 +279,7 @@ var _Agent = class _Agent extends Server {
|
|
|
265
279
|
error: e instanceof Error ? e.message : "Unknown error occurred",
|
|
266
280
|
id: parsed.id,
|
|
267
281
|
success: false,
|
|
268
|
-
type: "rpc"
|
|
282
|
+
type: "rpc" /* RPC */
|
|
269
283
|
};
|
|
270
284
|
connection.send(JSON.stringify(response));
|
|
271
285
|
console.error("RPC error:", e);
|
|
@@ -280,41 +294,39 @@ var _Agent = class _Agent extends Server {
|
|
|
280
294
|
this.onConnect = (connection, ctx2) => {
|
|
281
295
|
return agentContext.run(
|
|
282
296
|
{ agent: this, connection, request: ctx2.request, email: void 0 },
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
if (this.state) {
|
|
286
|
-
connection.send(
|
|
287
|
-
JSON.stringify({
|
|
288
|
-
state: this.state,
|
|
289
|
-
type: "cf_agent_state"
|
|
290
|
-
})
|
|
291
|
-
);
|
|
292
|
-
}
|
|
297
|
+
() => {
|
|
298
|
+
if (this.state) {
|
|
293
299
|
connection.send(
|
|
294
300
|
JSON.stringify({
|
|
295
|
-
|
|
296
|
-
type: "
|
|
301
|
+
state: this.state,
|
|
302
|
+
type: "cf_agent_state" /* CF_AGENT_STATE */
|
|
297
303
|
})
|
|
298
304
|
);
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
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
|
|
308
318
|
},
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
319
|
+
timestamp: Date.now(),
|
|
320
|
+
type: "connect"
|
|
321
|
+
},
|
|
322
|
+
this.ctx
|
|
323
|
+
);
|
|
324
|
+
return this._tryCatch(() => _onConnect(connection, ctx2));
|
|
313
325
|
}
|
|
314
326
|
);
|
|
315
327
|
};
|
|
316
328
|
const _onStart = this.onStart.bind(this);
|
|
317
|
-
this.onStart = async () => {
|
|
329
|
+
this.onStart = async (props) => {
|
|
318
330
|
return agentContext.run(
|
|
319
331
|
{
|
|
320
332
|
agent: this,
|
|
@@ -323,13 +335,19 @@ var _Agent = class _Agent extends Server {
|
|
|
323
335
|
email: void 0
|
|
324
336
|
},
|
|
325
337
|
async () => {
|
|
326
|
-
|
|
338
|
+
await this._tryCatch(() => {
|
|
339
|
+
const servers = this.sql`
|
|
327
340
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
328
341
|
`;
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
342
|
+
this.broadcast(
|
|
343
|
+
JSON.stringify({
|
|
344
|
+
mcp: this.getMcpServers(),
|
|
345
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
346
|
+
})
|
|
347
|
+
);
|
|
348
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
349
|
+
servers.forEach((server) => {
|
|
350
|
+
this._connectToMcpServerInternal(
|
|
333
351
|
server.name,
|
|
334
352
|
server.server_url,
|
|
335
353
|
server.callback_url,
|
|
@@ -338,18 +356,29 @@ var _Agent = class _Agent extends Server {
|
|
|
338
356
|
id: server.id,
|
|
339
357
|
oauthClientId: server.client_id ?? void 0
|
|
340
358
|
}
|
|
341
|
-
)
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
})
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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);
|
|
381
|
+
});
|
|
353
382
|
}
|
|
354
383
|
);
|
|
355
384
|
};
|
|
@@ -400,7 +429,6 @@ var _Agent = class _Agent extends Server {
|
|
|
400
429
|
}
|
|
401
430
|
}
|
|
402
431
|
_setStateInternal(state, source = "server") {
|
|
403
|
-
const previousState = this._state;
|
|
404
432
|
this._state = state;
|
|
405
433
|
this.sql`
|
|
406
434
|
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
@@ -413,7 +441,7 @@ var _Agent = class _Agent extends Server {
|
|
|
413
441
|
this.broadcast(
|
|
414
442
|
JSON.stringify({
|
|
415
443
|
state,
|
|
416
|
-
type: "cf_agent_state"
|
|
444
|
+
type: "cf_agent_state" /* CF_AGENT_STATE */
|
|
417
445
|
}),
|
|
418
446
|
source !== "server" ? [source.id] : []
|
|
419
447
|
);
|
|
@@ -426,10 +454,7 @@ var _Agent = class _Agent extends Server {
|
|
|
426
454
|
{
|
|
427
455
|
displayMessage: "State updated",
|
|
428
456
|
id: nanoid(),
|
|
429
|
-
payload: {
|
|
430
|
-
previousState,
|
|
431
|
-
state
|
|
432
|
-
},
|
|
457
|
+
payload: {},
|
|
433
458
|
timestamp: Date.now(),
|
|
434
459
|
type: "state:update"
|
|
435
460
|
},
|
|
@@ -546,28 +571,22 @@ var _Agent = class _Agent extends Server {
|
|
|
546
571
|
while (proto && proto !== Object.prototype && depth < 10) {
|
|
547
572
|
const methodNames = Object.getOwnPropertyNames(proto);
|
|
548
573
|
for (const methodName of methodNames) {
|
|
549
|
-
|
|
574
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
575
|
+
if (baseMethods.has(methodName) || methodName.startsWith("_") || !descriptor || !!descriptor.get || typeof descriptor.value !== "function") {
|
|
550
576
|
continue;
|
|
551
577
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
wrappedFunction,
|
|
563
|
-
callableMetadata.get(
|
|
564
|
-
this[methodName]
|
|
565
|
-
)
|
|
566
|
-
);
|
|
567
|
-
}
|
|
568
|
-
this.constructor.prototype[methodName] = wrappedFunction;
|
|
569
|
-
}
|
|
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
|
+
);
|
|
570
588
|
}
|
|
589
|
+
this.constructor.prototype[methodName] = wrappedFunction;
|
|
571
590
|
}
|
|
572
591
|
proto = Object.getPrototypeOf(proto);
|
|
573
592
|
depth++;
|
|
@@ -714,7 +733,10 @@ var _Agent = class _Agent extends Server {
|
|
|
714
733
|
{
|
|
715
734
|
displayMessage: `Schedule ${schedule.id} created`,
|
|
716
735
|
id: nanoid(),
|
|
717
|
-
payload:
|
|
736
|
+
payload: {
|
|
737
|
+
callback,
|
|
738
|
+
id
|
|
739
|
+
},
|
|
718
740
|
timestamp: Date.now(),
|
|
719
741
|
type: "schedule:create"
|
|
720
742
|
},
|
|
@@ -849,7 +871,10 @@ var _Agent = class _Agent extends Server {
|
|
|
849
871
|
{
|
|
850
872
|
displayMessage: `Schedule ${id} cancelled`,
|
|
851
873
|
id: nanoid(),
|
|
852
|
-
payload:
|
|
874
|
+
payload: {
|
|
875
|
+
callback: schedule.callback,
|
|
876
|
+
id: schedule.id
|
|
877
|
+
},
|
|
853
878
|
timestamp: Date.now(),
|
|
854
879
|
type: "schedule:cancel"
|
|
855
880
|
},
|
|
@@ -935,7 +960,7 @@ var _Agent = class _Agent extends Server {
|
|
|
935
960
|
this.broadcast(
|
|
936
961
|
JSON.stringify({
|
|
937
962
|
mcp: this.getMcpServers(),
|
|
938
|
-
type: "cf_agent_mcp_servers"
|
|
963
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
939
964
|
})
|
|
940
965
|
);
|
|
941
966
|
return result;
|
|
@@ -988,7 +1013,7 @@ var _Agent = class _Agent extends Server {
|
|
|
988
1013
|
this.broadcast(
|
|
989
1014
|
JSON.stringify({
|
|
990
1015
|
mcp: this.getMcpServers(),
|
|
991
|
-
type: "cf_agent_mcp_servers"
|
|
1016
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
992
1017
|
})
|
|
993
1018
|
);
|
|
994
1019
|
}
|
|
@@ -1206,7 +1231,7 @@ var StreamingResponse = class {
|
|
|
1206
1231
|
id: this._id,
|
|
1207
1232
|
result: chunk,
|
|
1208
1233
|
success: true,
|
|
1209
|
-
type: "rpc"
|
|
1234
|
+
type: "rpc" /* RPC */
|
|
1210
1235
|
};
|
|
1211
1236
|
this._connection.send(JSON.stringify(response));
|
|
1212
1237
|
}
|
|
@@ -1224,7 +1249,7 @@ var StreamingResponse = class {
|
|
|
1224
1249
|
id: this._id,
|
|
1225
1250
|
result: finalChunk,
|
|
1226
1251
|
success: true,
|
|
1227
|
-
type: "rpc"
|
|
1252
|
+
type: "rpc" /* RPC */
|
|
1228
1253
|
};
|
|
1229
1254
|
this._connection.send(JSON.stringify(response));
|
|
1230
1255
|
}
|
|
@@ -1256,6 +1281,7 @@ function isLocalMode() {
|
|
|
1256
1281
|
|
|
1257
1282
|
export {
|
|
1258
1283
|
genericObservability,
|
|
1284
|
+
callable,
|
|
1259
1285
|
unstable_callable,
|
|
1260
1286
|
getCurrentAgent,
|
|
1261
1287
|
Agent,
|
|
@@ -1267,4 +1293,4 @@ export {
|
|
|
1267
1293
|
getAgentByName,
|
|
1268
1294
|
StreamingResponse
|
|
1269
1295
|
};
|
|
1270
|
-
//# sourceMappingURL=chunk-
|
|
1296
|
+
//# sourceMappingURL=chunk-YDUDMOL6.js.map
|