@firstlovecenter/ai-chat 0.9.2 → 0.9.3

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/ui/index.js CHANGED
@@ -699,6 +699,11 @@ function AiChat({
699
699
  cancelled = true;
700
700
  };
701
701
  }, []);
702
+ useEffect(() => {
703
+ if (initialSessionId != null && activeSessionId !== initialSessionId) {
704
+ setActiveSessionId(initialSessionId);
705
+ }
706
+ }, [initialSessionId]);
702
707
  useEffect(() => {
703
708
  if (initialSessionId == null) {
704
709
  setAnswers([]);
@@ -711,13 +716,18 @@ function AiChat({
711
716
  const res = await fetch(`/api/chat/sessions/${id}`, { cache: "no-store" });
712
717
  if (cancelled) return;
713
718
  if (!res.ok) {
719
+ console.warn(
720
+ "[AiChat] hydration: GET /api/chat/sessions failed",
721
+ { id, status: res.status }
722
+ );
714
723
  setAnswers([]);
715
724
  return;
716
725
  }
717
726
  const data = await res.json();
718
727
  if (cancelled) return;
719
728
  setAnswers(messagesToAnswers(data.messages ?? []));
720
- } catch {
729
+ } catch (err) {
730
+ console.error("[AiChat] hydration threw \u2014 setting empty answers", err);
721
731
  if (!cancelled) setAnswers([]);
722
732
  } finally {
723
733
  if (!cancelled) setLoadingSession(false);
@@ -1459,6 +1469,15 @@ function updateLast(prev, updater) {
1459
1469
  next[next.length - 1] = updater(next[next.length - 1]);
1460
1470
  return next;
1461
1471
  }
1472
+ function coerceJsonField(v) {
1473
+ if (v == null) return null;
1474
+ if (typeof v !== "string") return v;
1475
+ try {
1476
+ return JSON.parse(v);
1477
+ } catch {
1478
+ return null;
1479
+ }
1480
+ }
1462
1481
  function messagesToAnswers(messages) {
1463
1482
  const out = [];
1464
1483
  let pendingUser = null;
@@ -1469,19 +1488,24 @@ function messagesToAnswers(messages) {
1469
1488
  const question = pendingUser ?? "";
1470
1489
  pendingUser = null;
1471
1490
  const blocks = [];
1472
- const stored = m.blocks ?? [];
1473
- stored.forEach((b, i) => {
1474
- const sanitised = sanitiseBlock({ ...b});
1475
- if (sanitised.kind === "paragraph_brief") {
1476
- sanitised.prose = m.prose?.[String(i)] ?? "";
1477
- }
1478
- blocks[i] = sanitised;
1479
- });
1491
+ const storedBlocks = coerceJsonField(m.blocks) ?? [];
1492
+ const storedProse = coerceJsonField(m.prose) ?? {};
1493
+ const storedError = coerceJsonField(m.errorJson);
1494
+ if (Array.isArray(storedBlocks)) {
1495
+ storedBlocks.forEach((b, i) => {
1496
+ const sanitised = sanitiseBlock({
1497
+ ...b});
1498
+ if (sanitised.kind === "paragraph_brief") {
1499
+ sanitised.prose = storedProse[String(i)] ?? "";
1500
+ }
1501
+ blocks[i] = sanitised;
1502
+ });
1503
+ }
1480
1504
  out.push({
1481
1505
  question,
1482
1506
  blocks,
1483
1507
  done: true,
1484
- error: m.errorJson ?? void 0
1508
+ error: storedError ?? void 0
1485
1509
  });
1486
1510
  }
1487
1511
  }
@@ -2377,6 +2401,15 @@ function UserChip2({ text }) {
2377
2401
  )
2378
2402
  ] });
2379
2403
  }
2404
+ function coerceJsonField2(v) {
2405
+ if (v == null) return null;
2406
+ if (typeof v !== "string") return v;
2407
+ try {
2408
+ return JSON.parse(v);
2409
+ } catch {
2410
+ return null;
2411
+ }
2412
+ }
2380
2413
  function storedToUseChat(stored) {
2381
2414
  const uiMessages = [];
2382
2415
  const blocksMap = {};
@@ -2392,15 +2425,20 @@ function storedToUseChat(stored) {
2392
2425
  if (m.role === "assistant") {
2393
2426
  const id = `assistant-${m.id}`;
2394
2427
  const blocks = [];
2395
- const stored2 = m.blocks ?? [];
2396
- stored2.forEach((b, i) => {
2397
- const sanitised = sanitiseBlock({ ...b});
2398
- if (sanitised.kind === "paragraph_brief") {
2399
- sanitised.prose = m.prose?.[String(i)] ?? "";
2400
- }
2401
- blocks[i] = sanitised;
2402
- });
2403
- const joinedProse = Object.values(m.prose ?? {}).filter(Boolean).join("\n\n");
2428
+ const storedBlocks = coerceJsonField2(m.blocks) ?? [];
2429
+ const storedProse = coerceJsonField2(m.prose) ?? {};
2430
+ const storedError = coerceJsonField2(m.errorJson);
2431
+ if (Array.isArray(storedBlocks)) {
2432
+ storedBlocks.forEach((b, i) => {
2433
+ const sanitised = sanitiseBlock({
2434
+ ...b});
2435
+ if (sanitised.kind === "paragraph_brief") {
2436
+ sanitised.prose = storedProse[String(i)] ?? "";
2437
+ }
2438
+ blocks[i] = sanitised;
2439
+ });
2440
+ }
2441
+ const joinedProse = Object.values(storedProse).filter(Boolean).join("\n\n");
2404
2442
  uiMessages.push({
2405
2443
  id,
2406
2444
  role: "assistant",
@@ -2408,7 +2446,7 @@ function storedToUseChat(stored) {
2408
2446
  });
2409
2447
  blocksMap[id] = blocks;
2410
2448
  if (joinedProse) proseMap[id] = joinedProse;
2411
- if (m.errorJson) errorsMap[id] = m.errorJson;
2449
+ if (storedError) errorsMap[id] = storedError;
2412
2450
  }
2413
2451
  }
2414
2452
  return { uiMessages, blocksMap, proseMap, errorsMap };