import { createServerFn } from "@tanstack/react-start"; import { z } from "zod"; import { requireSupabaseAuth } from "@/integrations/supabase/auth-middleware"; import { CLOUD_LIVE_MODEL, CLOUD_LIVE_VOICE, CLOUD_TOOLS, cloudInstructions, cloudProjects } from "./cloud-policy"; export const getCloudWorkspace = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]).handler(async ({ context }) => { const { data, error } = await context.supabase.from("voice_builds").select("id,session_id,prompt,created_at,deployed_url").eq("user_id", context.userId).order("created_at", { ascending: false }).limit(200); if (error) throw new Error("Your saved cloud projects could not load. Try refreshing."); return { projects: cloudProjects(data ?? []), versions: data?.length ?? 0, checkedAt: new Date().toISOString(), publishingConfigured: Boolean(process.env["Cloudfare_Api_keys"]), voiceConfigured: Boolean(process.env["GEMINI_API_KEY"]), limit: 200 }; }); export const startCloudLive = createServerFn({ method: "POST" }).middleware([requireSupabaseAuth]) .inputValidator((input: unknown) => z.object({ name: z.string().trim().min(1).max(40) }).parse(input)) .handler(async ({ data }) => { const { mintLiveToken } = await import("./gemini-live.server"); return mintLiveToken(process.env["GEMINI_API_KEY"], { model: `models/${CLOUD_LIVE_MODEL}`, generationConfig: { responseModalities: ["AUDIO"], speechConfig: { voiceConfig: { prebuiltVoiceConfig: { voiceName: CLOUD_LIVE_VOICE } } } }, systemInstruction: { parts: [{ text: cloudInstructions(data.name) }] }, inputAudioTranscription: {}, outputAudioTranscription: {}, tools: CLOUD_TOOLS, }); });