Chat & Session History - Quick Reference

🎯 Overview

Implementation guide for chat and session history functionality in Terraphim AI.

πŸ“Š Architecture at a Glance

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      FRONTEND (Svelte)                       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  SessionList  β”‚  Chat Component  β”‚  Context Manager         β”‚
β”‚  Component    β”‚  (Enhanced)      β”‚  UI                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚               β”‚                    β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚      Svelte Stores             β”‚
        β”‚  - currentConversation         β”‚
        β”‚  - conversationList            β”‚
        β”‚  - Auto-save logic             β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚   Tauri Commands / HTTP API    β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     BACKEND (Rust)                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚            ConversationService                               β”‚
β”‚  - CRUD operations                                          β”‚
β”‚  - Search & filtering                                       β”‚
β”‚  - Import/Export                                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
        β”‚  Context      β”‚
        β”‚  Manager      β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              PERSISTENCE LAYER                               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         ConversationPersistence (OpenDAL)                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚ SQLite  β”‚ DashMap  β”‚  Memory  β”‚  S3 (opt)   β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”‘ Key Components

Backend Components

| Component | Location | Purpose | |-----------|----------|---------| | ConversationService | crates/terraphim_service/src/conversation_service.rs | NEW - Business logic for conversation management | | ConversationPersistence | crates/terraphim_persistence/src/conversation.rs | NEW - Persistence trait and OpenDAL implementation | | ContextManager | crates/terraphim_service/src/context_manager.rs | ENHANCE - Add archive, restore, clone methods | | API Endpoints | terraphim_server/src/api.rs | ENHANCE - Add 8 new REST endpoints | | Tauri Commands | desktop/src-tauri/src/cmd.rs | ENHANCE - Add 9 new commands |

Frontend Components

| Component | Location | Purpose | |-----------|----------|---------| | SessionList.svelte | desktop/src/lib/Chat/SessionList.svelte | NEW - Conversation list with search/filter | | Chat.svelte | desktop/src/lib/Chat/Chat.svelte | ENHANCE - Integrate session management | | Stores | desktop/src/lib/stores.ts | ENHANCE - Add conversation stores, auto-save |

Data Types (Already Exist)

| Type | Location | Purpose | |------|----------|---------| | Conversation | crates/terraphim_types/src/lib.rs:1053 | Full conversation with messages and context | | ConversationSummary | crates/terraphim_types/src/lib.rs:1123 | Lightweight summary for listing | | ChatMessage | crates/terraphim_types/src/lib.rs:981 | Individual message with context | | ContextItem | crates/terraphim_types/src/lib.rs:706 | Context attached to messages |

πŸ“‹ Implementation Checklist

Phase 1: Backend Foundation ⏱️ 1-2 weeks

  • [ ] Create ConversationPersistence trait
  • [ ] Implement OpenDALConversationPersistence
  • [ ] Create ConversationService with:
    • [ ] create_conversation()
    • [ ] get_conversation()
    • [ ] update_conversation()
    • [ ] delete_conversation()
    • [ ] list_conversations()
    • [ ] search_conversations()
  • [ ] Add 8 new API endpoints (REST)
  • [ ] Add 9 new Tauri commands
  • [ ] Write unit tests (target: 80% coverage)

Phase 2: Frontend UI ⏱️ 2 weeks

  • [ ] Create SessionList.svelte component
  • [ ] Add conversation stores to stores.ts
  • [ ] Implement auto-save with 2s debounce
  • [ ] Enhance Chat.svelte:
    • [ ] Session sidebar toggle
    • [ ] Load from currentConversation store
    • [ ] Auto-save integration
  • [ ] Write component tests

Phase 3: Search & Filtering ⏱️ 1 week

  • [ ] Backend search implementation
  • [ ] Frontend search UI
  • [ ] Filter by role, date, tags
  • [ ] Keyboard shortcuts

Phase 4: Import/Export ⏱️ 1 week

  • [ ] JSON export endpoint
  • [ ] JSON import with validation
  • [ ] Bulk export
  • [ ] Frontend UI for import/export

Phase 5: Polish ⏱️ 2 weeks

  • [ ] Performance optimization
  • [ ] Pagination for messages
  • [ ] Virtual scrolling
  • [ ] Analytics dashboard
  • [ ] Documentation
  • [ ] E2E tests

πŸš€ Quick Start Guide

1. Backend Setup

// crates/terraphim_persistence/src/conversation.rs
#[async_trait]
pub trait ConversationPersistence {
    async fn save(&self, conversation: &Conversation) -> Result<()>;
    async fn load(&self, id: &ConversationId) -> Result<Conversation>;
    async fn delete(&self, id: &ConversationId) -> Result<()>;
    async fn list_ids(&self) -> Result<Vec<ConversationId>>;
}

pub struct OpenDALConversationPersistence {
    storage: Arc<DeviceStorage>,
    cache: LruCache<ConversationId, Conversation>,
}

2. Service Layer

// crates/terraphim_service/src/conversation_service.rs
pub struct ConversationService {
    persistence: Arc<Mutex<dyn ConversationPersistence>>,
    context_manager: Arc<Mutex<ContextManager>>,
    cache: LruCache<ConversationId, Conversation>,
}

impl ConversationService {
    pub async fn create_conversation(
        &mut self,
        title: String,
        role: RoleName,
    ) -> Result<Conversation> {
        let conversation = Conversation::new(title, role);
        self.persistence.lock().await.save(&conversation).await?;
        self.cache.put(conversation.id.clone(), conversation.clone());
        Ok(conversation)
    }
}

3. API Endpoints

// terraphim_server/src/api.rs
pub async fn list_conversations(
    Query(params): Query<ListConversationsParams>,
) -> Result<Json<ListConversationsResponse>> {
    let service = get_conversation_service().lock().await;
    let conversations = service.list_conversations(params.into()).await?;
    Ok(Json(ListConversationsResponse { conversations }))
}

4. Frontend Store

// desktop/src/lib/stores.ts
export const currentConversation: Writable<Conversation | null> = writable(null);
export const conversationList: Writable<ConversationSummary[]> = writable([]);

export function setupAutoSave() {
  let saveTimeout: NodeJS.Timeout;
  currentConversation.subscribe(conversation => {
    if (conversation) {
      clearTimeout(saveTimeout);
      saveTimeout = setTimeout(async () => {
        await saveConversation(conversation);
      }, 2000);
    }
  });
}

5. Frontend Component

<!-- desktop/src/lib/Chat/SessionList.svelte -->
<script lang="ts">
  async function loadConversations() {
    const response = $is_tauri
      ? await invoke('list_all_conversations', { limit: 100 })
      : await fetch(`${CONFIG.ServerURL}/conversations?limit=100`).then(r => r.json());
    conversationList.set(response.conversations);
  }
</script>

<div class="session-list">
  {#each $filteredConversations as conversation}
    <div class="session-item" on:click={() => selectConversation(conversation.id)}>
      {conversation.title}
    </div>
  {/each}
</div>

πŸ“‘ API Reference

REST Endpoints

| Method | Endpoint | Description | |--------|----------|-------------| | GET | /api/conversations | List conversations with pagination | | GET | /api/conversations/:id | Get specific conversation | | POST | /api/conversations | Create new conversation | | PUT | /api/conversations/:id | Update conversation metadata | | DELETE | /api/conversations/:id | Delete conversation | | GET | /api/conversations/search | Search conversations | | POST | /api/conversations/:id/export | Export to JSON | | POST | /api/conversations/import | Import from JSON |

Tauri Commands

| Command | Parameters | Returns | |---------|------------|---------| | list_all_conversations | skip, limit, filter | ListConversationsResponse | | create_new_conversation | title, role | CreateConversationResponse | | load_conversation | conversation_id | GetConversationResponse | | update_conversation_info | conversation_id, title, metadata | UpdateConversationResponse | | delete_conversation_by_id | conversation_id | DeleteConversationResponse | | search_conversation_history | query, limit | SearchConversationsResponse | | export_conversation_to_file | conversation_id, file_path | ExportConversationResponse | | import_conversation_from_file | file_path | ImportConversationResponse | | get_conversation_stats | - | ConversationStatistics |

πŸ’Ύ Storage Structure

conversations/
β”œβ”€β”€ index.json                    # Index of all conversations
β”œβ”€β”€ {uuid-1}.json                # Individual conversation files
β”œβ”€β”€ {uuid-2}.json
β”œβ”€β”€ {uuid-3}.json
└── archive/
    β”œβ”€β”€ {uuid-archived}.json     # Archived conversations
    └── ...

πŸ§ͺ Testing Commands

# Backend tests
cargo test --package terraphim_persistence conversation_persistence
cargo test --package terraphim_service conversation_service

# Frontend tests
cd desktop
yarn test:unit SessionList
yarn test:e2e chat-session

# Integration tests
./scripts/test_conversation_integration.sh

πŸ”§ Configuration

No new configuration required! Uses existing:

  • OpenDAL profiles for persistence
  • Tauri configuration for desktop
  • Server configuration for web mode

πŸ“š Related Documentation

  • Full Specification: docs/specifications/chat-session-history-spec.md
  • Architecture Docs: docs/architecture/
  • API Documentation: docs/api/
  • Types Documentation: crates/terraphim_types/src/lib.rs (lines 979-1377)

🎯 Success Criteria

  • [ ] Users can create, view, edit, and delete conversations
  • [ ] Conversations persist across sessions
  • [ ] Search returns relevant results in < 500ms
  • [ ] Auto-save works without data loss
  • [ ] UI is responsive with 100+ conversations
  • [ ] Export/import maintains data integrity
  • [ ] 80%+ test coverage for new code
  • [ ] Zero regressions in existing functionality

πŸ“ž Support

For questions or issues during implementation:

  1. Check the full specification document
  2. Review existing ContextManager implementation
  3. Examine terraphim_types for data structures
  4. Refer to OpenDAL documentation for persistence patterns

Quick Ref Version: 1.0.0 Last Updated: 2025-10-05