/* 초기노선 갈래 **저장 왕복**(계획서 20) 헬퍼 — 서버 답을 화면 코드 그대로 편다. * * 표준입력 JSON `{ jobs: [...] }` 를 받아 일마다 결과 한 줄을 돌려준다. * - `apply` : `/route/initial` 답(+보낸 `criteria` 겹침) → 편집값(`normalizeRouteInitial`) * → [확인] 몸통(`_RouteEdit_Apply` 의 vertices 만들기 · `_Initial.current()` 의 * initial{mode, weights, criteria}) * - `reopen` : `/route/plan` 답 → 다시 연 노드(`flattenServerPlan`) · 현재 초기값(`serverBase`) * · 캐시 초기값과 같은가(`sameBase` — [초기화]가 캐시 노드를 쓸지 서버에 물을지) * * ⚠ 확장자가 `.cjs` 인 까닭은 `helper_b05_cut_merged_loop.cjs` 머리에 적힌 것과 같다. */ const fs = require("fs"); const path = require("path"); const ts = require(path.join(__dirname, "..", "..", "config", "node_modules", "typescript")); const ROOT = path.join(__dirname, "..", ".."); const cache = new Map(); function memoryStorage() { const map = new Map(); return { getItem: (key) => (map.has(key) ? map.get(key) : null), setItem: (key, value) => map.set(key, String(value)), removeItem: (key) => map.delete(key), key: (index) => [...map.keys()][index] ?? null, get length() { return map.size; }, }; } globalThis.window = { localStorage: memoryStorage(), sessionStorage: memoryStorage() }; globalThis.fetch = () => Promise.resolve({ ok: false }); const STUBS = { "@ui/ui_template_palette": { themeColor: (_name, fallback) => fallback } }; function loadTs(file) { const full = path.resolve(file); if (cache.has(full)) return cache.get(full); const js = ts.transpileModule(fs.readFileSync(full, "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, }).outputText; const box = { exports: {} }; cache.set(full, box.exports); const localRequire = (request) => { if (STUBS[request]) return STUBS[request]; if (request.startsWith(".")) return loadTs(path.join(path.dirname(full), `${request}.ts`)); return require(request); }; new Function("exports", "module", "require", js)(box.exports, box, localRequire); cache.set(full, box.exports); return box.exports; } const model = loadTs(path.join(ROOT, "B05_Profile", "B05_Profile_UI_RouteEdit_Initial_Model.ts")); const edits = loadTs(path.join(ROOT, "B05_Profile", "B05_Profile_UI_RouteEdit_Edits.ts")); const { jobs } = JSON.parse(fs.readFileSync(0, "utf8")); const out = jobs.map((job) => { if (job.kind === "apply") { const choice = model.normalizeRouteInitial( job.response, job.logic, job.weights ?? null, job.criteria ?? null, ); const { planned, curveOn, curveRadius } = choice.snapshot; return { choice, // `_RouteEdit_Apply` [확인] 몸통 그대로. body: { vertices: planned.map(([x, y], index) => ({ x, y, curve: curveOn[index] !== false, radius_m: curveRadius[index] ?? null, })), initial: { mode: choice.logic, weights: choice.weights, criteria: choice.criteriaOverrides, }, }, label: model.choiceLabel(choice.logic, choice.weights), metrics: model.metricsLines(choice.metrics), violations: model.violationsLine(choice.metrics), relaxed: model.relaxedLine(choice), criteria: model.criteriaLine(choice.criteria, 0), }; } const flat = edits.flattenServerPlan(job.plan.nodes ?? [], job.plan.curves ?? []); const base = model.serverBase(job.plan.current_initial); return { planned: flat.planned, base, label: model.choiceLabel(base.logic, base.weights), sameAsCache: job.cached ? model.sameBase(job.cached, base) : null, }; }); process.stdout.write(JSON.stringify(out));