@topthink/chat 1.3.1 → 1.3.3
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 +80 -77
- package/package.json +3 -3
- package/types/components/chat-input/index.d.ts +34 -0
- package/types/components/message-box/context.d.ts +20 -12
- package/types/components/message-box/file-icon.d.ts +5 -0
- package/types/components/message-box/index.d.ts +3 -0
- package/types/components/message-box/input-box/index.d.ts +3 -1
- package/types/index.d.ts +4 -0
package/es/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{styled as n,useAsync as i,css as o,formatSize as s,Tooltip as a,isCancel as l,isRequestError as c,Form as d,Toast as u,InfiniteScroller as m,useDebounce as h,Offcanvas as p,ModalForm as g,RequestButton as f,Loader as b,useImmer as v,getAbsoluteUrl as y,Lightbox as x,Clipboard as w,Button as k,isImageUrl as E,request as N}from"@topthink/components";import*as j from"react";import{createContext as C,useContext as $,useRef as R,useCallback as S,useEffect as M,useState as q,forwardRef as F,useMemo as A,useImperativeHandle as z,Fragment as I,memo as L,Component as D}from"react";import*as P from"path";import T from"path";import{Spinner as O,Badge as B,Dropdown as U,FormControl as _,CloseButton as H,OverlayTrigger as Q,Popover as J}from"react-bootstrap";import K from"eventemitter3";import W from"react-textarea-autosize";import X from"lodash/isEqual";import{toString as V}from"hast-util-to-string";import Y from"react-markdown";import"katex/dist/katex.min.css";import G from"rehype-highlight";import Z from"rehype-katex";import ee from"remark-breaks";import te from"remark-gfm";import re from"remark-math";import{codes as ne,types as ie}from"micromark-util-symbol";import{markdownLineEnding as oe}from"micromark-util-character";import{createPortal as se}from"react-dom";import ae from"use-resize-observer";import{unified as le}from"unified";import ce from"rehype-stringify";import de from"remark-parse";import ue from"remark-rehype";function me(e,t){return e.name===t}function he({name:t,className:r}){const n=T.extname(t).substring(1),{result:o}=i(async e=>{try{return(await function(e){switch(e){case"../../images/file/docx.svg":return import("./docx-2LwnwWwv.js");case"../../images/file/html.svg":return import("./html-BSKTzJZQ.js");case"../../images/file/jpg.svg":return import("./jpg-muicsKjX.js");case"../../images/file/md.svg":return import("./md-DRS7QmE8.js");case"../../images/file/mp3.svg":return import("./mp3-HhP1Gk4B.js");case"../../images/file/mp4.svg":return import("./mp4-CWxSyjdn.js");case"../../images/file/pdf.svg":return import("./pdf-CWBi2u4Z.js");case"../../images/file/png.svg":return import("./png-DNwbFeJk.js");case"../../images/file/pptx.svg":return import("./pptx-Bv4V_F_3.js");case"../../images/file/py.svg":return import("./py-D30pRTJN.js");case"../../images/file/txt.svg":return import("./txt-CsqmEKjF.js");case"../../images/file/unknown.svg":return import("./unknown-DW8znerP.js");case"../../images/file/xlsx.svg":return import("./xlsx-CmkshfKZ.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}catch{return(await import("./unknown-DW8znerP.js")).default}},[n]);return o?e(pe,{src:o,className:r}):null}const pe=n.img`
|
|
2
|
+
width: 30px;
|
|
3
|
+
height: 30px;
|
|
4
|
+
`;function ge({className:r,name:n,size:i=0,loading:o,error:l,onRemove:c,onClick:d}){const u=t(ye,{className:r,$error:!!l,onClick:d,children:[t(ve,{children:[e(he,{name:n}),o&&e(O,{variant:"primary"})]}),t(be,{children:[e("h4",{children:n}),i>0&&e("p",{children:s(i,1)})]}),c&&e(fe,{onClick:e=>{e.preventDefault(),c()},children:e("i",{className:"bi bi-trash3"})})]});return l?e(a,{placement:"top",tooltip:l,children:u}):u}const fe=n.div`
|
|
2
5
|
width: 1.75rem;
|
|
3
6
|
height: 1.75rem;
|
|
4
7
|
align-items: center;
|
|
@@ -13,7 +16,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
13
16
|
color: var(--bs-danger);
|
|
14
17
|
background-color: var(--bs-danger-bg-subtle);
|
|
15
18
|
}
|
|
16
|
-
`,
|
|
19
|
+
`,be=n.div`
|
|
17
20
|
flex: 1;
|
|
18
21
|
overflow: hidden;
|
|
19
22
|
|
|
@@ -34,7 +37,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
34
37
|
line-height: 20px;
|
|
35
38
|
margin-bottom: 0;
|
|
36
39
|
}
|
|
37
|
-
`,
|
|
40
|
+
`,ve=n.div`
|
|
38
41
|
position: relative;
|
|
39
42
|
width: 3rem;
|
|
40
43
|
height: 3rem;
|
|
@@ -48,7 +51,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
48
51
|
position: absolute;
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
`,
|
|
54
|
+
`,ye=n.div`
|
|
52
55
|
display: flex;
|
|
53
56
|
gap: .5rem;
|
|
54
57
|
padding: .5rem;
|
|
@@ -62,10 +65,10 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
62
65
|
border-color: var(--bs-primary);
|
|
63
66
|
}
|
|
64
67
|
|
|
65
|
-
${e=>e.$error&&
|
|
68
|
+
${e=>e.$error&&o`
|
|
66
69
|
border-color: var(--bs-danger) !important;
|
|
67
70
|
`}
|
|
68
|
-
`,
|
|
71
|
+
`,xe=[".txt",".md",".pdf",".docx",".xlsx",".pptx",".png",".jpg"],we=C(null),ke=C(null);function Ee(){const e=$(we);if(!e)throw new Error("useChatContext must be used within a ChatProvider");return e}function Ne(){return $(ke)}function je(){const e=$(we),t=$(ke);if(!e||!t)throw new Error("useContext must be used within a Provider");return{...e,...t}}function Ce({children:t,...r}){return e(we.Provider,{value:r,children:t})}function $e({children:t,...r}){const{request:n,loading:i,setLoading:o,...s}=r;return e(we.Provider,{value:{request:n,loading:i,setLoading:o},children:e(ke.Provider,{value:s,children:t})})}function Re({variables:t,values:r,onSubmit:n,onChange:i,children:o}){const s={},a=[],l={};return t.forEach(e=>{s[e.key]={type:"string",title:e.label},"textarea"===e.type?l[e.key]={"ui:widget":"textarea"}:"select"===e.type&&e.options&&(s[e.key].enum=e.options,s[e.key].enumNames=e.options,e.options.length>0&&(s[e.key].default=e.options[0])),e.required&&a.push(e.key)}),e(d,{schema:{type:"object",properties:s,required:a},formData:r,omitExtraData:!0,uiSchema:l,submitText:"开始对话",onSubmit:n,onChange:i,children:o})}const Se=n.button`
|
|
69
72
|
color: var(--bs-secondary);
|
|
70
73
|
cursor: pointer;
|
|
71
74
|
display: flex;
|
|
@@ -88,7 +91,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
88
91
|
background-color: var(--bs-gray-200);
|
|
89
92
|
}
|
|
90
93
|
|
|
91
|
-
${({$fill:e})=>e&&
|
|
94
|
+
${({$fill:e})=>e&&o`
|
|
92
95
|
background-color: var(--bs-gray-200);
|
|
93
96
|
|
|
94
97
|
&:hover:not(:disabled) {
|
|
@@ -96,12 +99,12 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
96
99
|
}
|
|
97
100
|
`};
|
|
98
101
|
|
|
99
|
-
`;let
|
|
102
|
+
`;let Me=class{#e;#t;constructor(){this.#t=new K}onResult(e){this.#t.on("result",e)}onRecording(e){this.#t.on("recording",e)}onTranscribing(e){this.#t.on("transcribing",e)}start(){if(!this.#e){const e=window.SpeechRecognition||window.webkitSpeechRecognition;if(!e)return void u.error("浏览器不支持语音识别");const t=new e;t.continuous=!0,t.lang=navigator.language,t.onresult=e=>{const t=Array.from(e.results).at(-1)?.item(0).transcript;t&&this.#t.emit("result",t)},t.onspeechend=()=>{this.#t.emit("recording",!1)},t.onerror=e=>{this.#t.emit("recording",!1),u.error(e.error)},this.#e=t}this.#t.emit("recording",!0),this.#e.start()}stop(){this.#e?.stop(),this.#t.emit("recording",!1)}},qe=class{#t;#r;#n;#i;constructor(e){this.#r=e,this.#t=new K}async start(){try{this.#n=await navigator.mediaDevices.getUserMedia({audio:!0}),this.#i=new AudioContext;const e=this.#i.createScriptProcessor(0,1,1);this.#i.createMediaStreamSource(this.#n).connect(e),e.connect(this.#i.destination);let t=[],r=0;const n=.01,i=1;e.onaudioprocess=e=>{const o=e.playbackTime,s=e.inputBuffer.getChannelData(0);let a=!0;for(let e=0;e<s.length;e++)if(Math.abs(s[e])>n){a=!1;break}if(a){const e=o-r;e>i&&(t.length>1&&(this.save(t),t=[]),e>10&&this.stop())}else t.push(new Float32Array(s)),r=o},this.#t.emit("recording",!0)}catch(e){u.error(e.message)}}convertBuffer(e){const t=new Float32Array(e),r=new Int16Array(e.length);for(let e=0;e<t.length;e++){const n=Math.max(-1,Math.min(1,t[e]));r[e]=n<0?32768*n:32767*n}return r}async encodeMP3(e){const{Mp3Encoder:t}=await import("@breezystack/lamejs"),r=new t(1,44100,128),n=[];for(const t of e){const e=this.convertBuffer(t),i=1152;let o=e.length;for(let t=0;o>=0;t+=i){const s=e.subarray(t,t+i),a=r.encodeBuffer(s);n.push(new Int8Array(a)),o-=i}}return n.push(r.flush()),new Blob(n,{type:"audio/mp3"})}async save(e){const t=await this.encodeMP3(e);try{this.#t.emit("transcribing",!0);const e=await this.#r(t);e&&this.#t.emit("result",e)}catch(e){}finally{this.#t.emit("transcribing",!1)}}stop(){this.#n&&(this.#n.getTracks().forEach(e=>e.stop()),this.#n=void 0),this.#i&&(this.#i.close(),this.#i=void 0),this.#t.emit("recording",!1)}onResult(e){this.#t.on("result",e)}onRecording(e){this.#t.on("recording",e)}onTranscribing(e){this.#t.on("transcribing",e)}};const Fe=F(({onResult:r,model:n="builtin"},i)=>{const{request:o}=Ee(),s=A(()=>{if("builtin"!==n)return async e=>{const t=new FormData;t.append("file",e,"audio.mp3"),t.append("model",n);return(await o({url:"/transcriptions",method:"post",data:t})).text}},[n]),{start:l,stop:c,recording:d,transcribing:u}=function({onResult:e,loader:t}){const r=R(),[n,i]=q(!1),[o,s]=q(!1);return M(()=>()=>{r.current&&(r.current.stop(),r.current=void 0)},[]),{recording:n,transcribing:o,start:async()=>{r.current||(r.current=t?new qe(t):new Me,r.current.onRecording(i),r.current.onTranscribing(s),r.current.onResult(e)),r.current.start()},stop:()=>{r.current&&r.current.stop()}}}({onResult:r,loader:s});return z(i,()=>({start:l,stop:c}),[l,c]),e(a,{tooltip:d?"停止":"语音输入",placement:"top",children:t(Se,{onMouseDown:e=>{e.preventDefault(),d?c():l()},children:[u&&e(Ae,{animation:"border",variant:"primary",size:"sm"}),e("i",d?{className:"bi bi-mic-fill text-danger"}:{className:"bi bi-mic"})]})})}),Ae=n(O)`
|
|
100
103
|
position: absolute;
|
|
101
104
|
--bs-spinner-width: .75rem;
|
|
102
105
|
--bs-spinner-height: .75rem;
|
|
103
106
|
--bs-spinner-border-width: 0.1em;
|
|
104
|
-
`,
|
|
107
|
+
`,ze=F(({className:t,children:r,size:n,tooltip:i,placement:o,onClick:s,onMouseDown:l},c)=>{const d=e(Ie,{ref:c,className:t,onClick:s,onMouseDown:l,$size:n,children:r});return i?e(a,{tooltip:i,placement:o,children:d}):d}),Ie=n.div`
|
|
105
108
|
width: 2rem;
|
|
106
109
|
height: 2rem;
|
|
107
110
|
align-items: center;
|
|
@@ -116,7 +119,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
116
119
|
font-size: 1.5rem;
|
|
117
120
|
}
|
|
118
121
|
|
|
119
|
-
${({$size:e})=>"sm"===e&&
|
|
122
|
+
${({$size:e})=>"sm"===e&&o`
|
|
120
123
|
width: 1.5rem;
|
|
121
124
|
height: 1.5rem;
|
|
122
125
|
font-size: 1rem;
|
|
@@ -125,10 +128,10 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
125
128
|
&:hover {
|
|
126
129
|
background-color: var(--bs-secondary-bg-subtle);
|
|
127
130
|
}
|
|
128
|
-
`;function
|
|
131
|
+
`;function Le(){const{request:n,reset:i,conversationId:o}=je(),[s,a]=q(!1),[l,c]=q(""),[d,u]=q(""),m=h(e=>{u(e)},500);return M(()=>{m(l)},[l]),t(r,{children:[e(ze,{tooltip:"历史记录",placement:"top",onClick:()=>a(!0),children:e("i",{className:"bi bi-clock-history"})}),e(p,{show:s,onHide:()=>{a(!1)},placement:"end",headerAs:Be,header:t(r,{children:[e("span",{children:"聊天历史记录"}),e(Ue,{children:e(_,{type:"search",placeholder:"搜索聊天记录...",value:l,onChange:e=>c(e.target.value),autoFocus:!0})})]}),bodyAs:De,children:s&&e(Oe,{children:e(Te,{useWindow:!1,source:e=>n({url:"/conversation",params:{...e,...d&&{keyword:d}}}),render:({data:s,loading:l,setData:c})=>s.length||l?t(r,{children:[s.map((s,l)=>{const d=o===s.id;return t(I,{children:[l>0&&e("hr",{className:"mx-2 my-2"}),t(Pe,{children:[d&&e(B,{bg:"secondary",children:"当前"}),e("span",{className:"overflow-hidden text-truncate flex-fill",onClick:()=>{i(s),a(!1)},children:s.title||"未命名对话"}),t(U,{align:"end",children:[e(U.Toggle,{size:"sm",className:"no-caret",as:ze,children:e("i",{className:"bi bi-three-dots-vertical"})}),t(U.Menu,{children:[e(g,{buttonProps:{as:U.Item},text:t(r,{children:[e("i",{className:"bi bi-pencil me-2"}),"重命名"]}),modalProps:{header:"编辑对话名称"},schema:{type:"object",properties:{title:{type:"string"}}},uiSchema:{title:{"ui:label":!1,"ui:autofocus":!0}},formData:{title:s.title},onRequest:e=>n({url:`/conversation/${s.id}`,method:"PUT",data:e}),onSuccess:(e,t)=>{c(e=>{const r=e.findIndex(e=>e.id===s.id);-1!==r&&Object.assign(e[r],t)})}}),t(f,{as:U.Item,confirm:"确定要删除吗?",onRequest:()=>n({url:`/conversation/${s.id}`,method:"DELETE"}),onSuccess:()=>{c(e=>{const t=e.findIndex(e=>e.id===s.id);-1!==t&&e.splice(t,1)}),d&&i()},children:[e("i",{className:"bi bi-trash3 me-2"}),"删除"]})]})]})]})]},s.id)}),l&&e(b,{wrap:!0})]}):e("div",{className:"text-muted text-center py-5",children:d?"未找到匹配的聊天记录":"暂无对话记录"})},d)})})]})}const De=n.div`
|
|
129
132
|
padding: 0;
|
|
130
133
|
overflow: hidden;
|
|
131
|
-
`,
|
|
134
|
+
`,Pe=n.div`
|
|
132
135
|
gap: .5rem;
|
|
133
136
|
display: flex;
|
|
134
137
|
cursor: pointer;
|
|
@@ -147,50 +150,49 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
147
150
|
display: block;
|
|
148
151
|
}
|
|
149
152
|
}
|
|
150
|
-
`,
|
|
153
|
+
`,Te=n(m)`
|
|
151
154
|
display: flex;
|
|
152
155
|
flex-direction: column;
|
|
153
156
|
padding: .5rem;
|
|
154
|
-
`,
|
|
157
|
+
`,Oe=n.div`
|
|
155
158
|
height: 100%;
|
|
156
159
|
overflow-y: auto;
|
|
157
|
-
`,
|
|
160
|
+
`,Be=n.div`
|
|
158
161
|
display: flex;
|
|
159
162
|
align-items: center;
|
|
160
163
|
justify-content: space-between;
|
|
161
164
|
gap: 0.5rem;
|
|
162
165
|
flex: 1;
|
|
163
166
|
margin-right: .5rem;
|
|
164
|
-
`,
|
|
167
|
+
`,Ue=n.div`
|
|
165
168
|
display: flex;
|
|
166
169
|
align-items: center;
|
|
167
170
|
gap: 0.5rem;
|
|
168
|
-
`;var
|
|
171
|
+
`;var _e;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)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},He.apply(null,arguments)}const Qe=e=>j.createElement("svg",He({width:"1em",height:"1em",fill:"currentColor",viewBox:"0 0 256 256"},e),_e||(_e=j.createElement("path",{fillRule:"evenodd",d:"M128 22C69.458 22 22 69.458 22 128v96c0 5.523 4.477 10 10 10h96c58.542 0 106-47.458 106-106S186.542 22 128 22m0 56c5.523 0 10 4.477 10 10v30h30c5.523 0 10 4.477 10 10s-4.477 10-10 10h-30v30c0 5.523-4.477 10-10 10s-10-4.477-10-10v-30H88c-5.523 0-10-4.477-10-10s4.477-10 10-10h30V88c0-5.523 4.477-10 10-10",clipRule:"evenodd","data-follow-fill":"#000"})));function Je({onClick:t}){const{reset:r}=je();return e(ze,{className:"text-primary",tooltip:"新会话",placement:"top",onClick:()=>t?.()??r(),children:e(Qe,{})})}function Ke({children:n,actions:i}){const o=t(r,{children:[e(Le,{}),e(Je,{})]}),s="function"==typeof i?i({History:Le,Reset:Je}):t(r,{children:[i,o]});return t(Xe,{children:[n,e(We,{children:s})]})}const We=n.div`
|
|
169
172
|
display: flex;
|
|
170
173
|
gap: .25rem;
|
|
171
174
|
margin-left: auto;
|
|
172
|
-
`,
|
|
175
|
+
`,Xe=n.div`
|
|
173
176
|
margin-bottom: .5rem;
|
|
174
177
|
display: flex;
|
|
175
178
|
padding: 0 .125rem;
|
|
176
|
-
`;var
|
|
179
|
+
`;var Ve=F(function({readonly:t,autoFocus:r,placeholder:n,minRows:i,maxRows:o,onSubmit:s,onFocusedChange:a,onQueryChange:l,onPaste:c},d){const u=R(null),[m,h]=q(""),p=S(()=>{const e=u.current;if(!e)return;e.focus();const t=e.value.length;e.setSelectionRange(t,t)},[]),g=S(e=>{const t="string"==typeof e?e:e(m);h(t),l?.(t.trim()),requestAnimationFrame(()=>{p()})},[m,l,p]);return z(d,()=>({setContent:g,focus(){p()}}),[g,p]),e(Ye,{ref:u,value:m,minRows:i,maxRows:o,readOnly:t,autoFocus:r,placeholder:n,onFocus:()=>a?.(!0),onBlur:()=>a?.(!1),onChange:e=>{h(e.target.value),l?.(e.target.value.trim())},onPaste:e=>{c?.(e.nativeEvent)},onKeyDown:e=>{e.nativeEvent.isComposing||"Enter"!==e.key||e.shiftKey||(e.preventDefault(),e.stopPropagation(),s?.())}})});const Ye=n(W)`
|
|
177
180
|
min-height: calc(1.5em + 1rem);
|
|
178
181
|
width: 100%;
|
|
179
182
|
padding: 0.5rem 0.5rem 0.5rem 0.75rem;
|
|
180
183
|
border: 0;
|
|
181
184
|
outline: none;
|
|
182
185
|
resize: none;
|
|
183
|
-
flex: 1;
|
|
184
186
|
line-height: 1.5;
|
|
185
187
|
background: transparent;
|
|
186
|
-
`;var
|
|
188
|
+
`;var Ge=F(function({className:r,fileTypes:n,speech:i,scrollRef:o,variables:s,minRows:d=1,maxRows:m=5,toolbar:h,toolbarActions:p,disabled:g,autoFocus:f,tools:b,renderEditableInput:y,placeholder:x="请输入你的问题, Enter+Shift换行, Enter发送",supportStop:w=!1,onEvent:k,onNewConversation:E},N){const j=R(null),C=R(null),$=R(null),[F,I]=q(""),[L,D]=q(""),[T,O]=v([]),[B,U]=q(!1),_=n&&n.length>0?n:xe,Q=S(()=>{requestAnimationFrame(()=>{$.current?.focus()})},[]),{send:J,resume:K,stop:W}=function(e){const{setLoading:t,request:r}=Ee(),n=Ne(),i=n?.setMessages,o=n?.setConversationId,s=n?.conversationId,a=n?.messages,d=!0===e?.supportStop,{onEvent:u,onNewConversation:m,onComplete:h}=e??{},p=R(null),g=R(),f=R(s),b=()=>(p.current&&p.current.abort(),p.current=new AbortController,p.current.signal),v=S(e=>{const t=(e,r)=>{Object.entries(r).forEach(([r,n])=>{if("string"!=typeof n)if(!n||"object"!=typeof n||Array.isArray(n))e[r]=n;else if("index"in n){const{index:i,...o}=n;Array.isArray(e[r])||(e[r]=[]),e[r][i]||(e[r][i]={}),t(e[r][i],o)}else e[r]=n;else e[r]="string"==typeof e[r]?e[r]+n:n})};if(e.data)if("[DONE]"!=e.data)try{const r=JSON.parse(e.data);r.conversation?(f.current=r.conversation,o?.(r.conversation)):r.event?u?.(r.event):i?.(e=>{const n=e[e.length-1];if(r.chunks){if(n.chunks){const e=r.chunks.index;if(n.chunks[e]||(n.chunks[e]={content:"",reasoning:"",tools:[]}),r.chunks.error)n.chunks[e].error=r.chunks.error;else if(r.chunks.tools){n.chunks[e].tools||(n.chunks[e].tools=[]);const{index:i,...o}=r.chunks.tools;n.chunks[e].tools[i]||(n.chunks[e].tools[i]={name:"",title:"",arguments:""}),t(n.chunks[e].tools[i],o)}else if(r.chunks.reasoning)n.chunks[e].reasoning+=r.chunks.reasoning;else if(r.chunks.content)if("object"==typeof r.chunks.content){Array.isArray(n.chunks[e].content)||(n.chunks[e].content=[]);const t=r.chunks.content.index,i=r.chunks.content.value;"string"==typeof i?(n.chunks[e].content[t]||(n.chunks[e].content[t]=""),n.chunks[e].content[t]+=i):n.chunks[e].content[t]=i}else n.chunks[e].content+=r.chunks.content;else r.chunks.node&&(n.chunks[e].node={...n.chunks[e].node,...r.chunks.node})}}else r.stats?n.stats=r.stats:r.id&&(n.id=r.id)})}catch(e){console.error(e)}else i?.(e=>{e[e.length-1].loading=!1})},[u]),y=S(e=>{if(l(e))i?.(e=>{const t=e[e.length-1];t&&(t.loading=!1)});else{let t="未知错误";c(e)&&(t=401==e.response?.status?"未授权或授权已过期,请刷新页面后重试":"string"==typeof e.errors?e.errors:Object.values(e.errors).join("\n")),i?.(e=>{const r=e[e.length-1];r.chunks&&(0===r.chunks.length?r.chunks=[{error:t}]:r.chunks[r.chunks.length-1].error=t),r.loading=!1})}},[]),x=S(async e=>{g.current=e;try{const n=b();t(!0),i?.(e=>{e[e.length-1].loading=!0,delete e[e.length-1].stats});let o={url:"message",method:"get",params:{id:e},onMessage:v,signal:n};await r(o)}catch(e){y(e)}finally{p.current=null,g.current=void 0}t(!1),h?.()},[r,u,m,h]),w=S(async(e,...o)=>{"string"==typeof e&&(e={query:e,...o});const{query:s,files:a=[],variables:l={},quote:c}=e;let d=null,u=null;if(s||a.length>0){const e=b();t(!0),n?.setSuggestions?.([]),i?.(e=>{e.push({query:s,quote:c,files:a,variables:l,chunks:[],loading:!0})});try{let t={method:"post",data:{query:s,quote:c,files:a,variables:l,conversation:f.current},onMessage:v,signal:e};const n=await r(t);d=n.message??null,u=n.conversation??null}catch(e){y(e)}finally{p.current=null}if(d){let e=!1;u&&!f.current&&(e=m?.(u)??!1),e||await x(d.id)}else t(!1),h?.()}},[r,u,m,h]),k=S(async e=>{let n=null;try{const o=b();t(!0),i?.(e=>{e[e.length-1].loading=!0});let s={url:"resume",method:"post",data:{conversation:f.current,chunk:e.chunk,tool:e.tool,payload:e.payload},onMessage:v,signal:o};n=(await r(s)).message??null}catch(e){y(e)}finally{p.current=null}n?await x(n.id):(t(!1),h?.())},[r,u,m,h]),E=S(async()=>{if(d)if(g.current)try{await r({url:"stop",method:"post",data:{id:g.current}})}catch{}else p.current?.abort()},[r,d]),N=()=>{const e=a?.[a.length-1];e&&e.id&&2===e.status&&x(e.id)};return M(()=>(N(),()=>{p.current&&p.current.abort()}),[]),M(()=>{s!=f.current&&(f.current=s,p.current&&p.current.abort(),N())},[s]),{send:w,resume:k,stop:E}}({supportStop:w,onEvent:k,onNewConversation:E,onComplete:Q}),{loading:X,request:V}=Ee(),Y=Ne(),[G,Z]=q();M(()=>{const e=(Y?.messages||[]).filter(e=>!!e.query);Z(e.at(-1)?.variables)},[Y?.messages]),M(()=>{!n&&T.length>0&&O([])},[n]),M(()=>{const e=o?.current;if(e){const t=e=>{if(e.target instanceof HTMLAnchorElement&&"#!question"===e.target.hash){e.preventDefault();const t={query:e.target.text};s&&(t.variables={...G,...s.values}),J(t)}};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[s,G]);const ee=S(e=>{const t=e.map((e,t)=>{if(e.name)return e;const r=e.type.split("/")[1]||"png";return new File([e],`screenshot-${Date.now()}-${t}.${r}`,{type:e.type})}).filter(e=>!(!_.includes("*")&&!_.includes(P.extname(e.name)))&&!(e.size>20971520)).slice(0,6-T.length);return 0!==t.length&&(O(e=>{e.push(...t.map(e=>({file:e})))}),t.forEach(async e=>{try{const t=new FormData;t.set("file",e);const r=await V({method:"POST",url:"/upload",data:t});O(t=>{const n=t.find(t=>t.file===e);n&&(n.path=r.path)})}catch(t){const r=c(t)?t.message:"unknown error";O(t=>{const n=t.find(t=>t.file===e);n&&(n.error=r)})}}),!0)},[T.length,_,V,O]),te=S(({target:e})=>{e.files&&ee(Array.from(e.files)),e.value=""},[ee]),re=S(e=>{const t=Array.from(e.clipboardData?.files||[]),r=Array.from(e.clipboardData?.items||[]).filter(e=>"file"===e.kind).map(e=>e.getAsFile()).filter(e=>!!e),n=t.length>0?t:r;0!==n.length&&ee(n)&&e.preventDefault()},[ee]),ne=T.flatMap(({file:e,path:t})=>t?[{name:e.name,size:e.size,path:t}]:[]),ie=S(()=>{if(F||ne.length>0){const e={query:F,files:ne};s&&(e.variables={...G,...s.values}),L&&(e.quote=L),J(e),$.current?.setContent(""),D(""),O([]),C.current?.stop(!0)}},[F,ne,s,G,L]),oe=A(()=>({setQuery(e){$.current?.setContent(e)},setQuote:D,send:J,resume:K,focus(){$.current?.focus()}}),[D,J,K]);if(M(()=>{Y?.setInput?.(oe)},[oe]),z(N,()=>oe,[oe]),s){const t=s.config.filter(e=>!(e.key in(s.values||{}))),r=(e,r=!1)=>!!e&&t.every(t=>{const n=!t.required||e?.[t.key];return!n&&r&&u.error("请完善必填信息"),n});if(t.length>0&&!r(G))return e(et,{children:e(Ze,{children:e(Re,{values:G,variables:t,onSubmit:({formData:e})=>{if(r(e,!0))return Z(e),!0}})})})}return t("div",{className:r,children:[h&&e(Ke,{actions:p,children:h}),t(st,{$focused:B&&!X,children:[T.length>0&&e(rt,{children:T.map(({file:t,error:r,path:n},i)=>{const o=void 0===r&&void 0===n;return e(tt,{name:t.name,size:t.size,error:r,loading:o,onRemove:()=>{O(e=>{const r=e.findIndex(e=>e.file===t);-1!==r&&e.splice(r,1)})}},i)})}),!!L&&t(ot,{children:[e("i",{className:"bi bi-quote"}),e("p",{children:L}),e(H,{onClick:()=>D("")})]}),t(nt,{$minRows:d,children:[y?.({Component:Ve,props:{ref:$,readonly:X||g,autoFocus:f,placeholder:x,minRows:d,maxRows:m,onSubmit:ie,onFocusedChange:U,onQueryChange:I,onPaste:re}})||e(Ve,{ref:$,readonly:X||g,autoFocus:f,placeholder:x,minRows:d,maxRows:m,onSubmit:ie,onFocusedChange:U,onQueryChange:I,onPaste:re}),t(it,{children:[b,n&&e(a,{tooltip:"可上传不超过6个小于20MB的文件",placement:"top",children:t(Se,{disabled:T.length>=6,onClick:()=>j.current?.click(),children:[e("input",{onChange:te,multiple:!0,accept:_.join(","),ref:j,type:"file",hidden:!0}),e("i",{className:"bi bi-file-earmark-arrow-up"})]})}),i&&e(Fe,{ref:C,model:i.model,onResult:e=>{$.current?.setContent(t=>t+e+" ")}}),e(Se,{$fill:X&&w,className:X&&w?"text-danger":"text-primary",disabled:!F&&0===ne.length&&!X||X&&!w,onClick:e=>{e.preventDefault(),X&&w?W():ie()},children:e("i",X&&w?{className:"bi bi-square-fill fs-7"}:{className:"bi bi-send-fill"})})]})]})]})]})});const Ze=n.div`
|
|
187
189
|
border-radius: var(--bs-border-radius-lg);
|
|
188
190
|
box-shadow: var(--bs-box-shadow-sm);
|
|
189
191
|
margin: 1rem;
|
|
190
192
|
padding: 1rem;
|
|
191
193
|
width: 100%;
|
|
192
194
|
max-width: 500px;
|
|
193
|
-
`,
|
|
195
|
+
`,et=n.div`
|
|
194
196
|
position: absolute;
|
|
195
197
|
left: 0;
|
|
196
198
|
right: 0;
|
|
@@ -200,25 +202,26 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
200
202
|
display: flex;
|
|
201
203
|
justify-content: center;
|
|
202
204
|
align-items: center;
|
|
203
|
-
`,
|
|
205
|
+
`,tt=n(ge)`
|
|
204
206
|
width: calc((100% - 0.75rem) / 2);
|
|
205
|
-
`,
|
|
207
|
+
`,rt=n.div`
|
|
206
208
|
display: flex;
|
|
207
209
|
flex-wrap: wrap;
|
|
208
210
|
padding: 0.75rem;
|
|
209
211
|
gap: 0.75rem;
|
|
210
212
|
border-bottom: var(--bs-border-width) var(--bs-border-style) var(--bs-border-color);
|
|
211
|
-
`,
|
|
213
|
+
`,nt=n.div`
|
|
212
214
|
display: flex;
|
|
213
215
|
gap: 0.25rem;
|
|
214
|
-
align-items: end;
|
|
215
|
-
flex-direction: row;
|
|
216
|
-
`,
|
|
216
|
+
align-items: ${e=>e.$minRows&&e.$minRows>1?"stretch":"end"};
|
|
217
|
+
flex-direction: ${e=>e.$minRows&&e.$minRows>1?"column":"row"};
|
|
218
|
+
`,it=n.div`
|
|
217
219
|
display: flex;
|
|
218
220
|
flex-direction: row;
|
|
221
|
+
justify-content: end;
|
|
219
222
|
gap: 0.25rem;
|
|
220
223
|
padding: 0.375rem 0.5rem 0.375rem 0.5rem;
|
|
221
|
-
`,
|
|
224
|
+
`,ot=n.div`
|
|
222
225
|
margin: 0.75rem 0.75rem 0 0.75rem;
|
|
223
226
|
padding: 4px 8px;
|
|
224
227
|
display: flex;
|
|
@@ -239,14 +242,12 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
239
242
|
-webkit-box-orient: vertical;
|
|
240
243
|
display: -webkit-box;
|
|
241
244
|
}
|
|
242
|
-
`,
|
|
245
|
+
`,st=n.div`
|
|
243
246
|
background: #fff;
|
|
244
247
|
border: var(--bs-border-width) var(--bs-border-style) ${e=>e.$focused?"var(--bs-primary)":"var(--bs-border-color)"};
|
|
245
248
|
border-radius: var(--bs-border-radius-lg);
|
|
246
249
|
box-shadow: 0.125rem 0.25rem 0.5rem rgba(0, 0, 0, 0.1);
|
|
247
|
-
|
|
248
|
-
margin: 0 1rem 1rem;
|
|
249
|
-
`;function nt(e){return e.map(e=>({id:e.id,query:e.query,quote:e.quote||void 0,files:e.files||void 0,variables:e.variables,chunks:e.chunks.map(({content:e="",node:t,error:r,tools:n=[],reasoning:i})=>({content:e,reasoning:i,error:r,tools:n,node:t})),annotation:e.annotation,stats:{usage:e.usage,latency:e.latency},status:e.status??1}))}function it(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}var ot,st,at;function lt(){return lt=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},lt.apply(null,arguments)}const ct=e=>j.createElement("svg",lt({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},e),ot||(ot=j.createElement("circle",{cx:15,cy:15,r:15,fill:"#3c60ff"},j.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),j.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))),st||(st=j.createElement("circle",{cx:60,cy:15,r:9,fill:"#3c60ff",fillOpacity:.3},j.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:9,repeatCount:"indefinite",to:9,values:"9;15;9"}),j.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:.5,repeatCount:"indefinite",to:.5,values:".5;1;.5"}))),at||(at=j.createElement("circle",{cx:105,cy:15,r:15,fill:"#3c60ff"},j.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),j.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))));function dt(e){if(!e)return"";if("string"==typeof e)return e;if("image"===e.type){return`})`}return""}const ut=function(e,t,r){function n(t){return t!==ne.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(ie.chunkString,{contentType:"string"}),i(t)):(e.consume(t),n)}function i(n){return n===ne.rightSquareBracket?(e.exit(ie.chunkString),e.exit("citeData"),e.enter("citeMarker"),function(n){if(n!==ne.rightSquareBracket)return r(n);return e.consume(n),e.exit("citeMarker"),e.exit("cite"),t(n)}(n)):oe(n)||n===ne.eof||n>57||n<48?r(n):(e.consume(n),i)}return function(t){return t!==ne.leftSquareBracket?r(t):(e.enter("cite"),e.enter("citeMarker"),n(t))}},mt=function(){const e=this.data();e.micromarkExtensions.push({text:{[ne.leftSquareBracket]:{tokenize:ut}}}),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}}})},ht=x.Image,pt={pre:({children:r,node:n})=>{if(!n)return null;const[i,o]=F(!1),s=z(()=>{const e=n.children[0];return"element"===(t=e).type&&"code"===t.tagName&&!!e.properties?.className?.includes("language-svg");var t},[n]);R(()=>{s&&o(s)},[s]);const a=z(()=>V(n),[n]);return e(xt,{children:[e(yt,{children:[!i&&t(w,{tooltip:!1,as:vt,content:a}),s&&i&&t(vt,{onClick:()=>{const e=new Blob([a],{type:"image/svg+xml"}),t=URL.createObjectURL(e),r=document.createElement("a");r.href=t,r.download="image.svg",r.click(),URL.revokeObjectURL(t)},children:"下载"}),s&&t(vt,{onClick:()=>o(!i),children:i?"源码":"预览"})]}),s&&i?t("div",{dangerouslySetInnerHTML:{__html:a}}):t("pre",{children:r})]})},a:({node:e,href:r,...n})=>{const i=r?.startsWith("http")?"_blank":"_self";return t("a",{...n,href:r,target:i})},img:({node:e,...r})=>t(ht,{...r})},gt=[[re,{singleDollarTextMath:!1}],te,ee],ft=[Z,[G,{detect:!1,ignoreMissing:!0}]],bt=D(q(({content:e,components:r,cite:n},i)=>{e=e.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$");const o=z(()=>{const e={...pt,...r};return n&&(e.cite=n),e},[r,n]),s=z(()=>n?[...gt,mt]:gt,[n]);return t(wt,{ref:i,children:t(Y,{remarkPlugins:s,rehypePlugins:ft,components:o,children:e})})}),X),vt=n.div`
|
|
250
|
+
`;function at(e){return e.map(e=>({id:e.id,query:e.query,quote:e.quote||void 0,files:e.files||void 0,variables:e.variables,chunks:e.chunks.map(({content:e="",node:t,error:r,tools:n=[],reasoning:i})=>({content:e,reasoning:i,error:r,tools:n,node:t})),annotation:e.annotation,stats:{usage:e.usage,latency:e.latency},status:e.status??1}))}function lt(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}var ct,dt,ut;function mt(){return mt=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},mt.apply(null,arguments)}const ht=e=>j.createElement("svg",mt({xmlns:"http://www.w3.org/2000/svg",width:20,height:10,fill:"#fff",viewBox:"0 0 120 30"},e),ct||(ct=j.createElement("circle",{cx:15,cy:15,r:15,fill:"#3c60ff"},j.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),j.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))),dt||(dt=j.createElement("circle",{cx:60,cy:15,r:9,fill:"#3c60ff",fillOpacity:.3},j.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:9,repeatCount:"indefinite",to:9,values:"9;15;9"}),j.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:.5,repeatCount:"indefinite",to:.5,values:".5;1;.5"}))),ut||(ut=j.createElement("circle",{cx:105,cy:15,r:15,fill:"#3c60ff"},j.createElement("animate",{attributeName:"r",begin:"0s",calcMode:"linear",dur:"0.8s",from:15,repeatCount:"indefinite",to:15,values:"15;9;15"}),j.createElement("animate",{attributeName:"fill-opacity",begin:"0s",calcMode:"linear",dur:"0.8s",from:1,repeatCount:"indefinite",to:1,values:"1;.5;1"}))));function pt(e){if(!e)return"";if("string"==typeof e)return e;if("image"===e.type){return`})`}return""}const gt=function(e,t,r){function n(t){return t!==ne.leftSquareBracket?(e.exit("citeMarker"),e.enter("citeData"),e.enter(ie.chunkString,{contentType:"string"}),i(t)):(e.consume(t),n)}function i(n){return n===ne.rightSquareBracket?(e.exit(ie.chunkString),e.exit("citeData"),e.enter("citeMarker"),function(n){if(n!==ne.rightSquareBracket)return r(n);return e.consume(n),e.exit("citeMarker"),e.exit("cite"),t(n)}(n)):oe(n)||n===ne.eof||n>57||n<48?r(n):(e.consume(n),i)}return function(t){return t!==ne.leftSquareBracket?r(t):(e.enter("cite"),e.enter("citeMarker"),n(t))}},ft=function(){const e=this.data();e.micromarkExtensions.push({text:{[ne.leftSquareBracket]:{tokenize:gt}}}),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}}})},bt=x.Image,vt={pre:({children:r,node:n})=>{if(!n)return null;const[i,o]=q(!1),s=A(()=>{const e=n.children[0];return"element"===(t=e).type&&"code"===t.tagName&&!!e.properties?.className?.includes("language-svg");var t},[n]);M(()=>{s&&o(s)},[s]);const a=A(()=>V(n),[n]);return t(Nt,{children:[t(Et,{children:[!i&&e(w,{tooltip:!1,as:kt,content:a}),s&&i&&e(kt,{onClick:()=>{const e=new Blob([a],{type:"image/svg+xml"}),t=URL.createObjectURL(e),r=document.createElement("a");r.href=t,r.download="image.svg",r.click(),URL.revokeObjectURL(t)},children:"下载"}),s&&e(kt,{onClick:()=>o(!i),children:i?"源码":"预览"})]}),s&&i?e("div",{dangerouslySetInnerHTML:{__html:a}}):e("pre",{children:r})]})},a:({node:t,href:r,...n})=>{const i=r?.startsWith("http")?"_blank":"_self";return e("a",{...n,href:r,target:i})},img:({node:t,...r})=>e(bt,{...r})},yt=[[re,{singleDollarTextMath:!1}],te,ee],xt=[Z,[G,{detect:!1,ignoreMissing:!0}]],wt=L(F(({content:t,components:r,cite:n},i)=>{t=t.replace(/\\\(\s(.+?)\s\\\)/g,"$$$$ $1 $$$$");const o=A(()=>{const e={...vt,...r};return n&&(e.cite=n),e},[r,n]),s=A(()=>n?[...yt,ft]:yt,[n]);return e(jt,{ref:i,children:e(Y,{remarkPlugins:s,rehypePlugins:xt,components:o,children:t})})}),X),kt=n.div`
|
|
250
251
|
cursor: pointer;
|
|
251
252
|
padding: 2px 10px;
|
|
252
253
|
background-color: #303030;
|
|
@@ -256,7 +257,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
256
257
|
font-size: 12px;
|
|
257
258
|
font-family: initial;
|
|
258
259
|
user-select: none;
|
|
259
|
-
`,
|
|
260
|
+
`,Et=n.div`
|
|
260
261
|
position: absolute;
|
|
261
262
|
right: 10px;
|
|
262
263
|
top: 1em;
|
|
@@ -270,17 +271,17 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
270
271
|
&:hover {
|
|
271
272
|
opacity: 1;
|
|
272
273
|
}
|
|
273
|
-
`,
|
|
274
|
+
`,Nt=n.div`
|
|
274
275
|
position: relative;
|
|
275
276
|
|
|
276
277
|
&:hover {
|
|
277
|
-
${
|
|
278
|
+
${Et} {
|
|
278
279
|
pointer-events: all;
|
|
279
280
|
transform: translateX(0px);
|
|
280
281
|
opacity: 0.7;
|
|
281
282
|
}
|
|
282
283
|
}
|
|
283
|
-
`,
|
|
284
|
+
`,jt=n.div`
|
|
284
285
|
-ms-text-size-adjust: 100%;
|
|
285
286
|
-webkit-text-size-adjust: 100%;
|
|
286
287
|
line-height: 1.8;
|
|
@@ -550,7 +551,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
550
551
|
.hljs-strong {
|
|
551
552
|
font-weight: 700;
|
|
552
553
|
}
|
|
553
|
-
`;var
|
|
554
|
+
`;var Ct=F(function({children:t,tooltip:r,onClick:n,disabled:i,...o},s){i&&(n=void 0);const l=e($t,{ref:s,onClick:n,$disabled:i,...o,children:t});return r?e(a,{tooltip:r,placement:"top",children:l}):l});const $t=n.div`
|
|
554
555
|
align-items: center;
|
|
555
556
|
justify-content: center;
|
|
556
557
|
border-radius: .375rem;
|
|
@@ -565,7 +566,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
565
566
|
height: 1.875rem;
|
|
566
567
|
overflow: hidden;
|
|
567
568
|
|
|
568
|
-
${e=>e.$disabled&&
|
|
569
|
+
${e=>e.$disabled&&o`
|
|
569
570
|
color: var(--bs-gray-400) !important;
|
|
570
571
|
`}
|
|
571
572
|
.bi {
|
|
@@ -575,7 +576,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
575
576
|
line-height: 1.75rem;
|
|
576
577
|
text-align: center;
|
|
577
578
|
|
|
578
|
-
${e=>!e.$disabled&&
|
|
579
|
+
${e=>!e.$disabled&&o`
|
|
579
580
|
&:hover {
|
|
580
581
|
color: var(--bs-dark);
|
|
581
582
|
background-color: var(--bs-gray-200);
|
|
@@ -583,7 +584,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
583
584
|
`}
|
|
584
585
|
|
|
585
586
|
}
|
|
586
|
-
`,
|
|
587
|
+
`,Rt=C(null);var St=Rt.Provider;const Mt=x.Image;function qt({src:t,...r}){const{imageResolver:n}=je();return n&&t&&(t=n(t)),e(Ft,{children:e(Mt,{src:t,...r})},t)}const Ft=n.div`
|
|
587
588
|
margin-bottom: .5rem;
|
|
588
589
|
margin-top: .5rem;
|
|
589
590
|
border: var(--bs-border-width) var(--bs-border-style) var(--bs-border-color);
|
|
@@ -608,7 +609,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
608
609
|
max-height: 100%;
|
|
609
610
|
max-width: 100%;
|
|
610
611
|
}
|
|
611
|
-
`,
|
|
612
|
+
`,At=C(null);function zt(){const e=$(At);if(!e)throw new Error("useContext must be used within a Provider");return e}function It({children:t,...r}){return e(At.Provider,{value:r,children:t})}function Lt(e){const[t,r]=q(e);return M(()=>{r(e)},[e]),[t,r]}function Dt({description:r,options:n}){const{resume:i,disabled:o,response:s}=zt(),[a,l]=Lt(s?.result),c=o||void 0!==a;return t(Pt,{children:[r&&e(Tt,{children:r}),n.map((t,r)=>e(Ot,{$selected:a===t,$disabled:c,onClick:()=>(e=>{c||(l(e),i(e))})(t),children:t},r))]})}const Pt=n.div`
|
|
612
613
|
display: flex;
|
|
613
614
|
flex-direction: column;
|
|
614
615
|
gap: 0.5rem;
|
|
@@ -621,10 +622,10 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
621
622
|
&:last-child {
|
|
622
623
|
margin-bottom: 0;
|
|
623
624
|
}
|
|
624
|
-
`,
|
|
625
|
+
`,Tt=n.div`
|
|
625
626
|
color: var(--bs-secondary);
|
|
626
627
|
line-height: 1.5;
|
|
627
|
-
`,
|
|
628
|
+
`,Ot=n.div`
|
|
628
629
|
padding: 0.375rem 0.75rem;
|
|
629
630
|
background-color: ${e=>e.$selected?"rgba(var(--bs-primary-rgb), 0.1)":"#FFFFFF"};
|
|
630
631
|
border: 1px solid ${e=>e.$selected?"rgba(var(--bs-primary-rgb), 0.3)":"var(--bs-border-color)"};
|
|
@@ -643,7 +644,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
643
644
|
&:active {
|
|
644
645
|
transform: ${e=>e.$disabled?"none":"scale(0.98)"};
|
|
645
646
|
}
|
|
646
|
-
`;function
|
|
647
|
+
`;function Bt({description:r,fields:n}){const{resume:i,disabled:o,response:s}=zt(),[a,l]=Lt(void 0!==s),c=o||a,{schema:u,uiSchema:m}=A(()=>{const e={},t=[],r={};for(const i of n)e[i.name]={type:"number"===i.type?"number":"string",title:i.label,default:i.default},i.placeholder&&(r[i.name]={"ui:placeholder":i.placeholder}),"textarea"===i.type?r[i.name]={...r[i.name],"ui:widget":"textarea"}:"select"!==i.type&&"radio"!==i.type||!i.options?"checkbox"===i.type&&i.options?(e[i.name].type="array",e[i.name].uniqueItems=!0,e[i.name].items={type:"string",enum:i.options},r[i.name]={...r[i.name],"ui:widget":"checkboxes","ui:enumNames":i.options,"ui:inline":!0}):"email"===i.type?e[i.name].format="email":"date"===i.type?e[i.name].format="date":"tel"===i.type&&(r[i.name]={...r[i.name],"ui:options":{inputType:"tel"}}):(r[i.name]={...r[i.name],"ui:widget":i.type},e[i.name].enum=i.options,e[i.name].enumNames=i.options),i.required&&t.push(i.name);return{schema:{type:"object",properties:e,required:t},uiSchema:r}},[n]);return t(_t,{className:"rounded bg-white shadow-sm",children:[r&&e(Ht,{children:r}),e(d,{schema:u,uiSchema:m,formData:s,onSubmit:({formData:e})=>{c||(l(!0),i(e))},disabled:c,validate:!0,showErrorList:!1,submitText:a?"已提交":"提交",omitExtraData:!0,children:({submit:t})=>e(Ut,{children:t})})]})}const Ut=n.div`
|
|
647
648
|
display: flex;
|
|
648
649
|
justify-content: flex-end;
|
|
649
650
|
border-top: 1px solid var(--bs-border-color);
|
|
@@ -652,7 +653,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
652
653
|
button {
|
|
653
654
|
padding: .25rem;
|
|
654
655
|
}
|
|
655
|
-
`,
|
|
656
|
+
`,_t=n.div`
|
|
656
657
|
display: flex;
|
|
657
658
|
flex-direction: column;
|
|
658
659
|
gap: 0.5rem;
|
|
@@ -670,10 +671,10 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
670
671
|
&:last-child {
|
|
671
672
|
margin-bottom: 0;
|
|
672
673
|
}
|
|
673
|
-
`,
|
|
674
|
+
`,Ht=n.div`
|
|
674
675
|
color: var(--bs-secondary);
|
|
675
676
|
line-height: 1.5;
|
|
676
|
-
`;function
|
|
677
|
+
`;function Qt({content:r}){const{resume:n,disabled:i,response:o}=zt(),[s,a]=Lt(void 0!==o),l=i||s;return t(Jt,{className:"rounded bg-white shadow-sm",children:[r&&e(Kt,{children:e(wt,{content:r})}),e(Wt,{children:e(k,{disabled:l,onClick:()=>{l||(a(!0),n("已确认"))},children:s?"已确认":"确认"})})]})}const Jt=n.div`
|
|
677
678
|
display: flex;
|
|
678
679
|
flex-direction: column;
|
|
679
680
|
gap: .5rem;
|
|
@@ -687,10 +688,10 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
687
688
|
&:last-child {
|
|
688
689
|
margin-bottom: 0;
|
|
689
690
|
}
|
|
690
|
-
`,
|
|
691
|
+
`,Kt=n.div`
|
|
691
692
|
line-height: 1.6;
|
|
692
693
|
color: var(--bs-body-color);
|
|
693
|
-
`,
|
|
694
|
+
`,Wt=n.div`
|
|
694
695
|
display: flex;
|
|
695
696
|
justify-content: flex-end;
|
|
696
697
|
border-top: 1px solid var(--bs-border-color);
|
|
@@ -699,27 +700,27 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
699
700
|
button {
|
|
700
701
|
padding: .25rem 1.5rem;
|
|
701
702
|
}
|
|
702
|
-
`;function
|
|
703
|
+
`;function Xt(e){return e instanceof Error&&e.message||String(e)}class Vt extends D{state={hasError:!1};static getDerivedStateFromError(){return{hasError:!0}}componentDidCatch(e,t){this.props.onRenderError(e)}render(){return this.state.hasError?null:this.props.children}}function Yt({suspend:t}){const{readonly:r,input:n}=je(),{chunkIndex:i,chunk:o,toolIndex:s,tool:a,isLastMessage:l}=function(){const e=$(Rt);if(!e)throw new Error("useContext must be used within a Provider");return e}(),c=S(e=>{n?.resume({chunk:i,tool:s,payload:e})},[i,n,s]),d=S(e=>{c({error:Xt(e)})},[c]);let u=null;switch(t.type){case"select":u=e(Dt,{...t.select});break;case"form":u=e(Bt,{...t.form});break;case"confirm":u=e(Qt,{...t.confirm})}const m=a?.response||o.node?.output;return e(It,{resume:c,disabled:r||!!o.error||!l,response:"(Empty)"===m?void 0:m,children:e(Vt,{onRenderError:d,children:u})})}function Gt({value:t,cite:n}){const i=t=>"string"==typeof t?e(wt,{content:t,cite:n}):"image"===t.type?e(qt,{src:t.image}):"suspend"===t.type?e(Yt,{suspend:t.suspend}):null;return Array.isArray(t)?e(r,{children:t.map((t,r)=>e(I,{children:i(t)},r))}):i(t)}const Zt=({reasoning:r,loading:n})=>{const[i,o]=q(!1);return M(()=>{n||o(!1)},[n]),e(I,{children:e("div",{className:"mb-2",children:t("div",i?{className:"shadow-sm rounded bg-white fs-7",children:[t("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[n?e(O,{animation:"border",variant:"primary",size:"sm"}):e("i",{className:"bi bi-check-circle-fill text-success"}),e("span",{className:"text-muted",children:n?"思考中":"已思考"}),e("i",{className:"bi bi-caret-up-fill text-muted"})]}),e("div",{className:"border-top p-2 d-flex flex-column gap-2",children:e("div",{className:"border rounded bg-light p-2",children:e(wt,{content:r})})})]}:{onClick:()=>o(!0),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[n?e(O,{animation:"border",variant:"primary",size:"sm"}):e("i",{className:"bi bi-check-circle-fill text-success"}),e("span",{className:"text-muted",children:n?"思考中":"已思考"}),e("i",{className:"bi bi-caret-down-fill text-muted"})]})})})},er=L(t=>{const{renderReasoning:r}=je();return r?r({Component:Zt,props:t}):e(Zt,{...t})});function tr({tool:r,loading:n}){const{logLevel:i="none",renderToolContent:o}=je(),s="response"in r,a=!s&&!n,{response:l,title:c,content:d,showStatus:u,defaultExpanded:m}=A(()=>{const i=(e=>{try{return JSON.parse(e.arguments)}catch(e){return null}})(r),a=e("span",{className:"text-truncate",children:r.title}),l=t("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"参数"})}),e(nr,{className:"border-top p-2",children:r.arguments})]}),s&&t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"响应"})}),e(nr,{className:"border-top p-2",children:r.response||"None"})]})]}),c=r.content&&e(Gt,{value:r.content});if(o){const e=o({tool:r,args:i,loading:n});if(e)return{title:null===e.title?null:e.title??a,response:null===e.response?null:e.response??l,content:null===e.content?null:e.content??c,showStatus:e.showStatus??!0,defaultExpanded:e.defaultExpanded??!1}}return{title:a,response:l,content:c,showStatus:!0,defaultExpanded:!1}},[r,o]),[h,p]=q(m),g="all"==i&&null!==l;return t(I,{children:[c&&e("div",{className:"mb-2",children:t(rr,{className:"shadow-sm rounded bg-white fs-7",$open:h,children:[t("div",{onClick:()=>p(g&&!h),role:"button",className:"d-flex align-items-center flex-nowrap p-1 px-2 gap-2",children:[a?e("i",{className:"bi bi-dash-circle-fill text-muted"}):s?r.error?e("i",{className:"bi bi-x-circle-fill text-danger"}):e("i",{className:"bi bi-check-circle-fill text-success"}):e(O,{animation:"border",variant:"primary",size:"sm"}),u&&e("span",{className:"text-muted",children:a?"已取消":s?"已使用":"正在使用"}),c,g&&e("i",h?{className:"bi bi-caret-up-fill text-muted"}:{className:"bi bi-caret-down-fill text-muted"})]}),h&&l]})}),d]})}const rr=n.div`
|
|
703
704
|
width: ${e=>e.$open?"auto":"fit-content"};
|
|
704
705
|
max-width: 100%;
|
|
705
706
|
overflow:hidden;
|
|
706
|
-
`,
|
|
707
|
+
`,nr=n.div`
|
|
707
708
|
white-space: pre-wrap;
|
|
708
709
|
overflow-y: auto;
|
|
709
710
|
overflow-x: hidden;
|
|
710
711
|
max-height: 150px;
|
|
711
|
-
`;function
|
|
712
|
+
`;function ir({node:r}){const[n,i]=q(!1),{nodeIconResolver:o}=je(),s=!("latency"in r),a=s?e(O,{animation:"border",variant:"primary",size:"sm"}):r.error?e("i",{className:"bi bi-x-circle-fill text-danger"}):e("i",{className:"bi bi-check-circle-fill text-success"});return t("div",{className:"shadow-sm rounded bg-white fs-7",children:[t(sr,{onClick:()=>i(e=>!e),role:"button",className:"d-flex align-items-center p-1 px-2 gap-1",children:[e("i",{className:"bi bi-chevron-down text-muted"}),o?.(r.type),e(ar,{className:"me-2 text-truncate",children:r.title}),e("span",{className:"me-auto"}),!s&&e("span",{className:"text-nowrap text-muted",children:or(r.latency)}),e("span",{children:a})]}),n&&t("div",{className:"border-top p-2 d-flex flex-column gap-2",children:[t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"输入"})}),e("div",{className:"border-top p-2 overflow-hidden text-pre-wrap",children:r.input?JSON.stringify(r.input,null,4):"None"})]}),t("div",{className:"border rounded bg-light",children:[e("div",{className:"d-flex align-items-center p-1 px-2 fs-7 gap-2",children:e("span",{className:"text-muted",children:"输出"})}),e("div",{className:"border-top p-2 overflow-hidden text-pre-wrap",children:r.output?JSON.stringify(r.output,null,4):"None"})]})]})]})}const or=e=>e<1?`${(1e3*e).toFixed(3)} ms`:e>60?`${parseInt(Math.round(e/60).toString())} m ${(e%60).toFixed(3)} s`:`${e.toFixed(3)} s`,sr=n.div`
|
|
712
713
|
svg {
|
|
713
714
|
flex-shrink: 0;
|
|
714
715
|
}
|
|
715
|
-
`,
|
|
716
|
+
`,ar=n.span`
|
|
716
717
|
display: inline-block;
|
|
717
|
-
`;function
|
|
718
|
-
${e=>e.$deleted&&
|
|
718
|
+
`;function lr({nodes:r,loading:n}){const[i,o]=q(!1),s=r.some(e=>"error"in e),a=n?e(O,{animation:"border",variant:"primary",size:"sm"}):e("i",s?{className:"bi bi-x-circle-fill text-danger"}:{className:"bi bi-check-circle-fill text-success"});return e("div",{className:"mb-2",children:t("div",i?{className:"shadow-sm rounded bg-white fs-7",children:[t("div",{onClick:()=>o(!1),role:"button",className:"d-flex align-items-center p-1 px-2 gap-2",children:[a,e("span",{children:"工作流"}),e("i",{className:"bi bi-caret-up-fill text-muted"})]}),e("div",{className:"border-top p-2 d-flex flex-column gap-1 bg-light bg-opacity-50",children:r.map((t,r)=>e(ir,{node:t},r))})]}:{onClick:()=>o(!0),role:"button",className:"d-inline-flex align-items-center shadow-sm rounded bg-white p-1 px-2 fs-7 gap-2",children:[a,e("span",{children:"工作流"}),e("i",{className:"bi bi-caret-down-fill text-muted"})]})})}const cr=L(F(({chunks:r,loading:n,cite:i,isLast:o=!1,deleted:s=!1},a)=>{const{logLevel:l="none"}=je(),c=A(()=>r.flatMap(e=>e.node?[e.node]:[]),[r]);return t(dr,{ref:a,$deleted:s,children:["all"===l&&c.length>0&&e(lr,{loading:n,nodes:c}),r.map((r,s)=>t(I,{children:[r.reasoning&&["stats","all"].includes(l)&&e(er,{reasoning:r.reasoning,loading:n&&!r.content&&0===(r.tools||[]).length}),r.content&&e(St,{value:{chunkIndex:s,chunk:r,isLastMessage:o},children:e(Gt,{cite:i,value:r.content})}),(r.tools||[]).map((t,i)=>e(St,{value:{chunkIndex:s,chunk:r,toolIndex:i,tool:t,isLastMessage:o},children:e(tr,{tool:t,loading:n})},i)),r.error&&e(wt,{content:`[${r.error}]`})]},s))]})})),dr=n.div`
|
|
719
|
+
${e=>e.$deleted&&o`
|
|
719
720
|
text-decoration-line: line-through;
|
|
720
721
|
color: var(--bs-secondary);
|
|
721
722
|
`}
|
|
722
|
-
`;class
|
|
723
|
+
`;class ur{#t;#o=[];#s=0;constructor(){this.#t=new K}speak(){for(this.#t.emit("playing",!0);this.#s<this.#o.length;){const e=this.#o[this.#s],t=new SpeechSynthesisUtterance(e),r=++this.#s;t.onend=()=>{r>=this.#o.length&&this.clear()},t.onerror=()=>{this.clear()},window.speechSynthesis.speak(t)}}clear(){this.#t.emit("playing",!1)}start(e,t=!1){this.#o=Array.isArray(e)?e:[e],t||(window.speechSynthesis.cancel(),this.#s=0),this.speak()}stop(){window.speechSynthesis.cancel(),this.clear()}onPlaying(e){this.#t.on("playing",e)}onSpeaking(e){this.#t.on("speaking",e)}}const mr=new Map;class hr{#r;#t;#o=[];#a=new Map;#l=!1;#s=0;#c=!1;constructor(e){this.#r=e,this.#t=new K}createLoader(e){const t=new Promise(async t=>{try{const{type:r,data:n}=await this.#r(e),i=new Audio(`data:${r};base64,${n}`);this.#a.set(e,i),t(i)}catch{this.#a.delete(e),t(void 0)}});return this.#a.set(e,t),t}async loadAudioData(){this.#c=!0,this.#t.emit("speaking",!0);let e=0;for(;e<this.#o.length;){const t=this.#o[e];!this.#a.has(t)&&this.#c&&await this.createLoader(t),e++}this.#t.emit("speaking",!1),this.#c=!1}waitForAudioLoaded(e){const t=this.#a.get(e);return t instanceof Promise?t:Promise.resolve(t)}async playAudio(e=0){this.#l=!0;const t=await this.waitForAudioLoaded(this.#o[e]);this.#t.emit("playing",!0),t?(mr.set(this,t),t.onpause=async()=>{t.ended?(this.#s++,this.#s<this.#o.length?await this.playAudio(this.#s):this.clear()):this.clear()},await t.play()):this.clear()}clear(){this.#l=!1,this.#c=!1,this.#t.emit("playing",!1),this.#t.emit("speaking",!1),mr.delete(this)}start(e,t=!1){mr.forEach((e,t)=>{t!==this&&(e.pause(),e.currentTime=0)}),this.#o=Array.isArray(e)?e:[e],t||(this.#s=0),this.#c||this.loadAudioData(),this.#l||this.playAudio()}update(e){this.#o=Array.isArray(e)?e:[e]}stop(){const e=mr.get(this);e&&(e.pause(),e.currentTime=0)}onPlaying(e){this.#t.on("playing",e)}onSpeaking(e){this.#t.on("speaking",e)}}function pr({text:e,loading:t,loader:r}){const n=R(),[i,o]=q(!1),[s,a]=q(!1),l=R(t),c=async(i=!1)=>{n.current||(n.current=r?new hr(r):new ur,n.current.onSpeaking(o),n.current.onPlaying(a));let s="";"string"==typeof e?s=e:e.current&&(s=function(e){const t=e.querySelectorAll("p, h1, h2, h3, h4, h5, h6, li:not(:has(p,h1, h2, h3, h4, h5, h6))");return Array.from(t).flatMap(e=>e.textContent?[e.textContent]:[])}(e.current),t&&s.pop()),s&&0!==s.length&&n.current.start(s,i)};return M(()=>{if(l.current&&"string"!=typeof e&&e.current){if(t){const t={childList:!0,subtree:!0},r=new MutationObserver(function(){c(!0)});return r.observe(e.current,t),c(),()=>{r.disconnect()}}c(!0)}},[t]),M(()=>()=>{n.current&&(n.current.stop(),n.current=void 0)},[]),{playing:s,speaking:i,start:c,stop:()=>{n.current&&n.current.stop()}}}function gr({model:r="builtin",voice:n,loading:i,autoplay:o,contentRef:s,avatarRef:a}){const{request:l}=je(),c=A(()=>{if("builtin"!==r)return async e=>(await l({url:"/speech",method:"post",data:{model:r,voice:n,input:e}})).audio},[r,n]),{playing:d,speaking:u,start:m,stop:h}=pr({loader:c,text:s,loading:o&&i});let p=null;return a.current&&(u&&(p=se(e(fr,{children:e(O,{size:"sm",variant:"light"})}),a.current)),d&&(p=se(e(fr,{children:e(br,{className:"bi bi-volume-down-fill text-light"})}),a.current))),t(Ct,{disabled:i&&!d,onClick:()=>{d?h():m()},children:[d?e(br,{className:"bi bi-volume-down-fill"}):u?e(O,{size:"sm",variant:"primary"}):e(br,{className:"bi bi-volume-down"}),p]})}const fr=n.div`
|
|
723
724
|
position: absolute;
|
|
724
725
|
top: 0;
|
|
725
726
|
left: 0;
|
|
@@ -729,12 +730,12 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
729
730
|
align-items: center;
|
|
730
731
|
justify-content: center;
|
|
731
732
|
background-color: rgba(0, 0, 0, .3);
|
|
732
|
-
`,
|
|
733
|
+
`,br=n.i`
|
|
733
734
|
|
|
734
735
|
&::before {
|
|
735
736
|
transform: scale(1.4)
|
|
736
737
|
}
|
|
737
|
-
`,
|
|
738
|
+
`,vr=L(({message:n,actions:i,setMessage:o,cite:s,speech:a,isLast:l})=>{const{query:c,quote:d,files:u=[],chunks:m,annotation:h,loading:p}=n,{user:g,bot:f,logLevel:b="none",messages:v,renderFileItem:y}=je(),x=A(()=>m?m.reduce(function(e,t){const r=Array.isArray(t.content)?t.content.reduce((e,t)=>{const r=pt(t);return r?e+r+"\n":e},""):pt(t.content);return e+r+"\n"+(t.tools||[]).reduce((e,t)=>{if(t.content){const r=pt(t.content);if(r)return e+r+"\n"}return e},"")},""):"",[m]),k=R(null),N=R(null),j=R(null),C=c||u.length>0;return t(r,{children:[C&&t(Rr,{$reverse:!0,children:[g&&e(jr,{children:e("img",{src:g.avatar})}),e(Cr,{}),t($r,{ref:k,children:[c&&e(wt,{content:c}),d&&e(Q,{trigger:"click",container:k,rootClose:!0,placement:"bottom",overlay:e(yr,{body:!0,children:d}),children:e(wr,{children:e(xr,{children:d})})}),u.length>0&&u.map(({name:t,size:r,path:n},i)=>{if(E(n))return e(qt,{src:`/uploads/${n}`},i);const o={name:t,url:`/uploads/${n}`,size:r};return e(kr,{children:y?y({Component:ge,props:o}):e(ge,{...o})},i)})]})]}),m&&t(Rr,{children:[f&&e(jr,{ref:N,children:e("img",{src:f.avatar})}),e(Cr,{}),t($r,{children:[C&&!1!==i&&t(Er,{children:[e(w,{placement:"top",content:x,as:Ct,tooltip:!0}),a&&e(gr,{loading:n.loading,avatarRef:N,contentRef:j,...a}),i?.({Component:Ct,message:n,content:x,setMessage:o,messages:v})]}),e(Nr,{children:["stats","all"].includes(b)&&n.stats&&t(r,{children:[t("span",{children:["耗时 ",n.stats.latency/1e3," 秒"]}),t("span",{children:["花费 Token ",n.stats.usage]})]})}),e(cr,{ref:j,chunks:m,loading:p,isLast:l,cite:s,deleted:!!h}),h&&t("div",{children:[t("div",{className:"d-flex align-items-center",children:[e("span",{className:"fs-7 text-secondary me-2",children:"标注的答案"}),e("hr",{className:"flex-fill"})]}),e(wt,{content:h.answer})]}),n.loading&&e(ht,{})]})]})]})}),yr=n(J)`
|
|
738
739
|
max-width: calc(100% - .875rem * 2);
|
|
739
740
|
width: calc(100% - .875rem * 2);
|
|
740
741
|
font-size: 1rem;
|
|
@@ -743,20 +744,20 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
743
744
|
.popover-body {
|
|
744
745
|
padding: .5rem .75rem;
|
|
745
746
|
}
|
|
746
|
-
`,
|
|
747
|
+
`,xr=n.div`
|
|
747
748
|
overflow: hidden;
|
|
748
749
|
text-overflow: ellipsis;
|
|
749
750
|
word-break: break-all;
|
|
750
751
|
-webkit-line-clamp: 2;
|
|
751
752
|
-webkit-box-orient: vertical;
|
|
752
753
|
display: -webkit-box;
|
|
753
|
-
`,
|
|
754
|
+
`,wr=n.div`
|
|
754
755
|
padding: .5rem .75rem;
|
|
755
756
|
color: var(--bs-secondary);
|
|
756
757
|
border-radius: var(--bs-border-radius-lg);
|
|
757
758
|
background: #FFFFFF;
|
|
758
759
|
cursor: pointer;
|
|
759
|
-
`,
|
|
760
|
+
`,kr=n.div`
|
|
760
761
|
width: 100%;
|
|
761
762
|
margin-bottom: .5rem;
|
|
762
763
|
margin-top: .5rem;
|
|
@@ -768,7 +769,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
768
769
|
&:last-child {
|
|
769
770
|
margin-bottom: 0;
|
|
770
771
|
}
|
|
771
|
-
`,
|
|
772
|
+
`,Er=n.div`
|
|
772
773
|
position: absolute;
|
|
773
774
|
top: -1.1rem;
|
|
774
775
|
right: 0;
|
|
@@ -776,7 +777,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
776
777
|
gap: .5rem;
|
|
777
778
|
opacity: 0;
|
|
778
779
|
z-index: 1;
|
|
779
|
-
`,
|
|
780
|
+
`,Nr=n.div`
|
|
780
781
|
position: absolute;
|
|
781
782
|
bottom: -1.4rem;
|
|
782
783
|
left: 0;
|
|
@@ -789,7 +790,7 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
789
790
|
display: flex;
|
|
790
791
|
opacity: 0;
|
|
791
792
|
z-index: 1;
|
|
792
|
-
`,
|
|
793
|
+
`,jr=n.div`
|
|
793
794
|
width: 35px;
|
|
794
795
|
height: 35px;
|
|
795
796
|
border-radius: 50%;
|
|
@@ -800,14 +801,14 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
800
801
|
width: 100%;
|
|
801
802
|
height: 100%;
|
|
802
803
|
}
|
|
803
|
-
`,
|
|
804
|
+
`,Cr=n.div`
|
|
804
805
|
width: 0;
|
|
805
806
|
height: 0;
|
|
806
807
|
content: "";
|
|
807
808
|
border: 5px solid transparent;
|
|
808
809
|
border-right-color: #f3f3f3;
|
|
809
810
|
transform: translateY(10px);
|
|
810
|
-
|
|
811
|
+
`,$r=n.div`
|
|
811
812
|
padding: .875rem;
|
|
812
813
|
background-color: rgb(243, 243, 243);
|
|
813
814
|
border-radius: var(--bs-border-radius-lg);
|
|
@@ -815,31 +816,31 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
815
816
|
position: relative;
|
|
816
817
|
|
|
817
818
|
&:hover {
|
|
818
|
-
${
|
|
819
|
+
${Nr}, ${Er} {
|
|
819
820
|
opacity: 1;
|
|
820
821
|
}
|
|
821
822
|
}
|
|
822
|
-
`,
|
|
823
|
+
`,Rr=n.div`
|
|
823
824
|
display: flex;
|
|
824
825
|
justify-content: flex-start;
|
|
825
826
|
padding: 1rem 0 1rem 0;
|
|
826
|
-
${e=>e.$reverse&&
|
|
827
|
+
${e=>e.$reverse&&o`
|
|
827
828
|
flex-direction: row-reverse;
|
|
828
829
|
|
|
829
|
-
${
|
|
830
|
+
${Cr} {
|
|
830
831
|
border-left-color: rgba(var(--bs-primary-rgb), 0.1);
|
|
831
832
|
border-right-color: transparent;
|
|
832
833
|
}
|
|
833
834
|
|
|
834
|
-
${
|
|
835
|
+
${$r} {
|
|
835
836
|
background: rgba(var(--bs-primary-rgb), 0.1);
|
|
836
837
|
}
|
|
837
838
|
`};
|
|
838
|
-
`;function
|
|
839
|
+
`;function Sr(r){const{placeholder:n,actions:i,cite:o,speech:s,renderItem:a,scrollRef:l}=r,{messages:c,setMessages:d,loading:u,suggestions:m,conversationId:h}=je(),p=R(r.loaded),g=S(()=>{const e=l.current;e&&requestAnimationFrame(()=>{e.scrollTo({top:e.scrollHeight})})},[]),f=R(null),{height:b}=ae({ref:f});M(()=>{(u||!p.current)&&c.length>0&&(u&&(p.current=!0),g())},[b,u,p,c]),M(()=>{m.length>0&&g()},[m]),M(()=>{p.current=r.loaded},[h]),M(()=>{const e=f.current;if(e){const t=()=>{p.current=!0};return e.addEventListener("click",t),()=>{e.removeEventListener("click",t)}}},[f]);const v=c.slice(-30);return t(Mr,{ref:f,children:[0===v.length&&n,v.map((t,r)=>{const n={actions:i,cite:o,speech:s,message:t,setMessage:e=>{d(r=>{const n=r.findIndex(e=>e.id===t.id);-1!==n&&e(r[n])})},isLast:r===v.length-1};return a?e(I,{children:a({Component:vr,props:n})},r):e(vr,{...n},r)}),m.length>0&&e(qr,{children:m.map((t,r)=>e("a",{href:"#!question",children:t},r))})]})}const Mr=n.div`
|
|
839
840
|
display: flex;
|
|
840
841
|
flex-direction: column;
|
|
841
842
|
flex: 1;
|
|
842
|
-
|
|
843
|
+
`,qr=n.div`
|
|
843
844
|
display: flex;
|
|
844
845
|
flex-direction: column;
|
|
845
846
|
align-items: flex-start;
|
|
@@ -856,16 +857,18 @@ import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{styled as
|
|
|
856
857
|
background: var(--bs-secondary-bg-subtle);
|
|
857
858
|
}
|
|
858
859
|
}
|
|
859
|
-
`,
|
|
860
|
+
`,Fr=L(F(({className:r,bot:n,user:i,input:o,logLevel:s,imageResolver:a,nodeIconResolver:l,placeholder:c,actions:d,onboarding:u,conversation:m,cite:h,speech:p,renderItem:g,renderFileItem:f,renderToolContent:b,renderReasoning:y,messages:w,request:k,onReset:E,transformMessages:j=at},C)=>{const $=S(e=>{const t=[];if(u&&!1!==u.enable){const e=lt(u.questions||[]);t.push({chunks:[{content:[u.prologue,...e.map(e=>`[${e}](#!question)`)].join("\n"),tools:[]}],loading:u.loading})}return e?t.concat(j(e.messages||[])):w?t.concat(j(w)):t},[u]),[M,F]=q(m?.id),[I,L]=q(),[D,P]=v(()=>$(m)),[T,O]=q(!1),B=R(null),[U,_]=q([]),H=S(e=>{F(e?.id),P($(e)),_([]),O(!1),E?.(e)},[T,$,E]);return z(C,()=>({reset:H}),[H]),e($e,{request:A(()=>k?"string"==typeof k?N.create({baseURL:k}):k:N,[k]),bot:n,user:i,logLevel:s,imageResolver:a,nodeIconResolver:l,conversationId:M,setConversationId:F,input:I,setInput:L,messages:D,setMessages:P,suggestions:U,setSuggestions:_,loading:T,setLoading:O,renderFileItem:f,reset:H,readonly:!o,renderToolContent:b,renderReasoning:y,children:e(Ir,{className:r,children:t(x,{children:[e(zr,{ref:B,children:e(Sr,{placeholder:c,cite:h,actions:d,loaded:!o,speech:p,renderItem:g,scrollRef:B})}),o&&e(Ar,{...o,scrollRef:B})]})})})}),X),Ar=n(Ge)`
|
|
861
|
+
margin: 0 1rem 1rem;
|
|
862
|
+
`,zr=n.div`
|
|
860
863
|
display: flex;
|
|
861
864
|
flex-direction: column;
|
|
862
865
|
flex: 1;
|
|
863
866
|
overflow-y: auto;
|
|
864
867
|
padding: 0 1rem;
|
|
865
868
|
margin-bottom: .5rem;
|
|
866
|
-
`,
|
|
869
|
+
`,Ir=n.div`
|
|
867
870
|
display: flex;
|
|
868
871
|
flex-direction: column;
|
|
869
872
|
height: 100%;
|
|
870
873
|
position: relative;
|
|
871
|
-
|
|
874
|
+
`,Lr=L(F(({className:t,request:r,disabled:n,supportStop:i,fileTypes:o,speech:s,minRows:a,maxRows:l,toolbar:c,toolbarActions:d,tools:u,autoFocus:m,renderEditableInput:h,placeholder:p,variables:g,onEvent:f,onNewConversation:b},v)=>{const[y,x]=q(!1);return e(Ce,{request:A(()=>r?"string"==typeof r?N.create({baseURL:r}):r:N,[r]),loading:y,setLoading:x,children:e(Ge,{ref:v,className:t,fileTypes:o,speech:s,variables:g,minRows:a,maxRows:l,toolbar:c,toolbarActions:d,tools:u,disabled:n,autoFocus:m,renderEditableInput:h,placeholder:p,supportStop:i,onEvent:f,onNewConversation:b})})}),X);async function Dr(e){const t=await le().use(de).use(ee).use(te).use(re).use(ue).use(ce).process(e);return String(t)}export{ze as ActionButton,Lr as ChatInput,cr as Chunks,Ve as EditableInput,he as FileIcon,ge as FileItem,Se as InputButton,wt as Markdown,Fr as MessageBox,Re as VariableForm,me as isToolMessage,Dr as md2html,lt as pickQuestions,je as useMessageContext,pr as useSynthesis};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@topthink/chat",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"module": "es/index.js",
|
|
5
5
|
"types": "types/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@babel/runtime": "^7.11.2",
|
|
13
13
|
"@breezystack/lamejs": "^1.2.7",
|
|
14
|
-
"@topthink/components": "1.2.
|
|
14
|
+
"@topthink/components": "1.2.7",
|
|
15
15
|
"@types/hast": "^3.0.4",
|
|
16
16
|
"@types/mdast": "^4.0.4",
|
|
17
17
|
"eventemitter3": "^5.0.1",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
},
|
|
64
64
|
"author": "yunwuxin <tzzhangyajun@qq.com> (https://github.com/yunwuxin)",
|
|
65
65
|
"license": "MIT",
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "89dcd5333b71d6c86b1a75e9102c3fb0c808c4a3",
|
|
67
67
|
"scripts": {
|
|
68
68
|
"prebuild": "rimraf es types",
|
|
69
69
|
"build": "rollup -c --environment NODE_ENV:production",
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { RequestInstance } from '@topthink/components';
|
|
3
|
+
import { Conversation, Input, Variable } from '../../types';
|
|
4
|
+
import type { RenderEditableInput } from '../message-box/input-box';
|
|
5
|
+
import type { ToolbarActions } from '../message-box/input-box/toolbar';
|
|
6
|
+
export interface ChatInputProps {
|
|
7
|
+
className?: string;
|
|
8
|
+
request?: RequestInstance | string;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
supportStop?: boolean;
|
|
11
|
+
fileTypes?: string[];
|
|
12
|
+
speech?: {
|
|
13
|
+
model?: string;
|
|
14
|
+
};
|
|
15
|
+
minRows?: number;
|
|
16
|
+
maxRows?: number;
|
|
17
|
+
toolbar?: ReactNode;
|
|
18
|
+
toolbarActions?: ToolbarActions;
|
|
19
|
+
tools?: ReactNode;
|
|
20
|
+
autoFocus?: boolean;
|
|
21
|
+
renderEditableInput?: RenderEditableInput;
|
|
22
|
+
placeholder?: string;
|
|
23
|
+
variables?: {
|
|
24
|
+
config: Variable[];
|
|
25
|
+
values?: Record<string, string>;
|
|
26
|
+
};
|
|
27
|
+
onEvent?: (event: {
|
|
28
|
+
type: string;
|
|
29
|
+
data?: any;
|
|
30
|
+
}) => void;
|
|
31
|
+
onNewConversation?: (conversation: Conversation) => boolean;
|
|
32
|
+
}
|
|
33
|
+
declare const ChatInput: import("react").MemoExoticComponent<import("react").ForwardRefExoticComponent<ChatInputProps & import("react").RefAttributes<Input>>>;
|
|
34
|
+
export default ChatInput;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Dispatch, FC, PropsWithChildren, ReactElement, ReactNode, SetStateAction } from 'react';
|
|
2
2
|
import { Conversation, Input, LogLevel, Message, ToolMessage } from '../../types';
|
|
3
3
|
import { Updater } from '@topthink/components/types/hooks/use-immer';
|
|
4
4
|
import { RequestInstance } from '@topthink/components';
|
|
@@ -22,7 +22,12 @@ export interface ToolRenderResult {
|
|
|
22
22
|
/** 是否默认展开详情 */
|
|
23
23
|
defaultExpanded?: boolean;
|
|
24
24
|
}
|
|
25
|
-
interface
|
|
25
|
+
interface ChatContextType {
|
|
26
|
+
request: RequestInstance;
|
|
27
|
+
loading: boolean;
|
|
28
|
+
setLoading: Dispatch<SetStateAction<boolean>>;
|
|
29
|
+
}
|
|
30
|
+
interface DisplayContextType {
|
|
26
31
|
bot?: {
|
|
27
32
|
name: string;
|
|
28
33
|
avatar: string;
|
|
@@ -31,33 +36,36 @@ interface ContextType {
|
|
|
31
36
|
name: string;
|
|
32
37
|
avatar: string;
|
|
33
38
|
};
|
|
34
|
-
request: RequestInstance;
|
|
35
|
-
imageResolver?: (url: string) => string;
|
|
36
39
|
logLevel?: LogLevel;
|
|
40
|
+
imageResolver?: (url: string) => string;
|
|
37
41
|
nodeIconResolver?: (type: string) => ReactNode;
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
reset: (conversation?: Conversation) => void;
|
|
43
|
+
readonly: boolean;
|
|
40
44
|
input?: Input;
|
|
41
45
|
setInput: Dispatch<SetStateAction<Input | undefined>>;
|
|
46
|
+
conversationId?: string;
|
|
47
|
+
setConversationId: Dispatch<SetStateAction<string | undefined>>;
|
|
42
48
|
messages: Message[];
|
|
43
49
|
setMessages: Updater<Message[]>;
|
|
44
50
|
suggestions: string[];
|
|
45
51
|
setSuggestions: Dispatch<SetStateAction<string[]>>;
|
|
46
|
-
loading: boolean;
|
|
47
|
-
setLoading: Dispatch<SetStateAction<boolean>>;
|
|
48
|
-
reset: (conversation?: Conversation) => void;
|
|
49
|
-
readonly: boolean;
|
|
50
52
|
renderFileItem?: (props: {
|
|
51
|
-
Component:
|
|
53
|
+
Component: FC<FileItemProps>;
|
|
52
54
|
props: FileItemProps;
|
|
53
55
|
}) => ReactElement;
|
|
54
56
|
renderReasoning?: (props: {
|
|
55
|
-
Component:
|
|
57
|
+
Component: FC<ReasoningProps>;
|
|
56
58
|
props: ReasoningProps;
|
|
57
59
|
}) => ReactElement;
|
|
58
60
|
/** 自定义工具渲染函数 */
|
|
59
61
|
renderToolContent?: (options: ToolRenderOptions) => ToolRenderResult | undefined;
|
|
60
62
|
}
|
|
63
|
+
interface ContextType extends ChatContextType, DisplayContextType {
|
|
64
|
+
}
|
|
65
|
+
export declare function useChatContext(): ChatContextType;
|
|
66
|
+
export declare function useDisplayContext(): DisplayContextType;
|
|
67
|
+
export declare function useOptionalDisplayContext(): DisplayContextType | null;
|
|
61
68
|
export declare function useContext(): ContextType;
|
|
69
|
+
export declare function ChatProvider({ children, ...props }: PropsWithChildren<ChatContextType>): import("react/jsx-runtime").JSX.Element;
|
|
62
70
|
export declare function Provider({ children, ...props }: PropsWithChildren<ContextType>): import("react/jsx-runtime").JSX.Element;
|
|
63
71
|
export {};
|
|
@@ -8,6 +8,8 @@ import { ToolRenderOptions, ToolRenderResult } from './context';
|
|
|
8
8
|
import { Components } from 'react-markdown';
|
|
9
9
|
import { FileItemProps } from './file-item';
|
|
10
10
|
import { ReasoningProps } from './reasoning';
|
|
11
|
+
export { default as FileIcon } from './file-icon';
|
|
12
|
+
export type { FileIconProps } from './file-icon';
|
|
11
13
|
export interface MessageBoxProps {
|
|
12
14
|
className?: string;
|
|
13
15
|
bot?: {
|
|
@@ -45,6 +47,7 @@ export interface MessageBoxProps {
|
|
|
45
47
|
minRows?: number;
|
|
46
48
|
toolbar?: ReactNode;
|
|
47
49
|
toolbarActions?: ToolbarActions;
|
|
50
|
+
tools?: ReactNode;
|
|
48
51
|
autoFocus?: boolean;
|
|
49
52
|
renderEditableInput?: RenderEditableInput;
|
|
50
53
|
placeholder?: string;
|
|
@@ -8,6 +8,7 @@ export type RenderEditableInput = (options: {
|
|
|
8
8
|
props: EditableInputProps & RefAttributes<EditableInputRef>;
|
|
9
9
|
}) => ReactNode;
|
|
10
10
|
export interface Props {
|
|
11
|
+
className?: string;
|
|
11
12
|
supportStop?: boolean;
|
|
12
13
|
variables?: {
|
|
13
14
|
config: Variable[];
|
|
@@ -19,12 +20,13 @@ export interface Props {
|
|
|
19
20
|
};
|
|
20
21
|
minRows?: number;
|
|
21
22
|
maxRows?: number;
|
|
22
|
-
scrollRef
|
|
23
|
+
scrollRef?: MutableRefObject<HTMLDivElement | null>;
|
|
23
24
|
toolbar?: ReactNode | true;
|
|
24
25
|
toolbarActions?: ToolbarActions;
|
|
25
26
|
disabled?: boolean;
|
|
26
27
|
placeholder?: string;
|
|
27
28
|
autoFocus?: boolean;
|
|
29
|
+
tools?: ReactNode;
|
|
28
30
|
renderEditableInput?: RenderEditableInput;
|
|
29
31
|
onEvent?: (event: {
|
|
30
32
|
type: string;
|
package/types/index.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export { default as MessageBox, MessageBoxProps } from './components/message-box';
|
|
3
|
+
export { default as ChatInput, ChatInputProps } from './components/chat-input';
|
|
3
4
|
export { default as Chunks, ChunksProps } from './components/message-box/chunks';
|
|
4
5
|
export { useContext as useMessageContext, ToolRenderOptions, ToolRenderResult } from './components/message-box/context';
|
|
5
6
|
export { default as EditableInput } from './components/message-box/input-box/editable-input';
|
|
6
7
|
export type { EditableInputProps, EditableInputRef } from './components/message-box/input-box/editable-input';
|
|
7
8
|
export type { EditableInputComponent, RenderEditableInput } from './components/message-box/input-box';
|
|
9
|
+
export { default as InputButton } from './components/message-box/input-box/input-button';
|
|
8
10
|
export { default as ActionButton } from './components/action-button';
|
|
9
11
|
export { default as Markdown } from './components/markdown';
|
|
10
12
|
export { default as VariableForm } from './components/variable-form';
|
|
11
13
|
export { default as FileItem } from './components/message-box/file-item';
|
|
14
|
+
export { default as FileIcon } from './components/message-box/file-icon';
|
|
15
|
+
export type { FileIconProps } from './components/message-box/file-icon';
|
|
12
16
|
export { default as pickQuestions } from './utils/pick-questions';
|
|
13
17
|
export { default as useSynthesis } from './speech/use-synthesis';
|
|
14
18
|
export { default as md2html } from './utils/md2html';
|