← Commit history
Upload chipsmith source (MIT) — Ray can commit on top
6 files changed
+1882
src/cli/list_components.rsadded+42@@ -0,0 +1,42 @@+use crate::cli::{eval, DEFAULT_PORT};+use anyhow::Result;+use clap::Parser;++#[derive(Parser)]+pub struct Args {+ /// Print as JSON instead of indented tree.+ #[arg(long)]+ pub json: bool,++ #[arg(long, default_value_t = 5)]+ pub timeout_secs: u64,++ #[arg(long, default_value_t = DEFAULT_PORT)]+ pub port: u16,+}++pub fn run(args: Args) -> Result<()> {+ let code = "return window.adomStep ? window.adomStep.listComponents() : { error: 'adomStep not loaded yet' };";+ let result = eval::eval_snippet(code, args.timeout_secs, args.port, true)?;++ if args.json {+ println!("{}", serde_json::to_string_pretty(&result)?);+ return Ok(());+ }++ if let Some(err) = result.get("error").and_then(|v| v.as_str()) {+ eprintln!("ERROR: {err}");+ return Err(anyhow::anyhow!("{err}"));+ }+ let empty = vec![];+ let nodes = result.as_array().unwrap_or(&empty);+ for n in nodes {+ let depth = n.get("depth").and_then(|v| v.as_u64()).unwrap_or(0) as usize;+ let name = n.get("name").and_then(|v| v.as_str()).unwrap_or("?");+ let visible = n.get("visible").and_then(|v| v.as_bool()).unwrap_or(true);+ let mark = if visible { "●" } else { "○" };+ let pad = " ".repeat(depth);+ println!("{}{} {}", pad, mark, name);+ }+ Ok(())+}
src/cli/open.rsadded+100@@ -0,0 +1,100 @@+use crate::cli::{err, hint, ok, DEFAULT_PORT};+use anyhow::{anyhow, Result};+use clap::Parser;+use std::process::Command;++const WEBVIEW_PANEL_TYPE: &str = "adom/a1b2c3d4-0031-4000-a000-000000000031";++#[derive(Parser)]+pub struct Args {+ #[arg(long, default_value_t = DEFAULT_PORT)]+ pub port: u16,++ #[arg(long, default_value = "STEP Viewer")]+ pub tab_name: String,++ #[arg(long, default_value = "mdi:cube-outline")]+ pub display_icon: String,+}++pub fn run(args: Args) -> Result<()> {+ open_webview_tab(args.port, &args.tab_name, &args.display_icon)+}++pub fn open_webview_tab(port: u16, tab_name: &str, display_icon: &str) -> Result<()> {+ let proxy_uri = std::env::var("VSCODE_PROXY_URI").ok();+ let base_url = match &proxy_uri {+ Some(t) => t.replace("{{port}}", &port.to_string()),+ None => format!("http://127.0.0.1:{}/", port),+ };+ let sep = if base_url.contains('?') { '&' } else { '?' };+ let cb = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|d| d.as_millis())+ .unwrap_or(0);+ let webview_url = format!("{}{}_cb={}", base_url, sep, cb);++ let leaf_id = match find_leaf_pane_id() {+ Ok(id) => id,+ Err(e) => {+ err(format!("{e}"));+ hint("Open any web view tab first (or split a new pane), then re-run.");+ return Err(e);+ }+ };+ let initial_state = format!(r#"{{"url":"{}"}}"#, webview_url);++ let status = Command::new("adom-cli")+ .args([+ "hydrogen",+ "workspace",+ "add-tab",+ "--panel-id",+ &leaf_id,+ "--panel-type",+ WEBVIEW_PANEL_TYPE,+ "--display-name",+ tab_name,+ "--display-icon",+ display_icon,+ "--initial-state",+ &initial_state,+ ])+ .status()?;+ if !status.success() {+ return Err(anyhow!(+ "adom-cli workspace add-tab failed (is a tab already named '{}'? pass --tab-name to override)",+ tab_name+ ));+ }+ ok(format!("webview tab '{tab_name}' created"));+ Ok(())+}++/// Find a Web View pane to host the tab. Per memory `feedback_never_cover_vscode`:+/// only place tabs in panes that already host a Web View tab.+fn find_leaf_pane_id() -> Result<String> {+ let output = Command::new("adom-cli")+ .args(["hydrogen", "workspace", "tabs"])+ .output()?;+ let text = String::from_utf8_lossy(&output.stdout);+ let v: serde_json::Value = serde_json::from_str(&text)?;+ let tabs = v+ .get("tabs")+ .and_then(|t| t.as_array())+ .ok_or_else(|| anyhow!("adom-cli workspace tabs returned no tabs array"))?;+ for tab in tabs {+ let ptype = tab+ .get("panelType")+ .and_then(|p| p.as_str())+ .unwrap_or("");+ if ptype == WEBVIEW_PANEL_TYPE {+ if let Some(pid) = tab.get("panelId").and_then(|p| p.as_str()) {+ return Ok(pid.to_string());+ }+ }+ }+ Err(anyhow!(+ "no existing Web View pane found. Open any web view tab first or split a new pane."+ ))+}
src/cli/restart.rsadded+112@@ -0,0 +1,112 @@+use anyhow::Result;+use clap::Args as ClapArgs;++/// Kill the running server, rebuild (if --build), restart it on the+/// same port + chip library, and reload the pup browser window.+/// One command replaces the 5-step restart cycle that kept tripping+/// up the AI: kill → build → start → confirm alive → reload browser.+#[derive(ClapArgs)]+pub struct Args {+ /// Port (default 8872)+ #[arg(long, default_value_t = 8872)]+ port: u16,++ /// Rebuild before restarting (`cargo build --release` + touch mod.rs)+ #[arg(long)]+ build: bool,++ /// Pup session ID to reload after restart+ #[arg(long, default_value = "chipsmith")]+ session: String,++ /// Max seconds to wait for the server to become healthy+ #[arg(long, default_value_t = 30)]+ timeout: u32,+}++pub fn run(args: Args) -> Result<()> {+ let port = args.port;++ // 1. Kill the running server+ eprint!("stopping port {}... ", port);+ let _ = ureq::post(&format!("http://127.0.0.1:{port}/shutdown"))+ .call();+ std::thread::sleep(std::time::Duration::from_secs(1));+ eprintln!("done");++ // 2. Optional rebuild+ if args.build {+ eprint!("building... ");+ // Touch mod.rs to force include_bytes! re-embed of changed assets+ let _ = std::fs::File::open("src/server/mod.rs")+ .and_then(|f| f.set_modified(std::time::SystemTime::now()).map(|_| f));+ let status = std::process::Command::new("cargo")+ .args(["build", "--release"])+ .status()?;+ if !status.success() {+ anyhow::bail!("cargo build failed");+ }+ eprintln!("done");+ }++ // 3. Read the last-known library dir from the server state (saved+ // at /tmp/adom-chipsmith-last-library). Fall back to env var.+ let state_file = std::path::PathBuf::from("/tmp/adom-chipsmith-last-library");+ let lib_dir = if state_file.exists() {+ std::fs::read_to_string(&state_file)?.trim().to_string()+ } else if let Ok(d) = std::env::var("CHIPSMITH_LIBRARY") {+ d+ } else {+ anyhow::bail!("no last-known library dir — pass CHIPSMITH_LIBRARY env or run view-library first");+ };++ // 4. Start the server in background+ eprint!("starting on port {} with {}... ", port, lib_dir);+ let exe = std::env::current_exe()?;+ std::process::Command::new(&exe)+ .args(["view-library", "--no-open", "--port", &port.to_string(), &lib_dir])+ .stdin(std::process::Stdio::null())+ .stdout(std::process::Stdio::null())+ .stderr(std::process::Stdio::null())+ .spawn()?;++ // 5. Wait for healthy+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(args.timeout as u64);+ let url = format!("http://127.0.0.1:{port}/api/paths");+ loop {+ if std::time::Instant::now() > deadline {+ anyhow::bail!("server did not become healthy within {}s", args.timeout);+ }+ std::thread::sleep(std::time::Duration::from_millis(500));+ if ureq::get(&url).call().is_ok() {+ break;+ }+ }+ super::ok(format!("server alive on port {port}"));++ // 6. Reload the pup browser window+ let reload_result = std::process::Command::new("adom-desktop")+ .args(["browser_reload", &format!("{{\"sessionId\":\"{}\"}}", args.session)])+ .output();+ match reload_result {+ Ok(o) if o.status.success() => {+ super::ok(format!("browser reloaded (session: {})", args.session));+ }+ _ => {+ super::hint(format!("browser reload failed — manually reload session '{}'", args.session));+ }+ }++ // 7. Wait for chipsmith JS to init (poll eval)+ let eval_url = format!("http://127.0.0.1:{port}/eval");+ let deadline2 = std::time::Instant::now() + std::time::Duration::from_secs(15);+ loop {+ if std::time::Instant::now() > deadline2 { break; }+ std::thread::sleep(std::time::Duration::from_secs(1));+ // Can't easily check the browser JS from here — the server's+ // /eval endpoint doesn't exist. Just wait a reasonable time.+ }++ super::ok("restart complete — server + browser reloaded");+ Ok(())+}
src/server/cache.rsadded+40@@ -0,0 +1,40 @@+//! On-disk content-hashed cache for converted GLBs.+//! Files land at /tmp/adom-chipsmith-cache/<sha256>.glb plus a sibling+//! <sha256>.materials.json with the materials payload from the service.++use anyhow::{anyhow, Result};+use sha2::{Digest, Sha256};+use std::path::{Path, PathBuf};++pub fn cache_dir() -> PathBuf {+ let base = std::env::var("ADOM_STEP_CACHE")+ .unwrap_or_else(|_| "/tmp/adom-chipsmith-cache".to_string());+ PathBuf::from(base)+}++pub fn ensure_dir() -> Result<PathBuf> {+ let d = cache_dir();+ std::fs::create_dir_all(&d).map_err(|e| anyhow!("create cache dir {}: {e}", d.display()))?;+ Ok(d)+}++pub fn sha256_hex(bytes: &[u8]) -> String {+ let mut h = Sha256::new();+ h.update(bytes);+ hex::encode(h.finalize())+}++pub fn glb_path(hash: &str) -> PathBuf {+ cache_dir().join(format!("{hash}.glb"))+}++pub fn materials_path(hash: &str) -> PathBuf {+ cache_dir().join(format!("{hash}.materials.json"))+}++pub fn read_materials(p: &Path) -> serde_json::Value {+ std::fs::read_to_string(p)+ .ok()+ .and_then(|s| serde_json::from_str(&s).ok())+ .unwrap_or(serde_json::json!([]))+}
src/server/convert.rsadded+274@@ -0,0 +1,274 @@+//! Thin client to service-step2glb. Converts STEP -> GLB, caches by SHA-256.+//!+//! Mirrors the HTTP shape of step2glb's client.rs (Material structure,+//! X-Materials-Json header, X-Meshes / X-Nodes / X-Duration-Ms headers)+//! but inlines it here so adom-chipsmith has zero dependency on the step2glb crate.++use crate::cli::step2glb_service_url;+use crate::server::cache;+use anyhow::{anyhow, Result};+use std::path::PathBuf;+use std::time::Duration;++pub struct ConvertResult {+ pub glb_path: PathBuf,+ pub size_bytes: u64,+ pub meshes: u32,+ pub nodes: u32,+ pub materials: serde_json::Value,+ pub duration_ms: u64,+ pub cache_hit: bool,+}++/// Convert raw STEP bytes via the service. Cache key = SHA-256 of bytes.+/// On hit, returns instantly without hitting the network.+pub fn convert_step_bytes(step: &[u8]) -> Result<ConvertResult> {+ let dir = cache::ensure_dir()?;+ let hash = cache::sha256_hex(step);+ let glb = cache::glb_path(&hash);+ let mat_path = cache::materials_path(&hash);++ // Cache hit+ if glb.exists() {+ let size = std::fs::metadata(&glb).map(|m| m.len()).unwrap_or(0);+ let materials = cache::read_materials(&mat_path);+ return Ok(ConvertResult {+ glb_path: glb,+ size_bytes: size,+ meshes: 0, // not re-introspected on cache hit+ nodes: 0,+ materials,+ duration_ms: 0,+ cache_hit: true,+ });+ }++ // Cache miss — POST to service-step2glb /convert.+ let url = format!("{}/convert", step2glb_service_url());+ let resp = ureq::post(&url)+ .set("Content-Type", "application/step")+ .timeout(Duration::from_secs(180))+ .send_bytes(step)+ .map_err(|e| anyhow!("POST {url}: {e}"))?;++ if resp.status() != 200 {+ let body = resp.into_string().unwrap_or_default();+ return Err(anyhow!("service-step2glb returned HTTP non-200: {body}"));+ }++ let meshes = header_u32(&resp, "X-Meshes");+ let nodes = header_u32(&resp, "X-Nodes");+ let duration_ms = u64::from(header_u32(&resp, "X-Duration-Ms"));+ let mats_json_str = resp.header("X-Materials-Json").unwrap_or("[]").to_string();+ let materials: serde_json::Value =+ serde_json::from_str(&mats_json_str).unwrap_or(serde_json::json!([]));++ // Stream body to disk in cache.+ let mut tmp = dir.join(format!("{hash}.glb.tmp"));+ let mut file = std::fs::File::create(&tmp)+ .map_err(|e| anyhow!("create {}: {e}", tmp.display()))?;+ let mut reader = resp.into_reader();+ let size_bytes = std::io::copy(&mut reader, &mut file)+ .map_err(|e| anyhow!("write GLB: {e}"))?;+ drop(file);+ std::fs::rename(&tmp, &glb).map_err(|e| anyhow!("rename {}: {e}", tmp.display()))?;+ tmp.clear();++ // Write materials sidecar.+ let _ = std::fs::write(&mat_path, mats_json_str);++ Ok(ConvertResult {+ glb_path: glb,+ size_bytes,+ meshes,+ nodes,+ materials,+ duration_ms,+ cache_hit: false,+ })+}++/// Convert from a service-kicad library: GET /convert?library=L&name=N.+pub fn convert_from_library(lib_slash_name: &str) -> Result<ConvertResult> {+ let (lib, name) = lib_slash_name+ .rsplit_once('/')+ .ok_or_else(|| anyhow!("bad path {lib_slash_name:?}; expected <Library>/<Name>"))?;+ let url = format!(+ "{}/convert?library={}&name={}",+ step2glb_service_url(),+ urlencode(lib),+ urlencode(name),+ );+ // Cache key for library entries: hash of the URL path itself.+ let dir = cache::ensure_dir()?;+ let hash = cache::sha256_hex(format!("lib:{lib}/{name}").as_bytes());+ let glb = cache::glb_path(&hash);+ let mat_path = cache::materials_path(&hash);++ if glb.exists() {+ let size = std::fs::metadata(&glb).map(|m| m.len()).unwrap_or(0);+ let materials = cache::read_materials(&mat_path);+ return Ok(ConvertResult {+ glb_path: glb,+ size_bytes: size,+ meshes: 0,+ nodes: 0,+ materials,+ duration_ms: 0,+ cache_hit: true,+ });+ }++ let resp = ureq::get(&url)+ .timeout(Duration::from_secs(180))+ .call()+ .map_err(|e| anyhow!("GET {url}: {e}"))?;++ if resp.status() != 200 {+ let body = resp.into_string().unwrap_or_default();+ return Err(anyhow!(+ "service-step2glb returned HTTP non-200 for library {lib}/{name}: {body}"+ ));+ }++ let meshes = header_u32(&resp, "X-Meshes");+ let nodes = header_u32(&resp, "X-Nodes");+ let duration_ms = u64::from(header_u32(&resp, "X-Duration-Ms"));+ let mats_json_str = resp.header("X-Materials-Json").unwrap_or("[]").to_string();+ let materials: serde_json::Value =+ serde_json::from_str(&mats_json_str).unwrap_or(serde_json::json!([]));++ let tmp = dir.join(format!("{hash}.glb.tmp"));+ let mut file = std::fs::File::create(&tmp)+ .map_err(|e| anyhow!("create {}: {e}", tmp.display()))?;+ let mut reader = resp.into_reader();+ let size_bytes = std::io::copy(&mut reader, &mut file)+ .map_err(|e| anyhow!("write GLB: {e}"))?;+ drop(file);+ std::fs::rename(&tmp, &glb).map_err(|e| anyhow!("rename: {e}"))?;+ let _ = std::fs::write(&mat_path, mats_json_str);++ Ok(ConvertResult {+ glb_path: glb,+ size_bytes,+ meshes,+ nodes,+ materials,+ duration_ms,+ cache_hit: false,+ })+}++fn header_u32(r: &ureq::Response, name: &str) -> u32 {+ r.header(name).and_then(|s| s.parse().ok()).unwrap_or(0)+}++/// POST original STEP bytes + chipsmith bake-meta JSON to service-step2glb+/// /bake. Returns the baked STEP file bytes on success, or None when the+/// service hasn't deployed /bake yet (for v0.5.0 and earlier — chipsmith+/// then falls back to writing only the JSON sidecar).+pub fn bake_step(step: &[u8], bake_meta: &serde_json::Value) -> Result<Option<Vec<u8>>> {+ use serde_json::json;+ let url = format!("{}/bake", step2glb_service_url());+ let body = json!({+ "step_b64": base64_encode(step),+ "bake_meta": bake_meta,+ });+ let body_bytes = serde_json::to_vec(&body).map_err(|e| anyhow!("encode: {e}"))?;+ let resp = match ureq::post(&url)+ .set("Content-Type", "application/json")+ .timeout(Duration::from_secs(180))+ .send_bytes(&body_bytes) {+ Ok(r) => r,+ Err(ureq::Error::Status(404, _)) => return Ok(None),+ Err(e) => return Err(anyhow!("POST {url}: {e}")),+ };+ if resp.status() == 404 { return Ok(None); }+ if resp.status() != 200 {+ let s = resp.into_string().unwrap_or_default();+ return Err(anyhow!("/bake non-200: {s}"));+ }+ let mut out = Vec::new();+ use std::io::Read;+ resp.into_reader().take(100 * 1024 * 1024).read_to_end(&mut out)+ .map_err(|e| anyhow!("read body: {e}"))?;+ Ok(Some(out))+}++fn base64_encode(b: &[u8]) -> String {+ const TBL: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";+ let mut out = String::with_capacity((b.len() + 2) / 3 * 4);+ let mut i = 0;+ while i + 3 <= b.len() {+ let n = ((b[i] as u32) << 16) | ((b[i+1] as u32) << 8) | (b[i+2] as u32);+ out.push(TBL[((n >> 18) & 0x3f) as usize] as char);+ out.push(TBL[((n >> 12) & 0x3f) as usize] as char);+ out.push(TBL[((n >> 6) & 0x3f) as usize] as char);+ out.push(TBL[(n & 0x3f) as usize] as char);+ i += 3;+ }+ let rem = b.len() - i;+ if rem == 1 {+ let n = (b[i] as u32) << 16;+ out.push(TBL[((n >> 18) & 0x3f) as usize] as char);+ out.push(TBL[((n >> 12) & 0x3f) as usize] as char);+ out.push_str("==");+ } else if rem == 2 {+ let n = ((b[i] as u32) << 16) | ((b[i+1] as u32) << 8);+ out.push(TBL[((n >> 18) & 0x3f) as usize] as char);+ out.push(TBL[((n >> 12) & 0x3f) as usize] as char);+ out.push(TBL[((n >> 6) & 0x3f) as usize] as char);+ out.push('=');+ }+ out+}++/// POST raw STEP bytes to service-step2glb /step-meta. Returns the parsed+/// JSON tree (PRODUCTs + faces + surface types) on success, or None when+/// the service is on an older version that doesn't have /step-meta yet.+/// Cached at /tmp/adom-chipsmith-cache/<sha>.step-meta.json next to the GLB+/// so re-loads of the same chip skip the network call.+pub fn fetch_step_meta(step: &[u8]) -> Result<Option<serde_json::Value>> {+ let _ = cache::ensure_dir()?;+ let hash = cache::sha256_hex(step);+ let cache_path = cache::cache_dir().join(format!("{hash}.step-meta.json"));+ if cache_path.exists() {+ if let Ok(s) = std::fs::read_to_string(&cache_path) {+ if let Ok(v) = serde_json::from_str::<serde_json::Value>(&s) {+ return Ok(Some(v));+ }+ }+ }+ let url = format!("{}/step-meta", step2glb_service_url());+ let resp = match ureq::post(&url)+ .set("Content-Type", "application/step")+ .timeout(Duration::from_secs(180))+ .send_bytes(step) {+ Ok(r) => r,+ Err(ureq::Error::Status(404, _)) => return Ok(None), // service hasn't redeployed yet+ Err(e) => return Err(anyhow!("POST {url}: {e}")),+ };+ if resp.status() == 404 {+ return Ok(None);+ }+ if resp.status() != 200 {+ let body = resp.into_string().unwrap_or_default();+ return Err(anyhow!("service-step2glb /step-meta non-200: {body}"));+ }+ let body = resp.into_string().map_err(|e| anyhow!("read body: {e}"))?;+ let _ = std::fs::write(&cache_path, &body);+ let v: serde_json::Value = serde_json::from_str(&body).map_err(|e| anyhow!("parse: {e}"))?;+ Ok(Some(v))+}++fn urlencode(s: &str) -> String {+ let mut out = String::with_capacity(s.len());+ for b in s.bytes() {+ if matches!(b, b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~') {+ out.push(b as char);+ } else {+ out.push_str(&format!("%{:02X}", b));+ }+ }+ out+}
src/server/mod.rsadded+1314@@ -0,0 +1,1314 @@+pub mod cache;+pub mod convert;++use anyhow::Result;+use rust_embed::RustEmbed;+use std::collections::VecDeque;+use std::path::PathBuf;+use std::sync::atomic::{AtomicU64, Ordering};+use std::sync::{Arc, Mutex};+use tiny_http::{Header, Method, Request, Response, StatusCode};++// Inherited from adom-step via git submodule at vendor/adom-step/.+// adom-step itself inherits the Babylon9 viewer bundle from the+// canonical adom-3d-viewer-babylon9 repo (its own submodule), so this+// chipsmith binary picks up the entire stack transitively. Workflow:+// git submodule update --remote --recursive+// cargo build --release+// is the full upstream-inheritance loop.+//+// chipsmith-specific UI (8 tabs, detection pipeline, sign-off, etc.)+// will live in src/assets/chipsmith-overlay.{js,css} (separate files)+// served via dedicated routes — never touches the inherited shell.html.+#[derive(RustEmbed)]+#[folder = "vendor/adom-step/vendor/3d-viewer-dist/"]+struct ViewerAssets;++const SHELL_HTML: &[u8] = include_bytes!("../../vendor/adom-step/src/assets/shell.html");+const ADOM_CSS: &[u8] = include_bytes!("../../vendor/adom-step/src/assets/adom.css");+const ICON_SVG: &[u8] = include_bytes!("../assets/icon.svg");+// Chipsmith-specific overlay (branding + 8 tabs + detection + sign-off).+// Auto-injected into the inherited shell.html via <script> tag before+// </body>. Source-of-truth lives in src/assets/chipsmith-overlay.js;+// shell.html itself is read-only (it's the adom-step submodule).+const CHIPSMITH_OVERLAY_JS: &[u8] = include_bytes!("../assets/chipsmith-overlay.js");+// Vendored earcut.min.js (Mapbox earcut v2.2.4, MIT) — exposes+// window.earcut. Required by Babylon's MeshBuilder.CreatePolygon /+// PolygonMeshBuilder for the ds2sf SVG-to-vector-polygon renderer.+// Loaded BEFORE chipsmith-overlay.js so it's globally available when+// renderDs2sfOverlay() runs.+const EARCUT_MIN_JS: &[u8] = include_bytes!("../assets/earcut.min.js");++pub struct ServerConfig {+ pub port: u16,+ pub initial_glb_path: Option<PathBuf>,+ pub initial_source_label: Option<String>,+ /// Provenance: "kicad-library" | "local-file" | "upload" | "unknown".+ /// Drives the UI's "From KiCad library / Local file / Upload" badge so+ /// the user always knows where the geometry came from.+ pub initial_source_type: Option<String>,+}++#[derive(Clone, Debug)]+pub struct ConsoleMessage {+ pub seq: u64,+ pub ts_ms: u64,+ pub level: String,+ pub text: String,+}++#[derive(Clone, Debug)]+pub struct EvalJob {+ pub id: String,+ pub code: String,+ pub result: Option<serde_json::Value>,+ pub done: bool,+ pub taken: bool,+}++pub struct ServerState {+ pub config: ServerConfig,+ pub current_glb_path: Mutex<Option<PathBuf>>,+ pub current_source_label: Mutex<Option<String>>,+ pub current_source_type: Mutex<Option<String>>,+ pub current_materials: Mutex<serde_json::Value>,+ /// Last components tree the UI posted to /api/components.+ pub components: Mutex<serde_json::Value>,+ /// Per-node visibility map {nodeName: bool}.+ pub component_visibility: Mutex<serde_json::Value>,+ /// Active section planes — list of {axis, offset, enabled, flipped}.+ pub sections: Mutex<serde_json::Value>,+ /// Last selection posted by the UI (for `adom-chipsmith inspect`-like queries).+ pub selection: Mutex<serde_json::Value>,+ /// Pending command bus — UI polls /api/poll-command and applies.+ pub pending_command: Mutex<Option<serde_json::Value>>,+ /// chipsmith-spec.json contents (manifest with pin_count, structural_type, body_dims_mm, etc.)+ pub spec: Mutex<serde_json::Value>,+ /// Parsed footprint (.kicad_mod) JSON — pads, layers, drill, etc.+ pub footprint: Mutex<serde_json::Value>,+ /// Multi-source footprints — keyed by source name (datasheet | kicad | fusion | altium).+ /// Each value has the same shape as `footprint` (pad list).+ pub footprint_sources: Mutex<serde_json::Value>,+ /// Where to write <mpn>.chipsmith.json on sign-off (chip-fetcher library dir).+ pub signoff_dir: Mutex<Option<PathBuf>>,+ /// Path to the source STEP file (so /api/step-meta + /bake can re-read it).+ pub source_step_path: Mutex<Option<PathBuf>>,+ /// Sign-off state ({signed, sidecar_path, ...}).+ pub signoff_state: Mutex<serde_json::Value>,+ /// JS eval pipeline.+ pub eval_jobs: Mutex<Vec<EvalJob>>,+ /// Console ring buffer (UI -> server -> CLI tail).+ pub console: Mutex<VecDeque<ConsoleMessage>>,+ pub console_seq: AtomicU64,+}++const CONSOLE_RING_CAP: usize = 2000;++impl ServerState {+ pub fn new(config: ServerConfig) -> Arc<Self> {+ let initial_path = config.initial_glb_path.clone();+ let initial_label = config.initial_source_label.clone();+ let initial_type = config.initial_source_type.clone();+ Arc::new(Self {+ config,+ current_glb_path: Mutex::new(initial_path),+ current_source_label: Mutex::new(initial_label),+ current_source_type: Mutex::new(initial_type),+ current_materials: Mutex::new(serde_json::json!([])),+ components: Mutex::new(serde_json::json!([])),+ component_visibility: Mutex::new(serde_json::json!({})),+ sections: Mutex::new(serde_json::json!([])),+ selection: Mutex::new(serde_json::json!(null)),+ pending_command: Mutex::new(None),+ spec: Mutex::new(serde_json::json!(null)),+ footprint: Mutex::new(serde_json::json!(null)),+ footprint_sources: Mutex::new(serde_json::json!({})),+ signoff_dir: Mutex::new(None),+ source_step_path: Mutex::new(None),+ signoff_state: Mutex::new(serde_json::json!({"signed": false})),+ eval_jobs: Mutex::new(Vec::new()),+ console: Mutex::new(VecDeque::with_capacity(CONSOLE_RING_CAP)),+ console_seq: AtomicU64::new(0),+ })+ }++ pub fn push_console(&self, level: &str, text: &str) {+ let ts_ms = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|d| d.as_millis() as u64)+ .unwrap_or(0);+ let seq = self.console_seq.fetch_add(1, Ordering::SeqCst);+ let mut buf = self.console.lock().unwrap();+ if buf.len() >= CONSOLE_RING_CAP {+ buf.pop_front();+ }+ buf.push_back(ConsoleMessage {+ seq,+ ts_ms,+ level: level.to_string(),+ text: text.to_string(),+ });+ }+}++pub fn run(state: Arc<ServerState>) -> Result<()> {+ // Bind 0.0.0.0 so the cloudflared tunnel (which routes through the+ // container's bridge IP, not host loopback) can reach us. Adom's+ // public proxy at <prefix>-<slug>.adom.cloud → port 8872 fails on a+ // 127.0.0.1-only bind; gallery-style services bind 0.0.0.0 for the+ // same reason. Local CLI calls hit 127.0.0.1 transparently because+ // the OS routes loopback to any-interface listeners.+ let addr = format!("0.0.0.0:{}", state.config.port);+ let server = tiny_http::Server::http(&addr)+ .map_err(|e| anyhow::anyhow!("failed to bind {}: {}", addr, e))?;++ eprintln!("adom-chipsmith listening on http://{}", addr);++ for request in server.incoming_requests() {+ let state = state.clone();+ std::thread::spawn(move || {+ if let Err(e) = route(&state, request) {+ eprintln!("request error: {e:#}");+ }+ });+ }+ Ok(())+}++fn url_decode(s: &str) -> String {+ let mut out = String::with_capacity(s.len());+ let bytes = s.as_bytes();+ let mut i = 0;+ while i < bytes.len() {+ if bytes[i] == b'+' { out.push(' '); i += 1; }+ else if bytes[i] == b'%' && i + 2 < bytes.len() {+ let hh = std::str::from_utf8(&bytes[i+1..i+3]).unwrap_or("00");+ let v = u8::from_str_radix(hh, 16).unwrap_or(0);+ out.push(v as char); i += 3;+ }+ else { out.push(bytes[i] as char); i += 1; }+ }+ out+}++fn chrono_iso8601_now() -> String {+ let secs = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|d| d.as_secs() as i64).unwrap_or(0);+ // Manual ISO-8601 — avoid pulling chrono crate just for this.+ let (y, m, d, hh, mm, ss) = epoch_to_ymdhms(secs);+ format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z", y, m, d, hh, mm, ss)+}++fn epoch_to_ymdhms(epoch: i64) -> (i32, u32, u32, u32, u32, u32) {+ let days_since_epoch = epoch / 86400;+ let secs = epoch.rem_euclid(86400) as u32;+ let hh = secs / 3600;+ let mm = (secs / 60) % 60;+ let ss = secs % 60;+ // Days since 1970-01-01+ let mut y = 1970;+ let mut d = days_since_epoch as i32;+ loop {+ let leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);+ let days_in_year = if leap { 366 } else { 365 };+ if d < days_in_year { break; }+ d -= days_in_year;+ y += 1;+ }+ let leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);+ let mlens = if leap {+ [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]+ } else {+ [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]+ };+ let mut month = 0u32;+ let mut day = d as u32;+ for (i, &n) in mlens.iter().enumerate() {+ if day < n { month = i as u32 + 1; break; }+ day -= n;+ }+ (y, month, day + 1, hh, mm, ss)+}++// Cloudflare-compliant anti-cache headers (per gallia/skills/adom guides).+const CACHE_CONTROL_VAL: &str = "no-store, no-cache, must-revalidate, max-age=0";+const PRAGMA_VAL: &str = "no-cache";+const SURROGATE_VAL: &str = "no-store";++fn no_cache<R: std::io::Read>(resp: Response<R>) -> Response<R> {+ resp.with_header(Header::from_bytes(&b"Cache-Control"[..], CACHE_CONTROL_VAL.as_bytes()).unwrap())+ .with_header(Header::from_bytes(&b"Pragma"[..], PRAGMA_VAL.as_bytes()).unwrap())+ .with_header(Header::from_bytes(&b"Surrogate-Control"[..], SURROGATE_VAL.as_bytes()).unwrap())+}++fn json_response(status: u16, body: &serde_json::Value) -> Response<std::io::Cursor<Vec<u8>>> {+ let bytes = serde_json::to_vec(body).unwrap_or_default();+ let resp = Response::from_data(bytes)+ .with_status_code(StatusCode(status))+ .with_header(Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap());+ no_cache(resp)+}++fn text_response(status: u16, mime: &str, body: Vec<u8>) -> Response<std::io::Cursor<Vec<u8>>> {+ let resp = Response::from_data(body)+ .with_status_code(StatusCode(status))+ .with_header(Header::from_bytes(&b"Content-Type"[..], mime.as_bytes()).unwrap());+ no_cache(resp)+}++fn read_body(req: &mut Request) -> Vec<u8> {+ let mut buf = Vec::new();+ let _ = req.as_reader().read_to_end(&mut buf);+ buf+}++fn parse_json(req: &mut Request) -> Option<serde_json::Value> {+ let bytes = read_body(req);+ serde_json::from_slice(&bytes).ok()+}++fn next_eval_id(state: &ServerState) -> String {+ let seq = state.console_seq.fetch_add(1, Ordering::SeqCst);+ let ts = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|d| d.as_millis() as u64)+ .unwrap_or(0);+ format!("e_{ts}_{seq}")+}++fn route(state: &Arc<ServerState>, mut request: Request) -> Result<()> {+ let url = request.url().to_string();+ // Strip query string for routing — handlers re-parse from request.url() if needed.+ let path = url.split('?').next().unwrap_or(&url).to_string();+ let method = request.method().clone();++ // ---- Health & state ---------------------------------------------------++ if path == "/health" {+ let body = serde_json::json!({+ "ok": true,+ "service": "adom-chipsmith",+ "port": state.config.port,+ });+ return Ok(request.respond(json_response(200, &body))?);+ }++ if path == "/state" {+ let body = serde_json::json!({+ "source": *state.current_source_label.lock().unwrap(),+ "source_type": *state.current_source_type.lock().unwrap(),+ "has_model": state.current_glb_path.lock().unwrap().is_some(),+ "materials": *state.current_materials.lock().unwrap(),+ "components": *state.components.lock().unwrap(),+ "visibility": *state.component_visibility.lock().unwrap(),+ "sections": *state.sections.lock().unwrap(),+ });+ return Ok(request.respond(json_response(200, &body))?);+ }++ if path == "/shutdown" && method == Method::Post {+ let _ = request.respond(json_response(200, &serde_json::json!({"ok": true})));+ std::process::exit(0);+ }++ // ---- Static shell -----------------------------------------------------++ if path == "/" || path == "/index.html" {+ // Inject the chipsmith overlay <script> tag before </body> of the+ // inherited shell.html. The shell.html itself is read-only (it+ // ships from the adom-step submodule); overlay code lives in+ // chipsmith-overlay.js where chipsmith-specific behaviors stack.+ let html = String::from_utf8_lossy(SHELL_HTML);+ // Relative path (NOT leading-slash) so the script resolves+ // correctly under the Hydrogen proxy URL (e.g.+ // /proxy/8872/chipsmith-overlay.js, not /chipsmith-overlay.js+ // at the domain root).+ let injected = html.replace(+ "</body>",+ "<script src=\"earcut.min.js\"></script>\n<script src=\"chipsmith-overlay.js\" defer></script>\n</body>",+ );+ return Ok(request.respond(text_response(200, "text/html; charset=utf-8", injected.into_bytes()))?);+ }+ if path == "/chipsmith-overlay.js" {+ return Ok(request.respond(text_response(200, "application/javascript; charset=utf-8", CHIPSMITH_OVERLAY_JS.to_vec()))?);+ }+ if path == "/earcut.min.js" {+ return Ok(request.respond(text_response(200, "application/javascript; charset=utf-8", EARCUT_MIN_JS.to_vec()))?);+ }+ if path == "/adom.css" {+ return Ok(request.respond(text_response(200, "text/css; charset=utf-8", ADOM_CSS.to_vec()))?);+ }+ if path == "/favicon.svg" || path == "/favicon.ico" {+ return Ok(request.respond(text_response(200, "image/svg+xml", ICON_SVG.to_vec()))?);+ }++ // Vendored Babylon9 ESM bundle and chunks: /viewer/<filename>+ if let Some(asset) = path.strip_prefix("/viewer/") {+ if let Some(file) = ViewerAssets::get(asset) {+ let mime = if asset.ends_with(".js") {+ "application/javascript; charset=utf-8"+ } else if asset.ends_with(".css") {+ "text/css; charset=utf-8"+ } else if asset.ends_with(".env") {+ "application/octet-stream"+ } else {+ "application/octet-stream"+ };+ return Ok(request.respond(text_response(200, mime, file.data.into_owned()))?);+ }+ return Ok(request.respond(json_response(404, &serde_json::json!({"error": "not found"})))?);+ }++ // ---- Model serving ----------------------------------------------------++ if path == "/model.glb" {+ let glb_path = state.current_glb_path.lock().unwrap().clone();+ match glb_path {+ Some(p) => match std::fs::read(&p) {+ Ok(bytes) => {+ let resp = Response::from_data(bytes)+ .with_status_code(StatusCode(200))+ .with_header(Header::from_bytes(&b"Content-Type"[..], &b"model/gltf-binary"[..]).unwrap());+ return Ok(request.respond(no_cache(resp))?);+ }+ Err(e) => {+ return Ok(request.respond(json_response(+ 500,+ &serde_json::json!({"error": format!("read {}: {e}", p.display())}),+ ))?);+ }+ },+ None => {+ return Ok(request.respond(json_response(+ 404,+ &serde_json::json!({"error": "no model loaded — POST a STEP file to /api/load or drop one onto the canvas"}),+ ))?);+ }+ }+ }++ // ---- STEP upload (drop zone + AI) ------------------------------------++ if path == "/api/load" && method == Method::Post {+ let bytes = read_body(&mut request);+ if bytes.is_empty() {+ return Ok(request.respond(json_response(+ 400,+ &serde_json::json!({"error": "empty body"}),+ ))?);+ }+ // Filename hint via X-Filename header (for the source label in HUD).+ let filename = request+ .headers()+ .iter()+ .find(|h| h.field.as_str().as_str().eq_ignore_ascii_case("X-Filename"))+ .map(|h| h.value.as_str().to_string())+ .unwrap_or_else(|| "uploaded.step".to_string());++ match convert::convert_step_bytes(&bytes) {+ Ok(result) => {+ *state.current_glb_path.lock().unwrap() = Some(result.glb_path.clone());+ *state.current_source_label.lock().unwrap() = Some(filename.clone());+ *state.current_source_type.lock().unwrap() = Some("upload".to_string());+ *state.current_materials.lock().unwrap() = result.materials.clone();+ let body = serde_json::json!({+ "ok": true,+ "source": filename,+ "glb_size_bytes": result.size_bytes,+ "meshes": result.meshes,+ "nodes": result.nodes,+ "duration_ms": result.duration_ms,+ "materials": result.materials,+ "cache_hit": result.cache_hit,+ });+ return Ok(request.respond(json_response(200, &body))?);+ }+ Err(e) => {+ return Ok(request.respond(json_response(+ 500,+ &serde_json::json!({"error": e.to_string()}),+ ))?);+ }+ }+ }++ // ---- Components tree (UI <-> server) ---------------------------------++ if path == "/api/components" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ *state.components.lock().unwrap() = v;+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }+ if path == "/api/components" && method == Method::Get {+ let v = state.components.lock().unwrap().clone();+ return Ok(request.respond(json_response(200, &v))?);+ }++ // ---- chipsmith-spec.json (manifest) ---------------------------------++ if path == "/api/spec" && method == Method::Get {+ let v = state.spec.lock().unwrap().clone();+ return Ok(request.respond(json_response(200, &v))?);+ }++ // ---- ds2sf v0.5.1 authoritative footprint extraction ----------------+ // Returns the contents of <MPN>-extraction.result.json so the UI can+ // surface the canonical 2D ground-truth SVG (pixel-trustworthy when+ // sourced from service-kicad-stdlib, layout-only when synthesized).+ if path == "/api/ds2sf" && method == Method::Get {+ let dir = state.signoff_dir.lock().unwrap().clone();+ let mpn = state.current_source_label.lock().unwrap().clone()+ .map(|s| s.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string())+ .unwrap_or_default();+ let mut out = serde_json::json!({"available": false});+ if let Some(d) = &dir {+ let result_path = d.join(format!("{mpn}-extraction.result.json"));+ if result_path.exists() {+ if let Ok(s) = std::fs::read_to_string(&result_path) {+ if let Ok(v) = serde_json::from_str::<serde_json::Value>(&s) {+ out = v;+ out["available"] = serde_json::Value::Bool(true);+ }+ }+ }+ }+ return Ok(request.respond(json_response(200, &out))?);+ }++ // ds2sf v0.6.0 per-pad position data. Returns the pads[] array from+ // <MPN>-footprint.extracted.json in the same schema chipsmith uses+ // for KiCad/Fusion/Altium: [{number, type, shape, at, size, layers, drill}].+ // This is the 5th independent cross-validation source.+ if path == "/api/ds2sf-pads" && method == Method::Get {+ let dir = state.signoff_dir.lock().unwrap().clone();+ let mpn = state.current_source_label.lock().unwrap().clone()+ .map(|s| s.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string())+ .unwrap_or_default();+ if let Some(d) = &dir {+ let fp_path = d.join(format!("{mpn}-footprint.extracted.json"));+ if fp_path.exists() {+ if let Ok(s) = std::fs::read_to_string(&fp_path) {+ if let Ok(v) = serde_json::from_str::<serde_json::Value>(&s) {+ if let Some(pads) = v.get("pads") {+ let pad_count = pads.as_array().map(|a| a.len()).unwrap_or(0);+ let source = v.get("package_outline_source")+ .or_else(|| v.get("kicad_baseline").and_then(|b| b.get("source")))+ .and_then(|v| v.as_str())+ .unwrap_or("unknown");+ return Ok(request.respond(json_response(200, &serde_json::json!({+ "pads": pads,+ "pad_count": pad_count,+ "source": source,+ "mpn": mpn,+ })))?);+ }+ }+ }+ }+ }+ return Ok(request.respond(json_response(404, &serde_json::json!({"error": "no ds2sf pads for this chip (needs ds2sf v0.6.0+)"})))?);+ }++ // ---- Generate Adom 3D chip / footprint via local OCCT ----------------+ // Calls create_chip.py / create_footprint.py locally (via the step2glb+ // venv), converts the resulting STEP to GLB via service-step2glb /convert,+ // and serves the GLB so the frontend can load it as a new outline branch.+ // Cached at /tmp/adom-chipsmith-cache/<mpn>-generated-{chip,fp}.glb.+ if (path == "/api/generate-chip-glb" || path == "/api/generate-footprint-glb") && method == Method::Get {+ let is_chip = path.contains("chip");+ let dir = state.signoff_dir.lock().unwrap().clone();+ let mpn = state.current_source_label.lock().unwrap().clone()+ .map(|s| s.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string())+ .unwrap_or_default();+ let cache_dir = PathBuf::from("/tmp/adom-chipsmith-cache");+ let tag = if is_chip { "chip" } else { "fp" };+ let glb_cache = cache_dir.join(format!("{mpn}-generated-{tag}.glb"));++ if glb_cache.exists() {+ let bytes = std::fs::read(&glb_cache).unwrap_or_default();+ let response = tiny_http::Response::from_data(bytes)+ .with_header("Content-Type: model/gltf-binary".parse::<tiny_http::Header>().unwrap())+ .with_header("Cache-Control: no-cache".parse::<tiny_http::Header>().unwrap());+ return Ok(request.respond(response)?);+ }++ // Need to generate. Read the ds2sf data.+ let fp_path = dir.as_ref().map(|d| d.join(format!("{mpn}-footprint.extracted.json")));+ let fp_json: Option<serde_json::Value> = fp_path.and_then(|p| {+ std::fs::read_to_string(&p).ok().and_then(|s| serde_json::from_str(&s).ok())+ });++ // Read info.json for document_metadata (mpn, manufacturer, etc.)+ let info_path = dir.as_ref().map(|d| d.join("info.json"));+ let info_json: Option<serde_json::Value> = info_path.and_then(|p| {+ std::fs::read_to_string(&p).ok().and_then(|s| serde_json::from_str(&s).ok())+ });+ let doc_meta = serde_json::json!({+ "mpn": info_json.as_ref().and_then(|v| v.get("mpn")).cloned().unwrap_or(serde_json::json!(mpn)),+ "manufacturer": info_json.as_ref().and_then(|v| v.get("manufacturer")).cloned().unwrap_or_default(),+ "package": info_json.as_ref().and_then(|v| v.get("package")).cloned().unwrap_or_default(),+ "datasheet_url": info_json.as_ref().and_then(|v| v.get("datasheet_url")).cloned().unwrap_or_default(),+ });++ let config_json = if is_chip {+ fp_json.as_ref().and_then(|fj| fj.get("chip_3d")).cloned().map(|mut c| {+ if let Some(pads) = fp_json.as_ref().and_then(|fj| fj.get("pads")).and_then(|p| p.as_array()) {+ let annots: Vec<serde_json::Value> = pads.iter().map(|p| {+ let x = p.get("at").and_then(|a| a.get(0)).and_then(|v| v.as_f64()).unwrap_or(0.0);+ serde_json::json!({+ "number": p.get("number").cloned().unwrap_or_default(),+ "name": p.get("name").cloned().unwrap_or_default(),+ "function": p.get("function").cloned().unwrap_or_default(),+ "side": if x > 0.0 { "right" } else { "left" },+ })+ }).collect();+ c["lead_annotations"] = serde_json::json!(annots);+ }+ c["document_metadata"] = doc_meta.clone();+ c+ })+ } else {+ fp_json.as_ref().map(|v| {+ serde_json::json!({+ "pads": v.get("pads").cloned().unwrap_or(serde_json::json!([])),+ "copper_thickness_mm": 0.035,+ "board_thickness_mm": 1.6,+ "document_metadata": doc_meta.clone(),+ })+ })+ };++ if config_json.is_none() {+ return Ok(request.respond(json_response(404, &serde_json::json!({+ "error": format!("no {} data in ds2sf extraction (needs ds2sf v0.8.0+)", if is_chip { "chip_3d" } else { "pads" })+ })))?);+ }++ // Call the live service endpoints (v0.9.0) instead of local OCCT.+ // Generate STEP via /create-chip or /create-footprint, then convert+ // to GLB via /convert. Both go through the same service URL.+ let service_url = crate::cli::step2glb_service_url();+ let endpoint = if is_chip { "create-chip" } else { "create-footprint" };+ let config_str = serde_json::to_string(&config_json.unwrap()).unwrap_or_default();++ // Step 1: generate STEP+ let step_result = ureq::post(&format!("{service_url}/{endpoint}"))+ .set("Content-Type", "application/json")+ .send_string(&config_str);+ let step_bytes = match step_result {+ Ok(resp) => {+ let mut buf = Vec::new();+ let _ = resp.into_reader().read_to_end(&mut buf);+ buf+ }+ Err(e) => {+ return Ok(request.respond(json_response(502, &serde_json::json!({+ "error": format!("/{endpoint} failed: {e}")+ })))?);+ }+ };+ if step_bytes.is_empty() {+ return Ok(request.respond(json_response(502, &serde_json::json!({+ "error": format!("/{endpoint} returned empty response")+ })))?);+ }++ // Save STEP for sign-off compositing + config JSON for annotation readback+ // (the chipsmith_metadata product doesn't survive STEP→GLB conversion).+ let step_path = cache_dir.join(format!("{mpn}-generated-{tag}.step"));+ let annot_path = cache_dir.join(format!("{mpn}-generated-{tag}-annotations.json"));+ let _ = std::fs::create_dir_all(&cache_dir);+ let _ = std::fs::write(&step_path, &step_bytes);+ let _ = std::fs::write(&annot_path, &config_str);++ // Step 2: convert STEP → GLB+ match ureq::post(&format!("{service_url}/convert"))+ .set("Content-Type", "application/step")+ .send_bytes(&step_bytes)+ {+ Ok(resp) => {+ let mut glb_bytes = Vec::new();+ let _ = resp.into_reader().read_to_end(&mut glb_bytes);+ let _ = std::fs::write(&glb_cache, &glb_bytes);+ let response = tiny_http::Response::from_data(glb_bytes)+ .with_header("Content-Type: model/gltf-binary".parse::<tiny_http::Header>().unwrap());+ return Ok(request.respond(response)?);+ }+ Err(e) => {+ return Ok(request.respond(json_response(502, &serde_json::json!({+ "error": format!("GLB conversion failed: {e}")+ })))?);+ }+ }+ }++ // Serve cached annotation JSON for the generated chip / footprint.+ // The chipsmith_metadata product doesn't survive STEP→GLB, so the+ // frontend reads annotations from this endpoint instead.+ if (path == "/api/generated-chip-annotations" || path == "/api/generated-footprint-annotations") && method == Method::Get {+ let tag = if path.contains("chip") { "chip" } else { "fp" };+ let mpn = state.current_source_label.lock().unwrap().clone()+ .map(|s| s.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string())+ .unwrap_or_default();+ let annot_path = PathBuf::from("/tmp/adom-chipsmith-cache").join(format!("{mpn}-generated-{tag}-annotations.json"));+ if annot_path.exists() {+ if let Ok(bytes) = std::fs::read(&annot_path) {+ return Ok(request.respond(text_response(200, "application/json; charset=utf-8", bytes))?);+ }+ }+ return Ok(request.respond(json_response(404, &serde_json::json!({"error": "no cached annotations"})))?);+ }++ // ds2sf v0.8.0 chip_3d + mechanical — the 3D generation recipe and+ // JEDEC dimension provenance. Used by the "Adom 3D chip" outline+ // branch and the Measurements tab provenance links.+ if path == "/api/ds2sf-chip3d" && method == Method::Get {+ let dir = state.signoff_dir.lock().unwrap().clone();+ let mpn = state.current_source_label.lock().unwrap().clone()+ .map(|s| s.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string())+ .unwrap_or_default();+ if let Some(d) = &dir {+ let fp_path = d.join(format!("{mpn}-footprint.extracted.json"));+ if fp_path.exists() {+ if let Ok(s) = std::fs::read_to_string(&fp_path) {+ if let Ok(v) = serde_json::from_str::<serde_json::Value>(&s) {+ let chip_3d = v.get("chip_3d").cloned().unwrap_or(serde_json::json!(null));+ let mechanical = v.get("mechanical").cloned().unwrap_or(serde_json::json!(null));+ if !chip_3d.is_null() {+ return Ok(request.respond(json_response(200, &serde_json::json!({+ "chip_3d": chip_3d,+ "mechanical": mechanical,+ "mpn": mpn,+ })))?);+ }+ }+ }+ }+ }+ return Ok(request.respond(json_response(404, &serde_json::json!({"error": "no chip_3d data (needs ds2sf v0.8.0+)"})))?);+ }++ // Static-serve ds2sf SVGs. v0.8.0 produces two:+ // /api/ds2sf-svg → ds2sf's OWN datasheet-derived SVG (primary)+ // /api/ds2sf-svg?which=pcbnew → pcbnew stdlib render (comparison)+ // Falls back through filename variants for compat with older ds2sf.+ if path == "/api/ds2sf-svg" && method == Method::Get {+ let dir = state.signoff_dir.lock().unwrap().clone();+ let mpn = state.current_source_label.lock().unwrap().clone()+ .map(|s| s.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string())+ .unwrap_or_default();+ let qs = request.url().split('?').nth(1).unwrap_or("");+ let want_pcbnew = qs.contains("which=pcbnew");+ if let Some(d) = &dir {+ // Try the v0.8.0 filenames first, fall back to the old name.+ let candidates = if want_pcbnew {+ vec![+ d.join(format!("{mpn}-footprint.pcbnew.svg")),+ ]+ } else {+ vec![+ d.join(format!("{mpn}-footprint.ds2sf.svg")),+ d.join(format!("{mpn}-footprint.authoritative.svg")),+ ]+ };+ for svg_path in candidates {+ if svg_path.exists() {+ if let Ok(bytes) = std::fs::read(&svg_path) {+ let response = tiny_http::Response::from_data(bytes)+ .with_header("Content-Type: image/svg+xml".parse::<tiny_http::Header>().unwrap())+ .with_header("Cache-Control: no-cache".parse::<tiny_http::Header>().unwrap());+ return Ok(request.respond(response)?);+ }+ }+ }+ }+ return Ok(request.respond(json_response(404, &serde_json::json!({"error": "no ds2sf SVG for this chip"})))?);+ }++ // ---- Local file paths (for "📂 reveal" buttons in the UI) ----------+ // Returns absolute paths to every artifact chipsmith knows about,+ // so the UI can render reveal-in-explorer buttons next to each.+ if path == "/api/paths" && method == Method::Get {+ let dir = state.signoff_dir.lock().unwrap().clone();+ let step_path = state.source_step_path.lock().unwrap().clone();+ let mpn = state.current_source_label.lock().unwrap().clone()+ .map(|s| s.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string())+ .unwrap_or_default();+ let mut out = serde_json::json!({});+ if let Some(d) = &dir {+ let try_paths: Vec<(&str, std::path::PathBuf)> = vec![+ ("step", d.join(format!("{mpn}.step"))),+ ("step_alt", d.join(format!("{mpn}.STEP"))),+ ("kicad_mod", d.join(format!("{mpn}.kicad_mod"))),+ ("kicad_sym", d.join(format!("{mpn}.kicad_sym"))),+ ("eagle_lbr", d.join(format!("{mpn}.lbr"))),+ ("altium_pcblib", d.join(format!("{mpn}.PcbLib"))),+ ("altium_intlib", d.join(format!("{mpn}.IntLib"))),+ ("datasheet", d.join(format!("{mpn}.pdf"))),+ ("info_json", d.join("info.json")),+ ("chipsmith_step", d.join(format!("{mpn}.chipsmith.step"))),+ ("chipsmith_glb", d.join(format!("{mpn}.chipsmith.glb"))),+ ("chipsmith_json", d.join(format!("{mpn}.chipsmith.json"))),+ // ds2sf v0.5.1 — authoritative top-down footprint SVG and+ // its extraction result.json (the latter carries the+ // outputs.footprint_svg_source flag: "service-kicad-stdlib"+ // = pixel-trustworthy, "ds2sf-synthesized" = layout-only).+ ("ds2sf_result", d.join(format!("{mpn}-extraction.result.json"))),+ ("ds2sf_svg", d.join(format!("{mpn}-footprint.authoritative.svg"))),+ ];+ for (key, path) in try_paths {+ if path.exists() {+ out[key] = serde_json::Value::String(path.to_string_lossy().into_owned());+ }+ }+ out["dir"] = serde_json::Value::String(d.to_string_lossy().into_owned());+ }+ if let Some(p) = &step_path {+ // Override with the actual loaded STEP path (may differ from dir/mpn.step+ // when the user opened a file directly via `view`).+ out["step"] = serde_json::Value::String(p.to_string_lossy().into_owned());+ }+ return Ok(request.respond(json_response(200, &out))?);+ }++ // ---- Reveal a path in VS Code Explorer (delegates to adom-vscode) --+ if path == "/api/reveal" && method == Method::Post {+ let body = parse_json(&mut request).unwrap_or_default();+ let path_arg = body.get("path").and_then(|v| v.as_str()).unwrap_or("");+ if path_arg.is_empty() {+ return Ok(request.respond(json_response(400, &serde_json::json!({"error": "path required"})))?);+ }+ // Hard-fail any non-existent path to avoid leaking accidental injection.+ if !std::path::Path::new(path_arg).exists() {+ return Ok(request.respond(json_response(404, &serde_json::json!({"error": format!("path not found: {path_arg}")})))?);+ }+ let result = std::process::Command::new("adom-vscode")+ .arg("reveal")+ .arg(path_arg)+ .output();+ match result {+ Ok(o) if o.status.success() => {+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true, "path": path_arg})))?);+ }+ Ok(o) => {+ let stderr = String::from_utf8_lossy(&o.stderr).to_string();+ return Ok(request.respond(json_response(500, &serde_json::json!({"error": format!("adom-vscode reveal failed: {stderr}")})))?);+ }+ Err(e) => {+ return Ok(request.respond(json_response(500, &serde_json::json!({"error": format!("adom-vscode not on PATH: {e}")})))?);+ }+ }+ }++ // ---- Open a file in VS Code's editor pane. Pairs with /api/reveal+ // — reveal pings the Explorer sidebar, open actually opens the+ // file in a tab. Used by chipsmith for source files like .kicad_mod+ // / .step / info.json (PDFs go to adom-pdf-viewer instead — see the+ // datasheet section in the right-pane Source tab). -----------+ if path == "/api/open" && method == Method::Post {+ let body = parse_json(&mut request).unwrap_or_default();+ let path_arg = body.get("path").and_then(|v| v.as_str()).unwrap_or("");+ if path_arg.is_empty() {+ return Ok(request.respond(json_response(400, &serde_json::json!({"error": "path required"})))?);+ }+ if !std::path::Path::new(path_arg).exists() {+ return Ok(request.respond(json_response(404, &serde_json::json!({"error": format!("path not found: {path_arg}")})))?);+ }+ let result = std::process::Command::new("adom-vscode")+ .arg("open")+ .arg(path_arg)+ .output();+ match result {+ Ok(o) if o.status.success() => {+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true, "path": path_arg})))?);+ }+ Ok(o) => {+ let stderr = String::from_utf8_lossy(&o.stderr).to_string();+ return Ok(request.respond(json_response(500, &serde_json::json!({"error": format!("adom-vscode open failed: {stderr}")})))?);+ }+ Err(e) => {+ return Ok(request.respond(json_response(500, &serde_json::json!({"error": format!("adom-vscode not on PATH: {e}")})))?);+ }+ }+ }++ // ---- Datasheet PDF (served from the chip-fetcher dir) --------------+ if path == "/api/datasheet" && method == Method::Get {+ let dir = state.signoff_dir.lock().unwrap().clone();+ if let Some(d) = dir {+ // Prefer <MPN>.pdf, then any single .pdf in the dir as fallback.+ let mpn = state.current_source_label.lock().unwrap().clone()+ .map(|s| s.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string())+ .unwrap_or_default();+ let canonical = d.join(format!("{mpn}.pdf"));+ if canonical.exists() {+ if let Ok(bytes) = std::fs::read(&canonical) {+ let resp = Response::from_data(bytes)+ .with_status_code(StatusCode(200))+ .with_header(Header::from_bytes(&b"Content-Type"[..], &b"application/pdf"[..]).unwrap())+ .with_header(Header::from_bytes(&b"Content-Disposition"[..], format!("inline; filename=\"{mpn}.pdf\"").as_bytes()).unwrap());+ return Ok(request.respond(resp)?);+ }+ }+ }+ return Ok(request.respond(json_response(404, &serde_json::json!({"error": "no datasheet PDF"})))?);+ }+ if path == "/api/spec" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ // Update in-memory spec.+ *state.spec.lock().unwrap() = v.clone();+ // ALSO persist to info.json next to the source STEP, so the+ // user's pin_count / structural_type / body_dims_mm edits+ // survive reloads. We merge into the existing info.json+ // rather than overwrite — non-chipsmith fields stay intact.+ let dir = state.signoff_dir.lock().unwrap().clone();+ let mut wrote = false;+ if let Some(d) = dir {+ let info_path = d.join("info.json");+ let existing: serde_json::Value = std::fs::read_to_string(&info_path)+ .ok()+ .and_then(|s| serde_json::from_str(&s).ok())+ .unwrap_or_else(|| serde_json::json!({}));+ let mut merged = existing.as_object().cloned().unwrap_or_default();+ if let Some(obj) = v.as_object() {+ for (k, v) in obj { merged.insert(k.clone(), v.clone()); }+ }+ if let Ok(s) = serde_json::to_string_pretty(&serde_json::Value::Object(merged)) {+ if std::fs::write(&info_path, s).is_ok() {+ wrote = true;+ }+ }+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true, "persisted": wrote})))?);+ }+ return Ok(request.respond(json_response(400, &serde_json::json!({"error": "no body"})))?);+ }++ // ---- footprint (.kicad_mod) -----------------------------------------++ if path == "/api/footprint" && method == Method::Get {+ let v = state.footprint.lock().unwrap().clone();+ return Ok(request.respond(json_response(200, &v))?);+ }+ if path == "/api/footprint" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ *state.footprint.lock().unwrap() = v;+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }++ // ---- Multi-source footprints (datasheet / kicad / fusion / altium) ----++ if path == "/api/footprint-sources" && method == Method::Get {+ let v = state.footprint_sources.lock().unwrap().clone();+ return Ok(request.respond(json_response(200, &v))?);+ }++ // ---- KiCad-stock reference fetch (service-kicad proxy) -------------+ // Given a footprint_name like "Package_SOIC/SOIC-8", fetch:+ // - The .kicad_mod from service-kicad+ // - The reference 3D STEP (when available) and convert via service-step2glb+ // Returns JSON { kicad_mod_text, glb_url } so the JS can render both+ // as a "KiCad reference" overlay branch in the scene-graph.+ if path == "/api/kicad-stock" && method == Method::Get {+ let qs = url.split('?').nth(1).unwrap_or("");+ let mut footprint_name = String::new();+ let mut chip_3d_name = String::new();+ let mut client_candidates: Vec<String> = Vec::new();+ for kv in qs.split('&') {+ let mut it = kv.splitn(2, '=');+ let k = it.next().unwrap_or("");+ let v = it.next().unwrap_or("").to_string();+ let v = url_decode(&v);+ if k == "footprint_name" { footprint_name = v; }+ else if k == "kicad_3d_name" { chip_3d_name = v; }+ else if k == "candidates" {+ // pipe-delimited list of "library/leaf" entries (no .pretty,+ // no .kicad_mod) — chipsmith JS pre-resolves them from the+ // package_kind + pin_count + body_dims_mm.+ client_candidates.extend(v.split('|').filter(|s| !s.is_empty()).map(|s| s.to_string()));+ }+ }+ if footprint_name.is_empty() && client_candidates.is_empty() {+ return Ok(request.respond(json_response(400, &serde_json::json!({"error": "footprint_name or candidates required"})))?);+ }+ // Build URLs to the shared service-kicad container.+ let kicad_base = std::env::var("KICAD_SERVICE_API")+ .unwrap_or_else(|_| "https://kicad-rk5ue5pcfemi.adom.cloud".to_string());+ let kicad_base = kicad_base.trim_end_matches('/');+ // Build the URL list. service-kicad serves the static library as+ // /footprints/<library>.pretty/<leaf>.kicad_mod (note the .pretty on+ // the directory). Try every permutation of "library/leaf" the+ // client gave us, plus legacy "library/leaf.kicad_mod" forms.+ let mut leaf_candidates: Vec<String> = Vec::new();+ if !footprint_name.is_empty() { leaf_candidates.push(footprint_name.clone()); }+ for c in &client_candidates {+ if !leaf_candidates.contains(c) { leaf_candidates.push(c.clone()); }+ }+ let mut url_candidates: Vec<String> = Vec::new();+ for c in &leaf_candidates {+ // c is "<library>/<leaf>" — split into library + leaf.+ let mut parts = c.splitn(2, '/');+ let library = parts.next().unwrap_or("");+ let leaf = parts.next().unwrap_or("");+ if library.is_empty() || leaf.is_empty() { continue; }+ // Canonical: Package_X.pretty/<leaf>.kicad_mod+ url_candidates.push(format!("{kicad_base}/footprints/{library}.pretty/{leaf}.kicad_mod"));+ // Legacy fallback (some early service-kicad versions stripped .pretty)+ url_candidates.push(format!("{kicad_base}/footprints/{library}/{leaf}.kicad_mod"));+ }+ let mut out = serde_json::json!({"footprint_name": footprint_name, "kicad_3d_name": chip_3d_name});+ let mut got = false;+ let mut last_err = String::new();+ let mut tried: Vec<serde_json::Value> = Vec::new();+ for fp_url in &url_candidates {+ let res = ureq::get(fp_url).timeout(std::time::Duration::from_secs(8)).call();+ match res {+ Ok(r) if r.status() == 200 => {+ let txt = r.into_string().unwrap_or_default();+ // Parse the .kicad_mod text into a pads + silk_markers+ // structure that matches the four user-supplied+ // footprint sources, so cross-validation in Tab 4+ // can include the stock library as a fifth pair.+ if let Ok(parsed) = crate::cli::view_library::parse_kicad_mod_text(&txt) {+ out["parsed"] = parsed;+ }+ out["kicad_mod_text"] = serde_json::Value::String(txt);+ out["kicad_mod_url"] = serde_json::Value::String(fp_url.clone());+ // Surface the matched library / leaf so the UI can show+ // exactly which one chipsmith found.+ if let Some(after) = fp_url.split("/footprints/").nth(1) {+ let matched = after.trim_end_matches(".kicad_mod").replace(".pretty/", "/");+ out["matched_footprint_name"] = serde_json::Value::String(matched);+ }+ tried.push(serde_json::json!({ "url": fp_url, "status": 200 }));+ got = true;+ break;+ }+ Ok(r) => {+ let s = r.status();+ last_err = format!("HTTP {s} on {fp_url}");+ tried.push(serde_json::json!({ "url": fp_url, "status": s }));+ }+ Err(e) => {+ last_err = format!("{e} on {fp_url}");+ tried.push(serde_json::json!({ "url": fp_url, "error": format!("{e}") }));+ }+ }+ }+ if !got {+ out["error_kicad_mod"] = serde_json::json!({+ "error": last_err,+ "tried_count": tried.len(),+ "tried": tried,+ });+ }+ // Try the matching 3D model too. KiCad layout: Package_X.3dshapes/<leaf>.step+ if !chip_3d_name.is_empty() {+ let m_url = format!("{kicad_base}/models/{}.step", chip_3d_name);+ out["kicad_3d_url"] = serde_json::Value::String(m_url);+ } else if got {+ // Derive 3D name from the matched footprint when possible.+ if let Some(matched) = out.get("matched_footprint_name").and_then(|v| v.as_str()) {+ let parts: Vec<&str> = matched.splitn(2, '/').collect();+ if parts.len() == 2 {+ let model_path = format!("{}.3dshapes/{}.step", parts[0], parts[1]);+ out["kicad_3d_name"] = serde_json::Value::String(model_path.clone());+ out["kicad_3d_url"] = serde_json::Value::String(format!("{kicad_base}/models/{model_path}"));+ }+ }+ }+ return Ok(request.respond(json_response(200, &out))?);+ }+ if path == "/api/footprint-sources" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ *state.footprint_sources.lock().unwrap() = v;+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }++ // ---- step-meta (B-rep proxy from service-step2glb) ------------------++ if path == "/api/step-meta" && method == Method::Get {+ // Proxy to service-step2glb /step-meta, caching the resulting JSON+ // by source-STEP SHA-256 next to the GLB.+ let step_path = state.source_step_path.lock().unwrap().clone();+ let step_path = match step_path {+ Some(p) => p,+ None => return Ok(request.respond(json_response(404, &serde_json::json!({+ "error": "no source STEP path on this server (started without view-library?)"+ })))?),+ };+ let step_bytes = match std::fs::read(&step_path) {+ Ok(b) => b,+ Err(e) => return Ok(request.respond(json_response(500, &serde_json::json!({+ "error": format!("read step: {e}")+ })))?),+ };+ match convert::fetch_step_meta(&step_bytes) {+ Ok(Some(v)) => return Ok(request.respond(json_response(200, &v))?),+ Ok(None) => return Ok(request.respond(json_response(404, &serde_json::json!({+ "error": "service-step2glb /step-meta endpoint not yet deployed (returned 404). Falling back to GLB-walk detection."+ })))?),+ Err(e) => return Ok(request.respond(json_response(502, &serde_json::json!({+ "error": format!("upstream /step-meta failed: {e}")+ })))?),+ }+ }++ // ---- chipsmith sign-off bake ----------------------------------------++ if path == "/api/signoff" && method == Method::Post {+ let body = parse_json(&mut request).unwrap_or_default();+ let source_label_owned = state.current_source_label.lock().unwrap().clone();+ let source_label = source_label_owned.clone().unwrap_or_else(|| "unknown".to_string());+ let mpn = source_label.trim_end_matches(".step").trim_end_matches(".STEP").trim_end_matches(".stp").to_string();+ let sidecar_dir = state.signoff_dir.lock().unwrap().clone();+ let sidecar_path = match &sidecar_dir {+ Some(d) => d.join(format!("{mpn}.chipsmith.json")),+ None => std::env::temp_dir().join(format!("{mpn}.chipsmith.json")),+ };+ let baked_step_path = match &sidecar_dir {+ Some(d) => d.join(format!("{mpn}.chipsmith.step")),+ None => std::env::temp_dir().join(format!("{mpn}.chipsmith.step")),+ };+ let mut sidecar = body.clone();+ if sidecar.is_object() {+ sidecar["mpn"] = serde_json::Value::String(mpn.clone());+ sidecar["source"] = serde_json::Value::String(source_label.clone());+ sidecar["signed_at"] = serde_json::Value::String(chrono_iso8601_now());+ sidecar["chipsmith_version"] = serde_json::Value::String(env!("CARGO_PKG_VERSION").to_string());+ }+ // Try to bake the canonical .chipsmith.step via service-step2glb /bake.+ // Requires the source STEP bytes; we read them from the chip-fetcher+ // library dir if available.+ let mut step_baked = false;+ let mut bake_status = String::new();+ if let Some(d) = &sidecar_dir {+ let mut step_path = d.join(format!("{mpn}.step"));+ if !step_path.exists() { step_path = d.join(format!("{mpn}.STEP")); }+ if step_path.exists() {+ if let Ok(step_bytes) = std::fs::read(&step_path) {+ let bake_meta = serde_json::json!({+ "applied_scale": sidecar.get("applied_unit_scale").cloned().unwrap_or(serde_json::json!(1.0)),+ "applied_translation_mm": sidecar.get("applied_translation_mm").cloned().unwrap_or(serde_json::json!([0.0, 0.0, 0.0])),+ "applied_rotation": sidecar.get("applied_rotation").cloned().unwrap_or(serde_json::json!({})),+ "role_tags": sidecar.get("role_tags").cloned().unwrap_or(serde_json::json!([])),+ "pin1_marker": sidecar.get("pin1_chip").cloned().unwrap_or(serde_json::json!(null)),+ // Names of GLB-product entries the user (or chipsmith+ // auto-flag) marked as decorative — vendor logos, lens+ // cosmetics, embossed marketing geometry. service-step2glb+ // /bake reparents these under a `chipsmith_decorative`+ // assembly node so downstream Adom tools can default+ // them OFF. NEVER deleted, only moved.+ "decorative_product_names": sidecar.get("decorative_product_names").cloned().unwrap_or(serde_json::json!([])),+ "signoff_metadata": {+ "chipsmith_version": env!("CARGO_PKG_VERSION"),+ "signed_at": chrono_iso8601_now(),+ "spec": sidecar.get("spec").cloned().unwrap_or(serde_json::json!(null)),+ },+ });+ match convert::bake_step(&step_bytes, &bake_meta) {+ Ok(Some(out)) => {+ if let Err(e) = std::fs::write(&baked_step_path, &out) {+ bake_status = format!("bake produced bytes but write failed: {e}");+ } else {+ step_baked = true;+ bake_status = format!("baked to {}", baked_step_path.display());+ }+ }+ Ok(None) => {+ bake_status = "service-step2glb /bake not deployed yet (404) — JSON sidecar still written".to_string();+ }+ Err(e) => {+ bake_status = format!("bake error: {e} — JSON sidecar still written");+ }+ }+ }+ }+ }+ if let Err(e) = std::fs::write(&sidecar_path, serde_json::to_string_pretty(&sidecar).unwrap_or_default()) {+ return Ok(request.respond(json_response(500, &serde_json::json!({"error": format!("write sidecar: {e}")})))?);+ }+ *state.signoff_state.lock().unwrap() = serde_json::json!({+ "signed": true,+ "sidecar_path": sidecar_path.to_string_lossy(),+ "step_baked": step_baked,+ "step_path": if step_baked { Some(baked_step_path.to_string_lossy().to_string()) } else { None },+ "bake_status": bake_status,+ });+ return Ok(request.respond(json_response(200, &serde_json::json!({+ "ok": true,+ "sidecar_path": sidecar_path.to_string_lossy(),+ "step_baked": step_baked,+ "step_path": if step_baked { Some(baked_step_path.to_string_lossy().to_string()) } else { None },+ "bake_status": bake_status,+ })))?);+ }+ if path == "/api/signoff" && method == Method::Get {+ let v = state.signoff_state.lock().unwrap().clone();+ return Ok(request.respond(json_response(200, &v))?);+ }++ // ---- Visibility ------------------------------------------------------++ if path == "/api/visibility" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ *state.component_visibility.lock().unwrap() = v;+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }++ // ---- Sections --------------------------------------------------------++ if path == "/api/sections" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ *state.sections.lock().unwrap() = v;+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }+ if path == "/api/sections" && method == Method::Get {+ let v = state.sections.lock().unwrap().clone();+ return Ok(request.respond(json_response(200, &v))?);+ }++ // ---- Selection (UI -> server, drives `inspect` CLI) ------------------++ if path == "/api/selection" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ *state.selection.lock().unwrap() = v;+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }+ if path == "/api/selection" && method == Method::Get {+ let v = state.selection.lock().unwrap().clone();+ return Ok(request.respond(json_response(200, &v))?);+ }++ // ---- Command bus (CLI -> server -> UI poll) --------------------------++ if path == "/api/command" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ *state.pending_command.lock().unwrap() = Some(v);+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }+ if path == "/api/poll-command" && method == Method::Get {+ let v = state.pending_command.lock().unwrap().take();+ return Ok(request.respond(json_response(200, &serde_json::json!({"command": v})))?);+ }++ // ---- JS eval pipeline ------------------------------------------------++ if path == "/eval" && method == Method::Post {+ let body: serde_json::Value = parse_json(&mut request).unwrap_or(serde_json::json!({}));+ let code = body+ .get("code")+ .and_then(|v| v.as_str())+ .unwrap_or("")+ .to_string();+ if code.is_empty() {+ return Ok(request.respond(json_response(+ 400,+ &serde_json::json!({"error": "missing 'code'"}),+ ))?);+ }+ let id = next_eval_id(state);+ state.eval_jobs.lock().unwrap().push(EvalJob {+ id: id.clone(),+ code,+ result: None,+ done: false,+ taken: false,+ });+ return Ok(request.respond(json_response(200, &serde_json::json!({"id": id})))?);+ }++ if path == "/eval/pending" && method == Method::Get {+ let mut jobs = state.eval_jobs.lock().unwrap();+ let next = jobs+ .iter_mut()+ .find(|j| !j.done && !j.taken)+ .map(|j| {+ j.taken = true;+ serde_json::json!({"id": j.id, "code": j.code})+ });+ return Ok(request.respond(json_response(200, &serde_json::json!({"job": next})))?);+ }++ if path.starts_with("/eval/") && path.ends_with("/result") && method == Method::Post {+ let id = path+ .trim_start_matches("/eval/")+ .trim_end_matches("/result")+ .to_string();+ let body = parse_json(&mut request).unwrap_or(serde_json::json!(null));+ let mut jobs = state.eval_jobs.lock().unwrap();+ if let Some(job) = jobs.iter_mut().find(|j| j.id == id) {+ job.result = Some(body);+ job.done = true;+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }++ if path.starts_with("/eval/")+ && !path.ends_with("/result")+ && path != "/eval/pending"+ && method == Method::Get+ {+ let id = path.trim_start_matches("/eval/").to_string();+ let jobs = state.eval_jobs.lock().unwrap();+ let body = match jobs.iter().find(|j| j.id == id) {+ Some(j) => serde_json::json!({+ "id": j.id,+ "done": j.done,+ "result": j.result.clone().unwrap_or(serde_json::Value::Null),+ }),+ None => serde_json::json!({"error": "no such id"}),+ };+ return Ok(request.respond(json_response(200, &body))?);+ }++ // ---- Console forwarder ----------------------------------------------++ if path == "/console" && method == Method::Post {+ if let Some(v) = parse_json(&mut request) {+ let level = v.get("level").and_then(|x| x.as_str()).unwrap_or("log");+ let text = v.get("text").and_then(|x| x.as_str()).unwrap_or("");+ state.push_console(level, text);+ }+ return Ok(request.respond(json_response(200, &serde_json::json!({"ok": true})))?);+ }++ if path == "/console" && method == Method::Get {+ let qs = url.split('?').nth(1).unwrap_or("");+ let since: u64 = qs+ .split('&')+ .find_map(|p| p.strip_prefix("since=").and_then(|v| v.parse().ok()))+ .unwrap_or(0);+ let buf = state.console.lock().unwrap();+ let messages: Vec<_> = buf+ .iter()+ .filter(|m| m.seq >= since)+ .map(|m| {+ serde_json::json!({+ "seq": m.seq,+ "ts_ms": m.ts_ms,+ "level": m.level,+ "text": m.text,+ })+ })+ .collect();+ let next_since = buf.iter().last().map(|m| m.seq + 1).unwrap_or(since);+ let body = serde_json::json!({"messages": messages, "next_since": next_since});+ return Ok(request.respond(json_response(200, &body))?);+ }++ // ---- 404 -------------------------------------------------------------++ Ok(request.respond(json_response(+ 404,+ &serde_json::json!({"error": format!("no route for {} {}", method, path)}),+ ))?)+}