@automattic/agenttic-client 0.1.54 → 0.1.56

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