- ai-chat service: sendAiMessage 新增 patientId 可选参数 - messages 页面: 从 authStore 获取 currentPatient.id 传入 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
48 lines
1.1 KiB
TypeScript
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 */ }
|
|
}
|