fix(coding): close PI-100 review findings

This commit is contained in:
2026-08-23 20:55:49 +08:00
parent 22b4a9f9c4
commit 52b2467d5d
16 changed files with 679 additions and 122 deletions

View File

@@ -1,5 +1,7 @@
import type { IncomingMessage, ServerResponse } from 'node:http';
import type { ProjectType } from '../../../shared/project-config';
import type { CodingProjectConfigSnapshot } from '../../coding-projects/project-service';
import type { CodingProject } from '../../coding-projects/project-store';
import type { HostApiContext } from '../context';
import { parseJsonBody, sendJson, sendNoContent } from '../route-utils';
import { sendCodingRouteError } from './coding-route-errors';
@@ -9,6 +11,19 @@ function isProjectRoute(pathname: string): boolean {
|| pathname.startsWith('/api/coding/projects/');
}
function publicProject(project: CodingProject | null): Omit<CodingProject, 'path'> | null {
if (!project) return null;
const { path: _path, ...safe } = project;
return safe;
}
function publicProjectSnapshot(snapshot: CodingProjectConfigSnapshot) {
return {
...snapshot,
project: publicProject(snapshot.project),
};
}
export async function handleCodingProjectRoutes(
req: IncomingMessage,
res: ServerResponse,
@@ -22,7 +37,7 @@ export async function handleCodingProjectRoutes(
sendJson(res, 503, {
success: false,
code: 'CODING_CORE_UNAVAILABLE',
error: 'Coding services are unavailable',
error: '本地编程服务暂时不可用。',
});
return true;
}
@@ -33,12 +48,15 @@ export async function handleCodingProjectRoutes(
projects.listProjects(),
projects.getActiveProject(),
]);
sendJson(res, 200, { projects: items, activeProjectId: activeProject?.id ?? null });
sendJson(res, 200, {
projects: items.map((project) => publicProject(project)),
activeProjectId: activeProject?.id ?? null,
});
return true;
}
if (url.pathname === '/api/coding/projects/open' && req.method === 'POST') {
const body = await parseJsonBody<{ projectPath?: string }>(req);
sendJson(res, 200, { project: await projects.openProject(body.projectPath ?? '') });
sendJson(res, 200, { project: publicProject(await projects.openProject(body.projectPath ?? '')) });
return true;
}
if (url.pathname === '/api/coding/projects/create' && req.method === 'POST') {
@@ -48,7 +66,7 @@ export async function handleCodingProjectRoutes(
projectName?: string;
projectType?: ProjectType;
}>(req);
sendJson(res, 201, { snapshot: await projects.createProject(body) });
sendJson(res, 201, { snapshot: publicProjectSnapshot(await projects.createProject(body)) });
return true;
}
if (url.pathname === '/api/coding/projects/remove' && req.method === 'POST') {
@@ -58,24 +76,26 @@ export async function handleCodingProjectRoutes(
return true;
}
if (url.pathname === '/api/coding/projects/active' && req.method === 'GET') {
sendJson(res, 200, { project: await projects.getActiveProject() });
sendJson(res, 200, { project: publicProject(await projects.getActiveProject()) });
return true;
}
if (url.pathname === '/api/coding/projects/active' && req.method === 'POST') {
const body = await parseJsonBody<{ projectId?: string }>(req);
sendJson(res, 200, { project: await projects.setActiveProject(body.projectId ?? '') });
sendJson(res, 200, { project: publicProject(await projects.setActiveProject(body.projectId ?? '')) });
return true;
}
if (url.pathname === '/api/coding/projects/config' && req.method === 'GET') {
sendJson(res, 200, {
snapshot: await projects.getConfig(url.searchParams.get('projectId')?.trim() || undefined),
snapshot: publicProjectSnapshot(
await projects.getConfig(url.searchParams.get('projectId')?.trim() || undefined),
),
});
return true;
}
if (url.pathname === '/api/coding/projects/config' && req.method === 'PUT') {
const body = await parseJsonBody<{ projectId?: string; config?: unknown }>(req);
sendJson(res, 200, {
snapshot: await projects.saveConfig(body.projectId ?? '', body.config),
snapshot: publicProjectSnapshot(await projects.saveConfig(body.projectId ?? '', body.config)),
});
return true;
}