@glodon-aiot/bot-client-sdk 3.3.0-beta.13
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 +157 -0
- package/dist/agent-cli-sdk.js +33141 -0
- package/dist/agent-cli-sdk.umd.cjs +140 -0
- package/dist/es/AgentPlugin.mjs +20 -0
- package/dist/es/AgentSession.mjs +285 -0
- package/dist/es/BotClient.mjs +250 -0
- package/dist/es/Config.mjs +36 -0
- package/dist/es/DialogSession.mjs +301 -0
- package/dist/es/EventBus.mjs +64 -0
- package/dist/es/Logger.mjs +31 -0
- package/dist/es/PayloadTypes.mjs +4 -0
- package/dist/es/Session.mjs +683 -0
- package/dist/es/api/fetchEventSource/fetch.mjs +112 -0
- package/dist/es/api/fetchEventSource/parse.mjs +97 -0
- package/dist/es/api/index.mjs +41 -0
- package/dist/es/index.mjs +11 -0
- package/dist/es/utils.mjs +11 -0
- package/dist/lib/index.js +4 -0
- package/dist/src/AgentPlugin.d.ts +17 -0
- package/dist/src/AgentSession.d.ts +13 -0
- package/dist/src/BotClient.d.ts +48 -0
- package/dist/src/Config.d.ts +46 -0
- package/dist/src/DialogSession.d.ts +26 -0
- package/dist/src/EventBus.d.ts +23 -0
- package/dist/src/Logger.d.ts +12 -0
- package/dist/src/PayloadTypes.d.ts +84 -0
- package/dist/src/Session.d.ts +222 -0
- package/dist/src/__ tests __/AgentClient.spec.d.ts +1 -0
- package/dist/src/__ tests __/AgentSession.spec.d.ts +1 -0
- package/dist/src/__ tests __/DialogClient.spec.d.ts +1 -0
- package/dist/src/api/fetchEventSource/fetch.d.ts +44 -0
- package/dist/src/api/fetchEventSource/index.d.ts +1 -0
- package/dist/src/api/fetchEventSource/parse.d.ts +36 -0
- package/dist/src/api/index.d.ts +2 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/utils.d.ts +4 -0
- package/package.json +90 -0
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
var S = Object.defineProperty, E = Object.defineProperties;
|
|
2
|
+
var V = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var b = Object.getOwnPropertySymbols;
|
|
4
|
+
var x = Object.prototype.hasOwnProperty, Q = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var L = (h, e, t) => e in h ? S(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t, f = (h, e) => {
|
|
6
|
+
for (var t in e || (e = {}))
|
|
7
|
+
x.call(e, t) && L(h, t, e[t]);
|
|
8
|
+
if (b)
|
|
9
|
+
for (var t of b(e))
|
|
10
|
+
Q.call(e, t) && L(h, t, e[t]);
|
|
11
|
+
return h;
|
|
12
|
+
}, w = (h, e) => E(h, V(e));
|
|
13
|
+
var c = (h, e, t) => (L(h, typeof e != "symbol" ? e + "" : e, t), t);
|
|
14
|
+
var r = (h, e, t) => new Promise((i, n) => {
|
|
15
|
+
var o = (s) => {
|
|
16
|
+
try {
|
|
17
|
+
d(t.next(s));
|
|
18
|
+
} catch (p) {
|
|
19
|
+
n(p);
|
|
20
|
+
}
|
|
21
|
+
}, l = (s) => {
|
|
22
|
+
try {
|
|
23
|
+
d(t.throw(s));
|
|
24
|
+
} catch (p) {
|
|
25
|
+
n(p);
|
|
26
|
+
}
|
|
27
|
+
}, d = (s) => s.done ? i(s.value) : Promise.resolve(s.value).then(o, l);
|
|
28
|
+
d((t = t.apply(h, e)).next());
|
|
29
|
+
});
|
|
30
|
+
import { v4 as M } from "uuid";
|
|
31
|
+
import { parseJWT as R, removeNewlines as T, removeLeadingAndTrailingBackslashes as z } from "./utils.mjs";
|
|
32
|
+
import { PromptStatus as O, ApplicationTypes as C, ChatMessageRole as _ } from "@glodon-aiot/apis";
|
|
33
|
+
import P from "./Logger.mjs";
|
|
34
|
+
import { configuration as k } from "./Config.mjs";
|
|
35
|
+
import q from "./EventBus.mjs";
|
|
36
|
+
import { getCVForceService as F } from "./api/index.mjs";
|
|
37
|
+
import N from "dayjs";
|
|
38
|
+
const u = new P("[Session] ");
|
|
39
|
+
var B = /* @__PURE__ */ ((h) => (h.DataRequestFailed = "data request error", h.SocketConnectionFailed = "socket error", h.IdUndefined = "session id is not defined", h.SendingEmptyContent = "failed at sending empty content", h.Frozen = "session is frozen", h.UserModeError = "failed at setting socket user mode", h.Inactive = "session is inactive", h))(B || {});
|
|
40
|
+
class $ {
|
|
41
|
+
/**
|
|
42
|
+
* Session 类的构造函数,用于初始化会话实例。
|
|
43
|
+
* @param token - 用户的身份验证令牌,用于解析应用 ID。
|
|
44
|
+
* @param _id - 会话的 ID,可选参数,默认为空字符串。
|
|
45
|
+
* @param _data - 会话的初始数据,可选参数,包含提示变量、知识库等信息。
|
|
46
|
+
* @param lazyload - 是否懒加载数据,可选参数,默认为 undefined。
|
|
47
|
+
* @param _application - 应用信息,可选参数。
|
|
48
|
+
* @param client - Bot 客户端实例,可选参数。
|
|
49
|
+
*/
|
|
50
|
+
constructor(e, t = "", i, n, o, l) {
|
|
51
|
+
c(this, "_id");
|
|
52
|
+
c(this, "_frozen", !1);
|
|
53
|
+
//是否可发送新问题
|
|
54
|
+
c(this, "inactivating", !1);
|
|
55
|
+
c(this, "_ready", !1);
|
|
56
|
+
// ready = false;
|
|
57
|
+
c(this, "event", new q());
|
|
58
|
+
//从token解析出来的用户信息
|
|
59
|
+
c(this, "_applicationId");
|
|
60
|
+
c(this, "_application", null);
|
|
61
|
+
c(this, "data", null);
|
|
62
|
+
c(this, "client", null);
|
|
63
|
+
c(this, "messages", []);
|
|
64
|
+
c(this, "_loadingMessages", !1);
|
|
65
|
+
c(this, "_loadingMessagesSize", 10);
|
|
66
|
+
c(this, "cvforceApi");
|
|
67
|
+
c(this, "socket", null);
|
|
68
|
+
c(this, "_promptVariables", []);
|
|
69
|
+
c(this, "active", !1);
|
|
70
|
+
c(this, "activing", !1);
|
|
71
|
+
c(this, "_stopAnswer", !1);
|
|
72
|
+
c(this, "_dataLoading", !1);
|
|
73
|
+
c(this, "_dataLoaded", !1);
|
|
74
|
+
c(this, "_mcpServers", null);
|
|
75
|
+
this.token = e, this.lazyload = n, this.client = l || null, this._applicationId = R(e).rid || k.applicationId, this.cvforceApi = this.getService(), o && (this._application = o), this.event.emit("init", {
|
|
76
|
+
applicationId: this._applicationId,
|
|
77
|
+
sessionId: t
|
|
78
|
+
}), this.loadApplicationInfo().then(() => {
|
|
79
|
+
var d;
|
|
80
|
+
if (t)
|
|
81
|
+
this._id = t, this.event.emit("data:loading"), i ? (this.data = i, n || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((s) => r(this, null, function* () {
|
|
82
|
+
this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
83
|
+
})).then(() => r(this, null, function* () {
|
|
84
|
+
this.messages = [], yield this.fetchMessages(), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data);
|
|
85
|
+
})))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((s) => r(this, null, function* () {
|
|
86
|
+
this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
87
|
+
})).then(() => r(this, null, function* () {
|
|
88
|
+
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;
|
|
89
|
+
})));
|
|
90
|
+
else {
|
|
91
|
+
const s = (i == null ? void 0 : i.name) || "新建会话";
|
|
92
|
+
this.event.emit("data:creating", {
|
|
93
|
+
applicationId: this._applicationId,
|
|
94
|
+
name: s
|
|
95
|
+
}), this.event.emit("data:loading"), this.cvforceApi.postSession(this.applicationId, {
|
|
96
|
+
name: s,
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
netOpen: i == null ? void 0 : i.netOpen,
|
|
99
|
+
knowledgeIdList: (d = i == null ? void 0 : i.knowledges) == null ? void 0 : d.map((p) => p.id),
|
|
100
|
+
applicationId: this._applicationId
|
|
101
|
+
}).then((p) => r(this, null, function* () {
|
|
102
|
+
this._id = p.id, this.data = w(f({}, p), {
|
|
103
|
+
knowledges: i == null ? void 0 : i.knowledges
|
|
104
|
+
}), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), yield (() => r(this, null, function* () {
|
|
105
|
+
if (i != null && i.knowledges)
|
|
106
|
+
return this.setKnowledges((i == null ? void 0 : i.knowledges) || void 0);
|
|
107
|
+
}))(), yield (() => {
|
|
108
|
+
if (i != null && i.promptVariables) {
|
|
109
|
+
const g = this._promptVariables.map((m) => {
|
|
110
|
+
var a, y;
|
|
111
|
+
const v = ((y = (a = i.promptVariables) == null ? void 0 : a.find((I) => I.key === m.key)) == null ? void 0 : y.value) || "";
|
|
112
|
+
return w(f({}, m), {
|
|
113
|
+
value: v
|
|
114
|
+
});
|
|
115
|
+
}).sort((m, v) => m.key.localeCompare(v.key));
|
|
116
|
+
return this.setPromptVariables(g || []);
|
|
117
|
+
} else
|
|
118
|
+
return;
|
|
119
|
+
})(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), this._ready = !0, this.event.emit("ready");
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
fetchData() {
|
|
125
|
+
return r(this, null, function* () {
|
|
126
|
+
if (!this.data)
|
|
127
|
+
throw Error("session data is not loaded");
|
|
128
|
+
return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) => r(this, null, function* () {
|
|
129
|
+
return this.data = e, this.event.emit("data:updated", this.data), e;
|
|
130
|
+
}));
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
get id() {
|
|
134
|
+
var e;
|
|
135
|
+
return this._id || ((e = this.data) == null ? void 0 : e.id) || "";
|
|
136
|
+
}
|
|
137
|
+
get applicationId() {
|
|
138
|
+
return this._applicationId;
|
|
139
|
+
}
|
|
140
|
+
get application() {
|
|
141
|
+
return this._application;
|
|
142
|
+
}
|
|
143
|
+
set application(e) {
|
|
144
|
+
this._application = e;
|
|
145
|
+
}
|
|
146
|
+
loadApplicationInfo() {
|
|
147
|
+
if (this._application)
|
|
148
|
+
return Promise.resolve(this._application);
|
|
149
|
+
if (this._applicationId && !this._application)
|
|
150
|
+
return this.event.emit("application:data:loading"), this.cvforceApi.getApplicationId(this._applicationId).then((e) => (this._application = e, this.event.emit("application:data:loaded", e), e));
|
|
151
|
+
throw Error("application id is not defined");
|
|
152
|
+
}
|
|
153
|
+
get ready() {
|
|
154
|
+
return this._ready;
|
|
155
|
+
}
|
|
156
|
+
get frozen() {
|
|
157
|
+
return this._frozen;
|
|
158
|
+
}
|
|
159
|
+
set frozen(e) {
|
|
160
|
+
e === !1 && this.inactivating && this.socket && (this.socket.disconnect(), this.socket = null, this.event.emit("inactivated")), this._frozen = e, this.event.emit("frozen", e);
|
|
161
|
+
}
|
|
162
|
+
get knowledges() {
|
|
163
|
+
if (!this.data)
|
|
164
|
+
throw Error("session data is not loaded");
|
|
165
|
+
return this.data.knowledges || void 0;
|
|
166
|
+
}
|
|
167
|
+
setKnowledges(e) {
|
|
168
|
+
return r(this, null, function* () {
|
|
169
|
+
if (!this.data)
|
|
170
|
+
throw Error("session data is not loaded");
|
|
171
|
+
return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
|
|
172
|
+
knowledges: e
|
|
173
|
+
}), this.data.knowledges = e, this.event.emit("data:updated", this.data), this.event.emit("data:updated:knowledges", {
|
|
174
|
+
knowledges: e
|
|
175
|
+
}), e;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
validateConfigs() {
|
|
179
|
+
if (!this._id)
|
|
180
|
+
throw Error("session id is not defined");
|
|
181
|
+
if (!this.data)
|
|
182
|
+
throw Error("session data is not loaded");
|
|
183
|
+
const e = this.promptVariables.filter((t) => t.status === O.Quoted).filter((t) => !t.value);
|
|
184
|
+
if (e.length)
|
|
185
|
+
throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((t) => t.key).join(", ")}`);
|
|
186
|
+
}
|
|
187
|
+
get promptVariables() {
|
|
188
|
+
return this._promptVariables.filter((e) => e.key !== "query");
|
|
189
|
+
}
|
|
190
|
+
setPromptVariables(e) {
|
|
191
|
+
return r(this, null, function* () {
|
|
192
|
+
const t = this._promptVariables.map((i) => {
|
|
193
|
+
var o;
|
|
194
|
+
const n = ((o = e == null ? void 0 : e.find((l) => l.key === i.key)) == null ? void 0 : o.value) || i.value;
|
|
195
|
+
return w(f({}, i), {
|
|
196
|
+
value: n
|
|
197
|
+
});
|
|
198
|
+
}).sort((i, n) => i.key.localeCompare(n.key));
|
|
199
|
+
if (yield Promise.all(t.filter((i) => i.key !== "query").map((i) => {
|
|
200
|
+
if (!this.data)
|
|
201
|
+
throw Error("session data is not loaded");
|
|
202
|
+
if (!(!i.id || !i.key))
|
|
203
|
+
return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id, i.id, {
|
|
204
|
+
value: i.value
|
|
205
|
+
}).catch(console.error);
|
|
206
|
+
})), !this.data)
|
|
207
|
+
throw Error("session data is not loaded");
|
|
208
|
+
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this.data.id), this.event.emit("data:updated", this.data), this.event.emit("data:updated:promptVariables", {
|
|
209
|
+
promptVariables: this._promptVariables
|
|
210
|
+
}), this._promptVariables;
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
get netOpen() {
|
|
214
|
+
if (!this.data)
|
|
215
|
+
throw Error("session data is not loaded");
|
|
216
|
+
return this.data.netOpen || !1;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* 异步设置会话的网络访问开关状态
|
|
220
|
+
* @param v - 布尔值,表示是否开启网络访问,true 为开启,false 为关闭
|
|
221
|
+
* @returns 如果设置成功,返回最终的网络访问开关状态;若不满足设置条件,则提前返回 undefined
|
|
222
|
+
*/
|
|
223
|
+
setNetOpen(e) {
|
|
224
|
+
return r(this, null, function* () {
|
|
225
|
+
var t, i, n;
|
|
226
|
+
if (u.log("setNetOpen:", e), u.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((i = this._application) == null ? void 0 : i.type) === C.Agent) {
|
|
227
|
+
console.warn("Agent Application Sessions are disabled to setNetOpen.");
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (((n = this._application) == null ? void 0 : n.arrange[0].enableNetOpen) === !1) {
|
|
231
|
+
console.warn("This Application is disabled to change netOpen.");
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (!this.data)
|
|
235
|
+
throw Error("session data is not loaded");
|
|
236
|
+
return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
|
|
237
|
+
netOpen: e ? 1 : 2
|
|
238
|
+
/* disable */
|
|
239
|
+
}), this.data.netOpen = e, u.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.event.emit("data:updated:netOpen", {
|
|
240
|
+
netOpen: e
|
|
241
|
+
}), e;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
getService() {
|
|
245
|
+
return F(k.apiRoot, this.token, (e, t) => {
|
|
246
|
+
const i = new Error(
|
|
247
|
+
"data request error"
|
|
248
|
+
/* DataRequestFailed */
|
|
249
|
+
// {
|
|
250
|
+
// cause: { status, message },
|
|
251
|
+
// }
|
|
252
|
+
);
|
|
253
|
+
u.error(i), this.event.emit("error", i);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
addEventListener(e, t, i = !1) {
|
|
257
|
+
if (typeof t != "function")
|
|
258
|
+
throw Error("event listener is not a function");
|
|
259
|
+
if (i) {
|
|
260
|
+
this.event.once(e, t);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
this.event.on(e, t);
|
|
264
|
+
}
|
|
265
|
+
removeEventListener(e, t) {
|
|
266
|
+
this.event.off(e, t);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* 获取对话记录
|
|
270
|
+
* @param params ChatHistoryParams
|
|
271
|
+
* @returns Promise<false|Message[]>
|
|
272
|
+
*/
|
|
273
|
+
fetchMessages() {
|
|
274
|
+
return r(this, arguments, function* (e = {}) {
|
|
275
|
+
var i, n;
|
|
276
|
+
const t = {
|
|
277
|
+
pageSize: this._loadingMessagesSize
|
|
278
|
+
};
|
|
279
|
+
if (this._id) {
|
|
280
|
+
e = f(f({}, t), e), this.event.emit("history:loading"), u.log("history:loading"), (e.endTime || e.startTime) && (this.event.emit("history:moreloading", e), u.log("history:moreloading", e)), this._loadingMessages = !0;
|
|
281
|
+
const o = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), l = o.map((s, p) => {
|
|
282
|
+
var v, a;
|
|
283
|
+
let g = [], m;
|
|
284
|
+
return typeof ((v = s.agentHistoryList) == null ? void 0 : v.agentList) == "string" && (g = JSON.parse(s.agentHistoryList.agentList).reduce((I, A) => (I[A.data.step - 1] || (I[A.data.step - 1] = []), I[A.data.step - 1].push(A), I), [])), p === o.length - 1 && (m = (a = this.data) == null ? void 0 : a.lastRelatedQuestion), w(f({}, s), {
|
|
285
|
+
actions: g,
|
|
286
|
+
relatedQuesion: m
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
if (!e.endTime && !e.startTime) {
|
|
290
|
+
const s = this.messages[this.messages.length - 1];
|
|
291
|
+
s && !s.relatedQuesion && (s.relatedQuesion = (i = this.data) == null ? void 0 : i.lastRelatedQuestion), this.messages = [...l, ...this.messages];
|
|
292
|
+
} else if (e.endTime && !e.startTime) {
|
|
293
|
+
const s = this.messages.findIndex((p) => p.ts === e.endTime);
|
|
294
|
+
this.messages.splice(0, s + 1), this.messages = [...l, ...this.messages];
|
|
295
|
+
} else if (!e.endTime && e.startTime) {
|
|
296
|
+
const s = this.messages.findLastIndex((p) => p.ts === e.endTime);
|
|
297
|
+
this.messages.splice(s, this.messages.length - s - 1), this.messages = [...this.messages, ...l];
|
|
298
|
+
} else {
|
|
299
|
+
const s = this.messages.findIndex((g) => g.ts === e.endTime), p = this.messages.findLastIndex((g) => g.ts === e.endTime);
|
|
300
|
+
this.messages.splice(s, p - s + 1, ...l);
|
|
301
|
+
}
|
|
302
|
+
const d = this.messages[this.messages.length - 1];
|
|
303
|
+
return d && !d.relatedQuesion && (d.relatedQuesion = (n = this.data) == null ? void 0 : n.lastRelatedQuestion), this.event.emit("history:moreloaded", l), u.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, l;
|
|
304
|
+
} else {
|
|
305
|
+
const o = new Error(
|
|
306
|
+
"session id is not defined"
|
|
307
|
+
/* IdUndefined */
|
|
308
|
+
// {
|
|
309
|
+
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
310
|
+
// }
|
|
311
|
+
);
|
|
312
|
+
return u.error(o), this.event.emit("error", o), !1;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
rename(e) {
|
|
317
|
+
return r(this, null, function* () {
|
|
318
|
+
if (!this._id)
|
|
319
|
+
throw Error("session is not created yet");
|
|
320
|
+
if (e.trim() === "")
|
|
321
|
+
throw Error("name is empty");
|
|
322
|
+
return this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
323
|
+
name: e
|
|
324
|
+
}), u.log("data:updated", this.messages), this.event.emit("data:updated", this.data), this.event.emit("data:updated:name", {
|
|
325
|
+
name: e
|
|
326
|
+
}), !0;
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
pushMessage(e) {
|
|
330
|
+
return r(this, null, function* () {
|
|
331
|
+
this.messages.push(e), u.log("history:add", e), this.event.emit("history:add", e), this.event.emit("message:new", e), this.event.emit("message:updated", e);
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
reloadSessionInfo() {
|
|
335
|
+
if (!this._id)
|
|
336
|
+
throw Error("session is not created yet");
|
|
337
|
+
return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => r(this, null, function* () {
|
|
338
|
+
if (this.data = e, !this._id)
|
|
339
|
+
throw Error("session is not created yet");
|
|
340
|
+
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), u.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.data;
|
|
341
|
+
})).then(() => r(this, null, function* () {
|
|
342
|
+
return this.messages = [], yield this.fetchMessages(), this._dataLoading = !1, this._dataLoaded = !0, this.event.emit("data:loaded", this.data), this;
|
|
343
|
+
}));
|
|
344
|
+
}
|
|
345
|
+
reload() {
|
|
346
|
+
return r(this, null, function* () {
|
|
347
|
+
return yield this.reloadSessionInfo(), this.socket ? (this.inactivate(!0), this.activate()) : (this.inactivate(!0), this.activate());
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
delete() {
|
|
351
|
+
return r(this, null, function* () {
|
|
352
|
+
if (!this.id) {
|
|
353
|
+
const e = new Error(
|
|
354
|
+
"session id is not defined"
|
|
355
|
+
/* IdUndefined */
|
|
356
|
+
// {
|
|
357
|
+
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
358
|
+
// }
|
|
359
|
+
);
|
|
360
|
+
return u.error(e), this.event.emit("error", e), !1;
|
|
361
|
+
}
|
|
362
|
+
return this.event.emit("data:deleting"), yield this.cvforceApi.delSessionsId(this.applicationId, this.id), this.event.emit("data:deleted"), this.destroy();
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
activate() {
|
|
366
|
+
return r(this, null, function* () {
|
|
367
|
+
if (this.activing)
|
|
368
|
+
return new Promise((t) => {
|
|
369
|
+
this.addEventListener("activated", () => {
|
|
370
|
+
t(!0);
|
|
371
|
+
}, !0);
|
|
372
|
+
});
|
|
373
|
+
if (!this._id)
|
|
374
|
+
throw Error("session is not avalible. id is undefined");
|
|
375
|
+
this.activing = !0;
|
|
376
|
+
const e = this._id;
|
|
377
|
+
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((t) => r(this, null, function* () {
|
|
378
|
+
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e), t;
|
|
379
|
+
})), 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, this.frozen = !0, yield this.prepareConnection(), this.event.emit("activated", this.socket), this.active = !0, this.activing = !1, this;
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
inactivate(e = !1) {
|
|
383
|
+
return this.socket ? this._frozen ? (e || (this.inactivating = !0), this.activing = !1, !0) : (this.socket.close(), this.socket = null, this.event.emit("inactivated"), this.active = !1, this.activing = !1, !0) : (this.activing = !1, !0);
|
|
384
|
+
}
|
|
385
|
+
//TODO: remove mcp server
|
|
386
|
+
// abstract removeMcpServers(mcpServers: MCPConfig): Promise<boolean>;
|
|
387
|
+
get dataLoaded() {
|
|
388
|
+
return this._dataLoaded;
|
|
389
|
+
}
|
|
390
|
+
get messagesLoading() {
|
|
391
|
+
return this._loadingMessages;
|
|
392
|
+
}
|
|
393
|
+
send(i) {
|
|
394
|
+
return r(this, arguments, function* (e, t = {
|
|
395
|
+
stream: !1
|
|
396
|
+
}) {
|
|
397
|
+
var v;
|
|
398
|
+
try {
|
|
399
|
+
this.validateConfigs();
|
|
400
|
+
} catch (a) {
|
|
401
|
+
console.error(a);
|
|
402
|
+
}
|
|
403
|
+
const {
|
|
404
|
+
stream: n = !1,
|
|
405
|
+
referenceFirst: o = !1
|
|
406
|
+
} = t, l = e.uuid || M(), d = ((v = e.text) == null ? void 0 : v.trim()) || "", s = e.fileIds;
|
|
407
|
+
if (!d && /*!files.length &&*/
|
|
408
|
+
!(s != null && s.length)) {
|
|
409
|
+
const a = new Error(
|
|
410
|
+
"failed at sending empty content"
|
|
411
|
+
/* SendingEmptyContent */
|
|
412
|
+
// {
|
|
413
|
+
// cause: SESSION_ERROR_CAUSE[SessionErrors.SendingEmptyContent],
|
|
414
|
+
// }
|
|
415
|
+
);
|
|
416
|
+
return u.error(a), this.event.emit("error", a), !1;
|
|
417
|
+
}
|
|
418
|
+
if (!this._id) {
|
|
419
|
+
const a = new Error(
|
|
420
|
+
"session id is not defined"
|
|
421
|
+
/* IdUndefined */
|
|
422
|
+
// {
|
|
423
|
+
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
424
|
+
// }
|
|
425
|
+
);
|
|
426
|
+
return u.error(a), this.event.emit("error", a), !1;
|
|
427
|
+
}
|
|
428
|
+
if (this.frozen) {
|
|
429
|
+
const a = new Error(
|
|
430
|
+
"session is frozen"
|
|
431
|
+
/* Frozen */
|
|
432
|
+
// {
|
|
433
|
+
// cause: { code: -1, message: 'session is frozen.' },
|
|
434
|
+
// }
|
|
435
|
+
);
|
|
436
|
+
return u.error(a), this.event.emit("error", a), !1;
|
|
437
|
+
}
|
|
438
|
+
this.frozen = !0, this._stopAnswer = !1;
|
|
439
|
+
const p = {
|
|
440
|
+
sessionId: this.id,
|
|
441
|
+
role: 2,
|
|
442
|
+
content: d,
|
|
443
|
+
messageId: l,
|
|
444
|
+
reference: [],
|
|
445
|
+
searchReference: [],
|
|
446
|
+
messageTime: ""
|
|
447
|
+
}, g = {
|
|
448
|
+
content: d,
|
|
449
|
+
stream: n,
|
|
450
|
+
requestId: l,
|
|
451
|
+
fileIds: s,
|
|
452
|
+
referenceFirst: o
|
|
453
|
+
};
|
|
454
|
+
this.pushMessage(p), this.event.emit("question:content", d);
|
|
455
|
+
const m = (a) => r(this, null, function* () {
|
|
456
|
+
if (a.role !== _.Robot)
|
|
457
|
+
return;
|
|
458
|
+
switch (this.removeEventListener("answer:finish", m), this._id && a.content && a.index && a.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
459
|
+
finialMessage: a.content
|
|
460
|
+
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
461
|
+
finialMessage: a.content
|
|
462
|
+
})), yield this.fillMessageReference(a), a.code) {
|
|
463
|
+
case 11915:
|
|
464
|
+
a.content = "请您换个问题问我吧~", a.index = -1, a.isSensitive = !0, a.isLoading = !1, a.sessionId = this._id || "", this.event.emit("message:error", a);
|
|
465
|
+
break;
|
|
466
|
+
case 200:
|
|
467
|
+
a.index = -1, a.isLoading = !1;
|
|
468
|
+
break;
|
|
469
|
+
default:
|
|
470
|
+
a.content = a.message || "未知错误", a.isLoading = !1, a.index = -1, this.event.emit("message:error", a);
|
|
471
|
+
}
|
|
472
|
+
this.getRelatedQuesion(a), a.isLoading = !1;
|
|
473
|
+
let y = this.messages[this.messages.length - 1];
|
|
474
|
+
if (y && y.role === _.Robot) {
|
|
475
|
+
const I = f(f({}, y), a);
|
|
476
|
+
this.messages[this.messages.length - 1] = I, this.event.emit("message:updated", I);
|
|
477
|
+
} else
|
|
478
|
+
this.pushMessage(a);
|
|
479
|
+
this.frozen = !1;
|
|
480
|
+
});
|
|
481
|
+
return this.addEventListener("answer:finish", m), this.sendQuestion(g, w(f({}, t), {
|
|
482
|
+
stream: n,
|
|
483
|
+
referenceFirst: o
|
|
484
|
+
})), p;
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
getRelatedQuesion(e) {
|
|
488
|
+
return r(this, null, function* () {
|
|
489
|
+
var o, l;
|
|
490
|
+
if (!e.content || e.isSensitive || e.code !== 200 || !((o = this.application) != null && o.arrange[0].recommendQuestionOpen))
|
|
491
|
+
return e;
|
|
492
|
+
this.event.emit("message:relatedQuesion:loading", e);
|
|
493
|
+
const t = yield this.cvforceApi.postGeneques({
|
|
494
|
+
content: e.content
|
|
495
|
+
}).catch((d) => null);
|
|
496
|
+
if (!t) {
|
|
497
|
+
const d = [], s = w(f({}, e), {
|
|
498
|
+
relatedQuesion: d,
|
|
499
|
+
index: -3,
|
|
500
|
+
isLoading: !1
|
|
501
|
+
});
|
|
502
|
+
return this.event.emit("message:relatedQuesion:loaded", {
|
|
503
|
+
relatedQuesion: d
|
|
504
|
+
}), this.event.emit("message:updated", s), s;
|
|
505
|
+
}
|
|
506
|
+
const i = (l = t.content) == null ? void 0 : l.split("*").map(T).map(z).filter((d) => !!d);
|
|
507
|
+
e.relatedQuesion = i, e.index = -3, e.isLoading = !1;
|
|
508
|
+
const n = w(f({}, e), {
|
|
509
|
+
relatedQuesion: i,
|
|
510
|
+
index: -3,
|
|
511
|
+
isLoading: !1
|
|
512
|
+
});
|
|
513
|
+
return this.event.emit("message:relatedQuesion:loaded", n), this.event.emit("message:updated", n), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
514
|
+
lastRelatedQuestion: i
|
|
515
|
+
}), n;
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
fillMessageReference(e) {
|
|
519
|
+
return r(this, null, function* () {
|
|
520
|
+
const t = yield Promise.all(e.reference.map((i) => this.cvforceApi.getKnowledgeIdFileId(i.knowledgeId, i.docId)));
|
|
521
|
+
return e.reference = e.reference.map((i, n) => f(f({}, i), t[n])), this.event.emit("message:updated", e), e;
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
answerAgain() {
|
|
525
|
+
return r(this, arguments, function* (e = {
|
|
526
|
+
stream: !1
|
|
527
|
+
}) {
|
|
528
|
+
const t = this.messages[this.messages.length - 1];
|
|
529
|
+
if (!t)
|
|
530
|
+
throw Error("message list is empty");
|
|
531
|
+
this.frozen = !0, this._stopAnswer = !1;
|
|
532
|
+
const {
|
|
533
|
+
stream: i = !1
|
|
534
|
+
} = e;
|
|
535
|
+
let n;
|
|
536
|
+
t.role !== _.User && (n = e.oldMessageId || t.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", t), this.event.emit("history:remove", t));
|
|
537
|
+
const o = this.messages[this.messages.length - 1], l = {
|
|
538
|
+
content: o.content,
|
|
539
|
+
fileIds: o.fileIds,
|
|
540
|
+
requestId: o.messageId,
|
|
541
|
+
stream: i,
|
|
542
|
+
oldMessageId: n
|
|
543
|
+
}, d = (s) => r(this, null, function* () {
|
|
544
|
+
if (s.role === _.Robot && s.index === -1) {
|
|
545
|
+
switch (this._id && s.content && s.index && s.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
546
|
+
finialMessage: s.content
|
|
547
|
+
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
548
|
+
finialMessage: s.content
|
|
549
|
+
})), yield this.fillMessageReference(s), s.code) {
|
|
550
|
+
case 11915:
|
|
551
|
+
s.content = "请您换个问题问我吧~", s.index = -1, s.isSensitive = !0, s.isLoading = !1, s.sessionId = this._id || "", this.event.emit("message:error", s);
|
|
552
|
+
break;
|
|
553
|
+
case 200:
|
|
554
|
+
s.index = -1, s.isLoading = !1;
|
|
555
|
+
break;
|
|
556
|
+
default:
|
|
557
|
+
s.content = s.message || "未知错误", s.isLoading = !1, s.index = -1, this.event.emit("message:error", s);
|
|
558
|
+
}
|
|
559
|
+
this.getRelatedQuesion(s), this.removeEventListener("message:content", d), this.frozen = !1;
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
this.addEventListener("message:content", d), this.sendQuestion(l, w(f({}, e), {
|
|
563
|
+
stream: i
|
|
564
|
+
}));
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
setTop(e) {
|
|
568
|
+
return r(this, null, function* () {
|
|
569
|
+
if (!this._id)
|
|
570
|
+
throw Error("session is not avalible. id is undefined");
|
|
571
|
+
this.data = yield this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
572
|
+
top: e
|
|
573
|
+
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:top", {
|
|
574
|
+
top: e
|
|
575
|
+
});
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* 暂停当前会话的回答过程
|
|
580
|
+
* 该方法会停止当前回答,标记回答停止状态,处理正在进行的消息和操作,
|
|
581
|
+
* 并根据情况更新消息内容或添加新的系统消息。
|
|
582
|
+
* @returns 返回当前会话实例
|
|
583
|
+
*/
|
|
584
|
+
suspend() {
|
|
585
|
+
var i;
|
|
586
|
+
this.stopAnswer(), this.frozen = !1, this._stopAnswer = !0;
|
|
587
|
+
let e = this.messages[this.messages.length - 1];
|
|
588
|
+
const t = {
|
|
589
|
+
messageId: M(),
|
|
590
|
+
// 生成唯一的消息 ID
|
|
591
|
+
isLoading: !1,
|
|
592
|
+
// 标记消息已停止加载
|
|
593
|
+
index: -2,
|
|
594
|
+
// 消息索引,用于标识特殊状态
|
|
595
|
+
messageTime: N(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:ss:mm"),
|
|
596
|
+
// 消息时间
|
|
597
|
+
content: "",
|
|
598
|
+
// 消息内容
|
|
599
|
+
role: _.System,
|
|
600
|
+
// 消息角色为系统消息
|
|
601
|
+
sessionId: this.id || "",
|
|
602
|
+
// 会话 ID
|
|
603
|
+
reference: [],
|
|
604
|
+
// 引用列表
|
|
605
|
+
searchReference: []
|
|
606
|
+
// 搜索引用列表
|
|
607
|
+
};
|
|
608
|
+
if (e && !e.messageId) {
|
|
609
|
+
if (e.index === -2)
|
|
610
|
+
return;
|
|
611
|
+
if (e.actions) {
|
|
612
|
+
const n = e.actions[((i = e.actions) == null ? void 0 : i.length) - 1];
|
|
613
|
+
if (n) {
|
|
614
|
+
const o = n[n.length - 1];
|
|
615
|
+
if (o && (o.type === "BEFORE_CALL" || o.type === "FUNCTION_CALL")) {
|
|
616
|
+
const {
|
|
617
|
+
pluginName: l,
|
|
618
|
+
pluginCode: d,
|
|
619
|
+
funcCode: s,
|
|
620
|
+
funcName: p
|
|
621
|
+
} = o.data;
|
|
622
|
+
n.push({
|
|
623
|
+
data: {
|
|
624
|
+
pluginName: l,
|
|
625
|
+
pluginCode: d,
|
|
626
|
+
funcCode: s,
|
|
627
|
+
funcName: p,
|
|
628
|
+
step: o.data.step + 1
|
|
629
|
+
},
|
|
630
|
+
type: "ABORT"
|
|
631
|
+
}), this.event.emit("message:action", {
|
|
632
|
+
messageId: e.messageId,
|
|
633
|
+
action: n
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
e = f(f({}, e), t), this.event.emit("message:content", e), this.event.emit("message:updated", e);
|
|
639
|
+
} else
|
|
640
|
+
this.pushMessage(t), this.event.emit("message:content", t);
|
|
641
|
+
return this;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* 异步提交评论信息
|
|
645
|
+
* @param params - 评论参数,包含评论相关的信息,如消息 ID 等
|
|
646
|
+
* @returns 返回调用 API 提交评论后的响应结果
|
|
647
|
+
*/
|
|
648
|
+
postMessageComment(e) {
|
|
649
|
+
return r(this, null, function* () {
|
|
650
|
+
if (!this._applicationId || !this._id)
|
|
651
|
+
throw Error("session is not avalible. id is undefined");
|
|
652
|
+
return this.cvforceApi.postComments(this._applicationId, this._id, e).then((t) => {
|
|
653
|
+
const i = this.messages.find((n) => n.messageId === e.messageId);
|
|
654
|
+
return i && (i.comment = e, this.event.emit("message:comment", i)), t;
|
|
655
|
+
});
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* 清除当前会话的上下文信息
|
|
660
|
+
* 该方法会调用 API 清除指定会话的上下文,并在操作成功后触发相应事件
|
|
661
|
+
* @returns 如果会话 ID 存在且清除操作成功,返回一个解析为 true 的 Promise;若会话 ID 不存在,抛出错误
|
|
662
|
+
*/
|
|
663
|
+
clear() {
|
|
664
|
+
if (!this._id)
|
|
665
|
+
throw Error("session is not avalible. id is undefined");
|
|
666
|
+
return this.cvforceApi.patchApplicationIdSessionsIdContext(this._applicationId, this._id).then(() => (this.event.emit("context:clear"), !0));
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* 销毁当前会话实例,执行清理操作并触发销毁事件
|
|
670
|
+
* @returns 返回当前会话的 ID,如果会话 ID 不存在则返回空字符串
|
|
671
|
+
*/
|
|
672
|
+
destroy() {
|
|
673
|
+
var e;
|
|
674
|
+
return this._ready = !1, this.event.removeAllListeners(), (e = this.socket) == null || e.disconnect(), this.socket = null, this.event.emit("destroy", {
|
|
675
|
+
applicationId: this._applicationId,
|
|
676
|
+
sessionId: this._id
|
|
677
|
+
}), this._id || "";
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
export {
|
|
681
|
+
B as SessionErrors,
|
|
682
|
+
$ as default
|
|
683
|
+
};
|