@ghuri/ai-widget 0.3.1 → 0.4.0

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,97 @@ 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, d = .7, f = {
151
+ "image/jpeg": "jpg",
152
+ "image/png": "png",
153
+ "image/webp": "webp",
154
+ "image/gif": "gif"
155
+ }, p = /\.(jpe?g|jfif|png|webp|gif|heic|heif|avif|bmp|tiff?)$/i;
156
+ function m(e, t) {
157
+ let n = f[t];
158
+ if (!n) return e;
159
+ let r = p.exec(e)?.[1]?.toLowerCase();
160
+ return r === n || n === "jpg" && (r === "jpeg" || r === "jfif") ? e : `${r ? e.slice(0, -r.length - 1) : e}.${n}`;
161
+ }
162
+ function h(e, t, n) {
111
163
  let r = Math.max(e, t);
112
164
  if (!r || r <= n) return {
113
165
  width: e,
@@ -119,7 +171,7 @@ function s(e, t, n) {
119
171
  height: Math.max(1, Math.round(t * i))
120
172
  };
121
173
  }
122
- async function c(e, t) {
174
+ async function g(e, t) {
123
175
  let { contentType: n } = t;
124
176
  if (n === "image/gif" || typeof createImageBitmap != "function") return {
125
177
  blob: e,
@@ -135,41 +187,64 @@ async function c(e, t) {
135
187
  };
136
188
  }
137
189
  try {
138
- let i = s(r.width, r.height, t.maxEdge), a = await l(r, i, n);
139
- return a ? {
140
- blob: a,
141
- contentType: a.type || n,
142
- ...i
143
- } : {
190
+ let i = h(r.width, r.height, t.maxEdge), a = y(r, i), o = a && await b(a, n, u);
191
+ if (!a || !o) return {
144
192
  blob: e,
145
193
  contentType: n,
146
194
  width: r.width,
147
195
  height: r.height
148
196
  };
197
+ let s = t.targetBytes && o.size > t.targetBytes ? await _(a, t.targetBytes) : null, c = s && s.size < o.size ? s : o;
198
+ return {
199
+ blob: c,
200
+ contentType: c.type || n,
201
+ ...i
202
+ };
149
203
  } finally {
150
204
  r.close();
151
205
  }
152
206
  }
153
- async function l(e, t, n) {
154
- if (typeof OffscreenCanvas == "function") {
155
- let r = new OffscreenCanvas(t.width, t.height), i = r.getContext("2d");
156
- return i ? (i.drawImage(e, 0, 0, t.width, t.height), r.convertToBlob({
157
- type: n,
158
- quality: o
159
- }).catch(() => null)) : null;
160
- }
161
- if (typeof document > "u") return null;
162
- let r = document.createElement("canvas");
163
- r.width = t.width, r.height = t.height;
164
- 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;
207
+ async function _(e, t) {
208
+ let n = v(e) ? "image/jpeg" : "image/webp", r = await b(e, n, u);
209
+ if (!r || r.type !== n) return null;
210
+ if (r.size > t) {
211
+ let t = await b(e, n, d);
212
+ t && t.type === n && t.size < r.size && (r = t);
213
+ }
214
+ return r;
215
+ }
216
+ function v(e) {
217
+ let t = e.getContext("2d");
218
+ if (!t) return !1;
219
+ let n;
220
+ try {
221
+ n = t.getImageData(0, 0, e.width, e.height).data;
222
+ } catch {
223
+ return !1;
224
+ }
225
+ for (let e = 3; e < n.length; e += 4) if (n[e] !== 255) return !1;
226
+ return !0;
227
+ }
228
+ function y(e, t) {
229
+ let n;
230
+ if (typeof OffscreenCanvas == "function") n = new OffscreenCanvas(t.width, t.height);
231
+ else if (typeof document < "u") n = document.createElement("canvas"), n.width = t.width, n.height = t.height;
232
+ else return null;
233
+ let r = n.getContext("2d");
234
+ return r ? (r.drawImage(e, 0, 0, t.width, t.height), n) : null;
235
+ }
236
+ function b(e, t, n) {
237
+ return "convertToBlob" in e ? e.convertToBlob({
238
+ type: t,
239
+ quality: n
240
+ }).catch(() => null) : new Promise((r) => e.toBlob((e) => r(e), t, n));
166
241
  }
167
242
  // src/core/sse.ts
168
- function u(e) {
169
- let t = new d(), n = t.push(e);
243
+ function x(e) {
244
+ let t = new S(), n = t.push(e);
170
245
  return n.push(...t.end()), n;
171
246
  }
172
- var d = class {
247
+ var S = class {
173
248
  buffer = "";
174
249
  carry = "";
175
250
  done = !1;
@@ -206,8 +281,8 @@ var d = class {
206
281
  }
207
282
  }
208
283
  };
209
- async function* f(e, t) {
210
- let n = new d(), r = new TextDecoder(), i = e.getReader(), a = () => void i.cancel().catch(() => {});
284
+ async function* C(e, t) {
285
+ let n = new S(), r = new TextDecoder(), i = e.getReader(), a = () => void i.cancel().catch(() => {});
211
286
  t?.addEventListener("abort", a, { once: !0 });
212
287
  try {
213
288
  for (;;) {
@@ -223,7 +298,7 @@ async function* f(e, t) {
223
298
  }
224
299
  }
225
300
  // src/core/client.ts
226
- var p = 3e4, m = {
301
+ var w = 3e4, T = {
227
302
  accept: [
228
303
  "image/jpeg",
229
304
  "image/png",
@@ -233,8 +308,11 @@ var p = 3e4, m = {
233
308
  maxCount: 10,
234
309
  maxBytes: 5242880,
235
310
  maxEdge: 2048
311
+ }, E = {
312
+ maxEdge: 1568,
313
+ targetBytes: 1e6
236
314
  };
237
- function h(e = !1) {
315
+ function D(e = !1) {
238
316
  return {
239
317
  status: "idle",
240
318
  conversationId: null,
@@ -247,19 +325,19 @@ function h(e = !1) {
247
325
  allowances: []
248
326
  };
249
327
  }
250
- function g(e) {
328
+ function O(e) {
251
329
  try {
252
330
  return typeof URL.createObjectURL == "function" ? URL.createObjectURL(e) : void 0;
253
331
  } catch {
254
332
  return;
255
333
  }
256
334
  }
257
- function _(e) {
335
+ function k(e) {
258
336
  try {
259
337
  e && typeof URL.revokeObjectURL == "function" && URL.revokeObjectURL(e);
260
338
  } catch {}
261
339
  }
262
- var v = class extends Error {
340
+ var A = class extends Error {
263
341
  status;
264
342
  code;
265
343
  detail;
@@ -269,16 +347,16 @@ var v = class extends Error {
269
347
  super(n || `HTTP ${e}`), this.status = e, this.code = t, this.detail = n, this.retryAfter = r, this.fields = i;
270
348
  }
271
349
  };
272
- async function y(e) {
350
+ async function j(e) {
273
351
  let t = null;
274
352
  try {
275
353
  t = await e.json();
276
354
  } catch {}
277
355
  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 : {});
356
+ return new A(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
357
  }
280
- function b(e) {
281
- if (!(e instanceof v)) return { code: "unavailable" };
358
+ function M(e) {
359
+ if (!(e instanceof A)) return { code: "unavailable" };
282
360
  let { status: t, code: n, detail: r } = e, i = e.fields.attachments;
283
361
  return t === 400 && Array.isArray(i) ? {
284
362
  code: "photos-refused",
@@ -291,14 +369,14 @@ function b(e) {
291
369
  ...r ? { message: r } : {}
292
370
  };
293
371
  }
294
- function x(e) {
372
+ function N(e) {
295
373
  return e instanceof DOMException ? e.name === "AbortError" : e?.name === "AbortError";
296
374
  }
297
- function S(e, t) {
375
+ function P(e, t) {
298
376
  let n = new Date(e.expiresAt).getTime();
299
- return Number.isNaN(n) || n - p > t;
377
+ return Number.isNaN(n) || n - w > t;
300
378
  }
301
- function C(e) {
379
+ function F(e) {
302
380
  return {
303
381
  ...e,
304
382
  messages: e.messages.map((e) => ({
@@ -310,7 +388,7 @@ function C(e) {
310
388
  allowances: [...e.allowances]
311
389
  };
312
390
  }
313
- var w = class {
391
+ var I = class {
314
392
  opts;
315
393
  state;
316
394
  view;
@@ -328,11 +406,12 @@ var w = class {
328
406
  previews = /* @__PURE__ */ new Set();
329
407
  policy = null;
330
408
  loadingPolicy = null;
409
+ capabilitiesWanted = !1;
331
410
  constructor(e) {
332
411
  this.opts = {
333
412
  ...e,
334
413
  apiUrl: e.apiUrl.replace(/\/+$/, "")
335
- }, this.state = h(!1), this.view = C(this.state);
414
+ }, this.state = D(!1), this.view = F(this.state);
336
415
  }
337
416
  getState() {
338
417
  return this.view;
@@ -348,7 +427,7 @@ var w = class {
348
427
  for (let n of this.events.get(e) ?? []) n(t);
349
428
  }
350
429
  changed(e = {}) {
351
- Object.assign(this.state, e), this.view = C(this.state);
430
+ Object.assign(this.state, e), this.view = F(this.state);
352
431
  for (let e of this.listeners) e(this.view);
353
432
  }
354
433
  id(e) {
@@ -359,7 +438,7 @@ var w = class {
359
438
  }
360
439
  async ensureSession() {
361
440
  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(() => {
441
+ return e && P(e, this.now()) ? e : (this.session = null, this.starting ||= this.opts.startSession().then((e) => (this.session = e, e)).finally(() => {
363
442
  this.starting = null;
364
443
  }), this.starting);
365
444
  }
@@ -371,7 +450,7 @@ var w = class {
371
450
  headers: i
372
451
  });
373
452
  if (a.status !== 401 || n) return a;
374
- let o = await y(a);
453
+ let o = await j(a);
375
454
  if (this.changed({ problem: { code: "reconnecting" } }), o.code === "user_token_expired") try {
376
455
  await this.opts.refreshUserToken(r.sessionId);
377
456
  } catch {
@@ -383,7 +462,7 @@ var w = class {
383
462
  }
384
463
  async json(e, t = {}) {
385
464
  let n = await this.request(e, t);
386
- if (!n.ok) throw await y(n);
465
+ if (!n.ok) throw await j(n);
387
466
  return await n.json();
388
467
  }
389
468
  async send(e) {
@@ -414,7 +493,7 @@ var w = class {
414
493
  role: "user",
415
494
  parts: i
416
495
  });
417
- for (let e of this.state.uploads) e.status !== "ready" && _(e.previewUrl);
496
+ for (let e of this.state.uploads) e.status !== "ready" && k(e.previewUrl);
418
497
  this.files = /* @__PURE__ */ new Map(), this.changed({
419
498
  pendingApprovals: [],
420
499
  problem: null,
@@ -429,12 +508,13 @@ var w = class {
429
508
  });
430
509
  }
431
510
  attachmentLimits() {
432
- let e = this.opts.attachments ?? {}, t = e.accept?.length ? e.accept : [...m.accept], n = this.policy?.accept?.length ? this.policy.accept : null;
511
+ let e = this.opts.attachments ?? {}, t = e.accept?.length ? e.accept : [...T.accept], n = this.policy?.accept?.length ? this.policy.accept : null, r = this.policy?.vision === !0;
433
512
  return {
434
513
  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
514
+ maxCount: Math.min(e.maxCount ?? T.maxCount, this.policy?.maxCount ?? T.maxCount, T.maxCount),
515
+ maxBytes: e.maxBytes ?? T.maxBytes,
516
+ maxEdge: e.maxEdge ?? (r ? E.maxEdge : T.maxEdge),
517
+ ...e.targetBytes || r ? { targetBytes: e.targetBytes ?? E.targetBytes } : {}
438
518
  };
439
519
  }
440
520
  retryable() {
@@ -444,16 +524,17 @@ var w = class {
444
524
  return !!this.opts.uploadFile && this.policy?.enabled === !0;
445
525
  }
446
526
  loadCapabilities() {
447
- return !this.opts.uploadFile || this.policy ? Promise.resolve() : (this.loadingPolicy ||= (async () => {
527
+ return !this.opts.uploadFile || (this.capabilitiesWanted = !0, this.policy) ? Promise.resolve() : (this.loadingPolicy ||= (async () => {
448
528
  try {
449
529
  let e = (await this.json("/v1/capabilities"))?.attachments ?? {};
450
530
  this.policy = {
451
531
  enabled: e.enabled === !0,
452
532
  ...Array.isArray(e.accept) ? { accept: e.accept.filter((e) => typeof e == "string") } : {},
453
- ...Number.isInteger(e.maxCount) ? { maxCount: e.maxCount } : {}
533
+ ...Number.isInteger(e.maxCount) ? { maxCount: e.maxCount } : {},
534
+ ...e.vision === !0 ? { vision: !0 } : {}
454
535
  };
455
536
  } catch (e) {
456
- e instanceof v && e.status === 404 && (this.policy = { enabled: !1 });
537
+ e instanceof A && e.status === 404 && (this.policy = { enabled: !1 });
457
538
  } finally {
458
539
  this.loadingPolicy = null;
459
540
  }
@@ -485,7 +566,7 @@ var w = class {
485
566
  size: r.size,
486
567
  status: "preparing",
487
568
  progress: 0
488
- }, o = t.includes(i) ? g(r) : void 0;
569
+ }, o = t.includes(i) ? O(r) : void 0;
489
570
  o && (a.previewUrl = o, this.previews.add(o)), this.state.uploads.push(a);
490
571
  let s = this.state.uploads.filter((e) => e.status !== "failed" || e.error === "upload").length;
491
572
  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 +575,7 @@ var w = class {
494
575
  }
495
576
  removeUpload(e) {
496
577
  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) }));
578
+ t && (t.attachment && this.discard(t.attachment), this.files.get(e)?.controller?.abort(), this.files.delete(e), k(t.previewUrl), t.previewUrl && this.previews.delete(t.previewUrl), this.changed({ uploads: this.state.uploads.filter((t) => t.id !== e) }));
498
579
  }
499
580
  retryUpload(e) {
500
581
  let t = this.upload(e);
@@ -510,13 +591,14 @@ var w = class {
510
591
  error: void 0,
511
592
  message: void 0
512
593
  }), this.changed();
513
- let { maxBytes: s, maxEdge: l } = this.attachmentLimits();
594
+ let { maxBytes: s, maxEdge: c, targetBytes: l } = this.attachmentLimits();
514
595
  try {
515
- let e = this.opts.prepareImage ?? c, i;
596
+ let e = this.opts.prepareImage ?? g, i;
516
597
  try {
517
598
  i = await e(t.file, {
518
- maxEdge: l,
519
- contentType: n.contentType
599
+ maxEdge: c,
600
+ contentType: n.contentType,
601
+ ...l ? { targetBytes: l } : {}
520
602
  });
521
603
  } catch {
522
604
  i = {
@@ -531,7 +613,9 @@ var w = class {
531
613
  }
532
614
  Object.assign(n, {
533
615
  status: "uploading",
534
- size: i.blob.size
616
+ size: i.blob.size,
617
+ name: m(n.name, i.contentType),
618
+ contentType: i.contentType
535
619
  }), this.changed();
536
620
  let u = await r(i.blob, {
537
621
  name: n.name,
@@ -544,7 +628,10 @@ var w = class {
544
628
  a() && n.status === "uploading" && (n.progress = Math.max(0, Math.min(1, Number(e) || 0)), this.changed());
545
629
  }
546
630
  });
547
- if (!a() || o.signal.aborted) return;
631
+ if (!a() || o.signal.aborted) {
632
+ u && typeof u.ref == "string" && u.ref && this.discard(u);
633
+ return;
634
+ }
548
635
  if (!u || typeof u.ref != "string" || !u.ref) throw Error("The upload gave no reference.");
549
636
  Object.assign(n, {
550
637
  status: "ready",
@@ -558,14 +645,21 @@ var w = class {
558
645
  }
559
646
  }), this.changed();
560
647
  } catch (e) {
561
- if (!a() || x(e) || o.signal.aborted) return;
648
+ if (!a() || N(e) || o.signal.aborted) return;
562
649
  this.refuse(n, "upload", e instanceof Error && e.message ? e.message : void 0), this.changed();
563
650
  }
564
651
  }
652
+ discard(e) {
653
+ let t = this.opts.discardFile;
654
+ if (t) try {
655
+ Promise.resolve(t(e)).catch(() => {});
656
+ } catch {}
657
+ }
565
658
  dropPhotos() {
659
+ for (let e of this.state.uploads) e.attachment && this.discard(e.attachment);
566
660
  for (let e of this.files.values()) e.controller?.abort();
567
661
  this.files = /* @__PURE__ */ new Map();
568
- for (let e of this.previews) _(e);
662
+ for (let e of this.previews) k(e);
569
663
  this.previews = /* @__PURE__ */ new Set();
570
664
  }
571
665
  async decide(e) {
@@ -597,7 +691,7 @@ var w = class {
597
691
  let n = await this.json(`/v1/conversations/${encodeURIComponent(t)}/allowances/${encodeURIComponent(e)}`, { method: "DELETE" });
598
692
  return this.changed({ allowances: n.allowances ?? [] }), !0;
599
693
  } catch (t) {
600
- let n = b(t);
694
+ let n = M(t);
601
695
  return this.changed({
602
696
  allowances: this.state.allowances.filter((t) => t.toolName !== e),
603
697
  ...t?.status === 404 ? {} : { problem: n }
@@ -628,7 +722,10 @@ var w = class {
628
722
  this.changed({ problem: null });
629
723
  }
630
724
  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();
725
+ this.reset(), this.capabilitiesWanted && this.loadCapabilities();
726
+ }
727
+ reset() {
728
+ this.generation += 1, this.controller?.abort(), this.dropPhotos(), this.tools = /* @__PURE__ */ new Map(), this.retryAction = null, this.state = D(this.newConversationTakesPhotos()), this.changed();
632
729
  }
633
730
  async listConversations(e = {}) {
634
731
  let t = new URLSearchParams({
@@ -650,7 +747,7 @@ var w = class {
650
747
  for (let [e, t] of Object.entries(n.toolTitles ?? {})) typeof t == "string" && t && this.titles.set(e, t);
651
748
  let r = n.pendingApprovals ?? [];
652
749
  for (let e of r) e.title && this.titles.set(e.toolName, e.title);
653
- let i = a(n.messages ?? [], r, this.titles);
750
+ let i = l(n.messages ?? [], r, this.titles);
654
751
  this.tools = /* @__PURE__ */ new Map();
655
752
  for (let e of i) for (let t of e.parts) t.kind === "tool" && this.tools.set(t.toolCallId, t);
656
753
  this.keepPreviews(r);
@@ -659,7 +756,7 @@ var w = class {
659
756
  t && (t.allowed = !0, e.preview && (t.preview = e.preview));
660
757
  }
661
758
  this.retryAction = null, this.dropPhotos(), this.state = {
662
- ...h(!!this.opts.uploadFile && n.attachments === !0),
759
+ ...D(!!this.opts.uploadFile && n.attachments === !0),
663
760
  conversationId: e,
664
761
  messages: i,
665
762
  pendingApprovals: r,
@@ -670,7 +767,7 @@ var w = class {
670
767
  if (t !== this.generation) return;
671
768
  this.retryAction = () => this.openConversation(e), this.changed({
672
769
  status: "idle",
673
- problem: b(n)
770
+ problem: M(n)
674
771
  });
675
772
  }
676
773
  }
@@ -680,7 +777,7 @@ var w = class {
680
777
  async end() {
681
778
  this.controller?.abort();
682
779
  let e = this.session;
683
- this.session = null, this.newConversation(), e && await this.opts.endSession?.(e.sessionId);
780
+ this.session = null, this.capabilitiesWanted = !1, this.reset(), e && await this.opts.endSession?.(e.sessionId);
684
781
  }
685
782
  busy() {
686
783
  return this.state.status === "sending" || this.state.status === "streaming" || this.state.status === "loading";
@@ -701,11 +798,11 @@ var w = class {
701
798
  body: JSON.stringify(t),
702
799
  headers: { accept: "text/event-stream, application/json;q=0.9" },
703
800
  signal: a.signal
704
- }), !o.ok) throw await y(o);
801
+ }), !o.ok) throw await j(o);
705
802
  } catch (n) {
706
803
  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);
804
+ if (this.drop(i), N(n)) return this.changed({ status: "idle" }), "not-sent";
805
+ let o = M(n);
709
806
  if (o.code === "photos-refused") {
710
807
  let e = this.state.messages.at(-1);
711
808
  return e?.role === "user" && this.drop(e), this.policy = null, this.changed({
@@ -723,13 +820,13 @@ var w = class {
723
820
  this.changed({ status: "streaming" });
724
821
  let s = !1, c = null, l = !1, u = /* @__PURE__ */ new Map();
725
822
  try {
726
- let e = o.body ? f(o.body, a.signal) : T(await o.text());
823
+ let e = o.body ? C(o.body, a.signal) : L(await o.text());
727
824
  for await (let t of e) {
728
825
  if (!r()) break;
729
826
  t.type === "finish" ? s = !0 : t.type === "data-turn" && (c = t.data), this.apply(t, i, u), this.changed();
730
827
  }
731
828
  } catch (e) {
732
- x(e) || (l = !0);
829
+ N(e) || (l = !0);
733
830
  } finally {
734
831
  this.controller === a && (this.controller = null);
735
832
  }
@@ -777,9 +874,9 @@ var w = class {
777
874
  e && t.preview && (e.preview = t.preview);
778
875
  }
779
876
  }
780
- apply(t, n, r) {
781
- let i = t;
782
- switch (t.type) {
877
+ apply(e, t, n) {
878
+ let i = e;
879
+ switch (e.type) {
783
880
  case "start": {
784
881
  let e = i.messageMetadata?.conversationId;
785
882
  typeof e == "string" && (this.state.conversationId = e);
@@ -791,26 +888,26 @@ var w = class {
791
888
  id: this.id("t"),
792
889
  text: ""
793
890
  };
794
- r.set(String(i.id), e), n.parts.push(e);
891
+ n.set(String(i.id), e), t.parts.push(e);
795
892
  return;
796
893
  }
797
894
  case "text-delta": {
798
- let e = r.get(String(i.id));
895
+ let e = n.get(String(i.id));
799
896
  e || (e = {
800
897
  kind: "text",
801
898
  id: this.id("t"),
802
899
  text: ""
803
- }, r.set(String(i.id), e), n.parts.push(e)), e.text += String(i.delta ?? "");
900
+ }, n.set(String(i.id), e), t.parts.push(e)), e.text += String(i.delta ?? "");
804
901
  return;
805
902
  }
806
903
  case "tool-input-start":
807
904
  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"));
905
+ let n = this.toolPart(i, t);
906
+ e.type === "tool-input-available" && (n.input = i.input, n.state === "input" && (n.state = "running"));
810
907
  return;
811
908
  }
812
909
  case "tool-input-error": {
813
- let e = this.toolPart(i, n);
910
+ let e = this.toolPart(i, t);
814
911
  e.state = "error", e.errorText = String(i.errorText ?? "");
815
912
  return;
816
913
  }
@@ -821,14 +918,20 @@ var w = class {
821
918
  }
822
919
  case "tool-output-available":
823
920
  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 }
921
+ let t = this.tools.get(String(i.toolCallId));
922
+ if (!t) return;
923
+ if (e.type === "tool-output-available") {
924
+ t.state = "done";
925
+ let { output: e, images: n } = r(i.output);
926
+ t.output = e, n.length && (t.images = n);
927
+ } else t.state = "error", t.errorText = String(i.errorText ?? "");
928
+ this.emit("tool-result", {
929
+ toolCallId: t.toolCallId,
930
+ toolName: t.toolName,
931
+ ...t.title ? { title: t.title } : {},
932
+ input: t.input,
933
+ ...t.state === "done" ? { output: t.output } : { error: t.errorText },
934
+ ...t.images?.length ? { images: t.images } : {}
832
935
  });
833
936
  return;
834
937
  }
@@ -867,9 +970,9 @@ var w = class {
867
970
  return r;
868
971
  }
869
972
  };
870
- async function* T(e) {
871
- yield* u(e);
973
+ async function* L(e) {
974
+ yield* x(e);
872
975
  }
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 };
976
+ export { x as a, h as c, i as d, r as f, S as i, l, I as n, C as o, M as r, g as s, T as t, e as u };
874
977
 
875
- //# sourceMappingURL=core-2Z4rCitF.js.map
978
+ //# sourceMappingURL=core-9svu5kzm.js.map