super-agent-sdk 1.0.7 → 1.0.8
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/README.md +79 -18
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +7 -2
- package/dist/index.mjs +90 -85
- package/dist/widget.cjs +51 -8
- package/dist/widget.d.ts +11 -4
- package/dist/widget.mjs +1256 -1065
- package/package.json +1 -1
package/dist/widget.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("react"),Se=require("react-dom/client"),e=require("react/jsx-runtime");function ee(a){const{sdk:s,sessionId:r,onSessionCreated:n,onStreamStart:t,onStreamEnd:i,onError:l,onMessageSend:c}=a,[d,u]=o.useState([]),[f,p]=o.useState("ready"),[x,h]=o.useState(null),g=o.useRef(null),w=o.useRef(!1),S=o.useRef(r),E=o.useRef(!0);S.current=r,E.current=d.length===0;const M=o.useCallback(y=>{if(w.current)return;w.current=!0,c==null||c(y);const b={id:`user_${Date.now()}`,role:"user",parts:[{type:"text",content:y}],timestamp:Date.now()},v=`assistant_${Date.now()}`,j={id:v,role:"assistant",parts:[],timestamp:Date.now()},R=E.current;u(z=>[...z,b,j]),p("submitted"),h(null);const $=z=>{let D="",A="",N=[],P=!1;const L=()=>{const T=[...N];if(D){const H=T.findIndex(O=>O.type==="thinking");H>=0?T[H]={type:"thinking",content:D}:T.unshift({type:"thinking",content:D})}if(A){const H=T.findLastIndex(O=>O.type==="text");H>=0?T[H]={type:"text",content:A}:T.push({type:"text",content:A})}u(H=>H.map(O=>O.id===v?{...O,parts:T}:O))};g.current=s.chat({sessionId:z,message:y,stream:!0,onMessage:T=>{switch(P||(P=!0,p("streaming")),T.type){case"thinking":D+=T.content;break;case"ai":A+=T.content;break;case"tool_call":N.push({type:"tool_call",toolName:T.toolName,toolCallId:T.toolCallId,args:T.args??"{}"});break;case"tool_result":N.push({type:"tool_result",toolName:T.toolName??"",toolCallId:T.toolCallId,content:T.content,artifacts:T.artifacts,renderMode:T.renderMode,html:T.html}),A&&(N.push({type:"text",content:A}),A="");break;case"interrupt":T.interrupt&&N.push({type:"interrupt",interrupt:T.interrupt,resolved:!1});break;case"stop":T.content&&(A=T.content),L();return}L()},onDone:T=>{(A||N.length>0||D)&&L(),T.messageId&&u(O=>O.map(W=>W.id===v?{...W,id:T.messageId}:W)),T.sessionId&&T.sessionId!==S.current&&(n==null||n(T.sessionId));const H=()=>{i==null||i(T.sessionId||z),p("ready"),w.current=!1,g.current=null};if(R){const O=y.slice(0,30),W=T.sessionId||z;s.renameConversation(W,O).catch(()=>{}).finally(H)}else H()},onError:T=>{N.push({type:"error",content:T.message}),L(),h(T),p("error"),l==null||l(T),w.current=!1,g.current=null}}),t==null||t(z)};S.current?$(S.current):s.createSession().then(z=>{S.current=z,n==null||n(z),$(z)}).catch(z=>{h(z instanceof Error?z:new Error(String(z))),p("error"),l==null||l(z instanceof Error?z:new Error(String(z))),w.current=!1})},[s,n,t,i,l,c]),k=o.useCallback(()=>{var b;const y=S.current;(b=g.current)==null||b.abort(),g.current=null,p("ready"),w.current=!1,y&&s.stopGeneration(y).catch(()=>{})},[s]),m=o.useCallback(()=>{if(w.current)return;const y=d.findLastIndex(v=>v.role==="user");if(y<0)return;const b=d[y].parts.filter(v=>v.type==="text").map(v=>v.content).join("");u(v=>v.slice(0,y)),M(b)},[d,M]),I=o.useCallback((y,b)=>{u(N=>N.map(P=>({...P,parts:P.parts.map(L=>L.type==="interrupt"&&L.interrupt.interruptId===y?{...L,resolved:!0,response:b}:L)})));const v=S.current;if(!v)return;const j=`assistant_${Date.now()}`,R={id:j,role:"assistant",parts:[],timestamp:Date.now()};u(N=>[...N,R]),p("streaming");let $="",z="",D=[];const A=()=>{const N=[...D];if($){const P=N.findIndex(L=>L.type==="thinking");P>=0?N[P]={type:"thinking",content:$}:N.unshift({type:"thinking",content:$})}if(z){const P=N.findLastIndex(L=>L.type==="text");P>=0?N[P]={type:"text",content:z}:N.push({type:"text",content:z})}u(P=>P.map(L=>L.id===j?{...L,parts:N}:L))};s.respondInterrupt(y,v,b,N=>{switch(N.type){case"thinking":$+=N.content;break;case"ai":z+=N.content;break;case"tool_call":D.push({type:"tool_call",toolName:N.toolName,toolCallId:N.toolCallId,args:N.args??"{}"});break;case"tool_result":D.push({type:"tool_result",toolName:N.toolName??"",toolCallId:N.toolCallId,content:N.content,artifacts:N.artifacts,renderMode:N.renderMode,html:N.html}),z&&(D.push({type:"text",content:z}),z="");break;case"interrupt":N.interrupt&&D.push({type:"interrupt",interrupt:N.interrupt,resolved:!1});break;case"done":N.messageId&&u(P=>P.map(L=>L.id===j?{...L,id:N.messageId}:L)),p("ready");break}A()},N=>{D.push({type:"error",content:N.message}),A(),h(N),p("error"),l==null||l(N)}).catch(N=>{l==null||l(N instanceof Error?N:new Error(String(N))),p("error")})},[s,l]);return{messages:d,status:f,error:x,sendMessage:M,stop:k,regenerate:m,setMessages:u,respondToInterrupt:I}}const U="sa_active_conversation",Y=20;function J(){try{return localStorage.getItem(U)}catch{return null}}function Ie(a){try{a?localStorage.setItem(U,a):localStorage.removeItem(U)}catch{}}function ae(a){const{sdk:s,onConversationChange:r}=a,[n,t]=o.useState([]),[i,l]=o.useState(()=>J()),[c,d]=o.useState(!1),[u,f]=o.useState(!0),p=o.useRef(!0),x=o.useRef(1),h=o.useRef(!1);o.useEffect(()=>()=>{p.current=!1},[]);const g=o.useCallback(y=>{l(y),Ie(y),y&&(r==null||r(y))},[r]),w=o.useCallback(async()=>{if(!h.current){h.current=!0,d(!0);try{x.current=1;const y=await s.listConversations({page:1,size:Y});if(!p.current)return;t(y.items),f(y.items.length<y.total);const b=J();b&&!y.items.find(v=>v.sessionId===b)&&g(null)}catch{}finally{h.current=!1,p.current&&d(!1)}}},[s,g]),S=o.useCallback(async()=>{if(!(h.current||!u)){h.current=!0,d(!0);try{const y=x.current+1,b=await s.listConversations({page:y,size:Y});if(!p.current)return;x.current=y,t(j=>{const R=new Set(j.map(z=>z.sessionId)),$=b.items.filter(z=>!R.has(z.sessionId));return[...j,...$]});const v=x.current*Y;f(v<b.total)}catch{}finally{h.current=!1,p.current&&d(!1)}}},[s,u]),E=o.useCallback(async y=>(g(y),await s.getMessages(y)),[s,g]),M=o.useCallback(async()=>{try{const y=await s.createSession(),b=new Date().toISOString();t(v=>[{sessionId:y,botId:0,title:"新对话",createTime:b,updateTime:b},...v]),g(y)}catch{g(null)}},[s,g]),k=o.useCallback(async y=>{await s.deleteConversation(y),t(b=>b.filter(v=>v.sessionId!==y)),i===y&&g(null)},[s,i,g]),m=o.useCallback(async(y,b)=>{await s.renameConversation(y,b),t(v=>v.map(j=>j.sessionId===y?{...j,title:b}:j))},[s]),{enabled:I=!0}=a;return o.useEffect(()=>{I&&w()},[I,w]),{conversations:n,activeConversationId:i,loading:c,hasMore:u,loadConversations:w,loadMore:S,switchConversation:E,newConversation:M,deleteConversation:k,renameConversation:m,setActiveConversationId:g}}function te(a){return!a&&a!==0?"":a<1024?`${a} B`:a<1024*1024?`${(a/1024).toFixed(1)} KB`:`${(a/1024/1024).toFixed(1)} MB`}function se(a){return a==="pdf"?"PDF":a==="docx"?"DOC":a==="html"?"HTM":a.slice(0,3).toUpperCase()||"FILE"}function re(a){if(!a)return[];const s=[];for(const r of a)r.type==="file"&&r.data&&s.push({id:r.data.id??"",type:r.data.fileType??r.data.type??"",filename:r.data.name??r.data.filename??"",url:r.data.url??"",mime:r.data.mime,size:r.data.size,summary:r.data.summary});return s}async function K(a){try{const s=await fetch(a.url);if(!s.ok)throw new Error(`HTTP ${s.status}`);const r=await s.blob(),n=URL.createObjectURL(r),t=document.createElement("a");t.href=n,t.download=a.filename,document.body.appendChild(t),t.click(),t.remove(),setTimeout(()=>URL.revokeObjectURL(n),4e3)}catch{const s=document.createElement("a");s.href=a.url,s.download=a.filename,s.target="_blank",document.body.appendChild(s),s.click(),s.remove()}}const ne=o.createContext(null);function _(){const a=o.useContext(ne);if(!a)throw new Error("useChatContext must be used within ChatProvider");return a}function V({sdk:a,hooks:s,children:r}){const[n,t]=o.useState(!1),[i,l]=o.useState(!1),[c,d]=o.useState(null),[u,f]=o.useState(!1);o.useEffect(()=>{a.getToken().then(()=>{f(!0)}).catch(()=>{f(!0)})},[a]);const p=ae({sdk:a,onConversationChange:s==null?void 0:s.onConversationChange,enabled:u}),x=ee({sdk:a,sessionId:p.activeConversationId,onSessionCreated:b=>{p.setActiveConversationId(b)},onStreamStart:s==null?void 0:s.onStreamStart,onStreamEnd:b=>{var v;(v=s==null?void 0:s.onStreamEnd)==null||v.call(s,b),p.loadConversations()},onError:s==null?void 0:s.onError,onMessageSend:s==null?void 0:s.onMessageSend}),h=o.useCallback(async b=>{const v=await p.switchConversation(b);x.setMessages(v),t(!1)},[p,x]),g=o.useCallback(async()=>{x.setMessages([]),t(!1),await p.newConversation()},[p,x]),w=o.useCallback(async b=>{await p.deleteConversation(b),p.activeConversationId===b&&x.setMessages([])},[p,x]);o.useEffect(()=>{p.activeConversationId&&x.messages.length===0&&a.getMessages(p.activeConversationId).then(b=>{x.setMessages(b)}).catch(()=>{})},[]);const S=o.useCallback(()=>{t(b=>!b)},[]),E=o.useCallback(()=>{l(b=>!b),d(b=>null)},[]),M=o.useCallback(b=>{d(b),l(!0)},[]),k=o.useMemo(()=>{const b=new Set,v=[];for(const j of x.messages)for(const R of j.parts)if(R.type==="tool_result"&&R.artifacts)for(const $ of re(R.artifacts)){const z=$.id||`${$.url}:${$.filename}`;b.has(z)||(b.add(z),v.push($))}return v},[x.messages]),m=o.useCallback((b,v,j)=>{a.feedback(b,v,j).catch(()=>{})},[a]),I=o.useCallback(b=>{a.cancelFeedback(b).catch(()=>{})},[a]),y=o.useMemo(()=>({sdk:a,status:x.status,error:x.error,messages:x.messages,conversations:p.conversations,activeConversationId:p.activeConversationId,isThreadListOpen:n,isArtifactDrawerOpen:i,allArtifacts:k,activePreviewArtifact:c,hasMore:p.hasMore,sendMessage:x.sendMessage,stop:x.stop,regenerate:x.regenerate,switchConversation:h,newConversation:g,deleteConversation:w,renameConversation:p.renameConversation,loadMoreConversations:p.loadMore,toggleThreadList:S,toggleArtifactDrawer:E,openArtifactPreview:M,feedback:m,cancelFeedback:I,respondToInterrupt:x.respondToInterrupt}),[a,x.status,x.error,x.messages,x.sendMessage,x.stop,x.regenerate,x.respondToInterrupt,p.conversations,p.activeConversationId,p.renameConversation,p.hasMore,p.loadMore,n,i,k,c,h,g,w,S,E,M,m,I]);return e.jsx(ne.Provider,{value:y,children:r})}const Te="https://at.alicdn.com/t/c/font_5210396_pffyg5g21kn.js";let Z=!1;function ie(a=Te){if(Z||typeof document>"u")return;Z=!0;const s=a.split("?")[0].split("/").pop();if(s&&document.querySelector(`script[src*="${s}"]`))return;const r=document.createElement("script");r.src=a,document.head.appendChild(r)}ie();function C({name:a,size:s=16,color:r="currentColor",className:n,style:t}){return e.jsx("svg",{className:n,style:{width:s,height:s,fill:r,verticalAlign:"middle",...t},"aria-hidden":"true",children:e.jsx("use",{href:`#icon-${a}`})})}function oe({isOpen:a,onClick:s}){return e.jsx("button",{className:`sa-trigger ${a?"sa-trigger-open":""}`,onClick:s,children:a?e.jsx(C,{name:"x",size:24,color:"#fff"}):e.jsx(C,{name:"message-square",size:24,color:"#fff"})})}function le({title:a,onClose:s,onToggleThreadList:r,showClose:n=!0,artifactCount:t=0,onToggleArtifactDrawer:i}){return e.jsxs("div",{className:"sa-header",children:[e.jsxs("div",{className:"sa-header-left",children:[e.jsx("div",{className:"sa-header-avatar",children:"A"}),e.jsxs("div",{className:"sa-header-info",children:[e.jsx("div",{className:"sa-header-title",children:a}),e.jsx("div",{className:"sa-header-status",children:"● Online"})]})]}),e.jsxs("div",{className:"sa-header-actions",children:[t>0&&i&&e.jsxs("button",{className:"sa-header-btn sa-header-btn-artifact",onClick:i,title:"会话产物",children:[e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{children:"产物"})]}),e.jsx("button",{className:"sa-header-btn",onClick:r,title:"会话列表",children:e.jsx(C,{name:"menu",size:16})}),n&&e.jsx("button",{className:"sa-header-btn",onClick:s,title:"关闭",children:e.jsx(C,{name:"x",size:16})})]})]})}const q=a=>String(a).padStart(2,"0");function ce(a){if(!a)return"";const s=new Date(a);return isNaN(s.getTime())?"":`${s.getFullYear()}-${q(s.getMonth()+1)}-${q(s.getDate())} ${q(s.getHours())}:${q(s.getMinutes())}:${q(s.getSeconds())}`}function Me(a){const s=new Date,r=new Date(s.getFullYear(),s.getMonth(),s.getDate()).getTime(),n=r-6*864e5,t=[],i=[],l=[];for(const d of a){const u=new Date(d.updateTime).getTime();u>=r?t.push(d):u>=n?i.push(d):l.push(d)}const c=[];return t.length&&c.push({label:"今天",items:t}),i.length&&c.push({label:"最近 7 天",items:i}),l.length&&c.push({label:"更早",items:l}),c}function de(){const{conversations:a,activeConversationId:s,switchConversation:r,newConversation:n,deleteConversation:t,renameConversation:i,isThreadListOpen:l,toggleThreadList:c,hasMore:d,loadMoreConversations:u}=_(),[f,p]=o.useState(null),[x,h]=o.useState(""),g=o.useRef(null),w=o.useCallback((k,m)=>{p(k),h(m??"")},[]),S=o.useCallback(k=>{x.trim()&&i(k,x.trim()),p(null)},[x,i]),E=o.useCallback(()=>{const k=g.current;!k||!d||k.scrollHeight-k.scrollTop-k.clientHeight<50&&u()},[d,u]),M=o.useMemo(()=>Me(a),[a]);return l?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"sa-thread-list-overlay",onClick:c}),e.jsxs("div",{className:"sa-thread-list",children:[e.jsxs("div",{className:"sa-thread-list-header",children:[e.jsx("span",{className:"sa-thread-list-title",children:"会话记录"}),e.jsxs("button",{className:"sa-thread-list-new",onClick:n,children:[e.jsx(C,{name:"plus",size:14})," 新会话"]})]}),e.jsxs("div",{className:"sa-thread-list-items",ref:g,onScroll:E,children:[a.length===0&&e.jsx("div",{className:"sa-thread-list-empty",children:"暂无会话"}),M.map(k=>e.jsxs("div",{className:"sa-thread-group",children:[e.jsx("div",{className:"sa-thread-group-label",children:k.label}),k.items.map(m=>e.jsxs("div",{className:`sa-thread-item ${m.sessionId===s?"active":""}`,onClick:()=>{f!==m.sessionId&&r(m.sessionId)},children:[f===m.sessionId?e.jsx("input",{className:"sa-thread-item-edit",value:x,onChange:I=>h(I.target.value),onBlur:()=>S(m.sessionId),onKeyDown:I=>{I.key==="Enter"&&S(m.sessionId),I.key==="Escape"&&p(null)},onClick:I=>I.stopPropagation(),autoFocus:!0}):e.jsxs("div",{className:"sa-thread-item-info",children:[e.jsx("span",{className:"sa-thread-item-title",children:m.title||"新会话"}),e.jsx("span",{className:"sa-thread-item-time",children:ce(m.updateTime)})]}),e.jsxs("div",{className:"sa-thread-item-actions",children:[e.jsx("button",{className:"sa-thread-item-action-btn",onClick:I=>{I.stopPropagation(),w(m.sessionId,m.title)},title:"重命名",children:e.jsx(C,{name:"pen",size:13})}),e.jsx("button",{className:"sa-thread-item-action-btn",onClick:I=>{I.stopPropagation(),t(m.sessionId)},title:"删除",children:e.jsx(C,{name:"trash2",size:13})})]})]},m.sessionId))]},k.label)),d&&e.jsx("div",{className:"sa-thread-list-loading",children:"加载更多..."})]})]})]}):null}function pe(a){const s=o.useRef(null),r=o.useRef(!1),n=o.useRef(0),t=o.useCallback(()=>{const l=s.current;if(!l)return;const{scrollTop:c,scrollHeight:d,clientHeight:u}=l,f=d-c-u<40;c<n.current&&!f&&(r.current=!0),f&&(r.current=!1),n.current=c},[]);o.useEffect(()=>{const l=s.current;if(l)return l.addEventListener("scroll",t,{passive:!0}),()=>l.removeEventListener("scroll",t)},[t]),o.useEffect(()=>{if(r.current)return;const l=s.current;l&&requestAnimationFrame(()=>{l.scrollTop=l.scrollHeight})},a);const i=o.useCallback(()=>{r.current=!1;const l=s.current;l&&(l.scrollTop=l.scrollHeight)},[]);return{containerRef:s,scrollToBottom:i}}function B(a){return a.value++}function F(a,s){const r=[],n=[],t=()=>{n.length>0&&(r.push(n.join("")),n.length=0)};let i=0;for(;i<a.length;){const l=a[i];if(l==="`"){const c=a.indexOf("`",i+1);if(c!==-1){t(),r.push(e.jsx("code",{className:"sa-inline-code",children:a.slice(i+1,c)},B(s))),i=c+1;continue}}else if(l==="*"&&a[i+1]==="*"){const c=a.indexOf("**",i+2);if(c!==-1){t(),r.push(e.jsx("strong",{children:F(a.slice(i+2,c),s)},B(s))),i=c+2;continue}}else if(l==="*"){const c=a.indexOf("*",i+1);if(c!==-1){t(),r.push(e.jsx("em",{children:F(a.slice(i+1,c),s)},B(s))),i=c+1;continue}}else if(l==="["){const c=a.indexOf("]",i+1);if(c!==-1&&a[c+1]==="("){const d=a.indexOf(")",c+2);if(d!==-1){const u=a.slice(i+1,c),f=a.slice(c+2,d);t(),r.push(e.jsx("a",{href:f,target:"_blank",rel:"noreferrer",children:F(u,s)},B(s))),i=d+1;continue}}}n.push(l),i++}return t(),r}function Ee(a,s){const r=[],n=a.split(`
|
|
2
|
-
`);let t=0;const i=d=>d.trim()==="",l=d=>/^#{1,6}\s+\S/.test(d.trim()),c=d=>/^(?:[-*+]|\d+[.)])\s+\S/.test(d.trim());for(;t<n.length;){const d=n[t],u=d.trim();if(i(d)){t++;continue}if(l(d)){const p=u.match(/^(#{1,6})\s+(.*)$/);if(p){const h=`h${Math.min(p[1].length,6)}`;r.push(o.createElement(h,{key:B(s)},F(p[2],s)))}t++;continue}if(c(d)){const p=/^\d+[.)]/.test(u),x=[];for(;t<n.length&&c(n[t]);){const h=n[t].trim().match(/^(?:[-*+]|\d+[.)])\s+(.*)$/);h&&x.push(e.jsx("li",{children:F(h[1],s)},B(s))),t++}r.push(p?e.jsx("ol",{className:"sa-list",children:x},B(s)):e.jsx("ul",{className:"sa-list",children:x},B(s)));continue}const f=[];for(;t<n.length&&!i(n[t])&&!l(n[t])&&!c(n[t]);)f.push(n[t]),t++;r.push(e.jsx("p",{className:"sa-text-paragraph",children:F(f.join(" "),s)},B(s)))}return r}function De(a){const s={value:0},r=[],n=a.split("```");for(let t=0;t<n.length;t++)if(t%2===0)r.push(...Ee(n[t],s));else if(n[t].trim()!==""){let i=n[t].replace(/^\r?\n/,"");i=i.replace(/^[a-zA-Z0-9_+#.-]+\r?\n/,""),i=i.replace(/\r?\n$/,""),r.push(e.jsx("pre",{className:"sa-code-block",children:e.jsx("code",{children:i})},B(s)))}return r}function ue({content:a}){return e.jsx("div",{className:"sa-text-part",children:De(a)})}function fe({content:a}){const[s,r]=o.useState(!1);return e.jsxs("div",{className:"sa-thinking-part",onClick:()=>r(!s),children:[e.jsxs("div",{className:"sa-thinking-header",children:[e.jsx("span",{className:"sa-thinking-icon",children:e.jsx(C,{name:"brain",size:14})}),e.jsx("span",{className:"sa-thinking-label",children:"思考过程"}),e.jsx("span",{className:`sa-thinking-arrow ${s?"expanded":""}`,children:e.jsx(C,{name:"chevron-right",size:12})})]}),s&&e.jsx("div",{className:"sa-thinking-content",children:a})]})}function xe({toolName:a,toolCallId:s,args:r}){const[n,t]=o.useState(!1);let i=r;try{i=JSON.stringify(JSON.parse(r),null,2)}catch{}return e.jsxs("div",{className:"sa-tool-call-part",children:[e.jsxs("div",{className:"sa-tool-call-header",onClick:()=>t(!n),children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(C,{name:"zap",size:14})}),e.jsxs("span",{className:"sa-tool-name",children:["调用工具: ",a]}),e.jsx("span",{className:`sa-thinking-arrow ${n?"expanded":""}`,children:e.jsx(C,{name:"chevron-right",size:12})})]}),n&&e.jsx("pre",{className:"sa-tool-args",children:i})]})}function $e({html:a}){const[s,r]=o.useState(null),n=o.useRef(null);o.useEffect(()=>{let i=!1;return Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs")).then(l=>{i||r(l.default.sanitize(a,{FORCE_BODY:!0}))}),()=>{i=!0}},[a]);const t=o.useCallback(()=>{var l;const i=n.current;if(i)try{const c=i.contentDocument||((l=i.contentWindow)==null?void 0:l.document);if(!(c!=null&&c.body))return;const d=c.body.scrollHeight;i.style.height=Math.min(d,500)+"px"}catch{}},[]);return o.useEffect(()=>{const i=n.current;if(!(!i||s===null))return i.addEventListener("load",t),()=>i.removeEventListener("load",t)},[s,t]),s===null?null:e.jsx("div",{className:"sa-tool-result-html",children:e.jsx("iframe",{ref:n,srcDoc:s,className:"sa-tool-result-html-iframe",sandbox:"allow-same-origin",title:"HTML 产物"})})}function me({toolName:a,content:s,artifacts:r,renderMode:n,html:t,artifactPreview:i}){const{openArtifactPreview:l}=_(),c=re(r);return e.jsxs("div",{className:"sa-tool-result-part",children:[e.jsxs("div",{className:"sa-tool-result-header",children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(C,{name:"circle-check",size:14,color:"#10b981"})}),e.jsx("span",{className:"sa-tool-result-label",children:a||"工具返回"})]}),n==="html"&&t?e.jsx($e,{html:t}):s?e.jsx("div",{className:"sa-tool-result-content",children:s}):null,c.length>0&&e.jsx("ul",{className:"sa-artifact-file-list",role:"list",children:c.map((d,u)=>e.jsxs("li",{className:"sa-artifact-file-card",onClick:i?()=>l(d):void 0,onKeyDown:i?f=>{(f.key==="Enter"||f.key===" ")&&(f.preventDefault(),l(d))}:void 0,tabIndex:i?0:void 0,role:i?"button":void 0,"aria-label":i?`预览 ${d.filename}`:void 0,style:i?{cursor:"pointer"}:void 0,children:[e.jsx("div",{className:"sa-artifact-file-icon",children:e.jsx("span",{className:"sa-artifact-chip-type",children:se(d.type)})}),e.jsxs("div",{className:"sa-artifact-file-info",children:[e.jsx("span",{className:"sa-artifact-file-name",children:d.filename}),d.size!=null&&e.jsx("span",{className:"sa-artifact-file-size",children:te(d.size)})]}),i&&e.jsx("button",{className:"sa-artifact-file-download",title:"下载","aria-label":`下载 ${d.filename}`,onClick:f=>{f.stopPropagation(),K(d)},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})})]},d.id||`art-${u}`))})]})}function he({content:a,onRetry:s}){return e.jsxs("div",{className:"sa-error-part",children:[e.jsxs("div",{className:"sa-error-content",children:[e.jsx("span",{className:"sa-error-icon",children:e.jsx(C,{name:"triangle-alert",size:14})}),e.jsx("span",{children:a})]}),s&&e.jsx("button",{className:"sa-error-retry",onClick:s,children:"重试"})]})}function Re({interrupt:a,onRespond:s,disabled:r,answeredAction:n}){var c,d;const t=a.options,i=(typeof(t==null?void 0:t[0])=="string"?t[0]:(c=t==null?void 0:t[0])==null?void 0:c.label)||"确认",l=(typeof(t==null?void 0:t[1])=="string"?t[1]:(d=t==null?void 0:t[1])==null?void 0:d.label)||"取消";return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(C,{name:"circle-alert",size:16,color:"#f59e0b"}),e.jsx("span",{children:a.content})]}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-primary ${n==="confirm"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>s({action:"confirm"}),children:[e.jsx(C,{name:"check",size:14})," ",i]}),e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-secondary ${n==="cancel"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>s({action:"cancel"}),children:[e.jsx(C,{name:"x",size:14})," ",l]})]}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function Le({interrupt:a,onRespond:s,disabled:r,answeredAction:n}){var u,f,p,x;const t=a.options,i=(typeof(t==null?void 0:t[0])=="string"?t[0]:(u=t==null?void 0:t[0])==null?void 0:u.label)||"批准",l=(typeof(t==null?void 0:t[1])=="string"?t[1]:(f=t==null?void 0:t[1])==null?void 0:f.label)||"拒绝",c=(typeof(t==null?void 0:t[2])=="string"?t[2]:(p=t==null?void 0:t[2])==null?void 0:p.label)||"跳过",d=((x=a.metadata)==null?void 0:x.showSkip)!==!1;return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(C,{name:"circle-alert",size:16,color:"#f59e0b"}),e.jsx("span",{children:a.content})]}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-primary ${n==="approve"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>s({action:"approve"}),children:[e.jsx(C,{name:"check",size:14})," ",i]}),e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-danger ${n==="reject"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>s({action:"reject"}),children:[e.jsx(C,{name:"x",size:14})," ",l]}),d&&e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-secondary ${n==="skip"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>s({action:"skip"}),children:[e.jsx(C,{name:"chevron-right",size:14})," ",c]})]}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function Ae(a){return typeof a=="string"?{value:a,label:a}:a}function Pe({interrupt:a,onRespond:s,disabled:r,initialValue:n}){const t=(a.options??[]).map(Ae);return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(C,{name:"list",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-options",children:t.map(i=>{const l=n===i.value;return e.jsxs("button",{className:`sa-interrupt-option ${i.danger?"sa-interrupt-option-danger":""} ${l?"sa-interrupt-option-selected":""}`,disabled:r,onClick:()=>s({action:"select",value:i.value}),children:[e.jsx(C,{name:l?"circle-check":"circle",size:14}),i.icon&&e.jsx(C,{name:i.icon,size:14}),e.jsx("span",{children:i.label}),i.description&&e.jsx("span",{className:"sa-interrupt-option-desc",children:i.description})]},i.value)})}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function Oe(a){return typeof a=="string"?{value:a,label:a}:a}function He({interrupt:a,onRespond:s,disabled:r,initialValue:n}){const t=(a.options??[]).map(Oe),[i,l]=o.useState(()=>new Set(Array.isArray(n)?n:[])),c=d=>{r||l(u=>{const f=new Set(u);return f.has(d)?f.delete(d):f.add(d),f})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(C,{name:"list-checks",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-options",children:t.map(d=>e.jsxs("button",{className:`sa-interrupt-option ${i.has(d.value)?"sa-interrupt-option-selected":""}`,disabled:r,onClick:()=>c(d.value),children:[e.jsx(C,{name:i.has(d.value)?"square-check":"square",size:14}),e.jsx("span",{children:d.label})]},d.value))}),!r&&e.jsx("div",{className:"sa-interrupt-actions",children:e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:i.size===0,onClick:()=>s({action:"submit",value:Array.from(i)}),children:[e.jsx(C,{name:"check",size:14})," 确认 (",i.size,")"]})}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function Be({interrupt:a,onRespond:s,disabled:r,initialValue:n}){const[t,i]=o.useState(n??""),l=a.inputType??"text",c=l==="textarea",d=()=>{r||a.required&&!t.trim()||s({action:"submit",value:t.trim()})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(C,{name:"text-cursor-input",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-input-area",children:c?e.jsx("textarea",{className:"sa-interrupt-textarea",value:t,onChange:u=>i(u.target.value),placeholder:a.placeholder,maxLength:a.maxLength,rows:3,disabled:r}):e.jsx("input",{className:"sa-interrupt-input",type:l,value:t,onChange:u=>i(u.target.value),placeholder:a.placeholder,maxLength:a.maxLength,onKeyDown:u=>u.key==="Enter"&&d(),disabled:r})}),!r&&e.jsx("div",{className:"sa-interrupt-actions",children:e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:a.required?!t.trim():!1,onClick:d,children:[e.jsx(C,{name:"check",size:14})," 提交"]})}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function _e({interrupt:a,onRespond:s,disabled:r,initialValue:n}){const t=a.fields??[],[i,l]=o.useState(()=>{const f={};for(const p of t)f[p.name]=(n==null?void 0:n[p.name])??p.defaultValue??"";return f}),c=(f,p)=>{r||l(x=>({...x,[f]:p}))},d=!r&&t.every(f=>!f.required||i[f.name]),u=()=>{d&&s({action:"submit",value:i})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(C,{name:"file-text",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-form",children:t.map(f=>e.jsxs("div",{className:"sa-interrupt-form-field",children:[e.jsxs("label",{className:"sa-interrupt-form-label",children:[f.label,f.required&&e.jsx("span",{className:"sa-interrupt-form-required",children:"*"})]}),Fe(f,i[f.name],p=>c(f.name,p),r)]},f.name))}),!r&&e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:!d,onClick:u,children:[e.jsx(C,{name:"check",size:14})," 提交"]}),e.jsx("button",{className:"sa-interrupt-btn sa-interrupt-btn-secondary",onClick:()=>s({action:"cancel"}),children:"取消"})]}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function Fe(a,s,r,n){switch(a.type){case"textarea":return e.jsx("textarea",{className:"sa-interrupt-textarea",value:s,onChange:t=>r(t.target.value),placeholder:a.placeholder,rows:3,disabled:n});case"select":return e.jsxs("select",{className:"sa-interrupt-select",value:s,onChange:t=>r(t.target.value),disabled:n,children:[e.jsx("option",{value:"",children:"请选择"}),(a.options??[]).map(t=>e.jsx("option",{value:t,children:t},t))]});default:return e.jsx("input",{className:"sa-interrupt-input",type:a.type,value:s,onChange:t=>r(t.target.value),placeholder:a.placeholder,disabled:n})}}function ge({interrupt:a,resolved:s,response:r,onRespond:n}){const t=s,i=r==null?void 0:r.value;switch(a.interruptType){case"confirm":return e.jsx(Re,{interrupt:a,onRespond:n,disabled:t,answeredAction:r==null?void 0:r.action});case"approve":return e.jsx(Le,{interrupt:a,onRespond:n,disabled:t,answeredAction:r==null?void 0:r.action});case"select":return e.jsx(Pe,{interrupt:a,onRespond:n,disabled:t,initialValue:i});case"multiSelect":return e.jsx(He,{interrupt:a,onRespond:n,disabled:t,initialValue:i});case"input":return e.jsx(Be,{interrupt:a,onRespond:n,disabled:t,initialValue:i});case"form":return e.jsx(_e,{interrupt:a,onRespond:n,disabled:t,initialValue:i});default:return e.jsxs("div",{className:"sa-interrupt-unknown",children:["未知的交互类型: ",a.interruptType]})}}const We=[{value:1,label:"事实错误"},{value:2,label:"逻辑问题"},{value:3,label:"不相关"},{value:4,label:"信息过时"},{value:5,label:"冗长啰嗦"},{value:6,label:"难以理解"}];function be({message:a,onCopy:s,onRegenerate:r,onFeedback:n}){const{status:t,cancelFeedback:i}=_(),[l,c]=o.useState(null),[d,u]=o.useState(!1),[f,p]=o.useState(!1),x=o.useRef(null),h=o.useCallback(()=>{s(),u(!0),setTimeout(()=>u(!1),2e3)},[s]),g=o.useCallback(()=>{l==="like"?(c(null),i(a.id)):(c("like"),p(!1),n==null||n(a.id,"like"))},[l,a.id,n,i]),w=o.useCallback(()=>{l==="dislike"?(c(null),p(!1),i(a.id)):p(!0)},[l,a.id,i]),S=o.useCallback((M,k)=>{c("dislike"),p(!1),n==null||n(a.id,"dislike",{reason:M,remark:(k==null?void 0:k.trim())||void 0})},[a.id,n]),E=o.useCallback(()=>{p(!1)},[]);return o.useEffect(()=>{if(!f)return;const M=m=>{x.current&&!x.current.contains(m.target)&&p(!1)},k=m=>{m.key==="Escape"&&p(!1)};return document.addEventListener("mousedown",M),document.addEventListener("keydown",k),()=>{document.removeEventListener("mousedown",M),document.removeEventListener("keydown",k)}},[f]),t!=="ready"&&t!=="error"?null:e.jsxs("div",{className:"sa-action-bar",children:[e.jsxs("button",{className:"sa-action-btn",onClick:h,title:"复制",children:[e.jsx(C,{name:d?"circle-check":"copy",size:14})," ",d?"已复制":"复制"]}),r&&e.jsxs("button",{className:"sa-action-btn",onClick:r,title:"重新生成",children:[e.jsx(C,{name:"refresh-cw",size:14})," 重新生成"]}),n&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"sa-action-divider"}),e.jsx("button",{className:`sa-action-btn ${l==="like"?"sa-action-btn-active":""}`,onClick:g,title:"有用","aria-label":"点赞",children:e.jsx(C,{name:"thumbs-up",size:14})}),e.jsx("button",{className:`sa-action-btn ${l==="dislike"?"sa-action-btn-active":""}`,onClick:w,title:"无用","aria-label":"踩","aria-expanded":f,"aria-haspopup":"dialog",children:e.jsx(C,{name:"thumbs-down",size:14})})]}),f&&e.jsx("div",{ref:x,children:e.jsx(qe,{onSubmit:S,onCancel:E})})]})}function qe({onSubmit:a,onCancel:s}){const[r,n]=o.useState(void 0),[t,i]=o.useState("");return e.jsxs("div",{className:"sa-dislike-panel",children:[e.jsx("div",{className:"sa-dislike-panel-title",children:"请选择不满意的原因(可选)"}),e.jsx("div",{className:"sa-dislike-reasons",children:We.map(l=>e.jsx("button",{className:`sa-dislike-reason-btn ${r===l.value?"active":""}`,onClick:()=>n(r===l.value?void 0:l.value),children:l.label},l.value))}),e.jsx("textarea",{className:"sa-dislike-remark",placeholder:"补充描述(选填,≤500 字)",maxLength:500,value:t,onChange:l=>i(l.target.value),rows:2}),e.jsxs("div",{className:"sa-dislike-actions",children:[e.jsx("button",{className:"sa-dislike-submit",onClick:()=>a(r,t||void 0),children:"提交"}),e.jsx("button",{className:"sa-dislike-cancel",onClick:s,children:"取消"})]})]})}function Q({src:a,role:s}){const r=s==="assistant"?"A":"U";return a?a.startsWith("http")||a.startsWith("/")||a.startsWith("data:")?e.jsx("div",{className:`sa-avatar sa-avatar-${s}`,children:e.jsx("img",{src:a,alt:s,style:{width:"100%",height:"100%",borderRadius:"inherit",objectFit:"cover"}})}):e.jsx("div",{className:`sa-avatar sa-avatar-${s}`,children:a}):e.jsx("div",{className:`sa-avatar sa-avatar-${s}`,children:r})}function ve({message:a,isStreaming:s,isLast:r,slots:n,avatar:t,artifactPreview:i}){const{regenerate:l,feedback:c,respondToInterrupt:d}=_(),u=o.useCallback(()=>{var I;const m=a.parts.filter(y=>y.type==="text").map(y=>y.content).join(`
|
|
3
|
-
`);(I=navigator.clipboard)==null||I.writeText(m).catch(()=>{})},[a]),f=a.role==="user",p=n.Message;if(p)return e.jsx(p,{message:a,isStreaming:s,isLast:r,avatar:t,slots:n,onCopy:u,onRegenerate:l,onFeedback:c});const x=n.TextPart??ue,h=n.ThinkingPart??fe,g=n.ToolCallPart??xe,w=n.ToolResultPart??me,S=n.ErrorPart??he,E=n.ActionBar??be,M=!f&&a.parts.some(m=>m.type==="text"&&m.content),k=!a.id.startsWith("assistant_")&&!a.id.startsWith("user_");return e.jsxs("div",{className:`sa-message ${f?"sa-message-user":"sa-message-assistant"}`,children:[!f&&e.jsx(Q,{src:t==null?void 0:t.assistant,role:"assistant"}),e.jsxs("div",{className:"sa-message-content",children:[e.jsx("span",{className:`sa-message-time ${f?"sa-message-time-right":""}`,children:ce(a.timestamp)}),e.jsxs("div",{className:"sa-message-body",children:[a.parts.map((m,I)=>{switch(m.type){case"text":return e.jsx(x,{content:m.content},I);case"thinking":return e.jsx(h,{content:m.content},I);case"tool_call":return e.jsx(g,{toolName:m.toolName,toolCallId:m.toolCallId,args:m.args},I);case"tool_result":return e.jsx(w,{toolName:m.toolName,toolCallId:m.toolCallId,content:m.content,artifacts:m.artifacts,renderMode:m.renderMode,html:m.html,artifactPreview:i},I);case"error":return e.jsx(S,{content:m.content,onRetry:l},I);case"interrupt":return e.jsx(ge,{interrupt:m.interrupt,resolved:m.resolved??!1,response:m.response,onRespond:y=>d(m.interrupt.interruptId,y)},I);default:return null}}),s&&r&&!f&&e.jsx("span",{className:"sa-streaming-cursor",children:"|"}),!f&&!s&&M&&e.jsx(E,{message:a,onCopy:u,onRegenerate:r?l:void 0,onFeedback:k?c:void 0})]})]}),f&&e.jsx(Q,{src:t==null?void 0:t.user,role:"user"})]})}function we({message:a,suggestedPrompts:s,onPromptClick:r}){return e.jsxs("div",{className:"sa-welcome",children:[e.jsx("div",{className:"sa-welcome-icon",children:e.jsx(C,{name:"sparkles",size:40})}),e.jsx("p",{className:"sa-welcome-message",children:a||"你好!有什么可以帮你的?"}),s&&s.length>0&&e.jsx("div",{className:"sa-welcome-prompts",children:s.map((n,t)=>e.jsx("button",{className:"sa-welcome-prompt",onClick:()=>r(n),children:n},t))})]})}function X({slots:a,welcomeMessage:s,suggestedPrompts:r,avatar:n,artifactPreview:t}){const{messages:i,status:l,sendMessage:c}=_(),{containerRef:d}=pe([i]),u=l==="streaming",f=a.WelcomeScreen??we;return i.length===0?e.jsx("div",{className:"sa-thread",ref:d,children:e.jsx(f,{message:s,suggestedPrompts:r,onPromptClick:c})}):e.jsx("div",{className:"sa-thread",ref:d,children:i.map((p,x)=>e.jsx(ve,{message:p,isStreaming:u,isLast:x===i.length-1,slots:a,avatar:n,artifactPreview:t},p.id))})}function Ke({items:a,activeIndex:s,onSelect:r}){const n=o.useRef(null);return o.useEffect(()=>{var i;const t=(i=n.current)==null?void 0:i.children[s];t==null||t.scrollIntoView({block:"nearest"})},[s]),a.length===0?e.jsx("div",{className:"sa-slash-menu",children:e.jsx("div",{className:"sa-slash-menu-empty",children:"没有匹配的技能"})}):e.jsx("div",{className:"sa-slash-menu",ref:n,children:a.map((t,i)=>e.jsxs("div",{className:`sa-slash-menu-item${i===s?" sa-slash-menu-item--active":""}`,onMouseDown:l=>{l.preventDefault(),r(t)},children:[e.jsxs("span",{className:"sa-slash-menu-item-name",children:["/",t.name]}),(t.displayName||t.description)&&e.jsx("span",{className:"sa-slash-menu-item-desc",children:t.displayName||t.description})]},t.code))})}function Ye({sdk:a,input:s,onSelect:r}){const[n,t]=o.useState([]),[i,l]=o.useState(!1),[c,d]=o.useState(0),u=o.useRef(!1);o.useEffect(()=>{u.current||(u.current=!0,a.listSkills().then(t).catch(()=>t([])))},[a]);const f=/^\/\S*$/.test(s);o.useEffect(()=>{l(f)},[f]),o.useEffect(()=>{f&&d(0)},[s,f]);const p=s.startsWith("/")?s.slice(1).toLowerCase():"",x=o.useMemo(()=>n.filter(k=>!p||k.code.toLowerCase().includes(p)||k.name.toLowerCase().includes(p)||k.displayName&&k.displayName.toLowerCase().includes(p)),[n,p]),h=o.useRef(i),g=o.useRef(x),w=o.useRef(c),S=o.useRef(r);h.current=i,g.current=x,w.current=c,S.current=r;const E=o.useCallback(k=>{if(!h.current||g.current.length===0)return!1;switch(k.key){case"ArrowDown":return k.preventDefault(),d(m=>(m+1)%g.current.length),!0;case"ArrowUp":return k.preventDefault(),d(m=>(m-1+g.current.length)%g.current.length),!0;case"Enter":case"Tab":return k.preventDefault(),S.current(g.current[w.current]),l(!1),!0;case"Escape":return k.preventDefault(),l(!1),!0;default:return!1}},[]),M=o.useCallback(()=>l(!1),[]);return{isOpen:i,items:x,activeIndex:c,handleKeyDown:E,close:M}}function G(){const{status:a,sendMessage:s,stop:r,sdk:n}=_(),[t,i]=o.useState(""),l=o.useRef(null),c=a==="streaming"||a==="submitted",d=o.useCallback(h=>{i(`/${h.name} `),setTimeout(()=>{const g=l.current;g&&(g.focus(),g.selectionStart=g.selectionEnd=g.value.length)},0)},[]),u=Ye({sdk:n,input:t,onSelect:d}),f=o.useCallback(()=>{const h=t.trim();!h||c||(s(h),i(""),l.current&&(l.current.style.height="auto"))},[t,c,s]),p=o.useCallback(h=>{u.handleKeyDown(h)||h.key==="Enter"&&!h.shiftKey&&(h.preventDefault(),f())},[f,u.handleKeyDown]),x=o.useCallback(h=>{i(h.target.value);const g=h.target;g.style.height="auto",g.style.height=Math.min(g.scrollHeight,120)+"px"},[]);return e.jsxs("div",{className:"sa-composer",children:[u.isOpen&&e.jsx(Ke,{items:u.items,activeIndex:u.activeIndex,onSelect:d}),e.jsx("textarea",{ref:l,className:"sa-composer-input",value:t,onChange:x,onKeyDown:p,placeholder:"输入消息,按 / 选择技能...",rows:1,disabled:a==="submitted"}),c?e.jsx("button",{className:"sa-composer-stop",onClick:r,title:"停止",children:e.jsx(C,{name:"circle-stop",size:16,color:"#fff"})}):e.jsx("button",{className:"sa-composer-send",onClick:f,disabled:!t.trim(),title:"发送",children:e.jsx(C,{name:"arrow-up",size:16,color:"#fff"})})]})}function Ue({artifact:a}){const[s,r]=o.useState(null),[n,t]=o.useState(!0),[i,l]=o.useState(null);return o.useEffect(()=>{let c=!1;t(!0),l(null),r(null);async function d(){try{if(a.type==="pdf"){c||t(!1);return}if(a.type==="html"){const u=await fetch(a.url);if(!u.ok)throw new Error(`HTTP ${u.status}`);const f=await u.text(),p=(await Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs"))).default;c||(r(p.sanitize(f,{FORCE_BODY:!0,ADD_ATTR:["target"]})),t(!1));return}if(a.type==="docx"){const u=await fetch(a.url);if(!u.ok)throw new Error(`HTTP ${u.status}`);const f=await u.arrayBuffer(),x=await(await Promise.resolve().then(()=>require("./mammoth.browser-DiAPPO3x.cjs")).then(g=>g.mammoth_browser)).convertToHtml({arrayBuffer:f}),h=(await Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs"))).default;c||(r(h.sanitize(x.value,{FORCE_BODY:!0,ADD_ATTR:["target"]})),t(!1));return}c||(t(!1),l(`不支持预览「${a.type}」类型`))}catch(u){c||(t(!1),l(u instanceof Error?u.message:"加载失败"))}}return d(),()=>{c=!0}},[a]),n?e.jsx("div",{className:"sa-drawer-preview-loading",children:"加载中"}):i?e.jsxs("div",{className:"sa-drawer-preview-error",children:[i,e.jsx("button",{className:"sa-drawer-btn-sm",onClick:()=>void K(a),children:"下载查看"})]}):a.type==="pdf"?e.jsx("iframe",{src:a.url,title:a.filename,className:"sa-drawer-preview-iframe",sandbox:"allow-scripts"}):s!==null?e.jsx("iframe",{srcDoc:s,title:a.filename,className:"sa-drawer-preview-iframe",sandbox:"allow-same-origin"}):null}function ye({artifacts:a,initialArtifact:s,onClose:r}){const[n,t]=o.useState(s?{mode:"preview",artifact:s}:{mode:"list"}),[i,l]=o.useState(!1),c=o.useRef(null),[d,u]=o.useState(340),f=o.useRef(null);o.useEffect(()=>{s&&(c.current&&(clearTimeout(c.current),c.current=null,l(!1)),t({mode:"preview",artifact:s}))},[s]);const p=o.useCallback(()=>{l(!0),c.current=setTimeout(()=>{c.current=null,r()},240)},[r]);o.useEffect(()=>()=>{var w;c.current&&clearTimeout(c.current),(w=f.current)==null||w.call(f)},[]);const x=o.useCallback(w=>{t({mode:"preview",artifact:w})},[]),h=o.useCallback(()=>{t({mode:"list"})},[]);o.useEffect(()=>{const w=S=>{S.key==="Escape"&&p()};return document.addEventListener("keydown",w),()=>document.removeEventListener("keydown",w)},[p]);const g=o.useCallback(w=>{w.preventDefault();const S=w.clientX,E=d,M=m=>{const I=S-m.clientX;u(Math.max(240,Math.min(window.innerWidth*.7,E+I)))},k=()=>{document.removeEventListener("mousemove",M),document.removeEventListener("mouseup",k),document.body.style.cursor="",document.body.style.userSelect="",f.current=null};document.body.style.cursor="col-resize",document.body.style.userSelect="none",document.addEventListener("mousemove",M),document.addEventListener("mouseup",k),f.current=k},[d]);return e.jsxs("div",{className:`sa-artifact-drawer ${i?"sa-drawer-closing":""}`,style:{width:d},children:[e.jsx("div",{className:"sa-drawer-resize-handle",onMouseDown:g}),e.jsxs("div",{className:"sa-artifact-drawer-header",children:[n.mode==="preview"?e.jsxs(e.Fragment,{children:[e.jsx("button",{className:"sa-drawer-btn",onClick:h,title:"返回列表","aria-label":"返回列表",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M15 18l-6-6 6-6"})})}),e.jsx("span",{className:"sa-artifact-drawer-title",children:n.artifact.filename})]}):e.jsxs(e.Fragment,{children:[e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",style:{flexShrink:0},children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{className:"sa-artifact-drawer-title",children:"会话产物"})]}),e.jsx("div",{style:{flex:1}}),n.mode==="preview"&&e.jsx("button",{className:"sa-drawer-btn",onClick:()=>void K(n.artifact),title:"下载","aria-label":"下载",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})}),e.jsx("button",{className:"sa-drawer-btn",onClick:p,title:"关闭","aria-label":"关闭",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M18 6L6 18M6 6l12 12"})})})]}),e.jsx("div",{className:"sa-artifact-drawer-body",children:n.mode==="list"?a.length===0?e.jsx("div",{className:"sa-drawer-empty",children:"暂无产物"}):e.jsx("ul",{className:"sa-drawer-file-list",role:"list",children:a.map((w,S)=>e.jsxs("li",{className:"sa-drawer-file-card",onClick:()=>x(w),onKeyDown:E=>{(E.key==="Enter"||E.key===" ")&&(E.preventDefault(),x(w))},tabIndex:0,role:"button","aria-label":`预览 ${w.filename}`,children:[e.jsx("div",{className:"sa-drawer-file-icon",children:e.jsx("span",{className:"sa-artifact-chip-type",children:se(w.type)})}),e.jsxs("div",{className:"sa-drawer-file-info",children:[e.jsx("span",{className:"sa-drawer-file-name",children:w.filename}),e.jsx("span",{className:"sa-drawer-file-size",children:te(w.size)})]}),e.jsx("button",{className:"sa-drawer-btn",title:"下载","aria-label":`下载 ${w.filename}`,onClick:E=>{E.stopPropagation(),K(w)},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})})]},w.id||`artifact-${S}`))}):e.jsx("div",{className:"sa-drawer-view-enter",children:e.jsx(Ue,{artifact:n.artifact})})})]})}function Ve({isOpen:a,onClose:s,title:r,slots:n,welcomeMessage:t,suggestedPrompts:i,avatar:l,artifactPreview:c}){const{status:d,sendMessage:u,stop:f,toggleThreadList:p,toggleArtifactDrawer:x,conversations:h,activeConversationId:g,switchConversation:w,newConversation:S,deleteConversation:E,renameConversation:M,isThreadListOpen:k,isArtifactDrawerOpen:m,allArtifacts:I,activePreviewArtifact:y}=_();if(!a)return null;const b=n.Header,v=n.Composer,j=n.ThreadList;return e.jsxs("div",{className:"sa-panel",children:[b?e.jsx(b,{title:r,onClose:s,onToggleThreadList:p}):e.jsx(le,{title:r,onClose:s,onToggleThreadList:p,artifactCount:c?I.length:0,onToggleArtifactDrawer:c?x:void 0}),j&&k?e.jsx(j,{conversations:h,activeSessionId:g,onSwitch:w,onNew:S,onDelete:E,onRename:M}):e.jsx(de,{}),e.jsxs("div",{className:"sa-panel-content",children:[e.jsx(X,{slots:n,welcomeMessage:t,suggestedPrompts:i,avatar:l,artifactPreview:c}),c&&m&&e.jsx(ye,{artifacts:I,initialArtifact:y,onClose:x})]}),v?e.jsx(v,{status:d,onSend:u,onStop:f}):e.jsx(G,{})]})}function Xe({conversations:a,activeSessionId:s,onSwitch:r,onNew:n,onDelete:t,onRename:i,isCollapsed:l,onToggleCollapse:c,hasMore:d,onLoadMore:u}){const[f,p]=o.useState(null),[x,h]=o.useState(null),[g,w]=o.useState(null),[S,E]=o.useState(""),M=o.useCallback((v,j)=>{j.stopPropagation(),x===v?(t(v),h(null)):(h(v),setTimeout(()=>h(null),3e3))},[x,t]),k=o.useCallback((v,j,R)=>{R.stopPropagation(),w(v),E(j??"")},[]),m=o.useCallback(v=>{S.trim()&&i(v,S.trim()),w(null)},[S,i]),I=o.useRef(null),y=o.useCallback(()=>{const v=I.current;!v||!d||!u||v.scrollHeight-v.scrollTop-v.clientHeight<50&&u()},[d,u]),b=(()=>{const v=Date.now(),j=[],R=[],$=[];for(const D of a){const A=v-new Date(D.updateTime).getTime();A<864e5?j.push(D):A<6048e5?R.push(D):$.push(D)}const z=[];return j.length&&z.push({label:"今天",items:j}),R.length&&z.push({label:"最近 7 天",items:R}),$.length&&z.push({label:"更早",items:$}),z})();return l?e.jsxs("div",{className:"sa-fp-sidebar sa-fp-sidebar-collapsed",children:[e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:c,title:"展开",children:e.jsx(C,{name:"panel-left",size:18})}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:n,title:"新会话",style:{marginTop:4},children:e.jsx(C,{name:"plus",size:18})})]}):e.jsxs("div",{className:"sa-fp-sidebar",children:[e.jsxs("div",{className:"sa-fp-sidebar-header",children:[e.jsxs("button",{className:"sa-fp-sidebar-new",onClick:n,children:[e.jsx(C,{name:"plus",size:15})," ",e.jsx("span",{children:"新会话"})]}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:c,title:"收起侧边栏",children:e.jsx(C,{name:"panel-left-close",size:18})})]}),e.jsxs("div",{className:"sa-fp-sidebar-list",ref:I,onScroll:y,children:[a.length===0&&e.jsx("div",{className:"sa-fp-sidebar-empty",children:"开始你的第一个对话"}),b.map(v=>e.jsxs("div",{children:[e.jsx("div",{className:"sa-fp-sidebar-group",children:v.label}),v.items.map(j=>{const R=j.sessionId===s,$=f===j.sessionId,z=x===j.sessionId;return e.jsxs("div",{className:`sa-fp-sidebar-item ${R?"active":""}`,onClick:()=>{g!==j.sessionId&&r(j.sessionId)},onMouseEnter:()=>p(j.sessionId),onMouseLeave:()=>p(null),children:[g===j.sessionId?e.jsx("input",{className:"sa-fp-sidebar-item-edit",value:S,onChange:D=>E(D.target.value),onBlur:()=>m(j.sessionId),onKeyDown:D=>{D.key==="Enter"&&m(j.sessionId),D.key==="Escape"&&w(null)},onClick:D=>D.stopPropagation(),autoFocus:!0}):e.jsx("span",{className:"sa-fp-sidebar-item-title",children:j.title||"新会话"}),e.jsxs("div",{className:`sa-fp-sidebar-item-actions ${$&&g!==j.sessionId?"visible":""}`,children:[e.jsx("button",{className:"sa-fp-sidebar-item-action",onClick:D=>k(j.sessionId,j.title,D),title:"重命名",children:e.jsx(C,{name:"pen",size:14})}),e.jsx("button",{className:`sa-fp-sidebar-item-action ${z?"danger":""}`,onClick:D=>M(j.sessionId,D),title:z?"确认删除":"删除",children:e.jsx(C,{name:z?"x":"trash2",size:14})})]})]},j.sessionId)})]},v.label)),d&&e.jsx("div",{className:"sa-thread-list-loading",children:"加载更多..."})]})]})}function ke({title:a,slots:s,welcomeMessage:r,suggestedPrompts:n,sidebarDefaultOpen:t=!0,onClose:i,avatar:l,artifactPreview:c}){const{status:d,sendMessage:u,stop:f,conversations:p,activeConversationId:x,switchConversation:h,newConversation:g,deleteConversation:w,renameConversation:S,toggleArtifactDrawer:E,isArtifactDrawerOpen:M,allArtifacts:k,activePreviewArtifact:m,hasMore:I,loadMoreConversations:y}=_(),[b,v]=o.useState(t),j=o.useCallback(()=>v(z=>!z),[]),R=s.ThreadList,$=s.Composer;return e.jsxs("div",{className:"sa-fullpage",children:[R?e.jsx("div",{className:`sa-fp-sidebar-wrapper ${b?"":"sa-fp-sidebar-wrapper-collapsed"}`,children:e.jsx(R,{conversations:p,activeSessionId:x,onSwitch:h,onNew:g,onDelete:w,onRename:S,hasMore:I,onLoadMore:y})}):e.jsx(Xe,{conversations:p,activeSessionId:x,onSwitch:h,onNew:g,onDelete:w,onRename:S,isCollapsed:!b,onToggleCollapse:j,hasMore:I,onLoadMore:y}),e.jsxs("div",{className:"sa-fp-main",children:[e.jsxs("div",{className:"sa-fp-header",children:[!b&&!R&&e.jsx("button",{className:"sa-fp-header-btn",onClick:j,title:"展开侧边栏",children:e.jsx(C,{name:"panel-left",size:18})}),e.jsx("div",{className:"sa-fp-header-title",children:a}),e.jsxs("div",{className:"sa-fp-header-actions",children:[c&&k.length>0&&e.jsxs("button",{className:"sa-fp-header-btn sa-header-btn-artifact",onClick:E,title:"会话产物",children:[e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{children:"产物"})]}),i&&e.jsx("button",{className:"sa-fp-header-btn",onClick:i,title:"关闭",children:e.jsx(C,{name:"x",size:18})})]})]}),e.jsxs("div",{className:"sa-fp-content-row",children:[e.jsx(X,{slots:s,welcomeMessage:r,suggestedPrompts:n,avatar:l,artifactPreview:c}),c&&M&&e.jsx(ye,{artifacts:k,initialArtifact:m,onClose:E})]}),$?e.jsx($,{status:d,onSend:u,onStop:f}):e.jsx(G,{})]})]})}function je(a){const{sdk:s,mode:r="floating",slots:n={},hooks:t={},welcomeMessage:i,suggestedPrompts:l,title:c="AI Assistant",initialOpen:d=!1,onOpenChange:u,sidebarDefaultOpen:f,avatar:p,artifactPreview:x}=a,[h,g]=o.useState(d);o.useEffect(()=>{g(d)},[d]);const w=o.useCallback(()=>{var k,m;const M=!h;g(M),u==null||u(M),M?(k=t.onOpen)==null||k.call(t):(m=t.onClose)==null||m.call(t)},[h,t,u]),S=o.useCallback(()=>{var M;g(!1),u==null||u(!1),(M=t.onClose)==null||M.call(t)},[t,u]),E=n.Trigger??oe;return r==="fullpage"?h?e.jsx(V,{sdk:s,hooks:t,children:e.jsx(ke,{title:c,slots:n,welcomeMessage:i,suggestedPrompts:l,sidebarDefaultOpen:f,onClose:S,avatar:p,artifactPreview:x})}):null:e.jsxs(V,{sdk:s,hooks:t,children:[e.jsx(E,{isOpen:h,onClick:w}),e.jsx(Ve,{isOpen:h,onClose:S,title:c,slots:n,welcomeMessage:i,suggestedPrompts:l,avatar:p,artifactPreview:x})]})}const Ce=`
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("react"),Me=require("react-dom/client"),e=require("react/jsx-runtime");function te(t){const{sdk:a,sessionId:r,onSessionCreated:n,onStreamStart:s,onStreamEnd:i,onError:l,onMessageSend:c}=t,[d,p]=o.useState([]),[u,m]=o.useState("ready"),[w,f]=o.useState(null),x=o.useRef(null),v=o.useRef(!1),z=o.useRef(null),S=o.useRef(r),E=o.useRef(!0);S.current=r,E.current=d.length===0;const g=o.useCallback(j=>{if(v.current)return;v.current=!0,c==null||c(j);const D={id:`user_${Date.now()}`,role:"user",parts:[{type:"text",content:j}],timestamp:Date.now()},y=`assistant_${Date.now()}`,h={id:y,role:"assistant",parts:[],timestamp:Date.now()},$=E.current;p(k=>[...k,D,h]),m("submitted"),f(null);const M=k=>{let R="",b="",A=[],P=!1;const H=()=>{const T=[...A];if(R){const O=T.findIndex(B=>B.type==="thinking");O>=0?T[O]={type:"thinking",content:R}:T.unshift({type:"thinking",content:R})}if(b){const O=T[T.length-1];(O==null?void 0:O.type)==="text"?T[T.length-1]={type:"text",content:b}:T.push({type:"text",content:b})}p(O=>O.map(B=>B.id===y?{...B,parts:T}:B))};x.current=a.chat({sessionId:k,message:j,stream:!0,onMessage:T=>{switch(P||(P=!0,m("streaming")),T.type){case"thinking":R+=T.content;break;case"ai":b+=T.content;break;case"tool_call":b&&(A.push({type:"text",content:b}),b=""),A.push({type:"tool_call",toolName:T.toolName,toolCallId:T.toolCallId,args:T.args??"{}"});break;case"tool_result":b&&(A.push({type:"text",content:b}),b=""),A.push({type:"tool_result",toolName:T.toolName??"",toolCallId:T.toolCallId,content:T.content,artifacts:T.artifacts,renderMode:T.renderMode,html:T.html});break;case"interrupt":b&&(A.push({type:"text",content:b}),b=""),T.interrupt&&A.push({type:"interrupt",interrupt:T.interrupt,resolved:!1});break;case"stop":T.content&&(b=T.content),H();return}H()},onDone:T=>{(b||A.length>0||R)&&H(),T.sessionId&&T.sessionId!==S.current&&(n==null||n(T.sessionId));const O=()=>{z.current&&(clearTimeout(z.current),z.current=null),i==null||i(T.sessionId||k),m("ready"),v.current=!1,x.current=null};if($){const B=j.slice(0,30),Te=T.sessionId||k;a.renameConversation(Te,B).catch(()=>{}).finally(O)}else O()},onError:T=>{A.push({type:"error",content:T.message}),H(),z.current&&(clearTimeout(z.current),z.current=null),f(T),m("error"),l==null||l(T),v.current=!1,x.current=null}}),s==null||s(k)};S.current?M(S.current):a.createSession().then(k=>{S.current=k,n==null||n(k),M(k)}).catch(k=>{f(k instanceof Error?k:new Error(String(k))),m("error"),l==null||l(k instanceof Error?k:new Error(String(k))),v.current=!1})},[a,n,s,i,l,c]),C=o.useCallback(()=>{const j=S.current;j&&(z.current&&clearTimeout(z.current),z.current=setTimeout(()=>{var D;z.current=null,(D=x.current)==null||D.abort(),x.current=null,v.current=!1,m("ready")},8e3),a.stopGeneration(j).catch(()=>{}))},[a]),I=o.useCallback(()=>{if(v.current)return;const j=d.findLastIndex(y=>y.role==="user");if(j<0)return;const D=d[j].parts.filter(y=>y.type==="text").map(y=>y.content).join("");p(y=>y.slice(0,j)),g(D)},[d,g]),L=o.useCallback((j,D)=>{p(b=>b.map(A=>({...A,parts:A.parts.map(P=>P.type==="interrupt"&&P.interrupt.interruptId===j?{...P,resolved:!0,response:D}:P)})));const y=S.current;if(!y)return;let h=0;p(b=>b.length>0&&b[b.length-1].role==="assistant"?(h=b[b.length-1].parts.length,b):(h=0,[...b,{id:`assistant_${Date.now()}`,role:"assistant",parts:[],timestamp:Date.now()}])),m("streaming");let $="",M="",k=[];const R=()=>{const b=[...k];if($){const A=b.findIndex(P=>P.type==="thinking");A>=0?b[A]={type:"thinking",content:$}:b.unshift({type:"thinking",content:$})}if(M){const A=b[b.length-1];(A==null?void 0:A.type)==="text"?b[b.length-1]={type:"text",content:M}:b.push({type:"text",content:M})}p(A=>{const P=A.findLastIndex(H=>H.role==="assistant");return P<0?A:A.map((H,T)=>T===P?{...H,parts:[...H.parts.slice(0,h),...b]}:H)})};a.respondInterrupt(j,y,D,b=>{switch(b.type){case"thinking":$+=b.content;break;case"ai":M+=b.content;break;case"tool_call":M&&(k.push({type:"text",content:M}),M=""),k.push({type:"tool_call",toolName:b.toolName,toolCallId:b.toolCallId,args:b.args??"{}"});break;case"tool_result":M&&(k.push({type:"text",content:M}),M=""),k.push({type:"tool_result",toolName:b.toolName??"",toolCallId:b.toolCallId,content:b.content,artifacts:b.artifacts,renderMode:b.renderMode,html:b.html});break;case"interrupt":M&&(k.push({type:"text",content:M}),M=""),b.interrupt&&k.push({type:"interrupt",interrupt:b.interrupt,resolved:!1});break;case"done":i==null||i(y),m("ready");break}R()},b=>{k.push({type:"error",content:b.message}),R(),f(b),m("error"),l==null||l(b)}).catch(b=>{l==null||l(b instanceof Error?b:new Error(String(b))),m("error")})},[a,l,i]);return{messages:d,status:u,error:w,sendMessage:g,stop:C,regenerate:I,setMessages:p,respondToInterrupt:L}}const ae="sa_active_conversation",Y=20;function G(t){try{return localStorage.getItem(`${ae}_${t}`)}catch{return null}}function $e(t,a){try{const r=`${ae}_${t}`;a?localStorage.setItem(r,a):localStorage.removeItem(r)}catch{}}function se(t){const{sdk:a,onConversationChange:r}=t,n=a.botId,[s,i]=o.useState([]),[l,c]=o.useState(()=>G(n)),[d,p]=o.useState(!1),[u,m]=o.useState(!0),w=o.useRef(!0),f=o.useRef(1),x=o.useRef(!1);o.useEffect(()=>()=>{w.current=!1},[]);const v=o.useCallback(j=>{c(j),$e(n,j),j&&(r==null||r(j))},[n,r]),z=o.useCallback(async()=>{if(!x.current){x.current=!0,p(!0);try{f.current=1;const j=await a.listConversations({page:1,size:Y});if(!w.current)return;i(j.items),m(j.items.length<j.total);const D=G(n);D&&!j.items.find(y=>y.sessionId===D)&&v(null)}catch{}finally{x.current=!1,w.current&&p(!1)}}},[a,v]),S=o.useCallback(async()=>{if(!(x.current||!u)){x.current=!0,p(!0);try{const j=f.current+1,D=await a.listConversations({page:j,size:Y});if(!w.current)return;f.current=j,i(h=>{const $=new Set(h.map(k=>k.sessionId)),M=D.items.filter(k=>!$.has(k.sessionId));return[...h,...M]});const y=f.current*Y;m(y<D.total)}catch{}finally{x.current=!1,w.current&&p(!1)}}},[a,u]),E=o.useCallback(async j=>(v(j),await a.getMessages(j)),[a,v]),g=o.useCallback(async()=>{try{const j=await a.createSession(),D=new Date().toISOString();i(y=>[{sessionId:j,botId:0,title:"新对话",createTime:D,updateTime:D},...y]),v(j)}catch{v(null)}},[a,v]),C=o.useCallback(async j=>{i(D=>D.filter(y=>y.sessionId!==j)),l===j&&v(null);try{await a.deleteConversation(j)}catch{}},[a,l,v]),I=o.useCallback(async(j,D)=>{await a.renameConversation(j,D),i(y=>y.map(h=>h.sessionId===j?{...h,title:D}:h))},[a]),{enabled:L=!0}=t;return o.useEffect(()=>{L&&z()},[L,z]),{conversations:s,activeConversationId:l,loading:d,hasMore:u,loadConversations:z,loadMore:S,switchConversation:E,newConversation:g,deleteConversation:C,renameConversation:I,setActiveConversationId:v}}function re(t){return!t&&t!==0?"":t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:`${(t/1024/1024).toFixed(1)} MB`}function ne(t){return t==="pdf"?"PDF":t==="docx"?"DOC":t==="html"?"HTM":t.slice(0,3).toUpperCase()||"FILE"}function ie(t){if(!t)return[];const a=[];for(const r of t)r.type==="file"&&r.data&&a.push({id:r.data.id??"",type:r.data.fileType??r.data.type??"",filename:r.data.name??r.data.filename??"",url:r.data.url??"",mime:r.data.mime,size:r.data.size,summary:r.data.summary});return a}async function K(t){try{const a=await fetch(t.url);if(!a.ok)throw new Error(`HTTP ${a.status}`);const r=await a.blob(),n=URL.createObjectURL(r),s=document.createElement("a");s.href=n,s.download=t.filename,document.body.appendChild(s),s.click(),s.remove(),setTimeout(()=>URL.revokeObjectURL(n),4e3)}catch{const a=document.createElement("a");a.href=t.url,a.download=t.filename,a.target="_blank",document.body.appendChild(a),a.click(),a.remove()}}const oe=o.createContext(null);function _(){const t=o.useContext(oe);if(!t)throw new Error("useChatContext must be used within ChatProvider");return t}function U({sdk:t,hooks:a,children:r}){const[n,s]=o.useState(!1),[i,l]=o.useState(!1),[c,d]=o.useState(null),[p,u]=o.useState(!1),[m,w]=o.useState(!1);o.useEffect(()=>{t.getToken().then(()=>{u(!0)}).catch(()=>{u(!0)})},[t]);const f=se({sdk:t,onConversationChange:a==null?void 0:a.onConversationChange,enabled:p}),x=te({sdk:t,sessionId:f.activeConversationId,onSessionCreated:h=>{f.setActiveConversationId(h)},onStreamStart:a==null?void 0:a.onStreamStart,onStreamEnd:h=>{var $;($=a==null?void 0:a.onStreamEnd)==null||$.call(a,h),f.loadConversations()},onError:a==null?void 0:a.onError,onMessageSend:a==null?void 0:a.onMessageSend}),v=o.useCallback(async h=>{w(!0);try{const $=await f.switchConversation(h);x.setMessages($),s(!1)}finally{w(!1)}},[f,x]),z=o.useCallback(async()=>{x.setMessages([]),s(!1),await f.newConversation()},[f,x]),S=o.useCallback(async h=>{f.activeConversationId===h&&x.setMessages([]),await f.deleteConversation(h)},[f,x]);o.useEffect(()=>{f.activeConversationId&&x.messages.length===0&&(w(!0),t.getMessages(f.activeConversationId).then(h=>{x.setMessages(h)}).catch(()=>{}).finally(()=>w(!1)))},[]);const E=o.useCallback(()=>{s(h=>!h)},[]),g=o.useCallback(()=>{l(h=>!h),d(h=>null)},[]),C=o.useCallback(h=>{d(h),l(!0)},[]),I=o.useMemo(()=>{const h=new Set,$=[];for(const M of x.messages)for(const k of M.parts)if(k.type==="tool_result"&&k.artifacts)for(const R of ie(k.artifacts)){const b=R.id||`${R.url}:${R.filename}`;h.has(b)||(h.add(b),$.push(R))}return $},[x.messages]),L=o.useCallback(async()=>{const h=f.activeConversationId;if(!h)return null;try{const $=await t.getMessages(h);x.setMessages($);const M=$.findLast(k=>k.role==="assistant");return(M==null?void 0:M.id)??null}catch{return null}},[t,f.activeConversationId,x]),j=o.useCallback((h,$,M)=>{if(/^\d+$/.test(h)){t.feedback(h,$,M).catch(()=>{});return}L().then(k=>{k&&t.feedback(k,$,M).catch(()=>{})})},[t,L]),D=o.useCallback(h=>{if(/^\d+$/.test(h)){t.cancelFeedback(h).catch(()=>{});return}L().then($=>{$&&t.cancelFeedback($).catch(()=>{})})},[t,L]),y=o.useMemo(()=>({sdk:t,status:x.status,error:x.error,messages:x.messages,conversations:f.conversations,activeConversationId:f.activeConversationId,isThreadListOpen:n,isArtifactDrawerOpen:i,allArtifacts:I,activePreviewArtifact:c,hasMore:f.hasMore,conversationsLoading:f.loading,messagesLoading:m,sendMessage:x.sendMessage,stop:x.stop,regenerate:x.regenerate,switchConversation:v,newConversation:z,deleteConversation:S,renameConversation:f.renameConversation,loadMoreConversations:f.loadMore,toggleThreadList:E,toggleArtifactDrawer:g,openArtifactPreview:C,feedback:j,cancelFeedback:D,respondToInterrupt:x.respondToInterrupt}),[t,x.status,x.error,x.messages,x.sendMessage,x.stop,x.regenerate,x.respondToInterrupt,f.conversations,f.activeConversationId,f.renameConversation,f.hasMore,f.loadMore,f.loading,n,i,I,c,m,v,z,S,E,g,C,j,D]);return e.jsx(oe.Provider,{value:y,children:r})}const Ee="https://at.alicdn.com/t/c/font_5210396_pffyg5g21kn.js";let J=!1;function le(t=Ee){if(J||typeof document>"u")return;J=!0;const a=t.split("?")[0].split("/").pop();if(a&&document.querySelector(`script[src*="${a}"]`))return;const r=document.createElement("script");r.src=t,document.head.appendChild(r)}le();function N({name:t,size:a=16,color:r="currentColor",className:n,style:s}){return e.jsx("svg",{className:n,style:{width:a,height:a,fill:r,verticalAlign:"middle",...s},"aria-hidden":"true",children:e.jsx("use",{href:`#icon-${t}`})})}function ce({isOpen:t,onClick:a}){return e.jsx("button",{className:`sa-trigger ${t?"sa-trigger-open":""}`,onClick:a,children:t?e.jsx(N,{name:"x",size:24,color:"#fff"}):e.jsx(N,{name:"message-square",size:24,color:"#fff"})})}function de({title:t,onClose:a,onToggleThreadList:r,showClose:n=!0,artifactCount:s=0,onToggleArtifactDrawer:i}){return e.jsxs("div",{className:"sa-header",children:[e.jsxs("div",{className:"sa-header-left",children:[e.jsx("div",{className:"sa-header-avatar",children:"A"}),e.jsxs("div",{className:"sa-header-info",children:[e.jsx("div",{className:"sa-header-title",children:t}),e.jsx("div",{className:"sa-header-status",children:"● Online"})]})]}),e.jsxs("div",{className:"sa-header-actions",children:[s>0&&i&&e.jsxs("button",{className:"sa-header-btn sa-header-btn-artifact",onClick:i,title:"会话产物",children:[e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{children:"产物"})]}),e.jsx("button",{className:"sa-header-btn",onClick:r,title:"会话列表",children:e.jsx(N,{name:"menu",size:16})}),n&&e.jsx("button",{className:"sa-header-btn",onClick:a,title:"关闭",children:e.jsx(N,{name:"x",size:16})})]})]})}const q=t=>String(t).padStart(2,"0");function pe(t){if(!t)return"";const a=new Date(t);return isNaN(a.getTime())?"":`${a.getFullYear()}-${q(a.getMonth()+1)}-${q(a.getDate())} ${q(a.getHours())}:${q(a.getMinutes())}:${q(a.getSeconds())}`}function De(t){const a=new Date,r=new Date(a.getFullYear(),a.getMonth(),a.getDate()).getTime(),n=r-6*864e5,s=[],i=[],l=[];for(const d of t){const p=new Date(d.updateTime).getTime();p>=r?s.push(d):p>=n?i.push(d):l.push(d)}const c=[];return s.length&&c.push({label:"今天",items:s}),i.length&&c.push({label:"最近 7 天",items:i}),l.length&&c.push({label:"更早",items:l}),c}function ue(){const{conversations:t,activeConversationId:a,switchConversation:r,newConversation:n,deleteConversation:s,renameConversation:i,isThreadListOpen:l,toggleThreadList:c,hasMore:d,loadMoreConversations:p,conversationsLoading:u}=_(),[m,w]=o.useState(null),[f,x]=o.useState(""),v=o.useRef(null),z=o.useCallback((C,I)=>{w(C),x(I??"")},[]),S=o.useCallback(C=>{f.trim()&&i(C,f.trim()),w(null)},[f,i]),E=o.useCallback(()=>{const C=v.current;!C||!d||C.scrollHeight-C.scrollTop-C.clientHeight<50&&p()},[d,p]),g=o.useMemo(()=>De(t),[t]);return l?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"sa-thread-list-overlay",onClick:c}),e.jsxs("div",{className:"sa-thread-list",children:[e.jsxs("div",{className:"sa-thread-list-header",children:[e.jsx("span",{className:"sa-thread-list-title",children:"会话记录"}),e.jsxs("button",{className:"sa-thread-list-new",onClick:n,children:[e.jsx(N,{name:"plus",size:14})," 新会话"]})]}),e.jsxs("div",{className:"sa-thread-list-items",ref:v,onScroll:E,children:[u&&t.length===0&&e.jsx("div",{className:"sa-thread-list-loading",children:"加载中..."}),!u&&t.length===0&&e.jsx("div",{className:"sa-thread-list-empty",children:"暂无会话"}),g.map(C=>e.jsxs("div",{className:"sa-thread-group",children:[e.jsx("div",{className:"sa-thread-group-label",children:C.label}),C.items.map(I=>e.jsxs("div",{className:`sa-thread-item ${I.sessionId===a?"active":""}`,onClick:()=>{m!==I.sessionId&&r(I.sessionId)},children:[m===I.sessionId?e.jsx("input",{className:"sa-thread-item-edit",value:f,onChange:L=>x(L.target.value),onBlur:()=>S(I.sessionId),onKeyDown:L=>{L.key==="Enter"&&S(I.sessionId),L.key==="Escape"&&w(null)},onClick:L=>L.stopPropagation(),autoFocus:!0}):e.jsxs("div",{className:"sa-thread-item-info",children:[e.jsx("span",{className:"sa-thread-item-title",children:I.title||"新会话"}),e.jsx("span",{className:"sa-thread-item-time",children:pe(I.updateTime)})]}),e.jsxs("div",{className:"sa-thread-item-actions",children:[e.jsx("button",{className:"sa-thread-item-action-btn",onClick:L=>{L.stopPropagation(),z(I.sessionId,I.title)},title:"重命名",children:e.jsx(N,{name:"pen",size:13})}),e.jsx("button",{className:"sa-thread-item-action-btn",onClick:L=>{L.stopPropagation(),s(I.sessionId)},title:"删除",children:e.jsx(N,{name:"trash2",size:13})})]})]},I.sessionId))]},C.label)),u&&t.length>0&&e.jsx("div",{className:"sa-thread-list-loading",children:d?"加载更多...":"加载中..."}),!u&&d&&e.jsx("div",{className:"sa-thread-list-hint",children:"向下滚动加载更多"})]})]})]}):null}function fe(t){const a=o.useRef(null),r=o.useRef(!1),n=o.useRef(0),s=o.useCallback(()=>{const l=a.current;if(!l)return;const{scrollTop:c,scrollHeight:d,clientHeight:p}=l,u=d-c-p<40;c<n.current&&!u&&(r.current=!0),u&&(r.current=!1),n.current=c},[]);o.useEffect(()=>{const l=a.current;if(l)return l.addEventListener("scroll",s,{passive:!0}),()=>l.removeEventListener("scroll",s)},[s]),o.useEffect(()=>{if(r.current)return;const l=a.current;l&&requestAnimationFrame(()=>{l.scrollTop=l.scrollHeight})},t);const i=o.useCallback(()=>{r.current=!1;const l=a.current;l&&(l.scrollTop=l.scrollHeight)},[]);return{containerRef:a,scrollToBottom:i}}function F(t){return t.value++}function W(t,a){const r=[],n=[],s=()=>{n.length>0&&(r.push(n.join("")),n.length=0)};let i=0;for(;i<t.length;){const l=t[i];if(l==="`"){const c=t.indexOf("`",i+1);if(c!==-1){s(),r.push(e.jsx("code",{className:"sa-inline-code",children:t.slice(i+1,c)},F(a))),i=c+1;continue}}else if(l==="*"&&t[i+1]==="*"){const c=t.indexOf("**",i+2);if(c!==-1){s(),r.push(e.jsx("strong",{children:W(t.slice(i+2,c),a)},F(a))),i=c+2;continue}}else if(l==="*"){const c=t.indexOf("*",i+1);if(c!==-1){s(),r.push(e.jsx("em",{children:W(t.slice(i+1,c),a)},F(a))),i=c+1;continue}}else if(l==="["){const c=t.indexOf("]",i+1);if(c!==-1&&t[c+1]==="("){const d=t.indexOf(")",c+2);if(d!==-1){const p=t.slice(i+1,c),u=t.slice(c+2,d);s(),r.push(e.jsx("a",{href:u,target:"_blank",rel:"noreferrer",children:W(p,a)},F(a))),i=d+1;continue}}}n.push(l),i++}return s(),r}function Re(t,a){const r=[],n=t.split(`
|
|
2
|
+
`);let s=0;const i=d=>d.trim()==="",l=d=>/^#{1,6}\s+\S/.test(d.trim()),c=d=>/^(?:[-*+]|\d+[.)])\s+\S/.test(d.trim());for(;s<n.length;){const d=n[s],p=d.trim();if(i(d)){s++;continue}if(l(d)){const m=p.match(/^(#{1,6})\s+(.*)$/);if(m){const f=`h${Math.min(m[1].length,6)}`;r.push(o.createElement(f,{key:F(a)},W(m[2],a)))}s++;continue}if(c(d)){const m=/^\d+[.)]/.test(p),w=[];for(;s<n.length&&c(n[s]);){const f=n[s].trim().match(/^(?:[-*+]|\d+[.)])\s+(.*)$/);f&&w.push(e.jsx("li",{children:W(f[1],a)},F(a))),s++}r.push(m?e.jsx("ol",{className:"sa-list",children:w},F(a)):e.jsx("ul",{className:"sa-list",children:w},F(a)));continue}const u=[];for(;s<n.length&&!i(n[s])&&!l(n[s])&&!c(n[s]);)u.push(n[s]),s++;r.push(e.jsx("p",{className:"sa-text-paragraph",children:W(u.join(" "),a)},F(a)))}return r}function Le(t){const a={value:0},r=[],n=t.split("```");for(let s=0;s<n.length;s++)if(s%2===0)r.push(...Re(n[s],a));else if(n[s].trim()!==""){let i=n[s].replace(/^\r?\n/,"");i=i.replace(/^[a-zA-Z0-9_+#.-]+\r?\n/,""),i=i.replace(/\r?\n$/,""),r.push(e.jsx("pre",{className:"sa-code-block",children:e.jsx("code",{children:i})},F(a)))}return r}function xe({content:t}){return e.jsx("div",{className:"sa-text-part",children:Le(t)})}function me({content:t}){const[a,r]=o.useState(!1);return e.jsxs("div",{className:"sa-thinking-part",onClick:()=>r(!a),children:[e.jsxs("div",{className:"sa-thinking-header",children:[e.jsx("span",{className:"sa-thinking-icon",children:e.jsx(N,{name:"brain",size:14})}),e.jsx("span",{className:"sa-thinking-label",children:"思考过程"}),e.jsx("span",{className:`sa-thinking-arrow ${a?"expanded":""}`,children:e.jsx(N,{name:"chevron-right",size:12})})]}),a&&e.jsx("div",{className:"sa-thinking-content",children:t})]})}function he({toolName:t,toolCallId:a,args:r}){const[n,s]=o.useState(!1),[i,l]=o.useState(!1);let c=r;try{c=JSON.stringify(JSON.parse(r),null,2)}catch{}const d=o.useCallback(()=>{var p;(p=navigator.clipboard)==null||p.writeText(c).then(()=>{l(!0),setTimeout(()=>l(!1),2e3)}).catch(()=>{})},[c]);return e.jsxs("div",{className:"sa-tool-call-part",children:[e.jsxs("div",{className:"sa-tool-call-header",onClick:()=>s(!n),children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(N,{name:"zap",size:14})}),e.jsxs("span",{className:"sa-tool-name",children:["调用工具: ",t]}),e.jsx("span",{className:`sa-thinking-arrow ${n?"expanded":""}`,children:e.jsx(N,{name:"chevron-right",size:12})})]}),n&&e.jsxs(e.Fragment,{children:[e.jsx("pre",{className:"sa-tool-args",children:c}),e.jsx("div",{className:"sa-tool-result-actions",children:e.jsxs("button",{className:"sa-action-btn",onClick:d,title:"复制",children:[e.jsx(N,{name:i?"circle-check":"copy",size:14})," ",i?"已复制":"复制"]})})]})]})}function Z({text:t}){const[a,r]=o.useState(!1),n=()=>{var s;(s=navigator.clipboard)==null||s.writeText(t).then(()=>{r(!0),setTimeout(()=>r(!1),2e3)}).catch(()=>{})};return e.jsxs("button",{className:"sa-action-btn",onClick:n,title:"复制",children:[e.jsx(N,{name:a?"circle-check":"copy",size:14})," ",a?"已复制":"复制"]})}function Ae({html:t}){const[a,r]=o.useState(null);return o.useEffect(()=>{let n=!1;return Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs")).then(s=>{n||r(s.default.sanitize(t,{FORCE_BODY:!0,ADD_ATTR:["target"]}))}),()=>{n=!0}},[t]),a===null?null:e.jsx("div",{className:"sa-tool-result-html-scroll",children:e.jsx("div",{className:"sa-tool-result-html sa-text-part",dangerouslySetInnerHTML:{__html:a}})})}function ge({toolName:t,content:a,artifacts:r,renderMode:n,html:s,artifactPreview:i}){const{openArtifactPreview:l}=_(),c=ie(r);return e.jsxs("div",{className:"sa-tool-result-part",children:[e.jsxs("div",{className:"sa-tool-result-header",children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(N,{name:"circle-check",size:14,color:"#10b981"})}),e.jsx("span",{className:"sa-tool-result-label",children:t||"工具返回"})]}),n==="html"&&s?e.jsxs(e.Fragment,{children:[e.jsx(Ae,{html:s}),e.jsx("div",{className:"sa-tool-result-actions",children:e.jsx(Z,{text:s})})]}):a?e.jsxs(e.Fragment,{children:[e.jsx("pre",{className:"sa-tool-result-content",children:a}),e.jsx("div",{className:"sa-tool-result-actions",children:e.jsx(Z,{text:a})})]}):e.jsx("pre",{className:"sa-tool-result-content",children:"—"}),c.length>0&&e.jsx("ul",{className:"sa-artifact-file-list",role:"list",children:c.map((d,p)=>e.jsxs("li",{className:"sa-artifact-file-card",onClick:i?()=>l(d):void 0,onKeyDown:i?u=>{(u.key==="Enter"||u.key===" ")&&(u.preventDefault(),l(d))}:void 0,tabIndex:i?0:void 0,role:i?"button":void 0,"aria-label":i?`预览 ${d.filename}`:void 0,style:i?{cursor:"pointer"}:void 0,children:[e.jsx("div",{className:"sa-artifact-file-icon",children:e.jsx("span",{className:"sa-artifact-chip-type",children:ne(d.type)})}),e.jsxs("div",{className:"sa-artifact-file-info",children:[e.jsx("span",{className:"sa-artifact-file-name",children:d.filename}),d.size!=null&&e.jsx("span",{className:"sa-artifact-file-size",children:re(d.size)})]}),i&&e.jsx("button",{className:"sa-artifact-file-download",title:"下载","aria-label":`下载 ${d.filename}`,onClick:u=>{u.stopPropagation(),K(d)},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})})]},d.id||`art-${p}`))})]})}function be({content:t,onRetry:a}){return e.jsxs("div",{className:"sa-error-part",children:[e.jsxs("div",{className:"sa-error-content",children:[e.jsx("span",{className:"sa-error-icon",children:e.jsx(N,{name:"triangle-alert",size:14})}),e.jsx("span",{children:t})]}),a&&e.jsx("button",{className:"sa-error-retry",onClick:a,children:"重试"})]})}function Pe({interrupt:t,onRespond:a,disabled:r,answeredAction:n}){var c,d;const s=t.options,i=(typeof(s==null?void 0:s[0])=="string"?s[0]:(c=s==null?void 0:s[0])==null?void 0:c.label)||"确认",l=(typeof(s==null?void 0:s[1])=="string"?s[1]:(d=s==null?void 0:s[1])==null?void 0:d.label)||"取消";return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(N,{name:"circle-alert",size:16,color:"#f59e0b"}),e.jsx("span",{children:t.content})]}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-primary ${n==="confirm"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>a({action:"confirm"}),children:[e.jsx(N,{name:"check",size:14})," ",i]}),e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-secondary ${n==="cancel"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>a({action:"cancel"}),children:[e.jsx(N,{name:"x",size:14})," ",l]})]}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function Oe({interrupt:t,onRespond:a,disabled:r,answeredAction:n}){var p,u,m,w;const s=t.options,i=(typeof(s==null?void 0:s[0])=="string"?s[0]:(p=s==null?void 0:s[0])==null?void 0:p.label)||"批准",l=(typeof(s==null?void 0:s[1])=="string"?s[1]:(u=s==null?void 0:s[1])==null?void 0:u.label)||"拒绝",c=(typeof(s==null?void 0:s[2])=="string"?s[2]:(m=s==null?void 0:s[2])==null?void 0:m.label)||"跳过",d=((w=t.metadata)==null?void 0:w.showSkip)!==!1;return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(N,{name:"circle-alert",size:16,color:"#f59e0b"}),e.jsx("span",{children:t.content})]}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-primary ${n==="approve"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>a({action:"approve"}),children:[e.jsx(N,{name:"check",size:14})," ",i]}),e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-danger ${n==="reject"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>a({action:"reject"}),children:[e.jsx(N,{name:"x",size:14})," ",l]}),d&&e.jsxs("button",{className:`sa-interrupt-btn sa-interrupt-btn-secondary ${n==="skip"?"sa-interrupt-btn-answered":""}`,disabled:r,onClick:()=>a({action:"skip"}),children:[e.jsx(N,{name:"chevron-right",size:14})," ",c]})]}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function He(t){return typeof t=="string"?{value:t,label:t}:t}function Fe({interrupt:t,onRespond:a,disabled:r,initialValue:n}){const s=(t.options??[]).map(He);return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(N,{name:"list",size:16,color:"#6366f1"}),e.jsx("span",{children:t.content})]}),e.jsx("div",{className:"sa-interrupt-options",children:s.map(i=>{const l=n===i.value;return e.jsxs("button",{className:`sa-interrupt-option ${i.danger?"sa-interrupt-option-danger":""} ${l?"sa-interrupt-option-selected":""}`,disabled:r,onClick:()=>a({action:"select",value:i.value}),children:[e.jsx(N,{name:l?"circle-check":"circle",size:14}),i.icon&&e.jsx(N,{name:i.icon,size:14}),e.jsx("span",{children:i.label}),i.description&&e.jsx("span",{className:"sa-interrupt-option-desc",children:i.description})]},i.value)})}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function _e(t){return typeof t=="string"?{value:t,label:t}:t}function Be({interrupt:t,onRespond:a,disabled:r,initialValue:n}){const s=(t.options??[]).map(_e),[i,l]=o.useState(()=>new Set(Array.isArray(n)?n:[])),c=d=>{r||l(p=>{const u=new Set(p);return u.has(d)?u.delete(d):u.add(d),u})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(N,{name:"list-checks",size:16,color:"#6366f1"}),e.jsx("span",{children:t.content})]}),e.jsx("div",{className:"sa-interrupt-options",children:s.map(d=>e.jsxs("button",{className:`sa-interrupt-option ${i.has(d.value)?"sa-interrupt-option-selected":""}`,disabled:r,onClick:()=>c(d.value),children:[e.jsx(N,{name:i.has(d.value)?"square-check":"square",size:14}),e.jsx("span",{children:d.label})]},d.value))}),!r&&e.jsx("div",{className:"sa-interrupt-actions",children:e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:i.size===0,onClick:()=>a({action:"submit",value:Array.from(i)}),children:[e.jsx(N,{name:"check",size:14})," 确认 (",i.size,")"]})}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function We({interrupt:t,onRespond:a,disabled:r,initialValue:n}){const[s,i]=o.useState(n??""),l=t.inputType??"text",c=l==="textarea",d=()=>{r||t.required&&!s.trim()||a({action:"submit",value:s.trim()})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(N,{name:"text-cursor-input",size:16,color:"#6366f1"}),e.jsx("span",{children:t.content})]}),e.jsx("div",{className:"sa-interrupt-input-area",children:c?e.jsx("textarea",{className:"sa-interrupt-textarea",value:s,onChange:p=>i(p.target.value),placeholder:t.placeholder,maxLength:t.maxLength,rows:3,disabled:r}):e.jsx("input",{className:"sa-interrupt-input",type:l,value:s,onChange:p=>i(p.target.value),placeholder:t.placeholder,maxLength:t.maxLength,onKeyDown:p=>p.key==="Enter"&&d(),disabled:r})}),!r&&e.jsx("div",{className:"sa-interrupt-actions",children:e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:t.required?!s.trim():!1,onClick:d,children:[e.jsx(N,{name:"check",size:14})," 提交"]})}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function qe({interrupt:t,onRespond:a,disabled:r,initialValue:n}){const s=t.fields??[],[i,l]=o.useState(()=>{const u={};for(const m of s)u[m.name]=(n==null?void 0:n[m.name])??m.defaultValue??"";return u}),c=(u,m)=>{r||l(w=>({...w,[u]:m}))},d=!r&&s.every(u=>!u.required||i[u.name]),p=()=>{d&&a({action:"submit",value:i})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(N,{name:"file-text",size:16,color:"#6366f1"}),e.jsx("span",{children:t.content})]}),e.jsx("div",{className:"sa-interrupt-form",children:s.map(u=>e.jsxs("div",{className:"sa-interrupt-form-field",children:[e.jsxs("label",{className:"sa-interrupt-form-label",children:[u.label,u.required&&e.jsx("span",{className:"sa-interrupt-form-required",children:"*"})]}),Ke(u,i[u.name],m=>c(u.name,m),r)]},u.name))}),!r&&e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:!d,onClick:p,children:[e.jsx(N,{name:"check",size:14})," 提交"]}),e.jsx("button",{className:"sa-interrupt-btn sa-interrupt-btn-secondary",onClick:()=>a({action:"cancel"}),children:"取消"})]}),r&&e.jsx("div",{className:"sa-interrupt-submitted",children:"已提交"})]})}function Q(t){return typeof t=="string"?{value:t,label:t}:t}function Ke(t,a,r,n){switch(t.type){case"textarea":return e.jsx("textarea",{className:"sa-interrupt-textarea",value:a,onChange:s=>r(s.target.value),placeholder:t.placeholder,rows:3,disabled:n});case"select":{const s=(t.options??[]).map(Q);return e.jsxs("select",{className:"sa-interrupt-select",value:a,onChange:i=>r(i.target.value),disabled:n,children:[e.jsx("option",{value:"",children:"请选择"}),s.map(i=>e.jsx("option",{value:i.value,children:i.label},i.value))]})}case"multiSelect":{const s=(t.options??[]).map(Q),i=Array.isArray(a)?a:[],l=c=>{n||r(i.includes(c)?i.filter(d=>d!==c):[...i,c])};return e.jsx("div",{className:"sa-interrupt-options",children:s.map(c=>e.jsx("button",{type:"button",className:`sa-interrupt-option ${i.includes(c.value)?"sa-interrupt-option-selected":""}`,disabled:n,onClick:()=>l(c.value),children:c.label},c.value))})}case"password":return e.jsx("input",{className:"sa-interrupt-input",type:"password",value:a,onChange:s=>r(s.target.value),placeholder:t.placeholder,disabled:n});default:return e.jsx("input",{className:"sa-interrupt-input",type:t.type==="number"?"number":t.type==="date"?"date":"text",value:a,onChange:s=>r(s.target.value),placeholder:t.placeholder,disabled:n})}}function ve({interrupt:t,resolved:a,response:r,onRespond:n}){const s=a,i=r==null?void 0:r.value;switch(t.interruptType){case"confirm":return e.jsx(Pe,{interrupt:t,onRespond:n,disabled:s,answeredAction:r==null?void 0:r.action});case"approve":return e.jsx(Oe,{interrupt:t,onRespond:n,disabled:s,answeredAction:r==null?void 0:r.action});case"select":return e.jsx(Fe,{interrupt:t,onRespond:n,disabled:s,initialValue:i});case"multiSelect":return e.jsx(Be,{interrupt:t,onRespond:n,disabled:s,initialValue:i});case"input":return e.jsx(We,{interrupt:t,onRespond:n,disabled:s,initialValue:i});case"form":return e.jsx(qe,{interrupt:t,onRespond:n,disabled:s,initialValue:i});default:return e.jsxs("div",{className:"sa-interrupt-unknown",children:["未知的交互类型: ",t.interruptType]})}}const Ye=[{value:1,label:"事实错误"},{value:2,label:"逻辑问题"},{value:3,label:"不相关"},{value:4,label:"信息过时"},{value:5,label:"冗长啰嗦"},{value:6,label:"难以理解"}];function we({message:t,onCopy:a,onRegenerate:r,onFeedback:n}){const{status:s,cancelFeedback:i}=_(),[l,c]=o.useState(null),[d,p]=o.useState(!1),[u,m]=o.useState(!1),w=o.useRef(null),f=o.useCallback(()=>{a(),p(!0),setTimeout(()=>p(!1),2e3)},[a]),x=o.useCallback(()=>{l==="like"?(c(null),i(t.id)):(c("like"),m(!1),n==null||n(t.id,"like"))},[l,t.id,n,i]),v=o.useCallback(()=>{l==="dislike"?(c(null),m(!1),i(t.id)):m(!0)},[l,t.id,i]),z=o.useCallback((E,g)=>{c("dislike"),m(!1),n==null||n(t.id,"dislike",{reason:E,remark:(g==null?void 0:g.trim())||void 0})},[t.id,n]),S=o.useCallback(()=>{m(!1)},[]);return o.useEffect(()=>{if(!u)return;const E=C=>{w.current&&!w.current.contains(C.target)&&m(!1)},g=C=>{C.key==="Escape"&&m(!1)};return document.addEventListener("mousedown",E),document.addEventListener("keydown",g),()=>{document.removeEventListener("mousedown",E),document.removeEventListener("keydown",g)}},[u]),s!=="ready"&&s!=="error"?null:e.jsxs("div",{className:"sa-action-bar",children:[e.jsxs("button",{className:"sa-action-btn",onClick:f,title:"复制",children:[e.jsx(N,{name:d?"circle-check":"copy",size:14})," ",d?"已复制":"复制"]}),r&&e.jsxs("button",{className:"sa-action-btn",onClick:r,title:"重新生成",children:[e.jsx(N,{name:"refresh-cw",size:14})," 重新生成"]}),n&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"sa-action-divider"}),e.jsx("button",{className:`sa-action-btn ${l==="like"?"sa-action-btn-active":""}`,onClick:x,title:"有用","aria-label":"点赞",children:e.jsx(N,{name:"thumbs-up",size:14})}),e.jsx("button",{className:`sa-action-btn ${l==="dislike"?"sa-action-btn-active":""}`,onClick:v,title:"无用","aria-label":"踩","aria-expanded":u,"aria-haspopup":"dialog",children:e.jsx(N,{name:"thumbs-down",size:14})})]}),u&&e.jsx("div",{ref:w,children:e.jsx(Ue,{onSubmit:z,onCancel:S})})]})}function Ue({onSubmit:t,onCancel:a}){const[r,n]=o.useState(void 0),[s,i]=o.useState("");return e.jsxs("div",{className:"sa-dislike-panel",children:[e.jsx("div",{className:"sa-dislike-panel-title",children:"请选择不满意的原因(可选)"}),e.jsx("div",{className:"sa-dislike-reasons",children:Ye.map(l=>e.jsx("button",{className:`sa-dislike-reason-btn ${r===l.value?"active":""}`,onClick:()=>n(r===l.value?void 0:l.value),children:l.label},l.value))}),e.jsx("textarea",{className:"sa-dislike-remark",placeholder:"补充描述(选填,≤500 字)",maxLength:500,value:s,onChange:l=>i(l.target.value),rows:2}),e.jsxs("div",{className:"sa-dislike-actions",children:[e.jsx("button",{className:"sa-dislike-submit",onClick:()=>t(r,s||void 0),children:"提交"}),e.jsx("button",{className:"sa-dislike-cancel",onClick:a,children:"取消"})]})]})}function ee({src:t,role:a}){const r=a==="assistant"?"A":"U";return t?t.startsWith("http")||t.startsWith("/")||t.startsWith("data:")?e.jsx("div",{className:`sa-avatar sa-avatar-${a}`,children:e.jsx("img",{src:t,alt:a,style:{width:"100%",height:"100%",borderRadius:"inherit",objectFit:"cover"}})}):e.jsx("div",{className:`sa-avatar sa-avatar-${a}`,children:t}):e.jsx("div",{className:`sa-avatar sa-avatar-${a}`,children:r})}function ye({message:t,isStreaming:a,isLast:r,slots:n,avatar:s,artifactPreview:i}){const{regenerate:l,feedback:c,respondToInterrupt:d}=_(),p=o.useCallback(()=>{var C;const g=t.parts.filter(I=>I.type==="text").map(I=>I.content).join(`
|
|
3
|
+
`);(C=navigator.clipboard)==null||C.writeText(g).catch(()=>{})},[t]),u=t.role==="user",m=n.Message;if(m)return e.jsx(m,{message:t,isStreaming:a,isLast:r,avatar:s,slots:n,onCopy:p,onRegenerate:l,onFeedback:c});const w=n.TextPart??xe,f=n.ThinkingPart??me,x=n.ToolCallPart??he,v=n.ToolResultPart??ge,z=n.ErrorPart??be,S=n.ActionBar??we,E=!u&&t.parts.some(g=>g.type==="text"&&g.content);return e.jsxs("div",{className:`sa-message ${u?"sa-message-user":"sa-message-assistant"}`,children:[!u&&e.jsx(ee,{src:s==null?void 0:s.assistant,role:"assistant"}),e.jsxs("div",{className:"sa-message-content",children:[e.jsx("span",{className:`sa-message-time ${u?"sa-message-time-right":""}`,children:pe(t.timestamp)}),e.jsxs("div",{className:"sa-message-body",children:[t.parts.map((g,C)=>{switch(g.type){case"text":return e.jsx(w,{content:g.content},C);case"thinking":return e.jsx(f,{content:g.content},C);case"tool_call":return e.jsx(x,{toolName:g.toolName,toolCallId:g.toolCallId,args:g.args},C);case"tool_result":return e.jsx(v,{toolName:g.toolName,toolCallId:g.toolCallId,content:g.content,artifacts:g.artifacts,renderMode:g.renderMode,html:g.html,artifactPreview:i},C);case"error":return e.jsx(z,{content:g.content,onRetry:l},C);case"interrupt":return e.jsx(ve,{interrupt:g.interrupt,resolved:g.resolved??!1,response:g.response,onRespond:I=>d(g.interrupt.interruptId,I)},C);default:return null}}),a&&r&&!u&&e.jsx("span",{className:"sa-streaming-cursor",children:"|"}),!u&&!a&&E&&e.jsx(S,{message:t,onCopy:p,onRegenerate:r?l:void 0,onFeedback:c})]})]}),u&&e.jsx(ee,{src:s==null?void 0:s.user,role:"user"})]})}function ke({message:t,suggestedPrompts:a,onPromptClick:r}){return e.jsxs("div",{className:"sa-welcome",children:[e.jsx("div",{className:"sa-welcome-icon",children:e.jsx(N,{name:"sparkles",size:40})}),e.jsx("p",{className:"sa-welcome-message",children:t||"你好!有什么可以帮你的?"}),a&&a.length>0&&e.jsx("div",{className:"sa-welcome-prompts",children:a.map((n,s)=>e.jsx("button",{className:"sa-welcome-prompt",onClick:()=>r(n),children:n},s))})]})}function V({slots:t,welcomeMessage:a,suggestedPrompts:r,avatar:n,artifactPreview:s}){const{messages:i,status:l,sendMessage:c}=_(),{containerRef:d}=fe([i]),p=l==="streaming",u=t.WelcomeScreen??ke;return i.length===0?e.jsx("div",{className:"sa-thread",ref:d,children:e.jsx(u,{message:a,suggestedPrompts:r,onPromptClick:c})}):e.jsx("div",{className:"sa-thread",ref:d,children:i.map((m,w)=>e.jsx(ye,{message:m,isStreaming:p,isLast:w===i.length-1,slots:t,avatar:n,artifactPreview:s},m.id))})}function Ve({items:t,activeIndex:a,onSelect:r}){const n=o.useRef(null);return o.useEffect(()=>{var i;const s=(i=n.current)==null?void 0:i.children[a];s==null||s.scrollIntoView({block:"nearest"})},[a]),t.length===0?e.jsx("div",{className:"sa-slash-menu",children:e.jsx("div",{className:"sa-slash-menu-empty",children:"没有匹配的技能"})}):e.jsx("div",{className:"sa-slash-menu",ref:n,children:t.map((s,i)=>e.jsxs("div",{className:`sa-slash-menu-item${i===a?" sa-slash-menu-item--active":""}`,onMouseDown:l=>{l.preventDefault(),r(s)},children:[e.jsxs("span",{className:"sa-slash-menu-item-name",children:["/",s.name]}),(s.displayName||s.description)&&e.jsx("span",{className:"sa-slash-menu-item-desc",children:s.displayName||s.description})]},s.code))})}function Xe({sdk:t,input:a,onSelect:r}){const[n,s]=o.useState([]),[i,l]=o.useState(!1),[c,d]=o.useState(0),p=o.useRef(!1);o.useEffect(()=>{p.current||(p.current=!0,t.listSkills().then(s).catch(()=>s([])))},[t]);const u=/^\/\S*$/.test(a);o.useEffect(()=>{l(u)},[u]),o.useEffect(()=>{u&&d(0)},[a,u]);const m=a.startsWith("/")?a.slice(1).toLowerCase():"",w=o.useMemo(()=>n.filter(g=>!m||g.code.toLowerCase().includes(m)||g.name.toLowerCase().includes(m)||g.displayName&&g.displayName.toLowerCase().includes(m)),[n,m]),f=o.useRef(i),x=o.useRef(w),v=o.useRef(c),z=o.useRef(r);f.current=i,x.current=w,v.current=c,z.current=r;const S=o.useCallback(g=>{if(!f.current||x.current.length===0)return!1;switch(g.key){case"ArrowDown":return g.preventDefault(),d(C=>(C+1)%x.current.length),!0;case"ArrowUp":return g.preventDefault(),d(C=>(C-1+x.current.length)%x.current.length),!0;case"Enter":case"Tab":return g.preventDefault(),z.current(x.current[v.current]),l(!1),!0;case"Escape":return g.preventDefault(),l(!1),!0;default:return!1}},[]),E=o.useCallback(()=>l(!1),[]);return{isOpen:i,items:w,activeIndex:c,handleKeyDown:S,close:E}}function X(){const{status:t,sendMessage:a,stop:r,sdk:n}=_(),[s,i]=o.useState(""),l=o.useRef(null),c=t==="streaming"||t==="submitted",d=o.useCallback(f=>{i(`/${f.name} `),setTimeout(()=>{const x=l.current;x&&(x.focus(),x.selectionStart=x.selectionEnd=x.value.length)},0)},[]),p=Xe({sdk:n,input:s,onSelect:d}),u=o.useCallback(()=>{const f=s.trim();!f||c||(a(f),i(""),l.current&&(l.current.style.height="auto"))},[s,c,a]),m=o.useCallback(f=>{p.handleKeyDown(f)||f.key==="Enter"&&!f.shiftKey&&(f.preventDefault(),u())},[u,p.handleKeyDown]),w=o.useCallback(f=>{i(f.target.value);const x=f.target;x.style.height="auto",x.style.height=Math.min(x.scrollHeight,120)+"px"},[]);return e.jsxs("div",{className:"sa-composer",children:[p.isOpen&&e.jsx(Ve,{items:p.items,activeIndex:p.activeIndex,onSelect:d}),e.jsx("textarea",{ref:l,className:"sa-composer-input",value:s,onChange:w,onKeyDown:m,placeholder:c?"回答生成中...":"输入消息,按 / 选择技能...",rows:1,disabled:c}),c?e.jsx("button",{className:"sa-composer-stop",onClick:r,title:"停止",children:e.jsx(N,{name:"circle-stop",size:16,color:"#fff"})}):e.jsx("button",{className:"sa-composer-send",onClick:u,disabled:!s.trim(),title:"发送",children:e.jsx(N,{name:"arrow-up",size:16,color:"#fff"})})]})}function Ge({artifact:t}){const[a,r]=o.useState(null),[n,s]=o.useState(!0),[i,l]=o.useState(null);return o.useEffect(()=>{let c=!1;s(!0),l(null),r(null);async function d(){try{if(t.type==="pdf"){c||s(!1);return}if(t.type==="html"){const p=await fetch(t.url);if(!p.ok)throw new Error(`HTTP ${p.status}`);const u=await p.text(),m=(await Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs"))).default;c||(r(m.sanitize(u,{FORCE_BODY:!0,ADD_ATTR:["target"]})),s(!1));return}if(t.type==="docx"){const p=await fetch(t.url);if(!p.ok)throw new Error(`HTTP ${p.status}`);const u=await p.arrayBuffer(),w=await(await Promise.resolve().then(()=>require("./mammoth.browser-DiAPPO3x.cjs")).then(x=>x.mammoth_browser)).convertToHtml({arrayBuffer:u}),f=(await Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs"))).default;c||(r(f.sanitize(w.value,{FORCE_BODY:!0,ADD_ATTR:["target"]})),s(!1));return}c||(s(!1),l(`不支持预览「${t.type}」类型`))}catch(p){c||(s(!1),l(p instanceof Error?p.message:"加载失败"))}}return d(),()=>{c=!0}},[t]),n?e.jsx("div",{className:"sa-drawer-preview-loading",children:"加载中"}):i?e.jsxs("div",{className:"sa-drawer-preview-error",children:[i,e.jsx("button",{className:"sa-drawer-btn-sm",onClick:()=>void K(t),children:"下载查看"})]}):t.type==="pdf"?e.jsx("iframe",{src:t.url,title:t.filename,className:"sa-drawer-preview-iframe",sandbox:"allow-scripts"}):a!==null?e.jsx("iframe",{srcDoc:a,title:t.filename,className:"sa-drawer-preview-iframe",sandbox:"allow-same-origin"}):null}function je({artifacts:t,initialArtifact:a,onClose:r}){const[n,s]=o.useState(a?{mode:"preview",artifact:a}:{mode:"list"}),[i,l]=o.useState(!1),c=o.useRef(null),[d,p]=o.useState(340),u=o.useRef(null);o.useEffect(()=>{a&&(c.current&&(clearTimeout(c.current),c.current=null,l(!1)),s({mode:"preview",artifact:a}))},[a]);const m=o.useCallback(()=>{l(!0),c.current=setTimeout(()=>{c.current=null,r()},240)},[r]);o.useEffect(()=>()=>{var v;c.current&&clearTimeout(c.current),(v=u.current)==null||v.call(u)},[]);const w=o.useCallback(v=>{s({mode:"preview",artifact:v})},[]),f=o.useCallback(()=>{s({mode:"list"})},[]);o.useEffect(()=>{const v=z=>{z.key==="Escape"&&m()};return document.addEventListener("keydown",v),()=>document.removeEventListener("keydown",v)},[m]);const x=o.useCallback(v=>{v.preventDefault();const z=v.clientX,S=d,E=C=>{const I=z-C.clientX;p(Math.max(240,Math.min(window.innerWidth*.7,S+I)))},g=()=>{document.removeEventListener("mousemove",E),document.removeEventListener("mouseup",g),document.body.style.cursor="",document.body.style.userSelect="",u.current=null};document.body.style.cursor="col-resize",document.body.style.userSelect="none",document.addEventListener("mousemove",E),document.addEventListener("mouseup",g),u.current=g},[d]);return e.jsxs("div",{className:`sa-artifact-drawer ${i?"sa-drawer-closing":""}`,style:{width:d},children:[e.jsx("div",{className:"sa-drawer-resize-handle",onMouseDown:x}),e.jsxs("div",{className:"sa-artifact-drawer-header",children:[n.mode==="preview"?e.jsxs(e.Fragment,{children:[e.jsx("button",{className:"sa-drawer-btn",onClick:f,title:"返回列表","aria-label":"返回列表",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M15 18l-6-6 6-6"})})}),e.jsx("span",{className:"sa-artifact-drawer-title",children:n.artifact.filename})]}):e.jsxs(e.Fragment,{children:[e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",style:{flexShrink:0},children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{className:"sa-artifact-drawer-title",children:"会话产物"})]}),e.jsx("div",{style:{flex:1}}),n.mode==="preview"&&e.jsx("button",{className:"sa-drawer-btn",onClick:()=>void K(n.artifact),title:"下载","aria-label":"下载",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})}),e.jsx("button",{className:"sa-drawer-btn",onClick:m,title:"关闭","aria-label":"关闭",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M18 6L6 18M6 6l12 12"})})})]}),e.jsx("div",{className:"sa-artifact-drawer-body",children:n.mode==="list"?t.length===0?e.jsx("div",{className:"sa-drawer-empty",children:"暂无产物"}):e.jsx("ul",{className:"sa-drawer-file-list",role:"list",children:t.map((v,z)=>e.jsxs("li",{className:"sa-drawer-file-card",onClick:()=>w(v),onKeyDown:S=>{(S.key==="Enter"||S.key===" ")&&(S.preventDefault(),w(v))},tabIndex:0,role:"button","aria-label":`预览 ${v.filename}`,children:[e.jsx("div",{className:"sa-drawer-file-icon",children:e.jsx("span",{className:"sa-artifact-chip-type",children:ne(v.type)})}),e.jsxs("div",{className:"sa-drawer-file-info",children:[e.jsx("span",{className:"sa-drawer-file-name",children:v.filename}),e.jsx("span",{className:"sa-drawer-file-size",children:re(v.size)})]}),e.jsx("button",{className:"sa-drawer-btn",title:"下载","aria-label":`下载 ${v.filename}`,onClick:S=>{S.stopPropagation(),K(v)},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})})]},v.id||`artifact-${z}`))}):e.jsx("div",{className:"sa-drawer-view-enter",children:e.jsx(Ge,{artifact:n.artifact})})})]})}function Je({isOpen:t,onClose:a,title:r,slots:n,welcomeMessage:s,suggestedPrompts:i,avatar:l,artifactPreview:c}){const{status:d,sendMessage:p,stop:u,toggleThreadList:m,toggleArtifactDrawer:w,conversations:f,activeConversationId:x,switchConversation:v,newConversation:z,deleteConversation:S,renameConversation:E,isThreadListOpen:g,isArtifactDrawerOpen:C,allArtifacts:I,activePreviewArtifact:L,messagesLoading:j}=_();if(!t)return null;const D=n.Header,y=n.Composer,h=n.ThreadList;return e.jsxs("div",{className:"sa-panel",children:[D?e.jsx(D,{title:r,onClose:a,onToggleThreadList:m}):e.jsx(de,{title:r,onClose:a,onToggleThreadList:m,artifactCount:c?I.length:0,onToggleArtifactDrawer:c?w:void 0}),h&&g?e.jsx(h,{conversations:f,activeSessionId:x,onSwitch:v,onNew:z,onDelete:S,onRename:E}):e.jsx(ue,{}),e.jsxs("div",{className:"sa-panel-content",children:[j?e.jsxs("div",{className:"sa-messages-loading",children:[e.jsx("span",{className:"sa-loading-spinner"}),"加载对话..."]}):e.jsx(V,{slots:n,welcomeMessage:s,suggestedPrompts:i,avatar:l,artifactPreview:c}),c&&C&&e.jsx(je,{artifacts:I,initialArtifact:L,onClose:w})]}),y?e.jsx(y,{status:d,onSend:p,onStop:u}):e.jsx(X,{})]})}function Ze({conversations:t,activeSessionId:a,onSwitch:r,onNew:n,onDelete:s,onRename:i,isCollapsed:l,onToggleCollapse:c,hasMore:d,onLoadMore:p,conversationsLoading:u}){const[m,w]=o.useState(null),[f,x]=o.useState(null),[v,z]=o.useState(null),[S,E]=o.useState(""),g=o.useCallback((y,h)=>{h.stopPropagation(),f===y?(s(y),x(null)):(x(y),setTimeout(()=>x(null),3e3))},[f,s]),C=o.useCallback((y,h,$)=>{$.stopPropagation(),z(y),E(h??"")},[]),I=o.useCallback(y=>{S.trim()&&i(y,S.trim()),z(null)},[S,i]),L=o.useRef(null),j=o.useCallback(()=>{const y=L.current;!y||!d||!p||y.scrollHeight-y.scrollTop-y.clientHeight<50&&p()},[d,p]),D=(()=>{const y=Date.now(),h=[],$=[],M=[];for(const R of t){const b=y-new Date(R.updateTime).getTime();b<864e5?h.push(R):b<6048e5?$.push(R):M.push(R)}const k=[];return h.length&&k.push({label:"今天",items:h}),$.length&&k.push({label:"最近 7 天",items:$}),M.length&&k.push({label:"更早",items:M}),k})();return l?e.jsxs("div",{className:"sa-fp-sidebar sa-fp-sidebar-collapsed",children:[e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:c,title:"展开",children:e.jsx(N,{name:"panel-left",size:18})}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:()=>{n(),c()},title:"新会话",style:{marginTop:4},children:e.jsx(N,{name:"plus",size:18})})]}):e.jsxs("div",{className:"sa-fp-sidebar",children:[e.jsxs("div",{className:"sa-fp-sidebar-header",children:[e.jsxs("button",{className:"sa-fp-sidebar-new",onClick:n,children:[e.jsx(N,{name:"plus",size:15})," ",e.jsx("span",{children:"新会话"})]}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:c,title:"收起侧边栏",children:e.jsx(N,{name:"panel-left-close",size:18})})]}),e.jsxs("div",{className:"sa-fp-sidebar-list",ref:L,onScroll:j,children:[u&&t.length===0&&e.jsx("div",{className:"sa-thread-list-loading",children:"加载中..."}),!u&&t.length===0&&e.jsx("div",{className:"sa-fp-sidebar-empty",children:"开始你的第一个对话"}),D.map(y=>e.jsxs("div",{children:[e.jsx("div",{className:"sa-fp-sidebar-group",children:y.label}),y.items.map(h=>{const $=h.sessionId===a,M=m===h.sessionId,k=f===h.sessionId;return e.jsxs("div",{className:`sa-fp-sidebar-item ${$?"active":""}`,onClick:()=>{v!==h.sessionId&&r(h.sessionId)},onMouseEnter:()=>w(h.sessionId),onMouseLeave:()=>w(null),children:[v===h.sessionId?e.jsx("input",{className:"sa-fp-sidebar-item-edit",value:S,onChange:R=>E(R.target.value),onBlur:()=>I(h.sessionId),onKeyDown:R=>{R.key==="Enter"&&I(h.sessionId),R.key==="Escape"&&z(null)},onClick:R=>R.stopPropagation(),autoFocus:!0}):e.jsx("span",{className:"sa-fp-sidebar-item-title",children:h.title||"新会话"}),e.jsxs("div",{className:`sa-fp-sidebar-item-actions ${M&&v!==h.sessionId?"visible":""}`,children:[e.jsx("button",{className:"sa-fp-sidebar-item-action",onClick:R=>C(h.sessionId,h.title,R),title:"重命名",children:e.jsx(N,{name:"pen",size:14})}),e.jsx("button",{className:`sa-fp-sidebar-item-action ${k?"danger":""}`,onClick:R=>g(h.sessionId,R),title:k?"确认删除":"删除",children:e.jsx(N,{name:k?"x":"trash2",size:14})})]})]},h.sessionId)})]},y.label)),d&&e.jsx("div",{className:"sa-thread-list-loading",children:"加载更多..."})]})]})}function Ce({title:t,slots:a,welcomeMessage:r,suggestedPrompts:n,sidebarDefaultOpen:s=!0,onClose:i,avatar:l,artifactPreview:c}){const{status:d,sendMessage:p,stop:u,conversations:m,activeConversationId:w,switchConversation:f,newConversation:x,deleteConversation:v,renameConversation:z,toggleArtifactDrawer:S,isArtifactDrawerOpen:E,allArtifacts:g,activePreviewArtifact:C,hasMore:I,loadMoreConversations:L,conversationsLoading:j,messagesLoading:D}=_(),[y,h]=o.useState(s),$=o.useCallback(()=>h(R=>!R),[]),M=a.ThreadList,k=a.Composer;return e.jsxs("div",{className:"sa-fullpage",children:[M?e.jsx("div",{className:`sa-fp-sidebar-wrapper ${y?"":"sa-fp-sidebar-wrapper-collapsed"}`,children:e.jsx(M,{conversations:m,activeSessionId:w,onSwitch:f,onNew:x,onDelete:v,onRename:z,hasMore:I,onLoadMore:L})}):e.jsx(Ze,{conversations:m,activeSessionId:w,onSwitch:f,onNew:x,onDelete:v,onRename:z,isCollapsed:!y,onToggleCollapse:$,hasMore:I,onLoadMore:L,conversationsLoading:j}),e.jsxs("div",{className:"sa-fp-main",children:[e.jsxs("div",{className:"sa-fp-header",children:[!y&&!M&&e.jsx("button",{className:"sa-fp-header-btn",onClick:$,title:"展开侧边栏",children:e.jsx(N,{name:"panel-left",size:18})}),e.jsx("div",{className:"sa-fp-header-title",children:t}),e.jsxs("div",{className:"sa-fp-header-actions",children:[c&&g.length>0&&e.jsxs("button",{className:"sa-fp-header-btn sa-header-btn-artifact",onClick:S,title:"会话产物",children:[e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{children:"产物"})]}),i&&e.jsx("button",{className:"sa-fp-header-btn",onClick:i,title:"关闭",children:e.jsx(N,{name:"x",size:18})})]})]}),e.jsxs("div",{className:"sa-fp-content-row",children:[D?e.jsxs("div",{className:"sa-messages-loading",children:[e.jsx("span",{className:"sa-loading-spinner"}),"加载对话..."]}):e.jsx(V,{slots:a,welcomeMessage:r,suggestedPrompts:n,avatar:l,artifactPreview:c}),c&&E&&e.jsx(je,{artifacts:g,initialArtifact:C,onClose:S})]}),k?e.jsx(k,{status:d,onSend:p,onStop:u}):e.jsx(X,{})]})]})}function Ne(t){const{sdk:a,mode:r="floating",slots:n={},hooks:s={},welcomeMessage:i,suggestedPrompts:l,title:c="AI Assistant",initialOpen:d=!1,onOpenChange:p,sidebarDefaultOpen:u,avatar:m,artifactPreview:w}=t,[f,x]=o.useState(d);o.useEffect(()=>{x(d)},[d]);const v=o.useCallback(()=>{var g,C;const E=!f;x(E),p==null||p(E),E?(g=s.onOpen)==null||g.call(s):(C=s.onClose)==null||C.call(s)},[f,s,p]),z=o.useCallback(()=>{var E;x(!1),p==null||p(!1),(E=s.onClose)==null||E.call(s)},[s,p]),S=n.Trigger??ce;return r==="fullpage"?f?e.jsx(U,{sdk:a,hooks:s,children:e.jsx(Ce,{title:c,slots:n,welcomeMessage:i,suggestedPrompts:l,sidebarDefaultOpen:u,onClose:z,avatar:m,artifactPreview:w})}):null:e.jsxs(U,{sdk:a,hooks:s,children:[e.jsx(S,{isOpen:f,onClick:v}),e.jsx(Je,{isOpen:f,onClose:z,title:c,slots:n,welcomeMessage:i,suggestedPrompts:l,avatar:m,artifactPreview:w})]})}const ze=`
|
|
4
4
|
[data-super-agent-widget] {
|
|
5
5
|
--sa-primary-dark: color-mix(in srgb, var(--sa-primary) 82%, #000);
|
|
6
6
|
--sa-primary-light: color-mix(in srgb, var(--sa-primary) 10%, #fff);
|
|
@@ -488,14 +488,24 @@
|
|
|
488
488
|
}
|
|
489
489
|
|
|
490
490
|
.sa-tool-result-content {
|
|
491
|
-
max-height:
|
|
492
|
-
overflow
|
|
491
|
+
max-height: 256px;
|
|
492
|
+
overflow: auto;
|
|
493
|
+
margin: 8px 0 0;
|
|
493
494
|
padding: 10px 12px;
|
|
494
495
|
color: #4b5563;
|
|
495
496
|
font-size: 13px;
|
|
496
497
|
line-height: 1.55;
|
|
497
498
|
white-space: pre-wrap;
|
|
498
499
|
word-break: break-word;
|
|
500
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
501
|
+
background: #f9fafb;
|
|
502
|
+
border-radius: 8px;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
.sa-tool-result-actions {
|
|
506
|
+
display: flex;
|
|
507
|
+
gap: 8px;
|
|
508
|
+
margin: 4px 0;
|
|
499
509
|
}
|
|
500
510
|
|
|
501
511
|
/* ── Error part ──────────────────────────────────────────── */
|
|
@@ -840,6 +850,38 @@
|
|
|
840
850
|
font-size: 13px;
|
|
841
851
|
}
|
|
842
852
|
|
|
853
|
+
.sa-thread-list-hint {
|
|
854
|
+
padding: 8px 16px 12px;
|
|
855
|
+
text-align: center;
|
|
856
|
+
color: #d1d5db;
|
|
857
|
+
font-size: 12px;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/* 消息区加载态 */
|
|
861
|
+
.sa-messages-loading {
|
|
862
|
+
flex: 1;
|
|
863
|
+
display: flex;
|
|
864
|
+
align-items: center;
|
|
865
|
+
justify-content: center;
|
|
866
|
+
gap: 8px;
|
|
867
|
+
color: #9ca3af;
|
|
868
|
+
font-size: 14px;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
.sa-loading-spinner {
|
|
872
|
+
width: 16px;
|
|
873
|
+
height: 16px;
|
|
874
|
+
border: 2px solid var(--sa-border, #e5e7eb);
|
|
875
|
+
border-top-color: var(--sa-primary, #6366f1);
|
|
876
|
+
border-radius: 50%;
|
|
877
|
+
animation: sa-spin 0.8s linear infinite;
|
|
878
|
+
flex-shrink: 0;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
@keyframes sa-spin {
|
|
882
|
+
to { transform: rotate(360deg); }
|
|
883
|
+
}
|
|
884
|
+
|
|
843
885
|
.sa-thread-group-label {
|
|
844
886
|
padding: 16px 8px 6px;
|
|
845
887
|
font-size: 12px;
|
|
@@ -1560,8 +1602,9 @@
|
|
|
1560
1602
|
.sa-artifact-file-download:active { transform: scale(0.9); }
|
|
1561
1603
|
|
|
1562
1604
|
/* ── Inline HTML rendering (render_mode=html) ── */
|
|
1563
|
-
|
|
1564
|
-
.sa-tool-result-html-
|
|
1605
|
+
/* HTML 工具结果:外层滚动容器 + 内层内容层(与 Playground 一致,无 iframe) */
|
|
1606
|
+
.sa-tool-result-html-scroll { margin-top: 8px; max-height: 60vh; overflow: auto; border: 1px solid var(--sa-border); border-radius: 8px; }
|
|
1607
|
+
.sa-tool-result-html { padding: 12px; background: #fff; min-width: 100%; width: max-content; }
|
|
1565
1608
|
|
|
1566
1609
|
/* ── Header artifact button ── */
|
|
1567
1610
|
.sa-header-btn-artifact {
|
|
@@ -1690,4 +1733,4 @@
|
|
|
1690
1733
|
from { opacity: 0; transform: translateX(12px); }
|
|
1691
1734
|
to { opacity: 1; transform: translateX(0); }
|
|
1692
1735
|
}
|
|
1693
|
-
`,
|
|
1736
|
+
`,Se={primaryColor:"#6366f1",backgroundColor:"#ffffff",fontFamily:"system-ui, -apple-system, sans-serif",borderRadius:16,panelWidth:380,panelHeight:520,zIndex:9999};function Qe(t,a){const r=parseInt(t.slice(1,3),16),n=parseInt(t.slice(3,5),16),s=parseInt(t.slice(5,7),16),i=1-a;return`#${Math.round(r*i).toString(16).padStart(2,"0")}${Math.round(n*i).toString(16).padStart(2,"0")}${Math.round(s*i).toString(16).padStart(2,"0")}`}function et(t,a){const r=parseInt(t.slice(1,3),16),n=parseInt(t.slice(3,5),16),s=parseInt(t.slice(5,7),16);return`#${Math.round(r+(255-r)*a).toString(16).padStart(2,"0")}${Math.round(n+(255-n)*a).toString(16).padStart(2,"0")}${Math.round(s+(255-s)*a).toString(16).padStart(2,"0")}`}function Ie(t){const a={...Se,...t},r=a.primaryColor,n=a.backgroundColor;return[`--sa-primary: ${r}`,`--sa-bg: ${n}`,`--sa-font: ${a.fontFamily}`,`--sa-radius: ${a.borderRadius}px`,`--sa-panel-width: ${a.panelWidth}px`,`--sa-panel-height: ${a.panelHeight}px`,`--sa-z-index: ${a.zIndex}`,`--sa-primary-dark: ${Qe(r,.15)}`,`--sa-primary-light: ${et(r,.9)}`,`--sa-surface: ${n}`,"--sa-border: #e5e7eb","--sa-hover: #f4f4f5","--sa-text: #374151","--sa-text-muted: #9ca3af","--sa-text-light: #6b7280"].join("; ")}function tt(t,a){const r=typeof t=="string"?document.querySelector(t):t;if(!r)throw new Error(`Target element not found: ${t}`);const n=document.createElement("div");n.setAttribute("data-super-agent-widget","");const s=Ie(a.theme??{});if(n.setAttribute("style",`${s}; height: 100%;`),r.appendChild(n),!document.getElementById("sa-widget-styles")){const d=document.createElement("style");d.id="sa-widget-styles",d.textContent=ze,document.head.appendChild(d)}let i=Me.createRoot(n),l=!1;const c=d=>{i&&i.render(o.createElement(Ne,{...a,initialOpen:d??l,onOpenChange:p=>{l=p}}))};return c(!1),{open(){l=!0,c(!0)},close(){l=!1,c(!1)},destroy(){i&&(i.unmount(),i=null),n.remove()}}}exports.ActionBar=we;exports.ChatProvider=U;exports.ChatWidget=Ne;exports.Composer=X;exports.ErrorPart=be;exports.FullpageLayout=Ce;exports.Header=de;exports.Icon=N;exports.InterruptCard=ve;exports.Message=ye;exports.TextPart=xe;exports.ThinkingPart=me;exports.Thread=V;exports.ThreadList=ue;exports.ToolCallPart=he;exports.ToolResultPart=ge;exports.Trigger=ce;exports.WelcomeScreen=ke;exports.defaultTheme=Se;exports.ensureIconFont=le;exports.mount=tt;exports.themeToCSSVars=Ie;exports.useAutoScroll=fe;exports.useChat=te;exports.useChatContext=_;exports.useConversations=se;exports.widgetStyles=ze;
|