@opentiny/next-sdk 0.1.7 → 0.1.8

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.
@@ -31,14 +31,12 @@ export class AgentModelProvider {
31
31
  /** 需要实时过滤掉的tools name*/
32
32
  ignoreToolnames: string[] = []
33
33
 
34
- /** 在 chat 之前,自动更新 所有的tools */
35
- autoUpdateTools = true
36
-
37
34
  /** chat 时,自动更新 所有的tools 后的事件 */
38
35
  onUpdatedTools: (() => void) | undefined
39
36
  /** 内部报错时,抛出错误事件 */
40
37
  onError: ((msg: string, err?: any) => void) | undefined
41
38
 
39
+ /** 缓存 ai-sdk response 中的 多轮会话 */
42
40
  messages: any[] = []
43
41
 
44
42
  constructor({ llmConfig, mcpServers, llm }: IAgentModelProviderOption) {
@@ -115,7 +113,7 @@ export class AgentModelProvider {
115
113
  await Promise.all(
116
114
  this.mcpClients.map(async (client) => {
117
115
  try {
118
- await client.close()
116
+ await client?.close()
119
117
  } catch (error: unknown) {
120
118
  if (this.onError) {
121
119
  this.onError((error as Error)?.message || `Failed to close client`, error)
@@ -176,7 +174,7 @@ export class AgentModelProvider {
176
174
  }
177
175
 
178
176
  /** 创建临时允许调用的tools集合 */
179
- tempMergeTools(extraTool = {}) {
177
+ private _tempMergeTools(extraTool = {}) {
180
178
  const toolsResult = this.mcpTools.reduce((acc, curr) => ({ ...acc, ...curr }), {})
181
179
  Object.assign(toolsResult, extraTool)
182
180
 
@@ -186,7 +184,7 @@ export class AgentModelProvider {
186
184
  return toolsResult
187
185
  }
188
186
 
189
- async _chat(
187
+ private async _chat(
190
188
  chatMethod: ChatMethodFn,
191
189
  { model, maxSteps = 5, ...options }: Parameters<typeof generateText>[0] & { maxSteps?: number; message?: string }
192
190
  ): Promise<any> {
@@ -194,17 +192,16 @@ export class AgentModelProvider {
194
192
  throw new Error('LLM is not initialized')
195
193
  }
196
194
 
197
- if (this.autoUpdateTools) {
198
- await this._createMpcTools()
199
- this.onUpdatedTools?.()
200
- }
195
+ await this.initClientsAndTools()
196
+
197
+ this.onUpdatedTools?.()
201
198
 
202
199
  const chatOptions = {
203
200
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
204
201
  model: this.llm(model),
205
202
  stopWhen: stepCountIs(maxSteps),
206
203
  ...options,
207
- tools: this.tempMergeTools(options.tools) as ToolSet
204
+ tools: this._tempMergeTools(options.tools) as ToolSet
208
205
  }
209
206
 
210
207
  if (options.message && !options.messages) {
@@ -214,6 +211,7 @@ export class AgentModelProvider {
214
211
 
215
212
  const result = chatMethod(chatOptions)
216
213
 
214
+ // 缓存 ai-sdk的多轮对话的消息
217
215
  ;(result as StreamTextResult<ToolSet, unknown>)?.response?.then((res: any) => {
218
216
  this.messages.push(...res.messages)
219
217
  })
@@ -8,7 +8,6 @@ export declare const AIProviderFactories: {
8
8
  openai: typeof createOpenAI;
9
9
  deepseek: typeof createDeepSeek;
10
10
  };
11
- type ChatMethodFn = typeof streamText | typeof generateText;
12
11
  /** 一个通用的ai-sdk的agent封装
13
12
  * @summary 内部自动管理了 llm, mcpServer, ai-sdk的clients 和 tools
14
13
  * @returns 暴露了 chat, chatStream方法
@@ -24,12 +23,11 @@ export declare class AgentModelProvider {
24
23
  mcpTools: Array<Record<string, any>>;
25
24
  /** 需要实时过滤掉的tools name*/
26
25
  ignoreToolnames: string[];
27
- /** 在 chat 之前,自动更新 所有的tools */
28
- autoUpdateTools: boolean;
29
26
  /** chat 时,自动更新 所有的tools 后的事件 */
30
27
  onUpdatedTools: (() => void) | undefined;
31
28
  /** 内部报错时,抛出错误事件 */
32
29
  onError: ((msg: string, err?: any) => void) | undefined;
30
+ /** 缓存 ai-sdk response 中的 多轮会话 */
33
31
  messages: any[];
34
32
  constructor({ llmConfig, mcpServers, llm }: IAgentModelProviderOption);
35
33
  /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 Null */
@@ -46,11 +44,8 @@ export declare class AgentModelProvider {
46
44
  /** 通过引用,删除一个 mcpServers mcpClients mcpTools ignoreToolnames */
47
45
  removeMcpServer(mcpServer: McpServerConfig): void;
48
46
  /** 创建临时允许调用的tools集合 */
49
- tempMergeTools(extraTool?: {}): Record<string, any>;
50
- _chat(chatMethod: ChatMethodFn, { model, maxSteps, ...options }: Parameters<typeof generateText>[0] & {
51
- maxSteps?: number;
52
- message?: string;
53
- }): Promise<any>;
47
+ private _tempMergeTools;
48
+ private _chat;
54
49
  chat(options: Parameters<typeof generateText>[0] & {
55
50
  maxSteps?: number;
56
51
  message?: string;
@@ -60,4 +55,3 @@ export declare class AgentModelProvider {
60
55
  message?: string;
61
56
  }): Promise<any>;
62
57
  }
63
- export {};
@@ -42,8 +42,7 @@ export class AgentModelProvider {
42
42
  this.mcpTools = [];
43
43
  /** 需要实时过滤掉的tools name*/
44
44
  this.ignoreToolnames = [];
45
- /** chat 之前,自动更新 所有的tools */
46
- this.autoUpdateTools = true;
45
+ /** 缓存 ai-sdk response 中的 多轮会话 */
47
46
  this.messages = [];
48
47
  // 1、保存 mcpServer
49
48
  this.mcpServers = mcpServers || [];
@@ -123,7 +122,7 @@ export class AgentModelProvider {
123
122
  return __awaiter(this, void 0, void 0, function* () {
124
123
  yield Promise.all(this.mcpClients.map((client) => __awaiter(this, void 0, void 0, function* () {
125
124
  try {
126
- yield client.close();
125
+ yield (client === null || client === void 0 ? void 0 : client.close());
127
126
  }
128
127
  catch (error) {
129
128
  if (this.onError) {
@@ -184,7 +183,7 @@ export class AgentModelProvider {
184
183
  }
185
184
  }
186
185
  /** 创建临时允许调用的tools集合 */
187
- tempMergeTools(extraTool = {}) {
186
+ _tempMergeTools(extraTool = {}) {
188
187
  const toolsResult = this.mcpTools.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), curr)), {});
189
188
  Object.assign(toolsResult, extraTool);
190
189
  this.ignoreToolnames.forEach((name) => {
@@ -199,13 +198,11 @@ export class AgentModelProvider {
199
198
  if (!this.llm) {
200
199
  throw new Error('LLM is not initialized');
201
200
  }
202
- if (this.autoUpdateTools) {
203
- yield this._createMpcTools();
204
- (_b = this.onUpdatedTools) === null || _b === void 0 ? void 0 : _b.call(this);
205
- }
201
+ yield this.initClientsAndTools();
202
+ (_b = this.onUpdatedTools) === null || _b === void 0 ? void 0 : _b.call(this);
206
203
  const chatOptions = Object.assign(Object.assign({
207
204
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
208
- model: this.llm(model), stopWhen: stepCountIs(maxSteps) }, options), { tools: this.tempMergeTools(options.tools) });
205
+ model: this.llm(model), stopWhen: stepCountIs(maxSteps) }, options), { tools: this._tempMergeTools(options.tools) });
209
206
  if (options.message && !options.messages) {
210
207
  this.messages.push({ role: 'user', content: options.message });
211
208
  chatOptions.messages = [...this.messages];
@@ -24885,6 +24885,8 @@ class QrCode {
24885
24885
  img.src = await this.toDataURL();
24886
24886
  }
24887
24887
  }
24888
+ const DEFAULT_REMOTE_URL = "https://agent.opentiny.design/tiny-robot";
24889
+ const DEFAULT_QR_CODE_URL = "https://ai.opentiny.design/next-remoter";
24888
24890
  const getDefaultMenuItems = (options) => {
24889
24891
  return [
24890
24892
  {
@@ -24933,8 +24935,8 @@ const getDefaultMenuItems = (options) => {
24933
24935
  {
24934
24936
  action: "remote-url",
24935
24937
  show: true,
24936
- text: `${options.qrCodeUrl}`,
24937
- tip: options.qrCodeUrl,
24938
+ text: `${options.remoteUrl}`,
24939
+ tip: options.remoteUrl,
24938
24940
  showCopyIcon: true,
24939
24941
  icon: `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
24940
24942
  <path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" fill="none"/>
@@ -24950,8 +24952,9 @@ class FloatingBlock {
24950
24952
  throw new Error("sessionId is required");
24951
24953
  }
24952
24954
  this.options = {
24953
- qrCodeUrl: options.qrCodeUrl || "https://ai.opentiny.design/next-remoter",
24954
- ...options
24955
+ ...options,
24956
+ qrCodeUrl: options.qrCodeUrl || DEFAULT_QR_CODE_URL,
24957
+ remoteUrl: options.remoteUrl || DEFAULT_REMOTE_URL
24955
24958
  };
24956
24959
  this.menuItems = this.mergeMenuItems(options.menuItems);
24957
24960
  this.init();
@@ -24995,7 +24998,7 @@ class FloatingBlock {
24995
24998
  this.floatingBlock.className = "tiny-remoter-floating-block";
24996
24999
  this.floatingBlock.innerHTML = `
24997
25000
  <div class="tiny-remoter-floating-block__icon">
24998
- <img style="display: block; width: 56px;" src="https://ai.opentiny.design/next-remoter/svgs/logo-next-no-bg-left.svg" alt="icon" />
25001
+ <img style="display: block; width: 56px;" src="${DEFAULT_QR_CODE_URL}/svgs/logo-next-no-bg-left.svg" alt="icon" />
24999
25002
  </div>
25000
25003
  `;
25001
25004
  document.body.appendChild(this.floatingBlock);
@@ -25095,7 +25098,7 @@ class FloatingBlock {
25095
25098
  this.copyToClipboard(this.options.sessionId.slice(-6));
25096
25099
  }
25097
25100
  copyRemoteURL() {
25098
- this.copyToClipboard((this.options.qrCodeUrl || "") + this.sessionPrefix + this.options.sessionId);
25101
+ this.copyToClipboard((this.options.remoteUrl || DEFAULT_REMOTE_URL) + this.sessionPrefix + this.options.sessionId);
25099
25102
  }
25100
25103
  // 实现复制到剪贴板功能
25101
25104
  async copyToClipboard(text2) {
@@ -42810,7 +42813,6 @@ class AgentModelProvider {
42810
42813
  this.mcpClients = [];
42811
42814
  this.mcpTools = [];
42812
42815
  this.ignoreToolnames = [];
42813
- this.autoUpdateTools = true;
42814
42816
  this.messages = [];
42815
42817
  this.mcpServers = mcpServers || [];
42816
42818
  if (llm) {
@@ -42878,7 +42880,7 @@ class AgentModelProvider {
42878
42880
  await Promise.all(
42879
42881
  this.mcpClients.map(async (client) => {
42880
42882
  try {
42881
- await client.close();
42883
+ await (client == null ? void 0 : client.close());
42882
42884
  } catch (error2) {
42883
42885
  if (this.onError) {
42884
42886
  this.onError((error2 == null ? void 0 : error2.message) || `Failed to close client`, error2);
@@ -42928,7 +42930,7 @@ class AgentModelProvider {
42928
42930
  }
42929
42931
  }
42930
42932
  /** 创建临时允许调用的tools集合 */
42931
- tempMergeTools(extraTool = {}) {
42933
+ _tempMergeTools(extraTool = {}) {
42932
42934
  const toolsResult = this.mcpTools.reduce((acc, curr) => ({ ...acc, ...curr }), {});
42933
42935
  Object.assign(toolsResult, extraTool);
42934
42936
  this.ignoreToolnames.forEach((name16) => {
@@ -42941,16 +42943,14 @@ class AgentModelProvider {
42941
42943
  if (!this.llm) {
42942
42944
  throw new Error("LLM is not initialized");
42943
42945
  }
42944
- if (this.autoUpdateTools) {
42945
- await this._createMpcTools();
42946
- (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
42947
- }
42946
+ await this.initClientsAndTools();
42947
+ (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
42948
42948
  const chatOptions = {
42949
42949
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
42950
42950
  model: this.llm(model),
42951
42951
  stopWhen: stepCountIs(maxSteps),
42952
42952
  ...options,
42953
- tools: this.tempMergeTools(options.tools)
42953
+ tools: this._tempMergeTools(options.tools)
42954
42954
  };
42955
42955
  if (options.message && !options.messages) {
42956
42956
  this.messages.push({ role: "user", content: options.message });