@automattic/agenttic-client 0.1.26 → 0.1.28

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