@opentiny/next-sdk 0.1.6 → 0.1.7

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.
@@ -1,4 +1,4 @@
1
- import { streamText, stepCountIs, generateText } from 'ai'
1
+ import { streamText, stepCountIs, generateText, StreamTextResult } from 'ai'
2
2
  import { experimental_createMCPClient as createMCPClient, experimental_MCPClientConfig as MCPClientConfig } from 'ai'
3
3
  import type { ToolSet } from 'ai'
4
4
  import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
@@ -39,6 +39,8 @@ export class AgentModelProvider {
39
39
  /** 内部报错时,抛出错误事件 */
40
40
  onError: ((msg: string, err?: any) => void) | undefined
41
41
 
42
+ messages: any[] = []
43
+
42
44
  constructor({ llmConfig, mcpServers, llm }: IAgentModelProviderOption) {
43
45
  // 1、保存 mcpServer
44
46
  this.mcpServers = mcpServers || []
@@ -75,9 +77,9 @@ export class AgentModelProvider {
75
77
  }
76
78
 
77
79
  return await createMCPClient({ transport: transport as MCPClientConfig['transport'] })
78
- } catch (error) {
80
+ } catch (error: unknown) {
79
81
  if (this.onError) {
80
- this.onError(`Failed to create MCP client`, error)
82
+ this.onError((error as Error)?.message || `Failed to create MCP client`, error)
81
83
  }
82
84
  console.error(`Failed to create MCP client`, serverConfig, error)
83
85
  return null
@@ -98,9 +100,9 @@ export class AgentModelProvider {
98
100
  this.mcpClients.map(async (client) => {
99
101
  try {
100
102
  return client ? await client?.tools?.() : null
101
- } catch (error) {
103
+ } catch (error: unknown) {
102
104
  if (this.onError) {
103
- this.onError(`Failed to query tools`, error)
105
+ this.onError((error as Error)?.message || `Failed to query tools`, error)
104
106
  }
105
107
  console.error(`Failed to query tools`, error)
106
108
  return null
@@ -113,8 +115,13 @@ export class AgentModelProvider {
113
115
  await Promise.all(
114
116
  this.mcpClients.map(async (client) => {
115
117
  try {
116
- client.close()
117
- } catch (error) {}
118
+ await client.close()
119
+ } catch (error: unknown) {
120
+ if (this.onError) {
121
+ this.onError((error as Error)?.message || `Failed to close client`, error)
122
+ }
123
+ console.error(`Failed to close client`, error)
124
+ }
118
125
  })
119
126
  )
120
127
  }
@@ -181,7 +188,7 @@ export class AgentModelProvider {
181
188
 
182
189
  async _chat(
183
190
  chatMethod: ChatMethodFn,
184
- { model, maxSteps = 5, ...options }: Parameters<typeof generateText>[0] & { maxSteps?: number }
191
+ { model, maxSteps = 5, ...options }: Parameters<typeof generateText>[0] & { maxSteps?: number; message?: string }
185
192
  ): Promise<any> {
186
193
  if (!this.llm) {
187
194
  throw new Error('LLM is not initialized')
@@ -192,20 +199,33 @@ export class AgentModelProvider {
192
199
  this.onUpdatedTools?.()
193
200
  }
194
201
 
195
- return chatMethod({
202
+ const chatOptions = {
196
203
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
197
204
  model: this.llm(model),
198
205
  stopWhen: stepCountIs(maxSteps),
199
206
  ...options,
200
207
  tools: this.tempMergeTools(options.tools) as ToolSet
208
+ }
209
+
210
+ if (options.message && !options.messages) {
211
+ this.messages.push({ role: 'user', content: options.message })
212
+ chatOptions.messages = [...this.messages]
213
+ }
214
+
215
+ const result = chatMethod(chatOptions)
216
+
217
+ ;(result as StreamTextResult<ToolSet, unknown>)?.response?.then((res: any) => {
218
+ this.messages.push(...res.messages)
201
219
  })
220
+
221
+ return result
202
222
  }
203
223
 
204
- async chat(options: Parameters<typeof generateText>[0] & { maxSteps?: number }): Promise<any> {
224
+ async chat(options: Parameters<typeof generateText>[0] & { maxSteps?: number; message?: string }): Promise<any> {
205
225
  return this._chat(generateText, options)
206
226
  }
207
227
 
208
- async chatStream(options: Parameters<typeof streamText>[0] & { maxSteps?: number }): Promise<any> {
228
+ async chatStream(options: Parameters<typeof streamText>[0] & { maxSteps?: number; message?: string }): Promise<any> {
209
229
  return this._chat(streamText, options as any)
210
230
  }
211
231
  }
@@ -30,6 +30,7 @@ export declare class AgentModelProvider {
30
30
  onUpdatedTools: (() => void) | undefined;
31
31
  /** 内部报错时,抛出错误事件 */
32
32
  onError: ((msg: string, err?: any) => void) | undefined;
33
+ messages: any[];
33
34
  constructor({ llmConfig, mcpServers, llm }: IAgentModelProviderOption);
34
35
  /** 创建一个 ai-sdk的 mcpClient, 创建失败则返回 Null */
35
36
  private _createOneClient;
@@ -48,12 +49,15 @@ export declare class AgentModelProvider {
48
49
  tempMergeTools(extraTool?: {}): Record<string, any>;
49
50
  _chat(chatMethod: ChatMethodFn, { model, maxSteps, ...options }: Parameters<typeof generateText>[0] & {
50
51
  maxSteps?: number;
52
+ message?: string;
51
53
  }): Promise<any>;
52
54
  chat(options: Parameters<typeof generateText>[0] & {
53
55
  maxSteps?: number;
56
+ message?: string;
54
57
  }): Promise<any>;
55
58
  chatStream(options: Parameters<typeof streamText>[0] & {
56
59
  maxSteps?: number;
60
+ message?: string;
57
61
  }): Promise<any>;
58
62
  }
59
63
  export {};
@@ -44,6 +44,7 @@ export class AgentModelProvider {
44
44
  this.ignoreToolnames = [];
45
45
  /** 在 chat 之前,自动更新 所有的tools */
46
46
  this.autoUpdateTools = true;
47
+ this.messages = [];
47
48
  // 1、保存 mcpServer
48
49
  this.mcpServers = mcpServers || [];
49
50
  // 2、保存 llm
@@ -83,7 +84,7 @@ export class AgentModelProvider {
83
84
  }
84
85
  catch (error) {
85
86
  if (this.onError) {
86
- this.onError(`Failed to create MCP client`, error);
87
+ this.onError((error === null || error === void 0 ? void 0 : error.message) || `Failed to create MCP client`, error);
87
88
  }
88
89
  console.error(`Failed to create MCP client`, serverConfig, error);
89
90
  return null;
@@ -109,7 +110,7 @@ export class AgentModelProvider {
109
110
  }
110
111
  catch (error) {
111
112
  if (this.onError) {
112
- this.onError(`Failed to query tools`, error);
113
+ this.onError((error === null || error === void 0 ? void 0 : error.message) || `Failed to query tools`, error);
113
114
  }
114
115
  console.error(`Failed to query tools`, error);
115
116
  return null;
@@ -122,9 +123,14 @@ export class AgentModelProvider {
122
123
  return __awaiter(this, void 0, void 0, function* () {
123
124
  yield Promise.all(this.mcpClients.map((client) => __awaiter(this, void 0, void 0, function* () {
124
125
  try {
125
- client.close();
126
+ yield client.close();
127
+ }
128
+ catch (error) {
129
+ if (this.onError) {
130
+ this.onError((error === null || error === void 0 ? void 0 : error.message) || `Failed to close client`, error);
131
+ }
132
+ console.error(`Failed to close client`, error);
126
133
  }
127
- catch (error) { }
128
134
  })));
129
135
  });
130
136
  }
@@ -188,7 +194,7 @@ export class AgentModelProvider {
188
194
  }
189
195
  _chat(chatMethod, _a) {
190
196
  return __awaiter(this, void 0, void 0, function* () {
191
- var _b;
197
+ var _b, _c;
192
198
  var { model, maxSteps = 5 } = _a, options = __rest(_a, ["model", "maxSteps"]);
193
199
  if (!this.llm) {
194
200
  throw new Error('LLM is not initialized');
@@ -197,9 +203,18 @@ export class AgentModelProvider {
197
203
  yield this._createMpcTools();
198
204
  (_b = this.onUpdatedTools) === null || _b === void 0 ? void 0 : _b.call(this);
199
205
  }
200
- return chatMethod(Object.assign(Object.assign({
206
+ const chatOptions = Object.assign(Object.assign({
201
207
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
202
- model: this.llm(model), stopWhen: stepCountIs(maxSteps) }, options), { tools: this.tempMergeTools(options.tools) }));
208
+ model: this.llm(model), stopWhen: stepCountIs(maxSteps) }, options), { tools: this.tempMergeTools(options.tools) });
209
+ if (options.message && !options.messages) {
210
+ this.messages.push({ role: 'user', content: options.message });
211
+ chatOptions.messages = [...this.messages];
212
+ }
213
+ const result = chatMethod(chatOptions);
214
+ (_c = result === null || result === void 0 ? void 0 : result.response) === null || _c === void 0 ? void 0 : _c.then((res) => {
215
+ this.messages.push(...res.messages);
216
+ });
217
+ return result;
203
218
  });
204
219
  }
205
220
  chat(options) {
@@ -42811,6 +42811,7 @@ class AgentModelProvider {
42811
42811
  this.mcpTools = [];
42812
42812
  this.ignoreToolnames = [];
42813
42813
  this.autoUpdateTools = true;
42814
+ this.messages = [];
42814
42815
  this.mcpServers = mcpServers || [];
42815
42816
  if (llm) {
42816
42817
  this.llm = llm;
@@ -42841,7 +42842,7 @@ class AgentModelProvider {
42841
42842
  return await createMCPClient({ transport });
42842
42843
  } catch (error2) {
42843
42844
  if (this.onError) {
42844
- this.onError(`Failed to create MCP client`, error2);
42845
+ this.onError((error2 == null ? void 0 : error2.message) || `Failed to create MCP client`, error2);
42845
42846
  }
42846
42847
  console.error(`Failed to create MCP client`, serverConfig, error2);
42847
42848
  return null;
@@ -42864,7 +42865,7 @@ class AgentModelProvider {
42864
42865
  return client ? await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)) : null;
42865
42866
  } catch (error2) {
42866
42867
  if (this.onError) {
42867
- this.onError(`Failed to query tools`, error2);
42868
+ this.onError((error2 == null ? void 0 : error2.message) || `Failed to query tools`, error2);
42868
42869
  }
42869
42870
  console.error(`Failed to query tools`, error2);
42870
42871
  return null;
@@ -42877,8 +42878,12 @@ class AgentModelProvider {
42877
42878
  await Promise.all(
42878
42879
  this.mcpClients.map(async (client) => {
42879
42880
  try {
42880
- client.close();
42881
+ await client.close();
42881
42882
  } catch (error2) {
42883
+ if (this.onError) {
42884
+ this.onError((error2 == null ? void 0 : error2.message) || `Failed to close client`, error2);
42885
+ }
42886
+ console.error(`Failed to close client`, error2);
42882
42887
  }
42883
42888
  })
42884
42889
  );
@@ -42932,7 +42937,7 @@ class AgentModelProvider {
42932
42937
  return toolsResult;
42933
42938
  }
42934
42939
  async _chat(chatMethod, { model, maxSteps = 5, ...options }) {
42935
- var _a16;
42940
+ var _a16, _b8;
42936
42941
  if (!this.llm) {
42937
42942
  throw new Error("LLM is not initialized");
42938
42943
  }
@@ -42940,13 +42945,22 @@ class AgentModelProvider {
42940
42945
  await this._createMpcTools();
42941
42946
  (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
42942
42947
  }
42943
- return chatMethod({
42948
+ const chatOptions = {
42944
42949
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
42945
42950
  model: this.llm(model),
42946
42951
  stopWhen: stepCountIs(maxSteps),
42947
42952
  ...options,
42948
42953
  tools: this.tempMergeTools(options.tools)
42954
+ };
42955
+ if (options.message && !options.messages) {
42956
+ this.messages.push({ role: "user", content: options.message });
42957
+ chatOptions.messages = [...this.messages];
42958
+ }
42959
+ const result = chatMethod(chatOptions);
42960
+ (_b8 = result == null ? void 0 : result.response) == null ? void 0 : _b8.then((res) => {
42961
+ this.messages.push(...res.messages);
42949
42962
  });
42963
+ return result;
42950
42964
  }
42951
42965
  async chat(options) {
42952
42966
  return this._chat(generateText, options);
package/dist/index.es.js CHANGED
@@ -31735,7 +31735,7 @@ const E3 = {
31735
31735
  };
31736
31736
  class oD {
31737
31737
  constructor({ llmConfig: e, mcpServers: r, llm: a }) {
31738
- if (this.mcpServers = [], this.mcpClients = [], this.mcpTools = [], this.ignoreToolnames = [], this.autoUpdateTools = !0, this.mcpServers = r || [], a)
31738
+ if (this.mcpServers = [], this.mcpClients = [], this.mcpTools = [], this.ignoreToolnames = [], this.autoUpdateTools = !0, this.messages = [], this.mcpServers = r || [], a)
31739
31739
  this.llm = a;
31740
31740
  else if (e) {
31741
31741
  let n;
@@ -31752,7 +31752,7 @@ class oD {
31752
31752
  let r;
31753
31753
  return "type" in e && e.type.toLocaleLowerCase() === "streamablehttp" ? r = new $a(new URL(e.url)) : r = e, await jM({ transport: r });
31754
31754
  } catch (r) {
31755
- return this.onError && this.onError("Failed to create MCP client", r), console.error("Failed to create MCP client", e, r), null;
31755
+ return this.onError && this.onError((r == null ? void 0 : r.message) || "Failed to create MCP client", r), console.error("Failed to create MCP client", e, r), null;
31756
31756
  }
31757
31757
  }
31758
31758
  /** 创建 ai-sdk的 mcpClient, 失败则保存为null */
@@ -31769,7 +31769,7 @@ class oD {
31769
31769
  try {
31770
31770
  return e ? await ((r = e == null ? void 0 : e.tools) == null ? void 0 : r.call(e)) : null;
31771
31771
  } catch (a) {
31772
- return this.onError && this.onError("Failed to query tools", a), console.error("Failed to query tools", a), null;
31772
+ return this.onError && this.onError((a == null ? void 0 : a.message) || "Failed to query tools", a), console.error("Failed to query tools", a), null;
31773
31773
  }
31774
31774
  })
31775
31775
  );
@@ -31779,8 +31779,9 @@ class oD {
31779
31779
  await Promise.all(
31780
31780
  this.mcpClients.map(async (e) => {
31781
31781
  try {
31782
- e.close();
31783
- } catch {
31782
+ await e.close();
31783
+ } catch (r) {
31784
+ this.onError && this.onError((r == null ? void 0 : r.message) || "Failed to close client", r), console.error("Failed to close client", r);
31784
31785
  }
31785
31786
  })
31786
31787
  );
@@ -31823,16 +31824,22 @@ class oD {
31823
31824
  }), r;
31824
31825
  }
31825
31826
  async _chat(e, { model: r, maxSteps: a = 5, ...n }) {
31826
- var s;
31827
+ var i, l;
31827
31828
  if (!this.llm)
31828
31829
  throw new Error("LLM is not initialized");
31829
- return this.autoUpdateTools && (await this._createMpcTools(), (s = this.onUpdatedTools) == null || s.call(this)), e({
31830
+ this.autoUpdateTools && (await this._createMpcTools(), (i = this.onUpdatedTools) == null || i.call(this));
31831
+ const s = {
31830
31832
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
31831
31833
  model: this.llm(r),
31832
31834
  stopWhen: Gd(a),
31833
31835
  ...n,
31834
31836
  tools: this.tempMergeTools(n.tools)
31835
- });
31837
+ };
31838
+ n.message && !n.messages && (this.messages.push({ role: "user", content: n.message }), s.messages = [...this.messages]);
31839
+ const o = e(s);
31840
+ return (l = o == null ? void 0 : o.response) == null || l.then((c) => {
31841
+ this.messages.push(...c.messages);
31842
+ }), o;
31836
31843
  }
31837
31844
  async chat(e) {
31838
31845
  return this._chat(U4, e);
@@ -43205,6 +43205,7 @@ ${user}:`]
43205
43205
  this.mcpTools = [];
43206
43206
  this.ignoreToolnames = [];
43207
43207
  this.autoUpdateTools = true;
43208
+ this.messages = [];
43208
43209
  this.mcpServers = mcpServers || [];
43209
43210
  if (llm) {
43210
43211
  this.llm = llm;
@@ -43235,7 +43236,7 @@ ${user}:`]
43235
43236
  return await createMCPClient({ transport });
43236
43237
  } catch (error2) {
43237
43238
  if (this.onError) {
43238
- this.onError(`Failed to create MCP client`, error2);
43239
+ this.onError((error2 == null ? void 0 : error2.message) || `Failed to create MCP client`, error2);
43239
43240
  }
43240
43241
  console.error(`Failed to create MCP client`, serverConfig, error2);
43241
43242
  return null;
@@ -43258,7 +43259,7 @@ ${user}:`]
43258
43259
  return client ? await ((_a16 = client == null ? void 0 : client.tools) == null ? void 0 : _a16.call(client)) : null;
43259
43260
  } catch (error2) {
43260
43261
  if (this.onError) {
43261
- this.onError(`Failed to query tools`, error2);
43262
+ this.onError((error2 == null ? void 0 : error2.message) || `Failed to query tools`, error2);
43262
43263
  }
43263
43264
  console.error(`Failed to query tools`, error2);
43264
43265
  return null;
@@ -43271,8 +43272,12 @@ ${user}:`]
43271
43272
  await Promise.all(
43272
43273
  this.mcpClients.map(async (client) => {
43273
43274
  try {
43274
- client.close();
43275
+ await client.close();
43275
43276
  } catch (error2) {
43277
+ if (this.onError) {
43278
+ this.onError((error2 == null ? void 0 : error2.message) || `Failed to close client`, error2);
43279
+ }
43280
+ console.error(`Failed to close client`, error2);
43276
43281
  }
43277
43282
  })
43278
43283
  );
@@ -43326,7 +43331,7 @@ ${user}:`]
43326
43331
  return toolsResult;
43327
43332
  }
43328
43333
  async _chat(chatMethod, { model, maxSteps = 5, ...options }) {
43329
- var _a16;
43334
+ var _a16, _b8;
43330
43335
  if (!this.llm) {
43331
43336
  throw new Error("LLM is not initialized");
43332
43337
  }
@@ -43334,13 +43339,22 @@ ${user}:`]
43334
43339
  await this._createMpcTools();
43335
43340
  (_a16 = this.onUpdatedTools) == null ? void 0 : _a16.call(this);
43336
43341
  }
43337
- return chatMethod({
43342
+ const chatOptions = {
43338
43343
  // @ts-ignore ProviderV2 是所有llm的父类, 在每一个具体的llm 类都有一个选择model的函数用法
43339
43344
  model: this.llm(model),
43340
43345
  stopWhen: stepCountIs(maxSteps),
43341
43346
  ...options,
43342
43347
  tools: this.tempMergeTools(options.tools)
43348
+ };
43349
+ if (options.message && !options.messages) {
43350
+ this.messages.push({ role: "user", content: options.message });
43351
+ chatOptions.messages = [...this.messages];
43352
+ }
43353
+ const result = chatMethod(chatOptions);
43354
+ (_b8 = result == null ? void 0 : result.response) == null ? void 0 : _b8.then((res) => {
43355
+ this.messages.push(...res.messages);
43343
43356
  });
43357
+ return result;
43344
43358
  }
43345
43359
  async chat(options) {
43346
43360
  return this._chat(generateText, options);