@opentiny/next-sdk 0.1.8 → 0.1.10

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.
@@ -22635,25 +22635,14 @@
22635
22635
  await this.client.connect(this.transport);
22636
22636
  return { transport: this.transport, sessionId: this.transport.sessionId };
22637
22637
  }
22638
- const { url, token, sessionId, authProvider, type: type2, agent, onError, onUnauthorized, onReconnect } = options;
22638
+ const { url, token, sessionId, type: type2, agent, onError } = options;
22639
22639
  if (agent === true) {
22640
- const proxyOptions = { client: this.client, url, token, sessionId, authProvider };
22641
- let reconnect = false;
22640
+ const proxyOptions = { client: this.client, url, token, sessionId };
22642
22641
  let response;
22643
22642
  const connectProxy = async () => {
22644
22643
  const { transport: transport2, sessionId: sessionId2 } = type2 === "sse" ? await createSseProxy(proxyOptions) : type2 === "socket" ? await createSocketProxy(proxyOptions) : await createStreamProxy(proxyOptions);
22645
22644
  transport2.onerror = async (error2) => {
22646
22645
  onError == null ? void 0 : onError(error2);
22647
- if (error2.message === "Unauthorized" && !reconnect) {
22648
- if (typeof onUnauthorized === "function") {
22649
- await onUnauthorized(connectProxy);
22650
- } else {
22651
- reconnect = true;
22652
- await connectProxy();
22653
- reconnect = false;
22654
- await (onReconnect == null ? void 0 : onReconnect());
22655
- }
22656
- }
22657
22646
  };
22658
22647
  response = { transport: transport2, sessionId: sessionId2 };
22659
22648
  };
@@ -22667,14 +22656,9 @@
22667
22656
  await this.client.connect(transport);
22668
22657
  }
22669
22658
  if (type2 === "sse") {
22670
- if (authProvider) {
22671
- const createTransport = () => new SSEClientTransport(endpoint, { authProvider });
22672
- transport = await attemptConnection(this.client, authProvider.waitForOAuthCode, createTransport);
22673
- } else {
22674
- const opts = sseOptions(token, sessionId);
22675
- transport = new SSEClientTransport(endpoint, opts);
22676
- await this.client.connect(transport);
22677
- }
22659
+ const opts = sseOptions(token, sessionId);
22660
+ transport = new SSEClientTransport(endpoint, opts);
22661
+ await this.client.connect(transport);
22678
22662
  }
22679
22663
  if (type2 === "socket") {
22680
22664
  transport = new WebSocketClientTransport(new URL(`${url}?sessionId=${sessionId}&token=${token}`));
@@ -22682,14 +22666,9 @@
22682
22666
  await this.client.connect(transport);
22683
22667
  }
22684
22668
  if (typeof transport === "undefined") {
22685
- if (authProvider) {
22686
- const createTransport = () => new StreamableHTTPClientTransport(endpoint, { authProvider });
22687
- transport = await attemptConnection(this.client, authProvider.waitForOAuthCode, createTransport);
22688
- } else {
22689
- const opts = streamOptions(token, sessionId);
22690
- transport = new StreamableHTTPClientTransport(endpoint, opts);
22691
- await this.client.connect(transport);
22692
- }
22669
+ const opts = streamOptions(token, sessionId);
22670
+ transport = new StreamableHTTPClientTransport(endpoint, opts);
22671
+ await this.client.connect(transport);
22693
22672
  }
22694
22673
  this.transport = transport;
22695
22674
  return { transport: this.transport, sessionId: this.transport.sessionId };
@@ -25102,7 +25081,7 @@
25102
25081
  this.copyToClipboard(this.options.sessionId.slice(-6));
25103
25082
  }
25104
25083
  copyRemoteURL() {
25105
- this.copyToClipboard((this.options.remoteUrl || DEFAULT_REMOTE_URL) + this.sessionPrefix + this.options.sessionId);
25084
+ this.copyToClipboard(this.options.remoteUrl + this.sessionPrefix + this.options.sessionId);
25106
25085
  }
25107
25086
  // 实现复制到剪贴板功能
25108
25087
  async copyToClipboard(text2) {
@@ -43203,12 +43182,12 @@ ${user}:`]
43203
43182
  };
43204
43183
  class AgentModelProvider {
43205
43184
  constructor({ llmConfig, mcpServers, llm }) {
43206
- this.mcpServers = [];
43185
+ this.mcpServers = {};
43207
43186
  this.mcpClients = [];
43208
43187
  this.mcpTools = [];
43209
43188
  this.ignoreToolnames = [];
43210
43189
  this.messages = [];
43211
- this.mcpServers = mcpServers || [];
43190
+ this.mcpServers = mcpServers || {};
43212
43191
  if (llm) {
43213
43192
  this.llm = llm;
43214
43193
  } else if (llmConfig) {
@@ -43226,7 +43205,7 @@ ${user}:`]
43226
43205
  throw new Error("Either llmConfig or llm must be provided");
43227
43206
  }
43228
43207
  }
43229
- /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 Null */
43208
+ /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
43230
43209
  async _createOneClient(serverConfig) {
43231
43210
  try {
43232
43211
  let transport;
@@ -43235,7 +43214,9 @@ ${user}:`]
43235
43214
  } else {
43236
43215
  transport = serverConfig;
43237
43216
  }
43238
- return await createMCPClient({ transport });
43217
+ const client = await createMCPClient({ transport });
43218
+ client["__transport__"] = transport;
43219
+ return client;
43239
43220
  } catch (error2) {
43240
43221
  if (this.onError) {
43241
43222
  this.onError((error2 == null ? void 0 : error2.message) || `Failed to create MCP client`, error2);
@@ -43244,15 +43225,25 @@ ${user}:`]
43244
43225
  return null;
43245
43226
  }
43246
43227
  }
43247
- /** 创建 ai-sdk的 mcpClient, 失败则保存为null */
43228
+ /** 关闭一个 mcpClient */
43229
+ async _closeOneClient(client) {
43230
+ var _a16, _b8, _c, _d, _e;
43231
+ try {
43232
+ await ((_b8 = (_a16 = client["__transport__"]) == null ? void 0 : _a16.terminateSession) == null ? void 0 : _b8.call(_a16));
43233
+ await ((_d = (_c = client["__transport__"]) == null ? void 0 : _c.close) == null ? void 0 : _d.call(_c));
43234
+ await ((_e = client == null ? void 0 : client.close) == null ? void 0 : _e.call(client));
43235
+ } catch (error2) {
43236
+ }
43237
+ }
43238
+ /** 创建所有 mcpClients */
43248
43239
  async _createMpcClients() {
43249
43240
  this.mcpClients = await Promise.all(
43250
- this.mcpServers.map(async (server) => {
43241
+ Object.values(this.mcpServers).map(async (server) => {
43251
43242
  return this._createOneClient(server);
43252
43243
  })
43253
43244
  );
43254
43245
  }
43255
- /** 创建所有 mcpClients 的 tools, 失败则保存为null */
43246
+ /** 查询所有 mcpClients 的 tools, 失败则保存为null */
43256
43247
  async _createMpcTools() {
43257
43248
  this.mcpTools = await Promise.all(
43258
43249
  this.mcpClients.map(async (client) => {
@@ -43274,7 +43265,7 @@ ${user}:`]
43274
43265
  await Promise.all(
43275
43266
  this.mcpClients.map(async (client) => {
43276
43267
  try {
43277
- await (client == null ? void 0 : client.close());
43268
+ await this._closeOneClient(client);
43278
43269
  } catch (error2) {
43279
43270
  if (this.onError) {
43280
43271
  this.onError((error2 == null ? void 0 : error2.message) || `Failed to close client`, error2);
@@ -43284,35 +43275,44 @@ ${user}:`]
43284
43275
  })
43285
43276
  );
43286
43277
  }
43278
+ /** 创建所有的 mcpClients,并更新它们的tools */
43287
43279
  async initClientsAndTools() {
43280
+ var _a16;
43288
43281
  await this._createMpcClients();
43289
43282
  await this._createMpcTools();
43283
+ (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
43290
43284
  }
43285
+ /** 全量更新所有的 mcpServers */
43291
43286
  async updateMcpServers(mcpServers) {
43292
43287
  await this.closeAll();
43293
- this.mcpServers = mcpServers;
43288
+ this.mcpServers = mcpServers || this.mcpServers;
43294
43289
  await this.initClientsAndTools();
43295
43290
  }
43296
- async insertMcpServer(mcpServer) {
43297
- var _a16;
43298
- const find = this.mcpServers.find((item) => "url" in item && "url" in mcpServer && item.url === mcpServer.url);
43299
- if (!find) {
43300
- this.mcpServers = [...this.mcpServers, mcpServer];
43301
- const client = await this._createOneClient(mcpServer);
43302
- this.mcpClients.push(client);
43303
- this.mcpTools.push(await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)));
43304
- return true;
43291
+ /** 插入一个新的mcpServer,如果已经存在则返回false */
43292
+ async insertMcpServer(serverName, mcpServer) {
43293
+ var _a16, _b8;
43294
+ if (this.mcpServers[serverName]) {
43295
+ return false;
43305
43296
  }
43306
- return false;
43297
+ this.mcpServers[serverName] = mcpServer;
43298
+ const client = await this._createOneClient(mcpServer);
43299
+ this.mcpClients.push(client);
43300
+ this.mcpTools.push(await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)));
43301
+ (_b8 = this.onUpdatedTools) == null ? void 0 : _b8.call(this);
43302
+ return true;
43307
43303
  }
43308
- /** 通过引用,删除一个 mcpServers mcpClients mcpTools ignoreToolnames */
43309
- removeMcpServer(mcpServer) {
43310
- const index = this.mcpServers.findIndex((server) => server === mcpServer);
43311
- this.mcpServers.splice(index, 1);
43304
+ /** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
43305
+ async removeMcpServer(serverName) {
43306
+ if (!this.mcpServers[serverName]) {
43307
+ return;
43308
+ }
43309
+ const serverNames = Object.keys(this.mcpServers);
43310
+ const index = serverNames.indexOf(serverName);
43311
+ delete this.mcpServers[serverName];
43312
43312
  const delClient = this.mcpClients[index];
43313
43313
  this.mcpClients.splice(index, 1);
43314
43314
  try {
43315
- delClient == null ? void 0 : delClient.close();
43315
+ await this._closeOneClient(delClient);
43316
43316
  } catch (error2) {
43317
43317
  }
43318
43318
  const delTool = this.mcpTools[index];
@@ -43333,12 +43333,11 @@ ${user}:`]
43333
43333
  return toolsResult;
43334
43334
  }
43335
43335
  async _chat(chatMethod, { model, maxSteps = 5, ...options }) {
43336
- var _a16, _b8;
43336
+ var _a16;
43337
43337
  if (!this.llm) {
43338
43338
  throw new Error("LLM is not initialized");
43339
43339
  }
43340
43340
  await this.initClientsAndTools();
43341
- (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
43342
43341
  const chatOptions = {
43343
43342
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
43344
43343
  model: this.llm(model),
@@ -43351,7 +43350,7 @@ ${user}:`]
43351
43350
  chatOptions.messages = [...this.messages];
43352
43351
  }
43353
43352
  const result = chatMethod(chatOptions);
43354
- (_b8 = result == null ? void 0 : result.response) == null ? void 0 : _b8.then((res) => {
43353
+ (_a16 = result == null ? void 0 : result.response) == null ? void 0 : _a16.then((res) => {
43355
43354
  this.messages.push(...res.messages);
43356
43355
  });
43357
43356
  return result;