chat-service.ts


Overview

The chat-service.ts file serves as a centralized client-side service module for managing all chat-related HTTP API interactions within the application. It acts as an abstraction layer over raw API endpoints, organizing numerous RESTful operations related to dialogs, conversations, tokens, external conversations, and other chat functionalities into a clean, type-safe interface.

This module imports predefined API endpoint URLs and a request handler utility, then maps them to HTTP methods. It registers these method configurations using a generic registerServer utility, which returns a strongly-typed service object to be used throughout the app for seamless server communication.


Detailed Explanation

Imported Dependencies


Constants and Objects

methods

A constant object that maps each chat-related API operation to its corresponding URL and HTTP method.

Example entry:

getDialog: {
  url: getDialog,
  method: 'get',
}

This design centralizes all endpoint configurations, improving maintainability and readability.


Main Exported Object

chatService

// Example of fetching a dialog by ID
const dialog = await chatService.getDialog({ params: { dialogId: '123' } });

// Example of creating a new conversation
const conversation = await chatService.setConversation({ data: { title: 'New Chat' } });

Available Methods and Their Purpose

Method Name

HTTP Method

Description

getDialog

GET

Retrieve a specific dialog.

setDialog

POST

Create or update a dialog.

removeDialog

POST

Delete a dialog.

listDialog

GET

List all dialogs.

listConversation

GET

List all conversations.

getConversation

GET

Retrieve a specific conversation.

getConversationSSE

GET

Retrieve conversation updates via Server-Sent Events (SSE).

setConversation

POST

Create or update a conversation.

completeConversation

POST

Mark a conversation as complete.

removeConversation

POST

Delete a conversation.

createToken

POST

Generate a new authorization token.

listToken

GET

List all tokens.

removeToken

POST

Revoke/delete a token.

getStats

GET

Retrieve chat statistics.

createExternalConversation

GET

Create conversation from an external source.

getExternalConversation

GET

Retrieve an external conversation.

completeExternalConversation

POST

Mark an external conversation as complete.

uploadAndParseExternal

POST

Upload and parse external files for conversation data.

deleteMessage

POST

Delete a message from a conversation.

thumbup

POST

Upvote or "thumbs up" a message or conversation.

tts

POST

Text-to-speech conversion of chat content.

ask

POST

Submit a query or message to the chat system.

getMindMap

POST

Generate or retrieve a mind map related to chat content.

getRelatedQuestions

POST

Get questions related to the current conversation.


Implementation Details


Integration and Interaction


Usage Example

import chatService from '@/services/chat-service';

// Fetch all dialogs
async function fetchDialogs() {
  try {
    const dialogs = await chatService.listDialog();
    console.log('Dialogs:', dialogs);
  } catch (error) {
    console.error('Failed to fetch dialogs:', error);
  }
}

// Start a new conversation
async function startConversation(title: string) {
  const response = await chatService.setConversation({ data: { title } });
  return response;
}

Visual Diagram

classDiagram
    class chatService {
        <<interface>>
        +getDialog(params)
        +setDialog(data)
        +removeDialog(data)
        +listDialog()
        +listConversation()
        +getConversation(params)
        +getConversationSSE(params)
        +setConversation(data)
        +completeConversation(data)
        +removeConversation(data)
        +createToken(data)
        +listToken()
        +removeToken(data)
        +getStats()
        +createExternalConversation(params)
        +getExternalConversation(params)
        +completeExternalConversation(data)
        +uploadAndParseExternal(data)
        +deleteMessage(data)
        +thumbup(data)
        +tts(data)
        +ask(data)
        +getMindMap(data)
        +getRelatedQuestions(data)
    }

    class methods {
        +url: string
        +method: string
    }

    chatService ..> methods : "wraps"

Summary

The chat-service.ts file provides a well-structured, type-safe, and maintainable client service for all chat-related backend API calls. It abstracts the underlying HTTP request details and exposes intuitive method names for use throughout the application, supporting a wide range of chat operations from dialog management to advanced features like SSE and mind maps. Its integration with registerServer and request utilities promotes modularity and reusability.

This module is essential for enabling consistent and efficient communication between the frontend chat components and the backend chat APIs.