@nexface/chat 0.1.21-alpha.5 → 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.
- package/dist/BUNDLED_DEPENDENCIES.json +12 -0
- package/dist/BUNDLED_LICENSES.txt +50 -0
- package/dist/assistant-components.d.ts +1 -2
- package/dist/assistant-components.js +6 -8
- package/dist/assistant-runtime.d.ts +1 -4
- package/dist/assistant-runtime.js +5 -19
- package/dist/client.js +12 -24
- package/dist/default-chat.d.ts +1 -1
- package/dist/default-chat.js +7864 -1553
- package/dist/messages.d.ts +0 -3
- package/dist/protocol-messages.d.ts +2 -3
- package/dist/protocol-messages.js +27 -145
- package/dist/protocol-response.js +2 -2
- package/dist/react.d.ts +1 -11
- package/dist/react.js +100 -515
- package/dist/storage.d.ts +6 -3
- package/dist/storage.js +44 -50
- package/dist/styles.css +676 -1503
- package/dist/tool-result.d.ts +0 -5
- package/dist/tool-result.js +0 -31
- package/dist/types.d.ts +1 -3
- package/dist/ui/home-demo/action-bar.js +2 -13
- package/dist/ui/home-demo/greeting.d.ts +1 -3
- package/dist/ui/home-demo/greeting.js +2 -2
- package/dist/ui/home-demo/suggestions.d.ts +4 -2
- package/dist/ui/home-demo/suggestions.js +4 -5
- package/dist/ui/home-demo/trace.js +3 -2
- package/dist/ui/markdown-text.d.ts +1 -6
- package/dist/ui/markdown-text.js +2 -39
- package/dist/ui/thread-list.aui.d.ts +1 -2
- package/dist/ui/thread-list.aui.js +2 -6
- package/package.json +3 -4
- package/dist/fonts/Inter-OFL.txt +0 -93
- package/dist/fonts/inter-medium.ttf +0 -0
- package/dist/session-coordinator.d.ts +0 -23
- package/dist/session-coordinator.js +0 -139
- package/dist/session-history.d.ts +0 -29
- package/dist/session-history.js +0 -282
- package/dist/shared/text-only-reply.d.ts +0 -6
- package/dist/shared/text-only-reply.js +0 -10
- package/dist/shared/tool-presentation.d.ts +0 -18
- package/dist/shared/tool-presentation.js +0 -5
- package/dist/shared/tool-result-markdown.d.ts +0 -15
- package/dist/shared/tool-result-markdown.js +0 -55
- package/dist/shared/tool-result-reference.d.ts +0 -2
- package/dist/shared/tool-result-reference.js +0 -5
- package/dist/tool-presentation.d.ts +0 -6
- package/dist/tool-presentation.js +0 -113
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 {
|
|
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
|
-
|
|
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
|
-
|
|
176
|
-
|
|
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
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
215
|
-
|
|
136
|
+
else {
|
|
137
|
+
store.sessions = [...restored, ...store.sessions];
|
|
216
138
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (store.
|
|
232
|
-
|
|
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
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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
|
-
|
|
264
|
-
|
|
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.
|
|
359
|
-
|
|
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
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
return
|
|
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
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
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
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
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
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
store.
|
|
484
|
-
|
|
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
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
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
|
-
|
|
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()
|
|
382
|
+
messages: projectMessages(client.getMessages()),
|
|
730
383
|
status: store.switching
|
|
731
384
|
? "switching-model"
|
|
732
|
-
:
|
|
733
|
-
?
|
|
734
|
-
|
|
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
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
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
|
-
|
|
776
|
-
|
|
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"
|
|
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"
|
|
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(
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
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);
|