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