Parser Contract
The signature, inputs, outputs, and constraints a conforming parser must satisfy.
Parser Contract
The parser transforms normalized input into a data model. It is the only place in an MView where "understanding the file" happens — the component downstream should never need to.
Signature
export default async function parse(input: any, context: any): Promise<unknown>;input— the normalized representation of the source file, shaped according to the capability declared inaccepts. See Capabilities for the exact shape per capability.context— everything else the parser is allowed to know:
{
input, // normalized source data
source, // { path, contentHash } — metadata only, not a file handle
settings, // the manifest's settings, merged with any runtime overrides
resources, // resolved paths under resources/
workspace // workspace-level metadata (no arbitrary fs access)
}- Return value — any JSON-serializable data model. This becomes the
dataprop passed to the component.
Example
export default async function parse(input: any, context: any) {
const rows = input.rows ?? [];
const totalSales = rows.reduce((sum: number, row: any) => {
return sum + Number(row.Total ?? row.total ?? 0);
}, 0);
const salesByMonth = rows.map((row: any) => ({
month: row.Month ?? row.month,
total: Number(row.Total ?? row.total ?? 0)
}));
const topClients = [...rows]
.sort((a: any, b: any) => Number(b.Total ?? b.total ?? 0) - Number(a.Total ?? a.total ?? 0))
.slice(0, context.settings?.topClientsLimit ?? 10);
return { totalSales, salesByMonth, topClients };
}Constraints (MUST / MUST NOT)
- The parser MUST NOT read files directly. It only sees the normalized
inputit's handed. - The parser MUST NOT use absolute paths. Any path it produces or consumes is relative.
- The parser MUST be a pure function of
(input, context)— same inputs, same output. No reliance on hidden global or session state. - The parser MUST be re-executable: running it again after the source file changes should produce an updated, correct data model without manual intervention.
- The parser SHOULD fail with a clear, catchable error rather than throwing something opaque
when
inputdoesn't match what it expects (e.g., an expected column is missing) — the runtime surfaces this to the user with an option to regenerate.
What the runtime guarantees the parser
inputhas already been normalized according to the capability the MView declared inaccepts— the parser is not responsible for parsing raw file bytes.context.sourcedescribes the file but does not grant filesystem access.context.settingsis the manifest'ssettings, already merged with any user overrides.
Relationship to the component
The parser's return value is opaque to the runtime — it's whatever shape the paired component expects. The Component Contract defines what happens next.