Spaces:
Running
Running
Download algospaced-ui/src/services/reviewService.ts from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 9.34 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/services/reviewService.ts
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/algospaced-ui/src/services/reviewService.ts
-
curl -L -o reviewService.ts https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/services/reviewService.ts
9.34 kB
| import { supabase } from '../firebaseClient'; | |
| import type { CatalogProblem } from './catalogService'; | |
| import type { ProblemSolution, ProblemItem } from '../types/reviewTypes'; | |
| export interface ActivateProblemPayload { | |
| problem_number: number; | |
| name: string; | |
| pattern: string; | |
| difficulty: string; | |
| time_complexity?: string; | |
| space_complexity?: string; | |
| description?: string; | |
| starter_code?: string; | |
| reference_solution?: string; | |
| examples?: Array<{ input: string; output: string; explanation?: string }>; | |
| constraints?: string[]; | |
| leetcode_url?: string; | |
| } | |
| export interface ActivateProblemResult { | |
| success: boolean; | |
| problem_id: string; | |
| fsrs_state: number; | |
| message: string; | |
| } | |
| export interface SaveReferenceResult { | |
| success: boolean; | |
| problem_id: string; | |
| reference_code?: string; | |
| message: string; | |
| } | |
| export interface SaveReferenceMetadata { | |
| problem_number?: number; | |
| problem_name?: string; | |
| pattern?: string; | |
| difficulty?: string; | |
| optimal_time?: string; | |
| optimal_space?: string; | |
| description?: string; | |
| starter_code?: string; | |
| examples?: Array<{ input: string; output: string; explanation?: string }>; | |
| constraints?: string[]; | |
| } | |
| export interface AiSolutionResult { | |
| success: boolean; | |
| solution: string; | |
| source: 'stored' | 'catalog' | 'ai_generated'; | |
| } | |
| export interface SandboxTestCaseResult { | |
| index: number; | |
| status: 'passed' | 'failed' | 'error'; | |
| passed: boolean; | |
| input: unknown; | |
| expected: unknown; | |
| output?: unknown; | |
| got?: unknown; | |
| error?: string; | |
| } | |
| export interface SandboxRunResult { | |
| success: boolean; | |
| status?: 'passed' | 'failed' | 'error'; | |
| passed_count?: number; | |
| total_tests?: number; | |
| duration_ms?: number; | |
| results?: SandboxTestCaseResult[]; | |
| error?: string; | |
| sandbox_type?: string; | |
| sandbox_warning?: string; | |
| } | |
| async function getAuthToken(): Promise<string> { | |
| try { | |
| const { data: { session } } = await supabase.auth.getSession(); | |
| return session ? session.access_token : 'sandbox'; | |
| } catch { | |
| return 'sandbox'; | |
| } | |
| } | |
| /** | |
| * Activates a problem into the FSRS review queue and marks journey progress. | |
| */ | |
| export async function activateProblem(payload: ActivateProblemPayload): Promise<ActivateProblemResult> { | |
| const token = await getAuthToken(); | |
| const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/reviews/activate`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${token}` | |
| }, | |
| body: JSON.stringify(payload) | |
| }); | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({ detail: 'Failed to activate problem' })); | |
| throw new Error(err.detail || 'Failed to activate problem'); | |
| } | |
| return res.json(); | |
| } | |
| /** | |
| * Activates directly from a CatalogProblem object. | |
| */ | |
| export async function activateCatalogProblem(catalogProb: CatalogProblem): Promise<ActivateProblemResult> { | |
| return activateProblem({ | |
| problem_number: catalogProb.problem_number, | |
| name: catalogProb.name, | |
| pattern: catalogProb.pattern, | |
| difficulty: catalogProb.difficulty, | |
| time_complexity: catalogProb.time_complexity, | |
| space_complexity: catalogProb.space_complexity, | |
| description: catalogProb.description, | |
| starter_code: catalogProb.starter_code, | |
| reference_solution: catalogProb.reference_solution, | |
| examples: catalogProb.examples, | |
| constraints: catalogProb.constraints, | |
| leetcode_url: catalogProb.leetcode_url, | |
| }); | |
| } | |
| /** | |
| * Saves verified user code as the official reference solution for a problem. | |
| */ | |
| export async function saveReferenceSolution( | |
| problemId: string | null | undefined, | |
| code: string, | |
| metadata?: SaveReferenceMetadata | number | |
| ): Promise<SaveReferenceResult> { | |
| const token = await getAuthToken(); | |
| const metaObj: SaveReferenceMetadata = | |
| typeof metadata === 'number' | |
| ? { problem_number: metadata } | |
| : (metadata || {}); | |
| const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/reviews/save-reference`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${token}` | |
| }, | |
| body: JSON.stringify({ | |
| problem_id: problemId || undefined, | |
| code, | |
| ...metaObj | |
| }) | |
| }); | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({ detail: 'Failed to save reference solution' })); | |
| throw new Error(err.detail || 'Failed to save reference solution'); | |
| } | |
| return res.json(); | |
| } | |
| /** | |
| * Updates the solutions array for a problem. | |
| */ | |
| export async function updateProblemSolutions( | |
| problemId: string, | |
| solutions: ProblemSolution[], | |
| optimalTime?: string, | |
| optimalSpace?: string, | |
| additionalData?: Partial<ProblemItem> | |
| ): Promise<{ success: boolean; problem_id: string }> { | |
| const token = await getAuthToken(); | |
| const optimalSol = solutions.find((s) => s.is_optimal) || solutions[0]; | |
| const payload = { | |
| name: additionalData?.name || 'Problem', | |
| pattern: optimalSol?.pattern || additionalData?.pattern || 'General', | |
| difficulty: additionalData?.difficulty || 'Medium', | |
| reference_code: optimalSol?.code || additionalData?.reference_code || '', | |
| description: additionalData?.description || '', | |
| problem_number: additionalData?.problem_number ?? null, | |
| optimal_time: optimalTime || optimalSol?.time_complexity || additionalData?.optimal_time || 'O(N)', | |
| optimal_space: optimalSpace || optimalSol?.space_complexity || additionalData?.optimal_space || 'O(1)', | |
| examples: additionalData?.examples, | |
| constraints: additionalData?.constraints, | |
| how_to_solve: additionalData?.how_to_solve || '', | |
| solutions | |
| }; | |
| const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/explorer/problems/${problemId}`, { | |
| method: 'PUT', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${token}` | |
| }, | |
| body: JSON.stringify(payload) | |
| }); | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({ detail: 'Failed to update problem solutions' })); | |
| throw new Error(err.detail || 'Failed to update problem solutions'); | |
| } | |
| return res.json(); | |
| } | |
| /** | |
| * Retrieves the stored reference solution (0 LLM tokens) or generates via AI grimoire. | |
| */ | |
| export async function getOrGenerateAiSolution(params: { | |
| problemId?: string; | |
| problemNumber?: number; | |
| problemName?: string; | |
| pattern?: string; | |
| }): Promise<AiSolutionResult> { | |
| const token = await getAuthToken(); | |
| const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/reviews/ai-solution`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${token}` | |
| }, | |
| body: JSON.stringify({ | |
| problem_id: params.problemId, | |
| problem_number: params.problemNumber, | |
| problem_name: params.problemName, | |
| pattern: params.pattern | |
| }) | |
| }); | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({ detail: 'Failed to fetch solution' })); | |
| throw new Error(err.detail || 'Failed to fetch solution'); | |
| } | |
| return res.json(); | |
| } | |
| /** | |
| * Zero-token test runner: executes Python code in isolated sandbox against test cases. | |
| */ | |
| export async function runSandboxTests( | |
| code: string, | |
| testCases: Array<{ input: unknown; expected: unknown }>, | |
| timeout = 4.0, | |
| starterCode?: string, | |
| problemName?: string | |
| ): Promise<SandboxRunResult> { | |
| const token = await getAuthToken(); | |
| const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/reviews/test-run`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${token}` | |
| }, | |
| body: JSON.stringify({ | |
| code, | |
| test_cases: testCases, | |
| timeout, | |
| starter_code: starterCode, | |
| problem_name: problemName | |
| }) | |
| }); | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({ detail: 'Execution service error' })); | |
| return { | |
| success: false, | |
| error: err.detail || 'Execution service error' | |
| }; | |
| } | |
| return res.json(); | |
| } | |
| export interface BatchActivatePayload { | |
| problems: ActivateProblemPayload[]; | |
| } | |
| export interface BatchActivateResult { | |
| success: boolean; | |
| enrolled_count: number; | |
| message: string; | |
| } | |
| /** | |
| * Batch activates catalog problems into FSRS (State = 0) and marks journey progress. | |
| */ | |
| export async function batchActivateProblems( | |
| problems: CatalogProblem[] | |
| ): Promise<BatchActivateResult> { | |
| const token = await getAuthToken(); | |
| const payload: BatchActivatePayload = { | |
| problems: problems.map((p) => ({ | |
| problem_number: p.problem_number, | |
| name: p.name, | |
| pattern: p.pattern, | |
| difficulty: p.difficulty, | |
| time_complexity: p.time_complexity, | |
| space_complexity: p.space_complexity, | |
| description: p.description, | |
| starter_code: p.starter_code, | |
| reference_solution: p.reference_solution, | |
| examples: p.examples, | |
| constraints: p.constraints, | |
| leetcode_url: p.leetcode_url, | |
| })) | |
| }; | |
| const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/reviews/activate-batch`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${token}` | |
| }, | |
| body: JSON.stringify(payload) | |
| }); | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({ detail: 'Failed to batch activate problems' })); | |
| throw new Error(err.detail || 'Failed to batch activate problems'); | |
| } | |
| return res.json(); | |
| } | |