utils.ts


Overview

The utils.ts file provides a collection of utility functions that facilitate handling and processing chat conversation data within the application. These utilities primarily focus on validating conversation identifiers, extracting document IDs from conversation references, building message reference objects, and normalizing text patterns used for indexing and referencing documents in chat messages.

These helpers abstract common operations performed on chat conversation and message data structures, ensuring consistency and reusability across the application’s chat-related modules.


Functions

1. isConversationIdExist

export const isConversationIdExist = (conversationId: string) => boolean;

Purpose:
Checks whether a given conversation ID is valid and exists by confirming it is neither empty nor equal to a predefined empty conversation ID constant.

Parameters:

Returns:

Usage Example:

import { isConversationIdExist } from './utils';

const valid = isConversationIdExist('abc123'); // true or false

2. getDocumentIdsFromConversionReference

export const getDocumentIdsFromConversionReference = (data: IConversation) => string;

Purpose:
Extracts all unique document IDs referenced within a conversation's references and returns them as a comma-separated string.

Parameters:

Returns:

Implementation Details:

Usage Example:

import { getDocumentIdsFromConversionReference } from './utils';

const docIds = getDocumentIdsFromConversionReference(conversationObj);
console.log(docIds); // "doc1,doc2,doc3"

3. buildMessageItemReference

export const buildMessageItemReference = (
  conversation: { message: IMessage[]; reference: IReference[] },
  message: IMessage
) => IReference;

Purpose:
Constructs or retrieves the appropriate reference object associated with a specific chat message within a conversation.

Parameters:

Returns:

Implementation Details:

Usage Example:

import { buildMessageItemReference } from './utils';

const ref = buildMessageItemReference(conversation, message);
console.log(ref.doc_aggs);

4. replaceTextByOldReg

export const replaceTextByOldReg = (text: string) => string;

Purpose:
Normalizes legacy indexed text patterns to the current format used for document referencing.

Parameters:

Returns:

Implementation Details:

Related Constant:

Usage Example:

import { replaceTextByOldReg } from './utils';

const normalizedText = replaceTextByOldReg("Reference ##123$$ here");
console.log(normalizedText); // "Reference [ID:123] here"

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[isConversationIdExist] -->|Validates| B[Conversation ID]

    subgraph Extract Document IDs
        C[getDocumentIdsFromConversionReference]
        C -->|Iterate references| D[References]
        D --> E[doc_aggs]
        E --> F[doc_id]
        F --> G[Unique doc IDs]
        G --> H[Comma-separated string]
    end

    subgraph Build Message Reference
        I[buildMessageItemReference]
        I -->|Input| J[Conversation: messages & references]
        I -->|Input| K[Message]
        J --> L[Assistant messages (excluding first)]
        L --> M[Find index of message]
        M --> N[Select reference by index]
        K -->|Check message.reference| O[Use message reference if present]
        N & O --> P[Resulting reference or default]
    end

    subgraph Text Replacement
        Q[replaceTextByOldReg]
        Q -->|Input text with old pattern| R[Text]
        R -->|Regex replace| S[Text with new pattern]
    end

Summary

The utils.ts file offers essential helpers for:

These utilities improve code maintainability, data integrity, and support backward compatibility within the chat system of the application.