Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { ASSEMBLER_SERVICE_URL } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { findStorageObjectBuckets } from "@carbon/database/storage-objects";
import { getCompanyPrivateBucket, TEMP_STAGING_BUCKET } from "@carbon/files";
import type { LoaderFunctionArgs } from "react-router";
import { getDatabaseClient } from "~/services/database.server";
// Resolves a model's optimised / preview artifact storage paths for the
// progressive ModelPreview (parity with the ERP api+/model.artifacts route).
// The MES model tab fetches this by modelUploadId (derived from modelPath —
// modelUpload.id is the model filename) so the viewer only requests artifacts
// that actually exist, and falls back to the raw upload (WASM tier) otherwise.
// Returns only paths; the bytes are still served through the auth-checked
// /file/preview proxy, scoped to the company.
export async function loader({ request, params }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {});
const { modelUploadId } = params;
if (!modelUploadId) throw new Response("Not found", { status: 404 });
const model = await client
.from("modelUpload")
.select(
"size, originalSize, optimizedSize, optimizedModelPath, glbPath, thumbnailPath, pmiPath, optimizeStatus, optimizedAt, modelPath, originalPath"
)
.eq("id", modelUploadId)
.eq("companyId", companyId)
.maybeSingle();
// A real query failure is a 5xx, not a 404 — don't mask a backend error as
// "no such model".
if (model.error) throw new Response("Internal error", { status: 500 });
// No such model for this tenant → 404, don't fabricate an all-nulls body. An
// all-nulls 200 is indistinguishable from "exists but not optimised", so the
// viewer's reuse guard can't tell the two apart and would auto-fire an
// optimise against an id that doesn't exist (→ a reoptimise 404 loop). The
// client query treats this 404 as "no artifacts", and never auto-fires.
if (!model.data) throw new Response("Not found", { status: 404 });
// Raw-source pointer for the viewer's WASM fallback tier (renders the original
// upload when no artifact exists). Prefer the retained ORIGINAL (xbf-compacted
// rows keep the uploaded STEP at `originalPath` — the `.xbf.zst` itself isn't
// WASM-renderable); else the uncompacted `modelPath`. A retained raw is
// copied to the company bucket; a just-uploaded one is briefly in
// ephemeral `temp-staging` before relocation; a pruned one is in neither → no
// raw tier (rely on the GLB).
const modelPath = model.data?.modelPath ?? null;
const rawCandidate =
model.data?.originalPath ??
(modelPath && !modelPath.toLowerCase().endsWith(".zst") ? modelPath : null);
// A raw lives in the company's own bucket once it is durable, or in
// `temp-staging` before that. One query answers both paths.
const buckets = await findStorageObjectBuckets(getDatabaseClient(), {
names: [rawCandidate, modelPath].filter((name): name is string => !!name),
buckets: [getCompanyPrivateBucket(companyId), TEMP_STAGING_BUCKET]
});
const rawBucket = rawCandidate ? (buckets.get(rawCandidate) ?? null) : null;
const rawPath = rawBucket ? rawCandidate : null;
const sourceAvailable = modelPath ? buckets.has(modelPath) : false;
return {
optimizedModelPath: model.data?.optimizedModelPath ?? null,
lodPath: null as string | null,
glbPath: model.data?.glbPath ?? null,
thumbnailPath: model.data?.thumbnailPath ?? null,
// The model's PMI (pmi.json), when its STEP carried any.
pmiPath: model.data?.pmiPath ?? null,
rawPath,
rawBucket: rawBucket ?? getCompanyPrivateBucket(companyId),
// Whether the optimiser is configured at all. When false, the viewer hides
// the "Optimize failed · Retry" affordance and never auto-fires an optimise.
optimizerAvailable: Boolean(ASSEMBLER_SERVICE_URL),
optimizeStatus: model.data?.optimizeStatus ?? null,
size: model.data?.originalSize ?? model.data?.size ?? null,
optimizedSize: model.data?.optimizedSize ?? null,
// Cache-buster for the optimised GLB: it lives at a STABLE path behind an
// immutable-cached preview URL, so a re-optimise would otherwise keep
// serving the browser-cached old mesh.
optimizedAt: model.data?.optimizedAt ?? null,
sourceAvailable
};
}