Git Komut Rehberi & Terminal Asistanı
Sıfırdan Yeni Bir Depo Başlatın:
Mevcut Bir Git Deposu veya Klasörü Gönderin:
Hataları Geri Alma, İptal & Temizlik:
Son commit'i geri al (yapılan değişiklikleri korur):
git reset --soft HEAD~1
Yapılan yerel değişiklikleri iptal et (çalışma ağacını temizler):
git restore .
Takip edilmeyen gereksiz dosyaları sil:
git clean -fd
Değişiklikleri geçici sakla ve geri getir (stash):
git stash git stash pop
Dal (Branch) ve Sürüm Etiketleri:
Yeni dal aç ve geçiş yap:
git checkout -b yeni-ozellik git push -u origin yeni-ozellik
Sürüm etiketi (tag) oluştur ve gönder:
git tag -a v1.0.0 -m "Sürüm 1.0.0" git push origin --tags
Revert "AI Scene Bridge (MCP)"
This commit is contained in:
@@ -1,162 +0,0 @@
|
||||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
import { NextResponse } from "next/server";
|
||||
import type { WorkplaneShape } from "@/types/sketchforge";
|
||||
|
||||
export const revalidate = false;
|
||||
|
||||
// Scene bridge: stages MCP-pushed commands on disk for the client poller (Phase 2)
|
||||
// to apply via the editor's existing updateProjectShapes() path. The Node side never
|
||||
// touches IndexedDB/localStorage — see CLAUDE.md. This route only writes/reads a file.
|
||||
const PENDING_SCENES_DIR = path.join(process.cwd(), ".codex", "pending-scenes");
|
||||
const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
|
||||
const MAX_SCENE_BYTES = 16 * 1024 * 1024;
|
||||
const SUPPORTED_OPS = new Set(["upsertScene"]);
|
||||
|
||||
type SceneOp = "upsertScene";
|
||||
|
||||
type StagedSceneCommand = {
|
||||
stagingRevision: number;
|
||||
op: SceneOp;
|
||||
projectId: string;
|
||||
name?: string;
|
||||
shapes: WorkplaneShape[];
|
||||
updatedAt: number;
|
||||
};
|
||||
|
||||
// Copied from the sibling api/project-thumbnail route — keep the bridge in lockstep
|
||||
// with the app's existing same-origin policy rather than inventing a new one.
|
||||
function safeProjectId(projectId: string) {
|
||||
const clean = projectId.replace(/[^a-zA-Z0-9_-]/g, "");
|
||||
return clean || null;
|
||||
}
|
||||
|
||||
function pendingScenePath(projectId: string) {
|
||||
const safeId = safeProjectId(projectId);
|
||||
if (!safeId) {
|
||||
return null;
|
||||
}
|
||||
return path.join(PENDING_SCENES_DIR, `${safeId}.json`);
|
||||
}
|
||||
|
||||
function isLocalSameOriginRequest(request: Request) {
|
||||
const requestUrl = new URL(request.url);
|
||||
if (!LOCAL_HOSTS.has(requestUrl.hostname)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const origin = request.headers.get("origin");
|
||||
if (origin) {
|
||||
try {
|
||||
const originUrl = new URL(origin);
|
||||
if (!LOCAL_HOSTS.has(originUrl.hostname) || originUrl.port !== requestUrl.port || originUrl.protocol !== requestUrl.protocol) {
|
||||
return false;
|
||||
}
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const fetchSite = request.headers.get("sec-fetch-site");
|
||||
return !fetchSite || fetchSite === "same-origin" || fetchSite === "same-site" || fetchSite === "none";
|
||||
}
|
||||
|
||||
async function readStagedCommand(filePath: string): Promise<StagedSceneCommand | null> {
|
||||
try {
|
||||
const raw = await fs.readFile(filePath, "utf8");
|
||||
return JSON.parse(raw) as StagedSceneCommand;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// The bridge's OWN counter — separate from the app's project revision. Monotonic per
|
||||
// project and survives a server restart by stepping past whatever the staged file holds.
|
||||
function nextStagingRevision(previous: StagedSceneCommand | null) {
|
||||
const prior = typeof previous?.stagingRevision === "number" ? previous.stagingRevision : 0;
|
||||
return Math.max(Date.now(), prior + 1);
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
if (!isLocalSameOriginRequest(request)) {
|
||||
return NextResponse.json({ error: "Scene bridge is only available from this localhost app" }, { status: 403 });
|
||||
}
|
||||
|
||||
const params = new URL(request.url).searchParams;
|
||||
const filePath = pendingScenePath(params.get("projectId") ?? "");
|
||||
if (!filePath) {
|
||||
return NextResponse.json({ error: "Invalid project id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const sinceRaw = Number(params.get("sinceStaging"));
|
||||
const sinceStaging = Number.isFinite(sinceRaw) ? sinceRaw : 0;
|
||||
|
||||
const command = await readStagedCommand(filePath);
|
||||
if (!command || command.stagingRevision <= sinceStaging) {
|
||||
return new NextResponse(null, { status: 204 });
|
||||
}
|
||||
|
||||
return NextResponse.json(command, { headers: { "Cache-Control": "no-store" } });
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
if (!isLocalSameOriginRequest(request)) {
|
||||
return NextResponse.json({ error: "Scene bridge is only available from this localhost app" }, { status: 403 });
|
||||
}
|
||||
|
||||
const contentLength = Number(request.headers.get("content-length"));
|
||||
if (Number.isFinite(contentLength) && contentLength > MAX_SCENE_BYTES) {
|
||||
return NextResponse.json({ error: "Scene is too large" }, { status: 413 });
|
||||
}
|
||||
|
||||
const rawBody = await request.text();
|
||||
if (Buffer.byteLength(rawBody, "utf8") > MAX_SCENE_BYTES) {
|
||||
return NextResponse.json({ error: "Scene is too large" }, { status: 413 });
|
||||
}
|
||||
|
||||
let body: { projectId?: unknown; shapes?: unknown; op?: unknown; name?: unknown };
|
||||
try {
|
||||
body = JSON.parse(rawBody) as typeof body;
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Invalid scene request" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof body.projectId !== "string" || !Array.isArray(body.shapes)) {
|
||||
return NextResponse.json({ error: "Invalid scene request" }, { status: 400 });
|
||||
}
|
||||
|
||||
const filePath = pendingScenePath(body.projectId);
|
||||
if (!filePath) {
|
||||
return NextResponse.json({ error: "Invalid project id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const op = body.op === undefined ? "upsertScene" : body.op;
|
||||
if (typeof op !== "string" || !SUPPORTED_OPS.has(op)) {
|
||||
return NextResponse.json({ error: `Unsupported op: ${String(body.op)}` }, { status: 400 });
|
||||
}
|
||||
|
||||
if (body.name !== undefined && typeof body.name !== "string") {
|
||||
return NextResponse.json({ error: "Invalid scene name" }, { status: 400 });
|
||||
}
|
||||
|
||||
const previous = await readStagedCommand(filePath);
|
||||
const command: StagedSceneCommand = {
|
||||
stagingRevision: nextStagingRevision(previous),
|
||||
op: op as SceneOp,
|
||||
projectId: body.projectId,
|
||||
shapes: body.shapes as WorkplaneShape[],
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
if (typeof body.name === "string") {
|
||||
command.name = body.name;
|
||||
}
|
||||
|
||||
await fs.mkdir(PENDING_SCENES_DIR, { recursive: true });
|
||||
await fs.writeFile(filePath, JSON.stringify(command, null, 2), "utf8");
|
||||
|
||||
return NextResponse.json({ stagingRevision: command.stagingRevision });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: error instanceof Error ? error.message : "Could not stage scene" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import { SketchForgeEditor, importedShapeFromStl } from "@/components/SketchForg
|
||||
import { createLocalId } from "@/lib/localIds";
|
||||
import { importExtensionSupported } from "@/lib/stlImport";
|
||||
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings } from "@/lib/workplaneSettings";
|
||||
import { createPollingSceneTransport, resolveSceneBridgeTarget, type StagedSceneCommand } from "@/lib/sceneBridge";
|
||||
import type { GridSize, WorkplaneShape, WorkplaneWorkspaceSettings } from "@/types/sketchforge";
|
||||
|
||||
type AppView = "dashboard" | "editor";
|
||||
@@ -239,62 +238,6 @@ function projectNameFromFileName(fileName: string) {
|
||||
return fileName.replace(/\.[^.]+$/, "").trim() || "Imported STL design";
|
||||
}
|
||||
|
||||
const SCENE_BRIDGE_POLL_MS = 1000;
|
||||
|
||||
// Scene bridge poller. Lives in Home (not a components/ module) because applying a
|
||||
// staged command needs the project lifecycle + updateProjectShapes that live here.
|
||||
// V2 will swap createPollingSceneTransport for a WebSocket transport without touching
|
||||
// this hook's apply path. Defers while a pointer is down so an incoming push never
|
||||
// fights an in-progress drag.
|
||||
function useSceneBridge({ ready, apply }: { ready: boolean; apply: (command: StagedSceneCommand) => void }) {
|
||||
const applyRef = useRef(apply);
|
||||
applyRef.current = apply;
|
||||
const lastAppliedRef = useRef(0);
|
||||
const pointerDownRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!ready || typeof window === "undefined") return;
|
||||
const envEnabled = process.env.NEXT_PUBLIC_SCENE_BRIDGE === "true";
|
||||
const { enabled, projectId } = resolveSceneBridgeTarget(window.location.search, envEnabled);
|
||||
if (!enabled || !projectId) return;
|
||||
|
||||
const transport = createPollingSceneTransport(projectId);
|
||||
let active = true;
|
||||
|
||||
const onPointerDown = () => {
|
||||
pointerDownRef.current = true;
|
||||
};
|
||||
const onPointerUp = () => {
|
||||
pointerDownRef.current = false;
|
||||
};
|
||||
window.addEventListener("pointerdown", onPointerDown, true);
|
||||
window.addEventListener("pointerup", onPointerUp, true);
|
||||
window.addEventListener("pointercancel", onPointerUp, true);
|
||||
|
||||
const tick = async () => {
|
||||
if (!active || pointerDownRef.current) return;
|
||||
try {
|
||||
const command = await transport.poll(lastAppliedRef.current);
|
||||
if (!active || !command || command.stagingRevision <= lastAppliedRef.current) return;
|
||||
lastAppliedRef.current = command.stagingRevision;
|
||||
applyRef.current(command);
|
||||
} catch {
|
||||
// Transient (server reload, file mid-write). The command stays staged; retry next tick.
|
||||
}
|
||||
};
|
||||
|
||||
void tick();
|
||||
const interval = window.setInterval(() => void tick(), SCENE_BRIDGE_POLL_MS);
|
||||
return () => {
|
||||
active = false;
|
||||
window.clearInterval(interval);
|
||||
window.removeEventListener("pointerdown", onPointerDown, true);
|
||||
window.removeEventListener("pointerup", onPointerUp, true);
|
||||
window.removeEventListener("pointercancel", onPointerUp, true);
|
||||
};
|
||||
}, [ready]);
|
||||
}
|
||||
|
||||
export default function Home() {
|
||||
const [mounted, setMounted] = useState(false);
|
||||
const [view, setView] = useState<AppView>("dashboard");
|
||||
@@ -555,37 +498,6 @@ export default function Home() {
|
||||
);
|
||||
}, []);
|
||||
|
||||
const applyBridgeCommand = useCallback(
|
||||
(command: StagedSceneCommand) => {
|
||||
if (command.op !== "upsertScene") return;
|
||||
const projectId = command.projectId;
|
||||
const name = command.name?.trim();
|
||||
// Create the localStorage metadata entry if the id is unknown — the editor needs
|
||||
// BOTH a project entry and a shapes record to open. Never overwrite an existing name.
|
||||
setProjects((current) => {
|
||||
if (current.some((project) => project.id === projectId)) return current;
|
||||
const created: DashboardProject = { ...newProject(name || "AI scene", current.length), id: projectId };
|
||||
return [created, ...current];
|
||||
});
|
||||
// Existing save path: writes IndexedDB + localStorage + a winning project revision.
|
||||
// The editor's rehydration effect (keyed on initialShapes/projectRevision) re-renders.
|
||||
updateProjectShapes({ projectId, shapes: command.shapes });
|
||||
setActiveProjectId(projectId);
|
||||
setEditorStarted(true);
|
||||
setView("editor");
|
||||
if (typeof window !== "undefined") {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
params.set("editor", "1");
|
||||
params.set("project", projectId);
|
||||
if (!params.has("bridge")) params.set("bridge", "1");
|
||||
window.history.replaceState(null, "", `/?${params.toString()}`);
|
||||
}
|
||||
},
|
||||
[updateProjectShapes],
|
||||
);
|
||||
|
||||
useSceneBridge({ ready: mounted, apply: applyBridgeCommand });
|
||||
|
||||
const createAndOpenProject = (name?: string) => {
|
||||
const project = newProject(name ?? `Untitled design ${projects.length + 1}`, projects.length);
|
||||
setProjectShapesById((current) => ({
|
||||
|
||||
Reference in New Issue
Block a user