wi-chat 0.6.2 → 0.7.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.
Files changed (78) hide show
  1. package/dist/core/index.cjs +1 -1
  2. package/dist/core/index.d.ts +12 -0
  3. package/dist/core/index.js +731 -328
  4. package/dist/core/useAnswerActions.d.ts +61 -0
  5. package/dist/core/useChat.d.ts +29 -3
  6. package/dist/core/useChatAuth.d.ts +96 -0
  7. package/dist/core/useChatRooms.d.ts +66 -0
  8. package/dist/core/useKeepAlive.d.ts +41 -0
  9. package/dist/core/useLlm.d.ts +73 -0
  10. package/dist/core/useRunningGroups.d.ts +58 -0
  11. package/dist/index.cjs +1 -1
  12. package/dist/index.d.ts +3 -1
  13. package/dist/index.js +119 -108
  14. package/dist/mobile-ui/chat/ChatContainer.vue.d.ts +2 -2
  15. package/dist/mobile-ui/inputbox/Inputbox.vue.d.ts +1 -1
  16. package/dist/parser/answerCom/generic/parseGeneric.d.ts +52 -0
  17. package/dist/parser/answerCom/generic/types.d.ts +75 -0
  18. package/dist/parser/answerCom/outputFile/parseOutputFile.d.ts +7 -6
  19. package/dist/parser/answerCom/outputFile/types.d.ts +15 -5
  20. package/dist/parser/answerCom/presentFiles/parsePresentFiles.d.ts +58 -0
  21. package/dist/parser/answerCom/presentFiles/types.d.ts +27 -0
  22. package/dist/parser/answerMeta.d.ts +71 -0
  23. package/dist/parser/deepThinking/parseDeepThinking.d.ts +8 -0
  24. package/dist/parser/history/history.d.ts +9 -0
  25. package/dist/parser/history/index.d.ts +1 -1
  26. package/dist/parser/index.cjs +6 -2
  27. package/dist/parser/index.d.ts +11 -2
  28. package/dist/parser/index.js +591 -366
  29. package/dist/parser/message.d.ts +9 -0
  30. package/dist/parser/runTime.d.ts +29 -0
  31. package/dist/renderers/TextAnswer-BgKGvG_o.cjs +2 -0
  32. package/dist/renderers/TextAnswer-PQwV3K7v.js +5 -0
  33. package/dist/renderers/answerCard/AnswerCard.vue.d.ts +65 -0
  34. package/dist/renderers/answerCard/AnswerToolbar.vue.d.ts +42 -0
  35. package/dist/renderers/answerConfig.d.ts +33 -0
  36. package/dist/renderers/deviceLastValue/DeviceLastValue.vue.d.ts +37 -0
  37. package/dist/renderers/index.cjs +6 -1
  38. package/dist/renderers/index.d.ts +14 -0
  39. package/dist/renderers/index.js +1485 -574
  40. package/dist/renderers/media/MediaAnswer.vue.d.ts +19 -0
  41. package/dist/renderers/outputFile/OutputFileLink.vue.d.ts +19 -0
  42. package/dist/renderers/outputFile/localFile.d.ts +46 -0
  43. package/dist/renderers/outputFile/useOutputFile.d.ts +32 -3
  44. package/dist/renderers/presentFiles/PresentFiles.vue.d.ts +19 -0
  45. package/dist/renderers/recordset/RecordsetView.vue.d.ts +19 -0
  46. package/dist/renderers/sqlQuery/SqlQuery.vue.d.ts +19 -0
  47. package/dist/renderers/style.css +1 -1
  48. package/dist/renderers/text/TextAnswer.vue.d.ts +19 -0
  49. package/dist/request/api.d.ts +121 -1
  50. package/dist/request/client.d.ts +6 -0
  51. package/dist/request/index.cjs +4 -2
  52. package/dist/request/index.d.ts +4 -2
  53. package/dist/request/index.js +753 -173
  54. package/dist/request/ws.d.ts +11 -86
  55. package/dist/request/wsCore.d.ts +104 -0
  56. package/dist/request/wsSession.d.ts +353 -0
  57. package/dist/right-panel/RightPanelTabs.vue.d.ts +4 -0
  58. package/dist/right-panel/index.cjs +1 -1
  59. package/dist/right-panel/index.d.ts +1 -1
  60. package/dist/right-panel/index.js +598 -499
  61. package/dist/right-panel/style.css +1 -1
  62. package/dist/right-panel/types.d.ts +10 -0
  63. package/dist/tools/index.cjs +5 -5
  64. package/dist/tools/index.d.ts +2 -2
  65. package/dist/tools/index.js +323 -208
  66. package/dist/tools/markdown/index.d.ts +2 -0
  67. package/dist/tools/markdown/interactions.d.ts +39 -0
  68. package/dist/tools/markdown/sanitize.d.ts +6 -0
  69. package/dist/tools/style.css +1 -1
  70. package/dist/web-ui/attach/AttachmentCard.vue.d.ts +1 -1
  71. package/dist/web-ui/attach/AttachmentList.vue.d.ts +1 -1
  72. package/dist/web-ui/chat/ChatContainer.vue.d.ts +5 -5
  73. package/dist/web-ui/envswitch/EnvSwitch.vue.d.ts +1 -1
  74. package/dist/web-ui/index.cjs +1 -1
  75. package/dist/web-ui/index.js +1 -1
  76. package/dist/web-ui/inputbox/Inputbox.vue.d.ts +5 -5
  77. package/dist/web-ui/style.css +1 -1
  78. package/package.json +2 -2
@@ -1,472 +1,875 @@
1
- import { ref as p, getCurrentInstance as oe, onBeforeUnmount as Le, reactive as se, onMounted as xe, watch as re } from "vue";
2
- import { clearPendingClarifications as Ae, loadGroupMessagesByStep as ne, createHistoryGroup as ie, nextLocalMessageId as le, createEmptyAnswerValues as Ce, buildChatTitle as Ie, consumeStream as _e, applyAnswerChunk as be, collectThinkingSteps as Re, expirePendingClarificationsForReply as Ee, loadGroupMessages as Se, deleteHistoryGroup as qe, setHistoryGroupTitle as Fe, getHistoryGroups as Me, resolveRemoteFileUrl as Qe } from "wi-chat/parser";
3
- import { createRequest as Ue, createChatApi as Pe, login as Oe, isCancelError as Te } from "wi-chat/request";
4
- const Ke = (e) => {
5
- const l = {
1
+ import { ref as b, getCurrentInstance as te, onBeforeUnmount as we, computed as X, getCurrentScope as pe, onScopeDispose as me, reactive as V, onMounted as Fe, shallowReactive as Pe, effectScope as Qe, watch as ue } from "vue";
2
+ import { createEmptyAnswerValues as ce, nextLocalMessageId as fe, buildChatTitle as De, createHistoryGroup as Se, loadGroupMessagesByStep as de, clearPendingClarifications as He, consumeStream as Ne, parseServerStartTime as Je, normalizeAnswer as ke, collectThinkingSteps as ve, applyAnswerChunk as je, expirePendingClarificationsForReply as We, mergeAnswerMeta as ze, readAnswerMeta as Ae, loadGroupMessages as Be, deleteHistoryGroup as Ve, setHistoryGroupTitle as $e, getHistoryGroups as Ye, resolveRemoteFileUrl as Xe, collectAnswerMarkdown as ye, markdownToPlainText as Ze } from "wi-chat/parser";
3
+ import { AuthExpiredError as et, createRequest as $, createChatApi as _e, login as Y, isCancelError as tt } from "wi-chat/request";
4
+ const nt = (e) => {
5
+ const { api: r, interval: g = 5 * 60 * 1e3, renew: f, onExpired: l, immediate: h = !0 } = e, c = b(!1), o = b(0);
6
+ let x = null, L = null;
7
+ const p = async () => {
8
+ const d = f ? await f().catch(() => !1) : !1;
9
+ return d || l == null || l(), d;
10
+ }, i = () => L || (L = (async () => {
11
+ if (!r.pingLogin) return !0;
12
+ try {
13
+ const d = await r.pingLogin();
14
+ return o.value = Date.now(), (d == null ? void 0 : d.is_login) === !1 ? p() : !0;
15
+ } catch (d) {
16
+ return d instanceof et ? p() : !0;
17
+ } finally {
18
+ L = null;
19
+ }
20
+ })(), L), t = () => {
21
+ x || (c.value = !0, x = setInterval(() => void i(), g));
22
+ }, s = () => {
23
+ x && clearInterval(x), x = null, c.value = !1;
24
+ };
25
+ return h && t(), te() && we(s), { active: c, lastPingAt: o, start: t, stop: s, pingNow: i };
26
+ }, H = {
27
+ get(e) {
28
+ try {
29
+ return typeof localStorage < "u" ? localStorage.getItem(e) : null;
30
+ } catch {
31
+ return null;
32
+ }
33
+ },
34
+ set(e, r) {
35
+ try {
36
+ typeof localStorage < "u" && localStorage.setItem(e, r);
37
+ } catch {
38
+ }
39
+ },
40
+ remove(e) {
41
+ try {
42
+ typeof localStorage < "u" && localStorage.removeItem(e);
43
+ } catch {
44
+ }
45
+ }
46
+ }, rt = (e) => btoa(unescape(encodeURIComponent(e))), at = (e) => decodeURIComponent(escape(atob(e))), be = (e) => {
47
+ let r = null;
48
+ return () => (r || (r = Promise.resolve().then(e).catch(() => !1).finally(() => {
49
+ setTimeout(() => r = null, 0);
50
+ })), r);
51
+ }, st = (e) => ({
52
+ get() {
53
+ const r = H.get(e);
54
+ if (!r) return null;
55
+ try {
56
+ const g = JSON.parse(at(r));
57
+ return g != null && g.user && (g != null && g.pass) ? g : null;
58
+ } catch {
59
+ return null;
60
+ }
61
+ },
62
+ set(r) {
63
+ H.set(e, rt(JSON.stringify(r)));
64
+ },
65
+ clear() {
66
+ H.remove(e);
67
+ }
68
+ }), wt = (e) => {
69
+ const r = e.sessionKey === void 0 ? "wi-chat-session" : e.sessionKey, g = e.remember ?? !1;
70
+ let f = null;
71
+ const l = e.credentialStore ?? (g && r ? st(`${r}-account`) : {
72
+ get: () => f,
73
+ set: (A) => {
74
+ f = A;
75
+ },
76
+ clear: () => {
77
+ f = null;
78
+ }
79
+ }), h = r ? `${r}-user` : null, c = b(r ? H.get(r) ?? "" : ""), o = b(null);
80
+ if (h)
81
+ try {
82
+ o.value = JSON.parse(H.get(h) || "null");
83
+ } catch {
84
+ o.value = null;
85
+ }
86
+ const x = X(() => !!c.value), L = (A) => {
87
+ c.value = A.wisession || "", o.value = A, r && H.set(r, c.value), h && H.set(h, JSON.stringify(A));
88
+ }, p = () => {
89
+ c.value = "", o.value = null, r && H.remove(r), h && H.remove(h);
90
+ }, i = $({ baseURL: e.baseURL });
91
+ let t = 0;
92
+ const s = be(async () => {
93
+ const A = t;
94
+ try {
95
+ const q = await l.get();
96
+ if (!q) return !1;
97
+ const T = await Y(i, { ...q, client: e.client });
98
+ return A !== t ? !1 : (L(T), !0);
99
+ } catch {
100
+ return !1;
101
+ }
102
+ }), d = $({
103
+ baseURL: e.baseURL,
104
+ getWisession: () => c.value,
105
+ onAuthExpired: async () => {
106
+ const A = await s();
107
+ return A || y(), A;
108
+ }
109
+ }), a = _e(d, {
110
+ resumable: e.resumable ?? !0,
111
+ // 对话 WS 收到 user not login:用记住的账号静默重登,失败则按彻底失效处理
112
+ refreshSession: async () => {
113
+ const A = await s();
114
+ return A || y(), A;
115
+ }
116
+ }), u = nt({
117
+ api: a,
118
+ interval: e.pingInterval || 5 * 60 * 1e3,
119
+ // ping 的请求拦截器已先行续期并(失败时)清过登录态,这里不重复登录
120
+ renew: async () => c.value ? s() : !1,
121
+ onExpired: () => y(),
122
+ immediate: !1
123
+ }), y = () => {
124
+ var A;
125
+ c.value && (p(), u.stop(), (A = e.onExpired) == null || A.call(e));
126
+ }, G = () => {
127
+ e.pingInterval !== 0 && u.start();
128
+ }, E = async (A, q, T = {}) => {
129
+ const k = { ...T, user: A, pass: q, client: e.client }, O = await Y(i, k);
130
+ return L(O), await l.set({ user: A, pass: q }), G(), O;
131
+ }, I = async () => {
132
+ const A = await s();
133
+ return A && G(), A;
134
+ }, P = async (A = !0) => {
135
+ t += 1, p(), u.stop(), A && await l.clear();
136
+ };
137
+ return c.value && (G(), u.pingNow()), { session: c, userInfo: o, isLoggedIn: x, request: d, api: a, keepAlive: u, login: E, autoLogin: I, renew: s, logout: P };
138
+ }, lt = (e) => {
139
+ const r = {
6
140
  requestId: e,
7
141
  sender: null,
8
142
  queue: [],
9
143
  closed: !1,
10
- reply: (h) => ce(l, h)
144
+ reply: (g) => xe(r, g)
11
145
  };
12
- return l;
13
- }, ce = (e, l, h) => {
146
+ return r;
147
+ }, xe = (e, r, g) => {
14
148
  if (e.closed) return !1;
15
- const w = typeof l == "string" ? l : JSON.stringify(l) ?? "";
16
- return e.sender ? e.sender(w, h) !== !1 : (e.queue.push({ payloadText: w, chunkSize: h }), !0);
17
- }, ue = (e) => {
18
- e.closed || (e.closed = !0, e.queue.length = 0, e.sender = null, Ee(e.reply));
19
- }, ke = (e) => {
20
- const l = e.login, h = e.defaultGroupTitle ?? "新对话", w = e.lastCount ?? 10, u = e.autoSetGroupTitleOnFirstQuestion ?? !0, L = {
149
+ const f = typeof r == "string" ? r : JSON.stringify(r) ?? "";
150
+ return e.sender ? e.sender(f, g) !== !1 : (e.queue.push({ payloadText: f, chunkSize: g }), !0);
151
+ }, ge = (e) => {
152
+ e.closed || (e.closed = !0, e.queue.length = 0, e.sender = null, We(e.reply));
153
+ }, Le = (e) => {
154
+ const r = e.login, g = e.defaultGroupTitle ?? "新对话", f = e.lastCount ?? 10, l = e.autoSetGroupTitleOnFirstQuestion ?? !0, h = {
21
155
  beforeKnowledgeRender: e.beforeKnowledgeRender,
22
156
  parsers: e.parsers,
23
157
  actionExecute: {
24
158
  ...e.actionExecute,
25
159
  reply: () => !1
26
160
  }
27
- }, v = p(""), o = p([]), C = p(""), Q = p(e.incPrevQuestion ?? "Y"), s = p(!1), i = p(!1), t = p(""), n = p(!1), m = p(""), d = p(!0), y = p(!1), A = p(0), W = e.api ? null : Ue({
161
+ }, c = b(""), o = b([]), x = b(""), L = b(e.incPrevQuestion ?? "Y"), p = b(!1), i = b(!1), t = b(""), s = b(!1), d = b(""), a = b(!0), u = b(!1), y = b(0), G = e.api ? null : $({
28
162
  baseURL: e.baseURL ?? "",
29
- getWisession: () => v.value
30
- }), U = e.api ?? Pe(W);
31
- let I = null, R = null, D = 0, _ = 0, j = 0, E = null;
32
- const V = async () => {
33
- if (m.value = "", !W || !l) {
34
- n.value = !0;
163
+ getWisession: () => c.value,
164
+ onAuthExpired: () => P()
165
+ }), E = e.api ?? _e(G, { resumable: e.resumable, refreshSession: () => P() }), I = G ? $({ baseURL: e.baseURL ?? "" }) : null, P = be(async () => {
166
+ if (e.onAuthExpired) return e.onAuthExpired();
167
+ if (!G || !r || !I) return !1;
168
+ const n = await Y(I, r);
169
+ return c.value = n.wisession || "", !!c.value;
170
+ }), A = (n, _) => {
171
+ var w;
172
+ n && ((w = e.onRunningChange) == null || w.call(e, n, _));
173
+ };
174
+ let q = null, T = null, k = 0, O = 0, m = 0, S = null;
175
+ const D = async () => {
176
+ if (d.value = "", !G || !r) {
177
+ s.value = !0;
35
178
  return;
36
179
  }
37
180
  try {
38
- const r = await Oe(W, l);
39
- return v.value = r.wisession || "", n.value = !0, r;
40
- } catch (r) {
41
- throw m.value = r instanceof Error ? r.message : String(r), r;
42
- }
43
- }, a = (r) => r === D, f = (r) => {
44
- typeof URL > "u" || typeof URL.revokeObjectURL != "function" || r.forEach((G) => {
45
- G.role !== "user" || !G.attachments || G.attachments.forEach((x) => {
46
- var b;
47
- const S = (b = x.attachContent) == null ? void 0 : b.url;
48
- S && S.startsWith("blob:") && URL.revokeObjectURL(S);
181
+ const n = await Y(I ?? G, r);
182
+ return c.value = n.wisession || "", s.value = !0, n;
183
+ } catch (n) {
184
+ throw d.value = n instanceof Error ? n.message : String(n), n;
185
+ }
186
+ }, K = (n) => n === k, W = (n) => {
187
+ typeof URL > "u" || typeof URL.revokeObjectURL != "function" || n.forEach((_) => {
188
+ _.role !== "user" || !_.attachments || _.attachments.forEach((w) => {
189
+ var R;
190
+ const M = (R = w.attachContent) == null ? void 0 : R.url;
191
+ M && M.startsWith("blob:") && URL.revokeObjectURL(M);
49
192
  });
50
193
  });
51
- }, P = (r) => ({
194
+ }, Ge = (n) => ({
52
195
  beforeKnowledgeRender: e.beforeKnowledgeRender,
53
196
  parsers: e.parsers,
54
197
  actionExecute: {
55
198
  ...e.actionExecute,
56
- reply: r.reply
57
- }
58
- }), F = async (r) => {
59
- const G = ++_;
60
- j += 1, s.value = !0, f(o.value), E && Ae(E.reply), o.value = [], t.value = r, d.value = !0, y.value = !1, A.value = 0;
61
- const x = [], S = () => {
62
- const g = [];
63
- x.forEach((O) => {
64
- g.push(O.user), O.assistant && g.push(O.assistant);
65
- }), G === _ && (o.value = g);
66
- }, b = (g) => {
67
- G === _ && (g.type === "users" ? (x.length = 0, g.users.forEach((O) => x.push({ user: O }))) : x[g.index] && (g.user && (x[g.index].user = g.user), x[g.index].assistant = g.assistant), S());
199
+ reply: n.reply
200
+ }
201
+ }), Re = async (n) => {
202
+ const _ = ++O;
203
+ m += 1, p.value = !0, W(o.value), S && He(S.reply), o.value = [], t.value = n, a.value = !0, u.value = !1, y.value = 0;
204
+ const w = [], M = () => {
205
+ const v = [];
206
+ w.forEach((F) => {
207
+ v.push(F.user), F.assistant && v.push(F.assistant);
208
+ }), _ === O && (o.value = v);
209
+ }, R = (v) => {
210
+ _ === O && (v.type === "users" ? (w.length = 0, v.users.forEach((F) => w.push({ user: F }))) : w[v.index] && (v.user && (w[v.index].user = v.user), w[v.index].assistant = v.assistant), M());
68
211
  };
69
212
  try {
70
- await ne(U, r, w, b, 0, L), S(), G === _ && (A.value = x.length, x.length < w && (d.value = !1));
213
+ await de(E, n, f, R, 0, h), M(), _ === O && (y.value = w.length, w.length < f && (a.value = !1));
71
214
  } finally {
72
- j -= 1, j <= 0 && (j = 0, s.value = !1);
215
+ m -= 1, m <= 0 && (m = 0, p.value = !1);
73
216
  }
74
- }, Y = async () => {
75
- if (!t.value || s.value || !d.value || y.value) return;
76
- const r = t.value, G = _, x = A.value;
77
- y.value = !0;
217
+ }, Ce = async () => {
218
+ if (!t.value || p.value || !a.value || u.value) return;
219
+ const n = t.value, _ = O, w = y.value;
220
+ u.value = !0;
78
221
  try {
79
- const S = await ne(U, r, w, void 0, x, L), b = S.filter((g) => g.role === "user").length;
80
- if (b < w && (d.value = !1), G === _ && t.value === r) {
81
- const g = /* @__PURE__ */ new Set();
82
- o.value.forEach((q) => {
83
- q.historyId && g.add(q.historyId), q.id && g.add(q.id);
222
+ const M = await de(E, n, f, void 0, w, h), R = M.filter((v) => v.role === "user").length;
223
+ if (R < f && (a.value = !1), _ === O && t.value === n) {
224
+ const v = /* @__PURE__ */ new Set();
225
+ o.value.forEach((C) => {
226
+ C.historyId && v.add(C.historyId), C.id && v.add(C.id);
84
227
  });
85
- const O = S.filter((q) => !(q.historyId && g.has(q.historyId) || q.id && g.has(q.id)));
86
- o.value = [...O, ...o.value], A.value += b;
228
+ const F = M.filter((C) => !(C.historyId && v.has(C.historyId) || C.id && v.has(C.id)));
229
+ o.value = [...F, ...o.value], y.value += R;
87
230
  }
88
231
  } finally {
89
- y.value = !1;
90
- }
91
- }, de = async (r = h) => {
92
- const G = await ie(U, r);
93
- return t.value = G, G;
94
- }, Z = () => {
95
- D += 1, _ += 1, R && (R.stopped = !0, R.thinking.expanded = !1), I == null || I.abort(), I = null, E && ue(E), E = null, R = null, i.value = !1;
96
- }, fe = () => {
97
- R && (R.error = !0, R.thinking.expanded = !1);
98
- }, ge = async (r) => {
99
- var ee, te;
100
- const G = typeof r == "string" ? { text: r } : r || {}, { text: x, entryId: S, files: b, attachments: g, extArgs: O, ...q } = G, B = (x !== void 0 ? x : C.value).trim(), pe = O ?? e.extArgs ?? {}, he = b && b.length > 0 ? b : (g ?? []).map((k) => {
101
- var z;
102
- return (z = k.attachContent) == null ? void 0 : z.raw;
103
- }).filter((k) => !!k);
104
- if (!B || i.value || !t.value) return;
105
- const T = D + 1;
106
- D = T, _ += 1;
107
- const K = Ke(T);
108
- E = K;
109
- const we = P(K);
110
- let $ = !1;
232
+ u.value = !1;
233
+ }
234
+ }, Ee = async (n = g) => {
235
+ const _ = await Se(E, n);
236
+ return t.value = _, _;
237
+ }, ne = (n = !0) => {
238
+ var _;
239
+ n !== !1 && i.value && t.value && ((_ = E.stopChat) == null || _.call(E, t.value)), k += 1, O += 1, T && (T.stopped = !0, T.thinking.expanded = !1, Z(T)), q == null || q.abort(), q = null, S && ge(S), S = null, T = null, i.value = !1;
240
+ }, Ie = () => {
241
+ T && (T.error = !0, T.thinking.expanded = !1, Z(T));
242
+ }, re = async (n, _, w, M) => {
243
+ const R = k + 1;
244
+ k = R, O += 1;
245
+ const v = lt(R);
246
+ S = v;
247
+ const F = Ge(v), C = M || t.value;
248
+ T = n, i.value = !0, A(C, !0);
249
+ let Q = !1;
111
250
  try {
112
- i.value = !0;
113
- const k = t.value, z = g || (b || []).map((c) => {
114
- var H;
115
- const J = c.name.includes(".") && ((H = c.name.split(".").pop()) == null ? void 0 : H.toLowerCase()) || "";
116
- return {
117
- type: "file",
118
- name: c.name,
119
- description: J ? J.toUpperCase() : "",
120
- attachContent: {
121
- file_name: c.name,
122
- size: c.size,
123
- suffix: J,
124
- raw: c
125
- }
126
- };
127
- }), me = A.value === 0 && !o.value.some((c) => c.role === "user"), X = k, ae = se({
128
- id: le("user"),
129
- role: "user",
130
- content: B,
131
- attachments: z.length > 0 ? z : void 0
132
- });
133
- o.value.push(ae);
134
- const M = se({
135
- id: le("answer"),
136
- role: "assistant",
137
- answer: Ce("", B),
138
- error: !1,
139
- stopped: !1,
140
- thinking: { steps: [], expanded: !0 }
141
- });
142
- if (R = M, o.value.push(M), C.value = "", (ee = e.onSubmitQuestion) == null || ee.call(e, B), me && X) {
143
- const c = Ie(B);
144
- e.onFirstQuestion ? e.onFirstQuestion({ question: c, groupId: X }) : u && c && U.setHistoryGroupTitle(X, c).catch(() => {
145
- });
251
+ q = new AbortController();
252
+ const z = await w((j) => {
253
+ if (!K(R) || v.closed) return;
254
+ v.sender = j;
255
+ const N = v.queue;
256
+ v.queue = [], N.forEach(({ payloadText: B, chunkSize: ee }) => j(B, ee));
257
+ }, q.signal);
258
+ await Ne(z, (j) => {
259
+ var N;
260
+ K(R) && (Me(n, _, j, F), (N = e.onChunk) == null || N.call(e, j));
261
+ }), K(R) && (Q = !0, n.thinking.expanded = !1, Z(n));
262
+ } catch (z) {
263
+ K(R) && !tt(z) && Ie();
264
+ } finally {
265
+ ge(v), S === v && (S = null), A(C, !1), K(R) && (i.value = !1, q = null, T = null);
266
+ }
267
+ return Q && Ue(n), Q;
268
+ }, Me = (n, _, w, M) => {
269
+ if (w.mode === "history_id") {
270
+ const v = typeof w.value == "object" && w.value !== null ? w.value.history_id : w.value;
271
+ v && (_ && (_.historyId = String(v)), n.historyId = String(v), n.answer.id = String(v));
272
+ return;
273
+ }
274
+ if (w.mode === "start") {
275
+ const v = Je(w.value ?? w.time, n.runStartAt);
276
+ v != null && n.runStartAt != null && (n.runStartAt = v);
277
+ return;
278
+ }
279
+ if (w.mode === "history_answer") {
280
+ const v = ke(w.value, n.historyId ?? "", n.answer.question ?? "", h);
281
+ n.answer = v, n.thinking.steps = ve(v);
282
+ return;
283
+ }
284
+ je(n.answer, w, M).includes("deep_thinking") && (n.thinking.steps = ve(n.answer)), w.mode === "finish" && (n.thinking.expanded = !1);
285
+ }, Z = (n) => {
286
+ !n || n.runStartAt == null || (n.runMs = Math.max(0, Date.now() - n.runStartAt), delete n.runStartAt);
287
+ }, Ue = async (n) => {
288
+ if (!(!e.syncMetaOnFinish || !n.historyId))
289
+ try {
290
+ const _ = await E.getHistoryAnswer(n.historyId);
291
+ ze(n.answer, _);
292
+ const w = Ae(n.answer).runMs;
293
+ w != null && (n.runMs = w);
294
+ } catch {
146
295
  }
147
- I = new AbortController();
148
- const Ge = await U.questionStream(
296
+ }, qe = async (n) => {
297
+ var le, oe;
298
+ const _ = typeof n == "string" ? { text: n } : n || {}, { text: w, entryId: M, files: R, attachments: v, extArgs: F, ...C } = _, Q = (w !== void 0 ? w : x.value).trim(), z = F ?? e.extArgs ?? {}, j = R && R.length > 0 ? R : (v ?? []).map((U) => {
299
+ var J;
300
+ return (J = U.attachContent) == null ? void 0 : J.raw;
301
+ }).filter((U) => !!U);
302
+ if (!Q || i.value || !t.value) return;
303
+ const N = t.value, B = v || (R || []).map((U) => {
304
+ var ie;
305
+ const J = U.name.includes(".") && ((ie = U.name.split(".").pop()) == null ? void 0 : ie.toLowerCase()) || "";
306
+ return {
307
+ type: "file",
308
+ name: U.name,
309
+ description: J ? J.toUpperCase() : "",
310
+ attachContent: { file_name: U.name, size: U.size, suffix: J, raw: U }
311
+ };
312
+ }), ee = y.value === 0 && !o.value.some((U) => U.role === "user"), ae = V({
313
+ id: fe("user"),
314
+ role: "user",
315
+ content: Q,
316
+ attachments: B.length > 0 ? B : void 0
317
+ });
318
+ o.value.push(ae);
319
+ const se = V({
320
+ id: fe("answer"),
321
+ role: "assistant",
322
+ answer: ce("", Q),
323
+ error: !1,
324
+ stopped: !1,
325
+ runStartAt: Date.now(),
326
+ thinking: { steps: [], expanded: !0 }
327
+ });
328
+ if (o.value.push(se), x.value = "", (le = e.onSubmitQuestion) == null || le.call(e, Q), ee) {
329
+ const U = De(Q);
330
+ e.onFirstQuestion ? e.onFirstQuestion({ question: U, groupId: N }) : l && U && E.setHistoryGroupTitle(N, U).catch(() => {
331
+ });
332
+ }
333
+ y.value += 1, await re(
334
+ se,
335
+ ae,
336
+ (U, J) => E.questionStream(
149
337
  {
150
- // 先铺开外部透传的额外字段(raw_mode / tables / metrics / group_type 等),
151
- // 已知字段随后覆盖,避免同名额外字段篡改核心参数
152
- ...q,
153
- question: B,
154
- historyGroupId: k,
155
- entryId: S,
156
- files: he,
157
- ext_args: JSON.stringify(pe),
158
- incPrevQuestion: Q.value,
159
- // 连接建立且首包发完后接收分片发送器,并按序补发连接建立前入队的追发消息
160
- // (sendWsMessage / wait_reply 在连接未就绪时调用会先入队,见 sendViaChannel)
161
- onOpen: (c) => {
162
- if (!a(T) || K.closed) return;
163
- K.sender = c;
164
- const J = K.queue;
165
- K.queue = [], J.forEach(({ payloadText: H, chunkSize: N }) => c(H, N));
166
- }
338
+ // 先铺开外部透传的额外字段,已知字段随后覆盖,避免同名额外字段篡改核心参数
339
+ ...C,
340
+ question: Q,
341
+ historyGroupId: N,
342
+ entryId: M,
343
+ files: j,
344
+ ext_args: JSON.stringify(z),
345
+ incPrevQuestion: L.value,
346
+ onOpen: U
167
347
  },
168
- I.signal
169
- );
170
- A.value += 1, await _e(Ge, (c) => {
171
- var H;
172
- if (!a(T)) return;
173
- if (c.mode === "history_id") {
174
- const N = typeof c.value == "object" && c.value !== null ? c.value.history_id : c.value;
175
- N && (ae.historyId = String(N), M.historyId = String(N), M.answer && (M.answer.id = String(N)));
176
- }
177
- be(M.answer, c, we).includes("deep_thinking") && (M.thinking.steps = Re(M.answer)), c.mode === "finish" && (M.thinking.expanded = !1), (H = e.onChunk) == null || H.call(e, c);
178
- }), a(T) && (M.thinking.expanded = !1), a(T) && ($ = !0);
179
- } catch (k) {
180
- a(T) && !Te(k) && fe();
181
- } finally {
182
- ue(K), E === K && (E = null), a(T) && (i.value = !1, I = null, R = null);
183
- }
184
- $ && ((te = e.onFinish) == null || te.call(e));
185
- }, ve = (r, G) => !i.value || !E ? !1 : ce(E, r, G), ye = e.autoLogin !== !1 ? V().then(() => {
348
+ J
349
+ )
350
+ ) && ((oe = e.onFinish) == null || oe.call(e));
351
+ }, Te = async (n) => {
352
+ var F;
353
+ if (i.value || !(n != null && n.history_id) || t.value && n.history_group_id && n.history_group_id !== t.value) return !1;
354
+ !t.value && n.history_group_id && (t.value = n.history_group_id);
355
+ const _ = o.value.find((C) => C.role === "user" && C.historyId === n.history_id);
356
+ o.value = o.value.filter((C) => !(C.role === "assistant" && C.historyId === n.history_id));
357
+ let w = _ ?? null;
358
+ w || (w = V({
359
+ id: n.history_id,
360
+ historyId: n.history_id,
361
+ role: "user",
362
+ content: n.question ?? ""
363
+ }), o.value.push(w), y.value += 1);
364
+ const M = V({
365
+ id: `${n.history_id}-answer`,
366
+ historyId: n.history_id,
367
+ role: "assistant",
368
+ answer: ce(n.history_id, n.question ?? ""),
369
+ error: !1,
370
+ stopped: !1,
371
+ runStartAt: Date.now(),
372
+ thinking: { steps: [], expanded: !0 }
373
+ }), R = o.value.indexOf(w);
374
+ o.value.splice(R + 1, 0, M);
375
+ const v = await re(
376
+ M,
377
+ w,
378
+ (C, Q) => E.questionStream(
379
+ {
380
+ question: n.question ?? "",
381
+ historyGroupId: n.history_group_id || t.value,
382
+ resumeTask: n,
383
+ onOpen: C
384
+ },
385
+ Q
386
+ ),
387
+ n.history_group_id || t.value
388
+ );
389
+ return v && ((F = e.onFinish) == null || F.call(e)), v;
390
+ }, Oe = (n, _) => !i.value || !S ? !1 : xe(S, n, _), Ke = e.autoLogin !== !1 ? D().then(() => {
186
391
  }).catch(() => {
187
392
  }) : Promise.resolve();
188
- return oe() && Le(() => {
189
- Z(), f(o.value);
393
+ return pe() && me(() => {
394
+ ne(!1), W(o.value);
190
395
  }), {
191
396
  /** 当前分组的消息列表(user / assistant 交错;loadGroup / loadOlder / submitQuestion 会更新) */
192
397
  messages: o,
193
398
  /** 输入框内容(v-model 绑定;submitQuestion 成功后清空) */
194
- question: C,
399
+ question: x,
195
400
  /**
196
401
  * 提问是否携带之前对话上下文(inc_prev_question:'Y' 不忽略 / 'N' 忽略,默认 'Y')。
197
402
  * submitQuestion 提交时读取当前值发送;可在外部随时修改(如绑定输入框组件的勾选开关)。
198
403
  */
199
- incPrevQuestion: Q,
404
+ incPrevQuestion: L,
200
405
  /** 会话发送 / 流式回答进行中(true 时 submitQuestion 不再受理新提问) */
201
406
  talking: i,
202
407
  /** 历史加载中(首屏 loadGroup 及并发加载全部完成后结束) */
203
- loading: s,
408
+ loading: p,
204
409
  /** 当前会话 token(自动登录写入;未自动登录时可自行赋值) */
205
- session: v,
410
+ session: c,
206
411
  /** 自动登录流程已结束(成功 / 失败 / 跳过均置 true;UI 可等待它再渲染登录状态) */
207
- autoLoginDone: n,
412
+ autoLoginDone: s,
208
413
  /** 最近一次自动登录的错误信息(UI 可据此提示重试) */
209
- autoLoginError: m,
414
+ autoLoginError: d,
210
415
  /** 自动登录完成时机(无论成败都会 resolve;宿主用它编排"登录后才能发"的请求,如加载历史分组) */
211
- loginDone: ye,
416
+ loginDone: Ke,
212
417
  /** 当前会话所在的历史分组 id */
213
418
  historyGroupId: t,
214
419
  /** 内部自建的 request 客户端(注入 api 时为 null,无使用价值) */
215
- selfRequest: W,
420
+ selfRequest: G,
216
421
  /** 实际使用的业务 API(外部注入的实例或内部自建) */
217
- api: U,
422
+ api: E,
218
423
  /** 手动触发一次自动登录(成功返回登录结果;失败抛错并记录到 autoLoginError) */
219
- doAutoLogin: V,
424
+ doAutoLogin: D,
220
425
  /** 按分组加载消息(插空式渐进渲染;并发调用时仅最新一次生效) */
221
- loadGroup: F,
426
+ loadGroup: Re,
222
427
  /** 加载更早一页历史(向上翻页),前插到消息列表顶部 */
223
- loadOlder: Y,
428
+ loadOlder: Ce,
224
429
  /** 是否仍可能有更早历史(向上翻页) */
225
- hasMore: d,
430
+ hasMore: a,
226
431
  /** 向上翻页加载中(触顶防重) */
227
- loadMoreLoading: y,
432
+ loadMoreLoading: u,
228
433
  /** 新建历史分组并选中(返回分组 id) */
229
- createGroup: de,
434
+ createGroup: Ee,
230
435
  /** 提交问题并走 SSE 流式问答(支持传参 entryId、files 与文本;无文本 / 发送中 / 无分组时不发送) */
231
- submitQuestion: ge,
436
+ submitQuestion: qe,
232
437
  /**
233
438
  * 向当前提问的 WebSocket 连接追发自定义消息(B/M/E 分帧):
234
439
  * 连接就绪立即发出、未就绪入队待连接建立后按序补发;仅在 talking 期间受理,
235
440
  * 无在途会话返回 false。见 sendWsMessage 的 JSDoc。
236
441
  */
237
- sendWsMessage: ve,
238
- /** 停止在途会话:中断流、把当前回答标记为 stopped,并复位 talking */
239
- stopQuestion: Z
442
+ sendWsMessage: Oe,
443
+ /** 停止在途会话:向后端发 STOP(可续传模式)、中断流、把当前回答标记为 stopped,并复位 talking */
444
+ stopQuestion: ne,
445
+ /** 接管当前分组正在进行的任务(刷新 / 切回房间后 CONNECT 重放),需 resumable */
446
+ resumeTask: Te,
447
+ /** 手动触发一次登录态续期(并发调用合并为一次) */
448
+ renewSession: P
240
449
  };
241
- }, De = "wi-chat-history-groups", He = (e) => {
242
- var V;
450
+ }, ot = "wi-chat-history-groups", it = (e) => {
451
+ var O;
243
452
  const {
244
- api: l,
245
- defaultGroupTitle: h = "新对话",
246
- lastCount: w = 10,
247
- ensureGroup: u = !0,
248
- autoSelectFirst: L = !0,
249
- autoLoadMessages: v = !0
250
- } = e, o = e.persistGroups ?? !1, C = e.storageKey ?? De, Q = e.beforeKnowledgeRender || (V = e.parsers) != null && V.length ? { beforeKnowledgeRender: e.beforeKnowledgeRender, parsers: e.parsers } : void 0, s = p([]), i = p(""), t = p([]), n = p(!1);
251
- let m = !1, d = null;
252
- const y = () => {
253
- if (d === !1) return null;
254
- let a;
453
+ api: r,
454
+ defaultGroupTitle: g = "新对话",
455
+ lastCount: f = 10,
456
+ ensureGroup: l = !0,
457
+ autoSelectFirst: h = !0,
458
+ autoLoadMessages: c = !0
459
+ } = e, o = e.persistGroups ?? !1, x = e.storageKey ?? ot, L = e.beforeKnowledgeRender || (O = e.parsers) != null && O.length ? { beforeKnowledgeRender: e.beforeKnowledgeRender, parsers: e.parsers } : void 0, p = b([]), i = b(""), t = b([]), s = b(!1);
460
+ let d = !1, a = null;
461
+ const u = () => {
462
+ if (a === !1) return null;
463
+ let m;
255
464
  try {
256
- a = localStorage.getItem(C);
465
+ m = localStorage.getItem(x);
257
466
  } catch {
258
- return d = !1, null;
467
+ return a = !1, null;
259
468
  }
260
- if (a === null) return [];
469
+ if (m === null) return [];
261
470
  try {
262
- const f = JSON.parse(a);
263
- return Array.isArray(f) ? f : [];
471
+ const S = JSON.parse(m);
472
+ return Array.isArray(S) ? S : [];
264
473
  } catch {
265
474
  return [];
266
475
  }
267
- }, A = (a) => {
268
- if (d !== !1)
476
+ }, y = (m) => {
477
+ if (a !== !1)
269
478
  try {
270
- localStorage.setItem(C, JSON.stringify(a));
479
+ localStorage.setItem(x, JSON.stringify(m));
271
480
  } catch {
272
- d = !1;
481
+ a = !1;
273
482
  }
274
- }, W = (a) => {
275
- const f = y();
276
- f !== null && A([...f, a]);
277
- }, U = async (a, f = w) => {
278
- n.value = !0;
483
+ }, G = (m) => {
484
+ const S = u();
485
+ S !== null && y([...S, m]);
486
+ }, E = async (m, S = f) => {
487
+ s.value = !0;
279
488
  try {
280
- t.value = a ? await Se(l, a, f, 0, Q) : [];
489
+ t.value = m ? await Be(r, m, S, 0, L) : [];
281
490
  } finally {
282
- n.value = !1;
491
+ s.value = !1;
283
492
  }
284
- }, I = async (a) => {
285
- i.value = a, v && await U(a);
286
- }, R = async () => {
287
- if (!m) {
288
- m = !0;
493
+ }, I = async (m) => {
494
+ i.value = m, c && await E(m);
495
+ }, P = async () => {
496
+ if (!d) {
497
+ d = !0;
289
498
  try {
290
- s.value.length === 0 ? u && await _(h) : !i.value && L && await I(s.value[0].group_id);
499
+ p.value.length === 0 ? l && await q(g) : !i.value && h && await I(p.value[0].group_id);
291
500
  } finally {
292
- m = !1;
501
+ d = !1;
293
502
  }
294
503
  }
295
- }, D = async () => {
296
- n.value = !0;
504
+ }, A = async () => {
505
+ s.value = !0;
297
506
  try {
298
- const a = await Me(l);
507
+ const m = await Ye(r);
299
508
  if (o) {
300
- const f = y();
301
- if (f === null)
302
- s.value = a;
509
+ const S = u();
510
+ if (S === null)
511
+ p.value = m;
303
512
  else {
304
- const P = new Set(f.map((Y) => Y.group_id)), F = a.filter((Y) => P.has(Y.group_id));
305
- A(F), s.value = F;
513
+ const D = new Set(S.map((W) => W.group_id)), K = m.filter((W) => D.has(W.group_id));
514
+ y(K), p.value = K;
306
515
  }
307
516
  } else
308
- s.value = a;
309
- await R();
517
+ p.value = m;
518
+ await P();
310
519
  } finally {
311
- n.value = !1;
520
+ s.value = !1;
312
521
  }
313
- }, _ = async (a = h) => {
314
- const f = await ie(l, a);
315
- return o && W({ group_id: f, group_title: a }), await D(), i.value !== f && await I(f), f;
522
+ }, q = async (m = g) => {
523
+ const S = await Se(r, m);
524
+ return o && G({ group_id: S, group_title: m }), await A(), i.value !== S && await I(S), S;
316
525
  };
317
526
  return {
318
- groups: s,
527
+ groups: p,
319
528
  activeGroupId: i,
320
529
  messages: t,
321
- loading: n,
322
- loadGroups: D,
323
- createGroup: _,
324
- renameGroup: async (a, f) => {
325
- await Fe(l, a, f);
326
- const P = s.value.find((F) => F.group_id === a);
327
- P && (P.group_title = f), o && A(s.value);
530
+ loading: s,
531
+ loadGroups: A,
532
+ createGroup: q,
533
+ renameGroup: async (m, S) => {
534
+ await $e(r, m, S);
535
+ const D = p.value.find((K) => K.group_id === m);
536
+ D && (D.group_title = S), o && y(p.value);
328
537
  },
329
- deleteGroup: async (a) => {
330
- const f = s.value.findIndex((F) => F.group_id === a);
331
- if (await qe(l, a), s.value = s.value.filter((F) => F.group_id !== a), o && A(s.value), i.value !== a) return;
332
- const P = s.value[f] ?? s.value[s.value.length - 1];
333
- if (P) {
334
- await I(P.group_id);
538
+ deleteGroup: async (m) => {
539
+ const S = p.value.findIndex((K) => K.group_id === m);
540
+ if (await Ve(r, m), p.value = p.value.filter((K) => K.group_id !== m), o && y(p.value), i.value !== m) return;
541
+ const D = p.value[S] ?? p.value[p.value.length - 1];
542
+ if (D) {
543
+ await I(D.group_id);
335
544
  return;
336
545
  }
337
- u ? await _(h) : (i.value = "", t.value = []);
546
+ l ? await q(g) : (i.value = "", t.value = []);
338
547
  },
339
548
  selectGroup: I,
340
- loadMessages: U
549
+ loadMessages: E
341
550
  };
342
- }, We = (e) => {
343
- const { api: l, defaultEntryId: h, entryGroup: w = "智能助手", autoLoad: u = !0 } = e, L = p([]), v = p(null), o = p(!1), C = (i) => i ? typeof l.getFileUrl == "function" ? l.getFileUrl(i) : Qe(i, "", "/algorithm/get_file_data") : "", Q = (i) => {
344
- v.value = i;
345
- }, s = async () => {
551
+ }, ut = (e) => {
552
+ const { api: r, defaultEntryId: g, entryGroup: f = "智能助手", autoLoad: l = !0 } = e, h = b([]), c = b(null), o = b(!1), x = (i) => i ? typeof r.getFileUrl == "function" ? r.getFileUrl(i) : Xe(i, "", "/algorithm/get_file_data") : "", L = (i) => {
553
+ c.value = i;
554
+ }, p = async () => {
346
555
  var i;
347
556
  o.value = !0;
348
557
  try {
349
- let t = h || ((i = v.value) == null ? void 0 : i.entry_id);
558
+ let t = g || ((i = c.value) == null ? void 0 : i.entry_id);
350
559
  if (!t)
351
560
  try {
352
- const y = await l.getDefaultLlmId();
353
- t = y == null ? void 0 : y.default_entry_id;
354
- } catch (y) {
355
- console.warn("[useAgent] 获取默认智能体 ID 失败:", y);
561
+ const u = await r.getDefaultLlmId();
562
+ t = u == null ? void 0 : u.default_entry_id;
563
+ } catch (u) {
564
+ console.warn("[useAgent] 获取默认智能体 ID 失败:", u);
356
565
  }
357
- const n = await l.getLlmEntryListByRole({ entry_group: w }), d = ((n == null ? void 0 : n.values) || []).filter((y) => y.entry_enable !== !1);
358
- if (L.value = d, t) {
359
- const y = d.find((A) => A.entry_id === t);
360
- y ? v.value = y : d.length > 0 && (v.value = d[0]);
361
- } else d.length > 0 && !v.value && (v.value = d[0]);
566
+ const s = await r.getLlmEntryListByRole({ entry_group: f }), a = ((s == null ? void 0 : s.values) || []).filter((u) => u.entry_enable !== !1);
567
+ if (h.value = a, t) {
568
+ const u = a.find((y) => y.entry_id === t);
569
+ u ? c.value = u : a.length > 0 && (c.value = a[0]);
570
+ } else a.length > 0 && !c.value && (c.value = a[0]);
362
571
  } finally {
363
572
  o.value = !1;
364
573
  }
365
574
  };
366
- return u && (oe() ? xe(() => {
367
- s();
368
- }) : s()), {
369
- agentList: L,
370
- currentAgent: v,
575
+ return l && (te() ? Fe(() => {
576
+ p();
577
+ }) : p()), {
578
+ agentList: h,
579
+ currentAgent: c,
371
580
  loading: o,
372
- getFileUrl: C,
373
- loadAgents: s,
374
- selectAgent: Q
581
+ getFileUrl: x,
582
+ loadAgents: p,
583
+ selectAgent: L
584
+ };
585
+ }, ct = (e) => {
586
+ const { api: r, interval: g = 5e3, immediate: f = !1 } = e, l = b([]), h = b(/* @__PURE__ */ new Set());
587
+ let c = null, o = null;
588
+ const x = X(() => {
589
+ const a = new Set(h.value);
590
+ return l.value.forEach((u) => u.history_group && a.add(String(u.history_group))), a;
591
+ }), L = (a) => !!a && x.value.has(a), p = (a, u) => {
592
+ if (!a) return;
593
+ const y = new Set(h.value);
594
+ if (u ? y.add(a) : y.delete(a), h.value = y, !u) {
595
+ const G = o;
596
+ (async () => (G && await G, await t()))();
597
+ }
598
+ }, i = (a) => l.value.find(
599
+ (u) => String(u.history_group) === a && u.is_connected !== !0 && !h.value.has(a)
600
+ ), t = () => o || (o = (async () => {
601
+ if (!r.getChatTaskList) return l.value;
602
+ try {
603
+ const a = (await r.getChatTaskList()).filter((u) => u && u.is_stop !== !0);
604
+ if (l.value = a, e.onUnknownGroups && e.isKnownGroup) {
605
+ const u = a.map((y) => String(y.history_group)).filter((y) => y && !e.isKnownGroup(y));
606
+ u.length && e.onUnknownGroups(u);
607
+ }
608
+ } catch {
609
+ } finally {
610
+ o = null;
611
+ }
612
+ return l.value;
613
+ })(), o), s = () => {
614
+ c || !g || (t(), c = setInterval(() => void t(), g));
615
+ }, d = () => {
616
+ c && clearInterval(c), c = null, l.value = [];
617
+ };
618
+ return f && s(), te() && we(d), { tasks: l, runningIds: x, isRunning: L, setLocalRunning: p, findResumableTask: i, refresh: t, start: s, stop: d };
619
+ }, pt = (e) => {
620
+ const { api: r, maxRooms: g = 10 } = e, f = Pe(/* @__PURE__ */ new Map()), l = b(""), h = ct({
621
+ api: r,
622
+ interval: e.taskPollInterval ?? 5e3,
623
+ onUnknownGroups: e.onUnknownGroups,
624
+ isKnownGroup: e.isKnownGroup
625
+ }), c = (t) => {
626
+ if (f.size <= g) return;
627
+ const s = [...f.entries()].filter(([d, a]) => d !== l.value && d !== t && !a.chat.talking.value).sort((d, a) => d[1].touchedAt - a[1].touchedAt);
628
+ for (; f.size > g && s.length; ) {
629
+ const [d] = s.shift();
630
+ L(d);
631
+ }
632
+ }, o = (t) => {
633
+ const s = f.get(t);
634
+ if (s)
635
+ return s.touchedAt = Date.now(), s.chat;
636
+ const d = Qe(!0), a = d.run(
637
+ () => {
638
+ var y;
639
+ return Le({
640
+ ...(y = e.roomOptions) == null ? void 0 : y.call(e, t),
641
+ api: r,
642
+ autoLogin: !1,
643
+ parsers: e.parsers,
644
+ beforeKnowledgeRender: e.beforeKnowledgeRender,
645
+ actionExecute: e.actionExecute,
646
+ lastCount: e.lastCount,
647
+ syncMetaOnFinish: !0,
648
+ onRunningChange: h.setLocalRunning
649
+ });
650
+ }
651
+ ), u = { chat: a, scope: d, loaded: a.loadGroup(t).catch(() => {
652
+ }), touchedAt: Date.now() };
653
+ return f.set(t, u), c(t), a;
654
+ }, x = async (t) => {
655
+ var a;
656
+ if (l.value = t, !t) return;
657
+ const s = o(t);
658
+ if (await ((a = f.get(t)) == null ? void 0 : a.loaded), s.talking.value) return;
659
+ await h.refresh();
660
+ const d = h.findResumableTask(t);
661
+ d && l.value === t && s.resumeTask({ history_id: d.history_id, history_group_id: String(d.history_group), question: d.question });
662
+ }, L = (t) => {
663
+ const s = f.get(t);
664
+ s && (s.chat.stopQuestion(!1), s.scope.stop(), f.delete(t), l.value === t && (l.value = ""));
665
+ }, p = () => {
666
+ [...f.keys()].forEach(L), h.stop();
667
+ }, i = X(() => {
668
+ var t;
669
+ return l.value ? ((t = f.get(l.value)) == null ? void 0 : t.chat) ?? null : null;
670
+ });
671
+ return pe() && me(p), { activeId: l, active: i, running: h, getRoom: o, open: x, dispose: L, disposeAll: p };
672
+ }, ft = (e) => {
673
+ if (!e || typeof localStorage > "u") return {};
674
+ try {
675
+ const r = JSON.parse(localStorage.getItem(e) || "{}");
676
+ return r && typeof r == "object" ? r : {};
677
+ } catch {
678
+ return {};
679
+ }
680
+ }, dt = (e, r) => {
681
+ if (!(!e || typeof localStorage > "u"))
682
+ try {
683
+ localStorage.setItem(e, JSON.stringify(r));
684
+ } catch {
685
+ }
686
+ }, mt = (e) => {
687
+ const r = e.storageKey === void 0 ? "wi-chat-room-llm" : e.storageKey, g = b([]), f = b(!1), l = b(ft(r)), h = X(() => new Set(g.value.flatMap((a) => (a.children ?? []).map((u) => u.id)))), c = (a) => {
688
+ l.value = a, dt(r, a);
375
689
  };
376
- }, je = (e = {}) => {
377
- const l = e.parsers ?? [];
378
- let h = null, w;
379
- const u = ke({
690
+ let o = null;
691
+ const x = () => f.value ? Promise.resolve() : o || (o = (async () => {
692
+ var a, u;
693
+ try {
694
+ const y = await ((u = (a = e.api).getLlmInfoList) == null ? void 0 : u.call(a));
695
+ if (!y) return;
696
+ const G = y.connections ?? [];
697
+ g.value = (y.factorys ?? []).map((I) => ({
698
+ id: I.factory_id,
699
+ name: I.factory_title,
700
+ type: "factory",
701
+ children: G.filter((P) => P.factory_id === I.factory_id).map((P) => ({ id: P.connect_id, name: P.connect_title, type: "connection" }))
702
+ })).filter((I) => I.children.length > 0), f.value = !0;
703
+ const E = Object.entries(l.value).filter(([, I]) => h.value.has(I));
704
+ E.length !== Object.keys(l.value).length && c(Object.fromEntries(E));
705
+ } catch {
706
+ } finally {
707
+ o = null;
708
+ }
709
+ })(), o), L = (a) => !!a && (!f.value || h.value.has(a));
710
+ return { llmTree: g, loaded: f, roomLlmMap: l, validIds: h, load: x, resolve: (a, u) => {
711
+ const y = a ? l.value[a] : "";
712
+ return y && L(y) ? y : u || "";
713
+ }, select: (a, u, y) => {
714
+ if (!a) return;
715
+ const G = { ...l.value };
716
+ u && u !== y ? G[a] = u : delete G[a], c(G);
717
+ }, migrate: (a, u) => {
718
+ const y = l.value[a];
719
+ if (!y || a === u) return;
720
+ const G = { ...l.value, [u]: y };
721
+ delete G[a], c(G);
722
+ }, nameOf: (a) => {
723
+ var u;
724
+ if (!a) return "";
725
+ for (const y of g.value) {
726
+ const G = (u = y.children) == null ? void 0 : u.find((E) => E.id === a);
727
+ if (G) return G.name;
728
+ }
729
+ return "";
730
+ }, isUnavailable: (a) => !!a && f.value && !h.value.has(a) };
731
+ }, he = async (e) => {
732
+ var f;
733
+ if (typeof navigator < "u" && ((f = navigator.clipboard) != null && f.writeText))
734
+ try {
735
+ await navigator.clipboard.writeText(e);
736
+ return;
737
+ } catch {
738
+ }
739
+ if (typeof document > "u") throw new Error("当前环境不支持剪贴板");
740
+ const r = document.createElement("textarea");
741
+ r.value = e, r.style.position = "fixed", r.style.opacity = "0", document.body.appendChild(r), r.select();
742
+ const g = document.execCommand("copy");
743
+ if (document.body.removeChild(r), !g) throw new Error("复制失败");
744
+ }, St = (e) => {
745
+ const { api: r, buildShareUrl: g, shareDays: f = 1 } = e, l = (i) => !!ye(i.answer), h = async (i, t = !1) => {
746
+ const s = ye(i.answer);
747
+ if (!s) return !1;
748
+ try {
749
+ return await he(t ? s : Ze(s)), !0;
750
+ } catch {
751
+ return !1;
752
+ }
753
+ }, c = (i) => Ae(i.answer).state, o = /* @__PURE__ */ new WeakSet();
754
+ return { canCopy: l, copy: h, stateOf: c, toggleState: async (i, t) => {
755
+ if (!(!i.historyId || !r.setHistoryAnswerState || o.has(i))) {
756
+ o.add(i);
757
+ try {
758
+ const s = c(i) === t ? null : t;
759
+ await r.setHistoryAnswerState(i.historyId, s), i.answer.answer || (i.answer.answer = { reports: [] }), i.answer.answer.state = s;
760
+ } finally {
761
+ o.delete(i);
762
+ }
763
+ }
764
+ }, feedback: async (i, t) => {
765
+ !i.historyId || !t.trim() || !r.reportHistoryProblem || await r.reportHistoryProblem(i.historyId, t.trim());
766
+ }, share: async (i) => {
767
+ if (!i.historyId || !r.shareChatHistory) throw new Error("当前回答暂不支持分享");
768
+ const t = await r.shareChatHistory([i.historyId], f), s = g(t);
769
+ return await he(s), s;
770
+ } };
771
+ }, At = (e = {}) => {
772
+ const r = e.parsers ?? [];
773
+ let g = null, f;
774
+ const l = Le({
380
775
  ...e.chat,
381
- parsers: l,
382
- onFirstQuestion: ({ question: t, groupId: n }) => {
383
- w == null || w(n, t);
776
+ parsers: r,
777
+ onFirstQuestion: ({ question: t, groupId: s }) => {
778
+ f == null || f(s, t);
384
779
  }
385
- }), L = He({
780
+ }), h = it({
386
781
  ...e.history,
387
- api: u.api,
782
+ api: l.api,
388
783
  autoLoadMessages: !1,
389
- parsers: l
784
+ parsers: r
390
785
  });
391
- w = (t, n) => {
392
- h = L.renameGroup(t, n).catch((m) => {
393
- var d;
394
- (d = e.onError) == null || d.call(e, m, "groups");
786
+ f = (t, s) => {
787
+ g = h.renameGroup(t, s).catch((d) => {
788
+ var a;
789
+ (a = e.onError) == null || a.call(e, d, "groups");
395
790
  });
396
791
  };
397
- const v = We({
792
+ const c = ut({
398
793
  ...e.agent,
399
- api: u.api,
794
+ api: l.api,
400
795
  autoLoad: !1
401
- }), o = (t, n) => {
402
- var m;
403
- (m = e.onError) == null || m.call(e, t, n);
404
- }, C = async () => {
796
+ }), o = (t, s) => {
797
+ var d;
798
+ (d = e.onError) == null || d.call(e, t, s);
799
+ }, x = async () => {
405
800
  try {
406
- await L.loadGroups();
801
+ await h.loadGroups();
407
802
  } catch (t) {
408
803
  o(t, "groups");
409
804
  }
410
- }, Q = async (t) => {
805
+ }, L = async (t) => {
411
806
  try {
412
- await u.loadGroup(t);
413
- } catch (n) {
414
- o(n, "messages");
807
+ await l.loadGroup(t);
808
+ } catch (s) {
809
+ o(s, "messages");
415
810
  }
416
- }, s = async () => {
811
+ }, p = async () => {
417
812
  try {
418
- await v.loadAgents();
813
+ await c.loadAgents();
419
814
  } catch (t) {
420
815
  o(t, "agents");
421
816
  }
422
817
  }, i = async (t) => {
423
- var d;
424
- const n = typeof t == "string" ? { text: t } : t;
425
- await u.submitQuestion({
426
- ...n,
427
- entryId: (n == null ? void 0 : n.entryId) ?? ((d = v.currentAgent.value) == null ? void 0 : d.entry_id)
818
+ var a;
819
+ const s = typeof t == "string" ? { text: t } : t;
820
+ await l.submitQuestion({
821
+ ...s,
822
+ entryId: (s == null ? void 0 : s.entryId) ?? ((a = c.currentAgent.value) == null ? void 0 : a.entry_id)
428
823
  });
429
- const m = h;
430
- h = null, m && await m, await C();
824
+ const d = g;
825
+ g = null, d && await d, await x();
431
826
  };
432
- return re(u.session, (t) => {
433
- t && (C(), s());
434
- }), re(L.activeGroupId, (t) => {
435
- t && Q(t);
827
+ return ue(l.session, (t) => {
828
+ t && (x(), p());
829
+ }), ue(h.activeGroupId, (t) => {
830
+ t && L(t);
436
831
  }), {
437
- chat: u,
438
- history: L,
439
- agent: v,
440
- parsers: l,
441
- messages: u.messages,
442
- question: u.question,
443
- incPrevQuestion: u.incPrevQuestion,
444
- talking: u.talking,
445
- loading: u.loading,
446
- session: u.session,
447
- groups: L.groups,
448
- activeGroupId: L.activeGroupId,
449
- historyLoading: L.loading,
450
- agentList: v.agentList,
451
- currentAgent: v.currentAgent,
452
- agentLoading: v.loading,
453
- hasMore: u.hasMore,
454
- loadMoreLoading: u.loadMoreLoading,
455
- loadGroup: Q,
456
- loadOlder: u.loadOlder,
457
- refreshGroups: C,
458
- loadAgents: s,
459
- selectAgent: v.selectAgent,
460
- getFileUrl: v.getFileUrl,
832
+ chat: l,
833
+ history: h,
834
+ agent: c,
835
+ parsers: r,
836
+ messages: l.messages,
837
+ question: l.question,
838
+ incPrevQuestion: l.incPrevQuestion,
839
+ talking: l.talking,
840
+ loading: l.loading,
841
+ session: l.session,
842
+ groups: h.groups,
843
+ activeGroupId: h.activeGroupId,
844
+ historyLoading: h.loading,
845
+ agentList: c.agentList,
846
+ currentAgent: c.currentAgent,
847
+ agentLoading: c.loading,
848
+ hasMore: l.hasMore,
849
+ loadMoreLoading: l.loadMoreLoading,
850
+ loadGroup: L,
851
+ loadOlder: l.loadOlder,
852
+ refreshGroups: x,
853
+ loadAgents: p,
854
+ selectAgent: c.selectAgent,
855
+ getFileUrl: c.getFileUrl,
461
856
  submitQuestion: i,
462
- sendWsMessage: u.sendWsMessage,
463
- stopQuestion: u.stopQuestion
857
+ sendWsMessage: l.sendWsMessage,
858
+ stopQuestion: l.stopQuestion
464
859
  };
465
860
  };
466
861
  export {
467
- We as useAgent,
468
- ke as useChat,
469
- je as useChatSession,
470
- He as useHistory
862
+ st as createLocalCredentialStore,
863
+ be as createRenewer,
864
+ ut as useAgent,
865
+ St as useAnswerActions,
866
+ Le as useChat,
867
+ wt as useChatAuth,
868
+ pt as useChatRooms,
869
+ At as useChatSession,
870
+ it as useHistory,
871
+ nt as useKeepAlive,
872
+ mt as useLlm,
873
+ ct as useRunningGroups
471
874
  };
472
875
  //# sourceMappingURL=index.js.map