@glodon-aiot/bot-client-sdk 3.26.0 → 3.27.0-alpha.0
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/README.md +3 -0
- package/dist/es/AgentSession.mjs +192 -184
- package/dist/es/Config.mjs +2 -1
- package/dist/es/package.json.mjs +7 -7
- package/dist/lib/index.js +17 -17
- package/dist/src/Config.d.ts +8 -0
- package/dist/src/PayloadTypes.d.ts +5 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -50,6 +50,7 @@ const client = new BotClient(token, {
|
|
|
50
50
|
env: 'dev', // 'dev' | 'test' | 'prod' | 'local',决定默认 API 地址
|
|
51
51
|
// apiRoot: 'https://example.com/api/cvforce', // 传入后覆盖 env
|
|
52
52
|
stream: true, // 省略时 Agent / 对话型默认流式
|
|
53
|
+
// appVersion: 'v1', // 编排版本:draft | latest | v1/v2…;省略时默认传 latest
|
|
53
54
|
// plugins、builtinPlugins 见「Agent 插件」
|
|
54
55
|
});
|
|
55
56
|
|
|
@@ -65,6 +66,8 @@ client.addEventListener('ready', () => {
|
|
|
65
66
|
|
|
66
67
|
应用类型由平台应用决定,`loadSession` 按类型创建对应会话。
|
|
67
68
|
|
|
69
|
+
`appVersion` 只作用于 Agent 的 WebSocket 建连。`draft` 使用草稿,`v1` / `v2` 等指定已发布版本(大小写不敏感),`latest` 始终使用最新版本。不传或传空白时 SDK 默认传 `latest`。其他值不会导致连接失败,服务端会改用最新版本。ready 带回的版本与请求不一致时,SDK 输出告警。
|
|
70
|
+
|
|
68
71
|
## 对话界面
|
|
69
72
|
|
|
70
73
|
界面自管渲染状态,数据以 `session.messages` 为准。事件都带当前消息或列表,收到后重绘即可。
|
package/dist/es/AgentSession.mjs
CHANGED
|
@@ -1,84 +1,90 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var S = (
|
|
1
|
+
var x = Object.defineProperty, j = Object.defineProperties;
|
|
2
|
+
var O = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var T = Object.getOwnPropertySymbols;
|
|
4
|
+
var V = Object.prototype.hasOwnProperty, F = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var S = (n, d, e) => d in n ? x(n, d, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[d] = e, m = (n, d) => {
|
|
6
6
|
for (var e in d || (d = {}))
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var e of
|
|
10
|
-
F.call(d, e) && S(
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
var
|
|
14
|
-
var v = (
|
|
15
|
-
var
|
|
7
|
+
V.call(d, e) && S(n, e, d[e]);
|
|
8
|
+
if (T)
|
|
9
|
+
for (var e of T(d))
|
|
10
|
+
F.call(d, e) && S(n, e, d[e]);
|
|
11
|
+
return n;
|
|
12
|
+
}, C = (n, d) => j(n, O(d));
|
|
13
|
+
var I = (n, d, e) => (S(n, typeof d != "symbol" ? d + "" : d, e), e);
|
|
14
|
+
var v = (n, d, e) => new Promise((i, a) => {
|
|
15
|
+
var o = (t) => {
|
|
16
16
|
try {
|
|
17
|
-
|
|
18
|
-
} catch (
|
|
19
|
-
a(
|
|
17
|
+
g(e.next(t));
|
|
18
|
+
} catch (s) {
|
|
19
|
+
a(s);
|
|
20
20
|
}
|
|
21
21
|
}, c = (t) => {
|
|
22
22
|
try {
|
|
23
|
-
|
|
24
|
-
} catch (
|
|
25
|
-
a(
|
|
23
|
+
g(e.throw(t));
|
|
24
|
+
} catch (s) {
|
|
25
|
+
a(s);
|
|
26
26
|
}
|
|
27
|
-
},
|
|
28
|
-
|
|
27
|
+
}, g = (t) => t.done ? i(t.value) : Promise.resolve(t.value).then(o, c);
|
|
28
|
+
g((e = e.apply(n, d)).next());
|
|
29
29
|
});
|
|
30
|
-
import { ChatMessageRole as
|
|
30
|
+
import { ChatMessageRole as w, PromptVariableType as L } from "@glodon-aiot/apis";
|
|
31
31
|
import N from "./Logger.mjs";
|
|
32
32
|
import { configuration as p } from "./Config.mjs";
|
|
33
|
-
import { io as
|
|
34
|
-
import { UserMode as
|
|
35
|
-
import
|
|
36
|
-
import
|
|
37
|
-
import { v4 as
|
|
38
|
-
import { connectLocalMcpBridge as
|
|
39
|
-
import { registerLocalMcp as
|
|
40
|
-
const
|
|
41
|
-
function
|
|
42
|
-
if (typeof
|
|
43
|
-
return
|
|
44
|
-
const d =
|
|
33
|
+
import { io as $ } from "socket.io-client";
|
|
34
|
+
import { UserMode as R } from "./PayloadTypes.mjs";
|
|
35
|
+
import D, { SessionErrors as A } from "./Session.mjs";
|
|
36
|
+
import z from "dayjs";
|
|
37
|
+
import { v4 as B } from "uuid";
|
|
38
|
+
import { connectLocalMcpBridge as J, subscribeLocalMcpEndpoints as Q } from "./localMcp/endpoints.mjs";
|
|
39
|
+
import { registerLocalMcp as Y } from "./localMcp/register.mjs";
|
|
40
|
+
const r = new N("[AgentSession] ");
|
|
41
|
+
function U(n) {
|
|
42
|
+
if (typeof n != "string")
|
|
43
|
+
return n;
|
|
44
|
+
const d = n.trim();
|
|
45
45
|
if (d)
|
|
46
46
|
try {
|
|
47
47
|
return JSON.parse(d);
|
|
48
48
|
} catch (e) {
|
|
49
|
-
return
|
|
49
|
+
return n;
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
function
|
|
53
|
-
const d =
|
|
54
|
-
return Array.isArray(d) ?
|
|
52
|
+
function H(n) {
|
|
53
|
+
const d = n.imageContent;
|
|
54
|
+
return Array.isArray(d) ? C(m({}, n), {
|
|
55
55
|
imageContent: `[${d.length}]`
|
|
56
|
-
}) :
|
|
56
|
+
}) : n;
|
|
57
57
|
}
|
|
58
|
-
function
|
|
59
|
-
|
|
58
|
+
function K(n, d) {
|
|
59
|
+
if (typeof d != "string")
|
|
60
|
+
return;
|
|
61
|
+
const e = d.trim();
|
|
62
|
+
!e || n.trim().toLowerCase() === e.toLowerCase() || console.warn(`[AgentSession] 应用版本「${n}」未生效,服务端已改用「${e}」。`);
|
|
60
63
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
function _(n) {
|
|
65
|
+
return n == null || n === "" || typeof n == "object" && !Array.isArray(n) && Object.keys(n).length === 0;
|
|
66
|
+
}
|
|
67
|
+
function G(n, d) {
|
|
68
|
+
const e = U(n == null ? void 0 : n.query);
|
|
69
|
+
if (!_(e))
|
|
64
70
|
return e;
|
|
65
|
-
const i =
|
|
66
|
-
return
|
|
71
|
+
const i = U(d);
|
|
72
|
+
return _(i) && e != null ? e : i;
|
|
67
73
|
}
|
|
68
|
-
class
|
|
69
|
-
constructor(e, i = "", a,
|
|
70
|
-
super(e, i, a,
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
class ce extends D {
|
|
75
|
+
constructor(e, i = "", a, o, c, g) {
|
|
76
|
+
super(e, i, a, o, c, g);
|
|
77
|
+
I(this, "inflightCalls", /* @__PURE__ */ new Set());
|
|
78
|
+
I(this, "localMcpTicket", 0);
|
|
79
|
+
I(this, "stopLocalMcpUpdates");
|
|
74
80
|
/** 已发出 `update`、尚未收到结果;下一条 ready / ack 按换票结果处理,不拆连接。 */
|
|
75
|
-
|
|
81
|
+
I(this, "pendingTokenUpdate", !1);
|
|
76
82
|
}
|
|
77
83
|
prepareConnection() {
|
|
78
84
|
return new Promise((e) => v(this, null, function* () {
|
|
79
|
-
var c;
|
|
85
|
+
var c, g;
|
|
80
86
|
if ((c = this.socket) != null && c.connected) {
|
|
81
|
-
|
|
87
|
+
r.log("socket:reuse", this.id), e(!0);
|
|
82
88
|
return;
|
|
83
89
|
}
|
|
84
90
|
this.socket && (this.socket.removeAllListeners(), this.socket.close(), this.socket = null);
|
|
@@ -88,70 +94,72 @@ class re extends z {
|
|
|
88
94
|
appId: this._applicationId,
|
|
89
95
|
sessionId: this.id,
|
|
90
96
|
Authorization: `${this.requestToken}`,
|
|
91
|
-
userMode: p.debug ?
|
|
92
|
-
}
|
|
93
|
-
|
|
97
|
+
userMode: p.debug ? R.Debug : R.Normal
|
|
98
|
+
};
|
|
99
|
+
i.appVersion = ((g = p.appVersion) == null ? void 0 : g.trim()) || "latest";
|
|
100
|
+
const a = new URL(p.apiRoot);
|
|
101
|
+
r.log("socket:connecting", {
|
|
94
102
|
sessionId: this.id,
|
|
95
103
|
origin: a.origin
|
|
96
104
|
});
|
|
97
|
-
const
|
|
105
|
+
const o = yield $(a.origin, {
|
|
98
106
|
path: a.pathname.replace("api", "socketio") + "/copilot",
|
|
99
107
|
query: i,
|
|
100
108
|
transports: ["websocket"]
|
|
101
109
|
});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}),
|
|
105
|
-
|
|
106
|
-
}),
|
|
110
|
+
o.on("connect", () => {
|
|
111
|
+
r.log("socket:connect"), this.event.emit("socket:connect"), o.emit("init");
|
|
112
|
+
}), o.on("disconnect", () => {
|
|
113
|
+
r.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1, this.pendingTokenUpdate = !1;
|
|
114
|
+
}), o.on("ready", (t) => {
|
|
107
115
|
if (this.pendingTokenUpdate) {
|
|
108
|
-
this.handleResourceTokenUpdateResult(
|
|
116
|
+
this.handleResourceTokenUpdateResult(t);
|
|
109
117
|
return;
|
|
110
118
|
}
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
119
|
+
if (t.code === 200) {
|
|
120
|
+
r.log("ready"), K(i.appVersion, t.appVersion);
|
|
121
|
+
const s = p.debug ? R.Debug : R.Normal;
|
|
122
|
+
s === R.Debug && t.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), this.registerLocalMcp(), e(!0);
|
|
115
123
|
} else {
|
|
116
124
|
this._ready && (this._ready = !1);
|
|
117
|
-
const
|
|
118
|
-
|
|
125
|
+
const s = new Error(
|
|
126
|
+
A.SocketConnectionFailed
|
|
119
127
|
// , {
|
|
120
128
|
// cause: data,
|
|
121
129
|
// }
|
|
122
130
|
);
|
|
123
|
-
|
|
131
|
+
r.error("error", s), this.event.emit("error", s), e(!1);
|
|
124
132
|
}
|
|
125
|
-
}),
|
|
126
|
-
var
|
|
133
|
+
}), o.on("action", (t, s) => v(this, null, function* () {
|
|
134
|
+
var b;
|
|
127
135
|
if (this._stopAnswer || !this.id)
|
|
128
136
|
return;
|
|
129
137
|
const {
|
|
130
138
|
type: l,
|
|
131
139
|
data: {
|
|
132
|
-
runnerIsClient:
|
|
133
|
-
pluginCode:
|
|
134
|
-
funcCode:
|
|
135
|
-
parameters:
|
|
136
|
-
requestBody:
|
|
140
|
+
runnerIsClient: y,
|
|
141
|
+
pluginCode: u,
|
|
142
|
+
funcCode: h,
|
|
143
|
+
parameters: f,
|
|
144
|
+
requestBody: q
|
|
137
145
|
}
|
|
138
|
-
} =
|
|
146
|
+
} = t, k = this.messages[this.messages.length - 1];
|
|
139
147
|
if (!k.messageId)
|
|
140
|
-
k.actions || (k.actions = []), k.actions[
|
|
148
|
+
k.actions || (k.actions = []), k.actions[t.data.step - 1] || (k.actions[t.data.step - 1] = []), k.actions[t.data.step - 1].push(t), this.event.emit("message:action", {
|
|
141
149
|
messageId: k.messageId,
|
|
142
|
-
action:
|
|
150
|
+
action: t
|
|
143
151
|
}), this.event.emit("message:updated", k);
|
|
144
152
|
else {
|
|
145
153
|
const M = {
|
|
146
154
|
messageId: "",
|
|
147
155
|
content: "",
|
|
148
|
-
role:
|
|
156
|
+
role: w.Robot,
|
|
149
157
|
sessionId: this.id,
|
|
150
158
|
reference: [],
|
|
151
159
|
searchReference: [],
|
|
152
160
|
isLoading: !0,
|
|
153
161
|
messageTime: "",
|
|
154
|
-
actions: [[
|
|
162
|
+
actions: [[t]],
|
|
155
163
|
comment: {
|
|
156
164
|
isAgree: 0
|
|
157
165
|
},
|
|
@@ -159,50 +167,50 @@ class re extends z {
|
|
|
159
167
|
};
|
|
160
168
|
this.pushMessage(M), this.event.emit("message:action", {
|
|
161
169
|
messageId: M.messageId,
|
|
162
|
-
action:
|
|
170
|
+
action: t
|
|
163
171
|
});
|
|
164
172
|
}
|
|
165
|
-
if (l === "FUNCTION_CALL" && !
|
|
166
|
-
|
|
167
|
-
${JSON.stringify(
|
|
168
|
-
const M = (
|
|
173
|
+
if (l === "FUNCTION_CALL" && !y && r.log("server function", u, h), l === "FUNCTION_CALL" && y) {
|
|
174
|
+
r.log(`calling function ${h} of plugin ${u} with argument:
|
|
175
|
+
${JSON.stringify(f)}`);
|
|
176
|
+
const M = (b = p.plugins) == null ? void 0 : b[u];
|
|
169
177
|
if (!M) {
|
|
170
|
-
|
|
178
|
+
r.error("plugin not found", u, h), s({
|
|
171
179
|
code: 404,
|
|
172
180
|
message: "没有找到插件。"
|
|
173
181
|
});
|
|
174
182
|
return;
|
|
175
183
|
}
|
|
176
|
-
const
|
|
177
|
-
if (typeof
|
|
178
|
-
|
|
184
|
+
const P = M[h];
|
|
185
|
+
if (typeof P != "function") {
|
|
186
|
+
r.error("plugin function not found", u, h), s({
|
|
179
187
|
code: 404,
|
|
180
188
|
message: "没有找到插件方法。"
|
|
181
189
|
});
|
|
182
190
|
return;
|
|
183
191
|
}
|
|
184
|
-
const
|
|
185
|
-
this.invokeClientPlugin(
|
|
186
|
-
requestId:
|
|
187
|
-
pluginCode:
|
|
188
|
-
functionCode:
|
|
192
|
+
const E = G(f, q);
|
|
193
|
+
this.invokeClientPlugin(P, E, s, {
|
|
194
|
+
requestId: t.requestId,
|
|
195
|
+
pluginCode: u,
|
|
196
|
+
functionCode: h
|
|
189
197
|
});
|
|
190
198
|
}
|
|
191
|
-
})),
|
|
192
|
-
if (
|
|
193
|
-
if (
|
|
194
|
-
let
|
|
195
|
-
if (
|
|
196
|
-
this.handleStreamAnswer(
|
|
199
|
+
})), o.on("answer", (t) => {
|
|
200
|
+
if (r.log("answer", t == null ? void 0 : t.data), !this._stopAnswer)
|
|
201
|
+
if (t.data.code !== 200 && (t.data.index = -1), t.data.stream || t.data.stream === void 0) {
|
|
202
|
+
let s = this.messages[this.messages.length - 1];
|
|
203
|
+
if (s && s.role === w.Robot)
|
|
204
|
+
this.handleStreamAnswer(t.data, s), this.messages[this.messages.length - 1] = s, this.event.emit("message:content", s), this.event.emit("message:updated", s), s.index === -1 && (s.isReplying = !1, this.event.emit("answer:finish", s));
|
|
197
205
|
else {
|
|
198
206
|
const l = {
|
|
199
207
|
content: "",
|
|
200
|
-
role:
|
|
208
|
+
role: w.Robot,
|
|
201
209
|
reference: [],
|
|
202
210
|
searchReference: [],
|
|
203
211
|
messageId: "",
|
|
204
212
|
sessionId: this.id,
|
|
205
|
-
messageTime:
|
|
213
|
+
messageTime: z(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
|
|
206
214
|
comment: {
|
|
207
215
|
isAgree: 0
|
|
208
216
|
},
|
|
@@ -217,17 +225,17 @@ class re extends z {
|
|
|
217
225
|
thinkingStatus: "",
|
|
218
226
|
searchingStatus: ""
|
|
219
227
|
};
|
|
220
|
-
this.handleStreamAnswer(
|
|
228
|
+
this.handleStreamAnswer(t.data, l), this.pushMessage(l), this.event.emit("message:content", l), l.index === -1 && (l.isReplying = !1, this.event.emit("answer:finish", l));
|
|
221
229
|
}
|
|
222
|
-
} else if (
|
|
223
|
-
let
|
|
224
|
-
if (
|
|
225
|
-
|
|
230
|
+
} else if (t.data.updateSession && this.data && (this.data = m(m({}, this.data), t.data.updateSession), this.event.emit("data:updated", this.data)), t.data.index === -1) {
|
|
231
|
+
let s = this.messages[this.messages.length - 1];
|
|
232
|
+
if (s && !s.messageId)
|
|
233
|
+
s = m(m({}, s), t.data), this.event.emit("message:content", s), this.event.emit("message:updated", s), this.event.emit("answer:finish", s);
|
|
226
234
|
else {
|
|
227
|
-
const l =
|
|
235
|
+
const l = m({
|
|
228
236
|
messageId: "",
|
|
229
237
|
content: "",
|
|
230
|
-
role:
|
|
238
|
+
role: w.Robot,
|
|
231
239
|
sessionId: this.id,
|
|
232
240
|
reference: [],
|
|
233
241
|
searchReference: [],
|
|
@@ -238,15 +246,15 @@ class re extends z {
|
|
|
238
246
|
isAgree: 0
|
|
239
247
|
},
|
|
240
248
|
isReplying: !0
|
|
241
|
-
},
|
|
249
|
+
}, t.data);
|
|
242
250
|
this.event.emit("message:content", l), this.pushMessage(l), this.event.emit("answer:finish", l);
|
|
243
251
|
}
|
|
244
252
|
} else {
|
|
245
|
-
let
|
|
246
|
-
|
|
253
|
+
let s = this.messages[this.messages.length - 1];
|
|
254
|
+
s && !s.messageId ? (s = m(m({}, s), t.data), this.messages[this.messages.length - 1] = s, this.event.emit("message:content", s), this.event.emit("message:updated", s)) : (this.pushMessage(m({
|
|
247
255
|
messageId: "",
|
|
248
256
|
content: "",
|
|
249
|
-
role:
|
|
257
|
+
role: w.Robot,
|
|
250
258
|
sessionId: this.id,
|
|
251
259
|
reference: [],
|
|
252
260
|
searchReference: [],
|
|
@@ -257,40 +265,40 @@ class re extends z {
|
|
|
257
265
|
isAgree: 0
|
|
258
266
|
},
|
|
259
267
|
isReplying: !0
|
|
260
|
-
},
|
|
268
|
+
}, t.data)), this.event.emit("message:content", t.data));
|
|
261
269
|
}
|
|
262
|
-
}),
|
|
270
|
+
}), o.on("reference", (t) => v(this, null, function* () {
|
|
263
271
|
if (this._stopAnswer)
|
|
264
272
|
return;
|
|
265
273
|
if (!this.id) {
|
|
266
|
-
const
|
|
267
|
-
|
|
274
|
+
const f = new Error(
|
|
275
|
+
A.IdUndefined
|
|
268
276
|
// {
|
|
269
277
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
270
278
|
// }
|
|
271
279
|
);
|
|
272
|
-
|
|
280
|
+
r.error(f), this.event.emit("error", f), this.frozen = !1;
|
|
273
281
|
return;
|
|
274
282
|
}
|
|
275
283
|
const {
|
|
276
|
-
type:
|
|
284
|
+
type: s,
|
|
277
285
|
data: l,
|
|
278
|
-
requestId:
|
|
279
|
-
} =
|
|
280
|
-
|
|
281
|
-
type:
|
|
282
|
-
requestId:
|
|
286
|
+
requestId: y
|
|
287
|
+
} = t;
|
|
288
|
+
r.log("reference", {
|
|
289
|
+
type: s,
|
|
290
|
+
requestId: y,
|
|
283
291
|
data: l
|
|
284
292
|
});
|
|
285
|
-
const
|
|
286
|
-
if (
|
|
293
|
+
const u = this.messages.findIndex((f) => f.messageId === y && f.role === w.User);
|
|
294
|
+
if (u < 0)
|
|
287
295
|
return;
|
|
288
|
-
const
|
|
289
|
-
if (
|
|
290
|
-
const
|
|
296
|
+
const h = u + 1;
|
|
297
|
+
if (h >= this.messages.length) {
|
|
298
|
+
const f = {
|
|
291
299
|
messageId: "",
|
|
292
300
|
content: "",
|
|
293
|
-
role:
|
|
301
|
+
role: w.Robot,
|
|
294
302
|
sessionId: this.id,
|
|
295
303
|
reference: [],
|
|
296
304
|
searchReference: [],
|
|
@@ -302,14 +310,14 @@ class re extends z {
|
|
|
302
310
|
},
|
|
303
311
|
isReplying: !0
|
|
304
312
|
};
|
|
305
|
-
this.pushMessage(
|
|
313
|
+
this.pushMessage(f);
|
|
306
314
|
}
|
|
307
|
-
|
|
315
|
+
s === "KG" ? (this.messages[h] = C(m({}, this.messages[h]), {
|
|
308
316
|
reference: l.knowledgeReference
|
|
309
|
-
}), yield this.fillMessageReference(this.messages[
|
|
317
|
+
}), yield this.fillMessageReference(this.messages[h]), this.event.emit("message:reference", l.knowledgeReference), this.event.emit("message:updated", this.messages[h])) : s === "WEB_SEARCH" && (this.messages[h] = C(m({}, this.messages[h]), {
|
|
310
318
|
searchReference: l.knowledgeReference
|
|
311
|
-
}), this.event.emit("message:searchreference", l.searchReference), this.event.emit("message:updated", this.messages[
|
|
312
|
-
})), this.socket =
|
|
319
|
+
}), this.event.emit("message:searchreference", l.searchReference), this.event.emit("message:updated", this.messages[h]));
|
|
320
|
+
})), this.socket = o, o.open();
|
|
313
321
|
}));
|
|
314
322
|
}
|
|
315
323
|
registerMcpFromClientConfig() {
|
|
@@ -319,39 +327,39 @@ class re extends z {
|
|
|
319
327
|
return;
|
|
320
328
|
}
|
|
321
329
|
this.socket.emit("add_mcp_servers", p.mcp, (a) => {
|
|
322
|
-
a.code === 200 ? (Object.keys(a.data).forEach((
|
|
323
|
-
a.data[
|
|
324
|
-
}),
|
|
330
|
+
a.code === 200 ? (Object.keys(a.data).forEach((o) => {
|
|
331
|
+
a.data[o].code !== 200 && r.report(o, "MCP Server 注册失败。", a.data[o]);
|
|
332
|
+
}), r.log("mcp:registered", Object.keys(a.data)), e(!0)) : (r.error("MCP Server 注册失败。", a), i(a));
|
|
325
333
|
});
|
|
326
|
-
}) : (
|
|
334
|
+
}) : (r.log("mcp:skip"), Promise.resolve(!0));
|
|
327
335
|
}
|
|
328
336
|
registerLocalMcp() {
|
|
329
|
-
var a,
|
|
337
|
+
var a, o;
|
|
330
338
|
const e = this.socket;
|
|
331
339
|
if (!e)
|
|
332
340
|
return;
|
|
333
|
-
|
|
341
|
+
J(), this.stopLocalMcpUpdates || (this.stopLocalMcpUpdates = Q(() => this.registerLocalMcp()));
|
|
334
342
|
const i = ++this.localMcpTicket;
|
|
335
|
-
|
|
336
|
-
arrange: (
|
|
343
|
+
Y({
|
|
344
|
+
arrange: (o = (a = this.application) == null ? void 0 : a.arrange) == null ? void 0 : o[0],
|
|
337
345
|
initMcp: p.mcp,
|
|
338
346
|
apiRoot: p.apiRoot,
|
|
339
347
|
token: this.requestToken,
|
|
340
348
|
socket: e,
|
|
341
349
|
isCurrent: () => i === this.localMcpTicket
|
|
342
350
|
}).catch((c) => {
|
|
343
|
-
|
|
351
|
+
r.report("local mcp", c instanceof Error ? c.message : String(c));
|
|
344
352
|
});
|
|
345
353
|
}
|
|
346
354
|
addMcpServers(e) {
|
|
347
355
|
return v(this, null, function* () {
|
|
348
356
|
return this.ready ? this.socket ? new Promise((i, a) => {
|
|
349
|
-
this.socket && this.socket.emit("add_mcp_servers", e, (
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}),
|
|
357
|
+
this.socket && this.socket.emit("add_mcp_servers", e, (o) => {
|
|
358
|
+
o.code === 200 ? (Object.keys(o.data).forEach((c) => {
|
|
359
|
+
o.data[c].code !== 200 && r.report(c, "MCP Server 注册失败。", o.data[c]);
|
|
360
|
+
}), r.log("mcp:registered", Object.keys(o.data)), i(!0)) : (r.error("MCP Server 注册失败。", o), a(o));
|
|
353
361
|
});
|
|
354
|
-
}) : (
|
|
362
|
+
}) : (r.error("socket is undefined."), Promise.reject("socket is undefined")) : (r.error("session is not avalible."), Promise.reject("session is not avalible"));
|
|
355
363
|
});
|
|
356
364
|
}
|
|
357
365
|
listMcpServers() {
|
|
@@ -360,21 +368,21 @@ class re extends z {
|
|
|
360
368
|
this.socket && this.socket.emit("list_mcp_servers", (a) => {
|
|
361
369
|
a.code === 200 ? e(a.data) : i(a);
|
|
362
370
|
});
|
|
363
|
-
}) : (
|
|
371
|
+
}) : (r.error("socket is undefined."), Promise.reject("socket is undefined")) : (r.error("session is not avalible."), Promise.reject("session is not avalible"));
|
|
364
372
|
});
|
|
365
373
|
}
|
|
366
374
|
sendQuestion(e, i) {
|
|
367
375
|
return v(this, null, function* () {
|
|
368
|
-
var
|
|
376
|
+
var o;
|
|
369
377
|
this.convertKnowledgesToChatFormat(this.knowledges);
|
|
370
|
-
let a =
|
|
371
|
-
promptVariables: this.promptVariables.filter((c) => c.type ===
|
|
372
|
-
[
|
|
378
|
+
let a = C(m({}, e), {
|
|
379
|
+
promptVariables: this.promptVariables.filter((c) => c.type === L.User).reduce((c, g) => C(m({}, c), {
|
|
380
|
+
[g.key]: g.value
|
|
373
381
|
}), {}),
|
|
374
|
-
systemPromptVariables: this.promptVariables.filter((c) => c.type ===
|
|
375
|
-
[
|
|
382
|
+
systemPromptVariables: this.promptVariables.filter((c) => c.type === L.System).reduce((c, g) => C(m({}, c), {
|
|
383
|
+
[g.key]: g.value
|
|
376
384
|
}), {}),
|
|
377
|
-
knowledges: (
|
|
385
|
+
knowledges: (o = this.knowledges) == null ? void 0 : o.map((c) => c.id),
|
|
378
386
|
//agent支持指定文件时再放开
|
|
379
387
|
// knowledges: knowledgesForChat,
|
|
380
388
|
enableTitleSummary: this.resolveEnableTitleSummary()
|
|
@@ -382,12 +390,12 @@ class re extends z {
|
|
|
382
390
|
if (!this.active) {
|
|
383
391
|
if (yield this.activate(), !this.socket) {
|
|
384
392
|
const c = new Error(
|
|
385
|
-
|
|
393
|
+
A.SocketConnectionFailed
|
|
386
394
|
// {
|
|
387
395
|
// cause: { code: -1, message: 'socket is null' },
|
|
388
396
|
// }
|
|
389
397
|
);
|
|
390
|
-
return
|
|
398
|
+
return r.warn("error", c), this.event.emit("error", c), !1;
|
|
391
399
|
}
|
|
392
400
|
this.emitQuestion(a);
|
|
393
401
|
return;
|
|
@@ -397,7 +405,7 @@ class re extends z {
|
|
|
397
405
|
}
|
|
398
406
|
emitQuestion(e) {
|
|
399
407
|
var i;
|
|
400
|
-
|
|
408
|
+
r.log("question:request", H(e)), (i = this.socket) == null || i.emit("question", e), r.log("question:sent", {
|
|
401
409
|
sessionId: this.id
|
|
402
410
|
});
|
|
403
411
|
}
|
|
@@ -405,30 +413,30 @@ class re extends z {
|
|
|
405
413
|
* 调用客户端插件:传入第三个参数 context(旧插件可忽略),结果最多回传一次,
|
|
406
414
|
* 停止回答或调用被取消后不再回传。
|
|
407
415
|
*/
|
|
408
|
-
invokeClientPlugin(e, i, a,
|
|
416
|
+
invokeClientPlugin(e, i, a, o) {
|
|
409
417
|
const c = new AbortController();
|
|
410
418
|
this.inflightCalls.add(c);
|
|
411
|
-
let
|
|
412
|
-
const t = (...
|
|
413
|
-
|
|
414
|
-
},
|
|
415
|
-
requestId:
|
|
419
|
+
let g = !1;
|
|
420
|
+
const t = (...l) => {
|
|
421
|
+
g || (g = !0, this.inflightCalls.delete(c), !(this._stopAnswer || c.signal.aborted) && a(...l));
|
|
422
|
+
}, s = {
|
|
423
|
+
requestId: o.requestId || B(),
|
|
416
424
|
applicationId: this._applicationId,
|
|
417
425
|
sessionId: this.id,
|
|
418
|
-
pluginCode:
|
|
419
|
-
functionCode:
|
|
426
|
+
pluginCode: o.pluginCode,
|
|
427
|
+
functionCode: o.functionCode,
|
|
420
428
|
signal: c.signal
|
|
421
429
|
};
|
|
422
430
|
try {
|
|
423
|
-
const
|
|
424
|
-
|
|
425
|
-
|
|
431
|
+
const l = e(i, t, s);
|
|
432
|
+
l && typeof l.catch == "function" && l.catch((y) => {
|
|
433
|
+
r.error("client plugin failed", y), t({
|
|
426
434
|
code: 500,
|
|
427
435
|
message: "客户端插件执行失败。"
|
|
428
436
|
});
|
|
429
437
|
});
|
|
430
|
-
} catch (
|
|
431
|
-
|
|
438
|
+
} catch (l) {
|
|
439
|
+
r.error("client plugin failed", l), t({
|
|
432
440
|
code: 500,
|
|
433
441
|
message: "客户端插件执行失败。"
|
|
434
442
|
});
|
|
@@ -447,7 +455,7 @@ class re extends z {
|
|
|
447
455
|
if (!this.socket)
|
|
448
456
|
return;
|
|
449
457
|
const i = this.socket.io.opts.query;
|
|
450
|
-
i && (i.Authorization = e), this.socket.connected && (this.pendingTokenUpdate = !0,
|
|
458
|
+
i && (i.Authorization = e), this.socket.connected && (this.pendingTokenUpdate = !0, r.log("socket:update", this.id), this.socket.emit("update", {
|
|
451
459
|
resourceToken: e
|
|
452
460
|
}, (a) => {
|
|
453
461
|
this.pendingTokenUpdate && this.handleResourceTokenUpdateResult(a);
|
|
@@ -455,17 +463,17 @@ class re extends z {
|
|
|
455
463
|
}
|
|
456
464
|
handleResourceTokenUpdateResult(e) {
|
|
457
465
|
if (this.pendingTokenUpdate = !1, !e || e.code === 200) {
|
|
458
|
-
|
|
466
|
+
r.log("socket:update:ok", this.id);
|
|
459
467
|
return;
|
|
460
468
|
}
|
|
461
|
-
const i = new Error(
|
|
469
|
+
const i = new Error(A.SocketConnectionFailed, {
|
|
462
470
|
cause: e
|
|
463
471
|
});
|
|
464
|
-
|
|
472
|
+
r.error("socket:update:failed", e), this.event.emit("error", i);
|
|
465
473
|
}
|
|
466
474
|
stopAnswer() {
|
|
467
475
|
var e;
|
|
468
|
-
|
|
476
|
+
r.log("stopAnswer", this.id), this.abortClientPluginCalls(), (e = this.socket) == null || e.emit("stop_answer");
|
|
469
477
|
}
|
|
470
478
|
inactivate(e = !1) {
|
|
471
479
|
return !e && this.frozen && !!this.socket || this.abortClientPluginCalls(), super.inactivate(e);
|
|
@@ -482,5 +490,5 @@ class re extends z {
|
|
|
482
490
|
}
|
|
483
491
|
}
|
|
484
492
|
export {
|
|
485
|
-
|
|
493
|
+
ce as default
|
|
486
494
|
};
|