Skip to content

Add AI Chats sidebar section with list and + New chat - #2178

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2153-ai-chats-sidebar
Aug 20, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2153-ai-chats-sidebar

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Adds a top-level "AI Chats" sidebar section — a sibling of Collections and Schema Types (not nested under Schema Types, and ai-chat is NOT added to SIDENAV_CORE_TYPES). It lists existing ai-chat nodes most-recently-modified first, with a "+ New chat" action that creates immediately (no name prompt, unlike Collections) and opens the new chat in a tab.

This lands before core#2152 (which removes the /ai-chat slash command), so there is no window where users lose a way to start a conversation.

Changes

  • New aiChatsData store (stores/ai-chats.svelte.ts): loads via backendAdapter.queryNodes({ nodeType: 'ai-chat', limit: 50 }), sorts client-side by modifiedAt (the query has no order-by), and exposes createChat() backed by createSchemaInstance('ai-chat') with createBusy/createError for the button's in-flight/failure states.
  • layout.svelte.ts gains aiChatsExpanded (interface field, default false, persisted-hydration line, setAiChatsExpanded, module export), and layout-persistence-service.ts bumps its schema to version 4 to persist it, with a v3→v4 migration step.
  • navigation-sidebar.svelte renders the new Collapsible section (expanded list + collapsed-icon variant), reusing the existing aiSquare glyph markup (design/icons/ui/ai-square.ts) inline since it doesn't fit through the shared icon-component path used elsewhere in the nav. Clicking a chat reuses the tab-reuse pattern from handleSchemaClick, but routes nodeType: 'ai-chat' so it opens via AiChatNodeViewer instead of QueryNodeViewer. Empty-content chats (the normal state before core#1698's background titling runs) show "Untitled chat".
  • database.svelte.ts reloads aiChatsData on database switch, mirroring the existing collectionsData/schemasData reload.

Acceptance criteria

  • "AI Chats" renders as its own top-level sidebar section, sibling of Collections and Schema Types
  • ai-chat NOT added to SIDENAV_CORE_TYPES
  • Section expands/collapses like Collections and Schema Types
  • aiChatsExpanded added to layout store, persists across restarts
  • Collapsed-sidebar state shows icon-only variant consistent with other sections
  • Expanded section lists existing ai-chat nodes, most-recent-first, with a defined limit
  • Chats with empty content show a readable placeholder label
  • Clicking a chat opens it via AiChatNodeViewer; already-open chat activates existing tab instead of duplicating
  • "+ New chat" creates immediately (no name prompt) and opens it
  • Create failures surface an error and re-enable the button
  • New chats appear in the list without requiring a restart (prepended optimistically on create)
  • Tests: layout-store persistence of the new flag, and list/create behavior
  • No raw console.* — uses createLogger

Test plan

  • bun run test — all 223 test files / 4761 tests pass, including new ai-chats.test.ts and updated layout.test.ts / layout-persistence-service.test.ts / database.test.ts
  • bun run --cwd packages/desktop-app quality:fix — eslint + svelte-check clean (0 errors, 0 warnings)

Closes #2153

🤖 Generated with Claude Code

Adds a top-level "AI Chats" sidebar section, a sibling of Collections and
Schema Types (not nested under Schema Types, and ai-chat is not added to
SIDENAV_CORE_TYPES). It lists existing ai-chat nodes most-recently-modified
first, with a "+ New chat" action that creates immediately (no name prompt,
unlike collections) and opens the new chat in a tab.

- New aiChatsData store (ai-chats.svelte.ts): loads via
  backendAdapter.queryNodes({ nodeType: 'ai-chat', limit: 50 }), sorts
  client-side by modifiedAt (the query has no order-by), and exposes
  createChat() backed by createSchemaInstance('ai-chat') with
  createBusy/createError for the button's in-flight/failure states.
- layout store gains aiChatsExpanded (interface field, default false,
  persisted-hydration, setAiChatsExpanded, module export), and
  layout-persistence-service bumps to schema version 4 to persist it,
  with a v3->v4 migration step.
- navigation-sidebar.svelte renders the new Collapsible section (expanded
  and collapsed-icon variants), reusing the existing aiSquare glyph markup.
  Clicking a chat reuses the tab-reuse pattern from handleSchemaClick, but
  routes nodeType: 'ai-chat' so it opens via AiChatNodeViewer instead of
  QueryNodeViewer. Empty-content chats show "Untitled chat".
- database.svelte.ts reloads aiChatsData on database switch, mirroring the
  existing collectionsData/schemasData reload.

Tests: layout-store/persistence-service round-trip of aiChatsExpanded
(including the v1->v4 and v3->v4 migration paths), and ai-chats store
load-sort/create/error-recovery behavior.
…race

- loadAiChats() no longer passes a fetch-side limit to queryNodes. The
  backend query has no order-by, so a fetch-side LIMIT returns an arbitrary
  (effectively insertion-order) subset with no ORDER BY clause; sorting that
  subset afterward could not recover chats the LIMIT had already excluded,
  silently hiding genuinely-recent ones once a user passed 50 ai-chat nodes.
  Now fetches everything, sorts by modifiedAt, then slices to DISPLAY_LIMIT
  — mirroring collectionsData.loadCollections(), which also fetches its full
  list unbounded.
- createChat() gains a #generation guard (mirroring collectionsData's
  #generation around createCollection): if a database switch invalidates the
  store mid-create, the result is discarded instead of being written into
  (or erroring against) a store that now represents a different database.
- New invalidateForDatabaseSwitch() bumps that generation and clears any
  stale createError left over from the previous database; wired into
  database.svelte.ts's switchTo alongside collectionsData.forgetLocallyCreated().
@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE

Adversarial review (code-review skill, effort=high) ran against the initial diff and found 4 issues, ranked real-bug/blocker-adjacent:

  1. Fixed — createChat() had no guard against a database switch landing mid-create; the new chat node could be written into a store now representing a different database. Added a #generation counter (mirrors collectionsData's guard around createCollection) and invalidateForDatabaseSwitch(), wired into database.svelte.ts's switchTo.
  2. Fixed — queryNodes({ nodeType: 'ai-chat', limit: 50 }) applied the limit before the client-side sort. Since the backend query has no order-by, SQLite returns an arbitrary (effectively insertion-order) subset under LIMIT, so sorting afterward could not recover chats already excluded — a user with >50 ai-chat nodes could have genuinely-recent chats silently missing from the list. Now fetches unbounded and slices to DISPLAY_LIMIT after sorting, matching collectionsData.loadCollections()'s own unbounded fetch.
  3. Fixed — a failed create's createError was never cleared on database switch, so a stale error banner from database A could linger into database B. invalidateForDatabaseSwitch() now clears it.
  4. Deferred, not fixed — loadAiChats() has no epoch guard against out-of-order resolution across rapid database switches. The reviewer confirmed this mirrors an existing, unfixed gap in the sibling schemasData.loadSchemas() store, not something unique to this PR. Fixing it here would mean also fixing schemasData for consistency, which is out of scope for core#2153 — left as-is to match the majority precedent (2 of 3 sibling stores follow this simpler pattern; only collectionsData has the guard, tied to its optimistic-create complexity).

Fix commit: c8820a9. Full test suite (223 files / 4765 tests) and quality:fix (eslint + svelte-check) both clean after the fix. Merging.

@mstomar125
mstomar125 merged commit dc387ff into main Aug 20, 2026
@mstomar125
mstomar125 deleted the issue-2153-ai-chats-sidebar branch August 20, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an "AI Chats" sidebar section listing conversations with a "+ New chat" button

1 participant