Reusable kits (authoring.kit)¶
The kit is a set of small, pure building blocks lifted out of the ALEF, KernelF and VibroSelect languages (the ALEF port has since moved out of this repository, see the changelog), so a new language does not have to copy them: exact numbers, generated output, table analysis, test-suite bookkeeping, derived diagram edges and structural checks. Plan and history: Reusable kits plan.
Where it lives and how you get it¶
| Your code | How you reach the kit |
|---|---|
| A built-in plugin in this repository | import { … } from "../../../src/kit/<area>/…" (or src/kit/index.ts) |
An external plugin (a .ts/.tsx loaded from a dev folder or shipped as an extension) |
window.StruktJS.authoring.kit.<area> |
An external plugin is one self-contained module with no module resolution, so it can only use what the host hands it. The hosts that load external plugins (the Theia IDE in the browser, the desktop app and the VS Code webview) all provide window.StruktJS.authoring; the standalone dev webview and the embeddable editor do not load external plugins:
const { BaseConcept, ChildSlot, Prop, conceptMetaFromClass, kit } = (window as any).StruktJS.authoring;
const { Decimal, CurrencyTable, makeMoney } = kit.numeric;
const { analyzeCells, cellMatches } = kit.tables;
const { placementCheck, declaredCheck } = kit.checks;
The kit is Provisional until 1.0 (Stability policy): its exports are pinned by tests/fixtures/api-surface.json, and
kit.KIT_API_VERSION is bumped on an incompatible change so a plugin can feature-detect.
The areas¶
| Area | What it gives you | Used by |
|---|---|---|
numeric |
Rat (exact fractions, seven rounding modes), CurrencyTable + Money (a rounding policy applied on every operation), UnitTable + Quantity (exact units, affine temperatures), Decimal |
KernelF, ALEF (moved out), devices |
report |
escaping per context, el + xmlText/htmlText, jsonText, camelName/slug/safeUrl, htmlPage/standaloneHtml, ExportFile |
vibro and robot previews, lang-xml |
tables |
analyzeCells (gaps and rows that can never win in a first-match table) and cellMatches |
KernelF table and split |
testing |
SuiteRecorder: the counts, errors and per-assertion details every Tests-pane provider reports |
four test languages |
graph |
keyedEdges and edgesIntoProvider: the connections a diagram reads from its boxes' content |
device lifecycle |
checks |
placementCheck, declaredCheck, oneOfCheck, rootOf: the structural checks concepts keep needing |
the ALEF plugins (moved out) |
The API of each area is in the API reference.
Rules every kit area follows¶
- Pure. No AST,
Language, Solid or DOM; nodes are read only byheadandchildren.tests/test-kit-purity.mjsenforces it. - Instances, not ambient state. A currency table or a unit table is an object you create and pass in; two languages never share hidden state.
- You word the messages. A check or failure takes its text from you, so diagnostics stay in your language.
- Failures are deliberate. Kit arithmetic fails through
EvalFailure(src/runtime-values.ts), which an evaluator reports as a failed evaluation, not a crash.
A worked example: a tariff language from the kit alone¶
examples/kit-tariff-plugin/tariff.tsx is a complete shipping-tariff language — a rate table by weight and zone, exact money, quotes with VAT, an HTML and JSON
price list — with no import and no npm package. Each concept leans on one area:
| Concept | What it does | Kit |
|---|---|---|
currency("EUR", 2, "half-even") |
declares the money policy; checks the declaration | numeric.CurrencyTable, numeric.currencyProblem |
rates("parcel", rateRow("<5", "EU", 4.9), …) |
a first-match table whose cells are text (<5, 5..20, !=EU, any) |
tables.analyzeCells for a "no row covers weight = 5, zone = …" or "row 2 is unreachable" warning |
rateRow(…) |
must sit inside rates |
checks.placementCheck |
quote("parcel", 3, "EU", 2, 21) |
first matching row × quantity + VAT, each step rounded by the currency | tables.cellMatches, numeric.makeMoney/mulMoney/addMoney, checks.declaredCheck for the table name |
named outputs priceListHtml / priceListJson (transformations with a root files rule) |
the price list | report.el, report.htmlPage, report.htmlText, report.jsonText |
Pack and install it like any language extension:
npm run plugin:pack -- examples/kit-tariff-plugin --out /tmp
code --install-extension /tmp/struktjs.lang-tariff-kit-sample-1.0.0.vsix
tests/test-kit-proof-tariff.mjs takes the same file through the path an outside plugin takes: compiled with the plugin loader's options, loaded with only
window.StruktJS.authoring in scope, parsed, checked, evaluated, exported and packed, and compares its money with an independent decimal.js calculation.
What building it taught (read before writing your own)¶
- A model file's top-level calls are statements of an implicit root. The concept implementing
IRootis the file's root node; do not write it into the file. Declare it with anameand astmtsslot and writecurrency(…),rates(…)at the top level, aslang-heatingdoes. #lines load as acommentconcept. A language without one cannot hold a leading comment; give it a one-propcommentstatement.- A plugin cannot
import "decimal.js". The money and unit APIs take and returnDecimal; get the class fromkit.numeric.Decimal. - Match what you analyse.
analyzeCellssays which rows can win;cellMatchessays which row does. Parse your own surface text (here<5,5..20) into the kit's cells once and use both. - Hooks are methods of the concept class:
eval({ env, evaluate, output })with the slots onthis, andchecks()returning objects whosecheck(node, lang, navFor)receives the raw node, with children in slot order (the kit's checks take exactly that). - Not on
authoringtoday:lineCommentand the concept interfaces (Concept(IStatement, …)); useText/Childand thecategorystring, as the example does.
What the kit does not have, and why¶
The plan also considered a decision-table concept pack, a documentation pack, a locale pack, a quantities concept pack, wizard templates and a shared rule fixpoint. Reading the code showed that these either share no vocabulary, have a single language behind them, or need design rather than extraction; each is explained in the plan's "what was built" sections.
Adding to the kit¶
- Two consumers first (or one plus a real outside need): extract code that already exists in two places, not code you expect to need.
- Keep it pure: put it in
src/kit/<area>/, import nothing from the engine or fromplugins/;tests/test-kit-purity.mjswill tell you. - Move the existing code onto it without changing its behaviour, and prove that with the language's own output: the exported files'
sha256fromstrukts generate,strukts check --all/test --all, or the resolved edges, before and after. - Export it from the area's
index.ts, then runnode scripts/api-surface.mjs --write; a removal or change needs a changelog entry (stability policy).