@automattic/agenttic-client 0.1.15 → 0.1.17

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