@opentiny/next-sdk 0.0.1-alpha.3 → 0.0.1-alpha.5

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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export * from './client';
2
- export * from './server';
1
+ export * from './next-client';
2
+ export * from './next-server';
3
3
  export * from './mcp-host';
4
4
  export * from './plugins/createMessageChannelTransport';
5
5
  export * from './plugins/createProxy';
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { NextClient as t, createClient as o } from "./client/createClient.mjs";
2
- import { NextServer as n, createServer as p } from "./server/createServer.mjs";
1
+ import { NextClient as t, createClient as o } from "./next-client/index.mjs";
2
+ import { NextServer as n, createServer as p } from "./next-server/index.mjs";
3
3
  import { createMCPHost as c } from "./mcp-host/index.mjs";
4
4
  import { createMessageChannelTransport as f } from "./plugins/createMessageChannelTransport.mjs";
5
5
  import { createClientProxy as v, createServerProxy as C, serverTransport as l } from "./plugins/createProxy.mjs";
@@ -1,34 +1,39 @@
1
1
  var S = Object.defineProperty;
2
- var A = (g, t, o) => t in g ? S(g, t, { enumerable: !0, configurable: !0, writable: !0, value: o }) : g[t] = o;
3
- var u = (g, t, o) => A(g, typeof t != "symbol" ? t + "" : t, o);
2
+ var A = (g, t, s) => t in g ? S(g, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : g[t] = s;
3
+ var p = (g, t, s) => A(g, typeof t != "symbol" ? t + "" : t, s);
4
4
  import O from "openai";
5
5
  import { Role as r } from "../../type.mjs";
6
6
  const M = 3;
7
7
  class D {
8
8
  // 缓存资源防止资源读取频繁触发
9
- constructor({ llmOption: t, mcpClients: o }) {
10
- u(this, "llmOption");
11
- u(this, "mcpClients");
12
- u(this, "llm");
13
- u(this, "mcpClientMap", /* @__PURE__ */ new Map());
14
- u(this, "messages", []);
15
- u(this, "toolClientMap", /* @__PURE__ */ new Map());
16
- u(this, "iteration", M);
9
+ constructor({ llmOption: t, mcpClients: s }) {
10
+ p(this, "llmOption");
11
+ p(this, "mcpClients");
12
+ p(this, "llm");
13
+ p(this, "mcpClientMap", /* @__PURE__ */ new Map());
14
+ p(this, "messages", []);
15
+ p(this, "toolClientMap", /* @__PURE__ */ new Map());
16
+ p(this, "iteration", M);
17
17
  // 最大迭代次数
18
- u(this, "resourcesMap", /* @__PURE__ */ new Map());
19
- this.llmOption = t, this.mcpClients = o, this.messages.push({ role: r.SYSTEM, content: "You are a helpful assistant with access to tools." });
18
+ p(this, "resourcesMap", /* @__PURE__ */ new Map());
19
+ this.llmOption = t, this.mcpClients = s, this.messages.push({ role: r.SYSTEM, content: "You are a helpful assistant with access to tools." });
20
20
  }
21
21
  async generateMcpClient() {
22
22
  for (let t = 0; t < this.mcpClients.length; t++) {
23
- const o = this.mcpClients[t], { tools: s } = await o.listTools();
24
- this.mcpClientMap.set(o, s), s.forEach((a) => {
25
- this.toolClientMap.set(a.name, o);
23
+ const s = this.mcpClients[t], { tools: o } = await s.listTools();
24
+ this.mcpClientMap.set(s, o), o.forEach((n) => {
25
+ this.toolClientMap.set(n.name, s);
26
26
  });
27
- const { resources: n } = await o.listResources();
28
- for (const a of n)
29
- if (!this.resourcesMap.has(a.uri)) {
30
- const c = await o.readResource({ uri: a.uri }), l = { ...a, content: c.contents };
31
- this.resourcesMap.set(a.uri, l), this.messages.push({ role: r.SYSTEM, content: JSON.stringify(l) });
27
+ let a = [];
28
+ try {
29
+ const { resources: n } = await s.listResources();
30
+ a = n;
31
+ } catch {
32
+ }
33
+ for (const n of a)
34
+ if (!this.resourcesMap.has(n.uri)) {
35
+ const c = await s.readResource({ uri: n.uri }), l = { ...n, content: c.contents };
36
+ this.resourcesMap.set(n.uri, l), this.messages.push({ role: r.SYSTEM, content: JSON.stringify(l) });
32
37
  }
33
38
  }
34
39
  }
@@ -42,43 +47,43 @@ class D {
42
47
  validateRequest(t) {
43
48
  if (!t.messages || !Array.isArray(t.messages) || t.messages.length === 0)
44
49
  throw new Error("请求必须包含至少一条消息");
45
- for (const o of t.messages)
46
- if (!o.role || !o.content)
50
+ for (const s of t.messages)
51
+ if (!s.role || !s.content)
47
52
  throw new Error("每条消息必须包含角色和内容");
48
53
  }
49
54
  async getMcpTools() {
50
55
  const t = [];
51
- for (const [, s] of this.mcpClientMap.entries())
52
- t.push(...s);
53
- return t.map((s) => ({
56
+ for (const [, o] of this.mcpClientMap.entries())
57
+ t.push(...o);
58
+ return t.map((o) => ({
54
59
  type: "function",
55
60
  function: {
56
- name: s.name,
57
- description: s.description,
58
- parameters: s.inputSchema
61
+ name: o.name,
62
+ description: o.description,
63
+ parameters: o.inputSchema
59
64
  }
60
65
  }));
61
66
  }
62
67
  async doLLMChart() {
63
- const t = await this.getMcpTools(), o = {
68
+ const t = await this.getMcpTools(), s = {
64
69
  messages: this.messages,
65
70
  model: this.llmOption.model,
66
71
  stream: !0
67
72
  };
68
- return t.length > 0 && (o.tools = t), await this.llm.chat.completions.create(o);
73
+ return t.length > 0 && (s.tools = t), await this.llm.chat.completions.create(s);
69
74
  }
70
75
  /**
71
76
  * 解析 LLM 回复,提取工具调用和最终回复内容
72
77
  * @param response LLM 回复对象
73
78
  * @returns [工具调用数组, 回复内容]
74
79
  */
75
- async parseToolCalls(t, o) {
80
+ async parseToolCalls(t, s) {
76
81
  var c, l, h, e;
77
- const s = /* @__PURE__ */ new Map();
78
- let n = "", a = "";
82
+ const o = /* @__PURE__ */ new Map();
83
+ let a = "", n = "";
79
84
  for await (const C of t) {
80
- const p = C.choices[0].delta, f = p == null ? void 0 : p.tool_calls, T = p == null ? void 0 : p.content;
81
- if (T && (n += T, o.onData({
85
+ const u = C.choices[0].delta, f = u == null ? void 0 : u.tool_calls, T = u == null ? void 0 : u.content;
86
+ if (T && (a += T, s.onData({
82
87
  delta: {
83
88
  role: "assistant",
84
89
  content: T
@@ -86,7 +91,7 @@ class D {
86
91
  })), f && f.length > 0) {
87
92
  const i = f[0];
88
93
  if (i.id) {
89
- a = i.id;
94
+ n = i.id;
90
95
  const m = {
91
96
  id: i.id,
92
97
  type: i.type,
@@ -95,7 +100,7 @@ class D {
95
100
  arguments: ((l = i.function) == null ? void 0 : l.arguments) || ""
96
101
  }
97
102
  };
98
- s.set(a, m), o.onData({
103
+ o.set(n, m), s.onData({
99
104
  delta: {
100
105
  role: r.ASSISTANT,
101
106
  content: `
@@ -106,9 +111,9 @@ class D {
106
111
  }
107
112
  });
108
113
  }
109
- if (s.has(a)) {
110
- const m = s.get(a);
111
- m && (m.function.arguments += ((h = i.function) == null ? void 0 : h.arguments) || "", o.onData({
114
+ if (o.has(n)) {
115
+ const m = o.get(n);
116
+ m && (m.function.arguments += ((h = i.function) == null ? void 0 : h.arguments) || "", s.onData({
112
117
  delta: {
113
118
  role: r.ASSISTANT,
114
119
  content: `${(e = i.function) == null ? void 0 : e.arguments}`
@@ -117,27 +122,27 @@ class D {
117
122
  }
118
123
  }
119
124
  }
120
- return o.onData({
125
+ return s.onData({
121
126
  delta: {
122
127
  role: "assistant",
123
128
  content: `
124
129
 
125
130
  `
126
131
  }
127
- }), [Array.from(s.values()), n];
132
+ }), [Array.from(o.values()), a];
128
133
  }
129
134
  /**
130
135
  * 执行工具调用
131
136
  * @param toolCalls 工具调用请求列表
132
137
  * @returns 工具调用结果和消息
133
138
  */
134
- async callTools(t, o) {
135
- var s, n, a, c;
139
+ async callTools(t, s) {
140
+ var o, a, n, c;
136
141
  try {
137
142
  const l = [], h = [];
138
143
  for (const e of t) {
139
- const C = e.function.name, p = this.toolClientMap.get(C);
140
- if (!p)
144
+ const C = e.function.name, u = this.toolClientMap.get(C);
145
+ if (!u)
141
146
  continue;
142
147
  let f = {};
143
148
  try {
@@ -149,17 +154,17 @@ class D {
149
154
  id: e.id,
150
155
  type: e.type,
151
156
  function: {
152
- name: ((s = e.function) == null ? void 0 : s.name) || "",
153
- arguments: ((n = e.function) == null ? void 0 : n.arguments) || ""
157
+ name: ((o = e.function) == null ? void 0 : o.name) || "",
158
+ arguments: ((a = e.function) == null ? void 0 : a.arguments) || ""
154
159
  }
155
160
  };
156
- o.onData({
161
+ s.onData({
157
162
  delta: {
158
163
  role: r.TOOL,
159
164
  toolCall: T
160
165
  }
161
166
  });
162
- const i = await p.callTool({
167
+ const i = await u.callTool({
163
168
  name: C,
164
169
  arguments: f
165
170
  }), m = this.getToolCallMessage(i), y = {
@@ -171,11 +176,11 @@ class D {
171
176
  type: e.type,
172
177
  callToolContent: m,
173
178
  function: {
174
- name: ((a = e.function) == null ? void 0 : a.name) || "",
179
+ name: ((n = e.function) == null ? void 0 : n.name) || "",
175
180
  arguments: ((c = e.function) == null ? void 0 : c.arguments) || ""
176
181
  }
177
182
  };
178
- o.onData({
183
+ s.onData({
179
184
  delta: {
180
185
  role: r.TOOL,
181
186
  toolCall: d
@@ -196,24 +201,24 @@ class D {
196
201
  * @returns 拼接后的字符串
197
202
  */
198
203
  getToolCallMessage(t) {
199
- var s;
200
- let o = "";
201
- return (s = t.content) != null && s.length && t.content.forEach((n) => {
202
- switch (n.type) {
204
+ var o;
205
+ let s = "";
206
+ return (o = t.content) != null && o.length && t.content.forEach((a) => {
207
+ switch (a.type) {
203
208
  case "text":
204
- o += n.text;
209
+ s += a.text;
205
210
  break;
206
211
  case "image":
207
212
  case "audio":
208
213
  case "resource":
209
- o += n.data;
214
+ s += a.data;
210
215
  break;
211
216
  }
212
- }), o;
217
+ }), s;
213
218
  }
214
- async chatStream(t, o) {
215
- return await this.generateMcpClient(), typeof t == "string" ? this.messages.push({ role: r.USER, content: t }) : this.messages.push(t.messages[t.messages.length - 1]), this.iteration = M, await this.processSteamToolCallsAndResponses(o).catch((s) => {
216
- console.error("Chat failed:", s);
219
+ async chatStream(t, s) {
220
+ return await this.generateMcpClient(), typeof t == "string" ? this.messages.push({ role: r.USER, content: t }) : this.messages.push(t.messages[t.messages.length - 1]), this.iteration = M, await this.processSteamToolCallsAndResponses(s).catch((o) => {
221
+ console.error("Chat failed:", o);
217
222
  }), "ok";
218
223
  }
219
224
  clearMessages() {
@@ -224,28 +229,28 @@ class D {
224
229
  */
225
230
  async processSteamToolCallsAndResponses(t) {
226
231
  try {
227
- const o = [];
232
+ const s = [];
228
233
  for (; this.iteration > 0; ) {
229
- const s = await this.doLLMChart(), [n, a] = await this.parseToolCalls(s, t);
230
- if (n.length) {
234
+ const o = await this.doLLMChart(), [a, n] = await this.parseToolCalls(o, t);
235
+ if (a.length) {
231
236
  const c = {
232
237
  role: r.ASSISTANT,
233
- content: `调用工具:${n.map((e) => e.function.name).join(",")}`,
234
- tool_calls: n.map((e) => ({
238
+ content: `调用工具:${a.map((e) => e.function.name).join(",")}`,
239
+ tool_calls: a.map((e) => ({
235
240
  id: e.id,
236
241
  type: e.type,
237
242
  function: e.function
238
243
  }))
239
244
  };
240
245
  this.messages.push(c);
241
- const { toolResults: l, toolCallMessages: h } = await this.callTools(n, t);
242
- o.push(...l), h.forEach((e) => this.messages.push(e)), this.iteration--;
246
+ const { toolResults: l, toolCallMessages: h } = await this.callTools(a, t);
247
+ s.push(...l), h.forEach((e) => this.messages.push(e)), this.iteration--;
243
248
  } else
244
- this.messages.push({ role: r.ASSISTANT, content: a }), this.iteration = 0;
249
+ this.messages.push({ role: r.ASSISTANT, content: n }), this.iteration = 0;
245
250
  }
246
251
  t.onDone();
247
- } catch (o) {
248
- throw console.error("Chat iteration failed:", o), o;
252
+ } catch (s) {
253
+ throw console.error("Chat iteration failed:", s), s;
249
254
  }
250
255
  }
251
256
  }