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