import { api } from './request'; import { secureGet, secureSet } from '@/utils/secure-storage'; export interface AiChatMessage { id: string; role: 'user' | 'assistant'; content: string; created_at: string; } export interface AiChatResponse { reply: string; message_id: string; display_hints?: DisplayHint[]; } export type DisplayHint = | { type: 'vital_card'; indicator_type: string; values: [string, number][]; unit: string } | { type: 'lab_report_card'; report_date: string; abnormal_count: number } | { type: 'action_confirm'; action_type: string; summary: string; confirm_payload: unknown } | { type: 'risk_alert'; level: string; message: string } | { type: 'trend_chart'; metrics: string[]; period: string; summary: string } | { type: 'insight_card'; title: string; severity: string; items: string[] } | { type: 'patient_profile'; chronic_conditions: string[]; medication_count: number } | { type: 'text' }; /** 发送消息给 AI 客服 */ export async function sendAiMessage( message: string, history?: AiChatMessage[], patientId?: string, sessionId?: string, ): Promise { const body: Record = { message, history: history?.slice(-10), }; if (patientId) { body.patient_id = patientId; } if (sessionId) { body.session_id = sessionId; } const resp = await api.post('/ai/chat', body); return resp; } // === 会话 API === export interface AiChatSession { id: string; title: string | null; patient_id: string | null; status: string; created_at: string; updated_at: string; } export async function createSession(patientId?: string, title?: string): Promise { const body: Record = {}; if (patientId) body.patient_id = patientId; if (title) body.title = title; return api.post('/ai/chat/sessions', body); } export async function listSessions(): Promise { return api.get('/ai/chat/sessions'); } export async function renameSession(sessionId: string, title: string): Promise { await api.put(`/ai/chat/sessions/${sessionId}/rename`, { title }); } export async function closeSession(sessionId: string): Promise { await api.post(`/ai/chat/sessions/${sessionId}/close`, {}); } /** 获取聊天历史(本地加密缓存) */ export function getLocalHistory(): AiChatMessage[] { try { const raw = secureGet('ai_chat_history'); return raw ? JSON.parse(raw) : []; } catch (err) { console.warn('[ai-chat] 读取本地聊天历史失败:', err); return []; } } /** 保存聊天历史到本地(加密存储) */ export function saveLocalHistory(messages: AiChatMessage[]): void { try { secureSet('ai_chat_history', JSON.stringify(messages.slice(-100))); } catch (err) { console.warn('[ai-chat] 保存本地聊天历史失败:', err); } }