"use client"; import { useEffect, useState } from "react"; import { Check, ChevronsUpDown, Loader2 } from "lucide-react"; import { Button } from "./ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "./ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover"; import { cn } from "@/lib/utils"; import { toast } from "sonner"; import { Switch } from "./ui/switch"; interface OpenAIConfigProps { openaiApiKey: string; openaiModel: string; webGrounding?: boolean; onInputChange: (value: string | boolean, field: string) => void; } export default function OpenAIConfig({ openaiApiKey, openaiModel, webGrounding, onInputChange }: OpenAIConfigProps) { const [openModelSelect, setOpenModelSelect] = useState(false); const [availableModels, setAvailableModels] = useState([]); const [modelsLoading, setModelsLoading] = useState(false); const [modelsChecked, setModelsChecked] = useState(false); const [apiKey, setApiKey] = useState(openaiApiKey); const openaiUrl = "https://api.openai.com/v1"; useEffect(() => { setAvailableModels([]); setModelsChecked(false); onInputChange("", "openai_model"); }, [apiKey]); const onApiKeyChange = (value: string) => { setApiKey(value); onInputChange(value, "openai_api_key"); }; const fetchAvailableModels = async () => { if (!openaiApiKey) return; setModelsLoading(true); try { const response = await fetch('/api/v1/ppt/openai/models/available', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ url: openaiUrl, api_key: openaiApiKey }), }); if (response.ok) { const data = await response.json(); setAvailableModels(data); setModelsChecked(true); onInputChange("gpt-4.1", "openai_model"); } else { console.error('Failed to fetch models'); setAvailableModels([]); setModelsChecked(true); } } catch (error) { console.error('Error fetching models:', error); toast.error('Error fetching models'); setAvailableModels([]); setModelsChecked(true); } finally { setModelsLoading(false); } }; return (
{/* API Key Input */}
onApiKeyChange(e.target.value)} className="w-full px-4 py-2.5 outline-none border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-colors" placeholder="Enter your API key" />

Your API key will be stored locally and never shared

{/* Check for available models button - show when no models checked or no models found */} {(!modelsChecked || (modelsChecked && availableModels.length === 0)) && (
)} {/* Show message if no models found */} {modelsChecked && availableModels.length === 0 && (

No models found. Please make sure your API key is valid and has access to OpenAI models.

)} {/* Model Selection - only show if models are available */} {modelsChecked && availableModels.length > 0 ? (
No model found. {availableModels.map((model, index) => ( { onInputChange(value, "openai_model"); setOpenModelSelect(false); }} >
{model}
))}
) : null} {/* Web Grounding Toggle - show at the end, below models dropdown */}
onInputChange(checked, "web_grounding")} />

If enabled, the model can use web search grounding when available.

); }