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