super-agent-sdk 1.0.26 → 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/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 g(r) {
4
- if (Array.isArray(r)) return r.map((e) => g(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 [o, s] of Object.entries(r)) {
8
- const d = o.replace(/_([a-z])/g, (t, n) => n.toUpperCase());
9
- e[d] = g(s);
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 o = e.baseUrl ?? A;
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;
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 o = `${this.isAbsoluteGateway ? this.tokenGateway : `${this.getOrigin()}${this.tokenGateway}`}/superagent/v2/token/generate`, s = {
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 && (s["X-Tenant-Id"] = String(this.tenantId));
49
- const d = await fetch(o, {
48
+ this.tenantId != null && (n["X-Tenant-Id"] = String(this.tenantId));
49
+ const d = await fetch(i, {
50
50
  method: "POST",
51
- headers: s,
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 n = g(t.data);
60
- return this.token = n.token, n.appId && !this.appId && (this.appId = n.appId), n.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, o, s) {
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}${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}`);
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: s != null && s.body ? JSON.stringify(s.body) : void 0
103
+ body: n != null && n.body ? JSON.stringify(n.body) : void 0
104
104
  });
105
- if (t.status === 401 && !(s != null && s.retry) && await this.handleTokenRefresh())
106
- return this.request(e, o, { ...s, retry: !0 });
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 a = `HTTP ${t.status}: ${t.statusText}`;
108
+ let o = `HTTP ${t.status}: ${t.statusText}`;
109
109
  try {
110
- const i = await t.json();
111
- i != null && i.message && (a = i.message);
110
+ const a = await t.json();
111
+ a != null && a.message && (o = a.message);
112
112
  } catch {
113
113
  }
114
- throw new Error(a);
114
+ throw new Error(o);
115
115
  }
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);
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, o) {
122
- return this.request("GET", e, { params: o });
121
+ get(e, i) {
122
+ return this.request("GET", e, { params: i });
123
123
  }
124
- post(e, o) {
125
- return this.request("POST", e, { body: o });
124
+ post(e, i) {
125
+ return this.request("POST", e, { body: i });
126
126
  }
127
- patch(e, o) {
128
- return this.request("PATCH", e, { body: o });
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, o, s) {
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(o),
141
- signal: s
140
+ body: JSON.stringify(i),
141
+ signal: n
142
142
  });
143
143
  if (t.status === 401) {
144
- const n = await b(t.clone());
145
- if (n.isCookieExpired && typeof (o == null ? void 0 : o.message) == "string") {
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: o.message,
149
- sessionId: o.sessionId ?? ""
148
+ message: i.message,
149
+ sessionId: i.sessionId ?? ""
150
150
  });
151
- const i = S(n.loginUrl ?? this.loginUrl);
151
+ const a = S(s.loginUrl ?? this.loginUrl);
152
152
  return this.onAuthExpired ? this.onAuthExpired({
153
- loginUrl: i,
154
- sessionId: n.sessionId,
153
+ loginUrl: a,
154
+ sessionId: s.sessionId,
155
155
  botCode: this.botCode
156
- }) : i && window.location.replace(i), t;
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(o),
163
- signal: s
162
+ body: JSON.stringify(i),
163
+ signal: n
164
164
  });
165
165
  }
166
166
  return t;
167
167
  }
168
168
  }
169
- function h(r) {
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((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();
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
- ${i}` : i;
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 n = t.type === "tool_result" && typeof t.content == "object" && t.content ? t.content : void 0;
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: s,
209
- id: o
208
+ event: n,
209
+ id: i
210
210
  };
211
211
  case "user":
212
- return { type: "user", content: h(t.content), event: s, id: o };
212
+ return { type: "user", content: m(t.content), event: n, id: i };
213
213
  case "thinking":
214
- return { type: "thinking", content: h(t.content), event: s, id: o };
214
+ return { type: "thinking", content: m(t.content), event: n, id: i };
215
215
  case "ai":
216
- return { type: "ai", content: h(t.content), event: s, id: o };
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: s,
225
- id: o
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: s,
233
- id: o
233
+ event: n,
234
+ id: i
234
235
  };
235
236
  case "tool_result": {
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 ?? ""), {
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: i,
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: (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,
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: s,
249
- id: o
250
+ event: n,
251
+ id: i
250
252
  };
251
253
  }
252
254
  case "done":
253
255
  return {
254
256
  type: "done",
255
- content: h(t.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: s,
261
- id: o
262
+ event: n,
263
+ id: i
262
264
  };
263
265
  case "stop":
264
266
  return {
265
267
  type: "stop",
266
- content: h(t.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: s,
271
- id: o
272
+ event: n,
273
+ id: i
272
274
  };
273
275
  case "error":
274
- return { type: "error", content: t.message ?? "Unknown error", event: s, id: o };
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: s,
292
- id: o
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, o) {
300
+ async function T(r, e, i) {
299
301
  if (!r.body) {
300
- o(new Error("Response body is null"));
302
+ i(new Error("Response body is null"));
301
303
  return;
302
304
  }
303
- const s = r.body.getReader(), d = new TextDecoder();
305
+ const n = r.body.getReader(), d = new TextDecoder();
304
306
  let t = "";
305
307
  try {
306
308
  for (; ; ) {
307
- const { value: n, done: a } = await s.read();
308
- if (a) break;
309
- t += d.decode(n, { stream: !0 });
310
- let i;
311
- for (; (i = t.indexOf(`
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, i);
315
- t = t.slice(i + 2);
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 n = w(t);
322
- n && e(n);
323
+ const s = w(t);
324
+ s && e(s);
323
325
  }
324
- } catch (n) {
325
- (n == null ? void 0 : n.name) !== "AbortError" && o(n instanceof Error ? n : new Error(String(n)));
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
- s.releaseLock();
330
+ n.releaseLock();
329
331
  } catch {
330
332
  }
331
333
  }
332
334
  }
333
335
  function $(r, e) {
334
336
  var t;
335
- const o = new AbortController(), s = e.signal ? x(e.signal, o.signal) : o.signal, d = {
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: n, ...a }) => a)
344
+ attachments: e.attachments.map(({ url: s, ...o }) => o)
343
345
  } : {}
344
346
  };
345
- return d.stream ? r.streamPost("/chat", d, s).then(async (n) => {
346
- var a;
347
- if (!n.ok) {
348
- let i = `HTTP ${n.status}`;
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 n.json();
351
- c != null && c.message && (i = c.message);
352
+ const c = await s.json();
353
+ c != null && c.message && (a = c.message);
352
354
  } catch {
353
355
  }
354
- (a = e.onError) == null || a.call(e, new Error(i));
356
+ (o = e.onError) == null || o.call(e, new Error(a));
355
357
  return;
356
358
  }
357
359
  return T(
358
- n,
359
- (i) => {
360
- var c, l, f, u, m, y, I, p;
361
- 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" ? ((f = e.onMessage) == null || f.call(e, i), (u = e.onDone) == null || u.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);
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
- (i) => {
365
+ (a) => {
364
366
  var c;
365
- return (c = e.onError) == null ? void 0 : c.call(e, i);
367
+ return (c = e.onError) == null ? void 0 : c.call(e, a);
366
368
  }
367
369
  );
368
- }).catch((n) => {
369
- var a;
370
- (n == null ? void 0 : n.name) !== "AbortError" && ((a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n))));
371
- }) : r.post("/chat", d).then((n) => {
372
- var a;
373
- (a = e.onDone) == null || a.call(e, {
374
- sessionId: n.sessionId,
375
- content: n.content ?? "",
376
- messageId: n.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((n) => {
379
- var a;
380
- (a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n)));
381
- }), o;
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 o = new AbortController(), s = () => o.abort();
385
- return r.addEventListener("abort", s, { once: !0 }), e.addEventListener("abort", s, { once: !0 }), (r.aborted || e.aborted) && o.abort(), o.signal;
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 P(r) {
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 v(r, e) {
404
- const o = await r.get("/chat/conversations", {
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
- ...o,
411
- items: o.items.map(P)
412
+ ...i,
413
+ items: i.items.map(v)
412
414
  };
413
415
  }
414
- async function O(r, e, o) {
415
- await r.patch(`/chat/conversations/${e}`, { title: o });
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 M(r, e) {
421
- const o = await r.get(
422
+ async function N(r, e, i) {
423
+ const n = await r.get(
422
424
  `/chat/conversations/${e}/messages`
423
425
  );
424
- return N(o);
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 N(r) {
430
- var d;
431
- const e = r.length > 0 && Array.isArray((d = r[0]) == null ? void 0 : d.data), o = [];
432
- if (e)
433
- for (const t of r)
434
- o.push({
435
- roundId: t.roundId ?? k(),
436
- artifacts: Array.isArray(t.artifacts) && t.artifacts.length ? t.artifacts : void 0,
437
- items: Array.isArray(t.data) ? t.data : []
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
- o.push({ roundId: k(), items: r });
441
- const s = [];
442
- for (const t of o) {
443
- const n = D(t.items);
444
- for (const a of n) a.roundId = t.roundId;
445
- if (t.artifacts) {
446
- const a = n.findLast((i) => i.role === "assistant");
447
- a && (a.artifacts = t.artifacts);
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
- s.push(...n);
458
+ d.push(...o);
450
459
  }
451
- return s;
460
+ return d;
452
461
  }
453
- function D(r) {
454
- var n;
455
- const e = [...r].sort((a, i) => {
456
- const c = new Date(a.createTime ?? 0).getTime() || 0, l = new Date(i.createTime ?? 0).getTime() || 0;
457
- return c !== l ? c - l : (a.seq ?? 0) - (i.seq ?? 0);
458
- }), o = /* @__PURE__ */ new Map();
459
- for (const a of e)
460
- a.role === "tool" && a.toolCallId && o.set(a.toolCallId, a.content || "");
461
- const s = /* @__PURE__ */ new Set(), d = [], t = (a, i, c, l, f) => {
462
- const u = d[d.length - 1];
463
- (u == null ? void 0 : u.role) === "assistant" ? (u.parts.push(...a), u.id = i, l && (u.messageId = l, u.feedback = f ?? null)) : d.push({
464
- id: i,
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: [...a],
467
- timestamp: c,
468
- messageId: l,
469
- feedback: f ?? null
475
+ parts: [...c],
476
+ timestamp: h,
477
+ messageId: u,
478
+ feedback: g ?? null
470
479
  });
471
480
  };
472
- for (const a of e) {
473
- if (a.role === "system") continue;
474
- const i = String(a.id ?? a.messageId ?? `msg_${Date.now()}`), c = new Date(a.createTime ?? "").getTime() || Date.now();
475
- if (a.role === "human") {
476
- const l = (a.attachments ?? []).map(
477
- (u) => ({
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: u.nosKey ?? u.attachmentId ?? "",
489
+ nosKey: f.nosKey ?? f.attachmentId ?? "",
481
490
  // 历史回显后端不带 nosKey,占位空串
482
- filename: u.filename ?? "file",
483
- size: u.size ?? void 0,
484
- mime: u.mime ?? void 0,
485
- url: u.url ?? void 0
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
- ), f = (n = a.metadata) == null ? void 0 : n.raw_message;
490
- d.push({
491
- id: i,
498
+ ), g = (o = c.metadata) == null ? void 0 : o.raw_message;
499
+ t.push({
500
+ id: l,
492
501
  role: "user",
493
- parts: [...l, { type: "text", content: h(a.content) }],
494
- timestamp: c,
495
- messageId: a.messageId,
496
- rawContent: typeof f == "string" && f ? f : void 0
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 (a.role === "ai") {
501
- const l = q(a, o, s);
502
- l.length > 0 && t(l, i, c, a.messageId, a.feedback);
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 (a.role === "tool") {
506
- if (a.toolCallId && s.has(a.toolCallId)) continue;
507
- t([j(a)], i, c);
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 d;
526
+ return t;
511
527
  }
512
- function q(r, e, o) {
513
- const s = [];
514
- r.reasoning && s.push({ type: "thinking", content: r.reasoning }), r.content && s.push({ type: "text", content: h(r.content) });
515
- for (const d of r.toolCalls ?? [])
516
- d.is_interrupt ?? d.isInterrupt ? (o.add(d.id), s.push(L(d, e))) : s.push(J(d));
517
- return s;
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 o = e.has(r.id), s = typeof r.args == "object" ? r.args : {};
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: s.interruptType ?? "input",
526
- content: s.content ?? "",
527
- options: s.options,
528
- placeholder: s.placeholder,
529
- inputType: s.inputType,
530
- maxLength: s.maxLength,
531
- required: s.required,
532
- fields: s.fields,
533
- metadata: s.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: o,
536
- response: o ? { action: "submit", value: G(e.get(r.id)) } : void 0
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 o, s, d, t, n;
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: h(r.content),
583
+ content: m(r.content),
562
584
  artifacts: e,
563
- renderMode: r.renderMode ?? ((o = r.metadata) == null ? void 0 : o.renderMode) ?? ((s = r.metadata) == null ? void 0 : s.render_mode),
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 ?? ((n = r.metadata) == null ? void 0 : n.default_expanded)
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, o, s, d, t, n) {
578
- const a = await r.streamPost(
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: o,
582
- action: s.action,
583
- value: s.value
603
+ sessionId: i,
604
+ action: n.action,
605
+ value: n.value
584
606
  },
585
- n
607
+ s
586
608
  );
587
- if (!a.ok) {
588
- const i = new Error(`HTTP ${a.status}`);
589
- throw t == null || t(i), i;
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
- a,
593
- (i) => {
594
- i.type === "error" ? t == null || t(new Error(i.content)) : d(i);
614
+ o,
615
+ (a) => {
616
+ a.type === "error" ? t == null || t(new Error(a.content)) : d(a);
595
617
  },
596
- (i) => t == null ? void 0 : t(i)
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: (s) => $(e, s),
612
- listConversations: (s) => v(e, s),
613
- renameConversation: (s, d) => O(e, s, d),
614
- deleteConversation: (s) => U(e, s),
615
- getMessages: (s) => M(e, s),
616
- respondInterrupt: (s, d, t, n, a, i) => R(e, s, d, t, n, a, i),
617
- setToken: (s) => e.setToken(s),
618
- feedback: async (s, d, t) => {
619
- const n = { type: d };
620
- 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);
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 (s) => {
623
- await e.del(`/chat/messages/${s}/feedback`);
644
+ cancelFeedback: async (n) => {
645
+ await e.del(`/chat/messages/${n}/feedback`);
624
646
  },
625
647
  listSkills: () => B(e),
626
- stopGeneration: (s) => K(e, s)
648
+ stopGeneration: (n) => K(e, n)
627
649
  };
628
650
  }
629
651
  export {