@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.
@@ -226,7 +226,7 @@ class FloatingBlock {
226
226
  this.copyToClipboard(this.options.sessionId.slice(-6));
227
227
  }
228
228
  copyRemoteURL() {
229
- this.copyToClipboard((this.options.remoteUrl || DEFAULT_REMOTE_URL) + this.sessionPrefix + this.options.sessionId);
229
+ this.copyToClipboard(this.options.remoteUrl + this.sessionPrefix + this.options.sessionId);
230
230
  }
231
231
  // 实现复制到剪贴板功能
232
232
  copyToClipboard(text) {
@@ -24640,12 +24640,12 @@ ${user}:`]
24640
24640
  };
24641
24641
  class AgentModelProvider {
24642
24642
  constructor({ llmConfig, mcpServers, llm }) {
24643
- this.mcpServers = [];
24643
+ this.mcpServers = {};
24644
24644
  this.mcpClients = [];
24645
24645
  this.mcpTools = [];
24646
24646
  this.ignoreToolnames = [];
24647
24647
  this.messages = [];
24648
- this.mcpServers = mcpServers || [];
24648
+ this.mcpServers = mcpServers || {};
24649
24649
  if (llm) {
24650
24650
  this.llm = llm;
24651
24651
  } else if (llmConfig) {
@@ -24663,7 +24663,7 @@ ${user}:`]
24663
24663
  throw new Error("Either llmConfig or llm must be provided");
24664
24664
  }
24665
24665
  }
24666
- /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 Null */
24666
+ /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
24667
24667
  async _createOneClient(serverConfig) {
24668
24668
  try {
24669
24669
  let transport;
@@ -24672,7 +24672,9 @@ ${user}:`]
24672
24672
  } else {
24673
24673
  transport = serverConfig;
24674
24674
  }
24675
- return await createMCPClient({ transport });
24675
+ const client = await createMCPClient({ transport });
24676
+ client["__transport__"] = transport;
24677
+ return client;
24676
24678
  } catch (error) {
24677
24679
  if (this.onError) {
24678
24680
  this.onError((error == null ? void 0 : error.message) || `Failed to create MCP client`, error);
@@ -24681,15 +24683,25 @@ ${user}:`]
24681
24683
  return null;
24682
24684
  }
24683
24685
  }
24684
- /** 创建 ai-sdk的 mcpClient, 失败则保存为null */
24686
+ /** 关闭一个 mcpClient */
24687
+ async _closeOneClient(client) {
24688
+ var _a16, _b8, _c, _d, _e;
24689
+ try {
24690
+ await ((_b8 = (_a16 = client["__transport__"]) == null ? void 0 : _a16.terminateSession) == null ? void 0 : _b8.call(_a16));
24691
+ await ((_d = (_c = client["__transport__"]) == null ? void 0 : _c.close) == null ? void 0 : _d.call(_c));
24692
+ await ((_e = client == null ? void 0 : client.close) == null ? void 0 : _e.call(client));
24693
+ } catch (error) {
24694
+ }
24695
+ }
24696
+ /** 创建所有 mcpClients */
24685
24697
  async _createMpcClients() {
24686
24698
  this.mcpClients = await Promise.all(
24687
- this.mcpServers.map(async (server) => {
24699
+ Object.values(this.mcpServers).map(async (server) => {
24688
24700
  return this._createOneClient(server);
24689
24701
  })
24690
24702
  );
24691
24703
  }
24692
- /** 创建所有 mcpClients 的 tools, 失败则保存为null */
24704
+ /** 查询所有 mcpClients 的 tools, 失败则保存为null */
24693
24705
  async _createMpcTools() {
24694
24706
  this.mcpTools = await Promise.all(
24695
24707
  this.mcpClients.map(async (client) => {
@@ -24711,7 +24723,7 @@ ${user}:`]
24711
24723
  await Promise.all(
24712
24724
  this.mcpClients.map(async (client) => {
24713
24725
  try {
24714
- await (client == null ? void 0 : client.close());
24726
+ await this._closeOneClient(client);
24715
24727
  } catch (error) {
24716
24728
  if (this.onError) {
24717
24729
  this.onError((error == null ? void 0 : error.message) || `Failed to close client`, error);
@@ -24721,35 +24733,44 @@ ${user}:`]
24721
24733
  })
24722
24734
  );
24723
24735
  }
24736
+ /** 创建所有的 mcpClients,并更新它们的tools */
24724
24737
  async initClientsAndTools() {
24738
+ var _a16;
24725
24739
  await this._createMpcClients();
24726
24740
  await this._createMpcTools();
24741
+ (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
24727
24742
  }
24743
+ /** 全量更新所有的 mcpServers */
24728
24744
  async updateMcpServers(mcpServers) {
24729
24745
  await this.closeAll();
24730
- this.mcpServers = mcpServers;
24746
+ this.mcpServers = mcpServers || this.mcpServers;
24731
24747
  await this.initClientsAndTools();
24732
24748
  }
24733
- async insertMcpServer(mcpServer) {
24734
- var _a16;
24735
- const find = this.mcpServers.find((item) => "url" in item && "url" in mcpServer && item.url === mcpServer.url);
24736
- if (!find) {
24737
- this.mcpServers = [...this.mcpServers, mcpServer];
24738
- const client = await this._createOneClient(mcpServer);
24739
- this.mcpClients.push(client);
24740
- this.mcpTools.push(await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)));
24741
- return true;
24749
+ /** 插入一个新的mcpServer,如果已经存在则返回false */
24750
+ async insertMcpServer(serverName, mcpServer) {
24751
+ var _a16, _b8;
24752
+ if (this.mcpServers[serverName]) {
24753
+ return false;
24742
24754
  }
24743
- return false;
24755
+ this.mcpServers[serverName] = mcpServer;
24756
+ const client = await this._createOneClient(mcpServer);
24757
+ this.mcpClients.push(client);
24758
+ this.mcpTools.push(await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)));
24759
+ (_b8 = this.onUpdatedTools) == null ? void 0 : _b8.call(this);
24760
+ return true;
24744
24761
  }
24745
- /** 通过引用,删除一个 mcpServers mcpClients mcpTools ignoreToolnames */
24746
- removeMcpServer(mcpServer) {
24747
- const index = this.mcpServers.findIndex((server) => server === mcpServer);
24748
- this.mcpServers.splice(index, 1);
24762
+ /** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
24763
+ async removeMcpServer(serverName) {
24764
+ if (!this.mcpServers[serverName]) {
24765
+ return;
24766
+ }
24767
+ const serverNames = Object.keys(this.mcpServers);
24768
+ const index = serverNames.indexOf(serverName);
24769
+ delete this.mcpServers[serverName];
24749
24770
  const delClient = this.mcpClients[index];
24750
24771
  this.mcpClients.splice(index, 1);
24751
24772
  try {
24752
- delClient == null ? void 0 : delClient.close();
24773
+ await this._closeOneClient(delClient);
24753
24774
  } catch (error) {
24754
24775
  }
24755
24776
  const delTool = this.mcpTools[index];
@@ -24770,12 +24791,11 @@ ${user}:`]
24770
24791
  return toolsResult;
24771
24792
  }
24772
24793
  async _chat(chatMethod, { model, maxSteps = 5, ...options }) {
24773
- var _a16, _b8;
24794
+ var _a16;
24774
24795
  if (!this.llm) {
24775
24796
  throw new Error("LLM is not initialized");
24776
24797
  }
24777
24798
  await this.initClientsAndTools();
24778
- (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
24779
24799
  const chatOptions = {
24780
24800
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
24781
24801
  model: this.llm(model),
@@ -24788,7 +24808,7 @@ ${user}:`]
24788
24808
  chatOptions.messages = [...this.messages];
24789
24809
  }
24790
24810
  const result = chatMethod(chatOptions);
24791
- (_b8 = result == null ? void 0 : result.response) == null ? void 0 : _b8.then((res) => {
24811
+ (_a16 = result == null ? void 0 : result.response) == null ? void 0 : _a16.then((res) => {
24792
24812
  this.messages.push(...res.messages);
24793
24813
  });
24794
24814
  return result;
@@ -26995,7 +27015,7 @@ ${user}:`]
26995
27015
  this.copyToClipboard(this.options.sessionId.slice(-6));
26996
27016
  }
26997
27017
  copyRemoteURL() {
26998
- this.copyToClipboard((this.options.remoteUrl || DEFAULT_REMOTE_URL) + this.sessionPrefix + this.options.sessionId);
27018
+ this.copyToClipboard(this.options.remoteUrl + this.sessionPrefix + this.options.sessionId);
26999
27019
  }
27000
27020
  // 实现复制到剪贴板功能
27001
27021
  async copyToClipboard(text2) {
@@ -24246,12 +24246,12 @@ const AIProviderFactories = {
24246
24246
  };
24247
24247
  class AgentModelProvider {
24248
24248
  constructor({ llmConfig, mcpServers, llm }) {
24249
- this.mcpServers = [];
24249
+ this.mcpServers = {};
24250
24250
  this.mcpClients = [];
24251
24251
  this.mcpTools = [];
24252
24252
  this.ignoreToolnames = [];
24253
24253
  this.messages = [];
24254
- this.mcpServers = mcpServers || [];
24254
+ this.mcpServers = mcpServers || {};
24255
24255
  if (llm) {
24256
24256
  this.llm = llm;
24257
24257
  } else if (llmConfig) {
@@ -24269,7 +24269,7 @@ class AgentModelProvider {
24269
24269
  throw new Error("Either llmConfig or llm must be provided");
24270
24270
  }
24271
24271
  }
24272
- /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 Null */
24272
+ /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 null */
24273
24273
  async _createOneClient(serverConfig) {
24274
24274
  try {
24275
24275
  let transport;
@@ -24278,7 +24278,9 @@ class AgentModelProvider {
24278
24278
  } else {
24279
24279
  transport = serverConfig;
24280
24280
  }
24281
- return await createMCPClient({ transport });
24281
+ const client = await createMCPClient({ transport });
24282
+ client["__transport__"] = transport;
24283
+ return client;
24282
24284
  } catch (error) {
24283
24285
  if (this.onError) {
24284
24286
  this.onError((error == null ? void 0 : error.message) || `Failed to create MCP client`, error);
@@ -24287,15 +24289,25 @@ class AgentModelProvider {
24287
24289
  return null;
24288
24290
  }
24289
24291
  }
24290
- /** 创建 ai-sdk的 mcpClient, 失败则保存为null */
24292
+ /** 关闭一个 mcpClient */
24293
+ async _closeOneClient(client) {
24294
+ var _a16, _b8, _c, _d, _e;
24295
+ try {
24296
+ await ((_b8 = (_a16 = client["__transport__"]) == null ? void 0 : _a16.terminateSession) == null ? void 0 : _b8.call(_a16));
24297
+ await ((_d = (_c = client["__transport__"]) == null ? void 0 : _c.close) == null ? void 0 : _d.call(_c));
24298
+ await ((_e = client == null ? void 0 : client.close) == null ? void 0 : _e.call(client));
24299
+ } catch (error) {
24300
+ }
24301
+ }
24302
+ /** 创建所有 mcpClients */
24291
24303
  async _createMpcClients() {
24292
24304
  this.mcpClients = await Promise.all(
24293
- this.mcpServers.map(async (server) => {
24305
+ Object.values(this.mcpServers).map(async (server) => {
24294
24306
  return this._createOneClient(server);
24295
24307
  })
24296
24308
  );
24297
24309
  }
24298
- /** 创建所有 mcpClients 的 tools, 失败则保存为null */
24310
+ /** 查询所有 mcpClients 的 tools, 失败则保存为null */
24299
24311
  async _createMpcTools() {
24300
24312
  this.mcpTools = await Promise.all(
24301
24313
  this.mcpClients.map(async (client) => {
@@ -24317,7 +24329,7 @@ class AgentModelProvider {
24317
24329
  await Promise.all(
24318
24330
  this.mcpClients.map(async (client) => {
24319
24331
  try {
24320
- await (client == null ? void 0 : client.close());
24332
+ await this._closeOneClient(client);
24321
24333
  } catch (error) {
24322
24334
  if (this.onError) {
24323
24335
  this.onError((error == null ? void 0 : error.message) || `Failed to close client`, error);
@@ -24327,35 +24339,44 @@ class AgentModelProvider {
24327
24339
  })
24328
24340
  );
24329
24341
  }
24342
+ /** 创建所有的 mcpClients,并更新它们的tools */
24330
24343
  async initClientsAndTools() {
24344
+ var _a16;
24331
24345
  await this._createMpcClients();
24332
24346
  await this._createMpcTools();
24347
+ (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
24333
24348
  }
24349
+ /** 全量更新所有的 mcpServers */
24334
24350
  async updateMcpServers(mcpServers) {
24335
24351
  await this.closeAll();
24336
- this.mcpServers = mcpServers;
24352
+ this.mcpServers = mcpServers || this.mcpServers;
24337
24353
  await this.initClientsAndTools();
24338
24354
  }
24339
- async insertMcpServer(mcpServer) {
24340
- var _a16;
24341
- const find = this.mcpServers.find((item) => "url" in item && "url" in mcpServer && item.url === mcpServer.url);
24342
- if (!find) {
24343
- this.mcpServers = [...this.mcpServers, mcpServer];
24344
- const client = await this._createOneClient(mcpServer);
24345
- this.mcpClients.push(client);
24346
- this.mcpTools.push(await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)));
24347
- return true;
24355
+ /** 插入一个新的mcpServer,如果已经存在则返回false */
24356
+ async insertMcpServer(serverName, mcpServer) {
24357
+ var _a16, _b8;
24358
+ if (this.mcpServers[serverName]) {
24359
+ return false;
24348
24360
  }
24349
- return false;
24361
+ this.mcpServers[serverName] = mcpServer;
24362
+ const client = await this._createOneClient(mcpServer);
24363
+ this.mcpClients.push(client);
24364
+ this.mcpTools.push(await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)));
24365
+ (_b8 = this.onUpdatedTools) == null ? void 0 : _b8.call(this);
24366
+ return true;
24350
24367
  }
24351
- /** 通过引用,删除一个 mcpServers mcpClients mcpTools ignoreToolnames */
24352
- removeMcpServer(mcpServer) {
24353
- const index = this.mcpServers.findIndex((server) => server === mcpServer);
24354
- this.mcpServers.splice(index, 1);
24368
+ /** 通过服务器名称删除mcpServer: mcpServers mcpClients mcpTools ignoreToolnames */
24369
+ async removeMcpServer(serverName) {
24370
+ if (!this.mcpServers[serverName]) {
24371
+ return;
24372
+ }
24373
+ const serverNames = Object.keys(this.mcpServers);
24374
+ const index = serverNames.indexOf(serverName);
24375
+ delete this.mcpServers[serverName];
24355
24376
  const delClient = this.mcpClients[index];
24356
24377
  this.mcpClients.splice(index, 1);
24357
24378
  try {
24358
- delClient == null ? void 0 : delClient.close();
24379
+ await this._closeOneClient(delClient);
24359
24380
  } catch (error) {
24360
24381
  }
24361
24382
  const delTool = this.mcpTools[index];
@@ -24376,12 +24397,11 @@ class AgentModelProvider {
24376
24397
  return toolsResult;
24377
24398
  }
24378
24399
  async _chat(chatMethod, { model, maxSteps = 5, ...options }) {
24379
- var _a16, _b8;
24400
+ var _a16;
24380
24401
  if (!this.llm) {
24381
24402
  throw new Error("LLM is not initialized");
24382
24403
  }
24383
24404
  await this.initClientsAndTools();
24384
- (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
24385
24405
  const chatOptions = {
24386
24406
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
24387
24407
  model: this.llm(model),
@@ -24394,7 +24414,7 @@ class AgentModelProvider {
24394
24414
  chatOptions.messages = [...this.messages];
24395
24415
  }
24396
24416
  const result = chatMethod(chatOptions);
24397
- (_b8 = result == null ? void 0 : result.response) == null ? void 0 : _b8.then((res) => {
24417
+ (_a16 = result == null ? void 0 : result.response) == null ? void 0 : _a16.then((res) => {
24398
24418
  this.messages.push(...res.messages);
24399
24419
  });
24400
24420
  return result;
@@ -26601,7 +26621,7 @@ class FloatingBlock {
26601
26621
  this.copyToClipboard(this.options.sessionId.slice(-6));
26602
26622
  }
26603
26623
  copyRemoteURL() {
26604
- this.copyToClipboard((this.options.remoteUrl || DEFAULT_REMOTE_URL) + this.sessionPrefix + this.options.sessionId);
26624
+ this.copyToClipboard(this.options.remoteUrl + this.sessionPrefix + this.options.sessionId);
26605
26625
  }
26606
26626
  // 实现复制到剪贴板功能
26607
26627
  async copyToClipboard(text2) {