@dudko.dev/agent-web-react 0.0.41 → 0.0.42

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/index.cjs CHANGED
@@ -2333,7 +2333,18 @@ var useChatHistory = (agent, options = {}) => {
2333
2333
  const agentRef = (0, import_react2.useRef)(agent);
2334
2334
  agentRef.current = agent;
2335
2335
  const baseRef = (0, import_react2.useRef)(void 0);
2336
+ const loadingRef = (0, import_react2.useRef)(void 0);
2337
+ if (loadingRef.current && agent.messages !== loadingRef.current) loadingRef.current = void 0;
2336
2338
  const staleRef = (0, import_react2.useRef)(void 0);
2339
+ const queueRef = (0, import_react2.useRef)(Promise.resolve());
2340
+ const serially = (0, import_react2.useCallback)((task) => {
2341
+ const next = queueRef.current.then(task);
2342
+ queueRef.current = next.catch(() => {
2343
+ });
2344
+ return next;
2345
+ }, []);
2346
+ const baseOf = (id) => baseRef.current?.id === id ? baseRef.current : void 0;
2347
+ const unsavedHere = (messages2, id) => !loadingRef.current && ownUnsaved(messages2, baseOf(id));
2337
2348
  const refresh = (0, import_react2.useCallback)(async () => {
2338
2349
  try {
2339
2350
  setChats(await store.list());
@@ -2343,19 +2354,79 @@ var useChatHistory = (agent, options = {}) => {
2343
2354
  const newChat = (0, import_react2.useCallback)(() => {
2344
2355
  agentRef.current.loadChat({ sessionId: newChatId(), messages: [] });
2345
2356
  }, []);
2346
- const select = (0, import_react2.useCallback)(
2357
+ const show = (0, import_react2.useCallback)((record) => {
2358
+ createdRef.current.set(record.id, record.createdAt);
2359
+ baseRef.current = {
2360
+ id: record.id,
2361
+ known: new Set(record.messages),
2362
+ updatedAt: record.updatedAt
2363
+ };
2364
+ loadingRef.current ??= agentRef.current.messages;
2365
+ agentRef.current.loadChat({
2366
+ sessionId: record.id,
2367
+ messages: record.messages,
2368
+ totalUsage: record.totalUsage
2369
+ });
2370
+ }, []);
2371
+ const saveOpen = (0, import_react2.useCallback)(
2347
2372
  async (id) => {
2348
- const record = await store.get(id).catch(() => void 0);
2349
- if (!record) return;
2350
- createdRef.current.set(id, record.createdAt);
2351
- baseRef.current = { id, known: new Set(record.messages), updatedAt: record.updatedAt };
2352
- agentRef.current.loadChat({
2353
- sessionId: record.id,
2354
- messages: record.messages,
2355
- totalUsage: record.totalUsage
2356
- });
2373
+ const current = agentRef.current;
2374
+ if (current.sessionId !== id || current.isRunning) return;
2375
+ const { messages: messages2, totalUsage: totalUsage2, remote: remote2 } = current;
2376
+ if (messages2.length === 0) return;
2377
+ const base2 = baseOf(id);
2378
+ if (loadingRef.current || unchangedSince(messages2, base2)) return;
2379
+ if (remote2 && !ownUnsaved(messages2, base2)) return;
2380
+ const now = Date.now();
2381
+ const createdAt = createdRef.current.get(id) ?? now;
2382
+ createdRef.current.set(id, createdAt);
2383
+ const view = {
2384
+ id,
2385
+ title: chatTitle(messages2, textsRef.current),
2386
+ createdAt,
2387
+ updatedAt: now,
2388
+ messages: messages2,
2389
+ totalUsage: totalUsage2
2390
+ };
2391
+ const { record, merged } = await saveChat(store, view, base2, textsRef.current);
2392
+ baseRef.current = {
2393
+ id,
2394
+ known: new Set(merged ? record.messages : messages2),
2395
+ updatedAt: record.updatedAt
2396
+ };
2397
+ await refresh();
2398
+ if (merged && agentRef.current.sessionId === id) show(record);
2357
2399
  },
2358
- [store]
2400
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2401
+ [store, show]
2402
+ );
2403
+ const reread = (0, import_react2.useCallback)(
2404
+ (id) => serially(async () => {
2405
+ const settled = () => {
2406
+ const current = agentRef.current;
2407
+ if (current.sessionId !== id) return false;
2408
+ if (current.isRunning) {
2409
+ if (current.remote?.running) staleRef.current = id;
2410
+ return false;
2411
+ }
2412
+ return true;
2413
+ };
2414
+ if (!settled()) return;
2415
+ if (unsavedHere(agentRef.current.messages, id)) return saveOpen(id);
2416
+ const record = await store.get(id).catch(() => void 0);
2417
+ if (!record || !settled()) return;
2418
+ if (unsavedHere(agentRef.current.messages, id)) return saveOpen(id);
2419
+ show(record);
2420
+ }),
2421
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2422
+ [store, serially, saveOpen, show]
2423
+ );
2424
+ const select = (0, import_react2.useCallback)(
2425
+ (id) => serially(async () => {
2426
+ const record = await store.get(id).catch(() => void 0);
2427
+ if (record) show(record);
2428
+ }),
2429
+ [store, serially, show]
2359
2430
  );
2360
2431
  const remove = (0, import_react2.useCallback)(
2361
2432
  async (id) => {
@@ -2399,45 +2470,34 @@ var useChatHistory = (agent, options = {}) => {
2399
2470
  const current = agentRef.current;
2400
2471
  if (key === void 0 || key !== current.sessionId) return;
2401
2472
  if (current.remote?.running) staleRef.current = key;
2402
- else if (!current.isRunning) void select(key);
2473
+ else if (!current.isRunning) void reread(key);
2403
2474
  }
2404
2475
  );
2405
- }, [enabled, store, refresh, select]);
2476
+ }, [enabled, store, refresh, reread]);
2406
2477
  const { messages, totalUsage, sessionId, isRunning, remote } = agent;
2407
2478
  (0, import_react2.useEffect)(() => {
2408
2479
  if (!enabled || loading || isRunning) return;
2409
2480
  if (staleRef.current === sessionId) {
2410
2481
  staleRef.current = void 0;
2411
- void select(sessionId);
2482
+ void reread(sessionId).catch(() => {
2483
+ });
2412
2484
  return;
2413
2485
  }
2414
2486
  if (messages.length === 0) return;
2415
- const base2 = baseRef.current?.id === sessionId ? baseRef.current : void 0;
2416
- if (unchangedSince(messages, base2)) return;
2417
- if (remote && !ownUnsaved(messages, base2)) return;
2418
- const now = Date.now();
2419
- const createdAt = createdRef.current.get(sessionId) ?? now;
2420
- createdRef.current.set(sessionId, createdAt);
2421
- const view = {
2422
- id: sessionId,
2423
- title: chatTitle(messages, textsRef.current),
2424
- createdAt,
2425
- updatedAt: now,
2426
- messages,
2427
- totalUsage
2428
- };
2429
- void (async () => {
2430
- const { record, merged } = await saveChat(store, view, base2, textsRef.current);
2431
- baseRef.current = {
2432
- id: sessionId,
2433
- known: new Set(merged ? record.messages : messages),
2434
- updatedAt: record.updatedAt
2435
- };
2436
- await refresh();
2437
- if (merged && agentRef.current.sessionId === sessionId) await select(sessionId);
2438
- })().catch(() => {
2487
+ void serially(() => saveOpen(sessionId)).catch(() => {
2439
2488
  });
2440
- }, [enabled, loading, isRunning, remote, messages, totalUsage, sessionId, store, refresh, select]);
2489
+ }, [
2490
+ enabled,
2491
+ loading,
2492
+ isRunning,
2493
+ remote,
2494
+ messages,
2495
+ totalUsage,
2496
+ sessionId,
2497
+ serially,
2498
+ saveOpen,
2499
+ reread
2500
+ ]);
2441
2501
  return { chats, currentId: sessionId, loading, select, newChat, remove };
2442
2502
  };
2443
2503