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