import type { CreateMindmapPayload, Mindmap } from "../types/mindmap"; const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://127.0.0.1:8000"; async function request(path: string, init?: RequestInit): Promise { const response = await fetch(`${API_BASE_URL}${path}`, { ...init, headers: { "Content-Type": "application/json", ...(init?.headers ?? {}), }, cache: "no-store", }); if (!response.ok) { const message = await response.text(); throw new Error(message || "请求失败"); } return (await response.json()) as T; } export async function createMindmap( payload: CreateMindmapPayload, ): Promise { return request("/api/mindmaps", { method: "POST", body: JSON.stringify(payload), }); } export async function getMindmap(uniqueId: string): Promise { return request(`/api/mindmaps/${uniqueId}`); } export async function sendChatMessage( sessionId: string, content: string, visitorBizId: string = "default_visitor", signal?: AbortSignal, ): Promise { const response = await fetch(`${API_BASE_URL}/api/chat`, { method: "POST", headers: { "Content-Type": "application/json" }, signal, body: JSON.stringify({ session_id: sessionId, content, visitor_biz_id: visitorBizId, }), }); if (!response.ok) { const message = await response.text(); throw new Error(message || "请求失败"); } return response; }