@automattic/agenttic-client 0.1.58 → 0.1.61

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