@automattic/agenttic-client 0.1.21 → 0.1.23

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,28 +1,16 @@
1
1
  var Rt = Object.defineProperty;
2
2
  var Et = (t, e, s) => e in t ? Rt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
3
3
  var Z = (t, e, s) => Et(t, typeof e != "symbol" ? e + "" : e, s);
4
- import { useMemo as pt, useCallback as $, useState as yt, useRef as Ot, useEffect as tt } from "react";
5
- import ut from "@wordpress/api-fetch";
6
- const R = (t, ...e) => {
7
- _t() && console.log(`[agenttic-client] ${t}`, ...e);
8
- };
9
- function _t() {
10
- var t;
11
- return typeof globalThis < "u" && "window" in globalThis && ((t = globalThis.window) == null ? void 0 : t.DEBUG) === "agenttic-client";
12
- }
13
- function wt(t) {
14
- return JSON.stringify(t, null, 2);
15
- }
16
- function dt(t, ...e) {
17
- console.log(`[agenttic-client] ${t}`, ...e);
18
- }
4
+ import { useMemo as ht, useCallback as $, useState as yt, useRef as Ot, useEffect as tt } from "react";
5
+ import { l as v, f as wt, a as ut } from "./logger-aKHbTlwk.js";
6
+ import dt from "@wordpress/api-fetch";
19
7
  function Ae(t) {
20
- return pt(() => t ? {
8
+ return ht(() => t ? {
21
9
  getClientContext: () => {
22
10
  try {
23
11
  return t() || {};
24
- } catch (n) {
25
- return R("Error getting client context: %O", n), {};
12
+ } catch (a) {
13
+ return v("Error getting client context: %O", a), {};
26
14
  }
27
15
  }
28
16
  } : void 0, [t]);
@@ -32,36 +20,36 @@ function ot({
32
20
  executeTool: e,
33
21
  getAbilities: s
34
22
  }) {
35
- const n = $(async () => {
23
+ const a = $(async () => {
36
24
  const o = [];
37
25
  if (t)
38
26
  try {
39
- const c = await t();
40
- o.push(...c);
41
- } catch (c) {
42
- R("Error getting available tools: %O", c);
27
+ const l = await t();
28
+ o.push(...l);
29
+ } catch (l) {
30
+ v("Error getting available tools: %O", l);
43
31
  }
44
32
  return o;
45
- }, [t]), a = $(
46
- async (o, c) => {
33
+ }, [t]), n = $(
34
+ async (o, l) => {
47
35
  if (!e)
48
36
  throw new Error("No executeTool callback provided");
49
37
  try {
50
- return await e(o, c);
38
+ return await e(o, l);
51
39
  } catch (i) {
52
- throw R("Error executing tool %s: %O", o, i), i;
40
+ throw v("Error executing tool %s: %O", o, i), i;
53
41
  }
54
42
  },
55
43
  [e]
56
44
  );
57
- return pt(() => {
45
+ return ht(() => {
58
46
  if (!t && !s)
59
47
  return;
60
48
  const o = {};
61
- return t && (o.getAvailableTools = n), e && (o.executeTool = a), s && (o.getAbilities = s), o;
49
+ return t && (o.getAvailableTools = a), e && (o.executeTool = n), s && (o.getAbilities = s), o;
62
50
  }, [
63
- n,
64
51
  a,
52
+ n,
65
53
  t,
66
54
  e,
67
55
  s
@@ -82,8 +70,8 @@ function ve(t) {
82
70
  });
83
71
  }
84
72
  function ke(t) {
85
- const { getClientTools: e, executeTool: s, getAbilities: n } = t;
86
- if (!e && !n)
73
+ const { getClientTools: e, executeTool: s, getAbilities: a } = t;
74
+ if (!e && !a)
87
75
  throw new Error(
88
76
  "At least one of getClientTools or getAbilities must be provided to useClientToolsWithAbilities."
89
77
  );
@@ -94,7 +82,7 @@ function ke(t) {
94
82
  return ot({
95
83
  getClientTools: e,
96
84
  executeTool: s,
97
- getAbilities: n
85
+ getAbilities: a
98
86
  });
99
87
  }
100
88
  function rt() {
@@ -104,13 +92,13 @@ function rt() {
104
92
  e += t.charAt(Math.floor(Math.random() * t.length));
105
93
  return e;
106
94
  }
107
- function q() {
95
+ function N() {
108
96
  return rt();
109
97
  }
110
- function Pt() {
98
+ function _t() {
111
99
  return `req-${rt()}`;
112
100
  }
113
- function Nt() {
101
+ function Pt() {
114
102
  return `task-${rt()}`;
115
103
  }
116
104
  function It(t, e) {
@@ -120,22 +108,22 @@ function It(t, e) {
120
108
  ...e && { metadata: e }
121
109
  };
122
110
  }
123
- function qt(t, e = "message/send", s = !1) {
124
- const n = {
111
+ function Nt(t, e = "message/send", s = !1) {
112
+ const a = {
125
113
  jsonrpc: "2.0",
126
- id: Pt(),
114
+ id: _t(),
127
115
  method: e,
128
116
  params: {
129
- id: t.id || Nt(),
117
+ id: t.id || Pt(),
130
118
  ...t
131
119
  }
132
120
  };
133
- return s && e === "message/stream" && (n.tokenStreaming = !0), n;
121
+ return s && e === "message/stream" && (a.tokenStreaming = !0), a;
134
122
  }
135
123
  function D(t) {
136
124
  return !t || !t.parts || !Array.isArray(t.parts) ? "" : t.parts.filter((e) => e.type === "text").map((e) => e.text).join(" ");
137
125
  }
138
- function Ht(t) {
126
+ function qt(t) {
139
127
  return {
140
128
  type: "data",
141
129
  data: {
@@ -147,7 +135,7 @@ function Ht(t) {
147
135
  metadata: {}
148
136
  };
149
137
  }
150
- function $t(t) {
138
+ function Ht(t) {
151
139
  return {
152
140
  type: "data",
153
141
  data: {
@@ -167,7 +155,7 @@ function F(t) {
167
155
  (e) => e.type === "data" && "toolCallId" in e.data && "toolId" in e.data && "arguments" in e.data
168
156
  );
169
157
  }
170
- function j(t, e, s, n) {
158
+ function j(t, e, s, a) {
171
159
  return {
172
160
  type: "data",
173
161
  data: {
@@ -175,10 +163,10 @@ function j(t, e, s, n) {
175
163
  toolId: e,
176
164
  result: s
177
165
  },
178
- metadata: n ? { error: n } : void 0
166
+ metadata: a ? { error: a } : void 0
179
167
  };
180
168
  }
181
- function Dt(t) {
169
+ function $t(t) {
182
170
  return {
183
171
  type: "data",
184
172
  data: {
@@ -187,16 +175,16 @@ function Dt(t) {
187
175
  metadata: {}
188
176
  };
189
177
  }
190
- function at(t, e) {
191
- const { contentType: s, ...n } = e || {};
178
+ function nt(t, e) {
179
+ const { contentType: s, ...a } = e || {};
192
180
  return {
193
181
  role: "user",
194
182
  parts: [It(t, s ? { contentType: s } : void 0)],
195
183
  kind: "message",
196
- messageId: q(),
184
+ messageId: N(),
197
185
  metadata: {
198
186
  timestamp: Date.now(),
199
- ...n
187
+ ...a
200
188
  }
201
189
  };
202
190
  }
@@ -205,7 +193,7 @@ function W(t) {
205
193
  role: "agent",
206
194
  parts: [It(t)],
207
195
  kind: "message",
208
- messageId: q(),
196
+ messageId: N(),
209
197
  metadata: {
210
198
  timestamp: Date.now()
211
199
  }
@@ -223,12 +211,12 @@ function it(t) {
223
211
  returnToAgent: !0
224
212
  };
225
213
  }
226
- function G(t, e = []) {
214
+ function Q(t, e = []) {
227
215
  return {
228
216
  role: "user",
229
217
  kind: "message",
230
218
  parts: [...e, ...t],
231
- messageId: q(),
219
+ messageId: N(),
232
220
  metadata: {
233
221
  timestamp: Date.now()
234
222
  }
@@ -237,23 +225,23 @@ function G(t, e = []) {
237
225
  function Re() {
238
226
  return new AbortController();
239
227
  }
240
- async function Ft(t, e) {
228
+ async function Dt(t, e) {
241
229
  if (!e)
242
230
  return t;
243
231
  try {
244
232
  const s = [];
245
233
  if (e.getAvailableTools) {
246
- const n = await e.getAvailableTools();
247
- if (n.length > 0) {
248
- const a = n.map(Ht);
249
- s.push(...a);
234
+ const a = await e.getAvailableTools();
235
+ if (a.length > 0) {
236
+ const n = a.map(qt);
237
+ s.push(...n);
250
238
  }
251
239
  }
252
240
  if (e.getAbilities) {
253
- const n = await e.getAbilities();
254
- if (n.length > 0) {
255
- const a = n.map($t);
256
- s.push(...a);
241
+ const a = await e.getAbilities();
242
+ if (a.length > 0) {
243
+ const n = a.map(Ht);
244
+ s.push(...n);
257
245
  }
258
246
  }
259
247
  return s.length === 0 ? t : {
@@ -261,134 +249,134 @@ async function Ft(t, e) {
261
249
  parts: [...t.parts, ...s]
262
250
  };
263
251
  } catch (s) {
264
- return R("Warning: Failed to get tools: %s", s), t;
252
+ return v("Warning: Failed to get tools: %s", s), t;
265
253
  }
266
254
  }
267
- function Ut(t, e) {
255
+ function Ft(t, e) {
268
256
  if (!e)
269
257
  return t;
270
258
  try {
271
259
  const s = e.getClientContext();
272
260
  if (!s || Object.keys(s).length === 0)
273
261
  return t;
274
- const n = Dt(s);
262
+ const a = $t(s);
275
263
  return {
276
264
  ...t,
277
- parts: [...t.parts, n]
265
+ parts: [...t.parts, a]
278
266
  };
279
267
  } catch (s) {
280
- return R("Warning: Failed to get context: %s", s), t;
268
+ return v("Warning: Failed to get context: %s", s), t;
281
269
  }
282
270
  }
283
- async function jt(t, e, s) {
284
- let n = await Ft(
271
+ async function Ut(t, e, s) {
272
+ let a = await Dt(
285
273
  t,
286
274
  e
287
275
  );
288
- n = Ut(
289
- n,
276
+ a = Ft(
277
+ a,
290
278
  s
291
279
  );
292
- const { metadata: a, ...r } = n;
280
+ const { metadata: n, ...r } = a;
293
281
  return r;
294
282
  }
295
- function Jt(t, e = "") {
296
- const s = [], n = e + t;
297
- let a = "", r = 0, o = 0;
298
- for (; o < n.length; ) {
299
- const i = n.indexOf(
283
+ function jt(t, e = "") {
284
+ const s = [], a = e + t;
285
+ let n = "", r = 0, o = 0;
286
+ for (; o < a.length; ) {
287
+ const i = a.indexOf(
300
288
  `
301
289
  `,
302
290
  o
303
- ), f = i === -1 ? n.substring(o) : n.substring(o, i);
304
- if (f.startsWith("data:"))
305
- a !== "" && (a += `
306
- `), a += f.substring(
307
- f.startsWith("data: ") ? 6 : 5
291
+ ), m = i === -1 ? a.substring(o) : a.substring(o, i);
292
+ if (m.startsWith("data:"))
293
+ n !== "" && (n += `
294
+ `), n += m.substring(
295
+ m.startsWith("data: ") ? 6 : 5
308
296
  );
309
- else if (f.trim() === "" && a) {
297
+ else if (m.trim() === "" && n) {
310
298
  try {
311
- s.push(JSON.parse(a)), r = i === -1 ? n.length : i + 1;
312
- } catch (u) {
313
- R("Failed to parse SSE event: %o", u), R("Problematic payload: %s", a);
299
+ s.push(JSON.parse(n)), r = i === -1 ? a.length : i + 1;
300
+ } catch (g) {
301
+ v("Failed to parse SSE event: %o", g), v("Problematic payload: %s", n);
314
302
  }
315
- a = "";
303
+ n = "";
316
304
  }
317
- i === -1 ? o = n.length : o = i + 1;
305
+ i === -1 ? o = a.length : o = i + 1;
318
306
  }
319
- const c = n.substring(r);
320
- return { events: s, nextBuffer: c };
307
+ const l = a.substring(r);
308
+ return { events: s, nextBuffer: l };
321
309
  }
322
- async function* Kt(t, e = {}) {
323
- var f, u, p;
324
- const { supportDeltas: s = !1 } = e, n = t.getReader(), a = new TextDecoder();
310
+ async function* Jt(t, e = {}) {
311
+ var m, g, h;
312
+ const { supportDeltas: s = !1 } = e, a = t.getReader(), n = new TextDecoder();
325
313
  let r = "";
326
- const o = new Lt();
327
- let c = null, i = null;
314
+ const o = new Kt();
315
+ let l = null, i = null;
328
316
  try {
329
317
  for (; ; ) {
330
- const { done: g, value: h } = await n.read();
331
- if (g)
318
+ const { done: f, value: p } = await a.read();
319
+ if (f)
332
320
  break;
333
- const M = a.decode(h, { stream: !0 }), { events: m, nextBuffer: l } = Jt(M, r);
334
- if (m && Array.isArray(m))
335
- for (let y = 0; y < m.length; y++) {
336
- const d = m[y];
337
- if (y > 0 && d.method === "message/delta" && typeof requestAnimationFrame < "u" && await new Promise((I) => {
338
- requestAnimationFrame(() => I(void 0));
321
+ const S = n.decode(p, { stream: !0 }), { events: c, nextBuffer: u } = jt(S, r);
322
+ if (c && Array.isArray(c))
323
+ for (let w = 0; w < c.length; w++) {
324
+ const d = c[w];
325
+ if (w > 0 && d.method === "message/delta" && typeof requestAnimationFrame < "u" && await new Promise((y) => {
326
+ requestAnimationFrame(() => y(void 0));
339
327
  }), d.error)
340
328
  throw new Error(
341
329
  `Streaming error: ${d.error.message}`
342
330
  );
343
- if (s && d.method === "message/delta" && ((f = d.params) != null && f.delta)) {
344
- const I = d.params.delta;
331
+ if (s && d.method === "message/delta" && ((m = d.params) != null && m.delta)) {
332
+ const y = d.params.delta;
345
333
  try {
346
- if (I.deltaType === "content" && (o.processContentDelta(
347
- I.content
348
- ), !c && d.params.id && (c = d.params.id), c)) {
349
- const k = o.getCurrentMessage();
334
+ if (y.deltaType === "content" && (o.processContentDelta(
335
+ y.content
336
+ ), !l && d.params.id && (l = d.params.id), l)) {
337
+ const x = o.getCurrentMessage();
350
338
  yield {
351
- id: c,
339
+ id: l,
352
340
  status: {
353
341
  state: "working",
354
- message: k
342
+ message: x
355
343
  },
356
344
  final: !1,
357
345
  text: o.getTextContent()
358
346
  };
359
347
  }
360
- } catch (k) {
361
- R("Failed to process delta: %o", k);
348
+ } catch (x) {
349
+ v("Failed to process delta: %o", x);
362
350
  }
363
- } else d.result && d.result.status ? (c = d.result.id, i = d.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset(), yield {
351
+ } else d.result && d.result.status ? (l = d.result.id, i = d.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset(), yield {
364
352
  id: d.result.id,
365
353
  status: d.result.status,
366
354
  final: d.result.status.state === "completed" || d.result.status.state === "failed" || d.result.status.state === "canceled",
367
355
  text: D(
368
- ((u = d.result.status) == null ? void 0 : u.message) || {
356
+ ((g = d.result.status) == null ? void 0 : g.message) || {
369
357
  role: "agent",
370
358
  parts: []
371
359
  }
372
360
  )
373
- }) : d.id && d.result && (c = d.result.id, d.result.status && (yield {
361
+ }) : d.id && d.result && (l = d.result.id, d.result.status && (yield {
374
362
  id: d.result.id,
375
363
  status: d.result.status,
376
364
  final: d.result.status.state === "completed" || d.result.status.state === "failed" || d.result.status.state === "canceled",
377
365
  text: D(
378
- ((p = d.result.status) == null ? void 0 : p.message) || {
366
+ ((h = d.result.status) == null ? void 0 : h.message) || {
379
367
  role: "agent",
380
368
  parts: []
381
369
  }
382
370
  )
383
371
  }));
384
372
  }
385
- r = l;
373
+ r = u;
386
374
  }
387
375
  } finally {
388
- n.releaseLock();
376
+ a.releaseLock();
389
377
  }
390
378
  }
391
- class Lt {
379
+ class Kt {
392
380
  constructor() {
393
381
  Z(this, "textContent", "");
394
382
  Z(this, "toolCalls", /* @__PURE__ */ new Map());
@@ -443,9 +431,9 @@ class Lt {
443
431
  type: "text",
444
432
  text: this.textContent
445
433
  });
446
- for (const [n, a] of this.toolCalls)
447
- if (a.toolName) {
448
- const r = a.argumentFragments.join("");
434
+ for (const [a, n] of this.toolCalls)
435
+ if (n.toolName) {
436
+ const r = n.argumentFragments.join("");
449
437
  let o = {};
450
438
  if (r)
451
439
  try {
@@ -456,8 +444,8 @@ class Lt {
456
444
  s.push({
457
445
  type: "data",
458
446
  data: {
459
- toolCallId: a.toolCallId,
460
- toolId: a.toolName,
447
+ toolCallId: n.toolCallId,
448
+ toolId: n.toolName,
461
449
  arguments: o
462
450
  }
463
451
  });
@@ -466,7 +454,7 @@ class Lt {
466
454
  role: e,
467
455
  parts: s,
468
456
  kind: "message",
469
- messageId: q()
457
+ messageId: N()
470
458
  };
471
459
  }
472
460
  /**
@@ -477,20 +465,20 @@ class Lt {
477
465
  }
478
466
  }
479
467
  function Tt(t, e, s = "request") {
480
- throw clearTimeout(e), R("%s failed with error: %O", s, t), t instanceof Error && (R("Error message: %s", t.message), R("Error stack: %s", t.stack)), t;
468
+ throw clearTimeout(e), v("%s failed with error: %O", s, t), t instanceof Error && (v("Error message: %s", t.message), v("Error stack: %s", t.stack)), t;
481
469
  }
482
470
  function Ct(t, e = "request") {
483
471
  if (!t.ok)
484
472
  throw new Error(`HTTP error! status: ${t.status}`);
485
473
  }
486
- function Wt(t, e = "request") {
474
+ function Lt(t, e = "request") {
487
475
  if (t.error)
488
476
  throw new Error(`A2A ${e} error: ${t.error.message}`);
489
477
  if (!t.result)
490
478
  throw new Error(`No result in ${e} response`);
491
479
  return t.result;
492
480
  }
493
- function Bt(t, e = "streaming request") {
481
+ function Wt(t, e = "streaming request") {
494
482
  if (Ct(t, e), !t.body)
495
483
  throw new Error(`No response body for ${e}`);
496
484
  }
@@ -504,28 +492,28 @@ function xt(t, e = "request") {
504
492
  function Vt(t, e) {
505
493
  return `${t}/${e}`;
506
494
  }
507
- function zt(t, e, s, n) {
508
- R("Request: %s %s", t, e), R("Headers: %o", s), n && R("Body: %s", wt(n));
495
+ function Bt(t, e, s, a) {
496
+ v("Request: %s %s", t, e), v("Headers: %o", s), a && v("Body: %s", wt(a));
509
497
  }
510
- async function Gt(t, e = !1) {
498
+ async function zt(t, e = !1) {
511
499
  const s = {
512
500
  "Content-Type": "application/json"
513
501
  };
514
502
  if (e && (s.Accept = "text/event-stream"), t) {
515
- const n = await t();
516
- return { ...s, ...n };
503
+ const a = await t();
504
+ return { ...s, ...a };
517
505
  }
518
506
  return s;
519
507
  }
520
508
  function bt(t, e) {
521
509
  if (!e)
522
510
  return t;
523
- const s = new AbortController(), n = (a) => {
524
- s.signal.aborted || s.abort(a.reason);
511
+ const s = new AbortController(), a = (n) => {
512
+ s.signal.aborted || s.abort(n.reason);
525
513
  };
526
- return t.aborted ? s.abort(t.reason) : t.addEventListener("abort", () => n(t), {
514
+ return t.aborted ? s.abort(t.reason) : t.addEventListener("abort", () => a(t), {
527
515
  once: !0
528
- }), e.aborted ? s.abort(e.reason) : e.addEventListener("abort", () => n(e), {
516
+ }), e.aborted ? s.abort(e.reason) : e.addEventListener("abort", () => a(e), {
529
517
  once: !0
530
518
  }), s.signal;
531
519
  }
@@ -537,94 +525,94 @@ function Mt(t, e, s) {
537
525
  signal: s
538
526
  };
539
527
  }
540
- async function Q(t, e, s, n, a, r) {
541
- const { message: o, sessionId: c, taskId: i, metadata: f } = t, { agentId: u, agentUrl: p, authProvider: g, proxy: h } = e, { isStreaming: M = !1, enableTokenStreaming: m = !1 } = s, l = c || r, y = Vt(p, u), d = await jt(
528
+ async function X(t, e, s, a, n, r) {
529
+ const { message: o, sessionId: l, taskId: i, metadata: m } = t, { agentId: g, agentUrl: h, authProvider: f, proxy: p } = e, { isStreaming: S = !1, enableTokenStreaming: c = !1 } = s, u = l || r, w = Vt(h, g), d = await Ut(
542
530
  o,
543
- n,
544
- a
545
- ), I = qt(
531
+ a,
532
+ n
533
+ ), y = Nt(
546
534
  {
547
535
  id: i,
548
- sessionId: l,
536
+ sessionId: u,
549
537
  message: d,
550
- metadata: f
538
+ metadata: m
551
539
  },
552
- M ? "message/stream" : "message/send",
553
- m && M
540
+ S ? "message/stream" : "message/send",
541
+ c && S
554
542
  // Only enable token streaming if using SSE
555
- ), k = await Gt(g, M);
556
- return zt("POST", y, k, I), {
557
- request: I,
558
- headers: k,
543
+ ), x = await zt(f, S);
544
+ return Bt("POST", w, x, y), {
545
+ request: y,
546
+ headers: x,
559
547
  enhancedMessage: d,
560
- effectiveSessionId: l,
561
- fullAgentUrl: y
548
+ effectiveSessionId: u,
549
+ fullAgentUrl: w
562
550
  };
563
551
  }
564
552
  async function At(t, e, s = {}) {
565
- const { request: n, headers: a, fullAgentUrl: r } = t, { timeout: o } = e, { abortSignal: c } = s, { timeoutId: i, controller: f } = xt(
553
+ const { request: a, headers: n, fullAgentUrl: r } = t, { timeout: o } = e, { abortSignal: l } = s, { timeoutId: i, controller: m } = xt(
566
554
  o,
567
555
  "request"
568
- ), u = c ? bt(f.signal, c) : f.signal;
556
+ ), g = l ? bt(m.signal, l) : m.signal;
569
557
  try {
570
- const p = Mt(
571
- a,
572
- JSON.stringify(n),
573
- u
558
+ const h = Mt(
559
+ n,
560
+ JSON.stringify(a),
561
+ g
574
562
  );
575
- R("Making request to %s with options: %O", r, {
576
- method: p.method,
577
- headers: p.headers
563
+ v("Making request to %s with options: %O", r, {
564
+ method: h.method,
565
+ headers: h.headers
578
566
  });
579
- const g = await fetch(r, p);
580
- clearTimeout(i), Ct(g, "request");
581
- const h = await g.json();
582
- return R(
567
+ const f = await fetch(r, h);
568
+ clearTimeout(i), Ct(f, "request");
569
+ const p = await f.json();
570
+ return v(
583
571
  "Response from %s: %d %O",
584
572
  r,
585
- g.status,
586
- wt(h)
587
- ), Wt(h, "request");
588
- } catch (p) {
589
- Tt(p, i, "request");
573
+ f.status,
574
+ wt(p)
575
+ ), Lt(p, "request");
576
+ } catch (h) {
577
+ Tt(h, i, "request");
590
578
  }
591
579
  }
592
580
  async function* St(t, e, s) {
593
- const { request: n, headers: a, fullAgentUrl: r } = t, {
581
+ const { request: a, headers: n, fullAgentUrl: r } = t, {
594
582
  streamingTimeout: o = 6e4,
595
- abortSignal: c,
583
+ abortSignal: l,
596
584
  enableTokenStreaming: i = !1
597
- } = s, { timeoutId: f, controller: u } = xt(
585
+ } = s, { timeoutId: m, controller: g } = xt(
598
586
  o,
599
587
  "streaming request"
600
- ), p = c ? bt(u.signal, c) : u.signal;
588
+ ), h = l ? bt(g.signal, l) : g.signal;
601
589
  try {
602
- const g = JSON.stringify(n), h = Mt(a, g, p), M = await fetch(r, h);
603
- if (clearTimeout(f), Bt(M, "streaming request"), !M.body)
590
+ const f = JSON.stringify(a), p = Mt(n, f, h), S = await fetch(r, p);
591
+ if (clearTimeout(m), Wt(S, "streaming request"), !S.body)
604
592
  throw new Error(
605
593
  "Response body is null - server may not support streaming"
606
594
  );
607
- const m = i && n.tokenStreaming === !0;
608
- yield* Kt(M.body, {
609
- supportDeltas: m
595
+ const c = i && a.tokenStreaming === !0;
596
+ yield* Jt(S.body, {
597
+ supportDeltas: c
610
598
  });
611
- } catch (g) {
612
- Tt(g, f, "streaming request");
599
+ } catch (f) {
600
+ Tt(f, m, "streaming request");
613
601
  }
614
602
  }
615
603
  const Qt = 12e4;
616
- async function X(t, e, s, n, a) {
604
+ async function G(t, e, s, a, n) {
617
605
  if (t.getAbilities) {
618
606
  const r = await t.getAbilities();
619
607
  if (r.length > 0)
620
608
  for (const o of r) {
621
- const c = o.name.replace(/\//g, "__").replace(/-/g, "_");
622
- if (e === c || e === o.name) {
609
+ const l = o.name.replace(/\//g, "__").replace(/-/g, "_");
610
+ if (e === l || e === o.name) {
623
611
  if (o.callback)
624
612
  try {
625
613
  return { result: await o.callback(s), returnToAgent: !0 };
626
614
  } catch (i) {
627
- return R(
615
+ return v(
628
616
  "Error executing ability %s: %O",
629
617
  o.name,
630
618
  i
@@ -643,7 +631,7 @@ async function X(t, e, s, n, a) {
643
631
  s
644
632
  ), returnToAgent: !0 };
645
633
  } catch (i) {
646
- return R(
634
+ return v(
647
635
  "Error executing ability %s: %O",
648
636
  o.name,
649
637
  i
@@ -665,8 +653,8 @@ async function X(t, e, s, n, a) {
665
653
  return await t.executeTool(
666
654
  e,
667
655
  s,
668
- n,
669
- a
656
+ a,
657
+ n
670
658
  );
671
659
  throw new Error(
672
660
  `No handler found for tool: ${e}. Tool provider must implement executeTool for non-ability tools.`
@@ -680,10 +668,10 @@ async function Xt(t, e) {
680
668
  if (s.length === 0)
681
669
  return !1;
682
670
  try {
683
- const n = await t.getAvailableTools();
684
- for (const a of s)
685
- if (n.some(
686
- (o) => o.id === a.data.toolId
671
+ const a = await t.getAvailableTools();
672
+ for (const n of s)
673
+ if (a.some(
674
+ (o) => o.id === n.data.toolId
687
675
  ))
688
676
  return !0;
689
677
  } catch {
@@ -691,76 +679,76 @@ async function Xt(t, e) {
691
679
  }
692
680
  return !1;
693
681
  }
694
- function Yt() {
682
+ function Gt() {
695
683
  lt.clear();
696
684
  }
697
- function Zt(t) {
685
+ function Yt(t) {
698
686
  return t.map((e) => {
699
- const s = e.data.toolCallId, n = lt.get(s);
700
- if (n && n.resolvedValue !== null) {
701
- const a = n.resolvedValue;
702
- return a.error ? j(
687
+ const s = e.data.toolCallId, a = lt.get(s);
688
+ if (a && a.resolvedValue !== null) {
689
+ const n = a.resolvedValue;
690
+ return n.error ? j(
703
691
  s,
704
692
  e.data.toolId,
705
693
  void 0,
706
- a.error
694
+ n.error
707
695
  ) : j(
708
696
  s,
709
697
  e.data.toolId,
710
- a
698
+ n
711
699
  );
712
700
  }
713
701
  return e;
714
702
  });
715
703
  }
716
704
  async function vt(t, e, s) {
717
- const n = [], a = [];
705
+ const a = [], n = [];
718
706
  let r = !1;
719
707
  for (const o of t) {
720
- const { toolCallId: c, toolId: i, arguments: f } = o.data;
708
+ const { toolCallId: l, toolId: i, arguments: m } = o.data;
721
709
  try {
722
- const u = await X(
710
+ const g = await G(
723
711
  e,
724
712
  i,
725
- f,
713
+ m,
726
714
  s,
727
- c
728
- ), { result: p, returnToAgent: g, agentMessage: h } = it(u);
729
- g && (r = !0), h && a.push(W(h)), n.push(
715
+ l
716
+ ), { result: h, returnToAgent: f, agentMessage: p } = it(g);
717
+ f && (r = !0), p && n.push(W(p)), a.push(
730
718
  j(
731
- c,
719
+ l,
732
720
  i,
733
- p
721
+ h
734
722
  )
735
723
  );
736
- } catch (u) {
737
- r = !0, n.push(
724
+ } catch (g) {
725
+ r = !0, a.push(
738
726
  j(
739
- c,
727
+ l,
740
728
  i,
741
729
  void 0,
742
- u instanceof Error ? u.message : String(u)
730
+ g instanceof Error ? g.message : String(g)
743
731
  )
744
732
  );
745
733
  }
746
734
  }
747
- return { results: n, shouldReturnToAgent: r, agentMessages: a };
735
+ return { results: a, shouldReturnToAgent: r, agentMessages: n };
748
736
  }
749
737
  function gt(t) {
750
738
  const e = [];
751
739
  for (const s of t)
752
- for (const n of s.parts)
753
- n.type === "text" ? e.push({
740
+ for (const a of s.parts)
741
+ a.type === "text" ? e.push({
754
742
  type: "data",
755
743
  data: {
756
744
  role: s.role,
757
- text: n.text
745
+ text: a.text
758
746
  }
759
- }) : n.type === "data" && e.push(n);
747
+ }) : a.type === "data" && e.push(a);
760
748
  return e;
761
749
  }
762
- async function et(t, e, s, n, a, r, o) {
763
- const i = await Q(
750
+ async function et(t, e, s, a, n, r, o) {
751
+ const i = await X(
764
752
  {
765
753
  message: e,
766
754
  taskId: t,
@@ -769,37 +757,37 @@ async function et(t, e, s, n, a, r, o) {
769
757
  },
770
758
  s,
771
759
  { isStreaming: !1 },
772
- n,
773
760
  a,
761
+ n,
774
762
  r
775
763
  );
776
764
  return await At(i, s, {
777
765
  abortSignal: o
778
766
  });
779
767
  }
780
- async function ft(t, e, s, n, a, r, o, c, i = []) {
781
- const f = {
768
+ async function ft(t, e, s, a, n, r, o, l, i = []) {
769
+ const m = {
782
770
  message: e,
783
771
  taskId: t,
784
772
  sessionId: void 0
785
773
  // Use task's session
786
- }, u = c || { isStreaming: !0 }, p = await Q(
787
- f,
774
+ }, g = l || { isStreaming: !0 }, h = await X(
775
+ m,
788
776
  s,
789
777
  {
790
- ...u
778
+ ...g
791
779
  },
792
- n,
793
780
  a,
781
+ n,
794
782
  r
795
- ), g = St(p, s, {
796
- ...u,
783
+ ), f = St(h, s, {
784
+ ...g,
797
785
  abortSignal: o
798
786
  });
799
787
  return kt(
800
- g,
801
- n,
788
+ f,
802
789
  a,
790
+ n,
803
791
  s,
804
792
  r,
805
793
  !0,
@@ -807,163 +795,161 @@ async function ft(t, e, s, n, a, r, o, c, i = []) {
807
795
  i,
808
796
  // preserve conversation parts across continuation
809
797
  o,
810
- u
798
+ g
811
799
  // Pass through the same request options
812
800
  );
813
801
  }
814
- async function* kt(t, e, s, n, a, r = !0, o = [], c, i) {
815
- var f, u, p, g, h, M, m, l, y, d, I, k;
816
- for await (const w of t) {
817
- if (yield w, w.status.state === "running" && w.status.message && e && await Xt(
802
+ async function* kt(t, e, s, a, n, r = !0, o = [], l, i) {
803
+ var m, g, h, f, p, S, c, u, w, d, y, x;
804
+ for await (const I of t) {
805
+ if (yield I, I.status.state === "running" && I.status.message && e && await Xt(
818
806
  e,
819
- w.status.message
807
+ I.status.message
820
808
  )) {
821
809
  const P = F(
822
- w.status.message
810
+ I.status.message
823
811
  );
824
812
  for (const T of P) {
825
- const { toolCallId: x, toolId: v, arguments: C } = T.data;
826
- X(
813
+ const { toolCallId: b, toolId: k, arguments: M } = T.data;
814
+ G(
827
815
  e,
828
- v,
829
- C,
830
- (u = (f = w.status) == null ? void 0 : f.message) == null ? void 0 : u.messageId,
831
- x
832
- ).catch((S) => {
833
- console.error(
834
- `Tool execution failed for ${v}:`,
835
- S
836
- );
816
+ k,
817
+ M,
818
+ (g = (m = I.status) == null ? void 0 : m.message) == null ? void 0 : g.messageId,
819
+ b
820
+ ).catch((C) => {
821
+ v("Tool execution failed for %s: %O", k, C);
837
822
  });
838
823
  }
839
824
  yield {
840
- id: w.id,
825
+ id: I.id,
841
826
  status: {
842
827
  state: "running",
843
828
  message: {
844
829
  role: "agent",
845
830
  kind: "message",
846
831
  parts: P,
847
- messageId: q()
832
+ messageId: N()
848
833
  }
849
834
  },
850
835
  final: !1,
851
836
  text: ""
852
837
  };
853
838
  }
854
- if (w.status.state === "input-required" && w.status.message && e) {
839
+ if (I.status.state === "input-required" && I.status.message && e) {
855
840
  const P = F(
856
- w.status.message
841
+ I.status.message
857
842
  );
858
843
  if (P.length > 0) {
859
844
  const T = [];
860
- let x = !1;
861
- const v = [], C = [];
862
- for (const S of P) {
845
+ let b = !1;
846
+ const k = [], M = [];
847
+ for (const C of P) {
863
848
  const {
864
- toolCallId: b,
865
- toolId: _,
866
- arguments: N
867
- } = S.data;
849
+ toolCallId: A,
850
+ toolId: q,
851
+ arguments: _
852
+ } = C.data;
868
853
  try {
869
- const E = await X(
854
+ const E = await G(
870
855
  e,
856
+ q,
871
857
  _,
872
- N,
873
- (g = (p = w.status) == null ? void 0 : p.message) == null ? void 0 : g.messageId,
874
- b
875
- ), { result: A, returnToAgent: O, agentMessage: J } = it(E);
876
- if (O && (x = !0), J && C.push(
858
+ (f = (h = I.status) == null ? void 0 : h.message) == null ? void 0 : f.messageId,
859
+ A
860
+ ), { result: R, returnToAgent: O, agentMessage: J } = it(E);
861
+ if (O && (b = !0), J && M.push(
877
862
  W(J)
878
- ), A.result instanceof Promise) {
879
- const V = A.result, K = {
880
- promise: V,
863
+ ), R.result instanceof Promise) {
864
+ const B = R.result, K = {
865
+ promise: B,
881
866
  resolvedValue: null
882
867
  };
883
868
  lt.set(
884
- b,
869
+ A,
885
870
  K
886
- ), V.then((H) => {
871
+ ), B.then((H) => {
887
872
  K.resolvedValue = H;
888
873
  }).catch((H) => {
889
- console.error(
890
- `Promise rejected for tool call ${b}:`,
874
+ v(
875
+ "Promise rejected for tool call %s: %O",
876
+ A,
891
877
  H
892
878
  ), K.resolvedValue = {
893
879
  error: H instanceof Error ? H.message : String(H)
894
880
  };
895
881
  });
896
882
  }
897
- const B = j(
898
- b,
899
- _,
900
- A
883
+ const V = j(
884
+ A,
885
+ q,
886
+ R
901
887
  );
902
- T.push(B), v.push(B);
888
+ T.push(V), k.push(V);
903
889
  } catch (E) {
904
- const A = j(
905
- b,
906
- _,
890
+ const R = j(
891
+ A,
892
+ q,
907
893
  void 0,
908
894
  E instanceof Error ? E.message : String(E)
909
895
  );
910
- T.push(A), v.push(A);
896
+ T.push(R), k.push(R);
911
897
  }
912
898
  }
913
- if (o.push(w.status.message), T.length > 0 && o.push({
899
+ if (o.push(I.status.message), T.length > 0 && o.push({
914
900
  role: "agent",
915
901
  kind: "message",
916
902
  parts: T,
917
- messageId: q()
918
- }), x) {
919
- const S = gt(o), b = G(
903
+ messageId: N()
904
+ }), b) {
905
+ const C = gt(o), A = Q(
920
906
  [],
921
907
  // Empty array - tool results are already in historyDataParts
922
- S
908
+ C
923
909
  );
924
910
  yield {
925
- id: w.id,
911
+ id: I.id,
926
912
  status: {
927
913
  state: "working",
928
- message: b
914
+ message: A
929
915
  },
930
916
  final: !1,
931
917
  text: ""
932
918
  };
933
- const _ = await ft(
934
- w.id,
935
- b,
936
- n,
919
+ const q = await ft(
920
+ I.id,
921
+ A,
922
+ a,
937
923
  e,
938
924
  s,
939
- a,
940
- c,
925
+ n,
926
+ l,
941
927
  i,
942
928
  o
943
929
  );
944
- let N = null;
945
- for await (const O of _)
946
- O.final ? N = O : yield O;
947
- if (!N)
930
+ let _ = null;
931
+ for await (const O of q)
932
+ O.final ? _ = O : yield O;
933
+ if (!_)
948
934
  throw new Error(
949
935
  "Continue task stream ended without final result"
950
936
  );
951
- let E = (h = N.status) != null && h.message ? F(
952
- N.status.message
953
- ) : [], A = N;
937
+ let E = (p = _.status) != null && p.message ? F(
938
+ _.status.message
939
+ ) : [], R = _;
954
940
  if (E.length > 0)
955
941
  for (yield {
956
- ...N,
942
+ ..._,
957
943
  final: !1,
958
944
  text: D(
959
- ((M = N.status) == null ? void 0 : M.message) || {
945
+ ((S = _.status) == null ? void 0 : S.message) || {
960
946
  parts: [],
961
- messageId: q()
947
+ messageId: N()
962
948
  }
963
949
  )
964
950
  }; E.length > 0; ) {
965
- (m = A.status) != null && m.message && o.push(
966
- A.status.message
951
+ (c = R.status) != null && c.message && o.push(
952
+ R.status.message
967
953
  );
968
954
  const {
969
955
  results: O,
@@ -971,36 +957,36 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, i) {
971
957
  } = await vt(
972
958
  E,
973
959
  e,
974
- (y = (l = A.status) == null ? void 0 : l.message) == null ? void 0 : y.messageId
960
+ (w = (u = R.status) == null ? void 0 : u.message) == null ? void 0 : w.messageId
975
961
  );
976
962
  if (O.length > 0 && (yield {
977
- id: A.id,
963
+ id: R.id,
978
964
  status: {
979
965
  state: "working",
980
966
  message: {
981
967
  role: "agent",
982
968
  kind: "message",
983
969
  parts: O,
984
- messageId: q()
970
+ messageId: N()
985
971
  }
986
972
  // Simple message with just the results
987
973
  },
988
974
  final: !1,
989
975
  text: ""
990
976
  }), J) {
991
- const B = r ? gt(
977
+ const V = r ? gt(
992
978
  o
993
- ) : [], V = G(
979
+ ) : [], B = Q(
994
980
  O,
995
- B
981
+ V
996
982
  ), K = await ft(
997
- A.id,
998
- V,
999
- n,
983
+ R.id,
984
+ B,
985
+ a,
1000
986
  e,
1001
987
  s,
1002
- a,
1003
- c,
988
+ n,
989
+ l,
1004
990
  i,
1005
991
  o
1006
992
  );
@@ -1011,16 +997,16 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, i) {
1011
997
  throw new Error(
1012
998
  "Continue task stream ended without final result"
1013
999
  );
1014
- A = H, E = (d = A.status) != null && d.message ? F(
1015
- A.status.message
1000
+ R = H, E = (d = R.status) != null && d.message ? F(
1001
+ R.status.message
1016
1002
  ) : [], E.length > 0 && (yield {
1017
- id: A.id,
1018
- status: A.status,
1003
+ id: R.id,
1004
+ status: R.status,
1019
1005
  final: !1,
1020
1006
  text: D(
1021
- ((I = A.status) == null ? void 0 : I.message) || {
1007
+ ((y = R.status) == null ? void 0 : y.message) || {
1022
1008
  parts: [],
1023
- messageId: q()
1009
+ messageId: N()
1024
1010
  }
1025
1011
  )
1026
1012
  });
@@ -1028,39 +1014,39 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, i) {
1028
1014
  break;
1029
1015
  }
1030
1016
  yield {
1031
- ...A,
1017
+ ...R,
1032
1018
  final: !0,
1033
1019
  text: D(
1034
- ((k = A.status) == null ? void 0 : k.message) || {
1020
+ ((x = R.status) == null ? void 0 : x.message) || {
1035
1021
  parts: [],
1036
- messageId: q()
1022
+ messageId: N()
1037
1023
  }
1038
1024
  )
1039
1025
  };
1040
1026
  } else {
1041
- const S = {
1042
- ...w.status.message,
1043
- parts: v
1044
- }, b = {
1045
- ...w,
1027
+ const C = {
1028
+ ...I.status.message,
1029
+ parts: k
1030
+ }, A = {
1031
+ ...I,
1046
1032
  status: {
1047
- ...w.status,
1048
- message: S
1033
+ ...I.status,
1034
+ message: C
1049
1035
  },
1050
- final: C.length === 0,
1036
+ final: M.length === 0,
1051
1037
  // Only final if no agent messages to follow
1052
- text: D(S)
1038
+ text: D(C)
1053
1039
  };
1054
- if (yield b, C.length > 0) {
1055
- const _ = C.map((E) => D(E)).join(" "), N = W(_);
1040
+ if (yield A, M.length > 0) {
1041
+ const q = M.map((E) => D(E)).join(" "), _ = W(q);
1056
1042
  yield {
1057
- id: b.id,
1043
+ id: A.id,
1058
1044
  status: {
1059
1045
  state: "completed",
1060
- message: N
1046
+ message: _
1061
1047
  },
1062
1048
  final: !0,
1063
- text: _
1049
+ text: q
1064
1050
  };
1065
1051
  }
1066
1052
  }
@@ -1068,257 +1054,257 @@ async function* kt(t, e, s, n, a, r = !0, o = [], c, i) {
1068
1054
  }
1069
1055
  }
1070
1056
  }
1071
- function te(t) {
1057
+ function Zt(t) {
1072
1058
  const {
1073
1059
  agentId: e,
1074
1060
  agentUrl: s,
1075
- authProvider: n,
1076
- defaultSessionId: a,
1061
+ authProvider: a,
1062
+ defaultSessionId: n,
1077
1063
  timeout: r = Qt,
1078
1064
  toolProvider: o,
1079
- contextProvider: c,
1065
+ contextProvider: l,
1080
1066
  enableStreaming: i = !1
1081
- } = t, f = {
1067
+ } = t, m = {
1082
1068
  agentId: e,
1083
1069
  agentUrl: s,
1084
- authProvider: n,
1070
+ authProvider: a,
1085
1071
  timeout: r
1086
1072
  };
1087
1073
  return {
1088
- async sendMessage(u) {
1089
- var I, k;
1090
- const { withHistory: p = !0, abortSignal: g } = u, h = u.sessionId || a || void 0, M = [];
1091
- M.push(u.message);
1092
- const m = await Q(
1093
- u,
1094
- f,
1074
+ async sendMessage(g) {
1075
+ var d, y;
1076
+ const { abortSignal: h } = g, f = g.sessionId || n || void 0, p = [];
1077
+ p.push(g.message);
1078
+ const S = await X(
1079
+ g,
1080
+ m,
1095
1081
  { isStreaming: !1 },
1096
1082
  o,
1097
- c,
1098
- h
1083
+ l,
1084
+ f
1099
1085
  );
1100
- let l = await At(
1086
+ let c = await At(
1087
+ S,
1101
1088
  m,
1102
- f,
1103
- { abortSignal: g }
1089
+ { abortSignal: h }
1104
1090
  );
1105
- const y = [], d = [];
1106
- for (; l.status.message && o; ) {
1107
- const w = F(
1108
- l.status.message
1091
+ const u = [], w = [];
1092
+ for (; c.status.message && o; ) {
1093
+ const x = F(
1094
+ c.status.message
1109
1095
  );
1110
- if (w.length === 0)
1096
+ if (x.length === 0)
1111
1097
  break;
1112
- y.push(...w);
1113
- const P = [];
1114
- let T = !1;
1115
- for (const x of w) {
1098
+ u.push(...x);
1099
+ const I = [];
1100
+ let P = !1;
1101
+ for (const T of x) {
1116
1102
  const {
1117
- toolCallId: v,
1118
- toolId: C,
1119
- arguments: S
1120
- } = x.data;
1103
+ toolCallId: b,
1104
+ toolId: k,
1105
+ arguments: M
1106
+ } = T.data;
1121
1107
  try {
1122
- const b = await X(
1108
+ const C = await G(
1123
1109
  o,
1124
- C,
1125
- S
1126
- ), { result: _, returnToAgent: N, agentMessage: E } = it(b);
1127
- N && (T = !0), E && d.push(
1128
- W(E)
1110
+ k,
1111
+ M
1112
+ ), { result: A, returnToAgent: q, agentMessage: _ } = it(C);
1113
+ q && (P = !0), _ && w.push(
1114
+ W(_)
1129
1115
  );
1130
- const A = j(
1131
- v,
1132
- C,
1133
- _
1116
+ const E = j(
1117
+ b,
1118
+ k,
1119
+ A
1134
1120
  );
1135
- P.push(A), y.push(A);
1136
- } catch (b) {
1137
- const _ = j(
1138
- v,
1139
- C,
1121
+ I.push(E), u.push(E);
1122
+ } catch (C) {
1123
+ const A = j(
1124
+ b,
1125
+ k,
1140
1126
  void 0,
1141
- b instanceof Error ? b.message : String(b)
1127
+ C instanceof Error ? C.message : String(C)
1142
1128
  );
1143
- P.push(_), y.push(_);
1129
+ I.push(A), u.push(A);
1144
1130
  }
1145
1131
  }
1146
- if (M.push(l.status.message), T) {
1147
- const x = G(P);
1148
- l = await et(
1149
- l.id,
1150
- x,
1151
- f,
1132
+ if (p.push(c.status.message), P) {
1133
+ const T = Q(I);
1134
+ c = await et(
1135
+ c.id,
1136
+ T,
1137
+ m,
1152
1138
  o,
1153
- c,
1154
- h,
1155
- g
1139
+ l,
1140
+ f,
1141
+ h
1156
1142
  );
1157
1143
  } else
1158
1144
  break;
1159
1145
  }
1160
- if (y.length > 0 && (I = l.status) != null && I.message) {
1161
- const w = {
1162
- ...l.status.message,
1163
- parts: y
1146
+ if (u.length > 0 && (d = c.status) != null && d.message) {
1147
+ const x = {
1148
+ ...c.status.message,
1149
+ parts: u
1164
1150
  };
1165
- l = {
1166
- ...l,
1151
+ c = {
1152
+ ...c,
1167
1153
  status: {
1168
- ...l.status,
1169
- message: w
1154
+ ...c.status,
1155
+ message: x
1170
1156
  }
1171
1157
  };
1172
1158
  }
1173
- if (d.length > 0) {
1174
- const w = d.map((T) => D(T)).join(" "), P = W(w);
1159
+ if (w.length > 0) {
1160
+ const x = w.map((P) => D(P)).join(" "), I = W(x);
1175
1161
  return {
1176
- ...l,
1162
+ ...c,
1177
1163
  // Keep the enhanced message with tool results
1178
1164
  // The agent message will be handled separately by the caller
1179
- text: w,
1180
- agentMessage: P
1165
+ text: x,
1166
+ agentMessage: I
1181
1167
  // Add this for the caller to handle
1182
1168
  };
1183
1169
  }
1184
1170
  return {
1185
- ...l,
1171
+ ...c,
1186
1172
  text: D(
1187
- ((k = l.status) == null ? void 0 : k.message) || {
1173
+ ((y = c.status) == null ? void 0 : y.message) || {
1188
1174
  parts: [],
1189
- messageId: q()
1175
+ messageId: N()
1190
1176
  }
1191
1177
  )
1192
1178
  };
1193
1179
  },
1194
- async *sendMessageStream(u) {
1180
+ async *sendMessageStream(g) {
1195
1181
  const {
1196
- withHistory: p = !0,
1197
- abortSignal: g,
1198
- enableStreaming: h
1199
- } = u, M = u.sessionId || a || void 0, m = h ?? i, l = [];
1200
- l.push(u.message);
1201
- const y = await Q(
1202
- u,
1203
- f,
1182
+ withHistory: h = !0,
1183
+ abortSignal: f,
1184
+ enableStreaming: p
1185
+ } = g, S = g.sessionId || n || void 0, c = p ?? i, u = [];
1186
+ u.push(g.message);
1187
+ const w = await X(
1188
+ g,
1189
+ m,
1204
1190
  {
1205
1191
  isStreaming: !0,
1206
1192
  // Always use message/stream endpoint for SSE
1207
- enableTokenStreaming: m
1193
+ enableTokenStreaming: c
1208
1194
  },
1209
1195
  o,
1210
- c,
1211
- M
1196
+ l,
1197
+ S
1212
1198
  ), d = St(
1213
- y,
1214
- f,
1199
+ w,
1200
+ m,
1215
1201
  {
1216
- enableTokenStreaming: m,
1202
+ enableTokenStreaming: c,
1217
1203
  // Token streaming is optional
1218
1204
  streamingTimeout: r,
1219
- abortSignal: g
1205
+ abortSignal: f
1220
1206
  }
1221
1207
  );
1222
1208
  yield* kt(
1223
1209
  d,
1224
1210
  o,
1225
- c,
1226
- f,
1227
- M,
1228
- p,
1229
1211
  l,
1230
- g,
1212
+ m,
1213
+ S,
1214
+ h,
1215
+ u,
1216
+ f,
1231
1217
  {
1232
1218
  isStreaming: !0,
1233
- enableTokenStreaming: m,
1219
+ enableTokenStreaming: c,
1234
1220
  streamingTimeout: r
1235
1221
  }
1236
1222
  );
1237
1223
  },
1238
- async continueTask(u, p, g) {
1239
- var l;
1240
- const h = at(p);
1241
- let m = await et(
1242
- u,
1243
- h,
1244
- f,
1224
+ async continueTask(g, h, f) {
1225
+ var u;
1226
+ const p = nt(h);
1227
+ let c = await et(
1228
+ g,
1229
+ p,
1230
+ m,
1245
1231
  o,
1246
- c,
1247
- g
1232
+ l,
1233
+ f
1248
1234
  );
1249
- for (; m.status.state === "input-required" && m.status.message && o; ) {
1250
- const y = F(
1251
- m.status.message
1235
+ for (; c.status.state === "input-required" && c.status.message && o; ) {
1236
+ const w = F(
1237
+ c.status.message
1252
1238
  );
1253
- if (y.length === 0)
1239
+ if (w.length === 0)
1254
1240
  break;
1255
- const { results: d, shouldReturnToAgent: I } = await vt(y, o);
1256
- if (I) {
1257
- const k = G(d);
1258
- m = await et(
1259
- m.id,
1260
- k,
1261
- f,
1241
+ const { results: d, shouldReturnToAgent: y } = await vt(w, o);
1242
+ if (y) {
1243
+ const x = Q(d);
1244
+ c = await et(
1245
+ c.id,
1246
+ x,
1247
+ m,
1262
1248
  o,
1263
- c,
1264
- g
1249
+ l,
1250
+ f
1265
1251
  );
1266
1252
  } else
1267
1253
  break;
1268
1254
  }
1269
1255
  return {
1270
- ...m,
1256
+ ...c,
1271
1257
  text: D(
1272
- ((l = m.status) == null ? void 0 : l.message) || {
1258
+ ((u = c.status) == null ? void 0 : u.message) || {
1273
1259
  parts: [],
1274
- messageId: q()
1260
+ messageId: N()
1275
1261
  }
1276
1262
  )
1277
1263
  };
1278
1264
  },
1279
- async getTask(u) {
1265
+ async getTask() {
1280
1266
  throw new Error("getTask not implemented yet");
1281
1267
  },
1282
- async cancelTask(u) {
1268
+ async cancelTask() {
1283
1269
  throw new Error("cancelTask not implemented yet");
1284
1270
  }
1285
1271
  };
1286
1272
  }
1287
1273
  const ct = "a8c_agenttic_conversation_history";
1288
- function ee(t) {
1289
- var u, p;
1274
+ function te(t) {
1275
+ var g, h;
1290
1276
  const e = t.parts.filter(
1291
- (g) => g.type === "text"
1292
- ), s = e.map((g) => g.text).join(`
1293
- `), n = e.some(
1294
- (g) => {
1295
- var h;
1296
- return ((h = g.metadata) == null ? void 0 : h.contentType) === "context";
1277
+ (f) => f.type === "text"
1278
+ ), s = e.map((f) => f.text).join(`
1279
+ `), a = e.some(
1280
+ (f) => {
1281
+ var p;
1282
+ return ((p = f.metadata) == null ? void 0 : p.contentType) === "context";
1297
1283
  }
1298
- ) ? "context" : void 0, a = t.parts.filter(
1299
- (g) => g.type === "data" && "toolCallId" in g.data && "arguments" in g.data
1300
- ).map((g) => ({
1301
- toolCallId: g.data.toolCallId,
1302
- toolId: g.data.toolId,
1303
- arguments: g.data.arguments
1284
+ ) ? "context" : void 0, n = t.parts.filter(
1285
+ (f) => f.type === "data" && "toolCallId" in f.data && "arguments" in f.data
1286
+ ).map((f) => ({
1287
+ toolCallId: f.data.toolCallId,
1288
+ toolId: f.data.toolId,
1289
+ arguments: f.data.arguments
1304
1290
  })), r = t.parts.filter(
1305
- (g) => g.type === "data" && "toolCallId" in g.data && "result" in g.data
1306
- ).map((g) => ({
1307
- toolCallId: g.data.toolCallId,
1308
- result: g.data.result,
1309
- error: g.data.error
1310
- })), c = a.length > 0 || r.length > 0 ? "agent" : t.role, i = ((u = t.metadata) == null ? void 0 : u.timestamp) ?? Date.now(), f = ((p = t.metadata) == null ? void 0 : p.archived) ?? void 0;
1291
+ (f) => f.type === "data" && "toolCallId" in f.data && "result" in f.data
1292
+ ).map((f) => ({
1293
+ toolCallId: f.data.toolCallId,
1294
+ result: f.data.result,
1295
+ error: f.data.error
1296
+ })), l = n.length > 0 || r.length > 0 ? "agent" : t.role, i = ((g = t.metadata) == null ? void 0 : g.timestamp) ?? Date.now(), m = ((h = t.metadata) == null ? void 0 : h.archived) ?? void 0;
1311
1297
  return {
1312
- role: c,
1298
+ role: l,
1313
1299
  content: s || "(No text content)",
1314
1300
  timestamp: i,
1315
- ...f !== void 0 && { archived: f },
1316
- ...n && { contentType: n },
1317
- ...a.length > 0 && { toolCalls: a },
1301
+ ...m !== void 0 && { archived: m },
1302
+ ...a && { contentType: a },
1303
+ ...n.length > 0 && { toolCalls: n },
1318
1304
  ...r.length > 0 && { toolResults: r }
1319
1305
  };
1320
1306
  }
1321
- function se(t) {
1307
+ function ee(t) {
1322
1308
  const e = [];
1323
1309
  if (t.content && t.content !== "(No text content)" && e.push({
1324
1310
  type: "text",
@@ -1352,7 +1338,7 @@ function se(t) {
1352
1338
  role: t.role,
1353
1339
  kind: "message",
1354
1340
  parts: e,
1355
- messageId: q(),
1341
+ messageId: N(),
1356
1342
  metadata: {
1357
1343
  timestamp: t.timestamp,
1358
1344
  // only store archived if it was already present.
@@ -1362,65 +1348,65 @@ function se(t) {
1362
1348
  }
1363
1349
  };
1364
1350
  }
1365
- const U = /* @__PURE__ */ new Map(), ne = 50;
1351
+ const U = /* @__PURE__ */ new Map(), se = 50;
1366
1352
  async function ae(t, e, s) {
1367
- const n = s || t;
1368
- if (U.set(n, [...e]), U.size > ne) {
1369
- const a = U.keys().next().value;
1370
- a && U.delete(a);
1353
+ const a = s || t;
1354
+ if (U.set(a, [...e]), U.size > se) {
1355
+ const n = U.keys().next().value;
1356
+ n && U.delete(n);
1371
1357
  }
1372
1358
  if (!(typeof sessionStorage > "u"))
1373
1359
  try {
1374
- const a = {
1375
- storageKey: n,
1376
- messages: e.map(ee),
1360
+ const n = {
1361
+ storageKey: a,
1362
+ messages: e.map(te),
1377
1363
  lastUpdated: Date.now()
1378
1364
  };
1379
1365
  sessionStorage.setItem(
1380
- `${ct}_${n}`,
1381
- JSON.stringify(a)
1366
+ `${ct}_${a}`,
1367
+ JSON.stringify(n)
1382
1368
  );
1383
- } catch (a) {
1384
- R(
1369
+ } catch (n) {
1370
+ v(
1385
1371
  "Failed to store conversation in sessionStorage for key %s: %O",
1386
- n,
1387
- a
1372
+ a,
1373
+ n
1388
1374
  );
1389
1375
  }
1390
1376
  }
1391
- async function oe(t, e) {
1377
+ async function ne(t, e) {
1392
1378
  const s = e || t;
1393
1379
  if (U.has(s))
1394
1380
  return [...U.get(s)];
1395
1381
  if (typeof sessionStorage > "u")
1396
1382
  return [];
1397
1383
  try {
1398
- const n = sessionStorage.getItem(
1384
+ const a = sessionStorage.getItem(
1399
1385
  `${ct}_${s}`
1400
1386
  );
1401
- if (n) {
1402
- const r = JSON.parse(n).messages.map(se);
1387
+ if (a) {
1388
+ const r = JSON.parse(a).messages.map(ee);
1403
1389
  return U.set(s, r), [...r];
1404
1390
  }
1405
- } catch (n) {
1406
- R(
1391
+ } catch (a) {
1392
+ v(
1407
1393
  "Failed to load conversation from sessionStorage for key %s: %O",
1408
1394
  s,
1409
- n
1395
+ a
1410
1396
  );
1411
1397
  }
1412
1398
  return [];
1413
1399
  }
1414
- async function re(t, e) {
1400
+ async function oe(t, e) {
1415
1401
  const s = e || t;
1416
1402
  if (U.delete(s), !(typeof sessionStorage > "u"))
1417
1403
  try {
1418
1404
  sessionStorage.removeItem(`${ct}_${s}`);
1419
- } catch (n) {
1420
- R(
1405
+ } catch (a) {
1406
+ v(
1421
1407
  "Failed to clear conversation from sessionStorage for key %s: %O",
1422
1408
  s,
1423
- n
1409
+ a
1424
1410
  );
1425
1411
  }
1426
1412
  }
@@ -1435,27 +1421,27 @@ function L(t) {
1435
1421
  }
1436
1422
  };
1437
1423
  }
1438
- function ie(t) {
1424
+ function re(t) {
1439
1425
  const e = [];
1440
1426
  for (const s of t)
1441
- for (const n of s.parts)
1442
- if (n.type === "text")
1427
+ for (const a of s.parts)
1428
+ if (a.type === "text")
1443
1429
  e.push({
1444
1430
  type: "data",
1445
1431
  data: {
1446
1432
  role: s.role,
1447
- text: n.text
1433
+ text: a.text
1448
1434
  }
1449
1435
  });
1450
- else if (n.type === "data") {
1451
- if ("role" in n.data && "text" in n.data)
1436
+ else if (a.type === "data") {
1437
+ if ("role" in a.data && "text" in a.data)
1452
1438
  continue;
1453
- if ("toolCallId" in n.data && "arguments" in n.data) {
1454
- e.push(n);
1439
+ if ("toolCallId" in a.data && "arguments" in a.data) {
1440
+ e.push(a);
1455
1441
  continue;
1456
1442
  }
1457
- if ("toolCallId" in n.data && "result" in n.data) {
1458
- e.push(n);
1443
+ if ("toolCallId" in a.data && "result" in a.data) {
1444
+ e.push(a);
1459
1445
  continue;
1460
1446
  }
1461
1447
  }
@@ -1465,92 +1451,92 @@ function mt(t, e = []) {
1465
1451
  return {
1466
1452
  role: "user",
1467
1453
  parts: [
1468
- ...ie(e),
1454
+ ...re(e),
1469
1455
  {
1470
1456
  type: "text",
1471
1457
  text: t
1472
1458
  }
1473
1459
  ],
1474
1460
  kind: "message",
1475
- messageId: q(),
1461
+ messageId: N(),
1476
1462
  metadata: {
1477
1463
  timestamp: Date.now()
1478
1464
  }
1479
1465
  };
1480
1466
  }
1481
- function le(t) {
1467
+ function ie(t) {
1482
1468
  return t != null && t.parts ? t.parts.filter(
1483
1469
  (e) => e.type === "data" && "toolCallId" in e.data && "result" in e.data
1484
1470
  ) : [];
1485
1471
  }
1486
- async function ce(t) {
1472
+ async function le(t) {
1487
1473
  const e = [];
1488
1474
  for (const s of t)
1489
1475
  if (s.parts && Array.isArray(s.parts))
1490
1476
  if (s.parts.some(
1491
- (a) => a.type === "data" && "toolCallId" in a.data && "result" in a.data
1477
+ (n) => n.type === "data" && "toolCallId" in n.data && "result" in n.data
1492
1478
  )) {
1493
- const a = Zt(
1479
+ const n = Yt(
1494
1480
  s.parts
1495
1481
  );
1496
1482
  e.push({
1497
1483
  ...s,
1498
- parts: a
1484
+ parts: n
1499
1485
  });
1500
1486
  } else
1501
1487
  e.push(s);
1502
1488
  else
1503
1489
  e.push(s);
1504
- return Yt(), e;
1490
+ return Gt(), e;
1505
1491
  }
1506
- function ue() {
1492
+ function ce() {
1507
1493
  const t = /* @__PURE__ */ new Map();
1508
- async function e(s, n) {
1509
- const a = t.get(s);
1510
- if (a != null && a.sessionId)
1494
+ async function e(s, a) {
1495
+ const n = t.get(s);
1496
+ if (n != null && n.sessionId)
1511
1497
  try {
1512
1498
  await ae(
1513
- a.sessionId,
1514
- n,
1515
- a.conversationStorageKey
1499
+ n.sessionId,
1500
+ a,
1501
+ n.conversationStorageKey
1516
1502
  );
1517
1503
  } catch (r) {
1518
- dt(
1504
+ ut(
1519
1505
  `Failed to persist conversation history for agent ${s}:`,
1520
1506
  r
1521
1507
  );
1522
1508
  }
1523
1509
  }
1524
1510
  return {
1525
- async createAgent(s, n) {
1511
+ async createAgent(s, a) {
1526
1512
  if (t.has(s))
1527
1513
  return t.get(s).client;
1528
- const a = te(n), r = n.sessionId || null, o = n.conversationStorageKey;
1529
- let c = [];
1514
+ const n = Zt(a), r = a.sessionId || null, o = a.conversationStorageKey;
1515
+ let l = [];
1530
1516
  if (r)
1531
1517
  try {
1532
- c = await oe(
1518
+ l = await ne(
1533
1519
  r,
1534
1520
  o
1535
1521
  );
1536
- } catch (f) {
1537
- dt(
1522
+ } catch (m) {
1523
+ ut(
1538
1524
  `Failed to load conversation history for agent ${s} with session ${r}:`,
1539
- f
1525
+ m
1540
1526
  );
1541
1527
  }
1542
1528
  const i = {
1543
- client: a,
1529
+ client: n,
1544
1530
  sessionId: r,
1545
1531
  conversationStorageKey: o,
1546
- conversationHistory: c,
1532
+ conversationHistory: l,
1547
1533
  currentAbortController: null
1548
1534
  };
1549
- return t.set(s, i), a;
1535
+ return t.set(s, i), n;
1550
1536
  },
1551
1537
  getAgent(s) {
1552
- const n = t.get(s);
1553
- return (n == null ? void 0 : n.client) || null;
1538
+ const a = t.get(s);
1539
+ return (a == null ? void 0 : a.client) || null;
1554
1540
  },
1555
1541
  hasAgent(s) {
1556
1542
  return t.has(s);
@@ -1558,257 +1544,257 @@ function ue() {
1558
1544
  removeAgent(s) {
1559
1545
  return t.delete(s);
1560
1546
  },
1561
- async sendMessage(s, n, a = {}) {
1562
- var m;
1547
+ async sendMessage(s, a, n = {}) {
1548
+ var c;
1563
1549
  const r = t.get(s);
1564
1550
  if (!r)
1565
1551
  throw new Error(`Agent with key "${s}" not found`);
1566
- const { withHistory: o = !0, ...c } = a, { client: i, conversationHistory: f } = r, u = a.message || mt(n, f), p = await i.sendMessage({
1567
- message: u,
1552
+ const { withHistory: o = !0, ...l } = n, { client: i, conversationHistory: m } = r, g = n.message || mt(a, m), h = await i.sendMessage({
1553
+ message: g,
1568
1554
  withHistory: o,
1569
- ...c
1555
+ ...l
1570
1556
  });
1571
- let g = null;
1572
- if ((m = p.status) != null && m.message) {
1573
- const l = p.status.message.parts.filter(
1557
+ let f = null;
1558
+ if ((c = h.status) != null && c.message) {
1559
+ const u = h.status.message.parts.filter(
1574
1560
  (d) => d.type === "data" && "toolCallId" in d.data && ("arguments" in d.data || "result" in d.data)
1575
- ), y = p.status.message.parts.filter(
1561
+ ), w = h.status.message.parts.filter(
1576
1562
  (d) => d.type === "text"
1577
1563
  );
1578
- g = {
1564
+ f = {
1579
1565
  role: "agent",
1580
1566
  kind: "message",
1581
- parts: [...l, ...y],
1582
- messageId: q(),
1567
+ parts: [...u, ...w],
1568
+ messageId: N(),
1583
1569
  metadata: {
1584
1570
  timestamp: Date.now()
1585
1571
  }
1586
1572
  };
1587
1573
  }
1588
- const h = [
1589
- ...f,
1574
+ const p = [
1575
+ ...m,
1590
1576
  // Store only the new content from the user message (without history parts)
1591
- at(n),
1577
+ nt(a),
1592
1578
  // Add complete agent response with tool calls/results if present
1593
- ...g ? [L(g)] : []
1579
+ ...f ? [L(f)] : []
1594
1580
  ];
1595
- let M = h;
1596
- if (p.agentMessage) {
1597
- const l = L(
1598
- p.agentMessage
1581
+ let S = p;
1582
+ if (h.agentMessage) {
1583
+ const u = L(
1584
+ h.agentMessage
1599
1585
  );
1600
- M = [
1601
- ...h,
1602
- l
1586
+ S = [
1587
+ ...p,
1588
+ u
1603
1589
  ];
1604
1590
  }
1605
- return r.conversationHistory = M, o && await e(
1591
+ return r.conversationHistory = S, o && await e(
1606
1592
  s,
1607
- M
1608
- ), p;
1593
+ S
1594
+ ), h;
1609
1595
  },
1610
- async *sendMessageStream(s, n, a = {}) {
1611
- var d, I, k, w, P, T;
1596
+ async *sendMessageStream(s, a, n = {}) {
1597
+ var d, y, x, I, P, T;
1612
1598
  const r = t.get(s);
1613
1599
  if (!r)
1614
1600
  throw new Error(`Agent with key "${s}" not found`);
1615
1601
  const {
1616
1602
  withHistory: o = !0,
1617
- abortSignal: c,
1603
+ abortSignal: l,
1618
1604
  metadata: i,
1619
- ...f
1620
- } = a, { client: u } = r, p = i ? (({ contentType: x, ...v }) => v)(i) : void 0, g = new AbortController();
1621
- r.currentAbortController = g, c && c.addEventListener(
1605
+ ...m
1606
+ } = n, { client: g } = r, h = i ? (({ contentType: b, ...k }) => k)(i) : void 0, f = new AbortController();
1607
+ r.currentAbortController = f, l && l.addEventListener(
1622
1608
  "abort",
1623
- () => g.abort()
1609
+ () => f.abort()
1624
1610
  );
1625
- let h = [
1611
+ let p = [
1626
1612
  ...r.conversationHistory
1627
- ], M = [];
1628
- const m = await ce(
1629
- h
1613
+ ], S = [];
1614
+ const c = await le(
1615
+ p
1630
1616
  );
1631
- r.conversationHistory = m, h = m, o && await e(
1617
+ r.conversationHistory = c, p = c, o && await e(
1632
1618
  s,
1633
- m
1619
+ c
1634
1620
  );
1635
- const l = a.message || mt(
1636
- n,
1637
- m
1621
+ const u = n.message || mt(
1622
+ a,
1623
+ c
1638
1624
  );
1639
- if (a.metadata && !a.message) {
1640
- const { contentType: x, ...v } = a.metadata;
1641
- if (x) {
1642
- const C = l.parts[l.parts.length - 1];
1643
- C && C.type === "text" && (C.metadata = {
1644
- ...C.metadata,
1645
- contentType: x
1625
+ if (n.metadata && !n.message) {
1626
+ const { contentType: b, ...k } = n.metadata;
1627
+ if (b) {
1628
+ const M = u.parts[u.parts.length - 1];
1629
+ M && M.type === "text" && (M.metadata = {
1630
+ ...M.metadata,
1631
+ contentType: b
1646
1632
  });
1647
1633
  }
1648
- Object.keys(v).length > 0 && (l.metadata = {
1649
- ...l.metadata,
1650
- ...v
1634
+ Object.keys(k).length > 0 && (u.metadata = {
1635
+ ...u.metadata,
1636
+ ...k
1651
1637
  });
1652
1638
  }
1653
- const y = at(n, a.metadata);
1654
- h = [
1655
- ...h,
1656
- y
1657
- ], r.conversationHistory = h, o && await e(
1639
+ const w = nt(a, n.metadata);
1640
+ p = [
1641
+ ...p,
1642
+ w
1643
+ ], r.conversationHistory = p, o && await e(
1658
1644
  s,
1659
- h
1645
+ p
1660
1646
  );
1661
- for await (const x of u.sendMessageStream({
1662
- message: l,
1647
+ for await (const b of g.sendMessageStream({
1648
+ message: u,
1663
1649
  withHistory: o,
1664
- abortSignal: g.signal,
1665
- ...f,
1666
- ...p && Object.keys(p).length > 0 && {
1667
- metadata: p
1650
+ abortSignal: f.signal,
1651
+ ...m,
1652
+ ...h && Object.keys(h).length > 0 && {
1653
+ metadata: h
1668
1654
  }
1669
1655
  })) {
1670
- if (((d = x.status) == null ? void 0 : d.state) === "input-required" && ((I = x.status) != null && I.message)) {
1671
- M = F(
1672
- x.status.message
1656
+ if (((d = b.status) == null ? void 0 : d.state) === "input-required" && ((y = b.status) != null && y.message)) {
1657
+ S = F(
1658
+ b.status.message
1673
1659
  ).map(
1674
- (S) => S.data.toolCallId
1660
+ (C) => C.data.toolCallId
1675
1661
  );
1676
- const C = L(
1677
- x.status.message
1662
+ const M = L(
1663
+ b.status.message
1678
1664
  );
1679
- h = [
1680
- ...h,
1681
- C
1682
- ], r.conversationHistory = h, o && await e(
1665
+ p = [
1666
+ ...p,
1667
+ M
1668
+ ], r.conversationHistory = p, o && await e(
1683
1669
  s,
1684
- h
1670
+ p
1685
1671
  );
1686
1672
  }
1687
- if (((k = x.status) == null ? void 0 : k.state) === "working" && ((w = x.status) != null && w.message) && !x.final) {
1688
- const C = le(
1689
- x.status.message
1673
+ if (((x = b.status) == null ? void 0 : x.state) === "working" && ((I = b.status) != null && I.message) && !b.final) {
1674
+ const M = ie(
1675
+ b.status.message
1690
1676
  ).filter(
1691
- (S) => M.includes(
1692
- S.data.toolCallId
1677
+ (C) => S.includes(
1678
+ C.data.toolCallId
1693
1679
  )
1694
1680
  );
1695
- if (C.length > 0) {
1696
- const S = {
1681
+ if (M.length > 0) {
1682
+ const C = {
1697
1683
  role: "agent",
1698
1684
  kind: "message",
1699
- parts: C,
1700
- messageId: q()
1685
+ parts: M,
1686
+ messageId: N()
1701
1687
  };
1702
- h = [
1703
- ...h,
1704
- L(S)
1705
- ], r.conversationHistory = h, o && await e(
1688
+ p = [
1689
+ ...p,
1690
+ L(C)
1691
+ ], r.conversationHistory = p, o && await e(
1706
1692
  s,
1707
- h
1693
+ p
1708
1694
  );
1709
1695
  }
1710
1696
  }
1711
- if (x.final && ((P = x.status) == null ? void 0 : P.state) !== "input-required") {
1712
- M = [];
1713
- let v = null;
1714
- (T = x.status) != null && T.message && (v = L(
1715
- x.status.message
1716
- ), h = [
1717
- ...h,
1718
- v
1719
- ], r.conversationHistory = h, o && await e(
1697
+ if (b.final && ((P = b.status) == null ? void 0 : P.state) !== "input-required") {
1698
+ S = [];
1699
+ let k = null;
1700
+ (T = b.status) != null && T.message && (k = L(
1701
+ b.status.message
1702
+ ), p = [
1703
+ ...p,
1704
+ k
1705
+ ], r.conversationHistory = p, o && await e(
1720
1706
  s,
1721
- h
1707
+ p
1722
1708
  ));
1723
1709
  }
1724
- yield x;
1710
+ yield b;
1725
1711
  }
1726
1712
  r.currentAbortController = null;
1727
1713
  },
1728
1714
  async resetConversation(s) {
1729
- const n = t.get(s);
1730
- if (!n)
1715
+ const a = t.get(s);
1716
+ if (!a)
1731
1717
  throw new Error(`Agent with key "${s}" not found`);
1732
- n.conversationHistory = [], n.sessionId && await re(
1733
- n.sessionId,
1734
- n.conversationStorageKey
1718
+ a.conversationHistory = [], a.sessionId && await oe(
1719
+ a.sessionId,
1720
+ a.conversationStorageKey
1735
1721
  );
1736
1722
  },
1737
1723
  getConversationHistory(s) {
1738
- const n = t.get(s);
1739
- if (!n)
1724
+ const a = t.get(s);
1725
+ if (!a)
1740
1726
  throw new Error(`Agent with key "${s}" not found`);
1741
- return [...n.conversationHistory];
1727
+ return [...a.conversationHistory];
1742
1728
  },
1743
1729
  abortCurrentRequest(s) {
1744
- const n = t.get(s);
1745
- if (!n)
1730
+ const a = t.get(s);
1731
+ if (!a)
1746
1732
  throw new Error(`Agent with key "${s}" not found`);
1747
- n.currentAbortController && (n.currentAbortController.abort(), n.currentAbortController = null);
1733
+ a.currentAbortController && (a.currentAbortController.abort(), a.currentAbortController = null);
1748
1734
  },
1749
1735
  clear() {
1750
1736
  t.clear();
1751
1737
  }
1752
1738
  };
1753
1739
  }
1754
- const de = ue();
1740
+ const ue = ce();
1755
1741
  function st() {
1756
- return de;
1742
+ return ue;
1757
1743
  }
1758
- function ge() {
1744
+ function de() {
1759
1745
  const [t, e] = yt([]), s = $(
1760
1746
  (r) => {
1761
1747
  e((o) => {
1762
- const c = o.findIndex(
1748
+ const l = o.findIndex(
1763
1749
  (i) => i.id === r.id
1764
1750
  );
1765
- if (c >= 0) {
1751
+ if (l >= 0) {
1766
1752
  const i = [...o];
1767
- return i[c] = r, i;
1753
+ return i[l] = r, i;
1768
1754
  }
1769
1755
  return [...o, r];
1770
1756
  });
1771
1757
  },
1772
1758
  []
1773
- ), n = $((r) => {
1774
- e((o) => o.filter((c) => c.id !== r));
1775
- }, []), a = $(() => {
1759
+ ), a = $((r) => {
1760
+ e((o) => o.filter((l) => l.id !== r));
1761
+ }, []), n = $(() => {
1776
1762
  e([]);
1777
1763
  }, []);
1778
1764
  return {
1779
1765
  registerMessageActions: s,
1780
- unregisterMessageActions: n,
1781
- clearAllMessageActions: a,
1766
+ unregisterMessageActions: a,
1767
+ clearAllMessageActions: n,
1782
1768
  registrations: t
1783
1769
  };
1784
1770
  }
1785
- function fe(t, e) {
1786
- return e.flatMap((a) => typeof a.actions == "function" ? a.actions(t) : a.actions).filter((a) => !(a.condition && !a.condition(t))).map((a) => ({
1787
- id: a.id,
1788
- label: a.label,
1789
- icon: a.icon,
1790
- onClick: a.onClick,
1791
- tooltip: a.tooltip,
1792
- disabled: a.disabled || !1,
1793
- pressed: a.pressed,
1794
- showLabel: a.showLabel
1771
+ function ge(t, e) {
1772
+ return e.flatMap((n) => typeof n.actions == "function" ? n.actions(t) : n.actions).filter((n) => !(n.condition && !n.condition(t))).map((n) => ({
1773
+ id: n.id,
1774
+ label: n.label,
1775
+ icon: n.icon,
1776
+ onClick: n.onClick,
1777
+ tooltip: n.tooltip,
1778
+ disabled: n.disabled || !1,
1779
+ pressed: n.pressed,
1780
+ showLabel: n.showLabel
1795
1781
  }));
1796
1782
  }
1797
- const nt = (t) => [...t].sort((e, s) => e.timestamp - s.timestamp), z = (t, e = []) => {
1798
- var o, c;
1783
+ const at = (t) => [...t].sort((e, s) => e.timestamp - s.timestamp), z = (t, e = []) => {
1784
+ var o, l;
1799
1785
  if (t.parts.some((i) => {
1800
1786
  if (i.type === "data") {
1801
- const f = i.data;
1802
- return f.toolCallId || f.toolId || f.result;
1787
+ const m = i.data;
1788
+ return m.toolCallId || m.toolId || m.result;
1803
1789
  }
1804
1790
  return !1;
1805
1791
  }))
1806
1792
  return null;
1807
- const n = t.parts.map((i) => {
1808
- var f;
1793
+ const a = t.parts.map((i) => {
1794
+ var m;
1809
1795
  if (i.type === "text")
1810
1796
  return {
1811
- type: ((f = i.metadata) == null ? void 0 : f.contentType) || "text",
1797
+ type: ((m = i.metadata) == null ? void 0 : m.contentType) || "text",
1812
1798
  text: i.text
1813
1799
  };
1814
1800
  if (i.type === "file")
@@ -1817,55 +1803,55 @@ const nt = (t) => [...t].sort((e, s) => e.timestamp - s.timestamp), z = (t, e =
1817
1803
  image_url: i.file.uri || `data:${i.file.mimeType};base64,${i.file.bytes}`
1818
1804
  };
1819
1805
  if (i.type === "data") {
1820
- const u = i.data;
1821
- return u.component && u.componentProps ? {
1806
+ const g = i.data;
1807
+ return g.component && g.componentProps ? {
1822
1808
  type: "component",
1823
- component: u.component,
1824
- componentProps: u.componentProps
1809
+ component: g.component,
1810
+ componentProps: g.componentProps
1825
1811
  } : {
1826
1812
  type: "text",
1827
- text: JSON.stringify(u)
1813
+ text: JSON.stringify(g)
1828
1814
  };
1829
1815
  }
1830
1816
  return {
1831
1817
  type: "text",
1832
1818
  text: "[Unsupported content]"
1833
1819
  };
1834
- }), a = ((o = t.metadata) == null ? void 0 : o.timestamp) ?? Date.now(), r = {
1820
+ }), n = ((o = t.metadata) == null ? void 0 : o.timestamp) ?? Date.now(), r = {
1835
1821
  id: t.messageId,
1836
1822
  role: t.role === "agent" ? "agent" : "user",
1837
- content: n,
1838
- timestamp: a,
1839
- archived: !!((c = t.metadata) != null && c.archived),
1823
+ content: a,
1824
+ timestamp: n,
1825
+ archived: !!((l = t.metadata) != null && l.archived),
1840
1826
  showIcon: t.role === "agent",
1841
1827
  icon: t.role === "agent" ? "assistant" : void 0
1842
1828
  };
1843
1829
  if (t.role === "agent" && e.length > 0) {
1844
- const i = fe(
1830
+ const i = ge(
1845
1831
  r,
1846
1832
  e
1847
1833
  );
1848
1834
  i.length > 0 && (r.actions = i);
1849
1835
  }
1850
1836
  return r;
1851
- }, me = () => ({
1837
+ }, fe = () => ({
1852
1838
  getClientContext: () => ({})
1853
- }), he = () => ({
1839
+ }), me = () => ({
1854
1840
  getAvailableTools: async () => [],
1855
1841
  executeTool: async () => ({
1856
1842
  success: !0,
1857
1843
  result: "No tools available"
1858
1844
  })
1859
1845
  }), pe = (t) => ["agentId", "agentUrl", "sessionId"].every((s) => {
1860
- const n = t[s];
1861
- return typeof n == "string" && n.trim().length > 0;
1846
+ const a = t[s];
1847
+ return typeof a == "string" && a.trim().length > 0;
1862
1848
  });
1863
1849
  function Ee(t) {
1864
1850
  const e = {
1865
1851
  agentId: t.agentId,
1866
1852
  agentUrl: t.agentUrl,
1867
1853
  sessionId: t.sessionId
1868
- }, s = pe(e), [n, a] = yt({
1854
+ }, s = pe(e), [a, n] = yt({
1869
1855
  clientMessages: [],
1870
1856
  uiMessages: [],
1871
1857
  isProcessing: !1,
@@ -1874,38 +1860,38 @@ function Ee(t) {
1874
1860
  }), {
1875
1861
  registerMessageActions: r,
1876
1862
  unregisterMessageActions: o,
1877
- clearAllMessageActions: c,
1863
+ clearAllMessageActions: l,
1878
1864
  registrations: i
1879
- } = ge(), f = Ot(i);
1865
+ } = de(), m = Ot(i);
1880
1866
  tt(() => {
1881
- f.current = i;
1867
+ m.current = i;
1882
1868
  }, [i]), tt(() => {
1883
1869
  if (!s)
1884
1870
  return;
1885
1871
  (async () => {
1886
1872
  if (e.sessionId) {
1887
- const l = st(), y = `${e.agentId}-${e.sessionId}`;
1888
- l.hasAgent(y) || await l.createAgent(y, {
1873
+ const u = st(), w = `${e.agentId}-${e.sessionId}`;
1874
+ u.hasAgent(w) || await u.createAgent(w, {
1889
1875
  agentId: e.agentId,
1890
1876
  agentUrl: e.agentUrl,
1891
1877
  sessionId: e.sessionId,
1892
- contextProvider: t.contextProvider || me(),
1893
- toolProvider: t.toolProvider || he(),
1878
+ contextProvider: t.contextProvider || fe(),
1879
+ toolProvider: t.toolProvider || me(),
1894
1880
  authProvider: t.authProvider,
1895
1881
  enableStreaming: t.enableStreaming
1896
1882
  });
1897
- const d = l.getConversationHistory(y);
1898
- a((I) => {
1899
- const k = d.map(
1900
- (w) => z(
1901
- w,
1902
- f.current
1883
+ const d = u.getConversationHistory(w);
1884
+ n((y) => {
1885
+ const x = d.map(
1886
+ (I) => z(
1887
+ I,
1888
+ m.current
1903
1889
  )
1904
- ).filter((w) => w !== null);
1890
+ ).filter((I) => I !== null);
1905
1891
  return {
1906
- ...I,
1892
+ ...y,
1907
1893
  clientMessages: d,
1908
- uiMessages: k
1894
+ uiMessages: x
1909
1895
  };
1910
1896
  });
1911
1897
  }
@@ -1920,277 +1906,277 @@ function Ee(t) {
1920
1906
  t.enableStreaming,
1921
1907
  s
1922
1908
  ]);
1923
- const u = $(
1924
- async (m, l) => {
1909
+ const g = $(
1910
+ async (c, u) => {
1925
1911
  var P;
1926
1912
  if (!s)
1927
1913
  throw new Error("Invalid agent configuration");
1928
- const y = st(), d = `${e.agentId}-${e.sessionId}`, I = Date.now(), k = (l == null ? void 0 : l.type) || "text", w = {
1929
- id: `user-${I}`,
1914
+ const w = st(), d = `${e.agentId}-${e.sessionId}`, y = Date.now(), x = (u == null ? void 0 : u.type) || "text", I = {
1915
+ id: `user-${y}`,
1930
1916
  role: "user",
1931
- content: [{ type: k, text: m }],
1932
- timestamp: I,
1933
- archived: (l == null ? void 0 : l.archived) ?? !1,
1917
+ content: [{ type: x, text: c }],
1918
+ timestamp: y,
1919
+ archived: (u == null ? void 0 : u.archived) ?? !1,
1934
1920
  showIcon: !1
1935
1921
  };
1936
- a((T) => ({
1922
+ n((T) => ({
1937
1923
  ...T,
1938
- uiMessages: [...T.uiMessages, w],
1924
+ uiMessages: [...T.uiMessages, I],
1939
1925
  isProcessing: !0,
1940
1926
  error: null
1941
1927
  }));
1942
1928
  try {
1943
- let T = null, x = !1;
1944
- const v = {};
1945
- (l != null && l.archived || l != null && l.type) && (v.metadata = {
1946
- ...(l == null ? void 0 : l.archived) && { archived: !0 },
1947
- ...(l == null ? void 0 : l.type) && { contentType: l.type }
1929
+ let T = null, b = !1;
1930
+ const k = {};
1931
+ (u != null && u.archived || u != null && u.type) && (k.metadata = {
1932
+ ...(u == null ? void 0 : u.archived) && { archived: !0 },
1933
+ ...(u == null ? void 0 : u.type) && { contentType: u.type }
1948
1934
  });
1949
- for await (const C of y.sendMessageStream(
1935
+ for await (const M of w.sendMessageStream(
1950
1936
  d,
1951
- m,
1952
- v
1937
+ c,
1938
+ k
1953
1939
  )) {
1954
- if (!C.final && C.text)
1940
+ if (!M.final && M.text)
1955
1941
  if (T)
1956
- a((S) => ({
1957
- ...S,
1958
- uiMessages: S.uiMessages.map(
1959
- (b) => b.id === T ? {
1960
- ...b,
1942
+ n((C) => ({
1943
+ ...C,
1944
+ uiMessages: C.uiMessages.map(
1945
+ (A) => A.id === T ? {
1946
+ ...A,
1961
1947
  content: [
1962
1948
  {
1963
1949
  type: "text",
1964
- text: C.text
1950
+ text: M.text
1965
1951
  }
1966
1952
  ]
1967
- } : b
1953
+ } : A
1968
1954
  )
1969
1955
  }));
1970
1956
  else {
1971
1957
  T = `agent-streaming-${Date.now()}`;
1972
- const S = {
1958
+ const C = {
1973
1959
  id: T,
1974
1960
  role: "agent",
1975
1961
  content: [
1976
- { type: "text", text: C.text }
1962
+ { type: "text", text: M.text }
1977
1963
  ],
1978
1964
  timestamp: Date.now(),
1979
1965
  archived: !1,
1980
1966
  showIcon: !0,
1981
1967
  icon: "assistant"
1982
1968
  };
1983
- a((b) => ({
1984
- ...b,
1969
+ n((A) => ({
1970
+ ...A,
1985
1971
  uiMessages: [
1986
- ...b.uiMessages,
1987
- S
1972
+ ...A.uiMessages,
1973
+ C
1988
1974
  ]
1989
1975
  }));
1990
1976
  }
1991
- if (C.final && ((P = C.status) != null && P.message) && T) {
1992
- x = !0;
1993
- const S = T, b = z(
1994
- C.status.message,
1995
- f.current
1977
+ if (M.final && ((P = M.status) != null && P.message) && T) {
1978
+ b = !0;
1979
+ const C = T, A = z(
1980
+ M.status.message,
1981
+ m.current
1996
1982
  );
1997
- b && a((_) => {
1998
- const N = _.uiMessages.map(
1999
- (A) => A.id === S ? b : A
2000
- ), E = y.getConversationHistory(
1983
+ A && n((q) => {
1984
+ const _ = q.uiMessages.map(
1985
+ (R) => R.id === C ? A : R
1986
+ ), E = w.getConversationHistory(
2001
1987
  d
2002
1988
  );
2003
1989
  return {
2004
- ..._,
1990
+ ...q,
2005
1991
  clientMessages: E,
2006
- uiMessages: N,
1992
+ uiMessages: _,
2007
1993
  isProcessing: !1
2008
1994
  };
2009
1995
  }), T = null;
2010
1996
  }
2011
1997
  }
2012
- if (!x) {
2013
- const C = y.getConversationHistory(d);
2014
- a((S) => {
2015
- let b = S.uiMessages;
2016
- T && (b = S.uiMessages.filter(
1998
+ if (!b) {
1999
+ const M = w.getConversationHistory(d);
2000
+ n((C) => {
2001
+ let A = C.uiMessages;
2002
+ T && (A = C.uiMessages.filter(
2017
2003
  (O) => O.id !== T
2018
2004
  ));
2019
- const _ = C.map(
2005
+ const q = M.map(
2020
2006
  (O) => z(
2021
2007
  O,
2022
- f.current
2008
+ m.current
2023
2009
  )
2024
2010
  ).filter(
2025
2011
  (O) => O !== null
2026
- ), N = new Set(
2027
- C.map((O) => O.messageId)
2028
- ), E = b.filter(
2012
+ ), _ = new Set(
2013
+ M.map((O) => O.messageId)
2014
+ ), E = A.filter(
2029
2015
  (O) => {
2030
2016
  var J;
2031
- return !N.has(O.id) && ((J = O.content[0]) == null ? void 0 : J.type) === "component";
2017
+ return !_.has(O.id) && ((J = O.content[0]) == null ? void 0 : J.type) === "component";
2032
2018
  }
2033
- ), A = nt([
2034
- ..._,
2019
+ ), R = at([
2020
+ ...q,
2035
2021
  ...E
2036
2022
  ]);
2037
2023
  return {
2038
- ...S,
2039
- clientMessages: C,
2040
- uiMessages: A,
2024
+ ...C,
2025
+ clientMessages: M,
2026
+ uiMessages: R,
2041
2027
  isProcessing: !1
2042
2028
  };
2043
2029
  });
2044
2030
  }
2045
2031
  } catch (T) {
2046
2032
  if (T instanceof Error && T.name === "AbortError") {
2047
- console.log("Request was aborted by user"), a((v) => ({
2048
- ...v,
2033
+ v("Request was aborted by user"), n((k) => ({
2034
+ ...k,
2049
2035
  isProcessing: !1,
2050
2036
  error: null
2051
2037
  // Don't show error for user-initiated abort
2052
2038
  }));
2053
2039
  return;
2054
2040
  }
2055
- const x = T instanceof Error ? T.message : "Failed to send message";
2056
- throw a((v) => ({
2057
- ...v,
2041
+ const b = T instanceof Error ? T.message : "Failed to send message";
2042
+ throw n((k) => ({
2043
+ ...k,
2058
2044
  isProcessing: !1,
2059
- error: x
2045
+ error: b
2060
2046
  })), T;
2061
2047
  }
2062
2048
  },
2063
2049
  [e.agentId, e.sessionId, s]
2064
- ), p = $((m) => {
2065
- a((l) => ({
2066
- ...l,
2067
- uiMessages: nt([...l.uiMessages, m])
2050
+ ), h = $((c) => {
2051
+ n((u) => ({
2052
+ ...u,
2053
+ uiMessages: at([...u.uiMessages, c])
2068
2054
  }));
2069
- }, []), g = $((m) => {
2070
- a((l) => ({
2071
- ...l,
2072
- suggestions: m
2055
+ }, []), f = $((c) => {
2056
+ n((u) => ({
2057
+ ...u,
2058
+ suggestions: c
2073
2059
  }));
2074
- }, []), h = $(() => {
2075
- a((m) => ({
2076
- ...m,
2060
+ }, []), p = $(() => {
2061
+ n((c) => ({
2062
+ ...c,
2077
2063
  suggestions: []
2078
2064
  }));
2079
2065
  }, []);
2080
2066
  tt(() => {
2081
- a((m) => {
2082
- if (m.clientMessages.length === 0)
2083
- return m;
2084
- const l = m.clientMessages.map(
2085
- (I) => z(I, f.current)
2086
- ).filter((I) => I !== null), y = new Set(
2087
- m.clientMessages.map((I) => I.messageId)
2088
- ), d = m.uiMessages.filter(
2089
- (I) => {
2090
- var k;
2091
- return !y.has(I.id) && ((k = I.content[0]) == null ? void 0 : k.type) === "component";
2067
+ n((c) => {
2068
+ if (c.clientMessages.length === 0)
2069
+ return c;
2070
+ const u = c.clientMessages.map(
2071
+ (y) => z(y, m.current)
2072
+ ).filter((y) => y !== null), w = new Set(
2073
+ c.clientMessages.map((y) => y.messageId)
2074
+ ), d = c.uiMessages.filter(
2075
+ (y) => {
2076
+ var x;
2077
+ return !w.has(y.id) && ((x = y.content[0]) == null ? void 0 : x.type) === "component";
2092
2078
  }
2093
2079
  );
2094
2080
  return {
2095
- ...m,
2096
- uiMessages: nt([
2097
- ...l,
2081
+ ...c,
2082
+ uiMessages: at([
2083
+ ...u,
2098
2084
  ...d
2099
2085
  ])
2100
2086
  };
2101
2087
  });
2102
2088
  }, [i]);
2103
- const M = $(() => {
2089
+ const S = $(() => {
2104
2090
  if (!s)
2105
2091
  return;
2106
- const m = st(), l = `${e.agentId}-${e.sessionId}`;
2107
- m.abortCurrentRequest(l);
2092
+ const c = st(), u = `${e.agentId}-${e.sessionId}`;
2093
+ c.abortCurrentRequest(u);
2108
2094
  }, [e.agentId, e.sessionId, s]);
2109
2095
  return {
2110
2096
  // AgentUI props
2111
- messages: n.uiMessages,
2112
- isProcessing: n.isProcessing,
2113
- error: n.error,
2114
- onSubmit: u,
2115
- suggestions: n.suggestions,
2097
+ messages: a.uiMessages,
2098
+ isProcessing: a.isProcessing,
2099
+ error: a.error,
2100
+ onSubmit: g,
2101
+ suggestions: a.suggestions,
2116
2102
  // UI management methods
2117
- registerSuggestions: g,
2118
- clearSuggestions: h,
2103
+ registerSuggestions: f,
2104
+ clearSuggestions: p,
2119
2105
  // Message actions methods
2120
2106
  registerMessageActions: r,
2121
2107
  unregisterMessageActions: o,
2122
- clearAllMessageActions: c,
2108
+ clearAllMessageActions: l,
2123
2109
  // Tool integration
2124
- addMessage: p,
2110
+ addMessage: h,
2125
2111
  // Abort control
2126
- abortCurrentRequest: M
2112
+ abortCurrentRequest: S
2127
2113
  };
2128
2114
  }
2129
- var ye = /* @__PURE__ */ ((t) => (t[t.PARSE_ERROR = -32700] = "PARSE_ERROR", t[t.INVALID_REQUEST = -32600] = "INVALID_REQUEST", t[t.METHOD_NOT_FOUND = -32601] = "METHOD_NOT_FOUND", t[t.INVALID_PARAMS = -32602] = "INVALID_PARAMS", t[t.INTERNAL_ERROR = -32603] = "INTERNAL_ERROR", t[t.SERVER_ERROR = -32e3] = "SERVER_ERROR", t))(ye || {});
2130
- const ht = "jetpack-ai-jwt-token", we = 30 * 60 * 1e3;
2131
- function Ie() {
2132
- var s;
2133
- return window.JP_CONNECTION_INITIAL_STATE ? !1 : !!((s = window.Jetpack_Editor_Initial_State) != null && s.wpcomBlogId);
2115
+ var he = /* @__PURE__ */ ((t) => (t[t.PARSE_ERROR = -32700] = "PARSE_ERROR", t[t.INVALID_REQUEST = -32600] = "INVALID_REQUEST", t[t.METHOD_NOT_FOUND = -32601] = "METHOD_NOT_FOUND", t[t.INVALID_PARAMS = -32602] = "INVALID_PARAMS", t[t.INTERNAL_ERROR = -32603] = "INTERNAL_ERROR", t[t.SERVER_ERROR = -32e3] = "SERVER_ERROR", t))(he || {});
2116
+ const pt = "jetpack-ai-jwt-token", ye = 30 * 60 * 1e3;
2117
+ function we() {
2118
+ var s, a, n;
2119
+ 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);
2134
2120
  }
2135
- async function Te(t, e = !0) {
2136
- var i, f;
2137
- const s = localStorage.getItem(ht);
2138
- let n;
2121
+ async function Ie(t, e = !0) {
2122
+ var i, m;
2123
+ const s = localStorage.getItem(pt);
2124
+ let a;
2139
2125
  if (s)
2140
2126
  try {
2141
- n = JSON.parse(s);
2142
- } catch (u) {
2143
- console.warn("Invalid cached Jetpack token:", u);
2127
+ a = JSON.parse(s);
2128
+ } catch (g) {
2129
+ v("Invalid cached Jetpack token: %O", g);
2144
2130
  }
2145
- if (n && (n != null && n.token) && (n != null && n.expire) && (n == null ? void 0 : n.expire) > Date.now() && e)
2146
- return n;
2147
- const a = (i = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : i.apiNonce, r = (f = window.Jetpack_Editor_Initial_State) == null ? void 0 : f.wpcomBlogId;
2131
+ if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() && e)
2132
+ return a;
2133
+ const n = (i = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : i.apiNonce, r = (m = window.Jetpack_Editor_Initial_State) == null ? void 0 : m.wpcomBlogId;
2148
2134
  let o = {
2149
2135
  token: "",
2150
2136
  blog_id: ""
2151
2137
  };
2152
2138
  try {
2153
- Ie() ? o = await ut({
2139
+ we() ? o = await dt({
2154
2140
  path: "/wpcom/v2/sites/" + r + "/jetpack-openai-query/jwt",
2155
2141
  method: "POST"
2156
- }) : o = await ut({
2142
+ }) : o = await dt({
2157
2143
  path: "/jetpack/v4/jetpack-ai-jwt?_cacheBuster=" + Date.now(),
2158
2144
  credentials: "same-origin",
2159
2145
  headers: {
2160
- "X-WP-Nonce": a || ""
2146
+ "X-WP-Nonce": n || ""
2161
2147
  },
2162
2148
  method: "POST"
2163
2149
  });
2164
- } catch (u) {
2165
- throw console.log("Failed to fetch Jetpack token:", u), new Error(t(u));
2150
+ } catch (g) {
2151
+ throw v("Failed to fetch Jetpack token: %O", g), new Error(t(g));
2166
2152
  }
2167
2153
  if (!(o != null && o.token))
2168
2154
  throw new Error(
2169
2155
  "Authentication failed. Please ensure Jetpack is properly connected and try again."
2170
2156
  );
2171
- const c = {
2157
+ const l = {
2172
2158
  token: o.token,
2173
2159
  blogId: o.blog_id || "",
2174
- expire: Date.now() + we
2160
+ expire: Date.now() + ye
2175
2161
  };
2176
2162
  try {
2177
- localStorage.setItem(ht, JSON.stringify(c));
2178
- } catch (u) {
2179
- console.log("Error storing token in localStorage:", u);
2163
+ localStorage.setItem(pt, JSON.stringify(l));
2164
+ } catch (g) {
2165
+ v("Error storing token in localStorage: %O", g);
2180
2166
  }
2181
- return c;
2167
+ return l;
2182
2168
  }
2183
2169
  const Oe = (t) => async () => {
2184
2170
  const e = {};
2185
2171
  try {
2186
- const s = await Te(t);
2172
+ const s = await Ie(t);
2187
2173
  s != null && s.token && (e.Authorization = `${s.token}`);
2188
2174
  } catch (s) {
2189
- throw console.error("Failed to get Jetpack token for auth:", s), s;
2175
+ throw v("Failed to get Jetpack token for auth: %O", s), s;
2190
2176
  }
2191
2177
  return e;
2192
2178
  };
2193
- function Ce(t) {
2179
+ function Te(t) {
2194
2180
  const e = {
2195
2181
  type: "object",
2196
2182
  properties: {}
@@ -2206,21 +2192,21 @@ function Ce(t) {
2206
2192
  };
2207
2193
  }
2208
2194
  function _e(t) {
2209
- return t.map(Ce);
2195
+ return t.map(Te);
2210
2196
  }
2211
2197
  function Pe(t) {
2212
2198
  return (t == null ? void 0 : t._source) === "wordpress-ability";
2213
2199
  }
2214
2200
  export {
2215
- ye as A2AErrorCodes,
2201
+ he as A2AErrorCodes,
2216
2202
  _e as convertAbilitiesToTools,
2217
- Ce as convertAbilityToTool,
2203
+ Te as convertAbilityToTool,
2218
2204
  Re as createAbortController,
2219
- te as createClient,
2205
+ Zt as createClient,
2220
2206
  Oe as createJetpackAuthProvider,
2221
- Pt as createRequestId,
2222
- Nt as createTaskId,
2223
- at as createTextMessage,
2207
+ _t as createRequestId,
2208
+ Pt as createTaskId,
2209
+ nt as createTextMessage,
2224
2210
  D as extractTextFromMessage,
2225
2211
  F as extractToolCallsFromMessage,
2226
2212
  st as getAgentManager,
@@ -2230,5 +2216,5 @@ export {
2230
2216
  Ae as useClientContext,
2231
2217
  Se as useClientTools,
2232
2218
  ke as useClientToolsWithAbilities,
2233
- ge as useMessageActions
2219
+ de as useMessageActions
2234
2220
  };