Files
deerflow-code/frontend-web/src/core/note/api.ts
T
2026-05-21 19:40:54 +08:00

104 lines
2.4 KiB
TypeScript

import { getNoteApiBaseURL } from "@/core/config";
import {
ensureNoteAuth,
getNoteAuthorizationHeader,
getStoredNoteAuth,
} from "./auth";
export type { NoteAuth } from "./auth";
export interface SearchSpace {
created_at: string;
id: number;
name: string;
description: string;
user_id: string;
citations_enabled: boolean;
qna_custom_instructions: string;
summary: string | null;
avatar_url: string | null;
sources_limit: number;
sources_used: number;
is_shared: boolean;
is_owner: boolean;
owner_email: string | null;
member_count?: number;
source_count?: number;
membership_role?: string;
}
export interface ListSearchSpacesQuery {
owned_only?: boolean;
limit?: number;
order_by?: string;
}
export interface CreateSearchSpaceInput {
name: string;
description?: string;
citations_enabled?: boolean;
}
export async function noteApiFetch(
path: string,
init: RequestInit = {},
): Promise<Response> {
const auth = getStoredNoteAuth() ?? (await ensureNoteAuth());
const headers = new Headers(init.headers);
headers.set("Authorization", getNoteAuthorizationHeader(auth));
return fetch(`${getNoteApiBaseURL()}${path}`, {
...init,
headers,
});
}
export async function createSearchSpace(
input: CreateSearchSpaceInput,
): Promise<SearchSpace> {
await ensureNoteAuth();
const response = await noteApiFetch("/api/v1/searchspaces", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: input.name,
description: input.description ?? "",
citations_enabled: input.citations_enabled ?? true,
}),
});
if (!response.ok) {
throw new Error(`创建笔记失败: ${response.status} ${response.statusText}`);
}
return (await response.json()) as SearchSpace;
}
export async function listSearchSpaces(
query: ListSearchSpacesQuery = {},
): Promise<SearchSpace[]> {
await ensureNoteAuth();
const params = new URLSearchParams({
owned_only: String(query.owned_only ?? true),
limit: String(query.limit ?? 1000),
order_by: query.order_by ?? "created_at",
});
const response = await noteApiFetch(`/api/v1/searchspaces?${params.toString()}`, {
method: "GET",
});
if (!response.ok) {
throw new Error(`获取笔记列表失败: ${response.status} ${response.statusText}`);
}
const data: unknown = await response.json();
return Array.isArray(data) ? (data as SearchSpace[]) : [];
}