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