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: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.
import type { CatalogInput } from "./catalog";
import type { ValueOrRef, ValueType } from "./types";
type ListType = Extract<ValueType, { kind: "list" }>;
/** What the validator needs of a step — an action or an integration step alike. */
export type BatchableStep = {
inputs: Record<string, CatalogInput>;
/** Hidden behind the form's Advanced section, but written into the same `inputs`
* bag — so every check the visible ones get, these need too. */
advancedInputs?: Record<string, CatalogInput>;
outputs: Record<string, ValueType>;
batchable: boolean;
/** Each group needs at least one of its input names supplied. */
requireOneOf?: string[][];
};
/**
* Whether an action step repeats, and over what. Derived from the wiring rather than
* stored: a list fed to a slot that takes one value has exactly one sensible meaning,
* so a flag saying so could only ever agree or be wrong.
*/
export type BatchPlan =
| { kind: "none" }
| { kind: "repeats"; input: string; type: ListType }
/** More than one list: we will not guess which the customer meant to repeat over.
* The two named are the first two in declaration order, which is enough to explain it. */
| { kind: "ambiguous"; first: string; second: string };
/**
* The inputs that could carry the list a step repeats over: declared to take a single
* value, and actually supplied. An input already reading the loop item is skipped, or
* resolving it would recurse into the loop it helps define.
*/
export function batchCandidates(
action: BatchableStep,
inputs: Record<string, ValueOrRef>
): string[] {
if (!action.batchable) return [];
// Advanced inputs share the stored bag, so they are candidates like any other —
// reading only the visible map would let a list wired into an Advanced scalar
// slot run the step once instead of once per item.
const declared = { ...action.inputs, ...action.advancedInputs };
// Declaration order, so the validator and the engine always agree on "the first one".
return Object.keys(declared).filter((name) => {
if (declared[name]?.type.kind === "list") return false;
// Rows are never the list a step repeats over.
if (declared[name]?.pairs) return false;
const supplied = inputs[name];
return supplied !== undefined && supplied.kind !== "item";
});
}
/** The same rule the engine follows, resolved through whatever knows an input's type. */
export function batchPlan(
action: BatchableStep,
inputs: Record<string, ValueOrRef>,
typeOfInput: (name: string) => ValueType | undefined
): BatchPlan {
const lists: { input: string; type: ListType }[] = [];
for (const input of batchCandidates(action, inputs)) {
const type = typeOfInput(input);
if (type?.kind === "list") lists.push({ input, type });
}
const [first, second] = lists;
if (first === undefined) return { kind: "none" };
return second === undefined
? { kind: "repeats", input: first.input, type: first.type }
: { kind: "ambiguous", first: first.input, second: second.input };
}