@topthink/chat 1.1.0 → 1.1.1
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 +3 -3
- package/package.json +2 -2
package/es/index.js
CHANGED
|
@@ -568,7 +568,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{Lightbox
|
|
|
568
568
|
background-color: var(--bs-gray-200);
|
|
569
569
|
}
|
|
570
570
|
|
|
571
|
-
`;class Ge{#s;#e;constructor(){this.#e=new J}onResult(e){this.#e.on("result",e)}onRecording(e){this.#e.on("recording",e)}onTranscribing(e){this.#e.on("transcribing",e)}start(){if(!this.#s){const e=window.SpeechRecognition||window.webkitSpeechRecognition;if(!e)return void m.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.#e.emit("result",t)},t.onspeechend=()=>{this.#e.emit("recording",!1)},t.onerror=
|
|
571
|
+
`;class Ge{#s;#e;constructor(){this.#e=new J}onResult(e){this.#e.on("result",e)}onRecording(e){this.#e.on("recording",e)}onTranscribing(e){this.#e.on("transcribing",e)}start(){if(!this.#s){const e=window.SpeechRecognition||window.webkitSpeechRecognition;if(!e)return void m.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.#e.emit("result",t)},t.onspeechend=()=>{this.#e.emit("recording",!1)},t.onerror=e=>{this.#e.emit("recording",!1),m.error(e.error)},this.#s=t}this.#e.emit("recording",!0),this.#s.start()}stop(){this.#s?.stop(),this.#e.emit("recording",!1)}}class Ze{#e;#n;#l;#c;constructor(e){this.#n=e,this.#e=new J}async start(){try{this.#l=await navigator.mediaDevices.getUserMedia({audio:!0}),this.#c=new AudioContext;const e=this.#c.createScriptProcessor(0,1,1);this.#c.createMediaStreamSource(this.#l).connect(e),e.connect(this.#c.destination);let t=[],r=0;const n=.01,i=1;e.onaudioprocess=e=>{const o=e.playbackTime,a=e.inputBuffer.getChannelData(0);let s=!0;for(let e=0;e<a.length;e++)if(Math.abs(a[e])>n){s=!1;break}if(s){const e=o-r;e>i&&(t.length>1&&(this.save(t),t=[]),e>10&&this.stop())}else t.push(new Float32Array(a)),r=o},this.#e.emit("recording",!0)}catch(e){m.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 a=e.subarray(t,t+i),s=r.encodeBuffer(a);n.push(new Int8Array(s)),o-=i}}return n.push(r.flush()),new Blob(n,{type:"audio/mp3"})}async save(e){const t=await this.encodeMP3(e);try{this.#e.emit("transcribing",!0);const e=await this.#n(t);e&&this.#e.emit("result",e)}catch(e){}finally{this.#e.emit("transcribing",!1)}}stop(){this.#l&&(this.#l.getTracks().forEach((e=>e.stop())),this.#l=void 0),this.#c&&(this.#c.close(),this.#c=void 0),this.#e.emit("recording",!1)}onResult(e){this.#e.on("result",e)}onRecording(e){this.#e.on("recording",e)}onTranscribing(e){this.#e.on("transcribing",e)}}const et=q(((r,n)=>{let{onResult:i,model:o="builtin"}=r;const{request:a}=de(),s=k((()=>{if("builtin"!==o)return async e=>{const t=new FormData;t.append("file",e,"audio.mp3"),t.append("model",o);return(await a({url:"/transcriptions",method:"post",data:t})).text}}),[o]),{start:c,stop:d,recording:h,transcribing:u}=function(e){let{onResult:t,loader:r}=e;const n=E(),[i,o]=C(!1),[a,s]=C(!1);return z((()=>()=>{n.current&&(n.current.stop(),n.current=void 0)}),[]),{recording:i,transcribing:a,start:async()=>{n.current||(n.current=r?new Ze(r):new Ge,n.current.onRecording(o),n.current.onTranscribing(s),n.current.onResult(t)),n.current.start()},stop:()=>{n.current&&n.current.stop()}}}({onResult:i,loader:s});return S(n,(()=>({start:c,stop:d})),[c,d]),e(l,{tooltip:h?"停止":"语音输入",placement:"top",children:t(Ye,{onMouseDown:e=>{e.preventDefault(),h?d():c()},children:[u&&e(tt,{animation:"border",variant:"primary",size:"sm"}),e("i",h?{className:"bi bi-mic-fill text-danger"}:{className:"bi bi-mic"})]})})})),tt=i(_)`
|
|
572
572
|
position: absolute;
|
|
573
573
|
--bs-spinner-width: .75rem;
|
|
574
574
|
--bs-spinner-height: .75rem;
|
|
@@ -597,7 +597,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{Lightbox
|
|
|
597
597
|
&:hover {
|
|
598
598
|
background-color: var(--bs-secondary-bg-subtle);
|
|
599
599
|
}
|
|
600
|
-
`;function it(){const{request:n,reset:i,conversationId:o}=de(),[a,s]=C(!1);return t(r,{children:[e(rt,{tooltip:"历史记录",placement:"top",onClick:()=>s(!0),children:e("i",{className:"bi bi-clock-history"})}),e(g,{show:a,onHide:()=>{
|
|
600
|
+
`;function it(){const{request:n,reset:i,conversationId:o}=de(),[a,s]=C(!1);return t(r,{children:[e(rt,{tooltip:"历史记录",placement:"top",onClick:()=>s(!0),children:e("i",{className:"bi bi-clock-history"})}),e(g,{show:a,onHide:()=>{s(!1)},placement:"end",header:"聊天历史记录",bodyAs:ot,children:a&&e(dt,{children:e(ct,{useWindow:!1,source:e=>n({url:"/conversation",params:e}),render:r=>{let{data:a,loading:c,setData:d}=r;return a.length||c?a.map(((r,a)=>{const c=r.messages?.reduce(((e,t)=>e+t.chunks.reduce(((e,t)=>e+(t.content||"")),"")),"");return t(M,{children:[a>0&&e("hr",{className:"mx-2 my-2"}),t(lt,{onClick:()=>{i(r),s(!1)},children:[t(st,{children:[o===r.id&&e(K,{bg:"secondary",children:"当前"}),e("span",{className:"fw-bold overflow-hidden text-truncate me-auto",children:r.title||"未命名对话"}),e("span",{className:"text-body text-opacity-50 flex-shrink-0 ms-5",children:f(r.update_time).fromNow()})]}),t(at,{children:[e("span",{className:"overflow-hidden text-truncate me-auto",children:c||" "}),e("span",{className:"ms-3",children:o!==r.id&&e(l,{tooltip:"删除",children:e(b,{as:"a",confirm:"确定要删除吗?",onRequest:()=>n({url:`/conversation/${r.id}`,method:"DELETE"}),onSuccess:()=>{d((e=>{const t=e.findIndex((e=>e.id===r.id));-1!==t&&e.splice(t,1)}))},children:e("i",{className:"bi bi-trash3"})})})})]})]})]},r.id)})):e("div",{className:"text-muted text-center py-5",children:"暂无对话记录"})}})})})]})}const ot=i.div`
|
|
601
601
|
padding: 0;
|
|
602
602
|
overflow: hidden;
|
|
603
603
|
`,at=i.div`
|
|
@@ -647,7 +647,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{Lightbox
|
|
|
647
647
|
margin-bottom: .5rem;
|
|
648
648
|
display: flex;
|
|
649
649
|
padding: 0 .125rem;
|
|
650
|
-
`;var vt=q((function(r,n){let{suggestion:i,fileTypes:o,speech:a,scrollRef:s,variables:c,minRows:d,toolbar:u,disabled:p,autoFocus:g}=r;const f=E(null),b=E(null),x=E(null),[y,w]=C(""),[k,j]=C(""),[N,M]=v([]),[q,A]=C(!1),F=o&&o.length>0?o:Je,{messages:R,loading:D,request:I}=de(),{send:T}=function(e){let{suggestion:t,onSuccess:r}=e;const{setMessages:n,setSuggestions:i,setLoading:o,conversationId:a,setConversationId:s,request:l}=de(),c=E(null),d=$((async()=>{c.current=new AbortController;try{let e={method:"post",url:"/suggestion",data:{conversation:a},signal:c.current.signal};const t=await l(e);i(t)}catch{}finally{c.current=null}}),[a]),u=$((async function(e){if("string"==typeof e){for(var u=arguments.length,m=new Array(u>1?u-1:0),p=1;p<u;p++)m[p-1]=arguments[p];e={query:e,...m}}const{query:g,files:f=[],variables:b={},quote:v}=e;if(g||f.length>0){c.current&&c.current.abort(),o(!0),i([]),n((e=>{e.push({query:g,quote:v,files:f,variables:b,chunks:[],loading:!0})}));try{let e={method:"post",data:{query:g,quote:v,files:f,variables:b,conversation:a},onMessage:e=>{if(e.data)if("[DONE]"!=e.data)try{const t=JSON.parse(e.data);t.conversation?s(t.conversation):n((e=>{const r=e[e.length-1];if(r.chunks)if(t.chunks){const e=t.chunks.index;if(r.chunks[e]||(r.chunks[e]={content:"",tools:[]}),t.chunks.error)r.chunks[e].error=t.chunks.error;else if(t.chunks.tools){const n=t.chunks.tools.index;"response"in t.chunks.tools?(r.chunks[e].tools[n].response=t.chunks.tools.response,r.chunks[e].tools[n].error=t.chunks.tools.error,r.chunks[e].tools[n].content=t.chunks.tools.content):r.chunks[e].tools[n]={name:t.chunks.tools.name,title:t.chunks.tools.title,arguments:t.chunks.tools.arguments}}else t.chunks.content&&(r.chunks[e].content+=t.chunks.content)}else t.stats&&(r.stats=t.stats)}))}catch(e){console.error(e)}else n((e=>{e[e.length-1].loading=!1}))}};await l(e),t&&d()}catch(e){let t="未知错误";h(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),n((e=>{const r=e[e.length-1];r.chunks&&(0===r.chunks.length?r.chunks=[{content:`[${t}]`,tools:[]}]:r.chunks[r.chunks.length-1].content=`[${t}]`),r.loading=!1}))}o(!1),r?.()}}),[]);return{send:u}}({suggestion:i,onSuccess(){requestAnimationFrame((()=>{f.current?.focus()}))}}),[P,L]=C((()=>{const e=R.filter((e=>!!e.query));
|
|
650
|
+
`;var vt=q((function(r,n){let{suggestion:i,fileTypes:o,speech:a,scrollRef:s,variables:c,minRows:d,toolbar:u,disabled:p,autoFocus:g}=r;const f=E(null),b=E(null),x=E(null),[y,w]=C(""),[k,j]=C(""),[N,M]=v([]),[q,A]=C(!1),F=o&&o.length>0?o:Je,{messages:R,loading:D,request:I}=de(),{send:T}=function(e){let{suggestion:t,onSuccess:r}=e;const{setMessages:n,setSuggestions:i,setLoading:o,conversationId:a,setConversationId:s,request:l}=de(),c=E(null),d=$((async()=>{c.current=new AbortController;try{let e={method:"post",url:"/suggestion",data:{conversation:a},signal:c.current.signal};const t=await l(e);i(t)}catch{}finally{c.current=null}}),[a,l]),u=$((async function(e){if("string"==typeof e){for(var u=arguments.length,m=new Array(u>1?u-1:0),p=1;p<u;p++)m[p-1]=arguments[p];e={query:e,...m}}const{query:g,files:f=[],variables:b={},quote:v}=e;if(g||f.length>0){c.current&&c.current.abort(),o(!0),i([]),n((e=>{e.push({query:g,quote:v,files:f,variables:b,chunks:[],loading:!0})}));try{let e={method:"post",data:{query:g,quote:v,files:f,variables:b,conversation:a},onMessage:e=>{if(e.data)if("[DONE]"!=e.data)try{const t=JSON.parse(e.data);t.conversation?s(t.conversation):n((e=>{const r=e[e.length-1];if(r.chunks)if(t.chunks){const e=t.chunks.index;if(r.chunks[e]||(r.chunks[e]={content:"",tools:[]}),t.chunks.error)r.chunks[e].error=t.chunks.error;else if(t.chunks.tools){const n=t.chunks.tools.index;"response"in t.chunks.tools?(r.chunks[e].tools[n].response=t.chunks.tools.response,r.chunks[e].tools[n].error=t.chunks.tools.error,r.chunks[e].tools[n].content=t.chunks.tools.content):r.chunks[e].tools[n]={name:t.chunks.tools.name,title:t.chunks.tools.title,arguments:t.chunks.tools.arguments}}else t.chunks.content&&(r.chunks[e].content+=t.chunks.content)}else t.stats&&(r.stats=t.stats)}))}catch(e){console.error(e)}else n((e=>{e[e.length-1].loading=!1}))}};await l(e),t&&d()}catch(e){let t="未知错误";h(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),n((e=>{const r=e[e.length-1];r.chunks&&(0===r.chunks.length?r.chunks=[{content:`[${t}]`,tools:[]}]:r.chunks[r.chunks.length-1].content=`[${t}]`),r.loading=!1}))}o(!1),r?.()}}),[a,l]);return{send:u}}({suggestion:i,onSuccess(){requestAnimationFrame((()=>{f.current?.focus()}))}}),[P,L]=C();z((()=>{const e=R.filter((e=>!!e.query));L(e.at(-1)?.variables)}),[R]),z((()=>{!o&&N.length>0&&M([])}),[o]),z((()=>{const e=s.current;if(e&&T){const t=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const t={query:e.target.text};c&&(t.variables={...P,...c.values}),T(t)}};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}}),[c,P]);const B=$((e=>{let{target:t}=e;if(t.files){const e=Array.from(t.files).filter((e=>!!F.includes(W.extname(e.name))&&(!(e.size>20971520)&&e))).slice(0,6-N.length);M((t=>{t.push(...e.map((e=>({file:e}))))})),e.forEach((async e=>{try{const t=new FormData;t.set("file",e);const r=await I({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=h(t)?t.message:"unknown error";M((t=>{const n=t.find((t=>t.file===e));n&&(n.error=r)}))}}))}t.value=""}),[N,F]),O=N.flatMap((e=>{let{file:t,path:r}=e;return r?[{name:t.name,size:t.size,path:r}]:[]})),_=$((()=>{if(y||O.length>0){const e={query:y,files:O};c&&(e.variables={...P,...c.values}),k&&(e.quote=k),T(e),w(""),j(""),M([]),x.current?.stop(!0)}}),[y,O,c,P,k]);if(S(n,(()=>({setQuery:w,setQuote:j,send:T,focus(){f.current?.focus()}}))),c){const t=c.config.filter((e=>!(e.key in(c.values||{})))),r=function(e){let r=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return!!e&&t.every((t=>{const n=!t.required||e?.[t.key];return!n&&r&&m.error("请完善必填信息"),n}))};if(t.length>0&&!r(P))return e(yt,{children:e(xt,{children:e(Ve,{values:P,variables:t,onSubmit:e=>{let{formData:t}=e;if(r(t,!0))return L(t),!0}})})})}return t(Ct,{children:[u&&e(gt,{children:u}),t(Nt,{$focused:q&&!D,children:[N.length>0&&e(kt,{children:N.map(((t,r)=>{let{file:n,error:i,path:o}=t;const a=void 0===i&&void 0===o;return e(wt,{name:n.name,size:n.size,error:i,loading:a,onRemove:()=>{M((e=>{const t=e.findIndex((e=>e.file===n));-1!==t&&e.splice(t,1)}))}},r)}))}),!!k&&t(jt,{children:[e("i",{className:"bi bi-quote"}),e("p",{children:k}),e(Q,{onClick:()=>j("")})]}),t(Et,{children:[e(Y,{disabled:D||p,ref:f,placeholder:"请输入你的问题, Enter+Shift换行, Enter发送",minRows:d,maxRows:5,value:y,onChange:e=>w(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),_())},autoFocus:g,onFocus:()=>A(!0),onBlur:()=>A(!1)}),o&&e(l,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:t(Ye,{disabled:N.length>=6,onClick:()=>b.current?.click(),children:[e("input",{onChange:B,multiple:!0,accept:F.join(","),ref:b,type:"file",hidden:!0}),e("i",{className:"bi bi-file-earmark-arrow-up"})]})}),a&&e(et,{ref:x,model:a.model,onResult:e=>{f.current?.focus(),w((t=>t+e+" "))}}),e(Ye,{className:"text-primary",disabled:!y&&0===O.length,onClick:e=>{e.preventDefault(),_()},children:e("i",{className:"bi bi-send-fill"})})]})]})]})}));const xt=i.div`
|
|
651
651
|
border-radius: var(--bs-border-radius-lg);
|
|
652
652
|
box-shadow: var(--bs-box-shadow-sm);
|
|
653
653
|
margin: 1rem;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prebuild": "rimraf es types",
|
|
6
6
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
},
|
|
66
66
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
67
67
|
"license": "MIT",
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "6a784abf4ccfa9bd4f2961fcc266016497184138"
|
|
69
69
|
}
|