@automattic/agenttic-client 0.1.19 → 0.1.21

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,9 +1,9 @@
1
1
  var Rt = Object.defineProperty;
2
2
  var Et = (t, e, s) => e in t ? Rt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
3
3
  var Z = (t, e, s) => Et(t, typeof e != "symbol" ? e + "" : e, s);
4
- import { useMemo as ht, useCallback as $, useState as yt, useRef as Ot, useEffect as tt } from "react";
4
+ import { useMemo as pt, useCallback as $, useState as yt, useRef as Ot, useEffect as tt } from "react";
5
5
  import ut from "@wordpress/api-fetch";
6
- const O = (t, ...e) => {
6
+ const R = (t, ...e) => {
7
7
  _t() && console.log(`[agenttic-client] ${t}`, ...e);
8
8
  };
9
9
  function _t() {
@@ -16,13 +16,13 @@ function wt(t) {
16
16
  function dt(t, ...e) {
17
17
  console.log(`[agenttic-client] ${t}`, ...e);
18
18
  }
19
- function Se(t) {
20
- return ht(() => t ? {
19
+ function Ae(t) {
20
+ return pt(() => t ? {
21
21
  getClientContext: () => {
22
22
  try {
23
23
  return t() || {};
24
24
  } catch (n) {
25
- return O("Error getting client context: %O", n), {};
25
+ return R("Error getting client context: %O", n), {};
26
26
  }
27
27
  }
28
28
  } : void 0, [t]);
@@ -30,7 +30,7 @@ function Se(t) {
30
30
  function ot({
31
31
  getClientTools: t,
32
32
  executeTool: e,
33
- abilities: s
33
+ getAbilities: s
34
34
  }) {
35
35
  const n = $(async () => {
36
36
  const o = [];
@@ -39,7 +39,7 @@ function ot({
39
39
  const c = await t();
40
40
  o.push(...c);
41
41
  } catch (c) {
42
- O("Error getting available tools: %O", c);
42
+ R("Error getting available tools: %O", c);
43
43
  }
44
44
  return o;
45
45
  }, [t]), a = $(
@@ -48,40 +48,44 @@ function ot({
48
48
  throw new Error("No executeTool callback provided");
49
49
  try {
50
50
  return await e(o, c);
51
- } catch (l) {
52
- throw O("Error executing tool %s: %O", o, l), l;
51
+ } catch (i) {
52
+ throw R("Error executing tool %s: %O", o, i), i;
53
53
  }
54
54
  },
55
55
  [e]
56
56
  );
57
- return ht(() => {
58
- if (!(!t && (!s || s.length === 0)))
59
- return {
60
- getAvailableTools: n,
61
- executeTool: a,
62
- abilities: s
63
- };
64
- }, [n, a, t, s]);
57
+ return pt(() => {
58
+ if (!t && !s)
59
+ return;
60
+ const o = {};
61
+ return t && (o.getAvailableTools = n), e && (o.executeTool = a), s && (o.getAbilities = s), o;
62
+ }, [
63
+ n,
64
+ a,
65
+ t,
66
+ e,
67
+ s
68
+ ]);
65
69
  }
66
- function ve(t, e) {
70
+ function Se(t, e) {
67
71
  return ot({
68
72
  getClientTools: t,
69
73
  executeTool: e,
70
- abilities: void 0
74
+ getAbilities: void 0
71
75
  });
72
76
  }
73
- function Ae(t) {
77
+ function ve(t) {
74
78
  return ot({
75
79
  getClientTools: void 0,
76
80
  executeTool: void 0,
77
- abilities: t
81
+ getAbilities: t
78
82
  });
79
83
  }
80
84
  function ke(t) {
81
- const { getClientTools: e, executeTool: s, abilities: n } = t;
82
- if (!e && (!n || n.length === 0))
85
+ const { getClientTools: e, executeTool: s, getAbilities: n } = t;
86
+ if (!e && !n)
83
87
  throw new Error(
84
- "At least one of getClientTools or abilities must be provided to useClientToolsWithAbilities."
88
+ "At least one of getClientTools or getAbilities must be provided to useClientToolsWithAbilities."
85
89
  );
86
90
  if (e && !s)
87
91
  throw new Error(
@@ -90,7 +94,7 @@ function ke(t) {
90
94
  return ot({
91
95
  getClientTools: e,
92
96
  executeTool: s,
93
- abilities: n
97
+ getAbilities: n
94
98
  });
95
99
  }
96
100
  function rt() {
@@ -237,23 +241,27 @@ async function Ft(t, e) {
237
241
  if (!e)
238
242
  return t;
239
243
  try {
240
- const s = await e.getAvailableTools(), n = [];
241
- if (s.length > 0) {
242
- const a = s.map(Ht);
243
- n.push(...a);
244
+ const s = [];
245
+ if (e.getAvailableTools) {
246
+ const n = await e.getAvailableTools();
247
+ if (n.length > 0) {
248
+ const a = n.map(Ht);
249
+ s.push(...a);
250
+ }
244
251
  }
245
- if (e.abilities && e.abilities.length > 0) {
246
- const a = e.abilities.map(
247
- $t
248
- );
249
- n.push(...a);
252
+ if (e.getAbilities) {
253
+ const n = await e.getAbilities();
254
+ if (n.length > 0) {
255
+ const a = n.map($t);
256
+ s.push(...a);
257
+ }
250
258
  }
251
- return n.length === 0 ? t : {
259
+ return s.length === 0 ? t : {
252
260
  ...t,
253
- parts: [...t.parts, ...n]
261
+ parts: [...t.parts, ...s]
254
262
  };
255
263
  } catch (s) {
256
- return O("Warning: Failed to get tools: %s", s), t;
264
+ return R("Warning: Failed to get tools: %s", s), t;
257
265
  }
258
266
  }
259
267
  function Ut(t, e) {
@@ -269,7 +277,7 @@ function Ut(t, e) {
269
277
  parts: [...t.parts, n]
270
278
  };
271
279
  } catch (s) {
272
- return O("Warning: Failed to get context: %s", s), t;
280
+ return R("Warning: Failed to get context: %s", s), t;
273
281
  }
274
282
  }
275
283
  async function jt(t, e, s) {
@@ -288,11 +296,11 @@ function Jt(t, e = "") {
288
296
  const s = [], n = e + t;
289
297
  let a = "", r = 0, o = 0;
290
298
  for (; o < n.length; ) {
291
- const l = n.indexOf(
299
+ const i = n.indexOf(
292
300
  `
293
301
  `,
294
302
  o
295
- ), f = l === -1 ? n.substring(o) : n.substring(o, l);
303
+ ), f = i === -1 ? n.substring(o) : n.substring(o, i);
296
304
  if (f.startsWith("data:"))
297
305
  a !== "" && (a += `
298
306
  `), a += f.substring(
@@ -300,29 +308,29 @@ function Jt(t, e = "") {
300
308
  );
301
309
  else if (f.trim() === "" && a) {
302
310
  try {
303
- s.push(JSON.parse(a)), r = l === -1 ? n.length : l + 1;
311
+ s.push(JSON.parse(a)), r = i === -1 ? n.length : i + 1;
304
312
  } catch (u) {
305
- O("Failed to parse SSE event: %o", u), O("Problematic payload: %s", a);
313
+ R("Failed to parse SSE event: %o", u), R("Problematic payload: %s", a);
306
314
  }
307
315
  a = "";
308
316
  }
309
- l === -1 ? o = n.length : o = l + 1;
317
+ i === -1 ? o = n.length : o = i + 1;
310
318
  }
311
319
  const c = n.substring(r);
312
320
  return { events: s, nextBuffer: c };
313
321
  }
314
322
  async function* Kt(t, e = {}) {
315
- var f, u, h;
323
+ var f, u, p;
316
324
  const { supportDeltas: s = !1 } = e, n = t.getReader(), a = new TextDecoder();
317
325
  let r = "";
318
326
  const o = new Lt();
319
- let c = null, l = null;
327
+ let c = null, i = null;
320
328
  try {
321
329
  for (; ; ) {
322
- const { done: g, value: p } = await n.read();
330
+ const { done: g, value: h } = await n.read();
323
331
  if (g)
324
332
  break;
325
- const b = a.decode(p, { stream: !0 }), { events: m, nextBuffer: i } = Jt(b, r);
333
+ const M = a.decode(h, { stream: !0 }), { events: m, nextBuffer: l } = Jt(M, r);
326
334
  if (m && Array.isArray(m))
327
335
  for (let y = 0; y < m.length; y++) {
328
336
  const d = m[y];
@@ -350,9 +358,9 @@ async function* Kt(t, e = {}) {
350
358
  };
351
359
  }
352
360
  } catch (k) {
353
- O("Failed to process delta: %o", k);
361
+ R("Failed to process delta: %o", k);
354
362
  }
355
- } else d.result && d.result.status ? (c = d.result.id, l = d.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset(), yield {
363
+ } else d.result && d.result.status ? (c = d.result.id, i = d.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset(), yield {
356
364
  id: d.result.id,
357
365
  status: d.result.status,
358
366
  final: d.result.status.state === "completed" || d.result.status.state === "failed" || d.result.status.state === "canceled",
@@ -367,14 +375,14 @@ async function* Kt(t, e = {}) {
367
375
  status: d.result.status,
368
376
  final: d.result.status.state === "completed" || d.result.status.state === "failed" || d.result.status.state === "canceled",
369
377
  text: D(
370
- ((h = d.result.status) == null ? void 0 : h.message) || {
378
+ ((p = d.result.status) == null ? void 0 : p.message) || {
371
379
  role: "agent",
372
380
  parts: []
373
381
  }
374
382
  )
375
383
  }));
376
384
  }
377
- r = i;
385
+ r = l;
378
386
  }
379
387
  } finally {
380
388
  n.releaseLock();
@@ -469,7 +477,7 @@ class Lt {
469
477
  }
470
478
  }
471
479
  function Tt(t, e, s = "request") {
472
- throw clearTimeout(e), O("%s failed with error: %O", s, t), t instanceof Error && (O("Error message: %s", t.message), O("Error stack: %s", t.stack)), t;
480
+ throw clearTimeout(e), R("%s failed with error: %O", s, t), t instanceof Error && (R("Error message: %s", t.message), R("Error stack: %s", t.stack)), t;
473
481
  }
474
482
  function Ct(t, e = "request") {
475
483
  if (!t.ok)
@@ -497,7 +505,7 @@ function Vt(t, e) {
497
505
  return `${t}/${e}`;
498
506
  }
499
507
  function zt(t, e, s, n) {
500
- O("Request: %s %s", t, e), O("Headers: %o", s), n && O("Body: %s", wt(n));
508
+ R("Request: %s %s", t, e), R("Headers: %o", s), n && R("Body: %s", wt(n));
501
509
  }
502
510
  async function Gt(t, e = !1) {
503
511
  const s = {
@@ -509,7 +517,7 @@ async function Gt(t, e = !1) {
509
517
  }
510
518
  return s;
511
519
  }
512
- function Mt(t, e) {
520
+ function bt(t, e) {
513
521
  if (!e)
514
522
  return t;
515
523
  const s = new AbortController(), n = (a) => {
@@ -521,7 +529,7 @@ function Mt(t, e) {
521
529
  once: !0
522
530
  }), s.signal;
523
531
  }
524
- function bt(t, e, s) {
532
+ function Mt(t, e, s) {
525
533
  return {
526
534
  method: "POST",
527
535
  headers: t,
@@ -530,74 +538,74 @@ function bt(t, e, s) {
530
538
  };
531
539
  }
532
540
  async function Q(t, e, s, n, a, r) {
533
- const { message: o, sessionId: c, taskId: l, metadata: f } = t, { agentId: u, agentUrl: h, authProvider: g, proxy: p } = e, { isStreaming: b = !1, enableTokenStreaming: m = !1 } = s, i = c || r, y = Vt(h, u), d = await jt(
541
+ const { message: o, sessionId: c, taskId: i, metadata: f } = t, { agentId: u, agentUrl: p, authProvider: g, proxy: h } = e, { isStreaming: M = !1, enableTokenStreaming: m = !1 } = s, l = c || r, y = Vt(p, u), d = await jt(
534
542
  o,
535
543
  n,
536
544
  a
537
545
  ), I = qt(
538
546
  {
539
- id: l,
540
- sessionId: i,
547
+ id: i,
548
+ sessionId: l,
541
549
  message: d,
542
550
  metadata: f
543
551
  },
544
- b ? "message/stream" : "message/send",
545
- m && b
552
+ M ? "message/stream" : "message/send",
553
+ m && M
546
554
  // Only enable token streaming if using SSE
547
- ), k = await Gt(g, b);
555
+ ), k = await Gt(g, M);
548
556
  return zt("POST", y, k, I), {
549
557
  request: I,
550
558
  headers: k,
551
559
  enhancedMessage: d,
552
- effectiveSessionId: i,
560
+ effectiveSessionId: l,
553
561
  fullAgentUrl: y
554
562
  };
555
563
  }
556
- async function St(t, e, s = {}) {
557
- const { request: n, headers: a, fullAgentUrl: r } = t, { timeout: o } = e, { abortSignal: c } = s, { timeoutId: l, controller: f } = xt(
564
+ async function At(t, e, s = {}) {
565
+ const { request: n, headers: a, fullAgentUrl: r } = t, { timeout: o } = e, { abortSignal: c } = s, { timeoutId: i, controller: f } = xt(
558
566
  o,
559
567
  "request"
560
- ), u = c ? Mt(f.signal, c) : f.signal;
568
+ ), u = c ? bt(f.signal, c) : f.signal;
561
569
  try {
562
- const h = bt(
570
+ const p = Mt(
563
571
  a,
564
572
  JSON.stringify(n),
565
573
  u
566
574
  );
567
- O("Making request to %s with options: %O", r, {
568
- method: h.method,
569
- headers: h.headers
575
+ R("Making request to %s with options: %O", r, {
576
+ method: p.method,
577
+ headers: p.headers
570
578
  });
571
- const g = await fetch(r, h);
572
- clearTimeout(l), Ct(g, "request");
573
- const p = await g.json();
574
- return O(
579
+ const g = await fetch(r, p);
580
+ clearTimeout(i), Ct(g, "request");
581
+ const h = await g.json();
582
+ return R(
575
583
  "Response from %s: %d %O",
576
584
  r,
577
585
  g.status,
578
- wt(p)
579
- ), Wt(p, "request");
580
- } catch (h) {
581
- Tt(h, l, "request");
586
+ wt(h)
587
+ ), Wt(h, "request");
588
+ } catch (p) {
589
+ Tt(p, i, "request");
582
590
  }
583
591
  }
584
- async function* vt(t, e, s) {
592
+ async function* St(t, e, s) {
585
593
  const { request: n, headers: a, fullAgentUrl: r } = t, {
586
594
  streamingTimeout: o = 6e4,
587
595
  abortSignal: c,
588
- enableTokenStreaming: l = !1
596
+ enableTokenStreaming: i = !1
589
597
  } = s, { timeoutId: f, controller: u } = xt(
590
598
  o,
591
599
  "streaming request"
592
- ), h = c ? Mt(u.signal, c) : u.signal;
600
+ ), p = c ? bt(u.signal, c) : u.signal;
593
601
  try {
594
- const g = JSON.stringify(n), p = bt(a, g, h), b = await fetch(r, p);
595
- if (clearTimeout(f), Bt(b, "streaming request"), !b.body)
602
+ const g = JSON.stringify(n), h = Mt(a, g, p), M = await fetch(r, h);
603
+ if (clearTimeout(f), Bt(M, "streaming request"), !M.body)
596
604
  throw new Error(
597
605
  "Response body is null - server may not support streaming"
598
606
  );
599
- const m = l && n.tokenStreaming === !0;
600
- yield* Kt(b.body, {
607
+ const m = i && n.tokenStreaming === !0;
608
+ yield* Kt(M.body, {
601
609
  supportDeltas: m
602
610
  });
603
611
  } catch (g) {
@@ -606,31 +614,62 @@ async function* vt(t, e, s) {
606
614
  }
607
615
  const Qt = 12e4;
608
616
  async function X(t, e, s, n, a) {
609
- if (t.abilities && t.abilities.length > 0)
610
- for (const r of t.abilities) {
611
- const o = r.name.replace(/\//g, "__").replace(/-/g, "_");
612
- if ((e === o || e === r.name) && r.callback)
613
- try {
614
- return { result: await r.callback(s), returnToAgent: !0 };
615
- } catch (c) {
616
- return O(
617
- "Error executing ability %s: %O",
618
- r.name,
619
- c
620
- ), {
621
- result: {
622
- error: c instanceof Error ? c.message : String(c),
623
- success: !1
624
- },
625
- returnToAgent: !0
626
- };
617
+ if (t.getAbilities) {
618
+ const r = await t.getAbilities();
619
+ if (r.length > 0)
620
+ for (const o of r) {
621
+ const c = o.name.replace(/\//g, "__").replace(/-/g, "_");
622
+ if (e === c || e === o.name) {
623
+ if (o.callback)
624
+ try {
625
+ return { result: await o.callback(s), returnToAgent: !0 };
626
+ } catch (i) {
627
+ return R(
628
+ "Error executing ability %s: %O",
629
+ o.name,
630
+ i
631
+ ), {
632
+ result: {
633
+ error: i instanceof Error ? i.message : String(i),
634
+ success: !1
635
+ },
636
+ returnToAgent: !0
637
+ };
638
+ }
639
+ if (!o.callback && t.executeAbility)
640
+ try {
641
+ return { result: await t.executeAbility(
642
+ o.name,
643
+ s
644
+ ), returnToAgent: !0 };
645
+ } catch (i) {
646
+ return R(
647
+ "Error executing ability %s: %O",
648
+ o.name,
649
+ i
650
+ ), {
651
+ result: {
652
+ error: i instanceof Error ? i.message : String(i),
653
+ success: !1
654
+ },
655
+ returnToAgent: !0
656
+ };
657
+ }
658
+ throw new Error(
659
+ `Ability ${o.name} has no callback and no handler`
660
+ );
627
661
  }
628
- }
629
- return await t.executeTool(
630
- e,
631
- s,
632
- n,
633
- a
662
+ }
663
+ }
664
+ if (t.executeTool)
665
+ return await t.executeTool(
666
+ e,
667
+ s,
668
+ n,
669
+ a
670
+ );
671
+ throw new Error(
672
+ `No handler found for tool: ${e}. Tool provider must implement executeTool for non-ability tools.`
634
673
  );
635
674
  }
636
675
  const lt = /* @__PURE__ */ new Map();
@@ -674,31 +713,31 @@ function Zt(t) {
674
713
  return e;
675
714
  });
676
715
  }
677
- async function At(t, e, s) {
716
+ async function vt(t, e, s) {
678
717
  const n = [], a = [];
679
718
  let r = !1;
680
719
  for (const o of t) {
681
- const { toolCallId: c, toolId: l, arguments: f } = o.data;
720
+ const { toolCallId: c, toolId: i, arguments: f } = o.data;
682
721
  try {
683
722
  const u = await X(
684
723
  e,
685
- l,
724
+ i,
686
725
  f,
687
726
  s,
688
727
  c
689
- ), { result: h, returnToAgent: g, agentMessage: p } = it(u);
690
- g && (r = !0), p && a.push(W(p)), n.push(
728
+ ), { result: p, returnToAgent: g, agentMessage: h } = it(u);
729
+ g && (r = !0), h && a.push(W(h)), n.push(
691
730
  j(
692
731
  c,
693
- l,
694
- h
732
+ i,
733
+ p
695
734
  )
696
735
  );
697
736
  } catch (u) {
698
737
  r = !0, n.push(
699
738
  j(
700
739
  c,
701
- l,
740
+ i,
702
741
  void 0,
703
742
  u instanceof Error ? u.message : String(u)
704
743
  )
@@ -721,7 +760,7 @@ function gt(t) {
721
760
  return e;
722
761
  }
723
762
  async function et(t, e, s, n, a, r, o) {
724
- const l = await Q(
763
+ const i = await Q(
725
764
  {
726
765
  message: e,
727
766
  taskId: t,
@@ -734,17 +773,17 @@ async function et(t, e, s, n, a, r, o) {
734
773
  a,
735
774
  r
736
775
  );
737
- return await St(l, s, {
776
+ return await At(i, s, {
738
777
  abortSignal: o
739
778
  });
740
779
  }
741
- async function ft(t, e, s, n, a, r, o, c, l = []) {
780
+ async function ft(t, e, s, n, a, r, o, c, i = []) {
742
781
  const f = {
743
782
  message: e,
744
783
  taskId: t,
745
784
  sessionId: void 0
746
785
  // Use task's session
747
- }, u = c || { isStreaming: !0 }, h = await Q(
786
+ }, u = c || { isStreaming: !0 }, p = await Q(
748
787
  f,
749
788
  s,
750
789
  {
@@ -753,7 +792,7 @@ async function ft(t, e, s, n, a, r, o, c, l = []) {
753
792
  n,
754
793
  a,
755
794
  r
756
- ), g = vt(h, s, {
795
+ ), g = St(p, s, {
757
796
  ...u,
758
797
  abortSignal: o
759
798
  });
@@ -765,15 +804,15 @@ async function ft(t, e, s, n, a, r, o, c, l = []) {
765
804
  r,
766
805
  !0,
767
806
  // withHistory
768
- l,
807
+ i,
769
808
  // preserve conversation parts across continuation
770
809
  o,
771
810
  u
772
811
  // Pass through the same request options
773
812
  );
774
813
  }
775
- async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
776
- var f, u, h, g, p, b, m, i, y, d, I, k;
814
+ async function* kt(t, e, s, n, a, r = !0, o = [], c, i) {
815
+ var f, u, p, g, h, M, m, l, y, d, I, k;
777
816
  for await (const w of t) {
778
817
  if (yield w, w.status.state === "running" && w.status.message && e && await Xt(
779
818
  e,
@@ -783,17 +822,17 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
783
822
  w.status.message
784
823
  );
785
824
  for (const T of P) {
786
- const { toolCallId: x, toolId: A, arguments: C } = T.data;
825
+ const { toolCallId: x, toolId: v, arguments: C } = T.data;
787
826
  X(
788
827
  e,
789
- A,
828
+ v,
790
829
  C,
791
830
  (u = (f = w.status) == null ? void 0 : f.message) == null ? void 0 : u.messageId,
792
831
  x
793
- ).catch((v) => {
832
+ ).catch((S) => {
794
833
  console.error(
795
- `Tool execution failed for ${A}:`,
796
- v
834
+ `Tool execution failed for ${v}:`,
835
+ S
797
836
  );
798
837
  });
799
838
  }
@@ -819,36 +858,36 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
819
858
  if (P.length > 0) {
820
859
  const T = [];
821
860
  let x = !1;
822
- const A = [], C = [];
823
- for (const v of P) {
861
+ const v = [], C = [];
862
+ for (const S of P) {
824
863
  const {
825
- toolCallId: M,
864
+ toolCallId: b,
826
865
  toolId: _,
827
866
  arguments: N
828
- } = v.data;
867
+ } = S.data;
829
868
  try {
830
- const R = await X(
869
+ const E = await X(
831
870
  e,
832
871
  _,
833
872
  N,
834
- (g = (h = w.status) == null ? void 0 : h.message) == null ? void 0 : g.messageId,
835
- M
836
- ), { result: S, returnToAgent: E, agentMessage: J } = it(R);
837
- if (E && (x = !0), J && C.push(
873
+ (g = (p = w.status) == null ? void 0 : p.message) == null ? void 0 : g.messageId,
874
+ b
875
+ ), { result: A, returnToAgent: O, agentMessage: J } = it(E);
876
+ if (O && (x = !0), J && C.push(
838
877
  W(J)
839
- ), S.result instanceof Promise) {
840
- const V = S.result, K = {
878
+ ), A.result instanceof Promise) {
879
+ const V = A.result, K = {
841
880
  promise: V,
842
881
  resolvedValue: null
843
882
  };
844
883
  lt.set(
845
- M,
884
+ b,
846
885
  K
847
886
  ), V.then((H) => {
848
887
  K.resolvedValue = H;
849
888
  }).catch((H) => {
850
889
  console.error(
851
- `Promise rejected for tool call ${M}:`,
890
+ `Promise rejected for tool call ${b}:`,
852
891
  H
853
892
  ), K.resolvedValue = {
854
893
  error: H instanceof Error ? H.message : String(H)
@@ -856,19 +895,19 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
856
895
  });
857
896
  }
858
897
  const B = j(
859
- M,
898
+ b,
860
899
  _,
861
- S
900
+ A
862
901
  );
863
- T.push(B), A.push(B);
864
- } catch (R) {
865
- const S = j(
866
- M,
902
+ T.push(B), v.push(B);
903
+ } catch (E) {
904
+ const A = j(
905
+ b,
867
906
  _,
868
907
  void 0,
869
- R instanceof Error ? R.message : String(R)
908
+ E instanceof Error ? E.message : String(E)
870
909
  );
871
- T.push(S), A.push(S);
910
+ T.push(A), v.push(A);
872
911
  }
873
912
  }
874
913
  if (o.push(w.status.message), T.length > 0 && o.push({
@@ -877,71 +916,71 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
877
916
  parts: T,
878
917
  messageId: q()
879
918
  }), x) {
880
- const v = gt(o), M = G(
919
+ const S = gt(o), b = G(
881
920
  [],
882
921
  // Empty array - tool results are already in historyDataParts
883
- v
922
+ S
884
923
  );
885
924
  yield {
886
925
  id: w.id,
887
926
  status: {
888
927
  state: "working",
889
- message: M
928
+ message: b
890
929
  },
891
930
  final: !1,
892
931
  text: ""
893
932
  };
894
933
  const _ = await ft(
895
934
  w.id,
896
- M,
935
+ b,
897
936
  n,
898
937
  e,
899
938
  s,
900
939
  a,
901
940
  c,
902
- l,
941
+ i,
903
942
  o
904
943
  );
905
944
  let N = null;
906
- for await (const E of _)
907
- E.final ? N = E : yield E;
945
+ for await (const O of _)
946
+ O.final ? N = O : yield O;
908
947
  if (!N)
909
948
  throw new Error(
910
949
  "Continue task stream ended without final result"
911
950
  );
912
- let R = (p = N.status) != null && p.message ? F(
951
+ let E = (h = N.status) != null && h.message ? F(
913
952
  N.status.message
914
- ) : [], S = N;
915
- if (R.length > 0)
953
+ ) : [], A = N;
954
+ if (E.length > 0)
916
955
  for (yield {
917
956
  ...N,
918
957
  final: !1,
919
958
  text: D(
920
- ((b = N.status) == null ? void 0 : b.message) || {
959
+ ((M = N.status) == null ? void 0 : M.message) || {
921
960
  parts: [],
922
961
  messageId: q()
923
962
  }
924
963
  )
925
- }; R.length > 0; ) {
926
- (m = S.status) != null && m.message && o.push(
927
- S.status.message
964
+ }; E.length > 0; ) {
965
+ (m = A.status) != null && m.message && o.push(
966
+ A.status.message
928
967
  );
929
968
  const {
930
- results: E,
969
+ results: O,
931
970
  shouldReturnToAgent: J
932
- } = await At(
933
- R,
971
+ } = await vt(
972
+ E,
934
973
  e,
935
- (y = (i = S.status) == null ? void 0 : i.message) == null ? void 0 : y.messageId
974
+ (y = (l = A.status) == null ? void 0 : l.message) == null ? void 0 : y.messageId
936
975
  );
937
- if (E.length > 0 && (yield {
938
- id: S.id,
976
+ if (O.length > 0 && (yield {
977
+ id: A.id,
939
978
  status: {
940
979
  state: "working",
941
980
  message: {
942
981
  role: "agent",
943
982
  kind: "message",
944
- parts: E,
983
+ parts: O,
945
984
  messageId: q()
946
985
  }
947
986
  // Simple message with just the results
@@ -952,17 +991,17 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
952
991
  const B = r ? gt(
953
992
  o
954
993
  ) : [], V = G(
955
- E,
994
+ O,
956
995
  B
957
996
  ), K = await ft(
958
- S.id,
997
+ A.id,
959
998
  V,
960
999
  n,
961
1000
  e,
962
1001
  s,
963
1002
  a,
964
1003
  c,
965
- l,
1004
+ i,
966
1005
  o
967
1006
  );
968
1007
  let H = null;
@@ -972,14 +1011,14 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
972
1011
  throw new Error(
973
1012
  "Continue task stream ended without final result"
974
1013
  );
975
- S = H, R = (d = S.status) != null && d.message ? F(
976
- S.status.message
977
- ) : [], R.length > 0 && (yield {
978
- id: S.id,
979
- status: S.status,
1014
+ A = H, E = (d = A.status) != null && d.message ? F(
1015
+ A.status.message
1016
+ ) : [], E.length > 0 && (yield {
1017
+ id: A.id,
1018
+ status: A.status,
980
1019
  final: !1,
981
1020
  text: D(
982
- ((I = S.status) == null ? void 0 : I.message) || {
1021
+ ((I = A.status) == null ? void 0 : I.message) || {
983
1022
  parts: [],
984
1023
  messageId: q()
985
1024
  }
@@ -989,33 +1028,33 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, l) {
989
1028
  break;
990
1029
  }
991
1030
  yield {
992
- ...S,
1031
+ ...A,
993
1032
  final: !0,
994
1033
  text: D(
995
- ((k = S.status) == null ? void 0 : k.message) || {
1034
+ ((k = A.status) == null ? void 0 : k.message) || {
996
1035
  parts: [],
997
1036
  messageId: q()
998
1037
  }
999
1038
  )
1000
1039
  };
1001
1040
  } else {
1002
- const v = {
1041
+ const S = {
1003
1042
  ...w.status.message,
1004
- parts: A
1005
- }, M = {
1043
+ parts: v
1044
+ }, b = {
1006
1045
  ...w,
1007
1046
  status: {
1008
1047
  ...w.status,
1009
- message: v
1048
+ message: S
1010
1049
  },
1011
1050
  final: C.length === 0,
1012
1051
  // Only final if no agent messages to follow
1013
- text: D(v)
1052
+ text: D(S)
1014
1053
  };
1015
- if (yield M, C.length > 0) {
1016
- const _ = C.map((R) => D(R)).join(" "), N = W(_);
1054
+ if (yield b, C.length > 0) {
1055
+ const _ = C.map((E) => D(E)).join(" "), N = W(_);
1017
1056
  yield {
1018
- id: M.id,
1057
+ id: b.id,
1019
1058
  status: {
1020
1059
  state: "completed",
1021
1060
  message: N
@@ -1038,7 +1077,7 @@ function te(t) {
1038
1077
  timeout: r = Qt,
1039
1078
  toolProvider: o,
1040
1079
  contextProvider: c,
1041
- enableStreaming: l = !1
1080
+ enableStreaming: i = !1
1042
1081
  } = t, f = {
1043
1082
  agentId: e,
1044
1083
  agentUrl: s,
@@ -1048,25 +1087,25 @@ function te(t) {
1048
1087
  return {
1049
1088
  async sendMessage(u) {
1050
1089
  var I, k;
1051
- const { withHistory: h = !0, abortSignal: g } = u, p = u.sessionId || a || void 0, b = [];
1052
- b.push(u.message);
1090
+ const { withHistory: p = !0, abortSignal: g } = u, h = u.sessionId || a || void 0, M = [];
1091
+ M.push(u.message);
1053
1092
  const m = await Q(
1054
1093
  u,
1055
1094
  f,
1056
1095
  { isStreaming: !1 },
1057
1096
  o,
1058
1097
  c,
1059
- p
1098
+ h
1060
1099
  );
1061
- let i = await St(
1100
+ let l = await At(
1062
1101
  m,
1063
1102
  f,
1064
1103
  { abortSignal: g }
1065
1104
  );
1066
1105
  const y = [], d = [];
1067
- for (; i.status.message && o; ) {
1106
+ for (; l.status.message && o; ) {
1068
1107
  const w = F(
1069
- i.status.message
1108
+ l.status.message
1070
1109
  );
1071
1110
  if (w.length === 0)
1072
1111
  break;
@@ -1075,58 +1114,58 @@ function te(t) {
1075
1114
  let T = !1;
1076
1115
  for (const x of w) {
1077
1116
  const {
1078
- toolCallId: A,
1117
+ toolCallId: v,
1079
1118
  toolId: C,
1080
- arguments: v
1119
+ arguments: S
1081
1120
  } = x.data;
1082
1121
  try {
1083
- const M = await X(
1122
+ const b = await X(
1084
1123
  o,
1085
1124
  C,
1086
- v
1087
- ), { result: _, returnToAgent: N, agentMessage: R } = it(M);
1088
- N && (T = !0), R && d.push(
1089
- W(R)
1125
+ S
1126
+ ), { result: _, returnToAgent: N, agentMessage: E } = it(b);
1127
+ N && (T = !0), E && d.push(
1128
+ W(E)
1090
1129
  );
1091
- const S = j(
1092
- A,
1130
+ const A = j(
1131
+ v,
1093
1132
  C,
1094
1133
  _
1095
1134
  );
1096
- P.push(S), y.push(S);
1097
- } catch (M) {
1135
+ P.push(A), y.push(A);
1136
+ } catch (b) {
1098
1137
  const _ = j(
1099
- A,
1138
+ v,
1100
1139
  C,
1101
1140
  void 0,
1102
- M instanceof Error ? M.message : String(M)
1141
+ b instanceof Error ? b.message : String(b)
1103
1142
  );
1104
1143
  P.push(_), y.push(_);
1105
1144
  }
1106
1145
  }
1107
- if (b.push(i.status.message), T) {
1146
+ if (M.push(l.status.message), T) {
1108
1147
  const x = G(P);
1109
- i = await et(
1110
- i.id,
1148
+ l = await et(
1149
+ l.id,
1111
1150
  x,
1112
1151
  f,
1113
1152
  o,
1114
1153
  c,
1115
- p,
1154
+ h,
1116
1155
  g
1117
1156
  );
1118
1157
  } else
1119
1158
  break;
1120
1159
  }
1121
- if (y.length > 0 && (I = i.status) != null && I.message) {
1160
+ if (y.length > 0 && (I = l.status) != null && I.message) {
1122
1161
  const w = {
1123
- ...i.status.message,
1162
+ ...l.status.message,
1124
1163
  parts: y
1125
1164
  };
1126
- i = {
1127
- ...i,
1165
+ l = {
1166
+ ...l,
1128
1167
  status: {
1129
- ...i.status,
1168
+ ...l.status,
1130
1169
  message: w
1131
1170
  }
1132
1171
  };
@@ -1134,7 +1173,7 @@ function te(t) {
1134
1173
  if (d.length > 0) {
1135
1174
  const w = d.map((T) => D(T)).join(" "), P = W(w);
1136
1175
  return {
1137
- ...i,
1176
+ ...l,
1138
1177
  // Keep the enhanced message with tool results
1139
1178
  // The agent message will be handled separately by the caller
1140
1179
  text: w,
@@ -1143,9 +1182,9 @@ function te(t) {
1143
1182
  };
1144
1183
  }
1145
1184
  return {
1146
- ...i,
1185
+ ...l,
1147
1186
  text: D(
1148
- ((k = i.status) == null ? void 0 : k.message) || {
1187
+ ((k = l.status) == null ? void 0 : k.message) || {
1149
1188
  parts: [],
1150
1189
  messageId: q()
1151
1190
  }
@@ -1154,11 +1193,11 @@ function te(t) {
1154
1193
  },
1155
1194
  async *sendMessageStream(u) {
1156
1195
  const {
1157
- withHistory: h = !0,
1196
+ withHistory: p = !0,
1158
1197
  abortSignal: g,
1159
- enableStreaming: p
1160
- } = u, b = u.sessionId || a || void 0, m = p ?? l, i = [];
1161
- i.push(u.message);
1198
+ enableStreaming: h
1199
+ } = u, M = u.sessionId || a || void 0, m = h ?? i, l = [];
1200
+ l.push(u.message);
1162
1201
  const y = await Q(
1163
1202
  u,
1164
1203
  f,
@@ -1169,8 +1208,8 @@ function te(t) {
1169
1208
  },
1170
1209
  o,
1171
1210
  c,
1172
- b
1173
- ), d = vt(
1211
+ M
1212
+ ), d = St(
1174
1213
  y,
1175
1214
  f,
1176
1215
  {
@@ -1185,9 +1224,9 @@ function te(t) {
1185
1224
  o,
1186
1225
  c,
1187
1226
  f,
1188
- b,
1189
- h,
1190
- i,
1227
+ M,
1228
+ p,
1229
+ l,
1191
1230
  g,
1192
1231
  {
1193
1232
  isStreaming: !0,
@@ -1196,12 +1235,12 @@ function te(t) {
1196
1235
  }
1197
1236
  );
1198
1237
  },
1199
- async continueTask(u, h, g) {
1200
- var i;
1201
- const p = at(h);
1238
+ async continueTask(u, p, g) {
1239
+ var l;
1240
+ const h = at(p);
1202
1241
  let m = await et(
1203
1242
  u,
1204
- p,
1243
+ h,
1205
1244
  f,
1206
1245
  o,
1207
1246
  c,
@@ -1213,7 +1252,7 @@ function te(t) {
1213
1252
  );
1214
1253
  if (y.length === 0)
1215
1254
  break;
1216
- const { results: d, shouldReturnToAgent: I } = await At(y, o);
1255
+ const { results: d, shouldReturnToAgent: I } = await vt(y, o);
1217
1256
  if (I) {
1218
1257
  const k = G(d);
1219
1258
  m = await et(
@@ -1230,7 +1269,7 @@ function te(t) {
1230
1269
  return {
1231
1270
  ...m,
1232
1271
  text: D(
1233
- ((i = m.status) == null ? void 0 : i.message) || {
1272
+ ((l = m.status) == null ? void 0 : l.message) || {
1234
1273
  parts: [],
1235
1274
  messageId: q()
1236
1275
  }
@@ -1247,14 +1286,14 @@ function te(t) {
1247
1286
  }
1248
1287
  const ct = "a8c_agenttic_conversation_history";
1249
1288
  function ee(t) {
1250
- var u, h;
1289
+ var u, p;
1251
1290
  const e = t.parts.filter(
1252
1291
  (g) => g.type === "text"
1253
1292
  ), s = e.map((g) => g.text).join(`
1254
1293
  `), n = e.some(
1255
1294
  (g) => {
1256
- var p;
1257
- return ((p = g.metadata) == null ? void 0 : p.contentType) === "context";
1295
+ var h;
1296
+ return ((h = g.metadata) == null ? void 0 : h.contentType) === "context";
1258
1297
  }
1259
1298
  ) ? "context" : void 0, a = t.parts.filter(
1260
1299
  (g) => g.type === "data" && "toolCallId" in g.data && "arguments" in g.data
@@ -1268,11 +1307,11 @@ function ee(t) {
1268
1307
  toolCallId: g.data.toolCallId,
1269
1308
  result: g.data.result,
1270
1309
  error: g.data.error
1271
- })), c = a.length > 0 || r.length > 0 ? "agent" : t.role, l = ((u = t.metadata) == null ? void 0 : u.timestamp) ?? Date.now(), f = ((h = t.metadata) == null ? void 0 : h.archived) ?? void 0;
1310
+ })), c = a.length > 0 || r.length > 0 ? "agent" : t.role, i = ((u = t.metadata) == null ? void 0 : u.timestamp) ?? Date.now(), f = ((p = t.metadata) == null ? void 0 : p.archived) ?? void 0;
1272
1311
  return {
1273
1312
  role: c,
1274
1313
  content: s || "(No text content)",
1275
- timestamp: l,
1314
+ timestamp: i,
1276
1315
  ...f !== void 0 && { archived: f },
1277
1316
  ...n && { contentType: n },
1278
1317
  ...a.length > 0 && { toolCalls: a },
@@ -1342,7 +1381,7 @@ async function ae(t, e, s) {
1342
1381
  JSON.stringify(a)
1343
1382
  );
1344
1383
  } catch (a) {
1345
- O(
1384
+ R(
1346
1385
  "Failed to store conversation in sessionStorage for key %s: %O",
1347
1386
  n,
1348
1387
  a
@@ -1364,7 +1403,7 @@ async function oe(t, e) {
1364
1403
  return U.set(s, r), [...r];
1365
1404
  }
1366
1405
  } catch (n) {
1367
- O(
1406
+ R(
1368
1407
  "Failed to load conversation from sessionStorage for key %s: %O",
1369
1408
  s,
1370
1409
  n
@@ -1378,7 +1417,7 @@ async function re(t, e) {
1378
1417
  try {
1379
1418
  sessionStorage.removeItem(`${ct}_${s}`);
1380
1419
  } catch (n) {
1381
- O(
1420
+ R(
1382
1421
  "Failed to clear conversation from sessionStorage for key %s: %O",
1383
1422
  s,
1384
1423
  n
@@ -1500,14 +1539,14 @@ function ue() {
1500
1539
  f
1501
1540
  );
1502
1541
  }
1503
- const l = {
1542
+ const i = {
1504
1543
  client: a,
1505
1544
  sessionId: r,
1506
1545
  conversationStorageKey: o,
1507
1546
  conversationHistory: c,
1508
1547
  currentAbortController: null
1509
1548
  };
1510
- return t.set(s, l), a;
1549
+ return t.set(s, i), a;
1511
1550
  },
1512
1551
  getAgent(s) {
1513
1552
  const n = t.get(s);
@@ -1524,49 +1563,49 @@ function ue() {
1524
1563
  const r = t.get(s);
1525
1564
  if (!r)
1526
1565
  throw new Error(`Agent with key "${s}" not found`);
1527
- const { withHistory: o = !0, ...c } = a, { client: l, conversationHistory: f } = r, u = a.message || mt(n, f), h = await l.sendMessage({
1566
+ const { withHistory: o = !0, ...c } = a, { client: i, conversationHistory: f } = r, u = a.message || mt(n, f), p = await i.sendMessage({
1528
1567
  message: u,
1529
1568
  withHistory: o,
1530
1569
  ...c
1531
1570
  });
1532
1571
  let g = null;
1533
- if ((m = h.status) != null && m.message) {
1534
- const i = h.status.message.parts.filter(
1572
+ if ((m = p.status) != null && m.message) {
1573
+ const l = p.status.message.parts.filter(
1535
1574
  (d) => d.type === "data" && "toolCallId" in d.data && ("arguments" in d.data || "result" in d.data)
1536
- ), y = h.status.message.parts.filter(
1575
+ ), y = p.status.message.parts.filter(
1537
1576
  (d) => d.type === "text"
1538
1577
  );
1539
1578
  g = {
1540
1579
  role: "agent",
1541
1580
  kind: "message",
1542
- parts: [...i, ...y],
1581
+ parts: [...l, ...y],
1543
1582
  messageId: q(),
1544
1583
  metadata: {
1545
1584
  timestamp: Date.now()
1546
1585
  }
1547
1586
  };
1548
1587
  }
1549
- const p = [
1588
+ const h = [
1550
1589
  ...f,
1551
1590
  // Store only the new content from the user message (without history parts)
1552
1591
  at(n),
1553
1592
  // Add complete agent response with tool calls/results if present
1554
1593
  ...g ? [L(g)] : []
1555
1594
  ];
1556
- let b = p;
1557
- if (h.agentMessage) {
1558
- const i = L(
1559
- h.agentMessage
1595
+ let M = h;
1596
+ if (p.agentMessage) {
1597
+ const l = L(
1598
+ p.agentMessage
1560
1599
  );
1561
- b = [
1562
- ...p,
1563
- i
1600
+ M = [
1601
+ ...h,
1602
+ l
1564
1603
  ];
1565
1604
  }
1566
- return r.conversationHistory = b, o && await e(
1605
+ return r.conversationHistory = M, o && await e(
1567
1606
  s,
1568
- b
1569
- ), h;
1607
+ M
1608
+ ), p;
1570
1609
  },
1571
1610
  async *sendMessageStream(s, n, a = {}) {
1572
1611
  var d, I, k, w, P, T;
@@ -1576,110 +1615,110 @@ function ue() {
1576
1615
  const {
1577
1616
  withHistory: o = !0,
1578
1617
  abortSignal: c,
1579
- metadata: l,
1618
+ metadata: i,
1580
1619
  ...f
1581
- } = a, { client: u } = r, h = l ? (({ contentType: x, ...A }) => A)(l) : void 0, g = new AbortController();
1620
+ } = a, { client: u } = r, p = i ? (({ contentType: x, ...v }) => v)(i) : void 0, g = new AbortController();
1582
1621
  r.currentAbortController = g, c && c.addEventListener(
1583
1622
  "abort",
1584
1623
  () => g.abort()
1585
1624
  );
1586
- let p = [
1625
+ let h = [
1587
1626
  ...r.conversationHistory
1588
- ], b = [];
1627
+ ], M = [];
1589
1628
  const m = await ce(
1590
- p
1629
+ h
1591
1630
  );
1592
- r.conversationHistory = m, p = m, o && await e(
1631
+ r.conversationHistory = m, h = m, o && await e(
1593
1632
  s,
1594
1633
  m
1595
1634
  );
1596
- const i = a.message || mt(
1635
+ const l = a.message || mt(
1597
1636
  n,
1598
1637
  m
1599
1638
  );
1600
1639
  if (a.metadata && !a.message) {
1601
- const { contentType: x, ...A } = a.metadata;
1640
+ const { contentType: x, ...v } = a.metadata;
1602
1641
  if (x) {
1603
- const C = i.parts[i.parts.length - 1];
1642
+ const C = l.parts[l.parts.length - 1];
1604
1643
  C && C.type === "text" && (C.metadata = {
1605
1644
  ...C.metadata,
1606
1645
  contentType: x
1607
1646
  });
1608
1647
  }
1609
- Object.keys(A).length > 0 && (i.metadata = {
1610
- ...i.metadata,
1611
- ...A
1648
+ Object.keys(v).length > 0 && (l.metadata = {
1649
+ ...l.metadata,
1650
+ ...v
1612
1651
  });
1613
1652
  }
1614
1653
  const y = at(n, a.metadata);
1615
- p = [
1616
- ...p,
1654
+ h = [
1655
+ ...h,
1617
1656
  y
1618
- ], r.conversationHistory = p, o && await e(
1657
+ ], r.conversationHistory = h, o && await e(
1619
1658
  s,
1620
- p
1659
+ h
1621
1660
  );
1622
1661
  for await (const x of u.sendMessageStream({
1623
- message: i,
1662
+ message: l,
1624
1663
  withHistory: o,
1625
1664
  abortSignal: g.signal,
1626
1665
  ...f,
1627
- ...h && Object.keys(h).length > 0 && {
1628
- metadata: h
1666
+ ...p && Object.keys(p).length > 0 && {
1667
+ metadata: p
1629
1668
  }
1630
1669
  })) {
1631
1670
  if (((d = x.status) == null ? void 0 : d.state) === "input-required" && ((I = x.status) != null && I.message)) {
1632
- b = F(
1671
+ M = F(
1633
1672
  x.status.message
1634
1673
  ).map(
1635
- (v) => v.data.toolCallId
1674
+ (S) => S.data.toolCallId
1636
1675
  );
1637
1676
  const C = L(
1638
1677
  x.status.message
1639
1678
  );
1640
- p = [
1641
- ...p,
1679
+ h = [
1680
+ ...h,
1642
1681
  C
1643
- ], r.conversationHistory = p, o && await e(
1682
+ ], r.conversationHistory = h, o && await e(
1644
1683
  s,
1645
- p
1684
+ h
1646
1685
  );
1647
1686
  }
1648
1687
  if (((k = x.status) == null ? void 0 : k.state) === "working" && ((w = x.status) != null && w.message) && !x.final) {
1649
1688
  const C = le(
1650
1689
  x.status.message
1651
1690
  ).filter(
1652
- (v) => b.includes(
1653
- v.data.toolCallId
1691
+ (S) => M.includes(
1692
+ S.data.toolCallId
1654
1693
  )
1655
1694
  );
1656
1695
  if (C.length > 0) {
1657
- const v = {
1696
+ const S = {
1658
1697
  role: "agent",
1659
1698
  kind: "message",
1660
1699
  parts: C,
1661
1700
  messageId: q()
1662
1701
  };
1663
- p = [
1664
- ...p,
1665
- L(v)
1666
- ], r.conversationHistory = p, o && await e(
1702
+ h = [
1703
+ ...h,
1704
+ L(S)
1705
+ ], r.conversationHistory = h, o && await e(
1667
1706
  s,
1668
- p
1707
+ h
1669
1708
  );
1670
1709
  }
1671
1710
  }
1672
1711
  if (x.final && ((P = x.status) == null ? void 0 : P.state) !== "input-required") {
1673
- b = [];
1674
- let A = null;
1675
- (T = x.status) != null && T.message && (A = L(
1712
+ M = [];
1713
+ let v = null;
1714
+ (T = x.status) != null && T.message && (v = L(
1676
1715
  x.status.message
1677
- ), p = [
1678
- ...p,
1679
- A
1680
- ], r.conversationHistory = p, o && await e(
1716
+ ), h = [
1717
+ ...h,
1718
+ v
1719
+ ], r.conversationHistory = h, o && await e(
1681
1720
  s,
1682
- p
1721
+ h
1683
1722
  ));
1684
1723
  }
1685
1724
  yield x;
@@ -1721,11 +1760,11 @@ function ge() {
1721
1760
  (r) => {
1722
1761
  e((o) => {
1723
1762
  const c = o.findIndex(
1724
- (l) => l.id === r.id
1763
+ (i) => i.id === r.id
1725
1764
  );
1726
1765
  if (c >= 0) {
1727
- const l = [...o];
1728
- return l[c] = r, l;
1766
+ const i = [...o];
1767
+ return i[c] = r, i;
1729
1768
  }
1730
1769
  return [...o, r];
1731
1770
  });
@@ -1757,28 +1796,28 @@ function fe(t, e) {
1757
1796
  }
1758
1797
  const nt = (t) => [...t].sort((e, s) => e.timestamp - s.timestamp), z = (t, e = []) => {
1759
1798
  var o, c;
1760
- if (t.parts.some((l) => {
1761
- if (l.type === "data") {
1762
- const f = l.data;
1799
+ if (t.parts.some((i) => {
1800
+ if (i.type === "data") {
1801
+ const f = i.data;
1763
1802
  return f.toolCallId || f.toolId || f.result;
1764
1803
  }
1765
1804
  return !1;
1766
1805
  }))
1767
1806
  return null;
1768
- const n = t.parts.map((l) => {
1807
+ const n = t.parts.map((i) => {
1769
1808
  var f;
1770
- if (l.type === "text")
1809
+ if (i.type === "text")
1771
1810
  return {
1772
- type: ((f = l.metadata) == null ? void 0 : f.contentType) || "text",
1773
- text: l.text
1811
+ type: ((f = i.metadata) == null ? void 0 : f.contentType) || "text",
1812
+ text: i.text
1774
1813
  };
1775
- if (l.type === "file")
1814
+ if (i.type === "file")
1776
1815
  return {
1777
1816
  type: "image_url",
1778
- image_url: l.file.uri || `data:${l.file.mimeType};base64,${l.file.bytes}`
1817
+ image_url: i.file.uri || `data:${i.file.mimeType};base64,${i.file.bytes}`
1779
1818
  };
1780
- if (l.type === "data") {
1781
- const u = l.data;
1819
+ if (i.type === "data") {
1820
+ const u = i.data;
1782
1821
  return u.component && u.componentProps ? {
1783
1822
  type: "component",
1784
1823
  component: u.component,
@@ -1802,22 +1841,22 @@ const nt = (t) => [...t].sort((e, s) => e.timestamp - s.timestamp), z = (t, e =
1802
1841
  icon: t.role === "agent" ? "assistant" : void 0
1803
1842
  };
1804
1843
  if (t.role === "agent" && e.length > 0) {
1805
- const l = fe(
1844
+ const i = fe(
1806
1845
  r,
1807
1846
  e
1808
1847
  );
1809
- l.length > 0 && (r.actions = l);
1848
+ i.length > 0 && (r.actions = i);
1810
1849
  }
1811
1850
  return r;
1812
1851
  }, me = () => ({
1813
1852
  getClientContext: () => ({})
1814
- }), pe = () => ({
1853
+ }), he = () => ({
1815
1854
  getAvailableTools: async () => [],
1816
1855
  executeTool: async () => ({
1817
1856
  success: !0,
1818
1857
  result: "No tools available"
1819
1858
  })
1820
- }), he = (t) => ["agentId", "agentUrl", "sessionId"].every((s) => {
1859
+ }), pe = (t) => ["agentId", "agentUrl", "sessionId"].every((s) => {
1821
1860
  const n = t[s];
1822
1861
  return typeof n == "string" && n.trim().length > 0;
1823
1862
  });
@@ -1826,7 +1865,7 @@ function Ee(t) {
1826
1865
  agentId: t.agentId,
1827
1866
  agentUrl: t.agentUrl,
1828
1867
  sessionId: t.sessionId
1829
- }, s = he(e), [n, a] = yt({
1868
+ }, s = pe(e), [n, a] = yt({
1830
1869
  clientMessages: [],
1831
1870
  uiMessages: [],
1832
1871
  isProcessing: !1,
@@ -1836,43 +1875,39 @@ function Ee(t) {
1836
1875
  registerMessageActions: r,
1837
1876
  unregisterMessageActions: o,
1838
1877
  clearAllMessageActions: c,
1839
- registrations: l
1840
- } = ge(), f = Ot(l);
1878
+ registrations: i
1879
+ } = ge(), f = Ot(i);
1841
1880
  tt(() => {
1842
- f.current = l;
1843
- }, [l]), tt(() => {
1881
+ f.current = i;
1882
+ }, [i]), tt(() => {
1844
1883
  if (!s)
1845
1884
  return;
1846
1885
  (async () => {
1847
1886
  if (e.sessionId) {
1848
- const i = st(), y = `${e.agentId}-${e.sessionId}`;
1849
- if (!i.hasAgent(y)) {
1850
- await i.createAgent(y, {
1851
- agentId: e.agentId,
1852
- agentUrl: e.agentUrl,
1853
- sessionId: e.sessionId,
1854
- contextProvider: t.contextProvider || me(),
1855
- toolProvider: t.toolProvider || pe(),
1856
- authProvider: t.authProvider,
1857
- enableStreaming: t.enableStreaming
1858
- });
1859
- const d = i.getConversationHistory(y);
1860
- a((I) => {
1861
- const k = d.map(
1862
- (w) => z(
1863
- w,
1864
- f.current
1865
- )
1866
- ).filter(
1867
- (w) => w !== null
1868
- );
1869
- return {
1870
- ...I,
1871
- clientMessages: d,
1872
- uiMessages: k
1873
- };
1874
- });
1875
- }
1887
+ const l = st(), y = `${e.agentId}-${e.sessionId}`;
1888
+ l.hasAgent(y) || await l.createAgent(y, {
1889
+ agentId: e.agentId,
1890
+ agentUrl: e.agentUrl,
1891
+ sessionId: e.sessionId,
1892
+ contextProvider: t.contextProvider || me(),
1893
+ toolProvider: t.toolProvider || he(),
1894
+ authProvider: t.authProvider,
1895
+ enableStreaming: t.enableStreaming
1896
+ });
1897
+ const d = l.getConversationHistory(y);
1898
+ a((I) => {
1899
+ const k = d.map(
1900
+ (w) => z(
1901
+ w,
1902
+ f.current
1903
+ )
1904
+ ).filter((w) => w !== null);
1905
+ return {
1906
+ ...I,
1907
+ clientMessages: d,
1908
+ uiMessages: k
1909
+ };
1910
+ });
1876
1911
  }
1877
1912
  })();
1878
1913
  }, [
@@ -1886,16 +1921,16 @@ function Ee(t) {
1886
1921
  s
1887
1922
  ]);
1888
1923
  const u = $(
1889
- async (m, i) => {
1924
+ async (m, l) => {
1890
1925
  var P;
1891
1926
  if (!s)
1892
1927
  throw new Error("Invalid agent configuration");
1893
- const y = st(), d = `${e.agentId}-${e.sessionId}`, I = Date.now(), k = (i == null ? void 0 : i.type) || "text", w = {
1928
+ const y = st(), d = `${e.agentId}-${e.sessionId}`, I = Date.now(), k = (l == null ? void 0 : l.type) || "text", w = {
1894
1929
  id: `user-${I}`,
1895
1930
  role: "user",
1896
1931
  content: [{ type: k, text: m }],
1897
1932
  timestamp: I,
1898
- archived: (i == null ? void 0 : i.archived) ?? !1,
1933
+ archived: (l == null ? void 0 : l.archived) ?? !1,
1899
1934
  showIcon: !1
1900
1935
  };
1901
1936
  a((T) => ({
@@ -1906,35 +1941,35 @@ function Ee(t) {
1906
1941
  }));
1907
1942
  try {
1908
1943
  let T = null, x = !1;
1909
- const A = {};
1910
- (i != null && i.archived || i != null && i.type) && (A.metadata = {
1911
- ...(i == null ? void 0 : i.archived) && { archived: !0 },
1912
- ...(i == null ? void 0 : i.type) && { contentType: i.type }
1944
+ const v = {};
1945
+ (l != null && l.archived || l != null && l.type) && (v.metadata = {
1946
+ ...(l == null ? void 0 : l.archived) && { archived: !0 },
1947
+ ...(l == null ? void 0 : l.type) && { contentType: l.type }
1913
1948
  });
1914
1949
  for await (const C of y.sendMessageStream(
1915
1950
  d,
1916
1951
  m,
1917
- A
1952
+ v
1918
1953
  )) {
1919
1954
  if (!C.final && C.text)
1920
1955
  if (T)
1921
- a((v) => ({
1922
- ...v,
1923
- uiMessages: v.uiMessages.map(
1924
- (M) => M.id === T ? {
1925
- ...M,
1956
+ a((S) => ({
1957
+ ...S,
1958
+ uiMessages: S.uiMessages.map(
1959
+ (b) => b.id === T ? {
1960
+ ...b,
1926
1961
  content: [
1927
1962
  {
1928
1963
  type: "text",
1929
1964
  text: C.text
1930
1965
  }
1931
1966
  ]
1932
- } : M
1967
+ } : b
1933
1968
  )
1934
1969
  }));
1935
1970
  else {
1936
1971
  T = `agent-streaming-${Date.now()}`;
1937
- const v = {
1972
+ const S = {
1938
1973
  id: T,
1939
1974
  role: "agent",
1940
1975
  content: [
@@ -1945,29 +1980,29 @@ function Ee(t) {
1945
1980
  showIcon: !0,
1946
1981
  icon: "assistant"
1947
1982
  };
1948
- a((M) => ({
1949
- ...M,
1983
+ a((b) => ({
1984
+ ...b,
1950
1985
  uiMessages: [
1951
- ...M.uiMessages,
1952
- v
1986
+ ...b.uiMessages,
1987
+ S
1953
1988
  ]
1954
1989
  }));
1955
1990
  }
1956
1991
  if (C.final && ((P = C.status) != null && P.message) && T) {
1957
1992
  x = !0;
1958
- const v = T, M = z(
1993
+ const S = T, b = z(
1959
1994
  C.status.message,
1960
1995
  f.current
1961
1996
  );
1962
- M && a((_) => {
1997
+ b && a((_) => {
1963
1998
  const N = _.uiMessages.map(
1964
- (S) => S.id === v ? M : S
1965
- ), R = y.getConversationHistory(
1999
+ (A) => A.id === S ? b : A
2000
+ ), E = y.getConversationHistory(
1966
2001
  d
1967
2002
  );
1968
2003
  return {
1969
2004
  ..._,
1970
- clientMessages: R,
2005
+ clientMessages: E,
1971
2006
  uiMessages: N,
1972
2007
  isProcessing: !1
1973
2008
  };
@@ -1976,41 +2011,41 @@ function Ee(t) {
1976
2011
  }
1977
2012
  if (!x) {
1978
2013
  const C = y.getConversationHistory(d);
1979
- a((v) => {
1980
- let M = v.uiMessages;
1981
- T && (M = v.uiMessages.filter(
1982
- (E) => E.id !== T
2014
+ a((S) => {
2015
+ let b = S.uiMessages;
2016
+ T && (b = S.uiMessages.filter(
2017
+ (O) => O.id !== T
1983
2018
  ));
1984
2019
  const _ = C.map(
1985
- (E) => z(
1986
- E,
2020
+ (O) => z(
2021
+ O,
1987
2022
  f.current
1988
2023
  )
1989
2024
  ).filter(
1990
- (E) => E !== null
2025
+ (O) => O !== null
1991
2026
  ), N = new Set(
1992
- C.map((E) => E.messageId)
1993
- ), R = M.filter(
1994
- (E) => {
2027
+ C.map((O) => O.messageId)
2028
+ ), E = b.filter(
2029
+ (O) => {
1995
2030
  var J;
1996
- return !N.has(E.id) && ((J = E.content[0]) == null ? void 0 : J.type) === "component";
2031
+ return !N.has(O.id) && ((J = O.content[0]) == null ? void 0 : J.type) === "component";
1997
2032
  }
1998
- ), S = nt([
2033
+ ), A = nt([
1999
2034
  ..._,
2000
- ...R
2035
+ ...E
2001
2036
  ]);
2002
2037
  return {
2003
- ...v,
2038
+ ...S,
2004
2039
  clientMessages: C,
2005
- uiMessages: S,
2040
+ uiMessages: A,
2006
2041
  isProcessing: !1
2007
2042
  };
2008
2043
  });
2009
2044
  }
2010
2045
  } catch (T) {
2011
2046
  if (T instanceof Error && T.name === "AbortError") {
2012
- console.log("Request was aborted by user"), a((A) => ({
2013
- ...A,
2047
+ console.log("Request was aborted by user"), a((v) => ({
2048
+ ...v,
2014
2049
  isProcessing: !1,
2015
2050
  error: null
2016
2051
  // Don't show error for user-initiated abort
@@ -2018,25 +2053,25 @@ function Ee(t) {
2018
2053
  return;
2019
2054
  }
2020
2055
  const x = T instanceof Error ? T.message : "Failed to send message";
2021
- throw a((A) => ({
2022
- ...A,
2056
+ throw a((v) => ({
2057
+ ...v,
2023
2058
  isProcessing: !1,
2024
2059
  error: x
2025
2060
  })), T;
2026
2061
  }
2027
2062
  },
2028
2063
  [e.agentId, e.sessionId, s]
2029
- ), h = $((m) => {
2030
- a((i) => ({
2031
- ...i,
2032
- uiMessages: nt([...i.uiMessages, m])
2064
+ ), p = $((m) => {
2065
+ a((l) => ({
2066
+ ...l,
2067
+ uiMessages: nt([...l.uiMessages, m])
2033
2068
  }));
2034
2069
  }, []), g = $((m) => {
2035
- a((i) => ({
2036
- ...i,
2070
+ a((l) => ({
2071
+ ...l,
2037
2072
  suggestions: m
2038
2073
  }));
2039
- }, []), p = $(() => {
2074
+ }, []), h = $(() => {
2040
2075
  a((m) => ({
2041
2076
  ...m,
2042
2077
  suggestions: []
@@ -2046,7 +2081,7 @@ function Ee(t) {
2046
2081
  a((m) => {
2047
2082
  if (m.clientMessages.length === 0)
2048
2083
  return m;
2049
- const i = m.clientMessages.map(
2084
+ const l = m.clientMessages.map(
2050
2085
  (I) => z(I, f.current)
2051
2086
  ).filter((I) => I !== null), y = new Set(
2052
2087
  m.clientMessages.map((I) => I.messageId)
@@ -2059,17 +2094,17 @@ function Ee(t) {
2059
2094
  return {
2060
2095
  ...m,
2061
2096
  uiMessages: nt([
2062
- ...i,
2097
+ ...l,
2063
2098
  ...d
2064
2099
  ])
2065
2100
  };
2066
2101
  });
2067
- }, [l]);
2068
- const b = $(() => {
2102
+ }, [i]);
2103
+ const M = $(() => {
2069
2104
  if (!s)
2070
2105
  return;
2071
- const m = st(), i = `${e.agentId}-${e.sessionId}`;
2072
- m.abortCurrentRequest(i);
2106
+ const m = st(), l = `${e.agentId}-${e.sessionId}`;
2107
+ m.abortCurrentRequest(l);
2073
2108
  }, [e.agentId, e.sessionId, s]);
2074
2109
  return {
2075
2110
  // AgentUI props
@@ -2080,26 +2115,26 @@ function Ee(t) {
2080
2115
  suggestions: n.suggestions,
2081
2116
  // UI management methods
2082
2117
  registerSuggestions: g,
2083
- clearSuggestions: p,
2118
+ clearSuggestions: h,
2084
2119
  // Message actions methods
2085
2120
  registerMessageActions: r,
2086
2121
  unregisterMessageActions: o,
2087
2122
  clearAllMessageActions: c,
2088
2123
  // Tool integration
2089
- addMessage: h,
2124
+ addMessage: p,
2090
2125
  // Abort control
2091
- abortCurrentRequest: b
2126
+ abortCurrentRequest: M
2092
2127
  };
2093
2128
  }
2094
2129
  var ye = /* @__PURE__ */ ((t) => (t[t.PARSE_ERROR = -32700] = "PARSE_ERROR", t[t.INVALID_REQUEST = -32600] = "INVALID_REQUEST", t[t.METHOD_NOT_FOUND = -32601] = "METHOD_NOT_FOUND", t[t.INVALID_PARAMS = -32602] = "INVALID_PARAMS", t[t.INTERNAL_ERROR = -32603] = "INTERNAL_ERROR", t[t.SERVER_ERROR = -32e3] = "SERVER_ERROR", t))(ye || {});
2095
- const pt = "jetpack-ai-jwt-token", we = 30 * 60 * 1e3;
2130
+ const ht = "jetpack-ai-jwt-token", we = 30 * 60 * 1e3;
2096
2131
  function Ie() {
2097
2132
  var s;
2098
2133
  return window.JP_CONNECTION_INITIAL_STATE ? !1 : !!((s = window.Jetpack_Editor_Initial_State) != null && s.wpcomBlogId);
2099
2134
  }
2100
2135
  async function Te(t, e = !0) {
2101
- var l, f;
2102
- const s = localStorage.getItem(pt);
2136
+ var i, f;
2137
+ const s = localStorage.getItem(ht);
2103
2138
  let n;
2104
2139
  if (s)
2105
2140
  try {
@@ -2109,7 +2144,7 @@ async function Te(t, e = !0) {
2109
2144
  }
2110
2145
  if (n && (n != null && n.token) && (n != null && n.expire) && (n == null ? void 0 : n.expire) > Date.now() && e)
2111
2146
  return n;
2112
- const a = (l = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : l.apiNonce, r = (f = window.Jetpack_Editor_Initial_State) == null ? void 0 : f.wpcomBlogId;
2147
+ const a = (i = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : i.apiNonce, r = (f = window.Jetpack_Editor_Initial_State) == null ? void 0 : f.wpcomBlogId;
2113
2148
  let o = {
2114
2149
  token: "",
2115
2150
  blog_id: ""
@@ -2139,7 +2174,7 @@ async function Te(t, e = !0) {
2139
2174
  expire: Date.now() + we
2140
2175
  };
2141
2176
  try {
2142
- localStorage.setItem(pt, JSON.stringify(c));
2177
+ localStorage.setItem(ht, JSON.stringify(c));
2143
2178
  } catch (u) {
2144
2179
  console.log("Error storing token in localStorage:", u);
2145
2180
  }
@@ -2191,9 +2226,9 @@ export {
2191
2226
  st as getAgentManager,
2192
2227
  Pe as isWordPressAbility,
2193
2228
  Ee as useAgentChat,
2194
- Ae as useClientAbilities,
2195
- Se as useClientContext,
2196
- ve as useClientTools,
2229
+ ve as useClientAbilities,
2230
+ Ae as useClientContext,
2231
+ Se as useClientTools,
2197
2232
  ke as useClientToolsWithAbilities,
2198
2233
  ge as useMessageActions
2199
2234
  };