@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/dist/es/Session.mjs
CHANGED
|
@@ -1,44 +1,45 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var b = (
|
|
1
|
+
var O = Object.defineProperty, M = Object.defineProperties;
|
|
2
|
+
var z = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var C = Object.getOwnPropertySymbols;
|
|
4
|
+
var F = Object.prototype.hasOwnProperty, P = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var b = (f, e, t) => e in f ? O(f, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : f[e] = t, u = (f, e) => {
|
|
6
6
|
for (var t in e || (e = {}))
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var t of
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
}, y = (
|
|
13
|
-
var
|
|
14
|
-
var
|
|
15
|
-
var
|
|
7
|
+
F.call(e, t) && b(f, t, e[t]);
|
|
8
|
+
if (C)
|
|
9
|
+
for (var t of C(e))
|
|
10
|
+
P.call(e, t) && b(f, t, e[t]);
|
|
11
|
+
return f;
|
|
12
|
+
}, y = (f, e) => M(f, z(e));
|
|
13
|
+
var m = (f, e, t) => (b(f, typeof e != "symbol" ? e + "" : e, t), t);
|
|
14
|
+
var l = (f, e, t) => new Promise((i, s) => {
|
|
15
|
+
var a = (n) => {
|
|
16
16
|
try {
|
|
17
|
-
|
|
18
|
-
} catch (
|
|
19
|
-
o
|
|
17
|
+
r(t.next(n));
|
|
18
|
+
} catch (o) {
|
|
19
|
+
s(o);
|
|
20
20
|
}
|
|
21
|
-
},
|
|
21
|
+
}, c = (n) => {
|
|
22
22
|
try {
|
|
23
|
-
|
|
24
|
-
} catch (
|
|
25
|
-
o
|
|
23
|
+
r(t.throw(n));
|
|
24
|
+
} catch (o) {
|
|
25
|
+
s(o);
|
|
26
26
|
}
|
|
27
|
-
},
|
|
28
|
-
|
|
27
|
+
}, r = (n) => n.done ? i(n.value) : Promise.resolve(n.value).then(a, c);
|
|
28
|
+
r((t = t.apply(f, e)).next());
|
|
29
29
|
});
|
|
30
30
|
import { v4 as Q } from "uuid";
|
|
31
|
-
import { parseJWT as
|
|
32
|
-
import { ApplicationTypes as
|
|
33
|
-
import
|
|
34
|
-
import { configuration as
|
|
35
|
-
import
|
|
31
|
+
import { parseJWT as q, resolveRelatedQuestionContent as N, removeNewlines as U, removeLeadingAndTrailingBackslashes as B } from "./utils.mjs";
|
|
32
|
+
import { ApplicationTypes as R, PromptStatus as H, PromptVariableType as L, ChatMessageRole as A } from "@glodon-aiot/apis";
|
|
33
|
+
import j from "./Logger.mjs";
|
|
34
|
+
import { configuration as S } from "./Config.mjs";
|
|
35
|
+
import G from "./EventBus.mjs";
|
|
36
|
+
import { UsageErrorCode as E, USAGE_ERROR_DEFAULT_MESSAGES as T } from "./PayloadTypes.mjs";
|
|
36
37
|
import { getCVForceService as K } from "./api/index.mjs";
|
|
37
38
|
import Y from "dayjs";
|
|
38
|
-
import { uploadSessionFile as
|
|
39
|
-
const
|
|
40
|
-
var
|
|
41
|
-
class
|
|
39
|
+
import { uploadSessionFile as J } from "./sessionUpload.mjs";
|
|
40
|
+
const V = 100, h = new j("[Session] ");
|
|
41
|
+
var W = /* @__PURE__ */ ((f) => (f.DataRequestFailed = "data request error", f.SocketConnectionFailed = "socket error", f.IdUndefined = "session id is not defined", f.SendingEmptyContent = "failed at sending empty content", f.Frozen = "session is frozen", f.UserModeError = "failed at setting socket user mode", f.Inactive = "session is inactive", f.UsageError = "usage error", f))(W || {});
|
|
42
|
+
class re {
|
|
42
43
|
/**
|
|
43
44
|
* Session 类的构造函数,用于初始化会话实例。
|
|
44
45
|
* @param token - 用户的身份验证令牌,用于解析应用 ID。
|
|
@@ -48,83 +49,83 @@ class ne {
|
|
|
48
49
|
* @param _application - 应用信息,可选参数。
|
|
49
50
|
* @param client - Bot 客户端实例,可选参数。
|
|
50
51
|
*/
|
|
51
|
-
constructor(e, t = "", i,
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
constructor(e, t = "", i, s, a, c) {
|
|
53
|
+
m(this, "_id");
|
|
54
|
+
m(this, "_frozen", !1);
|
|
54
55
|
//是否可发送新问题
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
m(this, "inactivating", !1);
|
|
57
|
+
m(this, "_ready", !1);
|
|
57
58
|
// ready = false;
|
|
58
|
-
|
|
59
|
+
m(this, "event", new G());
|
|
59
60
|
//从token解析出来的用户信息
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this.token = e, this.lazyload =
|
|
61
|
+
m(this, "_applicationId");
|
|
62
|
+
m(this, "_application", null);
|
|
63
|
+
m(this, "data", null);
|
|
64
|
+
m(this, "client", null);
|
|
65
|
+
m(this, "messages", []);
|
|
66
|
+
m(this, "_loadingMessages", !1);
|
|
67
|
+
m(this, "_loadingMessagesSize", 10);
|
|
68
|
+
m(this, "cvforceApi");
|
|
69
|
+
m(this, "socket", null);
|
|
70
|
+
m(this, "_promptVariables", []);
|
|
71
|
+
m(this, "_sessionActive", !1);
|
|
72
|
+
m(this, "activing", !1);
|
|
73
|
+
m(this, "_stopAnswer", !1);
|
|
74
|
+
m(this, "_dataLoading", !1);
|
|
75
|
+
m(this, "_dataLoaded", !1);
|
|
76
|
+
m(this, "_mcpServers", null);
|
|
77
|
+
this.token = e, this.lazyload = s, this.client = c || null, this._applicationId = q(e).rid || S.applicationId, this.cvforceApi = this.getService(), a && (this._application = a), this.event.emit("init", {
|
|
77
78
|
applicationId: this._applicationId,
|
|
78
79
|
sessionId: t
|
|
79
80
|
}), this.loadApplicationInfo().then(() => {
|
|
80
|
-
var
|
|
81
|
+
var r;
|
|
81
82
|
if (t)
|
|
82
|
-
this._id = t, this.event.emit("data:loading"), i ? (this.data = i,
|
|
83
|
-
this.data =
|
|
84
|
-
})).then(() =>
|
|
83
|
+
this._id = t, this.event.emit("data:loading"), i ? (this.data = i, s || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((n) => l(this, null, function* () {
|
|
84
|
+
this.data = n, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
85
|
+
})).then(() => l(this, null, function* () {
|
|
85
86
|
this.messages = [], yield this.fetchMessages(), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data);
|
|
86
|
-
})))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((
|
|
87
|
-
this.data =
|
|
88
|
-
})).then(() =>
|
|
87
|
+
})))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((n) => l(this, null, function* () {
|
|
88
|
+
this.data = n, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
89
|
+
})).then(() => l(this, null, function* () {
|
|
89
90
|
this.messages = [], yield this.fetchMessages(), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), this._dataLoading = !1, this._dataLoaded = !0;
|
|
90
91
|
})));
|
|
91
92
|
else {
|
|
92
|
-
const
|
|
93
|
+
const n = (i == null ? void 0 : i.name) || "新建会话";
|
|
93
94
|
this.event.emit("data:creating", {
|
|
94
95
|
applicationId: this._applicationId,
|
|
95
|
-
name:
|
|
96
|
+
name: n
|
|
96
97
|
}), this.event.emit("data:loading"), this.cvforceApi.postSession(this.applicationId, {
|
|
97
|
-
name:
|
|
98
|
+
name: n,
|
|
98
99
|
// @ts-ignore
|
|
99
100
|
netOpen: i == null ? void 0 : i.netOpen,
|
|
100
|
-
knowledgeIdList: (
|
|
101
|
+
knowledgeIdList: (r = i == null ? void 0 : i.knowledges) == null ? void 0 : r.map((o) => o.id),
|
|
101
102
|
applicationId: this._applicationId
|
|
102
|
-
}).then((
|
|
103
|
-
var
|
|
104
|
-
this._id =
|
|
103
|
+
}).then((o) => l(this, null, function* () {
|
|
104
|
+
var p;
|
|
105
|
+
this._id = o.id, this.data = y(u({}, o), {
|
|
105
106
|
knowledges: i == null ? void 0 : i.knowledges
|
|
106
107
|
}), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id);
|
|
107
108
|
try {
|
|
108
|
-
yield (() =>
|
|
109
|
+
yield (() => l(this, null, function* () {
|
|
109
110
|
if (i != null && i.knowledges)
|
|
110
111
|
return this.setKnowledges((i == null ? void 0 : i.knowledges) || void 0);
|
|
111
112
|
}))();
|
|
112
|
-
} catch (
|
|
113
|
-
|
|
113
|
+
} catch (g) {
|
|
114
|
+
h.error(g);
|
|
114
115
|
}
|
|
115
116
|
yield (() => {
|
|
116
117
|
if (i != null && i.promptVariables) {
|
|
117
|
-
const
|
|
118
|
-
var
|
|
119
|
-
const w = ((
|
|
120
|
-
return y(u({},
|
|
118
|
+
const g = this._promptVariables.map((v) => {
|
|
119
|
+
var k, I;
|
|
120
|
+
const w = ((I = (k = i.promptVariables) == null ? void 0 : k.find((d) => d.key === v.key && d.type === v.type)) == null ? void 0 : I.value) || "";
|
|
121
|
+
return y(u({}, v), {
|
|
121
122
|
value: w
|
|
122
123
|
});
|
|
123
|
-
}).sort((
|
|
124
|
-
return this.setPromptVariables(
|
|
124
|
+
}).sort((v, w) => v.key.localeCompare(w.key));
|
|
125
|
+
return this.setPromptVariables(g || []);
|
|
125
126
|
} else
|
|
126
127
|
return;
|
|
127
|
-
})(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((
|
|
128
|
+
})(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((p = this.application) == null ? void 0 : p.type) !== R.Agent && (this._ready = !0, this.event.emit("ready"));
|
|
128
129
|
}));
|
|
129
130
|
}
|
|
130
131
|
});
|
|
@@ -134,10 +135,10 @@ class ne {
|
|
|
134
135
|
return this._sessionActive;
|
|
135
136
|
}
|
|
136
137
|
fetchData() {
|
|
137
|
-
return
|
|
138
|
+
return l(this, null, function* () {
|
|
138
139
|
if (!this.data)
|
|
139
140
|
throw Error("session data is not loaded");
|
|
140
|
-
return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) =>
|
|
141
|
+
return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) => l(this, null, function* () {
|
|
141
142
|
return this.data = e, this.event.emit("data:updated", this.data), e;
|
|
142
143
|
}));
|
|
143
144
|
});
|
|
@@ -177,17 +178,17 @@ class ne {
|
|
|
177
178
|
return this.data.knowledges || void 0;
|
|
178
179
|
}
|
|
179
180
|
setKnowledges(e) {
|
|
180
|
-
return
|
|
181
|
+
return l(this, null, function* () {
|
|
181
182
|
if (!this.data)
|
|
182
183
|
throw Error("session data is not loaded");
|
|
183
|
-
if ((e || []).some((
|
|
184
|
+
if ((e || []).some((s) => Array.isArray(s.fileIds) && s.fileIds.length === 0))
|
|
184
185
|
throw Error("fileIds cannot be an empty array");
|
|
185
|
-
if ((e || []).reduce((
|
|
186
|
-
var
|
|
187
|
-
const
|
|
188
|
-
return
|
|
189
|
-
}, 0) >
|
|
190
|
-
throw Error(`knowledges and fileIds total count cannot exceed ${
|
|
186
|
+
if ((e || []).reduce((s, a) => {
|
|
187
|
+
var r, n;
|
|
188
|
+
const c = (n = (r = a.fileIds) == null ? void 0 : r.length) != null ? n : 0;
|
|
189
|
+
return s + (c > 0 ? c : 1);
|
|
190
|
+
}, 0) > V)
|
|
191
|
+
throw Error(`knowledges and fileIds total count cannot exceed ${V}`);
|
|
191
192
|
return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
|
|
192
193
|
knowledges: e
|
|
193
194
|
}), this.data.knowledges = e, this.event.emit("data:updated", this.data), this.event.emit("data:updated:knowledges", {
|
|
@@ -200,7 +201,7 @@ class ne {
|
|
|
200
201
|
throw Error("session id is not defined");
|
|
201
202
|
if (!this.data)
|
|
202
203
|
throw Error("session data is not loaded");
|
|
203
|
-
const e = this.promptVariables.filter((t) => t.status ===
|
|
204
|
+
const e = this.promptVariables.filter((t) => t.status === H.Quoted).filter((t) => !t.value);
|
|
204
205
|
if (e.length)
|
|
205
206
|
throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((t) => t.key).join(", ")}`);
|
|
206
207
|
}
|
|
@@ -208,24 +209,24 @@ class ne {
|
|
|
208
209
|
return this._promptVariables.filter((e) => !(e.type === L.User && e.key === "query") && !(e.type === L.System && e.key === "datetime"));
|
|
209
210
|
}
|
|
210
211
|
setPromptVariables(e) {
|
|
211
|
-
return
|
|
212
|
-
const t = e == null ? void 0 : e.map((
|
|
212
|
+
return l(this, null, function* () {
|
|
213
|
+
const t = e == null ? void 0 : e.map((a) => a.type ? a : y(u({}, a), {
|
|
213
214
|
type: "instruction"
|
|
214
|
-
})), i = this._promptVariables.map((
|
|
215
|
-
var
|
|
216
|
-
const
|
|
217
|
-
return y(u({},
|
|
218
|
-
value:
|
|
215
|
+
})), i = this._promptVariables.map((a) => {
|
|
216
|
+
var r;
|
|
217
|
+
const c = ((r = t == null ? void 0 : t.find((n) => n.key === a.key && n.type === a.type)) == null ? void 0 : r.value) || a.value;
|
|
218
|
+
return y(u({}, a), {
|
|
219
|
+
value: c
|
|
219
220
|
});
|
|
220
|
-
}).sort((
|
|
221
|
-
if (!(yield Promise.all(i.filter((
|
|
221
|
+
}).sort((a, c) => a.key.localeCompare(c.key));
|
|
222
|
+
if (!(yield Promise.all(i.filter((a) => !(a.type === L.User && a.key === "query") && !(a.type === L.System && a.key === "datetime")).map((a) => {
|
|
222
223
|
if (!this.data)
|
|
223
224
|
throw Error("session data is not loaded");
|
|
224
|
-
if (!(!
|
|
225
|
-
return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id,
|
|
226
|
-
value:
|
|
225
|
+
if (!(!a.id || !a.key))
|
|
226
|
+
return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id, a.id, {
|
|
227
|
+
value: a.value
|
|
227
228
|
});
|
|
228
|
-
})).then(() => !0).catch((
|
|
229
|
+
})).then(() => !0).catch((a) => !1)))
|
|
229
230
|
return !1;
|
|
230
231
|
if (!this.data)
|
|
231
232
|
throw Error("session data is not loaded");
|
|
@@ -245,14 +246,14 @@ class ne {
|
|
|
245
246
|
* @returns 如果设置成功,返回最终的网络访问开关状态;若不满足设置条件,则提前返回 undefined
|
|
246
247
|
*/
|
|
247
248
|
setNetOpen(e) {
|
|
248
|
-
return
|
|
249
|
-
var t, i,
|
|
250
|
-
if (
|
|
251
|
-
|
|
249
|
+
return l(this, null, function* () {
|
|
250
|
+
var t, i, s;
|
|
251
|
+
if (h.log("setNetOpen:", e), h.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((i = this._application) == null ? void 0 : i.type) === R.Agent) {
|
|
252
|
+
h.warn("Agent Application Sessions are disabled to setNetOpen.");
|
|
252
253
|
return;
|
|
253
254
|
}
|
|
254
|
-
if (((
|
|
255
|
-
|
|
255
|
+
if (((s = this._application) == null ? void 0 : s.arrange[0].enableNetOpen) === !1) {
|
|
256
|
+
h.warn("This Application is disabled to change netOpen.");
|
|
256
257
|
return;
|
|
257
258
|
}
|
|
258
259
|
if (!this.data)
|
|
@@ -260,7 +261,7 @@ class ne {
|
|
|
260
261
|
return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
|
|
261
262
|
netOpen: e ? 1 : 2
|
|
262
263
|
/* disable */
|
|
263
|
-
}), this.data.netOpen = e,
|
|
264
|
+
}), this.data.netOpen = e, h.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.event.emit("data:updated:netOpen", {
|
|
264
265
|
netOpen: e
|
|
265
266
|
}), e;
|
|
266
267
|
});
|
|
@@ -279,14 +280,14 @@ class ne {
|
|
|
279
280
|
updateSocketResourceToken(e) {
|
|
280
281
|
}
|
|
281
282
|
getService() {
|
|
282
|
-
return K(
|
|
283
|
+
return K(S.apiRoot, () => this.requestToken, (e, t) => {
|
|
283
284
|
const i = new Error("data request error", {
|
|
284
285
|
cause: {
|
|
285
286
|
status: e,
|
|
286
287
|
message: t
|
|
287
288
|
}
|
|
288
289
|
});
|
|
289
|
-
|
|
290
|
+
h.error(i), this.event.emit("error", i);
|
|
290
291
|
});
|
|
291
292
|
}
|
|
292
293
|
addEventListener(e, t, i = !1) {
|
|
@@ -307,85 +308,85 @@ class ne {
|
|
|
307
308
|
* @returns Promise<false|Message[]>
|
|
308
309
|
*/
|
|
309
310
|
fetchMessages() {
|
|
310
|
-
return
|
|
311
|
-
var i,
|
|
311
|
+
return l(this, arguments, function* (e = {}) {
|
|
312
|
+
var i, s;
|
|
312
313
|
const t = {
|
|
313
314
|
pageSize: this._loadingMessagesSize
|
|
314
315
|
};
|
|
315
316
|
if (this._id) {
|
|
316
|
-
e = u(u({}, t), e), this.event.emit("history:loading"),
|
|
317
|
-
const
|
|
318
|
-
var
|
|
319
|
-
let
|
|
320
|
-
return typeof ((
|
|
321
|
-
actions:
|
|
322
|
-
relatedQuesion:
|
|
317
|
+
e = u(u({}, t), e), this.event.emit("history:loading"), h.log("history:loading"), (e.endTime || e.startTime) && (this.event.emit("history:moreloading", e), h.log("history:moreloading", e)), this._loadingMessages = !0;
|
|
318
|
+
const a = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), c = a.map((n, o) => {
|
|
319
|
+
var v, w;
|
|
320
|
+
let p = [], g;
|
|
321
|
+
return typeof ((v = n.agentHistoryList) == null ? void 0 : v.agentList) == "string" && (p = JSON.parse(n.agentHistoryList.agentList).reduce((I, d) => (I[d.data.step - 1] || (I[d.data.step - 1] = []), I[d.data.step - 1].push(d), I), [])), o === a.length - 1 && (g = (w = this.data) == null ? void 0 : w.lastRelatedQuestion), y(u({}, n), {
|
|
322
|
+
actions: p,
|
|
323
|
+
relatedQuesion: g,
|
|
323
324
|
fromHistory: !0
|
|
324
325
|
});
|
|
325
326
|
});
|
|
326
327
|
if (!e.endTime && !e.startTime) {
|
|
327
|
-
const
|
|
328
|
-
|
|
328
|
+
const n = this.messages[this.messages.length - 1];
|
|
329
|
+
n && !n.relatedQuesion && (n.relatedQuesion = (i = this.data) == null ? void 0 : i.lastRelatedQuestion), this.messages = [...c, ...this.messages];
|
|
329
330
|
} else if (e.endTime && !e.startTime) {
|
|
330
|
-
const
|
|
331
|
-
this.messages.splice(0,
|
|
331
|
+
const n = this.messages.findIndex((o) => o.ts === e.endTime);
|
|
332
|
+
this.messages.splice(0, n + 1), this.messages = [...c, ...this.messages];
|
|
332
333
|
} else if (!e.endTime && e.startTime) {
|
|
333
|
-
const
|
|
334
|
-
this.messages.splice(
|
|
334
|
+
const n = this.messages.findLastIndex((o) => o.ts === e.endTime);
|
|
335
|
+
this.messages.splice(n, this.messages.length - n - 1), this.messages = [...this.messages, ...c];
|
|
335
336
|
} else {
|
|
336
|
-
const
|
|
337
|
-
this.messages.splice(
|
|
337
|
+
const n = this.messages.findIndex((p) => p.ts === e.endTime), o = this.messages.findLastIndex((p) => p.ts === e.endTime);
|
|
338
|
+
this.messages.splice(n, o - n + 1, ...c);
|
|
338
339
|
}
|
|
339
|
-
const
|
|
340
|
-
return
|
|
340
|
+
const r = this.messages[this.messages.length - 1];
|
|
341
|
+
return r && !r.relatedQuesion && (r.relatedQuesion = (s = this.data) == null ? void 0 : s.lastRelatedQuestion), this.event.emit("history:moreloaded", c), h.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, c;
|
|
341
342
|
} else {
|
|
342
|
-
const
|
|
343
|
+
const a = new Error(
|
|
343
344
|
"session id is not defined"
|
|
344
345
|
/* IdUndefined */
|
|
345
346
|
// {
|
|
346
347
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
347
348
|
// }
|
|
348
349
|
);
|
|
349
|
-
return
|
|
350
|
+
return h.error(a), this.event.emit("error", a), !1;
|
|
350
351
|
}
|
|
351
352
|
});
|
|
352
353
|
}
|
|
353
354
|
rename(e) {
|
|
354
|
-
return
|
|
355
|
+
return l(this, null, function* () {
|
|
355
356
|
if (!this._id)
|
|
356
357
|
throw Error("session is not created yet");
|
|
357
358
|
if (e.trim() === "")
|
|
358
359
|
throw Error("name is empty");
|
|
359
360
|
return this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
360
361
|
name: e
|
|
361
|
-
}),
|
|
362
|
+
}), h.log("data:updated", this.messages), this.event.emit("data:updated", this.data), this.event.emit("data:updated:name", {
|
|
362
363
|
name: e
|
|
363
364
|
}), !0;
|
|
364
365
|
});
|
|
365
366
|
}
|
|
366
367
|
pushMessage(e) {
|
|
367
|
-
return
|
|
368
|
-
this.messages.push(e),
|
|
368
|
+
return l(this, null, function* () {
|
|
369
|
+
this.messages.push(e), h.log("history:add", e), this.event.emit("history:add", e), this.event.emit("message:new", e), this.event.emit("message:updated", e);
|
|
369
370
|
});
|
|
370
371
|
}
|
|
371
372
|
reloadSessionInfo() {
|
|
372
373
|
if (!this._id)
|
|
373
374
|
throw Error("session is not created yet");
|
|
374
|
-
return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) =>
|
|
375
|
+
return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => l(this, null, function* () {
|
|
375
376
|
if (this.data = e, !this._id)
|
|
376
377
|
throw Error("session is not created yet");
|
|
377
|
-
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id),
|
|
378
|
-
})).then(() =>
|
|
378
|
+
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), h.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.data;
|
|
379
|
+
})).then(() => l(this, null, function* () {
|
|
379
380
|
return this.messages = [], yield this.fetchMessages(), this._dataLoading = !1, this._dataLoaded = !0, this.event.emit("data:loaded", this.data), this;
|
|
380
381
|
}));
|
|
381
382
|
}
|
|
382
383
|
reload() {
|
|
383
|
-
return
|
|
384
|
+
return l(this, null, function* () {
|
|
384
385
|
return yield this.reloadSessionInfo(), this.socket ? (this.inactivate(!0), this.activate()) : (this.inactivate(!0), this.activate());
|
|
385
386
|
});
|
|
386
387
|
}
|
|
387
388
|
delete() {
|
|
388
|
-
return
|
|
389
|
+
return l(this, null, function* () {
|
|
389
390
|
if (!this.id) {
|
|
390
391
|
const e = new Error(
|
|
391
392
|
"session id is not defined"
|
|
@@ -394,13 +395,13 @@ class ne {
|
|
|
394
395
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
395
396
|
// }
|
|
396
397
|
);
|
|
397
|
-
return
|
|
398
|
+
return h.error(e), this.event.emit("error", e), !1;
|
|
398
399
|
}
|
|
399
400
|
return this.event.emit("data:deleting"), yield this.cvforceApi.delSessionsId(this.applicationId, this.id), this.event.emit("data:deleted"), this.destroy();
|
|
400
401
|
});
|
|
401
402
|
}
|
|
402
403
|
activate() {
|
|
403
|
-
return
|
|
404
|
+
return l(this, null, function* () {
|
|
404
405
|
var t;
|
|
405
406
|
if (this.activing)
|
|
406
407
|
return new Promise((i) => {
|
|
@@ -410,22 +411,22 @@ class ne {
|
|
|
410
411
|
});
|
|
411
412
|
if (!this._id)
|
|
412
413
|
throw Error("session is not avalible. id is undefined");
|
|
413
|
-
|
|
414
|
+
h.log("activate", this._id), this.activing = !0;
|
|
414
415
|
const e = this._id;
|
|
415
|
-
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((i) =>
|
|
416
|
+
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((i) => l(this, null, function* () {
|
|
416
417
|
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e), i;
|
|
417
|
-
})), this.messages.length || (this.messages = [], yield this.fetchMessages()), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), this._dataLoaded = !0, this._dataLoading = !1, this.inactivating = !1, (t = this.socket) != null && t.connected || (this.frozen = !0), yield this.prepareConnection(), this.event.emit("activated", this.socket),
|
|
418
|
+
})), this.messages.length || (this.messages = [], yield this.fetchMessages()), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), this._dataLoaded = !0, this._dataLoading = !1, this.inactivating = !1, (t = this.socket) != null && t.connected || (this.frozen = !0), yield this.prepareConnection(), this.event.emit("activated", this.socket), h.log("activated", this._id), this._sessionActive = !0, this.activing = !1, this;
|
|
418
419
|
});
|
|
419
420
|
}
|
|
420
421
|
inactivate(e = !1) {
|
|
421
|
-
return this.socket ? (
|
|
422
|
+
return this.socket ? (h.log("inactivate", this.id), this._frozen ? (e || (this.inactivating = !0), this.activing = !1, !0) : (this.socket.close(), this.socket = null, this.event.emit("inactivated"), this._sessionActive = !1, this.activing = !1, !0)) : (this.activing = !1, !0);
|
|
422
423
|
}
|
|
423
424
|
/**
|
|
424
425
|
* 会话标题摘要开关。应用详情返回布尔值时用该值,字段缺失时默认开启。
|
|
425
426
|
*/
|
|
426
427
|
resolveEnableTitleSummary() {
|
|
427
|
-
var t, i,
|
|
428
|
-
const e = (
|
|
428
|
+
var t, i, s;
|
|
429
|
+
const e = (s = (i = (t = this.application) == null ? void 0 : t.arrange) == null ? void 0 : i[0]) == null ? void 0 : s.autoGenerateSessionTitle;
|
|
429
430
|
return typeof e == "boolean" ? e : !0;
|
|
430
431
|
}
|
|
431
432
|
//TODO: remove mcp server
|
|
@@ -441,236 +442,269 @@ class ne {
|
|
|
441
442
|
* 图片地址放进 send 的 imageContent。
|
|
442
443
|
*/
|
|
443
444
|
upload(e) {
|
|
444
|
-
return
|
|
445
|
+
return l(this, null, function* () {
|
|
445
446
|
if (!this._id)
|
|
446
447
|
throw Error("session is not avalible. id is undefined");
|
|
447
|
-
return
|
|
448
|
+
return J(this.cvforceApi, this._id, e);
|
|
448
449
|
});
|
|
449
450
|
}
|
|
450
451
|
send(i) {
|
|
451
|
-
return
|
|
452
|
-
stream:
|
|
452
|
+
return l(this, arguments, function* (e, t = {
|
|
453
|
+
stream: S.stream
|
|
453
454
|
}) {
|
|
454
|
-
var
|
|
455
|
+
var k, I;
|
|
455
456
|
try {
|
|
456
457
|
this.validateConfigs();
|
|
457
|
-
} catch (
|
|
458
|
-
|
|
458
|
+
} catch (d) {
|
|
459
|
+
h.report(d);
|
|
459
460
|
}
|
|
460
461
|
const {
|
|
461
|
-
stream:
|
|
462
|
-
referenceFirst:
|
|
463
|
-
} = t,
|
|
464
|
-
if (!
|
|
465
|
-
const
|
|
462
|
+
stream: s = S.stream,
|
|
463
|
+
referenceFirst: a = !1
|
|
464
|
+
} = t, c = e.uuid || Q(), r = ((k = e.text) == null ? void 0 : k.trim()) || "", n = e.files || [], o = e.fileIds, p = e.imageContent;
|
|
465
|
+
if (!r && !n.length && !(o != null && o.length)) {
|
|
466
|
+
const d = new Error(
|
|
466
467
|
"failed at sending empty content"
|
|
467
468
|
/* SendingEmptyContent */
|
|
468
469
|
// {
|
|
469
470
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.SendingEmptyContent],
|
|
470
471
|
// }
|
|
471
472
|
);
|
|
472
|
-
return
|
|
473
|
+
return h.error(d), this.event.emit("error", d), !1;
|
|
473
474
|
}
|
|
474
475
|
if (!this._id) {
|
|
475
|
-
const
|
|
476
|
+
const d = new Error(
|
|
476
477
|
"session id is not defined"
|
|
477
478
|
/* IdUndefined */
|
|
478
479
|
// {
|
|
479
480
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
480
481
|
// }
|
|
481
482
|
);
|
|
482
|
-
return
|
|
483
|
+
return h.error(d), this.event.emit("error", d), !1;
|
|
483
484
|
}
|
|
484
485
|
if (this.frozen) {
|
|
485
|
-
const
|
|
486
|
+
const d = new Error(
|
|
486
487
|
"session is frozen"
|
|
487
488
|
/* Frozen */
|
|
488
489
|
// {
|
|
489
490
|
// cause: { code: -1, message: 'session is frozen.' },
|
|
490
491
|
// }
|
|
491
492
|
);
|
|
492
|
-
return
|
|
493
|
+
return h.error(d), this.event.emit("error", d), !1;
|
|
493
494
|
}
|
|
494
|
-
this.frozen = !0, this._stopAnswer = !1,
|
|
495
|
+
this.frozen = !0, this._stopAnswer = !1, h.log("send", {
|
|
495
496
|
sessionId: this._id,
|
|
496
|
-
stream:
|
|
497
|
-
contentLength:
|
|
498
|
-
fileCount:
|
|
499
|
-
fileIdCount: (
|
|
497
|
+
stream: s,
|
|
498
|
+
contentLength: r.length,
|
|
499
|
+
fileCount: n.length,
|
|
500
|
+
fileIdCount: (I = o == null ? void 0 : o.length) != null ? I : 0
|
|
500
501
|
});
|
|
501
|
-
const
|
|
502
|
+
const g = {
|
|
502
503
|
sessionId: this.id,
|
|
503
504
|
role: 2,
|
|
504
|
-
content:
|
|
505
|
-
messageId:
|
|
505
|
+
content: r,
|
|
506
|
+
messageId: c,
|
|
506
507
|
reference: [],
|
|
507
508
|
searchReference: [],
|
|
508
509
|
messageTime: "",
|
|
509
|
-
files:
|
|
510
|
-
imageContent:
|
|
511
|
-
},
|
|
512
|
-
content:
|
|
513
|
-
stream:
|
|
514
|
-
requestId:
|
|
515
|
-
fileIds:
|
|
516
|
-
referenceFirst:
|
|
517
|
-
imageContent:
|
|
510
|
+
files: n,
|
|
511
|
+
imageContent: p
|
|
512
|
+
}, v = {
|
|
513
|
+
content: r,
|
|
514
|
+
stream: s,
|
|
515
|
+
requestId: c,
|
|
516
|
+
fileIds: o,
|
|
517
|
+
referenceFirst: a,
|
|
518
|
+
imageContent: p
|
|
518
519
|
};
|
|
519
|
-
this.pushMessage(
|
|
520
|
-
const w = (
|
|
521
|
-
if (
|
|
520
|
+
this.pushMessage(g), this.event.emit("question:content", r);
|
|
521
|
+
const w = (d) => l(this, null, function* () {
|
|
522
|
+
if (d.role === A.Robot) {
|
|
522
523
|
this.removeEventListener("answer:finish", w);
|
|
523
524
|
try {
|
|
524
|
-
|
|
525
|
-
finialMessage:
|
|
525
|
+
if (this._id && d.content && d.index && d.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
526
|
+
finialMessage: d.content
|
|
526
527
|
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
527
|
-
finialMessage:
|
|
528
|
-
})), yield this.fillMessageReference(
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
default:
|
|
539
|
-
r.content = r.message || "未知错误", r.isLoading = !1, r.index = -1, this.event.emit("message:error", r);
|
|
528
|
+
finialMessage: d.content
|
|
529
|
+
})), yield this.fillMessageReference(d), this.finalizeAnswerMessage(d), this.getRelatedQuesion(d), d.isLoading = !1, this.isEmptyQuotaRejection(d)) {
|
|
530
|
+
const _ = this.messages[this.messages.length - 1];
|
|
531
|
+
_ && _.role === A.Robot && !_.content && !(_.actions && _.actions.length > 0) && this.messages.pop(), this.event.emit("message:updated", d);
|
|
532
|
+
} else {
|
|
533
|
+
let _ = this.messages[this.messages.length - 1];
|
|
534
|
+
if (_ && _.role === A.Robot) {
|
|
535
|
+
const x = u(u({}, _), d);
|
|
536
|
+
this.messages[this.messages.length - 1] = x, this.event.emit("message:updated", x);
|
|
537
|
+
} else
|
|
538
|
+
this.pushMessage(d);
|
|
540
539
|
}
|
|
541
|
-
this.getRelatedQuesion(r), r.isLoading = !1;
|
|
542
|
-
let k = this.messages[this.messages.length - 1];
|
|
543
|
-
if (k && k.role === S.Robot) {
|
|
544
|
-
const E = u(u({}, k), r);
|
|
545
|
-
this.messages[this.messages.length - 1] = E, this.event.emit("message:updated", E);
|
|
546
|
-
} else
|
|
547
|
-
this.pushMessage(r);
|
|
548
540
|
} finally {
|
|
549
541
|
this.frozen = !1;
|
|
550
542
|
}
|
|
551
543
|
}
|
|
552
544
|
});
|
|
553
|
-
return this.addEventListener("answer:finish", w), this.sendQuestion(
|
|
554
|
-
stream:
|
|
555
|
-
referenceFirst:
|
|
556
|
-
})),
|
|
545
|
+
return this.addEventListener("answer:finish", w), this.sendQuestion(v, y(u({}, t), {
|
|
546
|
+
stream: s,
|
|
547
|
+
referenceFirst: a
|
|
548
|
+
})), g;
|
|
557
549
|
});
|
|
558
550
|
}
|
|
551
|
+
isEmptyQuotaRejection(e) {
|
|
552
|
+
return (e.code === E.InsufficientBalance || e.code === E.QuotaCheckFailed) && !e.content && !(e.actions && e.actions.length > 0);
|
|
553
|
+
}
|
|
554
|
+
finalizeAnswerMessage(e) {
|
|
555
|
+
switch (e.code) {
|
|
556
|
+
case 11915:
|
|
557
|
+
e.content = "请您换个问题问我吧~", e.index = -1, e.isSensitive = !0, e.isLoading = !1, e.sessionId = this._id || "", this.event.emit("message:error", e);
|
|
558
|
+
break;
|
|
559
|
+
case 11916:
|
|
560
|
+
e.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", e.index = -1, e.isSensitive = !0, e.isLoading = !1, e.sessionId = this._id || "", this.event.emit("message:error", e);
|
|
561
|
+
break;
|
|
562
|
+
case E.ConversionFailed: {
|
|
563
|
+
e.index = -1, e.isLoading = !1, e.sessionId = e.sessionId || this._id || "", this.event.emit("message:error", e);
|
|
564
|
+
const t = {
|
|
565
|
+
code: E.ConversionFailed,
|
|
566
|
+
message: e.message || T[E.ConversionFailed],
|
|
567
|
+
usageExtension: e.usageExtension,
|
|
568
|
+
messageId: e.messageId || void 0,
|
|
569
|
+
sessionId: this._id || e.sessionId || void 0
|
|
570
|
+
}, i = new Error("usage error", {
|
|
571
|
+
cause: t
|
|
572
|
+
});
|
|
573
|
+
h.error("usage error", t), this.event.emit("error", i);
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
case E.InsufficientBalance:
|
|
577
|
+
case E.QuotaCheckFailed: {
|
|
578
|
+
e.index = -1, e.isLoading = !1, e.sessionId = e.sessionId || this._id || "", this.event.emit("message:error", e);
|
|
579
|
+
const t = e.code, i = {
|
|
580
|
+
code: t,
|
|
581
|
+
message: e.message || T[t],
|
|
582
|
+
messageId: e.messageId || void 0,
|
|
583
|
+
sessionId: this._id || e.sessionId || void 0
|
|
584
|
+
}, s = new Error("usage error", {
|
|
585
|
+
cause: i
|
|
586
|
+
});
|
|
587
|
+
h.error("usage error", i), this.event.emit("error", s);
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
case 200:
|
|
591
|
+
e.index = -1, e.isLoading = !1;
|
|
592
|
+
break;
|
|
593
|
+
default:
|
|
594
|
+
e.content = e.message || "未知错误", e.isLoading = !1, e.index = -1, this.event.emit("message:error", e);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
559
597
|
currentUserQuestion() {
|
|
560
598
|
for (let e = this.messages.length - 1; e >= 0; e -= 1) {
|
|
561
599
|
const t = this.messages[e];
|
|
562
|
-
if (t.role ===
|
|
600
|
+
if (t.role === A.User)
|
|
563
601
|
return t.content || "";
|
|
564
602
|
}
|
|
565
603
|
return "";
|
|
566
604
|
}
|
|
567
605
|
getRelatedQuesion(e) {
|
|
568
|
-
return
|
|
569
|
-
var
|
|
570
|
-
const t = (
|
|
606
|
+
return l(this, null, function* () {
|
|
607
|
+
var r, n;
|
|
608
|
+
const t = (r = this.application) == null ? void 0 : r.arrange[0];
|
|
571
609
|
if (!e.content || e.isSensitive || e.code !== 200)
|
|
572
|
-
return
|
|
610
|
+
return h.log("related:skip", {
|
|
573
611
|
code: e.code,
|
|
574
612
|
sensitive: !!e.isSensitive,
|
|
575
613
|
hasContent: !!e.content
|
|
576
614
|
}), e;
|
|
577
|
-
const i =
|
|
615
|
+
const i = N(t == null ? void 0 : t.recommendQuestionOpen, t == null ? void 0 : t.relatedQuestionApp, e.content, this.currentUserQuestion());
|
|
578
616
|
if (i === null)
|
|
579
|
-
return
|
|
617
|
+
return h.log("related:skip", {
|
|
580
618
|
recommendQuestionOpen: t == null ? void 0 : t.recommendQuestionOpen
|
|
581
619
|
}), e;
|
|
582
|
-
|
|
583
|
-
const
|
|
620
|
+
h.log("related:request", i), this.event.emit("message:relatedQuesion:loading", e);
|
|
621
|
+
const s = yield this.cvforceApi.postGeneques({
|
|
584
622
|
content: i
|
|
585
|
-
}).catch((
|
|
586
|
-
if (
|
|
587
|
-
const
|
|
588
|
-
relatedQuesion:
|
|
623
|
+
}).catch((o) => (h.error("related:failed", o), null));
|
|
624
|
+
if (h.log("related:response", s), !s) {
|
|
625
|
+
const o = [], p = y(u({}, e), {
|
|
626
|
+
relatedQuesion: o,
|
|
589
627
|
index: -3,
|
|
590
628
|
isLoading: !1
|
|
591
629
|
});
|
|
592
630
|
return this.event.emit("message:relatedQuesion:loaded", {
|
|
593
|
-
relatedQuesion:
|
|
594
|
-
}), this.event.emit("message:updated",
|
|
631
|
+
relatedQuesion: o
|
|
632
|
+
}), this.event.emit("message:updated", p), p;
|
|
595
633
|
}
|
|
596
|
-
const
|
|
597
|
-
e.relatedQuesion =
|
|
598
|
-
const
|
|
599
|
-
relatedQuesion:
|
|
634
|
+
const a = (n = s.content) == null ? void 0 : n.split("*").map(U).map(B).filter((o) => !!o);
|
|
635
|
+
e.relatedQuesion = a, e.index = -3, e.isLoading = !1;
|
|
636
|
+
const c = y(u({}, e), {
|
|
637
|
+
relatedQuesion: a,
|
|
600
638
|
index: -3,
|
|
601
639
|
isLoading: !1
|
|
602
640
|
});
|
|
603
|
-
return this.event.emit("message:relatedQuesion:loaded",
|
|
604
|
-
lastRelatedQuestion:
|
|
605
|
-
}),
|
|
641
|
+
return this.event.emit("message:relatedQuesion:loaded", c), this.event.emit("message:updated", c), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
642
|
+
lastRelatedQuestion: a
|
|
643
|
+
}), c;
|
|
606
644
|
});
|
|
607
645
|
}
|
|
608
646
|
fillMessageReference(e) {
|
|
609
|
-
return
|
|
647
|
+
return l(this, null, function* () {
|
|
610
648
|
try {
|
|
611
649
|
const t = yield Promise.all(e.reference.map((i) => this.cvforceApi.getKnowledgeIdFileId(i.knowledgeId, i.docId)));
|
|
612
|
-
e.reference = e.reference.map((i,
|
|
650
|
+
e.reference = e.reference.map((i, s) => u(u({}, i), t[s]));
|
|
613
651
|
} catch (t) {
|
|
614
|
-
|
|
652
|
+
h.error("Failed to fill message reference:", t);
|
|
615
653
|
}
|
|
616
654
|
return this.event.emit("message:updated", e), e;
|
|
617
655
|
});
|
|
618
656
|
}
|
|
619
657
|
answerAgain() {
|
|
620
|
-
return
|
|
621
|
-
stream:
|
|
658
|
+
return l(this, arguments, function* (e = {
|
|
659
|
+
stream: S.stream
|
|
622
660
|
}) {
|
|
623
|
-
var
|
|
661
|
+
var n;
|
|
624
662
|
const t = this.messages[this.messages.length - 1];
|
|
625
663
|
if (!t)
|
|
626
664
|
throw Error("message list is empty");
|
|
627
|
-
this.frozen = !0, this._stopAnswer = !1,
|
|
665
|
+
this.frozen = !0, this._stopAnswer = !1, h.log("answerAgain", {
|
|
628
666
|
sessionId: this._id,
|
|
629
|
-
stream: (
|
|
667
|
+
stream: (n = e.stream) != null ? n : S.stream
|
|
630
668
|
});
|
|
631
669
|
const {
|
|
632
|
-
stream: i =
|
|
670
|
+
stream: i = S.stream
|
|
633
671
|
} = e;
|
|
634
|
-
let
|
|
635
|
-
t.role !==
|
|
636
|
-
const
|
|
637
|
-
content:
|
|
638
|
-
fileIds:
|
|
639
|
-
requestId:
|
|
672
|
+
let s;
|
|
673
|
+
t.role !== A.User && (s = e.oldMessageId || t.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", t), this.event.emit("history:remove", t));
|
|
674
|
+
const a = this.messages[this.messages.length - 1], c = {
|
|
675
|
+
content: a.content,
|
|
676
|
+
fileIds: a.fileIds,
|
|
677
|
+
requestId: a.messageId,
|
|
640
678
|
stream: i,
|
|
641
|
-
oldMessageId:
|
|
642
|
-
},
|
|
643
|
-
if (
|
|
679
|
+
oldMessageId: s
|
|
680
|
+
}, r = (o) => l(this, null, function* () {
|
|
681
|
+
if (o.role === A.Robot && o.index === -1)
|
|
644
682
|
try {
|
|
645
|
-
|
|
646
|
-
finialMessage:
|
|
683
|
+
if (this._id && o.content && o.index && o.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
684
|
+
finialMessage: o.content
|
|
647
685
|
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
648
|
-
finialMessage:
|
|
649
|
-
})), yield this.fillMessageReference(
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
break;
|
|
659
|
-
default:
|
|
660
|
-
s.content = s.message || "未知错误", s.isLoading = !1, s.index = -1, this.event.emit("message:error", s);
|
|
686
|
+
finialMessage: o.content
|
|
687
|
+
})), yield this.fillMessageReference(o), this.finalizeAnswerMessage(o), this.getRelatedQuesion(o), this.isEmptyQuotaRejection(o)) {
|
|
688
|
+
const p = this.messages[this.messages.length - 1];
|
|
689
|
+
p && p.role === A.Robot && !p.content && !(p.actions && p.actions.length > 0) && this.messages.pop(), this.event.emit("message:updated", o);
|
|
690
|
+
} else {
|
|
691
|
+
const p = this.messages[this.messages.length - 1];
|
|
692
|
+
if (p && p.role === A.Robot) {
|
|
693
|
+
const g = u(u({}, p), o);
|
|
694
|
+
this.messages[this.messages.length - 1] = g, this.event.emit("message:updated", g);
|
|
695
|
+
}
|
|
661
696
|
}
|
|
662
|
-
this.getRelatedQuesion(s);
|
|
663
697
|
} finally {
|
|
664
|
-
this.removeEventListener("message:content",
|
|
698
|
+
this.removeEventListener("message:content", r), this.frozen = !1;
|
|
665
699
|
}
|
|
666
700
|
});
|
|
667
|
-
this.addEventListener("message:content",
|
|
701
|
+
this.addEventListener("message:content", r), this.sendQuestion(c, y(u({}, e), {
|
|
668
702
|
stream: i
|
|
669
703
|
}));
|
|
670
704
|
});
|
|
671
705
|
}
|
|
672
706
|
setTop(e) {
|
|
673
|
-
return
|
|
707
|
+
return l(this, null, function* () {
|
|
674
708
|
if (!this._id)
|
|
675
709
|
throw Error("session is not avalible. id is undefined");
|
|
676
710
|
this.data = yield this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
@@ -701,7 +735,7 @@ class ne {
|
|
|
701
735
|
// 消息时间
|
|
702
736
|
content: "",
|
|
703
737
|
// 消息内容
|
|
704
|
-
role:
|
|
738
|
+
role: A.System,
|
|
705
739
|
// 消息角色为系统消息
|
|
706
740
|
sessionId: this.id || "",
|
|
707
741
|
// 会话 ID
|
|
@@ -714,28 +748,28 @@ class ne {
|
|
|
714
748
|
if (e.index === -2)
|
|
715
749
|
return;
|
|
716
750
|
if (e.actions) {
|
|
717
|
-
const
|
|
718
|
-
if (
|
|
719
|
-
const
|
|
720
|
-
if (
|
|
751
|
+
const s = e.actions[((i = e.actions) == null ? void 0 : i.length) - 1];
|
|
752
|
+
if (s) {
|
|
753
|
+
const a = s[s.length - 1];
|
|
754
|
+
if (a && (a.type === "BEFORE_CALL" || a.type === "FUNCTION_CALL")) {
|
|
721
755
|
const {
|
|
722
|
-
pluginName:
|
|
723
|
-
pluginCode:
|
|
724
|
-
funcCode:
|
|
725
|
-
funcName:
|
|
726
|
-
} =
|
|
727
|
-
|
|
756
|
+
pluginName: c,
|
|
757
|
+
pluginCode: r,
|
|
758
|
+
funcCode: n,
|
|
759
|
+
funcName: o
|
|
760
|
+
} = a.data;
|
|
761
|
+
s.push({
|
|
728
762
|
data: {
|
|
729
|
-
pluginName:
|
|
730
|
-
pluginCode:
|
|
731
|
-
funcCode:
|
|
732
|
-
funcName:
|
|
733
|
-
step:
|
|
763
|
+
pluginName: c,
|
|
764
|
+
pluginCode: r,
|
|
765
|
+
funcCode: n,
|
|
766
|
+
funcName: o,
|
|
767
|
+
step: a.data.step + 1
|
|
734
768
|
},
|
|
735
769
|
type: "ABORT"
|
|
736
770
|
}), this.event.emit("message:action", {
|
|
737
771
|
messageId: e.messageId,
|
|
738
|
-
action:
|
|
772
|
+
action: s
|
|
739
773
|
});
|
|
740
774
|
}
|
|
741
775
|
}
|
|
@@ -751,12 +785,14 @@ class ne {
|
|
|
751
785
|
* @param targetMessage - 要更新的目标聊天消息对象。
|
|
752
786
|
*/
|
|
753
787
|
handleStreamAnswer(e, t) {
|
|
754
|
-
var
|
|
755
|
-
if (!e.data)
|
|
788
|
+
var c;
|
|
789
|
+
if (!e.data) {
|
|
790
|
+
e.code != null && e.code !== 200 && (t.code = e.code, t.message = e.message, t.index = -1);
|
|
756
791
|
return;
|
|
757
|
-
|
|
792
|
+
}
|
|
793
|
+
const i = e.data.type, s = e.data.payload;
|
|
758
794
|
t.isReplying = !0;
|
|
759
|
-
const
|
|
795
|
+
const a = () => {
|
|
760
796
|
if (!t.searchingStatus) {
|
|
761
797
|
t.searchingStatus = "start";
|
|
762
798
|
return;
|
|
@@ -766,25 +802,28 @@ class ne {
|
|
|
766
802
|
return;
|
|
767
803
|
}
|
|
768
804
|
};
|
|
769
|
-
if (i === "error" && (t.index = -1), i === "beforeSearch" &&
|
|
805
|
+
if (i === "error" && (t.index = -1), i === "beforeSearch" && a(), i === "searchOver" && a(), i === "search" && t.searchReference.push(s), i === "thinking") {
|
|
770
806
|
t.thinkingStatus = "start";
|
|
771
|
-
const
|
|
772
|
-
t.reasoningContent = (t.reasoningContent || "") + ((
|
|
807
|
+
const n = s.choices[0];
|
|
808
|
+
t.reasoningContent = (t.reasoningContent || "") + ((c = n.delta.reasoningContent) != null ? c : "");
|
|
773
809
|
}
|
|
774
810
|
if (i === "thinkingOver") {
|
|
775
811
|
t.thinkingStatus = "end";
|
|
776
|
-
const
|
|
777
|
-
t.thinkingSecs =
|
|
812
|
+
const r = s.thinkingSecs;
|
|
813
|
+
t.thinkingSecs = r;
|
|
778
814
|
}
|
|
779
815
|
if (i === "text") {
|
|
780
|
-
const
|
|
781
|
-
t.content = (t.content || "") +
|
|
816
|
+
const n = s.choices[0];
|
|
817
|
+
t.content = (t.content || "") + n.delta.content;
|
|
818
|
+
}
|
|
819
|
+
if (i === "reference" && (t.reference = s), i === "updateSession" && s && this.data && (this.data = u(u({}, this.data), s), this.event.emit("data:updated", this.data)), i === "result") {
|
|
820
|
+
const r = s == null ? void 0 : s.messageId, n = s == null ? void 0 : s.llmModel, o = s == null ? void 0 : s.messageTime;
|
|
821
|
+
r !== void 0 && (t.messageId = r), n !== void 0 && (t.llmModel = n), o !== void 0 && (t.messageTime = o), (s == null ? void 0 : s.usageExtension) != null && (t.usageExtension = s.usageExtension);
|
|
782
822
|
}
|
|
783
|
-
if (i === "
|
|
784
|
-
const
|
|
785
|
-
|
|
823
|
+
if (i === "over" && (t.index = -1), e.code != null) {
|
|
824
|
+
const r = t.code != null && t.code !== 200;
|
|
825
|
+
(e.code !== 200 || !r) && (t.code = e.code, t.message = e.message);
|
|
786
826
|
}
|
|
787
|
-
i === "over" && (t.index = -1), e.code != null && (t.code = e.code, t.message = e.message);
|
|
788
827
|
}
|
|
789
828
|
/**
|
|
790
829
|
* 异步提交评论信息
|
|
@@ -795,7 +834,7 @@ class ne {
|
|
|
795
834
|
* 获取当前会话的产出物。没有产出物时返回空数组。
|
|
796
835
|
*/
|
|
797
836
|
getArtifacts() {
|
|
798
|
-
return
|
|
837
|
+
return l(this, null, function* () {
|
|
799
838
|
var t;
|
|
800
839
|
if (!this._applicationId || !this._id)
|
|
801
840
|
throw Error("session is not avalible. id is undefined");
|
|
@@ -804,11 +843,11 @@ class ne {
|
|
|
804
843
|
});
|
|
805
844
|
}
|
|
806
845
|
postMessageComment(e) {
|
|
807
|
-
return
|
|
846
|
+
return l(this, null, function* () {
|
|
808
847
|
if (!this._applicationId || !this._id)
|
|
809
848
|
throw Error("session is not avalible. id is undefined");
|
|
810
849
|
return this.cvforceApi.postComments(this._applicationId, this._id, e).then((t) => {
|
|
811
|
-
const i = this.messages.find((
|
|
850
|
+
const i = this.messages.find((s) => s.messageId === e.messageId);
|
|
812
851
|
return i && (i.comment = e, this.event.emit("message:comment", i)), t;
|
|
813
852
|
});
|
|
814
853
|
});
|
|
@@ -836,7 +875,7 @@ class ne {
|
|
|
836
875
|
}
|
|
837
876
|
}
|
|
838
877
|
export {
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
878
|
+
V as MAX_KNOWLEDGES_AND_FILES_COUNT,
|
|
879
|
+
W as SessionErrors,
|
|
880
|
+
re as default
|
|
842
881
|
};
|