feat(kb): 批量文件入库(文件列表) + 项目/案件知识库 + owner 作用域隔离
回应三点诉求:一次入一批文件、按文件夹/项目/案件组织、且只有我能查我的库。 隔离(核心):知识库实际分区键 = "owner/name",owner 由网关从 X-User-ID 注入, 客户端只发库名、发不了 owner —— 故任何人都只能查到自己 owner 前缀下的数据。 - gateway: scopedKB(owner/kb) 注入 ingest/search/graph;ingest/search/graph 全部带身份头。 - store: sundynix_kb 注册表(owner+name 唯一 + kind),ListKB/EnsureKB(OnConflict DoNothing)。 项目/案件组织: - gateway: GET /kb/list(owner 隔离列表)、POST /kb/create(folder/project/case/general); 入库时 EnsureKB 自动登记。 - 前端: KbView 顶部知识库下拉 + 新建(项目/案件/文件夹/通用),检索/图谱/入库都绑定所选库。 批量文件: - 前端: 选择文件(multiple) + 选择文件夹(webkitdirectory) + 拖拽一批 → 每文件一个 job, 文件列表实时显示各自状态(排队/解析/向量化/写入/抽取/完成/失败)+ 完成/失败计数。 验证:curl 证隔离 —— wt 入 default→可检索;alice 查同名 default→[] 空;alice 列表不含 wt 案件库。 Preview 证 UI —— 知识库下拉含 案件-2024-001(案件)+default(通用)、owner 隔离徽标、批量/文件夹按钮。 tsc+vite+gateway build 通过;重建 .app 重启窗口。 注:身份目前来自 X-User-ID 头(可信前端),生产应换 JWT 鉴权中间件——隔离机制(owner 前缀)已就位。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -91,11 +91,41 @@ export interface IngestEvent {
|
||||
error?: string;
|
||||
}
|
||||
|
||||
// idHeaders 把身份带进请求头 —— 网关据此把知识库锁进 owner 作用域(隔离)。
|
||||
function idHeaders(id: Identity): Record<string, string> {
|
||||
return { "X-User-ID": id.userId, "X-Session-ID": id.sessionId };
|
||||
}
|
||||
|
||||
export interface KbInfo {
|
||||
name: string;
|
||||
kind: string; // folder / project / case / general
|
||||
}
|
||||
|
||||
// listKb: GET /api/v1/kb/list —— 当前用户的知识库列表(owner 隔离)。
|
||||
export async function listKb(id: Identity): Promise<KbInfo[]> {
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/list`, { headers: idHeaders(id) });
|
||||
const data = (await res.json()) as { kbs?: KbInfo[]; error?: string };
|
||||
if (!res.ok) throw new Error(data.error ?? `list failed: ${res.status}`);
|
||||
return data.kbs ?? [];
|
||||
}
|
||||
|
||||
// createKb: POST /api/v1/kb/create —— 新建知识库(项目/案件/文件夹/通用)。
|
||||
export async function createKb(id: Identity, name: string, kind: string): Promise<KbInfo> {
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/create`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", ...idHeaders(id) },
|
||||
body: JSON.stringify({ name, kind }),
|
||||
});
|
||||
const data = (await res.json()) as { name?: string; kind?: string; error?: string };
|
||||
if (!res.ok || !data.name) throw new Error(data.error ?? `create failed: ${res.status}`);
|
||||
return { name: data.name, kind: data.kind ?? kind };
|
||||
}
|
||||
|
||||
// ingestKb: POST /api/v1/kb/ingest —— 文本入库(异步,返回 job_id)。
|
||||
export async function ingestKb(kb: string, text: string): Promise<string> {
|
||||
export async function ingestKb(id: Identity, kb: string, text: string): Promise<string> {
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/ingest`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: { "Content-Type": "application/json", ...idHeaders(id) },
|
||||
body: JSON.stringify({ kb, text }),
|
||||
});
|
||||
const data = (await res.json()) as { job_id?: string; error?: string };
|
||||
@@ -104,11 +134,11 @@ export async function ingestKb(kb: string, text: string): Promise<string> {
|
||||
}
|
||||
|
||||
// ingestFile: POST /api/v1/kb/ingest_file(multipart)—— 文件入库(异步,返回 job_id)。
|
||||
export async function ingestFile(kb: string, file: File): Promise<string> {
|
||||
export async function ingestFile(id: Identity, kb: string, file: File): Promise<string> {
|
||||
const fd = new FormData();
|
||||
fd.append("kb", kb);
|
||||
fd.append("file", file);
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/ingest_file`, { method: "POST", body: fd });
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/ingest_file`, { method: "POST", headers: idHeaders(id), body: fd });
|
||||
const data = (await res.json()) as { job_id?: string; error?: string };
|
||||
if (!res.ok || !data.job_id) throw new Error(data.error ?? `ingest file failed: ${res.status}`);
|
||||
return data.job_id;
|
||||
@@ -146,18 +176,18 @@ export interface Triple {
|
||||
}
|
||||
|
||||
// graphKb: GET /api/v1/kb/graph —— 取某知识库的图谱三元组(→ mcp-go kb_graph,Neo4j)。
|
||||
export async function graphKb(kb: string): Promise<Triple[]> {
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/graph?kb=${encodeURIComponent(kb)}`);
|
||||
export async function graphKb(id: Identity, kb: string): Promise<Triple[]> {
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/graph?kb=${encodeURIComponent(kb)}`, { headers: idHeaders(id) });
|
||||
const data = (await res.json()) as { triples?: Triple[]; error?: string };
|
||||
if (!res.ok) throw new Error(data.error ?? `graph failed: ${res.status}`);
|
||||
return data.triples ?? [];
|
||||
}
|
||||
|
||||
// searchKb: POST /api/v1/kb/search,检索台查询(→ mcp-go kb_search,带分数)。
|
||||
export async function searchKb(kb: string, q: string, topK = 5): Promise<KbHit[]> {
|
||||
export async function searchKb(id: Identity, kb: string, q: string, topK = 5): Promise<KbHit[]> {
|
||||
const res = await fetch(`${GATEWAY}/api/v1/kb/search`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: { "Content-Type": "application/json", ...idHeaders(id) },
|
||||
body: JSON.stringify({ kb, q, topK }),
|
||||
});
|
||||
const data = (await res.json()) as { hits?: KbHit[]; error?: string };
|
||||
|
||||
Reference in New Issue
Block a user