@topthink/chat 1.1.12 → 1.1.14
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 +3 -3
- package/types/components/message-box/list.d.ts +2 -1
package/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as n,css as i,formatSize as o,Tooltip as s,useAsync as a,isRequestError as l,Form as c,Toast as d,InfiniteScroller as h,Offcanvas as u,dayjs as m,RequestButton as p,useImmer as g,Lightbox as f,Clipboard as b,isImageUrl as v,request as x}from"@topthink/components";import*as y from"react";import{createContext as w,useContext as k,useRef as E,useCallback as j,useState as N,useEffect as C,forwardRef as M,useMemo as q,useImperativeHandle as z,Fragment as $,memo as S}from"react";import A from"react-textarea-autosize";import*as
|
|
1
|
+
import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as n,css as i,formatSize as o,Tooltip as s,useAsync as a,isRequestError as l,Form as c,Toast as d,InfiniteScroller as h,Offcanvas as u,dayjs as m,RequestButton as p,useImmer as g,Lightbox as f,Clipboard as b,isImageUrl as v,request as x}from"@topthink/components";import*as y from"react";import{createContext as w,useContext as k,useRef as E,useCallback as j,useState as N,useEffect as C,forwardRef as M,useMemo as q,useImperativeHandle as z,Fragment as $,memo as S}from"react";import A from"react-textarea-autosize";import*as R from"path";import F from"path";import{Spinner as D,Badge as I,CloseButton as T,OverlayTrigger as L,Popover as P}from"react-bootstrap";import B from"eventemitter3";import O from"lodash/isEqual";import _ from"react-markdown";import"katex/dist/katex.min.css";import U from"remark-math";import H from"remark-breaks";import K from"remark-gfm";import Q from"rehype-katex";import W from"rehype-highlight";import{codes as X,types as J}from"micromark-util-symbol";import{markdownLineEnding as V}from"micromark-util-character";import{createPortal as Y}from"react-dom";import G from"use-resize-observer";const Z=function(e){let{ext:r}=e;const{result:n}=a((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),[r]);return n?t("img",{width:30,height:30,src:n}):null};function ee(r){let{className:n,name:i,size:a,loading:l,error:c,onRemove:d}=r;const h=F.extname(i).substring(1),u=e(ie,{className:n,$error:!!c,children:[e(ne,{children:[t(Z,{ext:h}),l&&t(D,{variant:"primary"})]}),e(re,{children:[t("h4",{children:i}),t("p",{children:o(a,1)})]}),d&&t(te,{onClick:e=>{e.preventDefault(),d()},children:t("i",{className:"bi bi-trash3"})})]});return c?t(s,{placement:"top",tooltip:c,children:u}):u}const te=n.div`
|
|
2
2
|
width: 1.75rem;
|
|
3
3
|
height: 1.75rem;
|
|
4
4
|
align-items: center;
|
|
@@ -166,14 +166,14 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
166
166
|
margin-bottom: .5rem;
|
|
167
167
|
display: flex;
|
|
168
168
|
padding: 0 .125rem;
|
|
169
|
-
`;var Se=M((function(r,n){let{suggestion:i,fileTypes:o,speech:a,scrollRef:c,variables:h,minRows:u,toolbar:m,disabled:p,autoFocus:f,placeholder:b="请输入你的问题, Enter+Shift换行, Enter发送"}=r;const v=E(null),x=E(null),y=E(null),[w,k]=N(""),[M,q]=N(""),[$,S]=g([]),[
|
|
169
|
+
`;var Se=M((function(r,n){let{suggestion:i,fileTypes:o,speech:a,scrollRef:c,variables:h,minRows:u,toolbar:m,disabled:p,autoFocus:f,placeholder:b="请输入你的问题, Enter+Shift换行, Enter发送"}=r;const v=E(null),x=E(null),y=E(null),[w,k]=N(""),[M,q]=N(""),[$,S]=g([]),[F,D]=N(!1),I=o&&o.length>0?o:oe,{messages:L,loading:P,request:B}=ae(),{send:O}=function(e){let{suggestion:t,onSuccess:r}=e;const{setMessages:n,setSuggestions:i,setLoading:o,conversationId:s,setConversationId:a,request:c}=ae(),d=E(null),h=j((async()=>{d.current=new AbortController;try{let e={method:"post",url:"/suggestion",data:{conversation:s},signal:d.current.signal};const t=await c(e);i(t)}catch{}finally{d.current=null}}),[s,c]),u=j((async function(e){if("string"==typeof e){for(var u=arguments.length,m=new Array(u>1?u-1:0),p=1;p<u;p++)m[p-1]=arguments[p];e={query:e,...m}}const{query:g,files:f=[],variables:b={},quote:v}=e;if(g||f.length>0){d.current&&d.current.abort(),o(!0),i([]),n((e=>{e.push({query:g,quote:v,files:f,variables:b,chunks:[],loading:!0})}));try{let e={method:"post",data:{query:g,quote:v,files:f,variables:b,conversation:s},onMessage:e=>{if(e.data)if("[DONE]"!=e.data)try{const t=JSON.parse(e.data);t.conversation?a(t.conversation):n((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:"",reasoning:"",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.reasoning?r.chunks[e].reasoning+=t.chunks.reasoning:t.chunks.content&&(r.chunks[e].content+=t.chunks.content)}else t.stats?r.stats=t.stats:t.id&&(r.id=t.id)}))}catch(e){console.error(e)}else n((e=>{e[e.length-1].loading=!1}))}};await c(e),t&&h()}catch(e){let t="未知错误";l(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),n((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}))}o(!1),r?.()}}),[s,c]);return{send:u}}({suggestion:i,onSuccess(){requestAnimationFrame((()=>{v.current?.focus()}))}}),[_,U]=N();C((()=>{const e=L.filter((e=>!!e.query));U(e.at(-1)?.variables)}),[L]),C((()=>{!o&&$.length>0&&S([])}),[o]),C((()=>{const e=c.current;if(e&&O){const t=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const t={query:e.target.text};h&&(t.variables={..._,...h.values}),O(t)}};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}}),[h,_]);const H=j((e=>{let{target:t}=e;if(t.files){const e=Array.from(t.files).filter((e=>!!I.includes(R.extname(e.name))&&(!(e.size>20971520)&&e))).slice(0,6-$.length);S((t=>{t.push(...e.map((e=>({file:e}))))})),e.forEach((async e=>{try{const t=new FormData;t.set("file",e);const r=await B({method:"POST",url:"/upload",data:t});S((t=>{const n=t.find((t=>t.file===e));n&&(n.path=r.path)}))}catch(t){const r=l(t)?t.message:"unknown error";S((t=>{const n=t.find((t=>t.file===e));n&&(n.error=r)}))}}))}t.value=""}),[$,I]),K=$.flatMap((e=>{let{file:t,path:r}=e;return r?[{name:t.name,size:t.size,path:r}]:[]})),Q=j((()=>{if(w||K.length>0){const e={query:w,files:K};h&&(e.variables={..._,...h.values}),M&&(e.quote=M),O(e),k(""),q(""),S([]),y.current?.stop(!0)}}),[w,K,h,_,M]);if(z(n,(()=>({setQuery:k,setQuote:q,send:O,focus(){v.current?.focus()}}))),h){const e=h.config.filter((e=>!(e.key in(h.values||{})))),r=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&&d.error("请完善必填信息"),n}))};if(e.length>0&&!r(_))return t(Re,{children:t(Ae,{children:t(ce,{values:_,variables:e,onSubmit:e=>{let{formData:t}=e;if(r(t,!0))return U(t),!0}})})})}return e(Pe,{children:[m&&t(qe,{children:m}),e(Le,{$focused:F&&!P,children:[$.length>0&&t(De,{children:$.map(((e,r)=>{let{file:n,error:i,path:o}=e;const s=void 0===i&&void 0===o;return t(Fe,{name:n.name,size:n.size,error:i,loading:s,onRemove:()=>{S((e=>{const t=e.findIndex((e=>e.file===n));-1!==t&&e.splice(t,1)}))}},r)}))}),!!M&&e(Te,{children:[t("i",{className:"bi bi-quote"}),t("p",{children:M}),t(T,{onClick:()=>q("")})]}),e(Ie,{children:[t(A,{disabled:P||p,ref:v,placeholder:b,minRows:u,maxRows:5,value:w,onChange:e=>k(e.target.value),onKeyDown:e=>{"Enter"!==e.key||e.shiftKey||(e.preventDefault(),Q())},autoFocus:f,onFocus:()=>D(!0),onBlur:()=>D(!1)}),o&&t(s,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:e(de,{disabled:$.length>=6,onClick:()=>x.current?.click(),children:[t("input",{onChange:H,multiple:!0,accept:I.join(","),ref:x,type:"file",hidden:!0}),t("i",{className:"bi bi-file-earmark-arrow-up"})]})}),a&&t(me,{ref:y,model:a.model,onResult:e=>{v.current?.focus(),k((t=>t+e+" "))}}),t(de,{className:"text-primary",disabled:!w&&0===K.length,onClick:e=>{e.preventDefault(),Q()},children:t("i",{className:"bi bi-send-fill"})})]})]})]})}));const Ae=n.div`
|
|
170
170
|
border-radius: var(--bs-border-radius-lg);
|
|
171
171
|
box-shadow: var(--bs-box-shadow-sm);
|
|
172
172
|
margin: 1rem;
|
|
173
173
|
padding: 1rem;
|
|
174
174
|
width: 100%;
|
|
175
175
|
max-width: 500px;
|
|
176
|
-
`,
|
|
176
|
+
`,Re=n.div`
|
|
177
177
|
position: absolute;
|
|
178
178
|
left: 0;
|
|
179
179
|
right: 0;
|
|
@@ -183,7 +183,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
183
183
|
display: flex;
|
|
184
184
|
justify-content: center;
|
|
185
185
|
align-items: center;
|
|
186
|
-
`,
|
|
186
|
+
`,Fe=n(ee)`
|
|
187
187
|
width: calc((100% - .75rem) / 2);
|
|
188
188
|
`,De=n.div`
|
|
189
189
|
display: flex;
|
|
@@ -717,18 +717,11 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
717
717
|
background: rgba(var(--bs-primary-rgb), 0.1);
|
|
718
718
|
}
|
|
719
719
|
`};
|
|
720
|
-
`;function $t(r){const{placeholder:n,actions:i,cite:o,speech:s,renderItem:a}=r,{messages:
|
|
720
|
+
`;function $t(r){const{placeholder:n,actions:i,cite:o,speech:s,renderItem:a,scrollRef:l}=r,{messages:c,setMessages:d,loading:h,suggestions:u,conversationId:m}=ae(),p=E(r.loaded),g=j((()=>{const e=l.current;e&&requestAnimationFrame((()=>{e.scrollTo({top:e.scrollHeight})}))}),[]),f=E(null),{height:b}=G({ref:f});return C((()=>{(h||!p.current)&&c.length>0&&(h&&(p.current=!0),g())}),[b,h,p,c]),C((()=>{u.length>0&&g()}),[u]),C((()=>{p.current=r.loaded}),[m]),C((()=>{const e=f.current;if(e){const t=()=>{p.current=!0};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}}),[f]),e(St,{ref:f,children:[0===c.length&&n,c.slice(-30).map(((e,r)=>{const n={actions:i,cite:o,speech:s,message:e,setMessage:t=>{d((r=>{const n=r.findIndex((t=>t.id===e.id));-1!==n&&t(r[n])}))}};return a?a({Component:vt,props:n},r):t(vt,{...n},r)})),u.length>0&&t(At,{children:u.map(((e,r)=>t("a",{href:"#!question",children:e},r)))})]})}const St=n.div`
|
|
721
721
|
display: flex;
|
|
722
722
|
flex-direction: column;
|
|
723
723
|
flex: 1;
|
|
724
|
-
overflow-y: auto;
|
|
725
|
-
padding: 0 1rem;
|
|
726
|
-
margin-bottom: .5rem;
|
|
727
724
|
`,At=n.div`
|
|
728
|
-
display: flex;
|
|
729
|
-
flex-direction: column;
|
|
730
|
-
flex: 1;
|
|
731
|
-
`,Ft=n.div`
|
|
732
725
|
display: flex;
|
|
733
726
|
flex-direction: column;
|
|
734
727
|
align-items: flex-start;
|
|
@@ -745,7 +738,14 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
745
738
|
background: var(--bs-secondary-bg-subtle);
|
|
746
739
|
}
|
|
747
740
|
}
|
|
748
|
-
`,Rt=S(M(((r,n)=>{let{className:i,bot:o,user:s,input:a,logLevel:l,imageResolver:c,placeholder:d,actions:h,onboarding:u,conversation:m,cite:p,speech:b,renderItem:v,messages:y,request:w}=r;const k=j((e=>{const t=[];if(u&&!1!==u.enable){const e=Oe(u.questions||[]);t.push({chunks:[{content:[u.prologue,...e.map((e=>`[${e}](#!question)`))].join("\n"),tools:[]}],loading:u.loading})}return e?t.concat(Be(e.messages||[])):y?t.concat(Be(y)):t}),[u]),[M,$]=N(m?.id),[S,A]=g([]);C((()=>{A(k(m))}),[u]);const[F
|
|
741
|
+
`,Rt=S(M(((r,n)=>{let{className:i,bot:o,user:s,input:a,logLevel:l,imageResolver:c,placeholder:d,actions:h,onboarding:u,conversation:m,cite:p,speech:b,renderItem:v,messages:y,request:w}=r;const k=j((e=>{const t=[];if(u&&!1!==u.enable){const e=Oe(u.questions||[]);t.push({chunks:[{content:[u.prologue,...e.map((e=>`[${e}](#!question)`))].join("\n"),tools:[]}],loading:u.loading})}return e?t.concat(Be(e.messages||[])):y?t.concat(Be(y)):t}),[u]),[M,$]=N(m?.id),[S,A]=g([]);C((()=>{A(k(m))}),[u]);const[R,F]=N(!1),D=E(null),[I,T]=N([]),L=j((e=>{R||($(e?.id),A(k(e)),T([]))}),[R,k]);return z(n,(()=>({reset:L})),[L]),t(le,{request:q((()=>w?"string"==typeof w?x.create({baseURL:w}):w:x),[w]),bot:o,user:s,logLevel:l,imageResolver:c,conversationId:M,setConversationId:$,messages:S,setMessages:A,suggestions:I,setSuggestions:T,loading:R,setLoading:F,reset:L,children:t(Dt,{className:i,children:e(f,{children:[t(Ft,{ref:D,children:t($t,{placeholder:d,cite:p,actions:h,loaded:!a,speech:b,renderItem:v,scrollRef:D})}),a&&t(Se,{...a,scrollRef:D})]})})})})),O),Ft=n.div`
|
|
742
|
+
display: flex;
|
|
743
|
+
flex-direction: column;
|
|
744
|
+
flex: 1;
|
|
745
|
+
overflow-y: auto;
|
|
746
|
+
padding: 0 1rem;
|
|
747
|
+
margin-bottom: .5rem;
|
|
748
|
+
`,Dt=n.div`
|
|
749
749
|
display: flex;
|
|
750
750
|
flex-direction: column;
|
|
751
751
|
height: 100%;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.14",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prebuild": "rimraf es types",
|
|
6
6
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@babel/runtime": "^7.11.2",
|
|
20
20
|
"@breezystack/lamejs": "^1.2.7",
|
|
21
|
-
"@topthink/components": "^1.1.
|
|
21
|
+
"@topthink/components": "^1.1.9",
|
|
22
22
|
"@types/mdast": "^4.0.4",
|
|
23
23
|
"eventemitter3": "^5.0.1",
|
|
24
24
|
"katex": "^0.16.9",
|
|
@@ -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": "71aa5f67c2f6a9788626856addb94249e138b6fd"
|
|
69
69
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import MessageItem, { MessageActions, MessageItemProps } from './message-item';
|
|
2
|
-
import { ReactNode } from 'react';
|
|
2
|
+
import { MutableRefObject, ReactNode } from 'react';
|
|
3
3
|
import { Components } from 'react-markdown';
|
|
4
4
|
interface Props {
|
|
5
5
|
placeholder: ReactNode;
|
|
@@ -15,6 +15,7 @@ interface Props {
|
|
|
15
15
|
Component: typeof MessageItem;
|
|
16
16
|
props: MessageItemProps;
|
|
17
17
|
}, index: number) => ReactNode;
|
|
18
|
+
scrollRef: MutableRefObject<HTMLDivElement | null>;
|
|
18
19
|
}
|
|
19
20
|
export default function List(props: Props): import("react/jsx-runtime").JSX.Element;
|
|
20
21
|
export {};
|