@nexface/chat 0.1.21-alpha.4 → 0.1.21-alpha.6

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.
Files changed (48) hide show
  1. package/dist/BUNDLED_DEPENDENCIES.json +12 -0
  2. package/dist/BUNDLED_LICENSES.txt +50 -0
  3. package/dist/assistant-components.d.ts +1 -2
  4. package/dist/assistant-components.js +6 -8
  5. package/dist/assistant-runtime.d.ts +1 -4
  6. package/dist/assistant-runtime.js +5 -19
  7. package/dist/client.js +9 -27
  8. package/dist/default-chat.d.ts +1 -1
  9. package/dist/default-chat.js +7864 -1552
  10. package/dist/messages.d.ts +0 -3
  11. package/dist/protocol-messages.d.ts +2 -3
  12. package/dist/protocol-messages.js +27 -145
  13. package/dist/protocol-response.js +2 -2
  14. package/dist/react.d.ts +1 -11
  15. package/dist/react.js +100 -515
  16. package/dist/storage.d.ts +6 -3
  17. package/dist/storage.js +44 -50
  18. package/dist/styles.css +676 -1503
  19. package/dist/tool-result.d.ts +0 -5
  20. package/dist/tool-result.js +0 -31
  21. package/dist/types.d.ts +1 -3
  22. package/dist/ui/home-demo/action-bar.js +2 -13
  23. package/dist/ui/home-demo/greeting.d.ts +1 -3
  24. package/dist/ui/home-demo/greeting.js +2 -2
  25. package/dist/ui/home-demo/suggestions.d.ts +4 -2
  26. package/dist/ui/home-demo/suggestions.js +4 -5
  27. package/dist/ui/home-demo/trace.js +3 -2
  28. package/dist/ui/markdown-text.d.ts +1 -6
  29. package/dist/ui/markdown-text.js +2 -39
  30. package/dist/ui/thread-list.aui.d.ts +1 -2
  31. package/dist/ui/thread-list.aui.js +2 -6
  32. package/package.json +3 -4
  33. package/dist/fonts/Inter-OFL.txt +0 -93
  34. package/dist/fonts/inter-medium.ttf +0 -0
  35. package/dist/session-coordinator.d.ts +0 -23
  36. package/dist/session-coordinator.js +0 -139
  37. package/dist/session-history.d.ts +0 -29
  38. package/dist/session-history.js +0 -282
  39. package/dist/shared/text-only-reply.d.ts +0 -6
  40. package/dist/shared/text-only-reply.js +0 -10
  41. package/dist/shared/tool-presentation.d.ts +0 -17
  42. package/dist/shared/tool-presentation.js +0 -5
  43. package/dist/shared/tool-result-markdown.d.ts +0 -15
  44. package/dist/shared/tool-result-markdown.js +0 -55
  45. package/dist/shared/tool-result-reference.d.ts +0 -2
  46. package/dist/shared/tool-result-reference.js +0 -5
  47. package/dist/tool-presentation.d.ts +0 -6
  48. package/dist/tool-presentation.js +0 -112
package/dist/react.js CHANGED
@@ -7,9 +7,7 @@ import { PageConfirmationProvider, } from "./page-confirmation.js";
7
7
  import { createChatClient, getClientController, ClientError, } from "./client.js";
8
8
  import { attachmentContent } from "./attachments.js";
9
9
  import { projectMessages } from "./protocol-messages.js";
10
- import { encodeSession } from "./storage.js";
11
- import { SessionHistory, sessionLockedError } from "./session-history.js";
12
- import { getToolPresentationController, } from "./shared/tool-presentation.js";
10
+ import { chatStorageKey, readChatStorage, writeChatStorage } from "./storage.js";
13
11
  export function isChatError(error) {
14
12
  const value = error;
15
13
  return (!!value &&
@@ -17,34 +15,6 @@ export function isChatError(error) {
17
15
  typeof value.code === "string" &&
18
16
  typeof value.message === "string");
19
17
  }
20
- function persistenceSnapshot(session) {
21
- return encodeSession({
22
- id: session.id,
23
- ...(session.title ? { title: session.title } : {}),
24
- ...(getClientController(session.client).appendOnly ? { appendOnly: true } : {}),
25
- messages: [...projectMessages(session.client.getMessages())],
26
- });
27
- }
28
- const historyStates = new WeakMap();
29
- export function isChatHistoryReady(chat) {
30
- return historyStates.get(chat)?.loading === false;
31
- }
32
- export function isSessionOccupied(chat, id) {
33
- return historyStates.get(chat)?.occupied.has(id) ?? false;
34
- }
35
- export function isSessionOperationPending(chat) {
36
- return historyStates.get(chat)?.pending ?? false;
37
- }
38
- export function watchHistorySidebar(chat) {
39
- return historyStates.get(chat)?.history.coordinator.watch();
40
- }
41
- export function historyNotice(chat) {
42
- return historyStates.get(chat)?.notice;
43
- }
44
- const commandResults = new WeakMap();
45
- export function sessionCommandResult(chat) {
46
- return commandResults.get(chat.sendMessage);
47
- }
48
18
  const sendGuards = new WeakMap();
49
19
  const appendOnlySessions = new WeakMap();
50
20
  const chatsWithoutRecovery = new WeakSet();
@@ -59,20 +29,6 @@ export function assertChatSendable(chat, sessionId) {
59
29
  sendGuards.get(chat.sendMessage)?.(sessionId);
60
30
  }
61
31
  const ChatContext = createContext(null);
62
- const ToolPresentationContext = createContext(null);
63
- function ToolPresentationProvider({ controller, report, children, }) {
64
- const [, changed] = useReducer((revision) => revision + 1, 0);
65
- useLayoutEffect(() => {
66
- const unsubscribe = controller?.subscribe(changed);
67
- // Child bindings may have registered before this provider's layout effect.
68
- changed();
69
- return unsubscribe;
70
- }, [controller]);
71
- return (_jsx(ToolPresentationContext.Provider, { value: { controller, report }, children: children }));
72
- }
73
- export function useToolPresentations() {
74
- return useContext(ToolPresentationContext);
75
- }
76
32
  const createId = () => globalThis.crypto.randomUUID();
77
33
  const textOf = (message) => message.parts
78
34
  .filter((part) => part.type === "text")
@@ -128,7 +84,6 @@ export function NexfaceProvider(props) {
128
84
  id: saved?.id ?? controller.id,
129
85
  client,
130
86
  ...(saved?.title ? { title: saved.title } : {}),
131
- persisted: Boolean(saved),
132
87
  unsubscribe: client.subscribe(changed),
133
88
  };
134
89
  }, [props.agent, props.run, toolRuntime, changed]);
@@ -139,14 +94,7 @@ export function NexfaceProvider(props) {
139
94
  activeId: first.id,
140
95
  loaded: false,
141
96
  switching: false,
142
- history: new SessionHistory(),
143
- tail: Promise.resolve(),
144
- pending: 0,
145
- admitting: false,
146
- admissionCancelled: false,
147
- suspended: false,
148
- intendedId: first.id,
149
- occupied: new Set(),
97
+ version: { value: null },
150
98
  };
151
99
  }
152
100
  const store = storeRef.current;
@@ -158,7 +106,6 @@ export function NexfaceProvider(props) {
158
106
  return session;
159
107
  };
160
108
  const busy = () => store.switching ||
161
- store.admitting ||
162
109
  store.sessions.some((session) => getClientController(session.client).busy && session.client.getStatus() !== "waiting");
163
110
  const assertIdle = (code = "CHAT_RUN_ACTIVE") => {
164
111
  if (busy())
@@ -172,381 +119,114 @@ export function NexfaceProvider(props) {
172
119
  if (active().client.getStatus() === "waiting")
173
120
  active().client.cancel();
174
121
  };
175
- const storageFailure = (cause) => {
176
- const original = asError(cause);
177
- const error = original.code.startsWith("CHAT_") || original.code === "UNKNOWN_SESSION"
178
- ? original
179
- : new ClientError("CHAT_STORAGE_UNAVAILABLE", `Chat history could not be updated. ${original.message}`);
180
- if (store.error?.code !== error.code || store.error.message !== error.message) {
181
- store.error = error;
182
- changed();
183
- }
184
- };
185
- const refreshHistory = (initial = false) => {
186
- if (!store.loaded || !mounted.current || (store.pending && !initial))
122
+ useEffect(() => {
123
+ if (store.loaded)
187
124
  return;
188
- try {
189
- const saved = store.history.list();
190
- const ids = new Set(saved.map((item) => item.id));
191
- const previous = store.sessions;
192
- const records = saved.map((item) => {
193
- const existing = previous.find((session) => session.id === item.id);
194
- if (existing) {
195
- if (existing.id !== store.activeId &&
196
- !store.history.coordinator.owns(existing.id) &&
197
- !store.history.unchangedSinceSaved(existing.id) &&
198
- JSON.stringify(persistenceSnapshot(existing)) !== JSON.stringify(encodeSession(item))) {
199
- release(existing);
200
- return createSessionValue(item);
201
- }
202
- existing.persisted = true;
203
- return existing;
204
- }
205
- return createSessionValue(item);
206
- });
207
- const local = previous.filter((item) => !ids.has(item.id) && (!item.persisted || item.id === store.activeId));
208
- for (const item of previous)
209
- if (!ids.has(item.id) && !records.includes(item) && !local.includes(item))
210
- release(item);
211
- store.sessions = [...records, ...local];
212
- changed();
125
+ store.loaded = true;
126
+ const saved = readChatStorage(chatStorageKey, store.version);
127
+ if (!saved)
128
+ return;
129
+ const restored = saved.sessions.map(createSessionValue);
130
+ const latest = restored.at(-1);
131
+ if (latest.client.getMessages().length === 0) {
132
+ store.sessions.forEach(release);
133
+ store.sessions = restored;
134
+ store.activeId = latest.id;
213
135
  }
214
- catch (cause) {
215
- storageFailure(cause);
136
+ else {
137
+ store.sessions = [...restored, ...store.sessions];
216
138
  }
217
- };
218
- const ready = () => (store.ready ??= (async () => {
219
- await store.history.initialize();
220
- await store.history.open(store.activeId, true);
221
- store.loaded = true;
222
- refreshHistory(true);
223
- })());
224
- const saveSession = async (session) => {
225
- if (!store.history.coordinator.owns(session.id))
226
- throw sessionLockedError();
227
- const snapshot = persistenceSnapshot(session);
228
- await store.history.save(snapshot);
229
- if (snapshot.messages.length || snapshot.title)
230
- session.persisted = true;
231
- if (store.error?.code.startsWith("CHAT_STORAGE")) {
232
- delete store.error;
139
+ changed();
140
+ }, []);
141
+ const persist = () => {
142
+ if (store.warning)
143
+ return;
144
+ const stored = {
145
+ activeSessionId: store.activeId,
146
+ sessions: store.sessions.map((session) => ({
147
+ id: session.id,
148
+ ...(session.title ? { title: session.title } : {}),
149
+ ...(getClientController(session.client).appendOnly ? { appendOnly: true } : {}),
150
+ messages: [...projectMessages(session.client.getMessages())],
151
+ })),
152
+ };
153
+ if (writeChatStorage(chatStorageKey, stored, store.version) === "conflict") {
154
+ store.warning =
155
+ "Chat history was updated in another tab. Saving is paused in this tab. Copy any new messages you want to keep, then refresh to load the latest history.";
233
156
  changed();
234
157
  }
235
158
  };
236
- const persist = async () => {
237
- await ready();
238
- const session = active();
239
- await saveSession(session);
240
- };
241
- const enqueue = (operation) => {
242
- store.pending++;
243
- const result = store.tail.then(async () => {
244
- await ready();
245
- if (!mounted.current)
246
- throw new ClientError("CHAT_SESSION_SWITCH_BLOCKED", "The chat was unmounted before the operation started.");
247
- await operation();
248
- });
249
- commandResults.set(sendMessage, result);
250
- store.tail = result.catch(storageFailure).finally(() => {
251
- store.pending--;
252
- if (!store.pending) {
253
- store.intendedId = store.activeId;
254
- refreshHistory();
255
- }
256
- changed();
257
- });
258
- changed();
259
- return result;
260
- };
159
+ useEffect(() => {
160
+ const timer = setTimeout(persist, busy() ? 250 : 0);
161
+ return () => clearTimeout(timer);
162
+ });
261
163
  useEffect(() => {
262
164
  mounted.current = true;
263
- let disposed = false;
264
- let reconciling = false;
265
- let lifecycle = 0;
266
- const reconcile = () => {
267
- if (disposed || reconciling)
268
- return;
269
- reconciling = true;
270
- queueMicrotask(() => {
271
- reconciling = false;
272
- if (!disposed) {
273
- refreshHistory();
274
- void store.history.coordinator
275
- .occupied()
276
- .then((occupied) => {
277
- if (disposed)
278
- return;
279
- const changedLocks = [...occupied].sort().join("\0") !== [...store.occupied].sort().join("\0");
280
- store.occupied = occupied;
281
- if (changedLocks)
282
- changed();
283
- })
284
- .catch(storageFailure);
285
- }
286
- });
287
- };
288
- store.history.coordinator.connect(reconcile);
289
- store.history.onEvict = (count) => {
290
- store.notice = `Removed ${count} older conversation${count === 1 ? "" : "s"} to free storage space.`;
291
- changed();
292
- };
293
- void ready().then(reconcile).catch(storageFailure);
294
- const flush = () => {
295
- void store.tail.then(persist).catch(storageFailure);
296
- };
297
- const onHide = (event) => {
298
- if (!event.persisted) {
299
- flush();
300
- return;
301
- }
302
- const ticket = ++lifecycle;
303
- store.suspended = true;
304
- store.admissionCancelled = true;
305
- active().client.cancel();
306
- changed();
307
- void store.tail
308
- .then(persist)
309
- .catch(storageFailure)
310
- .finally(() => {
311
- if (ticket === lifecycle)
312
- store.history.coordinator.release(store.activeId);
313
- });
314
- };
315
- const onShow = (event) => {
316
- reconcile();
317
- if (!event.persisted && !store.suspended)
318
- return;
319
- lifecycle++;
320
- store.suspended = false;
321
- void enqueue(async () => {
322
- if (store.history.coordinator.owns(store.activeId))
323
- return;
324
- const previous = active();
325
- try {
326
- const saved = await store.history.open(previous.id, !previous.persisted);
327
- if (saved && !store.history.canReuse(previous.id)) {
328
- const replacement = createSessionValue(saved);
329
- store.history.adoptRestored(persistenceSnapshot(replacement));
330
- release(previous);
331
- store.sessions = store.sessions.map((item) => (item === previous ? replacement : item));
332
- }
333
- }
334
- catch (cause) {
335
- if (asError(cause).code !== "CHAT_SESSION_LOCKED" &&
336
- asError(cause).code !== "UNKNOWN_SESSION")
337
- throw cause;
338
- const fresh = createSessionValue();
339
- await store.history.open(fresh.id, true);
340
- store.sessions.push(fresh);
341
- store.activeId = fresh.id;
342
- store.notice = "The previous conversation is unavailable. A new conversation was opened.";
343
- }
344
- }).catch(() => undefined);
345
- };
346
- window.addEventListener("storage", reconcile);
347
- window.addEventListener("focus", reconcile);
348
- window.addEventListener("pageshow", onShow);
349
- window.addEventListener("pagehide", onHide);
350
- // Reconcile missed release notifications after a tab crashes; no lock stealing.
351
- const timer = window.setInterval(() => {
352
- if (store.history.coordinator.visible && document.visibilityState !== "hidden")
353
- reconcile();
354
- }, 3000);
165
+ const flush = () => persist();
166
+ window.addEventListener("pagehide", flush);
355
167
  return () => {
356
- disposed = true;
357
168
  mounted.current = false;
358
- window.clearInterval(timer);
359
- window.removeEventListener("storage", reconcile);
360
- window.removeEventListener("focus", reconcile);
361
- window.removeEventListener("pageshow", onShow);
362
- window.removeEventListener("pagehide", onHide);
169
+ window.removeEventListener("pagehide", flush);
170
+ flush();
363
171
  queueMicrotask(() => {
364
- if (mounted.current)
365
- return;
366
- const close = () => {
172
+ if (!mounted.current)
367
173
  store.sessions.forEach(release);
368
- store.history.coordinator.close();
369
- };
370
- try {
371
- if (!store.pending &&
372
- !getClientController(active().client).busy &&
373
- store.history.isSaved(persistenceSnapshot(active()))) {
374
- close();
375
- return;
376
- }
377
- }
378
- catch {
379
- /* Fall through to the best-effort final save. */
380
- }
381
- store.sessions.forEach((session) => session.client.cancel());
382
- void store.tail
383
- .then(persist)
384
- .catch(() => undefined)
385
- .finally(() => {
386
- if (mounted.current)
387
- return;
388
- close();
389
- });
390
174
  });
391
175
  };
392
176
  }, []);
393
- useEffect(() => {
394
- if (!store.loaded || store.pending || store.admitting)
395
- return;
396
- const timer = setTimeout(() => {
397
- void store.tail.then(persist).catch(storageFailure);
398
- }, busy() ? 250 : 0);
399
- return () => clearTimeout(timer);
400
- });
401
- const activate = async (target) => {
402
- if (target.id === store.activeId)
403
- return;
404
- const previous = active();
405
- const saved = await store.history.open(target.id, !target.persisted);
406
- try {
407
- await saveSession(previous);
408
- if (!mounted.current)
409
- throw new ClientError("CHAT_SESSION_SWITCH_BLOCKED", "The chat was unmounted before the conversation opened.");
410
- const wasWaiting = previous.client.getStatus() === "waiting";
411
- cancelWaiting();
412
- if (wasWaiting && getClientController(previous.client).busy)
413
- await new Promise((resolve) => {
414
- const done = () => {
415
- if (!getClientController(previous.client).busy) {
416
- unsubscribe();
417
- resolve();
418
- }
419
- };
420
- const unsubscribe = previous.client.subscribe(done);
421
- done();
422
- });
423
- // Cancellation can add a final outcome; persist it before releasing ownership.
424
- await saveSession(previous);
425
- if (saved && !store.history.canReuse(target.id)) {
426
- const replacement = createSessionValue(saved);
427
- store.history.adoptRestored(persistenceSnapshot(replacement));
428
- release(target);
429
- store.sessions = store.sessions.map((item) => (item === target ? replacement : item));
430
- }
431
- store.activeId = target.id;
432
- delete store.error;
433
- store.history.coordinator.release(previous.id);
434
- }
435
- catch (cause) {
436
- store.history.coordinator.release(target.id);
437
- throw cause;
438
- }
439
- };
440
177
  const createSession = useCallback(() => {
441
178
  assertIdle("CHAT_SESSION_CREATE_BLOCKED");
179
+ cancelWaiting();
442
180
  const latest = store.sessions.at(-1);
443
- const target = latest.client.getMessages().length === 0 && !latest.persisted ? latest : createSessionValue();
444
- store.intendedId = target.id;
445
- if (target.id === store.activeId) {
446
- commandResults.delete(sendMessage);
447
- return target.id;
181
+ if (latest.client.getMessages().length === 0) {
182
+ store.activeId = latest.id;
183
+ delete store.error;
184
+ changed();
185
+ return latest.id;
448
186
  }
449
- void enqueue(async () => {
450
- const created = !store.sessions.includes(target);
451
- if (created)
452
- store.sessions.push(target);
453
- try {
454
- await activate(target);
455
- }
456
- catch (cause) {
457
- if (created && store.activeId !== target.id && !target.persisted) {
458
- store.sessions = store.sessions.filter((item) => item !== target);
459
- release(target);
460
- }
461
- throw cause;
462
- }
463
- }).catch(() => undefined);
464
- return target.id;
187
+ const session = createSessionValue();
188
+ store.sessions.push(session);
189
+ store.activeId = session.id;
190
+ delete store.error;
191
+ changed();
192
+ return session.id;
465
193
  }, [createSessionValue]);
466
194
  const switchSession = (id) => {
467
195
  assertIdle("CHAT_SESSION_SWITCH_BLOCKED");
468
- if (store.loaded && !store.pending)
469
- find(id);
470
- store.intendedId = id;
471
- void enqueue(async () => {
472
- await activate(find(id));
473
- }).catch(() => undefined);
196
+ find(id);
197
+ if (store.activeId === id)
198
+ return;
199
+ cancelWaiting();
200
+ store.activeId = id;
201
+ delete store.error;
202
+ changed();
474
203
  };
475
204
  const deleteSession = (id) => {
476
205
  assertIdle();
477
- if (store.loaded && !store.pending)
478
- find(id);
479
- void enqueue(async () => {
480
- const session = find(id);
481
- await store.history.remove([id]);
482
- release(session);
483
- store.sessions = store.sessions.filter((item) => item !== session);
484
- store.history.coordinator.release(id);
485
- if (store.activeId === id) {
486
- const first = createSessionValue();
487
- await store.history.open(first.id, true);
488
- store.sessions.push(first);
489
- store.activeId = first.id;
490
- store.intendedId = first.id;
491
- }
492
- delete store.error;
493
- }).catch(() => undefined);
206
+ const session = find(id);
207
+ release(session);
208
+ store.sessions = store.sessions.filter((item) => item !== session);
209
+ if (!store.sessions.length)
210
+ store.sessions.push(createSessionValue());
211
+ if (store.activeId === id)
212
+ store.activeId = store.sessions[0].id;
213
+ changed();
494
214
  };
495
215
  const clearSession = (id = store.activeId) => {
496
216
  assertIdle();
497
- if (store.loaded && !store.pending)
498
- find(id);
499
- void enqueue(async () => {
500
- const previous = find(id);
501
- await store.history.remove([id]);
502
- const session = createSessionValue();
503
- if (previous.title)
504
- session.title = previous.title;
505
- await store.history.open(session.id, true);
506
- try {
507
- if (session.title)
508
- await saveSession(session);
509
- }
510
- finally {
511
- if (store.activeId !== id)
512
- store.history.coordinator.release(session.id);
513
- }
514
- release(previous);
515
- store.history.coordinator.release(id);
516
- store.sessions = store.sessions.map((item) => (item === previous ? session : item));
517
- if (store.activeId === id) {
518
- store.activeId = session.id;
519
- store.intendedId = session.id;
520
- }
521
- delete store.error;
522
- }).catch(() => undefined);
217
+ const previous = find(id);
218
+ const session = createSessionValue();
219
+ if (previous.title)
220
+ session.title = previous.title;
221
+ release(previous);
222
+ store.sessions = store.sessions.map((item) => (item === previous ? session : item));
223
+ if (store.activeId === id)
224
+ store.activeId = session.id;
225
+ changed();
523
226
  };
524
227
  const sendMessage = useCallback(async (text, attachments = [], revisionOf) => {
525
- if (store.suspended)
526
- throw new ClientError("CHAT_RUN_ACTIVE", "Wait for the page to resume before sending.");
527
228
  if (store.switching)
528
229
  throw new ClientError("CHAT_RUN_ACTIVE", "The model is switching.");
529
- const targetId = store.intendedId;
530
- const admission = store.tail;
531
- if (store.admitting)
532
- throw new ClientError("CHAT_RUN_ACTIVE", "A task is already starting.");
533
- store.admitting = true;
534
- store.admissionCancelled = false;
535
- changed();
536
- try {
537
- await ready();
538
- await admission;
539
- if (store.admissionCancelled)
540
- throw new DOMException("Agent task cancelled.", "AbortError");
541
- if (!mounted.current || store.activeId !== targetId)
542
- throw new ClientError("CHAT_RUN_ACTIVE", "The session has changed. Return to the original session and send again.");
543
- if (!store.history.coordinator.owns(targetId))
544
- throw sessionLockedError();
545
- }
546
- finally {
547
- store.admitting = false;
548
- changed();
549
- }
550
230
  let session = active();
551
231
  const content = text.trim();
552
232
  if (!content && !attachments.length)
@@ -577,15 +257,7 @@ export function NexfaceProvider(props) {
577
257
  if (session.client.getMessages().length > messageCount)
578
258
  delete store.completion;
579
259
  const result = await pending;
580
- if (store.sessions.includes(session) && store.history.coordinator.owns(session.id)) {
581
- try {
582
- await saveSession(session);
583
- }
584
- catch (cause) {
585
- storageFailure(cause);
586
- }
587
- store.completion = { id: createId(), sessionId: session.id, result };
588
- }
260
+ store.completion = { id: createId(), sessionId: session.id, result };
589
261
  changed();
590
262
  return result;
591
263
  }
@@ -596,14 +268,6 @@ export function NexfaceProvider(props) {
596
268
  store.sessions = store.sessions.map((item) => (item === session ? previous : item));
597
269
  previous = undefined;
598
270
  }
599
- if (store.sessions.includes(session) && store.history.coordinator.owns(session.id)) {
600
- try {
601
- await saveSession(session);
602
- }
603
- catch (cause) {
604
- storageFailure(cause);
605
- }
606
- }
607
271
  changed();
608
272
  throw error;
609
273
  }
@@ -615,10 +279,7 @@ export function NexfaceProvider(props) {
615
279
  sendGuards.set(sendMessage, (sessionId) => {
616
280
  if (store.activeId !== sessionId)
617
281
  throw new ClientError("CHAT_RUN_ACTIVE", "The session has changed. Return to the original session and send again.");
618
- if (busy() ||
619
- store.pending ||
620
- store.suspended ||
621
- (store.loaded && !store.history.coordinator.owns(sessionId)))
282
+ if (busy())
622
283
  throw new ClientError("CHAT_RUN_ACTIVE", "A task is already running. Try sending again later.");
623
284
  getClientController(active().client).assertRecoveryAvailable();
624
285
  });
@@ -657,8 +318,6 @@ export function NexfaceProvider(props) {
657
318
  confirmationControllers.set(sendMessage, {
658
319
  state: confirmationState,
659
320
  async respond(sessionId, request, approved) {
660
- if (!store.loaded)
661
- await ready();
662
321
  sendGuards.get(sendMessage)?.(sessionId);
663
322
  if (confirmationState(sessionId, request) !== "pending")
664
323
  throw new ClientError("INVALID_INPUT", "This confirmation is no longer available.");
@@ -688,12 +347,6 @@ export function NexfaceProvider(props) {
688
347
  changed();
689
348
  }
690
349
  const result = await pending;
691
- try {
692
- await saveSession(session);
693
- }
694
- catch (cause) {
695
- storageFailure(cause);
696
- }
697
350
  store.completion = { id: createId(), sessionId, result };
698
351
  }
699
352
  finally {
@@ -726,18 +379,15 @@ export function NexfaceProvider(props) {
726
379
  })
727
380
  .reverse(),
728
381
  activeSessionId: session.id,
729
- messages: projectMessages(client.getMessages(), client.getStatus() === "running" && !controller.cancelling),
382
+ messages: projectMessages(client.getMessages()),
730
383
  status: store.switching
731
384
  ? "switching-model"
732
- : store.admitting
733
- ? store.admissionCancelled
734
- ? "cancelling"
735
- : "running"
736
- : controller.cancelling
737
- ? "cancelling"
738
- : client.getStatus(),
385
+ : controller.cancelling
386
+ ? "cancelling"
387
+ : client.getStatus(),
739
388
  model: controller.agent?.model ?? { provider: "server", model: "server" },
740
389
  ...(approval ? { interrupt: approval } : {}),
390
+ ...(store.warning ? { storageWarning: store.warning } : {}),
741
391
  ...(error || store.error ? { error: error ? asError(error) : store.error } : {}),
742
392
  ...(store.completion?.sessionId === session.id
743
393
  ? { completed: { id: store.completion.id, result: store.completion.result } }
@@ -755,72 +405,27 @@ export function NexfaceProvider(props) {
755
405
  clearSession,
756
406
  clearAllSessions() {
757
407
  assertIdle();
758
- void enqueue(async () => {
759
- await store.history.remove(store.sessions.map((item) => item.id), true);
760
- store.sessions.forEach((item) => {
761
- release(item);
762
- store.history.coordinator.release(item.id);
763
- });
764
- const first = createSessionValue();
765
- await store.history.open(first.id, true);
766
- store.sessions = [first];
767
- store.activeId = first.id;
768
- store.intendedId = first.id;
769
- delete store.error;
770
- }).catch(() => undefined);
408
+ store.sessions.forEach(release);
409
+ const first = createSessionValue();
410
+ store.sessions = [first];
411
+ store.activeId = first.id;
412
+ changed();
771
413
  },
772
414
  renameSession(id, title) {
773
415
  if (!title.trim())
774
416
  throw new ClientError("INVALID_INPUT", "Session title is required.");
775
- assertIdle();
776
- if (store.loaded && !store.pending)
777
- find(id);
778
- void enqueue(async () => {
779
- const session = find(id);
780
- const temporary = !store.history.coordinator.owns(id);
781
- try {
782
- if (temporary) {
783
- const saved = await store.history.open(id, !session.persisted);
784
- if (saved) {
785
- release(session);
786
- const replacement = createSessionValue(saved);
787
- store.sessions = store.sessions.map((item) => item === session ? replacement : item);
788
- }
789
- }
790
- const current = find(id);
791
- const previous = current.title;
792
- current.title = title.trim().slice(0, 80);
793
- try {
794
- await saveSession(current);
795
- }
796
- catch (cause) {
797
- if (previous === undefined)
798
- delete current.title;
799
- else
800
- current.title = previous;
801
- throw cause;
802
- }
803
- delete store.error;
804
- }
805
- finally {
806
- if (temporary)
807
- store.history.coordinator.release(id);
808
- }
809
- }).catch(() => undefined);
417
+ find(id).title = title.trim().slice(0, 80);
418
+ changed();
810
419
  },
811
420
  cancel() {
812
- if (store.admitting) {
813
- store.admissionCancelled = true;
814
- changed();
815
- }
816
421
  active().client.cancel();
817
422
  },
818
423
  confirmInterrupt() {
819
- if (approval?.mode === "chat" && !store.pending && store.history.coordinator.owns(session.id))
424
+ if (approval?.mode === "chat")
820
425
  void client.respondToApproval(approval.id, { approved: true }).catch(() => undefined);
821
426
  },
822
427
  rejectInterrupt() {
823
- if (approval?.mode === "chat" && !store.pending && store.history.coordinator.owns(session.id))
428
+ if (approval?.mode === "chat")
824
429
  void client.respondToApproval(approval.id, { approved: false }).catch(() => undefined);
825
430
  },
826
431
  async switchModel(model) {
@@ -841,37 +446,17 @@ export function NexfaceProvider(props) {
841
446
  }
842
447
  },
843
448
  };
844
- historyStates.set(value, {
845
- history: store.history,
846
- loading: !store.loaded,
847
- pending: store.pending > 0 || store.admitting || store.suspended,
848
- occupied: store.occupied,
849
- notice: (store.error?.code.startsWith("CHAT_STORAGE") ||
850
- store.error?.code === "CHAT_HISTORY_CONFLICT" ||
851
- store.error?.code === "CHAT_SESSION_LOCKED"
852
- ? store.error.message
853
- : undefined) ?? store.notice,
854
- });
855
449
  if (controller.recoveryUnavailable)
856
450
  chatsWithoutRecovery.add(value);
857
451
  appendOnlySessions.set(value, new Set(store.sessions
858
452
  .filter(({ client }) => getClientController(client).appendOnly)
859
453
  .map(({ id }) => id)));
860
- return (_jsx(ChatContext.Provider, { value: value, children: _jsx(ToolPresentationProvider, { controller: getToolPresentationController(toolRuntime), report: (cause, sessionId, toolCallId) => reportError({
861
- cause,
862
- sessionId,
863
- toolCallId,
864
- code: "CHAT_TOOL_RENDER_FAILED",
865
- }, onError.current), children: _jsx(ToolsProvider, { toolRuntime: toolRuntime, webMcp: webMcp, children: _jsx(PageConfirmationProvider, { interrupt: approval, onConfirm: () => {
866
- if (store.pending || !store.history.coordinator.owns(store.activeId))
867
- return;
868
- return getClientController(active().client).confirmPage();
869
- }, onBindingRemoved: () => getClientController(active().client).invalidate("Page confirmation entry was removed."), onBindingRegistration: (name, registered) => {
870
- if (registered)
871
- pageTools.current.add(name);
872
- else
873
- pageTools.current.delete(name);
874
- }, children: children }) }) }) }));
454
+ return (_jsx(ChatContext.Provider, { value: value, children: _jsx(ToolsProvider, { toolRuntime: toolRuntime, webMcp: webMcp, children: _jsx(PageConfirmationProvider, { interrupt: approval, onConfirm: () => getClientController(active().client).confirmPage(), onBindingRemoved: () => getClientController(active().client).invalidate("Page confirmation entry was removed."), onBindingRegistration: (name, registered) => {
455
+ if (registered)
456
+ pageTools.current.add(name);
457
+ else
458
+ pageTools.current.delete(name);
459
+ }, children: children }) }) }));
875
460
  }
876
461
  export function useChat() {
877
462
  const chat = useContext(ChatContext);