super-agent-sdk 1.0.1 → 1.0.2

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/dist/widget.cjs CHANGED
@@ -1,6 +1,6 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("react"),he=require("react-dom"),e=require("react/jsx-runtime");var _,A=he;if(process.env.NODE_ENV==="production")_=A.createRoot,A.hydrateRoot;else{var U=A.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;_=function(a,s){U.usingClientEntryPoint=!0;try{return A.createRoot(a,s)}finally{U.usingClientEntryPoint=!1}}}function Y(a){const{sdk:s,sessionId:l,onSessionCreated:r,onStreamStart:t,onStreamEnd:i,onError:d,onMessageSend:n}=a,[o,x]=c.useState([]),[f,m]=c.useState("ready"),[v,g]=c.useState(null),p=c.useRef(null),h=c.useRef(!1),b=c.useRef(l);b.current=l;const u=c.useCallback(j=>{if(h.current)return;h.current=!0,n==null||n(j);const L={id:`user_${Date.now()}`,role:"user",parts:[{type:"text",content:j}],timestamp:Date.now()},z=`assistant_${Date.now()}`,xe={id:z,role:"assistant",parts:[],timestamp:Date.now()};x(N=>[...N,L,xe]),m("submitted"),g(null);const F=N=>{let $="",I="",M=[],q=!1;const O=()=>{const w=[...M];if($){const E=w.findIndex(R=>R.type==="thinking");E>=0?w[E]={type:"thinking",content:$}:w.unshift({type:"thinking",content:$})}if(I){const E=w.findLastIndex(R=>R.type==="text");E>=0?w[E]={type:"text",content:I}:w.push({type:"text",content:I})}x(E=>E.map(R=>R.id===z?{...R,parts:w}:R))};p.current=s.chat({sessionId:N,message:j,stream:!0,onMessage:w=>{switch(q||(q=!0,m("streaming")),w.type){case"thinking":$+=w.content;break;case"ai":I+=w.content;break;case"tool_call":M.push({type:"tool_call",toolName:w.toolName,toolCallId:w.toolCallId,args:w.args??"{}"});break;case"tool_result":M.push({type:"tool_result",toolName:w.toolName??"",toolCallId:w.toolCallId,content:w.content}),I&&(M.push({type:"text",content:I}),I="");break}O()},onDone:w=>{(I||M.length>0||$)&&O(),w.sessionId&&w.sessionId!==b.current&&(r==null||r(w.sessionId)),i==null||i(w.sessionId||N),m("ready"),h.current=!1,p.current=null},onError:w=>{M.push({type:"error",content:w.message}),O(),g(w),m("error"),d==null||d(w),h.current=!1,p.current=null}}),t==null||t(N)};b.current?F(b.current):s.createSession().then(N=>{b.current=N,r==null||r(N),F(N)}).catch(N=>{g(N instanceof Error?N:new Error(String(N))),m("error"),d==null||d(N instanceof Error?N:new Error(String(N))),h.current=!1})},[s,r,t,i,d,n]),y=c.useCallback(()=>{var j;(j=p.current)==null||j.abort(),p.current=null,m("ready"),h.current=!1},[]),C=c.useCallback(()=>{if(h.current)return;const j=o.findLastIndex(z=>z.role==="user");if(j<0)return;const L=o[j].parts.filter(z=>z.type==="text").map(z=>z.content).join("");x(z=>z.slice(0,j)),u(L)},[o,u]);return{messages:o,status:f,error:v,sendMessage:u,stop:y,regenerate:C,setMessages:x}}const D="sa_active_conversation";function K(){try{return localStorage.getItem(D)}catch{return null}}function me(a){try{a?localStorage.setItem(D,a):localStorage.removeItem(D)}catch{}}function J(a){const{sdk:s,onConversationChange:l}=a,[r,t]=c.useState([]),[i,d]=c.useState(()=>K()),[n,o]=c.useState(!1),x=c.useRef(!0);c.useEffect(()=>()=>{x.current=!1},[]);const f=c.useCallback(u=>{d(u),me(u),u&&(l==null||l(u))},[l]),m=c.useCallback(async()=>{o(!0);try{const u=await s.listConversations({page:1,size:100});if(!x.current)return;t(u.items);const y=K();y&&!u.items.find(C=>C.sessionId===y)&&f(null)}catch{}finally{x.current&&o(!1)}},[s,f]),v=c.useCallback(async u=>(f(u),await s.getMessages(u)),[s,f]),g=c.useCallback(()=>{f(null)},[f]),p=c.useCallback(async u=>{await s.deleteConversation(u),t(y=>y.filter(C=>C.sessionId!==u)),i===u&&f(null)},[s,i,f]),h=c.useCallback(async(u,y)=>{await s.renameConversation(u,y),t(C=>C.map(j=>j.sessionId===u?{...j,title:y}:j))},[s]),{enabled:b=!0}=a;return c.useEffect(()=>{b&&m()},[b,m]),{conversations:r,activeConversationId:i,loading:n,loadConversations:m,switchConversation:v,newConversation:g,deleteConversation:p,renameConversation:h,setActiveConversationId:f}}const X=c.createContext(null);function T(){const a=c.useContext(X);if(!a)throw new Error("useChatContext must be used within ChatProvider");return a}function H({sdk:a,hooks:s,children:l}){const[r,t]=c.useState(!1),[i,d]=c.useState(!1);c.useEffect(()=>{a.getToken().then(()=>{d(!0)}).catch(()=>{d(!0)})},[a]);const n=J({sdk:a,onConversationChange:s==null?void 0:s.onConversationChange,enabled:i}),o=Y({sdk:a,sessionId:n.activeConversationId,onSessionCreated:p=>{n.setActiveConversationId(p),n.loadConversations()},onStreamStart:s==null?void 0:s.onStreamStart,onStreamEnd:p=>{var h;(h=s==null?void 0:s.onStreamEnd)==null||h.call(s,p),n.loadConversations()},onError:s==null?void 0:s.onError,onMessageSend:s==null?void 0:s.onMessageSend}),x=c.useCallback(async p=>{const h=await n.switchConversation(p);o.setMessages(h),t(!1)},[n,o]),f=c.useCallback(()=>{n.newConversation(),o.setMessages([]),t(!1)},[n,o]),m=c.useCallback(async p=>{await n.deleteConversation(p),n.activeConversationId===p&&o.setMessages([])},[n,o]);c.useEffect(()=>{n.activeConversationId&&o.messages.length===0&&a.getMessages(n.activeConversationId).then(p=>{o.setMessages(p)}).catch(()=>{})},[]);const v=c.useCallback(()=>{t(p=>!p)},[]),g={status:o.status,error:o.error,messages:o.messages,conversations:n.conversations,activeConversationId:n.activeConversationId,isThreadListOpen:r,sendMessage:o.sendMessage,stop:o.stop,regenerate:o.regenerate,switchConversation:x,newConversation:f,deleteConversation:m,renameConversation:n.renameConversation,toggleThreadList:v,feedback:(p,h)=>{a.feedback(p,h)}};return e.jsx(X.Provider,{value:g,children:l})}function k({name:a,size:s=16,color:l="currentColor",className:r,style:t}){return e.jsx("svg",{className:r,style:{width:s,height:s,fill:l,verticalAlign:"middle",...t},"aria-hidden":"true",children:e.jsx("use",{href:`#icon-${a}`})})}function G({isOpen:a,onClick:s}){return e.jsx("button",{className:`sa-trigger ${a?"sa-trigger-open":""}`,onClick:s,children:a?e.jsx(k,{name:"x",size:24,color:"#fff"}):e.jsx(k,{name:"message-square",size:24,color:"#fff"})})}function Z({title:a,onClose:s,onToggleThreadList:l,showClose:r=!0}){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:[e.jsx("button",{className:"sa-header-btn",onClick:l,title:"会话列表",children:e.jsx(k,{name:"menu",size:16})}),r&&e.jsx("button",{className:"sa-header-btn",onClick:s,title:"关闭",children:e.jsx(k,{name:"x",size:16})})]})]})}function Q(){const{conversations:a,activeConversationId:s,switchConversation:l,newConversation:r,deleteConversation:t,renameConversation:i,isThreadListOpen:d,toggleThreadList:n}=T(),[o,x]=c.useState(null),[f,m]=c.useState(""),v=c.useCallback((p,h)=>{x(p),m(h??"")},[]),g=c.useCallback(p=>{f.trim()&&i(p,f.trim()),x(null)},[f,i]);return d?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"sa-thread-list-overlay",onClick:n}),e.jsxs("div",{className:"sa-thread-list",children:[e.jsxs("div",{className:"sa-thread-list-header",children:[e.jsx("span",{children:"会话列表"}),e.jsxs("button",{className:"sa-thread-list-new",onClick:r,children:[e.jsx(k,{name:"plus",size:14})," 新会话"]})]}),e.jsxs("div",{className:"sa-thread-list-items",children:[a.length===0&&e.jsx("div",{className:"sa-thread-list-empty",children:"暂无会话"}),a.map(p=>e.jsxs("div",{className:`sa-thread-item ${p.sessionId===s?"active":""}`,onClick:()=>l(p.sessionId),children:[o===p.sessionId?e.jsx("input",{className:"sa-thread-item-edit",value:f,onChange:h=>m(h.target.value),onBlur:()=>g(p.sessionId),onKeyDown:h=>{h.key==="Enter"&&g(p.sessionId),h.key==="Escape"&&x(null)},onClick:h=>h.stopPropagation(),autoFocus:!0}):e.jsx("span",{className:"sa-thread-item-title",onDoubleClick:h=>{h.stopPropagation(),v(p.sessionId,p.title)},children:p.title||"新会话"}),e.jsx("button",{className:"sa-thread-item-delete",onClick:h=>{h.stopPropagation(),t(p.sessionId)},title:"删除",children:e.jsx(k,{name:"trash2",size:14})})]},p.sessionId))]})]})]}):null}function ee(a){const s=c.useRef(null),l=c.useRef(!1),r=c.useRef(0),t=c.useCallback(()=>{const d=s.current;if(!d)return;const{scrollTop:n,scrollHeight:o,clientHeight:x}=d,f=o-n-x<40;n<r.current&&!f&&(l.current=!0),f&&(l.current=!1),r.current=n},[]);c.useEffect(()=>{const d=s.current;if(d)return d.addEventListener("scroll",t,{passive:!0}),()=>d.removeEventListener("scroll",t)},[t]),c.useEffect(()=>{if(l.current)return;const d=s.current;d&&requestAnimationFrame(()=>{d.scrollTop=d.scrollHeight})},a);const i=c.useCallback(()=>{l.current=!1;const d=s.current;d&&(d.scrollTop=d.scrollHeight)},[]);return{containerRef:s,scrollToBottom:i}}function S(a){return a.value++}function P(a,s){const l=[],r=[],t=()=>{r.length>0&&(l.push(r.join("")),r.length=0)};let i=0;for(;i<a.length;){const d=a[i];if(d==="`"){const n=a.indexOf("`",i+1);if(n!==-1){t(),l.push(e.jsx("code",{className:"sa-inline-code",children:a.slice(i+1,n)},S(s))),i=n+1;continue}}else if(d==="*"&&a[i+1]==="*"){const n=a.indexOf("**",i+2);if(n!==-1){t(),l.push(e.jsx("strong",{children:P(a.slice(i+2,n),s)},S(s))),i=n+2;continue}}else if(d==="*"){const n=a.indexOf("*",i+1);if(n!==-1){t(),l.push(e.jsx("em",{children:P(a.slice(i+1,n),s)},S(s))),i=n+1;continue}}else if(d==="["){const n=a.indexOf("]",i+1);if(n!==-1&&a[n+1]==="("){const o=a.indexOf(")",n+2);if(o!==-1){const x=a.slice(i+1,n),f=a.slice(n+2,o);t(),l.push(e.jsx("a",{href:f,target:"_blank",rel:"noreferrer",children:P(x,s)},S(s))),i=o+1;continue}}}r.push(d),i++}return t(),l}function ge(a,s){const l=[],r=a.split(`
2
- `);let t=0;const i=o=>o.trim()==="",d=o=>/^#{1,6}\s+\S/.test(o.trim()),n=o=>/^(?:[-*+]|\d+[.)])\s+\S/.test(o.trim());for(;t<r.length;){const o=r[t],x=o.trim();if(i(o)){t++;continue}if(d(o)){const m=x.match(/^(#{1,6})\s+(.*)$/);if(m){const g=`h${Math.min(m[1].length,6)}`;l.push(c.createElement(g,{key:S(s)},P(m[2],s)))}t++;continue}if(n(o)){const m=/^\d+[.)]/.test(x),v=[];for(;t<r.length&&n(r[t]);){const g=r[t].trim().match(/^(?:[-*+]|\d+[.)])\s+(.*)$/);g&&v.push(e.jsx("li",{children:P(g[1],s)},S(s))),t++}l.push(m?e.jsx("ol",{className:"sa-list",children:v},S(s)):e.jsx("ul",{className:"sa-list",children:v},S(s)));continue}const f=[];for(;t<r.length&&!i(r[t])&&!d(r[t])&&!n(r[t]);)f.push(r[t]),t++;l.push(e.jsx("p",{className:"sa-text-paragraph",children:P(f.join(" "),s)},S(s)))}return l}function be(a){const s={value:0},l=[],r=a.split("```");for(let t=0;t<r.length;t++)if(t%2===0)l.push(...ge(r[t],s));else if(r[t].trim()!==""){let i=r[t].replace(/^\r?\n/,"");i=i.replace(/^[a-zA-Z0-9_+#.-]+\r?\n/,""),i=i.replace(/\r?\n$/,""),l.push(e.jsx("pre",{className:"sa-code-block",children:e.jsx("code",{children:i})},S(s)))}return l}function se({content:a}){return e.jsx("div",{className:"sa-text-part",children:be(a)})}function ae({content:a}){const[s,l]=c.useState(!1);return e.jsxs("div",{className:"sa-thinking-part",onClick:()=>l(!s),children:[e.jsxs("div",{className:"sa-thinking-header",children:[e.jsx("span",{className:"sa-thinking-icon",children:e.jsx(k,{name:"brain",size:14})}),e.jsx("span",{className:"sa-thinking-label",children:"思考过程"}),e.jsx("span",{className:`sa-thinking-arrow ${s?"expanded":""}`,children:e.jsx(k,{name:"chevron-right",size:12})})]}),s&&e.jsx("div",{className:"sa-thinking-content",children:a})]})}function te({toolName:a,toolCallId:s,args:l}){const[r,t]=c.useState(!1);let i=l;try{i=JSON.stringify(JSON.parse(l),null,2)}catch{}return e.jsxs("div",{className:"sa-tool-call-part",children:[e.jsxs("div",{className:"sa-tool-call-header",onClick:()=>t(!r),children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(k,{name:"zap",size:14})}),e.jsxs("span",{className:"sa-tool-name",children:["调用工具: ",a]}),e.jsx("span",{className:`sa-thinking-arrow ${r?"expanded":""}`,children:e.jsx(k,{name:"chevron-right",size:12})})]}),r&&e.jsx("pre",{className:"sa-tool-args",children:i})]})}function re({toolName:a,toolCallId:s,content:l}){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(k,{name:"circle-check",size:14,color:"#10b981"})}),e.jsx("span",{className:"sa-tool-result-label",children:a||"工具返回"})]}),e.jsx("div",{className:"sa-tool-result-content",children:l})]})}function ne({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(k,{name:"triangle-alert",size:14})}),e.jsx("span",{children:a})]}),s&&e.jsx("button",{className:"sa-error-retry",onClick:s,children:"重试"})]})}function ie({message:a,onCopy:s,onRegenerate:l,onFeedback:r}){const{status:t}=T(),[i,d]=c.useState(null),[n,o]=c.useState(!1);if(t!=="ready"&&t!=="error")return null;const x=c.useCallback(()=>{s(),o(!0),setTimeout(()=>o(!1),2e3)},[s]),f=c.useCallback(m=>{const v=i===m?null:m;d(v),v&&(r==null||r(a.id,v))},[i,a.id,r]);return e.jsxs("div",{className:"sa-action-bar",children:[e.jsxs("button",{className:"sa-action-btn",onClick:x,title:"复制",children:[e.jsx(k,{name:n?"circle-check":"copy",size:14})," ",n?"已复制":"复制"]}),e.jsxs("button",{className:"sa-action-btn",onClick:l,title:"重新生成",children:[e.jsx(k,{name:"refresh-cw",size:14})," 重新生成"]}),e.jsx("span",{className:"sa-action-divider"}),e.jsx("button",{className:`sa-action-btn ${i==="like"?"sa-action-btn-active":""}`,onClick:()=>f("like"),title:"有用",children:e.jsx(k,{name:"thumbs-up",size:14})}),e.jsx("button",{className:`sa-action-btn ${i==="dislike"?"sa-action-btn-active":""}`,onClick:()=>f("dislike"),title:"无用",children:e.jsx(k,{name:"thumbs-down",size:14})})]})}function V({src:a,role:s}){const l=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:l})}function oe({message:a,isStreaming:s,isLast:l,slots:r,avatar:t}){const{regenerate:i,feedback:d}=T(),n=c.useCallback(()=>{var u;const b=a.parts.filter(y=>y.type==="text").map(y=>y.content).join(`
3
- `);(u=navigator.clipboard)==null||u.writeText(b).catch(()=>{})},[a]),o=a.role==="user",x=r.Message;if(x)return e.jsx(x,{message:a,isStreaming:s,isLast:l,avatar:t,slots:r,onCopy:n,onRegenerate:i,onFeedback:d});const f=r.TextPart??se,m=r.ThinkingPart??ae,v=r.ToolCallPart??te,g=r.ToolResultPart??re,p=r.ErrorPart??ne,h=r.ActionBar??ie;return e.jsxs("div",{className:`sa-message ${o?"sa-message-user":"sa-message-assistant"}`,children:[!o&&e.jsx(V,{src:t==null?void 0:t.assistant,role:"assistant"}),e.jsxs("div",{className:"sa-message-body",children:[a.parts.map((b,u)=>{switch(b.type){case"text":return e.jsx(f,{content:b.content},u);case"thinking":return e.jsx(m,{content:b.content},u);case"tool_call":return e.jsx(v,{toolName:b.toolName,toolCallId:b.toolCallId,args:b.args},u);case"tool_result":return e.jsx(g,{toolName:b.toolName,toolCallId:b.toolCallId,content:b.content},u);case"error":return e.jsx(p,{content:b.content,onRetry:i},u);default:return null}}),s&&l&&!o&&e.jsx("span",{className:"sa-streaming-cursor",children:"|"}),!o&&!s&&a.parts.length>0&&e.jsx(h,{message:a,onCopy:n,onRegenerate:i,onFeedback:d})]}),o&&e.jsx(V,{src:t==null?void 0:t.user,role:"user"})]})}function le({message:a,suggestedPrompts:s,onPromptClick:l}){return e.jsxs("div",{className:"sa-welcome",children:[e.jsx("div",{className:"sa-welcome-icon",children:e.jsx(k,{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((r,t)=>e.jsx("button",{className:"sa-welcome-prompt",onClick:()=>l(r),children:r},t))})]})}function B({slots:a,welcomeMessage:s,suggestedPrompts:l,avatar:r}){const{messages:t,status:i,sendMessage:d}=T(),{containerRef:n}=ee([t]),o=i==="streaming",x=a.WelcomeScreen??le;return t.length===0?e.jsx("div",{className:"sa-thread",ref:n,children:e.jsx(x,{message:s,suggestedPrompts:l,onPromptClick:d})}):e.jsx("div",{className:"sa-thread",ref:n,children:t.map((f,m)=>e.jsx(oe,{message:f,isStreaming:o,isLast:m===t.length-1,slots:a,avatar:r},f.id))})}function W(){const{status:a,sendMessage:s,stop:l}=T(),[r,t]=c.useState(""),i=c.useRef(null),d=a==="streaming"||a==="submitted",n=c.useCallback(()=>{const f=r.trim();!f||d||(s(f),t(""),i.current&&(i.current.style.height="auto"))},[r,d,s]),o=c.useCallback(f=>{f.key==="Enter"&&!f.shiftKey&&(f.preventDefault(),n())},[n]),x=c.useCallback(f=>{t(f.target.value);const m=f.target;m.style.height="auto",m.style.height=Math.min(m.scrollHeight,120)+"px"},[]);return e.jsxs("div",{className:"sa-composer",children:[e.jsx("textarea",{ref:i,className:"sa-composer-input",value:r,onChange:x,onKeyDown:o,placeholder:"输入消息...",rows:1,disabled:a==="submitted"}),d?e.jsx("button",{className:"sa-composer-stop",onClick:l,title:"停止",children:e.jsx(k,{name:"circle-stop",size:16,color:"#fff"})}):e.jsx("button",{className:"sa-composer-send",onClick:n,disabled:!r.trim(),title:"发送",children:e.jsx(k,{name:"arrow-up",size:16,color:"#fff"})})]})}function ve({isOpen:a,onClose:s,title:l,slots:r,welcomeMessage:t,suggestedPrompts:i,avatar:d}){const{status:n,sendMessage:o,stop:x,toggleThreadList:f,conversations:m,activeConversationId:v,switchConversation:g,newConversation:p,deleteConversation:h,renameConversation:b,isThreadListOpen:u}=T();if(!a)return null;const y=r.Header,C=r.Composer,j=r.ThreadList;return e.jsxs("div",{className:"sa-panel",children:[y?e.jsx(y,{title:l,onClose:s,onToggleThreadList:f}):e.jsx(Z,{title:l,onClose:s,onToggleThreadList:f}),j&&u?e.jsx(j,{conversations:m,activeSessionId:v,onSwitch:g,onNew:p,onDelete:h,onRename:b}):e.jsx(Q,{}),e.jsx(B,{slots:r,welcomeMessage:t,suggestedPrompts:i,avatar:d}),C?e.jsx(C,{status:n,onSend:o,onStop:x}):e.jsx(W,{})]})}function ye({conversations:a,activeSessionId:s,onSwitch:l,onNew:r,onDelete:t,isCollapsed:i,onToggleCollapse:d}){const[n,o]=c.useState(null),[x,f]=c.useState(null),m=c.useCallback((g,p)=>{p.stopPropagation(),x===g?(t(g),f(null)):(f(g),setTimeout(()=>f(null),3e3))},[x,t]),v=(()=>{const g=Date.now(),p=[],h=[],b=[];for(const y of a){const C=g-new Date(y.updateTime).getTime();C<864e5?p.push(y):C<6048e5?h.push(y):b.push(y)}const u=[];return p.length&&u.push({label:"今天",items:p}),h.length&&u.push({label:"最近 7 天",items:h}),b.length&&u.push({label:"更早",items:b}),u})();return i?e.jsxs("div",{className:"sa-fp-sidebar sa-fp-sidebar-collapsed",children:[e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:d,title:"展开",children:e.jsx(k,{name:"panel-left",size:18})}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:r,title:"新会话",style:{marginTop:4},children:e.jsx(k,{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:r,children:[e.jsx(k,{name:"plus",size:15})," ",e.jsx("span",{children:"新会话"})]}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:d,title:"收起侧边栏",children:e.jsx(k,{name:"panel-left-close",size:18})})]}),e.jsxs("div",{className:"sa-fp-sidebar-list",children:[a.length===0&&e.jsx("div",{className:"sa-fp-sidebar-empty",children:"开始你的第一个对话"}),v.map(g=>e.jsxs("div",{children:[e.jsx("div",{className:"sa-fp-sidebar-group",children:g.label}),g.items.map(p=>{const h=p.sessionId===s,b=n===p.sessionId,u=x===p.sessionId;return e.jsxs("div",{className:`sa-fp-sidebar-item ${h?"active":""}`,onClick:()=>l(p.sessionId),onMouseEnter:()=>o(p.sessionId),onMouseLeave:()=>o(null),children:[e.jsx("span",{className:"sa-fp-sidebar-item-title",children:p.title||"新会话"}),b&&e.jsx("button",{className:`sa-fp-sidebar-item-action ${u?"danger":""}`,onClick:y=>m(p.sessionId,y),title:u?"确认删除":"删除",children:e.jsx(k,{name:u?"x":"trash2",size:14})})]},p.sessionId)})]},g.label))]})]})}function ce({title:a,slots:s,welcomeMessage:l,suggestedPrompts:r,sidebarDefaultOpen:t=!0,onClose:i,avatar:d}){const{status:n,sendMessage:o,stop:x,conversations:f,activeConversationId:m,switchConversation:v,newConversation:g,deleteConversation:p,renameConversation:h}=T(),[b,u]=c.useState(t),y=c.useCallback(()=>u(L=>!L),[]),C=s.ThreadList,j=s.Composer;return e.jsxs("div",{className:"sa-fullpage",children:[C?e.jsx("div",{className:`sa-fp-sidebar-wrapper ${b?"":"sa-fp-sidebar-wrapper-collapsed"}`,children:e.jsx(C,{conversations:f,activeSessionId:m,onSwitch:v,onNew:g,onDelete:p,onRename:h})}):e.jsx(ye,{conversations:f,activeSessionId:m,onSwitch:v,onNew:g,onDelete:p,onRename:h,isCollapsed:!b,onToggleCollapse:y}),e.jsxs("div",{className:"sa-fp-main",children:[e.jsxs("div",{className:"sa-fp-header",children:[!b&&!C&&e.jsx("button",{className:"sa-fp-header-btn",onClick:y,title:"展开侧边栏",children:e.jsx(k,{name:"panel-left",size:18})}),e.jsx("div",{className:"sa-fp-header-title",children:a}),e.jsx("div",{className:"sa-fp-header-actions",children:i&&e.jsx("button",{className:"sa-fp-header-btn",onClick:i,title:"关闭",children:e.jsx(k,{name:"x",size:18})})})]}),e.jsx(B,{slots:s,welcomeMessage:l,suggestedPrompts:r,avatar:d}),j?e.jsx(j,{status:n,onSend:o,onStop:x}):e.jsx(W,{})]})]})}function de(a){const{sdk:s,mode:l="floating",slots:r={},hooks:t={},welcomeMessage:i,suggestedPrompts:d,title:n="AI Assistant",initialOpen:o=!1,onOpenChange:x,sidebarDefaultOpen:f,avatar:m}=a,[v,g]=c.useState(o);c.useEffect(()=>{g(o)},[o]);const p=c.useCallback(()=>{var y,C;const u=!v;g(u),x==null||x(u),u?(y=t.onOpen)==null||y.call(t):(C=t.onClose)==null||C.call(t)},[v,t,x]),h=c.useCallback(()=>{var u;g(!1),x==null||x(!1),(u=t.onClose)==null||u.call(t)},[t,x]),b=r.Trigger??G;return l==="fullpage"?v?e.jsx(H,{sdk:s,hooks:t,children:e.jsx(ce,{title:n,slots:r,welcomeMessage:i,suggestedPrompts:d,sidebarDefaultOpen:f,onClose:h,avatar:m})}):null:e.jsxs(H,{sdk:s,hooks:t,children:[e.jsx(b,{isOpen:v,onClick:p}),e.jsx(ve,{isOpen:v,onClose:h,title:n,slots:r,welcomeMessage:i,suggestedPrompts:d,avatar:m})]})}const pe=`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("react"),fe=require("react-dom/client"),e=require("react/jsx-runtime");function K(a){const{sdk:s,sessionId:l,onSessionCreated:r,onStreamStart:t,onStreamEnd:i,onError:d,onMessageSend:n}=a,[o,x]=c.useState([]),[f,m]=c.useState("ready"),[v,g]=c.useState(null),p=c.useRef(null),h=c.useRef(!1),b=c.useRef(l);b.current=l;const u=c.useCallback(j=>{if(h.current)return;h.current=!0,n==null||n(j);const L={id:`user_${Date.now()}`,role:"user",parts:[{type:"text",content:j}],timestamp:Date.now()},z=`assistant_${Date.now()}`,pe={id:z,role:"assistant",parts:[],timestamp:Date.now()};x(N=>[...N,L,pe]),m("submitted"),g(null);const _=N=>{let A="",I="",M=[],W=!1;const $=()=>{const y=[...M];if(A){const E=y.findIndex(R=>R.type==="thinking");E>=0?y[E]={type:"thinking",content:A}:y.unshift({type:"thinking",content:A})}if(I){const E=y.findLastIndex(R=>R.type==="text");E>=0?y[E]={type:"text",content:I}:y.push({type:"text",content:I})}x(E=>E.map(R=>R.id===z?{...R,parts:y}:R))};p.current=s.chat({sessionId:N,message:j,stream:!0,onMessage:y=>{switch(W||(W=!0,m("streaming")),y.type){case"thinking":A+=y.content;break;case"ai":I+=y.content;break;case"tool_call":M.push({type:"tool_call",toolName:y.toolName,toolCallId:y.toolCallId,args:y.args??"{}"});break;case"tool_result":M.push({type:"tool_result",toolName:y.toolName??"",toolCallId:y.toolCallId,content:y.content}),I&&(M.push({type:"text",content:I}),I="");break}$()},onDone:y=>{(I||M.length>0||A)&&$(),y.sessionId&&y.sessionId!==b.current&&(r==null||r(y.sessionId)),i==null||i(y.sessionId||N),m("ready"),h.current=!1,p.current=null},onError:y=>{M.push({type:"error",content:y.message}),$(),g(y),m("error"),d==null||d(y),h.current=!1,p.current=null}}),t==null||t(N)};b.current?_(b.current):s.createSession().then(N=>{b.current=N,r==null||r(N),_(N)}).catch(N=>{g(N instanceof Error?N:new Error(String(N))),m("error"),d==null||d(N instanceof Error?N:new Error(String(N))),h.current=!1})},[s,r,t,i,d,n]),k=c.useCallback(()=>{var j;(j=p.current)==null||j.abort(),p.current=null,m("ready"),h.current=!1},[]),C=c.useCallback(()=>{if(h.current)return;const j=o.findLastIndex(z=>z.role==="user");if(j<0)return;const L=o[j].parts.filter(z=>z.type==="text").map(z=>z.content).join("");x(z=>z.slice(0,j)),u(L)},[o,u]);return{messages:o,status:f,error:v,sendMessage:u,stop:k,regenerate:C,setMessages:x}}const D="sa_active_conversation";function F(){try{return localStorage.getItem(D)}catch{return null}}function ue(a){try{a?localStorage.setItem(D,a):localStorage.removeItem(D)}catch{}}function U(a){const{sdk:s,onConversationChange:l}=a,[r,t]=c.useState([]),[i,d]=c.useState(()=>F()),[n,o]=c.useState(!1),x=c.useRef(!0);c.useEffect(()=>()=>{x.current=!1},[]);const f=c.useCallback(u=>{d(u),ue(u),u&&(l==null||l(u))},[l]),m=c.useCallback(async()=>{o(!0);try{const u=await s.listConversations({page:1,size:100});if(!x.current)return;t(u.items);const k=F();k&&!u.items.find(C=>C.sessionId===k)&&f(null)}catch{}finally{x.current&&o(!1)}},[s,f]),v=c.useCallback(async u=>(f(u),await s.getMessages(u)),[s,f]),g=c.useCallback(()=>{f(null)},[f]),p=c.useCallback(async u=>{await s.deleteConversation(u),t(k=>k.filter(C=>C.sessionId!==u)),i===u&&f(null)},[s,i,f]),h=c.useCallback(async(u,k)=>{await s.renameConversation(u,k),t(C=>C.map(j=>j.sessionId===u?{...j,title:k}:j))},[s]),{enabled:b=!0}=a;return c.useEffect(()=>{b&&m()},[b,m]),{conversations:r,activeConversationId:i,loading:n,loadConversations:m,switchConversation:v,newConversation:g,deleteConversation:p,renameConversation:h,setActiveConversationId:f}}const V=c.createContext(null);function T(){const a=c.useContext(V);if(!a)throw new Error("useChatContext must be used within ChatProvider");return a}function O({sdk:a,hooks:s,children:l}){const[r,t]=c.useState(!1),[i,d]=c.useState(!1);c.useEffect(()=>{a.getToken().then(()=>{d(!0)}).catch(()=>{d(!0)})},[a]);const n=U({sdk:a,onConversationChange:s==null?void 0:s.onConversationChange,enabled:i}),o=K({sdk:a,sessionId:n.activeConversationId,onSessionCreated:p=>{n.setActiveConversationId(p),n.loadConversations()},onStreamStart:s==null?void 0:s.onStreamStart,onStreamEnd:p=>{var h;(h=s==null?void 0:s.onStreamEnd)==null||h.call(s,p),n.loadConversations()},onError:s==null?void 0:s.onError,onMessageSend:s==null?void 0:s.onMessageSend}),x=c.useCallback(async p=>{const h=await n.switchConversation(p);o.setMessages(h),t(!1)},[n,o]),f=c.useCallback(()=>{n.newConversation(),o.setMessages([]),t(!1)},[n,o]),m=c.useCallback(async p=>{await n.deleteConversation(p),n.activeConversationId===p&&o.setMessages([])},[n,o]);c.useEffect(()=>{n.activeConversationId&&o.messages.length===0&&a.getMessages(n.activeConversationId).then(p=>{o.setMessages(p)}).catch(()=>{})},[]);const v=c.useCallback(()=>{t(p=>!p)},[]),g={status:o.status,error:o.error,messages:o.messages,conversations:n.conversations,activeConversationId:n.activeConversationId,isThreadListOpen:r,sendMessage:o.sendMessage,stop:o.stop,regenerate:o.regenerate,switchConversation:x,newConversation:f,deleteConversation:m,renameConversation:n.renameConversation,toggleThreadList:v,feedback:(p,h)=>{a.feedback(p,h)}};return e.jsx(V.Provider,{value:g,children:l})}function w({name:a,size:s=16,color:l="currentColor",className:r,style:t}){return e.jsx("svg",{className:r,style:{width:s,height:s,fill:l,verticalAlign:"middle",...t},"aria-hidden":"true",children:e.jsx("use",{href:`#icon-${a}`})})}function Y({isOpen:a,onClick:s}){return e.jsx("button",{className:`sa-trigger ${a?"sa-trigger-open":""}`,onClick:s,children:a?e.jsx(w,{name:"x",size:24,color:"#fff"}):e.jsx(w,{name:"message-square",size:24,color:"#fff"})})}function J({title:a,onClose:s,onToggleThreadList:l,showClose:r=!0}){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:[e.jsx("button",{className:"sa-header-btn",onClick:l,title:"会话列表",children:e.jsx(w,{name:"menu",size:16})}),r&&e.jsx("button",{className:"sa-header-btn",onClick:s,title:"关闭",children:e.jsx(w,{name:"x",size:16})})]})]})}function X(){const{conversations:a,activeConversationId:s,switchConversation:l,newConversation:r,deleteConversation:t,renameConversation:i,isThreadListOpen:d,toggleThreadList:n}=T(),[o,x]=c.useState(null),[f,m]=c.useState(""),v=c.useCallback((p,h)=>{x(p),m(h??"")},[]),g=c.useCallback(p=>{f.trim()&&i(p,f.trim()),x(null)},[f,i]);return d?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"sa-thread-list-overlay",onClick:n}),e.jsxs("div",{className:"sa-thread-list",children:[e.jsxs("div",{className:"sa-thread-list-header",children:[e.jsx("span",{children:"会话列表"}),e.jsxs("button",{className:"sa-thread-list-new",onClick:r,children:[e.jsx(w,{name:"plus",size:14})," 新会话"]})]}),e.jsxs("div",{className:"sa-thread-list-items",children:[a.length===0&&e.jsx("div",{className:"sa-thread-list-empty",children:"暂无会话"}),a.map(p=>e.jsxs("div",{className:`sa-thread-item ${p.sessionId===s?"active":""}`,onClick:()=>l(p.sessionId),children:[o===p.sessionId?e.jsx("input",{className:"sa-thread-item-edit",value:f,onChange:h=>m(h.target.value),onBlur:()=>g(p.sessionId),onKeyDown:h=>{h.key==="Enter"&&g(p.sessionId),h.key==="Escape"&&x(null)},onClick:h=>h.stopPropagation(),autoFocus:!0}):e.jsx("span",{className:"sa-thread-item-title",onDoubleClick:h=>{h.stopPropagation(),v(p.sessionId,p.title)},children:p.title||"新会话"}),e.jsx("button",{className:"sa-thread-item-delete",onClick:h=>{h.stopPropagation(),t(p.sessionId)},title:"删除",children:e.jsx(w,{name:"trash2",size:14})})]},p.sessionId))]})]})]}):null}function G(a){const s=c.useRef(null),l=c.useRef(!1),r=c.useRef(0),t=c.useCallback(()=>{const d=s.current;if(!d)return;const{scrollTop:n,scrollHeight:o,clientHeight:x}=d,f=o-n-x<40;n<r.current&&!f&&(l.current=!0),f&&(l.current=!1),r.current=n},[]);c.useEffect(()=>{const d=s.current;if(d)return d.addEventListener("scroll",t,{passive:!0}),()=>d.removeEventListener("scroll",t)},[t]),c.useEffect(()=>{if(l.current)return;const d=s.current;d&&requestAnimationFrame(()=>{d.scrollTop=d.scrollHeight})},a);const i=c.useCallback(()=>{l.current=!1;const d=s.current;d&&(d.scrollTop=d.scrollHeight)},[]);return{containerRef:s,scrollToBottom:i}}function S(a){return a.value++}function P(a,s){const l=[],r=[],t=()=>{r.length>0&&(l.push(r.join("")),r.length=0)};let i=0;for(;i<a.length;){const d=a[i];if(d==="`"){const n=a.indexOf("`",i+1);if(n!==-1){t(),l.push(e.jsx("code",{className:"sa-inline-code",children:a.slice(i+1,n)},S(s))),i=n+1;continue}}else if(d==="*"&&a[i+1]==="*"){const n=a.indexOf("**",i+2);if(n!==-1){t(),l.push(e.jsx("strong",{children:P(a.slice(i+2,n),s)},S(s))),i=n+2;continue}}else if(d==="*"){const n=a.indexOf("*",i+1);if(n!==-1){t(),l.push(e.jsx("em",{children:P(a.slice(i+1,n),s)},S(s))),i=n+1;continue}}else if(d==="["){const n=a.indexOf("]",i+1);if(n!==-1&&a[n+1]==="("){const o=a.indexOf(")",n+2);if(o!==-1){const x=a.slice(i+1,n),f=a.slice(n+2,o);t(),l.push(e.jsx("a",{href:f,target:"_blank",rel:"noreferrer",children:P(x,s)},S(s))),i=o+1;continue}}}r.push(d),i++}return t(),l}function xe(a,s){const l=[],r=a.split(`
2
+ `);let t=0;const i=o=>o.trim()==="",d=o=>/^#{1,6}\s+\S/.test(o.trim()),n=o=>/^(?:[-*+]|\d+[.)])\s+\S/.test(o.trim());for(;t<r.length;){const o=r[t],x=o.trim();if(i(o)){t++;continue}if(d(o)){const m=x.match(/^(#{1,6})\s+(.*)$/);if(m){const g=`h${Math.min(m[1].length,6)}`;l.push(c.createElement(g,{key:S(s)},P(m[2],s)))}t++;continue}if(n(o)){const m=/^\d+[.)]/.test(x),v=[];for(;t<r.length&&n(r[t]);){const g=r[t].trim().match(/^(?:[-*+]|\d+[.)])\s+(.*)$/);g&&v.push(e.jsx("li",{children:P(g[1],s)},S(s))),t++}l.push(m?e.jsx("ol",{className:"sa-list",children:v},S(s)):e.jsx("ul",{className:"sa-list",children:v},S(s)));continue}const f=[];for(;t<r.length&&!i(r[t])&&!d(r[t])&&!n(r[t]);)f.push(r[t]),t++;l.push(e.jsx("p",{className:"sa-text-paragraph",children:P(f.join(" "),s)},S(s)))}return l}function he(a){const s={value:0},l=[],r=a.split("```");for(let t=0;t<r.length;t++)if(t%2===0)l.push(...xe(r[t],s));else if(r[t].trim()!==""){let i=r[t].replace(/^\r?\n/,"");i=i.replace(/^[a-zA-Z0-9_+#.-]+\r?\n/,""),i=i.replace(/\r?\n$/,""),l.push(e.jsx("pre",{className:"sa-code-block",children:e.jsx("code",{children:i})},S(s)))}return l}function Z({content:a}){return e.jsx("div",{className:"sa-text-part",children:he(a)})}function Q({content:a}){const[s,l]=c.useState(!1);return e.jsxs("div",{className:"sa-thinking-part",onClick:()=>l(!s),children:[e.jsxs("div",{className:"sa-thinking-header",children:[e.jsx("span",{className:"sa-thinking-icon",children:e.jsx(w,{name:"brain",size:14})}),e.jsx("span",{className:"sa-thinking-label",children:"思考过程"}),e.jsx("span",{className:`sa-thinking-arrow ${s?"expanded":""}`,children:e.jsx(w,{name:"chevron-right",size:12})})]}),s&&e.jsx("div",{className:"sa-thinking-content",children:a})]})}function ee({toolName:a,toolCallId:s,args:l}){const[r,t]=c.useState(!1);let i=l;try{i=JSON.stringify(JSON.parse(l),null,2)}catch{}return e.jsxs("div",{className:"sa-tool-call-part",children:[e.jsxs("div",{className:"sa-tool-call-header",onClick:()=>t(!r),children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(w,{name:"zap",size:14})}),e.jsxs("span",{className:"sa-tool-name",children:["调用工具: ",a]}),e.jsx("span",{className:`sa-thinking-arrow ${r?"expanded":""}`,children:e.jsx(w,{name:"chevron-right",size:12})})]}),r&&e.jsx("pre",{className:"sa-tool-args",children:i})]})}function se({toolName:a,toolCallId:s,content:l}){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(w,{name:"circle-check",size:14,color:"#10b981"})}),e.jsx("span",{className:"sa-tool-result-label",children:a||"工具返回"})]}),e.jsx("div",{className:"sa-tool-result-content",children:l})]})}function ae({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(w,{name:"triangle-alert",size:14})}),e.jsx("span",{children:a})]}),s&&e.jsx("button",{className:"sa-error-retry",onClick:s,children:"重试"})]})}function te({message:a,onCopy:s,onRegenerate:l,onFeedback:r}){const{status:t}=T(),[i,d]=c.useState(null),[n,o]=c.useState(!1);if(t!=="ready"&&t!=="error")return null;const x=c.useCallback(()=>{s(),o(!0),setTimeout(()=>o(!1),2e3)},[s]),f=c.useCallback(m=>{const v=i===m?null:m;d(v),v&&(r==null||r(a.id,v))},[i,a.id,r]);return e.jsxs("div",{className:"sa-action-bar",children:[e.jsxs("button",{className:"sa-action-btn",onClick:x,title:"复制",children:[e.jsx(w,{name:n?"circle-check":"copy",size:14})," ",n?"已复制":"复制"]}),e.jsxs("button",{className:"sa-action-btn",onClick:l,title:"重新生成",children:[e.jsx(w,{name:"refresh-cw",size:14})," 重新生成"]}),e.jsx("span",{className:"sa-action-divider"}),e.jsx("button",{className:`sa-action-btn ${i==="like"?"sa-action-btn-active":""}`,onClick:()=>f("like"),title:"有用",children:e.jsx(w,{name:"thumbs-up",size:14})}),e.jsx("button",{className:`sa-action-btn ${i==="dislike"?"sa-action-btn-active":""}`,onClick:()=>f("dislike"),title:"无用",children:e.jsx(w,{name:"thumbs-down",size:14})})]})}function q({src:a,role:s}){const l=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:l})}function re({message:a,isStreaming:s,isLast:l,slots:r,avatar:t}){const{regenerate:i,feedback:d}=T(),n=c.useCallback(()=>{var u;const b=a.parts.filter(k=>k.type==="text").map(k=>k.content).join(`
3
+ `);(u=navigator.clipboard)==null||u.writeText(b).catch(()=>{})},[a]),o=a.role==="user",x=r.Message;if(x)return e.jsx(x,{message:a,isStreaming:s,isLast:l,avatar:t,slots:r,onCopy:n,onRegenerate:i,onFeedback:d});const f=r.TextPart??Z,m=r.ThinkingPart??Q,v=r.ToolCallPart??ee,g=r.ToolResultPart??se,p=r.ErrorPart??ae,h=r.ActionBar??te;return e.jsxs("div",{className:`sa-message ${o?"sa-message-user":"sa-message-assistant"}`,children:[!o&&e.jsx(q,{src:t==null?void 0:t.assistant,role:"assistant"}),e.jsxs("div",{className:"sa-message-body",children:[a.parts.map((b,u)=>{switch(b.type){case"text":return e.jsx(f,{content:b.content},u);case"thinking":return e.jsx(m,{content:b.content},u);case"tool_call":return e.jsx(v,{toolName:b.toolName,toolCallId:b.toolCallId,args:b.args},u);case"tool_result":return e.jsx(g,{toolName:b.toolName,toolCallId:b.toolCallId,content:b.content},u);case"error":return e.jsx(p,{content:b.content,onRetry:i},u);default:return null}}),s&&l&&!o&&e.jsx("span",{className:"sa-streaming-cursor",children:"|"}),!o&&!s&&a.parts.length>0&&e.jsx(h,{message:a,onCopy:n,onRegenerate:i,onFeedback:d})]}),o&&e.jsx(q,{src:t==null?void 0:t.user,role:"user"})]})}function ne({message:a,suggestedPrompts:s,onPromptClick:l}){return e.jsxs("div",{className:"sa-welcome",children:[e.jsx("div",{className:"sa-welcome-icon",children:e.jsx(w,{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((r,t)=>e.jsx("button",{className:"sa-welcome-prompt",onClick:()=>l(r),children:r},t))})]})}function H({slots:a,welcomeMessage:s,suggestedPrompts:l,avatar:r}){const{messages:t,status:i,sendMessage:d}=T(),{containerRef:n}=G([t]),o=i==="streaming",x=a.WelcomeScreen??ne;return t.length===0?e.jsx("div",{className:"sa-thread",ref:n,children:e.jsx(x,{message:s,suggestedPrompts:l,onPromptClick:d})}):e.jsx("div",{className:"sa-thread",ref:n,children:t.map((f,m)=>e.jsx(re,{message:f,isStreaming:o,isLast:m===t.length-1,slots:a,avatar:r},f.id))})}function B(){const{status:a,sendMessage:s,stop:l}=T(),[r,t]=c.useState(""),i=c.useRef(null),d=a==="streaming"||a==="submitted",n=c.useCallback(()=>{const f=r.trim();!f||d||(s(f),t(""),i.current&&(i.current.style.height="auto"))},[r,d,s]),o=c.useCallback(f=>{f.key==="Enter"&&!f.shiftKey&&(f.preventDefault(),n())},[n]),x=c.useCallback(f=>{t(f.target.value);const m=f.target;m.style.height="auto",m.style.height=Math.min(m.scrollHeight,120)+"px"},[]);return e.jsxs("div",{className:"sa-composer",children:[e.jsx("textarea",{ref:i,className:"sa-composer-input",value:r,onChange:x,onKeyDown:o,placeholder:"输入消息...",rows:1,disabled:a==="submitted"}),d?e.jsx("button",{className:"sa-composer-stop",onClick:l,title:"停止",children:e.jsx(w,{name:"circle-stop",size:16,color:"#fff"})}):e.jsx("button",{className:"sa-composer-send",onClick:n,disabled:!r.trim(),title:"发送",children:e.jsx(w,{name:"arrow-up",size:16,color:"#fff"})})]})}function me({isOpen:a,onClose:s,title:l,slots:r,welcomeMessage:t,suggestedPrompts:i,avatar:d}){const{status:n,sendMessage:o,stop:x,toggleThreadList:f,conversations:m,activeConversationId:v,switchConversation:g,newConversation:p,deleteConversation:h,renameConversation:b,isThreadListOpen:u}=T();if(!a)return null;const k=r.Header,C=r.Composer,j=r.ThreadList;return e.jsxs("div",{className:"sa-panel",children:[k?e.jsx(k,{title:l,onClose:s,onToggleThreadList:f}):e.jsx(J,{title:l,onClose:s,onToggleThreadList:f}),j&&u?e.jsx(j,{conversations:m,activeSessionId:v,onSwitch:g,onNew:p,onDelete:h,onRename:b}):e.jsx(X,{}),e.jsx(H,{slots:r,welcomeMessage:t,suggestedPrompts:i,avatar:d}),C?e.jsx(C,{status:n,onSend:o,onStop:x}):e.jsx(B,{})]})}function ge({conversations:a,activeSessionId:s,onSwitch:l,onNew:r,onDelete:t,isCollapsed:i,onToggleCollapse:d}){const[n,o]=c.useState(null),[x,f]=c.useState(null),m=c.useCallback((g,p)=>{p.stopPropagation(),x===g?(t(g),f(null)):(f(g),setTimeout(()=>f(null),3e3))},[x,t]),v=(()=>{const g=Date.now(),p=[],h=[],b=[];for(const k of a){const C=g-new Date(k.updateTime).getTime();C<864e5?p.push(k):C<6048e5?h.push(k):b.push(k)}const u=[];return p.length&&u.push({label:"今天",items:p}),h.length&&u.push({label:"最近 7 天",items:h}),b.length&&u.push({label:"更早",items:b}),u})();return i?e.jsxs("div",{className:"sa-fp-sidebar sa-fp-sidebar-collapsed",children:[e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:d,title:"展开",children:e.jsx(w,{name:"panel-left",size:18})}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:r,title:"新会话",style:{marginTop:4},children:e.jsx(w,{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:r,children:[e.jsx(w,{name:"plus",size:15})," ",e.jsx("span",{children:"新会话"})]}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:d,title:"收起侧边栏",children:e.jsx(w,{name:"panel-left-close",size:18})})]}),e.jsxs("div",{className:"sa-fp-sidebar-list",children:[a.length===0&&e.jsx("div",{className:"sa-fp-sidebar-empty",children:"开始你的第一个对话"}),v.map(g=>e.jsxs("div",{children:[e.jsx("div",{className:"sa-fp-sidebar-group",children:g.label}),g.items.map(p=>{const h=p.sessionId===s,b=n===p.sessionId,u=x===p.sessionId;return e.jsxs("div",{className:`sa-fp-sidebar-item ${h?"active":""}`,onClick:()=>l(p.sessionId),onMouseEnter:()=>o(p.sessionId),onMouseLeave:()=>o(null),children:[e.jsx("span",{className:"sa-fp-sidebar-item-title",children:p.title||"新会话"}),b&&e.jsx("button",{className:`sa-fp-sidebar-item-action ${u?"danger":""}`,onClick:k=>m(p.sessionId,k),title:u?"确认删除":"删除",children:e.jsx(w,{name:u?"x":"trash2",size:14})})]},p.sessionId)})]},g.label))]})]})}function ie({title:a,slots:s,welcomeMessage:l,suggestedPrompts:r,sidebarDefaultOpen:t=!0,onClose:i,avatar:d}){const{status:n,sendMessage:o,stop:x,conversations:f,activeConversationId:m,switchConversation:v,newConversation:g,deleteConversation:p,renameConversation:h}=T(),[b,u]=c.useState(t),k=c.useCallback(()=>u(L=>!L),[]),C=s.ThreadList,j=s.Composer;return e.jsxs("div",{className:"sa-fullpage",children:[C?e.jsx("div",{className:`sa-fp-sidebar-wrapper ${b?"":"sa-fp-sidebar-wrapper-collapsed"}`,children:e.jsx(C,{conversations:f,activeSessionId:m,onSwitch:v,onNew:g,onDelete:p,onRename:h})}):e.jsx(ge,{conversations:f,activeSessionId:m,onSwitch:v,onNew:g,onDelete:p,onRename:h,isCollapsed:!b,onToggleCollapse:k}),e.jsxs("div",{className:"sa-fp-main",children:[e.jsxs("div",{className:"sa-fp-header",children:[!b&&!C&&e.jsx("button",{className:"sa-fp-header-btn",onClick:k,title:"展开侧边栏",children:e.jsx(w,{name:"panel-left",size:18})}),e.jsx("div",{className:"sa-fp-header-title",children:a}),e.jsx("div",{className:"sa-fp-header-actions",children:i&&e.jsx("button",{className:"sa-fp-header-btn",onClick:i,title:"关闭",children:e.jsx(w,{name:"x",size:18})})})]}),e.jsx(H,{slots:s,welcomeMessage:l,suggestedPrompts:r,avatar:d}),j?e.jsx(j,{status:n,onSend:o,onStop:x}):e.jsx(B,{})]})]})}function oe(a){const{sdk:s,mode:l="floating",slots:r={},hooks:t={},welcomeMessage:i,suggestedPrompts:d,title:n="AI Assistant",initialOpen:o=!1,onOpenChange:x,sidebarDefaultOpen:f,avatar:m}=a,[v,g]=c.useState(o);c.useEffect(()=>{g(o)},[o]);const p=c.useCallback(()=>{var k,C;const u=!v;g(u),x==null||x(u),u?(k=t.onOpen)==null||k.call(t):(C=t.onClose)==null||C.call(t)},[v,t,x]),h=c.useCallback(()=>{var u;g(!1),x==null||x(!1),(u=t.onClose)==null||u.call(t)},[t,x]),b=r.Trigger??Y;return l==="fullpage"?v?e.jsx(O,{sdk:s,hooks:t,children:e.jsx(ie,{title:n,slots:r,welcomeMessage:i,suggestedPrompts:d,sidebarDefaultOpen:f,onClose:h,avatar:m})}):null:e.jsxs(O,{sdk:s,hooks:t,children:[e.jsx(b,{isOpen:v,onClick:p}),e.jsx(me,{isOpen:v,onClose:h,title:n,slots:r,welcomeMessage:i,suggestedPrompts:d,avatar:m})]})}const le=`
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);
@@ -1121,4 +1121,4 @@
1121
1121
  border-color: #d4d4d8;
1122
1122
  box-shadow: 0 0 0 3px rgba(99,102,241,0.08);
1123
1123
  }
1124
- `,fe={primaryColor:"#6366f1",backgroundColor:"#ffffff",fontFamily:"system-ui, -apple-system, sans-serif",borderRadius:16,panelWidth:380,panelHeight:520,zIndex:9999};function ue(a){const s={...fe,...a};return[`--sa-primary: ${s.primaryColor}`,`--sa-bg: ${s.backgroundColor}`,`--sa-font: ${s.fontFamily}`,`--sa-radius: ${s.borderRadius}px`,`--sa-panel-width: ${s.panelWidth}px`,`--sa-panel-height: ${s.panelHeight}px`,`--sa-z-index: ${s.zIndex}`].join("; ")}function ke(a,s){const l=typeof a=="string"?document.querySelector(a):a;if(!l)throw new Error(`Target element not found: ${a}`);const r=document.createElement("div");r.setAttribute("data-super-agent-widget","");const t=ue(s.theme??{});if(r.setAttribute("style",t),l.appendChild(r),!document.getElementById("sa-widget-styles")){const o=document.createElement("style");o.id="sa-widget-styles",o.textContent=pe,document.head.appendChild(o)}let i=_(r),d=!1;const n=o=>{i&&i.render(c.createElement(de,{...s,initialOpen:o??d,onOpenChange:x=>{d=x}}))};return n(!1),{open(){d=!0,n(!0)},close(){d=!1,n(!1)},destroy(){i&&(i.unmount(),i=null),r.remove()}}}exports.ActionBar=ie;exports.ChatProvider=H;exports.ChatWidget=de;exports.Composer=W;exports.ErrorPart=ne;exports.FullpageLayout=ce;exports.Header=Z;exports.Icon=k;exports.Message=oe;exports.TextPart=se;exports.ThinkingPart=ae;exports.Thread=B;exports.ThreadList=Q;exports.ToolCallPart=te;exports.ToolResultPart=re;exports.Trigger=G;exports.WelcomeScreen=le;exports.defaultTheme=fe;exports.mount=ke;exports.themeToCSSVars=ue;exports.useAutoScroll=ee;exports.useChat=Y;exports.useChatContext=T;exports.useConversations=J;exports.widgetStyles=pe;
1124
+ `,ce={primaryColor:"#6366f1",backgroundColor:"#ffffff",fontFamily:"system-ui, -apple-system, sans-serif",borderRadius:16,panelWidth:380,panelHeight:520,zIndex:9999};function de(a){const s={...ce,...a};return[`--sa-primary: ${s.primaryColor}`,`--sa-bg: ${s.backgroundColor}`,`--sa-font: ${s.fontFamily}`,`--sa-radius: ${s.borderRadius}px`,`--sa-panel-width: ${s.panelWidth}px`,`--sa-panel-height: ${s.panelHeight}px`,`--sa-z-index: ${s.zIndex}`].join("; ")}function be(a,s){const l=typeof a=="string"?document.querySelector(a):a;if(!l)throw new Error(`Target element not found: ${a}`);const r=document.createElement("div");r.setAttribute("data-super-agent-widget","");const t=de(s.theme??{});if(r.setAttribute("style",t),l.appendChild(r),!document.getElementById("sa-widget-styles")){const o=document.createElement("style");o.id="sa-widget-styles",o.textContent=le,document.head.appendChild(o)}let i=fe.createRoot(r),d=!1;const n=o=>{i&&i.render(c.createElement(oe,{...s,initialOpen:o??d,onOpenChange:x=>{d=x}}))};return n(!1),{open(){d=!0,n(!0)},close(){d=!1,n(!1)},destroy(){i&&(i.unmount(),i=null),r.remove()}}}exports.ActionBar=te;exports.ChatProvider=O;exports.ChatWidget=oe;exports.Composer=B;exports.ErrorPart=ae;exports.FullpageLayout=ie;exports.Header=J;exports.Icon=w;exports.Message=re;exports.TextPart=Z;exports.ThinkingPart=Q;exports.Thread=H;exports.ThreadList=X;exports.ToolCallPart=ee;exports.ToolResultPart=se;exports.Trigger=Y;exports.WelcomeScreen=ne;exports.defaultTheme=ce;exports.mount=be;exports.themeToCSSVars=de;exports.useAutoScroll=G;exports.useChat=K;exports.useChatContext=T;exports.useConversations=U;exports.widgetStyles=le;
package/dist/widget.mjs CHANGED
@@ -1,52 +1,38 @@
1
- import X, { useState as I, useRef as M, useCallback as w, useEffect as P, createContext as ae, useContext as te } from "react";
2
- import re from "react-dom";
3
- import { jsx as e, jsxs as g, Fragment as se } from "react/jsx-runtime";
4
- var F, H = re;
5
- if (process.env.NODE_ENV === "production")
6
- F = H.createRoot, H.hydrateRoot;
7
- else {
8
- var q = H.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
9
- F = function(t, a) {
10
- q.usingClientEntryPoint = !0;
11
- try {
12
- return H.createRoot(t, a);
13
- } finally {
14
- q.usingClientEntryPoint = !1;
15
- }
16
- };
17
- }
18
- function ne(t) {
19
- const { sdk: a, sessionId: l, onSessionCreated: s, onStreamStart: r, onStreamEnd: i, onError: c, onMessageSend: n } = t, [o, u] = I([]), [p, m] = I("ready"), [v, x] = I(null), d = M(null), h = M(!1), b = M(l);
1
+ import V, { useState as I, useRef as R, useCallback as y, useEffect as A, createContext as Z, useContext as Q } from "react";
2
+ import { createRoot as ee } from "react-dom/client";
3
+ import { jsx as e, jsxs as g, Fragment as ae } from "react/jsx-runtime";
4
+ function te(t) {
5
+ const { sdk: a, sessionId: l, onSessionCreated: s, onStreamStart: r, onStreamEnd: i, onError: c, onMessageSend: n } = t, [o, h] = I([]), [p, m] = I("ready"), [v, x] = I(null), d = R(null), u = R(!1), b = R(l);
20
6
  b.current = l;
21
- const f = w((z) => {
22
- if (h.current) return;
23
- h.current = !0, n == null || n(z);
24
- const D = {
7
+ const f = y((z) => {
8
+ if (u.current) return;
9
+ u.current = !0, n == null || n(z);
10
+ const O = {
25
11
  id: `user_${Date.now()}`,
26
12
  role: "user",
27
13
  parts: [{ type: "text", content: z }],
28
14
  timestamp: Date.now()
29
- }, S = `assistant_${Date.now()}`, ee = {
15
+ }, S = `assistant_${Date.now()}`, G = {
30
16
  id: S,
31
17
  role: "assistant",
32
18
  parts: [],
33
19
  timestamp: Date.now()
34
20
  };
35
- u((T) => [...T, D, ee]), m("submitted"), x(null);
36
- const U = (T) => {
37
- let j = "", R = "", _ = [], K = !1;
38
- const B = () => {
39
- const k = [..._];
40
- if (j) {
41
- const L = k.findIndex(($) => $.type === "thinking");
42
- L >= 0 ? k[L] = { type: "thinking", content: j } : k.unshift({ type: "thinking", content: j });
21
+ h((T) => [...T, O, G]), m("submitted"), x(null);
22
+ const F = (T) => {
23
+ let H = "", E = "", D = [], W = !1;
24
+ const _ = () => {
25
+ const k = [...D];
26
+ if (H) {
27
+ const $ = k.findIndex((L) => L.type === "thinking");
28
+ $ >= 0 ? k[$] = { type: "thinking", content: H } : k.unshift({ type: "thinking", content: H });
43
29
  }
44
- if (R) {
45
- const L = k.findLastIndex(($) => $.type === "text");
46
- L >= 0 ? k[L] = { type: "text", content: R } : k.push({ type: "text", content: R });
30
+ if (E) {
31
+ const $ = k.findLastIndex((L) => L.type === "text");
32
+ $ >= 0 ? k[$] = { type: "text", content: E } : k.push({ type: "text", content: E });
47
33
  }
48
- u(
49
- (L) => L.map(($) => $.id === S ? { ...$, parts: k } : $)
34
+ h(
35
+ ($) => $.map((L) => L.id === S ? { ...L, parts: k } : L)
50
36
  );
51
37
  };
52
38
  d.current = a.chat({
@@ -54,15 +40,15 @@ function ne(t) {
54
40
  message: z,
55
41
  stream: !0,
56
42
  onMessage: (k) => {
57
- switch (K || (K = !0, m("streaming")), k.type) {
43
+ switch (W || (W = !0, m("streaming")), k.type) {
58
44
  case "thinking":
59
- j += k.content;
45
+ H += k.content;
60
46
  break;
61
47
  case "ai":
62
- R += k.content;
48
+ E += k.content;
63
49
  break;
64
50
  case "tool_call":
65
- _.push({
51
+ D.push({
66
52
  type: "tool_call",
67
53
  toolName: k.toolName,
68
54
  toolCallId: k.toolCallId,
@@ -70,86 +56,86 @@ function ne(t) {
70
56
  });
71
57
  break;
72
58
  case "tool_result":
73
- _.push({
59
+ D.push({
74
60
  type: "tool_result",
75
61
  toolName: k.toolName ?? "",
76
62
  toolCallId: k.toolCallId,
77
63
  content: k.content
78
- }), R && (_.push({ type: "text", content: R }), R = "");
64
+ }), E && (D.push({ type: "text", content: E }), E = "");
79
65
  break;
80
66
  }
81
- B();
67
+ _();
82
68
  },
83
69
  onDone: (k) => {
84
- (R || _.length > 0 || j) && B(), k.sessionId && k.sessionId !== b.current && (s == null || s(k.sessionId)), i == null || i(k.sessionId || T), m("ready"), h.current = !1, d.current = null;
70
+ (E || D.length > 0 || H) && _(), k.sessionId && k.sessionId !== b.current && (s == null || s(k.sessionId)), i == null || i(k.sessionId || T), m("ready"), u.current = !1, d.current = null;
85
71
  },
86
72
  onError: (k) => {
87
- _.push({ type: "error", content: k.message }), B(), x(k), m("error"), c == null || c(k), h.current = !1, d.current = null;
73
+ D.push({ type: "error", content: k.message }), _(), x(k), m("error"), c == null || c(k), u.current = !1, d.current = null;
88
74
  }
89
75
  }), r == null || r(T);
90
76
  };
91
- b.current ? U(b.current) : a.createSession().then((T) => {
92
- b.current = T, s == null || s(T), U(T);
77
+ b.current ? F(b.current) : a.createSession().then((T) => {
78
+ b.current = T, s == null || s(T), F(T);
93
79
  }).catch((T) => {
94
- x(T instanceof Error ? T : new Error(String(T))), m("error"), c == null || c(T instanceof Error ? T : new Error(String(T))), h.current = !1;
80
+ x(T instanceof Error ? T : new Error(String(T))), m("error"), c == null || c(T instanceof Error ? T : new Error(String(T))), u.current = !1;
95
81
  });
96
- }, [a, s, r, i, c, n]), y = w(() => {
82
+ }, [a, s, r, i, c, n]), w = y(() => {
97
83
  var z;
98
- (z = d.current) == null || z.abort(), d.current = null, m("ready"), h.current = !1;
99
- }, []), N = w(() => {
100
- if (h.current) return;
84
+ (z = d.current) == null || z.abort(), d.current = null, m("ready"), u.current = !1;
85
+ }, []), N = y(() => {
86
+ if (u.current) return;
101
87
  const z = o.findLastIndex((S) => S.role === "user");
102
88
  if (z < 0) return;
103
- const D = o[z].parts.filter((S) => S.type === "text").map((S) => S.content).join("");
104
- u((S) => S.slice(0, z)), f(D);
89
+ const O = o[z].parts.filter((S) => S.type === "text").map((S) => S.content).join("");
90
+ h((S) => S.slice(0, z)), f(O);
105
91
  }, [o, f]);
106
- return { messages: o, status: p, error: v, sendMessage: f, stop: y, regenerate: N, setMessages: u };
92
+ return { messages: o, status: p, error: v, sendMessage: f, stop: w, regenerate: N, setMessages: h };
107
93
  }
108
- const W = "sa_active_conversation";
109
- function V() {
94
+ const B = "sa_active_conversation";
95
+ function K() {
110
96
  try {
111
- return localStorage.getItem(W);
97
+ return localStorage.getItem(B);
112
98
  } catch {
113
99
  return null;
114
100
  }
115
101
  }
116
- function ie(t) {
102
+ function re(t) {
117
103
  try {
118
- t ? localStorage.setItem(W, t) : localStorage.removeItem(W);
104
+ t ? localStorage.setItem(B, t) : localStorage.removeItem(B);
119
105
  } catch {
120
106
  }
121
107
  }
122
- function oe(t) {
108
+ function se(t) {
123
109
  const { sdk: a, onConversationChange: l } = t, [s, r] = I([]), [i, c] = I(
124
- () => V()
125
- ), [n, o] = I(!1), u = M(!0);
126
- P(() => () => {
127
- u.current = !1;
110
+ () => K()
111
+ ), [n, o] = I(!1), h = R(!0);
112
+ A(() => () => {
113
+ h.current = !1;
128
114
  }, []);
129
- const p = w((f) => {
130
- c(f), ie(f), f && (l == null || l(f));
131
- }, [l]), m = w(async () => {
115
+ const p = y((f) => {
116
+ c(f), re(f), f && (l == null || l(f));
117
+ }, [l]), m = y(async () => {
132
118
  o(!0);
133
119
  try {
134
120
  const f = await a.listConversations({ page: 1, size: 100 });
135
- if (!u.current) return;
121
+ if (!h.current) return;
136
122
  r(f.items);
137
- const y = V();
138
- y && !f.items.find((N) => N.sessionId === y) && p(null);
123
+ const w = K();
124
+ w && !f.items.find((N) => N.sessionId === w) && p(null);
139
125
  } catch {
140
126
  } finally {
141
- u.current && o(!1);
127
+ h.current && o(!1);
142
128
  }
143
- }, [a, p]), v = w(async (f) => (p(f), await a.getMessages(f)), [a, p]), x = w(() => {
129
+ }, [a, p]), v = y(async (f) => (p(f), await a.getMessages(f)), [a, p]), x = y(() => {
144
130
  p(null);
145
- }, [p]), d = w(async (f) => {
146
- await a.deleteConversation(f), r((y) => y.filter((N) => N.sessionId !== f)), i === f && p(null);
147
- }, [a, i, p]), h = w(async (f, y) => {
148
- await a.renameConversation(f, y), r(
149
- (N) => N.map((z) => z.sessionId === f ? { ...z, title: y } : z)
131
+ }, [p]), d = y(async (f) => {
132
+ await a.deleteConversation(f), r((w) => w.filter((N) => N.sessionId !== f)), i === f && p(null);
133
+ }, [a, i, p]), u = y(async (f, w) => {
134
+ await a.renameConversation(f, w), r(
135
+ (N) => N.map((z) => z.sessionId === f ? { ...z, title: w } : z)
150
136
  );
151
137
  }, [a]), { enabled: b = !0 } = t;
152
- return P(() => {
138
+ return A(() => {
153
139
  b && m();
154
140
  }, [b, m]), {
155
141
  conversations: s,
@@ -159,30 +145,30 @@ function oe(t) {
159
145
  switchConversation: v,
160
146
  newConversation: x,
161
147
  deleteConversation: d,
162
- renameConversation: h,
148
+ renameConversation: u,
163
149
  setActiveConversationId: p
164
150
  };
165
151
  }
166
- const G = ae(null);
167
- function A() {
168
- const t = te(G);
152
+ const Y = Z(null);
153
+ function P() {
154
+ const t = Q(Y);
169
155
  if (!t) throw new Error("useChatContext must be used within ChatProvider");
170
156
  return t;
171
157
  }
172
- function Y({ sdk: t, hooks: a, children: l }) {
158
+ function U({ sdk: t, hooks: a, children: l }) {
173
159
  const [s, r] = I(!1), [i, c] = I(!1);
174
- P(() => {
160
+ A(() => {
175
161
  t.getToken().then(() => {
176
162
  c(!0);
177
163
  }).catch(() => {
178
164
  c(!0);
179
165
  });
180
166
  }, [t]);
181
- const n = oe({
167
+ const n = se({
182
168
  sdk: t,
183
169
  onConversationChange: a == null ? void 0 : a.onConversationChange,
184
170
  enabled: i
185
- }), o = ne({
171
+ }), o = te({
186
172
  sdk: t,
187
173
  sessionId: n.activeConversationId,
188
174
  onSessionCreated: (d) => {
@@ -190,26 +176,26 @@ function Y({ sdk: t, hooks: a, children: l }) {
190
176
  },
191
177
  onStreamStart: a == null ? void 0 : a.onStreamStart,
192
178
  onStreamEnd: (d) => {
193
- var h;
194
- (h = a == null ? void 0 : a.onStreamEnd) == null || h.call(a, d), n.loadConversations();
179
+ var u;
180
+ (u = a == null ? void 0 : a.onStreamEnd) == null || u.call(a, d), n.loadConversations();
195
181
  },
196
182
  onError: a == null ? void 0 : a.onError,
197
183
  onMessageSend: a == null ? void 0 : a.onMessageSend
198
- }), u = w(async (d) => {
199
- const h = await n.switchConversation(d);
200
- o.setMessages(h), r(!1);
201
- }, [n, o]), p = w(() => {
184
+ }), h = y(async (d) => {
185
+ const u = await n.switchConversation(d);
186
+ o.setMessages(u), r(!1);
187
+ }, [n, o]), p = y(() => {
202
188
  n.newConversation(), o.setMessages([]), r(!1);
203
- }, [n, o]), m = w(async (d) => {
189
+ }, [n, o]), m = y(async (d) => {
204
190
  await n.deleteConversation(d), n.activeConversationId === d && o.setMessages([]);
205
191
  }, [n, o]);
206
- P(() => {
192
+ A(() => {
207
193
  n.activeConversationId && o.messages.length === 0 && t.getMessages(n.activeConversationId).then((d) => {
208
194
  o.setMessages(d);
209
195
  }).catch(() => {
210
196
  });
211
197
  }, []);
212
- const v = w(() => {
198
+ const v = y(() => {
213
199
  r((d) => !d);
214
200
  }, []), x = {
215
201
  status: o.status,
@@ -221,16 +207,16 @@ function Y({ sdk: t, hooks: a, children: l }) {
221
207
  sendMessage: o.sendMessage,
222
208
  stop: o.stop,
223
209
  regenerate: o.regenerate,
224
- switchConversation: u,
210
+ switchConversation: h,
225
211
  newConversation: p,
226
212
  deleteConversation: m,
227
213
  renameConversation: n.renameConversation,
228
214
  toggleThreadList: v,
229
- feedback: (d, h) => {
230
- t.feedback(d, h);
215
+ feedback: (d, u) => {
216
+ t.feedback(d, u);
231
217
  }
232
218
  };
233
- return /* @__PURE__ */ e(G.Provider, { value: x, children: l });
219
+ return /* @__PURE__ */ e(Y.Provider, { value: x, children: l });
234
220
  }
235
221
  function C({ name: t, size: a = 16, color: l = "currentColor", className: s, style: r }) {
236
222
  return /* @__PURE__ */ e(
@@ -243,10 +229,10 @@ function C({ name: t, size: a = 16, color: l = "currentColor", className: s, sty
243
229
  }
244
230
  );
245
231
  }
246
- function le({ isOpen: t, onClick: a }) {
232
+ function ne({ isOpen: t, onClick: a }) {
247
233
  return /* @__PURE__ */ e("button", { className: `sa-trigger ${t ? "sa-trigger-open" : ""}`, onClick: a, children: t ? /* @__PURE__ */ e(C, { name: "x", size: 24, color: "#fff" }) : /* @__PURE__ */ e(C, { name: "message-square", size: 24, color: "#fff" }) });
248
234
  }
249
- function ce({ title: t, onClose: a, onToggleThreadList: l, showClose: s = !0 }) {
235
+ function ie({ title: t, onClose: a, onToggleThreadList: l, showClose: s = !0 }) {
250
236
  return /* @__PURE__ */ g("div", { className: "sa-header", children: [
251
237
  /* @__PURE__ */ g("div", { className: "sa-header-left", children: [
252
238
  /* @__PURE__ */ e("div", { className: "sa-header-avatar", children: "A" }),
@@ -261,7 +247,7 @@ function ce({ title: t, onClose: a, onToggleThreadList: l, showClose: s = !0 })
261
247
  ] })
262
248
  ] });
263
249
  }
264
- function de() {
250
+ function oe() {
265
251
  const {
266
252
  conversations: t,
267
253
  activeConversationId: a,
@@ -271,12 +257,12 @@ function de() {
271
257
  renameConversation: i,
272
258
  isThreadListOpen: c,
273
259
  toggleThreadList: n
274
- } = A(), [o, u] = I(null), [p, m] = I(""), v = w((d, h) => {
275
- u(d), m(h ?? "");
276
- }, []), x = w((d) => {
277
- p.trim() && i(d, p.trim()), u(null);
260
+ } = P(), [o, h] = I(null), [p, m] = I(""), v = y((d, u) => {
261
+ h(d), m(u ?? "");
262
+ }, []), x = y((d) => {
263
+ p.trim() && i(d, p.trim()), h(null);
278
264
  }, [p, i]);
279
- return c ? /* @__PURE__ */ g(se, { children: [
265
+ return c ? /* @__PURE__ */ g(ae, { children: [
280
266
  /* @__PURE__ */ e("div", { className: "sa-thread-list-overlay", onClick: n }),
281
267
  /* @__PURE__ */ g("div", { className: "sa-thread-list", children: [
282
268
  /* @__PURE__ */ g("div", { className: "sa-thread-list-header", children: [
@@ -299,20 +285,20 @@ function de() {
299
285
  {
300
286
  className: "sa-thread-item-edit",
301
287
  value: p,
302
- onChange: (h) => m(h.target.value),
288
+ onChange: (u) => m(u.target.value),
303
289
  onBlur: () => x(d.sessionId),
304
- onKeyDown: (h) => {
305
- h.key === "Enter" && x(d.sessionId), h.key === "Escape" && u(null);
290
+ onKeyDown: (u) => {
291
+ u.key === "Enter" && x(d.sessionId), u.key === "Escape" && h(null);
306
292
  },
307
- onClick: (h) => h.stopPropagation(),
293
+ onClick: (u) => u.stopPropagation(),
308
294
  autoFocus: !0
309
295
  }
310
296
  ) : /* @__PURE__ */ e(
311
297
  "span",
312
298
  {
313
299
  className: "sa-thread-item-title",
314
- onDoubleClick: (h) => {
315
- h.stopPropagation(), v(d.sessionId, d.title);
300
+ onDoubleClick: (u) => {
301
+ u.stopPropagation(), v(d.sessionId, d.title);
316
302
  },
317
303
  children: d.title || "新会话"
318
304
  }
@@ -321,8 +307,8 @@ function de() {
321
307
  "button",
322
308
  {
323
309
  className: "sa-thread-item-delete",
324
- onClick: (h) => {
325
- h.stopPropagation(), r(d.sessionId);
310
+ onClick: (u) => {
311
+ u.stopPropagation(), r(d.sessionId);
326
312
  },
327
313
  title: "删除",
328
314
  children: /* @__PURE__ */ e(C, { name: "trash2", size: 14 })
@@ -336,35 +322,35 @@ function de() {
336
322
  ] })
337
323
  ] }) : null;
338
324
  }
339
- function pe(t) {
340
- const a = M(null), l = M(!1), s = M(0), r = w(() => {
325
+ function le(t) {
326
+ const a = R(null), l = R(!1), s = R(0), r = y(() => {
341
327
  const c = a.current;
342
328
  if (!c) return;
343
- const { scrollTop: n, scrollHeight: o, clientHeight: u } = c, p = o - n - u < 40;
329
+ const { scrollTop: n, scrollHeight: o, clientHeight: h } = c, p = o - n - h < 40;
344
330
  n < s.current && !p && (l.current = !0), p && (l.current = !1), s.current = n;
345
331
  }, []);
346
- P(() => {
332
+ A(() => {
347
333
  const c = a.current;
348
334
  if (c)
349
335
  return c.addEventListener("scroll", r, { passive: !0 }), () => c.removeEventListener("scroll", r);
350
- }, [r]), P(() => {
336
+ }, [r]), A(() => {
351
337
  if (l.current) return;
352
338
  const c = a.current;
353
339
  c && requestAnimationFrame(() => {
354
340
  c.scrollTop = c.scrollHeight;
355
341
  });
356
342
  }, t);
357
- const i = w(() => {
343
+ const i = y(() => {
358
344
  l.current = !1;
359
345
  const c = a.current;
360
346
  c && (c.scrollTop = c.scrollHeight);
361
347
  }, []);
362
348
  return { containerRef: a, scrollToBottom: i };
363
349
  }
364
- function E(t) {
350
+ function M(t) {
365
351
  return t.value++;
366
352
  }
367
- function O(t, a) {
353
+ function j(t, a) {
368
354
  const l = [], s = [], r = () => {
369
355
  s.length > 0 && (l.push(s.join("")), s.length = 0);
370
356
  };
@@ -375,7 +361,7 @@ function O(t, a) {
375
361
  const n = t.indexOf("`", i + 1);
376
362
  if (n !== -1) {
377
363
  r(), l.push(
378
- /* @__PURE__ */ e("code", { className: "sa-inline-code", children: t.slice(i + 1, n) }, E(a))
364
+ /* @__PURE__ */ e("code", { className: "sa-inline-code", children: t.slice(i + 1, n) }, M(a))
379
365
  ), i = n + 1;
380
366
  continue;
381
367
  }
@@ -383,7 +369,7 @@ function O(t, a) {
383
369
  const n = t.indexOf("**", i + 2);
384
370
  if (n !== -1) {
385
371
  r(), l.push(
386
- /* @__PURE__ */ e("strong", { children: O(t.slice(i + 2, n), a) }, E(a))
372
+ /* @__PURE__ */ e("strong", { children: j(t.slice(i + 2, n), a) }, M(a))
387
373
  ), i = n + 2;
388
374
  continue;
389
375
  }
@@ -391,7 +377,7 @@ function O(t, a) {
391
377
  const n = t.indexOf("*", i + 1);
392
378
  if (n !== -1) {
393
379
  r(), l.push(
394
- /* @__PURE__ */ e("em", { children: O(t.slice(i + 1, n), a) }, E(a))
380
+ /* @__PURE__ */ e("em", { children: j(t.slice(i + 1, n), a) }, M(a))
395
381
  ), i = n + 1;
396
382
  continue;
397
383
  }
@@ -400,7 +386,7 @@ function O(t, a) {
400
386
  if (n !== -1 && t[n + 1] === "(") {
401
387
  const o = t.indexOf(")", n + 2);
402
388
  if (o !== -1) {
403
- const u = t.slice(i + 1, n), p = t.slice(n + 2, o);
389
+ const h = t.slice(i + 1, n), p = t.slice(n + 2, o);
404
390
  r(), l.push(
405
391
  /* @__PURE__ */ e(
406
392
  "a",
@@ -408,9 +394,9 @@ function O(t, a) {
408
394
  href: p,
409
395
  target: "_blank",
410
396
  rel: "noreferrer",
411
- children: O(u, a)
397
+ children: j(h, a)
412
398
  },
413
- E(a)
399
+ M(a)
414
400
  )
415
401
  ), i = o + 1;
416
402
  continue;
@@ -421,26 +407,26 @@ function O(t, a) {
421
407
  }
422
408
  return r(), l;
423
409
  }
424
- function fe(t, a) {
410
+ function ce(t, a) {
425
411
  const l = [], s = t.split(`
426
412
  `);
427
413
  let r = 0;
428
414
  const i = (o) => o.trim() === "", c = (o) => /^#{1,6}\s+\S/.test(o.trim()), n = (o) => /^(?:[-*+]|\d+[.)])\s+\S/.test(o.trim());
429
415
  for (; r < s.length; ) {
430
- const o = s[r], u = o.trim();
416
+ const o = s[r], h = o.trim();
431
417
  if (i(o)) {
432
418
  r++;
433
419
  continue;
434
420
  }
435
421
  if (c(o)) {
436
- const m = u.match(/^(#{1,6})\s+(.*)$/);
422
+ const m = h.match(/^(#{1,6})\s+(.*)$/);
437
423
  if (m) {
438
424
  const x = `h${Math.min(m[1].length, 6)}`;
439
425
  l.push(
440
- X.createElement(
426
+ V.createElement(
441
427
  x,
442
- { key: E(a) },
443
- O(m[2], a)
428
+ { key: M(a) },
429
+ j(m[2], a)
444
430
  )
445
431
  );
446
432
  }
@@ -448,15 +434,15 @@ function fe(t, a) {
448
434
  continue;
449
435
  }
450
436
  if (n(o)) {
451
- const m = /^\d+[.)]/.test(u), v = [];
437
+ const m = /^\d+[.)]/.test(h), v = [];
452
438
  for (; r < s.length && n(s[r]); ) {
453
439
  const x = s[r].trim().match(/^(?:[-*+]|\d+[.)])\s+(.*)$/);
454
440
  x && v.push(
455
- /* @__PURE__ */ e("li", { children: O(x[1], a) }, E(a))
441
+ /* @__PURE__ */ e("li", { children: j(x[1], a) }, M(a))
456
442
  ), r++;
457
443
  }
458
444
  l.push(
459
- m ? /* @__PURE__ */ e("ol", { className: "sa-list", children: v }, E(a)) : /* @__PURE__ */ e("ul", { className: "sa-list", children: v }, E(a))
445
+ m ? /* @__PURE__ */ e("ol", { className: "sa-list", children: v }, M(a)) : /* @__PURE__ */ e("ul", { className: "sa-list", children: v }, M(a))
460
446
  );
461
447
  continue;
462
448
  }
@@ -464,28 +450,28 @@ function fe(t, a) {
464
450
  for (; r < s.length && !i(s[r]) && !c(s[r]) && !n(s[r]); )
465
451
  p.push(s[r]), r++;
466
452
  l.push(
467
- /* @__PURE__ */ e("p", { className: "sa-text-paragraph", children: O(p.join(" "), a) }, E(a))
453
+ /* @__PURE__ */ e("p", { className: "sa-text-paragraph", children: j(p.join(" "), a) }, M(a))
468
454
  );
469
455
  }
470
456
  return l;
471
457
  }
472
- function ue(t) {
458
+ function de(t) {
473
459
  const a = { value: 0 }, l = [], s = t.split("```");
474
460
  for (let r = 0; r < s.length; r++)
475
461
  if (r % 2 === 0)
476
- l.push(...fe(s[r], a));
462
+ l.push(...ce(s[r], a));
477
463
  else if (s[r].trim() !== "") {
478
464
  let i = s[r].replace(/^\r?\n/, "");
479
465
  i = i.replace(/^[a-zA-Z0-9_+#.-]+\r?\n/, ""), i = i.replace(/\r?\n$/, ""), l.push(
480
- /* @__PURE__ */ e("pre", { className: "sa-code-block", children: /* @__PURE__ */ e("code", { children: i }) }, E(a))
466
+ /* @__PURE__ */ e("pre", { className: "sa-code-block", children: /* @__PURE__ */ e("code", { children: i }) }, M(a))
481
467
  );
482
468
  }
483
469
  return l;
484
470
  }
485
- function he({ content: t }) {
486
- return /* @__PURE__ */ e("div", { className: "sa-text-part", children: ue(t) });
471
+ function pe({ content: t }) {
472
+ return /* @__PURE__ */ e("div", { className: "sa-text-part", children: de(t) });
487
473
  }
488
- function me({ content: t }) {
474
+ function fe({ content: t }) {
489
475
  const [a, l] = I(!1);
490
476
  return /* @__PURE__ */ g("div", { className: "sa-thinking-part", onClick: () => l(!a), children: [
491
477
  /* @__PURE__ */ g("div", { className: "sa-thinking-header", children: [
@@ -496,7 +482,7 @@ function me({ content: t }) {
496
482
  a && /* @__PURE__ */ e("div", { className: "sa-thinking-content", children: t })
497
483
  ] });
498
484
  }
499
- function ge({ toolName: t, toolCallId: a, args: l }) {
485
+ function he({ toolName: t, toolCallId: a, args: l }) {
500
486
  const [s, r] = I(!1);
501
487
  let i = l;
502
488
  try {
@@ -515,7 +501,7 @@ function ge({ toolName: t, toolCallId: a, args: l }) {
515
501
  s && /* @__PURE__ */ e("pre", { className: "sa-tool-args", children: i })
516
502
  ] });
517
503
  }
518
- function xe({ toolName: t, toolCallId: a, content: l }) {
504
+ function ue({ toolName: t, toolCallId: a, content: l }) {
519
505
  return /* @__PURE__ */ g("div", { className: "sa-tool-result-part", children: [
520
506
  /* @__PURE__ */ g("div", { className: "sa-tool-result-header", children: [
521
507
  /* @__PURE__ */ e("span", { className: "sa-tool-icon", children: /* @__PURE__ */ e(C, { name: "circle-check", size: 14, color: "#10b981" }) }),
@@ -524,7 +510,7 @@ function xe({ toolName: t, toolCallId: a, content: l }) {
524
510
  /* @__PURE__ */ e("div", { className: "sa-tool-result-content", children: l })
525
511
  ] });
526
512
  }
527
- function be({ content: t, onRetry: a }) {
513
+ function me({ content: t, onRetry: a }) {
528
514
  return /* @__PURE__ */ g("div", { className: "sa-error-part", children: [
529
515
  /* @__PURE__ */ g("div", { className: "sa-error-content", children: [
530
516
  /* @__PURE__ */ e("span", { className: "sa-error-icon", children: /* @__PURE__ */ e(C, { name: "triangle-alert", size: 14 }) }),
@@ -533,17 +519,17 @@ function be({ content: t, onRetry: a }) {
533
519
  a && /* @__PURE__ */ e("button", { className: "sa-error-retry", onClick: a, children: "重试" })
534
520
  ] });
535
521
  }
536
- function ve({ message: t, onCopy: a, onRegenerate: l, onFeedback: s }) {
537
- const { status: r } = A(), [i, c] = I(null), [n, o] = I(!1);
522
+ function ge({ message: t, onCopy: a, onRegenerate: l, onFeedback: s }) {
523
+ const { status: r } = P(), [i, c] = I(null), [n, o] = I(!1);
538
524
  if (r !== "ready" && r !== "error") return null;
539
- const u = w(() => {
525
+ const h = y(() => {
540
526
  a(), o(!0), setTimeout(() => o(!1), 2e3);
541
- }, [a]), p = w((m) => {
527
+ }, [a]), p = y((m) => {
542
528
  const v = i === m ? null : m;
543
529
  c(v), v && (s == null || s(t.id, v));
544
530
  }, [i, t.id, s]);
545
531
  return /* @__PURE__ */ g("div", { className: "sa-action-bar", children: [
546
- /* @__PURE__ */ g("button", { className: "sa-action-btn", onClick: u, title: "复制", children: [
532
+ /* @__PURE__ */ g("button", { className: "sa-action-btn", onClick: h, title: "复制", children: [
547
533
  /* @__PURE__ */ e(C, { name: n ? "circle-check" : "copy", size: 14 }),
548
534
  " ",
549
535
  n ? "已复制" : "复制"
@@ -573,21 +559,21 @@ function ve({ message: t, onCopy: a, onRegenerate: l, onFeedback: s }) {
573
559
  )
574
560
  ] });
575
561
  }
576
- function J({ src: t, role: a }) {
562
+ function q({ src: t, role: a }) {
577
563
  const l = a === "assistant" ? "A" : "U";
578
564
  return t ? t.startsWith("http") || t.startsWith("/") || t.startsWith("data:") ? /* @__PURE__ */ e("div", { className: `sa-avatar sa-avatar-${a}`, children: /* @__PURE__ */ e("img", { src: t, alt: a, style: { width: "100%", height: "100%", borderRadius: "inherit", objectFit: "cover" } }) }) : /* @__PURE__ */ e("div", { className: `sa-avatar sa-avatar-${a}`, children: t }) : /* @__PURE__ */ e("div", { className: `sa-avatar sa-avatar-${a}`, children: l });
579
565
  }
580
- function ye({ message: t, isStreaming: a, isLast: l, slots: s, avatar: r }) {
581
- const { regenerate: i, feedback: c } = A(), n = w(() => {
566
+ function xe({ message: t, isStreaming: a, isLast: l, slots: s, avatar: r }) {
567
+ const { regenerate: i, feedback: c } = P(), n = y(() => {
582
568
  var f;
583
- const b = t.parts.filter((y) => y.type === "text").map((y) => y.content).join(`
569
+ const b = t.parts.filter((w) => w.type === "text").map((w) => w.content).join(`
584
570
  `);
585
571
  (f = navigator.clipboard) == null || f.writeText(b).catch(() => {
586
572
  });
587
- }, [t]), o = t.role === "user", u = s.Message;
588
- if (u)
573
+ }, [t]), o = t.role === "user", h = s.Message;
574
+ if (h)
589
575
  return /* @__PURE__ */ e(
590
- u,
576
+ h,
591
577
  {
592
578
  message: t,
593
579
  isStreaming: a,
@@ -599,9 +585,9 @@ function ye({ message: t, isStreaming: a, isLast: l, slots: s, avatar: r }) {
599
585
  onFeedback: c
600
586
  }
601
587
  );
602
- const p = s.TextPart ?? he, m = s.ThinkingPart ?? me, v = s.ToolCallPart ?? ge, x = s.ToolResultPart ?? xe, d = s.ErrorPart ?? be, h = s.ActionBar ?? ve;
588
+ const p = s.TextPart ?? pe, m = s.ThinkingPart ?? fe, v = s.ToolCallPart ?? he, x = s.ToolResultPart ?? ue, d = s.ErrorPart ?? me, u = s.ActionBar ?? ge;
603
589
  return /* @__PURE__ */ g("div", { className: `sa-message ${o ? "sa-message-user" : "sa-message-assistant"}`, children: [
604
- !o && /* @__PURE__ */ e(J, { src: r == null ? void 0 : r.assistant, role: "assistant" }),
590
+ !o && /* @__PURE__ */ e(q, { src: r == null ? void 0 : r.assistant, role: "assistant" }),
605
591
  /* @__PURE__ */ g("div", { className: "sa-message-body", children: [
606
592
  t.parts.map((b, f) => {
607
593
  switch (b.type) {
@@ -620,12 +606,12 @@ function ye({ message: t, isStreaming: a, isLast: l, slots: s, avatar: r }) {
620
606
  }
621
607
  }),
622
608
  a && l && !o && /* @__PURE__ */ e("span", { className: "sa-streaming-cursor", children: "|" }),
623
- !o && !a && t.parts.length > 0 && /* @__PURE__ */ e(h, { message: t, onCopy: n, onRegenerate: i, onFeedback: c })
609
+ !o && !a && t.parts.length > 0 && /* @__PURE__ */ e(u, { message: t, onCopy: n, onRegenerate: i, onFeedback: c })
624
610
  ] }),
625
- o && /* @__PURE__ */ e(J, { src: r == null ? void 0 : r.user, role: "user" })
611
+ o && /* @__PURE__ */ e(q, { src: r == null ? void 0 : r.user, role: "user" })
626
612
  ] });
627
613
  }
628
- function we({ message: t, suggestedPrompts: a, onPromptClick: l }) {
614
+ function be({ message: t, suggestedPrompts: a, onPromptClick: l }) {
629
615
  return /* @__PURE__ */ g("div", { className: "sa-welcome", children: [
630
616
  /* @__PURE__ */ e("div", { className: "sa-welcome-icon", children: /* @__PURE__ */ e(C, { name: "sparkles", size: 40 }) }),
631
617
  /* @__PURE__ */ e("p", { className: "sa-welcome-message", children: t || "你好!有什么可以帮你的?" }),
@@ -640,17 +626,17 @@ function we({ message: t, suggestedPrompts: a, onPromptClick: l }) {
640
626
  )) })
641
627
  ] });
642
628
  }
643
- function Z({ slots: t, welcomeMessage: a, suggestedPrompts: l, avatar: s }) {
644
- const { messages: r, status: i, sendMessage: c } = A(), { containerRef: n } = pe([r]), o = i === "streaming", u = t.WelcomeScreen ?? we;
629
+ function J({ slots: t, welcomeMessage: a, suggestedPrompts: l, avatar: s }) {
630
+ const { messages: r, status: i, sendMessage: c } = P(), { containerRef: n } = le([r]), o = i === "streaming", h = t.WelcomeScreen ?? be;
645
631
  return r.length === 0 ? /* @__PURE__ */ e("div", { className: "sa-thread", ref: n, children: /* @__PURE__ */ e(
646
- u,
632
+ h,
647
633
  {
648
634
  message: a,
649
635
  suggestedPrompts: l,
650
636
  onPromptClick: c
651
637
  }
652
638
  ) }) : /* @__PURE__ */ e("div", { className: "sa-thread", ref: n, children: r.map((p, m) => /* @__PURE__ */ e(
653
- ye,
639
+ xe,
654
640
  {
655
641
  message: p,
656
642
  isStreaming: o,
@@ -661,13 +647,13 @@ function Z({ slots: t, welcomeMessage: a, suggestedPrompts: l, avatar: s }) {
661
647
  p.id
662
648
  )) });
663
649
  }
664
- function Q() {
665
- const { status: t, sendMessage: a, stop: l } = A(), [s, r] = I(""), i = M(null), c = t === "streaming" || t === "submitted", n = w(() => {
650
+ function X() {
651
+ const { status: t, sendMessage: a, stop: l } = P(), [s, r] = I(""), i = R(null), c = t === "streaming" || t === "submitted", n = y(() => {
666
652
  const p = s.trim();
667
653
  !p || c || (a(p), r(""), i.current && (i.current.style.height = "auto"));
668
- }, [s, c, a]), o = w((p) => {
654
+ }, [s, c, a]), o = y((p) => {
669
655
  p.key === "Enter" && !p.shiftKey && (p.preventDefault(), n());
670
- }, [n]), u = w((p) => {
656
+ }, [n]), h = y((p) => {
671
657
  r(p.target.value);
672
658
  const m = p.target;
673
659
  m.style.height = "auto", m.style.height = Math.min(m.scrollHeight, 120) + "px";
@@ -679,7 +665,7 @@ function Q() {
679
665
  ref: i,
680
666
  className: "sa-composer-input",
681
667
  value: s,
682
- onChange: u,
668
+ onChange: h,
683
669
  onKeyDown: o,
684
670
  placeholder: "输入消息...",
685
671
  rows: 1,
@@ -698,12 +684,12 @@ function Q() {
698
684
  )
699
685
  ] });
700
686
  }
701
- function ke({ isOpen: t, onClose: a, title: l, slots: s, welcomeMessage: r, suggestedPrompts: i, avatar: c }) {
702
- const { status: n, sendMessage: o, stop: u, toggleThreadList: p, conversations: m, activeConversationId: v, switchConversation: x, newConversation: d, deleteConversation: h, renameConversation: b, isThreadListOpen: f } = A();
687
+ function ve({ isOpen: t, onClose: a, title: l, slots: s, welcomeMessage: r, suggestedPrompts: i, avatar: c }) {
688
+ const { status: n, sendMessage: o, stop: h, toggleThreadList: p, conversations: m, activeConversationId: v, switchConversation: x, newConversation: d, deleteConversation: u, renameConversation: b, isThreadListOpen: f } = P();
703
689
  if (!t) return null;
704
- const y = s.Header, N = s.Composer, z = s.ThreadList;
690
+ const w = s.Header, N = s.Composer, z = s.ThreadList;
705
691
  return /* @__PURE__ */ g("div", { className: "sa-panel", children: [
706
- y ? /* @__PURE__ */ e(y, { title: l, onClose: a, onToggleThreadList: p }) : /* @__PURE__ */ e(ce, { title: l, onClose: a, onToggleThreadList: p }),
692
+ w ? /* @__PURE__ */ e(w, { title: l, onClose: a, onToggleThreadList: p }) : /* @__PURE__ */ e(ie, { title: l, onClose: a, onToggleThreadList: p }),
707
693
  z && f ? /* @__PURE__ */ e(
708
694
  z,
709
695
  {
@@ -711,15 +697,15 @@ function ke({ isOpen: t, onClose: a, title: l, slots: s, welcomeMessage: r, sugg
711
697
  activeSessionId: v,
712
698
  onSwitch: x,
713
699
  onNew: d,
714
- onDelete: h,
700
+ onDelete: u,
715
701
  onRename: b
716
702
  }
717
- ) : /* @__PURE__ */ e(de, {}),
718
- /* @__PURE__ */ e(Z, { slots: s, welcomeMessage: r, suggestedPrompts: i, avatar: c }),
719
- N ? /* @__PURE__ */ e(N, { status: n, onSend: o, onStop: u }) : /* @__PURE__ */ e(Q, {})
703
+ ) : /* @__PURE__ */ e(oe, {}),
704
+ /* @__PURE__ */ e(J, { slots: s, welcomeMessage: r, suggestedPrompts: i, avatar: c }),
705
+ N ? /* @__PURE__ */ e(N, { status: n, onSend: o, onStop: h }) : /* @__PURE__ */ e(X, {})
720
706
  ] });
721
707
  }
722
- function Ce({
708
+ function we({
723
709
  conversations: t,
724
710
  activeSessionId: a,
725
711
  onSwitch: l,
@@ -728,16 +714,16 @@ function Ce({
728
714
  isCollapsed: i,
729
715
  onToggleCollapse: c
730
716
  }) {
731
- const [n, o] = I(null), [u, p] = I(null), m = w((x, d) => {
732
- d.stopPropagation(), u === x ? (r(x), p(null)) : (p(x), setTimeout(() => p(null), 3e3));
733
- }, [u, r]), v = (() => {
734
- const x = Date.now(), d = [], h = [], b = [];
735
- for (const y of t) {
736
- const N = x - new Date(y.updateTime).getTime();
737
- N < 864e5 ? d.push(y) : N < 6048e5 ? h.push(y) : b.push(y);
717
+ const [n, o] = I(null), [h, p] = I(null), m = y((x, d) => {
718
+ d.stopPropagation(), h === x ? (r(x), p(null)) : (p(x), setTimeout(() => p(null), 3e3));
719
+ }, [h, r]), v = (() => {
720
+ const x = Date.now(), d = [], u = [], b = [];
721
+ for (const w of t) {
722
+ const N = x - new Date(w.updateTime).getTime();
723
+ N < 864e5 ? d.push(w) : N < 6048e5 ? u.push(w) : b.push(w);
738
724
  }
739
725
  const f = [];
740
- return d.length && f.push({ label: "今天", items: d }), h.length && f.push({ label: "最近 7 天", items: h }), b.length && f.push({ label: "更早", items: b }), f;
726
+ return d.length && f.push({ label: "今天", items: d }), u.length && f.push({ label: "最近 7 天", items: u }), b.length && f.push({ label: "更早", items: b }), f;
741
727
  })();
742
728
  return i ? /* @__PURE__ */ g("div", { className: "sa-fp-sidebar sa-fp-sidebar-collapsed", children: [
743
729
  /* @__PURE__ */ e("button", { className: "sa-fp-sidebar-toggle", onClick: c, title: "展开", children: /* @__PURE__ */ e(C, { name: "panel-left", size: 18 }) }),
@@ -756,11 +742,11 @@ function Ce({
756
742
  v.map((x) => /* @__PURE__ */ g("div", { children: [
757
743
  /* @__PURE__ */ e("div", { className: "sa-fp-sidebar-group", children: x.label }),
758
744
  x.items.map((d) => {
759
- const h = d.sessionId === a, b = n === d.sessionId, f = u === d.sessionId;
745
+ const u = d.sessionId === a, b = n === d.sessionId, f = h === d.sessionId;
760
746
  return /* @__PURE__ */ g(
761
747
  "div",
762
748
  {
763
- className: `sa-fp-sidebar-item ${h ? "active" : ""}`,
749
+ className: `sa-fp-sidebar-item ${u ? "active" : ""}`,
764
750
  onClick: () => l(d.sessionId),
765
751
  onMouseEnter: () => o(d.sessionId),
766
752
  onMouseLeave: () => o(null),
@@ -770,7 +756,7 @@ function Ce({
770
756
  "button",
771
757
  {
772
758
  className: `sa-fp-sidebar-item-action ${f ? "danger" : ""}`,
773
- onClick: (y) => m(d.sessionId, y),
759
+ onClick: (w) => m(d.sessionId, w),
774
760
  title: f ? "确认删除" : "删除",
775
761
  children: /* @__PURE__ */ e(C, { name: f ? "x" : "trash2", size: 14 })
776
762
  }
@@ -784,18 +770,18 @@ function Ce({
784
770
  ] })
785
771
  ] });
786
772
  }
787
- function Ne({ title: t, slots: a, welcomeMessage: l, suggestedPrompts: s, sidebarDefaultOpen: r = !0, onClose: i, avatar: c }) {
773
+ function ye({ title: t, slots: a, welcomeMessage: l, suggestedPrompts: s, sidebarDefaultOpen: r = !0, onClose: i, avatar: c }) {
788
774
  const {
789
775
  status: n,
790
776
  sendMessage: o,
791
- stop: u,
777
+ stop: h,
792
778
  conversations: p,
793
779
  activeConversationId: m,
794
780
  switchConversation: v,
795
781
  newConversation: x,
796
782
  deleteConversation: d,
797
- renameConversation: h
798
- } = A(), [b, f] = I(r), y = w(() => f((D) => !D), []), N = a.ThreadList, z = a.Composer;
783
+ renameConversation: u
784
+ } = P(), [b, f] = I(r), w = y(() => f((O) => !O), []), N = a.ThreadList, z = a.Composer;
799
785
  return /* @__PURE__ */ g("div", { className: "sa-fullpage", children: [
800
786
  N ? /* @__PURE__ */ e("div", { className: `sa-fp-sidebar-wrapper ${b ? "" : "sa-fp-sidebar-wrapper-collapsed"}`, children: /* @__PURE__ */ e(
801
787
  N,
@@ -805,63 +791,63 @@ function Ne({ title: t, slots: a, welcomeMessage: l, suggestedPrompts: s, sideba
805
791
  onSwitch: v,
806
792
  onNew: x,
807
793
  onDelete: d,
808
- onRename: h
794
+ onRename: u
809
795
  }
810
796
  ) }) : /* @__PURE__ */ e(
811
- Ce,
797
+ we,
812
798
  {
813
799
  conversations: p,
814
800
  activeSessionId: m,
815
801
  onSwitch: v,
816
802
  onNew: x,
817
803
  onDelete: d,
818
- onRename: h,
804
+ onRename: u,
819
805
  isCollapsed: !b,
820
- onToggleCollapse: y
806
+ onToggleCollapse: w
821
807
  }
822
808
  ),
823
809
  /* @__PURE__ */ g("div", { className: "sa-fp-main", children: [
824
810
  /* @__PURE__ */ g("div", { className: "sa-fp-header", children: [
825
- !b && !N && /* @__PURE__ */ e("button", { className: "sa-fp-header-btn", onClick: y, title: "展开侧边栏", children: /* @__PURE__ */ e(C, { name: "panel-left", size: 18 }) }),
811
+ !b && !N && /* @__PURE__ */ e("button", { className: "sa-fp-header-btn", onClick: w, title: "展开侧边栏", children: /* @__PURE__ */ e(C, { name: "panel-left", size: 18 }) }),
826
812
  /* @__PURE__ */ e("div", { className: "sa-fp-header-title", children: t }),
827
813
  /* @__PURE__ */ e("div", { className: "sa-fp-header-actions", children: i && /* @__PURE__ */ e("button", { className: "sa-fp-header-btn", onClick: i, title: "关闭", children: /* @__PURE__ */ e(C, { name: "x", size: 18 }) }) })
828
814
  ] }),
829
- /* @__PURE__ */ e(Z, { slots: a, welcomeMessage: l, suggestedPrompts: s, avatar: c }),
830
- z ? /* @__PURE__ */ e(z, { status: n, onSend: o, onStop: u }) : /* @__PURE__ */ e(Q, {})
815
+ /* @__PURE__ */ e(J, { slots: a, welcomeMessage: l, suggestedPrompts: s, avatar: c }),
816
+ z ? /* @__PURE__ */ e(z, { status: n, onSend: o, onStop: h }) : /* @__PURE__ */ e(X, {})
831
817
  ] })
832
818
  ] });
833
819
  }
834
- function ze(t) {
835
- const { sdk: a, mode: l = "floating", slots: s = {}, hooks: r = {}, welcomeMessage: i, suggestedPrompts: c, title: n = "AI Assistant", initialOpen: o = !1, onOpenChange: u, sidebarDefaultOpen: p, avatar: m } = t, [v, x] = I(o);
836
- P(() => {
820
+ function ke(t) {
821
+ const { sdk: a, mode: l = "floating", slots: s = {}, hooks: r = {}, welcomeMessage: i, suggestedPrompts: c, title: n = "AI Assistant", initialOpen: o = !1, onOpenChange: h, sidebarDefaultOpen: p, avatar: m } = t, [v, x] = I(o);
822
+ A(() => {
837
823
  x(o);
838
824
  }, [o]);
839
- const d = w(() => {
840
- var y, N;
825
+ const d = y(() => {
826
+ var w, N;
841
827
  const f = !v;
842
- x(f), u == null || u(f), f ? (y = r.onOpen) == null || y.call(r) : (N = r.onClose) == null || N.call(r);
843
- }, [v, r, u]), h = w(() => {
828
+ x(f), h == null || h(f), f ? (w = r.onOpen) == null || w.call(r) : (N = r.onClose) == null || N.call(r);
829
+ }, [v, r, h]), u = y(() => {
844
830
  var f;
845
- x(!1), u == null || u(!1), (f = r.onClose) == null || f.call(r);
846
- }, [r, u]), b = s.Trigger ?? le;
847
- return l === "fullpage" ? v ? /* @__PURE__ */ e(Y, { sdk: a, hooks: r, children: /* @__PURE__ */ e(
848
- Ne,
831
+ x(!1), h == null || h(!1), (f = r.onClose) == null || f.call(r);
832
+ }, [r, h]), b = s.Trigger ?? ne;
833
+ return l === "fullpage" ? v ? /* @__PURE__ */ e(U, { sdk: a, hooks: r, children: /* @__PURE__ */ e(
834
+ ye,
849
835
  {
850
836
  title: n,
851
837
  slots: s,
852
838
  welcomeMessage: i,
853
839
  suggestedPrompts: c,
854
840
  sidebarDefaultOpen: p,
855
- onClose: h,
841
+ onClose: u,
856
842
  avatar: m
857
843
  }
858
- ) }) : null : /* @__PURE__ */ g(Y, { sdk: a, hooks: r, children: [
844
+ ) }) : null : /* @__PURE__ */ g(U, { sdk: a, hooks: r, children: [
859
845
  /* @__PURE__ */ e(b, { isOpen: v, onClick: d }),
860
846
  /* @__PURE__ */ e(
861
- ke,
847
+ ve,
862
848
  {
863
849
  isOpen: v,
864
- onClose: h,
850
+ onClose: u,
865
851
  title: n,
866
852
  slots: s,
867
853
  welcomeMessage: i,
@@ -871,7 +857,7 @@ function ze(t) {
871
857
  )
872
858
  ] });
873
859
  }
874
- const Ie = `
860
+ const Ce = `
875
861
  [data-super-agent-widget] {
876
862
  --sa-primary-dark: color-mix(in srgb, var(--sa-primary) 82%, #000);
877
863
  --sa-primary-light: color-mix(in srgb, var(--sa-primary) 10%, #fff);
@@ -1992,7 +1978,7 @@ const Ie = `
1992
1978
  border-color: #d4d4d8;
1993
1979
  box-shadow: 0 0 0 3px rgba(99,102,241,0.08);
1994
1980
  }
1995
- `, Te = {
1981
+ `, Ne = {
1996
1982
  primaryColor: "#6366f1",
1997
1983
  backgroundColor: "#ffffff",
1998
1984
  fontFamily: "system-ui, -apple-system, sans-serif",
@@ -2001,8 +1987,8 @@ const Ie = `
2001
1987
  panelHeight: 520,
2002
1988
  zIndex: 9999
2003
1989
  };
2004
- function Se(t) {
2005
- const a = { ...Te, ...t };
1990
+ function ze(t) {
1991
+ const a = { ...Ne, ...t };
2006
1992
  return [
2007
1993
  `--sa-primary: ${a.primaryColor}`,
2008
1994
  `--sa-bg: ${a.backgroundColor}`,
@@ -2013,25 +1999,25 @@ function Se(t) {
2013
1999
  `--sa-z-index: ${a.zIndex}`
2014
2000
  ].join("; ");
2015
2001
  }
2016
- function Le(t, a) {
2002
+ function Me(t, a) {
2017
2003
  const l = typeof t == "string" ? document.querySelector(t) : t;
2018
2004
  if (!l)
2019
2005
  throw new Error(`Target element not found: ${t}`);
2020
2006
  const s = document.createElement("div");
2021
2007
  s.setAttribute("data-super-agent-widget", "");
2022
- const r = Se(a.theme ?? {});
2008
+ const r = ze(a.theme ?? {});
2023
2009
  if (s.setAttribute("style", r), l.appendChild(s), !document.getElementById("sa-widget-styles")) {
2024
2010
  const o = document.createElement("style");
2025
- o.id = "sa-widget-styles", o.textContent = Ie, document.head.appendChild(o);
2011
+ o.id = "sa-widget-styles", o.textContent = Ce, document.head.appendChild(o);
2026
2012
  }
2027
- let i = F(s), c = !1;
2013
+ let i = ee(s), c = !1;
2028
2014
  const n = (o) => {
2029
2015
  i && i.render(
2030
- X.createElement(ze, {
2016
+ V.createElement(ke, {
2031
2017
  ...a,
2032
2018
  initialOpen: o ?? c,
2033
- onOpenChange: (u) => {
2034
- c = u;
2019
+ onOpenChange: (h) => {
2020
+ c = h;
2035
2021
  }
2036
2022
  })
2037
2023
  );
@@ -2049,29 +2035,29 @@ function Le(t, a) {
2049
2035
  };
2050
2036
  }
2051
2037
  export {
2052
- ve as ActionBar,
2053
- Y as ChatProvider,
2054
- ze as ChatWidget,
2055
- Q as Composer,
2056
- be as ErrorPart,
2057
- Ne as FullpageLayout,
2058
- ce as Header,
2038
+ ge as ActionBar,
2039
+ U as ChatProvider,
2040
+ ke as ChatWidget,
2041
+ X as Composer,
2042
+ me as ErrorPart,
2043
+ ye as FullpageLayout,
2044
+ ie as Header,
2059
2045
  C as Icon,
2060
- ye as Message,
2061
- he as TextPart,
2062
- me as ThinkingPart,
2063
- Z as Thread,
2064
- de as ThreadList,
2065
- ge as ToolCallPart,
2066
- xe as ToolResultPart,
2067
- le as Trigger,
2068
- we as WelcomeScreen,
2069
- Te as defaultTheme,
2070
- Le as mount,
2071
- Se as themeToCSSVars,
2072
- pe as useAutoScroll,
2073
- ne as useChat,
2074
- A as useChatContext,
2075
- oe as useConversations,
2076
- Ie as widgetStyles
2046
+ xe as Message,
2047
+ pe as TextPart,
2048
+ fe as ThinkingPart,
2049
+ J as Thread,
2050
+ oe as ThreadList,
2051
+ he as ToolCallPart,
2052
+ ue as ToolResultPart,
2053
+ ne as Trigger,
2054
+ be as WelcomeScreen,
2055
+ Ne as defaultTheme,
2056
+ Me as mount,
2057
+ ze as themeToCSSVars,
2058
+ le as useAutoScroll,
2059
+ te as useChat,
2060
+ P as useChatContext,
2061
+ se as useConversations,
2062
+ Ce as widgetStyles
2077
2063
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "super-agent-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Super Agent Web SDK — API + 嵌入式聊天组件",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",