"use client"; import { useQuery } from "@tanstack/react-query"; import { AlertTriangle, Info } from "lucide-react"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { Skeleton } from "@/components/ui/skeleton"; import { Textarea } from "@/components/ui/textarea"; import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service"; import { authenticatedFetch } from "@/lib/auth-utils"; import { cacheKeys } from "@/lib/query-client/cache-keys"; interface PromptConfigManagerProps { searchSpaceId: number; } export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps) { const { data: searchSpace, isLoading: loading, refetch: fetchSearchSpace, } = useQuery({ queryKey: cacheKeys.searchSpaces.detail(searchSpaceId.toString()), queryFn: () => searchSpacesApiService.getSearchSpace({ id: searchSpaceId }), enabled: !!searchSpaceId, }); const [customInstructions, setCustomInstructions] = useState(""); const [saving, setSaving] = useState(false); const [hasChanges, setHasChanges] = useState(false); // Initialize state from fetched search space useEffect(() => { if (searchSpace) { setCustomInstructions(searchSpace.qna_custom_instructions || ""); setHasChanges(false); } }, [searchSpace]); // Track changes useEffect(() => { if (searchSpace) { const currentCustom = searchSpace.qna_custom_instructions || ""; const changed = currentCustom !== customInstructions; setHasChanges(changed); } }, [searchSpace, customInstructions]); const handleSave = async () => { try { setSaving(true); const payload = { qna_custom_instructions: customInstructions.trim() || "", }; const response = await authenticatedFetch( `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces/${searchSpaceId}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), } ); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.detail || "Failed to save system instructions"); } toast.success("System instructions saved successfully"); setHasChanges(false); await fetchSearchSpace(); } catch (error: any) { console.error("Error saving system instructions:", error); toast.error(error.message || "Failed to save system instructions"); } finally { setSaving(false); } }; if (loading) { return (
); } return (
{/* Work in Progress Notice */} Work in Progress: This functionality is currently under development and not yet connected to the backend. Your instructions will be saved but won't affect AI behavior until the feature is fully implemented. System instructions apply to all AI interactions in this search space. They guide how the AI responds, its tone, focus areas, and behavior patterns. {/* System Instructions Card */} Custom System Instructions Provide specific guidelines for how you want the AI to respond. These instructions will be applied to all answers in this search space.