ollama-server2 / CHAT_INTERFACE_IMPLEMENTATION.md
bahadur3093's picture
feat: Implement chat interface with persistent history and modern design
63e82f2
|
Raw
History Blame Contribute Delete
8.04 kB

Chat Interface Implementation Guide

Overview

This document describes the complete transformation of the Ollama UI from a simple prompt-response interface to a modern, Google Material Design 3 themed chat application with persistent chat history.

Features Implemented

1. Modern Chat Interface

  • Real-time message display with user and assistant messages
  • Auto-scrolling to latest messages
  • Typing indicator during message generation
  • Empty state with helpful instructions
  • Message timestamps and model information

2. Persistent Chat History

  • Local file-based storage (chat-history.json)
  • Messages persist across page refreshes
  • Automatic loading of chat history on app start
  • Each message includes:
    • Unique ID
    • Role (user/assistant)
    • Content
    • Timestamp
    • Model used

3. Clear Chat History

  • Dedicated button in the header (πŸ—‘οΈ icon)
  • Confirmation dialog before clearing
  • Clears both UI state and persistent storage

4. Settings Panel

  • Collapsible settings panel (βš™οΈ icon)
  • Model selection dropdown
  • Pull new models functionality
  • View and delete installed models
  • Smooth slide-down animation

5. Google Material Design 3 Theme

  • Clean, modern aesthetic matching Google's latest design language
  • Proper color palette:
    • Primary: #1967d2 (Google Blue)
    • Background: #f8f9fa (Light gray)
    • Surface: #ffffff (White)
    • Text: #202124 (Dark gray)
    • Secondary text: #5f6368 (Medium gray)
  • Rounded corners and subtle shadows
  • Smooth transitions and animations

6. Mobile Responsive Design

  • Fully responsive layout with breakpoints:
    • Desktop: Full features
    • Tablet (≀768px): Adjusted spacing and font sizes
    • Mobile (≀480px): Optimized for small screens
  • Touch-friendly button sizes (minimum 44px)
  • Proper font sizing to prevent zoom on iOS (16px for inputs)
  • Flexible layout that adapts to screen size
  • Hidden model badge on very small screens

7. Accessibility Features

  • ARIA labels for icon buttons
  • Semantic HTML structure
  • Keyboard navigation support
  • Focus states for interactive elements
  • Proper contrast ratios

File Structure

ollama-server/
β”œβ”€β”€ pages/
β”‚   β”œβ”€β”€ index.tsx              # Main chat interface (updated)
β”‚   β”œβ”€β”€ _app.tsx               # App wrapper (unchanged)
β”‚   └── api/
β”‚       β”œβ”€β”€ chat.ts            # NEW: Chat history API
β”‚       β”œβ”€β”€ generate.ts        # Existing: Message generation
β”‚       β”œβ”€β”€ models.ts          # Existing: Model management
β”‚       └── pull.ts            # Existing: Model pulling
β”œβ”€β”€ styles/
β”‚   └── globals.css            # Complete redesign with Material Design 3
β”œβ”€β”€ chat-history.json          # NEW: Auto-created chat storage file
└── CHAT_INTERFACE_IMPLEMENTATION.md  # This file

API Endpoints

/api/chat (NEW)

GET - Retrieve Chat History

Response: {
  messages: ChatMessage[]
}

POST - Save Message

Request: {
  message: {
    id: string
    role: "user" | "assistant"
    content: string
    timestamp: number
    model?: string
  }
}

Response: {
  success: true
}

DELETE - Clear Chat History

Response: {
  success: true
}

Component Architecture

State Management

// Model management
installedModels: OllamaModel[]
newModel: string
pulling: boolean
pullStatus: string
deleting: string

// Chat interface
model: string
prompt: string
chatMessages: ChatMessage[]
loading: boolean
showSettings: boolean
chatEndRef: React.RefObject<HTMLDivElement>

Key Functions

  1. fetchChatHistory() - Loads messages from /api/chat on mount
  2. saveMessage() - Persists individual messages to storage
  3. clearChatHistory() - Deletes all messages with confirmation
  4. sendMessage() - Handles message sending workflow:
    • Creates user message
    • Updates UI immediately
    • Saves to storage
    • Calls generation API
    • Displays assistant response
    • Handles errors gracefully

User Experience Flow

First Time Use

  1. User sees empty chat state with instructions
  2. Opens settings panel (βš™οΈ)
  3. Pulls a model (e.g., "mistral")
  4. Selects the model from dropdown
  5. Types message and sends
  6. Sees conversation build up

Returning User

  1. App loads previous chat history automatically
  2. Can continue conversation where left off
  3. Can clear history if desired
  4. Can switch models mid-conversation

Mobile Optimizations

Touch Interactions

  • All buttons are minimum 44x44px (Apple HIG recommendation)
  • Adequate spacing between interactive elements
  • No hover-dependent functionality

Layout Adaptations

  • Settings panel becomes full-width on mobile
  • Pull model input stacks vertically on small screens
  • Model badge hidden on very small screens to save space
  • Font sizes adjusted for readability

Performance

  • CSS animations use transform/opacity for GPU acceleration
  • Minimal re-renders with proper React optimization
  • Efficient scroll behavior with smooth scrolling

Design Tokens (Material Design 3)

Colors

/* Primary */
--primary: #1967d2;
--primary-hover: #1557b0;
--primary-active: #1446a0;

/* Surfaces */
--surface: #ffffff;
--surface-variant: #f8f9fa;
--surface-hover: #f1f3f4;

/* Borders */
--border: #e8eaed;
--border-hover: #dadce0;

/* Text */
--text-primary: #202124;
--text-secondary: #5f6368;
--text-disabled: #9aa0a6;

/* Accent Colors */
--blue-light: #e8f0fe;
--blue-border: #d2e3fc;
--red-light: #fce8e6;
--red: #d93025;

Typography

/* Font Family */
font-family: 'Google Sans', 'Roboto', -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

/* Sizes */
--text-xs: 12px;
--text-sm: 13px;
--text-base: 14px;
--text-lg: 16px;
--text-xl: 18px;
--text-2xl: 20px;
--text-3xl: 22px;
--text-4xl: 24px;

Spacing

/* Base unit: 4px */
--space-1: 4px;
--space-2: 8px;
--space-3: 12px;
--space-4: 16px;
--space-5: 20px;
--space-6: 24px;
--space-8: 32px;

Border Radius

--radius-sm: 8px;
--radius-md: 12px;
--radius-lg: 16px;
--radius-full: 50%;
--radius-pill: 24px;

Browser Compatibility

  • Chrome/Edge: Full support
  • Firefox: Full support
  • Safari: Full support (iOS 12+)
  • Mobile browsers: Optimized for iOS Safari and Chrome Android

Future Enhancements

Potential Features

  1. Message Actions

    • Copy message content
    • Regenerate response
    • Edit user messages
  2. Conversation Management

    • Multiple conversation threads
    • Search chat history
    • Export conversations
  3. Advanced Settings

    • Temperature control
    • Max tokens
    • System prompts
  4. UI Enhancements

    • Dark mode toggle
    • Markdown rendering for messages
    • Code syntax highlighting
    • Image support
  5. Performance

    • Streaming responses
    • Virtual scrolling for long chats
    • Message pagination

Troubleshooting

Chat history not persisting

  • Check file permissions for chat-history.json
  • Verify API endpoint /api/chat is accessible
  • Check browser console for errors

Messages not sending

  • Ensure a model is selected
  • Verify Ollama is running on localhost:11434
  • Check network tab for API failures

UI not responsive

  • Clear browser cache
  • Check for CSS conflicts
  • Verify viewport meta tag in HTML

Development Notes

Running the Application

npm run dev
# App runs on http://localhost:7860

Building for Production

npm run build
npm start

Testing Chat History

  1. Send a few messages
  2. Refresh the page
  3. Verify messages reappear
  4. Click clear history button
  5. Confirm messages are deleted

Conclusion

The chat interface has been completely redesigned to provide a modern, intuitive experience that matches Google's Material Design 3 guidelines. The implementation includes persistent storage, mobile responsiveness, and accessibility features, making it production-ready for a wide range of users and devices.