import { api } from './request'; export interface AiChatMessage { id: string; role: 'user' | 'assistant'; content: string; created_at: string; } export interface AiChatResponse { reply: string; message_id: string; } /** 发送消息给 AI 客服 */ export async function sendAiMessage( message: string, history?: AiChatMessage[], patientId?: string, ): Promise { const body: Record = { message, history: history?.slice(-10), }; if (patientId) { body.patient_id = patientId; } const resp = await api.post('/ai/chat', body); return resp; } /** 获取聊天历史(本地缓存) */ export function getLocalHistory(): AiChatMessage[] { try { const raw = wx.getStorageSync('ai_chat_history'); return raw ? JSON.parse(raw) : []; } catch { return []; } } /** 保存聊天历史到本地 */ export function saveLocalHistory(messages: AiChatMessage[]): void { try { wx.setStorageSync('ai_chat_history', JSON.stringify(messages.slice(-100))); } catch { /* ignore */ } }