@automattic/agenttic-client 0.1.39 → 0.1.41

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,26 +1,26 @@
1
1
  var $e = Object.defineProperty;
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);
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
4
  import qe, { useMemo as Se, useCallback as F, useState as ve, useRef as He, useEffect as se } from "react";
5
5
  import { l as M, f as Me, a as K } from "./logger-aKHbTlwk.js";
6
6
  import pe from "@wordpress/api-fetch";
7
- function qt(e) {
7
+ function Ht(e) {
8
8
  return Se(() => e ? {
9
9
  getClientContext: () => {
10
10
  try {
11
11
  return e() || {};
12
- } catch (a) {
13
- return M("Error getting client context: %O", a), {};
12
+ } catch (n) {
13
+ return M("Error getting client context: %O", n), {};
14
14
  }
15
15
  }
16
16
  } : void 0, [e]);
17
17
  }
18
18
  function ce({
19
19
  getClientTools: e,
20
- executeTool: s,
21
- getAbilities: t
20
+ executeTool: t,
21
+ getAbilities: s
22
22
  }) {
23
- const a = F(async () => {
23
+ const n = F(async () => {
24
24
  const o = [];
25
25
  if (e)
26
26
  try {
@@ -30,67 +30,67 @@ function ce({
30
30
  M("Error getting available tools: %O", c);
31
31
  }
32
32
  return o;
33
- }, [e]), n = F(
33
+ }, [e]), a = F(
34
34
  async (o, c) => {
35
- if (!s)
35
+ if (!t)
36
36
  throw new Error("No executeTool callback provided");
37
37
  try {
38
- return await s(o, c);
38
+ return await t(o, c);
39
39
  } catch (l) {
40
40
  throw M("Error executing tool %s: %O", o, l), l;
41
41
  }
42
42
  },
43
- [s]
43
+ [t]
44
44
  );
45
45
  return Se(() => {
46
- if (!e && !t)
46
+ if (!e && !s)
47
47
  return;
48
48
  const o = {};
49
- return e && (o.getAvailableTools = a), s && (o.executeTool = n), t && (o.getAbilities = t), o;
49
+ return e && (o.getAvailableTools = n), t && (o.executeTool = a), s && (o.getAbilities = s), o;
50
50
  }, [
51
- a,
52
51
  n,
52
+ a,
53
53
  e,
54
- s,
55
- t
54
+ t,
55
+ s
56
56
  ]);
57
57
  }
58
- function Ht(e, s) {
58
+ function Ft(e, t) {
59
59
  return ce({
60
60
  getClientTools: e,
61
- executeTool: s,
61
+ executeTool: t,
62
62
  getAbilities: void 0
63
63
  });
64
64
  }
65
- function Ft(e) {
65
+ function jt(e) {
66
66
  return ce({
67
67
  getClientTools: void 0,
68
68
  executeTool: void 0,
69
69
  getAbilities: e
70
70
  });
71
71
  }
72
- function jt(e) {
73
- const { getClientTools: s, executeTool: t, getAbilities: a } = e;
74
- if (!s && !a)
72
+ function Dt(e) {
73
+ const { getClientTools: t, executeTool: s, getAbilities: n } = e;
74
+ if (!t && !n)
75
75
  throw new Error(
76
76
  "At least one of getClientTools or getAbilities must be provided to useClientToolsWithAbilities."
77
77
  );
78
- if (s && !t)
78
+ if (t && !s)
79
79
  throw new Error(
80
80
  "executeTool is required when providing getClientTools."
81
81
  );
82
82
  return ce({
83
- getClientTools: s,
84
- executeTool: t,
85
- getAbilities: a
83
+ getClientTools: t,
84
+ executeTool: s,
85
+ getAbilities: n
86
86
  });
87
87
  }
88
88
  function ue() {
89
89
  const e = "abcdefghijklmnopqrstuvwxyz0123456789";
90
- let s = "";
91
- for (let t = 0; t < 8; t++)
92
- s += e.charAt(Math.floor(Math.random() * e.length));
93
- return s;
90
+ let t = "";
91
+ for (let s = 0; s < 8; s++)
92
+ t += e.charAt(Math.floor(Math.random() * e.length));
93
+ return t;
94
94
  }
95
95
  function N() {
96
96
  return ue();
@@ -101,33 +101,33 @@ function Fe() {
101
101
  function je() {
102
102
  return `task-${ue()}`;
103
103
  }
104
- function xe(e, s) {
104
+ function xe(e, t) {
105
105
  return {
106
106
  type: "text",
107
107
  text: e,
108
- ...s && { metadata: s }
108
+ ...t && { metadata: t }
109
109
  };
110
110
  }
111
- function De(e, s = "message/send", t = !1) {
112
- const a = {
111
+ function De(e, t = "message/send", s = !1) {
112
+ const n = {
113
113
  jsonrpc: "2.0",
114
114
  id: Fe(),
115
- method: s,
115
+ method: t,
116
116
  params: {
117
117
  id: e.id || je(),
118
118
  ...e
119
119
  }
120
120
  };
121
- return t && s === "message/stream" && (a.tokenStreaming = !0), a;
121
+ return s && t === "message/stream" && (n.tokenStreaming = !0), n;
122
122
  }
123
123
  function D(e) {
124
- return !e || !e.parts || !Array.isArray(e.parts) ? "" : e.parts.filter((s) => s.type === "text").map((s) => s.text).join(" ");
124
+ return !e || !e.parts || !Array.isArray(e.parts) ? "" : e.parts.filter((t) => t.type === "text").map((t) => t.text).join(" ");
125
125
  }
126
126
  function he(e) {
127
127
  if (!e || !e.parts || !Array.isArray(e.parts))
128
128
  return;
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
- return s ? s.data.summary : void 0;
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
+ return t ? t.data.summary : void 0;
131
131
  }
132
132
  function Je(e) {
133
133
  return {
@@ -158,18 +158,18 @@ function Ke(e) {
158
158
  }
159
159
  function L(e) {
160
160
  return !e || !e.parts || !Array.isArray(e.parts) ? [] : e.parts.filter(
161
- (s) => s.type === "data" && "toolCallId" in s.data && "toolId" in s.data && "arguments" in s.data
161
+ (t) => t.type === "data" && "toolCallId" in t.data && "toolId" in t.data && "arguments" in t.data
162
162
  );
163
163
  }
164
- function W(e, s, t, a) {
164
+ function W(e, t, s, n) {
165
165
  return {
166
166
  type: "data",
167
167
  data: {
168
168
  toolCallId: e,
169
- toolId: s,
170
- result: t
169
+ toolId: t,
170
+ result: s
171
171
  },
172
- metadata: a ? { error: a } : void 0
172
+ metadata: n ? { error: n } : void 0
173
173
  };
174
174
  }
175
175
  function Le(e) {
@@ -181,16 +181,16 @@ function Le(e) {
181
181
  metadata: {}
182
182
  };
183
183
  }
184
- function ie(e, s) {
185
- const { contentType: t, ...a } = s || {};
184
+ function ie(e, t) {
185
+ const { contentType: s, ...n } = t || {};
186
186
  return {
187
187
  role: "user",
188
- parts: [xe(e, t ? { contentType: t } : void 0)],
188
+ parts: [xe(e, s ? { contentType: s } : void 0)],
189
189
  kind: "message",
190
190
  messageId: N(),
191
191
  metadata: {
192
192
  timestamp: Date.now(),
193
- ...a
193
+ ...n
194
194
  }
195
195
  };
196
196
  }
@@ -217,124 +217,124 @@ function de(e) {
217
217
  returnToAgent: !0
218
218
  };
219
219
  }
220
- function X(e, s = []) {
220
+ function X(e, t = []) {
221
221
  return {
222
222
  role: "user",
223
223
  kind: "message",
224
- parts: [...s, ...e],
224
+ parts: [...t, ...e],
225
225
  messageId: N(),
226
226
  metadata: {
227
227
  timestamp: Date.now()
228
228
  }
229
229
  };
230
230
  }
231
- function Dt() {
231
+ function Jt() {
232
232
  return new AbortController();
233
233
  }
234
- async function Be(e, s) {
235
- if (!s)
234
+ async function Be(e, t) {
235
+ if (!t)
236
236
  return e;
237
237
  try {
238
- const t = [];
239
- if (s.getAvailableTools) {
240
- const a = await s.getAvailableTools();
241
- if (a.length > 0) {
242
- const n = a.map(Je);
243
- t.push(...n);
238
+ const s = [];
239
+ if (t.getAvailableTools) {
240
+ const n = await t.getAvailableTools();
241
+ if (n.length > 0) {
242
+ const a = n.map(Je);
243
+ s.push(...a);
244
244
  }
245
245
  }
246
- if (s.getAbilities) {
247
- const a = await s.getAbilities();
248
- if (a.length > 0) {
249
- const n = a.map(Ke);
250
- t.push(...n);
246
+ if (t.getAbilities) {
247
+ const n = await t.getAbilities();
248
+ if (n.length > 0) {
249
+ const a = n.map(Ke);
250
+ s.push(...a);
251
251
  }
252
252
  }
253
- return t.length === 0 ? e : {
253
+ return s.length === 0 ? e : {
254
254
  ...e,
255
- parts: [...e.parts, ...t]
255
+ parts: [...e.parts, ...s]
256
256
  };
257
- } catch (t) {
258
- return M("Warning: Failed to get tools: %s", t), e;
257
+ } catch (s) {
258
+ return M("Warning: Failed to get tools: %s", s), e;
259
259
  }
260
260
  }
261
- function We(e, s) {
262
- if (!s)
261
+ function We(e, t) {
262
+ if (!t)
263
263
  return e;
264
264
  try {
265
- const t = s.getClientContext();
266
- if (!t || Object.keys(t).length === 0)
265
+ const s = t.getClientContext();
266
+ if (!s || Object.keys(s).length === 0)
267
267
  return e;
268
- const a = Le(t);
268
+ const n = Le(s);
269
269
  return {
270
270
  ...e,
271
- parts: [...e.parts, a]
271
+ parts: [...e.parts, n]
272
272
  };
273
- } catch (t) {
274
- return M("Warning: Failed to get context: %s", t), e;
273
+ } catch (s) {
274
+ return M("Warning: Failed to get context: %s", s), e;
275
275
  }
276
276
  }
277
- async function Ve(e, s, t) {
278
- let a = await Be(
277
+ async function Ve(e, t, s) {
278
+ let n = await Be(
279
279
  e,
280
- s
281
- );
282
- a = We(
283
- a,
284
280
  t
285
281
  );
286
- const { metadata: n, ...r } = a;
282
+ n = We(
283
+ n,
284
+ s
285
+ );
286
+ const { metadata: a, ...r } = n;
287
287
  return r;
288
288
  }
289
- function ze(e, s = "") {
290
- const t = [], a = s + e;
291
- let n = "", r = 0, o = 0;
292
- for (; o < a.length; ) {
293
- const l = a.indexOf(
289
+ function ze(e, t = "") {
290
+ const s = [], n = t + e;
291
+ let a = "", r = 0, o = 0;
292
+ for (; o < n.length; ) {
293
+ const l = n.indexOf(
294
294
  `
295
295
  `,
296
296
  o
297
- ), d = l === -1 ? a.substring(o) : a.substring(o, l);
298
- if (d.startsWith("data:"))
299
- n !== "" && (n += `
300
- `), n += d.substring(
301
- d.startsWith("data: ") ? 6 : 5
297
+ ), u = l === -1 ? n.substring(o) : n.substring(o, l);
298
+ if (u.startsWith("data:"))
299
+ a !== "" && (a += `
300
+ `), a += u.substring(
301
+ u.startsWith("data: ") ? 6 : 5
302
302
  );
303
- else if (d.trim() === "" && n) {
303
+ else if (u.trim() === "" && a) {
304
304
  try {
305
- t.push(JSON.parse(n)), r = l === -1 ? a.length : l + 1;
306
- } catch (u) {
307
- M("Failed to parse SSE event: %o", u), M("Problematic payload: %s", n);
305
+ s.push(JSON.parse(a)), r = l === -1 ? n.length : l + 1;
306
+ } catch (d) {
307
+ M("Failed to parse SSE event: %o", d), M("Problematic payload: %s", a);
308
308
  }
309
- n = "";
309
+ a = "";
310
310
  }
311
- l === -1 ? o = a.length : o = l + 1;
311
+ l === -1 ? o = n.length : o = l + 1;
312
312
  }
313
- const c = a.substring(r);
314
- return { events: t, nextBuffer: c };
313
+ const c = n.substring(r);
314
+ return { events: s, nextBuffer: c };
315
315
  }
316
- async function* Ge(e, s = {}) {
317
- var d, u, h;
318
- const { supportDeltas: t = !1 } = s, a = e.getReader(), n = new TextDecoder();
316
+ async function* Ge(e, t = {}) {
317
+ var u, d, p;
318
+ const { supportDeltas: s = !1 } = t, n = e.getReader(), a = new TextDecoder();
319
319
  let r = "";
320
320
  const o = new Qe();
321
321
  let c = null, l = null;
322
322
  try {
323
323
  for (; ; ) {
324
- const { done: p, value: g } = await a.read();
325
- if (p)
324
+ const { done: m, value: f } = await n.read();
325
+ if (m)
326
326
  break;
327
- const y = n.decode(g, { stream: !0 }), { events: I, nextBuffer: A } = ze(y, r);
328
- if (I && Array.isArray(I))
329
- for (let f = 0; f < I.length; f++) {
330
- const i = I[f];
331
- if (f > 0 && i.method === "message/delta" && typeof requestAnimationFrame < "u" && await new Promise((T) => {
327
+ const I = a.decode(f, { stream: !0 }), { events: h, nextBuffer: A } = ze(I, r);
328
+ if (h && Array.isArray(h))
329
+ for (let g = 0; g < h.length; g++) {
330
+ const i = h[g];
331
+ if (g > 0 && i.method === "message/delta" && typeof requestAnimationFrame < "u" && await new Promise((T) => {
332
332
  requestAnimationFrame(() => T(void 0));
333
333
  }), i.error)
334
334
  throw new Error(
335
335
  `Streaming error: ${i.error.message}`
336
336
  );
337
- if (t && i.method === "message/delta" && ((d = i.params) != null && d.delta)) {
337
+ if (s && i.method === "message/delta" && ((u = i.params) != null && u.delta)) {
338
338
  const T = i.params.delta;
339
339
  try {
340
340
  if (T.deltaType === "content" && (o.processContentDelta(
@@ -356,7 +356,7 @@ async function* Ge(e, s = {}) {
356
356
  }
357
357
  } else if (i.result && i.result.status) {
358
358
  c = i.result.id, l = i.result.status, (o.getTextContent() || o.getCurrentMessage().parts.length > 0) && o.reset();
359
- const T = ((u = i.result.status) == null ? void 0 : u.message) || {
359
+ const T = ((d = i.result.status) == null ? void 0 : d.message) || {
360
360
  role: "agent",
361
361
  parts: []
362
362
  };
@@ -369,7 +369,7 @@ async function* Ge(e, s = {}) {
369
369
  progressMessage: he(T)
370
370
  };
371
371
  } else if (i.id && i.result && (c = i.result.id, i.result.status)) {
372
- const T = ((h = i.result.status) == null ? void 0 : h.message) || {
372
+ const T = ((p = i.result.status) == null ? void 0 : p.message) || {
373
373
  role: "agent",
374
374
  parts: []
375
375
  };
@@ -386,7 +386,7 @@ async function* Ge(e, s = {}) {
386
386
  r = A;
387
387
  }
388
388
  } finally {
389
- a.releaseLock();
389
+ n.releaseLock();
390
390
  }
391
391
  }
392
392
  class Qe {
@@ -398,33 +398,33 @@ class Qe {
398
398
  * Process a simple content delta (server's actual format)
399
399
  * @param content - The text content to append
400
400
  */
401
- processContentDelta(s) {
402
- this.textContent += s;
401
+ processContentDelta(t) {
402
+ this.textContent += t;
403
403
  }
404
404
  /**
405
405
  * Process a delta message and accumulate the content (original format)
406
406
  * @param delta - The delta message to process
407
407
  */
408
- processDelta(s) {
409
- switch (s.type) {
408
+ processDelta(t) {
409
+ switch (t.type) {
410
410
  case "content":
411
- this.textContent += s.content;
411
+ this.textContent += t.content;
412
412
  break;
413
413
  case "tool_name":
414
- this.toolCalls.has(s.toolCallIndex) || this.toolCalls.set(s.toolCallIndex, {
415
- toolCallId: s.toolCallId,
414
+ this.toolCalls.has(t.toolCallIndex) || this.toolCalls.set(t.toolCallIndex, {
415
+ toolCallId: t.toolCallId,
416
416
  toolName: "",
417
417
  argumentFragments: []
418
418
  });
419
- const t = this.toolCalls.get(s.toolCallIndex);
420
- t.toolName += s.content;
419
+ const s = this.toolCalls.get(t.toolCallIndex);
420
+ s.toolName += t.content;
421
421
  break;
422
422
  case "tool_argument":
423
- this.toolCalls.has(s.toolCallIndex) || this.toolCalls.set(s.toolCallIndex, {
424
- toolCallId: s.toolCallId,
423
+ this.toolCalls.has(t.toolCallIndex) || this.toolCalls.set(t.toolCallIndex, {
424
+ toolCallId: t.toolCallId,
425
425
  toolName: "",
426
426
  argumentFragments: []
427
- }), this.toolCalls.get(s.toolCallIndex).argumentFragments.push(s.content);
427
+ }), this.toolCalls.get(t.toolCallIndex).argumentFragments.push(t.content);
428
428
  break;
429
429
  }
430
430
  }
@@ -438,15 +438,15 @@ class Qe {
438
438
  * Get the current accumulated message
439
439
  * @param role - The role for the message (default: 'agent')
440
440
  */
441
- getCurrentMessage(s = "agent") {
442
- const t = [];
443
- this.textContent && t.push({
441
+ getCurrentMessage(t = "agent") {
442
+ const s = [];
443
+ this.textContent && s.push({
444
444
  type: "text",
445
445
  text: this.textContent
446
446
  });
447
- for (const [a, n] of this.toolCalls)
448
- if (n.toolName) {
449
- const r = n.argumentFragments.join("");
447
+ for (const [n, a] of this.toolCalls)
448
+ if (a.toolName) {
449
+ const r = a.argumentFragments.join("");
450
450
  let o = {};
451
451
  if (r)
452
452
  try {
@@ -454,18 +454,18 @@ class Qe {
454
454
  } catch {
455
455
  o = { _raw: r };
456
456
  }
457
- t.push({
457
+ s.push({
458
458
  type: "data",
459
459
  data: {
460
- toolCallId: n.toolCallId,
461
- toolId: n.toolName,
460
+ toolCallId: a.toolCallId,
461
+ toolId: a.toolName,
462
462
  arguments: o
463
463
  }
464
464
  });
465
465
  }
466
466
  return {
467
- role: s,
468
- parts: t,
467
+ role: t,
468
+ parts: s,
469
469
  kind: "message",
470
470
  messageId: N()
471
471
  };
@@ -477,164 +477,164 @@ class Qe {
477
477
  this.textContent = "", this.toolCalls.clear();
478
478
  }
479
479
  }
480
- function Ae(e, s, t = "request") {
481
- 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;
480
+ function Ae(e, t, s = "request") {
481
+ throw clearTimeout(t), M("%s failed with error: %O", s, e), e instanceof Error && (M("Error message: %s", e.message), M("Error stack: %s", e.stack)), e;
482
482
  }
483
- function Ce(e, s = "request") {
483
+ function Ce(e, t = "request") {
484
484
  if (!e.ok)
485
485
  throw new Error(`HTTP error! status: ${e.status}`);
486
486
  }
487
- function Xe(e, s = "request") {
487
+ function Xe(e, t = "request") {
488
488
  if (e.error)
489
489
  throw new Error(
490
- `Protocol ${s} error: ${e.error.message}`
490
+ `Protocol ${t} error: ${e.error.message}`
491
491
  );
492
492
  if (!e.result)
493
- throw new Error(`No result in ${s} response`);
493
+ throw new Error(`No result in ${t} response`);
494
494
  return e.result;
495
495
  }
496
- function Ye(e, s = "streaming request") {
497
- if (Ce(e, s), !e.body)
498
- throw new Error(`No response body for ${s}`);
496
+ function Ye(e, t = "streaming request") {
497
+ if (Ce(e, t), !e.body)
498
+ throw new Error(`No response body for ${t}`);
499
499
  }
500
- function be(e, s = "request") {
501
- const t = new AbortController();
500
+ function be(e, t = "request") {
501
+ const s = new AbortController();
502
502
  return { timeoutId: setTimeout(
503
- () => t.abort(),
503
+ () => s.abort(),
504
504
  e
505
- ), controller: t };
505
+ ), controller: s };
506
506
  }
507
- function Ze(e, s) {
508
- return `${e}/${s}`;
507
+ function Ze(e, t) {
508
+ return `${e}/${t}`;
509
509
  }
510
- function et(e, s, t, a) {
511
- M("Request: %s %s", e, s), M("Headers: %o", t), a && M("Body: %s", Me(a));
510
+ function et(e, t, s, n) {
511
+ M("Request: %s %s", e, t), M("Headers: %o", s), n && M("Body: %s", Me(n));
512
512
  }
513
- async function tt(e, s = !1) {
514
- const t = {
513
+ async function tt(e, t = !1) {
514
+ const s = {
515
515
  "Content-Type": "application/json"
516
516
  };
517
- if (s && (t.Accept = "text/event-stream"), e) {
518
- const a = await e();
519
- return { ...t, ...a };
517
+ if (t && (s.Accept = "text/event-stream"), e) {
518
+ const n = await e();
519
+ return { ...s, ...n };
520
520
  }
521
- return t;
521
+ return s;
522
522
  }
523
- function ke(e, s) {
524
- if (!s)
523
+ function ke(e, t) {
524
+ if (!t)
525
525
  return e;
526
- const t = new AbortController(), a = (n) => {
527
- t.signal.aborted || t.abort(n.reason);
526
+ const s = new AbortController(), n = (a) => {
527
+ s.signal.aborted || s.abort(a.reason);
528
528
  };
529
- return e.aborted ? t.abort(e.reason) : e.addEventListener("abort", () => a(e), {
529
+ return e.aborted ? s.abort(e.reason) : e.addEventListener("abort", () => n(e), {
530
530
  once: !0
531
- }), s.aborted ? t.abort(s.reason) : s.addEventListener("abort", () => a(s), {
531
+ }), t.aborted ? s.abort(t.reason) : t.addEventListener("abort", () => n(t), {
532
532
  once: !0
533
- }), t.signal;
533
+ }), s.signal;
534
534
  }
535
- function Re(e, s, t) {
535
+ function Re(e, t, s) {
536
536
  return {
537
537
  method: "POST",
538
538
  headers: e,
539
- body: s,
540
- signal: t
539
+ body: t,
540
+ signal: s
541
541
  };
542
542
  }
543
- async function Y(e, s, t, a, n, r) {
544
- const { message: o, sessionId: c, taskId: l, metadata: d } = e, { agentId: u, agentUrl: h, authProvider: p, proxy: g } = s, { isStreaming: y = !1, enableTokenStreaming: I = !1 } = t, A = c || r, f = Ze(h, u), i = await Ve(
543
+ async function Y(e, t, s, n, a, r) {
544
+ const { message: o, sessionId: c, taskId: l, metadata: u } = e, { agentId: d, agentUrl: p, authProvider: m, proxy: f } = t, { isStreaming: I = !1, enableTokenStreaming: h = !1 } = s, A = c || r, g = Ze(p, d), i = await Ve(
545
545
  o,
546
- a,
547
- n
546
+ n,
547
+ a
548
548
  ), T = {
549
549
  id: l,
550
550
  message: i,
551
- metadata: d
551
+ metadata: u
552
552
  };
553
553
  A && (T.sessionId = A);
554
554
  const x = De(
555
555
  T,
556
- y ? "message/stream" : "message/send",
557
- I && y
556
+ I ? "message/stream" : "message/send",
557
+ h && I
558
558
  // Only enable token streaming if using SSE
559
- ), w = await tt(p, y);
560
- return et("POST", f, w, x), {
559
+ ), w = await tt(m, I);
560
+ return et("POST", g, w, x), {
561
561
  request: x,
562
562
  headers: w,
563
563
  enhancedMessage: i,
564
564
  effectiveSessionId: A,
565
- fullAgentUrl: f
565
+ fullAgentUrl: g
566
566
  };
567
567
  }
568
- async function Ee(e, s, t = {}) {
569
- const { request: a, headers: n, fullAgentUrl: r } = e, { timeout: o } = s, { abortSignal: c } = t, { timeoutId: l, controller: d } = be(
568
+ async function Ee(e, t, s = {}) {
569
+ const { request: n, headers: a, fullAgentUrl: r } = e, { timeout: o } = t, { abortSignal: c } = s, { timeoutId: l, controller: u } = be(
570
570
  o,
571
571
  "request"
572
- ), u = c ? ke(d.signal, c) : d.signal;
572
+ ), d = c ? ke(u.signal, c) : u.signal;
573
573
  try {
574
- const h = Re(
575
- n,
576
- JSON.stringify(a),
577
- u
574
+ const p = Re(
575
+ a,
576
+ JSON.stringify(n),
577
+ d
578
578
  );
579
579
  M("Making request to %s with options: %O", r, {
580
- method: h.method,
581
- headers: h.headers
580
+ method: p.method,
581
+ headers: p.headers
582
582
  });
583
- const p = await fetch(r, h);
584
- clearTimeout(l), Ce(p, "request");
585
- const g = await p.json();
583
+ const m = await fetch(r, p);
584
+ clearTimeout(l), Ce(m, "request");
585
+ const f = await m.json();
586
586
  return M(
587
587
  "Response from %s: %d %O",
588
588
  r,
589
- p.status,
590
- Me(g)
591
- ), Xe(g, "request");
592
- } catch (h) {
593
- Ae(h, l, "request");
589
+ m.status,
590
+ Me(f)
591
+ ), Xe(f, "request");
592
+ } catch (p) {
593
+ Ae(p, l, "request");
594
594
  }
595
595
  }
596
- async function* Pe(e, s, t) {
597
- const { request: a, headers: n, fullAgentUrl: r } = e, {
596
+ async function* Pe(e, t, s) {
597
+ const { request: n, headers: a, fullAgentUrl: r } = e, {
598
598
  streamingTimeout: o = 6e4,
599
599
  abortSignal: c,
600
600
  enableTokenStreaming: l = !1
601
- } = t, { timeoutId: d, controller: u } = be(
601
+ } = s, { timeoutId: u, controller: d } = be(
602
602
  o,
603
603
  "streaming request"
604
- ), h = c ? ke(u.signal, c) : u.signal;
604
+ ), p = c ? ke(d.signal, c) : d.signal;
605
605
  try {
606
- const p = JSON.stringify(a), g = Re(n, p, h), y = await fetch(r, g);
607
- if (clearTimeout(d), Ye(y, "streaming request"), !y.body)
606
+ const m = JSON.stringify(n), f = Re(a, m, p), I = await fetch(r, f);
607
+ if (clearTimeout(u), Ye(I, "streaming request"), !I.body)
608
608
  throw new Error(
609
609
  "Response body is null - server may not support streaming"
610
610
  );
611
- const I = l && a.tokenStreaming === !0;
612
- yield* Ge(y.body, {
613
- supportDeltas: I
611
+ const h = l && n.tokenStreaming === !0;
612
+ yield* Ge(I.body, {
613
+ supportDeltas: h
614
614
  });
615
- } catch (p) {
616
- Ae(p, d, "streaming request");
615
+ } catch (m) {
616
+ Ae(m, u, "streaming request");
617
617
  }
618
618
  }
619
619
  const st = 12e4;
620
- async function Z(e, s, t, a, n) {
620
+ async function Z(e, t, s, n, a) {
621
621
  if (e.getAbilities) {
622
622
  const r = await e.getAbilities();
623
623
  if (r.length > 0)
624
624
  for (const o of r) {
625
625
  const c = o.name.replace(/\//g, "__").replace(/-/g, "_");
626
- if (s === c || s === o.name) {
626
+ if (t === c || t === o.name) {
627
627
  if (o.callback)
628
628
  try {
629
629
  const l = {
630
- ...t,
631
- ...a && { messageId: a }
632
- }, d = await o.callback(l);
630
+ ...s,
631
+ ...n && { messageId: n }
632
+ }, u = await o.callback(l);
633
633
  return {
634
- result: d,
635
- returnToAgent: (d == null ? void 0 : d.returnToAgent) !== void 0 ? d.returnToAgent : !0,
636
- ...(d == null ? void 0 : d.agentMessage) && {
637
- agentMessage: d.agentMessage
634
+ result: u,
635
+ returnToAgent: (u == null ? void 0 : u.returnToAgent) !== void 0 ? u.returnToAgent : !0,
636
+ ...(u == null ? void 0 : u.agentMessage) && {
637
+ agentMessage: u.agentMessage
638
638
  }
639
639
  };
640
640
  } catch (l) {
@@ -654,7 +654,7 @@ async function Z(e, s, t, a, n) {
654
654
  try {
655
655
  return { result: await e.executeAbility(
656
656
  o.name,
657
- t
657
+ s
658
658
  ), returnToAgent: !0 };
659
659
  } catch (l) {
660
660
  return M(
@@ -677,27 +677,27 @@ async function Z(e, s, t, a, n) {
677
677
  }
678
678
  if (e.executeTool)
679
679
  return await e.executeTool(
680
- s,
681
680
  t,
682
- a,
683
- n
681
+ s,
682
+ n,
683
+ a
684
684
  );
685
685
  throw new Error(
686
- `No handler found for tool: ${s}. Tool provider must implement executeTool for non-ability tools.`
686
+ `No handler found for tool: ${t}. Tool provider must implement executeTool for non-ability tools.`
687
687
  );
688
688
  }
689
689
  const ge = /* @__PURE__ */ new Map();
690
- async function at(e, s) {
691
- if (!e || !s || !e.getAvailableTools)
690
+ async function nt(e, t) {
691
+ if (!e || !t || !e.getAvailableTools)
692
692
  return !1;
693
- const t = L(s);
694
- if (t.length === 0)
693
+ const s = L(t);
694
+ if (s.length === 0)
695
695
  return !1;
696
696
  try {
697
- const a = await e.getAvailableTools();
698
- for (const n of t)
699
- if (a.some(
700
- (o) => o.id === n.data.toolId
697
+ const n = await e.getAvailableTools();
698
+ for (const a of s)
699
+ if (n.some(
700
+ (o) => o.id === a.data.toolId
701
701
  ))
702
702
  return !0;
703
703
  } catch {
@@ -705,143 +705,143 @@ async function at(e, s) {
705
705
  }
706
706
  return !1;
707
707
  }
708
- function nt() {
708
+ function at() {
709
709
  ge.clear();
710
710
  }
711
711
  function ot(e) {
712
- return e.map((s) => {
713
- const t = s.data.toolCallId, a = ge.get(t);
714
- if (a && a.resolvedValue !== null) {
715
- const n = a.resolvedValue;
716
- return n.error ? W(
717
- t,
718
- s.data.toolId,
712
+ return e.map((t) => {
713
+ const s = t.data.toolCallId, n = ge.get(s);
714
+ if (n && n.resolvedValue !== null) {
715
+ const a = n.resolvedValue;
716
+ return a.error ? W(
717
+ s,
718
+ t.data.toolId,
719
719
  void 0,
720
- n.error
720
+ a.error
721
721
  ) : W(
722
- t,
723
- s.data.toolId,
724
- n
722
+ s,
723
+ t.data.toolId,
724
+ a
725
725
  );
726
726
  }
727
- return s;
727
+ return t;
728
728
  });
729
729
  }
730
- async function Oe(e, s, t) {
731
- const a = [], n = [];
730
+ async function _e(e, t, s) {
731
+ const n = [], a = [];
732
732
  let r = !1;
733
733
  for (const o of e) {
734
- const { toolCallId: c, toolId: l, arguments: d } = o.data;
734
+ const { toolCallId: c, toolId: l, arguments: u } = o.data;
735
735
  try {
736
- const u = await Z(
737
- s,
738
- l,
739
- d,
736
+ const d = await Z(
740
737
  t,
738
+ l,
739
+ u,
740
+ s,
741
741
  c
742
- ), { result: h, returnToAgent: p, agentMessage: g } = de(u);
743
- p && (r = !0), g && n.push(G(g)), a.push(
742
+ ), { result: p, returnToAgent: m, agentMessage: f } = de(d);
743
+ m && (r = !0), f && a.push(G(f)), n.push(
744
744
  W(
745
745
  c,
746
746
  l,
747
- h
747
+ p
748
748
  )
749
749
  );
750
- } catch (u) {
751
- r = !0, a.push(
750
+ } catch (d) {
751
+ r = !0, n.push(
752
752
  W(
753
753
  c,
754
754
  l,
755
755
  void 0,
756
- u instanceof Error ? u.message : String(u)
756
+ d instanceof Error ? d.message : String(d)
757
757
  )
758
758
  );
759
759
  }
760
760
  }
761
- return { results: a, shouldReturnToAgent: r, agentMessages: n };
761
+ return { results: n, shouldReturnToAgent: r, agentMessages: a };
762
762
  }
763
763
  function ye(e) {
764
- const s = [];
765
- for (const t of e)
766
- for (const a of t.parts)
767
- a.type === "text" ? s.push({
764
+ const t = [];
765
+ for (const s of e)
766
+ for (const n of s.parts)
767
+ n.type === "text" ? t.push({
768
768
  type: "data",
769
769
  data: {
770
- role: t.role,
771
- text: a.text
770
+ role: s.role,
771
+ text: n.text
772
772
  }
773
- }) : a.type === "data" && s.push(a);
774
- return s;
773
+ }) : n.type === "data" && t.push(n);
774
+ return t;
775
775
  }
776
- async function ae(e, s, t, a, n, r, o) {
776
+ async function ne(e, t, s, n, a, r, o) {
777
777
  const l = await Y(
778
778
  {
779
- message: s,
779
+ message: t,
780
780
  taskId: e,
781
781
  sessionId: void 0
782
782
  // Use task's session
783
783
  },
784
- t,
784
+ s,
785
785
  { isStreaming: !1 },
786
- a,
787
786
  n,
787
+ a,
788
788
  r
789
789
  );
790
- return await Ee(l, t, {
790
+ return await Ee(l, s, {
791
791
  abortSignal: o
792
792
  });
793
793
  }
794
- async function Ie(e, s, t, a, n, r, o, c, l = []) {
795
- const d = {
796
- message: s,
794
+ async function Ie(e, t, s, n, a, r, o, c, l = []) {
795
+ const u = {
796
+ message: t,
797
797
  taskId: e,
798
798
  sessionId: r
799
799
  // Use the provided sessionId to maintain conversation continuity
800
- }, u = c || { isStreaming: !0 }, h = await Y(
801
- d,
802
- t,
800
+ }, d = c || { isStreaming: !0 }, p = await Y(
801
+ u,
802
+ s,
803
803
  {
804
- ...u
804
+ ...d
805
805
  },
806
- a,
807
806
  n,
807
+ a,
808
808
  r
809
- ), p = Pe(h, t, {
810
- ...u,
809
+ ), m = Pe(p, s, {
810
+ ...d,
811
811
  abortSignal: o
812
812
  });
813
- return _e(
814
- p,
815
- a,
813
+ return Oe(
814
+ m,
816
815
  n,
817
- t,
816
+ a,
817
+ s,
818
818
  r,
819
819
  !0,
820
820
  // withHistory
821
821
  l,
822
822
  // preserve conversation parts across continuation
823
823
  o,
824
- u
824
+ d
825
825
  // Pass through the same request options
826
826
  );
827
827
  }
828
- async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
829
- var d, u, h, p, g, y, I, A, f, i, T, x;
828
+ async function* Oe(e, t, s, n, a, r = !0, o = [], c, l) {
829
+ var u, d, p, m, f, I, h, A, g, i, T, x;
830
830
  for await (const w of e) {
831
- if (w.sessionId && !n && (n = w.sessionId), yield w, w.status.state === "running" && w.status.message && s && await at(
832
- s,
831
+ if (w.sessionId && !a && (a = w.sessionId), yield w, w.status.state === "running" && w.status.message && t && await nt(
832
+ t,
833
833
  w.status.message
834
834
  )) {
835
835
  const P = L(
836
836
  w.status.message
837
837
  );
838
- for (const _ of P) {
839
- const { toolCallId: $, toolId: S, arguments: m } = _.data;
838
+ for (const O of P) {
839
+ const { toolCallId: $, toolId: S, arguments: y } = O.data;
840
840
  Z(
841
- s,
841
+ t,
842
842
  S,
843
- m,
844
- (u = (d = w.status) == null ? void 0 : d.message) == null ? void 0 : u.messageId,
843
+ y,
844
+ (d = (u = w.status) == null ? void 0 : u.message) == null ? void 0 : d.messageId,
845
845
  $
846
846
  ).catch((C) => {
847
847
  M("Tool execution failed for %s: %O", S, C);
@@ -862,14 +862,14 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
862
862
  text: ""
863
863
  };
864
864
  }
865
- if (w.status.state === "input-required" && w.status.message && s) {
865
+ if (w.status.state === "input-required" && w.status.message && t) {
866
866
  const P = L(
867
867
  w.status.message
868
868
  );
869
869
  if (P.length > 0) {
870
- const _ = [];
870
+ const O = [];
871
871
  let $ = !1;
872
- const S = [], m = [];
872
+ const S = [], y = [];
873
873
  for (const C of P) {
874
874
  const {
875
875
  toolCallId: v,
@@ -878,23 +878,23 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
878
878
  } = C.data;
879
879
  try {
880
880
  const b = await Z(
881
- s,
881
+ t,
882
882
  R,
883
883
  k,
884
- (p = (h = w.status) == null ? void 0 : h.message) == null ? void 0 : p.messageId,
884
+ (m = (p = w.status) == null ? void 0 : p.message) == null ? void 0 : m.messageId,
885
885
  v
886
886
  ), { result: E, returnToAgent: U, agentMessage: J } = de(b);
887
- if (U && ($ = !0), J && m.push(
887
+ if (U && ($ = !0), J && y.push(
888
888
  G(J)
889
889
  ), E.result instanceof Promise) {
890
- const O = E.result, j = {
891
- promise: O,
890
+ const _ = E.result, j = {
891
+ promise: _,
892
892
  resolvedValue: null
893
893
  };
894
894
  ge.set(
895
895
  v,
896
896
  j
897
- ), O.then((H) => {
897
+ ), _.then((H) => {
898
898
  j.resolvedValue = H;
899
899
  }).catch((H) => {
900
900
  M(
@@ -911,7 +911,7 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
911
911
  R,
912
912
  E
913
913
  );
914
- _.push(q), S.push(q);
914
+ O.push(q), S.push(q);
915
915
  } catch (b) {
916
916
  const E = W(
917
917
  v,
@@ -919,13 +919,13 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
919
919
  void 0,
920
920
  b instanceof Error ? b.message : String(b)
921
921
  );
922
- _.push(E), S.push(E);
922
+ O.push(E), S.push(E);
923
923
  }
924
924
  }
925
- if (o.push(w.status.message), _.length > 0 && o.push({
925
+ if (o.push(w.status.message), O.length > 0 && o.push({
926
926
  role: "agent",
927
927
  kind: "message",
928
- parts: _,
928
+ parts: O,
929
929
  messageId: N()
930
930
  }), $) {
931
931
  const C = ye(o), v = X(
@@ -945,10 +945,10 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
945
945
  const R = await Ie(
946
946
  w.id,
947
947
  v,
948
- a,
949
- s,
950
- t,
951
948
  n,
949
+ t,
950
+ s,
951
+ a,
952
952
  c,
953
953
  l,
954
954
  o
@@ -960,7 +960,7 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
960
960
  throw new Error(
961
961
  "Continue task stream ended without final result"
962
962
  );
963
- let b = (g = k.status) != null && g.message ? L(
963
+ let b = (f = k.status) != null && f.message ? L(
964
964
  k.status.message
965
965
  ) : [], E = k;
966
966
  if (b.length > 0)
@@ -968,22 +968,22 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
968
968
  ...k,
969
969
  final: !1,
970
970
  text: D(
971
- ((y = k.status) == null ? void 0 : y.message) || {
971
+ ((I = k.status) == null ? void 0 : I.message) || {
972
972
  parts: [],
973
973
  messageId: N()
974
974
  }
975
975
  )
976
976
  }; b.length > 0; ) {
977
- (I = E.status) != null && I.message && o.push(
977
+ (h = E.status) != null && h.message && o.push(
978
978
  E.status.message
979
979
  );
980
980
  const {
981
981
  results: U,
982
982
  shouldReturnToAgent: J
983
- } = await Oe(
983
+ } = await _e(
984
984
  b,
985
- s,
986
- (f = (A = E.status) == null ? void 0 : A.message) == null ? void 0 : f.messageId
985
+ t,
986
+ (g = (A = E.status) == null ? void 0 : A.message) == null ? void 0 : g.messageId
987
987
  );
988
988
  if (U.length > 0 && (yield {
989
989
  id: E.id,
@@ -1002,16 +1002,16 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
1002
1002
  }), J) {
1003
1003
  const q = r ? ye(
1004
1004
  o
1005
- ) : [], O = X(
1005
+ ) : [], _ = X(
1006
1006
  U,
1007
1007
  q
1008
1008
  ), j = await Ie(
1009
1009
  E.id,
1010
- O,
1011
- a,
1012
- s,
1013
- t,
1010
+ _,
1014
1011
  n,
1012
+ t,
1013
+ s,
1014
+ a,
1015
1015
  c,
1016
1016
  l,
1017
1017
  o
@@ -1059,12 +1059,12 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
1059
1059
  ...w.status,
1060
1060
  message: C
1061
1061
  },
1062
- final: m.length === 0,
1062
+ final: y.length === 0,
1063
1063
  // Only final if no agent messages to follow
1064
1064
  text: D(C)
1065
1065
  };
1066
- if (yield v, m.length > 0) {
1067
- const R = m.map((b) => D(b)).join(" "), k = G(R);
1066
+ if (yield v, y.length > 0) {
1067
+ const R = y.map((b) => D(b)).join(" "), k = G(R);
1068
1068
  yield {
1069
1069
  id: v.id,
1070
1070
  status: {
@@ -1082,61 +1082,61 @@ async function* _e(e, s, t, a, n, r = !0, o = [], c, l) {
1082
1082
  }
1083
1083
  function rt(e) {
1084
1084
  const {
1085
- agentId: s,
1086
- agentUrl: t,
1087
- authProvider: a,
1088
- defaultSessionId: n,
1085
+ agentId: t,
1086
+ agentUrl: s,
1087
+ authProvider: n,
1088
+ defaultSessionId: a,
1089
1089
  timeout: r = st,
1090
1090
  toolProvider: o,
1091
1091
  contextProvider: c,
1092
1092
  enableStreaming: l = !1
1093
- } = e, d = {
1094
- agentId: s,
1095
- agentUrl: t,
1096
- authProvider: a,
1093
+ } = e, u = {
1094
+ agentId: t,
1095
+ agentUrl: s,
1096
+ authProvider: n,
1097
1097
  timeout: r
1098
1098
  };
1099
1099
  return {
1100
- async sendMessage(u) {
1100
+ async sendMessage(d) {
1101
1101
  var i, T;
1102
- const { abortSignal: h } = u, p = u.sessionId || n || void 0, g = [];
1103
- g.push(u.message);
1104
- const y = await Y(
1105
- u,
1102
+ const { abortSignal: p } = d, m = d.sessionId || a || void 0, f = [];
1103
+ f.push(d.message);
1104
+ const I = await Y(
1106
1105
  d,
1106
+ u,
1107
1107
  { isStreaming: !1 },
1108
1108
  o,
1109
1109
  c,
1110
- p
1110
+ m
1111
1111
  );
1112
- let I = await Ee(
1113
- y,
1114
- d,
1115
- { abortSignal: h }
1112
+ let h = await Ee(
1113
+ I,
1114
+ u,
1115
+ { abortSignal: p }
1116
1116
  );
1117
- const A = [], f = [];
1118
- for (; I.status.message && o; ) {
1117
+ const A = [], g = [];
1118
+ for (; h.status.message && o; ) {
1119
1119
  const x = L(
1120
- I.status.message
1120
+ h.status.message
1121
1121
  );
1122
1122
  if (x.length === 0)
1123
1123
  break;
1124
1124
  A.push(...x);
1125
1125
  const w = [];
1126
1126
  let P = !1;
1127
- for (const _ of x) {
1127
+ for (const O of x) {
1128
1128
  const {
1129
1129
  toolCallId: $,
1130
1130
  toolId: S,
1131
- arguments: m
1132
- } = _.data;
1131
+ arguments: y
1132
+ } = O.data;
1133
1133
  try {
1134
1134
  const C = await Z(
1135
1135
  o,
1136
1136
  S,
1137
- m
1137
+ y
1138
1138
  ), { result: v, returnToAgent: R, agentMessage: k } = de(C);
1139
- R && (P = !0), k && f.push(
1139
+ R && (P = !0), k && g.push(
1140
1140
  G(k)
1141
1141
  );
1142
1142
  const b = W(
@@ -1155,37 +1155,37 @@ function rt(e) {
1155
1155
  w.push(v), A.push(v);
1156
1156
  }
1157
1157
  }
1158
- if (g.push(I.status.message), P) {
1159
- const _ = X(w);
1160
- I = await ae(
1161
- I.id,
1162
- _,
1163
- d,
1158
+ if (f.push(h.status.message), P) {
1159
+ const O = X(w);
1160
+ h = await ne(
1161
+ h.id,
1162
+ O,
1163
+ u,
1164
1164
  o,
1165
1165
  c,
1166
- p,
1167
- h
1166
+ m,
1167
+ p
1168
1168
  );
1169
1169
  } else
1170
1170
  break;
1171
1171
  }
1172
- if (A.length > 0 && (i = I.status) != null && i.message) {
1172
+ if (A.length > 0 && (i = h.status) != null && i.message) {
1173
1173
  const x = {
1174
- ...I.status.message,
1174
+ ...h.status.message,
1175
1175
  parts: A
1176
1176
  };
1177
- I = {
1178
- ...I,
1177
+ h = {
1178
+ ...h,
1179
1179
  status: {
1180
- ...I.status,
1180
+ ...h.status,
1181
1181
  message: x
1182
1182
  }
1183
1183
  };
1184
1184
  }
1185
- if (f.length > 0) {
1186
- const x = f.map((P) => D(P)).join(" "), w = G(x);
1185
+ if (g.length > 0) {
1186
+ const x = g.map((P) => D(P)).join(" "), w = G(x);
1187
1187
  return {
1188
- ...I,
1188
+ ...h,
1189
1189
  // Keep the enhanced message with tool results
1190
1190
  // The agent message will be handled separately by the caller
1191
1191
  text: x,
@@ -1194,94 +1194,94 @@ function rt(e) {
1194
1194
  };
1195
1195
  }
1196
1196
  return {
1197
- ...I,
1197
+ ...h,
1198
1198
  text: D(
1199
- ((T = I.status) == null ? void 0 : T.message) || {
1199
+ ((T = h.status) == null ? void 0 : T.message) || {
1200
1200
  parts: [],
1201
1201
  messageId: N()
1202
1202
  }
1203
1203
  )
1204
1204
  };
1205
1205
  },
1206
- async *sendMessageStream(u) {
1206
+ async *sendMessageStream(d) {
1207
1207
  const {
1208
- withHistory: h = !0,
1209
- abortSignal: p,
1210
- enableStreaming: g
1211
- } = u, y = u.sessionId || n || void 0, I = g ?? l, A = [];
1212
- A.push(u.message);
1213
- const f = await Y(
1214
- u,
1208
+ withHistory: p = !0,
1209
+ abortSignal: m,
1210
+ enableStreaming: f
1211
+ } = d, I = d.sessionId || a || void 0, h = f ?? l, A = [];
1212
+ A.push(d.message);
1213
+ const g = await Y(
1215
1214
  d,
1215
+ u,
1216
1216
  {
1217
1217
  isStreaming: !0,
1218
1218
  // Always use message/stream endpoint for SSE
1219
- enableTokenStreaming: I
1219
+ enableTokenStreaming: h
1220
1220
  },
1221
1221
  o,
1222
1222
  c,
1223
- y
1223
+ I
1224
1224
  ), i = Pe(
1225
- f,
1226
- d,
1225
+ g,
1226
+ u,
1227
1227
  {
1228
- enableTokenStreaming: I,
1228
+ enableTokenStreaming: h,
1229
1229
  // Token streaming is optional
1230
1230
  streamingTimeout: r,
1231
- abortSignal: p
1231
+ abortSignal: m
1232
1232
  }
1233
1233
  );
1234
- yield* _e(
1234
+ yield* Oe(
1235
1235
  i,
1236
1236
  o,
1237
1237
  c,
1238
- d,
1239
- y,
1240
- h,
1241
- A,
1238
+ u,
1239
+ I,
1242
1240
  p,
1241
+ A,
1242
+ m,
1243
1243
  {
1244
1244
  isStreaming: !0,
1245
- enableTokenStreaming: I,
1245
+ enableTokenStreaming: h,
1246
1246
  streamingTimeout: r
1247
1247
  }
1248
1248
  );
1249
1249
  },
1250
- async continueTask(u, h, p) {
1250
+ async continueTask(d, p, m) {
1251
1251
  var A;
1252
- const g = ie(h);
1253
- let I = await ae(
1254
- u,
1255
- g,
1252
+ const f = ie(p);
1253
+ let h = await ne(
1256
1254
  d,
1255
+ f,
1256
+ u,
1257
1257
  o,
1258
1258
  c,
1259
- p
1259
+ m
1260
1260
  );
1261
- for (; I.status.state === "input-required" && I.status.message && o; ) {
1262
- const f = L(
1263
- I.status.message
1261
+ for (; h.status.state === "input-required" && h.status.message && o; ) {
1262
+ const g = L(
1263
+ h.status.message
1264
1264
  );
1265
- if (f.length === 0)
1265
+ if (g.length === 0)
1266
1266
  break;
1267
- const { results: i, shouldReturnToAgent: T } = await Oe(f, o);
1267
+ const { results: i, shouldReturnToAgent: T } = await _e(g, o);
1268
1268
  if (T) {
1269
1269
  const x = X(i);
1270
- I = await ae(
1271
- I.id,
1270
+ h = await ne(
1271
+ h.id,
1272
1272
  x,
1273
- d,
1273
+ u,
1274
1274
  o,
1275
1275
  c,
1276
- p
1276
+ m
1277
1277
  );
1278
1278
  } else
1279
1279
  break;
1280
1280
  }
1281
1281
  return {
1282
- ...I,
1282
+ ...h,
1283
1283
  text: D(
1284
- ((A = I.status) == null ? void 0 : A.message) || {
1284
+ ((A = h.status) == null ? void 0 : A.message) || {
1285
1285
  parts: [],
1286
1286
  messageId: N()
1287
1287
  }
@@ -1297,17 +1297,17 @@ function rt(e) {
1297
1297
  };
1298
1298
  }
1299
1299
  function it(e) {
1300
- const s = [];
1300
+ const t = [];
1301
1301
  if (e.content) {
1302
1302
  const r = {
1303
1303
  type: "text",
1304
1304
  text: e.content
1305
1305
  };
1306
- s.push(r);
1306
+ t.push(r);
1307
1307
  }
1308
- const t = e.context;
1309
- if (t && !Array.isArray(t) && Array.isArray(t.file_parts)) {
1310
- for (const r of t.file_parts)
1308
+ const s = e.context;
1309
+ if (s && !Array.isArray(s) && Array.isArray(s.file_parts)) {
1310
+ for (const r of s.file_parts)
1311
1311
  if (r != null && r.uri) {
1312
1312
  const o = {
1313
1313
  type: "file",
@@ -1318,138 +1318,145 @@ function it(e) {
1318
1318
  // Pass attachment ID in metadata if available
1319
1319
  metadata: r.id ? { id: r.id } : void 0
1320
1320
  };
1321
- s.push(o);
1321
+ t.push(o);
1322
1322
  }
1323
1323
  }
1324
- const a = e.role === "user" ? "user" : "agent", n = e.ts ? e.ts * 1e3 : (/* @__PURE__ */ new Date(
1324
+ const n = e.role === "user" ? "user" : "agent", a = e.ts ? e.ts * 1e3 : (/* @__PURE__ */ new Date(
1325
1325
  e.created_at.replace(" ", "T") + "Z"
1326
1326
  )).getTime();
1327
1327
  return {
1328
- role: a,
1328
+ role: n,
1329
1329
  kind: "message",
1330
- parts: s,
1330
+ parts: t,
1331
1331
  messageId: N(),
1332
1332
  metadata: {
1333
- timestamp: n,
1333
+ timestamp: a,
1334
1334
  serverId: e.message_id,
1335
1335
  chatId: e.chat_id
1336
1336
  }
1337
1337
  };
1338
1338
  }
1339
1339
  function lt(e) {
1340
- var n, r, o, c, l, d;
1341
- const t = e.messages.filter(
1342
- (u) => u.role !== "tool_call" && u.role !== "tool_result"
1343
- ).map(it), a = {
1344
- currentPage: ((n = e.metadata) == null ? void 0 : n.current_page) ?? 1,
1345
- itemsPerPage: ((r = e.metadata) == null ? void 0 : r.items_per_page) ?? 10,
1346
- totalPages: ((o = e.metadata) == null ? void 0 : o.total_pages) ?? 1,
1347
- totalMessages: ((c = e.metadata) == null ? void 0 : c.total_messages) ?? t.length,
1348
- hasMore: (((l = e.metadata) == null ? void 0 : l.current_page) ?? 1) < (((d = e.metadata) == null ? void 0 : d.total_pages) ?? 1)
1340
+ if (e.role !== "tool_call" && e.role !== "tool_result")
1341
+ return !1;
1342
+ try {
1343
+ return JSON.parse(e.content).tool_id === "wpcom__think";
1344
+ } catch {
1345
+ return !1;
1346
+ }
1347
+ }
1348
+ function ct(e, t = !1) {
1349
+ var r, o, c, l, u, d;
1350
+ const n = e.messages.filter((p) => p.role === "tool_call" ? !1 : p.role === "tool_result" ? !(!t || lt(p)) : !0).map(it), a = {
1351
+ currentPage: ((r = e.metadata) == null ? void 0 : r.current_page) ?? 1,
1352
+ itemsPerPage: ((o = e.metadata) == null ? void 0 : o.items_per_page) ?? 10,
1353
+ totalPages: ((c = e.metadata) == null ? void 0 : c.total_pages) ?? 1,
1354
+ totalMessages: ((l = e.metadata) == null ? void 0 : l.total_messages) ?? n.length,
1355
+ hasMore: (((u = e.metadata) == null ? void 0 : u.current_page) ?? 1) < (((d = e.metadata) == null ? void 0 : d.total_pages) ?? 1)
1349
1356
  };
1350
1357
  return {
1351
- messages: t,
1358
+ messages: n,
1352
1359
  pagination: a,
1353
1360
  chatId: e.chat_id,
1354
1361
  sessionId: e.session_id
1355
1362
  };
1356
1363
  }
1357
1364
  class V extends Error {
1358
- constructor(s, t, a) {
1359
- super(s), this.statusCode = t, this.details = a, this.name = "ServerConversationError";
1365
+ constructor(t, s, n) {
1366
+ super(t), this.statusCode = s, this.details = n, this.name = "ServerConversationError";
1360
1367
  }
1361
1368
  }
1362
1369
  const fe = "https://public-api.wordpress.com";
1363
- async function le(e, s, t = 1, a = 50) {
1364
- const { botId: n, apiBaseUrl: r = fe, authProvider: o } = s;
1365
- if (!e || !n)
1370
+ async function le(e, t, s = 1, n = 50, a = !1) {
1371
+ const { botId: r, apiBaseUrl: o = fe, authProvider: c } = t;
1372
+ if (!e || !r)
1366
1373
  throw new Error(
1367
1374
  "chatId and botId are required to load conversation from server"
1368
1375
  );
1369
- const c = Math.max(1, Math.min(t, 100)), l = Math.max(1, Math.min(a, 100)), d = new URL(
1370
- `${r}/wpcom/v2/odie/chat/${encodeURIComponent(
1371
- n
1376
+ const l = Math.max(1, Math.min(s, 100)), u = Math.max(1, Math.min(n, 100)), d = new URL(
1377
+ `${o}/wpcom/v2/odie/chat/${encodeURIComponent(
1378
+ r
1372
1379
  )}/${encodeURIComponent(e)}`
1373
1380
  );
1374
- d.searchParams.set("page_number", c.toString()), d.searchParams.set("items_per_page", l.toString()), M(
1381
+ d.searchParams.set("page_number", l.toString()), d.searchParams.set("items_per_page", u.toString()), M(
1375
1382
  "Loading conversation from server: %s (page %d)",
1376
1383
  e,
1377
- c
1384
+ l
1378
1385
  );
1379
1386
  try {
1380
- const u = {
1387
+ const p = {
1381
1388
  "Content-Type": "application/json"
1382
1389
  };
1383
- if (o) {
1384
- const y = await o();
1385
- Object.assign(u, y);
1390
+ if (c) {
1391
+ const h = await c();
1392
+ Object.assign(p, h);
1386
1393
  }
1387
- const h = await fetch(d.toString(), {
1394
+ const m = await fetch(d.toString(), {
1388
1395
  method: "GET",
1389
- headers: u
1396
+ headers: p
1390
1397
  });
1391
- if (!h.ok) {
1392
- const y = await h.text();
1393
- let I = `Failed to load conversation from server: ${h.status} ${h.statusText}`;
1398
+ if (!m.ok) {
1399
+ const h = await m.text();
1400
+ let A = `Failed to load conversation from server: ${m.status} ${m.statusText}`;
1394
1401
  try {
1395
- const f = JSON.parse(y);
1396
- f.message && (I = f.message);
1402
+ const i = JSON.parse(h);
1403
+ i.message && (A = i.message);
1397
1404
  } catch {
1398
1405
  }
1399
- const A = new V(
1400
- I,
1401
- h.status,
1402
- y
1406
+ const g = new V(
1407
+ A,
1408
+ m.status,
1409
+ h
1403
1410
  );
1404
- throw M("Failed to load conversation from server: %O", A), A;
1411
+ throw M("Failed to load conversation from server: %O", g), g;
1405
1412
  }
1406
- const p = await h.json(), g = lt(p);
1413
+ const f = await m.json(), I = ct(f, a);
1407
1414
  return M(
1408
1415
  "Loaded %d messages from server (page %d/%d)",
1409
- g.messages.length,
1410
- g.pagination.currentPage,
1411
- g.pagination.totalPages
1412
- ), g;
1413
- } catch (u) {
1414
- if (u instanceof V)
1415
- throw u;
1416
- const h = new V(
1417
- `Network error loading conversation: ${u.message}`,
1416
+ I.messages.length,
1417
+ I.pagination.currentPage,
1418
+ I.pagination.totalPages
1419
+ ), I;
1420
+ } catch (p) {
1421
+ if (p instanceof V)
1422
+ throw p;
1423
+ const m = new V(
1424
+ `Network error loading conversation: ${p.message}`,
1418
1425
  void 0,
1419
- u
1426
+ p
1420
1427
  );
1421
- throw M("Network error loading conversation: %O", u), h;
1428
+ throw M("Network error loading conversation: %O", p), m;
1422
1429
  }
1423
1430
  }
1424
- async function Jt(e, s, t = !1) {
1425
- const { apiBaseUrl: a = fe, authProvider: n } = s, r = new URL(
1426
- `${a}/wpcom/v2/odie/conversations/${encodeURIComponent(
1431
+ async function Kt(e, t, s = !1) {
1432
+ const { apiBaseUrl: n = fe, authProvider: a } = t, r = new URL(
1433
+ `${n}/wpcom/v2/odie/conversations/${encodeURIComponent(
1427
1434
  e
1428
1435
  )}`
1429
1436
  );
1430
1437
  r.searchParams.set(
1431
1438
  "truncation_method",
1432
- t ? "first_message" : "last_message"
1439
+ s ? "first_message" : "last_message"
1433
1440
  ), M("Listing conversations from server for bot: %s", e);
1434
1441
  try {
1435
1442
  const o = {
1436
1443
  "Content-Type": "application/json"
1437
1444
  };
1438
- if (n) {
1439
- const d = await n();
1440
- Object.assign(o, d);
1445
+ if (a) {
1446
+ const u = await a();
1447
+ Object.assign(o, u);
1441
1448
  }
1442
1449
  const c = await fetch(r.toString(), {
1443
1450
  method: "GET",
1444
1451
  headers: o
1445
1452
  });
1446
1453
  if (!c.ok) {
1447
- const d = await c.text(), u = `Failed to list conversations: ${c.status} ${c.statusText}`, h = new V(
1448
- u,
1454
+ const u = await c.text(), d = `Failed to list conversations: ${c.status} ${c.statusText}`, p = new V(
1455
+ d,
1449
1456
  c.status,
1450
- d
1457
+ u
1451
1458
  );
1452
- throw M("Failed to list conversations: %O", h), h;
1459
+ throw M("Failed to list conversations: %O", p), p;
1453
1460
  }
1454
1461
  const l = await c.json();
1455
1462
  return M("Loaded %d conversations from server", l.length), l;
@@ -1464,79 +1471,87 @@ async function Jt(e, s, t = !1) {
1464
1471
  throw M("Network error listing conversations: %O", o), c;
1465
1472
  }
1466
1473
  }
1467
- async function Kt(e, s, t = 10) {
1468
- const a = await le(e, s, 1, 50);
1474
+ async function Lt(e, t, s = 10, n = !1) {
1475
+ const a = await le(
1476
+ e,
1477
+ t,
1478
+ 1,
1479
+ 50,
1480
+ n
1481
+ );
1469
1482
  if (!a.pagination.hasMore)
1470
1483
  return a;
1471
- const n = Math.min(
1484
+ const r = Math.min(
1472
1485
  a.pagination.totalPages,
1473
- t
1474
- ), r = [...a.messages], o = [];
1475
- for (let c = 2; c <= n; c++)
1476
- o.push(le(e, s, c, 50));
1486
+ s
1487
+ ), o = [...a.messages], c = [];
1488
+ for (let l = 2; l <= r; l++)
1489
+ c.push(
1490
+ le(e, t, l, 50, n)
1491
+ );
1477
1492
  try {
1478
- const c = await Promise.all(o);
1479
- for (const l of c)
1480
- r.push(...l.messages);
1493
+ const l = await Promise.all(c);
1494
+ for (const u of l)
1495
+ o.push(...u.messages);
1481
1496
  return {
1482
- messages: r,
1497
+ messages: o,
1483
1498
  pagination: {
1484
1499
  ...a.pagination,
1485
- currentPage: n,
1486
- hasMore: n < a.pagination.totalPages
1500
+ currentPage: r,
1501
+ hasMore: r < a.pagination.totalPages
1487
1502
  },
1488
1503
  chatId: a.chatId
1489
1504
  };
1490
- } catch (c) {
1491
- return M("Failed to load all pages: %O", c), {
1492
- messages: r,
1505
+ } catch (l) {
1506
+ return M("Failed to load all pages: %O", l), {
1507
+ messages: o,
1493
1508
  pagination: a.pagination,
1494
1509
  chatId: a.chatId
1495
1510
  };
1496
1511
  }
1497
1512
  }
1498
1513
  const me = "a8c_agenttic_conversation_history";
1499
- function ct(e) {
1500
- var h, p;
1501
- const s = e.parts.filter(
1502
- (g) => g.type === "text"
1503
- ), t = s.map((g) => g.text).join(`
1504
- `), a = s.some(
1505
- (g) => {
1506
- var y;
1507
- return ((y = g.metadata) == null ? void 0 : y.contentType) === "context";
1514
+ function ut(e) {
1515
+ var p, m;
1516
+ const t = e.parts.filter(
1517
+ (f) => f.type === "text"
1518
+ ), s = t.map((f) => f.text).join(`
1519
+ `), n = t.some(
1520
+ (f) => {
1521
+ var I;
1522
+ return ((I = f.metadata) == null ? void 0 : I.contentType) === "context";
1508
1523
  }
1509
- ) ? "context" : void 0, n = e.parts.filter(
1510
- (g) => g.type === "data" && "toolCallId" in g.data && "arguments" in g.data
1511
- ).map((g) => ({
1512
- toolCallId: g.data.toolCallId,
1513
- toolId: g.data.toolId,
1514
- arguments: g.data.arguments
1524
+ ) ? "context" : void 0, a = e.parts.filter(
1525
+ (f) => f.type === "data" && "toolCallId" in f.data && "arguments" in f.data
1526
+ ).map((f) => ({
1527
+ toolCallId: f.data.toolCallId,
1528
+ toolId: f.data.toolId,
1529
+ arguments: f.data.arguments
1515
1530
  })), r = e.parts.filter(
1516
- (g) => g.type === "data" && "toolCallId" in g.data && "result" in g.data
1517
- ).map((g) => ({
1518
- toolCallId: g.data.toolCallId,
1519
- result: g.data.result,
1520
- error: g.data.error
1521
- })), o = e.parts.filter((g) => g.type === "file").map((g) => ({
1522
- name: g.file.name,
1523
- mimeType: g.file.mimeType,
1524
- uri: g.file.uri
1525
- })), l = n.length > 0 || r.length > 0 ? "agent" : e.role, d = ((h = e.metadata) == null ? void 0 : h.timestamp) ?? Date.now(), u = ((p = e.metadata) == null ? void 0 : p.archived) ?? void 0;
1531
+ (f) => f.type === "data" && "toolCallId" in f.data && "result" in f.data
1532
+ ).map((f) => ({
1533
+ toolCallId: f.data.toolCallId,
1534
+ result: f.data.result,
1535
+ error: f.data.error
1536
+ })), o = e.parts.filter((f) => f.type === "file").map((f) => ({
1537
+ name: f.file.name,
1538
+ mimeType: f.file.mimeType,
1539
+ uri: f.file.uri
1540
+ })), l = a.length > 0 || r.length > 0 ? "agent" : e.role, u = ((p = e.metadata) == null ? void 0 : p.timestamp) ?? Date.now(), d = ((m = e.metadata) == null ? void 0 : m.archived) ?? void 0;
1526
1541
  return {
1527
1542
  role: l,
1528
- content: t || "(No text content)",
1529
- timestamp: d,
1530
- ...u !== void 0 && { archived: u },
1531
- ...a && { contentType: a },
1543
+ content: s || "(No text content)",
1544
+ timestamp: u,
1545
+ ...d !== void 0 && { archived: d },
1546
+ ...n && { contentType: n },
1532
1547
  ...o.length > 0 && { files: o },
1533
- ...n.length > 0 && { toolCalls: n },
1548
+ ...a.length > 0 && { toolCalls: a },
1534
1549
  ...r.length > 0 && { toolResults: r }
1535
1550
  };
1536
1551
  }
1537
- function ut(e) {
1538
- const s = [];
1539
- if (e.content && e.content !== "(No text content)" && s.push({
1552
+ function dt(e) {
1553
+ const t = [];
1554
+ if (e.content && e.content !== "(No text content)" && t.push({
1540
1555
  type: "text",
1541
1556
  text: e.content,
1542
1557
  ...e.contentType && {
@@ -1545,39 +1560,39 @@ function ut(e) {
1545
1560
  }
1546
1561
  }
1547
1562
  }), e.files)
1548
- for (const t of e.files)
1549
- s.push({
1563
+ for (const s of e.files)
1564
+ t.push({
1550
1565
  type: "file",
1551
1566
  file: {
1552
- name: t.name,
1553
- mimeType: t.mimeType,
1554
- uri: t.uri
1567
+ name: s.name,
1568
+ mimeType: s.mimeType,
1569
+ uri: s.uri
1555
1570
  }
1556
1571
  });
1557
1572
  if (e.toolCalls)
1558
- for (const t of e.toolCalls)
1559
- s.push({
1573
+ for (const s of e.toolCalls)
1574
+ t.push({
1560
1575
  type: "data",
1561
1576
  data: {
1562
- toolCallId: t.toolCallId,
1563
- toolId: t.toolId,
1564
- arguments: t.arguments
1577
+ toolCallId: s.toolCallId,
1578
+ toolId: s.toolId,
1579
+ arguments: s.arguments
1565
1580
  }
1566
1581
  });
1567
1582
  if (e.toolResults)
1568
- for (const t of e.toolResults)
1569
- s.push({
1583
+ for (const s of e.toolResults)
1584
+ t.push({
1570
1585
  type: "data",
1571
1586
  data: {
1572
- toolCallId: t.toolCallId,
1573
- result: t.result,
1574
- ...t.error && { error: t.error }
1587
+ toolCallId: s.toolCallId,
1588
+ result: s.result,
1589
+ ...s.error && { error: s.error }
1575
1590
  }
1576
1591
  });
1577
1592
  return {
1578
1593
  role: e.role,
1579
1594
  kind: "message",
1580
- parts: s,
1595
+ parts: t,
1581
1596
  messageId: N(),
1582
1597
  metadata: {
1583
1598
  timestamp: e.timestamp,
@@ -1588,50 +1603,50 @@ function ut(e) {
1588
1603
  }
1589
1604
  };
1590
1605
  }
1591
- const B = /* @__PURE__ */ new Map(), dt = 50;
1592
- async function gt(e, s, t) {
1593
- const a = t || e;
1594
- if (B.set(a, [...s]), B.size > dt) {
1595
- const n = B.keys().next().value;
1596
- n && B.delete(n);
1606
+ const B = /* @__PURE__ */ new Map(), gt = 50;
1607
+ async function ft(e, t, s) {
1608
+ const n = s || e;
1609
+ if (B.set(n, [...t]), B.size > gt) {
1610
+ const a = B.keys().next().value;
1611
+ a && B.delete(a);
1597
1612
  }
1598
1613
  if (!(typeof sessionStorage > "u"))
1599
1614
  try {
1600
- const n = {
1601
- storageKey: a,
1602
- messages: s.map(ct),
1615
+ const a = {
1616
+ storageKey: n,
1617
+ messages: t.map(ut),
1603
1618
  lastUpdated: Date.now()
1604
1619
  };
1605
1620
  sessionStorage.setItem(
1606
- `${me}_${a}`,
1607
- JSON.stringify(n)
1621
+ `${me}_${n}`,
1622
+ JSON.stringify(a)
1608
1623
  );
1609
- } catch (n) {
1624
+ } catch (a) {
1610
1625
  M(
1611
1626
  "Failed to store conversation in sessionStorage for key %s: %O",
1612
- a,
1613
- n
1627
+ n,
1628
+ a
1614
1629
  );
1615
1630
  }
1616
1631
  }
1617
- async function ft(e, s, t) {
1618
- return t != null && t.odieBotId ? mt(e, t) : pt(
1632
+ async function mt(e, t, s) {
1633
+ return s != null && s.odieBotId ? pt(e, s) : ht(
1619
1634
  e,
1620
- s
1635
+ t
1621
1636
  );
1622
1637
  }
1623
- async function mt(e, s) {
1624
- const { odieBotId: t, authProvider: a } = s;
1625
- if (!t)
1638
+ async function pt(e, t) {
1639
+ const { odieBotId: s, authProvider: n } = t;
1640
+ if (!s)
1626
1641
  throw new Error("odieBotId is required for server storage");
1627
- const n = fe;
1642
+ const a = fe;
1628
1643
  try {
1629
1644
  const o = await le(
1630
1645
  e,
1631
1646
  {
1632
- botId: t,
1633
- apiBaseUrl: n,
1634
- authProvider: a
1647
+ botId: s,
1648
+ apiBaseUrl: a,
1649
+ authProvider: n
1635
1650
  },
1636
1651
  1,
1637
1652
  50
@@ -1650,85 +1665,85 @@ async function mt(e, s) {
1650
1665
  throw M("Failed to load conversation from server: %O", r), r;
1651
1666
  }
1652
1667
  }
1653
- async function pt(e, s) {
1654
- const t = s || e;
1655
- if (B.has(t))
1668
+ async function ht(e, t) {
1669
+ const s = t || e;
1670
+ if (B.has(s))
1656
1671
  return {
1657
- messages: [...B.get(t)]
1672
+ messages: [...B.get(s)]
1658
1673
  };
1659
1674
  if (typeof sessionStorage > "u")
1660
1675
  return { messages: [] };
1661
1676
  try {
1662
- const a = sessionStorage.getItem(
1663
- `${me}_${t}`
1677
+ const n = sessionStorage.getItem(
1678
+ `${me}_${s}`
1664
1679
  );
1665
- if (a) {
1666
- const r = JSON.parse(a).messages.map(ut);
1667
- return B.set(t, r), { messages: [...r] };
1680
+ if (n) {
1681
+ const r = JSON.parse(n).messages.map(dt);
1682
+ return B.set(s, r), { messages: [...r] };
1668
1683
  }
1669
- } catch (a) {
1684
+ } catch (n) {
1670
1685
  M(
1671
1686
  "Failed to load conversation from sessionStorage for key %s: %O",
1672
- t,
1673
- a
1687
+ s,
1688
+ n
1674
1689
  );
1675
1690
  }
1676
1691
  return { messages: [] };
1677
1692
  }
1678
- async function ht(e, s) {
1679
- const t = s || e;
1680
- if (B.delete(t), !(typeof sessionStorage > "u"))
1693
+ async function yt(e, t) {
1694
+ const s = t || e;
1695
+ if (B.delete(s), !(typeof sessionStorage > "u"))
1681
1696
  try {
1682
- sessionStorage.removeItem(`${me}_${t}`);
1683
- } catch (a) {
1697
+ sessionStorage.removeItem(`${me}_${s}`);
1698
+ } catch (n) {
1684
1699
  M(
1685
1700
  "Failed to clear conversation from sessionStorage for key %s: %O",
1686
- t,
1687
- a
1701
+ s,
1702
+ n
1688
1703
  );
1689
1704
  }
1690
1705
  }
1691
1706
  function z(e) {
1692
- 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 || "toolCallId" in t.data && "result" in t.data : !0);
1707
+ 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 || "toolCallId" in s.data && "result" in s.data : !0);
1693
1708
  return {
1694
1709
  ...e,
1695
- parts: s,
1710
+ parts: t,
1696
1711
  // Preserve metadata if it exists, otherwise add timestamp
1697
1712
  metadata: e.metadata || {
1698
1713
  timestamp: Date.now()
1699
1714
  }
1700
1715
  };
1701
1716
  }
1702
- function yt(e) {
1703
- const s = [];
1704
- for (const t of e)
1705
- for (const a of t.parts)
1706
- if (a.type === "text")
1707
- s.push({
1717
+ function It(e) {
1718
+ const t = [];
1719
+ for (const s of e)
1720
+ for (const n of s.parts)
1721
+ if (n.type === "text")
1722
+ t.push({
1708
1723
  type: "data",
1709
1724
  data: {
1710
- role: t.role,
1711
- text: a.text
1725
+ role: s.role,
1726
+ text: n.text
1712
1727
  }
1713
1728
  });
1714
- else if (a.type === "file")
1715
- s.push(a);
1716
- else if (a.type === "data") {
1717
- if ("role" in a.data && "text" in a.data)
1729
+ else if (n.type === "file")
1730
+ t.push(n);
1731
+ else if (n.type === "data") {
1732
+ if ("role" in n.data && "text" in n.data)
1718
1733
  continue;
1719
- if ("toolCallId" in a.data && "arguments" in a.data) {
1720
- s.push(a);
1734
+ if ("toolCallId" in n.data && "arguments" in n.data) {
1735
+ t.push(n);
1721
1736
  continue;
1722
1737
  }
1723
- if ("toolCallId" in a.data && "result" in a.data) {
1724
- s.push(a);
1738
+ if ("toolCallId" in n.data && "result" in n.data) {
1739
+ t.push(n);
1725
1740
  continue;
1726
1741
  }
1727
1742
  }
1728
- return s;
1743
+ return t;
1729
1744
  }
1730
- function we(e, s = [], t = []) {
1731
- const a = yt(s), n = t.map((r) => {
1745
+ function we(e, t = [], s = []) {
1746
+ const n = It(t), a = s.map((r) => {
1732
1747
  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";
1733
1748
  return {
1734
1749
  type: "file",
@@ -1743,12 +1758,12 @@ function we(e, s = [], t = []) {
1743
1758
  return {
1744
1759
  role: "user",
1745
1760
  parts: [
1746
- ...a,
1761
+ ...n,
1747
1762
  {
1748
1763
  type: "text",
1749
1764
  text: e
1750
1765
  },
1751
- ...n
1766
+ ...a
1752
1767
  ],
1753
1768
  kind: "message",
1754
1769
  messageId: N(),
@@ -1757,241 +1772,241 @@ function we(e, s = [], t = []) {
1757
1772
  }
1758
1773
  };
1759
1774
  }
1760
- function It(e) {
1775
+ function wt(e) {
1761
1776
  return e != null && e.parts ? e.parts.filter(
1762
- (s) => s.type === "data" && "toolCallId" in s.data && "result" in s.data
1777
+ (t) => t.type === "data" && "toolCallId" in t.data && "result" in t.data
1763
1778
  ) : [];
1764
1779
  }
1765
- function ne(e, s) {
1780
+ function ae(e, t) {
1766
1781
  if (typeof localStorage > "u") {
1767
1782
  K("localStorage not available, cannot update session ID");
1768
1783
  return;
1769
1784
  }
1770
1785
  try {
1771
- const t = localStorage.getItem(e);
1772
- if (t) {
1773
- const a = JSON.parse(t), n = a.sessionId;
1774
- a.sessionId = s, localStorage.setItem(e, JSON.stringify(a)), K(
1786
+ const s = localStorage.getItem(e);
1787
+ if (s) {
1788
+ const n = JSON.parse(s), a = n.sessionId;
1789
+ n.sessionId = t, localStorage.setItem(e, JSON.stringify(n)), K(
1775
1790
  "Updated localStorage[%s] session ID: %s -> %s",
1776
1791
  e,
1777
- n,
1778
- s
1792
+ a,
1793
+ t
1779
1794
  );
1780
1795
  } else {
1781
- const a = {
1782
- sessionId: s,
1796
+ const n = {
1797
+ sessionId: t,
1783
1798
  timestamp: Date.now()
1784
1799
  };
1785
- localStorage.setItem(e, JSON.stringify(a)), K(
1800
+ localStorage.setItem(e, JSON.stringify(n)), K(
1786
1801
  "Created new session in localStorage[%s]: %s",
1787
1802
  e,
1788
- s
1803
+ t
1789
1804
  );
1790
1805
  }
1791
- } catch (t) {
1806
+ } catch (s) {
1792
1807
  K(
1793
1808
  "Failed to update localStorage sessionId to %s: %O",
1794
- s,
1795
- t
1809
+ t,
1810
+ s
1796
1811
  );
1797
1812
  }
1798
1813
  }
1799
- async function wt(e) {
1800
- const s = [];
1801
- for (const t of e)
1802
- if (t.parts && Array.isArray(t.parts))
1803
- if (t.parts.some(
1804
- (n) => n.type === "data" && "toolCallId" in n.data && "result" in n.data
1814
+ async function Tt(e) {
1815
+ const t = [];
1816
+ for (const s of e)
1817
+ if (s.parts && Array.isArray(s.parts))
1818
+ if (s.parts.some(
1819
+ (a) => a.type === "data" && "toolCallId" in a.data && "result" in a.data
1805
1820
  )) {
1806
- const n = ot(
1807
- t.parts
1821
+ const a = ot(
1822
+ s.parts
1808
1823
  );
1809
- s.push({
1810
- ...t,
1811
- parts: n
1824
+ t.push({
1825
+ ...s,
1826
+ parts: a
1812
1827
  });
1813
1828
  } else
1814
- s.push(t);
1829
+ t.push(s);
1815
1830
  else
1816
- s.push(t);
1817
- return nt(), s;
1831
+ t.push(s);
1832
+ return at(), t;
1818
1833
  }
1819
- function Tt() {
1834
+ function St() {
1820
1835
  const e = /* @__PURE__ */ new Map();
1821
- async function s(t, a) {
1822
- const n = e.get(t);
1823
- if (n != null && n.sessionId)
1836
+ async function t(s, n) {
1837
+ const a = e.get(s);
1838
+ if (a != null && a.sessionId)
1824
1839
  try {
1825
- await gt(
1826
- n.sessionId,
1827
- a,
1828
- n.conversationStorageKey
1840
+ await ft(
1841
+ a.sessionId,
1842
+ n,
1843
+ a.conversationStorageKey
1829
1844
  );
1830
1845
  } catch (r) {
1831
1846
  K(
1832
- `Failed to persist conversation history for agent ${t}:`,
1847
+ `Failed to persist conversation history for agent ${s}:`,
1833
1848
  r
1834
1849
  );
1835
1850
  }
1836
1851
  }
1837
1852
  return {
1838
- async createAgent(t, a) {
1839
- if (e.has(t))
1840
- return e.get(t).client;
1841
- const n = rt(a), r = a.sessionId || null, o = a.conversationStorageKey, c = a.sessionIdStorageKey, l = {
1842
- odieBotId: a.odieBotId,
1843
- authProvider: a.authProvider
1853
+ async createAgent(s, n) {
1854
+ if (e.has(s))
1855
+ return e.get(s).client;
1856
+ const a = rt(n), r = n.sessionId || null, o = n.conversationStorageKey, c = n.sessionIdStorageKey, l = {
1857
+ odieBotId: n.odieBotId,
1858
+ authProvider: n.authProvider
1844
1859
  };
1845
- let d = [];
1860
+ let u = [];
1846
1861
  if (r)
1847
1862
  try {
1848
- d = (await ft(
1863
+ u = (await mt(
1849
1864
  r,
1850
1865
  o,
1851
1866
  l
1852
1867
  )).messages;
1853
- } catch (h) {
1868
+ } catch (p) {
1854
1869
  K(
1855
- `Failed to load conversation history for agent ${t} with session ${r}:`,
1856
- h
1870
+ `Failed to load conversation history for agent ${s} with session ${r}:`,
1871
+ p
1857
1872
  );
1858
1873
  }
1859
- const u = {
1860
- client: n,
1874
+ const d = {
1875
+ client: a,
1861
1876
  sessionId: r,
1862
1877
  conversationStorageKey: o,
1863
1878
  sessionIdStorageKey: c,
1864
1879
  storageConfig: l,
1865
- conversationHistory: d,
1880
+ conversationHistory: u,
1866
1881
  currentAbortController: null
1867
1882
  };
1868
- return e.set(t, u), n;
1883
+ return e.set(s, d), a;
1869
1884
  },
1870
- getAgent(t) {
1871
- const a = e.get(t);
1872
- return (a == null ? void 0 : a.client) || null;
1885
+ getAgent(s) {
1886
+ const n = e.get(s);
1887
+ return (n == null ? void 0 : n.client) || null;
1873
1888
  },
1874
- hasAgent(t) {
1875
- return e.has(t);
1889
+ hasAgent(s) {
1890
+ return e.has(s);
1876
1891
  },
1877
- removeAgent(t) {
1878
- return e.delete(t);
1892
+ removeAgent(s) {
1893
+ return e.delete(s);
1879
1894
  },
1880
- async sendMessage(t, a, n = {}) {
1895
+ async sendMessage(s, n, a = {}) {
1881
1896
  var A;
1882
- const r = e.get(t);
1897
+ const r = e.get(s);
1883
1898
  if (!r)
1884
- throw new Error(`Agent with key "${t}" not found`);
1885
- const { withHistory: o = !0, sessionId: c, ...l } = n, { client: d, conversationHistory: u } = r, h = n.message || we(
1886
- a,
1887
- u,
1888
- n.imageUrls
1889
- ), p = await d.sendMessage({
1890
- message: h,
1899
+ throw new Error(`Agent with key "${s}" not found`);
1900
+ const { withHistory: o = !0, sessionId: c, ...l } = a, { client: u, conversationHistory: d } = r, p = a.message || we(
1901
+ n,
1902
+ d,
1903
+ a.imageUrls
1904
+ ), m = await u.sendMessage({
1905
+ message: p,
1891
1906
  withHistory: o,
1892
1907
  sessionId: c || r.sessionId || void 0,
1893
1908
  ...l
1894
1909
  });
1895
- if (p.sessionId) {
1896
- const f = r.sessionId;
1897
- r.sessionId = p.sessionId, f && p.sessionId && f !== p.sessionId && r.sessionIdStorageKey && (K(
1910
+ if (m.sessionId) {
1911
+ const g = r.sessionId;
1912
+ r.sessionId = m.sessionId, g && m.sessionId && g !== m.sessionId && r.sessionIdStorageKey && (K(
1898
1913
  "Session ID changed from %s to %s, updating localStorage",
1899
- f,
1900
- p.sessionId
1901
- ), ne(
1914
+ g,
1915
+ m.sessionId
1916
+ ), ae(
1902
1917
  r.sessionIdStorageKey,
1903
- p.sessionId
1918
+ m.sessionId
1904
1919
  ));
1905
1920
  }
1906
- let g = null;
1907
- if ((A = p.status) != null && A.message) {
1908
- const f = p.status.message.parts.filter(
1921
+ let f = null;
1922
+ if ((A = m.status) != null && A.message) {
1923
+ const g = m.status.message.parts.filter(
1909
1924
  (T) => T.type === "data" && "toolCallId" in T.data && ("arguments" in T.data || "result" in T.data)
1910
- ), i = p.status.message.parts.filter(
1925
+ ), i = m.status.message.parts.filter(
1911
1926
  (T) => T.type === "text"
1912
1927
  );
1913
- g = {
1928
+ f = {
1914
1929
  role: "agent",
1915
1930
  kind: "message",
1916
- parts: [...f, ...i],
1931
+ parts: [...g, ...i],
1917
1932
  messageId: N(),
1918
1933
  metadata: {
1919
1934
  timestamp: Date.now()
1920
1935
  }
1921
1936
  };
1922
1937
  }
1923
- const y = [
1924
- ...u,
1938
+ const I = [
1939
+ ...d,
1925
1940
  // Store only the new content from the user message (without history parts)
1926
- ie(a),
1941
+ ie(n),
1927
1942
  // Add complete agent response with tool calls/results if present
1928
- ...g ? [z(g)] : []
1943
+ ...f ? [z(f)] : []
1929
1944
  ];
1930
- let I = y;
1931
- if (p.agentMessage) {
1932
- const f = z(
1933
- p.agentMessage
1945
+ let h = I;
1946
+ if (m.agentMessage) {
1947
+ const g = z(
1948
+ m.agentMessage
1934
1949
  );
1935
- I = [
1936
- ...y,
1937
- f
1950
+ h = [
1951
+ ...I,
1952
+ g
1938
1953
  ];
1939
1954
  }
1940
- return r.conversationHistory = I, o && await s(
1941
- t,
1942
- I
1943
- ), p;
1955
+ return r.conversationHistory = h, o && await t(
1956
+ s,
1957
+ h
1958
+ ), m;
1944
1959
  },
1945
- async *sendMessageStream(t, a, n = {}) {
1946
- var T, x, w, P, _, $;
1947
- const r = e.get(t);
1960
+ async *sendMessageStream(s, n, a = {}) {
1961
+ var T, x, w, P, O, $;
1962
+ const r = e.get(s);
1948
1963
  if (!r)
1949
- throw new Error(`Agent with key "${t}" not found`);
1964
+ throw new Error(`Agent with key "${s}" not found`);
1950
1965
  const {
1951
1966
  withHistory: o = !0,
1952
1967
  abortSignal: c,
1953
1968
  metadata: l,
1954
- sessionId: d,
1955
- ...u
1956
- } = n, { client: h } = r, p = l ? (({ contentType: S, ...m }) => m)(l) : void 0, g = new AbortController();
1957
- r.currentAbortController = g, c && c.addEventListener(
1969
+ sessionId: u,
1970
+ ...d
1971
+ } = a, { client: p } = r, m = l ? (({ contentType: S, ...y }) => y)(l) : void 0, f = new AbortController();
1972
+ r.currentAbortController = f, c && c.addEventListener(
1958
1973
  "abort",
1959
- () => g.abort()
1974
+ () => f.abort()
1960
1975
  );
1961
- let y = [
1976
+ let I = [
1962
1977
  ...r.conversationHistory
1963
- ], I = [];
1964
- const A = await wt(
1965
- y
1978
+ ], h = [];
1979
+ const A = await Tt(
1980
+ I
1966
1981
  );
1967
- r.conversationHistory = A, y = A, o && await s(
1968
- t,
1982
+ r.conversationHistory = A, I = A, o && await t(
1983
+ s,
1969
1984
  A
1970
1985
  );
1971
- const f = n.message || we(
1972
- a,
1986
+ const g = a.message || we(
1987
+ n,
1973
1988
  A,
1974
- n.imageUrls
1989
+ a.imageUrls
1975
1990
  );
1976
- if (n.metadata && !n.message) {
1977
- const { contentType: S, ...m } = n.metadata;
1991
+ if (a.metadata && !a.message) {
1992
+ const { contentType: S, ...y } = a.metadata;
1978
1993
  if (S) {
1979
- const C = f.parts[f.parts.length - 1];
1994
+ const C = g.parts[g.parts.length - 1];
1980
1995
  C && C.type === "text" && (C.metadata = {
1981
1996
  ...C.metadata,
1982
1997
  contentType: S
1983
1998
  });
1984
1999
  }
1985
- Object.keys(m).length > 0 && (f.metadata = {
1986
- ...f.metadata,
1987
- ...m
2000
+ Object.keys(y).length > 0 && (g.metadata = {
2001
+ ...g.metadata,
2002
+ ...y
1988
2003
  });
1989
2004
  }
1990
- const i = ie(a, n.metadata);
1991
- if (n.imageUrls && n.imageUrls.length > 0) {
1992
- const S = n.imageUrls.map(
1993
- (m) => {
1994
- const C = typeof m == "string" ? m : m.url, v = typeof m == "string" ? void 0 : m.metadata, R = (v == null ? void 0 : v.fileType) || "image/jpeg";
2005
+ const i = ie(n, a.metadata);
2006
+ if (a.imageUrls && a.imageUrls.length > 0) {
2007
+ const S = a.imageUrls.map(
2008
+ (y) => {
2009
+ const C = typeof y == "string" ? y : y.url, v = typeof y == "string" ? void 0 : y.metadata, R = (v == null ? void 0 : v.fileType) || "image/jpeg";
1995
2010
  return {
1996
2011
  type: "file",
1997
2012
  file: {
@@ -2005,35 +2020,35 @@ function Tt() {
2005
2020
  );
2006
2021
  i.parts.push(...S);
2007
2022
  }
2008
- y = [
2009
- ...y,
2023
+ I = [
2024
+ ...I,
2010
2025
  i
2011
- ], r.conversationHistory = y, o && await s(
2012
- t,
2013
- y
2026
+ ], r.conversationHistory = I, o && await t(
2027
+ s,
2028
+ I
2014
2029
  );
2015
- for await (const S of h.sendMessageStream({
2016
- message: f,
2030
+ for await (const S of p.sendMessageStream({
2031
+ message: g,
2017
2032
  withHistory: o,
2018
- sessionId: d || r.sessionId || void 0,
2019
- abortSignal: g.signal,
2020
- ...u,
2021
- ...p && Object.keys(p).length > 0 && {
2022
- metadata: p
2033
+ sessionId: u || r.sessionId || void 0,
2034
+ abortSignal: f.signal,
2035
+ ...d,
2036
+ ...m && Object.keys(m).length > 0 && {
2037
+ metadata: m
2023
2038
  }
2024
2039
  })) {
2025
2040
  if (S.sessionId) {
2026
- const m = r.sessionId;
2027
- r.sessionId = S.sessionId, S.sessionId && m !== S.sessionId && r.sessionIdStorageKey && (K(
2041
+ const y = r.sessionId;
2042
+ r.sessionId = S.sessionId, S.sessionId && y !== S.sessionId && r.sessionIdStorageKey && (K(
2028
2043
  "Session ID %s, updating localStorage",
2029
- m ? `changed from ${m} to ${S.sessionId}` : `received: ${S.sessionId}`
2030
- ), ne(
2044
+ y ? `changed from ${y} to ${S.sessionId}` : `received: ${S.sessionId}`
2045
+ ), ae(
2031
2046
  r.sessionIdStorageKey,
2032
2047
  S.sessionId
2033
2048
  ));
2034
2049
  }
2035
2050
  if (((T = S.status) == null ? void 0 : T.state) === "input-required" && ((x = S.status) != null && x.message)) {
2036
- I = L(
2051
+ h = L(
2037
2052
  S.status.message
2038
2053
  ).map(
2039
2054
  (v) => v.data.toolCallId
@@ -2041,19 +2056,19 @@ function Tt() {
2041
2056
  const C = z(
2042
2057
  S.status.message
2043
2058
  );
2044
- y = [
2045
- ...y,
2059
+ I = [
2060
+ ...I,
2046
2061
  C
2047
- ], r.conversationHistory = y, o && await s(
2048
- t,
2049
- y
2062
+ ], r.conversationHistory = I, o && await t(
2063
+ s,
2064
+ I
2050
2065
  );
2051
2066
  }
2052
2067
  if (((w = S.status) == null ? void 0 : w.state) === "working" && ((P = S.status) != null && P.message) && !S.final) {
2053
- const C = It(
2068
+ const C = wt(
2054
2069
  S.status.message
2055
2070
  ).filter(
2056
- (v) => I.includes(
2071
+ (v) => h.includes(
2057
2072
  v.data.toolCallId
2058
2073
  )
2059
2074
  );
@@ -2064,81 +2079,81 @@ function Tt() {
2064
2079
  parts: C,
2065
2080
  messageId: N()
2066
2081
  };
2067
- y = [
2068
- ...y,
2082
+ I = [
2083
+ ...I,
2069
2084
  z(v)
2070
- ], r.conversationHistory = y, o && await s(
2071
- t,
2072
- y
2085
+ ], r.conversationHistory = I, o && await t(
2086
+ s,
2087
+ I
2073
2088
  );
2074
2089
  }
2075
2090
  }
2076
- if (S.final && ((_ = S.status) == null ? void 0 : _.state) !== "input-required") {
2077
- I = [];
2078
- let m = null;
2079
- ($ = S.status) != null && $.message && (m = z(
2091
+ if (S.final && ((O = S.status) == null ? void 0 : O.state) !== "input-required") {
2092
+ h = [];
2093
+ let y = null;
2094
+ ($ = S.status) != null && $.message && (y = z(
2080
2095
  S.status.message
2081
- ), y = [
2082
- ...y,
2083
- m
2084
- ], r.conversationHistory = y, o && await s(
2085
- t,
2096
+ ), I = [
2097
+ ...I,
2086
2098
  y
2099
+ ], r.conversationHistory = I, o && await t(
2100
+ s,
2101
+ I
2087
2102
  ));
2088
2103
  }
2089
2104
  yield S;
2090
2105
  }
2091
2106
  r.currentAbortController = null;
2092
2107
  },
2093
- async resetConversation(t) {
2094
- const a = e.get(t);
2095
- if (!a)
2096
- throw new Error(`Agent with key "${t}" not found`);
2097
- a.conversationHistory = [], a.sessionId && await ht(
2098
- a.sessionId,
2099
- a.conversationStorageKey
2100
- );
2101
- },
2102
- async replaceMessages(t, a) {
2103
- const n = e.get(t);
2108
+ async resetConversation(s) {
2109
+ const n = e.get(s);
2104
2110
  if (!n)
2105
- throw new Error(`Agent with key "${t}" not found`);
2106
- n.conversationHistory = [...a], n.sessionId && await s(t, a);
2111
+ throw new Error(`Agent with key "${s}" not found`);
2112
+ n.conversationHistory = [], n.sessionId && await yt(
2113
+ n.sessionId,
2114
+ n.conversationStorageKey
2115
+ );
2107
2116
  },
2108
- getConversationHistory(t) {
2109
- const a = e.get(t);
2117
+ async replaceMessages(s, n) {
2118
+ const a = e.get(s);
2110
2119
  if (!a)
2111
- throw new Error(`Agent with key "${t}" not found`);
2112
- return [...a.conversationHistory];
2120
+ throw new Error(`Agent with key "${s}" not found`);
2121
+ a.conversationHistory = [...n], a.sessionId && await t(s, n);
2113
2122
  },
2114
- updateSessionId(t, a) {
2115
- const n = e.get(t);
2123
+ getConversationHistory(s) {
2124
+ const n = e.get(s);
2116
2125
  if (!n)
2117
- throw new Error(`Agent with key "${t}" not found`);
2118
- n.sessionId = a, n.sessionIdStorageKey && ne(
2119
- n.sessionIdStorageKey,
2120
- a
2121
- );
2126
+ throw new Error(`Agent with key "${s}" not found`);
2127
+ return [...n.conversationHistory];
2122
2128
  },
2123
- abortCurrentRequest(t) {
2124
- const a = e.get(t);
2129
+ updateSessionId(s, n) {
2130
+ const a = e.get(s);
2125
2131
  if (!a)
2126
- throw new Error(`Agent with key "${t}" not found`);
2127
- a.currentAbortController && (a.currentAbortController.abort(), a.currentAbortController = null);
2132
+ throw new Error(`Agent with key "${s}" not found`);
2133
+ a.sessionId = n, a.sessionIdStorageKey && ae(
2134
+ a.sessionIdStorageKey,
2135
+ n
2136
+ );
2137
+ },
2138
+ abortCurrentRequest(s) {
2139
+ const n = e.get(s);
2140
+ if (!n)
2141
+ throw new Error(`Agent with key "${s}" not found`);
2142
+ n.currentAbortController && (n.currentAbortController.abort(), n.currentAbortController = null);
2128
2143
  },
2129
2144
  clear() {
2130
2145
  e.clear();
2131
2146
  }
2132
2147
  };
2133
2148
  }
2134
- const St = Tt();
2149
+ const vt = St();
2135
2150
  function Q() {
2136
- return St;
2151
+ return vt;
2137
2152
  }
2138
- function vt() {
2139
- const [e, s] = ve([]), t = F(
2153
+ function Mt() {
2154
+ const [e, t] = ve([]), s = F(
2140
2155
  (r) => {
2141
- s((o) => {
2156
+ t((o) => {
2142
2157
  const c = o.findIndex(
2143
2158
  (l) => l.id === r.id
2144
2159
  );
@@ -2150,37 +2165,37 @@ function vt() {
2150
2165
  });
2151
2166
  },
2152
2167
  []
2153
- ), a = F((r) => {
2154
- s((o) => o.filter((c) => c.id !== r));
2155
- }, []), n = F(() => {
2156
- s([]);
2168
+ ), n = F((r) => {
2169
+ t((o) => o.filter((c) => c.id !== r));
2170
+ }, []), a = F(() => {
2171
+ t([]);
2157
2172
  }, []);
2158
2173
  return {
2159
- registerMessageActions: t,
2160
- unregisterMessageActions: a,
2161
- clearAllMessageActions: n,
2174
+ registerMessageActions: s,
2175
+ unregisterMessageActions: n,
2176
+ clearAllMessageActions: a,
2162
2177
  registrations: e
2163
2178
  };
2164
2179
  }
2165
- function Mt(e, s) {
2166
- return s.flatMap((n) => typeof n.actions == "function" ? n.actions(e) : n.actions).filter((n) => !(n.condition && !n.condition(e))).map((n) => ({
2167
- id: n.id,
2168
- label: n.label,
2169
- icon: n.icon,
2170
- onClick: n.onClick,
2171
- tooltip: n.tooltip,
2172
- disabled: n.disabled || !1,
2173
- pressed: n.pressed,
2174
- showLabel: n.showLabel
2180
+ function xt(e, t) {
2181
+ return t.flatMap((a) => typeof a.actions == "function" ? a.actions(e) : a.actions).filter((a) => !(a.condition && !a.condition(e))).map((a) => ({
2182
+ id: a.id,
2183
+ label: a.label,
2184
+ icon: a.icon,
2185
+ onClick: a.onClick,
2186
+ tooltip: a.tooltip,
2187
+ disabled: a.disabled || !1,
2188
+ pressed: a.pressed,
2189
+ showLabel: a.showLabel
2175
2190
  }));
2176
2191
  }
2177
- const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s = "40%") => ({
2192
+ const oe = (e) => [...e].sort((t, s) => t.timestamp - s.timestamp), Ne = (e, t = "40%") => ({
2178
2193
  type: "component",
2179
2194
  component: () => qe.createElement("img", {
2180
2195
  src: e,
2181
2196
  alt: "Uploaded image",
2182
2197
  style: {
2183
- maxWidth: s,
2198
+ maxWidth: t,
2184
2199
  height: "auto",
2185
2200
  borderRadius: "8px",
2186
2201
  marginTop: "8px",
@@ -2188,83 +2203,83 @@ const oe = (e) => [...e].sort((s, t) => s.timestamp - t.timestamp), Ne = (e, s =
2188
2203
  display: "inline-block"
2189
2204
  }
2190
2205
  })
2191
- }), re = (e, s = []) => {
2206
+ }), re = (e, t = []) => {
2192
2207
  var o, c;
2193
2208
  if (e.parts.some((l) => {
2194
2209
  if (l.type === "data") {
2195
- const d = l.data;
2196
- return d.toolCallId || d.toolId || d.result;
2210
+ const u = l.data;
2211
+ return u.toolCallId || u.toolId || u.result;
2197
2212
  }
2198
2213
  return !1;
2199
2214
  }))
2200
2215
  return null;
2201
- const a = e.parts.map((l) => {
2202
- var d;
2216
+ const n = e.parts.map((l) => {
2217
+ var u;
2203
2218
  if (l.type === "text")
2204
2219
  return {
2205
- type: ((d = l.metadata) == null ? void 0 : d.contentType) || "text",
2220
+ type: ((u = l.metadata) == null ? void 0 : u.contentType) || "text",
2206
2221
  text: l.text
2207
2222
  };
2208
2223
  if (l.type === "file") {
2209
- const u = l.file.uri || (l.file.mimeType && l.file.bytes ? `data:${l.file.mimeType};base64,${l.file.bytes}` : void 0);
2210
- if (u)
2211
- return Ne(u);
2224
+ const d = l.file.uri || (l.file.mimeType && l.file.bytes ? `data:${l.file.mimeType};base64,${l.file.bytes}` : void 0);
2225
+ if (d)
2226
+ return Ne(d);
2212
2227
  }
2213
2228
  if (l.type === "data") {
2214
- const u = l.data;
2215
- return u.component && u.componentProps ? {
2229
+ const d = l.data;
2230
+ return d.component && d.componentProps ? {
2216
2231
  type: "component",
2217
- component: u.component,
2218
- componentProps: u.componentProps
2232
+ component: d.component,
2233
+ componentProps: d.componentProps
2219
2234
  } : {
2220
2235
  type: "text",
2221
- text: JSON.stringify(u)
2236
+ text: JSON.stringify(d)
2222
2237
  };
2223
2238
  }
2224
2239
  return {
2225
2240
  type: "text",
2226
2241
  text: "[Unsupported content]"
2227
2242
  };
2228
- }), n = ((o = e.metadata) == null ? void 0 : o.timestamp) ?? Date.now(), r = {
2243
+ }), a = ((o = e.metadata) == null ? void 0 : o.timestamp) ?? Date.now(), r = {
2229
2244
  id: e.messageId,
2230
2245
  role: e.role === "agent" ? "agent" : "user",
2231
- content: a,
2232
- timestamp: n,
2246
+ content: n,
2247
+ timestamp: a,
2233
2248
  archived: !!((c = e.metadata) != null && c.archived),
2234
2249
  showIcon: e.role === "agent",
2235
2250
  icon: e.role === "agent" ? "assistant" : void 0
2236
2251
  };
2237
- if (e.role === "agent" && s.length > 0) {
2238
- const l = Mt(
2252
+ if (e.role === "agent" && t.length > 0) {
2253
+ const l = xt(
2239
2254
  r,
2240
- s
2255
+ t
2241
2256
  );
2242
2257
  l.length > 0 && (r.actions = l);
2243
2258
  }
2244
2259
  return r;
2245
- }, xt = () => ({
2260
+ }, At = () => ({
2246
2261
  getClientContext: () => ({})
2247
- }), At = () => ({
2262
+ }), Ct = () => ({
2248
2263
  getAvailableTools: async () => [],
2249
2264
  executeTool: async () => ({
2250
2265
  success: !0,
2251
2266
  result: "No tools available"
2252
2267
  })
2253
- }), Ct = (e) => ["agentId", "agentUrl"].every((t) => {
2254
- const a = e[t];
2255
- return typeof a == "string" && a.trim().length > 0;
2268
+ }), bt = (e) => ["agentId", "agentUrl"].every((s) => {
2269
+ const n = e[s];
2270
+ return typeof n == "string" && n.trim().length > 0;
2256
2271
  });
2257
- function Lt(e) {
2258
- const s = {
2272
+ function Bt(e) {
2273
+ const t = {
2259
2274
  agentId: e.agentId,
2260
2275
  agentUrl: e.agentUrl,
2261
2276
  sessionId: e.sessionId,
2262
2277
  sessionIdStorageKey: e.sessionIdStorageKey
2263
- }, t = Ct(s), [a, n] = ve({
2278
+ }, s = bt(t), [n, a] = ve({
2264
2279
  clientMessages: [],
2265
2280
  uiMessages: [],
2266
2281
  isProcessing: !1,
2267
- error: t ? null : "Invalid agent configuration",
2282
+ error: s ? null : "Invalid agent configuration",
2268
2283
  suggestions: [],
2269
2284
  progressMessage: null
2270
2285
  }), {
@@ -2272,85 +2287,85 @@ function Lt(e) {
2272
2287
  unregisterMessageActions: o,
2273
2288
  clearAllMessageActions: c,
2274
2289
  registrations: l
2275
- } = vt(), d = He(l);
2290
+ } = Mt(), u = He(l);
2276
2291
  se(() => {
2277
- d.current = l;
2292
+ u.current = l;
2278
2293
  }, [l]);
2279
- const u = F(
2280
- (f) => f.map(
2281
- (i) => re(i, d.current)
2294
+ const d = F(
2295
+ (g) => g.map(
2296
+ (i) => re(i, u.current)
2282
2297
  ).filter((i) => i !== null),
2283
2298
  []
2284
2299
  // registrationsRef is stable, so no deps needed
2285
2300
  );
2286
2301
  se(() => {
2287
- if (!t)
2302
+ if (!s)
2288
2303
  return;
2289
2304
  (async () => {
2290
- const i = Q(), T = s.agentId;
2305
+ const i = Q(), T = t.agentId;
2291
2306
  if (i.hasAgent(T))
2292
- s.sessionId ? (i.updateSessionId(T, s.sessionId), i.getConversationHistory(T).length === 0 && n((P) => ({
2307
+ t.sessionId ? (i.updateSessionId(T, t.sessionId), i.getConversationHistory(T).length === 0 && a((P) => ({
2293
2308
  ...P,
2294
2309
  clientMessages: [],
2295
2310
  uiMessages: []
2296
- }))) : (i.updateSessionId(T, ""), await i.replaceMessages(T, []), n((w) => ({
2311
+ }))) : (i.updateSessionId(T, ""), await i.replaceMessages(T, []), a((w) => ({
2297
2312
  ...w,
2298
2313
  clientMessages: [],
2299
2314
  uiMessages: []
2300
2315
  })));
2301
2316
  else if (await i.createAgent(T, {
2302
- agentId: s.agentId,
2303
- agentUrl: s.agentUrl,
2304
- sessionId: s.sessionId,
2317
+ agentId: t.agentId,
2318
+ agentUrl: t.agentUrl,
2319
+ sessionId: t.sessionId,
2305
2320
  // Can be empty for new chats
2306
- sessionIdStorageKey: s.sessionIdStorageKey,
2307
- contextProvider: e.contextProvider || xt(),
2308
- toolProvider: e.toolProvider || At(),
2321
+ sessionIdStorageKey: t.sessionIdStorageKey,
2322
+ contextProvider: e.contextProvider || At(),
2323
+ toolProvider: e.toolProvider || Ct(),
2309
2324
  authProvider: e.authProvider,
2310
2325
  enableStreaming: e.enableStreaming,
2311
2326
  odieBotId: e.odieBotId
2312
- }), s.sessionId) {
2327
+ }), t.sessionId) {
2313
2328
  const w = i.getConversationHistory(T);
2314
- n((P) => {
2315
- const _ = u(w);
2329
+ a((P) => {
2330
+ const O = d(w);
2316
2331
  return {
2317
2332
  ...P,
2318
2333
  clientMessages: w,
2319
- uiMessages: _
2334
+ uiMessages: O
2320
2335
  };
2321
2336
  });
2322
2337
  } else
2323
- n((w) => ({
2338
+ a((w) => ({
2324
2339
  ...w,
2325
2340
  clientMessages: [],
2326
2341
  uiMessages: []
2327
2342
  }));
2328
2343
  })();
2329
2344
  }, [
2330
- s.sessionId,
2331
- s.agentId,
2332
- s.agentUrl,
2333
- s.sessionIdStorageKey,
2345
+ t.sessionId,
2346
+ t.agentId,
2347
+ t.agentUrl,
2348
+ t.sessionIdStorageKey,
2334
2349
  e.contextProvider,
2335
2350
  e.toolProvider,
2336
2351
  e.authProvider,
2337
2352
  e.enableStreaming,
2338
2353
  e.odieBotId,
2339
- t
2354
+ s
2340
2355
  ]);
2341
- const h = F(
2342
- async (f, i) => {
2356
+ const p = F(
2357
+ async (g, i) => {
2343
2358
  var $, S;
2344
- if (!t)
2359
+ if (!s)
2345
2360
  throw new Error("Invalid agent configuration");
2346
- const T = Q(), x = s.agentId, w = Date.now(), P = (i == null ? void 0 : i.type) || "text", _ = {
2361
+ const T = Q(), x = t.agentId, w = Date.now(), P = (i == null ? void 0 : i.type) || "text", O = {
2347
2362
  id: `user-${w}`,
2348
2363
  role: "user",
2349
2364
  content: [
2350
- { type: P, text: f },
2365
+ { type: P, text: g },
2351
2366
  // Map image URLs to component content parts
2352
- ...(($ = i == null ? void 0 : i.imageUrls) == null ? void 0 : $.map((m) => {
2353
- const C = typeof m == "string" ? m : m.url;
2367
+ ...(($ = i == null ? void 0 : i.imageUrls) == null ? void 0 : $.map((y) => {
2368
+ const C = typeof y == "string" ? y : y.url;
2354
2369
  return Ne(C);
2355
2370
  })) ?? []
2356
2371
  ],
@@ -2358,14 +2373,14 @@ function Lt(e) {
2358
2373
  archived: (i == null ? void 0 : i.archived) ?? !1,
2359
2374
  showIcon: !1
2360
2375
  };
2361
- n((m) => ({
2362
- ...m,
2363
- uiMessages: [...m.uiMessages, _],
2376
+ a((y) => ({
2377
+ ...y,
2378
+ uiMessages: [...y.uiMessages, O],
2364
2379
  isProcessing: !0,
2365
2380
  error: null
2366
2381
  }));
2367
2382
  try {
2368
- let m = null, C = !1;
2383
+ let y = null, C = !1;
2369
2384
  const v = {};
2370
2385
  (i != null && i.archived || i != null && i.type) && (v.metadata = {
2371
2386
  ...(i == null ? void 0 : i.archived) && { archived: !0 },
@@ -2373,18 +2388,18 @@ function Lt(e) {
2373
2388
  }), i != null && i.sessionId && (v.sessionId = i.sessionId), i != null && i.imageUrls && (v.imageUrls = i.imageUrls);
2374
2389
  for await (const R of T.sendMessageStream(
2375
2390
  x,
2376
- f,
2391
+ g,
2377
2392
  v
2378
2393
  )) {
2379
- if (R.progressMessage && n((k) => ({
2394
+ if (R.progressMessage && a((k) => ({
2380
2395
  ...k,
2381
2396
  progressMessage: R.progressMessage || null
2382
2397
  })), !R.final && R.text)
2383
- if (m)
2384
- n((k) => ({
2398
+ if (y)
2399
+ a((k) => ({
2385
2400
  ...k,
2386
2401
  uiMessages: k.uiMessages.map(
2387
- (b) => b.id === m ? {
2402
+ (b) => b.id === y ? {
2388
2403
  ...b,
2389
2404
  content: [
2390
2405
  {
@@ -2396,9 +2411,9 @@ function Lt(e) {
2396
2411
  )
2397
2412
  }));
2398
2413
  else {
2399
- m = `agent-streaming-${Date.now()}`;
2414
+ y = `agent-streaming-${Date.now()}`;
2400
2415
  const k = {
2401
- id: m,
2416
+ id: y,
2402
2417
  role: "agent",
2403
2418
  content: [
2404
2419
  { type: "text", text: R.text }
@@ -2407,10 +2422,10 @@ function Lt(e) {
2407
2422
  archived: !1,
2408
2423
  showIcon: !0,
2409
2424
  icon: "assistant",
2410
- reactKey: m
2425
+ reactKey: y
2411
2426
  // Stable key for React rendering
2412
2427
  };
2413
- n((b) => ({
2428
+ a((b) => ({
2414
2429
  ...b,
2415
2430
  uiMessages: [
2416
2431
  ...b.uiMessages,
@@ -2418,18 +2433,18 @@ function Lt(e) {
2418
2433
  ]
2419
2434
  }));
2420
2435
  }
2421
- if (R.final && ((S = R.status) != null && S.message) && m) {
2436
+ if (R.final && ((S = R.status) != null && S.message) && y) {
2422
2437
  C = !0;
2423
- const k = m, b = re(
2438
+ const k = y, b = re(
2424
2439
  R.status.message,
2425
- d.current
2440
+ u.current
2426
2441
  );
2427
- b && n((E) => {
2442
+ b && a((E) => {
2428
2443
  const U = E.uiMessages.map(
2429
2444
  (q) => {
2430
- var O, j;
2445
+ var _, j;
2431
2446
  if (q.id === k) {
2432
- const H = b.content.length > 0 && ((O = b.content[0]) == null ? void 0 : O.text) && ((j = q.content[0]) == null ? void 0 : j.text) && b.content[0].text.length > q.content[0].text.length;
2447
+ const H = b.content.length > 0 && ((_ = b.content[0]) == null ? void 0 : _.text) && ((j = q.content[0]) == null ? void 0 : j.text) && b.content[0].text.length > q.content[0].text.length;
2433
2448
  return {
2434
2449
  ...b,
2435
2450
  reactKey: q.reactKey || k,
@@ -2449,29 +2464,29 @@ function Lt(e) {
2449
2464
  isProcessing: !1,
2450
2465
  progressMessage: null
2451
2466
  };
2452
- }), m = null;
2467
+ }), y = null;
2453
2468
  }
2454
2469
  }
2455
2470
  if (!C) {
2456
2471
  const R = T.getConversationHistory(x);
2457
- n((k) => {
2472
+ a((k) => {
2458
2473
  let b = k.uiMessages;
2459
- m && (b = k.uiMessages.filter(
2460
- (O) => O.id !== m
2474
+ y && (b = k.uiMessages.filter(
2475
+ (_) => _.id !== y
2461
2476
  ));
2462
2477
  const E = R.map(
2463
- (O) => re(
2464
- O,
2465
- d.current
2478
+ (_) => re(
2479
+ _,
2480
+ u.current
2466
2481
  )
2467
2482
  ).filter(
2468
- (O) => O !== null
2483
+ (_) => _ !== null
2469
2484
  ), U = new Set(
2470
- R.map((O) => O.messageId)
2485
+ R.map((_) => _.messageId)
2471
2486
  ), J = b.filter(
2472
- (O) => {
2487
+ (_) => {
2473
2488
  var j;
2474
- return !U.has(O.id) && ((j = O.content[0]) == null ? void 0 : j.type) === "component";
2489
+ return !U.has(_.id) && ((j = _.content[0]) == null ? void 0 : j.type) === "component";
2475
2490
  }
2476
2491
  ), q = oe([
2477
2492
  ...E,
@@ -2486,9 +2501,9 @@ function Lt(e) {
2486
2501
  };
2487
2502
  });
2488
2503
  }
2489
- } catch (m) {
2490
- if (m instanceof Error && m.name === "AbortError") {
2491
- M("Request was aborted by user"), n((v) => ({
2504
+ } catch (y) {
2505
+ if (y instanceof Error && y.name === "AbortError") {
2506
+ M("Request was aborted by user"), a((v) => ({
2492
2507
  ...v,
2493
2508
  isProcessing: !1,
2494
2509
  progressMessage: null,
@@ -2497,46 +2512,46 @@ function Lt(e) {
2497
2512
  }));
2498
2513
  return;
2499
2514
  }
2500
- const C = m instanceof Error ? m.message : "Failed to send message";
2501
- throw n((v) => ({
2515
+ const C = y instanceof Error ? y.message : "Failed to send message";
2516
+ throw a((v) => ({
2502
2517
  ...v,
2503
2518
  isProcessing: !1,
2504
2519
  progressMessage: null,
2505
2520
  error: C
2506
- })), m;
2521
+ })), y;
2507
2522
  }
2508
2523
  },
2509
- [s.agentId, t]
2510
- ), p = F((f) => {
2511
- n((i) => ({
2524
+ [t.agentId, s]
2525
+ ), m = F((g) => {
2526
+ a((i) => ({
2512
2527
  ...i,
2513
- uiMessages: oe([...i.uiMessages, f])
2528
+ uiMessages: oe([...i.uiMessages, g])
2514
2529
  }));
2515
- }, []), g = F((f) => {
2516
- n((i) => ({
2530
+ }, []), f = F((g) => {
2531
+ a((i) => ({
2517
2532
  ...i,
2518
- suggestions: f
2533
+ suggestions: g
2519
2534
  }));
2520
- }, []), y = F(() => {
2521
- n((f) => ({
2522
- ...f,
2535
+ }, []), I = F(() => {
2536
+ a((g) => ({
2537
+ ...g,
2523
2538
  suggestions: []
2524
2539
  }));
2525
2540
  }, []);
2526
2541
  se(() => {
2527
- n((f) => {
2528
- if (f.clientMessages.length === 0)
2529
- return f;
2530
- const i = u(f.clientMessages), T = new Set(
2531
- f.clientMessages.map((w) => w.messageId)
2532
- ), x = f.uiMessages.filter(
2542
+ a((g) => {
2543
+ if (g.clientMessages.length === 0)
2544
+ return g;
2545
+ const i = d(g.clientMessages), T = new Set(
2546
+ g.clientMessages.map((w) => w.messageId)
2547
+ ), x = g.uiMessages.filter(
2533
2548
  (w) => {
2534
2549
  var P;
2535
2550
  return !T.has(w.id) && ((P = w.content[0]) == null ? void 0 : P.type) === "component";
2536
2551
  }
2537
2552
  );
2538
2553
  return {
2539
- ...f,
2554
+ ...g,
2540
2555
  uiMessages: oe([
2541
2556
  ...i,
2542
2557
  ...x
@@ -2544,86 +2559,86 @@ function Lt(e) {
2544
2559
  };
2545
2560
  });
2546
2561
  }, [l]);
2547
- const I = F(() => {
2548
- if (!t)
2562
+ const h = F(() => {
2563
+ if (!s)
2549
2564
  return;
2550
- const f = Q(), i = s.agentId;
2551
- f.abortCurrentRequest(i);
2552
- }, [s.agentId, t]), A = F(
2553
- async (f) => {
2554
- if (!t)
2565
+ const g = Q(), i = t.agentId;
2566
+ g.abortCurrentRequest(i);
2567
+ }, [t.agentId, s]), A = F(
2568
+ async (g) => {
2569
+ if (!s)
2555
2570
  return;
2556
- const i = Q(), T = s.agentId;
2557
- await i.replaceMessages(T, f);
2558
- const x = u(f);
2559
- n((w) => ({
2571
+ const i = Q(), T = t.agentId;
2572
+ await i.replaceMessages(T, g);
2573
+ const x = d(g);
2574
+ a((w) => ({
2560
2575
  ...w,
2561
- clientMessages: f,
2576
+ clientMessages: g,
2562
2577
  uiMessages: x
2563
2578
  }));
2564
2579
  },
2565
- [s.agentId, t]
2580
+ [t.agentId, s]
2566
2581
  );
2567
2582
  return {
2568
2583
  // AgentUI props
2569
- messages: a.uiMessages,
2570
- isProcessing: a.isProcessing,
2571
- error: a.error,
2572
- onSubmit: h,
2573
- suggestions: a.suggestions,
2574
- progressMessage: a.progressMessage,
2584
+ messages: n.uiMessages,
2585
+ isProcessing: n.isProcessing,
2586
+ error: n.error,
2587
+ onSubmit: p,
2588
+ suggestions: n.suggestions,
2589
+ progressMessage: n.progressMessage,
2575
2590
  // UI management methods
2576
- registerSuggestions: g,
2577
- clearSuggestions: y,
2591
+ registerSuggestions: f,
2592
+ clearSuggestions: I,
2578
2593
  // Message actions methods
2579
2594
  registerMessageActions: r,
2580
2595
  unregisterMessageActions: o,
2581
2596
  clearAllMessageActions: c,
2582
2597
  // Tool integration
2583
- addMessage: p,
2598
+ addMessage: m,
2584
2599
  // Abort control
2585
- abortCurrentRequest: I,
2600
+ abortCurrentRequest: h,
2586
2601
  // Conversation loading
2587
2602
  loadMessages: A
2588
2603
  };
2589
2604
  }
2590
- var bt = /* @__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))(bt || {});
2591
- const Te = "jetpack-ai-jwt-token", kt = 30 * 60 * 1e3;
2592
- function Rt() {
2593
- var t, a, n;
2594
- 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);
2595
- }
2596
- async function Et(e, s = !0) {
2597
- var l, d;
2598
- const t = localStorage.getItem(Te);
2599
- let a;
2600
- if (t)
2605
+ 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 || {});
2606
+ const Te = "jetpack-ai-jwt-token", Rt = 30 * 60 * 1e3;
2607
+ function Et() {
2608
+ var s, n, a;
2609
+ return ((n = (s = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : s.connectionStatus) == null ? void 0 : n.isRegistered) ? !1 : !!((a = window.Jetpack_Editor_Initial_State) != null && a.wpcomBlogId);
2610
+ }
2611
+ async function Pt(e, t = !0) {
2612
+ var l, u;
2613
+ const s = localStorage.getItem(Te);
2614
+ let n;
2615
+ if (s)
2601
2616
  try {
2602
- a = JSON.parse(t);
2603
- } catch (u) {
2604
- M("Invalid cached Jetpack token: %O", u);
2617
+ n = JSON.parse(s);
2618
+ } catch (d) {
2619
+ M("Invalid cached Jetpack token: %O", d);
2605
2620
  }
2606
- if (a && (a != null && a.token) && (a != null && a.expire) && (a == null ? void 0 : a.expire) > Date.now() && s)
2607
- return a;
2608
- 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;
2621
+ if (n && (n != null && n.token) && (n != null && n.expire) && (n == null ? void 0 : n.expire) > Date.now() && t)
2622
+ return n;
2623
+ const a = (l = window.JP_CONNECTION_INITIAL_STATE) == null ? void 0 : l.apiNonce, r = (u = window.Jetpack_Editor_Initial_State) == null ? void 0 : u.wpcomBlogId;
2609
2624
  let o = {
2610
2625
  token: "",
2611
2626
  blog_id: ""
2612
2627
  };
2613
2628
  try {
2614
- Rt() ? o = await pe({
2629
+ Et() ? o = await pe({
2615
2630
  path: "/wpcom/v2/sites/" + r + "/jetpack-openai-query/jwt",
2616
2631
  method: "POST"
2617
2632
  }) : o = await pe({
2618
2633
  path: "/jetpack/v4/jetpack-ai-jwt?_cacheBuster=" + Date.now(),
2619
2634
  credentials: "same-origin",
2620
2635
  headers: {
2621
- "X-WP-Nonce": n || ""
2636
+ "X-WP-Nonce": a || ""
2622
2637
  },
2623
2638
  method: "POST"
2624
2639
  });
2625
- } catch (u) {
2626
- throw M("Failed to fetch Jetpack token: %O", u), new Error(e(u));
2640
+ } catch (d) {
2641
+ throw M("Failed to fetch Jetpack token: %O", d), new Error(e(d));
2627
2642
  }
2628
2643
  if (!(o != null && o.token))
2629
2644
  throw new Error(
@@ -2632,27 +2647,27 @@ async function Et(e, s = !0) {
2632
2647
  const c = {
2633
2648
  token: o.token,
2634
2649
  blogId: o.blog_id || "",
2635
- expire: Date.now() + kt
2650
+ expire: Date.now() + Rt
2636
2651
  };
2637
2652
  try {
2638
2653
  localStorage.setItem(Te, JSON.stringify(c));
2639
- } catch (u) {
2640
- M("Error storing token in localStorage: %O", u);
2654
+ } catch (d) {
2655
+ M("Error storing token in localStorage: %O", d);
2641
2656
  }
2642
2657
  return c;
2643
2658
  }
2644
- const Bt = (e) => async () => {
2645
- const s = {};
2659
+ const Wt = (e) => async () => {
2660
+ const t = {};
2646
2661
  try {
2647
- const t = await Et(e);
2648
- t != null && t.token && (s.Authorization = `${t.token}`);
2649
- } catch (t) {
2650
- throw M("Failed to get Jetpack token for auth: %O", t), t;
2662
+ const s = await Pt(e);
2663
+ s != null && s.token && (t.Authorization = `${s.token}`);
2664
+ } catch (s) {
2665
+ throw M("Failed to get Jetpack token for auth: %O", s), s;
2651
2666
  }
2652
- return s;
2667
+ return t;
2653
2668
  };
2654
- function Pt(e) {
2655
- const s = {
2669
+ function _t(e) {
2670
+ const t = {
2656
2671
  type: "object",
2657
2672
  properties: {}
2658
2673
  };
@@ -2660,73 +2675,73 @@ function Pt(e) {
2660
2675
  id: e.name.replace(/\//g, "-"),
2661
2676
  name: e.label,
2662
2677
  description: e.description,
2663
- input_schema: e.input_schema || s,
2678
+ input_schema: e.input_schema || t,
2664
2679
  // Store metadata for detection and execution
2665
2680
  _source: "wordpress-ability",
2666
2681
  _originalAbility: e
2667
2682
  };
2668
2683
  }
2669
- function Wt(e) {
2670
- return e.map(Pt);
2671
- }
2672
2684
  function Vt(e) {
2685
+ return e.map(_t);
2686
+ }
2687
+ function zt(e) {
2673
2688
  return (e == null ? void 0 : e._source) === "wordpress-ability";
2674
2689
  }
2675
- function zt(e, s = "agent", t = "wpcom") {
2676
- const a = e.replace(/-/g, "_");
2677
- return `${t}-${s}-${a}`;
2690
+ function Gt(e, t = "agent", s = "wpcom") {
2691
+ const n = e.replace(/-/g, "_");
2692
+ return `${s}-${t}-${n}`;
2678
2693
  }
2679
2694
  function Ot(e) {
2680
- const s = e.split("-");
2681
- if (s.length < 3)
2695
+ const t = e.split("-");
2696
+ if (t.length < 3)
2682
2697
  throw new Error(
2683
2698
  `Invalid Odie bot ID format: ${e}. Expected format: {product}-{type}-{slug}`
2684
2699
  );
2685
- const t = s[0], a = s[1];
2686
- if (!["agent", "workflow", "chain"].includes(a))
2700
+ const s = t[0], n = t[1];
2701
+ if (!["agent", "workflow", "chain"].includes(n))
2687
2702
  throw new Error(
2688
- `Invalid Odie bot type: ${a}. Expected one of: agent, workflow, chain`
2703
+ `Invalid Odie bot type: ${n}. Expected one of: agent, workflow, chain`
2689
2704
  );
2690
- const n = a, r = s.slice(2).join("-"), o = r.replace(/_/g, "-");
2705
+ const a = n, r = t.slice(2).join("-"), o = r.replace(/_/g, "-");
2691
2706
  return {
2692
- product: t,
2693
- type: n,
2707
+ product: s,
2708
+ type: a,
2694
2709
  slug: r,
2695
2710
  agentId: o
2696
2711
  };
2697
2712
  }
2698
- function Gt(e) {
2713
+ function Qt(e) {
2699
2714
  try {
2700
- const s = Ot(e);
2701
- return s.product.length > 0 && ["agent", "workflow", "chain"].includes(s.type) && s.slug.length > 0;
2715
+ const t = Ot(e);
2716
+ return t.product.length > 0 && ["agent", "workflow", "chain"].includes(t.type) && t.slug.length > 0;
2702
2717
  } catch {
2703
2718
  return !1;
2704
2719
  }
2705
2720
  }
2706
2721
  export {
2707
- bt as ErrorCodes,
2708
- Wt as convertAbilitiesToTools,
2709
- Pt as convertAbilityToTool,
2710
- Dt as createAbortController,
2722
+ kt as ErrorCodes,
2723
+ Vt as convertAbilitiesToTools,
2724
+ _t as convertAbilityToTool,
2725
+ Jt as createAbortController,
2711
2726
  rt as createClient,
2712
- Bt as createJetpackAuthProvider,
2713
- zt as createOdieBotId,
2727
+ Wt as createJetpackAuthProvider,
2728
+ Gt as createOdieBotId,
2714
2729
  Fe as createRequestId,
2715
2730
  je as createTaskId,
2716
2731
  ie as createTextMessage,
2717
2732
  D as extractTextFromMessage,
2718
2733
  L as extractToolCallsFromMessage,
2719
2734
  Q as getAgentManager,
2720
- Gt as isOdieBotId,
2721
- Vt as isWordPressAbility,
2722
- Jt as listConversationsFromServer,
2723
- Kt as loadAllMessagesFromServer,
2735
+ Qt as isOdieBotId,
2736
+ zt as isWordPressAbility,
2737
+ Kt as listConversationsFromServer,
2738
+ Lt as loadAllMessagesFromServer,
2724
2739
  le as loadChatFromServer,
2725
2740
  Ot as parseOdieBotId,
2726
- Lt as useAgentChat,
2727
- Ft as useClientAbilities,
2728
- qt as useClientContext,
2729
- Ht as useClientTools,
2730
- jt as useClientToolsWithAbilities,
2731
- vt as useMessageActions
2741
+ Bt as useAgentChat,
2742
+ jt as useClientAbilities,
2743
+ Ht as useClientContext,
2744
+ Ft as useClientTools,
2745
+ Dt as useClientToolsWithAbilities,
2746
+ Mt as useMessageActions
2732
2747
  };