@topthink/chat 1.0.33 → 1.0.35
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 +13 -13
- package/package.json +2 -2
package/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import*as e from"react";import{useCallback as t,useState as r,useRef as n,useImperativeHandle as o,memo as i,useMemo 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 u,isRequestError as p,Lightbox as g,styled as f,Clipboard as b,css as v,formatSize as x,Tooltip as y,useAsync as k,isImageUrl as w,Form as j,Toast as N}from"@topthink/components";import{jsx as
|
|
1
|
+
import*as e from"react";import{useCallback as t,useState as r,useRef as n,useImperativeHandle as o,memo as i,useMemo 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 u,isRequestError as p,Lightbox as g,styled as f,Clipboard as b,css as v,formatSize as x,Tooltip as y,useAsync as k,isImageUrl as w,Form as j,Toast as N}from"@topthink/components";import{jsx as z,jsxs as M,Fragment as $}from"react/jsx-runtime";import q from"react-markdown";import"katex/dist/katex.min.css";import C from"remark-math";import E from"remark-breaks";import F from"remark-gfm";import S from"rehype-katex";import D from"rehype-highlight";import{codes as R,types as T}from"micromark-util-symbol";import{markdownLineEnding as _}from"micromark-util-character";import{Spinner as B}from"react-bootstrap";import*as L from"path";import O from"path";import A from"react-textarea-autosize";import P from"use-resize-observer";function I(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 U(e){return e.map((e=>({id:e.id,query:e.query,files:e.files||void 0,variables:e.variables,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 H(e){let{url:i,onboarding:a,suggestion:s,onSuccess:l,ref:c,transformRequest:d,...m}=e;const g=t((e=>{const t=[];if(a&&!1!==a.enable&&a.prologue){const e=I(a.questions);t.push({chunks:[{content:[a.prologue,...e.map((e=>`[${e}](#!question)`))].join("\n"),tools:[]}]})}return e?t.concat(U(e.messages||[])):t}),[a]),[f,b]=h((()=>g(m.conversation))),[v,x]=r([]),[y,k]=r(!1),w=n(m.conversation?.id),j=n(null),N=t((async()=>{j.current=new AbortController;try{let e={method:"post",url:`${i}/suggestion`,data:{conversation:w.current},signal:j.current.signal};d&&(e=d("suggestion",e));const t=await u(e);x(t)}catch{}finally{j.current=null}}),[d,i]),z=t((async function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=arguments.length>2?arguments[2]:void 0;if(e||t.length>0){j.current&&j.current.abort(),k(!0),x([]),b((n=>{n.push({query:e,files:t,variables:r,chunks:[],loading:!0})}));try{let n={method:"post",url:i,data:{query:e,files:t,variables:r,conversation:w.current},onMessage:e=>{if(e.data)if("[DONE]"!=e.data)try{const t=JSON.parse(e.data);t.conversation?w.current=t.conversation:b((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 b((e=>{e[e.length-1].loading=!1}))}};d&&(n=d("chat",n)),await u(n),s?.enable&&N()}catch(e){let t="未知错误";p(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),b((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}))}k(!1),l?.()}}),[d,i]),M=t((e=>{y||(w.current=e?.id,b(g(e)),x([]))}),[y,g]);return o(c,(()=>({reset:M,send:z}))),{messages:f,suggestions:v,loading:y,send:z,reset:M}}const K=function(e,t,r){function n(t){return t!==R.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(T.chunkString,{contentType:"string"}),o(t)):(e.consume(t),n)}function o(n){return n===R.rightSquareBracket?(e.exit(T.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)):_(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))}},X=function(){const e=this.data();e.micromarkExtensions.push({text:{[R.leftSquareBracket]:{tokenize:K}}}),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=g.Image,W={pre:e=>{let{children:t}=e;const r=n(null);return M(Z,{ref:r,children:[z(Y,{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 z("a",{...n,href:r,target:o})},img:e=>{let{node:t,...r}=e;return z(J,{...r})}},G=[[C,{singleDollarTextMath:!1}],F,E],Q=[S,[D,{detect:!1,ignoreMissing:!0}]],V=i((e=>{let{content:t,components:r,cite:n}=e;return t=t.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$"),z(ee,{remarkPlugins:a((()=>n?[...G,X]:G),[n]),rehypePlugins:Q,components:a((()=>({...W,...r})),[r]),children:t})})),Y=f(b)`
|
|
2
2
|
position: absolute;
|
|
3
3
|
right: 10px;
|
|
4
4
|
top: 1em;
|
|
@@ -298,7 +298,7 @@ import*as e from"react";import{useCallback as t,useState as r,useRef as n,useImp
|
|
|
298
298
|
.hljs-strong {
|
|
299
299
|
font-weight: 700;
|
|
300
300
|
}
|
|
301
|
-
`,te=s(null);function re(){const e=l(te);if(!e)throw new Error("useContext must be used within a Provider");return e}function ne(e){let{children:t,...r}=e;return
|
|
301
|
+
`,te=s(null);function re(){const e=l(te);if(!e)throw new Error("useContext must be used within a Provider");return e}function ne(e){let{children:t,...r}=e;return z(te.Provider,{value:r,children:t})}const oe=g.Image;function ie(e){let{src:t,...r}=e;const{imageResolver:n}=re();return n&&t&&(t=n(t)),z(ae,{children:z(oe,{src:t,...r})})}const ae=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,useRef as n,useImp
|
|
|
323
323
|
max-height: 100%;
|
|
324
324
|
max-width: 100%;
|
|
325
325
|
}
|
|
326
|
-
`;function se(e){let{tool:t}=e;const[n,o]=r(!1),{logLevel:i="none"}=re();let a=null;t.content&&("string"==typeof t.content?a
|
|
326
|
+
`;function se(e){let{tool:t}=e;const[n,o]=r(!1),{logLevel:i="none"}=re();let a=null;t.content&&("string"==typeof t.content?a=z(V,{content:t.content}):"image"===t.content.type&&(a=z(ie,{src:t.content.image})));const s="response"in t;return M(c,{children:["stats"==i&&z("div",{className:"mb-2",children:M("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?z("i",{className:"bi bi-x-circle-fill text-danger"}):z("i",{className:"bi bi-check-circle-fill text-success"}):z(B,{animation:"border",variant:"primary",size:"sm"}),z("span",{className:"text-muted",children:s?"已使用":"正在使用"}),z("span",{children:t.title})]})}),"all"==i&&z("div",{className:"mb-2",children:M("div",n&&s?{className:"shadow-sm rounded bg-white fs-7",children:[M("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[t.error?z("i",{className:"bi bi-x-circle-fill text-danger"}):z("i",{className:"bi bi-check-circle-fill text-success"}),z("span",{className:"text-muted",children:"已使用"}),z("span",{children:t.title}),z("i",{className:"bi bi-caret-up-fill text-muted"})]}),M("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[M("div",{className:"border rounded bg-light",children:[z("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:z("span",{className:"text-muted",children:"参数"})}),z("div",{className:"border-top p-2 overflow-hidden",children:t.arguments})]}),M("div",{className:"border rounded bg-light",children:[z("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:z("span",{className:"text-muted",children:"响应"})}),z(le,{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?z("i",{className:"bi bi-x-circle-fill text-danger"}):z("i",{className:"bi bi-check-circle-fill text-success"}):z(B,{animation:"border",variant:"primary",size:"sm"}),z("span",{className:"text-muted",children:s?"已使用":"正在使用"}),z("span",{children:t.title}),s&&z("i",{className:"bi bi-caret-down-fill text-muted"})]})}),a]})}const le=f.div`
|
|
327
327
|
white-space: pre-wrap;
|
|
328
|
-
`;var ce,de,me;function he(){return he=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},he.apply(this,arguments)}const ue=t=>e.createElement("svg",he({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},t),ce||(ce=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"}))),de||(de=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"}))),me||(me=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 pe=function(e){let{ext:t}=e;const{result:r}=k((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
|
|
328
|
+
`;var ce,de,me;function he(){return he=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},he.apply(this,arguments)}const ue=t=>e.createElement("svg",he({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},t),ce||(ce=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"}))),de||(de=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"}))),me||(me=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 pe=function(e){let{ext:t}=e;const{result:r}=k((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?z("img",{width:30,height:30,src:r}):null};function ge(e){let{className:t,name:r,size:n,loading:o,error:i,onRemove:a}=e;const s=O.extname(r).substring(1),l=M(xe,{className:t,$error:!!i,children:[M(ve,{children:[z(pe,{ext:s}),o&&z(B,{variant:"primary"})]}),M(be,{children:[z("h4",{children:r}),z("p",{children:x(n,1)})]}),a&&z(fe,{onClick:e=>{e.preventDefault(),a()},children:z("i",{className:"bi bi-trash3"})})]});return i?z(y,{placement:"top",tooltip:i,children:l}):l}const fe=f.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 t,useState as r,useRef as n,useImp
|
|
|
391
391
|
${e=>e.$error&&v`
|
|
392
392
|
border-color: var(--bs-danger) !important;
|
|
393
393
|
`}
|
|
394
|
-
`;var ye=d((function(e,t){let{children:r,tooltip:n,onClick:o,...i}=e;const a
|
|
394
|
+
`;var ye=d((function(e,t){let{children:r,tooltip:n,onClick:o,...i}=e;const a=z(ke,{ref:t,onClick:o,...i,children:r});return n?z(y,{tooltip:n,placement:"top",children:a}):a}));const ke=f.div`
|
|
395
395
|
align-items: center;
|
|
396
396
|
justify-content: center;
|
|
397
397
|
border-radius: .375rem;
|
|
@@ -415,7 +415,7 @@ import*as e from"react";import{useCallback as t,useState as r,useRef as n,useImp
|
|
|
415
415
|
background-color: var(--bs-gray-200);
|
|
416
416
|
}
|
|
417
417
|
}
|
|
418
|
-
`,we=i((e=>{let{message:t,actions:r,updater:n}=e;const{query:o,files:i=[],chunks:s,annotation:l}=t,{user:d,bot:m,logLevel:h="none"}=re(),u=a((()=>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
|
|
418
|
+
`,we=i((e=>{let{message:t,actions:r,updater:n}=e;const{query:o,files:i=[],chunks:s,annotation:l}=t,{user:d,bot:m,logLevel:h="none"}=re(),u=a((()=>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 M($,{children:[(o||i.length>0)&&M(Ce,{$reverse:!0,children:[d&&z($e,{children:z("img",{src:d.avatar})}),M(qe,{children:[o&&z(V,{content:o}),i.length>0&&i.map(((e,t)=>{let{name:r,size:n,path:o}=e;return w(o)?z(ie,{src:`/uploads/${o}`},t):z(Ne,{children:z(ge,{name:r,size:n})},t)}))]})]}),s&&M(Ce,{children:[z($e,{children:z("img",{src:m.avatar})}),M(qe,{children:[(o||i.length>0)&&M(ze,{children:[z(b,{placement:"top",content:u,as:ye,tooltip:!0}),r?.({Component:ye,message:t,content:u,setMessage:n})]}),z(Me,{children:["stats","all"].includes(h)&&t.stats&&M($,{children:[M("span",{children:["耗时 ",t.stats.latency/1e3," 秒"]}),M("span",{children:["花费 Token ",t.stats.usage]})]})}),z(je,{$deleted:!!l,children:s.length>0&&s.map(((e,t)=>M(c,{children:[e.content&&z(V,{content:e.content}),e.tools.map(((e,t)=>z(se,{tool:e},t))),e.error&&z(V,{content:`[${e.error}]`})]},t)))}),l&&M("div",{children:[M("div",{className:"d-flex align-items-center",children:[z("span",{className:"fs-7 text-secondary me-2",children:"标注的答案"}),z("hr",{className:"flex-fill"})]}),z(V,{content:l.answer})]}),t.loading&&z(ue,{})]})]})]})})),je=f.div`
|
|
419
419
|
${e=>e.$deleted&&v`
|
|
420
420
|
text-decoration-line: line-through;
|
|
421
421
|
color: var(--bs-secondary);
|
|
@@ -432,7 +432,7 @@ import*as e from"react";import{useCallback as t,useState as r,useRef as n,useImp
|
|
|
432
432
|
&:last-child {
|
|
433
433
|
margin-bottom: 0;
|
|
434
434
|
}
|
|
435
|
-
|
|
435
|
+
`,ze=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,useRef as n,useImp
|
|
|
440
440
|
gap: .5rem;
|
|
441
441
|
opacity: 0;
|
|
442
442
|
z-index: 1;
|
|
443
|
-
`,
|
|
443
|
+
`,Me=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,useRef as n,useImp
|
|
|
453
453
|
display: flex;
|
|
454
454
|
opacity: 0;
|
|
455
455
|
z-index: 1;
|
|
456
|
-
|
|
456
|
+
`,$e=f.div`
|
|
457
457
|
width: 35px;
|
|
458
458
|
height: 35px;
|
|
459
459
|
border-radius: 50%;
|
|
@@ -483,7 +483,7 @@ import*as e from"react";import{useCallback as t,useState as r,useRef as n,useImp
|
|
|
483
483
|
}
|
|
484
484
|
|
|
485
485
|
&:hover {
|
|
486
|
-
${
|
|
486
|
+
${Me}, ${ze} {
|
|
487
487
|
opacity: 1;
|
|
488
488
|
}
|
|
489
489
|
}
|
|
@@ -509,9 +509,9 @@ import*as e from"react";import{useCallback as t,useState as r,useRef as n,useImp
|
|
|
509
509
|
}
|
|
510
510
|
}
|
|
511
511
|
`};
|
|
512
|
-
`,Ee=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"];function Fe(e){let{variables:t,values:r,onSubmit:n,onChange:o,children:i}=e;const a={},s=[],l={};return t.forEach((e=>{a[e.key]={type:"string",title:e.label},"textarea"===e.type?l[e.key]={"ui:widget":"textarea"}:"select"===e.type&&e.options
|
|
512
|
+
`,Ee=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"];function Fe(e){let{variables:t,values:r,onSubmit:n,onChange:o,children:i}=e;const a={},s=[],l={};return t.forEach((e=>{a[e.key]={type:"string",title:e.label},"textarea"===e.type?l[e.key]={"ui:widget":"textarea"}:"select"===e.type&&e.options&&(a[e.key].enum=e.options,a[e.key].enumNames=e.options,e.options.length>0&&(a[e.key].default=e.options[0])),e.required&&s.push(e.key)})),z(j,{schema:{type:"object",properties:a,required:s},formData:r,omitExtraData:!0,uiSchema:l,submitText:"开始对话",onSubmit:n,onChange:o,children:i})}function Se(e){let{chat:o,fileTypes:i,scrollRef:a,onMessages:s,onSuggestions:l,variables:c,minRows:d,toolbar:g,disabled:f,onLoading:b,autoFocus:v}=e;const x=n(null),k=n(null),[w,j]=r(""),[$,q]=h([]),[C,E]=r(!1),F=i&&i.length>0?i:Ee,{loading:S,messages:D,suggestions:R,send:T}=H({...o,onSuccess(){requestAnimationFrame((()=>{x.current?.focus()})),o.onSuccess?.()}}),[_,B]=r((()=>{const e=D.filter((e=>!!e.query));if(e.length>0){return e[e.length-1].variables}}));m((()=>{s(D)}),[D]),m((()=>{l(R)}),[R]),m((()=>{b?.(S)}),[S]),m((()=>{!i&&$.length>0&&q([])}),[i]),m((()=>{const e=a.current;if(e&&T){const t=e=>{e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash&&(e.preventDefault(),T(e.target.text))};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}}),[a,T]);const O=t((e=>{let{target:t}=e;if(t.files){const e=Array.from(t.files).filter((e=>!!F.includes(L.extname(e.name))&&(!(e.size>20971520)&&e))).slice(0,6-$.length);q((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:`${o.url}/upload`,data:t};o.transformRequest&&(r=o.transformRequest("upload",r));const n=await u(r);q((t=>{const r=t.find((t=>t.file===e));r&&(r.path=n.path)}))}catch(t){const r=p(t)?t.message:"unknown error";q((t=>{const n=t.find((t=>t.file===e));n&&(n.error=r)}))}}))}t.value=""}),[$,F]),P=$.flatMap((e=>{let{file:t,path:r}=e;return r?[{name:t.name,size:t.size,path:r}]:[]})),I=()=>{(w||P.length>0)&&(c?T(w,P,{..._,...c.values}):T(w,P),j(""),q([]))};if(c){const e=c.config.filter((e=>!(e.key in(c.values||{})))),t=function(t){let r=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return!!t&&e.every((e=>{const n=!e.required||t?.[e.key];return!n&&r&&N.error("请完善必填信息"),n}))};if(e.length>0&&!t(_))return z(Re,{children:z(De,{children:z(Fe,{values:_,variables:e,onSubmit:e=>{let{formData:r}=e;if(t(r,!0))return B(r),!0}})})})}return M(Ae,{children:[g,M(Oe,{$focused:C&&!S,children:[$.length>0&&z(_e,{children:$.map(((e,t)=>{let{file:r,error:n,path:o}=e;const i=void 0===n&&void 0===o;return z(Te,{name:r.name,size:r.size,error:n,loading:i,onRemove:()=>{q((e=>{const t=e.findIndex((e=>e.file===r));-1!==t&&e.splice(t,1)}))}},t)}))}),M(Le,{children:[z(A,{disabled:S||f,ref:x,placeholder:"请输入你的问题, Enter+Shift换行, Enter发送",minRows:d,maxRows:5,value:w,onChange:e=>j(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),I())},autoFocus:v,onFocus:()=>E(!0),onBlur:()=>E(!1)}),i&&z(y,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:M(Be,{disabled:$.length>=6,onClick:()=>k.current?.click(),children:[z("input",{onChange:O,multiple:!0,accept:F.join(","),ref:k,type:"file",hidden:!0}),z("i",{className:"bi bi-file-earmark-arrow-up"})]})}),z(Be,{className:"text-primary",disabled:!w&&0===P.length,onClick:e=>{e.preventDefault(),I()},children:z("i",{className:"bi bi-send-fill"})})]})]})]})}const De=f.div`
|
|
513
513
|
border-radius: var(--bs-border-radius-lg);
|
|
514
|
-
box-shadow:
|
|
514
|
+
box-shadow: var(--bs-box-shadow-sm);
|
|
515
515
|
margin: 1rem;
|
|
516
516
|
padding: 1rem;
|
|
517
517
|
width: 100%;
|
|
@@ -580,7 +580,7 @@ import*as e from"react";import{useCallback as t,useState as r,useRef as n,useImp
|
|
|
580
580
|
box-shadow: 0.125rem 0.25rem 0.5rem rgba(0, 0, 0, 0.1);
|
|
581
581
|
`,Ae=f.div`
|
|
582
582
|
margin: 0 1rem 1rem;
|
|
583
|
-
`;function Pe(e){let{className:o,bot:i,user:a,input:s,logLevel:l,imageResolver:c,placeholder:d,actions:u,...p}=e;const[f,b]=h((()=>U(p.messages||[]))),[v,x]=r(!1),y=n(!s),k=n(null),w=t((()=>{const e=k.current;e&&requestAnimationFrame((()=>{e.scrollTo({top:e.scrollHeight})}))}),[]),{ref:j,height:N}=P(),[
|
|
583
|
+
`;function Pe(e){let{className:o,bot:i,user:a,input:s,logLevel:l,imageResolver:c,placeholder:d,actions:u,...p}=e;const[f,b]=h((()=>U(p.messages||[]))),[v,x]=r(!1),y=n(!s),k=n(null),w=t((()=>{const e=k.current;e&&requestAnimationFrame((()=>{e.scrollTo({top:e.scrollHeight})}))}),[]),{ref:j,height:N}=P(),[$,q]=r([]);return m((()=>{!v&&y.current||(v&&(y.current=!0),w())}),[N,v,y]),m((()=>{w()}),[$]),z(ne,{bot:i,user:a,logLevel:l,imageResolver:c,children:z(Ke,{className:o,children:M(g,{children:[z(Ue,{ref:k,children:M(He,{ref:j,children:[0===f.length&&d,f.slice(-30).map(((e,t)=>z(we,{actions:u,message:e,updater:t=>{b((r=>{const n=r.findIndex((t=>t.id===e.id));-1!==n&&t(r[n])}))}},t))),$.length>0&&z(Ie,{children:$.map(((e,t)=>z("a",{href:"#!question",children:e},t)))})]})}),s&&z(Se,{...s,onMessages:b,onSuggestions:q,scrollRef:k,onLoading:x})]})})})}const Ie=f.div`
|
|
584
584
|
display: flex;
|
|
585
585
|
flex-direction: column;
|
|
586
586
|
align-items: flex-start;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.35",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prebuild": "rimraf es types",
|
|
6
6
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
},
|
|
66
66
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
67
67
|
"license": "MIT",
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "72fc8ffdf592b98c9230a1a2f5bb352dc084c578"
|
|
69
69
|
}
|