@topthink/chat 1.1.35 → 1.1.36
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 +2 -2
- package/package.json +2 -2
package/es/index.js
CHANGED
|
@@ -64,7 +64,7 @@ import{jsxs as e,jsx as r,Fragment as t}from"react/jsx-runtime";import{styled as
|
|
|
64
64
|
${e=>e.$error&&i`
|
|
65
65
|
border-color: var(--bs-danger) !important;
|
|
66
66
|
`}
|
|
67
|
-
`,se=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"],le=w(null);function ce(e){const r=E(le);if(!r)throw new Error("useContext must be used within a Provider");const{setHandles:t}=r;return $((()=>{if(e)return t(e),()=>{t({})}}),[]),r}function de(e){let{children:t,...n}=e;const[i,o]=N({});return r(le.Provider,{value:{...n,handles:i,setHandles:o},children:t})}function he(){const{setMessages:e,setSuggestions:r,setLoading:t,conversationId:n,setConversationId:i,request:o,handles:a}=ce(),s=j(null),c=j(n);$((()=>{
|
|
67
|
+
`,se=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"],le=w(null);function ce(e){const r=E(le);if(!r)throw new Error("useContext must be used within a Provider");const{setHandles:t}=r;return $((()=>{if(e)return t(e),()=>{t({})}}),[e]),r}function de(e){let{children:t,...n}=e;const[i,o]=N({});return r(le.Provider,{value:{...n,handles:i,setHandles:o},children:t})}function he(){const{setMessages:e,setSuggestions:r,setLoading:t,conversationId:n,setConversationId:i,request:o,handles:a}=ce(),s=j(null),c=j(!1),d=j(n);$((()=>{d.current=n}),[n]);const{onSuccess:h,onComplete:u}=a,m=C((r=>{if(r.data)if("[DONE]"!=r.data)try{const t=JSON.parse(r.data);t.conversation?(d.current=t.conversation,i(t.conversation)):e((e=>{const r=e[e.length-1];if(t.chunks){if(r.chunks){const e=t.chunks.index;if(r.chunks[e]||(r.chunks[e]={content:"",reasoning:"",tools:[]}),t.chunks.error)r.chunks[e].error=t.chunks.error;else if(t.chunks.tools){r.chunks[e].tools||(r.chunks[e].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 if(t.chunks.reasoning)r.chunks[e].reasoning+=t.chunks.reasoning;else if(t.chunks.content)if("object"==typeof t.chunks.content){Array.isArray(r.chunks[e].content)||(r.chunks[e].content=[]);const n=t.chunks.content.index,i=t.chunks.content.value;"string"==typeof i?(r.chunks[e].content[n]||(r.chunks[e].content[n]=""),r.chunks[e].content[n]+=i):r.chunks[e].content[n]=i}else r.chunks[e].content+=t.chunks.content;else t.chunks.node&&(r.chunks[e].node={...r.chunks[e].node,...t.chunks.node})}}else t.stats?r.stats=t.stats:t.id?r.id=t.id:t.suspend&&(c.current=!0)}))}catch(e){console.error(e)}else e((e=>{e[e.length-1].loading=!1}))}),[]),p=C((r=>{let t="未知错误";l(r)&&(t=401==r.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof r.errors?r.errors:Object.values(r.errors).join("\n")),e((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}))}),[]),g=C((async function(n){if("string"==typeof n){for(var i=arguments.length,a=new Array(i>1?i-1:0),l=1;l<i;l++)a[l-1]=arguments[l];n={query:n,...a}}const{query:g,files:b=[],variables:f={},quote:v}=n;if(g||b.length>0){s.current&&s.current.abort(),c.current=!1,t(!0),r([]),e((e=>{e.push({query:g,quote:v,files:b,variables:f,chunks:[],loading:!0})}));try{let e={method:"post",data:{query:g,quote:v,files:b,variables:f,conversation:d.current},onMessage:m};await o(e),c.current||h?.()}catch(e){p(e)}t(!1),u?.()}}),[o,a]);return{send:g,resume:C((async r=>{try{s.current&&s.current.abort(),c.current=!1,t(!0),e((e=>{e[e.length-1].loading=!0}));let n={url:"resume",method:"post",data:{conversation:d.current,chunk:r.chunk,payload:r.payload},onMessage:m};await o(n),c.current||h?.()}catch(e){p(e)}t(!1),u?.()}),[o,a]),suggest:C((async()=>{s.current=new AbortController;try{let e={method:"post",url:"/suggestion",data:{conversation:d.current},signal:s.current.signal};const t=await o(e);r(t)}catch{}finally{s.current=null}}),[o])}}function ue(e){let{variables:t,values:n,onSubmit:i,onChange:o,children:a}=e;const s={},l=[],d={};return t.forEach((e=>{s[e.key]={type:"string",title:e.label},"textarea"===e.type?d[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&&l.push(e.key)})),r(c,{schema:{type:"object",properties:s,required:l},formData:n,omitExtraData:!0,uiSchema:d,submitText:"开始对话",onSubmit:i,onChange:o,children:a})}const me=n.button`
|
|
68
68
|
color: var(--bs-secondary);
|
|
69
69
|
cursor: pointer;
|
|
70
70
|
display: flex;
|
|
@@ -166,7 +166,7 @@ import{jsxs as e,jsx as r,Fragment as t}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(t,n){let{suggestion:i,fileTypes:o,speech:s,scrollRef:c,variables:h,minRows:u,toolbar:m,disabled:p,autoFocus:b,placeholder:f="请输入你的问题, Enter+Shift换行, Enter发送"}=t;const v=j(null),x=j(null),y=j(null),[k,w]=N(""),[E,M]=N(""),[
|
|
169
|
+
`;var Ae=M((function(t,n){let{suggestion:i,fileTypes:o,speech:s,scrollRef:c,variables:h,minRows:u,toolbar:m,disabled:p,autoFocus:b,placeholder:f="请输入你的问题, Enter+Shift换行, Enter发送"}=t;const v=j(null),x=j(null),y=j(null),[k,w]=N(""),[E,M]=N(""),[q,S]=g([]),[I,L]=N(!1),D=o&&o.length>0?o:se,{send:O,resume:T,suggest:B}=he(),_=F((()=>({onSuccess(){i&&B()},onComplete(){requestAnimationFrame((()=>{v.current?.focus()}))}})),[i]),{messages:U,loading:H,request:J}=ce(_),[K,Q]=N();$((()=>{const e=U.filter((e=>!!e.query));Q(e.at(-1)?.variables)}),[U]),$((()=>{!o&&q.length>0&&S([])}),[o]),$((()=>{const e=c.current;if(e&&O){const r=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const r={query:e.target.text};h&&(r.variables={...K,...h.values}),O(r)}};return e.addEventListener("click",r),()=>{e.removeEventListener("click",r)}}}),[h,K]);const W=C((e=>{let{target:r}=e;if(r.files){const e=Array.from(r.files).filter((e=>!!D.includes(A.extname(e.name))&&(!(e.size>20971520)&&e))).slice(0,6-q.length);S((r=>{r.push(...e.map((e=>({file:e}))))})),e.forEach((async e=>{try{const r=new FormData;r.set("file",e);const t=await J({method:"POST",url:"/upload",data:r});S((r=>{const n=r.find((r=>r.file===e));n&&(n.path=t.path)}))}catch(r){const t=l(r)?r.message:"unknown error";S((r=>{const n=r.find((r=>r.file===e));n&&(n.error=t)}))}}))}r.value=""}),[q,D]),X=q.flatMap((e=>{let{file:r,path:t}=e;return t?[{name:r.name,size:r.size,path:t}]:[]})),V=C((()=>{if(k||X.length>0){const e={query:k,files:X};h&&(e.variables={...K,...h.values}),E&&(e.quote=E),O(e),w(""),M(""),S([]),y.current?.stop(!0)}}),[k,X,h,K,E]);if(z(n,(()=>({setQuery:w,setQuote:M,send:O,resume:T,focus(){v.current?.focus()}}))),h){const e=h.config.filter((e=>!(e.key in(h.values||{})))),t=function(r){let t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return!!r&&e.every((e=>{const n=!e.required||r?.[e.key];return!n&&t&&d.error("请完善必填信息"),n}))};if(e.length>0&&!t(K))return r(Le,{children:r(Ie,{children:r(ue,{values:K,variables:e,onSubmit:e=>{let{formData:r}=e;if(t(r,!0))return Q(r),!0}})})})}return e(_e,{children:[m&&r(qe,{children:m}),e(Be,{$focused:I&&!H,children:[q.length>0&&r(Pe,{children:q.map(((e,t)=>{let{file:n,error:i,path:o}=e;const a=void 0===i&&void 0===o;return r(De,{name:n.name,size:n.size,error:i,loading:a,onRemove:()=>{S((e=>{const r=e.findIndex((e=>e.file===n));-1!==r&&e.splice(r,1)}))}},t)}))}),!!E&&e(Te,{children:[r("i",{className:"bi bi-quote"}),r("p",{children:E}),r(P,{onClick:()=>M("")})]}),e(Oe,{children:[r(R,{disabled:H||p,ref:v,placeholder:f,minRows:u,maxRows:5,value:k,onChange:e=>w(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),V())},autoFocus:b,onFocus:()=>L(!0),onBlur:()=>L(!1)}),o&&r(a,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:e(me,{disabled:q.length>=6,onClick:()=>x.current?.click(),children:[r("input",{onChange:W,multiple:!0,accept:D.join(","),ref:x,type:"file",hidden:!0}),r("i",{className:"bi bi-file-earmark-arrow-up"})]})}),s&&r(be,{ref:y,model:s.model,onResult:e=>{v.current?.focus(),w((r=>r+e+" "))}}),r(me,{className:"text-primary",disabled:!k&&0===X.length,onClick:e=>{e.preventDefault(),V()},children:r("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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.36",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prebuild": "rimraf es types",
|
|
6
6
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
},
|
|
68
68
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
69
69
|
"license": "MIT",
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "8c15e5e12e16b56e3bee47322959f3e5364e54ba"
|
|
71
71
|
}
|