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