feat: prepare Zhinian desktop pilot
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { IncomingMessage, ServerResponse } from 'node:http';
|
||||
import crypto from 'node:crypto';
|
||||
import { basename, extname, join } from 'node:path';
|
||||
import { mkdir, readFile, stat, writeFile, copyFile } from 'node:fs/promises';
|
||||
import { mkdir, readFile, stat, writeFile, copyFile, rm } from 'node:fs/promises';
|
||||
import type { HostApiContext } from '../context';
|
||||
import { parseJsonBody, sendJson } from '../route-utils';
|
||||
import { getDataDir } from '../../utils/paths';
|
||||
@@ -225,6 +225,27 @@ export async function buildKnowledgeContext(params: {
|
||||
};
|
||||
}
|
||||
|
||||
async function deleteKnowledgeDocument(params: {
|
||||
workspaceId?: string;
|
||||
documentId: string;
|
||||
}): Promise<{ deleted: KnowledgeDocument | null }> {
|
||||
const workspaceId = sanitizeWorkspaceId(params.workspaceId);
|
||||
const documentId = params.documentId.trim();
|
||||
if (!documentId) return { deleted: null };
|
||||
|
||||
const registry = await readRegistry(workspaceId);
|
||||
const target = registry.find((doc) => doc.id === documentId);
|
||||
if (!target) return { deleted: null };
|
||||
|
||||
await Promise.all([
|
||||
target.storedPath ? rm(target.storedPath, { force: true }).catch(() => undefined) : Promise.resolve(),
|
||||
target.textPath ? rm(target.textPath, { force: true }).catch(() => undefined) : Promise.resolve(),
|
||||
]);
|
||||
await writeRegistry(workspaceId, registry.filter((doc) => doc.id !== documentId));
|
||||
|
||||
return { deleted: target };
|
||||
}
|
||||
|
||||
export async function handleKnowledgeRoutes(
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
@@ -238,6 +259,22 @@ export async function handleKnowledgeRoutes(
|
||||
return true;
|
||||
}
|
||||
|
||||
if (url.pathname.startsWith('/api/knowledge/files/') && req.method === 'DELETE') {
|
||||
try {
|
||||
const documentId = decodeURIComponent(url.pathname.slice('/api/knowledge/files/'.length));
|
||||
const workspaceId = sanitizeWorkspaceId(url.searchParams.get('workspaceId') ?? undefined);
|
||||
const result = await deleteKnowledgeDocument({ workspaceId, documentId });
|
||||
if (!result.deleted) {
|
||||
sendJson(res, 404, { success: false, error: 'Knowledge document not found' });
|
||||
return true;
|
||||
}
|
||||
sendJson(res, 200, { success: true, document: result.deleted });
|
||||
} catch (error) {
|
||||
sendJson(res, 500, { success: false, error: String(error) });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/knowledge/import-paths' && req.method === 'POST') {
|
||||
try {
|
||||
const body = await parseJsonBody<{ workspaceId?: string; filePaths?: string[] }>(req);
|
||||
|
||||
Reference in New Issue
Block a user