super-agent-sdk 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -18
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +7 -2
- package/dist/index.mjs +90 -85
- package/dist/widget.cjs +51 -8
- package/dist/widget.d.ts +11 -4
- package/dist/widget.mjs +1256 -1065
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ function u(r) {
|
|
|
3
3
|
if (r && typeof r == "object" && r.constructor === Object) {
|
|
4
4
|
const e = {};
|
|
5
5
|
for (const [t, s] of Object.entries(r)) {
|
|
6
|
-
const o = t.replace(/_([a-z])/g, (
|
|
6
|
+
const o = t.replace(/_([a-z])/g, (a, n) => n.toUpperCase());
|
|
7
7
|
e[o] = u(s);
|
|
8
8
|
}
|
|
9
9
|
return e;
|
|
@@ -42,8 +42,8 @@ class w {
|
|
|
42
42
|
const o = await s.json();
|
|
43
43
|
if (o.code !== "200")
|
|
44
44
|
throw new Error(o.message || `API error: ${o.code}`);
|
|
45
|
-
const
|
|
46
|
-
return this.token =
|
|
45
|
+
const a = u(o.data);
|
|
46
|
+
return this.token = a.token, a.appId && !this.appId && (this.appId = a.appId), a.token;
|
|
47
47
|
}
|
|
48
48
|
headers(e) {
|
|
49
49
|
return {
|
|
@@ -77,19 +77,19 @@ class w {
|
|
|
77
77
|
const c = i.toString();
|
|
78
78
|
c && (o += `?${c}`);
|
|
79
79
|
}
|
|
80
|
-
const
|
|
80
|
+
const a = await fetch(o, {
|
|
81
81
|
method: e,
|
|
82
82
|
headers: this.headers(),
|
|
83
83
|
body: s != null && s.body ? JSON.stringify(s.body) : void 0
|
|
84
84
|
});
|
|
85
|
-
if (
|
|
85
|
+
if (a.status === 401 && !(s != null && s.retry) && await this.handleTokenRefresh())
|
|
86
86
|
return this.request(e, t, { ...s, retry: !0 });
|
|
87
|
-
if (!
|
|
88
|
-
throw new Error(`HTTP ${
|
|
89
|
-
const
|
|
90
|
-
if (
|
|
91
|
-
throw new Error(
|
|
92
|
-
return u(
|
|
87
|
+
if (!a.ok)
|
|
88
|
+
throw new Error(`HTTP ${a.status}: ${a.statusText}`);
|
|
89
|
+
const n = await a.json();
|
|
90
|
+
if (n.code !== "200")
|
|
91
|
+
throw new Error(n.message || `API error: ${n.code}`);
|
|
92
|
+
return u(n.data);
|
|
93
93
|
}
|
|
94
94
|
get(e, t) {
|
|
95
95
|
return this.request("GET", e, { params: t });
|
|
@@ -107,18 +107,18 @@ class w {
|
|
|
107
107
|
async streamPost(e, t, s) {
|
|
108
108
|
if (!this.token && !await this.handleTokenRefresh())
|
|
109
109
|
throw new Error("SDK token not available: POST /token failed");
|
|
110
|
-
const o = this.isAbsoluteUrl ? `${this.baseUrl}${e}` : `${this.getOrigin()}${this.baseUrl}${e}`,
|
|
110
|
+
const o = this.isAbsoluteUrl ? `${this.baseUrl}${e}` : `${this.getOrigin()}${this.baseUrl}${e}`, a = await fetch(o, {
|
|
111
111
|
method: "POST",
|
|
112
112
|
headers: this.headers(),
|
|
113
113
|
body: JSON.stringify(t),
|
|
114
114
|
signal: s
|
|
115
115
|
});
|
|
116
|
-
return
|
|
116
|
+
return a.status === 401 && await this.handleTokenRefresh() ? fetch(o, {
|
|
117
117
|
method: "POST",
|
|
118
118
|
headers: this.headers(),
|
|
119
119
|
body: JSON.stringify(t),
|
|
120
120
|
signal: s
|
|
121
|
-
}) :
|
|
121
|
+
}) : a;
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
function y(r) {
|
|
@@ -170,7 +170,8 @@ function y(r) {
|
|
|
170
170
|
return {
|
|
171
171
|
type: "stop",
|
|
172
172
|
content: t.content ?? "",
|
|
173
|
-
sessionId: t.sessionId ?? t.session_id ?? ""
|
|
173
|
+
sessionId: t.sessionId ?? t.session_id ?? "",
|
|
174
|
+
messageId: t.messageId ?? t.message_id
|
|
174
175
|
};
|
|
175
176
|
case "error":
|
|
176
177
|
return { type: "error", content: t.message ?? "Unknown error" };
|
|
@@ -201,28 +202,28 @@ async function m(r, e, t) {
|
|
|
201
202
|
return;
|
|
202
203
|
}
|
|
203
204
|
const s = r.body.getReader(), o = new TextDecoder();
|
|
204
|
-
let
|
|
205
|
+
let a = "";
|
|
205
206
|
try {
|
|
206
207
|
for (; ; ) {
|
|
207
|
-
const { value:
|
|
208
|
+
const { value: n, done: i } = await s.read();
|
|
208
209
|
if (i) break;
|
|
209
|
-
|
|
210
|
+
a += o.decode(n, { stream: !0 });
|
|
210
211
|
let c;
|
|
211
|
-
for (; (c =
|
|
212
|
+
for (; (c = a.indexOf(`
|
|
212
213
|
|
|
213
214
|
`)) !== -1; ) {
|
|
214
|
-
const d =
|
|
215
|
-
|
|
215
|
+
const d = a.slice(0, c);
|
|
216
|
+
a = a.slice(c + 2);
|
|
216
217
|
const l = y(d);
|
|
217
218
|
l && e(l);
|
|
218
219
|
}
|
|
219
220
|
}
|
|
220
|
-
if (
|
|
221
|
-
const
|
|
222
|
-
|
|
221
|
+
if (a.trim()) {
|
|
222
|
+
const n = y(a);
|
|
223
|
+
n && e(n);
|
|
223
224
|
}
|
|
224
|
-
} catch (
|
|
225
|
-
(
|
|
225
|
+
} catch (n) {
|
|
226
|
+
(n == null ? void 0 : n.name) !== "AbortError" && t(n instanceof Error ? n : new Error(String(n)));
|
|
226
227
|
} finally {
|
|
227
228
|
try {
|
|
228
229
|
s.releaseLock();
|
|
@@ -231,45 +232,45 @@ async function m(r, e, t) {
|
|
|
231
232
|
}
|
|
232
233
|
}
|
|
233
234
|
function p(r, e) {
|
|
234
|
-
const t = new AbortController(), s = e.signal ?
|
|
235
|
+
const t = new AbortController(), s = e.signal ? T(e.signal, t.signal) : t.signal, o = {
|
|
235
236
|
botId: r.getBotId(),
|
|
236
237
|
message: e.message,
|
|
237
238
|
sessionId: e.sessionId,
|
|
238
239
|
stream: e.stream !== !1
|
|
239
240
|
};
|
|
240
|
-
return o.stream ? r.streamPost("/chat", o, s).then((
|
|
241
|
-
var
|
|
242
|
-
if (!
|
|
243
|
-
(
|
|
241
|
+
return o.stream ? r.streamPost("/chat", o, s).then((a) => {
|
|
242
|
+
var n;
|
|
243
|
+
if (!a.ok) {
|
|
244
|
+
(n = e.onError) == null || n.call(e, new Error(`HTTP ${a.status}`));
|
|
244
245
|
return;
|
|
245
246
|
}
|
|
246
247
|
return m(
|
|
247
|
-
|
|
248
|
+
a,
|
|
248
249
|
(i) => {
|
|
249
250
|
var c, d, l, h, f, g;
|
|
250
|
-
i.type === "done" && i.sessionId ? (c = e.onDone) == null || c.call(e, { sessionId: i.sessionId, content: i.content, messageId: i.messageId }) : i.type === "done" ? (d = e.onDone) == null || d.call(e, { sessionId: "", content: i.content, messageId: i.messageId }) : i.type === "stop" ? ((l = e.onMessage) == null || l.call(e, i), (h = e.onDone) == null || h.call(e, { sessionId: i.sessionId ?? "", content: i.content })) : i.type === "error" ? (f = e.onError) == null || f.call(e, new Error(i.content)) : (g = e.onMessage) == null || g.call(e, i);
|
|
251
|
+
i.type === "done" && i.sessionId ? (c = e.onDone) == null || c.call(e, { sessionId: i.sessionId, content: i.content, messageId: i.messageId }) : i.type === "done" ? (d = e.onDone) == null || d.call(e, { sessionId: "", content: i.content, messageId: i.messageId }) : i.type === "stop" ? ((l = e.onMessage) == null || l.call(e, i), (h = e.onDone) == null || h.call(e, { sessionId: i.sessionId ?? "", content: i.content, messageId: i.messageId })) : i.type === "error" ? (f = e.onError) == null || f.call(e, new Error(i.content)) : (g = e.onMessage) == null || g.call(e, i);
|
|
251
252
|
},
|
|
252
253
|
(i) => {
|
|
253
254
|
var c;
|
|
254
255
|
return (c = e.onError) == null ? void 0 : c.call(e, i);
|
|
255
256
|
}
|
|
256
257
|
);
|
|
257
|
-
}).catch((
|
|
258
|
-
var
|
|
259
|
-
(
|
|
260
|
-
}) : r.post("/chat", o).then((
|
|
261
|
-
var
|
|
262
|
-
(
|
|
263
|
-
sessionId:
|
|
264
|
-
content:
|
|
265
|
-
messageId:
|
|
258
|
+
}).catch((a) => {
|
|
259
|
+
var n;
|
|
260
|
+
(a == null ? void 0 : a.name) !== "AbortError" && ((n = e.onError) == null || n.call(e, a instanceof Error ? a : new Error(String(a))));
|
|
261
|
+
}) : r.post("/chat", o).then((a) => {
|
|
262
|
+
var n;
|
|
263
|
+
(n = e.onDone) == null || n.call(e, {
|
|
264
|
+
sessionId: a.sessionId,
|
|
265
|
+
content: a.content ?? "",
|
|
266
|
+
messageId: a.messageId
|
|
266
267
|
});
|
|
267
|
-
}).catch((
|
|
268
|
-
var
|
|
269
|
-
(
|
|
268
|
+
}).catch((a) => {
|
|
269
|
+
var n;
|
|
270
|
+
(n = e.onError) == null || n.call(e, a instanceof Error ? a : new Error(String(a)));
|
|
270
271
|
}), t;
|
|
271
272
|
}
|
|
272
|
-
function
|
|
273
|
+
function T(r, e) {
|
|
273
274
|
const t = new AbortController(), s = () => t.abort();
|
|
274
275
|
return r.addEventListener("abort", s, { once: !0 }), e.addEventListener("abort", s, { once: !0 }), (r.aborted || e.aborted) && t.abort(), t.signal;
|
|
275
276
|
}
|
|
@@ -278,7 +279,7 @@ async function b(r) {
|
|
|
278
279
|
botId: r.getBotId()
|
|
279
280
|
})).sessionId;
|
|
280
281
|
}
|
|
281
|
-
function
|
|
282
|
+
function k(r) {
|
|
282
283
|
return {
|
|
283
284
|
// 后端返回 thread_id,toCamelCase 转为 threadId,映射为 sessionId
|
|
284
285
|
sessionId: r.threadId ?? r.sessionId,
|
|
@@ -296,7 +297,7 @@ async function S(r, e) {
|
|
|
296
297
|
});
|
|
297
298
|
return {
|
|
298
299
|
...t,
|
|
299
|
-
items: t.items.map(
|
|
300
|
+
items: t.items.map(k)
|
|
300
301
|
};
|
|
301
302
|
}
|
|
302
303
|
async function $(r, e, t) {
|
|
@@ -310,40 +311,43 @@ async function A(r, e) {
|
|
|
310
311
|
return O(t);
|
|
311
312
|
}
|
|
312
313
|
function O(r) {
|
|
313
|
-
const e = [...r].sort((
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
314
|
+
const e = [...r].sort((n, i) => {
|
|
315
|
+
const c = new Date(n.createTime ?? 0).getTime() || 0, d = new Date(i.createTime ?? 0).getTime() || 0;
|
|
316
|
+
return c !== d ? c - d : (n.seq ?? 0) - (i.seq ?? 0);
|
|
317
|
+
}), t = /* @__PURE__ */ new Map();
|
|
318
|
+
for (const n of e)
|
|
319
|
+
n.role === "tool" && n.toolCallId && t.set(n.toolCallId, n.content || "");
|
|
320
|
+
const s = /* @__PURE__ */ new Set(), o = [], a = (n, i, c) => {
|
|
317
321
|
const d = o[o.length - 1];
|
|
318
|
-
(d == null ? void 0 : d.role) === "assistant" ? (d.parts.push(...
|
|
322
|
+
(d == null ? void 0 : d.role) === "assistant" ? (d.parts.push(...n), d.id = i) : o.push({ id: i, role: "assistant", parts: [...n], timestamp: c });
|
|
319
323
|
};
|
|
320
|
-
for (const
|
|
321
|
-
if (
|
|
322
|
-
const i =
|
|
323
|
-
if (
|
|
324
|
-
o.push({ id: i, role: "user", parts: [{ type: "text", content:
|
|
324
|
+
for (const n of e) {
|
|
325
|
+
if (n.role === "system") continue;
|
|
326
|
+
const i = String(n.id ?? n.messageId ?? `msg_${Date.now()}`), c = new Date(n.createTime ?? "").getTime() || Date.now();
|
|
327
|
+
if (n.role === "human") {
|
|
328
|
+
o.push({ id: i, role: "user", parts: [{ type: "text", content: n.content ?? "" }], timestamp: c });
|
|
325
329
|
continue;
|
|
326
330
|
}
|
|
327
|
-
if (
|
|
328
|
-
const d =
|
|
329
|
-
d.length > 0 &&
|
|
331
|
+
if (n.role === "ai") {
|
|
332
|
+
const d = _(n, t, s);
|
|
333
|
+
d.length > 0 && a(d, i, c);
|
|
330
334
|
continue;
|
|
331
335
|
}
|
|
332
|
-
if (
|
|
333
|
-
if (
|
|
334
|
-
|
|
336
|
+
if (n.role === "tool") {
|
|
337
|
+
if (n.toolCallId && s.has(n.toolCallId)) continue;
|
|
338
|
+
a([v(n)], i, c);
|
|
335
339
|
}
|
|
336
340
|
}
|
|
337
341
|
return o;
|
|
338
342
|
}
|
|
339
|
-
function
|
|
343
|
+
function _(r, e, t) {
|
|
340
344
|
const s = [];
|
|
341
345
|
r.reasoning && s.push({ type: "thinking", content: r.reasoning }), r.content && s.push({ type: "text", content: r.content });
|
|
342
346
|
for (const o of r.toolCalls ?? [])
|
|
343
|
-
o.is_interrupt ? (t.add(o.id), s.push(
|
|
347
|
+
o.is_interrupt ? (t.add(o.id), s.push(P(o, e))) : s.push(E(o));
|
|
344
348
|
return s;
|
|
345
349
|
}
|
|
346
|
-
function
|
|
350
|
+
function P(r, e) {
|
|
347
351
|
const t = e.has(r.id), s = typeof r.args == "object" ? r.args : {};
|
|
348
352
|
return {
|
|
349
353
|
type: "interrupt",
|
|
@@ -393,8 +397,8 @@ function x(r) {
|
|
|
393
397
|
return r;
|
|
394
398
|
}
|
|
395
399
|
}
|
|
396
|
-
async function U(r, e, t, s, o,
|
|
397
|
-
const
|
|
400
|
+
async function U(r, e, t, s, o, a) {
|
|
401
|
+
const n = await r.streamPost(
|
|
398
402
|
`/chat/interrupt/${e}/respond`,
|
|
399
403
|
{
|
|
400
404
|
sessionId: t,
|
|
@@ -402,27 +406,28 @@ async function U(r, e, t, s, o, n) {
|
|
|
402
406
|
value: s.value
|
|
403
407
|
}
|
|
404
408
|
);
|
|
405
|
-
if (!
|
|
406
|
-
const i = new Error(`HTTP ${
|
|
407
|
-
throw
|
|
409
|
+
if (!n.ok) {
|
|
410
|
+
const i = new Error(`HTTP ${n.status}`);
|
|
411
|
+
throw a == null || a(i), i;
|
|
408
412
|
}
|
|
409
413
|
o && await m(
|
|
410
|
-
|
|
414
|
+
n,
|
|
411
415
|
(i) => {
|
|
412
|
-
i.type === "error" ?
|
|
416
|
+
i.type === "error" ? a == null || a(new Error(i.content)) : o(i);
|
|
413
417
|
},
|
|
414
|
-
(i) =>
|
|
418
|
+
(i) => a == null ? void 0 : a(i)
|
|
415
419
|
);
|
|
416
420
|
}
|
|
417
|
-
async function
|
|
421
|
+
async function D(r) {
|
|
418
422
|
return r.get(`/chat/bots/${r.getBotId()}/skills`);
|
|
419
423
|
}
|
|
420
|
-
async function
|
|
424
|
+
async function q(r, e) {
|
|
421
425
|
await r.post("/chat/stop", { sessionId: e });
|
|
422
426
|
}
|
|
423
|
-
function
|
|
427
|
+
function N(r) {
|
|
424
428
|
const e = new w(r);
|
|
425
429
|
return {
|
|
430
|
+
botId: e.getBotId(),
|
|
426
431
|
getToken: () => e.fetchToken(),
|
|
427
432
|
createSession: () => b(e),
|
|
428
433
|
chat: (s) => p(e, s),
|
|
@@ -430,19 +435,19 @@ function L(r) {
|
|
|
430
435
|
renameConversation: (s, o) => $(e, s, o),
|
|
431
436
|
deleteConversation: (s) => C(e, s),
|
|
432
437
|
getMessages: (s) => A(e, s),
|
|
433
|
-
respondInterrupt: (s, o,
|
|
438
|
+
respondInterrupt: (s, o, a, n, i) => U(e, s, o, a, n, i),
|
|
434
439
|
setToken: (s) => e.setToken(s),
|
|
435
|
-
feedback: async (s, o,
|
|
436
|
-
const
|
|
437
|
-
o === "dislike" && ((
|
|
440
|
+
feedback: async (s, o, a) => {
|
|
441
|
+
const n = { type: o };
|
|
442
|
+
o === "dislike" && ((a == null ? void 0 : a.reason) != null && (n.reason = a.reason), a != null && a.remark && (n.remark = a.remark)), await e.post(`/chat/messages/${s}/feedback`, n);
|
|
438
443
|
},
|
|
439
444
|
cancelFeedback: async (s) => {
|
|
440
445
|
await e.del(`/chat/messages/${s}/feedback`);
|
|
441
446
|
},
|
|
442
|
-
listSkills: () =>
|
|
443
|
-
stopGeneration: (s) =>
|
|
447
|
+
listSkills: () => D(e),
|
|
448
|
+
stopGeneration: (s) => q(e, s)
|
|
444
449
|
};
|
|
445
450
|
}
|
|
446
451
|
export {
|
|
447
|
-
|
|
452
|
+
N as createSuperAgent
|
|
448
453
|
};
|