super-agent-sdk 1.0.20 → 1.0.22
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 -9
- package/dist/index.cjs +4 -4
- package/dist/index.d.ts +12 -2
- package/dist/index.mjs +302 -284
- package/dist/{mammoth.browser-CxQENq5Q.cjs → mammoth.browser-CKp13R4W.cjs} +1 -1
- package/dist/{mammoth.browser-xkqZbK20.js → mammoth.browser-_e1UoKEX.js} +1 -1
- package/dist/{nosUploader-DjJ7u2B1.js → nosUploader-2VXYPLap.js} +2 -1
- package/dist/{nosUploader-DLX7JsnR.cjs → nosUploader-Dk2GyRpT.cjs} +4 -4
- package/dist/widget.cjs +349 -29
- package/dist/widget.d.ts +83 -13
- package/dist/widget.mjs +28105 -5533
- package/package.json +3 -1
package/dist/index.mjs
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { p as b, s as C } from "./nosUploader-
|
|
2
|
-
import { c as W } from "./nosUploader-
|
|
3
|
-
function
|
|
4
|
-
if (Array.isArray(
|
|
5
|
-
if (
|
|
1
|
+
import { p as b, s as C } from "./nosUploader-2VXYPLap.js";
|
|
2
|
+
import { c as W } from "./nosUploader-2VXYPLap.js";
|
|
3
|
+
function g(r) {
|
|
4
|
+
if (Array.isArray(r)) return r.map((e) => g(e));
|
|
5
|
+
if (r && typeof r == "object" && r.constructor === Object) {
|
|
6
6
|
const e = {};
|
|
7
|
-
for (const [
|
|
8
|
-
const d =
|
|
9
|
-
e[d] =
|
|
7
|
+
for (const [o, s] of Object.entries(r)) {
|
|
8
|
+
const d = o.replace(/_([a-z])/g, (t, n) => n.toUpperCase());
|
|
9
|
+
e[d] = g(s);
|
|
10
10
|
}
|
|
11
11
|
return e;
|
|
12
12
|
}
|
|
13
|
-
return
|
|
13
|
+
return r;
|
|
14
14
|
}
|
|
15
15
|
const S = "/api/agent/runtime/v1";
|
|
16
16
|
class A {
|
|
17
17
|
constructor(e) {
|
|
18
18
|
this.refreshing = null, this.tokenPromise = null;
|
|
19
|
-
const
|
|
20
|
-
this.baseUrl =
|
|
19
|
+
const o = e.baseUrl ?? S;
|
|
20
|
+
this.baseUrl = o.replace(/\/$/, ""), this.tokenGateway = e.tokenGateway.replace(/\/$/, ""), this.appId = e.appId ?? "", this.token = e.token ?? "", this.botCode = e.botCode, this.tenantId = e.tenantId, this.isAbsoluteUrl = /^https?:\/\//.test(this.baseUrl), this.isAbsoluteGateway = /^https?:\/\//.test(this.tokenGateway), this.loginUrl = e.loginUrl, this.onAuthExpired = e.onAuthExpired;
|
|
21
21
|
}
|
|
22
22
|
getBotCode() {
|
|
23
23
|
return this.botCode;
|
|
@@ -42,13 +42,13 @@ class A {
|
|
|
42
42
|
if (this.token) return this.token;
|
|
43
43
|
if (this.tokenPromise) return this.tokenPromise;
|
|
44
44
|
this.tokenPromise = (async () => {
|
|
45
|
-
const
|
|
45
|
+
const o = `${this.isAbsoluteGateway ? this.tokenGateway : `${this.getOrigin()}${this.tokenGateway}`}/superagent/v2/token/generate`, s = {
|
|
46
46
|
"Content-Type": "application/json"
|
|
47
47
|
};
|
|
48
|
-
this.tenantId != null && (
|
|
49
|
-
const d = await fetch(
|
|
48
|
+
this.tenantId != null && (s["X-Tenant-Id"] = String(this.tenantId));
|
|
49
|
+
const d = await fetch(o, {
|
|
50
50
|
method: "POST",
|
|
51
|
-
headers:
|
|
51
|
+
headers: s,
|
|
52
52
|
body: JSON.stringify({ botCode: this.botCode })
|
|
53
53
|
});
|
|
54
54
|
if (!d.ok)
|
|
@@ -56,8 +56,8 @@ class A {
|
|
|
56
56
|
const t = await d.json();
|
|
57
57
|
if (t.code !== "200")
|
|
58
58
|
throw new Error(t.message || `API error: ${t.code}`);
|
|
59
|
-
const
|
|
60
|
-
return this.token =
|
|
59
|
+
const n = g(t.data);
|
|
60
|
+
return this.token = n.token, n.appId && !this.appId && (this.appId = n.appId), n.token;
|
|
61
61
|
})();
|
|
62
62
|
try {
|
|
63
63
|
return await this.tokenPromise;
|
|
@@ -86,109 +86,109 @@ class A {
|
|
|
86
86
|
this.refreshing = null;
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
|
-
async request(e,
|
|
89
|
+
async request(e, o, s) {
|
|
90
90
|
if (!this.token && !await this.handleTokenRefresh())
|
|
91
91
|
throw new Error("SDK token not available: POST /token failed");
|
|
92
|
-
let d = this.isAbsoluteUrl ? `${this.baseUrl}${
|
|
93
|
-
if (
|
|
94
|
-
const
|
|
95
|
-
for (const [c, l] of Object.entries(
|
|
96
|
-
l != null &&
|
|
97
|
-
const
|
|
98
|
-
|
|
92
|
+
let d = this.isAbsoluteUrl ? `${this.baseUrl}${o}` : `${this.getOrigin()}${this.baseUrl}${o}`;
|
|
93
|
+
if (s != null && s.params) {
|
|
94
|
+
const a = new URLSearchParams();
|
|
95
|
+
for (const [c, l] of Object.entries(s.params))
|
|
96
|
+
l != null && a.set(c, String(l));
|
|
97
|
+
const i = a.toString();
|
|
98
|
+
i && (d += `?${i}`);
|
|
99
99
|
}
|
|
100
100
|
const t = await fetch(d, {
|
|
101
101
|
method: e,
|
|
102
102
|
headers: this.headers(),
|
|
103
|
-
body:
|
|
103
|
+
body: s != null && s.body ? JSON.stringify(s.body) : void 0
|
|
104
104
|
});
|
|
105
|
-
if (t.status === 401 && !(
|
|
106
|
-
return this.request(e,
|
|
105
|
+
if (t.status === 401 && !(s != null && s.retry) && await this.handleTokenRefresh())
|
|
106
|
+
return this.request(e, o, { ...s, retry: !0 });
|
|
107
107
|
if (!t.ok) {
|
|
108
|
-
let
|
|
108
|
+
let a = `HTTP ${t.status}: ${t.statusText}`;
|
|
109
109
|
try {
|
|
110
|
-
const
|
|
111
|
-
|
|
110
|
+
const i = await t.json();
|
|
111
|
+
i != null && i.message && (a = i.message);
|
|
112
112
|
} catch {
|
|
113
113
|
}
|
|
114
|
-
throw new Error(
|
|
114
|
+
throw new Error(a);
|
|
115
115
|
}
|
|
116
|
-
const
|
|
117
|
-
if (
|
|
118
|
-
throw new Error(
|
|
119
|
-
return
|
|
116
|
+
const n = await t.json();
|
|
117
|
+
if (n.code !== "200")
|
|
118
|
+
throw new Error(n.message || `API error: ${n.code}`);
|
|
119
|
+
return g(n.data);
|
|
120
120
|
}
|
|
121
|
-
get(e,
|
|
122
|
-
return this.request("GET", e, { params:
|
|
121
|
+
get(e, o) {
|
|
122
|
+
return this.request("GET", e, { params: o });
|
|
123
123
|
}
|
|
124
|
-
post(e,
|
|
125
|
-
return this.request("POST", e, { body:
|
|
124
|
+
post(e, o) {
|
|
125
|
+
return this.request("POST", e, { body: o });
|
|
126
126
|
}
|
|
127
|
-
patch(e,
|
|
128
|
-
return this.request("PATCH", e, { body:
|
|
127
|
+
patch(e, o) {
|
|
128
|
+
return this.request("PATCH", e, { body: o });
|
|
129
129
|
}
|
|
130
130
|
del(e) {
|
|
131
131
|
return this.request("DELETE", e);
|
|
132
132
|
}
|
|
133
133
|
/** For SSE streaming — returns raw Response (no envelope unwrap) */
|
|
134
|
-
async streamPost(e,
|
|
134
|
+
async streamPost(e, o, s) {
|
|
135
135
|
if (!this.token && !await this.handleTokenRefresh())
|
|
136
136
|
throw new Error("SDK token not available: POST /token failed");
|
|
137
137
|
const d = this.isAbsoluteUrl ? `${this.baseUrl}${e}` : `${this.getOrigin()}${this.baseUrl}${e}`, t = await fetch(d, {
|
|
138
138
|
method: "POST",
|
|
139
139
|
headers: this.headers(),
|
|
140
|
-
body: JSON.stringify(
|
|
141
|
-
signal:
|
|
140
|
+
body: JSON.stringify(o),
|
|
141
|
+
signal: s
|
|
142
142
|
});
|
|
143
143
|
if (t.status === 401) {
|
|
144
|
-
const
|
|
145
|
-
if (
|
|
144
|
+
const n = await b(t.clone());
|
|
145
|
+
if (n.isCookieExpired && typeof (o == null ? void 0 : o.message) == "string") {
|
|
146
146
|
C({
|
|
147
147
|
botCode: this.botCode,
|
|
148
|
-
message:
|
|
149
|
-
sessionId:
|
|
148
|
+
message: o.message,
|
|
149
|
+
sessionId: o.sessionId ?? ""
|
|
150
150
|
});
|
|
151
|
-
const
|
|
151
|
+
const i = n.loginUrl ?? this.loginUrl;
|
|
152
152
|
return this.onAuthExpired ? this.onAuthExpired({
|
|
153
|
-
loginUrl:
|
|
154
|
-
sessionId:
|
|
153
|
+
loginUrl: i,
|
|
154
|
+
sessionId: n.sessionId,
|
|
155
155
|
botCode: this.botCode
|
|
156
|
-
}) :
|
|
156
|
+
}) : i && window.location.replace(i), t;
|
|
157
157
|
}
|
|
158
158
|
if (await this.handleTokenRefresh())
|
|
159
159
|
return fetch(d, {
|
|
160
160
|
method: "POST",
|
|
161
161
|
headers: this.headers(),
|
|
162
|
-
body: JSON.stringify(
|
|
163
|
-
signal:
|
|
162
|
+
body: JSON.stringify(o),
|
|
163
|
+
signal: s
|
|
164
164
|
});
|
|
165
165
|
}
|
|
166
166
|
return t;
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
|
-
function
|
|
170
|
-
return typeof
|
|
169
|
+
function h(r) {
|
|
170
|
+
return typeof r == "string" ? r : Array.isArray(r) ? r.map((e) => {
|
|
171
171
|
if (typeof e == "string") return e;
|
|
172
172
|
if (e && typeof e == "object") {
|
|
173
173
|
if (typeof e.text == "string") return e.text;
|
|
174
174
|
if (e.type) return `[${e.type}${e.mime_type ? ": " + e.mime_type : ""}]`;
|
|
175
175
|
}
|
|
176
176
|
return "";
|
|
177
|
-
}).join("") :
|
|
177
|
+
}).join("") : r == null ? "" : String(r);
|
|
178
178
|
}
|
|
179
|
-
function w(
|
|
180
|
-
const e =
|
|
181
|
-
`).map((
|
|
182
|
-
let
|
|
183
|
-
for (const
|
|
184
|
-
if (
|
|
185
|
-
|
|
186
|
-
else if (
|
|
187
|
-
|
|
188
|
-
else if (
|
|
189
|
-
const
|
|
179
|
+
function w(r) {
|
|
180
|
+
const e = r.split(`
|
|
181
|
+
`).map((a) => a.trim()).filter((a) => a.length > 0);
|
|
182
|
+
let o, s, d;
|
|
183
|
+
for (const a of e)
|
|
184
|
+
if (a.startsWith("id:"))
|
|
185
|
+
o = a.slice(3).trim();
|
|
186
|
+
else if (a.startsWith("event:"))
|
|
187
|
+
s = a.slice(6).trim();
|
|
188
|
+
else if (a.startsWith("data:")) {
|
|
189
|
+
const i = a.slice(5).trim();
|
|
190
190
|
d = d ? `${d}
|
|
191
|
-
${
|
|
191
|
+
${i}` : i;
|
|
192
192
|
}
|
|
193
193
|
if (d === void 0) return null;
|
|
194
194
|
let t;
|
|
@@ -197,7 +197,7 @@ ${a}` : a;
|
|
|
197
197
|
} catch {
|
|
198
198
|
return null;
|
|
199
199
|
}
|
|
200
|
-
const
|
|
200
|
+
const n = t.type === "tool_result" && typeof t.content == "object" && t.content ? t.content : void 0;
|
|
201
201
|
switch (t.type) {
|
|
202
202
|
case "session.start":
|
|
203
203
|
return {
|
|
@@ -205,15 +205,15 @@ ${a}` : a;
|
|
|
205
205
|
content: "",
|
|
206
206
|
sessionId: t.sessionId ?? t.session_id ?? "",
|
|
207
207
|
roundId: t.roundId ?? t.round_id,
|
|
208
|
-
event:
|
|
209
|
-
id:
|
|
208
|
+
event: s,
|
|
209
|
+
id: o
|
|
210
210
|
};
|
|
211
211
|
case "user":
|
|
212
|
-
return { type: "user", content:
|
|
212
|
+
return { type: "user", content: h(t.content), event: s, id: o };
|
|
213
213
|
case "thinking":
|
|
214
|
-
return { type: "thinking", content:
|
|
214
|
+
return { type: "thinking", content: h(t.content), event: s, id: o };
|
|
215
215
|
case "ai":
|
|
216
|
-
return { type: "ai", content:
|
|
216
|
+
return { type: "ai", content: h(t.content), event: s, id: o };
|
|
217
217
|
case "tool_call":
|
|
218
218
|
return {
|
|
219
219
|
type: "tool_call",
|
|
@@ -221,56 +221,56 @@ ${a}` : a;
|
|
|
221
221
|
toolName: t.name,
|
|
222
222
|
toolCallId: t.id,
|
|
223
223
|
args: typeof t.args == "string" ? t.args : JSON.stringify(t.args ?? {}),
|
|
224
|
-
event:
|
|
225
|
-
id:
|
|
224
|
+
event: s,
|
|
225
|
+
id: o
|
|
226
226
|
};
|
|
227
227
|
case "tool_delta":
|
|
228
228
|
return {
|
|
229
229
|
type: "tool_delta",
|
|
230
230
|
content: typeof t.content == "string" ? t.content : "",
|
|
231
231
|
toolCallId: t.tool_call_id ?? t.toolCallId ?? "",
|
|
232
|
-
event:
|
|
233
|
-
id:
|
|
232
|
+
event: s,
|
|
233
|
+
id: o
|
|
234
234
|
};
|
|
235
235
|
case "tool_result": {
|
|
236
|
-
const
|
|
237
|
-
let
|
|
238
|
-
return typeof
|
|
236
|
+
const a = n == null ? void 0 : n.result;
|
|
237
|
+
let i;
|
|
238
|
+
return typeof a == "string" ? i = a : a != null && typeof a == "object" ? i = JSON.stringify(a) : n && typeof n == "object" && Object.keys(n).length > 0 ? i = JSON.stringify(n) : i = h(t.output ?? ""), {
|
|
239
239
|
type: "tool_result",
|
|
240
|
-
content:
|
|
240
|
+
content: i,
|
|
241
241
|
toolName: t.name,
|
|
242
242
|
toolCallId: t.id,
|
|
243
243
|
artifacts: Array.isArray(t.artifacts) ? t.artifacts : void 0,
|
|
244
|
-
renderMode: (
|
|
245
|
-
html: (
|
|
246
|
-
markdown: (
|
|
247
|
-
event:
|
|
248
|
-
id:
|
|
244
|
+
renderMode: (n == null ? void 0 : n.render_mode) ?? t.render_mode ?? t.renderMode,
|
|
245
|
+
html: (n == null ? void 0 : n.html) ?? t.html,
|
|
246
|
+
markdown: (n == null ? void 0 : n.markdown) ?? t.markdown,
|
|
247
|
+
event: s,
|
|
248
|
+
id: o
|
|
249
249
|
};
|
|
250
250
|
}
|
|
251
251
|
case "done":
|
|
252
252
|
return {
|
|
253
253
|
type: "done",
|
|
254
|
-
content:
|
|
254
|
+
content: h(t.content),
|
|
255
255
|
sessionId: t.sessionId ?? t.session_id ?? "",
|
|
256
256
|
messageId: t.messageId ?? t.message_id,
|
|
257
257
|
roundId: t.roundId ?? t.round_id,
|
|
258
258
|
artifacts: Array.isArray(t.artifacts) ? t.artifacts : void 0,
|
|
259
|
-
event:
|
|
260
|
-
id:
|
|
259
|
+
event: s,
|
|
260
|
+
id: o
|
|
261
261
|
};
|
|
262
262
|
case "stop":
|
|
263
263
|
return {
|
|
264
264
|
type: "stop",
|
|
265
|
-
content:
|
|
265
|
+
content: h(t.content),
|
|
266
266
|
sessionId: t.sessionId ?? t.session_id ?? "",
|
|
267
267
|
messageId: t.messageId ?? t.message_id,
|
|
268
268
|
roundId: t.roundId ?? t.round_id,
|
|
269
|
-
event:
|
|
270
|
-
id:
|
|
269
|
+
event: s,
|
|
270
|
+
id: o
|
|
271
271
|
};
|
|
272
272
|
case "error":
|
|
273
|
-
return { type: "error", content: t.message ?? "Unknown error", event:
|
|
273
|
+
return { type: "error", content: t.message ?? "Unknown error", event: s, id: o };
|
|
274
274
|
case "interrupt":
|
|
275
275
|
return {
|
|
276
276
|
type: "interrupt",
|
|
@@ -287,322 +287,340 @@ ${a}` : a;
|
|
|
287
287
|
fields: t.fields,
|
|
288
288
|
metadata: t.metadata
|
|
289
289
|
},
|
|
290
|
-
event:
|
|
291
|
-
id:
|
|
290
|
+
event: s,
|
|
291
|
+
id: o
|
|
292
292
|
};
|
|
293
293
|
default:
|
|
294
294
|
return null;
|
|
295
295
|
}
|
|
296
296
|
}
|
|
297
|
-
async function T(
|
|
298
|
-
if (!
|
|
299
|
-
|
|
297
|
+
async function T(r, e, o) {
|
|
298
|
+
if (!r.body) {
|
|
299
|
+
o(new Error("Response body is null"));
|
|
300
300
|
return;
|
|
301
301
|
}
|
|
302
|
-
const
|
|
302
|
+
const s = r.body.getReader(), d = new TextDecoder();
|
|
303
303
|
let t = "";
|
|
304
304
|
try {
|
|
305
305
|
for (; ; ) {
|
|
306
|
-
const { value:
|
|
307
|
-
if (
|
|
308
|
-
t += d.decode(
|
|
309
|
-
let
|
|
310
|
-
for (; (
|
|
306
|
+
const { value: n, done: a } = await s.read();
|
|
307
|
+
if (a) break;
|
|
308
|
+
t += d.decode(n, { stream: !0 });
|
|
309
|
+
let i;
|
|
310
|
+
for (; (i = t.indexOf(`
|
|
311
311
|
|
|
312
312
|
`)) !== -1; ) {
|
|
313
|
-
const c = t.slice(0,
|
|
314
|
-
t = t.slice(
|
|
313
|
+
const c = t.slice(0, i);
|
|
314
|
+
t = t.slice(i + 2);
|
|
315
315
|
const l = w(c);
|
|
316
316
|
l && e(l);
|
|
317
317
|
}
|
|
318
318
|
}
|
|
319
319
|
if (t.trim()) {
|
|
320
|
-
const
|
|
321
|
-
|
|
320
|
+
const n = w(t);
|
|
321
|
+
n && e(n);
|
|
322
322
|
}
|
|
323
|
-
} catch (
|
|
324
|
-
(
|
|
323
|
+
} catch (n) {
|
|
324
|
+
(n == null ? void 0 : n.name) !== "AbortError" && o(n instanceof Error ? n : new Error(String(n)));
|
|
325
325
|
} finally {
|
|
326
326
|
try {
|
|
327
|
-
|
|
327
|
+
s.releaseLock();
|
|
328
328
|
} catch {
|
|
329
329
|
}
|
|
330
330
|
}
|
|
331
331
|
}
|
|
332
|
-
function
|
|
332
|
+
function _(r, e) {
|
|
333
333
|
var t;
|
|
334
|
-
const
|
|
335
|
-
botCode:
|
|
334
|
+
const o = new AbortController(), s = e.signal ? $(e.signal, o.signal) : o.signal, d = {
|
|
335
|
+
botCode: r.getBotCode(),
|
|
336
336
|
message: e.message,
|
|
337
337
|
sessionId: e.sessionId,
|
|
338
338
|
stream: e.stream !== !1,
|
|
339
339
|
// url 仅前端回显(下载/预览),剥离后发送——签名 URL 不进后端
|
|
340
340
|
...(t = e.attachments) != null && t.length ? {
|
|
341
|
-
attachments: e.attachments.map(({ url:
|
|
341
|
+
attachments: e.attachments.map(({ url: n, ...a }) => a)
|
|
342
342
|
} : {}
|
|
343
343
|
};
|
|
344
|
-
return d.stream ?
|
|
345
|
-
var
|
|
346
|
-
if (!
|
|
347
|
-
let
|
|
344
|
+
return d.stream ? r.streamPost("/chat", d, s).then(async (n) => {
|
|
345
|
+
var a;
|
|
346
|
+
if (!n.ok) {
|
|
347
|
+
let i = `HTTP ${n.status}`;
|
|
348
348
|
try {
|
|
349
|
-
const c = await
|
|
350
|
-
c != null && c.message && (
|
|
349
|
+
const c = await n.json();
|
|
350
|
+
c != null && c.message && (i = c.message);
|
|
351
351
|
} catch {
|
|
352
352
|
}
|
|
353
|
-
(
|
|
353
|
+
(a = e.onError) == null || a.call(e, new Error(i));
|
|
354
354
|
return;
|
|
355
355
|
}
|
|
356
356
|
return T(
|
|
357
|
-
|
|
358
|
-
(
|
|
359
|
-
var c, l,
|
|
360
|
-
|
|
357
|
+
n,
|
|
358
|
+
(i) => {
|
|
359
|
+
var c, l, u, f, m, y, I, p;
|
|
360
|
+
i.type === "done" && i.sessionId ? ((c = e.onMessage) == null || c.call(e, i), (l = e.onDone) == null || l.call(e, { sessionId: i.sessionId, content: i.content, messageId: i.messageId, roundId: i.roundId, artifacts: i.artifacts })) : i.type === "done" ? ((u = e.onMessage) == null || u.call(e, i), (f = e.onDone) == null || f.call(e, { sessionId: "", content: i.content, messageId: i.messageId, roundId: i.roundId, artifacts: i.artifacts })) : i.type === "stop" ? ((m = e.onMessage) == null || m.call(e, i), (y = e.onDone) == null || y.call(e, { sessionId: i.sessionId ?? "", content: i.content, messageId: i.messageId, roundId: i.roundId })) : i.type === "error" ? (I = e.onError) == null || I.call(e, new Error(i.content)) : (p = e.onMessage) == null || p.call(e, i);
|
|
361
361
|
},
|
|
362
|
-
(
|
|
362
|
+
(i) => {
|
|
363
363
|
var c;
|
|
364
|
-
return (c = e.onError) == null ? void 0 : c.call(e,
|
|
364
|
+
return (c = e.onError) == null ? void 0 : c.call(e, i);
|
|
365
365
|
}
|
|
366
366
|
);
|
|
367
|
-
}).catch((
|
|
368
|
-
var
|
|
369
|
-
(
|
|
370
|
-
}) :
|
|
371
|
-
var
|
|
372
|
-
(
|
|
373
|
-
sessionId:
|
|
374
|
-
content:
|
|
375
|
-
messageId:
|
|
367
|
+
}).catch((n) => {
|
|
368
|
+
var a;
|
|
369
|
+
(n == null ? void 0 : n.name) !== "AbortError" && ((a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n))));
|
|
370
|
+
}) : r.post("/chat", d).then((n) => {
|
|
371
|
+
var a;
|
|
372
|
+
(a = e.onDone) == null || a.call(e, {
|
|
373
|
+
sessionId: n.sessionId,
|
|
374
|
+
content: n.content ?? "",
|
|
375
|
+
messageId: n.messageId
|
|
376
376
|
});
|
|
377
|
-
}).catch((
|
|
378
|
-
var
|
|
379
|
-
(
|
|
380
|
-
}),
|
|
377
|
+
}).catch((n) => {
|
|
378
|
+
var a;
|
|
379
|
+
(a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n)));
|
|
380
|
+
}), o;
|
|
381
381
|
}
|
|
382
|
-
function
|
|
383
|
-
const
|
|
384
|
-
return
|
|
382
|
+
function $(r, e) {
|
|
383
|
+
const o = new AbortController(), s = () => o.abort();
|
|
384
|
+
return r.addEventListener("abort", s, { once: !0 }), e.addEventListener("abort", s, { once: !0 }), (r.aborted || e.aborted) && o.abort(), o.signal;
|
|
385
385
|
}
|
|
386
|
-
async function P(
|
|
387
|
-
return (await
|
|
388
|
-
botCode:
|
|
386
|
+
async function P(r) {
|
|
387
|
+
return (await r.post("/chat/sessions", {
|
|
388
|
+
botCode: r.getBotCode()
|
|
389
389
|
})).sessionId;
|
|
390
390
|
}
|
|
391
|
-
function
|
|
391
|
+
function v(r) {
|
|
392
392
|
return {
|
|
393
393
|
// 后端返回 thread_id,toCamelCase 转为 threadId,映射为 sessionId
|
|
394
|
-
sessionId:
|
|
394
|
+
sessionId: r.threadId ?? r.sessionId,
|
|
395
395
|
// bot 外部标识统一 botCode(createSuperAgent 入参,全链消费);
|
|
396
396
|
// 后端回传的内部 bot 标识字段 SDK 不消费、不暴露
|
|
397
|
-
title:
|
|
398
|
-
createTime:
|
|
399
|
-
updateTime:
|
|
397
|
+
title: r.title,
|
|
398
|
+
createTime: r.createTime,
|
|
399
|
+
updateTime: r.updateTime
|
|
400
400
|
};
|
|
401
401
|
}
|
|
402
|
-
async function
|
|
403
|
-
const
|
|
404
|
-
botCode:
|
|
402
|
+
async function x(r, e) {
|
|
403
|
+
const o = await r.get("/chat/conversations", {
|
|
404
|
+
botCode: r.getBotCode(),
|
|
405
405
|
page: (e == null ? void 0 : e.page) ?? 1,
|
|
406
406
|
size: (e == null ? void 0 : e.size) ?? 20
|
|
407
407
|
});
|
|
408
408
|
return {
|
|
409
|
-
...
|
|
410
|
-
items:
|
|
409
|
+
...o,
|
|
410
|
+
items: o.items.map(v)
|
|
411
411
|
};
|
|
412
412
|
}
|
|
413
|
-
async function
|
|
414
|
-
await
|
|
413
|
+
async function E(r, e, o) {
|
|
414
|
+
await r.patch(`/chat/conversations/${e}`, { title: o });
|
|
415
415
|
}
|
|
416
|
-
async function O(
|
|
417
|
-
await
|
|
416
|
+
async function O(r, e) {
|
|
417
|
+
await r.del(`/chat/conversations/${e}`);
|
|
418
418
|
}
|
|
419
|
-
async function U(
|
|
420
|
-
const
|
|
421
|
-
|
|
419
|
+
async function U(r, e) {
|
|
420
|
+
const o = await r.get(
|
|
421
|
+
`/chat/conversations/${e}/messages`
|
|
422
|
+
);
|
|
423
|
+
return M(o);
|
|
422
424
|
}
|
|
423
425
|
function k() {
|
|
424
426
|
return `r_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
425
427
|
}
|
|
426
|
-
function
|
|
428
|
+
function M(r) {
|
|
427
429
|
var d;
|
|
428
|
-
const e =
|
|
430
|
+
const e = r.length > 0 && Array.isArray((d = r[0]) == null ? void 0 : d.data), o = [];
|
|
429
431
|
if (e)
|
|
430
|
-
for (const t of
|
|
431
|
-
|
|
432
|
+
for (const t of r)
|
|
433
|
+
o.push({
|
|
432
434
|
roundId: t.roundId ?? k(),
|
|
433
435
|
artifacts: Array.isArray(t.artifacts) && t.artifacts.length ? t.artifacts : void 0,
|
|
434
436
|
items: Array.isArray(t.data) ? t.data : []
|
|
435
437
|
});
|
|
436
438
|
else
|
|
437
|
-
|
|
438
|
-
const
|
|
439
|
-
for (const t of
|
|
440
|
-
const
|
|
441
|
-
for (const
|
|
439
|
+
o.push({ roundId: k(), items: r });
|
|
440
|
+
const s = [];
|
|
441
|
+
for (const t of o) {
|
|
442
|
+
const n = N(t.items);
|
|
443
|
+
for (const a of n) a.roundId = t.roundId;
|
|
442
444
|
if (t.artifacts) {
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
+
const a = n.findLast((i) => i.role === "assistant");
|
|
446
|
+
a && (a.artifacts = t.artifacts);
|
|
445
447
|
}
|
|
446
|
-
|
|
448
|
+
s.push(...n);
|
|
447
449
|
}
|
|
448
|
-
return
|
|
450
|
+
return s;
|
|
449
451
|
}
|
|
450
|
-
function
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
452
|
+
function N(r) {
|
|
453
|
+
var n;
|
|
454
|
+
const e = [...r].sort((a, i) => {
|
|
455
|
+
const c = new Date(a.createTime ?? 0).getTime() || 0, l = new Date(i.createTime ?? 0).getTime() || 0;
|
|
456
|
+
return c !== l ? c - l : (a.seq ?? 0) - (i.seq ?? 0);
|
|
457
|
+
}), o = /* @__PURE__ */ new Map();
|
|
458
|
+
for (const a of e)
|
|
459
|
+
a.role === "tool" && a.toolCallId && o.set(a.toolCallId, a.content || "");
|
|
460
|
+
const s = /* @__PURE__ */ new Set(), d = [], t = (a, i, c, l) => {
|
|
461
|
+
const u = d[d.length - 1];
|
|
462
|
+
(u == null ? void 0 : u.role) === "assistant" ? (u.parts.push(...a), u.id = i, l && (u.messageId = l)) : d.push({
|
|
463
|
+
id: i,
|
|
464
|
+
role: "assistant",
|
|
465
|
+
parts: [...a],
|
|
466
|
+
timestamp: c,
|
|
467
|
+
messageId: l
|
|
468
|
+
});
|
|
460
469
|
};
|
|
461
|
-
for (const
|
|
462
|
-
if (
|
|
463
|
-
const
|
|
464
|
-
if (
|
|
465
|
-
const
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
470
|
+
for (const a of e) {
|
|
471
|
+
if (a.role === "system") continue;
|
|
472
|
+
const i = String(a.id ?? a.messageId ?? `msg_${Date.now()}`), c = new Date(a.createTime ?? "").getTime() || Date.now();
|
|
473
|
+
if (a.role === "human") {
|
|
474
|
+
const l = (a.attachments ?? []).map(
|
|
475
|
+
(f) => ({
|
|
476
|
+
type: "attachment",
|
|
477
|
+
attachment: {
|
|
478
|
+
nosKey: f.nosKey ?? f.attachmentId ?? "",
|
|
479
|
+
// 历史回显后端不带 nosKey,占位空串
|
|
480
|
+
filename: f.filename ?? "file",
|
|
481
|
+
size: f.size ?? void 0,
|
|
482
|
+
mime: f.mime ?? void 0,
|
|
483
|
+
url: f.url ?? void 0
|
|
484
|
+
// 公开读桶下载链接(有则 chip 可点击下载)
|
|
485
|
+
}
|
|
486
|
+
})
|
|
487
|
+
), u = (n = a.metadata) == null ? void 0 : n.raw_message;
|
|
477
488
|
d.push({
|
|
478
|
-
id:
|
|
489
|
+
id: i,
|
|
479
490
|
role: "user",
|
|
480
|
-
parts: [...
|
|
481
|
-
timestamp:
|
|
491
|
+
parts: [...l, { type: "text", content: h(a.content) }],
|
|
492
|
+
timestamp: c,
|
|
493
|
+
messageId: a.messageId,
|
|
494
|
+
rawContent: typeof u == "string" && u ? u : void 0
|
|
482
495
|
});
|
|
483
496
|
continue;
|
|
484
497
|
}
|
|
485
|
-
if (
|
|
486
|
-
const
|
|
487
|
-
|
|
498
|
+
if (a.role === "ai") {
|
|
499
|
+
const l = D(a, o, s);
|
|
500
|
+
l.length > 0 && t(l, i, c, a.messageId);
|
|
488
501
|
continue;
|
|
489
502
|
}
|
|
490
|
-
if (
|
|
491
|
-
if (
|
|
492
|
-
t([L(
|
|
503
|
+
if (a.role === "tool") {
|
|
504
|
+
if (a.toolCallId && s.has(a.toolCallId)) continue;
|
|
505
|
+
t([L(a)], i, c);
|
|
493
506
|
}
|
|
494
507
|
}
|
|
495
508
|
return d;
|
|
496
509
|
}
|
|
497
|
-
function
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
for (const d of
|
|
501
|
-
d.is_interrupt ?? d.isInterrupt ? (
|
|
502
|
-
return
|
|
510
|
+
function D(r, e, o) {
|
|
511
|
+
const s = [];
|
|
512
|
+
r.reasoning && s.push({ type: "thinking", content: r.reasoning }), r.content && s.push({ type: "text", content: h(r.content) });
|
|
513
|
+
for (const d of r.toolCalls ?? [])
|
|
514
|
+
d.is_interrupt ?? d.isInterrupt ? (o.add(d.id), s.push(q(d, e))) : s.push(J(d));
|
|
515
|
+
return s;
|
|
503
516
|
}
|
|
504
|
-
function q(
|
|
505
|
-
const
|
|
517
|
+
function q(r, e) {
|
|
518
|
+
const o = e.has(r.id), s = typeof r.args == "object" ? r.args : {};
|
|
506
519
|
return {
|
|
507
520
|
type: "interrupt",
|
|
508
521
|
interrupt: {
|
|
509
|
-
interruptId:
|
|
510
|
-
interruptType:
|
|
511
|
-
content:
|
|
512
|
-
options:
|
|
513
|
-
placeholder:
|
|
514
|
-
inputType:
|
|
515
|
-
maxLength:
|
|
516
|
-
required:
|
|
517
|
-
fields:
|
|
518
|
-
metadata:
|
|
522
|
+
interruptId: r.id ?? "",
|
|
523
|
+
interruptType: s.interruptType ?? "input",
|
|
524
|
+
content: s.content ?? "",
|
|
525
|
+
options: s.options,
|
|
526
|
+
placeholder: s.placeholder,
|
|
527
|
+
inputType: s.inputType,
|
|
528
|
+
maxLength: s.maxLength,
|
|
529
|
+
required: s.required,
|
|
530
|
+
fields: s.fields,
|
|
531
|
+
metadata: s.metadata
|
|
519
532
|
},
|
|
520
|
-
resolved:
|
|
521
|
-
response:
|
|
533
|
+
resolved: o,
|
|
534
|
+
response: o ? { action: "submit", value: j(e.get(r.id)) } : void 0
|
|
522
535
|
};
|
|
523
536
|
}
|
|
524
|
-
function J(
|
|
537
|
+
function J(r) {
|
|
525
538
|
let e;
|
|
526
539
|
try {
|
|
527
|
-
e = typeof
|
|
540
|
+
e = typeof r.args == "string" ? r.args : JSON.stringify(r.args ?? {});
|
|
528
541
|
} catch {
|
|
529
|
-
e = String(
|
|
542
|
+
e = String(r.args ?? "{}");
|
|
530
543
|
}
|
|
531
|
-
return {
|
|
544
|
+
return {
|
|
545
|
+
type: "tool_call",
|
|
546
|
+
toolName: r.name ?? "",
|
|
547
|
+
toolCallId: r.id ?? "",
|
|
548
|
+
args: e
|
|
549
|
+
};
|
|
532
550
|
}
|
|
533
|
-
function L(
|
|
534
|
-
var
|
|
535
|
-
const e = Array.isArray(
|
|
551
|
+
function L(r) {
|
|
552
|
+
var o, s, d, t;
|
|
553
|
+
const e = Array.isArray(r.artifacts) ? r.artifacts : void 0;
|
|
536
554
|
return {
|
|
537
555
|
type: "tool_result",
|
|
538
|
-
toolName:
|
|
539
|
-
toolCallId:
|
|
556
|
+
toolName: r.toolName ?? "",
|
|
557
|
+
toolCallId: r.toolCallId ?? "",
|
|
540
558
|
// tool 行后端 content 即展示摘要(_build_tool_fields 与 SSE 口径一致),直接读 content
|
|
541
|
-
content:
|
|
559
|
+
content: h(r.content),
|
|
542
560
|
artifacts: e,
|
|
543
|
-
renderMode:
|
|
544
|
-
html:
|
|
545
|
-
markdown:
|
|
561
|
+
renderMode: r.renderMode ?? ((o = r.metadata) == null ? void 0 : o.renderMode) ?? ((s = r.metadata) == null ? void 0 : s.render_mode),
|
|
562
|
+
html: r.html ?? ((d = r.metadata) == null ? void 0 : d.html),
|
|
563
|
+
markdown: r.markdown ?? ((t = r.metadata) == null ? void 0 : t.markdown)
|
|
546
564
|
};
|
|
547
565
|
}
|
|
548
|
-
function j(
|
|
549
|
-
if (
|
|
566
|
+
function j(r) {
|
|
567
|
+
if (r != null)
|
|
550
568
|
try {
|
|
551
|
-
return JSON.parse(
|
|
569
|
+
return JSON.parse(r);
|
|
552
570
|
} catch {
|
|
553
|
-
return
|
|
571
|
+
return r;
|
|
554
572
|
}
|
|
555
573
|
}
|
|
556
|
-
async function G(
|
|
557
|
-
const
|
|
574
|
+
async function G(r, e, o, s, d, t, n) {
|
|
575
|
+
const a = await r.streamPost(
|
|
558
576
|
`/chat/interrupt/${e}/respond`,
|
|
559
577
|
{
|
|
560
|
-
sessionId:
|
|
561
|
-
action:
|
|
562
|
-
value:
|
|
578
|
+
sessionId: o,
|
|
579
|
+
action: s.action,
|
|
580
|
+
value: s.value
|
|
563
581
|
},
|
|
564
|
-
|
|
582
|
+
n
|
|
565
583
|
);
|
|
566
|
-
if (!
|
|
567
|
-
const
|
|
568
|
-
throw t == null || t(
|
|
584
|
+
if (!a.ok) {
|
|
585
|
+
const i = new Error(`HTTP ${a.status}`);
|
|
586
|
+
throw t == null || t(i), i;
|
|
569
587
|
}
|
|
570
588
|
d && await T(
|
|
571
|
-
|
|
572
|
-
(
|
|
573
|
-
|
|
589
|
+
a,
|
|
590
|
+
(i) => {
|
|
591
|
+
i.type === "error" ? t == null || t(new Error(i.content)) : d(i);
|
|
574
592
|
},
|
|
575
|
-
(
|
|
593
|
+
(i) => t == null ? void 0 : t(i)
|
|
576
594
|
);
|
|
577
595
|
}
|
|
578
|
-
async function R(
|
|
579
|
-
return
|
|
596
|
+
async function R(r) {
|
|
597
|
+
return r.get(`/chat/bots/${r.getBotCode()}/skills`);
|
|
580
598
|
}
|
|
581
|
-
async function B(
|
|
582
|
-
await
|
|
599
|
+
async function B(r, e) {
|
|
600
|
+
await r.post("/chat/stop", { sessionId: e });
|
|
583
601
|
}
|
|
584
|
-
function z(
|
|
585
|
-
const e = new A(
|
|
602
|
+
function z(r) {
|
|
603
|
+
const e = new A(r);
|
|
586
604
|
return {
|
|
587
605
|
botCode: e.getBotCode(),
|
|
588
606
|
getToken: () => e.fetchToken(),
|
|
589
607
|
createSession: () => P(e),
|
|
590
|
-
chat: (
|
|
591
|
-
listConversations: (
|
|
592
|
-
renameConversation: (
|
|
593
|
-
deleteConversation: (
|
|
594
|
-
getMessages: (
|
|
595
|
-
respondInterrupt: (
|
|
596
|
-
setToken: (
|
|
597
|
-
feedback: async (
|
|
598
|
-
const
|
|
599
|
-
d === "dislike" && ((t == null ? void 0 : t.reason) != null && (
|
|
608
|
+
chat: (s) => _(e, s),
|
|
609
|
+
listConversations: (s) => x(e, s),
|
|
610
|
+
renameConversation: (s, d) => E(e, s, d),
|
|
611
|
+
deleteConversation: (s) => O(e, s),
|
|
612
|
+
getMessages: (s) => U(e, s),
|
|
613
|
+
respondInterrupt: (s, d, t, n, a, i) => G(e, s, d, t, n, a, i),
|
|
614
|
+
setToken: (s) => e.setToken(s),
|
|
615
|
+
feedback: async (s, d, t) => {
|
|
616
|
+
const n = { type: d };
|
|
617
|
+
d === "dislike" && ((t == null ? void 0 : t.reason) != null && (n.reason = t.reason), t != null && t.remark && (n.remark = t.remark)), await e.post(`/chat/messages/${s}/feedback`, n);
|
|
600
618
|
},
|
|
601
|
-
cancelFeedback: async (
|
|
602
|
-
await e.del(`/chat/messages/${
|
|
619
|
+
cancelFeedback: async (s) => {
|
|
620
|
+
await e.del(`/chat/messages/${s}/feedback`);
|
|
603
621
|
},
|
|
604
622
|
listSkills: () => R(e),
|
|
605
|
-
stopGeneration: (
|
|
623
|
+
stopGeneration: (s) => B(e, s)
|
|
606
624
|
};
|
|
607
625
|
}
|
|
608
626
|
export {
|