30 lines
900 B
TypeScript
30 lines
900 B
TypeScript
import { getBackendBaseURL } from '@/core/config';
|
|
import { apiFetch } from '@/core/api';
|
|
|
|
export interface AIWritingSampleExtractResult {
|
|
filename: string;
|
|
text: string;
|
|
char_count: number;
|
|
truncated: boolean;
|
|
}
|
|
|
|
export async function extractAIWritingSample(file: File): Promise<AIWritingSampleExtractResult> {
|
|
const form = new FormData();
|
|
form.append('file', file);
|
|
const resp = await apiFetch(`${getBackendBaseURL()}/api/ai-writing/sample/extract`, {
|
|
method: 'POST',
|
|
body: form,
|
|
});
|
|
if (!resp.ok) {
|
|
const text = await resp.text().catch(() => '');
|
|
let detail = '';
|
|
try {
|
|
const parsed = text ? JSON.parse(text) as { detail?: unknown } : null;
|
|
detail = parsed?.detail ? String(parsed.detail) : '';
|
|
} catch {}
|
|
if (detail) throw new Error(detail);
|
|
throw new Error(text || `样文解析失败:${resp.status}`);
|
|
}
|
|
return resp.json();
|
|
}
|