@glodon-aiot/bot-client-sdk 3.26.0-alpha.8 → 3.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +284 -254
- package/dist/es/AgentSession.mjs +267 -197
- package/dist/es/BotClient.mjs +200 -123
- package/dist/es/Config.mjs +55 -88
- package/dist/es/DialogSession.mjs +123 -118
- package/dist/es/EventBus.mjs +38 -34
- package/dist/es/Logger.mjs +19 -21
- package/dist/es/Session.mjs +338 -284
- package/dist/es/api/index.mjs +31 -30
- package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +201 -0
- package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +356 -0
- package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +350 -0
- package/dist/es/builtinTools/browserRuntime/assets.mjs +9 -0
- package/dist/es/builtinTools/browserRuntime/loadBash.mjs +17 -0
- package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +96 -0
- package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +242 -0
- package/dist/es/builtinTools/command.mjs +56 -0
- package/dist/es/builtinTools/constants.mjs +17 -0
- package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +266 -0
- package/dist/es/builtinTools/overview.mjs +77 -0
- package/dist/es/builtinTools/paths.mjs +70 -0
- package/dist/es/builtinTools/platformSkills.mjs +248 -0
- package/dist/es/builtinTools/schemas.mjs +261 -0
- package/dist/es/builtinTools/tools.mjs +595 -0
- package/dist/es/index.mjs +66 -64
- package/dist/es/localFiles/adapters/FileSnapshotAdapter.mjs +160 -0
- package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +185 -113
- package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +127 -137
- package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +132 -97
- package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +32 -28
- package/dist/es/localFiles/constants.mjs +24 -28
- package/dist/es/localFiles/createLocalFilesPlugin.mjs +53 -52
- package/dist/es/localFiles/errors.mjs +44 -35
- package/dist/es/localFiles/host/connectHostBridge.mjs +43 -35
- package/dist/es/localFiles/host/protocol.mjs +27 -13
- package/dist/es/localFiles/host/sandboxClient.mjs +494 -0
- package/dist/es/localFiles/host/sandboxFrames.mjs +21 -0
- package/dist/es/localFiles/preview.mjs +33 -0
- package/dist/es/localFiles/schemas.mjs +40 -86
- package/dist/es/localFiles/validate.mjs +37 -37
- package/dist/es/localFiles/workspace/LocalWorkspace.mjs +176 -90
- package/dist/es/localFiles/workspace/WorkspaceFiles.mjs +246 -0
- package/dist/es/localMcp/client.mjs +234 -0
- package/dist/es/localMcp/endpoints.mjs +94 -0
- package/dist/es/localMcp/register.mjs +207 -0
- package/dist/es/localMcp/registry.mjs +43 -0
- package/dist/es/package.json.mjs +49 -60
- package/dist/es/sessionUpload.mjs +49 -0
- package/dist/es/utils.mjs +24 -11
- package/dist/lib/index.js +382 -10
- package/dist/src/AgentSession.d.ts +13 -0
- package/dist/src/BotClient.d.ts +13 -1
- package/dist/src/Config.d.ts +7 -8
- package/dist/src/EventBus.d.ts +3 -0
- package/dist/src/Logger.d.ts +15 -8
- package/dist/src/PayloadTypes.d.ts +2 -0
- package/dist/src/Session.d.ts +19 -2
- package/dist/src/api/index.d.ts +2 -2
- package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +26 -0
- package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +67 -0
- package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +51 -0
- package/dist/src/builtinTools/browserRuntime/assets.d.ts +13 -0
- package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +3 -0
- package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +44 -0
- package/dist/src/builtinTools/command.d.ts +35 -0
- package/dist/src/builtinTools/constants.d.ts +25 -0
- package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +11 -0
- package/dist/src/builtinTools/overview.d.ts +5 -0
- package/dist/src/builtinTools/paths.d.ts +16 -0
- package/dist/src/builtinTools/platformSkills.d.ts +92 -0
- package/dist/src/builtinTools/schemas.d.ts +3 -0
- package/dist/src/builtinTools/tools.d.ts +73 -0
- package/dist/src/constant.d.ts +1 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/localFiles/adapters/FileSnapshotAdapter.d.ts +24 -0
- package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +4 -1
- package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +30 -14
- package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +4 -1
- package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +22 -0
- package/dist/src/localFiles/adapters/fsaTypes.d.ts +2 -0
- package/dist/src/localFiles/constants.d.ts +5 -13
- package/dist/src/localFiles/errors.d.ts +1 -1
- package/dist/src/localFiles/host/protocol.d.ts +34 -16
- package/dist/src/localFiles/host/sandboxClient.d.ts +112 -0
- package/dist/src/localFiles/host/sandboxFrames.d.ts +9 -0
- package/dist/src/localFiles/index.d.ts +11 -6
- package/dist/src/localFiles/preview.d.ts +2 -0
- package/dist/src/localFiles/schemas.d.ts +2 -5
- package/dist/src/localFiles/types.d.ts +16 -23
- package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +35 -1
- package/dist/src/localFiles/workspace/WorkspaceFiles.d.ts +63 -0
- package/dist/src/localMcp/client.d.ts +15 -0
- package/dist/src/localMcp/endpoints.d.ts +19 -0
- package/dist/src/localMcp/register.d.ts +34 -0
- package/dist/src/localMcp/registry.d.ts +27 -0
- package/dist/src/sessionUpload.d.ts +14 -0
- package/dist/src/utils.d.ts +6 -0
- package/package.json +13 -24
- package/dist/es/localFiles/createLocalPythonPlugin.mjs +0 -196
package/dist/es/Session.mjs
CHANGED
|
@@ -1,43 +1,44 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var R = Object.prototype.hasOwnProperty,
|
|
5
|
-
var b = (
|
|
1
|
+
var V = Object.defineProperty, M = Object.defineProperties;
|
|
2
|
+
var O = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var x = Object.getOwnPropertySymbols;
|
|
4
|
+
var R = Object.prototype.hasOwnProperty, z = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var b = (p, e, t) => e in p ? V(p, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : p[e] = t, u = (p, e) => {
|
|
6
6
|
for (var t in e || (e = {}))
|
|
7
|
-
R.call(e, t) && b(
|
|
8
|
-
if (
|
|
9
|
-
for (var t of
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
var
|
|
14
|
-
var c = (
|
|
15
|
-
var n = (
|
|
7
|
+
R.call(e, t) && b(p, t, e[t]);
|
|
8
|
+
if (x)
|
|
9
|
+
for (var t of x(e))
|
|
10
|
+
z.call(e, t) && b(p, t, e[t]);
|
|
11
|
+
return p;
|
|
12
|
+
}, y = (p, e) => M(p, O(e));
|
|
13
|
+
var f = (p, e, t) => (b(p, typeof e != "symbol" ? e + "" : e, t), t);
|
|
14
|
+
var c = (p, e, t) => new Promise((i, o) => {
|
|
15
|
+
var n = (a) => {
|
|
16
16
|
try {
|
|
17
|
-
|
|
18
|
-
} catch (
|
|
19
|
-
|
|
17
|
+
d(t.next(a));
|
|
18
|
+
} catch (s) {
|
|
19
|
+
o(s);
|
|
20
20
|
}
|
|
21
|
-
},
|
|
21
|
+
}, h = (a) => {
|
|
22
22
|
try {
|
|
23
|
-
|
|
24
|
-
} catch (
|
|
25
|
-
|
|
23
|
+
d(t.throw(a));
|
|
24
|
+
} catch (s) {
|
|
25
|
+
o(s);
|
|
26
26
|
}
|
|
27
|
-
},
|
|
28
|
-
|
|
27
|
+
}, d = (a) => a.done ? i(a.value) : Promise.resolve(a.value).then(n, h);
|
|
28
|
+
d((t = t.apply(p, e)).next());
|
|
29
29
|
});
|
|
30
|
-
import { v4 as
|
|
31
|
-
import { parseJWT as
|
|
32
|
-
import { ApplicationTypes as
|
|
33
|
-
import
|
|
34
|
-
import { configuration as
|
|
35
|
-
import
|
|
36
|
-
import { getCVForceService as
|
|
37
|
-
import
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
30
|
+
import { v4 as Q } from "uuid";
|
|
31
|
+
import { parseJWT as P, resolveRelatedQuestionContent as q, removeNewlines as F, removeLeadingAndTrailingBackslashes as N } from "./utils.mjs";
|
|
32
|
+
import { ApplicationTypes as T, PromptStatus as U, PromptVariableType as L, ChatMessageRole as S } from "@glodon-aiot/apis";
|
|
33
|
+
import B from "./Logger.mjs";
|
|
34
|
+
import { configuration as _ } from "./Config.mjs";
|
|
35
|
+
import H from "./EventBus.mjs";
|
|
36
|
+
import { getCVForceService as K } from "./api/index.mjs";
|
|
37
|
+
import Y from "dayjs";
|
|
38
|
+
import { uploadSessionFile as G } from "./sessionUpload.mjs";
|
|
39
|
+
const C = 100, l = new B("[Session] ");
|
|
40
|
+
var j = /* @__PURE__ */ ((p) => (p.DataRequestFailed = "data request error", p.SocketConnectionFailed = "socket error", p.IdUndefined = "session id is not defined", p.SendingEmptyContent = "failed at sending empty content", p.Frozen = "session is frozen", p.UserModeError = "failed at setting socket user mode", p.Inactive = "session is inactive", p))(j || {});
|
|
41
|
+
class ne {
|
|
41
42
|
/**
|
|
42
43
|
* Session 类的构造函数,用于初始化会话实例。
|
|
43
44
|
* @param token - 用户的身份验证令牌,用于解析应用 ID。
|
|
@@ -47,87 +48,91 @@ class te {
|
|
|
47
48
|
* @param _application - 应用信息,可选参数。
|
|
48
49
|
* @param client - Bot 客户端实例,可选参数。
|
|
49
50
|
*/
|
|
50
|
-
constructor(e, t = "",
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
constructor(e, t = "", i, o, n, h) {
|
|
52
|
+
f(this, "_id");
|
|
53
|
+
f(this, "_frozen", !1);
|
|
53
54
|
//是否可发送新问题
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
f(this, "inactivating", !1);
|
|
56
|
+
f(this, "_ready", !1);
|
|
56
57
|
// ready = false;
|
|
57
|
-
|
|
58
|
+
f(this, "event", new H());
|
|
58
59
|
//从token解析出来的用户信息
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
this.token = e, this.lazyload =
|
|
60
|
+
f(this, "_applicationId");
|
|
61
|
+
f(this, "_application", null);
|
|
62
|
+
f(this, "data", null);
|
|
63
|
+
f(this, "client", null);
|
|
64
|
+
f(this, "messages", []);
|
|
65
|
+
f(this, "_loadingMessages", !1);
|
|
66
|
+
f(this, "_loadingMessagesSize", 10);
|
|
67
|
+
f(this, "cvforceApi");
|
|
68
|
+
f(this, "socket", null);
|
|
69
|
+
f(this, "_promptVariables", []);
|
|
70
|
+
f(this, "_sessionActive", !1);
|
|
71
|
+
f(this, "activing", !1);
|
|
72
|
+
f(this, "_stopAnswer", !1);
|
|
73
|
+
f(this, "_dataLoading", !1);
|
|
74
|
+
f(this, "_dataLoaded", !1);
|
|
75
|
+
f(this, "_mcpServers", null);
|
|
76
|
+
this.token = e, this.lazyload = o, this.client = h || null, this._applicationId = P(e).rid || _.applicationId, this.cvforceApi = this.getService(), n && (this._application = n), this.event.emit("init", {
|
|
76
77
|
applicationId: this._applicationId,
|
|
77
78
|
sessionId: t
|
|
78
79
|
}), this.loadApplicationInfo().then(() => {
|
|
79
|
-
var
|
|
80
|
+
var d;
|
|
80
81
|
if (t)
|
|
81
|
-
this._id = t, this.event.emit("data:loading"),
|
|
82
|
-
this.data =
|
|
82
|
+
this._id = t, this.event.emit("data:loading"), i ? (this.data = i, o || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((a) => c(this, null, function* () {
|
|
83
|
+
this.data = a, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
83
84
|
})).then(() => c(this, null, function* () {
|
|
84
85
|
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((
|
|
86
|
-
this.data =
|
|
86
|
+
})))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((a) => c(this, null, function* () {
|
|
87
|
+
this.data = a, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
87
88
|
})).then(() => c(this, null, function* () {
|
|
88
89
|
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
|
})));
|
|
90
91
|
else {
|
|
91
|
-
const
|
|
92
|
+
const a = (i == null ? void 0 : i.name) || "新建会话";
|
|
92
93
|
this.event.emit("data:creating", {
|
|
93
94
|
applicationId: this._applicationId,
|
|
94
|
-
name:
|
|
95
|
+
name: a
|
|
95
96
|
}), this.event.emit("data:loading"), this.cvforceApi.postSession(this.applicationId, {
|
|
96
|
-
name:
|
|
97
|
+
name: a,
|
|
97
98
|
// @ts-ignore
|
|
98
|
-
netOpen:
|
|
99
|
-
knowledgeIdList: (
|
|
99
|
+
netOpen: i == null ? void 0 : i.netOpen,
|
|
100
|
+
knowledgeIdList: (d = i == null ? void 0 : i.knowledges) == null ? void 0 : d.map((s) => s.id),
|
|
100
101
|
applicationId: this._applicationId
|
|
101
|
-
}).then((
|
|
102
|
+
}).then((s) => c(this, null, function* () {
|
|
102
103
|
var m;
|
|
103
|
-
this._id =
|
|
104
|
-
knowledges:
|
|
104
|
+
this._id = s.id, this.data = y(u({}, s), {
|
|
105
|
+
knowledges: i == null ? void 0 : i.knowledges
|
|
105
106
|
}), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id);
|
|
106
107
|
try {
|
|
107
108
|
yield (() => c(this, null, function* () {
|
|
108
|
-
if (
|
|
109
|
-
return this.setKnowledges((
|
|
109
|
+
if (i != null && i.knowledges)
|
|
110
|
+
return this.setKnowledges((i == null ? void 0 : i.knowledges) || void 0);
|
|
110
111
|
}))();
|
|
111
|
-
} catch (
|
|
112
|
-
|
|
112
|
+
} catch (I) {
|
|
113
|
+
l.error(I);
|
|
113
114
|
}
|
|
114
115
|
yield (() => {
|
|
115
|
-
if (
|
|
116
|
-
const
|
|
117
|
-
var
|
|
118
|
-
const w = ((
|
|
119
|
-
return
|
|
116
|
+
if (i != null && i.promptVariables) {
|
|
117
|
+
const I = this._promptVariables.map((g) => {
|
|
118
|
+
var A, v;
|
|
119
|
+
const w = ((v = (A = i.promptVariables) == null ? void 0 : A.find((r) => r.key === g.key && r.type === g.type)) == null ? void 0 : v.value) || "";
|
|
120
|
+
return y(u({}, g), {
|
|
120
121
|
value: w
|
|
121
122
|
});
|
|
122
|
-
}).sort((
|
|
123
|
-
return this.setPromptVariables(
|
|
123
|
+
}).sort((g, w) => g.key.localeCompare(w.key));
|
|
124
|
+
return this.setPromptVariables(I || []);
|
|
124
125
|
} else
|
|
125
126
|
return;
|
|
126
|
-
})(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((m = this.application) == null ? void 0 : m.type) !==
|
|
127
|
+
})(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((m = this.application) == null ? void 0 : m.type) !== T.Agent && (this._ready = !0, this.event.emit("ready"));
|
|
127
128
|
}));
|
|
128
129
|
}
|
|
129
130
|
});
|
|
130
131
|
}
|
|
132
|
+
/** 是否为当前正在使用的会话。 */
|
|
133
|
+
get active() {
|
|
134
|
+
return this._sessionActive;
|
|
135
|
+
}
|
|
131
136
|
fetchData() {
|
|
132
137
|
return c(this, null, function* () {
|
|
133
138
|
if (!this.data)
|
|
@@ -175,14 +180,14 @@ class te {
|
|
|
175
180
|
return c(this, null, function* () {
|
|
176
181
|
if (!this.data)
|
|
177
182
|
throw Error("session data is not loaded");
|
|
178
|
-
if ((e || []).some((
|
|
183
|
+
if ((e || []).some((o) => Array.isArray(o.fileIds) && o.fileIds.length === 0))
|
|
179
184
|
throw Error("fileIds cannot be an empty array");
|
|
180
|
-
if ((e || []).reduce((
|
|
181
|
-
var
|
|
182
|
-
const
|
|
183
|
-
return
|
|
184
|
-
}, 0) >
|
|
185
|
-
throw Error(`knowledges and fileIds total count cannot exceed ${
|
|
185
|
+
if ((e || []).reduce((o, n) => {
|
|
186
|
+
var d, a;
|
|
187
|
+
const h = (a = (d = n.fileIds) == null ? void 0 : d.length) != null ? a : 0;
|
|
188
|
+
return o + (h > 0 ? h : 1);
|
|
189
|
+
}, 0) > C)
|
|
190
|
+
throw Error(`knowledges and fileIds total count cannot exceed ${C}`);
|
|
186
191
|
return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
|
|
187
192
|
knowledges: e
|
|
188
193
|
}), this.data.knowledges = e, this.event.emit("data:updated", this.data), this.event.emit("data:updated:knowledges", {
|
|
@@ -195,7 +200,7 @@ class te {
|
|
|
195
200
|
throw Error("session id is not defined");
|
|
196
201
|
if (!this.data)
|
|
197
202
|
throw Error("session data is not loaded");
|
|
198
|
-
const e = this.promptVariables.filter((t) => t.status ===
|
|
203
|
+
const e = this.promptVariables.filter((t) => t.status === U.Quoted).filter((t) => !t.value);
|
|
199
204
|
if (e.length)
|
|
200
205
|
throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((t) => t.key).join(", ")}`);
|
|
201
206
|
}
|
|
@@ -204,16 +209,16 @@ class te {
|
|
|
204
209
|
}
|
|
205
210
|
setPromptVariables(e) {
|
|
206
211
|
return c(this, null, function* () {
|
|
207
|
-
const t = e == null ? void 0 : e.map((n) => n.type ? n :
|
|
212
|
+
const t = e == null ? void 0 : e.map((n) => n.type ? n : y(u({}, n), {
|
|
208
213
|
type: "instruction"
|
|
209
|
-
})),
|
|
210
|
-
var
|
|
211
|
-
const
|
|
212
|
-
return
|
|
213
|
-
value:
|
|
214
|
+
})), i = this._promptVariables.map((n) => {
|
|
215
|
+
var d;
|
|
216
|
+
const h = ((d = t == null ? void 0 : t.find((a) => a.key === n.key && a.type === n.type)) == null ? void 0 : d.value) || n.value;
|
|
217
|
+
return y(u({}, n), {
|
|
218
|
+
value: h
|
|
214
219
|
});
|
|
215
|
-
}).sort((n,
|
|
216
|
-
if (!(yield Promise.all(
|
|
220
|
+
}).sort((n, h) => n.key.localeCompare(h.key));
|
|
221
|
+
if (!(yield Promise.all(i.filter((n) => !(n.type === L.User && n.key === "query") && !(n.type === L.System && n.key === "datetime")).map((n) => {
|
|
217
222
|
if (!this.data)
|
|
218
223
|
throw Error("session data is not loaded");
|
|
219
224
|
if (!(!n.id || !n.key))
|
|
@@ -241,13 +246,13 @@ class te {
|
|
|
241
246
|
*/
|
|
242
247
|
setNetOpen(e) {
|
|
243
248
|
return c(this, null, function* () {
|
|
244
|
-
var t,
|
|
245
|
-
if (
|
|
246
|
-
|
|
249
|
+
var t, i, o;
|
|
250
|
+
if (l.log("setNetOpen:", e), l.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((i = this._application) == null ? void 0 : i.type) === T.Agent) {
|
|
251
|
+
l.warn("Agent Application Sessions are disabled to setNetOpen.");
|
|
247
252
|
return;
|
|
248
253
|
}
|
|
249
|
-
if (((
|
|
250
|
-
|
|
254
|
+
if (((o = this._application) == null ? void 0 : o.arrange[0].enableNetOpen) === !1) {
|
|
255
|
+
l.warn("This Application is disabled to change netOpen.");
|
|
251
256
|
return;
|
|
252
257
|
}
|
|
253
258
|
if (!this.data)
|
|
@@ -255,26 +260,39 @@ class te {
|
|
|
255
260
|
return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
|
|
256
261
|
netOpen: e ? 1 : 2
|
|
257
262
|
/* disable */
|
|
258
|
-
}), this.data.netOpen = e,
|
|
263
|
+
}), this.data.netOpen = e, l.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.event.emit("data:updated:netOpen", {
|
|
259
264
|
netOpen: e
|
|
260
265
|
}), e;
|
|
261
266
|
});
|
|
262
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* 发请求用的 token。有 BotClient 时跟它同步,便于同 payload 静默换票。
|
|
270
|
+
*/
|
|
271
|
+
get requestToken() {
|
|
272
|
+
var e, t;
|
|
273
|
+
return (t = (e = this.client) == null ? void 0 : e.token) != null ? t : this.token;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* 同 payload 静默换票时通知长连接。
|
|
277
|
+
* Agent 发 socket `update`;Dialog / 无连接默认空操作(HTTP 已现读 requestToken)。
|
|
278
|
+
*/
|
|
279
|
+
updateSocketResourceToken(e) {
|
|
280
|
+
}
|
|
263
281
|
getService() {
|
|
264
|
-
return
|
|
265
|
-
const
|
|
282
|
+
return K(_.apiRoot, () => this.requestToken, (e, t) => {
|
|
283
|
+
const i = new Error("data request error", {
|
|
266
284
|
cause: {
|
|
267
285
|
status: e,
|
|
268
286
|
message: t
|
|
269
287
|
}
|
|
270
288
|
});
|
|
271
|
-
|
|
289
|
+
l.error(i), this.event.emit("error", i);
|
|
272
290
|
});
|
|
273
291
|
}
|
|
274
|
-
addEventListener(e, t,
|
|
292
|
+
addEventListener(e, t, i = !1) {
|
|
275
293
|
if (typeof t != "function")
|
|
276
294
|
throw Error("event listener is not a function");
|
|
277
|
-
if (
|
|
295
|
+
if (i) {
|
|
278
296
|
this.event.once(e, t);
|
|
279
297
|
return;
|
|
280
298
|
}
|
|
@@ -290,36 +308,36 @@ class te {
|
|
|
290
308
|
*/
|
|
291
309
|
fetchMessages() {
|
|
292
310
|
return c(this, arguments, function* (e = {}) {
|
|
293
|
-
var
|
|
311
|
+
var i, o;
|
|
294
312
|
const t = {
|
|
295
313
|
pageSize: this._loadingMessagesSize
|
|
296
314
|
};
|
|
297
315
|
if (this._id) {
|
|
298
|
-
e =
|
|
299
|
-
const n = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e),
|
|
300
|
-
var
|
|
301
|
-
let m = [],
|
|
302
|
-
return typeof ((
|
|
316
|
+
e = u(u({}, t), e), this.event.emit("history:loading"), l.log("history:loading"), (e.endTime || e.startTime) && (this.event.emit("history:moreloading", e), l.log("history:moreloading", e)), this._loadingMessages = !0;
|
|
317
|
+
const n = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), h = n.map((a, s) => {
|
|
318
|
+
var g, w;
|
|
319
|
+
let m = [], I;
|
|
320
|
+
return typeof ((g = a.agentHistoryList) == null ? void 0 : g.agentList) == "string" && (m = JSON.parse(a.agentHistoryList.agentList).reduce((v, r) => (v[r.data.step - 1] || (v[r.data.step - 1] = []), v[r.data.step - 1].push(r), v), [])), s === n.length - 1 && (I = (w = this.data) == null ? void 0 : w.lastRelatedQuestion), y(u({}, a), {
|
|
303
321
|
actions: m,
|
|
304
|
-
relatedQuesion:
|
|
322
|
+
relatedQuesion: I,
|
|
305
323
|
fromHistory: !0
|
|
306
324
|
});
|
|
307
325
|
});
|
|
308
326
|
if (!e.endTime && !e.startTime) {
|
|
309
|
-
const
|
|
310
|
-
|
|
327
|
+
const a = this.messages[this.messages.length - 1];
|
|
328
|
+
a && !a.relatedQuesion && (a.relatedQuesion = (i = this.data) == null ? void 0 : i.lastRelatedQuestion), this.messages = [...h, ...this.messages];
|
|
311
329
|
} else if (e.endTime && !e.startTime) {
|
|
312
|
-
const
|
|
313
|
-
this.messages.splice(0,
|
|
330
|
+
const a = this.messages.findIndex((s) => s.ts === e.endTime);
|
|
331
|
+
this.messages.splice(0, a + 1), this.messages = [...h, ...this.messages];
|
|
314
332
|
} else if (!e.endTime && e.startTime) {
|
|
315
|
-
const
|
|
316
|
-
this.messages.splice(
|
|
333
|
+
const a = this.messages.findLastIndex((s) => s.ts === e.endTime);
|
|
334
|
+
this.messages.splice(a, this.messages.length - a - 1), this.messages = [...this.messages, ...h];
|
|
317
335
|
} else {
|
|
318
|
-
const
|
|
319
|
-
this.messages.splice(
|
|
336
|
+
const a = this.messages.findIndex((m) => m.ts === e.endTime), s = this.messages.findLastIndex((m) => m.ts === e.endTime);
|
|
337
|
+
this.messages.splice(a, s - a + 1, ...h);
|
|
320
338
|
}
|
|
321
|
-
const
|
|
322
|
-
return
|
|
339
|
+
const d = this.messages[this.messages.length - 1];
|
|
340
|
+
return d && !d.relatedQuesion && (d.relatedQuesion = (o = this.data) == null ? void 0 : o.lastRelatedQuestion), this.event.emit("history:moreloaded", h), l.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, h;
|
|
323
341
|
} else {
|
|
324
342
|
const n = new Error(
|
|
325
343
|
"session id is not defined"
|
|
@@ -328,7 +346,7 @@ class te {
|
|
|
328
346
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
329
347
|
// }
|
|
330
348
|
);
|
|
331
|
-
return
|
|
349
|
+
return l.error(n), this.event.emit("error", n), !1;
|
|
332
350
|
}
|
|
333
351
|
});
|
|
334
352
|
}
|
|
@@ -340,14 +358,14 @@ class te {
|
|
|
340
358
|
throw Error("name is empty");
|
|
341
359
|
return this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
342
360
|
name: e
|
|
343
|
-
}),
|
|
361
|
+
}), l.log("data:updated", this.messages), this.event.emit("data:updated", this.data), this.event.emit("data:updated:name", {
|
|
344
362
|
name: e
|
|
345
363
|
}), !0;
|
|
346
364
|
});
|
|
347
365
|
}
|
|
348
366
|
pushMessage(e) {
|
|
349
367
|
return c(this, null, function* () {
|
|
350
|
-
this.messages.push(e),
|
|
368
|
+
this.messages.push(e), l.log("history:add", e), this.event.emit("history:add", e), this.event.emit("message:new", e), this.event.emit("message:updated", e);
|
|
351
369
|
});
|
|
352
370
|
}
|
|
353
371
|
reloadSessionInfo() {
|
|
@@ -356,7 +374,7 @@ class te {
|
|
|
356
374
|
return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => c(this, null, function* () {
|
|
357
375
|
if (this.data = e, !this._id)
|
|
358
376
|
throw Error("session is not created yet");
|
|
359
|
-
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id),
|
|
377
|
+
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), l.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.data;
|
|
360
378
|
})).then(() => c(this, null, function* () {
|
|
361
379
|
return this.messages = [], yield this.fetchMessages(), this._dataLoading = !1, this._dataLoaded = !0, this.event.emit("data:loaded", this.data), this;
|
|
362
380
|
}));
|
|
@@ -376,37 +394,38 @@ class te {
|
|
|
376
394
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
377
395
|
// }
|
|
378
396
|
);
|
|
379
|
-
return
|
|
397
|
+
return l.error(e), this.event.emit("error", e), !1;
|
|
380
398
|
}
|
|
381
399
|
return this.event.emit("data:deleting"), yield this.cvforceApi.delSessionsId(this.applicationId, this.id), this.event.emit("data:deleted"), this.destroy();
|
|
382
400
|
});
|
|
383
401
|
}
|
|
384
402
|
activate() {
|
|
385
403
|
return c(this, null, function* () {
|
|
404
|
+
var t;
|
|
386
405
|
if (this.activing)
|
|
387
|
-
return new Promise((
|
|
406
|
+
return new Promise((i) => {
|
|
388
407
|
this.addEventListener("activated", () => {
|
|
389
|
-
|
|
408
|
+
i(!0);
|
|
390
409
|
}, !0);
|
|
391
410
|
});
|
|
392
411
|
if (!this._id)
|
|
393
412
|
throw Error("session is not avalible. id is undefined");
|
|
394
|
-
this.activing = !0;
|
|
413
|
+
l.log("activate", this._id), this.activing = !0;
|
|
395
414
|
const e = this._id;
|
|
396
|
-
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((
|
|
397
|
-
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e),
|
|
398
|
-
})), 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.
|
|
415
|
+
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((i) => c(this, null, function* () {
|
|
416
|
+
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), l.log("activated", this._id), this._sessionActive = !0, this.activing = !1, this;
|
|
399
418
|
});
|
|
400
419
|
}
|
|
401
420
|
inactivate(e = !1) {
|
|
402
|
-
return this.socket ? this._frozen ? (e || (this.inactivating = !0), this.activing = !1, !0) : (this.socket.close(), this.socket = null, this.event.emit("inactivated"), this.
|
|
421
|
+
return this.socket ? (l.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);
|
|
403
422
|
}
|
|
404
423
|
/**
|
|
405
424
|
* 会话标题摘要开关。应用详情返回布尔值时用该值,字段缺失时默认开启。
|
|
406
425
|
*/
|
|
407
426
|
resolveEnableTitleSummary() {
|
|
408
|
-
var t,
|
|
409
|
-
const e = (
|
|
427
|
+
var t, i, o;
|
|
428
|
+
const e = (o = (i = (t = this.application) == null ? void 0 : t.arrange) == null ? void 0 : i[0]) == null ? void 0 : o.autoGenerateSessionTitle;
|
|
410
429
|
return typeof e == "boolean" ? e : !0;
|
|
411
430
|
}
|
|
412
431
|
//TODO: remove mcp server
|
|
@@ -417,201 +436,236 @@ class te {
|
|
|
417
436
|
get messagesLoading() {
|
|
418
437
|
return this._loadingMessages;
|
|
419
438
|
}
|
|
420
|
-
|
|
439
|
+
/**
|
|
440
|
+
* 把文件上传到当前会话的对象存储,返回可访问的地址。
|
|
441
|
+
* 图片地址放进 send 的 imageContent。
|
|
442
|
+
*/
|
|
443
|
+
upload(e) {
|
|
444
|
+
return c(this, null, function* () {
|
|
445
|
+
if (!this._id)
|
|
446
|
+
throw Error("session is not avalible. id is undefined");
|
|
447
|
+
return G(this.cvforceApi, this._id, e);
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
send(i) {
|
|
421
451
|
return c(this, arguments, function* (e, t = {
|
|
422
|
-
stream:
|
|
452
|
+
stream: _.stream
|
|
423
453
|
}) {
|
|
424
|
-
var
|
|
454
|
+
var A, v;
|
|
425
455
|
try {
|
|
426
456
|
this.validateConfigs();
|
|
427
|
-
} catch (
|
|
428
|
-
|
|
457
|
+
} catch (r) {
|
|
458
|
+
l.report(r);
|
|
429
459
|
}
|
|
430
460
|
const {
|
|
431
|
-
stream:
|
|
461
|
+
stream: o = _.stream,
|
|
432
462
|
referenceFirst: n = !1
|
|
433
|
-
} = t,
|
|
434
|
-
if (!
|
|
435
|
-
const
|
|
463
|
+
} = t, h = e.uuid || Q(), d = ((A = e.text) == null ? void 0 : A.trim()) || "", a = e.files || [], s = e.fileIds, m = e.imageContent;
|
|
464
|
+
if (!d && !a.length && !(s != null && s.length)) {
|
|
465
|
+
const r = new Error(
|
|
436
466
|
"failed at sending empty content"
|
|
437
467
|
/* SendingEmptyContent */
|
|
438
468
|
// {
|
|
439
469
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.SendingEmptyContent],
|
|
440
470
|
// }
|
|
441
471
|
);
|
|
442
|
-
return
|
|
472
|
+
return l.error(r), this.event.emit("error", r), !1;
|
|
443
473
|
}
|
|
444
474
|
if (!this._id) {
|
|
445
|
-
const
|
|
475
|
+
const r = new Error(
|
|
446
476
|
"session id is not defined"
|
|
447
477
|
/* IdUndefined */
|
|
448
478
|
// {
|
|
449
479
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
450
480
|
// }
|
|
451
481
|
);
|
|
452
|
-
return
|
|
482
|
+
return l.error(r), this.event.emit("error", r), !1;
|
|
453
483
|
}
|
|
454
484
|
if (this.frozen) {
|
|
455
|
-
const
|
|
485
|
+
const r = new Error(
|
|
456
486
|
"session is frozen"
|
|
457
487
|
/* Frozen */
|
|
458
488
|
// {
|
|
459
489
|
// cause: { code: -1, message: 'session is frozen.' },
|
|
460
490
|
// }
|
|
461
491
|
);
|
|
462
|
-
return
|
|
492
|
+
return l.error(r), this.event.emit("error", r), !1;
|
|
463
493
|
}
|
|
464
|
-
this.frozen = !0, this._stopAnswer = !1
|
|
465
|
-
|
|
494
|
+
this.frozen = !0, this._stopAnswer = !1, l.log("send", {
|
|
495
|
+
sessionId: this._id,
|
|
496
|
+
stream: o,
|
|
497
|
+
contentLength: d.length,
|
|
498
|
+
fileCount: a.length,
|
|
499
|
+
fileIdCount: (v = s == null ? void 0 : s.length) != null ? v : 0
|
|
500
|
+
});
|
|
501
|
+
const I = {
|
|
466
502
|
sessionId: this.id,
|
|
467
503
|
role: 2,
|
|
468
|
-
content:
|
|
469
|
-
messageId:
|
|
504
|
+
content: d,
|
|
505
|
+
messageId: h,
|
|
470
506
|
reference: [],
|
|
471
507
|
searchReference: [],
|
|
472
508
|
messageTime: "",
|
|
473
|
-
files:
|
|
509
|
+
files: a,
|
|
474
510
|
imageContent: m
|
|
475
|
-
},
|
|
476
|
-
content:
|
|
477
|
-
stream:
|
|
478
|
-
requestId:
|
|
479
|
-
fileIds:
|
|
511
|
+
}, g = {
|
|
512
|
+
content: d,
|
|
513
|
+
stream: o,
|
|
514
|
+
requestId: h,
|
|
515
|
+
fileIds: s,
|
|
480
516
|
referenceFirst: n,
|
|
481
517
|
imageContent: m
|
|
482
518
|
};
|
|
483
|
-
this.pushMessage(
|
|
484
|
-
const w = (
|
|
485
|
-
if (
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
519
|
+
this.pushMessage(I), this.event.emit("question:content", d);
|
|
520
|
+
const w = (r) => c(this, null, function* () {
|
|
521
|
+
if (r.role === S.Robot) {
|
|
522
|
+
this.removeEventListener("answer:finish", w);
|
|
523
|
+
try {
|
|
524
|
+
switch (this._id && r.content && r.index && r.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
525
|
+
finialMessage: r.content
|
|
526
|
+
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
527
|
+
finialMessage: r.content
|
|
528
|
+
})), yield this.fillMessageReference(r), r.code) {
|
|
529
|
+
case 11915:
|
|
530
|
+
r.content = "请您换个问题问我吧~", r.index = -1, r.isSensitive = !0, r.isLoading = !1, r.sessionId = this._id || "", this.event.emit("message:error", r);
|
|
531
|
+
break;
|
|
532
|
+
case 11916:
|
|
533
|
+
r.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", r.index = -1, r.isSensitive = !0, r.isLoading = !1, r.sessionId = this._id || "", this.event.emit("message:error", r);
|
|
534
|
+
break;
|
|
535
|
+
case 200:
|
|
536
|
+
r.index = -1, r.isLoading = !1;
|
|
537
|
+
break;
|
|
538
|
+
default:
|
|
539
|
+
r.content = r.message || "未知错误", r.isLoading = !1, r.index = -1, this.event.emit("message:error", r);
|
|
540
|
+
}
|
|
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
|
+
} finally {
|
|
549
|
+
this.frozen = !1;
|
|
550
|
+
}
|
|
503
551
|
}
|
|
504
|
-
this.getRelatedQuesion(o), o.isLoading = !1;
|
|
505
|
-
let g = this.messages[this.messages.length - 1];
|
|
506
|
-
if (g && g.role === A.Robot) {
|
|
507
|
-
const k = f(f({}, g), o);
|
|
508
|
-
this.messages[this.messages.length - 1] = k, this.event.emit("message:updated", k);
|
|
509
|
-
} else
|
|
510
|
-
this.pushMessage(o);
|
|
511
|
-
this.frozen = !1;
|
|
512
552
|
});
|
|
513
|
-
return this.addEventListener("answer:finish", w), this.sendQuestion(
|
|
514
|
-
stream:
|
|
553
|
+
return this.addEventListener("answer:finish", w), this.sendQuestion(g, y(u({}, t), {
|
|
554
|
+
stream: o,
|
|
515
555
|
referenceFirst: n
|
|
516
|
-
})),
|
|
556
|
+
})), I;
|
|
517
557
|
});
|
|
518
558
|
}
|
|
519
559
|
currentUserQuestion() {
|
|
520
560
|
for (let e = this.messages.length - 1; e >= 0; e -= 1) {
|
|
521
561
|
const t = this.messages[e];
|
|
522
|
-
if (t.role ===
|
|
562
|
+
if (t.role === S.User)
|
|
523
563
|
return t.content || "";
|
|
524
564
|
}
|
|
525
565
|
return "";
|
|
526
566
|
}
|
|
527
567
|
getRelatedQuesion(e) {
|
|
528
568
|
return c(this, null, function* () {
|
|
529
|
-
var
|
|
569
|
+
var d, a;
|
|
570
|
+
const t = (d = this.application) == null ? void 0 : d.arrange[0];
|
|
530
571
|
if (!e.content || e.isSensitive || e.code !== 200)
|
|
531
|
-
return
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
const
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
572
|
+
return l.log("related:skip", {
|
|
573
|
+
code: e.code,
|
|
574
|
+
sensitive: !!e.isSensitive,
|
|
575
|
+
hasContent: !!e.content
|
|
576
|
+
}), e;
|
|
577
|
+
const i = q(t == null ? void 0 : t.recommendQuestionOpen, t == null ? void 0 : t.relatedQuestionApp, e.content, this.currentUserQuestion());
|
|
578
|
+
if (i === null)
|
|
579
|
+
return l.log("related:skip", {
|
|
580
|
+
recommendQuestionOpen: t == null ? void 0 : t.recommendQuestionOpen
|
|
581
|
+
}), e;
|
|
582
|
+
l.log("related:request", i), this.event.emit("message:relatedQuesion:loading", e);
|
|
583
|
+
const o = yield this.cvforceApi.postGeneques({
|
|
584
|
+
content: i
|
|
585
|
+
}).catch((s) => (l.error("related:failed", s), null));
|
|
586
|
+
if (l.log("related:response", o), !o) {
|
|
587
|
+
const s = [], m = y(u({}, e), {
|
|
588
|
+
relatedQuesion: s,
|
|
542
589
|
index: -3,
|
|
543
590
|
isLoading: !1
|
|
544
591
|
});
|
|
545
592
|
return this.event.emit("message:relatedQuesion:loaded", {
|
|
546
|
-
relatedQuesion:
|
|
593
|
+
relatedQuesion: s
|
|
547
594
|
}), this.event.emit("message:updated", m), m;
|
|
548
595
|
}
|
|
549
|
-
const n = (
|
|
596
|
+
const n = (a = o.content) == null ? void 0 : a.split("*").map(F).map(N).filter((s) => !!s);
|
|
550
597
|
e.relatedQuesion = n, e.index = -3, e.isLoading = !1;
|
|
551
|
-
const
|
|
598
|
+
const h = y(u({}, e), {
|
|
552
599
|
relatedQuesion: n,
|
|
553
600
|
index: -3,
|
|
554
601
|
isLoading: !1
|
|
555
602
|
});
|
|
556
|
-
return this.event.emit("message:relatedQuesion:loaded",
|
|
603
|
+
return this.event.emit("message:relatedQuesion:loaded", h), this.event.emit("message:updated", h), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
557
604
|
lastRelatedQuestion: n
|
|
558
|
-
}),
|
|
605
|
+
}), h;
|
|
559
606
|
});
|
|
560
607
|
}
|
|
561
608
|
fillMessageReference(e) {
|
|
562
609
|
return c(this, null, function* () {
|
|
563
610
|
try {
|
|
564
|
-
const t = yield Promise.all(e.reference.map((
|
|
565
|
-
e.reference = e.reference.map((
|
|
611
|
+
const t = yield Promise.all(e.reference.map((i) => this.cvforceApi.getKnowledgeIdFileId(i.knowledgeId, i.docId)));
|
|
612
|
+
e.reference = e.reference.map((i, o) => u(u({}, i), t[o]));
|
|
566
613
|
} catch (t) {
|
|
567
|
-
|
|
614
|
+
l.error("Failed to fill message reference:", t);
|
|
568
615
|
}
|
|
569
616
|
return this.event.emit("message:updated", e), e;
|
|
570
617
|
});
|
|
571
618
|
}
|
|
572
619
|
answerAgain() {
|
|
573
620
|
return c(this, arguments, function* (e = {
|
|
574
|
-
stream:
|
|
621
|
+
stream: _.stream
|
|
575
622
|
}) {
|
|
623
|
+
var a;
|
|
576
624
|
const t = this.messages[this.messages.length - 1];
|
|
577
625
|
if (!t)
|
|
578
626
|
throw Error("message list is empty");
|
|
579
|
-
this.frozen = !0, this._stopAnswer = !1
|
|
627
|
+
this.frozen = !0, this._stopAnswer = !1, l.log("answerAgain", {
|
|
628
|
+
sessionId: this._id,
|
|
629
|
+
stream: (a = e.stream) != null ? a : _.stream
|
|
630
|
+
});
|
|
580
631
|
const {
|
|
581
|
-
stream:
|
|
632
|
+
stream: i = _.stream
|
|
582
633
|
} = e;
|
|
583
|
-
let
|
|
584
|
-
t.role !==
|
|
585
|
-
const n = this.messages[this.messages.length - 1],
|
|
634
|
+
let o;
|
|
635
|
+
t.role !== S.User && (o = e.oldMessageId || t.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", t), this.event.emit("history:remove", t));
|
|
636
|
+
const n = this.messages[this.messages.length - 1], h = {
|
|
586
637
|
content: n.content,
|
|
587
638
|
fileIds: n.fileIds,
|
|
588
639
|
requestId: n.messageId,
|
|
589
|
-
stream:
|
|
590
|
-
oldMessageId:
|
|
591
|
-
},
|
|
592
|
-
if (
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
640
|
+
stream: i,
|
|
641
|
+
oldMessageId: o
|
|
642
|
+
}, d = (s) => c(this, null, function* () {
|
|
643
|
+
if (s.role === S.Robot && s.index === -1)
|
|
644
|
+
try {
|
|
645
|
+
switch (this._id && s.content && s.index && s.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
646
|
+
finialMessage: s.content
|
|
647
|
+
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
648
|
+
finialMessage: s.content
|
|
649
|
+
})), yield this.fillMessageReference(s), s.code) {
|
|
650
|
+
case 11915:
|
|
651
|
+
s.content = "请您换个问题问我吧~", s.index = -1, s.isSensitive = !0, s.isLoading = !1, s.sessionId = this._id || "", this.event.emit("message:error", s);
|
|
652
|
+
break;
|
|
653
|
+
case 11916:
|
|
654
|
+
s.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", s.index = -1, s.isSensitive = !0, s.isLoading = !1, s.sessionId = this._id || "", this.event.emit("message:error", s);
|
|
655
|
+
break;
|
|
656
|
+
case 200:
|
|
657
|
+
s.index = -1, s.isLoading = !1;
|
|
658
|
+
break;
|
|
659
|
+
default:
|
|
660
|
+
s.content = s.message || "未知错误", s.isLoading = !1, s.index = -1, this.event.emit("message:error", s);
|
|
661
|
+
}
|
|
662
|
+
this.getRelatedQuesion(s);
|
|
663
|
+
} finally {
|
|
664
|
+
this.removeEventListener("message:content", d), this.frozen = !1;
|
|
609
665
|
}
|
|
610
|
-
this.getRelatedQuesion(i), this.removeEventListener("message:content", r), this.frozen = !1;
|
|
611
|
-
}
|
|
612
666
|
});
|
|
613
|
-
this.addEventListener("message:content",
|
|
614
|
-
stream:
|
|
667
|
+
this.addEventListener("message:content", d), this.sendQuestion(h, y(u({}, e), {
|
|
668
|
+
stream: i
|
|
615
669
|
}));
|
|
616
670
|
});
|
|
617
671
|
}
|
|
@@ -633,21 +687,21 @@ class te {
|
|
|
633
687
|
* @returns 返回当前会话实例
|
|
634
688
|
*/
|
|
635
689
|
suspend() {
|
|
636
|
-
var
|
|
690
|
+
var i;
|
|
637
691
|
this.stopAnswer(), this.frozen = !1, this._stopAnswer = !0;
|
|
638
692
|
let e = this.messages[this.messages.length - 1];
|
|
639
693
|
const t = {
|
|
640
|
-
messageId:
|
|
694
|
+
messageId: Q(),
|
|
641
695
|
// 生成唯一的消息 ID
|
|
642
696
|
isLoading: !1,
|
|
643
697
|
// 标记消息已停止加载
|
|
644
698
|
index: -2,
|
|
645
699
|
// 消息索引,用于标识特殊状态
|
|
646
|
-
messageTime:
|
|
700
|
+
messageTime: Y(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:ss:mm"),
|
|
647
701
|
// 消息时间
|
|
648
702
|
content: "",
|
|
649
703
|
// 消息内容
|
|
650
|
-
role:
|
|
704
|
+
role: S.System,
|
|
651
705
|
// 消息角色为系统消息
|
|
652
706
|
sessionId: this.id || "",
|
|
653
707
|
// 会话 ID
|
|
@@ -660,33 +714,33 @@ class te {
|
|
|
660
714
|
if (e.index === -2)
|
|
661
715
|
return;
|
|
662
716
|
if (e.actions) {
|
|
663
|
-
const
|
|
664
|
-
if (
|
|
665
|
-
const n =
|
|
717
|
+
const o = e.actions[((i = e.actions) == null ? void 0 : i.length) - 1];
|
|
718
|
+
if (o) {
|
|
719
|
+
const n = o[o.length - 1];
|
|
666
720
|
if (n && (n.type === "BEFORE_CALL" || n.type === "FUNCTION_CALL")) {
|
|
667
721
|
const {
|
|
668
|
-
pluginName:
|
|
669
|
-
pluginCode:
|
|
670
|
-
funcCode:
|
|
671
|
-
funcName:
|
|
722
|
+
pluginName: h,
|
|
723
|
+
pluginCode: d,
|
|
724
|
+
funcCode: a,
|
|
725
|
+
funcName: s
|
|
672
726
|
} = n.data;
|
|
673
|
-
|
|
727
|
+
o.push({
|
|
674
728
|
data: {
|
|
675
|
-
pluginName:
|
|
676
|
-
pluginCode:
|
|
677
|
-
funcCode:
|
|
678
|
-
funcName:
|
|
729
|
+
pluginName: h,
|
|
730
|
+
pluginCode: d,
|
|
731
|
+
funcCode: a,
|
|
732
|
+
funcName: s,
|
|
679
733
|
step: n.data.step + 1
|
|
680
734
|
},
|
|
681
735
|
type: "ABORT"
|
|
682
736
|
}), this.event.emit("message:action", {
|
|
683
737
|
messageId: e.messageId,
|
|
684
|
-
action:
|
|
738
|
+
action: o
|
|
685
739
|
});
|
|
686
740
|
}
|
|
687
741
|
}
|
|
688
742
|
}
|
|
689
|
-
e =
|
|
743
|
+
e = u(u({}, e), t), this.event.emit("message:content", e), this.event.emit("message:updated", e);
|
|
690
744
|
} else
|
|
691
745
|
this.pushMessage(t), this.event.emit("message:content", t);
|
|
692
746
|
return this;
|
|
@@ -697,10 +751,10 @@ class te {
|
|
|
697
751
|
* @param targetMessage - 要更新的目标聊天消息对象。
|
|
698
752
|
*/
|
|
699
753
|
handleStreamAnswer(e, t) {
|
|
700
|
-
var
|
|
754
|
+
var h;
|
|
701
755
|
if (!e.data)
|
|
702
756
|
return;
|
|
703
|
-
const
|
|
757
|
+
const i = e.data.type, o = e.data.payload;
|
|
704
758
|
t.isReplying = !0;
|
|
705
759
|
const n = () => {
|
|
706
760
|
if (!t.searchingStatus) {
|
|
@@ -712,25 +766,25 @@ class te {
|
|
|
712
766
|
return;
|
|
713
767
|
}
|
|
714
768
|
};
|
|
715
|
-
if (
|
|
769
|
+
if (i === "error" && (t.index = -1), i === "beforeSearch" && n(), i === "searchOver" && n(), i === "search" && t.searchReference.push(o), i === "thinking") {
|
|
716
770
|
t.thinkingStatus = "start";
|
|
717
|
-
const
|
|
718
|
-
t.reasoningContent = (t.reasoningContent || "") + ((
|
|
771
|
+
const a = o.choices[0];
|
|
772
|
+
t.reasoningContent = (t.reasoningContent || "") + ((h = a.delta.reasoningContent) != null ? h : "");
|
|
719
773
|
}
|
|
720
|
-
if (
|
|
774
|
+
if (i === "thinkingOver") {
|
|
721
775
|
t.thinkingStatus = "end";
|
|
722
|
-
const
|
|
723
|
-
t.thinkingSecs =
|
|
776
|
+
const d = o.thinkingSecs;
|
|
777
|
+
t.thinkingSecs = d;
|
|
724
778
|
}
|
|
725
|
-
if (
|
|
726
|
-
const
|
|
727
|
-
t.content = (t.content || "") +
|
|
779
|
+
if (i === "text") {
|
|
780
|
+
const a = o.choices[0];
|
|
781
|
+
t.content = (t.content || "") + a.delta.content;
|
|
728
782
|
}
|
|
729
|
-
if (
|
|
730
|
-
const
|
|
731
|
-
t.messageId =
|
|
783
|
+
if (i === "reference" && (t.reference = o), i === "updateSession" && o && this.data && (this.data = u(u({}, this.data), o), this.event.emit("data:updated", this.data)), i === "result") {
|
|
784
|
+
const d = o.messageId, a = o.llmModel, s = o.messageTime;
|
|
785
|
+
t.messageId = d, t.llmModel = a, t.messageTime = s;
|
|
732
786
|
}
|
|
733
|
-
|
|
787
|
+
i === "over" && (t.index = -1), e.code != null && (t.code = e.code, t.message = e.message);
|
|
734
788
|
}
|
|
735
789
|
/**
|
|
736
790
|
* 异步提交评论信息
|
|
@@ -754,8 +808,8 @@ class te {
|
|
|
754
808
|
if (!this._applicationId || !this._id)
|
|
755
809
|
throw Error("session is not avalible. id is undefined");
|
|
756
810
|
return this.cvforceApi.postComments(this._applicationId, this._id, e).then((t) => {
|
|
757
|
-
const
|
|
758
|
-
return
|
|
811
|
+
const i = this.messages.find((o) => o.messageId === e.messageId);
|
|
812
|
+
return i && (i.comment = e, this.event.emit("message:comment", i)), t;
|
|
759
813
|
});
|
|
760
814
|
});
|
|
761
815
|
}
|
|
@@ -782,7 +836,7 @@ class te {
|
|
|
782
836
|
}
|
|
783
837
|
}
|
|
784
838
|
export {
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
839
|
+
C as MAX_KNOWLEDGES_AND_FILES_COUNT,
|
|
840
|
+
j as SessionErrors,
|
|
841
|
+
ne as default
|
|
788
842
|
};
|