super-agent-sdk 1.0.15 → 1.0.17

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