@opentiny/next-sdk 0.3.3 → 0.4.1
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/agent/AgentModelProvider.ts +78 -81
- package/agent/type.ts +6 -9
- package/agent/utils/getAISDKTools.ts +0 -1
- package/agent/utils/getBuiltinMcpTools.ts +7 -7
- package/core.ts +1 -3
- package/dist/SimulatorMask-BHVXyogh-iNUb4dyF.js +361 -0
- package/dist/agent/type.d.ts +4 -12
- package/dist/agent/utils/getBuiltinMcpTools.d.ts +3 -3
- package/dist/core.d.ts +1 -1
- package/dist/core.js +18 -18
- package/dist/index-CXK8CcKq.js +6076 -0
- package/dist/index.d.ts +2 -4
- package/dist/index.js +57 -2783
- package/dist/initialize-builtin-WebMCP-rALfqS-o.js +6389 -0
- package/dist/page-tools/a11y/build.d.ts +11 -0
- package/dist/page-tools/a11y/constants.d.ts +9 -0
- package/dist/page-tools/a11y/search.d.ts +17 -0
- package/dist/page-tools/a11y/types.d.ts +104 -0
- package/dist/page-tools/a11y/utils.d.ts +34 -0
- package/dist/page-tools/a11y/vnode.d.ts +37 -0
- package/dist/page-tools/a11y-tree.d.ts +12 -0
- package/dist/page-tools/bridge.d.ts +0 -6
- package/dist/page-tools/constants.d.ts +22 -0
- package/dist/page-tools/context.d.ts +28 -0
- package/dist/page-tools/handlers/browserState.d.ts +8 -0
- package/dist/page-tools/handlers/click.d.ts +8 -0
- package/dist/page-tools/handlers/executeJavascript.d.ts +8 -0
- package/dist/page-tools/handlers/fill.d.ts +8 -0
- package/dist/page-tools/handlers/scroll.d.ts +8 -0
- package/dist/page-tools/handlers/searchTree.d.ts +8 -0
- package/dist/page-tools/handlers/select.d.ts +8 -0
- package/dist/page-tools/initialize-builtin-WebMCP.d.ts +1 -0
- package/dist/page-tools/page-agent-highlight/index.d.ts +9 -0
- package/dist/page-tools/page-agent-mask/SimulatorMask.d.ts +14 -0
- package/dist/page-tools/page-agent-mask/checkDarkMode.d.ts +5 -0
- package/dist/page-tools/page-agent-tool.d.ts +4 -0
- package/dist/page-tools/page-state-cache.d.ts +36 -0
- package/dist/page-tools/schema.d.ts +43 -0
- package/dist/page-tools/utils/dom.d.ts +6 -0
- package/dist/page-tools/utils/scroll.d.ts +15 -0
- package/dist/runtime.d.ts +4 -0
- package/dist/runtime.js +744 -0
- package/dist/utils/builtinProxy.d.ts +1 -1
- package/index.ts +2 -19
- package/package.json +14 -18
- package/page-tools/a11y/build.ts +70 -0
- package/page-tools/a11y/constants.ts +110 -0
- package/page-tools/a11y/search.ts +129 -0
- package/page-tools/a11y/types.ts +108 -0
- package/page-tools/a11y/utils.ts +266 -0
- package/page-tools/a11y/vnode.ts +229 -0
- package/page-tools/a11y-tree.ts +13 -0
- package/page-tools/bridge.ts +49 -15
- package/page-tools/constants.ts +72 -0
- package/page-tools/context.ts +13 -0
- package/page-tools/handlers/browserState.ts +12 -0
- package/page-tools/handlers/click.ts +26 -0
- package/page-tools/handlers/executeJavascript.ts +22 -0
- package/page-tools/handlers/fill.ts +60 -0
- package/page-tools/handlers/scroll.ts +66 -0
- package/page-tools/handlers/searchTree.ts +30 -0
- package/page-tools/handlers/select.ts +34 -0
- package/page-tools/initialize-builtin-WebMCP.ts +20 -0
- package/page-tools/page-agent-highlight/index.ts +198 -0
- package/page-tools/page-agent-mask/SimulatorMask.module.css +14 -0
- package/page-tools/page-agent-mask/SimulatorMask.ts +279 -0
- package/page-tools/page-agent-mask/checkDarkMode.ts +181 -0
- package/page-tools/page-agent-mask/cursor-border.svg +3 -0
- package/page-tools/page-agent-mask/cursor-fill.svg +5 -0
- package/page-tools/page-agent-mask/cursor.module.css +70 -0
- package/page-tools/page-agent-prompt.md +139 -0
- package/page-tools/page-agent-tool.ts +175 -0
- package/page-tools/page-state-cache.ts +78 -0
- package/page-tools/schema.ts +51 -0
- package/page-tools/utils/dom.ts +155 -0
- package/page-tools/utils/scroll.ts +58 -0
- package/runtime.ts +13 -0
- package/utils/builtinProxy.ts +45 -13
- package/vite.config.runtime.ts +22 -0
- package/vite.config.ts +52 -8
- package/McpSdk.ts +0 -14
- package/WebAgent.ts +0 -5
- package/WebMcp.ts +0 -20
- package/Zod.ts +0 -1
- package/dist/AgentModelProvider-CwqOT28f.js +0 -4198
- package/dist/McpSdk.d.ts +0 -14
- package/dist/WebAgent.d.ts +0 -5
- package/dist/WebMcp.d.ts +0 -20
- package/dist/Zod.d.ts +0 -1
- package/dist/index.es.dev.js +0 -53427
- package/dist/index.es.js +0 -42147
- package/dist/index.umd.dev.js +0 -53820
- package/dist/index.umd.js +0 -814
- package/dist/mcpsdk@1.25.3.dev.js +0 -22621
- package/dist/mcpsdk@1.25.3.es.dev.js +0 -22619
- package/dist/mcpsdk@1.25.3.es.js +0 -16802
- package/dist/mcpsdk@1.25.3.js +0 -43
- package/dist/transport/ExtensionClientTransport.d.ts +0 -24
- package/dist/transport/ExtensionContentServerTransport.d.ts +0 -39
- package/dist/transport/ExtensionPageServerTransport.d.ts +0 -36
- package/dist/transport/messages.d.ts +0 -9
- package/dist/vite.config.mcpSdk.d.ts +0 -2
- package/dist/vite.config.webAgent.d.ts +0 -2
- package/dist/vite.config.webMcp.d.ts +0 -2
- package/dist/vite.config.webMcpFull.d.ts +0 -2
- package/dist/vite.config.zod.d.ts +0 -2
- package/dist/webagent.dev.js +0 -47597
- package/dist/webagent.es.dev.js +0 -47204
- package/dist/webagent.es.js +0 -37494
- package/dist/webagent.js +0 -634
- package/dist/webmcp-full.dev.js +0 -24430
- package/dist/webmcp-full.es.dev.js +0 -24426
- package/dist/webmcp-full.es.js +0 -18333
- package/dist/webmcp-full.js +0 -43
- package/dist/webmcp.dev.js +0 -1595
- package/dist/webmcp.es.dev.js +0 -1589
- package/dist/webmcp.es.js +0 -1406
- package/dist/webmcp.js +0 -1
- package/dist/zod@3.25.76.dev.js +0 -4037
- package/dist/zod@3.25.76.es.dev.js +0 -4033
- package/dist/zod@3.25.76.es.js +0 -2945
- package/dist/zod@3.25.76.js +0 -1
- package/transport/ExtensionClientTransport.ts +0 -100
- package/transport/ExtensionContentServerTransport.ts +0 -162
- package/transport/ExtensionPageServerTransport.ts +0 -149
- package/transport/messages.ts +0 -63
- package/vite-build-tsc.ts +0 -63
- package/vite-env.d.ts +0 -5
- package/vite.config.mcpSdk.ts +0 -28
- package/vite.config.webAgent.ts +0 -19
- package/vite.config.webMcp.ts +0 -40
- package/vite.config.webMcpFull.ts +0 -19
- package/vite.config.zod.ts +0 -23
- /package/dist/{vite-build-tsc.d.ts → vite.config.runtime.d.ts} +0 -0
|
@@ -1,4198 +0,0 @@
|
|
|
1
|
-
import { MessageChannelServerTransport as ge, createTransportPair as ht, MessageChannelClientTransport as ye, createSseProxy as mt, createSocketProxy as ft, createStreamProxy as gt, sseOptions as yt, streamOptions as vt, MessageChannelTransport as wt } from "@opentiny/next";
|
|
2
|
-
import { McpServer as He } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
-
import { SetLevelRequestSchema as Ce, SubscribeRequestSchema as _t, UnsubscribeRequestSchema as bt, ListResourcesRequestSchema as St, RootsListChangedNotificationSchema as Tt, CallToolResultSchema as Ct, ElicitRequestSchema as Pt, CreateMessageRequestSchema as Rt, ListRootsRequestSchema as Mt, ToolListChangedNotificationSchema as Et, PromptListChangedNotificationSchema as At, ResourceListChangedNotificationSchema as kt, ResourceUpdatedNotificationSchema as Ot, LoggingMessageNotificationSchema as xt, JSONRPCMessageSchema as It } from "@modelcontextprotocol/sdk/types.js";
|
|
4
|
-
import { dynamicTool as Fe, jsonSchema as Ze, generateText as Pe, streamText as ie, stepCountIs as jt } from "ai";
|
|
5
|
-
import { TypeValidationError as de, JSONParseError as Re, InvalidArgumentError as Lt, AISDKError as K } from "@ai-sdk/provider";
|
|
6
|
-
import * as Me from "zod/v4";
|
|
7
|
-
import { z as o } from "zod/v4";
|
|
8
|
-
import { ZodFirstPartyTypeKind as w } from "zod/v3";
|
|
9
|
-
import { StreamableHTTPClientTransport as se } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
10
|
-
import { SSEClientTransport as ne } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
11
|
-
import { InMemoryTransport as Ut } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
12
|
-
import { createOpenAI as $t } from "@ai-sdk/openai";
|
|
13
|
-
import { createDeepSeek as Nt } from "@ai-sdk/deepseek";
|
|
14
|
-
import { Client as De } from "@modelcontextprotocol/sdk/client/index.js";
|
|
15
|
-
import { WebSocketClientTransport as qt } from "@modelcontextprotocol/sdk/client/websocket.js";
|
|
16
|
-
class Tn {
|
|
17
|
-
constructor(e, r) {
|
|
18
|
-
const s = {
|
|
19
|
-
name: "web-mcp-server",
|
|
20
|
-
version: "1.0.0"
|
|
21
|
-
}, n = {
|
|
22
|
-
prompts: { listChanged: !0 },
|
|
23
|
-
resources: { subscribe: !0, listChanged: !0 },
|
|
24
|
-
tools: { listChanged: !0 },
|
|
25
|
-
completions: {},
|
|
26
|
-
logging: {}
|
|
27
|
-
};
|
|
28
|
-
this.server = new He(e || s, r || { capabilities: n }), this.server.server.oninitialized = () => {
|
|
29
|
-
this.oninitialized?.();
|
|
30
|
-
}, this.server.server.onclose = () => {
|
|
31
|
-
this.onclose?.();
|
|
32
|
-
}, this.server.server.onerror = (a) => {
|
|
33
|
-
this.onerror?.(a);
|
|
34
|
-
}, this.server.server.setRequestHandler(Ce, async () => ({}));
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Connects the server to a transport via the specified option.
|
|
38
|
-
*/
|
|
39
|
-
async connect(e) {
|
|
40
|
-
return typeof e.start == "function" ? (this.transport = e, this.transport.onclose = void 0, this.transport.onerror = void 0, this.transport.onmessage = void 0) : (this.transport = new ge(e), await this.transport.listen()), await this.server.connect(this.transport), this.transport;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Closes the connection.
|
|
44
|
-
*/
|
|
45
|
-
async close() {
|
|
46
|
-
await this.server.close();
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Registers a tool with a config object and callback.
|
|
50
|
-
*/
|
|
51
|
-
registerTool(e, r, s) {
|
|
52
|
-
return this.server.registerTool(e, r, s);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Registers a prompt with a config object and callback.
|
|
56
|
-
*/
|
|
57
|
-
registerPrompt(e, r, s) {
|
|
58
|
-
return this.server.registerPrompt(e, r, s);
|
|
59
|
-
}
|
|
60
|
-
registerResource(e, r, s, n) {
|
|
61
|
-
return typeof r == "string" ? this.server.registerResource(e, r, s, n) : this.server.registerResource(e, r, s, n);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Checks if the server is connected to a transport.
|
|
65
|
-
* @returns True if the server is connected
|
|
66
|
-
*/
|
|
67
|
-
isConnected() {
|
|
68
|
-
return this.server.isConnected();
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Sends a resource list changed event to the client, if connected.
|
|
72
|
-
*/
|
|
73
|
-
sendResourceListChanged() {
|
|
74
|
-
this.server.sendResourceListChanged();
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Sends a tool list changed event to the client, if connected.
|
|
78
|
-
*/
|
|
79
|
-
sendToolListChanged() {
|
|
80
|
-
this.server.sendToolListChanged();
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Sends a prompt list changed event to the client, if connected.
|
|
84
|
-
*/
|
|
85
|
-
sendPromptListChanged() {
|
|
86
|
-
this.server.sendPromptListChanged();
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* After initialization has completed, this will be populated with the client's reported capabilities.
|
|
90
|
-
*/
|
|
91
|
-
getClientCapabilities() {
|
|
92
|
-
return this.server.server.getClientCapabilities();
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* After initialization has completed, this will be populated with information about the client's name and version.
|
|
96
|
-
*/
|
|
97
|
-
getClientVersion() {
|
|
98
|
-
return this.server.server.getClientVersion();
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Sends a ping to the client to check if it is still connected.
|
|
102
|
-
*/
|
|
103
|
-
async ping() {
|
|
104
|
-
return await this.server.server.ping();
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Creates a LLM message to be sent to the client.
|
|
108
|
-
*/
|
|
109
|
-
async createMessage(e, r) {
|
|
110
|
-
return await this.server.server.createMessage(e, r);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Elicits input from the client, such as a prompt or resource.
|
|
114
|
-
*/
|
|
115
|
-
async elicitInput(e, r) {
|
|
116
|
-
return await this.server.server.elicitInput(e, r);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Lists the root resources available to the client.
|
|
120
|
-
*/
|
|
121
|
-
async listRoots(e, r) {
|
|
122
|
-
return await this.server.server.listRoots(e, r);
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Sends a logging message to the client.
|
|
126
|
-
*/
|
|
127
|
-
async sendLoggingMessage(e) {
|
|
128
|
-
return await this.server.server.sendLoggingMessage(e);
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Sends a resource updated notification to the client.
|
|
132
|
-
*/
|
|
133
|
-
async sendResourceUpdated(e) {
|
|
134
|
-
return await this.server.server.sendResourceUpdated(e);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Sends a request and wait for a response.
|
|
138
|
-
*
|
|
139
|
-
* Do not use this method to emit notifications! Use notification() instead.
|
|
140
|
-
*/
|
|
141
|
-
request(e, r, s) {
|
|
142
|
-
return this.server.server.request(e, r, s);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Emits a notification, which is a one-way message that does not expect a response.
|
|
146
|
-
*/
|
|
147
|
-
async notification(e, r) {
|
|
148
|
-
return await this.server.server.notification(e, r);
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Registers a handler to invoke when this protocol object receives a request with the given method.
|
|
152
|
-
*
|
|
153
|
-
* Note that this will replace any previous request handler for the same method.
|
|
154
|
-
*/
|
|
155
|
-
setRequestHandler(e, r) {
|
|
156
|
-
this.server.server.setRequestHandler(e, r);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Removes the request handler for the given method.
|
|
160
|
-
*/
|
|
161
|
-
removeRequestHandler(e) {
|
|
162
|
-
this.server.server.removeRequestHandler(e);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Registers a handler to invoke when this protocol object receives a notification with the given method.
|
|
166
|
-
*
|
|
167
|
-
* Note that this will replace any previous notification handler for the same method.
|
|
168
|
-
*/
|
|
169
|
-
setNotificationHandler(e, r) {
|
|
170
|
-
this.server.server.setNotificationHandler(e, r);
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Removes the notification handler for the given method.
|
|
174
|
-
*/
|
|
175
|
-
removeNotificationHandler(e) {
|
|
176
|
-
this.server.server.removeNotificationHandler(e);
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Registers a handler for the subscribe request.
|
|
180
|
-
*/
|
|
181
|
-
onSubscribe(e) {
|
|
182
|
-
this.server.server.setRequestHandler(_t, e);
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Registers a handler for the unsubscribe request.
|
|
186
|
-
*/
|
|
187
|
-
onUnsubscribe(e) {
|
|
188
|
-
this.server.server.setRequestHandler(bt, e);
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Registers a handler for the set log level request.
|
|
192
|
-
*/
|
|
193
|
-
onSetLogLevel(e) {
|
|
194
|
-
this.server.server.setRequestHandler(Ce, e);
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Registers a handler for the list tools request.
|
|
198
|
-
*/
|
|
199
|
-
onListResources(e) {
|
|
200
|
-
this.server.server.setRequestHandler(St, e);
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Registers a handler for the roots list changed notification.
|
|
204
|
-
*/
|
|
205
|
-
onRootsListChanged(e) {
|
|
206
|
-
this.server.server.setNotificationHandler(Tt, e);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Close the transport for window.addEventListener('pagehide')
|
|
210
|
-
*/
|
|
211
|
-
async onPagehide(e) {
|
|
212
|
-
e.persisted || this.transport && typeof this.transport.close == "function" && await this.transport.close();
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
const Cn = (t, e) => new ge(t, e), Pn = () => ht(), Rn = (t) => t instanceof ge, Mn = (t) => t instanceof He, Ht = (t) => {
|
|
216
|
-
const e = () => typeof navigator > "u" ? null : navigator.modelContextTesting || null;
|
|
217
|
-
t.onmessage = async (r) => {
|
|
218
|
-
if (!r || typeof r != "object") return;
|
|
219
|
-
const s = r.id, n = r.method;
|
|
220
|
-
try {
|
|
221
|
-
if (n === "initialize")
|
|
222
|
-
await t.send({
|
|
223
|
-
jsonrpc: "2.0",
|
|
224
|
-
id: s,
|
|
225
|
-
result: {
|
|
226
|
-
protocolVersion: "2024-11-05",
|
|
227
|
-
capabilities: {
|
|
228
|
-
tools: { listChanged: !0 },
|
|
229
|
-
logging: {}
|
|
230
|
-
},
|
|
231
|
-
serverInfo: { name: "browser-builtin-webmcp-proxy", version: "1.0.0" }
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
else if (n !== "notifications/initialized")
|
|
235
|
-
if (n === "ping" || n === "custom_ping")
|
|
236
|
-
await t.send({ jsonrpc: "2.0", id: s, result: {} });
|
|
237
|
-
else if (n === "tools/list") {
|
|
238
|
-
const a = e();
|
|
239
|
-
if (a && a.listTools) {
|
|
240
|
-
const u = (await a.listTools()).map((c) => {
|
|
241
|
-
let l = {};
|
|
242
|
-
if (typeof c.inputSchema == "string")
|
|
243
|
-
try {
|
|
244
|
-
l = JSON.parse(c.inputSchema);
|
|
245
|
-
} catch (p) {
|
|
246
|
-
console.error("Failed to parse inputSchema:", p);
|
|
247
|
-
}
|
|
248
|
-
else typeof c.inputSchema == "object" && c.inputSchema !== null && (l = c.inputSchema);
|
|
249
|
-
return {
|
|
250
|
-
name: c.name,
|
|
251
|
-
description: c.description || "",
|
|
252
|
-
inputSchema: {
|
|
253
|
-
type: "object",
|
|
254
|
-
properties: l.properties || {},
|
|
255
|
-
required: l.required || []
|
|
256
|
-
}
|
|
257
|
-
};
|
|
258
|
-
});
|
|
259
|
-
await t.send({ jsonrpc: "2.0", id: s, result: { tools: u } });
|
|
260
|
-
} else
|
|
261
|
-
await t.send({ jsonrpc: "2.0", id: s, result: { tools: [] } });
|
|
262
|
-
} else if (n === "tools/call") {
|
|
263
|
-
const a = e();
|
|
264
|
-
if (a && a.executeTool) {
|
|
265
|
-
if (!r.params || typeof r.params != "object" || !r.params.name) {
|
|
266
|
-
const p = new Error('Invalid params: "name" is required and params must be an object');
|
|
267
|
-
throw p.code = -32602, p;
|
|
268
|
-
}
|
|
269
|
-
const { name: i, arguments: u } = r.params, c = await a.executeTool(i, JSON.stringify(u || {})), l = c && typeof c == "object" && "content" in c ? c : { content: [{ type: "text", text: typeof c == "string" ? c : JSON.stringify(c) }] };
|
|
270
|
-
await t.send({ jsonrpc: "2.0", id: s, result: l });
|
|
271
|
-
} else {
|
|
272
|
-
const i = new Error("executeTool not implemented in Browser built-in WebMCP");
|
|
273
|
-
throw i.code = -32601, i;
|
|
274
|
-
}
|
|
275
|
-
} else n === "logging/setLevel" ? await t.send({ jsonrpc: "2.0", id: s, result: {} }) : n === "prompts/list" ? await t.send({ jsonrpc: "2.0", id: s, result: { prompts: [] } }) : n === "resources/list" ? await t.send({ jsonrpc: "2.0", id: s, result: { resources: [] } }) : s !== void 0 && await t.send({
|
|
276
|
-
jsonrpc: "2.0",
|
|
277
|
-
id: s,
|
|
278
|
-
error: { code: -32601, message: `Method not found: ${n}` }
|
|
279
|
-
});
|
|
280
|
-
} catch (a) {
|
|
281
|
-
s !== void 0 && await t.send({
|
|
282
|
-
jsonrpc: "2.0",
|
|
283
|
-
id: s,
|
|
284
|
-
error: { code: a.code || -32e3, message: a.message || String(a) }
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
};
|
|
289
|
-
class Ft {
|
|
290
|
-
constructor(e, r) {
|
|
291
|
-
const s = {
|
|
292
|
-
name: "web-mcp-client",
|
|
293
|
-
version: "1.0.0"
|
|
294
|
-
}, n = {
|
|
295
|
-
roots: { listChanged: !0 },
|
|
296
|
-
sampling: {},
|
|
297
|
-
elicitation: {}
|
|
298
|
-
};
|
|
299
|
-
this.client = new De(e || s, r || { capabilities: n }), this.client.onclose = () => {
|
|
300
|
-
this.onclose?.();
|
|
301
|
-
}, this.client.onerror = (a) => {
|
|
302
|
-
this.onerror?.(a);
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Connects the client to a transport via the specified option.
|
|
307
|
-
*/
|
|
308
|
-
async connect(e) {
|
|
309
|
-
if (typeof e.start == "function")
|
|
310
|
-
return this.transport = e, this.transport.onclose = void 0, this.transport.onerror = void 0, this.transport.onmessage = void 0, await this.client.connect(this.transport), { transport: this.transport, sessionId: this.transport.sessionId };
|
|
311
|
-
const { url: r, token: s, sessionId: n, type: a, agent: i, builtin: u, onError: c } = e;
|
|
312
|
-
if (i) {
|
|
313
|
-
const m = { client: this.client, url: r, token: s, sessionId: n }, d = a === "sse" ? await mt(m) : a === "socket" ? await ft(m) : await gt(m);
|
|
314
|
-
return d.transport.onerror = async (f) => {
|
|
315
|
-
c?.(f);
|
|
316
|
-
}, u && Ht(d.transport), d;
|
|
317
|
-
}
|
|
318
|
-
const l = new URL(r);
|
|
319
|
-
let p;
|
|
320
|
-
if (a === "channel" && (p = new ye(r), await this.client.connect(p)), a === "sse") {
|
|
321
|
-
const m = yt(s, n);
|
|
322
|
-
p = new ne(l, m), await this.client.connect(p);
|
|
323
|
-
}
|
|
324
|
-
if (a === "socket" && (p = new qt(new URL(`${r}?sessionId=${n}&token=${s}`)), p.sessionId = n, await this.client.connect(p)), a === "stream" || typeof p > "u") {
|
|
325
|
-
const m = vt(s, n);
|
|
326
|
-
p = new se(l, m), await this.client.connect(p);
|
|
327
|
-
}
|
|
328
|
-
return this.transport = p, { transport: this.transport, sessionId: this.transport.sessionId };
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Closes the connection.
|
|
332
|
-
*/
|
|
333
|
-
async close() {
|
|
334
|
-
await this.client.close();
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* After initialization has completed, this will be populated with the server's reported capabilities.
|
|
338
|
-
*/
|
|
339
|
-
getServerCapabilities() {
|
|
340
|
-
return this.client.getServerCapabilities();
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* After initialization has completed, this will be populated with information about the server's name and version.
|
|
344
|
-
*/
|
|
345
|
-
getServerVersion() {
|
|
346
|
-
return this.client.getServerVersion();
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* After initialization has completed, this may be populated with information about the server's instructions.
|
|
350
|
-
*/
|
|
351
|
-
getInstructions() {
|
|
352
|
-
return this.client.getInstructions();
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Sends a ping to the server to check if it is still connected.
|
|
356
|
-
*/
|
|
357
|
-
async ping(e) {
|
|
358
|
-
return await this.client.ping(e);
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Sends a completion request to the server.
|
|
362
|
-
*/
|
|
363
|
-
async complete(e, r) {
|
|
364
|
-
return await this.client.complete(e, r);
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* Sends a request for setting the logging level to the server.
|
|
368
|
-
*/
|
|
369
|
-
async setLoggingLevel(e, r) {
|
|
370
|
-
return await this.client.setLoggingLevel(e, r);
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Gets the prompt with the given params from the server.
|
|
374
|
-
*/
|
|
375
|
-
async getPrompt(e, r) {
|
|
376
|
-
return await this.client.getPrompt(e, r);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Lists all prompts available on the server.
|
|
380
|
-
*/
|
|
381
|
-
async listPrompts(e, r) {
|
|
382
|
-
return await this.client.listPrompts(e, r);
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* Lists all resources available on the server.
|
|
386
|
-
*/
|
|
387
|
-
async listResources(e, r) {
|
|
388
|
-
return await this.client.listResources(e, r);
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Lists all resource templates available on the server.
|
|
392
|
-
*/
|
|
393
|
-
async listResourceTemplates(e, r) {
|
|
394
|
-
return await this.client.listResourceTemplates(e, r);
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Reads the resource with the given params from the server.
|
|
398
|
-
*/
|
|
399
|
-
async readResource(e, r) {
|
|
400
|
-
return await this.client.readResource(e, r);
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Subscribes to a resource on the server.
|
|
404
|
-
*/
|
|
405
|
-
async subscribeResource(e, r) {
|
|
406
|
-
return await this.client.subscribeResource(e, r);
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Unsubscribes from a resource on the server.
|
|
410
|
-
*/
|
|
411
|
-
async unsubscribeResource(e, r) {
|
|
412
|
-
return await this.client.unsubscribeResource(e, r);
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Calls a tool on the server with the given parameters.
|
|
416
|
-
*/
|
|
417
|
-
async callTool(e, r) {
|
|
418
|
-
return await this.client.callTool(e, Ct, r);
|
|
419
|
-
}
|
|
420
|
-
/**
|
|
421
|
-
* Lists all tools available on the server.
|
|
422
|
-
*/
|
|
423
|
-
async listTools(e, r) {
|
|
424
|
-
return await this.client.listTools(e, r);
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* Sends a notification for the roots list changed event to the server.
|
|
428
|
-
*/
|
|
429
|
-
async sendRootsListChanged() {
|
|
430
|
-
return await this.client.sendRootsListChanged();
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Sends a request and wait for a response.
|
|
434
|
-
*
|
|
435
|
-
* Do not use this method to emit notifications! Use notification() instead.
|
|
436
|
-
*/
|
|
437
|
-
request(e, r, s) {
|
|
438
|
-
return this.client.request(e, r, s);
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Emits a notification, which is a one-way message that does not expect a response.
|
|
442
|
-
*/
|
|
443
|
-
async notification(e, r) {
|
|
444
|
-
return await this.client.notification(e, r);
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Registers a handler to invoke when this protocol object receives a request with the given method.
|
|
448
|
-
*
|
|
449
|
-
* Note that this will replace any previous request handler for the same method.
|
|
450
|
-
*/
|
|
451
|
-
setRequestHandler(e, r) {
|
|
452
|
-
this.client.setRequestHandler(e, r);
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Removes the request handler for the given method.
|
|
456
|
-
*/
|
|
457
|
-
removeRequestHandler(e) {
|
|
458
|
-
this.client.removeRequestHandler(e);
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Registers a handler to invoke when this protocol object receives a notification with the given method.
|
|
462
|
-
*
|
|
463
|
-
* Note that this will replace any previous notification handler for the same method.
|
|
464
|
-
*/
|
|
465
|
-
setNotificationHandler(e, r) {
|
|
466
|
-
this.client.setNotificationHandler(e, r);
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* Removes the notification handler for the given method.
|
|
470
|
-
*/
|
|
471
|
-
removeNotificationHandler(e) {
|
|
472
|
-
this.client.removeNotificationHandler(e);
|
|
473
|
-
}
|
|
474
|
-
/**
|
|
475
|
-
* Registers a handler for the elicitation request.
|
|
476
|
-
*/
|
|
477
|
-
onElicit(e) {
|
|
478
|
-
this.client.setRequestHandler(Pt, e);
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* Registers a handler for the create LLM message request.
|
|
482
|
-
*/
|
|
483
|
-
onCreateMessage(e) {
|
|
484
|
-
this.client.setRequestHandler(Rt, e);
|
|
485
|
-
}
|
|
486
|
-
/**
|
|
487
|
-
* Registers a handler for the list roots request.
|
|
488
|
-
*/
|
|
489
|
-
onListRoots(e) {
|
|
490
|
-
this.client.setRequestHandler(Mt, e);
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
* Registers a handler for the tool list changed notification.
|
|
494
|
-
*/
|
|
495
|
-
onToolListChanged(e) {
|
|
496
|
-
this.client.setNotificationHandler(Et, e);
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* Registers a handler for the prompt list changed notification.
|
|
500
|
-
*/
|
|
501
|
-
onPromptListChanged(e) {
|
|
502
|
-
this.client.setNotificationHandler(At, e);
|
|
503
|
-
}
|
|
504
|
-
/**
|
|
505
|
-
* Registers a handler for the resource list changed notification.
|
|
506
|
-
*/
|
|
507
|
-
onResourceListChanged(e) {
|
|
508
|
-
this.client.setNotificationHandler(kt, e);
|
|
509
|
-
}
|
|
510
|
-
/**
|
|
511
|
-
* Registers a handler for the resource updated notification.
|
|
512
|
-
*/
|
|
513
|
-
onResourceUpdated(e) {
|
|
514
|
-
this.client.setNotificationHandler(Ot, e);
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* Registers a handler for the logging message notification.
|
|
518
|
-
*/
|
|
519
|
-
onLoggingMessage(e) {
|
|
520
|
-
this.client.setNotificationHandler(xt, e);
|
|
521
|
-
}
|
|
522
|
-
/**
|
|
523
|
-
* Close the transport for window.addEventListener('pagehide')
|
|
524
|
-
*/
|
|
525
|
-
async onPagehide(e) {
|
|
526
|
-
e.persisted || (Zt(this.transport) ? await this.transport.terminateSession() : this.transport && typeof this.transport.close == "function" && await this.transport.close());
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
const En = (t, e) => new ne(t, e), An = (t, e) => new se(t, e), kn = (t, e) => new ye(t, e), On = (t) => t instanceof ne, Zt = (t) => t instanceof se, xn = (t) => t instanceof ye, In = (t) => t instanceof De, jn = (t, e, r) => {
|
|
530
|
-
window.postMessage({ type: t, direction: r, data: e }, "*");
|
|
531
|
-
}, Ln = (t, e, r) => {
|
|
532
|
-
const s = async function(n) {
|
|
533
|
-
n.source === window && n.data.type === t && n.data.direction === r && await e(n.data.data);
|
|
534
|
-
};
|
|
535
|
-
return window.addEventListener("message", s), () => window.removeEventListener("message", s);
|
|
536
|
-
}, Dt = (t, e, r) => {
|
|
537
|
-
if (r.endsWith("content"))
|
|
538
|
-
chrome.tabs.query({}, (s) => {
|
|
539
|
-
s.forEach((n) => {
|
|
540
|
-
chrome.tabs.sendMessage(n.id, { type: t, data: e, direction: r, tabId: n.id });
|
|
541
|
-
});
|
|
542
|
-
});
|
|
543
|
-
else
|
|
544
|
-
return chrome.runtime.sendMessage({ direction: r, type: t, data: e });
|
|
545
|
-
}, zt = (t, e, r, s) => {
|
|
546
|
-
const n = (a, i, u) => {
|
|
547
|
-
if (a.type === t && a.direction === r && (!s || s && a.tabId === s)) {
|
|
548
|
-
const { data: c } = a;
|
|
549
|
-
e(c, i, u), u(i);
|
|
550
|
-
}
|
|
551
|
-
};
|
|
552
|
-
return chrome.runtime.onMessage.addListener(n), () => chrome.runtime.onMessage.removeListener(n);
|
|
553
|
-
};
|
|
554
|
-
class Vt {
|
|
555
|
-
constructor(e) {
|
|
556
|
-
this._isStarted = !1, this._isClosed = !1, this.targetSessionId = e, this._messageListener = zt(
|
|
557
|
-
"mcp-server-to-client",
|
|
558
|
-
(r) => {
|
|
559
|
-
try {
|
|
560
|
-
if (r.sessionId !== this.targetSessionId) return;
|
|
561
|
-
const s = It.parse(r.mcpMessage);
|
|
562
|
-
this.onmessage?.(s);
|
|
563
|
-
} catch (s) {
|
|
564
|
-
console.log("【Client Transport】处理server消息错误:", s);
|
|
565
|
-
}
|
|
566
|
-
},
|
|
567
|
-
"content->bg"
|
|
568
|
-
);
|
|
569
|
-
}
|
|
570
|
-
// 是否已关闭
|
|
571
|
-
_throwError(e, r) {
|
|
572
|
-
if (e()) {
|
|
573
|
-
const s = new Error(r);
|
|
574
|
-
throw console.log(r, s), this.onerror && this.onerror(s), s;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
/** 启动 transport,开始监听消息 */
|
|
578
|
-
async start() {
|
|
579
|
-
this._throwError(() => this._isClosed, "【Client Transport】 未启动,无法重新启动"), this._isStarted = !0;
|
|
580
|
-
}
|
|
581
|
-
/** 发送消息到 MCP Server */
|
|
582
|
-
async send(e, r) {
|
|
583
|
-
this._throwError(() => !this._isStarted, "【Client Transport】 未启动,无法发送消息"), this._throwError(() => this._isClosed, "【Client Transport】 已关闭,无法发送消息");
|
|
584
|
-
let s;
|
|
585
|
-
if (chrome.sessionRegistry) {
|
|
586
|
-
const n = chrome.sessionRegistry.get(this.targetSessionId);
|
|
587
|
-
n && n.tabIds.length > 0 && (s = n.tabIds[n.tabIds.length - 1]);
|
|
588
|
-
}
|
|
589
|
-
s == null && (s = await chrome.runtime.sendMessage({ type: "get-session-tab-id", sessionId: this.targetSessionId })), this._throwError(() => s == null, `【Client Transport】后台未找到活动的tabId用于${this.targetSessionId}`), Dt(
|
|
590
|
-
"mcp-client-to-server",
|
|
591
|
-
{ sessionId: this.targetSessionId, tabId: s, mcpMessage: e },
|
|
592
|
-
"bg->content"
|
|
593
|
-
);
|
|
594
|
-
}
|
|
595
|
-
/** 关闭 transport */
|
|
596
|
-
async close() {
|
|
597
|
-
if (!this._isClosed)
|
|
598
|
-
try {
|
|
599
|
-
this._isClosed = !0, this._isStarted = !1, this._messageListener && this._messageListener(), this.onclose && this.onclose();
|
|
600
|
-
} catch {
|
|
601
|
-
this._throwError(() => !0, "【Client Transport】 关闭时发生错误");
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
class Ee extends Error {
|
|
606
|
-
constructor(e, r) {
|
|
607
|
-
super(e), this.name = "ParseError", this.type = r.type, this.field = r.field, this.value = r.value, this.line = r.line;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
function ce(t) {
|
|
611
|
-
}
|
|
612
|
-
function Jt(t) {
|
|
613
|
-
if (typeof t == "function")
|
|
614
|
-
throw new TypeError(
|
|
615
|
-
"`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
|
|
616
|
-
);
|
|
617
|
-
const { onEvent: e = ce, onError: r = ce, onRetry: s = ce, onComment: n } = t;
|
|
618
|
-
let a = "", i = !0, u, c = "", l = "";
|
|
619
|
-
function p(h) {
|
|
620
|
-
const g = i ? h.replace(/^\xEF\xBB\xBF/, "") : h, [v, b] = Wt(`${a}${g}`);
|
|
621
|
-
for (const S of v)
|
|
622
|
-
m(S);
|
|
623
|
-
a = b, i = !1;
|
|
624
|
-
}
|
|
625
|
-
function m(h) {
|
|
626
|
-
if (h === "") {
|
|
627
|
-
f();
|
|
628
|
-
return;
|
|
629
|
-
}
|
|
630
|
-
if (h.startsWith(":")) {
|
|
631
|
-
n && n(h.slice(h.startsWith(": ") ? 2 : 1));
|
|
632
|
-
return;
|
|
633
|
-
}
|
|
634
|
-
const g = h.indexOf(":");
|
|
635
|
-
if (g !== -1) {
|
|
636
|
-
const v = h.slice(0, g), b = h[g + 1] === " " ? 2 : 1, S = h.slice(g + b);
|
|
637
|
-
d(v, S, h);
|
|
638
|
-
return;
|
|
639
|
-
}
|
|
640
|
-
d(h, "", h);
|
|
641
|
-
}
|
|
642
|
-
function d(h, g, v) {
|
|
643
|
-
switch (h) {
|
|
644
|
-
case "event":
|
|
645
|
-
l = g;
|
|
646
|
-
break;
|
|
647
|
-
case "data":
|
|
648
|
-
c = `${c}${g}
|
|
649
|
-
`;
|
|
650
|
-
break;
|
|
651
|
-
case "id":
|
|
652
|
-
u = g.includes("\0") ? void 0 : g;
|
|
653
|
-
break;
|
|
654
|
-
case "retry":
|
|
655
|
-
/^\d+$/.test(g) ? s(parseInt(g, 10)) : r(
|
|
656
|
-
new Ee(`Invalid \`retry\` value: "${g}"`, {
|
|
657
|
-
type: "invalid-retry",
|
|
658
|
-
value: g,
|
|
659
|
-
line: v
|
|
660
|
-
})
|
|
661
|
-
);
|
|
662
|
-
break;
|
|
663
|
-
default:
|
|
664
|
-
r(
|
|
665
|
-
new Ee(
|
|
666
|
-
`Unknown field "${h.length > 20 ? `${h.slice(0, 20)}…` : h}"`,
|
|
667
|
-
{ type: "unknown-field", field: h, value: g, line: v }
|
|
668
|
-
)
|
|
669
|
-
);
|
|
670
|
-
break;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
function f() {
|
|
674
|
-
c.length > 0 && e({
|
|
675
|
-
id: u,
|
|
676
|
-
event: l || void 0,
|
|
677
|
-
// If the data buffer's last character is a U+000A LINE FEED (LF) character,
|
|
678
|
-
// then remove the last character from the data buffer.
|
|
679
|
-
data: c.endsWith(`
|
|
680
|
-
`) ? c.slice(0, -1) : c
|
|
681
|
-
}), u = void 0, c = "", l = "";
|
|
682
|
-
}
|
|
683
|
-
function y(h = {}) {
|
|
684
|
-
a && h.consume && m(a), i = !0, u = void 0, c = "", l = "", a = "";
|
|
685
|
-
}
|
|
686
|
-
return { feed: p, reset: y };
|
|
687
|
-
}
|
|
688
|
-
function Wt(t) {
|
|
689
|
-
const e = [];
|
|
690
|
-
let r = "", s = 0;
|
|
691
|
-
for (; s < t.length; ) {
|
|
692
|
-
const n = t.indexOf("\r", s), a = t.indexOf(`
|
|
693
|
-
`, s);
|
|
694
|
-
let i = -1;
|
|
695
|
-
if (n !== -1 && a !== -1 ? i = Math.min(n, a) : n !== -1 ? n === t.length - 1 ? i = -1 : i = n : a !== -1 && (i = a), i === -1) {
|
|
696
|
-
r = t.slice(s);
|
|
697
|
-
break;
|
|
698
|
-
} else {
|
|
699
|
-
const u = t.slice(s, i);
|
|
700
|
-
e.push(u), s = i + 1, t[s - 1] === "\r" && t[s] === `
|
|
701
|
-
` && s++;
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
return [e, r];
|
|
705
|
-
}
|
|
706
|
-
class he extends TransformStream {
|
|
707
|
-
constructor({ onError: e, onRetry: r, onComment: s } = {}) {
|
|
708
|
-
let n;
|
|
709
|
-
super({
|
|
710
|
-
start(a) {
|
|
711
|
-
n = Jt({
|
|
712
|
-
onEvent: (i) => {
|
|
713
|
-
a.enqueue(i);
|
|
714
|
-
},
|
|
715
|
-
onError(i) {
|
|
716
|
-
e === "terminate" ? a.error(i) : typeof e == "function" && e(i);
|
|
717
|
-
},
|
|
718
|
-
onRetry: r,
|
|
719
|
-
onComment: s
|
|
720
|
-
});
|
|
721
|
-
},
|
|
722
|
-
transform(a) {
|
|
723
|
-
n.feed(a);
|
|
724
|
-
}
|
|
725
|
-
});
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
var Bt = ({
|
|
729
|
-
prefix: t,
|
|
730
|
-
size: e = 16,
|
|
731
|
-
alphabet: r = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
732
|
-
separator: s = "-"
|
|
733
|
-
} = {}) => {
|
|
734
|
-
const n = () => {
|
|
735
|
-
const a = r.length, i = new Array(e);
|
|
736
|
-
for (let u = 0; u < e; u++)
|
|
737
|
-
i[u] = r[Math.random() * a | 0];
|
|
738
|
-
return i.join("");
|
|
739
|
-
};
|
|
740
|
-
if (t == null)
|
|
741
|
-
return n;
|
|
742
|
-
if (r.includes(s))
|
|
743
|
-
throw new Lt({
|
|
744
|
-
argument: "separator",
|
|
745
|
-
message: `The separator "${s}" must not be part of the alphabet "${r}".`
|
|
746
|
-
});
|
|
747
|
-
return () => `${t}${s}${n()}`;
|
|
748
|
-
};
|
|
749
|
-
Bt();
|
|
750
|
-
function ze(t = globalThis) {
|
|
751
|
-
var e, r, s;
|
|
752
|
-
return t.window ? "runtime/browser" : (e = t.navigator) != null && e.userAgent ? `runtime/${t.navigator.userAgent.toLowerCase()}` : (s = (r = t.process) == null ? void 0 : r.versions) != null && s.node ? `runtime/node.js/${t.process.version.substring(0)}` : t.EdgeRuntime ? "runtime/vercel-edge" : "runtime/unknown";
|
|
753
|
-
}
|
|
754
|
-
function Gt(t) {
|
|
755
|
-
if (t == null)
|
|
756
|
-
return {};
|
|
757
|
-
const e = {};
|
|
758
|
-
if (t instanceof Headers)
|
|
759
|
-
t.forEach((r, s) => {
|
|
760
|
-
e[s.toLowerCase()] = r;
|
|
761
|
-
});
|
|
762
|
-
else {
|
|
763
|
-
Array.isArray(t) || (t = Object.entries(t));
|
|
764
|
-
for (const [r, s] of t)
|
|
765
|
-
s != null && (e[r.toLowerCase()] = s);
|
|
766
|
-
}
|
|
767
|
-
return e;
|
|
768
|
-
}
|
|
769
|
-
function Ve(t, ...e) {
|
|
770
|
-
const r = new Headers(Gt(t)), s = r.get("user-agent") || "";
|
|
771
|
-
return r.set(
|
|
772
|
-
"user-agent",
|
|
773
|
-
[s, ...e].filter(Boolean).join(" ")
|
|
774
|
-
), Object.fromEntries(r.entries());
|
|
775
|
-
}
|
|
776
|
-
var Kt = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/, Xt = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
|
|
777
|
-
function Ae(t) {
|
|
778
|
-
const e = JSON.parse(t);
|
|
779
|
-
return e === null || typeof e != "object" || Kt.test(t) === !1 && Xt.test(t) === !1 ? e : Qt(e);
|
|
780
|
-
}
|
|
781
|
-
function Qt(t) {
|
|
782
|
-
let e = [t];
|
|
783
|
-
for (; e.length; ) {
|
|
784
|
-
const r = e;
|
|
785
|
-
e = [];
|
|
786
|
-
for (const s of r) {
|
|
787
|
-
if (Object.prototype.hasOwnProperty.call(s, "__proto__"))
|
|
788
|
-
throw new SyntaxError("Object contains forbidden prototype property");
|
|
789
|
-
if (Object.prototype.hasOwnProperty.call(s, "constructor") && s.constructor !== null && typeof s.constructor == "object" && Object.prototype.hasOwnProperty.call(s.constructor, "prototype"))
|
|
790
|
-
throw new SyntaxError("Object contains forbidden prototype property");
|
|
791
|
-
for (const n in s) {
|
|
792
|
-
const a = s[n];
|
|
793
|
-
a && typeof a == "object" && e.push(a);
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
return t;
|
|
798
|
-
}
|
|
799
|
-
function Yt(t) {
|
|
800
|
-
const { stackTraceLimit: e } = Error;
|
|
801
|
-
try {
|
|
802
|
-
Error.stackTraceLimit = 0;
|
|
803
|
-
} catch {
|
|
804
|
-
return Ae(t);
|
|
805
|
-
}
|
|
806
|
-
try {
|
|
807
|
-
return Ae(t);
|
|
808
|
-
} finally {
|
|
809
|
-
Error.stackTraceLimit = e;
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
function ve(t) {
|
|
813
|
-
if (t.type === "object" || Array.isArray(t.type) && t.type.includes("object")) {
|
|
814
|
-
t.additionalProperties = !1;
|
|
815
|
-
const { properties: r } = t;
|
|
816
|
-
if (r != null)
|
|
817
|
-
for (const s of Object.keys(r))
|
|
818
|
-
r[s] = q(r[s]);
|
|
819
|
-
}
|
|
820
|
-
t.items != null && (t.items = Array.isArray(t.items) ? t.items.map(q) : q(t.items)), t.anyOf != null && (t.anyOf = t.anyOf.map(q)), t.allOf != null && (t.allOf = t.allOf.map(q)), t.oneOf != null && (t.oneOf = t.oneOf.map(q));
|
|
821
|
-
const { definitions: e } = t;
|
|
822
|
-
if (e != null)
|
|
823
|
-
for (const r of Object.keys(e))
|
|
824
|
-
e[r] = q(e[r]);
|
|
825
|
-
return t;
|
|
826
|
-
}
|
|
827
|
-
function q(t) {
|
|
828
|
-
return typeof t == "boolean" ? t : ve(t);
|
|
829
|
-
}
|
|
830
|
-
var er = /* @__PURE__ */ Symbol(
|
|
831
|
-
"Let zodToJsonSchema decide on which parser to use"
|
|
832
|
-
), ke = {
|
|
833
|
-
name: void 0,
|
|
834
|
-
$refStrategy: "root",
|
|
835
|
-
basePath: ["#"],
|
|
836
|
-
effectStrategy: "input",
|
|
837
|
-
pipeStrategy: "all",
|
|
838
|
-
dateStrategy: "format:date-time",
|
|
839
|
-
mapStrategy: "entries",
|
|
840
|
-
removeAdditionalStrategy: "passthrough",
|
|
841
|
-
allowedAdditionalProperties: !0,
|
|
842
|
-
rejectedAdditionalProperties: !1,
|
|
843
|
-
definitionPath: "definitions",
|
|
844
|
-
strictUnions: !1,
|
|
845
|
-
definitions: {},
|
|
846
|
-
errorMessages: !1,
|
|
847
|
-
patternStrategy: "escape",
|
|
848
|
-
applyRegexFlags: !1,
|
|
849
|
-
emailStrategy: "format:email",
|
|
850
|
-
base64Strategy: "contentEncoding:base64",
|
|
851
|
-
nameStrategy: "ref"
|
|
852
|
-
}, tr = (t) => typeof t == "string" ? {
|
|
853
|
-
...ke,
|
|
854
|
-
name: t
|
|
855
|
-
} : {
|
|
856
|
-
...ke,
|
|
857
|
-
...t
|
|
858
|
-
};
|
|
859
|
-
function E() {
|
|
860
|
-
return {};
|
|
861
|
-
}
|
|
862
|
-
function rr(t, e) {
|
|
863
|
-
var r, s, n;
|
|
864
|
-
const a = {
|
|
865
|
-
type: "array"
|
|
866
|
-
};
|
|
867
|
-
return (r = t.type) != null && r._def && ((n = (s = t.type) == null ? void 0 : s._def) == null ? void 0 : n.typeName) !== w.ZodAny && (a.items = T(t.type._def, {
|
|
868
|
-
...e,
|
|
869
|
-
currentPath: [...e.currentPath, "items"]
|
|
870
|
-
})), t.minLength && (a.minItems = t.minLength.value), t.maxLength && (a.maxItems = t.maxLength.value), t.exactLength && (a.minItems = t.exactLength.value, a.maxItems = t.exactLength.value), a;
|
|
871
|
-
}
|
|
872
|
-
function sr(t) {
|
|
873
|
-
const e = {
|
|
874
|
-
type: "integer",
|
|
875
|
-
format: "int64"
|
|
876
|
-
};
|
|
877
|
-
if (!t.checks) return e;
|
|
878
|
-
for (const r of t.checks)
|
|
879
|
-
switch (r.kind) {
|
|
880
|
-
case "min":
|
|
881
|
-
r.inclusive ? e.minimum = r.value : e.exclusiveMinimum = r.value;
|
|
882
|
-
break;
|
|
883
|
-
case "max":
|
|
884
|
-
r.inclusive ? e.maximum = r.value : e.exclusiveMaximum = r.value;
|
|
885
|
-
break;
|
|
886
|
-
case "multipleOf":
|
|
887
|
-
e.multipleOf = r.value;
|
|
888
|
-
break;
|
|
889
|
-
}
|
|
890
|
-
return e;
|
|
891
|
-
}
|
|
892
|
-
function nr() {
|
|
893
|
-
return { type: "boolean" };
|
|
894
|
-
}
|
|
895
|
-
function Je(t, e) {
|
|
896
|
-
return T(t.type._def, e);
|
|
897
|
-
}
|
|
898
|
-
var or = (t, e) => T(t.innerType._def, e);
|
|
899
|
-
function We(t, e, r) {
|
|
900
|
-
const s = r ?? e.dateStrategy;
|
|
901
|
-
if (Array.isArray(s))
|
|
902
|
-
return {
|
|
903
|
-
anyOf: s.map((n, a) => We(t, e, n))
|
|
904
|
-
};
|
|
905
|
-
switch (s) {
|
|
906
|
-
case "string":
|
|
907
|
-
case "format:date-time":
|
|
908
|
-
return {
|
|
909
|
-
type: "string",
|
|
910
|
-
format: "date-time"
|
|
911
|
-
};
|
|
912
|
-
case "format:date":
|
|
913
|
-
return {
|
|
914
|
-
type: "string",
|
|
915
|
-
format: "date"
|
|
916
|
-
};
|
|
917
|
-
case "integer":
|
|
918
|
-
return ar(t);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
var ar = (t) => {
|
|
922
|
-
const e = {
|
|
923
|
-
type: "integer",
|
|
924
|
-
format: "unix-time"
|
|
925
|
-
};
|
|
926
|
-
for (const r of t.checks)
|
|
927
|
-
switch (r.kind) {
|
|
928
|
-
case "min":
|
|
929
|
-
e.minimum = r.value;
|
|
930
|
-
break;
|
|
931
|
-
case "max":
|
|
932
|
-
e.maximum = r.value;
|
|
933
|
-
break;
|
|
934
|
-
}
|
|
935
|
-
return e;
|
|
936
|
-
};
|
|
937
|
-
function ir(t, e) {
|
|
938
|
-
return {
|
|
939
|
-
...T(t.innerType._def, e),
|
|
940
|
-
default: t.defaultValue()
|
|
941
|
-
};
|
|
942
|
-
}
|
|
943
|
-
function cr(t, e) {
|
|
944
|
-
return e.effectStrategy === "input" ? T(t.schema._def, e) : E();
|
|
945
|
-
}
|
|
946
|
-
function lr(t) {
|
|
947
|
-
return {
|
|
948
|
-
type: "string",
|
|
949
|
-
enum: Array.from(t.values)
|
|
950
|
-
};
|
|
951
|
-
}
|
|
952
|
-
var ur = (t) => "type" in t && t.type === "string" ? !1 : "allOf" in t;
|
|
953
|
-
function pr(t, e) {
|
|
954
|
-
const r = [
|
|
955
|
-
T(t.left._def, {
|
|
956
|
-
...e,
|
|
957
|
-
currentPath: [...e.currentPath, "allOf", "0"]
|
|
958
|
-
}),
|
|
959
|
-
T(t.right._def, {
|
|
960
|
-
...e,
|
|
961
|
-
currentPath: [...e.currentPath, "allOf", "1"]
|
|
962
|
-
})
|
|
963
|
-
].filter((n) => !!n), s = [];
|
|
964
|
-
return r.forEach((n) => {
|
|
965
|
-
if (ur(n))
|
|
966
|
-
s.push(...n.allOf);
|
|
967
|
-
else {
|
|
968
|
-
let a = n;
|
|
969
|
-
if ("additionalProperties" in n && n.additionalProperties === !1) {
|
|
970
|
-
const { additionalProperties: i, ...u } = n;
|
|
971
|
-
a = u;
|
|
972
|
-
}
|
|
973
|
-
s.push(a);
|
|
974
|
-
}
|
|
975
|
-
}), s.length ? { allOf: s } : void 0;
|
|
976
|
-
}
|
|
977
|
-
function dr(t) {
|
|
978
|
-
const e = typeof t.value;
|
|
979
|
-
return e !== "bigint" && e !== "number" && e !== "boolean" && e !== "string" ? {
|
|
980
|
-
type: Array.isArray(t.value) ? "array" : "object"
|
|
981
|
-
} : {
|
|
982
|
-
type: e === "bigint" ? "integer" : e,
|
|
983
|
-
const: t.value
|
|
984
|
-
};
|
|
985
|
-
}
|
|
986
|
-
var le = void 0, j = {
|
|
987
|
-
/**
|
|
988
|
-
* `c` was changed to `[cC]` to replicate /i flag
|
|
989
|
-
*/
|
|
990
|
-
cuid: /^[cC][^\s-]{8,}$/,
|
|
991
|
-
cuid2: /^[0-9a-z]+$/,
|
|
992
|
-
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
|
993
|
-
/**
|
|
994
|
-
* `a-z` was added to replicate /i flag
|
|
995
|
-
*/
|
|
996
|
-
email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
|
|
997
|
-
/**
|
|
998
|
-
* Constructed a valid Unicode RegExp
|
|
999
|
-
*
|
|
1000
|
-
* Lazily instantiate since this type of regex isn't supported
|
|
1001
|
-
* in all envs (e.g. React Native).
|
|
1002
|
-
*
|
|
1003
|
-
* See:
|
|
1004
|
-
* https://github.com/colinhacks/zod/issues/2433
|
|
1005
|
-
* Fix in Zod:
|
|
1006
|
-
* https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
|
|
1007
|
-
*/
|
|
1008
|
-
emoji: () => (le === void 0 && (le = RegExp(
|
|
1009
|
-
"^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$",
|
|
1010
|
-
"u"
|
|
1011
|
-
)), le),
|
|
1012
|
-
/**
|
|
1013
|
-
* Unused
|
|
1014
|
-
*/
|
|
1015
|
-
uuid: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/,
|
|
1016
|
-
/**
|
|
1017
|
-
* Unused
|
|
1018
|
-
*/
|
|
1019
|
-
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
|
|
1020
|
-
ipv4Cidr: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/,
|
|
1021
|
-
/**
|
|
1022
|
-
* Unused
|
|
1023
|
-
*/
|
|
1024
|
-
ipv6: /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/,
|
|
1025
|
-
ipv6Cidr: /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,
|
|
1026
|
-
base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
|
|
1027
|
-
base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
|
|
1028
|
-
nanoid: /^[a-zA-Z0-9_-]{21}$/,
|
|
1029
|
-
jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
|
|
1030
|
-
};
|
|
1031
|
-
function Be(t, e) {
|
|
1032
|
-
const r = {
|
|
1033
|
-
type: "string"
|
|
1034
|
-
};
|
|
1035
|
-
if (t.checks)
|
|
1036
|
-
for (const s of t.checks)
|
|
1037
|
-
switch (s.kind) {
|
|
1038
|
-
case "min":
|
|
1039
|
-
r.minLength = typeof r.minLength == "number" ? Math.max(r.minLength, s.value) : s.value;
|
|
1040
|
-
break;
|
|
1041
|
-
case "max":
|
|
1042
|
-
r.maxLength = typeof r.maxLength == "number" ? Math.min(r.maxLength, s.value) : s.value;
|
|
1043
|
-
break;
|
|
1044
|
-
case "email":
|
|
1045
|
-
switch (e.emailStrategy) {
|
|
1046
|
-
case "format:email":
|
|
1047
|
-
L(r, "email", s.message, e);
|
|
1048
|
-
break;
|
|
1049
|
-
case "format:idn-email":
|
|
1050
|
-
L(r, "idn-email", s.message, e);
|
|
1051
|
-
break;
|
|
1052
|
-
case "pattern:zod":
|
|
1053
|
-
P(r, j.email, s.message, e);
|
|
1054
|
-
break;
|
|
1055
|
-
}
|
|
1056
|
-
break;
|
|
1057
|
-
case "url":
|
|
1058
|
-
L(r, "uri", s.message, e);
|
|
1059
|
-
break;
|
|
1060
|
-
case "uuid":
|
|
1061
|
-
L(r, "uuid", s.message, e);
|
|
1062
|
-
break;
|
|
1063
|
-
case "regex":
|
|
1064
|
-
P(r, s.regex, s.message, e);
|
|
1065
|
-
break;
|
|
1066
|
-
case "cuid":
|
|
1067
|
-
P(r, j.cuid, s.message, e);
|
|
1068
|
-
break;
|
|
1069
|
-
case "cuid2":
|
|
1070
|
-
P(r, j.cuid2, s.message, e);
|
|
1071
|
-
break;
|
|
1072
|
-
case "startsWith":
|
|
1073
|
-
P(
|
|
1074
|
-
r,
|
|
1075
|
-
RegExp(`^${ue(s.value, e)}`),
|
|
1076
|
-
s.message,
|
|
1077
|
-
e
|
|
1078
|
-
);
|
|
1079
|
-
break;
|
|
1080
|
-
case "endsWith":
|
|
1081
|
-
P(
|
|
1082
|
-
r,
|
|
1083
|
-
RegExp(`${ue(s.value, e)}$`),
|
|
1084
|
-
s.message,
|
|
1085
|
-
e
|
|
1086
|
-
);
|
|
1087
|
-
break;
|
|
1088
|
-
case "datetime":
|
|
1089
|
-
L(r, "date-time", s.message, e);
|
|
1090
|
-
break;
|
|
1091
|
-
case "date":
|
|
1092
|
-
L(r, "date", s.message, e);
|
|
1093
|
-
break;
|
|
1094
|
-
case "time":
|
|
1095
|
-
L(r, "time", s.message, e);
|
|
1096
|
-
break;
|
|
1097
|
-
case "duration":
|
|
1098
|
-
L(r, "duration", s.message, e);
|
|
1099
|
-
break;
|
|
1100
|
-
case "length":
|
|
1101
|
-
r.minLength = typeof r.minLength == "number" ? Math.max(r.minLength, s.value) : s.value, r.maxLength = typeof r.maxLength == "number" ? Math.min(r.maxLength, s.value) : s.value;
|
|
1102
|
-
break;
|
|
1103
|
-
case "includes": {
|
|
1104
|
-
P(
|
|
1105
|
-
r,
|
|
1106
|
-
RegExp(ue(s.value, e)),
|
|
1107
|
-
s.message,
|
|
1108
|
-
e
|
|
1109
|
-
);
|
|
1110
|
-
break;
|
|
1111
|
-
}
|
|
1112
|
-
case "ip": {
|
|
1113
|
-
s.version !== "v6" && L(r, "ipv4", s.message, e), s.version !== "v4" && L(r, "ipv6", s.message, e);
|
|
1114
|
-
break;
|
|
1115
|
-
}
|
|
1116
|
-
case "base64url":
|
|
1117
|
-
P(r, j.base64url, s.message, e);
|
|
1118
|
-
break;
|
|
1119
|
-
case "jwt":
|
|
1120
|
-
P(r, j.jwt, s.message, e);
|
|
1121
|
-
break;
|
|
1122
|
-
case "cidr": {
|
|
1123
|
-
s.version !== "v6" && P(r, j.ipv4Cidr, s.message, e), s.version !== "v4" && P(r, j.ipv6Cidr, s.message, e);
|
|
1124
|
-
break;
|
|
1125
|
-
}
|
|
1126
|
-
case "emoji":
|
|
1127
|
-
P(r, j.emoji(), s.message, e);
|
|
1128
|
-
break;
|
|
1129
|
-
case "ulid": {
|
|
1130
|
-
P(r, j.ulid, s.message, e);
|
|
1131
|
-
break;
|
|
1132
|
-
}
|
|
1133
|
-
case "base64": {
|
|
1134
|
-
switch (e.base64Strategy) {
|
|
1135
|
-
case "format:binary": {
|
|
1136
|
-
L(r, "binary", s.message, e);
|
|
1137
|
-
break;
|
|
1138
|
-
}
|
|
1139
|
-
case "contentEncoding:base64": {
|
|
1140
|
-
r.contentEncoding = "base64";
|
|
1141
|
-
break;
|
|
1142
|
-
}
|
|
1143
|
-
case "pattern:zod": {
|
|
1144
|
-
P(r, j.base64, s.message, e);
|
|
1145
|
-
break;
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1148
|
-
break;
|
|
1149
|
-
}
|
|
1150
|
-
case "nanoid":
|
|
1151
|
-
P(r, j.nanoid, s.message, e);
|
|
1152
|
-
}
|
|
1153
|
-
return r;
|
|
1154
|
-
}
|
|
1155
|
-
function ue(t, e) {
|
|
1156
|
-
return e.patternStrategy === "escape" ? mr(t) : t;
|
|
1157
|
-
}
|
|
1158
|
-
var hr = new Set(
|
|
1159
|
-
"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
|
|
1160
|
-
);
|
|
1161
|
-
function mr(t) {
|
|
1162
|
-
let e = "";
|
|
1163
|
-
for (let r = 0; r < t.length; r++)
|
|
1164
|
-
hr.has(t[r]) || (e += "\\"), e += t[r];
|
|
1165
|
-
return e;
|
|
1166
|
-
}
|
|
1167
|
-
function L(t, e, r, s) {
|
|
1168
|
-
var n;
|
|
1169
|
-
t.format || (n = t.anyOf) != null && n.some((a) => a.format) ? (t.anyOf || (t.anyOf = []), t.format && (t.anyOf.push({
|
|
1170
|
-
format: t.format
|
|
1171
|
-
}), delete t.format), t.anyOf.push({
|
|
1172
|
-
format: e,
|
|
1173
|
-
...r && s.errorMessages && { errorMessage: { format: r } }
|
|
1174
|
-
})) : t.format = e;
|
|
1175
|
-
}
|
|
1176
|
-
function P(t, e, r, s) {
|
|
1177
|
-
var n;
|
|
1178
|
-
t.pattern || (n = t.allOf) != null && n.some((a) => a.pattern) ? (t.allOf || (t.allOf = []), t.pattern && (t.allOf.push({
|
|
1179
|
-
pattern: t.pattern
|
|
1180
|
-
}), delete t.pattern), t.allOf.push({
|
|
1181
|
-
pattern: Oe(e, s),
|
|
1182
|
-
...r && s.errorMessages && { errorMessage: { pattern: r } }
|
|
1183
|
-
})) : t.pattern = Oe(e, s);
|
|
1184
|
-
}
|
|
1185
|
-
function Oe(t, e) {
|
|
1186
|
-
var r;
|
|
1187
|
-
if (!e.applyRegexFlags || !t.flags)
|
|
1188
|
-
return t.source;
|
|
1189
|
-
const s = {
|
|
1190
|
-
i: t.flags.includes("i"),
|
|
1191
|
-
// Case-insensitive
|
|
1192
|
-
m: t.flags.includes("m"),
|
|
1193
|
-
// `^` and `$` matches adjacent to newline characters
|
|
1194
|
-
s: t.flags.includes("s")
|
|
1195
|
-
// `.` matches newlines
|
|
1196
|
-
}, n = s.i ? t.source.toLowerCase() : t.source;
|
|
1197
|
-
let a = "", i = !1, u = !1, c = !1;
|
|
1198
|
-
for (let l = 0; l < n.length; l++) {
|
|
1199
|
-
if (i) {
|
|
1200
|
-
a += n[l], i = !1;
|
|
1201
|
-
continue;
|
|
1202
|
-
}
|
|
1203
|
-
if (s.i) {
|
|
1204
|
-
if (u) {
|
|
1205
|
-
if (n[l].match(/[a-z]/)) {
|
|
1206
|
-
c ? (a += n[l], a += `${n[l - 2]}-${n[l]}`.toUpperCase(), c = !1) : n[l + 1] === "-" && ((r = n[l + 2]) != null && r.match(/[a-z]/)) ? (a += n[l], c = !0) : a += `${n[l]}${n[l].toUpperCase()}`;
|
|
1207
|
-
continue;
|
|
1208
|
-
}
|
|
1209
|
-
} else if (n[l].match(/[a-z]/)) {
|
|
1210
|
-
a += `[${n[l]}${n[l].toUpperCase()}]`;
|
|
1211
|
-
continue;
|
|
1212
|
-
}
|
|
1213
|
-
}
|
|
1214
|
-
if (s.m) {
|
|
1215
|
-
if (n[l] === "^") {
|
|
1216
|
-
a += `(^|(?<=[\r
|
|
1217
|
-
]))`;
|
|
1218
|
-
continue;
|
|
1219
|
-
} else if (n[l] === "$") {
|
|
1220
|
-
a += `($|(?=[\r
|
|
1221
|
-
]))`;
|
|
1222
|
-
continue;
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
if (s.s && n[l] === ".") {
|
|
1226
|
-
a += u ? `${n[l]}\r
|
|
1227
|
-
` : `[${n[l]}\r
|
|
1228
|
-
]`;
|
|
1229
|
-
continue;
|
|
1230
|
-
}
|
|
1231
|
-
a += n[l], n[l] === "\\" ? i = !0 : u && n[l] === "]" ? u = !1 : !u && n[l] === "[" && (u = !0);
|
|
1232
|
-
}
|
|
1233
|
-
try {
|
|
1234
|
-
new RegExp(a);
|
|
1235
|
-
} catch {
|
|
1236
|
-
return console.warn(
|
|
1237
|
-
`Could not convert regex pattern at ${e.currentPath.join(
|
|
1238
|
-
"/"
|
|
1239
|
-
)} to a flag-independent form! Falling back to the flag-ignorant source`
|
|
1240
|
-
), t.source;
|
|
1241
|
-
}
|
|
1242
|
-
return a;
|
|
1243
|
-
}
|
|
1244
|
-
function Ge(t, e) {
|
|
1245
|
-
var r, s, n, a, i, u;
|
|
1246
|
-
const c = {
|
|
1247
|
-
type: "object",
|
|
1248
|
-
additionalProperties: (r = T(t.valueType._def, {
|
|
1249
|
-
...e,
|
|
1250
|
-
currentPath: [...e.currentPath, "additionalProperties"]
|
|
1251
|
-
})) != null ? r : e.allowedAdditionalProperties
|
|
1252
|
-
};
|
|
1253
|
-
if (((s = t.keyType) == null ? void 0 : s._def.typeName) === w.ZodString && ((n = t.keyType._def.checks) != null && n.length)) {
|
|
1254
|
-
const { type: l, ...p } = Be(t.keyType._def, e);
|
|
1255
|
-
return {
|
|
1256
|
-
...c,
|
|
1257
|
-
propertyNames: p
|
|
1258
|
-
};
|
|
1259
|
-
} else {
|
|
1260
|
-
if (((a = t.keyType) == null ? void 0 : a._def.typeName) === w.ZodEnum)
|
|
1261
|
-
return {
|
|
1262
|
-
...c,
|
|
1263
|
-
propertyNames: {
|
|
1264
|
-
enum: t.keyType._def.values
|
|
1265
|
-
}
|
|
1266
|
-
};
|
|
1267
|
-
if (((i = t.keyType) == null ? void 0 : i._def.typeName) === w.ZodBranded && t.keyType._def.type._def.typeName === w.ZodString && ((u = t.keyType._def.type._def.checks) != null && u.length)) {
|
|
1268
|
-
const { type: l, ...p } = Je(
|
|
1269
|
-
t.keyType._def,
|
|
1270
|
-
e
|
|
1271
|
-
);
|
|
1272
|
-
return {
|
|
1273
|
-
...c,
|
|
1274
|
-
propertyNames: p
|
|
1275
|
-
};
|
|
1276
|
-
}
|
|
1277
|
-
}
|
|
1278
|
-
return c;
|
|
1279
|
-
}
|
|
1280
|
-
function fr(t, e) {
|
|
1281
|
-
if (e.mapStrategy === "record")
|
|
1282
|
-
return Ge(t, e);
|
|
1283
|
-
const r = T(t.keyType._def, {
|
|
1284
|
-
...e,
|
|
1285
|
-
currentPath: [...e.currentPath, "items", "items", "0"]
|
|
1286
|
-
}) || E(), s = T(t.valueType._def, {
|
|
1287
|
-
...e,
|
|
1288
|
-
currentPath: [...e.currentPath, "items", "items", "1"]
|
|
1289
|
-
}) || E();
|
|
1290
|
-
return {
|
|
1291
|
-
type: "array",
|
|
1292
|
-
maxItems: 125,
|
|
1293
|
-
items: {
|
|
1294
|
-
type: "array",
|
|
1295
|
-
items: [r, s],
|
|
1296
|
-
minItems: 2,
|
|
1297
|
-
maxItems: 2
|
|
1298
|
-
}
|
|
1299
|
-
};
|
|
1300
|
-
}
|
|
1301
|
-
function gr(t) {
|
|
1302
|
-
const e = t.values, s = Object.keys(t.values).filter((a) => typeof e[e[a]] != "number").map((a) => e[a]), n = Array.from(
|
|
1303
|
-
new Set(s.map((a) => typeof a))
|
|
1304
|
-
);
|
|
1305
|
-
return {
|
|
1306
|
-
type: n.length === 1 ? n[0] === "string" ? "string" : "number" : ["string", "number"],
|
|
1307
|
-
enum: s
|
|
1308
|
-
};
|
|
1309
|
-
}
|
|
1310
|
-
function yr() {
|
|
1311
|
-
return { not: E() };
|
|
1312
|
-
}
|
|
1313
|
-
function vr() {
|
|
1314
|
-
return {
|
|
1315
|
-
type: "null"
|
|
1316
|
-
};
|
|
1317
|
-
}
|
|
1318
|
-
var me = {
|
|
1319
|
-
ZodString: "string",
|
|
1320
|
-
ZodNumber: "number",
|
|
1321
|
-
ZodBigInt: "integer",
|
|
1322
|
-
ZodBoolean: "boolean",
|
|
1323
|
-
ZodNull: "null"
|
|
1324
|
-
};
|
|
1325
|
-
function wr(t, e) {
|
|
1326
|
-
const r = t.options instanceof Map ? Array.from(t.options.values()) : t.options;
|
|
1327
|
-
if (r.every(
|
|
1328
|
-
(s) => s._def.typeName in me && (!s._def.checks || !s._def.checks.length)
|
|
1329
|
-
)) {
|
|
1330
|
-
const s = r.reduce((n, a) => {
|
|
1331
|
-
const i = me[a._def.typeName];
|
|
1332
|
-
return i && !n.includes(i) ? [...n, i] : n;
|
|
1333
|
-
}, []);
|
|
1334
|
-
return {
|
|
1335
|
-
type: s.length > 1 ? s : s[0]
|
|
1336
|
-
};
|
|
1337
|
-
} else if (r.every((s) => s._def.typeName === "ZodLiteral" && !s.description)) {
|
|
1338
|
-
const s = r.reduce(
|
|
1339
|
-
(n, a) => {
|
|
1340
|
-
const i = typeof a._def.value;
|
|
1341
|
-
switch (i) {
|
|
1342
|
-
case "string":
|
|
1343
|
-
case "number":
|
|
1344
|
-
case "boolean":
|
|
1345
|
-
return [...n, i];
|
|
1346
|
-
case "bigint":
|
|
1347
|
-
return [...n, "integer"];
|
|
1348
|
-
case "object":
|
|
1349
|
-
if (a._def.value === null) return [...n, "null"];
|
|
1350
|
-
case "symbol":
|
|
1351
|
-
case "undefined":
|
|
1352
|
-
case "function":
|
|
1353
|
-
default:
|
|
1354
|
-
return n;
|
|
1355
|
-
}
|
|
1356
|
-
},
|
|
1357
|
-
[]
|
|
1358
|
-
);
|
|
1359
|
-
if (s.length === r.length) {
|
|
1360
|
-
const n = s.filter((a, i, u) => u.indexOf(a) === i);
|
|
1361
|
-
return {
|
|
1362
|
-
type: n.length > 1 ? n : n[0],
|
|
1363
|
-
enum: r.reduce(
|
|
1364
|
-
(a, i) => a.includes(i._def.value) ? a : [...a, i._def.value],
|
|
1365
|
-
[]
|
|
1366
|
-
)
|
|
1367
|
-
};
|
|
1368
|
-
}
|
|
1369
|
-
} else if (r.every((s) => s._def.typeName === "ZodEnum"))
|
|
1370
|
-
return {
|
|
1371
|
-
type: "string",
|
|
1372
|
-
enum: r.reduce(
|
|
1373
|
-
(s, n) => [
|
|
1374
|
-
...s,
|
|
1375
|
-
...n._def.values.filter((a) => !s.includes(a))
|
|
1376
|
-
],
|
|
1377
|
-
[]
|
|
1378
|
-
)
|
|
1379
|
-
};
|
|
1380
|
-
return _r(t, e);
|
|
1381
|
-
}
|
|
1382
|
-
var _r = (t, e) => {
|
|
1383
|
-
const r = (t.options instanceof Map ? Array.from(t.options.values()) : t.options).map(
|
|
1384
|
-
(s, n) => T(s._def, {
|
|
1385
|
-
...e,
|
|
1386
|
-
currentPath: [...e.currentPath, "anyOf", `${n}`]
|
|
1387
|
-
})
|
|
1388
|
-
).filter(
|
|
1389
|
-
(s) => !!s && (!e.strictUnions || typeof s == "object" && Object.keys(s).length > 0)
|
|
1390
|
-
);
|
|
1391
|
-
return r.length ? { anyOf: r } : void 0;
|
|
1392
|
-
};
|
|
1393
|
-
function br(t, e) {
|
|
1394
|
-
if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(
|
|
1395
|
-
t.innerType._def.typeName
|
|
1396
|
-
) && (!t.innerType._def.checks || !t.innerType._def.checks.length))
|
|
1397
|
-
return {
|
|
1398
|
-
type: [
|
|
1399
|
-
me[t.innerType._def.typeName],
|
|
1400
|
-
"null"
|
|
1401
|
-
]
|
|
1402
|
-
};
|
|
1403
|
-
const r = T(t.innerType._def, {
|
|
1404
|
-
...e,
|
|
1405
|
-
currentPath: [...e.currentPath, "anyOf", "0"]
|
|
1406
|
-
});
|
|
1407
|
-
return r && { anyOf: [r, { type: "null" }] };
|
|
1408
|
-
}
|
|
1409
|
-
function Sr(t) {
|
|
1410
|
-
const e = {
|
|
1411
|
-
type: "number"
|
|
1412
|
-
};
|
|
1413
|
-
if (!t.checks) return e;
|
|
1414
|
-
for (const r of t.checks)
|
|
1415
|
-
switch (r.kind) {
|
|
1416
|
-
case "int":
|
|
1417
|
-
e.type = "integer";
|
|
1418
|
-
break;
|
|
1419
|
-
case "min":
|
|
1420
|
-
r.inclusive ? e.minimum = r.value : e.exclusiveMinimum = r.value;
|
|
1421
|
-
break;
|
|
1422
|
-
case "max":
|
|
1423
|
-
r.inclusive ? e.maximum = r.value : e.exclusiveMaximum = r.value;
|
|
1424
|
-
break;
|
|
1425
|
-
case "multipleOf":
|
|
1426
|
-
e.multipleOf = r.value;
|
|
1427
|
-
break;
|
|
1428
|
-
}
|
|
1429
|
-
return e;
|
|
1430
|
-
}
|
|
1431
|
-
function Tr(t, e) {
|
|
1432
|
-
const r = {
|
|
1433
|
-
type: "object",
|
|
1434
|
-
properties: {}
|
|
1435
|
-
}, s = [], n = t.shape();
|
|
1436
|
-
for (const i in n) {
|
|
1437
|
-
let u = n[i];
|
|
1438
|
-
if (u === void 0 || u._def === void 0)
|
|
1439
|
-
continue;
|
|
1440
|
-
const c = Pr(u), l = T(u._def, {
|
|
1441
|
-
...e,
|
|
1442
|
-
currentPath: [...e.currentPath, "properties", i],
|
|
1443
|
-
propertyPath: [...e.currentPath, "properties", i]
|
|
1444
|
-
});
|
|
1445
|
-
l !== void 0 && (r.properties[i] = l, c || s.push(i));
|
|
1446
|
-
}
|
|
1447
|
-
s.length && (r.required = s);
|
|
1448
|
-
const a = Cr(t, e);
|
|
1449
|
-
return a !== void 0 && (r.additionalProperties = a), r;
|
|
1450
|
-
}
|
|
1451
|
-
function Cr(t, e) {
|
|
1452
|
-
if (t.catchall._def.typeName !== "ZodNever")
|
|
1453
|
-
return T(t.catchall._def, {
|
|
1454
|
-
...e,
|
|
1455
|
-
currentPath: [...e.currentPath, "additionalProperties"]
|
|
1456
|
-
});
|
|
1457
|
-
switch (t.unknownKeys) {
|
|
1458
|
-
case "passthrough":
|
|
1459
|
-
return e.allowedAdditionalProperties;
|
|
1460
|
-
case "strict":
|
|
1461
|
-
return e.rejectedAdditionalProperties;
|
|
1462
|
-
case "strip":
|
|
1463
|
-
return e.removeAdditionalStrategy === "strict" ? e.allowedAdditionalProperties : e.rejectedAdditionalProperties;
|
|
1464
|
-
}
|
|
1465
|
-
}
|
|
1466
|
-
function Pr(t) {
|
|
1467
|
-
try {
|
|
1468
|
-
return t.isOptional();
|
|
1469
|
-
} catch {
|
|
1470
|
-
return !0;
|
|
1471
|
-
}
|
|
1472
|
-
}
|
|
1473
|
-
var Rr = (t, e) => {
|
|
1474
|
-
var r;
|
|
1475
|
-
if (e.currentPath.toString() === ((r = e.propertyPath) == null ? void 0 : r.toString()))
|
|
1476
|
-
return T(t.innerType._def, e);
|
|
1477
|
-
const s = T(t.innerType._def, {
|
|
1478
|
-
...e,
|
|
1479
|
-
currentPath: [...e.currentPath, "anyOf", "1"]
|
|
1480
|
-
});
|
|
1481
|
-
return s ? { anyOf: [{ not: E() }, s] } : E();
|
|
1482
|
-
}, Mr = (t, e) => {
|
|
1483
|
-
if (e.pipeStrategy === "input")
|
|
1484
|
-
return T(t.in._def, e);
|
|
1485
|
-
if (e.pipeStrategy === "output")
|
|
1486
|
-
return T(t.out._def, e);
|
|
1487
|
-
const r = T(t.in._def, {
|
|
1488
|
-
...e,
|
|
1489
|
-
currentPath: [...e.currentPath, "allOf", "0"]
|
|
1490
|
-
}), s = T(t.out._def, {
|
|
1491
|
-
...e,
|
|
1492
|
-
currentPath: [...e.currentPath, "allOf", r ? "1" : "0"]
|
|
1493
|
-
});
|
|
1494
|
-
return {
|
|
1495
|
-
allOf: [r, s].filter((n) => n !== void 0)
|
|
1496
|
-
};
|
|
1497
|
-
};
|
|
1498
|
-
function Er(t, e) {
|
|
1499
|
-
return T(t.type._def, e);
|
|
1500
|
-
}
|
|
1501
|
-
function Ar(t, e) {
|
|
1502
|
-
const s = {
|
|
1503
|
-
type: "array",
|
|
1504
|
-
uniqueItems: !0,
|
|
1505
|
-
items: T(t.valueType._def, {
|
|
1506
|
-
...e,
|
|
1507
|
-
currentPath: [...e.currentPath, "items"]
|
|
1508
|
-
})
|
|
1509
|
-
};
|
|
1510
|
-
return t.minSize && (s.minItems = t.minSize.value), t.maxSize && (s.maxItems = t.maxSize.value), s;
|
|
1511
|
-
}
|
|
1512
|
-
function kr(t, e) {
|
|
1513
|
-
return t.rest ? {
|
|
1514
|
-
type: "array",
|
|
1515
|
-
minItems: t.items.length,
|
|
1516
|
-
items: t.items.map(
|
|
1517
|
-
(r, s) => T(r._def, {
|
|
1518
|
-
...e,
|
|
1519
|
-
currentPath: [...e.currentPath, "items", `${s}`]
|
|
1520
|
-
})
|
|
1521
|
-
).reduce(
|
|
1522
|
-
(r, s) => s === void 0 ? r : [...r, s],
|
|
1523
|
-
[]
|
|
1524
|
-
),
|
|
1525
|
-
additionalItems: T(t.rest._def, {
|
|
1526
|
-
...e,
|
|
1527
|
-
currentPath: [...e.currentPath, "additionalItems"]
|
|
1528
|
-
})
|
|
1529
|
-
} : {
|
|
1530
|
-
type: "array",
|
|
1531
|
-
minItems: t.items.length,
|
|
1532
|
-
maxItems: t.items.length,
|
|
1533
|
-
items: t.items.map(
|
|
1534
|
-
(r, s) => T(r._def, {
|
|
1535
|
-
...e,
|
|
1536
|
-
currentPath: [...e.currentPath, "items", `${s}`]
|
|
1537
|
-
})
|
|
1538
|
-
).reduce(
|
|
1539
|
-
(r, s) => s === void 0 ? r : [...r, s],
|
|
1540
|
-
[]
|
|
1541
|
-
)
|
|
1542
|
-
};
|
|
1543
|
-
}
|
|
1544
|
-
function Or() {
|
|
1545
|
-
return {
|
|
1546
|
-
not: E()
|
|
1547
|
-
};
|
|
1548
|
-
}
|
|
1549
|
-
function xr() {
|
|
1550
|
-
return E();
|
|
1551
|
-
}
|
|
1552
|
-
var Ir = (t, e) => T(t.innerType._def, e), jr = (t, e, r) => {
|
|
1553
|
-
switch (e) {
|
|
1554
|
-
case w.ZodString:
|
|
1555
|
-
return Be(t, r);
|
|
1556
|
-
case w.ZodNumber:
|
|
1557
|
-
return Sr(t);
|
|
1558
|
-
case w.ZodObject:
|
|
1559
|
-
return Tr(t, r);
|
|
1560
|
-
case w.ZodBigInt:
|
|
1561
|
-
return sr(t);
|
|
1562
|
-
case w.ZodBoolean:
|
|
1563
|
-
return nr();
|
|
1564
|
-
case w.ZodDate:
|
|
1565
|
-
return We(t, r);
|
|
1566
|
-
case w.ZodUndefined:
|
|
1567
|
-
return Or();
|
|
1568
|
-
case w.ZodNull:
|
|
1569
|
-
return vr();
|
|
1570
|
-
case w.ZodArray:
|
|
1571
|
-
return rr(t, r);
|
|
1572
|
-
case w.ZodUnion:
|
|
1573
|
-
case w.ZodDiscriminatedUnion:
|
|
1574
|
-
return wr(t, r);
|
|
1575
|
-
case w.ZodIntersection:
|
|
1576
|
-
return pr(t, r);
|
|
1577
|
-
case w.ZodTuple:
|
|
1578
|
-
return kr(t, r);
|
|
1579
|
-
case w.ZodRecord:
|
|
1580
|
-
return Ge(t, r);
|
|
1581
|
-
case w.ZodLiteral:
|
|
1582
|
-
return dr(t);
|
|
1583
|
-
case w.ZodEnum:
|
|
1584
|
-
return lr(t);
|
|
1585
|
-
case w.ZodNativeEnum:
|
|
1586
|
-
return gr(t);
|
|
1587
|
-
case w.ZodNullable:
|
|
1588
|
-
return br(t, r);
|
|
1589
|
-
case w.ZodOptional:
|
|
1590
|
-
return Rr(t, r);
|
|
1591
|
-
case w.ZodMap:
|
|
1592
|
-
return fr(t, r);
|
|
1593
|
-
case w.ZodSet:
|
|
1594
|
-
return Ar(t, r);
|
|
1595
|
-
case w.ZodLazy:
|
|
1596
|
-
return () => t.getter()._def;
|
|
1597
|
-
case w.ZodPromise:
|
|
1598
|
-
return Er(t, r);
|
|
1599
|
-
case w.ZodNaN:
|
|
1600
|
-
case w.ZodNever:
|
|
1601
|
-
return yr();
|
|
1602
|
-
case w.ZodEffects:
|
|
1603
|
-
return cr(t, r);
|
|
1604
|
-
case w.ZodAny:
|
|
1605
|
-
return E();
|
|
1606
|
-
case w.ZodUnknown:
|
|
1607
|
-
return xr();
|
|
1608
|
-
case w.ZodDefault:
|
|
1609
|
-
return ir(t, r);
|
|
1610
|
-
case w.ZodBranded:
|
|
1611
|
-
return Je(t, r);
|
|
1612
|
-
case w.ZodReadonly:
|
|
1613
|
-
return Ir(t, r);
|
|
1614
|
-
case w.ZodCatch:
|
|
1615
|
-
return or(t, r);
|
|
1616
|
-
case w.ZodPipeline:
|
|
1617
|
-
return Mr(t, r);
|
|
1618
|
-
case w.ZodFunction:
|
|
1619
|
-
case w.ZodVoid:
|
|
1620
|
-
case w.ZodSymbol:
|
|
1621
|
-
return;
|
|
1622
|
-
default:
|
|
1623
|
-
return /* @__PURE__ */ ((s) => {
|
|
1624
|
-
})();
|
|
1625
|
-
}
|
|
1626
|
-
}, Lr = (t, e) => {
|
|
1627
|
-
let r = 0;
|
|
1628
|
-
for (; r < t.length && r < e.length && t[r] === e[r]; r++)
|
|
1629
|
-
;
|
|
1630
|
-
return [(t.length - r).toString(), ...e.slice(r)].join("/");
|
|
1631
|
-
};
|
|
1632
|
-
function T(t, e, r = !1) {
|
|
1633
|
-
var s;
|
|
1634
|
-
const n = e.seen.get(t);
|
|
1635
|
-
if (e.override) {
|
|
1636
|
-
const c = (s = e.override) == null ? void 0 : s.call(
|
|
1637
|
-
e,
|
|
1638
|
-
t,
|
|
1639
|
-
e,
|
|
1640
|
-
n,
|
|
1641
|
-
r
|
|
1642
|
-
);
|
|
1643
|
-
if (c !== er)
|
|
1644
|
-
return c;
|
|
1645
|
-
}
|
|
1646
|
-
if (n && !r) {
|
|
1647
|
-
const c = Ur(n, e);
|
|
1648
|
-
if (c !== void 0)
|
|
1649
|
-
return c;
|
|
1650
|
-
}
|
|
1651
|
-
const a = { def: t, path: e.currentPath, jsonSchema: void 0 };
|
|
1652
|
-
e.seen.set(t, a);
|
|
1653
|
-
const i = jr(t, t.typeName, e), u = typeof i == "function" ? T(i(), e) : i;
|
|
1654
|
-
if (u && $r(t, e, u), e.postProcess) {
|
|
1655
|
-
const c = e.postProcess(u, t, e);
|
|
1656
|
-
return a.jsonSchema = u, c;
|
|
1657
|
-
}
|
|
1658
|
-
return a.jsonSchema = u, u;
|
|
1659
|
-
}
|
|
1660
|
-
var Ur = (t, e) => {
|
|
1661
|
-
switch (e.$refStrategy) {
|
|
1662
|
-
case "root":
|
|
1663
|
-
return { $ref: t.path.join("/") };
|
|
1664
|
-
case "relative":
|
|
1665
|
-
return { $ref: Lr(e.currentPath, t.path) };
|
|
1666
|
-
case "none":
|
|
1667
|
-
case "seen":
|
|
1668
|
-
return t.path.length < e.currentPath.length && t.path.every((r, s) => e.currentPath[s] === r) ? (console.warn(
|
|
1669
|
-
`Recursive reference detected at ${e.currentPath.join(
|
|
1670
|
-
"/"
|
|
1671
|
-
)}! Defaulting to any`
|
|
1672
|
-
), E()) : e.$refStrategy === "seen" ? E() : void 0;
|
|
1673
|
-
}
|
|
1674
|
-
}, $r = (t, e, r) => (t.description && (r.description = t.description), r), Nr = (t) => {
|
|
1675
|
-
const e = tr(t), r = e.name !== void 0 ? [...e.basePath, e.definitionPath, e.name] : e.basePath;
|
|
1676
|
-
return {
|
|
1677
|
-
...e,
|
|
1678
|
-
currentPath: r,
|
|
1679
|
-
propertyPath: void 0,
|
|
1680
|
-
seen: new Map(
|
|
1681
|
-
Object.entries(e.definitions).map(([s, n]) => [
|
|
1682
|
-
n._def,
|
|
1683
|
-
{
|
|
1684
|
-
def: n._def,
|
|
1685
|
-
path: [...e.basePath, e.definitionPath, s],
|
|
1686
|
-
// Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
|
|
1687
|
-
jsonSchema: void 0
|
|
1688
|
-
}
|
|
1689
|
-
])
|
|
1690
|
-
)
|
|
1691
|
-
};
|
|
1692
|
-
}, qr = (t, e) => {
|
|
1693
|
-
var r;
|
|
1694
|
-
const s = Nr(e);
|
|
1695
|
-
let n = typeof e == "object" && e.definitions ? Object.entries(e.definitions).reduce(
|
|
1696
|
-
(l, [p, m]) => {
|
|
1697
|
-
var d;
|
|
1698
|
-
return {
|
|
1699
|
-
...l,
|
|
1700
|
-
[p]: (d = T(
|
|
1701
|
-
m._def,
|
|
1702
|
-
{
|
|
1703
|
-
...s,
|
|
1704
|
-
currentPath: [...s.basePath, s.definitionPath, p]
|
|
1705
|
-
},
|
|
1706
|
-
!0
|
|
1707
|
-
)) != null ? d : E()
|
|
1708
|
-
};
|
|
1709
|
-
},
|
|
1710
|
-
{}
|
|
1711
|
-
) : void 0;
|
|
1712
|
-
const a = typeof e == "string" ? e : e?.nameStrategy === "title" ? void 0 : e?.name, i = (r = T(
|
|
1713
|
-
t._def,
|
|
1714
|
-
a === void 0 ? s : {
|
|
1715
|
-
...s,
|
|
1716
|
-
currentPath: [...s.basePath, s.definitionPath, a]
|
|
1717
|
-
},
|
|
1718
|
-
!1
|
|
1719
|
-
)) != null ? r : E(), u = typeof e == "object" && e.name !== void 0 && e.nameStrategy === "title" ? e.name : void 0;
|
|
1720
|
-
u !== void 0 && (i.title = u);
|
|
1721
|
-
const c = a === void 0 ? n ? {
|
|
1722
|
-
...i,
|
|
1723
|
-
[s.definitionPath]: n
|
|
1724
|
-
} : i : {
|
|
1725
|
-
$ref: [
|
|
1726
|
-
...s.$refStrategy === "relative" ? [] : s.basePath,
|
|
1727
|
-
s.definitionPath,
|
|
1728
|
-
a
|
|
1729
|
-
].join("/"),
|
|
1730
|
-
[s.definitionPath]: {
|
|
1731
|
-
...n,
|
|
1732
|
-
[a]: i
|
|
1733
|
-
}
|
|
1734
|
-
};
|
|
1735
|
-
return c.$schema = "http://json-schema.org/draft-07/schema#", c;
|
|
1736
|
-
}, fe = /* @__PURE__ */ Symbol.for("vercel.ai.schema");
|
|
1737
|
-
function J(t, {
|
|
1738
|
-
validate: e
|
|
1739
|
-
} = {}) {
|
|
1740
|
-
return {
|
|
1741
|
-
[fe]: !0,
|
|
1742
|
-
_type: void 0,
|
|
1743
|
-
// should never be used directly
|
|
1744
|
-
get jsonSchema() {
|
|
1745
|
-
return typeof t == "function" && (t = t()), t;
|
|
1746
|
-
},
|
|
1747
|
-
validate: e
|
|
1748
|
-
};
|
|
1749
|
-
}
|
|
1750
|
-
function Hr(t) {
|
|
1751
|
-
return typeof t == "object" && t !== null && fe in t && t[fe] === !0 && "jsonSchema" in t && "validate" in t;
|
|
1752
|
-
}
|
|
1753
|
-
function Ke(t) {
|
|
1754
|
-
return t == null ? J({ properties: {}, additionalProperties: !1 }) : Hr(t) ? t : "~standard" in t ? t["~standard"].vendor === "zod" ? Vr(t) : Fr(t) : t();
|
|
1755
|
-
}
|
|
1756
|
-
function Fr(t) {
|
|
1757
|
-
return J(
|
|
1758
|
-
() => ve(
|
|
1759
|
-
t["~standard"].jsonSchema.input({
|
|
1760
|
-
target: "draft-07"
|
|
1761
|
-
})
|
|
1762
|
-
),
|
|
1763
|
-
{
|
|
1764
|
-
validate: async (e) => {
|
|
1765
|
-
const r = await t["~standard"].validate(e);
|
|
1766
|
-
return "value" in r ? { success: !0, value: r.value } : {
|
|
1767
|
-
success: !1,
|
|
1768
|
-
error: new de({
|
|
1769
|
-
value: e,
|
|
1770
|
-
cause: r.issues
|
|
1771
|
-
})
|
|
1772
|
-
};
|
|
1773
|
-
}
|
|
1774
|
-
}
|
|
1775
|
-
);
|
|
1776
|
-
}
|
|
1777
|
-
function Zr(t, e) {
|
|
1778
|
-
var r;
|
|
1779
|
-
const s = (r = void 0) != null ? r : !1;
|
|
1780
|
-
return J(
|
|
1781
|
-
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
|
1782
|
-
() => qr(t, {
|
|
1783
|
-
$refStrategy: s ? "root" : "none"
|
|
1784
|
-
}),
|
|
1785
|
-
{
|
|
1786
|
-
validate: async (n) => {
|
|
1787
|
-
const a = await t.safeParseAsync(n);
|
|
1788
|
-
return a.success ? { success: !0, value: a.data } : { success: !1, error: a.error };
|
|
1789
|
-
}
|
|
1790
|
-
}
|
|
1791
|
-
);
|
|
1792
|
-
}
|
|
1793
|
-
function Dr(t, e) {
|
|
1794
|
-
var r;
|
|
1795
|
-
const s = (r = void 0) != null ? r : !1;
|
|
1796
|
-
return J(
|
|
1797
|
-
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
|
1798
|
-
() => ve(
|
|
1799
|
-
Me.toJSONSchema(t, {
|
|
1800
|
-
target: "draft-7",
|
|
1801
|
-
io: "input",
|
|
1802
|
-
reused: s ? "ref" : "inline"
|
|
1803
|
-
})
|
|
1804
|
-
),
|
|
1805
|
-
{
|
|
1806
|
-
validate: async (n) => {
|
|
1807
|
-
const a = await Me.safeParseAsync(t, n);
|
|
1808
|
-
return a.success ? { success: !0, value: a.data } : { success: !1, error: a.error };
|
|
1809
|
-
}
|
|
1810
|
-
}
|
|
1811
|
-
);
|
|
1812
|
-
}
|
|
1813
|
-
function zr(t) {
|
|
1814
|
-
return "_zod" in t;
|
|
1815
|
-
}
|
|
1816
|
-
function Vr(t, e) {
|
|
1817
|
-
return zr(t) ? Dr(t) : Zr(t);
|
|
1818
|
-
}
|
|
1819
|
-
async function Xe({
|
|
1820
|
-
value: t,
|
|
1821
|
-
schema: e,
|
|
1822
|
-
context: r
|
|
1823
|
-
}) {
|
|
1824
|
-
const s = Ke(e);
|
|
1825
|
-
try {
|
|
1826
|
-
if (s.validate == null)
|
|
1827
|
-
return { success: !0, value: t, rawValue: t };
|
|
1828
|
-
const n = await s.validate(t);
|
|
1829
|
-
return n.success ? { success: !0, value: n.value, rawValue: t } : {
|
|
1830
|
-
success: !1,
|
|
1831
|
-
error: de.wrap({ value: t, cause: n.error, context: r }),
|
|
1832
|
-
rawValue: t
|
|
1833
|
-
};
|
|
1834
|
-
} catch (n) {
|
|
1835
|
-
return {
|
|
1836
|
-
success: !1,
|
|
1837
|
-
error: de.wrap({ value: t, cause: n, context: r }),
|
|
1838
|
-
rawValue: t
|
|
1839
|
-
};
|
|
1840
|
-
}
|
|
1841
|
-
}
|
|
1842
|
-
async function Jr({
|
|
1843
|
-
text: t,
|
|
1844
|
-
schema: e
|
|
1845
|
-
}) {
|
|
1846
|
-
try {
|
|
1847
|
-
const r = Yt(t);
|
|
1848
|
-
return e == null ? { success: !0, value: r, rawValue: r } : await Xe({ value: r, schema: e });
|
|
1849
|
-
} catch (r) {
|
|
1850
|
-
return {
|
|
1851
|
-
success: !1,
|
|
1852
|
-
error: Re.isInstance(r) ? r : new Re({ text: t, cause: r }),
|
|
1853
|
-
rawValue: void 0
|
|
1854
|
-
};
|
|
1855
|
-
}
|
|
1856
|
-
}
|
|
1857
|
-
function Wr(t) {
|
|
1858
|
-
return { ...t, type: "dynamic" };
|
|
1859
|
-
}
|
|
1860
|
-
let we;
|
|
1861
|
-
we = globalThis.crypto;
|
|
1862
|
-
async function Br(t) {
|
|
1863
|
-
return (await we).getRandomValues(new Uint8Array(t));
|
|
1864
|
-
}
|
|
1865
|
-
async function Gr(t) {
|
|
1866
|
-
const e = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~", r = Math.pow(2, 8) - Math.pow(2, 8) % e.length;
|
|
1867
|
-
let s = "";
|
|
1868
|
-
for (; s.length < t; ) {
|
|
1869
|
-
const n = await Br(t - s.length);
|
|
1870
|
-
for (const a of n)
|
|
1871
|
-
a < r && (s += e[a % e.length]);
|
|
1872
|
-
}
|
|
1873
|
-
return s;
|
|
1874
|
-
}
|
|
1875
|
-
async function Kr(t) {
|
|
1876
|
-
return await Gr(t);
|
|
1877
|
-
}
|
|
1878
|
-
async function Xr(t) {
|
|
1879
|
-
const e = await (await we).subtle.digest("SHA-256", new TextEncoder().encode(t));
|
|
1880
|
-
return btoa(String.fromCharCode(...new Uint8Array(e))).replace(/\//g, "_").replace(/\+/g, "-").replace(/=/g, "");
|
|
1881
|
-
}
|
|
1882
|
-
async function Qr(t) {
|
|
1883
|
-
if (t || (t = 43), t < 43 || t > 128)
|
|
1884
|
-
throw `Expected a length between 43 and 128. Received ${t}.`;
|
|
1885
|
-
const e = await Kr(t), r = await Xr(e);
|
|
1886
|
-
return {
|
|
1887
|
-
code_verifier: e,
|
|
1888
|
-
code_challenge: r
|
|
1889
|
-
};
|
|
1890
|
-
}
|
|
1891
|
-
var Yr = "AI_MCPClientError", Qe = `vercel.ai.error.${Yr}`, es = Symbol.for(Qe), xe, Ie, _ = class extends (Ie = K, xe = es, Ie) {
|
|
1892
|
-
constructor({
|
|
1893
|
-
name: t = "MCPClientError",
|
|
1894
|
-
message: e,
|
|
1895
|
-
cause: r,
|
|
1896
|
-
data: s,
|
|
1897
|
-
code: n
|
|
1898
|
-
}) {
|
|
1899
|
-
super({ name: t, message: e, cause: r }), this[xe] = !0, this.data = s, this.code = n;
|
|
1900
|
-
}
|
|
1901
|
-
static isInstance(t) {
|
|
1902
|
-
return K.hasMarker(t, Qe);
|
|
1903
|
-
}
|
|
1904
|
-
}, Z = "2025-11-25", ts = [
|
|
1905
|
-
Z,
|
|
1906
|
-
"2025-06-18",
|
|
1907
|
-
"2025-03-26",
|
|
1908
|
-
"2024-11-05"
|
|
1909
|
-
], rs = o.optional(o.record(o.string(), o.unknown())), ss = o.looseObject({
|
|
1910
|
-
name: o.string(),
|
|
1911
|
-
version: o.string(),
|
|
1912
|
-
title: o.optional(o.string())
|
|
1913
|
-
}), oe = o.looseObject({
|
|
1914
|
-
_meta: o.optional(o.object({}).loose())
|
|
1915
|
-
}), U = oe, Ye = o.object({
|
|
1916
|
-
method: o.string(),
|
|
1917
|
-
params: o.optional(oe)
|
|
1918
|
-
}), et = o.object({
|
|
1919
|
-
applyDefaults: o.optional(o.boolean())
|
|
1920
|
-
}).loose(), ns = o.looseObject({
|
|
1921
|
-
experimental: o.optional(o.object({}).loose()),
|
|
1922
|
-
logging: o.optional(o.object({}).loose()),
|
|
1923
|
-
prompts: o.optional(
|
|
1924
|
-
o.looseObject({
|
|
1925
|
-
listChanged: o.optional(o.boolean())
|
|
1926
|
-
})
|
|
1927
|
-
),
|
|
1928
|
-
resources: o.optional(
|
|
1929
|
-
o.looseObject({
|
|
1930
|
-
subscribe: o.optional(o.boolean()),
|
|
1931
|
-
listChanged: o.optional(o.boolean())
|
|
1932
|
-
})
|
|
1933
|
-
),
|
|
1934
|
-
tools: o.optional(
|
|
1935
|
-
o.looseObject({
|
|
1936
|
-
listChanged: o.optional(o.boolean())
|
|
1937
|
-
})
|
|
1938
|
-
),
|
|
1939
|
-
elicitation: o.optional(et)
|
|
1940
|
-
});
|
|
1941
|
-
o.object({
|
|
1942
|
-
elicitation: o.optional(et)
|
|
1943
|
-
}).loose();
|
|
1944
|
-
var os = U.extend({
|
|
1945
|
-
protocolVersion: o.string(),
|
|
1946
|
-
capabilities: ns,
|
|
1947
|
-
serverInfo: ss,
|
|
1948
|
-
instructions: o.optional(o.string())
|
|
1949
|
-
}), _e = U.extend({
|
|
1950
|
-
nextCursor: o.optional(o.string())
|
|
1951
|
-
}), as = o.object({
|
|
1952
|
-
name: o.string(),
|
|
1953
|
-
/**
|
|
1954
|
-
* @see https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool
|
|
1955
|
-
*/
|
|
1956
|
-
title: o.optional(o.string()),
|
|
1957
|
-
description: o.optional(o.string()),
|
|
1958
|
-
inputSchema: o.object({
|
|
1959
|
-
type: o.literal("object"),
|
|
1960
|
-
properties: o.optional(o.object({}).loose())
|
|
1961
|
-
}).loose(),
|
|
1962
|
-
/**
|
|
1963
|
-
* @see https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema
|
|
1964
|
-
*/
|
|
1965
|
-
outputSchema: o.optional(o.object({}).loose()),
|
|
1966
|
-
annotations: o.optional(
|
|
1967
|
-
o.object({
|
|
1968
|
-
title: o.optional(o.string())
|
|
1969
|
-
}).loose()
|
|
1970
|
-
),
|
|
1971
|
-
_meta: rs
|
|
1972
|
-
}).loose(), is = _e.extend({
|
|
1973
|
-
tools: o.array(as)
|
|
1974
|
-
}), tt = o.object({
|
|
1975
|
-
type: o.literal("text"),
|
|
1976
|
-
text: o.string()
|
|
1977
|
-
}).loose(), rt = o.object({
|
|
1978
|
-
type: o.literal("image"),
|
|
1979
|
-
data: o.base64(),
|
|
1980
|
-
mimeType: o.string()
|
|
1981
|
-
}).loose(), cs = o.object({
|
|
1982
|
-
uri: o.string(),
|
|
1983
|
-
name: o.string(),
|
|
1984
|
-
title: o.optional(o.string()),
|
|
1985
|
-
description: o.optional(o.string()),
|
|
1986
|
-
mimeType: o.optional(o.string()),
|
|
1987
|
-
size: o.optional(o.number())
|
|
1988
|
-
}).loose(), ls = _e.extend({
|
|
1989
|
-
resources: o.array(cs)
|
|
1990
|
-
}), st = o.object({
|
|
1991
|
-
/**
|
|
1992
|
-
* The URI of this resource.
|
|
1993
|
-
*/
|
|
1994
|
-
uri: o.string(),
|
|
1995
|
-
/**
|
|
1996
|
-
* Optional display name of the resource content.
|
|
1997
|
-
*/
|
|
1998
|
-
name: o.optional(o.string()),
|
|
1999
|
-
/**
|
|
2000
|
-
* Optional human readable title.
|
|
2001
|
-
*/
|
|
2002
|
-
title: o.optional(o.string()),
|
|
2003
|
-
/**
|
|
2004
|
-
* The MIME type of this resource, if known.
|
|
2005
|
-
*/
|
|
2006
|
-
mimeType: o.optional(o.string())
|
|
2007
|
-
}).loose(), nt = st.extend({
|
|
2008
|
-
text: o.string()
|
|
2009
|
-
}), ot = st.extend({
|
|
2010
|
-
blob: o.base64()
|
|
2011
|
-
}), at = o.object({
|
|
2012
|
-
type: o.literal("resource"),
|
|
2013
|
-
resource: o.union([nt, ot])
|
|
2014
|
-
}).loose(), us = U.extend({
|
|
2015
|
-
content: o.array(
|
|
2016
|
-
o.union([tt, rt, at])
|
|
2017
|
-
),
|
|
2018
|
-
/**
|
|
2019
|
-
* @see https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content
|
|
2020
|
-
*/
|
|
2021
|
-
structuredContent: o.optional(o.unknown()),
|
|
2022
|
-
isError: o.boolean().default(!1).optional()
|
|
2023
|
-
}).or(
|
|
2024
|
-
U.extend({
|
|
2025
|
-
toolResult: o.unknown()
|
|
2026
|
-
})
|
|
2027
|
-
), ps = o.object({
|
|
2028
|
-
uriTemplate: o.string(),
|
|
2029
|
-
name: o.string(),
|
|
2030
|
-
title: o.optional(o.string()),
|
|
2031
|
-
description: o.optional(o.string()),
|
|
2032
|
-
mimeType: o.optional(o.string())
|
|
2033
|
-
}).loose(), ds = U.extend({
|
|
2034
|
-
resourceTemplates: o.array(ps)
|
|
2035
|
-
}), hs = U.extend({
|
|
2036
|
-
contents: o.array(
|
|
2037
|
-
o.union([nt, ot])
|
|
2038
|
-
)
|
|
2039
|
-
}), ms = o.object({
|
|
2040
|
-
name: o.string(),
|
|
2041
|
-
description: o.optional(o.string()),
|
|
2042
|
-
required: o.optional(o.boolean())
|
|
2043
|
-
}).loose(), fs = o.object({
|
|
2044
|
-
name: o.string(),
|
|
2045
|
-
title: o.optional(o.string()),
|
|
2046
|
-
description: o.optional(o.string()),
|
|
2047
|
-
arguments: o.optional(o.array(ms))
|
|
2048
|
-
}).loose(), gs = _e.extend({
|
|
2049
|
-
prompts: o.array(fs)
|
|
2050
|
-
}), ys = o.object({
|
|
2051
|
-
role: o.union([o.literal("user"), o.literal("assistant")]),
|
|
2052
|
-
content: o.union([
|
|
2053
|
-
tt,
|
|
2054
|
-
rt,
|
|
2055
|
-
at
|
|
2056
|
-
])
|
|
2057
|
-
}).loose(), vs = U.extend({
|
|
2058
|
-
description: o.optional(o.string()),
|
|
2059
|
-
messages: o.array(ys)
|
|
2060
|
-
}), ws = oe.extend({
|
|
2061
|
-
message: o.string(),
|
|
2062
|
-
requestedSchema: o.unknown()
|
|
2063
|
-
}), je = Ye.extend({
|
|
2064
|
-
method: o.literal("elicitation/create"),
|
|
2065
|
-
params: ws
|
|
2066
|
-
}), _s = U.extend({
|
|
2067
|
-
action: o.union([
|
|
2068
|
-
o.literal("accept"),
|
|
2069
|
-
o.literal("decline"),
|
|
2070
|
-
o.literal("cancel")
|
|
2071
|
-
]),
|
|
2072
|
-
content: o.optional(o.record(o.string(), o.unknown()))
|
|
2073
|
-
}), ae = "2.0", bs = o.object({
|
|
2074
|
-
jsonrpc: o.literal(ae),
|
|
2075
|
-
id: o.union([o.string(), o.number().int()])
|
|
2076
|
-
}).merge(Ye).strict(), Ss = o.object({
|
|
2077
|
-
jsonrpc: o.literal(ae),
|
|
2078
|
-
id: o.union([o.string(), o.number().int()]),
|
|
2079
|
-
result: U
|
|
2080
|
-
}).strict(), Ts = o.object({
|
|
2081
|
-
jsonrpc: o.literal(ae),
|
|
2082
|
-
id: o.union([o.string(), o.number().int()]),
|
|
2083
|
-
error: o.object({
|
|
2084
|
-
code: o.number().int(),
|
|
2085
|
-
message: o.string(),
|
|
2086
|
-
data: o.optional(o.unknown())
|
|
2087
|
-
})
|
|
2088
|
-
}).strict(), Cs = o.object({
|
|
2089
|
-
jsonrpc: o.literal(ae)
|
|
2090
|
-
}).merge(
|
|
2091
|
-
o.object({
|
|
2092
|
-
method: o.string(),
|
|
2093
|
-
params: o.optional(oe)
|
|
2094
|
-
})
|
|
2095
|
-
).strict(), V = o.union([
|
|
2096
|
-
bs,
|
|
2097
|
-
Cs,
|
|
2098
|
-
Ss,
|
|
2099
|
-
Ts
|
|
2100
|
-
]), it = typeof __PACKAGE_VERSION__ < "u" ? __PACKAGE_VERSION__ : "0.0.0-test", ct = o.object({
|
|
2101
|
-
access_token: o.string(),
|
|
2102
|
-
id_token: o.string().optional(),
|
|
2103
|
-
// Optional for OAuth 2.1, but necessary in OpenID Connect
|
|
2104
|
-
token_type: o.string(),
|
|
2105
|
-
expires_in: o.number().optional(),
|
|
2106
|
-
scope: o.string().optional(),
|
|
2107
|
-
refresh_token: o.string().optional()
|
|
2108
|
-
}).strip(), R = o.string().url().superRefine((t, e) => {
|
|
2109
|
-
if (!URL.canParse(t))
|
|
2110
|
-
return e.addIssue({
|
|
2111
|
-
code: o.ZodIssueCode.custom,
|
|
2112
|
-
message: "URL must be parseable",
|
|
2113
|
-
fatal: !0
|
|
2114
|
-
}), o.NEVER;
|
|
2115
|
-
}).refine(
|
|
2116
|
-
(t) => {
|
|
2117
|
-
const e = new URL(t);
|
|
2118
|
-
return e.protocol !== "javascript:" && e.protocol !== "data:" && e.protocol !== "vbscript:";
|
|
2119
|
-
},
|
|
2120
|
-
{ message: "URL cannot use javascript:, data:, or vbscript: scheme" }
|
|
2121
|
-
), Ps = o.object({
|
|
2122
|
-
resource: o.string().url(),
|
|
2123
|
-
authorization_servers: o.array(R).optional(),
|
|
2124
|
-
jwks_uri: o.string().url().optional(),
|
|
2125
|
-
scopes_supported: o.array(o.string()).optional(),
|
|
2126
|
-
bearer_methods_supported: o.array(o.string()).optional(),
|
|
2127
|
-
resource_signing_alg_values_supported: o.array(o.string()).optional(),
|
|
2128
|
-
resource_name: o.string().optional(),
|
|
2129
|
-
resource_documentation: o.string().optional(),
|
|
2130
|
-
resource_policy_uri: o.string().url().optional(),
|
|
2131
|
-
resource_tos_uri: o.string().url().optional(),
|
|
2132
|
-
tls_client_certificate_bound_access_tokens: o.boolean().optional(),
|
|
2133
|
-
authorization_details_types_supported: o.array(o.string()).optional(),
|
|
2134
|
-
dpop_signing_alg_values_supported: o.array(o.string()).optional(),
|
|
2135
|
-
dpop_bound_access_tokens_required: o.boolean().optional()
|
|
2136
|
-
}).passthrough(), lt = o.object({
|
|
2137
|
-
issuer: o.string(),
|
|
2138
|
-
authorization_endpoint: R,
|
|
2139
|
-
token_endpoint: R,
|
|
2140
|
-
registration_endpoint: R.optional(),
|
|
2141
|
-
scopes_supported: o.array(o.string()).optional(),
|
|
2142
|
-
response_types_supported: o.array(o.string()),
|
|
2143
|
-
grant_types_supported: o.array(o.string()).optional(),
|
|
2144
|
-
code_challenge_methods_supported: o.array(o.string()),
|
|
2145
|
-
token_endpoint_auth_methods_supported: o.array(o.string()).optional(),
|
|
2146
|
-
token_endpoint_auth_signing_alg_values_supported: o.array(o.string()).optional()
|
|
2147
|
-
}).passthrough(), Rs = o.object({
|
|
2148
|
-
issuer: o.string(),
|
|
2149
|
-
authorization_endpoint: R,
|
|
2150
|
-
token_endpoint: R,
|
|
2151
|
-
userinfo_endpoint: R.optional(),
|
|
2152
|
-
jwks_uri: R,
|
|
2153
|
-
registration_endpoint: R.optional(),
|
|
2154
|
-
scopes_supported: o.array(o.string()).optional(),
|
|
2155
|
-
response_types_supported: o.array(o.string()),
|
|
2156
|
-
grant_types_supported: o.array(o.string()).optional(),
|
|
2157
|
-
subject_types_supported: o.array(o.string()),
|
|
2158
|
-
id_token_signing_alg_values_supported: o.array(o.string()),
|
|
2159
|
-
claims_supported: o.array(o.string()).optional(),
|
|
2160
|
-
token_endpoint_auth_methods_supported: o.array(o.string()).optional()
|
|
2161
|
-
}).passthrough(), Ms = Rs.merge(
|
|
2162
|
-
lt.pick({
|
|
2163
|
-
code_challenge_methods_supported: !0
|
|
2164
|
-
})
|
|
2165
|
-
), Es = o.object({
|
|
2166
|
-
client_id: o.string(),
|
|
2167
|
-
client_secret: o.string().optional(),
|
|
2168
|
-
client_id_issued_at: o.number().optional(),
|
|
2169
|
-
client_secret_expires_at: o.number().optional()
|
|
2170
|
-
}).strip(), As = o.object({
|
|
2171
|
-
redirect_uris: o.array(R),
|
|
2172
|
-
token_endpoint_auth_method: o.string().optional(),
|
|
2173
|
-
grant_types: o.array(o.string()).optional(),
|
|
2174
|
-
response_types: o.array(o.string()).optional(),
|
|
2175
|
-
client_name: o.string().optional(),
|
|
2176
|
-
client_uri: R.optional(),
|
|
2177
|
-
logo_uri: R.optional(),
|
|
2178
|
-
scope: o.string().optional(),
|
|
2179
|
-
contacts: o.array(o.string()).optional(),
|
|
2180
|
-
tos_uri: R.optional(),
|
|
2181
|
-
policy_uri: o.string().optional(),
|
|
2182
|
-
jwks_uri: R.optional(),
|
|
2183
|
-
jwks: o.any().optional(),
|
|
2184
|
-
software_id: o.string().optional(),
|
|
2185
|
-
software_version: o.string().optional(),
|
|
2186
|
-
software_statement: o.string().optional()
|
|
2187
|
-
}).strip(), ks = o.object({
|
|
2188
|
-
error: o.string(),
|
|
2189
|
-
error_description: o.string().optional(),
|
|
2190
|
-
error_uri: o.string().optional()
|
|
2191
|
-
}), Os = As.merge(
|
|
2192
|
-
Es
|
|
2193
|
-
), xs = "AI_MCPClientOAuthError", ut = `vercel.ai.error.${xs}`, Is = Symbol.for(ut), Le, Ue, W = class extends (Ue = K, Le = Is, Ue) {
|
|
2194
|
-
constructor({
|
|
2195
|
-
name: t = "MCPClientOAuthError",
|
|
2196
|
-
message: e,
|
|
2197
|
-
cause: r
|
|
2198
|
-
}) {
|
|
2199
|
-
super({ name: t, message: e, cause: r }), this[Le] = !0;
|
|
2200
|
-
}
|
|
2201
|
-
static isInstance(t) {
|
|
2202
|
-
return K.hasMarker(t, ut);
|
|
2203
|
-
}
|
|
2204
|
-
}, F = class extends W {
|
|
2205
|
-
};
|
|
2206
|
-
F.errorCode = "server_error";
|
|
2207
|
-
var X = class extends W {
|
|
2208
|
-
};
|
|
2209
|
-
X.errorCode = "invalid_client";
|
|
2210
|
-
var Q = class extends W {
|
|
2211
|
-
};
|
|
2212
|
-
Q.errorCode = "invalid_grant";
|
|
2213
|
-
var Y = class extends W {
|
|
2214
|
-
};
|
|
2215
|
-
Y.errorCode = "unauthorized_client";
|
|
2216
|
-
var js = {
|
|
2217
|
-
[F.errorCode]: F,
|
|
2218
|
-
[X.errorCode]: X,
|
|
2219
|
-
[Q.errorCode]: Q,
|
|
2220
|
-
[Y.errorCode]: Y
|
|
2221
|
-
};
|
|
2222
|
-
function Ls(t) {
|
|
2223
|
-
const e = typeof t == "string" ? new URL(t) : new URL(t.href);
|
|
2224
|
-
return e.hash = "", e;
|
|
2225
|
-
}
|
|
2226
|
-
function be(t) {
|
|
2227
|
-
const e = t.href;
|
|
2228
|
-
return t.pathname === "/" && e.endsWith("/") ? e.slice(0, -1) : e;
|
|
2229
|
-
}
|
|
2230
|
-
function Us({
|
|
2231
|
-
requestedResource: t,
|
|
2232
|
-
configuredResource: e
|
|
2233
|
-
}) {
|
|
2234
|
-
const r = typeof t == "string" ? new URL(t) : new URL(t.href), s = typeof e == "string" ? new URL(e) : new URL(e.href);
|
|
2235
|
-
if (r.origin !== s.origin || r.pathname.length < s.pathname.length)
|
|
2236
|
-
return !1;
|
|
2237
|
-
const n = r.pathname.endsWith("/") ? r.pathname : r.pathname + "/", a = s.pathname.endsWith("/") ? s.pathname : s.pathname + "/";
|
|
2238
|
-
return n.startsWith(a);
|
|
2239
|
-
}
|
|
2240
|
-
var ee = class extends Error {
|
|
2241
|
-
constructor(t = "Unauthorized") {
|
|
2242
|
-
super(t), this.name = "UnauthorizedError";
|
|
2243
|
-
}
|
|
2244
|
-
};
|
|
2245
|
-
function te(t) {
|
|
2246
|
-
var e;
|
|
2247
|
-
const r = (e = t.headers.get("www-authenticate")) != null ? e : t.headers.get("WWW-Authenticate");
|
|
2248
|
-
if (!r)
|
|
2249
|
-
return;
|
|
2250
|
-
const [s, n] = r.split(" ");
|
|
2251
|
-
if (s.toLowerCase() !== "bearer" || !n)
|
|
2252
|
-
return;
|
|
2253
|
-
const a = /resource_metadata="([^"]*)"/, i = r.match(a);
|
|
2254
|
-
if (i)
|
|
2255
|
-
try {
|
|
2256
|
-
return new URL(i[1]);
|
|
2257
|
-
} catch {
|
|
2258
|
-
return;
|
|
2259
|
-
}
|
|
2260
|
-
}
|
|
2261
|
-
function $s(t, e = "", r = {}) {
|
|
2262
|
-
return e.endsWith("/") && (e = e.slice(0, -1)), r.prependPathname ? `${e}/.well-known/${t}` : `/.well-known/${t}${e}`;
|
|
2263
|
-
}
|
|
2264
|
-
async function Se(t, e, r = fetch) {
|
|
2265
|
-
try {
|
|
2266
|
-
return await r(t, { headers: e });
|
|
2267
|
-
} catch (s) {
|
|
2268
|
-
if (s instanceof TypeError)
|
|
2269
|
-
return e ? Se(t, void 0, r) : void 0;
|
|
2270
|
-
throw s;
|
|
2271
|
-
}
|
|
2272
|
-
}
|
|
2273
|
-
async function $e(t, e, r = fetch) {
|
|
2274
|
-
return await Se(t, {
|
|
2275
|
-
"MCP-Protocol-Version": e
|
|
2276
|
-
}, r);
|
|
2277
|
-
}
|
|
2278
|
-
function Ns(t, e) {
|
|
2279
|
-
return !t || t.status >= 400 && t.status < 500 && e !== "/";
|
|
2280
|
-
}
|
|
2281
|
-
async function qs(t, e, r, s) {
|
|
2282
|
-
var n, a;
|
|
2283
|
-
const i = new URL(t), u = (n = s?.protocolVersion) != null ? n : Z;
|
|
2284
|
-
let c;
|
|
2285
|
-
if (s?.metadataUrl)
|
|
2286
|
-
c = new URL(s.metadataUrl);
|
|
2287
|
-
else {
|
|
2288
|
-
const p = $s(e, i.pathname);
|
|
2289
|
-
c = new URL(p, (a = s?.metadataServerUrl) != null ? a : i), c.search = i.search;
|
|
2290
|
-
}
|
|
2291
|
-
let l = await $e(c, u, r);
|
|
2292
|
-
if (!s?.metadataUrl && Ns(l, i.pathname)) {
|
|
2293
|
-
const p = new URL(`/.well-known/${e}`, i);
|
|
2294
|
-
l = await $e(p, u, r);
|
|
2295
|
-
}
|
|
2296
|
-
return l;
|
|
2297
|
-
}
|
|
2298
|
-
async function Hs(t, e, r = fetch) {
|
|
2299
|
-
const s = await qs(
|
|
2300
|
-
t,
|
|
2301
|
-
"oauth-protected-resource",
|
|
2302
|
-
r,
|
|
2303
|
-
{
|
|
2304
|
-
protocolVersion: e?.protocolVersion,
|
|
2305
|
-
metadataUrl: e?.resourceMetadataUrl
|
|
2306
|
-
}
|
|
2307
|
-
);
|
|
2308
|
-
if (!s || s.status === 404)
|
|
2309
|
-
throw new Error(
|
|
2310
|
-
"Resource server does not implement OAuth 2.0 Protected Resource Metadata."
|
|
2311
|
-
);
|
|
2312
|
-
if (!s.ok)
|
|
2313
|
-
throw new Error(
|
|
2314
|
-
`HTTP ${s.status} trying to load well-known OAuth protected resource metadata.`
|
|
2315
|
-
);
|
|
2316
|
-
return Ps.parse(await s.json());
|
|
2317
|
-
}
|
|
2318
|
-
function Fs(t) {
|
|
2319
|
-
const e = typeof t == "string" ? new URL(t) : t, r = e.pathname !== "/", s = [];
|
|
2320
|
-
if (!r)
|
|
2321
|
-
return s.push({
|
|
2322
|
-
url: new URL("/.well-known/oauth-authorization-server", e.origin),
|
|
2323
|
-
type: "oauth"
|
|
2324
|
-
}), s.push({
|
|
2325
|
-
url: new URL("/.well-known/openid-configuration", e.origin),
|
|
2326
|
-
type: "oidc"
|
|
2327
|
-
}), s;
|
|
2328
|
-
let n = e.pathname;
|
|
2329
|
-
return n.endsWith("/") && (n = n.slice(0, -1)), s.push({
|
|
2330
|
-
url: new URL(
|
|
2331
|
-
`/.well-known/oauth-authorization-server${n}`,
|
|
2332
|
-
e.origin
|
|
2333
|
-
),
|
|
2334
|
-
type: "oauth"
|
|
2335
|
-
}), s.push({
|
|
2336
|
-
url: new URL("/.well-known/oauth-authorization-server", e.origin),
|
|
2337
|
-
type: "oauth"
|
|
2338
|
-
}), s.push({
|
|
2339
|
-
url: new URL(`/.well-known/openid-configuration${n}`, e.origin),
|
|
2340
|
-
type: "oidc"
|
|
2341
|
-
}), s.push({
|
|
2342
|
-
url: new URL(`${n}/.well-known/openid-configuration`, e.origin),
|
|
2343
|
-
type: "oidc"
|
|
2344
|
-
}), s;
|
|
2345
|
-
}
|
|
2346
|
-
async function Zs(t, {
|
|
2347
|
-
fetchFn: e = fetch,
|
|
2348
|
-
protocolVersion: r = Z
|
|
2349
|
-
} = {}) {
|
|
2350
|
-
var s;
|
|
2351
|
-
const n = { "MCP-Protocol-Version": r }, a = Fs(t);
|
|
2352
|
-
for (const { url: i, type: u } of a) {
|
|
2353
|
-
const c = await Se(i, n, e);
|
|
2354
|
-
if (c) {
|
|
2355
|
-
if (!c.ok) {
|
|
2356
|
-
if (c.status >= 400 && c.status < 500)
|
|
2357
|
-
continue;
|
|
2358
|
-
throw new Error(
|
|
2359
|
-
`HTTP ${c.status} trying to load ${u === "oauth" ? "OAuth" : "OpenID provider"} metadata from ${i}`
|
|
2360
|
-
);
|
|
2361
|
-
}
|
|
2362
|
-
if (u === "oauth")
|
|
2363
|
-
return lt.parse(await c.json());
|
|
2364
|
-
{
|
|
2365
|
-
const l = Ms.parse(
|
|
2366
|
-
await c.json()
|
|
2367
|
-
);
|
|
2368
|
-
if (!((s = l.code_challenge_methods_supported) != null && s.includes("S256")))
|
|
2369
|
-
throw new Error(
|
|
2370
|
-
`Incompatible OIDC provider at ${i}: does not support S256 code challenge method required by MCP specification`
|
|
2371
|
-
);
|
|
2372
|
-
return l;
|
|
2373
|
-
}
|
|
2374
|
-
}
|
|
2375
|
-
}
|
|
2376
|
-
}
|
|
2377
|
-
async function Ds(t, {
|
|
2378
|
-
metadata: e,
|
|
2379
|
-
clientInformation: r,
|
|
2380
|
-
redirectUrl: s,
|
|
2381
|
-
scope: n,
|
|
2382
|
-
state: a,
|
|
2383
|
-
resource: i
|
|
2384
|
-
}) {
|
|
2385
|
-
const u = "code", c = "S256";
|
|
2386
|
-
let l;
|
|
2387
|
-
if (e) {
|
|
2388
|
-
if (l = new URL(e.authorization_endpoint), !e.response_types_supported.includes(u))
|
|
2389
|
-
throw new Error(
|
|
2390
|
-
`Incompatible auth server: does not support response type ${u}`
|
|
2391
|
-
);
|
|
2392
|
-
if (!e.code_challenge_methods_supported || !e.code_challenge_methods_supported.includes(c))
|
|
2393
|
-
throw new Error(
|
|
2394
|
-
`Incompatible auth server: does not support code challenge method ${c}`
|
|
2395
|
-
);
|
|
2396
|
-
} else
|
|
2397
|
-
l = new URL("/authorize", t);
|
|
2398
|
-
const p = await Qr(), m = p.code_verifier, d = p.code_challenge;
|
|
2399
|
-
return l.searchParams.set("response_type", u), l.searchParams.set("client_id", r.client_id), l.searchParams.set("code_challenge", d), l.searchParams.set(
|
|
2400
|
-
"code_challenge_method",
|
|
2401
|
-
c
|
|
2402
|
-
), l.searchParams.set("redirect_uri", String(s)), a && l.searchParams.set("state", a), n && l.searchParams.set("scope", n), n?.includes("offline_access") && l.searchParams.append("prompt", "consent"), i && l.searchParams.set(
|
|
2403
|
-
"resource",
|
|
2404
|
-
be(i)
|
|
2405
|
-
), { authorizationUrl: l, codeVerifier: m };
|
|
2406
|
-
}
|
|
2407
|
-
function pt(t, e) {
|
|
2408
|
-
const r = t.client_secret !== void 0;
|
|
2409
|
-
return e.length === 0 ? r ? "client_secret_post" : "none" : r && e.includes("client_secret_basic") ? "client_secret_basic" : r && e.includes("client_secret_post") ? "client_secret_post" : e.includes("none") ? "none" : r ? "client_secret_post" : "none";
|
|
2410
|
-
}
|
|
2411
|
-
function dt(t, e, r, s) {
|
|
2412
|
-
const { client_id: n, client_secret: a } = e;
|
|
2413
|
-
switch (t) {
|
|
2414
|
-
case "client_secret_basic":
|
|
2415
|
-
zs(n, a, r);
|
|
2416
|
-
return;
|
|
2417
|
-
case "client_secret_post":
|
|
2418
|
-
Vs(n, a, s);
|
|
2419
|
-
return;
|
|
2420
|
-
case "none":
|
|
2421
|
-
Js(n, s);
|
|
2422
|
-
return;
|
|
2423
|
-
default:
|
|
2424
|
-
throw new Error(`Unsupported client authentication method: ${t}`);
|
|
2425
|
-
}
|
|
2426
|
-
}
|
|
2427
|
-
function zs(t, e, r) {
|
|
2428
|
-
if (!e)
|
|
2429
|
-
throw new Error(
|
|
2430
|
-
"client_secret_basic authentication requires a client_secret"
|
|
2431
|
-
);
|
|
2432
|
-
const s = btoa(`${t}:${e}`);
|
|
2433
|
-
r.set("Authorization", `Basic ${s}`);
|
|
2434
|
-
}
|
|
2435
|
-
function Vs(t, e, r) {
|
|
2436
|
-
r.set("client_id", t), e && r.set("client_secret", e);
|
|
2437
|
-
}
|
|
2438
|
-
function Js(t, e) {
|
|
2439
|
-
e.set("client_id", t);
|
|
2440
|
-
}
|
|
2441
|
-
async function Te(t) {
|
|
2442
|
-
const e = t instanceof Response ? t.status : void 0, r = t instanceof Response ? await t.text() : t;
|
|
2443
|
-
try {
|
|
2444
|
-
const s = ks.parse(JSON.parse(r)), { error: n, error_description: a, error_uri: i } = s, u = js[n] || F;
|
|
2445
|
-
return new u({
|
|
2446
|
-
message: a || "",
|
|
2447
|
-
cause: i
|
|
2448
|
-
});
|
|
2449
|
-
} catch (s) {
|
|
2450
|
-
const n = `${e ? `HTTP ${e}: ` : ""}Invalid OAuth error response: ${s}. Raw body: ${r}`;
|
|
2451
|
-
return new F({ message: n });
|
|
2452
|
-
}
|
|
2453
|
-
}
|
|
2454
|
-
async function Ws(t, {
|
|
2455
|
-
metadata: e,
|
|
2456
|
-
clientInformation: r,
|
|
2457
|
-
authorizationCode: s,
|
|
2458
|
-
codeVerifier: n,
|
|
2459
|
-
redirectUri: a,
|
|
2460
|
-
resource: i,
|
|
2461
|
-
addClientAuthentication: u,
|
|
2462
|
-
fetchFn: c
|
|
2463
|
-
}) {
|
|
2464
|
-
var l;
|
|
2465
|
-
const p = "authorization_code", m = e?.token_endpoint ? new URL(e.token_endpoint) : new URL("/token", t);
|
|
2466
|
-
if (e?.grant_types_supported && !e.grant_types_supported.includes(p))
|
|
2467
|
-
throw new Error(
|
|
2468
|
-
`Incompatible auth server: does not support grant type ${p}`
|
|
2469
|
-
);
|
|
2470
|
-
const d = new Headers({
|
|
2471
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
2472
|
-
Accept: "application/json"
|
|
2473
|
-
}), f = new URLSearchParams({
|
|
2474
|
-
grant_type: p,
|
|
2475
|
-
code: s,
|
|
2476
|
-
code_verifier: n,
|
|
2477
|
-
redirect_uri: String(a)
|
|
2478
|
-
});
|
|
2479
|
-
if (u)
|
|
2480
|
-
u(d, f, t, e);
|
|
2481
|
-
else {
|
|
2482
|
-
const h = (l = e?.token_endpoint_auth_methods_supported) != null ? l : [], g = pt(
|
|
2483
|
-
r,
|
|
2484
|
-
h
|
|
2485
|
-
);
|
|
2486
|
-
dt(g, r, d, f);
|
|
2487
|
-
}
|
|
2488
|
-
i && f.set("resource", be(i));
|
|
2489
|
-
const y = await (c ?? fetch)(m, {
|
|
2490
|
-
method: "POST",
|
|
2491
|
-
headers: d,
|
|
2492
|
-
body: f
|
|
2493
|
-
});
|
|
2494
|
-
if (!y.ok)
|
|
2495
|
-
throw await Te(y);
|
|
2496
|
-
return ct.parse(await y.json());
|
|
2497
|
-
}
|
|
2498
|
-
async function Bs(t, {
|
|
2499
|
-
metadata: e,
|
|
2500
|
-
clientInformation: r,
|
|
2501
|
-
refreshToken: s,
|
|
2502
|
-
resource: n,
|
|
2503
|
-
addClientAuthentication: a,
|
|
2504
|
-
fetchFn: i
|
|
2505
|
-
}) {
|
|
2506
|
-
var u;
|
|
2507
|
-
const c = "refresh_token";
|
|
2508
|
-
let l;
|
|
2509
|
-
if (e) {
|
|
2510
|
-
if (l = new URL(e.token_endpoint), e.grant_types_supported && !e.grant_types_supported.includes(c))
|
|
2511
|
-
throw new Error(
|
|
2512
|
-
`Incompatible auth server: does not support grant type ${c}`
|
|
2513
|
-
);
|
|
2514
|
-
} else
|
|
2515
|
-
l = new URL("/token", t);
|
|
2516
|
-
const p = new Headers({
|
|
2517
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
2518
|
-
Accept: "application/json"
|
|
2519
|
-
}), m = new URLSearchParams({
|
|
2520
|
-
grant_type: c,
|
|
2521
|
-
refresh_token: s
|
|
2522
|
-
});
|
|
2523
|
-
if (a)
|
|
2524
|
-
a(p, m, t, e);
|
|
2525
|
-
else {
|
|
2526
|
-
const f = (u = e?.token_endpoint_auth_methods_supported) != null ? u : [], y = pt(
|
|
2527
|
-
r,
|
|
2528
|
-
f
|
|
2529
|
-
);
|
|
2530
|
-
dt(y, r, p, m);
|
|
2531
|
-
}
|
|
2532
|
-
n && m.set("resource", be(n));
|
|
2533
|
-
const d = await (i ?? fetch)(l, {
|
|
2534
|
-
method: "POST",
|
|
2535
|
-
headers: p,
|
|
2536
|
-
body: m
|
|
2537
|
-
});
|
|
2538
|
-
if (!d.ok)
|
|
2539
|
-
throw await Te(d);
|
|
2540
|
-
return ct.parse({
|
|
2541
|
-
refresh_token: s,
|
|
2542
|
-
...await d.json()
|
|
2543
|
-
});
|
|
2544
|
-
}
|
|
2545
|
-
async function Gs(t, {
|
|
2546
|
-
metadata: e,
|
|
2547
|
-
clientMetadata: r,
|
|
2548
|
-
fetchFn: s
|
|
2549
|
-
}) {
|
|
2550
|
-
let n;
|
|
2551
|
-
if (e) {
|
|
2552
|
-
if (!e.registration_endpoint)
|
|
2553
|
-
throw new Error(
|
|
2554
|
-
"Incompatible auth server: does not support dynamic client registration"
|
|
2555
|
-
);
|
|
2556
|
-
n = new URL(e.registration_endpoint);
|
|
2557
|
-
} else
|
|
2558
|
-
n = new URL("/register", t);
|
|
2559
|
-
const a = await (s ?? fetch)(n, {
|
|
2560
|
-
method: "POST",
|
|
2561
|
-
headers: {
|
|
2562
|
-
"Content-Type": "application/json"
|
|
2563
|
-
},
|
|
2564
|
-
body: JSON.stringify(r)
|
|
2565
|
-
});
|
|
2566
|
-
if (!a.ok)
|
|
2567
|
-
throw await Te(a);
|
|
2568
|
-
return Os.parse(await a.json());
|
|
2569
|
-
}
|
|
2570
|
-
async function re(t, e) {
|
|
2571
|
-
var r, s;
|
|
2572
|
-
try {
|
|
2573
|
-
return await pe(t, e);
|
|
2574
|
-
} catch (n) {
|
|
2575
|
-
if (n instanceof X || n instanceof Y)
|
|
2576
|
-
return await ((r = t.invalidateCredentials) == null ? void 0 : r.call(t, "all")), await pe(t, e);
|
|
2577
|
-
if (n instanceof Q)
|
|
2578
|
-
return await ((s = t.invalidateCredentials) == null ? void 0 : s.call(t, "tokens")), await pe(t, e);
|
|
2579
|
-
throw n;
|
|
2580
|
-
}
|
|
2581
|
-
}
|
|
2582
|
-
async function Ks(t, e, r) {
|
|
2583
|
-
const s = Ls(t);
|
|
2584
|
-
if (e.validateResourceURL)
|
|
2585
|
-
return await e.validateResourceURL(
|
|
2586
|
-
s,
|
|
2587
|
-
r?.resource
|
|
2588
|
-
);
|
|
2589
|
-
if (r) {
|
|
2590
|
-
if (!Us({
|
|
2591
|
-
requestedResource: s,
|
|
2592
|
-
configuredResource: r.resource
|
|
2593
|
-
}))
|
|
2594
|
-
throw new Error(
|
|
2595
|
-
`Protected resource ${r.resource} does not match expected ${s} (or origin)`
|
|
2596
|
-
);
|
|
2597
|
-
return new URL(r.resource);
|
|
2598
|
-
}
|
|
2599
|
-
}
|
|
2600
|
-
async function pe(t, {
|
|
2601
|
-
serverUrl: e,
|
|
2602
|
-
authorizationCode: r,
|
|
2603
|
-
callbackState: s,
|
|
2604
|
-
scope: n,
|
|
2605
|
-
resourceMetadataUrl: a,
|
|
2606
|
-
fetchFn: i
|
|
2607
|
-
}) {
|
|
2608
|
-
let u, c;
|
|
2609
|
-
try {
|
|
2610
|
-
u = await Hs(
|
|
2611
|
-
e,
|
|
2612
|
-
{ resourceMetadataUrl: a },
|
|
2613
|
-
i
|
|
2614
|
-
), u.authorization_servers && u.authorization_servers.length > 0 && (c = u.authorization_servers[0]);
|
|
2615
|
-
} catch {
|
|
2616
|
-
}
|
|
2617
|
-
c || (c = e);
|
|
2618
|
-
const l = await Ks(
|
|
2619
|
-
e,
|
|
2620
|
-
t,
|
|
2621
|
-
u
|
|
2622
|
-
), p = await Zs(
|
|
2623
|
-
c,
|
|
2624
|
-
{
|
|
2625
|
-
fetchFn: i
|
|
2626
|
-
}
|
|
2627
|
-
);
|
|
2628
|
-
let m = await Promise.resolve(t.clientInformation());
|
|
2629
|
-
if (!m) {
|
|
2630
|
-
if (r !== void 0)
|
|
2631
|
-
throw new Error(
|
|
2632
|
-
"Existing OAuth client information is required when exchanging an authorization code"
|
|
2633
|
-
);
|
|
2634
|
-
if (!t.saveClientInformation)
|
|
2635
|
-
throw new Error(
|
|
2636
|
-
"OAuth client information must be saveable for dynamic registration"
|
|
2637
|
-
);
|
|
2638
|
-
const g = await Gs(c, {
|
|
2639
|
-
metadata: p,
|
|
2640
|
-
clientMetadata: t.clientMetadata,
|
|
2641
|
-
fetchFn: i
|
|
2642
|
-
});
|
|
2643
|
-
await t.saveClientInformation(g), m = g;
|
|
2644
|
-
}
|
|
2645
|
-
if (r !== void 0) {
|
|
2646
|
-
if (t.storedState) {
|
|
2647
|
-
const b = await t.storedState();
|
|
2648
|
-
if (b !== void 0 && b !== s)
|
|
2649
|
-
throw new Error(
|
|
2650
|
-
"OAuth state parameter mismatch - possible CSRF attack"
|
|
2651
|
-
);
|
|
2652
|
-
}
|
|
2653
|
-
const g = await t.codeVerifier(), v = await Ws(c, {
|
|
2654
|
-
metadata: p,
|
|
2655
|
-
clientInformation: m,
|
|
2656
|
-
authorizationCode: r,
|
|
2657
|
-
codeVerifier: g,
|
|
2658
|
-
redirectUri: t.redirectUrl,
|
|
2659
|
-
resource: l,
|
|
2660
|
-
addClientAuthentication: t.addClientAuthentication,
|
|
2661
|
-
fetchFn: i
|
|
2662
|
-
});
|
|
2663
|
-
return await t.saveTokens(v), "AUTHORIZED";
|
|
2664
|
-
}
|
|
2665
|
-
const d = await t.tokens();
|
|
2666
|
-
if (d?.refresh_token)
|
|
2667
|
-
try {
|
|
2668
|
-
const g = await Bs(c, {
|
|
2669
|
-
metadata: p,
|
|
2670
|
-
clientInformation: m,
|
|
2671
|
-
refreshToken: d.refresh_token,
|
|
2672
|
-
resource: l,
|
|
2673
|
-
addClientAuthentication: t.addClientAuthentication,
|
|
2674
|
-
fetchFn: i
|
|
2675
|
-
});
|
|
2676
|
-
return await t.saveTokens(g), "AUTHORIZED";
|
|
2677
|
-
} catch (g) {
|
|
2678
|
-
if (
|
|
2679
|
-
// If this is a ServerError, or an unknown type, log it out and try to continue. Otherwise, escalate so we can fix things and retry.
|
|
2680
|
-
!(!(g instanceof W) || g instanceof F)
|
|
2681
|
-
) throw g;
|
|
2682
|
-
}
|
|
2683
|
-
const f = t.state ? await t.state() : void 0;
|
|
2684
|
-
f && t.saveState && await t.saveState(f);
|
|
2685
|
-
const { authorizationUrl: y, codeVerifier: h } = await Ds(
|
|
2686
|
-
c,
|
|
2687
|
-
{
|
|
2688
|
-
metadata: p,
|
|
2689
|
-
clientInformation: m,
|
|
2690
|
-
state: f,
|
|
2691
|
-
redirectUrl: t.redirectUrl,
|
|
2692
|
-
scope: n || t.clientMetadata.scope,
|
|
2693
|
-
resource: l
|
|
2694
|
-
}
|
|
2695
|
-
);
|
|
2696
|
-
return await t.saveCodeVerifier(h), await t.redirectToAuthorization(y), "REDIRECT";
|
|
2697
|
-
}
|
|
2698
|
-
var Xs = class {
|
|
2699
|
-
constructor({
|
|
2700
|
-
url: t,
|
|
2701
|
-
headers: e,
|
|
2702
|
-
authProvider: r,
|
|
2703
|
-
redirect: s = "follow",
|
|
2704
|
-
fetch: n
|
|
2705
|
-
}) {
|
|
2706
|
-
this.connected = !1, this.url = new URL(t), this.headers = e, this.authProvider = r, this.redirectMode = s, this.fetchFn = n ?? globalThis.fetch;
|
|
2707
|
-
}
|
|
2708
|
-
async commonHeaders(t) {
|
|
2709
|
-
const e = {
|
|
2710
|
-
...this.headers,
|
|
2711
|
-
...t,
|
|
2712
|
-
"mcp-protocol-version": Z
|
|
2713
|
-
};
|
|
2714
|
-
if (this.authProvider) {
|
|
2715
|
-
const r = await this.authProvider.tokens();
|
|
2716
|
-
r?.access_token && (e.Authorization = `Bearer ${r.access_token}`);
|
|
2717
|
-
}
|
|
2718
|
-
return Ve(
|
|
2719
|
-
e,
|
|
2720
|
-
`ai-sdk/${it}`,
|
|
2721
|
-
ze()
|
|
2722
|
-
);
|
|
2723
|
-
}
|
|
2724
|
-
async start() {
|
|
2725
|
-
return new Promise((t, e) => {
|
|
2726
|
-
if (this.connected)
|
|
2727
|
-
return t();
|
|
2728
|
-
this.abortController = new AbortController();
|
|
2729
|
-
const r = async (s = !1) => {
|
|
2730
|
-
var n, a, i, u, c;
|
|
2731
|
-
try {
|
|
2732
|
-
const l = await this.commonHeaders({
|
|
2733
|
-
Accept: "text/event-stream"
|
|
2734
|
-
}), p = await this.fetchFn(this.url.href, {
|
|
2735
|
-
headers: l,
|
|
2736
|
-
signal: (n = this.abortController) == null ? void 0 : n.signal,
|
|
2737
|
-
redirect: this.redirectMode
|
|
2738
|
-
});
|
|
2739
|
-
if (p.status === 401 && this.authProvider && !s) {
|
|
2740
|
-
this.resourceMetadataUrl = te(p);
|
|
2741
|
-
try {
|
|
2742
|
-
if (await re(this.authProvider, {
|
|
2743
|
-
serverUrl: this.url,
|
|
2744
|
-
resourceMetadataUrl: this.resourceMetadataUrl,
|
|
2745
|
-
fetchFn: this.fetchFn
|
|
2746
|
-
}) !== "AUTHORIZED") {
|
|
2747
|
-
const h = new ee();
|
|
2748
|
-
return (a = this.onerror) == null || a.call(this, h), e(h);
|
|
2749
|
-
}
|
|
2750
|
-
} catch (y) {
|
|
2751
|
-
return (i = this.onerror) == null || i.call(this, y), e(y);
|
|
2752
|
-
}
|
|
2753
|
-
return r(!0);
|
|
2754
|
-
}
|
|
2755
|
-
if (!p.ok || !p.body) {
|
|
2756
|
-
let y = `MCP SSE Transport Error: ${p.status} ${p.statusText}`;
|
|
2757
|
-
p.status === 405 && (y += ". This server does not support SSE transport. Try using `http` transport instead");
|
|
2758
|
-
const h = new _({
|
|
2759
|
-
message: y
|
|
2760
|
-
});
|
|
2761
|
-
return (u = this.onerror) == null || u.call(this, h), e(h);
|
|
2762
|
-
}
|
|
2763
|
-
const d = p.body.pipeThrough(new TextDecoderStream()).pipeThrough(new he()).getReader(), f = async () => {
|
|
2764
|
-
var y, h, g;
|
|
2765
|
-
try {
|
|
2766
|
-
for (; ; ) {
|
|
2767
|
-
const { done: v, value: b } = await d.read();
|
|
2768
|
-
if (v) {
|
|
2769
|
-
if (this.connected)
|
|
2770
|
-
throw this.connected = !1, new _({
|
|
2771
|
-
message: "MCP SSE Transport Error: Connection closed unexpectedly"
|
|
2772
|
-
});
|
|
2773
|
-
return;
|
|
2774
|
-
}
|
|
2775
|
-
const { event: S, data: A } = b;
|
|
2776
|
-
if (S === "endpoint") {
|
|
2777
|
-
if (this.endpoint = new URL(A, this.url), this.endpoint.origin !== this.url.origin)
|
|
2778
|
-
throw new _({
|
|
2779
|
-
message: `MCP SSE Transport Error: Endpoint origin does not match connection origin: ${this.endpoint.origin}`
|
|
2780
|
-
});
|
|
2781
|
-
this.connected = !0, t();
|
|
2782
|
-
} else if (S === "message")
|
|
2783
|
-
try {
|
|
2784
|
-
const M = V.parse(
|
|
2785
|
-
JSON.parse(A)
|
|
2786
|
-
);
|
|
2787
|
-
(y = this.onmessage) == null || y.call(this, M);
|
|
2788
|
-
} catch (M) {
|
|
2789
|
-
const k = new _({
|
|
2790
|
-
message: "MCP SSE Transport Error: Failed to parse message",
|
|
2791
|
-
cause: M
|
|
2792
|
-
});
|
|
2793
|
-
(h = this.onerror) == null || h.call(this, k);
|
|
2794
|
-
}
|
|
2795
|
-
}
|
|
2796
|
-
} catch (v) {
|
|
2797
|
-
if (v instanceof Error && v.name === "AbortError")
|
|
2798
|
-
return;
|
|
2799
|
-
(g = this.onerror) == null || g.call(this, v), e(v);
|
|
2800
|
-
}
|
|
2801
|
-
};
|
|
2802
|
-
this.sseConnection = {
|
|
2803
|
-
close: () => d.cancel()
|
|
2804
|
-
}, f();
|
|
2805
|
-
} catch (l) {
|
|
2806
|
-
if (l instanceof Error && l.name === "AbortError")
|
|
2807
|
-
return;
|
|
2808
|
-
(c = this.onerror) == null || c.call(this, l), e(l);
|
|
2809
|
-
}
|
|
2810
|
-
};
|
|
2811
|
-
r();
|
|
2812
|
-
});
|
|
2813
|
-
}
|
|
2814
|
-
async close() {
|
|
2815
|
-
var t, e, r;
|
|
2816
|
-
this.connected = !1, (t = this.sseConnection) == null || t.close(), (e = this.abortController) == null || e.abort(), (r = this.onclose) == null || r.call(this);
|
|
2817
|
-
}
|
|
2818
|
-
async send(t) {
|
|
2819
|
-
if (!this.endpoint || !this.connected)
|
|
2820
|
-
throw new _({
|
|
2821
|
-
message: "MCP SSE Transport Error: Not connected"
|
|
2822
|
-
});
|
|
2823
|
-
const e = this.endpoint, r = async (s = !1) => {
|
|
2824
|
-
var n, a, i, u, c;
|
|
2825
|
-
try {
|
|
2826
|
-
const p = {
|
|
2827
|
-
method: "POST",
|
|
2828
|
-
headers: await this.commonHeaders({
|
|
2829
|
-
"Content-Type": "application/json"
|
|
2830
|
-
}),
|
|
2831
|
-
body: JSON.stringify(t),
|
|
2832
|
-
signal: (n = this.abortController) == null ? void 0 : n.signal,
|
|
2833
|
-
redirect: this.redirectMode
|
|
2834
|
-
}, m = await this.fetchFn(e.href, p);
|
|
2835
|
-
if (m.status === 401 && this.authProvider && !s) {
|
|
2836
|
-
this.resourceMetadataUrl = te(m);
|
|
2837
|
-
try {
|
|
2838
|
-
if (await re(this.authProvider, {
|
|
2839
|
-
serverUrl: this.url,
|
|
2840
|
-
resourceMetadataUrl: this.resourceMetadataUrl,
|
|
2841
|
-
fetchFn: this.fetchFn
|
|
2842
|
-
}) !== "AUTHORIZED") {
|
|
2843
|
-
const f = new ee();
|
|
2844
|
-
(a = this.onerror) == null || a.call(this, f);
|
|
2845
|
-
return;
|
|
2846
|
-
}
|
|
2847
|
-
} catch (d) {
|
|
2848
|
-
(i = this.onerror) == null || i.call(this, d);
|
|
2849
|
-
return;
|
|
2850
|
-
}
|
|
2851
|
-
return r(!0);
|
|
2852
|
-
}
|
|
2853
|
-
if (!m.ok) {
|
|
2854
|
-
const d = await m.text().catch(() => null), f = new _({
|
|
2855
|
-
message: `MCP SSE Transport Error: POSTing to endpoint (HTTP ${m.status}): ${d}`
|
|
2856
|
-
});
|
|
2857
|
-
(u = this.onerror) == null || u.call(this, f);
|
|
2858
|
-
return;
|
|
2859
|
-
}
|
|
2860
|
-
} catch (l) {
|
|
2861
|
-
(c = this.onerror) == null || c.call(this, l);
|
|
2862
|
-
return;
|
|
2863
|
-
}
|
|
2864
|
-
};
|
|
2865
|
-
await r();
|
|
2866
|
-
}
|
|
2867
|
-
}, Qs = class {
|
|
2868
|
-
constructor({
|
|
2869
|
-
url: t,
|
|
2870
|
-
headers: e,
|
|
2871
|
-
authProvider: r,
|
|
2872
|
-
redirect: s = "follow",
|
|
2873
|
-
fetch: n
|
|
2874
|
-
}) {
|
|
2875
|
-
this.inboundReconnectAttempts = 0, this.reconnectionOptions = {
|
|
2876
|
-
initialReconnectionDelay: 1e3,
|
|
2877
|
-
maxReconnectionDelay: 3e4,
|
|
2878
|
-
reconnectionDelayGrowFactor: 1.5,
|
|
2879
|
-
maxRetries: 2
|
|
2880
|
-
}, this.url = new URL(t), this.headers = e, this.authProvider = r, this.redirectMode = s, this.fetchFn = n ?? globalThis.fetch;
|
|
2881
|
-
}
|
|
2882
|
-
async commonHeaders(t) {
|
|
2883
|
-
const e = {
|
|
2884
|
-
...this.headers,
|
|
2885
|
-
...t,
|
|
2886
|
-
"mcp-protocol-version": Z
|
|
2887
|
-
};
|
|
2888
|
-
if (this.sessionId && (e["mcp-session-id"] = this.sessionId), this.authProvider) {
|
|
2889
|
-
const r = await this.authProvider.tokens();
|
|
2890
|
-
r?.access_token && (e.Authorization = `Bearer ${r.access_token}`);
|
|
2891
|
-
}
|
|
2892
|
-
return Ve(
|
|
2893
|
-
e,
|
|
2894
|
-
`ai-sdk/${it}`,
|
|
2895
|
-
ze()
|
|
2896
|
-
);
|
|
2897
|
-
}
|
|
2898
|
-
async start() {
|
|
2899
|
-
if (this.abortController)
|
|
2900
|
-
throw new _({
|
|
2901
|
-
message: "MCP HTTP Transport Error: Transport already started. Note: client.connect() calls start() automatically."
|
|
2902
|
-
});
|
|
2903
|
-
this.abortController = new AbortController(), this.openInboundSse();
|
|
2904
|
-
}
|
|
2905
|
-
async close() {
|
|
2906
|
-
var t, e, r;
|
|
2907
|
-
(t = this.inboundSseConnection) == null || t.close();
|
|
2908
|
-
try {
|
|
2909
|
-
if (this.sessionId && this.abortController && !this.abortController.signal.aborted) {
|
|
2910
|
-
const s = await this.commonHeaders({});
|
|
2911
|
-
await this.fetchFn(this.url.href, {
|
|
2912
|
-
method: "DELETE",
|
|
2913
|
-
headers: s,
|
|
2914
|
-
signal: this.abortController.signal,
|
|
2915
|
-
redirect: this.redirectMode
|
|
2916
|
-
}).catch(() => {
|
|
2917
|
-
});
|
|
2918
|
-
}
|
|
2919
|
-
} catch {
|
|
2920
|
-
}
|
|
2921
|
-
(e = this.abortController) == null || e.abort(), (r = this.onclose) == null || r.call(this);
|
|
2922
|
-
}
|
|
2923
|
-
async send(t) {
|
|
2924
|
-
const e = async (r = !1) => {
|
|
2925
|
-
var s, n, a, i, u, c, l;
|
|
2926
|
-
try {
|
|
2927
|
-
const m = {
|
|
2928
|
-
method: "POST",
|
|
2929
|
-
headers: await this.commonHeaders({
|
|
2930
|
-
"Content-Type": "application/json",
|
|
2931
|
-
Accept: "application/json, text/event-stream"
|
|
2932
|
-
}),
|
|
2933
|
-
body: JSON.stringify(t),
|
|
2934
|
-
signal: (s = this.abortController) == null ? void 0 : s.signal,
|
|
2935
|
-
redirect: this.redirectMode
|
|
2936
|
-
}, d = await this.fetchFn(this.url.href, m), f = d.headers.get("mcp-session-id");
|
|
2937
|
-
if (f && (this.sessionId = f), d.status === 401 && this.authProvider && !r) {
|
|
2938
|
-
this.resourceMetadataUrl = te(d);
|
|
2939
|
-
try {
|
|
2940
|
-
if (await re(this.authProvider, {
|
|
2941
|
-
serverUrl: this.url,
|
|
2942
|
-
resourceMetadataUrl: this.resourceMetadataUrl,
|
|
2943
|
-
fetchFn: this.fetchFn
|
|
2944
|
-
}) !== "AUTHORIZED")
|
|
2945
|
-
throw new ee();
|
|
2946
|
-
} catch (v) {
|
|
2947
|
-
throw (n = this.onerror) == null || n.call(this, v), v;
|
|
2948
|
-
}
|
|
2949
|
-
return e(!0);
|
|
2950
|
-
}
|
|
2951
|
-
if (d.status === 202) {
|
|
2952
|
-
this.inboundSseConnection || this.openInboundSse();
|
|
2953
|
-
return;
|
|
2954
|
-
}
|
|
2955
|
-
if (!d.ok) {
|
|
2956
|
-
const v = await d.text().catch(() => null);
|
|
2957
|
-
let b = `MCP HTTP Transport Error: POSTing to endpoint (HTTP ${d.status}): ${v}`;
|
|
2958
|
-
d.status === 404 && (b += ". This server does not support HTTP transport. Try using `sse` transport instead");
|
|
2959
|
-
const S = new _({
|
|
2960
|
-
message: b
|
|
2961
|
-
});
|
|
2962
|
-
throw (a = this.onerror) == null || a.call(this, S), S;
|
|
2963
|
-
}
|
|
2964
|
-
if (!("id" in t))
|
|
2965
|
-
return;
|
|
2966
|
-
const h = d.headers.get("content-type") || "";
|
|
2967
|
-
if (h.includes("application/json")) {
|
|
2968
|
-
const v = await d.json(), b = Array.isArray(v) ? v.map((S) => V.parse(S)) : [V.parse(v)];
|
|
2969
|
-
for (const S of b) (i = this.onmessage) == null || i.call(this, S);
|
|
2970
|
-
return;
|
|
2971
|
-
}
|
|
2972
|
-
if (h.includes("text/event-stream")) {
|
|
2973
|
-
if (!d.body) {
|
|
2974
|
-
const A = new _({
|
|
2975
|
-
message: "MCP HTTP Transport Error: text/event-stream response without body"
|
|
2976
|
-
});
|
|
2977
|
-
throw (u = this.onerror) == null || u.call(this, A), A;
|
|
2978
|
-
}
|
|
2979
|
-
const b = d.body.pipeThrough(new TextDecoderStream()).pipeThrough(new he()).getReader();
|
|
2980
|
-
(async () => {
|
|
2981
|
-
var A, M, k;
|
|
2982
|
-
try {
|
|
2983
|
-
for (; ; ) {
|
|
2984
|
-
const { done: O, value: H } = await b.read();
|
|
2985
|
-
if (O) return;
|
|
2986
|
-
const { event: x, data: $ } = H;
|
|
2987
|
-
if (x === "message")
|
|
2988
|
-
try {
|
|
2989
|
-
const D = V.parse(JSON.parse($));
|
|
2990
|
-
(A = this.onmessage) == null || A.call(this, D);
|
|
2991
|
-
} catch (D) {
|
|
2992
|
-
const I = new _({
|
|
2993
|
-
message: "MCP HTTP Transport Error: Failed to parse message",
|
|
2994
|
-
cause: D
|
|
2995
|
-
});
|
|
2996
|
-
(M = this.onerror) == null || M.call(this, I);
|
|
2997
|
-
}
|
|
2998
|
-
}
|
|
2999
|
-
} catch (O) {
|
|
3000
|
-
if (O instanceof Error && O.name === "AbortError")
|
|
3001
|
-
return;
|
|
3002
|
-
(k = this.onerror) == null || k.call(this, O);
|
|
3003
|
-
}
|
|
3004
|
-
})();
|
|
3005
|
-
return;
|
|
3006
|
-
}
|
|
3007
|
-
const g = new _({
|
|
3008
|
-
message: `MCP HTTP Transport Error: Unexpected content type: ${h}`
|
|
3009
|
-
});
|
|
3010
|
-
throw (c = this.onerror) == null || c.call(this, g), g;
|
|
3011
|
-
} catch (p) {
|
|
3012
|
-
throw (l = this.onerror) == null || l.call(this, p), p;
|
|
3013
|
-
}
|
|
3014
|
-
};
|
|
3015
|
-
await e();
|
|
3016
|
-
}
|
|
3017
|
-
getNextReconnectionDelay(t) {
|
|
3018
|
-
const {
|
|
3019
|
-
initialReconnectionDelay: e,
|
|
3020
|
-
reconnectionDelayGrowFactor: r,
|
|
3021
|
-
maxReconnectionDelay: s
|
|
3022
|
-
} = this.reconnectionOptions;
|
|
3023
|
-
return Math.min(
|
|
3024
|
-
e * Math.pow(r, t),
|
|
3025
|
-
s
|
|
3026
|
-
);
|
|
3027
|
-
}
|
|
3028
|
-
scheduleInboundSseReconnection() {
|
|
3029
|
-
var t;
|
|
3030
|
-
const { maxRetries: e } = this.reconnectionOptions;
|
|
3031
|
-
if (e > 0 && this.inboundReconnectAttempts >= e) {
|
|
3032
|
-
(t = this.onerror) == null || t.call(
|
|
3033
|
-
this,
|
|
3034
|
-
new _({
|
|
3035
|
-
message: `MCP HTTP Transport Error: Maximum reconnection attempts (${e}) exceeded.`
|
|
3036
|
-
})
|
|
3037
|
-
);
|
|
3038
|
-
return;
|
|
3039
|
-
}
|
|
3040
|
-
const r = this.getNextReconnectionDelay(this.inboundReconnectAttempts);
|
|
3041
|
-
this.inboundReconnectAttempts += 1, setTimeout(async () => {
|
|
3042
|
-
var s;
|
|
3043
|
-
(s = this.abortController) != null && s.signal.aborted || await this.openInboundSse(!1, this.lastInboundEventId);
|
|
3044
|
-
}, r);
|
|
3045
|
-
}
|
|
3046
|
-
// Open optional inbound SSE stream; best-effort and resumable
|
|
3047
|
-
async openInboundSse(t = !1, e) {
|
|
3048
|
-
var r, s, n, a, i, u;
|
|
3049
|
-
try {
|
|
3050
|
-
const c = await this.commonHeaders({
|
|
3051
|
-
Accept: "text/event-stream"
|
|
3052
|
-
});
|
|
3053
|
-
e && (c["last-event-id"] = e);
|
|
3054
|
-
const l = await this.fetchFn(this.url.href, {
|
|
3055
|
-
method: "GET",
|
|
3056
|
-
headers: c,
|
|
3057
|
-
signal: (r = this.abortController) == null ? void 0 : r.signal,
|
|
3058
|
-
redirect: this.redirectMode
|
|
3059
|
-
}), p = l.headers.get("mcp-session-id");
|
|
3060
|
-
if (p && (this.sessionId = p), l.status === 401 && this.authProvider && !t) {
|
|
3061
|
-
this.resourceMetadataUrl = te(l);
|
|
3062
|
-
try {
|
|
3063
|
-
if (await re(this.authProvider, {
|
|
3064
|
-
serverUrl: this.url,
|
|
3065
|
-
resourceMetadataUrl: this.resourceMetadataUrl,
|
|
3066
|
-
fetchFn: this.fetchFn
|
|
3067
|
-
}) !== "AUTHORIZED") {
|
|
3068
|
-
const h = new ee();
|
|
3069
|
-
(s = this.onerror) == null || s.call(this, h);
|
|
3070
|
-
return;
|
|
3071
|
-
}
|
|
3072
|
-
} catch (y) {
|
|
3073
|
-
(n = this.onerror) == null || n.call(this, y);
|
|
3074
|
-
return;
|
|
3075
|
-
}
|
|
3076
|
-
return this.openInboundSse(!0, e);
|
|
3077
|
-
}
|
|
3078
|
-
if (l.status === 405)
|
|
3079
|
-
return;
|
|
3080
|
-
if (!l.ok || !l.body) {
|
|
3081
|
-
const y = new _({
|
|
3082
|
-
message: `MCP HTTP Transport Error: GET SSE failed: ${l.status} ${l.statusText}`
|
|
3083
|
-
});
|
|
3084
|
-
(a = this.onerror) == null || a.call(this, y);
|
|
3085
|
-
return;
|
|
3086
|
-
}
|
|
3087
|
-
const d = l.body.pipeThrough(new TextDecoderStream()).pipeThrough(new he()).getReader(), f = async () => {
|
|
3088
|
-
var y, h, g, v;
|
|
3089
|
-
try {
|
|
3090
|
-
for (; ; ) {
|
|
3091
|
-
const { done: b, value: S } = await d.read();
|
|
3092
|
-
if (b) return;
|
|
3093
|
-
const { event: A, data: M, id: k } = S;
|
|
3094
|
-
if (k && (this.lastInboundEventId = k), A === "message")
|
|
3095
|
-
try {
|
|
3096
|
-
const O = V.parse(JSON.parse(M));
|
|
3097
|
-
(y = this.onmessage) == null || y.call(this, O);
|
|
3098
|
-
} catch (O) {
|
|
3099
|
-
const H = new _({
|
|
3100
|
-
message: "MCP HTTP Transport Error: Failed to parse message",
|
|
3101
|
-
cause: O
|
|
3102
|
-
});
|
|
3103
|
-
(h = this.onerror) == null || h.call(this, H);
|
|
3104
|
-
}
|
|
3105
|
-
}
|
|
3106
|
-
} catch (b) {
|
|
3107
|
-
if (b instanceof Error && b.name === "AbortError")
|
|
3108
|
-
return;
|
|
3109
|
-
(g = this.onerror) == null || g.call(this, b), (v = this.abortController) != null && v.signal.aborted || this.scheduleInboundSseReconnection();
|
|
3110
|
-
}
|
|
3111
|
-
};
|
|
3112
|
-
this.inboundSseConnection = {
|
|
3113
|
-
close: () => d.cancel()
|
|
3114
|
-
}, this.inboundReconnectAttempts = 0, f();
|
|
3115
|
-
} catch (c) {
|
|
3116
|
-
if (c instanceof Error && c.name === "AbortError")
|
|
3117
|
-
return;
|
|
3118
|
-
(i = this.onerror) == null || i.call(this, c), (u = this.abortController) != null && u.signal.aborted || this.scheduleInboundSseReconnection();
|
|
3119
|
-
}
|
|
3120
|
-
}
|
|
3121
|
-
};
|
|
3122
|
-
function Ys(t) {
|
|
3123
|
-
switch (t.type) {
|
|
3124
|
-
case "sse":
|
|
3125
|
-
return new Xs(t);
|
|
3126
|
-
case "http":
|
|
3127
|
-
return new Qs(t);
|
|
3128
|
-
default:
|
|
3129
|
-
throw new _({
|
|
3130
|
-
message: "Unsupported or invalid transport configuration. If you are using a custom transport, make sure it implements the MCPTransport interface."
|
|
3131
|
-
});
|
|
3132
|
-
}
|
|
3133
|
-
}
|
|
3134
|
-
function en(t) {
|
|
3135
|
-
return "start" in t && typeof t.start == "function" && "send" in t && typeof t.send == "function" && "close" in t && typeof t.close == "function";
|
|
3136
|
-
}
|
|
3137
|
-
var tn = "1.0.0";
|
|
3138
|
-
function Ne({
|
|
3139
|
-
output: t
|
|
3140
|
-
}) {
|
|
3141
|
-
const e = t;
|
|
3142
|
-
return !("content" in e) || !Array.isArray(e.content) ? { type: "json", value: e } : { type: "content", value: e.content.map(
|
|
3143
|
-
(s) => s.type === "text" && "text" in s ? { type: "text", text: s.text } : s.type === "image" && "data" in s && "mimeType" in s ? {
|
|
3144
|
-
type: "image-data",
|
|
3145
|
-
data: s.data,
|
|
3146
|
-
mediaType: s.mimeType
|
|
3147
|
-
} : { type: "text", text: JSON.stringify(s) }
|
|
3148
|
-
) };
|
|
3149
|
-
}
|
|
3150
|
-
async function rn(t) {
|
|
3151
|
-
const e = new sn(t);
|
|
3152
|
-
return await e.init(), e;
|
|
3153
|
-
}
|
|
3154
|
-
var sn = class {
|
|
3155
|
-
constructor({
|
|
3156
|
-
transport: t,
|
|
3157
|
-
name: e = "ai-sdk-mcp-client",
|
|
3158
|
-
version: r = tn,
|
|
3159
|
-
onUncaughtError: s,
|
|
3160
|
-
capabilities: n
|
|
3161
|
-
}) {
|
|
3162
|
-
this.requestMessageId = 0, this.responseHandlers = /* @__PURE__ */ new Map(), this.serverCapabilities = {}, this._serverInfo = { name: "", version: "" }, this.isClosed = !0, this.onUncaughtError = s, this.clientCapabilities = n ?? {}, en(t) ? this.transport = t : this.transport = Ys(t), this.transport.onclose = () => this.onClose(), this.transport.onerror = (a) => this.onError(a), this.transport.onmessage = (a) => {
|
|
3163
|
-
if ("method" in a) {
|
|
3164
|
-
"id" in a ? this.onRequestMessage(a) : this.onError(
|
|
3165
|
-
new _({
|
|
3166
|
-
message: "Unsupported message type"
|
|
3167
|
-
})
|
|
3168
|
-
);
|
|
3169
|
-
return;
|
|
3170
|
-
}
|
|
3171
|
-
this.onResponse(a);
|
|
3172
|
-
}, this.clientInfo = {
|
|
3173
|
-
name: e,
|
|
3174
|
-
version: r
|
|
3175
|
-
};
|
|
3176
|
-
}
|
|
3177
|
-
get serverInfo() {
|
|
3178
|
-
return this._serverInfo;
|
|
3179
|
-
}
|
|
3180
|
-
async init() {
|
|
3181
|
-
try {
|
|
3182
|
-
await this.transport.start(), this.isClosed = !1;
|
|
3183
|
-
const t = await this.request({
|
|
3184
|
-
request: {
|
|
3185
|
-
method: "initialize",
|
|
3186
|
-
params: {
|
|
3187
|
-
protocolVersion: Z,
|
|
3188
|
-
capabilities: this.clientCapabilities,
|
|
3189
|
-
clientInfo: this.clientInfo
|
|
3190
|
-
}
|
|
3191
|
-
},
|
|
3192
|
-
resultSchema: os
|
|
3193
|
-
});
|
|
3194
|
-
if (t === void 0)
|
|
3195
|
-
throw new _({
|
|
3196
|
-
message: "Server sent invalid initialize result"
|
|
3197
|
-
});
|
|
3198
|
-
if (!ts.includes(t.protocolVersion))
|
|
3199
|
-
throw new _({
|
|
3200
|
-
message: `Server's protocol version is not supported: ${t.protocolVersion}`
|
|
3201
|
-
});
|
|
3202
|
-
return this.serverCapabilities = t.capabilities, this._serverInfo = t.serverInfo, await this.notification({
|
|
3203
|
-
method: "notifications/initialized"
|
|
3204
|
-
}), this;
|
|
3205
|
-
} catch (t) {
|
|
3206
|
-
throw await this.close(), t;
|
|
3207
|
-
}
|
|
3208
|
-
}
|
|
3209
|
-
async close() {
|
|
3210
|
-
var t;
|
|
3211
|
-
this.isClosed || (await ((t = this.transport) == null ? void 0 : t.close()), this.onClose());
|
|
3212
|
-
}
|
|
3213
|
-
assertCapability(t) {
|
|
3214
|
-
switch (t) {
|
|
3215
|
-
case "initialize":
|
|
3216
|
-
break;
|
|
3217
|
-
case "tools/list":
|
|
3218
|
-
case "tools/call":
|
|
3219
|
-
if (!this.serverCapabilities.tools)
|
|
3220
|
-
throw new _({
|
|
3221
|
-
message: "Server does not support tools"
|
|
3222
|
-
});
|
|
3223
|
-
break;
|
|
3224
|
-
case "resources/list":
|
|
3225
|
-
case "resources/read":
|
|
3226
|
-
case "resources/templates/list":
|
|
3227
|
-
if (!this.serverCapabilities.resources)
|
|
3228
|
-
throw new _({
|
|
3229
|
-
message: "Server does not support resources"
|
|
3230
|
-
});
|
|
3231
|
-
break;
|
|
3232
|
-
case "prompts/list":
|
|
3233
|
-
case "prompts/get":
|
|
3234
|
-
if (!this.serverCapabilities.prompts)
|
|
3235
|
-
throw new _({
|
|
3236
|
-
message: "Server does not support prompts"
|
|
3237
|
-
});
|
|
3238
|
-
break;
|
|
3239
|
-
default:
|
|
3240
|
-
throw new _({
|
|
3241
|
-
message: `Unsupported method: ${t}`
|
|
3242
|
-
});
|
|
3243
|
-
}
|
|
3244
|
-
}
|
|
3245
|
-
async request({
|
|
3246
|
-
request: t,
|
|
3247
|
-
resultSchema: e,
|
|
3248
|
-
options: r
|
|
3249
|
-
}) {
|
|
3250
|
-
return new Promise((s, n) => {
|
|
3251
|
-
if (this.isClosed)
|
|
3252
|
-
return n(
|
|
3253
|
-
new _({
|
|
3254
|
-
message: "Attempted to send a request from a closed client"
|
|
3255
|
-
})
|
|
3256
|
-
);
|
|
3257
|
-
this.assertCapability(t.method);
|
|
3258
|
-
const a = r?.signal;
|
|
3259
|
-
a?.throwIfAborted();
|
|
3260
|
-
const i = this.requestMessageId++, u = {
|
|
3261
|
-
...t,
|
|
3262
|
-
jsonrpc: "2.0",
|
|
3263
|
-
id: i
|
|
3264
|
-
}, c = () => {
|
|
3265
|
-
this.responseHandlers.delete(i);
|
|
3266
|
-
};
|
|
3267
|
-
this.responseHandlers.set(i, (l) => {
|
|
3268
|
-
if (a?.aborted)
|
|
3269
|
-
return n(
|
|
3270
|
-
new _({
|
|
3271
|
-
message: "Request was aborted",
|
|
3272
|
-
cause: a.reason
|
|
3273
|
-
})
|
|
3274
|
-
);
|
|
3275
|
-
if (l instanceof Error)
|
|
3276
|
-
return n(l);
|
|
3277
|
-
try {
|
|
3278
|
-
const p = e.parse(l.result);
|
|
3279
|
-
s(p);
|
|
3280
|
-
} catch (p) {
|
|
3281
|
-
const m = new _({
|
|
3282
|
-
message: "Failed to parse server response",
|
|
3283
|
-
cause: p
|
|
3284
|
-
});
|
|
3285
|
-
n(m);
|
|
3286
|
-
}
|
|
3287
|
-
}), this.transport.send(u).catch((l) => {
|
|
3288
|
-
c(), n(l);
|
|
3289
|
-
});
|
|
3290
|
-
});
|
|
3291
|
-
}
|
|
3292
|
-
async listTools({
|
|
3293
|
-
params: t,
|
|
3294
|
-
options: e
|
|
3295
|
-
} = {}) {
|
|
3296
|
-
return this.request({
|
|
3297
|
-
request: { method: "tools/list", params: t },
|
|
3298
|
-
resultSchema: is,
|
|
3299
|
-
options: e
|
|
3300
|
-
});
|
|
3301
|
-
}
|
|
3302
|
-
async callTool({
|
|
3303
|
-
name: t,
|
|
3304
|
-
args: e,
|
|
3305
|
-
options: r
|
|
3306
|
-
}) {
|
|
3307
|
-
try {
|
|
3308
|
-
return this.request({
|
|
3309
|
-
request: { method: "tools/call", params: { name: t, arguments: e } },
|
|
3310
|
-
resultSchema: us,
|
|
3311
|
-
options: {
|
|
3312
|
-
signal: r?.abortSignal
|
|
3313
|
-
}
|
|
3314
|
-
});
|
|
3315
|
-
} catch (s) {
|
|
3316
|
-
throw s;
|
|
3317
|
-
}
|
|
3318
|
-
}
|
|
3319
|
-
async listResourcesInternal({
|
|
3320
|
-
params: t,
|
|
3321
|
-
options: e
|
|
3322
|
-
} = {}) {
|
|
3323
|
-
try {
|
|
3324
|
-
return this.request({
|
|
3325
|
-
request: { method: "resources/list", params: t },
|
|
3326
|
-
resultSchema: ls,
|
|
3327
|
-
options: e
|
|
3328
|
-
});
|
|
3329
|
-
} catch (r) {
|
|
3330
|
-
throw r;
|
|
3331
|
-
}
|
|
3332
|
-
}
|
|
3333
|
-
async readResourceInternal({
|
|
3334
|
-
uri: t,
|
|
3335
|
-
options: e
|
|
3336
|
-
}) {
|
|
3337
|
-
try {
|
|
3338
|
-
return this.request({
|
|
3339
|
-
request: { method: "resources/read", params: { uri: t } },
|
|
3340
|
-
resultSchema: hs,
|
|
3341
|
-
options: e
|
|
3342
|
-
});
|
|
3343
|
-
} catch (r) {
|
|
3344
|
-
throw r;
|
|
3345
|
-
}
|
|
3346
|
-
}
|
|
3347
|
-
async listResourceTemplatesInternal({
|
|
3348
|
-
options: t
|
|
3349
|
-
} = {}) {
|
|
3350
|
-
try {
|
|
3351
|
-
return this.request({
|
|
3352
|
-
request: { method: "resources/templates/list" },
|
|
3353
|
-
resultSchema: ds,
|
|
3354
|
-
options: t
|
|
3355
|
-
});
|
|
3356
|
-
} catch (e) {
|
|
3357
|
-
throw e;
|
|
3358
|
-
}
|
|
3359
|
-
}
|
|
3360
|
-
async listPromptsInternal({
|
|
3361
|
-
params: t,
|
|
3362
|
-
options: e
|
|
3363
|
-
} = {}) {
|
|
3364
|
-
try {
|
|
3365
|
-
return this.request({
|
|
3366
|
-
request: { method: "prompts/list", params: t },
|
|
3367
|
-
resultSchema: gs,
|
|
3368
|
-
options: e
|
|
3369
|
-
});
|
|
3370
|
-
} catch (r) {
|
|
3371
|
-
throw r;
|
|
3372
|
-
}
|
|
3373
|
-
}
|
|
3374
|
-
async getPromptInternal({
|
|
3375
|
-
name: t,
|
|
3376
|
-
args: e,
|
|
3377
|
-
options: r
|
|
3378
|
-
}) {
|
|
3379
|
-
try {
|
|
3380
|
-
return this.request({
|
|
3381
|
-
request: { method: "prompts/get", params: { name: t, arguments: e } },
|
|
3382
|
-
resultSchema: vs,
|
|
3383
|
-
options: r
|
|
3384
|
-
});
|
|
3385
|
-
} catch (s) {
|
|
3386
|
-
throw s;
|
|
3387
|
-
}
|
|
3388
|
-
}
|
|
3389
|
-
async notification(t) {
|
|
3390
|
-
const e = {
|
|
3391
|
-
...t,
|
|
3392
|
-
jsonrpc: "2.0"
|
|
3393
|
-
};
|
|
3394
|
-
await this.transport.send(e);
|
|
3395
|
-
}
|
|
3396
|
-
/**
|
|
3397
|
-
* Returns a set of AI SDK tools from the MCP server.
|
|
3398
|
-
* This fetches tool definitions and wraps them with execute functions.
|
|
3399
|
-
* @returns A record of tool names to their implementations
|
|
3400
|
-
*/
|
|
3401
|
-
async tools({
|
|
3402
|
-
schemas: t = "automatic"
|
|
3403
|
-
} = {}) {
|
|
3404
|
-
const e = await this.listTools();
|
|
3405
|
-
return this.toolsFromDefinitions(e, {
|
|
3406
|
-
schemas: t
|
|
3407
|
-
});
|
|
3408
|
-
}
|
|
3409
|
-
/**
|
|
3410
|
-
* Creates AI SDK tools from tool definitions without fetching from the server.
|
|
3411
|
-
*/
|
|
3412
|
-
toolsFromDefinitions(t, { schemas: e = "automatic" } = {}) {
|
|
3413
|
-
var r, s;
|
|
3414
|
-
const n = {};
|
|
3415
|
-
for (const {
|
|
3416
|
-
name: a,
|
|
3417
|
-
title: i,
|
|
3418
|
-
description: u,
|
|
3419
|
-
inputSchema: c,
|
|
3420
|
-
annotations: l,
|
|
3421
|
-
_meta: p
|
|
3422
|
-
} of t.tools) {
|
|
3423
|
-
const m = i ?? l?.title;
|
|
3424
|
-
if (e !== "automatic" && !(a in e))
|
|
3425
|
-
continue;
|
|
3426
|
-
const d = this, f = e !== "automatic" ? (r = e[a]) == null ? void 0 : r.outputSchema : void 0, y = async (g, v) => {
|
|
3427
|
-
var b;
|
|
3428
|
-
(b = v?.abortSignal) == null || b.throwIfAborted();
|
|
3429
|
-
const S = await d.callTool({ name: a, args: g, options: v });
|
|
3430
|
-
return S.isError ? S : f != null ? d.extractStructuredContent(S, f, a) : S;
|
|
3431
|
-
}, h = e === "automatic" ? Wr({
|
|
3432
|
-
description: u,
|
|
3433
|
-
title: m,
|
|
3434
|
-
inputSchema: J({
|
|
3435
|
-
...c,
|
|
3436
|
-
properties: (s = c.properties) != null ? s : {},
|
|
3437
|
-
additionalProperties: !1
|
|
3438
|
-
}),
|
|
3439
|
-
execute: y,
|
|
3440
|
-
toModelOutput: Ne
|
|
3441
|
-
}) : {
|
|
3442
|
-
description: u,
|
|
3443
|
-
title: m,
|
|
3444
|
-
inputSchema: e[a].inputSchema,
|
|
3445
|
-
...f != null ? { outputSchema: f } : {},
|
|
3446
|
-
execute: y,
|
|
3447
|
-
toModelOutput: Ne
|
|
3448
|
-
};
|
|
3449
|
-
n[a] = { ...h, _meta: p };
|
|
3450
|
-
}
|
|
3451
|
-
return n;
|
|
3452
|
-
}
|
|
3453
|
-
/**
|
|
3454
|
-
* Extracts and validates structuredContent from a tool result.
|
|
3455
|
-
*/
|
|
3456
|
-
async extractStructuredContent(t, e, r) {
|
|
3457
|
-
if ("structuredContent" in t && t.structuredContent != null) {
|
|
3458
|
-
const s = await Xe({
|
|
3459
|
-
value: t.structuredContent,
|
|
3460
|
-
schema: Ke(e)
|
|
3461
|
-
});
|
|
3462
|
-
if (!s.success)
|
|
3463
|
-
throw new _({
|
|
3464
|
-
message: `Tool "${r}" returned structuredContent that does not match the expected outputSchema`,
|
|
3465
|
-
cause: s.error
|
|
3466
|
-
});
|
|
3467
|
-
return s.value;
|
|
3468
|
-
}
|
|
3469
|
-
if ("content" in t && Array.isArray(t.content)) {
|
|
3470
|
-
const s = t.content.find((n) => n.type === "text");
|
|
3471
|
-
if (s && "text" in s) {
|
|
3472
|
-
const n = await Jr({
|
|
3473
|
-
text: s.text,
|
|
3474
|
-
schema: e
|
|
3475
|
-
});
|
|
3476
|
-
if (!n.success)
|
|
3477
|
-
throw new _({
|
|
3478
|
-
message: `Tool "${r}" returned content that does not match the expected outputSchema`,
|
|
3479
|
-
cause: n.error
|
|
3480
|
-
});
|
|
3481
|
-
return n.value;
|
|
3482
|
-
}
|
|
3483
|
-
}
|
|
3484
|
-
throw new _({
|
|
3485
|
-
message: `Tool "${r}" did not return structuredContent or parseable text content`
|
|
3486
|
-
});
|
|
3487
|
-
}
|
|
3488
|
-
listResources({
|
|
3489
|
-
params: t,
|
|
3490
|
-
options: e
|
|
3491
|
-
} = {}) {
|
|
3492
|
-
return this.listResourcesInternal({ params: t, options: e });
|
|
3493
|
-
}
|
|
3494
|
-
readResource({
|
|
3495
|
-
uri: t,
|
|
3496
|
-
options: e
|
|
3497
|
-
}) {
|
|
3498
|
-
return this.readResourceInternal({ uri: t, options: e });
|
|
3499
|
-
}
|
|
3500
|
-
listResourceTemplates({
|
|
3501
|
-
options: t
|
|
3502
|
-
} = {}) {
|
|
3503
|
-
return this.listResourceTemplatesInternal({ options: t });
|
|
3504
|
-
}
|
|
3505
|
-
experimental_listPrompts({
|
|
3506
|
-
params: t,
|
|
3507
|
-
options: e
|
|
3508
|
-
} = {}) {
|
|
3509
|
-
return this.listPromptsInternal({ params: t, options: e });
|
|
3510
|
-
}
|
|
3511
|
-
experimental_getPrompt({
|
|
3512
|
-
name: t,
|
|
3513
|
-
arguments: e,
|
|
3514
|
-
options: r
|
|
3515
|
-
}) {
|
|
3516
|
-
return this.getPromptInternal({ name: t, args: e, options: r });
|
|
3517
|
-
}
|
|
3518
|
-
onElicitationRequest(t, e) {
|
|
3519
|
-
if (t !== je)
|
|
3520
|
-
throw new _({
|
|
3521
|
-
message: "Unsupported request schema. Only ElicitationRequestSchema is supported."
|
|
3522
|
-
});
|
|
3523
|
-
this.elicitationRequestHandler = e;
|
|
3524
|
-
}
|
|
3525
|
-
async onRequestMessage(t) {
|
|
3526
|
-
try {
|
|
3527
|
-
if (t.method !== "elicitation/create") {
|
|
3528
|
-
await this.transport.send({
|
|
3529
|
-
jsonrpc: "2.0",
|
|
3530
|
-
id: t.id,
|
|
3531
|
-
error: {
|
|
3532
|
-
code: -32601,
|
|
3533
|
-
message: `Unsupported request method: ${t.method}`
|
|
3534
|
-
}
|
|
3535
|
-
});
|
|
3536
|
-
return;
|
|
3537
|
-
}
|
|
3538
|
-
if (!this.elicitationRequestHandler) {
|
|
3539
|
-
await this.transport.send({
|
|
3540
|
-
jsonrpc: "2.0",
|
|
3541
|
-
id: t.id,
|
|
3542
|
-
error: {
|
|
3543
|
-
code: -32601,
|
|
3544
|
-
message: "No elicitation handler registered on client"
|
|
3545
|
-
}
|
|
3546
|
-
});
|
|
3547
|
-
return;
|
|
3548
|
-
}
|
|
3549
|
-
const e = je.safeParse({
|
|
3550
|
-
method: t.method,
|
|
3551
|
-
params: t.params
|
|
3552
|
-
});
|
|
3553
|
-
if (!e.success) {
|
|
3554
|
-
await this.transport.send({
|
|
3555
|
-
jsonrpc: "2.0",
|
|
3556
|
-
id: t.id,
|
|
3557
|
-
error: {
|
|
3558
|
-
code: -32602,
|
|
3559
|
-
message: `Invalid elicitation request: ${e.error.message}`,
|
|
3560
|
-
data: e.error.issues
|
|
3561
|
-
}
|
|
3562
|
-
});
|
|
3563
|
-
return;
|
|
3564
|
-
}
|
|
3565
|
-
try {
|
|
3566
|
-
const r = await this.elicitationRequestHandler(e.data), s = _s.parse(r);
|
|
3567
|
-
await this.transport.send({
|
|
3568
|
-
jsonrpc: "2.0",
|
|
3569
|
-
id: t.id,
|
|
3570
|
-
result: s
|
|
3571
|
-
});
|
|
3572
|
-
} catch (r) {
|
|
3573
|
-
await this.transport.send({
|
|
3574
|
-
jsonrpc: "2.0",
|
|
3575
|
-
id: t.id,
|
|
3576
|
-
error: {
|
|
3577
|
-
code: -32603,
|
|
3578
|
-
message: r instanceof Error ? r.message : "Failed to handle elicitation request"
|
|
3579
|
-
}
|
|
3580
|
-
}), this.onError(r);
|
|
3581
|
-
}
|
|
3582
|
-
} catch (e) {
|
|
3583
|
-
this.onError(e);
|
|
3584
|
-
}
|
|
3585
|
-
}
|
|
3586
|
-
onClose() {
|
|
3587
|
-
if (this.isClosed) return;
|
|
3588
|
-
this.isClosed = !0;
|
|
3589
|
-
const t = new _({
|
|
3590
|
-
message: "Connection closed"
|
|
3591
|
-
});
|
|
3592
|
-
for (const e of this.responseHandlers.values())
|
|
3593
|
-
e(t);
|
|
3594
|
-
this.responseHandlers.clear();
|
|
3595
|
-
}
|
|
3596
|
-
onError(t) {
|
|
3597
|
-
this.onUncaughtError && this.onUncaughtError(t);
|
|
3598
|
-
}
|
|
3599
|
-
onResponse(t) {
|
|
3600
|
-
const e = Number(t.id), r = this.responseHandlers.get(e);
|
|
3601
|
-
if (r === void 0)
|
|
3602
|
-
throw new _({
|
|
3603
|
-
message: `Protocol error: Received a response for an unknown message ID: ${JSON.stringify(
|
|
3604
|
-
t
|
|
3605
|
-
)}`
|
|
3606
|
-
});
|
|
3607
|
-
this.responseHandlers.delete(e), r(
|
|
3608
|
-
"result" in t ? t : new _({
|
|
3609
|
-
message: t.error.message,
|
|
3610
|
-
code: t.error.code,
|
|
3611
|
-
data: t.error.data,
|
|
3612
|
-
cause: t.error
|
|
3613
|
-
})
|
|
3614
|
-
);
|
|
3615
|
-
}
|
|
3616
|
-
};
|
|
3617
|
-
const nn = async (t) => {
|
|
3618
|
-
const e = {};
|
|
3619
|
-
try {
|
|
3620
|
-
const r = await t.listTools();
|
|
3621
|
-
for (const { name: s, description: n, inputSchema: a } of r.tools) {
|
|
3622
|
-
const i = async (u, c) => t.callTool({ name: s, arguments: u }, { signal: c?.abortSignal });
|
|
3623
|
-
e[s] = Fe({
|
|
3624
|
-
description: n,
|
|
3625
|
-
inputSchema: Ze({
|
|
3626
|
-
...a,
|
|
3627
|
-
properties: a.properties ?? {},
|
|
3628
|
-
additionalProperties: !1
|
|
3629
|
-
}),
|
|
3630
|
-
execute: i
|
|
3631
|
-
});
|
|
3632
|
-
}
|
|
3633
|
-
return e;
|
|
3634
|
-
} catch (r) {
|
|
3635
|
-
throw r;
|
|
3636
|
-
}
|
|
3637
|
-
}, on = async (t) => {
|
|
3638
|
-
const e = {};
|
|
3639
|
-
if (!t)
|
|
3640
|
-
return e;
|
|
3641
|
-
const r = t, s = r.listTools ?? r.getTools;
|
|
3642
|
-
if (!s)
|
|
3643
|
-
return e;
|
|
3644
|
-
const n = await s.call(r), a = Array.isArray(n) ? n : [];
|
|
3645
|
-
for (const i of a) {
|
|
3646
|
-
const { name: u, description: c } = i, l = i.inputSchema;
|
|
3647
|
-
let p = {};
|
|
3648
|
-
if (typeof l == "string")
|
|
3649
|
-
try {
|
|
3650
|
-
p = JSON.parse(l);
|
|
3651
|
-
} catch (d) {
|
|
3652
|
-
console.error("Failed to parse inputSchema in getBuiltinMcpTools:", d);
|
|
3653
|
-
}
|
|
3654
|
-
else typeof l == "object" && l !== null && (p = l);
|
|
3655
|
-
const m = {
|
|
3656
|
-
type: "object",
|
|
3657
|
-
properties: p.properties ?? {},
|
|
3658
|
-
...p.required ? { required: p.required } : {},
|
|
3659
|
-
additionalProperties: !1,
|
|
3660
|
-
...p
|
|
3661
|
-
};
|
|
3662
|
-
e[u] = Fe({
|
|
3663
|
-
description: c ?? "",
|
|
3664
|
-
inputSchema: Ze(m),
|
|
3665
|
-
async execute(d) {
|
|
3666
|
-
if (!r.executeTool)
|
|
3667
|
-
throw new Error("navigator.modelContextTesting.executeTool is not available");
|
|
3668
|
-
return r.executeTool(u, JSON.stringify(d ?? {}));
|
|
3669
|
-
}
|
|
3670
|
-
});
|
|
3671
|
-
}
|
|
3672
|
-
return e;
|
|
3673
|
-
};
|
|
3674
|
-
function an(t) {
|
|
3675
|
-
const e = Object.entries(t);
|
|
3676
|
-
if (e.length === 0)
|
|
3677
|
-
return "";
|
|
3678
|
-
let r = `
|
|
3679
|
-
# 工具调用
|
|
3680
|
-
|
|
3681
|
-
你可以根据需要调用以下工具:
|
|
3682
|
-
|
|
3683
|
-
<tools>
|
|
3684
|
-
`;
|
|
3685
|
-
return e.forEach(([s, n]) => {
|
|
3686
|
-
const a = n, i = a.description || "无描述", u = a.parameters || a.inputSchema || {};
|
|
3687
|
-
r += `${JSON.stringify({
|
|
3688
|
-
name: s,
|
|
3689
|
-
description: i,
|
|
3690
|
-
parameters: u
|
|
3691
|
-
}, null, 2)}
|
|
3692
|
-
`;
|
|
3693
|
-
}), r += `
|
|
3694
|
-
</tools>
|
|
3695
|
-
|
|
3696
|
-
## 工具调用格式
|
|
3697
|
-
|
|
3698
|
-
要调用工具,请使用以下 XML 格式:
|
|
3699
|
-
Thought: [你的思考过程]
|
|
3700
|
-
<tool_call>
|
|
3701
|
-
{"name": "toolName", "arguments": {"arg1": "value1"}}
|
|
3702
|
-
</tool_call>
|
|
3703
|
-
|
|
3704
|
-
工具执行后,你将收到 <tool_response> 格式的结果。你可以继续思考或调用其他工具。
|
|
3705
|
-
|
|
3706
|
-
## 使用示例
|
|
3707
|
-
|
|
3708
|
-
如果用户要求"获取今天的日期",你可以这样调用工具:
|
|
3709
|
-
Thought: 用户想要获取今天的日期,我需要调用日期相关的工具。
|
|
3710
|
-
<tool_call>{"name": "get-today", "arguments": {}}</tool_call>
|
|
3711
|
-
|
|
3712
|
-
然后等待工具返回结果(Observation),再根据结果给出最终答案。
|
|
3713
|
-
|
|
3714
|
-
## 任务完成
|
|
3715
|
-
|
|
3716
|
-
当任务完成或无法继续时,直接给出最终答案即可。
|
|
3717
|
-
|
|
3718
|
-
**重要提示**:
|
|
3719
|
-
- 必须严格按照 XML 格式调用工具
|
|
3720
|
-
- arguments 必须是有效的 JSON 格式
|
|
3721
|
-
- 如果不需要调用工具,直接给出最终答案即可
|
|
3722
|
-
`, r;
|
|
3723
|
-
}
|
|
3724
|
-
function qe(t, e) {
|
|
3725
|
-
if (!t || typeof t != "string")
|
|
3726
|
-
return null;
|
|
3727
|
-
const r = t.match(/<tool_call>([\s\S]*?)<\/tool_call>/i);
|
|
3728
|
-
if (r)
|
|
3729
|
-
try {
|
|
3730
|
-
const s = r[1].trim(), n = JSON.parse(s), a = n.name || n.action || n.tool, i = n.arguments || n.args || n.input || {};
|
|
3731
|
-
if (a && e[a])
|
|
3732
|
-
return { toolName: a, arguments: i };
|
|
3733
|
-
} catch {
|
|
3734
|
-
}
|
|
3735
|
-
return null;
|
|
3736
|
-
}
|
|
3737
|
-
const cn = {
|
|
3738
|
-
openai: $t,
|
|
3739
|
-
deepseek: Nt
|
|
3740
|
-
};
|
|
3741
|
-
class Un {
|
|
3742
|
-
constructor({ llmConfig: e, mcpServers: r }) {
|
|
3743
|
-
if (this.mcpServers = {}, this.mcpClients = {}, this.mcpTools = {}, this.ignoreToolnames = [], this.responseMessages = [], this.useReActMode = !1, !e)
|
|
3744
|
-
throw new Error("llmConfig is required to initialize AgentModelProvider");
|
|
3745
|
-
if (this.mcpServers = r || {}, this.mcpClients = {}, this.mcpTools = {}, e.llm)
|
|
3746
|
-
this.llm = e.llm;
|
|
3747
|
-
else if (e.providerType) {
|
|
3748
|
-
const s = e.providerType;
|
|
3749
|
-
let n;
|
|
3750
|
-
typeof s == "string" ? n = cn[s] : n = s, this.llm = n({
|
|
3751
|
-
apiKey: e.apiKey,
|
|
3752
|
-
baseURL: e.baseURL
|
|
3753
|
-
});
|
|
3754
|
-
} else
|
|
3755
|
-
throw new Error("Either llmConfig.llm or llmConfig.providerType must be provided");
|
|
3756
|
-
this.useReActMode = e.useReActMode ?? !1;
|
|
3757
|
-
}
|
|
3758
|
-
/** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
|
|
3759
|
-
async _createOneClient(e) {
|
|
3760
|
-
try {
|
|
3761
|
-
let r;
|
|
3762
|
-
if ("type" in e && e.type === "builtin") {
|
|
3763
|
-
const n = e.client;
|
|
3764
|
-
return {
|
|
3765
|
-
tools: () => on(n),
|
|
3766
|
-
close: async () => {
|
|
3767
|
-
}
|
|
3768
|
-
};
|
|
3769
|
-
}
|
|
3770
|
-
if ("type" in e && e.type.toLocaleLowerCase() === "streamablehttp") {
|
|
3771
|
-
const n = e, a = n.headers ? { headers: n.headers } : void 0;
|
|
3772
|
-
r = new se(new URL(n.url), { requestInit: a });
|
|
3773
|
-
} else if ("type" in e && e.type === "sse") {
|
|
3774
|
-
const n = e, a = n.headers ? { headers: n.headers } : void 0;
|
|
3775
|
-
r = new ne(new URL(n.url), { requestInit: a });
|
|
3776
|
-
} else "type" in e && e.type === "extension" ? r = new Vt(e.sessionId) : "transport" in e ? r = e.transport : r = e;
|
|
3777
|
-
if (e.useAISdkClient ?? !1) {
|
|
3778
|
-
const n = await rn({ transport: r });
|
|
3779
|
-
return n.__transport__ = r, n;
|
|
3780
|
-
} else {
|
|
3781
|
-
const n = new Ft(
|
|
3782
|
-
{ name: "mcp-web-client", version: "1.0.0" },
|
|
3783
|
-
{ capabilities: { roots: { listChanged: !0 }, sampling: {}, elicitation: {} } }
|
|
3784
|
-
);
|
|
3785
|
-
return await n.connect(r), n.__transport__ = r, n;
|
|
3786
|
-
}
|
|
3787
|
-
} catch (r) {
|
|
3788
|
-
return this.onError && this.onError(r?.message || "Failed to create MCP client", r), console.error("Failed to create MCP client", e, r), null;
|
|
3789
|
-
}
|
|
3790
|
-
}
|
|
3791
|
-
/** 关闭一个 mcpClient */
|
|
3792
|
-
async _closeOneClient(e) {
|
|
3793
|
-
try {
|
|
3794
|
-
const r = e.__transport__;
|
|
3795
|
-
if (r && r instanceof Ut || r && r instanceof wt)
|
|
3796
|
-
return;
|
|
3797
|
-
await r?.terminateSession?.(), await r?.close?.(), await e?.close?.();
|
|
3798
|
-
} catch {
|
|
3799
|
-
}
|
|
3800
|
-
}
|
|
3801
|
-
/** 创建所有 mcpClients */
|
|
3802
|
-
async _createMpcClients() {
|
|
3803
|
-
const e = Object.entries(this.mcpServers), r = await Promise.all(
|
|
3804
|
-
e.map(async ([s, n]) => {
|
|
3805
|
-
const a = await this._createOneClient(n);
|
|
3806
|
-
return { serverName: s, client: a };
|
|
3807
|
-
})
|
|
3808
|
-
);
|
|
3809
|
-
this.mcpClients = {}, r.forEach(({ serverName: s, client: n }) => {
|
|
3810
|
-
this.mcpClients[s] = n;
|
|
3811
|
-
});
|
|
3812
|
-
}
|
|
3813
|
-
/** 兼容两种 client 类型的 tools 获取方法 */
|
|
3814
|
-
async _getClientTools(e, r) {
|
|
3815
|
-
if (!e)
|
|
3816
|
-
return null;
|
|
3817
|
-
try {
|
|
3818
|
-
return typeof e.tools == "function" ? await e.tools() : await nn(e);
|
|
3819
|
-
} catch (s) {
|
|
3820
|
-
return this.onError && this.onError(s?.message || `Failed to query tools for ${r}`, s), console.error(`Failed to query tools for ${r}`, s), null;
|
|
3821
|
-
}
|
|
3822
|
-
}
|
|
3823
|
-
/** 查询所有 mcpClients 的 tools, 失败则保存为null */
|
|
3824
|
-
async _createMpcTools() {
|
|
3825
|
-
const e = Object.entries(this.mcpClients), r = await Promise.all(
|
|
3826
|
-
e.map(async ([s, n]) => {
|
|
3827
|
-
const a = await this._getClientTools(n, s);
|
|
3828
|
-
return { serverName: s, tools: a };
|
|
3829
|
-
})
|
|
3830
|
-
);
|
|
3831
|
-
this.mcpTools = {}, r.forEach(({ serverName: s, tools: n }) => {
|
|
3832
|
-
const a = n && typeof n == "object" ? n : {};
|
|
3833
|
-
this.mcpTools[s] = a;
|
|
3834
|
-
});
|
|
3835
|
-
}
|
|
3836
|
-
/** 关闭所有的 clients */
|
|
3837
|
-
async closeAll() {
|
|
3838
|
-
await Promise.all(
|
|
3839
|
-
Object.values(this.mcpClients).map(async (e) => {
|
|
3840
|
-
try {
|
|
3841
|
-
await this._closeOneClient(e);
|
|
3842
|
-
} catch (r) {
|
|
3843
|
-
this.onError && this.onError(r?.message || "Failed to close client", r), console.error("Failed to close client", r);
|
|
3844
|
-
}
|
|
3845
|
-
})
|
|
3846
|
-
);
|
|
3847
|
-
}
|
|
3848
|
-
/** 创建所有的 mcpClients,并更新它们的tools */
|
|
3849
|
-
async initClientsAndTools() {
|
|
3850
|
-
await this._createMpcClients(), await this._createMpcTools(), this.onUpdatedTools?.();
|
|
3851
|
-
}
|
|
3852
|
-
/** 仅刷新已连接 clients 的 tools(不重建 client,适合工具目录动态变化场景) */
|
|
3853
|
-
async refreshTools() {
|
|
3854
|
-
await this._createMpcTools(), this.onUpdatedTools?.();
|
|
3855
|
-
}
|
|
3856
|
-
/** 全量更新所有的 mcpServers */
|
|
3857
|
-
async updateMcpServers(e) {
|
|
3858
|
-
await this.closeAll(), this.mcpServers = e || this.mcpServers, await this.initClientsAndTools();
|
|
3859
|
-
}
|
|
3860
|
-
/** 插入一个新的mcpServer,如果已经存在则返回false */
|
|
3861
|
-
async insertMcpServer(e, r) {
|
|
3862
|
-
if (this.mcpServers[e])
|
|
3863
|
-
return !1;
|
|
3864
|
-
const s = await this._createOneClient(r);
|
|
3865
|
-
if (!s)
|
|
3866
|
-
return this.onError?.(`Failed to create MCP client: ${e}`), null;
|
|
3867
|
-
this.mcpClients[e] = s;
|
|
3868
|
-
const n = await this._getClientTools(s, e);
|
|
3869
|
-
return this.mcpTools[e] = n && typeof n == "object" ? n : {}, this.mcpServers[e] = r, this.onUpdatedTools?.(), s;
|
|
3870
|
-
}
|
|
3871
|
-
/** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
|
|
3872
|
-
async removeMcpServer(e) {
|
|
3873
|
-
if (!this.mcpServers[e])
|
|
3874
|
-
return;
|
|
3875
|
-
delete this.mcpServers[e];
|
|
3876
|
-
const r = this.mcpClients[e];
|
|
3877
|
-
delete this.mcpClients[e];
|
|
3878
|
-
try {
|
|
3879
|
-
await this._closeOneClient(r);
|
|
3880
|
-
} catch {
|
|
3881
|
-
}
|
|
3882
|
-
const s = this.mcpTools[e];
|
|
3883
|
-
delete this.mcpTools[e], s && Object.keys(s).forEach((n) => {
|
|
3884
|
-
this.ignoreToolnames = this.ignoreToolnames.filter((a) => a !== n);
|
|
3885
|
-
}), this.onUpdatedTools?.();
|
|
3886
|
-
}
|
|
3887
|
-
/** 创建临时允许调用的 tools 集合,合并 mcpTools 与 extraTool */
|
|
3888
|
-
_tempMergeTools(e = {}, r = !0) {
|
|
3889
|
-
const s = Object.values(this.mcpTools).reduce(
|
|
3890
|
-
(n, a) => ({ ...n, ...a }),
|
|
3891
|
-
{}
|
|
3892
|
-
);
|
|
3893
|
-
return Object.assign(s, e), r && this.ignoreToolnames.forEach((n) => {
|
|
3894
|
-
delete s[n];
|
|
3895
|
-
}), s;
|
|
3896
|
-
}
|
|
3897
|
-
/** 获取当前激活的 tools 名称列表(过滤 ignoreToolnames) */
|
|
3898
|
-
_getActiveToolNames(e) {
|
|
3899
|
-
return Object.keys(e).filter((r) => !this.ignoreToolnames.includes(r));
|
|
3900
|
-
}
|
|
3901
|
-
/** 生成 ReAct 模式的系统提示词(包含工具描述) */
|
|
3902
|
-
_generateReActSystemPrompt(e, r, s) {
|
|
3903
|
-
const n = an(e);
|
|
3904
|
-
return s ? `${s}${n}` : `你是一个智能助手,可以通过调用工具来完成任务。
|
|
3905
|
-
${n}`;
|
|
3906
|
-
}
|
|
3907
|
-
/** 执行 ReAct 模式下的工具调用 */
|
|
3908
|
-
async _executeReActToolCall(e, r, s) {
|
|
3909
|
-
const n = s[e];
|
|
3910
|
-
if (!n)
|
|
3911
|
-
return { success: !1, error: `工具 ${e} 不存在` };
|
|
3912
|
-
try {
|
|
3913
|
-
const a = n, i = a.execute || a.call;
|
|
3914
|
-
return typeof i != "function" ? { success: !1, error: `工具 ${e} 没有可执行的函数` } : { success: !0, result: await i(r, {}) };
|
|
3915
|
-
} catch (a) {
|
|
3916
|
-
return { success: !1, error: a?.message || String(a) || "工具执行失败" };
|
|
3917
|
-
}
|
|
3918
|
-
}
|
|
3919
|
-
/** ReAct 模式的对话实现 */
|
|
3920
|
-
async _chatReAct(e, { model: r, maxSteps: s = 5, ...n }) {
|
|
3921
|
-
if (!this.llm)
|
|
3922
|
-
throw new Error("LLM is not initialized");
|
|
3923
|
-
await this.initClientsAndTools();
|
|
3924
|
-
const a = this._tempMergeTools(n.tools);
|
|
3925
|
-
if (Object.keys(a).length === 0)
|
|
3926
|
-
return this._chat(e, { model: r, maxSteps: s, ...n });
|
|
3927
|
-
let u = [];
|
|
3928
|
-
n.message && !n.messages ? u.push({ role: "user", content: n.message }) : n.messages ? u = [...n.messages] : u = [...this.responseMessages];
|
|
3929
|
-
const c = typeof r == "string" ? r : r?.modelId || "default-model", p = { role: "system", content: this._generateReActSystemPrompt(a, c, n.system) }, m = u[0]?.role === "system" ? u : [p, ...u];
|
|
3930
|
-
return e === ie ? this._chatReActStream(m, a, c, s, n) : this._chatReActNonStream(m, a, c, s, n);
|
|
3931
|
-
}
|
|
3932
|
-
/**
|
|
3933
|
-
* 检查消息内容是否包含图片
|
|
3934
|
-
* @param content 消息内容
|
|
3935
|
-
* @returns 是否包含图片
|
|
3936
|
-
*/
|
|
3937
|
-
_messageHasImage(e) {
|
|
3938
|
-
return e && Array.isArray(e) ? e.some((r) => r && r.type === "image") : !1;
|
|
3939
|
-
}
|
|
3940
|
-
/**
|
|
3941
|
-
* 从消息中移除图片,但保留文本内容
|
|
3942
|
-
* @param message 原始消息
|
|
3943
|
-
* @returns 移除图片后的消息(如果只有图片没有文本,返回 null)
|
|
3944
|
-
*/
|
|
3945
|
-
_removeImageFromMessage(e) {
|
|
3946
|
-
if (!e || !e.content)
|
|
3947
|
-
return null;
|
|
3948
|
-
if (!Array.isArray(e.content))
|
|
3949
|
-
return e;
|
|
3950
|
-
const r = e.content.filter((s) => s && s.type !== "image");
|
|
3951
|
-
return r.length === 0 ? null : {
|
|
3952
|
-
...e,
|
|
3953
|
-
content: r
|
|
3954
|
-
};
|
|
3955
|
-
}
|
|
3956
|
-
/**
|
|
3957
|
-
* 构建用于模型调用的消息列表
|
|
3958
|
-
* 策略:保留所有文本消息,仅限制图片数量(类似 magentic-ui 的 maybe_remove_old_screenshots)
|
|
3959
|
-
*
|
|
3960
|
-
* @param systemMessage 系统提示词
|
|
3961
|
-
* @param allMessages 所有消息历史(包括初始消息和后续对话)
|
|
3962
|
-
* @param maxImages 最多保留的图片数量(默认3张)
|
|
3963
|
-
* @returns 构建好的消息列表
|
|
3964
|
-
*/
|
|
3965
|
-
_buildMessagesForModel(e, r, s = 3) {
|
|
3966
|
-
const n = [];
|
|
3967
|
-
e && n.push(e);
|
|
3968
|
-
let a = 0;
|
|
3969
|
-
const i = [];
|
|
3970
|
-
for (let u = r.length - 1; u >= 0; u--) {
|
|
3971
|
-
const c = r[u];
|
|
3972
|
-
if (this._messageHasImage(c.content))
|
|
3973
|
-
if (a < s)
|
|
3974
|
-
i.unshift(c), a++;
|
|
3975
|
-
else {
|
|
3976
|
-
const p = this._removeImageFromMessage(c);
|
|
3977
|
-
p && i.unshift(p);
|
|
3978
|
-
}
|
|
3979
|
-
else
|
|
3980
|
-
i.unshift(c);
|
|
3981
|
-
}
|
|
3982
|
-
return n.push(...i), n;
|
|
3983
|
-
}
|
|
3984
|
-
/** ReAct 模式非流式对话 */
|
|
3985
|
-
async _chatReActNonStream(e, r, s, n, a) {
|
|
3986
|
-
let i = [...e];
|
|
3987
|
-
const u = e[0]?.role === "system" ? e[0] : null, c = u ? e.slice(1) : e;
|
|
3988
|
-
let l = 0;
|
|
3989
|
-
const p = a.maxImages ?? 3;
|
|
3990
|
-
for (; l < n; ) {
|
|
3991
|
-
l++;
|
|
3992
|
-
const d = this._buildMessagesForModel(u, c, p), { tools: f, ...y } = a, g = (await Pe({
|
|
3993
|
-
// @ts-ignore ProviderV2 是所有llm的父类,在每一个具体的llm类都有一个选择model的函数用法
|
|
3994
|
-
model: this.llm(s),
|
|
3995
|
-
messages: d,
|
|
3996
|
-
...y
|
|
3997
|
-
})).text, v = { role: "assistant", content: g };
|
|
3998
|
-
c.push(v), i.push(v);
|
|
3999
|
-
const b = qe(g, r);
|
|
4000
|
-
if (!b)
|
|
4001
|
-
return this.responseMessages = i, {
|
|
4002
|
-
text: g,
|
|
4003
|
-
response: { messages: i }
|
|
4004
|
-
};
|
|
4005
|
-
const S = await this._executeReActToolCall(b.toolName, b.arguments, r), k = {
|
|
4006
|
-
role: "user",
|
|
4007
|
-
content: `<tool_response>
|
|
4008
|
-
${S.success ? JSON.stringify(S.result) : `工具执行失败 - ${S.error}`}
|
|
4009
|
-
</tool_response>`
|
|
4010
|
-
};
|
|
4011
|
-
c.push(k), i.push(k);
|
|
4012
|
-
}
|
|
4013
|
-
return this.responseMessages = i, {
|
|
4014
|
-
text: i[i.length - 2]?.content || "",
|
|
4015
|
-
response: { messages: i }
|
|
4016
|
-
};
|
|
4017
|
-
}
|
|
4018
|
-
/** ReAct 模式流式对话 */
|
|
4019
|
-
_chatReActStream(e, r, s, n, a) {
|
|
4020
|
-
const i = this, u = this.llm(s);
|
|
4021
|
-
let c, l;
|
|
4022
|
-
const p = new Promise((d, f) => {
|
|
4023
|
-
c = d, l = f;
|
|
4024
|
-
});
|
|
4025
|
-
return {
|
|
4026
|
-
fullStream: new ReadableStream({
|
|
4027
|
-
async start(d) {
|
|
4028
|
-
let f = [...e];
|
|
4029
|
-
const y = e[0]?.role === "system" ? e[0] : null, h = y ? e.slice(1) : [...e];
|
|
4030
|
-
let g = 0, v = "";
|
|
4031
|
-
const b = a.maxImages ?? 3;
|
|
4032
|
-
d.enqueue({ type: "start" }), d.enqueue({ type: "start-step" });
|
|
4033
|
-
try {
|
|
4034
|
-
for (; g < n; ) {
|
|
4035
|
-
g++;
|
|
4036
|
-
const S = i._buildMessagesForModel(y, h, b), { tools: A, ...M } = a;
|
|
4037
|
-
delete M.system, delete M.onFinish;
|
|
4038
|
-
const k = await ie({
|
|
4039
|
-
...M,
|
|
4040
|
-
model: u,
|
|
4041
|
-
messages: S
|
|
4042
|
-
});
|
|
4043
|
-
let O = "";
|
|
4044
|
-
for await (const C of k.fullStream)
|
|
4045
|
-
C.type === "text-delta" ? (O += C.text || "", d.enqueue({
|
|
4046
|
-
type: "text-delta",
|
|
4047
|
-
text: C.text
|
|
4048
|
-
})) : C.type === "text-start" ? d.enqueue({ type: "text-start" }) : C.type === "text-end" || C.type === "finish-step" || C.type === "finish" || C.type === "start" || C.type === "start-step" || d.enqueue(C);
|
|
4049
|
-
v += O;
|
|
4050
|
-
const H = { role: "assistant", content: v };
|
|
4051
|
-
h.push(H), f.push(H);
|
|
4052
|
-
const x = qe(v, r);
|
|
4053
|
-
if (!x) {
|
|
4054
|
-
d.enqueue({ type: "text-end" }), d.enqueue({ type: "finish-step" }), d.enqueue({ type: "finish" }), d.close(), i.responseMessages = f, c({ messages: f });
|
|
4055
|
-
return;
|
|
4056
|
-
}
|
|
4057
|
-
if (x.toolName === "computer" && x.arguments?.action === "terminate") {
|
|
4058
|
-
d.enqueue({ type: "text-end" }), d.enqueue({ type: "finish-step" }), d.enqueue({ type: "finish" }), d.close(), i.responseMessages = f, c({ messages: f });
|
|
4059
|
-
return;
|
|
4060
|
-
}
|
|
4061
|
-
const $ = `react-${Date.now()}`;
|
|
4062
|
-
d.enqueue({
|
|
4063
|
-
type: "tool-input-start",
|
|
4064
|
-
id: $,
|
|
4065
|
-
toolName: x.toolName
|
|
4066
|
-
});
|
|
4067
|
-
const D = JSON.stringify(x.arguments, null, 2);
|
|
4068
|
-
d.enqueue({
|
|
4069
|
-
type: "tool-input-delta",
|
|
4070
|
-
id: $,
|
|
4071
|
-
delta: D
|
|
4072
|
-
}), d.enqueue({
|
|
4073
|
-
type: "tool-input-end",
|
|
4074
|
-
id: $
|
|
4075
|
-
}), d.enqueue({
|
|
4076
|
-
type: "tool-call",
|
|
4077
|
-
toolCallId: $,
|
|
4078
|
-
toolName: x.toolName,
|
|
4079
|
-
input: x.arguments
|
|
4080
|
-
});
|
|
4081
|
-
const I = await i._executeReActToolCall(x.toolName, x.arguments, r);
|
|
4082
|
-
let B, N = I.result;
|
|
4083
|
-
if (I.success && I.result && typeof I.result == "object" && I.result.screenshot) {
|
|
4084
|
-
B = I.result.screenshot;
|
|
4085
|
-
const { screenshot: C, ...G } = I.result;
|
|
4086
|
-
N = G;
|
|
4087
|
-
}
|
|
4088
|
-
let z = "";
|
|
4089
|
-
if (I.success) {
|
|
4090
|
-
N && Array.isArray(N.content) && N.content.length > 0 && N.content[0].text ? z = N.content[0].text : z = JSON.stringify(N);
|
|
4091
|
-
let C = `<tool_response>
|
|
4092
|
-
${z}
|
|
4093
|
-
</tool_response>`;
|
|
4094
|
-
B && (C += `
|
|
4095
|
-
请检查截图以确认操作是否成功。如果成功,请继续下一步;如果失败,请重试。`), d.enqueue({
|
|
4096
|
-
type: "tool-result",
|
|
4097
|
-
toolCallId: $,
|
|
4098
|
-
result: C
|
|
4099
|
-
});
|
|
4100
|
-
const G = B ? {
|
|
4101
|
-
role: "user",
|
|
4102
|
-
content: [
|
|
4103
|
-
{ type: "text", text: C },
|
|
4104
|
-
{ type: "image", image: B }
|
|
4105
|
-
]
|
|
4106
|
-
} : {
|
|
4107
|
-
role: "user",
|
|
4108
|
-
content: C
|
|
4109
|
-
};
|
|
4110
|
-
h.push(G), f.push(G), v = "";
|
|
4111
|
-
} else
|
|
4112
|
-
z = `工具执行失败 - ${I.error}`, d.enqueue({
|
|
4113
|
-
type: "tool-error",
|
|
4114
|
-
toolCallId: $,
|
|
4115
|
-
input: x.arguments,
|
|
4116
|
-
error: { message: z }
|
|
4117
|
-
});
|
|
4118
|
-
}
|
|
4119
|
-
d.enqueue({ type: "text-end" }), d.enqueue({ type: "finish-step" }), d.enqueue({ type: "finish" }), d.close(), i.responseMessages = f, c({ messages: f });
|
|
4120
|
-
} catch (S) {
|
|
4121
|
-
d.error(S), l(S);
|
|
4122
|
-
}
|
|
4123
|
-
}
|
|
4124
|
-
}),
|
|
4125
|
-
response: p
|
|
4126
|
-
};
|
|
4127
|
-
}
|
|
4128
|
-
async _chat(e, { model: r, maxSteps: s = 5, ...n }) {
|
|
4129
|
-
if (this.useReActMode)
|
|
4130
|
-
return this._chatReAct(e, { model: r, maxSteps: s, ...n });
|
|
4131
|
-
if (!this.llm)
|
|
4132
|
-
throw new Error("LLM is not initialized");
|
|
4133
|
-
await this.initClientsAndTools();
|
|
4134
|
-
const a = n.tools || {}, i = this._tempMergeTools(a, !1), u = () => {
|
|
4135
|
-
const f = this._tempMergeTools(a, !1);
|
|
4136
|
-
Object.entries(f).forEach(([y, h]) => {
|
|
4137
|
-
i[y] = h;
|
|
4138
|
-
}), Object.keys(i).forEach((y) => {
|
|
4139
|
-
y in f || delete i[y];
|
|
4140
|
-
});
|
|
4141
|
-
}, c = n.prepareStep, l = async (f) => {
|
|
4142
|
-
u();
|
|
4143
|
-
const y = this._getActiveToolNames(i), h = typeof c == "function" ? await c(f) : void 0, g = h && typeof h == "object" ? h : {};
|
|
4144
|
-
return {
|
|
4145
|
-
...g,
|
|
4146
|
-
activeTools: Array.isArray(g.activeTools) ? g.activeTools.filter((v) => y.includes(v)) : y
|
|
4147
|
-
};
|
|
4148
|
-
}, p = {
|
|
4149
|
-
// @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
|
|
4150
|
-
model: this.llm(r),
|
|
4151
|
-
stopWhen: jt(s),
|
|
4152
|
-
...n,
|
|
4153
|
-
tools: i,
|
|
4154
|
-
prepareStep: l,
|
|
4155
|
-
activeTools: this._getActiveToolNames(i)
|
|
4156
|
-
};
|
|
4157
|
-
let m = null;
|
|
4158
|
-
if (n.message && !n.messages)
|
|
4159
|
-
m = { role: "user", content: n.message }, this.responseMessages.push(m), p.messages = [...this.responseMessages];
|
|
4160
|
-
else if (n.messages && n.messages.length > 0) {
|
|
4161
|
-
const f = n.messages[n.messages.length - 1];
|
|
4162
|
-
f.role === "user" && (m = f);
|
|
4163
|
-
}
|
|
4164
|
-
const d = e(p);
|
|
4165
|
-
return d?.response?.then((f) => {
|
|
4166
|
-
const y = f.messages?.[0];
|
|
4167
|
-
m && y?.role !== "user" && this.responseMessages.push(m), this.responseMessages.push(...f.messages);
|
|
4168
|
-
}), d;
|
|
4169
|
-
}
|
|
4170
|
-
async chat(e) {
|
|
4171
|
-
return this._chat(Pe, e);
|
|
4172
|
-
}
|
|
4173
|
-
async chatStream(e) {
|
|
4174
|
-
return this._chat(ie, e);
|
|
4175
|
-
}
|
|
4176
|
-
}
|
|
4177
|
-
export {
|
|
4178
|
-
Un as A,
|
|
4179
|
-
Vt as E,
|
|
4180
|
-
Tn as W,
|
|
4181
|
-
Pn as a,
|
|
4182
|
-
Mn as b,
|
|
4183
|
-
Cn as c,
|
|
4184
|
-
Ft as d,
|
|
4185
|
-
En as e,
|
|
4186
|
-
An as f,
|
|
4187
|
-
nn as g,
|
|
4188
|
-
kn as h,
|
|
4189
|
-
Rn as i,
|
|
4190
|
-
On as j,
|
|
4191
|
-
Zt as k,
|
|
4192
|
-
xn as l,
|
|
4193
|
-
In as m,
|
|
4194
|
-
zt as n,
|
|
4195
|
-
Ln as o,
|
|
4196
|
-
Dt as p,
|
|
4197
|
-
jn as s
|
|
4198
|
-
};
|