@spellkave/sdk 0.0.0-stage → 0.1.0
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 +155 -2
- package/package.json +63 -4
- package/src/binding_rows.ts +186 -0
- package/src/connection.ts +964 -0
- package/src/core/actor.ts +62 -0
- package/src/core/actor_control.ts +193 -0
- package/src/core/actor_control_query_methods.ts +101 -0
- package/src/core/actor_control_reaction_methods.ts +79 -0
- package/src/core/actor_control_sense_methods.ts +81 -0
- package/src/core/actor_control_source.ts +57 -0
- package/src/core/actor_session_methods.ts +226 -0
- package/src/core/binding_drift.ts +301 -0
- package/src/core/credential.ts +259 -0
- package/src/core/driver_control.ts +54 -0
- package/src/core/effective_environment_state.ts +71 -0
- package/src/core/factored_manipulation_attempt.ts +158 -0
- package/src/core/ids.ts +60 -0
- package/src/core/interaction/canonical.ts +15 -0
- package/src/core/interaction/fingerprint.ts +118 -0
- package/src/core/interaction/index.ts +31 -0
- package/src/core/interaction/intent.ts +284 -0
- package/src/core/interaction/outcome.ts +67 -0
- package/src/core/interaction/rejection_ledger.ts +108 -0
- package/src/core/kernel_contract.generated.ts +10 -0
- package/src/core/log.ts +23 -0
- package/src/core/movement.ts +111 -0
- package/src/core/node-io.ts +208 -0
- package/src/core/paths.ts +220 -0
- package/src/core/profile.ts +270 -0
- package/src/core/profiles.ts +9 -0
- package/src/core/provenance_buffer.ts +102 -0
- package/src/core/readers/_helpers.ts +273 -0
- package/src/core/readers/action_description.ts +17 -0
- package/src/core/readers/action_economy_events.ts +149 -0
- package/src/core/readers/actor_body_swaps.ts +62 -0
- package/src/core/readers/actor_snapshot.ts +197 -0
- package/src/core/readers/actor_states.ts +163 -0
- package/src/core/readers/actors_and_bodies.ts +179 -0
- package/src/core/readers/affordance_cue.ts +59 -0
- package/src/core/readers/affordance_defaults.ts +158 -0
- package/src/core/readers/affordance_group_membership.ts +67 -0
- package/src/core/readers/affordance_input.ts +129 -0
- package/src/core/readers/affordance_reference.ts +41 -0
- package/src/core/readers/apply_consequences.ts +54 -0
- package/src/core/readers/audience_trace_presentation.ts +62 -0
- package/src/core/readers/bounded_collection.ts +29 -0
- package/src/core/readers/bundle_definitions.ts +74 -0
- package/src/core/readers/bundle_entities.ts +62 -0
- package/src/core/readers/co_location.ts +71 -0
- package/src/core/readers/combat_session.ts +124 -0
- package/src/core/readers/combat_session_active.ts +44 -0
- package/src/core/readers/combat_session_feedback.ts +37 -0
- package/src/core/readers/combat_session_grid.ts +117 -0
- package/src/core/readers/combat_session_roster.ts +98 -0
- package/src/core/readers/combat_session_types.ts +51 -0
- package/src/core/readers/condition_deltas.ts +53 -0
- package/src/core/readers/container_contents.ts +92 -0
- package/src/core/readers/dialogue_guide.ts +99 -0
- package/src/core/readers/dialogue_prompt_presentation.ts +90 -0
- package/src/core/readers/driver_control.ts +64 -0
- package/src/core/readers/entity_art.ts +219 -0
- package/src/core/readers/entity_containment.ts +119 -0
- package/src/core/readers/equipment.ts +99 -0
- package/src/core/readers/intervention_windows.ts +136 -0
- package/src/core/readers/inventory_query.ts +145 -0
- package/src/core/readers/item_detail.ts +198 -0
- package/src/core/readers/location_snapshot.ts +216 -0
- package/src/core/readers/manifest_apply_provenance.ts +32 -0
- package/src/core/readers/manifest_export.ts +81 -0
- package/src/core/readers/manipulation_context.ts +475 -0
- package/src/core/readers/map_layers.ts +49 -0
- package/src/core/readers/metadata_rows.ts +56 -0
- package/src/core/readers/object_state.ts +124 -0
- package/src/core/readers/observer_knowledge.ts +41 -0
- package/src/core/readers/observer_place_knowledge.ts +37 -0
- package/src/core/readers/observer_trace_knowledge.ts +45 -0
- package/src/core/readers/outcome_template.ts +41 -0
- package/src/core/readers/place_topology.ts +109 -0
- package/src/core/readers/reaction_options.ts +156 -0
- package/src/core/readers/resource_deltas.ts +47 -0
- package/src/core/readers/scene.ts +275 -0
- package/src/core/readers/seats.ts +33 -0
- package/src/core/readers/self.ts +245 -0
- package/src/core/readers/self_capabilities.ts +159 -0
- package/src/core/readers/self_economy.ts +101 -0
- package/src/core/readers/self_economy_budget.ts +93 -0
- package/src/core/readers/self_evaluations.ts +128 -0
- package/src/core/readers/self_identity.ts +104 -0
- package/src/core/readers/sense_context.ts +112 -0
- package/src/core/readers/session.ts +207 -0
- package/src/core/readers/session_detail.ts +211 -0
- package/src/core/readers/session_status.ts +68 -0
- package/src/core/readers/spatial_environment_state.ts +74 -0
- package/src/core/readers/spatial_niche_attachments.ts +28 -0
- package/src/core/readers/substrate_definitions.ts +27 -0
- package/src/core/readers/suggest_next_action.ts +72 -0
- package/src/core/readers/surface_membership.ts +78 -0
- package/src/core/readers/tag_reads.ts +54 -0
- package/src/core/readers/trace_query.ts +120 -0
- package/src/core/readers/trace_response.ts +80 -0
- package/src/core/readers/traces.ts +124 -0
- package/src/core/readers/travel_session.ts +88 -0
- package/src/core/readers/turn_state.ts +34 -0
- package/src/core/readers/world_events.ts +43 -0
- package/src/core/readers/world_status.ts +63 -0
- package/src/core/reducers/manipulation_args.ts +239 -0
- package/src/core/reducers/manipulation_receipt.ts +66 -0
- package/src/core/reducers/r_actor_wake_up.ts +32 -0
- package/src/core/reducers/r_inscribe_trace.ts +109 -0
- package/src/core/reducers/r_manipulate.ts +163 -0
- package/src/core/reducers/r_move.ts +155 -0
- package/src/core/reducers/r_react.ts +143 -0
- package/src/core/reducers/r_release_claim.ts +81 -0
- package/src/core/reducers/r_sense.ts +84 -0
- package/src/core/reducers/r_session_close.ts +130 -0
- package/src/core/reducers/r_session_move.ts +116 -0
- package/src/core/reducers/r_session_open.ts +91 -0
- package/src/core/reducers/session_lookup.ts +64 -0
- package/src/core/reducers/tick.ts +65 -0
- package/src/core/reducers/trace_lookup.ts +29 -0
- package/src/core/reducers/transport.ts +84 -0
- package/src/core/resolve.ts +228 -0
- package/src/core/shape_telemetry.ts +146 -0
- package/src/core/snapshot_rows.ts +109 -0
- package/src/core/tick_cadence.ts +61 -0
- package/src/core/types.ts +501 -0
- package/src/core/watch_provenance.ts +113 -0
- package/src/core/world-contract/classify.ts +38 -0
- package/src/core/world-contract/index.ts +12 -0
- package/src/core/world-contract/normalise.ts +574 -0
- package/src/core/world-contract/runtime.ts +144 -0
- package/src/core/world-contract/served.ts +173 -0
- package/src/core/world-contract/strict-reducers.ts +14 -0
- package/src/core/world-contract/typetext.ts +94 -0
- package/src/core/world_time.ts +22 -0
- package/src/index.ts +262 -0
- package/src/manifest-apply.ts +497 -0
- package/src/provenance.ts +568 -0
- package/src/reader-manifest.ts +338 -0
- package/src/readers.ts +392 -0
- package/src/replay.ts +515 -0
- package/src/watch-buffer.ts +438 -0
- package/src/world-events/builder.ts +1042 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Reader: enumerate an actor's own object-state pools, enriched with the
|
|
2
|
+
// authored display name + tags from the bundle's resource definitions.
|
|
3
|
+
//
|
|
4
|
+
// This is the "list my states" read surface (#1229). Before it, an actor could
|
|
5
|
+
// only read a pool it already knew the id of (`readObjectStateInt` needs a
|
|
6
|
+
// `stateId`), and `readActorSnapshot` returned a bare sheet with no pools at
|
|
7
|
+
// all — so authored state (hp, a quest stage) was imperceptible to a curious
|
|
8
|
+
// player. Here we enumerate every visible pool the entity carries and left-join:
|
|
9
|
+
// - `resource_definition` → `display_name`, `value_kind`
|
|
10
|
+
// - `resource_definition_tag` → `tags[]`
|
|
11
|
+
// keyed on `state_id == resource_definition_id`.
|
|
12
|
+
//
|
|
13
|
+
// Content-neutrality (ADR-0021, and the same posture the rest of this package
|
|
14
|
+
// holds — see `object_state.ts`): the reader forwards whatever tags the bundle
|
|
15
|
+
// authored (`quest`, `currency`, a modder's `faction`) verbatim. It does NOT
|
|
16
|
+
// mint a `quest` → "objective" classification — the consumer (companion tool,
|
|
17
|
+
// player UI, playtest scorer) interprets a tag's meaning. Surfacing the tag IS
|
|
18
|
+
// the fix; the interpretation is presentation.
|
|
19
|
+
|
|
20
|
+
import type {
|
|
21
|
+
ObjectStateRow,
|
|
22
|
+
ResourceDefinitionRow,
|
|
23
|
+
ResourceDefinitionTagRow,
|
|
24
|
+
} from "../../binding_rows";
|
|
25
|
+
import {
|
|
26
|
+
type RequiredTables,
|
|
27
|
+
registerReaderManifest,
|
|
28
|
+
type SnapshotDb,
|
|
29
|
+
} from "../../reader-manifest";
|
|
30
|
+
import type { EntityRef } from "../ids";
|
|
31
|
+
import { asNumber, asString, resolveEntityId, rows } from "./_helpers";
|
|
32
|
+
|
|
33
|
+
export const requiredTables: RequiredTables<
|
|
34
|
+
[
|
|
35
|
+
"owned_object_state",
|
|
36
|
+
"resource_definition",
|
|
37
|
+
"resource_definition_tag",
|
|
38
|
+
"visible_manifest_authored_entity",
|
|
39
|
+
"visible_manifest_authored_entity_coverage",
|
|
40
|
+
]
|
|
41
|
+
> = [
|
|
42
|
+
"owned_object_state",
|
|
43
|
+
"resource_definition",
|
|
44
|
+
"resource_definition_tag",
|
|
45
|
+
"visible_manifest_authored_entity",
|
|
46
|
+
"visible_manifest_authored_entity_coverage",
|
|
47
|
+
];
|
|
48
|
+
registerReaderManifest({ requiredTables });
|
|
49
|
+
|
|
50
|
+
export interface ActorStateEntry {
|
|
51
|
+
/** Pool id, e.g. `srd/hp` or `phandelver/escort_stage`. */
|
|
52
|
+
state_id: string;
|
|
53
|
+
/** Authored label from `resource_definition`; falls back to `state_id`. */
|
|
54
|
+
display_name: string;
|
|
55
|
+
/** Optional authored resource prose; absent when no description was supplied. */
|
|
56
|
+
description?: string;
|
|
57
|
+
/** Authored kind (`int`, `bool`, …) from `resource_definition`; `""` if unknown. */
|
|
58
|
+
value_kind: string;
|
|
59
|
+
/** Parsed integer value when the pool holds an int; `null` otherwise. */
|
|
60
|
+
value_int: number | null;
|
|
61
|
+
/** Human-readable rendering of the pool value (always present). */
|
|
62
|
+
value: string;
|
|
63
|
+
/** Authored tags (sorted), e.g. `["quest"]`. A consumer decides what they mean. */
|
|
64
|
+
tags: string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ReadActorStatesOptions {
|
|
68
|
+
/**
|
|
69
|
+
* Cap on returned rows, keeping the most-recently-written ones
|
|
70
|
+
* (`set_at_tick` desc, row-insertion-order tiebreak) when there are more
|
|
71
|
+
* matches than the cap. `undefined` or `0` = unbounded (today's full-dump
|
|
72
|
+
* behavior) — existing callers that pass no options see NO change
|
|
73
|
+
* (#1839: the companion tool layer, not this reader, owns the default
|
|
74
|
+
* bounded view; other SDK consumers — web panels, srd-runner proofs —
|
|
75
|
+
* still need the complete list).
|
|
76
|
+
*/
|
|
77
|
+
limit?: number;
|
|
78
|
+
/** Optional filter: only states whose state_id starts with this prefix. */
|
|
79
|
+
statePrefix?: string;
|
|
80
|
+
/** Optional filter: only states carrying this exact author-defined resource tag. */
|
|
81
|
+
stateTag?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* All object-state pools visible on one actor's entity, enriched and sorted by
|
|
86
|
+
* `state_id`. Returns `[]` when the actor id does not resolve to an entity.
|
|
87
|
+
*/
|
|
88
|
+
export function readActorStates(
|
|
89
|
+
db: SnapshotDb,
|
|
90
|
+
actorId: EntityRef,
|
|
91
|
+
opts: ReadActorStatesOptions = {},
|
|
92
|
+
): ActorStateEntry[] {
|
|
93
|
+
const entityId = resolveEntityId(db, actorId);
|
|
94
|
+
if (entityId === undefined) return [];
|
|
95
|
+
const targetId = String(entityId);
|
|
96
|
+
|
|
97
|
+
const definitions = new Map<string, ResourceDefinitionRow>();
|
|
98
|
+
for (const def of rows<ResourceDefinitionRow>(db, "resource_definition")) {
|
|
99
|
+
definitions.set(asString(def.resourceDefinitionId), def);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const tagsByState = new Map<string, string[]>();
|
|
103
|
+
for (const row of rows<ResourceDefinitionTagRow>(db, "resource_definition_tag")) {
|
|
104
|
+
const key = asString(row.resourceDefinitionId);
|
|
105
|
+
const list = tagsByState.get(key) ?? [];
|
|
106
|
+
list.push(asString(row.tag));
|
|
107
|
+
tagsByState.set(key, list);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let matched = rows<ObjectStateRow>(db, "owned_object_state").filter(
|
|
111
|
+
(row) => asString(row.targetKind) === "entity" && String(row.targetId) === targetId,
|
|
112
|
+
);
|
|
113
|
+
matched = matched.filter(
|
|
114
|
+
(row) =>
|
|
115
|
+
(!opts.statePrefix || asString(row.stateId).startsWith(opts.statePrefix)) &&
|
|
116
|
+
(!opts.stateTag || (tagsByState.get(asString(row.stateId)) ?? []).includes(opts.stateTag)),
|
|
117
|
+
);
|
|
118
|
+
if (opts.limit && opts.limit > 0 && matched.length > opts.limit) {
|
|
119
|
+
matched = matched
|
|
120
|
+
.slice()
|
|
121
|
+
.sort((a, b) => {
|
|
122
|
+
const tickDiff = asNumber(b.setAtTick) - asNumber(a.setAtTick);
|
|
123
|
+
return tickDiff !== 0 ? tickDiff : asString(b.key).localeCompare(asString(a.key));
|
|
124
|
+
})
|
|
125
|
+
.slice(0, opts.limit);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return matched
|
|
129
|
+
.map((row) => {
|
|
130
|
+
const stateId = asString(row.stateId);
|
|
131
|
+
const def = definitions.get(stateId);
|
|
132
|
+
const intValue = dslIntValue(row.value);
|
|
133
|
+
const tags = (tagsByState.get(stateId) ?? []).slice().sort();
|
|
134
|
+
return {
|
|
135
|
+
state_id: stateId,
|
|
136
|
+
display_name: def ? asString(def.displayName) : stateId,
|
|
137
|
+
...(def && asString(def.description) ? { description: asString(def.description) } : {}),
|
|
138
|
+
value_kind: def ? asString(def.valueKind) : "",
|
|
139
|
+
value_int: intValue === null ? null : Number(intValue),
|
|
140
|
+
value: intValue === null ? stringifyValue(row.value) : String(intValue),
|
|
141
|
+
tags,
|
|
142
|
+
};
|
|
143
|
+
})
|
|
144
|
+
.sort((a, b) => (a.state_id < b.state_id ? -1 : a.state_id > b.state_id ? 1 : 0));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function dslIntValue(value: unknown): bigint | null {
|
|
148
|
+
if (typeof value === "bigint") return value;
|
|
149
|
+
if (typeof value === "number") return BigInt(value);
|
|
150
|
+
if (typeof value === "string" && /^-?\d+$/.test(value)) return BigInt(value);
|
|
151
|
+
if (!value || typeof value !== "object") return null;
|
|
152
|
+
|
|
153
|
+
const record = value as Record<string, unknown>;
|
|
154
|
+
if (asString(record.tag) === "Int") return dslIntValue(record.value);
|
|
155
|
+
if ("Int" in record) return dslIntValue(record.Int);
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function stringifyValue(value: unknown): string {
|
|
160
|
+
if (value === undefined || value === null) return "";
|
|
161
|
+
if (typeof value !== "object") return String(value);
|
|
162
|
+
return JSON.stringify(value, (_key, v: unknown) => (typeof v === "bigint" ? v.toString() : v));
|
|
163
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
VisibleActorIdentityRow as DbActorIdentityRow,
|
|
3
|
+
ActorRow as DbActorRow,
|
|
4
|
+
EntityRow as DbEntityRow,
|
|
5
|
+
ObjectStateRow as DbObjectStateRow,
|
|
6
|
+
} from "../../binding_rows";
|
|
7
|
+
import {
|
|
8
|
+
type RequiredTables,
|
|
9
|
+
registerReaderManifest,
|
|
10
|
+
type SnapshotDb,
|
|
11
|
+
} from "../../reader-manifest";
|
|
12
|
+
import { type ActorId, asActorId, asEntityId, type EntityId, type EntityRef } from "../ids";
|
|
13
|
+
import {
|
|
14
|
+
asNumber,
|
|
15
|
+
asString,
|
|
16
|
+
optNumber,
|
|
17
|
+
optString,
|
|
18
|
+
resolveEntityId,
|
|
19
|
+
rows,
|
|
20
|
+
tagString,
|
|
21
|
+
} from "./_helpers";
|
|
22
|
+
|
|
23
|
+
// visible_manifest_authored_entity backs resolveEntityId (manifest-key refs). #504 L1.
|
|
24
|
+
// `actor_identity` is private (#915 PR-5); `visible_actor_identity` is its
|
|
25
|
+
// self-only view (ADR-0084 D-5 Family 1 shape) — see `readActorIdentityBindings`.
|
|
26
|
+
export const requiredTables: RequiredTables<
|
|
27
|
+
[
|
|
28
|
+
"actor",
|
|
29
|
+
"visible_actor_identity",
|
|
30
|
+
"entity",
|
|
31
|
+
"visible_object_state",
|
|
32
|
+
"visible_manifest_authored_entity",
|
|
33
|
+
"visible_manifest_authored_entity_coverage",
|
|
34
|
+
]
|
|
35
|
+
> = [
|
|
36
|
+
"actor",
|
|
37
|
+
"visible_actor_identity",
|
|
38
|
+
"entity",
|
|
39
|
+
"visible_object_state",
|
|
40
|
+
"visible_manifest_authored_entity",
|
|
41
|
+
"visible_manifest_authored_entity_coverage",
|
|
42
|
+
];
|
|
43
|
+
registerReaderManifest({ requiredTables });
|
|
44
|
+
|
|
45
|
+
/** BSATN option<ConnectionId> → lowercase hex string, or null when unbound (durable seat). */
|
|
46
|
+
function connectionIdHex(value: unknown): string | null {
|
|
47
|
+
if (value === undefined || value === null) return null;
|
|
48
|
+
const some =
|
|
49
|
+
typeof value === "object" && "some" in (value as object)
|
|
50
|
+
? (value as { some?: unknown }).some
|
|
51
|
+
: value;
|
|
52
|
+
if (some === undefined || some === null) return null;
|
|
53
|
+
const hex = (some as { toHexString?: () => string }).toHexString;
|
|
54
|
+
if (typeof hex === "function") return hex.call(some);
|
|
55
|
+
const s = String(some);
|
|
56
|
+
return s.length > 0 && s !== "[object Object]" ? s : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ActorPrincipalRow {
|
|
60
|
+
actor_id: ActorId;
|
|
61
|
+
name: string;
|
|
62
|
+
current_body: EntityId;
|
|
63
|
+
created_at_tick: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ActorIdentityBindingRow {
|
|
67
|
+
/** ADR-0146 storage key: `[actor_id, identity hex]`. */
|
|
68
|
+
key: string;
|
|
69
|
+
actor_id: ActorId;
|
|
70
|
+
identity: string;
|
|
71
|
+
role: string;
|
|
72
|
+
provenance: string;
|
|
73
|
+
assigned_by?: number;
|
|
74
|
+
bound_at_tick: number;
|
|
75
|
+
/** Null when the binding is durable (trusted IdP or trusted-authorship). */
|
|
76
|
+
connection_id: string | null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface EntityBodyRow {
|
|
80
|
+
entity_id: EntityId;
|
|
81
|
+
entity_def_id: string;
|
|
82
|
+
name: string;
|
|
83
|
+
kind: string;
|
|
84
|
+
quantity: number;
|
|
85
|
+
owned_by_entity_id: string;
|
|
86
|
+
created_at_tick: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface EntityObjectStateRow {
|
|
90
|
+
/** ADR-0146 storage key: `[target_kind, target_id, state_id]`. */
|
|
91
|
+
key: string;
|
|
92
|
+
target_id: string;
|
|
93
|
+
state_id: string;
|
|
94
|
+
value: string;
|
|
95
|
+
source_template_id: string;
|
|
96
|
+
set_at_tick: number;
|
|
97
|
+
expires_at_tick: number;
|
|
98
|
+
bundle_id: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Schema pair (`Actor.actor_id` / `Actor.current_body`). */
|
|
102
|
+
export function readActorPrincipals(db: SnapshotDb): ActorPrincipalRow[] {
|
|
103
|
+
return rows<DbActorRow>(db, "actor").map((row) => ({
|
|
104
|
+
actor_id: asActorId(String(row.actorId)),
|
|
105
|
+
name: asString(row.name),
|
|
106
|
+
current_body: asEntityId(String(row.currentBody)),
|
|
107
|
+
created_at_tick: asNumber(row.createdAtTick),
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* `actor_identity` is private (#915 PR-5 — every login Identity + role
|
|
113
|
+
* binding is deanonymization-grade metadata: who controls which character,
|
|
114
|
+
* who holds a high-trust seat like the DM). This reads the self-only
|
|
115
|
+
* `visible_actor_identity` view (ADR-0084 D-5 Family 1 shape: extent never
|
|
116
|
+
* widens the root set — a DM's class×extent elevation covers world/sheet
|
|
117
|
+
* state, not player login identities), so it now returns only the
|
|
118
|
+
* CALLER'S OWN bindings, never another identity's.
|
|
119
|
+
*
|
|
120
|
+
* This reader returns steering principals (`Actor.actor_id`); neighbouring
|
|
121
|
+
* `readEntityBody` takes an entity ref.
|
|
122
|
+
*/
|
|
123
|
+
export function readActorIdentityBindings(db: SnapshotDb): ActorIdentityBindingRow[] {
|
|
124
|
+
return rows<DbActorIdentityRow>(db, "visible_actor_identity").map((row) => ({
|
|
125
|
+
key: asString(row.key),
|
|
126
|
+
actor_id: asActorId(String(row.actorId)),
|
|
127
|
+
identity: asString(row.identity),
|
|
128
|
+
role: asString(row.role),
|
|
129
|
+
provenance: tagString(row.provenance).toLowerCase(),
|
|
130
|
+
assigned_by: optNumber(row.assignedBy),
|
|
131
|
+
bound_at_tick: asNumber(row.boundAtTick),
|
|
132
|
+
connection_id: connectionIdHex(row.connectionId),
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function readEntityBody(db: SnapshotDb, entityId: EntityRef): EntityBodyRow | null {
|
|
137
|
+
const id = resolveEntityId(db, entityId);
|
|
138
|
+
if (id === undefined) return null;
|
|
139
|
+
const row = rows<DbEntityRow>(db, "entity").find((candidate) => candidate.entityId === id);
|
|
140
|
+
if (!row) return null;
|
|
141
|
+
return {
|
|
142
|
+
entity_id: asEntityId(String(row.entityId)),
|
|
143
|
+
entity_def_id: asString(row.entityDefId),
|
|
144
|
+
name: asString(row.name),
|
|
145
|
+
kind: asString(row.kind),
|
|
146
|
+
quantity: asNumber(row.quantity),
|
|
147
|
+
owned_by_entity_id: String(row.ownedByEntityId),
|
|
148
|
+
created_at_tick: asNumber(row.createdAtTick),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function readObjectStatesForEntity(
|
|
153
|
+
db: SnapshotDb,
|
|
154
|
+
entityId: EntityRef,
|
|
155
|
+
): EntityObjectStateRow[] {
|
|
156
|
+
// Resolve to the numeric targetId form first, else the string compare below
|
|
157
|
+
// silently matches nothing for a manifest key (quiet sibling of BigInt throw).
|
|
158
|
+
const resolved = resolveEntityId(db, entityId);
|
|
159
|
+
if (resolved === undefined) return [];
|
|
160
|
+
const targetId = String(resolved);
|
|
161
|
+
return rows<DbObjectStateRow>(db, "visible_object_state")
|
|
162
|
+
.filter((row) => asString(row.targetKind) === "entity" && asString(row.targetId) === targetId)
|
|
163
|
+
.map((row) => ({
|
|
164
|
+
key: asString(row.key),
|
|
165
|
+
target_id: asString(row.targetId),
|
|
166
|
+
state_id: asString(row.stateId),
|
|
167
|
+
value: stringifyValue(row.value),
|
|
168
|
+
source_template_id: asString(row.sourceTemplateId),
|
|
169
|
+
set_at_tick: asNumber(row.setAtTick),
|
|
170
|
+
expires_at_tick: asNumber(row.expiresAtTick),
|
|
171
|
+
bundle_id: asString(row.bundleId),
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function stringifyValue(value: unknown): string {
|
|
176
|
+
if (value === undefined || value === null) return "";
|
|
177
|
+
if (typeof value !== "object") return String(value);
|
|
178
|
+
return JSON.stringify(value, (_key, v: unknown) => (typeof v === "bigint" ? v.toString() : v));
|
|
179
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RequiredTables,
|
|
3
|
+
registerReaderManifest,
|
|
4
|
+
type SnapshotDb,
|
|
5
|
+
} from "../../reader-manifest";
|
|
6
|
+
import { asString } from "./_helpers";
|
|
7
|
+
import { readMetadataAttachmentRows } from "./metadata_rows";
|
|
8
|
+
|
|
9
|
+
export const requiredTables: RequiredTables<
|
|
10
|
+
["public_metadata_attachment", "visible_metadata_attachment"]
|
|
11
|
+
> = ["public_metadata_attachment", "visible_metadata_attachment"];
|
|
12
|
+
registerReaderManifest({ requiredTables });
|
|
13
|
+
|
|
14
|
+
const PUBLIC_AUDIENCE = "public";
|
|
15
|
+
const AFFORDANCE_TEMPLATE_TARGET_KIND = "affordance_template";
|
|
16
|
+
const AFFORDANCE_CUE_NAMESPACE = "ui.affordance_cue";
|
|
17
|
+
|
|
18
|
+
/** A public authoring hint. It ranks or explains a disclosed action, but does
|
|
19
|
+
* not claim eligibility, bind a target, or carry response-contract context. */
|
|
20
|
+
export interface AffordanceCue {
|
|
21
|
+
rank?: number;
|
|
22
|
+
hint?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function cueFromPayload(payloadJson: unknown): AffordanceCue | undefined {
|
|
26
|
+
let payload: unknown;
|
|
27
|
+
try {
|
|
28
|
+
payload = JSON.parse(asString(payloadJson));
|
|
29
|
+
} catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) return undefined;
|
|
33
|
+
const row = payload as Record<string, unknown>;
|
|
34
|
+
const rank =
|
|
35
|
+
typeof row.rank === "number" && Number.isInteger(row.rank) && row.rank >= 0 && row.rank <= 9
|
|
36
|
+
? row.rank
|
|
37
|
+
: undefined;
|
|
38
|
+
const hint = typeof row.hint === "string" && row.hint.trim() !== "" ? row.hint : undefined;
|
|
39
|
+
return rank === undefined && hint === undefined
|
|
40
|
+
? undefined
|
|
41
|
+
: { ...(rank === undefined ? {} : { rank }), ...(hint === undefined ? {} : { hint }) };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Reads public template cues. The first authored source template remains the
|
|
45
|
+
* sole join authority in manipulation_context, matching input/reference UI. */
|
|
46
|
+
export function readAffordanceCues(db: SnapshotDb): Map<string, AffordanceCue> {
|
|
47
|
+
const out = new Map<string, AffordanceCue>();
|
|
48
|
+
for (const row of readMetadataAttachmentRows(db)) {
|
|
49
|
+
if (
|
|
50
|
+
asString(row.namespace) !== AFFORDANCE_CUE_NAMESPACE ||
|
|
51
|
+
asString(row.targetKind) !== AFFORDANCE_TEMPLATE_TARGET_KIND ||
|
|
52
|
+
asString(row.audience) !== PUBLIC_AUDIENCE
|
|
53
|
+
)
|
|
54
|
+
continue;
|
|
55
|
+
const cue = cueFromPayload(row.payloadJson);
|
|
56
|
+
if (cue !== undefined) out.set(asString(row.targetRef), cue);
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RequiredTables,
|
|
3
|
+
registerReaderManifest,
|
|
4
|
+
type SnapshotDb,
|
|
5
|
+
} from "../../reader-manifest";
|
|
6
|
+
import { asString } from "./_helpers";
|
|
7
|
+
import type { AffordanceInputSchema } from "./affordance_input";
|
|
8
|
+
import { readMetadataAttachmentRows } from "./metadata_rows";
|
|
9
|
+
|
|
10
|
+
export const requiredTables: RequiredTables<
|
|
11
|
+
["public_metadata_attachment", "visible_metadata_attachment"]
|
|
12
|
+
> = ["public_metadata_attachment", "visible_metadata_attachment"];
|
|
13
|
+
registerReaderManifest({ requiredTables });
|
|
14
|
+
|
|
15
|
+
export const AFFORDANCE_DEFAULTS_NAMESPACE = "ui.affordance_defaults";
|
|
16
|
+
const PUBLIC_AUDIENCE = "public";
|
|
17
|
+
const AFFORDANCE_TEMPLATE_TARGET_KIND = "affordance_template";
|
|
18
|
+
|
|
19
|
+
/** Author-published draft values. They are never an eligibility assertion,
|
|
20
|
+
* reducer fallback, prompt response contract, or dispatch route. */
|
|
21
|
+
export interface AffordanceDefaults {
|
|
22
|
+
target?: { kind: "actor" } | { kind: "reference"; target_ref: string };
|
|
23
|
+
input_values?: Record<string, string | number>;
|
|
24
|
+
provenance: {
|
|
25
|
+
namespace: typeof AFFORDANCE_DEFAULTS_NAMESPACE;
|
|
26
|
+
template_id: string;
|
|
27
|
+
attachment_id: string;
|
|
28
|
+
/** Changes when the authored attachment changes, even if the surrounding
|
|
29
|
+
* manipulation-context revision is unchanged. */
|
|
30
|
+
content_key: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function parseInputValues(
|
|
35
|
+
value: unknown,
|
|
36
|
+
schema: AffordanceInputSchema | undefined,
|
|
37
|
+
): Record<string, string | number> | undefined {
|
|
38
|
+
if (value === undefined) return {};
|
|
39
|
+
if (value === null || typeof value !== "object" || Array.isArray(value) || schema === undefined)
|
|
40
|
+
return undefined;
|
|
41
|
+
const fields = new Map(schema.fields.map((field) => [field.name, field]));
|
|
42
|
+
const values: Record<string, string | number> = {};
|
|
43
|
+
for (const [name, raw] of Object.entries(value as Record<string, unknown>)) {
|
|
44
|
+
// Transport and occurrence identity are never an authored input default,
|
|
45
|
+
// even if an accidentally named form field exists in a fixture.
|
|
46
|
+
if (
|
|
47
|
+
[
|
|
48
|
+
"semantic_context",
|
|
49
|
+
"occurrence_id",
|
|
50
|
+
"source_ref",
|
|
51
|
+
"action_id",
|
|
52
|
+
"target_ref",
|
|
53
|
+
"participants",
|
|
54
|
+
].includes(name)
|
|
55
|
+
)
|
|
56
|
+
return undefined;
|
|
57
|
+
const field = fields.get(name);
|
|
58
|
+
if (field === undefined) return undefined;
|
|
59
|
+
if (field.control === "number") {
|
|
60
|
+
if (typeof raw !== "number" || !Number.isFinite(raw)) return undefined;
|
|
61
|
+
if (
|
|
62
|
+
(field.min !== undefined && raw < field.min) ||
|
|
63
|
+
(field.max !== undefined && raw > field.max)
|
|
64
|
+
)
|
|
65
|
+
return undefined;
|
|
66
|
+
values[name] = raw;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (typeof raw !== "string") return undefined;
|
|
70
|
+
if (field.maxLength !== undefined && raw.length > field.maxLength) return undefined;
|
|
71
|
+
values[name] = raw;
|
|
72
|
+
}
|
|
73
|
+
return values;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function parseDefaults(
|
|
77
|
+
payloadJson: unknown,
|
|
78
|
+
templateId: string,
|
|
79
|
+
attachmentId: string,
|
|
80
|
+
setAtTick: unknown,
|
|
81
|
+
schema: AffordanceInputSchema | undefined,
|
|
82
|
+
): AffordanceDefaults | undefined {
|
|
83
|
+
let payload: unknown;
|
|
84
|
+
try {
|
|
85
|
+
payload = JSON.parse(asString(payloadJson));
|
|
86
|
+
} catch {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) return undefined;
|
|
90
|
+
const row = payload as Record<string, unknown>;
|
|
91
|
+
if (row.version !== 1) return undefined;
|
|
92
|
+
let target: AffordanceDefaults["target"];
|
|
93
|
+
if (row.target !== undefined) {
|
|
94
|
+
if (row.target === null || typeof row.target !== "object" || Array.isArray(row.target))
|
|
95
|
+
return undefined;
|
|
96
|
+
const targetRow = row.target as Record<string, unknown>;
|
|
97
|
+
if (targetRow.kind === "actor" && Object.keys(targetRow).length === 1)
|
|
98
|
+
target = { kind: "actor" };
|
|
99
|
+
else if (
|
|
100
|
+
targetRow.kind === "reference" &&
|
|
101
|
+
typeof targetRow.target_ref === "string" &&
|
|
102
|
+
targetRow.target_ref.trim() !== "" &&
|
|
103
|
+
Object.keys(targetRow).length === 2
|
|
104
|
+
)
|
|
105
|
+
target = { kind: "reference", target_ref: targetRow.target_ref };
|
|
106
|
+
else return undefined;
|
|
107
|
+
}
|
|
108
|
+
const inputValues = parseInputValues(row.input_values, schema);
|
|
109
|
+
if (inputValues === undefined || (target === undefined && Object.keys(inputValues).length === 0))
|
|
110
|
+
return undefined;
|
|
111
|
+
return {
|
|
112
|
+
...(target === undefined ? {} : { target }),
|
|
113
|
+
...(Object.keys(inputValues).length === 0 ? {} : { input_values: inputValues }),
|
|
114
|
+
provenance: {
|
|
115
|
+
namespace: AFFORDANCE_DEFAULTS_NAMESPACE,
|
|
116
|
+
template_id: templateId,
|
|
117
|
+
attachment_id: attachmentId,
|
|
118
|
+
content_key: `${attachmentId}:${asString(setAtTick)}:${asString(payloadJson)}`,
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Read one bounded author-published default per source template. Context
|
|
124
|
+
* projection later resolves actor/reference targets against the caller's own
|
|
125
|
+
* visible facts; this reader intentionally never infers a target. */
|
|
126
|
+
export function readAffordanceDefaults(
|
|
127
|
+
db: SnapshotDb,
|
|
128
|
+
inputByTemplate: ReadonlyMap<string, AffordanceInputSchema>,
|
|
129
|
+
): Map<string, AffordanceDefaults> {
|
|
130
|
+
const out = new Map<string, AffordanceDefaults>();
|
|
131
|
+
const conflictedTemplates = new Set<string>();
|
|
132
|
+
for (const row of readMetadataAttachmentRows(db)) {
|
|
133
|
+
if (
|
|
134
|
+
asString(row.namespace) !== AFFORDANCE_DEFAULTS_NAMESPACE ||
|
|
135
|
+
asString(row.targetKind) !== AFFORDANCE_TEMPLATE_TARGET_KIND ||
|
|
136
|
+
asString(row.audience) !== PUBLIC_AUDIENCE
|
|
137
|
+
)
|
|
138
|
+
continue;
|
|
139
|
+
const templateId = asString(row.targetRef);
|
|
140
|
+
const defaults = parseDefaults(
|
|
141
|
+
row.payloadJson,
|
|
142
|
+
templateId,
|
|
143
|
+
asString(row.key),
|
|
144
|
+
row.setAtTick,
|
|
145
|
+
inputByTemplate.get(templateId),
|
|
146
|
+
);
|
|
147
|
+
if (defaults === undefined || conflictedTemplates.has(templateId)) continue;
|
|
148
|
+
// Defaults form one authored whole. Multiple attachments could not be
|
|
149
|
+
// combined without inventing precedence, so withhold them all instead.
|
|
150
|
+
if (out.has(templateId)) {
|
|
151
|
+
out.delete(templateId);
|
|
152
|
+
conflictedTemplates.add(templateId);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
out.set(templateId, defaults);
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RequiredTables,
|
|
3
|
+
registerReaderManifest,
|
|
4
|
+
type SnapshotDb,
|
|
5
|
+
} from "../../reader-manifest";
|
|
6
|
+
import { asString } from "./_helpers";
|
|
7
|
+
import { readMetadataAttachmentRows } from "./metadata_rows";
|
|
8
|
+
|
|
9
|
+
export const requiredTables: RequiredTables<
|
|
10
|
+
["public_metadata_attachment", "visible_metadata_attachment"]
|
|
11
|
+
> = ["public_metadata_attachment", "visible_metadata_attachment"];
|
|
12
|
+
registerReaderManifest({ requiredTables });
|
|
13
|
+
|
|
14
|
+
const PUBLIC_AUDIENCE = "public";
|
|
15
|
+
const AFFORDANCE_TEMPLATE_TARGET_KIND = "affordance_template";
|
|
16
|
+
const AFFORDANCE_GROUP_NAMESPACE = "ui.affordance_group";
|
|
17
|
+
|
|
18
|
+
/** Reader-owned payload schema for `ui.affordance_group` (ADR-0073 G-2, T4c
|
|
19
|
+
* tool-UX decision log): `{"group": "combat"}` is the whole contract; anything
|
|
20
|
+
* else is malformed and the row is skipped. */
|
|
21
|
+
function parseGroupPayload(payloadJson: unknown): string | undefined {
|
|
22
|
+
let parsed: unknown;
|
|
23
|
+
try {
|
|
24
|
+
parsed = JSON.parse(asString(payloadJson));
|
|
25
|
+
} catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
if (typeof parsed !== "object" || parsed === null) return undefined;
|
|
29
|
+
const { group } = parsed as Record<string, unknown>;
|
|
30
|
+
if (typeof group !== "string" || group.length === 0) return undefined;
|
|
31
|
+
return group;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* ADR-0073 `ui.affordance_group` membership (T4c,
|
|
36
|
+
* `packages/spellkave-srd-cc/ledger/designs/tool-ux-decision-log.md`): which
|
|
37
|
+
* `affordance_template` (by `template_id`, matching an `AffordanceRow`'s
|
|
38
|
+
* `derived_by_template_id`) belongs to which bundle-authored UI group (e.g.
|
|
39
|
+
* "combat"). Unlike `readSurfaceMembership`'s single-slot membership, a
|
|
40
|
+
* template MAY carry more than one group attachment (different bundles, or a
|
|
41
|
+
* future multi-group template), so this returns every group per target_ref
|
|
42
|
+
* rather than resolving one winner. Filters to `public`-audience rows only:
|
|
43
|
+
* `visible_metadata_attachment` (ADR-0130 D-5) can carry non-public rows this
|
|
44
|
+
* caller is admitted to (visibility-class relations like `hidden`/`internal`,
|
|
45
|
+
* the strict `owned` relation, or capability audiences), so a PLAYER-facing
|
|
46
|
+
* reader must not trust "present in the union" to mean "public" — the
|
|
47
|
+
* explicit check stays load-bearing even post-ADR-0130.
|
|
48
|
+
*
|
|
49
|
+
* No group vocabulary lives here — the reader is generic over whatever
|
|
50
|
+
* string an authoring bundle attaches; group-name knowledge (e.g. "combat")
|
|
51
|
+
* lives entirely in the authoring generator, per the tool-UX consumer maxim.
|
|
52
|
+
*/
|
|
53
|
+
export function readAffordanceGroupMembership(db: SnapshotDb): Map<string, Set<string>> {
|
|
54
|
+
const out = new Map<string, Set<string>>();
|
|
55
|
+
for (const row of readMetadataAttachmentRows(db)) {
|
|
56
|
+
if (asString(row.namespace) !== AFFORDANCE_GROUP_NAMESPACE) continue;
|
|
57
|
+
if (asString(row.targetKind) !== AFFORDANCE_TEMPLATE_TARGET_KIND) continue;
|
|
58
|
+
if (asString(row.audience) !== PUBLIC_AUDIENCE) continue;
|
|
59
|
+
const group = parseGroupPayload(row.payloadJson);
|
|
60
|
+
if (group === undefined) continue;
|
|
61
|
+
const targetRef = asString(row.targetRef);
|
|
62
|
+
const set = out.get(targetRef) ?? new Set<string>();
|
|
63
|
+
set.add(group);
|
|
64
|
+
out.set(targetRef, set);
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|