feat(mp): Day 10 — 小程序会话 API 封装

- sendAiMessage 支持 sessionId 参数
- 新增 createSession / listSessions / renameSession / closeSession
- AiChatSession 接口定义
This commit is contained in:
iven
2026-05-19 11:45:22 +08:00
parent 8e5bc97f93
commit 975928233f

View File

@@ -28,6 +28,7 @@ export async function sendAiMessage(
message: string,
history?: AiChatMessage[],
patientId?: string,
sessionId?: string,
): Promise<AiChatResponse> {
const body: Record<string, unknown> = {
message,
@@ -36,10 +37,43 @@ export async function sendAiMessage(
if (patientId) {
body.patient_id = patientId;
}
if (sessionId) {
body.session_id = sessionId;
}
const resp = await api.post<AiChatResponse>('/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<AiChatSession> {
const body: Record<string, unknown> = {};
if (patientId) body.patient_id = patientId;
if (title) body.title = title;
return api.post<AiChatSession>('/ai/chat/sessions', body);
}
export async function listSessions(): Promise<AiChatSession[]> {
return api.get<AiChatSession[]>('/ai/chat/sessions');
}
export async function renameSession(sessionId: string, title: string): Promise<void> {
await api.put(`/ai/chat/sessions/${sessionId}/rename`, { title });
}
export async function closeSession(sessionId: string): Promise<void> {
await api.post(`/ai/chat/sessions/${sessionId}/close`, {});
}
/** 获取聊天历史(本地缓存) */
export function getLocalHistory(): AiChatMessage[] {
try {