61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
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<T>(path: string, init?: RequestInit): Promise<T> {
|
|
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<Mindmap> {
|
|
return request<Mindmap>("/api/mindmaps", {
|
|
method: "POST",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function getMindmap(uniqueId: string): Promise<Mindmap> {
|
|
return request<Mindmap>(`/api/mindmaps/${uniqueId}`);
|
|
}
|
|
|
|
export async function sendChatMessage(
|
|
sessionId: string,
|
|
content: string,
|
|
visitorBizId: string = "default_visitor",
|
|
signal?: AbortSignal,
|
|
): Promise<Response> {
|
|
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;
|
|
}
|