@topthink/chat 1.0.28 → 1.0.30

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*as e from"react";import{useCallback as t,useState as r,useImperativeHandle as n,memo as o,useMemo as i,useRef as a,createContext as s,useContext as l,Fragment as c,forwardRef as d,useEffect as m}from"react";import{useImmer as h,request as p,isRequestError as u,Lightbox as g,styled as f,Clipboard as b,css as x,formatSize as v,Tooltip as k,useAsync as y,isImageUrl as w}from"@topthink/components";import{jsx as j,jsxs as N,Fragment as z}from"react/jsx-runtime";import M from"react-markdown";import"katex/dist/katex.min.css";import $ from"remark-math";import q from"remark-breaks";import C from"remark-gfm";import E from"rehype-katex";import F from"rehype-highlight";import{codes as R,types as S}from"micromark-util-symbol";import{markdownLineEnding as D}from"micromark-util-character";import{Spinner as T}from"react-bootstrap";import*as _ from"path";import B from"path";import L from"react-textarea-autosize";import O from"use-resize-observer";function A(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}function P(e){return e.map((e=>({id:e.id,query:e.query,files:e.files||void 0,chunks:e.chunks.map((e=>{let{content:t="",error:r,tools:n=[]}=e;return{content:t,error:r,tools:n}})),annotation:e.annotation,stats:{usage:e.usage,latency:e.latency}})))}function I(e){let{url:o,onboarding:i,onSuccess:a,ref:s,transformRequest:l,...c}=e;const d=t((e=>{const t=[];if(i&&!1!==i.enable&&i.prologue){const e=A(i.questions);t.push({chunks:[{content:[i.prologue,...e.map((e=>`[${e}](#!question)`))].join("\n"),tools:[]}]})}return e?t.concat(P(e.messages||[])):t}),[i]),[m,g]=h((()=>d(c.conversation))),[f,b]=r(!1),[x,v]=r(c.conversation?.id),k=t((async function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(e||t.length>0){b(!0),g((r=>{r.push({query:e,files:t,chunks:[],loading:!0})}));try{let r={method:"post",url:o,data:{query:e,files:t,conversation:x},onMessage:e=>{if(e.data)if("[DONE]"!=e.data)try{const t=JSON.parse(e.data);t.conversation?v(t.conversation):g((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 g((e=>{e[e.length-1].loading=!1}))}};l&&(r=l("chat",r)),await p(r)}catch(e){let t="未知错误";u(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),g((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}))}b(!1),a?.()}}),[l,x]),y=t((e=>{f||(v(e?.id),g(d(e)))}),[f,d]);return n(s,(()=>({reset:y,send:k}))),{messages:m,loading:f,send:k,reset:y}}const U=function(e,t,r){function n(t){return t!==R.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(S.chunkString,{contentType:"string"}),o(t)):(e.consume(t),n)}function o(n){return n===R.rightSquareBracket?(e.exit(S.chunkString),e.exit("citeData"),e.enter("citeMarker"),function(n){if(n!==R.rightSquareBracket)return r(n);return e.consume(n),e.exit("citeMarker"),e.exit("cite"),t(n)}(n)):D(n)||n===R.eof?r(n):(e.consume(n),o)}return function(t){return t!==R.leftSquareBracket?r(t):(e.enter("cite"),e.enter("citeMarker"),n(t))}},H=g.Image,K={pre:e=>{let{children:t}=e;const r=a(null);return N(Q,{ref:r,children:[j(G,{tooltip:!1,content:()=>{const e=r.current?.getElementsByTagName("code");return e?.[0]?.innerText||""}}),t]})},a:e=>{let{node:t,href:r,...n}=e;const o=r?.startsWith("http")?"_blank":"_self";return j("a",{...n,href:r,target:o})},img:e=>{let{node:t,...r}=e;return j(H,{...r})}},X=[[$,{singleDollarTextMath:!1}],C,q,function(){const e=this.data();e.micromarkExtensions.push({text:{[R.leftSquareBracket]:{tokenize:U}}}),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}}})}],J=[E,[F,{detect:!1,ignoreMissing:!0}]],W=o((e=>{let{content:t,components:r}=e;return t=t.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$"),j(V,{remarkPlugins:X,rehypePlugins:J,components:i((()=>({...K,...r})),[r]),children:t})})),G=f(b)`
1
+ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandle as n,memo as o,useMemo as i,useRef as a,createContext as s,useContext as l,Fragment as c,forwardRef as d,useEffect as m}from"react";import{useImmer as h,request as p,isRequestError as u,Lightbox as g,styled as f,Clipboard as b,css as x,formatSize as v,Tooltip as k,useAsync as y,isImageUrl as w}from"@topthink/components";import{jsx as j,jsxs as N,Fragment as z}from"react/jsx-runtime";import M from"react-markdown";import"katex/dist/katex.min.css";import $ from"remark-math";import q from"remark-breaks";import C from"remark-gfm";import E from"rehype-katex";import F from"rehype-highlight";import{codes as R,types as S}from"micromark-util-symbol";import{markdownLineEnding as D}from"micromark-util-character";import{Spinner as T}from"react-bootstrap";import*as _ from"path";import B from"path";import L from"react-textarea-autosize";import O from"use-resize-observer";function A(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}function P(e){return e.map((e=>({id:e.id,query:e.query,files:e.files||void 0,chunks:e.chunks.map((e=>{let{content:t="",error:r,tools:n=[]}=e;return{content:t,error:r,tools:n}})),annotation:e.annotation,stats:{usage:e.usage,latency:e.latency}})))}function I(e){let{url:o,onboarding:i,onSuccess:a,ref:s,transformRequest:l,...c}=e;const d=t((e=>{const t=[];if(i&&!1!==i.enable&&i.prologue){const e=A(i.questions);t.push({chunks:[{content:[i.prologue,...e.map((e=>`[${e}](#!question)`))].join("\n"),tools:[]}]})}return e?t.concat(P(e.messages||[])):t}),[i]),[m,g]=h((()=>d(c.conversation))),[f,b]=r(!1),[x,v]=r(c.conversation?.id),k=t((async function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(e||t.length>0){b(!0),g((r=>{r.push({query:e,files:t,chunks:[],loading:!0})}));try{let r={method:"post",url:o,data:{query:e,files:t,conversation:x},onMessage:e=>{if(e.data)if("[DONE]"!=e.data)try{const t=JSON.parse(e.data);t.conversation?v(t.conversation):g((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 g((e=>{e[e.length-1].loading=!1}))}};l&&(r=l("chat",r)),await p(r)}catch(e){let t="未知错误";u(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),g((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}))}b(!1),a?.()}}),[l,x]),y=t((e=>{f||(v(e?.id),g(d(e)))}),[f,d]);return n(s,(()=>({reset:y,send:k}))),{messages:m,loading:f,send:k,reset:y}}const U=function(e,t,r){function n(t){return t!==R.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(S.chunkString,{contentType:"string"}),o(t)):(e.consume(t),n)}function o(n){return n===R.rightSquareBracket?(e.exit(S.chunkString),e.exit("citeData"),e.enter("citeMarker"),function(n){if(n!==R.rightSquareBracket)return r(n);return e.consume(n),e.exit("citeMarker"),e.exit("cite"),t(n)}(n)):D(n)||n===R.eof||n>57||n<48?r(n):(e.consume(n),o)}return function(t){return t!==R.leftSquareBracket?r(t):(e.enter("cite"),e.enter("citeMarker"),n(t))}},H=function(){const e=this.data();e.micromarkExtensions.push({text:{[R.leftSquareBracket]:{tokenize:U}}}),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}}})},K=g.Image,X={pre:e=>{let{children:t}=e;const r=a(null);return N(V,{ref:r,children:[j(Q,{tooltip:!1,content:()=>{const e=r.current?.getElementsByTagName("code");return e?.[0]?.innerText||""}}),t]})},a:e=>{let{node:t,href:r,...n}=e;const o=r?.startsWith("http")?"_blank":"_self";return j("a",{...n,href:r,target:o})},img:e=>{let{node:t,...r}=e;return j(K,{...r})}},J=[[$,{singleDollarTextMath:!1}],C,q],W=[E,[F,{detect:!1,ignoreMissing:!0}]],G=o((e=>{let{content:t,components:r,cite:n}=e;return t=t.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$"),j(Y,{remarkPlugins:i((()=>n?[...J,H]:J),[n]),rehypePlugins:W,components:i((()=>({...X,...r})),[r]),children:t})})),Q=f(b)`
2
2
  position: absolute;
3
3
  right: 10px;
4
4
  top: 1em;
@@ -18,17 +18,17 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
18
18
  &:hover {
19
19
  opacity: 1;
20
20
  }
21
- `,Q=f.pre`
21
+ `,V=f.pre`
22
22
  position: relative;
23
23
 
24
24
  &:hover {
25
- ${G} {
25
+ ${Q} {
26
26
  pointer-events: all;
27
27
  transform: translateX(0px);
28
28
  opacity: 0.5;
29
29
  }
30
30
  }
31
- `,V=f(M)`
31
+ `,Y=f(M)`
32
32
  -ms-text-size-adjust: 100%;
33
33
  -webkit-text-size-adjust: 100%;
34
34
  line-height: 1.8;
@@ -298,7 +298,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
298
298
  .hljs-strong {
299
299
  font-weight: 700;
300
300
  }
301
- `,Y=s(null);function Z(){const e=l(Y);if(!e)throw new Error("useContext must be used within a Provider");return e}function ee(e){let{children:t,...r}=e;return j(Y.Provider,{value:r,children:t})}const te=g.Image;function re(e){let{src:t,...r}=e;const{imageResolver:n}=Z();return n&&t&&(t=n(t)),j(ne,{children:j(te,{src:t,...r})})}const ne=f.div`
301
+ `,Z=s(null);function ee(){const e=l(Z);if(!e)throw new Error("useContext must be used within a Provider");return e}function te(e){let{children:t,...r}=e;return j(Z.Provider,{value:r,children:t})}const re=g.Image;function ne(e){let{src:t,...r}=e;const{imageResolver:n}=ee();return n&&t&&(t=n(t)),j(oe,{children:j(re,{src:t,...r})})}const oe=f.div`
302
302
  margin-bottom: .5rem;
303
303
  margin-top: .5rem;
304
304
  border: var(--bs-border-width) var(--bs-border-style) var(--bs-border-color);
@@ -323,9 +323,9 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
323
323
  max-height: 100%;
324
324
  max-width: 100%;
325
325
  }
326
- `;function oe(e){let{tool:t}=e;const[n,o]=r(!1),{logLevel:i="none"}=Z();let a=null;t.content&&("string"==typeof t.content?a=j(W,{content:t.content}):"image"===t.content.type&&(a=j(re,{src:t.content.image})));const s="response"in t;return N(c,{children:["stats"==i&&j("div",{className:"mb-2",children:N("div",{role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[s?t.error?j("i",{className:"bi bi-x-circle-fill text-danger"}):j("i",{className:"bi bi-check-circle-fill text-success"}):j(T,{animation:"border",variant:"primary",size:"sm"}),j("span",{className:"text-muted",children:s?"已使用":"正在使用"}),j("span",{children:t.title})]})}),"all"==i&&j("div",{className:"mb-2",children:N("div",n&&s?{className:"shadow-sm rounded bg-white fs-7",children:[N("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[t.error?j("i",{className:"bi bi-x-circle-fill text-danger"}):j("i",{className:"bi bi-check-circle-fill text-success"}),j("span",{className:"text-muted",children:"已使用"}),j("span",{children:t.title}),j("i",{className:"bi bi-caret-up-fill text-muted"})]}),N("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[N("div",{className:"border rounded bg-light",children:[j("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:j("span",{className:"text-muted",children:"参数"})}),j("div",{className:"border-top p-2 overflow-hidden",children:t.arguments})]}),N("div",{className:"border rounded bg-light",children:[j("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:j("span",{className:"text-muted",children:"响应"})}),j(ie,{className:"border-top p-2 overflow-hidden",children:t.response||"None"})]})]})]}:{onClick:()=>o(s),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[s?t.error?j("i",{className:"bi bi-x-circle-fill text-danger"}):j("i",{className:"bi bi-check-circle-fill text-success"}):j(T,{animation:"border",variant:"primary",size:"sm"}),j("span",{className:"text-muted",children:s?"已使用":"正在使用"}),j("span",{children:t.title}),s&&j("i",{className:"bi bi-caret-down-fill text-muted"})]})}),a]})}const ie=f.div`
326
+ `;function ie(e){let{tool:t}=e;const[n,o]=r(!1),{logLevel:i="none"}=ee();let a=null;t.content&&("string"==typeof t.content?a=j(G,{content:t.content}):"image"===t.content.type&&(a=j(ne,{src:t.content.image})));const s="response"in t;return N(c,{children:["stats"==i&&j("div",{className:"mb-2",children:N("div",{role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[s?t.error?j("i",{className:"bi bi-x-circle-fill text-danger"}):j("i",{className:"bi bi-check-circle-fill text-success"}):j(T,{animation:"border",variant:"primary",size:"sm"}),j("span",{className:"text-muted",children:s?"已使用":"正在使用"}),j("span",{children:t.title})]})}),"all"==i&&j("div",{className:"mb-2",children:N("div",n&&s?{className:"shadow-sm rounded bg-white fs-7",children:[N("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[t.error?j("i",{className:"bi bi-x-circle-fill text-danger"}):j("i",{className:"bi bi-check-circle-fill text-success"}),j("span",{className:"text-muted",children:"已使用"}),j("span",{children:t.title}),j("i",{className:"bi bi-caret-up-fill text-muted"})]}),N("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[N("div",{className:"border rounded bg-light",children:[j("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:j("span",{className:"text-muted",children:"参数"})}),j("div",{className:"border-top p-2 overflow-hidden",children:t.arguments})]}),N("div",{className:"border rounded bg-light",children:[j("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:j("span",{className:"text-muted",children:"响应"})}),j(ae,{className:"border-top p-2 overflow-hidden",children:t.response||"None"})]})]})]}:{onClick:()=>o(s),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[s?t.error?j("i",{className:"bi bi-x-circle-fill text-danger"}):j("i",{className:"bi bi-check-circle-fill text-success"}):j(T,{animation:"border",variant:"primary",size:"sm"}),j("span",{className:"text-muted",children:s?"已使用":"正在使用"}),j("span",{children:t.title}),s&&j("i",{className:"bi bi-caret-down-fill text-muted"})]})}),a]})}const ae=f.div`
327
327
  white-space: pre-wrap;
328
- `;var ae,se,le;function ce(){return ce=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},ce.apply(this,arguments)}const de=t=>e.createElement("svg",ce({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},t),ae||(ae=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"}))),se||(se=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"}))),le||(le=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 me=function(e){let{ext:t}=e;const{result:r}=y((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(t,r){("function"==typeof queueMicrotask?queueMicrotask:setTimeout)(r.bind(null,new Error("Unknown variable dynamic import: "+e)))}))}}(`../../images/file/${e}.svg`)).default),[t]);return r?j("img",{width:30,height:30,src:r}):null};function he(e){let{className:t,name:r,size:n,loading:o,error:i,onRemove:a}=e;const s=B.extname(r).substring(1),l=N(fe,{className:t,$error:!!i,children:[N(ge,{children:[j(me,{ext:s}),o&&j(T,{variant:"primary"})]}),N(ue,{children:[j("h4",{children:r}),j("p",{children:v(n,1)})]}),a&&j(pe,{onClick:e=>{e.preventDefault(),a()},children:j("i",{className:"bi bi-trash3"})})]});return i?j(k,{placement:"top",tooltip:i,children:l}):l}const pe=f.div`
328
+ `;var se,le,ce;function de(){return de=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},de.apply(this,arguments)}const me=t=>e.createElement("svg",de({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},t),se||(se=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"}))),le||(le=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"}))),ce||(ce=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 he=function(e){let{ext:t}=e;const{result:r}=y((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(t,r){("function"==typeof queueMicrotask?queueMicrotask:setTimeout)(r.bind(null,new Error("Unknown variable dynamic import: "+e)))}))}}(`../../images/file/${e}.svg`)).default),[t]);return r?j("img",{width:30,height:30,src:r}):null};function pe(e){let{className:t,name:r,size:n,loading:o,error:i,onRemove:a}=e;const s=B.extname(r).substring(1),l=N(be,{className:t,$error:!!i,children:[N(fe,{children:[j(he,{ext:s}),o&&j(T,{variant:"primary"})]}),N(ge,{children:[j("h4",{children:r}),j("p",{children:v(n,1)})]}),a&&j(ue,{onClick:e=>{e.preventDefault(),a()},children:j("i",{className:"bi bi-trash3"})})]});return i?j(k,{placement:"top",tooltip:i,children:l}):l}const ue=f.div`
329
329
  width: 1.75rem;
330
330
  height: 1.75rem;
331
331
  align-items: center;
@@ -340,7 +340,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
340
340
  color: var(--bs-danger);
341
341
  background-color: var(--bs-danger-bg-subtle);
342
342
  }
343
- `,ue=f.div`
343
+ `,ge=f.div`
344
344
  flex: 1;
345
345
  overflow: hidden;
346
346
 
@@ -361,7 +361,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
361
361
  line-height: 20px;
362
362
  margin-bottom: 0;
363
363
  }
364
- `,ge=f.div`
364
+ `,fe=f.div`
365
365
  position: relative;
366
366
  width: 3rem;
367
367
  height: 3rem;
@@ -375,7 +375,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
375
375
  position: absolute;
376
376
  }
377
377
 
378
- `,fe=f.div`
378
+ `,be=f.div`
379
379
  display: flex;
380
380
  gap: .5rem;
381
381
  padding: .5rem;
@@ -391,7 +391,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
391
391
  ${e=>e.$error&&x`
392
392
  border-color: var(--bs-danger) !important;
393
393
  `}
394
- `;var be=d((function(e,t){let{children:r,tooltip:n,onClick:o,...i}=e;const a=j(xe,{ref:t,onClick:o,...i,children:r});return n?j(k,{tooltip:n,placement:"top",children:a}):a}));const xe=f.div`
394
+ `;var xe=d((function(e,t){let{children:r,tooltip:n,onClick:o,...i}=e;const a=j(ve,{ref:t,onClick:o,...i,children:r});return n?j(k,{tooltip:n,placement:"top",children:a}):a}));const ve=f.div`
395
395
  align-items: center;
396
396
  justify-content: center;
397
397
  border-radius: .375rem;
@@ -415,12 +415,12 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
415
415
  background-color: var(--bs-gray-200);
416
416
  }
417
417
  }
418
- `,ve=o((e=>{let{message:t,actions:r,updater:n}=e;const{query:o,files:a=[],chunks:s,annotation:l}=t,{user:d,bot:m,logLevel:h="none"}=Z(),p=i((()=>s?s.reduce((function(e,t){return e+t.content+"\n"+t.tools.reduce(((e,t)=>t.content&&"string"==typeof t.content?e+t.content+"\n":e),"")}),""):""),[s]);return N(z,{children:[(o||a.length>0)&&N(Me,{$reverse:!0,children:[d&&j(Ne,{children:j("img",{src:d.avatar})}),N(ze,{children:[o&&j(W,{content:o}),a.length>0&&a.map(((e,t)=>{let{name:r,size:n,path:o}=e;return w(o)?j(re,{src:`/uploads/${o}`},t):j(ye,{children:j(he,{name:r,size:n})},t)}))]})]}),s&&N(Me,{children:[j(Ne,{children:j("img",{src:m.avatar})}),N(ze,{children:[(o||a.length>0)&&N(we,{children:[j(b,{placement:"top",content:p,as:be,tooltip:!0}),r?.({Component:be,message:t,content:p,setMessage:n})]}),j(je,{children:["stats","all"].includes(h)&&t.stats&&N(z,{children:[N("span",{children:["耗时 ",t.stats.latency/1e3," 秒"]}),N("span",{children:["花费 Token ",t.stats.usage]})]})}),j(ke,{$deleted:!!l,children:s.length>0&&s.map(((e,t)=>N(c,{children:[e.content&&j(W,{content:e.content}),e.tools.map(((e,t)=>j(oe,{tool:e},t))),e.error&&j(W,{content:`[${e.error}]`})]},t)))}),l&&N("div",{children:[N("div",{className:"d-flex align-items-center",children:[j("span",{className:"fs-7 text-secondary me-2",children:"标注的答案"}),j("hr",{className:"flex-fill"})]}),j(W,{content:l.answer})]}),t.loading&&j(de,{})]})]})]})})),ke=f.div`
418
+ `,ke=o((e=>{let{message:t,actions:r,updater:n}=e;const{query:o,files:a=[],chunks:s,annotation:l}=t,{user:d,bot:m,logLevel:h="none"}=ee(),p=i((()=>s?s.reduce((function(e,t){return e+t.content+"\n"+t.tools.reduce(((e,t)=>t.content&&"string"==typeof t.content?e+t.content+"\n":e),"")}),""):""),[s]);return N(z,{children:[(o||a.length>0)&&N($e,{$reverse:!0,children:[d&&j(ze,{children:j("img",{src:d.avatar})}),N(Me,{children:[o&&j(G,{content:o}),a.length>0&&a.map(((e,t)=>{let{name:r,size:n,path:o}=e;return w(o)?j(ne,{src:`/uploads/${o}`},t):j(we,{children:j(pe,{name:r,size:n})},t)}))]})]}),s&&N($e,{children:[j(ze,{children:j("img",{src:m.avatar})}),N(Me,{children:[(o||a.length>0)&&N(je,{children:[j(b,{placement:"top",content:p,as:xe,tooltip:!0}),r?.({Component:xe,message:t,content:p,setMessage:n})]}),j(Ne,{children:["stats","all"].includes(h)&&t.stats&&N(z,{children:[N("span",{children:["耗时 ",t.stats.latency/1e3," 秒"]}),N("span",{children:["花费 Token ",t.stats.usage]})]})}),j(ye,{$deleted:!!l,children:s.length>0&&s.map(((e,t)=>N(c,{children:[e.content&&j(G,{content:e.content}),e.tools.map(((e,t)=>j(ie,{tool:e},t))),e.error&&j(G,{content:`[${e.error}]`})]},t)))}),l&&N("div",{children:[N("div",{className:"d-flex align-items-center",children:[j("span",{className:"fs-7 text-secondary me-2",children:"标注的答案"}),j("hr",{className:"flex-fill"})]}),j(G,{content:l.answer})]}),t.loading&&j(me,{})]})]})]})})),ye=f.div`
419
419
  ${e=>e.$deleted&&x`
420
420
  text-decoration-line: line-through;
421
421
  color: var(--bs-secondary);
422
422
  `}
423
- `,ye=f.div`
423
+ `,we=f.div`
424
424
  width: 100%;
425
425
  margin-bottom: .5rem;
426
426
  margin-top: .5rem;
@@ -432,7 +432,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
432
432
  &:last-child {
433
433
  margin-bottom: 0;
434
434
  }
435
- `,we=f.div`
435
+ `,je=f.div`
436
436
  position: absolute;
437
437
  top: -1.1rem;
438
438
  right: 0;
@@ -440,7 +440,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
440
440
  gap: .5rem;
441
441
  opacity: 0;
442
442
  z-index: 1;
443
- `,je=f.div`
443
+ `,Ne=f.div`
444
444
  position: absolute;
445
445
  bottom: -1.4rem;
446
446
  left: 0;
@@ -453,7 +453,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
453
453
  display: flex;
454
454
  opacity: 0;
455
455
  z-index: 1;
456
- `,Ne=f.div`
456
+ `,ze=f.div`
457
457
  width: 35px;
458
458
  height: 35px;
459
459
  border-radius: 50%;
@@ -463,7 +463,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
463
463
  width: 100%;
464
464
  height: 100%;
465
465
  }
466
- `,ze=f.div`
466
+ `,Me=f.div`
467
467
  padding: .875rem;
468
468
  background-color: rgb(243, 243, 243);
469
469
  border-radius: var(--bs-border-radius-lg);
@@ -483,18 +483,18 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
483
483
  }
484
484
 
485
485
  &:hover {
486
- ${je}, ${we} {
486
+ ${Ne}, ${je} {
487
487
  opacity: 1;
488
488
  }
489
489
  }
490
- `,Me=f.div`
490
+ `,$e=f.div`
491
491
  display: flex;
492
492
  justify-content: flex-start;
493
493
  padding: 1rem 0 1rem 0;
494
494
  ${e=>e.$reverse&&x`
495
495
  flex-direction: row-reverse;
496
496
 
497
- ${ze} {
497
+ ${Me} {
498
498
  background: rgba(var(--bs-primary-rgb), 0.1);
499
499
  margin-right: 10px;
500
500
  margin-left: 0;
@@ -509,15 +509,15 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
509
509
  }
510
510
  }
511
511
  `};
512
- `,$e=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"];function qe(e){let{chat:n,fileTypes:o,scrollRef:i,onMessages:s,minRows:l,toolbar:c,disabled:d,onLoading:g}=e;const f=a(null),b=a(null),[x,v]=r(""),[y,w]=h([]),[z,M]=r(!1),$=o&&o.length>0?o:$e,{loading:q,messages:C,send:E}=I({...n,onSuccess(){requestAnimationFrame((()=>{f.current?.focus()})),n.onSuccess?.()}});m((()=>{s(C)}),[C]),m((()=>{g?.(q)}),[q]),m((()=>{!o&&y.length>0&&w([])}),[o]),m((()=>{const e=i.current;if(e&&E){const t=e=>{e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash&&(e.preventDefault(),E(e.target.text))};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}}),[i,E]);const F=t((e=>{let{target:t}=e;if(t.files){const e=Array.from(t.files).filter((e=>!!$.includes(_.extname(e.name))&&(!(e.size>20971520)&&e))).slice(0,6-y.length);w((t=>{t.push(...e.map((e=>({file:e}))))})),e.forEach((async e=>{try{const t=new FormData;t.set("file",e);let r={method:"POST",url:`${n.url}/upload`,data:t};n.transformRequest&&(r=n.transformRequest("upload",r));const o=await p(r);w((t=>{const r=t.find((t=>t.file===e));r&&(r.path=o.path)}))}catch(t){const r=u(t)?t.message:"unknown error";w((t=>{const n=t.find((t=>t.file===e));n&&(n.error=r)}))}}))}t.value=""}),[y,$]),R=y.flatMap((e=>{let{file:t,path:r}=e;return r?[{name:t.name,size:t.size,path:r}]:[]})),S=()=>{(x||R.length>0)&&(E(x,R),v(""),w([]))};return N(De,{children:[c,N(Se,{$focused:z&&!q,children:[y.length>0&&j(Ee,{children:y.map(((e,t)=>{let{file:r,error:n,path:o}=e;const i=void 0===n&&void 0===o;return j(Ce,{name:r.name,size:r.size,error:n,loading:i,onRemove:()=>{w((e=>{const t=e.findIndex((e=>e.file===r));-1!==t&&e.splice(t,1)}))}},t)}))}),N(Re,{children:[j(L,{disabled:q||d,ref:f,placeholder:"请输入你的问题, Enter+Shift换行, Enter发送",minRows:l,maxRows:5,value:x,onChange:e=>v(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),S())},onFocus:()=>M(!0),onBlur:()=>M(!1)}),o&&j(k,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:N(Fe,{disabled:y.length>=6,onClick:()=>b.current?.click(),children:[j("input",{onChange:F,multiple:!0,accept:$.join(","),ref:b,type:"file",hidden:!0}),j("i",{className:"bi bi-file-earmark-arrow-up"})]})}),j(Fe,{className:"text-primary",disabled:!x&&0===R.length,onClick:e=>{e.preventDefault(),S()},children:j("i",{className:"bi bi-send-fill"})})]})]})]})}const Ce=f(he)`
512
+ `,qe=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"];function Ce(e){let{chat:n,fileTypes:o,scrollRef:i,onMessages:s,minRows:l,toolbar:c,disabled:d,onLoading:g,autoFocus:f}=e;const b=a(null),x=a(null),[v,y]=r(""),[w,z]=h([]),[M,$]=r(!1),q=o&&o.length>0?o:qe,{loading:C,messages:E,send:F}=I({...n,onSuccess(){requestAnimationFrame((()=>{b.current?.focus()})),n.onSuccess?.()}});m((()=>{s(E)}),[E]),m((()=>{g?.(C)}),[C]),m((()=>{!o&&w.length>0&&z([])}),[o]),m((()=>{const e=i.current;if(e&&F){const t=e=>{e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash&&(e.preventDefault(),F(e.target.text))};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}}),[i,F]);const R=t((e=>{let{target:t}=e;if(t.files){const e=Array.from(t.files).filter((e=>!!q.includes(_.extname(e.name))&&(!(e.size>20971520)&&e))).slice(0,6-w.length);z((t=>{t.push(...e.map((e=>({file:e}))))})),e.forEach((async e=>{try{const t=new FormData;t.set("file",e);let r={method:"POST",url:`${n.url}/upload`,data:t};n.transformRequest&&(r=n.transformRequest("upload",r));const o=await p(r);z((t=>{const r=t.find((t=>t.file===e));r&&(r.path=o.path)}))}catch(t){const r=u(t)?t.message:"unknown error";z((t=>{const n=t.find((t=>t.file===e));n&&(n.error=r)}))}}))}t.value=""}),[w,q]),S=w.flatMap((e=>{let{file:t,path:r}=e;return r?[{name:t.name,size:t.size,path:r}]:[]})),D=()=>{(v||S.length>0)&&(F(v,S),y(""),z([]))};return N(Te,{children:[c,N(De,{$focused:M&&!C,children:[w.length>0&&j(Fe,{children:w.map(((e,t)=>{let{file:r,error:n,path:o}=e;const i=void 0===n&&void 0===o;return j(Ee,{name:r.name,size:r.size,error:n,loading:i,onRemove:()=>{z((e=>{const t=e.findIndex((e=>e.file===r));-1!==t&&e.splice(t,1)}))}},t)}))}),N(Se,{children:[j(L,{disabled:C||d,ref:b,placeholder:"请输入你的问题, Enter+Shift换行, Enter发送",minRows:l,maxRows:5,value:v,onChange:e=>y(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),D())},autoFocus:f,onFocus:()=>$(!0),onBlur:()=>$(!1)}),o&&j(k,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:N(Re,{disabled:w.length>=6,onClick:()=>x.current?.click(),children:[j("input",{onChange:R,multiple:!0,accept:q.join(","),ref:x,type:"file",hidden:!0}),j("i",{className:"bi bi-file-earmark-arrow-up"})]})}),j(Re,{className:"text-primary",disabled:!v&&0===S.length,onClick:e=>{e.preventDefault(),D()},children:j("i",{className:"bi bi-send-fill"})})]})]})]})}const Ee=f(pe)`
513
513
  width: calc((100% - .75rem) / 2);
514
- `,Ee=f.div`
514
+ `,Fe=f.div`
515
515
  display: flex;
516
516
  flex-wrap: wrap;
517
517
  padding: .75rem;
518
518
  gap: .75rem;
519
519
  border-bottom: var(--bs-border-width) var(--bs-border-style) var(--bs-border-color);
520
- `,Fe=f.button`
520
+ `,Re=f.button`
521
521
  color: var(--bs-secondary);
522
522
  cursor: pointer;
523
523
  display: flex;
@@ -539,7 +539,7 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
539
539
  background-color: var(--bs-gray-200);
540
540
  }
541
541
 
542
- `,Re=f.div`
542
+ `,Se=f.div`
543
543
  display: flex;
544
544
  padding: .5rem .5rem .5rem 1rem;
545
545
  gap: .25rem;
@@ -556,26 +556,26 @@ import*as e from"react";import{useCallback as t,useState as r,useImperativeHandl
556
556
  color: rgba(54, 54, 54, .3);
557
557
  }
558
558
  }
559
- `,Se=f.div`
559
+ `,De=f.div`
560
560
  background: #FFF;
561
561
  border: var(--bs-border-width) var(--bs-border-style) ${e=>e.$focused?"var(--bs-primary)":"var(--bs-border-color)"};
562
562
  border-radius: var(--bs-border-radius-lg);
563
563
  box-shadow: 0.125rem 0.25rem 0.5rem rgba(0, 0, 0, 0.1);
564
- `,De=f.div`
564
+ `,Te=f.div`
565
565
  margin: 0 1rem 1rem;
566
- `;function Te(e){let{className:n,bot:o,user:i,input:s,logLevel:l,imageResolver:c,placeholder:d,actions:p,...u}=e;const[f,b]=h((()=>P(u.messages||[]))),[x,v]=r(!1),k=a(!s),y=a(null),w=t((()=>{const e=y.current;e&&requestAnimationFrame((()=>{e.scrollTo({top:e.scrollHeight})}))}),[]),{ref:z,height:M}=O();return m((()=>{!x&&k.current||(x&&(k.current=!0),w())}),[M,x,k]),j(ee,{bot:o,user:i,logLevel:l,imageResolver:c,children:j(Le,{className:n,children:N(g,{children:[j(_e,{ref:y,children:N(Be,{ref:z,children:[0===f.length&&d,f.map(((e,t)=>j(ve,{actions:p,message:e,updater:t=>{b((r=>{const n=r.findIndex((t=>t.id===e.id));-1!==n&&t(r[n])}))}},t)))]})}),s&&j(qe,{onMessages:b,scrollRef:y,onLoading:v,...s})]})})})}const _e=f.div`
566
+ `;function _e(e){let{className:n,bot:o,user:i,input:s,logLevel:l,imageResolver:c,placeholder:d,actions:p,...u}=e;const[f,b]=h((()=>P(u.messages||[]))),[x,v]=r(!1),k=a(!s),y=a(null),w=t((()=>{const e=y.current;e&&requestAnimationFrame((()=>{e.scrollTo({top:e.scrollHeight})}))}),[]),{ref:z,height:M}=O();return m((()=>{!x&&k.current||(x&&(k.current=!0),w())}),[M,x,k]),j(te,{bot:o,user:i,logLevel:l,imageResolver:c,children:j(Oe,{className:n,children:N(g,{children:[j(Be,{ref:y,children:N(Le,{ref:z,children:[0===f.length&&d,f.slice(-30).map(((e,t)=>j(ke,{actions:p,message:e,updater:t=>{b((r=>{const n=r.findIndex((t=>t.id===e.id));-1!==n&&t(r[n])}))}},t)))]})}),s&&j(Ce,{onMessages:b,scrollRef:y,onLoading:v,...s})]})})})}const Be=f.div`
567
567
  display: flex;
568
568
  flex-direction: column;
569
569
  flex: 1;
570
570
  overflow-y: auto;
571
571
  padding: 0 1rem;
572
572
  margin-bottom: .5rem;
573
- `,Be=f.div`
573
+ `,Le=f.div`
574
574
  display: flex;
575
575
  flex-direction: column;
576
576
  flex: 1;
577
- `,Le=f.div`
577
+ `,Oe=f.div`
578
578
  display: flex;
579
579
  flex-direction: column;
580
580
  height: 100%;
581
- `;export{W as Markdown,Te as MessageBox,A as pickQuestions,I as useChat};
581
+ `;export{G as Markdown,_e as MessageBox,A as pickQuestions,I as useChat};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@topthink/chat",
3
- "version": "1.0.28",
3
+ "version": "1.0.30",
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": "cbab38428fb6fbf05357c229cf3b5d49f80cfd3d"
68
+ "gitHead": "afc5154914b1f3a021798d6e947cc0e3050bde26"
69
69
  }
@@ -4,6 +4,7 @@ import 'katex/dist/katex.min.css';
4
4
  interface Props {
5
5
  content: string;
6
6
  components?: Components;
7
+ cite?: boolean;
7
8
  }
8
- export declare const Markdown: import("react").MemoExoticComponent<({ content, components }: Props) => JSX.Element>;
9
+ export declare const Markdown: import("react").MemoExoticComponent<({ content, components, cite }: Props) => JSX.Element>;
9
10
  export default Markdown;
@@ -22,6 +22,7 @@ interface Props {
22
22
  minRows?: number;
23
23
  chat: ChatOptions;
24
24
  toolbar?: ReactNode;
25
+ autoFocus?: boolean;
25
26
  };
26
27
  actions?: MessageActions;
27
28
  }
@@ -10,6 +10,7 @@ interface Props {
10
10
  toolbar?: ReactNode;
11
11
  disabled?: boolean;
12
12
  onLoading?: (loading: boolean) => void;
13
+ autoFocus?: boolean;
13
14
  }
14
- export default function ({ chat, fileTypes, scrollRef, onMessages, minRows, toolbar, disabled, onLoading }: Props): JSX.Element;
15
+ export default function ({ chat, fileTypes, scrollRef, onMessages, minRows, toolbar, disabled, onLoading, autoFocus }: Props): JSX.Element;
15
16
  export {};