wi-chat 0.0.0-stage → 0.4.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 (99) hide show
  1. package/dist/core/index.cjs +2 -0
  2. package/dist/core/index.d.ts +13 -0
  3. package/dist/core/index.js +454 -0
  4. package/dist/core/useAgent.d.ts +35 -0
  5. package/dist/core/useChat.d.ts +195 -0
  6. package/dist/core/useChatSession.d.ts +89 -0
  7. package/dist/core/useHistory.d.ts +63 -0
  8. package/dist/index.cjs +2 -0
  9. package/dist/index.d.ts +11 -0
  10. package/dist/index.js +81 -0
  11. package/dist/mobile-ui/chat/ChatContainer.vue.d.ts +129 -0
  12. package/dist/mobile-ui/chat/ChatMessagesView.vue.d.ts +77 -0
  13. package/dist/mobile-ui/chat/types.d.ts +9 -0
  14. package/dist/mobile-ui/index.cjs +2 -0
  15. package/dist/mobile-ui/index.d.ts +7 -0
  16. package/dist/mobile-ui/index.js +225 -0
  17. package/dist/mobile-ui/inputbox/Inputbox.vue.d.ts +95 -0
  18. package/dist/mobile-ui/style.css +1 -0
  19. package/dist/parser/answer.d.ts +51 -0
  20. package/dist/parser/answerCom/iframePage/parseIframePage.d.ts +49 -0
  21. package/dist/parser/answerCom/iframePage/types.d.ts +11 -0
  22. package/dist/parser/answerCom/knowledgeStream/parseKnowledgeStream.d.ts +54 -0
  23. package/dist/parser/answerCom/knowledgeStream/types.d.ts +27 -0
  24. package/dist/parser/answerCom/outputFile/parseOutputFile.d.ts +43 -0
  25. package/dist/parser/answerCom/outputFile/types.d.ts +17 -0
  26. package/dist/parser/answerCom/recordset/parseRecordset.d.ts +48 -0
  27. package/dist/parser/answerCom/recordset/types.d.ts +36 -0
  28. package/dist/parser/attach/attachActions.d.ts +43 -0
  29. package/dist/parser/attach/index.d.ts +9 -0
  30. package/dist/parser/attach/parseAttach.d.ts +27 -0
  31. package/dist/parser/attach/types.d.ts +48 -0
  32. package/dist/parser/deepThinking/parseDeepThinking.d.ts +30 -0
  33. package/dist/parser/deepThinking/types.d.ts +14 -0
  34. package/dist/parser/history/history.d.ts +54 -0
  35. package/dist/parser/history/index.d.ts +7 -0
  36. package/dist/parser/index.cjs +3 -0
  37. package/dist/parser/index.d.ts +45 -0
  38. package/dist/parser/index.js +479 -0
  39. package/dist/parser/message.d.ts +60 -0
  40. package/dist/parser/registry.d.ts +81 -0
  41. package/dist/parser/sse.d.ts +34 -0
  42. package/dist/parser/stream.d.ts +8 -0
  43. package/dist/renderers/aiAnswer/AiAnswerBubble.vue.d.ts +58 -0
  44. package/dist/renderers/aiAnswer/contentMap.d.ts +13 -0
  45. package/dist/renderers/deepThinking/DeepThinkingSteps.vue.d.ts +21 -0
  46. package/dist/renderers/iframePage/IframePage.vue.d.ts +19 -0
  47. package/dist/renderers/iframePage/useIframePage.d.ts +64 -0
  48. package/dist/renderers/index.cjs +2 -0
  49. package/dist/renderers/index.d.ts +12 -0
  50. package/dist/renderers/index.js +609 -0
  51. package/dist/renderers/knowledgeStream/KnowledgeStreamResult.vue.d.ts +19 -0
  52. package/dist/renderers/outputFile/OutputFile.vue.d.ts +19 -0
  53. package/dist/renderers/outputFile/useOutputFile.d.ts +85 -0
  54. package/dist/renderers/style.css +1 -0
  55. package/dist/request/api.d.ts +132 -0
  56. package/dist/request/client.d.ts +30 -0
  57. package/dist/request/index.cjs +4 -0
  58. package/dist/request/index.d.ts +18 -0
  59. package/dist/request/index.js +264 -0
  60. package/dist/request/login.d.ts +37 -0
  61. package/dist/request/ws.d.ts +91 -0
  62. package/dist/style.css +24 -0
  63. package/dist/styles/github.css +2 -0
  64. package/dist/styles/katex.css +2 -0
  65. package/dist/theme/index.cjs +3 -0
  66. package/dist/theme/index.d.ts +3 -0
  67. package/dist/theme/index.js +37 -0
  68. package/dist/theme/theme.d.ts +40 -0
  69. package/dist/tools/attachDownload.d.ts +20 -0
  70. package/dist/tools/index.cjs +17 -0
  71. package/dist/tools/index.d.ts +8 -0
  72. package/dist/tools/index.js +251 -0
  73. package/dist/tools/markdown/MarkdownContent.vue.d.ts +36 -0
  74. package/dist/tools/markdown/imagePreview.d.ts +15 -0
  75. package/dist/tools/markdown/index.d.ts +5 -0
  76. package/dist/tools/markdown/renderMarkdown.d.ts +22 -0
  77. package/dist/tools/markdown/tableDownload.d.ts +14 -0
  78. package/dist/tools/stream.d.ts +9 -0
  79. package/dist/tools/style.css +1 -0
  80. package/dist/tools/useAutoScroll.d.ts +31 -0
  81. package/dist/web-ui/attach/AttachmentCard.vue.d.ts +91 -0
  82. package/dist/web-ui/attach/AttachmentList.vue.d.ts +94 -0
  83. package/dist/web-ui/attach/types.d.ts +5 -0
  84. package/dist/web-ui/chat/ChatContainer.vue.d.ts +165 -0
  85. package/dist/web-ui/chat/ChatMessagesView.vue.d.ts +88 -0
  86. package/dist/web-ui/chat/types.d.ts +24 -0
  87. package/dist/web-ui/envswitch/EnvSwitch.vue.d.ts +67 -0
  88. package/dist/web-ui/envswitch/types.d.ts +22 -0
  89. package/dist/web-ui/index.cjs +2 -0
  90. package/dist/web-ui/index.d.ts +14 -0
  91. package/dist/web-ui/index.js +1048 -0
  92. package/dist/web-ui/inputbox/Inputbox.vue.d.ts +215 -0
  93. package/dist/web-ui/recordset/RecordsetTable.vue.d.ts +37 -0
  94. package/dist/web-ui/recordset/webAnswerContentMap.d.ts +9 -0
  95. package/dist/web-ui/style.css +1 -0
  96. package/package.json +124 -4
  97. package/styles/github.css +2 -0
  98. package/styles/katex.css +2 -0
  99. package/README.md +0 -3
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("vue"),L=require("wi-chat/parser"),X=require("wi-chat/request"),ne=e=>{var ee;const p=e.login,C=e.defaultGroupTitle??"新对话",A=e.lastCount??10,o=e.autoSetGroupTitleOnFirstQuestion??!0,G=e.beforeKnowledgeRender||(ee=e.parsers)!=null&&ee.length?{beforeKnowledgeRender:e.beforeKnowledgeRender,parsers:e.parsers}:void 0,v=l.ref(""),i=l.ref([]),x=l.ref(""),q=l.ref(e.incPrevQuestion??"Y"),s=l.ref(!1),c=l.ref(!1),t=l.ref(""),u=l.ref(!1),m=l.ref(""),d=l.ref(!0),y=l.ref(!1),I=l.ref(0),D=e.api?null:X.createRequest({baseURL:e.baseURL??"",getWisession:()=>v.value}),T=e.api??X.createChatApi(D);let S=null,M=null,k=0,_=0,V=0,B=null,P=[];const a=async()=>{if(m.value="",!D||!p){u.value=!0;return}try{const n=await X.login(D,p);return v.value=n.wisession||"",u.value=!0,n}catch(n){throw m.value=n instanceof Error?n.message:String(n),n}},r=n=>n===k,Q=n=>{typeof URL>"u"||typeof URL.revokeObjectURL!="function"||n.forEach(w=>{w.role!=="user"||!w.attachments||w.attachments.forEach(h=>{var b;const E=(b=h.attachContent)==null?void 0:b.url;E&&E.startsWith("blob:")&&URL.revokeObjectURL(E)})})},F=async n=>{const w=++_;V+=1,s.value=!0,Q(i.value),i.value=[],t.value=n,d.value=!0,y.value=!1,I.value=0;const h=[],E=()=>{const g=[];h.forEach(O=>{g.push(O.user),O.assistant&&g.push(O.assistant)}),w===_&&(i.value=g)},b=g=>{w===_&&(g.type==="users"?(h.length=0,g.users.forEach(O=>h.push({user:O}))):h[g.index]&&(g.user&&(h[g.index].user=g.user),h[g.index].assistant=g.assistant),E())};try{await L.loadGroupMessagesByStep(T,n,A,b,0,G),E(),w===_&&(I.value=h.length,h.length<A&&(d.value=!1))}finally{V-=1,V<=0&&(V=0,s.value=!1)}},Y=async()=>{if(!t.value||s.value||!d.value||y.value)return;const n=t.value,w=_,h=I.value;y.value=!0;try{const E=await L.loadGroupMessagesByStep(T,n,A,void 0,h,G),b=E.filter(g=>g.role==="user").length;if(b<A&&(d.value=!1),w===_&&t.value===n){const g=new Set;i.value.forEach(R=>{R.historyId&&g.add(R.historyId),R.id&&g.add(R.id)});const O=E.filter(R=>!(R.historyId&&g.has(R.historyId)||R.id&&g.has(R.id)));i.value=[...O,...i.value],I.value+=b}}finally{y.value=!1}},oe=async(n=C)=>{const w=await L.createHistoryGroup(T,n);return t.value=w,w},$=()=>{k+=1,_+=1,M&&(M.stopped=!0,M.thinking.expanded=!1),S==null||S.abort(),S=null,B=null,P=[],M=null,c.value=!1},ie=()=>{M&&(M.error=!0,M.thinking.expanded=!1)},ce=async n=>{var ae,se;const w=typeof n=="string"?{text:n}:n||{},{text:h,entryId:E,files:b,attachments:g,extArgs:O,...R}=w,j=(h!==void 0?h:x.value).trim(),ge=O??e.extArgs??{},ve=b&&b.length>0?b:(g??[]).map(K=>{var z;return(z=K.attachContent)==null?void 0:z.raw}).filter(K=>!!K);if(!j||c.value||!t.value)return;const H=k+1;k=H,_+=1;let te=!1;try{c.value=!0;const K=t.value,z=g||(b||[]).map(f=>{var W;const J=f.name.includes(".")&&((W=f.name.split(".").pop())==null?void 0:W.toLowerCase())||"";return{type:"file",name:f.name,description:J?J.toUpperCase():"",attachContent:{file_name:f.name,size:f.size,suffix:J,raw:f}}}),ye=I.value===0&&!i.value.some(f=>f.role==="user"),Z=K,re=l.reactive({id:L.nextLocalMessageId("user"),role:"user",content:j,attachments:z.length>0?z:void 0});i.value.push(re);const U=l.reactive({id:L.nextLocalMessageId("answer"),role:"assistant",answer:L.createEmptyAnswerValues("",j),error:!1,stopped:!1,thinking:{steps:[],expanded:!0}});if(M=U,i.value.push(U),x.value="",(ae=e.onSubmitQuestion)==null||ae.call(e,j),ye&&Z){const f=j.replace(/[\r\n]+/g," ").trim().slice(0,100);e.onFirstQuestion?e.onFirstQuestion({question:f,groupId:Z}):o&&f&&T.setHistoryGroupTitle(Z,f).catch(()=>{})}S=new AbortController;const he=await T.questionStream({...R,question:j,historyGroupId:K,entryId:E,files:ve,ext_args:JSON.stringify(ge),incPrevQuestion:q.value,onOpen:f=>{if(!r(H))return;B=f;const J=P;P=[],J.forEach(({payloadText:W,chunkSize:N})=>f(W,N))}},S.signal);I.value+=1,await L.consumeStream(he,f=>{var W;if(!r(H))return;if(f.mode==="history_id"){const N=typeof f.value=="object"&&f.value!==null?f.value.history_id:f.value;N&&(re.historyId=String(N),U.historyId=String(N),U.answer&&(U.answer.id=String(N)))}L.applyAnswerChunk(U.answer,f,G).includes("deep_thinking")&&(U.thinking.steps=L.collectThinkingSteps(U.answer)),f.mode==="finish"&&(U.thinking.expanded=!1),(W=e.onChunk)==null||W.call(e,f)}),r(H)&&(U.thinking.expanded=!1),r(H)&&(te=!0)}catch(K){r(H)&&!X.isCancelError(K)&&ie()}finally{r(H)&&(c.value=!1,S=null,M=null,B=null,P=[])}te&&((se=e.onFinish)==null||se.call(e))},fe=(n,w)=>{if(!c.value)return!1;const h=typeof n=="string"?n:JSON.stringify(n)??"";return B?B(h,w):P.push({payloadText:h,chunkSize:w}),!0},de=e.autoLogin!==!1?a().then(()=>{}).catch(()=>{}):Promise.resolve();return l.getCurrentInstance()&&l.onBeforeUnmount(()=>{$(),Q(i.value)}),{messages:i,question:x,incPrevQuestion:q,talking:c,loading:s,session:v,autoLoginDone:u,autoLoginError:m,loginDone:de,historyGroupId:t,selfRequest:D,api:T,doAutoLogin:a,loadGroup:F,loadOlder:Y,hasMore:d,loadMoreLoading:y,createGroup:oe,submitQuestion:ce,sendWsMessage:fe,stopQuestion:$}},pe="wi-chat-history-groups",le=e=>{var P;const{api:p,defaultGroupTitle:C="新对话",lastCount:A=10,ensureGroup:o=!0,autoSelectFirst:G=!0,autoLoadMessages:v=!0}=e,i=e.persistGroups??!1,x=e.storageKey??pe,q=e.beforeKnowledgeRender||(P=e.parsers)!=null&&P.length?{beforeKnowledgeRender:e.beforeKnowledgeRender,parsers:e.parsers}:void 0,s=l.ref([]),c=l.ref(""),t=l.ref([]),u=l.ref(!1);let m=!1,d=null;const y=()=>{if(d===!1)return null;let a;try{a=localStorage.getItem(x)}catch{return d=!1,null}if(a===null)return[];try{const r=JSON.parse(a);return Array.isArray(r)?r:[]}catch{return[]}},I=a=>{if(d!==!1)try{localStorage.setItem(x,JSON.stringify(a))}catch{d=!1}},D=a=>{const r=y();r!==null&&I([...r,a])},T=async(a,r=A)=>{u.value=!0;try{t.value=a?await L.loadGroupMessages(p,a,r,0,q):[]}finally{u.value=!1}},S=async a=>{c.value=a,v&&await T(a)},M=async()=>{if(!m){m=!0;try{s.value.length===0?o&&await _(C):!c.value&&G&&await S(s.value[0].group_id)}finally{m=!1}}},k=async()=>{u.value=!0;try{const a=await L.getHistoryGroups(p);if(i){const r=y();if(r===null)s.value=a;else{const Q=new Set(r.map(Y=>Y.group_id)),F=a.filter(Y=>Q.has(Y.group_id));I(F),s.value=F}}else s.value=a;await M()}finally{u.value=!1}},_=async(a=C)=>{const r=await L.createHistoryGroup(p,a);return i&&D({group_id:r,group_title:a}),await k(),c.value!==r&&await S(r),r};return{groups:s,activeGroupId:c,messages:t,loading:u,loadGroups:k,createGroup:_,renameGroup:async(a,r)=>{await L.setHistoryGroupTitle(p,a,r);const Q=s.value.find(F=>F.group_id===a);Q&&(Q.group_title=r),i&&I(s.value)},deleteGroup:async a=>{const r=s.value.findIndex(F=>F.group_id===a);if(await L.deleteHistoryGroup(p,a),s.value=s.value.filter(F=>F.group_id!==a),i&&I(s.value),c.value!==a)return;const Q=s.value[r]??s.value[s.value.length-1];if(Q){await S(Q.group_id);return}o?await _(C):(c.value="",t.value=[])},selectGroup:S,loadMessages:T}},ue=e=>{const{api:p,defaultEntryId:C,entryGroup:A="智能助手",autoLoad:o=!0}=e,G=l.ref([]),v=l.ref(null),i=l.ref(!1),x=c=>c?typeof p.getFileUrl=="function"?p.getFileUrl(c):L.resolveRemoteFileUrl(c,"","/algorithm/get_file_data"):"",q=c=>{v.value=c},s=async()=>{var c;i.value=!0;try{let t=C||((c=v.value)==null?void 0:c.entry_id);if(!t)try{const y=await p.getDefaultLlmId();t=y==null?void 0:y.default_entry_id}catch(y){console.warn("[useAgent] 获取默认智能体 ID 失败:",y)}const u=await p.getLlmEntryListByRole({entry_group:A}),d=((u==null?void 0:u.values)||[]).filter(y=>y.entry_enable!==!1);if(G.value=d,t){const y=d.find(I=>I.entry_id===t);y?v.value=y:d.length>0&&(v.value=d[0])}else d.length>0&&!v.value&&(v.value=d[0])}finally{i.value=!1}};return o&&(l.getCurrentInstance()?l.onMounted(()=>{s()}):s()),{agentList:G,currentAgent:v,loading:i,getFileUrl:x,loadAgents:s,selectAgent:q}},we=(e={})=>{const p=e.parsers??[];let C=null,A;const o=ne({...e.chat,parsers:p,onFirstQuestion:({question:t,groupId:u})=>{A==null||A(u,t)}}),G=le({...e.history,api:o.api,autoLoadMessages:!1,parsers:p});A=(t,u)=>{C=G.renameGroup(t,u).catch(m=>{var d;(d=e.onError)==null||d.call(e,m,"groups")})};const v=ue({...e.agent,api:o.api,autoLoad:!1}),i=(t,u)=>{var m;(m=e.onError)==null||m.call(e,t,u)},x=async()=>{try{await G.loadGroups()}catch(t){i(t,"groups")}},q=async t=>{try{await o.loadGroup(t)}catch(u){i(u,"messages")}},s=async()=>{try{await v.loadAgents()}catch(t){i(t,"agents")}},c=async t=>{var d;const u=typeof t=="string"?{text:t}:t;await o.submitQuestion({...u,entryId:(u==null?void 0:u.entryId)??((d=v.currentAgent.value)==null?void 0:d.entry_id)});const m=C;C=null,m&&await m,await x()};return l.watch(o.session,t=>{t&&(x(),s())}),l.watch(G.activeGroupId,t=>{t&&q(t)}),{chat:o,history:G,agent:v,parsers:p,messages:o.messages,question:o.question,incPrevQuestion:o.incPrevQuestion,talking:o.talking,loading:o.loading,session:o.session,groups:G.groups,activeGroupId:G.activeGroupId,historyLoading:G.loading,agentList:v.agentList,currentAgent:v.currentAgent,agentLoading:v.loading,hasMore:o.hasMore,loadMoreLoading:o.loadMoreLoading,loadGroup:q,loadOlder:o.loadOlder,refreshGroups:x,loadAgents:s,selectAgent:v.selectAgent,getFileUrl:v.getFileUrl,submitQuestion:c,sendWsMessage:o.sendWsMessage,stopQuestion:o.stopQuestion}};exports.useAgent=ue;exports.useChat=ne;exports.useChatSession=we;exports.useHistory=le;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,13 @@
1
+ /**
2
+ * wi-chat/core —— 对话状态机(core)
3
+ * 承载 useChat、useHistory、useAgent、useChatSession:消息状态、发送、停止、按分组加载历史、智能体选择等对话流程编排。
4
+ * 网络/登录来自 wi-chat/request,解析/历史业务逻辑来自 wi-chat/parser,基础 composable 来自 wi-chat/tools。
5
+ */
6
+ export { useChat } from './useChat';
7
+ export type { SubmitQuestionOptions, UseChatOptions, UseChatReturn } from './useChat';
8
+ export { useHistory } from './useHistory';
9
+ export type { UseHistoryOptions, UseHistoryReturn } from './useHistory';
10
+ export { useAgent } from './useAgent';
11
+ export type { UseAgentOptions, UseAgentReturn } from './useAgent';
12
+ export { useChatSession } from './useChatSession';
13
+ export type { UseChatSessionAgentOptions, UseChatSessionChatOptions, UseChatSessionErrorScope, UseChatSessionHistoryOptions, UseChatSessionOptions, UseChatSessionReturn, } from './useChatSession';
@@ -0,0 +1,454 @@
1
+ import { ref as y, getCurrentInstance as ue, onBeforeUnmount as we, reactive as se, onMounted as me, watch as re } from "vue";
2
+ import { loadGroupMessagesByStep as ne, createHistoryGroup as oe, nextLocalMessageId as le, createEmptyAnswerValues as Ge, consumeStream as Le, applyAnswerChunk as Ae, collectThinkingSteps as xe, loadGroupMessages as Ie, deleteHistoryGroup as _e, setHistoryGroupTitle as be, getHistoryGroups as Se, resolveRemoteFileUrl as Ce } from "wi-chat/parser";
3
+ import { createRequest as Ee, createChatApi as Me, login as Re, isCancelError as Qe } from "wi-chat/request";
4
+ const Fe = (e) => {
5
+ var Z;
6
+ const p = e.login, S = e.defaultGroupTitle ?? "新对话", L = e.lastCount ?? 10, u = e.autoSetGroupTitleOnFirstQuestion ?? !0, m = e.beforeKnowledgeRender || (Z = e.parsers) != null && Z.length ? { beforeKnowledgeRender: e.beforeKnowledgeRender, parsers: e.parsers } : void 0, g = y(""), o = y([]), x = y(""), U = y(e.incPrevQuestion ?? "Y"), s = 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 : Ee({
7
+ baseURL: e.baseURL ?? "",
8
+ getWisession: () => g.value
9
+ }), q = e.api ?? Me(D);
10
+ let I = null, C = null, P = 0, _ = 0, j = 0, H = null, O = [];
11
+ const a = async () => {
12
+ if (G.value = "", !D || !p) {
13
+ l.value = !0;
14
+ return;
15
+ }
16
+ try {
17
+ const n = await Re(D, p);
18
+ return g.value = n.wisession || "", l.value = !0, n;
19
+ } catch (n) {
20
+ throw G.value = n instanceof Error ? n.message : String(n), n;
21
+ }
22
+ }, r = (n) => n === P, R = (n) => {
23
+ typeof URL > "u" || typeof URL.revokeObjectURL != "function" || n.forEach((w) => {
24
+ w.role !== "user" || !w.attachments || w.attachments.forEach((h) => {
25
+ var b;
26
+ const E = (b = h.attachContent) == null ? void 0 : b.url;
27
+ E && E.startsWith("blob:") && URL.revokeObjectURL(E);
28
+ });
29
+ });
30
+ }, Q = async (n) => {
31
+ const w = ++_;
32
+ j += 1, s.value = !0, R(o.value), o.value = [], t.value = n, f.value = !0, v.value = !1, A.value = 0;
33
+ const h = [], E = () => {
34
+ const d = [];
35
+ h.forEach((T) => {
36
+ d.push(T.user), T.assistant && d.push(T.assistant);
37
+ }), w === _ && (o.value = d);
38
+ }, b = (d) => {
39
+ w === _ && (d.type === "users" ? (h.length = 0, d.users.forEach((T) => h.push({ user: T }))) : h[d.index] && (d.user && (h[d.index].user = d.user), h[d.index].assistant = d.assistant), E());
40
+ };
41
+ try {
42
+ await ne(q, n, L, b, 0, m), E(), w === _ && (A.value = h.length, h.length < L && (f.value = !1));
43
+ } finally {
44
+ j -= 1, j <= 0 && (j = 0, s.value = !1);
45
+ }
46
+ }, V = async () => {
47
+ if (!t.value || s.value || !f.value || v.value)
48
+ return;
49
+ const n = t.value, w = _, h = A.value;
50
+ v.value = !0;
51
+ try {
52
+ const E = await ne(q, n, L, void 0, h, m), b = E.filter((d) => d.role === "user").length;
53
+ if (b < L && (f.value = !1), w === _ && t.value === n) {
54
+ const d = /* @__PURE__ */ new Set();
55
+ o.value.forEach((M) => {
56
+ M.historyId && d.add(M.historyId), M.id && d.add(M.id);
57
+ });
58
+ const T = E.filter((M) => !(M.historyId && d.has(M.historyId) || M.id && d.has(M.id)));
59
+ o.value = [...T, ...o.value], A.value += b;
60
+ }
61
+ } finally {
62
+ v.value = !1;
63
+ }
64
+ }, ie = async (n = S) => {
65
+ const w = await oe(q, n);
66
+ return t.value = w, w;
67
+ }, X = () => {
68
+ P += 1, _ += 1, C && (C.stopped = !0, C.thinking.expanded = !1), I == null || I.abort(), I = null, H = null, O = [], C = null, i.value = !1;
69
+ }, ce = () => {
70
+ C && (C.error = !0, C.thinking.expanded = !1);
71
+ }, fe = async (n) => {
72
+ var ee, te;
73
+ const w = typeof n == "string" ? { text: n } : n || {}, { text: h, entryId: E, files: b, attachments: d, extArgs: T, ...M } = w, B = (h !== void 0 ? h : x.value).trim(), ve = T ?? e.extArgs ?? {}, ye = b && b.length > 0 ? b : (d ?? []).map((K) => {
74
+ var Y;
75
+ return (Y = K.attachContent) == null ? void 0 : Y.raw;
76
+ }).filter((K) => !!K);
77
+ if (!B || i.value || !t.value)
78
+ return;
79
+ const k = P + 1;
80
+ P = k, _ += 1;
81
+ let $ = !1;
82
+ try {
83
+ i.value = !0;
84
+ const K = t.value, Y = d || (b || []).map((c) => {
85
+ var W;
86
+ const J = c.name.includes(".") && ((W = c.name.split(".").pop()) == null ? void 0 : W.toLowerCase()) || "";
87
+ return {
88
+ type: "file",
89
+ name: c.name,
90
+ description: J ? J.toUpperCase() : "",
91
+ attachContent: {
92
+ file_name: c.name,
93
+ size: c.size,
94
+ suffix: J,
95
+ raw: c
96
+ }
97
+ };
98
+ }), he = A.value === 0 && !o.value.some((c) => c.role === "user"), z = K, ae = se({
99
+ id: le("user"),
100
+ role: "user",
101
+ content: B,
102
+ attachments: Y.length > 0 ? Y : void 0
103
+ });
104
+ o.value.push(ae);
105
+ const F = se({
106
+ id: le("answer"),
107
+ role: "assistant",
108
+ answer: Ge("", B),
109
+ error: !1,
110
+ stopped: !1,
111
+ thinking: { steps: [], expanded: !0 }
112
+ });
113
+ if (C = F, o.value.push(F), x.value = "", (ee = e.onSubmitQuestion) == null || ee.call(e, B), he && z) {
114
+ const c = B.replace(/[\r\n]+/g, " ").trim().slice(0, 100);
115
+ e.onFirstQuestion ? e.onFirstQuestion({ question: c, groupId: z }) : u && c && q.setHistoryGroupTitle(z, c).catch(() => {
116
+ });
117
+ }
118
+ I = new AbortController();
119
+ const pe = await q.questionStream(
120
+ {
121
+ // 先铺开外部透传的额外字段(raw_mode / tables / metrics / group_type 等),
122
+ // 已知字段随后覆盖,避免同名额外字段篡改核心参数
123
+ ...M,
124
+ question: B,
125
+ historyGroupId: K,
126
+ entryId: E,
127
+ files: ye,
128
+ ext_args: JSON.stringify(ve),
129
+ incPrevQuestion: U.value,
130
+ // 连接建立且首包发完后接收分片发送器,并按序补发连接建立前入队的追发消息
131
+ // (sendWsMessage 在连接未就绪时调用会先入队,见 sendWsMessage)
132
+ onOpen: (c) => {
133
+ if (!r(k))
134
+ return;
135
+ H = c;
136
+ const J = O;
137
+ O = [], J.forEach(({ payloadText: W, chunkSize: N }) => c(W, N));
138
+ }
139
+ },
140
+ I.signal
141
+ );
142
+ A.value += 1, await Le(pe, (c) => {
143
+ var W;
144
+ if (!r(k))
145
+ return;
146
+ if (c.mode === "history_id") {
147
+ const N = typeof c.value == "object" && c.value !== null ? c.value.history_id : c.value;
148
+ N && (ae.historyId = String(N), F.historyId = String(N), F.answer && (F.answer.id = String(N)));
149
+ }
150
+ Ae(F.answer, c, m).includes("deep_thinking") && (F.thinking.steps = xe(F.answer)), c.mode === "finish" && (F.thinking.expanded = !1), (W = e.onChunk) == null || W.call(e, c);
151
+ }), r(k) && (F.thinking.expanded = !1), r(k) && ($ = !0);
152
+ } catch (K) {
153
+ r(k) && !Qe(K) && ce();
154
+ } finally {
155
+ r(k) && (i.value = !1, I = null, C = null, H = null, O = []);
156
+ }
157
+ $ && ((te = e.onFinish) == null || te.call(e));
158
+ }, de = (n, w) => {
159
+ if (!i.value)
160
+ return !1;
161
+ const h = typeof n == "string" ? n : JSON.stringify(n) ?? "";
162
+ return H ? H(h, w) : O.push({ payloadText: h, chunkSize: w }), !0;
163
+ }, ge = e.autoLogin !== !1 ? a().then(() => {
164
+ }).catch(() => {
165
+ }) : Promise.resolve();
166
+ return ue() && we(() => {
167
+ X(), R(o.value);
168
+ }), {
169
+ /** 当前分组的消息列表(user / assistant 交错;loadGroup / loadOlder / submitQuestion 会更新) */
170
+ messages: o,
171
+ /** 输入框内容(v-model 绑定;submitQuestion 成功后清空) */
172
+ question: x,
173
+ /**
174
+ * 提问是否携带之前对话上下文(inc_prev_question:'Y' 不忽略 / 'N' 忽略,默认 'Y')。
175
+ * submitQuestion 提交时读取当前值发送;可在外部随时修改(如绑定输入框组件的勾选开关)。
176
+ */
177
+ incPrevQuestion: U,
178
+ /** 会话发送 / 流式回答进行中(true 时 submitQuestion 不再受理新提问) */
179
+ talking: i,
180
+ /** 历史加载中(首屏 loadGroup 及并发加载全部完成后结束) */
181
+ loading: s,
182
+ /** 当前会话 token(自动登录写入;未自动登录时可自行赋值) */
183
+ session: g,
184
+ /** 自动登录流程已结束(成功 / 失败 / 跳过均置 true;UI 可等待它再渲染登录状态) */
185
+ autoLoginDone: l,
186
+ /** 最近一次自动登录的错误信息(UI 可据此提示重试) */
187
+ autoLoginError: G,
188
+ /** 自动登录完成时机(无论成败都会 resolve;宿主用它编排"登录后才能发"的请求,如加载历史分组) */
189
+ loginDone: ge,
190
+ /** 当前会话所在的历史分组 id */
191
+ historyGroupId: t,
192
+ /** 内部自建的 request 客户端(注入 api 时为 null,无使用价值) */
193
+ selfRequest: D,
194
+ /** 实际使用的业务 API(外部注入的实例或内部自建) */
195
+ api: q,
196
+ /** 手动触发一次自动登录(成功返回登录结果;失败抛错并记录到 autoLoginError) */
197
+ doAutoLogin: a,
198
+ /** 按分组加载消息(插空式渐进渲染;并发调用时仅最新一次生效) */
199
+ loadGroup: Q,
200
+ /** 加载更早一页历史(向上翻页),前插到消息列表顶部 */
201
+ loadOlder: V,
202
+ /** 是否仍可能有更早历史(向上翻页) */
203
+ hasMore: f,
204
+ /** 向上翻页加载中(触顶防重) */
205
+ loadMoreLoading: v,
206
+ /** 新建历史分组并选中(返回分组 id) */
207
+ createGroup: ie,
208
+ /** 提交问题并走 SSE 流式问答(支持传参 entryId、files 与文本;无文本 / 发送中 / 无分组时不发送) */
209
+ submitQuestion: fe,
210
+ /**
211
+ * 向当前提问的 WebSocket 连接追发自定义消息(B/M/E 分帧):
212
+ * 连接就绪立即发出、未就绪入队待连接建立后按序补发;仅在 talking 期间受理,
213
+ * 无在途会话返回 false。见 sendWsMessage 的 JSDoc。
214
+ */
215
+ sendWsMessage: de,
216
+ /** 停止在途会话:中断流、把当前回答标记为 stopped,并复位 talking */
217
+ stopQuestion: X
218
+ };
219
+ }, Ue = "wi-chat-history-groups", qe = (e) => {
220
+ var O;
221
+ const {
222
+ api: p,
223
+ defaultGroupTitle: S = "新对话",
224
+ lastCount: L = 10,
225
+ ensureGroup: u = !0,
226
+ autoSelectFirst: m = !0,
227
+ autoLoadMessages: g = !0
228
+ } = e, o = e.persistGroups ?? !1, x = e.storageKey ?? Ue, U = e.beforeKnowledgeRender || (O = e.parsers) != null && O.length ? { beforeKnowledgeRender: e.beforeKnowledgeRender, parsers: e.parsers } : void 0, s = y([]), i = y(""), t = y([]), l = y(!1);
229
+ let G = !1, f = null;
230
+ const v = () => {
231
+ if (f === !1)
232
+ return null;
233
+ let a;
234
+ try {
235
+ a = localStorage.getItem(x);
236
+ } catch {
237
+ return f = !1, null;
238
+ }
239
+ if (a === null)
240
+ return [];
241
+ try {
242
+ const r = JSON.parse(a);
243
+ return Array.isArray(r) ? r : [];
244
+ } catch {
245
+ return [];
246
+ }
247
+ }, A = (a) => {
248
+ if (f !== !1)
249
+ try {
250
+ localStorage.setItem(x, JSON.stringify(a));
251
+ } catch {
252
+ f = !1;
253
+ }
254
+ }, D = (a) => {
255
+ const r = v();
256
+ r !== null && A([...r, a]);
257
+ }, q = async (a, r = L) => {
258
+ l.value = !0;
259
+ try {
260
+ t.value = a ? await Ie(p, a, r, 0, U) : [];
261
+ } finally {
262
+ l.value = !1;
263
+ }
264
+ }, I = async (a) => {
265
+ i.value = a, g && await q(a);
266
+ }, C = async () => {
267
+ if (!G) {
268
+ G = !0;
269
+ try {
270
+ s.value.length === 0 ? u && await _(S) : !i.value && m && await I(s.value[0].group_id);
271
+ } finally {
272
+ G = !1;
273
+ }
274
+ }
275
+ }, P = async () => {
276
+ l.value = !0;
277
+ try {
278
+ const a = await Se(p);
279
+ if (o) {
280
+ const r = v();
281
+ if (r === null)
282
+ s.value = a;
283
+ else {
284
+ const R = new Set(r.map((V) => V.group_id)), Q = a.filter((V) => R.has(V.group_id));
285
+ A(Q), s.value = Q;
286
+ }
287
+ } else
288
+ s.value = a;
289
+ await C();
290
+ } finally {
291
+ l.value = !1;
292
+ }
293
+ }, _ = async (a = S) => {
294
+ const r = await oe(p, a);
295
+ return o && D({ group_id: r, group_title: a }), await P(), i.value !== r && await I(r), r;
296
+ };
297
+ return {
298
+ groups: s,
299
+ activeGroupId: i,
300
+ messages: t,
301
+ loading: l,
302
+ loadGroups: P,
303
+ createGroup: _,
304
+ renameGroup: async (a, r) => {
305
+ await be(p, a, r);
306
+ const R = s.value.find((Q) => Q.group_id === a);
307
+ R && (R.group_title = r), o && A(s.value);
308
+ },
309
+ deleteGroup: async (a) => {
310
+ const r = s.value.findIndex((Q) => Q.group_id === a);
311
+ if (await _e(p, a), s.value = s.value.filter((Q) => Q.group_id !== a), o && A(s.value), i.value !== a)
312
+ return;
313
+ const R = s.value[r] ?? s.value[s.value.length - 1];
314
+ if (R) {
315
+ await I(R.group_id);
316
+ return;
317
+ }
318
+ u ? await _(S) : (i.value = "", t.value = []);
319
+ },
320
+ selectGroup: I,
321
+ loadMessages: q
322
+ };
323
+ }, Te = (e) => {
324
+ const { api: p, defaultEntryId: S, entryGroup: L = "智能助手", autoLoad: u = !0 } = e, m = y([]), g = y(null), o = y(!1), x = (i) => i ? typeof p.getFileUrl == "function" ? p.getFileUrl(i) : Ce(i, "", "/algorithm/get_file_data") : "", U = (i) => {
325
+ g.value = i;
326
+ }, s = async () => {
327
+ var i;
328
+ o.value = !0;
329
+ try {
330
+ let t = S || ((i = g.value) == null ? void 0 : i.entry_id);
331
+ if (!t)
332
+ try {
333
+ const v = await p.getDefaultLlmId();
334
+ t = v == null ? void 0 : v.default_entry_id;
335
+ } catch (v) {
336
+ console.warn("[useAgent] 获取默认智能体 ID 失败:", v);
337
+ }
338
+ const l = await p.getLlmEntryListByRole({ entry_group: L }), f = ((l == null ? void 0 : l.values) || []).filter((v) => v.entry_enable !== !1);
339
+ if (m.value = f, t) {
340
+ const v = f.find((A) => A.entry_id === t);
341
+ v ? g.value = v : f.length > 0 && (g.value = f[0]);
342
+ } else
343
+ f.length > 0 && !g.value && (g.value = f[0]);
344
+ } finally {
345
+ o.value = !1;
346
+ }
347
+ };
348
+ return u && (ue() ? me(() => {
349
+ s();
350
+ }) : s()), {
351
+ agentList: m,
352
+ currentAgent: g,
353
+ loading: o,
354
+ getFileUrl: x,
355
+ loadAgents: s,
356
+ selectAgent: U
357
+ };
358
+ }, ke = (e = {}) => {
359
+ const p = e.parsers ?? [];
360
+ let S = null, L;
361
+ const u = Fe({
362
+ ...e.chat,
363
+ parsers: p,
364
+ onFirstQuestion: ({ question: t, groupId: l }) => {
365
+ L == null || L(l, t);
366
+ }
367
+ }), m = qe({
368
+ ...e.history,
369
+ api: u.api,
370
+ autoLoadMessages: !1,
371
+ parsers: p
372
+ });
373
+ L = (t, l) => {
374
+ S = m.renameGroup(t, l).catch((G) => {
375
+ var f;
376
+ (f = e.onError) == null || f.call(e, G, "groups");
377
+ });
378
+ };
379
+ const g = Te({
380
+ ...e.agent,
381
+ api: u.api,
382
+ autoLoad: !1
383
+ }), o = (t, l) => {
384
+ var G;
385
+ (G = e.onError) == null || G.call(e, t, l);
386
+ }, x = async () => {
387
+ try {
388
+ await m.loadGroups();
389
+ } catch (t) {
390
+ o(t, "groups");
391
+ }
392
+ }, U = async (t) => {
393
+ try {
394
+ await u.loadGroup(t);
395
+ } catch (l) {
396
+ o(l, "messages");
397
+ }
398
+ }, s = async () => {
399
+ try {
400
+ await g.loadAgents();
401
+ } catch (t) {
402
+ o(t, "agents");
403
+ }
404
+ }, i = async (t) => {
405
+ var f;
406
+ const l = typeof t == "string" ? { text: t } : t;
407
+ await u.submitQuestion({
408
+ ...l,
409
+ entryId: (l == null ? void 0 : l.entryId) ?? ((f = g.currentAgent.value) == null ? void 0 : f.entry_id)
410
+ });
411
+ const G = S;
412
+ S = null, G && await G, await x();
413
+ };
414
+ return re(u.session, (t) => {
415
+ t && (x(), s());
416
+ }), re(m.activeGroupId, (t) => {
417
+ t && U(t);
418
+ }), {
419
+ chat: u,
420
+ history: m,
421
+ agent: g,
422
+ parsers: p,
423
+ messages: u.messages,
424
+ question: u.question,
425
+ incPrevQuestion: u.incPrevQuestion,
426
+ talking: u.talking,
427
+ loading: u.loading,
428
+ session: u.session,
429
+ groups: m.groups,
430
+ activeGroupId: m.activeGroupId,
431
+ historyLoading: m.loading,
432
+ agentList: g.agentList,
433
+ currentAgent: g.currentAgent,
434
+ agentLoading: g.loading,
435
+ hasMore: u.hasMore,
436
+ loadMoreLoading: u.loadMoreLoading,
437
+ loadGroup: U,
438
+ loadOlder: u.loadOlder,
439
+ refreshGroups: x,
440
+ loadAgents: s,
441
+ selectAgent: g.selectAgent,
442
+ getFileUrl: g.getFileUrl,
443
+ submitQuestion: i,
444
+ sendWsMessage: u.sendWsMessage,
445
+ stopQuestion: u.stopQuestion
446
+ };
447
+ };
448
+ export {
449
+ Te as useAgent,
450
+ Fe as useChat,
451
+ ke as useChatSession,
452
+ qe as useHistory
453
+ };
454
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,35 @@
1
+ import { Ref } from 'vue';
2
+ import { AgentEntryItem, ChatApi } from 'wi-chat/request';
3
+
4
+ /** useAgent 构造入参配置 */
5
+ export interface UseAgentOptions {
6
+ /** 业务 API 聚合实例(ChatApi) */
7
+ api: ChatApi;
8
+ /** 指定默认 entry_id(若提供则直接作为初始激活智能体,不再调用 getDefaultLlmId) */
9
+ defaultEntryId?: string;
10
+ /** 智能体分组名称,默认 '智能助手' */
11
+ entryGroup?: string;
12
+ /** 是否在初始化(onMounted)时自动加载智能体列表,默认 true */
13
+ autoLoad?: boolean;
14
+ }
15
+ /** useAgent 返回对象接口 */
16
+ export interface UseAgentReturn {
17
+ /** 智能体列表(仅包含已启用的项) */
18
+ agentList: Ref<AgentEntryItem[]>;
19
+ /** 当前选中的智能体对象,未选中时为 null */
20
+ currentAgent: Ref<AgentEntryItem | null>;
21
+ /** 当前是否正在加载智能体列表或默认 ID */
22
+ loading: Ref<boolean>;
23
+ /** 获取文件/图片完整访问地址 */
24
+ getFileUrl: (fileId: string) => string;
25
+ /** 重新加载智能体列表并同步选中状态 */
26
+ loadAgents: () => Promise<void>;
27
+ /** 切换当前选中的智能体 */
28
+ selectAgent: (agent: AgentEntryItem) => void;
29
+ }
30
+ /**
31
+ * 智能体选择管理 composable
32
+ * @param options 配置参数,见 UseAgentOptions
33
+ * @returns 状态与操作函数集合,见 UseAgentReturn
34
+ */
35
+ export declare const useAgent: (options: UseAgentOptions) => UseAgentReturn;