File Manager & Filesystem Operations: - Add FileManagerTree component with drag-and-drop (dnd-kit), inline rename, right-click context menu, and compact sidebar mode - Add context-menu component (open, new file/folder, rename, duplicate, copy, paste, move, delete) rendered via portal - Add InlineRename component with validation and shake-on-error animation - Add useWorkspaceWatcher hook with SSE live-reload and polling fallback - Add API routes: mkdir, rename, copy, move, watch (SSE file-change events), and DELETE on /api/workspace/file with system-file protection - Add safeResolveNewPath and isSystemFile helpers to workspace lib - Replace inline WorkspaceTreeNode in sidebar with shared FileManagerTree (compact mode), add workspace refresh callback Object Relation Resolution: - Resolve relation fields to human-readable display labels server-side (resolveRelationLabels, resolveDisplayField helpers) - Add reverse relation discovery (findReverseRelations) — surfaces incoming links from other objects - Add display_field column migration (idempotent ALTER TABLE) and PATCH /api/workspace/objects/[name]/display-field endpoint - Enrich object API response with relationLabels, reverseRelations, effectiveDisplayField, and related_object_name per field - Add RelationCell, RelationChip, ReverseRelationCell, LinkIcon components to object-table with clickable cross-object navigation - Add relation label rendering to kanban cards - Extract ObjectView component in workspace page with display-field selector dropdown and relation/reverse-relation badge counts Chat Panel Extraction: - Extract chat logic from page.tsx into standalone ChatPanel component with forwardRef/useImperativeHandle for session control - ChatPanel supports file-scoped sessions (filePath param) and context-aware file chat sidebar - Simplify page.tsx to thin orchestrator delegating to ChatPanel - Add filePath filter to GET /api/web-sessions for scoped session lists Dependencies: - Add @dnd-kit/core, @dnd-kit/sortable, @dnd-kit/utilities - Add duckdbExec and parseRelationValue to workspace lib Co-authored-by: Cursor <cursoragent@cursor.com>
122 lines
3.1 KiB
TypeScript
122 lines
3.1 KiB
TypeScript
import { writeFileSync, mkdirSync, rmSync, statSync } from "node:fs";
|
|
import { dirname } from "node:path";
|
|
import { readWorkspaceFile, safeResolvePath, safeResolveNewPath, isSystemFile } from "@/lib/workspace";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
export const runtime = "nodejs";
|
|
|
|
export async function GET(req: Request) {
|
|
const url = new URL(req.url);
|
|
const path = url.searchParams.get("path");
|
|
|
|
if (!path) {
|
|
return Response.json(
|
|
{ error: "Missing 'path' query parameter" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const file = readWorkspaceFile(path);
|
|
if (!file) {
|
|
return Response.json(
|
|
{ error: "File not found or access denied" },
|
|
{ status: 404 },
|
|
);
|
|
}
|
|
|
|
return Response.json(file);
|
|
}
|
|
|
|
/**
|
|
* POST /api/workspace/file
|
|
* Body: { path: string, content: string }
|
|
*
|
|
* Writes a file to the dench workspace. Creates parent directories as needed.
|
|
*/
|
|
export async function POST(req: Request) {
|
|
let body: { path?: string; content?: string };
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
}
|
|
|
|
const { path: relPath, content } = body;
|
|
if (!relPath || typeof relPath !== "string" || typeof content !== "string") {
|
|
return Response.json(
|
|
{ error: "Missing 'path' and 'content' fields" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
// Use safeResolveNewPath (not safeResolvePath) because the file may not exist yet
|
|
const absPath = safeResolveNewPath(relPath);
|
|
if (!absPath) {
|
|
return Response.json(
|
|
{ error: "Invalid path or path traversal rejected" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
try {
|
|
mkdirSync(dirname(absPath), { recursive: true });
|
|
writeFileSync(absPath, content, "utf-8");
|
|
return Response.json({ ok: true, path: relPath });
|
|
} catch (err) {
|
|
return Response.json(
|
|
{ error: err instanceof Error ? err.message : "Write failed" },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/workspace/file
|
|
* Body: { path: string }
|
|
*
|
|
* Deletes a file or folder from the dench workspace.
|
|
* System files (.object.yaml, workspace.duckdb, etc.) are protected.
|
|
*/
|
|
export async function DELETE(req: Request) {
|
|
let body: { path?: string };
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
}
|
|
|
|
const { path: relPath } = body;
|
|
if (!relPath || typeof relPath !== "string") {
|
|
return Response.json(
|
|
{ error: "Missing 'path' field" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
if (isSystemFile(relPath)) {
|
|
return Response.json(
|
|
{ error: "Cannot delete system file" },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
|
|
const absPath = safeResolvePath(relPath);
|
|
if (!absPath) {
|
|
return Response.json(
|
|
{ error: "File not found or path traversal rejected" },
|
|
{ status: 404 },
|
|
);
|
|
}
|
|
|
|
try {
|
|
const stat = statSync(absPath);
|
|
rmSync(absPath, { recursive: stat.isDirectory() });
|
|
return Response.json({ ok: true, path: relPath });
|
|
} catch (err) {
|
|
return Response.json(
|
|
{ error: err instanceof Error ? err.message : "Delete failed" },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|