super-agent-sdk 1.0.20 → 1.0.22

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,56 @@ ${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
+ event: s,
248
+ id: o
249
249
  };
250
250
  }
251
251
  case "done":
252
252
  return {
253
253
  type: "done",
254
- content: u(t.content),
254
+ content: h(t.content),
255
255
  sessionId: t.sessionId ?? t.session_id ?? "",
256
256
  messageId: t.messageId ?? t.message_id,
257
257
  roundId: t.roundId ?? t.round_id,
258
258
  artifacts: Array.isArray(t.artifacts) ? t.artifacts : void 0,
259
- event: n,
260
- id: i
259
+ event: s,
260
+ id: o
261
261
  };
262
262
  case "stop":
263
263
  return {
264
264
  type: "stop",
265
- content: u(t.content),
265
+ content: h(t.content),
266
266
  sessionId: t.sessionId ?? t.session_id ?? "",
267
267
  messageId: t.messageId ?? t.message_id,
268
268
  roundId: t.roundId ?? t.round_id,
269
- event: n,
270
- id: i
269
+ event: s,
270
+ id: o
271
271
  };
272
272
  case "error":
273
- return { type: "error", content: t.message ?? "Unknown error", event: n, id: i };
273
+ return { type: "error", content: t.message ?? "Unknown error", event: s, id: o };
274
274
  case "interrupt":
275
275
  return {
276
276
  type: "interrupt",
@@ -287,322 +287,340 @@ ${a}` : a;
287
287
  fields: t.fields,
288
288
  metadata: t.metadata
289
289
  },
290
- event: n,
291
- id: i
290
+ event: s,
291
+ id: o
292
292
  };
293
293
  default:
294
294
  return null;
295
295
  }
296
296
  }
297
- async function T(s, e, i) {
298
- if (!s.body) {
299
- i(new Error("Response body is null"));
297
+ async function T(r, e, o) {
298
+ if (!r.body) {
299
+ o(new Error("Response body is null"));
300
300
  return;
301
301
  }
302
- const n = s.body.getReader(), d = new TextDecoder();
302
+ const s = r.body.getReader(), d = new TextDecoder();
303
303
  let t = "";
304
304
  try {
305
305
  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(`
306
+ const { value: n, done: a } = await s.read();
307
+ if (a) break;
308
+ t += d.decode(n, { stream: !0 });
309
+ let i;
310
+ for (; (i = t.indexOf(`
311
311
 
312
312
  `)) !== -1; ) {
313
- const c = t.slice(0, a);
314
- t = t.slice(a + 2);
313
+ const c = t.slice(0, i);
314
+ t = t.slice(i + 2);
315
315
  const l = w(c);
316
316
  l && e(l);
317
317
  }
318
318
  }
319
319
  if (t.trim()) {
320
- const r = w(t);
321
- r && e(r);
320
+ const n = w(t);
321
+ n && e(n);
322
322
  }
323
- } catch (r) {
324
- (r == null ? void 0 : r.name) !== "AbortError" && i(r instanceof Error ? r : new Error(String(r)));
323
+ } catch (n) {
324
+ (n == null ? void 0 : n.name) !== "AbortError" && o(n instanceof Error ? n : new Error(String(n)));
325
325
  } finally {
326
326
  try {
327
- n.releaseLock();
327
+ s.releaseLock();
328
328
  } catch {
329
329
  }
330
330
  }
331
331
  }
332
- function $(s, e) {
332
+ function _(r, e) {
333
333
  var t;
334
- const i = new AbortController(), n = e.signal ? _(e.signal, i.signal) : i.signal, d = {
335
- botCode: s.getBotCode(),
334
+ const o = new AbortController(), s = e.signal ? $(e.signal, o.signal) : o.signal, d = {
335
+ botCode: r.getBotCode(),
336
336
  message: e.message,
337
337
  sessionId: e.sessionId,
338
338
  stream: e.stream !== !1,
339
339
  // url 仅前端回显(下载/预览),剥离后发送——签名 URL 不进后端
340
340
  ...(t = e.attachments) != null && t.length ? {
341
- attachments: e.attachments.map(({ url: r, ...o }) => o)
341
+ attachments: e.attachments.map(({ url: n, ...a }) => a)
342
342
  } : {}
343
343
  };
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}`;
344
+ return d.stream ? r.streamPost("/chat", d, s).then(async (n) => {
345
+ var a;
346
+ if (!n.ok) {
347
+ let i = `HTTP ${n.status}`;
348
348
  try {
349
- const c = await r.json();
350
- c != null && c.message && (a = c.message);
349
+ const c = await n.json();
350
+ c != null && c.message && (i = c.message);
351
351
  } catch {
352
352
  }
353
- (o = e.onError) == null || o.call(e, new Error(a));
353
+ (a = e.onError) == null || a.call(e, new Error(i));
354
354
  return;
355
355
  }
356
356
  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);
357
+ n,
358
+ (i) => {
359
+ var c, l, u, f, m, y, I, p;
360
+ 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" ? ((u = e.onMessage) == null || u.call(e, i), (f = e.onDone) == null || f.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
361
  },
362
- (a) => {
362
+ (i) => {
363
363
  var c;
364
- return (c = e.onError) == null ? void 0 : c.call(e, a);
364
+ return (c = e.onError) == null ? void 0 : c.call(e, i);
365
365
  }
366
366
  );
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
367
+ }).catch((n) => {
368
+ var a;
369
+ (n == null ? void 0 : n.name) !== "AbortError" && ((a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n))));
370
+ }) : r.post("/chat", d).then((n) => {
371
+ var a;
372
+ (a = e.onDone) == null || a.call(e, {
373
+ sessionId: n.sessionId,
374
+ content: n.content ?? "",
375
+ messageId: n.messageId
376
376
  });
377
- }).catch((r) => {
378
- var o;
379
- (o = e.onError) == null || o.call(e, r instanceof Error ? r : new Error(String(r)));
380
- }), i;
377
+ }).catch((n) => {
378
+ var a;
379
+ (a = e.onError) == null || a.call(e, n instanceof Error ? n : new Error(String(n)));
380
+ }), o;
381
381
  }
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;
382
+ function $(r, e) {
383
+ const o = new AbortController(), s = () => o.abort();
384
+ return r.addEventListener("abort", s, { once: !0 }), e.addEventListener("abort", s, { once: !0 }), (r.aborted || e.aborted) && o.abort(), o.signal;
385
385
  }
386
- async function P(s) {
387
- return (await s.post("/chat/sessions", {
388
- botCode: s.getBotCode()
386
+ async function P(r) {
387
+ return (await r.post("/chat/sessions", {
388
+ botCode: r.getBotCode()
389
389
  })).sessionId;
390
390
  }
391
- function x(s) {
391
+ function v(r) {
392
392
  return {
393
393
  // 后端返回 thread_id,toCamelCase 转为 threadId,映射为 sessionId
394
- sessionId: s.threadId ?? s.sessionId,
394
+ sessionId: r.threadId ?? r.sessionId,
395
395
  // bot 外部标识统一 botCode(createSuperAgent 入参,全链消费);
396
396
  // 后端回传的内部 bot 标识字段 SDK 不消费、不暴露
397
- title: s.title,
398
- createTime: s.createTime,
399
- updateTime: s.updateTime
397
+ title: r.title,
398
+ createTime: r.createTime,
399
+ updateTime: r.updateTime
400
400
  };
401
401
  }
402
- async function E(s, e) {
403
- const i = await s.get("/chat/conversations", {
404
- botCode: s.getBotCode(),
402
+ async function x(r, e) {
403
+ const o = await r.get("/chat/conversations", {
404
+ botCode: r.getBotCode(),
405
405
  page: (e == null ? void 0 : e.page) ?? 1,
406
406
  size: (e == null ? void 0 : e.size) ?? 20
407
407
  });
408
408
  return {
409
- ...i,
410
- items: i.items.map(x)
409
+ ...o,
410
+ items: o.items.map(v)
411
411
  };
412
412
  }
413
- async function v(s, e, i) {
414
- await s.patch(`/chat/conversations/${e}`, { title: i });
413
+ async function E(r, e, o) {
414
+ await r.patch(`/chat/conversations/${e}`, { title: o });
415
415
  }
416
- async function O(s, e) {
417
- await s.del(`/chat/conversations/${e}`);
416
+ async function O(r, e) {
417
+ await r.del(`/chat/conversations/${e}`);
418
418
  }
419
- async function U(s, e) {
420
- const i = await s.get(`/chat/conversations/${e}/messages`);
421
- return N(i);
419
+ async function U(r, e) {
420
+ const o = await r.get(
421
+ `/chat/conversations/${e}/messages`
422
+ );
423
+ return M(o);
422
424
  }
423
425
  function k() {
424
426
  return `r_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
425
427
  }
426
- function N(s) {
428
+ function M(r) {
427
429
  var d;
428
- const e = s.length > 0 && Array.isArray((d = s[0]) == null ? void 0 : d.data), i = [];
430
+ const e = r.length > 0 && Array.isArray((d = r[0]) == null ? void 0 : d.data), o = [];
429
431
  if (e)
430
- for (const t of s)
431
- i.push({
432
+ for (const t of r)
433
+ o.push({
432
434
  roundId: t.roundId ?? k(),
433
435
  artifacts: Array.isArray(t.artifacts) && t.artifacts.length ? t.artifacts : void 0,
434
436
  items: Array.isArray(t.data) ? t.data : []
435
437
  });
436
438
  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;
439
+ o.push({ roundId: k(), items: r });
440
+ const s = [];
441
+ for (const t of o) {
442
+ const n = N(t.items);
443
+ for (const a of n) a.roundId = t.roundId;
442
444
  if (t.artifacts) {
443
- const o = r.findLast((a) => a.role === "assistant");
444
- o && (o.artifacts = t.artifacts);
445
+ const a = n.findLast((i) => i.role === "assistant");
446
+ a && (a.artifacts = t.artifacts);
445
447
  }
446
- n.push(...r);
448
+ s.push(...n);
447
449
  }
448
- return n;
450
+ return s;
449
451
  }
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 });
452
+ function N(r) {
453
+ var n;
454
+ const e = [...r].sort((a, i) => {
455
+ const c = new Date(a.createTime ?? 0).getTime() || 0, l = new Date(i.createTime ?? 0).getTime() || 0;
456
+ return c !== l ? c - l : (a.seq ?? 0) - (i.seq ?? 0);
457
+ }), o = /* @__PURE__ */ new Map();
458
+ for (const a of e)
459
+ a.role === "tool" && a.toolCallId && o.set(a.toolCallId, a.content || "");
460
+ const s = /* @__PURE__ */ new Set(), d = [], t = (a, i, c, l) => {
461
+ const u = d[d.length - 1];
462
+ (u == null ? void 0 : u.role) === "assistant" ? (u.parts.push(...a), u.id = i, l && (u.messageId = l)) : d.push({
463
+ id: i,
464
+ role: "assistant",
465
+ parts: [...a],
466
+ timestamp: c,
467
+ messageId: l
468
+ });
460
469
  };
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
- }));
470
+ for (const a of e) {
471
+ if (a.role === "system") continue;
472
+ const i = String(a.id ?? a.messageId ?? `msg_${Date.now()}`), c = new Date(a.createTime ?? "").getTime() || Date.now();
473
+ if (a.role === "human") {
474
+ const l = (a.attachments ?? []).map(
475
+ (f) => ({
476
+ type: "attachment",
477
+ attachment: {
478
+ nosKey: f.nosKey ?? f.attachmentId ?? "",
479
+ // 历史回显后端不带 nosKey,占位空串
480
+ filename: f.filename ?? "file",
481
+ size: f.size ?? void 0,
482
+ mime: f.mime ?? void 0,
483
+ url: f.url ?? void 0
484
+ // 公开读桶下载链接(有则 chip 可点击下载)
485
+ }
486
+ })
487
+ ), u = (n = a.metadata) == null ? void 0 : n.raw_message;
477
488
  d.push({
478
- id: o,
489
+ id: i,
479
490
  role: "user",
480
- parts: [...c, { type: "text", content: u(r.content) }],
481
- timestamp: a
491
+ parts: [...l, { type: "text", content: h(a.content) }],
492
+ timestamp: c,
493
+ messageId: a.messageId,
494
+ rawContent: typeof u == "string" && u ? u : void 0
482
495
  });
483
496
  continue;
484
497
  }
485
- if (r.role === "ai") {
486
- const c = M(r, i, n);
487
- c.length > 0 && t(c, o, a);
498
+ if (a.role === "ai") {
499
+ const l = D(a, o, s);
500
+ l.length > 0 && t(l, i, c, a.messageId);
488
501
  continue;
489
502
  }
490
- if (r.role === "tool") {
491
- if (r.toolCallId && n.has(r.toolCallId)) continue;
492
- t([L(r)], o, a);
503
+ if (a.role === "tool") {
504
+ if (a.toolCallId && s.has(a.toolCallId)) continue;
505
+ t([L(a)], i, c);
493
506
  }
494
507
  }
495
508
  return d;
496
509
  }
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;
510
+ function D(r, e, o) {
511
+ const s = [];
512
+ r.reasoning && s.push({ type: "thinking", content: r.reasoning }), r.content && s.push({ type: "text", content: h(r.content) });
513
+ for (const d of r.toolCalls ?? [])
514
+ d.is_interrupt ?? d.isInterrupt ? (o.add(d.id), s.push(q(d, e))) : s.push(J(d));
515
+ return s;
503
516
  }
504
- function q(s, e) {
505
- const i = e.has(s.id), n = typeof s.args == "object" ? s.args : {};
517
+ function q(r, e) {
518
+ const o = e.has(r.id), s = typeof r.args == "object" ? r.args : {};
506
519
  return {
507
520
  type: "interrupt",
508
521
  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
522
+ interruptId: r.id ?? "",
523
+ interruptType: s.interruptType ?? "input",
524
+ content: s.content ?? "",
525
+ options: s.options,
526
+ placeholder: s.placeholder,
527
+ inputType: s.inputType,
528
+ maxLength: s.maxLength,
529
+ required: s.required,
530
+ fields: s.fields,
531
+ metadata: s.metadata
519
532
  },
520
- resolved: i,
521
- response: i ? { action: "submit", value: j(e.get(s.id)) } : void 0
533
+ resolved: o,
534
+ response: o ? { action: "submit", value: j(e.get(r.id)) } : void 0
522
535
  };
523
536
  }
524
- function J(s) {
537
+ function J(r) {
525
538
  let e;
526
539
  try {
527
- e = typeof s.args == "string" ? s.args : JSON.stringify(s.args ?? {});
540
+ e = typeof r.args == "string" ? r.args : JSON.stringify(r.args ?? {});
528
541
  } catch {
529
- e = String(s.args ?? "{}");
542
+ e = String(r.args ?? "{}");
530
543
  }
531
- return { type: "tool_call", toolName: s.name ?? "", toolCallId: s.id ?? "", args: e };
544
+ return {
545
+ type: "tool_call",
546
+ toolName: r.name ?? "",
547
+ toolCallId: r.id ?? "",
548
+ args: e
549
+ };
532
550
  }
533
- function L(s) {
534
- var i, n, d, t;
535
- const e = Array.isArray(s.artifacts) ? s.artifacts : void 0;
551
+ function L(r) {
552
+ var o, s, d, t;
553
+ const e = Array.isArray(r.artifacts) ? r.artifacts : void 0;
536
554
  return {
537
555
  type: "tool_result",
538
- toolName: s.toolName ?? "",
539
- toolCallId: s.toolCallId ?? "",
556
+ toolName: r.toolName ?? "",
557
+ toolCallId: r.toolCallId ?? "",
540
558
  // tool 行后端 content 即展示摘要(_build_tool_fields 与 SSE 口径一致),直接读 content
541
- content: u(s.content),
559
+ content: h(r.content),
542
560
  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)
561
+ renderMode: r.renderMode ?? ((o = r.metadata) == null ? void 0 : o.renderMode) ?? ((s = r.metadata) == null ? void 0 : s.render_mode),
562
+ html: r.html ?? ((d = r.metadata) == null ? void 0 : d.html),
563
+ markdown: r.markdown ?? ((t = r.metadata) == null ? void 0 : t.markdown)
546
564
  };
547
565
  }
548
- function j(s) {
549
- if (s != null)
566
+ function j(r) {
567
+ if (r != null)
550
568
  try {
551
- return JSON.parse(s);
569
+ return JSON.parse(r);
552
570
  } catch {
553
- return s;
571
+ return r;
554
572
  }
555
573
  }
556
- async function G(s, e, i, n, d, t, r) {
557
- const o = await s.streamPost(
574
+ async function G(r, e, o, s, d, t, n) {
575
+ const a = await r.streamPost(
558
576
  `/chat/interrupt/${e}/respond`,
559
577
  {
560
- sessionId: i,
561
- action: n.action,
562
- value: n.value
578
+ sessionId: o,
579
+ action: s.action,
580
+ value: s.value
563
581
  },
564
- r
582
+ n
565
583
  );
566
- if (!o.ok) {
567
- const a = new Error(`HTTP ${o.status}`);
568
- throw t == null || t(a), a;
584
+ if (!a.ok) {
585
+ const i = new Error(`HTTP ${a.status}`);
586
+ throw t == null || t(i), i;
569
587
  }
570
588
  d && await T(
571
- o,
572
- (a) => {
573
- a.type === "error" ? t == null || t(new Error(a.content)) : d(a);
589
+ a,
590
+ (i) => {
591
+ i.type === "error" ? t == null || t(new Error(i.content)) : d(i);
574
592
  },
575
- (a) => t == null ? void 0 : t(a)
593
+ (i) => t == null ? void 0 : t(i)
576
594
  );
577
595
  }
578
- async function R(s) {
579
- return s.get(`/chat/bots/${s.getBotCode()}/skills`);
596
+ async function R(r) {
597
+ return r.get(`/chat/bots/${r.getBotCode()}/skills`);
580
598
  }
581
- async function B(s, e) {
582
- await s.post("/chat/stop", { sessionId: e });
599
+ async function B(r, e) {
600
+ await r.post("/chat/stop", { sessionId: e });
583
601
  }
584
- function z(s) {
585
- const e = new A(s);
602
+ function z(r) {
603
+ const e = new A(r);
586
604
  return {
587
605
  botCode: e.getBotCode(),
588
606
  getToken: () => e.fetchToken(),
589
607
  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);
608
+ chat: (s) => _(e, s),
609
+ listConversations: (s) => x(e, s),
610
+ renameConversation: (s, d) => E(e, s, d),
611
+ deleteConversation: (s) => O(e, s),
612
+ getMessages: (s) => U(e, s),
613
+ respondInterrupt: (s, d, t, n, a, i) => G(e, s, d, t, n, a, i),
614
+ setToken: (s) => e.setToken(s),
615
+ feedback: async (s, d, t) => {
616
+ const n = { type: d };
617
+ 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
618
  },
601
- cancelFeedback: async (n) => {
602
- await e.del(`/chat/messages/${n}/feedback`);
619
+ cancelFeedback: async (s) => {
620
+ await e.del(`/chat/messages/${s}/feedback`);
603
621
  },
604
622
  listSkills: () => R(e),
605
- stopGeneration: (n) => B(e, n)
623
+ stopGeneration: (s) => B(e, s)
606
624
  };
607
625
  }
608
626
  export {