@topthink/chat 1.3.21 → 1.3.23
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 +1 -1
- package/package.json +3 -3
package/es/index.js
CHANGED
|
@@ -185,7 +185,7 @@ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
185
185
|
resize: none;
|
|
186
186
|
line-height: 1.5;
|
|
187
187
|
background: transparent;
|
|
188
|
-
`;var tt=A(function({className:r,fileTypes:n,speech:i,scrollRef:o,variables:s,minRows:d=1,maxRows:m=5,toolbar:h,toolbarActions:p,disabled:g,autoFocus:f,tools:b,renderEditableInput:y,placeholder:x="请输入你的问题, Enter+Shift换行, Enter发送",supportStop:w=!1,onEvent:k,onNewConversation:E},N){const C=M(null),j=M(null),$=M(null),[R,S]=z(""),[A,D]=z(""),[P,T]=v([]),[B,U]=z(!1),_=n&&n.length>0?n:Ee,H=F(()=>{requestAnimationFrame(()=>{$.current?.focus()})},[]),{send:J,resume:K,stop:W}=function(e){const{setLoading:t,request:r}=je(),n=$e(),i=n?.setMessages,o=n?.setConversationId,s=n?.conversationId,a=n?.messages,d=!0===e?.supportStop,{onEvent:u,onNewConversation:m,onComplete:h}=e??{},p=M(null),g=M(),f=M(s),b=()=>(p.current&&p.current.abort(),p.current=new AbortController,p.current.signal),v=F(e=>{const t=(e,r)=>{Object.entries(r).forEach(([r,n])=>{if("string"!=typeof n)if(!n||"object"!=typeof n||Array.isArray(n))e[r]=n;else if("index"in n){const{index:i,...o}=n;Array.isArray(e[r])||(e[r]=[]),e[r][i]||(e[r][i]={}),t(e[r][i],o)}else e[r]=n;else e[r]="string"==typeof e[r]?e[r]+n:n})};if(e.data)if("[DONE]"!=e.data)try{const r=JSON.parse(e.data);r.conversation?(f.current=r.conversation,o?.(r.conversation)):r.event?u?.(r.event):i?.(e=>{const n=e[e.length-1];if(r.chunks){if(n.chunks){const e=r.chunks.index;if(n.chunks[e]||(n.chunks[e]={content:"",reasoning:"",tools:[]}),r.chunks.error)n.chunks[e].error=r.chunks.error;else if(r.chunks.tools){n.chunks[e].tools||(n.chunks[e].tools=[]);const{index:i,...o}=r.chunks.tools;n.chunks[e].tools[i]||(n.chunks[e].tools[i]={name:"",title:"",arguments:""}),t(n.chunks[e].tools[i],o)}else if(r.chunks.reasoning)n.chunks[e].reasoning+=r.chunks.reasoning;else if(r.chunks.content)if("object"==typeof r.chunks.content){Array.isArray(n.chunks[e].content)||(n.chunks[e].content=[]);const t=r.chunks.content.index,i=r.chunks.content.value;"string"==typeof i?(n.chunks[e].content[t]||(n.chunks[e].content[t]=""),n.chunks[e].content[t]+=i):n.chunks[e].content[t]=i}else n.chunks[e].content+=r.chunks.content;else r.chunks.node&&(n.chunks[e].node={...n.chunks[e].node,...r.chunks.node})}}else r.stats?n.stats=r.stats:r.id&&(n.id=r.id)})}catch(e){console.error(e)}else i?.(e=>{e[e.length-1].loading=!1})},[u]),y=F(e=>{if(l(e))i?.(e=>{const t=e[e.length-1];t&&(t.loading=!1)});else{let t="未知错误";c(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),i?.(e=>{const r=e[e.length-1];r.chunks&&(0===r.chunks.length?r.chunks=[{error:t}]:r.chunks[r.chunks.length-1].error=t),r.loading=!1})}},[]),x=F(async e=>{g.current=e;try{const n=b();t(!0),i?.(e=>{e[e.length-1].loading=!0,delete e[e.length-1].stats});let o={url:"message",method:"get",params:{id:e},onMessage:v,signal:n};await r(o)}catch(e){y(e)}finally{p.current=null,g.current=void 0}t(!1),h?.()},[r,u,m,h]),w=F(async(e,...o)=>{"string"==typeof e&&(e={query:e,...o});const{query:s,files:a=[],variables:l={},extra:c={},quote:d}=e;let u=null,g=null;if(s||a.length>0){const e=b();t(!0),n?.setSuggestions?.([]),i?.(e=>{e.push({query:s,quote:d,files:a,variables:l,chunks:[],loading:!0})});try{let t={method:"post",data:{query:s,quote:d,files:a,variables:l,extra:c,conversation:f.current},onMessage:v,signal:e};const n=await r(t);u=n.message??null,g=n.conversation??null}catch(e){y(e)}finally{p.current=null}if(u){let e=!1;g&&!f.current&&(e=m?.(g)??!1),e||await x(u.id)}else t(!1),h?.()}},[r,u,m,h]),k=F(async e=>{let n=null;try{const o=b();t(!0),i?.(e=>{e[e.length-1].loading=!0});let s={url:"resume",method:"post",data:{conversation:f.current,chunk:e.chunk,tool:e.tool,payload:e.payload},onMessage:v,signal:o};n=(await r(s)).message??null}catch(e){y(e)}finally{p.current=null}n?await x(n.id):(t(!1),h?.())},[r,u,m,h]),E=F(async()=>{if(d)if(g.current)try{await r({url:"stop",method:"post",data:{id:g.current}})}catch{}else p.current?.abort()},[r,d]),N=()=>{const e=a?.[a.length-1];e&&e.id&&2===e.status&&x(e.id)};return q(()=>(N(),()=>{p.current&&p.current.abort()}),[]),q(()=>{s!=f.current&&(f.current=s,p.current&&p.current.abort(),N())},[s]),{send:w,resume:k,stop:E}}({supportStop:w,onEvent:k,onNewConversation:E,onComplete:H}),{loading:X,request:V,inputRef:Y}=je(),G=$e(),[Z,ee]=z(),[te,re]=v({});q(()=>{const e=(G?.messages||[]).filter(e=>!!e.query);ee(e.at(-1)?.variables)},[G?.messages]),q(()=>{!n&&P.length>0&&T([])},[n]),q(()=>{const e=o?.current;if(e){const t=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const t={query:e.target.text};s&&(t.variables={...Z,...s.values}),J(t)}};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[s,Z]);const ne=F(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=>!(!_.includes("*")&&!_.includes(O.extname(e.name)))&&!(e.size>20971520)).slice(0,6-P.length);return 0!==t.length&&(T(e=>{e.push(...t.map(e=>({file:e})))}),t.forEach(async e=>{try{const t=new FormData;t.set("file",e);const r=await V({method:"POST",url:"/upload",data:t});T(t=>{const n=t.find(t=>t.file===e);n&&(n.path=r.path)})}catch(t){const r=c(t)?t.message:"unknown error";T(t=>{const n=t.find(t=>t.file===e);n&&(n.error=r)})}}),!0)},[P.length,_,V,T]),ie=F(({target:e})=>{e.files&&ne(Array.from(e.files)),e.value=""},[ne]),oe=F(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&&ne(n)&&e.preventDefault()},[ne]),se=P.flatMap(({file:e,path:t})=>t?[{name:e.name,size:e.size,path:t}]:[]),ae=F(()=>{if(R||se.length>0){const e={query:R,files:se,extra:te};s&&(e.variables={...Z,...s.values}),A&&(e.quote=A),J(e),$.current?.setContent(""),D(""),T([]),j.current?.stop(!0)}},[R,se,s,Z,te,A]),le=I(()=>{const e={setQuery(e){$.current?.setContent(e)},setQuote:D,setExtra:re,send:J,resume:K,focus(){$.current?.focus()}};return Y.current=e,e},[D,re,J,K]);if(L(N,()=>le,[le]),s){const t=s.config.filter(e=>!(e.key in(s.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(Z))return e(nt,{children:e(rt,{children:e(Fe,{values:Z,variables:t,onSubmit:({formData:e})=>{if(r(e,!0))return ee(e),!0}})})})}return t("div",{className:r,children:[h&&e(Ve,{actions:p,children:h}),t(ct,{$focused:B&&!X,children:[P.length>0&&e(ot,{children:P.map(({file:t,error:r,path:n},i)=>{const o=void 0===r&&void 0===n;return e(it,{name:t.name,size:t.size,error:r,loading:o,onRemove:()=>{T(e=>{const r=e.findIndex(e=>e.file===t);-1!==r&&e.splice(r,1)})}},i)})}),!!A&&t(lt,{children:[e("i",{className:"bi bi-quote"}),e("p",{children:A}),e(Q,{onClick:()=>D("")})]}),t(st,{$minRows:d,children:[y?.({Component:Ze,props:{ref:$,readonly:X||g,autoFocus:f,placeholder:x,minRows:d,maxRows:m,onSubmit:ae,onFocusedChange:U,onQueryChange:S,onPaste:oe}})||e(Ze,{ref:$,readonly:X||g,autoFocus:f,placeholder:x,minRows:d,maxRows:m,onSubmit:ae,onFocusedChange:U,onQueryChange:S,onPaste:oe}),t(at,{children:[b,n&&e(a,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:t(qe,{disabled:P.length>=6,onClick:()=>C.current?.click(),children:[e("input",{onChange:ie,multiple:!0,accept:_.join(","),ref:C,type:"file",hidden:!0}),e("i",{className:"bi bi-file-earmark-arrow-up"})]})}),i&&e(Ie,{ref:j,model:i.model,onResult:e=>{$.current?.setContent(t=>t+e+" ")}}),e(qe,{$fill:X&&w,className:X&&w?"text-danger":"text-primary",disabled:!R&&0===se.length&&!X||X&&!w,onClick:e=>{e.preventDefault(),X&&w?W():ae()},children:e("i",X&&w?{className:"bi bi-square-fill fs-7"}:{className:"bi bi-send-fill"})})]})]})]})]})});const rt=n.div`
|
|
188
|
+
`;var tt=A(function({className:r,fileTypes:n,speech:i,scrollRef:o,variables:s,minRows:d=1,maxRows:m=5,toolbar:h,toolbarActions:p,disabled:g,autoFocus:f,tools:b,renderEditableInput:y,placeholder:x="请输入你的问题, Enter+Shift换行, Enter发送",supportStop:w=!1,onEvent:k,onNewConversation:E},N){const C=M(null),j=M(null),$=M(null),[R,S]=z(""),[A,D]=z(""),[P,T]=v([]),[B,U]=z(!1),_=n&&n.length>0?n:Ee,H=F(()=>{requestAnimationFrame(()=>{$.current?.focus()})},[]),{send:J,resume:K,stop:W}=function(e){const{setLoading:t,request:r}=je(),n=$e(),i=n?.setMessages,o=n?.setConversationId,s=n?.conversationId,a=n?.messages,d=!0===e?.supportStop,{onEvent:u,onNewConversation:m,onComplete:h}=e??{},p=M(null),g=M(),f=M(s),b=()=>(p.current&&p.current.abort(),p.current=new AbortController,p.current.signal),v=F(e=>{const t=(e,r)=>{Object.entries(r).forEach(([r,n])=>{if("string"!=typeof n)if(!n||"object"!=typeof n||Array.isArray(n))e[r]=n;else if("index"in n){const{index:i,...o}=n;Array.isArray(e[r])||(e[r]=[]),e[r][i]||(e[r][i]={}),t(e[r][i],o)}else e[r]=n;else e[r]="string"==typeof e[r]?e[r]+n:n})};if(e.data)if("[DONE]"!=e.data)try{const r=JSON.parse(e.data);r.conversation?(f.current=r.conversation,o?.(r.conversation)):r.event?u?.(r.event):i?.(e=>{const n=e[e.length-1];if(r.chunks){if(n.chunks){const e=r.chunks.index;if(!r.chunks.init&&n.chunks[e]||(n.chunks[e]={content:"",reasoning:"",tools:[]}),r.chunks.error)n.chunks[e].error=r.chunks.error;else if(r.chunks.tools){n.chunks[e].tools||(n.chunks[e].tools=[]);const{index:i,...o}=r.chunks.tools;n.chunks[e].tools[i]||(n.chunks[e].tools[i]={name:"",title:"",arguments:""}),t(n.chunks[e].tools[i],o)}else if(r.chunks.reasoning)n.chunks[e].reasoning+=r.chunks.reasoning;else if(r.chunks.content)if("object"==typeof r.chunks.content){Array.isArray(n.chunks[e].content)||(n.chunks[e].content=[]);const t=r.chunks.content.index,i=r.chunks.content.value;"string"==typeof i?(n.chunks[e].content[t]||(n.chunks[e].content[t]=""),n.chunks[e].content[t]+=i):n.chunks[e].content[t]=i}else n.chunks[e].content+=r.chunks.content;else r.chunks.node&&(n.chunks[e].node={...n.chunks[e].node,...r.chunks.node})}}else r.stats?n.stats=r.stats:r.id&&(n.id=r.id)})}catch(e){console.error(e)}else i?.(e=>{e[e.length-1].loading=!1})},[u]),y=F(e=>{if(l(e))i?.(e=>{const t=e[e.length-1];t&&(t.loading=!1)});else{let t="未知错误";c(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),i?.(e=>{const r=e[e.length-1];r.chunks&&(0===r.chunks.length?r.chunks=[{error:t}]:r.chunks[r.chunks.length-1].error=t),r.loading=!1})}},[]),x=F(async e=>{g.current=e;try{const n=b();t(!0),i?.(e=>{e[e.length-1].loading=!0,delete e[e.length-1].stats});let o={url:"message",method:"get",params:{id:e},onMessage:v,signal:n};await r(o)}catch(e){y(e)}finally{p.current=null,g.current=void 0}t(!1),h?.()},[r,u,m,h]),w=F(async(e,...o)=>{"string"==typeof e&&(e={query:e,...o});const{query:s,files:a=[],variables:l={},extra:c={},quote:d}=e;let u=null,g=null;if(s||a.length>0){const e=b();t(!0),n?.setSuggestions?.([]),i?.(e=>{e.push({query:s,quote:d,files:a,variables:l,chunks:[],loading:!0})});try{let t={method:"post",data:{query:s,quote:d,files:a,variables:l,extra:c,conversation:f.current},onMessage:v,signal:e};const n=await r(t);u=n.message??null,g=n.conversation??null}catch(e){y(e)}finally{p.current=null}if(u){let e=!1;g&&!f.current&&(e=m?.(g)??!1),e||await x(u.id)}else t(!1),h?.()}},[r,u,m,h]),k=F(async e=>{let n=null;try{const o=b();t(!0),i?.(e=>{e[e.length-1].loading=!0});let s={url:"resume",method:"post",data:{conversation:f.current,chunk:e.chunk,tool:e.tool,payload:e.payload},onMessage:v,signal:o};n=(await r(s)).message??null}catch(e){y(e)}finally{p.current=null}n?await x(n.id):(t(!1),h?.())},[r,u,m,h]),E=F(async()=>{if(d)if(g.current)try{await r({url:"stop",method:"post",data:{id:g.current}})}catch{}else p.current?.abort()},[r,d]),N=()=>{const e=a?.[a.length-1];e&&e.id&&2===e.status&&x(e.id)};return q(()=>(N(),()=>{p.current&&p.current.abort()}),[]),q(()=>{s!=f.current&&(f.current=s,p.current&&p.current.abort(),N())},[s]),{send:w,resume:k,stop:E}}({supportStop:w,onEvent:k,onNewConversation:E,onComplete:H}),{loading:X,request:V,inputRef:Y}=je(),G=$e(),[Z,ee]=z(),[te,re]=v({});q(()=>{const e=(G?.messages||[]).filter(e=>!!e.query);ee(e.at(-1)?.variables)},[G?.messages]),q(()=>{!n&&P.length>0&&T([])},[n]),q(()=>{const e=o?.current;if(e){const t=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const t={query:e.target.text};s&&(t.variables={...Z,...s.values}),J(t)}};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[s,Z]);const ne=F(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=>!(!_.includes("*")&&!_.includes(O.extname(e.name)))&&!(e.size>20971520)).slice(0,6-P.length);return 0!==t.length&&(T(e=>{e.push(...t.map(e=>({file:e})))}),t.forEach(async e=>{try{const t=new FormData;t.set("file",e);const r=await V({method:"POST",url:"/upload",data:t});T(t=>{const n=t.find(t=>t.file===e);n&&(n.path=r.path)})}catch(t){const r=c(t)?t.message:"unknown error";T(t=>{const n=t.find(t=>t.file===e);n&&(n.error=r)})}}),!0)},[P.length,_,V,T]),ie=F(({target:e})=>{e.files&&ne(Array.from(e.files)),e.value=""},[ne]),oe=F(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&&ne(n)&&e.preventDefault()},[ne]),se=P.flatMap(({file:e,path:t})=>t?[{name:e.name,size:e.size,path:t}]:[]),ae=F(()=>{if(R||se.length>0){const e={query:R,files:se,extra:te};s&&(e.variables={...Z,...s.values}),A&&(e.quote=A),J(e),$.current?.setContent(""),D(""),T([]),j.current?.stop(!0)}},[R,se,s,Z,te,A]),le=I(()=>{const e={setQuery(e){$.current?.setContent(e)},setQuote:D,setExtra:re,send:J,resume:K,focus(){$.current?.focus()}};return Y.current=e,e},[D,re,J,K]);if(L(N,()=>le,[le]),s){const t=s.config.filter(e=>!(e.key in(s.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(Z))return e(nt,{children:e(rt,{children:e(Fe,{values:Z,variables:t,onSubmit:({formData:e})=>{if(r(e,!0))return ee(e),!0}})})})}return t("div",{className:r,children:[h&&e(Ve,{actions:p,children:h}),t(ct,{$focused:B&&!X,children:[P.length>0&&e(ot,{children:P.map(({file:t,error:r,path:n},i)=>{const o=void 0===r&&void 0===n;return e(it,{name:t.name,size:t.size,error:r,loading:o,onRemove:()=>{T(e=>{const r=e.findIndex(e=>e.file===t);-1!==r&&e.splice(r,1)})}},i)})}),!!A&&t(lt,{children:[e("i",{className:"bi bi-quote"}),e("p",{children:A}),e(Q,{onClick:()=>D("")})]}),t(st,{$minRows:d,children:[y?.({Component:Ze,props:{ref:$,readonly:X||g,autoFocus:f,placeholder:x,minRows:d,maxRows:m,onSubmit:ae,onFocusedChange:U,onQueryChange:S,onPaste:oe}})||e(Ze,{ref:$,readonly:X||g,autoFocus:f,placeholder:x,minRows:d,maxRows:m,onSubmit:ae,onFocusedChange:U,onQueryChange:S,onPaste:oe}),t(at,{children:[b,n&&e(a,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:t(qe,{disabled:P.length>=6,onClick:()=>C.current?.click(),children:[e("input",{onChange:ie,multiple:!0,accept:_.join(","),ref:C,type:"file",hidden:!0}),e("i",{className:"bi bi-file-earmark-arrow-up"})]})}),i&&e(Ie,{ref:j,model:i.model,onResult:e=>{$.current?.setContent(t=>t+e+" ")}}),e(qe,{$fill:X&&w,className:X&&w?"text-danger":"text-primary",disabled:!R&&0===se.length&&!X||X&&!w,onClick:e=>{e.preventDefault(),X&&w?W():ae()},children:e("i",X&&w?{className:"bi bi-square-fill fs-7"}:{className:"bi bi-send-fill"})})]})]})]})]})});const rt=n.div`
|
|
189
189
|
border-radius: var(--bs-border-radius-lg);
|
|
190
190
|
box-shadow: var(--bs-box-shadow-sm);
|
|
191
191
|
margin: 1rem;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.23",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prebuild": "rimraf es types",
|
|
6
6
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@babel/runtime": "^7.11.2",
|
|
20
20
|
"@breezystack/lamejs": "^1.2.7",
|
|
21
|
-
"@topthink/components": "1.2.
|
|
21
|
+
"@topthink/components": "1.2.17",
|
|
22
22
|
"@types/hast": "^3.0.4",
|
|
23
23
|
"@types/mdast": "^4.0.4",
|
|
24
24
|
"eventemitter3": "^5.0.1",
|
|
@@ -71,5 +71,5 @@
|
|
|
71
71
|
},
|
|
72
72
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
73
73
|
"license": "MIT",
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "7ef386bf3e64bcb8e7432170e99c06b2432537f0"
|
|
75
75
|
}
|