mirror of https://github.com/buster-so/buster.git
Refactor updateMessageEntries for cache-first streaming approach
- Updated logic to prioritize cache as the source of truth during streaming, with asynchronous database updates for persistence. - Improved error handling for background database updates, ensuring cache validity even if DB update fails. - Cleaned up mergeResponseMessages and mergeReasoningMessages functions by removing redundant code.
This commit is contained in:
parent
0d44a230d7
commit
ce8d9a3064
|
@ -19,11 +19,9 @@ export function mergeResponseMessages(
|
|||
|
||||
// Create a map of new messages by ID
|
||||
const updateMap = new Map<string, ChatMessageResponseMessage>();
|
||||
const updateMap = new Map<string, ChatMessageResponseMessage>();
|
||||
|
||||
for (const msg of updates) {
|
||||
updateMap.set(msg.id, msg);
|
||||
updateIds.add(msg.id);
|
||||
}
|
||||
|
||||
// Keep track of which IDs we've already processed
|
||||
|
@ -67,11 +65,9 @@ export function mergeReasoningMessages(
|
|||
|
||||
// Create a map of new messages by ID
|
||||
const updateMap = new Map<string, ChatMessageReasoningMessage>();
|
||||
const updateMap = new Map<string, ChatMessageReasoningMessage>();
|
||||
|
||||
for (const msg of updates) {
|
||||
updateMap.set(msg.id, msg);
|
||||
updateIds.add(msg.id);
|
||||
}
|
||||
|
||||
// Keep track of which IDs we've already processed
|
||||
|
|
|
@ -22,8 +22,8 @@ const UpdateMessageEntriesSchema = z.object({
|
|||
export type UpdateMessageEntriesParams = z.infer<typeof UpdateMessageEntriesSchema>;
|
||||
|
||||
/**
|
||||
* Updates message entries using TypeScript-based merge logic with write-through caching.
|
||||
* Fetches existing entries from cache/database, merges with updates, and saves back.
|
||||
* Updates message entries with cache-first approach for streaming.
|
||||
* Cache is the source of truth during streaming, DB is updated for persistence.
|
||||
*
|
||||
* Merge logic:
|
||||
* - responseMessages: upsert by 'id' field, maintaining order
|
||||
|
@ -57,7 +57,11 @@ export async function updateMessageEntries({
|
|||
: existingEntries.rawLlmMessages,
|
||||
};
|
||||
|
||||
// Update database with merged entries
|
||||
// Update cache immediately (cache is source of truth during streaming)
|
||||
messageEntriesCache.set(messageId, mergedEntries);
|
||||
|
||||
// Update database asynchronously for persistence (fire-and-forget)
|
||||
// If this fails, cache still has the latest state for next update
|
||||
const updateData: Record<string, unknown> = {
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
|
@ -74,18 +78,14 @@ export async function updateMessageEntries({
|
|||
updateData.rawLlmMessages = mergedEntries.rawLlmMessages;
|
||||
}
|
||||
|
||||
await db
|
||||
.update(messages)
|
||||
// Non-blocking DB update - don't await
|
||||
db.update(messages)
|
||||
.set(updateData)
|
||||
.where(and(eq(messages.id, messageId), isNull(messages.deletedAt)));
|
||||
|
||||
await db
|
||||
.update(messages)
|
||||
.set(updateData)
|
||||
.where(and(eq(messages.id, messageId), isNull(messages.deletedAt)));
|
||||
|
||||
// Write-through: update cache with merged entries only after successful DB update
|
||||
messageEntriesCache.set(messageId, mergedEntries);
|
||||
.where(and(eq(messages.id, messageId), isNull(messages.deletedAt)))
|
||||
.catch((error) => {
|
||||
// Log but don't fail - cache has the truth
|
||||
console.error('Background DB update failed (cache still valid):', error);
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
|
|
Loading…
Reference in New Issue