@automattic/agenttic-client 0.1.55 → 0.1.57

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