super-agent-sdk 1.0.4 → 1.0.5
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 +21 -12
- package/dist/index.cjs +4 -4
- package/dist/index.d.ts +38 -8
- package/dist/index.mjs +195 -167
- package/dist/mammoth.browser-COPV53yV.js +14917 -0
- package/dist/mammoth.browser-DiAPPO3x.cjs +230 -0
- package/dist/purify.es-C-6FFqDW.js +898 -0
- package/dist/purify.es-Dsdnkgrg.cjs +3 -0
- package/dist/widget.cjs +214 -5
- package/dist/widget.d.ts +61 -13
- package/dist/widget.mjs +1308 -655
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -141,8 +141,8 @@ export interface SuperAgentSDK {
|
|
|
141
141
|
getMessages(sessionId: string): Promise<UIMessage[]>;
|
|
142
142
|
getToken(): Promise<string>;
|
|
143
143
|
setToken(token: string): void;
|
|
144
|
-
feedback(messageId: string, type: "like" | "dislike"): void
|
|
145
|
-
|
|
144
|
+
feedback(messageId: string, type: "like" | "dislike", options?: { reason?: number; remark?: string }): Promise<void>;
|
|
145
|
+
cancelFeedback(messageId: string): Promise<void>;
|
|
146
146
|
}
|
|
147
147
|
```
|
|
148
148
|
|
|
@@ -296,21 +296,28 @@ sdk.setToken(token: string): void
|
|
|
296
296
|
sdk.setToken("<TOKEN>");
|
|
297
297
|
```
|
|
298
298
|
|
|
299
|
-
### 5.9 feedback() /
|
|
299
|
+
### 5.9 feedback() / cancelFeedback()
|
|
300
300
|
|
|
301
|
-
|
|
301
|
+
消息反馈(赞/踩)。调用 `sdk.feedback()` 发送 `POST /chat/messages/{messageId}/feedback`;调用 `sdk.cancelFeedback()` 发送 `DELETE` 取消反馈。
|
|
302
302
|
|
|
303
303
|
```ts
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
console.log(`Feedback: ${type} on message ${messageId}`);
|
|
307
|
-
// 上报到自己的业务后端
|
|
308
|
-
};
|
|
304
|
+
// 点赞
|
|
305
|
+
await sdk.feedback("msg_123", "like");
|
|
309
306
|
|
|
310
|
-
|
|
307
|
+
// 踩(可附原因 + 备注)
|
|
308
|
+
await sdk.feedback("msg_123", "dislike", {
|
|
309
|
+
reason: 1, // 1=事实错误 2=逻辑问题 3=不相关 4=信息过时 5=冗长啰嗦 6=难以理解
|
|
310
|
+
remark: "时间描述有误",
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// 取消反馈
|
|
314
|
+
await sdk.cancelFeedback("msg_123");
|
|
311
315
|
```
|
|
312
316
|
|
|
313
|
-
|
|
317
|
+
组件内置的点赞/踩按钮已自动调用 `feedback()`/`cancelFeedback()`:
|
|
318
|
+
- 点击已选中的按钮 → 取消(DELETE)
|
|
319
|
+
- 点击另一个按钮 → 切换(POST)
|
|
320
|
+
- 点踩时弹出原因选择面板(可选填原因 + 备注后提交)
|
|
314
321
|
|
|
315
322
|
### 5.10 后端接口对照
|
|
316
323
|
|
|
@@ -320,10 +327,12 @@ SDK 方法 → 后端接口的完整映射:
|
|
|
320
327
|
|
|
321
328
|
| SDK 方法 | HTTP 请求 | 请求体 | 后端返回 |
|
|
322
329
|
| --------------------- | ------------------------- | --------------------------------------- | ----------------------------------------------- |
|
|
323
|
-
| `getToken()` | `POST /
|
|
330
|
+
| `getToken()` | `POST {gateway}/v1/token` | `{ botId }` | `{ token, appId }` |
|
|
324
331
|
| `createSession()` | `POST /chat/sessions` | `{ botId }` | `{ sessionId }` |
|
|
325
332
|
| `chat()` | `POST /chat` | `{ botId, sessionId, message, stream }` | SSE 流,`done` 事件含 `sessionId` |
|
|
326
333
|
| `listConversations()` | `GET /chat/conversations` | 查询参数 `botId`、`page`、`size` | `{ items: [{ sessionId, botId, ... }], total }` |
|
|
334
|
+
| `feedback()` | `POST /chat/messages/{messageId}/feedback` | `{ type, reason?, remark? }` | `null` |
|
|
335
|
+
| `cancelFeedback()` | `DELETE /chat/messages/{messageId}/feedback` | 无 body | `null` |
|
|
327
336
|
|
|
328
337
|
---
|
|
329
338
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function
|
|
2
|
-
`).map(
|
|
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};case"done":return{type:"done",content:t.content??"",sessionId:t.sessionId??t.session_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
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function f(a){if(Array.isArray(a))return a.map(e=>f(e));if(a&&typeof a=="object"&&a.constructor===Object){const e={};for(const[t,n]of Object.entries(a)){const o=t.replace(/_([a-z])/g,(r,s)=>s.toUpperCase());e[o]=f(n)}return e}return a}const w="/api/agent/runtime/v1";class I{constructor(e){this.refreshing=null;const t=e.baseUrl??w;this.baseUrl=t.replace(/\/$/,""),this.tokenGateway=e.tokenGateway.replace(/\/$/,""),this.appId=e.appId??"",this.token=e.token??"",this.botId=e.botId,this.isAbsoluteUrl=/^https?:\/\//.test(this.baseUrl),this.isAbsoluteGateway=/^https?:\/\//.test(this.tokenGateway)}getBotId(){return this.botId}getAppKey(){return this.appId}setToken(e){this.token=e}getOrigin(){return typeof window<"u"?window.location.origin:""}async fetchToken(){const t=`${this.isAbsoluteGateway?this.tokenGateway:`${this.getOrigin()}${this.tokenGateway}`}/v1/token`,n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({botId:this.botId})});if(!n.ok)throw new Error(`HTTP ${n.status}: ${n.statusText}`);const o=await n.json();if(o.code!=="200")throw new Error(o.message||`API error: ${o.code}`);const r=f(o.data);return this.token=r.token,r.appId&&!this.appId&&(this.appId=r.appId),r.token}headers(e){return{"Content-Type":"application/json","X-App-Id":this.appId,"X-Token":this.token,...e}}async handleTokenRefresh(){if(this.refreshing)return await this.refreshing,!0;try{return this.refreshing=(async()=>{await this.fetchToken()})(),await this.refreshing,!0}catch{return!1}finally{this.refreshing=null}}async request(e,t,n){if(!this.token&&!await this.handleTokenRefresh())throw new Error("SDK token not available: POST /token failed");let o=this.isAbsoluteUrl?`${this.baseUrl}${t}`:`${this.getOrigin()}${this.baseUrl}${t}`;if(n!=null&&n.params){const i=new URLSearchParams;for(const[l,d]of Object.entries(n.params))d!=null&&i.set(l,String(d));const c=i.toString();c&&(o+=`?${c}`)}const r=await fetch(o,{method:e,headers:this.headers(),body:n!=null&&n.body?JSON.stringify(n.body):void 0});if(r.status===401&&!(n!=null&&n.retry)&&await this.handleTokenRefresh())return this.request(e,t,{...n,retry:!0});if(!r.ok)throw new Error(`HTTP ${r.status}: ${r.statusText}`);const s=await r.json();if(s.code!=="200")throw new Error(s.message||`API error: ${s.code}`);return f(s.data)}get(e,t){return this.request("GET",e,{params:t})}post(e,t){return this.request("POST",e,{body:t})}patch(e,t){return this.request("PATCH",e,{body:t})}del(e){return this.request("DELETE",e)}async streamPost(e,t,n){if(!this.token&&!await this.handleTokenRefresh())throw new Error("SDK token not available: POST /token failed");const o=this.isAbsoluteUrl?`${this.baseUrl}${e}`:`${this.getOrigin()}${this.baseUrl}${e}`,r=await fetch(o,{method:"POST",headers:this.headers(),body:JSON.stringify(t),signal:n});return r.status===401&&await this.handleTokenRefresh()?fetch(o,{method:"POST",headers:this.headers(),body:JSON.stringify(t),signal:n}):r}}function y(a){const e=a.split(`
|
|
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
|
+
`))}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=
|
|
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;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/** 通用产物(INTERFACE 约定:type + data,适配 table/chart/file/text/image/html/custom) */
|
|
2
|
+
export declare interface Artifact {
|
|
3
|
+
type: string;
|
|
4
|
+
data: Record<string, any>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export declare interface ArtifactFile {
|
|
8
|
+
id: string;
|
|
9
|
+
type: "html" | "pdf" | "docx" | string;
|
|
10
|
+
filename: string;
|
|
11
|
+
url: string;
|
|
12
|
+
mime?: string;
|
|
13
|
+
size?: number;
|
|
14
|
+
summary?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
1
17
|
export declare interface ChatEvent {
|
|
2
18
|
type: "user" | "thinking" | "ai" | "tool_call" | "tool_result" | "done" | "error" | "interrupt";
|
|
3
19
|
content: string;
|
|
@@ -5,7 +21,11 @@ export declare interface ChatEvent {
|
|
|
5
21
|
toolCallId?: string;
|
|
6
22
|
args?: string;
|
|
7
23
|
sessionId?: string;
|
|
24
|
+
messageId?: string;
|
|
8
25
|
interrupt?: InterruptEvent;
|
|
26
|
+
artifacts?: Artifact[];
|
|
27
|
+
renderMode?: string;
|
|
28
|
+
html?: string;
|
|
9
29
|
}
|
|
10
30
|
|
|
11
31
|
export declare interface ChatOptions {
|
|
@@ -17,6 +37,7 @@ export declare interface ChatOptions {
|
|
|
17
37
|
onDone?: (result: {
|
|
18
38
|
sessionId: string;
|
|
19
39
|
content: string;
|
|
40
|
+
messageId?: string;
|
|
20
41
|
}) => void;
|
|
21
42
|
signal?: AbortSignal;
|
|
22
43
|
}
|
|
@@ -67,7 +88,7 @@ export declare interface InterruptResponse {
|
|
|
67
88
|
value?: any;
|
|
68
89
|
}
|
|
69
90
|
|
|
70
|
-
export declare type InterruptType = 'confirm' | 'select' | 'multiSelect' | 'input' | 'form';
|
|
91
|
+
export declare type InterruptType = 'confirm' | 'select' | 'multiSelect' | 'input' | 'form' | 'approve';
|
|
71
92
|
|
|
72
93
|
export declare interface ListConversationsParams {
|
|
73
94
|
page?: number;
|
|
@@ -95,6 +116,9 @@ export declare type MessagePart = {
|
|
|
95
116
|
toolName: string;
|
|
96
117
|
toolCallId: string;
|
|
97
118
|
content: string;
|
|
119
|
+
artifacts?: Artifact[];
|
|
120
|
+
renderMode?: string;
|
|
121
|
+
html?: string;
|
|
98
122
|
} | {
|
|
99
123
|
type: "error";
|
|
100
124
|
content: string;
|
|
@@ -106,14 +130,15 @@ export declare type MessagePart = {
|
|
|
106
130
|
};
|
|
107
131
|
|
|
108
132
|
export declare interface SDKConfig {
|
|
109
|
-
baseUrl: string;
|
|
110
133
|
botId: number;
|
|
111
|
-
|
|
134
|
+
tokenGateway: string;
|
|
135
|
+
baseUrl?: string;
|
|
136
|
+
appId?: string;
|
|
112
137
|
token?: string;
|
|
113
138
|
}
|
|
114
139
|
|
|
115
140
|
export declare interface SuperAgentSDK {
|
|
116
|
-
/** 获取 token(调用 POST /
|
|
141
|
+
/** 获取 token(调用 POST {tokenGateway}/v1/token),自动设置内部凭证 */
|
|
117
142
|
getToken(): Promise<string>;
|
|
118
143
|
/** 创建新会话,返回 sessionId */
|
|
119
144
|
createSession(): Promise<string>;
|
|
@@ -122,11 +147,16 @@ export declare interface SuperAgentSDK {
|
|
|
122
147
|
renameConversation(sessionId: string, title: string): Promise<void>;
|
|
123
148
|
deleteConversation(sessionId: string): Promise<void>;
|
|
124
149
|
getMessages(sessionId: string): Promise<UIMessage[]>;
|
|
125
|
-
/**
|
|
126
|
-
respondInterrupt(interruptId: string, sessionId: string, response: InterruptResponse): Promise<void>;
|
|
150
|
+
/** 响应中断事件(返回 SSE 流,后端继续推送后续事件) */
|
|
151
|
+
respondInterrupt(interruptId: string, sessionId: string, response: InterruptResponse, onMessage?: (event: ChatEvent) => void, onError?: (error: Error) => void): Promise<void>;
|
|
127
152
|
setToken(token: string): void;
|
|
128
|
-
|
|
129
|
-
|
|
153
|
+
/** 消息反馈(赞/踩),POST /chat/messages/{messageId}/feedback */
|
|
154
|
+
feedback(messageId: string, type: "like" | "dislike", options?: {
|
|
155
|
+
reason?: number;
|
|
156
|
+
remark?: string;
|
|
157
|
+
}): Promise<void>;
|
|
158
|
+
/** 取消消息反馈,DELETE /chat/messages/{messageId}/feedback */
|
|
159
|
+
cancelFeedback(messageId: string): Promise<void>;
|
|
130
160
|
}
|
|
131
161
|
|
|
132
162
|
export declare interface UIMessage {
|