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