@spellkave/sdk 0.0.0-bootstrap
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +89 -0
- package/README.md +156 -0
- package/package.json +65 -0
- 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,129 @@
|
|
|
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_INPUT_NAMESPACE = "ui.affordance_input";
|
|
17
|
+
|
|
18
|
+
/** A world-authored field in an affordance's input form. The widget choice is
|
|
19
|
+
* presentation only; action identity and dispatch remain authored by the
|
|
20
|
+
* affordance/action rows. */
|
|
21
|
+
export interface AffordanceInputField {
|
|
22
|
+
name: string;
|
|
23
|
+
label: string;
|
|
24
|
+
control: "text" | "textarea" | "number";
|
|
25
|
+
placeholder?: string;
|
|
26
|
+
required?: boolean;
|
|
27
|
+
min?: number;
|
|
28
|
+
max?: number;
|
|
29
|
+
maxLength?: number;
|
|
30
|
+
/** Which PLAYER transport carries the value: `"utterance"` means the
|
|
31
|
+
* world reads it from what the actor SAYS (spellkave_communicate's
|
|
32
|
+
* utterance; a JSON object or plain text), `"args_json"` means the
|
|
33
|
+
* structured argument blob. Absent means the author did not say;
|
|
34
|
+
* the tool descriptions apply (sk-jay0.1.46). */
|
|
35
|
+
channel?: "utterance" | "args_json";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Public, authored input schema for one affordance template. An explicit
|
|
39
|
+
* empty `fields` array is meaningful: it says this action takes no form
|
|
40
|
+
* input, even if its underlying consequence happens to carry dispatch text. */
|
|
41
|
+
export interface AffordanceInputSchema {
|
|
42
|
+
fields: readonly AffordanceInputField[];
|
|
43
|
+
submitLabel?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function optionalString(value: unknown): string | undefined {
|
|
47
|
+
return typeof value === "string" && value.trim() !== "" ? value : undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function optionalFiniteNumber(value: unknown): number | undefined {
|
|
51
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function parseField(value: unknown): AffordanceInputField | undefined {
|
|
55
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
56
|
+
const row = value as Record<string, unknown>;
|
|
57
|
+
const name = optionalString(row.name);
|
|
58
|
+
const label = optionalString(row.label);
|
|
59
|
+
const control = row.control;
|
|
60
|
+
if (
|
|
61
|
+
name === undefined ||
|
|
62
|
+
label === undefined ||
|
|
63
|
+
(control !== "text" && control !== "textarea" && control !== "number")
|
|
64
|
+
)
|
|
65
|
+
return undefined;
|
|
66
|
+
const min = optionalFiniteNumber(row.min);
|
|
67
|
+
const max = optionalFiniteNumber(row.max);
|
|
68
|
+
const maxLength = optionalFiniteNumber(row.max_length);
|
|
69
|
+
const channel =
|
|
70
|
+
row.channel === "utterance" || row.channel === "args_json" ? row.channel : undefined;
|
|
71
|
+
if (
|
|
72
|
+
(min !== undefined && max !== undefined && min > max) ||
|
|
73
|
+
(maxLength !== undefined && maxLength < 1)
|
|
74
|
+
)
|
|
75
|
+
return undefined;
|
|
76
|
+
return {
|
|
77
|
+
name,
|
|
78
|
+
label,
|
|
79
|
+
control,
|
|
80
|
+
...(optionalString(row.placeholder) === undefined
|
|
81
|
+
? {}
|
|
82
|
+
: { placeholder: optionalString(row.placeholder) }),
|
|
83
|
+
...(row.required === true ? { required: true } : {}),
|
|
84
|
+
...(min === undefined ? {} : { min }),
|
|
85
|
+
...(max === undefined ? {} : { max }),
|
|
86
|
+
...(maxLength === undefined ? {} : { maxLength }),
|
|
87
|
+
...(channel === undefined ? {} : { channel }),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Reader-owned validation for `ui.affordance_input`. Bad authored payloads
|
|
92
|
+
* are omitted rather than guessed: a player client must never invent an input
|
|
93
|
+
* shape for a world action. */
|
|
94
|
+
function parseInputPayload(payloadJson: unknown): AffordanceInputSchema | undefined {
|
|
95
|
+
let parsed: unknown;
|
|
96
|
+
try {
|
|
97
|
+
parsed = JSON.parse(asString(payloadJson));
|
|
98
|
+
} catch {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return undefined;
|
|
102
|
+
const row = parsed as Record<string, unknown>;
|
|
103
|
+
if (!Array.isArray(row.fields)) return undefined;
|
|
104
|
+
const fields = row.fields.map(parseField);
|
|
105
|
+
if (fields.some((field) => field === undefined)) return undefined;
|
|
106
|
+
const typedFields = fields as AffordanceInputField[];
|
|
107
|
+
if (new Set(typedFields.map((field) => field.name)).size !== typedFields.length) return undefined;
|
|
108
|
+
return {
|
|
109
|
+
fields: typedFields,
|
|
110
|
+
...(optionalString(row.submit_label) === undefined
|
|
111
|
+
? {}
|
|
112
|
+
: { submitLabel: optionalString(row.submit_label) }),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Reads public, template-keyed form schemas. There is deliberately no action
|
|
117
|
+
* vocabulary in this reader: a template's manifest attachment is the sole
|
|
118
|
+
* authority for which fields its offered action presents. */
|
|
119
|
+
export function readAffordanceInputSchemas(db: SnapshotDb): Map<string, AffordanceInputSchema> {
|
|
120
|
+
const out = new Map<string, AffordanceInputSchema>();
|
|
121
|
+
for (const row of readMetadataAttachmentRows(db)) {
|
|
122
|
+
if (asString(row.namespace) !== AFFORDANCE_INPUT_NAMESPACE) continue;
|
|
123
|
+
if (asString(row.targetKind) !== AFFORDANCE_TEMPLATE_TARGET_KIND) continue;
|
|
124
|
+
if (asString(row.audience) !== PUBLIC_AUDIENCE) continue;
|
|
125
|
+
const schema = parseInputPayload(row.payloadJson);
|
|
126
|
+
if (schema !== undefined) out.set(asString(row.targetRef), schema);
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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_REFERENCE_NAMESPACE = "ui.affordance_reference";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Reads exact, world-authored definition references for affordance templates.
|
|
20
|
+
* The attachment is deliberately optional: actions remain usable when a world
|
|
21
|
+
* has no reference card, while consumers never infer one from display prose.
|
|
22
|
+
*/
|
|
23
|
+
export function readAffordanceDefinitionRefs(db: SnapshotDb): Map<string, string> {
|
|
24
|
+
const out = new Map<string, string>();
|
|
25
|
+
for (const row of readMetadataAttachmentRows(db)) {
|
|
26
|
+
if (asString(row.namespace) !== AFFORDANCE_REFERENCE_NAMESPACE) continue;
|
|
27
|
+
if (asString(row.targetKind) !== AFFORDANCE_TEMPLATE_TARGET_KIND) continue;
|
|
28
|
+
if (asString(row.audience) !== PUBLIC_AUDIENCE) continue;
|
|
29
|
+
let payload: unknown;
|
|
30
|
+
try {
|
|
31
|
+
payload = JSON.parse(asString(row.payloadJson));
|
|
32
|
+
} catch {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (typeof payload !== "object" || payload === null) continue;
|
|
36
|
+
const reference = (payload as Record<string, unknown>).definition_ref;
|
|
37
|
+
if (typeof reference !== "string" || reference.trim().length === 0) continue;
|
|
38
|
+
out.set(asString(row.targetRef), reference);
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { EventApplyConsequenceRow as DbEventApplyConsequenceRow } from "../../binding_rows";
|
|
2
|
+
import {
|
|
3
|
+
type RequiredTables,
|
|
4
|
+
registerReaderManifest,
|
|
5
|
+
type SnapshotDb,
|
|
6
|
+
} from "../../reader-manifest";
|
|
7
|
+
import { asNumber, asString, optString, rows } from "./_helpers";
|
|
8
|
+
|
|
9
|
+
// `event_apply_consequence` is private (ADR-0120, #915) — subscribe + read
|
|
10
|
+
// the per-caller `visible_event_apply_consequence` view instead
|
|
11
|
+
// (owner/DM-extent-bounded on target/source body).
|
|
12
|
+
export const requiredTables: RequiredTables<["visible_event_apply_consequence"]> = [
|
|
13
|
+
"visible_event_apply_consequence",
|
|
14
|
+
];
|
|
15
|
+
registerReaderManifest({ requiredTables });
|
|
16
|
+
|
|
17
|
+
export interface EventApplyConsequenceEntry {
|
|
18
|
+
event_id: string;
|
|
19
|
+
template_id: string;
|
|
20
|
+
target_entity_id?: string;
|
|
21
|
+
/** ADR-0093 B2: the acting/source entity, denormalised onto the row at emit
|
|
22
|
+
* time (`EventApplyConsequence.source_entity_ref`). `undefined` for an
|
|
23
|
+
* ambient/ownerless consequence — the server carries this as an empty
|
|
24
|
+
* string (`String`, not `Option<u64>`), so an empty value maps to
|
|
25
|
+
* `undefined` here, never `""`. Was previously dropped by this reader even
|
|
26
|
+
* though the server has always carried it. */
|
|
27
|
+
source_entity_ref?: string;
|
|
28
|
+
frame_writes_count: number;
|
|
29
|
+
traces_inscribed_count: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function readEventApplyConsequences(db: SnapshotDb): EventApplyConsequenceEntry[] {
|
|
33
|
+
return rows<DbEventApplyConsequenceRow>(db, "visible_event_apply_consequence").map((row) => ({
|
|
34
|
+
event_id: asString(row.eventId),
|
|
35
|
+
template_id: row.templateId,
|
|
36
|
+
target_entity_id: optionString(row.targetEntityId),
|
|
37
|
+
source_entity_ref: optString(row.sourceEntityRef),
|
|
38
|
+
frame_writes_count: asNumber(row.frameWritesCount),
|
|
39
|
+
traces_inscribed_count: asNumber(row.tracesInscribedCount),
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function optionString(value: unknown): string | undefined {
|
|
44
|
+
if (value === undefined || value === null) return undefined;
|
|
45
|
+
if (typeof value === "bigint" || typeof value === "number" || typeof value === "string")
|
|
46
|
+
return String(value);
|
|
47
|
+
if (typeof value === "object") {
|
|
48
|
+
const record = value as { tag?: unknown; value?: unknown; some?: unknown };
|
|
49
|
+
if (record.tag === "some" || record.tag === "Some")
|
|
50
|
+
return optionString(record.value ?? record.some);
|
|
51
|
+
if ("some" in record) return optionString(record.some);
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** ADR-0155 D-6: prompt labels bind to the occurrence's captured speech trace. */
|
|
2
|
+
import type { TraceRow } from "../../binding_rows";
|
|
3
|
+
import {
|
|
4
|
+
type RequiredTables,
|
|
5
|
+
registerReaderManifest,
|
|
6
|
+
type SnapshotDb,
|
|
7
|
+
} from "../../reader-manifest";
|
|
8
|
+
import { rows } from "./_helpers";
|
|
9
|
+
import { parseDialogueGuide } from "./dialogue_guide";
|
|
10
|
+
import type { DialoguePromptPresentation } from "./dialogue_prompt_presentation";
|
|
11
|
+
export const requiredTables: RequiredTables<["visible_trace"]> = ["visible_trace"];
|
|
12
|
+
registerReaderManifest({ requiredTables });
|
|
13
|
+
|
|
14
|
+
export function readAudienceTracePresentation(
|
|
15
|
+
db: SnapshotDb,
|
|
16
|
+
traceId: string,
|
|
17
|
+
responseIds: readonly string[],
|
|
18
|
+
authored: DialoguePromptPresentation | undefined,
|
|
19
|
+
): DialoguePromptPresentation | undefined {
|
|
20
|
+
if (authored?.presentation !== "audience_trace") return authored;
|
|
21
|
+
const fallback = Object.fromEntries(responseIds.map((id, index) => [id, String(index + 1)]));
|
|
22
|
+
const trace = rows<TraceRow>(db, "visible_trace").find((row) => String(row.traceId) === traceId);
|
|
23
|
+
let payload: unknown;
|
|
24
|
+
try {
|
|
25
|
+
payload = JSON.parse(trace?.payloadJson ?? "null");
|
|
26
|
+
} catch {
|
|
27
|
+
payload = undefined;
|
|
28
|
+
}
|
|
29
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload))
|
|
30
|
+
return { labels: fallback };
|
|
31
|
+
const { text, options, option_definition_refs, context } = payload as Record<string, unknown>;
|
|
32
|
+
// The speaker's machine side rides the same captured trace, so it is bound
|
|
33
|
+
// to this occurrence like the labels are.
|
|
34
|
+
const guide = parseDialogueGuide(context);
|
|
35
|
+
const valid =
|
|
36
|
+
Array.isArray(options) &&
|
|
37
|
+
options.length === responseIds.length &&
|
|
38
|
+
options.every((label) => typeof label === "string");
|
|
39
|
+
const refs =
|
|
40
|
+
valid &&
|
|
41
|
+
Array.isArray(option_definition_refs) &&
|
|
42
|
+
option_definition_refs.length === responseIds.length &&
|
|
43
|
+
option_definition_refs.every(
|
|
44
|
+
(ref) => ref === null || (typeof ref === "string" && ref.length > 0),
|
|
45
|
+
)
|
|
46
|
+
? Object.fromEntries(
|
|
47
|
+
responseIds.flatMap((id, index) =>
|
|
48
|
+
typeof option_definition_refs[index] === "string"
|
|
49
|
+
? [[id, option_definition_refs[index]]]
|
|
50
|
+
: [],
|
|
51
|
+
),
|
|
52
|
+
)
|
|
53
|
+
: undefined;
|
|
54
|
+
return {
|
|
55
|
+
...(typeof text === "string" ? { question: text } : {}),
|
|
56
|
+
...(refs === undefined || Object.keys(refs).length === 0 ? {} : { response_refs: refs }),
|
|
57
|
+
...(guide === undefined ? {} : { guide }),
|
|
58
|
+
labels: valid
|
|
59
|
+
? Object.fromEntries(responseIds.map((id, i) => [id, (options as string[])[i] as string]))
|
|
60
|
+
: fallback,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const EXAMPLE_BOUND = 4;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Self-describing bounded collection: `listing` tells a caller whether
|
|
5
|
+
* `items` is the entire authorized collection ("complete") or a bounded
|
|
6
|
+
* example slice ("examples") that needs a follow-up `continuation` call to
|
|
7
|
+
* see the rest. Never silent-truncate a collection without saying so.
|
|
8
|
+
*/
|
|
9
|
+
export interface BoundedCollection<T> {
|
|
10
|
+
count: number;
|
|
11
|
+
listing: "complete" | "examples";
|
|
12
|
+
items: T[];
|
|
13
|
+
continuation?: { tool: string; args: Record<string, unknown> };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Build a `BoundedCollection` from a full authorized row set. */
|
|
17
|
+
export function boundedCollection<T>(
|
|
18
|
+
all: readonly T[],
|
|
19
|
+
continuation: { tool: string; args: Record<string, unknown> },
|
|
20
|
+
limit = EXAMPLE_BOUND,
|
|
21
|
+
): BoundedCollection<T> {
|
|
22
|
+
const complete = all.length <= limit;
|
|
23
|
+
return {
|
|
24
|
+
count: all.length,
|
|
25
|
+
listing: complete ? "complete" : "examples",
|
|
26
|
+
items: all.slice(0, limit),
|
|
27
|
+
...(complete ? {} : { continuation }),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { CapabilityDefinitionRow, ResourceDefinitionRow } from "../../binding_rows";
|
|
2
|
+
import {
|
|
3
|
+
type RequiredTables,
|
|
4
|
+
registerReaderManifest,
|
|
5
|
+
type SnapshotDb,
|
|
6
|
+
} from "../../reader-manifest";
|
|
7
|
+
import { asNumber, asString, optNumber, rows } from "./_helpers";
|
|
8
|
+
|
|
9
|
+
export const capabilityRequiredTables: RequiredTables<["capability_definition"]> = [
|
|
10
|
+
"capability_definition",
|
|
11
|
+
];
|
|
12
|
+
registerReaderManifest({ requiredTables: capabilityRequiredTables });
|
|
13
|
+
|
|
14
|
+
export const resourceRequiredTables: RequiredTables<["resource_definition"]> = [
|
|
15
|
+
"resource_definition",
|
|
16
|
+
];
|
|
17
|
+
registerReaderManifest({ requiredTables: resourceRequiredTables });
|
|
18
|
+
|
|
19
|
+
export interface BundleCapabilityDefinitionRow {
|
|
20
|
+
capability_id: string;
|
|
21
|
+
bundle_id: string;
|
|
22
|
+
actor_kind: string;
|
|
23
|
+
display_name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface BundleResourceDefinitionRow {
|
|
28
|
+
resource_definition_id: string;
|
|
29
|
+
bundle_id: string;
|
|
30
|
+
display_name: string;
|
|
31
|
+
value_kind: string;
|
|
32
|
+
min_value: number;
|
|
33
|
+
max_value: number;
|
|
34
|
+
recovery_rule: string;
|
|
35
|
+
initial_value: number | null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function readCapabilityDefinition(
|
|
39
|
+
db: SnapshotDb,
|
|
40
|
+
capabilityId: string,
|
|
41
|
+
): BundleCapabilityDefinitionRow | null {
|
|
42
|
+
const row = rows<CapabilityDefinitionRow>(db, "capability_definition").find(
|
|
43
|
+
(r) => r.capabilityId === capabilityId,
|
|
44
|
+
);
|
|
45
|
+
if (!row) return null;
|
|
46
|
+
return {
|
|
47
|
+
capability_id: asString(row.capabilityId),
|
|
48
|
+
bundle_id: asString(row.bundleId),
|
|
49
|
+
actor_kind: asString(row.actorKind),
|
|
50
|
+
display_name: asString(row.displayName),
|
|
51
|
+
description: asString(row.description),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function readResourceDefinition(
|
|
56
|
+
db: SnapshotDb,
|
|
57
|
+
resourceDefinitionId: string,
|
|
58
|
+
): BundleResourceDefinitionRow | null {
|
|
59
|
+
const row = rows<ResourceDefinitionRow>(db, "resource_definition").find(
|
|
60
|
+
(r) => r.resourceDefinitionId === resourceDefinitionId,
|
|
61
|
+
);
|
|
62
|
+
if (!row) return null;
|
|
63
|
+
const initial = optNumber(row.initialValue);
|
|
64
|
+
return {
|
|
65
|
+
resource_definition_id: asString(row.resourceDefinitionId),
|
|
66
|
+
bundle_id: asString(row.bundleId),
|
|
67
|
+
display_name: asString(row.displayName),
|
|
68
|
+
value_kind: asString(row.valueKind),
|
|
69
|
+
min_value: asNumber(row.minValue),
|
|
70
|
+
max_value: asNumber(row.maxValue),
|
|
71
|
+
recovery_rule: asString(row.recoveryRule),
|
|
72
|
+
initial_value: initial === undefined || !Number.isFinite(initial) ? null : initial,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { EntityRow } 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
|
+
["entity", "visible_manifest_authored_entity", "visible_manifest_authored_entity_coverage"]
|
|
12
|
+
> = ["entity", "visible_manifest_authored_entity", "visible_manifest_authored_entity_coverage"];
|
|
13
|
+
registerReaderManifest({ requiredTables });
|
|
14
|
+
|
|
15
|
+
export interface BundleEntityRow {
|
|
16
|
+
entity_id: string;
|
|
17
|
+
entity_key: string | null;
|
|
18
|
+
entity_def_id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
kind: string;
|
|
21
|
+
created_at_tick: number;
|
|
22
|
+
quantity: number;
|
|
23
|
+
owned_by_entity_id: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type BundleItemRow = BundleEntityRow & { kind: "item" };
|
|
27
|
+
|
|
28
|
+
export function readEntity(db: SnapshotDb, entityKey: EntityRef): BundleEntityRow | null {
|
|
29
|
+
const entityId = resolveEntityId(db, entityKey);
|
|
30
|
+
const entityRows = rows<EntityRow>(db, "entity");
|
|
31
|
+
if (entityId === undefined) return null;
|
|
32
|
+
const entity = entityRows.find((r) => r.entityId === entityId);
|
|
33
|
+
return entity ? toEntityRow(db, entity, entityKey) : null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function readItem(db: SnapshotDb, itemKey: EntityRef): BundleItemRow | null {
|
|
37
|
+
const entity = readEntity(db, itemKey);
|
|
38
|
+
return entity?.kind === "item" ? (entity as BundleItemRow) : null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function toEntityRow(db: SnapshotDb, row: EntityRow, lookupKey: string): BundleEntityRow {
|
|
42
|
+
return {
|
|
43
|
+
entity_id: String(row.entityId),
|
|
44
|
+
entity_key: entityKeyFor(db, row.entityId, lookupKey),
|
|
45
|
+
entity_def_id: asString(row.entityDefId),
|
|
46
|
+
name: asString(row.name),
|
|
47
|
+
kind: asString(row.kind),
|
|
48
|
+
created_at_tick: asNumber(row.createdAtTick),
|
|
49
|
+
quantity: asNumber(row.quantity),
|
|
50
|
+
owned_by_entity_id: String(row.ownedByEntityId),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function entityKeyFor(db: SnapshotDb, entityId: bigint, lookupKey: string): string | null {
|
|
55
|
+
const authored = rows<{ manifestKey: string; entityId: bigint }>(
|
|
56
|
+
db,
|
|
57
|
+
"visible_manifest_authored_entity",
|
|
58
|
+
).find((r) => r.entityId === entityId);
|
|
59
|
+
if (!authored) return null;
|
|
60
|
+
const idx = authored.manifestKey.indexOf(":");
|
|
61
|
+
return idx >= 0 ? authored.manifestKey.slice(idx + 1) : lookupKey;
|
|
62
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Reader: actor co-location snapshot — the two place-node values a perceiver
|
|
2
|
+
// occupies in the spatial vs. the entity track. The co-evolution Verifier
|
|
3
|
+
// (packages/world-explorer) compares these BEFORE trusting any affordance/
|
|
4
|
+
// perception query, because the round-13 #655 false-negative slipped in when
|
|
5
|
+
// the perceiver's `actor_spatial_location` (market_lane) diverged from its
|
|
6
|
+
// `entity_location`. This reader only READS both rows; it does NOT judge — the
|
|
7
|
+
// divergence verdict is the Verifier's pure `assertCoLocation` (SDK CLAUDE.md §1:
|
|
8
|
+
// no runtime state-dependent decision in the SDK).
|
|
9
|
+
|
|
10
|
+
import type { ActorSpatialLocationRow, EntityLocationRow } from "../../binding_rows";
|
|
11
|
+
import {
|
|
12
|
+
type RequiredTables,
|
|
13
|
+
registerReaderManifest,
|
|
14
|
+
type SnapshotDb,
|
|
15
|
+
} from "../../reader-manifest";
|
|
16
|
+
import type { EntityRef } from "../ids";
|
|
17
|
+
import { resolveEntityId, rows } from "./_helpers";
|
|
18
|
+
|
|
19
|
+
export const requiredTables: RequiredTables<
|
|
20
|
+
[
|
|
21
|
+
"visible_actor_spatial_location",
|
|
22
|
+
"visible_entity_location",
|
|
23
|
+
"visible_manifest_authored_entity",
|
|
24
|
+
"visible_manifest_authored_entity_coverage",
|
|
25
|
+
]
|
|
26
|
+
> = [
|
|
27
|
+
"visible_actor_spatial_location",
|
|
28
|
+
"visible_entity_location",
|
|
29
|
+
"visible_manifest_authored_entity",
|
|
30
|
+
"visible_manifest_authored_entity_coverage",
|
|
31
|
+
];
|
|
32
|
+
registerReaderManifest({ requiredTables });
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The two place-node ids a perceiver is recorded at. `null` for either track
|
|
36
|
+
* means no row exists there for the entity (which `assertCoLocation` treats as
|
|
37
|
+
* a divergence, never as a silent match).
|
|
38
|
+
*/
|
|
39
|
+
export interface ActorCoLocation {
|
|
40
|
+
/** The entity id this snapshot is for (resolved from the ref). */
|
|
41
|
+
entity_id: string;
|
|
42
|
+
/** `actor_spatial_location.placeNodeId`, or `null` if no row. */
|
|
43
|
+
actor_spatial_location: string | null;
|
|
44
|
+
/** `entity_location.placeNodeId`, or `null` if no row. */
|
|
45
|
+
entity_location: string | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Read the perceiver's co-location across both tracks. Returns `null` only when
|
|
50
|
+
* the ref does not resolve to an entity id at all; an existing entity with no
|
|
51
|
+
* row in one track yields that track as `null` (a divergence the Verifier
|
|
52
|
+
* withholds on). Accepts the same ref forms as the other perceive readers
|
|
53
|
+
* (numeric, `entity:N`, or a manifest authored-entity key).
|
|
54
|
+
*/
|
|
55
|
+
export function readActorCoLocation(db: SnapshotDb, entityRef: EntityRef): ActorCoLocation | null {
|
|
56
|
+
const entityId = resolveEntityId(db, entityRef);
|
|
57
|
+
if (entityId === undefined) return null;
|
|
58
|
+
|
|
59
|
+
const spatial = rows<ActorSpatialLocationRow>(db, "visible_actor_spatial_location").find(
|
|
60
|
+
(r) => String(r.entityId) === String(entityId),
|
|
61
|
+
);
|
|
62
|
+
const entity = rows<EntityLocationRow>(db, "visible_entity_location").find(
|
|
63
|
+
(r) => String(r.entityId) === String(entityId),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
entity_id: String(entityId),
|
|
68
|
+
actor_spatial_location: spatial ? String(spatial.placeNodeId) : null,
|
|
69
|
+
entity_location: entity ? String(entity.placeNodeId) : null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ActorSpatialLocationRow,
|
|
3
|
+
EntityRow,
|
|
4
|
+
EvaluationProvenanceRow,
|
|
5
|
+
ObjectStateRow,
|
|
6
|
+
PlaceEdgeAffordanceRow,
|
|
7
|
+
PlaceEdgeRow,
|
|
8
|
+
PlaceGraphDefinitionRow,
|
|
9
|
+
PlaceNodeRow,
|
|
10
|
+
WorldEventRow,
|
|
11
|
+
} from "../../binding_rows";
|
|
12
|
+
import {
|
|
13
|
+
type RequiredTables,
|
|
14
|
+
registerReaderManifest,
|
|
15
|
+
type SnapshotDb,
|
|
16
|
+
} from "../../reader-manifest";
|
|
17
|
+
import { asString, resolveEntityId, rows } from "./_helpers";
|
|
18
|
+
import { activeSessionForParticipant } from "./combat_session_active";
|
|
19
|
+
import { latestFeedback } from "./combat_session_feedback";
|
|
20
|
+
import { gridForSession } from "./combat_session_grid";
|
|
21
|
+
import { buildRoster, nextRosterEntry } from "./combat_session_roster";
|
|
22
|
+
|
|
23
|
+
export type {
|
|
24
|
+
CombatFeedback,
|
|
25
|
+
CombatGridCell,
|
|
26
|
+
CombatMove,
|
|
27
|
+
CombatRosterEntry,
|
|
28
|
+
CombatSessionView,
|
|
29
|
+
} from "./combat_session_types";
|
|
30
|
+
|
|
31
|
+
import type { EntityRef } from "../ids";
|
|
32
|
+
import type { CombatSessionView } from "./combat_session_types";
|
|
33
|
+
|
|
34
|
+
export const requiredTables: RequiredTables<
|
|
35
|
+
[
|
|
36
|
+
"visible_session",
|
|
37
|
+
"visible_session_participant",
|
|
38
|
+
"visible_actor_spatial_location",
|
|
39
|
+
"entity",
|
|
40
|
+
"visible_object_state",
|
|
41
|
+
"visible_manifest_authored_entity",
|
|
42
|
+
"visible_manifest_authored_entity_coverage",
|
|
43
|
+
"visible_place_graph_definition",
|
|
44
|
+
"visible_place_node",
|
|
45
|
+
"visible_place_edge",
|
|
46
|
+
"place_edge_affordance",
|
|
47
|
+
"visible_evaluation_provenance",
|
|
48
|
+
"visible_world_event",
|
|
49
|
+
]
|
|
50
|
+
> = [
|
|
51
|
+
"visible_session",
|
|
52
|
+
"visible_session_participant",
|
|
53
|
+
"visible_actor_spatial_location",
|
|
54
|
+
"entity",
|
|
55
|
+
"visible_object_state",
|
|
56
|
+
"visible_manifest_authored_entity",
|
|
57
|
+
"visible_manifest_authored_entity_coverage",
|
|
58
|
+
"visible_place_graph_definition",
|
|
59
|
+
"visible_place_node",
|
|
60
|
+
"visible_place_edge",
|
|
61
|
+
"place_edge_affordance",
|
|
62
|
+
"visible_evaluation_provenance",
|
|
63
|
+
// `world_event` is private (#915 PR-8); read the per-caller dispatch view.
|
|
64
|
+
"visible_world_event",
|
|
65
|
+
];
|
|
66
|
+
registerReaderManifest({ requiredTables });
|
|
67
|
+
|
|
68
|
+
export function readCombatSessionView(
|
|
69
|
+
db: SnapshotDb,
|
|
70
|
+
actorRef: EntityRef,
|
|
71
|
+
): CombatSessionView | null {
|
|
72
|
+
const heroEntity = resolveEntityId(db, actorRef);
|
|
73
|
+
const graphs = rows<PlaceGraphDefinitionRow>(db, "visible_place_graph_definition");
|
|
74
|
+
const nodes = rows<PlaceNodeRow>(db, "visible_place_node");
|
|
75
|
+
const edges = rows<PlaceEdgeRow>(db, "visible_place_edge");
|
|
76
|
+
const edgeAffordances = rows<PlaceEdgeAffordanceRow>(db, "place_edge_affordance");
|
|
77
|
+
const spatialRows = rows<ActorSpatialLocationRow>(db, "visible_actor_spatial_location");
|
|
78
|
+
const entityRows = rows<EntityRow>(db, "entity");
|
|
79
|
+
const stateRows = rows<ObjectStateRow>(db, "visible_object_state");
|
|
80
|
+
const provenanceRows = rows<EvaluationProvenanceRow>(db, "visible_evaluation_provenance");
|
|
81
|
+
const eventRows = rows<WorldEventRow>(db, "visible_world_event");
|
|
82
|
+
const heroId = heroEntity === undefined ? undefined : String(heroEntity);
|
|
83
|
+
if (heroId === undefined) return null;
|
|
84
|
+
|
|
85
|
+
const active = activeSessionForParticipant(db, heroId);
|
|
86
|
+
if (active === null) return null;
|
|
87
|
+
|
|
88
|
+
const positions = new Map(
|
|
89
|
+
spatialRows.map((row) => [String(row.entityId), asString(row.placeNodeId)]),
|
|
90
|
+
);
|
|
91
|
+
const roster = buildRoster(
|
|
92
|
+
active.participants,
|
|
93
|
+
active.session.currentActor,
|
|
94
|
+
heroId,
|
|
95
|
+
entityRows,
|
|
96
|
+
stateRows,
|
|
97
|
+
positions,
|
|
98
|
+
nodes,
|
|
99
|
+
);
|
|
100
|
+
const currentActorName =
|
|
101
|
+
roster.find((entry) => entry.isCurrent)?.name ?? `entity:${active.session.currentActor}`;
|
|
102
|
+
const nextActorName = nextRosterEntry(roster, active.session.currentActor)?.name ?? null;
|
|
103
|
+
const isHeroTurn = active.session.currentActor === heroId;
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
session: active.session,
|
|
107
|
+
roster,
|
|
108
|
+
currentActorName,
|
|
109
|
+
nextActorName,
|
|
110
|
+
isHeroTurn,
|
|
111
|
+
waitingReason: isHeroTurn ? null : `Waiting for ${currentActorName}. It is not your turn.`,
|
|
112
|
+
grid: gridForSession(
|
|
113
|
+
active.session.sessionId,
|
|
114
|
+
heroId,
|
|
115
|
+
graphs,
|
|
116
|
+
nodes,
|
|
117
|
+
edges,
|
|
118
|
+
edgeAffordances,
|
|
119
|
+
roster,
|
|
120
|
+
positions,
|
|
121
|
+
),
|
|
122
|
+
feedback: latestFeedback(heroId, provenanceRows, eventRows),
|
|
123
|
+
};
|
|
124
|
+
}
|