Files
chess-project/src/lib/apiKeyHelper.ts
T
Stefan 002ed92bea Add mobile app support and opening training feature
This commit implements iOS/Android mobile app support using Capacitor
and adds a comprehensive opening training feature with LLM-powered
explanations.

## Mobile App Infrastructure

- Add Capacitor configuration for iOS/Android builds
- Create mobile build script that excludes API routes
- Update Next.js config for conditional static export
- Add layout components with generateStaticParams for static builds
- Generate 500+ static pages for offline mobile use

## Chess Engine Abstraction

- Create ChessEngine interface for pluggable implementations
- Add LocalEngine (GPL - uses stockfish.js in browser)
- Add RemoteEngine (proprietary - calls API server)
- Factory pattern selects engine based on environment
- Enables GPL compliance for web, proprietary for mobile

## Opening Training Feature

- Interactive opening repertoire training
- Move validation with engine-backed feedback
- LLM explanations using Gemini API
- Wikipedia integration for opening context
- Opening family grouping (e4, d4, c4, etc.)
- Session state management
- Real-time move feedback with evaluation

Components:
- OpeningSelector: Browse and select openings by family
- OpeningTrainer: Main training interface with chessboard
- MoveFeedback: Display move quality and LLM explanations
- WikipediaSummary: Show opening history and context
- ErrorBoundary: Graceful error handling

Services:
- openingLoader: Load and filter opening database
- engineService: Engine evaluation wrapper
- moveValidator: Validate moves against repertoire
- feedbackGenerator: Generate contextual feedback
- wikipediaService: Fetch and cache Wikipedia data
- sessionManager: Track training session state

## Wikipedia Integration

- Automatic Wikipedia article fetching for openings
- Client-side and server-side caching
- Sanitized summaries with proper formatting
- Link opening database to Wikipedia slugs
- API endpoints for on-demand fetching

## Docker Improvements

- Add entrypoint script for automatic data setup
- Fetch Wikipedia data on first container startup
- Generate opening move index automatically
- Remove generated data from git (public/openings/*.json, public/wikipedia/*.json)
- Add READMEs explaining data requirements
- Update .gitignore for generated files

## Dual Licensing Strategy

- Add LICENSING.md explaining dual licensing approach
- GPL-3.0 for web builds (includes Stockfish)
- Proprietary option for mobile builds (no GPL code)
- Single codebase, multiple licensing models
- Legal compliance documented

## API Endpoints

- POST /api/v1/llm/opening-explanation - Get LLM move explanations
- GET /api/v1/wikipedia/summary - Fetch Wikipedia summaries

## Type Updates

- Add openingTraining types
- Update Tutor component to use ChessEngine interface
- Add Gemini error handling types

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-07 18:40:58 +01:00

74 lines
1.7 KiB
TypeScript

/**
* Helper utilities for API key management
*/
export type ApiKeySource = 'localStorage' | 'env' | 'none';
export interface ApiKeyInfo {
key: string | null;
source: ApiKeySource;
anonymized: string;
}
/**
* Anonymize an API key by showing only first 2 and last 2 characters
* Example: "AIzaSyABC...XYZ123" becomes "AI...23"
*/
export function anonymizeApiKey(key: string | null): string {
if (!key || key.length < 8) {
return '••••••••';
}
const first2 = key.substring(0, 2);
const last2 = key.substring(key.length - 2);
return `${first2}${'•'.repeat(6)}${last2}`;
}
/**
* Get the current API key and its source
*/
export function getApiKeyInfo(): ApiKeyInfo {
// Check localStorage first (user-set key takes precedence)
if (typeof window !== 'undefined') {
const storedKey = localStorage.getItem('gemini_api_key');
if (storedKey && storedKey.trim()) {
return {
key: storedKey,
source: 'localStorage',
anonymized: anonymizeApiKey(storedKey),
};
}
}
// Check environment variable (fallback)
const envKey = process.env.NEXT_PUBLIC_GEMINI_API_KEY;
if (envKey && envKey.trim()) {
return {
key: envKey,
source: 'env',
anonymized: anonymizeApiKey(envKey),
};
}
// No key found
return {
key: null,
source: 'none',
anonymized: '••••••••',
};
}
/**
* Get user-friendly description of API key source
*/
export function getApiKeySourceDescription(source: ApiKeySource): string {
switch (source) {
case 'localStorage':
return 'User Settings (localStorage)';
case 'env':
return 'Environment Variable (.env)';
case 'none':
return 'Not configured';
}
}