From 24cb2ce19b20599a1abd9ca11366389b51b0fa6e Mon Sep 17 00:00:00 2001 From: Jeremy Date: Tue, 22 Apr 2025 22:40:01 -0700 Subject: [PATCH] chore: move server functions to db/utils (#951) --- lib/constants.ts | 2 +- lib/db/queries.ts | 7 ++-- lib/db/utils.ts | 16 ++++++++ lib/utils.ts | 100 +--------------------------------------------- package.json | 2 +- 5 files changed, 22 insertions(+), 105 deletions(-) create mode 100644 lib/db/utils.ts diff --git a/lib/constants.ts b/lib/constants.ts index 9e68601..d0b573b 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -1,4 +1,4 @@ -import { generateDummyPassword } from './utils'; +import { generateDummyPassword } from './db/utils'; export const isProductionEnvironment = process.env.NODE_ENV === 'production'; diff --git a/lib/db/queries.ts b/lib/db/queries.ts index d79e7a1..911bc73 100644 --- a/lib/db/queries.ts +++ b/lib/db/queries.ts @@ -1,6 +1,5 @@ import 'server-only'; -import { genSaltSync, hashSync } from 'bcrypt-ts'; import { and, asc, @@ -28,6 +27,7 @@ import { type Chat, } from './schema'; import type { ArtifactKind } from '@/components/artifact'; +import { generateHashedPassword } from './utils'; // Optionally, if not using email/pass login, you can // use the Drizzle adapter for Auth.js / NextAuth @@ -47,11 +47,10 @@ export async function getUser(email: string): Promise> { } export async function createUser(email: string, password: string) { - const salt = genSaltSync(10); - const hash = hashSync(password, salt); + const hashedPassword = generateHashedPassword(password); try { - return await db.insert(user).values({ email, password: hash }); + return await db.insert(user).values({ email, password: hashedPassword }); } catch (error) { console.error('Failed to create user in database'); throw error; diff --git a/lib/db/utils.ts b/lib/db/utils.ts new file mode 100644 index 0000000..be097c0 --- /dev/null +++ b/lib/db/utils.ts @@ -0,0 +1,16 @@ +import { generateId } from 'ai'; +import { genSaltSync, hashSync } from 'bcrypt-ts'; + +export function generateHashedPassword(password: string) { + const salt = genSaltSync(10); + const hash = hashSync(password, salt); + + return hash; +} + +export function generateDummyPassword() { + const password = generateId(12); + const hashedPassword = generateHashedPassword(password); + + return hashedPassword; +} diff --git a/lib/utils.ts b/lib/utils.ts index e24bc34..064a216 100644 --- a/lib/utils.ts +++ b/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; -}): Array { - 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; - reasoning: string | undefined; -}) { - const toolResultIds: Array = []; - - 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) { 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; -} diff --git a/package.json b/package.json index 926741d..76beb44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ai-chatbot", - "version": "3.0.5", + "version": "3.0.6", "private": true, "scripts": { "dev": "next dev --turbo",