Files
hms/apps/miniprogram/src/services/ai-chat.ts
iven 38b0d91407 feat(mp): AI 聊天传递 patient_id 支持体征数据查询 Tool Call
- ai-chat service: sendAiMessage 新增 patientId 可选参数
- messages 页面: 从 authStore 获取 currentPatient.id 传入

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-18 09:35:18 +08:00

48 lines
1.1 KiB
TypeScript

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<AiChatResponse> {
const body: Record<string, unknown> = {
message,
history: history?.slice(-10),
};
if (patientId) {
body.patient_id = patientId;
}
const resp = await api.post<AiChatResponse>('/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 */ }
}