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/dist/index.mjs CHANGED
@@ -1,23 +1,23 @@
1
- import { p as w, s as k } from "./nosUploader-CnYDALPr.js";
2
- import { c as K } from "./nosUploader-CnYDALPr.js";
3
- function h(r) {
4
- if (Array.isArray(r)) return r.map((e) => h(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 [t, s] of Object.entries(r)) {
8
- const o = t.replace(/_([a-z])/g, (a, n) => n.toUpperCase());
9
- e[o] = h(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] = f(n);
10
10
  }
11
11
  return e;
12
12
  }
13
13
  return r;
14
14
  }
15
- const T = "/api/agent/runtime/v1";
16
- class b {
15
+ const A = "/api/agent/runtime/v1";
16
+ class $ {
17
17
  constructor(e) {
18
18
  this.refreshing = null;
19
- const t = e.baseUrl ?? T;
20
- this.baseUrl = t.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;
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}/v1/token — 获取 token + appId */
35
+ /** POST {origin}{tokenGateway}/v2/token/generate — 获取 token + appId */
36
36
  async fetchToken() {
37
- const t = `${this.isAbsoluteGateway ? this.tokenGateway : `${this.getOrigin()}${this.tokenGateway}`}/v1/token`, s = await fetch(t, {
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 (!s.ok)
43
- throw new Error(`HTTP ${s.status}: ${s.statusText}`);
44
- const o = await s.json();
45
- if (o.code !== "200")
46
- throw new Error(o.message || `API error: ${o.code}`);
47
- const a = h(o.data);
48
- return this.token = a.token, a.appId && !this.appId && (this.appId = a.appId), a.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, t, s) {
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 o = this.isAbsoluteUrl ? `${this.baseUrl}${t}` : `${this.getOrigin()}${this.baseUrl}${t}`;
75
- if (s != null && s.params) {
76
- const c = new URLSearchParams();
77
- for (const [d, l] of Object.entries(s.params))
78
- l != null && c.set(d, String(l));
79
- const i = c.toString();
80
- i && (o += `?${i}`);
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 a = await fetch(o, {
82
+ const t = await fetch(d, {
83
83
  method: e,
84
84
  headers: this.headers(),
85
- body: s != null && s.body ? JSON.stringify(s.body) : void 0
85
+ body: n != null && n.body ? JSON.stringify(n.body) : void 0
86
86
  });
87
- if (a.status === 401 && !(s != null && s.retry) && await this.handleTokenRefresh())
88
- return this.request(e, t, { ...s, retry: !0 });
89
- if (!a.ok)
90
- throw new Error(`HTTP ${a.status}: ${a.statusText}`);
91
- const n = await a.json();
92
- if (n.code !== "200")
93
- throw new Error(n.message || `API error: ${n.code}`);
94
- return h(n.data);
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, t) {
97
- return this.request("GET", e, { params: t });
103
+ get(e, i) {
104
+ return this.request("GET", e, { params: i });
98
105
  }
99
- post(e, t) {
100
- return this.request("POST", e, { body: t });
106
+ post(e, i) {
107
+ return this.request("POST", e, { body: i });
101
108
  }
102
- patch(e, t) {
103
- return this.request("PATCH", e, { body: t });
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, t, s) {
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 o = this.isAbsoluteUrl ? `${this.baseUrl}${e}` : `${this.getOrigin()}${this.baseUrl}${e}`, a = await fetch(o, {
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(t),
116
- signal: s
122
+ body: JSON.stringify(i),
123
+ signal: n
117
124
  });
118
- if (a.status === 401) {
119
- const n = await w(a.clone());
120
- if (n.isCookieExpired && typeof (t == null ? void 0 : t.message) == "string") {
121
- k({
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: t.message,
124
- sessionId: t.sessionId ?? ""
130
+ message: i.message,
131
+ sessionId: i.sessionId ?? ""
125
132
  });
126
- const i = n.loginUrl ?? this.loginUrl;
133
+ const a = s.loginUrl ?? this.loginUrl;
127
134
  return this.onAuthExpired ? this.onAuthExpired({
128
- loginUrl: i,
129
- sessionId: n.sessionId,
135
+ loginUrl: a,
136
+ sessionId: s.sessionId,
130
137
  botId: this.botId
131
- }) : i && window.location.replace(i), a;
138
+ }) : a && window.location.replace(a), t;
132
139
  }
133
140
  if (await this.handleTokenRefresh())
134
- return fetch(o, {
141
+ return fetch(d, {
135
142
  method: "POST",
136
143
  headers: this.headers(),
137
- body: JSON.stringify(t),
138
- signal: s
144
+ body: JSON.stringify(i),
145
+ signal: n
139
146
  });
140
147
  }
141
- return a;
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 I(r) {
161
+ function w(r) {
155
162
  const e = r.split(`
156
- `).map((s) => s.trim()).filter((s) => s.startsWith("data:")).map((s) => s.slice(5).trim());
157
- if (!e.length) return null;
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(e.join(`
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
- return {
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: u(t.output),
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
- // fill_html_template 的 render_mode + html
189
- renderMode: t.render_mode ?? t.renderMode,
190
- html: t.html
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 p(r, e, t) {
271
+ async function T(r, e, i) {
230
272
  if (!r.body) {
231
- t(new Error("Response body is null"));
273
+ i(new Error("Response body is null"));
232
274
  return;
233
275
  }
234
- const s = r.body.getReader(), o = new TextDecoder();
235
- let a = "";
276
+ const n = r.body.getReader(), d = new TextDecoder();
277
+ let t = "";
236
278
  try {
237
279
  for (; ; ) {
238
- const { value: n, done: c } = await s.read();
239
- if (c) break;
240
- a += o.decode(n, { stream: !0 });
241
- let i;
242
- for (; (i = a.indexOf(`
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 d = a.slice(0, i);
246
- a = a.slice(i + 2);
247
- const l = I(d);
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 (a.trim()) {
252
- const n = I(a);
253
- n && e(n);
293
+ if (t.trim()) {
294
+ const s = w(t);
295
+ s && e(s);
254
296
  }
255
- } catch (n) {
256
- (n == null ? void 0 : n.name) !== "AbortError" && t(n instanceof Error ? n : new Error(String(n)));
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
- s.releaseLock();
301
+ n.releaseLock();
260
302
  } catch {
261
303
  }
262
304
  }
263
305
  }
264
- function S(r, e) {
265
- var a;
266
- const t = new AbortController(), s = e.signal ? $(e.signal, t.signal) : t.signal, o = {
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
- ...(a = e.attachments) != null && a.length ? {
273
- attachments: e.attachments.map(({ url: n, ...c }) => c)
314
+ ...(t = e.attachments) != null && t.length ? {
315
+ attachments: e.attachments.map(({ url: s, ...o }) => o)
274
316
  } : {}
275
317
  };
276
- return o.stream ? r.streamPost("/chat", o, s).then((n) => {
277
- var c;
278
- if (!n.ok) {
279
- (c = e.onError) == null || c.call(e, new Error(`HTTP ${n.status}`));
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 p(
283
- n,
284
- (i) => {
285
- var d, l, f, g, m, y;
286
- i.type === "done" && i.sessionId ? (d = e.onDone) == null || d.call(e, { sessionId: i.sessionId, content: i.content, messageId: i.messageId }) : i.type === "done" ? (l = e.onDone) == null || l.call(e, { sessionId: "", content: i.content, messageId: i.messageId }) : i.type === "stop" ? ((f = e.onMessage) == null || f.call(e, i), (g = e.onDone) == null || g.call(e, { sessionId: i.sessionId ?? "", content: i.content, messageId: i.messageId })) : i.type === "error" ? (m = e.onError) == null || m.call(e, new Error(i.content)) : (y = e.onMessage) == null || y.call(e, i);
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
- (i) => {
289
- var d;
290
- return (d = e.onError) == null ? void 0 : d.call(e, i);
336
+ (a) => {
337
+ var c;
338
+ return (c = e.onError) == null ? void 0 : c.call(e, a);
291
339
  }
292
340
  );
293
- }).catch((n) => {
294
- var c;
295
- (n == null ? void 0 : n.name) !== "AbortError" && ((c = e.onError) == null || c.call(e, n instanceof Error ? n : new Error(String(n))));
296
- }) : r.post("/chat", o).then((n) => {
297
- var c;
298
- (c = e.onDone) == null || c.call(e, {
299
- sessionId: n.sessionId,
300
- content: n.content ?? "",
301
- messageId: n.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((n) => {
304
- var c;
305
- (c = e.onError) == null || c.call(e, n instanceof Error ? n : new Error(String(n)));
306
- }), t;
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 $(r, e) {
309
- const t = new AbortController(), s = () => t.abort();
310
- return r.addEventListener("abort", s, { once: !0 }), e.addEventListener("abort", s, { once: !0 }), (r.aborted || e.aborted) && t.abort(), t.signal;
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 A(r) {
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 C(r) {
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 x(r, e) {
328
- const t = await r.get("/chat/conversations", {
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
- ...t,
335
- items: t.items.map(C)
382
+ ...i,
383
+ items: i.items.map(O)
336
384
  };
337
385
  }
338
- async function _(r, e, t) {
339
- await r.patch(`/chat/conversations/${e}`, { title: t });
386
+ async function C(r, e, i) {
387
+ await r.patch(`/chat/conversations/${e}`, { title: i });
340
388
  }
341
- async function E(r, e) {
389
+ async function P(r, e) {
342
390
  await r.del(`/chat/conversations/${e}`);
343
391
  }
344
- async function P(r, e) {
345
- const t = await r.get(`/chat/conversations/${e}/messages`);
346
- return O(t);
392
+ async function U(r, e) {
393
+ const i = await r.get(`/chat/conversations/${e}/messages`);
394
+ return N(i);
347
395
  }
348
- function O(r) {
349
- const e = [...r].sort((n, c) => {
350
- const i = new Date(n.createTime ?? 0).getTime() || 0, d = new Date(c.createTime ?? 0).getTime() || 0;
351
- return i !== d ? i - d : (n.seq ?? 0) - (c.seq ?? 0);
352
- }), t = /* @__PURE__ */ new Map();
353
- for (const n of e)
354
- n.role === "tool" && n.toolCallId && t.set(n.toolCallId, n.content || "");
355
- const s = /* @__PURE__ */ new Set(), o = [], a = (n, c, i) => {
356
- const d = o[o.length - 1];
357
- (d == null ? void 0 : d.role) === "assistant" ? (d.parts.push(...n), d.id = c) : o.push({ id: c, role: "assistant", parts: [...n], timestamp: i });
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 n of e) {
360
- if (n.role === "system") continue;
361
- const c = String(n.id ?? n.messageId ?? `msg_${Date.now()}`), i = new Date(n.createTime ?? "").getTime() || Date.now();
362
- if (n.role === "human") {
363
- const d = (n.attachments ?? []).map((l) => ({
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
- o.push({
376
- id: c,
450
+ d.push({
451
+ id: o,
377
452
  role: "user",
378
- parts: [...d, { type: "text", content: u(n.content) }],
379
- timestamp: i
453
+ parts: [...c, { type: "text", content: u(s.content) }],
454
+ timestamp: a
380
455
  });
381
456
  continue;
382
457
  }
383
- if (n.role === "ai") {
384
- const d = U(n, t, s);
385
- d.length > 0 && a(d, c, i);
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 (n.role === "tool") {
389
- if (n.toolCallId && s.has(n.toolCallId)) continue;
390
- a([N(n)], c, i);
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 o;
468
+ return d;
394
469
  }
395
- function U(r, e, t) {
396
- const s = [];
397
- r.reasoning && s.push({ type: "thinking", content: r.reasoning }), r.content && s.push({ type: "text", content: u(r.content) });
398
- for (const o of r.toolCalls ?? [])
399
- o.is_interrupt ? (t.add(o.id), s.push(v(o, e))) : s.push(D(o));
400
- return s;
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 v(r, e) {
403
- const t = e.has(r.id), s = typeof r.args == "object" ? r.args : {};
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: s.interruptType ?? "input",
409
- content: s.content ?? "",
410
- options: s.options,
411
- placeholder: s.placeholder,
412
- inputType: s.inputType,
413
- maxLength: s.maxLength,
414
- required: s.required,
415
- fields: s.fields,
416
- metadata: s.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: t,
419
- response: t ? { action: "submit", value: q(e.get(r.id)) } : void 0
493
+ resolved: i,
494
+ response: i ? { action: "submit", value: j(e.get(r.id)) } : void 0
420
495
  };
421
496
  }
422
- function D(r) {
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 N(r) {
432
- var t, s, o;
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: ((t = r.metadata) == null ? void 0 : t.renderMode) ?? ((s = r.metadata) == null ? void 0 : s.render_mode),
441
- html: (o = r.metadata) == null ? void 0 : o.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 q(r) {
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 L(r, e, t, s, o, a, n) {
453
- const c = await r.streamPost(
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: t,
457
- action: s.action,
458
- value: s.value
533
+ sessionId: i,
534
+ action: n.action,
535
+ value: n.value
459
536
  },
460
- n
537
+ s
461
538
  );
462
- if (!c.ok) {
463
- const i = new Error(`HTTP ${c.status}`);
464
- throw a == null || a(i), i;
539
+ if (!o.ok) {
540
+ const a = new Error(`HTTP ${o.status}`);
541
+ throw t == null || t(a), a;
465
542
  }
466
- o && await p(
467
- c,
468
- (i) => {
469
- i.type === "error" ? a == null || a(new Error(i.content)) : o(i);
543
+ d && await T(
544
+ o,
545
+ (a) => {
546
+ a.type === "error" ? t == null || t(new Error(a.content)) : d(a);
470
547
  },
471
- (i) => a == null ? void 0 : a(i)
548
+ (a) => t == null ? void 0 : t(a)
472
549
  );
473
550
  }
474
- async function G(r) {
551
+ async function R(r) {
475
552
  return r.get(`/chat/bots/${r.getBotId()}/skills`);
476
553
  }
477
- async function R(r, e) {
554
+ async function B(r, e) {
478
555
  await r.post("/chat/stop", { sessionId: e });
479
556
  }
480
- function J(r) {
481
- const e = new b(r);
557
+ function z(r) {
558
+ const e = new $(r);
482
559
  return {
483
560
  botId: e.getBotId(),
484
561
  getToken: () => e.fetchToken(),
485
- createSession: () => A(e),
486
- chat: (s) => S(e, s),
487
- listConversations: (s) => x(e, s),
488
- renameConversation: (s, o) => _(e, s, o),
489
- deleteConversation: (s) => E(e, s),
490
- getMessages: (s) => P(e, s),
491
- respondInterrupt: (s, o, a, n, c, i) => L(e, s, o, a, n, c, i),
492
- setToken: (s) => e.setToken(s),
493
- feedback: async (s, o, a) => {
494
- const n = { type: o };
495
- o === "dislike" && ((a == null ? void 0 : a.reason) != null && (n.reason = a.reason), a != null && a.remark && (n.remark = a.remark)), await e.post(`/chat/messages/${s}/feedback`, n);
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 (s) => {
498
- await e.del(`/chat/messages/${s}/feedback`);
574
+ cancelFeedback: async (n) => {
575
+ await e.del(`/chat/messages/${n}/feedback`);
499
576
  },
500
- listSkills: () => G(e),
501
- stopGeneration: (s) => R(e, s)
577
+ listSkills: () => R(e),
578
+ stopGeneration: (n) => B(e, n)
502
579
  };
503
580
  }
504
581
  export {
505
- K as createNosUploader,
506
- J as createSuperAgent
582
+ W as createNosUploader,
583
+ z as createSuperAgent
507
584
  };