@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 +2 -2
- package/dist/index.mjs +2 -2
- package/dist/mcp-host/core/index.mjs +77 -72
- package/dist/next-client/index.d.ts +1708 -0
- package/dist/next-server/index.d.ts +1098 -0
- package/dist/{server/createServer.mjs → next-server/index.mjs} +7 -4
- package/dist/plugins/connectMcpServer.d.ts +1 -1
- package/dist/plugins/createInMemoryTransport.d.ts +2 -2
- package/dist/plugins/createInMemoryTransport.mjs +1 -1
- package/dist/plugins/createMessageChannelTransport.d.ts +2 -2
- package/dist/plugins/createProxy.d.ts +2 -2
- package/package.json +3 -1
- package/dist/client/index.d.ts +0 -1
- package/dist/server/index.d.ts +0 -1
- /package/dist/{client/createClient.mjs → next-client/index.mjs} +0 -0
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { NextClient as t, createClient as o } from "./client/
|
|
2
|
-
import { NextServer as n, createServer as p } from "./server/
|
|
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,
|
|
3
|
-
var
|
|
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:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
19
|
-
this.llmOption = t, this.mcpClients =
|
|
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
|
|
24
|
-
this.mcpClientMap.set(
|
|
25
|
-
this.toolClientMap.set(
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
46
|
-
if (!
|
|
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 [,
|
|
52
|
-
t.push(...
|
|
53
|
-
return t.map((
|
|
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:
|
|
57
|
-
description:
|
|
58
|
-
parameters:
|
|
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(),
|
|
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 && (
|
|
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,
|
|
80
|
+
async parseToolCalls(t, s) {
|
|
76
81
|
var c, l, h, e;
|
|
77
|
-
const
|
|
78
|
-
let
|
|
82
|
+
const o = /* @__PURE__ */ new Map();
|
|
83
|
+
let a = "", n = "";
|
|
79
84
|
for await (const C of t) {
|
|
80
|
-
const
|
|
81
|
-
if (T && (
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
110
|
-
const m =
|
|
111
|
-
m && (m.function.arguments += ((h = i.function) == null ? void 0 : h.arguments) || "",
|
|
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
|
|
125
|
+
return s.onData({
|
|
121
126
|
delta: {
|
|
122
127
|
role: "assistant",
|
|
123
128
|
content: `
|
|
124
129
|
|
|
125
130
|
`
|
|
126
131
|
}
|
|
127
|
-
}), [Array.from(
|
|
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,
|
|
135
|
-
var
|
|
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,
|
|
140
|
-
if (!
|
|
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: ((
|
|
153
|
-
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
|
-
|
|
161
|
+
s.onData({
|
|
157
162
|
delta: {
|
|
158
163
|
role: r.TOOL,
|
|
159
164
|
toolCall: T
|
|
160
165
|
}
|
|
161
166
|
});
|
|
162
|
-
const i = await
|
|
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: ((
|
|
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
|
-
|
|
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
|
|
200
|
-
let
|
|
201
|
-
return (
|
|
202
|
-
switch (
|
|
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
|
-
|
|
209
|
+
s += a.text;
|
|
205
210
|
break;
|
|
206
211
|
case "image":
|
|
207
212
|
case "audio":
|
|
208
213
|
case "resource":
|
|
209
|
-
|
|
214
|
+
s += a.data;
|
|
210
215
|
break;
|
|
211
216
|
}
|
|
212
|
-
}),
|
|
217
|
+
}), s;
|
|
213
218
|
}
|
|
214
|
-
async chatStream(t,
|
|
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(
|
|
216
|
-
console.error("Chat failed:",
|
|
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
|
|
232
|
+
const s = [];
|
|
228
233
|
for (; this.iteration > 0; ) {
|
|
229
|
-
const
|
|
230
|
-
if (
|
|
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: `调用工具:${
|
|
234
|
-
tool_calls:
|
|
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(
|
|
242
|
-
|
|
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:
|
|
249
|
+
this.messages.push({ role: r.ASSISTANT, content: n }), this.iteration = 0;
|
|
245
250
|
}
|
|
246
251
|
t.onDone();
|
|
247
|
-
} catch (
|
|
248
|
-
throw console.error("Chat iteration failed:",
|
|
252
|
+
} catch (s) {
|
|
253
|
+
throw console.error("Chat iteration failed:", s), s;
|
|
249
254
|
}
|
|
250
255
|
}
|
|
251
256
|
}
|