@openeryc/pi-coding-agent 0.75.54 → 0.75.56
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/CHANGELOG.md +30 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +4 -0
- package/dist/cli.js.map +1 -1
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +11 -26
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/fetch-openai-models.d.ts +25 -0
- package/dist/core/fetch-openai-models.d.ts.map +1 -0
- package/dist/core/fetch-openai-models.js +107 -0
- package/dist/core/fetch-openai-models.js.map +1 -0
- package/dist/core/mcp/client.d.ts +16 -7
- package/dist/core/mcp/client.d.ts.map +1 -1
- package/dist/core/mcp/client.js +135 -107
- package/dist/core/mcp/client.js.map +1 -1
- package/dist/core/mcp/manager.d.ts +8 -1
- package/dist/core/mcp/manager.d.ts.map +1 -1
- package/dist/core/mcp/manager.js +56 -33
- package/dist/core/mcp/manager.js.map +1 -1
- package/dist/core/mcp/types.d.ts +0 -13
- package/dist/core/mcp/types.d.ts.map +1 -1
- package/dist/core/mcp/types.js.map +1 -1
- package/dist/core/model-registry.d.ts +112 -0
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +99 -2
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +3 -1
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +1 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +4 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +12 -0
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +2 -2
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/modes/interactive/components/index.d.ts +1 -1
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +1 -1
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/components/model-hub.d.ts +115 -0
- package/dist/modes/interactive/components/model-hub.d.ts.map +1 -0
- package/dist/modes/interactive/components/model-hub.js +753 -0
- package/dist/modes/interactive/components/model-hub.js.map +1 -0
- package/dist/modes/interactive/components/model-selector.d.ts +2 -44
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/model-selector.js +2 -275
- package/dist/modes/interactive/components/model-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +22 -2
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +666 -557
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +5 -1
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/utils/sleep.d.ts.map +1 -1
- package/dist/utils/sleep.js +7 -3
- package/dist/utils/sleep.js.map +1 -1
- package/dist/utils/tools-manager.d.ts.map +1 -1
- package/dist/utils/tools-manager.js.map +1 -1
- package/docs/models.md +20 -0
- package/docs/providers.md +13 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +12 -12
- package/package.json +4 -4
package/dist/core/mcp/manager.js
CHANGED
|
@@ -103,6 +103,8 @@ export class MCPManager {
|
|
|
103
103
|
this.serverConfigs.delete(serverName);
|
|
104
104
|
this.toolTimeoutMap.delete(serverName);
|
|
105
105
|
this.tools.delete(serverName);
|
|
106
|
+
// Also clean up resource URIs for this server
|
|
107
|
+
this.resourceUris = this.resourceUris.filter((u) => !u.startsWith(`mcp://${serverName}`));
|
|
106
108
|
const client = this.clients.get(serverName);
|
|
107
109
|
if (client) {
|
|
108
110
|
client.onDisconnect = null;
|
|
@@ -113,18 +115,31 @@ export class MCPManager {
|
|
|
113
115
|
}
|
|
114
116
|
async connectServer(serverName, config) {
|
|
115
117
|
this.setStatus(serverName, "reconnecting");
|
|
118
|
+
let disconnected = false;
|
|
119
|
+
const onDisconnect = () => {
|
|
120
|
+
if (disconnected)
|
|
121
|
+
return;
|
|
122
|
+
disconnected = true;
|
|
123
|
+
this.setStatus(serverName, "disconnected");
|
|
124
|
+
this.tools.delete(serverName);
|
|
125
|
+
this.scheduleReconnect(serverName);
|
|
126
|
+
};
|
|
116
127
|
try {
|
|
117
128
|
const client = new MCPClient(config.timeoutMs);
|
|
118
|
-
client.onDisconnect = (
|
|
119
|
-
this.setStatus(serverName, "disconnected");
|
|
120
|
-
this.tools.delete(serverName);
|
|
121
|
-
this.scheduleReconnect(serverName);
|
|
122
|
-
};
|
|
129
|
+
client.onDisconnect = () => onDisconnect();
|
|
123
130
|
await client.connect(config);
|
|
131
|
+
// If disconnect happened during connect(), bail out
|
|
132
|
+
if (disconnected) {
|
|
133
|
+
this.clients.delete(serverName);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
124
136
|
const { tools: mcpTools } = await client.listTools();
|
|
137
|
+
if (disconnected) {
|
|
138
|
+
client.disconnect().catch(() => { });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
125
141
|
this.clients.set(serverName, client);
|
|
126
142
|
this.buildToolDefs(serverName, mcpTools);
|
|
127
|
-
// Discover resources
|
|
128
143
|
try {
|
|
129
144
|
const { resources } = await client.listResources();
|
|
130
145
|
for (const r of resources) {
|
|
@@ -134,14 +149,19 @@ export class MCPManager {
|
|
|
134
149
|
catch {
|
|
135
150
|
// Server may not support resources
|
|
136
151
|
}
|
|
152
|
+
if (disconnected) {
|
|
153
|
+
// Was disconnected during tool/resource discovery
|
|
154
|
+
client.disconnect().catch(() => { });
|
|
155
|
+
this.clients.delete(serverName);
|
|
156
|
+
this.tools.delete(serverName);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
137
159
|
this.setStatus(serverName, "connected");
|
|
138
160
|
this.reconnectAttempts.delete(serverName);
|
|
139
161
|
}
|
|
140
162
|
catch (error) {
|
|
141
|
-
|
|
163
|
+
onDisconnect();
|
|
142
164
|
console.warn(`Failed to connect to MCP server "${serverName}": ${error instanceof Error ? error.message : String(error)}`);
|
|
143
|
-
this.setStatus(serverName, "disconnected");
|
|
144
|
-
this.scheduleReconnect(serverName);
|
|
145
165
|
}
|
|
146
166
|
}
|
|
147
167
|
buildToolDefs(serverName, mcpTools) {
|
|
@@ -151,17 +171,20 @@ export class MCPManager {
|
|
|
151
171
|
const defs = mcpTools.map((tool) => {
|
|
152
172
|
// Resolve effective timeout: per-tool > server default > client default (120s)
|
|
153
173
|
const toolSpecificTimeoutMs = perToolTimeout?.get(tool.name);
|
|
154
|
-
// Add optional _timeoutMs parameter so the AI can override timeout per-call
|
|
174
|
+
// Add optional _timeoutMs parameter so the AI can override timeout per-call.
|
|
175
|
+
// We must produce a flat properties schema (not allOf) because LLM providers
|
|
176
|
+
// access tool.parameters.properties directly.
|
|
155
177
|
const baseParams = toTypeBox(tool.inputSchema);
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
_timeoutMs: Type.Optional(Type.Number({
|
|
178
|
+
const baseSchema = baseParams;
|
|
179
|
+
const mergedProps = {
|
|
180
|
+
...(baseSchema.properties ?? {}),
|
|
181
|
+
_timeoutMs: {
|
|
182
|
+
type: "number",
|
|
162
183
|
description: "Optional timeout override in milliseconds for this specific call (e.g. 300000 for 5 min, 5000 for 5s). Overrides the server default timeout.",
|
|
163
|
-
}
|
|
164
|
-
}
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
const mergedRequired = [...(baseSchema.required ?? [])];
|
|
187
|
+
const parameters = Type.Object(mergedProps, { required: mergedRequired });
|
|
165
188
|
return {
|
|
166
189
|
name: `mcp_${safeServerName}_${sanitizeMcpName(tool.name)}`,
|
|
167
190
|
label: `mcp.${serverName}.${tool.name}`,
|
|
@@ -236,7 +259,7 @@ export class MCPManager {
|
|
|
236
259
|
}
|
|
237
260
|
this.reconnectAttempts.set(serverName, attempt);
|
|
238
261
|
this.setStatus(serverName, "reconnecting");
|
|
239
|
-
const timer = setTimeout(
|
|
262
|
+
const timer = setTimeout(() => {
|
|
240
263
|
this.reconnectTimers.delete(serverName);
|
|
241
264
|
const cfg = this.serverConfigs.get(serverName);
|
|
242
265
|
if (!cfg)
|
|
@@ -245,10 +268,10 @@ export class MCPManager {
|
|
|
245
268
|
const oldClient = this.clients.get(serverName);
|
|
246
269
|
if (oldClient) {
|
|
247
270
|
oldClient.onDisconnect = null;
|
|
248
|
-
|
|
271
|
+
oldClient.disconnect().catch(() => { });
|
|
249
272
|
this.clients.delete(serverName);
|
|
250
273
|
}
|
|
251
|
-
|
|
274
|
+
this.connectServer(serverName, cfg).catch((err) => console.warn(`MCP reconnect failed for "${serverName}":`, err));
|
|
252
275
|
}, intervalMs * Math.min(attempt, 5)); // exponential backoff, cap at 5x
|
|
253
276
|
this.reconnectTimers.set(serverName, timer);
|
|
254
277
|
}
|
|
@@ -273,27 +296,27 @@ export class MCPManager {
|
|
|
273
296
|
this.reconnectAttempts.delete(serverName);
|
|
274
297
|
await this.connectServer(serverName, config);
|
|
275
298
|
}
|
|
276
|
-
// Rebuild tool definitions for a server (used after reconnect to pick up any changes)
|
|
277
299
|
getToolDefinitions() {
|
|
278
300
|
return Array.from(this.tools.values()).flat();
|
|
279
301
|
}
|
|
302
|
+
/** Get all MCP tool descriptions grouped by server (for system prompt). */
|
|
303
|
+
getMcpToolDescriptions() {
|
|
304
|
+
const result = [];
|
|
305
|
+
for (const [serverName, defs] of this.tools) {
|
|
306
|
+
const tools = defs.map((d) => ({
|
|
307
|
+
name: d.name,
|
|
308
|
+
description: d.description ?? `Tool from MCP server "${serverName}"`,
|
|
309
|
+
}));
|
|
310
|
+
result.push({ serverName, tools });
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
280
314
|
getServerNames() {
|
|
281
315
|
return [...this.clients.keys()];
|
|
282
316
|
}
|
|
283
317
|
getResourceUris() {
|
|
284
318
|
return this.resourceUris;
|
|
285
319
|
}
|
|
286
|
-
async readResource(uri) {
|
|
287
|
-
const serverName = this.clients.keys().next().value;
|
|
288
|
-
if (!serverName)
|
|
289
|
-
throw new Error("No MCP servers connected");
|
|
290
|
-
const mcpUri = uri.startsWith(`mcp://${serverName}`) ? uri.slice(`mcp://${serverName}`.length) : uri;
|
|
291
|
-
const client = this.clients.get(serverName);
|
|
292
|
-
if (!client)
|
|
293
|
-
throw new Error(`MCP server "${serverName}" not connected`);
|
|
294
|
-
const result = await client.readResource(mcpUri);
|
|
295
|
-
return result.contents.map((c) => c.text ?? "").join("\n");
|
|
296
|
-
}
|
|
297
320
|
async stop() {
|
|
298
321
|
// Cancel all reconnect timers
|
|
299
322
|
for (const [, timer] of this.reconnectTimers) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/core/mcp/manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,OAAO,EAAE,SAAS,EAAwB,MAAM,aAAa,CAAC;AAG9D,MAAM,8BAA8B,GAAG,CAAC,CAAC;AACzC,MAAM,6BAA6B,GAAG,IAAI,CAAC;AAE3C,SAAS,SAAS,CAAC,MAAwC,EAAkC;IAC5F,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvE,OAAO,IAAI,C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,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAAA,CAC3D;IAED,KAAK,CAAC,IAAI,GAAkB;QAC3B,8BAA8B;QAC9B,KAAK,MAAM,CAAC,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YAC9C,YAAY,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC;QACD,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC;QAE/B,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC;YACzE,IAAI,CAAC;gBACJ,MAAM,MAAM,CAAC,UAAU,EAAE,CAAC;YAC3B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,OAAO,CAAC,IAAI,CACX,mCAAmC,IAAI,MAAM,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CACrG,CAAC;YACH,CAAC;QAAA,CACD,CAAC,CAAC;QACH,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC;QACvB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAAA,CAC3B;CACD","sourcesContent":["import type { AgentToolResult } from \"@openeryc/pi-agent-core\";\nimport { Type } from \"typebox\";\nimport type { ToolDefinition } from \"../extensions/types.ts\";\nimport { MCPClient, type MCPServerConfig } from \"./client.ts\";\nimport type { MCPConnectionStatus, MCPToolDefinition } from \"./types.ts\";\n\nconst DEFAULT_RECONNECT_MAX_ATTEMPTS = 5;\nconst DEFAULT_RECONNECT_INTERVAL_MS = 5000;\n\nfunction toTypeBox(schema: MCPToolDefinition[\"inputSchema\"]): ReturnType<typeof Type.Object> {\n\tif (!schema.properties || Object.keys(schema.properties).length === 0) {\n\t\treturn Type.Object({});\n\t}\n\tconst required = new Set(schema.required ?? []);\n\tconst fields: Record<string, unknown> = {};\n\tfor (const [key, prop] of Object.entries(schema.properties)) {\n\t\tconst p = prop as { type?: string; description?: string; enum?: string[] };\n\t\tconst t = p.type ?? \"string\";\n\t\tconst desc = p.description ? { description: p.description } : {};\n\t\tlet f: unknown;\n\t\tswitch (t) {\n\t\t\tcase \"string\":\n\t\t\t\tf = p.enum ? Type.Unsafe<string>({ type: \"string\", enum: p.enum, ...desc }) : Type.String(desc);\n\t\t\t\tbreak;\n\t\t\tcase \"number\":\n\t\t\tcase \"integer\":\n\t\t\t\tf = Type.Number(desc);\n\t\t\t\tbreak;\n\t\t\tcase \"boolean\":\n\t\t\t\tf = Type.Boolean(desc);\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tf = Type.Any(desc);\n\t\t}\n\t\tfields[key] = required.has(key) ? f : Type.Optional(f as never);\n\t}\n\treturn Type.Object(fields as Record<string, ReturnType<typeof Type.String>>);\n}\n\nfunction sanitizeMcpName(name: string): string {\n\treturn name.replace(/[^a-zA-Z0-9_-]/g, \"_\");\n}\n\nexport type MCPStatusChangeHandler = (serverName: string, status: MCPConnectionStatus) => void;\n\nexport class MCPManager {\n\tprivate clients = new Map<string, MCPClient>();\n\tprivate serverConfigs = new Map<string, MCPServerConfig>();\n\tprivate tools = new Map<string, ToolDefinition[]>();\n\tprivate resourceUris: string[] = [];\n\tprivate reconnectTimers = new Map<string, ReturnType<typeof setTimeout>>();\n\tprivate reconnectAttempts = new Map<string, number>();\n\tprivate _statuses = new Map<string, MCPConnectionStatus>();\n\tprivate toolTimeoutMap = new Map<string, Map<string, number>>();\n\n\t/** Called when any server's connection status changes */\n\tonStatusChange: MCPStatusChangeHandler | null = null;\n\n\tgetServerStatus(serverName: string): MCPConnectionStatus {\n\t\treturn this._statuses.get(serverName) ?? \"disconnected\";\n\t}\n\n\tgetAllStatuses(): Array<{ serverName: string; status: MCPConnectionStatus; toolCount: number }> {\n\t\treturn [...this._statuses.entries()].map(([serverName, status]) => ({\n\t\t\tserverName,\n\t\t\tstatus,\n\t\t\ttoolCount: this.tools.get(serverName)?.length ?? 0,\n\t\t}));\n\t}\n\n\tprivate setStatus(serverName: string, status: MCPConnectionStatus): void {\n\t\tconst prev = this._statuses.get(serverName);\n\t\tif (prev === status) return;\n\t\tthis._statuses.set(serverName, status);\n\t\ttry {\n\t\t\tthis.onStatusChange?.(serverName, status);\n\t\t} catch {\n\t\t\t// ignore\n\t\t}\n\t}\n\n\tasync start(serverConfigs: Record<string, MCPServerConfig>): Promise<void> {\n\t\tconst pending: Array<Promise<void>> = [];\n\t\tfor (const [serverName, config] of Object.entries(serverConfigs)) {\n\t\t\tif (config.enabled === false) {\n\t\t\t\tthis.setStatus(serverName, \"disabled\");\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.serverConfigs.set(serverName, config);\n\t\t\tif (config.toolTimeouts) {\n\t\t\t\tthis.toolTimeoutMap.set(serverName, new Map(Object.entries(config.toolTimeouts)));\n\t\t\t}\n\t\t\tpending.push(this.connectServer(serverName, config));\n\t\t}\n\t\t// Connect all servers in parallel for faster startup\n\t\tawait Promise.allSettled(pending);\n\t}\n\n\t/** Start a single server (used by reloadMcp for targeted toggle). */\n\tasync startServer(serverName: string, config: MCPServerConfig): Promise<void> {\n\t\tthis.serverConfigs.set(serverName, config);\n\t\tif (config.toolTimeouts) {\n\t\t\tthis.toolTimeoutMap.set(serverName, new Map(Object.entries(config.toolTimeouts)));\n\t\t}\n\t\tawait this.connectServer(serverName, config);\n\t}\n\n\t/** Stop a single server without affecting others. */\n\tasync stopServer(serverName: string): Promise<void> {\n\t\t// Cancel reconnect timer for this server\n\t\tconst timer = this.reconnectTimers.get(serverName);\n\t\tif (timer) {\n\t\t\tclearTimeout(timer);\n\t\t\tthis.reconnectTimers.delete(serverName);\n\t\t}\n\t\tthis.reconnectAttempts.delete(serverName);\n\t\tthis.serverConfigs.delete(serverName);\n\t\tthis.toolTimeoutMap.delete(serverName);\n\t\tthis.tools.delete(serverName);\n\n\t\tconst client = this.clients.get(serverName);\n\t\tif (client) {\n\t\t\tclient.onDisconnect = null;\n\t\t\tthis.clients.delete(serverName);\n\t\t\tawait client.disconnect().catch(() => {});\n\t\t}\n\t\tthis.setStatus(serverName, \"disabled\");\n\t}\n\n\tprivate async connectServer(serverName: string, config: MCPServerConfig): Promise<void> {\n\t\tthis.setStatus(serverName, \"reconnecting\");\n\t\ttry {\n\t\t\tconst client = new MCPClient(config.timeoutMs);\n\t\t\tclient.onDisconnect = (_reason) => {\n\t\t\t\tthis.setStatus(serverName, \"disconnected\");\n\t\t\t\tthis.tools.delete(serverName);\n\t\t\t\tthis.scheduleReconnect(serverName);\n\t\t\t};\n\n\t\t\tawait client.connect(config);\n\t\t\tconst { tools: mcpTools } = await client.listTools();\n\t\t\tthis.clients.set(serverName, client);\n\n\t\t\tthis.buildToolDefs(serverName, mcpTools);\n\n\t\t\t// Discover resources\n\t\t\ttry {\n\t\t\t\tconst { resources } = await client.listResources();\n\t\t\t\tfor (const r of resources) {\n\t\t\t\t\tthis.resourceUris.push(`mcp://${serverName}${r.uri}`);\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Server may not support resources\n\t\t\t}\n\n\t\t\tthis.setStatus(serverName, \"connected\");\n\t\t\tthis.reconnectAttempts.delete(serverName);\n\t\t} catch (error) {\n\t\t\tthis.clients.delete(serverName);\n\t\t\tconsole.warn(\n\t\t\t\t`Failed to connect to MCP server \"${serverName}\": ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t\tthis.setStatus(serverName, \"disconnected\");\n\t\t\tthis.scheduleReconnect(serverName);\n\t\t}\n\t}\n\n\tprivate buildToolDefs(serverName: string, mcpTools: MCPToolDefinition[]): void {\n\t\tconst safeServerName = sanitizeMcpName(serverName);\n\t\tconst client = this.clients.get(serverName)!;\n\t\tconst perToolTimeout = this.toolTimeoutMap.get(serverName);\n\n\t\tconst defs: ToolDefinition[] = mcpTools.map((tool) => {\n\t\t\t// Resolve effective timeout: per-tool > server default > client default (120s)\n\t\t\tconst toolSpecificTimeoutMs = perToolTimeout?.get(tool.name);\n\n\t\t\t// Add optional _timeoutMs parameter so the AI can override timeout per-call\n\t\t\tconst baseParams = toTypeBox(tool.inputSchema);\n\t\t\t// Merge _timeoutMs into the existing schema (preserving required fields)\n\t\t\tconst existingProps = (baseParams as any).properties ?? {};\n\t\t\tconst existingRequired = (baseParams as any).required ?? [];\n\t\t\tconst parameters = Type.Object(\n\t\t\t\t{\n\t\t\t\t\t...existingProps,\n\t\t\t\t\t_timeoutMs: Type.Optional(\n\t\t\t\t\t\tType.Number({\n\t\t\t\t\t\t\tdescription:\n\t\t\t\t\t\t\t\t\"Optional timeout override in milliseconds for this specific call (e.g. 300000 for 5 min, 5000 for 5s). Overrides the server default timeout.\",\n\t\t\t\t\t\t}),\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t\t{ required: existingRequired },\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\tname: `mcp_${safeServerName}_${sanitizeMcpName(tool.name)}`,\n\t\t\t\tlabel: `mcp.${serverName}.${tool.name}`,\n\t\t\t\tdescription: tool.description\n\t\t\t\t\t? `${tool.description} (from MCP server \"${serverName}\")`\n\t\t\t\t\t: `Tool from MCP server \"${serverName}\"`,\n\t\t\t\tpromptSnippet: tool.description\n\t\t\t\t\t? `[${serverName}] ${tool.description.split(\"\\n\")[0]}`\n\t\t\t\t\t: `[${serverName}] ${tool.name}`,\n\t\t\t\tparameters,\n\t\t\t\trenderShell: \"default\" as const,\n\t\t\t\texecute: async (_id, params, signal) => {\n\t\t\t\t\tif (!client.isConnected) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\t\t\t\ttext: `MCP server \"${serverName}\" is disconnected. Use /mcp to check status.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { error: \"disconnected\" },\n\t\t\t\t\t\t} satisfies AgentToolResult<unknown>;\n\t\t\t\t\t}\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// Extract _timeoutMs from params (AI-specified timeout override)\n\t\t\t\t\t\tconst rawParams = params as Record<string, unknown>;\n\t\t\t\t\t\tconst callTimeoutMs =\n\t\t\t\t\t\t\ttypeof rawParams._timeoutMs === \"number\" ? rawParams._timeoutMs : toolSpecificTimeoutMs;\n\t\t\t\t\t\tconst { _timeoutMs: _ignored, ...mcpParams } = rawParams;\n\n\t\t\t\t\t\tconst effectiveSignal =\n\t\t\t\t\t\t\tcallTimeoutMs !== undefined\n\t\t\t\t\t\t\t\t? AbortSignal.any([AbortSignal.timeout(callTimeoutMs), ...(signal ? [signal] : [])])\n\t\t\t\t\t\t\t\t: signal;\n\t\t\t\t\t\tconst result = await client.callTool(tool.name, mcpParams, effectiveSignal);\n\t\t\t\t\t\tconst text = result.content\n\t\t\t\t\t\t\t.map((item) => (item.type === \"text\" && item.text ? item.text : `[Image: ${item.mimeType}]`))\n\t\t\t\t\t\t\t.join(\"\\n\");\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text }],\n\t\t\t\t\t\t\tdetails: result,\n\t\t\t\t\t\t} satisfies AgentToolResult<unknown>;\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tconst msg = error instanceof Error ? error.message : String(error);\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: `MCP tool error: ${msg}` }],\n\t\t\t\t\t\t\tdetails: { error: msg },\n\t\t\t\t\t\t} satisfies AgentToolResult<unknown>;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\t\tthis.tools.set(serverName, defs);\n\t}\n\n\tprivate scheduleReconnect(serverName: string): void {\n\t\t// Cancel any existing reconnect timer for this server\n\t\tconst existing = this.reconnectTimers.get(serverName);\n\t\tif (existing) {\n\t\t\tclearTimeout(existing);\n\t\t}\n\n\t\tconst config = this.serverConfigs.get(serverName);\n\t\tif (!config) return;\n\n\t\tconst reconnectCfg = config.reconnect ?? {};\n\t\tif (reconnectCfg.enabled === false) return;\n\n\t\tconst maxAttempts = reconnectCfg.maxAttempts ?? DEFAULT_RECONNECT_MAX_ATTEMPTS;\n\t\tconst intervalMs = reconnectCfg.intervalMs ?? DEFAULT_RECONNECT_INTERVAL_MS;\n\t\tconst attempt = (this.reconnectAttempts.get(serverName) ?? 0) + 1;\n\n\t\tif (attempt > maxAttempts) {\n\t\t\tconsole.warn(`MCP server \"${serverName}\": max reconnection attempts (${maxAttempts}) reached, giving up.`);\n\t\t\tthis.reconnectAttempts.delete(serverName);\n\t\t\treturn;\n\t\t}\n\n\t\tthis.reconnectAttempts.set(serverName, attempt);\n\t\tthis.setStatus(serverName, \"reconnecting\");\n\n\t\tconst timer = setTimeout(async () => {\n\t\t\tthis.reconnectTimers.delete(serverName);\n\t\t\tconst cfg = this.serverConfigs.get(serverName);\n\t\t\tif (!cfg) return;\n\n\t\t\t// Remove old client if any\n\t\t\tconst oldClient = this.clients.get(serverName);\n\t\t\tif (oldClient) {\n\t\t\t\toldClient.onDisconnect = null;\n\t\t\t\tawait oldClient.disconnect().catch(() => {});\n\t\t\t\tthis.clients.delete(serverName);\n\t\t\t}\n\n\t\t\tawait this.connectServer(serverName, cfg);\n\t\t}, intervalMs * Math.min(attempt, 5)); // exponential backoff, cap at 5x\n\n\t\tthis.reconnectTimers.set(serverName, timer);\n\t}\n\n\t/** Manually trigger reconnection for a specific server */\n\tasync reconnect(serverName: string): Promise<void> {\n\t\tconst existingTimer = this.reconnectTimers.get(serverName);\n\t\tif (existingTimer) {\n\t\t\tclearTimeout(existingTimer);\n\t\t\tthis.reconnectTimers.delete(serverName);\n\t\t}\n\n\t\tconst oldClient = this.clients.get(serverName);\n\t\tif (oldClient) {\n\t\t\toldClient.onDisconnect = null;\n\t\t\tawait oldClient.disconnect().catch(() => {});\n\t\t\tthis.clients.delete(serverName);\n\t\t}\n\n\t\tconst config = this.serverConfigs.get(serverName);\n\t\tif (!config) {\n\t\t\tthis.setStatus(serverName, \"disconnected\");\n\t\t\treturn;\n\t\t}\n\n\t\tthis.reconnectAttempts.delete(serverName);\n\t\tawait this.connectServer(serverName, config);\n\t}\n\n\t// Rebuild tool definitions for a server (used after reconnect to pick up any changes)\n\tgetToolDefinitions(): ToolDefinition[] {\n\t\treturn Array.from(this.tools.values()).flat();\n\t}\n\n\tgetServerNames(): string[] {\n\t\treturn [...this.clients.keys()];\n\t}\n\n\tgetResourceUris(): string[] {\n\t\treturn this.resourceUris;\n\t}\n\n\tasync readResource(uri: string): Promise<string> {\n\t\tconst serverName = this.clients.keys().next().value;\n\t\tif (!serverName) throw new Error(\"No MCP servers connected\");\n\t\tconst mcpUri = uri.startsWith(`mcp://${serverName}`) ? uri.slice(`mcp://${serverName}`.length) : uri;\n\t\tconst client = this.clients.get(serverName);\n\t\tif (!client) throw new Error(`MCP server \"${serverName}\" not connected`);\n\t\tconst result = await client.readResource(mcpUri);\n\t\treturn result.contents.map((c) => c.text ?? \"\").join(\"\\n\");\n\t}\n\n\tasync stop(): Promise<void> {\n\t\t// Cancel all reconnect timers\n\t\tfor (const [, timer] of this.reconnectTimers) {\n\t\t\tclearTimeout(timer);\n\t\t}\n\t\tthis.reconnectTimers.clear();\n\t\tthis.reconnectAttempts.clear();\n\n\t\tconst pending = [...this.clients.entries()].map(async ([name, client]) => {\n\t\t\ttry {\n\t\t\t\tawait client.disconnect();\n\t\t\t} catch (error) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`Error disconnecting MCP server \"${name}\": ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t);\n\t\t\t}\n\t\t});\n\t\tthis.clients.clear();\n\t\tthis.tools.clear();\n\t\tthis.resourceUris = [];\n\t\tthis._statuses.clear();\n\t\tawait Promise.all(pending);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/core/mcp/manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,OAAO,EAAE,SAAS,EAAwB,MAAM,aAAa,CAAC;AAG9D,MAAM,8BAA8B,GAAG,CAAC,CAAC;AACzC,MAAM,6BAA6B,GAAG,IAAI,CAAC;AAE3C,SAAS,SAAS,CAAC,MAAwC,EAAkC;IAC5F,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvE,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC;IACD,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;IAChD,MAAM,MAAM,GAA4B,EAAE,CAAC;IAC3C,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;QAC7D,MAAM,CAAC,GAAG,IAAgE,CAAC;QAC3E,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,IAAI,QAAQ,CAAC;QAC7B,MAAM,IAAI,GAAG,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjE,IAAI,CAAU,CAAC;QACf,QAAQ,CAAC,EA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;IAED,eAAe,GAAa;QAC3B,OAAO,IAAI,CAAC,YAAY,CAAC;IAAA,CACzB;IAED,KAAK,CAAC,IAAI,GAAkB;QAC3B,8BAA8B;QAC9B,KAAK,MAAM,CAAC,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YAC9C,YAAY,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC;QACD,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC;QAE/B,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC;YACzE,IAAI,CAAC;gBACJ,MAAM,MAAM,CAAC,UAAU,EAAE,CAAC;YAC3B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,OAAO,CAAC,IAAI,CACX,mCAAmC,IAAI,MAAM,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CACrG,CAAC;YACH,CAAC;QAAA,CACD,CAAC,CAAC;QACH,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC;QACvB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAAA,CAC3B;CACD","sourcesContent":["import type { AgentToolResult } from \"@openeryc/pi-agent-core\";\nimport { Type } from \"typebox\";\nimport type { ToolDefinition } from \"../extensions/types.ts\";\nimport { MCPClient, type MCPServerConfig } from \"./client.ts\";\nimport type { MCPConnectionStatus, MCPToolDefinition } from \"./types.ts\";\n\nconst DEFAULT_RECONNECT_MAX_ATTEMPTS = 5;\nconst DEFAULT_RECONNECT_INTERVAL_MS = 5000;\n\nfunction toTypeBox(schema: MCPToolDefinition[\"inputSchema\"]): ReturnType<typeof Type.Object> {\n\tif (!schema.properties || Object.keys(schema.properties).length === 0) {\n\t\treturn Type.Object({});\n\t}\n\tconst required = new Set(schema.required ?? []);\n\tconst fields: Record<string, unknown> = {};\n\tfor (const [key, prop] of Object.entries(schema.properties)) {\n\t\tconst p = prop as { type?: string; description?: string; enum?: string[] };\n\t\tconst t = p.type ?? \"string\";\n\t\tconst desc = p.description ? { description: p.description } : {};\n\t\tlet f: unknown;\n\t\tswitch (t) {\n\t\t\tcase \"string\":\n\t\t\t\tf = p.enum ? Type.Unsafe<string>({ type: \"string\", enum: p.enum, ...desc }) : Type.String(desc);\n\t\t\t\tbreak;\n\t\t\tcase \"number\":\n\t\t\tcase \"integer\":\n\t\t\t\tf = Type.Number(desc);\n\t\t\t\tbreak;\n\t\t\tcase \"boolean\":\n\t\t\t\tf = Type.Boolean(desc);\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tf = Type.Any(desc);\n\t\t}\n\t\tfields[key] = required.has(key) ? f : Type.Optional(f as never);\n\t}\n\treturn Type.Object(fields as Record<string, ReturnType<typeof Type.String>>);\n}\n\nfunction sanitizeMcpName(name: string): string {\n\treturn name.replace(/[^a-zA-Z0-9_-]/g, \"_\");\n}\n\nexport type MCPStatusChangeHandler = (serverName: string, status: MCPConnectionStatus) => void;\n\nexport class MCPManager {\n\tprivate clients = new Map<string, MCPClient>();\n\tprivate serverConfigs = new Map<string, MCPServerConfig>();\n\tprivate tools = new Map<string, ToolDefinition[]>();\n\tprivate resourceUris: string[] = [];\n\tprivate reconnectTimers = new Map<string, ReturnType<typeof setTimeout>>();\n\tprivate reconnectAttempts = new Map<string, number>();\n\tprivate _statuses = new Map<string, MCPConnectionStatus>();\n\tprivate toolTimeoutMap = new Map<string, Map<string, number>>();\n\n\t/** Called when any server's connection status changes */\n\tonStatusChange: MCPStatusChangeHandler | null = null;\n\n\tgetServerStatus(serverName: string): MCPConnectionStatus {\n\t\treturn this._statuses.get(serverName) ?? \"disconnected\";\n\t}\n\n\tgetAllStatuses(): Array<{ serverName: string; status: MCPConnectionStatus; toolCount: number }> {\n\t\treturn [...this._statuses.entries()].map(([serverName, status]) => ({\n\t\t\tserverName,\n\t\t\tstatus,\n\t\t\ttoolCount: this.tools.get(serverName)?.length ?? 0,\n\t\t}));\n\t}\n\n\tprivate setStatus(serverName: string, status: MCPConnectionStatus): void {\n\t\tconst prev = this._statuses.get(serverName);\n\t\tif (prev === status) return;\n\t\tthis._statuses.set(serverName, status);\n\t\ttry {\n\t\t\tthis.onStatusChange?.(serverName, status);\n\t\t} catch {\n\t\t\t// ignore\n\t\t}\n\t}\n\n\tasync start(serverConfigs: Record<string, MCPServerConfig>): Promise<void> {\n\t\tconst pending: Array<Promise<void>> = [];\n\t\tfor (const [serverName, config] of Object.entries(serverConfigs)) {\n\t\t\tif (config.enabled === false) {\n\t\t\t\tthis.setStatus(serverName, \"disabled\");\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.serverConfigs.set(serverName, config);\n\t\t\tif (config.toolTimeouts) {\n\t\t\t\tthis.toolTimeoutMap.set(serverName, new Map(Object.entries(config.toolTimeouts)));\n\t\t\t}\n\t\t\tpending.push(this.connectServer(serverName, config));\n\t\t}\n\t\t// Connect all servers in parallel for faster startup\n\t\tawait Promise.allSettled(pending);\n\t}\n\n\t/** Start a single server (used by reloadMcp for targeted toggle). */\n\tasync startServer(serverName: string, config: MCPServerConfig): Promise<void> {\n\t\tthis.serverConfigs.set(serverName, config);\n\t\tif (config.toolTimeouts) {\n\t\t\tthis.toolTimeoutMap.set(serverName, new Map(Object.entries(config.toolTimeouts)));\n\t\t}\n\t\tawait this.connectServer(serverName, config);\n\t}\n\n\t/** Stop a single server without affecting others. */\n\tasync stopServer(serverName: string): Promise<void> {\n\t\t// Cancel reconnect timer for this server\n\t\tconst timer = this.reconnectTimers.get(serverName);\n\t\tif (timer) {\n\t\t\tclearTimeout(timer);\n\t\t\tthis.reconnectTimers.delete(serverName);\n\t\t}\n\t\tthis.reconnectAttempts.delete(serverName);\n\t\tthis.serverConfigs.delete(serverName);\n\t\tthis.toolTimeoutMap.delete(serverName);\n\t\tthis.tools.delete(serverName);\n\t\t// Also clean up resource URIs for this server\n\t\tthis.resourceUris = this.resourceUris.filter((u) => !u.startsWith(`mcp://${serverName}`));\n\n\t\tconst client = this.clients.get(serverName);\n\t\tif (client) {\n\t\t\tclient.onDisconnect = null;\n\t\t\tthis.clients.delete(serverName);\n\t\t\tawait client.disconnect().catch(() => {});\n\t\t}\n\t\tthis.setStatus(serverName, \"disabled\");\n\t}\n\n\tprivate async connectServer(serverName: string, config: MCPServerConfig): Promise<void> {\n\t\tthis.setStatus(serverName, \"reconnecting\");\n\t\tlet disconnected = false;\n\t\tconst onDisconnect = () => {\n\t\t\tif (disconnected) return;\n\t\t\tdisconnected = true;\n\t\t\tthis.setStatus(serverName, \"disconnected\");\n\t\t\tthis.tools.delete(serverName);\n\t\t\tthis.scheduleReconnect(serverName);\n\t\t};\n\n\t\ttry {\n\t\t\tconst client = new MCPClient(config.timeoutMs);\n\t\t\tclient.onDisconnect = () => onDisconnect();\n\n\t\t\tawait client.connect(config);\n\t\t\t// If disconnect happened during connect(), bail out\n\t\t\tif (disconnected) {\n\t\t\t\tthis.clients.delete(serverName);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst { tools: mcpTools } = await client.listTools();\n\t\t\tif (disconnected) {\n\t\t\t\tclient.disconnect().catch(() => {});\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthis.clients.set(serverName, client);\n\n\t\t\tthis.buildToolDefs(serverName, mcpTools);\n\n\t\t\ttry {\n\t\t\t\tconst { resources } = await client.listResources();\n\t\t\t\tfor (const r of resources) {\n\t\t\t\t\tthis.resourceUris.push(`mcp://${serverName}${r.uri}`);\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Server may not support resources\n\t\t\t}\n\n\t\t\tif (disconnected) {\n\t\t\t\t// Was disconnected during tool/resource discovery\n\t\t\t\tclient.disconnect().catch(() => {});\n\t\t\t\tthis.clients.delete(serverName);\n\t\t\t\tthis.tools.delete(serverName);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.setStatus(serverName, \"connected\");\n\t\t\tthis.reconnectAttempts.delete(serverName);\n\t\t} catch (error) {\n\t\t\tonDisconnect();\n\t\t\tconsole.warn(\n\t\t\t\t`Failed to connect to MCP server \"${serverName}\": ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate buildToolDefs(serverName: string, mcpTools: MCPToolDefinition[]): void {\n\t\tconst safeServerName = sanitizeMcpName(serverName);\n\t\tconst client = this.clients.get(serverName)!;\n\t\tconst perToolTimeout = this.toolTimeoutMap.get(serverName);\n\n\t\tconst defs: ToolDefinition[] = mcpTools.map((tool) => {\n\t\t\t// Resolve effective timeout: per-tool > server default > client default (120s)\n\t\t\tconst toolSpecificTimeoutMs = perToolTimeout?.get(tool.name);\n\n\t\t\t// Add optional _timeoutMs parameter so the AI can override timeout per-call.\n\t\t\t// We must produce a flat properties schema (not allOf) because LLM providers\n\t\t\t// access tool.parameters.properties directly.\n\t\t\tconst baseParams = toTypeBox(tool.inputSchema);\n\t\t\tconst baseSchema = baseParams as unknown as Record<string, unknown>;\n\t\t\tconst mergedProps = {\n\t\t\t\t...((baseSchema.properties ?? {}) as Record<string, unknown>),\n\t\t\t\t_timeoutMs: {\n\t\t\t\t\ttype: \"number\",\n\t\t\t\t\tdescription:\n\t\t\t\t\t\t\"Optional timeout override in milliseconds for this specific call (e.g. 300000 for 5 min, 5000 for 5s). Overrides the server default timeout.\",\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst mergedRequired = [...((baseSchema.required ?? []) as string[])];\n\t\t\tconst parameters = Type.Object(mergedProps, { required: mergedRequired });\n\n\t\t\treturn {\n\t\t\t\tname: `mcp_${safeServerName}_${sanitizeMcpName(tool.name)}`,\n\t\t\t\tlabel: `mcp.${serverName}.${tool.name}`,\n\t\t\t\tdescription: tool.description\n\t\t\t\t\t? `${tool.description} (from MCP server \"${serverName}\")`\n\t\t\t\t\t: `Tool from MCP server \"${serverName}\"`,\n\t\t\t\tpromptSnippet: tool.description\n\t\t\t\t\t? `[${serverName}] ${tool.description.split(\"\\n\")[0]}`\n\t\t\t\t\t: `[${serverName}] ${tool.name}`,\n\t\t\t\tparameters,\n\t\t\t\trenderShell: \"default\" as const,\n\t\t\t\texecute: async (_id, params, signal) => {\n\t\t\t\t\tif (!client.isConnected) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\t\t\t\ttext: `MCP server \"${serverName}\" is disconnected. Use /mcp to check status.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { error: \"disconnected\" },\n\t\t\t\t\t\t} satisfies AgentToolResult<unknown>;\n\t\t\t\t\t}\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// Extract _timeoutMs from params (AI-specified timeout override)\n\t\t\t\t\t\tconst rawParams = params as Record<string, unknown>;\n\t\t\t\t\t\tconst callTimeoutMs =\n\t\t\t\t\t\t\ttypeof rawParams._timeoutMs === \"number\" ? rawParams._timeoutMs : toolSpecificTimeoutMs;\n\t\t\t\t\t\tconst { _timeoutMs: _ignored, ...mcpParams } = rawParams;\n\n\t\t\t\t\t\tconst effectiveSignal =\n\t\t\t\t\t\t\tcallTimeoutMs !== undefined\n\t\t\t\t\t\t\t\t? AbortSignal.any([AbortSignal.timeout(callTimeoutMs), ...(signal ? [signal] : [])])\n\t\t\t\t\t\t\t\t: signal;\n\t\t\t\t\t\tconst result = await client.callTool(tool.name, mcpParams, effectiveSignal);\n\t\t\t\t\t\tconst text = result.content\n\t\t\t\t\t\t\t.map((item) => (item.type === \"text\" && item.text ? item.text : `[Image: ${item.mimeType}]`))\n\t\t\t\t\t\t\t.join(\"\\n\");\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text }],\n\t\t\t\t\t\t\tdetails: result,\n\t\t\t\t\t\t} satisfies AgentToolResult<unknown>;\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tconst msg = error instanceof Error ? error.message : String(error);\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: `MCP tool error: ${msg}` }],\n\t\t\t\t\t\t\tdetails: { error: msg },\n\t\t\t\t\t\t} satisfies AgentToolResult<unknown>;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\t\tthis.tools.set(serverName, defs);\n\t}\n\n\tprivate scheduleReconnect(serverName: string): void {\n\t\t// Cancel any existing reconnect timer for this server\n\t\tconst existing = this.reconnectTimers.get(serverName);\n\t\tif (existing) {\n\t\t\tclearTimeout(existing);\n\t\t}\n\n\t\tconst config = this.serverConfigs.get(serverName);\n\t\tif (!config) return;\n\n\t\tconst reconnectCfg = config.reconnect ?? {};\n\t\tif (reconnectCfg.enabled === false) return;\n\n\t\tconst maxAttempts = reconnectCfg.maxAttempts ?? DEFAULT_RECONNECT_MAX_ATTEMPTS;\n\t\tconst intervalMs = reconnectCfg.intervalMs ?? DEFAULT_RECONNECT_INTERVAL_MS;\n\t\tconst attempt = (this.reconnectAttempts.get(serverName) ?? 0) + 1;\n\n\t\tif (attempt > maxAttempts) {\n\t\t\tconsole.warn(`MCP server \"${serverName}\": max reconnection attempts (${maxAttempts}) reached, giving up.`);\n\t\t\tthis.reconnectAttempts.delete(serverName);\n\t\t\treturn;\n\t\t}\n\n\t\tthis.reconnectAttempts.set(serverName, attempt);\n\t\tthis.setStatus(serverName, \"reconnecting\");\n\n\t\tconst timer = setTimeout(() => {\n\t\t\tthis.reconnectTimers.delete(serverName);\n\t\t\tconst cfg = this.serverConfigs.get(serverName);\n\t\t\tif (!cfg) return;\n\n\t\t\t// Remove old client if any\n\t\t\tconst oldClient = this.clients.get(serverName);\n\t\t\tif (oldClient) {\n\t\t\t\toldClient.onDisconnect = null;\n\t\t\t\toldClient.disconnect().catch(() => {});\n\t\t\t\tthis.clients.delete(serverName);\n\t\t\t}\n\n\t\t\tthis.connectServer(serverName, cfg).catch((err) =>\n\t\t\t\tconsole.warn(`MCP reconnect failed for \"${serverName}\":`, err),\n\t\t\t);\n\t\t}, intervalMs * Math.min(attempt, 5)); // exponential backoff, cap at 5x\n\n\t\tthis.reconnectTimers.set(serverName, timer);\n\t}\n\n\t/** Manually trigger reconnection for a specific server */\n\tasync reconnect(serverName: string): Promise<void> {\n\t\tconst existingTimer = this.reconnectTimers.get(serverName);\n\t\tif (existingTimer) {\n\t\t\tclearTimeout(existingTimer);\n\t\t\tthis.reconnectTimers.delete(serverName);\n\t\t}\n\n\t\tconst oldClient = this.clients.get(serverName);\n\t\tif (oldClient) {\n\t\t\toldClient.onDisconnect = null;\n\t\t\tawait oldClient.disconnect().catch(() => {});\n\t\t\tthis.clients.delete(serverName);\n\t\t}\n\n\t\tconst config = this.serverConfigs.get(serverName);\n\t\tif (!config) {\n\t\t\tthis.setStatus(serverName, \"disconnected\");\n\t\t\treturn;\n\t\t}\n\n\t\tthis.reconnectAttempts.delete(serverName);\n\t\tawait this.connectServer(serverName, config);\n\t}\n\n\tgetToolDefinitions(): ToolDefinition[] {\n\t\treturn Array.from(this.tools.values()).flat();\n\t}\n\n\t/** Get all MCP tool descriptions grouped by server (for system prompt). */\n\tgetMcpToolDescriptions(): Array<{ serverName: string; tools: Array<{ name: string; description: string }> }> {\n\t\tconst result: Array<{ serverName: string; tools: Array<{ name: string; description: string }> }> = [];\n\t\tfor (const [serverName, defs] of this.tools) {\n\t\t\tconst tools = defs.map((d) => ({\n\t\t\t\tname: d.name,\n\t\t\t\tdescription: d.description ?? `Tool from MCP server \"${serverName}\"`,\n\t\t\t}));\n\t\t\tresult.push({ serverName, tools });\n\t\t}\n\t\treturn result;\n\t}\n\n\tgetServerNames(): string[] {\n\t\treturn [...this.clients.keys()];\n\t}\n\n\tgetResourceUris(): string[] {\n\t\treturn this.resourceUris;\n\t}\n\n\tasync stop(): Promise<void> {\n\t\t// Cancel all reconnect timers\n\t\tfor (const [, timer] of this.reconnectTimers) {\n\t\t\tclearTimeout(timer);\n\t\t}\n\t\tthis.reconnectTimers.clear();\n\t\tthis.reconnectAttempts.clear();\n\n\t\tconst pending = [...this.clients.entries()].map(async ([name, client]) => {\n\t\t\ttry {\n\t\t\t\tawait client.disconnect();\n\t\t\t} catch (error) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`Error disconnecting MCP server \"${name}\": ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t);\n\t\t\t}\n\t\t});\n\t\tthis.clients.clear();\n\t\tthis.tools.clear();\n\t\tthis.resourceUris = [];\n\t\tthis._statuses.clear();\n\t\tawait Promise.all(pending);\n\t}\n}\n"]}
|
package/dist/core/mcp/types.d.ts
CHANGED
|
@@ -32,19 +32,6 @@ export interface MCPResource {
|
|
|
32
32
|
description?: string;
|
|
33
33
|
mimeType?: string;
|
|
34
34
|
}
|
|
35
|
-
export interface MCPResourceTemplate {
|
|
36
|
-
uriTemplate: string;
|
|
37
|
-
name: string;
|
|
38
|
-
description?: string;
|
|
39
|
-
}
|
|
40
|
-
export interface MCPReadResourceResult {
|
|
41
|
-
contents: Array<{
|
|
42
|
-
uri: string;
|
|
43
|
-
mimeType?: string;
|
|
44
|
-
text?: string;
|
|
45
|
-
blob?: string;
|
|
46
|
-
}>;
|
|
47
|
-
}
|
|
48
35
|
export interface MCPToolDefinition {
|
|
49
36
|
name: string;
|
|
50
37
|
description?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/core/mcp/types.ts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CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAClF,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB","sourcesContent":["/**\n * MCP (Model Context Protocol) client types.\n */\n\nexport interface MCPReconnectConfig {\n\t/** Whether auto-reconnect is enabled (default: true) */\n\tenabled?: boolean;\n\t/** Maximum reconnection attempts before giving up (default: 5) */\n\tmaxAttempts?: number;\n\t/** Delay between reconnection attempts in milliseconds (default: 5000) */\n\tintervalMs?: number;\n}\n\nexport interface MCPServerConfig {\n\tenabled?: boolean;\n\tcommand?: string;\n\targs?: string[];\n\tenv?: Record<string, string>;\n\turl?: string;\n\ttransport?: \"stdio\" | \"sse\" | \"http\";\n\theaders?: Record<string, string>;\n\t/** Request timeout in milliseconds (default: 120000). 0 = no timeout. */\n\ttimeoutMs?: number;\n\t/** Per-tool timeout overrides (key: tool name, value: timeout in ms). Takes precedence over timeoutMs. */\n\ttoolTimeouts?: Record<string, number>;\n\t/** Auto-reconnect configuration */\n\treconnect?: MCPReconnectConfig;\n}\n\n/** Connection status for an MCP server */\nexport type MCPConnectionStatus = \"connected\" | \"disconnected\" | \"reconnecting\" | \"disabled\";\n\nexport interface MCPResource {\n\turi: string;\n\tname: string;\n\tdescription?: string;\n\tmimeType?: string;\n}\n\nexport interface MCPToolDefinition {\n\tname: string;\n\tdescription?: string;\n\tinputSchema: {\n\t\ttype: string;\n\t\tproperties?: Record<string, unknown>;\n\t\trequired?: string[];\n\t};\n}\n\nexport interface MCPCallToolResult {\n\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\tisError?: boolean;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/core/mcp/types.ts"],"names":[],"mappings":"AAAA;;GAEG","sourcesContent":["/**\n * MCP (Model Context Protocol) client types.\n */\n\nexport interface MCPReconnectConfig {\n\t/** Whether auto-reconnect is enabled (default: true) */\n\tenabled?: boolean;\n\t/** Maximum reconnection attempts before giving up (default: 5) */\n\tmaxAttempts?: number;\n\t/** Delay between reconnection attempts in milliseconds (default: 5000) */\n\tintervalMs?: number;\n}\n\nexport interface MCPServerConfig {\n\tenabled?: boolean;\n\tcommand?: string;\n\targs?: string[];\n\tenv?: Record<string, string>;\n\turl?: string;\n\ttransport?: \"stdio\" | \"sse\" | \"http\";\n\theaders?: Record<string, string>;\n\t/** Request timeout in milliseconds (default: 120000). 0 = no timeout. */\n\ttimeoutMs?: number;\n\t/** Per-tool timeout overrides (key: tool name, value: timeout in ms). Takes precedence over timeoutMs. */\n\ttoolTimeouts?: Record<string, number>;\n\t/** Auto-reconnect configuration */\n\treconnect?: MCPReconnectConfig;\n}\n\n/** Connection status for an MCP server */\nexport type MCPConnectionStatus = \"connected\" | \"disconnected\" | \"reconnecting\" | \"disabled\";\n\nexport interface MCPResource {\n\turi: string;\n\tname: string;\n\tdescription?: string;\n\tmimeType?: string;\n}\n\nexport interface
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/core/mcp/types.ts"],"names":[],"mappings":"AAAA;;GAEG","sourcesContent":["/**\n * MCP (Model Context Protocol) client types.\n */\n\nexport interface MCPReconnectConfig {\n\t/** Whether auto-reconnect is enabled (default: true) */\n\tenabled?: boolean;\n\t/** Maximum reconnection attempts before giving up (default: 5) */\n\tmaxAttempts?: number;\n\t/** Delay between reconnection attempts in milliseconds (default: 5000) */\n\tintervalMs?: number;\n}\n\nexport interface MCPServerConfig {\n\tenabled?: boolean;\n\tcommand?: string;\n\targs?: string[];\n\tenv?: Record<string, string>;\n\turl?: string;\n\ttransport?: \"stdio\" | \"sse\" | \"http\";\n\theaders?: Record<string, string>;\n\t/** Request timeout in milliseconds (default: 120000). 0 = no timeout. */\n\ttimeoutMs?: number;\n\t/** Per-tool timeout overrides (key: tool name, value: timeout in ms). Takes precedence over timeoutMs. */\n\ttoolTimeouts?: Record<string, number>;\n\t/** Auto-reconnect configuration */\n\treconnect?: MCPReconnectConfig;\n}\n\n/** Connection status for an MCP server */\nexport type MCPConnectionStatus = \"connected\" | \"disconnected\" | \"reconnecting\" | \"disabled\";\n\nexport interface MCPResource {\n\turi: string;\n\tname: string;\n\tdescription?: string;\n\tmimeType?: string;\n}\n\nexport interface MCPToolDefinition {\n\tname: string;\n\tdescription?: string;\n\tinputSchema: {\n\t\ttype: string;\n\t\tproperties?: Record<string, unknown>;\n\t\trequired?: string[];\n\t};\n}\n\nexport interface MCPCallToolResult {\n\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\tisError?: boolean;\n}\n"]}
|
|
@@ -2,8 +2,101 @@
|
|
|
2
2
|
* Model registry - manages built-in and custom models, provides API key resolution.
|
|
3
3
|
*/
|
|
4
4
|
import { type Api, type AssistantMessageEventStream, type Context, type Model, type OAuthProviderInterface, type SimpleStreamOptions } from "@openeryc/pi-ai";
|
|
5
|
+
import { type Static, Type } from "typebox";
|
|
5
6
|
import type { AuthStatus, AuthStorage, OpenAICompatibleProviderInfo } from "./auth-storage.ts";
|
|
6
7
|
import { clearConfigValueCache } from "./resolve-config-value.ts";
|
|
8
|
+
declare const ProviderCompatSchema: Type.TUnion<[Type.TObject<{
|
|
9
|
+
supportsStore: Type.TOptional<Type.TBoolean>;
|
|
10
|
+
supportsDeveloperRole: Type.TOptional<Type.TBoolean>;
|
|
11
|
+
supportsReasoningEffort: Type.TOptional<Type.TBoolean>;
|
|
12
|
+
supportsUsageInStreaming: Type.TOptional<Type.TBoolean>;
|
|
13
|
+
maxTokensField: Type.TOptional<Type.TUnion<[Type.TLiteral<"max_completion_tokens">, Type.TLiteral<"max_tokens">]>>;
|
|
14
|
+
requiresToolResultName: Type.TOptional<Type.TBoolean>;
|
|
15
|
+
requiresAssistantAfterToolResult: Type.TOptional<Type.TBoolean>;
|
|
16
|
+
requiresThinkingAsText: Type.TOptional<Type.TBoolean>;
|
|
17
|
+
requiresReasoningContentOnAssistantMessages: Type.TOptional<Type.TBoolean>;
|
|
18
|
+
thinkingFormat: Type.TOptional<Type.TUnion<[Type.TLiteral<"openai">, Type.TLiteral<"openrouter">, Type.TLiteral<"together">, Type.TLiteral<"deepseek">, Type.TLiteral<"zai">, Type.TLiteral<"qwen">, Type.TLiteral<"qwen-chat-template">]>>;
|
|
19
|
+
cacheControlFormat: Type.TOptional<Type.TLiteral<"anthropic">>;
|
|
20
|
+
openRouterRouting: Type.TOptional<Type.TObject<{
|
|
21
|
+
allow_fallbacks: Type.TOptional<Type.TBoolean>;
|
|
22
|
+
require_parameters: Type.TOptional<Type.TBoolean>;
|
|
23
|
+
data_collection: Type.TOptional<Type.TUnion<[Type.TLiteral<"deny">, Type.TLiteral<"allow">]>>;
|
|
24
|
+
zdr: Type.TOptional<Type.TBoolean>;
|
|
25
|
+
enforce_distillable_text: Type.TOptional<Type.TBoolean>;
|
|
26
|
+
order: Type.TOptional<Type.TArray<Type.TString>>;
|
|
27
|
+
only: Type.TOptional<Type.TArray<Type.TString>>;
|
|
28
|
+
ignore: Type.TOptional<Type.TArray<Type.TString>>;
|
|
29
|
+
quantizations: Type.TOptional<Type.TArray<Type.TString>>;
|
|
30
|
+
sort: Type.TOptional<Type.TUnion<[Type.TString, Type.TObject<{
|
|
31
|
+
by: Type.TOptional<Type.TString>;
|
|
32
|
+
partition: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
33
|
+
}>]>>;
|
|
34
|
+
max_price: Type.TOptional<Type.TObject<{
|
|
35
|
+
prompt: Type.TOptional<Type.TUnion<[Type.TNumber, Type.TString]>>;
|
|
36
|
+
completion: Type.TOptional<Type.TUnion<[Type.TNumber, Type.TString]>>;
|
|
37
|
+
image: Type.TOptional<Type.TUnion<[Type.TNumber, Type.TString]>>;
|
|
38
|
+
audio: Type.TOptional<Type.TUnion<[Type.TNumber, Type.TString]>>;
|
|
39
|
+
request: Type.TOptional<Type.TUnion<[Type.TNumber, Type.TString]>>;
|
|
40
|
+
}>>;
|
|
41
|
+
preferred_min_throughput: Type.TOptional<Type.TUnion<[Type.TNumber, Type.TObject<{
|
|
42
|
+
p50: Type.TOptional<Type.TNumber>;
|
|
43
|
+
p75: Type.TOptional<Type.TNumber>;
|
|
44
|
+
p90: Type.TOptional<Type.TNumber>;
|
|
45
|
+
p99: Type.TOptional<Type.TNumber>;
|
|
46
|
+
}>]>>;
|
|
47
|
+
preferred_max_latency: Type.TOptional<Type.TUnion<[Type.TNumber, Type.TObject<{
|
|
48
|
+
p50: Type.TOptional<Type.TNumber>;
|
|
49
|
+
p75: Type.TOptional<Type.TNumber>;
|
|
50
|
+
p90: Type.TOptional<Type.TNumber>;
|
|
51
|
+
p99: Type.TOptional<Type.TNumber>;
|
|
52
|
+
}>]>>;
|
|
53
|
+
}>>;
|
|
54
|
+
vercelGatewayRouting: Type.TOptional<Type.TObject<{
|
|
55
|
+
only: Type.TOptional<Type.TArray<Type.TString>>;
|
|
56
|
+
order: Type.TOptional<Type.TArray<Type.TString>>;
|
|
57
|
+
}>>;
|
|
58
|
+
supportsStrictMode: Type.TOptional<Type.TBoolean>;
|
|
59
|
+
supportsLongCacheRetention: Type.TOptional<Type.TBoolean>;
|
|
60
|
+
}>, Type.TObject<{
|
|
61
|
+
sendSessionIdHeader: Type.TOptional<Type.TBoolean>;
|
|
62
|
+
supportsLongCacheRetention: Type.TOptional<Type.TBoolean>;
|
|
63
|
+
}>, Type.TObject<{
|
|
64
|
+
supportsEagerToolInputStreaming: Type.TOptional<Type.TBoolean>;
|
|
65
|
+
supportsLongCacheRetention: Type.TOptional<Type.TBoolean>;
|
|
66
|
+
}>]>;
|
|
67
|
+
/** Writable provider config for models.json (subset used by TUI Manage). */
|
|
68
|
+
export type ModelsJsonProviderWrite = {
|
|
69
|
+
baseUrl?: string;
|
|
70
|
+
apiKey?: string;
|
|
71
|
+
api?: string;
|
|
72
|
+
headers?: Record<string, string>;
|
|
73
|
+
authHeader?: boolean;
|
|
74
|
+
compat?: Static<typeof ProviderCompatSchema>;
|
|
75
|
+
models?: Array<{
|
|
76
|
+
id: string;
|
|
77
|
+
name?: string;
|
|
78
|
+
api?: string;
|
|
79
|
+
reasoning?: boolean;
|
|
80
|
+
input?: Array<"text" | "image">;
|
|
81
|
+
contextWindow?: number;
|
|
82
|
+
maxTokens?: number;
|
|
83
|
+
cost?: {
|
|
84
|
+
input: number;
|
|
85
|
+
output: number;
|
|
86
|
+
cacheRead: number;
|
|
87
|
+
cacheWrite: number;
|
|
88
|
+
};
|
|
89
|
+
}>;
|
|
90
|
+
};
|
|
91
|
+
export type ModelsJsonCustomProviderInfo = {
|
|
92
|
+
name: string;
|
|
93
|
+
baseUrl?: string;
|
|
94
|
+
api?: string;
|
|
95
|
+
hasApiKey: boolean;
|
|
96
|
+
modelIds: string[];
|
|
97
|
+
/** True when this provider name is a built-in (override/merge), not purely custom. */
|
|
98
|
+
isBuiltIn: boolean;
|
|
99
|
+
};
|
|
7
100
|
export type ResolvedRequestAuth = {
|
|
8
101
|
ok: true;
|
|
9
102
|
apiKey?: string;
|
|
@@ -36,6 +129,24 @@ export declare class ModelRegistry {
|
|
|
36
129
|
* Get any error from loading models.json (undefined if no error).
|
|
37
130
|
*/
|
|
38
131
|
getError(): string | undefined;
|
|
132
|
+
/** Path to models.json if this registry is file-backed. */
|
|
133
|
+
getModelsJsonPath(): string | undefined;
|
|
134
|
+
/**
|
|
135
|
+
* List providers defined in models.json (custom + overrides).
|
|
136
|
+
* Returns empty when no models.json or on parse error.
|
|
137
|
+
*/
|
|
138
|
+
listModelsJsonProviders(): ModelsJsonCustomProviderInfo[];
|
|
139
|
+
/**
|
|
140
|
+
* Upsert a provider entry in models.json, then refresh the registry.
|
|
141
|
+
* Rewrites the file as pretty JSON (comments/trailing commas are not preserved).
|
|
142
|
+
*/
|
|
143
|
+
upsertProviderInModelsJson(providerName: string, config: ModelsJsonProviderWrite): void;
|
|
144
|
+
/**
|
|
145
|
+
* Remove a provider entry from models.json, then refresh.
|
|
146
|
+
*/
|
|
147
|
+
removeProviderFromModelsJson(providerName: string): void;
|
|
148
|
+
private readModelsConfigFile;
|
|
149
|
+
private writeModelsConfigFile;
|
|
39
150
|
private loadModels;
|
|
40
151
|
/** Load built-in models and apply provider/model overrides */
|
|
41
152
|
private loadBuiltInModels;
|
|
@@ -166,4 +277,5 @@ export interface ProviderConfigInput {
|
|
|
166
277
|
compat?: Model<Api>["compat"];
|
|
167
278
|
}>;
|
|
168
279
|
}
|
|
280
|
+
export {};
|
|
169
281
|
//# sourceMappingURL=model-registry.d.ts.map
|