@spellkave/sdk 0.0.0-bootstrap → 0.0.0-stage

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 (144) hide show
  1. package/README.md +2 -155
  2. package/package.json +4 -63
  3. package/LICENSE +0 -89
  4. package/src/binding_rows.ts +0 -186
  5. package/src/connection.ts +0 -964
  6. package/src/core/actor.ts +0 -62
  7. package/src/core/actor_control.ts +0 -193
  8. package/src/core/actor_control_query_methods.ts +0 -101
  9. package/src/core/actor_control_reaction_methods.ts +0 -79
  10. package/src/core/actor_control_sense_methods.ts +0 -81
  11. package/src/core/actor_control_source.ts +0 -57
  12. package/src/core/actor_session_methods.ts +0 -226
  13. package/src/core/binding_drift.ts +0 -301
  14. package/src/core/credential.ts +0 -259
  15. package/src/core/driver_control.ts +0 -54
  16. package/src/core/effective_environment_state.ts +0 -71
  17. package/src/core/factored_manipulation_attempt.ts +0 -158
  18. package/src/core/ids.ts +0 -60
  19. package/src/core/interaction/canonical.ts +0 -15
  20. package/src/core/interaction/fingerprint.ts +0 -118
  21. package/src/core/interaction/index.ts +0 -31
  22. package/src/core/interaction/intent.ts +0 -284
  23. package/src/core/interaction/outcome.ts +0 -67
  24. package/src/core/interaction/rejection_ledger.ts +0 -108
  25. package/src/core/kernel_contract.generated.ts +0 -10
  26. package/src/core/log.ts +0 -23
  27. package/src/core/movement.ts +0 -111
  28. package/src/core/node-io.ts +0 -208
  29. package/src/core/paths.ts +0 -220
  30. package/src/core/profile.ts +0 -270
  31. package/src/core/profiles.ts +0 -9
  32. package/src/core/provenance_buffer.ts +0 -102
  33. package/src/core/readers/_helpers.ts +0 -273
  34. package/src/core/readers/action_description.ts +0 -17
  35. package/src/core/readers/action_economy_events.ts +0 -149
  36. package/src/core/readers/actor_body_swaps.ts +0 -62
  37. package/src/core/readers/actor_snapshot.ts +0 -197
  38. package/src/core/readers/actor_states.ts +0 -163
  39. package/src/core/readers/actors_and_bodies.ts +0 -179
  40. package/src/core/readers/affordance_cue.ts +0 -59
  41. package/src/core/readers/affordance_defaults.ts +0 -158
  42. package/src/core/readers/affordance_group_membership.ts +0 -67
  43. package/src/core/readers/affordance_input.ts +0 -129
  44. package/src/core/readers/affordance_reference.ts +0 -41
  45. package/src/core/readers/apply_consequences.ts +0 -54
  46. package/src/core/readers/audience_trace_presentation.ts +0 -62
  47. package/src/core/readers/bounded_collection.ts +0 -29
  48. package/src/core/readers/bundle_definitions.ts +0 -74
  49. package/src/core/readers/bundle_entities.ts +0 -62
  50. package/src/core/readers/co_location.ts +0 -71
  51. package/src/core/readers/combat_session.ts +0 -124
  52. package/src/core/readers/combat_session_active.ts +0 -44
  53. package/src/core/readers/combat_session_feedback.ts +0 -37
  54. package/src/core/readers/combat_session_grid.ts +0 -117
  55. package/src/core/readers/combat_session_roster.ts +0 -98
  56. package/src/core/readers/combat_session_types.ts +0 -51
  57. package/src/core/readers/condition_deltas.ts +0 -53
  58. package/src/core/readers/container_contents.ts +0 -92
  59. package/src/core/readers/dialogue_guide.ts +0 -99
  60. package/src/core/readers/dialogue_prompt_presentation.ts +0 -90
  61. package/src/core/readers/driver_control.ts +0 -64
  62. package/src/core/readers/entity_art.ts +0 -219
  63. package/src/core/readers/entity_containment.ts +0 -119
  64. package/src/core/readers/equipment.ts +0 -99
  65. package/src/core/readers/intervention_windows.ts +0 -136
  66. package/src/core/readers/inventory_query.ts +0 -145
  67. package/src/core/readers/item_detail.ts +0 -198
  68. package/src/core/readers/location_snapshot.ts +0 -216
  69. package/src/core/readers/manifest_apply_provenance.ts +0 -32
  70. package/src/core/readers/manifest_export.ts +0 -81
  71. package/src/core/readers/manipulation_context.ts +0 -475
  72. package/src/core/readers/map_layers.ts +0 -49
  73. package/src/core/readers/metadata_rows.ts +0 -56
  74. package/src/core/readers/object_state.ts +0 -124
  75. package/src/core/readers/observer_knowledge.ts +0 -41
  76. package/src/core/readers/observer_place_knowledge.ts +0 -37
  77. package/src/core/readers/observer_trace_knowledge.ts +0 -45
  78. package/src/core/readers/outcome_template.ts +0 -41
  79. package/src/core/readers/place_topology.ts +0 -109
  80. package/src/core/readers/reaction_options.ts +0 -156
  81. package/src/core/readers/resource_deltas.ts +0 -47
  82. package/src/core/readers/scene.ts +0 -275
  83. package/src/core/readers/seats.ts +0 -33
  84. package/src/core/readers/self.ts +0 -245
  85. package/src/core/readers/self_capabilities.ts +0 -159
  86. package/src/core/readers/self_economy.ts +0 -101
  87. package/src/core/readers/self_economy_budget.ts +0 -93
  88. package/src/core/readers/self_evaluations.ts +0 -128
  89. package/src/core/readers/self_identity.ts +0 -104
  90. package/src/core/readers/sense_context.ts +0 -112
  91. package/src/core/readers/session.ts +0 -207
  92. package/src/core/readers/session_detail.ts +0 -211
  93. package/src/core/readers/session_status.ts +0 -68
  94. package/src/core/readers/spatial_environment_state.ts +0 -74
  95. package/src/core/readers/spatial_niche_attachments.ts +0 -28
  96. package/src/core/readers/substrate_definitions.ts +0 -27
  97. package/src/core/readers/suggest_next_action.ts +0 -72
  98. package/src/core/readers/surface_membership.ts +0 -78
  99. package/src/core/readers/tag_reads.ts +0 -54
  100. package/src/core/readers/trace_query.ts +0 -120
  101. package/src/core/readers/trace_response.ts +0 -80
  102. package/src/core/readers/traces.ts +0 -124
  103. package/src/core/readers/travel_session.ts +0 -88
  104. package/src/core/readers/turn_state.ts +0 -34
  105. package/src/core/readers/world_events.ts +0 -43
  106. package/src/core/readers/world_status.ts +0 -63
  107. package/src/core/reducers/manipulation_args.ts +0 -239
  108. package/src/core/reducers/manipulation_receipt.ts +0 -66
  109. package/src/core/reducers/r_actor_wake_up.ts +0 -32
  110. package/src/core/reducers/r_inscribe_trace.ts +0 -109
  111. package/src/core/reducers/r_manipulate.ts +0 -163
  112. package/src/core/reducers/r_move.ts +0 -155
  113. package/src/core/reducers/r_react.ts +0 -143
  114. package/src/core/reducers/r_release_claim.ts +0 -81
  115. package/src/core/reducers/r_sense.ts +0 -84
  116. package/src/core/reducers/r_session_close.ts +0 -130
  117. package/src/core/reducers/r_session_move.ts +0 -116
  118. package/src/core/reducers/r_session_open.ts +0 -91
  119. package/src/core/reducers/session_lookup.ts +0 -64
  120. package/src/core/reducers/tick.ts +0 -65
  121. package/src/core/reducers/trace_lookup.ts +0 -29
  122. package/src/core/reducers/transport.ts +0 -84
  123. package/src/core/resolve.ts +0 -228
  124. package/src/core/shape_telemetry.ts +0 -146
  125. package/src/core/snapshot_rows.ts +0 -109
  126. package/src/core/tick_cadence.ts +0 -61
  127. package/src/core/types.ts +0 -501
  128. package/src/core/watch_provenance.ts +0 -113
  129. package/src/core/world-contract/classify.ts +0 -38
  130. package/src/core/world-contract/index.ts +0 -12
  131. package/src/core/world-contract/normalise.ts +0 -574
  132. package/src/core/world-contract/runtime.ts +0 -144
  133. package/src/core/world-contract/served.ts +0 -173
  134. package/src/core/world-contract/strict-reducers.ts +0 -14
  135. package/src/core/world-contract/typetext.ts +0 -94
  136. package/src/core/world_time.ts +0 -22
  137. package/src/index.ts +0 -262
  138. package/src/manifest-apply.ts +0 -497
  139. package/src/provenance.ts +0 -568
  140. package/src/reader-manifest.ts +0 -338
  141. package/src/readers.ts +0 -392
  142. package/src/replay.ts +0 -515
  143. package/src/watch-buffer.ts +0 -438
  144. package/src/world-events/builder.ts +0 -1042
@@ -1,64 +0,0 @@
1
- import type { DbConnection } from "@spellkave/bindings";
2
- import { registerReaderManifest, type SnapshotDb } from "../../reader-manifest";
3
- import type { DriverToken } from "../driver_control";
4
- import { rows } from "./_helpers";
5
-
6
- export const requiredTables = [
7
- "visible_actor_driver_control",
8
- "visible_actor_control_transition",
9
- "visible_control_continuation",
10
- "visible_control_continuation_refusal",
11
- "visible_event_steering",
12
- ] as const;
13
- registerReaderManifest({ requiredTables });
14
- type RowOf<T> = T extends { iter(): Iterable<infer R> } ? R : never;
15
- export type DriverControlRow = RowOf<DbConnection["db"]["visibleActorDriverControl"]>;
16
- export function readDriverControl(db: SnapshotDb, body?: string | bigint): DriverControlRow[] {
17
- return rows<DriverControlRow>(
18
- db,
19
- "visibleActorDriverControl",
20
- "visible_actor_driver_control",
21
- ).filter(
22
- (row) =>
23
- body === undefined || String(row.bodyEntityId) === String(body).replace(/^entity:/, ""),
24
- );
25
- }
26
- /** Call with the perception snapshot, then carry the returned immutable value with the decision. */
27
- export function captureDriverToken(db: SnapshotDb, body: string | bigint): DriverToken | undefined {
28
- const row = readDriverControl(db, body)[0];
29
- if (row === undefined || row.tombstone) return undefined;
30
- return Object.freeze({
31
- worldInstanceId: row.worldInstanceId,
32
- actorId: row.actorId,
33
- bodyEntityId: row.bodyEntityId,
34
- generation: row.generation,
35
- });
36
- }
37
- export function readSteeringEvents(db: SnapshotDb) {
38
- return rows<RowOf<DbConnection["db"]["visibleEventSteering"]>>(
39
- db,
40
- "visibleEventSteering",
41
- "visible_event_steering",
42
- );
43
- }
44
- export function readControlTransitions(db: SnapshotDb) {
45
- return rows<RowOf<DbConnection["db"]["visibleActorControlTransition"]>>(
46
- db,
47
- "visibleActorControlTransition",
48
- "visible_actor_control_transition",
49
- );
50
- }
51
- export function readControlContinuations(db: SnapshotDb) {
52
- return rows<RowOf<DbConnection["db"]["visibleControlContinuation"]>>(
53
- db,
54
- "visibleControlContinuation",
55
- "visible_control_continuation",
56
- );
57
- }
58
- export function readControlContinuationRefusals(db: SnapshotDb) {
59
- return rows<RowOf<DbConnection["db"]["visibleControlContinuationRefusal"]>>(
60
- db,
61
- "visibleControlContinuationRefusal",
62
- "visible_control_continuation_refusal",
63
- );
64
- }
@@ -1,219 +0,0 @@
1
- import type { ManifestAuthoredEntityRow } from "../../binding_rows";
2
- import {
3
- type RequiredTables,
4
- registerReaderManifest,
5
- type SnapshotDb,
6
- } from "../../reader-manifest";
7
- import { asString, rows } from "./_helpers";
8
- import { readMetadataAttachmentRows } from "./metadata_rows";
9
-
10
- export const requiredTables: RequiredTables<
11
- [
12
- "public_metadata_attachment",
13
- "visible_metadata_attachment",
14
- "visible_manifest_authored_entity",
15
- "visible_manifest_authored_entity_coverage",
16
- ]
17
- > = [
18
- "public_metadata_attachment",
19
- "visible_metadata_attachment",
20
- "visible_manifest_authored_entity",
21
- "visible_manifest_authored_entity_coverage",
22
- ];
23
- registerReaderManifest({ requiredTables });
24
-
25
- /** Public, application-independent <bundle>.art.image convention (ADR-0073).
26
- * Path starts with its installed adventure asset module, not a host directory. */
27
- export interface EntityArt {
28
- path: string;
29
- hash: string;
30
- license: string;
31
- }
32
-
33
- /** Neutral name for the same projected payload. */
34
- export type PublicSubjectArt = EntityArt;
35
-
36
- /** Admitted `metadata_attachments.target_kind` values for public subject art.
37
- * Hand-mirror of `spellkave_validate::METADATA_TARGET_KINDS` (ADR-0073). */
38
- export const METADATA_TARGET_KINDS = [
39
- "entity",
40
- "entity_definition",
41
- "place_graph",
42
- "place_node",
43
- "place_edge",
44
- "item_definition",
45
- "prompt_definition",
46
- "resource_definition",
47
- "affordance_template",
48
- "capability_definition",
49
- "bundle",
50
- ] as const;
51
-
52
- export type PublicArtTargetKind = (typeof METADATA_TARGET_KINDS)[number];
53
-
54
- const INSTANCE_KINDS = new Set<string>(["entity", "place_node", "place_edge"]);
55
-
56
- export interface PublicArtSubject {
57
- kind: string;
58
- ref: string;
59
- }
60
-
61
- function parseArt(payload: string): EntityArt | undefined {
62
- let value: unknown;
63
- try {
64
- value = JSON.parse(payload);
65
- } catch {
66
- return undefined;
67
- }
68
- if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
69
- const { path, hash, license } = value as Record<string, unknown>;
70
- if (typeof path !== "string" || typeof hash !== "string" || typeof license !== "string")
71
- return undefined;
72
- if (!license.trim() || !/^[a-f0-9]{64}$/.test(hash)) return undefined;
73
- if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*\//.test(path) || /[\\%?#:]/.test(path)) return undefined;
74
- if ([...path].some((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127))
75
- return undefined;
76
- if (path.split("/").some((part) => part === "" || part === "." || part === ".."))
77
- return undefined;
78
- if (!/\.(webp|png|jpe?g)$/i.test(path)) return undefined;
79
- return { path, hash, license };
80
- }
81
-
82
- function pickArt(candidates: EntityArt[]): EntityArt | undefined {
83
- return candidates.sort((a, b) => {
84
- const left = `${a.path}\n${a.hash}\n${a.license}`;
85
- const right = `${b.path}\n${b.hash}\n${b.license}`;
86
- return left < right ? -1 : left > right ? 1 : 0;
87
- })[0];
88
- }
89
-
90
- function isAdmittedKind(kind: string): kind is PublicArtTargetKind {
91
- return (METADATA_TARGET_KINDS as readonly string[]).includes(kind);
92
- }
93
-
94
- /** Public art attachments keyed by `${kind}\0${ref}` — one pass over
95
- * `metadata_attachment` (plus the `visible_manifest_authored_entity` alias for
96
- * `entity` rows), reusable across many subject lookups. Payloads stay raw
97
- * until a lookup asks for that key, so a reader wanting one subject does not
98
- * pay for parsing every art row. Build it once per reader call that
99
- * resolves art for many rows; never cache it across calls (the subscription
100
- * cache mutates underneath). */
101
- export interface PublicArtIndex {
102
- readonly byKey: ReadonlyMap<string, readonly string[]>;
103
- /** Memoised {@link parseArt} verdict per raw payload. */
104
- readonly parsed: Map<string, EntityArt | undefined>;
105
- }
106
-
107
- function indexKey(kind: string, ref: string): string {
108
- return `${kind}\0${ref}`;
109
- }
110
-
111
- export function buildPublicArtIndex(db: SnapshotDb): PublicArtIndex {
112
- // manifest key (`<bundle>:<ref>`) → authored entity ids it names, so an
113
- // `entity` attachment authored by ref also answers a lookup by entity id.
114
- const aliases = new Map<string, string[]>();
115
- for (const entry of rows<ManifestAuthoredEntityRow>(db, "visible_manifest_authored_entity")) {
116
- const key = asString(entry.manifestKey);
117
- const ids = aliases.get(key);
118
- if (ids) ids.push(String(entry.entityId));
119
- else aliases.set(key, [String(entry.entityId)]);
120
- }
121
- const byKey = new Map<string, string[]>();
122
- const add = (kind: string, ref: string, payload: string): void => {
123
- const key = indexKey(kind, ref);
124
- const list = byKey.get(key);
125
- if (list) list.push(payload);
126
- else byKey.set(key, [payload]);
127
- };
128
- // ADR-0130: the raw table is private; read the public and per-caller views.
129
- for (const row of readMetadataAttachmentRows(db)) {
130
- const bundle = asString(row.bundleId);
131
- if (
132
- !bundle ||
133
- asString(row.namespace) !== `${bundle}.art.image` ||
134
- asString(row.audience) !== "public"
135
- )
136
- continue;
137
- const kind = asString(row.targetKind);
138
- if (!isAdmittedKind(kind)) continue;
139
- const ref = asString(row.targetRef);
140
- const payload = asString(row.payloadJson);
141
- add(kind, ref, payload);
142
- if (kind === "entity") {
143
- for (const id of aliases.get(`${bundle}:${ref}`) ?? []) add(kind, id, payload);
144
- }
145
- }
146
- return { byKey, parsed: new Map() };
147
- }
148
-
149
- function parsedArt(index: PublicArtIndex, payload: string): EntityArt | undefined {
150
- if (index.parsed.has(payload)) return index.parsed.get(payload);
151
- const art = parseArt(payload);
152
- index.parsed.set(payload, art);
153
- return art;
154
- }
155
-
156
- /** Same contract as {@link readPublicSubjectArt}, against a prebuilt index. */
157
- export function lookupPublicSubjectArt(
158
- index: PublicArtIndex,
159
- subjects: readonly PublicArtSubject[],
160
- ): EntityArt | undefined {
161
- const instance: EntityArt[] = [];
162
- const definition: EntityArt[] = [];
163
- for (const subject of subjects) {
164
- if (!isAdmittedKind(subject.kind) || subject.ref === "") continue;
165
- const found = index.byKey.get(indexKey(subject.kind, subject.ref));
166
- if (!found) continue;
167
- const bucket = INSTANCE_KINDS.has(subject.kind) ? instance : definition;
168
- for (const payload of found) {
169
- const art = parsedArt(index, payload);
170
- if (art) bucket.push(art);
171
- }
172
- }
173
- return pickArt(instance.length ? instance : definition);
174
- }
175
-
176
- /** Same contract as {@link readPublicEntityArt}, against a prebuilt index. */
177
- export function lookupPublicEntityArt(
178
- index: PublicArtIndex,
179
- entityId: string,
180
- definitionId: string,
181
- ): EntityArt | undefined {
182
- return lookupPublicSubjectArt(index, entitySubjects(entityId, definitionId));
183
- }
184
-
185
- function entitySubjects(entityId: string, definitionId: string): PublicArtSubject[] {
186
- const subjects: PublicArtSubject[] = [{ kind: "entity", ref: entityId }];
187
- if (definitionId !== "") {
188
- subjects.push(
189
- { kind: "entity_definition", ref: definitionId },
190
- { kind: "item_definition", ref: definitionId },
191
- );
192
- }
193
- return subjects;
194
- }
195
-
196
- /** Caller must first authorize every subject. This helper never grants visibility.
197
- * Valid instance-kind attachments (`entity`, `place_node`, `place_edge`) win over
198
- * definition-kind fallbacks. Malformed or private annotations do not mask a
199
- * valid fallback. Ties sort by path/hash/license, independent of subscription order.
200
- * Scans `metadata_attachment` once per call; a reader resolving art for many
201
- * rows builds {@link buildPublicArtIndex} once and uses {@link lookupPublicSubjectArt}. */
202
- export function readPublicSubjectArt(
203
- db: SnapshotDb,
204
- subjects: readonly PublicArtSubject[],
205
- ): EntityArt | undefined {
206
- if (!subjects.some((subject) => isAdmittedKind(subject.kind) && subject.ref !== "")) {
207
- return undefined;
208
- }
209
- return lookupPublicSubjectArt(buildPublicArtIndex(db), subjects);
210
- }
211
-
212
- /** Entity/item helper: authorized instance first, then definition / item_definition. */
213
- export function readPublicEntityArt(
214
- db: SnapshotDb,
215
- entityId: string,
216
- definitionId: string,
217
- ): EntityArt | undefined {
218
- return readPublicSubjectArt(db, entitySubjects(entityId, definitionId));
219
- }
@@ -1,119 +0,0 @@
1
- // Reader: entity containment (post Phase 2 entity-unification, Item 3).
2
- //
3
- // `entity_containment` replaces the dissolved `entity_inventory` table.
4
- // Each row records that an entity (`entity_id`) is held inside a container
5
- // entity (`container_id`). Items in an actor's loose inventory have an empty
6
- // `slot_id`; items slotted into equipment have a non-empty `slot_id`.
7
-
8
- import type { EntityContainmentRow as DbEntityContainmentRow } from "../../binding_rows";
9
- import {
10
- type RequiredTables,
11
- registerReaderManifest,
12
- type SnapshotDb,
13
- } from "../../reader-manifest";
14
- import type { EntityRef } from "../ids";
15
- import {
16
- asNumber,
17
- asString,
18
- buildEntitySummaryMap,
19
- optString,
20
- resolveEntityId,
21
- rows,
22
- } from "./_helpers";
23
-
24
- // visible_manifest_authored_entity is required so a caller may pass a manifest key
25
- // (e.g. "core/player") instead of a numeric entity id — resolveEntityId reads it.
26
- // entity is required for the #1227 name/kind/entity_def_id/quantity join below.
27
- export const requiredTables: RequiredTables<
28
- [
29
- "visible_entity_containment",
30
- "visible_manifest_authored_entity",
31
- "visible_manifest_authored_entity_coverage",
32
- "entity",
33
- ]
34
- > = [
35
- "visible_entity_containment",
36
- "visible_manifest_authored_entity",
37
- "visible_manifest_authored_entity_coverage",
38
- "entity",
39
- ];
40
- registerReaderManifest({ requiredTables });
41
-
42
- const EMPTY_SUMMARY = { name: "", entity_def_id: "", kind: "", quantity: 0 };
43
-
44
- export interface EntityContainmentRow {
45
- entity_id: string;
46
- container_id: string;
47
- slot_id: string;
48
- /** Authored attach direction, e.g. for a mounted/anchored container slot.
49
- * Omitted (not "") when unauthored. */
50
- direction?: string;
51
- inserted_at_tick: number;
52
- /**
53
- * The contained entity's identity, joined from the `entity` table (#1227:
54
- * a bare `entity_id` forced a caller into a per-item follow-up call —
55
- * `spellkave_object_state`, which dead-ends on `[]` for gear with no
56
- * state pools — just to learn what it was holding). Falls back to the
57
- * empty-string/zero shape on the defensive case of a dangling containment
58
- * row with no matching `entity` row.
59
- */
60
- name: string;
61
- entity_def_id: string;
62
- kind: string;
63
- quantity: number;
64
- }
65
-
66
- /**
67
- * Read all containment rows for a given container entity (i.e., all entities
68
- * held by `containerEntityId`). Accepts a numeric id, `entity:N`, or a manifest
69
- * key ("core/player"). Returns `[]` when the table is empty, the ref does not
70
- * resolve, or the container has no contents.
71
- */
72
- export function readEntityContainment(
73
- db: SnapshotDb,
74
- containerEntityId: EntityRef,
75
- ): EntityContainmentRow[] {
76
- const containerIdBigInt = resolveEntityId(db, containerEntityId);
77
- if (containerIdBigInt === undefined) return [];
78
- const summaries = buildEntitySummaryMap(db);
79
- return rows<DbEntityContainmentRow>(db, "visible_entity_containment")
80
- .filter((r) => r.containerId === containerIdBigInt)
81
- .map((r) => {
82
- const direction = optString(r.direction);
83
- return {
84
- entity_id: String(r.entityId),
85
- container_id: String(r.containerId),
86
- slot_id: asString(r.slotId),
87
- ...(direction === undefined ? {} : { direction }),
88
- inserted_at_tick: asNumber(r.insertedAtTick),
89
- ...(summaries.get(String(r.entityId)) ?? EMPTY_SUMMARY),
90
- };
91
- });
92
- }
93
-
94
- /**
95
- * Read the containment row for a specific entity, if it is contained.
96
- * Accepts a numeric id, `entity:N`, or a manifest key. Returns `null` when the
97
- * ref does not resolve or `entityId` is not in any container.
98
- */
99
- export function readEntityContainer(
100
- db: SnapshotDb,
101
- entityId: EntityRef,
102
- ): EntityContainmentRow | null {
103
- const entityIdBigInt = resolveEntityId(db, entityId);
104
- if (entityIdBigInt === undefined) return null;
105
- const found = rows<DbEntityContainmentRow>(db, "visible_entity_containment").find(
106
- (r) => r.entityId === entityIdBigInt,
107
- );
108
- if (!found) return null;
109
- const summary = buildEntitySummaryMap(db).get(String(found.entityId)) ?? EMPTY_SUMMARY;
110
- const direction = optString(found.direction);
111
- return {
112
- entity_id: String(found.entityId),
113
- container_id: String(found.containerId),
114
- slot_id: asString(found.slotId),
115
- ...(direction === undefined ? {} : { direction }),
116
- inserted_at_tick: asNumber(found.insertedAtTick),
117
- ...summary,
118
- };
119
- }
@@ -1,99 +0,0 @@
1
- import type {
2
- EntityEquipmentSlotRow,
3
- EntityRow,
4
- EquipmentSlotDefinitionRow,
5
- } from "../../binding_rows";
6
- import {
7
- type RequiredTables,
8
- registerReaderManifest,
9
- type SnapshotDb,
10
- } from "../../reader-manifest";
11
- import type { EntityRef } from "../ids";
12
- import { asNumber, asString, resolveEntityId, rows } from "./_helpers";
13
-
14
- export const requiredTables: RequiredTables<
15
- [
16
- "visible_entity_equipment_slot",
17
- "equipment_slot_definition",
18
- "entity",
19
- "visible_manifest_authored_entity",
20
- "visible_manifest_authored_entity_coverage",
21
- ]
22
- > = [
23
- "visible_entity_equipment_slot",
24
- "equipment_slot_definition",
25
- "entity",
26
- "visible_manifest_authored_entity",
27
- "visible_manifest_authored_entity_coverage",
28
- ];
29
- registerReaderManifest({ requiredTables });
30
-
31
- export interface EquipmentSlotRow {
32
- slot_id: string;
33
- label: string;
34
- default_compatibility_predicate: string;
35
- slot_bundle_id: string;
36
- equipped_entity_id: string;
37
- equipped_entity_key: string | null;
38
- equipped_entity_def_id: string;
39
- equipped_entity_name: string;
40
- equipped_at_tick: number;
41
- }
42
-
43
- export interface EquipmentSnapshot {
44
- actor_key: string;
45
- actor_entity_id: string;
46
- slots: EquipmentSlotRow[];
47
- }
48
-
49
- export function readEquipment(db: SnapshotDb, actorKey: EntityRef): EquipmentSnapshot | null {
50
- const actorEntityId = resolveEntityId(db, actorKey);
51
- const equipmentRows = rows<EntityEquipmentSlotRow>(db, "visible_entity_equipment_slot");
52
- const slotDefs = new Map(
53
- rows<EquipmentSlotDefinitionRow>(db, "equipment_slot_definition").map((r) => [r.slotId, r]),
54
- );
55
- const entities = rows<EntityRow>(db, "entity");
56
- const authored = rows<{ manifestKey: string; entityId: bigint }>(
57
- db,
58
- "visible_manifest_authored_entity",
59
- );
60
- if (actorEntityId === undefined) return null;
61
-
62
- return {
63
- actor_key: actorKey,
64
- actor_entity_id: String(actorEntityId),
65
- slots: equipmentRows
66
- .filter((r) => r.entityId === actorEntityId)
67
- .map((r) => toEquipmentSlotRow(r, slotDefs.get(r.slotId), entities, authored)),
68
- };
69
- }
70
-
71
- function toEquipmentSlotRow(
72
- row: EntityEquipmentSlotRow,
73
- slotDef: EquipmentSlotDefinitionRow | undefined,
74
- entities: EntityRow[],
75
- authored: { manifestKey: string; entityId: bigint }[],
76
- ): EquipmentSlotRow {
77
- const equipped = entities.find((r) => r.entityId === row.equippedEntityId);
78
- return {
79
- slot_id: asString(row.slotId),
80
- label: slotDef ? asString(slotDef.label) : "",
81
- default_compatibility_predicate: slotDef ? asString(slotDef.defaultCompatibilityPredicate) : "",
82
- slot_bundle_id: slotDef ? asString(slotDef.bundleId) : "",
83
- equipped_entity_id: String(row.equippedEntityId),
84
- equipped_entity_key: entityKeyFor(authored, row.equippedEntityId),
85
- equipped_entity_def_id: equipped ? asString(equipped.entityDefId) : "",
86
- equipped_entity_name: equipped ? asString(equipped.name) : "",
87
- equipped_at_tick: asNumber(row.equippedAtTick),
88
- };
89
- }
90
-
91
- function entityKeyFor(
92
- authored: { manifestKey: string; entityId: bigint }[],
93
- entityId: bigint,
94
- ): string | null {
95
- const row = authored.find((r) => r.entityId === entityId);
96
- if (!row) return null;
97
- const idx = row.manifestKey.indexOf(":");
98
- return idx >= 0 ? row.manifestKey.slice(idx + 1) : row.manifestKey;
99
- }
@@ -1,136 +0,0 @@
1
- import type { PendingResolutionRow, PendingStochasticResolutionRow } from "../../binding_rows";
2
- import {
3
- type RequiredTables,
4
- registerReaderManifest,
5
- type SnapshotDb,
6
- } from "../../reader-manifest";
7
- import type { EntityRef } from "../ids";
8
- import { asNumber, asString, resolveEntityId, rows } from "./_helpers";
9
-
10
- export const requiredTables: RequiredTables<
11
- [
12
- "visible_pending_resolution",
13
- "visible_pending_stochastic_resolution",
14
- "visible_manifest_authored_entity",
15
- "visible_manifest_authored_entity_coverage",
16
- ]
17
- > = [
18
- "visible_pending_resolution",
19
- "visible_pending_stochastic_resolution",
20
- "visible_manifest_authored_entity",
21
- "visible_manifest_authored_entity_coverage",
22
- ];
23
- registerReaderManifest({ requiredTables });
24
-
25
- /** One already-drawn, already-visible die face recorded on the parked window,
26
- * in draw order (index 0 first). Two entries mean the underlying roll was
27
- * made with Advantage/Disadvantage (two independent draws land before any
28
- * decision); one entry is the common case. */
29
- export interface InterventionWindowFace {
30
- source_id: string;
31
- value: number;
32
- }
33
-
34
- /** A currently OPEN post-roll intervention window belonging to one of the
35
- * connected actor's own stochastic actions — the pause between a stochastic
36
- * roll parking and its outcome committing. Distinct from the general
37
- * multi-actor reaction-seat mechanism ({@link ReactionOptionProjection}):
38
- * this row has no `visible_pending_resolution_reactor` seat at all — the
39
- * eligible responder is `target_actor` on the parked `pending_resolution`
40
- * row itself. */
41
- export interface InterventionWindowProjection {
42
- /** Internal typed reducer argument. Presentation layers must hide this id. */
43
- pending_id: bigint;
44
- originating_action_id: string;
45
- originating_actor: string;
46
- target_actor: string;
47
- session_id: bigint;
48
- window_opens_at_micros: number;
49
- window_closes_at_micros: number;
50
- depth: number;
51
- bundle_id: string;
52
- /** The face(s) already drawn for this window, in draw order. */
53
- faces: InterventionWindowFace[];
54
- }
55
-
56
- function bigint(value: unknown): bigint {
57
- return typeof value === "bigint" ? value : BigInt(String(value ?? 0));
58
- }
59
-
60
- /**
61
- * Return every currently open intervention window the connected actor is
62
- * eligible to respond to: a `visible_pending_resolution` row whose
63
- * `target_actor` resolves to this actor AND that has a sibling
64
- * `visible_pending_stochastic_resolution` row (the frozen draws — what makes
65
- * this an interveneable stochastic window rather than a plain multi-actor
66
- * reaction seat, which this reader deliberately does not surface; that shape
67
- * stays `readReactionOptionsForActor`'s job).
68
- */
69
- export function readInterventionWindowsForActor(
70
- db: SnapshotDb,
71
- actorEntityRef: EntityRef,
72
- ): InterventionWindowProjection[] {
73
- const actorEntityId = resolveEntityId(db, actorEntityRef);
74
- if (actorEntityId === undefined) return [];
75
- const actorEntityIdText = String(actorEntityId);
76
-
77
- const stochasticByPendingId = new Map<string, PendingStochasticResolutionRow>();
78
- for (const row of rows<PendingStochasticResolutionRow>(
79
- db,
80
- "visible_pending_stochastic_resolution",
81
- )) {
82
- const raw = row as unknown as { pendingId: unknown };
83
- stochasticByPendingId.set(String(raw.pendingId), row);
84
- }
85
-
86
- const windows: InterventionWindowProjection[] = [];
87
- for (const pending of rows<PendingResolutionRow>(db, "visible_pending_resolution")) {
88
- const row = pending as unknown as {
89
- pendingId: unknown;
90
- originatingActionId: unknown;
91
- originatingActor: unknown;
92
- targetActor: unknown;
93
- sessionId: unknown;
94
- windowOpensAtMicros: unknown;
95
- windowClosesAtMicros: unknown;
96
- depth: unknown;
97
- bundleId: unknown;
98
- };
99
- if (asString(row.targetActor) !== actorEntityIdText) continue;
100
- const stochastic = stochasticByPendingId.get(String(row.pendingId));
101
- if (stochastic === undefined) continue;
102
- const stochasticRaw = stochastic as unknown as { draws: unknown };
103
- const draws = Array.isArray(stochasticRaw.draws) ? stochasticRaw.draws : [];
104
- windows.push({
105
- pending_id: bigint(row.pendingId),
106
- originating_action_id: asString(row.originatingActionId),
107
- originating_actor: asString(row.originatingActor),
108
- target_actor: asString(row.targetActor),
109
- session_id: bigint(row.sessionId),
110
- window_opens_at_micros: asNumber(row.windowOpensAtMicros),
111
- window_closes_at_micros: asNumber(row.windowClosesAtMicros),
112
- depth: asNumber(row.depth),
113
- bundle_id: asString(row.bundleId),
114
- faces: draws
115
- .map((draw) => {
116
- const d = draw as { sourceId?: unknown; drawIndex?: unknown; value?: unknown };
117
- return {
118
- drawIndex: asNumber(d.drawIndex),
119
- source_id: asString(d.sourceId),
120
- value: asNumber(d.value),
121
- };
122
- })
123
- .sort((a, b) => a.drawIndex - b.drawIndex)
124
- .map(({ source_id, value }) => ({ source_id, value })),
125
- });
126
- }
127
- return windows.sort((a, b) =>
128
- a.window_closes_at_micros !== b.window_closes_at_micros
129
- ? a.window_closes_at_micros - b.window_closes_at_micros
130
- : a.pending_id < b.pending_id
131
- ? -1
132
- : a.pending_id > b.pending_id
133
- ? 1
134
- : 0,
135
- );
136
- }