import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { Button } from './button'; import { Textarea } from './textarea'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from './dialog'; import { Tabs, TabsContent, TabsList, TabsTrigger } from './tabs'; const PromptEditor = ({ userId, onPromptsChange }) => { const [isLoading, setIsLoading] = useState(false); const [prompts, setPrompts] = useState({ system_template: '', user_template: '' }); const [isOpen, setIsOpen] = useState(false); const [saveStatus, setSaveStatus] = useState(''); // Fetch current prompts when the component mounts or userId changes useEffect(() => { if (isOpen && userId) { fetchPrompts(); } }, [userId, isOpen]); const fetchPrompts = async () => { try { setIsLoading(true); const response = await axios.get(`/prompts${userId ? `?user_id=${userId}` : ''}`); setPrompts({ system_template: response.data.system_template, user_template: response.data.user_template }); setIsLoading(false); } catch (error) { console.error('Error fetching prompts:', error); setIsLoading(false); } }; const handleSave = async () => { try { setIsLoading(true); setSaveStatus('Saving...'); await axios.post('/prompts', prompts, { params: userId ? { user_id: userId } : {} }); setSaveStatus('Saved successfully!'); // Notify parent component about the prompt change if (onPromptsChange) { onPromptsChange(prompts); } setTimeout(() => { setSaveStatus(''); setIsOpen(false); }, 1500); setIsLoading(false); } catch (error) { console.error('Error saving prompts:', error); setSaveStatus('Error saving prompts'); setIsLoading(false); } }; const handleReset = async () => { try { setIsLoading(true); await axios.post('/prompts/reset', {}, { params: userId ? { user_id: userId } : {} }); // Fetch the reset prompts await fetchPrompts(); setSaveStatus('Reset to defaults'); setTimeout(() => setSaveStatus(''), 1500); // Notify parent component about the prompt change if (onPromptsChange) { onPromptsChange(prompts); } setIsLoading(false); } catch (error) { console.error('Error resetting prompts:', error); setSaveStatus('Error resetting prompts'); setIsLoading(false); } }; const handlePromptChange = (type, value) => { setPrompts(prev => ({ ...prev, [type]: value })); }; return ( Customize AI Prompts Customize how the AI responds to your questions. Changes are saved automatically to your browser. System Prompt User Prompt

System Prompt Template

This controls how the AI formats its response and interacts with the context.