@topthink/chat 1.0.24 → 1.0.25
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 +32 -32
- package/package.json +9 -3
- package/types/index.d.ts +1 -0
- package/types/utils/remark-cite.d.ts +38 -0
package/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import*as e from"react";import{useCallback as
|
|
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)`
|
|
2
2
|
position: absolute;
|
|
3
3
|
right: 10px;
|
|
4
4
|
top: 1em;
|
|
@@ -18,17 +18,17 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
|
|
|
18
18
|
&:hover {
|
|
19
19
|
opacity: 1;
|
|
20
20
|
}
|
|
21
|
-
`,
|
|
21
|
+
`,Q=f.pre`
|
|
22
22
|
position: relative;
|
|
23
23
|
|
|
24
24
|
&:hover {
|
|
25
|
-
${
|
|
25
|
+
${G} {
|
|
26
26
|
pointer-events: all;
|
|
27
27
|
transform: translateX(0px);
|
|
28
28
|
opacity: 0.5;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
`,
|
|
31
|
+
`,V=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 r,useState as t,useImperativeHandl
|
|
|
298
298
|
.hljs-strong {
|
|
299
299
|
font-weight: 700;
|
|
300
300
|
}
|
|
301
|
-
`,
|
|
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`
|
|
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 r,useState as t,useImperativeHandl
|
|
|
323
323
|
max-height: 100%;
|
|
324
324
|
max-width: 100%;
|
|
325
325
|
}
|
|
326
|
-
`;function
|
|
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`
|
|
327
327
|
white-space: pre-wrap;
|
|
328
|
-
`;var
|
|
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`
|
|
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 r,useState as t,useImperativeHandl
|
|
|
340
340
|
color: var(--bs-danger);
|
|
341
341
|
background-color: var(--bs-danger-bg-subtle);
|
|
342
342
|
}
|
|
343
|
-
`,
|
|
343
|
+
`,ue=f.div`
|
|
344
344
|
flex: 1;
|
|
345
345
|
overflow: hidden;
|
|
346
346
|
|
|
@@ -361,7 +361,7 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
|
|
|
361
361
|
line-height: 20px;
|
|
362
362
|
margin-bottom: 0;
|
|
363
363
|
}
|
|
364
|
-
`,
|
|
364
|
+
`,ge=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 r,useState as t,useImperativeHandl
|
|
|
375
375
|
position: absolute;
|
|
376
376
|
}
|
|
377
377
|
|
|
378
|
-
`,
|
|
378
|
+
`,fe=f.div`
|
|
379
379
|
display: flex;
|
|
380
380
|
gap: .5rem;
|
|
381
381
|
padding: .5rem;
|
|
@@ -388,10 +388,10 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
|
|
|
388
388
|
border-color: var(--bs-primary);
|
|
389
389
|
}
|
|
390
390
|
|
|
391
|
-
${e=>e.$error&&
|
|
391
|
+
${e=>e.$error&&x`
|
|
392
392
|
border-color: var(--bs-danger) !important;
|
|
393
393
|
`}
|
|
394
|
-
`;var
|
|
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`
|
|
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 r,useState as t,useImperativeHandl
|
|
|
415
415
|
background-color: var(--bs-gray-200);
|
|
416
416
|
}
|
|
417
417
|
}
|
|
418
|
-
`,
|
|
419
|
-
${e=>e.$deleted&&
|
|
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`
|
|
419
|
+
${e=>e.$deleted&&x`
|
|
420
420
|
text-decoration-line: line-through;
|
|
421
421
|
color: var(--bs-secondary);
|
|
422
422
|
`}
|
|
423
|
-
`,
|
|
423
|
+
`,ye=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 r,useState as t,useImperativeHandl
|
|
|
432
432
|
&:last-child {
|
|
433
433
|
margin-bottom: 0;
|
|
434
434
|
}
|
|
435
|
-
`,
|
|
435
|
+
`,we=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 r,useState as t,useImperativeHandl
|
|
|
440
440
|
gap: .5rem;
|
|
441
441
|
opacity: 0;
|
|
442
442
|
z-index: 1;
|
|
443
|
-
`,
|
|
443
|
+
`,je=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 r,useState as t,useImperativeHandl
|
|
|
453
453
|
display: flex;
|
|
454
454
|
opacity: 0;
|
|
455
455
|
z-index: 1;
|
|
456
|
-
`,
|
|
456
|
+
`,Ne=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 r,useState as t,useImperativeHandl
|
|
|
463
463
|
width: 100%;
|
|
464
464
|
height: 100%;
|
|
465
465
|
}
|
|
466
|
-
`,
|
|
466
|
+
`,ze=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 r,useState as t,useImperativeHandl
|
|
|
483
483
|
}
|
|
484
484
|
|
|
485
485
|
&:hover {
|
|
486
|
-
${
|
|
486
|
+
${je}, ${we} {
|
|
487
487
|
opacity: 1;
|
|
488
488
|
}
|
|
489
489
|
}
|
|
490
|
-
`,
|
|
490
|
+
`,Me=f.div`
|
|
491
491
|
display: flex;
|
|
492
492
|
justify-content: flex-start;
|
|
493
493
|
padding: 1rem 0 1rem 0;
|
|
494
|
-
${e=>e.$reverse&&
|
|
494
|
+
${e=>e.$reverse&&x`
|
|
495
495
|
flex-direction: row-reverse;
|
|
496
496
|
|
|
497
|
-
${
|
|
497
|
+
${ze} {
|
|
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 r,useState as t,useImperativeHandl
|
|
|
509
509
|
}
|
|
510
510
|
}
|
|
511
511
|
`};
|
|
512
|
-
|
|
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)`
|
|
513
513
|
width: calc((100% - .75rem) / 2);
|
|
514
|
-
|
|
514
|
+
`,Ee=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
|
-
`,
|
|
520
|
+
`,Fe=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 r,useState as t,useImperativeHandl
|
|
|
539
539
|
background-color: var(--bs-gray-200);
|
|
540
540
|
}
|
|
541
541
|
|
|
542
|
-
`,
|
|
542
|
+
`,Re=f.div`
|
|
543
543
|
display: flex;
|
|
544
544
|
padding: .5rem .5rem .5rem 1rem;
|
|
545
545
|
gap: .25rem;
|
|
@@ -556,21 +556,21 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
|
|
|
556
556
|
color: rgba(54, 54, 54, .3);
|
|
557
557
|
}
|
|
558
558
|
}
|
|
559
|
-
`,
|
|
559
|
+
`,Se=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
|
-
`,
|
|
564
|
+
`,De=f.div`
|
|
565
565
|
margin: 0 1rem 1rem;
|
|
566
|
-
`;function
|
|
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`
|
|
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
|
-
`,
|
|
573
|
+
`,Be=f.div`
|
|
574
574
|
display: flex;
|
|
575
575
|
flex-direction: column;
|
|
576
576
|
flex: 1;
|
|
@@ -578,4 +578,4 @@ import*as e from"react";import{useCallback as r,useState as t,useImperativeHandl
|
|
|
578
578
|
display: flex;
|
|
579
579
|
flex-direction: column;
|
|
580
580
|
height: 100%;
|
|
581
|
-
`;export{
|
|
581
|
+
`;export{W as Markdown,Te 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.
|
|
3
|
+
"version": "1.0.25",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prebuild": "rimraf es types",
|
|
6
6
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -18,14 +18,20 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@babel/runtime": "^7.11.2",
|
|
20
20
|
"@topthink/components": "^1.0.91",
|
|
21
|
+
"@types/mdast": "^4.0.4",
|
|
21
22
|
"katex": "^0.16.9",
|
|
23
|
+
"mdast-util-from-markdown": "^2.0.1",
|
|
24
|
+
"micromark-util-character": "^2.1.0",
|
|
25
|
+
"micromark-util-symbol": "^2.0.0",
|
|
26
|
+
"micromark-util-types": "^2.0.0",
|
|
22
27
|
"react-markdown": "^8.0.7",
|
|
23
28
|
"react-textarea-autosize": "^8.5.3",
|
|
24
29
|
"rehype-highlight": "^6.0.0",
|
|
25
|
-
"rehype-katex": "^6.0.
|
|
30
|
+
"rehype-katex": "^6.0.3",
|
|
26
31
|
"remark-breaks": "^3.0.2",
|
|
27
32
|
"remark-gfm": "^3.0.1",
|
|
28
33
|
"remark-math": "^5.1.1",
|
|
34
|
+
"unified": "^10.0.0",
|
|
29
35
|
"use-resize-observer": "^9.1.0"
|
|
30
36
|
},
|
|
31
37
|
"peerDependencies": {
|
|
@@ -56,5 +62,5 @@
|
|
|
56
62
|
},
|
|
57
63
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
58
64
|
"license": "MIT",
|
|
59
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "4c1d12f85f3287b2393d7a6f0ada99b9e739d7aa"
|
|
60
66
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export { default as useChat } from './hooks/use-chat';
|
|
3
3
|
export { default as MessageBox } from './components/message-box';
|
|
4
|
+
export { default as Markdown } from './components/markdown';
|
|
4
5
|
export { default as pickQuestions } from './utils/pick-questions';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Plugin } from 'unified';
|
|
2
|
+
import { Root } from 'mdast';
|
|
3
|
+
import { Options as FromMarkdownOptions } from 'mdast-util-from-markdown';
|
|
4
|
+
declare module 'micromark-util-types' {
|
|
5
|
+
interface TokenTypeMap {
|
|
6
|
+
cite: 'cite';
|
|
7
|
+
citeMarker: 'citeMarker';
|
|
8
|
+
citeData: 'citeData';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
declare module 'unified' {
|
|
12
|
+
interface Data {
|
|
13
|
+
micromarkExtensions: NonNullable<FromMarkdownOptions['extensions']>;
|
|
14
|
+
fromMarkdownExtensions: NonNullable<FromMarkdownOptions['mdastExtensions']>;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
interface FrozenProcessor {
|
|
18
|
+
data(): Data;
|
|
19
|
+
data<K extends keyof Data>(key: K): Data[K];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
declare module 'mdast' {
|
|
23
|
+
interface Cite extends Node {
|
|
24
|
+
type: 'cite';
|
|
25
|
+
data: {
|
|
26
|
+
hName: 'cite';
|
|
27
|
+
hChildren: [{
|
|
28
|
+
type: 'text';
|
|
29
|
+
value: string;
|
|
30
|
+
}];
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
interface RootContentMap {
|
|
34
|
+
cite: Cite;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
declare const remarkCite: Plugin<void[], Root, Root>;
|
|
38
|
+
export default remarkCite;
|