@topthink/chat 1.2.8 → 1.2.9
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/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as n,css as i,formatSize as o,Tooltip as s,useAsync as a,isCancel as l,isRequestError as c,Form as d,Toast as u,InfiniteScroller as m,useDebounce as h,Offcanvas as p,ModalForm as g,RequestButton as f,Loader as b,useImmer as v,getAbsoluteUrl as y,Lightbox as x,Clipboard as w,Button as k,isImageUrl as E,request as N}from"@topthink/components";import*as j from"react";import{createContext as C,useContext as $,useEffect as S,useState as M,useRef as F,useCallback as R,forwardRef as q,useMemo as z,useImperativeHandle as
|
|
1
|
+
import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as n,css as i,formatSize as o,Tooltip as s,useAsync as a,isCancel as l,isRequestError as c,Form as d,Toast as u,InfiniteScroller as m,useDebounce as h,Offcanvas as p,ModalForm as g,RequestButton as f,Loader as b,useImmer as v,getAbsoluteUrl as y,Lightbox as x,Clipboard as w,Button as k,isImageUrl as E,request as N}from"@topthink/components";import*as j from"react";import{createContext as C,useContext as $,useEffect as S,useState as M,useRef as F,useCallback as R,forwardRef as q,useMemo as z,useImperativeHandle as A,Fragment as I,memo as D,Component as L}from"react";import*as P from"path";import T from"path";import{Spinner as O,Badge as B,Dropdown as U,FormControl as _,CloseButton as H,OverlayTrigger as Q,Popover as J}from"react-bootstrap";import K from"eventemitter3";import W from"react-textarea-autosize";import X from"lodash/isEqual";import{toString as V}from"hast-util-to-string";import Y from"react-markdown";import"katex/dist/katex.min.css";import G from"rehype-highlight";import Z from"rehype-katex";import ee from"remark-breaks";import te from"remark-gfm";import re from"remark-math";import{codes as ne,types as ie}from"micromark-util-symbol";import{markdownLineEnding as oe}from"micromark-util-character";import{createPortal as se}from"react-dom";import ae from"use-resize-observer";import{unified as le}from"unified";import ce from"rehype-stringify";import de from"remark-parse";import ue from"remark-rehype";function me(e,t){return e.name===t}const he=C(null);function pe(e){const t=$(he);if(!t)throw new Error("useContext must be used within a Provider");const{setHandles:r}=t;return S(()=>{if(e)return r(e),()=>{r({})}},[e]),t}function ge({children:t,...r}){const[n,i]=M({});return e(he.Provider,{value:{...r,handles:n,setHandles:i},children:t})}const fe=function({ext:t}){const{result:r}=a(async e=>{try{return(await function(e){switch(e){case"../../images/file/docx.svg":return import("./docx-2LwnwWwv.js");case"../../images/file/html.svg":return import("./html-BSKTzJZQ.js");case"../../images/file/jpg.svg":return import("./jpg-muicsKjX.js");case"../../images/file/md.svg":return import("./md-DRS7QmE8.js");case"../../images/file/mp3.svg":return import("./mp3-HhP1Gk4B.js");case"../../images/file/mp4.svg":return import("./mp4-CWxSyjdn.js");case"../../images/file/pdf.svg":return import("./pdf-CWBi2u4Z.js");case"../../images/file/png.svg":return import("./png-DNwbFeJk.js");case"../../images/file/pptx.svg":return import("./pptx-Bv4V_F_3.js");case"../../images/file/py.svg":return import("./py-D30pRTJN.js");case"../../images/file/txt.svg":return import("./txt-CsqmEKjF.js");case"../../images/file/unknown.svg":return import("./unknown-DW8znerP.js");case"../../images/file/xlsx.svg":return import("./xlsx-CmkshfKZ.js");default:return new Promise(function(t,r){("function"==typeof queueMicrotask?queueMicrotask:setTimeout)(r.bind(null,new Error("Unknown variable dynamic import: "+e)))})}}(`../../images/file/${e}.svg`)).default}catch{return(await import("./unknown-DW8znerP.js")).default}},[t]);return r?e("img",{width:30,height:30,src:r}):null},be=function({className:r,name:n,size:i=0,loading:a,error:l,onRemove:c,onClick:d}){const u=T.extname(n).substring(1),m=t(ke,{className:r,$error:!!l,onClick:d,children:[t(we,{children:[e(fe,{ext:u}),a&&e(O,{variant:"primary"})]}),t(xe,{children:[e("h4",{children:n}),i>0&&e("p",{children:o(i,1)})]}),c&&e(ye,{onClick:e=>{e.preventDefault(),c()},children:e("i",{className:"bi bi-trash3"})})]});return l?e(s,{placement:"top",tooltip:l,children:m}):m};function ve(t){const{renderFileItem:r}=pe();return r?r({Component:be,props:t}):e(be,{...t})}const ye=n.div`
|
|
2
2
|
width: 1.75rem;
|
|
3
3
|
height: 1.75rem;
|
|
4
4
|
align-items: center;
|
|
@@ -96,7 +96,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
96
96
|
}
|
|
97
97
|
`};
|
|
98
98
|
|
|
99
|
-
`;let Ce=class{#e;#t;constructor(){this.#t=new K}onResult(e){this.#t.on("result",e)}onRecording(e){this.#t.on("recording",e)}onTranscribing(e){this.#t.on("transcribing",e)}start(){if(!this.#e){const e=window.SpeechRecognition||window.webkitSpeechRecognition;if(!e)return void u.error("浏览器不支持语音识别");const t=new e;t.continuous=!0,t.lang=navigator.language,t.onresult=e=>{const t=Array.from(e.results).at(-1)?.item(0).transcript;t&&this.#t.emit("result",t)},t.onspeechend=()=>{this.#t.emit("recording",!1)},t.onerror=e=>{this.#t.emit("recording",!1),u.error(e.error)},this.#e=t}this.#t.emit("recording",!0),this.#e.start()}stop(){this.#e?.stop(),this.#t.emit("recording",!1)}},$e=class{#t;#r;#n;#i;constructor(e){this.#r=e,this.#t=new K}async start(){try{this.#n=await navigator.mediaDevices.getUserMedia({audio:!0}),this.#i=new AudioContext;const e=this.#i.createScriptProcessor(0,1,1);this.#i.createMediaStreamSource(this.#n).connect(e),e.connect(this.#i.destination);let t=[],r=0;const n=.01,i=1;e.onaudioprocess=e=>{const o=e.playbackTime,s=e.inputBuffer.getChannelData(0);let a=!0;for(let e=0;e<s.length;e++)if(Math.abs(s[e])>n){a=!1;break}if(a){const e=o-r;e>i&&(t.length>1&&(this.save(t),t=[]),e>10&&this.stop())}else t.push(new Float32Array(s)),r=o},this.#t.emit("recording",!0)}catch(e){u.error(e.message)}}convertBuffer(e){const t=new Float32Array(e),r=new Int16Array(e.length);for(let e=0;e<t.length;e++){const n=Math.max(-1,Math.min(1,t[e]));r[e]=n<0?32768*n:32767*n}return r}async encodeMP3(e){const{Mp3Encoder:t}=await import("@breezystack/lamejs"),r=new t(1,44100,128),n=[];for(const t of e){const e=this.convertBuffer(t),i=1152;let o=e.length;for(let t=0;o>=0;t+=i){const s=e.subarray(t,t+i),a=r.encodeBuffer(s);n.push(new Int8Array(a)),o-=i}}return n.push(r.flush()),new Blob(n,{type:"audio/mp3"})}async save(e){const t=await this.encodeMP3(e);try{this.#t.emit("transcribing",!0);const e=await this.#r(t);e&&this.#t.emit("result",e)}catch(e){}finally{this.#t.emit("transcribing",!1)}}stop(){this.#n&&(this.#n.getTracks().forEach(e=>e.stop()),this.#n=void 0),this.#i&&(this.#i.close(),this.#i=void 0),this.#t.emit("recording",!1)}onResult(e){this.#t.on("result",e)}onRecording(e){this.#t.on("recording",e)}onTranscribing(e){this.#t.on("transcribing",e)}};const Se=q(({onResult:r,model:n="builtin"},i)=>{const{request:o}=pe(),a=z(()=>{if("builtin"!==n)return async e=>{const t=new FormData;t.append("file",e,"audio.mp3"),t.append("model",n);return(await o({url:"/transcriptions",method:"post",data:t})).text}},[n]),{start:l,stop:c,recording:d,transcribing:u}=function({onResult:e,loader:t}){const r=F(),[n,i]=M(!1),[o,s]=M(!1);return S(()=>()=>{r.current&&(r.current.stop(),r.current=void 0)},[]),{recording:n,transcribing:o,start:async()=>{r.current||(r.current=t?new $e(t):new Ce,r.current.onRecording(i),r.current.onTranscribing(s),r.current.onResult(e)),r.current.start()},stop:()=>{r.current&&r.current.stop()}}}({onResult:r,loader:a});return
|
|
99
|
+
`;let Ce=class{#e;#t;constructor(){this.#t=new K}onResult(e){this.#t.on("result",e)}onRecording(e){this.#t.on("recording",e)}onTranscribing(e){this.#t.on("transcribing",e)}start(){if(!this.#e){const e=window.SpeechRecognition||window.webkitSpeechRecognition;if(!e)return void u.error("浏览器不支持语音识别");const t=new e;t.continuous=!0,t.lang=navigator.language,t.onresult=e=>{const t=Array.from(e.results).at(-1)?.item(0).transcript;t&&this.#t.emit("result",t)},t.onspeechend=()=>{this.#t.emit("recording",!1)},t.onerror=e=>{this.#t.emit("recording",!1),u.error(e.error)},this.#e=t}this.#t.emit("recording",!0),this.#e.start()}stop(){this.#e?.stop(),this.#t.emit("recording",!1)}},$e=class{#t;#r;#n;#i;constructor(e){this.#r=e,this.#t=new K}async start(){try{this.#n=await navigator.mediaDevices.getUserMedia({audio:!0}),this.#i=new AudioContext;const e=this.#i.createScriptProcessor(0,1,1);this.#i.createMediaStreamSource(this.#n).connect(e),e.connect(this.#i.destination);let t=[],r=0;const n=.01,i=1;e.onaudioprocess=e=>{const o=e.playbackTime,s=e.inputBuffer.getChannelData(0);let a=!0;for(let e=0;e<s.length;e++)if(Math.abs(s[e])>n){a=!1;break}if(a){const e=o-r;e>i&&(t.length>1&&(this.save(t),t=[]),e>10&&this.stop())}else t.push(new Float32Array(s)),r=o},this.#t.emit("recording",!0)}catch(e){u.error(e.message)}}convertBuffer(e){const t=new Float32Array(e),r=new Int16Array(e.length);for(let e=0;e<t.length;e++){const n=Math.max(-1,Math.min(1,t[e]));r[e]=n<0?32768*n:32767*n}return r}async encodeMP3(e){const{Mp3Encoder:t}=await import("@breezystack/lamejs"),r=new t(1,44100,128),n=[];for(const t of e){const e=this.convertBuffer(t),i=1152;let o=e.length;for(let t=0;o>=0;t+=i){const s=e.subarray(t,t+i),a=r.encodeBuffer(s);n.push(new Int8Array(a)),o-=i}}return n.push(r.flush()),new Blob(n,{type:"audio/mp3"})}async save(e){const t=await this.encodeMP3(e);try{this.#t.emit("transcribing",!0);const e=await this.#r(t);e&&this.#t.emit("result",e)}catch(e){}finally{this.#t.emit("transcribing",!1)}}stop(){this.#n&&(this.#n.getTracks().forEach(e=>e.stop()),this.#n=void 0),this.#i&&(this.#i.close(),this.#i=void 0),this.#t.emit("recording",!1)}onResult(e){this.#t.on("result",e)}onRecording(e){this.#t.on("recording",e)}onTranscribing(e){this.#t.on("transcribing",e)}};const Se=q(({onResult:r,model:n="builtin"},i)=>{const{request:o}=pe(),a=z(()=>{if("builtin"!==n)return async e=>{const t=new FormData;t.append("file",e,"audio.mp3"),t.append("model",n);return(await o({url:"/transcriptions",method:"post",data:t})).text}},[n]),{start:l,stop:c,recording:d,transcribing:u}=function({onResult:e,loader:t}){const r=F(),[n,i]=M(!1),[o,s]=M(!1);return S(()=>()=>{r.current&&(r.current.stop(),r.current=void 0)},[]),{recording:n,transcribing:o,start:async()=>{r.current||(r.current=t?new $e(t):new Ce,r.current.onRecording(i),r.current.onTranscribing(s),r.current.onResult(e)),r.current.start()},stop:()=>{r.current&&r.current.stop()}}}({onResult:r,loader:a});return A(i,()=>({start:l,stop:c}),[l,c]),e(s,{tooltip:d?"停止":"语音输入",placement:"top",children:t(je,{onMouseDown:e=>{e.preventDefault(),d?c():l()},children:[u&&e(Me,{animation:"border",variant:"primary",size:"sm"}),e("i",d?{className:"bi bi-mic-fill text-danger"}:{className:"bi bi-mic"})]})})}),Me=n(O)`
|
|
100
100
|
position: absolute;
|
|
101
101
|
--bs-spinner-width: .75rem;
|
|
102
102
|
--bs-spinner-height: .75rem;
|
|
@@ -125,10 +125,10 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
125
125
|
&:hover {
|
|
126
126
|
background-color: var(--bs-secondary-bg-subtle);
|
|
127
127
|
}
|
|
128
|
-
`;function qe(){const{request:n,reset:i,conversationId:o}=pe(),[s,a]=M(!1),[l,c]=M(""),[d,u]=M(""),m=h(e=>{u(e)},500);return S(()=>{m(l)},[l]),t(r,{children:[e(Fe,{tooltip:"历史记录",placement:"top",onClick:()=>a(!0),children:e("i",{className:"bi bi-clock-history"})}),e(p,{show:s,onHide:()=>{a(!1)},placement:"end",headerAs:Le,header:t(r,{children:[e("span",{children:"聊天历史记录"}),e(Pe,{children:e(_,{type:"search",placeholder:"搜索聊天记录...",value:l,onChange:e=>c(e.target.value),autoFocus:!0})})]}),bodyAs:ze,children:s&&e(De,{children:e(
|
|
128
|
+
`;function qe(){const{request:n,reset:i,conversationId:o}=pe(),[s,a]=M(!1),[l,c]=M(""),[d,u]=M(""),m=h(e=>{u(e)},500);return S(()=>{m(l)},[l]),t(r,{children:[e(Fe,{tooltip:"历史记录",placement:"top",onClick:()=>a(!0),children:e("i",{className:"bi bi-clock-history"})}),e(p,{show:s,onHide:()=>{a(!1)},placement:"end",headerAs:Le,header:t(r,{children:[e("span",{children:"聊天历史记录"}),e(Pe,{children:e(_,{type:"search",placeholder:"搜索聊天记录...",value:l,onChange:e=>c(e.target.value),autoFocus:!0})})]}),bodyAs:ze,children:s&&e(De,{children:e(Ie,{useWindow:!1,source:e=>n({url:"/conversation",params:{...e,...d&&{keyword:d}}}),render:({data:s,loading:l,setData:c})=>s.length||l?t(r,{children:[s.map((s,l)=>t(I,{children:[l>0&&e("hr",{className:"mx-2 my-2"}),t(Ae,{children:[o===s.id&&e(B,{bg:"secondary",children:"当前"}),e("span",{className:"overflow-hidden text-truncate flex-fill",onClick:()=>{i(s),a(!1)},children:s.title||"未命名对话"}),t(U,{align:"end",children:[e(U.Toggle,{size:"sm",className:"no-caret",as:Fe,children:e("i",{className:"bi bi-three-dots-vertical"})}),t(U.Menu,{children:[e(g,{buttonProps:{as:U.Item},text:t(r,{children:[e("i",{className:"bi bi-pencil me-2"}),"重命名"]}),modalProps:{header:"编辑对话名称"},schema:{type:"object",properties:{title:{type:"string"}}},uiSchema:{title:{"ui:label":!1,"ui:autofocus":!0}},formData:{title:s.title},onRequest:e=>n({url:`/conversation/${s.id}`,method:"PUT",data:e}),onSuccess:(e,t)=>{c(e=>{const r=e.findIndex(e=>e.id===s.id);-1!==r&&Object.assign(e[r],t)})}}),t(f,{as:U.Item,confirm:"确定要删除吗?",onRequest:()=>n({url:`/conversation/${s.id}`,method:"DELETE"}),onSuccess:()=>{c(e=>{const t=e.findIndex(e=>e.id===s.id);-1!==t&&e.splice(t,1)})},children:[e("i",{className:"bi bi-trash3 me-2"}),"删除"]})]})]})]})]},s.id)),l&&e(b,{wrap:!0})]}):e("div",{className:"text-muted text-center py-5",children:d?"未找到匹配的聊天记录":"暂无对话记录"})},d)})})]})}const ze=n.div`
|
|
129
129
|
padding: 0;
|
|
130
130
|
overflow: hidden;
|
|
131
|
-
`,
|
|
131
|
+
`,Ae=n.div`
|
|
132
132
|
gap: .5rem;
|
|
133
133
|
display: flex;
|
|
134
134
|
cursor: pointer;
|
|
@@ -147,7 +147,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
147
147
|
display: block;
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
|
-
`,
|
|
150
|
+
`,Ie=n(m)`
|
|
151
151
|
display: flex;
|
|
152
152
|
flex-direction: column;
|
|
153
153
|
padding: .5rem;
|
|
@@ -173,7 +173,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
173
173
|
margin-bottom: .5rem;
|
|
174
174
|
display: flex;
|
|
175
175
|
padding: 0 .125rem;
|
|
176
|
-
`;var Je=q(function({readonly:t,autoFocus:r,placeholder:n,minRows:i,maxRows:o,onSubmit:s,onFocusedChange:a,onQueryChange:l,onPaste:c},d){const u=F(null),[m,h]=M(""),p=R(()=>{const e=u.current;if(!e)return;e.focus();const t=e.value.length;e.setSelectionRange(t,t)},[]),g=R(e=>{const t="string"==typeof e?e:e(m);h(t),l?.(t.trim()),requestAnimationFrame(()=>{p()})},[m,l,p]);return
|
|
176
|
+
`;var Je=q(function({readonly:t,autoFocus:r,placeholder:n,minRows:i,maxRows:o,onSubmit:s,onFocusedChange:a,onQueryChange:l,onPaste:c},d){const u=F(null),[m,h]=M(""),p=R(()=>{const e=u.current;if(!e)return;e.focus();const t=e.value.length;e.setSelectionRange(t,t)},[]),g=R(e=>{const t="string"==typeof e?e:e(m);h(t),l?.(t.trim()),requestAnimationFrame(()=>{p()})},[m,l,p]);return A(d,()=>({setContent:g,focus(){p()}}),[g,p]),e(Ke,{ref:u,value:m,minRows:i,maxRows:o,readOnly:t,autoFocus:r,placeholder:n,onFocus:()=>a?.(!0),onBlur:()=>a?.(!1),onChange:e=>{h(e.target.value),l?.(e.target.value.trim())},onPaste:e=>{c?.(e.nativeEvent)},onKeyDown:e=>{e.nativeEvent.isComposing||"Enter"!==e.key||e.shiftKey||(e.preventDefault(),e.stopPropagation(),s?.())}})});const Ke=n(W)`
|
|
177
177
|
min-height: calc(1.5em + 1rem);
|
|
178
178
|
width: 100%;
|
|
179
179
|
padding: 0.5rem 0.5rem 0.5rem 0.75rem;
|
|
@@ -183,7 +183,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
183
183
|
flex: 1;
|
|
184
184
|
line-height: 1.5;
|
|
185
185
|
background: transparent;
|
|
186
|
-
`;var We=q(function({suggestion:r,fileTypes:n,speech:i,scrollRef:o,variables:a,minRows:d=1,maxRows:m=5,toolbar:h,toolbarActions:p,disabled:g,autoFocus:f,renderEditableInput:b,placeholder:y="请输入你的问题, Enter+Shift换行, Enter发送",supportStop:x=!1,onEvent:w},k){const E=F(null),N=F(null),j=F(null),[C,$]=M(""),[q,
|
|
186
|
+
`;var We=q(function({suggestion:r,fileTypes:n,speech:i,scrollRef:o,variables:a,minRows:d=1,maxRows:m=5,toolbar:h,toolbarActions:p,disabled:g,autoFocus:f,renderEditableInput:b,placeholder:y="请输入你的问题, Enter+Shift换行, Enter发送",supportStop:x=!1,onEvent:w},k){const E=F(null),N=F(null),j=F(null),[C,$]=M(""),[q,I]=M(""),[D,L]=v([]),[T,O]=M(!1),B=n&&n.length>0?n:Ee,{send:U,resume:_,stop:Q,suggest:J}=function(e){const{setMessages:t,setSuggestions:r,setLoading:n,conversationId:i,setConversationId:o,request:s,handles:a,messages:d}=pe(),u=!0===e?.supportStop,m=F(null),h=F(),p=F(i),g=()=>(m.current&&m.current.abort(),m.current=new AbortController,m.current.signal),{onComplete:f,onEvent:b}=a,v=R(e=>{const r=(e,t)=>{Object.entries(t).forEach(([t,n])=>{if("string"!=typeof n)if(!n||"object"!=typeof n||Array.isArray(n))e[t]=n;else if("index"in n){const{index:i,...o}=n;Array.isArray(e[t])||(e[t]=[]),e[t][i]||(e[t][i]={}),r(e[t][i],o)}else e[t]=n;else e[t]="string"==typeof e[t]?e[t]+n:n})};if(e.data)if("[DONE]"!=e.data)try{const n=JSON.parse(e.data);n.conversation?(p.current=n.conversation,o(n.conversation)):n.event?b?.(n.event):t(e=>{const t=e[e.length-1];if(n.chunks){if(t.chunks){const e=n.chunks.index;if(t.chunks[e]||(t.chunks[e]={content:"",reasoning:"",tools:[]}),n.chunks.error)t.chunks[e].error=n.chunks.error;else if(n.chunks.tools){t.chunks[e].tools||(t.chunks[e].tools=[]);const{index:i,...o}=n.chunks.tools;t.chunks[e].tools[i]||(t.chunks[e].tools[i]={name:"",title:"",arguments:""}),r(t.chunks[e].tools[i],o)}else if(n.chunks.reasoning)t.chunks[e].reasoning+=n.chunks.reasoning;else if(n.chunks.content)if("object"==typeof n.chunks.content){Array.isArray(t.chunks[e].content)||(t.chunks[e].content=[]);const r=n.chunks.content.index,i=n.chunks.content.value;"string"==typeof i?(t.chunks[e].content[r]||(t.chunks[e].content[r]=""),t.chunks[e].content[r]+=i):t.chunks[e].content[r]=i}else t.chunks[e].content+=n.chunks.content;else n.chunks.node&&(t.chunks[e].node={...t.chunks[e].node,...n.chunks.node})}}else n.stats?t.stats=n.stats:n.id&&(t.id=n.id)})}catch(e){console.error(e)}else t(e=>{e[e.length-1].loading=!1})},[a]),y=R(e=>{if(l(e))t(e=>{const t=e[e.length-1];t&&(t.loading=!1)});else{let r="未知错误";c(e)&&(r=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),t(e=>{const t=e[e.length-1];t.chunks&&(0===t.chunks.length?t.chunks=[{error:r}]:t.chunks[t.chunks.length-1].error=r),t.loading=!1})}},[]),x=R(async e=>{h.current=e;try{const r=g();n(!0),t(e=>{e[e.length-1].loading=!0,delete e[e.length-1].stats});let i={url:"message",method:"get",params:{id:e},onMessage:v,signal:r};await s(i)}catch(e){y(e)}finally{m.current=null,h.current=void 0}n(!1),f?.()},[s,a]),w=R(async(e,...i)=>{"string"==typeof e&&(e={query:e,...i});const{query:o,files:a=[],variables:l={},quote:c}=e;let d=null;if(o||a.length>0){const e=g();n(!0),r([]),t(e=>{e.push({query:o,quote:c,files:a,variables:l,chunks:[],loading:!0})});try{let t={method:"post",data:{query:o,quote:c,files:a,variables:l,conversation:p.current},onMessage:v,signal:e};d=(await s(t)).message??null}catch(e){y(e)}finally{m.current=null}d?await x(d.id):(n(!1),f?.())}},[s,a]),k=R(async e=>{let r=null;try{const i=g();n(!0),t(e=>{e[e.length-1].loading=!0});let o={url:"resume",method:"post",data:{conversation:p.current,chunk:e.chunk,tool:e.tool,payload:e.payload},onMessage:v,signal:i};r=(await s(o)).message??null}catch(e){y(e)}finally{m.current=null}r?await x(r.id):(n(!1),f?.())},[s,a]),E=R(async()=>{try{const e=g();let t={method:"post",url:"/suggestion",data:{conversation:p.current},signal:e};const n=await s(t);r(n)}catch{}finally{m.current=null}},[s]),N=R(async()=>{if(u)if(h.current)try{await s({url:"stop",method:"post",data:{id:h.current}})}catch{}else m.current?.abort()},[s,u]),j=()=>{const e=d[d.length-1];e&&e.id&&2===e.status&&x(e.id)};return S(()=>(j(),()=>{m.current&&m.current.abort()}),[]),S(()=>{i!=p.current&&(p.current=i,m.current&&m.current.abort(),j())},[i]),{send:w,resume:k,suggest:E,stop:N}}({supportStop:x}),K=z(()=>({onSuccess(){r&&J()},onComplete(){requestAnimationFrame(()=>{j.current?.focus()})},onEvent:w}),[r,w]),{messages:W,loading:X,request:V,setInput:Y}=pe(K),[G,Z]=M();S(()=>{const e=W.filter(e=>!!e.query);Z(e.at(-1)?.variables)},[W]),S(()=>{!n&&D.length>0&&L([])},[n]),S(()=>{const e=o.current;if(e&&U){const t=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const t={query:e.target.text};a&&(t.variables={...G,...a.values}),U(t)}};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[a,G]);const ee=R(e=>{const t=e.map((e,t)=>{if(e.name)return e;const r=e.type.split("/")[1]||"png";return new File([e],`screenshot-${Date.now()}-${t}.${r}`,{type:e.type})}).filter(e=>!(!B.includes("*")&&!B.includes(P.extname(e.name)))&&!(e.size>20971520)).slice(0,6-D.length);return 0!==t.length&&(L(e=>{e.push(...t.map(e=>({file:e})))}),t.forEach(async e=>{try{const t=new FormData;t.set("file",e);const r=await V({method:"POST",url:"/upload",data:t});L(t=>{const n=t.find(t=>t.file===e);n&&(n.path=r.path)})}catch(t){const r=c(t)?t.message:"unknown error";L(t=>{const n=t.find(t=>t.file===e);n&&(n.error=r)})}}),!0)},[D.length,B,V,L]),te=R(({target:e})=>{e.files&&ee(Array.from(e.files)),e.value=""},[ee]),re=R(e=>{const t=Array.from(e.clipboardData?.files||[]),r=Array.from(e.clipboardData?.items||[]).filter(e=>"file"===e.kind).map(e=>e.getAsFile()).filter(e=>!!e),n=t.length>0?t:r;0!==n.length&&ee(n)&&e.preventDefault()},[ee]),ne=D.flatMap(({file:e,path:t})=>t?[{name:e.name,size:e.size,path:t}]:[]),ie=R(()=>{if(C||ne.length>0){const e={query:C,files:ne};a&&(e.variables={...G,...a.values}),q&&(e.quote=q),U(e),j.current?.setContent(""),I(""),L([]),N.current?.stop(!0)}},[C,ne,a,G,q]),oe=z(()=>({setQuery(e){j.current?.setContent(e)},setQuote:I,send:U,resume:_,focus(){j.current?.focus()}}),[I,U,_]);if(S(()=>{Y(oe)},[oe]),A(k,()=>oe,[oe]),a){const t=a.config.filter(e=>!(e.key in(a.values||{}))),r=(e,r=!1)=>!!e&&t.every(t=>{const n=!t.required||e?.[t.key];return!n&&r&&u.error("请完善必填信息"),n});if(t.length>0&&!r(G))return e(Ve,{children:e(Xe,{children:e(Ne,{values:G,variables:t,onSubmit:({formData:e})=>{if(r(e,!0))return Z(e),!0}})})})}return t(nt,{children:[h&&e(_e,{actions:p,children:h}),t(rt,{$focused:T&&!X,children:[D.length>0&&e(Ge,{children:D.map(({file:t,error:r,path:n},i)=>{const o=void 0===r&&void 0===n;return e(Ye,{name:t.name,size:t.size,error:r,loading:o,onRemove:()=>{L(e=>{const r=e.findIndex(e=>e.file===t);-1!==r&&e.splice(r,1)})}},i)})}),!!q&&t(tt,{children:[e("i",{className:"bi bi-quote"}),e("p",{children:q}),e(H,{onClick:()=>I("")})]}),t(Ze,{children:[b?.({Component:Je,props:{ref:j,readonly:X||g,autoFocus:f,placeholder:y,minRows:d,maxRows:m,onSubmit:ie,onFocusedChange:O,onQueryChange:$,onPaste:re}})||e(Je,{ref:j,readonly:X||g,autoFocus:f,placeholder:y,minRows:d,maxRows:m,onSubmit:ie,onFocusedChange:O,onQueryChange:$,onPaste:re}),t(et,{children:[n&&e(s,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:t(je,{disabled:D.length>=6,onClick:()=>E.current?.click(),children:[e("input",{onChange:te,multiple:!0,accept:B.join(","),ref:E,type:"file",hidden:!0}),e("i",{className:"bi bi-file-earmark-arrow-up"})]})}),i&&e(Se,{ref:N,model:i.model,onResult:e=>{j.current?.setContent(t=>t+e+" ")}}),e(je,{$fill:X&&x,className:X&&x?"text-danger":"text-primary",disabled:!C&&0===ne.length&&!X||X&&!x,onClick:e=>{e.preventDefault(),X&&x?Q():ie()},children:e("i",X&&x?{className:"bi bi-square-fill fs-7"}:{className:"bi bi-send-fill"})})]})]})]})]})});const Xe=n.div`
|
|
187
187
|
border-radius: var(--bs-border-radius-lg);
|
|
188
188
|
box-shadow: var(--bs-box-shadow-sm);
|
|
189
189
|
margin: 1rem;
|
|
@@ -608,7 +608,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
608
608
|
max-height: 100%;
|
|
609
609
|
max-width: 100%;
|
|
610
610
|
}
|
|
611
|
-
`,Ft=C(null);function Rt(){const e=$(Ft);if(!e)throw new Error("useContext must be used within a Provider");return e}function qt({children:t,...r}){return e(Ft.Provider,{value:r,children:t})}function zt(e){const[t,r]=M(e);return S(()=>{r(e)},[e]),[t,r]}function
|
|
611
|
+
`,Ft=C(null);function Rt(){const e=$(Ft);if(!e)throw new Error("useContext must be used within a Provider");return e}function qt({children:t,...r}){return e(Ft.Provider,{value:r,children:t})}function zt(e){const[t,r]=M(e);return S(()=>{r(e)},[e]),[t,r]}function At({description:r,options:n}){const{resume:i,disabled:o,response:s}=Rt(),[a,l]=zt(s?.result),c=o||void 0!==a;return t(It,{children:[r&&e(Dt,{children:r}),n.map((t,r)=>e(Lt,{$selected:a===t,$disabled:c,onClick:()=>(e=>{c||(l(e),i(e))})(t),children:t},r))]})}const It=n.div`
|
|
612
612
|
display: flex;
|
|
613
613
|
flex-direction: column;
|
|
614
614
|
gap: 0.5rem;
|
|
@@ -699,7 +699,27 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
699
699
|
button {
|
|
700
700
|
padding: .25rem 1.5rem;
|
|
701
701
|
}
|
|
702
|
-
`;function Jt(e){return e instanceof Error&&e.message||String(e)}class Kt extends L{state={hasError:!1};static getDerivedStateFromError(){return{hasError:!0}}componentDidCatch(e,t){this.props.onRenderError(e)}render(){return this.state.hasError?null:this.props.children}}function Wt({suspend:t}){const{readonly:r,input:n}=pe(),{chunkIndex:i,chunk:o,toolIndex:s,tool:a,isLastMessage:l}=function(){const e=$(jt);if(!e)throw new Error("useContext must be used within a Provider");return e}(),c=R(e=>{n?.resume({chunk:i,tool:s,payload:e})},[i,n,s]),d=R(e=>{c({error:Jt(e)})},[c]);let u=null;switch(t.type){case"select":u=e(
|
|
702
|
+
`;function Jt(e){return e instanceof Error&&e.message||String(e)}class Kt extends L{state={hasError:!1};static getDerivedStateFromError(){return{hasError:!0}}componentDidCatch(e,t){this.props.onRenderError(e)}render(){return this.state.hasError?null:this.props.children}}function Wt({suspend:t}){const{readonly:r,input:n}=pe(),{chunkIndex:i,chunk:o,toolIndex:s,tool:a,isLastMessage:l}=function(){const e=$(jt);if(!e)throw new Error("useContext must be used within a Provider");return e}(),c=R(e=>{n?.resume({chunk:i,tool:s,payload:e})},[i,n,s]),d=R(e=>{c({error:Jt(e)})},[c]);let u=null;switch(t.type){case"select":u=e(At,{...t.select});break;case"form":u=e(Pt,{...t.form});break;case"confirm":u=e(Ut,{...t.confirm})}const m=a?.response||o.node?.output;return e(qt,{resume:c,disabled:r||!!o.error||!l,response:"(Empty)"===m?void 0:m,children:e(Kt,{onRenderError:d,children:u})})}function Xt({value:t,cite:n}){const i=t=>"string"==typeof t?e(vt,{content:t,cite:n}):"image"===t.type?e(St,{src:t.image}):"suspend"===t.type?e(Wt,{suspend:t.suspend}):null;return Array.isArray(t)?e(r,{children:t.map((t,r)=>e(I,{children:i(t)},r))}):i(t)}const Vt=({reasoning:r,loading:n})=>{const[i,o]=M(!1);return S(()=>{n||o(!1)},[n]),e(I,{children:e("div",{className:"mb-2",children:t("div",i?{className:"shadow-sm rounded bg-white fs-7",children:[t("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[n?e(O,{animation:"border",variant:"primary",size:"sm"}):e("i",{className:"bi bi-check-circle-fill text-success"}),e("span",{className:"text-muted",children:n?"思考中":"已思考"}),e("i",{className:"bi bi-caret-up-fill text-muted"})]}),e("div",{className:"border-top p-2 d-flex flex-column gap-2",children:e("div",{className:"border rounded bg-light p-2",children:e(vt,{content:r})})})]}:{onClick:()=>o(!0),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[n?e(O,{animation:"border",variant:"primary",size:"sm"}):e("i",{className:"bi bi-check-circle-fill text-success"}),e("span",{className:"text-muted",children:n?"思考中":"已思考"}),e("i",{className:"bi bi-caret-down-fill text-muted"})]})})})},Yt=D(t=>{const{renderReasoning:r}=pe();return r?r({Component:Vt,props:t}):e(Vt,{...t})});function Gt({tool:r,loading:n}){const{logLevel:i="none",renderToolContent:o}=pe(),s="response"in r,a=!s&&!n,{response:l,title:c,content:d,showStatus:u,defaultExpanded:m}=z(()=>{const i=(e=>{try{return JSON.parse(e.arguments)}catch(e){return null}})(r),a=e("span",{className:"text-truncate",children:r.title}),l=t("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"参数"})}),e(er,{className:"border-top p-2",children:r.arguments})]}),s&&t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"响应"})}),e(er,{className:"border-top p-2",children:r.response||"None"})]})]}),c=r.content&&e(Xt,{value:r.content});if(o){const e=o({tool:r,args:i,loading:n});if(e)return{title:null===e.title?null:e.title??a,response:null===e.response?null:e.response??l,content:null===e.content?null:e.content??c,showStatus:e.showStatus??!0,defaultExpanded:e.defaultExpanded??!1}}return{title:a,response:l,content:c,showStatus:!0,defaultExpanded:!1}},[r,o]),[h,p]=M(m),g="all"==i&&null!==l;return t(I,{children:[c&&e("div",{className:"mb-2",children:t(Zt,{className:"shadow-sm rounded bg-white fs-7",$open:h,children:[t("div",{onClick:()=>p(g&&!h),role:"button",className:"d-flex align-items-center flex-nowrap p-1 px-2 gap-2",children:[a?e("i",{className:"bi bi-dash-circle-fill text-muted"}):s?r.error?e("i",{className:"bi bi-x-circle-fill text-danger"}):e("i",{className:"bi bi-check-circle-fill text-success"}):e(O,{animation:"border",variant:"primary",size:"sm"}),u&&e("span",{className:"text-muted",children:a?"已取消":s?"已使用":"正在使用"}),c,g&&e("i",h?{className:"bi bi-caret-up-fill text-muted"}:{className:"bi bi-caret-down-fill text-muted"})]}),h&&l]})}),d]})}const Zt=n.div`
|
|
703
|
+
width: ${e=>e.$open?"auto":"fit-content"};
|
|
704
|
+
max-width: 100%;
|
|
705
|
+
overflow:hidden;
|
|
706
|
+
`,er=n.div`
|
|
707
|
+
white-space: pre-wrap;
|
|
708
|
+
overflow-y: auto;
|
|
709
|
+
overflow-x: hidden;
|
|
710
|
+
max-height: 150px;
|
|
711
|
+
`;function tr({node:r}){const[n,i]=M(!1),{nodeIconResolver:o}=pe(),s=!("latency"in r),a=s?e(O,{animation:"border",variant:"primary",size:"sm"}):r.error?e("i",{className:"bi bi-x-circle-fill text-danger"}):e("i",{className:"bi bi-check-circle-fill text-success"});return t("div",{className:"shadow-sm rounded bg-white fs-7",children:[t(nr,{onClick:()=>i(e=>!e),role:"button",className:"d-flex align-items-center p-1 px-2 gap-1",children:[e("i",{className:"bi bi-chevron-down text-muted"}),o?.(r.type),e(ir,{className:"me-2 text-truncate",children:r.title}),e("span",{className:"me-auto"}),!s&&e("span",{className:"text-nowrap text-muted",children:rr(r.latency)}),e("span",{children:a})]}),n&&t("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"输入"})}),e("div",{className:"border-top p-2 overflow-hidden text-pre-wrap",children:r.input?JSON.stringify(r.input,null,4):"None"})]}),t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"输出"})}),e("div",{className:"border-top p-2 overflow-hidden text-pre-wrap",children:r.output?JSON.stringify(r.output,null,4):"None"})]})]})]})}const rr=e=>e<1?`${(1e3*e).toFixed(3)} ms`:e>60?`${parseInt(Math.round(e/60).toString())} m ${(e%60).toFixed(3)} s`:`${e.toFixed(3)} s`,nr=n.div`
|
|
712
|
+
svg {
|
|
713
|
+
flex-shrink: 0;
|
|
714
|
+
}
|
|
715
|
+
`,ir=n.span`
|
|
716
|
+
display: inline-block;
|
|
717
|
+
`;function or({nodes:r,loading:n}){const[i,o]=M(!1),s=r.some(e=>"error"in e),a=n?e(O,{animation:"border",variant:"primary",size:"sm"}):e("i",s?{className:"bi bi-x-circle-fill text-danger"}:{className:"bi bi-check-circle-fill text-success"});return e("div",{className:"mb-2",children:t("div",i?{className:"shadow-sm rounded bg-white fs-7",children:[t("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[a,e("span",{children:"工作流"}),e("i",{className:"bi bi-caret-up-fill text-muted"})]}),e("div",{className:"border-top p-2 d-flex flex-column gap-1 bg-light bg-opacity-50",children:r.map((t,r)=>e(tr,{node:t},r))})]}:{onClick:()=>o(!0),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[a,e("span",{children:"工作流"}),e("i",{className:"bi bi-caret-down-fill text-muted"})]})})}const sr=D(q(({chunks:r,loading:n,isLast:i,cite:o,deleted:s=!1},a)=>{const{logLevel:l="none"}=pe(),c=z(()=>r.flatMap(e=>e.node?[e.node]:[]),[r]);return t(ar,{ref:a,$deleted:s,children:["all"===l&&c.length>0&&e(or,{loading:n,nodes:c}),r.map((r,s)=>t(I,{children:[r.reasoning&&["stats","all"].includes(l)&&e(Yt,{reasoning:r.reasoning,loading:n&&!r.content&&0===(r.tools||[]).length}),r.content&&e(Ct,{value:{chunkIndex:s,chunk:r,isLastMessage:i},children:e(Xt,{cite:o,value:r.content})}),(r.tools||[]).map((t,o)=>e(Ct,{value:{chunkIndex:s,chunk:r,toolIndex:o,tool:t,isLastMessage:i},children:e(Gt,{tool:t,loading:n})},o)),r.error&&e(vt,{content:`[${r.error}]`})]},s))]})})),ar=n.div`
|
|
718
|
+
${e=>e.$deleted&&i`
|
|
719
|
+
text-decoration-line: line-through;
|
|
720
|
+
color: var(--bs-secondary);
|
|
721
|
+
`}
|
|
722
|
+
`;class lr{#t;#o=[];#s=0;constructor(){this.#t=new K}speak(){for(this.#t.emit("playing",!0);this.#s<this.#o.length;){const e=this.#o[this.#s],t=new SpeechSynthesisUtterance(e),r=++this.#s;t.onend=()=>{r>=this.#o.length&&this.clear()},t.onerror=()=>{this.clear()},window.speechSynthesis.speak(t)}}clear(){this.#t.emit("playing",!1)}start(e,t=!1){this.#o=Array.isArray(e)?e:[e],t||(window.speechSynthesis.cancel(),this.#s=0),this.speak()}stop(){window.speechSynthesis.cancel(),this.clear()}onPlaying(e){this.#t.on("playing",e)}onSpeaking(e){this.#t.on("speaking",e)}}const cr=new Map;class dr{#r;#t;#o=[];#a=new Map;#l=!1;#s=0;#c=!1;constructor(e){this.#r=e,this.#t=new K}createLoader(e){const t=new Promise(async t=>{try{const{type:r,data:n}=await this.#r(e),i=new Audio(`data:${r};base64,${n}`);this.#a.set(e,i),t(i)}catch{this.#a.delete(e),t(void 0)}});return this.#a.set(e,t),t}async loadAudioData(){this.#c=!0,this.#t.emit("speaking",!0);let e=0;for(;e<this.#o.length;){const t=this.#o[e];!this.#a.has(t)&&this.#c&&await this.createLoader(t),e++}this.#t.emit("speaking",!1),this.#c=!1}waitForAudioLoaded(e){const t=this.#a.get(e);return t instanceof Promise?t:Promise.resolve(t)}async playAudio(e=0){this.#l=!0;const t=await this.waitForAudioLoaded(this.#o[e]);this.#t.emit("playing",!0),t?(cr.set(this,t),t.onpause=async()=>{t.ended?(this.#s++,this.#s<this.#o.length?await this.playAudio(this.#s):this.clear()):this.clear()},await t.play()):this.clear()}clear(){this.#l=!1,this.#c=!1,this.#t.emit("playing",!1),this.#t.emit("speaking",!1),cr.delete(this)}start(e,t=!1){cr.forEach((e,t)=>{t!==this&&(e.pause(),e.currentTime=0)}),this.#o=Array.isArray(e)?e:[e],t||(this.#s=0),this.#c||this.loadAudioData(),this.#l||this.playAudio()}update(e){this.#o=Array.isArray(e)?e:[e]}stop(){const e=cr.get(this);e&&(e.pause(),e.currentTime=0)}onPlaying(e){this.#t.on("playing",e)}onSpeaking(e){this.#t.on("speaking",e)}}function ur({text:e,loading:t,loader:r}){const n=F(),[i,o]=M(!1),[s,a]=M(!1),l=F(t),c=async(i=!1)=>{n.current||(n.current=r?new dr(r):new lr,n.current.onSpeaking(o),n.current.onPlaying(a));let s="";"string"==typeof e?s=e:e.current&&(s=function(e){const t=e.querySelectorAll("p, h1, h2, h3, h4, h5, h6, li:not(:has(p,h1, h2, h3, h4, h5, h6))");return Array.from(t).flatMap(e=>e.textContent?[e.textContent]:[])}(e.current),t&&s.pop()),s&&0!==s.length&&n.current.start(s,i)};return S(()=>{if(l.current&&"string"!=typeof e&&e.current){if(t){const t={childList:!0,subtree:!0},r=new MutationObserver(function(){c(!0)});return r.observe(e.current,t),c(),()=>{r.disconnect()}}c(!0)}},[t]),S(()=>()=>{n.current&&(n.current.stop(),n.current=void 0)},[]),{playing:s,speaking:i,start:c,stop:()=>{n.current&&n.current.stop()}}}function mr({model:r="builtin",voice:n,loading:i,autoplay:o,contentRef:s,avatarRef:a}){const{request:l}=pe(),c=z(()=>{if("builtin"!==r)return async e=>(await l({url:"/speech",method:"post",data:{model:r,voice:n,input:e}})).audio},[r,n]),{playing:d,speaking:u,start:m,stop:h}=ur({loader:c,text:s,loading:o&&i});let p=null;return a.current&&(u&&(p=se(e(hr,{children:e(O,{size:"sm",variant:"light"})}),a.current)),d&&(p=se(e(hr,{children:e(pr,{className:"bi bi-volume-down-fill text-light"})}),a.current))),t(Et,{disabled:i&&!d,onClick:()=>{d?h():m()},children:[d?e(pr,{className:"bi bi-volume-down-fill"}):u?e(O,{size:"sm",variant:"primary"}):e(pr,{className:"bi bi-volume-down"}),p]})}const hr=n.div`
|
|
703
723
|
position: absolute;
|
|
704
724
|
top: 0;
|
|
705
725
|
left: 0;
|
|
@@ -709,27 +729,12 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
709
729
|
align-items: center;
|
|
710
730
|
justify-content: center;
|
|
711
731
|
background-color: rgba(0, 0, 0, .3);
|
|
712
|
-
`,
|
|
732
|
+
`,pr=n.i`
|
|
713
733
|
|
|
714
734
|
&::before {
|
|
715
735
|
transform: scale(1.4)
|
|
716
736
|
}
|
|
717
|
-
|
|
718
|
-
width: ${e=>e.$open?"auto":"fit-content"};
|
|
719
|
-
max-width: 100%;
|
|
720
|
-
overflow:hidden;
|
|
721
|
-
`,ar=n.div`
|
|
722
|
-
white-space: pre-wrap;
|
|
723
|
-
overflow-y: auto;
|
|
724
|
-
overflow-x: hidden;
|
|
725
|
-
max-height: 150px;
|
|
726
|
-
`;function lr({node:r}){const[n,i]=M(!1),{nodeIconResolver:o}=pe(),s=!("latency"in r),a=s?e(O,{animation:"border",variant:"primary",size:"sm"}):r.error?e("i",{className:"bi bi-x-circle-fill text-danger"}):e("i",{className:"bi bi-check-circle-fill text-success"});return t("div",{className:"shadow-sm rounded bg-white fs-7",children:[t(dr,{onClick:()=>i(e=>!e),role:"button",className:"d-flex align-items-center p-1 px-2 gap-1",children:[e("i",{className:"bi bi-chevron-down text-muted"}),o?.(r.type),e(ur,{className:"me-2 text-truncate",children:r.title}),e("span",{className:"me-auto"}),!s&&e("span",{className:"text-nowrap text-muted",children:cr(r.latency)}),e("span",{children:a})]}),n&&t("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"输入"})}),e("div",{className:"border-top p-2 overflow-hidden text-pre-wrap",children:r.input?JSON.stringify(r.input,null,4):"None"})]}),t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"输出"})}),e("div",{className:"border-top p-2 overflow-hidden text-pre-wrap",children:r.output?JSON.stringify(r.output,null,4):"None"})]})]})]})}const cr=e=>e<1?`${(1e3*e).toFixed(3)} ms`:e>60?`${parseInt(Math.round(e/60).toString())} m ${(e%60).toFixed(3)} s`:`${e.toFixed(3)} s`,dr=n.div`
|
|
727
|
-
svg {
|
|
728
|
-
flex-shrink: 0;
|
|
729
|
-
}
|
|
730
|
-
`,ur=n.span`
|
|
731
|
-
display: inline-block;
|
|
732
|
-
`;function mr({nodes:r,loading:n}){const[i,o]=M(!1),s=r.some(e=>"error"in e),a=n?e(O,{animation:"border",variant:"primary",size:"sm"}):e("i",s?{className:"bi bi-x-circle-fill text-danger"}:{className:"bi bi-check-circle-fill text-success"});return e("div",{className:"mb-2",children:t("div",i?{className:"shadow-sm rounded bg-white fs-7",children:[t("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[a,e("span",{children:"工作流"}),e("i",{className:"bi bi-caret-up-fill text-muted"})]}),e("div",{className:"border-top p-2 d-flex flex-column gap-1 bg-light bg-opacity-50",children:r.map((t,r)=>e(lr,{node:t},r))})]}:{onClick:()=>o(!0),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[a,e("span",{children:"工作流"}),e("i",{className:"bi bi-caret-down-fill text-muted"})]})})}const hr=D(({message:n,actions:i,setMessage:o,cite:s,speech:a,isLast:l})=>{const{query:c,quote:d,files:u=[],chunks:m,annotation:h,loading:p}=n,{user:g,bot:f,logLevel:b="none",messages:v}=pe(),y=z(()=>m?m.reduce(function(e,t){const r=Array.isArray(t.content)?t.content.reduce((e,t)=>{const r=ut(t);return r?e+r+"\n":e},""):ut(t.content);return e+r+"\n"+(t.tools||[]).reduce((e,t)=>{if(t.content){const r=ut(t.content);if(r)return e+r+"\n"}return e},"")},""):"",[m]),x=F(null),k=F(null),N=F(null),j=z(()=>m?.flatMap(e=>e.node?[e.node]:[])||[],[m]),C=c||u.length>0;return t(r,{children:[C&&t(Nr,{$reverse:!0,children:[g&&e(wr,{children:e("img",{src:g.avatar})}),e(kr,{}),t(Er,{ref:x,children:[c&&e(vt,{content:c}),d&&e(Q,{trigger:"click",container:x,rootClose:!0,placement:"bottom",overlay:e(pr,{body:!0,children:d}),children:e(fr,{children:e(gr,{children:d})})}),u.length>0&&u.map(({name:t,size:r,path:n},i)=>E(n)?e(St,{src:`/uploads/${n}`},i):e(vr,{children:e(ve,{name:t,url:`/uploads/${n}`,size:r})},i))]})]}),m&&t(Nr,{children:[f&&e(wr,{ref:k,children:e("img",{src:f.avatar})}),e(kr,{}),t(Er,{children:[C&&!1!==i&&t(yr,{children:[e(w,{placement:"top",content:y,as:Et,tooltip:!0}),a&&e(rr,{loading:n.loading,avatarRef:k,contentRef:N,...a}),i?.({Component:Et,message:n,content:y,setMessage:o,messages:v})]}),e(xr,{children:["stats","all"].includes(b)&&n.stats&&t(r,{children:[t("span",{children:["耗时 ",n.stats.latency/1e3," 秒"]}),t("span",{children:["花费 Token ",n.stats.usage]})]})}),t(br,{ref:N,$deleted:!!h,children:["all"===b&&j.length>0&&e(mr,{loading:p,nodes:j}),m.map((r,n)=>t(A,{children:[r.reasoning&&["stats","all"].includes(b)&&e(Yt,{reasoning:r.reasoning,loading:!r.content&&0===(r.tools||[]).length}),r.content&&e(Ct,{value:{chunkIndex:n,chunk:r,isLastMessage:l},children:e(Xt,{cite:s,value:r.content})}),(r.tools||[]).map((t,i)=>e(Ct,{value:{chunkIndex:n,chunk:r,toolIndex:i,tool:t,isLastMessage:l},children:e(or,{tool:t,loading:p})},i)),r.error&&e(vt,{content:`[${r.error}]`})]},n))]}),h&&t("div",{children:[t("div",{className:"d-flex align-items-center",children:[e("span",{className:"fs-7 text-secondary me-2",children:"标注的答案"}),e("hr",{className:"flex-fill"})]}),e(vt,{content:h.answer})]}),n.loading&&e(dt,{})]})]})]})}),pr=n(J)`
|
|
737
|
+
`,gr=D(({message:n,actions:i,setMessage:o,cite:s,speech:a,isLast:l})=>{const{query:c,quote:d,files:u=[],chunks:m,annotation:h,loading:p}=n,{user:g,bot:f,logLevel:b="none",messages:v}=pe(),y=z(()=>m?m.reduce(function(e,t){const r=Array.isArray(t.content)?t.content.reduce((e,t)=>{const r=ut(t);return r?e+r+"\n":e},""):ut(t.content);return e+r+"\n"+(t.tools||[]).reduce((e,t)=>{if(t.content){const r=ut(t.content);if(r)return e+r+"\n"}return e},"")},""):"",[m]),x=F(null),k=F(null),N=F(null),j=c||u.length>0;return t(r,{children:[j&&t(jr,{$reverse:!0,children:[g&&e(kr,{children:e("img",{src:g.avatar})}),e(Er,{}),t(Nr,{ref:x,children:[c&&e(vt,{content:c}),d&&e(Q,{trigger:"click",container:x,rootClose:!0,placement:"bottom",overlay:e(fr,{body:!0,children:d}),children:e(vr,{children:e(br,{children:d})})}),u.length>0&&u.map(({name:t,size:r,path:n},i)=>E(n)?e(St,{src:`/uploads/${n}`},i):e(yr,{children:e(ve,{name:t,url:`/uploads/${n}`,size:r})},i))]})]}),m&&t(jr,{children:[f&&e(kr,{ref:k,children:e("img",{src:f.avatar})}),e(Er,{}),t(Nr,{children:[j&&!1!==i&&t(xr,{children:[e(w,{placement:"top",content:y,as:Et,tooltip:!0}),a&&e(mr,{loading:n.loading,avatarRef:k,contentRef:N,...a}),i?.({Component:Et,message:n,content:y,setMessage:o,messages:v})]}),e(wr,{children:["stats","all"].includes(b)&&n.stats&&t(r,{children:[t("span",{children:["耗时 ",n.stats.latency/1e3," 秒"]}),t("span",{children:["花费 Token ",n.stats.usage]})]})}),e(sr,{ref:N,chunks:m,loading:p,isLast:l,cite:s,deleted:!!h}),h&&t("div",{children:[t("div",{className:"d-flex align-items-center",children:[e("span",{className:"fs-7 text-secondary me-2",children:"标注的答案"}),e("hr",{className:"flex-fill"})]}),e(vt,{content:h.answer})]}),n.loading&&e(dt,{})]})]})]})}),fr=n(J)`
|
|
733
738
|
max-width: calc(100% - .875rem * 2);
|
|
734
739
|
width: calc(100% - .875rem * 2);
|
|
735
740
|
font-size: 1rem;
|
|
@@ -738,25 +743,20 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
738
743
|
.popover-body {
|
|
739
744
|
padding: .5rem .75rem;
|
|
740
745
|
}
|
|
741
|
-
`,
|
|
746
|
+
`,br=n.div`
|
|
742
747
|
overflow: hidden;
|
|
743
748
|
text-overflow: ellipsis;
|
|
744
749
|
word-break: break-all;
|
|
745
750
|
-webkit-line-clamp: 2;
|
|
746
751
|
-webkit-box-orient: vertical;
|
|
747
752
|
display: -webkit-box;
|
|
748
|
-
`,
|
|
753
|
+
`,vr=n.div`
|
|
749
754
|
padding: .5rem .75rem;
|
|
750
755
|
color: var(--bs-secondary);
|
|
751
756
|
border-radius: var(--bs-border-radius-lg);
|
|
752
757
|
background: #FFFFFF;
|
|
753
758
|
cursor: pointer;
|
|
754
|
-
`,
|
|
755
|
-
${e=>e.$deleted&&i`
|
|
756
|
-
text-decoration-line: line-through;
|
|
757
|
-
color: var(--bs-secondary);
|
|
758
|
-
`}
|
|
759
|
-
`,vr=n.div`
|
|
759
|
+
`,yr=n.div`
|
|
760
760
|
width: 100%;
|
|
761
761
|
margin-bottom: .5rem;
|
|
762
762
|
margin-top: .5rem;
|
|
@@ -768,7 +768,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
768
768
|
&:last-child {
|
|
769
769
|
margin-bottom: 0;
|
|
770
770
|
}
|
|
771
|
-
`,
|
|
771
|
+
`,xr=n.div`
|
|
772
772
|
position: absolute;
|
|
773
773
|
top: -1.1rem;
|
|
774
774
|
right: 0;
|
|
@@ -776,7 +776,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
776
776
|
gap: .5rem;
|
|
777
777
|
opacity: 0;
|
|
778
778
|
z-index: 1;
|
|
779
|
-
`,
|
|
779
|
+
`,wr=n.div`
|
|
780
780
|
position: absolute;
|
|
781
781
|
bottom: -1.4rem;
|
|
782
782
|
left: 0;
|
|
@@ -789,7 +789,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
789
789
|
display: flex;
|
|
790
790
|
opacity: 0;
|
|
791
791
|
z-index: 1;
|
|
792
|
-
`,
|
|
792
|
+
`,kr=n.div`
|
|
793
793
|
width: 35px;
|
|
794
794
|
height: 35px;
|
|
795
795
|
border-radius: 50%;
|
|
@@ -800,14 +800,14 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
800
800
|
width: 100%;
|
|
801
801
|
height: 100%;
|
|
802
802
|
}
|
|
803
|
-
`,
|
|
803
|
+
`,Er=n.div`
|
|
804
804
|
width: 0;
|
|
805
805
|
height: 0;
|
|
806
806
|
content: "";
|
|
807
807
|
border: 5px solid transparent;
|
|
808
808
|
border-right-color: #f3f3f3;
|
|
809
809
|
transform: translateY(10px);
|
|
810
|
-
`,
|
|
810
|
+
`,Nr=n.div`
|
|
811
811
|
padding: .875rem;
|
|
812
812
|
background-color: rgb(243, 243, 243);
|
|
813
813
|
border-radius: var(--bs-border-radius-lg);
|
|
@@ -815,31 +815,31 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
815
815
|
position: relative;
|
|
816
816
|
|
|
817
817
|
&:hover {
|
|
818
|
-
${
|
|
818
|
+
${wr}, ${xr} {
|
|
819
819
|
opacity: 1;
|
|
820
820
|
}
|
|
821
821
|
}
|
|
822
|
-
`,
|
|
822
|
+
`,jr=n.div`
|
|
823
823
|
display: flex;
|
|
824
824
|
justify-content: flex-start;
|
|
825
825
|
padding: 1rem 0 1rem 0;
|
|
826
826
|
${e=>e.$reverse&&i`
|
|
827
827
|
flex-direction: row-reverse;
|
|
828
828
|
|
|
829
|
-
${
|
|
829
|
+
${Er} {
|
|
830
830
|
border-left-color: rgba(var(--bs-primary-rgb), 0.1);
|
|
831
831
|
border-right-color: transparent;
|
|
832
832
|
}
|
|
833
833
|
|
|
834
|
-
${
|
|
834
|
+
${Nr} {
|
|
835
835
|
background: rgba(var(--bs-primary-rgb), 0.1);
|
|
836
836
|
}
|
|
837
837
|
`};
|
|
838
|
-
`;function
|
|
838
|
+
`;function Cr(r){const{placeholder:n,actions:i,cite:o,speech:s,renderItem:a,scrollRef:l}=r,{messages:c,setMessages:d,loading:u,suggestions:m,conversationId:h}=pe(),p=F(r.loaded),g=R(()=>{const e=l.current;e&&requestAnimationFrame(()=>{e.scrollTo({top:e.scrollHeight})})},[]),f=F(null),{height:b}=ae({ref:f});S(()=>{(u||!p.current)&&c.length>0&&(u&&(p.current=!0),g())},[b,u,p,c]),S(()=>{m.length>0&&g()},[m]),S(()=>{p.current=r.loaded},[h]),S(()=>{const e=f.current;if(e){const t=()=>{p.current=!0};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[f]);const v=c.slice(-30);return t($r,{ref:f,children:[0===v.length&&n,v.map((t,r)=>{const n={actions:i,cite:o,speech:s,message:t,setMessage:e=>{d(r=>{const n=r.findIndex(e=>e.id===t.id);-1!==n&&e(r[n])})},isLast:r===v.length-1};return a?e(I,{children:a({Component:gr,props:n})},r):e(gr,{...n},r)}),m.length>0&&e(Sr,{children:m.map((t,r)=>e("a",{href:"#!question",children:t},r))})]})}const $r=n.div`
|
|
839
839
|
display: flex;
|
|
840
840
|
flex-direction: column;
|
|
841
841
|
flex: 1;
|
|
842
|
-
|
|
842
|
+
`,Sr=n.div`
|
|
843
843
|
display: flex;
|
|
844
844
|
flex-direction: column;
|
|
845
845
|
align-items: flex-start;
|
|
@@ -856,16 +856,16 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
856
856
|
background: var(--bs-secondary-bg-subtle);
|
|
857
857
|
}
|
|
858
858
|
}
|
|
859
|
-
`,
|
|
859
|
+
`,Mr=D(q(({className:r,bot:n,user:i,input:o,logLevel:s,imageResolver:a,nodeIconResolver:l,placeholder:c,actions:d,onboarding:u,conversation:m,cite:h,speech:p,renderItem:g,renderFileItem:f,renderToolContent:b,renderReasoning:y,messages:w,request:k,onReset:E,transformMessages:j=it},C)=>{const $=R(e=>{const t=[];if(u&&!1!==u.enable){const e=ot(u.questions||[]);t.push({chunks:[{content:[u.prologue,...e.map(e=>`[${e}](#!question)`)].join("\n"),tools:[]}],loading:u.loading})}return e?t.concat(j(e.messages||[])):w?t.concat(j(w)):t},[u]),[S,q]=M(m?.id),[I,D]=M(),[L,P]=v(()=>$(m)),[T,O]=M(!1),B=F(null),[U,_]=M([]),H=R(e=>{q(e?.id),P($(e)),_([]),O(!1),E?.(e)},[T,$,E]);return A(C,()=>({reset:H}),[H]),e(ge,{request:z(()=>k?"string"==typeof k?N.create({baseURL:k}):k:N,[k]),bot:n,user:i,logLevel:s,imageResolver:a,nodeIconResolver:l,conversationId:S,setConversationId:q,input:I,setInput:D,messages:L,setMessages:P,suggestions:U,setSuggestions:_,loading:T,setLoading:O,renderFileItem:f,reset:H,readonly:!o,renderToolContent:b,renderReasoning:y,children:e(Rr,{className:r,children:t(x,{children:[e(Fr,{ref:B,children:e(Cr,{placeholder:c,cite:h,actions:d,loaded:!o,speech:p,renderItem:g,scrollRef:B})}),o&&e(We,{...o,scrollRef:B})]})})})}),X),Fr=n.div`
|
|
860
860
|
display: flex;
|
|
861
861
|
flex-direction: column;
|
|
862
862
|
flex: 1;
|
|
863
863
|
overflow-y: auto;
|
|
864
864
|
padding: 0 1rem;
|
|
865
865
|
margin-bottom: .5rem;
|
|
866
|
-
`,
|
|
866
|
+
`,Rr=n.div`
|
|
867
867
|
display: flex;
|
|
868
868
|
flex-direction: column;
|
|
869
869
|
height: 100%;
|
|
870
870
|
position: relative;
|
|
871
|
-
`;async function
|
|
871
|
+
`;async function qr(e){const t=await le().use(de).use(ee).use(te).use(re).use(ue).use(ce).process(e);return String(t)}export{Fe as ActionButton,sr as Chunks,Je as EditableInput,ve as FileItem,vt as Markdown,Mr as MessageBox,Ne as VariableForm,me as isToolMessage,qr as md2html,ot as pickQuestions,pe as useMessageContext,ur as useSynthesis};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.9",
|
|
4
4
|
"module": "es/index.js",
|
|
5
5
|
"types": "types/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
},
|
|
64
64
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
65
65
|
"license": "MIT",
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "d1876459b0a20ddd708431c528a466e43f5628ae",
|
|
67
67
|
"scripts": {
|
|
68
68
|
"prebuild": "rimraf es types",
|
|
69
69
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Components } from 'react-markdown';
|
|
2
|
+
import { Chunk } from '../../types';
|
|
3
|
+
export interface ChunksProps {
|
|
4
|
+
chunks: Chunk[];
|
|
5
|
+
loading?: boolean;
|
|
6
|
+
isLast: boolean;
|
|
7
|
+
cite?: Components['cite'];
|
|
8
|
+
deleted?: boolean;
|
|
9
|
+
}
|
|
10
|
+
declare const Chunks: import("react").MemoExoticComponent<import("react").ForwardRefExoticComponent<ChunksProps & import("react").RefAttributes<HTMLDivElement>>>;
|
|
11
|
+
export default Chunks;
|
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export { default as MessageBox, MessageBoxProps } from './components/message-box';
|
|
3
|
+
export { default as Chunks, ChunksProps } from './components/message-box/chunks';
|
|
3
4
|
export { useContext as useMessageContext, ToolRenderOptions, ToolRenderResult } from './components/message-box/context';
|
|
4
5
|
export { default as EditableInput } from './components/message-box/input-box/editable-input';
|
|
5
6
|
export type { EditableInputProps, EditableInputRef } from './components/message-box/input-box/editable-input';
|
package/types/types.d.ts
CHANGED
|
@@ -47,6 +47,8 @@ export interface BaseToolMessage<N extends string = string, C = ContentType> {
|
|
|
47
47
|
response?: string;
|
|
48
48
|
error?: boolean;
|
|
49
49
|
content?: C;
|
|
50
|
+
metadata?: Record<string, any>;
|
|
51
|
+
[key: string]: any;
|
|
50
52
|
}
|
|
51
53
|
export interface ConfiguredToolMessage<N extends string = string, C = ContentType> {
|
|
52
54
|
name: N;
|