@spellkave/world-author 0.0.0-bootstrap

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 (57) hide show
  1. package/LICENSE +89 -0
  2. package/README.md +62 -0
  3. package/package.json +57 -0
  4. package/src/applyset-diff-types.ts +158 -0
  5. package/src/applyset.ts +82 -0
  6. package/src/authoring-harness.ts +487 -0
  7. package/src/browser.ts +114 -0
  8. package/src/compose.ts +699 -0
  9. package/src/corpus.generated.ts +279 -0
  10. package/src/corpus.ts +1180 -0
  11. package/src/damage.ts +75 -0
  12. package/src/diagnostics.ts +59 -0
  13. package/src/diff-manifest-core.ts +393 -0
  14. package/src/diff-manifest.ts +5 -0
  15. package/src/docs-index.ts +105 -0
  16. package/src/dsl.ts +10 -0
  17. package/src/evaluate.ts +100 -0
  18. package/src/explain-core.ts +219 -0
  19. package/src/explain.ts +43 -0
  20. package/src/filter-manifest-core.ts +122 -0
  21. package/src/filter-manifest.ts +41 -0
  22. package/src/grants-core.ts +38 -0
  23. package/src/grants.ts +10 -0
  24. package/src/included-manifest-metadata-core.ts +63 -0
  25. package/src/included-manifest-metadata.ts +21 -0
  26. package/src/index.ts +30 -0
  27. package/src/inspect.ts +35 -0
  28. package/src/inspection.ts +239 -0
  29. package/src/lint.ts +540 -0
  30. package/src/manifest-text.ts +21 -0
  31. package/src/manifest.ts +403 -0
  32. package/src/metadata.ts +137 -0
  33. package/src/npc-dm-authority-projection.ts +95 -0
  34. package/src/npc-dm-documents.ts +339 -0
  35. package/src/npc-dm-presentation.ts +17 -0
  36. package/src/npc-dm-srd.ts +51 -0
  37. package/src/npc-dm-types.ts +723 -0
  38. package/src/npc-kinds.ts +404 -0
  39. package/src/npc-quests.ts +319 -0
  40. package/src/observation-tokens.browser.ts +31 -0
  41. package/src/observation-tokens.ts +27 -0
  42. package/src/owner-instance-read.ts +765 -0
  43. package/src/patch.ts +47 -0
  44. package/src/ref-tags.ts +149 -0
  45. package/src/reference.ts +48 -0
  46. package/src/row-identity.ts +105 -0
  47. package/src/row-kinds-data.ts +35 -0
  48. package/src/row-kinds.ts +93 -0
  49. package/src/scenario-guidance.ts +180 -0
  50. package/src/scoped-instance-read.ts +187 -0
  51. package/src/service-only-audience.ts +14 -0
  52. package/src/session-predicates.ts +6 -0
  53. package/src/spell.ts +119 -0
  54. package/src/tag-prefix-families.ts +143 -0
  55. package/src/tooling.browser.ts +397 -0
  56. package/src/tooling.ts +38 -0
  57. package/src/validate.ts +255 -0
@@ -0,0 +1,100 @@
1
+ import { evaluate_dsl_expression_json } from "@spellkave/validate-wasm";
2
+
3
+ export type DslExpressionKind = "predicate" | "int" | "text";
4
+
5
+ export type EvaluationValue =
6
+ | { type: "Bool"; value: boolean }
7
+ | { type: "Int"; value: number }
8
+ | { type: "Text"; value: string };
9
+
10
+ export interface StochasticDraw {
11
+ source_id: string;
12
+ draw_index: number;
13
+ value: number;
14
+ }
15
+
16
+ export interface TraceScopeEntry {
17
+ subject: SubjectRef;
18
+ slot: string;
19
+ }
20
+
21
+ /** JSON shape emitted by Rust's externally-tagged `SubjectRef` enum. */
22
+ export type SubjectRef =
23
+ | "Actor"
24
+ | "ActorWieldedItem"
25
+ | "Target"
26
+ | "Location"
27
+ | "Session"
28
+ | "World"
29
+ | { Binding: { name: string } }
30
+ | { Event: "Kind" | { Detail: { field_id: string } } }
31
+ | { ActorEquippedItem: { slot_id: string } };
32
+
33
+ export interface RollTableSelection {
34
+ entry_id: string;
35
+ kept: boolean;
36
+ }
37
+
38
+ export interface TraceEntry {
39
+ node_id: number;
40
+ operator: string | null;
41
+ depth: number;
42
+ span: { start: number; end: number } | null;
43
+ scope: TraceScopeEntry[];
44
+ value: EvaluationValue | null;
45
+ error: string | null;
46
+ table_entries: RollTableSelection[] | null;
47
+ }
48
+
49
+ export interface EvaluationRejection {
50
+ kind: string;
51
+ message: string;
52
+ subject?: string;
53
+ field_id?: string;
54
+ pool?: string;
55
+ draw_number?: number;
56
+ bound?: string;
57
+ budget?: number;
58
+ macro_id?: string;
59
+ operator?: string;
60
+ diagnostics?: Array<{
61
+ code: string;
62
+ message: string;
63
+ start: number;
64
+ end: number;
65
+ }>;
66
+ }
67
+
68
+ export interface EvaluationResult {
69
+ ok: boolean;
70
+ value?: EvaluationValue;
71
+ draws?: StochasticDraw[];
72
+ trace?: TraceEntry[];
73
+ error?: string;
74
+ rejection?: EvaluationRejection;
75
+ }
76
+
77
+ export interface EvaluateDslExpressionOptions {
78
+ expression: string;
79
+ kind: DslExpressionKind;
80
+ facts?: Record<string, unknown> | string;
81
+ dice?: readonly number[] | string;
82
+ manifestToml?: string | null;
83
+ }
84
+
85
+ export { evaluate_dsl_expression_json };
86
+
87
+ export function evaluateDslExpression(options: EvaluateDslExpressionOptions): EvaluationResult {
88
+ const factsJson =
89
+ typeof options.facts === "string" ? options.facts : JSON.stringify(options.facts ?? {});
90
+ const diceJson =
91
+ typeof options.dice === "string" ? options.dice : JSON.stringify(options.dice ?? []);
92
+ const raw = evaluate_dsl_expression_json(
93
+ options.expression,
94
+ options.kind,
95
+ factsJson,
96
+ diceJson,
97
+ options.manifestToml ?? undefined,
98
+ );
99
+ return JSON.parse(raw) as EvaluationResult;
100
+ }
@@ -0,0 +1,219 @@
1
+ export type ExplainKind = "role" | "binding" | "capability" | "extent-kind" | "place";
2
+ export type VocabularyRow = Record<string, unknown>;
3
+ export type VocabularyLookup = (kind: string) => VocabularyRow[];
4
+ export const EXPLAIN_ROW_KINDS = [
5
+ "role_definitions",
6
+ "role_aggregations",
7
+ "role_bindings",
8
+ "capability_definitions",
9
+ "extent_kind_definitions",
10
+ "affordance_templates",
11
+ "action_definitions",
12
+ "place_graphs",
13
+ "place_nodes",
14
+ "place_seals",
15
+ ] as const;
16
+
17
+ export interface ExplainDisclosure {
18
+ connectedIdentity?: string;
19
+ readsAsPublisher?: boolean;
20
+ /** Only set for the server-filtered grant/v1 binding view. */
21
+ bindingSubjectsAdmitted?: boolean;
22
+ identitiesForPresence?: (body: string) => string[];
23
+ }
24
+
25
+ export type ExplainAnswer =
26
+ | { ok: false; kind: ExplainKind; id: string; source: "files" | "live"; nearest_ids: string[] }
27
+ | {
28
+ ok: true;
29
+ kind: ExplainKind;
30
+ id: string;
31
+ source: "files" | "live";
32
+ definition: VocabularyRow;
33
+ capabilities?: VocabularyRow[];
34
+ capability_ids?: string[];
35
+ contributions?: VocabularyRow[];
36
+ bound_to?: VocabularyRow[];
37
+ state?: "planned" | "inert" | "realised";
38
+ derived_elevation_class?: string;
39
+ identities?: string[];
40
+ roles?: VocabularyRow[];
41
+ affordance_templates?: VocabularyRow[];
42
+ action_definitions?: VocabularyRow[];
43
+ seal_state?: "open" | "sealed" | "seal_unknown" | "server decides";
44
+ sealing_graph?: string;
45
+ };
46
+
47
+ /** Structured projection only: descriptions and authority facts stay authored data. */
48
+ export function explainVocabularyWith(
49
+ kind: ExplainKind,
50
+ id: string,
51
+ source: "files" | "live",
52
+ lookup: VocabularyLookup,
53
+ merge: (roleId: string, lookup: VocabularyLookup) => string[],
54
+ disclosure: ExplainDisclosure = {},
55
+ ): ExplainAnswer {
56
+ const [rowKind, field] =
57
+ kind === "role"
58
+ ? ["role_definitions", "role_id"]
59
+ : kind === "binding"
60
+ ? ["role_bindings", "binding_id"]
61
+ : kind === "capability"
62
+ ? ["capability_definitions", "capability_id"]
63
+ : kind === "place"
64
+ ? ["place_graphs", "place_graph_id"]
65
+ : ["extent_kind_definitions", "kind_id"];
66
+ const rows = lookup(rowKind);
67
+ const definition =
68
+ rows.find((row) => row[field] === id) ??
69
+ (kind === "place" ? lookup("place_nodes").find((row) => row.node_id === id) : undefined);
70
+ if (definition === undefined) {
71
+ const prefixLength = (candidate: string) => {
72
+ let n = 0;
73
+ while (n < id.length && n < candidate.length && candidate[n] === id[n]) n++;
74
+ return n;
75
+ };
76
+ const nearest_ids = rows
77
+ .map((row) => String(row[field]))
78
+ .sort((a, b) => prefixLength(b) - prefixLength(a) || a.localeCompare(b))
79
+ .slice(0, 5);
80
+ return { ok: false, kind, id, source, nearest_ids };
81
+ }
82
+ if (kind === "place") {
83
+ if (source === "live" && !disclosure.readsAsPublisher) {
84
+ return { ok: true, kind, id, source, definition, seal_state: "server decides" };
85
+ }
86
+ const graphs = lookup("place_graphs");
87
+ const seals = lookup("place_seals");
88
+ let graph = String(definition.place_graph_id);
89
+ let sealing_graph: string | undefined;
90
+ const seen = new Set<string>();
91
+ for (let depth = 0; depth < 64; depth++) {
92
+ if (graph === "")
93
+ return {
94
+ ok: true,
95
+ kind,
96
+ id,
97
+ source,
98
+ definition,
99
+ seal_state: sealing_graph ? "sealed" : "open",
100
+ ...(sealing_graph ? { sealing_graph } : {}),
101
+ };
102
+ const row = graphs.find((r) => r.place_graph_id === graph);
103
+ if (row === undefined || seen.has(graph))
104
+ return {
105
+ ok: true,
106
+ kind,
107
+ id,
108
+ source,
109
+ definition,
110
+ seal_state: "seal_unknown",
111
+ sealing_graph: graph,
112
+ };
113
+ seen.add(graph);
114
+ if (
115
+ sealing_graph === undefined &&
116
+ seals.some((r) => r.place_graph_id === graph && r.broken !== true)
117
+ )
118
+ sealing_graph = graph;
119
+ graph = String(row.parent_graph_id ?? "");
120
+ }
121
+ return {
122
+ ok: true,
123
+ kind,
124
+ id,
125
+ source,
126
+ definition,
127
+ seal_state: "seal_unknown",
128
+ sealing_graph: graph,
129
+ };
130
+ }
131
+ const discloseIdentity = (identity: string) =>
132
+ source === "files" ||
133
+ disclosure.readsAsPublisher ||
134
+ identity.replace(/^0x/, "") === disclosure.connectedIdentity?.replace(/^0x/, "")
135
+ ? identity
136
+ : "another identity";
137
+ const discloseBinding = (row: VocabularyRow): VocabularyRow => {
138
+ const subject = String(row.subject ?? "");
139
+ return /^(?:0x)?[0-9a-f]{64}$/i.test(subject)
140
+ ? {
141
+ ...row,
142
+ subject: disclosure.bindingSubjectsAdmitted ? subject : discloseIdentity(subject),
143
+ }
144
+ : row;
145
+ };
146
+ if (kind === "binding") {
147
+ const capability_ids = merge(String(definition.role_id), lookup);
148
+ const presence = String(definition.presence_entity_id ?? "0");
149
+ const realised = source === "live" && presence !== "0" && presence !== "";
150
+ const subject = String(definition.subject ?? "");
151
+ const identities = realised
152
+ ? (disclosure.identitiesForPresence?.(presence) ??
153
+ (/^(?:0x)?[0-9a-f]{64}$/i.test(subject) ? [subject] : ["another identity"]))
154
+ : [];
155
+ return {
156
+ ok: true,
157
+ kind,
158
+ id,
159
+ source,
160
+ definition: discloseBinding(definition),
161
+ state: source === "files" ? "planned" : realised ? "realised" : "inert",
162
+ capability_ids,
163
+ derived_elevation_class: capability_ids.includes("frontier.read_internal")
164
+ ? "internal"
165
+ : "hidden",
166
+ identities: [
167
+ ...new Set(
168
+ identities.map((identity) =>
169
+ disclosure.bindingSubjectsAdmitted && identity === subject
170
+ ? identity
171
+ : discloseIdentity(identity),
172
+ ),
173
+ ),
174
+ ].sort(),
175
+ };
176
+ }
177
+ if (kind === "role") {
178
+ const capability_ids = merge(id, lookup);
179
+ return {
180
+ ok: true,
181
+ kind,
182
+ id,
183
+ source,
184
+ definition,
185
+ capability_ids,
186
+ bound_to: lookup("role_bindings")
187
+ .filter((row) => row.role_id === id)
188
+ .sort((a, b) => String(a.binding_id).localeCompare(String(b.binding_id)))
189
+ .map(discloseBinding),
190
+ capabilities: capability_ids.flatMap((capId) =>
191
+ lookup("capability_definitions").filter((row) => row.capability_id === capId),
192
+ ),
193
+ contributions: [
194
+ definition,
195
+ ...lookup("role_aggregations")
196
+ .filter((row) => row.role_id === id)
197
+ .sort((a, b) => String(a.bundle_id).localeCompare(String(b.bundle_id))),
198
+ ],
199
+ };
200
+ }
201
+ if (kind === "capability")
202
+ return {
203
+ ok: true,
204
+ kind,
205
+ id,
206
+ source,
207
+ definition,
208
+ roles: lookup("role_definitions")
209
+ .filter((row) => merge(String(row.role_id), lookup).includes(id))
210
+ .sort((a, b) => String(a.role_id).localeCompare(String(b.role_id))),
211
+ affordance_templates: lookup("affordance_templates")
212
+ .filter((row) => row.capability_id === id)
213
+ .map((row) => ({ ...row, privileged: id.startsWith("frontier.") })),
214
+ action_definitions: lookup("action_definitions").filter(
215
+ (row) => row.required_capability === id,
216
+ ),
217
+ };
218
+ return { ok: true, kind, id, source, definition };
219
+ }
package/src/explain.ts ADDED
@@ -0,0 +1,43 @@
1
+ import {
2
+ core_bootstrap_manifest,
3
+ core_extent_kinds_json,
4
+ merged_role_capability_ids_json,
5
+ } from "@spellkave/validate-wasm";
6
+ import {
7
+ type ExplainDisclosure,
8
+ type ExplainKind,
9
+ explainVocabularyWith,
10
+ type VocabularyLookup,
11
+ } from "./explain-core";
12
+ import { inspectManifest } from "./inspect";
13
+
14
+ export * from "./explain-core";
15
+
16
+ import type { VocabularyRow } from "./explain-core";
17
+
18
+ /** Files consult the actual bootstrap; live callers must supply live rows alone. */
19
+ export function bootstrapVocabularyRows(kind: string): VocabularyRow[] {
20
+ if (kind === "extent_kind_definitions") return JSON.parse(core_extent_kinds_json());
21
+ const value = inspectManifest(core_bootstrap_manifest()).manifest[kind];
22
+ return Array.isArray(value) ? (value as VocabularyRow[]) : [];
23
+ }
24
+
25
+ export function mergedRoleCapabilityIds(roleId: string, lookup: VocabularyLookup): string[] {
26
+ return JSON.parse(
27
+ merged_role_capability_ids_json(
28
+ roleId,
29
+ JSON.stringify(lookup("role_definitions")),
30
+ JSON.stringify(lookup("role_aggregations")),
31
+ ),
32
+ );
33
+ }
34
+
35
+ export function explainVocabulary(
36
+ kind: ExplainKind,
37
+ id: string,
38
+ source: "files" | "live",
39
+ lookup: VocabularyLookup,
40
+ disclosure?: ExplainDisclosure,
41
+ ) {
42
+ return explainVocabularyWith(kind, id, source, lookup, mergedRoleCapabilityIds, disclosure);
43
+ }
@@ -0,0 +1,122 @@
1
+ // Browser-safe actor-export selection. Filesystem output remains in
2
+ // filter-manifest.ts; this module only transforms manifest data.
3
+
4
+ export interface SchemaManifest {
5
+ bundles?: Array<{ bundle_id: string; [key: string]: unknown }>;
6
+ entities?: Array<{
7
+ entity_key: string;
8
+ bundle_id?: string;
9
+ owned_by_entity_key?: string;
10
+ [key: string]: unknown;
11
+ }>;
12
+ actors?: Array<{ entity_key: string; [key: string]: unknown }>;
13
+ actor_capabilities?: Array<{ actor_entity_key: string; [key: string]: unknown }>;
14
+ actor_spatial_locations?: Array<{ entity_key: string; [key: string]: unknown }>;
15
+ actor_equipment_slots?: Array<{
16
+ actor_entity_key: string;
17
+ item_key: string;
18
+ [key: string]: unknown;
19
+ }>;
20
+ entity_containment?: Array<{
21
+ entity_key: string;
22
+ container_entity_key: string;
23
+ [key: string]: unknown;
24
+ }>;
25
+ entity_tag?: Array<{ entity_key: string; [key: string]: unknown }>;
26
+ object_state?: Array<{ entity_key: string; expires_at_tick?: number; [key: string]: unknown }>;
27
+ [key: string]: unknown;
28
+ }
29
+
30
+ export function filterManifestForActor(src: SchemaManifest, actorKey: string): SchemaManifest {
31
+ const entities = src.entities ?? [];
32
+ const actors = src.actors ?? [];
33
+ if (
34
+ !entities.some((row) => row.entity_key === actorKey) &&
35
+ !actors.some((row) => row.entity_key === actorKey)
36
+ ) {
37
+ throw new Error(`manifest export: actor '${actorKey}' not found in exported bundle`);
38
+ }
39
+ const keep = collectActorEntityKeys(src, actorKey);
40
+ const kept = (key: string | undefined) => key !== undefined && keep.has(key);
41
+ const schemaVersionKey = "schema_version";
42
+ const out: SchemaManifest = { metadata: src.metadata };
43
+ out[schemaVersionKey] = src[schemaVersionKey];
44
+ const bundleIds = new Set(
45
+ entities.filter((row) => keep.has(row.entity_key)).map((row) => row.bundle_id ?? ""),
46
+ );
47
+ out.bundles = (src.bundles ?? []).filter(
48
+ (row) => bundleIds.size === 0 || bundleIds.has(row.bundle_id),
49
+ );
50
+ out.entities = entities.filter((row) => keep.has(row.entity_key));
51
+ out.actors = actors.filter((row) => keep.has(row.entity_key));
52
+ out.actor_capabilities = (src.actor_capabilities ?? []).filter((row) =>
53
+ kept(row.actor_entity_key),
54
+ );
55
+ out.actor_spatial_locations = (src.actor_spatial_locations ?? []).filter((row) =>
56
+ keep.has(row.entity_key),
57
+ );
58
+ out.actor_equipment_slots = (src.actor_equipment_slots ?? []).filter((row) =>
59
+ kept(row.actor_entity_key),
60
+ );
61
+ out.entity_containment = (src.entity_containment ?? []).filter(
62
+ (row) => keep.has(row.container_entity_key) && keep.has(row.entity_key),
63
+ );
64
+ out.entity_tag = (src.entity_tag ?? []).filter((row) => keep.has(row.entity_key));
65
+ out.object_state = (src.object_state ?? []).filter((row) => keep.has(row.entity_key));
66
+ return out;
67
+ }
68
+
69
+ /** Instance row kinds `filterManifestForActor` keeps (not `bundles`). */
70
+ export const INSTANCE_ROW_KINDS = [
71
+ "entities",
72
+ "actors",
73
+ "actor_spatial_locations",
74
+ "actor_capabilities",
75
+ "actor_equipment_slots",
76
+ "entity_containment",
77
+ "entity_tag",
78
+ "object_state",
79
+ ] as const;
80
+
81
+ export type InstanceRowKind = (typeof INSTANCE_ROW_KINDS)[number];
82
+
83
+ export function filteredRowCount(manifest: SchemaManifest): number {
84
+ let n = manifest.bundles?.length ?? 0;
85
+ for (const kind of INSTANCE_ROW_KINDS) {
86
+ const rows = manifest[kind];
87
+ n += Array.isArray(rows) ? rows.length : 0;
88
+ }
89
+ return n;
90
+ }
91
+
92
+ function collectActorEntityKeys(src: SchemaManifest, actorKey: string): Set<string> {
93
+ const keep = new Set<string>([actorKey]);
94
+ let grew = true;
95
+ while (grew) {
96
+ grew = false;
97
+ for (const row of src.entity_containment ?? []) {
98
+ if (keep.has(row.container_entity_key) && !keep.has(row.entity_key)) {
99
+ keep.add(row.entity_key);
100
+ grew = true;
101
+ }
102
+ }
103
+ for (const row of src.entities ?? []) {
104
+ if (
105
+ row.owned_by_entity_key !== undefined &&
106
+ row.owned_by_entity_key !== "" &&
107
+ keep.has(row.owned_by_entity_key) &&
108
+ !keep.has(row.entity_key)
109
+ ) {
110
+ keep.add(row.entity_key);
111
+ grew = true;
112
+ }
113
+ }
114
+ for (const row of src.actor_equipment_slots ?? []) {
115
+ if (keep.has(row.actor_entity_key) && !keep.has(row.item_key)) {
116
+ keep.add(row.item_key);
117
+ grew = true;
118
+ }
119
+ }
120
+ }
121
+ return keep;
122
+ }
@@ -0,0 +1,41 @@
1
+ import { mkdirSync, writeFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { filterManifestForActor, type SchemaManifest } from "./filter-manifest-core";
4
+ import { renderSchemaManifest } from "./manifest";
5
+
6
+ export type { SchemaManifest } from "./filter-manifest-core";
7
+ export {
8
+ filteredRowCount,
9
+ filterManifestForActor,
10
+ INSTANCE_ROW_KINDS,
11
+ type InstanceRowKind,
12
+ } from "./filter-manifest-core";
13
+
14
+ // One-actor view of a reconstructed schema-shaped Manifest (sk-0m4.43).
15
+ //
16
+ // Port of the closed PR's Rust `client/src/cli/manifest_filter.rs` (9453e2609)
17
+ // 1:1: keep-set closure over containment / owned_by / equipment; keep the
18
+ // bundles header plus instance rows that hang off the named actor. Catalog
19
+ // rows stay behind. Operates on snake_case schema JSON (the shape
20
+ // `r_export_manifest` writes), never camelCase authoring builders.
21
+
22
+ /** Recorder-only: zero `object_state.expires_at_tick` so a ±1 tick cannot jitter a story fixture. */
23
+ export function zeroObjectStateExpiry(manifest: SchemaManifest): SchemaManifest {
24
+ return {
25
+ ...manifest,
26
+ object_state: (manifest.object_state ?? []).map((row) => ({ ...row, expires_at_tick: 0 })),
27
+ };
28
+ }
29
+
30
+ /** Filter + zero expiry + wasm render. Used by the story recorder and tests. */
31
+ export function prepareActorExportToml(manifest: SchemaManifest, actorKey: string): string {
32
+ return renderSchemaManifest(zeroObjectStateExpiry(filterManifestForActor(manifest, actorKey)));
33
+ }
34
+
35
+ /** Write `toml` to `join(outDir, fileName)`. Caller chooses the file name. */
36
+ export function writeActorExport(outDir: string, toml: string, fileName: string): string {
37
+ mkdirSync(outDir, { recursive: true });
38
+ const dest = join(outDir, fileName);
39
+ writeFileSync(dest, toml.endsWith("\n") ? toml : `${toml}\n`);
40
+ return dest;
41
+ }
@@ -0,0 +1,38 @@
1
+ import type { compose_grant_json, grant_delta_json } from "@spellkave/authoring-wasm";
2
+
3
+ export interface GrantRow {
4
+ binding_id: string;
5
+ bundle_id: string;
6
+ subject: string;
7
+ role_id: string;
8
+ extent_kind_ref: string;
9
+ extent_ref: string;
10
+ expires_at_tick: string | null;
11
+ description: string;
12
+ handover_note?: string | null;
13
+ }
14
+ export interface GrantInput {
15
+ role: string;
16
+ subject: string;
17
+ extent_kind: string;
18
+ extent: string;
19
+ bundle?: string;
20
+ expires_at_tick?: string;
21
+ description?: string;
22
+ }
23
+ export function composeGrantWith(
24
+ compose: typeof compose_grant_json,
25
+ input: GrantInput,
26
+ identity: string,
27
+ existing: GrantRow[],
28
+ ): GrantRow {
29
+ return JSON.parse(compose(JSON.stringify({ input, identity, existing }))) as GrantRow;
30
+ }
31
+ export function grantDeltaWith(
32
+ render: typeof grant_delta_json,
33
+ rows: GrantRow[],
34
+ revoke: string[] = [],
35
+ instance?: string,
36
+ ): string {
37
+ return render(JSON.stringify({ rows, revoke, instance: instance ?? null }));
38
+ }
package/src/grants.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { compose_grant_json, grant_delta_json } from "@spellkave/authoring-wasm";
2
+ import { composeGrantWith, grantDeltaWith } from "./grants-core";
3
+
4
+ export type { GrantInput, GrantRow } from "./grants-core";
5
+ export const composeGrant = (
6
+ ...args: Parameters<typeof composeGrantWith> extends [unknown, ...infer T] ? T : never
7
+ ) => composeGrantWith(compose_grant_json, ...args);
8
+ export const grantDelta = (
9
+ ...args: Parameters<typeof grantDeltaWith> extends [unknown, ...infer T] ? T : never
10
+ ) => grantDeltaWith(grant_delta_json, ...args);
@@ -0,0 +1,63 @@
1
+ export interface IncludedManifestMetadata {
2
+ /** The exact JSON-safe `[metadata]` tree authored by this include file. */
3
+ metadata: Record<string, unknown>;
4
+ /** The same fragment with its root `[metadata]` tree omitted for row flattening. */
5
+ rowsToml: string;
6
+ }
7
+
8
+ export interface RenderManifestIncludeOptions {
9
+ /** Keep an authored `[metadata]` table in the include. Defaults to false. */
10
+ retainMetadata?: boolean;
11
+ }
12
+
13
+ /**
14
+ * Turn a complete renderer-authored workspace manifest into an include. The
15
+ * Rust formatter removes root-owned bundle/include fields without exposing
16
+ * native gameplay integers to JavaScript.
17
+ */
18
+ export function renderManifestIncludeWith(
19
+ render: (source: string, retainMetadata: boolean) => string,
20
+ rendered: string,
21
+ options: RenderManifestIncludeOptions = {},
22
+ ): string {
23
+ try {
24
+ return render(rendered, options.retainMetadata === true);
25
+ } catch (error) {
26
+ const message = error instanceof Error ? error.message : String(error);
27
+ throw new Error(`manifest include render failed: ${message}`);
28
+ }
29
+ }
30
+
31
+ function recordOf(value: unknown): Record<string, unknown> {
32
+ return value !== null && typeof value === "object" && !Array.isArray(value)
33
+ ? (value as Record<string, unknown>)
34
+ : {};
35
+ }
36
+
37
+ /**
38
+ * Parse and project one direct include in the Rust formatter. Gameplay values
39
+ * remain native TOML values all the way through row rendering, so legal i64
40
+ * and u64 rows never cross JavaScript's lossy number representation.
41
+ * Metadata-free fragments retain their original bytes; metadata-bearing ones
42
+ * return canonical rows with only their local `[metadata]` removed.
43
+ */
44
+ export function splitIncludedManifestMetadataWith(
45
+ split: (source: string) => string,
46
+ fragmentToml: string,
47
+ ): IncludedManifestMetadata {
48
+ let envelope: unknown;
49
+ try {
50
+ envelope = JSON.parse(split(fragmentToml));
51
+ } catch (error) {
52
+ const message = error instanceof Error ? error.message : String(error);
53
+ throw new Error(`included manifest fragment TOML parse failed: ${message}`);
54
+ }
55
+ if (envelope === null || typeof envelope !== "object" || Array.isArray(envelope)) {
56
+ throw new Error("included manifest fragment splitter returned an invalid envelope.");
57
+ }
58
+ const value = envelope as Record<string, unknown>;
59
+ if (typeof value.rowToml !== "string") {
60
+ throw new Error("included manifest fragment splitter returned no rowToml.");
61
+ }
62
+ return { metadata: recordOf(value.metadata), rowsToml: value.rowToml };
63
+ }
@@ -0,0 +1,21 @@
1
+ import { render_manifest_include, split_manifest_fragment } from "@spellkave/validate-wasm";
2
+ import {
3
+ type RenderManifestIncludeOptions,
4
+ renderManifestIncludeWith,
5
+ splitIncludedManifestMetadataWith,
6
+ } from "./included-manifest-metadata-core";
7
+
8
+ export type {
9
+ IncludedManifestMetadata,
10
+ RenderManifestIncludeOptions,
11
+ } from "./included-manifest-metadata-core";
12
+
13
+ export function renderManifestInclude(
14
+ rendered: string,
15
+ options: RenderManifestIncludeOptions = {},
16
+ ): string {
17
+ return renderManifestIncludeWith(render_manifest_include, rendered, options);
18
+ }
19
+ export function splitIncludedManifestMetadata(fragmentToml: string) {
20
+ return splitIncludedManifestMetadataWith(split_manifest_fragment, fragmentToml);
21
+ }
package/src/index.ts ADDED
@@ -0,0 +1,30 @@
1
+ export type { InstanceRead } from "@spellkave/tool-contracts";
2
+ export * from "./authoring-harness";
3
+ export * from "./compose";
4
+ export * from "./corpus";
5
+ export * from "./damage";
6
+ export * from "./diff-manifest";
7
+ export * from "./docs-index";
8
+ export * from "./dsl";
9
+ export * from "./evaluate";
10
+ export * from "./explain";
11
+ export * from "./filter-manifest";
12
+ export * from "./grants";
13
+ export * from "./included-manifest-metadata";
14
+ export * from "./inspect";
15
+ export * from "./lint";
16
+ export * from "./manifest";
17
+ export * from "./metadata";
18
+ export * from "./npc-quests";
19
+ export * from "./owner-instance-read";
20
+ export * from "./patch";
21
+ export * from "./ref-tags";
22
+ export * from "./reference";
23
+ export * from "./row-identity";
24
+ export * from "./row-kinds";
25
+ export * from "./scenario-guidance";
26
+ export * from "./scoped-instance-read";
27
+ export * from "./session-predicates";
28
+ export * from "./spell";
29
+ export * from "./tag-prefix-families";
30
+ export * from "./validate";