Refactor chat message handling by removing unused fields

- Eliminated `responseMessages`, `reasoning`, and `rawLlmMessages` from chat creation and handling functions to streamline data processing.
- Updated the `generateJsonbArraySql` function to accept pre-stringified JSON for better performance and clarity.

These changes enhance the efficiency of chat message management and reduce unnecessary complexity in the codebase.
This commit is contained in:
dal 2025-08-12 16:38:20 -06:00
parent e52c0c7d72
commit 1e594ac4ec
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
3 changed files with 9 additions and 12 deletions

View File

@ -326,10 +326,7 @@ export async function handleNewChat({
chatId: newChat.id, chatId: newChat.id,
createdBy: user.id, createdBy: user.id,
requestMessage: prompt, requestMessage: prompt,
responseMessages: {},
reasoning: {},
title: prompt, title: prompt,
rawLlmMessages: {},
isCompleted: false, isCompleted: false,
}) })
.returning(); .returning();

View File

@ -134,10 +134,7 @@ export async function createMessage(input: CreateMessageInput): Promise<Message>
chatId: validated.chatId, chatId: validated.chatId,
createdBy: validated.userId, createdBy: validated.userId,
requestMessage: validated.content, requestMessage: validated.content,
responseMessages: {},
reasoning: {},
title: validated.content.substring(0, 255), // Ensure title fits in database title: validated.content.substring(0, 255), // Ensure title fits in database
rawLlmMessages: {},
isCompleted: false, isCompleted: false,
}) })
.returning(); .returning();

View File

@ -27,14 +27,16 @@ type MessageFieldName = keyof typeof MESSAGE_FIELD_MAPPING;
/** /**
* Helper function to generate SQL for updating or appending to a JSONB array field * Helper function to generate SQL for updating or appending to a JSONB array field
* @param fieldName - The field name to update
* @param jsonString - Pre-stringified JSON to insert/update
* @param mode - Whether to append or update the last element
*/ */
function generateJsonbArraySql( function generateJsonbArraySql(
fieldName: MessageFieldName, fieldName: MessageFieldName,
entry: unknown, jsonString: string,
mode: UpdateMessageEntriesMode mode: UpdateMessageEntriesMode
): SQL { ): SQL {
const field = MESSAGE_FIELD_MAPPING[fieldName]; const field = MESSAGE_FIELD_MAPPING[fieldName];
const jsonString = JSON.stringify(entry);
if (mode === 'append') { if (mode === 'append') {
return sql`COALESCE(${field}, '[]'::jsonb) || ${jsonString}::jsonb`; return sql`COALESCE(${field}, '[]'::jsonb) || ${jsonString}::jsonb`;
@ -43,7 +45,7 @@ function generateJsonbArraySql(
// Update mode: replace last element or create new array // Update mode: replace last element or create new array
return sql` return sql`
CASE CASE
WHEN jsonb_array_length(${field}) > 0 THEN WHEN ${field} IS NOT NULL AND jsonb_array_length(${field}) > 0 THEN
jsonb_set( jsonb_set(
${field}, ${field},
ARRAY[jsonb_array_length(${field}) - 1]::text[], ARRAY[jsonb_array_length(${field}) - 1]::text[],
@ -73,16 +75,17 @@ export async function updateMessageEntries({
}; };
// Add each field conditionally using the helper function // Add each field conditionally using the helper function
// Stringify the entries before passing them to SQL to ensure proper JSONB casting
if (rawLlmMessage) { if (rawLlmMessage) {
setValues.rawLlmMessages = generateJsonbArraySql('rawLlmMessages', rawLlmMessage, mode); setValues.rawLlmMessages = generateJsonbArraySql('rawLlmMessages', JSON.stringify(rawLlmMessage), mode);
} }
if (responseEntry) { if (responseEntry) {
setValues.responseMessages = generateJsonbArraySql('responseMessages', responseEntry, mode); setValues.responseMessages = generateJsonbArraySql('responseMessages', JSON.stringify(responseEntry), mode);
} }
if (reasoningEntry) { if (reasoningEntry) {
setValues.reasoning = generateJsonbArraySql('reasoning', reasoningEntry, mode); setValues.reasoning = generateJsonbArraySql('reasoning', JSON.stringify(reasoningEntry), mode);
} }
await db await db