"use server"; import { eq } from "drizzle-orm"; import { revalidatePath } from "next/cache"; import { z } from "zod"; import { db } from "@/lib/db"; import { DEFAULT_ASSISTANT_NAME, MAX_ASSISTANT_NAME_LENGTH, MAX_ASSISTANT_SYSTEM_PROMPT_LENGTH, } from "@/lib/assistant-config"; import { users } from "@/modules/_core/schema"; import { getCurrentSession } from "@/lib/session"; const assistantNameSchema = z .string() .trim() .min(1, "Name is required") .max(MAX_ASSISTANT_NAME_LENGTH); const assistantSystemPromptSchema = z .string() .trim() .min(1, "Prompt cannot be empty") .max(MAX_ASSISTANT_SYSTEM_PROMPT_LENGTH); function revalidateAssistantSurfaces() { revalidatePath("/settings"); revalidatePath("/", "layout"); } export async function setAssistantEnabled(enabled: boolean): Promise { const { user } = await getCurrentSession(); await db.update(users).set({ assistantEnabled: enabled }).where(eq(users.id, user.id)); revalidateAssistantSurfaces(); } export async function setAssistantName(name: string): Promise { const parsed = assistantNameSchema.parse(name); const { user } = await getCurrentSession(); await db.update(users).set({ assistantName: parsed }).where(eq(users.id, user.id)); revalidateAssistantSurfaces(); } export async function setAssistantSystemPrompt(prompt: string | null): Promise { const { user } = await getCurrentSession(); const normalized = prompt === null ? null : assistantSystemPromptSchema.parse(prompt); await db.update(users).set({ assistantSystemPrompt: normalized }).where(eq(users.id, user.id)); revalidateAssistantSurfaces(); } export async function resetAssistantName(): Promise { await setAssistantName(DEFAULT_ASSISTANT_NAME); } export async function resetAssistantSystemPrompt(): Promise { await setAssistantSystemPrompt(null); }