Skip to content

Migrate frontend to unified conversation API endpoints #594

@manavgup

Description

@manavgup

Overview

This issue tracks the frontend migration from the deprecated /api/chat endpoints to the unified /api/conversations API after the Phase 4 Router Unification (PR #589).

Context

What Changed in Backend (PR #589)

The following endpoints were unified into /api/conversations:

Chat Endpoints → Conversation Endpoints Mapping

Old Endpoint (/api/chat) New Endpoint (/api/conversations) Notes
POST /chat/{session_id}/message POST /conversations/{session_id}/messages Message creation
GET /chat/{session_id}/messages GET /conversations/{session_id}/messages Message retrieval
GET /chat/{session_id}/suggestions GET /conversations/{session_id}/suggestions Question suggestions
POST /chat/{session_id}/conversation-suggestions POST /conversations/{session_id}/conversation-suggestions Smart suggestions

Note: All endpoints maintain the same request/response schemas for backward compatibility.

Frontend Files to Update

1. API Client Configuration (frontend/src/services/apiClient.ts)

Current state (assumed based on typical React/TypeScript setup):

// Example endpoints to migrate
const CHAT_BASE = '/api/chat';

export const chatApi = {
  sendMessage: (sessionId: string, message: string) => 
    axios.post(`${CHAT_BASE}/${sessionId}/message`, { message }),
  
  getMessages: (sessionId: string) => 
    axios.get(`${CHAT_BASE}/${sessionId}/messages`),
  
  getSuggestions: (sessionId: string, currentMessage: string) => 
    axios.get(`${CHAT_BASE}/${sessionId}/suggestions`, { params: { current_message: currentMessage } }),
};

Required changes:

// Update base URL
const CONVERSATIONS_BASE = '/api/conversations';

export const conversationApi = {
  sendMessage: (sessionId: string, message: string) => 
    axios.post(`${CONVERSATIONS_BASE}/${sessionId}/messages`, { message }),
  
  getMessages: (sessionId: string) => 
    axios.get(`${CONVERSATIONS_BASE}/${sessionId}/messages`),
  
  getSuggestions: (sessionId: string, currentMessage: string) => 
    axios.get(`${CONVERSATIONS_BASE}/${sessionId}/suggestions`, { params: { current_message: currentMessage } }),
};

2. Component Updates

Search for all files importing/using chat API endpoints:

# Find all references to /api/chat in frontend
grep -r "/api/chat" frontend/src/

# Find all imports of chatApi or similar
grep -r "chatApi" frontend/src/

Common files that may need updates:

  • frontend/src/components/Chat/ChatInterface.tsx
  • frontend/src/components/Chat/MessageList.tsx
  • frontend/src/components/Chat/SuggestionPanel.tsx
  • frontend/src/hooks/useChat.ts (custom hook)
  • frontend/src/contexts/ChatContext.tsx (context provider)

3. Type Definitions

Update any TypeScript interfaces referencing chat endpoints:

// Before
interface ChatApiClient {
  sendMessage: (sessionId: string, message: string) => Promise<MessageResponse>;
  getMessages: (sessionId: string) => Promise<Message[]>;
}

// After
interface ConversationApiClient {
  sendMessage: (sessionId: string, message: string) => Promise<MessageResponse>;
  getMessages: (sessionId: string) => Promise<Message[]>;
}

Implementation Plan

Step 1: Create API Migration Layer (Recommended Approach)

Add a compatibility layer to support gradual migration:

// frontend/src/services/conversationApi.ts (new file)
import axios from 'axios';

const BASE_URL = '/api/conversations';

export const conversationApi = {
  // Message operations
  sendMessage: async (sessionId: string, message: string) => {
    return axios.post(`${BASE_URL}/${sessionId}/messages`, { message });
  },

  getMessages: async (sessionId: string, limit?: number) => {
    return axios.get(`${BASE_URL}/${sessionId}/messages`, { params: { limit } });
  },

  // Suggestion operations
  getSuggestions: async (sessionId: string, currentMessage: string, maxSuggestions = 3) => {
    return axios.get(`${BASE_URL}/${sessionId}/suggestions`, {
      params: { current_message: currentMessage, max_suggestions: maxSuggestions }
    });
  },

  getConversationSuggestions: async (sessionId: string, suggestionInput: ConversationSuggestionInput) => {
    return axios.post(`${BASE_URL}/${sessionId}/conversation-suggestions`, suggestionInput);
  },
};

Step 2: Update Component Imports

// Before
import { chatApi } from '@/services/apiClient';

// After
import { conversationApi } from '@/services/conversationApi';

Step 3: Update API Calls

// Before
const messages = await chatApi.getMessages(sessionId);

// After
const messages = await conversationApi.getMessages(sessionId);

Step 4: Testing Checklist

  • Message sending: Verify messages are created and displayed correctly
  • Message retrieval: Verify conversation history loads properly
  • Suggestions: Verify question suggestions appear as expected
  • Smart suggestions: Verify conversation-aware suggestions work
  • Error handling: Verify 404/401/500 errors display user-friendly messages
  • Loading states: Verify spinners/skeletons appear during API calls
  • Session management: Verify session creation and switching works

Step 5: Remove Old Chat API References

After verification, remove deprecated imports:

# Search for any remaining /api/chat references
grep -r "/api/chat" frontend/src/

# Remove old chatApi.ts if it exists
rm frontend/src/services/chatApi.ts

Acceptance Criteria

  • All frontend API calls updated to use /api/conversations endpoints
  • No references to /api/chat remain in frontend code
  • All chat features work correctly with new endpoints:
    • Message sending and receiving
    • Message history retrieval
    • Question suggestions
    • Smart conversation suggestions
  • Error handling works correctly (404, 401, 500 responses)
  • Loading states display properly during API calls
  • No console errors or warnings in browser dev tools
  • Frontend integration tests pass (if they exist)
  • Manual testing completed across all chat features

Estimated Effort

2-3 hours (depending on frontend codebase size)

  • 30 min: Search and identify all /api/chat references
  • 1 hour: Update API client and component imports
  • 30 min: Update type definitions
  • 1 hour: Manual testing and verification

Testing Strategy

Manual Testing

  1. Create new conversation session

    • Verify session creation works
    • Verify session appears in session list
  2. Send messages

    • Send user message
    • Verify assistant response appears
    • Verify message history updates
  3. Test suggestions

    • Verify question suggestions load
    • Click suggestion and verify it populates input
    • Verify smart suggestions work with context
  4. Test error scenarios

    • Invalid session ID → 404 error
    • Unauthenticated request → 401 error
    • Server error → 500 error with user-friendly message

Automated Testing (if frontend tests exist)

// Example test update
describe('ConversationApi', () => {
  it('should send message to correct endpoint', async () => {
    const sessionId = 'test-session-123';
    const message = 'Hello, world\!';
    
    await conversationApi.sendMessage(sessionId, message);
    
    expect(mockAxios.post).toHaveBeenCalledWith(
      '/api/conversations/test-session-123/messages',
      { message: 'Hello, world\!' }
    );
  });
});

Migration Safety

Backward Compatibility: The backend maintains the same request/response schemas, so this is a low-risk migration. The only changes are:

  • URL path (/api/chat/api/conversations)
  • Endpoint names (/message/messages)

Rollback Plan: If issues arise, the deprecated chat_router.py remains functional until this migration is verified.

Follow-up Tasks

After this migration is complete:

Priority

Medium-High - Required to complete Phase 4 Router Unification, but not blocking current functionality (deprecated endpoints still work).

Labels

  • frontend
  • enhancement
  • chat

Metadata

Metadata

Assignees

Labels

chatChat and conversational interface featuresenhancementNew feature or requestfrontendFrontend/UI related

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions