"use client" import * as React from "react" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Textarea } from "@/components/ui/textarea" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from "@/components/ui/select" import { SettingsIcon, EraserIcon, ShieldCheckIcon } from "lucide-react" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { z } from "zod" import { toast } from "sonner" import { useAppDispatch, useAppSelector } from "@/common/hooks" import { ECozeBaseUrl } from "@/common/constant" import { setAgentSettings, setCozeSettings, resetCozeSettings, resetDifySettings, setGlobalSettingsDialog, setDifySettings, } from "@/store/reducers/global" const TABS_OPTIONS = [ { label: "Agent", value: "agent", }, { label: "Coze", value: "coze", }, { label: "Dify", value: "dify", }, ] export const useSettingsTabs = () => { const [tabs, setTabs] = React.useState(TABS_OPTIONS) const graphName = useAppSelector((state) => state.global.graphName) const enableCozeSettingsMemo = React.useMemo(() => { return isCozeGraph(graphName) }, [graphName]) const enableDifySettingsMemo = React.useMemo(() => { return isDifyGraph(graphName) }, [graphName]) const enableGreetingsOrPromptMemo: { greeting: boolean, prompt: boolean } = React.useMemo(() => { if (graphName === "va_gemini_v2v") { return { greeting: false, prompt: true, } } else if (graphName === "va_dify_azure") { return { greeting: true, prompt: false, } } else if (graphName === "story_teller_stt_integrated") { return { greeting: true, prompt: false, } } return { greeting: true, prompt: true, } }, [graphName]) React.useEffect(() => { if (enableCozeSettingsMemo) { setTabs((prev) => [ { label: "Agent", value: "agent" }, { label: "Coze", value: "coze" }, ] ) } else if (enableDifySettingsMemo) { setTabs((prev) => [ { label: "Agent", value: "agent" }, { label: "Dify", value: "dify" }, ] ) } else { setTabs((prev) => prev.filter((tab) => tab.value !== "coze" && tab.value !== "dify")) } }, [enableCozeSettingsMemo, enableDifySettingsMemo]) return { tabs, enableGreetingsOrPromptMemo, } } export default function SettingsDialog() { const dispatch = useAppDispatch() const globalSettingsDialog = useAppSelector( (state) => state.global.globalSettingsDialog, ) const { tabs, enableGreetingsOrPromptMemo } = useSettingsTabs() const handleClose = () => { dispatch(setGlobalSettingsDialog({ open: false, tab: undefined })) } return ( dispatch(setGlobalSettingsDialog({ open, tab: undefined })) } > Settings {tabs.length > 1 && ( {tabs.map((tab) => ( {tab.label} ))} )} ) } const formSchema = z.object({ greeting: z.string().optional(), prompt: z.string().optional(), }) export function CommonAgentSettingsTab(props: { enableGreeting?: boolean enablePrompt?: boolean handleClose?: () => void handleSubmit?: (values: z.infer) => void }) { const { handleSubmit, enableGreeting, enablePrompt } = props const dispatch = useAppDispatch() const agentSettings = useAppSelector((state) => state.global.agentSettings) const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { greeting: agentSettings.greeting, prompt: agentSettings.prompt, }, }) function onSubmit(values: z.infer) { console.log("Form Values:", values) dispatch(setAgentSettings(values)) handleSubmit?.(values) } return ( {enableGreeting && ( Greeting )} />} {enablePrompt && ( Prompt )} />} Save Agent Settings ) } export const cozeSettingsFormSchema = z.object({ token: z .string({ message: "Token is required", }) .min(1), bot_id: z .string({ message: "Bot ID is required", }) .min(1), base_url: z.nativeEnum(ECozeBaseUrl).default(ECozeBaseUrl.GLOBAL), }) export const isCozeGraph = (graphName: string) => { return graphName.toLowerCase().includes("coze") } export function CozeSettingsTab(props: { handleClose?: () => void handleSubmit?: (values: z.infer) => void }) { const { handleSubmit } = props const dispatch = useAppDispatch() const cozeSettings = useAppSelector((state) => state.global.cozeSettings) const form = useForm>({ resolver: zodResolver(cozeSettingsFormSchema), defaultValues: { token: cozeSettings.token, bot_id: cozeSettings.bot_id, base_url: cozeSettings.base_url as z.infer< typeof cozeSettingsFormSchema >["base_url"], }, }) function onSubmit(values: z.infer) { console.log("Coze Form Values:", values) dispatch(setCozeSettings(values)) handleSubmit?.(values) } return ( ( Bot ID* )} /> ( Token* )} /> ( Base URL* {ECozeBaseUrl.GLOBAL} {ECozeBaseUrl.CN} {/* {ECozeBaseUrl.CN} */} )} /> { e.preventDefault() e.stopPropagation() form.reset({ token: "", bot_id: "", base_url: ECozeBaseUrl.GLOBAL, }) dispatch(resetCozeSettings()) toast.success("Coze settings reset") }} > Save Coze Settings Settings are saved in your browser only ) } export const difySettingsFormSchema = z.object({ api_key: z .string({ message: "API Key is required", }) .min(1), }) export const isDifyGraph = (graphName: string) => { return graphName.toLowerCase().includes("dify") } export function DifySettingsTab(props: { handleClose?: () => void handleSubmit?: (values: z.infer) => void }) { const { handleSubmit } = props const dispatch = useAppDispatch() const difySettings = useAppSelector((state) => state.global.difySettings) const form = useForm>({ resolver: zodResolver(difySettingsFormSchema), defaultValues: { api_key: difySettings.api_key, }, }) function onSubmit(values: z.infer) { console.log("Dify Form Values:", values) dispatch(setDifySettings(values)) handleSubmit?.(values) } return ( ( API Key* )} /> { e.preventDefault() e.stopPropagation() form.reset({ api_key: "", }) dispatch(resetDifySettings()) toast.success("Dify settings reset") }} > Save Dify Settings Settings are saved in your browser only ) }