Initial commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { request, setToken } from './client';
|
||||
import type { AuthResult } from './types';
|
||||
|
||||
export async function register(email: string, password: string) {
|
||||
const result = await request<AuthResult>('/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
email,
|
||||
password,
|
||||
ui_language: 'de-DE',
|
||||
target_language: 'en-US',
|
||||
daily_goal_minutes: 10
|
||||
})
|
||||
});
|
||||
setToken(result.token);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function login(email: string, password: string) {
|
||||
const result = await request<AuthResult>('/auth/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ email, password })
|
||||
});
|
||||
setToken(result.token);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
const API_BASE = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8080';
|
||||
|
||||
export class ApiError extends Error {
|
||||
status: number;
|
||||
requestId: string;
|
||||
payload: unknown;
|
||||
|
||||
constructor(status: number, message: string, requestId = '', payload: unknown = null) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.requestId = requestId;
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
||||
|
||||
export function getToken() {
|
||||
return localStorage.getItem('speech_token');
|
||||
}
|
||||
|
||||
export function setToken(token: string) {
|
||||
localStorage.setItem('speech_token', token);
|
||||
}
|
||||
|
||||
export function clearToken() {
|
||||
localStorage.removeItem('speech_token');
|
||||
}
|
||||
|
||||
export async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
|
||||
const headers = new Headers(options.headers);
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
headers.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
if (options.body && !(options.body instanceof FormData) && !headers.has('Content-Type')) {
|
||||
headers.set('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE}${path}`, {
|
||||
...options,
|
||||
headers
|
||||
});
|
||||
const isJSON = response.headers.get('content-type')?.includes('application/json');
|
||||
const requestId = response.headers.get('x-request-id') ?? '';
|
||||
const payload = isJSON ? await response.json() : await response.text();
|
||||
|
||||
if (!response.ok) {
|
||||
const message = typeof payload === 'object' && payload?.error ? payload.error : response.statusText;
|
||||
throw new ApiError(response.status, message, requestId, payload);
|
||||
}
|
||||
return payload as T;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { request } from './client';
|
||||
import type { TeacherResponse } from './types';
|
||||
|
||||
export type ConversationResponse = {
|
||||
session?: { id: string };
|
||||
turn?: { text: string };
|
||||
user_turn?: { text: string };
|
||||
teacher_turn?: { text: string };
|
||||
teacher: TeacherResponse;
|
||||
audio?: { mime_type: string; base64: string } | null;
|
||||
};
|
||||
|
||||
export function startConversation() {
|
||||
return request<ConversationResponse>('/conversation/start', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({})
|
||||
});
|
||||
}
|
||||
|
||||
export function sendConversationTurn(sessionId: string, text: string) {
|
||||
return request<ConversationResponse>(`/conversation/${sessionId}/turn`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ text })
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { request } from './client';
|
||||
|
||||
export type DebugCheck = {
|
||||
ok: boolean;
|
||||
status: string;
|
||||
duration_ms?: number;
|
||||
http_status?: number;
|
||||
error?: string;
|
||||
details?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type DebugStatus = {
|
||||
request_id: string;
|
||||
debug: {
|
||||
enabled: boolean;
|
||||
verbose: boolean;
|
||||
log_level: string;
|
||||
};
|
||||
config: Record<string, unknown>;
|
||||
checks: {
|
||||
db: DebugDB;
|
||||
azure: DebugCheck;
|
||||
flowise: DebugCheck;
|
||||
};
|
||||
};
|
||||
|
||||
export type DebugDB = {
|
||||
ok: boolean;
|
||||
counts: Record<string, number>;
|
||||
seed_language_count: number;
|
||||
seed_exercise_count: number;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
export function fetchDebugStatus() {
|
||||
return request<DebugStatus>('/debug/status');
|
||||
}
|
||||
|
||||
export function fetchDebugDB() {
|
||||
return request<{ request_id: string; db: DebugDB }>('/debug/db');
|
||||
}
|
||||
|
||||
export function fetchDebugFlowise() {
|
||||
return request<{ request_id: string; flowise: DebugCheck }>('/debug/flowise');
|
||||
}
|
||||
|
||||
export function fetchDebugAzure() {
|
||||
return request<{ request_id: string; azure: DebugCheck }>('/debug/azure');
|
||||
}
|
||||
|
||||
export function fetchDebugRuntime() {
|
||||
return request<{ request_id: string; runtime: Record<string, unknown> }>('/debug/runtime');
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { request } from './client';
|
||||
import type { Dashboard, Exercise, Lesson, ProgressSummary } from './types';
|
||||
|
||||
export function fetchDashboard() {
|
||||
return request<Dashboard>('/dashboard');
|
||||
}
|
||||
|
||||
export function fetchProgress() {
|
||||
return request<ProgressSummary>('/progress');
|
||||
}
|
||||
|
||||
export function fetchNextLesson() {
|
||||
return request<{ lesson: Lesson; exercise: Exercise }>('/lessons/next', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({})
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { request } from './client';
|
||||
import type { AttemptResponse } from './types';
|
||||
|
||||
export function submitAttempt(exerciseId: string, audio: Blob) {
|
||||
const form = new FormData();
|
||||
form.append('exercise_id', exerciseId);
|
||||
form.append('audio', audio, `attempt-${Date.now()}.wav`);
|
||||
return request<AttemptResponse>('/practice/attempts', {
|
||||
method: 'POST',
|
||||
body: form
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
export type User = {
|
||||
id: string;
|
||||
email: string;
|
||||
ui_language: string;
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type LearnerProfile = {
|
||||
id: string;
|
||||
user_id: string;
|
||||
target_language: string;
|
||||
cefr_level: string;
|
||||
daily_goal_minutes: number;
|
||||
};
|
||||
|
||||
export type Lesson = {
|
||||
id: string;
|
||||
target_language: string;
|
||||
cefr_level: string;
|
||||
topic: string;
|
||||
title: string;
|
||||
};
|
||||
|
||||
export type Exercise = {
|
||||
id: string;
|
||||
lesson_id: string;
|
||||
target_language: string;
|
||||
cefr_level: string;
|
||||
type: string;
|
||||
prompt_text: string;
|
||||
target_text: string;
|
||||
difficulty: number;
|
||||
};
|
||||
|
||||
export type WordScore = {
|
||||
word: string;
|
||||
accuracy: number;
|
||||
error_type?: string;
|
||||
phonemes?: string[];
|
||||
highlight: 'good' | 'ok' | 'weak';
|
||||
};
|
||||
|
||||
export type Attempt = {
|
||||
id: string;
|
||||
exercise_id: string;
|
||||
target_language: string;
|
||||
recognized_text: string;
|
||||
overall_score: number;
|
||||
accuracy_score: number;
|
||||
fluency_score: number;
|
||||
completeness_score: number;
|
||||
prosody_score: number;
|
||||
word_scores: WordScore[];
|
||||
teacher_feedback: string;
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type ProgressSummary = {
|
||||
total_attempts: number;
|
||||
average_score: number;
|
||||
weak_words: WordScore[] | null;
|
||||
recent_attempts: Attempt[] | null;
|
||||
daily_goal_minutes: number;
|
||||
};
|
||||
|
||||
export type Dashboard = {
|
||||
user: User;
|
||||
profile: LearnerProfile;
|
||||
progress: ProgressSummary;
|
||||
next_lesson: Lesson;
|
||||
next_exercise: Exercise;
|
||||
};
|
||||
|
||||
export type TeacherResponse = {
|
||||
exercise_type: string;
|
||||
target_text: string;
|
||||
teacher_feedback: string;
|
||||
tts_text: string;
|
||||
ssml: string;
|
||||
next_recommendation: string;
|
||||
};
|
||||
|
||||
export type AuthResult = {
|
||||
token: string;
|
||||
user: User;
|
||||
profile: LearnerProfile;
|
||||
};
|
||||
|
||||
export type AttemptResponse = {
|
||||
attempt: Attempt;
|
||||
teacher: TeacherResponse;
|
||||
provider: string;
|
||||
};
|
||||
Reference in New Issue
Block a user