@world-engines/tmw 0.1.0-alpha.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.
Files changed (120) hide show
  1. package/LICENSE +46 -0
  2. package/dist/activation.d.ts +50 -0
  3. package/dist/activation.js +113 -0
  4. package/dist/audio-presentation-projection.d.ts +23 -0
  5. package/dist/audio-presentation-projection.js +39 -0
  6. package/dist/author-scenario-source-v3.d.ts +34 -0
  7. package/dist/author-scenario-source-v3.js +1 -0
  8. package/dist/authoring-runtime.d.ts +76 -0
  9. package/dist/authoring-runtime.js +282 -0
  10. package/dist/builder-diagnostics.d.ts +3 -0
  11. package/dist/builder-diagnostics.js +8 -0
  12. package/dist/builder-node.d.ts +178 -0
  13. package/dist/builder-node.js +212 -0
  14. package/dist/builder.d.ts +32 -0
  15. package/dist/builder.js +127 -0
  16. package/dist/capability-registry.d.ts +32 -0
  17. package/dist/capability-registry.js +33 -0
  18. package/dist/compile-target-profile.d.ts +16 -0
  19. package/dist/compile-target-profile.js +70 -0
  20. package/dist/compiled-plan.d.ts +44 -0
  21. package/dist/compiled-plan.js +1 -0
  22. package/dist/compiler-compatibility-gate.d.ts +96 -0
  23. package/dist/compiler-compatibility-gate.js +609 -0
  24. package/dist/compiler-release-authority.d.ts +212 -0
  25. package/dist/compiler-release-authority.js +676 -0
  26. package/dist/compiler-stage-cache.d.ts +42 -0
  27. package/dist/compiler-stage-cache.js +100 -0
  28. package/dist/compiler-stage-registry.d.ts +48 -0
  29. package/dist/compiler-stage-registry.js +126 -0
  30. package/dist/compiler.d.ts +7 -0
  31. package/dist/compiler.js +68 -0
  32. package/dist/condition-graph.d.ts +46 -0
  33. package/dist/condition-graph.js +135 -0
  34. package/dist/diagnostics.d.ts +20 -0
  35. package/dist/diagnostics.js +20 -0
  36. package/dist/durable-intent.d.ts +29 -0
  37. package/dist/durable-intent.js +60 -0
  38. package/dist/effect-flow.d.ts +69 -0
  39. package/dist/effect-flow.js +98 -0
  40. package/dist/event-fabric.d.ts +81 -0
  41. package/dist/event-fabric.js +227 -0
  42. package/dist/event-lifecycle.d.ts +59 -0
  43. package/dist/event-lifecycle.js +161 -0
  44. package/dist/evidence.d.ts +37 -0
  45. package/dist/evidence.js +92 -0
  46. package/dist/gameplay-state.d.ts +127 -0
  47. package/dist/gameplay-state.js +214 -0
  48. package/dist/index.d.ts +66 -0
  49. package/dist/index.js +52 -0
  50. package/dist/isolated-replay.d.ts +81 -0
  51. package/dist/isolated-replay.js +316 -0
  52. package/dist/music-candidate-set.d.ts +38 -0
  53. package/dist/music-candidate-set.js +102 -0
  54. package/dist/natural-language-chunking.d.ts +10 -0
  55. package/dist/natural-language-chunking.js +62 -0
  56. package/dist/natural-language-ingestion.d.ts +14 -0
  57. package/dist/natural-language-ingestion.js +128 -0
  58. package/dist/natural-language-structured-decode.d.ts +2 -0
  59. package/dist/natural-language-structured-decode.js +69 -0
  60. package/dist/natural-language-world-types.d.ts +142 -0
  61. package/dist/natural-language-world-types.js +2 -0
  62. package/dist/neutral-source-compiler.d.ts +135 -0
  63. package/dist/neutral-source-compiler.js +608 -0
  64. package/dist/presentation-intent.d.ts +42 -0
  65. package/dist/presentation-intent.js +137 -0
  66. package/dist/recall-query-envelope.d.ts +32 -0
  67. package/dist/recall-query-envelope.js +51 -0
  68. package/dist/rewrite-compensation.d.ts +28 -0
  69. package/dist/rewrite-compensation.js +85 -0
  70. package/dist/runtime-contract.d.ts +56 -0
  71. package/dist/runtime-contract.js +96 -0
  72. package/dist/runtime-domain-capabilities.d.ts +12 -0
  73. package/dist/runtime-domain-capabilities.js +20 -0
  74. package/dist/runtime-sav.d.ts +117 -0
  75. package/dist/runtime-sav.js +356 -0
  76. package/dist/runtime-trace.d.ts +44 -0
  77. package/dist/runtime-trace.js +119 -0
  78. package/dist/runtime.d.ts +238 -0
  79. package/dist/runtime.js +1887 -0
  80. package/dist/scene-author-source.d.ts +13 -0
  81. package/dist/scene-author-source.js +25 -0
  82. package/dist/scene-state.d.ts +275 -0
  83. package/dist/scene-state.js +334 -0
  84. package/dist/scheduler.d.ts +55 -0
  85. package/dist/scheduler.js +235 -0
  86. package/dist/selector-ast.d.ts +24 -0
  87. package/dist/selector-ast.js +18 -0
  88. package/dist/semantic-verdict.d.ts +48 -0
  89. package/dist/semantic-verdict.js +101 -0
  90. package/dist/simulator.d.ts +45 -0
  91. package/dist/simulator.js +58 -0
  92. package/dist/source-canonical.d.ts +4 -0
  93. package/dist/source-canonical.js +51 -0
  94. package/dist/source-crypto.d.ts +20 -0
  95. package/dist/source-crypto.js +17 -0
  96. package/dist/source-revision.d.ts +22 -0
  97. package/dist/source-revision.js +43 -0
  98. package/dist/spatial-impact-receipt.d.ts +38 -0
  99. package/dist/spatial-impact-receipt.js +67 -0
  100. package/dist/temporal-rewrite.d.ts +59 -0
  101. package/dist/temporal-rewrite.js +214 -0
  102. package/dist/time-domains.d.ts +37 -0
  103. package/dist/time-domains.js +142 -0
  104. package/dist/timeline-revision.d.ts +28 -0
  105. package/dist/timeline-revision.js +59 -0
  106. package/dist/trigger-document-v3.d.ts +99 -0
  107. package/dist/trigger-document-v3.js +334 -0
  108. package/dist/turn-commit.d.ts +56 -0
  109. package/dist/turn-commit.js +190 -0
  110. package/dist/validator.d.ts +7 -0
  111. package/dist/validator.js +142 -0
  112. package/dist/value-graph.d.ts +98 -0
  113. package/dist/value-graph.js +318 -0
  114. package/dist/view-event-delivery.d.ts +47 -0
  115. package/dist/view-event-delivery.js +197 -0
  116. package/dist/view-interaction-manifest-v3.d.ts +60 -0
  117. package/dist/view-interaction-manifest-v3.js +198 -0
  118. package/dist/view-payload-schema.d.ts +17 -0
  119. package/dist/view-payload-schema.js +84 -0
  120. package/package.json +62 -0
@@ -0,0 +1,3 @@
1
+ import { type TmwResult } from "./diagnostics.js";
2
+ export declare const builderStableIdError: (stableId: string, path: string) => TmwResult<never> | null;
3
+ export declare const builderFailure: <T>(code: string, path: string, message: string, relatedStableIds?: readonly string[]) => TmwResult<T>;
@@ -0,0 +1,8 @@
1
+ import { tmwDiagnostic, tmwFailure } from "./diagnostics.js";
2
+ const STABLE_ID = /^[a-z0-9](?:[a-z0-9._:-]{0,127})$/u;
3
+ export const builderStableIdError = (stableId, path) => STABLE_ID.test(stableId) && stableId.normalize("NFC") === stableId
4
+ ? null
5
+ : tmwFailure([tmwDiagnostic("stable_id_invalid", path, "stable_id is required and must be canonical", stableId.length === 0 ? [] : [stableId])]);
6
+ export const builderFailure = (code, path, message, relatedStableIds = []) => tmwFailure([
7
+ tmwDiagnostic(code, path, message, relatedStableIds),
8
+ ]);
@@ -0,0 +1,178 @@
1
+ import type { TriggerConditionNodeV3, TriggerEffectNodeV3, TriggerEventNodeV3, TriggerNodeV3, TriggerPresentationNodeV3 } from "./trigger-document-v3.js";
2
+ import { z } from "zod";
3
+ export declare const tmwStableIdSchema: z.ZodString;
4
+ export declare const tmwBuilderCommandSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
5
+ operation: z.ZodLiteral<"add_event">;
6
+ node: z.ZodObject<{
7
+ kind: z.ZodLiteral<"event">;
8
+ stable_id: z.ZodString;
9
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
10
+ event_type: z.ZodString;
11
+ hard_fact: z.ZodBoolean;
12
+ effective_time: z.ZodObject<{
13
+ domain: z.ZodEnum<{
14
+ logical_seq: "logical_seq";
15
+ world_time: "world_time";
16
+ presentation_seq: "presentation_seq";
17
+ real_time: "real_time";
18
+ }>;
19
+ due_value: z.ZodNumber;
20
+ effective_value: z.ZodNumber;
21
+ crossing_policy: z.ZodEnum<{
22
+ forward_only: "forward_only";
23
+ backward_only: "backward_only";
24
+ both_directions: "both_directions";
25
+ }>;
26
+ real_time_clock: z.ZodNullable<z.ZodEnum<{
27
+ monotonic_session: "monotonic_session";
28
+ active_play: "active_play";
29
+ wall_clock: "wall_clock";
30
+ }>>;
31
+ wall_clock_policy: z.ZodNullable<z.ZodEnum<{
32
+ elapse_while_suspended: "elapse_while_suspended";
33
+ active_session_only: "active_session_only";
34
+ reject: "reject";
35
+ }>>;
36
+ }, z.core.$strip>;
37
+ selector_json: z.ZodNullable<z.ZodString>;
38
+ }, z.core.$strip>;
39
+ }, z.core.$strip>, z.ZodObject<{
40
+ operation: z.ZodLiteral<"add_condition">;
41
+ node: z.ZodObject<{
42
+ kind: z.ZodLiteral<"condition">;
43
+ stable_id: z.ZodString;
44
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
45
+ effective_time: z.ZodObject<{
46
+ domain: z.ZodEnum<{
47
+ logical_seq: "logical_seq";
48
+ world_time: "world_time";
49
+ presentation_seq: "presentation_seq";
50
+ real_time: "real_time";
51
+ }>;
52
+ due_value: z.ZodNumber;
53
+ effective_value: z.ZodNumber;
54
+ crossing_policy: z.ZodEnum<{
55
+ forward_only: "forward_only";
56
+ backward_only: "backward_only";
57
+ both_directions: "both_directions";
58
+ }>;
59
+ real_time_clock: z.ZodNullable<z.ZodEnum<{
60
+ monotonic_session: "monotonic_session";
61
+ active_play: "active_play";
62
+ wall_clock: "wall_clock";
63
+ }>>;
64
+ wall_clock_policy: z.ZodNullable<z.ZodEnum<{
65
+ elapse_while_suspended: "elapse_while_suspended";
66
+ active_session_only: "active_session_only";
67
+ reject: "reject";
68
+ }>>;
69
+ }, z.core.$strip>;
70
+ expression_json: z.ZodString;
71
+ }, z.core.$strip>;
72
+ }, z.core.$strip>, z.ZodObject<{
73
+ operation: z.ZodLiteral<"add_effect">;
74
+ node: z.ZodObject<{
75
+ kind: z.ZodLiteral<"effect">;
76
+ stable_id: z.ZodString;
77
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
78
+ effective_time: z.ZodObject<{
79
+ domain: z.ZodEnum<{
80
+ logical_seq: "logical_seq";
81
+ world_time: "world_time";
82
+ presentation_seq: "presentation_seq";
83
+ real_time: "real_time";
84
+ }>;
85
+ due_value: z.ZodNumber;
86
+ effective_value: z.ZodNumber;
87
+ crossing_policy: z.ZodEnum<{
88
+ forward_only: "forward_only";
89
+ backward_only: "backward_only";
90
+ both_directions: "both_directions";
91
+ }>;
92
+ real_time_clock: z.ZodNullable<z.ZodEnum<{
93
+ monotonic_session: "monotonic_session";
94
+ active_play: "active_play";
95
+ wall_clock: "wall_clock";
96
+ }>>;
97
+ wall_clock_policy: z.ZodNullable<z.ZodEnum<{
98
+ elapse_while_suspended: "elapse_while_suspended";
99
+ active_session_only: "active_session_only";
100
+ reject: "reject";
101
+ }>>;
102
+ }, z.core.$strip>;
103
+ capability_id: z.ZodString;
104
+ selector_json: z.ZodNullable<z.ZodString>;
105
+ arguments_json: z.ZodString;
106
+ execution_policy: z.ZodEnum<{
107
+ attempt_once: "attempt_once";
108
+ ensure_until: "ensure_until";
109
+ }>;
110
+ rewrite_policy: z.ZodEnum<{
111
+ normal_only: "normal_only";
112
+ rewrite_only: "rewrite_only";
113
+ normal_and_rewrite: "normal_and_rewrite";
114
+ }>;
115
+ priority: z.ZodNumber;
116
+ idempotency_key: z.ZodString;
117
+ }, z.core.$strip>;
118
+ }, z.core.$strip>, z.ZodObject<{
119
+ operation: z.ZodLiteral<"add_presentation">;
120
+ node: z.ZodObject<{
121
+ kind: z.ZodLiteral<"presentation">;
122
+ stable_id: z.ZodString;
123
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
124
+ effective_time: z.ZodObject<{
125
+ domain: z.ZodEnum<{
126
+ logical_seq: "logical_seq";
127
+ world_time: "world_time";
128
+ presentation_seq: "presentation_seq";
129
+ real_time: "real_time";
130
+ }>;
131
+ due_value: z.ZodNumber;
132
+ effective_value: z.ZodNumber;
133
+ crossing_policy: z.ZodEnum<{
134
+ forward_only: "forward_only";
135
+ backward_only: "backward_only";
136
+ both_directions: "both_directions";
137
+ }>;
138
+ real_time_clock: z.ZodNullable<z.ZodEnum<{
139
+ monotonic_session: "monotonic_session";
140
+ active_play: "active_play";
141
+ wall_clock: "wall_clock";
142
+ }>>;
143
+ wall_clock_policy: z.ZodNullable<z.ZodEnum<{
144
+ elapse_while_suspended: "elapse_while_suspended";
145
+ active_session_only: "active_session_only";
146
+ reject: "reject";
147
+ }>>;
148
+ }, z.core.$strip>;
149
+ intent_type: z.ZodString;
150
+ payload_json: z.ZodString;
151
+ }, z.core.$strip>;
152
+ }, z.core.$strip>, z.ZodObject<{
153
+ operation: z.ZodLiteral<"connect">;
154
+ from_stable_id: z.ZodString;
155
+ to_stable_id: z.ZodString;
156
+ }, z.core.$strip>], "operation">;
157
+ export type TmwBuilderCommand = z.infer<typeof tmwBuilderCommandSchema>;
158
+ export type TmwBuilderNodeCommand = Exclude<TmwBuilderCommand, {
159
+ readonly operation: "connect";
160
+ }>;
161
+ export type TmwBuilderNodeInput = {
162
+ readonly kind: "event";
163
+ readonly node: TriggerEventNodeV3;
164
+ } | {
165
+ readonly kind: "condition";
166
+ readonly node: TriggerConditionNodeV3;
167
+ } | {
168
+ readonly kind: "effect";
169
+ readonly node: TriggerEffectNodeV3;
170
+ } | {
171
+ readonly kind: "presentation";
172
+ readonly node: TriggerPresentationNodeV3;
173
+ };
174
+ export declare const immutableBuilderNode: <T extends TriggerNodeV3>(node: T) => T;
175
+ export declare const builderNodeStableId: (input: TmwBuilderNodeInput) => string;
176
+ export declare const builderNodeKind: (node: TriggerNodeV3) => TmwBuilderNodeInput["kind"];
177
+ export declare const tmwBuilderCommandFromNode: (node: TriggerNodeV3) => TmwBuilderCommand;
178
+ export declare const tmwBuilderNodeFromCommand: (command: TmwBuilderNodeCommand) => TriggerNodeV3;
@@ -0,0 +1,212 @@
1
+ import { canonicalTmwJson } from "./runtime-contract.js";
2
+ import { z } from "zod";
3
+ export const tmwStableIdSchema = z.string().regex(/^[a-z0-9](?:[a-z0-9._:-]{0,127})$/u);
4
+ const canonicalJsonFragmentSchema = z.string()
5
+ .min(2)
6
+ .max(262_144)
7
+ .describe("canonical JSON fragment; object keys must be sorted and no insignificant whitespace is allowed");
8
+ const effectiveTimeSchema = z.object({
9
+ domain: z.enum(["logical_seq", "world_time", "presentation_seq", "real_time"]),
10
+ due_value: z.number(),
11
+ effective_value: z.number(),
12
+ crossing_policy: z.enum(["forward_only", "backward_only", "both_directions"]),
13
+ real_time_clock: z.enum(["monotonic_session", "active_play", "wall_clock"]).nullable(),
14
+ wall_clock_policy: z.enum(["elapse_while_suspended", "active_session_only", "reject"]).nullable(),
15
+ });
16
+ const eventNodeInputSchema = z.object({
17
+ kind: z.literal("event"),
18
+ stable_id: tmwStableIdSchema,
19
+ enabled: z.boolean().optional().default(true),
20
+ event_type: tmwStableIdSchema,
21
+ hard_fact: z.boolean(),
22
+ effective_time: effectiveTimeSchema,
23
+ selector_json: canonicalJsonFragmentSchema.nullable(),
24
+ });
25
+ const conditionNodeInputSchema = z.object({
26
+ kind: z.literal("condition"),
27
+ stable_id: tmwStableIdSchema,
28
+ enabled: z.boolean().optional().default(true),
29
+ effective_time: effectiveTimeSchema,
30
+ expression_json: canonicalJsonFragmentSchema,
31
+ });
32
+ const effectNodeInputSchema = z.object({
33
+ kind: z.literal("effect"),
34
+ stable_id: tmwStableIdSchema,
35
+ enabled: z.boolean().optional().default(true),
36
+ effective_time: effectiveTimeSchema,
37
+ capability_id: tmwStableIdSchema,
38
+ selector_json: canonicalJsonFragmentSchema.nullable(),
39
+ arguments_json: canonicalJsonFragmentSchema,
40
+ execution_policy: z.enum(["attempt_once", "ensure_until"]),
41
+ rewrite_policy: z.enum(["normal_only", "rewrite_only", "normal_and_rewrite"]),
42
+ priority: z.number().int().nonnegative(),
43
+ idempotency_key: tmwStableIdSchema,
44
+ });
45
+ const presentationNodeInputSchema = z.object({
46
+ kind: z.literal("presentation"),
47
+ stable_id: tmwStableIdSchema,
48
+ enabled: z.boolean().optional().default(true),
49
+ effective_time: effectiveTimeSchema,
50
+ intent_type: tmwStableIdSchema,
51
+ payload_json: canonicalJsonFragmentSchema,
52
+ });
53
+ export const tmwBuilderCommandSchema = z.discriminatedUnion("operation", [
54
+ z.object({ operation: z.literal("add_event"), node: eventNodeInputSchema }),
55
+ z.object({ operation: z.literal("add_condition"), node: conditionNodeInputSchema }),
56
+ z.object({ operation: z.literal("add_effect"), node: effectNodeInputSchema }),
57
+ z.object({ operation: z.literal("add_presentation"), node: presentationNodeInputSchema }),
58
+ z.object({
59
+ operation: z.literal("connect"),
60
+ from_stable_id: tmwStableIdSchema,
61
+ to_stable_id: tmwStableIdSchema,
62
+ }),
63
+ ]);
64
+ const cloneJson = (value) => structuredClone(value);
65
+ const deepFreeze = (value) => {
66
+ if (typeof value !== "object" || value === null || Object.isFrozen(value))
67
+ return value;
68
+ for (const child of Object.values(value))
69
+ deepFreeze(child);
70
+ return Object.freeze(value);
71
+ };
72
+ export const immutableBuilderNode = (node) => deepFreeze(cloneJson(node));
73
+ export const builderNodeStableId = (input) => input.node.stable_id;
74
+ export const builderNodeKind = (node) => {
75
+ if (node.kind === "event"
76
+ || node.kind === "condition"
77
+ || node.kind === "effect"
78
+ || node.kind === "presentation") {
79
+ return node.kind;
80
+ }
81
+ throw new Error("tmw_builder_node_kind_invalid");
82
+ };
83
+ const encodeCanonicalJsonFragment = (value) => canonicalTmwJson(value);
84
+ const parseCanonicalObject = (value, code) => {
85
+ let parsed;
86
+ try {
87
+ parsed = JSON.parse(value);
88
+ }
89
+ catch {
90
+ throw new Error(code);
91
+ }
92
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
93
+ throw new Error(code);
94
+ }
95
+ if (canonicalTmwJson(parsed) !== value)
96
+ throw new Error(code);
97
+ return parsed;
98
+ };
99
+ export const tmwBuilderCommandFromNode = (node) => {
100
+ switch (node.kind) {
101
+ case "event":
102
+ return {
103
+ operation: "add_event",
104
+ node: {
105
+ kind: node.kind,
106
+ stable_id: node.stable_id,
107
+ enabled: node.enabled !== false,
108
+ event_type: node.event_type,
109
+ hard_fact: node.hard_fact,
110
+ effective_time: node.effective_time,
111
+ selector_json: node.selector === null
112
+ ? null
113
+ : encodeCanonicalJsonFragment(node.selector),
114
+ },
115
+ };
116
+ case "condition":
117
+ return {
118
+ operation: "add_condition",
119
+ node: {
120
+ kind: node.kind,
121
+ stable_id: node.stable_id,
122
+ enabled: node.enabled !== false,
123
+ effective_time: node.effective_time,
124
+ expression_json: encodeCanonicalJsonFragment(node.expression),
125
+ },
126
+ };
127
+ case "effect":
128
+ return {
129
+ operation: "add_effect",
130
+ node: {
131
+ kind: node.kind,
132
+ stable_id: node.stable_id,
133
+ enabled: node.enabled !== false,
134
+ effective_time: node.effective_time,
135
+ capability_id: node.capability_id,
136
+ selector_json: node.selector === null
137
+ ? null
138
+ : encodeCanonicalJsonFragment(node.selector),
139
+ arguments_json: encodeCanonicalJsonFragment(node.arguments),
140
+ execution_policy: node.execution_policy,
141
+ rewrite_policy: node.rewrite_policy,
142
+ priority: node.priority,
143
+ idempotency_key: node.idempotency_key,
144
+ },
145
+ };
146
+ case "presentation":
147
+ return {
148
+ operation: "add_presentation",
149
+ node: {
150
+ kind: node.kind,
151
+ stable_id: node.stable_id,
152
+ enabled: node.enabled !== false,
153
+ effective_time: node.effective_time,
154
+ intent_type: node.intent_type,
155
+ payload_json: encodeCanonicalJsonFragment(node.payload),
156
+ },
157
+ };
158
+ }
159
+ };
160
+ export const tmwBuilderNodeFromCommand = (command) => {
161
+ if (command.operation === "add_event") {
162
+ const { node } = command;
163
+ return {
164
+ kind: node.kind,
165
+ stable_id: node.stable_id,
166
+ enabled: node.enabled,
167
+ event_type: node.event_type,
168
+ hard_fact: node.hard_fact,
169
+ effective_time: node.effective_time,
170
+ selector: node.selector_json === null
171
+ ? null
172
+ : parseCanonicalObject(node.selector_json, "tmw_builder_selector_json_invalid"),
173
+ };
174
+ }
175
+ if (command.operation === "add_condition") {
176
+ const { node } = command;
177
+ return {
178
+ kind: node.kind,
179
+ stable_id: node.stable_id,
180
+ enabled: node.enabled,
181
+ effective_time: node.effective_time,
182
+ expression: parseCanonicalObject(node.expression_json, "tmw_builder_expression_json_invalid"),
183
+ };
184
+ }
185
+ if (command.operation === "add_effect") {
186
+ const { node } = command;
187
+ return {
188
+ kind: node.kind,
189
+ stable_id: node.stable_id,
190
+ enabled: node.enabled,
191
+ effective_time: node.effective_time,
192
+ capability_id: node.capability_id,
193
+ selector: node.selector_json === null
194
+ ? null
195
+ : parseCanonicalObject(node.selector_json, "tmw_builder_selector_json_invalid"),
196
+ arguments: parseCanonicalObject(node.arguments_json, "tmw_builder_arguments_json_invalid"),
197
+ execution_policy: node.execution_policy,
198
+ rewrite_policy: node.rewrite_policy,
199
+ priority: node.priority,
200
+ idempotency_key: node.idempotency_key,
201
+ };
202
+ }
203
+ const { node } = command;
204
+ return {
205
+ kind: node.kind,
206
+ stable_id: node.stable_id,
207
+ enabled: node.enabled,
208
+ effective_time: node.effective_time,
209
+ intent_type: node.intent_type,
210
+ payload: parseCanonicalObject(node.payload_json, "tmw_builder_payload_json_invalid"),
211
+ };
212
+ };
@@ -0,0 +1,32 @@
1
+ import type { CanonicalTmwCapabilityRegistryV3 } from "./capability-registry.js";
2
+ import { type TmwResult } from "./diagnostics.js";
3
+ import { type SimulateTmw3Options, type SimulateTmw3Outcome } from "./simulator.js";
4
+ import type { TriggerConditionNodeV3, TriggerDocumentV3, TriggerEdgeV3, TriggerEffectNodeV3, TriggerEventNodeV3, TriggerPresentationNodeV3 } from "./trigger-document-v3.js";
5
+ export interface BeginTmwBuilderInput {
6
+ readonly runtime_contract_hash: string;
7
+ readonly registry: CanonicalTmwCapabilityRegistryV3;
8
+ }
9
+ interface BuilderState {
10
+ readonly events: readonly TriggerEventNodeV3[];
11
+ readonly conditions: readonly TriggerConditionNodeV3[];
12
+ readonly effects: readonly TriggerEffectNodeV3[];
13
+ readonly presentations: readonly TriggerPresentationNodeV3[];
14
+ readonly edges: readonly TriggerEdgeV3[];
15
+ }
16
+ export declare class TmwBuilder {
17
+ private readonly runtimeContractHash;
18
+ private readonly registry;
19
+ private readonly state;
20
+ constructor(runtimeContractHash: string, registry: CanonicalTmwCapabilityRegistryV3, state?: BuilderState);
21
+ private addNode;
22
+ addEvent(node: TriggerEventNodeV3): TmwResult<TmwBuilder>;
23
+ addCondition(node: TriggerConditionNodeV3): TmwResult<TmwBuilder>;
24
+ addEffect(node: TriggerEffectNodeV3): TmwResult<TmwBuilder>;
25
+ addPresentation(node: TriggerPresentationNodeV3): TmwResult<TmwBuilder>;
26
+ connect(fromStableId: string, toStableId: string): TmwResult<TmwBuilder>;
27
+ document(): TriggerDocumentV3;
28
+ build(): Promise<TmwResult<TriggerDocumentV3>>;
29
+ simulate(options?: SimulateTmw3Options): Promise<SimulateTmw3Outcome>;
30
+ }
31
+ export declare const beginTmwBuilder: (input: BeginTmwBuilderInput) => TmwBuilder;
32
+ export {};
@@ -0,0 +1,127 @@
1
+ import { TMW_RUNTIME_GENERATION } from "./runtime-contract.js";
2
+ import { tmwSuccess } from "./diagnostics.js";
3
+ import { simulateTmw3 } from "./simulator.js";
4
+ import { validateTmw3 } from "./validator.js";
5
+ import { builderFailure, builderStableIdError } from "./builder-diagnostics.js";
6
+ import { immutableBuilderNode } from "./builder-node.js";
7
+ const EMPTY_STATE = Object.freeze({
8
+ events: Object.freeze([]),
9
+ conditions: Object.freeze([]),
10
+ effects: Object.freeze([]),
11
+ presentations: Object.freeze([]),
12
+ edges: Object.freeze([]),
13
+ });
14
+ const freezeState = (state) => Object.freeze({
15
+ events: Object.freeze([...state.events]),
16
+ conditions: Object.freeze([...state.conditions]),
17
+ effects: Object.freeze([...state.effects]),
18
+ presentations: Object.freeze([...state.presentations]),
19
+ edges: Object.freeze(state.edges.map((edge) => Object.freeze({ ...edge }))),
20
+ });
21
+ const nodes = (state) => [
22
+ ...state.events,
23
+ ...state.conditions,
24
+ ...state.effects,
25
+ ...state.presentations,
26
+ ];
27
+ export class TmwBuilder {
28
+ runtimeContractHash;
29
+ registry;
30
+ state;
31
+ constructor(runtimeContractHash, registry, state = EMPTY_STATE) {
32
+ this.runtimeContractHash = runtimeContractHash;
33
+ this.registry = registry;
34
+ this.state = freezeState(state);
35
+ Object.freeze(this);
36
+ }
37
+ addNode(node, path, append) {
38
+ if (typeof node !== "object" || node === null || typeof node.stable_id !== "string") {
39
+ return builderFailure("builder_node_invalid", path, "builder node must be a typed TMW3 node");
40
+ }
41
+ const stableIdError = builderStableIdError(node.stable_id, `${path}/stable_id`);
42
+ if (stableIdError !== null)
43
+ return stableIdError;
44
+ if (nodes(this.state).some((existing) => existing.stable_id === node.stable_id)) {
45
+ return builderFailure("duplicate_stable_id", path, "stable_id must be unique across all node kinds", [node.stable_id]);
46
+ }
47
+ const next = append(this.state, immutableBuilderNode(node));
48
+ return tmwSuccess(new TmwBuilder(this.runtimeContractHash, this.registry, next));
49
+ }
50
+ addEvent(node) {
51
+ return this.addNode(node, "$/events", (state, value) => ({
52
+ ...state,
53
+ events: [...state.events, value],
54
+ }));
55
+ }
56
+ addCondition(node) {
57
+ return this.addNode(node, "$/conditions", (state, value) => ({
58
+ ...state,
59
+ conditions: [...state.conditions, value],
60
+ }));
61
+ }
62
+ addEffect(node) {
63
+ if (typeof node !== "object" || node === null || typeof node.capability_id !== "string") {
64
+ return builderFailure("builder_effect_invalid", "$/effects", "effect must declare a capability_id");
65
+ }
66
+ if (!this.registry.capabilities.some((capability) => capability.capability_id === node.capability_id)) {
67
+ return builderFailure("unknown_capability", "$/effects/capability_id", "effect capability is not registered", [node.stable_id, node.capability_id]);
68
+ }
69
+ return this.addNode(node, "$/effects", (state, value) => ({
70
+ ...state,
71
+ effects: [...state.effects, value],
72
+ }));
73
+ }
74
+ addPresentation(node) {
75
+ return this.addNode(node, "$/presentations", (state, value) => ({
76
+ ...state,
77
+ presentations: [...state.presentations, value],
78
+ }));
79
+ }
80
+ connect(fromStableId, toStableId) {
81
+ const fromError = builderStableIdError(fromStableId, "$/edges/from_stable_id");
82
+ if (fromError !== null)
83
+ return fromError;
84
+ const toError = builderStableIdError(toStableId, "$/edges/to_stable_id");
85
+ if (toError !== null)
86
+ return toError;
87
+ const ids = new Set(nodes(this.state).map((node) => node.stable_id));
88
+ if (!ids.has(fromStableId)) {
89
+ return builderFailure("dangling_edge_source", "$/edges/from_stable_id", "edge source does not exist", [fromStableId]);
90
+ }
91
+ if (!ids.has(toStableId)) {
92
+ return builderFailure("dangling_edge_target", "$/edges/to_stable_id", "edge target does not exist", [toStableId]);
93
+ }
94
+ if (this.state.edges.some((edge) => edge.from_stable_id === fromStableId && edge.to_stable_id === toStableId)) {
95
+ return builderFailure("duplicate_edge", "$/edges", "edge must be unique", [fromStableId, toStableId]);
96
+ }
97
+ return tmwSuccess(new TmwBuilder(this.runtimeContractHash, this.registry, {
98
+ ...this.state,
99
+ edges: [...this.state.edges, { from_stable_id: fromStableId, to_stable_id: toStableId }],
100
+ }));
101
+ }
102
+ document() {
103
+ const byStableId = (items) => Object.freeze([...items].sort((left, right) => left.stable_id.localeCompare(right.stable_id, "en")));
104
+ return Object.freeze({
105
+ version: 3,
106
+ runtime_generation: TMW_RUNTIME_GENERATION,
107
+ runtime_contract_hash: this.runtimeContractHash,
108
+ registry_version: this.registry.registry_version,
109
+ events: byStableId(this.state.events),
110
+ conditions: byStableId(this.state.conditions),
111
+ effects: byStableId(this.state.effects),
112
+ presentations: byStableId(this.state.presentations),
113
+ edges: Object.freeze([...this.state.edges].sort((left, right) => (left.from_stable_id.localeCompare(right.from_stable_id, "en")
114
+ || left.to_stable_id.localeCompare(right.to_stable_id, "en")))),
115
+ });
116
+ }
117
+ build() {
118
+ return validateTmw3(this.document(), this.registry, {
119
+ expected_runtime_contract_hash: this.runtimeContractHash,
120
+ });
121
+ }
122
+ async simulate(options = {}) {
123
+ const built = await this.build();
124
+ return built.ok ? simulateTmw3(built.value, this.registry, options) : built;
125
+ }
126
+ }
127
+ export const beginTmwBuilder = (input) => new TmwBuilder(input.runtime_contract_hash, input.registry);
@@ -0,0 +1,32 @@
1
+ import type { SelectorAstV3 } from "./selector-ast.js";
2
+ import type { TmwJsonValue } from "./runtime-contract.js";
3
+ export declare const TMW_CAPABILITY_REGISTRY_VERSION: "tmw.capabilities.v3";
4
+ export type TmwCapabilityPolicy = "deterministic" | "semantic_verdict_required" | "presentation_only";
5
+ export interface TmwCapabilityV3 {
6
+ readonly capability_id: string;
7
+ readonly effect_type: string;
8
+ readonly policy: TmwCapabilityPolicy;
9
+ /** Explicit temporal-rewrite safety authority for committed effects. */
10
+ readonly rewrite_safety: "reversible" | "external_irreversible";
11
+ readonly max_invocations_per_turn: number;
12
+ readonly argument_schema: TmwJsonValue;
13
+ readonly selector_required: boolean;
14
+ }
15
+ export interface TmwCapabilityRegistryV3 {
16
+ readonly registry_version: string;
17
+ readonly capabilities: readonly TmwCapabilityV3[];
18
+ }
19
+ declare const canonicalRegistryBrand: unique symbol;
20
+ export type CanonicalTmwCapabilityRegistryV3 = TmwCapabilityRegistryV3 & {
21
+ readonly [canonicalRegistryBrand]: true;
22
+ };
23
+ /** 唯一受信 registry provider;调用方不能以自报 version/capability 对象取得 authority。 */
24
+ export declare function getCanonicalTmwCapabilityRegistryV3(): CanonicalTmwCapabilityRegistryV3;
25
+ export declare function isCanonicalTmwCapabilityRegistryV3(registry: TmwCapabilityRegistryV3): registry is CanonicalTmwCapabilityRegistryV3;
26
+ export interface CapabilityCheckedEffectV3 {
27
+ readonly capability_id: string;
28
+ readonly selector: SelectorAstV3 | null;
29
+ readonly arguments: Readonly<Record<string, TmwJsonValue>>;
30
+ }
31
+ export declare function capabilityRegistryIndex(registry: TmwCapabilityRegistryV3): ReadonlyMap<string, TmwCapabilityV3>;
32
+ export {};
@@ -0,0 +1,33 @@
1
+ import { TMW_SCENE_CAPABILITY_ARGUMENT_SCHEMAS } from "./scene-state.js";
2
+ import { TMW_EVENT_CAPABILITY_ARGUMENT_SCHEMAS } from "./event-lifecycle.js";
3
+ import { TMW_GAMEPLAY_CAPABILITY_ARGUMENT_SCHEMAS } from "./gameplay-state.js";
4
+ export const TMW_CAPABILITY_REGISTRY_VERSION = "tmw.capabilities.v3";
5
+ const CANONICAL_TMW_CAPABILITY_REGISTRY_V3 = Object.freeze({
6
+ registry_version: TMW_CAPABILITY_REGISTRY_VERSION,
7
+ capabilities: Object.freeze([
8
+ Object.freeze({ capability_id: "world.set_attribute", effect_type: "world.set_attribute", policy: "deterministic", rewrite_safety: "reversible", max_invocations_per_turn: 256, argument_schema: Object.freeze({ type: "object" }), selector_required: true }),
9
+ Object.freeze({ capability_id: "world.emit_event", effect_type: "world.emit_event", policy: "deterministic", rewrite_safety: "reversible", max_invocations_per_turn: 256, argument_schema: Object.freeze({ type: "object" }), selector_required: false }),
10
+ Object.freeze({ capability_id: "semantic.request_verdict", effect_type: "semantic.request_verdict", policy: "semantic_verdict_required", rewrite_safety: "reversible", max_invocations_per_turn: 32, argument_schema: Object.freeze({ type: "object" }), selector_required: false }),
11
+ Object.freeze({ capability_id: "presentation.show_overlay", effect_type: "presentation.show_overlay", policy: "presentation_only", rewrite_safety: "reversible", max_invocations_per_turn: 64, argument_schema: Object.freeze({ type: "object" }), selector_required: false }),
12
+ Object.freeze({ capability_id: "presentation.play_music", effect_type: "presentation.play_music", policy: "presentation_only", rewrite_safety: "reversible", max_invocations_per_turn: 32, argument_schema: Object.freeze({ type: "object" }), selector_required: false }),
13
+ ...Object.entries({ ...TMW_SCENE_CAPABILITY_ARGUMENT_SCHEMAS, ...TMW_EVENT_CAPABILITY_ARGUMENT_SCHEMAS, ...TMW_GAMEPLAY_CAPABILITY_ARGUMENT_SCHEMAS }).map(([capabilityId, argumentSchema]) => Object.freeze({
14
+ capability_id: capabilityId,
15
+ effect_type: capabilityId,
16
+ policy: "deterministic",
17
+ rewrite_safety: "reversible",
18
+ max_invocations_per_turn: 256,
19
+ argument_schema: argumentSchema,
20
+ selector_required: false,
21
+ })),
22
+ ]),
23
+ });
24
+ /** 唯一受信 registry provider;调用方不能以自报 version/capability 对象取得 authority。 */
25
+ export function getCanonicalTmwCapabilityRegistryV3() {
26
+ return CANONICAL_TMW_CAPABILITY_REGISTRY_V3;
27
+ }
28
+ export function isCanonicalTmwCapabilityRegistryV3(registry) {
29
+ return registry === CANONICAL_TMW_CAPABILITY_REGISTRY_V3;
30
+ }
31
+ export function capabilityRegistryIndex(registry) {
32
+ return new Map(registry.capabilities.map((capability) => [capability.capability_id, capability]));
33
+ }
@@ -0,0 +1,16 @@
1
+ export declare const COMPILE_TARGET_PROFILE_SCHEMA_VERSION: 1;
2
+ export interface CompileTargetStageVersion {
3
+ readonly stage_id: string;
4
+ readonly stage_version: string;
5
+ }
6
+ export interface CompileTargetProfile {
7
+ readonly schema_version: typeof COMPILE_TARGET_PROFILE_SCHEMA_VERSION;
8
+ readonly compiler_release: string;
9
+ readonly runtime_semantics: string;
10
+ readonly sdk_schema: string;
11
+ readonly artifact_format: string;
12
+ readonly stage_versions: readonly CompileTargetStageVersion[];
13
+ }
14
+ export declare function parseCompileTargetProfile(value: unknown): CompileTargetProfile;
15
+ export declare function canonicalCompileTargetProfileBytes(profile: CompileTargetProfile): Uint8Array;
16
+ export declare function compileTargetProfileDigest(profile: CompileTargetProfile): Promise<string>;