@topthink/chat 1.0.9 → 1.0.11

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.
Files changed (2) hide show
  1. package/es/index.js +13 -13
  2. package/package.json +3 -3
package/es/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import*as e from"react";import{useCallback as r,useState as t,useImperativeHandle as o,memo as n,useRef as i,Fragment as s,useEffect as a}from"react";import{useImmer as l,request as c,isRequestError as d,Lightbox as m,styled as h,Clipboard as p,css as u,formatSize as g,Tooltip as f,useAsync as b,isImageUrl as v,getAbsoluteUrl as x}from"@topthink/components";import{jsx as y,jsxs as k,Fragment as w}from"react/jsx-runtime";import j from"react-markdown";import"katex/dist/katex.min.css";import N from"remark-math";import z from"remark-breaks";import $ from"remark-gfm";import M from"rehype-katex";import E from"rehype-highlight";import{Spinner as q}from"react-bootstrap";import*as C from"path";import F from"path";import P from"react-textarea-autosize";function R(e){let{url:n,transformRequest:i,onboarding:s,onSuccess:a,ref:m,...h}=e;const p=r((e=>{const r=[];if(s){let e=s.questions.filter((e=>!!e.trim()));if(e.length>3){const r=[...e];let t=e.length-3;for(;t--;){let e=Math.floor(Math.random()*r.length);r.splice(e,1)}e=r}r.push({chunks:[{content:[s.prologue,...e.map((e=>`[${e}](#!question)`))].join("\n"),tools:[]}]})}return e?r.concat(e.messages):r}),[s]),[u,g]=l((()=>p(h.conversation))),[f,b]=t(!1),[v,x]=t(h.conversation?.id),y=r((async function(e){let r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(e||r.length>0){b(!0),g((t=>{t.push({query:e,files:r,chunks:[],loading:!0})}));let t=0,o="";try{await c({method:"post",url:n,data:{query:e,files:r,conversation:v},transformRequest:i,onDownloadProgress:e=>{const r=e.target,{responseText:n}=r;for(;t<e.loaded;){const e=n.substring(t,++t);if(o+=e,"\n"===e){const e=o.match(/^data:(?<data>.*)/);if(e){const r=e.groups.data.trim();if("[DONE]"!=r)try{const e=JSON.parse(r);e.conversation?x(e.conversation):g((r=>{const t=r[r.length-1];if(t.chunks)if(e.chunks){const r=e.chunks.index;if(t.chunks[r]||(t.chunks[r]={content:"",tools:[]}),e.chunks.error)t.chunks[r].error=e.chunks.error;else if(e.chunks.tools){const o=e.chunks.tools.index;"response"in e.chunks.tools?(t.chunks[r].tools[o].response=e.chunks.tools.response,t.chunks[r].tools[o].error=e.chunks.tools.error):"content"in e.chunks.tools?t.chunks[r].tools[o].content=e.chunks.tools.content:t.chunks[r].tools[o]={name:e.chunks.tools.name,title:e.chunks.tools.title,arguments:e.chunks.tools.arguments}}else e.chunks.content&&(t.chunks[r].content+=e.chunks.content)}else e.stats&&(t.stats=e.stats)}))}catch(e){console.error(e)}else g((e=>{e[e.length-1].loading=!1}))}o=""}}}})}catch(e){let r="未知错误";d(e)&&(r=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),g((e=>{const t=e[e.length-1];t.chunks&&(0===t.chunks.length?t.chunks=[{content:`[${r}]`,tools:[]}]:t.chunks[t.chunks.length-1].content=`[${r}]`),t.loading=!1}))}b(!1),a?.()}}),[i,v]),k=r((e=>{f||(x(e?.id),g(p(e)))}),[f,p]);return o(m,(()=>({reset:k,send:y}))),{messages:u,loading:f,send:y,reset:k}}const T=m.Image,_=n((e=>{let{content:r,components:t}=e;return r=r.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$"),y(A,{remarkPlugins:[[N,{singleDollarTextMath:!1}],$,z],rehypePlugins:[M,[E,{detect:!1,ignoreMissing:!0}]],components:{pre:e=>{let{children:r}=e;const t=i(null);return k(O,{ref:t,children:[y(D,{tooltip:!1,content:()=>{const e=t.current?.getElementsByTagName("code");return e?.[0]?.innerText||""}}),r]})},a:e=>{let{node:r,href:t,...o}=e;const n=t?.startsWith("http")?"_blank":"_self";return y("a",{...o,href:t,target:n})},img:e=>{let{node:r,...t}=e;return y(T,{...t})},...t},children:r})})),D=h(p)`
1
+ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandle as o,memo as n,useRef as i,Fragment as a,useEffect as s}from"react";import{useImmer as l,request as c,isRequestError as d,Lightbox as m,styled as h,Clipboard as p,css as u,formatSize as g,Tooltip as f,useAsync as b,isImageUrl as x,getAbsoluteUrl as v}from"@topthink/components";import{jsx as y,jsxs as k,Fragment as w}from"react/jsx-runtime";import j from"react-markdown";import"katex/dist/katex.min.css";import N from"remark-math";import z from"remark-breaks";import $ from"remark-gfm";import M from"rehype-katex";import E from"rehype-highlight";import{Spinner as q}from"react-bootstrap";import*as C from"path";import F from"path";import R from"react-textarea-autosize";function P(e){let{url:n,transformRequest:i,onboarding:a,onSuccess:s,ref:m,...h}=e;const p=r((e=>{const r=[];if(a){let e=a.questions.filter((e=>!!e.trim()));if(e.length>3){const r=[...e];let t=e.length-3;for(;t--;){let e=Math.floor(Math.random()*r.length);r.splice(e,1)}e=r}r.push({chunks:[{content:[a.prologue,...e.map((e=>`[${e}](#!question)`))].join("\n"),tools:[]}]})}return e?r.concat(e.messages):r}),[a]),[u,g]=l((()=>p(h.conversation))),[f,b]=t(!1),[x,v]=t(h.conversation?.id),y=r((async function(e){let r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(e||r.length>0){b(!0),g((t=>{t.push({query:e,files:r,chunks:[],loading:!0})}));try{await c({method:"post",url:n,data:{query:e,files:r,conversation:x},transformRequest:i,onMessage:e=>{if(e.data)if("[DONE]"!=e.data)try{const r=JSON.parse(e.data);r.conversation?v(r.conversation):g((e=>{const t=e[e.length-1];if(t.chunks)if(r.chunks){const e=r.chunks.index;if(t.chunks[e]||(t.chunks[e]={content:"",tools:[]}),r.chunks.error)t.chunks[e].error=r.chunks.error;else if(r.chunks.tools){const o=r.chunks.tools.index;"response"in r.chunks.tools?(t.chunks[e].tools[o].response=r.chunks.tools.response,t.chunks[e].tools[o].error=r.chunks.tools.error):"content"in r.chunks.tools?t.chunks[e].tools[o].content=r.chunks.tools.content:t.chunks[e].tools[o]={name:r.chunks.tools.name,title:r.chunks.tools.title,arguments:r.chunks.tools.arguments}}else r.chunks.content&&(t.chunks[e].content+=r.chunks.content)}else r.stats&&(t.stats=r.stats)}))}catch(e){console.error(e)}else g((e=>{e[e.length-1].loading=!1}))}})}catch(e){let r="未知错误";d(e)&&(r=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),g((e=>{const t=e[e.length-1];t.chunks&&(0===t.chunks.length?t.chunks=[{content:`[${r}]`,tools:[]}]:t.chunks[t.chunks.length-1].content=`[${r}]`),t.loading=!1}))}b(!1),s?.()}}),[i,x]),k=r((e=>{f||(v(e?.id),g(p(e)))}),[f,p]);return o(m,(()=>({reset:k,send:y}))),{messages:u,loading:f,send:y,reset:k}}const _=m.Image,O=n((e=>{let{content:r,components:t}=e;return r=r.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$"),y(A,{remarkPlugins:[[N,{singleDollarTextMath:!1}],$,z],rehypePlugins:[M,[E,{detect:!1,ignoreMissing:!0}]],components:{pre:e=>{let{children:r}=e;const t=i(null);return k(D,{ref:t,children:[y(T,{tooltip:!1,content:()=>{const e=t.current?.getElementsByTagName("code");return e?.[0]?.innerText||""}}),r]})},a:e=>{let{node:r,href:t,...o}=e;const n=t?.startsWith("http")?"_blank":"_self";return y("a",{...o,href:t,target:n})},img:e=>{let{node:r,...t}=e;return y(_,{...t})},...t},children:r})})),T=h(p)`
2
2
  position: absolute;
3
3
  right: 10px;
4
4
  top: 1em;
@@ -18,11 +18,11 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
18
18
  &:hover {
19
19
  opacity: 1;
20
20
  }
21
- `,O=h.pre`
21
+ `,D=h.pre`
22
22
  position: relative;
23
23
 
24
24
  &:hover {
25
- ${D} {
25
+ ${T} {
26
26
  pointer-events: all;
27
27
  transform: translateX(0px);
28
28
  opacity: 0.5;
@@ -323,9 +323,9 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
323
323
  max-height: 100%;
324
324
  max-width: 100%;
325
325
  }
326
- `;function I(e){let{tool:r,showProcess:o}=e;const[n,i]=t(!1);let a=null;r.content&&("string"==typeof r.content?a=y(_,{content:r.content}):"image"===r.content.type&&(a=y(B,{src:r.content.image})));const l="response"in r;return k(s,{children:["all"==o&&y("div",{className:"mb-2",children:k("div",n&&l?{className:"shadow-sm rounded bg-white fs-7",children:[k("div",{onClick:()=>i(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[r.error?y("i",{className:"bi bi-x-circle-fill text-danger"}):y("i",{className:"bi bi-check-circle-fill text-success"}),y("span",{className:"text-muted",children:"已使用"}),y("span",{children:r.title}),y("i",{className:"bi bi-caret-up-fill text-muted"})]}),k("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[k("div",{className:"border rounded bg-light",children:[y("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:y("span",{className:"text-muted",children:"参数"})}),y("div",{className:"border-top p-2 overflow-hidden",children:r.arguments})]}),k("div",{className:"border rounded bg-light",children:[y("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:y("span",{className:"text-muted",children:"响应"})}),y(L,{className:"border-top p-2 overflow-hidden",children:r.response||"None"})]})]})]}:{onClick:()=>i(l),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[l?r.error?y("i",{className:"bi bi-x-circle-fill text-danger"}):y("i",{className:"bi bi-check-circle-fill text-success"}):y(q,{animation:"border",variant:"primary",size:"sm"}),y("span",{className:"text-muted",children:l?"已使用":"正在使用"}),y("span",{children:r.title}),l&&y("i",{className:"bi bi-caret-down-fill text-muted"})]})}),a]})}const L=h.div`
326
+ `;function I(e){let{tool:r,showProcess:o}=e;const[n,i]=t(!1);let s=null;r.content&&("string"==typeof r.content?s=y(O,{content:r.content}):"image"===r.content.type&&(s=y(B,{src:r.content.image})));const l="response"in r;return k(a,{children:["stats"==o&&y("div",{className:"mb-2",children:k("div",{role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[l?r.error?y("i",{className:"bi bi-x-circle-fill text-danger"}):y("i",{className:"bi bi-check-circle-fill text-success"}):y(q,{animation:"border",variant:"primary",size:"sm"}),y("span",{className:"text-muted",children:l?"已使用":"正在使用"}),y("span",{children:r.title})]})}),"all"==o&&y("div",{className:"mb-2",children:k("div",n&&l?{className:"shadow-sm rounded bg-white fs-7",children:[k("div",{onClick:()=>i(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[r.error?y("i",{className:"bi bi-x-circle-fill text-danger"}):y("i",{className:"bi bi-check-circle-fill text-success"}),y("span",{className:"text-muted",children:"已使用"}),y("span",{children:r.title}),y("i",{className:"bi bi-caret-up-fill text-muted"})]}),k("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[k("div",{className:"border rounded bg-light",children:[y("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:y("span",{className:"text-muted",children:"参数"})}),y("div",{className:"border-top p-2 overflow-hidden",children:r.arguments})]}),k("div",{className:"border rounded bg-light",children:[y("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:y("span",{className:"text-muted",children:"响应"})}),y(L,{className:"border-top p-2 overflow-hidden",children:r.response||"None"})]})]})]}:{onClick:()=>i(l),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[l?r.error?y("i",{className:"bi bi-x-circle-fill text-danger"}):y("i",{className:"bi bi-check-circle-fill text-success"}):y(q,{animation:"border",variant:"primary",size:"sm"}),y("span",{className:"text-muted",children:l?"已使用":"正在使用"}),y("span",{children:r.title}),l&&y("i",{className:"bi bi-caret-down-fill text-muted"})]})}),s]})}const L=h.div`
327
327
  white-space: pre-wrap;
328
- `;var H,K,X;function J(){return J=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},J.apply(this,arguments)}const W=r=>e.createElement("svg",J({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},r),H||(H=e.createElement("circle",{cx:15,cy:15,r:15,fill:"#3c60ff"},e.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),e.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))),K||(K=e.createElement("circle",{cx:60,cy:15,r:9,fill:"#3c60ff",fillOpacity:.3},e.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:9,repeatCount:"indefinite",to:9,values:"9;15;9"}),e.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:.5,repeatCount:"indefinite",to:.5,values:".5;1;.5"}))),X||(X=e.createElement("circle",{cx:105,cy:15,r:15,fill:"#3c60ff"},e.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),e.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))));const G=function(e){let{ext:r}=e;const{result:t}=b((async e=>(await function(e){switch(e){case"../../images/file/docx.svg":return import("./docx-nWXnlyT_.js");case"../../images/file/jpg.svg":return import("./jpg-6tBxSzWk.js");case"../../images/file/md.svg":return import("./md-GZ3HJPcw.js");case"../../images/file/pdf.svg":return import("./pdf-pVX1_E6T.js");case"../../images/file/png.svg":return import("./png-u0o1NMqQ.js");case"../../images/file/pptx.svg":return import("./pptx-Hprz0cON.js");case"../../images/file/txt.svg":return import("./txt-eHeCpvNO.js");case"../../images/file/xlsx.svg":return import("./xlsx-uOft-SV2.js");default:return new Promise((function(r,t){("function"==typeof queueMicrotask?queueMicrotask:setTimeout)(t.bind(null,new Error("Unknown variable dynamic import: "+e)))}))}}(`../../images/file/${e}.svg`)).default),[r]);return t?y("img",{width:30,height:30,src:t}):null};function Q(e){let{className:r,name:t,size:o,loading:n,error:i,onRemove:s}=e;const a=F.extname(t).substring(1),l=k(ee,{className:r,$error:!!i,children:[k(Z,{children:[y(G,{ext:a}),n&&y(q,{variant:"primary"})]}),k(Y,{children:[y("h4",{children:t}),y("p",{children:g(o,1)})]}),s&&y(V,{onClick:e=>{e.preventDefault(),s()},children:y("i",{className:"bi bi-trash3"})})]});return i?y(f,{placement:"top",tooltip:i,children:l}):l}const V=h.div`
328
+ `;var H,K,X;function J(){return J=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},J.apply(this,arguments)}const W=r=>e.createElement("svg",J({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},r),H||(H=e.createElement("circle",{cx:15,cy:15,r:15,fill:"#3c60ff"},e.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),e.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))),K||(K=e.createElement("circle",{cx:60,cy:15,r:9,fill:"#3c60ff",fillOpacity:.3},e.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:9,repeatCount:"indefinite",to:9,values:"9;15;9"}),e.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:.5,repeatCount:"indefinite",to:.5,values:".5;1;.5"}))),X||(X=e.createElement("circle",{cx:105,cy:15,r:15,fill:"#3c60ff"},e.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),e.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))));const G=function(e){let{ext:r}=e;const{result:t}=b((async e=>(await function(e){switch(e){case"../../images/file/docx.svg":return import("./docx-nWXnlyT_.js");case"../../images/file/jpg.svg":return import("./jpg-6tBxSzWk.js");case"../../images/file/md.svg":return import("./md-GZ3HJPcw.js");case"../../images/file/pdf.svg":return import("./pdf-pVX1_E6T.js");case"../../images/file/png.svg":return import("./png-u0o1NMqQ.js");case"../../images/file/pptx.svg":return import("./pptx-Hprz0cON.js");case"../../images/file/txt.svg":return import("./txt-eHeCpvNO.js");case"../../images/file/xlsx.svg":return import("./xlsx-uOft-SV2.js");default:return new Promise((function(r,t){("function"==typeof queueMicrotask?queueMicrotask:setTimeout)(t.bind(null,new Error("Unknown variable dynamic import: "+e)))}))}}(`../../images/file/${e}.svg`)).default),[r]);return t?y("img",{width:30,height:30,src:t}):null};function Q(e){let{className:r,name:t,size:o,loading:n,error:i,onRemove:a}=e;const s=F.extname(t).substring(1),l=k(ee,{className:r,$error:!!i,children:[k(Z,{children:[y(G,{ext:s}),n&&y(q,{variant:"primary"})]}),k(Y,{children:[y("h4",{children:t}),y("p",{children:g(o,1)})]}),a&&y(V,{onClick:e=>{e.preventDefault(),a()},children:y("i",{className:"bi bi-trash3"})})]});return i?y(f,{placement:"top",tooltip:i,children:l}):l}const V=h.div`
329
329
  width: 1.75rem;
330
330
  height: 1.75rem;
331
331
  align-items: center;
@@ -391,7 +391,7 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
391
391
  ${e=>e.$error&&u`
392
392
  border-color: var(--bs-danger) !important;
393
393
  `}
394
- `,re=n((e=>{let{bot:r,user:t,message:o,showProcess:n="none"}=e;const{query:i,files:a=[],chunks:l}=o;return k(w,{children:[(i||a.length>0)&&k(le,{$reverse:!0,children:[t&&y(se,{children:y("img",{src:t.avatar})}),k(ae,{children:[i&&y(_,{content:i}),a.length>0&&a.map(((e,r)=>{let{name:t,size:o,path:n}=e;return v(n)?y(B,{src:x(`/uploads/${n}`)},r):y(te,{children:y(Q,{name:t,size:o})},r)}))]})]}),l&&k(le,{children:[y(se,{children:y("img",{src:r.avatar})}),k(ae,{children:[o.query&&y(ne,{children:y(p,{placement:"top",content:l.reduce((function(e,r){return e+r.content+"\n"+r.tools.reduce(((e,r)=>r.content&&"string"==typeof r.content?e+r.content+"\n":e),"")}),""),as:oe})}),y(ie,{children:["stats","all"].includes(n)&&o.stats&&k(w,{children:[k("span",{children:["耗时 ",o.stats.latency/1e3," 秒"]}),k("span",{children:["花费 Token ",o.stats.usage]})]})}),l.length>0&&l.map(((e,r)=>k(s,{children:[e.content&&y(_,{content:e.content}),e.tools.map(((e,r)=>y(I,{tool:e,showProcess:n},r))),e.error&&y(_,{content:`[${e.error}]`})]},r))),o.loading&&y(W,{})]})]})]})})),te=h.div`
394
+ `,re=n((e=>{let{bot:r,user:t,message:o,showProcess:n="none"}=e;const{query:i,files:s=[],chunks:l}=o;return k(w,{children:[(i||s.length>0)&&k(le,{$reverse:!0,children:[t&&y(ae,{children:y("img",{src:t.avatar})}),k(se,{children:[i&&y(O,{content:i}),s.length>0&&s.map(((e,r)=>{let{name:t,size:o,path:n}=e;return x(n)?y(B,{src:v(`/uploads/${n}`)},r):y(te,{children:y(Q,{name:t,size:o})},r)}))]})]}),l&&k(le,{children:[y(ae,{children:y("img",{src:r.avatar})}),k(se,{children:[o.query&&y(ne,{children:y(p,{placement:"top",content:l.reduce((function(e,r){return e+r.content+"\n"+r.tools.reduce(((e,r)=>r.content&&"string"==typeof r.content?e+r.content+"\n":e),"")}),""),as:oe})}),y(ie,{children:["stats","all"].includes(n)&&o.stats&&k(w,{children:[k("span",{children:["耗时 ",o.stats.latency/1e3," 秒"]}),k("span",{children:["花费 Token ",o.stats.usage]})]})}),l.length>0&&l.map(((e,r)=>k(a,{children:[e.content&&y(O,{content:e.content}),e.tools.map(((e,r)=>y(I,{tool:e,showProcess:n},r))),e.error&&y(O,{content:`[${e.error}]`})]},r))),o.loading&&y(W,{})]})]})]})})),te=h.div`
395
395
  width: 100%;
396
396
  margin-bottom: .5rem;
397
397
  margin-top: .5rem;
@@ -448,7 +448,7 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
448
448
  display: flex;
449
449
  opacity: 0;
450
450
  z-index: 1;
451
- `,se=h.div`
451
+ `,ae=h.div`
452
452
  width: 35px;
453
453
  height: 35px;
454
454
  border-radius: 50%;
@@ -458,7 +458,7 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
458
458
  width: 100%;
459
459
  height: 100%;
460
460
  }
461
- `,ae=h.div`
461
+ `,se=h.div`
462
462
  padding: .875rem;
463
463
  background-color: rgb(243, 243, 243);
464
464
  border-radius: var(--bs-border-radius-lg);
@@ -489,7 +489,7 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
489
489
  ${e=>e.$reverse&&u`
490
490
  flex-direction: row-reverse;
491
491
 
492
- ${ae} {
492
+ ${se} {
493
493
  background: rgba(var(--bs-primary-rgb), 0.1);
494
494
  margin-right: 10px;
495
495
  margin-left: 0;
@@ -504,7 +504,7 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
504
504
  }
505
505
  }
506
506
  `};
507
- `,ce=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"];function de(e){let{chat:o,fileTypes:n,scrollRef:s,onMessages:m,minRows:h,toolbar:p}=e;const u=i(null),g=i(null),[b,v]=t(""),[x,w]=l([]),[j,N]=t(!1),z=n&&n.length>0?n:ce,{loading:$,messages:M,send:E}=R({...o,onSuccess(){requestAnimationFrame((()=>{u.current?.focus()})),o.onSuccess?.()}});a((()=>{m(M)}),[M]),a((()=>{const e=s.current;if(e&&E){const r=e=>{e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash&&(e.preventDefault(),E(e.target.text))};return e.addEventListener("click",r),()=>{e.removeEventListener("click",r)}}}),[s,E]);const q=r((e=>{let{target:r}=e;if(r.files){const e=Array.from(r.files).filter((e=>!!z.includes(C.extname(e.name))&&(!(e.size>20971520)&&e))).slice(0,6-x.length);w((r=>{r.push(...e.map((e=>({file:e}))))})),e.forEach((async e=>{try{const r=new FormData;r.set("file",e);const t=await c({method:"POST",url:`${o.url}/upload`,transformRequest:o.transformRequest,data:r});w((r=>{const o=r.find((r=>r.file===e));o&&(o.path=t.path)}))}catch(r){const t=d(r)?r.message:"unknown error";w((r=>{const o=r.find((r=>r.file===e));o&&(o.error=t)}))}}))}r.value=""}),[x,z]),F=x.flatMap((e=>{let{file:r,path:t}=e;return t?[{name:r.name,size:r.size,path:t}]:[]})),T=()=>{(b||F.length>0)&&(E(b,F),v(""),w([]))};return k(fe,{children:[p,k(ge,{$focused:j,children:[x.length>0&&y(he,{children:x.map(((e,r)=>{let{file:t,error:o,path:n}=e;const i=void 0===o&&void 0===n;return y(me,{name:t.name,size:t.size,error:o,loading:i,onRemove:()=>{w((e=>{const r=e.findIndex((e=>e.file===t));-1!==r&&e.splice(r,1)}))}},r)}))}),k(ue,{children:[y(P,{disabled:$,ref:u,placeholder:"请输入你的问题, Enter+Shift换行, Enter发送",minRows:h,maxRows:5,value:b,onChange:e=>v(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),T())},onFocus:()=>N(!0),onBlur:()=>N(!1)}),n&&y(f,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:k(pe,{disabled:x.length>=6,onClick:()=>g.current?.click(),children:[y("input",{onChange:q,multiple:!0,accept:z.join(","),ref:g,type:"file",hidden:!0}),y("i",{className:"bi bi-file-earmark-arrow-up"})]})}),y(pe,{className:"text-primary",disabled:!b&&0===F.length,onClick:e=>{e.preventDefault(),T()},children:y("i",{className:"bi bi-send-fill"})})]})]})]})}const me=h(Q)`
507
+ `,ce=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"];function de(e){let{chat:o,fileTypes:n,scrollRef:a,onMessages:m,minRows:h,toolbar:p}=e;const u=i(null),g=i(null),[b,x]=t(""),[v,w]=l([]),[j,N]=t(!1),z=n&&n.length>0?n:ce,{loading:$,messages:M,send:E}=P({...o,onSuccess(){requestAnimationFrame((()=>{u.current?.focus()})),o.onSuccess?.()}});s((()=>{m(M)}),[M]),s((()=>{!n&&v.length>0&&w([])}),[n]),s((()=>{const e=a.current;if(e&&E){const r=e=>{e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash&&(e.preventDefault(),E(e.target.text))};return e.addEventListener("click",r),()=>{e.removeEventListener("click",r)}}}),[a,E]);const q=r((e=>{let{target:r}=e;if(r.files){const e=Array.from(r.files).filter((e=>!!z.includes(C.extname(e.name))&&(!(e.size>20971520)&&e))).slice(0,6-v.length);w((r=>{r.push(...e.map((e=>({file:e}))))})),e.forEach((async e=>{try{const r=new FormData;r.set("file",e);const t=await c({method:"POST",url:`${o.url}/upload`,transformRequest:o.transformRequest,data:r});w((r=>{const o=r.find((r=>r.file===e));o&&(o.path=t.path)}))}catch(r){const t=d(r)?r.message:"unknown error";w((r=>{const o=r.find((r=>r.file===e));o&&(o.error=t)}))}}))}r.value=""}),[v,z]),F=v.flatMap((e=>{let{file:r,path:t}=e;return t?[{name:r.name,size:r.size,path:t}]:[]})),_=()=>{(b||F.length>0)&&(E(b,F),x(""),w([]))};return k(fe,{children:[p,k(ge,{$focused:j&&!$,children:[v.length>0&&y(he,{children:v.map(((e,r)=>{let{file:t,error:o,path:n}=e;const i=void 0===o&&void 0===n;return y(me,{name:t.name,size:t.size,error:o,loading:i,onRemove:()=>{w((e=>{const r=e.findIndex((e=>e.file===t));-1!==r&&e.splice(r,1)}))}},r)}))}),k(ue,{children:[y(R,{disabled:$,ref:u,placeholder:"请输入你的问题, Enter+Shift换行, Enter发送",minRows:h,maxRows:5,value:b,onChange:e=>x(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),_())},onFocus:()=>N(!0),onBlur:()=>N(!1)}),n&&y(f,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:k(pe,{disabled:v.length>=6,onClick:()=>g.current?.click(),children:[y("input",{onChange:q,multiple:!0,accept:z.join(","),ref:g,type:"file",hidden:!0}),y("i",{className:"bi bi-file-earmark-arrow-up"})]})}),y(pe,{className:"text-primary",disabled:!b&&0===F.length,onClick:e=>{e.preventDefault(),_()},children:y("i",{className:"bi bi-send-fill"})})]})]})]})}const me=h(Q)`
508
508
  width: calc((100% - .75rem) / 2);
509
509
  `,he=h.div`
510
510
  display: flex;
@@ -558,15 +558,15 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
558
558
  box-shadow: 0.125rem 0.25rem 0.5rem rgba(0, 0, 0, 0.1);
559
559
  `,fe=h.div`
560
560
  margin: 0 1rem 1rem;
561
- `;function be(e){let{className:o,bot:n,user:s,input:l,showProcess:c,...d}=e;const[h,p]=t(d.messages||[]),u=i(null),g=r((()=>{const e=u.current;e&&requestAnimationFrame((()=>e.scrollTo(0,e.scrollHeight)))}),[]);return a((()=>{g()})),y(xe,{className:o,children:k(m,{children:[y(ve,{ref:u,children:h.map(((e,r)=>y(re,{message:e,showProcess:c,bot:n,user:s},r)))}),l&&y(de,{onMessages:p,scrollRef:u,...l})]})})}const ve=h.div`
561
+ `;function be(e){let{className:o,bot:n,user:a,input:l,showProcess:c,...d}=e;const[h,p]=t(d.messages||[]),u=i(null),g=r((()=>{const e=u.current;e&&requestAnimationFrame((()=>e.scrollTo(0,e.scrollHeight)))}),[]);return s((()=>{g()})),y(ve,{className:o,children:k(m,{children:[y(xe,{ref:u,children:h.map(((e,r)=>y(re,{message:e,showProcess:c,bot:n,user:a},r)))}),l&&y(de,{onMessages:p,scrollRef:u,...l})]})})}const xe=h.div`
562
562
  display: flex;
563
563
  flex-direction: column;
564
564
  flex: 1;
565
565
  overflow-y: auto;
566
566
  padding: 0 1rem;
567
567
  margin-bottom: .5rem;
568
- `,xe=h.div`
568
+ `,ve=h.div`
569
569
  display: flex;
570
570
  flex-direction: column;
571
571
  height: 100%;
572
- `;export{be as MessageBox,R as useChat};
572
+ `;export{be as MessageBox,P as useChat};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@topthink/chat",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "scripts": {
5
5
  "prebuild": "rimraf es types",
6
6
  "build": "rollup -c --environment NODE_ENV:production",
@@ -17,7 +17,7 @@
17
17
  ],
18
18
  "dependencies": {
19
19
  "@babel/runtime": "^7.11.2",
20
- "@topthink/components": "^1.0.83",
20
+ "@topthink/components": "^1.0.85",
21
21
  "katex": "^0.16.9",
22
22
  "react-markdown": "^8.0.7",
23
23
  "react-textarea-autosize": "^8.5.3",
@@ -55,5 +55,5 @@
55
55
  },
56
56
  "author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
57
57
  "license": "MIT",
58
- "gitHead": "567a500d32580507a46a6283934c18962bca4a0f"
58
+ "gitHead": "b8101a125a856029b96edc8e00880b8b05683137"
59
59
  }