import { FASTAPI_BASE_URL, authHeaders, readError, type ApiEnvelope } from './session' export type ExamItem = { item_code: string item_name: string item_type: string } export type ExamResult = ExamItem & { result_text: string result_structured?: Record is_key?: boolean is_abnormal?: boolean context_written?: boolean already_ordered?: boolean } type ExamListResponse = { items: ExamItem[] } type ExamKind = 'physical-exams' | 'auxiliary-exams' export function fetchPhysicalExamItems(sessionId: number) { return fetchExamItems(sessionId, 'physical-exams') } export function fetchAuxiliaryExamItems(sessionId: number) { return fetchExamItems(sessionId, 'auxiliary-exams') } export function orderPhysicalExamResult(sessionId: number, itemCode: string) { return orderExamResult(sessionId, 'physical-exams', itemCode) } export function orderAuxiliaryExamResult(sessionId: number, itemCode: string) { return orderExamResult(sessionId, 'auxiliary-exams', itemCode) } function assertSessionId(sessionId: number) { if (!Number.isInteger(sessionId) || sessionId <= 0) { throw new Error('未找到当前会话,请先生成模拟场景') } } async function fetchExamItems(sessionId: number, kind: ExamKind) { assertSessionId(sessionId) const response = await fetch(`${FASTAPI_BASE_URL}/sessions/${sessionId}/${kind}`, { method: 'GET', headers: authHeaders() }) if (!response.ok) { throw new Error(await readError(response)) } const result = (await response.json()) as ApiEnvelope if (result.code !== 'OK' || !Array.isArray(result.data?.items)) { throw new Error(result.message || '检查列表加载失败') } return result.data.items } async function orderExamResult(sessionId: number, kind: ExamKind, itemCode: string) { assertSessionId(sessionId) const response = await fetch(`${FASTAPI_BASE_URL}/sessions/${sessionId}/${kind}/${encodeURIComponent(itemCode)}`, { method: 'POST', headers: authHeaders() }) if (!response.ok) { throw new Error(await readError(response)) } const result = (await response.json()) as ApiEnvelope if (result.code !== 'OK' || !result.data?.item_code) { throw new Error(result.message || '检查结果获取失败') } return result.data }