super-agent-sdk 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -24
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +2 -1
- package/dist/widget.cjs +6 -6
- package/dist/widget.d.ts +2 -2
- package/dist/widget.mjs +499 -489
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -63,9 +63,8 @@ import { createSuperAgent } from "super-agent-sdk";
|
|
|
63
63
|
import { mount } from "super-agent-sdk/widget";
|
|
64
64
|
|
|
65
65
|
const sdk = createSuperAgent({
|
|
66
|
-
baseUrl: "/api/v1",
|
|
67
66
|
botId: 1,
|
|
68
|
-
|
|
67
|
+
tokenGateway: "/api/gateway",
|
|
69
68
|
});
|
|
70
69
|
// Token 自动获取,无需手动设置
|
|
71
70
|
const widget = mount("#chat", { sdk });
|
|
@@ -84,39 +83,40 @@ function createSuperAgent(config: SDKConfig): SuperAgentSDK;
|
|
|
84
83
|
|
|
85
84
|
```ts
|
|
86
85
|
export interface SDKConfig {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
botId: number; // Bot ID(必填)
|
|
87
|
+
tokenGateway: string; // Token 网关前缀(必填,不带 host),如 "/api/gateway";token URL = {origin}{tokenGateway}/v1/token
|
|
88
|
+
baseUrl?: string; // 业务接口前缀(可选,默认 "/api/agent/runtime/v1"),不带 host
|
|
89
|
+
appId?: string; // 可选,不传则从 token 接口响应自动获取
|
|
90
|
+
token?: string; // 可选,不传则自动调 token 接口获取
|
|
90
91
|
}
|
|
91
92
|
```
|
|
92
93
|
|
|
93
|
-
只需提供 `
|
|
94
|
+
只需提供 `botId` 与 `tokenGateway` 即可。`baseUrl` 默认为 `/api/agent/runtime/v1`,`appId` 从 token 接口响应中自动获取。SDK 不要求在创建时传入 `token`——组件挂载后会自动调用 `sdk.getToken()` 获取 token 并内部保存,无需手动设置。
|
|
94
95
|
|
|
95
96
|
```ts
|
|
96
97
|
const sdk = createSuperAgent({
|
|
97
|
-
baseUrl: "https://agent.example.com/api/v1",
|
|
98
98
|
botId: 1,
|
|
99
|
-
|
|
99
|
+
tokenGateway: "/api/gateway",
|
|
100
100
|
});
|
|
101
101
|
```
|
|
102
102
|
|
|
103
103
|
凭证获取流程:
|
|
104
104
|
|
|
105
105
|
```
|
|
106
|
-
createSuperAgent({
|
|
106
|
+
createSuperAgent({ botId, tokenGateway })
|
|
107
107
|
↓
|
|
108
108
|
Widget 挂载时自动调用 sdk.getToken()
|
|
109
109
|
↓
|
|
110
|
-
POST /
|
|
110
|
+
POST {origin}{tokenGateway}/v1/token(body: { botId })
|
|
111
111
|
↓
|
|
112
|
-
后端返回 { token }
|
|
112
|
+
后端返回 { token, appId }
|
|
113
113
|
↓
|
|
114
|
-
SDK 内部自动保存 token
|
|
114
|
+
SDK 内部自动保存 token + appId
|
|
115
115
|
↓
|
|
116
116
|
ready
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
-
`appId`
|
|
119
|
+
`tokenGateway` 用于拼接 token 接口路径,`appId` 自动从 token 响应获取(也可手动传入覆盖)。
|
|
120
120
|
|
|
121
121
|
---
|
|
122
122
|
|
|
@@ -143,6 +143,7 @@ export interface SuperAgentSDK {
|
|
|
143
143
|
setToken(token: string): void;
|
|
144
144
|
feedback(messageId: string, type: "like" | "dislike", options?: { reason?: number; remark?: string }): Promise<void>;
|
|
145
145
|
cancelFeedback(messageId: string): Promise<void>;
|
|
146
|
+
respondInterrupt(interruptId: string, sessionId: string, response: InterruptResponse): Promise<void>;
|
|
146
147
|
}
|
|
147
148
|
```
|
|
148
149
|
|
|
@@ -275,9 +276,9 @@ sdk.getToken(): Promise<string>
|
|
|
275
276
|
|
|
276
277
|
自动获取并保存访问 token:
|
|
277
278
|
|
|
278
|
-
- SDK 调用 `POST /
|
|
279
|
-
- 后端返回 `{ token }`
|
|
280
|
-
- SDK 自动将 token 保存在内部
|
|
279
|
+
- SDK 调用 `POST {origin}{tokenGateway}/v1/token`,请求体 `{ botId }`
|
|
280
|
+
- 后端返回 `{ token, appId }`
|
|
281
|
+
- SDK 自动将 token + appId 保存在内部
|
|
281
282
|
- Widget 在挂载时自动调用此方法 —— 通常无需手动调用
|
|
282
283
|
|
|
283
284
|
```ts
|
|
@@ -373,9 +374,8 @@ import { createSuperAgent } from "super-agent-sdk";
|
|
|
373
374
|
import { mount } from "super-agent-sdk/widget";
|
|
374
375
|
|
|
375
376
|
const sdk = createSuperAgent({
|
|
376
|
-
baseUrl: "/api/v1",
|
|
377
377
|
botId: 1,
|
|
378
|
-
|
|
378
|
+
tokenGateway: "/api/gateway",
|
|
379
379
|
});
|
|
380
380
|
|
|
381
381
|
const widget = mount("#chat-root", {
|
|
@@ -867,7 +867,7 @@ mount("#chat-root", {
|
|
|
867
867
|
});
|
|
868
868
|
```
|
|
869
869
|
|
|
870
|
-
> 点赞 /
|
|
870
|
+
> 点赞 / 踩由内置 `ActionBar` 组件通过 `ChatState.feedback()` / `cancelFeedback()` 自动处理,无需额外配置(见 [5.9](#59-feedback--cancelfeedback))。
|
|
871
871
|
|
|
872
872
|
### 8.5 定制示例
|
|
873
873
|
|
|
@@ -885,9 +885,8 @@ import type {
|
|
|
885
885
|
} from "super-agent-sdk/widget";
|
|
886
886
|
|
|
887
887
|
const sdk = createSuperAgent({
|
|
888
|
-
baseUrl: "/api/v1",
|
|
889
888
|
botId: 1,
|
|
890
|
-
|
|
889
|
+
tokenGateway: "/api/gateway",
|
|
891
890
|
});
|
|
892
891
|
sdk.setToken("demo_token");
|
|
893
892
|
|
|
@@ -1313,9 +1312,8 @@ sdk.chat({
|
|
|
1313
1312
|
import { createSuperAgent } from "super-agent-sdk";
|
|
1314
1313
|
|
|
1315
1314
|
const sdk = createSuperAgent({
|
|
1316
|
-
baseUrl: "/api/v1",
|
|
1317
1315
|
botId: 1,
|
|
1318
|
-
|
|
1316
|
+
tokenGateway: "/api/gateway",
|
|
1319
1317
|
});
|
|
1320
1318
|
sdk.setToken("<TOKEN>");
|
|
1321
1319
|
|
|
@@ -1407,7 +1405,7 @@ function MyChat({ sdk }: { sdk: SuperAgentSDK }) {
|
|
|
1407
1405
|
|
|
1408
1406
|
### 13.3 Token 续期
|
|
1409
1407
|
|
|
1410
|
-
请求返回 `401` 时,SDK 自动重新调用 `POST /
|
|
1408
|
+
请求返回 `401` 时,SDK 自动重新调用 `POST {tokenGateway}/v1/token` 获取新 token。
|
|
1411
1409
|
|
|
1412
1410
|
### 13.4 取消请求
|
|
1413
1411
|
|
package/dist/index.cjs
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
`).map(n=>n.trim()).filter(n=>n.startsWith("data:")).map(n=>n.slice(5).trim());if(!e.length)return null;let t;try{t=JSON.parse(e.join(`
|
|
3
3
|
`))}catch{return null}switch(t.type){case"user":return{type:"user",content:t.content??""};case"thinking":return{type:"thinking",content:t.content??""};case"ai":return{type:"ai",content:t.content??""};case"tool_call":return{type:"tool_call",content:t.name??"",toolName:t.name,toolCallId:t.id,args:typeof t.args=="string"?t.args:JSON.stringify(t.args??{})};case"tool_result":return{type:"tool_result",content:t.output??"",toolName:t.name,toolCallId:t.id,artifacts:Array.isArray(t.artifacts)?t.artifacts:void 0,renderMode:t.render_mode??t.renderMode,html:t.html};case"done":return{type:"done",content:t.content??"",sessionId:t.sessionId??t.session_id??"",messageId:t.messageId??t.message_id};case"error":return{type:"error",content:t.message??"Unknown error"};case"interrupt":return{type:"interrupt",content:t.content??"",interrupt:{interruptId:t.interruptId??t.interrupt_id??"",interruptType:t.interruptType??t.interrupt_type??"confirm",content:t.content??"",options:t.options,placeholder:t.placeholder,inputType:t.inputType??t.input_type,maxLength:t.maxLength??t.max_length,required:t.required,fields:t.fields,metadata:t.metadata}};default:return null}}async function m(a,e,t){if(!a.body){t(new Error("Response body is null"));return}const n=a.body.getReader(),o=new TextDecoder;let r="";try{for(;;){const{value:s,done:i}=await n.read();if(i)break;r+=o.decode(s,{stream:!0});let c;for(;(c=r.indexOf(`
|
|
4
4
|
|
|
5
|
-
`))!==-1;){const l=r.slice(0,c);r=r.slice(c+2);const d=y(l);d&&e(d)}}if(r.trim()){const s=y(r);s&&e(s)}}catch(s){(s==null?void 0:s.name)!=="AbortError"&&t(s instanceof Error?s:new Error(String(s)))}finally{try{n.releaseLock()}catch{}}}function k(a,e){const t=new AbortController,n=e.signal?b(e.signal,t.signal):t.signal,o={botId:a.getBotId(),message:e.message,sessionId:e.sessionId,stream:e.stream!==!1};return o.stream?a.streamPost("/chat",o,n).then(r=>{var s;if(!r.ok){(s=e.onError)==null||s.call(e,new Error(`HTTP ${r.status}`));return}return m(r,i=>{var c,l,d,h;i.type==="done"&&i.sessionId?(c=e.onDone)==null||c.call(e,{sessionId:i.sessionId,content:i.content,messageId:i.messageId}):i.type==="done"?(l=e.onDone)==null||l.call(e,{sessionId:"",content:i.content,messageId:i.messageId}):i.type==="error"?(d=e.onError)==null||d.call(e,new Error(i.content)):(h=e.onMessage)==null||h.call(e,i)},i=>{var c;return(c=e.onError)==null?void 0:c.call(e,i)})}).catch(r=>{var s;(r==null?void 0:r.name)!=="AbortError"&&((s=e.onError)==null||s.call(e,r instanceof Error?r:new Error(String(r))))}):a.post("/chat",o).then(r=>{var s;(s=e.onDone)==null||s.call(e,{sessionId:r.sessionId,content:r.content??""})}).catch(r=>{var s;(s=e.onError)==null||s.call(e,r instanceof Error?r:new Error(String(r)))}),t}function b(a,e){const t=new AbortController,n=()=>t.abort();return a.addEventListener("abort",n,{once:!0}),e.addEventListener("abort",n,{once:!0}),(a.aborted||e.aborted)&&t.abort(),t.signal}async function T(a){return(await a.post("/chat/sessions",{botId:a.getBotId()})).sessionId}function S(a){return{sessionId:a.threadId??a.sessionId,botId:a.botId,title:a.title,createTime:a.createTime,updateTime:a.updateTime}}async function p(a,e){const t=await a.get("/chat/conversations",{botId:a.getBotId(),page:(e==null?void 0:e.page)??1,size:(e==null?void 0:e.size)??20});return{...t,items:t.items.map(S)}}async function $(a,e,t){await a.patch(`/chat/conversations/${e}`,{title:t})}async function A(a,e){await a.del(`/chat/conversations/${e}`)}async function C(a,e){const t=await a.get(`/chat/conversations/${e}/messages`);return O(t)}function O(a){var n,o,r;const e=[],t=[...a].sort((s,i)=>(s.seq??0)-(i.seq??0));for(const s of t){const i=s.role;if(i==="system")continue;const c=s.messageId??`msg_${s.id}`,l=new Date(s.createTime??"").getTime()||Date.now();if(i==="human")e.push({id:c,role:"user",parts:[{type:"text",content:s.content??""}],timestamp:l});else if(i==="ai"){const d=[];s.reasoning&&d.push({type:"thinking",content:s.reasoning}),s.content&&d.push({type:"text",content:s.content});const h=s.toolCalls;if(h&&Array.isArray(h))for(const u of h){let g;try{g=typeof u.args=="string"?u.args:JSON.stringify(u.args??{})}catch{g=String(u.args??"{}")}d.push({type:"tool_call",toolName:u.name??"",toolCallId:u.id??"",args:g})}e.push({id:c,role:"assistant",parts:d,timestamp:l})}else if(i==="tool"){const d=Array.isArray(s.artifacts)?s.artifacts:void 0;e.push({id:c,role:"assistant",parts:[{type:"tool_result",toolName:s.toolName??"",toolCallId:s.toolCallId??"",content:s.content??"",artifacts:d,renderMode:((n=s.metadata)==null?void 0:n.renderMode)??((o=s.metadata)==null?void 0:o.render_mode),html:(r=s.metadata)==null?void 0:r.html}],timestamp:l})}}return e}async function _(a,e,t,n,o,r){const s=await a.streamPost(`/chat/interrupt/${e}/respond`,{sessionId:t,action:n.action,value:n.value});if(!s.ok){const i=new Error(`HTTP ${s.status}`);throw r==null||r(i),i}o&&await m(s,i=>{i.type==="error"?r==null||r(new Error(i.content)):o(i)},i=>r==null?void 0:r(i))}function v(a){const e=new I(a);return{getToken:()=>e.fetchToken(),createSession:()=>T(e),chat:n=>k(e,n),listConversations:n=>p(e,n),renameConversation:(n,o)=>$(e,n,o),deleteConversation:n=>A(e,n),getMessages:n=>C(e,n),respondInterrupt:(n,o,r,s,i)=>_(e,n,o,r,s,i),setToken:n=>e.setToken(n),feedback:async(n,o,r)=>{const s={type:o};o==="dislike"&&((r==null?void 0:r.reason)!=null&&(s.reason=r.reason),r!=null&&r.remark&&(s.remark=r.remark)),await e.post(`/chat/messages/${n}/feedback`,s)},cancelFeedback:async n=>{await e.del(`/chat/messages/${n}/feedback`)}}}exports.createSuperAgent=v;
|
|
5
|
+
`))!==-1;){const l=r.slice(0,c);r=r.slice(c+2);const d=y(l);d&&e(d)}}if(r.trim()){const s=y(r);s&&e(s)}}catch(s){(s==null?void 0:s.name)!=="AbortError"&&t(s instanceof Error?s:new Error(String(s)))}finally{try{n.releaseLock()}catch{}}}function k(a,e){const t=new AbortController,n=e.signal?b(e.signal,t.signal):t.signal,o={botId:a.getBotId(),message:e.message,sessionId:e.sessionId,stream:e.stream!==!1};return o.stream?a.streamPost("/chat",o,n).then(r=>{var s;if(!r.ok){(s=e.onError)==null||s.call(e,new Error(`HTTP ${r.status}`));return}return m(r,i=>{var c,l,d,h;i.type==="done"&&i.sessionId?(c=e.onDone)==null||c.call(e,{sessionId:i.sessionId,content:i.content,messageId:i.messageId}):i.type==="done"?(l=e.onDone)==null||l.call(e,{sessionId:"",content:i.content,messageId:i.messageId}):i.type==="error"?(d=e.onError)==null||d.call(e,new Error(i.content)):(h=e.onMessage)==null||h.call(e,i)},i=>{var c;return(c=e.onError)==null?void 0:c.call(e,i)})}).catch(r=>{var s;(r==null?void 0:r.name)!=="AbortError"&&((s=e.onError)==null||s.call(e,r instanceof Error?r:new Error(String(r))))}):a.post("/chat",o).then(r=>{var s;(s=e.onDone)==null||s.call(e,{sessionId:r.sessionId,content:r.content??"",messageId:r.messageId})}).catch(r=>{var s;(s=e.onError)==null||s.call(e,r instanceof Error?r:new Error(String(r)))}),t}function b(a,e){const t=new AbortController,n=()=>t.abort();return a.addEventListener("abort",n,{once:!0}),e.addEventListener("abort",n,{once:!0}),(a.aborted||e.aborted)&&t.abort(),t.signal}async function T(a){return(await a.post("/chat/sessions",{botId:a.getBotId()})).sessionId}function S(a){return{sessionId:a.threadId??a.sessionId,botId:a.botId,title:a.title,createTime:a.createTime,updateTime:a.updateTime}}async function p(a,e){const t=await a.get("/chat/conversations",{botId:a.getBotId(),page:(e==null?void 0:e.page)??1,size:(e==null?void 0:e.size)??20});return{...t,items:t.items.map(S)}}async function $(a,e,t){await a.patch(`/chat/conversations/${e}`,{title:t})}async function A(a,e){await a.del(`/chat/conversations/${e}`)}async function C(a,e){const t=await a.get(`/chat/conversations/${e}/messages`);return O(t)}function O(a){var n,o,r;const e=[],t=[...a].sort((s,i)=>(s.seq??0)-(i.seq??0));for(const s of t){const i=s.role;if(i==="system")continue;const c=s.messageId??`msg_${s.id}`,l=new Date(s.createTime??"").getTime()||Date.now();if(i==="human")e.push({id:c,role:"user",parts:[{type:"text",content:s.content??""}],timestamp:l});else if(i==="ai"){const d=[];s.reasoning&&d.push({type:"thinking",content:s.reasoning}),s.content&&d.push({type:"text",content:s.content});const h=s.toolCalls;if(h&&Array.isArray(h))for(const u of h){let g;try{g=typeof u.args=="string"?u.args:JSON.stringify(u.args??{})}catch{g=String(u.args??"{}")}d.push({type:"tool_call",toolName:u.name??"",toolCallId:u.id??"",args:g})}e.push({id:c,role:"assistant",parts:d,timestamp:l})}else if(i==="tool"){const d=Array.isArray(s.artifacts)?s.artifacts:void 0;e.push({id:c,role:"assistant",parts:[{type:"tool_result",toolName:s.toolName??"",toolCallId:s.toolCallId??"",content:s.content??"",artifacts:d,renderMode:((n=s.metadata)==null?void 0:n.renderMode)??((o=s.metadata)==null?void 0:o.render_mode),html:(r=s.metadata)==null?void 0:r.html}],timestamp:l})}}return e}async function _(a,e,t,n,o,r){const s=await a.streamPost(`/chat/interrupt/${e}/respond`,{sessionId:t,action:n.action,value:n.value});if(!s.ok){const i=new Error(`HTTP ${s.status}`);throw r==null||r(i),i}o&&await m(s,i=>{i.type==="error"?r==null||r(new Error(i.content)):o(i)},i=>r==null?void 0:r(i))}function v(a){const e=new I(a);return{getToken:()=>e.fetchToken(),createSession:()=>T(e),chat:n=>k(e,n),listConversations:n=>p(e,n),renameConversation:(n,o)=>$(e,n,o),deleteConversation:n=>A(e,n),getMessages:n=>C(e,n),respondInterrupt:(n,o,r,s,i)=>_(e,n,o,r,s,i),setToken:n=>e.setToken(n),feedback:async(n,o,r)=>{const s={type:o};o==="dislike"&&((r==null?void 0:r.reason)!=null&&(s.reason=r.reason),r!=null&&r.remark&&(s.remark=r.remark)),await e.post(`/chat/messages/${n}/feedback`,s)},cancelFeedback:async n=>{await e.del(`/chat/messages/${n}/feedback`)}}}exports.createSuperAgent=v;
|
package/dist/index.mjs
CHANGED
package/dist/widget.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),ve=require("react-dom/client"),e=require("react/jsx-runtime");function U(a){const{sdk:t,sessionId:r,onSessionCreated:s,onStreamStart:n,onStreamEnd:o,onError:i,onMessageSend:l}=a,[c,p]=d.useState([]),[f,u]=d.useState("ready"),[m,v]=d.useState(null),y=d.useRef(null),h=d.useRef(!1),b=d.useRef(r);b.current=r;const x=d.useCallback(S=>{if(h.current)return;h.current=!0,l==null||l(S);const A={id:`user_${Date.now()}`,role:"user",parts:[{type:"text",content:S}],timestamp:Date.now()},k=`assistant_${Date.now()}`,I={id:k,role:"assistant",parts:[],timestamp:Date.now()};p(z=>[...z,A,I]),u("submitted"),v(null);const D=z=>{let M="",E="",P=[],C=!1;const L=()=>{const w=[...P];if(M){const O=w.findIndex($=>$.type==="thinking");O>=0?w[O]={type:"thinking",content:M}:w.unshift({type:"thinking",content:M})}if(E){const O=w.findLastIndex($=>$.type==="text");O>=0?w[O]={type:"text",content:E}:w.push({type:"text",content:E})}p(O=>O.map($=>$.id===k?{...$,parts:w}:$))};y.current=t.chat({sessionId:z,message:S,stream:!0,onMessage:w=>{switch(C||(C=!0,u("streaming")),w.type){case"thinking":M+=w.content;break;case"ai":E+=w.content;break;case"tool_call":P.push({type:"tool_call",toolName:w.toolName,toolCallId:w.toolCallId,args:w.args??"{}"});break;case"tool_result":P.push({type:"tool_result",toolName:w.toolName??"",toolCallId:w.toolCallId,content:w.content,artifacts:w.artifacts,renderMode:w.renderMode,html:w.html}),E&&(P.push({type:"text",content:E}),E="");break;case"interrupt":w.interrupt&&P.push({type:"interrupt",interrupt:w.interrupt,resolved:!1});break}L()},onDone:w=>{(E||P.length>0||M)&&L(),w.messageId&&p(O=>O.map($=>$.id===k?{...$,id:w.messageId}:$)),w.sessionId&&w.sessionId!==b.current&&(s==null||s(w.sessionId)),o==null||o(w.sessionId||z),u("ready"),h.current=!1,y.current=null},onError:w=>{P.push({type:"error",content:w.message}),L(),v(w),u("error"),i==null||i(w),h.current=!1,y.current=null}}),n==null||n(z)};b.current?D(b.current):t.createSession().then(z=>{b.current=z,s==null||s(z),D(z)}).catch(z=>{v(z instanceof Error?z:new Error(String(z))),u("error"),i==null||i(z instanceof Error?z:new Error(String(z))),h.current=!1})},[t,s,n,o,i,l]),g=d.useCallback(()=>{var S;(S=y.current)==null||S.abort(),y.current=null,u("ready"),h.current=!1},[]),N=d.useCallback(()=>{if(h.current)return;const S=c.findLastIndex(k=>k.role==="user");if(S<0)return;const A=c[S].parts.filter(k=>k.type==="text").map(k=>k.content).join("");p(k=>k.slice(0,S)),x(A)},[c,x]),T=d.useCallback((S,A)=>{p(C=>C.map(L=>({...L,parts:L.parts.map(w=>w.type==="interrupt"&&w.interrupt.interruptId===S?{...w,resolved:!0,response:A}:w)})));const k=b.current;if(!k)return;const I=`assistant_${Date.now()}`,D={id:I,role:"assistant",parts:[],timestamp:Date.now()};p(C=>[...C,D]),u("streaming");let z="",M="",E=[];const P=()=>{const C=[...E];if(z){const L=C.findIndex(w=>w.type==="thinking");L>=0?C[L]={type:"thinking",content:z}:C.unshift({type:"thinking",content:z})}if(M){const L=C.findLastIndex(w=>w.type==="text");L>=0?C[L]={type:"text",content:M}:C.push({type:"text",content:M})}p(L=>L.map(w=>w.id===I?{...w,parts:C}:w))};t.respondInterrupt(S,k,A,C=>{switch(C.type){case"thinking":z+=C.content;break;case"ai":M+=C.content;break;case"tool_call":E.push({type:"tool_call",toolName:C.toolName,toolCallId:C.toolCallId,args:C.args??"{}"});break;case"tool_result":E.push({type:"tool_result",toolName:C.toolName??"",toolCallId:C.toolCallId,content:C.content,artifacts:C.artifacts,renderMode:C.renderMode,html:C.html}),M&&(E.push({type:"text",content:M}),M="");break;case"interrupt":C.interrupt&&E.push({type:"interrupt",interrupt:C.interrupt,resolved:!1});break;case"done":u("ready");break}P()},C=>{E.push({type:"error",content:C.message}),P(),v(C),u("error"),i==null||i(C)}).catch(C=>{i==null||i(C instanceof Error?C:new Error(String(C))),u("error")})},[t,i]);return{messages:c,status:f,error:m,sendMessage:x,stop:g,regenerate:N,setMessages:p,respondToInterrupt:T}}const F="sa_active_conversation";function V(){try{return localStorage.getItem(F)}catch{return null}}function we(a){try{a?localStorage.setItem(F,a):localStorage.removeItem(F)}catch{}}function X(a){const{sdk:t,onConversationChange:r}=a,[s,n]=d.useState([]),[o,i]=d.useState(()=>V()),[l,c]=d.useState(!1),p=d.useRef(!0);d.useEffect(()=>()=>{p.current=!1},[]);const f=d.useCallback(x=>{i(x),we(x),x&&(r==null||r(x))},[r]),u=d.useCallback(async()=>{c(!0);try{const x=await t.listConversations({page:1,size:100});if(!p.current)return;n(x.items);const g=V();g&&!x.items.find(N=>N.sessionId===g)&&f(null)}catch{}finally{p.current&&c(!1)}},[t,f]),m=d.useCallback(async x=>(f(x),await t.getMessages(x)),[t,f]),v=d.useCallback(()=>{f(null)},[f]),y=d.useCallback(async x=>{await t.deleteConversation(x),n(g=>g.filter(N=>N.sessionId!==x)),o===x&&f(null)},[t,o,f]),h=d.useCallback(async(x,g)=>{await t.renameConversation(x,g),n(N=>N.map(T=>T.sessionId===x?{...T,title:g}:T))},[t]),{enabled:b=!0}=a;return d.useEffect(()=>{b&&u()},[b,u]),{conversations:s,activeConversationId:o,loading:l,loadConversations:u,switchConversation:m,newConversation:v,deleteConversation:y,renameConversation:h,setActiveConversationId:f}}function J(a){return!a&&a!==0?"":a<1024?`${a} B`:a<1024*1024?`${(a/1024).toFixed(1)} KB`:`${(a/1024/1024).toFixed(1)} MB`}function G(a){return a==="pdf"?"PDF":a==="docx"?"DOC":a==="html"?"HTM":a.slice(0,3).toUpperCase()||"FILE"}function Z(a){if(!a)return[];const t=[];for(const r of a)r.type==="file"&&r.data&&t.push({id:r.data.id??"",type:r.data.fileType??r.data.type??"",filename:r.data.name??r.data.filename??"",url:r.data.url??"",mime:r.data.mime,size:r.data.size,summary:r.data.summary});return t}async function _(a){try{const t=await fetch(a.url);if(!t.ok)throw new Error(`HTTP ${t.status}`);const r=await t.blob(),s=URL.createObjectURL(r),n=document.createElement("a");n.href=s,n.download=a.filename,document.body.appendChild(n),n.click(),n.remove(),setTimeout(()=>URL.revokeObjectURL(s),4e3)}catch{const t=document.createElement("a");t.href=a.url,t.download=a.filename,t.target="_blank",document.body.appendChild(t),t.click(),t.remove()}}const Q=d.createContext(null);function B(){const a=d.useContext(Q);if(!a)throw new Error("useChatContext must be used within ChatProvider");return a}function W({sdk:a,hooks:t,children:r}){const[s,n]=d.useState(!1),[o,i]=d.useState(!1),[l,c]=d.useState(null),[p,f]=d.useState(!1);d.useEffect(()=>{a.getToken().then(()=>{f(!0)}).catch(()=>{f(!0)})},[a]);const u=X({sdk:a,onConversationChange:t==null?void 0:t.onConversationChange,enabled:p}),m=U({sdk:a,sessionId:u.activeConversationId,onSessionCreated:k=>{u.setActiveConversationId(k),u.loadConversations()},onStreamStart:t==null?void 0:t.onStreamStart,onStreamEnd:k=>{var I;(I=t==null?void 0:t.onStreamEnd)==null||I.call(t,k),u.loadConversations()},onError:t==null?void 0:t.onError,onMessageSend:t==null?void 0:t.onMessageSend}),v=d.useCallback(async k=>{const I=await u.switchConversation(k);m.setMessages(I),n(!1)},[u,m]),y=d.useCallback(()=>{u.newConversation(),m.setMessages([]),n(!1)},[u,m]),h=d.useCallback(async k=>{await u.deleteConversation(k),u.activeConversationId===k&&m.setMessages([])},[u,m]);d.useEffect(()=>{u.activeConversationId&&m.messages.length===0&&a.getMessages(u.activeConversationId).then(k=>{m.setMessages(k)}).catch(()=>{})},[]);const b=d.useCallback(()=>{n(k=>!k)},[]),x=d.useCallback(()=>{i(k=>!k),c(k=>null)},[]),g=d.useCallback(k=>{c(k),i(!0)},[]),N=d.useMemo(()=>{const k=new Set,I=[];for(const D of m.messages)for(const z of D.parts)if(z.type==="tool_result"&&z.artifacts)for(const M of Z(z.artifacts)){const E=M.id||`${M.url}:${M.filename}`;k.has(E)||(k.add(E),I.push(M))}return I},[m.messages]),T=d.useCallback((k,I,D)=>{a.feedback(k,I,D).catch(()=>{})},[a]),S=d.useCallback(k=>{a.cancelFeedback(k).catch(()=>{})},[a]),A=d.useMemo(()=>({status:m.status,error:m.error,messages:m.messages,conversations:u.conversations,activeConversationId:u.activeConversationId,isThreadListOpen:s,isArtifactDrawerOpen:o,allArtifacts:N,activePreviewArtifact:l,sendMessage:m.sendMessage,stop:m.stop,regenerate:m.regenerate,switchConversation:v,newConversation:y,deleteConversation:h,renameConversation:u.renameConversation,toggleThreadList:b,toggleArtifactDrawer:x,openArtifactPreview:g,feedback:T,cancelFeedback:S,respondToInterrupt:m.respondToInterrupt}),[m.status,m.error,m.messages,m.sendMessage,m.stop,m.regenerate,m.respondToInterrupt,u.conversations,u.activeConversationId,u.renameConversation,s,o,N,l,v,y,h,b,x,g,T,S]);return e.jsx(Q.Provider,{value:A,children:r})}function j({name:a,size:t=16,color:r="currentColor",className:s,style:n}){return e.jsx("svg",{className:s,style:{width:t,height:t,fill:r,verticalAlign:"middle",...n},"aria-hidden":"true",children:e.jsx("use",{href:`#icon-${a}`})})}function ee({isOpen:a,onClick:t}){return e.jsx("button",{className:`sa-trigger ${a?"sa-trigger-open":""}`,onClick:t,children:a?e.jsx(j,{name:"x",size:24,color:"#fff"}):e.jsx(j,{name:"message-square",size:24,color:"#fff"})})}function ae({title:a,onClose:t,onToggleThreadList:r,showClose:s=!0,artifactCount:n=0,onToggleArtifactDrawer:o}){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:[n>0&&o&&e.jsxs("button",{className:"sa-header-btn sa-header-btn-artifact",onClick:o,title:"会话产物",children:[e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{children:"产物"})]}),e.jsx("button",{className:"sa-header-btn",onClick:r,title:"会话列表",children:e.jsx(j,{name:"menu",size:16})}),s&&e.jsx("button",{className:"sa-header-btn",onClick:t,title:"关闭",children:e.jsx(j,{name:"x",size:16})})]})]})}function ke(a){const t=new Date,r=new Date(t.getFullYear(),t.getMonth(),t.getDate()).getTime(),s=r-6*864e5,n=[],o=[],i=[];for(const c of a){const p=new Date(c.updateTime).getTime();p>=r?n.push(c):p>=s?o.push(c):i.push(c)}const l=[];return n.length&&l.push({label:"今天",items:n}),o.length&&l.push({label:"最近 7 天",items:o}),i.length&&l.push({label:"更早",items:i}),l}function te(){const{conversations:a,activeConversationId:t,switchConversation:r,newConversation:s,deleteConversation:n,renameConversation:o,isThreadListOpen:i,toggleThreadList:l}=B(),[c,p]=d.useState(null),[f,u]=d.useState(""),m=d.useCallback((h,b)=>{p(h),u(b??"")},[]),v=d.useCallback(h=>{f.trim()&&o(h,f.trim()),p(null)},[f,o]),y=d.useMemo(()=>ke(a),[a]);return i?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"sa-thread-list-overlay",onClick:l}),e.jsxs("div",{className:"sa-thread-list",children:[e.jsxs("div",{className:"sa-thread-list-header",children:[e.jsx("span",{className:"sa-thread-list-title",children:"会话记录"}),e.jsxs("button",{className:"sa-thread-list-new",onClick:s,children:[e.jsx(j,{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:"暂无会话"}),y.map(h=>e.jsxs("div",{className:"sa-thread-group",children:[e.jsx("div",{className:"sa-thread-group-label",children:h.label}),h.items.map(b=>e.jsxs("div",{className:`sa-thread-item ${b.sessionId===t?"active":""}`,onClick:()=>r(b.sessionId),children:[c===b.sessionId?e.jsx("input",{className:"sa-thread-item-edit",value:f,onChange:x=>u(x.target.value),onBlur:()=>v(b.sessionId),onKeyDown:x=>{x.key==="Enter"&&v(b.sessionId),x.key==="Escape"&&p(null)},onClick:x=>x.stopPropagation(),autoFocus:!0}):e.jsx("span",{className:"sa-thread-item-title",onDoubleClick:x=>{x.stopPropagation(),m(b.sessionId,b.title)},children:b.title||"新会话"}),e.jsx("button",{className:"sa-thread-item-delete",onClick:x=>{x.stopPropagation(),n(b.sessionId)},title:"删除",children:e.jsx(j,{name:"trash2",size:14})})]},b.sessionId))]},h.label))]})]})]}):null}function se(a){const t=d.useRef(null),r=d.useRef(!1),s=d.useRef(0),n=d.useCallback(()=>{const i=t.current;if(!i)return;const{scrollTop:l,scrollHeight:c,clientHeight:p}=i,f=c-l-p<40;l<s.current&&!f&&(r.current=!0),f&&(r.current=!1),s.current=l},[]);d.useEffect(()=>{const i=t.current;if(i)return i.addEventListener("scroll",n,{passive:!0}),()=>i.removeEventListener("scroll",n)},[n]),d.useEffect(()=>{if(r.current)return;const i=t.current;i&&requestAnimationFrame(()=>{i.scrollTop=i.scrollHeight})},a);const o=d.useCallback(()=>{r.current=!1;const i=t.current;i&&(i.scrollTop=i.scrollHeight)},[]);return{containerRef:t,scrollToBottom:o}}function R(a){return a.value++}function H(a,t){const r=[],s=[],n=()=>{s.length>0&&(r.push(s.join("")),s.length=0)};let o=0;for(;o<a.length;){const i=a[o];if(i==="`"){const l=a.indexOf("`",o+1);if(l!==-1){n(),r.push(e.jsx("code",{className:"sa-inline-code",children:a.slice(o+1,l)},R(t))),o=l+1;continue}}else if(i==="*"&&a[o+1]==="*"){const l=a.indexOf("**",o+2);if(l!==-1){n(),r.push(e.jsx("strong",{children:H(a.slice(o+2,l),t)},R(t))),o=l+2;continue}}else if(i==="*"){const l=a.indexOf("*",o+1);if(l!==-1){n(),r.push(e.jsx("em",{children:H(a.slice(o+1,l),t)},R(t))),o=l+1;continue}}else if(i==="["){const l=a.indexOf("]",o+1);if(l!==-1&&a[l+1]==="("){const c=a.indexOf(")",l+2);if(c!==-1){const p=a.slice(o+1,l),f=a.slice(l+2,c);n(),r.push(e.jsx("a",{href:f,target:"_blank",rel:"noreferrer",children:H(p,t)},R(t))),o=c+1;continue}}}s.push(i),o++}return n(),r}function ye(a,t){const r=[],s=a.split(`
|
|
2
|
-
`);let n=0;const o=c=>c.trim()==="",i=c=>/^#{1,6}\s+\S/.test(c.trim()),l=c=>/^(?:[-*+]|\d+[.)])\s+\S/.test(c.trim());for(;n<s.length;){const c=s[n],p=c.trim();if(o(c)){n++;continue}if(i(c)){const u=p.match(/^(#{1,6})\s+(.*)$/);if(u){const v=`h${Math.min(u[1].length,6)}`;r.push(d.createElement(v,{key:R(t)},H(u[2],t)))}n++;continue}if(l(c)){const u=/^\d+[.)]/.test(p),m=[];for(;n<s.length&&l(s[n]);){const v=s[n].trim().match(/^(?:[-*+]|\d+[.)])\s+(.*)$/);v&&m.push(e.jsx("li",{children:H(v[1],t)},R(t))),n++}r.push(u?e.jsx("ol",{className:"sa-list",children:m},R(t)):e.jsx("ul",{className:"sa-list",children:m},R(t)));continue}const f=[];for(;n<s.length&&!o(s[n])&&!i(s[n])&&!l(s[n]);)f.push(s[n]),n++;r.push(e.jsx("p",{className:"sa-text-paragraph",children:H(f.join(" "),t)},R(t)))}return r}function je(a){const t={value:0},r=[],s=a.split("```");for(let n=0;n<s.length;n++)if(n%2===0)r.push(...ye(s[n],t));else if(s[n].trim()!==""){let o=s[n].replace(/^\r?\n/,"");o=o.replace(/^[a-zA-Z0-9_+#.-]+\r?\n/,""),o=o.replace(/\r?\n$/,""),r.push(e.jsx("pre",{className:"sa-code-block",children:e.jsx("code",{children:o})},R(t)))}return r}function re({content:a}){return e.jsx("div",{className:"sa-text-part",children:je(a)})}function ne({content:a}){const[t,r]=d.useState(!1);return e.jsxs("div",{className:"sa-thinking-part",onClick:()=>r(!t),children:[e.jsxs("div",{className:"sa-thinking-header",children:[e.jsx("span",{className:"sa-thinking-icon",children:e.jsx(j,{name:"brain",size:14})}),e.jsx("span",{className:"sa-thinking-label",children:"思考过程"}),e.jsx("span",{className:`sa-thinking-arrow ${t?"expanded":""}`,children:e.jsx(j,{name:"chevron-right",size:12})})]}),t&&e.jsx("div",{className:"sa-thinking-content",children:a})]})}function ie({toolName:a,toolCallId:t,args:r}){const[s,n]=d.useState(!1);let o=r;try{o=JSON.stringify(JSON.parse(r),null,2)}catch{}return e.jsxs("div",{className:"sa-tool-call-part",children:[e.jsxs("div",{className:"sa-tool-call-header",onClick:()=>n(!s),children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(j,{name:"zap",size:14})}),e.jsxs("span",{className:"sa-tool-name",children:["调用工具: ",a]}),e.jsx("span",{className:`sa-thinking-arrow ${s?"expanded":""}`,children:e.jsx(j,{name:"chevron-right",size:12})})]}),s&&e.jsx("pre",{className:"sa-tool-args",children:o})]})}function Ce({html:a}){const[t,r]=d.useState(null);return d.useEffect(()=>{let s=!1;return Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs")).then(n=>{s||r(n.default.sanitize(a,{FORCE_BODY:!0}))}),()=>{s=!0}},[a]),t===null?null:e.jsx("div",{className:"sa-tool-result-html",children:e.jsx("iframe",{srcDoc:t,className:"sa-tool-result-html-iframe",sandbox:"",title:"HTML 产物"})})}function oe({toolName:a,content:t,artifacts:r,renderMode:s,html:n,artifactPreview:o}){const{openArtifactPreview:i}=B(),l=Z(r);return e.jsxs("div",{className:"sa-tool-result-part",children:[e.jsxs("div",{className:"sa-tool-result-header",children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(j,{name:"circle-check",size:14,color:"#10b981"})}),e.jsx("span",{className:"sa-tool-result-label",children:a||"工具返回"})]}),s==="html"&&n?e.jsx(Ce,{html:n}):t?e.jsx("div",{className:"sa-tool-result-content",children:t}):null,l.length>0&&e.jsx("ul",{className:"sa-artifact-file-list",role:"list",children:l.map((c,p)=>e.jsxs("li",{className:"sa-artifact-file-card",onClick:o?()=>i(c):void 0,onKeyDown:o?f=>{(f.key==="Enter"||f.key===" ")&&(f.preventDefault(),i(c))}:void 0,tabIndex:o?0:void 0,role:o?"button":void 0,"aria-label":o?`预览 ${c.filename}`:void 0,style:o?{cursor:"pointer"}:void 0,children:[e.jsx("div",{className:"sa-artifact-file-icon",children:e.jsx("span",{className:"sa-artifact-chip-type",children:G(c.type)})}),e.jsxs("div",{className:"sa-artifact-file-info",children:[e.jsx("span",{className:"sa-artifact-file-name",children:c.filename}),c.size!=null&&e.jsx("span",{className:"sa-artifact-file-size",children:J(c.size)})]}),o&&e.jsx("button",{className:"sa-artifact-file-download",title:"下载","aria-label":`下载 ${c.filename}`,onClick:f=>{f.stopPropagation(),_(c)},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})})]},c.id||`art-${p}`))})]})}function le({content:a,onRetry:t}){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(j,{name:"triangle-alert",size:14})}),e.jsx("span",{children:a})]}),t&&e.jsx("button",{className:"sa-error-retry",onClick:t,children:"重试"})]})}function Ne({interrupt:a,onRespond:t}){var o,i;const r=a.options,s=(typeof(r==null?void 0:r[0])=="string"?r[0]:(o=r==null?void 0:r[0])==null?void 0:o.label)||"确认",n=(typeof(r==null?void 0:r[1])=="string"?r[1]:(i=r==null?void 0:r[1])==null?void 0:i.label)||"取消";return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"circle-alert",size:16,color:"#f59e0b"}),e.jsx("span",{children:a.content})]}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",onClick:()=>t({action:"confirm"}),children:[e.jsx(j,{name:"check",size:14})," ",s]}),e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-secondary",onClick:()=>t({action:"cancel"}),children:[e.jsx(j,{name:"x",size:14})," ",n]})]})]})}function ze({interrupt:a,onRespond:t}){var l,c,p,f;const r=a.options,s=(typeof(r==null?void 0:r[0])=="string"?r[0]:(l=r==null?void 0:r[0])==null?void 0:l.label)||"批准",n=(typeof(r==null?void 0:r[1])=="string"?r[1]:(c=r==null?void 0:r[1])==null?void 0:c.label)||"拒绝",o=(typeof(r==null?void 0:r[2])=="string"?r[2]:(p=r==null?void 0:r[2])==null?void 0:p.label)||"跳过",i=((f=a.metadata)==null?void 0:f.showSkip)!==!1;return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"circle-alert",size:16,color:"#f59e0b"}),e.jsx("span",{children:a.content})]}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",onClick:()=>t({action:"approve"}),children:[e.jsx(j,{name:"check",size:14})," ",s]}),e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-danger",onClick:()=>t({action:"reject"}),children:[e.jsx(j,{name:"x",size:14})," ",n]}),i&&e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-secondary",onClick:()=>t({action:"skip"}),children:[e.jsx(j,{name:"chevron-right",size:14})," ",o]})]})]})}function Se(a){return typeof a=="string"?{value:a,label:a}:a}function Te({interrupt:a,onRespond:t}){const r=(a.options??[]).map(Se);return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"list",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-options",children:r.map(s=>e.jsxs("button",{className:`sa-interrupt-option ${s.danger?"sa-interrupt-option-danger":""}`,onClick:()=>t({action:"select",value:s.value}),children:[s.icon&&e.jsx(j,{name:s.icon,size:14}),e.jsx("span",{children:s.label}),s.description&&e.jsx("span",{className:"sa-interrupt-option-desc",children:s.description})]},s.value))})]})}function Ie(a){return typeof a=="string"?{value:a,label:a}:a}function Me({interrupt:a,onRespond:t}){const r=(a.options??[]).map(Ie),[s,n]=d.useState(new Set),o=i=>{n(l=>{const c=new Set(l);return c.has(i)?c.delete(i):c.add(i),c})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"list-checks",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-options",children:r.map(i=>e.jsxs("button",{className:`sa-interrupt-option ${s.has(i.value)?"sa-interrupt-option-selected":""}`,onClick:()=>o(i.value),children:[e.jsx(j,{name:s.has(i.value)?"square-check":"square",size:14}),e.jsx("span",{children:i.label})]},i.value))}),e.jsx("div",{className:"sa-interrupt-actions",children:e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:s.size===0,onClick:()=>t({action:"submit",value:Array.from(s)}),children:[e.jsx(j,{name:"check",size:14})," 确认 (",s.size,")"]})})]})}function Ee({interrupt:a,onRespond:t}){const[r,s]=d.useState(""),n=a.inputType??"text",o=n==="textarea",i=()=>{a.required&&!r.trim()||t({action:"submit",value:r.trim()})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"text-cursor-input",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-input-area",children:o?e.jsx("textarea",{className:"sa-interrupt-textarea",value:r,onChange:l=>s(l.target.value),placeholder:a.placeholder,maxLength:a.maxLength,rows:3}):e.jsx("input",{className:"sa-interrupt-input",type:n,value:r,onChange:l=>s(l.target.value),placeholder:a.placeholder,maxLength:a.maxLength,onKeyDown:l=>l.key==="Enter"&&i()})}),e.jsx("div",{className:"sa-interrupt-actions",children:e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:a.required?!r.trim():!1,onClick:i,children:[e.jsx(j,{name:"check",size:14})," 提交"]})})]})}function De({interrupt:a,onRespond:t}){const r=a.fields??[],[s,n]=d.useState(()=>{const c={};for(const p of r)c[p.name]=p.defaultValue??"";return c}),o=(c,p)=>{n(f=>({...f,[c]:p}))},i=r.every(c=>!c.required||s[c.name]),l=()=>{i&&t({action:"submit",value:s})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"file-text",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-form",children:r.map(c=>e.jsxs("div",{className:"sa-interrupt-form-field",children:[e.jsxs("label",{className:"sa-interrupt-form-label",children:[c.label,c.required&&e.jsx("span",{className:"sa-interrupt-form-required",children:"*"})]}),Le(c,s[c.name],p=>o(c.name,p))]},c.name))}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:!i,onClick:l,children:[e.jsx(j,{name:"check",size:14})," 提交"]}),e.jsx("button",{className:"sa-interrupt-btn sa-interrupt-btn-secondary",onClick:()=>t({action:"cancel"}),children:"取消"})]})]})}function Le(a,t,r){switch(a.type){case"textarea":return e.jsx("textarea",{className:"sa-interrupt-textarea",value:t,onChange:s=>r(s.target.value),placeholder:a.placeholder,rows:3});case"select":return e.jsxs("select",{className:"sa-interrupt-select",value:t,onChange:s=>r(s.target.value),children:[e.jsx("option",{value:"",children:"请选择"}),(a.options??[]).map(s=>e.jsx("option",{value:s,children:s},s))]});default:return e.jsx("input",{className:"sa-interrupt-input",type:a.type,value:t,onChange:s=>r(s.target.value),placeholder:a.placeholder})}}function ce({interrupt:a,resolved:t,response:r,onRespond:s}){if(t)return e.jsxs("div",{className:"sa-interrupt-resolved",children:[e.jsx(j,{name:"circle-check",size:14,color:"#10b981"}),e.jsx("span",{children:Ae(a.interruptType,r)})]});switch(a.interruptType){case"confirm":return e.jsx(Ne,{interrupt:a,onRespond:s});case"approve":return e.jsx(ze,{interrupt:a,onRespond:s});case"select":return e.jsx(Te,{interrupt:a,onRespond:s});case"multiSelect":return e.jsx(Me,{interrupt:a,onRespond:s});case"input":return e.jsx(Ee,{interrupt:a,onRespond:s});case"form":return e.jsx(De,{interrupt:a,onRespond:s});default:return e.jsxs("div",{className:"sa-interrupt-unknown",children:["未知的交互类型: ",a.interruptType]})}}function Ae(a,t){if(!t)return"已完成";switch(t.action){case"confirm":return"已确认";case"cancel":return"已取消";case"select":return`已选择: ${t.value}`;case"submit":return"已提交";case"approve":return"已批准";case"reject":return"已拒绝";case"skip":return"已跳过";default:return"已完成"}}const $e=[{value:1,label:"事实错误"},{value:2,label:"逻辑问题"},{value:3,label:"不相关"},{value:4,label:"信息过时"},{value:5,label:"冗长啰嗦"},{value:6,label:"难以理解"}];function de({message:a,onCopy:t,onRegenerate:r,onFeedback:s}){const{status:n,feedback:o,cancelFeedback:i}=B(),[l,c]=d.useState(null),[p,f]=d.useState(!1),[u,m]=d.useState(!1);if(n!=="ready"&&n!=="error")return null;const v=d.useCallback(()=>{t(),f(!0),setTimeout(()=>f(!1),2e3)},[t]),y=d.useCallback(()=>{l==="like"?(c(null),i(a.id)):(c("like"),m(!1),o(a.id,"like"),s==null||s(a.id,"like"))},[l,a.id,o,i,s]),h=d.useCallback(()=>{l==="dislike"?(c(null),m(!1),i(a.id)):m(!0)},[l,a.id,i]),b=d.useCallback((x,g)=>{c("dislike"),m(!1),o(a.id,"dislike",{reason:x,remark:g}),s==null||s(a.id,"dislike")},[a.id,o,s]);return e.jsxs("div",{className:"sa-action-bar",children:[e.jsxs("button",{className:"sa-action-btn",onClick:v,title:"复制",children:[e.jsx(j,{name:p?"circle-check":"copy",size:14})," ",p?"已复制":"复制"]}),e.jsxs("button",{className:"sa-action-btn",onClick:r,title:"重新生成",children:[e.jsx(j,{name:"refresh-cw",size:14})," 重新生成"]}),e.jsx("span",{className:"sa-action-divider"}),e.jsx("button",{className:`sa-action-btn ${l==="like"?"sa-action-btn-active":""}`,onClick:y,title:"有用","aria-label":"点赞",children:e.jsx(j,{name:"thumbs-up",size:14})}),e.jsx("button",{className:`sa-action-btn ${l==="dislike"?"sa-action-btn-active":""}`,onClick:h,title:"无用","aria-label":"踩",children:e.jsx(j,{name:"thumbs-down",size:14})}),u&&e.jsx(Pe,{onSubmit:b,onCancel:()=>m(!1)})]})}function Pe({onSubmit:a,onCancel:t}){const[r,s]=d.useState(void 0),[n,o]=d.useState("");return e.jsxs("div",{className:"sa-dislike-panel",children:[e.jsx("div",{className:"sa-dislike-panel-title",children:"请选择不满意的原因(可选)"}),e.jsx("div",{className:"sa-dislike-reasons",children:$e.map(i=>e.jsx("button",{className:`sa-dislike-reason-btn ${r===i.value?"active":""}`,onClick:()=>s(r===i.value?void 0:i.value),children:i.label},i.value))}),e.jsx("textarea",{className:"sa-dislike-remark",placeholder:"补充描述(选填,≤500 字)",maxLength:500,value:n,onChange:i=>o(i.target.value),rows:2}),e.jsxs("div",{className:"sa-dislike-actions",children:[e.jsx("button",{className:"sa-dislike-submit",onClick:()=>a(r,n||void 0),children:"提交"}),e.jsx("button",{className:"sa-dislike-cancel",onClick:t,children:"取消"})]})]})}function K({src:a,role:t}){const r=t==="assistant"?"A":"U";return a?a.startsWith("http")||a.startsWith("/")||a.startsWith("data:")?e.jsx("div",{className:`sa-avatar sa-avatar-${t}`,children:e.jsx("img",{src:a,alt:t,style:{width:"100%",height:"100%",borderRadius:"inherit",objectFit:"cover"}})}):e.jsx("div",{className:`sa-avatar sa-avatar-${t}`,children:a}):e.jsx("div",{className:`sa-avatar sa-avatar-${t}`,children:r})}function pe({message:a,isStreaming:t,isLast:r,slots:s,avatar:n,artifactPreview:o}){const{regenerate:i,feedback:l,respondToInterrupt:c}=B(),p=d.useCallback(()=>{var N;const g=a.parts.filter(T=>T.type==="text").map(T=>T.content).join(`
|
|
3
|
-
`);(N=navigator.clipboard)==null||N.writeText(g).catch(()=>{})},[a]),f=a.role==="user",u=s.Message;if(u)return e.jsx(u,{message:a,isStreaming:t,isLast:r,avatar:n,slots:s,onCopy:p,onRegenerate:i,onFeedback:l});const m=s.TextPart??re,v=s.ThinkingPart??ne,y=s.ToolCallPart??ie,h=s.ToolResultPart??oe,b=s.ErrorPart??le,x=s.ActionBar??de;return e.jsxs("div",{className:`sa-message ${f?"sa-message-user":"sa-message-assistant"}`,children:[!f&&e.jsx(K,{src:n==null?void 0:n.assistant,role:"assistant"}),e.jsxs("div",{className:"sa-message-body",children:[a.parts.map((g,N)=>{switch(g.type){case"text":return e.jsx(m,{content:g.content},N);case"thinking":return e.jsx(v,{content:g.content},N);case"tool_call":return e.jsx(y,{toolName:g.toolName,toolCallId:g.toolCallId,args:g.args},N);case"tool_result":return e.jsx(h,{toolName:g.toolName,toolCallId:g.toolCallId,content:g.content,artifacts:g.artifacts,renderMode:g.renderMode,html:g.html,artifactPreview:o},N);case"error":return e.jsx(b,{content:g.content,onRetry:i},N);case"interrupt":return e.jsx(ce,{interrupt:g.interrupt,resolved:g.resolved??!1,response:g.response,onRespond:T=>c(g.interrupt.interruptId,T)},N);default:return null}}),t&&r&&!f&&e.jsx("span",{className:"sa-streaming-cursor",children:"|"}),!f&&!t&&a.parts.length>0&&e.jsx(x,{message:a,onCopy:p,onRegenerate:i,onFeedback:l})]}),f&&e.jsx(K,{src:n==null?void 0:n.user,role:"user"})]})}function fe({message:a,suggestedPrompts:t,onPromptClick:r}){return e.jsxs("div",{className:"sa-welcome",children:[e.jsx("div",{className:"sa-welcome-icon",children:e.jsx(j,{name:"sparkles",size:40})}),e.jsx("p",{className:"sa-welcome-message",children:a||"你好!有什么可以帮你的?"}),t&&t.length>0&&e.jsx("div",{className:"sa-welcome-prompts",children:t.map((s,n)=>e.jsx("button",{className:"sa-welcome-prompt",onClick:()=>r(s),children:s},n))})]})}function q({slots:a,welcomeMessage:t,suggestedPrompts:r,avatar:s,artifactPreview:n}){const{messages:o,status:i,sendMessage:l}=B(),{containerRef:c}=se([o]),p=i==="streaming",f=a.WelcomeScreen??fe;return o.length===0?e.jsx("div",{className:"sa-thread",ref:c,children:e.jsx(f,{message:t,suggestedPrompts:r,onPromptClick:l})}):e.jsx("div",{className:"sa-thread",ref:c,children:o.map((u,m)=>e.jsx(pe,{message:u,isStreaming:p,isLast:m===o.length-1,slots:a,avatar:s,artifactPreview:n},u.id))})}function Y(){const{status:a,sendMessage:t,stop:r}=B(),[s,n]=d.useState(""),o=d.useRef(null),i=a==="streaming"||a==="submitted",l=d.useCallback(()=>{const f=s.trim();!f||i||(t(f),n(""),o.current&&(o.current.style.height="auto"))},[s,i,t]),c=d.useCallback(f=>{f.key==="Enter"&&!f.shiftKey&&(f.preventDefault(),l())},[l]),p=d.useCallback(f=>{n(f.target.value);const u=f.target;u.style.height="auto",u.style.height=Math.min(u.scrollHeight,120)+"px"},[]);return e.jsxs("div",{className:"sa-composer",children:[e.jsx("textarea",{ref:o,className:"sa-composer-input",value:s,onChange:p,onKeyDown:c,placeholder:"输入消息...",rows:1,disabled:a==="submitted"}),i?e.jsx("button",{className:"sa-composer-stop",onClick:r,title:"停止",children:e.jsx(j,{name:"circle-stop",size:16,color:"#fff"})}):e.jsx("button",{className:"sa-composer-send",onClick:l,disabled:!s.trim(),title:"发送",children:e.jsx(j,{name:"arrow-up",size:16,color:"#fff"})})]})}function Re({artifact:a}){const[t,r]=d.useState(null),[s,n]=d.useState(!0),[o,i]=d.useState(null);return d.useEffect(()=>{let l=!1;n(!0),i(null),r(null);async function c(){try{if(a.type==="pdf"){l||n(!1);return}if(a.type==="html"){const p=await fetch(a.url);if(!p.ok)throw new Error(`HTTP ${p.status}`);const f=await p.text(),u=(await Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs"))).default;l||(r(u.sanitize(f,{FORCE_BODY:!0})),n(!1));return}if(a.type==="docx"){const p=await fetch(a.url);if(!p.ok)throw new Error(`HTTP ${p.status}`);const f=await p.arrayBuffer(),m=await(await Promise.resolve().then(()=>require("./mammoth.browser-DiAPPO3x.cjs")).then(y=>y.mammoth_browser)).convertToHtml({arrayBuffer:f}),v=(await Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs"))).default;l||(r(v.sanitize(m.value,{FORCE_BODY:!0})),n(!1));return}l||(n(!1),i(`不支持预览「${a.type}」类型`))}catch(p){l||(n(!1),i(p instanceof Error?p.message:"加载失败"))}}return c(),()=>{l=!0}},[a]),s?e.jsx("div",{className:"sa-drawer-preview-loading",children:"加载中"}):o?e.jsxs("div",{className:"sa-drawer-preview-error",children:[o,e.jsx("button",{className:"sa-drawer-btn-sm",onClick:()=>void _(a),children:"下载查看"})]}):a.type==="pdf"?e.jsx("iframe",{src:a.url,title:a.filename,className:"sa-drawer-preview-iframe",sandbox:"allow-scripts"}):t!==null?e.jsx("iframe",{srcDoc:t,title:a.filename,className:"sa-drawer-preview-iframe",sandbox:"allow-same-origin"}):null}function ue({artifacts:a,initialArtifact:t,onClose:r}){const[s,n]=d.useState(t?{mode:"preview",artifact:t}:{mode:"list"}),[o,i]=d.useState(!1),l=d.useRef(null),[c,p]=d.useState(340),f=d.useRef(null);d.useEffect(()=>{t&&(l.current&&(clearTimeout(l.current),l.current=null,i(!1)),n({mode:"preview",artifact:t}))},[t]);const u=d.useCallback(()=>{i(!0),l.current=setTimeout(()=>{l.current=null,r()},240)},[r]);d.useEffect(()=>()=>{var h;l.current&&clearTimeout(l.current),(h=f.current)==null||h.call(f)},[]);const m=d.useCallback(h=>{n({mode:"preview",artifact:h})},[]),v=d.useCallback(()=>{n({mode:"list"})},[]);d.useEffect(()=>{const h=b=>{b.key==="Escape"&&u()};return document.addEventListener("keydown",h),()=>document.removeEventListener("keydown",h)},[u]);const y=d.useCallback(h=>{h.preventDefault();const b=h.clientX,x=c,g=T=>{const S=b-T.clientX;p(Math.max(240,Math.min(window.innerWidth*.7,x+S)))},N=()=>{document.removeEventListener("mousemove",g),document.removeEventListener("mouseup",N),document.body.style.cursor="",document.body.style.userSelect="",f.current=null};document.body.style.cursor="col-resize",document.body.style.userSelect="none",document.addEventListener("mousemove",g),document.addEventListener("mouseup",N),f.current=N},[c]);return e.jsxs("div",{className:`sa-artifact-drawer ${o?"sa-drawer-closing":""}`,style:{width:c},children:[e.jsx("div",{className:"sa-drawer-resize-handle",onMouseDown:y}),e.jsxs("div",{className:"sa-artifact-drawer-header",children:[s.mode==="preview"?e.jsxs(e.Fragment,{children:[e.jsx("button",{className:"sa-drawer-btn",onClick:v,title:"返回列表","aria-label":"返回列表",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M15 18l-6-6 6-6"})})}),e.jsx("span",{className:"sa-artifact-drawer-title",children:s.artifact.filename})]}):e.jsxs(e.Fragment,{children:[e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",style:{flexShrink:0},children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{className:"sa-artifact-drawer-title",children:"会话产物"})]}),e.jsx("div",{style:{flex:1}}),s.mode==="preview"&&e.jsx("button",{className:"sa-drawer-btn",onClick:()=>void _(s.artifact),title:"下载","aria-label":"下载",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})}),e.jsx("button",{className:"sa-drawer-btn",onClick:u,title:"关闭","aria-label":"关闭",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M18 6L6 18M6 6l12 12"})})})]}),e.jsx("div",{className:"sa-artifact-drawer-body",children:s.mode==="list"?a.length===0?e.jsx("div",{className:"sa-drawer-empty",children:"暂无产物"}):e.jsx("ul",{className:"sa-drawer-file-list",role:"list",children:a.map((h,b)=>e.jsxs("li",{className:"sa-drawer-file-card",onClick:()=>m(h),onKeyDown:x=>{(x.key==="Enter"||x.key===" ")&&(x.preventDefault(),m(h))},tabIndex:0,role:"button","aria-label":`预览 ${h.filename}`,children:[e.jsx("div",{className:"sa-drawer-file-icon",children:e.jsx("span",{className:"sa-artifact-chip-type",children:G(h.type)})}),e.jsxs("div",{className:"sa-drawer-file-info",children:[e.jsx("span",{className:"sa-drawer-file-name",children:h.filename}),e.jsx("span",{className:"sa-drawer-file-size",children:J(h.size)})]}),e.jsx("button",{className:"sa-drawer-btn",title:"下载","aria-label":`下载 ${h.filename}`,onClick:x=>{x.stopPropagation(),_(h)},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})})]},h.id||`artifact-${b}`))}):e.jsx("div",{className:"sa-drawer-view-enter",children:e.jsx(Re,{artifact:s.artifact})})})]})}function Oe({isOpen:a,onClose:t,title:r,slots:s,welcomeMessage:n,suggestedPrompts:o,avatar:i,artifactPreview:l}){const{status:c,sendMessage:p,stop:f,toggleThreadList:u,toggleArtifactDrawer:m,conversations:v,activeConversationId:y,switchConversation:h,newConversation:b,deleteConversation:x,renameConversation:g,isThreadListOpen:N,isArtifactDrawerOpen:T,allArtifacts:S,activePreviewArtifact:A}=B();if(!a)return null;const k=s.Header,I=s.Composer,D=s.ThreadList;return e.jsxs("div",{className:"sa-panel",children:[k?e.jsx(k,{title:r,onClose:t,onToggleThreadList:u}):e.jsx(ae,{title:r,onClose:t,onToggleThreadList:u,artifactCount:l?S.length:0,onToggleArtifactDrawer:l?m:void 0}),D&&N?e.jsx(D,{conversations:v,activeSessionId:y,onSwitch:h,onNew:b,onDelete:x,onRename:g}):e.jsx(te,{}),e.jsxs("div",{className:"sa-panel-content",children:[e.jsx(q,{slots:s,welcomeMessage:n,suggestedPrompts:o,avatar:i,artifactPreview:l}),l&&T&&e.jsx(ue,{artifacts:S,initialArtifact:A,onClose:m})]}),I?e.jsx(I,{status:c,onSend:p,onStop:f}):e.jsx(Y,{})]})}function Be({conversations:a,activeSessionId:t,onSwitch:r,onNew:s,onDelete:n,isCollapsed:o,onToggleCollapse:i}){const[l,c]=d.useState(null),[p,f]=d.useState(null),u=d.useCallback((v,y)=>{y.stopPropagation(),p===v?(n(v),f(null)):(f(v),setTimeout(()=>f(null),3e3))},[p,n]),m=(()=>{const v=Date.now(),y=[],h=[],b=[];for(const g of a){const N=v-new Date(g.updateTime).getTime();N<864e5?y.push(g):N<6048e5?h.push(g):b.push(g)}const x=[];return y.length&&x.push({label:"今天",items:y}),h.length&&x.push({label:"最近 7 天",items:h}),b.length&&x.push({label:"更早",items:b}),x})();return o?e.jsxs("div",{className:"sa-fp-sidebar sa-fp-sidebar-collapsed",children:[e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:i,title:"展开",children:e.jsx(j,{name:"panel-left",size:18})}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:s,title:"新会话",style:{marginTop:4},children:e.jsx(j,{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:s,children:[e.jsx(j,{name:"plus",size:15})," ",e.jsx("span",{children:"新会话"})]}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:i,title:"收起侧边栏",children:e.jsx(j,{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:"开始你的第一个对话"}),m.map(v=>e.jsxs("div",{children:[e.jsx("div",{className:"sa-fp-sidebar-group",children:v.label}),v.items.map(y=>{const h=y.sessionId===t,b=l===y.sessionId,x=p===y.sessionId;return e.jsxs("div",{className:`sa-fp-sidebar-item ${h?"active":""}`,onClick:()=>r(y.sessionId),onMouseEnter:()=>c(y.sessionId),onMouseLeave:()=>c(null),children:[e.jsx("span",{className:"sa-fp-sidebar-item-title",children:y.title||"新会话"}),b&&e.jsx("button",{className:`sa-fp-sidebar-item-action ${x?"danger":""}`,onClick:g=>u(y.sessionId,g),title:x?"确认删除":"删除",children:e.jsx(j,{name:x?"x":"trash2",size:14})})]},y.sessionId)})]},v.label))]})]})}function xe({title:a,slots:t,welcomeMessage:r,suggestedPrompts:s,sidebarDefaultOpen:n=!0,onClose:o,avatar:i,artifactPreview:l}){const{status:c,sendMessage:p,stop:f,conversations:u,activeConversationId:m,switchConversation:v,newConversation:y,deleteConversation:h,renameConversation:b,toggleArtifactDrawer:x,isArtifactDrawerOpen:g,allArtifacts:N,activePreviewArtifact:T}=B(),[S,A]=d.useState(n),k=d.useCallback(()=>A(z=>!z),[]),I=t.ThreadList,D=t.Composer;return e.jsxs("div",{className:"sa-fullpage",children:[I?e.jsx("div",{className:`sa-fp-sidebar-wrapper ${S?"":"sa-fp-sidebar-wrapper-collapsed"}`,children:e.jsx(I,{conversations:u,activeSessionId:m,onSwitch:v,onNew:y,onDelete:h,onRename:b})}):e.jsx(Be,{conversations:u,activeSessionId:m,onSwitch:v,onNew:y,onDelete:h,onRename:b,isCollapsed:!S,onToggleCollapse:k}),e.jsxs("div",{className:"sa-fp-main",children:[e.jsxs("div",{className:"sa-fp-header",children:[!S&&!I&&e.jsx("button",{className:"sa-fp-header-btn",onClick:k,title:"展开侧边栏",children:e.jsx(j,{name:"panel-left",size:18})}),e.jsx("div",{className:"sa-fp-header-title",children:a}),e.jsxs("div",{className:"sa-fp-header-actions",children:[l&&N.length>0&&e.jsxs("button",{className:"sa-fp-header-btn sa-header-btn-artifact",onClick:x,title:"会话产物",children:[e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{children:"产物"})]}),o&&e.jsx("button",{className:"sa-fp-header-btn",onClick:o,title:"关闭",children:e.jsx(j,{name:"x",size:18})})]})]}),e.jsxs("div",{className:"sa-fp-content-row",children:[e.jsx(q,{slots:t,welcomeMessage:r,suggestedPrompts:s,avatar:i,artifactPreview:l}),l&&g&&e.jsx(ue,{artifacts:N,initialArtifact:T,onClose:x})]}),D?e.jsx(D,{status:c,onSend:p,onStop:f}):e.jsx(Y,{})]})]})}function me(a){const{sdk:t,mode:r="floating",slots:s={},hooks:n={},welcomeMessage:o,suggestedPrompts:i,title:l="AI Assistant",initialOpen:c=!1,onOpenChange:p,sidebarDefaultOpen:f,avatar:u,artifactPreview:m}=a,[v,y]=d.useState(c);d.useEffect(()=>{y(c)},[c]);const h=d.useCallback(()=>{var N,T;const g=!v;y(g),p==null||p(g),g?(N=n.onOpen)==null||N.call(n):(T=n.onClose)==null||T.call(n)},[v,n,p]),b=d.useCallback(()=>{var g;y(!1),p==null||p(!1),(g=n.onClose)==null||g.call(n)},[n,p]),x=s.Trigger??ee;return r==="fullpage"?v?e.jsx(W,{sdk:t,hooks:n,children:e.jsx(xe,{title:l,slots:s,welcomeMessage:o,suggestedPrompts:i,sidebarDefaultOpen:f,onClose:b,avatar:u,artifactPreview:m})}):null:e.jsxs(W,{sdk:t,hooks:n,children:[e.jsx(x,{isOpen:v,onClick:h}),e.jsx(Oe,{isOpen:v,onClose:b,title:l,slots:s,welcomeMessage:o,suggestedPrompts:i,avatar:u,artifactPreview:m})]})}const he=`
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("react"),ve=require("react-dom/client"),e=require("react/jsx-runtime");function U(a){const{sdk:t,sessionId:s,onSessionCreated:r,onStreamStart:n,onStreamEnd:o,onError:i,onMessageSend:l}=a,[d,p]=c.useState([]),[f,u]=c.useState("ready"),[m,w]=c.useState(null),y=c.useRef(null),g=c.useRef(!1),v=c.useRef(s);v.current=s;const x=c.useCallback(I=>{if(g.current)return;g.current=!0,l==null||l(I);const A={id:`user_${Date.now()}`,role:"user",parts:[{type:"text",content:I}],timestamp:Date.now()},k=`assistant_${Date.now()}`,T={id:k,role:"assistant",parts:[],timestamp:Date.now()};p(S=>[...S,A,T]),u("submitted"),w(null);const D=S=>{let M="",E="",$=[],N=!1;const L=()=>{const b=[...$];if(M){const O=b.findIndex(P=>P.type==="thinking");O>=0?b[O]={type:"thinking",content:M}:b.unshift({type:"thinking",content:M})}if(E){const O=b.findLastIndex(P=>P.type==="text");O>=0?b[O]={type:"text",content:E}:b.push({type:"text",content:E})}p(O=>O.map(P=>P.id===k?{...P,parts:b}:P))};y.current=t.chat({sessionId:S,message:I,stream:!0,onMessage:b=>{switch(N||(N=!0,u("streaming")),b.type){case"thinking":M+=b.content;break;case"ai":E+=b.content;break;case"tool_call":$.push({type:"tool_call",toolName:b.toolName,toolCallId:b.toolCallId,args:b.args??"{}"});break;case"tool_result":$.push({type:"tool_result",toolName:b.toolName??"",toolCallId:b.toolCallId,content:b.content,artifacts:b.artifacts,renderMode:b.renderMode,html:b.html}),E&&($.push({type:"text",content:E}),E="");break;case"interrupt":b.interrupt&&$.push({type:"interrupt",interrupt:b.interrupt,resolved:!1});break}L()},onDone:b=>{(E||$.length>0||M)&&L(),b.messageId&&p(O=>O.map(P=>P.id===k?{...P,id:b.messageId}:P)),b.sessionId&&b.sessionId!==v.current&&(r==null||r(b.sessionId)),o==null||o(b.sessionId||S),u("ready"),g.current=!1,y.current=null},onError:b=>{$.push({type:"error",content:b.message}),L(),w(b),u("error"),i==null||i(b),g.current=!1,y.current=null}}),n==null||n(S)};v.current?D(v.current):t.createSession().then(S=>{v.current=S,r==null||r(S),D(S)}).catch(S=>{w(S instanceof Error?S:new Error(String(S))),u("error"),i==null||i(S instanceof Error?S:new Error(String(S))),g.current=!1})},[t,r,n,o,i,l]),h=c.useCallback(()=>{var I;(I=y.current)==null||I.abort(),y.current=null,u("ready"),g.current=!1},[]),C=c.useCallback(()=>{if(g.current)return;const I=d.findLastIndex(k=>k.role==="user");if(I<0)return;const A=d[I].parts.filter(k=>k.type==="text").map(k=>k.content).join("");p(k=>k.slice(0,I)),x(A)},[d,x]),z=c.useCallback((I,A)=>{p(N=>N.map(L=>({...L,parts:L.parts.map(b=>b.type==="interrupt"&&b.interrupt.interruptId===I?{...b,resolved:!0,response:A}:b)})));const k=v.current;if(!k)return;const T=`assistant_${Date.now()}`,D={id:T,role:"assistant",parts:[],timestamp:Date.now()};p(N=>[...N,D]),u("streaming");let S="",M="",E=[];const $=()=>{const N=[...E];if(S){const L=N.findIndex(b=>b.type==="thinking");L>=0?N[L]={type:"thinking",content:S}:N.unshift({type:"thinking",content:S})}if(M){const L=N.findLastIndex(b=>b.type==="text");L>=0?N[L]={type:"text",content:M}:N.push({type:"text",content:M})}p(L=>L.map(b=>b.id===T?{...b,parts:N}:b))};t.respondInterrupt(I,k,A,N=>{switch(N.type){case"thinking":S+=N.content;break;case"ai":M+=N.content;break;case"tool_call":E.push({type:"tool_call",toolName:N.toolName,toolCallId:N.toolCallId,args:N.args??"{}"});break;case"tool_result":E.push({type:"tool_result",toolName:N.toolName??"",toolCallId:N.toolCallId,content:N.content,artifacts:N.artifacts,renderMode:N.renderMode,html:N.html}),M&&(E.push({type:"text",content:M}),M="");break;case"interrupt":N.interrupt&&E.push({type:"interrupt",interrupt:N.interrupt,resolved:!1});break;case"done":N.messageId&&p(L=>L.map(b=>b.id===T?{...b,id:N.messageId}:b)),u("ready");break}$()},N=>{E.push({type:"error",content:N.message}),$(),w(N),u("error"),i==null||i(N)}).catch(N=>{i==null||i(N instanceof Error?N:new Error(String(N))),u("error")})},[t,i]);return{messages:d,status:f,error:m,sendMessage:x,stop:h,regenerate:C,setMessages:p,respondToInterrupt:z}}const _="sa_active_conversation";function K(){try{return localStorage.getItem(_)}catch{return null}}function we(a){try{a?localStorage.setItem(_,a):localStorage.removeItem(_)}catch{}}function X(a){const{sdk:t,onConversationChange:s}=a,[r,n]=c.useState([]),[o,i]=c.useState(()=>K()),[l,d]=c.useState(!1),p=c.useRef(!0);c.useEffect(()=>()=>{p.current=!1},[]);const f=c.useCallback(x=>{i(x),we(x),x&&(s==null||s(x))},[s]),u=c.useCallback(async()=>{d(!0);try{const x=await t.listConversations({page:1,size:100});if(!p.current)return;n(x.items);const h=K();h&&!x.items.find(C=>C.sessionId===h)&&f(null)}catch{}finally{p.current&&d(!1)}},[t,f]),m=c.useCallback(async x=>(f(x),await t.getMessages(x)),[t,f]),w=c.useCallback(()=>{f(null)},[f]),y=c.useCallback(async x=>{await t.deleteConversation(x),n(h=>h.filter(C=>C.sessionId!==x)),o===x&&f(null)},[t,o,f]),g=c.useCallback(async(x,h)=>{await t.renameConversation(x,h),n(C=>C.map(z=>z.sessionId===x?{...z,title:h}:z))},[t]),{enabled:v=!0}=a;return c.useEffect(()=>{v&&u()},[v,u]),{conversations:r,activeConversationId:o,loading:l,loadConversations:u,switchConversation:m,newConversation:w,deleteConversation:y,renameConversation:g,setActiveConversationId:f}}function J(a){return!a&&a!==0?"":a<1024?`${a} B`:a<1024*1024?`${(a/1024).toFixed(1)} KB`:`${(a/1024/1024).toFixed(1)} MB`}function G(a){return a==="pdf"?"PDF":a==="docx"?"DOC":a==="html"?"HTM":a.slice(0,3).toUpperCase()||"FILE"}function Z(a){if(!a)return[];const t=[];for(const s of a)s.type==="file"&&s.data&&t.push({id:s.data.id??"",type:s.data.fileType??s.data.type??"",filename:s.data.name??s.data.filename??"",url:s.data.url??"",mime:s.data.mime,size:s.data.size,summary:s.data.summary});return t}async function H(a){try{const t=await fetch(a.url);if(!t.ok)throw new Error(`HTTP ${t.status}`);const s=await t.blob(),r=URL.createObjectURL(s),n=document.createElement("a");n.href=r,n.download=a.filename,document.body.appendChild(n),n.click(),n.remove(),setTimeout(()=>URL.revokeObjectURL(r),4e3)}catch{const t=document.createElement("a");t.href=a.url,t.download=a.filename,t.target="_blank",document.body.appendChild(t),t.click(),t.remove()}}const Q=c.createContext(null);function B(){const a=c.useContext(Q);if(!a)throw new Error("useChatContext must be used within ChatProvider");return a}function W({sdk:a,hooks:t,children:s}){const[r,n]=c.useState(!1),[o,i]=c.useState(!1),[l,d]=c.useState(null),[p,f]=c.useState(!1);c.useEffect(()=>{a.getToken().then(()=>{f(!0)}).catch(()=>{f(!0)})},[a]);const u=X({sdk:a,onConversationChange:t==null?void 0:t.onConversationChange,enabled:p}),m=U({sdk:a,sessionId:u.activeConversationId,onSessionCreated:k=>{u.setActiveConversationId(k),u.loadConversations()},onStreamStart:t==null?void 0:t.onStreamStart,onStreamEnd:k=>{var T;(T=t==null?void 0:t.onStreamEnd)==null||T.call(t,k),u.loadConversations()},onError:t==null?void 0:t.onError,onMessageSend:t==null?void 0:t.onMessageSend}),w=c.useCallback(async k=>{const T=await u.switchConversation(k);m.setMessages(T),n(!1)},[u,m]),y=c.useCallback(()=>{u.newConversation(),m.setMessages([]),n(!1)},[u,m]),g=c.useCallback(async k=>{await u.deleteConversation(k),u.activeConversationId===k&&m.setMessages([])},[u,m]);c.useEffect(()=>{u.activeConversationId&&m.messages.length===0&&a.getMessages(u.activeConversationId).then(k=>{m.setMessages(k)}).catch(()=>{})},[]);const v=c.useCallback(()=>{n(k=>!k)},[]),x=c.useCallback(()=>{i(k=>!k),d(k=>null)},[]),h=c.useCallback(k=>{d(k),i(!0)},[]),C=c.useMemo(()=>{const k=new Set,T=[];for(const D of m.messages)for(const S of D.parts)if(S.type==="tool_result"&&S.artifacts)for(const M of Z(S.artifacts)){const E=M.id||`${M.url}:${M.filename}`;k.has(E)||(k.add(E),T.push(M))}return T},[m.messages]),z=c.useCallback((k,T,D)=>{a.feedback(k,T,D).catch(()=>{})},[a]),I=c.useCallback(k=>{a.cancelFeedback(k).catch(()=>{})},[a]),A=c.useMemo(()=>({status:m.status,error:m.error,messages:m.messages,conversations:u.conversations,activeConversationId:u.activeConversationId,isThreadListOpen:r,isArtifactDrawerOpen:o,allArtifacts:C,activePreviewArtifact:l,sendMessage:m.sendMessage,stop:m.stop,regenerate:m.regenerate,switchConversation:w,newConversation:y,deleteConversation:g,renameConversation:u.renameConversation,toggleThreadList:v,toggleArtifactDrawer:x,openArtifactPreview:h,feedback:z,cancelFeedback:I,respondToInterrupt:m.respondToInterrupt}),[m.status,m.error,m.messages,m.sendMessage,m.stop,m.regenerate,m.respondToInterrupt,u.conversations,u.activeConversationId,u.renameConversation,r,o,C,l,w,y,g,v,x,h,z,I]);return e.jsx(Q.Provider,{value:A,children:s})}function j({name:a,size:t=16,color:s="currentColor",className:r,style:n}){return e.jsx("svg",{className:r,style:{width:t,height:t,fill:s,verticalAlign:"middle",...n},"aria-hidden":"true",children:e.jsx("use",{href:`#icon-${a}`})})}function ee({isOpen:a,onClick:t}){return e.jsx("button",{className:`sa-trigger ${a?"sa-trigger-open":""}`,onClick:t,children:a?e.jsx(j,{name:"x",size:24,color:"#fff"}):e.jsx(j,{name:"message-square",size:24,color:"#fff"})})}function ae({title:a,onClose:t,onToggleThreadList:s,showClose:r=!0,artifactCount:n=0,onToggleArtifactDrawer:o}){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:[n>0&&o&&e.jsxs("button",{className:"sa-header-btn sa-header-btn-artifact",onClick:o,title:"会话产物",children:[e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{children:"产物"})]}),e.jsx("button",{className:"sa-header-btn",onClick:s,title:"会话列表",children:e.jsx(j,{name:"menu",size:16})}),r&&e.jsx("button",{className:"sa-header-btn",onClick:t,title:"关闭",children:e.jsx(j,{name:"x",size:16})})]})]})}function ke(a){const t=new Date,s=new Date(t.getFullYear(),t.getMonth(),t.getDate()).getTime(),r=s-6*864e5,n=[],o=[],i=[];for(const d of a){const p=new Date(d.updateTime).getTime();p>=s?n.push(d):p>=r?o.push(d):i.push(d)}const l=[];return n.length&&l.push({label:"今天",items:n}),o.length&&l.push({label:"最近 7 天",items:o}),i.length&&l.push({label:"更早",items:i}),l}function te(){const{conversations:a,activeConversationId:t,switchConversation:s,newConversation:r,deleteConversation:n,renameConversation:o,isThreadListOpen:i,toggleThreadList:l}=B(),[d,p]=c.useState(null),[f,u]=c.useState(""),m=c.useCallback((g,v)=>{p(g),u(v??"")},[]),w=c.useCallback(g=>{f.trim()&&o(g,f.trim()),p(null)},[f,o]),y=c.useMemo(()=>ke(a),[a]);return i?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"sa-thread-list-overlay",onClick:l}),e.jsxs("div",{className:"sa-thread-list",children:[e.jsxs("div",{className:"sa-thread-list-header",children:[e.jsx("span",{className:"sa-thread-list-title",children:"会话记录"}),e.jsxs("button",{className:"sa-thread-list-new",onClick:r,children:[e.jsx(j,{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:"暂无会话"}),y.map(g=>e.jsxs("div",{className:"sa-thread-group",children:[e.jsx("div",{className:"sa-thread-group-label",children:g.label}),g.items.map(v=>e.jsxs("div",{className:`sa-thread-item ${v.sessionId===t?"active":""}`,onClick:()=>s(v.sessionId),children:[d===v.sessionId?e.jsx("input",{className:"sa-thread-item-edit",value:f,onChange:x=>u(x.target.value),onBlur:()=>w(v.sessionId),onKeyDown:x=>{x.key==="Enter"&&w(v.sessionId),x.key==="Escape"&&p(null)},onClick:x=>x.stopPropagation(),autoFocus:!0}):e.jsx("span",{className:"sa-thread-item-title",onDoubleClick:x=>{x.stopPropagation(),m(v.sessionId,v.title)},children:v.title||"新会话"}),e.jsx("button",{className:"sa-thread-item-delete",onClick:x=>{x.stopPropagation(),n(v.sessionId)},title:"删除",children:e.jsx(j,{name:"trash2",size:14})})]},v.sessionId))]},g.label))]})]})]}):null}function se(a){const t=c.useRef(null),s=c.useRef(!1),r=c.useRef(0),n=c.useCallback(()=>{const i=t.current;if(!i)return;const{scrollTop:l,scrollHeight:d,clientHeight:p}=i,f=d-l-p<40;l<r.current&&!f&&(s.current=!0),f&&(s.current=!1),r.current=l},[]);c.useEffect(()=>{const i=t.current;if(i)return i.addEventListener("scroll",n,{passive:!0}),()=>i.removeEventListener("scroll",n)},[n]),c.useEffect(()=>{if(s.current)return;const i=t.current;i&&requestAnimationFrame(()=>{i.scrollTop=i.scrollHeight})},a);const o=c.useCallback(()=>{s.current=!1;const i=t.current;i&&(i.scrollTop=i.scrollHeight)},[]);return{containerRef:t,scrollToBottom:o}}function R(a){return a.value++}function F(a,t){const s=[],r=[],n=()=>{r.length>0&&(s.push(r.join("")),r.length=0)};let o=0;for(;o<a.length;){const i=a[o];if(i==="`"){const l=a.indexOf("`",o+1);if(l!==-1){n(),s.push(e.jsx("code",{className:"sa-inline-code",children:a.slice(o+1,l)},R(t))),o=l+1;continue}}else if(i==="*"&&a[o+1]==="*"){const l=a.indexOf("**",o+2);if(l!==-1){n(),s.push(e.jsx("strong",{children:F(a.slice(o+2,l),t)},R(t))),o=l+2;continue}}else if(i==="*"){const l=a.indexOf("*",o+1);if(l!==-1){n(),s.push(e.jsx("em",{children:F(a.slice(o+1,l),t)},R(t))),o=l+1;continue}}else if(i==="["){const l=a.indexOf("]",o+1);if(l!==-1&&a[l+1]==="("){const d=a.indexOf(")",l+2);if(d!==-1){const p=a.slice(o+1,l),f=a.slice(l+2,d);n(),s.push(e.jsx("a",{href:f,target:"_blank",rel:"noreferrer",children:F(p,t)},R(t))),o=d+1;continue}}}r.push(i),o++}return n(),s}function ye(a,t){const s=[],r=a.split(`
|
|
2
|
+
`);let n=0;const o=d=>d.trim()==="",i=d=>/^#{1,6}\s+\S/.test(d.trim()),l=d=>/^(?:[-*+]|\d+[.)])\s+\S/.test(d.trim());for(;n<r.length;){const d=r[n],p=d.trim();if(o(d)){n++;continue}if(i(d)){const u=p.match(/^(#{1,6})\s+(.*)$/);if(u){const w=`h${Math.min(u[1].length,6)}`;s.push(c.createElement(w,{key:R(t)},F(u[2],t)))}n++;continue}if(l(d)){const u=/^\d+[.)]/.test(p),m=[];for(;n<r.length&&l(r[n]);){const w=r[n].trim().match(/^(?:[-*+]|\d+[.)])\s+(.*)$/);w&&m.push(e.jsx("li",{children:F(w[1],t)},R(t))),n++}s.push(u?e.jsx("ol",{className:"sa-list",children:m},R(t)):e.jsx("ul",{className:"sa-list",children:m},R(t)));continue}const f=[];for(;n<r.length&&!o(r[n])&&!i(r[n])&&!l(r[n]);)f.push(r[n]),n++;s.push(e.jsx("p",{className:"sa-text-paragraph",children:F(f.join(" "),t)},R(t)))}return s}function je(a){const t={value:0},s=[],r=a.split("```");for(let n=0;n<r.length;n++)if(n%2===0)s.push(...ye(r[n],t));else if(r[n].trim()!==""){let o=r[n].replace(/^\r?\n/,"");o=o.replace(/^[a-zA-Z0-9_+#.-]+\r?\n/,""),o=o.replace(/\r?\n$/,""),s.push(e.jsx("pre",{className:"sa-code-block",children:e.jsx("code",{children:o})},R(t)))}return s}function re({content:a}){return e.jsx("div",{className:"sa-text-part",children:je(a)})}function ne({content:a}){const[t,s]=c.useState(!1);return e.jsxs("div",{className:"sa-thinking-part",onClick:()=>s(!t),children:[e.jsxs("div",{className:"sa-thinking-header",children:[e.jsx("span",{className:"sa-thinking-icon",children:e.jsx(j,{name:"brain",size:14})}),e.jsx("span",{className:"sa-thinking-label",children:"思考过程"}),e.jsx("span",{className:`sa-thinking-arrow ${t?"expanded":""}`,children:e.jsx(j,{name:"chevron-right",size:12})})]}),t&&e.jsx("div",{className:"sa-thinking-content",children:a})]})}function ie({toolName:a,toolCallId:t,args:s}){const[r,n]=c.useState(!1);let o=s;try{o=JSON.stringify(JSON.parse(s),null,2)}catch{}return e.jsxs("div",{className:"sa-tool-call-part",children:[e.jsxs("div",{className:"sa-tool-call-header",onClick:()=>n(!r),children:[e.jsx("span",{className:"sa-tool-icon",children:e.jsx(j,{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(j,{name:"chevron-right",size:12})})]}),r&&e.jsx("pre",{className:"sa-tool-args",children:o})]})}function Ce({html:a}){const[t,s]=c.useState(null);return c.useEffect(()=>{let r=!1;return Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs")).then(n=>{r||s(n.default.sanitize(a,{FORCE_BODY:!0}))}),()=>{r=!0}},[a]),t===null?null:e.jsx("div",{className:"sa-tool-result-html",children:e.jsx("iframe",{srcDoc:t,className:"sa-tool-result-html-iframe",sandbox:"",title:"HTML 产物"})})}function oe({toolName:a,content:t,artifacts:s,renderMode:r,html:n,artifactPreview:o}){const{openArtifactPreview:i}=B(),l=Z(s);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(j,{name:"circle-check",size:14,color:"#10b981"})}),e.jsx("span",{className:"sa-tool-result-label",children:a||"工具返回"})]}),r==="html"&&n?e.jsx(Ce,{html:n}):t?e.jsx("div",{className:"sa-tool-result-content",children:t}):null,l.length>0&&e.jsx("ul",{className:"sa-artifact-file-list",role:"list",children:l.map((d,p)=>e.jsxs("li",{className:"sa-artifact-file-card",onClick:o?()=>i(d):void 0,onKeyDown:o?f=>{(f.key==="Enter"||f.key===" ")&&(f.preventDefault(),i(d))}:void 0,tabIndex:o?0:void 0,role:o?"button":void 0,"aria-label":o?`预览 ${d.filename}`:void 0,style:o?{cursor:"pointer"}:void 0,children:[e.jsx("div",{className:"sa-artifact-file-icon",children:e.jsx("span",{className:"sa-artifact-chip-type",children:G(d.type)})}),e.jsxs("div",{className:"sa-artifact-file-info",children:[e.jsx("span",{className:"sa-artifact-file-name",children:d.filename}),d.size!=null&&e.jsx("span",{className:"sa-artifact-file-size",children:J(d.size)})]}),o&&e.jsx("button",{className:"sa-artifact-file-download",title:"下载","aria-label":`下载 ${d.filename}`,onClick:f=>{f.stopPropagation(),H(d)},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})})]},d.id||`art-${p}`))})]})}function le({content:a,onRetry:t}){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(j,{name:"triangle-alert",size:14})}),e.jsx("span",{children:a})]}),t&&e.jsx("button",{className:"sa-error-retry",onClick:t,children:"重试"})]})}function Ne({interrupt:a,onRespond:t}){var o,i;const s=a.options,r=(typeof(s==null?void 0:s[0])=="string"?s[0]:(o=s==null?void 0:s[0])==null?void 0:o.label)||"确认",n=(typeof(s==null?void 0:s[1])=="string"?s[1]:(i=s==null?void 0:s[1])==null?void 0:i.label)||"取消";return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"circle-alert",size:16,color:"#f59e0b"}),e.jsx("span",{children:a.content})]}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",onClick:()=>t({action:"confirm"}),children:[e.jsx(j,{name:"check",size:14})," ",r]}),e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-secondary",onClick:()=>t({action:"cancel"}),children:[e.jsx(j,{name:"x",size:14})," ",n]})]})]})}function ze({interrupt:a,onRespond:t}){var l,d,p,f;const s=a.options,r=(typeof(s==null?void 0:s[0])=="string"?s[0]:(l=s==null?void 0:s[0])==null?void 0:l.label)||"批准",n=(typeof(s==null?void 0:s[1])=="string"?s[1]:(d=s==null?void 0:s[1])==null?void 0:d.label)||"拒绝",o=(typeof(s==null?void 0:s[2])=="string"?s[2]:(p=s==null?void 0:s[2])==null?void 0:p.label)||"跳过",i=((f=a.metadata)==null?void 0:f.showSkip)!==!1;return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"circle-alert",size:16,color:"#f59e0b"}),e.jsx("span",{children:a.content})]}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",onClick:()=>t({action:"approve"}),children:[e.jsx(j,{name:"check",size:14})," ",r]}),e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-danger",onClick:()=>t({action:"reject"}),children:[e.jsx(j,{name:"x",size:14})," ",n]}),i&&e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-secondary",onClick:()=>t({action:"skip"}),children:[e.jsx(j,{name:"chevron-right",size:14})," ",o]})]})]})}function Se(a){return typeof a=="string"?{value:a,label:a}:a}function Ie({interrupt:a,onRespond:t}){const s=(a.options??[]).map(Se);return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"list",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-options",children:s.map(r=>e.jsxs("button",{className:`sa-interrupt-option ${r.danger?"sa-interrupt-option-danger":""}`,onClick:()=>t({action:"select",value:r.value}),children:[r.icon&&e.jsx(j,{name:r.icon,size:14}),e.jsx("span",{children:r.label}),r.description&&e.jsx("span",{className:"sa-interrupt-option-desc",children:r.description})]},r.value))})]})}function Te(a){return typeof a=="string"?{value:a,label:a}:a}function Me({interrupt:a,onRespond:t}){const s=(a.options??[]).map(Te),[r,n]=c.useState(new Set),o=i=>{n(l=>{const d=new Set(l);return d.has(i)?d.delete(i):d.add(i),d})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"list-checks",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-options",children:s.map(i=>e.jsxs("button",{className:`sa-interrupt-option ${r.has(i.value)?"sa-interrupt-option-selected":""}`,onClick:()=>o(i.value),children:[e.jsx(j,{name:r.has(i.value)?"square-check":"square",size:14}),e.jsx("span",{children:i.label})]},i.value))}),e.jsx("div",{className:"sa-interrupt-actions",children:e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:r.size===0,onClick:()=>t({action:"submit",value:Array.from(r)}),children:[e.jsx(j,{name:"check",size:14})," 确认 (",r.size,")"]})})]})}function Ee({interrupt:a,onRespond:t}){const[s,r]=c.useState(""),n=a.inputType??"text",o=n==="textarea",i=()=>{a.required&&!s.trim()||t({action:"submit",value:s.trim()})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"text-cursor-input",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-input-area",children:o?e.jsx("textarea",{className:"sa-interrupt-textarea",value:s,onChange:l=>r(l.target.value),placeholder:a.placeholder,maxLength:a.maxLength,rows:3}):e.jsx("input",{className:"sa-interrupt-input",type:n,value:s,onChange:l=>r(l.target.value),placeholder:a.placeholder,maxLength:a.maxLength,onKeyDown:l=>l.key==="Enter"&&i()})}),e.jsx("div",{className:"sa-interrupt-actions",children:e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:a.required?!s.trim():!1,onClick:i,children:[e.jsx(j,{name:"check",size:14})," 提交"]})})]})}function Le({interrupt:a,onRespond:t}){const s=a.fields??[],[r,n]=c.useState(()=>{const d={};for(const p of s)d[p.name]=p.defaultValue??"";return d}),o=(d,p)=>{n(f=>({...f,[d]:p}))},i=s.every(d=>!d.required||r[d.name]),l=()=>{i&&t({action:"submit",value:r})};return e.jsxs("div",{className:"sa-interrupt-card",children:[e.jsxs("div",{className:"sa-interrupt-content",children:[e.jsx(j,{name:"file-text",size:16,color:"#6366f1"}),e.jsx("span",{children:a.content})]}),e.jsx("div",{className:"sa-interrupt-form",children:s.map(d=>e.jsxs("div",{className:"sa-interrupt-form-field",children:[e.jsxs("label",{className:"sa-interrupt-form-label",children:[d.label,d.required&&e.jsx("span",{className:"sa-interrupt-form-required",children:"*"})]}),De(d,r[d.name],p=>o(d.name,p))]},d.name))}),e.jsxs("div",{className:"sa-interrupt-actions",children:[e.jsxs("button",{className:"sa-interrupt-btn sa-interrupt-btn-primary",disabled:!i,onClick:l,children:[e.jsx(j,{name:"check",size:14})," 提交"]}),e.jsx("button",{className:"sa-interrupt-btn sa-interrupt-btn-secondary",onClick:()=>t({action:"cancel"}),children:"取消"})]})]})}function De(a,t,s){switch(a.type){case"textarea":return e.jsx("textarea",{className:"sa-interrupt-textarea",value:t,onChange:r=>s(r.target.value),placeholder:a.placeholder,rows:3});case"select":return e.jsxs("select",{className:"sa-interrupt-select",value:t,onChange:r=>s(r.target.value),children:[e.jsx("option",{value:"",children:"请选择"}),(a.options??[]).map(r=>e.jsx("option",{value:r,children:r},r))]});default:return e.jsx("input",{className:"sa-interrupt-input",type:a.type,value:t,onChange:r=>s(r.target.value),placeholder:a.placeholder})}}function ce({interrupt:a,resolved:t,response:s,onRespond:r}){if(t)return e.jsxs("div",{className:"sa-interrupt-resolved",children:[e.jsx(j,{name:"circle-check",size:14,color:"#10b981"}),e.jsx("span",{children:Ae(a.interruptType,s)})]});switch(a.interruptType){case"confirm":return e.jsx(Ne,{interrupt:a,onRespond:r});case"approve":return e.jsx(ze,{interrupt:a,onRespond:r});case"select":return e.jsx(Ie,{interrupt:a,onRespond:r});case"multiSelect":return e.jsx(Me,{interrupt:a,onRespond:r});case"input":return e.jsx(Ee,{interrupt:a,onRespond:r});case"form":return e.jsx(Le,{interrupt:a,onRespond:r});default:return e.jsxs("div",{className:"sa-interrupt-unknown",children:["未知的交互类型: ",a.interruptType]})}}function Ae(a,t){if(!t)return"已完成";switch(t.action){case"confirm":return"已确认";case"cancel":return"已取消";case"select":return`已选择: ${t.value}`;case"submit":return"已提交";case"approve":return"已批准";case"reject":return"已拒绝";case"skip":return"已跳过";default:return"已完成"}}const Pe=[{value:1,label:"事实错误"},{value:2,label:"逻辑问题"},{value:3,label:"不相关"},{value:4,label:"信息过时"},{value:5,label:"冗长啰嗦"},{value:6,label:"难以理解"}];function de({message:a,onCopy:t,onRegenerate:s}){const{status:r,feedback:n,cancelFeedback:o}=B(),[i,l]=c.useState(null),[d,p]=c.useState(!1),[f,u]=c.useState(!1),m=c.useRef(null);if(r!=="ready"&&r!=="error")return null;const w=c.useCallback(()=>{t(),p(!0),setTimeout(()=>p(!1),2e3)},[t]),y=c.useCallback(()=>{i==="like"?(l(null),o(a.id)):(l("like"),u(!1),n(a.id,"like"))},[i,a.id,n,o]),g=c.useCallback(()=>{i==="dislike"?(l(null),u(!1),o(a.id)):u(!0)},[i,a.id,o]),v=c.useCallback((h,C)=>{l("dislike"),u(!1),n(a.id,"dislike",{reason:h,remark:(C==null?void 0:C.trim())||void 0})},[a.id,n]),x=c.useCallback(()=>{u(!1)},[]);return c.useEffect(()=>{if(!f)return;const h=z=>{m.current&&!m.current.contains(z.target)&&u(!1)},C=z=>{z.key==="Escape"&&u(!1)};return document.addEventListener("mousedown",h),document.addEventListener("keydown",C),()=>{document.removeEventListener("mousedown",h),document.removeEventListener("keydown",C)}},[f]),e.jsxs("div",{className:"sa-action-bar",children:[e.jsxs("button",{className:"sa-action-btn",onClick:w,title:"复制",children:[e.jsx(j,{name:d?"circle-check":"copy",size:14})," ",d?"已复制":"复制"]}),e.jsxs("button",{className:"sa-action-btn",onClick:s,title:"重新生成",children:[e.jsx(j,{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:y,title:"有用","aria-label":"点赞",children:e.jsx(j,{name:"thumbs-up",size:14})}),e.jsx("button",{className:`sa-action-btn ${i==="dislike"?"sa-action-btn-active":""}`,onClick:g,title:"无用","aria-label":"踩","aria-expanded":f,"aria-haspopup":"dialog",children:e.jsx(j,{name:"thumbs-down",size:14})}),f&&e.jsx("div",{ref:m,children:e.jsx($e,{onSubmit:v,onCancel:x})})]})}function $e({onSubmit:a,onCancel:t}){const[s,r]=c.useState(void 0),[n,o]=c.useState("");return e.jsxs("div",{className:"sa-dislike-panel",children:[e.jsx("div",{className:"sa-dislike-panel-title",children:"请选择不满意的原因(可选)"}),e.jsx("div",{className:"sa-dislike-reasons",children:Pe.map(i=>e.jsx("button",{className:`sa-dislike-reason-btn ${s===i.value?"active":""}`,onClick:()=>r(s===i.value?void 0:i.value),children:i.label},i.value))}),e.jsx("textarea",{className:"sa-dislike-remark",placeholder:"补充描述(选填,≤500 字)",maxLength:500,value:n,onChange:i=>o(i.target.value),rows:2}),e.jsxs("div",{className:"sa-dislike-actions",children:[e.jsx("button",{className:"sa-dislike-submit",onClick:()=>a(s,n||void 0),children:"提交"}),e.jsx("button",{className:"sa-dislike-cancel",onClick:t,children:"取消"})]})]})}function V({src:a,role:t}){const s=t==="assistant"?"A":"U";return a?a.startsWith("http")||a.startsWith("/")||a.startsWith("data:")?e.jsx("div",{className:`sa-avatar sa-avatar-${t}`,children:e.jsx("img",{src:a,alt:t,style:{width:"100%",height:"100%",borderRadius:"inherit",objectFit:"cover"}})}):e.jsx("div",{className:`sa-avatar sa-avatar-${t}`,children:a}):e.jsx("div",{className:`sa-avatar sa-avatar-${t}`,children:s})}function pe({message:a,isStreaming:t,isLast:s,slots:r,avatar:n,artifactPreview:o}){const{regenerate:i,feedback:l,respondToInterrupt:d}=B(),p=c.useCallback(()=>{var C;const h=a.parts.filter(z=>z.type==="text").map(z=>z.content).join(`
|
|
3
|
+
`);(C=navigator.clipboard)==null||C.writeText(h).catch(()=>{})},[a]),f=a.role==="user",u=r.Message;if(u)return e.jsx(u,{message:a,isStreaming:t,isLast:s,avatar:n,slots:r,onCopy:p,onRegenerate:i,onFeedback:l});const m=r.TextPart??re,w=r.ThinkingPart??ne,y=r.ToolCallPart??ie,g=r.ToolResultPart??oe,v=r.ErrorPart??le,x=r.ActionBar??de;return e.jsxs("div",{className:`sa-message ${f?"sa-message-user":"sa-message-assistant"}`,children:[!f&&e.jsx(V,{src:n==null?void 0:n.assistant,role:"assistant"}),e.jsxs("div",{className:"sa-message-body",children:[a.parts.map((h,C)=>{switch(h.type){case"text":return e.jsx(m,{content:h.content},C);case"thinking":return e.jsx(w,{content:h.content},C);case"tool_call":return e.jsx(y,{toolName:h.toolName,toolCallId:h.toolCallId,args:h.args},C);case"tool_result":return e.jsx(g,{toolName:h.toolName,toolCallId:h.toolCallId,content:h.content,artifacts:h.artifacts,renderMode:h.renderMode,html:h.html,artifactPreview:o},C);case"error":return e.jsx(v,{content:h.content,onRetry:i},C);case"interrupt":return e.jsx(ce,{interrupt:h.interrupt,resolved:h.resolved??!1,response:h.response,onRespond:z=>d(h.interrupt.interruptId,z)},C);default:return null}}),t&&s&&!f&&e.jsx("span",{className:"sa-streaming-cursor",children:"|"}),!f&&!t&&a.parts.length>0&&e.jsx(x,{message:a,onCopy:p,onRegenerate:i,onFeedback:l})]}),f&&e.jsx(V,{src:n==null?void 0:n.user,role:"user"})]})}function fe({message:a,suggestedPrompts:t,onPromptClick:s}){return e.jsxs("div",{className:"sa-welcome",children:[e.jsx("div",{className:"sa-welcome-icon",children:e.jsx(j,{name:"sparkles",size:40})}),e.jsx("p",{className:"sa-welcome-message",children:a||"你好!有什么可以帮你的?"}),t&&t.length>0&&e.jsx("div",{className:"sa-welcome-prompts",children:t.map((r,n)=>e.jsx("button",{className:"sa-welcome-prompt",onClick:()=>s(r),children:r},n))})]})}function q({slots:a,welcomeMessage:t,suggestedPrompts:s,avatar:r,artifactPreview:n}){const{messages:o,status:i,sendMessage:l}=B(),{containerRef:d}=se([o]),p=i==="streaming",f=a.WelcomeScreen??fe;return o.length===0?e.jsx("div",{className:"sa-thread",ref:d,children:e.jsx(f,{message:t,suggestedPrompts:s,onPromptClick:l})}):e.jsx("div",{className:"sa-thread",ref:d,children:o.map((u,m)=>e.jsx(pe,{message:u,isStreaming:p,isLast:m===o.length-1,slots:a,avatar:r,artifactPreview:n},u.id))})}function Y(){const{status:a,sendMessage:t,stop:s}=B(),[r,n]=c.useState(""),o=c.useRef(null),i=a==="streaming"||a==="submitted",l=c.useCallback(()=>{const f=r.trim();!f||i||(t(f),n(""),o.current&&(o.current.style.height="auto"))},[r,i,t]),d=c.useCallback(f=>{f.key==="Enter"&&!f.shiftKey&&(f.preventDefault(),l())},[l]),p=c.useCallback(f=>{n(f.target.value);const u=f.target;u.style.height="auto",u.style.height=Math.min(u.scrollHeight,120)+"px"},[]);return e.jsxs("div",{className:"sa-composer",children:[e.jsx("textarea",{ref:o,className:"sa-composer-input",value:r,onChange:p,onKeyDown:d,placeholder:"输入消息...",rows:1,disabled:a==="submitted"}),i?e.jsx("button",{className:"sa-composer-stop",onClick:s,title:"停止",children:e.jsx(j,{name:"circle-stop",size:16,color:"#fff"})}):e.jsx("button",{className:"sa-composer-send",onClick:l,disabled:!r.trim(),title:"发送",children:e.jsx(j,{name:"arrow-up",size:16,color:"#fff"})})]})}function Re({artifact:a}){const[t,s]=c.useState(null),[r,n]=c.useState(!0),[o,i]=c.useState(null);return c.useEffect(()=>{let l=!1;n(!0),i(null),s(null);async function d(){try{if(a.type==="pdf"){l||n(!1);return}if(a.type==="html"){const p=await fetch(a.url);if(!p.ok)throw new Error(`HTTP ${p.status}`);const f=await p.text(),u=(await Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs"))).default;l||(s(u.sanitize(f,{FORCE_BODY:!0})),n(!1));return}if(a.type==="docx"){const p=await fetch(a.url);if(!p.ok)throw new Error(`HTTP ${p.status}`);const f=await p.arrayBuffer(),m=await(await Promise.resolve().then(()=>require("./mammoth.browser-DiAPPO3x.cjs")).then(y=>y.mammoth_browser)).convertToHtml({arrayBuffer:f}),w=(await Promise.resolve().then(()=>require("./purify.es-Dsdnkgrg.cjs"))).default;l||(s(w.sanitize(m.value,{FORCE_BODY:!0})),n(!1));return}l||(n(!1),i(`不支持预览「${a.type}」类型`))}catch(p){l||(n(!1),i(p instanceof Error?p.message:"加载失败"))}}return d(),()=>{l=!0}},[a]),r?e.jsx("div",{className:"sa-drawer-preview-loading",children:"加载中"}):o?e.jsxs("div",{className:"sa-drawer-preview-error",children:[o,e.jsx("button",{className:"sa-drawer-btn-sm",onClick:()=>void H(a),children:"下载查看"})]}):a.type==="pdf"?e.jsx("iframe",{src:a.url,title:a.filename,className:"sa-drawer-preview-iframe",sandbox:"allow-scripts"}):t!==null?e.jsx("iframe",{srcDoc:t,title:a.filename,className:"sa-drawer-preview-iframe",sandbox:"allow-same-origin"}):null}function ue({artifacts:a,initialArtifact:t,onClose:s}){const[r,n]=c.useState(t?{mode:"preview",artifact:t}:{mode:"list"}),[o,i]=c.useState(!1),l=c.useRef(null),[d,p]=c.useState(340),f=c.useRef(null);c.useEffect(()=>{t&&(l.current&&(clearTimeout(l.current),l.current=null,i(!1)),n({mode:"preview",artifact:t}))},[t]);const u=c.useCallback(()=>{i(!0),l.current=setTimeout(()=>{l.current=null,s()},240)},[s]);c.useEffect(()=>()=>{var g;l.current&&clearTimeout(l.current),(g=f.current)==null||g.call(f)},[]);const m=c.useCallback(g=>{n({mode:"preview",artifact:g})},[]),w=c.useCallback(()=>{n({mode:"list"})},[]);c.useEffect(()=>{const g=v=>{v.key==="Escape"&&u()};return document.addEventListener("keydown",g),()=>document.removeEventListener("keydown",g)},[u]);const y=c.useCallback(g=>{g.preventDefault();const v=g.clientX,x=d,h=z=>{const I=v-z.clientX;p(Math.max(240,Math.min(window.innerWidth*.7,x+I)))},C=()=>{document.removeEventListener("mousemove",h),document.removeEventListener("mouseup",C),document.body.style.cursor="",document.body.style.userSelect="",f.current=null};document.body.style.cursor="col-resize",document.body.style.userSelect="none",document.addEventListener("mousemove",h),document.addEventListener("mouseup",C),f.current=C},[d]);return e.jsxs("div",{className:`sa-artifact-drawer ${o?"sa-drawer-closing":""}`,style:{width:d},children:[e.jsx("div",{className:"sa-drawer-resize-handle",onMouseDown:y}),e.jsxs("div",{className:"sa-artifact-drawer-header",children:[r.mode==="preview"?e.jsxs(e.Fragment,{children:[e.jsx("button",{className:"sa-drawer-btn",onClick:w,title:"返回列表","aria-label":"返回列表",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M15 18l-6-6 6-6"})})}),e.jsx("span",{className:"sa-artifact-drawer-title",children:r.artifact.filename})]}):e.jsxs(e.Fragment,{children:[e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",style:{flexShrink:0},children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{className:"sa-artifact-drawer-title",children:"会话产物"})]}),e.jsx("div",{style:{flex:1}}),r.mode==="preview"&&e.jsx("button",{className:"sa-drawer-btn",onClick:()=>void H(r.artifact),title:"下载","aria-label":"下载",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})}),e.jsx("button",{className:"sa-drawer-btn",onClick:u,title:"关闭","aria-label":"关闭",children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M18 6L6 18M6 6l12 12"})})})]}),e.jsx("div",{className:"sa-artifact-drawer-body",children:r.mode==="list"?a.length===0?e.jsx("div",{className:"sa-drawer-empty",children:"暂无产物"}):e.jsx("ul",{className:"sa-drawer-file-list",role:"list",children:a.map((g,v)=>e.jsxs("li",{className:"sa-drawer-file-card",onClick:()=>m(g),onKeyDown:x=>{(x.key==="Enter"||x.key===" ")&&(x.preventDefault(),m(g))},tabIndex:0,role:"button","aria-label":`预览 ${g.filename}`,children:[e.jsx("div",{className:"sa-drawer-file-icon",children:e.jsx("span",{className:"sa-artifact-chip-type",children:G(g.type)})}),e.jsxs("div",{className:"sa-drawer-file-info",children:[e.jsx("span",{className:"sa-drawer-file-name",children:g.filename}),e.jsx("span",{className:"sa-drawer-file-size",children:J(g.size)})]}),e.jsx("button",{className:"sa-drawer-btn",title:"下载","aria-label":`下载 ${g.filename}`,onClick:x=>{x.stopPropagation(),H(g)},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5M12 15V3"})})})]},g.id||`artifact-${v}`))}):e.jsx("div",{className:"sa-drawer-view-enter",children:e.jsx(Re,{artifact:r.artifact})})})]})}function Oe({isOpen:a,onClose:t,title:s,slots:r,welcomeMessage:n,suggestedPrompts:o,avatar:i,artifactPreview:l}){const{status:d,sendMessage:p,stop:f,toggleThreadList:u,toggleArtifactDrawer:m,conversations:w,activeConversationId:y,switchConversation:g,newConversation:v,deleteConversation:x,renameConversation:h,isThreadListOpen:C,isArtifactDrawerOpen:z,allArtifacts:I,activePreviewArtifact:A}=B();if(!a)return null;const k=r.Header,T=r.Composer,D=r.ThreadList;return e.jsxs("div",{className:"sa-panel",children:[k?e.jsx(k,{title:s,onClose:t,onToggleThreadList:u}):e.jsx(ae,{title:s,onClose:t,onToggleThreadList:u,artifactCount:l?I.length:0,onToggleArtifactDrawer:l?m:void 0}),D&&C?e.jsx(D,{conversations:w,activeSessionId:y,onSwitch:g,onNew:v,onDelete:x,onRename:h}):e.jsx(te,{}),e.jsxs("div",{className:"sa-panel-content",children:[e.jsx(q,{slots:r,welcomeMessage:n,suggestedPrompts:o,avatar:i,artifactPreview:l}),l&&z&&e.jsx(ue,{artifacts:I,initialArtifact:A,onClose:m})]}),T?e.jsx(T,{status:d,onSend:p,onStop:f}):e.jsx(Y,{})]})}function Be({conversations:a,activeSessionId:t,onSwitch:s,onNew:r,onDelete:n,isCollapsed:o,onToggleCollapse:i}){const[l,d]=c.useState(null),[p,f]=c.useState(null),u=c.useCallback((w,y)=>{y.stopPropagation(),p===w?(n(w),f(null)):(f(w),setTimeout(()=>f(null),3e3))},[p,n]),m=(()=>{const w=Date.now(),y=[],g=[],v=[];for(const h of a){const C=w-new Date(h.updateTime).getTime();C<864e5?y.push(h):C<6048e5?g.push(h):v.push(h)}const x=[];return y.length&&x.push({label:"今天",items:y}),g.length&&x.push({label:"最近 7 天",items:g}),v.length&&x.push({label:"更早",items:v}),x})();return o?e.jsxs("div",{className:"sa-fp-sidebar sa-fp-sidebar-collapsed",children:[e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:i,title:"展开",children:e.jsx(j,{name:"panel-left",size:18})}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:r,title:"新会话",style:{marginTop:4},children:e.jsx(j,{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(j,{name:"plus",size:15})," ",e.jsx("span",{children:"新会话"})]}),e.jsx("button",{className:"sa-fp-sidebar-toggle",onClick:i,title:"收起侧边栏",children:e.jsx(j,{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:"开始你的第一个对话"}),m.map(w=>e.jsxs("div",{children:[e.jsx("div",{className:"sa-fp-sidebar-group",children:w.label}),w.items.map(y=>{const g=y.sessionId===t,v=l===y.sessionId,x=p===y.sessionId;return e.jsxs("div",{className:`sa-fp-sidebar-item ${g?"active":""}`,onClick:()=>s(y.sessionId),onMouseEnter:()=>d(y.sessionId),onMouseLeave:()=>d(null),children:[e.jsx("span",{className:"sa-fp-sidebar-item-title",children:y.title||"新会话"}),v&&e.jsx("button",{className:`sa-fp-sidebar-item-action ${x?"danger":""}`,onClick:h=>u(y.sessionId,h),title:x?"确认删除":"删除",children:e.jsx(j,{name:x?"x":"trash2",size:14})})]},y.sessionId)})]},w.label))]})]})}function xe({title:a,slots:t,welcomeMessage:s,suggestedPrompts:r,sidebarDefaultOpen:n=!0,onClose:o,avatar:i,artifactPreview:l}){const{status:d,sendMessage:p,stop:f,conversations:u,activeConversationId:m,switchConversation:w,newConversation:y,deleteConversation:g,renameConversation:v,toggleArtifactDrawer:x,isArtifactDrawerOpen:h,allArtifacts:C,activePreviewArtifact:z}=B(),[I,A]=c.useState(n),k=c.useCallback(()=>A(S=>!S),[]),T=t.ThreadList,D=t.Composer;return e.jsxs("div",{className:"sa-fullpage",children:[T?e.jsx("div",{className:`sa-fp-sidebar-wrapper ${I?"":"sa-fp-sidebar-wrapper-collapsed"}`,children:e.jsx(T,{conversations:u,activeSessionId:m,onSwitch:w,onNew:y,onDelete:g,onRename:v})}):e.jsx(Be,{conversations:u,activeSessionId:m,onSwitch:w,onNew:y,onDelete:g,onRename:v,isCollapsed:!I,onToggleCollapse:k}),e.jsxs("div",{className:"sa-fp-main",children:[e.jsxs("div",{className:"sa-fp-header",children:[!I&&!T&&e.jsx("button",{className:"sa-fp-header-btn",onClick:k,title:"展开侧边栏",children:e.jsx(j,{name:"panel-left",size:18})}),e.jsx("div",{className:"sa-fp-header-title",children:a}),e.jsxs("div",{className:"sa-fp-header-actions",children:[l&&C.length>0&&e.jsxs("button",{className:"sa-fp-header-btn sa-header-btn-artifact",onClick:x,title:"会话产物",children:[e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsx("span",{children:"产物"})]}),o&&e.jsx("button",{className:"sa-fp-header-btn",onClick:o,title:"关闭",children:e.jsx(j,{name:"x",size:18})})]})]}),e.jsxs("div",{className:"sa-fp-content-row",children:[e.jsx(q,{slots:t,welcomeMessage:s,suggestedPrompts:r,avatar:i,artifactPreview:l}),l&&h&&e.jsx(ue,{artifacts:C,initialArtifact:z,onClose:x})]}),D?e.jsx(D,{status:d,onSend:p,onStop:f}):e.jsx(Y,{})]})]})}function me(a){const{sdk:t,mode:s="floating",slots:r={},hooks:n={},welcomeMessage:o,suggestedPrompts:i,title:l="AI Assistant",initialOpen:d=!1,onOpenChange:p,sidebarDefaultOpen:f,avatar:u,artifactPreview:m}=a,[w,y]=c.useState(d);c.useEffect(()=>{y(d)},[d]);const g=c.useCallback(()=>{var C,z;const h=!w;y(h),p==null||p(h),h?(C=n.onOpen)==null||C.call(n):(z=n.onClose)==null||z.call(n)},[w,n,p]),v=c.useCallback(()=>{var h;y(!1),p==null||p(!1),(h=n.onClose)==null||h.call(n)},[n,p]),x=r.Trigger??ee;return s==="fullpage"?w?e.jsx(W,{sdk:t,hooks:n,children:e.jsx(xe,{title:l,slots:r,welcomeMessage:o,suggestedPrompts:i,sidebarDefaultOpen:f,onClose:v,avatar:u,artifactPreview:m})}):null:e.jsxs(W,{sdk:t,hooks:n,children:[e.jsx(x,{isOpen:w,onClick:g}),e.jsx(Oe,{isOpen:w,onClose:v,title:l,slots:r,welcomeMessage:o,suggestedPrompts:i,avatar:u,artifactPreview:m})]})}const he=`
|
|
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);
|
|
@@ -558,8 +558,8 @@
|
|
|
558
558
|
|
|
559
559
|
/* ── Dislike Panel ── */
|
|
560
560
|
.sa-dislike-panel {
|
|
561
|
-
position: absolute;
|
|
562
|
-
margin-
|
|
561
|
+
position: absolute; top: 100%; left: 0; right: 0;
|
|
562
|
+
margin-top: 6px; padding: 12px;
|
|
563
563
|
background: #fff; border: 1px solid var(--sa-border); border-radius: 10px;
|
|
564
564
|
box-shadow: 0 4px 16px rgba(0,0,0,0.08);
|
|
565
565
|
animation: sa-fade-in-up 0.2s cubic-bezier(0.16,1,0.3,1) both;
|
|
@@ -1529,4 +1529,4 @@
|
|
|
1529
1529
|
from { opacity: 0; transform: translateX(12px); }
|
|
1530
1530
|
to { opacity: 1; transform: translateX(0); }
|
|
1531
1531
|
}
|
|
1532
|
-
`,ge={primaryColor:"#6366f1",backgroundColor:"#ffffff",fontFamily:"system-ui, -apple-system, sans-serif",borderRadius:16,panelWidth:380,panelHeight:520,zIndex:9999};function be(a){const t={...ge,...a};return[`--sa-primary: ${t.primaryColor}`,`--sa-bg: ${t.backgroundColor}`,`--sa-font: ${t.fontFamily}`,`--sa-radius: ${t.borderRadius}px`,`--sa-panel-width: ${t.panelWidth}px`,`--sa-panel-height: ${t.panelHeight}px`,`--sa-z-index: ${t.zIndex}`].join("; ")}function
|
|
1532
|
+
`,ge={primaryColor:"#6366f1",backgroundColor:"#ffffff",fontFamily:"system-ui, -apple-system, sans-serif",borderRadius:16,panelWidth:380,panelHeight:520,zIndex:9999};function be(a){const t={...ge,...a};return[`--sa-primary: ${t.primaryColor}`,`--sa-bg: ${t.backgroundColor}`,`--sa-font: ${t.fontFamily}`,`--sa-radius: ${t.borderRadius}px`,`--sa-panel-width: ${t.panelWidth}px`,`--sa-panel-height: ${t.panelHeight}px`,`--sa-z-index: ${t.zIndex}`].join("; ")}function Fe(a,t){const s=typeof a=="string"?document.querySelector(a):a;if(!s)throw new Error(`Target element not found: ${a}`);const r=document.createElement("div");r.setAttribute("data-super-agent-widget","");const n=be(t.theme??{});if(r.setAttribute("style",n),s.appendChild(r),!document.getElementById("sa-widget-styles")){const d=document.createElement("style");d.id="sa-widget-styles",d.textContent=he,document.head.appendChild(d)}let o=ve.createRoot(r),i=!1;const l=d=>{o&&o.render(c.createElement(me,{...t,initialOpen:d??i,onOpenChange:p=>{i=p}}))};return l(!1),{open(){i=!0,l(!0)},close(){i=!1,l(!1)},destroy(){o&&(o.unmount(),o=null),r.remove()}}}exports.ActionBar=de;exports.ChatProvider=W;exports.ChatWidget=me;exports.Composer=Y;exports.ErrorPart=le;exports.FullpageLayout=xe;exports.Header=ae;exports.Icon=j;exports.InterruptCard=ce;exports.Message=pe;exports.TextPart=re;exports.ThinkingPart=ne;exports.Thread=q;exports.ThreadList=te;exports.ToolCallPart=ie;exports.ToolResultPart=oe;exports.Trigger=ee;exports.WelcomeScreen=fe;exports.defaultTheme=ge;exports.mount=Fe;exports.themeToCSSVars=be;exports.useAutoScroll=se;exports.useChat=U;exports.useChatContext=B;exports.useConversations=X;exports.widgetStyles=he;
|