笔记本页面
This commit is contained in:
1 parent
d639b24c2d
commit
125e8b271b
10 files changed
+689
-11
No files matched your search
@@ -68,3 +68,13 @@ export function getLangGraphBaseURL(isMock?: boolean) {
|
||||
return "http://localhost:2026/api/langgraph";
|
||||
}
|
||||
}
|
||||
|
||||
/** 轻应用「新建笔记」系列接口的基础地址(与 DeerFlow Gateway 分离)。 */
|
||||
export function getNoteApiBaseURL(): string {
|
||||
if (env.NEXT_PUBLIC_NOTE_API_BASE_URL) {
|
||||
return new URL(env.NEXT_PUBLIC_NOTE_API_BASE_URL, getBaseOrigin())
|
||||
.toString()
|
||||
.replace(/\/+$/, "");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
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[]) : [];
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
import { getNoteApiBaseURL } from "@/core/config";
|
||||
|
||||
const NOTE_AUTH_STORAGE_KEY = "deerflow.note.auth";
|
||||
|
||||
const NOTE_LOGIN_USERNAME = "dhk@qq.com";
|
||||
const NOTE_LOGIN_PASSWORD = "1qaz@WSX";
|
||||
|
||||
export interface NoteAuth {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
}
|
||||
|
||||
function isBrowser(): boolean {
|
||||
return typeof window !== "undefined";
|
||||
}
|
||||
|
||||
function assertNoteApiBaseURL(): string {
|
||||
const baseURL = getNoteApiBaseURL();
|
||||
if (!baseURL) {
|
||||
throw new Error("未配置 VITE_NOTE_API_BASE_URL");
|
||||
}
|
||||
return baseURL;
|
||||
}
|
||||
|
||||
export function getStoredNoteAuth(): NoteAuth | null {
|
||||
if (!isBrowser()) {
|
||||
return null;
|
||||
}
|
||||
const raw = window.localStorage.getItem(NOTE_AUTH_STORAGE_KEY);
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return JSON.parse(raw) as NoteAuth;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setStoredNoteAuth(auth: NoteAuth): void {
|
||||
if (!isBrowser()) {
|
||||
return;
|
||||
}
|
||||
window.localStorage.setItem(NOTE_AUTH_STORAGE_KEY, JSON.stringify(auth));
|
||||
}
|
||||
|
||||
export function clearStoredNoteAuth(): void {
|
||||
if (!isBrowser()) {
|
||||
return;
|
||||
}
|
||||
window.localStorage.removeItem(NOTE_AUTH_STORAGE_KEY);
|
||||
}
|
||||
|
||||
export function getNoteAuthorizationHeader(auth: NoteAuth): string {
|
||||
const token = auth.access_token.trim();
|
||||
const tokenType = auth.token_type?.trim() || "Bearer";
|
||||
return `${tokenType} ${token}`;
|
||||
}
|
||||
|
||||
export async function loginNoteApi(): Promise<NoteAuth> {
|
||||
const formData = new FormData();
|
||||
formData.append("username", NOTE_LOGIN_USERNAME);
|
||||
formData.append("password", NOTE_LOGIN_PASSWORD);
|
||||
formData.append("grant_type", "password");
|
||||
|
||||
const response = await fetch(`${assertNoteApiBaseURL()}/auth/jwt/login`, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`笔记登录失败: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as Partial<NoteAuth>;
|
||||
if (!data.access_token) {
|
||||
throw new Error("笔记登录失败: 未返回 access_token");
|
||||
}
|
||||
|
||||
const auth: NoteAuth = {
|
||||
access_token: data.access_token,
|
||||
token_type: data.token_type?.trim() || "bearer",
|
||||
};
|
||||
setStoredNoteAuth(auth);
|
||||
return auth;
|
||||
}
|
||||
|
||||
export async function ensureNoteAuth(): Promise<NoteAuth> {
|
||||
const existing = getStoredNoteAuth();
|
||||
if (existing?.access_token) {
|
||||
return existing;
|
||||
}
|
||||
return loginNoteApi();
|
||||
}
|
||||
Reference in new issue
Block a user