78 lines
2.2 KiB
TypeScript
Raw Normal View History

Dench workspace: file manager, relation resolution, and chat refactor 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>
2026-02-11 19:22:53 -08:00
import { cpSync, existsSync, statSync } from "node:fs";
import { dirname, basename, extname } from "node:path";
import { safeResolvePath, safeResolveNewPath } from "@/lib/workspace";
Dench workspace: file manager, relation resolution, and chat refactor 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>
2026-02-11 19:22:53 -08:00
export const dynamic = "force-dynamic";
export const runtime = "nodejs";
/**
* POST /api/workspace/copy
* Body: { path: string, destinationPath?: string }
*
* Duplicates a file or folder. If no destinationPath is provided,
* creates a copy next to the original with " copy" appended.
*/
export async function POST(req: Request) {
let body: { path?: string; destinationPath?: string };
try {
body = await req.json();
} catch {
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
}
const { path: relPath, destinationPath } = body;
if (!relPath || typeof relPath !== "string") {
return Response.json(
{ error: "Missing 'path' field" },
{ status: 400 },
);
}
const srcAbs = safeResolvePath(relPath);
if (!srcAbs) {
return Response.json(
{ error: "Source not found or path traversal rejected" },
{ status: 404 },
);
}
let destRelPath: string;
if (destinationPath && typeof destinationPath === "string") {
destRelPath = destinationPath;
} else {
// Auto-generate "name copy.ext" or "name copy" for folders
const name = basename(relPath);
const dir = dirname(relPath);
const ext = extname(name);
const stem = ext ? name.slice(0, -ext.length) : name;
const copyName = ext ? `${stem} copy${ext}` : `${stem} copy`;
destRelPath = dir === "." ? copyName : `${dir}/${copyName}`;
}
const destAbs = safeResolveNewPath(destRelPath);
if (!destAbs) {
return Response.json(
{ error: "Invalid destination path" },
{ status: 400 },
);
}
if (existsSync(destAbs)) {
return Response.json(
{ error: "Destination already exists" },
{ status: 409 },
);
}
try {
const isDir = statSync(srcAbs).isDirectory();
cpSync(srcAbs, destAbs, { recursive: isDir });
return Response.json({ ok: true, sourcePath: relPath, newPath: destRelPath });
} catch (err) {
return Response.json(
{ error: err instanceof Error ? err.message : "Copy failed" },
{ status: 500 },
);
}
}