Fix de-dup of user messages
This commit is contained in:
@@ -16,10 +16,23 @@ type IncomingChatMessage = {
|
|||||||
name?: string;
|
name?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function sameMessage(a: IncomingChatMessage, b: IncomingChatMessage) {
|
function sameMessage(
|
||||||
|
a: { role: string; content: string; name?: string | null },
|
||||||
|
b: { role: string; content: string; name?: string | null }
|
||||||
|
) {
|
||||||
return a.role === b.role && a.content === b.content && (a.name ?? null) === (b.name ?? null);
|
return a.role === b.role && a.content === b.content && (a.name ?? null) === (b.name ?? null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isToolCallLogMetadata(value: unknown) {
|
||||||
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
||||||
|
const record = value as Record<string, unknown>;
|
||||||
|
return record.kind === "tool_call";
|
||||||
|
}
|
||||||
|
|
||||||
|
function isToolCallLogMessage(message: { role: string; metadata: unknown }) {
|
||||||
|
return message.role === "tool" && isToolCallLogMetadata(message.metadata);
|
||||||
|
}
|
||||||
|
|
||||||
async function storeNonAssistantMessages(chatId: string, messages: IncomingChatMessage[]) {
|
async function storeNonAssistantMessages(chatId: string, messages: IncomingChatMessage[]) {
|
||||||
const incoming = messages.filter((m) => m.role !== "assistant");
|
const incoming = messages.filter((m) => m.role !== "assistant");
|
||||||
if (!incoming.length) return;
|
if (!incoming.length) return;
|
||||||
@@ -27,13 +40,13 @@ async function storeNonAssistantMessages(chatId: string, messages: IncomingChatM
|
|||||||
const existing = await prisma.message.findMany({
|
const existing = await prisma.message.findMany({
|
||||||
where: { chatId },
|
where: { chatId },
|
||||||
orderBy: { createdAt: "asc" },
|
orderBy: { createdAt: "asc" },
|
||||||
select: { role: true, content: true, name: true },
|
select: { role: true, content: true, name: true, metadata: true },
|
||||||
});
|
});
|
||||||
const existingNonAssistant = existing.filter((m) => m.role !== "assistant");
|
const existingNonAssistant = existing.filter((m) => m.role !== "assistant" && !isToolCallLogMessage(m));
|
||||||
|
|
||||||
let sharedPrefix = 0;
|
let sharedPrefix = 0;
|
||||||
const max = Math.min(existingNonAssistant.length, incoming.length);
|
const max = Math.min(existingNonAssistant.length, incoming.length);
|
||||||
while (sharedPrefix < max && sameMessage(existingNonAssistant[sharedPrefix] as IncomingChatMessage, incoming[sharedPrefix])) {
|
while (sharedPrefix < max && sameMessage(existingNonAssistant[sharedPrefix], incoming[sharedPrefix])) {
|
||||||
sharedPrefix += 1;
|
sharedPrefix += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user