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.
- package/dist/core/index.cjs +1 -1
- package/dist/core/index.d.ts +12 -0
- package/dist/core/index.js +731 -328
- package/dist/core/useAnswerActions.d.ts +61 -0
- package/dist/core/useChat.d.ts +29 -3
- package/dist/core/useChatAuth.d.ts +96 -0
- package/dist/core/useChatRooms.d.ts +66 -0
- package/dist/core/useKeepAlive.d.ts +41 -0
- package/dist/core/useLlm.d.ts +73 -0
- package/dist/core/useRunningGroups.d.ts +58 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +119 -108
- package/dist/mobile-ui/chat/ChatContainer.vue.d.ts +2 -2
- package/dist/mobile-ui/inputbox/Inputbox.vue.d.ts +1 -1
- package/dist/parser/answerCom/generic/parseGeneric.d.ts +52 -0
- package/dist/parser/answerCom/generic/types.d.ts +75 -0
- package/dist/parser/answerCom/outputFile/parseOutputFile.d.ts +7 -6
- package/dist/parser/answerCom/outputFile/types.d.ts +15 -5
- package/dist/parser/answerCom/presentFiles/parsePresentFiles.d.ts +58 -0
- package/dist/parser/answerCom/presentFiles/types.d.ts +27 -0
- package/dist/parser/answerMeta.d.ts +71 -0
- package/dist/parser/deepThinking/parseDeepThinking.d.ts +8 -0
- package/dist/parser/history/history.d.ts +9 -0
- package/dist/parser/history/index.d.ts +1 -1
- package/dist/parser/index.cjs +6 -2
- package/dist/parser/index.d.ts +11 -2
- package/dist/parser/index.js +591 -366
- package/dist/parser/message.d.ts +9 -0
- package/dist/parser/runTime.d.ts +29 -0
- package/dist/renderers/TextAnswer-BgKGvG_o.cjs +2 -0
- package/dist/renderers/TextAnswer-PQwV3K7v.js +5 -0
- package/dist/renderers/answerCard/AnswerCard.vue.d.ts +65 -0
- package/dist/renderers/answerCard/AnswerToolbar.vue.d.ts +42 -0
- package/dist/renderers/answerConfig.d.ts +33 -0
- package/dist/renderers/deviceLastValue/DeviceLastValue.vue.d.ts +37 -0
- package/dist/renderers/index.cjs +6 -1
- package/dist/renderers/index.d.ts +14 -0
- package/dist/renderers/index.js +1485 -574
- package/dist/renderers/media/MediaAnswer.vue.d.ts +19 -0
- package/dist/renderers/outputFile/OutputFileLink.vue.d.ts +19 -0
- package/dist/renderers/outputFile/localFile.d.ts +46 -0
- package/dist/renderers/outputFile/useOutputFile.d.ts +32 -3
- package/dist/renderers/presentFiles/PresentFiles.vue.d.ts +19 -0
- package/dist/renderers/recordset/RecordsetView.vue.d.ts +19 -0
- package/dist/renderers/sqlQuery/SqlQuery.vue.d.ts +19 -0
- package/dist/renderers/style.css +1 -1
- package/dist/renderers/text/TextAnswer.vue.d.ts +19 -0
- package/dist/request/api.d.ts +121 -1
- package/dist/request/client.d.ts +6 -0
- package/dist/request/index.cjs +4 -2
- package/dist/request/index.d.ts +4 -2
- package/dist/request/index.js +753 -173
- package/dist/request/ws.d.ts +11 -86
- package/dist/request/wsCore.d.ts +104 -0
- package/dist/request/wsSession.d.ts +353 -0
- package/dist/right-panel/RightPanelTabs.vue.d.ts +4 -0
- package/dist/right-panel/index.cjs +1 -1
- package/dist/right-panel/index.d.ts +1 -1
- package/dist/right-panel/index.js +598 -499
- package/dist/right-panel/style.css +1 -1
- package/dist/right-panel/types.d.ts +10 -0
- package/dist/tools/index.cjs +5 -5
- package/dist/tools/index.d.ts +2 -2
- package/dist/tools/index.js +323 -208
- package/dist/tools/markdown/index.d.ts +2 -0
- package/dist/tools/markdown/interactions.d.ts +39 -0
- package/dist/tools/markdown/sanitize.d.ts +6 -0
- package/dist/tools/style.css +1 -1
- package/dist/web-ui/attach/AttachmentCard.vue.d.ts +1 -1
- package/dist/web-ui/attach/AttachmentList.vue.d.ts +1 -1
- package/dist/web-ui/chat/ChatContainer.vue.d.ts +5 -5
- package/dist/web-ui/envswitch/EnvSwitch.vue.d.ts +1 -1
- package/dist/web-ui/index.cjs +1 -1
- package/dist/web-ui/index.js +1 -1
- package/dist/web-ui/inputbox/Inputbox.vue.d.ts +5 -5
- package/dist/web-ui/style.css +1 -1
- package/package.json +2 -2
package/dist/core/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("vue"),h=require("wi-chat/parser"),Z=require("wi-chat/request"),me=e=>{const u={requestId:e,sender:null,queue:[],closed:!1,reply:w=>le(u,w)};return u},le=(e,u,w)=>{if(e.closed)return!1;const G=typeof u=="string"?u:JSON.stringify(u)??"";return e.sender?e.sender(G,w)!==!1:(e.queue.push({payloadText:G,chunkSize:w}),!0)},ne=e=>{e.closed||(e.closed=!0,e.queue.length=0,e.sender=null,h.expirePendingClarificationsForReply(e.reply))},ue=e=>{const u=e.login,w=e.defaultGroupTitle??"新对话",G=e.lastCount??10,o=e.autoSetGroupTitleOnFirstQuestion??!0,A={beforeKnowledgeRender:e.beforeKnowledgeRender,parsers:e.parsers,actionExecute:{...e.actionExecute,reply:()=>!1}},y=n.ref(""),i=n.ref([]),I=n.ref(""),U=n.ref(e.incPrevQuestion??"Y"),r=n.ref(!1),c=n.ref(!1),t=n.ref(""),l=n.ref(!1),m=n.ref(""),d=n.ref(!0),p=n.ref(!1),C=n.ref(0),W=e.api?null:Z.createRequest({baseURL:e.baseURL??"",getWisession:()=>y.value}),P=e.api??Z.createChatApi(W);let b=null,R=null,D=0,_=0,V=0,E=null;const Y=async()=>{if(m.value="",!W||!u){l.value=!0;return}try{const s=await Z.login(W,u);return y.value=s.wisession||"",l.value=!0,s}catch(s){throw m.value=s instanceof Error?s.message:String(s),s}},a=s=>s===D,g=s=>{typeof URL>"u"||typeof URL.revokeObjectURL!="function"||s.forEach(L=>{L.role!=="user"||!L.attachments||L.attachments.forEach(x=>{var S;const q=(S=x.attachContent)==null?void 0:S.url;q&&q.startsWith("blob:")&&URL.revokeObjectURL(q)})})},O=s=>({beforeKnowledgeRender:e.beforeKnowledgeRender,parsers:e.parsers,actionExecute:{...e.actionExecute,reply:s.reply}}),F=async s=>{const L=++_;V+=1,r.value=!0,g(i.value),E&&h.clearPendingClarifications(E.reply),i.value=[],t.value=s,d.value=!0,p.value=!1,C.value=0;const x=[],q=()=>{const v=[];x.forEach(T=>{v.push(T.user),T.assistant&&v.push(T.assistant)}),L===_&&(i.value=v)},S=v=>{L===_&&(v.type==="users"?(x.length=0,v.users.forEach(T=>x.push({user:T}))):x[v.index]&&(v.user&&(x[v.index].user=v.user),x[v.index].assistant=v.assistant),q())};try{await h.loadGroupMessagesByStep(P,s,G,S,0,A),q(),L===_&&(C.value=x.length,x.length<G&&(d.value=!1))}finally{V-=1,V<=0&&(V=0,r.value=!1)}},z=async()=>{if(!t.value||r.value||!d.value||p.value)return;const s=t.value,L=_,x=C.value;p.value=!0;try{const q=await h.loadGroupMessagesByStep(P,s,G,void 0,x,A),S=q.filter(v=>v.role==="user").length;if(S<G&&(d.value=!1),L===_&&t.value===s){const v=new Set;i.value.forEach(M=>{M.historyId&&v.add(M.historyId),M.id&&v.add(M.id)});const T=q.filter(M=>!(M.historyId&&v.has(M.historyId)||M.id&&v.has(M.id)));i.value=[...T,...i.value],C.value+=S}}finally{p.value=!1}},ce=async(s=w)=>{const L=await h.createHistoryGroup(P,s);return t.value=L,L},ee=()=>{D+=1,_+=1,R&&(R.stopped=!0,R.thinking.expanded=!1),b==null||b.abort(),b=null,E&&ne(E),E=null,R=null,c.value=!1},fe=()=>{R&&(R.error=!0,R.thinking.expanded=!1)},de=async s=>{var ae,re;const L=typeof s=="string"?{text:s}:s||{},{text:x,entryId:q,files:S,attachments:v,extArgs:T,...M}=L,j=(x!==void 0?x:I.value).trim(),ye=T??e.extArgs??{},pe=S&&S.length>0?S:(v??[]).map(H=>{var X;return(X=H.attachContent)==null?void 0:X.raw}).filter(H=>!!H);if(!j||c.value||!t.value)return;const K=D+1;D=K,_+=1;const k=me(K);E=k;const he=O(k);let te=!1;try{c.value=!0;const H=t.value,X=v||(S||[]).map(f=>{var B;const J=f.name.includes(".")&&((B=f.name.split(".").pop())==null?void 0:B.toLowerCase())||"";return{type:"file",name:f.name,description:J?J.toUpperCase():"",attachContent:{file_name:f.name,size:f.size,suffix:J,raw:f}}}),we=C.value===0&&!i.value.some(f=>f.role==="user"),$=H,se=n.reactive({id:h.nextLocalMessageId("user"),role:"user",content:j,attachments:X.length>0?X:void 0});i.value.push(se);const Q=n.reactive({id:h.nextLocalMessageId("answer"),role:"assistant",answer:h.createEmptyAnswerValues("",j),error:!1,stopped:!1,thinking:{steps:[],expanded:!0}});if(R=Q,i.value.push(Q),I.value="",(ae=e.onSubmitQuestion)==null||ae.call(e,j),we&&$){const f=h.buildChatTitle(j);e.onFirstQuestion?e.onFirstQuestion({question:f,groupId:$}):o&&f&&P.setHistoryGroupTitle($,f).catch(()=>{})}b=new AbortController;const Ge=await P.questionStream({...M,question:j,historyGroupId:H,entryId:q,files:pe,ext_args:JSON.stringify(ye),incPrevQuestion:U.value,onOpen:f=>{if(!a(K)||k.closed)return;k.sender=f;const J=k.queue;k.queue=[],J.forEach(({payloadText:B,chunkSize:N})=>f(B,N))}},b.signal);C.value+=1,await h.consumeStream(Ge,f=>{var B;if(!a(K))return;if(f.mode==="history_id"){const N=typeof f.value=="object"&&f.value!==null?f.value.history_id:f.value;N&&(se.historyId=String(N),Q.historyId=String(N),Q.answer&&(Q.answer.id=String(N)))}h.applyAnswerChunk(Q.answer,f,he).includes("deep_thinking")&&(Q.thinking.steps=h.collectThinkingSteps(Q.answer)),f.mode==="finish"&&(Q.thinking.expanded=!1),(B=e.onChunk)==null||B.call(e,f)}),a(K)&&(Q.thinking.expanded=!1),a(K)&&(te=!0)}catch(H){a(K)&&!Z.isCancelError(H)&&fe()}finally{ne(k),E===k&&(E=null),a(K)&&(c.value=!1,b=null,R=null)}te&&((re=e.onFinish)==null||re.call(e))},ge=(s,L)=>!c.value||!E?!1:le(E,s,L),ve=e.autoLogin!==!1?Y().then(()=>{}).catch(()=>{}):Promise.resolve();return n.getCurrentInstance()&&n.onBeforeUnmount(()=>{ee(),g(i.value)}),{messages:i,question:I,incPrevQuestion:U,talking:c,loading:r,session:y,autoLoginDone:l,autoLoginError:m,loginDone:ve,historyGroupId:t,selfRequest:W,api:P,doAutoLogin:Y,loadGroup:F,loadOlder:z,hasMore:d,loadMoreLoading:p,createGroup:ce,submitQuestion:de,sendWsMessage:ge,stopQuestion:ee}},Le="wi-chat-history-groups",oe=e=>{var Y;const{api:u,defaultGroupTitle:w="新对话",lastCount:G=10,ensureGroup:o=!0,autoSelectFirst:A=!0,autoLoadMessages:y=!0}=e,i=e.persistGroups??!1,I=e.storageKey??Le,U=e.beforeKnowledgeRender||(Y=e.parsers)!=null&&Y.length?{beforeKnowledgeRender:e.beforeKnowledgeRender,parsers:e.parsers}:void 0,r=n.ref([]),c=n.ref(""),t=n.ref([]),l=n.ref(!1);let m=!1,d=null;const p=()=>{if(d===!1)return null;let a;try{a=localStorage.getItem(I)}catch{return d=!1,null}if(a===null)return[];try{const g=JSON.parse(a);return Array.isArray(g)?g:[]}catch{return[]}},C=a=>{if(d!==!1)try{localStorage.setItem(I,JSON.stringify(a))}catch{d=!1}},W=a=>{const g=p();g!==null&&C([...g,a])},P=async(a,g=G)=>{l.value=!0;try{t.value=a?await h.loadGroupMessages(u,a,g,0,U):[]}finally{l.value=!1}},b=async a=>{c.value=a,y&&await P(a)},R=async()=>{if(!m){m=!0;try{r.value.length===0?o&&await _(w):!c.value&&A&&await b(r.value[0].group_id)}finally{m=!1}}},D=async()=>{l.value=!0;try{const a=await h.getHistoryGroups(u);if(i){const g=p();if(g===null)r.value=a;else{const O=new Set(g.map(z=>z.group_id)),F=a.filter(z=>O.has(z.group_id));C(F),r.value=F}}else r.value=a;await R()}finally{l.value=!1}},_=async(a=w)=>{const g=await h.createHistoryGroup(u,a);return i&&W({group_id:g,group_title:a}),await D(),c.value!==g&&await b(g),g};return{groups:r,activeGroupId:c,messages:t,loading:l,loadGroups:D,createGroup:_,renameGroup:async(a,g)=>{await h.setHistoryGroupTitle(u,a,g);const O=r.value.find(F=>F.group_id===a);O&&(O.group_title=g),i&&C(r.value)},deleteGroup:async a=>{const g=r.value.findIndex(F=>F.group_id===a);if(await h.deleteHistoryGroup(u,a),r.value=r.value.filter(F=>F.group_id!==a),i&&C(r.value),c.value!==a)return;const O=r.value[g]??r.value[r.value.length-1];if(O){await b(O.group_id);return}o?await _(w):(c.value="",t.value=[])},selectGroup:b,loadMessages:P}},ie=e=>{const{api:u,defaultEntryId:w,entryGroup:G="智能助手",autoLoad:o=!0}=e,A=n.ref([]),y=n.ref(null),i=n.ref(!1),I=c=>c?typeof u.getFileUrl=="function"?u.getFileUrl(c):h.resolveRemoteFileUrl(c,"","/algorithm/get_file_data"):"",U=c=>{y.value=c},r=async()=>{var c;i.value=!0;try{let t=w||((c=y.value)==null?void 0:c.entry_id);if(!t)try{const p=await u.getDefaultLlmId();t=p==null?void 0:p.default_entry_id}catch(p){console.warn("[useAgent] 获取默认智能体 ID 失败:",p)}const l=await u.getLlmEntryListByRole({entry_group:G}),d=((l==null?void 0:l.values)||[]).filter(p=>p.entry_enable!==!1);if(A.value=d,t){const p=d.find(C=>C.entry_id===t);p?y.value=p:d.length>0&&(y.value=d[0])}else d.length>0&&!y.value&&(y.value=d[0])}finally{i.value=!1}};return o&&(n.getCurrentInstance()?n.onMounted(()=>{r()}):r()),{agentList:A,currentAgent:y,loading:i,getFileUrl:I,loadAgents:r,selectAgent:U}},Ae=(e={})=>{const u=e.parsers??[];let w=null,G;const o=ue({...e.chat,parsers:u,onFirstQuestion:({question:t,groupId:l})=>{G==null||G(l,t)}}),A=oe({...e.history,api:o.api,autoLoadMessages:!1,parsers:u});G=(t,l)=>{w=A.renameGroup(t,l).catch(m=>{var d;(d=e.onError)==null||d.call(e,m,"groups")})};const y=ie({...e.agent,api:o.api,autoLoad:!1}),i=(t,l)=>{var m;(m=e.onError)==null||m.call(e,t,l)},I=async()=>{try{await A.loadGroups()}catch(t){i(t,"groups")}},U=async t=>{try{await o.loadGroup(t)}catch(l){i(l,"messages")}},r=async()=>{try{await y.loadAgents()}catch(t){i(t,"agents")}},c=async t=>{var d;const l=typeof t=="string"?{text:t}:t;await o.submitQuestion({...l,entryId:(l==null?void 0:l.entryId)??((d=y.currentAgent.value)==null?void 0:d.entry_id)});const m=w;w=null,m&&await m,await I()};return n.watch(o.session,t=>{t&&(I(),r())}),n.watch(A.activeGroupId,t=>{t&&U(t)}),{chat:o,history:A,agent:y,parsers:u,messages:o.messages,question:o.question,incPrevQuestion:o.incPrevQuestion,talking:o.talking,loading:o.loading,session:o.session,groups:A.groups,activeGroupId:A.activeGroupId,historyLoading:A.loading,agentList:y.agentList,currentAgent:y.currentAgent,agentLoading:y.loading,hasMore:o.hasMore,loadMoreLoading:o.loadMoreLoading,loadGroup:U,loadOlder:o.loadOlder,refreshGroups:I,loadAgents:r,selectAgent:y.selectAgent,getFileUrl:y.getFileUrl,submitQuestion:c,sendWsMessage:o.sendWsMessage,stopQuestion:o.stopQuestion}};exports.useAgent=ie;exports.useChat=ue;exports.useChatSession=Ae;exports.useHistory=oe;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("vue"),C=require("wi-chat/parser"),H=require("wi-chat/request"),ce=e=>{const{api:n,interval:h=5*60*1e3,renew:d,onExpired:l,immediate:w=!0}=e,c=f.ref(!1),o=f.ref(0);let L=null,x=null;const m=async()=>{const v=d?await d().catch(()=>!1):!1;return v||l==null||l(),v},i=()=>x||(x=(async()=>{if(!n.pingLogin)return!0;try{const v=await n.pingLogin();return o.value=Date.now(),(v==null?void 0:v.is_login)===!1?m():!0}catch(v){return v instanceof H.AuthExpiredError?m():!0}finally{x=null}})(),x),t=()=>{L||(c.value=!0,L=setInterval(()=>void i(),h))},s=()=>{L&&clearInterval(L),L=null,c.value=!1};return w&&t(),f.getCurrentInstance()&&f.onBeforeUnmount(s),{active:c,lastPingAt:o,start:t,stop:s,pingNow:i}},k={get(e){try{return typeof localStorage<"u"?localStorage.getItem(e):null}catch{return null}},set(e,n){try{typeof localStorage<"u"&&localStorage.setItem(e,n)}catch{}},remove(e){try{typeof localStorage<"u"&&localStorage.removeItem(e)}catch{}}},Re=e=>btoa(unescape(encodeURIComponent(e))),Ge=e=>decodeURIComponent(escape(atob(e))),Z=e=>{let n=null;return()=>(n||(n=Promise.resolve().then(e).catch(()=>!1).finally(()=>{setTimeout(()=>n=null,0)})),n)},fe=e=>({get(){const n=k.get(e);if(!n)return null;try{const h=JSON.parse(Ge(n));return h!=null&&h.user&&(h!=null&&h.pass)?h:null}catch{return null}},set(n){k.set(e,Re(JSON.stringify(n)))},clear(){k.remove(e)}}),Ee=e=>{const n=e.sessionKey===void 0?"wi-chat-session":e.sessionKey,h=e.remember??!1;let d=null;const l=e.credentialStore??(h&&n?fe(`${n}-account`):{get:()=>d,set:_=>{d=_},clear:()=>{d=null}}),w=n?`${n}-user`:null,c=f.ref(n?k.get(n)??"":""),o=f.ref(null);if(w)try{o.value=JSON.parse(k.get(w)||"null")}catch{o.value=null}const L=f.computed(()=>!!c.value),x=_=>{c.value=_.wisession||"",o.value=_,n&&k.set(n,c.value),w&&k.set(w,JSON.stringify(_))},m=()=>{c.value="",o.value=null,n&&k.remove(n),w&&k.remove(w)},i=H.createRequest({baseURL:e.baseURL});let t=0;const s=Z(async()=>{const _=t;try{const T=await l.get();if(!T)return!1;const O=await H.login(i,{...T,client:e.client});return _!==t?!1:(x(O),!0)}catch{return!1}}),v=H.createRequest({baseURL:e.baseURL,getWisession:()=>c.value,onAuthExpired:async()=>{const _=await s();return _||g(),_}}),a=H.createChatApi(v,{resumable:e.resumable??!0,refreshSession:async()=>{const _=await s();return _||g(),_}}),u=ce({api:a,interval:e.pingInterval||5*60*1e3,renew:async()=>c.value?s():!1,onExpired:()=>g(),immediate:!1}),g=()=>{var _;c.value&&(m(),u.stop(),(_=e.onExpired)==null||_.call(e))},R=()=>{e.pingInterval!==0&&u.start()},I=async(_,T,O={})=>{const W={...O,user:_,pass:T,client:e.client},K=await H.login(i,W);return x(K),await l.set({user:_,pass:T}),R(),K},M=async()=>{const _=await s();return _&&R(),_},Q=async(_=!0)=>{t+=1,m(),u.stop(),_&&await l.clear()};return c.value&&(R(),u.pingNow()),{session:c,userInfo:o,isLoggedIn:L,request:v,api:a,keepAlive:u,login:I,autoLogin:M,renew:s,logout:Q}},Ie=e=>{const n={requestId:e,sender:null,queue:[],closed:!1,reply:h=>de(n,h)};return n},de=(e,n,h)=>{if(e.closed)return!1;const d=typeof n=="string"?n:JSON.stringify(n)??"";return e.sender?e.sender(d,h)!==!1:(e.queue.push({payloadText:d,chunkSize:h}),!0)},ie=e=>{e.closed||(e.closed=!0,e.queue.length=0,e.sender=null,C.expirePendingClarificationsForReply(e.reply))},ee=e=>{const n=e.login,h=e.defaultGroupTitle??"新对话",d=e.lastCount??10,l=e.autoSetGroupTitleOnFirstQuestion??!0,w={beforeKnowledgeRender:e.beforeKnowledgeRender,parsers:e.parsers,actionExecute:{...e.actionExecute,reply:()=>!1}},c=f.ref(""),o=f.ref([]),L=f.ref(""),x=f.ref(e.incPrevQuestion??"Y"),m=f.ref(!1),i=f.ref(!1),t=f.ref(""),s=f.ref(!1),v=f.ref(""),a=f.ref(!0),u=f.ref(!1),g=f.ref(0),R=e.api?null:H.createRequest({baseURL:e.baseURL??"",getWisession:()=>c.value,onAuthExpired:()=>Q()}),I=e.api??H.createChatApi(R,{resumable:e.resumable,refreshSession:()=>Q()}),M=R?H.createRequest({baseURL:e.baseURL??""}):null,Q=Z(async()=>{if(e.onAuthExpired)return e.onAuthExpired();if(!R||!n||!M)return!1;const r=await H.login(M,n);return c.value=r.wisession||"",!!c.value}),_=(r,b)=>{var p;r&&((p=e.onRunningChange)==null||p.call(e,r,b))};let T=null,O=null,W=0,K=0,S=0,A=null;const N=async()=>{if(v.value="",!R||!n){s.value=!0;return}try{const r=await H.login(M??R,n);return c.value=r.wisession||"",s.value=!0,r}catch(r){throw v.value=r instanceof Error?r.message:String(r),r}},P=r=>r===W,V=r=>{typeof URL>"u"||typeof URL.revokeObjectURL!="function"||r.forEach(b=>{b.role!=="user"||!b.attachments||b.attachments.forEach(p=>{var G;const q=(G=p.attachContent)==null?void 0:G.url;q&&q.startsWith("blob:")&&URL.revokeObjectURL(q)})})},he=r=>({beforeKnowledgeRender:e.beforeKnowledgeRender,parsers:e.parsers,actionExecute:{...e.actionExecute,reply:r.reply}}),we=async r=>{const b=++K;S+=1,m.value=!0,V(o.value),A&&C.clearPendingClarifications(A.reply),o.value=[],t.value=r,a.value=!0,u.value=!1,g.value=0;const p=[],q=()=>{const y=[];p.forEach(F=>{y.push(F.user),F.assistant&&y.push(F.assistant)}),b===K&&(o.value=y)},G=y=>{b===K&&(y.type==="users"?(p.length=0,y.users.forEach(F=>p.push({user:F}))):p[y.index]&&(y.user&&(p[y.index].user=y.user),p[y.index].assistant=y.assistant),q())};try{await C.loadGroupMessagesByStep(I,r,d,G,0,w),q(),b===K&&(g.value=p.length,p.length<d&&(a.value=!1))}finally{S-=1,S<=0&&(S=0,m.value=!1)}},pe=async()=>{if(!t.value||m.value||!a.value||u.value)return;const r=t.value,b=K,p=g.value;u.value=!0;try{const q=await C.loadGroupMessagesByStep(I,r,d,void 0,p,w),G=q.filter(y=>y.role==="user").length;if(G<d&&(a.value=!1),b===K&&t.value===r){const y=new Set;o.value.forEach(E=>{E.historyId&&y.add(E.historyId),E.id&&y.add(E.id)});const F=q.filter(E=>!(E.historyId&&y.has(E.historyId)||E.id&&y.has(E.id)));o.value=[...F,...o.value],g.value+=G}}finally{u.value=!1}},me=async(r=h)=>{const b=await C.createHistoryGroup(I,r);return t.value=b,b},te=(r=!0)=>{var b;r!==!1&&i.value&&t.value&&((b=I.stopChat)==null||b.call(I,t.value)),W+=1,K+=1,O&&(O.stopped=!0,O.thinking.expanded=!1,Y(O)),T==null||T.abort(),T=null,A&&ie(A),A=null,O=null,i.value=!1},Se=()=>{O&&(O.error=!0,O.thinking.expanded=!1,Y(O))},re=async(r,b,p,q)=>{const G=W+1;W=G,K+=1;const y=Ie(G);A=y;const F=he(y),E=q||t.value;O=r,i.value=!0,_(E,!0);let D=!1;try{T=new AbortController;const z=await p(B=>{if(!P(G)||y.closed)return;y.sender=B;const J=y.queue;y.queue=[],J.forEach(({payloadText:$,chunkSize:X})=>B($,X))},T.signal);await C.consumeStream(z,B=>{var J;P(G)&&(Ae(r,b,B,F),(J=e.onChunk)==null||J.call(e,B))}),P(G)&&(D=!0,r.thinking.expanded=!1,Y(r))}catch(z){P(G)&&!H.isCancelError(z)&&Se()}finally{ie(y),A===y&&(A=null),_(E,!1),P(G)&&(i.value=!1,T=null,O=null)}return D&&_e(r),D},Ae=(r,b,p,q)=>{if(p.mode==="history_id"){const y=typeof p.value=="object"&&p.value!==null?p.value.history_id:p.value;y&&(b&&(b.historyId=String(y)),r.historyId=String(y),r.answer.id=String(y));return}if(p.mode==="start"){const y=C.parseServerStartTime(p.value??p.time,r.runStartAt);y!=null&&r.runStartAt!=null&&(r.runStartAt=y);return}if(p.mode==="history_answer"){const y=C.normalizeAnswer(p.value,r.historyId??"",r.answer.question??"",w);r.answer=y,r.thinking.steps=C.collectThinkingSteps(y);return}C.applyAnswerChunk(r.answer,p,q).includes("deep_thinking")&&(r.thinking.steps=C.collectThinkingSteps(r.answer)),p.mode==="finish"&&(r.thinking.expanded=!1)},Y=r=>{!r||r.runStartAt==null||(r.runMs=Math.max(0,Date.now()-r.runStartAt),delete r.runStartAt)},_e=async r=>{if(!(!e.syncMetaOnFinish||!r.historyId))try{const b=await I.getHistoryAnswer(r.historyId);C.mergeAnswerMeta(r.answer,b);const p=C.readAnswerMeta(r.answer).runMs;p!=null&&(r.runMs=p)}catch{}},be=async r=>{var se,le;const b=typeof r=="string"?{text:r}:r||{},{text:p,entryId:q,files:G,attachments:y,extArgs:F,...E}=b,D=(p!==void 0?p:L.value).trim(),z=F??e.extArgs??{},B=G&&G.length>0?G:(y??[]).map(U=>{var j;return(j=U.attachContent)==null?void 0:j.raw}).filter(U=>!!U);if(!D||i.value||!t.value)return;const J=t.value,$=y||(G||[]).map(U=>{var oe;const j=U.name.includes(".")&&((oe=U.name.split(".").pop())==null?void 0:oe.toLowerCase())||"";return{type:"file",name:U.name,description:j?j.toUpperCase():"",attachContent:{file_name:U.name,size:U.size,suffix:j,raw:U}}}),X=g.value===0&&!o.value.some(U=>U.role==="user"),ne=f.reactive({id:C.nextLocalMessageId("user"),role:"user",content:D,attachments:$.length>0?$:void 0});o.value.push(ne);const ae=f.reactive({id:C.nextLocalMessageId("answer"),role:"assistant",answer:C.createEmptyAnswerValues("",D),error:!1,stopped:!1,runStartAt:Date.now(),thinking:{steps:[],expanded:!0}});if(o.value.push(ae),L.value="",(se=e.onSubmitQuestion)==null||se.call(e,D),X){const U=C.buildChatTitle(D);e.onFirstQuestion?e.onFirstQuestion({question:U,groupId:J}):l&&U&&I.setHistoryGroupTitle(J,U).catch(()=>{})}g.value+=1,await re(ae,ne,(U,j)=>I.questionStream({...E,question:D,historyGroupId:J,entryId:q,files:B,ext_args:JSON.stringify(z),incPrevQuestion:x.value,onOpen:U},j))&&((le=e.onFinish)==null||le.call(e))},Le=async r=>{var F;if(i.value||!(r!=null&&r.history_id)||t.value&&r.history_group_id&&r.history_group_id!==t.value)return!1;!t.value&&r.history_group_id&&(t.value=r.history_group_id);const b=o.value.find(E=>E.role==="user"&&E.historyId===r.history_id);o.value=o.value.filter(E=>!(E.role==="assistant"&&E.historyId===r.history_id));let p=b??null;p||(p=f.reactive({id:r.history_id,historyId:r.history_id,role:"user",content:r.question??""}),o.value.push(p),g.value+=1);const q=f.reactive({id:`${r.history_id}-answer`,historyId:r.history_id,role:"assistant",answer:C.createEmptyAnswerValues(r.history_id,r.question??""),error:!1,stopped:!1,runStartAt:Date.now(),thinking:{steps:[],expanded:!0}}),G=o.value.indexOf(p);o.value.splice(G+1,0,q);const y=await re(q,p,(E,D)=>I.questionStream({question:r.question??"",historyGroupId:r.history_group_id||t.value,resumeTask:r,onOpen:E},D),r.history_group_id||t.value);return y&&((F=e.onFinish)==null||F.call(e)),y},Ce=(r,b)=>!i.value||!A?!1:de(A,r,b),xe=e.autoLogin!==!1?N().then(()=>{}).catch(()=>{}):Promise.resolve();return f.getCurrentScope()&&f.onScopeDispose(()=>{te(!1),V(o.value)}),{messages:o,question:L,incPrevQuestion:x,talking:i,loading:m,session:c,autoLoginDone:s,autoLoginError:v,loginDone:xe,historyGroupId:t,selfRequest:R,api:I,doAutoLogin:N,loadGroup:we,loadOlder:pe,hasMore:a,loadMoreLoading:u,createGroup:me,submitQuestion:be,sendWsMessage:Ce,stopQuestion:te,resumeTask:Le,renewSession:Q}},Me="wi-chat-history-groups",ve=e=>{var K;const{api:n,defaultGroupTitle:h="新对话",lastCount:d=10,ensureGroup:l=!0,autoSelectFirst:w=!0,autoLoadMessages:c=!0}=e,o=e.persistGroups??!1,L=e.storageKey??Me,x=e.beforeKnowledgeRender||(K=e.parsers)!=null&&K.length?{beforeKnowledgeRender:e.beforeKnowledgeRender,parsers:e.parsers}:void 0,m=f.ref([]),i=f.ref(""),t=f.ref([]),s=f.ref(!1);let v=!1,a=null;const u=()=>{if(a===!1)return null;let S;try{S=localStorage.getItem(L)}catch{return a=!1,null}if(S===null)return[];try{const A=JSON.parse(S);return Array.isArray(A)?A:[]}catch{return[]}},g=S=>{if(a!==!1)try{localStorage.setItem(L,JSON.stringify(S))}catch{a=!1}},R=S=>{const A=u();A!==null&&g([...A,S])},I=async(S,A=d)=>{s.value=!0;try{t.value=S?await C.loadGroupMessages(n,S,A,0,x):[]}finally{s.value=!1}},M=async S=>{i.value=S,c&&await I(S)},Q=async()=>{if(!v){v=!0;try{m.value.length===0?l&&await T(h):!i.value&&w&&await M(m.value[0].group_id)}finally{v=!1}}},_=async()=>{s.value=!0;try{const S=await C.getHistoryGroups(n);if(o){const A=u();if(A===null)m.value=S;else{const N=new Set(A.map(V=>V.group_id)),P=S.filter(V=>N.has(V.group_id));g(P),m.value=P}}else m.value=S;await Q()}finally{s.value=!1}},T=async(S=h)=>{const A=await C.createHistoryGroup(n,S);return o&&R({group_id:A,group_title:S}),await _(),i.value!==A&&await M(A),A};return{groups:m,activeGroupId:i,messages:t,loading:s,loadGroups:_,createGroup:T,renameGroup:async(S,A)=>{await C.setHistoryGroupTitle(n,S,A);const N=m.value.find(P=>P.group_id===S);N&&(N.group_title=A),o&&g(m.value)},deleteGroup:async S=>{const A=m.value.findIndex(P=>P.group_id===S);if(await C.deleteHistoryGroup(n,S),m.value=m.value.filter(P=>P.group_id!==S),o&&g(m.value),i.value!==S)return;const N=m.value[A]??m.value[m.value.length-1];if(N){await M(N.group_id);return}l?await T(h):(i.value="",t.value=[])},selectGroup:M,loadMessages:I}},ye=e=>{const{api:n,defaultEntryId:h,entryGroup:d="智能助手",autoLoad:l=!0}=e,w=f.ref([]),c=f.ref(null),o=f.ref(!1),L=i=>i?typeof n.getFileUrl=="function"?n.getFileUrl(i):C.resolveRemoteFileUrl(i,"","/algorithm/get_file_data"):"",x=i=>{c.value=i},m=async()=>{var i;o.value=!0;try{let t=h||((i=c.value)==null?void 0:i.entry_id);if(!t)try{const u=await n.getDefaultLlmId();t=u==null?void 0:u.default_entry_id}catch(u){console.warn("[useAgent] 获取默认智能体 ID 失败:",u)}const s=await n.getLlmEntryListByRole({entry_group:d}),a=((s==null?void 0:s.values)||[]).filter(u=>u.entry_enable!==!1);if(w.value=a,t){const u=a.find(g=>g.entry_id===t);u?c.value=u:a.length>0&&(c.value=a[0])}else a.length>0&&!c.value&&(c.value=a[0])}finally{o.value=!1}};return l&&(f.getCurrentInstance()?f.onMounted(()=>{m()}):m()),{agentList:w,currentAgent:c,loading:o,getFileUrl:L,loadAgents:m,selectAgent:x}},ge=e=>{const{api:n,interval:h=5e3,immediate:d=!1}=e,l=f.ref([]),w=f.ref(new Set);let c=null,o=null;const L=f.computed(()=>{const a=new Set(w.value);return l.value.forEach(u=>u.history_group&&a.add(String(u.history_group))),a}),x=a=>!!a&&L.value.has(a),m=(a,u)=>{if(!a)return;const g=new Set(w.value);if(u?g.add(a):g.delete(a),w.value=g,!u){const R=o;(async()=>(R&&await R,await t()))()}},i=a=>l.value.find(u=>String(u.history_group)===a&&u.is_connected!==!0&&!w.value.has(a)),t=()=>o||(o=(async()=>{if(!n.getChatTaskList)return l.value;try{const a=(await n.getChatTaskList()).filter(u=>u&&u.is_stop!==!0);if(l.value=a,e.onUnknownGroups&&e.isKnownGroup){const u=a.map(g=>String(g.history_group)).filter(g=>g&&!e.isKnownGroup(g));u.length&&e.onUnknownGroups(u)}}catch{}finally{o=null}return l.value})(),o),s=()=>{c||!h||(t(),c=setInterval(()=>void t(),h))},v=()=>{c&&clearInterval(c),c=null,l.value=[]};return d&&s(),f.getCurrentInstance()&&f.onBeforeUnmount(v),{tasks:l,runningIds:L,isRunning:x,setLocalRunning:m,findResumableTask:i,refresh:t,start:s,stop:v}},qe=e=>{const{api:n,maxRooms:h=10}=e,d=f.shallowReactive(new Map),l=f.ref(""),w=ge({api:n,interval:e.taskPollInterval??5e3,onUnknownGroups:e.onUnknownGroups,isKnownGroup:e.isKnownGroup}),c=t=>{if(d.size<=h)return;const s=[...d.entries()].filter(([v,a])=>v!==l.value&&v!==t&&!a.chat.talking.value).sort((v,a)=>v[1].touchedAt-a[1].touchedAt);for(;d.size>h&&s.length;){const[v]=s.shift();x(v)}},o=t=>{const s=d.get(t);if(s)return s.touchedAt=Date.now(),s.chat;const v=f.effectScope(!0),a=v.run(()=>{var g;return ee({...(g=e.roomOptions)==null?void 0:g.call(e,t),api:n,autoLogin:!1,parsers:e.parsers,beforeKnowledgeRender:e.beforeKnowledgeRender,actionExecute:e.actionExecute,lastCount:e.lastCount,syncMetaOnFinish:!0,onRunningChange:w.setLocalRunning})}),u={chat:a,scope:v,loaded:a.loadGroup(t).catch(()=>{}),touchedAt:Date.now()};return d.set(t,u),c(t),a},L=async t=>{var a;if(l.value=t,!t)return;const s=o(t);if(await((a=d.get(t))==null?void 0:a.loaded),s.talking.value)return;await w.refresh();const v=w.findResumableTask(t);v&&l.value===t&&s.resumeTask({history_id:v.history_id,history_group_id:String(v.history_group),question:v.question})},x=t=>{const s=d.get(t);s&&(s.chat.stopQuestion(!1),s.scope.stop(),d.delete(t),l.value===t&&(l.value=""))},m=()=>{[...d.keys()].forEach(x),w.stop()},i=f.computed(()=>{var t;return l.value?((t=d.get(l.value))==null?void 0:t.chat)??null:null});return f.getCurrentScope()&&f.onScopeDispose(m),{activeId:l,active:i,running:w,getRoom:o,open:L,dispose:x,disposeAll:m}},Ue=e=>{if(!e||typeof localStorage>"u")return{};try{const n=JSON.parse(localStorage.getItem(e)||"{}");return n&&typeof n=="object"?n:{}}catch{return{}}},Te=(e,n)=>{if(!(!e||typeof localStorage>"u"))try{localStorage.setItem(e,JSON.stringify(n))}catch{}},Oe=e=>{const n=e.storageKey===void 0?"wi-chat-room-llm":e.storageKey,h=f.ref([]),d=f.ref(!1),l=f.ref(Ue(n)),w=f.computed(()=>new Set(h.value.flatMap(a=>(a.children??[]).map(u=>u.id)))),c=a=>{l.value=a,Te(n,a)};let o=null;const L=()=>d.value?Promise.resolve():o||(o=(async()=>{var a,u;try{const g=await((u=(a=e.api).getLlmInfoList)==null?void 0:u.call(a));if(!g)return;const R=g.connections??[];h.value=(g.factorys??[]).map(M=>({id:M.factory_id,name:M.factory_title,type:"factory",children:R.filter(Q=>Q.factory_id===M.factory_id).map(Q=>({id:Q.connect_id,name:Q.connect_title,type:"connection"}))})).filter(M=>M.children.length>0),d.value=!0;const I=Object.entries(l.value).filter(([,M])=>w.value.has(M));I.length!==Object.keys(l.value).length&&c(Object.fromEntries(I))}catch{}finally{o=null}})(),o),x=a=>!!a&&(!d.value||w.value.has(a));return{llmTree:h,loaded:d,roomLlmMap:l,validIds:w,load:L,resolve:(a,u)=>{const g=a?l.value[a]:"";return g&&x(g)?g:u||""},select:(a,u,g)=>{if(!a)return;const R={...l.value};u&&u!==g?R[a]=u:delete R[a],c(R)},migrate:(a,u)=>{const g=l.value[a];if(!g||a===u)return;const R={...l.value,[u]:g};delete R[a],c(R)},nameOf:a=>{var u;if(!a)return"";for(const g of h.value){const R=(u=g.children)==null?void 0:u.find(I=>I.id===a);if(R)return R.name}return""},isUnavailable:a=>!!a&&d.value&&!w.value.has(a)}},ue=async e=>{var d;if(typeof navigator<"u"&&((d=navigator.clipboard)!=null&&d.writeText))try{await navigator.clipboard.writeText(e);return}catch{}if(typeof document>"u")throw new Error("当前环境不支持剪贴板");const n=document.createElement("textarea");n.value=e,n.style.position="fixed",n.style.opacity="0",document.body.appendChild(n),n.select();const h=document.execCommand("copy");if(document.body.removeChild(n),!h)throw new Error("复制失败")},Ke=e=>{const{api:n,buildShareUrl:h,shareDays:d=1}=e,l=i=>!!C.collectAnswerMarkdown(i.answer),w=async(i,t=!1)=>{const s=C.collectAnswerMarkdown(i.answer);if(!s)return!1;try{return await ue(t?s:C.markdownToPlainText(s)),!0}catch{return!1}},c=i=>C.readAnswerMeta(i.answer).state,o=new WeakSet;return{canCopy:l,copy:w,stateOf:c,toggleState:async(i,t)=>{if(!(!i.historyId||!n.setHistoryAnswerState||o.has(i))){o.add(i);try{const s=c(i)===t?null:t;await n.setHistoryAnswerState(i.historyId,s),i.answer.answer||(i.answer.answer={reports:[]}),i.answer.answer.state=s}finally{o.delete(i)}}},feedback:async(i,t)=>{!i.historyId||!t.trim()||!n.reportHistoryProblem||await n.reportHistoryProblem(i.historyId,t.trim())},share:async i=>{if(!i.historyId||!n.shareChatHistory)throw new Error("当前回答暂不支持分享");const t=await n.shareChatHistory([i.historyId],d),s=h(t);return await ue(s),s}}},Pe=(e={})=>{const n=e.parsers??[];let h=null,d;const l=ee({...e.chat,parsers:n,onFirstQuestion:({question:t,groupId:s})=>{d==null||d(s,t)}}),w=ve({...e.history,api:l.api,autoLoadMessages:!1,parsers:n});d=(t,s)=>{h=w.renameGroup(t,s).catch(v=>{var a;(a=e.onError)==null||a.call(e,v,"groups")})};const c=ye({...e.agent,api:l.api,autoLoad:!1}),o=(t,s)=>{var v;(v=e.onError)==null||v.call(e,t,s)},L=async()=>{try{await w.loadGroups()}catch(t){o(t,"groups")}},x=async t=>{try{await l.loadGroup(t)}catch(s){o(s,"messages")}},m=async()=>{try{await c.loadAgents()}catch(t){o(t,"agents")}},i=async t=>{var a;const s=typeof t=="string"?{text:t}:t;await l.submitQuestion({...s,entryId:(s==null?void 0:s.entryId)??((a=c.currentAgent.value)==null?void 0:a.entry_id)});const v=h;h=null,v&&await v,await L()};return f.watch(l.session,t=>{t&&(L(),m())}),f.watch(w.activeGroupId,t=>{t&&x(t)}),{chat:l,history:w,agent:c,parsers:n,messages:l.messages,question:l.question,incPrevQuestion:l.incPrevQuestion,talking:l.talking,loading:l.loading,session:l.session,groups:w.groups,activeGroupId:w.activeGroupId,historyLoading:w.loading,agentList:c.agentList,currentAgent:c.currentAgent,agentLoading:c.loading,hasMore:l.hasMore,loadMoreLoading:l.loadMoreLoading,loadGroup:x,loadOlder:l.loadOlder,refreshGroups:L,loadAgents:m,selectAgent:c.selectAgent,getFileUrl:c.getFileUrl,submitQuestion:i,sendWsMessage:l.sendWsMessage,stopQuestion:l.stopQuestion}};exports.createLocalCredentialStore=fe;exports.createRenewer=Z;exports.useAgent=ye;exports.useAnswerActions=Ke;exports.useChat=ee;exports.useChatAuth=Ee;exports.useChatRooms=qe;exports.useChatSession=Pe;exports.useHistory=ve;exports.useKeepAlive=ce;exports.useLlm=Oe;exports.useRunningGroups=ge;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/core/index.d.ts
CHANGED
|
@@ -9,5 +9,17 @@ export { useHistory } from './useHistory';
|
|
|
9
9
|
export type { UseHistoryOptions, UseHistoryReturn } from './useHistory';
|
|
10
10
|
export { useAgent } from './useAgent';
|
|
11
11
|
export type { UseAgentOptions, UseAgentReturn } from './useAgent';
|
|
12
|
+
export { useKeepAlive } from './useKeepAlive';
|
|
13
|
+
export type { UseKeepAliveOptions, UseKeepAliveReturn } from './useKeepAlive';
|
|
14
|
+
export { useRunningGroups } from './useRunningGroups';
|
|
15
|
+
export type { UseRunningGroupsOptions, UseRunningGroupsReturn } from './useRunningGroups';
|
|
16
|
+
export { useChatAuth, createLocalCredentialStore, createRenewer } from './useChatAuth';
|
|
17
|
+
export type { UseChatAuthOptions, UseChatAuthReturn, CredentialStore, RememberedCredential } from './useChatAuth';
|
|
18
|
+
export { useChatRooms } from './useChatRooms';
|
|
19
|
+
export type { UseChatRoomsOptions, UseChatRoomsReturn } from './useChatRooms';
|
|
20
|
+
export { useLlm } from './useLlm';
|
|
21
|
+
export type { LlmTreeNode, UseLlmOptions, UseLlmReturn } from './useLlm';
|
|
22
|
+
export { useAnswerActions } from './useAnswerActions';
|
|
23
|
+
export type { UseAnswerActionsOptions, UseAnswerActionsReturn } from './useAnswerActions';
|
|
12
24
|
export { useChatSession } from './useChatSession';
|
|
13
25
|
export type { UseChatSessionAgentOptions, UseChatSessionChatOptions, UseChatSessionErrorScope, UseChatSessionHistoryOptions, UseChatSessionOptions, UseChatSessionReturn, } from './useChatSession';
|