@ghuri/ai-widget 0.4.0 → 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.
package/README.md CHANGED
@@ -135,11 +135,15 @@ await chat.decide([{ approvalId, approved: true, allowForConversation: true }])
135
135
  await chat.revokeAllowance('theme_files_write') // "Ask every time": state.allowances lists what is allowed
136
136
  chat.stop(); await chat.retry(); await chat.reload()
137
137
  await chat.listConversations(); await chat.openConversation(id); chat.newConversation()
138
+ await chat.continueConversation() // a full conversation: carry the work over into a new one
138
139
  await chat.end() // logout or store switch
139
140
  ```
140
141
 
141
142
  `state.problem.code` says what went wrong in terms a UI can word: `reconnecting`, `token-expired` (offer "Continue"),
142
- `in-progress`, `conversation-full`, `throttled` (`retryAfter` seconds), `forbidden`, `store-unreachable`,
143
+ `in-progress`, `conversation-full` (no new message fits; waiting changes can still be answered, and `state.full` stays
144
+ true, so `send()` sends nothing; `continueConversation()` carries the work into a new conversation that starts with
145
+ a hand-over: `state.continuing` while it is written, then `state.continuedFrom` and `state.handover`, shown with
146
+ `handoverText()`; a continued conversation has `state.continuedIn` and is read-only), `throttled` (`retryAfter` seconds), `forbidden`, `store-unreachable`,
143
147
  `unavailable`, `step-limit` (the reply used all its steps with work unfinished; offer "Continue"), `too-long` (a
144
148
  tool call hit the output limit and was not done; offer "Continue", which sends `continueSmallerMessage`), `answer-cut` (an answer hit the output limit and is unfinished; offer "Continue"),
145
149
  `cut-off` (offer "Reload"), `failed` (`message`).
@@ -16,6 +16,11 @@ export declare const VISION_DEFAULTS: {
16
16
  readonly maxEdge: 1568;
17
17
  readonly targetBytes: 1000000;
18
18
  };
19
+ /**
20
+ * The readable part of a hand-over (`state.handover`): without its `<handover>` tags and the opening paragraph that
21
+ * tells the assistant how to read it.
22
+ */
23
+ export declare function handoverText(handover: string): string;
19
24
  /** What the user is told about a failed request. */
20
25
  export declare function problemFor(err: unknown): Problem;
21
26
  export declare class ChatClient {
@@ -119,6 +124,13 @@ export declare class ChatClient {
119
124
  /** Run the action that failed again (Try again). */
120
125
  retry(): Promise<void>;
121
126
  dismissProblem(): void;
127
+ /**
128
+ * Continue the open, full conversation in a new one: Ghuri AI writes a hand-over (a few seconds; `state.continuing`)
129
+ * and the new conversation opens with it (`state.continuedFrom`, `state.handover`). Nothing is sent to the
130
+ * assistant: the user carries on with a message. Not while a change is waiting (answer it first), and a conversation
131
+ * already continued just opens where it went on.
132
+ */
133
+ continueConversation(): Promise<void>;
122
134
  /** Start a new conversation (the next message creates it). */
123
135
  newConversation(): void;
124
136
  /** Forget the open conversation (new conversation, end). */
@@ -1,4 +1,4 @@
1
- export { ATTACHMENT_DEFAULTS, ChatClient, problemFor } from './client';
1
+ export { ATTACHMENT_DEFAULTS, ChatClient, handoverText, problemFor } from './client';
2
2
  export { messagesFromHistory } from './history';
3
3
  export { parsedOutput, splitSavedOutput, splitToolOutput, type ToolImage } from './output';
4
4
  export { prepareImage, scaledSize } from './prepare';
@@ -183,6 +183,20 @@ export interface PendingApproval {
183
183
  preview?: ChangePreview;
184
184
  /** The user may answer with "Allow for this conversation" (the product made this tool allowable). */
185
185
  allowable?: boolean;
186
+ /**
187
+ * It was confirmed before and started, but its result was lost. It never runs again: deciding it (either way) closes
188
+ * it as "outcome unknown".
189
+ */
190
+ started?: boolean;
191
+ }
192
+ /** What became of one change the user decided (Ghuri AI's `decisions` on a turn that answered changes). */
193
+ export interface DecisionOutcome {
194
+ approvalId: string;
195
+ toolCallId: string;
196
+ toolName: string;
197
+ /** `unknown`: it had started before and its result was lost; check before trying again. */
198
+ outcome: 'done' | 'failed' | 'cancelled' | 'unknown';
199
+ error?: string;
186
200
  }
187
201
  export interface Decision {
188
202
  approvalId: string;
@@ -207,7 +221,9 @@ export interface TurnResult {
207
221
  error?: string;
208
222
  /**
209
223
  * `output`: the reply was cut off at the output limit (see `cut`). `steps` (with status `ok`): the assistant used all
210
- * the steps one reply may take while it was still working; "Continue" picks the work up.
224
+ * the steps one reply may take while it was still working; "Continue" picks the work up. `conversation`: the
225
+ * conversation is full and takes no new message; with status `paused` its waiting changes can still be confirmed
226
+ * or cancelled, and with status `ok` the decided changes ran without the assistant writing a reply.
211
227
  */
212
228
  limitReached?: 'conversation' | 'turn' | 'output' | 'steps';
213
229
  /** The tools allowed for this conversation when the turn ended (absent: none). */
@@ -215,6 +231,8 @@ export interface TurnResult {
215
231
  /** With `limitReached: 'output'`: a tool call was cut (it never ran) or a text answer was cut (just unfinished). */
216
232
  cut?: 'tool-call' | 'text';
217
233
  userTokenExpired?: boolean;
234
+ /** For a turn that answered changes: what became of each one. */
235
+ decisions?: DecisionOutcome[];
218
236
  }
219
237
  /**
220
238
  * Something the user should be told, in plain words. The UI picks the wording; `retryAfter` is in seconds.
@@ -222,7 +240,7 @@ export interface TurnResult {
222
240
  * - `reconnecting`: getting a fresh session or token (no action needed)
223
241
  * - `token-expired`: the store stopped accepting the assistant mid-reply; the token was refreshed, offer "Continue"
224
242
  * - `in-progress`: the previous reply is still being written
225
- * - `conversation-full`: start a new conversation
243
+ * - `conversation-full`: no new message fits; answer any waiting change, then start a new conversation
226
244
  * - `throttled`: wait `retryAfter` seconds
227
245
  * - `forbidden`: the user may not do that in this store
228
246
  * - `store-unreachable`: the product's MCP server failed; nothing was changed
@@ -269,6 +287,22 @@ export interface ChatState {
269
287
  uploads: Upload[];
270
288
  /** Tools allowed for this conversation ("Allow for this conversation"); the bar above the composer lists them. */
271
289
  allowances: Allowance[];
290
+ /**
291
+ * The open conversation is full: it takes no new message (sending only shows `conversation-full` again), but its
292
+ * waiting changes can still be confirmed or cancelled, and it can be continued in a new one (`continueConversation`).
293
+ */
294
+ full: boolean;
295
+ /** The hand-over is being written (`continueConversation`): nothing else can be done until it ends. */
296
+ continuing: boolean;
297
+ /** The full conversation this one continues (its title is this one's too), for "Continued from …". */
298
+ continuedFrom: {
299
+ id: string;
300
+ title: string;
301
+ } | null;
302
+ /** What this conversation started with, for "What I carried over" (`handoverText()` makes it readable). */
303
+ handover: string | null;
304
+ /** Where this (full) conversation went on: it is read-only, with a link there. */
305
+ continuedIn: string | null;
272
306
  }
273
307
  /** Events for the host page (the element re-dispatches them as `ghuri:*` DOM events). */
274
308
  export interface ChatEvents {
@@ -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,19 +133,19 @@ 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
@@ -322,22 +324,31 @@ function D(e = !1) {
322
324
  lastTurn: null,
323
325
  canAttach: e,
324
326
  uploads: [],
325
- allowances: []
327
+ allowances: [],
328
+ full: !1,
329
+ continuing: !1,
330
+ continuedFrom: null,
331
+ handover: null,
332
+ continuedIn: null
326
333
  };
327
334
  }
328
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) {
329
340
  try {
330
341
  return typeof URL.createObjectURL == "function" ? URL.createObjectURL(e) : void 0;
331
342
  } catch {
332
343
  return;
333
344
  }
334
345
  }
335
- function k(e) {
346
+ function A(e) {
336
347
  try {
337
348
  e && typeof URL.revokeObjectURL == "function" && URL.revokeObjectURL(e);
338
349
  } catch {}
339
350
  }
340
- var A = 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 {
341
352
  status;
342
353
  code;
343
354
  detail;
@@ -347,16 +358,16 @@ var A = class extends Error {
347
358
  super(n || `HTTP ${e}`), this.status = e, this.code = t, this.detail = n, this.retryAfter = r, this.fields = i;
348
359
  }
349
360
  };
350
- async function j(e) {
361
+ async function N(e) {
351
362
  let t = null;
352
363
  try {
353
364
  t = await e.json();
354
365
  } catch {}
355
366
  let n = Number(e.headers.get("retry-after"));
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 : {});
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 : {});
357
368
  }
358
- function M(e) {
359
- if (!(e instanceof A)) return { code: "unavailable" };
369
+ function P(e) {
370
+ if (!(e instanceof M)) return { code: "unavailable" };
360
371
  let { status: t, code: n, detail: r } = e, i = e.fields.attachments;
361
372
  return t === 400 && Array.isArray(i) ? {
362
373
  code: "photos-refused",
@@ -369,14 +380,14 @@ function M(e) {
369
380
  ...r ? { message: r } : {}
370
381
  };
371
382
  }
372
- function N(e) {
383
+ function F(e) {
373
384
  return e instanceof DOMException ? e.name === "AbortError" : e?.name === "AbortError";
374
385
  }
375
- function P(e, t) {
386
+ function I(e, t) {
376
387
  let n = new Date(e.expiresAt).getTime();
377
388
  return Number.isNaN(n) || n - w > t;
378
389
  }
379
- function F(e) {
390
+ function L(e) {
380
391
  return {
381
392
  ...e,
382
393
  messages: e.messages.map((e) => ({
@@ -388,7 +399,7 @@ function F(e) {
388
399
  allowances: [...e.allowances]
389
400
  };
390
401
  }
391
- var I = class {
402
+ var R = class {
392
403
  opts;
393
404
  state;
394
405
  view;
@@ -411,7 +422,7 @@ var I = class {
411
422
  this.opts = {
412
423
  ...e,
413
424
  apiUrl: e.apiUrl.replace(/\/+$/, "")
414
- }, this.state = D(!1), this.view = F(this.state);
425
+ }, this.state = D(!1), this.view = L(this.state);
415
426
  }
416
427
  getState() {
417
428
  return this.view;
@@ -427,7 +438,7 @@ var I = class {
427
438
  for (let n of this.events.get(e) ?? []) n(t);
428
439
  }
429
440
  changed(e = {}) {
430
- Object.assign(this.state, e), this.view = F(this.state);
441
+ Object.assign(this.state, e), this.view = L(this.state);
431
442
  for (let e of this.listeners) e(this.view);
432
443
  }
433
444
  id(e) {
@@ -438,7 +449,7 @@ var I = class {
438
449
  }
439
450
  async ensureSession() {
440
451
  let e = this.session;
441
- return e && P(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(() => {
442
453
  this.starting = null;
443
454
  }), this.starting);
444
455
  }
@@ -450,7 +461,7 @@ var I = class {
450
461
  headers: i
451
462
  });
452
463
  if (a.status !== 401 || n) return a;
453
- let o = await j(a);
464
+ let o = await N(a);
454
465
  if (this.changed({ problem: { code: "reconnecting" } }), o.code === "user_token_expired") try {
455
466
  await this.opts.refreshUserToken(r.sessionId);
456
467
  } catch {
@@ -462,12 +473,16 @@ var I = class {
462
473
  }
463
474
  async json(e, t = {}) {
464
475
  let n = await this.request(e, t);
465
- if (!n.ok) throw await j(n);
476
+ if (!n.ok) throw await N(n);
466
477
  return await n.json();
467
478
  }
468
479
  async send(e) {
469
480
  let t = e.trim(), n = this.state.uploads.filter((e) => e.status === "ready" && e.attachment);
470
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
+ }
471
486
  for (let e of this.state.pendingApprovals) {
472
487
  let t = this.tools.get(e.toolCallId);
473
488
  t && (t.state = "denied");
@@ -493,7 +508,7 @@ var I = class {
493
508
  role: "user",
494
509
  parts: i
495
510
  });
496
- for (let e of this.state.uploads) e.status !== "ready" && k(e.previewUrl);
511
+ for (let e of this.state.uploads) e.status !== "ready" && A(e.previewUrl);
497
512
  this.files = /* @__PURE__ */ new Map(), this.changed({
498
513
  pendingApprovals: [],
499
514
  problem: null,
@@ -534,7 +549,7 @@ var I = class {
534
549
  ...e.vision === !0 ? { vision: !0 } : {}
535
550
  };
536
551
  } catch (e) {
537
- e instanceof A && e.status === 404 && (this.policy = { enabled: !1 });
552
+ e instanceof M && e.status === 404 && (this.policy = { enabled: !1 });
538
553
  } finally {
539
554
  this.loadingPolicy = null;
540
555
  }
@@ -566,7 +581,7 @@ var I = class {
566
581
  size: r.size,
567
582
  status: "preparing",
568
583
  progress: 0
569
- }, o = t.includes(i) ? O(r) : void 0;
584
+ }, o = t.includes(i) ? k(r) : void 0;
570
585
  o && (a.previewUrl = o, this.previews.add(o)), this.state.uploads.push(a);
571
586
  let s = this.state.uploads.filter((e) => e.status !== "failed" || e.error === "upload").length;
572
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");
@@ -575,7 +590,7 @@ var I = class {
575
590
  }
576
591
  removeUpload(e) {
577
592
  let t = this.upload(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) }));
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) }));
579
594
  }
580
595
  retryUpload(e) {
581
596
  let t = this.upload(e);
@@ -645,7 +660,7 @@ var I = class {
645
660
  }
646
661
  }), this.changed();
647
662
  } catch (e) {
648
- if (!a() || N(e) || o.signal.aborted) return;
663
+ if (!a() || F(e) || o.signal.aborted) return;
649
664
  this.refuse(n, "upload", e instanceof Error && e.message ? e.message : void 0), this.changed();
650
665
  }
651
666
  }
@@ -659,7 +674,7 @@ var I = class {
659
674
  for (let e of this.state.uploads) e.attachment && this.discard(e.attachment);
660
675
  for (let e of this.files.values()) e.controller?.abort();
661
676
  this.files = /* @__PURE__ */ new Map();
662
- for (let e of this.previews) k(e);
677
+ for (let e of this.previews) A(e);
663
678
  this.previews = /* @__PURE__ */ new Set();
664
679
  }
665
680
  async decide(e) {
@@ -668,7 +683,7 @@ var I = class {
668
683
  let n = this.state.pendingApprovals, r = new Map(e.map((e) => [e.approvalId, e]));
669
684
  for (let e of n) {
670
685
  let t = this.tools.get(e.toolCallId), n = r.get(e.approvalId);
671
- 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));
672
687
  }
673
688
  if (this.changed({
674
689
  pendingApprovals: [],
@@ -676,7 +691,7 @@ var I = class {
676
691
  }), await this.turn(`/v1/conversations/${encodeURIComponent(t)}/approvals`, { decisions: e }) === "not-sent") {
677
692
  for (let e of n) {
678
693
  let t = this.tools.get(e.toolCallId);
679
- t && (t.state = "awaiting-approval", delete t.granted);
694
+ t && (t.state = "awaiting-approval", delete t.granted, delete t.errorText);
680
695
  }
681
696
  this.retryAction = () => this.decide(e), this.changed({
682
697
  pendingApprovals: n,
@@ -691,7 +706,7 @@ var I = class {
691
706
  let n = await this.json(`/v1/conversations/${encodeURIComponent(t)}/allowances/${encodeURIComponent(e)}`, { method: "DELETE" });
692
707
  return this.changed({ allowances: n.allowances ?? [] }), !0;
693
708
  } catch (t) {
694
- let n = M(t);
709
+ let n = P(t);
695
710
  return this.changed({
696
711
  allowances: this.state.allowances.filter((t) => t.toolName !== e),
697
712
  ...t?.status === 404 ? {} : { problem: n }
@@ -721,6 +736,32 @@ var I = class {
721
736
  dismissProblem() {
722
737
  this.changed({ problem: null });
723
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
+ }
724
765
  newConversation() {
725
766
  this.reset(), this.capabilitiesWanted && this.loadCapabilities();
726
767
  }
@@ -761,13 +802,21 @@ var I = class {
761
802
  messages: i,
762
803
  pendingApprovals: r,
763
804
  allowances: n.allowances ?? [],
764
- 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
765
814
  }, this.changed();
766
815
  } catch (n) {
767
816
  if (t !== this.generation) return;
768
817
  this.retryAction = () => this.openConversation(e), this.changed({
769
818
  status: "idle",
770
- problem: M(n)
819
+ problem: P(n)
771
820
  });
772
821
  }
773
822
  }
@@ -798,11 +847,11 @@ var I = class {
798
847
  body: JSON.stringify(t),
799
848
  headers: { accept: "text/event-stream, application/json;q=0.9" },
800
849
  signal: a.signal
801
- }), !o.ok) throw await j(o);
850
+ }), !o.ok) throw await N(o);
802
851
  } catch (n) {
803
852
  if (this.controller === a && (this.controller = null), !r()) return "answered";
804
- if (this.drop(i), N(n)) return this.changed({ status: "idle" }), "not-sent";
805
- let o = M(n);
853
+ if (this.drop(i), F(n)) return this.changed({ status: "idle" }), "not-sent";
854
+ let o = P(n);
806
855
  if (o.code === "photos-refused") {
807
856
  let e = this.state.messages.at(-1);
808
857
  return e?.role === "user" && this.drop(e), this.policy = null, this.changed({
@@ -811,7 +860,7 @@ var I = class {
811
860
  canAttach: !1
812
861
  }), this.loadCapabilities(), "not-sent";
813
862
  }
814
- 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({
815
864
  status: "idle",
816
865
  problem: o
817
866
  }), "not-sent";
@@ -820,13 +869,13 @@ var I = class {
820
869
  this.changed({ status: "streaming" });
821
870
  let s = !1, c = null, l = !1, u = /* @__PURE__ */ new Map();
822
871
  try {
823
- let e = o.body ? C(o.body, a.signal) : L(await o.text());
872
+ let e = o.body ? C(o.body, a.signal) : z(await o.text());
824
873
  for await (let t of e) {
825
874
  if (!r()) break;
826
875
  t.type === "finish" ? s = !0 : t.type === "data-turn" && (c = t.data), this.apply(t, i, u), this.changed();
827
876
  }
828
877
  } catch (e) {
829
- N(e) || (l = !0);
878
+ F(e) || (l = !0);
830
879
  } finally {
831
880
  this.controller === a && (this.controller = null);
832
881
  }
@@ -847,6 +896,10 @@ var I = class {
847
896
  }
848
897
  finishTurn(e) {
849
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
+ }
850
903
  let t = e.pendingApprovals ?? [];
851
904
  for (let e of t) e.title && this.titles.set(e.toolName, e.title);
852
905
  this.keepPreviews(t);
@@ -855,7 +908,7 @@ var I = class {
855
908
  n = { code: "token-expired" };
856
909
  let e = this.session;
857
910
  e && this.opts.refreshUserToken(e.sessionId).catch(() => this.session = null);
858
- } 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" } : {
859
912
  code: "failed",
860
913
  ...e.error ? { message: e.error } : {}
861
914
  });
@@ -865,7 +918,8 @@ var I = class {
865
918
  lastTurn: e,
866
919
  allowances: e.allowances ?? [],
867
920
  problem: n,
868
- status: t.length ? "awaiting-approval" : "idle"
921
+ status: t.length ? "awaiting-approval" : "idle",
922
+ full: this.state.full || e.limitReached === "conversation"
869
923
  }), t.length && this.emit("approval-pending", { approvals: t }), this.emit("turn-end", e);
870
924
  }
871
925
  keepPreviews(e) {
@@ -970,9 +1024,9 @@ var I = class {
970
1024
  return r;
971
1025
  }
972
1026
  };
973
- async function* L(e) {
1027
+ async function* z(e) {
974
1028
  yield* x(e);
975
1029
  }
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 };
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 };
977
1031
 
978
- //# sourceMappingURL=core-9svu5kzm.js.map
1032
+ //# sourceMappingURL=core-4zFtqSww.js.map