@topthink/chat 1.1.39 → 1.1.40
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{jsxs as e,jsx 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,isRequestError as l,Form as c,Toast as d,InfiniteScroller as h,Offcanvas as u,dayjs as m,RequestButton as p,useImmer as g,getAbsoluteUrl as b,Lightbox as f,Clipboard as v,isImageUrl as x,request as y}from"@topthink/components";import*as w from"react";import{createContext as k,useContext as
|
|
1
|
+
import{jsxs as e,jsx 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,isRequestError as l,Form as c,Toast as d,InfiniteScroller as h,Offcanvas as u,dayjs as m,RequestButton as p,useImmer as g,getAbsoluteUrl as b,Lightbox as f,Clipboard as v,isImageUrl as x,request as y}from"@topthink/components";import*as w from"react";import{createContext as k,useContext as E,useEffect as N,useState as j,useRef as $,useCallback as C,forwardRef as M,useMemo as q,useImperativeHandle as z,Fragment as S,memo as F}from"react";import R from"react-textarea-autosize";import*as A from"path";import I from"path";import{Spinner as D,Badge as L,CloseButton as P,OverlayTrigger as T,Popover as O}from"react-bootstrap";import B from"eventemitter3";import _ from"lodash/isEqual";import{toString as U}from"hast-util-to-string";import H from"react-markdown";import"katex/dist/katex.min.css";import J from"rehype-highlight";import K from"rehype-katex";import Q from"remark-breaks";import W from"remark-gfm";import X from"remark-math";import{codes as V,types as Y}from"micromark-util-symbol";import{markdownLineEnding as G}from"micromark-util-character";import{createPortal as Z}from"react-dom";import ee from"use-resize-observer";const te=function({ext:e}){const{result:r}=a(async e=>(await function(e){switch(e){case"../../images/file/docx.svg":return import("./docx-2LwnwWwv.js");case"../../images/file/jpg.svg":return import("./jpg-muicsKjX.js");case"../../images/file/md.svg":return import("./md-DRS7QmE8.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/txt.svg":return import("./txt-CsqmEKjF.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,[e]);return r?t("img",{width:30,height:30,src:r}):null};function re({className:r,name:n,size:i,loading:a,error:l,onRemove:c}){const d=I.extname(n).substring(1),h=e(se,{className:r,$error:!!l,children:[e(oe,{children:[t(te,{ext:d}),a&&t(D,{variant:"primary"})]}),e(ie,{children:[t("h4",{children:n}),t("p",{children:o(i,1)})]}),c&&t(ne,{onClick:e=>{e.preventDefault(),c()},children:t("i",{className:"bi bi-trash3"})})]});return l?t(s,{placement:"top",tooltip:l,children:h}):h}const ne=n.div`
|
|
2
2
|
width: 1.75rem;
|
|
3
3
|
height: 1.75rem;
|
|
4
4
|
align-items: center;
|
|
@@ -64,7 +64,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
64
64
|
${e=>e.$error&&i`
|
|
65
65
|
border-color: var(--bs-danger) !important;
|
|
66
66
|
`}
|
|
67
|
-
`,ae=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"],le=k(null);function ce(e){const t=
|
|
67
|
+
`,ae=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"],le=k(null);function ce(e){const t=E(le);if(!t)throw new Error("useContext must be used within a Provider");const{setHandles:r}=t;return N(()=>{if(e)return r(e),()=>{r({})}},[e]),t}function de({children:e,...r}){const[n,i]=j({});return t(le.Provider,{value:{...r,handles:n,setHandles:i},children:e})}function he(){const{setMessages:e,setSuggestions:t,setLoading:r,conversationId:n,setConversationId:i,request:o,handles:s}=ce(),a=$(null),c=$(!1),d=$(n);N(()=>{d.current=n},[n]);const{onSuccess:h,onComplete:u,onEvent:m}=s,p=C(t=>{if(t.data)if("[DONE]"!=t.data)try{const r=JSON.parse(t.data);r.conversation?(d.current=r.conversation,i(r.conversation)):r.suspend?c.current=!0:r.event?m?.(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})},[]),g=C(t=>{let r="未知错误";l(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})},[]);return{send:C(async(n,...i)=>{"string"==typeof n&&(n={query:n,...i});const{query:s,files:l=[],variables:m={},quote:b}=n;if(s||l.length>0){a.current&&a.current.abort(),c.current=!1,r(!0),t([]),e(e=>{e.push({query:s,quote:b,files:l,variables:m,chunks:[],loading:!0})});try{let e={method:"post",data:{query:s,quote:b,files:l,variables:m,conversation:d.current},onMessage:p};await o(e),c.current||h?.()}catch(e){g(e)}r(!1),u?.()}},[o,s]),resume:C(async t=>{try{a.current&&a.current.abort(),c.current=!1,r(!0),e(e=>{e[e.length-1].loading=!0});let n={url:"resume",method:"post",data:{conversation:d.current,chunk:t.chunk,payload:t.payload},onMessage:p};await o(n),c.current||h?.()}catch(e){g(e)}r(!1),u?.()},[o,s]),suggest:C(async()=>{a.current=new AbortController;try{let e={method:"post",url:"/suggestion",data:{conversation:d.current},signal:a.current.signal};const r=await o(e);t(r)}catch{}finally{a.current=null}},[o])}}function ue({variables:e,values:r,onSubmit:n,onChange:i,children:o}){const s={},a=[],l={};return e.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)}),t(c,{schema:{type:"object",properties:s,required:a},formData:r,omitExtraData:!0,uiSchema:l,submitText:"开始对话",onSubmit:n,onChange:i,children:o})}const me=n.button`
|
|
68
68
|
color: var(--bs-secondary);
|
|
69
69
|
cursor: pointer;
|
|
70
70
|
display: flex;
|
|
@@ -87,7 +87,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
87
87
|
background-color: var(--bs-gray-200);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
`;let pe=class{#e;#t;constructor(){this.#t=new B}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 d.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),d.error(e.error)},this.#e=t}this.#t.emit("recording",!0),this.#e.start()}stop(){this.#e?.stop(),this.#t.emit("recording",!1)}},ge=class{#t;#r;#n;#i;constructor(e){this.#r=e,this.#t=new B}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){d.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 be=M(({onResult:r,model:n="builtin"},i)=>{const{request:o}=ce(),a=q(()=>{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:h}=function({onResult:e,loader:t}){const r=$(),[n,i]=j(!1),[o,s]=j(!1);return
|
|
90
|
+
`;let pe=class{#e;#t;constructor(){this.#t=new B}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 d.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),d.error(e.error)},this.#e=t}this.#t.emit("recording",!0),this.#e.start()}stop(){this.#e?.stop(),this.#t.emit("recording",!1)}},ge=class{#t;#r;#n;#i;constructor(e){this.#r=e,this.#t=new B}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){d.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 be=M(({onResult:r,model:n="builtin"},i)=>{const{request:o}=ce(),a=q(()=>{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:h}=function({onResult:e,loader:t}){const r=$(),[n,i]=j(!1),[o,s]=j(!1);return N(()=>()=>{r.current&&(r.current.stop(),r.current=void 0)},[]),{recording:n,transcribing:o,start:async()=>{r.current||(r.current=t?new ge(t):new pe,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 z(i,()=>({start:l,stop:c}),[l,c]),t(s,{tooltip:d?"停止":"语音输入",placement:"top",children:e(me,{onMouseDown:e=>{e.preventDefault(),d?c():l()},children:[h&&t(fe,{animation:"border",variant:"primary",size:"sm"}),t("i",d?{className:"bi bi-mic-fill text-danger"}:{className:"bi bi-mic"})]})})}),fe=n(D)`
|
|
91
91
|
position: absolute;
|
|
92
92
|
--bs-spinner-width: .75rem;
|
|
93
93
|
--bs-spinner-height: .75rem;
|
|
@@ -116,7 +116,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
116
116
|
&:hover {
|
|
117
117
|
background-color: var(--bs-secondary-bg-subtle);
|
|
118
118
|
}
|
|
119
|
-
`;function ye(){const{request:n,reset:i,conversationId:o}=ce(),[a,l]=j(!1);return e(r,{children:[t(ve,{tooltip:"历史记录",placement:"top",onClick:()=>l(!0),children:t("i",{className:"bi bi-clock-history"})}),t(u,{show:a,onHide:()=>{l(!1)},placement:"end",header:"聊天历史记录",bodyAs:we,children:a&&t($e,{children:t(je,{useWindow:!1,source:e=>n({url:"/conversation",params:e}),render:({data:r,loading:a,setData:c})=>r.length||a?r.map((r,a)=>{const d=r.messages?.reduce((e,t)=>e+t.chunks.reduce((e,t)=>e+(t.content||""),""),"");return e(S,{children:[a>0&&t("hr",{className:"mx-2 my-2"}),e(
|
|
119
|
+
`;function ye(){const{request:n,reset:i,conversationId:o}=ce(),[a,l]=j(!1);return e(r,{children:[t(ve,{tooltip:"历史记录",placement:"top",onClick:()=>l(!0),children:t("i",{className:"bi bi-clock-history"})}),t(u,{show:a,onHide:()=>{l(!1)},placement:"end",header:"聊天历史记录",bodyAs:we,children:a&&t($e,{children:t(je,{useWindow:!1,source:e=>n({url:"/conversation",params:e}),render:({data:r,loading:a,setData:c})=>r.length||a?r.map((r,a)=>{const d=r.messages?.reduce((e,t)=>e+t.chunks.reduce((e,t)=>e+(t.content||""),""),"");return e(S,{children:[a>0&&t("hr",{className:"mx-2 my-2"}),e(Ne,{onClick:()=>{i(r),l(!1)},children:[e(Ee,{children:[o===r.id&&t(L,{bg:"secondary",children:"当前"}),t("span",{className:"fw-bold overflow-hidden text-truncate me-auto",children:r.title||"未命名对话"}),t("span",{className:"text-body text-opacity-50 flex-shrink-0 ms-5",children:m(r.update_time).fromNow()})]}),e(ke,{children:[t("span",{className:"overflow-hidden text-truncate me-auto",children:d||" "}),t("span",{className:"ms-3",children:o!==r.id&&t(s,{tooltip:"删除",children:t(p,{as:"a",confirm:"确定要删除吗?",onRequest:()=>n({url:`/conversation/${r.id}`,method:"DELETE"}),onSuccess:()=>{c(e=>{const t=e.findIndex(e=>e.id===r.id);-1!==t&&e.splice(t,1)})},children:t("i",{className:"bi bi-trash3"})})})})]})]})]},r.id)}):t("div",{className:"text-muted text-center py-5",children:"暂无对话记录"})})})})]})}const we=n.div`
|
|
120
120
|
padding: 0;
|
|
121
121
|
overflow: hidden;
|
|
122
122
|
`,ke=n.div`
|
|
@@ -129,11 +129,11 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
129
129
|
& :last-child {
|
|
130
130
|
display: none;
|
|
131
131
|
}
|
|
132
|
-
`,
|
|
132
|
+
`,Ee=n.div`
|
|
133
133
|
display: flex;
|
|
134
134
|
align-items: center;
|
|
135
135
|
gap: .25rem;
|
|
136
|
-
`,
|
|
136
|
+
`,Ne=n.div`
|
|
137
137
|
gap: .5rem;
|
|
138
138
|
display: flex;
|
|
139
139
|
flex-direction: column;
|
|
@@ -166,7 +166,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
166
166
|
margin-bottom: .5rem;
|
|
167
167
|
display: flex;
|
|
168
168
|
padding: 0 .125rem;
|
|
169
|
-
`;var Ae=M(function({suggestion:r,fileTypes:n,speech:i,scrollRef:o,variables:a,minRows:c,toolbar:h,disabled:u,autoFocus:m,placeholder:p="请输入你的问题, Enter+Shift换行, Enter发送"},
|
|
169
|
+
`;var Ae=M(function({suggestion:r,fileTypes:n,speech:i,scrollRef:o,variables:a,minRows:c,toolbar:h,disabled:u,autoFocus:m,placeholder:p="请输入你的问题, Enter+Shift换行, Enter发送",onEvent:b},f){const v=$(null),x=$(null),y=$(null),[w,k]=j(""),[E,M]=j(""),[S,F]=g([]),[I,D]=j(!1),L=n&&n.length>0?n:ae,{send:T,resume:O,suggest:B}=he(),_=q(()=>({onSuccess(){r&&B()},onComplete(){requestAnimationFrame(()=>{v.current?.focus()})},onEvent:b}),[r,b]),{messages:U,loading:H,request:J}=ce(_),[K,Q]=j();N(()=>{const e=U.filter(e=>!!e.query);Q(e.at(-1)?.variables)},[U]),N(()=>{!n&&S.length>0&&F([])},[n]),N(()=>{const e=o.current;if(e&&T){const t=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const t={query:e.target.text};a&&(t.variables={...K,...a.values}),T(t)}};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[a,K]);const W=C(({target:e})=>{if(e.files){const t=Array.from(e.files).filter(e=>!!L.includes(A.extname(e.name))&&(!(e.size>20971520)&&e)).slice(0,6-S.length);F(e=>{e.push(...t.map(e=>({file:e})))}),t.forEach(async e=>{try{const t=new FormData;t.set("file",e);const r=await J({method:"POST",url:"/upload",data:t});F(t=>{const n=t.find(t=>t.file===e);n&&(n.path=r.path)})}catch(t){const r=l(t)?t.message:"unknown error";F(t=>{const n=t.find(t=>t.file===e);n&&(n.error=r)})}})}e.value=""},[S,L]),X=S.flatMap(({file:e,path:t})=>t?[{name:e.name,size:e.size,path:t}]:[]),V=C(()=>{if(w||X.length>0){const e={query:w,files:X};a&&(e.variables={...K,...a.values}),E&&(e.quote=E),T(e),k(""),M(""),F([]),y.current?.stop(!0)}},[w,X,a,K,E]);if(z(f,()=>({setQuery:k,setQuote:M,send:T,resume:O,focus(){v.current?.focus()}})),a){const e=a.config.filter(e=>!(e.key in(a.values||{}))),r=(t,r=!1)=>!!t&&e.every(e=>{const n=!e.required||t?.[e.key];return!n&&r&&d.error("请完善必填信息"),n});if(e.length>0&&!r(K))return t(De,{children:t(Ie,{children:t(ue,{values:K,variables:e,onSubmit:({formData:e})=>{if(r(e,!0))return Q(e),!0}})})})}return e(_e,{children:[h&&t(Se,{children:h}),e(Be,{$focused:I&&!H,children:[S.length>0&&t(Pe,{children:S.map(({file:e,error:r,path:n},i)=>{const o=void 0===r&&void 0===n;return t(Le,{name:e.name,size:e.size,error:r,loading:o,onRemove:()=>{F(t=>{const r=t.findIndex(t=>t.file===e);-1!==r&&t.splice(r,1)})}},i)})}),!!E&&e(Oe,{children:[t("i",{className:"bi bi-quote"}),t("p",{children:E}),t(P,{onClick:()=>M("")})]}),e(Te,{children:[t(R,{disabled:H||u,ref:v,placeholder:p,minRows:c,maxRows:5,value:w,onChange:e=>k(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),V())},autoFocus:m,onFocus:()=>D(!0),onBlur:()=>D(!1)}),n&&t(s,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:e(me,{disabled:S.length>=6,onClick:()=>x.current?.click(),children:[t("input",{onChange:W,multiple:!0,accept:L.join(","),ref:x,type:"file",hidden:!0}),t("i",{className:"bi bi-file-earmark-arrow-up"})]})}),i&&t(be,{ref:y,model:i.model,onResult:e=>{v.current?.focus(),k(t=>t+e+" ")}}),t(me,{className:"text-primary",disabled:!w&&0===X.length,onClick:e=>{e.preventDefault(),V()},children:t("i",{className:"bi bi-send-fill"})})]})]})]})});const Ie=n.div`
|
|
170
170
|
border-radius: var(--bs-border-radius-lg);
|
|
171
171
|
box-shadow: var(--bs-box-shadow-sm);
|
|
172
172
|
margin: 1rem;
|
|
@@ -236,7 +236,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
236
236
|
box-shadow: 0.125rem 0.25rem 0.5rem rgba(0, 0, 0, 0.1);
|
|
237
237
|
`,_e=n.div`
|
|
238
238
|
margin: 0 1rem 1rem;
|
|
239
|
-
`;function Ue(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 He(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 Je,Ke,Qe;function We(){return We=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},We.apply(null,arguments)}const Xe=e=>w.createElement("svg",We({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},e),Je||(Je=w.createElement("circle",{cx:15,cy:15,r:15,fill:"#3c60ff"},w.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),w.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))),Ke||(Ke=w.createElement("circle",{cx:60,cy:15,r:9,fill:"#3c60ff",fillOpacity:.3},w.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:9,repeatCount:"indefinite",to:9,values:"9;15;9"}),w.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:.5,repeatCount:"indefinite",to:.5,values:".5;1;.5"}))),Qe||(Qe=w.createElement("circle",{cx:105,cy:15,r:15,fill:"#3c60ff"},w.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),w.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))));function Ve(e){if(!e)return"";if("string"==typeof e)return e;if("image"===e.type){return`})`}return""}const Ye=function(e,t,r){function n(t){return t!==V.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(Y.chunkString,{contentType:"string"}),i(t)):(e.consume(t),n)}function i(n){return n===V.rightSquareBracket?(e.exit(Y.chunkString),e.exit("citeData"),e.enter("citeMarker"),function(n){if(n!==V.rightSquareBracket)return r(n);return e.consume(n),e.exit("citeMarker"),e.exit("cite"),t(n)}(n)):G(n)||n===V.eof||n>57||n<48?r(n):(e.consume(n),i)}return function(t){return t!==V.leftSquareBracket?r(t):(e.enter("cite"),e.enter("citeMarker"),n(t))}},Ge=function(){const e=this.data();e.micromarkExtensions.push({text:{[V.leftSquareBracket]:{tokenize:Ye}}}),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}}})},Ze=f.Image,et={pre:({children:r,node:n})=>{const[i,o]=j(!1),s=q(()=>{const e=n.children[0];return"element"===(t=e).type&&"code"===t.tagName&&!!e.properties?.className?.includes("language-svg");var t},[n]);
|
|
239
|
+
`;function Ue(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 He(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 Je,Ke,Qe;function We(){return We=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},We.apply(null,arguments)}const Xe=e=>w.createElement("svg",We({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},e),Je||(Je=w.createElement("circle",{cx:15,cy:15,r:15,fill:"#3c60ff"},w.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),w.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))),Ke||(Ke=w.createElement("circle",{cx:60,cy:15,r:9,fill:"#3c60ff",fillOpacity:.3},w.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:9,repeatCount:"indefinite",to:9,values:"9;15;9"}),w.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:.5,repeatCount:"indefinite",to:.5,values:".5;1;.5"}))),Qe||(Qe=w.createElement("circle",{cx:105,cy:15,r:15,fill:"#3c60ff"},w.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),w.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))));function Ve(e){if(!e)return"";if("string"==typeof e)return e;if("image"===e.type){return`})`}return""}const Ye=function(e,t,r){function n(t){return t!==V.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(Y.chunkString,{contentType:"string"}),i(t)):(e.consume(t),n)}function i(n){return n===V.rightSquareBracket?(e.exit(Y.chunkString),e.exit("citeData"),e.enter("citeMarker"),function(n){if(n!==V.rightSquareBracket)return r(n);return e.consume(n),e.exit("citeMarker"),e.exit("cite"),t(n)}(n)):G(n)||n===V.eof||n>57||n<48?r(n):(e.consume(n),i)}return function(t){return t!==V.leftSquareBracket?r(t):(e.enter("cite"),e.enter("citeMarker"),n(t))}},Ge=function(){const e=this.data();e.micromarkExtensions.push({text:{[V.leftSquareBracket]:{tokenize:Ye}}}),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}}})},Ze=f.Image,et={pre:({children:r,node:n})=>{const[i,o]=j(!1),s=q(()=>{const e=n.children[0];return"element"===(t=e).type&&"code"===t.tagName&&!!e.properties?.className?.includes("language-svg");var t},[n]);N(()=>{s&&o(s)},[s]);const a=q(()=>U(n),[n]);return e(st,{children:[e(ot,{children:[!i&&t(v,{tooltip:!1,as:it,content:a}),s&&i&&t(it,{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&&t(it,{onClick:()=>o(!i),children:i?"源码":"预览"})]}),s&&i?t("div",{dangerouslySetInnerHTML:{__html:a}}):t("pre",{children:r})]})},a:({node:e,href:r,...n})=>{const i=r?.startsWith("http")?"_blank":"_self";return t("a",{...n,href:r,target:i})},img:({node:e,...r})=>t(Ze,{...r})},tt=[[X,{singleDollarTextMath:!1}],W,Q],rt=[K,[J,{detect:!1,ignoreMissing:!0}]],nt=F(({content:e,components:r,cite:n})=>{e=e.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$");const i=q(()=>{const e={...et,...r};return n&&(e.cite=n),e},[r,n]),o=q(()=>n?[...tt,Ge]:tt,[n]);return t(at,{remarkPlugins:o,rehypePlugins:rt,components:i,children:e})},_),it=n.div`
|
|
240
240
|
cursor: pointer;
|
|
241
241
|
padding: 2px 10px;
|
|
242
242
|
background-color: #303030;
|
|
@@ -598,7 +598,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
598
598
|
max-height: 100%;
|
|
599
599
|
max-width: 100%;
|
|
600
600
|
}
|
|
601
|
-
`,gt=k(null);function bt(){const e=
|
|
601
|
+
`,gt=k(null);function bt(){const e=E(gt);if(!e)throw new Error("useContext must be used within a Provider");return e}function ft({children:e,...r}){return t(gt.Provider,{value:r,children:e})}function vt({description:r,options:n}){const{resume:i,disabled:o,output:s}=bt(),[a,l]=j(s?.result),c=o||void 0!==a;return e(xt,{children:[r&&t(yt,{children:r}),n.map((e,r)=>t(wt,{$selected:a===e,$disabled:c,onClick:()=>(e=>{c||(l(e),i(e))})(e),children:e},r))]})}const xt=n.div`
|
|
602
602
|
display: flex;
|
|
603
603
|
flex-direction: column;
|
|
604
604
|
gap: 0.5rem;
|
|
@@ -633,7 +633,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
633
633
|
&:active {
|
|
634
634
|
transform: ${e=>e.$disabled?"none":"scale(0.98)"};
|
|
635
635
|
}
|
|
636
|
-
`;function kt({description:r,fields:n}){const{resume:i,disabled:o,output:s}=bt(),[a,l]=j(void 0!==s),d=o||a,{schema:h,uiSchema:u}=q(()=>{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&&i.options?(e[i.name].enum=i.options,e[i.name].enumNames=i.options):"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"}}),i.required&&t.push(i.name);return{schema:{type:"object",properties:e,required:t},uiSchema:r}},[n]);return e(
|
|
636
|
+
`;function kt({description:r,fields:n}){const{resume:i,disabled:o,output:s}=bt(),[a,l]=j(void 0!==s),d=o||a,{schema:h,uiSchema:u}=q(()=>{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&&i.options?(e[i.name].enum=i.options,e[i.name].enumNames=i.options):"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"}}),i.required&&t.push(i.name);return{schema:{type:"object",properties:e,required:t},uiSchema:r}},[n]);return e(Et,{children:[r&&t(Nt,{children:r}),t(c,{schema:h,uiSchema:u,formData:s,onSubmit:({formData:e})=>{d||(l(!0),i(e))},disabled:d,validate:!0,showErrorList:!1,submitText:a?"已提交":"提交",omitExtraData:!0})]})}const Et=n.div`
|
|
637
637
|
display: flex;
|
|
638
638
|
flex-direction: column;
|
|
639
639
|
gap: 0.5rem;
|
|
@@ -650,10 +650,10 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
650
650
|
&:last-child {
|
|
651
651
|
margin-bottom: 0;
|
|
652
652
|
}
|
|
653
|
-
`,
|
|
653
|
+
`,Nt=n.div`
|
|
654
654
|
color: var(--bs-secondary);
|
|
655
655
|
line-height: 1.5;
|
|
656
|
-
`;function jt({suspend:e}){const{readonly:r}=ce(),{index:n,chunk:i,isLastMessage:o}=function(){const e=
|
|
656
|
+
`;function jt({suspend:e}){const{readonly:r}=ce(),{index:n,chunk:i,isLastMessage:o}=function(){const e=E(dt);if(!e)throw new Error("useContext must be used within a Provider");return e}(),{resume:s}=he(),a=C(e=>{s({chunk:n,payload:e})},[]);let l=null;switch(e.type){case"select":l=t(vt,{...e.select});break;case"form":l=t(kt,{...e.form})}return t(ft,{resume:a,disabled:r||!!i.error||!o,output:i.node?.output,children:l})}function $t({value:e,cite:n}){const i=e=>"string"==typeof e?t(nt,{content:e,cite:n}):"image"===e.type?t(mt,{src:e.image}):"suspend"===e.type?t(jt,{suspend:e.suspend}):null;return Array.isArray(e)?t(r,{children:e.map((e,r)=>t(S,{children:i(e)},r))}):i(e)}const Ct=F(({reasoning:r,loading:n})=>{const[i,o]=j(!1);return N(()=>{n||o(!1)},[n]),t(S,{children:t("div",{className:"mb-2",children:e("div",i?{className:"shadow-sm rounded bg-white fs-7",children:[e("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[n?t(D,{animation:"border",variant:"primary",size:"sm"}):t("i",{className:"bi bi-check-circle-fill text-success"}),t("span",{className:"text-muted",children:n?"思考中":"已思考"}),t("i",{className:"bi bi-caret-up-fill text-muted"})]}),t("div",{className:"border-top p-2 d-flex flex-column gap-2",children:t("div",{className:"border rounded bg-light p-2",children:t(nt,{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?t(D,{animation:"border",variant:"primary",size:"sm"}):t("i",{className:"bi bi-check-circle-fill text-success"}),t("span",{className:"text-muted",children:n?"思考中":"已思考"}),t("i",{className:"bi bi-caret-down-fill text-muted"})]})})})});class Mt{#t;#o=[];#s=0;constructor(){this.#t=new B}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 qt=new Map;class zt{#r;#t;#o=[];#a=new Map;#l=!1;#s=0;#c=!1;constructor(e){this.#r=e,this.#t=new B}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?(qt.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),qt.delete(this)}start(e,t=!1){qt.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=qt.get(this);e&&(e.pause(),e.currentTime=0)}onPlaying(e){this.#t.on("playing",e)}onSpeaking(e){this.#t.on("speaking",e)}}function St({text:e,loading:t,loader:r}){const n=$(),[i,o]=j(!1),[s,a]=j(!1),l=$(t),c=async(i=!1)=>{n.current||(n.current=r?new zt(r):new Mt,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 N(()=>{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]),N(()=>()=>{n.current&&(n.current.stop(),n.current=void 0)},[]),{playing:s,speaking:i,start:c,stop:()=>{n.current&&n.current.stop()}}}function Ft({model:r="builtin",voice:n,loading:i,autoplay:o,contentRef:s,avatarRef:a}){const{request:l}=ce(),c=q(()=>{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:h,start:u,stop:m}=St({loader:c,text:s,loading:o&&i});let p=null;return a.current&&(h&&(p=Z(t(Rt,{children:t(D,{size:"sm",variant:"light"})}),a.current)),d&&(p=Z(t(Rt,{children:t(At,{className:"bi bi-volume-down-fill text-light"})}),a.current))),e(lt,{disabled:i&&!d,onClick:()=>{d?m():u()},children:[d?t(At,{className:"bi bi-volume-down-fill"}):h?t(D,{size:"sm",variant:"primary"}):t(At,{className:"bi bi-volume-down"}),p]})}const Rt=n.div`
|
|
657
657
|
position: absolute;
|
|
658
658
|
top: 0;
|
|
659
659
|
left: 0;
|
|
@@ -676,7 +676,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
676
676
|
}
|
|
677
677
|
`,Ot=n.span`
|
|
678
678
|
display: inline-block;
|
|
679
|
-
`;function Bt({nodes:r,loading:n}){const[i,o]=j(!1),s=r.some(e=>"error"in e),a=n?t(D,{animation:"border",variant:"primary",size:"sm"}):t("i",s?{className:"bi bi-x-circle-fill text-danger"}:{className:"bi bi-check-circle-fill text-success"});return t("div",{className:"mb-2",children:e("div",i?{className:"shadow-sm rounded bg-white fs-7",children:[e("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[a,t("span",{children:"工作流"}),t("i",{className:"bi bi-caret-up-fill text-muted"})]}),t("div",{className:"border-top p-2 d-flex flex-column gap-1 bg-light bg-opacity-50",children:r.map((e,r)=>t(Lt,{node:e},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,t("span",{children:"工作流"}),t("i",{className:"bi bi-caret-down-fill text-muted"})]})})}const _t=F(({message:n,actions:i,setMessage:o,cite:s,speech:a,isLast:l})=>{const{query:c,quote:d,files:h=[],chunks:u,annotation:m,loading:p}=n,{user:g,bot:b,logLevel:f="none",messages:y}=ce(),w=q(()=>u?u.reduce(function(e,t){const r=Array.isArray(t.content)?t.content.reduce((e,t)=>{const r=Ve(t);return r?e+r+"\n":e},""):Ve(t.content);return e+r+"\n"+(t.tools||[]).reduce((e,t)=>{if(t.content){const r=Ve(t.content);if(r)return e+r+"\n"}return e},"")},""):"",[u]),k=$(null),
|
|
679
|
+
`;function Bt({nodes:r,loading:n}){const[i,o]=j(!1),s=r.some(e=>"error"in e),a=n?t(D,{animation:"border",variant:"primary",size:"sm"}):t("i",s?{className:"bi bi-x-circle-fill text-danger"}:{className:"bi bi-check-circle-fill text-success"});return t("div",{className:"mb-2",children:e("div",i?{className:"shadow-sm rounded bg-white fs-7",children:[e("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[a,t("span",{children:"工作流"}),t("i",{className:"bi bi-caret-up-fill text-muted"})]}),t("div",{className:"border-top p-2 d-flex flex-column gap-1 bg-light bg-opacity-50",children:r.map((e,r)=>t(Lt,{node:e},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,t("span",{children:"工作流"}),t("i",{className:"bi bi-caret-down-fill text-muted"})]})})}const _t=F(({message:n,actions:i,setMessage:o,cite:s,speech:a,isLast:l})=>{const{query:c,quote:d,files:h=[],chunks:u,annotation:m,loading:p}=n,{user:g,bot:b,logLevel:f="none",messages:y}=ce(),w=q(()=>u?u.reduce(function(e,t){const r=Array.isArray(t.content)?t.content.reduce((e,t)=>{const r=Ve(t);return r?e+r+"\n":e},""):Ve(t.content);return e+r+"\n"+(t.tools||[]).reduce((e,t)=>{if(t.content){const r=Ve(t.content);if(r)return e+r+"\n"}return e},"")},""):"",[u]),k=$(null),E=$(null),N=$(null),j=q(()=>u?.flatMap(e=>e.node?[e.node]:[])||[],[u]),C=c||h.length>0;return e(r,{children:[C&&e(Zt,{$reverse:!0,children:[g&&t(Vt,{children:t("img",{src:g.avatar})}),t(Yt,{}),e(Gt,{ref:k,children:[c&&t(nt,{content:c}),d&&t(T,{trigger:"click",container:k,rootClose:!0,placement:"bottom",overlay:t(Ut,{body:!0,children:d}),children:t(Jt,{children:t(Ht,{children:d})})}),h.length>0&&h.map(({name:e,size:r,path:n},i)=>x(n)?t(mt,{src:`/uploads/${n}`},i):t(Qt,{children:t(re,{name:e,size:r})},i))]})]}),u&&e(Zt,{children:[b&&t(Vt,{ref:E,children:t("img",{src:b.avatar})}),t(Yt,{}),e(Gt,{children:[C&&!1!==i&&e(Wt,{children:[t(v,{placement:"top",content:w,as:lt,tooltip:!0}),a&&t(Ft,{loading:n.loading,avatarRef:E,contentRef:N,...a}),i?.({Component:lt,message:n,content:w,setMessage:o,messages:y})]}),t(Xt,{children:["stats","all"].includes(f)&&n.stats&&e(r,{children:[e("span",{children:["耗时 ",n.stats.latency/1e3," 秒"]}),e("span",{children:["花费 Token ",n.stats.usage]})]})}),e(Kt,{ref:N,$deleted:!!m,children:["all"===f&&j.length>0&&t(Bt,{loading:p,nodes:j}),u.map((r,n)=>e(ht,{value:{index:n,chunk:r,isLastMessage:l},children:[r.reasoning&&["stats","all"].includes(f)&&t(Ct,{reasoning:r.reasoning,loading:!r.content&&0===(r.tools||[]).length}),r.content&&t($t,{cite:s,value:r.content}),(r.tools||[]).map((e,r)=>t(It,{tool:e,loading:p},r)),r.error&&t(nt,{content:`[${r.error}]`})]},n))]}),m&&e("div",{children:[e("div",{className:"d-flex align-items-center",children:[t("span",{className:"fs-7 text-secondary me-2",children:"标注的答案"}),t("hr",{className:"flex-fill"})]}),t(nt,{content:m.answer})]}),n.loading&&t(Xe,{})]})]})]})}),Ut=n(O)`
|
|
680
680
|
max-width: calc(100% - .875rem * 2);
|
|
681
681
|
width: calc(100% - .875rem * 2);
|
|
682
682
|
font-size: 1rem;
|
|
@@ -782,7 +782,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
782
782
|
background: rgba(var(--bs-primary-rgb), 0.1);
|
|
783
783
|
}
|
|
784
784
|
`};
|
|
785
|
-
`;function er(r){const{placeholder:n,actions:i,cite:o,speech:s,renderItem:a,scrollRef:l}=r,{messages:c,setMessages:d,loading:h,suggestions:u,conversationId:m}=ce(),p=$(r.loaded),g=C(()=>{const e=l.current;e&&requestAnimationFrame(()=>{e.scrollTo({top:e.scrollHeight})})},[]),b=$(null),{height:f}=ee({ref:b});
|
|
785
|
+
`;function er(r){const{placeholder:n,actions:i,cite:o,speech:s,renderItem:a,scrollRef:l}=r,{messages:c,setMessages:d,loading:h,suggestions:u,conversationId:m}=ce(),p=$(r.loaded),g=C(()=>{const e=l.current;e&&requestAnimationFrame(()=>{e.scrollTo({top:e.scrollHeight})})},[]),b=$(null),{height:f}=ee({ref:b});N(()=>{(h||!p.current)&&c.length>0&&(h&&(p.current=!0),g())},[f,h,p,c]),N(()=>{u.length>0&&g()},[u]),N(()=>{p.current=r.loaded},[m]),N(()=>{const e=b.current;if(e){const t=()=>{p.current=!0};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[b]);const v=c.slice(-30);return e(tr,{ref:b,children:[0===v.length&&n,v.map((e,r)=>{const n={actions:i,cite:o,speech:s,message:e,setMessage:t=>{d(r=>{const n=r.findIndex(t=>t.id===e.id);-1!==n&&t(r[n])})},isLast:r===v.length-1};return a?a({Component:_t,props:n},r):t(_t,{...n},r)}),u.length>0&&t(rr,{children:u.map((e,r)=>t("a",{href:"#!question",children:e},r))})]})}const tr=n.div`
|
|
786
786
|
display: flex;
|
|
787
787
|
flex-direction: column;
|
|
788
788
|
flex: 1;
|
|
@@ -803,7 +803,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
803
803
|
background: var(--bs-secondary-bg-subtle);
|
|
804
804
|
}
|
|
805
805
|
}
|
|
806
|
-
`,nr=F(M(({className:r,bot:n,user:i,input:o,logLevel:s,imageResolver:a,nodeIconResolver:l,placeholder:c,actions:d,onboarding:h,conversation:u,cite:m,speech:p,renderItem:b,messages:v,request:x,transformMessages:w=Ue},k)=>{const
|
|
806
|
+
`,nr=F(M(({className:r,bot:n,user:i,input:o,logLevel:s,imageResolver:a,nodeIconResolver:l,placeholder:c,actions:d,onboarding:h,conversation:u,cite:m,speech:p,renderItem:b,messages:v,request:x,transformMessages:w=Ue},k)=>{const E=C(e=>{const t=[];if(h&&!1!==h.enable){const e=He(h.questions||[]);t.push({chunks:[{content:[h.prologue,...e.map(e=>`[${e}](#!question)`)].join("\n"),tools:[]}],loading:h.loading})}return e?t.concat(w(e.messages||[])):v?t.concat(w(v)):t},[h]),[M,S]=j(u?.id),[F,R]=g([]);N(()=>{R(E(u))},[h]);const[A,I]=j(!1),D=$(null),[L,P]=j([]),T=C(e=>{A||(S(e?.id),R(E(e)),P([]))},[A,E]);return z(k,()=>({reset:T}),[T]),t(de,{request:q(()=>x?"string"==typeof x?y.create({baseURL:x}):x:y,[x]),bot:n,user:i,logLevel:s,imageResolver:a,nodeIconResolver:l,conversationId:M,setConversationId:S,messages:F,setMessages:R,suggestions:L,setSuggestions:P,loading:A,setLoading:I,reset:T,readonly:!o,children:t(or,{className:r,children:e(f,{children:[t(ir,{ref:D,children:t(er,{placeholder:c,cite:m,actions:d,loaded:!o,speech:p,renderItem:b,scrollRef:D})}),o&&t(Ae,{...o,scrollRef:D})]})})})}),_),ir=n.div`
|
|
807
807
|
display: flex;
|
|
808
808
|
flex-direction: column;
|
|
809
809
|
flex: 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.40",
|
|
4
4
|
"module": "es/index.js",
|
|
5
5
|
"types": "types/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
},
|
|
61
61
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
62
62
|
"license": "MIT",
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "ec9772724e9636ed8cd78ba99ba7ee33f6383ce5",
|
|
64
64
|
"scripts": {
|
|
65
65
|
"prebuild": "rimraf es types",
|
|
66
66
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -45,6 +45,10 @@ export interface MessageBoxProps {
|
|
|
45
45
|
config: Variable[];
|
|
46
46
|
values?: Record<string, string>;
|
|
47
47
|
};
|
|
48
|
+
onEvent?: (event: {
|
|
49
|
+
type: string;
|
|
50
|
+
data?: any;
|
|
51
|
+
}) => void;
|
|
48
52
|
ref?: MutableRefObject<Input | null>;
|
|
49
53
|
};
|
|
50
54
|
request?: RequestInstance | string;
|
|
@@ -16,6 +16,10 @@ interface Props {
|
|
|
16
16
|
disabled?: boolean;
|
|
17
17
|
placeholder?: string;
|
|
18
18
|
autoFocus?: boolean;
|
|
19
|
+
onEvent?: (event: {
|
|
20
|
+
type: string;
|
|
21
|
+
data?: any;
|
|
22
|
+
}) => void;
|
|
19
23
|
}
|
|
20
24
|
declare const _default: import("react").ForwardRefExoticComponent<Props & import("react").RefAttributes<Input>>;
|
|
21
25
|
export default _default;
|