@glodon-aiot/bot-client-sdk 3.26.0 → 3.26.2
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 +28 -0
- package/dist/es/AgentSession.mjs +159 -156
- package/dist/es/PayloadTypes.mjs +13 -2
- package/dist/es/Session.mjs +349 -310
- package/dist/es/index.mjs +71 -66
- package/dist/es/package.json.mjs +2 -2
- package/dist/lib/index.js +45 -45
- package/dist/src/PayloadTypes.d.ts +18 -0
- package/dist/src/Session.d.ts +5 -1
- package/dist/src/index.d.ts +4 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -105,6 +105,34 @@ await client.removeSession(id, true);
|
|
|
105
105
|
|
|
106
106
|
发送开始后 `session.frozen === true`,此时 `send` 会失败;输入框保持禁用直到 `answer:finish`。
|
|
107
107
|
|
|
108
|
+
## 额度校验与用量数据
|
|
109
|
+
|
|
110
|
+
Agent 应用绑定产线用量扣费后,服务端会在本次回答中附带用量换算返回正文 `usageExtension`,并在异常时下发对应错误码:
|
|
111
|
+
|
|
112
|
+
- **消息字段 `message.usageExtension`**:原样保存服务端返回的用量换算正文(`{ code, message, data: { remainingUsage?, ... } }`)。流式来自 `result` 帧的 `payload.usageExtension`,非流式来自回答外层 `data.usageExtension`。未绑定或未做用量换算时该字段不出现(不置 `{}` 或 `null`)。
|
|
113
|
+
- **用量错误码(`UsageErrorCode`)**:
|
|
114
|
+
- `11937`(`UsageErrorCode.InsufficientBalance`):余额不足,未调用模型,无回答正文。
|
|
115
|
+
- `11938`(`UsageErrorCode.ConversionFailed`):用量换算失败;**已生成的回答正文与 `usageExtension` 完整保留,不被改写**。
|
|
116
|
+
- `11939`(`UsageErrorCode.QuotaCheckFailed`):额度校验失败,未调用模型,无回答正文。
|
|
117
|
+
- **错误事件**:上述三类错误会同时触发 `session` 的 `message:error`(完整消息)与 `error` 事件(`error.message === SessionErrors.UsageError`,`error.cause` 为 `{ code, message, usageExtension?, messageId?, sessionId? }`)。
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import {
|
|
121
|
+
SessionErrors,
|
|
122
|
+
UsageErrorCode,
|
|
123
|
+
type IUsageExtension,
|
|
124
|
+
type UsageErrorCause,
|
|
125
|
+
} from '@glodon-aiot/bot-client-sdk';
|
|
126
|
+
|
|
127
|
+
session.addEventListener('error', (error: Error & { cause?: UsageErrorCause }) => {
|
|
128
|
+
if (error.message === SessionErrors.UsageError && error.cause) {
|
|
129
|
+
if (error.cause.code === UsageErrorCode.InsufficientBalance) {
|
|
130
|
+
// 余额不足处理
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
108
136
|
## 产出物
|
|
109
137
|
|
|
110
138
|
任务结束后可拉取本次会话留下的文件(会话级,不是单条消息附件)。无产出物返回空数组,不抛错。
|
package/dist/es/AgentSession.mjs
CHANGED
|
@@ -2,30 +2,30 @@ var E = Object.defineProperty, x = Object.defineProperties;
|
|
|
2
2
|
var j = Object.getOwnPropertyDescriptors;
|
|
3
3
|
var P = Object.getOwnPropertySymbols;
|
|
4
4
|
var O = Object.prototype.hasOwnProperty, F = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var S = (
|
|
6
|
-
for (var e in
|
|
7
|
-
O.call(
|
|
5
|
+
var S = (a, l, e) => l in a ? E(a, l, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[l] = e, h = (a, l) => {
|
|
6
|
+
for (var e in l || (l = {}))
|
|
7
|
+
O.call(l, e) && S(a, e, l[e]);
|
|
8
8
|
if (P)
|
|
9
|
-
for (var e of P(
|
|
10
|
-
F.call(
|
|
11
|
-
return
|
|
12
|
-
}, y = (
|
|
13
|
-
var w = (
|
|
14
|
-
var v = (
|
|
15
|
-
var
|
|
9
|
+
for (var e of P(l))
|
|
10
|
+
F.call(l, e) && S(a, e, l[e]);
|
|
11
|
+
return a;
|
|
12
|
+
}, y = (a, l) => x(a, j(l));
|
|
13
|
+
var w = (a, l, e) => (S(a, typeof l != "symbol" ? l + "" : l, e), e);
|
|
14
|
+
var v = (a, l, e) => new Promise((i, c) => {
|
|
15
|
+
var o = (t) => {
|
|
16
16
|
try {
|
|
17
17
|
s(e.next(t));
|
|
18
|
-
} catch (
|
|
19
|
-
|
|
18
|
+
} catch (n) {
|
|
19
|
+
c(n);
|
|
20
20
|
}
|
|
21
|
-
},
|
|
21
|
+
}, d = (t) => {
|
|
22
22
|
try {
|
|
23
23
|
s(e.throw(t));
|
|
24
|
-
} catch (
|
|
25
|
-
|
|
24
|
+
} catch (n) {
|
|
25
|
+
c(n);
|
|
26
26
|
}
|
|
27
|
-
}, s = (t) => t.done ? i(t.value) : Promise.resolve(t.value).then(
|
|
28
|
-
s((e = e.apply(
|
|
27
|
+
}, s = (t) => t.done ? i(t.value) : Promise.resolve(t.value).then(o, d);
|
|
28
|
+
s((e = e.apply(a, l)).next());
|
|
29
29
|
});
|
|
30
30
|
import { ChatMessageRole as C, PromptVariableType as T } from "@glodon-aiot/apis";
|
|
31
31
|
import N from "./Logger.mjs";
|
|
@@ -37,37 +37,37 @@ import V from "dayjs";
|
|
|
37
37
|
import { v4 as $ } from "uuid";
|
|
38
38
|
import { connectLocalMcpBridge as B, subscribeLocalMcpEndpoints as J } from "./localMcp/endpoints.mjs";
|
|
39
39
|
import { registerLocalMcp as Q } from "./localMcp/register.mjs";
|
|
40
|
-
const
|
|
41
|
-
function L(
|
|
42
|
-
if (typeof
|
|
43
|
-
return
|
|
44
|
-
const
|
|
45
|
-
if (
|
|
40
|
+
const r = new N("[AgentSession] ");
|
|
41
|
+
function L(a) {
|
|
42
|
+
if (typeof a != "string")
|
|
43
|
+
return a;
|
|
44
|
+
const l = a.trim();
|
|
45
|
+
if (l)
|
|
46
46
|
try {
|
|
47
|
-
return JSON.parse(
|
|
47
|
+
return JSON.parse(l);
|
|
48
48
|
} catch (e) {
|
|
49
|
-
return
|
|
49
|
+
return a;
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
function Y(
|
|
53
|
-
const
|
|
54
|
-
return Array.isArray(
|
|
55
|
-
imageContent: `[${
|
|
56
|
-
}) :
|
|
52
|
+
function Y(a) {
|
|
53
|
+
const l = a.imageContent;
|
|
54
|
+
return Array.isArray(l) ? y(h({}, a), {
|
|
55
|
+
imageContent: `[${l.length}]`
|
|
56
|
+
}) : a;
|
|
57
57
|
}
|
|
58
|
-
function U(
|
|
59
|
-
return
|
|
58
|
+
function U(a) {
|
|
59
|
+
return a == null || a === "" || typeof a == "object" && !Array.isArray(a) && Object.keys(a).length === 0;
|
|
60
60
|
}
|
|
61
|
-
function H(
|
|
62
|
-
const e = L(
|
|
61
|
+
function H(a, l) {
|
|
62
|
+
const e = L(a == null ? void 0 : a.query);
|
|
63
63
|
if (!U(e))
|
|
64
64
|
return e;
|
|
65
|
-
const i = L(
|
|
65
|
+
const i = L(l);
|
|
66
66
|
return U(i) && e != null ? e : i;
|
|
67
67
|
}
|
|
68
68
|
class re extends z {
|
|
69
|
-
constructor(e, i = "",
|
|
70
|
-
super(e, i,
|
|
69
|
+
constructor(e, i = "", c, o, d, s) {
|
|
70
|
+
super(e, i, c, o, d, s);
|
|
71
71
|
w(this, "inflightCalls", /* @__PURE__ */ new Set());
|
|
72
72
|
w(this, "localMcpTicket", 0);
|
|
73
73
|
w(this, "stopLocalMcpUpdates");
|
|
@@ -76,9 +76,9 @@ class re extends z {
|
|
|
76
76
|
}
|
|
77
77
|
prepareConnection() {
|
|
78
78
|
return new Promise((e) => v(this, null, function* () {
|
|
79
|
-
var
|
|
80
|
-
if ((
|
|
81
|
-
|
|
79
|
+
var d;
|
|
80
|
+
if ((d = this.socket) != null && d.connected) {
|
|
81
|
+
r.log("socket:reuse", this.id), e(!0);
|
|
82
82
|
return;
|
|
83
83
|
}
|
|
84
84
|
this.socket && (this.socket.removeAllListeners(), this.socket.close(), this.socket = null);
|
|
@@ -89,27 +89,27 @@ class re extends z {
|
|
|
89
89
|
sessionId: this.id,
|
|
90
90
|
Authorization: `${this.requestToken}`,
|
|
91
91
|
userMode: p.debug ? I.Debug : I.Normal
|
|
92
|
-
},
|
|
93
|
-
|
|
92
|
+
}, c = new URL(p.apiRoot);
|
|
93
|
+
r.log("socket:connecting", {
|
|
94
94
|
sessionId: this.id,
|
|
95
|
-
origin:
|
|
95
|
+
origin: c.origin
|
|
96
96
|
});
|
|
97
|
-
const
|
|
98
|
-
path:
|
|
97
|
+
const o = yield D(c.origin, {
|
|
98
|
+
path: c.pathname.replace("api", "socketio") + "/copilot",
|
|
99
99
|
query: i,
|
|
100
100
|
transports: ["websocket"]
|
|
101
101
|
});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}),
|
|
105
|
-
|
|
106
|
-
}),
|
|
102
|
+
o.on("connect", () => {
|
|
103
|
+
r.log("socket:connect"), this.event.emit("socket:connect"), o.emit("init");
|
|
104
|
+
}), o.on("disconnect", () => {
|
|
105
|
+
r.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1, this.pendingTokenUpdate = !1;
|
|
106
|
+
}), o.on("ready", (s) => {
|
|
107
107
|
if (this.pendingTokenUpdate) {
|
|
108
108
|
this.handleResourceTokenUpdateResult(s);
|
|
109
109
|
return;
|
|
110
110
|
}
|
|
111
111
|
if (s.code === 200) {
|
|
112
|
-
|
|
112
|
+
r.log("ready");
|
|
113
113
|
const t = p.debug ? I.Debug : I.Normal;
|
|
114
114
|
t === I.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), this.registerLocalMcp(), e(!0);
|
|
115
115
|
} else {
|
|
@@ -120,18 +120,18 @@ class re extends z {
|
|
|
120
120
|
// cause: data,
|
|
121
121
|
// }
|
|
122
122
|
);
|
|
123
|
-
|
|
123
|
+
r.error("error", t), this.event.emit("error", t), e(!1);
|
|
124
124
|
}
|
|
125
|
-
}),
|
|
125
|
+
}), o.on("action", (s, t) => v(this, null, function* () {
|
|
126
126
|
var A;
|
|
127
127
|
if (this._stopAnswer || !this.id)
|
|
128
128
|
return;
|
|
129
129
|
const {
|
|
130
|
-
type:
|
|
130
|
+
type: n,
|
|
131
131
|
data: {
|
|
132
|
-
runnerIsClient:
|
|
132
|
+
runnerIsClient: g,
|
|
133
133
|
pluginCode: f,
|
|
134
|
-
funcCode:
|
|
134
|
+
funcCode: m,
|
|
135
135
|
parameters: u,
|
|
136
136
|
requestBody: _
|
|
137
137
|
}
|
|
@@ -162,20 +162,20 @@ class re extends z {
|
|
|
162
162
|
action: s
|
|
163
163
|
});
|
|
164
164
|
}
|
|
165
|
-
if (
|
|
166
|
-
|
|
165
|
+
if (n === "FUNCTION_CALL" && !g && r.log("server function", f, m), n === "FUNCTION_CALL" && g) {
|
|
166
|
+
r.log(`calling function ${m} of plugin ${f} with argument:
|
|
167
167
|
${JSON.stringify(u)}`);
|
|
168
168
|
const M = (A = p.plugins) == null ? void 0 : A[f];
|
|
169
169
|
if (!M) {
|
|
170
|
-
|
|
170
|
+
r.error("plugin not found", f, m), t({
|
|
171
171
|
code: 404,
|
|
172
172
|
message: "没有找到插件。"
|
|
173
173
|
});
|
|
174
174
|
return;
|
|
175
175
|
}
|
|
176
|
-
const b = M[
|
|
176
|
+
const b = M[m];
|
|
177
177
|
if (typeof b != "function") {
|
|
178
|
-
|
|
178
|
+
r.error("plugin function not found", f, m), t({
|
|
179
179
|
code: 404,
|
|
180
180
|
message: "没有找到插件方法。"
|
|
181
181
|
});
|
|
@@ -185,17 +185,17 @@ class re extends z {
|
|
|
185
185
|
this.invokeClientPlugin(b, q, t, {
|
|
186
186
|
requestId: s.requestId,
|
|
187
187
|
pluginCode: f,
|
|
188
|
-
functionCode:
|
|
188
|
+
functionCode: m
|
|
189
189
|
});
|
|
190
190
|
}
|
|
191
|
-
})),
|
|
192
|
-
if (
|
|
191
|
+
})), o.on("answer", (s) => {
|
|
192
|
+
if (r.log("answer", s == null ? void 0 : s.data), !this._stopAnswer)
|
|
193
193
|
if (s.data.code !== 200 && (s.data.index = -1), s.data.stream || s.data.stream === void 0) {
|
|
194
194
|
let t = this.messages[this.messages.length - 1];
|
|
195
195
|
if (t && t.role === C.Robot)
|
|
196
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));
|
|
197
197
|
else {
|
|
198
|
-
const
|
|
198
|
+
const n = {
|
|
199
199
|
content: "",
|
|
200
200
|
role: C.Robot,
|
|
201
201
|
reference: [],
|
|
@@ -217,14 +217,35 @@ class re extends z {
|
|
|
217
217
|
thinkingStatus: "",
|
|
218
218
|
searchingStatus: ""
|
|
219
219
|
};
|
|
220
|
-
this.handleStreamAnswer(s.data,
|
|
220
|
+
this.handleStreamAnswer(s.data, n), this.pushMessage(n), this.event.emit("message:content", n), n.index === -1 && (n.isReplying = !1, this.event.emit("answer:finish", n));
|
|
221
221
|
}
|
|
222
|
-
} else
|
|
223
|
-
|
|
224
|
-
if (t &&
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
222
|
+
} else {
|
|
223
|
+
const t = h({}, s.data);
|
|
224
|
+
if (t.usageExtension == null && delete t.usageExtension, t.updateSession && this.data && (this.data = h(h({}, this.data), t.updateSession), this.event.emit("data:updated", this.data)), t.index === -1) {
|
|
225
|
+
let n = this.messages[this.messages.length - 1];
|
|
226
|
+
if (n && !n.messageId)
|
|
227
|
+
n = h(h({}, n), t), this.messages[this.messages.length - 1] = n, this.event.emit("message:content", n), this.event.emit("message:updated", n), this.event.emit("answer:finish", n);
|
|
228
|
+
else {
|
|
229
|
+
const g = h({
|
|
230
|
+
messageId: "",
|
|
231
|
+
content: "",
|
|
232
|
+
role: C.Robot,
|
|
233
|
+
sessionId: this.id,
|
|
234
|
+
reference: [],
|
|
235
|
+
searchReference: [],
|
|
236
|
+
isLoading: !1,
|
|
237
|
+
messageTime: "",
|
|
238
|
+
actions: [],
|
|
239
|
+
comment: {
|
|
240
|
+
isAgree: 0
|
|
241
|
+
},
|
|
242
|
+
isReplying: !0
|
|
243
|
+
}, t);
|
|
244
|
+
this.event.emit("message:content", g), this.pushMessage(g), this.event.emit("answer:finish", g);
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
let n = this.messages[this.messages.length - 1];
|
|
248
|
+
n && !n.messageId ? (n = h(h({}, n), t), this.messages[this.messages.length - 1] = n, this.event.emit("message:content", n), this.event.emit("message:updated", n)) : (this.pushMessage(h({
|
|
228
249
|
messageId: "",
|
|
229
250
|
content: "",
|
|
230
251
|
role: C.Robot,
|
|
@@ -238,28 +259,10 @@ class re extends z {
|
|
|
238
259
|
isAgree: 0
|
|
239
260
|
},
|
|
240
261
|
isReplying: !0
|
|
241
|
-
},
|
|
242
|
-
this.event.emit("message:content", l), this.pushMessage(l), this.event.emit("answer:finish", l);
|
|
262
|
+
}, t)), this.event.emit("message:content", t));
|
|
243
263
|
}
|
|
244
|
-
} else {
|
|
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({
|
|
247
|
-
messageId: "",
|
|
248
|
-
content: "",
|
|
249
|
-
role: C.Robot,
|
|
250
|
-
sessionId: this.id,
|
|
251
|
-
reference: [],
|
|
252
|
-
searchReference: [],
|
|
253
|
-
isLoading: !1,
|
|
254
|
-
messageTime: "",
|
|
255
|
-
actions: [],
|
|
256
|
-
comment: {
|
|
257
|
-
isAgree: 0
|
|
258
|
-
},
|
|
259
|
-
isReplying: !0
|
|
260
|
-
}, s.data)), this.event.emit("message:content", s.data));
|
|
261
264
|
}
|
|
262
|
-
}),
|
|
265
|
+
}), o.on("reference", (s) => v(this, null, function* () {
|
|
263
266
|
if (this._stopAnswer)
|
|
264
267
|
return;
|
|
265
268
|
if (!this.id) {
|
|
@@ -269,24 +272,24 @@ class re extends z {
|
|
|
269
272
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
270
273
|
// }
|
|
271
274
|
);
|
|
272
|
-
|
|
275
|
+
r.error(u), this.event.emit("error", u), this.frozen = !1;
|
|
273
276
|
return;
|
|
274
277
|
}
|
|
275
278
|
const {
|
|
276
279
|
type: t,
|
|
277
|
-
data:
|
|
278
|
-
requestId:
|
|
280
|
+
data: n,
|
|
281
|
+
requestId: g
|
|
279
282
|
} = s;
|
|
280
|
-
|
|
283
|
+
r.log("reference", {
|
|
281
284
|
type: t,
|
|
282
|
-
requestId:
|
|
283
|
-
data:
|
|
285
|
+
requestId: g,
|
|
286
|
+
data: n
|
|
284
287
|
});
|
|
285
|
-
const f = this.messages.findIndex((u) => u.messageId ===
|
|
288
|
+
const f = this.messages.findIndex((u) => u.messageId === g && u.role === C.User);
|
|
286
289
|
if (f < 0)
|
|
287
290
|
return;
|
|
288
|
-
const
|
|
289
|
-
if (
|
|
291
|
+
const m = f + 1;
|
|
292
|
+
if (m >= this.messages.length) {
|
|
290
293
|
const u = {
|
|
291
294
|
messageId: "",
|
|
292
295
|
content: "",
|
|
@@ -304,12 +307,12 @@ class re extends z {
|
|
|
304
307
|
};
|
|
305
308
|
this.pushMessage(u);
|
|
306
309
|
}
|
|
307
|
-
t === "KG" ? (this.messages[
|
|
308
|
-
reference:
|
|
309
|
-
}), yield this.fillMessageReference(this.messages[
|
|
310
|
-
searchReference:
|
|
311
|
-
}), this.event.emit("message:searchreference",
|
|
312
|
-
})), this.socket =
|
|
310
|
+
t === "KG" ? (this.messages[m] = y(h({}, this.messages[m]), {
|
|
311
|
+
reference: n.knowledgeReference
|
|
312
|
+
}), yield this.fillMessageReference(this.messages[m]), this.event.emit("message:reference", n.knowledgeReference), this.event.emit("message:updated", this.messages[m])) : t === "WEB_SEARCH" && (this.messages[m] = y(h({}, this.messages[m]), {
|
|
313
|
+
searchReference: n.knowledgeReference
|
|
314
|
+
}), this.event.emit("message:searchreference", n.searchReference), this.event.emit("message:updated", this.messages[m]));
|
|
315
|
+
})), this.socket = o, o.open();
|
|
313
316
|
}));
|
|
314
317
|
}
|
|
315
318
|
registerMcpFromClientConfig() {
|
|
@@ -318,86 +321,86 @@ class re extends z {
|
|
|
318
321
|
i("socket is undefined");
|
|
319
322
|
return;
|
|
320
323
|
}
|
|
321
|
-
this.socket.emit("add_mcp_servers", p.mcp, (
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
}),
|
|
324
|
+
this.socket.emit("add_mcp_servers", p.mcp, (c) => {
|
|
325
|
+
c.code === 200 ? (Object.keys(c.data).forEach((o) => {
|
|
326
|
+
c.data[o].code !== 200 && r.report(o, "MCP Server 注册失败。", c.data[o]);
|
|
327
|
+
}), r.log("mcp:registered", Object.keys(c.data)), e(!0)) : (r.error("MCP Server 注册失败。", c), i(c));
|
|
325
328
|
});
|
|
326
|
-
}) : (
|
|
329
|
+
}) : (r.log("mcp:skip"), Promise.resolve(!0));
|
|
327
330
|
}
|
|
328
331
|
registerLocalMcp() {
|
|
329
|
-
var
|
|
332
|
+
var c, o;
|
|
330
333
|
const e = this.socket;
|
|
331
334
|
if (!e)
|
|
332
335
|
return;
|
|
333
336
|
B(), this.stopLocalMcpUpdates || (this.stopLocalMcpUpdates = J(() => this.registerLocalMcp()));
|
|
334
337
|
const i = ++this.localMcpTicket;
|
|
335
338
|
Q({
|
|
336
|
-
arrange: (
|
|
339
|
+
arrange: (o = (c = this.application) == null ? void 0 : c.arrange) == null ? void 0 : o[0],
|
|
337
340
|
initMcp: p.mcp,
|
|
338
341
|
apiRoot: p.apiRoot,
|
|
339
342
|
token: this.requestToken,
|
|
340
343
|
socket: e,
|
|
341
344
|
isCurrent: () => i === this.localMcpTicket
|
|
342
|
-
}).catch((
|
|
343
|
-
|
|
345
|
+
}).catch((d) => {
|
|
346
|
+
r.report("local mcp", d instanceof Error ? d.message : String(d));
|
|
344
347
|
});
|
|
345
348
|
}
|
|
346
349
|
addMcpServers(e) {
|
|
347
350
|
return v(this, null, function* () {
|
|
348
|
-
return this.ready ? this.socket ? new Promise((i,
|
|
349
|
-
this.socket && this.socket.emit("add_mcp_servers", e, (
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}),
|
|
351
|
+
return this.ready ? this.socket ? new Promise((i, c) => {
|
|
352
|
+
this.socket && this.socket.emit("add_mcp_servers", e, (o) => {
|
|
353
|
+
o.code === 200 ? (Object.keys(o.data).forEach((d) => {
|
|
354
|
+
o.data[d].code !== 200 && r.report(d, "MCP Server 注册失败。", o.data[d]);
|
|
355
|
+
}), r.log("mcp:registered", Object.keys(o.data)), i(!0)) : (r.error("MCP Server 注册失败。", o), c(o));
|
|
353
356
|
});
|
|
354
|
-
}) : (
|
|
357
|
+
}) : (r.error("socket is undefined."), Promise.reject("socket is undefined")) : (r.error("session is not avalible."), Promise.reject("session is not avalible"));
|
|
355
358
|
});
|
|
356
359
|
}
|
|
357
360
|
listMcpServers() {
|
|
358
361
|
return v(this, null, function* () {
|
|
359
362
|
return this.ready ? this.socket ? new Promise((e, i) => {
|
|
360
|
-
this.socket && this.socket.emit("list_mcp_servers", (
|
|
361
|
-
|
|
363
|
+
this.socket && this.socket.emit("list_mcp_servers", (c) => {
|
|
364
|
+
c.code === 200 ? e(c.data) : i(c);
|
|
362
365
|
});
|
|
363
|
-
}) : (
|
|
366
|
+
}) : (r.error("socket is undefined."), Promise.reject("socket is undefined")) : (r.error("session is not avalible."), Promise.reject("session is not avalible"));
|
|
364
367
|
});
|
|
365
368
|
}
|
|
366
369
|
sendQuestion(e, i) {
|
|
367
370
|
return v(this, null, function* () {
|
|
368
|
-
var
|
|
371
|
+
var o;
|
|
369
372
|
this.convertKnowledgesToChatFormat(this.knowledges);
|
|
370
|
-
let
|
|
371
|
-
promptVariables: this.promptVariables.filter((
|
|
373
|
+
let c = y(h({}, e), {
|
|
374
|
+
promptVariables: this.promptVariables.filter((d) => d.type === T.User).reduce((d, s) => y(h({}, d), {
|
|
372
375
|
[s.key]: s.value
|
|
373
376
|
}), {}),
|
|
374
|
-
systemPromptVariables: this.promptVariables.filter((
|
|
377
|
+
systemPromptVariables: this.promptVariables.filter((d) => d.type === T.System).reduce((d, s) => y(h({}, d), {
|
|
375
378
|
[s.key]: s.value
|
|
376
379
|
}), {}),
|
|
377
|
-
knowledges: (
|
|
380
|
+
knowledges: (o = this.knowledges) == null ? void 0 : o.map((d) => d.id),
|
|
378
381
|
//agent支持指定文件时再放开
|
|
379
382
|
// knowledges: knowledgesForChat,
|
|
380
383
|
enableTitleSummary: this.resolveEnableTitleSummary()
|
|
381
384
|
});
|
|
382
385
|
if (!this.active) {
|
|
383
386
|
if (yield this.activate(), !this.socket) {
|
|
384
|
-
const
|
|
387
|
+
const d = new Error(
|
|
385
388
|
R.SocketConnectionFailed
|
|
386
389
|
// {
|
|
387
390
|
// cause: { code: -1, message: 'socket is null' },
|
|
388
391
|
// }
|
|
389
392
|
);
|
|
390
|
-
return
|
|
393
|
+
return r.warn("error", d), this.event.emit("error", d), !1;
|
|
391
394
|
}
|
|
392
|
-
this.emitQuestion(
|
|
395
|
+
this.emitQuestion(c);
|
|
393
396
|
return;
|
|
394
397
|
}
|
|
395
|
-
this.emitQuestion(
|
|
398
|
+
this.emitQuestion(c);
|
|
396
399
|
});
|
|
397
400
|
}
|
|
398
401
|
emitQuestion(e) {
|
|
399
402
|
var i;
|
|
400
|
-
|
|
403
|
+
r.log("question:request", Y(e)), (i = this.socket) == null || i.emit("question", e), r.log("question:sent", {
|
|
401
404
|
sessionId: this.id
|
|
402
405
|
});
|
|
403
406
|
}
|
|
@@ -405,30 +408,30 @@ class re extends z {
|
|
|
405
408
|
* 调用客户端插件:传入第三个参数 context(旧插件可忽略),结果最多回传一次,
|
|
406
409
|
* 停止回答或调用被取消后不再回传。
|
|
407
410
|
*/
|
|
408
|
-
invokeClientPlugin(e, i,
|
|
409
|
-
const
|
|
410
|
-
this.inflightCalls.add(
|
|
411
|
+
invokeClientPlugin(e, i, c, o) {
|
|
412
|
+
const d = new AbortController();
|
|
413
|
+
this.inflightCalls.add(d);
|
|
411
414
|
let s = !1;
|
|
412
|
-
const t = (...
|
|
413
|
-
s || (s = !0, this.inflightCalls.delete(
|
|
414
|
-
},
|
|
415
|
-
requestId:
|
|
415
|
+
const t = (...g) => {
|
|
416
|
+
s || (s = !0, this.inflightCalls.delete(d), !(this._stopAnswer || d.signal.aborted) && c(...g));
|
|
417
|
+
}, n = {
|
|
418
|
+
requestId: o.requestId || $(),
|
|
416
419
|
applicationId: this._applicationId,
|
|
417
420
|
sessionId: this.id,
|
|
418
|
-
pluginCode:
|
|
419
|
-
functionCode:
|
|
420
|
-
signal:
|
|
421
|
+
pluginCode: o.pluginCode,
|
|
422
|
+
functionCode: o.functionCode,
|
|
423
|
+
signal: d.signal
|
|
421
424
|
};
|
|
422
425
|
try {
|
|
423
|
-
const
|
|
424
|
-
|
|
425
|
-
|
|
426
|
+
const g = e(i, t, n);
|
|
427
|
+
g && typeof g.catch == "function" && g.catch((f) => {
|
|
428
|
+
r.error("client plugin failed", f), t({
|
|
426
429
|
code: 500,
|
|
427
430
|
message: "客户端插件执行失败。"
|
|
428
431
|
});
|
|
429
432
|
});
|
|
430
|
-
} catch (
|
|
431
|
-
|
|
433
|
+
} catch (g) {
|
|
434
|
+
r.error("client plugin failed", g), t({
|
|
432
435
|
code: 500,
|
|
433
436
|
message: "客户端插件执行失败。"
|
|
434
437
|
});
|
|
@@ -447,25 +450,25 @@ class re extends z {
|
|
|
447
450
|
if (!this.socket)
|
|
448
451
|
return;
|
|
449
452
|
const i = this.socket.io.opts.query;
|
|
450
|
-
i && (i.Authorization = e), this.socket.connected && (this.pendingTokenUpdate = !0,
|
|
453
|
+
i && (i.Authorization = e), this.socket.connected && (this.pendingTokenUpdate = !0, r.log("socket:update", this.id), this.socket.emit("update", {
|
|
451
454
|
resourceToken: e
|
|
452
|
-
}, (
|
|
453
|
-
this.pendingTokenUpdate && this.handleResourceTokenUpdateResult(
|
|
455
|
+
}, (c) => {
|
|
456
|
+
this.pendingTokenUpdate && this.handleResourceTokenUpdateResult(c);
|
|
454
457
|
}));
|
|
455
458
|
}
|
|
456
459
|
handleResourceTokenUpdateResult(e) {
|
|
457
460
|
if (this.pendingTokenUpdate = !1, !e || e.code === 200) {
|
|
458
|
-
|
|
461
|
+
r.log("socket:update:ok", this.id);
|
|
459
462
|
return;
|
|
460
463
|
}
|
|
461
464
|
const i = new Error(R.SocketConnectionFailed, {
|
|
462
465
|
cause: e
|
|
463
466
|
});
|
|
464
|
-
|
|
467
|
+
r.error("socket:update:failed", e), this.event.emit("error", i);
|
|
465
468
|
}
|
|
466
469
|
stopAnswer() {
|
|
467
470
|
var e;
|
|
468
|
-
|
|
471
|
+
r.log("stopAnswer", this.id), this.abortClientPluginCalls(), (e = this.socket) == null || e.emit("stop_answer");
|
|
469
472
|
}
|
|
470
473
|
inactivate(e = !1) {
|
|
471
474
|
return !e && this.frozen && !!this.socket || this.abortClientPluginCalls(), super.inactivate(e);
|
package/dist/es/PayloadTypes.mjs
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
var
|
|
1
|
+
var i = /* @__PURE__ */ ((n) => (n[n.Normal = 0] = "Normal", n[n.Debug = 1] = "Debug", n))(i || {}), a = /* @__PURE__ */ ((n) => (n[n.InsufficientBalance = 11937] = "InsufficientBalance", n[n.ConversionFailed = 11938] = "ConversionFailed", n[n.QuotaCheckFailed = 11939] = "QuotaCheckFailed", n))(a || {});
|
|
2
|
+
const t = {
|
|
3
|
+
11937: "余额不足",
|
|
4
|
+
11938: "用量换算失败",
|
|
5
|
+
11939: "额度校验失败"
|
|
6
|
+
};
|
|
7
|
+
function u(n) {
|
|
8
|
+
return n === 11937 || n === 11938 || n === 11939;
|
|
9
|
+
}
|
|
2
10
|
export {
|
|
3
|
-
|
|
11
|
+
t as USAGE_ERROR_DEFAULT_MESSAGES,
|
|
12
|
+
a as UsageErrorCode,
|
|
13
|
+
i as UserMode,
|
|
14
|
+
u as isUsageErrorCode
|
|
4
15
|
};
|