chore: move server functions to db/utils (#951)
This commit is contained in:
parent
a159b77fcf
commit
24cb2ce19b
5 changed files with 22 additions and 105 deletions
100
lib/utils.ts
100
lib/utils.ts
|
|
@ -1,15 +1,7 @@
|
|||
import {
|
||||
generateId,
|
||||
type CoreAssistantMessage,
|
||||
type CoreToolMessage,
|
||||
type Message,
|
||||
type UIMessage,
|
||||
} from 'ai';
|
||||
import type { CoreAssistantMessage, CoreToolMessage, UIMessage } from 'ai';
|
||||
import { type ClassValue, clsx } from 'clsx';
|
||||
import { twMerge } from 'tailwind-merge';
|
||||
|
||||
import type { Document } from '@/lib/db/schema';
|
||||
import { genSaltSync, hashSync } from 'bcrypt-ts';
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
|
|
@ -52,90 +44,9 @@ export function generateUUID(): string {
|
|||
});
|
||||
}
|
||||
|
||||
function addToolMessageToChat({
|
||||
toolMessage,
|
||||
messages,
|
||||
}: {
|
||||
toolMessage: CoreToolMessage;
|
||||
messages: Array<Message>;
|
||||
}): Array<Message> {
|
||||
return messages.map((message) => {
|
||||
if (message.toolInvocations) {
|
||||
return {
|
||||
...message,
|
||||
toolInvocations: message.toolInvocations.map((toolInvocation) => {
|
||||
const toolResult = toolMessage.content.find(
|
||||
(tool) => tool.toolCallId === toolInvocation.toolCallId,
|
||||
);
|
||||
|
||||
if (toolResult) {
|
||||
return {
|
||||
...toolInvocation,
|
||||
state: 'result',
|
||||
result: toolResult.result,
|
||||
};
|
||||
}
|
||||
|
||||
return toolInvocation;
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return message;
|
||||
});
|
||||
}
|
||||
|
||||
type ResponseMessageWithoutId = CoreToolMessage | CoreAssistantMessage;
|
||||
type ResponseMessage = ResponseMessageWithoutId & { id: string };
|
||||
|
||||
export function sanitizeResponseMessages({
|
||||
messages,
|
||||
reasoning,
|
||||
}: {
|
||||
messages: Array<ResponseMessage>;
|
||||
reasoning: string | undefined;
|
||||
}) {
|
||||
const toolResultIds: Array<string> = [];
|
||||
|
||||
for (const message of messages) {
|
||||
if (message.role === 'tool') {
|
||||
for (const content of message.content) {
|
||||
if (content.type === 'tool-result') {
|
||||
toolResultIds.push(content.toolCallId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const messagesBySanitizedContent = messages.map((message) => {
|
||||
if (message.role !== 'assistant') return message;
|
||||
|
||||
if (typeof message.content === 'string') return message;
|
||||
|
||||
const sanitizedContent = message.content.filter((content) =>
|
||||
content.type === 'tool-call'
|
||||
? toolResultIds.includes(content.toolCallId)
|
||||
: content.type === 'text'
|
||||
? content.text.length > 0
|
||||
: true,
|
||||
);
|
||||
|
||||
if (reasoning) {
|
||||
// @ts-expect-error: reasoning message parts in sdk is wip
|
||||
sanitizedContent.push({ type: 'reasoning', reasoning });
|
||||
}
|
||||
|
||||
return {
|
||||
...message,
|
||||
content: sanitizedContent,
|
||||
};
|
||||
});
|
||||
|
||||
return messagesBySanitizedContent.filter(
|
||||
(message) => message.content.length > 0,
|
||||
);
|
||||
}
|
||||
|
||||
export function getMostRecentUserMessage(messages: Array<UIMessage>) {
|
||||
const userMessages = messages.filter((message) => message.role === 'user');
|
||||
return userMessages.at(-1);
|
||||
|
|
@ -162,12 +73,3 @@ export function getTrailingMessageId({
|
|||
|
||||
return trailingMessage.id;
|
||||
}
|
||||
|
||||
export function generateDummyPassword() {
|
||||
const password = generateId(12);
|
||||
|
||||
const salt = genSaltSync(10);
|
||||
const hash = hashSync(password, salt);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue