@topthink/chat 1.2.3 → 1.2.4
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 +18 -18
- package/package.json +3 -3
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,createGlobalStyle as y,getAbsoluteUrl as x,Lightbox as k,Clipboard as w,Button as E,isImageUrl as N,request as j}from"@topthink/components";import*as $ from"react";import{createContext as C,useContext as M,useEffect as S,useState as q,useRef as R,useCallback as z,forwardRef 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,createGlobalStyle as y,getAbsoluteUrl as x,Lightbox as k,Clipboard as w,Button as E,isImageUrl as N,request as j}from"@topthink/components";import*as $ from"react";import{createContext as C,useContext as M,useEffect as S,useState as q,useRef as R,useCallback as z,forwardRef as F,useMemo as I,useImperativeHandle as A,Fragment as D,memo as L}from"react";import*as T from"path";import P from"path";import{Spinner as O,Badge as _,Dropdown as B,FormControl as U,CloseButton as H,OverlayTrigger as J,Popover as Q}from"react-bootstrap";import W from"eventemitter3";import{Editor as X,Delta as G,SKILL_KEY as K,VARS_KEY as V,SEL_VALUE_KEY as Y,SEL_KEY as Z,EditableInputPlugin as ee,SelectorInputPlugin as te,SkillInputPlugin as re,BlockKit as ne,Editable as ie,VARS_VALUE_KEY as oe}from"@block-kit/variable";import"@block-kit/variable/dist/style/index.css";import se from"lodash/isEqual";import{toString as ae}from"hast-util-to-string";import le from"react-markdown";import"katex/dist/katex.min.css";import ce from"rehype-highlight";import de from"rehype-katex";import ue from"remark-breaks";import me from"remark-gfm";import he from"remark-math";import{codes as pe,types as ge}from"micromark-util-symbol";import{markdownLineEnding as fe}from"micromark-util-character";import{createPortal as be}from"react-dom";import ve from"use-resize-observer";import{unified as ye}from"unified";import xe from"rehype-stringify";import ke from"remark-parse";import we from"remark-rehype";function Ee(e,t){return e.name===t}const Ne=C(null);function je(e){const t=M(Ne);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 $e({children:t,...r}){const[n,i]=q({});return e(Ne.Provider,{value:{...r,handles:n,setHandles:i},children:t})}const Ce=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},Me=function({className:r,name:n,size:i=0,loading:a,error:l,onRemove:c,onClick:d}){const u=P.extname(n).substring(1),m=t(Fe,{className:r,$error:!!l,onClick:d,children:[t(ze,{children:[e(Ce,{ext:u}),a&&e(O,{variant:"primary"})]}),t(Re,{children:[e("h4",{children:n}),i>0&&e("p",{children:o(i,1)})]}),c&&e(qe,{onClick:e=>{e.preventDefault(),c()},children:e("i",{className:"bi bi-trash3"})})]});return l?e(s,{placement:"top",tooltip:l,children:m}):m};function Se(t){const{renderFileItem:r}=je();return r?r({Component:Me,props:t}):e(Me,{...t})}const qe=n.div`
|
|
2
2
|
width: 1.75rem;
|
|
3
3
|
height: 1.75rem;
|
|
4
4
|
align-items: center;
|
|
@@ -48,7 +48,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
48
48
|
position: absolute;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
`,
|
|
51
|
+
`,Fe=n.div`
|
|
52
52
|
display: flex;
|
|
53
53
|
gap: .5rem;
|
|
54
54
|
padding: .5rem;
|
|
@@ -65,7 +65,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
65
65
|
${e=>e.$error&&i`
|
|
66
66
|
border-color: var(--bs-danger) !important;
|
|
67
67
|
`}
|
|
68
|
-
`,
|
|
68
|
+
`,Ie=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"];function Ae(){const{setMessages:e,setSuggestions:t,setLoading:r,conversationId:n,setConversationId:i,request:o,handles:s,messages:a}=je(),d=R(null),u=R(!1),m=R(n),h=()=>(d.current&&d.current.abort(),d.current=new AbortController,d.current.signal),{onSuccess:p,onComplete:g,onEvent:f}=s,b=z(t=>{if(t.data)if("[DONE]"!=t.data)try{const r=JSON.parse(t.data);r.conversation?(m.current=r.conversation,i(r.conversation)):r.suspend?u.current=!0:r.event?f?.(r.event):e(e=>{const t=e[e.length-1];if(r.chunks){if(t.chunks){const e=r.chunks.index;if(t.chunks[e]||(t.chunks[e]={content:"",reasoning:"",tools:[]}),r.chunks.error)t.chunks[e].error=r.chunks.error;else if(r.chunks.tools){t.chunks[e].tools||(t.chunks[e].tools=[]);const n=r.chunks.tools.index;delete r.chunks.tools.index,t.chunks[e].tools[n]&&"arguments"in r.chunks.tools?t.chunks[e].tools[n].arguments+=r.chunks.tools.arguments:t.chunks[e].tools[n]={...t.chunks[e].tools[n],...r.chunks.tools}}else if(r.chunks.reasoning)t.chunks[e].reasoning+=r.chunks.reasoning;else if(r.chunks.content)if("object"==typeof r.chunks.content){Array.isArray(t.chunks[e].content)||(t.chunks[e].content=[]);const n=r.chunks.content.index,i=r.chunks.content.value;"string"==typeof i?(t.chunks[e].content[n]||(t.chunks[e].content[n]=""),t.chunks[e].content[n]+=i):t.chunks[e].content[n]=i}else t.chunks[e].content+=r.chunks.content;else r.chunks.node&&(t.chunks[e].node={...t.chunks[e].node,...r.chunks.node})}}else r.stats?t.stats=r.stats:r.id&&(t.id=r.id)})}catch(e){console.error(e)}else e(e=>{e[e.length-1].loading=!1})},[s]),v=z(t=>{if(l(t))e(e=>{const t=e[e.length-1];t&&(t.loading=!1)});else{let r="未知错误";c(t)&&(r=401==t.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof t.errors?t.errors:Object.values(t.errors).join("\n")),e(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})}},[]),y=z(async()=>{try{const t=h();u.current=!1,r(!0),e(e=>{e[e.length-1].loading=!0,delete e[e.length-1].stats});let n={url:"message",method:"get",params:{conversation:m.current},onMessage:b,signal:t};await o(n),u.current||p?.()}catch(e){v(e)}finally{d.current=null}r(!1),g?.()},[o,s]),x=z(async(n,...i)=>{"string"==typeof n&&(n={query:n,...i});const{query:s,files:a=[],variables:l={},quote:c}=n;if(s||a.length>0){const n=h();u.current=!1,r(!0),t([]),e(e=>{e.push({query:s,quote:c,files:a,variables:l,chunks:[],loading:!0})});try{let e={method:"post",data:{query:s,quote:c,files:a,variables:l,conversation:m.current},onMessage:b,signal:n};await o(e),u.current||p?.()}catch(e){v(e)}finally{d.current=null}r(!1),g?.()}},[o,s]),k=z(async t=>{try{const n=h();u.current=!1,r(!0),e(e=>{e[e.length-1].loading=!0});let i={url:"resume",method:"post",data:{conversation:m.current,chunk:t.chunk,tool:t.tool,payload:t.payload},onMessage:b,signal:n};await o(i),u.current||p?.()}catch(e){v(e)}finally{d.current=null}r(!1),g?.()},[o,s]),w=z(async()=>{try{const e=h();let r={method:"post",url:"/suggestion",data:{conversation:m.current},signal:e};const n=await o(r);t(n)}catch{}finally{d.current=null}},[o]);return S(()=>{const e=a[a.length-1];e&&0==e.chunks?.length&&!e.loading&&y()},[]),S(()=>{if(n!=m.current){m.current=n,d.current&&d.current.abort();const e=a[a.length-1];e&&0==e.chunks?.length&&!e.loading&&y()}},[n]),{send:x,resume:k,suggest:w}}function De({variables:t,values:r,onSubmit:n,onChange:i,children:o}){const s={},a=[],l={};return t.forEach(e=>{s[e.key]={type:"string",title:e.label},"textarea"===e.type?l[e.key]={"ui:widget":"textarea"}:"select"===e.type&&e.options&&(s[e.key].enum=e.options,s[e.key].enumNames=e.options,e.options.length>0&&(s[e.key].default=e.options[0])),e.required&&a.push(e.key)}),e(d,{schema:{type:"object",properties:s,required:a},formData:r,omitExtraData:!0,uiSchema:l,submitText:"开始对话",onSubmit:n,onChange:i,children:o})}const Le=n.button`
|
|
69
69
|
color: var(--bs-secondary);
|
|
70
70
|
cursor: pointer;
|
|
71
71
|
display: flex;
|
|
@@ -88,12 +88,12 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
88
88
|
background-color: var(--bs-gray-200);
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
`;let Te=class{#e;#t;constructor(){this.#t=new W}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)}},Pe=class{#t;#r;#n;#i;constructor(e){this.#r=e,this.#t=new W}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 Oe=
|
|
91
|
+
`;let Te=class{#e;#t;constructor(){this.#t=new W}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)}},Pe=class{#t;#r;#n;#i;constructor(e){this.#r=e,this.#t=new W}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 Oe=F(({onResult:r,model:n="builtin"},i)=>{const{request:o}=je(),a=I(()=>{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=R(),[n,i]=q(!1),[o,s]=q(!1);return S(()=>()=>{r.current&&(r.current.stop(),r.current=void 0)},[]),{recording:n,transcribing:o,start:async()=>{r.current||(r.current=t?new Pe(t):new Te,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(Le,{onMouseDown:e=>{e.preventDefault(),d?c():l()},children:[u&&e(_e,{animation:"border",variant:"primary",size:"sm"}),e("i",d?{className:"bi bi-mic-fill text-danger"}:{className:"bi bi-mic"})]})})}),_e=n(O)`
|
|
92
92
|
position: absolute;
|
|
93
93
|
--bs-spinner-width: .75rem;
|
|
94
94
|
--bs-spinner-height: .75rem;
|
|
95
95
|
--bs-spinner-border-width: 0.1em;
|
|
96
|
-
`,Be=
|
|
96
|
+
`,Be=F(({className:t,children:r,size:n,tooltip:i,placement:o,onClick:a,onMouseDown:l},c)=>{const d=e(Ue,{ref:c,className:t,onClick:a,onMouseDown:l,$size:n,children:r});return i?e(s,{tooltip:i,placement:o,children:d}):d}),Ue=n.div`
|
|
97
97
|
width: 2rem;
|
|
98
98
|
height: 2rem;
|
|
99
99
|
align-items: center;
|
|
@@ -165,7 +165,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
165
165
|
margin-bottom: .5rem;
|
|
166
166
|
display: flex;
|
|
167
167
|
padding: 0 .125rem;
|
|
168
|
-
`,it=function(e){return e.ops.map(e=>e.attributes?.[oe]||e.attributes?.[Y]||e.insert||"").join("")};var ot=
|
|
168
|
+
`,it=function(e){return e.ops.map(e=>e.attributes?.[oe]||e.attributes?.[Y]||e.insert||"").join("")};var ot=F(function({suggestion:r,fileTypes:n,speech:i,scrollRef:o,variables:a,minRows:l=1,maxRows:d=5,toolbar:m,disabled:h,autoFocus:p,placeholder:g="请输入你的问题, Enter+Shift换行, Enter发送",onEvent:f},b){const y=R(null),x=R(null),k=R(),w=R(()=>{}),[E,N]=q(""),[j,$]=q(""),[C,M]=v([]),[F,D]=q(!1),L=n&&n.length>0?n:Ie,{send:P,resume:O,suggest:_}=Ae(),B=I(()=>{const e=new X({schema:{[V]:{void:!0,inline:!0},[Z]:{void:!0,inline:!0},[K]:{void:!0,inline:!0}}});return e.event.on("focus_capture",()=>D(!0)),e.event.on("blur_capture",()=>D(!1)),e.event.on("keydown_capture",t=>{if("Escape"===t.key){const{ops:t}=e.state.toBlock();t.some(e=>"skill-key"in(e.attributes||{}))&&e.state.setContent(new G)}else"Enter"!==t.key||t.shiftKey||(t.preventDefault(),t.stopPropagation(),k.current?.())}),e.event.on("CONTENT_CHANGE",e=>{N(it(e.current).trim())}),e.event.on("paste",e=>{w.current(e)}),e},[]),U=I(()=>({onSuccess(){r&&_()},onComplete(){requestAnimationFrame(()=>{B.selection.focus()})},onEvent:f}),[r,f]),{messages:J,loading:Q,request:W,setInput:ie}=je(U),[oe,se]=q();S(()=>{const e=J.filter(e=>!!e.query);se(e.at(-1)?.variables)},[J]),S(()=>{!n&&C.length>0&&M([])},[n]),S(()=>{const e=o.current;if(e&&P){const t=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const t={query:e.target.text};a&&(t.variables={...oe,...a.values}),P(t)}};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[a,oe]);const ae=z(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=>!(!L.includes("*")&&!L.includes(T.extname(e.name)))&&!(e.size>20971520)).slice(0,6-C.length);return 0!==t.length&&(M(e=>{e.push(...t.map(e=>({file:e})))}),t.forEach(async e=>{try{const t=new FormData;t.set("file",e);const r=await W({method:"POST",url:"/upload",data:t});M(t=>{const n=t.find(t=>t.file===e);n&&(n.path=r.path)})}catch(t){const r=c(t)?t.message:"unknown error";M(t=>{const n=t.find(t=>t.file===e);n&&(n.error=r)})}}),!0)},[C.length,L,W,M]),le=z(({target:e})=>{e.files&&ae(Array.from(e.files)),e.value=""},[ae]),ce=z(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&&ae(n)&&e.preventDefault()},[ae]);S(()=>{w.current=ce},[ce]);const de=C.flatMap(({file:e,path:t})=>t?[{name:e.name,size:e.size,path:t}]:[]),ue=z(()=>{if(E||de.length>0){const e={query:E,files:de};a&&(e.variables={...oe,...a.values}),j&&(e.quote=j),P(e),he(""),$(""),M([]),x.current?.stop(!0)}},[E,de,a,oe,j]);S(()=>{k.current=ue},[ue]);const me=z(e=>{const t={},r={},n=new G;let i=1,o=1,s=0;const a=/\(([^)]+)\)|\[([^\]]+)\]|\{([^}]+)\}/g;let l;for(;null!==(l=a.exec(e));){if(l.index>s){const t=e.substring(s,l.index);t&&n.insert(t)}if(l[1])n.insert(" ",{[K]:l[1].trim()});else if(l[2]){const e=`var-${i}`;t[e]=l[0],n.insert(" ",{[V]:e}),i++}else if(l[3]){const e=l[3].split("|").map(e=>e.trim()),t=`sel-${o}`;r[t]=e,n.insert(" ",{[Z]:t,[Y]:e[0]}),o++}s=a.lastIndex}if(s<e.length){const t=e.substring(s);t&&n.insert(t)}return{delta:n,placeholders:t,selector:r}},[]),he=z(e=>{let t;t="string"==typeof e?e:e(it(B.state.block.toDelta()));const{delta:r,placeholders:n,selector:i}=me(t);B.plugin.register([new ee({placeholders:n}),new te({selector:i}),new re]),B.state.setContent(r),requestAnimationFrame(()=>{B.selection.focus()})},[B,me]),pe=I(()=>({setQuery:he,setQuote:$,send:P,resume:O,editor:B,focus(){B.selection.focus()}}),[he,$,P,O,B]);if(S(()=>{ie(pe)},[pe]),A(b,()=>pe,[pe]),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(oe))return e(at,{children:e(st,{children:e(De,{values:oe,variables:t,onSubmit:({formData:e})=>{if(r(e,!0))return se(e),!0}})})})}return t(gt,{children:[e(ct,{}),m&&e(tt,{children:m}),t(pt,{$focused:F&&!Q,children:[C.length>0&&e(ut,{children:C.map(({file:t,error:r,path:n},i)=>{const o=void 0===r&&void 0===n;return e(lt,{name:t.name,size:t.size,error:r,loading:o,onRemove:()=>{M(e=>{const r=e.findIndex(e=>e.file===t);-1!==r&&e.splice(r,1)})}},i)})}),!!j&&t(ht,{children:[e("i",{className:"bi bi-quote"}),e("p",{children:j}),e(H,{onClick:()=>$("")})]}),t(mt,{children:[e(ne,{editor:B,readonly:Q||h,children:e(dt,{className:"block-kit-editable",autoFocus:p,placeholder:g,$minRows:l,$maxRows:d})}),n&&e(s,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:t(Le,{disabled:C.length>=6,onClick:()=>y.current?.click(),children:[e("input",{onChange:le,multiple:!0,accept:L.join(","),ref:y,type:"file",hidden:!0}),e("i",{className:"bi bi-file-earmark-arrow-up"})]})}),i&&e(Oe,{ref:x,model:i.model,onResult:e=>{he(t=>t+e+" ")}}),e(Le,{className:"text-primary",disabled:!E&&0===de.length,onClick:e=>{e.preventDefault(),ue()},children:e("i",{className:"bi bi-send-fill"})})]})]})]})});const st=n.div`
|
|
169
169
|
border-radius: var(--bs-border-radius-lg);
|
|
170
170
|
box-shadow: var(--bs-box-shadow-sm);
|
|
171
171
|
margin: 1rem;
|
|
@@ -267,7 +267,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
267
267
|
box-shadow: 0.125rem 0.25rem 0.5rem rgba(0, 0, 0, 0.1);
|
|
268
268
|
`,gt=n.div`
|
|
269
269
|
margin: 0 1rem 1rem;
|
|
270
|
-
`;function ft(e){return e.map(e=>({id:e.id,query:e.query,quote:e.quote||void 0,files:e.files||void 0,variables:e.variables,chunks:e.chunks.map(({content:e="",node:t,error:r,tools:n=[],reasoning:i})=>({content:e,reasoning:i,error:r,tools:n,node:t})),annotation:e.annotation,stats:{usage:e.usage,latency:e.latency}}))}function bt(e){if((e=e.filter(e=>!!e.trim())).length>3){const t=[...e];let r=e.length-3;for(;r--;){let e=Math.floor(Math.random()*t.length);t.splice(e,1)}e=t}return e}var vt,yt,xt;function kt(){return kt=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},kt.apply(null,arguments)}const wt=e=>$.createElement("svg",kt({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},e),vt||(vt=$.createElement("circle",{cx:15,cy:15,r:15,fill:"#3c60ff"},$.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),$.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))),yt||(yt=$.createElement("circle",{cx:60,cy:15,r:9,fill:"#3c60ff",fillOpacity:.3},$.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:9,repeatCount:"indefinite",to:9,values:"9;15;9"}),$.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:.5,repeatCount:"indefinite",to:.5,values:".5;1;.5"}))),xt||(xt=$.createElement("circle",{cx:105,cy:15,r:15,fill:"#3c60ff"},$.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),$.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))));function Et(e){if(!e)return"";if("string"==typeof e)return e;if("image"===e.type){return`})`}return""}const Nt=function(e,t,r){function n(t){return t!==pe.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(ge.chunkString,{contentType:"string"}),i(t)):(e.consume(t),n)}function i(n){return n===pe.rightSquareBracket?(e.exit(ge.chunkString),e.exit("citeData"),e.enter("citeMarker"),function(n){if(n!==pe.rightSquareBracket)return r(n);return e.consume(n),e.exit("citeMarker"),e.exit("cite"),t(n)}(n)):fe(n)||n===pe.eof||n>57||n<48?r(n):(e.consume(n),i)}return function(t){return t!==pe.leftSquareBracket?r(t):(e.enter("cite"),e.enter("citeMarker"),n(t))}},jt=function(){const e=this.data();e.micromarkExtensions.push({text:{[pe.leftSquareBracket]:{tokenize:Nt}}}),e.fromMarkdownExtensions.push({enter:{cite:function(e){this.enter({type:"cite",data:{hName:"cite",hChildren:[{type:"text",value:""}]}},e)},citeData:function(){this.buffer()}},exit:{cite:function(e){this.exit(e)},citeData:function(){const e=this.resume();this.stack[this.stack.length-1].data.hChildren[0].value=e}}})},$t=k.Image,Ct={pre:({children:r,node:n})=>{if(!n)return null;const[i,o]=q(!1),s=
|
|
270
|
+
`;function ft(e){return e.map(e=>({id:e.id,query:e.query,quote:e.quote||void 0,files:e.files||void 0,variables:e.variables,chunks:e.chunks.map(({content:e="",node:t,error:r,tools:n=[],reasoning:i})=>({content:e,reasoning:i,error:r,tools:n,node:t})),annotation:e.annotation,stats:{usage:e.usage,latency:e.latency}}))}function bt(e){if((e=e.filter(e=>!!e.trim())).length>3){const t=[...e];let r=e.length-3;for(;r--;){let e=Math.floor(Math.random()*t.length);t.splice(e,1)}e=t}return e}var vt,yt,xt;function kt(){return kt=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},kt.apply(null,arguments)}const wt=e=>$.createElement("svg",kt({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},e),vt||(vt=$.createElement("circle",{cx:15,cy:15,r:15,fill:"#3c60ff"},$.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),$.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))),yt||(yt=$.createElement("circle",{cx:60,cy:15,r:9,fill:"#3c60ff",fillOpacity:.3},$.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:9,repeatCount:"indefinite",to:9,values:"9;15;9"}),$.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:.5,repeatCount:"indefinite",to:.5,values:".5;1;.5"}))),xt||(xt=$.createElement("circle",{cx:105,cy:15,r:15,fill:"#3c60ff"},$.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),$.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))));function Et(e){if(!e)return"";if("string"==typeof e)return e;if("image"===e.type){return`})`}return""}const Nt=function(e,t,r){function n(t){return t!==pe.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(ge.chunkString,{contentType:"string"}),i(t)):(e.consume(t),n)}function i(n){return n===pe.rightSquareBracket?(e.exit(ge.chunkString),e.exit("citeData"),e.enter("citeMarker"),function(n){if(n!==pe.rightSquareBracket)return r(n);return e.consume(n),e.exit("citeMarker"),e.exit("cite"),t(n)}(n)):fe(n)||n===pe.eof||n>57||n<48?r(n):(e.consume(n),i)}return function(t){return t!==pe.leftSquareBracket?r(t):(e.enter("cite"),e.enter("citeMarker"),n(t))}},jt=function(){const e=this.data();e.micromarkExtensions.push({text:{[pe.leftSquareBracket]:{tokenize:Nt}}}),e.fromMarkdownExtensions.push({enter:{cite:function(e){this.enter({type:"cite",data:{hName:"cite",hChildren:[{type:"text",value:""}]}},e)},citeData:function(){this.buffer()}},exit:{cite:function(e){this.exit(e)},citeData:function(){const e=this.resume();this.stack[this.stack.length-1].data.hChildren[0].value=e}}})},$t=k.Image,Ct={pre:({children:r,node:n})=>{if(!n)return null;const[i,o]=q(!1),s=I(()=>{const e=n.children[0];return"element"===(t=e).type&&"code"===t.tagName&&!!e.properties?.className?.includes("language-svg");var t},[n]);S(()=>{s&&o(s)},[s]);const a=I(()=>ae(n),[n]);return t(Ft,{children:[t(zt,{children:[!i&&e(w,{tooltip:!1,as:Rt,content:a}),s&&i&&e(Rt,{onClick:()=>{const e=new Blob([a],{type:"image/svg+xml"}),t=URL.createObjectURL(e),r=document.createElement("a");r.href=t,r.download="image.svg",r.click(),URL.revokeObjectURL(t)},children:"下载"}),s&&e(Rt,{onClick:()=>o(!i),children:i?"源码":"预览"})]}),s&&i?e("div",{dangerouslySetInnerHTML:{__html:a}}):e("pre",{children:r})]})},a:({node:t,href:r,...n})=>{const i=r?.startsWith("http")?"_blank":"_self";return e("a",{...n,href:r,target:i})},img:({node:t,...r})=>e($t,{...r})},Mt=[[he,{singleDollarTextMath:!1}],me,ue],St=[de,[ce,{detect:!1,ignoreMissing:!0}]],qt=L(F(({content:t,components:r,cite:n},i)=>{t=t.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$");const o=I(()=>{const e={...Ct,...r};return n&&(e.cite=n),e},[r,n]),s=I(()=>n?[...Mt,jt]:Mt,[n]);return e(It,{ref:i,children:e(le,{remarkPlugins:s,rehypePlugins:St,components:o,children:t})})}),se),Rt=n.div`
|
|
271
271
|
cursor: pointer;
|
|
272
272
|
padding: 2px 10px;
|
|
273
273
|
background-color: #303030;
|
|
@@ -291,7 +291,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
291
291
|
&:hover {
|
|
292
292
|
opacity: 1;
|
|
293
293
|
}
|
|
294
|
-
`,
|
|
294
|
+
`,Ft=n.div`
|
|
295
295
|
position: relative;
|
|
296
296
|
|
|
297
297
|
&:hover {
|
|
@@ -301,7 +301,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
301
301
|
opacity: 0.7;
|
|
302
302
|
}
|
|
303
303
|
}
|
|
304
|
-
`,
|
|
304
|
+
`,It=n.div`
|
|
305
305
|
-ms-text-size-adjust: 100%;
|
|
306
306
|
-webkit-text-size-adjust: 100%;
|
|
307
307
|
line-height: 1.8;
|
|
@@ -571,7 +571,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
571
571
|
.hljs-strong {
|
|
572
572
|
font-weight: 700;
|
|
573
573
|
}
|
|
574
|
-
`;var At=
|
|
574
|
+
`;var At=F(function({children:t,tooltip:r,onClick:n,disabled:i,...o},a){i&&(n=void 0);const l=e(Dt,{ref:a,onClick:n,$disabled:i,...o,children:t});return r?e(s,{tooltip:r,placement:"top",children:l}):l});const Dt=n.div`
|
|
575
575
|
align-items: center;
|
|
576
576
|
justify-content: center;
|
|
577
577
|
border-radius: .375rem;
|
|
@@ -604,7 +604,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
604
604
|
`}
|
|
605
605
|
|
|
606
606
|
}
|
|
607
|
-
`,Lt=C(null);var Tt=Lt.Provider;const Pt=k.Image;function Ot({src:t,...r}){const{imageResolver:n}=je();return n&&t&&(t=n(t)),e(_t,{children:e(Pt,{src:t,...r})})}const _t=n.div`
|
|
607
|
+
`,Lt=C(null);var Tt=Lt.Provider;const Pt=k.Image;function Ot({src:t,...r}){const{imageResolver:n}=je();return n&&t&&(t=n(t)),e(_t,{children:e(Pt,{src:t,...r})},t)}const _t=n.div`
|
|
608
608
|
margin-bottom: .5rem;
|
|
609
609
|
margin-top: .5rem;
|
|
610
610
|
border: var(--bs-border-width) var(--bs-border-style) var(--bs-border-color);
|
|
@@ -664,7 +664,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
664
664
|
&:active {
|
|
665
665
|
transform: ${e=>e.$disabled?"none":"scale(0.98)"};
|
|
666
666
|
}
|
|
667
|
-
`;function Gt({description:r,fields:n}){const{resume:i,disabled:o,response:s}=Ut(),[a,l]=q(void 0!==s),c=o||a,{schema:u,uiSchema:m}=
|
|
667
|
+
`;function Gt({description:r,fields:n}){const{resume:i,disabled:o,response:s}=Ut(),[a,l]=q(void 0!==s),c=o||a,{schema:u,uiSchema:m}=I(()=>{const e={},t=[],r={};for(const i of n)e[i.name]={type:"number"===i.type?"number":"string",title:i.label},i.placeholder&&(r[i.name]={"ui:placeholder":i.placeholder}),"textarea"===i.type?r[i.name]={...r[i.name],"ui:widget":"textarea"}:"select"!==i.type&&"radio"!==i.type||!i.options?"checkbox"===i.type&&i.options?(e[i.name].type="array",e[i.name].uniqueItems=!0,e[i.name].items={type:"string",enum:i.options},r[i.name]={...r[i.name],"ui:widget":"checkboxes","ui:enumNames":i.options,"ui:inline":!0}):"email"===i.type?e[i.name].format="email":"date"===i.type?e[i.name].format="date":"tel"===i.type&&(r[i.name]={...r[i.name],"ui:options":{inputType:"tel"}}):(r[i.name]={...r[i.name],"ui:widget":i.type},e[i.name].enum=i.options,e[i.name].enumNames=i.options),i.required&&t.push(i.name);return{schema:{type:"object",properties:e,required:t},uiSchema:r}},[n]);return t(Vt,{className:"rounded bg-white shadow-sm",children:[r&&e(Yt,{children:r}),e(d,{schema:u,uiSchema:m,formData:s,onSubmit:({formData:e})=>{c||(l(!0),i(e))},disabled:c,validate:!0,showErrorList:!1,submitText:a?"已提交":"提交",omitExtraData:!0,children:({submit:t})=>e(Kt,{children:t})})]})}const Kt=n.div`
|
|
668
668
|
display: flex;
|
|
669
669
|
justify-content: flex-end;
|
|
670
670
|
border-top: 1px solid var(--bs-border-color);
|
|
@@ -719,7 +719,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
719
719
|
button {
|
|
720
720
|
padding: .25rem 1.5rem;
|
|
721
721
|
}
|
|
722
|
-
`;function nr({suspend:t}){const{readonly:r}=je(),{chunkIndex:n,chunk:i,toolIndex:o,tool:s,isLastMessage:a}=function(){const e=M(Lt);if(!e)throw new Error("useContext must be used within a Provider");return e}(),{resume:l}=Ae(),c=z(e=>{l({chunk:n,tool:o,payload:e})},[]);let d=null;switch(t.type){case"select":d=e(Jt,{...t.select});break;case"form":d=e(Gt,{...t.form});break;case"confirm":d=e(Zt,{...t.confirm})}const u=s?.response||i.node?.output;return e(Ht,{resume:c,disabled:r||!!i.error||!a,response:"(Empty)"===u?void 0:u,children:d})}function ir({value:t,cite:n}){const i=t=>"string"==typeof t?e(qt,{content:t,cite:n}):"image"===t.type?e(Ot,{src:t.image}):"suspend"===t.type?e(nr,{suspend:t.suspend}):null;return Array.isArray(t)?e(r,{children:t.map((t,r)=>e(D,{children:i(t)},r))}):i(t)}const or=({reasoning:r,loading:n})=>{const[i,o]=q(!1);return S(()=>{n||o(!1)},[n]),e(D,{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(qt,{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"})]})})})},sr=L(t=>{const{renderReasoning:r}=je();return r?r({Component:or,props:t}):e(or,{...t})});class ar{#t;#o=[];#s=0;constructor(){this.#t=new W}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 lr=new Map;class cr{#r;#t;#o=[];#a=new Map;#l=!1;#s=0;#c=!1;constructor(e){this.#r=e,this.#t=new W}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?(lr.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),lr.delete(this)}start(e,t=!1){lr.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=lr.get(this);e&&(e.pause(),e.currentTime=0)}onPlaying(e){this.#t.on("playing",e)}onSpeaking(e){this.#t.on("speaking",e)}}function dr({text:e,loading:t,loader:r}){const n=R(),[i,o]=q(!1),[s,a]=q(!1),l=R(t),c=async(i=!1)=>{n.current||(n.current=r?new cr(r):new ar,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 ur({model:r="builtin",voice:n,loading:i,autoplay:o,contentRef:s,avatarRef:a}){const{request:l}=je(),c=
|
|
722
|
+
`;function nr({suspend:t}){const{readonly:r}=je(),{chunkIndex:n,chunk:i,toolIndex:o,tool:s,isLastMessage:a}=function(){const e=M(Lt);if(!e)throw new Error("useContext must be used within a Provider");return e}(),{resume:l}=Ae(),c=z(e=>{l({chunk:n,tool:o,payload:e})},[]);let d=null;switch(t.type){case"select":d=e(Jt,{...t.select});break;case"form":d=e(Gt,{...t.form});break;case"confirm":d=e(Zt,{...t.confirm})}const u=s?.response||i.node?.output;return e(Ht,{resume:c,disabled:r||!!i.error||!a,response:"(Empty)"===u?void 0:u,children:d})}function ir({value:t,cite:n}){const i=t=>"string"==typeof t?e(qt,{content:t,cite:n}):"image"===t.type?e(Ot,{src:t.image}):"suspend"===t.type?e(nr,{suspend:t.suspend}):null;return Array.isArray(t)?e(r,{children:t.map((t,r)=>e(D,{children:i(t)},r))}):i(t)}const or=({reasoning:r,loading:n})=>{const[i,o]=q(!1);return S(()=>{n||o(!1)},[n]),e(D,{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(qt,{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"})]})})})},sr=L(t=>{const{renderReasoning:r}=je();return r?r({Component:or,props:t}):e(or,{...t})});class ar{#t;#o=[];#s=0;constructor(){this.#t=new W}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 lr=new Map;class cr{#r;#t;#o=[];#a=new Map;#l=!1;#s=0;#c=!1;constructor(e){this.#r=e,this.#t=new W}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?(lr.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),lr.delete(this)}start(e,t=!1){lr.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=lr.get(this);e&&(e.pause(),e.currentTime=0)}onPlaying(e){this.#t.on("playing",e)}onSpeaking(e){this.#t.on("speaking",e)}}function dr({text:e,loading:t,loader:r}){const n=R(),[i,o]=q(!1),[s,a]=q(!1),l=R(t),c=async(i=!1)=>{n.current||(n.current=r?new cr(r):new ar,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 ur({model:r="builtin",voice:n,loading:i,autoplay:o,contentRef:s,avatarRef:a}){const{request:l}=je(),c=I(()=>{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}=dr({loader:c,text:s,loading:o&&i});let p=null;return a.current&&(u&&(p=be(e(mr,{children:e(O,{size:"sm",variant:"light"})}),a.current)),d&&(p=be(e(mr,{children:e(hr,{className:"bi bi-volume-down-fill text-light"})}),a.current))),t(At,{disabled:i&&!d,onClick:()=>{d?h():m()},children:[d?e(hr,{className:"bi bi-volume-down-fill"}):u?e(O,{size:"sm",variant:"primary"}):e(hr,{className:"bi bi-volume-down"}),p]})}const mr=n.div`
|
|
723
723
|
position: absolute;
|
|
724
724
|
top: 0;
|
|
725
725
|
left: 0;
|
|
@@ -734,7 +734,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
734
734
|
&::before {
|
|
735
735
|
transform: scale(1.4)
|
|
736
736
|
}
|
|
737
|
-
`;function pr({tool:r,loading:n}){const{logLevel:i="none",renderToolContent:o}=je(),s="response"in r,a=!s&&!n,{response:l,title:c,content:d,showStatus:u,defaultExpanded:m}=
|
|
737
|
+
`;function pr({tool:r,loading:n}){const{logLevel:i="none",renderToolContent:o}=je(),s="response"in r,a=!s&&!n,{response:l,title:c,content:d,showStatus:u,defaultExpanded:m}=I(()=>{const n=(e=>{try{return JSON.parse(e.arguments)}catch(e){return null}})(r),i=e("span",{className:"text-truncate",children:r.title}),a=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(fr,{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(fr,{className:"border-top p-2",children:r.response||"None"})]})]}),l=r.content&&e(ir,{value:r.content});if(o){const e=o({tool:r,args:n});if(e)return{title:null===e.title?null:e.title??i,response:null===e.response?null:e.response??a,content:null===e.content?null:e.content??l,showStatus:e.showStatus??!0,defaultExpanded:e.defaultExpanded??!1}}return{title:i,response:a,content:l,showStatus:!0,defaultExpanded:!1}},[r,o]),[h,p]=q(m),g="all"==i&&null!==l;return t(D,{children:[c&&e("div",{className:"mb-2",children:t(gr,{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 gr=n.div`
|
|
738
738
|
width: ${e=>e.$open?"auto":"fit-content"};
|
|
739
739
|
max-width: 100%;
|
|
740
740
|
overflow:hidden;
|
|
@@ -749,7 +749,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
749
749
|
}
|
|
750
750
|
`,xr=n.span`
|
|
751
751
|
display: inline-block;
|
|
752
|
-
`;function kr({nodes:r,loading:n}){const[i,o]=q(!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(br,{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 wr=L(({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}=je(),y=
|
|
752
|
+
`;function kr({nodes:r,loading:n}){const[i,o]=q(!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(br,{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 wr=L(({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}=je(),y=I(()=>m?m.reduce(function(e,t){const r=Array.isArray(t.content)?t.content.reduce((e,t)=>{const r=Et(t);return r?e+r+"\n":e},""):Et(t.content);return e+r+"\n"+(t.tools||[]).reduce((e,t)=>{if(t.content){const r=Et(t.content);if(r)return e+r+"\n"}return e},"")},""):"",[m]),x=R(null),k=R(null),E=R(null),j=I(()=>m?.flatMap(e=>e.node?[e.node]:[])||[],[m]),$=c||u.length>0;return t(r,{children:[$&&t(Fr,{$reverse:!0,children:[g&&e(qr,{children:e("img",{src:g.avatar})}),e(Rr,{}),t(zr,{ref:x,children:[c&&e(qt,{content:c}),d&&e(J,{trigger:"click",container:x,rootClose:!0,placement:"bottom",overlay:e(Er,{body:!0,children:d}),children:e(jr,{children:e(Nr,{children:d})})}),u.length>0&&u.map(({name:t,size:r,path:n},i)=>N(n)?e(Ot,{src:`/uploads/${n}`},i):e(Cr,{children:e(Se,{name:t,url:`/uploads/${n}`,size:r})},i))]})]}),m&&t(Fr,{children:[f&&e(qr,{ref:k,children:e("img",{src:f.avatar})}),e(Rr,{}),t(zr,{children:[$&&!1!==i&&t(Mr,{children:[e(w,{placement:"top",content:y,as:At,tooltip:!0}),a&&e(ur,{loading:n.loading,avatarRef:k,contentRef:E,...a}),i?.({Component:At,message:n,content:y,setMessage:o,messages:v})]}),e(Sr,{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($r,{ref:E,$deleted:!!h,children:["all"===b&&j.length>0&&e(kr,{loading:p,nodes:j}),m.map((r,n)=>t(D,{children:[r.reasoning&&["stats","all"].includes(b)&&e(sr,{reasoning:r.reasoning,loading:!r.content&&0===(r.tools||[]).length}),r.content&&e(Tt,{value:{chunkIndex:n,chunk:r,isLastMessage:l},children:e(ir,{cite:s,value:r.content})}),(r.tools||[]).map((t,i)=>e(Tt,{value:{chunkIndex:n,chunk:r,toolIndex:i,tool:t,isLastMessage:l},children:e(pr,{tool:t,loading:p})},i)),r.error&&e(qt,{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(qt,{content:h.answer})]}),n.loading&&e(wt,{})]})]})]})}),Er=n(Q)`
|
|
753
753
|
max-width: calc(100% - .875rem * 2);
|
|
754
754
|
width: calc(100% - .875rem * 2);
|
|
755
755
|
font-size: 1rem;
|
|
@@ -839,7 +839,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
839
839
|
opacity: 1;
|
|
840
840
|
}
|
|
841
841
|
}
|
|
842
|
-
`,
|
|
842
|
+
`,Fr=n.div`
|
|
843
843
|
display: flex;
|
|
844
844
|
justify-content: flex-start;
|
|
845
845
|
padding: 1rem 0 1rem 0;
|
|
@@ -855,7 +855,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
855
855
|
background: rgba(var(--bs-primary-rgb), 0.1);
|
|
856
856
|
}
|
|
857
857
|
`};
|
|
858
|
-
`;function
|
|
858
|
+
`;function Ir(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}=je(),p=R(r.loaded),g=z(()=>{const e=l.current;e&&requestAnimationFrame(()=>{e.scrollTo({top:e.scrollHeight})})},[]),f=R(null),{height:b}=ve({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(Ar,{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?a({Component:wr,props:n},r):e(wr,{...n},r)}),m.length>0&&e(Dr,{children:m.map((t,r)=>e("a",{href:"#!question",children:t},r))})]})}const Ar=n.div`
|
|
859
859
|
display: flex;
|
|
860
860
|
flex-direction: column;
|
|
861
861
|
flex: 1;
|
|
@@ -876,7 +876,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
876
876
|
background: var(--bs-secondary-bg-subtle);
|
|
877
877
|
}
|
|
878
878
|
}
|
|
879
|
-
`,Lr=L(
|
|
879
|
+
`,Lr=L(F(({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:x,request:w,onReset:E,transformMessages:N=ft},$)=>{const C=z(e=>{const t=[];if(u&&!1!==u.enable){const e=bt(u.questions||[]);t.push({chunks:[{content:[u.prologue,...e.map(e=>`[${e}](#!question)`)].join("\n"),tools:[]}],loading:u.loading})}return e?t.concat(N(e.messages||[])):x?t.concat(N(x)):t},[u]),[M,F]=q(m?.id),[D,L]=q(),[T,P]=v([]);S(()=>{P(C(m))},[u]);const[O,_]=q(!1),B=R(null),[U,H]=q([]),J=z(e=>{F(e?.id),P(C(e)),H([]),_(!1),E?.(e)},[O,C,E]);return A($,()=>({reset:J}),[J]),e($e,{request:I(()=>w?"string"==typeof w?j.create({baseURL:w}):w:j,[w]),bot:n,user:i,logLevel:s,imageResolver:a,nodeIconResolver:l,conversationId:M,setConversationId:F,input:D,setInput:L,messages:T,setMessages:P,suggestions:U,setSuggestions:H,loading:O,setLoading:_,renderFileItem:f,reset:J,readonly:!o,renderToolContent:b,renderReasoning:y,children:e(Pr,{className:r,children:t(k,{children:[e(Tr,{ref:B,children:e(Ir,{placeholder:c,cite:h,actions:d,loaded:!o,speech:p,renderItem:g,scrollRef:B})}),o&&e(ot,{...o,scrollRef:B})]})})})}),se),Tr=n.div`
|
|
880
880
|
display: flex;
|
|
881
881
|
flex-direction: column;
|
|
882
882
|
flex: 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"module": "es/index.js",
|
|
5
5
|
"types": "types/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"@block-kit/core": "^1.0.22",
|
|
14
14
|
"@block-kit/variable": "^1.0.21",
|
|
15
15
|
"@breezystack/lamejs": "^1.2.7",
|
|
16
|
-
"@topthink/components": "1.2.
|
|
16
|
+
"@topthink/components": "1.2.3",
|
|
17
17
|
"@types/hast": "^3.0.4",
|
|
18
18
|
"@types/mdast": "^4.0.4",
|
|
19
19
|
"eventemitter3": "^5.0.1",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
},
|
|
65
65
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
66
66
|
"license": "MIT",
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "6add4cb134859a69668da4dc736941afa43ebe3c",
|
|
68
68
|
"scripts": {
|
|
69
69
|
"prebuild": "rimraf es types",
|
|
70
70
|
"build": "rollup -c --environment NODE_ENV:production",
|