@automattic/agenttic-client 0.1.62 → 0.1.64

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