@ghuri/ai-widget 0.3.0 → 0.3.2

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.
@@ -10,18 +10,51 @@ function e(e) {
10
10
  return e;
11
11
  }
12
12
  }
13
+ var t = [
14
+ "image/jpeg",
15
+ "image/png",
16
+ "image/webp"
17
+ ];
18
+ function n(e) {
19
+ return !e || typeof e != "object" || typeof e.data != "string" || !t.includes(e.mediaType) || !/^[A-Za-z0-9+/=]+$/.test(e.data) ? null : {
20
+ mediaType: e.mediaType,
21
+ data: e.data,
22
+ ...Number.isFinite(e.width) ? { width: e.width } : {},
23
+ ...Number.isFinite(e.height) ? { height: e.height } : {}
24
+ };
25
+ }
26
+ function r(t) {
27
+ let r = t;
28
+ return r && typeof r == "object" && r.ghuriToolImages === 1 && Array.isArray(r.images) ? {
29
+ output: e(r.output),
30
+ images: r.images.map(n).filter(Boolean)
31
+ } : {
32
+ output: e(t),
33
+ images: []
34
+ };
35
+ }
36
+ function i(t) {
37
+ let r = Array.isArray(t?.value) ? t.value : [], i = r.filter((e) => e?.type === "text" && typeof e.text == "string").map((e) => e.text).join("\n"), a = r.filter((e) => e?.type === "file" && e.data?.type === "data").map((e) => n({
38
+ mediaType: e.mediaType,
39
+ data: e.data.data
40
+ })).filter(Boolean);
41
+ return {
42
+ output: e(i),
43
+ images: a
44
+ };
45
+ }
13
46
  // src/core/history.ts
14
- function t(e) {
47
+ function a(e) {
15
48
  let t = e.providerOptions?.ghuri?.attachments;
16
49
  return Array.isArray(t) ? t.filter((e) => !!e && typeof e == "object" && typeof e.ref == "string" && typeof e.name == "string") : null;
17
50
  }
18
- function n(e) {
51
+ function o(e) {
19
52
  return typeof e.providerOptions?.ghuri?.note == "string";
20
53
  }
21
- function r(e) {
22
- return typeof e == "string" ? e : (e ?? []).filter((e) => e.type === "text" && typeof e.text == "string" && !t(e) && !n(e)).map((e) => e.text).join("");
54
+ function s(e) {
55
+ return typeof e == "string" ? e : (e ?? []).filter((e) => e.type === "text" && typeof e.text == "string" && !a(e) && !o(e)).map((e) => e.text).join("");
23
56
  }
24
- function i(t) {
57
+ function c(t) {
25
58
  switch (t?.type) {
26
59
  case "execution-denied": return {
27
60
  state: "denied",
@@ -36,78 +69,86 @@ function i(t) {
36
69
  state: "done",
37
70
  output: t
38
71
  };
72
+ case "content": {
73
+ let e = i(t);
74
+ return {
75
+ state: "done",
76
+ output: e.output,
77
+ ...e.images.length ? { images: e.images } : {}
78
+ };
79
+ }
39
80
  default: return {
40
81
  state: "done",
41
82
  output: e(t.value)
42
83
  };
43
84
  }
44
85
  }
45
- function a(e, n = [], a = /* @__PURE__ */ new Map()) {
46
- let o = [], s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map();
47
- for (let e of n) e.title && a.set(e.toolName, e.title);
86
+ function l(e, t = [], n = /* @__PURE__ */ new Map()) {
87
+ let r = [], i = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map();
88
+ for (let e of t) e.title && n.set(e.toolName, e.title);
48
89
  let l = null, u = 0, d = () => (l || (l = {
49
90
  id: `h${u++}`,
50
91
  role: "assistant",
51
92
  parts: []
52
- }, o.push(l)), l);
53
- for (let n of e) if (n.role === "user") {
93
+ }, r.push(l)), l);
94
+ for (let t of e) if (t.role === "user") {
54
95
  l = null;
55
- let e = Array.isArray(n.content) ? n.content.flatMap((e) => t(e) ?? []) : [], i = r(n.content), a = [];
56
- e.length && a.push({
96
+ let e = Array.isArray(t.content) ? t.content.flatMap((e) => a(e) ?? []) : [], n = s(t.content), i = [];
97
+ e.length && i.push({
57
98
  kind: "attachments",
58
99
  id: `h${u++}`,
59
100
  attachments: e
60
- }), (i || !e.length) && a.push({
101
+ }), (n || !e.length) && i.push({
61
102
  kind: "text",
62
103
  id: `h${u++}`,
63
- text: i
64
- }), o.push({
104
+ text: n
105
+ }), r.push({
65
106
  id: `h${u++}`,
66
107
  role: "user",
67
- parts: a
108
+ parts: i
68
109
  });
69
- } else if (n.role === "assistant") {
70
- let e = d(), t = typeof n.content == "string" ? [{
110
+ } else if (t.role === "assistant") {
111
+ let e = d(), r = typeof t.content == "string" ? [{
71
112
  type: "text",
72
- text: n.content
73
- }] : n.content ?? [];
74
- for (let n of t) if (n.type === "text" && typeof n.text == "string" && n.text) {
75
- let t = e.parts.at(-1);
76
- t?.kind === "text" ? t.text += n.text : e.parts.push({
113
+ text: t.content
114
+ }] : t.content ?? [];
115
+ for (let t of r) if (t.type === "text" && typeof t.text == "string" && t.text) {
116
+ let n = e.parts.at(-1);
117
+ n?.kind === "text" ? n.text += t.text : e.parts.push({
77
118
  kind: "text",
78
119
  id: `h${u++}`,
79
- text: n.text
120
+ text: t.text
80
121
  });
81
- } else if (n.type === "tool-call" && typeof n.toolCallId == "string") {
82
- let t = a.get(n.toolName), r = {
122
+ } else if (t.type === "tool-call" && typeof t.toolCallId == "string") {
123
+ let r = n.get(t.toolName), a = {
83
124
  kind: "tool",
84
- toolCallId: n.toolCallId,
85
- toolName: String(n.toolName ?? ""),
86
- ...t ? { title: t } : {},
87
- input: n.input,
125
+ toolCallId: t.toolCallId,
126
+ toolName: String(t.toolName ?? ""),
127
+ ...r ? { title: r } : {},
128
+ input: t.input,
88
129
  state: "running"
89
130
  };
90
- s.set(r.toolCallId, r), e.parts.push(r);
91
- } else if (n.type === "tool-approval-request" && typeof n.toolCallId == "string") {
92
- let e = s.get(n.toolCallId);
93
- e && (e.state = "awaiting-approval", e.approvalId = n.approvalId, c.set(n.approvalId, e));
131
+ i.set(a.toolCallId, a), e.parts.push(a);
132
+ } else if (t.type === "tool-approval-request" && typeof t.toolCallId == "string") {
133
+ let e = i.get(t.toolCallId);
134
+ e && (e.state = "awaiting-approval", e.approvalId = t.approvalId, o.set(t.approvalId, e));
94
135
  }
95
- } else if (n.role === "tool" && Array.isArray(n.content)) {
96
- for (let e of n.content) if (e.type === "tool-approval-response") {
97
- let t = c.get(e.approvalId);
136
+ } else if (t.role === "tool" && Array.isArray(t.content)) {
137
+ for (let e of t.content) if (e.type === "tool-approval-response") {
138
+ let t = o.get(e.approvalId);
98
139
  t && t.state === "awaiting-approval" && (t.state = e.approved ? "running" : "denied");
99
140
  } else if (e.type === "tool-result") {
100
- let t = s.get(e.toolCallId);
101
- t && Object.assign(t, i(e.output));
141
+ let t = i.get(e.toolCallId);
142
+ t && Object.assign(t, c(e.output));
102
143
  }
103
144
  }
104
- let f = new Set(n.map((e) => e.toolCallId));
105
- for (let e of s.values()) e.state === "awaiting-approval" && !f.has(e.toolCallId) && (e.state = "denied");
106
- return o.filter((e) => e.parts.length > 0);
145
+ let f = new Set(t.map((e) => e.toolCallId));
146
+ for (let e of i.values()) e.state === "awaiting-approval" && !f.has(e.toolCallId) && (e.state = "denied");
147
+ return r.filter((e) => e.parts.length > 0);
107
148
  }
108
149
  // src/core/prepare.ts
109
- var o = .85;
110
- function s(e, t, n) {
150
+ var u = .85;
151
+ function d(e, t, n) {
111
152
  let r = Math.max(e, t);
112
153
  if (!r || r <= n) return {
113
154
  width: e,
@@ -119,7 +160,7 @@ function s(e, t, n) {
119
160
  height: Math.max(1, Math.round(t * i))
120
161
  };
121
162
  }
122
- async function c(e, t) {
163
+ async function f(e, t) {
123
164
  let { contentType: n } = t;
124
165
  if (n === "image/gif" || typeof createImageBitmap != "function") return {
125
166
  blob: e,
@@ -135,7 +176,7 @@ async function c(e, t) {
135
176
  };
136
177
  }
137
178
  try {
138
- let i = s(r.width, r.height, t.maxEdge), a = await l(r, i, n);
179
+ let i = d(r.width, r.height, t.maxEdge), a = await p(r, i, n);
139
180
  return a ? {
140
181
  blob: a,
141
182
  contentType: a.type || n,
@@ -150,26 +191,26 @@ async function c(e, t) {
150
191
  r.close();
151
192
  }
152
193
  }
153
- async function l(e, t, n) {
194
+ async function p(e, t, n) {
154
195
  if (typeof OffscreenCanvas == "function") {
155
196
  let r = new OffscreenCanvas(t.width, t.height), i = r.getContext("2d");
156
197
  return i ? (i.drawImage(e, 0, 0, t.width, t.height), r.convertToBlob({
157
198
  type: n,
158
- quality: o
199
+ quality: u
159
200
  }).catch(() => null)) : null;
160
201
  }
161
202
  if (typeof document > "u") return null;
162
203
  let r = document.createElement("canvas");
163
204
  r.width = t.width, r.height = t.height;
164
205
  let i = r.getContext("2d");
165
- return i ? (i.drawImage(e, 0, 0, t.width, t.height), new Promise((e) => r.toBlob((t) => e(t), n, o))) : null;
206
+ return i ? (i.drawImage(e, 0, 0, t.width, t.height), new Promise((e) => r.toBlob((t) => e(t), n, u))) : null;
166
207
  }
167
208
  // src/core/sse.ts
168
- function u(e) {
169
- let t = new d(), n = t.push(e);
209
+ function m(e) {
210
+ let t = new h(), n = t.push(e);
170
211
  return n.push(...t.end()), n;
171
212
  }
172
- var d = class {
213
+ var h = class {
173
214
  buffer = "";
174
215
  carry = "";
175
216
  done = !1;
@@ -206,8 +247,8 @@ var d = class {
206
247
  }
207
248
  }
208
249
  };
209
- async function* f(e, t) {
210
- let n = new d(), r = new TextDecoder(), i = e.getReader(), a = () => void i.cancel().catch(() => {});
250
+ async function* g(e, t) {
251
+ let n = new h(), r = new TextDecoder(), i = e.getReader(), a = () => void i.cancel().catch(() => {});
211
252
  t?.addEventListener("abort", a, { once: !0 });
212
253
  try {
213
254
  for (;;) {
@@ -223,7 +264,7 @@ async function* f(e, t) {
223
264
  }
224
265
  }
225
266
  // src/core/client.ts
226
- var p = 3e4, m = {
267
+ var _ = 3e4, v = {
227
268
  accept: [
228
269
  "image/jpeg",
229
270
  "image/png",
@@ -234,7 +275,7 @@ var p = 3e4, m = {
234
275
  maxBytes: 5242880,
235
276
  maxEdge: 2048
236
277
  };
237
- function h(e = !1) {
278
+ function y(e = !1) {
238
279
  return {
239
280
  status: "idle",
240
281
  conversationId: null,
@@ -247,19 +288,19 @@ function h(e = !1) {
247
288
  allowances: []
248
289
  };
249
290
  }
250
- function g(e) {
291
+ function b(e) {
251
292
  try {
252
293
  return typeof URL.createObjectURL == "function" ? URL.createObjectURL(e) : void 0;
253
294
  } catch {
254
295
  return;
255
296
  }
256
297
  }
257
- function _(e) {
298
+ function x(e) {
258
299
  try {
259
300
  e && typeof URL.revokeObjectURL == "function" && URL.revokeObjectURL(e);
260
301
  } catch {}
261
302
  }
262
- var v = class extends Error {
303
+ var S = class extends Error {
263
304
  status;
264
305
  code;
265
306
  detail;
@@ -269,16 +310,16 @@ var v = class extends Error {
269
310
  super(n || `HTTP ${e}`), this.status = e, this.code = t, this.detail = n, this.retryAfter = r, this.fields = i;
270
311
  }
271
312
  };
272
- async function y(e) {
313
+ async function C(e) {
273
314
  let t = null;
274
315
  try {
275
316
  t = await e.json();
276
317
  } catch {}
277
318
  let n = Number(e.headers.get("retry-after"));
278
- return new v(e.status, typeof t?.code == "string" ? t.code : "", typeof t?.detail == "string" ? t.detail : "", Number.isFinite(n) && n > 0 ? n : void 0, t && typeof t == "object" && !Array.isArray(t) ? t : {});
319
+ return new S(e.status, typeof t?.code == "string" ? t.code : "", typeof t?.detail == "string" ? t.detail : "", Number.isFinite(n) && n > 0 ? n : void 0, t && typeof t == "object" && !Array.isArray(t) ? t : {});
279
320
  }
280
- function b(e) {
281
- if (!(e instanceof v)) return { code: "unavailable" };
321
+ function w(e) {
322
+ if (!(e instanceof S)) return { code: "unavailable" };
282
323
  let { status: t, code: n, detail: r } = e, i = e.fields.attachments;
283
324
  return t === 400 && Array.isArray(i) ? {
284
325
  code: "photos-refused",
@@ -291,14 +332,14 @@ function b(e) {
291
332
  ...r ? { message: r } : {}
292
333
  };
293
334
  }
294
- function x(e) {
335
+ function T(e) {
295
336
  return e instanceof DOMException ? e.name === "AbortError" : e?.name === "AbortError";
296
337
  }
297
- function S(e, t) {
338
+ function E(e, t) {
298
339
  let n = new Date(e.expiresAt).getTime();
299
- return Number.isNaN(n) || n - p > t;
340
+ return Number.isNaN(n) || n - _ > t;
300
341
  }
301
- function C(e) {
342
+ function D(e) {
302
343
  return {
303
344
  ...e,
304
345
  messages: e.messages.map((e) => ({
@@ -310,7 +351,7 @@ function C(e) {
310
351
  allowances: [...e.allowances]
311
352
  };
312
353
  }
313
- var w = class {
354
+ var O = class {
314
355
  opts;
315
356
  state;
316
357
  view;
@@ -328,11 +369,12 @@ var w = class {
328
369
  previews = /* @__PURE__ */ new Set();
329
370
  policy = null;
330
371
  loadingPolicy = null;
372
+ capabilitiesWanted = !1;
331
373
  constructor(e) {
332
374
  this.opts = {
333
375
  ...e,
334
376
  apiUrl: e.apiUrl.replace(/\/+$/, "")
335
- }, this.state = h(!1), this.view = C(this.state);
377
+ }, this.state = y(!1), this.view = D(this.state);
336
378
  }
337
379
  getState() {
338
380
  return this.view;
@@ -348,7 +390,7 @@ var w = class {
348
390
  for (let n of this.events.get(e) ?? []) n(t);
349
391
  }
350
392
  changed(e = {}) {
351
- Object.assign(this.state, e), this.view = C(this.state);
393
+ Object.assign(this.state, e), this.view = D(this.state);
352
394
  for (let e of this.listeners) e(this.view);
353
395
  }
354
396
  id(e) {
@@ -359,7 +401,7 @@ var w = class {
359
401
  }
360
402
  async ensureSession() {
361
403
  let e = this.session;
362
- return e && S(e, this.now()) ? e : (this.session = null, this.starting ||= this.opts.startSession().then((e) => (this.session = e, e)).finally(() => {
404
+ return e && E(e, this.now()) ? e : (this.session = null, this.starting ||= this.opts.startSession().then((e) => (this.session = e, e)).finally(() => {
363
405
  this.starting = null;
364
406
  }), this.starting);
365
407
  }
@@ -371,7 +413,7 @@ var w = class {
371
413
  headers: i
372
414
  });
373
415
  if (a.status !== 401 || n) return a;
374
- let o = await y(a);
416
+ let o = await C(a);
375
417
  if (this.changed({ problem: { code: "reconnecting" } }), o.code === "user_token_expired") try {
376
418
  await this.opts.refreshUserToken(r.sessionId);
377
419
  } catch {
@@ -383,7 +425,7 @@ var w = class {
383
425
  }
384
426
  async json(e, t = {}) {
385
427
  let n = await this.request(e, t);
386
- if (!n.ok) throw await y(n);
428
+ if (!n.ok) throw await C(n);
387
429
  return await n.json();
388
430
  }
389
431
  async send(e) {
@@ -414,7 +456,7 @@ var w = class {
414
456
  role: "user",
415
457
  parts: i
416
458
  });
417
- for (let e of this.state.uploads) e.status !== "ready" && _(e.previewUrl);
459
+ for (let e of this.state.uploads) e.status !== "ready" && x(e.previewUrl);
418
460
  this.files = /* @__PURE__ */ new Map(), this.changed({
419
461
  pendingApprovals: [],
420
462
  problem: null,
@@ -429,12 +471,12 @@ var w = class {
429
471
  });
430
472
  }
431
473
  attachmentLimits() {
432
- let e = this.opts.attachments ?? {}, t = e.accept?.length ? e.accept : [...m.accept], n = this.policy?.accept?.length ? this.policy.accept : null;
474
+ let e = this.opts.attachments ?? {}, t = e.accept?.length ? e.accept : [...v.accept], n = this.policy?.accept?.length ? this.policy.accept : null;
433
475
  return {
434
476
  accept: n ? t.filter((e) => n.includes(e)) : t,
435
- maxCount: Math.min(e.maxCount ?? m.maxCount, this.policy?.maxCount ?? m.maxCount, m.maxCount),
436
- maxBytes: e.maxBytes ?? m.maxBytes,
437
- maxEdge: e.maxEdge ?? m.maxEdge
477
+ maxCount: Math.min(e.maxCount ?? v.maxCount, this.policy?.maxCount ?? v.maxCount, v.maxCount),
478
+ maxBytes: e.maxBytes ?? v.maxBytes,
479
+ maxEdge: e.maxEdge ?? v.maxEdge
438
480
  };
439
481
  }
440
482
  retryable() {
@@ -444,7 +486,7 @@ var w = class {
444
486
  return !!this.opts.uploadFile && this.policy?.enabled === !0;
445
487
  }
446
488
  loadCapabilities() {
447
- return !this.opts.uploadFile || this.policy ? Promise.resolve() : (this.loadingPolicy ||= (async () => {
489
+ return !this.opts.uploadFile || (this.capabilitiesWanted = !0, this.policy) ? Promise.resolve() : (this.loadingPolicy ||= (async () => {
448
490
  try {
449
491
  let e = (await this.json("/v1/capabilities"))?.attachments ?? {};
450
492
  this.policy = {
@@ -453,7 +495,7 @@ var w = class {
453
495
  ...Number.isInteger(e.maxCount) ? { maxCount: e.maxCount } : {}
454
496
  };
455
497
  } catch (e) {
456
- e instanceof v && e.status === 404 && (this.policy = { enabled: !1 });
498
+ e instanceof S && e.status === 404 && (this.policy = { enabled: !1 });
457
499
  } finally {
458
500
  this.loadingPolicy = null;
459
501
  }
@@ -485,7 +527,7 @@ var w = class {
485
527
  size: r.size,
486
528
  status: "preparing",
487
529
  progress: 0
488
- }, o = t.includes(i) ? g(r) : void 0;
530
+ }, o = t.includes(i) ? b(r) : void 0;
489
531
  o && (a.previewUrl = o, this.previews.add(o)), this.state.uploads.push(a);
490
532
  let s = this.state.uploads.filter((e) => e.status !== "failed" || e.error === "upload").length;
491
533
  t.includes(i) ? s > n ? this.refuse(a, "count") : (this.files.set(a.id, { file: r }), this.startUpload(a.id)) : this.refuse(a, "type");
@@ -494,7 +536,7 @@ var w = class {
494
536
  }
495
537
  removeUpload(e) {
496
538
  let t = this.upload(e);
497
- t && (this.files.get(e)?.controller?.abort(), this.files.delete(e), _(t.previewUrl), t.previewUrl && this.previews.delete(t.previewUrl), this.changed({ uploads: this.state.uploads.filter((t) => t.id !== e) }));
539
+ t && (t.attachment && this.discard(t.attachment), this.files.get(e)?.controller?.abort(), this.files.delete(e), x(t.previewUrl), t.previewUrl && this.previews.delete(t.previewUrl), this.changed({ uploads: this.state.uploads.filter((t) => t.id !== e) }));
498
540
  }
499
541
  retryUpload(e) {
500
542
  let t = this.upload(e);
@@ -510,12 +552,12 @@ var w = class {
510
552
  error: void 0,
511
553
  message: void 0
512
554
  }), this.changed();
513
- let { maxBytes: s, maxEdge: l } = this.attachmentLimits();
555
+ let { maxBytes: s, maxEdge: c } = this.attachmentLimits();
514
556
  try {
515
- let e = this.opts.prepareImage ?? c, i;
557
+ let e = this.opts.prepareImage ?? f, i;
516
558
  try {
517
559
  i = await e(t.file, {
518
- maxEdge: l,
560
+ maxEdge: c,
519
561
  contentType: n.contentType
520
562
  });
521
563
  } catch {
@@ -533,7 +575,7 @@ var w = class {
533
575
  status: "uploading",
534
576
  size: i.blob.size
535
577
  }), this.changed();
536
- let u = await r(i.blob, {
578
+ let l = await r(i.blob, {
537
579
  name: n.name,
538
580
  contentType: i.contentType,
539
581
  size: i.blob.size,
@@ -544,28 +586,38 @@ var w = class {
544
586
  a() && n.status === "uploading" && (n.progress = Math.max(0, Math.min(1, Number(e) || 0)), this.changed());
545
587
  }
546
588
  });
547
- if (!a() || o.signal.aborted) return;
548
- if (!u || typeof u.ref != "string" || !u.ref) throw Error("The upload gave no reference.");
589
+ if (!a() || o.signal.aborted) {
590
+ l && typeof l.ref == "string" && l.ref && this.discard(l);
591
+ return;
592
+ }
593
+ if (!l || typeof l.ref != "string" || !l.ref) throw Error("The upload gave no reference.");
549
594
  Object.assign(n, {
550
595
  status: "ready",
551
596
  progress: 1,
552
597
  attachment: {
553
- ...u,
554
- name: u.name || n.name,
555
- contentType: u.contentType || i.contentType,
556
- ...i.width && !u.width ? { width: i.width } : {},
557
- ...i.height && !u.height ? { height: i.height } : {}
598
+ ...l,
599
+ name: l.name || n.name,
600
+ contentType: l.contentType || i.contentType,
601
+ ...i.width && !l.width ? { width: i.width } : {},
602
+ ...i.height && !l.height ? { height: i.height } : {}
558
603
  }
559
604
  }), this.changed();
560
605
  } catch (e) {
561
- if (!a() || x(e) || o.signal.aborted) return;
606
+ if (!a() || T(e) || o.signal.aborted) return;
562
607
  this.refuse(n, "upload", e instanceof Error && e.message ? e.message : void 0), this.changed();
563
608
  }
564
609
  }
610
+ discard(e) {
611
+ let t = this.opts.discardFile;
612
+ if (t) try {
613
+ Promise.resolve(t(e)).catch(() => {});
614
+ } catch {}
615
+ }
565
616
  dropPhotos() {
617
+ for (let e of this.state.uploads) e.attachment && this.discard(e.attachment);
566
618
  for (let e of this.files.values()) e.controller?.abort();
567
619
  this.files = /* @__PURE__ */ new Map();
568
- for (let e of this.previews) _(e);
620
+ for (let e of this.previews) x(e);
569
621
  this.previews = /* @__PURE__ */ new Set();
570
622
  }
571
623
  async decide(e) {
@@ -597,7 +649,7 @@ var w = class {
597
649
  let n = await this.json(`/v1/conversations/${encodeURIComponent(t)}/allowances/${encodeURIComponent(e)}`, { method: "DELETE" });
598
650
  return this.changed({ allowances: n.allowances ?? [] }), !0;
599
651
  } catch (t) {
600
- let n = b(t);
652
+ let n = w(t);
601
653
  return this.changed({
602
654
  allowances: this.state.allowances.filter((t) => t.toolName !== e),
603
655
  ...t?.status === 404 ? {} : { problem: n }
@@ -628,7 +680,10 @@ var w = class {
628
680
  this.changed({ problem: null });
629
681
  }
630
682
  newConversation() {
631
- this.generation += 1, this.controller?.abort(), this.dropPhotos(), this.tools = /* @__PURE__ */ new Map(), this.retryAction = null, this.state = h(this.newConversationTakesPhotos()), this.changed(), this.loadCapabilities();
683
+ this.reset(), this.capabilitiesWanted && this.loadCapabilities();
684
+ }
685
+ reset() {
686
+ this.generation += 1, this.controller?.abort(), this.dropPhotos(), this.tools = /* @__PURE__ */ new Map(), this.retryAction = null, this.state = y(this.newConversationTakesPhotos()), this.changed();
632
687
  }
633
688
  async listConversations(e = {}) {
634
689
  let t = new URLSearchParams({
@@ -650,7 +705,7 @@ var w = class {
650
705
  for (let [e, t] of Object.entries(n.toolTitles ?? {})) typeof t == "string" && t && this.titles.set(e, t);
651
706
  let r = n.pendingApprovals ?? [];
652
707
  for (let e of r) e.title && this.titles.set(e.toolName, e.title);
653
- let i = a(n.messages ?? [], r, this.titles);
708
+ let i = l(n.messages ?? [], r, this.titles);
654
709
  this.tools = /* @__PURE__ */ new Map();
655
710
  for (let e of i) for (let t of e.parts) t.kind === "tool" && this.tools.set(t.toolCallId, t);
656
711
  this.keepPreviews(r);
@@ -659,7 +714,7 @@ var w = class {
659
714
  t && (t.allowed = !0, e.preview && (t.preview = e.preview));
660
715
  }
661
716
  this.retryAction = null, this.dropPhotos(), this.state = {
662
- ...h(!!this.opts.uploadFile && n.attachments === !0),
717
+ ...y(!!this.opts.uploadFile && n.attachments === !0),
663
718
  conversationId: e,
664
719
  messages: i,
665
720
  pendingApprovals: r,
@@ -670,7 +725,7 @@ var w = class {
670
725
  if (t !== this.generation) return;
671
726
  this.retryAction = () => this.openConversation(e), this.changed({
672
727
  status: "idle",
673
- problem: b(n)
728
+ problem: w(n)
674
729
  });
675
730
  }
676
731
  }
@@ -680,7 +735,7 @@ var w = class {
680
735
  async end() {
681
736
  this.controller?.abort();
682
737
  let e = this.session;
683
- this.session = null, this.newConversation(), e && await this.opts.endSession?.(e.sessionId);
738
+ this.session = null, this.capabilitiesWanted = !1, this.reset(), e && await this.opts.endSession?.(e.sessionId);
684
739
  }
685
740
  busy() {
686
741
  return this.state.status === "sending" || this.state.status === "streaming" || this.state.status === "loading";
@@ -701,11 +756,11 @@ var w = class {
701
756
  body: JSON.stringify(t),
702
757
  headers: { accept: "text/event-stream, application/json;q=0.9" },
703
758
  signal: a.signal
704
- }), !o.ok) throw await y(o);
759
+ }), !o.ok) throw await C(o);
705
760
  } catch (n) {
706
761
  if (this.controller === a && (this.controller = null), !r()) return "answered";
707
- if (this.drop(i), x(n)) return this.changed({ status: "idle" }), "not-sent";
708
- let o = b(n);
762
+ if (this.drop(i), T(n)) return this.changed({ status: "idle" }), "not-sent";
763
+ let o = w(n);
709
764
  if (o.code === "photos-refused") {
710
765
  let e = this.state.messages.at(-1);
711
766
  return e?.role === "user" && this.drop(e), this.policy = null, this.changed({
@@ -723,13 +778,13 @@ var w = class {
723
778
  this.changed({ status: "streaming" });
724
779
  let s = !1, c = null, l = !1, u = /* @__PURE__ */ new Map();
725
780
  try {
726
- let e = o.body ? f(o.body, a.signal) : T(await o.text());
781
+ let e = o.body ? g(o.body, a.signal) : k(await o.text());
727
782
  for await (let t of e) {
728
783
  if (!r()) break;
729
784
  t.type === "finish" ? s = !0 : t.type === "data-turn" && (c = t.data), this.apply(t, i, u), this.changed();
730
785
  }
731
786
  } catch (e) {
732
- x(e) || (l = !0);
787
+ T(e) || (l = !0);
733
788
  } finally {
734
789
  this.controller === a && (this.controller = null);
735
790
  }
@@ -777,9 +832,9 @@ var w = class {
777
832
  e && t.preview && (e.preview = t.preview);
778
833
  }
779
834
  }
780
- apply(t, n, r) {
781
- let i = t;
782
- switch (t.type) {
835
+ apply(e, t, n) {
836
+ let i = e;
837
+ switch (e.type) {
783
838
  case "start": {
784
839
  let e = i.messageMetadata?.conversationId;
785
840
  typeof e == "string" && (this.state.conversationId = e);
@@ -791,26 +846,26 @@ var w = class {
791
846
  id: this.id("t"),
792
847
  text: ""
793
848
  };
794
- r.set(String(i.id), e), n.parts.push(e);
849
+ n.set(String(i.id), e), t.parts.push(e);
795
850
  return;
796
851
  }
797
852
  case "text-delta": {
798
- let e = r.get(String(i.id));
853
+ let e = n.get(String(i.id));
799
854
  e || (e = {
800
855
  kind: "text",
801
856
  id: this.id("t"),
802
857
  text: ""
803
- }, r.set(String(i.id), e), n.parts.push(e)), e.text += String(i.delta ?? "");
858
+ }, n.set(String(i.id), e), t.parts.push(e)), e.text += String(i.delta ?? "");
804
859
  return;
805
860
  }
806
861
  case "tool-input-start":
807
862
  case "tool-input-available": {
808
- let e = this.toolPart(i, n);
809
- t.type === "tool-input-available" && (e.input = i.input, e.state === "input" && (e.state = "running"));
863
+ let n = this.toolPart(i, t);
864
+ e.type === "tool-input-available" && (n.input = i.input, n.state === "input" && (n.state = "running"));
810
865
  return;
811
866
  }
812
867
  case "tool-input-error": {
813
- let e = this.toolPart(i, n);
868
+ let e = this.toolPart(i, t);
814
869
  e.state = "error", e.errorText = String(i.errorText ?? "");
815
870
  return;
816
871
  }
@@ -821,14 +876,20 @@ var w = class {
821
876
  }
822
877
  case "tool-output-available":
823
878
  case "tool-output-error": {
824
- let n = this.tools.get(String(i.toolCallId));
825
- if (!n) return;
826
- t.type === "tool-output-available" ? (n.state = "done", n.output = e(i.output)) : (n.state = "error", n.errorText = String(i.errorText ?? "")), this.emit("tool-result", {
827
- toolCallId: n.toolCallId,
828
- toolName: n.toolName,
829
- ...n.title ? { title: n.title } : {},
830
- input: n.input,
831
- ...n.state === "done" ? { output: n.output } : { error: n.errorText }
879
+ let t = this.tools.get(String(i.toolCallId));
880
+ if (!t) return;
881
+ if (e.type === "tool-output-available") {
882
+ t.state = "done";
883
+ let { output: e, images: n } = r(i.output);
884
+ t.output = e, n.length && (t.images = n);
885
+ } else t.state = "error", t.errorText = String(i.errorText ?? "");
886
+ this.emit("tool-result", {
887
+ toolCallId: t.toolCallId,
888
+ toolName: t.toolName,
889
+ ...t.title ? { title: t.title } : {},
890
+ input: t.input,
891
+ ...t.state === "done" ? { output: t.output } : { error: t.errorText },
892
+ ...t.images?.length ? { images: t.images } : {}
832
893
  });
833
894
  return;
834
895
  }
@@ -867,9 +928,9 @@ var w = class {
867
928
  return r;
868
929
  }
869
930
  };
870
- async function* T(e) {
871
- yield* u(e);
931
+ async function* k(e) {
932
+ yield* m(e);
872
933
  }
873
- export { u as a, s as c, d as i, a as l, w as n, f as o, b as r, c as s, m as t, e as u };
934
+ export { m as a, d as c, i as d, r as f, h as i, l, O as n, g as o, w as r, f as s, v as t, e as u };
874
935
 
875
- //# sourceMappingURL=core-2Z4rCitF.js.map
936
+ //# sourceMappingURL=core-vIc1mTTy.js.map