@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.
- package/LICENSE +89 -0
- package/README.md +62 -0
- package/package.json +57 -0
- package/src/applyset-diff-types.ts +158 -0
- package/src/applyset.ts +82 -0
- package/src/authoring-harness.ts +487 -0
- package/src/browser.ts +114 -0
- package/src/compose.ts +699 -0
- package/src/corpus.generated.ts +279 -0
- package/src/corpus.ts +1180 -0
- package/src/damage.ts +75 -0
- package/src/diagnostics.ts +59 -0
- package/src/diff-manifest-core.ts +393 -0
- package/src/diff-manifest.ts +5 -0
- package/src/docs-index.ts +105 -0
- package/src/dsl.ts +10 -0
- package/src/evaluate.ts +100 -0
- package/src/explain-core.ts +219 -0
- package/src/explain.ts +43 -0
- package/src/filter-manifest-core.ts +122 -0
- package/src/filter-manifest.ts +41 -0
- package/src/grants-core.ts +38 -0
- package/src/grants.ts +10 -0
- package/src/included-manifest-metadata-core.ts +63 -0
- package/src/included-manifest-metadata.ts +21 -0
- package/src/index.ts +30 -0
- package/src/inspect.ts +35 -0
- package/src/inspection.ts +239 -0
- package/src/lint.ts +540 -0
- package/src/manifest-text.ts +21 -0
- package/src/manifest.ts +403 -0
- package/src/metadata.ts +137 -0
- package/src/npc-dm-authority-projection.ts +95 -0
- package/src/npc-dm-documents.ts +339 -0
- package/src/npc-dm-presentation.ts +17 -0
- package/src/npc-dm-srd.ts +51 -0
- package/src/npc-dm-types.ts +723 -0
- package/src/npc-kinds.ts +404 -0
- package/src/npc-quests.ts +319 -0
- package/src/observation-tokens.browser.ts +31 -0
- package/src/observation-tokens.ts +27 -0
- package/src/owner-instance-read.ts +765 -0
- package/src/patch.ts +47 -0
- package/src/ref-tags.ts +149 -0
- package/src/reference.ts +48 -0
- package/src/row-identity.ts +105 -0
- package/src/row-kinds-data.ts +35 -0
- package/src/row-kinds.ts +93 -0
- package/src/scenario-guidance.ts +180 -0
- package/src/scoped-instance-read.ts +187 -0
- package/src/service-only-audience.ts +14 -0
- package/src/session-predicates.ts +6 -0
- package/src/spell.ts +119 -0
- package/src/tag-prefix-families.ts +143 -0
- package/src/tooling.browser.ts +397 -0
- package/src/tooling.ts +38 -0
- package/src/validate.ts +255 -0
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publisher owner-cache projector for `spellkave_get` / `spellkave_describe`.
|
|
3
|
+
* Wiring-only `snapshot` / `worldTick` — no `db` on the returned port.
|
|
4
|
+
* D-11 types live in `@spellkave/tool-contracts`; this module is the factory.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
AUTHORED_ENTITIES_ONLY,
|
|
8
|
+
type OwnerDescribeAnswer,
|
|
9
|
+
type OwnerGetAnswer,
|
|
10
|
+
type OwnerInstanceRead,
|
|
11
|
+
} from "@spellkave/tool-contracts";
|
|
12
|
+
import { ROW_KIND_NATURAL_KEY, ROW_KIND_STORAGE_KEY } from "@spellkave/world-author/tooling";
|
|
13
|
+
import { INSTANCE_ROW_KINDS, type InstanceRowKind } from "./filter-manifest-core";
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
AUTHORED_ENTITIES_ONLY,
|
|
17
|
+
type OwnerDescribeAnswer,
|
|
18
|
+
type OwnerDescribeInput,
|
|
19
|
+
type OwnerGetAnswer,
|
|
20
|
+
type OwnerGetInput,
|
|
21
|
+
type OwnerInstanceRead,
|
|
22
|
+
} from "@spellkave/tool-contracts";
|
|
23
|
+
|
|
24
|
+
export type OwnerSnapshotHandle = { iter(): Iterable<Record<string, unknown>> };
|
|
25
|
+
export type OwnerSnapshot = {
|
|
26
|
+
[table: string]: OwnerSnapshotHandle | undefined;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const DEFAULT_LIMIT = 50;
|
|
30
|
+
const LABELS = [AUTHORED_ENTITIES_ONLY] as const;
|
|
31
|
+
|
|
32
|
+
/** D-4: natural key if defined, else storage-key fields in schema order. */
|
|
33
|
+
export function instanceKeyFields(kind: string): readonly string[] {
|
|
34
|
+
const natural = ROW_KIND_NATURAL_KEY[kind];
|
|
35
|
+
if (natural) return [natural];
|
|
36
|
+
return ROW_KIND_STORAGE_KEY[kind] ?? [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function rowMatchesCallerKey(
|
|
40
|
+
row: Record<string, unknown>,
|
|
41
|
+
kind: string,
|
|
42
|
+
callerKey: string,
|
|
43
|
+
): boolean {
|
|
44
|
+
const fields = instanceKeyFields(kind);
|
|
45
|
+
if (fields.length === 0) return false;
|
|
46
|
+
const parts = callerKey.split("::");
|
|
47
|
+
if (parts.length !== fields.length) return false;
|
|
48
|
+
return fields.every((field, i) => String(row[field] ?? "") === parts[i]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function rowMatchesPattern(row: Record<string, unknown>, kind: string, pattern: string): boolean {
|
|
52
|
+
return instanceKeyFields(kind).some((field) => String(row[field] ?? "").includes(pattern));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function createOwnerInstanceRead(args: {
|
|
56
|
+
snapshot: () => OwnerSnapshot;
|
|
57
|
+
worldTick: () => bigint;
|
|
58
|
+
readsAsPublisher: boolean;
|
|
59
|
+
}): OwnerInstanceRead {
|
|
60
|
+
if (!args.readsAsPublisher)
|
|
61
|
+
return {
|
|
62
|
+
get: () => ({ ok: false, code: "not_publisher", message: "reads_as_publisher is false" }),
|
|
63
|
+
describe: () => ({
|
|
64
|
+
ok: false,
|
|
65
|
+
code: "not_publisher",
|
|
66
|
+
message: "reads_as_publisher is false",
|
|
67
|
+
}),
|
|
68
|
+
};
|
|
69
|
+
return createInstanceProjector({
|
|
70
|
+
...args,
|
|
71
|
+
snapshot: () => {
|
|
72
|
+
const snapshot = args.snapshot();
|
|
73
|
+
const mapping =
|
|
74
|
+
snapshot.operator_manifest_authored_entity ?? snapshot.operatorManifestAuthoredEntity;
|
|
75
|
+
if (!mapping) throw new Error("reader_unavailable: publisher authored mapping view missing");
|
|
76
|
+
return { ...snapshot, visible_manifest_authored_entity: mapping };
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Pure projector over a detached admitted snapshot; admission belongs to the source. */
|
|
82
|
+
export function createInstanceProjector(args: {
|
|
83
|
+
snapshot: () => OwnerSnapshot;
|
|
84
|
+
worldTick: () => bigint;
|
|
85
|
+
}): OwnerInstanceRead {
|
|
86
|
+
return {
|
|
87
|
+
get(input) {
|
|
88
|
+
if (!INSTANCE_ROW_KINDS.includes(input.kind as InstanceRowKind)) {
|
|
89
|
+
return {
|
|
90
|
+
ok: false,
|
|
91
|
+
code: "usage",
|
|
92
|
+
message: `kind '${input.kind}' is not read from a world by this tool; use sk query --target live`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const snap = args.snapshot();
|
|
96
|
+
const tick = String(args.worldTick());
|
|
97
|
+
const mae = loadMae(snap);
|
|
98
|
+
const resolved = resolveBundle(mae, input.kind, input.key, input.bundle);
|
|
99
|
+
if (resolved.ok === false) return resolved;
|
|
100
|
+
if (input.key !== undefined && input.key !== "") {
|
|
101
|
+
const collision = collidingAuthoredSelector(mae, input.key, resolved.bundle);
|
|
102
|
+
if (collision) return collision;
|
|
103
|
+
}
|
|
104
|
+
const cap = boundedLimit(input.limit);
|
|
105
|
+
if (cap.ok === false) return cap;
|
|
106
|
+
const projected = projectKind(snap, mae, input.kind as InstanceRowKind, resolved.bundle);
|
|
107
|
+
if (input.key !== undefined && input.key !== "") {
|
|
108
|
+
const row = projected.find((r) => rowMatchesCallerKey(r, input.kind, input.key ?? ""));
|
|
109
|
+
if (!row) {
|
|
110
|
+
return {
|
|
111
|
+
ok: false,
|
|
112
|
+
code: "unknown_key",
|
|
113
|
+
message: `no authored ${input.kind} row for key ${input.key}`,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
ok: true,
|
|
118
|
+
kind: input.kind,
|
|
119
|
+
bundle: resolved.bundle,
|
|
120
|
+
tick,
|
|
121
|
+
labels: LABELS,
|
|
122
|
+
row, // D-6: keyed get is the whole row; `fields` applies to listings only
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
let rows = projected;
|
|
126
|
+
if (input.pattern) {
|
|
127
|
+
rows = rows.filter((r) => rowMatchesPattern(r, input.kind, input.pattern ?? ""));
|
|
128
|
+
}
|
|
129
|
+
const limit = cap.limit;
|
|
130
|
+
const match_count = rows.length;
|
|
131
|
+
const sliced = rows.slice(0, limit).map((row) => pickFields(row, input.kind, input.fields));
|
|
132
|
+
return {
|
|
133
|
+
ok: true,
|
|
134
|
+
kind: input.kind,
|
|
135
|
+
bundle: resolved.bundle,
|
|
136
|
+
tick,
|
|
137
|
+
labels: LABELS,
|
|
138
|
+
rows: sliced,
|
|
139
|
+
match_count,
|
|
140
|
+
left_out: Math.max(0, match_count - sliced.length),
|
|
141
|
+
};
|
|
142
|
+
},
|
|
143
|
+
describe(input) {
|
|
144
|
+
if (input.kind !== "entities") {
|
|
145
|
+
return {
|
|
146
|
+
ok: false,
|
|
147
|
+
code: "usage",
|
|
148
|
+
message: "describe is defined for entities; use sk get",
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const snap = args.snapshot();
|
|
152
|
+
const tick = String(args.worldTick());
|
|
153
|
+
const mae = loadMae(snap);
|
|
154
|
+
const byId = maeById(mae);
|
|
155
|
+
const subject = resolveSubject(mae, byId, input.key, input.bundle);
|
|
156
|
+
if (subject.ok === false) return subject;
|
|
157
|
+
const cap = boundedLimit(input.limit);
|
|
158
|
+
if (cap.ok === false) return cap;
|
|
159
|
+
const entityRow = projectKind(snap, mae, "entities", subject.bundle).find(
|
|
160
|
+
(r) => String(r.numeric_id) === subject.numeric_id,
|
|
161
|
+
);
|
|
162
|
+
if (!entityRow) {
|
|
163
|
+
return {
|
|
164
|
+
ok: false,
|
|
165
|
+
code: "unknown_key",
|
|
166
|
+
message: `no authored entities row for ${input.key}`,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const place = ownPlace(snap, subject.numeric_id);
|
|
170
|
+
const { chain, chain_end } = upwardContainment(snap, mae, byId, subject.numeric_id);
|
|
171
|
+
const allBelonging = downwardBelonging(snap, mae, byId, subject.numeric_id);
|
|
172
|
+
const belonging = allBelonging.slice(0, cap.limit);
|
|
173
|
+
const slices: Partial<Record<InstanceRowKind, Record<string, unknown>[]>> = {};
|
|
174
|
+
for (const kind of INSTANCE_ROW_KINDS) {
|
|
175
|
+
const rows = projectKind(snap, mae, kind, undefined).filter((r) =>
|
|
176
|
+
sliceNamesSubject(kind, r, subject.numeric_id),
|
|
177
|
+
);
|
|
178
|
+
if (rows.length > 0) slices[kind] = rows;
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
ok: true,
|
|
182
|
+
kind: "entities",
|
|
183
|
+
bundle: subject.bundle,
|
|
184
|
+
tick,
|
|
185
|
+
labels: LABELS,
|
|
186
|
+
row: entityRow,
|
|
187
|
+
slices,
|
|
188
|
+
numeric_id: subject.numeric_id,
|
|
189
|
+
place: place ?? chain.at(-1)?.place,
|
|
190
|
+
container_chain: chain,
|
|
191
|
+
chain_end,
|
|
192
|
+
belonging,
|
|
193
|
+
match_count: allBelonging.length,
|
|
194
|
+
left_out: Math.max(0, allBelonging.length - belonging.length),
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
type MaeRow = { manifestKey: string; bundleId: string; entityId: string; entityKey: string };
|
|
201
|
+
|
|
202
|
+
function loadMae(snap: OwnerSnapshot): MaeRow[] {
|
|
203
|
+
return iter(snap, "visible_manifest_authored_entity").map((raw) => {
|
|
204
|
+
const bundleId = String(raw.bundleId ?? raw.bundle_id ?? "");
|
|
205
|
+
const manifestKey = String(raw.manifestKey ?? raw.manifest_key ?? "");
|
|
206
|
+
const prefix = `${bundleId}:`;
|
|
207
|
+
const entityKey = manifestKey.startsWith(prefix)
|
|
208
|
+
? manifestKey.slice(prefix.length)
|
|
209
|
+
: manifestKey;
|
|
210
|
+
return {
|
|
211
|
+
manifestKey,
|
|
212
|
+
bundleId,
|
|
213
|
+
entityId: idStr(raw.entityId ?? raw.entity_id),
|
|
214
|
+
entityKey,
|
|
215
|
+
};
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function maeById(mae: MaeRow[]): Map<string, MaeRow> {
|
|
220
|
+
const m = new Map<string, MaeRow>();
|
|
221
|
+
for (const row of mae) m.set(row.entityId, row);
|
|
222
|
+
return m;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const ENTITY_KEY_FIELDS = new Set(["entity_key", "actor_entity_key", "container_entity_key"]);
|
|
226
|
+
|
|
227
|
+
/** Entity keys inside a caller `::` key, in schema field order — never the whole composite. */
|
|
228
|
+
function callerEntityKeys(kind: string, callerKey: string): string[] {
|
|
229
|
+
const fields = instanceKeyFields(kind);
|
|
230
|
+
const parts = callerKey.split("::");
|
|
231
|
+
if (parts.length !== fields.length) {
|
|
232
|
+
return fields.length === 1 ? [callerKey] : [];
|
|
233
|
+
}
|
|
234
|
+
return fields.flatMap((field, i) => (ENTITY_KEY_FIELDS.has(field) ? [parts[i] ?? ""] : []));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function resolveBundle(
|
|
238
|
+
mae: MaeRow[],
|
|
239
|
+
kind: string,
|
|
240
|
+
key: string | undefined,
|
|
241
|
+
bundle: string | undefined,
|
|
242
|
+
): { ok: true; bundle?: string } | Extract<OwnerGetAnswer, { ok: false }> {
|
|
243
|
+
if (bundle) return { ok: true, bundle };
|
|
244
|
+
if (!key) return { ok: true };
|
|
245
|
+
const entityKeys = callerEntityKeys(kind, key);
|
|
246
|
+
if (entityKeys.length === 0) return { ok: true };
|
|
247
|
+
const hits = mae.filter((r) => entityKeys.includes(r.entityKey));
|
|
248
|
+
const bundles = [...new Set(hits.map((r) => r.bundleId))];
|
|
249
|
+
if (bundles.length > 1) {
|
|
250
|
+
return {
|
|
251
|
+
ok: false,
|
|
252
|
+
code: "ambiguous_bundle",
|
|
253
|
+
message: `entity_key is recorded in more than one bundle: ${bundles.join(", ")}`,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
return { ok: true, bundle: bundles[0] };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function collidingAuthoredSelector(
|
|
260
|
+
mae: MaeRow[],
|
|
261
|
+
key: string,
|
|
262
|
+
bundle?: string,
|
|
263
|
+
): Extract<OwnerGetAnswer, { ok: false }> | undefined {
|
|
264
|
+
const scoped = bundle ? mae.filter((r) => r.bundleId === bundle) : mae;
|
|
265
|
+
const keyHits = scoped.filter((r) => r.entityKey === key);
|
|
266
|
+
const idHits = scoped.filter((r) => r.entityId === key);
|
|
267
|
+
if (keyHits.length === 0 || idHits.length === 0) return undefined;
|
|
268
|
+
const keyIds = new Set(keyHits.map((r) => r.entityId));
|
|
269
|
+
const idIds = new Set(idHits.map((r) => r.entityId));
|
|
270
|
+
const same = keyIds.size === 1 && idIds.size === 1 && [...keyIds][0] === [...idIds][0];
|
|
271
|
+
if (same) return undefined;
|
|
272
|
+
const keyPart = keyHits
|
|
273
|
+
.map((r) => `entity_key '${r.entityKey}' is numeric_id ${r.entityId} (bundle ${r.bundleId})`)
|
|
274
|
+
.join("; ");
|
|
275
|
+
const idPart = idHits
|
|
276
|
+
.map((r) => `numeric_id ${r.entityId} is entity_key '${r.entityKey}' (bundle ${r.bundleId})`)
|
|
277
|
+
.join("; ");
|
|
278
|
+
return {
|
|
279
|
+
ok: false,
|
|
280
|
+
code: "usage",
|
|
281
|
+
message: `key '${key}' names both an authored entity_key and a different numeric_id: ${keyPart}; ${idPart}. Use the unique entity_key, or a numeric_id that is not also an entity_key.`,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function boundedLimit(
|
|
286
|
+
limit: number | undefined,
|
|
287
|
+
): { ok: true; limit: number } | { ok: false; code: "usage"; message: string } {
|
|
288
|
+
if (limit === undefined) return { ok: true, limit: DEFAULT_LIMIT };
|
|
289
|
+
if (!Number.isInteger(limit) || limit < 0) {
|
|
290
|
+
return { ok: false, code: "usage", message: "limit must be a non-negative integer" };
|
|
291
|
+
}
|
|
292
|
+
return { ok: true, limit };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function resolveSubject(
|
|
296
|
+
mae: MaeRow[],
|
|
297
|
+
byId: Map<string, MaeRow>,
|
|
298
|
+
key: string,
|
|
299
|
+
bundle?: string,
|
|
300
|
+
):
|
|
301
|
+
| { ok: true; bundle?: string; numeric_id: string; entityKey: string }
|
|
302
|
+
| Extract<OwnerDescribeAnswer, { ok: false }> {
|
|
303
|
+
const wanted = bundle !== undefined && bundle !== "" ? bundle : undefined;
|
|
304
|
+
const collision = collidingAuthoredSelector(mae, key, wanted);
|
|
305
|
+
if (collision) return collision;
|
|
306
|
+
const asId = byId.get(key);
|
|
307
|
+
const asIdInScope = asId !== undefined && (wanted === undefined || asId.bundleId === wanted);
|
|
308
|
+
if (asIdInScope && asId) {
|
|
309
|
+
return {
|
|
310
|
+
ok: true,
|
|
311
|
+
bundle: asId.bundleId,
|
|
312
|
+
numeric_id: asId.entityId,
|
|
313
|
+
entityKey: asId.entityKey,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
let hits = mae.filter((r) => r.entityKey === key);
|
|
317
|
+
if (wanted !== undefined) hits = hits.filter((r) => r.bundleId === wanted);
|
|
318
|
+
if (hits.length === 0) {
|
|
319
|
+
return {
|
|
320
|
+
ok: false,
|
|
321
|
+
code: "unknown_key",
|
|
322
|
+
message:
|
|
323
|
+
wanted !== undefined
|
|
324
|
+
? `no authored entity ${key} in bundle ${wanted}`
|
|
325
|
+
: `no authored entity ${key}`,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
if (hits.length > 1) {
|
|
329
|
+
return {
|
|
330
|
+
ok: false,
|
|
331
|
+
code: "ambiguous_bundle",
|
|
332
|
+
message: `entity_key '${key}' is recorded in more than one bundle: ${[...new Set(hits.map((r) => r.bundleId))].join(", ")}`,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
const hit = hits[0];
|
|
336
|
+
if (!hit) {
|
|
337
|
+
return { ok: false, code: "unknown_key", message: `no authored entity ${key}` };
|
|
338
|
+
}
|
|
339
|
+
return { ok: true, bundle: hit.bundleId, numeric_id: hit.entityId, entityKey: hit.entityKey };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function sliceNamesSubject(
|
|
343
|
+
kind: InstanceRowKind,
|
|
344
|
+
row: Record<string, unknown>,
|
|
345
|
+
numericId: string,
|
|
346
|
+
): boolean {
|
|
347
|
+
switch (kind) {
|
|
348
|
+
case "entities":
|
|
349
|
+
case "actors":
|
|
350
|
+
case "actor_spatial_locations":
|
|
351
|
+
return String(row.numeric_id) === numericId;
|
|
352
|
+
case "actor_capabilities":
|
|
353
|
+
case "actor_equipment_slots":
|
|
354
|
+
return String(row.numeric_id) === numericId;
|
|
355
|
+
case "entity_containment":
|
|
356
|
+
return String(row.numeric_id) === numericId || String(row.container_numeric_id) === numericId;
|
|
357
|
+
case "entity_tag":
|
|
358
|
+
case "object_state":
|
|
359
|
+
return String(row.numeric_id) === numericId;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function projectKind(
|
|
364
|
+
snap: OwnerSnapshot,
|
|
365
|
+
mae: MaeRow[],
|
|
366
|
+
kind: InstanceRowKind,
|
|
367
|
+
bundle?: string,
|
|
368
|
+
): Array<Record<string, unknown> & { entity_key?: string; numeric_id?: string }> {
|
|
369
|
+
const byId = maeById(mae);
|
|
370
|
+
const scoped = bundle ? mae.filter((r) => r.bundleId === bundle) : mae;
|
|
371
|
+
switch (kind) {
|
|
372
|
+
case "entities":
|
|
373
|
+
return iter(snap, "entity")
|
|
374
|
+
.map((raw) => {
|
|
375
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
376
|
+
const authored = byId.get(id);
|
|
377
|
+
if (!authored) return undefined;
|
|
378
|
+
if (bundle && authored.bundleId !== bundle) return undefined;
|
|
379
|
+
const ownerId = idStr(raw.ownedByEntityId ?? raw.owned_by_entity_id);
|
|
380
|
+
return {
|
|
381
|
+
entity_key: authored.entityKey,
|
|
382
|
+
bundle_id: authored.bundleId,
|
|
383
|
+
numeric_id: id,
|
|
384
|
+
name: raw.name,
|
|
385
|
+
kind: raw.kind,
|
|
386
|
+
entity_def_id: raw.entityDefId ?? raw.entity_def_id,
|
|
387
|
+
quantity: raw.quantity ?? 1,
|
|
388
|
+
description: descriptionFor(snap, id),
|
|
389
|
+
owned_by_entity_key: byId.get(ownerId)?.entityKey ?? "",
|
|
390
|
+
place_node_id: ownPlace(snap, id) ?? "",
|
|
391
|
+
};
|
|
392
|
+
})
|
|
393
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
394
|
+
case "actors":
|
|
395
|
+
return iter(snap, "actor")
|
|
396
|
+
.map((raw) => {
|
|
397
|
+
const body = idStr(raw.currentBody ?? raw.current_body);
|
|
398
|
+
const authored = byId.get(body);
|
|
399
|
+
if (!authored) return undefined;
|
|
400
|
+
if (bundle && authored.bundleId !== bundle) return undefined;
|
|
401
|
+
return {
|
|
402
|
+
entity_key: authored.entityKey,
|
|
403
|
+
bundle_id: authored.bundleId,
|
|
404
|
+
numeric_id: body,
|
|
405
|
+
actor_id: idStr(raw.actorId ?? raw.actor_id),
|
|
406
|
+
name: raw.name,
|
|
407
|
+
current_body: body,
|
|
408
|
+
description: raw.description,
|
|
409
|
+
};
|
|
410
|
+
})
|
|
411
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
412
|
+
case "actor_spatial_locations":
|
|
413
|
+
return iter(snap, "actor_spatial_location")
|
|
414
|
+
.map((raw) => {
|
|
415
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
416
|
+
const authored = byId.get(id);
|
|
417
|
+
if (!authored) return undefined;
|
|
418
|
+
if (bundle && authored.bundleId !== bundle) return undefined;
|
|
419
|
+
return {
|
|
420
|
+
entity_key: authored.entityKey,
|
|
421
|
+
bundle_id: authored.bundleId,
|
|
422
|
+
numeric_id: id,
|
|
423
|
+
place_node_id: String(raw.placeNodeId ?? raw.place_node_id ?? ""),
|
|
424
|
+
selection_path: String(raw.selectionPath ?? raw.selection_path ?? ""),
|
|
425
|
+
};
|
|
426
|
+
})
|
|
427
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
428
|
+
case "actor_capabilities":
|
|
429
|
+
return iter(snap, "capability")
|
|
430
|
+
.map((raw) => {
|
|
431
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
432
|
+
const authored = byId.get(id);
|
|
433
|
+
if (!authored) return undefined;
|
|
434
|
+
if (bundle && authored.bundleId !== bundle) return undefined;
|
|
435
|
+
const cap = String(raw.capabilityId ?? raw.capability_id ?? "");
|
|
436
|
+
const source = String(raw.source ?? "");
|
|
437
|
+
const source_args_hash = String(raw.sourceArgsHash ?? raw.source_args_hash ?? "");
|
|
438
|
+
return {
|
|
439
|
+
entity_key: authored.entityKey,
|
|
440
|
+
actor_entity_key: authored.entityKey,
|
|
441
|
+
bundle_id: String(raw.bundleId ?? raw.bundle_id ?? authored.bundleId),
|
|
442
|
+
numeric_id: id,
|
|
443
|
+
capability_id: cap,
|
|
444
|
+
source,
|
|
445
|
+
source_args_hash,
|
|
446
|
+
visibility: visTag(raw.visibility),
|
|
447
|
+
extent_ref: String(raw.extentRef ?? raw.extent_ref ?? ""),
|
|
448
|
+
extent_kind_ref: String(raw.extentKindRef ?? raw.extent_kind_ref ?? ""),
|
|
449
|
+
elevation_class: visTag(raw.elevationClass ?? raw.elevation_class),
|
|
450
|
+
};
|
|
451
|
+
})
|
|
452
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
453
|
+
case "actor_equipment_slots":
|
|
454
|
+
return iter(snap, "entity_equipment_slot")
|
|
455
|
+
.map((raw) => {
|
|
456
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
457
|
+
const authored = byId.get(id);
|
|
458
|
+
if (!authored) return undefined;
|
|
459
|
+
if (bundle && authored.bundleId !== bundle) return undefined;
|
|
460
|
+
const itemId = idStr(raw.equippedEntityId ?? raw.equipped_entity_id);
|
|
461
|
+
const slot = String(raw.slotId ?? raw.slot_id ?? "");
|
|
462
|
+
return {
|
|
463
|
+
entity_key: authored.entityKey,
|
|
464
|
+
actor_entity_key: authored.entityKey,
|
|
465
|
+
item_key: byId.get(itemId)?.entityKey,
|
|
466
|
+
slot_id: slot,
|
|
467
|
+
numeric_id: id,
|
|
468
|
+
};
|
|
469
|
+
})
|
|
470
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
471
|
+
case "entity_containment":
|
|
472
|
+
return iter(snap, "entity_containment")
|
|
473
|
+
.map((raw) => {
|
|
474
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
475
|
+
const authored = byId.get(id);
|
|
476
|
+
if (!authored) return undefined;
|
|
477
|
+
if (bundle && authored.bundleId !== bundle) return undefined;
|
|
478
|
+
const container = idStr(raw.containerId ?? raw.container_id);
|
|
479
|
+
return {
|
|
480
|
+
entity_key: authored.entityKey,
|
|
481
|
+
container_entity_key: byId.get(container)?.entityKey ?? "",
|
|
482
|
+
container_numeric_id: container,
|
|
483
|
+
slot_id: String(raw.slotId ?? raw.slot_id ?? ""),
|
|
484
|
+
direction: String(raw.direction ?? ""),
|
|
485
|
+
numeric_id: id,
|
|
486
|
+
};
|
|
487
|
+
})
|
|
488
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
489
|
+
case "entity_tag":
|
|
490
|
+
return iter(snap, "entity_tag")
|
|
491
|
+
.map((raw) => {
|
|
492
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
493
|
+
const authored = byId.get(id);
|
|
494
|
+
if (!authored) return undefined;
|
|
495
|
+
if (bundle && authored.bundleId !== bundle) return undefined;
|
|
496
|
+
const tag = String(raw.tag ?? "");
|
|
497
|
+
return {
|
|
498
|
+
entity_key: authored.entityKey,
|
|
499
|
+
tag,
|
|
500
|
+
bundle_id: String(raw.bundleId ?? raw.bundle_id ?? ""),
|
|
501
|
+
source: String(raw.source ?? ""),
|
|
502
|
+
numeric_id: id,
|
|
503
|
+
};
|
|
504
|
+
})
|
|
505
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
506
|
+
case "object_state":
|
|
507
|
+
return iter(snap, "object_state")
|
|
508
|
+
.filter((raw) => String(raw.targetKind ?? raw.target_kind ?? "") === "entity")
|
|
509
|
+
.map((raw) => {
|
|
510
|
+
const targetId = String(raw.targetId ?? raw.target_id ?? "").replace(/^entity:/, "");
|
|
511
|
+
const authored = byId.get(targetId);
|
|
512
|
+
if (!authored) return undefined;
|
|
513
|
+
if (bundle && authored.bundleId !== bundle) return undefined;
|
|
514
|
+
const stateId = String(raw.stateId ?? raw.state_id ?? "");
|
|
515
|
+
return {
|
|
516
|
+
entity_key: authored.entityKey,
|
|
517
|
+
state_id: stateId,
|
|
518
|
+
numeric_id: targetId,
|
|
519
|
+
bundle_id: String(raw.bundleId ?? raw.bundle_id ?? authored.bundleId),
|
|
520
|
+
source_template_id: String(raw.sourceTemplateId ?? raw.source_template_id ?? ""),
|
|
521
|
+
expires_at_tick: jsonInt(raw.expiresAtTick ?? raw.expires_at_tick ?? 0),
|
|
522
|
+
visibility: visTag(raw.visibility),
|
|
523
|
+
...objectStateCarriers(raw.value),
|
|
524
|
+
};
|
|
525
|
+
})
|
|
526
|
+
.filter((r): r is NonNullable<typeof r> => r !== undefined);
|
|
527
|
+
default:
|
|
528
|
+
return scoped.map((r) => ({ entity_key: r.entityKey }));
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function ownPlace(snap: OwnerSnapshot, numericId: string): string | undefined {
|
|
533
|
+
for (const raw of iter(snap, "entity_location")) {
|
|
534
|
+
if (idStr(raw.entityId ?? raw.entity_id) === numericId) {
|
|
535
|
+
const p = String(raw.placeNodeId ?? raw.place_node_id ?? "");
|
|
536
|
+
if (p) return p;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
for (const raw of iter(snap, "actor_spatial_location")) {
|
|
540
|
+
if (idStr(raw.entityId ?? raw.entity_id) === numericId) {
|
|
541
|
+
const p = String(raw.placeNodeId ?? raw.place_node_id ?? "");
|
|
542
|
+
if (p) return p;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return undefined;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function upwardContainment(
|
|
549
|
+
snap: OwnerSnapshot,
|
|
550
|
+
_mae: MaeRow[],
|
|
551
|
+
byId: Map<string, MaeRow>,
|
|
552
|
+
start: string,
|
|
553
|
+
): {
|
|
554
|
+
chain: Array<{ numeric_id: string; entity_key?: string; place?: string }>;
|
|
555
|
+
chain_end: { reason: "place" | "none" | "cycle"; numeric_id?: string };
|
|
556
|
+
} {
|
|
557
|
+
const byContained = new Map<string, string>();
|
|
558
|
+
for (const raw of iter(snap, "entity_containment")) {
|
|
559
|
+
byContained.set(
|
|
560
|
+
idStr(raw.entityId ?? raw.entity_id),
|
|
561
|
+
idStr(raw.containerId ?? raw.container_id),
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
const own = ownPlace(snap, start);
|
|
565
|
+
if (own) {
|
|
566
|
+
return { chain: [], chain_end: { reason: "place", numeric_id: start } };
|
|
567
|
+
}
|
|
568
|
+
const chain: Array<{ numeric_id: string; entity_key?: string; place?: string }> = [];
|
|
569
|
+
const visited = new Set<string>([start]);
|
|
570
|
+
let cur = start;
|
|
571
|
+
while (true) {
|
|
572
|
+
const next = byContained.get(cur);
|
|
573
|
+
if (!next) {
|
|
574
|
+
return { chain, chain_end: { reason: "none", numeric_id: cur } };
|
|
575
|
+
}
|
|
576
|
+
if (visited.has(next)) {
|
|
577
|
+
return { chain, chain_end: { reason: "cycle", numeric_id: next } };
|
|
578
|
+
}
|
|
579
|
+
visited.add(next);
|
|
580
|
+
const authored = byId.get(next);
|
|
581
|
+
const place = ownPlace(snap, next);
|
|
582
|
+
chain.push({
|
|
583
|
+
numeric_id: next,
|
|
584
|
+
entity_key: authored?.entityKey,
|
|
585
|
+
place,
|
|
586
|
+
});
|
|
587
|
+
if (place) {
|
|
588
|
+
return { chain, chain_end: { reason: "place", numeric_id: next } };
|
|
589
|
+
}
|
|
590
|
+
cur = next;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function downwardBelonging(
|
|
595
|
+
snap: OwnerSnapshot,
|
|
596
|
+
_mae: MaeRow[],
|
|
597
|
+
byId: Map<string, MaeRow>,
|
|
598
|
+
root: string,
|
|
599
|
+
): Array<{
|
|
600
|
+
entity_key: string;
|
|
601
|
+
numeric_id: string;
|
|
602
|
+
name?: string;
|
|
603
|
+
definition?: string;
|
|
604
|
+
belongs_to_key?: string;
|
|
605
|
+
belongs_to_numeric_id: string;
|
|
606
|
+
relation: "containment" | "ownership" | "equipment";
|
|
607
|
+
}> {
|
|
608
|
+
const names = new Map<string, { name?: string; definition?: string }>();
|
|
609
|
+
for (const raw of iter(snap, "entity")) {
|
|
610
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
611
|
+
names.set(id, {
|
|
612
|
+
name: raw.name as string | undefined,
|
|
613
|
+
definition: String(raw.entityDefId ?? raw.entity_def_id ?? ""),
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
const out: ReturnType<typeof downwardBelonging> = [];
|
|
617
|
+
const traverse = new Set<string>([root]);
|
|
618
|
+
const emitted = new Set<string>();
|
|
619
|
+
const queue = [root];
|
|
620
|
+
const containment = iter(snap, "entity_containment");
|
|
621
|
+
const equipment = iter(snap, "entity_equipment_slot");
|
|
622
|
+
const entities = iter(snap, "entity");
|
|
623
|
+
const emit = (
|
|
624
|
+
id: string,
|
|
625
|
+
cur: string,
|
|
626
|
+
curKey: string | undefined,
|
|
627
|
+
relation: "containment" | "ownership" | "equipment",
|
|
628
|
+
slot?: string,
|
|
629
|
+
) => {
|
|
630
|
+
if (id === root) return;
|
|
631
|
+
const edge = `${id}\0${relation}\0${cur}\0${slot ?? ""}`;
|
|
632
|
+
if (emitted.has(edge)) return;
|
|
633
|
+
emitted.add(edge);
|
|
634
|
+
const authored = byId.get(id);
|
|
635
|
+
if (!authored) return;
|
|
636
|
+
out.push({
|
|
637
|
+
entity_key: authored.entityKey,
|
|
638
|
+
numeric_id: id,
|
|
639
|
+
...names.get(id),
|
|
640
|
+
...(curKey ? { belongs_to_key: curKey } : {}),
|
|
641
|
+
belongs_to_numeric_id: cur,
|
|
642
|
+
relation,
|
|
643
|
+
});
|
|
644
|
+
};
|
|
645
|
+
const walkChild = (id: string) => {
|
|
646
|
+
if (id === "" || id === root) return;
|
|
647
|
+
if (traverse.has(id)) return;
|
|
648
|
+
traverse.add(id);
|
|
649
|
+
queue.push(id);
|
|
650
|
+
};
|
|
651
|
+
while (queue.length > 0) {
|
|
652
|
+
const cur = queue.shift() ?? "";
|
|
653
|
+
const curKey = byId.get(cur)?.entityKey;
|
|
654
|
+
for (const raw of containment) {
|
|
655
|
+
if (idStr(raw.containerId ?? raw.container_id) !== cur) continue;
|
|
656
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
657
|
+
emit(id, cur, curKey, "containment");
|
|
658
|
+
walkChild(id);
|
|
659
|
+
}
|
|
660
|
+
for (const raw of equipment) {
|
|
661
|
+
if (idStr(raw.entityId ?? raw.entity_id) !== cur) continue;
|
|
662
|
+
const id = idStr(raw.equippedEntityId ?? raw.equipped_entity_id);
|
|
663
|
+
const slot = String(raw.slotId ?? raw.slot_id ?? "");
|
|
664
|
+
emit(id, cur, curKey, "equipment", slot);
|
|
665
|
+
walkChild(id);
|
|
666
|
+
}
|
|
667
|
+
for (const raw of entities) {
|
|
668
|
+
const owner = idStr(raw.ownedByEntityId ?? raw.owned_by_entity_id);
|
|
669
|
+
if (owner !== cur) continue;
|
|
670
|
+
const id = idStr(raw.entityId ?? raw.entity_id);
|
|
671
|
+
emit(id, cur, curKey, "ownership");
|
|
672
|
+
walkChild(id);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return out;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
function iter(snap: OwnerSnapshot, table: string): Record<string, unknown>[] {
|
|
679
|
+
const handle = snap[table];
|
|
680
|
+
if (!handle) return [];
|
|
681
|
+
return [...handle.iter()];
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
function idStr(v: unknown): string {
|
|
685
|
+
if (v === undefined || v === null) return "";
|
|
686
|
+
return String(v);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function descriptionFor(snap: OwnerSnapshot, entityId: string): string {
|
|
690
|
+
for (const raw of iter(snap, "entity_description")) {
|
|
691
|
+
if (idStr(raw.entityId ?? raw.entity_id) === entityId) {
|
|
692
|
+
return String(raw.description ?? "");
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
return "";
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
function visTag(v: unknown): string {
|
|
699
|
+
if (v === undefined || v === null) return "";
|
|
700
|
+
if (typeof v === "string") return v;
|
|
701
|
+
if (typeof v === "object" && v !== null && "tag" in v)
|
|
702
|
+
return String((v as { tag: unknown }).tag ?? "");
|
|
703
|
+
return String(v);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
function jsonInt(v: unknown): string {
|
|
707
|
+
if (typeof v === "bigint") return v.toString();
|
|
708
|
+
if (typeof v === "number" && Number.isFinite(v)) {
|
|
709
|
+
return Number.isInteger(v) ? String(v) : String(Math.trunc(v));
|
|
710
|
+
}
|
|
711
|
+
if (typeof v === "string" && /^-?\d+$/.test(v)) return v;
|
|
712
|
+
if (v && typeof v === "object" && "toString" in v) return String(v);
|
|
713
|
+
return "0";
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
function objectStateCarriers(value: unknown): {
|
|
717
|
+
value_bool?: boolean;
|
|
718
|
+
value_int?: string;
|
|
719
|
+
value_text?: string;
|
|
720
|
+
} {
|
|
721
|
+
if (value === undefined || value === null) return {};
|
|
722
|
+
if (typeof value === "object" && value !== null && "tag" in value) {
|
|
723
|
+
const tagged = value as { tag?: string; value?: unknown };
|
|
724
|
+
const tag = tagged.tag ?? "";
|
|
725
|
+
if (tag === "Bool" || tag === "bool") return { value_bool: Boolean(tagged.value) };
|
|
726
|
+
if (tag === "Int" || tag === "int") return { value_int: jsonInt(tagged.value) };
|
|
727
|
+
if (tag === "Text" || tag === "text" || tag === "RowRef" || tag === "Outcome") {
|
|
728
|
+
return { value_text: String(tagged.value ?? "") };
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
return {};
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/** D-6 listing columns when the caller omits `fields`. Key fields are always kept. */
|
|
735
|
+
export function listingPreferredFields(kind: string): readonly string[] {
|
|
736
|
+
const extra: Record<string, readonly string[]> = {
|
|
737
|
+
entities: ["numeric_id", "bundle_id", "name", "kind", "entity_def_id", "place_node_id"],
|
|
738
|
+
actors: ["numeric_id", "bundle_id", "name", "actor_id", "current_body"],
|
|
739
|
+
actor_spatial_locations: ["numeric_id", "bundle_id", "place_node_id"],
|
|
740
|
+
actor_capabilities: ["numeric_id", "bundle_id", "capability_id"],
|
|
741
|
+
actor_equipment_slots: ["numeric_id", "item_key"],
|
|
742
|
+
entity_containment: ["numeric_id", "container_numeric_id"],
|
|
743
|
+
entity_tag: ["numeric_id", "bundle_id"],
|
|
744
|
+
object_state: ["numeric_id", "bundle_id", "value_int", "value_bool"],
|
|
745
|
+
};
|
|
746
|
+
return [
|
|
747
|
+
...new Set([...instanceKeyFields(kind), ...(extra[kind] ?? ["numeric_id", "bundle_id"])]),
|
|
748
|
+
];
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
function pickFields(
|
|
752
|
+
row: Record<string, unknown>,
|
|
753
|
+
kind: string,
|
|
754
|
+
fields?: string[],
|
|
755
|
+
): Record<string, unknown> {
|
|
756
|
+
const keep = new Set([
|
|
757
|
+
...((fields && fields.length > 0 ? fields : listingPreferredFields(kind)) ?? []),
|
|
758
|
+
...instanceKeyFields(kind),
|
|
759
|
+
]);
|
|
760
|
+
const out: Record<string, unknown> = {};
|
|
761
|
+
for (const f of keep) {
|
|
762
|
+
if (f in row) out[f] = row[f];
|
|
763
|
+
}
|
|
764
|
+
return out;
|
|
765
|
+
}
|