@holoscript/framework 6.0.3 → 6.0.4

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 (160) hide show
  1. package/CHANGELOG.md +1 -2
  2. package/ROADMAP.md +68 -66
  3. package/dist/{InvisibleWallet-BB6tFvRA.d.cts → InvisibleWallet-EFiuaLn3.d.cts} +1 -1
  4. package/dist/{OrchestratorAgent-BvWgf9uw.d.cts → OrchestratorAgent-CrLDGNL6.d.cts} +1 -1
  5. package/dist/agents/index.cjs +11 -10
  6. package/dist/agents/index.d.cts +4 -16
  7. package/dist/ai/index.cjs +2 -2
  8. package/dist/behavior.cjs +10 -0
  9. package/dist/economy/index.cjs +4 -4
  10. package/dist/economy/index.d.cts +2 -2
  11. package/dist/index.cjs +33 -11
  12. package/dist/index.d.cts +3 -3
  13. package/dist/swarm/index.cjs +3 -0
  14. package/package.json +14 -9
  15. package/src/__tests__/bounty-marketplace.test.ts +53 -21
  16. package/src/__tests__/delegation.test.ts +1 -4
  17. package/src/__tests__/done-log-audit.test.ts +38 -46
  18. package/src/__tests__/framework.test.ts +172 -53
  19. package/src/__tests__/goal-synthesizer.test.ts +9 -6
  20. package/src/__tests__/presence.test.ts +1 -1
  21. package/src/__tests__/protocol-agent.test.ts +12 -11
  22. package/src/__tests__/revenue-splitter.test.ts +22 -15
  23. package/src/__tests__/scenario-driven-todo.test.ts +55 -35
  24. package/src/__tests__/self-improve.test.ts +28 -9
  25. package/src/__tests__/service-lifecycle.test.ts +9 -3
  26. package/src/__tests__/skill-router.test.ts +3 -3
  27. package/src/agents/CulturalMemory.ts +6 -6
  28. package/src/agents/DelegationTraceHooks.ts +560 -0
  29. package/src/agents/FederatedRegistryAdapter.ts +1 -1
  30. package/src/agents/NormEngine.ts +3 -8
  31. package/src/agents/OrchestratorAgent.ts +1 -1
  32. package/src/agents/TaskDelegationService.ts +5 -9
  33. package/src/agents/__tests__/AgentWalletRegistry.test.ts +5 -4
  34. package/src/agents/__tests__/CrossRealityHandoff.test.ts +9 -3
  35. package/src/agents/__tests__/DelegationTraceHooks.test.ts +390 -0
  36. package/src/agents/__tests__/TaskDelegationService.test.ts +4 -2
  37. package/src/agents/spatial-comms/Layer1RealTime.ts +36 -19
  38. package/src/agents/spatial-comms/Layer2A2A.ts +1 -3
  39. package/src/agents/spatial-comms/Layer3MCP.ts +13 -4
  40. package/src/agents/spatial-comms/ProtocolTypes.ts +5 -2
  41. package/src/agents/spatial-comms/examples/multi-agent-world-creation.ts +2 -2
  42. package/src/ai/HoloScriptGenerator.ts +2 -2
  43. package/src/ai/__tests__/PerceptionSystem.prod.test.ts +1 -1
  44. package/src/ai/__tests__/PerceptionSystem.test.ts +14 -14
  45. package/src/ai/__tests__/SteeringBehaviors.prod.test.ts +1 -1
  46. package/src/ai/index.ts +5 -1
  47. package/src/board/audit.ts +17 -6
  48. package/src/board/board-ops.ts +45 -15
  49. package/src/board/board-types.ts +94 -20
  50. package/src/delegation.ts +5 -3
  51. package/src/distributed-claimer.ts +13 -2
  52. package/src/economy/BountyManager.ts +40 -18
  53. package/src/economy/KnowledgeMarketplace.ts +27 -8
  54. package/src/economy/PaymentWebhookService.ts +0 -1
  55. package/src/economy/RevenueSplitter.ts +2 -4
  56. package/src/economy/UnifiedBudgetOptimizer.ts +8 -9
  57. package/src/economy/_core-stubs.ts +1 -1
  58. package/src/economy/x402-facilitator.ts +17 -8
  59. package/src/index.ts +16 -12
  60. package/src/knowledge/__tests__/knowledge-consolidator.test.ts +138 -89
  61. package/src/knowledge/__tests__/knowledge-store-vector.test.ts +59 -16
  62. package/src/knowledge/brain.ts +7 -7
  63. package/src/knowledge/consolidation.ts +16 -16
  64. package/src/knowledge/knowledge-consolidator.ts +60 -30
  65. package/src/knowledge/knowledge-store.ts +83 -45
  66. package/src/learning/ProceduralCompiler.ts +6 -1
  67. package/src/learning/learning/MemoryConsolidator.ts +102 -0
  68. package/src/learning/learning/MemoryScorer.ts +69 -0
  69. package/src/learning/learning/ProceduralCompiler.ts +45 -0
  70. package/src/learning/learning/SemanticClusterer.ts +66 -0
  71. package/src/llm/llm-adapter.ts +24 -10
  72. package/src/mesh/index.ts +37 -17
  73. package/src/protocol/goal-synthesizer.ts +24 -34
  74. package/src/protocol/implementations.ts +91 -22
  75. package/src/protocol/micro-phase-decomposer.ts +25 -17
  76. package/src/protocol/micro-step-decomposer.test.ts +104 -39
  77. package/src/protocol-agent.test.ts +17 -7
  78. package/src/protocol-agent.ts +45 -42
  79. package/src/self-improve/absorb-scanner.ts +9 -6
  80. package/src/self-improve/evolution-engine.ts +36 -18
  81. package/src/self-improve/framework-absorber.ts +21 -16
  82. package/src/self-improve/index.ts +2 -10
  83. package/src/self-improve/prompt-optimizer.ts +31 -19
  84. package/src/self-improve/test-generator.ts +16 -12
  85. package/src/skill-router.ts +7 -6
  86. package/src/swarm/messaging/GossipProtocol.ts +1 -1
  87. package/src/swarm/messaging/__tests__/BroadcastChannel.prod.test.ts +31 -9
  88. package/src/swarm/messaging/__tests__/GossipProtocol.prod.test.ts +21 -7
  89. package/src/swarm/messaging/__tests__/SwarmEventBus.prod.test.ts +24 -8
  90. package/src/swarm/messaging/__tests__/SwarmEventBus.test.ts +6 -2
  91. package/src/team.ts +277 -122
  92. package/src/training/scripts/generate-spatial-dataset.ts +1 -1
  93. package/src/training/training/LRScheduler.ts +377 -0
  94. package/src/training/training/QualityScoringPipeline.ts +139 -0
  95. package/src/training/training/SoftDedup.ts +461 -0
  96. package/src/training/training/SparsityMonitor.ts +685 -0
  97. package/src/training/training/SparsityMonitorTypes.ts +209 -0
  98. package/src/training/training/SpatialTrainingDataGenerator.ts +1526 -0
  99. package/src/training/training/SpatialTrainingDataTypes.ts +216 -0
  100. package/src/training/training/TrainingPipelineConfig.ts +215 -0
  101. package/src/training/training/__tests__/CorpusValidation.test.ts +87 -0
  102. package/src/training/training/__tests__/LRScheduler.test.ts +592 -0
  103. package/src/training/training/__tests__/SoftDedup.test.ts +415 -0
  104. package/src/training/training/__tests__/SparsityMonitor.test.ts +1623 -0
  105. package/src/training/training/__tests__/SpatialCorpusValidation.test.ts +72 -0
  106. package/src/training/training/__tests__/SpatialTrainingDataGenerator.test.ts +1244 -0
  107. package/src/training/training/__tests__/TrainingMonkeyIntegration.test.ts +897 -0
  108. package/src/training/training/__tests__/TrainingPipelineConfig.test.ts +202 -0
  109. package/src/training/training/__tests__/schema.test.ts +72 -0
  110. package/src/training/training/__tests__/training-constants.test.ts +106 -0
  111. package/src/training/training/__tests__/trait-mappings.test.ts +81 -0
  112. package/src/training/training/constants.ts +94 -0
  113. package/src/training/training/index.ts +17 -0
  114. package/src/training/training/schema.ts +147 -0
  115. package/src/training/training/scripts/generate-novel-use-cases-dataset.ts +272 -0
  116. package/src/training/training/scripts/generate-spatial-dataset.ts +521 -0
  117. package/src/training/training/trainingmonkey/TrainingMonkeyIntegration.ts +477 -0
  118. package/src/training/training/trainingmonkey/TrainingMonkeyTypes.ts +230 -0
  119. package/src/training/training/trainingmonkey/index.ts +26 -0
  120. package/src/training/training/trait-mappings.ts +157 -0
  121. package/src/types.ts +2 -7
  122. package/ALL-test-results.json +0 -1
  123. package/LICENSE +0 -21
  124. package/dist/AgentManifest-CB4xM-Ma.d.ts +0 -704
  125. package/dist/BehaviorTree-BrBFECv5.d.ts +0 -103
  126. package/dist/InvisibleWallet-rtRrBOA8.d.ts +0 -1732
  127. package/dist/OrchestratorAgent-Q_CbVTmO.d.ts +0 -798
  128. package/dist/agents/index.d.ts +0 -1788
  129. package/dist/agents/index.js +0 -4695
  130. package/dist/ai/index.d.ts +0 -1753
  131. package/dist/ai/index.js +0 -5244
  132. package/dist/behavior.d.ts +0 -130
  133. package/dist/behavior.js +0 -407
  134. package/dist/economy/index.d.ts +0 -747
  135. package/dist/economy/index.js +0 -3617
  136. package/dist/implementations-D9T3un9D.d.ts +0 -236
  137. package/dist/index.d.ts +0 -1729
  138. package/dist/index.js +0 -24277
  139. package/dist/learning/index.d.ts +0 -104
  140. package/dist/learning/index.js +0 -189
  141. package/dist/negotiation/index.d.ts +0 -610
  142. package/dist/negotiation/index.js +0 -931
  143. package/dist/skills/index.d.ts +0 -289
  144. package/dist/skills/index.js +0 -1079
  145. package/dist/swarm/index.d.ts +0 -2433
  146. package/dist/swarm/index.js +0 -5221
  147. package/dist/training/index.d.ts +0 -1734
  148. package/dist/training/index.js +0 -2687
  149. package/extract-failures.js +0 -10
  150. package/src/training/training/data/novel-use-cases.jsonl +0 -153
  151. package/src/training/training/data/spatial-reasoning-10k.jsonl +0 -9354
  152. package/src/types/core-stubs.d.ts +0 -113
  153. package/test-output.txt +0 -0
  154. package/test-result.json +0 -1
  155. package/tsc-errors.txt +0 -4
  156. package/tsc_output.txt +0 -0
  157. package/typescript-errors-2.txt +0 -0
  158. package/typescript-errors.txt +0 -22
  159. package/vitest-log-utf8.txt +0 -268
  160. package/vitest-log.txt +0 -0
@@ -1,130 +0,0 @@
1
- import { B as BTNode } from './BehaviorTree-BrBFECv5.js';
2
- export { A as ActionNode, a as BTContext, b as BehaviorTree, C as ConditionNode, I as InverterNode, N as NodeStatus, R as RepeaterNode, S as SelectorNode, c as SequenceNode, W as WaitNode } from './BehaviorTree-BrBFECv5.js';
3
- import { TraitHandler } from '@holoscript/core';
4
-
5
- /**
6
- * BehaviorPresets.ts
7
- *
8
- * Pre-built behavior tree patterns for common NPC and object behaviors.
9
- */
10
-
11
- /**
12
- * Patrol: Move between waypoints with pauses.
13
- */
14
- declare function createPatrolBehavior(waypointCount?: number, pauseDuration?: number): BTNode;
15
- /**
16
- * Idle: Wait, look around, repeat.
17
- */
18
- declare function createIdleBehavior(idleDuration?: number): BTNode;
19
- /**
20
- * Interact: Approach target, interact, return to origin.
21
- */
22
- declare function createInteractBehavior(): BTNode;
23
- /**
24
- * Follow: Follow a target entity, maintaining distance.
25
- */
26
- declare function createFollowBehavior(minDist?: number): BTNode;
27
- /**
28
- * Alert: Check for threats, flee or fight.
29
- */
30
- declare function createAlertBehavior(): BTNode;
31
-
32
- /**
33
- * StateMachine.ts
34
- *
35
- * Finite State Machine (FSM) for game logic, NPC AI, and UI state management.
36
- * Supports:
37
- * - Named states with enter/update/exit callbacks
38
- * - Guarded transitions (conditions that must be true to transition)
39
- * - Transition actions (side effects on transition)
40
- * - Event-driven and tick-driven transitions
41
- * - State history tracking
42
- */
43
- interface StateConfig {
44
- name: string;
45
- onEnter?: (context: Record<string, unknown>) => void;
46
- onUpdate?: (context: Record<string, unknown>, delta: number) => void;
47
- onExit?: (context: Record<string, unknown>) => void;
48
- }
49
- interface TransitionConfig {
50
- from: string;
51
- to: string;
52
- event?: string;
53
- guard?: (context: Record<string, unknown>) => boolean;
54
- action?: (context: Record<string, unknown>) => void;
55
- }
56
- interface StateMachineConfig {
57
- initialState: string;
58
- states: StateConfig[];
59
- transitions: TransitionConfig[];
60
- context?: Record<string, unknown>;
61
- }
62
- declare class StateMachine {
63
- private states;
64
- private transitions;
65
- private currentStateName;
66
- private previousStateName;
67
- private context;
68
- private history;
69
- private maxHistory;
70
- constructor(config: StateMachineConfig);
71
- /**
72
- * Get the current state name.
73
- */
74
- getCurrentState(): string;
75
- /**
76
- * Get the previous state name.
77
- */
78
- getPreviousState(): string;
79
- /**
80
- * Get the shared context.
81
- */
82
- getContext(): Record<string, unknown>;
83
- /**
84
- * Get state transition history.
85
- */
86
- getHistory(): string[];
87
- /**
88
- * Send an event to the state machine. May trigger a transition.
89
- */
90
- send(event: string): boolean;
91
- /**
92
- * Try to transition based on guards (no event, just check conditions).
93
- * Called automatically during update, or manually.
94
- */
95
- evaluate(): boolean;
96
- /**
97
- * Force a transition to a specific state (bypasses guards).
98
- */
99
- forceTransition(stateName: string): void;
100
- /**
101
- * Update the state machine. Call every frame.
102
- */
103
- update(delta: number): void;
104
- /**
105
- * Check if we're in a specific state.
106
- */
107
- isInState(stateName: string): boolean;
108
- private executeTransition;
109
- private enterState;
110
- }
111
-
112
- /**
113
- * StateTrait.ts
114
- *
115
- * Declarative FSM attachment to HoloScript+ nodes.
116
- * Allows nodes to define states and transitions as trait config.
117
- *
118
- * @trait state
119
- */
120
-
121
- interface StateTraitConfig {
122
- machine: StateMachineConfig;
123
- }
124
- declare const stateTraitHandler: TraitHandler<StateTraitConfig>;
125
- /**
126
- * Get the StateMachine for a given node (for external event sending).
127
- */
128
- declare function getNodeStateMachine(nodeId: string): StateMachine | undefined;
129
-
130
- export { BTNode, type StateConfig, StateMachine, type StateMachineConfig, type StateTraitConfig, type TransitionConfig, createAlertBehavior, createFollowBehavior, createIdleBehavior, createInteractBehavior, createPatrolBehavior, getNodeStateMachine, stateTraitHandler };
package/dist/behavior.js DELETED
@@ -1,407 +0,0 @@
1
- // src/behavior/BehaviorTree.ts
2
- var ActionNode = class {
3
- constructor(name, fn) {
4
- this.name = name;
5
- this.fn = fn;
6
- }
7
- type = "action";
8
- tick(context, delta) {
9
- return this.fn(context, delta);
10
- }
11
- };
12
- var ConditionNode = class {
13
- constructor(name, predicate) {
14
- this.name = name;
15
- this.predicate = predicate;
16
- }
17
- type = "condition";
18
- tick(context) {
19
- return this.predicate(context) ? "success" : "failure";
20
- }
21
- };
22
- var WaitNode = class {
23
- constructor(duration) {
24
- this.duration = duration;
25
- }
26
- type = "wait";
27
- elapsed = 0;
28
- tick(_context, delta) {
29
- this.elapsed += delta;
30
- if (this.elapsed >= this.duration) {
31
- return "success";
32
- }
33
- return "running";
34
- }
35
- reset() {
36
- this.elapsed = 0;
37
- }
38
- };
39
- var SequenceNode = class {
40
- constructor(children) {
41
- this.children = children;
42
- }
43
- type = "sequence";
44
- currentIndex = 0;
45
- tick(context, delta) {
46
- while (this.currentIndex < this.children.length) {
47
- const status = this.children[this.currentIndex].tick(context, delta);
48
- if (status === "failure") {
49
- this.currentIndex = 0;
50
- return "failure";
51
- }
52
- if (status === "running") {
53
- return "running";
54
- }
55
- this.currentIndex++;
56
- }
57
- this.currentIndex = 0;
58
- return "success";
59
- }
60
- reset() {
61
- this.currentIndex = 0;
62
- for (const child of this.children) child.reset?.();
63
- }
64
- };
65
- var SelectorNode = class {
66
- constructor(children) {
67
- this.children = children;
68
- }
69
- type = "selector";
70
- currentIndex = 0;
71
- tick(context, delta) {
72
- while (this.currentIndex < this.children.length) {
73
- const status = this.children[this.currentIndex].tick(context, delta);
74
- if (status === "success") {
75
- this.currentIndex = 0;
76
- return "success";
77
- }
78
- if (status === "running") {
79
- return "running";
80
- }
81
- this.currentIndex++;
82
- }
83
- this.currentIndex = 0;
84
- return "failure";
85
- }
86
- reset() {
87
- this.currentIndex = 0;
88
- for (const child of this.children) child.reset?.();
89
- }
90
- };
91
- var InverterNode = class {
92
- constructor(child) {
93
- this.child = child;
94
- }
95
- type = "inverter";
96
- tick(context, delta) {
97
- const status = this.child.tick(context, delta);
98
- if (status === "success") return "failure";
99
- if (status === "failure") return "success";
100
- return "running";
101
- }
102
- reset() {
103
- this.child.reset?.();
104
- }
105
- };
106
- var RepeaterNode = class {
107
- constructor(child, maxCount = -1) {
108
- this.child = child;
109
- this.maxCount = maxCount;
110
- }
111
- type = "repeater";
112
- count = 0;
113
- tick(context, delta) {
114
- const status = this.child.tick(context, delta);
115
- if (status === "running") return "running";
116
- this.count++;
117
- this.child.reset?.();
118
- if (this.maxCount !== -1 && this.count >= this.maxCount) {
119
- this.count = 0;
120
- return "success";
121
- }
122
- return "running";
123
- }
124
- reset() {
125
- this.count = 0;
126
- this.child.reset?.();
127
- }
128
- };
129
- var BehaviorTree = class {
130
- root;
131
- context;
132
- constructor(root, context = {}) {
133
- this.root = root;
134
- this.context = context;
135
- }
136
- tick(delta) {
137
- return this.root.tick(this.context, delta);
138
- }
139
- getContext() {
140
- return this.context;
141
- }
142
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- matches BTContext index signature
143
- setContext(key, value) {
144
- this.context[key] = value;
145
- }
146
- reset() {
147
- this.root.reset?.();
148
- }
149
- };
150
-
151
- // src/behavior/BehaviorPresets.ts
152
- function createPatrolBehavior(waypointCount = 3, pauseDuration = 2) {
153
- return new RepeaterNode(
154
- new SequenceNode([
155
- new ActionNode("selectNextWaypoint", (ctx) => {
156
- ctx.waypointIndex = ((ctx.waypointIndex || 0) + 1) % waypointCount;
157
- ctx.targetReached = false;
158
- return "success";
159
- }),
160
- new ActionNode("moveToWaypoint", (ctx, delta) => {
161
- ctx.moveProgress = (ctx.moveProgress || 0) + delta * 0.5;
162
- if (ctx.moveProgress >= 1) {
163
- ctx.moveProgress = 0;
164
- ctx.targetReached = true;
165
- return "success";
166
- }
167
- return "running";
168
- }),
169
- new WaitNode(pauseDuration)
170
- ]),
171
- -1
172
- // Infinite loop
173
- );
174
- }
175
- function createIdleBehavior(idleDuration = 3) {
176
- return new RepeaterNode(
177
- new SequenceNode([
178
- new WaitNode(idleDuration),
179
- new ActionNode("lookAround", (ctx) => {
180
- ctx.lookAngle = Math.random() * Math.PI * 2;
181
- return "success";
182
- })
183
- ]),
184
- -1
185
- );
186
- }
187
- function createInteractBehavior() {
188
- return new SequenceNode([
189
- new ConditionNode("hasTarget", (ctx) => !!ctx.interactTarget),
190
- new ActionNode("approach", (ctx, delta) => {
191
- ctx.approachProgress = (ctx.approachProgress || 0) + delta;
192
- if (ctx.approachProgress >= 1.5) {
193
- ctx.approachProgress = 0;
194
- return "success";
195
- }
196
- return "running";
197
- }),
198
- new ActionNode("interact", (ctx) => {
199
- ctx.interactionCount = (ctx.interactionCount || 0) + 1;
200
- ctx.interactTarget = null;
201
- return "success";
202
- })
203
- ]);
204
- }
205
- function createFollowBehavior(minDist = 1.5) {
206
- return new RepeaterNode(
207
- new SelectorNode([
208
- // If close enough, idle
209
- new SequenceNode([
210
- new ConditionNode("closeEnough", (ctx) => {
211
- const dist = ctx.distanceToTarget || 0;
212
- return dist <= minDist;
213
- }),
214
- new WaitNode(0.5)
215
- ]),
216
- // Otherwise, move toward target
217
- new ActionNode("moveToTarget", (ctx, delta) => {
218
- ctx.distanceToTarget = Math.max(0, (ctx.distanceToTarget || 5) - delta * 2);
219
- return ctx.distanceToTarget <= minDist ? "success" : "running";
220
- })
221
- ]),
222
- -1
223
- );
224
- }
225
- function createAlertBehavior() {
226
- return new SelectorNode([
227
- // Fight if brave
228
- new SequenceNode([
229
- new ConditionNode("isBrave", (ctx) => ctx.courage > 0.5),
230
- new ConditionNode("hasThreat", (ctx) => !!ctx.threat),
231
- new ActionNode("fight", (ctx) => {
232
- ctx.fightCount = (ctx.fightCount || 0) + 1;
233
- ctx.threat = null;
234
- return "success";
235
- })
236
- ]),
237
- // Flee if threatened
238
- new SequenceNode([
239
- new ConditionNode("hasThreat", (ctx) => !!ctx.threat),
240
- new ActionNode("flee", (ctx, delta) => {
241
- ctx.fleeProgress = (ctx.fleeProgress || 0) + delta;
242
- if (ctx.fleeProgress >= 2) {
243
- ctx.fleeProgress = 0;
244
- ctx.threat = null;
245
- return "success";
246
- }
247
- return "running";
248
- })
249
- ]),
250
- // Default: idle
251
- new WaitNode(1)
252
- ]);
253
- }
254
-
255
- // src/behavior/StateMachine.ts
256
- var StateMachine = class {
257
- states = /* @__PURE__ */ new Map();
258
- transitions = [];
259
- currentStateName = "";
260
- previousStateName = "";
261
- context;
262
- history = [];
263
- maxHistory = 50;
264
- constructor(config) {
265
- this.context = config.context || {};
266
- for (const state of config.states) {
267
- this.states.set(state.name, state);
268
- }
269
- this.transitions = config.transitions;
270
- this.enterState(config.initialState);
271
- }
272
- /**
273
- * Get the current state name.
274
- */
275
- getCurrentState() {
276
- return this.currentStateName;
277
- }
278
- /**
279
- * Get the previous state name.
280
- */
281
- getPreviousState() {
282
- return this.previousStateName;
283
- }
284
- /**
285
- * Get the shared context.
286
- */
287
- getContext() {
288
- return this.context;
289
- }
290
- /**
291
- * Get state transition history.
292
- */
293
- getHistory() {
294
- return [...this.history];
295
- }
296
- /**
297
- * Send an event to the state machine. May trigger a transition.
298
- */
299
- send(event) {
300
- const transition = this.transitions.find(
301
- (t) => t.from === this.currentStateName && t.event === event && (!t.guard || t.guard(this.context))
302
- );
303
- if (!transition) return false;
304
- this.executeTransition(transition);
305
- return true;
306
- }
307
- /**
308
- * Try to transition based on guards (no event, just check conditions).
309
- * Called automatically during update, or manually.
310
- */
311
- evaluate() {
312
- const transition = this.transitions.find(
313
- (t) => t.from === this.currentStateName && !t.event && t.guard && t.guard(this.context)
314
- );
315
- if (!transition) return false;
316
- this.executeTransition(transition);
317
- return true;
318
- }
319
- /**
320
- * Force a transition to a specific state (bypasses guards).
321
- */
322
- forceTransition(stateName) {
323
- if (!this.states.has(stateName)) return;
324
- const currentState = this.states.get(this.currentStateName);
325
- if (currentState?.onExit) currentState.onExit(this.context);
326
- this.enterState(stateName);
327
- }
328
- /**
329
- * Update the state machine. Call every frame.
330
- */
331
- update(delta) {
332
- const state = this.states.get(this.currentStateName);
333
- if (state?.onUpdate) {
334
- state.onUpdate(this.context, delta);
335
- }
336
- this.evaluate();
337
- }
338
- /**
339
- * Check if we're in a specific state.
340
- */
341
- isInState(stateName) {
342
- return this.currentStateName === stateName;
343
- }
344
- executeTransition(transition) {
345
- const fromState = this.states.get(this.currentStateName);
346
- if (fromState?.onExit) fromState.onExit(this.context);
347
- if (transition.action) transition.action(this.context);
348
- this.enterState(transition.to);
349
- }
350
- enterState(stateName) {
351
- this.previousStateName = this.currentStateName;
352
- this.currentStateName = stateName;
353
- this.history.push(stateName);
354
- if (this.history.length > this.maxHistory) {
355
- this.history.shift();
356
- }
357
- const state = this.states.get(stateName);
358
- if (state?.onEnter) state.onEnter(this.context);
359
- }
360
- };
361
-
362
- // src/behavior/StateTrait.ts
363
- var nodeStateMachines = /* @__PURE__ */ new Map();
364
- var stateTraitHandler = {
365
- name: "state",
366
- defaultConfig: { machine: { initialState: "idle", states: [], transitions: [] } },
367
- onAttach(node, config, _context) {
368
- const nodeId = node.id;
369
- const sm = new StateMachine(config.machine);
370
- nodeStateMachines.set(nodeId, sm);
371
- if (node.properties) {
372
- node.properties._state = sm.getCurrentState();
373
- }
374
- },
375
- onDetach(node, _config, _context) {
376
- nodeStateMachines.delete(node.id);
377
- },
378
- onUpdate(node, _config, _context, delta) {
379
- const sm = nodeStateMachines.get(node.id);
380
- if (!sm) return;
381
- sm.update(delta);
382
- if (node.properties) {
383
- node.properties._state = sm.getCurrentState();
384
- }
385
- }
386
- };
387
- function getNodeStateMachine(nodeId) {
388
- return nodeStateMachines.get(nodeId);
389
- }
390
- export {
391
- ActionNode,
392
- BehaviorTree,
393
- ConditionNode,
394
- InverterNode,
395
- RepeaterNode,
396
- SelectorNode,
397
- SequenceNode,
398
- StateMachine,
399
- WaitNode,
400
- createAlertBehavior,
401
- createFollowBehavior,
402
- createIdleBehavior,
403
- createInteractBehavior,
404
- createPatrolBehavior,
405
- getNodeStateMachine,
406
- stateTraitHandler
407
- };