@automattic/agenttic-client 0.1.56 → 0.1.57

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.js CHANGED
@@ -1,16 +1,16 @@
1
- var Ue = Object.defineProperty;
2
- var qe = (e, s, t) => s in e ? Ue(e, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[s] = t;
3
- var se = (e, s, t) => qe(e, typeof s != "symbol" ? s + "" : s, t);
4
- import He, { useMemo as Ae, useCallback as F, useState as ve, useRef as Fe, useEffect as ae } from "react";
5
- import { l as A, f as Me, a as J } from "./logger-aKHbTlwk.js";
6
- import he from "@wordpress/api-fetch";
1
+ var He = Object.defineProperty;
2
+ var Fe = (e, s, t) => s in e ? He(e, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[s] = t;
3
+ var se = (e, s, t) => Fe(e, typeof s != "symbol" ? s + "" : s, t);
4
+ import je, { useMemo as Me, useCallback as j, useState as ve, useRef as he, useEffect as ae } from "react";
5
+ import { l as M, f as xe, a as B } from "./logger-aKHbTlwk.js";
6
+ import ye from "@wordpress/api-fetch";
7
7
  function Ft(e) {
8
- return Ae(() => e ? {
8
+ return Me(() => e ? {
9
9
  getClientContext: () => {
10
10
  try {
11
11
  return e() || {};
12
12
  } catch (a) {
13
- return A("Error getting client context: %O", a), {};
13
+ return M("Error getting client context: %O", a), {};
14
14
  }
15
15
  }
16
16
  } : void 0, [e]);
@@ -20,33 +20,33 @@ function ue({
20
20
  executeTool: s,
21
21
  getAbilities: t
22
22
  }) {
23
- const a = F(async () => {
24
- const o = [];
23
+ const a = j(async () => {
24
+ const r = [];
25
25
  if (e)
26
26
  try {
27
- const c = await e();
28
- o.push(...c);
29
- } catch (c) {
30
- A("Error getting available tools: %O", c);
27
+ const l = await e();
28
+ r.push(...l);
29
+ } catch (l) {
30
+ M("Error getting available tools: %O", l);
31
31
  }
32
- return o;
33
- }, [e]), n = F(
34
- async (o, c) => {
32
+ return r;
33
+ }, [e]), n = j(
34
+ async (r, l) => {
35
35
  if (!s)
36
36
  throw new Error("No executeTool callback provided");
37
37
  try {
38
- return await s(o, c);
39
- } catch (l) {
40
- throw A("Error executing tool %s: %O", o, l), l;
38
+ return await s(r, l);
39
+ } catch (i) {
40
+ throw M("Error executing tool %s: %O", r, i), i;
41
41
  }
42
42
  },
43
43
  [s]
44
44
  );
45
- return Ae(() => {
45
+ return Me(() => {
46
46
  if (!e && !t)
47
47
  return;
48
- const o = {};
49
- return e && (o.getAvailableTools = a), s && (o.executeTool = n), t && (o.getAbilities = t), o;
48
+ const r = {};
49
+ return e && (r.getAvailableTools = a), s && (r.executeTool = n), t && (r.getAbilities = t), r;
50
50
  }, [
51
51
  a,
52
52
  n,
@@ -92,29 +92,29 @@ function de() {
92
92
  s += e.charAt(Math.floor(Math.random() * e.length));
93
93
  return s;
94
94
  }
95
- function $() {
95
+ function N() {
96
96
  return de();
97
97
  }
98
- function je() {
98
+ function De() {
99
99
  return `req-${de()}`;
100
100
  }
101
- function De() {
101
+ function Je() {
102
102
  return `task-${de()}`;
103
103
  }
104
- function xe(e, s) {
104
+ function Ce(e, s) {
105
105
  return {
106
106
  type: "text",
107
107
  text: e,
108
108
  ...s && { metadata: s }
109
109
  };
110
110
  }
111
- function Je(e, s = "message/send", t = !1) {
111
+ function Ke(e, s = "message/send", t = !1) {
112
112
  const a = {
113
113
  jsonrpc: "2.0",
114
- id: je(),
114
+ id: De(),
115
115
  method: s,
116
116
  params: {
117
- id: e.id || De(),
117
+ id: e.id || Je(),
118
118
  ...e
119
119
  }
120
120
  };
@@ -123,7 +123,7 @@ function Je(e, s = "message/send", t = !1) {
123
123
  function D(e) {
124
124
  return !e || !e.parts || !Array.isArray(e.parts) ? "" : e.parts.filter((s) => s.type === "text").map((s) => s.text).join(" ");
125
125
  }
126
- function ye(e) {
126
+ function Ie(e) {
127
127
  if (!e || !e.parts || !Array.isArray(e.parts))
128
128
  return;
129
129
  const s = e.parts.find((t) => t.type === "progress" ? !0 : t.type === "data" ? "summary" in t.data && typeof t.data.summary == "string" : !1);
@@ -133,7 +133,7 @@ function ye(e) {
133
133
  phase: s.data.phase
134
134
  };
135
135
  }
136
- function Ke(e) {
136
+ function Le(e) {
137
137
  return {
138
138
  type: "data",
139
139
  data: {
@@ -145,7 +145,7 @@ function Ke(e) {
145
145
  metadata: {}
146
146
  };
147
147
  }
148
- function Le(e) {
148
+ function Be(e) {
149
149
  return {
150
150
  type: "data",
151
151
  data: {
@@ -160,12 +160,12 @@ function Le(e) {
160
160
  metadata: {}
161
161
  };
162
162
  }
163
- function K(e) {
163
+ function W(e) {
164
164
  return !e || !e.parts || !Array.isArray(e.parts) ? [] : e.parts.filter(
165
165
  (s) => s.type === "data" && "toolCallId" in s.data && "toolId" in s.data && "arguments" in s.data
166
166
  );
167
167
  }
168
- function B(e, s, t, a) {
168
+ function J(e, s, t, a) {
169
169
  return {
170
170
  type: "data",
171
171
  data: {
@@ -176,7 +176,7 @@ function B(e, s, t, a) {
176
176
  metadata: a ? { error: a } : void 0
177
177
  };
178
178
  }
179
- function Be(e) {
179
+ function We(e) {
180
180
  return {
181
181
  type: "data",
182
182
  data: {
@@ -189,21 +189,21 @@ function le(e, s) {
189
189
  const { contentType: t, ...a } = s || {};
190
190
  return {
191
191
  role: "user",
192
- parts: [xe(e, t ? { contentType: t } : void 0)],
192
+ parts: [Ce(e, t ? { contentType: t } : void 0)],
193
193
  kind: "message",
194
- messageId: $(),
194
+ messageId: N(),
195
195
  metadata: {
196
196
  timestamp: Date.now(),
197
197
  ...a
198
198
  }
199
199
  };
200
200
  }
201
- function Q(e) {
201
+ function X(e) {
202
202
  return {
203
203
  role: "agent",
204
- parts: [xe(e)],
204
+ parts: [Ce(e)],
205
205
  kind: "message",
206
- messageId: $(),
206
+ messageId: N(),
207
207
  metadata: {
208
208
  timestamp: Date.now()
209
209
  }
@@ -221,12 +221,12 @@ function ge(e) {
221
221
  returnToAgent: !0
222
222
  };
223
223
  }
224
- function Y(e, s = []) {
224
+ function Z(e, s = []) {
225
225
  return {
226
226
  role: "user",
227
227
  kind: "message",
228
228
  parts: [...s, ...e],
229
- messageId: $(),
229
+ messageId: N(),
230
230
  metadata: {
231
231
  timestamp: Date.now()
232
232
  }
@@ -235,7 +235,7 @@ function Y(e, s = []) {
235
235
  function Kt() {
236
236
  return new AbortController();
237
237
  }
238
- async function We(e, s) {
238
+ async function Ve(e, s) {
239
239
  if (!s)
240
240
  return e;
241
241
  try {
@@ -243,14 +243,14 @@ async function We(e, s) {
243
243
  if (s.getAvailableTools) {
244
244
  const a = await s.getAvailableTools();
245
245
  if (a.length > 0) {
246
- const n = a.map(Ke);
246
+ const n = a.map(Le);
247
247
  t.push(...n);
248
248
  }
249
249
  }
250
250
  if (s.getAbilities) {
251
251
  const a = await s.getAbilities();
252
252
  if (a.length > 0) {
253
- const n = a.map(Le);
253
+ const n = a.map(Be);
254
254
  t.push(...n);
255
255
  }
256
256
  }
@@ -259,46 +259,46 @@ async function We(e, s) {
259
259
  parts: [...e.parts, ...t]
260
260
  };
261
261
  } catch (t) {
262
- return A("Warning: Failed to get tools: %s", t), e;
262
+ return M("Warning: Failed to get tools: %s", t), e;
263
263
  }
264
264
  }
265
- function Ve(e, s) {
265
+ function ze(e, s) {
266
266
  if (!s)
267
267
  return e;
268
268
  try {
269
269
  const t = s.getClientContext();
270
270
  if (!t || Object.keys(t).length === 0)
271
271
  return e;
272
- const a = Be(t);
272
+ const a = We(t);
273
273
  return {
274
274
  ...e,
275
275
  parts: [...e.parts, a]
276
276
  };
277
277
  } catch (t) {
278
- return A("Warning: Failed to get context: %s", t), e;
278
+ return M("Warning: Failed to get context: %s", t), e;
279
279
  }
280
280
  }
281
- async function ze(e, s, t) {
282
- let a = await We(
281
+ async function Ge(e, s, t) {
282
+ let a = await Ve(
283
283
  e,
284
284
  s
285
285
  );
286
- a = Ve(
286
+ a = ze(
287
287
  a,
288
288
  t
289
289
  );
290
- const { metadata: n, ...r } = a;
291
- return r;
290
+ const { metadata: n, ...o } = a;
291
+ return o;
292
292
  }
293
- function Ge(e, s = "") {
293
+ function Qe(e, s = "") {
294
294
  const t = [], a = s + e;
295
- let n = "", r = 0, o = 0;
296
- for (; o < a.length; ) {
297
- const l = a.indexOf(
295
+ let n = "", o = 0, r = 0;
296
+ for (; r < a.length; ) {
297
+ const i = a.indexOf(
298
298
  `
299
299
  `,
300
- o
301
- ), d = l === -1 ? a.substring(o) : a.substring(o, l);
300
+ r
301
+ ), d = i === -1 ? a.substring(r) : a.substring(r, i);
302
302
  if (d.startsWith("data:"))
303
303
  n !== "" && (n += `
304
304
  `), n += d.substring(
@@ -306,96 +306,96 @@ function Ge(e, s = "") {
306
306
  );
307
307
  else if (d.trim() === "" && n) {
308
308
  try {
309
- t.push(JSON.parse(n)), r = l === -1 ? a.length : l + 1;
309
+ t.push(JSON.parse(n)), o = i === -1 ? a.length : i + 1;
310
310
  } catch (u) {
311
- A("Failed to parse SSE event: %o", u), A("Problematic payload: %s", n);
311
+ M("Failed to parse SSE event: %o", u), M("Problematic payload: %s", n);
312
312
  }
313
313
  n = "";
314
314
  }
315
- l === -1 ? o = a.length : o = l + 1;
315
+ i === -1 ? r = a.length : r = i + 1;
316
316
  }
317
- const c = a.substring(r);
318
- return { events: t, nextBuffer: c };
317
+ const l = a.substring(o);
318
+ return { events: t, nextBuffer: l };
319
319
  }
320
- async function* Qe(e, s = {}) {
320
+ async function* Xe(e, s = {}) {
321
321
  var d, u, m;
322
322
  const { supportDeltas: t = !1 } = s, a = e.getReader(), n = new TextDecoder();
323
- let r = "";
324
- const o = new Xe();
325
- let c = null, l = null;
323
+ let o = "";
324
+ const r = new Ye();
325
+ let l = null, i = null;
326
326
  try {
327
327
  for (; ; ) {
328
- const { done: p, value: g } = await a.read();
329
- if (p)
328
+ const { done: h, value: f } = await a.read();
329
+ if (h)
330
330
  break;
331
- const y = n.decode(g, { stream: !0 }), { events: x, nextBuffer: I } = Ge(y, r);
332
- if (x && Array.isArray(x))
333
- for (let f = 0; f < x.length; f++) {
334
- const i = x[f];
335
- if (f > 0 && i.method === "message/delta" && typeof requestAnimationFrame < "u" && await new Promise((w) => {
336
- requestAnimationFrame(() => w(void 0));
337
- }), i.error)
331
+ const v = n.decode(f, { stream: !0 }), { events: y, nextBuffer: I } = Qe(v, o);
332
+ if (y && Array.isArray(y))
333
+ for (let A = 0; A < y.length; A++) {
334
+ const g = y[A];
335
+ if (A > 0 && g.method === "message/delta" && typeof requestAnimationFrame < "u" && await new Promise((c) => {
336
+ requestAnimationFrame(() => c(void 0));
337
+ }), g.error)
338
338
  throw new Error(
339
- `Streaming error: ${i.error.message}`
339
+ `Streaming error: ${g.error.message}`
340
340
  );
341
- if (t && i.method === "message/delta" && ((d = i.params) != null && d.delta)) {
342
- const w = i.params.delta;
341
+ if (t && g.method === "message/delta" && ((d = g.params) != null && d.delta)) {
342
+ const c = g.params.delta;
343
343
  try {
344
- if (w.deltaType === "content" && (o.processContentDelta(
345
- w.content
346
- ), !c && i.params.id && (c = i.params.id), c)) {
347
- const T = o.getCurrentMessage();
344
+ if (c.deltaType === "content" && (r.processContentDelta(
345
+ c.content
346
+ ), !l && g.params.id && (l = g.params.id), l)) {
347
+ const w = r.getCurrentMessage();
348
348
  yield {
349
- id: c,
349
+ id: l,
350
350
  status: {
351
351
  state: "working",
352
- message: T
352
+ message: w
353
353
  },
354
354
  final: !1,
355
- text: o.getTextContent()
355
+ text: r.getTextContent()
356
356
  };
357
357
  }
358
- } catch (T) {
359
- A("Failed to process delta: %o", T);
358
+ } catch (w) {
359
+ M("Failed to process delta: %o", w);
360
360
  }
361
- } else if (i.result && i.result.status) {
362
- c = i.result.id, l = i.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset();
363
- const w = ((u = i.result.status) == null ? void 0 : u.message) || {
361
+ } else if (g.result && g.result.status) {
362
+ l = g.result.id, i = g.result.status, (r.getTextContent() || r.getCurrentMessage().parts.length > 0) && r.reset();
363
+ const c = ((u = g.result.status) == null ? void 0 : u.message) || {
364
364
  role: "agent",
365
365
  parts: []
366
- }, T = ye(w);
366
+ }, w = Ie(c);
367
367
  yield {
368
- id: i.result.id,
369
- sessionId: i.result.sessionId,
370
- status: i.result.status,
371
- final: i.result.status.state === "completed" || i.result.status.state === "failed" || i.result.status.state === "canceled",
372
- text: D(w),
373
- progressMessage: T == null ? void 0 : T.summary,
374
- progressPhase: T == null ? void 0 : T.phase
368
+ id: g.result.id,
369
+ sessionId: g.result.sessionId,
370
+ status: g.result.status,
371
+ final: g.result.status.state === "completed" || g.result.status.state === "failed" || g.result.status.state === "canceled",
372
+ text: D(c),
373
+ progressMessage: w == null ? void 0 : w.summary,
374
+ progressPhase: w == null ? void 0 : w.phase
375
375
  };
376
- } else if (i.id && i.result && (c = i.result.id, i.result.status)) {
377
- const w = ((m = i.result.status) == null ? void 0 : m.message) || {
376
+ } else if (g.id && g.result && (l = g.result.id, g.result.status)) {
377
+ const c = ((m = g.result.status) == null ? void 0 : m.message) || {
378
378
  role: "agent",
379
379
  parts: []
380
- }, T = ye(w);
380
+ }, w = Ie(c);
381
381
  yield {
382
- id: i.result.id,
383
- sessionId: i.result.sessionId,
384
- status: i.result.status,
385
- final: i.result.status.state === "completed" || i.result.status.state === "failed" || i.result.status.state === "canceled",
386
- text: D(w),
387
- progressMessage: T == null ? void 0 : T.summary,
388
- progressPhase: T == null ? void 0 : T.phase
382
+ id: g.result.id,
383
+ sessionId: g.result.sessionId,
384
+ status: g.result.status,
385
+ final: g.result.status.state === "completed" || g.result.status.state === "failed" || g.result.status.state === "canceled",
386
+ text: D(c),
387
+ progressMessage: w == null ? void 0 : w.summary,
388
+ progressPhase: w == null ? void 0 : w.phase
389
389
  };
390
390
  }
391
391
  }
392
- r = I;
392
+ o = I;
393
393
  }
394
394
  } finally {
395
395
  a.releaseLock();
396
396
  }
397
397
  }
398
- class Xe {
398
+ class Ye {
399
399
  constructor() {
400
400
  se(this, "textContent", "");
401
401
  se(this, "toolCalls", /* @__PURE__ */ new Map());
@@ -452,20 +452,20 @@ class Xe {
452
452
  });
453
453
  for (const [a, n] of this.toolCalls)
454
454
  if (n.toolName) {
455
- const r = n.argumentFragments.join("");
456
- let o = {};
457
- if (r)
455
+ const o = n.argumentFragments.join("");
456
+ let r = {};
457
+ if (o)
458
458
  try {
459
- o = JSON.parse(r);
459
+ r = JSON.parse(o);
460
460
  } catch {
461
- o = { _raw: r };
461
+ r = { _raw: o };
462
462
  }
463
463
  t.push({
464
464
  type: "data",
465
465
  data: {
466
466
  toolCallId: n.toolCallId,
467
467
  toolId: n.toolName,
468
- arguments: o
468
+ arguments: r
469
469
  }
470
470
  });
471
471
  }
@@ -473,7 +473,7 @@ class Xe {
473
473
  role: s,
474
474
  parts: t,
475
475
  kind: "message",
476
- messageId: $()
476
+ messageId: N()
477
477
  };
478
478
  }
479
479
  /**
@@ -483,14 +483,14 @@ class Xe {
483
483
  this.textContent = "", this.toolCalls.clear();
484
484
  }
485
485
  }
486
- function Ce(e, s, t = "request") {
487
- throw clearTimeout(s), A("%s failed with error: %O", t, e), e instanceof Error && (A("Error message: %s", e.message), A("Error stack: %s", e.stack)), e;
486
+ function be(e, s, t = "request") {
487
+ throw clearTimeout(s), M("%s failed with error: %O", t, e), e instanceof Error && (M("Error message: %s", e.message), M("Error stack: %s", e.stack)), e;
488
488
  }
489
- function be(e, s = "request") {
489
+ function ke(e, s = "request") {
490
490
  if (!e.ok)
491
491
  throw new Error(`HTTP error! status: ${e.status}`);
492
492
  }
493
- function Ye(e, s = "request") {
493
+ function Ze(e, s = "request") {
494
494
  if (e.error)
495
495
  throw new Error(
496
496
  `Protocol ${s} error: ${e.error.message}`
@@ -499,24 +499,24 @@ function Ye(e, s = "request") {
499
499
  throw new Error(`No result in ${s} response`);
500
500
  return e.result;
501
501
  }
502
- function Ze(e, s = "streaming request") {
503
- if (be(e, s), !e.body)
502
+ function et(e, s = "streaming request") {
503
+ if (ke(e, s), !e.body)
504
504
  throw new Error(`No response body for ${s}`);
505
505
  }
506
- function ke(e, s = "request") {
506
+ function Re(e, s = "request") {
507
507
  const t = new AbortController();
508
508
  return { timeoutId: setTimeout(
509
509
  () => t.abort(),
510
510
  e
511
511
  ), controller: t };
512
512
  }
513
- function et(e, s) {
513
+ function tt(e, s) {
514
514
  return `${e}/${s}`;
515
515
  }
516
- function tt(e, s, t, a) {
517
- A("Request: %s %s", e, s), A("Headers: %o", t), a && A("Body: %s", Me(a));
516
+ function st(e, s, t, a) {
517
+ M("Request: %s %s", e, s), M("Headers: %o", t), a && M("Body: %s", xe(a));
518
518
  }
519
- async function st(e, s = !1) {
519
+ async function at(e, s = !1) {
520
520
  const t = {
521
521
  "Content-Type": "application/json"
522
522
  };
@@ -538,7 +538,7 @@ function Pe(e, s) {
538
538
  once: !0
539
539
  }), t.signal;
540
540
  }
541
- function Re(e, s, t) {
541
+ function _e(e, s, t) {
542
542
  return {
543
543
  method: "POST",
544
544
  headers: e,
@@ -546,96 +546,98 @@ function Re(e, s, t) {
546
546
  signal: t
547
547
  };
548
548
  }
549
- async function Z(e, s, t, a, n, r) {
550
- const { message: o, sessionId: c, taskId: l, metadata: d } = e, { agentId: u, agentUrl: m, authProvider: p, proxy: g } = s, { isStreaming: y = !1, enableTokenStreaming: x = !1 } = t, I = c || r, f = et(m, u), i = await ze(
551
- o,
549
+ async function ee(e, s, t, a, n, o) {
550
+ const { message: r, sessionId: l, taskId: i, metadata: d } = e, { agentId: u, agentUrl: m, authProvider: h, proxy: f } = s, { isStreaming: v = !1, enableTokenStreaming: y = !1 } = t, I = l || o, A = tt(m, u), g = await Ge(
551
+ r,
552
552
  a,
553
553
  n
554
- ), w = {
555
- id: l,
556
- message: i,
554
+ ), c = {
555
+ id: i,
556
+ message: g,
557
557
  metadata: d
558
558
  };
559
- I && (w.sessionId = I);
560
- const T = Je(
561
- w,
562
- y ? "message/stream" : "message/send",
563
- x && y
559
+ I && (c.sessionId = I);
560
+ const w = Ke(
561
+ c,
562
+ v ? "message/stream" : "message/send",
563
+ y && v
564
564
  // Only enable token streaming if using SSE
565
- ), S = await st(p, y);
566
- return tt("POST", f, S, T), {
567
- request: T,
568
- headers: S,
569
- enhancedMessage: i,
565
+ ), k = await at(h, v);
566
+ return st("POST", A, k, w), {
567
+ request: w,
568
+ headers: k,
569
+ enhancedMessage: g,
570
570
  effectiveSessionId: I,
571
- fullAgentUrl: f
571
+ fullAgentUrl: A
572
572
  };
573
573
  }
574
- async function _e(e, s, t = {}, a) {
575
- const { request: n, headers: r, fullAgentUrl: o } = e, { timeout: c } = s, { abortSignal: l } = t, { timeoutId: d, controller: u } = ke(
576
- c,
574
+ async function Ee(e, s, t = {}, a) {
575
+ const { request: n, headers: o, fullAgentUrl: r } = e, { timeout: l } = s, { abortSignal: i } = t, { timeoutId: d, controller: u } = Re(
576
+ l,
577
577
  "request"
578
- ), m = l ? Pe(u.signal, l) : u.signal;
578
+ ), m = i ? Pe(u.signal, i) : u.signal;
579
579
  try {
580
- const p = Re(
581
- r,
580
+ const h = _e(
581
+ o,
582
582
  JSON.stringify(n),
583
583
  m
584
584
  );
585
- A("Making request to %s with options: %O", o, {
586
- method: p.method,
587
- headers: p.headers
585
+ M("Making request to %s with options: %O", r, {
586
+ method: h.method,
587
+ headers: h.headers
588
588
  });
589
- const g = await a(o, p);
590
- clearTimeout(d), be(g, "request");
591
- const y = await g.json();
592
- return A(
589
+ const f = await a(r, h);
590
+ clearTimeout(d), ke(f, "request");
591
+ const v = await f.json();
592
+ return M(
593
593
  "Response from %s: %d %O",
594
- o,
595
- g.status,
596
- Me(y)
597
- ), Ye(y, "request");
598
- } catch (p) {
599
- Ce(p, d, "request");
594
+ r,
595
+ f.status,
596
+ xe(v)
597
+ ), Ze(v, "request");
598
+ } catch (h) {
599
+ be(h, d, "request");
600
600
  }
601
601
  }
602
- async function* Ee(e, s, t, a) {
603
- const { request: n, headers: r, fullAgentUrl: o } = e, {
604
- streamingTimeout: c = 6e4,
605
- abortSignal: l,
602
+ async function* Oe(e, s, t, a) {
603
+ const { request: n, headers: o, fullAgentUrl: r } = e, {
604
+ streamingTimeout: l = 6e4,
605
+ abortSignal: i,
606
606
  enableTokenStreaming: d = !1
607
- } = t, { timeoutId: u, controller: m } = ke(
608
- c,
607
+ } = t, { timeoutId: u, controller: m } = Re(
608
+ l,
609
609
  "streaming request"
610
- ), p = l ? Pe(m.signal, l) : m.signal;
610
+ ), h = i ? Pe(m.signal, i) : m.signal;
611
611
  try {
612
- const g = JSON.stringify(n), y = Re(r, g, p), x = await a(o, y);
613
- if (clearTimeout(u), Ze(x, "streaming request"), !x.body)
612
+ const f = JSON.stringify(n), v = _e(o, f, h), y = await a(r, v);
613
+ if (clearTimeout(u), et(y, "streaming request"), !y.body)
614
614
  throw new Error(
615
615
  "Response body is null - server may not support streaming"
616
616
  );
617
617
  const I = d && n.tokenStreaming === !0;
618
- yield* Qe(x.body, {
618
+ yield* Xe(y.body, {
619
619
  supportDeltas: I
620
620
  });
621
- } catch (g) {
622
- Ce(g, u, "streaming request");
621
+ } catch (f) {
622
+ be(f, u, "streaming request");
623
623
  }
624
624
  }
625
- const at = 12e4;
626
- async function ee(e, s, t, a, n) {
625
+ const nt = 12e4;
626
+ async function te(e, s, t, a, n) {
627
627
  if (e.getAbilities) {
628
- const r = await e.getAbilities();
629
- if (r.length > 0)
630
- for (const o of r) {
631
- const c = o.name.replace(/\//g, "__").replace(/-/g, "_");
632
- if (s === c || s === o.name) {
633
- if (o.callback)
628
+ const o = await e.getAbilities();
629
+ if (o.length > 0)
630
+ for (const r of o) {
631
+ const l = r.name.replace(/\//g, "__").replace(/-/g, "_");
632
+ if (s === l || s === r.name) {
633
+ if (r.callback)
634
634
  try {
635
- const l = {
635
+ const i = {
636
636
  ...t,
637
- ...a && { messageId: a }
638
- }, d = await o.callback(l);
637
+ ...a && { messageId: a },
638
+ ...n && { toolCallId: n },
639
+ ...s && { toolId: s }
640
+ }, d = await r.callback(i);
639
641
  return {
640
642
  result: d,
641
643
  returnToAgent: (d == null ? void 0 : d.returnToAgent) !== void 0 ? d.returnToAgent : !0,
@@ -643,40 +645,40 @@ async function ee(e, s, t, a, n) {
643
645
  agentMessage: d.agentMessage
644
646
  }
645
647
  };
646
- } catch (l) {
647
- return A(
648
+ } catch (i) {
649
+ return M(
648
650
  "Error executing ability %s: %O",
649
- o.name,
650
- l
651
+ r.name,
652
+ i
651
653
  ), {
652
654
  result: {
653
- error: l instanceof Error ? l.message : String(l),
655
+ error: i instanceof Error ? i.message : String(i),
654
656
  success: !1
655
657
  },
656
658
  returnToAgent: !0
657
659
  };
658
660
  }
659
- if (!o.callback && e.executeAbility)
661
+ if (!r.callback && e.executeAbility)
660
662
  try {
661
663
  return { result: await e.executeAbility(
662
- o.name,
664
+ r.name,
663
665
  t
664
666
  ), returnToAgent: !0 };
665
- } catch (l) {
666
- return A(
667
+ } catch (i) {
668
+ return M(
667
669
  "Error executing ability %s: %O",
668
- o.name,
669
- l
670
+ r.name,
671
+ i
670
672
  ), {
671
673
  result: {
672
- error: l instanceof Error ? l.message : String(l),
674
+ error: i instanceof Error ? i.message : String(i),
673
675
  success: !1
674
676
  },
675
677
  returnToAgent: !0
676
678
  };
677
679
  }
678
680
  throw new Error(
679
- `Ability ${o.name} has no callback and no handler`
681
+ `Ability ${r.name} has no callback and no handler`
680
682
  );
681
683
  }
682
684
  }
@@ -693,17 +695,17 @@ async function ee(e, s, t, a, n) {
693
695
  );
694
696
  }
695
697
  const fe = /* @__PURE__ */ new Map();
696
- async function nt(e, s) {
698
+ async function rt(e, s) {
697
699
  if (!e || !s || !e.getAvailableTools)
698
700
  return !1;
699
- const t = K(s);
701
+ const t = W(s);
700
702
  if (t.length === 0)
701
703
  return !1;
702
704
  try {
703
705
  const a = await e.getAvailableTools();
704
706
  for (const n of t)
705
707
  if (a.some(
706
- (o) => o.id === n.data.toolId
708
+ (r) => r.id === n.data.toolId
707
709
  ))
708
710
  return !0;
709
711
  } catch {
@@ -714,17 +716,17 @@ async function nt(e, s) {
714
716
  function ot() {
715
717
  fe.clear();
716
718
  }
717
- function rt(e) {
719
+ function it(e) {
718
720
  return e.map((s) => {
719
721
  const t = s.data.toolCallId, a = fe.get(t);
720
722
  if (a && a.resolvedValue !== null) {
721
723
  const n = a.resolvedValue;
722
- return n.error ? B(
724
+ return n.error ? J(
723
725
  t,
724
726
  s.data.toolId,
725
727
  void 0,
726
728
  n.error
727
- ) : B(
729
+ ) : J(
728
730
  t,
729
731
  s.data.toolId,
730
732
  n
@@ -733,40 +735,40 @@ function rt(e) {
733
735
  return s;
734
736
  });
735
737
  }
736
- async function Oe(e, s, t) {
738
+ async function Ne(e, s, t) {
737
739
  const a = [], n = [];
738
- let r = !1;
739
- for (const o of e) {
740
- const { toolCallId: c, toolId: l, arguments: d } = o.data;
740
+ let o = !1;
741
+ for (const r of e) {
742
+ const { toolCallId: l, toolId: i, arguments: d } = r.data;
741
743
  try {
742
- const u = await ee(
744
+ const u = await te(
743
745
  s,
744
- l,
746
+ i,
745
747
  d,
746
748
  t,
747
- c
748
- ), { result: m, returnToAgent: p, agentMessage: g } = ge(u);
749
- p && (r = !0), g && n.push(Q(g)), a.push(
750
- B(
751
- c,
749
+ l
750
+ ), { result: m, returnToAgent: h, agentMessage: f } = ge(u);
751
+ h && (o = !0), f && n.push(X(f)), a.push(
752
+ J(
752
753
  l,
754
+ i,
753
755
  m
754
756
  )
755
757
  );
756
758
  } catch (u) {
757
- r = !0, a.push(
758
- B(
759
- c,
759
+ o = !0, a.push(
760
+ J(
760
761
  l,
762
+ i,
761
763
  void 0,
762
764
  u instanceof Error ? u.message : String(u)
763
765
  )
764
766
  );
765
767
  }
766
768
  }
767
- return { results: a, shouldReturnToAgent: r, agentMessages: n };
769
+ return { results: a, shouldReturnToAgent: o, agentMessages: n };
768
770
  }
769
- function Ie(e) {
771
+ function we(e) {
770
772
  const s = [];
771
773
  for (const t of e)
772
774
  for (const a of t.parts)
@@ -779,8 +781,8 @@ function Ie(e) {
779
781
  }) : (a.type === "data" || a.type === "file") && s.push(a);
780
782
  return s;
781
783
  }
782
- async function ne(e, s, t, a, n, r, o, c) {
783
- const d = await Z(
784
+ async function ne(e, s, t, a, n, o, r, l) {
785
+ const d = await ee(
784
786
  {
785
787
  message: s,
786
788
  taskId: e,
@@ -790,168 +792,168 @@ async function ne(e, s, t, a, n, r, o, c) {
790
792
  t,
791
793
  { isStreaming: !1 },
792
794
  n,
793
- r,
794
- o
795
+ o,
796
+ r
795
797
  );
796
- return await _e(
798
+ return await Ee(
797
799
  d,
798
800
  t,
799
801
  {
800
- abortSignal: c
802
+ abortSignal: l
801
803
  },
802
804
  a
803
805
  );
804
806
  }
805
- async function we(e, s, t, a, n, r, o, c, l, d = []) {
807
+ async function Te(e, s, t, a, n, o, r, l, i, d = []) {
806
808
  const u = {
807
809
  message: s,
808
810
  taskId: e,
809
- sessionId: o
811
+ sessionId: r
810
812
  // Use the provided sessionId to maintain conversation continuity
811
- }, m = l || { isStreaming: !0 }, p = await Z(
813
+ }, m = i || { isStreaming: !0 }, h = await ee(
812
814
  u,
813
815
  t,
814
816
  {
815
817
  ...m
816
818
  },
817
819
  n,
818
- r,
819
- o
820
- ), g = Ee(
821
- p,
820
+ o,
821
+ r
822
+ ), f = Oe(
823
+ h,
822
824
  t,
823
825
  {
824
826
  ...m,
825
- abortSignal: c
827
+ abortSignal: l
826
828
  },
827
829
  a
828
830
  );
829
- return Ne(
830
- g,
831
+ return $e(
832
+ f,
831
833
  n,
832
- r,
834
+ o,
833
835
  t,
834
836
  a,
835
- o,
837
+ r,
836
838
  !0,
837
839
  // withHistory
838
840
  d,
839
841
  // preserve conversation parts across continuation
840
- c,
842
+ l,
841
843
  m
842
844
  // Pass through the same request options
843
845
  );
844
846
  }
845
- async function* Ne(e, s, t, a, n, r, o = !0, c = [], l, d) {
846
- var u, m, p, g, y, x, I, f, i, w, T, S;
847
- for await (const C of e) {
848
- if (C.sessionId && !r && (r = C.sessionId), yield C, C.status.state === "running" && C.status.message && s && await nt(
847
+ async function* $e(e, s, t, a, n, o, r = !0, l = [], i, d) {
848
+ var u, m, h, f, v, y, I, A, g, c, w, k;
849
+ for await (const T of e) {
850
+ if (T.sessionId && !o && (o = T.sessionId), yield T, T.status.state === "running" && T.status.message && s && await rt(
849
851
  s,
850
- C.status.message
852
+ T.status.message
851
853
  )) {
852
- const N = K(
853
- C.status.message
854
+ const _ = W(
855
+ T.status.message
854
856
  );
855
- for (const U of N) {
856
- const { toolCallId: v, toolId: h, arguments: P } = U.data;
857
- ee(
857
+ for (const O of _) {
858
+ const { toolCallId: q, toolId: p, arguments: C } = O.data;
859
+ te(
858
860
  s,
859
- h,
860
- P,
861
- (m = (u = C.status) == null ? void 0 : u.message) == null ? void 0 : m.messageId,
862
- v
863
- ).catch((M) => {
864
- A("Tool execution failed for %s: %O", h, M);
861
+ p,
862
+ C,
863
+ (m = (u = T.status) == null ? void 0 : u.message) == null ? void 0 : m.messageId,
864
+ q
865
+ ).catch((S) => {
866
+ M("Tool execution failed for %s: %O", p, S);
865
867
  });
866
868
  }
867
869
  yield {
868
- id: C.id,
870
+ id: T.id,
869
871
  status: {
870
872
  state: "running",
871
873
  message: {
872
874
  role: "agent",
873
875
  kind: "message",
874
- parts: N,
875
- messageId: $()
876
+ parts: _,
877
+ messageId: N()
876
878
  }
877
879
  },
878
880
  final: !1,
879
881
  text: ""
880
882
  };
881
883
  }
882
- if (C.status.state === "input-required" && C.status.message && s) {
883
- const N = K(
884
- C.status.message
884
+ if (T.status.state === "input-required" && T.status.message && s) {
885
+ const _ = W(
886
+ T.status.message
885
887
  );
886
- if (N.length > 0) {
887
- const U = [];
888
- let v = !1;
889
- const h = [], P = [];
890
- for (const M of N) {
888
+ if (_.length > 0) {
889
+ const O = [];
890
+ let q = !1;
891
+ const p = [], C = [];
892
+ for (const S of _) {
891
893
  const {
892
894
  toolCallId: b,
893
- toolId: R,
894
- arguments: k
895
- } = M.data;
895
+ toolId: $,
896
+ arguments: R
897
+ } = S.data;
896
898
  try {
897
- const E = await ee(
899
+ const P = await te(
898
900
  s,
901
+ $,
899
902
  R,
900
- k,
901
- (g = (p = C.status) == null ? void 0 : p.message) == null ? void 0 : g.messageId,
903
+ (f = (h = T.status) == null ? void 0 : h.message) == null ? void 0 : f.messageId,
902
904
  b
903
- ), { result: _, returnToAgent: q, agentMessage: H } = ge(E);
904
- if (q && (v = !0), H && P.push(
905
- Q(H)
906
- ), _.result instanceof Promise) {
907
- const W = _.result, V = {
908
- promise: W,
905
+ ), { result: x, returnToAgent: U, agentMessage: K } = ge(P);
906
+ if (U && (q = !0), K && C.push(
907
+ X(K)
908
+ ), x.result instanceof Promise) {
909
+ const H = x.result, E = {
910
+ promise: H,
909
911
  resolvedValue: null
910
912
  };
911
913
  fe.set(
912
914
  b,
913
- V
914
- ), W.then((j) => {
915
- V.resolvedValue = j;
916
- }).catch((j) => {
917
- A(
915
+ E
916
+ ), H.then((F) => {
917
+ E.resolvedValue = F;
918
+ }).catch((F) => {
919
+ M(
918
920
  "Promise rejected for tool call %s: %O",
919
921
  b,
920
- j
921
- ), V.resolvedValue = {
922
- error: j instanceof Error ? j.message : String(j)
922
+ F
923
+ ), E.resolvedValue = {
924
+ error: F instanceof Error ? F.message : String(F)
923
925
  };
924
926
  });
925
927
  }
926
- const O = B(
928
+ const L = J(
927
929
  b,
928
- R,
929
- _
930
+ $,
931
+ x
930
932
  );
931
- U.push(O), h.push(O);
932
- } catch (E) {
933
- const _ = B(
933
+ O.push(L), p.push(L);
934
+ } catch (P) {
935
+ const x = J(
934
936
  b,
935
- R,
937
+ $,
936
938
  void 0,
937
- E instanceof Error ? E.message : String(E)
939
+ P instanceof Error ? P.message : String(P)
938
940
  );
939
- U.push(_), h.push(_);
941
+ O.push(x), p.push(x);
940
942
  }
941
943
  }
942
- if (c.push(C.status.message), U.length > 0 && c.push({
944
+ if (l.push(T.status.message), O.length > 0 && l.push({
943
945
  role: "agent",
944
946
  kind: "message",
945
- parts: U,
946
- messageId: $()
947
- }), v) {
948
- const M = Ie(c), b = Y(
947
+ parts: O,
948
+ messageId: N()
949
+ }), q) {
950
+ const S = we(l), b = Z(
949
951
  [],
950
952
  // Empty array - tool results are already in historyDataParts
951
- M
953
+ S
952
954
  );
953
955
  yield {
954
- id: C.id,
956
+ id: T.id,
955
957
  status: {
956
958
  state: "working",
957
959
  message: b
@@ -959,99 +961,99 @@ async function* Ne(e, s, t, a, n, r, o = !0, c = [], l, d) {
959
961
  final: !1,
960
962
  text: ""
961
963
  };
962
- const R = await we(
963
- C.id,
964
+ const $ = await Te(
965
+ T.id,
964
966
  b,
965
967
  a,
966
968
  s,
967
969
  t,
968
970
  n,
969
- r,
970
- l,
971
+ o,
972
+ i,
971
973
  d,
972
- c
974
+ l
973
975
  );
974
- let k = null;
975
- for await (const q of R)
976
- q.final ? k = q : yield q;
977
- if (!k)
976
+ let R = null;
977
+ for await (const U of $)
978
+ U.final ? R = U : yield U;
979
+ if (!R)
978
980
  throw new Error(
979
981
  "Continue task stream ended without final result"
980
982
  );
981
- let E = (y = k.status) != null && y.message ? K(
982
- k.status.message
983
- ) : [], _ = k;
984
- if (E.length > 0)
983
+ let P = (v = R.status) != null && v.message ? W(
984
+ R.status.message
985
+ ) : [], x = R;
986
+ if (P.length > 0)
985
987
  for (yield {
986
- ...k,
988
+ ...R,
987
989
  final: !1,
988
990
  text: D(
989
- ((x = k.status) == null ? void 0 : x.message) || {
991
+ ((y = R.status) == null ? void 0 : y.message) || {
990
992
  parts: [],
991
- messageId: $()
993
+ messageId: N()
992
994
  }
993
995
  )
994
- }; E.length > 0; ) {
995
- (I = _.status) != null && I.message && c.push(
996
- _.status.message
996
+ }; P.length > 0; ) {
997
+ (I = x.status) != null && I.message && l.push(
998
+ x.status.message
997
999
  );
998
1000
  const {
999
- results: q,
1000
- shouldReturnToAgent: H
1001
- } = await Oe(
1002
- E,
1001
+ results: U,
1002
+ shouldReturnToAgent: K
1003
+ } = await Ne(
1004
+ P,
1003
1005
  s,
1004
- (i = (f = _.status) == null ? void 0 : f.message) == null ? void 0 : i.messageId
1006
+ (g = (A = x.status) == null ? void 0 : A.message) == null ? void 0 : g.messageId
1005
1007
  );
1006
- if (q.length > 0 && (yield {
1007
- id: _.id,
1008
+ if (U.length > 0 && (yield {
1009
+ id: x.id,
1008
1010
  status: {
1009
1011
  state: "working",
1010
1012
  message: {
1011
1013
  role: "agent",
1012
1014
  kind: "message",
1013
- parts: q,
1014
- messageId: $()
1015
+ parts: U,
1016
+ messageId: N()
1015
1017
  }
1016
1018
  // Simple message with just the results
1017
1019
  },
1018
1020
  final: !1,
1019
1021
  text: ""
1020
- }), H) {
1021
- const O = o ? Ie(
1022
- c
1023
- ) : [], W = Y(
1024
- q,
1025
- O
1026
- ), V = await we(
1027
- _.id,
1028
- W,
1022
+ }), K) {
1023
+ const L = r ? we(
1024
+ l
1025
+ ) : [], H = Z(
1026
+ U,
1027
+ L
1028
+ ), E = await Te(
1029
+ x.id,
1030
+ H,
1029
1031
  a,
1030
1032
  s,
1031
1033
  t,
1032
1034
  n,
1033
- r,
1034
- l,
1035
+ o,
1036
+ i,
1035
1037
  d,
1036
- c
1038
+ l
1037
1039
  );
1038
- let j = null;
1039
- for await (const te of V)
1040
- te.final ? j = te : yield te;
1041
- if (!j)
1040
+ let F = null;
1041
+ for await (const G of E)
1042
+ G.final ? F = G : yield G;
1043
+ if (!F)
1042
1044
  throw new Error(
1043
1045
  "Continue task stream ended without final result"
1044
1046
  );
1045
- _ = j, E = (w = _.status) != null && w.message ? K(
1046
- _.status.message
1047
- ) : [], E.length > 0 && (yield {
1048
- id: _.id,
1049
- status: _.status,
1047
+ x = F, P = (c = x.status) != null && c.message ? W(
1048
+ x.status.message
1049
+ ) : [], P.length > 0 && (yield {
1050
+ id: x.id,
1051
+ status: x.status,
1050
1052
  final: !1,
1051
1053
  text: D(
1052
- ((T = _.status) == null ? void 0 : T.message) || {
1054
+ ((w = x.status) == null ? void 0 : w.message) || {
1053
1055
  parts: [],
1054
- messageId: $()
1056
+ messageId: N()
1055
1057
  }
1056
1058
  )
1057
1059
  });
@@ -1059,39 +1061,39 @@ async function* Ne(e, s, t, a, n, r, o = !0, c = [], l, d) {
1059
1061
  break;
1060
1062
  }
1061
1063
  yield {
1062
- ..._,
1064
+ ...x,
1063
1065
  final: !0,
1064
1066
  text: D(
1065
- ((S = _.status) == null ? void 0 : S.message) || {
1067
+ ((k = x.status) == null ? void 0 : k.message) || {
1066
1068
  parts: [],
1067
- messageId: $()
1069
+ messageId: N()
1068
1070
  }
1069
1071
  )
1070
1072
  };
1071
1073
  } else {
1072
- const M = {
1073
- ...C.status.message,
1074
- parts: h
1074
+ const S = {
1075
+ ...T.status.message,
1076
+ parts: p
1075
1077
  }, b = {
1076
- ...C,
1078
+ ...T,
1077
1079
  status: {
1078
- ...C.status,
1079
- message: M
1080
+ ...T.status,
1081
+ message: S
1080
1082
  },
1081
- final: P.length === 0,
1083
+ final: C.length === 0,
1082
1084
  // Only final if no agent messages to follow
1083
- text: D(M)
1085
+ text: D(S)
1084
1086
  };
1085
- if (yield b, P.length > 0) {
1086
- const R = P.map((E) => D(E)).join(" "), k = Q(R);
1087
+ if (yield b, C.length > 0) {
1088
+ const $ = C.map((P) => D(P)).join(" "), R = X($);
1087
1089
  yield {
1088
1090
  id: b.id,
1089
1091
  status: {
1090
1092
  state: "completed",
1091
- message: k
1093
+ message: R
1092
1094
  },
1093
1095
  final: !0,
1094
- text: R
1096
+ text: $
1095
1097
  };
1096
1098
  }
1097
1099
  }
@@ -1099,140 +1101,140 @@ async function* Ne(e, s, t, a, n, r, o = !0, c = [], l, d) {
1099
1101
  }
1100
1102
  }
1101
1103
  }
1102
- function it(e) {
1104
+ function lt(e) {
1103
1105
  const {
1104
1106
  agentId: s,
1105
1107
  agentUrl: t,
1106
1108
  authProvider: a,
1107
1109
  defaultSessionId: n,
1108
- timeout: r = at,
1109
- toolProvider: o,
1110
- contextProvider: c,
1111
- enableStreaming: l = !1,
1110
+ timeout: o = nt,
1111
+ toolProvider: r,
1112
+ contextProvider: l,
1113
+ enableStreaming: i = !1,
1112
1114
  fetchCallback: d = fetch
1113
1115
  } = e, u = {
1114
1116
  agentId: s,
1115
1117
  agentUrl: t,
1116
1118
  authProvider: a,
1117
- timeout: r
1119
+ timeout: o
1118
1120
  };
1119
1121
  return {
1120
1122
  async sendMessage(m) {
1121
- var w, T;
1122
- const { abortSignal: p } = m, g = m.sessionId || n || void 0, y = [];
1123
- y.push(m.message);
1124
- const x = await Z(
1123
+ var c, w;
1124
+ const { abortSignal: h } = m, f = m.sessionId || n || void 0, v = [];
1125
+ v.push(m.message);
1126
+ const y = await ee(
1125
1127
  m,
1126
1128
  u,
1127
1129
  { isStreaming: !1 },
1128
- o,
1129
- c,
1130
- g
1130
+ r,
1131
+ l,
1132
+ f
1131
1133
  );
1132
- let I = await _e(
1133
- x,
1134
+ let I = await Ee(
1135
+ y,
1134
1136
  u,
1135
- { abortSignal: p },
1137
+ { abortSignal: h },
1136
1138
  d
1137
1139
  );
1138
- const f = [], i = [];
1139
- for (; I.status.message && o; ) {
1140
- const S = K(
1140
+ const A = [], g = [];
1141
+ for (; I.status.message && r; ) {
1142
+ const k = W(
1141
1143
  I.status.message
1142
1144
  );
1143
- if (S.length === 0)
1145
+ if (k.length === 0)
1144
1146
  break;
1145
- f.push(...S);
1146
- const C = [];
1147
- let N = !1;
1148
- for (const U of S) {
1147
+ A.push(...k);
1148
+ const T = [];
1149
+ let _ = !1;
1150
+ for (const O of k) {
1149
1151
  const {
1150
- toolCallId: v,
1151
- toolId: h,
1152
- arguments: P
1153
- } = U.data;
1152
+ toolCallId: q,
1153
+ toolId: p,
1154
+ arguments: C
1155
+ } = O.data;
1154
1156
  try {
1155
- const M = await ee(
1156
- o,
1157
- h,
1158
- P
1159
- ), { result: b, returnToAgent: R, agentMessage: k } = ge(M);
1160
- R && (N = !0), k && i.push(
1161
- Q(k)
1157
+ const S = await te(
1158
+ r,
1159
+ p,
1160
+ C
1161
+ ), { result: b, returnToAgent: $, agentMessage: R } = ge(S);
1162
+ $ && (_ = !0), R && g.push(
1163
+ X(R)
1162
1164
  );
1163
- const E = B(
1164
- v,
1165
- h,
1165
+ const P = J(
1166
+ q,
1167
+ p,
1166
1168
  b
1167
1169
  );
1168
- C.push(E), f.push(E);
1169
- } catch (M) {
1170
- const b = B(
1171
- v,
1172
- h,
1170
+ T.push(P), A.push(P);
1171
+ } catch (S) {
1172
+ const b = J(
1173
+ q,
1174
+ p,
1173
1175
  void 0,
1174
- M instanceof Error ? M.message : String(M)
1176
+ S instanceof Error ? S.message : String(S)
1175
1177
  );
1176
- C.push(b), f.push(b);
1178
+ T.push(b), A.push(b);
1177
1179
  }
1178
1180
  }
1179
- if (y.push(I.status.message), N) {
1180
- const U = Y(C);
1181
+ if (v.push(I.status.message), _) {
1182
+ const O = Z(T);
1181
1183
  I = await ne(
1182
1184
  I.id,
1183
- U,
1185
+ O,
1184
1186
  u,
1185
1187
  d,
1186
- o,
1187
- c,
1188
- g,
1189
- p
1188
+ r,
1189
+ l,
1190
+ f,
1191
+ h
1190
1192
  );
1191
1193
  } else
1192
1194
  break;
1193
1195
  }
1194
- if (f.length > 0 && (w = I.status) != null && w.message) {
1195
- const S = {
1196
+ if (A.length > 0 && (c = I.status) != null && c.message) {
1197
+ const k = {
1196
1198
  ...I.status.message,
1197
- parts: f
1199
+ parts: A
1198
1200
  };
1199
1201
  I = {
1200
1202
  ...I,
1201
1203
  status: {
1202
1204
  ...I.status,
1203
- message: S
1205
+ message: k
1204
1206
  }
1205
1207
  };
1206
1208
  }
1207
- if (i.length > 0) {
1208
- const S = i.map((N) => D(N)).join(" "), C = Q(S);
1209
+ if (g.length > 0) {
1210
+ const k = g.map((_) => D(_)).join(" "), T = X(k);
1209
1211
  return {
1210
1212
  ...I,
1211
1213
  // Keep the enhanced message with tool results
1212
1214
  // The agent message will be handled separately by the caller
1213
- text: S,
1214
- agentMessage: C
1215
+ text: k,
1216
+ agentMessage: T
1215
1217
  // Add this for the caller to handle
1216
1218
  };
1217
1219
  }
1218
1220
  return {
1219
1221
  ...I,
1220
1222
  text: D(
1221
- ((T = I.status) == null ? void 0 : T.message) || {
1223
+ ((w = I.status) == null ? void 0 : w.message) || {
1222
1224
  parts: [],
1223
- messageId: $()
1225
+ messageId: N()
1224
1226
  }
1225
1227
  )
1226
1228
  };
1227
1229
  },
1228
1230
  async *sendMessageStream(m) {
1229
1231
  const {
1230
- withHistory: p = !0,
1231
- abortSignal: g,
1232
- enableStreaming: y
1233
- } = m, x = m.sessionId || n || void 0, I = y ?? l, f = [];
1234
- f.push(m.message);
1235
- const i = await Z(
1232
+ withHistory: h = !0,
1233
+ abortSignal: f,
1234
+ enableStreaming: v
1235
+ } = m, y = m.sessionId || n || void 0, I = v ?? i, A = [];
1236
+ A.push(m.message);
1237
+ const g = await ee(
1236
1238
  m,
1237
1239
  u,
1238
1240
  {
@@ -1240,66 +1242,66 @@ function it(e) {
1240
1242
  // Always use message/stream endpoint for SSE
1241
1243
  enableTokenStreaming: I
1242
1244
  },
1243
- o,
1244
- c,
1245
- x
1246
- ), w = Ee(
1247
- i,
1245
+ r,
1246
+ l,
1247
+ y
1248
+ ), c = Oe(
1249
+ g,
1248
1250
  u,
1249
1251
  {
1250
1252
  enableTokenStreaming: I,
1251
1253
  // Token streaming is optional
1252
- streamingTimeout: r,
1253
- abortSignal: g
1254
+ streamingTimeout: o,
1255
+ abortSignal: f
1254
1256
  },
1255
1257
  d
1256
1258
  );
1257
- yield* Ne(
1258
- w,
1259
- o,
1259
+ yield* $e(
1260
1260
  c,
1261
+ r,
1262
+ l,
1261
1263
  u,
1262
1264
  d,
1263
- x,
1264
- p,
1265
+ y,
1266
+ h,
1267
+ A,
1265
1268
  f,
1266
- g,
1267
1269
  {
1268
1270
  isStreaming: !0,
1269
1271
  enableTokenStreaming: I,
1270
- streamingTimeout: r
1272
+ streamingTimeout: o
1271
1273
  }
1272
1274
  );
1273
1275
  },
1274
- async continueTask(m, p, g) {
1275
- var f;
1276
- const y = le(p);
1276
+ async continueTask(m, h, f) {
1277
+ var A;
1278
+ const v = le(h);
1277
1279
  let I = await ne(
1278
1280
  m,
1279
- y,
1281
+ v,
1280
1282
  u,
1281
1283
  d,
1282
- o,
1283
- c,
1284
- g
1284
+ r,
1285
+ l,
1286
+ f
1285
1287
  );
1286
- for (; I.status.state === "input-required" && I.status.message && o; ) {
1287
- const i = K(
1288
+ for (; I.status.state === "input-required" && I.status.message && r; ) {
1289
+ const g = W(
1288
1290
  I.status.message
1289
1291
  );
1290
- if (i.length === 0)
1292
+ if (g.length === 0)
1291
1293
  break;
1292
- const { results: w, shouldReturnToAgent: T } = await Oe(i, o);
1293
- if (T) {
1294
- const S = Y(w);
1294
+ const { results: c, shouldReturnToAgent: w } = await Ne(g, r);
1295
+ if (w) {
1296
+ const k = Z(c);
1295
1297
  I = await ne(
1296
1298
  I.id,
1297
- S,
1299
+ k,
1298
1300
  u,
1299
1301
  d,
1300
- o,
1301
- c,
1302
- g
1302
+ r,
1303
+ l,
1304
+ f
1303
1305
  );
1304
1306
  } else
1305
1307
  break;
@@ -1307,9 +1309,9 @@ function it(e) {
1307
1309
  return {
1308
1310
  ...I,
1309
1311
  text: D(
1310
- ((f = I.status) == null ? void 0 : f.message) || {
1312
+ ((A = I.status) == null ? void 0 : A.message) || {
1311
1313
  parts: [],
1312
- messageId: $()
1314
+ messageId: N()
1313
1315
  }
1314
1316
  )
1315
1317
  };
@@ -1322,37 +1324,37 @@ function it(e) {
1322
1324
  }
1323
1325
  };
1324
1326
  }
1325
- function lt(e) {
1327
+ function ct(e) {
1326
1328
  const s = [];
1327
1329
  if (e.content) {
1328
- const r = {
1330
+ const o = {
1329
1331
  type: "text",
1330
1332
  text: e.content
1331
1333
  };
1332
- s.push(r);
1334
+ s.push(o);
1333
1335
  }
1334
1336
  const t = e.context;
1335
1337
  if (t && !Array.isArray(t) && Array.isArray(t.file_parts)) {
1336
- for (const r of t.file_parts)
1337
- if (r != null && r.uri) {
1338
- const o = {
1338
+ for (const o of t.file_parts)
1339
+ if (o != null && o.uri) {
1340
+ const r = {
1339
1341
  type: "file",
1340
1342
  file: {
1341
1343
  name: "image",
1342
- uri: r.uri
1344
+ uri: o.uri
1343
1345
  },
1344
1346
  // Pass attachment ID in metadata if available
1345
- metadata: r.id ? { id: r.id } : void 0
1347
+ metadata: o.id ? { id: o.id } : void 0
1346
1348
  };
1347
- s.push(o);
1349
+ s.push(r);
1348
1350
  }
1349
1351
  }
1350
1352
  if (t && !Array.isArray(t) && Array.isArray(t.sources) && t.sources.length > 0) {
1351
- const r = {
1353
+ const o = {
1352
1354
  type: "data",
1353
1355
  data: { sources: t.sources }
1354
1356
  };
1355
- s.push(r);
1357
+ s.push(o);
1356
1358
  }
1357
1359
  const a = e.role === "user" ? "user" : "agent", n = e.ts ? e.ts * 1e3 : (/* @__PURE__ */ new Date(
1358
1360
  e.created_at.replace(" ", "T") + "Z"
@@ -1361,7 +1363,7 @@ function lt(e) {
1361
1363
  role: a,
1362
1364
  kind: "message",
1363
1365
  parts: s,
1364
- messageId: $(),
1366
+ messageId: N(),
1365
1367
  metadata: {
1366
1368
  timestamp: n,
1367
1369
  serverId: e.message_id,
@@ -1369,7 +1371,7 @@ function lt(e) {
1369
1371
  }
1370
1372
  };
1371
1373
  }
1372
- function ct(e) {
1374
+ function ut(e) {
1373
1375
  if (e.role !== "tool_call" && e.role !== "tool_result")
1374
1376
  return !1;
1375
1377
  try {
@@ -1378,13 +1380,13 @@ function ct(e) {
1378
1380
  return !1;
1379
1381
  }
1380
1382
  }
1381
- function ut(e, s = !1) {
1382
- var r, o, c, l, d, u;
1383
- const a = e.messages.filter((m) => m.role === "tool_call" ? !1 : m.role === "tool_result" ? !(!s || ct(m)) : !0).map(lt), n = {
1384
- currentPage: ((r = e.metadata) == null ? void 0 : r.current_page) ?? 1,
1385
- itemsPerPage: ((o = e.metadata) == null ? void 0 : o.items_per_page) ?? 10,
1386
- totalPages: ((c = e.metadata) == null ? void 0 : c.total_pages) ?? 1,
1387
- totalMessages: ((l = e.metadata) == null ? void 0 : l.total_messages) ?? a.length,
1383
+ function dt(e, s = !1) {
1384
+ var o, r, l, i, d, u;
1385
+ const a = e.messages.filter((m) => m.role === "tool_call" ? !1 : m.role === "tool_result" ? !(!s || ut(m)) : !0).map(ct), n = {
1386
+ currentPage: ((o = e.metadata) == null ? void 0 : o.current_page) ?? 1,
1387
+ itemsPerPage: ((r = e.metadata) == null ? void 0 : r.items_per_page) ?? 10,
1388
+ totalPages: ((l = e.metadata) == null ? void 0 : l.total_pages) ?? 1,
1389
+ totalMessages: ((i = e.metadata) == null ? void 0 : i.total_messages) ?? a.length,
1388
1390
  hasMore: (((d = e.metadata) == null ? void 0 : d.current_page) ?? 1) < (((u = e.metadata) == null ? void 0 : u.total_pages) ?? 1)
1389
1391
  };
1390
1392
  return {
@@ -1401,107 +1403,107 @@ class z extends Error {
1401
1403
  }
1402
1404
  const me = "https://public-api.wordpress.com";
1403
1405
  async function ce(e, s, t = 1, a = 50, n = !1) {
1404
- const { botId: r, apiBaseUrl: o = me, authProvider: c } = s;
1405
- if (!e || !r)
1406
+ const { botId: o, apiBaseUrl: r = me, authProvider: l } = s;
1407
+ if (!e || !o)
1406
1408
  throw new Error(
1407
1409
  "chatId and botId are required to load conversation from server"
1408
1410
  );
1409
- const l = Math.max(1, Math.min(t, 100)), d = Math.max(1, Math.min(a, 100)), u = new URL(
1410
- `${o}/wpcom/v2/odie/chat/${encodeURIComponent(
1411
- r
1411
+ const i = Math.max(1, Math.min(t, 100)), d = Math.max(1, Math.min(a, 100)), u = new URL(
1412
+ `${r}/wpcom/v2/odie/chat/${encodeURIComponent(
1413
+ o
1412
1414
  )}/${encodeURIComponent(e)}`
1413
1415
  );
1414
- u.searchParams.set("page_number", l.toString()), u.searchParams.set("items_per_page", d.toString()), A(
1416
+ u.searchParams.set("page_number", i.toString()), u.searchParams.set("items_per_page", d.toString()), M(
1415
1417
  "Loading conversation from server: %s (page %d)",
1416
1418
  e,
1417
- l
1419
+ i
1418
1420
  );
1419
1421
  try {
1420
1422
  const m = {
1421
1423
  "Content-Type": "application/json"
1422
1424
  };
1423
- if (c) {
1424
- const x = await c();
1425
- Object.assign(m, x);
1425
+ if (l) {
1426
+ const y = await l();
1427
+ Object.assign(m, y);
1426
1428
  }
1427
- const p = await fetch(u.toString(), {
1429
+ const h = await fetch(u.toString(), {
1428
1430
  method: "GET",
1429
1431
  headers: m
1430
1432
  });
1431
- if (!p.ok) {
1432
- const x = await p.text();
1433
- let I = `Failed to load conversation from server: ${p.status} ${p.statusText}`;
1433
+ if (!h.ok) {
1434
+ const y = await h.text();
1435
+ let I = `Failed to load conversation from server: ${h.status} ${h.statusText}`;
1434
1436
  try {
1435
- const i = JSON.parse(x);
1436
- i.message && (I = i.message);
1437
+ const g = JSON.parse(y);
1438
+ g.message && (I = g.message);
1437
1439
  } catch {
1438
1440
  }
1439
- const f = new z(
1441
+ const A = new z(
1440
1442
  I,
1441
- p.status,
1442
- x
1443
+ h.status,
1444
+ y
1443
1445
  );
1444
- throw A("Failed to load conversation from server: %O", f), f;
1446
+ throw M("Failed to load conversation from server: %O", A), A;
1445
1447
  }
1446
- const g = await p.json(), y = ut(g, n);
1447
- return A(
1448
+ const f = await h.json(), v = dt(f, n);
1449
+ return M(
1448
1450
  "Loaded %d messages from server (page %d/%d)",
1449
- y.messages.length,
1450
- y.pagination.currentPage,
1451
- y.pagination.totalPages
1452
- ), y;
1451
+ v.messages.length,
1452
+ v.pagination.currentPage,
1453
+ v.pagination.totalPages
1454
+ ), v;
1453
1455
  } catch (m) {
1454
1456
  if (m instanceof z)
1455
1457
  throw m;
1456
- const p = new z(
1458
+ const h = new z(
1457
1459
  `Network error loading conversation: ${m.message}`,
1458
1460
  void 0,
1459
1461
  m
1460
1462
  );
1461
- throw A("Network error loading conversation: %O", m), p;
1463
+ throw M("Network error loading conversation: %O", m), h;
1462
1464
  }
1463
1465
  }
1464
1466
  async function Lt(e, s, t = !1) {
1465
- const { apiBaseUrl: a = me, authProvider: n } = s, r = new URL(
1467
+ const { apiBaseUrl: a = me, authProvider: n } = s, o = new URL(
1466
1468
  `${a}/wpcom/v2/odie/conversations/${encodeURIComponent(
1467
1469
  e
1468
1470
  )}`
1469
1471
  );
1470
- r.searchParams.set(
1472
+ o.searchParams.set(
1471
1473
  "truncation_method",
1472
1474
  t ? "first_message" : "last_message"
1473
- ), A("Listing conversations from server for bot: %s", e);
1475
+ ), M("Listing conversations from server for bot: %s", e);
1474
1476
  try {
1475
- const o = {
1477
+ const r = {
1476
1478
  "Content-Type": "application/json"
1477
1479
  };
1478
1480
  if (n) {
1479
1481
  const d = await n();
1480
- Object.assign(o, d);
1482
+ Object.assign(r, d);
1481
1483
  }
1482
- const c = await fetch(r.toString(), {
1484
+ const l = await fetch(o.toString(), {
1483
1485
  method: "GET",
1484
- headers: o
1486
+ headers: r
1485
1487
  });
1486
- if (!c.ok) {
1487
- const d = await c.text(), u = `Failed to list conversations: ${c.status} ${c.statusText}`, m = new z(
1488
+ if (!l.ok) {
1489
+ const d = await l.text(), u = `Failed to list conversations: ${l.status} ${l.statusText}`, m = new z(
1488
1490
  u,
1489
- c.status,
1491
+ l.status,
1490
1492
  d
1491
1493
  );
1492
- throw A("Failed to list conversations: %O", m), m;
1494
+ throw M("Failed to list conversations: %O", m), m;
1493
1495
  }
1494
- const l = await c.json();
1495
- return A("Loaded %d conversations from server", l.length), l;
1496
- } catch (o) {
1497
- if (o instanceof z)
1498
- throw o;
1499
- const c = new z(
1500
- `Network error listing conversations: ${o.message}`,
1496
+ const i = await l.json();
1497
+ return M("Loaded %d conversations from server", i.length), i;
1498
+ } catch (r) {
1499
+ if (r instanceof z)
1500
+ throw r;
1501
+ const l = new z(
1502
+ `Network error listing conversations: ${r.message}`,
1501
1503
  void 0,
1502
- o
1504
+ r
1503
1505
  );
1504
- throw A("Network error listing conversations: %O", o), c;
1506
+ throw M("Network error listing conversations: %O", r), l;
1505
1507
  }
1506
1508
  }
1507
1509
  async function Bt(e, s, t = 10, a = !1) {
@@ -1514,75 +1516,75 @@ async function Bt(e, s, t = 10, a = !1) {
1514
1516
  );
1515
1517
  if (!n.pagination.hasMore)
1516
1518
  return n;
1517
- const r = Math.min(
1519
+ const o = Math.min(
1518
1520
  n.pagination.totalPages,
1519
1521
  t
1520
- ), o = [...n.messages], c = [];
1521
- for (let l = 2; l <= r; l++)
1522
- c.push(
1523
- ce(e, s, l, 50, a)
1522
+ ), r = [...n.messages], l = [];
1523
+ for (let i = 2; i <= o; i++)
1524
+ l.push(
1525
+ ce(e, s, i, 50, a)
1524
1526
  );
1525
1527
  try {
1526
- const l = await Promise.all(c);
1527
- for (const d of l)
1528
- o.push(...d.messages);
1528
+ const i = await Promise.all(l);
1529
+ for (const d of i)
1530
+ r.push(...d.messages);
1529
1531
  return {
1530
- messages: o,
1532
+ messages: r,
1531
1533
  pagination: {
1532
1534
  ...n.pagination,
1533
- currentPage: r,
1534
- hasMore: r < n.pagination.totalPages
1535
+ currentPage: o,
1536
+ hasMore: o < n.pagination.totalPages
1535
1537
  },
1536
1538
  chatId: n.chatId
1537
1539
  };
1538
- } catch (l) {
1539
- return A("Failed to load all pages: %O", l), {
1540
- messages: o,
1540
+ } catch (i) {
1541
+ return M("Failed to load all pages: %O", i), {
1542
+ messages: r,
1541
1543
  pagination: n.pagination,
1542
1544
  chatId: n.chatId
1543
1545
  };
1544
1546
  }
1545
1547
  }
1546
1548
  const pe = "a8c_agenttic_conversation_history";
1547
- function dt(e) {
1548
- var m, p;
1549
+ function gt(e) {
1550
+ var m, h;
1549
1551
  const s = e.parts.filter(
1550
- (g) => g.type === "text"
1551
- ), t = s.map((g) => g.text).join(`
1552
+ (f) => f.type === "text"
1553
+ ), t = s.map((f) => f.text).join(`
1552
1554
  `), a = s.some(
1553
- (g) => {
1554
- var y;
1555
- return ((y = g.metadata) == null ? void 0 : y.contentType) === "context";
1555
+ (f) => {
1556
+ var v;
1557
+ return ((v = f.metadata) == null ? void 0 : v.contentType) === "context";
1556
1558
  }
1557
1559
  ) ? "context" : void 0, n = e.parts.filter(
1558
- (g) => g.type === "data" && "toolCallId" in g.data && "arguments" in g.data
1559
- ).map((g) => ({
1560
- toolCallId: g.data.toolCallId,
1561
- toolId: g.data.toolId,
1562
- arguments: g.data.arguments
1563
- })), r = e.parts.filter(
1564
- (g) => g.type === "data" && "toolCallId" in g.data && "result" in g.data
1565
- ).map((g) => ({
1566
- toolCallId: g.data.toolCallId,
1567
- result: g.data.result,
1568
- error: g.data.error
1569
- })), o = e.parts.filter((g) => g.type === "file").map((g) => ({
1570
- name: g.file.name,
1571
- mimeType: g.file.mimeType,
1572
- uri: g.file.uri
1573
- })), l = n.length > 0 || r.length > 0 ? "agent" : e.role, d = ((m = e.metadata) == null ? void 0 : m.timestamp) ?? Date.now(), u = ((p = e.metadata) == null ? void 0 : p.archived) ?? void 0;
1560
+ (f) => f.type === "data" && "toolCallId" in f.data && "arguments" in f.data
1561
+ ).map((f) => ({
1562
+ toolCallId: f.data.toolCallId,
1563
+ toolId: f.data.toolId,
1564
+ arguments: f.data.arguments
1565
+ })), o = e.parts.filter(
1566
+ (f) => f.type === "data" && "toolCallId" in f.data && "result" in f.data
1567
+ ).map((f) => ({
1568
+ toolCallId: f.data.toolCallId,
1569
+ result: f.data.result,
1570
+ error: f.data.error
1571
+ })), r = e.parts.filter((f) => f.type === "file").map((f) => ({
1572
+ name: f.file.name,
1573
+ mimeType: f.file.mimeType,
1574
+ uri: f.file.uri
1575
+ })), i = n.length > 0 || o.length > 0 ? "agent" : e.role, d = ((m = e.metadata) == null ? void 0 : m.timestamp) ?? Date.now(), u = ((h = e.metadata) == null ? void 0 : h.archived) ?? void 0;
1574
1576
  return {
1575
- role: l,
1577
+ role: i,
1576
1578
  content: t || "(No text content)",
1577
1579
  timestamp: d,
1578
1580
  ...u !== void 0 && { archived: u },
1579
1581
  ...a && { contentType: a },
1580
- ...o.length > 0 && { files: o },
1582
+ ...r.length > 0 && { files: r },
1581
1583
  ...n.length > 0 && { toolCalls: n },
1582
- ...r.length > 0 && { toolResults: r }
1584
+ ...o.length > 0 && { toolResults: o }
1583
1585
  };
1584
1586
  }
1585
- function gt(e) {
1587
+ function ft(e) {
1586
1588
  const s = [];
1587
1589
  if (e.content && e.content !== "(No text content)" && s.push({
1588
1590
  type: "text",
@@ -1626,7 +1628,7 @@ function gt(e) {
1626
1628
  role: e.role,
1627
1629
  kind: "message",
1628
1630
  parts: s,
1629
- messageId: $(),
1631
+ messageId: N(),
1630
1632
  metadata: {
1631
1633
  timestamp: e.timestamp,
1632
1634
  // only store archived if it was already present.
@@ -1636,18 +1638,18 @@ function gt(e) {
1636
1638
  }
1637
1639
  };
1638
1640
  }
1639
- const L = /* @__PURE__ */ new Map(), ft = 50;
1640
- async function mt(e, s, t) {
1641
+ const V = /* @__PURE__ */ new Map(), mt = 50;
1642
+ async function pt(e, s, t) {
1641
1643
  const a = t || e;
1642
- if (L.set(a, [...s]), L.size > ft) {
1643
- const n = L.keys().next().value;
1644
- n && L.delete(n);
1644
+ if (V.set(a, [...s]), V.size > mt) {
1645
+ const n = V.keys().next().value;
1646
+ n && V.delete(n);
1645
1647
  }
1646
1648
  if (!(typeof sessionStorage > "u"))
1647
1649
  try {
1648
1650
  const n = {
1649
1651
  storageKey: a,
1650
- messages: s.map(dt),
1652
+ messages: s.map(gt),
1651
1653
  lastUpdated: Date.now()
1652
1654
  };
1653
1655
  sessionStorage.setItem(
@@ -1655,26 +1657,26 @@ async function mt(e, s, t) {
1655
1657
  JSON.stringify(n)
1656
1658
  );
1657
1659
  } catch (n) {
1658
- A(
1660
+ M(
1659
1661
  "Failed to store conversation in sessionStorage for key %s: %O",
1660
1662
  a,
1661
1663
  n
1662
1664
  );
1663
1665
  }
1664
1666
  }
1665
- async function pt(e, s, t) {
1666
- return t != null && t.odieBotId ? ht(e, t) : yt(
1667
+ async function ht(e, s, t) {
1668
+ return t != null && t.odieBotId ? yt(e, t) : It(
1667
1669
  e,
1668
1670
  s
1669
1671
  );
1670
1672
  }
1671
- async function ht(e, s) {
1673
+ async function yt(e, s) {
1672
1674
  const { odieBotId: t, authProvider: a } = s;
1673
1675
  if (!t)
1674
1676
  throw new Error("odieBotId is required for server storage");
1675
1677
  const n = me;
1676
1678
  try {
1677
- const o = await ce(
1679
+ const r = await ce(
1678
1680
  e,
1679
1681
  {
1680
1682
  botId: t,
@@ -1684,25 +1686,25 @@ async function ht(e, s) {
1684
1686
  1,
1685
1687
  50
1686
1688
  );
1687
- return A(
1689
+ return M(
1688
1690
  "Loaded conversation from server: %s (%d messages, page %d/%d)",
1689
1691
  e,
1690
- o.messages.length,
1691
- o.pagination.currentPage,
1692
- o.pagination.totalPages
1692
+ r.messages.length,
1693
+ r.pagination.currentPage,
1694
+ r.pagination.totalPages
1693
1695
  ), {
1694
- messages: o.messages,
1695
- pagination: o.pagination
1696
+ messages: r.messages,
1697
+ pagination: r.pagination
1696
1698
  };
1697
- } catch (r) {
1698
- throw A("Failed to load conversation from server: %O", r), r;
1699
+ } catch (o) {
1700
+ throw M("Failed to load conversation from server: %O", o), o;
1699
1701
  }
1700
1702
  }
1701
- async function yt(e, s) {
1703
+ async function It(e, s) {
1702
1704
  const t = s || e;
1703
- if (L.has(t))
1705
+ if (V.has(t))
1704
1706
  return {
1705
- messages: [...L.get(t)]
1707
+ messages: [...V.get(t)]
1706
1708
  };
1707
1709
  if (typeof sessionStorage > "u")
1708
1710
  return { messages: [] };
@@ -1711,11 +1713,11 @@ async function yt(e, s) {
1711
1713
  `${pe}_${t}`
1712
1714
  );
1713
1715
  if (a) {
1714
- const r = JSON.parse(a).messages.map(gt);
1715
- return L.set(t, r), { messages: [...r] };
1716
+ const o = JSON.parse(a).messages.map(ft);
1717
+ return V.set(t, o), { messages: [...o] };
1716
1718
  }
1717
1719
  } catch (a) {
1718
- A(
1720
+ M(
1719
1721
  "Failed to load conversation from sessionStorage for key %s: %O",
1720
1722
  t,
1721
1723
  a
@@ -1723,20 +1725,20 @@ async function yt(e, s) {
1723
1725
  }
1724
1726
  return { messages: [] };
1725
1727
  }
1726
- async function It(e, s) {
1728
+ async function wt(e, s) {
1727
1729
  const t = s || e;
1728
- if (L.delete(t), !(typeof sessionStorage > "u"))
1730
+ if (V.delete(t), !(typeof sessionStorage > "u"))
1729
1731
  try {
1730
1732
  sessionStorage.removeItem(`${pe}_${t}`);
1731
1733
  } catch (a) {
1732
- A(
1734
+ M(
1733
1735
  "Failed to clear conversation from sessionStorage for key %s: %O",
1734
1736
  t,
1735
1737
  a
1736
1738
  );
1737
1739
  }
1738
1740
  }
1739
- function G(e) {
1741
+ function Q(e) {
1740
1742
  const s = e.parts.filter((t) => t.type === "text" ? !0 : t.type === "data" ? "role" in t.data && "text" in t.data ? !1 : !!("toolCallId" in t.data && "arguments" in t.data || "flags" in t.data && t.data.flags && typeof t.data.flags == "object" && "forward_to_human_support" in t.data.flags || "sources" in t.data && Array.isArray(t.data.sources) && t.data.sources.length > 0 || "toolCallId" in t.data && "result" in t.data) : !0);
1741
1743
  return {
1742
1744
  ...e,
@@ -1747,7 +1749,7 @@ function G(e) {
1747
1749
  }
1748
1750
  };
1749
1751
  }
1750
- function wt(e) {
1752
+ function Ue(e) {
1751
1753
  const s = [];
1752
1754
  for (const t of e)
1753
1755
  for (const a of t.parts)
@@ -1775,17 +1777,17 @@ function wt(e) {
1775
1777
  }
1776
1778
  return s;
1777
1779
  }
1778
- function Te(e, s = [], t = []) {
1779
- const a = wt(s), n = t.map((r) => {
1780
- const o = typeof r == "string" ? r : r.url, c = typeof r == "object" ? r.metadata : void 0, l = (c == null ? void 0 : c.fileType) || "image/jpeg";
1780
+ function Se(e, s = [], t = []) {
1781
+ const a = Ue(s), n = t.map((o) => {
1782
+ const r = typeof o == "string" ? o : o.url, l = typeof o == "object" ? o.metadata : void 0, i = (l == null ? void 0 : l.fileType) || "image/jpeg";
1781
1783
  return {
1782
1784
  type: "file",
1783
1785
  file: {
1784
- name: (c == null ? void 0 : c.fileName) || "image",
1785
- mimeType: l,
1786
- uri: o
1786
+ name: (l == null ? void 0 : l.fileName) || "image",
1787
+ mimeType: i,
1788
+ uri: r
1787
1789
  },
1788
- metadata: c
1790
+ metadata: l
1789
1791
  };
1790
1792
  });
1791
1793
  return {
@@ -1799,7 +1801,7 @@ function Te(e, s = [], t = []) {
1799
1801
  ...n
1800
1802
  ],
1801
1803
  kind: "message",
1802
- messageId: $(),
1804
+ messageId: N(),
1803
1805
  metadata: {
1804
1806
  timestamp: Date.now()
1805
1807
  }
@@ -1810,16 +1812,16 @@ function Tt(e) {
1810
1812
  (s) => s.type === "data" && "toolCallId" in s.data && "result" in s.data
1811
1813
  ) : [];
1812
1814
  }
1813
- function oe(e, s) {
1815
+ function re(e, s) {
1814
1816
  if (typeof localStorage > "u") {
1815
- J("localStorage not available, cannot update session ID");
1817
+ B("localStorage not available, cannot update session ID");
1816
1818
  return;
1817
1819
  }
1818
1820
  try {
1819
1821
  const t = localStorage.getItem(e);
1820
1822
  if (t) {
1821
1823
  const a = JSON.parse(t), n = a.sessionId;
1822
- a.sessionId = s, localStorage.setItem(e, JSON.stringify(a)), J(
1824
+ a.sessionId = s, localStorage.setItem(e, JSON.stringify(a)), B(
1823
1825
  "Updated localStorage[%s] session ID: %s -> %s",
1824
1826
  e,
1825
1827
  n,
@@ -1830,14 +1832,14 @@ function oe(e, s) {
1830
1832
  sessionId: s,
1831
1833
  timestamp: Date.now()
1832
1834
  };
1833
- localStorage.setItem(e, JSON.stringify(a)), J(
1835
+ localStorage.setItem(e, JSON.stringify(a)), B(
1834
1836
  "Created new session in localStorage[%s]: %s",
1835
1837
  e,
1836
1838
  s
1837
1839
  );
1838
1840
  }
1839
1841
  } catch (t) {
1840
- J(
1842
+ B(
1841
1843
  "Failed to update localStorage sessionId to %s: %O",
1842
1844
  s,
1843
1845
  t
@@ -1851,7 +1853,7 @@ async function St(e) {
1851
1853
  if (t.parts.some(
1852
1854
  (n) => n.type === "data" && "toolCallId" in n.data && "result" in n.data
1853
1855
  )) {
1854
- const n = rt(
1856
+ const n = it(
1855
1857
  t.parts
1856
1858
  );
1857
1859
  s.push({
@@ -1870,15 +1872,15 @@ function At() {
1870
1872
  const n = e.get(t);
1871
1873
  if (n != null && n.sessionId)
1872
1874
  try {
1873
- await mt(
1875
+ await pt(
1874
1876
  n.sessionId,
1875
1877
  a,
1876
1878
  n.conversationStorageKey
1877
1879
  );
1878
- } catch (r) {
1879
- J(
1880
+ } catch (o) {
1881
+ B(
1880
1882
  `Failed to persist conversation history for agent ${t}:`,
1881
- r
1883
+ o
1882
1884
  );
1883
1885
  }
1884
1886
  }
@@ -1886,30 +1888,30 @@ function At() {
1886
1888
  async createAgent(t, a) {
1887
1889
  if (e.has(t))
1888
1890
  return e.get(t).client;
1889
- const n = it(a), r = a.sessionId || null, o = a.conversationStorageKey, c = a.sessionIdStorageKey, l = {
1891
+ const n = lt(a), o = a.sessionId || null, r = a.conversationStorageKey, l = a.sessionIdStorageKey, i = {
1890
1892
  odieBotId: a.odieBotId,
1891
1893
  authProvider: a.authProvider
1892
1894
  };
1893
1895
  let d = [];
1894
- if (r)
1896
+ if (o)
1895
1897
  try {
1896
- d = (await pt(
1897
- r,
1898
+ d = (await ht(
1898
1899
  o,
1899
- l
1900
+ r,
1901
+ i
1900
1902
  )).messages;
1901
1903
  } catch (m) {
1902
- J(
1903
- `Failed to load conversation history for agent ${t} with session ${r}:`,
1904
+ B(
1905
+ `Failed to load conversation history for agent ${t} with session ${o}:`,
1904
1906
  m
1905
1907
  );
1906
1908
  }
1907
1909
  const u = {
1908
1910
  client: n,
1909
- sessionId: r,
1910
- conversationStorageKey: o,
1911
- sessionIdStorageKey: c,
1912
- storageConfig: l,
1911
+ sessionId: o,
1912
+ conversationStorageKey: r,
1913
+ sessionIdStorageKey: l,
1914
+ storageConfig: i,
1913
1915
  conversationHistory: d,
1914
1916
  currentAbortController: null
1915
1917
  };
@@ -1927,222 +1929,270 @@ function At() {
1927
1929
  },
1928
1930
  async sendMessage(t, a, n = {}) {
1929
1931
  var I;
1930
- const r = e.get(t);
1931
- if (!r)
1932
+ const o = e.get(t);
1933
+ if (!o)
1932
1934
  throw new Error(`Agent with key "${t}" not found`);
1933
- const { withHistory: o = !0, sessionId: c, ...l } = n, { client: d, conversationHistory: u } = r, m = n.message || Te(
1935
+ const { withHistory: r = !0, sessionId: l, ...i } = n, { client: d, conversationHistory: u } = o, m = n.message || Se(
1934
1936
  a,
1935
1937
  u,
1936
1938
  n.imageUrls
1937
- ), p = await d.sendMessage({
1939
+ ), h = await d.sendMessage({
1938
1940
  message: m,
1939
- withHistory: o,
1940
- sessionId: c || r.sessionId || void 0,
1941
- ...l
1941
+ withHistory: r,
1942
+ sessionId: l || o.sessionId || void 0,
1943
+ ...i
1942
1944
  });
1943
- if (p.sessionId) {
1944
- const f = r.sessionId;
1945
- r.sessionId = p.sessionId, f && p.sessionId && f !== p.sessionId && r.sessionIdStorageKey && (J(
1945
+ if (h.sessionId) {
1946
+ const A = o.sessionId;
1947
+ o.sessionId = h.sessionId, A && h.sessionId && A !== h.sessionId && o.sessionIdStorageKey && (B(
1946
1948
  "Session ID changed from %s to %s, updating localStorage",
1947
- f,
1948
- p.sessionId
1949
- ), oe(
1950
- r.sessionIdStorageKey,
1951
- p.sessionId
1949
+ A,
1950
+ h.sessionId
1951
+ ), re(
1952
+ o.sessionIdStorageKey,
1953
+ h.sessionId
1952
1954
  ));
1953
1955
  }
1954
- let g = null;
1955
- if ((I = p.status) != null && I.message) {
1956
- const f = p.status.message.parts.filter(
1957
- (w) => w.type === "data" && "toolCallId" in w.data && ("arguments" in w.data || "result" in w.data)
1958
- ), i = p.status.message.parts.filter(
1959
- (w) => w.type === "text"
1956
+ let f = null;
1957
+ if ((I = h.status) != null && I.message) {
1958
+ const A = h.status.message.parts.filter(
1959
+ (c) => c.type === "data" && "toolCallId" in c.data && ("arguments" in c.data || "result" in c.data)
1960
+ ), g = h.status.message.parts.filter(
1961
+ (c) => c.type === "text"
1960
1962
  );
1961
- g = {
1963
+ f = {
1962
1964
  role: "agent",
1963
1965
  kind: "message",
1964
- parts: [...f, ...i],
1965
- messageId: $(),
1966
+ parts: [...A, ...g],
1967
+ messageId: N(),
1966
1968
  metadata: {
1967
1969
  timestamp: Date.now()
1968
1970
  }
1969
1971
  };
1970
1972
  }
1971
- const y = [
1973
+ const v = [
1972
1974
  ...u,
1973
1975
  // Store only the new content from the user message (without history parts)
1974
1976
  le(a),
1975
1977
  // Add complete agent response with tool calls/results if present
1976
- ...g ? [G(g)] : []
1978
+ ...f ? [Q(f)] : []
1977
1979
  ];
1978
- let x = y;
1979
- if (p.agentMessage) {
1980
- const f = G(
1981
- p.agentMessage
1980
+ let y = v;
1981
+ if (h.agentMessage) {
1982
+ const A = Q(
1983
+ h.agentMessage
1982
1984
  );
1983
- x = [
1984
- ...y,
1985
- f
1985
+ y = [
1986
+ ...v,
1987
+ A
1986
1988
  ];
1987
1989
  }
1988
- return r.conversationHistory = x, o && await s(
1990
+ return o.conversationHistory = y, r && await s(
1989
1991
  t,
1990
- x
1991
- ), p;
1992
+ y
1993
+ ), h;
1992
1994
  },
1993
1995
  async *sendMessageStream(t, a, n = {}) {
1994
- var w, T, S, C, N, U;
1995
- const r = e.get(t);
1996
- if (!r)
1996
+ var w, k, T, _, O, q;
1997
+ const o = e.get(t);
1998
+ if (!o)
1997
1999
  throw new Error(`Agent with key "${t}" not found`);
1998
2000
  const {
1999
- withHistory: o = !0,
2000
- abortSignal: c,
2001
- metadata: l,
2001
+ withHistory: r = !0,
2002
+ abortSignal: l,
2003
+ metadata: i,
2002
2004
  sessionId: d,
2003
- ...u
2004
- } = n, { client: m } = r, p = l ? (({ contentType: v, ...h }) => h)(l) : void 0, g = new AbortController();
2005
- r.currentAbortController = g, c && c.addEventListener(
2005
+ message: u,
2006
+ ...m
2007
+ } = n, { client: h } = o, f = i ? (({ contentType: p, ...C }) => C)(i) : void 0, v = new AbortController();
2008
+ o.currentAbortController = v, l && l.addEventListener(
2006
2009
  "abort",
2007
- () => g.abort()
2010
+ () => v.abort()
2008
2011
  );
2009
2012
  let y = [
2010
- ...r.conversationHistory
2011
- ], x = [];
2012
- const I = await St(
2013
+ ...o.conversationHistory
2014
+ ], I = [];
2015
+ const A = await St(
2013
2016
  y
2014
2017
  );
2015
- r.conversationHistory = I, y = I, o && await s(
2018
+ o.conversationHistory = A, y = A, r && await s(
2016
2019
  t,
2017
- I
2020
+ A
2018
2021
  );
2019
- const f = n.message || Te(
2020
- a,
2021
- I,
2022
- n.imageUrls
2023
- );
2024
- if (n.metadata && !n.message) {
2025
- const { contentType: v, ...h } = n.metadata;
2026
- if (v) {
2027
- const P = f.parts[f.parts.length - 1];
2028
- P && P.type === "text" && (P.metadata = {
2029
- ...P.metadata,
2030
- contentType: v
2022
+ let g;
2023
+ if (u) {
2024
+ const p = Ue(
2025
+ A
2026
+ );
2027
+ g = {
2028
+ ...u,
2029
+ parts: [...p, ...u.parts]
2030
+ };
2031
+ } else
2032
+ g = Se(
2033
+ a,
2034
+ A,
2035
+ n.imageUrls
2036
+ );
2037
+ if (n.metadata && !u) {
2038
+ const { contentType: p, ...C } = n.metadata;
2039
+ if (p) {
2040
+ const S = g.parts[g.parts.length - 1];
2041
+ S && S.type === "text" && (S.metadata = {
2042
+ ...S.metadata,
2043
+ contentType: p
2031
2044
  });
2032
2045
  }
2033
- Object.keys(h).length > 0 && (f.metadata = {
2034
- ...f.metadata,
2035
- ...h
2046
+ Object.keys(C).length > 0 && (g.metadata = {
2047
+ ...g.metadata,
2048
+ ...C
2036
2049
  });
2037
2050
  }
2038
- const i = le(a, n.metadata);
2051
+ const c = u || le(a, n.metadata);
2039
2052
  if (n.imageUrls && n.imageUrls.length > 0) {
2040
- const v = n.imageUrls.map(
2041
- (h) => {
2042
- const P = typeof h == "string" ? h : h.url, M = typeof h == "string" ? void 0 : h.metadata, b = (M == null ? void 0 : M.fileType) || "image/jpeg";
2053
+ const p = n.imageUrls.map(
2054
+ (C) => {
2055
+ const S = typeof C == "string" ? C : C.url, b = typeof C == "string" ? void 0 : C.metadata, $ = (b == null ? void 0 : b.fileType) || "image/jpeg";
2043
2056
  return {
2044
2057
  type: "file",
2045
2058
  file: {
2046
- name: (M == null ? void 0 : M.fileName) || "image",
2047
- mimeType: b,
2048
- uri: P
2059
+ name: (b == null ? void 0 : b.fileName) || "image",
2060
+ mimeType: $,
2061
+ uri: S
2049
2062
  },
2050
- metadata: M
2063
+ metadata: b
2051
2064
  };
2052
2065
  }
2053
2066
  );
2054
- i.parts.push(...v);
2067
+ c.parts.push(...p);
2055
2068
  }
2056
2069
  y = [
2057
2070
  ...y,
2058
- i
2059
- ], r.conversationHistory = y, o && await s(
2071
+ c
2072
+ ], o.conversationHistory = y, r && await s(
2060
2073
  t,
2061
2074
  y
2062
2075
  );
2063
- for await (const v of m.sendMessageStream({
2064
- message: f,
2065
- withHistory: o,
2066
- sessionId: d || r.sessionId || void 0,
2067
- abortSignal: g.signal,
2068
- ...u,
2069
- ...p && Object.keys(p).length > 0 && {
2070
- metadata: p
2076
+ for await (const p of h.sendMessageStream({
2077
+ message: g,
2078
+ withHistory: r,
2079
+ sessionId: d || o.sessionId || void 0,
2080
+ abortSignal: v.signal,
2081
+ ...m,
2082
+ ...f && Object.keys(f).length > 0 && {
2083
+ metadata: f
2071
2084
  }
2072
2085
  })) {
2073
- if (v.sessionId) {
2074
- const h = r.sessionId;
2075
- r.sessionId = v.sessionId, v.sessionId && h !== v.sessionId && r.sessionIdStorageKey && (J(
2086
+ if (p.sessionId) {
2087
+ const C = o.sessionId;
2088
+ o.sessionId = p.sessionId, p.sessionId && C !== p.sessionId && o.sessionIdStorageKey && (B(
2076
2089
  "Session ID %s, updating localStorage",
2077
- h ? `changed from ${h} to ${v.sessionId}` : `received: ${v.sessionId}`
2078
- ), oe(
2079
- r.sessionIdStorageKey,
2080
- v.sessionId
2090
+ C ? `changed from ${C} to ${p.sessionId}` : `received: ${p.sessionId}`
2091
+ ), re(
2092
+ o.sessionIdStorageKey,
2093
+ p.sessionId
2081
2094
  ));
2082
2095
  }
2083
- if (((w = v.status) == null ? void 0 : w.state) === "input-required" && ((T = v.status) != null && T.message)) {
2084
- x = K(
2085
- v.status.message
2096
+ if (((w = p.status) == null ? void 0 : w.state) === "input-required" && ((k = p.status) != null && k.message)) {
2097
+ I = W(
2098
+ p.status.message
2086
2099
  ).map(
2087
- (M) => M.data.toolCallId
2100
+ (b) => b.data.toolCallId
2088
2101
  );
2089
- const P = G(
2090
- v.status.message
2102
+ const S = Q(
2103
+ p.status.message
2091
2104
  );
2092
2105
  y = [
2093
2106
  ...y,
2094
- P
2095
- ], r.conversationHistory = y, o && await s(
2107
+ S
2108
+ ], o.conversationHistory = y, r && await s(
2096
2109
  t,
2097
2110
  y
2098
2111
  );
2099
2112
  }
2100
- if (((S = v.status) == null ? void 0 : S.state) === "working" && ((C = v.status) != null && C.message) && !v.final) {
2101
- const P = Tt(
2102
- v.status.message
2113
+ if (((T = p.status) == null ? void 0 : T.state) === "working" && ((_ = p.status) != null && _.message) && !p.final) {
2114
+ const S = Tt(
2115
+ p.status.message
2103
2116
  ).filter(
2104
- (M) => x.includes(
2105
- M.data.toolCallId
2117
+ (b) => I.includes(
2118
+ b.data.toolCallId
2106
2119
  )
2107
2120
  );
2108
- if (P.length > 0) {
2109
- const M = {
2121
+ if (S.length > 0) {
2122
+ const b = {
2110
2123
  role: "agent",
2111
2124
  kind: "message",
2112
- parts: P,
2113
- messageId: $()
2125
+ parts: S,
2126
+ messageId: N()
2114
2127
  };
2115
2128
  y = [
2116
2129
  ...y,
2117
- G(M)
2118
- ], r.conversationHistory = y, o && await s(
2130
+ Q(b)
2131
+ ], o.conversationHistory = y, r && await s(
2119
2132
  t,
2120
2133
  y
2121
2134
  );
2122
2135
  }
2123
2136
  }
2124
- if (v.final && ((N = v.status) == null ? void 0 : N.state) !== "input-required") {
2125
- x = [];
2126
- let h = null;
2127
- (U = v.status) != null && U.message && (h = G(
2128
- v.status.message
2137
+ if (p.final && ((O = p.status) == null ? void 0 : O.state) !== "input-required") {
2138
+ I = [];
2139
+ let C = null;
2140
+ (q = p.status) != null && q.message && (C = Q(
2141
+ p.status.message
2129
2142
  ), y = [
2130
2143
  ...y,
2131
- h
2132
- ], r.conversationHistory = y, o && await s(
2144
+ C
2145
+ ], o.conversationHistory = y, r && await s(
2133
2146
  t,
2134
2147
  y
2135
2148
  ));
2136
2149
  }
2137
- yield v;
2150
+ yield p;
2138
2151
  }
2139
- r.currentAbortController = null;
2152
+ o.currentAbortController = null;
2153
+ },
2154
+ /**
2155
+ * Send a tool result for a previously executed tool call.
2156
+ * Removes any existing tool result for the same `toolCallId` from
2157
+ * conversation history before sending, so the server sees exactly
2158
+ * one result per call.
2159
+ *
2160
+ * @param key - The agent key
2161
+ * @param toolCallId - The tool call ID to respond to
2162
+ * @param toolId - The tool ID
2163
+ * @param result - The tool result payload
2164
+ * @param options - Optional send message params
2165
+ */
2166
+ async *sendToolResult(t, a, n, o, r = {}) {
2167
+ const l = e.get(t);
2168
+ if (!l)
2169
+ throw new Error(`Agent with key "${t}" not found`);
2170
+ const i = (d) => {
2171
+ var u;
2172
+ return d.type === "data" && ((u = d.data) == null ? void 0 : u.toolCallId) === a && "result" in d.data;
2173
+ };
2174
+ l.conversationHistory = l.conversationHistory.map((d) => ({
2175
+ ...d,
2176
+ parts: d.parts.filter(
2177
+ (u) => !i(u)
2178
+ )
2179
+ })).filter((d) => d.parts.length > 0), yield* this.sendMessageStream(t, "", {
2180
+ ...r,
2181
+ message: {
2182
+ role: "user",
2183
+ kind: "message",
2184
+ parts: [
2185
+ J(a, n, o)
2186
+ ],
2187
+ messageId: N()
2188
+ }
2189
+ });
2140
2190
  },
2141
2191
  async resetConversation(t) {
2142
2192
  const a = e.get(t);
2143
2193
  if (!a)
2144
2194
  throw new Error(`Agent with key "${t}" not found`);
2145
- a.conversationHistory = [], a.sessionId && await It(
2195
+ a.conversationHistory = [], a.sessionId && await wt(
2146
2196
  a.sessionId,
2147
2197
  a.conversationStorageKey
2148
2198
  );
@@ -2163,7 +2213,7 @@ function At() {
2163
2213
  const n = e.get(t);
2164
2214
  if (!n)
2165
2215
  throw new Error(`Agent with key "${t}" not found`);
2166
- n.sessionId = a, n.sessionIdStorageKey && oe(
2216
+ n.sessionId = a, n.sessionIdStorageKey && re(
2167
2217
  n.sessionIdStorageKey,
2168
2218
  a
2169
2219
  );
@@ -2179,28 +2229,28 @@ function At() {
2179
2229
  }
2180
2230
  };
2181
2231
  }
2182
- const vt = At();
2183
- function X() {
2184
- return vt;
2232
+ const Mt = At();
2233
+ function Y() {
2234
+ return Mt;
2185
2235
  }
2186
- function Mt() {
2187
- const [e, s] = ve([]), t = F(
2188
- (r) => {
2189
- s((o) => {
2190
- const c = o.findIndex(
2191
- (l) => l.id === r.id
2236
+ function vt() {
2237
+ const [e, s] = ve([]), t = j(
2238
+ (o) => {
2239
+ s((r) => {
2240
+ const l = r.findIndex(
2241
+ (i) => i.id === o.id
2192
2242
  );
2193
- if (c >= 0) {
2194
- const l = [...o];
2195
- return l[c] = r, l;
2243
+ if (l >= 0) {
2244
+ const i = [...r];
2245
+ return i[l] = o, i;
2196
2246
  }
2197
- return [...o, r];
2247
+ return [...r, o];
2198
2248
  });
2199
2249
  },
2200
2250
  []
2201
- ), a = F((r) => {
2202
- s((o) => o.filter((c) => c.id !== r));
2203
- }, []), n = F(() => {
2251
+ ), a = j((o) => {
2252
+ s((r) => r.filter((l) => l.id !== o));
2253
+ }, []), n = j(() => {
2204
2254
  s([]);
2205
2255
  }, []);
2206
2256
  return {
@@ -2211,7 +2261,13 @@ function Mt() {
2211
2261
  };
2212
2262
  }
2213
2263
  function xt(e, s) {
2214
- return s.flatMap((n) => typeof n.actions == "function" ? n.actions(e) : n.actions).filter((n) => !(n.condition && !n.condition(e))).map((n) => ({
2264
+ return s.flatMap((n) => typeof n.actions == "function" ? n.actions(e) : n.actions).filter((n) => !(n.condition && !n.condition(e))).map((n) => n.type === "component" ? {
2265
+ type: "component",
2266
+ id: n.id,
2267
+ label: n.label,
2268
+ component: n.component,
2269
+ componentProps: n.componentProps
2270
+ } : {
2215
2271
  id: n.id,
2216
2272
  label: n.label,
2217
2273
  icon: n.icon,
@@ -2220,11 +2276,11 @@ function xt(e, s) {
2220
2276
  disabled: n.disabled || !1,
2221
2277
  pressed: n.pressed,
2222
2278
  showLabel: n.showLabel
2223
- }));
2279
+ });
2224
2280
  }
2225
- const re = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), $e = (e, s = "40%") => ({
2281
+ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), qe = (e, s = "40%") => ({
2226
2282
  type: "component",
2227
- component: () => He.createElement("img", {
2283
+ component: () => je.createElement("img", {
2228
2284
  src: e,
2229
2285
  alt: "Uploaded image",
2230
2286
  style: {
@@ -2237,29 +2293,29 @@ const re = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), $e = (e, s =
2237
2293
  }
2238
2294
  })
2239
2295
  }), ie = (e, s = []) => {
2240
- var o, c;
2241
- if (e.parts.some((l) => {
2242
- if (l.type === "data") {
2243
- const d = l.data;
2296
+ var r, l;
2297
+ if (e.parts.some((i) => {
2298
+ if (i.type === "data") {
2299
+ const d = i.data;
2244
2300
  return d.toolCallId || d.toolId || d.result;
2245
2301
  }
2246
2302
  return !1;
2247
2303
  }))
2248
2304
  return null;
2249
- const a = e.parts.map((l) => {
2305
+ const a = e.parts.map((i) => {
2250
2306
  var d;
2251
- if (l.type === "text")
2307
+ if (i.type === "text")
2252
2308
  return {
2253
- type: ((d = l.metadata) == null ? void 0 : d.contentType) || "text",
2254
- text: l.text
2309
+ type: ((d = i.metadata) == null ? void 0 : d.contentType) || "text",
2310
+ text: i.text
2255
2311
  };
2256
- if (l.type === "file") {
2257
- const u = l.file.uri || (l.file.mimeType && l.file.bytes ? `data:${l.file.mimeType};base64,${l.file.bytes}` : void 0);
2312
+ if (i.type === "file") {
2313
+ const u = i.file.uri || (i.file.mimeType && i.file.bytes ? `data:${i.file.mimeType};base64,${i.file.bytes}` : void 0);
2258
2314
  if (u)
2259
- return $e(u);
2315
+ return qe(u);
2260
2316
  }
2261
- if (l.type === "data") {
2262
- const u = l.data;
2317
+ if (i.type === "data") {
2318
+ const u = i.data;
2263
2319
  return u.component && u.componentProps ? {
2264
2320
  type: "component",
2265
2321
  component: u.component,
@@ -2279,23 +2335,23 @@ const re = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), $e = (e, s =
2279
2335
  type: "text",
2280
2336
  text: "[Unsupported content]"
2281
2337
  };
2282
- }), n = ((o = e.metadata) == null ? void 0 : o.timestamp) ?? Date.now(), r = {
2338
+ }), n = ((r = e.metadata) == null ? void 0 : r.timestamp) ?? Date.now(), o = {
2283
2339
  id: e.messageId,
2284
2340
  role: e.role === "agent" ? "agent" : "user",
2285
2341
  content: a,
2286
2342
  timestamp: n,
2287
- archived: !!((c = e.metadata) != null && c.archived),
2343
+ archived: !!((l = e.metadata) != null && l.archived),
2288
2344
  showIcon: e.role === "agent",
2289
2345
  icon: e.role === "agent" ? "assistant" : void 0
2290
2346
  };
2291
2347
  if (e.role === "agent" && s.length > 0) {
2292
- const l = xt(
2293
- r,
2348
+ const i = xt(
2349
+ o,
2294
2350
  s
2295
2351
  );
2296
- l.length > 0 && (r.actions = l);
2352
+ i.length > 0 && (o.actions = i);
2297
2353
  }
2298
- return r;
2354
+ return o;
2299
2355
  }, Ct = () => ({
2300
2356
  getClientContext: () => ({})
2301
2357
  }), bt = () => ({
@@ -2324,18 +2380,18 @@ function Wt(e) {
2324
2380
  progressMessage: null,
2325
2381
  progressPhase: null
2326
2382
  }), {
2327
- registerMessageActions: r,
2328
- unregisterMessageActions: o,
2329
- clearAllMessageActions: c,
2330
- registrations: l
2331
- } = Mt(), d = Fe(l);
2383
+ registerMessageActions: o,
2384
+ unregisterMessageActions: r,
2385
+ clearAllMessageActions: l,
2386
+ registrations: i
2387
+ } = vt(), d = he(!1), u = he(i);
2332
2388
  ae(() => {
2333
- d.current = l;
2334
- }, [l]);
2335
- const u = F(
2336
- (f) => f.map(
2337
- (i) => ie(i, d.current)
2338
- ).filter((i) => i !== null),
2389
+ u.current = i;
2390
+ }, [i]);
2391
+ const m = j(
2392
+ (g) => g.map(
2393
+ (c) => ie(c, u.current)
2394
+ ).filter((c) => c !== null),
2339
2395
  []
2340
2396
  // registrationsRef is stable, so no deps needed
2341
2397
  );
@@ -2343,18 +2399,18 @@ function Wt(e) {
2343
2399
  if (!t)
2344
2400
  return;
2345
2401
  (async () => {
2346
- const i = X(), w = s.agentId;
2347
- if (i.hasAgent(w))
2348
- s.sessionId ? (i.updateSessionId(w, s.sessionId), i.getConversationHistory(w).length === 0 && n((C) => ({
2349
- ...C,
2402
+ const c = Y(), w = s.agentId;
2403
+ if (c.hasAgent(w))
2404
+ s.sessionId ? (c.updateSessionId(w, s.sessionId), c.getConversationHistory(w).length === 0 && n((_) => ({
2405
+ ..._,
2350
2406
  clientMessages: [],
2351
2407
  uiMessages: []
2352
- }))) : (i.updateSessionId(w, ""), await i.replaceMessages(w, []), n((S) => ({
2353
- ...S,
2408
+ }))) : (c.updateSessionId(w, ""), await c.replaceMessages(w, []), n((T) => ({
2409
+ ...T,
2354
2410
  clientMessages: [],
2355
2411
  uiMessages: []
2356
2412
  })));
2357
- else if (await i.createAgent(w, {
2413
+ else if (await c.createAgent(w, {
2358
2414
  agentId: s.agentId,
2359
2415
  agentUrl: s.agentUrl,
2360
2416
  sessionId: s.sessionId,
@@ -2367,18 +2423,18 @@ function Wt(e) {
2367
2423
  odieBotId: e.odieBotId,
2368
2424
  fetchCallback: s.fetchCallback
2369
2425
  }), s.sessionId) {
2370
- const S = i.getConversationHistory(w);
2371
- n((C) => {
2372
- const N = u(S);
2426
+ const T = c.getConversationHistory(w);
2427
+ n((_) => {
2428
+ const O = m(T);
2373
2429
  return {
2374
- ...C,
2375
- clientMessages: S,
2376
- uiMessages: N
2430
+ ..._,
2431
+ clientMessages: T,
2432
+ uiMessages: O
2377
2433
  };
2378
2434
  });
2379
2435
  } else
2380
- n((S) => ({
2381
- ...S,
2436
+ n((T) => ({
2437
+ ...T,
2382
2438
  clientMessages: [],
2383
2439
  uiMessages: []
2384
2440
  }));
@@ -2395,147 +2451,171 @@ function Wt(e) {
2395
2451
  e.odieBotId,
2396
2452
  t
2397
2453
  ]);
2398
- const m = F(
2399
- async (f, i) => {
2400
- var U, v;
2454
+ const h = j(
2455
+ async (g, c) => {
2456
+ var O, q;
2401
2457
  if (!t)
2402
2458
  throw new Error("Invalid agent configuration");
2403
- const w = X(), T = s.agentId, S = Date.now(), C = (i == null ? void 0 : i.type) || "text", N = {
2404
- id: `user-${S}`,
2405
- role: "user",
2406
- content: [
2407
- { type: C, text: f },
2408
- // Map image URLs to component content parts
2409
- ...((U = i == null ? void 0 : i.imageUrls) == null ? void 0 : U.map((h) => {
2410
- const P = typeof h == "string" ? h : h.url;
2411
- return $e(P);
2412
- })) ?? []
2413
- ],
2414
- timestamp: S,
2415
- archived: (i == null ? void 0 : i.archived) ?? !1,
2416
- showIcon: !1
2417
- };
2418
- n((h) => ({
2419
- ...h,
2420
- uiMessages: [...h.uiMessages, N],
2421
- isProcessing: !0,
2422
- error: null
2423
- }));
2459
+ if (d.current)
2460
+ return;
2461
+ d.current = !0;
2462
+ const w = (c == null ? void 0 : c.type) === "tool_result";
2463
+ if (w && (!(c != null && c.toolCallId) || !(c != null && c.toolId)))
2464
+ throw new Error(
2465
+ "`toolCallId` and `toolId` are required when type is `tool_result`"
2466
+ );
2467
+ const k = Y(), T = s.agentId, _ = Date.now();
2468
+ if (w)
2469
+ n((p) => ({
2470
+ ...p,
2471
+ isProcessing: !0,
2472
+ error: null
2473
+ }));
2474
+ else {
2475
+ const p = (c == null ? void 0 : c.type) || "text", C = {
2476
+ id: `user-${_}`,
2477
+ role: "user",
2478
+ content: [
2479
+ { type: p, text: g },
2480
+ // Map image URLs to component content parts
2481
+ ...((O = c == null ? void 0 : c.imageUrls) == null ? void 0 : O.map((S) => {
2482
+ const b = typeof S == "string" ? S : S.url;
2483
+ return qe(b);
2484
+ })) ?? []
2485
+ ],
2486
+ timestamp: _,
2487
+ archived: (c == null ? void 0 : c.archived) ?? !1,
2488
+ showIcon: !1
2489
+ };
2490
+ n((S) => ({
2491
+ ...S,
2492
+ uiMessages: [...S.uiMessages, C],
2493
+ isProcessing: !0,
2494
+ error: null
2495
+ }));
2496
+ }
2424
2497
  try {
2425
- let h = null, P = !1;
2426
- const M = {};
2427
- (i != null && i.archived || i != null && i.type) && (M.metadata = {
2428
- ...(i == null ? void 0 : i.archived) && { archived: !0 },
2429
- ...(i == null ? void 0 : i.type) && { contentType: i.type }
2430
- }), i != null && i.sessionId && (M.sessionId = i.sessionId), i != null && i.imageUrls && (M.imageUrls = i.imageUrls);
2431
- for await (const b of w.sendMessageStream(
2498
+ let p = null, C = !1;
2499
+ const S = {}, b = !!(c != null && c.type) && !w;
2500
+ (c != null && c.archived || b) && (S.metadata = {
2501
+ ...(c == null ? void 0 : c.archived) && { archived: !0 },
2502
+ ...b && { contentType: c.type }
2503
+ }), c != null && c.sessionId && (S.sessionId = c.sessionId), c != null && c.imageUrls && (S.imageUrls = c.imageUrls);
2504
+ const $ = w ? k.sendToolResult(
2505
+ T,
2506
+ c.toolCallId,
2507
+ c.toolId,
2508
+ { success: !0, message: g },
2509
+ S
2510
+ ) : k.sendMessageStream(
2432
2511
  T,
2433
- f,
2434
- M
2435
- )) {
2436
- if ((b.progressMessage || b.progressPhase) && n((R) => ({
2437
- ...R,
2438
- progressMessage: b.progressMessage || null,
2439
- progressPhase: b.progressPhase || null
2440
- })), !b.final && b.text)
2441
- if (h)
2442
- n((R) => ({
2443
- ...R,
2444
- uiMessages: R.uiMessages.map(
2445
- (k) => k.id === h ? {
2446
- ...k,
2512
+ g,
2513
+ S
2514
+ );
2515
+ for await (const R of $) {
2516
+ if ((R.progressMessage || R.progressPhase) && n((P) => ({
2517
+ ...P,
2518
+ progressMessage: R.progressMessage || null,
2519
+ progressPhase: R.progressPhase || null
2520
+ })), !R.final && R.text)
2521
+ if (p)
2522
+ n((P) => ({
2523
+ ...P,
2524
+ uiMessages: P.uiMessages.map(
2525
+ (x) => x.id === p ? {
2526
+ ...x,
2447
2527
  content: [
2448
2528
  {
2449
2529
  type: "text",
2450
- text: b.text
2530
+ text: R.text
2451
2531
  }
2452
2532
  ]
2453
- } : k
2533
+ } : x
2454
2534
  )
2455
2535
  }));
2456
2536
  else {
2457
- h = `agent-streaming-${Date.now()}`;
2458
- const R = {
2459
- id: h,
2537
+ p = `agent-streaming-${Date.now()}`;
2538
+ const P = {
2539
+ id: p,
2460
2540
  role: "agent",
2461
2541
  content: [
2462
- { type: "text", text: b.text }
2542
+ { type: "text", text: R.text }
2463
2543
  ],
2464
2544
  timestamp: Date.now(),
2465
2545
  archived: !1,
2466
2546
  showIcon: !0,
2467
2547
  icon: "assistant",
2468
- reactKey: h
2548
+ reactKey: p
2469
2549
  // Stable key for React rendering
2470
2550
  };
2471
- n((k) => ({
2472
- ...k,
2551
+ n((x) => ({
2552
+ ...x,
2473
2553
  uiMessages: [
2474
- ...k.uiMessages,
2475
- R
2554
+ ...x.uiMessages,
2555
+ P
2476
2556
  ]
2477
2557
  }));
2478
2558
  }
2479
- if (b.final && ((v = b.status) != null && v.message) && h) {
2480
- P = !0;
2481
- const R = h, k = ie(
2482
- b.status.message,
2483
- d.current
2559
+ if (R.final && ((q = R.status) != null && q.message) && p) {
2560
+ C = !0;
2561
+ const P = p, x = ie(
2562
+ R.status.message,
2563
+ u.current
2484
2564
  );
2485
- k && n((E) => {
2486
- const _ = E.uiMessages.map(
2565
+ x && n((U) => {
2566
+ const K = U.uiMessages.map(
2487
2567
  (H) => {
2488
- var O, W;
2489
- if (H.id === R) {
2490
- const V = k.content.length > 0 && ((O = k.content[0]) == null ? void 0 : O.text) && ((W = H.content[0]) == null ? void 0 : W.text) && k.content[0].text.length > H.content[0].text.length;
2568
+ var E, F;
2569
+ if (H.id === P) {
2570
+ const G = x.content.length > 0 && ((E = x.content[0]) == null ? void 0 : E.text) && ((F = H.content[0]) == null ? void 0 : F.text) && x.content[0].text.length > H.content[0].text.length;
2491
2571
  return {
2492
- ...k,
2493
- reactKey: H.reactKey || R,
2572
+ ...x,
2573
+ reactKey: H.reactKey || P,
2494
2574
  // Keep stable reactKey
2495
- content: V ? k.content : H.content
2575
+ content: G ? x.content : H.content
2496
2576
  };
2497
2577
  }
2498
2578
  return H;
2499
2579
  }
2500
- ), q = w.getConversationHistory(
2580
+ ), L = k.getConversationHistory(
2501
2581
  T
2502
2582
  );
2503
2583
  return {
2504
- ...E,
2505
- clientMessages: q,
2506
- uiMessages: _,
2584
+ ...U,
2585
+ clientMessages: L,
2586
+ uiMessages: K,
2507
2587
  isProcessing: !1,
2508
2588
  progressMessage: null,
2509
2589
  progressPhase: null
2510
2590
  };
2511
- }), h = null;
2591
+ }), p = null;
2512
2592
  }
2513
2593
  }
2514
- if (!P) {
2515
- const b = w.getConversationHistory(T);
2516
- n((R) => {
2517
- let k = R.uiMessages;
2518
- h && (k = R.uiMessages.filter(
2519
- (O) => O.id !== h
2594
+ if (!C) {
2595
+ const R = k.getConversationHistory(T);
2596
+ n((P) => {
2597
+ let x = P.uiMessages;
2598
+ p && (x = P.uiMessages.filter(
2599
+ (E) => E.id !== p
2520
2600
  ));
2521
- const E = b.map(
2522
- (O) => ie(
2523
- O,
2524
- d.current
2601
+ const U = R.map(
2602
+ (E) => ie(
2603
+ E,
2604
+ u.current
2525
2605
  )
2526
2606
  ).filter(
2527
- (O) => O !== null
2528
- ), _ = new Set(
2529
- b.map((O) => O.messageId)
2530
- ), q = k.filter(
2531
- (O) => !_.has(O.id) && O.role !== "user"
2532
- ), H = re([
2533
- ...E,
2534
- ...q
2607
+ (E) => E !== null
2608
+ ), K = new Set(
2609
+ R.map((E) => E.messageId)
2610
+ ), L = x.filter(
2611
+ (E) => !K.has(E.id) && E.role !== "user"
2612
+ ), H = oe([
2613
+ ...U,
2614
+ ...L
2535
2615
  ]);
2536
2616
  return {
2537
- ...R,
2538
- clientMessages: b,
2617
+ ...P,
2618
+ clientMessages: R,
2539
2619
  uiMessages: H,
2540
2620
  isProcessing: !1,
2541
2621
  progressMessage: null,
@@ -2543,10 +2623,10 @@ function Wt(e) {
2543
2623
  };
2544
2624
  });
2545
2625
  }
2546
- } catch (h) {
2547
- if (h instanceof Error && h.name === "AbortError") {
2548
- A("Request was aborted by user"), n((M) => ({
2549
- ...M,
2626
+ } catch (p) {
2627
+ if (p instanceof Error && p.name === "AbortError") {
2628
+ M("Request was aborted by user"), n((S) => ({
2629
+ ...S,
2550
2630
  isProcessing: !1,
2551
2631
  progressMessage: null,
2552
2632
  progressPhase: null,
@@ -2555,67 +2635,69 @@ function Wt(e) {
2555
2635
  }));
2556
2636
  return;
2557
2637
  }
2558
- const P = h instanceof Error ? h.message : "Failed to send message";
2559
- throw n((M) => ({
2560
- ...M,
2638
+ const C = p instanceof Error ? p.message : "Failed to send message";
2639
+ throw n((S) => ({
2640
+ ...S,
2561
2641
  isProcessing: !1,
2562
2642
  progressMessage: null,
2563
2643
  progressPhase: null,
2564
- error: P
2565
- })), h;
2644
+ error: C
2645
+ })), p;
2646
+ } finally {
2647
+ d.current = !1;
2566
2648
  }
2567
2649
  },
2568
2650
  [s.agentId, t]
2569
- ), p = F((f) => {
2570
- n((i) => ({
2571
- ...i,
2572
- uiMessages: re([...i.uiMessages, f])
2651
+ ), f = j((g) => {
2652
+ n((c) => ({
2653
+ ...c,
2654
+ uiMessages: oe([...c.uiMessages, g])
2573
2655
  }));
2574
- }, []), g = F((f) => {
2575
- n((i) => ({
2576
- ...i,
2577
- suggestions: f
2656
+ }, []), v = j((g) => {
2657
+ n((c) => ({
2658
+ ...c,
2659
+ suggestions: g
2578
2660
  }));
2579
- }, []), y = F(() => {
2580
- n((f) => ({
2581
- ...f,
2661
+ }, []), y = j(() => {
2662
+ n((g) => ({
2663
+ ...g,
2582
2664
  suggestions: []
2583
2665
  }));
2584
2666
  }, []);
2585
2667
  ae(() => {
2586
- n((f) => {
2587
- if (f.clientMessages.length === 0)
2588
- return f;
2589
- const i = u(f.clientMessages), w = new Set(
2590
- f.clientMessages.map((S) => S.messageId)
2591
- ), T = f.uiMessages.filter(
2592
- (S) => !w.has(S.id) && S.role !== "user"
2668
+ n((g) => {
2669
+ if (g.clientMessages.length === 0)
2670
+ return g;
2671
+ const c = m(g.clientMessages), w = new Set(
2672
+ g.clientMessages.map((T) => T.messageId)
2673
+ ), k = g.uiMessages.filter(
2674
+ (T) => !w.has(T.id) && T.role !== "user"
2593
2675
  );
2594
2676
  return {
2595
- ...f,
2596
- uiMessages: re([
2597
- ...i,
2598
- ...T
2677
+ ...g,
2678
+ uiMessages: oe([
2679
+ ...c,
2680
+ ...k
2599
2681
  ])
2600
2682
  };
2601
2683
  });
2602
- }, [l]);
2603
- const x = F(() => {
2684
+ }, [i]);
2685
+ const I = j(() => {
2604
2686
  if (!t)
2605
2687
  return;
2606
- const f = X(), i = s.agentId;
2607
- f.abortCurrentRequest(i);
2608
- }, [s.agentId, t]), I = F(
2609
- async (f) => {
2688
+ const g = Y(), c = s.agentId;
2689
+ g.abortCurrentRequest(c);
2690
+ }, [s.agentId, t]), A = j(
2691
+ async (g) => {
2610
2692
  if (!t)
2611
2693
  return;
2612
- const i = X(), w = s.agentId;
2613
- await i.replaceMessages(w, f);
2614
- const T = u(f);
2615
- n((S) => ({
2616
- ...S,
2617
- clientMessages: f,
2618
- uiMessages: T
2694
+ const c = Y(), w = s.agentId;
2695
+ await c.replaceMessages(w, g);
2696
+ const k = m(g);
2697
+ n((T) => ({
2698
+ ...T,
2699
+ clientMessages: g,
2700
+ uiMessages: k
2619
2701
  }));
2620
2702
  },
2621
2703
  [s.agentId, t]
@@ -2625,53 +2707,53 @@ function Wt(e) {
2625
2707
  messages: a.uiMessages,
2626
2708
  isProcessing: a.isProcessing,
2627
2709
  error: a.error,
2628
- onSubmit: m,
2710
+ onSubmit: h,
2629
2711
  suggestions: a.suggestions,
2630
2712
  progressMessage: a.progressMessage,
2631
2713
  progressPhase: a.progressPhase,
2632
2714
  // UI management methods
2633
- registerSuggestions: g,
2715
+ registerSuggestions: v,
2634
2716
  clearSuggestions: y,
2635
2717
  // Message actions methods
2636
- registerMessageActions: r,
2637
- unregisterMessageActions: o,
2638
- clearAllMessageActions: c,
2718
+ registerMessageActions: o,
2719
+ unregisterMessageActions: r,
2720
+ clearAllMessageActions: l,
2639
2721
  // Tool integration
2640
- addMessage: p,
2722
+ addMessage: f,
2641
2723
  // Abort control
2642
- abortCurrentRequest: x,
2724
+ abortCurrentRequest: I,
2643
2725
  // Conversation loading
2644
- loadMessages: I
2726
+ loadMessages: A
2645
2727
  };
2646
2728
  }
2647
- var Pt = /* @__PURE__ */ ((e) => (e[e.PARSE_ERROR = -32700] = "PARSE_ERROR", e[e.INVALID_REQUEST = -32600] = "INVALID_REQUEST", e[e.METHOD_NOT_FOUND = -32601] = "METHOD_NOT_FOUND", e[e.INVALID_PARAMS = -32602] = "INVALID_PARAMS", e[e.INTERNAL_ERROR = -32603] = "INTERNAL_ERROR", e[e.SERVER_ERROR = -32e3] = "SERVER_ERROR", e))(Pt || {});
2648
- const Se = "jetpack-ai-jwt-token", Rt = 30 * 60 * 1e3;
2729
+ var Rt = /* @__PURE__ */ ((e) => (e[e.PARSE_ERROR = -32700] = "PARSE_ERROR", e[e.INVALID_REQUEST = -32600] = "INVALID_REQUEST", e[e.METHOD_NOT_FOUND = -32601] = "METHOD_NOT_FOUND", e[e.INVALID_PARAMS = -32602] = "INVALID_PARAMS", e[e.INTERNAL_ERROR = -32603] = "INTERNAL_ERROR", e[e.SERVER_ERROR = -32e3] = "SERVER_ERROR", e))(Rt || {});
2730
+ const Ae = "jetpack-ai-jwt-token", Pt = 30 * 60 * 1e3;
2649
2731
  function _t() {
2650
2732
  var t, a, n;
2651
2733
  return ((a = (t = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : t.connectionStatus) == null ? void 0 : a.isRegistered) ? !1 : !!((n = window.Jetpack_Editor_Initial_State) != null && n.wpcomBlogId);
2652
2734
  }
2653
2735
  async function Et(e, s = !0) {
2654
- var l, d;
2655
- const t = localStorage.getItem(Se);
2736
+ var i, d;
2737
+ const t = localStorage.getItem(Ae);
2656
2738
  let a;
2657
2739
  if (t)
2658
2740
  try {
2659
2741
  a = JSON.parse(t);
2660
2742
  } catch (u) {
2661
- A("Invalid cached Jetpack token: %O", u);
2743
+ M("Invalid cached Jetpack token: %O", u);
2662
2744
  }
2663
2745
  if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() && s)
2664
2746
  return a;
2665
- const n = (l = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : l.apiNonce, r = (d = window.Jetpack_Editor_Initial_State) == null ? void 0 : d.wpcomBlogId;
2666
- let o = {
2747
+ const n = (i = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : i.apiNonce, o = (d = window.Jetpack_Editor_Initial_State) == null ? void 0 : d.wpcomBlogId;
2748
+ let r = {
2667
2749
  token: "",
2668
2750
  blog_id: ""
2669
2751
  };
2670
2752
  try {
2671
- _t() ? o = await he({
2672
- path: "/wpcom/v2/sites/" + r + "/jetpack-openai-query/jwt",
2753
+ _t() ? r = await ye({
2754
+ path: "/wpcom/v2/sites/" + o + "/jetpack-openai-query/jwt",
2673
2755
  method: "POST"
2674
- }) : o = await he({
2756
+ }) : r = await ye({
2675
2757
  path: "/jetpack/v4/jetpack-ai-jwt?_cacheBuster=" + Date.now(),
2676
2758
  credentials: "same-origin",
2677
2759
  headers: {
@@ -2680,23 +2762,23 @@ async function Et(e, s = !0) {
2680
2762
  method: "POST"
2681
2763
  });
2682
2764
  } catch (u) {
2683
- throw A("Failed to fetch Jetpack token: %O", u), new Error(e(u));
2765
+ throw M("Failed to fetch Jetpack token: %O", u), new Error(e(u));
2684
2766
  }
2685
- if (!(o != null && o.token))
2767
+ if (!(r != null && r.token))
2686
2768
  throw new Error(
2687
2769
  "Authentication failed. Please ensure Jetpack is properly connected and try again."
2688
2770
  );
2689
- const c = {
2690
- token: o.token,
2691
- blogId: o.blog_id || "",
2692
- expire: Date.now() + Rt
2771
+ const l = {
2772
+ token: r.token,
2773
+ blogId: r.blog_id || "",
2774
+ expire: Date.now() + Pt
2693
2775
  };
2694
2776
  try {
2695
- localStorage.setItem(Se, JSON.stringify(c));
2777
+ localStorage.setItem(Ae, JSON.stringify(l));
2696
2778
  } catch (u) {
2697
- A("Error storing token in localStorage: %O", u);
2779
+ M("Error storing token in localStorage: %O", u);
2698
2780
  }
2699
- return c;
2781
+ return l;
2700
2782
  }
2701
2783
  const Vt = (e) => async () => {
2702
2784
  const s = {};
@@ -2704,7 +2786,7 @@ const Vt = (e) => async () => {
2704
2786
  const t = await Et(e);
2705
2787
  t != null && t.token && (s.Authorization = `${t.token}`);
2706
2788
  } catch (t) {
2707
- throw A("Failed to get Jetpack token for auth: %O", t), t;
2789
+ throw M("Failed to get Jetpack token for auth: %O", t), t;
2708
2790
  }
2709
2791
  return s;
2710
2792
  };
@@ -2744,12 +2826,12 @@ function Nt(e) {
2744
2826
  throw new Error(
2745
2827
  `Invalid Odie bot type: ${a}. Expected one of: agent, workflow, chain`
2746
2828
  );
2747
- const n = a, r = s.slice(2).join("-"), o = r.replace(/_/g, "-");
2829
+ const n = a, o = s.slice(2).join("-"), r = o.replace(/_/g, "-");
2748
2830
  return {
2749
2831
  product: t,
2750
2832
  type: n,
2751
- slug: r,
2752
- agentId: o
2833
+ slug: o,
2834
+ agentId: r
2753
2835
  };
2754
2836
  }
2755
2837
  function Xt(e) {
@@ -2761,19 +2843,19 @@ function Xt(e) {
2761
2843
  }
2762
2844
  }
2763
2845
  export {
2764
- Pt as ErrorCodes,
2846
+ Rt as ErrorCodes,
2765
2847
  zt as convertAbilitiesToTools,
2766
2848
  Ot as convertAbilityToTool,
2767
2849
  Kt as createAbortController,
2768
- it as createClient,
2850
+ lt as createClient,
2769
2851
  Vt as createJetpackAuthProvider,
2770
2852
  Qt as createOdieBotId,
2771
- je as createRequestId,
2772
- De as createTaskId,
2853
+ De as createRequestId,
2854
+ Je as createTaskId,
2773
2855
  le as createTextMessage,
2774
2856
  D as extractTextFromMessage,
2775
- K as extractToolCallsFromMessage,
2776
- X as getAgentManager,
2857
+ W as extractToolCallsFromMessage,
2858
+ Y as getAgentManager,
2777
2859
  Xt as isOdieBotId,
2778
2860
  Gt as isWordPressAbility,
2779
2861
  Lt as listConversationsFromServer,
@@ -2785,5 +2867,5 @@ export {
2785
2867
  Ft as useClientContext,
2786
2868
  jt as useClientTools,
2787
2869
  Jt as useClientToolsWithAbilities,
2788
- Mt as useMessageActions
2870
+ vt as useMessageActions
2789
2871
  };