@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,275 +0,0 @@
1
- import type {
2
- ActorSpatialLocationRow,
3
- EntityLocationRow,
4
- ObjectStateRow,
5
- PlaceEdgeRow,
6
- PlaceNodeRow,
7
- } from "../../binding_rows";
8
- import {
9
- type RequiredTables,
10
- registerReaderManifest,
11
- type SnapshotDb,
12
- } from "../../reader-manifest";
13
- import type { EntityRef } from "../ids";
14
- import {
15
- asString,
16
- buildEntityDescriptionMap,
17
- buildEntitySummaryMap,
18
- type EntitySummary,
19
- resolveEntityId,
20
- rows,
21
- tagString,
22
- } from "./_helpers";
23
- import { type ActorConditionRow, buildEntityConditionsMap } from "./actor_snapshot";
24
- import {
25
- buildPublicArtIndex,
26
- type EntityArt,
27
- lookupPublicEntityArt,
28
- lookupPublicSubjectArt,
29
- } from "./entity_art";
30
- import { type LocationSnapshot, readLocationSnapshot } from "./location_snapshot";
31
- import { readTracesAt } from "./traces";
32
-
33
- /** Public hero-owned appearance prose (creation adopt → object_state Text). */
34
- const APPEARANCE_STATE_ID = "srd/appearance";
35
-
36
- export const requiredTables: RequiredTables<
37
- [
38
- "visible_actor_spatial_location",
39
- "visible_place_node",
40
- "visible_place_edge",
41
- "place_edge_affordance",
42
- "visible_place_graph_definition",
43
- "entity",
44
- "visible_entity_tag",
45
- "visible_entity_tag_coverage",
46
- "visible_entity_location",
47
- "visible_trace",
48
- "visible_target_condition",
49
- "entity_description",
50
- "visible_object_state",
51
- "public_metadata_attachment",
52
- "visible_metadata_attachment",
53
- "visible_manifest_authored_entity",
54
- "visible_manifest_authored_entity_coverage",
55
- ]
56
- > = [
57
- "visible_actor_spatial_location",
58
- "visible_place_node",
59
- "visible_place_edge",
60
- "place_edge_affordance",
61
- "visible_place_graph_definition",
62
- "entity",
63
- "visible_entity_tag",
64
- "visible_entity_tag_coverage",
65
- "visible_entity_location",
66
- "visible_trace",
67
- "visible_target_condition",
68
- "entity_description",
69
- "visible_object_state",
70
- "public_metadata_attachment",
71
- "visible_metadata_attachment",
72
- "visible_manifest_authored_entity",
73
- "visible_manifest_authored_entity_coverage",
74
- ];
75
- registerReaderManifest({ requiredTables });
76
-
77
- export type SceneTargetKind = "actor" | "entity" | "exit" | "trace";
78
-
79
- export interface SceneTarget {
80
- kind: SceneTargetKind;
81
- id: string;
82
- label: string;
83
- detail: Record<string, unknown>;
84
- }
85
-
86
- export interface CurrentSceneProjection {
87
- node_id: string;
88
- /** Authored display name of the current place ("" when the node has none). */
89
- node_display_name: string;
90
- node_description: string;
91
- graph_description: string;
92
- pose_elevation: number | null;
93
- /** Public place art (`place_node` wins over `place_graph`). */
94
- art?: EntityArt;
95
- counts: Record<SceneTargetKind, number>;
96
- targets: SceneTarget[];
97
- }
98
-
99
- // U4 (tool-ux-decision-log.md): scene_detail on a co-located actor used to
100
- // deliver only {entity_id, name, faction} — nothing beyond the listing row,
101
- // so the drill-down call cost but never paid off. `description` (authored
102
- // sideband prose, `entity_description`, #1968), `appearance` (Public Text
103
- // object_state `srd/appearance` from character creation), and `conditions`
104
- // (the SAME visibility-filtered view `readActorSnapshot` surfaces for the
105
- // connected actor's OWN body) close that gap for a THIRD-PARTY target: all
106
- // three fields are always present (never omitted), "" / [] being the
107
- // canonical "nothing authored/applied" shapes — never an error, matching
108
- // every other always-present field on this target (e.g. `faction: null`).
109
- // `entity_def_id` (sk-jay0.1.8.63.9) is no new disclosure — the `entity`
110
- // scene target already carries it via the same `summaries` map below; this
111
- // only closes the gap for the `actor` target.
112
- function actorTargets(
113
- location: LocationSnapshot,
114
- selfEntityId: string,
115
- descriptions: Map<string, string>,
116
- conditions: Map<string, ActorConditionRow[]>,
117
- appearances: Map<string, string>,
118
- summaries: Map<string, EntitySummary>,
119
- ): SceneTarget[] {
120
- return location.actor_details
121
- .filter((actor) => actor.entity_id !== selfEntityId)
122
- .map((actor) => ({
123
- kind: "actor" as const,
124
- id: actor.entity_id,
125
- label: actor.name || `entity:${actor.entity_id}`,
126
- detail: {
127
- entity_id: actor.entity_id,
128
- name: actor.name,
129
- faction: actor.faction,
130
- entity_def_id: summaries.get(actor.entity_id)?.entity_def_id ?? "",
131
- description: descriptions.get(actor.entity_id) ?? "",
132
- appearance: appearances.get(actor.entity_id) ?? "",
133
- conditions: conditions.get(actor.entity_id) ?? [],
134
- },
135
- }));
136
- }
137
-
138
- function dslTextValue(value: unknown): string | null {
139
- if (typeof value === "string") return value;
140
- if (!value || typeof value !== "object") return null;
141
- const record = value as Record<string, unknown>;
142
- if (tagString(record.tag) === "Text") {
143
- const inner = record.value;
144
- return inner === undefined || inner === null ? "" : String(inner);
145
- }
146
- if ("Text" in record) {
147
- const inner = record.Text;
148
- return inner === undefined || inner === null ? "" : String(inner);
149
- }
150
- return null;
151
- }
152
-
153
- function buildAppearanceMap(db: SnapshotDb): Map<string, string> {
154
- const out = new Map<string, string>();
155
- for (const row of rows<ObjectStateRow>(db, "visible_object_state")) {
156
- if (asString(row.stateId) !== APPEARANCE_STATE_ID) continue;
157
- const targetKind = tagString(row.targetKind);
158
- if (targetKind !== "entity" && targetKind !== "Entity") continue;
159
- const text = dslTextValue(row.value);
160
- if (text === null || text.length === 0) continue;
161
- out.set(String(row.targetId), text);
162
- }
163
- return out;
164
- }
165
-
166
- /** Current authorized scene. No caller-supplied node is accepted. */
167
- export function readCurrentScene(
168
- db: SnapshotDb,
169
- actorId: EntityRef,
170
- ): CurrentSceneProjection | null {
171
- const selfId = resolveEntityId(db, actorId);
172
- if (selfId === undefined) return null;
173
- const locationRow = rows<ActorSpatialLocationRow>(db, "visible_actor_spatial_location").find(
174
- (row) => String(row.entityId) === String(selfId),
175
- );
176
- if (locationRow === undefined) return null;
177
- const nodeId = String(locationRow.placeNodeId);
178
- const location = readLocationSnapshot(db, nodeId);
179
- if (location === null) return null;
180
-
181
- const summaries = buildEntitySummaryMap(db);
182
- const actors = actorTargets(
183
- location,
184
- String(selfId),
185
- buildEntityDescriptionMap(db),
186
- buildEntityConditionsMap(db),
187
- buildAppearanceMap(db),
188
- summaries,
189
- );
190
- const actorIds = new Set(location.actors_here);
191
- const entities: SceneTarget[] = rows<EntityLocationRow>(db, "visible_entity_location")
192
- .filter((row) => String(row.placeNodeId) === nodeId)
193
- .filter((row) => !actorIds.has(String(row.entityId)))
194
- .map((row) => {
195
- const id = String(row.entityId);
196
- const summary = summaries.get(id);
197
- return {
198
- kind: "entity" as const,
199
- id,
200
- label: summary?.name || `entity:${id}`,
201
- detail: {
202
- entity_id: id,
203
- name: summary?.name ?? "",
204
- entity_def_id: summary?.entity_def_id ?? "",
205
- entity_kind: summary?.kind ?? "",
206
- quantity: summary?.quantity ?? 0,
207
- },
208
- };
209
- });
210
- const exits: SceneTarget[] = location.outgoing_edges.map((edge) => ({
211
- kind: "exit",
212
- id: edge.edge_id,
213
- label: edge.direction ?? edge.to_node_id,
214
- detail: {
215
- ...(edge as unknown as Record<string, unknown>),
216
- // Destination display name, so a listing can say "Common Room" instead
217
- // of a node id (hosts used to pay a scene_detail round trip for this).
218
- to_display_name: readLocationSnapshot(db, edge.to_node_id)?.node_display_name ?? "",
219
- },
220
- }));
221
- const traces: SceneTarget[] = readTracesAt(db, "place_node", nodeId).map((trace) => ({
222
- kind: "trace",
223
- id: trace.trace_id,
224
- label: trace.kind,
225
- detail: trace as unknown as Record<string, unknown>,
226
- }));
227
- const targets = [...actors, ...entities, ...exits, ...traces].sort(
228
- (a, b) => a.kind.localeCompare(b.kind) || a.id.localeCompare(b.id),
229
- );
230
- // One art index per scene read: a per-target scan of `metadata_attachment`
231
- // was a third of npc-host CPU at rest (sk-jay0.6.15.56.24.5).
232
- const artIndex = buildPublicArtIndex(db);
233
- for (const target of targets) {
234
- if (target.kind === "actor" || target.kind === "entity") {
235
- const art = lookupPublicEntityArt(
236
- artIndex,
237
- target.id,
238
- summaries.get(target.id)?.entity_def_id ?? "",
239
- );
240
- if (art) target.detail.art = art;
241
- continue;
242
- }
243
- if (target.kind === "exit") {
244
- const edge = rows<PlaceEdgeRow>(db, "visible_place_edge").find(
245
- (row) => asString(row.edgeId) === target.id,
246
- );
247
- const edgeKey = edge ? asString(edge.edgeKey) : "";
248
- const art = lookupPublicSubjectArt(artIndex, [
249
- { kind: "place_edge", ref: target.id },
250
- ...(edgeKey !== "" ? [{ kind: "place_edge", ref: edgeKey }] : []),
251
- ]);
252
- if (art) target.detail.art = art;
253
- }
254
- }
255
- const graphId = asString(
256
- rows<PlaceNodeRow>(db, "visible_place_node").find((row) => asString(row.nodeId) === nodeId)
257
- ?.placeGraphId,
258
- );
259
- const placeArt = lookupPublicSubjectArt(artIndex, [
260
- { kind: "place_node", ref: nodeId },
261
- ...(graphId !== "" ? [{ kind: "place_graph", ref: graphId }] : []),
262
- ]);
263
- const counts: Record<SceneTargetKind, number> = { actor: 0, entity: 0, exit: 0, trace: 0 };
264
- for (const target of targets) counts[target.kind] += 1;
265
- return {
266
- node_id: nodeId,
267
- node_display_name: location.node_display_name,
268
- node_description: location.node_description,
269
- graph_description: location.graph_description,
270
- pose_elevation: location.pose_elevation,
271
- ...(placeArt ? { art: placeArt } : {}),
272
- counts,
273
- targets,
274
- };
275
- }
@@ -1,33 +0,0 @@
1
- // Playable in-world seats. Occupant identity is never on this row (ADR-0084).
2
- import type { SeatRow as DbSeatRow } from "../../binding_rows";
3
- import {
4
- type RequiredTables,
5
- registerReaderManifest,
6
- type SnapshotDb,
7
- } from "../../reader-manifest";
8
- import { type ActorId, asActorId, asEntityId, type EntityId } from "../ids";
9
- import { asString, rows } from "./_helpers";
10
-
11
- export const requiredTables: RequiredTables<["seat"]> = ["seat"];
12
- registerReaderManifest({ requiredTables });
13
-
14
- export interface SeatRow {
15
- actor_id: ActorId;
16
- entity_id: EntityId;
17
- name: string;
18
- embodied: boolean;
19
- /** True when any actor_identity binding exists. Not a live-connection flag. */
20
- claimed: boolean;
21
- }
22
-
23
- export function readSeats(db: SnapshotDb): SeatRow[] {
24
- return rows<DbSeatRow>(db, "seat")
25
- .map((row) => ({
26
- actor_id: asActorId(String(row.actorId)),
27
- entity_id: asEntityId(String(row.entityId)),
28
- name: asString(row.name),
29
- embodied: Boolean(row.embodied),
30
- claimed: Boolean(row.claimed),
31
- }))
32
- .sort((a, b) => Number(a.actor_id) - Number(b.actor_id));
33
- }
@@ -1,245 +0,0 @@
1
- import type {
2
- ActorSpatialLocationRow,
3
- ConditionDefinitionDescriptionRow,
4
- ObjectStateRow,
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 { asString, resolveEntityId, rows, tagString } from "./_helpers";
13
- import { type ActorConditionRow, type ActorSnapshot, readActorSnapshot } from "./actor_snapshot";
14
- import { type ActorStateEntry, type ReadActorStatesOptions, readActorStates } from "./actor_states";
15
- import { readActorIdentityBindings, readActorPrincipals } from "./actors_and_bodies";
16
- import type { BoundedCollection } from "./bounded_collection";
17
- import { boundedCollection } from "./bounded_collection";
18
- import { readSurfaceMembership, type SurfaceMembership } from "./surface_membership";
19
-
20
- // `metadata_attachment` backs `readSurfaceMembership`'s `ui.self_summary`
21
- // join (#2119): author-driven `vitals`/`quest` bucket membership, replacing
22
- // the old literal `"core"`/`"quest"` tag filters. The ENRICHED identity facets
23
- // (tag + catalog prefix + description) live on `self_identity.ts`'s
24
- // `readSelfIdentity` (#2119 follow-up, 2026-08-03). That change had also
25
- // stripped identity from the recurring glance entirely, on the reasoning that
26
- // static entity-definition data belongs at session start — but live play
27
- // (sk-0m4.120.34) showed players never made the extra call and could not tell
28
- // their own class/species/background. The player-tool `self_summary` therefore
29
- // re-surfaces the authored facets COMPACTLY (tag + catalog prefix, no
30
- // description, no minted field names) on the glance — the verbose per-facet
31
- // prose stays here at `self_states kind=identity`, both because it is #2119's
32
- // no-prose-on-the-glance intent and because carrying it forced so tight a bound
33
- // that the alphabetical facet sort dropped species. This SDK reader stays
34
- // content-neutral and identity-free — composition is the tool's job (§6).
35
- //
36
- // Appearance / backstory (sk-609b.19) are hero-owned Text object_state rows
37
- // from character creation — always present on the glance as empty string
38
- // when unset (same "" shape as scene_detail description).
39
- const APPEARANCE_STATE_ID = "srd/appearance";
40
- const BACKSTORY_STATE_ID = "srd/backstory";
41
-
42
- export const requiredTables: RequiredTables<
43
- [
44
- "public_metadata_attachment",
45
- "visible_metadata_attachment",
46
- "owned_object_state",
47
- "actor",
48
- "visible_actor_identity",
49
- "directly_controlled_actor_spatial_location",
50
- "condition_definition",
51
- ]
52
- > = [
53
- "public_metadata_attachment",
54
- "visible_metadata_attachment",
55
- "owned_object_state",
56
- "actor",
57
- "visible_actor_identity",
58
- "directly_controlled_actor_spatial_location",
59
- "condition_definition",
60
- ];
61
- registerReaderManifest({ requiredTables });
62
-
63
- /** ADR-0073 namespace for PLAYER self-summary bucket membership — audience
64
- * `public`, one namespace per player surface (#2119). */
65
- const SELF_SUMMARY_NAMESPACE = "ui.self_summary";
66
- const RESOURCE_DEFINITION_TARGET_KIND = "resource_definition";
67
-
68
- export interface CompactSelfState {
69
- state_id: string;
70
- display_name: string;
71
- value: string;
72
- }
73
-
74
- export interface CompactSelfCondition {
75
- condition_definition_id: string;
76
- stack_count: number;
77
- expires_at_tick?: number;
78
- }
79
-
80
- export interface SelfSummaryProjection {
81
- actor_id: EntityRef;
82
- entity_name: string;
83
- current_node_id?: string;
84
- state_count: number;
85
- /** Public appearance prose (`srd/appearance`); `""` when unset. */
86
- appearance: string;
87
- /** Hidden backstory prose (`srd/backstory`) — owner and DM-extent readers; `""` when unset. */
88
- backstory: string;
89
- conditions: BoundedCollection<CompactSelfCondition>;
90
- /** States the bundle authored into the `vitals` slot of `ui.self_summary`
91
- * (e.g. HP, AC, level), ordered by authored `order` then `state_id`
92
- * (#2119). Author-driven — no server-side vocabulary. */
93
- vitals: BoundedCollection<CompactSelfState>;
94
- /** States the bundle authored into the `quest` slot of `ui.self_summary`.
95
- * Empty when a bundle has no quest-tracking states — that is correct, not
96
- * a gap (#2119). */
97
- quest: BoundedCollection<CompactSelfState>;
98
- }
99
-
100
- function compact(state: ActorStateEntry): CompactSelfState {
101
- return { state_id: state.state_id, display_name: state.display_name, value: state.value };
102
- }
103
-
104
- /** States whose `state_id` is a member of `slot` in `membership`, ordered by
105
- * the authored `order` then `state_id` (#2119 dedupe/tie rule — membership
106
- * itself already resolved a `state_id` claimed by two bundles to the lowest
107
- * `order`, `readSurfaceMembership`'s job; this is the final display order). */
108
- function bucketStates(
109
- states: ActorStateEntry[],
110
- membership: Map<string, SurfaceMembership>,
111
- slot: string,
112
- ): ActorStateEntry[] {
113
- return states
114
- .map((state) => ({ state, entry: membership.get(state.state_id) }))
115
- .filter(
116
- (row): row is { state: ActorStateEntry; entry: SurfaceMembership } =>
117
- row.entry !== undefined && row.entry.slot === slot,
118
- )
119
- .sort((a, b) => {
120
- if (a.entry.order !== b.entry.order) return a.entry.order - b.entry.order;
121
- return a.state.state_id < b.state.state_id ? -1 : a.state.state_id > b.state.state_id ? 1 : 0;
122
- })
123
- .map((row) => row.state);
124
- }
125
-
126
- /** Exported for `self_capabilities.ts`, the second consumer of this same
127
- * entity-resolution + directly-controlled-body gate. */
128
- export function isDirectlyControlled(db: SnapshotDb, actorId: EntityRef): boolean {
129
- const entityId = resolveEntityId(db, actorId);
130
- if (entityId === undefined) return false;
131
- if (
132
- rows<ActorSpatialLocationRow>(db, "directly_controlled_actor_spatial_location").some(
133
- (row) => String(row.entityId) === String(entityId),
134
- )
135
- )
136
- return true;
137
- const ownedActorIds = new Set(
138
- readActorIdentityBindings(db)
139
- .filter((row) => row.role !== "co-pilot")
140
- .map((row) => row.actor_id),
141
- );
142
- return readActorPrincipals(db).some(
143
- (actor) => ownedActorIds.has(actor.actor_id) && String(actor.current_body) === String(entityId),
144
- );
145
- }
146
-
147
- function dslTextValue(value: unknown): string | null {
148
- if (typeof value === "string") return value;
149
- if (!value || typeof value !== "object") return null;
150
- const record = value as Record<string, unknown>;
151
- if (tagString(record.tag) === "Text") {
152
- const inner = record.value;
153
- return inner === undefined || inner === null ? "" : String(inner);
154
- }
155
- if ("Text" in record) {
156
- const inner = record.Text;
157
- return inner === undefined || inner === null ? "" : String(inner);
158
- }
159
- return null;
160
- }
161
-
162
- function readOwnedTextState(db: SnapshotDb, entityId: string, stateId: string): string {
163
- for (const row of rows<ObjectStateRow>(db, "owned_object_state")) {
164
- if (asString(row.stateId) !== stateId) continue;
165
- const targetKind = tagString(row.targetKind);
166
- if (targetKind !== "entity" && targetKind !== "Entity") continue;
167
- if (String(row.targetId) !== entityId) continue;
168
- const text = dslTextValue(row.value);
169
- if (text !== null) return text;
170
- }
171
- return "";
172
- }
173
-
174
- /** Compact directly-owned body sheet. Tag interpretation is presentation-only. */
175
- export function readSelfSummary(db: SnapshotDb, actorId: EntityRef): SelfSummaryProjection | null {
176
- if (!isDirectlyControlled(db, actorId)) return null;
177
- const snapshot: ActorSnapshot | null = readActorSnapshot(db, actorId);
178
- if (snapshot === null) return null;
179
- const entityId = resolveEntityId(db, actorId);
180
- const entityKey = entityId === undefined ? "" : String(entityId);
181
- const membership = readSurfaceMembership(
182
- db,
183
- SELF_SUMMARY_NAMESPACE,
184
- RESOURCE_DEFINITION_TARGET_KIND,
185
- );
186
- const vitalsStates = bucketStates(snapshot.states, membership, "vitals");
187
- const questStates = bucketStates(snapshot.states, membership, "quest");
188
- const conditions = snapshot.conditions.map((condition) => ({
189
- condition_definition_id: condition.condition_definition_id,
190
- stack_count: condition.stack_count,
191
- ...(condition.expires_at_tick === undefined
192
- ? {}
193
- : { expires_at_tick: condition.expires_at_tick }),
194
- }));
195
- const out: SelfSummaryProjection = {
196
- actor_id: actorId,
197
- entity_name: snapshot.entity_name,
198
- state_count: snapshot.states.length,
199
- appearance: entityKey === "" ? "" : readOwnedTextState(db, entityKey, APPEARANCE_STATE_ID),
200
- backstory: entityKey === "" ? "" : readOwnedTextState(db, entityKey, BACKSTORY_STATE_ID),
201
- conditions: boundedCollection(conditions, {
202
- tool: "spellkave_self_states",
203
- args: { kind: "condition" },
204
- }),
205
- vitals: boundedCollection(vitalsStates.map(compact), {
206
- tool: "spellkave_self_states",
207
- args: { kind: "state" },
208
- }),
209
- quest: boundedCollection(questStates.map(compact), {
210
- tool: "spellkave_self_states",
211
- args: { kind: "state" },
212
- }),
213
- };
214
- if (snapshot.current_node_id !== undefined) out.current_node_id = snapshot.current_node_id;
215
- return out;
216
- }
217
-
218
- /** Full authorized self-condition projection in stable domain order. */
219
- export function readSelfConditions(
220
- db: SnapshotDb,
221
- actorId: EntityRef,
222
- ): Array<ActorConditionRow & { description?: string }> {
223
- if (!isDirectlyControlled(db, actorId)) return [];
224
- const conditions = readActorSnapshot(db, actorId)?.conditions ?? [];
225
- const descriptions = new Map(
226
- rows<ConditionDefinitionDescriptionRow>(db, "condition_definition_description").map((row) => [
227
- row.conditionDefinitionId,
228
- asString(row.description),
229
- ]),
230
- );
231
- return conditions.map((condition) => {
232
- const description = descriptions.get(condition.condition_definition_id);
233
- return description?.trim() ? { ...condition, description } : condition;
234
- });
235
- }
236
-
237
- /** Full authorized self-state projection; callers apply shared pagination. */
238
- export function readSelfStates(
239
- db: SnapshotDb,
240
- actorId: EntityRef,
241
- options: ReadActorStatesOptions = {},
242
- ): ActorStateEntry[] {
243
- if (!isDirectlyControlled(db, actorId)) return [];
244
- return readActorStates(db, actorId, options);
245
- }