utils.ts


Overview

The utils.ts file contains a set of utility functions primarily designed to support operations related to conversations, messages, and references in a chat application context. These utilities facilitate tasks such as validating conversation IDs, extracting document IDs from conversation references, building message reference objects, and performing text replacements for legacy indexing formats.

This file acts as a helper layer, simplifying and standardizing common data manipulations and checks that interact with conversation and message data structures used across the chat system.


Functions

isConversationIdExist

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

Purpose:
Checks whether a given conversation ID is valid and exists, i.e., it is neither an empty string nor a predefined empty conversation ID constant.

Parameters:

Returns:
booleantrue if the conversation ID is valid and exists; false otherwise.

Usage example:

if (isConversationIdExist(convoId)) {
  // proceed with conversation-related logic
}

getDocumentIdsFromConversionReference

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

Purpose:
Extracts and concatenates unique document IDs from a conversation's reference data.

Parameters:

Returns:
string — A comma-separated string of unique document IDs found in the conversation references.

Detailed behaviour:

Usage example:

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

buildMessageItemReference

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

Purpose:
Constructs or retrieves the appropriate reference object for a given message within a conversation context.

Parameters:

Returns:
IReference — The reference object associated with the message. If no reference is found, returns a default empty reference object { doc_aggs: [], chunks: [], total: 0 }.

Detailed behaviour:

Usage example:

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

replaceTextByOldReg

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

Purpose:
Transforms text containing legacy index markers into the current index format to maintain compatibility.

Parameters:

Returns:
string — The text with old index markers replaced by the new format.

Detailed behaviour:

Example:

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

Constants and Regex

These regex patterns are used primarily for text replacement and parsing tasks related to message references.


Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Utility Functions Flowchart

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

    C[getDocumentIdsFromConversionReference] -->|extracts unique doc IDs| D[Conversation.reference]

    E[buildMessageItemReference] -->|uses| F[Conversation.message]
    E -->|uses| G[Conversation.reference]
    E -->|returns| H[Message Reference]

    I[replaceTextByOldReg] -->|replaces old index format| J[Text String]

    subgraph Inputs
        B
        D
        F
        G
        J
    end

    subgraph Outputs
        H
    end

Summary

The utils.ts file provides essential utility functions that streamline the handling of conversations and messages, particularly focusing on references and IDs. It handles validation, data extraction, backward compatibility, and data structuring in a reusable manner supporting a chat application's data flow and rendering logic.