feat: Add debug mode to inspect LLM prompts and responses

- Add DebugContext to track all LLM interactions
- Create DebugPanel component with floating UI
- Track prompts/responses in Tutor component (move analysis, hints, questions)
- Track prompts/responses in Analysis page (move commentary)
- Add NEXT_PUBLIC_DEBUG environment variable to enable/disable
- Include metadata (FEN, personality, language, etc.) in debug entries
- Add copy-to-clipboard and clear functionality
- Add DEBUG_MODE.md documentation
This commit is contained in:
Stefan
2025-11-27 18:20:12 +01:00
parent 54d92eaae5
commit c319904f00
6 changed files with 352 additions and 3 deletions
+59
View File
@@ -0,0 +1,59 @@
"use client";
import React, { createContext, useContext, useState, ReactNode } from 'react';
export interface DebugEntry {
id: string;
timestamp: number;
type: 'tutor' | 'analysis';
action: string; // e.g., "Best Move", "Hint", "General Question", "Move Analysis"
prompt: string;
response?: string;
metadata?: Record<string, any>;
}
interface DebugContextType {
isDebugMode: boolean;
entries: DebugEntry[];
addEntry: (entry: Omit<DebugEntry, 'id' | 'timestamp'>) => void;
clearEntries: () => void;
}
const DebugContext = createContext<DebugContextType | undefined>(undefined);
export function DebugProvider({ children }: { children: ReactNode }) {
// Check if debug mode is enabled via environment variable
const isDebugMode = process.env.NEXT_PUBLIC_DEBUG === 'true';
const [entries, setEntries] = useState<DebugEntry[]>([]);
const addEntry = (entry: Omit<DebugEntry, 'id' | 'timestamp'>) => {
if (!isDebugMode) return; // Don't track if debug mode is off
const newEntry: DebugEntry = {
...entry,
id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
timestamp: Date.now(),
};
setEntries(prev => [...prev, newEntry]);
};
const clearEntries = () => {
setEntries([]);
};
return (
<DebugContext.Provider value={{ isDebugMode, entries, addEntry, clearEntries }}>
{children}
</DebugContext.Provider>
);
}
export function useDebug() {
const context = useContext(DebugContext);
if (context === undefined) {
throw new Error('useDebug must be used within a DebugProvider');
}
return context;
}