@ghuri/ai-widget 0.3.2 → 0.5.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.
@@ -84,31 +84,33 @@ function c(t) {
84
84
  }
85
85
  }
86
86
  function l(e, t = [], n = /* @__PURE__ */ new Map()) {
87
- let r = [], i = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map();
87
+ let r = [], i = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map();
88
88
  for (let e of t) e.title && n.set(e.toolName, e.title);
89
- let l = null, u = 0, d = () => (l || (l = {
90
- id: `h${u++}`,
89
+ let u = null, d = 0, f = () => (u || (u = {
90
+ id: `h${d++}`,
91
91
  role: "assistant",
92
92
  parts: []
93
- }, r.push(l)), l);
93
+ }, r.push(u)), u);
94
94
  for (let t of e) if (t.role === "user") {
95
- l = null;
96
- let e = Array.isArray(t.content) ? t.content.flatMap((e) => a(e) ?? []) : [], n = s(t.content), i = [];
95
+ u = null;
96
+ let e = Array.isArray(t.content) ? t.content.flatMap((e) => a(e) ?? []) : [], n = s(t.content);
97
+ if (!n && !e.length && Array.isArray(t.content) && t.content.length && t.content.every(o)) continue;
98
+ let i = [];
97
99
  e.length && i.push({
98
100
  kind: "attachments",
99
- id: `h${u++}`,
101
+ id: `h${d++}`,
100
102
  attachments: e
101
103
  }), (n || !e.length) && i.push({
102
104
  kind: "text",
103
- id: `h${u++}`,
105
+ id: `h${d++}`,
104
106
  text: n
105
107
  }), r.push({
106
- id: `h${u++}`,
108
+ id: `h${d++}`,
107
109
  role: "user",
108
110
  parts: i
109
111
  });
110
112
  } else if (t.role === "assistant") {
111
- let e = d(), r = typeof t.content == "string" ? [{
113
+ let e = f(), r = typeof t.content == "string" ? [{
112
114
  type: "text",
113
115
  text: t.content
114
116
  }] : t.content ?? [];
@@ -116,7 +118,7 @@ function l(e, t = [], n = /* @__PURE__ */ new Map()) {
116
118
  let n = e.parts.at(-1);
117
119
  n?.kind === "text" ? n.text += t.text : e.parts.push({
118
120
  kind: "text",
119
- id: `h${u++}`,
121
+ id: `h${d++}`,
120
122
  text: t.text
121
123
  });
122
124
  } else if (t.type === "tool-call" && typeof t.toolCallId == "string") {
@@ -131,24 +133,35 @@ function l(e, t = [], n = /* @__PURE__ */ new Map()) {
131
133
  i.set(a.toolCallId, a), e.parts.push(a);
132
134
  } else if (t.type === "tool-approval-request" && typeof t.toolCallId == "string") {
133
135
  let e = i.get(t.toolCallId);
134
- e && (e.state = "awaiting-approval", e.approvalId = t.approvalId, o.set(t.approvalId, e));
136
+ e && (e.state = "awaiting-approval", e.approvalId = t.approvalId, l.set(t.approvalId, e));
135
137
  }
136
138
  } else if (t.role === "tool" && Array.isArray(t.content)) {
137
139
  for (let e of t.content) if (e.type === "tool-approval-response") {
138
- let t = o.get(e.approvalId);
140
+ let t = l.get(e.approvalId);
139
141
  t && t.state === "awaiting-approval" && (t.state = e.approved ? "running" : "denied");
140
142
  } else if (e.type === "tool-result") {
141
143
  let t = i.get(e.toolCallId);
142
144
  t && Object.assign(t, c(e.output));
143
145
  }
144
146
  }
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
+ let p = new Set(t.map((e) => e.toolCallId));
148
+ for (let e of i.values()) e.state === "awaiting-approval" && !p.has(e.toolCallId) && (e.state = "denied");
147
149
  return r.filter((e) => e.parts.length > 0);
148
150
  }
149
151
  // src/core/prepare.ts
150
- var u = .85;
151
- function d(e, t, n) {
152
+ var u = .85, d = .7, f = {
153
+ "image/jpeg": "jpg",
154
+ "image/png": "png",
155
+ "image/webp": "webp",
156
+ "image/gif": "gif"
157
+ }, p = /\.(jpe?g|jfif|png|webp|gif|heic|heif|avif|bmp|tiff?)$/i;
158
+ function m(e, t) {
159
+ let n = f[t];
160
+ if (!n) return e;
161
+ let r = p.exec(e)?.[1]?.toLowerCase();
162
+ return r === n || n === "jpg" && (r === "jpeg" || r === "jfif") ? e : `${r ? e.slice(0, -r.length - 1) : e}.${n}`;
163
+ }
164
+ function h(e, t, n) {
152
165
  let r = Math.max(e, t);
153
166
  if (!r || r <= n) return {
154
167
  width: e,
@@ -160,7 +173,7 @@ function d(e, t, n) {
160
173
  height: Math.max(1, Math.round(t * i))
161
174
  };
162
175
  }
163
- async function f(e, t) {
176
+ async function g(e, t) {
164
177
  let { contentType: n } = t;
165
178
  if (n === "image/gif" || typeof createImageBitmap != "function") return {
166
179
  blob: e,
@@ -176,41 +189,64 @@ async function f(e, t) {
176
189
  };
177
190
  }
178
191
  try {
179
- let i = d(r.width, r.height, t.maxEdge), a = await p(r, i, n);
180
- return a ? {
181
- blob: a,
182
- contentType: a.type || n,
183
- ...i
184
- } : {
192
+ let i = h(r.width, r.height, t.maxEdge), a = y(r, i), o = a && await b(a, n, u);
193
+ if (!a || !o) return {
185
194
  blob: e,
186
195
  contentType: n,
187
196
  width: r.width,
188
197
  height: r.height
189
198
  };
199
+ let s = t.targetBytes && o.size > t.targetBytes ? await _(a, t.targetBytes) : null, c = s && s.size < o.size ? s : o;
200
+ return {
201
+ blob: c,
202
+ contentType: c.type || n,
203
+ ...i
204
+ };
190
205
  } finally {
191
206
  r.close();
192
207
  }
193
208
  }
194
- async function p(e, t, n) {
195
- if (typeof OffscreenCanvas == "function") {
196
- let r = new OffscreenCanvas(t.width, t.height), i = r.getContext("2d");
197
- return i ? (i.drawImage(e, 0, 0, t.width, t.height), r.convertToBlob({
198
- type: n,
199
- quality: u
200
- }).catch(() => null)) : null;
201
- }
202
- if (typeof document > "u") return null;
203
- let r = document.createElement("canvas");
204
- r.width = t.width, r.height = t.height;
205
- let i = r.getContext("2d");
206
- return i ? (i.drawImage(e, 0, 0, t.width, t.height), new Promise((e) => r.toBlob((t) => e(t), n, u))) : null;
209
+ async function _(e, t) {
210
+ let n = v(e) ? "image/jpeg" : "image/webp", r = await b(e, n, u);
211
+ if (!r || r.type !== n) return null;
212
+ if (r.size > t) {
213
+ let t = await b(e, n, d);
214
+ t && t.type === n && t.size < r.size && (r = t);
215
+ }
216
+ return r;
217
+ }
218
+ function v(e) {
219
+ let t = e.getContext("2d");
220
+ if (!t) return !1;
221
+ let n;
222
+ try {
223
+ n = t.getImageData(0, 0, e.width, e.height).data;
224
+ } catch {
225
+ return !1;
226
+ }
227
+ for (let e = 3; e < n.length; e += 4) if (n[e] !== 255) return !1;
228
+ return !0;
229
+ }
230
+ function y(e, t) {
231
+ let n;
232
+ if (typeof OffscreenCanvas == "function") n = new OffscreenCanvas(t.width, t.height);
233
+ else if (typeof document < "u") n = document.createElement("canvas"), n.width = t.width, n.height = t.height;
234
+ else return null;
235
+ let r = n.getContext("2d");
236
+ return r ? (r.drawImage(e, 0, 0, t.width, t.height), n) : null;
237
+ }
238
+ function b(e, t, n) {
239
+ return "convertToBlob" in e ? e.convertToBlob({
240
+ type: t,
241
+ quality: n
242
+ }).catch(() => null) : new Promise((r) => e.toBlob((e) => r(e), t, n));
207
243
  }
208
244
  // src/core/sse.ts
209
- function m(e) {
210
- let t = new h(), n = t.push(e);
245
+ function x(e) {
246
+ let t = new S(), n = t.push(e);
211
247
  return n.push(...t.end()), n;
212
248
  }
213
- var h = class {
249
+ var S = class {
214
250
  buffer = "";
215
251
  carry = "";
216
252
  done = !1;
@@ -247,8 +283,8 @@ var h = class {
247
283
  }
248
284
  }
249
285
  };
250
- async function* g(e, t) {
251
- let n = new h(), r = new TextDecoder(), i = e.getReader(), a = () => void i.cancel().catch(() => {});
286
+ async function* C(e, t) {
287
+ let n = new S(), r = new TextDecoder(), i = e.getReader(), a = () => void i.cancel().catch(() => {});
252
288
  t?.addEventListener("abort", a, { once: !0 });
253
289
  try {
254
290
  for (;;) {
@@ -264,7 +300,7 @@ async function* g(e, t) {
264
300
  }
265
301
  }
266
302
  // src/core/client.ts
267
- var _ = 3e4, v = {
303
+ var w = 3e4, T = {
268
304
  accept: [
269
305
  "image/jpeg",
270
306
  "image/png",
@@ -274,8 +310,11 @@ var _ = 3e4, v = {
274
310
  maxCount: 10,
275
311
  maxBytes: 5242880,
276
312
  maxEdge: 2048
313
+ }, E = {
314
+ maxEdge: 1568,
315
+ targetBytes: 1e6
277
316
  };
278
- function y(e = !1) {
317
+ function D(e = !1) {
279
318
  return {
280
319
  status: "idle",
281
320
  conversationId: null,
@@ -285,22 +324,31 @@ function y(e = !1) {
285
324
  lastTurn: null,
286
325
  canAttach: e,
287
326
  uploads: [],
288
- allowances: []
327
+ allowances: [],
328
+ full: !1,
329
+ continuing: !1,
330
+ continuedFrom: null,
331
+ handover: null,
332
+ continuedIn: null
289
333
  };
290
334
  }
291
- function b(e) {
335
+ function O(e) {
336
+ let t = e.replace(/^\s*<handover>\s*\n?/, "").replace(/\n?\s*<\/handover>\s*$/, "").split("\n"), n = t.findIndex((e) => !e.trim());
337
+ return (n >= 0 ? t.slice(n + 1) : t).join("\n").trim();
338
+ }
339
+ function k(e) {
292
340
  try {
293
341
  return typeof URL.createObjectURL == "function" ? URL.createObjectURL(e) : void 0;
294
342
  } catch {
295
343
  return;
296
344
  }
297
345
  }
298
- function x(e) {
346
+ function A(e) {
299
347
  try {
300
348
  e && typeof URL.revokeObjectURL == "function" && URL.revokeObjectURL(e);
301
349
  } catch {}
302
350
  }
303
- var S = class extends Error {
351
+ var j = "This change had already started, and its result was not recorded. Check before trying again.", M = class extends Error {
304
352
  status;
305
353
  code;
306
354
  detail;
@@ -310,16 +358,16 @@ var S = class extends Error {
310
358
  super(n || `HTTP ${e}`), this.status = e, this.code = t, this.detail = n, this.retryAfter = r, this.fields = i;
311
359
  }
312
360
  };
313
- async function C(e) {
361
+ async function N(e) {
314
362
  let t = null;
315
363
  try {
316
364
  t = await e.json();
317
365
  } catch {}
318
366
  let n = Number(e.headers.get("retry-after"));
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 : {});
367
+ return new M(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 : {});
320
368
  }
321
- function w(e) {
322
- if (!(e instanceof S)) return { code: "unavailable" };
369
+ function P(e) {
370
+ if (!(e instanceof M)) return { code: "unavailable" };
323
371
  let { status: t, code: n, detail: r } = e, i = e.fields.attachments;
324
372
  return t === 400 && Array.isArray(i) ? {
325
373
  code: "photos-refused",
@@ -332,14 +380,14 @@ function w(e) {
332
380
  ...r ? { message: r } : {}
333
381
  };
334
382
  }
335
- function T(e) {
383
+ function F(e) {
336
384
  return e instanceof DOMException ? e.name === "AbortError" : e?.name === "AbortError";
337
385
  }
338
- function E(e, t) {
386
+ function I(e, t) {
339
387
  let n = new Date(e.expiresAt).getTime();
340
- return Number.isNaN(n) || n - _ > t;
388
+ return Number.isNaN(n) || n - w > t;
341
389
  }
342
- function D(e) {
390
+ function L(e) {
343
391
  return {
344
392
  ...e,
345
393
  messages: e.messages.map((e) => ({
@@ -351,7 +399,7 @@ function D(e) {
351
399
  allowances: [...e.allowances]
352
400
  };
353
401
  }
354
- var O = class {
402
+ var R = class {
355
403
  opts;
356
404
  state;
357
405
  view;
@@ -374,7 +422,7 @@ var O = class {
374
422
  this.opts = {
375
423
  ...e,
376
424
  apiUrl: e.apiUrl.replace(/\/+$/, "")
377
- }, this.state = y(!1), this.view = D(this.state);
425
+ }, this.state = D(!1), this.view = L(this.state);
378
426
  }
379
427
  getState() {
380
428
  return this.view;
@@ -390,7 +438,7 @@ var O = class {
390
438
  for (let n of this.events.get(e) ?? []) n(t);
391
439
  }
392
440
  changed(e = {}) {
393
- Object.assign(this.state, e), this.view = D(this.state);
441
+ Object.assign(this.state, e), this.view = L(this.state);
394
442
  for (let e of this.listeners) e(this.view);
395
443
  }
396
444
  id(e) {
@@ -401,7 +449,7 @@ var O = class {
401
449
  }
402
450
  async ensureSession() {
403
451
  let e = this.session;
404
- return e && E(e, this.now()) ? e : (this.session = null, this.starting ||= this.opts.startSession().then((e) => (this.session = e, e)).finally(() => {
452
+ return e && I(e, this.now()) ? e : (this.session = null, this.starting ||= this.opts.startSession().then((e) => (this.session = e, e)).finally(() => {
405
453
  this.starting = null;
406
454
  }), this.starting);
407
455
  }
@@ -413,7 +461,7 @@ var O = class {
413
461
  headers: i
414
462
  });
415
463
  if (a.status !== 401 || n) return a;
416
- let o = await C(a);
464
+ let o = await N(a);
417
465
  if (this.changed({ problem: { code: "reconnecting" } }), o.code === "user_token_expired") try {
418
466
  await this.opts.refreshUserToken(r.sessionId);
419
467
  } catch {
@@ -425,12 +473,16 @@ var O = class {
425
473
  }
426
474
  async json(e, t = {}) {
427
475
  let n = await this.request(e, t);
428
- if (!n.ok) throw await C(n);
476
+ if (!n.ok) throw await N(n);
429
477
  return await n.json();
430
478
  }
431
479
  async send(e) {
432
480
  let t = e.trim(), n = this.state.uploads.filter((e) => e.status === "ready" && e.attachment);
433
481
  if (!t && !n.length || this.busy() || this.uploading() || this.retryable()) return;
482
+ if (this.state.full) {
483
+ this.changed({ problem: { code: "conversation-full" } });
484
+ return;
485
+ }
434
486
  for (let e of this.state.pendingApprovals) {
435
487
  let t = this.tools.get(e.toolCallId);
436
488
  t && (t.state = "denied");
@@ -456,7 +508,7 @@ var O = class {
456
508
  role: "user",
457
509
  parts: i
458
510
  });
459
- for (let e of this.state.uploads) e.status !== "ready" && x(e.previewUrl);
511
+ for (let e of this.state.uploads) e.status !== "ready" && A(e.previewUrl);
460
512
  this.files = /* @__PURE__ */ new Map(), this.changed({
461
513
  pendingApprovals: [],
462
514
  problem: null,
@@ -471,12 +523,13 @@ var O = class {
471
523
  });
472
524
  }
473
525
  attachmentLimits() {
474
- let e = this.opts.attachments ?? {}, t = e.accept?.length ? e.accept : [...v.accept], n = this.policy?.accept?.length ? this.policy.accept : null;
526
+ 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;
475
527
  return {
476
528
  accept: n ? t.filter((e) => n.includes(e)) : t,
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
529
+ maxCount: Math.min(e.maxCount ?? T.maxCount, this.policy?.maxCount ?? T.maxCount, T.maxCount),
530
+ maxBytes: e.maxBytes ?? T.maxBytes,
531
+ maxEdge: e.maxEdge ?? (r ? E.maxEdge : T.maxEdge),
532
+ ...e.targetBytes || r ? { targetBytes: e.targetBytes ?? E.targetBytes } : {}
480
533
  };
481
534
  }
482
535
  retryable() {
@@ -492,10 +545,11 @@ var O = class {
492
545
  this.policy = {
493
546
  enabled: e.enabled === !0,
494
547
  ...Array.isArray(e.accept) ? { accept: e.accept.filter((e) => typeof e == "string") } : {},
495
- ...Number.isInteger(e.maxCount) ? { maxCount: e.maxCount } : {}
548
+ ...Number.isInteger(e.maxCount) ? { maxCount: e.maxCount } : {},
549
+ ...e.vision === !0 ? { vision: !0 } : {}
496
550
  };
497
551
  } catch (e) {
498
- e instanceof S && e.status === 404 && (this.policy = { enabled: !1 });
552
+ e instanceof M && e.status === 404 && (this.policy = { enabled: !1 });
499
553
  } finally {
500
554
  this.loadingPolicy = null;
501
555
  }
@@ -527,7 +581,7 @@ var O = class {
527
581
  size: r.size,
528
582
  status: "preparing",
529
583
  progress: 0
530
- }, o = t.includes(i) ? b(r) : void 0;
584
+ }, o = t.includes(i) ? k(r) : void 0;
531
585
  o && (a.previewUrl = o, this.previews.add(o)), this.state.uploads.push(a);
532
586
  let s = this.state.uploads.filter((e) => e.status !== "failed" || e.error === "upload").length;
533
587
  t.includes(i) ? s > n ? this.refuse(a, "count") : (this.files.set(a.id, { file: r }), this.startUpload(a.id)) : this.refuse(a, "type");
@@ -536,7 +590,7 @@ var O = class {
536
590
  }
537
591
  removeUpload(e) {
538
592
  let t = this.upload(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) }));
593
+ t && (t.attachment && this.discard(t.attachment), this.files.get(e)?.controller?.abort(), this.files.delete(e), A(t.previewUrl), t.previewUrl && this.previews.delete(t.previewUrl), this.changed({ uploads: this.state.uploads.filter((t) => t.id !== e) }));
540
594
  }
541
595
  retryUpload(e) {
542
596
  let t = this.upload(e);
@@ -552,13 +606,14 @@ var O = class {
552
606
  error: void 0,
553
607
  message: void 0
554
608
  }), this.changed();
555
- let { maxBytes: s, maxEdge: c } = this.attachmentLimits();
609
+ let { maxBytes: s, maxEdge: c, targetBytes: l } = this.attachmentLimits();
556
610
  try {
557
- let e = this.opts.prepareImage ?? f, i;
611
+ let e = this.opts.prepareImage ?? g, i;
558
612
  try {
559
613
  i = await e(t.file, {
560
614
  maxEdge: c,
561
- contentType: n.contentType
615
+ contentType: n.contentType,
616
+ ...l ? { targetBytes: l } : {}
562
617
  });
563
618
  } catch {
564
619
  i = {
@@ -573,9 +628,11 @@ var O = class {
573
628
  }
574
629
  Object.assign(n, {
575
630
  status: "uploading",
576
- size: i.blob.size
631
+ size: i.blob.size,
632
+ name: m(n.name, i.contentType),
633
+ contentType: i.contentType
577
634
  }), this.changed();
578
- let l = await r(i.blob, {
635
+ let u = await r(i.blob, {
579
636
  name: n.name,
580
637
  contentType: i.contentType,
581
638
  size: i.blob.size,
@@ -587,23 +644,23 @@ var O = class {
587
644
  }
588
645
  });
589
646
  if (!a() || o.signal.aborted) {
590
- l && typeof l.ref == "string" && l.ref && this.discard(l);
647
+ u && typeof u.ref == "string" && u.ref && this.discard(u);
591
648
  return;
592
649
  }
593
- if (!l || typeof l.ref != "string" || !l.ref) throw Error("The upload gave no reference.");
650
+ if (!u || typeof u.ref != "string" || !u.ref) throw Error("The upload gave no reference.");
594
651
  Object.assign(n, {
595
652
  status: "ready",
596
653
  progress: 1,
597
654
  attachment: {
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 } : {}
655
+ ...u,
656
+ name: u.name || n.name,
657
+ contentType: u.contentType || i.contentType,
658
+ ...i.width && !u.width ? { width: i.width } : {},
659
+ ...i.height && !u.height ? { height: i.height } : {}
603
660
  }
604
661
  }), this.changed();
605
662
  } catch (e) {
606
- if (!a() || T(e) || o.signal.aborted) return;
663
+ if (!a() || F(e) || o.signal.aborted) return;
607
664
  this.refuse(n, "upload", e instanceof Error && e.message ? e.message : void 0), this.changed();
608
665
  }
609
666
  }
@@ -617,7 +674,7 @@ var O = class {
617
674
  for (let e of this.state.uploads) e.attachment && this.discard(e.attachment);
618
675
  for (let e of this.files.values()) e.controller?.abort();
619
676
  this.files = /* @__PURE__ */ new Map();
620
- for (let e of this.previews) x(e);
677
+ for (let e of this.previews) A(e);
621
678
  this.previews = /* @__PURE__ */ new Set();
622
679
  }
623
680
  async decide(e) {
@@ -626,7 +683,7 @@ var O = class {
626
683
  let n = this.state.pendingApprovals, r = new Map(e.map((e) => [e.approvalId, e]));
627
684
  for (let e of n) {
628
685
  let t = this.tools.get(e.toolCallId), n = r.get(e.approvalId);
629
- t && (t.state = n?.approved ? "running" : "denied"), t && n?.approved && n.allowForConversation && (t.granted = !0);
686
+ t && e.started ? (t.state = "error", t.errorText = j) : (t && (t.state = n?.approved ? "running" : "denied"), t && n?.approved && n.allowForConversation && (t.granted = !0));
630
687
  }
631
688
  if (this.changed({
632
689
  pendingApprovals: [],
@@ -634,7 +691,7 @@ var O = class {
634
691
  }), await this.turn(`/v1/conversations/${encodeURIComponent(t)}/approvals`, { decisions: e }) === "not-sent") {
635
692
  for (let e of n) {
636
693
  let t = this.tools.get(e.toolCallId);
637
- t && (t.state = "awaiting-approval", delete t.granted);
694
+ t && (t.state = "awaiting-approval", delete t.granted, delete t.errorText);
638
695
  }
639
696
  this.retryAction = () => this.decide(e), this.changed({
640
697
  pendingApprovals: n,
@@ -649,7 +706,7 @@ var O = class {
649
706
  let n = await this.json(`/v1/conversations/${encodeURIComponent(t)}/allowances/${encodeURIComponent(e)}`, { method: "DELETE" });
650
707
  return this.changed({ allowances: n.allowances ?? [] }), !0;
651
708
  } catch (t) {
652
- let n = w(t);
709
+ let n = P(t);
653
710
  return this.changed({
654
711
  allowances: this.state.allowances.filter((t) => t.toolName !== e),
655
712
  ...t?.status === 404 ? {} : { problem: n }
@@ -679,11 +736,37 @@ var O = class {
679
736
  dismissProblem() {
680
737
  this.changed({ problem: null });
681
738
  }
739
+ async continueConversation() {
740
+ let e = this.state.conversationId;
741
+ if (!e || this.state.continuing || this.busy()) return;
742
+ if (this.state.continuedIn) return this.openConversation(this.state.continuedIn);
743
+ if (!this.state.full || this.state.pendingApprovals.length) return;
744
+ let t = this.generation;
745
+ this.retryAction = null, this.changed({
746
+ continuing: !0,
747
+ problem: null
748
+ });
749
+ let n;
750
+ try {
751
+ n = (await this.json(`/v1/conversations/${encodeURIComponent(e)}/continue`, {
752
+ method: "POST",
753
+ body: JSON.stringify(this.opts.uploadFile ? { capabilities: { attachments: !0 } } : {})
754
+ })).conversationId;
755
+ } catch (e) {
756
+ if (t !== this.generation) return;
757
+ this.retryAction = () => this.continueConversation(), this.changed({
758
+ continuing: !1,
759
+ problem: P(e)
760
+ });
761
+ return;
762
+ }
763
+ t === this.generation && await this.openConversation(n);
764
+ }
682
765
  newConversation() {
683
766
  this.reset(), this.capabilitiesWanted && this.loadCapabilities();
684
767
  }
685
768
  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();
769
+ this.generation += 1, this.controller?.abort(), this.dropPhotos(), this.tools = /* @__PURE__ */ new Map(), this.retryAction = null, this.state = D(this.newConversationTakesPhotos()), this.changed();
687
770
  }
688
771
  async listConversations(e = {}) {
689
772
  let t = new URLSearchParams({
@@ -714,18 +797,26 @@ var O = class {
714
797
  t && (t.allowed = !0, e.preview && (t.preview = e.preview));
715
798
  }
716
799
  this.retryAction = null, this.dropPhotos(), this.state = {
717
- ...y(!!this.opts.uploadFile && n.attachments === !0),
800
+ ...D(!!this.opts.uploadFile && n.attachments === !0),
718
801
  conversationId: e,
719
802
  messages: i,
720
803
  pendingApprovals: r,
721
804
  allowances: n.allowances ?? [],
722
- status: r.length ? "awaiting-approval" : "idle"
805
+ status: r.length ? "awaiting-approval" : "idle",
806
+ full: n.limitReached === "conversation",
807
+ problem: n.limitReached === "conversation" ? { code: "conversation-full" } : null,
808
+ continuedFrom: n.continuedFrom ? {
809
+ id: n.continuedFrom,
810
+ title: n.title ?? ""
811
+ } : null,
812
+ handover: n.handover || null,
813
+ continuedIn: n.continuedIn ?? null
723
814
  }, this.changed();
724
815
  } catch (n) {
725
816
  if (t !== this.generation) return;
726
817
  this.retryAction = () => this.openConversation(e), this.changed({
727
818
  status: "idle",
728
- problem: w(n)
819
+ problem: P(n)
729
820
  });
730
821
  }
731
822
  }
@@ -756,11 +847,11 @@ var O = class {
756
847
  body: JSON.stringify(t),
757
848
  headers: { accept: "text/event-stream, application/json;q=0.9" },
758
849
  signal: a.signal
759
- }), !o.ok) throw await C(o);
850
+ }), !o.ok) throw await N(o);
760
851
  } catch (n) {
761
852
  if (this.controller === a && (this.controller = null), !r()) return "answered";
762
- if (this.drop(i), T(n)) return this.changed({ status: "idle" }), "not-sent";
763
- let o = w(n);
853
+ if (this.drop(i), F(n)) return this.changed({ status: "idle" }), "not-sent";
854
+ let o = P(n);
764
855
  if (o.code === "photos-refused") {
765
856
  let e = this.state.messages.at(-1);
766
857
  return e?.role === "user" && this.drop(e), this.policy = null, this.changed({
@@ -769,7 +860,7 @@ var O = class {
769
860
  canAttach: !1
770
861
  }), this.loadCapabilities(), "not-sent";
771
862
  }
772
- return this.retryAction = () => this.turn(e, t).then(() => void 0), this.changed({
863
+ return this.retryAction = () => this.turn(e, t).then(() => void 0), o.code === "conversation-full" && (this.retryAction = null, this.state.full = !0), this.changed({
773
864
  status: "idle",
774
865
  problem: o
775
866
  }), "not-sent";
@@ -778,13 +869,13 @@ var O = class {
778
869
  this.changed({ status: "streaming" });
779
870
  let s = !1, c = null, l = !1, u = /* @__PURE__ */ new Map();
780
871
  try {
781
- let e = o.body ? g(o.body, a.signal) : k(await o.text());
872
+ let e = o.body ? C(o.body, a.signal) : z(await o.text());
782
873
  for await (let t of e) {
783
874
  if (!r()) break;
784
875
  t.type === "finish" ? s = !0 : t.type === "data-turn" && (c = t.data), this.apply(t, i, u), this.changed();
785
876
  }
786
877
  } catch (e) {
787
- T(e) || (l = !0);
878
+ F(e) || (l = !0);
788
879
  } finally {
789
880
  this.controller === a && (this.controller = null);
790
881
  }
@@ -805,6 +896,10 @@ var O = class {
805
896
  }
806
897
  finishTurn(e) {
807
898
  this.settleUnfinished(!1);
899
+ for (let t of e.decisions ?? []) {
900
+ let e = this.tools.get(t.toolCallId);
901
+ e && (t.outcome === "cancelled" ? e.state = "denied" : t.outcome === "done" ? e.state !== "done" && (e.state = "done") : (e.state = "error", e.errorText = t.outcome === "unknown" ? j : t.error ?? e.errorText ?? ""));
902
+ }
808
903
  let t = e.pendingApprovals ?? [];
809
904
  for (let e of t) e.title && this.titles.set(e.toolName, e.title);
810
905
  this.keepPreviews(t);
@@ -813,7 +908,7 @@ var O = class {
813
908
  n = { code: "token-expired" };
814
909
  let e = this.session;
815
910
  e && this.opts.refreshUserToken(e.sessionId).catch(() => this.session = null);
816
- } else e.limitReached === "steps" ? n = { code: "step-limit" } : e.status === "error" && (n = e.limitReached === "conversation" ? { code: "conversation-full" } : e.limitReached === "output" ? { code: e.cut === "text" ? "answer-cut" : "too-long" } : {
911
+ } else e.limitReached === "steps" ? n = { code: "step-limit" } : e.limitReached === "conversation" ? n = { code: "conversation-full" } : e.status === "error" && (n = e.limitReached === "output" ? { code: e.cut === "text" ? "answer-cut" : "too-long" } : {
817
912
  code: "failed",
818
913
  ...e.error ? { message: e.error } : {}
819
914
  });
@@ -823,7 +918,8 @@ var O = class {
823
918
  lastTurn: e,
824
919
  allowances: e.allowances ?? [],
825
920
  problem: n,
826
- status: t.length ? "awaiting-approval" : "idle"
921
+ status: t.length ? "awaiting-approval" : "idle",
922
+ full: this.state.full || e.limitReached === "conversation"
827
923
  }), t.length && this.emit("approval-pending", { approvals: t }), this.emit("turn-end", e);
828
924
  }
829
925
  keepPreviews(e) {
@@ -928,9 +1024,9 @@ var O = class {
928
1024
  return r;
929
1025
  }
930
1026
  };
931
- async function* k(e) {
932
- yield* m(e);
1027
+ async function* z(e) {
1028
+ yield* x(e);
933
1029
  }
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 };
1030
+ export { S as a, g as c, e as d, i as f, P as i, h as l, R as n, x as o, r as p, O as r, C as s, T as t, l as u };
935
1031
 
936
- //# sourceMappingURL=core-vIc1mTTy.js.map
1032
+ //# sourceMappingURL=core-4zFtqSww.js.map