2025-08-08 06:36:01 +08:00
|
|
|
import type { ModelMessage } from 'ai';
|
|
|
|
import { type SQL, and, eq, isNull, sql } from 'drizzle-orm';
|
2025-08-15 03:49:34 +08:00
|
|
|
import { z } from 'zod';
|
2025-08-08 06:36:01 +08:00
|
|
|
import { db } from '../../connection';
|
|
|
|
import { messages } from '../../schema';
|
2025-08-15 03:49:34 +08:00
|
|
|
import { ReasoningMessageSchema, ResponseMessageSchema } from '../../schemas/message-schemas';
|
2025-08-08 06:36:01 +08:00
|
|
|
|
2025-08-15 03:49:34 +08:00
|
|
|
const UpdateMessageEntriesSchema = z.object({
|
|
|
|
messageId: z.string().uuid(),
|
|
|
|
rawLlmMessages: z.array(z.custom<ModelMessage>()).optional(),
|
|
|
|
responseMessages: z.array(ResponseMessageSchema).optional(),
|
|
|
|
reasoningMessages: z.array(ReasoningMessageSchema).optional(),
|
|
|
|
});
|
2025-08-08 06:36:01 +08:00
|
|
|
|
2025-08-15 03:49:34 +08:00
|
|
|
export type UpdateMessageEntriesParams = z.infer<typeof UpdateMessageEntriesSchema>;
|
2025-08-08 06:36:01 +08:00
|
|
|
|
|
|
|
/**
|
2025-08-15 03:49:34 +08:00
|
|
|
* Updates message entries using optimized JSONB merge operations.
|
|
|
|
* Performs batch upserts for multiple entries in a single database operation.
|
|
|
|
*
|
|
|
|
* Upsert logic:
|
|
|
|
* - responseMessages: upsert by 'id' field
|
|
|
|
* - reasoningMessages: upsert by 'id' field
|
|
|
|
* - rawLlmMessages: upsert by combination of 'role' and 'toolCallId' in content array
|
2025-08-08 06:36:01 +08:00
|
|
|
*/
|
|
|
|
export async function updateMessageEntries({
|
|
|
|
messageId,
|
2025-08-15 03:49:34 +08:00
|
|
|
rawLlmMessages,
|
|
|
|
responseMessages,
|
|
|
|
reasoningMessages,
|
2025-08-08 06:36:01 +08:00
|
|
|
}: UpdateMessageEntriesParams): Promise<{ success: boolean }> {
|
|
|
|
try {
|
2025-08-15 04:09:53 +08:00
|
|
|
const updates: Record<string, SQL | string> = { updatedAt: new Date().toISOString() };
|
2025-08-08 06:36:01 +08:00
|
|
|
|
2025-08-15 03:49:34 +08:00
|
|
|
// Optimized merge for response messages - upsert by 'id'
|
|
|
|
if (responseMessages?.length) {
|
|
|
|
const newData = JSON.stringify(responseMessages);
|
|
|
|
updates.responseMessages = sql`
|
|
|
|
COALESCE(
|
2025-08-15 04:09:53 +08:00
|
|
|
(SELECT jsonb_agg(value ORDER BY ordinality)
|
2025-08-15 03:49:34 +08:00
|
|
|
FROM (
|
2025-08-15 04:09:53 +08:00
|
|
|
-- Keep existing messages that aren't being updated
|
|
|
|
SELECT value, ordinality
|
|
|
|
FROM jsonb_array_elements(COALESCE(${messages.responseMessages}, '[]'::jsonb)) WITH ORDINALITY AS t(value, ordinality)
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT 1 FROM jsonb_array_elements(${newData}::jsonb) AS new_msg
|
|
|
|
WHERE new_msg->>'id' = t.value->>'id'
|
|
|
|
)
|
|
|
|
UNION ALL
|
|
|
|
-- Add new/updated messages at the end
|
|
|
|
SELECT value, 1000000 + ordinality AS ordinality
|
|
|
|
FROM jsonb_array_elements(${newData}::jsonb) WITH ORDINALITY AS t(value, ordinality)
|
|
|
|
) combined),
|
2025-08-15 03:49:34 +08:00
|
|
|
'[]'::jsonb
|
|
|
|
)`;
|
2025-08-08 06:36:01 +08:00
|
|
|
}
|
|
|
|
|
2025-08-15 03:49:34 +08:00
|
|
|
// Optimized merge for reasoning messages - upsert by 'id'
|
|
|
|
if (reasoningMessages?.length) {
|
|
|
|
const newData = JSON.stringify(reasoningMessages);
|
|
|
|
updates.reasoning = sql`
|
|
|
|
COALESCE(
|
2025-08-15 04:09:53 +08:00
|
|
|
(SELECT jsonb_agg(value ORDER BY ordinality)
|
2025-08-15 03:49:34 +08:00
|
|
|
FROM (
|
2025-08-15 04:09:53 +08:00
|
|
|
-- Keep existing messages that aren't being updated
|
|
|
|
SELECT value, ordinality
|
|
|
|
FROM jsonb_array_elements(COALESCE(${messages.reasoning}, '[]'::jsonb)) WITH ORDINALITY AS t(value, ordinality)
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT 1 FROM jsonb_array_elements(${newData}::jsonb) AS new_msg
|
|
|
|
WHERE new_msg->>'id' = t.value->>'id'
|
|
|
|
)
|
|
|
|
UNION ALL
|
|
|
|
-- Add new/updated messages at the end
|
|
|
|
SELECT value, 1000000 + ordinality AS ordinality
|
|
|
|
FROM jsonb_array_elements(${newData}::jsonb) WITH ORDINALITY AS t(value, ordinality)
|
|
|
|
) combined),
|
2025-08-15 03:49:34 +08:00
|
|
|
'[]'::jsonb
|
|
|
|
)`;
|
2025-08-08 06:36:01 +08:00
|
|
|
}
|
|
|
|
|
2025-08-15 03:49:34 +08:00
|
|
|
// Optimized merge for raw LLM messages - upsert by role + toolCallId combination
|
|
|
|
if (rawLlmMessages?.length) {
|
|
|
|
const newData = JSON.stringify(rawLlmMessages);
|
|
|
|
updates.rawLlmMessages = sql`
|
|
|
|
COALESCE(
|
2025-08-15 04:09:53 +08:00
|
|
|
(SELECT jsonb_agg(value ORDER BY ordinality)
|
2025-08-15 03:49:34 +08:00
|
|
|
FROM (
|
2025-08-15 04:09:53 +08:00
|
|
|
-- Keep existing messages that aren't being updated
|
|
|
|
SELECT value, ordinality
|
|
|
|
FROM jsonb_array_elements(COALESCE(${messages.rawLlmMessages}, '[]'::jsonb)) WITH ORDINALITY AS t(value, ordinality)
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT 1 FROM jsonb_array_elements(${newData}::jsonb) AS new_msg
|
|
|
|
WHERE new_msg->>'role' = t.value->>'role'
|
|
|
|
AND (
|
|
|
|
-- Compare toolCallIds if they exist
|
|
|
|
(SELECT string_agg(content->>'toolCallId', ',' ORDER BY content->>'toolCallId')
|
|
|
|
FROM jsonb_array_elements(new_msg->'content') content
|
|
|
|
WHERE content->>'toolCallId' IS NOT NULL) =
|
|
|
|
(SELECT string_agg(content->>'toolCallId', ',' ORDER BY content->>'toolCallId')
|
|
|
|
FROM jsonb_array_elements(t.value->'content') content
|
|
|
|
WHERE content->>'toolCallId' IS NOT NULL)
|
|
|
|
OR
|
|
|
|
-- Both have no toolCallIds
|
|
|
|
((SELECT COUNT(*) FROM jsonb_array_elements(new_msg->'content') content WHERE content->>'toolCallId' IS NOT NULL) = 0
|
|
|
|
AND (SELECT COUNT(*) FROM jsonb_array_elements(t.value->'content') content WHERE content->>'toolCallId' IS NOT NULL) = 0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
UNION ALL
|
|
|
|
-- Add new/updated messages at the end
|
|
|
|
SELECT value, 1000000 + ordinality AS ordinality
|
|
|
|
FROM jsonb_array_elements(${newData}::jsonb) WITH ORDINALITY AS t(value, ordinality)
|
|
|
|
) combined),
|
2025-08-15 03:49:34 +08:00
|
|
|
'[]'::jsonb
|
|
|
|
)`;
|
2025-08-08 06:36:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
await db
|
|
|
|
.update(messages)
|
2025-08-15 03:49:34 +08:00
|
|
|
.set(updates)
|
2025-08-08 06:36:01 +08:00
|
|
|
.where(and(eq(messages.id, messageId), isNull(messages.deletedAt)));
|
|
|
|
|
|
|
|
return { success: true };
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to update message entries:', error);
|
|
|
|
throw new Error(`Failed to update message entries for message ${messageId}`);
|
|
|
|
}
|
|
|
|
}
|