@orthograph/compiler 0.0.0-stage → 0.1.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +4 -2
  3. package/dist/index.d.ts +12 -0
  4. package/dist/index.js +9 -0
  5. package/dist/main.d.ts +2 -0
  6. package/dist/main.js +18 -0
  7. package/dist/modules/app/internal/checks.d.ts +30 -0
  8. package/dist/modules/app/internal/checks.js +99 -0
  9. package/dist/modules/app/internal/describe.d.ts +6 -0
  10. package/dist/modules/app/internal/describe.js +59 -0
  11. package/dist/modules/app/internal/hash.d.ts +1 -0
  12. package/dist/modules/app/internal/hash.js +7 -0
  13. package/dist/modules/app/internal/known.d.ts +19 -0
  14. package/dist/modules/app/internal/known.js +45 -0
  15. package/dist/modules/app/internal/read.d.ts +6 -0
  16. package/dist/modules/app/internal/read.js +45 -0
  17. package/dist/modules/app/service.d.ts +2 -0
  18. package/dist/modules/app/service.js +3 -0
  19. package/dist/modules/app/types.d.ts +87 -0
  20. package/dist/modules/app/types.js +1 -0
  21. package/dist/modules/compare/internal/assets.d.ts +4 -0
  22. package/dist/modules/compare/internal/assets.js +40 -0
  23. package/dist/modules/compare/internal/chain.d.ts +4 -0
  24. package/dist/modules/compare/internal/chain.js +37 -0
  25. package/dist/modules/compare/internal/diff.d.ts +3 -0
  26. package/dist/modules/compare/internal/diff.js +27 -0
  27. package/dist/modules/compare/internal/versions.d.ts +9 -0
  28. package/dist/modules/compare/internal/versions.js +75 -0
  29. package/dist/modules/compare/service.d.ts +2 -0
  30. package/dist/modules/compare/service.js +43 -0
  31. package/dist/modules/compare/types.d.ts +47 -0
  32. package/dist/modules/compare/types.js +1 -0
  33. package/dist/modules/lock/internal/evolve.d.ts +7 -0
  34. package/dist/modules/lock/internal/evolve.js +52 -0
  35. package/dist/modules/lock/internal/runtime.d.ts +3 -0
  36. package/dist/modules/lock/internal/runtime.js +12 -0
  37. package/dist/modules/lock/service.d.ts +4 -0
  38. package/dist/modules/lock/service.js +20 -0
  39. package/dist/modules/lock/types.d.ts +101 -0
  40. package/dist/modules/lock/types.js +6 -0
  41. package/dist/modules/model/internal/allowed.d.ts +26 -0
  42. package/dist/modules/model/internal/allowed.js +78 -0
  43. package/dist/modules/model/internal/approval.d.ts +4 -0
  44. package/dist/modules/model/internal/approval.js +10 -0
  45. package/dist/modules/model/internal/assets.d.ts +5 -0
  46. package/dist/modules/model/internal/assets.js +31 -0
  47. package/dist/modules/model/internal/bits.d.ts +6 -0
  48. package/dist/modules/model/internal/bits.js +9 -0
  49. package/dist/modules/model/internal/chain.d.ts +9 -0
  50. package/dist/modules/model/internal/chain.js +47 -0
  51. package/dist/modules/model/internal/codes.d.ts +59 -0
  52. package/dist/modules/model/internal/codes.js +80 -0
  53. package/dist/modules/model/internal/derive.d.ts +11 -0
  54. package/dist/modules/model/internal/derive.js +31 -0
  55. package/dist/modules/model/internal/describe.d.ts +8 -0
  56. package/dist/modules/model/internal/describe.js +248 -0
  57. package/dist/modules/model/internal/device.d.ts +5 -0
  58. package/dist/modules/model/internal/device.js +12 -0
  59. package/dist/modules/model/internal/environments.d.ts +7 -0
  60. package/dist/modules/model/internal/environments.js +90 -0
  61. package/dist/modules/model/internal/exits.d.ts +49 -0
  62. package/dist/modules/model/internal/exits.js +85 -0
  63. package/dist/modules/model/internal/fields.d.ts +23 -0
  64. package/dist/modules/model/internal/fields.js +50 -0
  65. package/dist/modules/model/internal/fillable.d.ts +3 -0
  66. package/dist/modules/model/internal/fillable.js +9 -0
  67. package/dist/modules/model/internal/forms.d.ts +47 -0
  68. package/dist/modules/model/internal/forms.js +75 -0
  69. package/dist/modules/model/internal/found.d.ts +40 -0
  70. package/dist/modules/model/internal/found.js +60 -0
  71. package/dist/modules/model/internal/handler-files.d.ts +18 -0
  72. package/dist/modules/model/internal/handler-files.js +23 -0
  73. package/dist/modules/model/internal/hash.d.ts +4 -0
  74. package/dist/modules/model/internal/hash.js +7 -0
  75. package/dist/modules/model/internal/holds.d.ts +19 -0
  76. package/dist/modules/model/internal/holds.js +65 -0
  77. package/dist/modules/model/internal/invariants.d.ts +38 -0
  78. package/dist/modules/model/internal/invariants.js +134 -0
  79. package/dist/modules/model/internal/journeys.d.ts +15 -0
  80. package/dist/modules/model/internal/journeys.js +135 -0
  81. package/dist/modules/model/internal/layout.d.ts +143 -0
  82. package/dist/modules/model/internal/layout.js +203 -0
  83. package/dist/modules/model/internal/lock-fields.d.ts +3 -0
  84. package/dist/modules/model/internal/lock-fields.js +17 -0
  85. package/dist/modules/model/internal/money.d.ts +17 -0
  86. package/dist/modules/model/internal/money.js +49 -0
  87. package/dist/modules/model/internal/nullable.d.ts +7 -0
  88. package/dist/modules/model/internal/nullable.js +21 -0
  89. package/dist/modules/model/internal/operations.d.ts +27 -0
  90. package/dist/modules/model/internal/operations.js +81 -0
  91. package/dist/modules/model/internal/reads.d.ts +16 -0
  92. package/dist/modules/model/internal/reads.js +58 -0
  93. package/dist/modules/model/internal/rules.d.ts +2 -0
  94. package/dist/modules/model/internal/rules.js +33 -0
  95. package/dist/modules/model/internal/same-value.d.ts +10 -0
  96. package/dist/modules/model/internal/same-value.js +50 -0
  97. package/dist/modules/model/internal/secrets.d.ts +4 -0
  98. package/dist/modules/model/internal/secrets.js +35 -0
  99. package/dist/modules/model/internal/source-refusals.d.ts +7 -0
  100. package/dist/modules/model/internal/source-refusals.js +52 -0
  101. package/dist/modules/model/internal/sources.d.ts +12 -0
  102. package/dist/modules/model/internal/sources.js +65 -0
  103. package/dist/modules/model/internal/stores.d.ts +20 -0
  104. package/dist/modules/model/internal/stores.js +44 -0
  105. package/dist/modules/model/internal/things.d.ts +33 -0
  106. package/dist/modules/model/internal/things.js +99 -0
  107. package/dist/modules/model/service.d.ts +4 -0
  108. package/dist/modules/model/service.js +105 -0
  109. package/dist/modules/model/types.d.ts +485 -0
  110. package/dist/modules/model/types.js +5 -0
  111. package/dist/modules/mounts/fixtures/both/things/echo/thing.d.ts +1 -0
  112. package/dist/modules/mounts/fixtures/both/things/echo/thing.js +1 -0
  113. package/dist/modules/mounts/fixtures/both/things/echo/verbs/only.d.ts +1 -0
  114. package/dist/modules/mounts/fixtures/both/things/echo/verbs/only.js +1 -0
  115. package/dist/modules/mounts/internal/attribute.d.ts +2 -0
  116. package/dist/modules/mounts/internal/attribute.js +4 -0
  117. package/dist/modules/mounts/internal/calls.d.ts +12 -0
  118. package/dist/modules/mounts/internal/calls.js +42 -0
  119. package/dist/modules/mounts/internal/choices.d.ts +11 -0
  120. package/dist/modules/mounts/internal/choices.js +34 -0
  121. package/dist/modules/mounts/internal/fold.d.ts +6 -0
  122. package/dist/modules/mounts/internal/fold.js +8 -0
  123. package/dist/modules/mounts/internal/needs.d.ts +14 -0
  124. package/dist/modules/mounts/internal/needs.js +26 -0
  125. package/dist/modules/mounts/internal/verbs.d.ts +11 -0
  126. package/dist/modules/mounts/internal/verbs.js +44 -0
  127. package/dist/modules/mounts/service.d.ts +2 -0
  128. package/dist/modules/mounts/service.js +165 -0
  129. package/dist/modules/mounts/types.d.ts +78 -0
  130. package/dist/modules/mounts/types.js +1 -0
  131. package/dist/modules/source/internal/plugins.d.ts +25 -0
  132. package/dist/modules/source/internal/plugins.js +49 -0
  133. package/dist/modules/source/service.d.ts +6 -0
  134. package/dist/modules/source/service.js +86 -0
  135. package/dist/modules/source/types.d.ts +55 -0
  136. package/dist/modules/source/types.js +6 -0
  137. package/dist/modules/time/internal/anchor.d.ts +2 -0
  138. package/dist/modules/time/internal/anchor.js +48 -0
  139. package/dist/modules/time/internal/keeper.d.ts +13 -0
  140. package/dist/modules/time/internal/keeper.js +19 -0
  141. package/dist/modules/time/internal/zones.d.ts +1 -0
  142. package/dist/modules/time/internal/zones.js +3 -0
  143. package/dist/modules/time/service.d.ts +2 -0
  144. package/dist/modules/time/service.js +56 -0
  145. package/dist/modules/time/types.d.ts +89 -0
  146. package/dist/modules/time/types.js +1 -0
  147. package/package.json +35 -3
@@ -0,0 +1,135 @@
1
+ import { NotAllowed } from "orthograph";
2
+ import { Blanks, ErrorNames, EventNames, Scripting } from "orthograph/server";
3
+ const data = (value) => {
4
+ if (Scripting.isRef(value))
5
+ return { ref: Scripting.pathOf(value) };
6
+ if (typeof value === "bigint")
7
+ return value.toString();
8
+ if (Array.isArray(value))
9
+ return value.map(data);
10
+ if (value && typeof value === "object")
11
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, data(v)]));
12
+ return value;
13
+ };
14
+ /** A payload with each secret field redacted: `secret` says by key (a ref is only a path, so it stays). */
15
+ const publicData = (values, secret) => Object.fromEntries(Object.entries(values).map(([key, value]) => [
16
+ key, secret(key) && !Scripting.isRef(value) ? { redacted: true } : data(value),
17
+ ]));
18
+ /** The blanks a value is or holds one level down: a stub's world, payload, expected fields or id (K16 e). */
19
+ const blanksIn = (value) => {
20
+ if (Blanks.isBlank(value))
21
+ return [value.what];
22
+ if (!value || typeof value !== "object" || Scripting.isRef(value))
23
+ return [];
24
+ return Object.values(value).flatMap((v) => (Blanks.isBlank(v) ? [v.what] : []));
25
+ };
26
+ /** A step's blanks: in its call's payload and due instant (the keeper's) and in each check (the check itself, its fields, its id or its value). */
27
+ export const stepBlanks = (step) => [
28
+ ...(step.call.kind === "clock-call" || step.call.kind === "stand-call" ? [] : [...blanksIn(step.call.payload), ..."due" in step.call ? blanksIn(step.call.due) : []]),
29
+ ...step.checks.flatMap((c) => (Blanks.isBlank(c) ? [c.what]
30
+ : c.kind === "emits" || c.kind === "refuses" ? blanksIn(c.fields) : c.kind === "is-in" ? blanksIn(c.id) : c.kind === "expect-equal" ? blanksIn(c.value) : [])),
31
+ ];
32
+ /** The script's own words, which a cast member may not take: `s.world`, `s.last` and `s.step` are the script's. (`s.clock` is shared: a member may be named it.) */
33
+ const SCRIPT_WORDS = { world: "what the starting world gave", last: "the last answer", step: "its steps" };
34
+ /** The cast members whose names the script already uses, each refused with a plain message naming where it is cast. */
35
+ export const castClashes = (cast, file, who) => Object.keys(cast).filter((name) => Object.hasOwn(SCRIPT_WORDS, name)).map((name) => `${file}: ${who} casts a member \`${name}\`, which takes a name the script already uses for ${SCRIPT_WORDS[name]}; rename it`);
36
+ export const storiesOf = (layout, operations, allowed, problems = []) => {
37
+ const names = new Map([...layout.things.values()].flatMap((t) => [...t.verbs].map(([verb, v]) => [v.declaration, `${t.name}.${verb}`])));
38
+ const things = new Map([...layout.things.values()].map((t) => [t.thing, t.name]));
39
+ const refused = new Map([...layout.things.values()].map((t) => [t.machine?.notFrom, t.name]));
40
+ const castOf = (cast, file) => Object.fromEntries(Object.entries(cast).map(([name, role]) => [name, allowed(role, file)]));
41
+ const callOf = (call) => {
42
+ // A step that moves the world's clock has no operation: the model tells it as a step that is not in the model's operations, and prove runs it against the world's clock door (M8.07).
43
+ if (call.kind === "clock-call")
44
+ return undefined;
45
+ // A stand-in's command runs no operation either: prove sends it to the mount's stand-in (G16-adapter-mounts call 4).
46
+ if (call.kind === "stand-call")
47
+ return undefined;
48
+ // The tools list runs no operation: it is told as a step that is not in the model's operations.
49
+ if (call.operation === undefined)
50
+ return undefined;
51
+ const operation = names.get(call.operation);
52
+ if (operation === undefined)
53
+ return undefined;
54
+ // A blank operation has no model to read visibility from, but its payload fields are declared: redact by declaration.
55
+ const declared = call.operation.payload;
56
+ const secret = (key) => operations[operation] !== undefined
57
+ ? operations[operation].payload[key]?.visibility !== undefined
58
+ : Blanks.isBlank(declared) || declared?.[key]?.visibility !== "public";
59
+ return {
60
+ actor: call.actor, operation, payload: publicData(call.payload, secret),
61
+ ...(call.request === undefined ? {} : { request: call.request }),
62
+ ...(typeof call.due === "string" ? { due: call.due } : {}),
63
+ ...(call.via === undefined ? {} : { via: call.via }),
64
+ };
65
+ };
66
+ const checkOf = (check) => {
67
+ switch (check.kind) {
68
+ case "expect": return { kind: check.kind, match: check.match.source, flags: check.match.flags };
69
+ case "expect-equal": return { kind: check.kind, value: data(check.value) };
70
+ case "emits": return {
71
+ kind: check.kind, event: EventNames.nameOf(check.event),
72
+ fields: Object.fromEntries(Object.entries(check.fields).map(([k, v]) => [k, check.event.fields[k]?.visibility === "public" ? data(v) : { redacted: true }])),
73
+ };
74
+ case "refuses": {
75
+ const error = check.error;
76
+ const thing = "kind" in error && error.kind === "not-from" ? refused.get(error) : undefined;
77
+ const missing = "kind" in error && error.kind === "not-found" ? things.get(error.thing) : undefined;
78
+ const fields = "fields" in error ? error.fields : undefined;
79
+ return {
80
+ kind: check.kind,
81
+ error: thing !== undefined ? `${thing}.notFrom` : missing !== undefined ? `${missing}.notFound`
82
+ : error === NotAllowed ? NotAllowed.name : ErrorNames.nameOf(error),
83
+ fields: Object.fromEntries(Object.entries(check.fields).map(([k, v]) => [k, fields?.[k]?.visibility && fields[k]?.visibility !== "public" ? { redacted: true } : data(v)])),
84
+ };
85
+ }
86
+ case "is-in": return { kind: check.kind, thing: things.get(check.thing), id: data(check.id), state: check.state };
87
+ case "lacks":
88
+ case "has": return { kind: check.kind, operations: check.operations.map((op) => names.get(op) ?? "an undeclared operation") };
89
+ default: return check;
90
+ }
91
+ };
92
+ const journeys = {};
93
+ // A journey with blanks (a stub from `ortho add journeys`) is listed here, and prove skips it, naming it (K16 e).
94
+ const blanks = {};
95
+ for (const [file, { declaration: j, sourceHash }] of [...layout.journeys].sort(([a], [b]) => a.localeCompare(b))) {
96
+ const cast = castOf(j.cast, file);
97
+ // A clash would let the script's own `s.clock` hide a cast member silently, so the journey is refused and its steps not read.
98
+ const clashes = castClashes(j.cast, file, `the journey "${j.name}"`);
99
+ problems.push(...clashes);
100
+ const declared = clashes.length > 0 ? [] : j.steps(Scripting.script(j.cast));
101
+ const found = [...blanksIn(j.starts), ...declared.flatMap(stepBlanks)];
102
+ if (found.length > 0)
103
+ blanks[file] = found;
104
+ const steps = declared.map((step) => {
105
+ const call = callOf(step.call);
106
+ return call === undefined
107
+ ? { title: step.title, skipped: step.call.kind === "clock-call" ? "a step that moves the world's clock is not an operation" : step.call.kind === "stand-call" ? "a command to a stand-in is not an operation" : step.call.operation === undefined ? "the tools list is not an operation" : "operation is outside the model" }
108
+ : { title: step.title, call, checks: step.checks.filter((c) => !Blanks.isBlank(c)).map(checkOf) };
109
+ });
110
+ const starts = Blanks.isBlank(j.starts) ? null : j.starts ?? null;
111
+ journeys[file] = { name: j.name, file, cast, starts, steps, ...(found.length > 0 ? { blanks: found } : {}), ...(sourceHash ? { sourceHash } : {}) };
112
+ }
113
+ const startingWorlds = {};
114
+ for (const [name, { file, declaration: w, sourceHash }] of layout.startingWorlds) {
115
+ const cast = castOf(w.cast, file);
116
+ const clashes = castClashes(w.cast, file, `the starting world "${name}"`);
117
+ problems.push(...clashes);
118
+ const members = { ...w.cast };
119
+ const seen = new Set([name]);
120
+ for (let parent = w.from; parent !== undefined && !seen.has(parent);) {
121
+ seen.add(parent);
122
+ const ancestor = layout.startingWorlds.get(parent)?.declaration;
123
+ for (const [actor, role] of Object.entries(ancestor?.cast ?? {}))
124
+ members[actor] ??= role;
125
+ parent = ancestor?.from;
126
+ }
127
+ // A name an ancestor casts counts too: the script is not made for a cast it would refuse (the ancestor's own clash is a problem of its own).
128
+ const script = castClashes(members, file, name).length > 0 ? undefined : Scripting.script(members);
129
+ startingWorlds[name] = {
130
+ name, file, from: w.from ?? null, cast, ...(sourceHash ? { sourceHash } : {}),
131
+ ...(script === undefined ? { makes: [], gives: {} } : { makes: w.makes(script).map((call) => callOf(call) ?? null), gives: data(w.gives(script)) }),
132
+ };
133
+ }
134
+ return { journeys, startingWorlds, journeyBlanks: blanks };
135
+ };
@@ -0,0 +1,143 @@
1
+ import type { Agent, AnyErrorClass, Asset, Declared, Environment, Event, Held, Hold, Journey, Machine, Mount, Operation, Problem, Read, Role, SecretDeclaration, SourceKind, StartingWorld, Store, Thing, Unique, Value } from "orthograph";
2
+ import type { LoadedFile } from "#modules/source/types.js";
3
+ import type { CallReach } from "#modules/mounts/types.js";
4
+ import type { FromModel } from "../types.ts";
5
+ /** Where a plugin's store came from (M1-plugins section 7): `store` is the plugin's own name for it. */
6
+ export interface StoreFrom {
7
+ readonly mount: string;
8
+ readonly package: string;
9
+ readonly version: string;
10
+ readonly store: string;
11
+ }
12
+ /** A store declared in `blueprint/stores/<name>.ts`, or brought by a mount under the name `<mount>-<name>`. */
13
+ export interface StoreFile {
14
+ readonly name: string;
15
+ readonly file: string;
16
+ readonly store: Store;
17
+ readonly from?: StoreFrom;
18
+ }
19
+ export interface ThingFiles {
20
+ readonly name: string;
21
+ /** Its folder, as messages name it: `blueprint/things/<name>`, or `<package>/blueprint/things/<thing>`. */
22
+ readonly base: string;
23
+ readonly from: FromModel;
24
+ /** A generated adapter's `<package>/src/written.ts`, where its errors' exit base is set: what an exit refusal tells the author to change. */
25
+ readonly written?: string;
26
+ thing?: Thing;
27
+ machine?: Machine;
28
+ readonly events: Map<string, Event>;
29
+ readonly errors: Map<string, AnyErrorClass>;
30
+ readonly roles: Map<string, Role>;
31
+ /** Its rules (G5), by the name each export gives them: `handle.oneHolderPerName` is `oneHolderPerName`. */
32
+ readonly invariants: Map<string, Unique>;
33
+ readonly verbs: Map<string, {
34
+ readonly file: string;
35
+ readonly declaration: Operation | Read; /** Set on an operation the app adds to a plugin's thing. */
36
+ readonly from?: FromModel;
37
+ }>;
38
+ /** What can fail, beside the thing (`problem.ts`). */
39
+ problem?: {
40
+ readonly file: string;
41
+ readonly declaration: Problem;
42
+ };
43
+ /** The switches it declares (`holds.ts`), by the name each export gives them. */
44
+ readonly holds: Map<string, {
45
+ readonly file: string;
46
+ readonly declaration: Hold;
47
+ }>;
48
+ }
49
+ interface StoryFile<D> {
50
+ readonly file: string;
51
+ readonly declaration: D;
52
+ readonly sourceHash?: string;
53
+ }
54
+ /** A secret declared in a `secrets.ts`: its export's name, and the file that exports it. */
55
+ export interface SecretFile {
56
+ readonly export: string;
57
+ readonly file: string;
58
+ readonly declaration: SecretDeclaration;
59
+ }
60
+ /** A mount's secret: which mount brought it, so its name can be derived from the mount and the export. */
61
+ export interface MountSecret extends SecretFile {
62
+ readonly mount: string;
63
+ readonly kind: "feature" | "adapter" | "projection";
64
+ /** The mount's own file, `blueprint/features/auth.ts`. */
65
+ readonly mountFile: string;
66
+ readonly package: string;
67
+ readonly version: string;
68
+ }
69
+ export interface Layout {
70
+ readonly journeys: Map<string, StoryFile<Journey>>;
71
+ readonly startingWorlds: Map<string, StoryFile<StartingWorld>>;
72
+ readonly values: Map<string, Value>;
73
+ /** The file each named value is declared in, by the value's name: where a message about it points. */
74
+ readonly valueFiles: Map<string, string>;
75
+ /** Assets by the name their file gives them: `assets/usdc.ts` is `usdc` (M6-money item 2). */
76
+ readonly assets: Map<string, {
77
+ readonly file: string;
78
+ readonly asset: Asset;
79
+ }>;
80
+ /** Environments by the name their file gives them: `environments/staging.ts` is `staging`. */
81
+ readonly environments: Map<string, {
82
+ readonly file: string;
83
+ readonly declaration: Environment;
84
+ }>;
85
+ /** Stores by the name their file gives them: `stores/ledger.ts` is `ledger`. */
86
+ readonly stores: Map<string, StoreFile>;
87
+ readonly things: Map<string, ThingFiles>;
88
+ /** Mounts by the name their file gives them: `features/echo.ts` is `echo`. */
89
+ readonly mounts: Map<string, MountFile>;
90
+ /** What each mounted adapter's call is, by the call object a step holds (filled in when the mounts are folded). */
91
+ readonly calls: Map<unknown, CallReach>;
92
+ /** `blueprint/agents/<name>.ts`: an agent kind, by the file's name. */
93
+ readonly agents: Map<string, {
94
+ readonly file: string;
95
+ readonly declaration: Agent; /** The mount that brought it; absent for the app's own. */
96
+ readonly mount?: string;
97
+ }>;
98
+ /** `blueprint/agents/<name>.ts` that declare an instance of a thing (`Superagent.declare`). */
99
+ readonly declared: Map<string, {
100
+ readonly file: string;
101
+ readonly declaration: Declared;
102
+ }>;
103
+ /** `blueprint/sources/<name>.ts`: a source kind, by the file's name. */
104
+ readonly sources: Map<string, {
105
+ readonly file: string;
106
+ readonly declaration: SourceKind<any>; /** The name it is exported under: what a message says to add. */
107
+ readonly exportName: string;
108
+ readonly mount?: string;
109
+ readonly package?: string;
110
+ }>;
111
+ /** What a mount file says a hold holds (`export const pausable = processing.holds({...})`), with the file. */
112
+ readonly holdLists: Array<{
113
+ readonly file: string;
114
+ readonly name: string;
115
+ readonly declaration: Held;
116
+ }>;
117
+ /** The secrets of this blueprint's own `secrets.ts`, by export. */
118
+ readonly secrets: Map<string, SecretFile>;
119
+ /** The secrets of the mounted plugins (filled in when the mounts are folded). */
120
+ readonly mountSecrets: Array<MountSecret>;
121
+ readonly problems: Array<string>;
122
+ }
123
+ export declare const MOUNT_FOLDERS: {
124
+ readonly features: "feature";
125
+ readonly adapters: "adapter";
126
+ readonly projections: "projection";
127
+ };
128
+ export interface MountFile {
129
+ readonly name: string;
130
+ readonly kind: (typeof MOUNT_FOLDERS)[keyof typeof MOUNT_FOLDERS];
131
+ readonly file: string;
132
+ readonly mount: Mount;
133
+ }
134
+ export declare const NAME: RegExp;
135
+ /**
136
+ * Read files laid out as a blueprint. `at` says how a file is named in messages and imports: the project's own are
137
+ * `blueprint/<path>`; a plugin's are its package specifier, `@x/y/blueprint/<path>`, and only its `things/` are read.
138
+ */
139
+ export declare const readLayout: (files: ReadonlyArray<LoadedFile>, at?: (path: string) => string) => Layout;
140
+ /** `change-role` becomes `changeRole`; `allowance` becomes `Allowance` with `pascal`. */
141
+ export declare const camel: (name: string) => string;
142
+ export declare const pascal: (name: string) => string;
143
+ export {};
@@ -0,0 +1,203 @@
1
+ export const MOUNT_FOLDERS = { features: "feature", adapters: "adapter", projections: "projection" };
2
+ const kindOf = (x) => typeof x === "object" && x !== null ? x.kind : undefined;
3
+ const isErrorClass = (x) => typeof x === "function" && "exit" in x && "fields" in x && "schema" in x;
4
+ export const NAME = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
5
+ /**
6
+ * Read files laid out as a blueprint. `at` says how a file is named in messages and imports: the project's own are
7
+ * `blueprint/<path>`; a plugin's are its package specifier, `@x/y/blueprint/<path>`, and only its `things/` are read.
8
+ */
9
+ export const readLayout = (files, at = (path) => path.startsWith("../") ? path.slice(3) : `blueprint/${path}`) => {
10
+ const layout = { values: new Map(), valueFiles: new Map(), assets: new Map(), environments: new Map(), stores: new Map(), things: new Map(), mounts: new Map(), calls: new Map(), agents: new Map(), declared: new Map(), sources: new Map(), holdLists: [], secrets: new Map(), mountSecrets: [], journeys: new Map(), startingWorlds: new Map(), problems: [] };
11
+ const thingFiles = (name) => {
12
+ const found = layout.things.get(name);
13
+ if (found)
14
+ return found;
15
+ const fresh = { name, base: at(`things/${name}`), from: { kind: "app" }, events: new Map(), errors: new Map(), roles: new Map(), invariants: new Map(), verbs: new Map(), holds: new Map() };
16
+ layout.things.set(name, fresh);
17
+ return fresh;
18
+ };
19
+ for (const { path, exports, sourceHash } of files) {
20
+ const parts = path.replace(/\.(ts|js)$/, "").split("/");
21
+ if ((parts[0] === "journeys" && parts.length === 2) || (parts[0] === ".." && parts[1] === "journeys" && parts.length === 3)) {
22
+ const d = exports["default"];
23
+ if (kindOf(d) !== "journey")
24
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.journey`);
25
+ else
26
+ layout.journeys.set(at(path), { file: at(path), declaration: d, ...(sourceHash ? { sourceHash } : {}) });
27
+ continue;
28
+ }
29
+ if (parts[0] === "starting-worlds" && parts.length === 2) {
30
+ const d = exports["default"];
31
+ if (kindOf(d) !== "starting-world")
32
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.startingWorld`);
33
+ else if (layout.startingWorlds.has(d.name))
34
+ layout.problems.push(`${at(path)}: starting world "${d.name}" is already declared`);
35
+ else
36
+ layout.startingWorlds.set(d.name, { file: at(path), declaration: d, ...(sourceHash ? { sourceHash } : {}) });
37
+ continue;
38
+ }
39
+ const folder = parts[0];
40
+ if (Object.hasOwn(MOUNT_FOLDERS, folder) && parts.length === 2) {
41
+ const name = parts[1];
42
+ if (!NAME.test(name))
43
+ layout.problems.push(`${at(path)}: mount "${name}" must be lowercase words joined by dashes`);
44
+ const d = exports["default"];
45
+ if (kindOf(d) !== "mount")
46
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.use(…) of a ${MOUNT_FOLDERS[folder]}`);
47
+ else
48
+ layout.mounts.set(name, { name, kind: MOUNT_FOLDERS[folder], file: at(path), mount: d });
49
+ for (const [k, v] of Object.entries(exports))
50
+ if (kindOf(v) === "holds")
51
+ layout.holdLists.push({ file: at(path), name: k, declaration: v });
52
+ continue;
53
+ }
54
+ if (parts[0] === "agents" && parts.length === 2) {
55
+ const name = parts[1];
56
+ if (!NAME.test(name))
57
+ layout.problems.push(`${at(path)}: agent "${name}" must be lowercase words joined by dashes`);
58
+ const d = exports["default"];
59
+ if (kindOf(d) === "agent")
60
+ layout.agents.set(name, { file: at(path), declaration: d });
61
+ else if (kindOf(d) === "declared")
62
+ layout.declared.set(name, { file: at(path), declaration: d });
63
+ else
64
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.agent or a Blueprint.declare`);
65
+ continue;
66
+ }
67
+ if (parts[0] === "sources" && parts.length === 2) {
68
+ const name = parts[1];
69
+ const kinds = Object.entries(exports).filter(([, v]) => typeof v === "function" && v.kind === "source-kind");
70
+ if (kinds.length !== 1)
71
+ layout.problems.push(`${at(path)}: must export exactly one Source.from or Source.timer`);
72
+ else
73
+ layout.sources.set(name, { file: at(path), declaration: kinds[0][1], exportName: kinds[0][0] });
74
+ continue;
75
+ }
76
+ if (parts[0] === "secrets" && parts.length === 1) {
77
+ for (const [k, v] of Object.entries(exports)) {
78
+ if (kindOf(v) === "secret")
79
+ layout.secrets.set(k, { export: k, file: at(path), declaration: v });
80
+ else
81
+ layout.problems.push(`${at(path)}: export "${k}" must be a Blueprint.secret.generated(…) or Blueprint.secret.stored(…)`);
82
+ }
83
+ continue;
84
+ }
85
+ if (parts[0] === "values" && parts.length === 2) {
86
+ for (const [k, v] of Object.entries(exports))
87
+ if (kindOf(v) === "value") {
88
+ layout.values.set(k, v);
89
+ layout.valueFiles.set(k, at(path));
90
+ }
91
+ continue;
92
+ }
93
+ if (parts[0] === "assets" && parts.length === 2) {
94
+ const name = parts[1];
95
+ if (!NAME.test(name))
96
+ layout.problems.push(`${at(path)}: asset "${name}" must be lowercase words joined by dashes`);
97
+ const d = exports["default"];
98
+ if (kindOf(d) !== "asset")
99
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.asset.token(…) or Blueprint.asset.currency(…)`);
100
+ else
101
+ layout.assets.set(name, { file: at(path), asset: d });
102
+ continue;
103
+ }
104
+ if (parts[0] === "stores" && parts.length === 2) {
105
+ const name = parts[1];
106
+ if (!NAME.test(name))
107
+ layout.problems.push(`${at(path)}: store "${name}" must be lowercase words joined by dashes`);
108
+ const d = exports["default"];
109
+ if (kindOf(d) !== "store")
110
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.store.<kind>()`);
111
+ else
112
+ layout.stores.set(name, { name, file: at(path), store: d });
113
+ continue;
114
+ }
115
+ if (parts[0] === "environments" && parts.length === 2) {
116
+ const name = parts[1];
117
+ if (!NAME.test(name))
118
+ layout.problems.push(`${at(path)}: environment "${name}" must be lowercase words joined by dashes`);
119
+ const d = exports["default"];
120
+ if (kindOf(d) !== "environment")
121
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.environment`);
122
+ else
123
+ layout.environments.set(name, { file: at(path), declaration: d });
124
+ continue;
125
+ }
126
+ if (parts[0] !== "things" || parts.length < 3) {
127
+ layout.problems.push(`${at(path)}: not a place the blueprint reads (secrets.ts, values/<file>.ts, assets/<name>.ts, environments/<name>.ts, stores/<name>.ts, things/<thing>/..., or a mount in features/, adapters/ or projections/)`);
128
+ continue;
129
+ }
130
+ const name = parts[1];
131
+ if (!NAME.test(name))
132
+ layout.problems.push(`${at(path)}: thing folder "${name}" must be lowercase words joined by dashes`);
133
+ const t = thingFiles(name);
134
+ const file = parts.slice(2).join("/");
135
+ const each = (want) => Object.entries(exports).filter(([, v]) => kindOf(v) === want);
136
+ if (file === "thing") {
137
+ const found = each("thing");
138
+ if (found.length !== 1)
139
+ layout.problems.push(`${at(path)}: must export exactly one Blueprint.thing`);
140
+ else
141
+ t.thing = found[0][1];
142
+ }
143
+ else if (file === "machine") {
144
+ const found = each("machine");
145
+ if (found.length !== 1)
146
+ layout.problems.push(`${at(path)}: must export exactly one Blueprint.machine`);
147
+ else
148
+ t.machine = found[0][1];
149
+ }
150
+ else if (file === "events") {
151
+ for (const [k, v] of each("event"))
152
+ t.events.set(k, v);
153
+ }
154
+ else if (file === "errors") {
155
+ for (const [k, v] of Object.entries(exports))
156
+ if (isErrorClass(v))
157
+ t.errors.set(k, v);
158
+ }
159
+ else if (file === "roles") {
160
+ for (const [k, v] of each("role"))
161
+ t.roles.set(k, v);
162
+ }
163
+ else if (file === "problem") {
164
+ const d = exports["default"];
165
+ if (kindOf(d) !== "problem")
166
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.problem`);
167
+ else
168
+ t.problem = { file: at(path), declaration: d };
169
+ }
170
+ else if (file === "holds") {
171
+ for (const [k, v] of each("hold"))
172
+ t.holds.set(k, { file: at(path), declaration: v });
173
+ }
174
+ else if (file === "derive") {
175
+ // A derive is read through the reads that name it; the file only holds it.
176
+ }
177
+ else if (file === "invariants") {
178
+ for (const [k, v] of each("invariant"))
179
+ t.invariants.set(k, v);
180
+ }
181
+ else if (parts[2] === "verbs" && parts.length === 4) {
182
+ const verb = parts[3];
183
+ if (!NAME.test(verb))
184
+ layout.problems.push(`${at(path)}: verb "${verb}" must be lowercase words joined by dashes`);
185
+ const d = exports["default"];
186
+ const kind = kindOf(d);
187
+ if (kind !== "operation" && kind !== "read")
188
+ layout.problems.push(`${at(path)}: the default export must be a Blueprint.operation or a Blueprint.read`);
189
+ else
190
+ t.verbs.set(verb, { file: at(path), declaration: d });
191
+ }
192
+ else {
193
+ layout.problems.push(`${at(path)}: not a file a thing has (thing, machine, events, errors, roles, invariants, verbs/<verb>)`);
194
+ }
195
+ }
196
+ for (const t of layout.things.values())
197
+ if (!t.thing)
198
+ layout.problems.push(`${t.base}: has no thing.ts`);
199
+ return layout;
200
+ };
201
+ /** `change-role` becomes `changeRole`; `allowance` becomes `Allowance` with `pascal`. */
202
+ export const camel = (name) => name.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
203
+ export const pascal = (name) => camel(name).replace(/^[a-z]/, (c) => c.toUpperCase());
@@ -0,0 +1,3 @@
1
+ import type { LockValue } from "#modules/lock/types.js";
2
+ import type { FieldsModel } from "../types.ts";
3
+ export declare const lockFields: (fields: FieldsModel) => Readonly<Record<string, LockValue>>;
@@ -0,0 +1,17 @@
1
+ const valueOf = (meta) => {
2
+ switch (meta.type) {
3
+ case "uint": return { value: "uint", bits: meta.bits };
4
+ case "id": return { value: "id", prefix: meta.prefix };
5
+ case "text": return { value: "text", max: meta.max };
6
+ case "account": return { value: "account" };
7
+ case "money": return { value: "money", bits: meta.bits, signed: meta.signed, asset: meta.asset };
8
+ // Their own kinds, though both travel as ISO text: a field moving between an instant, a day and text changes what clients parse.
9
+ case "instant": return { value: "instant" };
10
+ case "day": return { value: "day" };
11
+ // Its own kind with its places: a field moving between a decimal and text, or changing its places, changes what clients parse.
12
+ case "decimal": return { value: "decimal", places: meta.places };
13
+ case "schema": return { value: "schema" };
14
+ }
15
+ };
16
+ // A field turning optional changes what clients parse too; a required field is written as it always was, so no lock changes.
17
+ export const lockFields = (fields) => Object.fromEntries(Object.entries(fields).map(([k, f]) => [k, f.optional === true ? { ...valueOf(f.value), optional: true } : valueOf(f.value)]));
@@ -0,0 +1,17 @@
1
+ import type { Asset, Thing, ValueMeta as Declared } from "orthograph";
2
+ import type { ValueMeta } from "../types.ts";
3
+ /** The thing a declaration belongs to: its fields, its operations, and the events and errors under its folder. */
4
+ export interface Owner {
5
+ readonly name: string;
6
+ readonly thing: Thing;
7
+ }
8
+ export interface Names {
9
+ /** Each asset by the file name it has: `usdc` for `blueprint/assets/usdc.ts`. */
10
+ readonly assets: ReadonlyMap<Asset, string>;
11
+ readonly things: ReadonlyMap<Thing, string>;
12
+ }
13
+ /**
14
+ * A value's meta as the model records it. A money value's asset is a live reference, so it is resolved to a name here:
15
+ * the asset file's, or the field of the owning thing. A reference that can't be resolved is a problem naming `where`.
16
+ */
17
+ export declare const metaOf: (meta: Declared, where: string, owner: Owner | undefined, names: Names, problems: Array<string>) => ValueMeta;
@@ -0,0 +1,49 @@
1
+ import { pascal } from "./layout.js";
2
+ const fieldOf = (owner, holder, where, problems) => {
3
+ const mine = Object.entries(owner.thing.fields);
4
+ const said = `${where}: money.of`;
5
+ let key;
6
+ if (holder.kind === "field") {
7
+ if (holder.of === owner.thing)
8
+ key = holder.key;
9
+ else
10
+ problems.push(`${said} counts the asset of a field of another thing (${holder.key}): an amount counts the asset of a field of the same thing, ${owner.name}, so a money field reused from another thing can't be used here`);
11
+ }
12
+ else {
13
+ const keys = mine.filter(([, f]) => f.value === holder).map(([k]) => k);
14
+ if (keys.length === 0)
15
+ problems.push(`${said} counts the asset of a value that is in none of ${owner.name}'s fields: take it from one, as ${pascal(owner.name)}.fields.<field>, or put the value in one`);
16
+ else if (keys.length > 1)
17
+ problems.push(`${said} counts the asset of a value that is in ${keys.length} of ${owner.name}'s fields (${keys.join(", ")}): name one of them with ${pascal(owner.name)}.fields.<field>`);
18
+ else
19
+ key = keys[0];
20
+ }
21
+ if (key !== undefined && owner.thing.fields[key]?.value.meta.type !== "account") {
22
+ problems.push(`${said} counts the asset held by ${owner.name}.${key}, which holds no asset: only an account() field does, read as a token contract`);
23
+ return undefined;
24
+ }
25
+ return key;
26
+ };
27
+ /**
28
+ * A value's meta as the model records it. A money value's asset is a live reference, so it is resolved to a name here:
29
+ * the asset file's, or the field of the owning thing. A reference that can't be resolved is a problem naming `where`.
30
+ */
31
+ export const metaOf = (meta, where, owner, names, problems) => {
32
+ if (meta.type !== "money")
33
+ return meta;
34
+ const { bits, signed, asset } = meta;
35
+ const resolved = (() => {
36
+ if (asset.kind === "fixed") {
37
+ const name = names.assets.get(asset.asset);
38
+ if (name === undefined)
39
+ problems.push(`${where}: money of an asset that is not the default export of a blueprint/assets/<name>.ts: declare it there`);
40
+ return asset.asset.of === "token" ? { token: name ?? "?" } : { currency: name ?? "?" };
41
+ }
42
+ if (owner === undefined) {
43
+ problems.push(`${where}: money.of is written outside any thing: an amount counts the asset of a field of the thing whose fields, operations, events or errors it is in`);
44
+ return { field: "?" };
45
+ }
46
+ return { field: fieldOf(owner, asset.of, where, problems) ?? "?" };
47
+ })();
48
+ return { type: "money", bits, signed, asset: resolved };
49
+ };
@@ -0,0 +1,7 @@
1
+ import type { Value } from "orthograph";
2
+ export declare const isNullable: (value: Value) => boolean;
3
+ /**
4
+ * A plain schema value (or a `Blueprint.list`) that travels as a list or an object, null aside: "list" or "object". Undefined for any other,
5
+ * a value that travels as text (`Schema.fromJsonString`) and an any-thing reference (which says so in its format) among them.
6
+ */
7
+ export declare const jsonShape: (value: Value) => "list" | "object" | undefined;
@@ -0,0 +1,21 @@
1
+ // What a value's encoded form is: a union with null (so the column that holds it may be NULL too; an optional value says so on its own),
2
+ // or a list or an object (so a command line takes it as JSON).
3
+ import { SchemaAST } from "effect";
4
+ const encoded = (value) => SchemaAST.toEncoded(value.schema.ast);
5
+ export const isNullable = (value) => {
6
+ if (value.meta.type !== "schema")
7
+ return false;
8
+ const ast = encoded(value);
9
+ return ast._tag === "Union" && ast.types.some((m) => m._tag === "Null");
10
+ };
11
+ /**
12
+ * A plain schema value (or a `Blueprint.list`) that travels as a list or an object, null aside: "list" or "object". Undefined for any other,
13
+ * a value that travels as text (`Schema.fromJsonString`) and an any-thing reference (which says so in its format) among them.
14
+ */
15
+ export const jsonShape = (value) => {
16
+ if (value.meta.type !== "schema" || (value.meta.format !== undefined && value.meta.format !== "list"))
17
+ return undefined;
18
+ const ast = encoded(value);
19
+ const kinds = new Set((ast._tag === "Union" ? ast.types : [ast]).filter((m) => m._tag !== "Null" && m._tag !== "Undefined").map((m) => m._tag));
20
+ return kinds.size !== 1 ? undefined : kinds.has("Arrays") ? "list" : kinds.has("Objects") ? "object" : undefined;
21
+ };
@@ -0,0 +1,27 @@
1
+ import type { Operation, Read, Role } from "orthograph";
2
+ import type { FieldsModel } from "../types.ts";
3
+ /** The entries the generated contract has of its own: a verb named like one would be declared twice. */
4
+ export declare const RESERVED_ENTRIES: ReadonlyArray<string>;
5
+ export interface ChainOperation {
6
+ readonly file: string;
7
+ readonly name: string;
8
+ readonly thing: string;
9
+ readonly op: Operation;
10
+ readonly roleNames: ReadonlyMap<Role, string>;
11
+ readonly payload: FieldsModel;
12
+ readonly answer: FieldsModel;
13
+ /** The fields of the event its move emits. */
14
+ readonly event: FieldsModel | undefined;
15
+ readonly emits: string;
16
+ /** The payload or answer field that carries the new row's id, for a creating move. */
17
+ readonly mints: string | undefined;
18
+ }
19
+ export declare const chainOperationProblems: ({ file, name, thing, op, roleNames, payload, answer, event, emits, mints }: ChainOperation) => ReadonlyArray<string>;
20
+ /** A read of a chain-stored thing is a public `view`: only `Blueprint.anyone` may be allowed, and only a read by id is one. */
21
+ export declare const chainReadProblems: (file: string, name: string, read: Read) => ReadonlyArray<string>;
22
+ /** Verbs of one chain store whose entry name collides: with an entry the generated contract has, or with another operation's signed entry. */
23
+ export declare const collisionProblems: (verbs: ReadonlyArray<{
24
+ readonly file: string;
25
+ readonly name: string;
26
+ readonly identifier: string;
27
+ }>) => ReadonlyArray<string>;