super-agent-sdk 1.0.0
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 +1316 -0
- package/dist/index.cjs +5 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.mjs +345 -0
- package/dist/widget.cjs +1124 -0
- package/dist/widget.d.ts +394 -0
- package/dist/widget.mjs +2077 -0
- package/package.json +50 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function h(s){if(Array.isArray(s))return s.map(e=>h(e));if(s&&typeof s=="object"&&s.constructor===Object){const e={};for(const[r,t]of Object.entries(s)){const o=r.replace(/_([a-z])/g,(n,a)=>a.toUpperCase());e[o]=h(t)}return e}return s}class g{constructor(e){this.refreshing=null,this.baseUrl=e.baseUrl.replace(/\/$/,""),this.appId=e.appId??"",this.token=e.token??"",this.botId=e.botId}getBotId(){return this.botId}setToken(e,r){this.appId=e,this.token=r}async postPublic(e,r){const t=`${this.baseUrl}${e}`,o=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:r?JSON.stringify(r):void 0});if(!o.ok)throw new Error(`HTTP ${o.status}: ${o.statusText}`);const n=await o.json();if(n.code!=="200")throw new Error(n.message||`API error: ${n.code}`);return h(n.data)}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()=>{const e=await this.postPublic("/token",{botId:this.botId});this.appId=e.appId,this.token=e.token})(),await this.refreshing,!0}catch{return!1}finally{this.refreshing=null}}async request(e,r,t){if((!this.appId||!this.token)&&!await this.handleTokenRefresh())throw new Error("SDK token not available: POST /token failed");let o=`${this.baseUrl}${r}`;if(t!=null&&t.params){const c=new URLSearchParams;for(const[l,d]of Object.entries(t.params))d!=null&&c.set(l,String(d));const i=c.toString();i&&(o+=`?${i}`)}const n=await fetch(o,{method:e,headers:this.headers(),body:t!=null&&t.body?JSON.stringify(t.body):void 0});if(n.status===401&&!(t!=null&&t.retry)&&await this.handleTokenRefresh())return this.request(e,r,{...t,retry:!0});if(!n.ok)throw new Error(`HTTP ${n.status}: ${n.statusText}`);const a=await n.json();if(a.code!=="200")throw new Error(a.message||`API error: ${a.code}`);return h(a.data)}get(e,r){return this.request("GET",e,{params:r})}post(e,r){return this.request("POST",e,{body:r})}patch(e,r){return this.request("PATCH",e,{body:r})}del(e){return this.request("DELETE",e)}async streamPost(e,r,t){if((!this.appId||!this.token)&&!await this.handleTokenRefresh())throw new Error("SDK token not available: POST /token failed");const o=`${this.baseUrl}${e}`,n=await fetch(o,{method:"POST",headers:this.headers(),body:JSON.stringify(r),signal:t});return n.status===401&&await this.handleTokenRefresh()?fetch(o,{method:"POST",headers:this.headers(),body:JSON.stringify(r),signal:t}):n}}function f(s){const e=s.split(`
|
|
2
|
+
`).map(t=>t.trim()).filter(t=>t.startsWith("data:")).map(t=>t.slice(5).trim());if(!e.length)return null;let r;try{r=JSON.parse(e.join(`
|
|
3
|
+
`))}catch{return null}switch(r.type){case"user":return{type:"user",content:r.content??""};case"thinking":return{type:"thinking",content:r.content??""};case"ai":return{type:"ai",content:r.content??""};case"tool_call":return{type:"tool_call",content:r.name??"",toolName:r.name,toolCallId:r.id,args:typeof r.args=="string"?r.args:JSON.stringify(r.args??{})};case"tool_result":return{type:"tool_result",content:r.output??"",toolName:r.name,toolCallId:r.id};case"done":return{type:"done",content:r.content??"",sessionId:r.sessionId??r.session_id??""};case"error":return{type:"error",content:r.message??"Unknown error"};default:return null}}async function y(s,e,r){if(!s.body){r(new Error("Response body is null"));return}const t=s.body.getReader(),o=new TextDecoder;let n="";try{for(;;){const{value:a,done:c}=await t.read();if(c)break;n+=o.decode(a,{stream:!0});let i;for(;(i=n.indexOf(`
|
|
4
|
+
|
|
5
|
+
`))!==-1;){const l=n.slice(0,i);n=n.slice(i+2);const d=f(l);d&&e(d)}}if(n.trim()){const a=f(n);a&&e(a)}}catch(a){(a==null?void 0:a.name)!=="AbortError"&&r(a instanceof Error?a:new Error(String(a)))}finally{try{t.releaseLock()}catch{}}}function m(s,e){const r=new AbortController,t=e.signal?I(e.signal,r.signal):r.signal,o={botId:s.getBotId(),message:e.message,sessionId:e.sessionId,stream:e.stream!==!1};return o.stream?s.streamPost("/chat",o,t).then(n=>{var a;if(!n.ok){(a=e.onError)==null||a.call(e,new Error(`HTTP ${n.status}`));return}return y(n,c=>{var i,l,d,u;c.type==="done"&&c.sessionId?(i=e.onDone)==null||i.call(e,{sessionId:c.sessionId,content:c.content}):c.type==="done"?(l=e.onDone)==null||l.call(e,{sessionId:"",content:c.content}):c.type==="error"?(d=e.onError)==null||d.call(e,new Error(c.content)):(u=e.onMessage)==null||u.call(e,c)},c=>{var i;return(i=e.onError)==null?void 0:i.call(e,c)})}).catch(n=>{var a;(n==null?void 0:n.name)!=="AbortError"&&((a=e.onError)==null||a.call(e,n instanceof Error?n:new Error(String(n))))}):s.post("/chat",o).then(n=>{var a;(a=e.onDone)==null||a.call(e,{sessionId:n.sessionId,content:n.content??""})}).catch(n=>{var a;(a=e.onError)==null||a.call(e,n instanceof Error?n:new Error(String(n)))}),r}function I(s,e){const r=new AbortController,t=()=>r.abort();return s.addEventListener("abort",t,{once:!0}),e.addEventListener("abort",t,{once:!0}),(s.aborted||e.aborted)&&r.abort(),r.signal}async function b(s){return(await s.post("/chat/sessions",{botId:s.getBotId()})).sessionId}function w(s){return{sessionId:s.threadId??s.sessionId,botId:s.botId,title:s.title,createTime:s.createTime,updateTime:s.updateTime}}async function k(s,e){const r=await s.get("/chat/conversations",{botId:s.getBotId(),page:(e==null?void 0:e.page)??1,size:(e==null?void 0:e.size)??20});return{...r,items:r.items.map(w)}}async function T(s,e,r){await s.patch(`/chat/conversations/${e}`,{title:r})}async function S(s,e){await s.del(`/chat/conversations/${e}`)}async function E(s,e){const r=await s.get(`/chat/conversations/${e}/messages`);return C(r)}function C(s){const e=[],r=[...s].sort((t,o)=>(t.seq??0)-(o.seq??0));for(const t of r){const o=t.role;if(o==="system")continue;const n=t.messageId??`msg_${t.id}`,a=new Date(t.createTime??"").getTime()||Date.now();if(o==="human")e.push({id:n,role:"user",parts:[{type:"text",content:t.content??""}],timestamp:a});else if(o==="ai"){const c=[];t.reasoning&&c.push({type:"thinking",content:t.reasoning}),t.content&&c.push({type:"text",content:t.content});const i=t.toolCalls;if(i&&Array.isArray(i))for(const l of i){let d;try{d=typeof l.args=="string"?l.args:JSON.stringify(l.args??{})}catch{d=String(l.args??"{}")}c.push({type:"tool_call",toolName:l.name??"",toolCallId:l.id??"",args:d})}e.push({id:n,role:"assistant",parts:c,timestamp:a})}else o==="tool"&&e.push({id:n,role:"assistant",parts:[{type:"tool_result",toolName:t.toolName??"",toolCallId:t.toolCallId??"",content:t.content??""}],timestamp:a})}return e}function P(s){const e=new g(s),r={getToken:async()=>{const t=await e.postPublic("/token",{botId:e.getBotId()});return e.setToken(t.appId,t.token),t},createSession:()=>b(e),chat:t=>m(e,t),listConversations:t=>k(e,t),renameConversation:(t,o)=>T(e,t,o),deleteConversation:t=>S(e,t),getMessages:t=>E(e,t),setToken:(t,o)=>e.setToken(t,o),feedback:(t,o)=>{var n;(n=r.onFeedback)==null||n.call(r,t,o)}};return r}exports.createSuperAgent=P;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
export declare interface ChatEvent {
|
|
2
|
+
type: "user" | "thinking" | "ai" | "tool_call" | "tool_result" | "done" | "error";
|
|
3
|
+
content: string;
|
|
4
|
+
toolName?: string;
|
|
5
|
+
toolCallId?: string;
|
|
6
|
+
args?: string;
|
|
7
|
+
sessionId?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export declare interface ChatOptions {
|
|
11
|
+
sessionId: string;
|
|
12
|
+
message: string;
|
|
13
|
+
stream?: boolean;
|
|
14
|
+
onMessage?: (event: ChatEvent) => void;
|
|
15
|
+
onError?: (error: Error) => void;
|
|
16
|
+
onDone?: (result: {
|
|
17
|
+
sessionId: string;
|
|
18
|
+
content: string;
|
|
19
|
+
}) => void;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export declare interface Conversation {
|
|
24
|
+
sessionId: string;
|
|
25
|
+
botId: number;
|
|
26
|
+
title: string | null;
|
|
27
|
+
createTime: string;
|
|
28
|
+
updateTime: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare function createSuperAgent(config: SDKConfig): SuperAgentSDK;
|
|
32
|
+
|
|
33
|
+
export declare interface ListConversationsParams {
|
|
34
|
+
page?: number;
|
|
35
|
+
size?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export declare interface ListConversationsResult {
|
|
39
|
+
items: Conversation[];
|
|
40
|
+
total: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export declare type MessagePart = {
|
|
44
|
+
type: "text";
|
|
45
|
+
content: string;
|
|
46
|
+
} | {
|
|
47
|
+
type: "thinking";
|
|
48
|
+
content: string;
|
|
49
|
+
} | {
|
|
50
|
+
type: "tool_call";
|
|
51
|
+
toolName: string;
|
|
52
|
+
toolCallId: string;
|
|
53
|
+
args: string;
|
|
54
|
+
} | {
|
|
55
|
+
type: "tool_result";
|
|
56
|
+
toolName: string;
|
|
57
|
+
toolCallId: string;
|
|
58
|
+
content: string;
|
|
59
|
+
} | {
|
|
60
|
+
type: "error";
|
|
61
|
+
content: string;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export declare interface SDKConfig {
|
|
65
|
+
baseUrl: string;
|
|
66
|
+
botId: number;
|
|
67
|
+
appId?: string;
|
|
68
|
+
token?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export declare interface SuperAgentSDK {
|
|
72
|
+
/** 获取 token(调用 POST /token),自动设置内部凭证 */
|
|
73
|
+
getToken(): Promise<{
|
|
74
|
+
appId: string;
|
|
75
|
+
token: string;
|
|
76
|
+
}>;
|
|
77
|
+
/** 创建新会话,返回 sessionId */
|
|
78
|
+
createSession(): Promise<string>;
|
|
79
|
+
chat(options: ChatOptions): AbortController;
|
|
80
|
+
listConversations(params?: ListConversationsParams): Promise<ListConversationsResult>;
|
|
81
|
+
renameConversation(sessionId: string, title: string): Promise<void>;
|
|
82
|
+
deleteConversation(sessionId: string): Promise<void>;
|
|
83
|
+
getMessages(sessionId: string): Promise<UIMessage[]>;
|
|
84
|
+
setToken(appId: string, token: string): void;
|
|
85
|
+
feedback(sessionId: string, type: "like" | "dislike"): void;
|
|
86
|
+
onFeedback?: (sessionId: string, type: "like" | "dislike") => void;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export declare interface UIMessage {
|
|
90
|
+
id: string;
|
|
91
|
+
role: "user" | "assistant";
|
|
92
|
+
parts: MessagePart[];
|
|
93
|
+
timestamp: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { }
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
function h(s) {
|
|
2
|
+
if (Array.isArray(s)) return s.map((e) => h(e));
|
|
3
|
+
if (s && typeof s == "object" && s.constructor === Object) {
|
|
4
|
+
const e = {};
|
|
5
|
+
for (const [r, t] of Object.entries(s)) {
|
|
6
|
+
const o = r.replace(/_([a-z])/g, (n, a) => a.toUpperCase());
|
|
7
|
+
e[o] = h(t);
|
|
8
|
+
}
|
|
9
|
+
return e;
|
|
10
|
+
}
|
|
11
|
+
return s;
|
|
12
|
+
}
|
|
13
|
+
class g {
|
|
14
|
+
constructor(e) {
|
|
15
|
+
this.refreshing = null, this.baseUrl = e.baseUrl.replace(/\/$/, ""), this.appId = e.appId ?? "", this.token = e.token ?? "", this.botId = e.botId;
|
|
16
|
+
}
|
|
17
|
+
getBotId() {
|
|
18
|
+
return this.botId;
|
|
19
|
+
}
|
|
20
|
+
setToken(e, r) {
|
|
21
|
+
this.appId = e, this.token = r;
|
|
22
|
+
}
|
|
23
|
+
/** POST without auth headers (for /token endpoint) */
|
|
24
|
+
async postPublic(e, r) {
|
|
25
|
+
const t = `${this.baseUrl}${e}`, o = await fetch(t, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
headers: { "Content-Type": "application/json" },
|
|
28
|
+
body: r ? JSON.stringify(r) : void 0
|
|
29
|
+
});
|
|
30
|
+
if (!o.ok)
|
|
31
|
+
throw new Error(`HTTP ${o.status}: ${o.statusText}`);
|
|
32
|
+
const n = await o.json();
|
|
33
|
+
if (n.code !== "200")
|
|
34
|
+
throw new Error(n.message || `API error: ${n.code}`);
|
|
35
|
+
return h(n.data);
|
|
36
|
+
}
|
|
37
|
+
headers(e) {
|
|
38
|
+
return {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
"X-App-Id": this.appId,
|
|
41
|
+
"X-Token": this.token,
|
|
42
|
+
...e
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async handleTokenRefresh() {
|
|
46
|
+
if (this.refreshing)
|
|
47
|
+
return await this.refreshing, !0;
|
|
48
|
+
try {
|
|
49
|
+
return this.refreshing = (async () => {
|
|
50
|
+
const e = await this.postPublic("/token", { botId: this.botId });
|
|
51
|
+
this.appId = e.appId, this.token = e.token;
|
|
52
|
+
})(), await this.refreshing, !0;
|
|
53
|
+
} catch {
|
|
54
|
+
return !1;
|
|
55
|
+
} finally {
|
|
56
|
+
this.refreshing = null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async request(e, r, t) {
|
|
60
|
+
if ((!this.appId || !this.token) && !await this.handleTokenRefresh())
|
|
61
|
+
throw new Error("SDK token not available: POST /token failed");
|
|
62
|
+
let o = `${this.baseUrl}${r}`;
|
|
63
|
+
if (t != null && t.params) {
|
|
64
|
+
const c = new URLSearchParams();
|
|
65
|
+
for (const [l, d] of Object.entries(t.params))
|
|
66
|
+
d != null && c.set(l, String(d));
|
|
67
|
+
const i = c.toString();
|
|
68
|
+
i && (o += `?${i}`);
|
|
69
|
+
}
|
|
70
|
+
const n = await fetch(o, {
|
|
71
|
+
method: e,
|
|
72
|
+
headers: this.headers(),
|
|
73
|
+
body: t != null && t.body ? JSON.stringify(t.body) : void 0
|
|
74
|
+
});
|
|
75
|
+
if (n.status === 401 && !(t != null && t.retry) && await this.handleTokenRefresh())
|
|
76
|
+
return this.request(e, r, { ...t, retry: !0 });
|
|
77
|
+
if (!n.ok)
|
|
78
|
+
throw new Error(`HTTP ${n.status}: ${n.statusText}`);
|
|
79
|
+
const a = await n.json();
|
|
80
|
+
if (a.code !== "200")
|
|
81
|
+
throw new Error(a.message || `API error: ${a.code}`);
|
|
82
|
+
return h(a.data);
|
|
83
|
+
}
|
|
84
|
+
get(e, r) {
|
|
85
|
+
return this.request("GET", e, { params: r });
|
|
86
|
+
}
|
|
87
|
+
post(e, r) {
|
|
88
|
+
return this.request("POST", e, { body: r });
|
|
89
|
+
}
|
|
90
|
+
patch(e, r) {
|
|
91
|
+
return this.request("PATCH", e, { body: r });
|
|
92
|
+
}
|
|
93
|
+
del(e) {
|
|
94
|
+
return this.request("DELETE", e);
|
|
95
|
+
}
|
|
96
|
+
/** For SSE streaming — returns raw Response (no envelope unwrap) */
|
|
97
|
+
async streamPost(e, r, t) {
|
|
98
|
+
if ((!this.appId || !this.token) && !await this.handleTokenRefresh())
|
|
99
|
+
throw new Error("SDK token not available: POST /token failed");
|
|
100
|
+
const o = `${this.baseUrl}${e}`, n = await fetch(o, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: this.headers(),
|
|
103
|
+
body: JSON.stringify(r),
|
|
104
|
+
signal: t
|
|
105
|
+
});
|
|
106
|
+
return n.status === 401 && await this.handleTokenRefresh() ? fetch(o, {
|
|
107
|
+
method: "POST",
|
|
108
|
+
headers: this.headers(),
|
|
109
|
+
body: JSON.stringify(r),
|
|
110
|
+
signal: t
|
|
111
|
+
}) : n;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function f(s) {
|
|
115
|
+
const e = s.split(`
|
|
116
|
+
`).map((t) => t.trim()).filter((t) => t.startsWith("data:")).map((t) => t.slice(5).trim());
|
|
117
|
+
if (!e.length) return null;
|
|
118
|
+
let r;
|
|
119
|
+
try {
|
|
120
|
+
r = JSON.parse(e.join(`
|
|
121
|
+
`));
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
switch (r.type) {
|
|
126
|
+
case "user":
|
|
127
|
+
return { type: "user", content: r.content ?? "" };
|
|
128
|
+
case "thinking":
|
|
129
|
+
return { type: "thinking", content: r.content ?? "" };
|
|
130
|
+
case "ai":
|
|
131
|
+
return { type: "ai", content: r.content ?? "" };
|
|
132
|
+
case "tool_call":
|
|
133
|
+
return {
|
|
134
|
+
type: "tool_call",
|
|
135
|
+
content: r.name ?? "",
|
|
136
|
+
toolName: r.name,
|
|
137
|
+
toolCallId: r.id,
|
|
138
|
+
args: typeof r.args == "string" ? r.args : JSON.stringify(r.args ?? {})
|
|
139
|
+
};
|
|
140
|
+
case "tool_result":
|
|
141
|
+
return {
|
|
142
|
+
type: "tool_result",
|
|
143
|
+
content: r.output ?? "",
|
|
144
|
+
toolName: r.name,
|
|
145
|
+
toolCallId: r.id
|
|
146
|
+
};
|
|
147
|
+
case "done":
|
|
148
|
+
return {
|
|
149
|
+
type: "done",
|
|
150
|
+
content: r.content ?? "",
|
|
151
|
+
// 后端 SSE 返回 session_id (snake_case),此处统一映射为 sessionId
|
|
152
|
+
sessionId: r.sessionId ?? r.session_id ?? ""
|
|
153
|
+
};
|
|
154
|
+
case "error":
|
|
155
|
+
return { type: "error", content: r.message ?? "Unknown error" };
|
|
156
|
+
default:
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async function y(s, e, r) {
|
|
161
|
+
if (!s.body) {
|
|
162
|
+
r(new Error("Response body is null"));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const t = s.body.getReader(), o = new TextDecoder();
|
|
166
|
+
let n = "";
|
|
167
|
+
try {
|
|
168
|
+
for (; ; ) {
|
|
169
|
+
const { value: a, done: c } = await t.read();
|
|
170
|
+
if (c) break;
|
|
171
|
+
n += o.decode(a, { stream: !0 });
|
|
172
|
+
let i;
|
|
173
|
+
for (; (i = n.indexOf(`
|
|
174
|
+
|
|
175
|
+
`)) !== -1; ) {
|
|
176
|
+
const l = n.slice(0, i);
|
|
177
|
+
n = n.slice(i + 2);
|
|
178
|
+
const d = f(l);
|
|
179
|
+
d && e(d);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (n.trim()) {
|
|
183
|
+
const a = f(n);
|
|
184
|
+
a && e(a);
|
|
185
|
+
}
|
|
186
|
+
} catch (a) {
|
|
187
|
+
(a == null ? void 0 : a.name) !== "AbortError" && r(a instanceof Error ? a : new Error(String(a)));
|
|
188
|
+
} finally {
|
|
189
|
+
try {
|
|
190
|
+
t.releaseLock();
|
|
191
|
+
} catch {
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function m(s, e) {
|
|
196
|
+
const r = new AbortController(), t = e.signal ? I(e.signal, r.signal) : r.signal, o = {
|
|
197
|
+
botId: s.getBotId(),
|
|
198
|
+
message: e.message,
|
|
199
|
+
sessionId: e.sessionId,
|
|
200
|
+
stream: e.stream !== !1
|
|
201
|
+
};
|
|
202
|
+
return o.stream ? s.streamPost("/chat", o, t).then((n) => {
|
|
203
|
+
var a;
|
|
204
|
+
if (!n.ok) {
|
|
205
|
+
(a = e.onError) == null || a.call(e, new Error(`HTTP ${n.status}`));
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
return y(
|
|
209
|
+
n,
|
|
210
|
+
(c) => {
|
|
211
|
+
var i, l, d, u;
|
|
212
|
+
c.type === "done" && c.sessionId ? (i = e.onDone) == null || i.call(e, { sessionId: c.sessionId, content: c.content }) : c.type === "done" ? (l = e.onDone) == null || l.call(e, { sessionId: "", content: c.content }) : c.type === "error" ? (d = e.onError) == null || d.call(e, new Error(c.content)) : (u = e.onMessage) == null || u.call(e, c);
|
|
213
|
+
},
|
|
214
|
+
(c) => {
|
|
215
|
+
var i;
|
|
216
|
+
return (i = e.onError) == null ? void 0 : i.call(e, c);
|
|
217
|
+
}
|
|
218
|
+
);
|
|
219
|
+
}).catch((n) => {
|
|
220
|
+
var a;
|
|
221
|
+
(n == null ? void 0 : n.name) !== "AbortError" && ((a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n))));
|
|
222
|
+
}) : s.post("/chat", o).then((n) => {
|
|
223
|
+
var a;
|
|
224
|
+
(a = e.onDone) == null || a.call(e, {
|
|
225
|
+
sessionId: n.sessionId,
|
|
226
|
+
content: n.content ?? ""
|
|
227
|
+
});
|
|
228
|
+
}).catch((n) => {
|
|
229
|
+
var a;
|
|
230
|
+
(a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n)));
|
|
231
|
+
}), r;
|
|
232
|
+
}
|
|
233
|
+
function I(s, e) {
|
|
234
|
+
const r = new AbortController(), t = () => r.abort();
|
|
235
|
+
return s.addEventListener("abort", t, { once: !0 }), e.addEventListener("abort", t, { once: !0 }), (s.aborted || e.aborted) && r.abort(), r.signal;
|
|
236
|
+
}
|
|
237
|
+
async function w(s) {
|
|
238
|
+
return (await s.post("/chat/sessions", {
|
|
239
|
+
botId: s.getBotId()
|
|
240
|
+
})).sessionId;
|
|
241
|
+
}
|
|
242
|
+
function b(s) {
|
|
243
|
+
return {
|
|
244
|
+
// 后端返回 thread_id,toCamelCase 转为 threadId,映射为 sessionId
|
|
245
|
+
sessionId: s.threadId ?? s.sessionId,
|
|
246
|
+
botId: s.botId,
|
|
247
|
+
title: s.title,
|
|
248
|
+
createTime: s.createTime,
|
|
249
|
+
updateTime: s.updateTime
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
async function k(s, e) {
|
|
253
|
+
const r = await s.get("/chat/conversations", {
|
|
254
|
+
botId: s.getBotId(),
|
|
255
|
+
page: (e == null ? void 0 : e.page) ?? 1,
|
|
256
|
+
size: (e == null ? void 0 : e.size) ?? 20
|
|
257
|
+
});
|
|
258
|
+
return {
|
|
259
|
+
...r,
|
|
260
|
+
items: r.items.map(b)
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
async function T(s, e, r) {
|
|
264
|
+
await s.patch(`/chat/conversations/${e}`, { title: r });
|
|
265
|
+
}
|
|
266
|
+
async function S(s, e) {
|
|
267
|
+
await s.del(`/chat/conversations/${e}`);
|
|
268
|
+
}
|
|
269
|
+
async function E(s, e) {
|
|
270
|
+
const r = await s.get(`/chat/conversations/${e}/messages`);
|
|
271
|
+
return C(r);
|
|
272
|
+
}
|
|
273
|
+
function C(s) {
|
|
274
|
+
const e = [], r = [...s].sort((t, o) => (t.seq ?? 0) - (o.seq ?? 0));
|
|
275
|
+
for (const t of r) {
|
|
276
|
+
const o = t.role;
|
|
277
|
+
if (o === "system") continue;
|
|
278
|
+
const n = t.messageId ?? `msg_${t.id}`, a = new Date(t.createTime ?? "").getTime() || Date.now();
|
|
279
|
+
if (o === "human")
|
|
280
|
+
e.push({
|
|
281
|
+
id: n,
|
|
282
|
+
role: "user",
|
|
283
|
+
parts: [{ type: "text", content: t.content ?? "" }],
|
|
284
|
+
timestamp: a
|
|
285
|
+
});
|
|
286
|
+
else if (o === "ai") {
|
|
287
|
+
const c = [];
|
|
288
|
+
t.reasoning && c.push({ type: "thinking", content: t.reasoning }), t.content && c.push({ type: "text", content: t.content });
|
|
289
|
+
const i = t.toolCalls;
|
|
290
|
+
if (i && Array.isArray(i))
|
|
291
|
+
for (const l of i) {
|
|
292
|
+
let d;
|
|
293
|
+
try {
|
|
294
|
+
d = typeof l.args == "string" ? l.args : JSON.stringify(l.args ?? {});
|
|
295
|
+
} catch {
|
|
296
|
+
d = String(l.args ?? "{}");
|
|
297
|
+
}
|
|
298
|
+
c.push({
|
|
299
|
+
type: "tool_call",
|
|
300
|
+
toolName: l.name ?? "",
|
|
301
|
+
toolCallId: l.id ?? "",
|
|
302
|
+
args: d
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
e.push({ id: n, role: "assistant", parts: c, timestamp: a });
|
|
306
|
+
} else o === "tool" && e.push({
|
|
307
|
+
id: n,
|
|
308
|
+
role: "assistant",
|
|
309
|
+
parts: [{
|
|
310
|
+
type: "tool_result",
|
|
311
|
+
toolName: t.toolName ?? "",
|
|
312
|
+
toolCallId: t.toolCallId ?? "",
|
|
313
|
+
content: t.content ?? ""
|
|
314
|
+
}],
|
|
315
|
+
timestamp: a
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
return e;
|
|
319
|
+
}
|
|
320
|
+
function $(s) {
|
|
321
|
+
const e = new g(s), r = {
|
|
322
|
+
getToken: async () => {
|
|
323
|
+
const t = await e.postPublic(
|
|
324
|
+
"/token",
|
|
325
|
+
{ botId: e.getBotId() }
|
|
326
|
+
);
|
|
327
|
+
return e.setToken(t.appId, t.token), t;
|
|
328
|
+
},
|
|
329
|
+
createSession: () => w(e),
|
|
330
|
+
chat: (t) => m(e, t),
|
|
331
|
+
listConversations: (t) => k(e, t),
|
|
332
|
+
renameConversation: (t, o) => T(e, t, o),
|
|
333
|
+
deleteConversation: (t) => S(e, t),
|
|
334
|
+
getMessages: (t) => E(e, t),
|
|
335
|
+
setToken: (t, o) => e.setToken(t, o),
|
|
336
|
+
feedback: (t, o) => {
|
|
337
|
+
var n;
|
|
338
|
+
(n = r.onFeedback) == null || n.call(r, t, o);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
return r;
|
|
342
|
+
}
|
|
343
|
+
export {
|
|
344
|
+
$ as createSuperAgent
|
|
345
|
+
};
|