Skip to main content

Types

ChatType

interface ChatType {
id?: string;
created_at?: string;
updated_at?: string;
}

MessageData

interface MessageData {
contentType: string;
foundInRag: boolean;
links: any[];
}

ChatMessage

interface ChatMessage {
id: number;
sender: string;
text: string;
chatId: string;
createdAt: Date;
updatedAt: Date;
media: any;
data?: MessageData;
}

ChatResponse

interface ChatResponse {
userMessage: ChatMessage;
sinaMessage: ChatMessage;
}

Helper Functions

parseChatMessage

Converts raw JSON to a ChatMessage object.

function parseChatMessage(json: any): ChatMessage {
return {
id: json.id,
sender: json.sender,
text: json.text,
chatId: json.chat_id,
createdAt: new Date(json.created_at),
updatedAt: new Date(json.updated_at),
media: json.media,
data: json.data
? {
contentType: json.data.content_type,
foundInRag: json.data.found_in_rag,
links: json.data.links,
}
: undefined,
};
}

parseChatResponse

Converts raw JSON to a ChatResponse object.

function parseChatResponse(json: any): ChatResponse {
return {
userMessage: parseChatMessage(json.user_message),
sinaMessage: parseChatMessage(json.sina_message),
};
}

serializeChatMessage

Converts a ChatMessage object to raw JSON.

function serializeChatMessage(msg: ChatMessage): any {
const base: any = {
id: msg.id,
sender: msg.sender,
text: msg.text,
chat_id: msg.chatId,
created_at: msg.createdAt.toISOString(),
updated_at: msg.updatedAt.toISOString(),
media: msg.media,
};
if (msg.data) {
base.data = {
content_type: msg.data.contentType,
found_in_rag: msg.data.foundInRag,
links: msg.data.links,
};
}
return base;
}

serializeChatResponse

Converts a ChatResponse object to raw JSON.

function serializeChatResponse(resp: ChatResponse): any {
return {
user_message: serializeChatMessage(resp.userMessage),
sina_message: serializeChatMessage(resp.sinaMessage),
};
}