@automattic/agenttic-client 0.1.53 → 0.1.55

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,8 +1,8 @@
1
1
  var $e = Object.defineProperty;
2
- var Ue = (e, t, s) => t in e ? $e(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
3
- var te = (e, t, s) => Ue(e, typeof t != "symbol" ? t + "" : t, s);
4
- import qe, { useMemo as Se, useCallback as F, useState as ve, useRef as He, useEffect as se } from "react";
5
- import { l as x, f as Me, a as K } from "./logger-aKHbTlwk.js";
2
+ var Ue = (e, s, t) => s in e ? $e(e, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[s] = t;
3
+ var te = (e, s, t) => Ue(e, typeof s != "symbol" ? s + "" : s, t);
4
+ import qe, { useMemo as Se, useCallback as F, useState as Ae, useRef as He, useEffect as se } from "react";
5
+ import { l as M, f as ve, a as K } from "./logger-aKHbTlwk.js";
6
6
  import pe from "@wordpress/api-fetch";
7
7
  function Ht(e) {
8
8
  return Se(() => e ? {
@@ -10,15 +10,15 @@ function Ht(e) {
10
10
  try {
11
11
  return e() || {};
12
12
  } catch (a) {
13
- return x("Error getting client context: %O", a), {};
13
+ return M("Error getting client context: %O", a), {};
14
14
  }
15
15
  }
16
16
  } : void 0, [e]);
17
17
  }
18
18
  function ce({
19
19
  getClientTools: e,
20
- executeTool: t,
21
- getAbilities: s
20
+ executeTool: s,
21
+ getAbilities: t
22
22
  }) {
23
23
  const a = F(async () => {
24
24
  const o = [];
@@ -27,38 +27,38 @@ function ce({
27
27
  const c = await e();
28
28
  o.push(...c);
29
29
  } catch (c) {
30
- x("Error getting available tools: %O", c);
30
+ M("Error getting available tools: %O", c);
31
31
  }
32
32
  return o;
33
33
  }, [e]), n = F(
34
34
  async (o, c) => {
35
- if (!t)
35
+ if (!s)
36
36
  throw new Error("No executeTool callback provided");
37
37
  try {
38
- return await t(o, c);
38
+ return await s(o, c);
39
39
  } catch (l) {
40
- throw x("Error executing tool %s: %O", o, l), l;
40
+ throw M("Error executing tool %s: %O", o, l), l;
41
41
  }
42
42
  },
43
- [t]
43
+ [s]
44
44
  );
45
45
  return Se(() => {
46
- if (!e && !s)
46
+ if (!e && !t)
47
47
  return;
48
48
  const o = {};
49
- return e && (o.getAvailableTools = a), t && (o.executeTool = n), s && (o.getAbilities = s), o;
49
+ return e && (o.getAvailableTools = a), s && (o.executeTool = n), t && (o.getAbilities = t), o;
50
50
  }, [
51
51
  a,
52
52
  n,
53
53
  e,
54
- t,
55
- s
54
+ s,
55
+ t
56
56
  ]);
57
57
  }
58
- function Ft(e, t) {
58
+ function Ft(e, s) {
59
59
  return ce({
60
60
  getClientTools: e,
61
- executeTool: t,
61
+ executeTool: s,
62
62
  getAbilities: void 0
63
63
  });
64
64
  }
@@ -70,27 +70,27 @@ function jt(e) {
70
70
  });
71
71
  }
72
72
  function Dt(e) {
73
- const { getClientTools: t, executeTool: s, getAbilities: a } = e;
74
- if (!t && !a)
73
+ const { getClientTools: s, executeTool: t, getAbilities: a } = e;
74
+ if (!s && !a)
75
75
  throw new Error(
76
76
  "At least one of getClientTools or getAbilities must be provided to useClientToolsWithAbilities."
77
77
  );
78
- if (t && !s)
78
+ if (s && !t)
79
79
  throw new Error(
80
80
  "executeTool is required when providing getClientTools."
81
81
  );
82
82
  return ce({
83
- getClientTools: t,
84
- executeTool: s,
83
+ getClientTools: s,
84
+ executeTool: t,
85
85
  getAbilities: a
86
86
  });
87
87
  }
88
88
  function ue() {
89
89
  const e = "abcdefghijklmnopqrstuvwxyz0123456789";
90
- let t = "";
91
- for (let s = 0; s < 8; s++)
92
- t += e.charAt(Math.floor(Math.random() * e.length));
93
- return t;
90
+ let s = "";
91
+ for (let t = 0; t < 8; t++)
92
+ s += e.charAt(Math.floor(Math.random() * e.length));
93
+ return s;
94
94
  }
95
95
  function N() {
96
96
  return ue();
@@ -101,36 +101,36 @@ function Fe() {
101
101
  function je() {
102
102
  return `task-${ue()}`;
103
103
  }
104
- function xe(e, t) {
104
+ function Me(e, s) {
105
105
  return {
106
106
  type: "text",
107
107
  text: e,
108
- ...t && { metadata: t }
108
+ ...s && { metadata: s }
109
109
  };
110
110
  }
111
- function De(e, t = "message/send", s = !1) {
111
+ function De(e, s = "message/send", t = !1) {
112
112
  const a = {
113
113
  jsonrpc: "2.0",
114
114
  id: Fe(),
115
- method: t,
115
+ method: s,
116
116
  params: {
117
117
  id: e.id || je(),
118
118
  ...e
119
119
  }
120
120
  };
121
- return s && t === "message/stream" && (a.tokenStreaming = !0), a;
121
+ return t && s === "message/stream" && (a.tokenStreaming = !0), a;
122
122
  }
123
123
  function j(e) {
124
- return !e || !e.parts || !Array.isArray(e.parts) ? "" : e.parts.filter((t) => t.type === "text").map((t) => t.text).join(" ");
124
+ return !e || !e.parts || !Array.isArray(e.parts) ? "" : e.parts.filter((s) => s.type === "text").map((s) => s.text).join(" ");
125
125
  }
126
126
  function he(e) {
127
127
  if (!e || !e.parts || !Array.isArray(e.parts))
128
128
  return;
129
- const t = e.parts.find((s) => s.type === "progress" ? !0 : s.type === "data" ? "summary" in s.data && typeof s.data.summary == "string" : !1);
130
- if (t)
129
+ const s = e.parts.find((t) => t.type === "progress" ? !0 : t.type === "data" ? "summary" in t.data && typeof t.data.summary == "string" : !1);
130
+ if (s)
131
131
  return {
132
- summary: t.data.summary,
133
- phase: t.data.phase
132
+ summary: s.data.summary,
133
+ phase: s.data.phase
134
134
  };
135
135
  }
136
136
  function Je(e) {
@@ -162,16 +162,16 @@ function Ke(e) {
162
162
  }
163
163
  function L(e) {
164
164
  return !e || !e.parts || !Array.isArray(e.parts) ? [] : e.parts.filter(
165
- (t) => t.type === "data" && "toolCallId" in t.data && "toolId" in t.data && "arguments" in t.data
165
+ (s) => s.type === "data" && "toolCallId" in s.data && "toolId" in s.data && "arguments" in s.data
166
166
  );
167
167
  }
168
- function W(e, t, s, a) {
168
+ function W(e, s, t, a) {
169
169
  return {
170
170
  type: "data",
171
171
  data: {
172
172
  toolCallId: e,
173
- toolId: t,
174
- result: s
173
+ toolId: s,
174
+ result: t
175
175
  },
176
176
  metadata: a ? { error: a } : void 0
177
177
  };
@@ -185,11 +185,11 @@ function Le(e) {
185
185
  metadata: {}
186
186
  };
187
187
  }
188
- function ie(e, t) {
189
- const { contentType: s, ...a } = t || {};
188
+ function ie(e, s) {
189
+ const { contentType: t, ...a } = s || {};
190
190
  return {
191
191
  role: "user",
192
- parts: [xe(e, s ? { contentType: s } : void 0)],
192
+ parts: [Me(e, t ? { contentType: t } : void 0)],
193
193
  kind: "message",
194
194
  messageId: N(),
195
195
  metadata: {
@@ -201,7 +201,7 @@ function ie(e, t) {
201
201
  function G(e) {
202
202
  return {
203
203
  role: "agent",
204
- parts: [xe(e)],
204
+ parts: [Me(e)],
205
205
  kind: "message",
206
206
  messageId: N(),
207
207
  metadata: {
@@ -221,11 +221,11 @@ function de(e) {
221
221
  returnToAgent: !0
222
222
  };
223
223
  }
224
- function X(e, t = []) {
224
+ function X(e, s = []) {
225
225
  return {
226
226
  role: "user",
227
227
  kind: "message",
228
- parts: [...t, ...e],
228
+ parts: [...s, ...e],
229
229
  messageId: N(),
230
230
  metadata: {
231
231
  timestamp: Date.now()
@@ -235,63 +235,63 @@ function X(e, t = []) {
235
235
  function Jt() {
236
236
  return new AbortController();
237
237
  }
238
- async function Be(e, t) {
239
- if (!t)
238
+ async function Be(e, s) {
239
+ if (!s)
240
240
  return e;
241
241
  try {
242
- const s = [];
243
- if (t.getAvailableTools) {
244
- const a = await t.getAvailableTools();
242
+ const t = [];
243
+ if (s.getAvailableTools) {
244
+ const a = await s.getAvailableTools();
245
245
  if (a.length > 0) {
246
246
  const n = a.map(Je);
247
- s.push(...n);
247
+ t.push(...n);
248
248
  }
249
249
  }
250
- if (t.getAbilities) {
251
- const a = await t.getAbilities();
250
+ if (s.getAbilities) {
251
+ const a = await s.getAbilities();
252
252
  if (a.length > 0) {
253
253
  const n = a.map(Ke);
254
- s.push(...n);
254
+ t.push(...n);
255
255
  }
256
256
  }
257
- return s.length === 0 ? e : {
257
+ return t.length === 0 ? e : {
258
258
  ...e,
259
- parts: [...e.parts, ...s]
259
+ parts: [...e.parts, ...t]
260
260
  };
261
- } catch (s) {
262
- return x("Warning: Failed to get tools: %s", s), e;
261
+ } catch (t) {
262
+ return M("Warning: Failed to get tools: %s", t), e;
263
263
  }
264
264
  }
265
- function We(e, t) {
266
- if (!t)
265
+ function We(e, s) {
266
+ if (!s)
267
267
  return e;
268
268
  try {
269
- const s = t.getClientContext();
270
- if (!s || Object.keys(s).length === 0)
269
+ const t = s.getClientContext();
270
+ if (!t || Object.keys(t).length === 0)
271
271
  return e;
272
- const a = Le(s);
272
+ const a = Le(t);
273
273
  return {
274
274
  ...e,
275
275
  parts: [...e.parts, a]
276
276
  };
277
- } catch (s) {
278
- return x("Warning: Failed to get context: %s", s), e;
277
+ } catch (t) {
278
+ return M("Warning: Failed to get context: %s", t), e;
279
279
  }
280
280
  }
281
- async function Ve(e, t, s) {
281
+ async function Ve(e, s, t) {
282
282
  let a = await Be(
283
283
  e,
284
- t
284
+ s
285
285
  );
286
286
  a = We(
287
287
  a,
288
- s
288
+ t
289
289
  );
290
290
  const { metadata: n, ...r } = a;
291
291
  return r;
292
292
  }
293
- function ze(e, t = "") {
294
- const s = [], a = t + e;
293
+ function ze(e, s = "") {
294
+ const t = [], a = s + e;
295
295
  let n = "", r = 0, o = 0;
296
296
  for (; o < a.length; ) {
297
297
  const l = a.indexOf(
@@ -306,20 +306,20 @@ function ze(e, t = "") {
306
306
  );
307
307
  else if (d.trim() === "" && n) {
308
308
  try {
309
- s.push(JSON.parse(n)), r = l === -1 ? a.length : l + 1;
309
+ t.push(JSON.parse(n)), r = l === -1 ? a.length : l + 1;
310
310
  } catch (u) {
311
- x("Failed to parse SSE event: %o", u), x("Problematic payload: %s", n);
311
+ M("Failed to parse SSE event: %o", u), M("Problematic payload: %s", n);
312
312
  }
313
313
  n = "";
314
314
  }
315
315
  l === -1 ? o = a.length : o = l + 1;
316
316
  }
317
317
  const c = a.substring(r);
318
- return { events: s, nextBuffer: c };
318
+ return { events: t, nextBuffer: c };
319
319
  }
320
- async function* Ge(e, t = {}) {
320
+ async function* Ge(e, s = {}) {
321
321
  var d, u, p;
322
- const { supportDeltas: s = !1 } = t, a = e.getReader(), n = new TextDecoder();
322
+ const { supportDeltas: t = !1 } = s, a = e.getReader(), n = new TextDecoder();
323
323
  let r = "";
324
324
  const o = new Qe();
325
325
  let c = null, l = null;
@@ -328,7 +328,7 @@ async function* Ge(e, t = {}) {
328
328
  const { done: m, value: f } = await a.read();
329
329
  if (m)
330
330
  break;
331
- const w = n.decode(f, { stream: !0 }), { events: h, nextBuffer: A } = ze(w, r);
331
+ const w = n.decode(f, { stream: !0 }), { events: h, nextBuffer: x } = ze(w, r);
332
332
  if (h && Array.isArray(h))
333
333
  for (let g = 0; g < h.length; g++) {
334
334
  const i = h[g];
@@ -338,7 +338,7 @@ async function* Ge(e, t = {}) {
338
338
  throw new Error(
339
339
  `Streaming error: ${i.error.message}`
340
340
  );
341
- if (s && i.method === "message/delta" && ((d = i.params) != null && d.delta)) {
341
+ if (t && i.method === "message/delta" && ((d = i.params) != null && d.delta)) {
342
342
  const T = i.params.delta;
343
343
  try {
344
344
  if (T.deltaType === "content" && (o.processContentDelta(
@@ -356,7 +356,7 @@ async function* Ge(e, t = {}) {
356
356
  };
357
357
  }
358
358
  } catch (S) {
359
- x("Failed to process delta: %o", S);
359
+ M("Failed to process delta: %o", S);
360
360
  }
361
361
  } else if (i.result && i.result.status) {
362
362
  c = i.result.id, l = i.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset();
@@ -389,7 +389,7 @@ async function* Ge(e, t = {}) {
389
389
  };
390
390
  }
391
391
  }
392
- r = A;
392
+ r = x;
393
393
  }
394
394
  } finally {
395
395
  a.releaseLock();
@@ -404,33 +404,33 @@ class Qe {
404
404
  * Process a simple content delta (server's actual format)
405
405
  * @param content - The text content to append
406
406
  */
407
- processContentDelta(t) {
408
- this.textContent += t;
407
+ processContentDelta(s) {
408
+ this.textContent += s;
409
409
  }
410
410
  /**
411
411
  * Process a delta message and accumulate the content (original format)
412
412
  * @param delta - The delta message to process
413
413
  */
414
- processDelta(t) {
415
- switch (t.type) {
414
+ processDelta(s) {
415
+ switch (s.type) {
416
416
  case "content":
417
- this.textContent += t.content;
417
+ this.textContent += s.content;
418
418
  break;
419
419
  case "tool_name":
420
- this.toolCalls.has(t.toolCallIndex) || this.toolCalls.set(t.toolCallIndex, {
421
- toolCallId: t.toolCallId,
420
+ this.toolCalls.has(s.toolCallIndex) || this.toolCalls.set(s.toolCallIndex, {
421
+ toolCallId: s.toolCallId,
422
422
  toolName: "",
423
423
  argumentFragments: []
424
424
  });
425
- const s = this.toolCalls.get(t.toolCallIndex);
426
- s.toolName += t.content;
425
+ const t = this.toolCalls.get(s.toolCallIndex);
426
+ t.toolName += s.content;
427
427
  break;
428
428
  case "tool_argument":
429
- this.toolCalls.has(t.toolCallIndex) || this.toolCalls.set(t.toolCallIndex, {
430
- toolCallId: t.toolCallId,
429
+ this.toolCalls.has(s.toolCallIndex) || this.toolCalls.set(s.toolCallIndex, {
430
+ toolCallId: s.toolCallId,
431
431
  toolName: "",
432
432
  argumentFragments: []
433
- }), this.toolCalls.get(t.toolCallIndex).argumentFragments.push(t.content);
433
+ }), this.toolCalls.get(s.toolCallIndex).argumentFragments.push(s.content);
434
434
  break;
435
435
  }
436
436
  }
@@ -444,9 +444,9 @@ class Qe {
444
444
  * Get the current accumulated message
445
445
  * @param role - The role for the message (default: 'agent')
446
446
  */
447
- getCurrentMessage(t = "agent") {
448
- const s = [];
449
- this.textContent && s.push({
447
+ getCurrentMessage(s = "agent") {
448
+ const t = [];
449
+ this.textContent && t.push({
450
450
  type: "text",
451
451
  text: this.textContent
452
452
  });
@@ -460,7 +460,7 @@ class Qe {
460
460
  } catch {
461
461
  o = { _raw: r };
462
462
  }
463
- s.push({
463
+ t.push({
464
464
  type: "data",
465
465
  data: {
466
466
  toolCallId: n.toolCallId,
@@ -470,8 +470,8 @@ class Qe {
470
470
  });
471
471
  }
472
472
  return {
473
- role: t,
474
- parts: s,
473
+ role: s,
474
+ parts: t,
475
475
  kind: "message",
476
476
  messageId: N()
477
477
  };
@@ -483,71 +483,71 @@ class Qe {
483
483
  this.textContent = "", this.toolCalls.clear();
484
484
  }
485
485
  }
486
- function Ae(e, t, s = "request") {
487
- throw clearTimeout(t), x("%s failed with error: %O", s, e), e instanceof Error && (x("Error message: %s", e.message), x("Error stack: %s", e.stack)), e;
486
+ function xe(e, s, t = "request") {
487
+ throw clearTimeout(s), M("%s failed with error: %O", t, e), e instanceof Error && (M("Error message: %s", e.message), M("Error stack: %s", e.stack)), e;
488
488
  }
489
- function Ce(e, t = "request") {
489
+ function Ce(e, s = "request") {
490
490
  if (!e.ok)
491
491
  throw new Error(`HTTP error! status: ${e.status}`);
492
492
  }
493
- function Xe(e, t = "request") {
493
+ function Xe(e, s = "request") {
494
494
  if (e.error)
495
495
  throw new Error(
496
- `Protocol ${t} error: ${e.error.message}`
496
+ `Protocol ${s} error: ${e.error.message}`
497
497
  );
498
498
  if (!e.result)
499
- throw new Error(`No result in ${t} response`);
499
+ throw new Error(`No result in ${s} response`);
500
500
  return e.result;
501
501
  }
502
- function Ye(e, t = "streaming request") {
503
- if (Ce(e, t), !e.body)
504
- throw new Error(`No response body for ${t}`);
502
+ function Ye(e, s = "streaming request") {
503
+ if (Ce(e, s), !e.body)
504
+ throw new Error(`No response body for ${s}`);
505
505
  }
506
- function be(e, t = "request") {
507
- const s = new AbortController();
506
+ function be(e, s = "request") {
507
+ const t = new AbortController();
508
508
  return { timeoutId: setTimeout(
509
- () => s.abort(),
509
+ () => t.abort(),
510
510
  e
511
- ), controller: s };
511
+ ), controller: t };
512
512
  }
513
- function Ze(e, t) {
514
- return `${e}/${t}`;
513
+ function Ze(e, s) {
514
+ return `${e}/${s}`;
515
515
  }
516
- function et(e, t, s, a) {
517
- x("Request: %s %s", e, t), x("Headers: %o", s), a && x("Body: %s", Me(a));
516
+ function et(e, s, t, a) {
517
+ M("Request: %s %s", e, s), M("Headers: %o", t), a && M("Body: %s", ve(a));
518
518
  }
519
- async function tt(e, t = !1) {
520
- const s = {
519
+ async function tt(e, s = !1) {
520
+ const t = {
521
521
  "Content-Type": "application/json"
522
522
  };
523
- if (t && (s.Accept = "text/event-stream"), e) {
523
+ if (s && (t.Accept = "text/event-stream"), e) {
524
524
  const a = await e();
525
- return { ...s, ...a };
525
+ return { ...t, ...a };
526
526
  }
527
- return s;
527
+ return t;
528
528
  }
529
- function ke(e, t) {
530
- if (!t)
529
+ function ke(e, s) {
530
+ if (!s)
531
531
  return e;
532
- const s = new AbortController(), a = (n) => {
533
- s.signal.aborted || s.abort(n.reason);
532
+ const t = new AbortController(), a = (n) => {
533
+ t.signal.aborted || t.abort(n.reason);
534
534
  };
535
- return e.aborted ? s.abort(e.reason) : e.addEventListener("abort", () => a(e), {
535
+ return e.aborted ? t.abort(e.reason) : e.addEventListener("abort", () => a(e), {
536
536
  once: !0
537
- }), t.aborted ? s.abort(t.reason) : t.addEventListener("abort", () => a(t), {
537
+ }), s.aborted ? t.abort(s.reason) : s.addEventListener("abort", () => a(s), {
538
538
  once: !0
539
- }), s.signal;
539
+ }), t.signal;
540
540
  }
541
- function Re(e, t, s) {
541
+ function Pe(e, s, t) {
542
542
  return {
543
543
  method: "POST",
544
544
  headers: e,
545
- body: t,
546
- signal: s
545
+ body: s,
546
+ signal: t
547
547
  };
548
548
  }
549
- async function Y(e, t, s, a, n, r) {
550
- const { message: o, sessionId: c, taskId: l, metadata: d } = e, { agentId: u, agentUrl: p, authProvider: m, proxy: f } = t, { isStreaming: w = !1, enableTokenStreaming: h = !1 } = s, A = c || r, g = Ze(p, u), i = await Ve(
549
+ async function Y(e, s, t, a, n, r) {
550
+ const { message: o, sessionId: c, taskId: l, metadata: d } = e, { agentId: u, agentUrl: p, authProvider: m, proxy: f } = s, { isStreaming: w = !1, enableTokenStreaming: h = !1 } = t, x = c || r, g = Ze(p, u), i = await Ve(
551
551
  o,
552
552
  a,
553
553
  n
@@ -556,7 +556,7 @@ async function Y(e, t, s, a, n, r) {
556
556
  message: i,
557
557
  metadata: d
558
558
  };
559
- A && (T.sessionId = A);
559
+ x && (T.sessionId = x);
560
560
  const S = De(
561
561
  T,
562
562
  w ? "message/stream" : "message/send",
@@ -567,49 +567,49 @@ async function Y(e, t, s, a, n, r) {
567
567
  request: S,
568
568
  headers: I,
569
569
  enhancedMessage: i,
570
- effectiveSessionId: A,
570
+ effectiveSessionId: x,
571
571
  fullAgentUrl: g
572
572
  };
573
573
  }
574
- async function Pe(e, t, s = {}) {
575
- const { request: a, headers: n, fullAgentUrl: r } = e, { timeout: o } = t, { abortSignal: c } = s, { timeoutId: l, controller: d } = be(
574
+ async function Re(e, s, t = {}) {
575
+ const { request: a, headers: n, fullAgentUrl: r } = e, { timeout: o } = s, { abortSignal: c } = t, { timeoutId: l, controller: d } = be(
576
576
  o,
577
577
  "request"
578
578
  ), u = c ? ke(d.signal, c) : d.signal;
579
579
  try {
580
- const p = Re(
580
+ const p = Pe(
581
581
  n,
582
582
  JSON.stringify(a),
583
583
  u
584
584
  );
585
- x("Making request to %s with options: %O", r, {
585
+ M("Making request to %s with options: %O", r, {
586
586
  method: p.method,
587
587
  headers: p.headers
588
588
  });
589
589
  const m = await fetch(r, p);
590
590
  clearTimeout(l), Ce(m, "request");
591
591
  const f = await m.json();
592
- return x(
592
+ return M(
593
593
  "Response from %s: %d %O",
594
594
  r,
595
595
  m.status,
596
- Me(f)
596
+ ve(f)
597
597
  ), Xe(f, "request");
598
598
  } catch (p) {
599
- Ae(p, l, "request");
599
+ xe(p, l, "request");
600
600
  }
601
601
  }
602
- async function* _e(e, t, s) {
602
+ async function* _e(e, s, t) {
603
603
  const { request: a, headers: n, fullAgentUrl: r } = e, {
604
604
  streamingTimeout: o = 6e4,
605
605
  abortSignal: c,
606
606
  enableTokenStreaming: l = !1
607
- } = s, { timeoutId: d, controller: u } = be(
607
+ } = t, { timeoutId: d, controller: u } = be(
608
608
  o,
609
609
  "streaming request"
610
610
  ), p = c ? ke(u.signal, c) : u.signal;
611
611
  try {
612
- const m = JSON.stringify(a), f = Re(n, m, p), w = await fetch(r, f);
612
+ const m = JSON.stringify(a), f = Pe(n, m, p), w = await fetch(r, f);
613
613
  if (clearTimeout(d), Ye(w, "streaming request"), !w.body)
614
614
  throw new Error(
615
615
  "Response body is null - server may not support streaming"
@@ -619,21 +619,21 @@ async function* _e(e, t, s) {
619
619
  supportDeltas: h
620
620
  });
621
621
  } catch (m) {
622
- Ae(m, d, "streaming request");
622
+ xe(m, d, "streaming request");
623
623
  }
624
624
  }
625
625
  const st = 12e4;
626
- async function Z(e, t, s, a, n) {
626
+ async function Z(e, s, t, a, n) {
627
627
  if (e.getAbilities) {
628
628
  const r = await e.getAbilities();
629
629
  if (r.length > 0)
630
630
  for (const o of r) {
631
631
  const c = o.name.replace(/\//g, "__").replace(/-/g, "_");
632
- if (t === c || t === o.name) {
632
+ if (s === c || s === o.name) {
633
633
  if (o.callback)
634
634
  try {
635
635
  const l = {
636
- ...s,
636
+ ...t,
637
637
  ...a && { messageId: a }
638
638
  }, d = await o.callback(l);
639
639
  return {
@@ -644,7 +644,7 @@ async function Z(e, t, s, a, n) {
644
644
  }
645
645
  };
646
646
  } catch (l) {
647
- return x(
647
+ return M(
648
648
  "Error executing ability %s: %O",
649
649
  o.name,
650
650
  l
@@ -660,10 +660,10 @@ async function Z(e, t, s, a, n) {
660
660
  try {
661
661
  return { result: await e.executeAbility(
662
662
  o.name,
663
- s
663
+ t
664
664
  ), returnToAgent: !0 };
665
665
  } catch (l) {
666
- return x(
666
+ return M(
667
667
  "Error executing ability %s: %O",
668
668
  o.name,
669
669
  l
@@ -683,25 +683,25 @@ async function Z(e, t, s, a, n) {
683
683
  }
684
684
  if (e.executeTool)
685
685
  return await e.executeTool(
686
- t,
687
686
  s,
687
+ t,
688
688
  a,
689
689
  n
690
690
  );
691
691
  throw new Error(
692
- `No handler found for tool: ${t}. Tool provider must implement executeTool for non-ability tools.`
692
+ `No handler found for tool: ${s}. Tool provider must implement executeTool for non-ability tools.`
693
693
  );
694
694
  }
695
695
  const ge = /* @__PURE__ */ new Map();
696
- async function at(e, t) {
697
- if (!e || !t || !e.getAvailableTools)
696
+ async function at(e, s) {
697
+ if (!e || !s || !e.getAvailableTools)
698
698
  return !1;
699
- const s = L(t);
700
- if (s.length === 0)
699
+ const t = L(s);
700
+ if (t.length === 0)
701
701
  return !1;
702
702
  try {
703
703
  const a = await e.getAvailableTools();
704
- for (const n of s)
704
+ for (const n of t)
705
705
  if (a.some(
706
706
  (o) => o.id === n.data.toolId
707
707
  ))
@@ -715,35 +715,35 @@ function nt() {
715
715
  ge.clear();
716
716
  }
717
717
  function ot(e) {
718
- return e.map((t) => {
719
- const s = t.data.toolCallId, a = ge.get(s);
718
+ return e.map((s) => {
719
+ const t = s.data.toolCallId, a = ge.get(t);
720
720
  if (a && a.resolvedValue !== null) {
721
721
  const n = a.resolvedValue;
722
722
  return n.error ? W(
723
- s,
724
- t.data.toolId,
723
+ t,
724
+ s.data.toolId,
725
725
  void 0,
726
726
  n.error
727
727
  ) : W(
728
- s,
729
- t.data.toolId,
728
+ t,
729
+ s.data.toolId,
730
730
  n
731
731
  );
732
732
  }
733
- return t;
733
+ return s;
734
734
  });
735
735
  }
736
- async function Ee(e, t, s) {
736
+ async function Ee(e, s, t) {
737
737
  const a = [], n = [];
738
738
  let r = !1;
739
739
  for (const o of e) {
740
740
  const { toolCallId: c, toolId: l, arguments: d } = o.data;
741
741
  try {
742
742
  const u = await Z(
743
- t,
743
+ s,
744
744
  l,
745
745
  d,
746
- s,
746
+ t,
747
747
  c
748
748
  ), { result: p, returnToAgent: m, agentMessage: f } = de(u);
749
749
  m && (r = !0), f && n.push(G(f)), a.push(
@@ -767,52 +767,52 @@ async function Ee(e, t, s) {
767
767
  return { results: a, shouldReturnToAgent: r, agentMessages: n };
768
768
  }
769
769
  function ye(e) {
770
- const t = [];
771
- for (const s of e)
772
- for (const a of s.parts)
773
- a.type === "text" ? t.push({
770
+ const s = [];
771
+ for (const t of e)
772
+ for (const a of t.parts)
773
+ a.type === "text" ? s.push({
774
774
  type: "data",
775
775
  data: {
776
- role: s.role,
776
+ role: t.role,
777
777
  text: a.text
778
778
  }
779
- }) : (a.type === "data" || a.type === "file") && t.push(a);
780
- return t;
779
+ }) : (a.type === "data" || a.type === "file") && s.push(a);
780
+ return s;
781
781
  }
782
- async function ae(e, t, s, a, n, r, o) {
782
+ async function ae(e, s, t, a, n, r, o) {
783
783
  const l = await Y(
784
784
  {
785
- message: t,
785
+ message: s,
786
786
  taskId: e,
787
787
  sessionId: void 0
788
788
  // Use task's session
789
789
  },
790
- s,
790
+ t,
791
791
  { isStreaming: !1 },
792
792
  a,
793
793
  n,
794
794
  r
795
795
  );
796
- return await Pe(l, s, {
796
+ return await Re(l, t, {
797
797
  abortSignal: o
798
798
  });
799
799
  }
800
- async function Ie(e, t, s, a, n, r, o, c, l = []) {
800
+ async function Ie(e, s, t, a, n, r, o, c, l = []) {
801
801
  const d = {
802
- message: t,
802
+ message: s,
803
803
  taskId: e,
804
804
  sessionId: r
805
805
  // Use the provided sessionId to maintain conversation continuity
806
806
  }, u = c || { isStreaming: !0 }, p = await Y(
807
807
  d,
808
- s,
808
+ t,
809
809
  {
810
810
  ...u
811
811
  },
812
812
  a,
813
813
  n,
814
814
  r
815
- ), m = _e(p, s, {
815
+ ), m = _e(p, t, {
816
816
  ...u,
817
817
  abortSignal: o
818
818
  });
@@ -820,7 +820,7 @@ async function Ie(e, t, s, a, n, r, o, c, l = []) {
820
820
  m,
821
821
  a,
822
822
  n,
823
- s,
823
+ t,
824
824
  r,
825
825
  !0,
826
826
  // withHistory
@@ -831,26 +831,26 @@ async function Ie(e, t, s, a, n, r, o, c, l = []) {
831
831
  // Pass through the same request options
832
832
  );
833
833
  }
834
- async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
835
- var d, u, p, m, f, w, h, A, g, i, T, S;
834
+ async function* Oe(e, s, t, a, n, r = !0, o = [], c, l) {
835
+ var d, u, p, m, f, w, h, x, g, i, T, S;
836
836
  for await (const I of e) {
837
- if (I.sessionId && !n && (n = I.sessionId), yield I, I.status.state === "running" && I.status.message && t && await at(
838
- t,
837
+ if (I.sessionId && !n && (n = I.sessionId), yield I, I.status.state === "running" && I.status.message && s && await at(
838
+ s,
839
839
  I.status.message
840
840
  )) {
841
841
  const _ = L(
842
842
  I.status.message
843
843
  );
844
844
  for (const O of _) {
845
- const { toolCallId: $, toolId: v, arguments: y } = O.data;
845
+ const { toolCallId: $, toolId: A, arguments: y } = O.data;
846
846
  Z(
847
- t,
848
- v,
847
+ s,
848
+ A,
849
849
  y,
850
850
  (u = (d = I.status) == null ? void 0 : d.message) == null ? void 0 : u.messageId,
851
851
  $
852
852
  ).catch((C) => {
853
- x("Tool execution failed for %s: %O", v, C);
853
+ M("Tool execution failed for %s: %O", A, C);
854
854
  });
855
855
  }
856
856
  yield {
@@ -868,44 +868,44 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
868
868
  text: ""
869
869
  };
870
870
  }
871
- if (I.status.state === "input-required" && I.status.message && t) {
871
+ if (I.status.state === "input-required" && I.status.message && s) {
872
872
  const _ = L(
873
873
  I.status.message
874
874
  );
875
875
  if (_.length > 0) {
876
876
  const O = [];
877
877
  let $ = !1;
878
- const v = [], y = [];
878
+ const A = [], y = [];
879
879
  for (const C of _) {
880
880
  const {
881
- toolCallId: M,
882
- toolId: R,
881
+ toolCallId: v,
882
+ toolId: P,
883
883
  arguments: k
884
884
  } = C.data;
885
885
  try {
886
886
  const b = await Z(
887
- t,
888
- R,
887
+ s,
888
+ P,
889
889
  k,
890
890
  (m = (p = I.status) == null ? void 0 : p.message) == null ? void 0 : m.messageId,
891
- M
892
- ), { result: P, returnToAgent: U, agentMessage: D } = de(b);
891
+ v
892
+ ), { result: R, returnToAgent: U, agentMessage: D } = de(b);
893
893
  if (U && ($ = !0), D && y.push(
894
894
  G(D)
895
- ), P.result instanceof Promise) {
896
- const E = P.result, J = {
895
+ ), R.result instanceof Promise) {
896
+ const E = R.result, J = {
897
897
  promise: E,
898
898
  resolvedValue: null
899
899
  };
900
900
  ge.set(
901
- M,
901
+ v,
902
902
  J
903
903
  ), E.then((H) => {
904
904
  J.resolvedValue = H;
905
905
  }).catch((H) => {
906
- x(
906
+ M(
907
907
  "Promise rejected for tool call %s: %O",
908
- M,
908
+ v,
909
909
  H
910
910
  ), J.resolvedValue = {
911
911
  error: H instanceof Error ? H.message : String(H)
@@ -913,19 +913,19 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
913
913
  });
914
914
  }
915
915
  const q = W(
916
- M,
917
- R,
918
- P
916
+ v,
917
+ P,
918
+ R
919
919
  );
920
- O.push(q), v.push(q);
920
+ O.push(q), A.push(q);
921
921
  } catch (b) {
922
- const P = W(
923
- M,
924
- R,
922
+ const R = W(
923
+ v,
924
+ P,
925
925
  void 0,
926
926
  b instanceof Error ? b.message : String(b)
927
927
  );
928
- O.push(P), v.push(P);
928
+ O.push(R), A.push(R);
929
929
  }
930
930
  }
931
931
  if (o.push(I.status.message), O.length > 0 && o.push({
@@ -934,7 +934,7 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
934
934
  parts: O,
935
935
  messageId: N()
936
936
  }), $) {
937
- const C = ye(o), M = X(
937
+ const C = ye(o), v = X(
938
938
  [],
939
939
  // Empty array - tool results are already in historyDataParts
940
940
  C
@@ -943,24 +943,24 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
943
943
  id: I.id,
944
944
  status: {
945
945
  state: "working",
946
- message: M
946
+ message: v
947
947
  },
948
948
  final: !1,
949
949
  text: ""
950
950
  };
951
- const R = await Ie(
951
+ const P = await Ie(
952
952
  I.id,
953
- M,
953
+ v,
954
954
  a,
955
- t,
956
955
  s,
956
+ t,
957
957
  n,
958
958
  c,
959
959
  l,
960
960
  o
961
961
  );
962
962
  let k = null;
963
- for await (const U of R)
963
+ for await (const U of P)
964
964
  U.final ? k = U : yield U;
965
965
  if (!k)
966
966
  throw new Error(
@@ -968,7 +968,7 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
968
968
  );
969
969
  let b = (f = k.status) != null && f.message ? L(
970
970
  k.status.message
971
- ) : [], P = k;
971
+ ) : [], R = k;
972
972
  if (b.length > 0)
973
973
  for (yield {
974
974
  ...k,
@@ -980,19 +980,19 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
980
980
  }
981
981
  )
982
982
  }; b.length > 0; ) {
983
- (h = P.status) != null && h.message && o.push(
984
- P.status.message
983
+ (h = R.status) != null && h.message && o.push(
984
+ R.status.message
985
985
  );
986
986
  const {
987
987
  results: U,
988
988
  shouldReturnToAgent: D
989
989
  } = await Ee(
990
990
  b,
991
- t,
992
- (g = (A = P.status) == null ? void 0 : A.message) == null ? void 0 : g.messageId
991
+ s,
992
+ (g = (x = R.status) == null ? void 0 : x.message) == null ? void 0 : g.messageId
993
993
  );
994
994
  if (U.length > 0 && (yield {
995
- id: P.id,
995
+ id: R.id,
996
996
  status: {
997
997
  state: "working",
998
998
  message: {
@@ -1012,11 +1012,11 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
1012
1012
  U,
1013
1013
  q
1014
1014
  ), J = await Ie(
1015
- P.id,
1015
+ R.id,
1016
1016
  E,
1017
1017
  a,
1018
- t,
1019
1018
  s,
1019
+ t,
1020
1020
  n,
1021
1021
  c,
1022
1022
  l,
@@ -1029,14 +1029,14 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
1029
1029
  throw new Error(
1030
1030
  "Continue task stream ended without final result"
1031
1031
  );
1032
- P = H, b = (i = P.status) != null && i.message ? L(
1033
- P.status.message
1032
+ R = H, b = (i = R.status) != null && i.message ? L(
1033
+ R.status.message
1034
1034
  ) : [], b.length > 0 && (yield {
1035
- id: P.id,
1036
- status: P.status,
1035
+ id: R.id,
1036
+ status: R.status,
1037
1037
  final: !1,
1038
1038
  text: j(
1039
- ((T = P.status) == null ? void 0 : T.message) || {
1039
+ ((T = R.status) == null ? void 0 : T.message) || {
1040
1040
  parts: [],
1041
1041
  messageId: N()
1042
1042
  }
@@ -1046,10 +1046,10 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
1046
1046
  break;
1047
1047
  }
1048
1048
  yield {
1049
- ...P,
1049
+ ...R,
1050
1050
  final: !0,
1051
1051
  text: j(
1052
- ((S = P.status) == null ? void 0 : S.message) || {
1052
+ ((S = R.status) == null ? void 0 : S.message) || {
1053
1053
  parts: [],
1054
1054
  messageId: N()
1055
1055
  }
@@ -1058,8 +1058,8 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
1058
1058
  } else {
1059
1059
  const C = {
1060
1060
  ...I.status.message,
1061
- parts: v
1062
- }, M = {
1061
+ parts: A
1062
+ }, v = {
1063
1063
  ...I,
1064
1064
  status: {
1065
1065
  ...I.status,
@@ -1069,16 +1069,16 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
1069
1069
  // Only final if no agent messages to follow
1070
1070
  text: j(C)
1071
1071
  };
1072
- if (yield M, y.length > 0) {
1073
- const R = y.map((b) => j(b)).join(" "), k = G(R);
1072
+ if (yield v, y.length > 0) {
1073
+ const P = y.map((b) => j(b)).join(" "), k = G(P);
1074
1074
  yield {
1075
- id: M.id,
1075
+ id: v.id,
1076
1076
  status: {
1077
1077
  state: "completed",
1078
1078
  message: k
1079
1079
  },
1080
1080
  final: !0,
1081
- text: R
1081
+ text: P
1082
1082
  };
1083
1083
  }
1084
1084
  }
@@ -1088,8 +1088,8 @@ async function* Oe(e, t, s, a, n, r = !0, o = [], c, l) {
1088
1088
  }
1089
1089
  function rt(e) {
1090
1090
  const {
1091
- agentId: t,
1092
- agentUrl: s,
1091
+ agentId: s,
1092
+ agentUrl: t,
1093
1093
  authProvider: a,
1094
1094
  defaultSessionId: n,
1095
1095
  timeout: r = st,
@@ -1097,8 +1097,8 @@ function rt(e) {
1097
1097
  contextProvider: c,
1098
1098
  enableStreaming: l = !1
1099
1099
  } = e, d = {
1100
- agentId: t,
1101
- agentUrl: s,
1100
+ agentId: s,
1101
+ agentUrl: t,
1102
1102
  authProvider: a,
1103
1103
  timeout: r
1104
1104
  };
@@ -1115,50 +1115,50 @@ function rt(e) {
1115
1115
  c,
1116
1116
  m
1117
1117
  );
1118
- let h = await Pe(
1118
+ let h = await Re(
1119
1119
  w,
1120
1120
  d,
1121
1121
  { abortSignal: p }
1122
1122
  );
1123
- const A = [], g = [];
1123
+ const x = [], g = [];
1124
1124
  for (; h.status.message && o; ) {
1125
1125
  const S = L(
1126
1126
  h.status.message
1127
1127
  );
1128
1128
  if (S.length === 0)
1129
1129
  break;
1130
- A.push(...S);
1130
+ x.push(...S);
1131
1131
  const I = [];
1132
1132
  let _ = !1;
1133
1133
  for (const O of S) {
1134
1134
  const {
1135
1135
  toolCallId: $,
1136
- toolId: v,
1136
+ toolId: A,
1137
1137
  arguments: y
1138
1138
  } = O.data;
1139
1139
  try {
1140
1140
  const C = await Z(
1141
1141
  o,
1142
- v,
1142
+ A,
1143
1143
  y
1144
- ), { result: M, returnToAgent: R, agentMessage: k } = de(C);
1145
- R && (_ = !0), k && g.push(
1144
+ ), { result: v, returnToAgent: P, agentMessage: k } = de(C);
1145
+ P && (_ = !0), k && g.push(
1146
1146
  G(k)
1147
1147
  );
1148
1148
  const b = W(
1149
1149
  $,
1150
- v,
1151
- M
1150
+ A,
1151
+ v
1152
1152
  );
1153
- I.push(b), A.push(b);
1153
+ I.push(b), x.push(b);
1154
1154
  } catch (C) {
1155
- const M = W(
1155
+ const v = W(
1156
1156
  $,
1157
- v,
1157
+ A,
1158
1158
  void 0,
1159
1159
  C instanceof Error ? C.message : String(C)
1160
1160
  );
1161
- I.push(M), A.push(M);
1161
+ I.push(v), x.push(v);
1162
1162
  }
1163
1163
  }
1164
1164
  if (f.push(h.status.message), _) {
@@ -1175,10 +1175,10 @@ function rt(e) {
1175
1175
  } else
1176
1176
  break;
1177
1177
  }
1178
- if (A.length > 0 && (i = h.status) != null && i.message) {
1178
+ if (x.length > 0 && (i = h.status) != null && i.message) {
1179
1179
  const S = {
1180
1180
  ...h.status.message,
1181
- parts: A
1181
+ parts: x
1182
1182
  };
1183
1183
  h = {
1184
1184
  ...h,
@@ -1214,8 +1214,8 @@ function rt(e) {
1214
1214
  withHistory: p = !0,
1215
1215
  abortSignal: m,
1216
1216
  enableStreaming: f
1217
- } = u, w = u.sessionId || n || void 0, h = f ?? l, A = [];
1218
- A.push(u.message);
1217
+ } = u, w = u.sessionId || n || void 0, h = f ?? l, x = [];
1218
+ x.push(u.message);
1219
1219
  const g = await Y(
1220
1220
  u,
1221
1221
  d,
@@ -1244,7 +1244,7 @@ function rt(e) {
1244
1244
  d,
1245
1245
  w,
1246
1246
  p,
1247
- A,
1247
+ x,
1248
1248
  m,
1249
1249
  {
1250
1250
  isStreaming: !0,
@@ -1254,7 +1254,7 @@ function rt(e) {
1254
1254
  );
1255
1255
  },
1256
1256
  async continueTask(u, p, m) {
1257
- var A;
1257
+ var x;
1258
1258
  const f = ie(p);
1259
1259
  let h = await ae(
1260
1260
  u,
@@ -1287,7 +1287,7 @@ function rt(e) {
1287
1287
  return {
1288
1288
  ...h,
1289
1289
  text: j(
1290
- ((A = h.status) == null ? void 0 : A.message) || {
1290
+ ((x = h.status) == null ? void 0 : x.message) || {
1291
1291
  parts: [],
1292
1292
  messageId: N()
1293
1293
  }
@@ -1303,17 +1303,17 @@ function rt(e) {
1303
1303
  };
1304
1304
  }
1305
1305
  function it(e) {
1306
- const t = [];
1306
+ const s = [];
1307
1307
  if (e.content) {
1308
1308
  const r = {
1309
1309
  type: "text",
1310
1310
  text: e.content
1311
1311
  };
1312
- t.push(r);
1312
+ s.push(r);
1313
1313
  }
1314
- const s = e.context;
1315
- if (s && !Array.isArray(s) && Array.isArray(s.file_parts)) {
1316
- for (const r of s.file_parts)
1314
+ const t = e.context;
1315
+ if (t && !Array.isArray(t) && Array.isArray(t.file_parts)) {
1316
+ for (const r of t.file_parts)
1317
1317
  if (r != null && r.uri) {
1318
1318
  const o = {
1319
1319
  type: "file",
@@ -1324,16 +1324,23 @@ function it(e) {
1324
1324
  // Pass attachment ID in metadata if available
1325
1325
  metadata: r.id ? { id: r.id } : void 0
1326
1326
  };
1327
- t.push(o);
1327
+ s.push(o);
1328
1328
  }
1329
1329
  }
1330
+ if (t && !Array.isArray(t) && Array.isArray(t.sources) && t.sources.length > 0) {
1331
+ const r = {
1332
+ type: "data",
1333
+ data: { sources: t.sources }
1334
+ };
1335
+ s.push(r);
1336
+ }
1330
1337
  const a = e.role === "user" ? "user" : "agent", n = e.ts ? e.ts * 1e3 : (/* @__PURE__ */ new Date(
1331
1338
  e.created_at.replace(" ", "T") + "Z"
1332
1339
  )).getTime();
1333
1340
  return {
1334
1341
  role: a,
1335
1342
  kind: "message",
1336
- parts: t,
1343
+ parts: s,
1337
1344
  messageId: N(),
1338
1345
  metadata: {
1339
1346
  timestamp: n,
@@ -1351,9 +1358,9 @@ function lt(e) {
1351
1358
  return !1;
1352
1359
  }
1353
1360
  }
1354
- function ct(e, t = !1) {
1361
+ function ct(e, s = !1) {
1355
1362
  var r, o, c, l, d, u;
1356
- const a = e.messages.filter((p) => p.role === "tool_call" ? !1 : p.role === "tool_result" ? !(!t || lt(p)) : !0).map(it), n = {
1363
+ const a = e.messages.filter((p) => p.role === "tool_call" ? !1 : p.role === "tool_result" ? !(!s || lt(p)) : !0).map(it), n = {
1357
1364
  currentPage: ((r = e.metadata) == null ? void 0 : r.current_page) ?? 1,
1358
1365
  itemsPerPage: ((o = e.metadata) == null ? void 0 : o.items_per_page) ?? 10,
1359
1366
  totalPages: ((c = e.metadata) == null ? void 0 : c.total_pages) ?? 1,
@@ -1368,23 +1375,23 @@ function ct(e, t = !1) {
1368
1375
  };
1369
1376
  }
1370
1377
  class V extends Error {
1371
- constructor(t, s, a) {
1372
- super(t), this.statusCode = s, this.details = a, this.name = "ServerConversationError";
1378
+ constructor(s, t, a) {
1379
+ super(s), this.statusCode = t, this.details = a, this.name = "ServerConversationError";
1373
1380
  }
1374
1381
  }
1375
1382
  const fe = "https://public-api.wordpress.com";
1376
- async function le(e, t, s = 1, a = 50, n = !1) {
1377
- const { botId: r, apiBaseUrl: o = fe, authProvider: c } = t;
1383
+ async function le(e, s, t = 1, a = 50, n = !1) {
1384
+ const { botId: r, apiBaseUrl: o = fe, authProvider: c } = s;
1378
1385
  if (!e || !r)
1379
1386
  throw new Error(
1380
1387
  "chatId and botId are required to load conversation from server"
1381
1388
  );
1382
- const l = Math.max(1, Math.min(s, 100)), d = Math.max(1, Math.min(a, 100)), u = new URL(
1389
+ const l = Math.max(1, Math.min(t, 100)), d = Math.max(1, Math.min(a, 100)), u = new URL(
1383
1390
  `${o}/wpcom/v2/odie/chat/${encodeURIComponent(
1384
1391
  r
1385
1392
  )}/${encodeURIComponent(e)}`
1386
1393
  );
1387
- u.searchParams.set("page_number", l.toString()), u.searchParams.set("items_per_page", d.toString()), x(
1394
+ u.searchParams.set("page_number", l.toString()), u.searchParams.set("items_per_page", d.toString()), M(
1388
1395
  "Loading conversation from server: %s (page %d)",
1389
1396
  e,
1390
1397
  l
@@ -1403,21 +1410,21 @@ async function le(e, t, s = 1, a = 50, n = !1) {
1403
1410
  });
1404
1411
  if (!m.ok) {
1405
1412
  const h = await m.text();
1406
- let A = `Failed to load conversation from server: ${m.status} ${m.statusText}`;
1413
+ let x = `Failed to load conversation from server: ${m.status} ${m.statusText}`;
1407
1414
  try {
1408
1415
  const i = JSON.parse(h);
1409
- i.message && (A = i.message);
1416
+ i.message && (x = i.message);
1410
1417
  } catch {
1411
1418
  }
1412
1419
  const g = new V(
1413
- A,
1420
+ x,
1414
1421
  m.status,
1415
1422
  h
1416
1423
  );
1417
- throw x("Failed to load conversation from server: %O", g), g;
1424
+ throw M("Failed to load conversation from server: %O", g), g;
1418
1425
  }
1419
1426
  const f = await m.json(), w = ct(f, n);
1420
- return x(
1427
+ return M(
1421
1428
  "Loaded %d messages from server (page %d/%d)",
1422
1429
  w.messages.length,
1423
1430
  w.pagination.currentPage,
@@ -1431,19 +1438,19 @@ async function le(e, t, s = 1, a = 50, n = !1) {
1431
1438
  void 0,
1432
1439
  p
1433
1440
  );
1434
- throw x("Network error loading conversation: %O", p), m;
1441
+ throw M("Network error loading conversation: %O", p), m;
1435
1442
  }
1436
1443
  }
1437
- async function Kt(e, t, s = !1) {
1438
- const { apiBaseUrl: a = fe, authProvider: n } = t, r = new URL(
1444
+ async function Kt(e, s, t = !1) {
1445
+ const { apiBaseUrl: a = fe, authProvider: n } = s, r = new URL(
1439
1446
  `${a}/wpcom/v2/odie/conversations/${encodeURIComponent(
1440
1447
  e
1441
1448
  )}`
1442
1449
  );
1443
1450
  r.searchParams.set(
1444
1451
  "truncation_method",
1445
- s ? "first_message" : "last_message"
1446
- ), x("Listing conversations from server for bot: %s", e);
1452
+ t ? "first_message" : "last_message"
1453
+ ), M("Listing conversations from server for bot: %s", e);
1447
1454
  try {
1448
1455
  const o = {
1449
1456
  "Content-Type": "application/json"
@@ -1462,10 +1469,10 @@ async function Kt(e, t, s = !1) {
1462
1469
  c.status,
1463
1470
  d
1464
1471
  );
1465
- throw x("Failed to list conversations: %O", p), p;
1472
+ throw M("Failed to list conversations: %O", p), p;
1466
1473
  }
1467
1474
  const l = await c.json();
1468
- return x("Loaded %d conversations from server", l.length), l;
1475
+ return M("Loaded %d conversations from server", l.length), l;
1469
1476
  } catch (o) {
1470
1477
  if (o instanceof V)
1471
1478
  throw o;
@@ -1474,13 +1481,13 @@ async function Kt(e, t, s = !1) {
1474
1481
  void 0,
1475
1482
  o
1476
1483
  );
1477
- throw x("Network error listing conversations: %O", o), c;
1484
+ throw M("Network error listing conversations: %O", o), c;
1478
1485
  }
1479
1486
  }
1480
- async function Lt(e, t, s = 10, a = !1) {
1487
+ async function Lt(e, s, t = 10, a = !1) {
1481
1488
  const n = await le(
1482
1489
  e,
1483
- t,
1490
+ s,
1484
1491
  1,
1485
1492
  50,
1486
1493
  a
@@ -1489,11 +1496,11 @@ async function Lt(e, t, s = 10, a = !1) {
1489
1496
  return n;
1490
1497
  const r = Math.min(
1491
1498
  n.pagination.totalPages,
1492
- s
1499
+ t
1493
1500
  ), o = [...n.messages], c = [];
1494
1501
  for (let l = 2; l <= r; l++)
1495
1502
  c.push(
1496
- le(e, t, l, 50, a)
1503
+ le(e, s, l, 50, a)
1497
1504
  );
1498
1505
  try {
1499
1506
  const l = await Promise.all(c);
@@ -1509,7 +1516,7 @@ async function Lt(e, t, s = 10, a = !1) {
1509
1516
  chatId: n.chatId
1510
1517
  };
1511
1518
  } catch (l) {
1512
- return x("Failed to load all pages: %O", l), {
1519
+ return M("Failed to load all pages: %O", l), {
1513
1520
  messages: o,
1514
1521
  pagination: n.pagination,
1515
1522
  chatId: n.chatId
@@ -1519,10 +1526,10 @@ async function Lt(e, t, s = 10, a = !1) {
1519
1526
  const me = "a8c_agenttic_conversation_history";
1520
1527
  function ut(e) {
1521
1528
  var p, m;
1522
- const t = e.parts.filter(
1529
+ const s = e.parts.filter(
1523
1530
  (f) => f.type === "text"
1524
- ), s = t.map((f) => f.text).join(`
1525
- `), a = t.some(
1531
+ ), t = s.map((f) => f.text).join(`
1532
+ `), a = s.some(
1526
1533
  (f) => {
1527
1534
  var w;
1528
1535
  return ((w = f.metadata) == null ? void 0 : w.contentType) === "context";
@@ -1546,7 +1553,7 @@ function ut(e) {
1546
1553
  })), l = n.length > 0 || r.length > 0 ? "agent" : e.role, d = ((p = e.metadata) == null ? void 0 : p.timestamp) ?? Date.now(), u = ((m = e.metadata) == null ? void 0 : m.archived) ?? void 0;
1547
1554
  return {
1548
1555
  role: l,
1549
- content: s || "(No text content)",
1556
+ content: t || "(No text content)",
1550
1557
  timestamp: d,
1551
1558
  ...u !== void 0 && { archived: u },
1552
1559
  ...a && { contentType: a },
@@ -1556,8 +1563,8 @@ function ut(e) {
1556
1563
  };
1557
1564
  }
1558
1565
  function dt(e) {
1559
- const t = [];
1560
- if (e.content && e.content !== "(No text content)" && t.push({
1566
+ const s = [];
1567
+ if (e.content && e.content !== "(No text content)" && s.push({
1561
1568
  type: "text",
1562
1569
  text: e.content,
1563
1570
  ...e.contentType && {
@@ -1566,39 +1573,39 @@ function dt(e) {
1566
1573
  }
1567
1574
  }
1568
1575
  }), e.files)
1569
- for (const s of e.files)
1570
- t.push({
1576
+ for (const t of e.files)
1577
+ s.push({
1571
1578
  type: "file",
1572
1579
  file: {
1573
- name: s.name,
1574
- mimeType: s.mimeType,
1575
- uri: s.uri
1580
+ name: t.name,
1581
+ mimeType: t.mimeType,
1582
+ uri: t.uri
1576
1583
  }
1577
1584
  });
1578
1585
  if (e.toolCalls)
1579
- for (const s of e.toolCalls)
1580
- t.push({
1586
+ for (const t of e.toolCalls)
1587
+ s.push({
1581
1588
  type: "data",
1582
1589
  data: {
1583
- toolCallId: s.toolCallId,
1584
- toolId: s.toolId,
1585
- arguments: s.arguments
1590
+ toolCallId: t.toolCallId,
1591
+ toolId: t.toolId,
1592
+ arguments: t.arguments
1586
1593
  }
1587
1594
  });
1588
1595
  if (e.toolResults)
1589
- for (const s of e.toolResults)
1590
- t.push({
1596
+ for (const t of e.toolResults)
1597
+ s.push({
1591
1598
  type: "data",
1592
1599
  data: {
1593
- toolCallId: s.toolCallId,
1594
- result: s.result,
1595
- ...s.error && { error: s.error }
1600
+ toolCallId: t.toolCallId,
1601
+ result: t.result,
1602
+ ...t.error && { error: t.error }
1596
1603
  }
1597
1604
  });
1598
1605
  return {
1599
1606
  role: e.role,
1600
1607
  kind: "message",
1601
- parts: t,
1608
+ parts: s,
1602
1609
  messageId: N(),
1603
1610
  metadata: {
1604
1611
  timestamp: e.timestamp,
@@ -1610,9 +1617,9 @@ function dt(e) {
1610
1617
  };
1611
1618
  }
1612
1619
  const B = /* @__PURE__ */ new Map(), gt = 50;
1613
- async function ft(e, t, s) {
1614
- const a = s || e;
1615
- if (B.set(a, [...t]), B.size > gt) {
1620
+ async function ft(e, s, t) {
1621
+ const a = t || e;
1622
+ if (B.set(a, [...s]), B.size > gt) {
1616
1623
  const n = B.keys().next().value;
1617
1624
  n && B.delete(n);
1618
1625
  }
@@ -1620,7 +1627,7 @@ async function ft(e, t, s) {
1620
1627
  try {
1621
1628
  const n = {
1622
1629
  storageKey: a,
1623
- messages: t.map(ut),
1630
+ messages: s.map(ut),
1624
1631
  lastUpdated: Date.now()
1625
1632
  };
1626
1633
  sessionStorage.setItem(
@@ -1628,36 +1635,36 @@ async function ft(e, t, s) {
1628
1635
  JSON.stringify(n)
1629
1636
  );
1630
1637
  } catch (n) {
1631
- x(
1638
+ M(
1632
1639
  "Failed to store conversation in sessionStorage for key %s: %O",
1633
1640
  a,
1634
1641
  n
1635
1642
  );
1636
1643
  }
1637
1644
  }
1638
- async function mt(e, t, s) {
1639
- return s != null && s.odieBotId ? pt(e, s) : ht(
1645
+ async function mt(e, s, t) {
1646
+ return t != null && t.odieBotId ? pt(e, t) : ht(
1640
1647
  e,
1641
- t
1648
+ s
1642
1649
  );
1643
1650
  }
1644
- async function pt(e, t) {
1645
- const { odieBotId: s, authProvider: a } = t;
1646
- if (!s)
1651
+ async function pt(e, s) {
1652
+ const { odieBotId: t, authProvider: a } = s;
1653
+ if (!t)
1647
1654
  throw new Error("odieBotId is required for server storage");
1648
1655
  const n = fe;
1649
1656
  try {
1650
1657
  const o = await le(
1651
1658
  e,
1652
1659
  {
1653
- botId: s,
1660
+ botId: t,
1654
1661
  apiBaseUrl: n,
1655
1662
  authProvider: a
1656
1663
  },
1657
1664
  1,
1658
1665
  50
1659
1666
  );
1660
- return x(
1667
+ return M(
1661
1668
  "Loaded conversation from server: %s (%d messages, page %d/%d)",
1662
1669
  e,
1663
1670
  o.messages.length,
@@ -1668,52 +1675,52 @@ async function pt(e, t) {
1668
1675
  pagination: o.pagination
1669
1676
  };
1670
1677
  } catch (r) {
1671
- throw x("Failed to load conversation from server: %O", r), r;
1678
+ throw M("Failed to load conversation from server: %O", r), r;
1672
1679
  }
1673
1680
  }
1674
- async function ht(e, t) {
1675
- const s = t || e;
1676
- if (B.has(s))
1681
+ async function ht(e, s) {
1682
+ const t = s || e;
1683
+ if (B.has(t))
1677
1684
  return {
1678
- messages: [...B.get(s)]
1685
+ messages: [...B.get(t)]
1679
1686
  };
1680
1687
  if (typeof sessionStorage > "u")
1681
1688
  return { messages: [] };
1682
1689
  try {
1683
1690
  const a = sessionStorage.getItem(
1684
- `${me}_${s}`
1691
+ `${me}_${t}`
1685
1692
  );
1686
1693
  if (a) {
1687
1694
  const r = JSON.parse(a).messages.map(dt);
1688
- return B.set(s, r), { messages: [...r] };
1695
+ return B.set(t, r), { messages: [...r] };
1689
1696
  }
1690
1697
  } catch (a) {
1691
- x(
1698
+ M(
1692
1699
  "Failed to load conversation from sessionStorage for key %s: %O",
1693
- s,
1700
+ t,
1694
1701
  a
1695
1702
  );
1696
1703
  }
1697
1704
  return { messages: [] };
1698
1705
  }
1699
- async function yt(e, t) {
1700
- const s = t || e;
1701
- if (B.delete(s), !(typeof sessionStorage > "u"))
1706
+ async function yt(e, s) {
1707
+ const t = s || e;
1708
+ if (B.delete(t), !(typeof sessionStorage > "u"))
1702
1709
  try {
1703
- sessionStorage.removeItem(`${me}_${s}`);
1710
+ sessionStorage.removeItem(`${me}_${t}`);
1704
1711
  } catch (a) {
1705
- x(
1712
+ M(
1706
1713
  "Failed to clear conversation from sessionStorage for key %s: %O",
1707
- s,
1714
+ t,
1708
1715
  a
1709
1716
  );
1710
1717
  }
1711
1718
  }
1712
1719
  function z(e) {
1713
- const t = e.parts.filter((s) => s.type === "text" ? !0 : s.type === "data" ? "role" in s.data && "text" in s.data ? !1 : !!("toolCallId" in s.data && "arguments" in s.data || "flags" in s.data && s.data.flags && typeof s.data.flags == "object" && "forward_to_human_support" in s.data.flags || "toolCallId" in s.data && "result" in s.data) : !0);
1720
+ 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);
1714
1721
  return {
1715
1722
  ...e,
1716
- parts: t,
1723
+ parts: s,
1717
1724
  // Preserve metadata if it exists, otherwise add timestamp
1718
1725
  metadata: e.metadata || {
1719
1726
  timestamp: Date.now()
@@ -1721,35 +1728,35 @@ function z(e) {
1721
1728
  };
1722
1729
  }
1723
1730
  function It(e) {
1724
- const t = [];
1725
- for (const s of e)
1726
- for (const a of s.parts)
1731
+ const s = [];
1732
+ for (const t of e)
1733
+ for (const a of t.parts)
1727
1734
  if (a.type === "text")
1728
- t.push({
1735
+ s.push({
1729
1736
  type: "data",
1730
1737
  data: {
1731
- role: s.role,
1738
+ role: t.role,
1732
1739
  text: a.text
1733
1740
  }
1734
1741
  });
1735
1742
  else if (a.type === "file")
1736
- t.push(a);
1743
+ s.push(a);
1737
1744
  else if (a.type === "data") {
1738
1745
  if ("role" in a.data && "text" in a.data)
1739
1746
  continue;
1740
1747
  if ("toolCallId" in a.data && "arguments" in a.data) {
1741
- t.push(a);
1748
+ s.push(a);
1742
1749
  continue;
1743
1750
  }
1744
1751
  if ("toolCallId" in a.data && "result" in a.data) {
1745
- t.push(a);
1752
+ s.push(a);
1746
1753
  continue;
1747
1754
  }
1748
1755
  }
1749
- return t;
1756
+ return s;
1750
1757
  }
1751
- function we(e, t = [], s = []) {
1752
- const a = It(t), n = s.map((r) => {
1758
+ function we(e, s = [], t = []) {
1759
+ const a = It(s), n = t.map((r) => {
1753
1760
  const o = typeof r == "string" ? r : r.url, c = typeof r == "object" ? r.metadata : void 0, l = (c == null ? void 0 : c.fileType) || "image/jpeg";
1754
1761
  return {
1755
1762
  type: "file",
@@ -1780,67 +1787,67 @@ function we(e, t = [], s = []) {
1780
1787
  }
1781
1788
  function wt(e) {
1782
1789
  return e != null && e.parts ? e.parts.filter(
1783
- (t) => t.type === "data" && "toolCallId" in t.data && "result" in t.data
1790
+ (s) => s.type === "data" && "toolCallId" in s.data && "result" in s.data
1784
1791
  ) : [];
1785
1792
  }
1786
- function ne(e, t) {
1793
+ function ne(e, s) {
1787
1794
  if (typeof localStorage > "u") {
1788
1795
  K("localStorage not available, cannot update session ID");
1789
1796
  return;
1790
1797
  }
1791
1798
  try {
1792
- const s = localStorage.getItem(e);
1793
- if (s) {
1794
- const a = JSON.parse(s), n = a.sessionId;
1795
- a.sessionId = t, localStorage.setItem(e, JSON.stringify(a)), K(
1799
+ const t = localStorage.getItem(e);
1800
+ if (t) {
1801
+ const a = JSON.parse(t), n = a.sessionId;
1802
+ a.sessionId = s, localStorage.setItem(e, JSON.stringify(a)), K(
1796
1803
  "Updated localStorage[%s] session ID: %s -> %s",
1797
1804
  e,
1798
1805
  n,
1799
- t
1806
+ s
1800
1807
  );
1801
1808
  } else {
1802
1809
  const a = {
1803
- sessionId: t,
1810
+ sessionId: s,
1804
1811
  timestamp: Date.now()
1805
1812
  };
1806
1813
  localStorage.setItem(e, JSON.stringify(a)), K(
1807
1814
  "Created new session in localStorage[%s]: %s",
1808
1815
  e,
1809
- t
1816
+ s
1810
1817
  );
1811
1818
  }
1812
- } catch (s) {
1819
+ } catch (t) {
1813
1820
  K(
1814
1821
  "Failed to update localStorage sessionId to %s: %O",
1815
- t,
1816
- s
1822
+ s,
1823
+ t
1817
1824
  );
1818
1825
  }
1819
1826
  }
1820
1827
  async function Tt(e) {
1821
- const t = [];
1822
- for (const s of e)
1823
- if (s.parts && Array.isArray(s.parts))
1824
- if (s.parts.some(
1828
+ const s = [];
1829
+ for (const t of e)
1830
+ if (t.parts && Array.isArray(t.parts))
1831
+ if (t.parts.some(
1825
1832
  (n) => n.type === "data" && "toolCallId" in n.data && "result" in n.data
1826
1833
  )) {
1827
1834
  const n = ot(
1828
- s.parts
1835
+ t.parts
1829
1836
  );
1830
- t.push({
1831
- ...s,
1837
+ s.push({
1838
+ ...t,
1832
1839
  parts: n
1833
1840
  });
1834
1841
  } else
1835
- t.push(s);
1842
+ s.push(t);
1836
1843
  else
1837
- t.push(s);
1838
- return nt(), t;
1844
+ s.push(t);
1845
+ return nt(), s;
1839
1846
  }
1840
1847
  function St() {
1841
1848
  const e = /* @__PURE__ */ new Map();
1842
- async function t(s, a) {
1843
- const n = e.get(s);
1849
+ async function s(t, a) {
1850
+ const n = e.get(t);
1844
1851
  if (n != null && n.sessionId)
1845
1852
  try {
1846
1853
  await ft(
@@ -1850,15 +1857,15 @@ function St() {
1850
1857
  );
1851
1858
  } catch (r) {
1852
1859
  K(
1853
- `Failed to persist conversation history for agent ${s}:`,
1860
+ `Failed to persist conversation history for agent ${t}:`,
1854
1861
  r
1855
1862
  );
1856
1863
  }
1857
1864
  }
1858
1865
  return {
1859
- async createAgent(s, a) {
1860
- if (e.has(s))
1861
- return e.get(s).client;
1866
+ async createAgent(t, a) {
1867
+ if (e.has(t))
1868
+ return e.get(t).client;
1862
1869
  const n = rt(a), r = a.sessionId || null, o = a.conversationStorageKey, c = a.sessionIdStorageKey, l = {
1863
1870
  odieBotId: a.odieBotId,
1864
1871
  authProvider: a.authProvider
@@ -1873,7 +1880,7 @@ function St() {
1873
1880
  )).messages;
1874
1881
  } catch (p) {
1875
1882
  K(
1876
- `Failed to load conversation history for agent ${s} with session ${r}:`,
1883
+ `Failed to load conversation history for agent ${t} with session ${r}:`,
1877
1884
  p
1878
1885
  );
1879
1886
  }
@@ -1886,23 +1893,23 @@ function St() {
1886
1893
  conversationHistory: d,
1887
1894
  currentAbortController: null
1888
1895
  };
1889
- return e.set(s, u), n;
1896
+ return e.set(t, u), n;
1890
1897
  },
1891
- getAgent(s) {
1892
- const a = e.get(s);
1898
+ getAgent(t) {
1899
+ const a = e.get(t);
1893
1900
  return (a == null ? void 0 : a.client) || null;
1894
1901
  },
1895
- hasAgent(s) {
1896
- return e.has(s);
1902
+ hasAgent(t) {
1903
+ return e.has(t);
1897
1904
  },
1898
- removeAgent(s) {
1899
- return e.delete(s);
1905
+ removeAgent(t) {
1906
+ return e.delete(t);
1900
1907
  },
1901
- async sendMessage(s, a, n = {}) {
1902
- var A;
1903
- const r = e.get(s);
1908
+ async sendMessage(t, a, n = {}) {
1909
+ var x;
1910
+ const r = e.get(t);
1904
1911
  if (!r)
1905
- throw new Error(`Agent with key "${s}" not found`);
1912
+ throw new Error(`Agent with key "${t}" not found`);
1906
1913
  const { withHistory: o = !0, sessionId: c, ...l } = n, { client: d, conversationHistory: u } = r, p = n.message || we(
1907
1914
  a,
1908
1915
  u,
@@ -1925,7 +1932,7 @@ function St() {
1925
1932
  ));
1926
1933
  }
1927
1934
  let f = null;
1928
- if ((A = m.status) != null && A.message) {
1935
+ if ((x = m.status) != null && x.message) {
1929
1936
  const g = m.status.message.parts.filter(
1930
1937
  (T) => T.type === "data" && "toolCallId" in T.data && ("arguments" in T.data || "result" in T.data)
1931
1938
  ), i = m.status.message.parts.filter(
@@ -1958,23 +1965,23 @@ function St() {
1958
1965
  g
1959
1966
  ];
1960
1967
  }
1961
- return r.conversationHistory = h, o && await t(
1962
- s,
1968
+ return r.conversationHistory = h, o && await s(
1969
+ t,
1963
1970
  h
1964
1971
  ), m;
1965
1972
  },
1966
- async *sendMessageStream(s, a, n = {}) {
1973
+ async *sendMessageStream(t, a, n = {}) {
1967
1974
  var T, S, I, _, O, $;
1968
- const r = e.get(s);
1975
+ const r = e.get(t);
1969
1976
  if (!r)
1970
- throw new Error(`Agent with key "${s}" not found`);
1977
+ throw new Error(`Agent with key "${t}" not found`);
1971
1978
  const {
1972
1979
  withHistory: o = !0,
1973
1980
  abortSignal: c,
1974
1981
  metadata: l,
1975
1982
  sessionId: d,
1976
1983
  ...u
1977
- } = n, { client: p } = r, m = l ? (({ contentType: v, ...y }) => y)(l) : void 0, f = new AbortController();
1984
+ } = n, { client: p } = r, m = l ? (({ contentType: A, ...y }) => y)(l) : void 0, f = new AbortController();
1978
1985
  r.currentAbortController = f, c && c.addEventListener(
1979
1986
  "abort",
1980
1987
  () => f.abort()
@@ -1982,25 +1989,25 @@ function St() {
1982
1989
  let w = [
1983
1990
  ...r.conversationHistory
1984
1991
  ], h = [];
1985
- const A = await Tt(
1992
+ const x = await Tt(
1986
1993
  w
1987
1994
  );
1988
- r.conversationHistory = A, w = A, o && await t(
1989
- s,
1990
- A
1995
+ r.conversationHistory = x, w = x, o && await s(
1996
+ t,
1997
+ x
1991
1998
  );
1992
1999
  const g = n.message || we(
1993
2000
  a,
1994
- A,
2001
+ x,
1995
2002
  n.imageUrls
1996
2003
  );
1997
2004
  if (n.metadata && !n.message) {
1998
- const { contentType: v, ...y } = n.metadata;
1999
- if (v) {
2005
+ const { contentType: A, ...y } = n.metadata;
2006
+ if (A) {
2000
2007
  const C = g.parts[g.parts.length - 1];
2001
2008
  C && C.type === "text" && (C.metadata = {
2002
2009
  ...C.metadata,
2003
- contentType: v
2010
+ contentType: A
2004
2011
  });
2005
2012
  }
2006
2013
  Object.keys(y).length > 0 && (g.metadata = {
@@ -2010,30 +2017,30 @@ function St() {
2010
2017
  }
2011
2018
  const i = ie(a, n.metadata);
2012
2019
  if (n.imageUrls && n.imageUrls.length > 0) {
2013
- const v = n.imageUrls.map(
2020
+ const A = n.imageUrls.map(
2014
2021
  (y) => {
2015
- const C = typeof y == "string" ? y : y.url, M = typeof y == "string" ? void 0 : y.metadata, R = (M == null ? void 0 : M.fileType) || "image/jpeg";
2022
+ const C = typeof y == "string" ? y : y.url, v = typeof y == "string" ? void 0 : y.metadata, P = (v == null ? void 0 : v.fileType) || "image/jpeg";
2016
2023
  return {
2017
2024
  type: "file",
2018
2025
  file: {
2019
- name: (M == null ? void 0 : M.fileName) || "image",
2020
- mimeType: R,
2026
+ name: (v == null ? void 0 : v.fileName) || "image",
2027
+ mimeType: P,
2021
2028
  uri: C
2022
2029
  },
2023
- metadata: M
2030
+ metadata: v
2024
2031
  };
2025
2032
  }
2026
2033
  );
2027
- i.parts.push(...v);
2034
+ i.parts.push(...A);
2028
2035
  }
2029
2036
  w = [
2030
2037
  ...w,
2031
2038
  i
2032
- ], r.conversationHistory = w, o && await t(
2033
- s,
2039
+ ], r.conversationHistory = w, o && await s(
2040
+ t,
2034
2041
  w
2035
2042
  );
2036
- for await (const v of p.sendMessageStream({
2043
+ for await (const A of p.sendMessageStream({
2037
2044
  message: g,
2038
2045
  withHistory: o,
2039
2046
  sessionId: d || r.sessionId || void 0,
@@ -2043,43 +2050,43 @@ function St() {
2043
2050
  metadata: m
2044
2051
  }
2045
2052
  })) {
2046
- if (v.sessionId) {
2053
+ if (A.sessionId) {
2047
2054
  const y = r.sessionId;
2048
- r.sessionId = v.sessionId, v.sessionId && y !== v.sessionId && r.sessionIdStorageKey && (K(
2055
+ r.sessionId = A.sessionId, A.sessionId && y !== A.sessionId && r.sessionIdStorageKey && (K(
2049
2056
  "Session ID %s, updating localStorage",
2050
- y ? `changed from ${y} to ${v.sessionId}` : `received: ${v.sessionId}`
2057
+ y ? `changed from ${y} to ${A.sessionId}` : `received: ${A.sessionId}`
2051
2058
  ), ne(
2052
2059
  r.sessionIdStorageKey,
2053
- v.sessionId
2060
+ A.sessionId
2054
2061
  ));
2055
2062
  }
2056
- if (((T = v.status) == null ? void 0 : T.state) === "input-required" && ((S = v.status) != null && S.message)) {
2063
+ if (((T = A.status) == null ? void 0 : T.state) === "input-required" && ((S = A.status) != null && S.message)) {
2057
2064
  h = L(
2058
- v.status.message
2065
+ A.status.message
2059
2066
  ).map(
2060
- (M) => M.data.toolCallId
2067
+ (v) => v.data.toolCallId
2061
2068
  );
2062
2069
  const C = z(
2063
- v.status.message
2070
+ A.status.message
2064
2071
  );
2065
2072
  w = [
2066
2073
  ...w,
2067
2074
  C
2068
- ], r.conversationHistory = w, o && await t(
2069
- s,
2075
+ ], r.conversationHistory = w, o && await s(
2076
+ t,
2070
2077
  w
2071
2078
  );
2072
2079
  }
2073
- if (((I = v.status) == null ? void 0 : I.state) === "working" && ((_ = v.status) != null && _.message) && !v.final) {
2080
+ if (((I = A.status) == null ? void 0 : I.state) === "working" && ((_ = A.status) != null && _.message) && !A.final) {
2074
2081
  const C = wt(
2075
- v.status.message
2082
+ A.status.message
2076
2083
  ).filter(
2077
- (M) => h.includes(
2078
- M.data.toolCallId
2084
+ (v) => h.includes(
2085
+ v.data.toolCallId
2079
2086
  )
2080
2087
  );
2081
2088
  if (C.length > 0) {
2082
- const M = {
2089
+ const v = {
2083
2090
  role: "agent",
2084
2091
  kind: "message",
2085
2092
  parts: C,
@@ -2087,64 +2094,64 @@ function St() {
2087
2094
  };
2088
2095
  w = [
2089
2096
  ...w,
2090
- z(M)
2091
- ], r.conversationHistory = w, o && await t(
2092
- s,
2097
+ z(v)
2098
+ ], r.conversationHistory = w, o && await s(
2099
+ t,
2093
2100
  w
2094
2101
  );
2095
2102
  }
2096
2103
  }
2097
- if (v.final && ((O = v.status) == null ? void 0 : O.state) !== "input-required") {
2104
+ if (A.final && ((O = A.status) == null ? void 0 : O.state) !== "input-required") {
2098
2105
  h = [];
2099
2106
  let y = null;
2100
- ($ = v.status) != null && $.message && (y = z(
2101
- v.status.message
2107
+ ($ = A.status) != null && $.message && (y = z(
2108
+ A.status.message
2102
2109
  ), w = [
2103
2110
  ...w,
2104
2111
  y
2105
- ], r.conversationHistory = w, o && await t(
2106
- s,
2112
+ ], r.conversationHistory = w, o && await s(
2113
+ t,
2107
2114
  w
2108
2115
  ));
2109
2116
  }
2110
- yield v;
2117
+ yield A;
2111
2118
  }
2112
2119
  r.currentAbortController = null;
2113
2120
  },
2114
- async resetConversation(s) {
2115
- const a = e.get(s);
2121
+ async resetConversation(t) {
2122
+ const a = e.get(t);
2116
2123
  if (!a)
2117
- throw new Error(`Agent with key "${s}" not found`);
2124
+ throw new Error(`Agent with key "${t}" not found`);
2118
2125
  a.conversationHistory = [], a.sessionId && await yt(
2119
2126
  a.sessionId,
2120
2127
  a.conversationStorageKey
2121
2128
  );
2122
2129
  },
2123
- async replaceMessages(s, a) {
2124
- const n = e.get(s);
2130
+ async replaceMessages(t, a) {
2131
+ const n = e.get(t);
2125
2132
  if (!n)
2126
- throw new Error(`Agent with key "${s}" not found`);
2127
- n.conversationHistory = [...a], n.sessionId && await t(s, a);
2133
+ throw new Error(`Agent with key "${t}" not found`);
2134
+ n.conversationHistory = [...a], n.sessionId && await s(t, a);
2128
2135
  },
2129
- getConversationHistory(s) {
2130
- const a = e.get(s);
2136
+ getConversationHistory(t) {
2137
+ const a = e.get(t);
2131
2138
  if (!a)
2132
- throw new Error(`Agent with key "${s}" not found`);
2139
+ throw new Error(`Agent with key "${t}" not found`);
2133
2140
  return [...a.conversationHistory];
2134
2141
  },
2135
- updateSessionId(s, a) {
2136
- const n = e.get(s);
2142
+ updateSessionId(t, a) {
2143
+ const n = e.get(t);
2137
2144
  if (!n)
2138
- throw new Error(`Agent with key "${s}" not found`);
2145
+ throw new Error(`Agent with key "${t}" not found`);
2139
2146
  n.sessionId = a, n.sessionIdStorageKey && ne(
2140
2147
  n.sessionIdStorageKey,
2141
2148
  a
2142
2149
  );
2143
2150
  },
2144
- abortCurrentRequest(s) {
2145
- const a = e.get(s);
2151
+ abortCurrentRequest(t) {
2152
+ const a = e.get(t);
2146
2153
  if (!a)
2147
- throw new Error(`Agent with key "${s}" not found`);
2154
+ throw new Error(`Agent with key "${t}" not found`);
2148
2155
  a.currentAbortController && (a.currentAbortController.abort(), a.currentAbortController = null);
2149
2156
  },
2150
2157
  clear() {
@@ -2152,14 +2159,14 @@ function St() {
2152
2159
  }
2153
2160
  };
2154
2161
  }
2155
- const vt = St();
2162
+ const At = St();
2156
2163
  function Q() {
2157
- return vt;
2164
+ return At;
2158
2165
  }
2159
- function Mt() {
2160
- const [e, t] = ve([]), s = F(
2166
+ function vt() {
2167
+ const [e, s] = Ae([]), t = F(
2161
2168
  (r) => {
2162
- t((o) => {
2169
+ s((o) => {
2163
2170
  const c = o.findIndex(
2164
2171
  (l) => l.id === r.id
2165
2172
  );
@@ -2172,19 +2179,19 @@ function Mt() {
2172
2179
  },
2173
2180
  []
2174
2181
  ), a = F((r) => {
2175
- t((o) => o.filter((c) => c.id !== r));
2182
+ s((o) => o.filter((c) => c.id !== r));
2176
2183
  }, []), n = F(() => {
2177
- t([]);
2184
+ s([]);
2178
2185
  }, []);
2179
2186
  return {
2180
- registerMessageActions: s,
2187
+ registerMessageActions: t,
2181
2188
  unregisterMessageActions: a,
2182
2189
  clearAllMessageActions: n,
2183
2190
  registrations: e
2184
2191
  };
2185
2192
  }
2186
- function xt(e, t) {
2187
- return t.flatMap((n) => typeof n.actions == "function" ? n.actions(e) : n.actions).filter((n) => !(n.condition && !n.condition(e))).map((n) => ({
2193
+ function Mt(e, s) {
2194
+ return s.flatMap((n) => typeof n.actions == "function" ? n.actions(e) : n.actions).filter((n) => !(n.condition && !n.condition(e))).map((n) => ({
2188
2195
  id: n.id,
2189
2196
  label: n.label,
2190
2197
  icon: n.icon,
@@ -2195,13 +2202,13 @@ function xt(e, t) {
2195
2202
  showLabel: n.showLabel
2196
2203
  }));
2197
2204
  }
2198
- const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t = "40%") => ({
2205
+ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s = "40%") => ({
2199
2206
  type: "component",
2200
2207
  component: () => qe.createElement("img", {
2201
2208
  src: e,
2202
2209
  alt: "Uploaded image",
2203
2210
  style: {
2204
- maxWidth: t,
2211
+ maxWidth: s,
2205
2212
  height: "auto",
2206
2213
  borderRadius: "8px",
2207
2214
  marginTop: "8px",
@@ -2209,7 +2216,7 @@ const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t =
2209
2216
  display: "inline-block"
2210
2217
  }
2211
2218
  })
2212
- }), re = (e, t = []) => {
2219
+ }), re = (e, s = []) => {
2213
2220
  var o, c;
2214
2221
  if (e.parts.some((l) => {
2215
2222
  if (l.type === "data") {
@@ -2240,6 +2247,9 @@ const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t =
2240
2247
  } : u.flags && typeof u.flags == "object" && "forward_to_human_support" in u.flags ? {
2241
2248
  type: "data",
2242
2249
  data: u
2250
+ } : Array.isArray(u.sources) && u.sources.length > 0 ? {
2251
+ type: "data",
2252
+ data: u
2243
2253
  } : {
2244
2254
  type: "text",
2245
2255
  text: JSON.stringify(u)
@@ -2258,15 +2268,15 @@ const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t =
2258
2268
  showIcon: e.role === "agent",
2259
2269
  icon: e.role === "agent" ? "assistant" : void 0
2260
2270
  };
2261
- if (e.role === "agent" && t.length > 0) {
2262
- const l = xt(
2271
+ if (e.role === "agent" && s.length > 0) {
2272
+ const l = Mt(
2263
2273
  r,
2264
- t
2274
+ s
2265
2275
  );
2266
2276
  l.length > 0 && (r.actions = l);
2267
2277
  }
2268
2278
  return r;
2269
- }, At = () => ({
2279
+ }, xt = () => ({
2270
2280
  getClientContext: () => ({})
2271
2281
  }), Ct = () => ({
2272
2282
  getAvailableTools: async () => [],
@@ -2274,21 +2284,21 @@ const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t =
2274
2284
  success: !0,
2275
2285
  result: "No tools available"
2276
2286
  })
2277
- }), bt = (e) => ["agentId", "agentUrl"].every((s) => {
2278
- const a = e[s];
2287
+ }), bt = (e) => ["agentId", "agentUrl"].every((t) => {
2288
+ const a = e[t];
2279
2289
  return typeof a == "string" && a.trim().length > 0;
2280
2290
  });
2281
2291
  function Bt(e) {
2282
- const t = {
2292
+ const s = {
2283
2293
  agentId: e.agentId,
2284
2294
  agentUrl: e.agentUrl,
2285
2295
  sessionId: e.sessionId,
2286
2296
  sessionIdStorageKey: e.sessionIdStorageKey
2287
- }, s = bt(t), [a, n] = ve({
2297
+ }, t = bt(s), [a, n] = Ae({
2288
2298
  clientMessages: [],
2289
2299
  uiMessages: [],
2290
2300
  isProcessing: !1,
2291
- error: s ? null : "Invalid agent configuration",
2301
+ error: t ? null : "Invalid agent configuration",
2292
2302
  suggestions: [],
2293
2303
  progressMessage: null,
2294
2304
  progressPhase: null
@@ -2297,7 +2307,7 @@ function Bt(e) {
2297
2307
  unregisterMessageActions: o,
2298
2308
  clearAllMessageActions: c,
2299
2309
  registrations: l
2300
- } = Mt(), d = He(l);
2310
+ } = vt(), d = He(l);
2301
2311
  se(() => {
2302
2312
  d.current = l;
2303
2313
  }, [l]);
@@ -2309,12 +2319,12 @@ function Bt(e) {
2309
2319
  // registrationsRef is stable, so no deps needed
2310
2320
  );
2311
2321
  se(() => {
2312
- if (!s)
2322
+ if (!t)
2313
2323
  return;
2314
2324
  (async () => {
2315
- const i = Q(), T = t.agentId;
2325
+ const i = Q(), T = s.agentId;
2316
2326
  if (i.hasAgent(T))
2317
- t.sessionId ? (i.updateSessionId(T, t.sessionId), i.getConversationHistory(T).length === 0 && n((_) => ({
2327
+ s.sessionId ? (i.updateSessionId(T, s.sessionId), i.getConversationHistory(T).length === 0 && n((_) => ({
2318
2328
  ..._,
2319
2329
  clientMessages: [],
2320
2330
  uiMessages: []
@@ -2324,17 +2334,17 @@ function Bt(e) {
2324
2334
  uiMessages: []
2325
2335
  })));
2326
2336
  else if (await i.createAgent(T, {
2327
- agentId: t.agentId,
2328
- agentUrl: t.agentUrl,
2329
- sessionId: t.sessionId,
2337
+ agentId: s.agentId,
2338
+ agentUrl: s.agentUrl,
2339
+ sessionId: s.sessionId,
2330
2340
  // Can be empty for new chats
2331
- sessionIdStorageKey: t.sessionIdStorageKey,
2332
- contextProvider: e.contextProvider || At(),
2341
+ sessionIdStorageKey: s.sessionIdStorageKey,
2342
+ contextProvider: e.contextProvider || xt(),
2333
2343
  toolProvider: e.toolProvider || Ct(),
2334
2344
  authProvider: e.authProvider,
2335
2345
  enableStreaming: e.enableStreaming,
2336
2346
  odieBotId: e.odieBotId
2337
- }), t.sessionId) {
2347
+ }), s.sessionId) {
2338
2348
  const I = i.getConversationHistory(T);
2339
2349
  n((_) => {
2340
2350
  const O = u(I);
@@ -2352,23 +2362,23 @@ function Bt(e) {
2352
2362
  }));
2353
2363
  })();
2354
2364
  }, [
2355
- t.sessionId,
2356
- t.agentId,
2357
- t.agentUrl,
2358
- t.sessionIdStorageKey,
2365
+ s.sessionId,
2366
+ s.agentId,
2367
+ s.agentUrl,
2368
+ s.sessionIdStorageKey,
2359
2369
  e.contextProvider,
2360
2370
  e.toolProvider,
2361
2371
  e.authProvider,
2362
2372
  e.enableStreaming,
2363
2373
  e.odieBotId,
2364
- s
2374
+ t
2365
2375
  ]);
2366
2376
  const p = F(
2367
2377
  async (g, i) => {
2368
- var $, v;
2369
- if (!s)
2378
+ var $, A;
2379
+ if (!t)
2370
2380
  throw new Error("Invalid agent configuration");
2371
- const T = Q(), S = t.agentId, I = Date.now(), _ = (i == null ? void 0 : i.type) || "text", O = {
2381
+ const T = Q(), S = s.agentId, I = Date.now(), _ = (i == null ? void 0 : i.type) || "text", O = {
2372
2382
  id: `user-${I}`,
2373
2383
  role: "user",
2374
2384
  content: [
@@ -2391,21 +2401,21 @@ function Bt(e) {
2391
2401
  }));
2392
2402
  try {
2393
2403
  let y = null, C = !1;
2394
- const M = {};
2395
- (i != null && i.archived || i != null && i.type) && (M.metadata = {
2404
+ const v = {};
2405
+ (i != null && i.archived || i != null && i.type) && (v.metadata = {
2396
2406
  ...(i == null ? void 0 : i.archived) && { archived: !0 },
2397
2407
  ...(i == null ? void 0 : i.type) && { contentType: i.type }
2398
- }), i != null && i.sessionId && (M.sessionId = i.sessionId), i != null && i.imageUrls && (M.imageUrls = i.imageUrls);
2399
- for await (const R of T.sendMessageStream(
2408
+ }), i != null && i.sessionId && (v.sessionId = i.sessionId), i != null && i.imageUrls && (v.imageUrls = i.imageUrls);
2409
+ for await (const P of T.sendMessageStream(
2400
2410
  S,
2401
2411
  g,
2402
- M
2412
+ v
2403
2413
  )) {
2404
- if ((R.progressMessage || R.progressPhase) && n((k) => ({
2414
+ if ((P.progressMessage || P.progressPhase) && n((k) => ({
2405
2415
  ...k,
2406
- progressMessage: R.progressMessage || null,
2407
- progressPhase: R.progressPhase || null
2408
- })), !R.final && R.text)
2416
+ progressMessage: P.progressMessage || null,
2417
+ progressPhase: P.progressPhase || null
2418
+ })), !P.final && P.text)
2409
2419
  if (y)
2410
2420
  n((k) => ({
2411
2421
  ...k,
@@ -2415,7 +2425,7 @@ function Bt(e) {
2415
2425
  content: [
2416
2426
  {
2417
2427
  type: "text",
2418
- text: R.text
2428
+ text: P.text
2419
2429
  }
2420
2430
  ]
2421
2431
  } : b
@@ -2427,7 +2437,7 @@ function Bt(e) {
2427
2437
  id: y,
2428
2438
  role: "agent",
2429
2439
  content: [
2430
- { type: "text", text: R.text }
2440
+ { type: "text", text: P.text }
2431
2441
  ],
2432
2442
  timestamp: Date.now(),
2433
2443
  archived: !1,
@@ -2444,14 +2454,14 @@ function Bt(e) {
2444
2454
  ]
2445
2455
  }));
2446
2456
  }
2447
- if (R.final && ((v = R.status) != null && v.message) && y) {
2457
+ if (P.final && ((A = P.status) != null && A.message) && y) {
2448
2458
  C = !0;
2449
2459
  const k = y, b = re(
2450
- R.status.message,
2460
+ P.status.message,
2451
2461
  d.current
2452
2462
  );
2453
- b && n((P) => {
2454
- const U = P.uiMessages.map(
2463
+ b && n((R) => {
2464
+ const U = R.uiMessages.map(
2455
2465
  (q) => {
2456
2466
  var E, J;
2457
2467
  if (q.id === k) {
@@ -2469,7 +2479,7 @@ function Bt(e) {
2469
2479
  S
2470
2480
  );
2471
2481
  return {
2472
- ...P,
2482
+ ...R,
2473
2483
  clientMessages: D,
2474
2484
  uiMessages: U,
2475
2485
  isProcessing: !1,
@@ -2480,13 +2490,13 @@ function Bt(e) {
2480
2490
  }
2481
2491
  }
2482
2492
  if (!C) {
2483
- const R = T.getConversationHistory(S);
2493
+ const P = T.getConversationHistory(S);
2484
2494
  n((k) => {
2485
2495
  let b = k.uiMessages;
2486
2496
  y && (b = k.uiMessages.filter(
2487
2497
  (E) => E.id !== y
2488
2498
  ));
2489
- const P = R.map(
2499
+ const R = P.map(
2490
2500
  (E) => re(
2491
2501
  E,
2492
2502
  d.current
@@ -2494,16 +2504,16 @@ function Bt(e) {
2494
2504
  ).filter(
2495
2505
  (E) => E !== null
2496
2506
  ), U = new Set(
2497
- R.map((E) => E.messageId)
2507
+ P.map((E) => E.messageId)
2498
2508
  ), D = b.filter(
2499
2509
  (E) => !U.has(E.id) && E.role !== "user"
2500
2510
  ), q = oe([
2501
- ...P,
2511
+ ...R,
2502
2512
  ...D
2503
2513
  ]);
2504
2514
  return {
2505
2515
  ...k,
2506
- clientMessages: R,
2516
+ clientMessages: P,
2507
2517
  uiMessages: q,
2508
2518
  isProcessing: !1,
2509
2519
  progressMessage: null,
@@ -2513,8 +2523,8 @@ function Bt(e) {
2513
2523
  }
2514
2524
  } catch (y) {
2515
2525
  if (y instanceof Error && y.name === "AbortError") {
2516
- x("Request was aborted by user"), n((M) => ({
2517
- ...M,
2526
+ M("Request was aborted by user"), n((v) => ({
2527
+ ...v,
2518
2528
  isProcessing: !1,
2519
2529
  progressMessage: null,
2520
2530
  progressPhase: null,
@@ -2524,8 +2534,8 @@ function Bt(e) {
2524
2534
  return;
2525
2535
  }
2526
2536
  const C = y instanceof Error ? y.message : "Failed to send message";
2527
- throw n((M) => ({
2528
- ...M,
2537
+ throw n((v) => ({
2538
+ ...v,
2529
2539
  isProcessing: !1,
2530
2540
  progressMessage: null,
2531
2541
  progressPhase: null,
@@ -2533,7 +2543,7 @@ function Bt(e) {
2533
2543
  })), y;
2534
2544
  }
2535
2545
  },
2536
- [t.agentId, s]
2546
+ [s.agentId, t]
2537
2547
  ), m = F((g) => {
2538
2548
  n((i) => ({
2539
2549
  ...i,
@@ -2569,15 +2579,15 @@ function Bt(e) {
2569
2579
  });
2570
2580
  }, [l]);
2571
2581
  const h = F(() => {
2572
- if (!s)
2582
+ if (!t)
2573
2583
  return;
2574
- const g = Q(), i = t.agentId;
2584
+ const g = Q(), i = s.agentId;
2575
2585
  g.abortCurrentRequest(i);
2576
- }, [t.agentId, s]), A = F(
2586
+ }, [s.agentId, t]), x = F(
2577
2587
  async (g) => {
2578
- if (!s)
2588
+ if (!t)
2579
2589
  return;
2580
- const i = Q(), T = t.agentId;
2590
+ const i = Q(), T = s.agentId;
2581
2591
  await i.replaceMessages(T, g);
2582
2592
  const S = u(g);
2583
2593
  n((I) => ({
@@ -2586,7 +2596,7 @@ function Bt(e) {
2586
2596
  uiMessages: S
2587
2597
  }));
2588
2598
  },
2589
- [t.agentId, s]
2599
+ [s.agentId, t]
2590
2600
  );
2591
2601
  return {
2592
2602
  // AgentUI props
@@ -2609,26 +2619,26 @@ function Bt(e) {
2609
2619
  // Abort control
2610
2620
  abortCurrentRequest: h,
2611
2621
  // Conversation loading
2612
- loadMessages: A
2622
+ loadMessages: x
2613
2623
  };
2614
2624
  }
2615
2625
  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 || {});
2616
- const Te = "jetpack-ai-jwt-token", Rt = 30 * 60 * 1e3;
2617
- function Pt() {
2618
- var s, a, n;
2619
- return ((a = (s = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : s.connectionStatus) == null ? void 0 : a.isRegistered) ? !1 : !!((n = window.Jetpack_Editor_Initial_State) != null && n.wpcomBlogId);
2626
+ const Te = "jetpack-ai-jwt-token", Pt = 30 * 60 * 1e3;
2627
+ function Rt() {
2628
+ var t, a, n;
2629
+ 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);
2620
2630
  }
2621
- async function _t(e, t = !0) {
2631
+ async function _t(e, s = !0) {
2622
2632
  var l, d;
2623
- const s = localStorage.getItem(Te);
2633
+ const t = localStorage.getItem(Te);
2624
2634
  let a;
2625
- if (s)
2635
+ if (t)
2626
2636
  try {
2627
- a = JSON.parse(s);
2637
+ a = JSON.parse(t);
2628
2638
  } catch (u) {
2629
- x("Invalid cached Jetpack token: %O", u);
2639
+ M("Invalid cached Jetpack token: %O", u);
2630
2640
  }
2631
- if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() && t)
2641
+ if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() && s)
2632
2642
  return a;
2633
2643
  const n = (l = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : l.apiNonce, r = (d = window.Jetpack_Editor_Initial_State) == null ? void 0 : d.wpcomBlogId;
2634
2644
  let o = {
@@ -2636,7 +2646,7 @@ async function _t(e, t = !0) {
2636
2646
  blog_id: ""
2637
2647
  };
2638
2648
  try {
2639
- Pt() ? o = await pe({
2649
+ Rt() ? o = await pe({
2640
2650
  path: "/wpcom/v2/sites/" + r + "/jetpack-openai-query/jwt",
2641
2651
  method: "POST"
2642
2652
  }) : o = await pe({
@@ -2648,7 +2658,7 @@ async function _t(e, t = !0) {
2648
2658
  method: "POST"
2649
2659
  });
2650
2660
  } catch (u) {
2651
- throw x("Failed to fetch Jetpack token: %O", u), new Error(e(u));
2661
+ throw M("Failed to fetch Jetpack token: %O", u), new Error(e(u));
2652
2662
  }
2653
2663
  if (!(o != null && o.token))
2654
2664
  throw new Error(
@@ -2657,27 +2667,27 @@ async function _t(e, t = !0) {
2657
2667
  const c = {
2658
2668
  token: o.token,
2659
2669
  blogId: o.blog_id || "",
2660
- expire: Date.now() + Rt
2670
+ expire: Date.now() + Pt
2661
2671
  };
2662
2672
  try {
2663
2673
  localStorage.setItem(Te, JSON.stringify(c));
2664
2674
  } catch (u) {
2665
- x("Error storing token in localStorage: %O", u);
2675
+ M("Error storing token in localStorage: %O", u);
2666
2676
  }
2667
2677
  return c;
2668
2678
  }
2669
2679
  const Wt = (e) => async () => {
2670
- const t = {};
2680
+ const s = {};
2671
2681
  try {
2672
- const s = await _t(e);
2673
- s != null && s.token && (t.Authorization = `${s.token}`);
2674
- } catch (s) {
2675
- throw x("Failed to get Jetpack token for auth: %O", s), s;
2682
+ const t = await _t(e);
2683
+ t != null && t.token && (s.Authorization = `${t.token}`);
2684
+ } catch (t) {
2685
+ throw M("Failed to get Jetpack token for auth: %O", t), t;
2676
2686
  }
2677
- return t;
2687
+ return s;
2678
2688
  };
2679
2689
  function Et(e) {
2680
- const t = {
2690
+ const s = {
2681
2691
  type: "object",
2682
2692
  properties: {}
2683
2693
  };
@@ -2685,7 +2695,7 @@ function Et(e) {
2685
2695
  id: e.name.replace(/\//g, "-"),
2686
2696
  name: e.label,
2687
2697
  description: e.description,
2688
- input_schema: e.input_schema || t,
2698
+ input_schema: e.input_schema || s,
2689
2699
  // Store metadata for detection and execution
2690
2700
  _source: "wordpress-ability",
2691
2701
  _originalAbility: e
@@ -2697,24 +2707,24 @@ function Vt(e) {
2697
2707
  function zt(e) {
2698
2708
  return (e == null ? void 0 : e._source) === "wordpress-ability";
2699
2709
  }
2700
- function Gt(e, t = "agent", s = "wpcom") {
2710
+ function Gt(e, s = "agent", t = "wpcom") {
2701
2711
  const a = e.replace(/-/g, "_");
2702
- return `${s}-${t}-${a}`;
2712
+ return `${t}-${s}-${a}`;
2703
2713
  }
2704
2714
  function Ot(e) {
2705
- const t = e.split("-");
2706
- if (t.length < 3)
2715
+ const s = e.split("-");
2716
+ if (s.length < 3)
2707
2717
  throw new Error(
2708
2718
  `Invalid Odie bot ID format: ${e}. Expected format: {product}-{type}-{slug}`
2709
2719
  );
2710
- const s = t[0], a = t[1];
2720
+ const t = s[0], a = s[1];
2711
2721
  if (!["agent", "workflow", "chain"].includes(a))
2712
2722
  throw new Error(
2713
2723
  `Invalid Odie bot type: ${a}. Expected one of: agent, workflow, chain`
2714
2724
  );
2715
- const n = a, r = t.slice(2).join("-"), o = r.replace(/_/g, "-");
2725
+ const n = a, r = s.slice(2).join("-"), o = r.replace(/_/g, "-");
2716
2726
  return {
2717
- product: s,
2727
+ product: t,
2718
2728
  type: n,
2719
2729
  slug: r,
2720
2730
  agentId: o
@@ -2722,8 +2732,8 @@ function Ot(e) {
2722
2732
  }
2723
2733
  function Qt(e) {
2724
2734
  try {
2725
- const t = Ot(e);
2726
- return t.product.length > 0 && ["agent", "workflow", "chain"].includes(t.type) && t.slug.length > 0;
2735
+ const s = Ot(e);
2736
+ return s.product.length > 0 && ["agent", "workflow", "chain"].includes(s.type) && s.slug.length > 0;
2727
2737
  } catch {
2728
2738
  return !1;
2729
2739
  }
@@ -2753,5 +2763,5 @@ export {
2753
2763
  Ht as useClientContext,
2754
2764
  Ft as useClientTools,
2755
2765
  Dt as useClientToolsWithAbilities,
2756
- Mt as useMessageActions
2766
+ vt as useMessageActions
2757
2767
  };