super-agent-sdk 1.0.21 → 1.0.23

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 b, s as C } from "./nosUploader-DjJ7u2B1.js";
2
- import { c as W } from "./nosUploader-DjJ7u2B1.js";
3
- function f(s) {
4
- if (Array.isArray(s)) return s.map((e) => f(e));
5
- if (s && typeof s == "object" && s.constructor === Object) {
1
+ import { p as b, s as C } from "./nosUploader-2VXYPLap.js";
2
+ import { c as W } from "./nosUploader-2VXYPLap.js";
3
+ function g(r) {
4
+ if (Array.isArray(r)) return r.map((e) => g(e));
5
+ if (r && typeof r == "object" && r.constructor === Object) {
6
6
  const e = {};
7
- for (const [i, n] of Object.entries(s)) {
8
- const d = i.replace(/_([a-z])/g, (t, r) => r.toUpperCase());
9
- e[d] = f(n);
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);
10
10
  }
11
11
  return e;
12
12
  }
13
- return s;
13
+ return r;
14
14
  }
15
15
  const S = "/api/agent/runtime/v1";
16
16
  class A {
17
17
  constructor(e) {
18
18
  this.refreshing = null, this.tokenPromise = null;
19
- const i = e.baseUrl ?? S;
20
- this.baseUrl = i.replace(/\/$/, ""), this.tokenGateway = e.tokenGateway.replace(/\/$/, ""), this.appId = e.appId ?? "", this.token = e.token ?? "", this.botCode = e.botCode, this.tenantId = e.tenantId, this.isAbsoluteUrl = /^https?:\/\//.test(this.baseUrl), this.isAbsoluteGateway = /^https?:\/\//.test(this.tokenGateway), this.loginUrl = e.loginUrl, this.onAuthExpired = e.onAuthExpired;
19
+ const o = e.baseUrl ?? S;
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;
21
21
  }
22
22
  getBotCode() {
23
23
  return this.botCode;
@@ -42,13 +42,13 @@ class A {
42
42
  if (this.token) return this.token;
43
43
  if (this.tokenPromise) return this.tokenPromise;
44
44
  this.tokenPromise = (async () => {
45
- const i = `${this.isAbsoluteGateway ? this.tokenGateway : `${this.getOrigin()}${this.tokenGateway}`}/superagent/v2/token/generate`, n = {
45
+ const o = `${this.isAbsoluteGateway ? this.tokenGateway : `${this.getOrigin()}${this.tokenGateway}`}/superagent/v2/token/generate`, s = {
46
46
  "Content-Type": "application/json"
47
47
  };
48
- this.tenantId != null && (n["X-Tenant-Id"] = String(this.tenantId));
49
- const d = await fetch(i, {
48
+ this.tenantId != null && (s["X-Tenant-Id"] = String(this.tenantId));
49
+ const d = await fetch(o, {
50
50
  method: "POST",
51
- headers: n,
51
+ headers: s,
52
52
  body: JSON.stringify({ botCode: this.botCode })
53
53
  });
54
54
  if (!d.ok)
@@ -56,8 +56,8 @@ class A {
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 r = f(t.data);
60
- return this.token = r.token, r.appId && !this.appId && (this.appId = r.appId), r.token;
59
+ const n = g(t.data);
60
+ return this.token = n.token, n.appId && !this.appId && (this.appId = n.appId), n.token;
61
61
  })();
62
62
  try {
63
63
  return await this.tokenPromise;
@@ -86,109 +86,109 @@ class A {
86
86
  this.refreshing = null;
87
87
  }
88
88
  }
89
- async request(e, i, n) {
89
+ async request(e, o, s) {
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}${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}`);
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}`);
99
99
  }
100
100
  const t = await fetch(d, {
101
101
  method: e,
102
102
  headers: this.headers(),
103
- body: n != null && n.body ? JSON.stringify(n.body) : void 0
103
+ body: s != null && s.body ? JSON.stringify(s.body) : void 0
104
104
  });
105
- if (t.status === 401 && !(n != null && n.retry) && await this.handleTokenRefresh())
106
- return this.request(e, i, { ...n, retry: !0 });
105
+ if (t.status === 401 && !(s != null && s.retry) && await this.handleTokenRefresh())
106
+ return this.request(e, o, { ...s, retry: !0 });
107
107
  if (!t.ok) {
108
- let o = `HTTP ${t.status}: ${t.statusText}`;
108
+ let a = `HTTP ${t.status}: ${t.statusText}`;
109
109
  try {
110
- const a = await t.json();
111
- a != null && a.message && (o = a.message);
110
+ const i = await t.json();
111
+ i != null && i.message && (a = i.message);
112
112
  } catch {
113
113
  }
114
- throw new Error(o);
114
+ throw new Error(a);
115
115
  }
116
- const r = await t.json();
117
- if (r.code !== "200")
118
- throw new Error(r.message || `API error: ${r.code}`);
119
- return f(r.data);
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);
120
120
  }
121
- get(e, i) {
122
- return this.request("GET", e, { params: i });
121
+ get(e, o) {
122
+ return this.request("GET", e, { params: o });
123
123
  }
124
- post(e, i) {
125
- return this.request("POST", e, { body: i });
124
+ post(e, o) {
125
+ return this.request("POST", e, { body: o });
126
126
  }
127
- patch(e, i) {
128
- return this.request("PATCH", e, { body: i });
127
+ patch(e, o) {
128
+ return this.request("PATCH", e, { body: o });
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, i, n) {
134
+ async streamPost(e, o, s) {
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(i),
141
- signal: n
140
+ body: JSON.stringify(o),
141
+ signal: s
142
142
  });
143
143
  if (t.status === 401) {
144
- const r = await b(t.clone());
145
- if (r.isCookieExpired && typeof (i == null ? void 0 : i.message) == "string") {
144
+ const n = await b(t.clone());
145
+ if (n.isCookieExpired && typeof (o == null ? void 0 : o.message) == "string") {
146
146
  C({
147
147
  botCode: this.botCode,
148
- message: i.message,
149
- sessionId: i.sessionId ?? ""
148
+ message: o.message,
149
+ sessionId: o.sessionId ?? ""
150
150
  });
151
- const a = r.loginUrl ?? this.loginUrl;
151
+ const i = n.loginUrl ?? this.loginUrl;
152
152
  return this.onAuthExpired ? this.onAuthExpired({
153
- loginUrl: a,
154
- sessionId: r.sessionId,
153
+ loginUrl: i,
154
+ sessionId: n.sessionId,
155
155
  botCode: this.botCode
156
- }) : a && window.location.replace(a), t;
156
+ }) : i && window.location.replace(i), 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(i),
163
- signal: n
162
+ body: JSON.stringify(o),
163
+ signal: s
164
164
  });
165
165
  }
166
166
  return t;
167
167
  }
168
168
  }
169
- function u(s) {
170
- return typeof s == "string" ? s : Array.isArray(s) ? s.map((e) => {
169
+ function h(r) {
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") {
173
173
  if (typeof e.text == "string") return e.text;
174
174
  if (e.type) return `[${e.type}${e.mime_type ? ": " + e.mime_type : ""}]`;
175
175
  }
176
176
  return "";
177
- }).join("") : s == null ? "" : String(s);
177
+ }).join("") : r == null ? "" : String(r);
178
178
  }
179
- function w(s) {
180
- const e = s.split(`
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();
179
+ function w(r) {
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();
190
190
  d = d ? `${d}
191
- ${a}` : a;
191
+ ${i}` : i;
192
192
  }
193
193
  if (d === void 0) return null;
194
194
  let t;
@@ -197,7 +197,7 @@ ${a}` : a;
197
197
  } catch {
198
198
  return null;
199
199
  }
200
- const r = t.type === "tool_result" && typeof t.content == "object" && t.content ? t.content : void 0;
200
+ const n = 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,15 +205,15 @@ ${a}` : a;
205
205
  content: "",
206
206
  sessionId: t.sessionId ?? t.session_id ?? "",
207
207
  roundId: t.roundId ?? t.round_id,
208
- event: n,
209
- id: i
208
+ event: s,
209
+ id: o
210
210
  };
211
211
  case "user":
212
- return { type: "user", content: u(t.content), event: n, id: i };
212
+ return { type: "user", content: h(t.content), event: s, id: o };
213
213
  case "thinking":
214
- return { type: "thinking", content: u(t.content), event: n, id: i };
214
+ return { type: "thinking", content: h(t.content), event: s, id: o };
215
215
  case "ai":
216
- return { type: "ai", content: u(t.content), event: n, id: i };
216
+ return { type: "ai", content: h(t.content), event: s, id: o };
217
217
  case "tool_call":
218
218
  return {
219
219
  type: "tool_call",
@@ -221,56 +221,57 @@ ${a}` : a;
221
221
  toolName: t.name,
222
222
  toolCallId: t.id,
223
223
  args: typeof t.args == "string" ? t.args : JSON.stringify(t.args ?? {}),
224
- event: n,
225
- id: i
224
+ event: s,
225
+ id: o
226
226
  };
227
227
  case "tool_delta":
228
228
  return {
229
229
  type: "tool_delta",
230
230
  content: typeof t.content == "string" ? t.content : "",
231
231
  toolCallId: t.tool_call_id ?? t.toolCallId ?? "",
232
- event: n,
233
- id: i
232
+ event: s,
233
+ id: o
234
234
  };
235
235
  case "tool_result": {
236
- const o = r == null ? void 0 : r.result;
237
- let a;
238
- return typeof o == "string" ? a = o : o != null && typeof o == "object" ? a = JSON.stringify(o) : r && typeof r == "object" && Object.keys(r).length > 0 ? a = JSON.stringify(r) : a = u(t.output ?? ""), {
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 ?? ""), {
239
239
  type: "tool_result",
240
- content: a,
240
+ content: i,
241
241
  toolName: t.name,
242
242
  toolCallId: t.id,
243
243
  artifacts: Array.isArray(t.artifacts) ? t.artifacts : void 0,
244
- renderMode: (r == null ? void 0 : r.render_mode) ?? t.render_mode ?? t.renderMode,
245
- html: (r == null ? void 0 : r.html) ?? t.html,
246
- markdown: (r == null ? void 0 : r.markdown) ?? t.markdown,
247
- event: n,
248
- id: i
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,
247
+ defaultExpanded: t.default_expanded ?? t.defaultExpanded,
248
+ event: s,
249
+ id: o
249
250
  };
250
251
  }
251
252
  case "done":
252
253
  return {
253
254
  type: "done",
254
- content: u(t.content),
255
+ content: h(t.content),
255
256
  sessionId: t.sessionId ?? t.session_id ?? "",
256
257
  messageId: t.messageId ?? t.message_id,
257
258
  roundId: t.roundId ?? t.round_id,
258
259
  artifacts: Array.isArray(t.artifacts) ? t.artifacts : void 0,
259
- event: n,
260
- id: i
260
+ event: s,
261
+ id: o
261
262
  };
262
263
  case "stop":
263
264
  return {
264
265
  type: "stop",
265
- content: u(t.content),
266
+ content: h(t.content),
266
267
  sessionId: t.sessionId ?? t.session_id ?? "",
267
268
  messageId: t.messageId ?? t.message_id,
268
269
  roundId: t.roundId ?? t.round_id,
269
- event: n,
270
- id: i
270
+ event: s,
271
+ id: o
271
272
  };
272
273
  case "error":
273
- return { type: "error", content: t.message ?? "Unknown error", event: n, id: i };
274
+ return { type: "error", content: t.message ?? "Unknown error", event: s, id: o };
274
275
  case "interrupt":
275
276
  return {
276
277
  type: "interrupt",
@@ -287,322 +288,342 @@ ${a}` : a;
287
288
  fields: t.fields,
288
289
  metadata: t.metadata
289
290
  },
290
- event: n,
291
- id: i
291
+ event: s,
292
+ id: o
292
293
  };
293
294
  default:
294
295
  return null;
295
296
  }
296
297
  }
297
- async function T(s, e, i) {
298
- if (!s.body) {
299
- i(new Error("Response body is null"));
298
+ async function T(r, e, o) {
299
+ if (!r.body) {
300
+ o(new Error("Response body is null"));
300
301
  return;
301
302
  }
302
- const n = s.body.getReader(), d = new TextDecoder();
303
+ const s = r.body.getReader(), d = new TextDecoder();
303
304
  let t = "";
304
305
  try {
305
306
  for (; ; ) {
306
- const { value: r, done: o } = await n.read();
307
- if (o) break;
308
- t += d.decode(r, { stream: !0 });
309
- let a;
310
- for (; (a = t.indexOf(`
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(`
311
312
 
312
313
  `)) !== -1; ) {
313
- const c = t.slice(0, a);
314
- t = t.slice(a + 2);
314
+ const c = t.slice(0, i);
315
+ t = t.slice(i + 2);
315
316
  const l = w(c);
316
317
  l && e(l);
317
318
  }
318
319
  }
319
320
  if (t.trim()) {
320
- const r = w(t);
321
- r && e(r);
321
+ const n = w(t);
322
+ n && e(n);
322
323
  }
323
- } catch (r) {
324
- (r == null ? void 0 : r.name) !== "AbortError" && i(r instanceof Error ? r : new Error(String(r)));
324
+ } catch (n) {
325
+ (n == null ? void 0 : n.name) !== "AbortError" && o(n instanceof Error ? n : new Error(String(n)));
325
326
  } finally {
326
327
  try {
327
- n.releaseLock();
328
+ s.releaseLock();
328
329
  } catch {
329
330
  }
330
331
  }
331
332
  }
332
- function $(s, e) {
333
+ function _(r, e) {
333
334
  var t;
334
- const i = new AbortController(), n = e.signal ? _(e.signal, i.signal) : i.signal, d = {
335
- botCode: s.getBotCode(),
335
+ const o = new AbortController(), s = e.signal ? $(e.signal, o.signal) : o.signal, d = {
336
+ botCode: r.getBotCode(),
336
337
  message: e.message,
337
338
  sessionId: e.sessionId,
338
339
  stream: e.stream !== !1,
339
340
  // url 仅前端回显(下载/预览),剥离后发送——签名 URL 不进后端
340
341
  ...(t = e.attachments) != null && t.length ? {
341
- attachments: e.attachments.map(({ url: r, ...o }) => o)
342
+ attachments: e.attachments.map(({ url: n, ...a }) => a)
342
343
  } : {}
343
344
  };
344
- return d.stream ? s.streamPost("/chat", d, n).then(async (r) => {
345
- var o;
346
- if (!r.ok) {
347
- let a = `HTTP ${r.status}`;
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}`;
348
349
  try {
349
- const c = await r.json();
350
- c != null && c.message && (a = c.message);
350
+ const c = await n.json();
351
+ c != null && c.message && (i = c.message);
351
352
  } catch {
352
353
  }
353
- (o = e.onError) == null || o.call(e, new Error(a));
354
+ (a = e.onError) == null || a.call(e, new Error(i));
354
355
  return;
355
356
  }
356
357
  return T(
357
- r,
358
- (a) => {
359
- var c, l, h, g, m, y, I, p;
360
- a.type === "done" && a.sessionId ? ((c = e.onMessage) == null || c.call(e, a), (l = e.onDone) == null || l.call(e, { sessionId: a.sessionId, content: a.content, messageId: a.messageId, roundId: a.roundId, artifacts: a.artifacts })) : a.type === "done" ? ((h = e.onMessage) == null || h.call(e, a), (g = e.onDone) == null || g.call(e, { sessionId: "", content: a.content, messageId: a.messageId, roundId: a.roundId, artifacts: a.artifacts })) : a.type === "stop" ? ((m = e.onMessage) == null || m.call(e, a), (y = e.onDone) == null || y.call(e, { sessionId: a.sessionId ?? "", content: a.content, messageId: a.messageId, roundId: a.roundId })) : a.type === "error" ? (I = e.onError) == null || I.call(e, new Error(a.content)) : (p = e.onMessage) == null || p.call(e, a);
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);
361
362
  },
362
- (a) => {
363
+ (i) => {
363
364
  var c;
364
- return (c = e.onError) == null ? void 0 : c.call(e, a);
365
+ return (c = e.onError) == null ? void 0 : c.call(e, i);
365
366
  }
366
367
  );
367
- }).catch((r) => {
368
- var o;
369
- (r == null ? void 0 : r.name) !== "AbortError" && ((o = e.onError) == null || o.call(e, r instanceof Error ? r : new Error(String(r))));
370
- }) : s.post("/chat", d).then((r) => {
371
- var o;
372
- (o = e.onDone) == null || o.call(e, {
373
- sessionId: r.sessionId,
374
- content: r.content ?? "",
375
- messageId: r.messageId
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
376
377
  });
377
- }).catch((r) => {
378
- var o;
379
- (o = e.onError) == null || o.call(e, r instanceof Error ? r : new Error(String(r)));
380
- }), i;
378
+ }).catch((n) => {
379
+ var a;
380
+ (a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n)));
381
+ }), o;
381
382
  }
382
- function _(s, e) {
383
- const i = new AbortController(), n = () => i.abort();
384
- return s.addEventListener("abort", n, { once: !0 }), e.addEventListener("abort", n, { once: !0 }), (s.aborted || e.aborted) && i.abort(), i.signal;
383
+ function $(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;
385
386
  }
386
- async function P(s) {
387
- return (await s.post("/chat/sessions", {
388
- botCode: s.getBotCode()
387
+ async function x(r) {
388
+ return (await r.post("/chat/sessions", {
389
+ botCode: r.getBotCode()
389
390
  })).sessionId;
390
391
  }
391
- function x(s) {
392
+ function E(r) {
392
393
  return {
393
394
  // 后端返回 thread_id,toCamelCase 转为 threadId,映射为 sessionId
394
- sessionId: s.threadId ?? s.sessionId,
395
+ sessionId: r.threadId ?? r.sessionId,
395
396
  // bot 外部标识统一 botCode(createSuperAgent 入参,全链消费);
396
397
  // 后端回传的内部 bot 标识字段 SDK 不消费、不暴露
397
- title: s.title,
398
- createTime: s.createTime,
399
- updateTime: s.updateTime
398
+ title: r.title,
399
+ createTime: r.createTime,
400
+ updateTime: r.updateTime
400
401
  };
401
402
  }
402
- async function E(s, e) {
403
- const i = await s.get("/chat/conversations", {
404
- botCode: s.getBotCode(),
403
+ async function P(r, e) {
404
+ const o = await r.get("/chat/conversations", {
405
+ botCode: r.getBotCode(),
405
406
  page: (e == null ? void 0 : e.page) ?? 1,
406
407
  size: (e == null ? void 0 : e.size) ?? 20
407
408
  });
408
409
  return {
409
- ...i,
410
- items: i.items.map(x)
410
+ ...o,
411
+ items: o.items.map(E)
411
412
  };
412
413
  }
413
- async function v(s, e, i) {
414
- await s.patch(`/chat/conversations/${e}`, { title: i });
414
+ async function v(r, e, o) {
415
+ await r.patch(`/chat/conversations/${e}`, { title: o });
415
416
  }
416
- async function O(s, e) {
417
- await s.del(`/chat/conversations/${e}`);
417
+ async function O(r, e) {
418
+ await r.del(`/chat/conversations/${e}`);
418
419
  }
419
- async function U(s, e) {
420
- const i = await s.get(`/chat/conversations/${e}/messages`);
421
- return N(i);
420
+ async function U(r, e) {
421
+ const o = await r.get(
422
+ `/chat/conversations/${e}/messages`
423
+ );
424
+ return M(o);
422
425
  }
423
426
  function k() {
424
427
  return `r_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
425
428
  }
426
- function N(s) {
429
+ function M(r) {
427
430
  var d;
428
- const e = s.length > 0 && Array.isArray((d = s[0]) == null ? void 0 : d.data), i = [];
431
+ const e = r.length > 0 && Array.isArray((d = r[0]) == null ? void 0 : d.data), o = [];
429
432
  if (e)
430
- for (const t of s)
431
- i.push({
433
+ for (const t of r)
434
+ o.push({
432
435
  roundId: t.roundId ?? k(),
433
436
  artifacts: Array.isArray(t.artifacts) && t.artifacts.length ? t.artifacts : void 0,
434
437
  items: Array.isArray(t.data) ? t.data : []
435
438
  });
436
439
  else
437
- i.push({ roundId: k(), items: s });
438
- const n = [];
439
- for (const t of i) {
440
- const r = D(t.items);
441
- for (const o of r) o.roundId = t.roundId;
440
+ o.push({ roundId: k(), items: r });
441
+ const s = [];
442
+ for (const t of o) {
443
+ const n = N(t.items);
444
+ for (const a of n) a.roundId = t.roundId;
442
445
  if (t.artifacts) {
443
- const o = r.findLast((a) => a.role === "assistant");
444
- o && (o.artifacts = t.artifacts);
446
+ const a = n.findLast((i) => i.role === "assistant");
447
+ a && (a.artifacts = t.artifacts);
445
448
  }
446
- n.push(...r);
449
+ s.push(...n);
447
450
  }
448
- return n;
451
+ return s;
449
452
  }
450
- function D(s) {
451
- const e = [...s].sort((r, o) => {
452
- const a = new Date(r.createTime ?? 0).getTime() || 0, c = new Date(o.createTime ?? 0).getTime() || 0;
453
- return a !== c ? a - c : (r.seq ?? 0) - (o.seq ?? 0);
454
- }), i = /* @__PURE__ */ new Map();
455
- for (const r of e)
456
- r.role === "tool" && r.toolCallId && i.set(r.toolCallId, r.content || "");
457
- const n = /* @__PURE__ */ new Set(), d = [], t = (r, o, a) => {
458
- const c = d[d.length - 1];
459
- (c == null ? void 0 : c.role) === "assistant" ? (c.parts.push(...r), c.id = o) : d.push({ id: o, role: "assistant", parts: [...r], timestamp: a });
453
+ function N(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,
465
+ role: "assistant",
466
+ parts: [...a],
467
+ timestamp: c,
468
+ messageId: l,
469
+ feedback: f ?? null
470
+ });
460
471
  };
461
- for (const r of e) {
462
- if (r.role === "system") continue;
463
- const o = String(r.id ?? r.messageId ?? `msg_${Date.now()}`), a = new Date(r.createTime ?? "").getTime() || Date.now();
464
- if (r.role === "human") {
465
- const c = (r.attachments ?? []).map((l) => ({
466
- type: "attachment",
467
- attachment: {
468
- nosKey: l.nosKey ?? l.attachmentId ?? "",
469
- // 历史回显后端不带 nosKey,占位空串
470
- filename: l.filename ?? "file",
471
- size: l.size ?? void 0,
472
- mime: l.mime ?? void 0,
473
- url: l.url ?? void 0
474
- // 公开读桶下载链接(有则 chip 可点击下载)
475
- }
476
- }));
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) => ({
478
+ type: "attachment",
479
+ attachment: {
480
+ nosKey: u.nosKey ?? u.attachmentId ?? "",
481
+ // 历史回显后端不带 nosKey,占位空串
482
+ filename: u.filename ?? "file",
483
+ size: u.size ?? void 0,
484
+ mime: u.mime ?? void 0,
485
+ url: u.url ?? void 0
486
+ // 公开读桶下载链接(有则 chip 可点击下载)
487
+ }
488
+ })
489
+ ), f = (n = a.metadata) == null ? void 0 : n.raw_message;
477
490
  d.push({
478
- id: o,
491
+ id: i,
479
492
  role: "user",
480
- parts: [...c, { type: "text", content: u(r.content) }],
481
- timestamp: a
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
482
497
  });
483
498
  continue;
484
499
  }
485
- if (r.role === "ai") {
486
- const c = M(r, i, n);
487
- c.length > 0 && t(c, o, a);
500
+ if (a.role === "ai") {
501
+ const l = D(a, o, s);
502
+ l.length > 0 && t(l, i, c, a.messageId, a.feedback);
488
503
  continue;
489
504
  }
490
- if (r.role === "tool") {
491
- if (r.toolCallId && n.has(r.toolCallId)) continue;
492
- t([L(r)], o, a);
505
+ if (a.role === "tool") {
506
+ if (a.toolCallId && s.has(a.toolCallId)) continue;
507
+ t([L(a)], i, c);
493
508
  }
494
509
  }
495
510
  return d;
496
511
  }
497
- function M(s, e, i) {
498
- const n = [];
499
- s.reasoning && n.push({ type: "thinking", content: s.reasoning }), s.content && n.push({ type: "text", content: u(s.content) });
500
- for (const d of s.toolCalls ?? [])
501
- d.is_interrupt ?? d.isInterrupt ? (i.add(d.id), n.push(q(d, e))) : n.push(J(d));
502
- return n;
512
+ function D(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(q(d, e))) : s.push(J(d));
517
+ return s;
503
518
  }
504
- function q(s, e) {
505
- const i = e.has(s.id), n = typeof s.args == "object" ? s.args : {};
519
+ function q(r, e) {
520
+ const o = e.has(r.id), s = typeof r.args == "object" ? r.args : {};
506
521
  return {
507
522
  type: "interrupt",
508
523
  interrupt: {
509
- interruptId: s.id ?? "",
510
- interruptType: n.interruptType ?? "input",
511
- content: n.content ?? "",
512
- options: n.options,
513
- placeholder: n.placeholder,
514
- inputType: n.inputType,
515
- maxLength: n.maxLength,
516
- required: n.required,
517
- fields: n.fields,
518
- metadata: n.metadata
524
+ 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
519
534
  },
520
- resolved: i,
521
- response: i ? { action: "submit", value: j(e.get(s.id)) } : void 0
535
+ resolved: o,
536
+ response: o ? { action: "submit", value: j(e.get(r.id)) } : void 0
522
537
  };
523
538
  }
524
- function J(s) {
539
+ function J(r) {
525
540
  let e;
526
541
  try {
527
- e = typeof s.args == "string" ? s.args : JSON.stringify(s.args ?? {});
542
+ e = typeof r.args == "string" ? r.args : JSON.stringify(r.args ?? {});
528
543
  } catch {
529
- e = String(s.args ?? "{}");
544
+ e = String(r.args ?? "{}");
530
545
  }
531
- return { type: "tool_call", toolName: s.name ?? "", toolCallId: s.id ?? "", args: e };
546
+ return {
547
+ type: "tool_call",
548
+ toolName: r.name ?? "",
549
+ toolCallId: r.id ?? "",
550
+ args: e
551
+ };
532
552
  }
533
- function L(s) {
534
- var i, n, d, t;
535
- const e = Array.isArray(s.artifacts) ? s.artifacts : void 0;
553
+ function L(r) {
554
+ var o, s, d, t, n;
555
+ const e = Array.isArray(r.artifacts) ? r.artifacts : void 0;
536
556
  return {
537
557
  type: "tool_result",
538
- toolName: s.toolName ?? "",
539
- toolCallId: s.toolCallId ?? "",
558
+ toolName: r.toolName ?? "",
559
+ toolCallId: r.toolCallId ?? "",
540
560
  // tool 行后端 content 即展示摘要(_build_tool_fields 与 SSE 口径一致),直接读 content
541
- content: u(s.content),
561
+ content: h(r.content),
542
562
  artifacts: e,
543
- renderMode: s.renderMode ?? ((i = s.metadata) == null ? void 0 : i.renderMode) ?? ((n = s.metadata) == null ? void 0 : n.render_mode),
544
- html: s.html ?? ((d = s.metadata) == null ? void 0 : d.html),
545
- markdown: s.markdown ?? ((t = s.metadata) == null ? void 0 : t.markdown)
563
+ renderMode: r.renderMode ?? ((o = r.metadata) == null ? void 0 : o.renderMode) ?? ((s = r.metadata) == null ? void 0 : s.render_mode),
564
+ html: r.html ?? ((d = r.metadata) == null ? void 0 : d.html),
565
+ markdown: r.markdown ?? ((t = r.metadata) == null ? void 0 : t.markdown),
566
+ defaultExpanded: r.defaultExpanded ?? ((n = r.metadata) == null ? void 0 : n.default_expanded)
546
567
  };
547
568
  }
548
- function j(s) {
549
- if (s != null)
569
+ function j(r) {
570
+ if (r != null)
550
571
  try {
551
- return JSON.parse(s);
572
+ return JSON.parse(r);
552
573
  } catch {
553
- return s;
574
+ return r;
554
575
  }
555
576
  }
556
- async function G(s, e, i, n, d, t, r) {
557
- const o = await s.streamPost(
577
+ async function G(r, e, o, s, d, t, n) {
578
+ const a = await r.streamPost(
558
579
  `/chat/interrupt/${e}/respond`,
559
580
  {
560
- sessionId: i,
561
- action: n.action,
562
- value: n.value
581
+ sessionId: o,
582
+ action: s.action,
583
+ value: s.value
563
584
  },
564
- r
585
+ n
565
586
  );
566
- if (!o.ok) {
567
- const a = new Error(`HTTP ${o.status}`);
568
- throw t == null || t(a), a;
587
+ if (!a.ok) {
588
+ const i = new Error(`HTTP ${a.status}`);
589
+ throw t == null || t(i), i;
569
590
  }
570
591
  d && await T(
571
- o,
572
- (a) => {
573
- a.type === "error" ? t == null || t(new Error(a.content)) : d(a);
592
+ a,
593
+ (i) => {
594
+ i.type === "error" ? t == null || t(new Error(i.content)) : d(i);
574
595
  },
575
- (a) => t == null ? void 0 : t(a)
596
+ (i) => t == null ? void 0 : t(i)
576
597
  );
577
598
  }
578
- async function R(s) {
579
- return s.get(`/chat/bots/${s.getBotCode()}/skills`);
599
+ async function R(r) {
600
+ return r.get(`/chat/bots/${r.getBotCode()}/skills`);
580
601
  }
581
- async function B(s, e) {
582
- await s.post("/chat/stop", { sessionId: e });
602
+ async function B(r, e) {
603
+ await r.post("/chat/stop", { sessionId: e });
583
604
  }
584
- function z(s) {
585
- const e = new A(s);
605
+ function z(r) {
606
+ const e = new A(r);
586
607
  return {
587
608
  botCode: e.getBotCode(),
588
609
  getToken: () => e.fetchToken(),
589
- createSession: () => P(e),
590
- chat: (n) => $(e, n),
591
- listConversations: (n) => E(e, n),
592
- renameConversation: (n, d) => v(e, n, d),
593
- deleteConversation: (n) => O(e, n),
594
- getMessages: (n) => U(e, n),
595
- respondInterrupt: (n, d, t, r, o, a) => G(e, n, d, t, r, o, a),
596
- setToken: (n) => e.setToken(n),
597
- feedback: async (n, d, t) => {
598
- const r = { type: d };
599
- d === "dislike" && ((t == null ? void 0 : t.reason) != null && (r.reason = t.reason), t != null && t.remark && (r.remark = t.remark)), await e.post(`/chat/messages/${n}/feedback`, r);
610
+ createSession: () => x(e),
611
+ chat: (s) => _(e, s),
612
+ listConversations: (s) => P(e, s),
613
+ renameConversation: (s, d) => v(e, s, d),
614
+ deleteConversation: (s) => O(e, s),
615
+ getMessages: (s) => U(e, s),
616
+ respondInterrupt: (s, d, t, n, a, i) => G(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);
600
621
  },
601
- cancelFeedback: async (n) => {
602
- await e.del(`/chat/messages/${n}/feedback`);
622
+ cancelFeedback: async (s) => {
623
+ await e.del(`/chat/messages/${s}/feedback`);
603
624
  },
604
625
  listSkills: () => R(e),
605
- stopGeneration: (n) => B(e, n)
626
+ stopGeneration: (s) => B(e, s)
606
627
  };
607
628
  }
608
629
  export {