wi-chat 0.6.1 → 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 (107) 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 +737 -322
  4. package/dist/core/useAnswerActions.d.ts +61 -0
  5. package/dist/core/useChat.d.ts +90 -15
  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 +4 -2
  13. package/dist/index.js +119 -108
  14. package/dist/mobile-ui/chat/ChatContainer.vue.d.ts +8 -8
  15. package/dist/mobile-ui/chat/ChatMessagesView.vue.d.ts +6 -6
  16. package/dist/mobile-ui/index.cjs +1 -1
  17. package/dist/mobile-ui/index.js +104 -104
  18. package/dist/mobile-ui/inputbox/Inputbox.vue.d.ts +6 -6
  19. package/dist/page-context/index.js +2 -4
  20. package/dist/parser/answerCom/actionExecute/actionProtocol.d.ts +1 -7
  21. package/dist/parser/answerCom/actionExecute/clarification.d.ts +15 -3
  22. package/dist/parser/answerCom/actionExecute/constants.d.ts +10 -0
  23. package/dist/parser/answerCom/actionExecute/parseCollectClarifications.d.ts +3 -2
  24. package/dist/parser/answerCom/actionExecute/parseHostAction.d.ts +3 -2
  25. package/dist/parser/answerCom/actionExecute/types.d.ts +3 -3
  26. package/dist/parser/answerCom/generic/parseGeneric.d.ts +52 -0
  27. package/dist/parser/answerCom/generic/types.d.ts +75 -0
  28. package/dist/parser/answerCom/outputFile/parseOutputFile.d.ts +7 -6
  29. package/dist/parser/answerCom/outputFile/types.d.ts +15 -5
  30. package/dist/parser/answerCom/presentFiles/parsePresentFiles.d.ts +58 -0
  31. package/dist/parser/answerCom/presentFiles/types.d.ts +27 -0
  32. package/dist/parser/answerMeta.d.ts +71 -0
  33. package/dist/parser/deepThinking/parseDeepThinking.d.ts +8 -0
  34. package/dist/parser/history/history.d.ts +9 -0
  35. package/dist/parser/history/index.d.ts +1 -1
  36. package/dist/parser/index.cjs +6 -2
  37. package/dist/parser/index.d.ts +13 -4
  38. package/dist/parser/index.js +640 -457
  39. package/dist/parser/message.d.ts +9 -0
  40. package/dist/parser/registry.d.ts +1 -1
  41. package/dist/parser/runTime.d.ts +29 -0
  42. package/dist/renderers/TextAnswer-BgKGvG_o.cjs +2 -0
  43. package/dist/renderers/TextAnswer-PQwV3K7v.js +5 -0
  44. package/dist/renderers/aiAnswer/AiAnswerBubble.vue.d.ts +4 -4
  45. package/dist/renderers/answerCard/AnswerCard.vue.d.ts +65 -0
  46. package/dist/renderers/answerCard/AnswerToolbar.vue.d.ts +42 -0
  47. package/dist/renderers/answerConfig.d.ts +33 -0
  48. package/dist/renderers/clarification/ClarificationCard.vue.d.ts +3 -3
  49. package/dist/renderers/deepThinking/DeepThinkingSteps.vue.d.ts +4 -4
  50. package/dist/renderers/deviceLastValue/DeviceLastValue.vue.d.ts +37 -0
  51. package/dist/renderers/hostAction/HostActionStatus.vue.d.ts +3 -3
  52. package/dist/renderers/iframePage/IframePage.vue.d.ts +3 -3
  53. package/dist/renderers/index.cjs +6 -1
  54. package/dist/renderers/index.d.ts +14 -0
  55. package/dist/renderers/index.js +1504 -607
  56. package/dist/renderers/knowledgeStream/KnowledgeStreamResult.vue.d.ts +3 -3
  57. package/dist/renderers/media/MediaAnswer.vue.d.ts +19 -0
  58. package/dist/renderers/outputFile/OutputFile.vue.d.ts +3 -3
  59. package/dist/renderers/outputFile/OutputFileLink.vue.d.ts +19 -0
  60. package/dist/renderers/outputFile/localFile.d.ts +46 -0
  61. package/dist/renderers/outputFile/useOutputFile.d.ts +32 -3
  62. package/dist/renderers/presentFiles/PresentFiles.vue.d.ts +19 -0
  63. package/dist/renderers/recordset/RecordsetView.vue.d.ts +19 -0
  64. package/dist/renderers/sqlQuery/SqlQuery.vue.d.ts +19 -0
  65. package/dist/renderers/style.css +1 -1
  66. package/dist/renderers/text/TextAnswer.vue.d.ts +19 -0
  67. package/dist/request/api.d.ts +121 -1
  68. package/dist/request/client.d.ts +6 -0
  69. package/dist/request/index.cjs +4 -2
  70. package/dist/request/index.d.ts +4 -2
  71. package/dist/request/index.js +754 -181
  72. package/dist/request/ws.d.ts +11 -86
  73. package/dist/request/wsCore.d.ts +104 -0
  74. package/dist/request/wsSession.d.ts +353 -0
  75. package/dist/right-panel/RightPanel.vue.d.ts +6 -6
  76. package/dist/right-panel/RightPanelIcon.vue.d.ts +4 -4
  77. package/dist/right-panel/RightPanelTabs.vue.d.ts +9 -5
  78. package/dist/right-panel/index.cjs +1 -1
  79. package/dist/right-panel/index.d.ts +1 -1
  80. package/dist/right-panel/index.js +617 -537
  81. package/dist/right-panel/panels/newtab/NewTabPanel.vue.d.ts +4 -4
  82. package/dist/right-panel/panels/web/WebPanel.vue.d.ts +3 -3
  83. package/dist/right-panel/shared/BrowserToolbar.vue.d.ts +3 -3
  84. package/dist/right-panel/shared/RightPanelToolList.vue.d.ts +5 -5
  85. package/dist/right-panel/style.css +1 -1
  86. package/dist/right-panel/types.d.ts +10 -0
  87. package/dist/theme/index.js +2 -4
  88. package/dist/tools/index.cjs +5 -5
  89. package/dist/tools/index.d.ts +2 -2
  90. package/dist/tools/index.js +323 -217
  91. package/dist/tools/markdown/MarkdownContent.vue.d.ts +5 -5
  92. package/dist/tools/markdown/index.d.ts +2 -0
  93. package/dist/tools/markdown/interactions.d.ts +39 -0
  94. package/dist/tools/markdown/sanitize.d.ts +6 -0
  95. package/dist/tools/style.css +1 -1
  96. package/dist/tools/useAutoScroll.d.ts +3 -3
  97. package/dist/web-ui/attach/AttachmentCard.vue.d.ts +6 -6
  98. package/dist/web-ui/attach/AttachmentList.vue.d.ts +6 -6
  99. package/dist/web-ui/chat/ChatContainer.vue.d.ts +10 -10
  100. package/dist/web-ui/chat/ChatMessagesView.vue.d.ts +6 -6
  101. package/dist/web-ui/envswitch/EnvSwitch.vue.d.ts +6 -6
  102. package/dist/web-ui/index.cjs +1 -1
  103. package/dist/web-ui/index.js +630 -635
  104. package/dist/web-ui/inputbox/Inputbox.vue.d.ts +11 -11
  105. package/dist/web-ui/recordset/RecordsetTable.vue.d.ts +4 -4
  106. package/dist/web-ui/style.css +1 -1
  107. package/package.json +2 -1
@@ -1,176 +1,397 @@
1
- import { ref as y, getCurrentInstance as oe, onBeforeUnmount as we, reactive as se, onMounted as me, watch as ne } from "vue";
2
- import { clearPendingClarifications as re, loadGroupMessagesByStep as le, createHistoryGroup as ie, nextLocalMessageId as ue, createEmptyAnswerValues as Ge, buildChatTitle as Le, consumeStream as Ae, applyAnswerChunk as xe, collectThinkingSteps as Ie, loadGroupMessages as _e, deleteHistoryGroup as be, setHistoryGroupTitle as Ce, getHistoryGroups as Se, resolveRemoteFileUrl as Ee } from "wi-chat/parser";
3
- import { createRequest as Me, createChatApi as Qe, login as Re, isCancelError as Fe } from "wi-chat/request";
4
- const Ue = (e) => {
5
- const p = e.login, C = e.defaultGroupTitle ?? "新对话", L = e.lastCount ?? 10, u = e.autoSetGroupTitleOnFirstQuestion ?? !0, m = {
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 = {
140
+ requestId: e,
141
+ sender: null,
142
+ queue: [],
143
+ closed: !1,
144
+ reply: (g) => xe(r, g)
145
+ };
146
+ return r;
147
+ }, xe = (e, r, g) => {
148
+ if (e.closed) return !1;
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 = {
6
155
  beforeKnowledgeRender: e.beforeKnowledgeRender,
7
156
  parsers: e.parsers,
8
157
  actionExecute: {
9
158
  ...e.actionExecute,
10
- reply: (s) => Z(s)
159
+ reply: () => !1
11
160
  }
12
- }, g = y(""), o = y([]), x = y(""), U = y(e.incPrevQuestion ?? "Y"), n = y(!1), i = y(!1), t = y(""), l = y(!1), G = y(""), f = y(!0), v = y(!1), A = y(0), D = e.api ? null : Me({
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 : $({
13
162
  baseURL: e.baseURL ?? "",
14
- getWisession: () => g.value
15
- }), T = e.api ?? Qe(D);
16
- let I = null, S = null, k = 0, _ = 0, j = 0, H = null, O = [];
17
- const a = async () => {
18
- if (G.value = "", !D || !p) {
19
- l.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;
20
178
  return;
21
179
  }
22
180
  try {
23
- const s = await Re(D, p);
24
- return g.value = s.wisession || "", l.value = !0, s;
25
- } catch (s) {
26
- throw G.value = s instanceof Error ? s.message : String(s), s;
27
- }
28
- }, r = (s) => s === k, Q = (s) => {
29
- typeof URL > "u" || typeof URL.revokeObjectURL != "function" || s.forEach((w) => {
30
- w.role !== "user" || !w.attachments || w.attachments.forEach((h) => {
31
- var b;
32
- const E = (b = h.attachContent) == null ? void 0 : b.url;
33
- E && E.startsWith("blob:") && URL.revokeObjectURL(E);
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);
34
192
  });
35
193
  });
36
- }, R = async (s) => {
37
- const w = ++_;
38
- j += 1, n.value = !0, Q(o.value), re(), o.value = [], t.value = s, f.value = !0, v.value = !1, A.value = 0;
39
- const h = [], E = () => {
40
- const d = [];
41
- h.forEach((q) => {
42
- d.push(q.user), q.assistant && d.push(q.assistant);
43
- }), w === _ && (o.value = d);
44
- }, b = (d) => {
45
- w === _ && (d.type === "users" ? (h.length = 0, d.users.forEach((q) => h.push({ user: q }))) : h[d.index] && (d.user && (h[d.index].user = d.user), h[d.index].assistant = d.assistant), E());
194
+ }, Ge = (n) => ({
195
+ beforeKnowledgeRender: e.beforeKnowledgeRender,
196
+ parsers: e.parsers,
197
+ actionExecute: {
198
+ ...e.actionExecute,
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());
46
211
  };
47
212
  try {
48
- await le(T, s, L, b, 0, m), E(), w === _ && (A.value = h.length, h.length < L && (f.value = !1));
213
+ await de(E, n, f, R, 0, h), M(), _ === O && (y.value = w.length, w.length < f && (a.value = !1));
49
214
  } finally {
50
- j -= 1, j <= 0 && (j = 0, n.value = !1);
215
+ m -= 1, m <= 0 && (m = 0, p.value = !1);
51
216
  }
52
- }, V = async () => {
53
- if (!t.value || n.value || !f.value || v.value)
54
- return;
55
- const s = t.value, w = _, h = A.value;
56
- v.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;
57
221
  try {
58
- const E = await le(T, s, L, void 0, h, m), b = E.filter((d) => d.role === "user").length;
59
- if (b < L && (f.value = !1), w === _ && t.value === s) {
60
- const d = /* @__PURE__ */ new Set();
61
- o.value.forEach((M) => {
62
- M.historyId && d.add(M.historyId), M.id && d.add(M.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);
63
227
  });
64
- const q = E.filter((M) => !(M.historyId && d.has(M.historyId) || M.id && d.has(M.id)));
65
- o.value = [...q, ...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;
66
230
  }
67
231
  } finally {
68
- v.value = !1;
69
- }
70
- }, ce = async (s = C) => {
71
- const w = await ie(T, s);
72
- return t.value = w, w;
73
- }, X = () => {
74
- k += 1, _ += 1, S && (S.stopped = !0, S.thinking.expanded = !1), I == null || I.abort(), I = null, H = null, O = [], re(), S = null, i.value = !1;
75
- }, fe = () => {
76
- S && (S.error = !0, S.thinking.expanded = !1);
77
- }, de = async (s) => {
78
- var ee, te;
79
- const w = typeof s == "string" ? { text: s } : s || {}, { text: h, entryId: E, files: b, attachments: d, extArgs: q, ...M } = w, B = (h !== void 0 ? h : x.value).trim(), ve = q ?? e.extArgs ?? {}, ye = b && b.length > 0 ? b : (d ?? []).map((P) => {
80
- var Y;
81
- return (Y = P.attachContent) == null ? void 0 : Y.raw;
82
- }).filter((P) => !!P);
83
- if (!B || i.value || !t.value)
84
- return;
85
- const K = k + 1;
86
- k = K, _ += 1;
87
- 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;
88
250
  try {
89
- i.value = !0;
90
- const P = t.value, Y = d || (b || []).map((c) => {
91
- var W;
92
- const J = c.name.includes(".") && ((W = c.name.split(".").pop()) == null ? void 0 : W.toLowerCase()) || "";
93
- return {
94
- type: "file",
95
- name: c.name,
96
- description: J ? J.toUpperCase() : "",
97
- attachContent: {
98
- file_name: c.name,
99
- size: c.size,
100
- suffix: J,
101
- raw: c
102
- }
103
- };
104
- }), he = A.value === 0 && !o.value.some((c) => c.role === "user"), z = P, ae = se({
105
- id: ue("user"),
106
- role: "user",
107
- content: B,
108
- attachments: Y.length > 0 ? Y : void 0
109
- });
110
- o.value.push(ae);
111
- const F = se({
112
- id: ue("answer"),
113
- role: "assistant",
114
- answer: Ge("", B),
115
- error: !1,
116
- stopped: !1,
117
- thinking: { steps: [], expanded: !0 }
118
- });
119
- if (S = F, o.value.push(F), x.value = "", (ee = e.onSubmitQuestion) == null || ee.call(e, B), he && z) {
120
- const c = Le(B);
121
- e.onFirstQuestion ? e.onFirstQuestion({ question: c, groupId: z }) : u && c && T.setHistoryGroupTitle(z, c).catch(() => {
122
- });
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 {
123
295
  }
124
- I = new AbortController();
125
- const pe = await T.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(
126
337
  {
127
- // 先铺开外部透传的额外字段(raw_mode / tables / metrics / group_type 等),
128
- // 已知字段随后覆盖,避免同名额外字段篡改核心参数
129
- ...M,
130
- question: B,
131
- historyGroupId: P,
132
- entryId: E,
133
- files: ye,
134
- ext_args: JSON.stringify(ve),
135
- incPrevQuestion: U.value,
136
- // 连接建立且首包发完后接收分片发送器,并按序补发连接建立前入队的追发消息
137
- // (sendWsMessage 在连接未就绪时调用会先入队,见 sendWsMessage)
138
- onOpen: (c) => {
139
- if (!r(K))
140
- return;
141
- H = c;
142
- const J = O;
143
- O = [], J.forEach(({ payloadText: W, chunkSize: N }) => c(W, N));
144
- }
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
145
347
  },
146
- I.signal
147
- );
148
- A.value += 1, await Ae(pe, (c) => {
149
- var W;
150
- if (!r(K))
151
- return;
152
- if (c.mode === "history_id") {
153
- const N = typeof c.value == "object" && c.value !== null ? c.value.history_id : c.value;
154
- N && (ae.historyId = String(N), F.historyId = String(N), F.answer && (F.answer.id = String(N)));
155
- }
156
- xe(F.answer, c, m).includes("deep_thinking") && (F.thinking.steps = Ie(F.answer)), c.mode === "finish" && (F.thinking.expanded = !1), (W = e.onChunk) == null || W.call(e, c);
157
- }), r(K) && (F.thinking.expanded = !1), r(K) && ($ = !0);
158
- } catch (P) {
159
- r(K) && !Fe(P) && fe();
160
- } finally {
161
- r(K) && (i.value = !1, I = null, S = null, H = null, O = []);
162
- }
163
- $ && ((te = e.onFinish) == null || te.call(e));
164
- }, Z = (s, w) => {
165
- if (!i.value)
166
- return !1;
167
- const h = typeof s == "string" ? s : JSON.stringify(s) ?? "";
168
- return H ? H(h, w) : O.push({ payloadText: h, chunkSize: w }), !0;
169
- }, ge = e.autoLogin !== !1 ? a().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(() => {
170
391
  }).catch(() => {
171
392
  }) : Promise.resolve();
172
- return oe() && we(() => {
173
- X(), Q(o.value);
393
+ return pe() && me(() => {
394
+ ne(!1), W(o.value);
174
395
  }), {
175
396
  /** 当前分组的消息列表(user / assistant 交错;loadGroup / loadOlder / submitQuestion 会更新) */
176
397
  messages: o,
@@ -180,281 +401,475 @@ const Ue = (e) => {
180
401
  * 提问是否携带之前对话上下文(inc_prev_question:'Y' 不忽略 / 'N' 忽略,默认 'Y')。
181
402
  * submitQuestion 提交时读取当前值发送;可在外部随时修改(如绑定输入框组件的勾选开关)。
182
403
  */
183
- incPrevQuestion: U,
404
+ incPrevQuestion: L,
184
405
  /** 会话发送 / 流式回答进行中(true 时 submitQuestion 不再受理新提问) */
185
406
  talking: i,
186
407
  /** 历史加载中(首屏 loadGroup 及并发加载全部完成后结束) */
187
- loading: n,
408
+ loading: p,
188
409
  /** 当前会话 token(自动登录写入;未自动登录时可自行赋值) */
189
- session: g,
410
+ session: c,
190
411
  /** 自动登录流程已结束(成功 / 失败 / 跳过均置 true;UI 可等待它再渲染登录状态) */
191
- autoLoginDone: l,
412
+ autoLoginDone: s,
192
413
  /** 最近一次自动登录的错误信息(UI 可据此提示重试) */
193
- autoLoginError: G,
414
+ autoLoginError: d,
194
415
  /** 自动登录完成时机(无论成败都会 resolve;宿主用它编排"登录后才能发"的请求,如加载历史分组) */
195
- loginDone: ge,
416
+ loginDone: Ke,
196
417
  /** 当前会话所在的历史分组 id */
197
418
  historyGroupId: t,
198
419
  /** 内部自建的 request 客户端(注入 api 时为 null,无使用价值) */
199
- selfRequest: D,
420
+ selfRequest: G,
200
421
  /** 实际使用的业务 API(外部注入的实例或内部自建) */
201
- api: T,
422
+ api: E,
202
423
  /** 手动触发一次自动登录(成功返回登录结果;失败抛错并记录到 autoLoginError) */
203
- doAutoLogin: a,
424
+ doAutoLogin: D,
204
425
  /** 按分组加载消息(插空式渐进渲染;并发调用时仅最新一次生效) */
205
- loadGroup: R,
426
+ loadGroup: Re,
206
427
  /** 加载更早一页历史(向上翻页),前插到消息列表顶部 */
207
- loadOlder: V,
428
+ loadOlder: Ce,
208
429
  /** 是否仍可能有更早历史(向上翻页) */
209
- hasMore: f,
430
+ hasMore: a,
210
431
  /** 向上翻页加载中(触顶防重) */
211
- loadMoreLoading: v,
432
+ loadMoreLoading: u,
212
433
  /** 新建历史分组并选中(返回分组 id) */
213
- createGroup: ce,
434
+ createGroup: Ee,
214
435
  /** 提交问题并走 SSE 流式问答(支持传参 entryId、files 与文本;无文本 / 发送中 / 无分组时不发送) */
215
- submitQuestion: de,
436
+ submitQuestion: qe,
216
437
  /**
217
438
  * 向当前提问的 WebSocket 连接追发自定义消息(B/M/E 分帧):
218
439
  * 连接就绪立即发出、未就绪入队待连接建立后按序补发;仅在 talking 期间受理,
219
440
  * 无在途会话返回 false。见 sendWsMessage 的 JSDoc。
220
441
  */
221
- sendWsMessage: Z,
222
- /** 停止在途会话:中断流、把当前回答标记为 stopped,并复位 talking */
223
- stopQuestion: X
442
+ sendWsMessage: Oe,
443
+ /** 停止在途会话:向后端发 STOP(可续传模式)、中断流、把当前回答标记为 stopped,并复位 talking */
444
+ stopQuestion: ne,
445
+ /** 接管当前分组正在进行的任务(刷新 / 切回房间后 CONNECT 重放),需 resumable */
446
+ resumeTask: Te,
447
+ /** 手动触发一次登录态续期(并发调用合并为一次) */
448
+ renewSession: P
224
449
  };
225
- }, Te = "wi-chat-history-groups", qe = (e) => {
450
+ }, ot = "wi-chat-history-groups", it = (e) => {
226
451
  var O;
227
452
  const {
228
- api: p,
229
- defaultGroupTitle: C = "新对话",
230
- lastCount: L = 10,
231
- ensureGroup: u = !0,
232
- autoSelectFirst: m = !0,
233
- autoLoadMessages: g = !0
234
- } = e, o = e.persistGroups ?? !1, x = e.storageKey ?? Te, U = e.beforeKnowledgeRender || (O = e.parsers) != null && O.length ? { beforeKnowledgeRender: e.beforeKnowledgeRender, parsers: e.parsers } : void 0, n = y([]), i = y(""), t = y([]), l = y(!1);
235
- let G = !1, f = null;
236
- const v = () => {
237
- if (f === !1)
238
- return null;
239
- 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;
240
464
  try {
241
- a = localStorage.getItem(x);
465
+ m = localStorage.getItem(x);
242
466
  } catch {
243
- return f = !1, null;
467
+ return a = !1, null;
244
468
  }
245
- if (a === null)
246
- return [];
469
+ if (m === null) return [];
247
470
  try {
248
- const r = JSON.parse(a);
249
- return Array.isArray(r) ? r : [];
471
+ const S = JSON.parse(m);
472
+ return Array.isArray(S) ? S : [];
250
473
  } catch {
251
474
  return [];
252
475
  }
253
- }, A = (a) => {
254
- if (f !== !1)
476
+ }, y = (m) => {
477
+ if (a !== !1)
255
478
  try {
256
- localStorage.setItem(x, JSON.stringify(a));
479
+ localStorage.setItem(x, JSON.stringify(m));
257
480
  } catch {
258
- f = !1;
481
+ a = !1;
259
482
  }
260
- }, D = (a) => {
261
- const r = v();
262
- r !== null && A([...r, a]);
263
- }, T = async (a, r = L) => {
264
- l.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;
265
488
  try {
266
- t.value = a ? await _e(p, a, r, 0, U) : [];
489
+ t.value = m ? await Be(r, m, S, 0, L) : [];
267
490
  } finally {
268
- l.value = !1;
491
+ s.value = !1;
269
492
  }
270
- }, I = async (a) => {
271
- i.value = a, g && await T(a);
272
- }, S = async () => {
273
- if (!G) {
274
- G = !0;
493
+ }, I = async (m) => {
494
+ i.value = m, c && await E(m);
495
+ }, P = async () => {
496
+ if (!d) {
497
+ d = !0;
275
498
  try {
276
- n.value.length === 0 ? u && await _(C) : !i.value && m && await I(n.value[0].group_id);
499
+ p.value.length === 0 ? l && await q(g) : !i.value && h && await I(p.value[0].group_id);
277
500
  } finally {
278
- G = !1;
501
+ d = !1;
279
502
  }
280
503
  }
281
- }, k = async () => {
282
- l.value = !0;
504
+ }, A = async () => {
505
+ s.value = !0;
283
506
  try {
284
- const a = await Se(p);
507
+ const m = await Ye(r);
285
508
  if (o) {
286
- const r = v();
287
- if (r === null)
288
- n.value = a;
509
+ const S = u();
510
+ if (S === null)
511
+ p.value = m;
289
512
  else {
290
- const Q = new Set(r.map((V) => V.group_id)), R = a.filter((V) => Q.has(V.group_id));
291
- A(R), n.value = R;
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;
292
515
  }
293
516
  } else
294
- n.value = a;
295
- await S();
517
+ p.value = m;
518
+ await P();
296
519
  } finally {
297
- l.value = !1;
520
+ s.value = !1;
298
521
  }
299
- }, _ = async (a = C) => {
300
- const r = await ie(p, a);
301
- return o && D({ group_id: r, group_title: a }), await k(), i.value !== r && await I(r), r;
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;
302
525
  };
303
526
  return {
304
- groups: n,
527
+ groups: p,
305
528
  activeGroupId: i,
306
529
  messages: t,
307
- loading: l,
308
- loadGroups: k,
309
- createGroup: _,
310
- renameGroup: async (a, r) => {
311
- await Ce(p, a, r);
312
- const Q = n.value.find((R) => R.group_id === a);
313
- Q && (Q.group_title = r), o && A(n.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);
314
537
  },
315
- deleteGroup: async (a) => {
316
- const r = n.value.findIndex((R) => R.group_id === a);
317
- if (await be(p, a), n.value = n.value.filter((R) => R.group_id !== a), o && A(n.value), i.value !== a)
318
- return;
319
- const Q = n.value[r] ?? n.value[n.value.length - 1];
320
- if (Q) {
321
- await I(Q.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);
322
544
  return;
323
545
  }
324
- u ? await _(C) : (i.value = "", t.value = []);
546
+ l ? await q(g) : (i.value = "", t.value = []);
325
547
  },
326
548
  selectGroup: I,
327
- loadMessages: T
549
+ loadMessages: E
328
550
  };
329
- }, Oe = (e) => {
330
- const { api: p, defaultEntryId: C, entryGroup: L = "智能助手", autoLoad: u = !0 } = e, m = y([]), g = y(null), o = y(!1), x = (i) => i ? typeof p.getFileUrl == "function" ? p.getFileUrl(i) : Ee(i, "", "/algorithm/get_file_data") : "", U = (i) => {
331
- g.value = i;
332
- }, n = 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 () => {
333
555
  var i;
334
556
  o.value = !0;
335
557
  try {
336
- let t = C || ((i = g.value) == null ? void 0 : i.entry_id);
558
+ let t = g || ((i = c.value) == null ? void 0 : i.entry_id);
337
559
  if (!t)
338
560
  try {
339
- const v = await p.getDefaultLlmId();
340
- t = v == null ? void 0 : v.default_entry_id;
341
- } catch (v) {
342
- console.warn("[useAgent] 获取默认智能体 ID 失败:", v);
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);
343
565
  }
344
- const l = await p.getLlmEntryListByRole({ entry_group: L }), f = ((l == null ? void 0 : l.values) || []).filter((v) => v.entry_enable !== !1);
345
- if (m.value = f, t) {
346
- const v = f.find((A) => A.entry_id === t);
347
- v ? g.value = v : f.length > 0 && (g.value = f[0]);
348
- } else
349
- f.length > 0 && !g.value && (g.value = f[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]);
350
571
  } finally {
351
572
  o.value = !1;
352
573
  }
353
574
  };
354
- return u && (oe() ? me(() => {
355
- n();
356
- }) : n()), {
357
- agentList: m,
358
- currentAgent: g,
575
+ return l && (te() ? Fe(() => {
576
+ p();
577
+ }) : p()), {
578
+ agentList: h,
579
+ currentAgent: c,
359
580
  loading: o,
360
581
  getFileUrl: x,
361
- loadAgents: n,
362
- selectAgent: U
582
+ loadAgents: p,
583
+ selectAgent: L
363
584
  };
364
- }, We = (e = {}) => {
365
- const p = e.parsers ?? [];
366
- let C = null, L;
367
- const u = Ue({
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);
689
+ };
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({
368
775
  ...e.chat,
369
- parsers: p,
370
- onFirstQuestion: ({ question: t, groupId: l }) => {
371
- L == null || L(l, t);
776
+ parsers: r,
777
+ onFirstQuestion: ({ question: t, groupId: s }) => {
778
+ f == null || f(s, t);
372
779
  }
373
- }), m = qe({
780
+ }), h = it({
374
781
  ...e.history,
375
- api: u.api,
782
+ api: l.api,
376
783
  autoLoadMessages: !1,
377
- parsers: p
784
+ parsers: r
378
785
  });
379
- L = (t, l) => {
380
- C = m.renameGroup(t, l).catch((G) => {
381
- var f;
382
- (f = e.onError) == null || f.call(e, G, "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");
383
790
  });
384
791
  };
385
- const g = Oe({
792
+ const c = ut({
386
793
  ...e.agent,
387
- api: u.api,
794
+ api: l.api,
388
795
  autoLoad: !1
389
- }), o = (t, l) => {
390
- var G;
391
- (G = e.onError) == null || G.call(e, t, l);
796
+ }), o = (t, s) => {
797
+ var d;
798
+ (d = e.onError) == null || d.call(e, t, s);
392
799
  }, x = async () => {
393
800
  try {
394
- await m.loadGroups();
801
+ await h.loadGroups();
395
802
  } catch (t) {
396
803
  o(t, "groups");
397
804
  }
398
- }, U = async (t) => {
805
+ }, L = async (t) => {
399
806
  try {
400
- await u.loadGroup(t);
401
- } catch (l) {
402
- o(l, "messages");
807
+ await l.loadGroup(t);
808
+ } catch (s) {
809
+ o(s, "messages");
403
810
  }
404
- }, n = async () => {
811
+ }, p = async () => {
405
812
  try {
406
- await g.loadAgents();
813
+ await c.loadAgents();
407
814
  } catch (t) {
408
815
  o(t, "agents");
409
816
  }
410
817
  }, i = async (t) => {
411
- var f;
412
- const l = typeof t == "string" ? { text: t } : t;
413
- await u.submitQuestion({
414
- ...l,
415
- entryId: (l == null ? void 0 : l.entryId) ?? ((f = g.currentAgent.value) == null ? void 0 : f.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)
416
823
  });
417
- const G = C;
418
- C = null, G && await G, await x();
824
+ const d = g;
825
+ g = null, d && await d, await x();
419
826
  };
420
- return ne(u.session, (t) => {
421
- t && (x(), n());
422
- }), ne(m.activeGroupId, (t) => {
423
- t && U(t);
827
+ return ue(l.session, (t) => {
828
+ t && (x(), p());
829
+ }), ue(h.activeGroupId, (t) => {
830
+ t && L(t);
424
831
  }), {
425
- chat: u,
426
- history: m,
427
- agent: g,
428
- parsers: p,
429
- messages: u.messages,
430
- question: u.question,
431
- incPrevQuestion: u.incPrevQuestion,
432
- talking: u.talking,
433
- loading: u.loading,
434
- session: u.session,
435
- groups: m.groups,
436
- activeGroupId: m.activeGroupId,
437
- historyLoading: m.loading,
438
- agentList: g.agentList,
439
- currentAgent: g.currentAgent,
440
- agentLoading: g.loading,
441
- hasMore: u.hasMore,
442
- loadMoreLoading: u.loadMoreLoading,
443
- loadGroup: U,
444
- loadOlder: u.loadOlder,
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,
445
852
  refreshGroups: x,
446
- loadAgents: n,
447
- selectAgent: g.selectAgent,
448
- getFileUrl: g.getFileUrl,
853
+ loadAgents: p,
854
+ selectAgent: c.selectAgent,
855
+ getFileUrl: c.getFileUrl,
449
856
  submitQuestion: i,
450
- sendWsMessage: u.sendWsMessage,
451
- stopQuestion: u.stopQuestion
857
+ sendWsMessage: l.sendWsMessage,
858
+ stopQuestion: l.stopQuestion
452
859
  };
453
860
  };
454
861
  export {
455
- Oe as useAgent,
456
- Ue as useChat,
457
- We as useChatSession,
458
- qe 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
459
874
  };
460
875
  //# sourceMappingURL=index.js.map