@automattic/agenttic-client 0.1.57 → 0.1.60

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,48 +1,48 @@
1
1
  var He = Object.defineProperty;
2
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";
3
+ var te = (e, s, t) => Fe(e, typeof s != "symbol" ? s + "" : s, t);
4
+ import je, { useMemo as Se, useCallback as H, useState as ve, useRef as pe, useEffect as se } from "react";
5
+ import { l as S, f as Ae, a as L } from "./logger-aKHbTlwk.js";
6
+ import he from "@wordpress/api-fetch";
7
7
  function Ft(e) {
8
- return Me(() => e ? {
8
+ return Se(() => e ? {
9
9
  getClientContext: () => {
10
10
  try {
11
11
  return e() || {};
12
12
  } catch (a) {
13
- return M("Error getting client context: %O", a), {};
13
+ return S("Error getting client context: %O", a), {};
14
14
  }
15
15
  }
16
16
  } : void 0, [e]);
17
17
  }
18
- function ue({
18
+ function ce({
19
19
  getClientTools: e,
20
20
  executeTool: s,
21
21
  getAbilities: t
22
22
  }) {
23
- const a = j(async () => {
23
+ const a = H(async () => {
24
24
  const r = [];
25
25
  if (e)
26
26
  try {
27
27
  const l = await e();
28
28
  r.push(...l);
29
29
  } catch (l) {
30
- M("Error getting available tools: %O", l);
30
+ S("Error getting available tools: %O", l);
31
31
  }
32
32
  return r;
33
- }, [e]), n = j(
33
+ }, [e]), n = H(
34
34
  async (r, l) => {
35
35
  if (!s)
36
36
  throw new Error("No executeTool callback provided");
37
37
  try {
38
38
  return await s(r, l);
39
39
  } catch (i) {
40
- throw M("Error executing tool %s: %O", r, i), i;
40
+ throw S("Error executing tool %s: %O", r, i), i;
41
41
  }
42
42
  },
43
43
  [s]
44
44
  );
45
- return Me(() => {
45
+ return Se(() => {
46
46
  if (!e && !t)
47
47
  return;
48
48
  const r = {};
@@ -56,14 +56,14 @@ function ue({
56
56
  ]);
57
57
  }
58
58
  function jt(e, s) {
59
- return ue({
59
+ return ce({
60
60
  getClientTools: e,
61
61
  executeTool: s,
62
62
  getAbilities: void 0
63
63
  });
64
64
  }
65
65
  function Dt(e) {
66
- return ue({
66
+ return ce({
67
67
  getClientTools: void 0,
68
68
  executeTool: void 0,
69
69
  getAbilities: e
@@ -79,13 +79,13 @@ function Jt(e) {
79
79
  throw new Error(
80
80
  "executeTool is required when providing getClientTools."
81
81
  );
82
- return ue({
82
+ return ce({
83
83
  getClientTools: s,
84
84
  executeTool: t,
85
85
  getAbilities: a
86
86
  });
87
87
  }
88
- function de() {
88
+ function ue() {
89
89
  const e = "abcdefghijklmnopqrstuvwxyz0123456789";
90
90
  let s = "";
91
91
  for (let t = 0; t < 8; t++)
@@ -93,15 +93,15 @@ function de() {
93
93
  return s;
94
94
  }
95
95
  function N() {
96
- return de();
96
+ return ue();
97
97
  }
98
98
  function De() {
99
- return `req-${de()}`;
99
+ return `req-${ue()}`;
100
100
  }
101
101
  function Je() {
102
- return `task-${de()}`;
102
+ return `task-${ue()}`;
103
103
  }
104
- function Ce(e, s) {
104
+ function xe(e, s) {
105
105
  return {
106
106
  type: "text",
107
107
  text: e,
@@ -120,10 +120,10 @@ function Ke(e, s = "message/send", t = !1) {
120
120
  };
121
121
  return t && s === "message/stream" && (a.tokenStreaming = !0), a;
122
122
  }
123
- function D(e) {
123
+ function F(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 Ie(e) {
126
+ function ye(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);
@@ -160,12 +160,12 @@ function Be(e) {
160
160
  metadata: {}
161
161
  };
162
162
  }
163
- function W(e) {
163
+ function B(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 J(e, s, t, a) {
168
+ function D(e, s, t, a) {
169
169
  return {
170
170
  type: "data",
171
171
  data: {
@@ -185,11 +185,11 @@ function We(e) {
185
185
  metadata: {}
186
186
  };
187
187
  }
188
- function le(e, s) {
188
+ function ie(e, s) {
189
189
  const { contentType: t, ...a } = s || {};
190
190
  return {
191
191
  role: "user",
192
- parts: [Ce(e, t ? { contentType: t } : void 0)],
192
+ parts: [xe(e, t ? { contentType: t } : void 0)],
193
193
  kind: "message",
194
194
  messageId: N(),
195
195
  metadata: {
@@ -198,10 +198,10 @@ function le(e, s) {
198
198
  }
199
199
  };
200
200
  }
201
- function X(e) {
201
+ function Q(e) {
202
202
  return {
203
203
  role: "agent",
204
- parts: [Ce(e)],
204
+ parts: [xe(e)],
205
205
  kind: "message",
206
206
  messageId: N(),
207
207
  metadata: {
@@ -209,7 +209,7 @@ function X(e) {
209
209
  }
210
210
  };
211
211
  }
212
- function ge(e) {
212
+ function de(e) {
213
213
  return e && typeof e == "object" && "result" in e ? {
214
214
  result: e.result,
215
215
  returnToAgent: e.returnToAgent !== !1,
@@ -221,7 +221,7 @@ function ge(e) {
221
221
  returnToAgent: !0
222
222
  };
223
223
  }
224
- function Z(e, s = []) {
224
+ function Y(e, s = []) {
225
225
  return {
226
226
  role: "user",
227
227
  kind: "message",
@@ -259,7 +259,7 @@ async function Ve(e, s) {
259
259
  parts: [...e.parts, ...t]
260
260
  };
261
261
  } catch (t) {
262
- return M("Warning: Failed to get tools: %s", t), e;
262
+ return S("Warning: Failed to get tools: %s", t), e;
263
263
  }
264
264
  }
265
265
  function ze(e, s) {
@@ -275,7 +275,7 @@ function ze(e, s) {
275
275
  parts: [...e.parts, a]
276
276
  };
277
277
  } catch (t) {
278
- return M("Warning: Failed to get context: %s", t), e;
278
+ return S("Warning: Failed to get context: %s", t), e;
279
279
  }
280
280
  }
281
281
  async function Ge(e, s, t) {
@@ -308,7 +308,7 @@ function Qe(e, s = "") {
308
308
  try {
309
309
  t.push(JSON.parse(n)), o = i === -1 ? a.length : i + 1;
310
310
  } catch (u) {
311
- M("Failed to parse SSE event: %o", u), M("Problematic payload: %s", n);
311
+ S("Failed to parse SSE event: %o", u), S("Problematic payload: %s", n);
312
312
  }
313
313
  n = "";
314
314
  }
@@ -318,20 +318,20 @@ function Qe(e, s = "") {
318
318
  return { events: t, nextBuffer: l };
319
319
  }
320
320
  async function* Xe(e, s = {}) {
321
- var d, u, m;
321
+ var d, u, h;
322
322
  const { supportDeltas: t = !1 } = s, a = e.getReader(), n = new TextDecoder();
323
323
  let o = "";
324
324
  const r = new Ye();
325
325
  let l = null, i = null;
326
326
  try {
327
327
  for (; ; ) {
328
- const { done: h, value: f } = await a.read();
329
- if (h)
328
+ const { done: y, value: x } = await a.read();
329
+ if (y)
330
330
  break;
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];
331
+ const m = n.decode(x, { stream: !0 }), { events: f, nextBuffer: v } = Qe(m, o);
332
+ if (f && Array.isArray(f))
333
+ for (let A = 0; A < f.length; A++) {
334
+ const g = f[A];
335
335
  if (A > 0 && g.method === "message/delta" && typeof requestAnimationFrame < "u" && await new Promise((c) => {
336
336
  requestAnimationFrame(() => c(void 0));
337
337
  }), g.error)
@@ -344,52 +344,52 @@ async function* Xe(e, s = {}) {
344
344
  if (c.deltaType === "content" && (r.processContentDelta(
345
345
  c.content
346
346
  ), !l && g.params.id && (l = g.params.id), l)) {
347
- const w = r.getCurrentMessage();
347
+ const I = r.getCurrentMessage();
348
348
  yield {
349
349
  id: l,
350
350
  status: {
351
351
  state: "working",
352
- message: w
352
+ message: I
353
353
  },
354
354
  final: !1,
355
355
  text: r.getTextContent()
356
356
  };
357
357
  }
358
- } catch (w) {
359
- M("Failed to process delta: %o", w);
358
+ } catch (I) {
359
+ S("Failed to process delta: %o", I);
360
360
  }
361
361
  } else if (g.result && g.result.status) {
362
362
  l = g.result.id, i = g.result.status, (r.getTextContent() || r.getCurrentMessage().parts.length > 0) && r.reset();
363
363
  const c = ((u = g.result.status) == null ? void 0 : u.message) || {
364
364
  role: "agent",
365
365
  parts: []
366
- }, w = Ie(c);
366
+ }, I = ye(c);
367
367
  yield {
368
368
  id: g.result.id,
369
369
  sessionId: g.result.sessionId,
370
370
  status: g.result.status,
371
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
372
+ text: F(c),
373
+ progressMessage: I == null ? void 0 : I.summary,
374
+ progressPhase: I == null ? void 0 : I.phase
375
375
  };
376
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) || {
377
+ const c = ((h = g.result.status) == null ? void 0 : h.message) || {
378
378
  role: "agent",
379
379
  parts: []
380
- }, w = Ie(c);
380
+ }, I = ye(c);
381
381
  yield {
382
382
  id: g.result.id,
383
383
  sessionId: g.result.sessionId,
384
384
  status: g.result.status,
385
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
386
+ text: F(c),
387
+ progressMessage: I == null ? void 0 : I.summary,
388
+ progressPhase: I == null ? void 0 : I.phase
389
389
  };
390
390
  }
391
391
  }
392
- o = I;
392
+ o = v;
393
393
  }
394
394
  } finally {
395
395
  a.releaseLock();
@@ -397,8 +397,8 @@ async function* Xe(e, s = {}) {
397
397
  }
398
398
  class Ye {
399
399
  constructor() {
400
- se(this, "textContent", "");
401
- se(this, "toolCalls", /* @__PURE__ */ new Map());
400
+ te(this, "textContent", "");
401
+ te(this, "toolCalls", /* @__PURE__ */ new Map());
402
402
  }
403
403
  /**
404
404
  * Process a simple content delta (server's actual format)
@@ -483,10 +483,10 @@ class Ye {
483
483
  this.textContent = "", this.toolCalls.clear();
484
484
  }
485
485
  }
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;
486
+ function Ce(e, s, t = "request") {
487
+ throw clearTimeout(s), S("%s failed with error: %O", t, e), e instanceof Error && (S("Error message: %s", e.message), S("Error stack: %s", e.stack)), e;
488
488
  }
489
- function ke(e, s = "request") {
489
+ function be(e, s = "request") {
490
490
  if (!e.ok)
491
491
  throw new Error(`HTTP error! status: ${e.status}`);
492
492
  }
@@ -500,7 +500,7 @@ function Ze(e, s = "request") {
500
500
  return e.result;
501
501
  }
502
502
  function et(e, s = "streaming request") {
503
- if (ke(e, s), !e.body)
503
+ if (be(e, s), !e.body)
504
504
  throw new Error(`No response body for ${s}`);
505
505
  }
506
506
  function Re(e, s = "request") {
@@ -514,7 +514,7 @@ function tt(e, s) {
514
514
  return `${e}/${s}`;
515
515
  }
516
516
  function st(e, s, t, a) {
517
- M("Request: %s %s", e, s), M("Headers: %o", t), a && M("Body: %s", xe(a));
517
+ S("Request: %s %s", e, s), S("Headers: %o", t), a && S("Body: %s", Ae(a));
518
518
  }
519
519
  async function at(e, s = !1) {
520
520
  const t = {
@@ -526,7 +526,7 @@ async function at(e, s = !1) {
526
526
  }
527
527
  return t;
528
528
  }
529
- function Pe(e, s) {
529
+ function ke(e, s) {
530
530
  if (!s)
531
531
  return e;
532
532
  const t = new AbortController(), a = (n) => {
@@ -538,7 +538,7 @@ function Pe(e, s) {
538
538
  once: !0
539
539
  }), t.signal;
540
540
  }
541
- function _e(e, s, t) {
541
+ function Pe(e, s, t) {
542
542
  return {
543
543
  method: "POST",
544
544
  headers: e,
@@ -546,8 +546,8 @@ function _e(e, s, t) {
546
546
  signal: t
547
547
  };
548
548
  }
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(
549
+ async function Z(e, s, t, a, n, o) {
550
+ const { message: r, sessionId: l, taskId: i, metadata: d } = e, { agentId: u, agentUrl: h, authProvider: y, proxy: x } = s, { isStreaming: m = !1, enableTokenStreaming: f = !1 } = t, v = l || o, A = tt(h, u), g = await Ge(
551
551
  r,
552
552
  a,
553
553
  n
@@ -556,74 +556,74 @@ async function ee(e, s, t, a, n, o) {
556
556
  message: g,
557
557
  metadata: d
558
558
  };
559
- I && (c.sessionId = I);
560
- const w = Ke(
559
+ v && (c.sessionId = v);
560
+ const I = Ke(
561
561
  c,
562
- v ? "message/stream" : "message/send",
563
- y && v
562
+ m ? "message/stream" : "message/send",
563
+ f && m
564
564
  // Only enable token streaming if using SSE
565
- ), k = await at(h, v);
566
- return st("POST", A, k, w), {
567
- request: w,
568
- headers: k,
565
+ ), T = await at(y, m);
566
+ return st("POST", A, T, I), {
567
+ request: I,
568
+ headers: T,
569
569
  enhancedMessage: g,
570
- effectiveSessionId: I,
570
+ effectiveSessionId: v,
571
571
  fullAgentUrl: A
572
572
  };
573
573
  }
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,
574
+ async function _e(e, s, t = {}) {
575
+ const { request: a, headers: n, fullAgentUrl: o } = e, { timeout: r } = s, { abortSignal: l } = t, { timeoutId: i, controller: d } = Re(
576
+ r,
577
577
  "request"
578
- ), m = i ? Pe(u.signal, i) : u.signal;
578
+ ), u = l ? ke(d.signal, l) : d.signal;
579
579
  try {
580
- const h = _e(
581
- o,
582
- JSON.stringify(n),
583
- m
580
+ const h = Pe(
581
+ n,
582
+ JSON.stringify(a),
583
+ u
584
584
  );
585
- M("Making request to %s with options: %O", r, {
585
+ S("Making request to %s with options: %O", o, {
586
586
  method: h.method,
587
587
  headers: h.headers
588
588
  });
589
- const f = await a(r, h);
590
- clearTimeout(d), ke(f, "request");
591
- const v = await f.json();
592
- return M(
589
+ const y = await fetch(o, h);
590
+ clearTimeout(i), be(y, "request");
591
+ const x = await y.json();
592
+ return S(
593
593
  "Response from %s: %d %O",
594
- r,
595
- f.status,
596
- xe(v)
597
- ), Ze(v, "request");
594
+ o,
595
+ y.status,
596
+ Ae(x)
597
+ ), Ze(x, "request");
598
598
  } catch (h) {
599
- be(h, d, "request");
599
+ Ce(h, i, "request");
600
600
  }
601
601
  }
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
- enableTokenStreaming: d = !1
607
- } = t, { timeoutId: u, controller: m } = Re(
608
- l,
602
+ async function* Ee(e, s, t) {
603
+ const { request: a, headers: n, fullAgentUrl: o } = e, {
604
+ streamingTimeout: r = 6e4,
605
+ abortSignal: l,
606
+ enableTokenStreaming: i = !1
607
+ } = t, { timeoutId: d, controller: u } = Re(
608
+ r,
609
609
  "streaming request"
610
- ), h = i ? Pe(m.signal, i) : m.signal;
610
+ ), h = l ? ke(u.signal, l) : u.signal;
611
611
  try {
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)
612
+ const y = JSON.stringify(a), x = Pe(n, y, h), m = await fetch(o, x);
613
+ if (clearTimeout(d), et(m, "streaming request"), !m.body)
614
614
  throw new Error(
615
615
  "Response body is null - server may not support streaming"
616
616
  );
617
- const I = d && n.tokenStreaming === !0;
618
- yield* Xe(y.body, {
619
- supportDeltas: I
617
+ const f = i && a.tokenStreaming === !0;
618
+ yield* Xe(m.body, {
619
+ supportDeltas: f
620
620
  });
621
- } catch (f) {
622
- be(f, u, "streaming request");
621
+ } catch (y) {
622
+ Ce(y, d, "streaming request");
623
623
  }
624
624
  }
625
625
  const nt = 12e4;
626
- async function te(e, s, t, a, n) {
626
+ async function ee(e, s, t, a, n) {
627
627
  if (e.getAbilities) {
628
628
  const o = await e.getAbilities();
629
629
  if (o.length > 0)
@@ -646,7 +646,7 @@ async function te(e, s, t, a, n) {
646
646
  }
647
647
  };
648
648
  } catch (i) {
649
- return M(
649
+ return S(
650
650
  "Error executing ability %s: %O",
651
651
  r.name,
652
652
  i
@@ -665,7 +665,7 @@ async function te(e, s, t, a, n) {
665
665
  t
666
666
  ), returnToAgent: !0 };
667
667
  } catch (i) {
668
- return M(
668
+ return S(
669
669
  "Error executing ability %s: %O",
670
670
  r.name,
671
671
  i
@@ -694,11 +694,11 @@ async function te(e, s, t, a, n) {
694
694
  `No handler found for tool: ${s}. Tool provider must implement executeTool for non-ability tools.`
695
695
  );
696
696
  }
697
- const fe = /* @__PURE__ */ new Map();
697
+ const ge = /* @__PURE__ */ new Map();
698
698
  async function rt(e, s) {
699
699
  if (!e || !s || !e.getAvailableTools)
700
700
  return !1;
701
- const t = W(s);
701
+ const t = B(s);
702
702
  if (t.length === 0)
703
703
  return !1;
704
704
  try {
@@ -714,19 +714,19 @@ async function rt(e, s) {
714
714
  return !1;
715
715
  }
716
716
  function ot() {
717
- fe.clear();
717
+ ge.clear();
718
718
  }
719
719
  function it(e) {
720
720
  return e.map((s) => {
721
- const t = s.data.toolCallId, a = fe.get(t);
721
+ const t = s.data.toolCallId, a = ge.get(t);
722
722
  if (a && a.resolvedValue !== null) {
723
723
  const n = a.resolvedValue;
724
- return n.error ? J(
724
+ return n.error ? D(
725
725
  t,
726
726
  s.data.toolId,
727
727
  void 0,
728
728
  n.error
729
- ) : J(
729
+ ) : D(
730
730
  t,
731
731
  s.data.toolId,
732
732
  n
@@ -735,29 +735,29 @@ function it(e) {
735
735
  return s;
736
736
  });
737
737
  }
738
- async function Ne(e, s, t) {
738
+ async function Oe(e, s, t) {
739
739
  const a = [], n = [];
740
740
  let o = !1;
741
741
  for (const r of e) {
742
742
  const { toolCallId: l, toolId: i, arguments: d } = r.data;
743
743
  try {
744
- const u = await te(
744
+ const u = await ee(
745
745
  s,
746
746
  i,
747
747
  d,
748
748
  t,
749
749
  l
750
- ), { result: m, returnToAgent: h, agentMessage: f } = ge(u);
751
- h && (o = !0), f && n.push(X(f)), a.push(
752
- J(
750
+ ), { result: h, returnToAgent: y, agentMessage: x } = de(u);
751
+ y && (o = !0), x && n.push(Q(x)), a.push(
752
+ D(
753
753
  l,
754
754
  i,
755
- m
755
+ h
756
756
  )
757
757
  );
758
758
  } catch (u) {
759
759
  o = !0, a.push(
760
- J(
760
+ D(
761
761
  l,
762
762
  i,
763
763
  void 0,
@@ -768,7 +768,7 @@ async function Ne(e, s, t) {
768
768
  }
769
769
  return { results: a, shouldReturnToAgent: o, agentMessages: n };
770
770
  }
771
- function we(e) {
771
+ function Ie(e) {
772
772
  const s = [];
773
773
  for (const t of e)
774
774
  for (const a of t.parts)
@@ -781,8 +781,8 @@ function we(e) {
781
781
  }) : (a.type === "data" || a.type === "file") && s.push(a);
782
782
  return s;
783
783
  }
784
- async function ne(e, s, t, a, n, o, r, l) {
785
- const d = await ee(
784
+ async function ae(e, s, t, a, n, o, r) {
785
+ const i = await Z(
786
786
  {
787
787
  message: s,
788
788
  taskId: e,
@@ -791,79 +791,68 @@ async function ne(e, s, t, a, n, o, r, l) {
791
791
  },
792
792
  t,
793
793
  { isStreaming: !1 },
794
+ a,
794
795
  n,
795
- o,
796
- r
797
- );
798
- return await Ee(
799
- d,
800
- t,
801
- {
802
- abortSignal: l
803
- },
804
- a
796
+ o
805
797
  );
798
+ return await _e(i, t, {
799
+ abortSignal: r
800
+ });
806
801
  }
807
- async function Te(e, s, t, a, n, o, r, l, i, d = []) {
808
- const u = {
802
+ async function we(e, s, t, a, n, o, r, l, i = []) {
803
+ const d = {
809
804
  message: s,
810
805
  taskId: e,
811
- sessionId: r
806
+ sessionId: o
812
807
  // Use the provided sessionId to maintain conversation continuity
813
- }, m = i || { isStreaming: !0 }, h = await ee(
814
- u,
808
+ }, u = l || { isStreaming: !0 }, h = await Z(
809
+ d,
815
810
  t,
816
811
  {
817
- ...m
812
+ ...u
818
813
  },
814
+ a,
819
815
  n,
820
- o,
821
- r
822
- ), f = Oe(
823
- h,
824
- t,
825
- {
826
- ...m,
827
- abortSignal: l
828
- },
829
- a
830
- );
831
- return $e(
832
- f,
816
+ o
817
+ ), y = Ee(h, t, {
818
+ ...u,
819
+ abortSignal: r
820
+ });
821
+ return Ne(
822
+ y,
823
+ a,
833
824
  n,
834
- o,
835
825
  t,
836
- a,
837
- r,
826
+ o,
838
827
  !0,
839
828
  // withHistory
840
- d,
829
+ i,
841
830
  // preserve conversation parts across continuation
842
- l,
843
- m
831
+ r,
832
+ u
844
833
  // Pass through the same request options
845
834
  );
846
835
  }
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;
836
+ async function* Ne(e, s, t, a, n, o = !0, r = [], l, i) {
837
+ var d, u, h, y, x, m, f, v, A, g, c, I;
849
838
  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(
839
+ if (T.sessionId && !n && (n = T.sessionId), yield T, T.status.state === "running" && T.status.message && s && await rt(
851
840
  s,
852
841
  T.status.message
853
842
  )) {
854
- const _ = W(
843
+ const b = B(
855
844
  T.status.message
856
845
  );
857
- for (const O of _) {
858
- const { toolCallId: q, toolId: p, arguments: C } = O.data;
859
- te(
846
+ for (const E of b) {
847
+ const { toolCallId: U, toolId: $, arguments: p } = E.data;
848
+ ee(
860
849
  s,
850
+ $,
861
851
  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);
852
+ (u = (d = T.status) == null ? void 0 : d.message) == null ? void 0 : u.messageId,
853
+ U
854
+ ).catch((M) => {
855
+ S("Tool execution failed for %s: %O", $, M);
867
856
  });
868
857
  }
869
858
  yield {
@@ -873,7 +862,7 @@ async function* $e(e, s, t, a, n, o, r = !0, l = [], i, d) {
873
862
  message: {
874
863
  role: "agent",
875
864
  kind: "message",
876
- parts: _,
865
+ parts: b,
877
866
  messageId: N()
878
867
  }
879
868
  },
@@ -882,176 +871,174 @@ async function* $e(e, s, t, a, n, o, r = !0, l = [], i, d) {
882
871
  };
883
872
  }
884
873
  if (T.status.state === "input-required" && T.status.message && s) {
885
- const _ = W(
874
+ const b = B(
886
875
  T.status.message
887
876
  );
888
- if (_.length > 0) {
889
- const O = [];
890
- let q = !1;
891
- const p = [], C = [];
892
- for (const S of _) {
877
+ if (b.length > 0) {
878
+ const E = [];
879
+ let U = !1;
880
+ const $ = [], p = [];
881
+ for (const M of b) {
893
882
  const {
894
- toolCallId: b,
895
- toolId: $,
896
- arguments: R
897
- } = S.data;
883
+ toolCallId: w,
884
+ toolId: k,
885
+ arguments: O
886
+ } = M.data;
898
887
  try {
899
- const P = await te(
888
+ const R = await ee(
900
889
  s,
901
- $,
902
- R,
903
- (f = (h = T.status) == null ? void 0 : h.message) == null ? void 0 : f.messageId,
904
- b
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,
890
+ k,
891
+ O,
892
+ (y = (h = T.status) == null ? void 0 : h.message) == null ? void 0 : y.messageId,
893
+ w
894
+ ), { result: C, returnToAgent: P, agentMessage: j } = de(R);
895
+ if (P && (U = !0), j && p.push(
896
+ Q(j)
897
+ ), C.result instanceof Promise) {
898
+ const K = C.result, q = {
899
+ promise: K,
911
900
  resolvedValue: null
912
901
  };
913
- fe.set(
914
- b,
915
- E
916
- ), H.then((F) => {
917
- E.resolvedValue = F;
918
- }).catch((F) => {
919
- M(
902
+ ge.set(
903
+ w,
904
+ q
905
+ ), K.then((_) => {
906
+ q.resolvedValue = _;
907
+ }).catch((_) => {
908
+ S(
920
909
  "Promise rejected for tool call %s: %O",
921
- b,
922
- F
923
- ), E.resolvedValue = {
924
- error: F instanceof Error ? F.message : String(F)
910
+ w,
911
+ _
912
+ ), q.resolvedValue = {
913
+ error: _ instanceof Error ? _.message : String(_)
925
914
  };
926
915
  });
927
916
  }
928
- const L = J(
929
- b,
930
- $,
931
- x
917
+ const J = D(
918
+ w,
919
+ k,
920
+ C
932
921
  );
933
- O.push(L), p.push(L);
934
- } catch (P) {
935
- const x = J(
936
- b,
937
- $,
922
+ E.push(J), $.push(J);
923
+ } catch (R) {
924
+ const C = D(
925
+ w,
926
+ k,
938
927
  void 0,
939
- P instanceof Error ? P.message : String(P)
928
+ R instanceof Error ? R.message : String(R)
940
929
  );
941
- O.push(x), p.push(x);
930
+ E.push(C), $.push(C);
942
931
  }
943
932
  }
944
- if (l.push(T.status.message), O.length > 0 && l.push({
933
+ if (r.push(T.status.message), E.length > 0 && r.push({
945
934
  role: "agent",
946
935
  kind: "message",
947
- parts: O,
936
+ parts: E,
948
937
  messageId: N()
949
- }), q) {
950
- const S = we(l), b = Z(
938
+ }), U) {
939
+ const M = Ie(r), w = Y(
951
940
  [],
952
941
  // Empty array - tool results are already in historyDataParts
953
- S
942
+ M
954
943
  );
955
944
  yield {
956
945
  id: T.id,
957
946
  status: {
958
947
  state: "working",
959
- message: b
948
+ message: w
960
949
  },
961
950
  final: !1,
962
951
  text: ""
963
952
  };
964
- const $ = await Te(
953
+ const k = await we(
965
954
  T.id,
966
- b,
955
+ w,
967
956
  a,
968
957
  s,
969
958
  t,
970
959
  n,
971
- o,
960
+ l,
972
961
  i,
973
- d,
974
- l
962
+ r
975
963
  );
976
- let R = null;
977
- for await (const U of $)
978
- U.final ? R = U : yield U;
979
- if (!R)
964
+ let O = null;
965
+ for await (const P of k)
966
+ P.final ? O = P : yield P;
967
+ if (!O)
980
968
  throw new Error(
981
969
  "Continue task stream ended without final result"
982
970
  );
983
- let P = (v = R.status) != null && v.message ? W(
984
- R.status.message
985
- ) : [], x = R;
986
- if (P.length > 0)
971
+ let R = (x = O.status) != null && x.message ? B(
972
+ O.status.message
973
+ ) : [], C = O;
974
+ if (R.length > 0)
987
975
  for (yield {
988
- ...R,
976
+ ...O,
989
977
  final: !1,
990
- text: D(
991
- ((y = R.status) == null ? void 0 : y.message) || {
978
+ text: F(
979
+ ((m = O.status) == null ? void 0 : m.message) || {
992
980
  parts: [],
993
981
  messageId: N()
994
982
  }
995
983
  )
996
- }; P.length > 0; ) {
997
- (I = x.status) != null && I.message && l.push(
998
- x.status.message
984
+ }; R.length > 0; ) {
985
+ (f = C.status) != null && f.message && r.push(
986
+ C.status.message
999
987
  );
1000
988
  const {
1001
- results: U,
1002
- shouldReturnToAgent: K
1003
- } = await Ne(
1004
- P,
989
+ results: P,
990
+ shouldReturnToAgent: j
991
+ } = await Oe(
992
+ R,
1005
993
  s,
1006
- (g = (A = x.status) == null ? void 0 : A.message) == null ? void 0 : g.messageId
994
+ (A = (v = C.status) == null ? void 0 : v.message) == null ? void 0 : A.messageId
1007
995
  );
1008
- if (U.length > 0 && (yield {
1009
- id: x.id,
996
+ if (P.length > 0 && (yield {
997
+ id: C.id,
1010
998
  status: {
1011
999
  state: "working",
1012
1000
  message: {
1013
1001
  role: "agent",
1014
1002
  kind: "message",
1015
- parts: U,
1003
+ parts: P,
1016
1004
  messageId: N()
1017
1005
  }
1018
1006
  // Simple message with just the results
1019
1007
  },
1020
1008
  final: !1,
1021
1009
  text: ""
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,
1010
+ }), j) {
1011
+ const J = o ? Ie(
1012
+ r
1013
+ ) : [], K = Y(
1014
+ P,
1015
+ J
1016
+ ), q = await we(
1017
+ C.id,
1018
+ K,
1031
1019
  a,
1032
1020
  s,
1033
1021
  t,
1034
1022
  n,
1035
- o,
1023
+ l,
1036
1024
  i,
1037
- d,
1038
- l
1025
+ r
1039
1026
  );
1040
- let F = null;
1041
- for await (const G of E)
1042
- G.final ? F = G : yield G;
1043
- if (!F)
1027
+ let _ = null;
1028
+ for await (const V of q)
1029
+ V.final ? _ = V : yield V;
1030
+ if (!_)
1044
1031
  throw new Error(
1045
1032
  "Continue task stream ended without final result"
1046
1033
  );
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,
1034
+ C = _, R = (g = C.status) != null && g.message ? B(
1035
+ C.status.message
1036
+ ) : [], R.length > 0 && (yield {
1037
+ id: C.id,
1038
+ status: C.status,
1052
1039
  final: !1,
1053
- text: D(
1054
- ((w = x.status) == null ? void 0 : w.message) || {
1040
+ text: F(
1041
+ ((c = C.status) == null ? void 0 : c.message) || {
1055
1042
  parts: [],
1056
1043
  messageId: N()
1057
1044
  }
@@ -1061,39 +1048,39 @@ async function* $e(e, s, t, a, n, o, r = !0, l = [], i, d) {
1061
1048
  break;
1062
1049
  }
1063
1050
  yield {
1064
- ...x,
1051
+ ...C,
1065
1052
  final: !0,
1066
- text: D(
1067
- ((k = x.status) == null ? void 0 : k.message) || {
1053
+ text: F(
1054
+ ((I = C.status) == null ? void 0 : I.message) || {
1068
1055
  parts: [],
1069
1056
  messageId: N()
1070
1057
  }
1071
1058
  )
1072
1059
  };
1073
1060
  } else {
1074
- const S = {
1061
+ const M = {
1075
1062
  ...T.status.message,
1076
- parts: p
1077
- }, b = {
1063
+ parts: $
1064
+ }, w = {
1078
1065
  ...T,
1079
1066
  status: {
1080
1067
  ...T.status,
1081
- message: S
1068
+ message: M
1082
1069
  },
1083
- final: C.length === 0,
1070
+ final: p.length === 0,
1084
1071
  // Only final if no agent messages to follow
1085
- text: D(S)
1072
+ text: F(M)
1086
1073
  };
1087
- if (yield b, C.length > 0) {
1088
- const $ = C.map((P) => D(P)).join(" "), R = X($);
1074
+ if (yield w, p.length > 0) {
1075
+ const k = p.map((R) => F(R)).join(" "), O = Q(k);
1089
1076
  yield {
1090
- id: b.id,
1077
+ id: w.id,
1091
1078
  status: {
1092
1079
  state: "completed",
1093
- message: R
1080
+ message: O
1094
1081
  },
1095
1082
  final: !0,
1096
- text: $
1083
+ text: k
1097
1084
  };
1098
1085
  }
1099
1086
  }
@@ -1110,206 +1097,199 @@ function lt(e) {
1110
1097
  timeout: o = nt,
1111
1098
  toolProvider: r,
1112
1099
  contextProvider: l,
1113
- enableStreaming: i = !1,
1114
- fetchCallback: d = fetch
1115
- } = e, u = {
1100
+ enableStreaming: i = !1
1101
+ } = e, d = {
1116
1102
  agentId: s,
1117
1103
  agentUrl: t,
1118
1104
  authProvider: a,
1119
1105
  timeout: o
1120
1106
  };
1121
1107
  return {
1122
- async sendMessage(m) {
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(
1127
- m,
1108
+ async sendMessage(u) {
1109
+ var g, c;
1110
+ const { abortSignal: h } = u, y = u.sessionId || n || void 0, x = [];
1111
+ x.push(u.message);
1112
+ const m = await Z(
1128
1113
  u,
1114
+ d,
1129
1115
  { isStreaming: !1 },
1130
1116
  r,
1131
1117
  l,
1132
- f
1118
+ y
1133
1119
  );
1134
- let I = await Ee(
1135
- y,
1136
- u,
1137
- { abortSignal: h },
1138
- d
1120
+ let f = await _e(
1121
+ m,
1122
+ d,
1123
+ { abortSignal: h }
1139
1124
  );
1140
- const A = [], g = [];
1141
- for (; I.status.message && r; ) {
1142
- const k = W(
1143
- I.status.message
1125
+ const v = [], A = [];
1126
+ for (; f.status.message && r; ) {
1127
+ const I = B(
1128
+ f.status.message
1144
1129
  );
1145
- if (k.length === 0)
1130
+ if (I.length === 0)
1146
1131
  break;
1147
- A.push(...k);
1132
+ v.push(...I);
1148
1133
  const T = [];
1149
- let _ = !1;
1150
- for (const O of k) {
1134
+ let b = !1;
1135
+ for (const E of I) {
1151
1136
  const {
1152
- toolCallId: q,
1153
- toolId: p,
1154
- arguments: C
1155
- } = O.data;
1137
+ toolCallId: U,
1138
+ toolId: $,
1139
+ arguments: p
1140
+ } = E.data;
1156
1141
  try {
1157
- const S = await te(
1142
+ const M = await ee(
1158
1143
  r,
1159
- p,
1160
- C
1161
- ), { result: b, returnToAgent: $, agentMessage: R } = ge(S);
1162
- $ && (_ = !0), R && g.push(
1163
- X(R)
1144
+ $,
1145
+ p
1146
+ ), { result: w, returnToAgent: k, agentMessage: O } = de(M);
1147
+ k && (b = !0), O && A.push(
1148
+ Q(O)
1164
1149
  );
1165
- const P = J(
1166
- q,
1167
- p,
1168
- b
1150
+ const R = D(
1151
+ U,
1152
+ $,
1153
+ w
1169
1154
  );
1170
- T.push(P), A.push(P);
1171
- } catch (S) {
1172
- const b = J(
1173
- q,
1174
- p,
1155
+ T.push(R), v.push(R);
1156
+ } catch (M) {
1157
+ const w = D(
1158
+ U,
1159
+ $,
1175
1160
  void 0,
1176
- S instanceof Error ? S.message : String(S)
1161
+ M instanceof Error ? M.message : String(M)
1177
1162
  );
1178
- T.push(b), A.push(b);
1163
+ T.push(w), v.push(w);
1179
1164
  }
1180
1165
  }
1181
- if (v.push(I.status.message), _) {
1182
- const O = Z(T);
1183
- I = await ne(
1184
- I.id,
1185
- O,
1186
- u,
1166
+ if (x.push(f.status.message), b) {
1167
+ const E = Y(T);
1168
+ f = await ae(
1169
+ f.id,
1170
+ E,
1187
1171
  d,
1188
1172
  r,
1189
1173
  l,
1190
- f,
1174
+ y,
1191
1175
  h
1192
1176
  );
1193
1177
  } else
1194
1178
  break;
1195
1179
  }
1196
- if (A.length > 0 && (c = I.status) != null && c.message) {
1197
- const k = {
1198
- ...I.status.message,
1199
- parts: A
1180
+ if (v.length > 0 && (g = f.status) != null && g.message) {
1181
+ const I = {
1182
+ ...f.status.message,
1183
+ parts: v
1200
1184
  };
1201
- I = {
1202
- ...I,
1185
+ f = {
1186
+ ...f,
1203
1187
  status: {
1204
- ...I.status,
1205
- message: k
1188
+ ...f.status,
1189
+ message: I
1206
1190
  }
1207
1191
  };
1208
1192
  }
1209
- if (g.length > 0) {
1210
- const k = g.map((_) => D(_)).join(" "), T = X(k);
1193
+ if (A.length > 0) {
1194
+ const I = A.map((b) => F(b)).join(" "), T = Q(I);
1211
1195
  return {
1212
- ...I,
1196
+ ...f,
1213
1197
  // Keep the enhanced message with tool results
1214
1198
  // The agent message will be handled separately by the caller
1215
- text: k,
1199
+ text: I,
1216
1200
  agentMessage: T
1217
1201
  // Add this for the caller to handle
1218
1202
  };
1219
1203
  }
1220
1204
  return {
1221
- ...I,
1222
- text: D(
1223
- ((w = I.status) == null ? void 0 : w.message) || {
1205
+ ...f,
1206
+ text: F(
1207
+ ((c = f.status) == null ? void 0 : c.message) || {
1224
1208
  parts: [],
1225
1209
  messageId: N()
1226
1210
  }
1227
1211
  )
1228
1212
  };
1229
1213
  },
1230
- async *sendMessageStream(m) {
1214
+ async *sendMessageStream(u) {
1231
1215
  const {
1232
1216
  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(
1238
- m,
1217
+ abortSignal: y,
1218
+ enableStreaming: x
1219
+ } = u, m = u.sessionId || n || void 0, f = x ?? i, v = [];
1220
+ v.push(u.message);
1221
+ const A = await Z(
1239
1222
  u,
1223
+ d,
1240
1224
  {
1241
1225
  isStreaming: !0,
1242
1226
  // Always use message/stream endpoint for SSE
1243
- enableTokenStreaming: I
1227
+ enableTokenStreaming: f
1244
1228
  },
1245
1229
  r,
1246
1230
  l,
1247
- y
1248
- ), c = Oe(
1249
- g,
1250
- u,
1231
+ m
1232
+ ), g = Ee(
1233
+ A,
1234
+ d,
1251
1235
  {
1252
- enableTokenStreaming: I,
1236
+ enableTokenStreaming: f,
1253
1237
  // Token streaming is optional
1254
1238
  streamingTimeout: o,
1255
- abortSignal: f
1256
- },
1257
- d
1239
+ abortSignal: y
1240
+ }
1258
1241
  );
1259
- yield* $e(
1260
- c,
1242
+ yield* Ne(
1243
+ g,
1261
1244
  r,
1262
1245
  l,
1263
- u,
1264
1246
  d,
1265
- y,
1247
+ m,
1266
1248
  h,
1267
- A,
1268
- f,
1249
+ v,
1250
+ y,
1269
1251
  {
1270
1252
  isStreaming: !0,
1271
- enableTokenStreaming: I,
1253
+ enableTokenStreaming: f,
1272
1254
  streamingTimeout: o
1273
1255
  }
1274
1256
  );
1275
1257
  },
1276
- async continueTask(m, h, f) {
1277
- var A;
1278
- const v = le(h);
1279
- let I = await ne(
1280
- m,
1281
- v,
1258
+ async continueTask(u, h, y) {
1259
+ var v;
1260
+ const x = ie(h);
1261
+ let f = await ae(
1282
1262
  u,
1263
+ x,
1283
1264
  d,
1284
1265
  r,
1285
1266
  l,
1286
- f
1267
+ y
1287
1268
  );
1288
- for (; I.status.state === "input-required" && I.status.message && r; ) {
1289
- const g = W(
1290
- I.status.message
1269
+ for (; f.status.state === "input-required" && f.status.message && r; ) {
1270
+ const A = B(
1271
+ f.status.message
1291
1272
  );
1292
- if (g.length === 0)
1273
+ if (A.length === 0)
1293
1274
  break;
1294
- const { results: c, shouldReturnToAgent: w } = await Ne(g, r);
1295
- if (w) {
1296
- const k = Z(c);
1297
- I = await ne(
1298
- I.id,
1299
- k,
1300
- u,
1275
+ const { results: g, shouldReturnToAgent: c } = await Oe(A, r);
1276
+ if (c) {
1277
+ const I = Y(g);
1278
+ f = await ae(
1279
+ f.id,
1280
+ I,
1301
1281
  d,
1302
1282
  r,
1303
1283
  l,
1304
- f
1284
+ y
1305
1285
  );
1306
1286
  } else
1307
1287
  break;
1308
1288
  }
1309
1289
  return {
1310
- ...I,
1311
- text: D(
1312
- ((A = I.status) == null ? void 0 : A.message) || {
1290
+ ...f,
1291
+ text: F(
1292
+ ((v = f.status) == null ? void 0 : v.message) || {
1313
1293
  parts: [],
1314
1294
  messageId: N()
1315
1295
  }
@@ -1349,12 +1329,15 @@ function ct(e) {
1349
1329
  s.push(r);
1350
1330
  }
1351
1331
  }
1352
- if (t && !Array.isArray(t) && Array.isArray(t.sources) && t.sources.length > 0) {
1353
- const o = {
1354
- type: "data",
1355
- data: { sources: t.sources }
1356
- };
1357
- s.push(o);
1332
+ if (t && !Array.isArray(t)) {
1333
+ const o = t, r = o.flags && typeof o.flags == "object" && o.flags !== null, l = Array.isArray(o.sources) && o.sources.length > 0;
1334
+ if (r || l) {
1335
+ const i = {};
1336
+ r && (i.flags = o.flags), l && (i.sources = o.sources), s.push({
1337
+ type: "data",
1338
+ data: i
1339
+ });
1340
+ }
1358
1341
  }
1359
1342
  const a = e.role === "user" ? "user" : "agent", n = e.ts ? e.ts * 1e3 : (/* @__PURE__ */ new Date(
1360
1343
  e.created_at.replace(" ", "T") + "Z"
@@ -1382,7 +1365,7 @@ function ut(e) {
1382
1365
  }
1383
1366
  function dt(e, s = !1) {
1384
1367
  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 = {
1368
+ const a = e.messages.filter((h) => h.role === "tool_call" ? !1 : h.role === "tool_result" ? !(!s || ut(h)) : !0).map(ct), n = {
1386
1369
  currentPage: ((o = e.metadata) == null ? void 0 : o.current_page) ?? 1,
1387
1370
  itemsPerPage: ((r = e.metadata) == null ? void 0 : r.items_per_page) ?? 10,
1388
1371
  totalPages: ((l = e.metadata) == null ? void 0 : l.total_pages) ?? 1,
@@ -1401,9 +1384,9 @@ class z extends Error {
1401
1384
  super(s), this.statusCode = t, this.details = a, this.name = "ServerConversationError";
1402
1385
  }
1403
1386
  }
1404
- const me = "https://public-api.wordpress.com";
1405
- async function ce(e, s, t = 1, a = 50, n = !1) {
1406
- const { botId: o, apiBaseUrl: r = me, authProvider: l } = s;
1387
+ const fe = "https://public-api.wordpress.com";
1388
+ async function le(e, s, t = 1, a = 50, n = !1) {
1389
+ const { botId: o, apiBaseUrl: r = fe, authProvider: l } = s;
1407
1390
  if (!e || !o)
1408
1391
  throw new Error(
1409
1392
  "chatId and botId are required to load conversation from server"
@@ -1413,58 +1396,58 @@ async function ce(e, s, t = 1, a = 50, n = !1) {
1413
1396
  o
1414
1397
  )}/${encodeURIComponent(e)}`
1415
1398
  );
1416
- u.searchParams.set("page_number", i.toString()), u.searchParams.set("items_per_page", d.toString()), M(
1399
+ u.searchParams.set("page_number", i.toString()), u.searchParams.set("items_per_page", d.toString()), S(
1417
1400
  "Loading conversation from server: %s (page %d)",
1418
1401
  e,
1419
1402
  i
1420
1403
  );
1421
1404
  try {
1422
- const m = {
1405
+ const h = {
1423
1406
  "Content-Type": "application/json"
1424
1407
  };
1425
1408
  if (l) {
1426
- const y = await l();
1427
- Object.assign(m, y);
1409
+ const f = await l();
1410
+ Object.assign(h, f);
1428
1411
  }
1429
- const h = await fetch(u.toString(), {
1412
+ const y = await fetch(u.toString(), {
1430
1413
  method: "GET",
1431
- headers: m
1414
+ headers: h
1432
1415
  });
1433
- if (!h.ok) {
1434
- const y = await h.text();
1435
- let I = `Failed to load conversation from server: ${h.status} ${h.statusText}`;
1416
+ if (!y.ok) {
1417
+ const f = await y.text();
1418
+ let v = `Failed to load conversation from server: ${y.status} ${y.statusText}`;
1436
1419
  try {
1437
- const g = JSON.parse(y);
1438
- g.message && (I = g.message);
1420
+ const g = JSON.parse(f);
1421
+ g.message && (v = g.message);
1439
1422
  } catch {
1440
1423
  }
1441
1424
  const A = new z(
1442
- I,
1443
- h.status,
1444
- y
1425
+ v,
1426
+ y.status,
1427
+ f
1445
1428
  );
1446
- throw M("Failed to load conversation from server: %O", A), A;
1429
+ throw S("Failed to load conversation from server: %O", A), A;
1447
1430
  }
1448
- const f = await h.json(), v = dt(f, n);
1449
- return M(
1431
+ const x = await y.json(), m = dt(x, n);
1432
+ return S(
1450
1433
  "Loaded %d messages from server (page %d/%d)",
1451
- v.messages.length,
1452
- v.pagination.currentPage,
1453
- v.pagination.totalPages
1454
- ), v;
1455
- } catch (m) {
1456
- if (m instanceof z)
1457
- throw m;
1458
- const h = new z(
1459
- `Network error loading conversation: ${m.message}`,
1434
+ m.messages.length,
1435
+ m.pagination.currentPage,
1436
+ m.pagination.totalPages
1437
+ ), m;
1438
+ } catch (h) {
1439
+ if (h instanceof z)
1440
+ throw h;
1441
+ const y = new z(
1442
+ `Network error loading conversation: ${h.message}`,
1460
1443
  void 0,
1461
- m
1444
+ h
1462
1445
  );
1463
- throw M("Network error loading conversation: %O", m), h;
1446
+ throw S("Network error loading conversation: %O", h), y;
1464
1447
  }
1465
1448
  }
1466
1449
  async function Lt(e, s, t = !1) {
1467
- const { apiBaseUrl: a = me, authProvider: n } = s, o = new URL(
1450
+ const { apiBaseUrl: a = fe, authProvider: n } = s, o = new URL(
1468
1451
  `${a}/wpcom/v2/odie/conversations/${encodeURIComponent(
1469
1452
  e
1470
1453
  )}`
@@ -1472,7 +1455,7 @@ async function Lt(e, s, t = !1) {
1472
1455
  o.searchParams.set(
1473
1456
  "truncation_method",
1474
1457
  t ? "first_message" : "last_message"
1475
- ), M("Listing conversations from server for bot: %s", e);
1458
+ ), S("Listing conversations from server for bot: %s", e);
1476
1459
  try {
1477
1460
  const r = {
1478
1461
  "Content-Type": "application/json"
@@ -1486,15 +1469,15 @@ async function Lt(e, s, t = !1) {
1486
1469
  headers: r
1487
1470
  });
1488
1471
  if (!l.ok) {
1489
- const d = await l.text(), u = `Failed to list conversations: ${l.status} ${l.statusText}`, m = new z(
1472
+ const d = await l.text(), u = `Failed to list conversations: ${l.status} ${l.statusText}`, h = new z(
1490
1473
  u,
1491
1474
  l.status,
1492
1475
  d
1493
1476
  );
1494
- throw M("Failed to list conversations: %O", m), m;
1477
+ throw S("Failed to list conversations: %O", h), h;
1495
1478
  }
1496
1479
  const i = await l.json();
1497
- return M("Loaded %d conversations from server", i.length), i;
1480
+ return S("Loaded %d conversations from server", i.length), i;
1498
1481
  } catch (r) {
1499
1482
  if (r instanceof z)
1500
1483
  throw r;
@@ -1503,11 +1486,11 @@ async function Lt(e, s, t = !1) {
1503
1486
  void 0,
1504
1487
  r
1505
1488
  );
1506
- throw M("Network error listing conversations: %O", r), l;
1489
+ throw S("Network error listing conversations: %O", r), l;
1507
1490
  }
1508
1491
  }
1509
1492
  async function Bt(e, s, t = 10, a = !1) {
1510
- const n = await ce(
1493
+ const n = await le(
1511
1494
  e,
1512
1495
  s,
1513
1496
  1,
@@ -1522,7 +1505,7 @@ async function Bt(e, s, t = 10, a = !1) {
1522
1505
  ), r = [...n.messages], l = [];
1523
1506
  for (let i = 2; i <= o; i++)
1524
1507
  l.push(
1525
- ce(e, s, i, 50, a)
1508
+ le(e, s, i, 50, a)
1526
1509
  );
1527
1510
  try {
1528
1511
  const i = await Promise.all(l);
@@ -1538,50 +1521,65 @@ async function Bt(e, s, t = 10, a = !1) {
1538
1521
  chatId: n.chatId
1539
1522
  };
1540
1523
  } catch (i) {
1541
- return M("Failed to load all pages: %O", i), {
1524
+ return S("Failed to load all pages: %O", i), {
1542
1525
  messages: r,
1543
1526
  pagination: n.pagination,
1544
1527
  chatId: n.chatId
1545
1528
  };
1546
1529
  }
1547
1530
  }
1548
- const pe = "a8c_agenttic_conversation_history";
1531
+ const me = "a8c_agenttic_conversation_history";
1549
1532
  function gt(e) {
1550
- var m, h;
1533
+ var y, x;
1551
1534
  const s = e.parts.filter(
1552
- (f) => f.type === "text"
1553
- ), t = s.map((f) => f.text).join(`
1535
+ (m) => m.type === "text"
1536
+ ), t = s.map((m) => m.text).join(`
1554
1537
  `), a = s.some(
1555
- (f) => {
1556
- var v;
1557
- return ((v = f.metadata) == null ? void 0 : v.contentType) === "context";
1538
+ (m) => {
1539
+ var f;
1540
+ return ((f = m.metadata) == null ? void 0 : f.contentType) === "context";
1558
1541
  }
1559
1542
  ) ? "context" : void 0, n = e.parts.filter(
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
1543
+ (m) => m.type === "data" && "toolCallId" in m.data && "arguments" in m.data
1544
+ ).map((m) => ({
1545
+ toolCallId: m.data.toolCallId,
1546
+ toolId: m.data.toolId,
1547
+ arguments: m.data.arguments
1565
1548
  })), 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;
1549
+ (m) => m.type === "data" && "toolCallId" in m.data && "result" in m.data
1550
+ ).map((m) => ({
1551
+ toolCallId: m.data.toolCallId,
1552
+ result: m.data.result,
1553
+ error: m.data.error
1554
+ })), r = e.parts.filter((m) => m.type === "file").map((m) => ({
1555
+ name: m.file.name,
1556
+ mimeType: m.file.mimeType,
1557
+ uri: m.file.uri
1558
+ }));
1559
+ let l;
1560
+ for (const m of e.parts) {
1561
+ if (m.type !== "data" || !m.data || typeof m.data != "object" || "toolCallId" in m.data)
1562
+ continue;
1563
+ const f = m.data, v = f.flags;
1564
+ if (v && typeof v == "object" && v !== null && "forward_to_human_support" in v) {
1565
+ l = {
1566
+ flags: v,
1567
+ ..."sources" in f && { sources: f.sources }
1568
+ };
1569
+ break;
1570
+ }
1571
+ }
1572
+ const d = n.length > 0 || o.length > 0 ? "agent" : e.role, u = ((y = e.metadata) == null ? void 0 : y.timestamp) ?? Date.now(), h = ((x = e.metadata) == null ? void 0 : x.archived) ?? void 0;
1576
1573
  return {
1577
- role: i,
1574
+ role: d,
1578
1575
  content: t || "(No text content)",
1579
- timestamp: d,
1580
- ...u !== void 0 && { archived: u },
1576
+ timestamp: u,
1577
+ ...h !== void 0 && { archived: h },
1581
1578
  ...a && { contentType: a },
1582
1579
  ...r.length > 0 && { files: r },
1583
1580
  ...n.length > 0 && { toolCalls: n },
1584
- ...o.length > 0 && { toolResults: o }
1581
+ ...o.length > 0 && { toolResults: o },
1582
+ ...l && { agentMessageData: l }
1585
1583
  };
1586
1584
  }
1587
1585
  function ft(e) {
@@ -1624,6 +1622,10 @@ function ft(e) {
1624
1622
  ...t.error && { error: t.error }
1625
1623
  }
1626
1624
  });
1625
+ if (e.agentMessageData) {
1626
+ const t = {};
1627
+ e.agentMessageData.flags !== void 0 && (t.flags = e.agentMessageData.flags), "sources" in e.agentMessageData && (t.sources = e.agentMessageData.sources ?? null), Object.keys(t).length > 0 && s.push({ type: "data", data: t });
1628
+ }
1627
1629
  return {
1628
1630
  role: e.role,
1629
1631
  kind: "message",
@@ -1638,12 +1640,12 @@ function ft(e) {
1638
1640
  }
1639
1641
  };
1640
1642
  }
1641
- const V = /* @__PURE__ */ new Map(), mt = 50;
1643
+ const W = /* @__PURE__ */ new Map(), mt = 50;
1642
1644
  async function pt(e, s, t) {
1643
1645
  const a = t || e;
1644
- if (V.set(a, [...s]), V.size > mt) {
1645
- const n = V.keys().next().value;
1646
- n && V.delete(n);
1646
+ if (W.set(a, [...s]), W.size > mt) {
1647
+ const n = W.keys().next().value;
1648
+ n && W.delete(n);
1647
1649
  }
1648
1650
  if (!(typeof sessionStorage > "u"))
1649
1651
  try {
@@ -1653,11 +1655,11 @@ async function pt(e, s, t) {
1653
1655
  lastUpdated: Date.now()
1654
1656
  };
1655
1657
  sessionStorage.setItem(
1656
- `${pe}_${a}`,
1658
+ `${me}_${a}`,
1657
1659
  JSON.stringify(n)
1658
1660
  );
1659
1661
  } catch (n) {
1660
- M(
1662
+ S(
1661
1663
  "Failed to store conversation in sessionStorage for key %s: %O",
1662
1664
  a,
1663
1665
  n
@@ -1674,9 +1676,9 @@ async function yt(e, s) {
1674
1676
  const { odieBotId: t, authProvider: a } = s;
1675
1677
  if (!t)
1676
1678
  throw new Error("odieBotId is required for server storage");
1677
- const n = me;
1679
+ const n = fe;
1678
1680
  try {
1679
- const r = await ce(
1681
+ const r = await le(
1680
1682
  e,
1681
1683
  {
1682
1684
  botId: t,
@@ -1686,7 +1688,7 @@ async function yt(e, s) {
1686
1688
  1,
1687
1689
  50
1688
1690
  );
1689
- return M(
1691
+ return S(
1690
1692
  "Loaded conversation from server: %s (%d messages, page %d/%d)",
1691
1693
  e,
1692
1694
  r.messages.length,
@@ -1697,27 +1699,27 @@ async function yt(e, s) {
1697
1699
  pagination: r.pagination
1698
1700
  };
1699
1701
  } catch (o) {
1700
- throw M("Failed to load conversation from server: %O", o), o;
1702
+ throw S("Failed to load conversation from server: %O", o), o;
1701
1703
  }
1702
1704
  }
1703
1705
  async function It(e, s) {
1704
1706
  const t = s || e;
1705
- if (V.has(t))
1707
+ if (W.has(t))
1706
1708
  return {
1707
- messages: [...V.get(t)]
1709
+ messages: [...W.get(t)]
1708
1710
  };
1709
1711
  if (typeof sessionStorage > "u")
1710
1712
  return { messages: [] };
1711
1713
  try {
1712
1714
  const a = sessionStorage.getItem(
1713
- `${pe}_${t}`
1715
+ `${me}_${t}`
1714
1716
  );
1715
1717
  if (a) {
1716
1718
  const o = JSON.parse(a).messages.map(ft);
1717
- return V.set(t, o), { messages: [...o] };
1719
+ return W.set(t, o), { messages: [...o] };
1718
1720
  }
1719
1721
  } catch (a) {
1720
- M(
1722
+ S(
1721
1723
  "Failed to load conversation from sessionStorage for key %s: %O",
1722
1724
  t,
1723
1725
  a
@@ -1727,18 +1729,18 @@ async function It(e, s) {
1727
1729
  }
1728
1730
  async function wt(e, s) {
1729
1731
  const t = s || e;
1730
- if (V.delete(t), !(typeof sessionStorage > "u"))
1732
+ if (W.delete(t), !(typeof sessionStorage > "u"))
1731
1733
  try {
1732
- sessionStorage.removeItem(`${pe}_${t}`);
1734
+ sessionStorage.removeItem(`${me}_${t}`);
1733
1735
  } catch (a) {
1734
- M(
1736
+ S(
1735
1737
  "Failed to clear conversation from sessionStorage for key %s: %O",
1736
1738
  t,
1737
1739
  a
1738
1740
  );
1739
1741
  }
1740
1742
  }
1741
- function Q(e) {
1743
+ function G(e) {
1742
1744
  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);
1743
1745
  return {
1744
1746
  ...e,
@@ -1749,7 +1751,7 @@ function Q(e) {
1749
1751
  }
1750
1752
  };
1751
1753
  }
1752
- function Ue(e) {
1754
+ function $e(e) {
1753
1755
  const s = [];
1754
1756
  for (const t of e)
1755
1757
  for (const a of t.parts)
@@ -1777,8 +1779,8 @@ function Ue(e) {
1777
1779
  }
1778
1780
  return s;
1779
1781
  }
1780
- function Se(e, s = [], t = []) {
1781
- const a = Ue(s), n = t.map((o) => {
1782
+ function Te(e, s = [], t = []) {
1783
+ const a = $e(s), n = t.map((o) => {
1782
1784
  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";
1783
1785
  return {
1784
1786
  type: "file",
@@ -1812,16 +1814,16 @@ function Tt(e) {
1812
1814
  (s) => s.type === "data" && "toolCallId" in s.data && "result" in s.data
1813
1815
  ) : [];
1814
1816
  }
1815
- function re(e, s) {
1817
+ function ne(e, s) {
1816
1818
  if (typeof localStorage > "u") {
1817
- B("localStorage not available, cannot update session ID");
1819
+ L("localStorage not available, cannot update session ID");
1818
1820
  return;
1819
1821
  }
1820
1822
  try {
1821
1823
  const t = localStorage.getItem(e);
1822
1824
  if (t) {
1823
1825
  const a = JSON.parse(t), n = a.sessionId;
1824
- a.sessionId = s, localStorage.setItem(e, JSON.stringify(a)), B(
1826
+ a.sessionId = s, localStorage.setItem(e, JSON.stringify(a)), L(
1825
1827
  "Updated localStorage[%s] session ID: %s -> %s",
1826
1828
  e,
1827
1829
  n,
@@ -1832,21 +1834,21 @@ function re(e, s) {
1832
1834
  sessionId: s,
1833
1835
  timestamp: Date.now()
1834
1836
  };
1835
- localStorage.setItem(e, JSON.stringify(a)), B(
1837
+ localStorage.setItem(e, JSON.stringify(a)), L(
1836
1838
  "Created new session in localStorage[%s]: %s",
1837
1839
  e,
1838
1840
  s
1839
1841
  );
1840
1842
  }
1841
1843
  } catch (t) {
1842
- B(
1844
+ L(
1843
1845
  "Failed to update localStorage sessionId to %s: %O",
1844
1846
  s,
1845
1847
  t
1846
1848
  );
1847
1849
  }
1848
1850
  }
1849
- async function St(e) {
1851
+ async function Mt(e) {
1850
1852
  const s = [];
1851
1853
  for (const t of e)
1852
1854
  if (t.parts && Array.isArray(t.parts))
@@ -1866,7 +1868,7 @@ async function St(e) {
1866
1868
  s.push(t);
1867
1869
  return ot(), s;
1868
1870
  }
1869
- function At() {
1871
+ function St() {
1870
1872
  const e = /* @__PURE__ */ new Map();
1871
1873
  async function s(t, a) {
1872
1874
  const n = e.get(t);
@@ -1878,7 +1880,7 @@ function At() {
1878
1880
  n.conversationStorageKey
1879
1881
  );
1880
1882
  } catch (o) {
1881
- B(
1883
+ L(
1882
1884
  `Failed to persist conversation history for agent ${t}:`,
1883
1885
  o
1884
1886
  );
@@ -1900,10 +1902,10 @@ function At() {
1900
1902
  r,
1901
1903
  i
1902
1904
  )).messages;
1903
- } catch (m) {
1904
- B(
1905
+ } catch (h) {
1906
+ L(
1905
1907
  `Failed to load conversation history for agent ${t} with session ${o}:`,
1906
- m
1908
+ h
1907
1909
  );
1908
1910
  }
1909
1911
  const u = {
@@ -1928,39 +1930,39 @@ function At() {
1928
1930
  return e.delete(t);
1929
1931
  },
1930
1932
  async sendMessage(t, a, n = {}) {
1931
- var I;
1933
+ var v;
1932
1934
  const o = e.get(t);
1933
1935
  if (!o)
1934
1936
  throw new Error(`Agent with key "${t}" not found`);
1935
- const { withHistory: r = !0, sessionId: l, ...i } = n, { client: d, conversationHistory: u } = o, m = n.message || Se(
1937
+ const { withHistory: r = !0, sessionId: l, ...i } = n, { client: d, conversationHistory: u } = o, h = n.message || Te(
1936
1938
  a,
1937
1939
  u,
1938
1940
  n.imageUrls
1939
- ), h = await d.sendMessage({
1940
- message: m,
1941
+ ), y = await d.sendMessage({
1942
+ message: h,
1941
1943
  withHistory: r,
1942
1944
  sessionId: l || o.sessionId || void 0,
1943
1945
  ...i
1944
1946
  });
1945
- if (h.sessionId) {
1947
+ if (y.sessionId) {
1946
1948
  const A = o.sessionId;
1947
- o.sessionId = h.sessionId, A && h.sessionId && A !== h.sessionId && o.sessionIdStorageKey && (B(
1949
+ o.sessionId = y.sessionId, A && y.sessionId && A !== y.sessionId && o.sessionIdStorageKey && (L(
1948
1950
  "Session ID changed from %s to %s, updating localStorage",
1949
1951
  A,
1950
- h.sessionId
1951
- ), re(
1952
+ y.sessionId
1953
+ ), ne(
1952
1954
  o.sessionIdStorageKey,
1953
- h.sessionId
1955
+ y.sessionId
1954
1956
  ));
1955
1957
  }
1956
- let f = null;
1957
- if ((I = h.status) != null && I.message) {
1958
- const A = h.status.message.parts.filter(
1958
+ let x = null;
1959
+ if ((v = y.status) != null && v.message) {
1960
+ const A = y.status.message.parts.filter(
1959
1961
  (c) => c.type === "data" && "toolCallId" in c.data && ("arguments" in c.data || "result" in c.data)
1960
- ), g = h.status.message.parts.filter(
1962
+ ), g = y.status.message.parts.filter(
1961
1963
  (c) => c.type === "text"
1962
1964
  );
1963
- f = {
1965
+ x = {
1964
1966
  role: "agent",
1965
1967
  kind: "message",
1966
1968
  parts: [...A, ...g],
@@ -1970,30 +1972,30 @@ function At() {
1970
1972
  }
1971
1973
  };
1972
1974
  }
1973
- const v = [
1975
+ const m = [
1974
1976
  ...u,
1975
1977
  // Store only the new content from the user message (without history parts)
1976
- le(a),
1978
+ ie(a),
1977
1979
  // Add complete agent response with tool calls/results if present
1978
- ...f ? [Q(f)] : []
1980
+ ...x ? [G(x)] : []
1979
1981
  ];
1980
- let y = v;
1981
- if (h.agentMessage) {
1982
- const A = Q(
1983
- h.agentMessage
1982
+ let f = m;
1983
+ if (y.agentMessage) {
1984
+ const A = G(
1985
+ y.agentMessage
1984
1986
  );
1985
- y = [
1986
- ...v,
1987
+ f = [
1988
+ ...m,
1987
1989
  A
1988
1990
  ];
1989
1991
  }
1990
- return o.conversationHistory = y, r && await s(
1992
+ return o.conversationHistory = f, r && await s(
1991
1993
  t,
1992
- y
1993
- ), h;
1994
+ f
1995
+ ), y;
1994
1996
  },
1995
1997
  async *sendMessageStream(t, a, n = {}) {
1996
- var w, k, T, _, O, q;
1998
+ var I, T, b, E, U, $;
1997
1999
  const o = e.get(t);
1998
2000
  if (!o)
1999
2001
  throw new Error(`Agent with key "${t}" not found`);
@@ -2003,25 +2005,25 @@ function At() {
2003
2005
  metadata: i,
2004
2006
  sessionId: d,
2005
2007
  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(
2008
+ ...h
2009
+ } = n, { client: y } = o, x = i ? (({ contentType: p, ...M }) => M)(i) : void 0, m = new AbortController();
2010
+ o.currentAbortController = m, l && l.addEventListener(
2009
2011
  "abort",
2010
- () => v.abort()
2012
+ () => m.abort()
2011
2013
  );
2012
- let y = [
2014
+ let f = [
2013
2015
  ...o.conversationHistory
2014
- ], I = [];
2015
- const A = await St(
2016
- y
2016
+ ], v = [];
2017
+ const A = await Mt(
2018
+ f
2017
2019
  );
2018
- o.conversationHistory = A, y = A, r && await s(
2020
+ o.conversationHistory = A, f = A, r && await s(
2019
2021
  t,
2020
2022
  A
2021
2023
  );
2022
2024
  let g;
2023
2025
  if (u) {
2024
- const p = Ue(
2026
+ const p = $e(
2025
2027
  A
2026
2028
  );
2027
2029
  g = {
@@ -2029,122 +2031,122 @@ function At() {
2029
2031
  parts: [...p, ...u.parts]
2030
2032
  };
2031
2033
  } else
2032
- g = Se(
2034
+ g = Te(
2033
2035
  a,
2034
2036
  A,
2035
2037
  n.imageUrls
2036
2038
  );
2037
2039
  if (n.metadata && !u) {
2038
- const { contentType: p, ...C } = n.metadata;
2040
+ const { contentType: p, ...M } = n.metadata;
2039
2041
  if (p) {
2040
- const S = g.parts[g.parts.length - 1];
2041
- S && S.type === "text" && (S.metadata = {
2042
- ...S.metadata,
2042
+ const w = g.parts[g.parts.length - 1];
2043
+ w && w.type === "text" && (w.metadata = {
2044
+ ...w.metadata,
2043
2045
  contentType: p
2044
2046
  });
2045
2047
  }
2046
- Object.keys(C).length > 0 && (g.metadata = {
2048
+ Object.keys(M).length > 0 && (g.metadata = {
2047
2049
  ...g.metadata,
2048
- ...C
2050
+ ...M
2049
2051
  });
2050
2052
  }
2051
- const c = u || le(a, n.metadata);
2053
+ const c = u || ie(a, n.metadata);
2052
2054
  if (n.imageUrls && n.imageUrls.length > 0) {
2053
2055
  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";
2056
+ (M) => {
2057
+ const w = typeof M == "string" ? M : M.url, k = typeof M == "string" ? void 0 : M.metadata, O = (k == null ? void 0 : k.fileType) || "image/jpeg";
2056
2058
  return {
2057
2059
  type: "file",
2058
2060
  file: {
2059
- name: (b == null ? void 0 : b.fileName) || "image",
2060
- mimeType: $,
2061
- uri: S
2061
+ name: (k == null ? void 0 : k.fileName) || "image",
2062
+ mimeType: O,
2063
+ uri: w
2062
2064
  },
2063
- metadata: b
2065
+ metadata: k
2064
2066
  };
2065
2067
  }
2066
2068
  );
2067
2069
  c.parts.push(...p);
2068
2070
  }
2069
- y = [
2070
- ...y,
2071
+ f = [
2072
+ ...f,
2071
2073
  c
2072
- ], o.conversationHistory = y, r && await s(
2074
+ ], o.conversationHistory = f, r && await s(
2073
2075
  t,
2074
- y
2076
+ f
2075
2077
  );
2076
- for await (const p of h.sendMessageStream({
2078
+ for await (const p of y.sendMessageStream({
2077
2079
  message: g,
2078
2080
  withHistory: r,
2079
2081
  sessionId: d || o.sessionId || void 0,
2080
- abortSignal: v.signal,
2081
- ...m,
2082
- ...f && Object.keys(f).length > 0 && {
2083
- metadata: f
2082
+ abortSignal: m.signal,
2083
+ ...h,
2084
+ ...x && Object.keys(x).length > 0 && {
2085
+ metadata: x
2084
2086
  }
2085
2087
  })) {
2086
2088
  if (p.sessionId) {
2087
- const C = o.sessionId;
2088
- o.sessionId = p.sessionId, p.sessionId && C !== p.sessionId && o.sessionIdStorageKey && (B(
2089
+ const M = o.sessionId;
2090
+ o.sessionId = p.sessionId, p.sessionId && M !== p.sessionId && o.sessionIdStorageKey && (L(
2089
2091
  "Session ID %s, updating localStorage",
2090
- C ? `changed from ${C} to ${p.sessionId}` : `received: ${p.sessionId}`
2091
- ), re(
2092
+ M ? `changed from ${M} to ${p.sessionId}` : `received: ${p.sessionId}`
2093
+ ), ne(
2092
2094
  o.sessionIdStorageKey,
2093
2095
  p.sessionId
2094
2096
  ));
2095
2097
  }
2096
- if (((w = p.status) == null ? void 0 : w.state) === "input-required" && ((k = p.status) != null && k.message)) {
2097
- I = W(
2098
+ if (((I = p.status) == null ? void 0 : I.state) === "input-required" && ((T = p.status) != null && T.message)) {
2099
+ v = B(
2098
2100
  p.status.message
2099
2101
  ).map(
2100
- (b) => b.data.toolCallId
2102
+ (k) => k.data.toolCallId
2101
2103
  );
2102
- const S = Q(
2104
+ const w = G(
2103
2105
  p.status.message
2104
2106
  );
2105
- y = [
2106
- ...y,
2107
- S
2108
- ], o.conversationHistory = y, r && await s(
2107
+ f = [
2108
+ ...f,
2109
+ w
2110
+ ], o.conversationHistory = f, r && await s(
2109
2111
  t,
2110
- y
2112
+ f
2111
2113
  );
2112
2114
  }
2113
- if (((T = p.status) == null ? void 0 : T.state) === "working" && ((_ = p.status) != null && _.message) && !p.final) {
2114
- const S = Tt(
2115
+ if (((b = p.status) == null ? void 0 : b.state) === "working" && ((E = p.status) != null && E.message) && !p.final) {
2116
+ const w = Tt(
2115
2117
  p.status.message
2116
2118
  ).filter(
2117
- (b) => I.includes(
2118
- b.data.toolCallId
2119
+ (k) => v.includes(
2120
+ k.data.toolCallId
2119
2121
  )
2120
2122
  );
2121
- if (S.length > 0) {
2122
- const b = {
2123
+ if (w.length > 0) {
2124
+ const k = {
2123
2125
  role: "agent",
2124
2126
  kind: "message",
2125
- parts: S,
2127
+ parts: w,
2126
2128
  messageId: N()
2127
2129
  };
2128
- y = [
2129
- ...y,
2130
- Q(b)
2131
- ], o.conversationHistory = y, r && await s(
2130
+ f = [
2131
+ ...f,
2132
+ G(k)
2133
+ ], o.conversationHistory = f, r && await s(
2132
2134
  t,
2133
- y
2135
+ f
2134
2136
  );
2135
2137
  }
2136
2138
  }
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(
2139
+ if (p.final && ((U = p.status) == null ? void 0 : U.state) !== "input-required") {
2140
+ v = [];
2141
+ let M = null;
2142
+ ($ = p.status) != null && $.message && (M = G(
2141
2143
  p.status.message
2142
- ), y = [
2143
- ...y,
2144
- C
2145
- ], o.conversationHistory = y, r && await s(
2144
+ ), f = [
2145
+ ...f,
2146
+ M
2147
+ ], o.conversationHistory = f, r && await s(
2146
2148
  t,
2147
- y
2149
+ f
2148
2150
  ));
2149
2151
  }
2150
2152
  yield p;
@@ -2182,7 +2184,7 @@ function At() {
2182
2184
  role: "user",
2183
2185
  kind: "message",
2184
2186
  parts: [
2185
- J(a, n, o)
2187
+ D(a, n, o)
2186
2188
  ],
2187
2189
  messageId: N()
2188
2190
  }
@@ -2213,7 +2215,7 @@ function At() {
2213
2215
  const n = e.get(t);
2214
2216
  if (!n)
2215
2217
  throw new Error(`Agent with key "${t}" not found`);
2216
- n.sessionId = a, n.sessionIdStorageKey && re(
2218
+ n.sessionId = a, n.sessionIdStorageKey && ne(
2217
2219
  n.sessionIdStorageKey,
2218
2220
  a
2219
2221
  );
@@ -2229,12 +2231,12 @@ function At() {
2229
2231
  }
2230
2232
  };
2231
2233
  }
2232
- const Mt = At();
2233
- function Y() {
2234
- return Mt;
2234
+ const vt = St();
2235
+ function X() {
2236
+ return vt;
2235
2237
  }
2236
- function vt() {
2237
- const [e, s] = ve([]), t = j(
2238
+ function At() {
2239
+ const [e, s] = ve([]), t = H(
2238
2240
  (o) => {
2239
2241
  s((r) => {
2240
2242
  const l = r.findIndex(
@@ -2248,9 +2250,9 @@ function vt() {
2248
2250
  });
2249
2251
  },
2250
2252
  []
2251
- ), a = j((o) => {
2253
+ ), a = H((o) => {
2252
2254
  s((r) => r.filter((l) => l.id !== o));
2253
- }, []), n = j(() => {
2255
+ }, []), n = H(() => {
2254
2256
  s([]);
2255
2257
  }, []);
2256
2258
  return {
@@ -2266,7 +2268,8 @@ function xt(e, s) {
2266
2268
  id: n.id,
2267
2269
  label: n.label,
2268
2270
  component: n.component,
2269
- componentProps: n.componentProps
2271
+ componentProps: n.componentProps,
2272
+ order: n.order
2270
2273
  } : {
2271
2274
  id: n.id,
2272
2275
  label: n.label,
@@ -2275,10 +2278,11 @@ function xt(e, s) {
2275
2278
  tooltip: n.tooltip,
2276
2279
  disabled: n.disabled || !1,
2277
2280
  pressed: n.pressed,
2278
- showLabel: n.showLabel
2279
- });
2281
+ showLabel: n.showLabel,
2282
+ order: n.order
2283
+ }).sort((n, o) => (n.order ?? 1 / 0) - (o.order ?? 1 / 0));
2280
2284
  }
2281
- const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), qe = (e, s = "40%") => ({
2285
+ const re = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ue = (e, s = "40%") => ({
2282
2286
  type: "component",
2283
2287
  component: () => je.createElement("img", {
2284
2288
  src: e,
@@ -2292,7 +2296,7 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), qe = (e, s =
2292
2296
  display: "inline-block"
2293
2297
  }
2294
2298
  })
2295
- }), ie = (e, s = []) => {
2299
+ }), oe = (e, s = []) => {
2296
2300
  var r, l;
2297
2301
  if (e.parts.some((i) => {
2298
2302
  if (i.type === "data") {
@@ -2312,7 +2316,7 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), qe = (e, s =
2312
2316
  if (i.type === "file") {
2313
2317
  const u = i.file.uri || (i.file.mimeType && i.file.bytes ? `data:${i.file.mimeType};base64,${i.file.bytes}` : void 0);
2314
2318
  if (u)
2315
- return qe(u);
2319
+ return Ue(u);
2316
2320
  }
2317
2321
  if (i.type === "data") {
2318
2322
  const u = i.data;
@@ -2360,7 +2364,7 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), qe = (e, s =
2360
2364
  success: !0,
2361
2365
  result: "No tools available"
2362
2366
  })
2363
- }), kt = (e) => ["agentId", "agentUrl"].every((t) => {
2367
+ }), Rt = (e) => ["agentId", "agentUrl"].every((t) => {
2364
2368
  const a = e[t];
2365
2369
  return typeof a == "string" && a.trim().length > 0;
2366
2370
  });
@@ -2369,9 +2373,8 @@ function Wt(e) {
2369
2373
  agentId: e.agentId,
2370
2374
  agentUrl: e.agentUrl,
2371
2375
  sessionId: e.sessionId,
2372
- sessionIdStorageKey: e.sessionIdStorageKey,
2373
- fetchCallback: e.fetchCallback
2374
- }, t = kt(s), [a, n] = ve({
2376
+ sessionIdStorageKey: e.sessionIdStorageKey
2377
+ }, t = Rt(s), [a, n] = ve({
2375
2378
  clientMessages: [],
2376
2379
  uiMessages: [],
2377
2380
  isProcessing: !1,
@@ -2384,33 +2387,33 @@ function Wt(e) {
2384
2387
  unregisterMessageActions: r,
2385
2388
  clearAllMessageActions: l,
2386
2389
  registrations: i
2387
- } = vt(), d = he(!1), u = he(i);
2388
- ae(() => {
2390
+ } = At(), d = pe(!1), u = pe(i);
2391
+ se(() => {
2389
2392
  u.current = i;
2390
2393
  }, [i]);
2391
- const m = j(
2394
+ const h = H(
2392
2395
  (g) => g.map(
2393
- (c) => ie(c, u.current)
2396
+ (c) => oe(c, u.current)
2394
2397
  ).filter((c) => c !== null),
2395
2398
  []
2396
2399
  // registrationsRef is stable, so no deps needed
2397
2400
  );
2398
- ae(() => {
2401
+ se(() => {
2399
2402
  if (!t)
2400
2403
  return;
2401
2404
  (async () => {
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
- ..._,
2405
+ const c = X(), I = s.agentId;
2406
+ if (c.hasAgent(I))
2407
+ s.sessionId ? (c.updateSessionId(I, s.sessionId), c.getConversationHistory(I).length === 0 && n((E) => ({
2408
+ ...E,
2406
2409
  clientMessages: [],
2407
2410
  uiMessages: []
2408
- }))) : (c.updateSessionId(w, ""), await c.replaceMessages(w, []), n((T) => ({
2409
- ...T,
2411
+ }))) : (c.updateSessionId(I, ""), await c.replaceMessages(I, []), n((b) => ({
2412
+ ...b,
2410
2413
  clientMessages: [],
2411
2414
  uiMessages: []
2412
2415
  })));
2413
- else if (await c.createAgent(w, {
2416
+ else if (await c.createAgent(I, {
2414
2417
  agentId: s.agentId,
2415
2418
  agentUrl: s.agentUrl,
2416
2419
  sessionId: s.sessionId,
@@ -2420,21 +2423,20 @@ function Wt(e) {
2420
2423
  toolProvider: e.toolProvider || bt(),
2421
2424
  authProvider: e.authProvider,
2422
2425
  enableStreaming: e.enableStreaming,
2423
- odieBotId: e.odieBotId,
2424
- fetchCallback: s.fetchCallback
2426
+ odieBotId: e.odieBotId
2425
2427
  }), s.sessionId) {
2426
- const T = c.getConversationHistory(w);
2427
- n((_) => {
2428
- const O = m(T);
2428
+ const b = c.getConversationHistory(I);
2429
+ n((E) => {
2430
+ const U = h(b);
2429
2431
  return {
2430
- ..._,
2431
- clientMessages: T,
2432
- uiMessages: O
2432
+ ...E,
2433
+ clientMessages: b,
2434
+ uiMessages: U
2433
2435
  };
2434
2436
  });
2435
2437
  } else
2436
- n((T) => ({
2437
- ...T,
2438
+ n((b) => ({
2439
+ ...b,
2438
2440
  clientMessages: [],
2439
2441
  uiMessages: []
2440
2442
  }));
@@ -2451,91 +2453,91 @@ function Wt(e) {
2451
2453
  e.odieBotId,
2452
2454
  t
2453
2455
  ]);
2454
- const h = j(
2456
+ const y = H(
2455
2457
  async (g, c) => {
2456
- var O, q;
2458
+ var U, $;
2457
2459
  if (!t)
2458
2460
  throw new Error("Invalid agent configuration");
2459
2461
  if (d.current)
2460
2462
  return;
2461
2463
  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
+ const I = (c == null ? void 0 : c.type) === "tool_result";
2465
+ if (I && (!(c != null && c.toolCallId) || !(c != null && c.toolId)))
2464
2466
  throw new Error(
2465
2467
  "`toolCallId` and `toolId` are required when type is `tool_result`"
2466
2468
  );
2467
- const k = Y(), T = s.agentId, _ = Date.now();
2468
- if (w)
2469
+ const T = X(), b = s.agentId, E = Date.now();
2470
+ if (I)
2469
2471
  n((p) => ({
2470
2472
  ...p,
2471
2473
  isProcessing: !0,
2472
2474
  error: null
2473
2475
  }));
2474
2476
  else {
2475
- const p = (c == null ? void 0 : c.type) || "text", C = {
2476
- id: `user-${_}`,
2477
+ const p = (c == null ? void 0 : c.type) || "text", M = {
2478
+ id: `user-${E}`,
2477
2479
  role: "user",
2478
2480
  content: [
2479
2481
  { type: p, text: g },
2480
2482
  // 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);
2483
+ ...((U = c == null ? void 0 : c.imageUrls) == null ? void 0 : U.map((w) => {
2484
+ const k = typeof w == "string" ? w : w.url;
2485
+ return Ue(k);
2484
2486
  })) ?? []
2485
2487
  ],
2486
- timestamp: _,
2488
+ timestamp: E,
2487
2489
  archived: (c == null ? void 0 : c.archived) ?? !1,
2488
2490
  showIcon: !1
2489
2491
  };
2490
- n((S) => ({
2491
- ...S,
2492
- uiMessages: [...S.uiMessages, C],
2492
+ n((w) => ({
2493
+ ...w,
2494
+ uiMessages: [...w.uiMessages, M],
2493
2495
  isProcessing: !0,
2494
2496
  error: null
2495
2497
  }));
2496
2498
  }
2497
2499
  try {
2498
- let p = null, C = !1;
2499
- const S = {}, b = !!(c != null && c.type) && !w;
2500
- (c != null && c.archived || b) && (S.metadata = {
2500
+ let p = null, M = !1;
2501
+ const w = {}, k = !!(c != null && c.type) && !I;
2502
+ (c != null && c.archived || k) && (w.metadata = {
2501
2503
  ...(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,
2504
+ ...k && { contentType: c.type }
2505
+ }), c != null && c.sessionId && (w.sessionId = c.sessionId), c != null && c.imageUrls && (w.imageUrls = c.imageUrls);
2506
+ const O = I ? T.sendToolResult(
2507
+ b,
2506
2508
  c.toolCallId,
2507
2509
  c.toolId,
2508
2510
  { success: !0, message: g },
2509
- S
2510
- ) : k.sendMessageStream(
2511
- T,
2511
+ w
2512
+ ) : T.sendMessageStream(
2513
+ b,
2512
2514
  g,
2513
- S
2515
+ w
2514
2516
  );
2515
- for await (const R of $) {
2516
- if ((R.progressMessage || R.progressPhase) && n((P) => ({
2517
- ...P,
2517
+ for await (const R of O) {
2518
+ if ((R.progressMessage || R.progressPhase) && n((C) => ({
2519
+ ...C,
2518
2520
  progressMessage: R.progressMessage || null,
2519
2521
  progressPhase: R.progressPhase || null
2520
2522
  })), !R.final && R.text)
2521
2523
  if (p)
2522
- n((P) => ({
2523
- ...P,
2524
- uiMessages: P.uiMessages.map(
2525
- (x) => x.id === p ? {
2526
- ...x,
2524
+ n((C) => ({
2525
+ ...C,
2526
+ uiMessages: C.uiMessages.map(
2527
+ (P) => P.id === p ? {
2528
+ ...P,
2527
2529
  content: [
2528
2530
  {
2529
2531
  type: "text",
2530
2532
  text: R.text
2531
2533
  }
2532
2534
  ]
2533
- } : x
2535
+ } : P
2534
2536
  )
2535
2537
  }));
2536
2538
  else {
2537
2539
  p = `agent-streaming-${Date.now()}`;
2538
- const P = {
2540
+ const C = {
2539
2541
  id: p,
2540
2542
  role: "agent",
2541
2543
  content: [
@@ -2548,42 +2550,42 @@ function Wt(e) {
2548
2550
  reactKey: p
2549
2551
  // Stable key for React rendering
2550
2552
  };
2551
- n((x) => ({
2552
- ...x,
2553
+ n((P) => ({
2554
+ ...P,
2553
2555
  uiMessages: [
2554
- ...x.uiMessages,
2555
- P
2556
+ ...P.uiMessages,
2557
+ C
2556
2558
  ]
2557
2559
  }));
2558
2560
  }
2559
- if (R.final && ((q = R.status) != null && q.message) && p) {
2560
- C = !0;
2561
- const P = p, x = ie(
2561
+ if (R.final && (($ = R.status) != null && $.message) && p) {
2562
+ M = !0;
2563
+ const C = p, P = oe(
2562
2564
  R.status.message,
2563
2565
  u.current
2564
2566
  );
2565
- x && n((U) => {
2566
- const K = U.uiMessages.map(
2567
- (H) => {
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;
2567
+ P && n((j) => {
2568
+ const J = j.uiMessages.map(
2569
+ (q) => {
2570
+ var _, V;
2571
+ if (q.id === C) {
2572
+ const qe = P.content.length > 0 && ((_ = P.content[0]) == null ? void 0 : _.text) && ((V = q.content[0]) == null ? void 0 : V.text) && P.content[0].text.length > q.content[0].text.length;
2571
2573
  return {
2572
- ...x,
2573
- reactKey: H.reactKey || P,
2574
+ ...P,
2575
+ reactKey: q.reactKey || C,
2574
2576
  // Keep stable reactKey
2575
- content: G ? x.content : H.content
2577
+ content: qe ? P.content : q.content
2576
2578
  };
2577
2579
  }
2578
- return H;
2580
+ return q;
2579
2581
  }
2580
- ), L = k.getConversationHistory(
2581
- T
2582
+ ), K = T.getConversationHistory(
2583
+ b
2582
2584
  );
2583
2585
  return {
2584
- ...U,
2585
- clientMessages: L,
2586
- uiMessages: K,
2586
+ ...j,
2587
+ clientMessages: K,
2588
+ uiMessages: J,
2587
2589
  isProcessing: !1,
2588
2590
  progressMessage: null,
2589
2591
  progressPhase: null
@@ -2591,32 +2593,32 @@ function Wt(e) {
2591
2593
  }), p = null;
2592
2594
  }
2593
2595
  }
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
2596
+ if (!M) {
2597
+ const R = T.getConversationHistory(b);
2598
+ n((C) => {
2599
+ let P = C.uiMessages;
2600
+ p && (P = C.uiMessages.filter(
2601
+ (_) => _.id !== p
2600
2602
  ));
2601
- const U = R.map(
2602
- (E) => ie(
2603
- E,
2603
+ const j = R.map(
2604
+ (_) => oe(
2605
+ _,
2604
2606
  u.current
2605
2607
  )
2606
2608
  ).filter(
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
2609
+ (_) => _ !== null
2610
+ ), J = new Set(
2611
+ R.map((_) => _.messageId)
2612
+ ), K = P.filter(
2613
+ (_) => !J.has(_.id) && _.role !== "user"
2614
+ ), q = re([
2615
+ ...j,
2616
+ ...K
2615
2617
  ]);
2616
2618
  return {
2617
- ...P,
2619
+ ...C,
2618
2620
  clientMessages: R,
2619
- uiMessages: H,
2621
+ uiMessages: q,
2620
2622
  isProcessing: !1,
2621
2623
  progressMessage: null,
2622
2624
  progressPhase: null
@@ -2625,8 +2627,8 @@ function Wt(e) {
2625
2627
  }
2626
2628
  } catch (p) {
2627
2629
  if (p instanceof Error && p.name === "AbortError") {
2628
- M("Request was aborted by user"), n((S) => ({
2629
- ...S,
2630
+ S("Request was aborted by user"), n((w) => ({
2631
+ ...w,
2630
2632
  isProcessing: !1,
2631
2633
  progressMessage: null,
2632
2634
  progressPhase: null,
@@ -2635,69 +2637,69 @@ function Wt(e) {
2635
2637
  }));
2636
2638
  return;
2637
2639
  }
2638
- const C = p instanceof Error ? p.message : "Failed to send message";
2639
- throw n((S) => ({
2640
- ...S,
2640
+ const M = p instanceof Error ? p.message : "Failed to send message";
2641
+ throw n((w) => ({
2642
+ ...w,
2641
2643
  isProcessing: !1,
2642
2644
  progressMessage: null,
2643
2645
  progressPhase: null,
2644
- error: C
2646
+ error: M
2645
2647
  })), p;
2646
2648
  } finally {
2647
2649
  d.current = !1;
2648
2650
  }
2649
2651
  },
2650
2652
  [s.agentId, t]
2651
- ), f = j((g) => {
2653
+ ), x = H((g) => {
2652
2654
  n((c) => ({
2653
2655
  ...c,
2654
- uiMessages: oe([...c.uiMessages, g])
2656
+ uiMessages: re([...c.uiMessages, g])
2655
2657
  }));
2656
- }, []), v = j((g) => {
2658
+ }, []), m = H((g) => {
2657
2659
  n((c) => ({
2658
2660
  ...c,
2659
2661
  suggestions: g
2660
2662
  }));
2661
- }, []), y = j(() => {
2663
+ }, []), f = H(() => {
2662
2664
  n((g) => ({
2663
2665
  ...g,
2664
2666
  suggestions: []
2665
2667
  }));
2666
2668
  }, []);
2667
- ae(() => {
2669
+ se(() => {
2668
2670
  n((g) => {
2669
2671
  if (g.clientMessages.length === 0)
2670
2672
  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"
2673
+ const c = h(g.clientMessages), I = new Set(
2674
+ g.clientMessages.map((b) => b.messageId)
2675
+ ), T = g.uiMessages.filter(
2676
+ (b) => !I.has(b.id) && b.role !== "user"
2675
2677
  );
2676
2678
  return {
2677
2679
  ...g,
2678
- uiMessages: oe([
2680
+ uiMessages: re([
2679
2681
  ...c,
2680
- ...k
2682
+ ...T
2681
2683
  ])
2682
2684
  };
2683
2685
  });
2684
2686
  }, [i]);
2685
- const I = j(() => {
2687
+ const v = H(() => {
2686
2688
  if (!t)
2687
2689
  return;
2688
- const g = Y(), c = s.agentId;
2690
+ const g = X(), c = s.agentId;
2689
2691
  g.abortCurrentRequest(c);
2690
- }, [s.agentId, t]), A = j(
2692
+ }, [s.agentId, t]), A = H(
2691
2693
  async (g) => {
2692
2694
  if (!t)
2693
2695
  return;
2694
- const c = Y(), w = s.agentId;
2695
- await c.replaceMessages(w, g);
2696
- const k = m(g);
2697
- n((T) => ({
2698
- ...T,
2696
+ const c = X(), I = s.agentId;
2697
+ await c.replaceMessages(I, g);
2698
+ const T = h(g);
2699
+ n((b) => ({
2700
+ ...b,
2699
2701
  clientMessages: g,
2700
- uiMessages: k
2702
+ uiMessages: T
2701
2703
  }));
2702
2704
  },
2703
2705
  [s.agentId, t]
@@ -2707,40 +2709,40 @@ function Wt(e) {
2707
2709
  messages: a.uiMessages,
2708
2710
  isProcessing: a.isProcessing,
2709
2711
  error: a.error,
2710
- onSubmit: h,
2712
+ onSubmit: y,
2711
2713
  suggestions: a.suggestions,
2712
2714
  progressMessage: a.progressMessage,
2713
2715
  progressPhase: a.progressPhase,
2714
2716
  // UI management methods
2715
- registerSuggestions: v,
2716
- clearSuggestions: y,
2717
+ registerSuggestions: m,
2718
+ clearSuggestions: f,
2717
2719
  // Message actions methods
2718
2720
  registerMessageActions: o,
2719
2721
  unregisterMessageActions: r,
2720
2722
  clearAllMessageActions: l,
2721
2723
  // Tool integration
2722
- addMessage: f,
2724
+ addMessage: x,
2723
2725
  // Abort control
2724
- abortCurrentRequest: I,
2726
+ abortCurrentRequest: v,
2725
2727
  // Conversation loading
2726
2728
  loadMessages: A
2727
2729
  };
2728
2730
  }
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;
2731
+ var kt = /* @__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))(kt || {});
2732
+ const Me = "jetpack-ai-jwt-token", Pt = 30 * 60 * 1e3;
2731
2733
  function _t() {
2732
2734
  var t, a, n;
2733
2735
  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);
2734
2736
  }
2735
2737
  async function Et(e, s = !0) {
2736
2738
  var i, d;
2737
- const t = localStorage.getItem(Ae);
2739
+ const t = localStorage.getItem(Me);
2738
2740
  let a;
2739
2741
  if (t)
2740
2742
  try {
2741
2743
  a = JSON.parse(t);
2742
2744
  } catch (u) {
2743
- M("Invalid cached Jetpack token: %O", u);
2745
+ S("Invalid cached Jetpack token: %O", u);
2744
2746
  }
2745
2747
  if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() && s)
2746
2748
  return a;
@@ -2750,10 +2752,10 @@ async function Et(e, s = !0) {
2750
2752
  blog_id: ""
2751
2753
  };
2752
2754
  try {
2753
- _t() ? r = await ye({
2755
+ _t() ? r = await he({
2754
2756
  path: "/wpcom/v2/sites/" + o + "/jetpack-openai-query/jwt",
2755
2757
  method: "POST"
2756
- }) : r = await ye({
2758
+ }) : r = await he({
2757
2759
  path: "/jetpack/v4/jetpack-ai-jwt?_cacheBuster=" + Date.now(),
2758
2760
  credentials: "same-origin",
2759
2761
  headers: {
@@ -2762,7 +2764,7 @@ async function Et(e, s = !0) {
2762
2764
  method: "POST"
2763
2765
  });
2764
2766
  } catch (u) {
2765
- throw M("Failed to fetch Jetpack token: %O", u), new Error(e(u));
2767
+ throw S("Failed to fetch Jetpack token: %O", u), new Error(e(u));
2766
2768
  }
2767
2769
  if (!(r != null && r.token))
2768
2770
  throw new Error(
@@ -2774,9 +2776,9 @@ async function Et(e, s = !0) {
2774
2776
  expire: Date.now() + Pt
2775
2777
  };
2776
2778
  try {
2777
- localStorage.setItem(Ae, JSON.stringify(l));
2779
+ localStorage.setItem(Me, JSON.stringify(l));
2778
2780
  } catch (u) {
2779
- M("Error storing token in localStorage: %O", u);
2781
+ S("Error storing token in localStorage: %O", u);
2780
2782
  }
2781
2783
  return l;
2782
2784
  }
@@ -2786,7 +2788,7 @@ const Vt = (e) => async () => {
2786
2788
  const t = await Et(e);
2787
2789
  t != null && t.token && (s.Authorization = `${t.token}`);
2788
2790
  } catch (t) {
2789
- throw M("Failed to get Jetpack token for auth: %O", t), t;
2791
+ throw S("Failed to get Jetpack token for auth: %O", t), t;
2790
2792
  }
2791
2793
  return s;
2792
2794
  };
@@ -2843,7 +2845,7 @@ function Xt(e) {
2843
2845
  }
2844
2846
  }
2845
2847
  export {
2846
- Rt as ErrorCodes,
2848
+ kt as ErrorCodes,
2847
2849
  zt as convertAbilitiesToTools,
2848
2850
  Ot as convertAbilityToTool,
2849
2851
  Kt as createAbortController,
@@ -2852,20 +2854,20 @@ export {
2852
2854
  Qt as createOdieBotId,
2853
2855
  De as createRequestId,
2854
2856
  Je as createTaskId,
2855
- le as createTextMessage,
2856
- D as extractTextFromMessage,
2857
- W as extractToolCallsFromMessage,
2858
- Y as getAgentManager,
2857
+ ie as createTextMessage,
2858
+ F as extractTextFromMessage,
2859
+ B as extractToolCallsFromMessage,
2860
+ X as getAgentManager,
2859
2861
  Xt as isOdieBotId,
2860
2862
  Gt as isWordPressAbility,
2861
2863
  Lt as listConversationsFromServer,
2862
2864
  Bt as loadAllMessagesFromServer,
2863
- ce as loadChatFromServer,
2865
+ le as loadChatFromServer,
2864
2866
  Nt as parseOdieBotId,
2865
2867
  Wt as useAgentChat,
2866
2868
  Dt as useClientAbilities,
2867
2869
  Ft as useClientContext,
2868
2870
  jt as useClientTools,
2869
2871
  Jt as useClientToolsWithAbilities,
2870
- vt as useMessageActions
2872
+ At as useMessageActions
2871
2873
  };