|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect } from 'react'; |
| 4 | + |
| 5 | +interface Wizard { |
| 6 | + name: string; |
| 7 | + category: string; |
| 8 | + description: string; |
| 9 | + capabilities?: string[]; |
| 10 | +} |
| 11 | + |
| 12 | +interface WizardPlaygroundProps { |
| 13 | + category?: 'healthcare' | 'software'; |
| 14 | +} |
| 15 | + |
| 16 | +export default function WizardPlayground({ category }: WizardPlaygroundProps) { |
| 17 | + const [wizards, setWizards] = useState<Wizard[]>([]); |
| 18 | + const [selectedWizard, setSelectedWizard] = useState<Wizard | null>(null); |
| 19 | + const [inputData, setInputData] = useState(''); |
| 20 | + const [output, setOutput] = useState(''); |
| 21 | + const [isLoading, setIsLoading] = useState(false); |
| 22 | + const [isLoadingWizards, setIsLoadingWizards] = useState(true); |
| 23 | + const [error, setError] = useState(''); |
| 24 | + |
| 25 | + // Fetch wizards on component mount |
| 26 | + useEffect(() => { |
| 27 | + fetchWizards(); |
| 28 | + }, [category]); |
| 29 | + |
| 30 | + const fetchWizards = async () => { |
| 31 | + setIsLoadingWizards(true); |
| 32 | + try { |
| 33 | + const response = await fetch('/api/wizards'); |
| 34 | + const data = await response.json(); |
| 35 | + |
| 36 | + let allWizards = data.wizards || []; |
| 37 | + |
| 38 | + // Filter by category if specified |
| 39 | + if (category) { |
| 40 | + allWizards = allWizards.filter((w: Wizard) => w.category === category); |
| 41 | + } |
| 42 | + |
| 43 | + setWizards(allWizards); |
| 44 | + setError(''); |
| 45 | + } catch (err) { |
| 46 | + console.error('Error fetching wizards:', err); |
| 47 | + setError('Failed to load wizards. Backend may be unavailable.'); |
| 48 | + } finally { |
| 49 | + setIsLoadingWizards(false); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + const executeWizard = async () => { |
| 54 | + if (!selectedWizard || !inputData.trim()) { |
| 55 | + setError('Please select a wizard and provide input data'); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + setIsLoading(true); |
| 60 | + setError(''); |
| 61 | + setOutput(''); |
| 62 | + |
| 63 | + try { |
| 64 | + const response = await fetch('/api/wizards/invoke', { |
| 65 | + method: 'POST', |
| 66 | + headers: { |
| 67 | + 'Content-Type': 'application/json', |
| 68 | + }, |
| 69 | + body: JSON.stringify({ |
| 70 | + wizard_name: selectedWizard.name, |
| 71 | + input_data: inputData, |
| 72 | + }), |
| 73 | + }); |
| 74 | + |
| 75 | + const data = await response.json(); |
| 76 | + |
| 77 | + if (!response.ok) { |
| 78 | + throw new Error(data.error || 'Failed to execute wizard'); |
| 79 | + } |
| 80 | + |
| 81 | + setOutput(JSON.stringify(data.result, null, 2)); |
| 82 | + } catch (err) { |
| 83 | + console.error('Error executing wizard:', err); |
| 84 | + setError(err instanceof Error ? err.message : 'Failed to execute wizard'); |
| 85 | + } finally { |
| 86 | + setIsLoading(false); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <div className="max-w-6xl mx-auto"> |
| 92 | + {/* Wizard Selection */} |
| 93 | + <div className="mb-8"> |
| 94 | + <h3 className="text-2xl font-bold mb-4">Select a Wizard</h3> |
| 95 | + |
| 96 | + {isLoadingWizards ? ( |
| 97 | + <div className="text-center py-8"> |
| 98 | + <div className="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-[var(--primary)]"></div> |
| 99 | + <p className="mt-2 text-[var(--text-secondary)]">Loading wizards...</p> |
| 100 | + </div> |
| 101 | + ) : wizards.length === 0 ? ( |
| 102 | + <div className="bg-[var(--border)] bg-opacity-20 rounded-lg p-8 text-center"> |
| 103 | + <p className="text-[var(--text-secondary)]"> |
| 104 | + No wizards available. Make sure the backend is running. |
| 105 | + </p> |
| 106 | + </div> |
| 107 | + ) : ( |
| 108 | + <div className="grid md:grid-cols-2 lg:grid-cols-3 gap-4"> |
| 109 | + {wizards.map((wizard) => ( |
| 110 | + <button |
| 111 | + key={wizard.name} |
| 112 | + onClick={() => setSelectedWizard(wizard)} |
| 113 | + className={`p-6 rounded-lg border-2 text-left transition-all ${ |
| 114 | + selectedWizard?.name === wizard.name |
| 115 | + ? 'border-[var(--primary)] bg-[var(--primary)] bg-opacity-5' |
| 116 | + : 'border-[var(--border)] hover:border-[var(--primary)] hover:border-opacity-50' |
| 117 | + }`} |
| 118 | + > |
| 119 | + <h4 className="font-bold mb-2">{wizard.name}</h4> |
| 120 | + <p className="text-sm text-[var(--text-secondary)] mb-3"> |
| 121 | + {wizard.description} |
| 122 | + </p> |
| 123 | + {wizard.capabilities && wizard.capabilities.length > 0 && ( |
| 124 | + <div className="flex flex-wrap gap-1"> |
| 125 | + {wizard.capabilities.slice(0, 2).map((cap) => ( |
| 126 | + <span |
| 127 | + key={cap} |
| 128 | + className="px-2 py-1 bg-[var(--border)] bg-opacity-30 rounded text-xs" |
| 129 | + > |
| 130 | + {cap.replace('_', ' ')} |
| 131 | + </span> |
| 132 | + ))} |
| 133 | + </div> |
| 134 | + )} |
| 135 | + </button> |
| 136 | + ))} |
| 137 | + </div> |
| 138 | + )} |
| 139 | + </div> |
| 140 | + |
| 141 | + {/* Wizard Execution */} |
| 142 | + {selectedWizard && ( |
| 143 | + <div className="bg-[var(--background)] border-2 border-[var(--border)] rounded-lg p-8"> |
| 144 | + <h3 className="text-2xl font-bold mb-4"> |
| 145 | + Execute: {selectedWizard.name} |
| 146 | + </h3> |
| 147 | + |
| 148 | + {error && ( |
| 149 | + <div className="mb-4 p-4 bg-[var(--error)] bg-opacity-10 border-2 border-[var(--error)] rounded-lg"> |
| 150 | + <p className="text-[var(--error)]">{error}</p> |
| 151 | + </div> |
| 152 | + )} |
| 153 | + |
| 154 | + <div className="mb-6"> |
| 155 | + <label htmlFor="input" className="block text-sm font-bold mb-2"> |
| 156 | + Input Data |
| 157 | + </label> |
| 158 | + <textarea |
| 159 | + id="input" |
| 160 | + rows={8} |
| 161 | + value={inputData} |
| 162 | + onChange={(e) => setInputData(e.target.value)} |
| 163 | + placeholder="Enter input data for the wizard (e.g., patient data, code snippet, etc.)" |
| 164 | + className="w-full px-4 py-3 rounded-lg border-2 border-[var(--border)] bg-[var(--background)] focus:border-[var(--primary)] focus:outline-none font-mono text-sm" |
| 165 | + /> |
| 166 | + </div> |
| 167 | + |
| 168 | + <button |
| 169 | + onClick={executeWizard} |
| 170 | + disabled={isLoading || !inputData.trim()} |
| 171 | + className="w-full btn btn-primary text-lg py-4 disabled:opacity-50 disabled:cursor-not-allowed" |
| 172 | + > |
| 173 | + {isLoading ? ( |
| 174 | + <span className="flex items-center justify-center gap-2"> |
| 175 | + <span className="inline-block animate-spin rounded-full h-5 w-5 border-b-2 border-white"></span> |
| 176 | + Executing... |
| 177 | + </span> |
| 178 | + ) : ( |
| 179 | + 'Execute Wizard' |
| 180 | + )} |
| 181 | + </button> |
| 182 | + |
| 183 | + {output && ( |
| 184 | + <div className="mt-6"> |
| 185 | + <label className="block text-sm font-bold mb-2">Output</label> |
| 186 | + <pre className="w-full px-4 py-3 rounded-lg border-2 border-[var(--success)] bg-[var(--success)] bg-opacity-5 overflow-x-auto font-mono text-sm whitespace-pre-wrap"> |
| 187 | + {output} |
| 188 | + </pre> |
| 189 | + </div> |
| 190 | + )} |
| 191 | + </div> |
| 192 | + )} |
| 193 | + </div> |
| 194 | + ); |
| 195 | +} |
0 commit comments