genesis-ai-cli 7.4.5
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/.env.example +78 -0
- package/README.md +282 -0
- package/dist/src/active-inference/actions.d.ts +75 -0
- package/dist/src/active-inference/actions.js +250 -0
- package/dist/src/active-inference/autonomous-loop.d.ts +103 -0
- package/dist/src/active-inference/autonomous-loop.js +289 -0
- package/dist/src/active-inference/core.d.ts +85 -0
- package/dist/src/active-inference/core.js +555 -0
- package/dist/src/active-inference/demo-autonomous-loop.d.ts +8 -0
- package/dist/src/active-inference/demo-autonomous-loop.js +338 -0
- package/dist/src/active-inference/demo-value-integration.d.ts +8 -0
- package/dist/src/active-inference/demo-value-integration.js +174 -0
- package/dist/src/active-inference/index.d.ts +32 -0
- package/dist/src/active-inference/index.js +88 -0
- package/dist/src/active-inference/integration.d.ts +114 -0
- package/dist/src/active-inference/integration.js +698 -0
- package/dist/src/active-inference/memory-integration.d.ts +51 -0
- package/dist/src/active-inference/memory-integration.js +232 -0
- package/dist/src/active-inference/observations.d.ts +67 -0
- package/dist/src/active-inference/observations.js +147 -0
- package/dist/src/active-inference/test-active-inference.d.ts +8 -0
- package/dist/src/active-inference/test-active-inference.js +320 -0
- package/dist/src/active-inference/test-value-integration.d.ts +6 -0
- package/dist/src/active-inference/test-value-integration.js +168 -0
- package/dist/src/active-inference/types.d.ts +150 -0
- package/dist/src/active-inference/types.js +59 -0
- package/dist/src/active-inference/value-integration.d.ts +164 -0
- package/dist/src/active-inference/value-integration.js +459 -0
- package/dist/src/agents/base-agent.d.ts +53 -0
- package/dist/src/agents/base-agent.js +178 -0
- package/dist/src/agents/builder.d.ts +67 -0
- package/dist/src/agents/builder.js +537 -0
- package/dist/src/agents/critic.d.ts +35 -0
- package/dist/src/agents/critic.js +322 -0
- package/dist/src/agents/ethicist.d.ts +54 -0
- package/dist/src/agents/ethicist.js +393 -0
- package/dist/src/agents/explorer.d.ts +26 -0
- package/dist/src/agents/explorer.js +216 -0
- package/dist/src/agents/feeling.d.ts +41 -0
- package/dist/src/agents/feeling.js +320 -0
- package/dist/src/agents/index.d.ts +111 -0
- package/dist/src/agents/index.js +222 -0
- package/dist/src/agents/memory.d.ts +69 -0
- package/dist/src/agents/memory.js +404 -0
- package/dist/src/agents/message-bus.d.ts +88 -0
- package/dist/src/agents/message-bus.js +267 -0
- package/dist/src/agents/narrator.d.ts +90 -0
- package/dist/src/agents/narrator.js +473 -0
- package/dist/src/agents/planner.d.ts +38 -0
- package/dist/src/agents/planner.js +341 -0
- package/dist/src/agents/predictor.d.ts +73 -0
- package/dist/src/agents/predictor.js +506 -0
- package/dist/src/agents/sensor.d.ts +88 -0
- package/dist/src/agents/sensor.js +377 -0
- package/dist/src/agents/test-agents.d.ts +6 -0
- package/dist/src/agents/test-agents.js +73 -0
- package/dist/src/agents/types.d.ts +194 -0
- package/dist/src/agents/types.js +7 -0
- package/dist/src/brain/index.d.ts +185 -0
- package/dist/src/brain/index.js +843 -0
- package/dist/src/brain/trace.d.ts +91 -0
- package/dist/src/brain/trace.js +327 -0
- package/dist/src/brain/types.d.ts +165 -0
- package/dist/src/brain/types.js +51 -0
- package/dist/src/cli/chat.d.ts +237 -0
- package/dist/src/cli/chat.js +1959 -0
- package/dist/src/cli/dispatcher.d.ts +182 -0
- package/dist/src/cli/dispatcher.js +718 -0
- package/dist/src/cli/human-loop.d.ts +170 -0
- package/dist/src/cli/human-loop.js +543 -0
- package/dist/src/cli/index.d.ts +12 -0
- package/dist/src/cli/index.js +28 -0
- package/dist/src/cli/interactive.d.ts +141 -0
- package/dist/src/cli/interactive.js +757 -0
- package/dist/src/cli/ui.d.ts +205 -0
- package/dist/src/cli/ui.js +632 -0
- package/dist/src/consciousness/attention-schema.d.ts +154 -0
- package/dist/src/consciousness/attention-schema.js +432 -0
- package/dist/src/consciousness/global-workspace.d.ts +149 -0
- package/dist/src/consciousness/global-workspace.js +422 -0
- package/dist/src/consciousness/index.d.ts +186 -0
- package/dist/src/consciousness/index.js +476 -0
- package/dist/src/consciousness/phi-calculator.d.ts +119 -0
- package/dist/src/consciousness/phi-calculator.js +445 -0
- package/dist/src/consciousness/phi-decisions.d.ts +169 -0
- package/dist/src/consciousness/phi-decisions.js +383 -0
- package/dist/src/consciousness/phi-monitor.d.ts +153 -0
- package/dist/src/consciousness/phi-monitor.js +465 -0
- package/dist/src/consciousness/types.d.ts +260 -0
- package/dist/src/consciousness/types.js +44 -0
- package/dist/src/daemon/dream-mode.d.ts +115 -0
- package/dist/src/daemon/dream-mode.js +470 -0
- package/dist/src/daemon/index.d.ts +162 -0
- package/dist/src/daemon/index.js +542 -0
- package/dist/src/daemon/maintenance.d.ts +139 -0
- package/dist/src/daemon/maintenance.js +549 -0
- package/dist/src/daemon/process.d.ts +82 -0
- package/dist/src/daemon/process.js +442 -0
- package/dist/src/daemon/scheduler.d.ts +90 -0
- package/dist/src/daemon/scheduler.js +494 -0
- package/dist/src/daemon/types.d.ts +213 -0
- package/dist/src/daemon/types.js +50 -0
- package/dist/src/epistemic/index.d.ts +74 -0
- package/dist/src/epistemic/index.js +225 -0
- package/dist/src/grounding/epistemic-stack.d.ts +100 -0
- package/dist/src/grounding/epistemic-stack.js +408 -0
- package/dist/src/grounding/feedback.d.ts +98 -0
- package/dist/src/grounding/feedback.js +276 -0
- package/dist/src/grounding/index.d.ts +123 -0
- package/dist/src/grounding/index.js +224 -0
- package/dist/src/grounding/verifier.d.ts +149 -0
- package/dist/src/grounding/verifier.js +484 -0
- package/dist/src/healing/detector.d.ts +110 -0
- package/dist/src/healing/detector.js +436 -0
- package/dist/src/healing/fixer.d.ts +138 -0
- package/dist/src/healing/fixer.js +572 -0
- package/dist/src/healing/index.d.ts +23 -0
- package/dist/src/healing/index.js +43 -0
- package/dist/src/hooks/index.d.ts +135 -0
- package/dist/src/hooks/index.js +317 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.js +1266 -0
- package/dist/src/kernel/index.d.ts +155 -0
- package/dist/src/kernel/index.js +795 -0
- package/dist/src/kernel/invariants.d.ts +153 -0
- package/dist/src/kernel/invariants.js +355 -0
- package/dist/src/kernel/test-kernel.d.ts +6 -0
- package/dist/src/kernel/test-kernel.js +108 -0
- package/dist/src/kernel/test-real-mcp.d.ts +10 -0
- package/dist/src/kernel/test-real-mcp.js +295 -0
- package/dist/src/llm/index.d.ts +146 -0
- package/dist/src/llm/index.js +428 -0
- package/dist/src/llm/router.d.ts +136 -0
- package/dist/src/llm/router.js +510 -0
- package/dist/src/mcp/index.d.ts +85 -0
- package/dist/src/mcp/index.js +657 -0
- package/dist/src/mcp/resilient.d.ts +139 -0
- package/dist/src/mcp/resilient.js +417 -0
- package/dist/src/memory/cache.d.ts +118 -0
- package/dist/src/memory/cache.js +356 -0
- package/dist/src/memory/cognitive-workspace.d.ts +231 -0
- package/dist/src/memory/cognitive-workspace.js +521 -0
- package/dist/src/memory/consolidation.d.ts +99 -0
- package/dist/src/memory/consolidation.js +443 -0
- package/dist/src/memory/episodic.d.ts +114 -0
- package/dist/src/memory/episodic.js +394 -0
- package/dist/src/memory/forgetting.d.ts +134 -0
- package/dist/src/memory/forgetting.js +324 -0
- package/dist/src/memory/index.d.ts +211 -0
- package/dist/src/memory/index.js +367 -0
- package/dist/src/memory/indexer.d.ts +123 -0
- package/dist/src/memory/indexer.js +479 -0
- package/dist/src/memory/procedural.d.ts +136 -0
- package/dist/src/memory/procedural.js +479 -0
- package/dist/src/memory/semantic.d.ts +132 -0
- package/dist/src/memory/semantic.js +497 -0
- package/dist/src/memory/types.d.ts +193 -0
- package/dist/src/memory/types.js +15 -0
- package/dist/src/orchestrator.d.ts +65 -0
- package/dist/src/orchestrator.js +317 -0
- package/dist/src/persistence/index.d.ts +257 -0
- package/dist/src/persistence/index.js +763 -0
- package/dist/src/pipeline/executor.d.ts +51 -0
- package/dist/src/pipeline/executor.js +695 -0
- package/dist/src/pipeline/index.d.ts +7 -0
- package/dist/src/pipeline/index.js +11 -0
- package/dist/src/self-production.d.ts +67 -0
- package/dist/src/self-production.js +205 -0
- package/dist/src/subagents/executor.d.ts +58 -0
- package/dist/src/subagents/executor.js +283 -0
- package/dist/src/subagents/index.d.ts +37 -0
- package/dist/src/subagents/index.js +53 -0
- package/dist/src/subagents/registry.d.ts +23 -0
- package/dist/src/subagents/registry.js +167 -0
- package/dist/src/subagents/types.d.ts +79 -0
- package/dist/src/subagents/types.js +14 -0
- package/dist/src/tools/bash.d.ts +139 -0
- package/dist/src/tools/bash.js +583 -0
- package/dist/src/tools/edit.d.ts +125 -0
- package/dist/src/tools/edit.js +424 -0
- package/dist/src/tools/git.d.ts +179 -0
- package/dist/src/tools/git.js +504 -0
- package/dist/src/tools/index.d.ts +21 -0
- package/dist/src/tools/index.js +163 -0
- package/dist/src/types.d.ts +145 -0
- package/dist/src/types.js +7 -0
- package/dist/src/world-model/decoder.d.ts +163 -0
- package/dist/src/world-model/decoder.js +517 -0
- package/dist/src/world-model/digital-twin.d.ts +219 -0
- package/dist/src/world-model/digital-twin.js +695 -0
- package/dist/src/world-model/encoder.d.ts +141 -0
- package/dist/src/world-model/encoder.js +564 -0
- package/dist/src/world-model/index.d.ts +221 -0
- package/dist/src/world-model/index.js +772 -0
- package/dist/src/world-model/predictor.d.ts +161 -0
- package/dist/src/world-model/predictor.js +681 -0
- package/dist/src/world-model/test-value-jepa.d.ts +8 -0
- package/dist/src/world-model/test-value-jepa.js +430 -0
- package/dist/src/world-model/types.d.ts +341 -0
- package/dist/src/world-model/types.js +69 -0
- package/dist/src/world-model/value-jepa.d.ts +247 -0
- package/dist/src/world-model/value-jepa.js +622 -0
- package/dist/test/brain.test.d.ts +11 -0
- package/dist/test/brain.test.js +358 -0
- package/dist/test/cli/dispatcher.test.d.ts +4 -0
- package/dist/test/cli/dispatcher.test.js +332 -0
- package/dist/test/cli/human-loop.test.d.ts +4 -0
- package/dist/test/cli/human-loop.test.js +270 -0
- package/dist/test/grounding/feedback.test.d.ts +4 -0
- package/dist/test/grounding/feedback.test.js +462 -0
- package/dist/test/grounding/verifier.test.d.ts +4 -0
- package/dist/test/grounding/verifier.test.js +442 -0
- package/dist/test/grounding.test.d.ts +6 -0
- package/dist/test/grounding.test.js +246 -0
- package/dist/test/healing/detector.test.d.ts +4 -0
- package/dist/test/healing/detector.test.js +266 -0
- package/dist/test/healing/fixer.test.d.ts +4 -0
- package/dist/test/healing/fixer.test.js +369 -0
- package/dist/test/integration.test.d.ts +5 -0
- package/dist/test/integration.test.js +290 -0
- package/dist/test/tools/bash.test.d.ts +4 -0
- package/dist/test/tools/bash.test.js +348 -0
- package/dist/test/tools/edit.test.d.ts +4 -0
- package/dist/test/tools/edit.test.js +350 -0
- package/dist/test/tools/git.test.d.ts +4 -0
- package/dist/test/tools/git.test.js +350 -0
- package/package.json +60 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Genesis 6.2 - Autonomous Loop
|
|
3
|
+
*
|
|
4
|
+
* Extracted to avoid circular dependencies.
|
|
5
|
+
* This module provides the AutonomousLoop class for running Active Inference cycles.
|
|
6
|
+
*/
|
|
7
|
+
import { ActiveInferenceEngine } from './core.js';
|
|
8
|
+
import { ObservationGatherer } from './observations.js';
|
|
9
|
+
import { ActionExecutorManager } from './actions.js';
|
|
10
|
+
import { Observation, Beliefs, ActionType } from './types.js';
|
|
11
|
+
export interface AutonomousLoopConfig {
|
|
12
|
+
cycleInterval: number;
|
|
13
|
+
maxCycles: number;
|
|
14
|
+
stopOnGoalAchieved: boolean;
|
|
15
|
+
stopOnEnergyCritical: boolean;
|
|
16
|
+
stopOnHighSurprise: boolean;
|
|
17
|
+
surpriseThreshold: number;
|
|
18
|
+
verbose: boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare const DEFAULT_LOOP_CONFIG: AutonomousLoopConfig;
|
|
21
|
+
export interface LoopStats {
|
|
22
|
+
cycles: number;
|
|
23
|
+
startTime: Date;
|
|
24
|
+
endTime?: Date;
|
|
25
|
+
actions: Record<string, number>;
|
|
26
|
+
avgSurprise: number;
|
|
27
|
+
finalBeliefs: Beliefs;
|
|
28
|
+
}
|
|
29
|
+
export declare class AutonomousLoop {
|
|
30
|
+
private config;
|
|
31
|
+
private engine;
|
|
32
|
+
private observations;
|
|
33
|
+
private actions;
|
|
34
|
+
private running;
|
|
35
|
+
private cycleCount;
|
|
36
|
+
private startTime?;
|
|
37
|
+
private stopReason?;
|
|
38
|
+
private onCycleHandlers;
|
|
39
|
+
private onStopHandlers;
|
|
40
|
+
private customStepFn;
|
|
41
|
+
constructor(config?: Partial<AutonomousLoopConfig>);
|
|
42
|
+
/**
|
|
43
|
+
* Run the autonomous loop
|
|
44
|
+
*/
|
|
45
|
+
run(maxCycles?: number): Promise<LoopStats>;
|
|
46
|
+
/**
|
|
47
|
+
* Run a single inference cycle
|
|
48
|
+
*/
|
|
49
|
+
cycle(): Promise<ActionType>;
|
|
50
|
+
/**
|
|
51
|
+
* Stop the loop
|
|
52
|
+
*/
|
|
53
|
+
stop(reason?: string): void;
|
|
54
|
+
private checkStoppingConditions;
|
|
55
|
+
private handleEngineEvent;
|
|
56
|
+
/**
|
|
57
|
+
* Subscribe to cycle events
|
|
58
|
+
*/
|
|
59
|
+
onCycle(handler: (cycle: number, action: ActionType, beliefs: Beliefs) => void): () => void;
|
|
60
|
+
/**
|
|
61
|
+
* Subscribe to stop events
|
|
62
|
+
*/
|
|
63
|
+
onStop(handler: (reason: string, stats: LoopStats) => void): () => void;
|
|
64
|
+
isRunning(): boolean;
|
|
65
|
+
getCycleCount(): number;
|
|
66
|
+
getBeliefs(): Beliefs;
|
|
67
|
+
getMostLikelyState(): {
|
|
68
|
+
viability: string;
|
|
69
|
+
worldState: string;
|
|
70
|
+
coupling: string;
|
|
71
|
+
goalProgress: string;
|
|
72
|
+
};
|
|
73
|
+
getStats(): LoopStats;
|
|
74
|
+
/**
|
|
75
|
+
* Configure observation sources
|
|
76
|
+
*/
|
|
77
|
+
configureObservations(sources: Parameters<ObservationGatherer['configure']>[0]): void;
|
|
78
|
+
/**
|
|
79
|
+
* Set action context
|
|
80
|
+
*/
|
|
81
|
+
setActionContext(context: Parameters<ActionExecutorManager['setContext']>[0]): void;
|
|
82
|
+
/**
|
|
83
|
+
* Set custom step function for value-augmented inference
|
|
84
|
+
*
|
|
85
|
+
* This allows replacing the default Active Inference engine step
|
|
86
|
+
* with a custom function (e.g., ValueAugmentedEngine.step)
|
|
87
|
+
*/
|
|
88
|
+
setCustomStepFunction(stepFn: ((obs: Observation) => Promise<{
|
|
89
|
+
action: ActionType;
|
|
90
|
+
beliefs: Beliefs;
|
|
91
|
+
}>) | null): void;
|
|
92
|
+
/**
|
|
93
|
+
* Get underlying components for advanced usage
|
|
94
|
+
*/
|
|
95
|
+
getComponents(): {
|
|
96
|
+
engine: ActiveInferenceEngine;
|
|
97
|
+
observations: ObservationGatherer;
|
|
98
|
+
actions: ActionExecutorManager;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export declare function createAutonomousLoop(config?: Partial<AutonomousLoopConfig>): AutonomousLoop;
|
|
102
|
+
export declare function getAutonomousLoop(config?: Partial<AutonomousLoopConfig>): AutonomousLoop;
|
|
103
|
+
export declare function resetAutonomousLoop(): void;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Genesis 6.2 - Autonomous Loop
|
|
4
|
+
*
|
|
5
|
+
* Extracted to avoid circular dependencies.
|
|
6
|
+
* This module provides the AutonomousLoop class for running Active Inference cycles.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.AutonomousLoop = exports.DEFAULT_LOOP_CONFIG = void 0;
|
|
10
|
+
exports.createAutonomousLoop = createAutonomousLoop;
|
|
11
|
+
exports.getAutonomousLoop = getAutonomousLoop;
|
|
12
|
+
exports.resetAutonomousLoop = resetAutonomousLoop;
|
|
13
|
+
const core_js_1 = require("./core.js");
|
|
14
|
+
const observations_js_1 = require("./observations.js");
|
|
15
|
+
const actions_js_1 = require("./actions.js");
|
|
16
|
+
exports.DEFAULT_LOOP_CONFIG = {
|
|
17
|
+
cycleInterval: 1000,
|
|
18
|
+
maxCycles: 0,
|
|
19
|
+
stopOnGoalAchieved: true,
|
|
20
|
+
stopOnEnergyCritical: true,
|
|
21
|
+
stopOnHighSurprise: false,
|
|
22
|
+
surpriseThreshold: 10,
|
|
23
|
+
verbose: false,
|
|
24
|
+
};
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// AutonomousLoop Class
|
|
27
|
+
// ============================================================================
|
|
28
|
+
class AutonomousLoop {
|
|
29
|
+
config;
|
|
30
|
+
// Components
|
|
31
|
+
engine;
|
|
32
|
+
observations;
|
|
33
|
+
actions;
|
|
34
|
+
// State
|
|
35
|
+
running = false;
|
|
36
|
+
cycleCount = 0;
|
|
37
|
+
startTime;
|
|
38
|
+
stopReason;
|
|
39
|
+
// Event handlers
|
|
40
|
+
onCycleHandlers = [];
|
|
41
|
+
onStopHandlers = [];
|
|
42
|
+
// Custom step function (for value integration)
|
|
43
|
+
customStepFn = null;
|
|
44
|
+
constructor(config = {}) {
|
|
45
|
+
this.config = { ...exports.DEFAULT_LOOP_CONFIG, ...config };
|
|
46
|
+
this.engine = (0, core_js_1.createActiveInferenceEngine)();
|
|
47
|
+
this.observations = (0, observations_js_1.createObservationGatherer)();
|
|
48
|
+
this.actions = (0, actions_js_1.createActionExecutorManager)();
|
|
49
|
+
// Subscribe to engine events
|
|
50
|
+
this.engine.on(this.handleEngineEvent.bind(this));
|
|
51
|
+
}
|
|
52
|
+
// ============================================================================
|
|
53
|
+
// Main Loop
|
|
54
|
+
// ============================================================================
|
|
55
|
+
/**
|
|
56
|
+
* Run the autonomous loop
|
|
57
|
+
*/
|
|
58
|
+
async run(maxCycles) {
|
|
59
|
+
if (this.running) {
|
|
60
|
+
throw new Error('Loop is already running');
|
|
61
|
+
}
|
|
62
|
+
this.running = true;
|
|
63
|
+
this.cycleCount = 0;
|
|
64
|
+
this.startTime = new Date();
|
|
65
|
+
this.stopReason = undefined;
|
|
66
|
+
const limit = maxCycles ?? this.config.maxCycles;
|
|
67
|
+
if (this.config.verbose) {
|
|
68
|
+
console.log(`[AI Loop] Starting autonomous loop (max cycles: ${limit || 'unlimited'})`);
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
while (this.running) {
|
|
72
|
+
// Check cycle limit
|
|
73
|
+
if (limit > 0 && this.cycleCount >= limit) {
|
|
74
|
+
this.stopReason = 'cycle_limit';
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
// Run one cycle
|
|
78
|
+
await this.cycle();
|
|
79
|
+
// Wait for next cycle
|
|
80
|
+
if (this.config.cycleInterval > 0) {
|
|
81
|
+
await new Promise(r => setTimeout(r, this.config.cycleInterval));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
this.stopReason = `error: ${error}`;
|
|
87
|
+
if (this.config.verbose) {
|
|
88
|
+
console.error(`[AI Loop] Error:`, error);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this.running = false;
|
|
92
|
+
const stats = this.getStats();
|
|
93
|
+
// Notify stop handlers
|
|
94
|
+
for (const handler of this.onStopHandlers) {
|
|
95
|
+
handler(this.stopReason || 'unknown', stats);
|
|
96
|
+
}
|
|
97
|
+
if (this.config.verbose) {
|
|
98
|
+
console.log(`[AI Loop] Stopped: ${this.stopReason}`);
|
|
99
|
+
console.log(`[AI Loop] Stats:`, stats);
|
|
100
|
+
}
|
|
101
|
+
return stats;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Run a single inference cycle
|
|
105
|
+
*/
|
|
106
|
+
async cycle() {
|
|
107
|
+
this.cycleCount++;
|
|
108
|
+
// 1. Gather observations
|
|
109
|
+
const obs = await this.observations.gather();
|
|
110
|
+
if (this.config.verbose) {
|
|
111
|
+
console.log(`[AI Loop] Cycle ${this.cycleCount} - Observation:`, obs);
|
|
112
|
+
}
|
|
113
|
+
// 2. Run inference (beliefs + policy + action)
|
|
114
|
+
let action;
|
|
115
|
+
let beliefs;
|
|
116
|
+
if (this.customStepFn) {
|
|
117
|
+
// Use custom step function (e.g., value-augmented inference)
|
|
118
|
+
const result = await this.customStepFn(obs);
|
|
119
|
+
action = result.action;
|
|
120
|
+
beliefs = result.beliefs;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// Use default Active Inference engine
|
|
124
|
+
action = this.engine.step(obs);
|
|
125
|
+
beliefs = this.engine.getBeliefs();
|
|
126
|
+
}
|
|
127
|
+
if (this.config.verbose) {
|
|
128
|
+
const state = this.engine.getMostLikelyState();
|
|
129
|
+
console.log(`[AI Loop] Cycle ${this.cycleCount} - Beliefs:`, state);
|
|
130
|
+
console.log(`[AI Loop] Cycle ${this.cycleCount} - Action: ${action}`);
|
|
131
|
+
}
|
|
132
|
+
// 3. Execute action
|
|
133
|
+
const result = await this.actions.execute(action);
|
|
134
|
+
if (this.config.verbose) {
|
|
135
|
+
console.log(`[AI Loop] Cycle ${this.cycleCount} - Result:`, result.success ? 'success' : result.error);
|
|
136
|
+
}
|
|
137
|
+
// 4. Check stopping conditions
|
|
138
|
+
this.checkStoppingConditions(obs);
|
|
139
|
+
// 5. Notify cycle handlers
|
|
140
|
+
for (const handler of this.onCycleHandlers) {
|
|
141
|
+
handler(this.cycleCount, action, beliefs);
|
|
142
|
+
}
|
|
143
|
+
return action;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Stop the loop
|
|
147
|
+
*/
|
|
148
|
+
stop(reason = 'manual') {
|
|
149
|
+
this.stopReason = reason;
|
|
150
|
+
this.running = false;
|
|
151
|
+
}
|
|
152
|
+
// ============================================================================
|
|
153
|
+
// Stopping Conditions
|
|
154
|
+
// ============================================================================
|
|
155
|
+
checkStoppingConditions(obs) {
|
|
156
|
+
// Goal achieved
|
|
157
|
+
if (this.config.stopOnGoalAchieved && obs.task === 3) {
|
|
158
|
+
this.stop('goal_achieved');
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// Energy critical
|
|
162
|
+
if (this.config.stopOnEnergyCritical && obs.energy === 0) {
|
|
163
|
+
this.stop('energy_critical');
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
// High surprise
|
|
167
|
+
if (this.config.stopOnHighSurprise) {
|
|
168
|
+
const stats = this.engine.getStats();
|
|
169
|
+
if (stats.averageSurprise > this.config.surpriseThreshold) {
|
|
170
|
+
this.stop('high_surprise');
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// ============================================================================
|
|
176
|
+
// Event Handling
|
|
177
|
+
// ============================================================================
|
|
178
|
+
handleEngineEvent(event) {
|
|
179
|
+
if (event.type === 'energy_critical' && this.config.stopOnEnergyCritical) {
|
|
180
|
+
this.stop('energy_critical');
|
|
181
|
+
}
|
|
182
|
+
if (event.type === 'goal_achieved' && this.config.stopOnGoalAchieved) {
|
|
183
|
+
this.stop('goal_achieved');
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Subscribe to cycle events
|
|
188
|
+
*/
|
|
189
|
+
onCycle(handler) {
|
|
190
|
+
this.onCycleHandlers.push(handler);
|
|
191
|
+
return () => {
|
|
192
|
+
const idx = this.onCycleHandlers.indexOf(handler);
|
|
193
|
+
if (idx >= 0)
|
|
194
|
+
this.onCycleHandlers.splice(idx, 1);
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Subscribe to stop events
|
|
199
|
+
*/
|
|
200
|
+
onStop(handler) {
|
|
201
|
+
this.onStopHandlers.push(handler);
|
|
202
|
+
return () => {
|
|
203
|
+
const idx = this.onStopHandlers.indexOf(handler);
|
|
204
|
+
if (idx >= 0)
|
|
205
|
+
this.onStopHandlers.splice(idx, 1);
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
// ============================================================================
|
|
209
|
+
// Getters
|
|
210
|
+
// ============================================================================
|
|
211
|
+
isRunning() {
|
|
212
|
+
return this.running;
|
|
213
|
+
}
|
|
214
|
+
getCycleCount() {
|
|
215
|
+
return this.cycleCount;
|
|
216
|
+
}
|
|
217
|
+
getBeliefs() {
|
|
218
|
+
return this.engine.getBeliefs();
|
|
219
|
+
}
|
|
220
|
+
getMostLikelyState() {
|
|
221
|
+
return this.engine.getMostLikelyState();
|
|
222
|
+
}
|
|
223
|
+
getStats() {
|
|
224
|
+
const engineStats = this.engine.getStats();
|
|
225
|
+
return {
|
|
226
|
+
cycles: this.cycleCount,
|
|
227
|
+
startTime: this.startTime || new Date(),
|
|
228
|
+
endTime: this.running ? undefined : new Date(),
|
|
229
|
+
actions: engineStats.actionCounts,
|
|
230
|
+
avgSurprise: engineStats.averageSurprise,
|
|
231
|
+
finalBeliefs: this.engine.getBeliefs(),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
// ============================================================================
|
|
235
|
+
// Configuration
|
|
236
|
+
// ============================================================================
|
|
237
|
+
/**
|
|
238
|
+
* Configure observation sources
|
|
239
|
+
*/
|
|
240
|
+
configureObservations(sources) {
|
|
241
|
+
this.observations.configure(sources);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Set action context
|
|
245
|
+
*/
|
|
246
|
+
setActionContext(context) {
|
|
247
|
+
this.actions.setContext(context);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Set custom step function for value-augmented inference
|
|
251
|
+
*
|
|
252
|
+
* This allows replacing the default Active Inference engine step
|
|
253
|
+
* with a custom function (e.g., ValueAugmentedEngine.step)
|
|
254
|
+
*/
|
|
255
|
+
setCustomStepFunction(stepFn) {
|
|
256
|
+
this.customStepFn = stepFn;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Get underlying components for advanced usage
|
|
260
|
+
*/
|
|
261
|
+
getComponents() {
|
|
262
|
+
return {
|
|
263
|
+
engine: this.engine,
|
|
264
|
+
observations: this.observations,
|
|
265
|
+
actions: this.actions,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
exports.AutonomousLoop = AutonomousLoop;
|
|
270
|
+
// ============================================================================
|
|
271
|
+
// Factory Functions
|
|
272
|
+
// ============================================================================
|
|
273
|
+
function createAutonomousLoop(config) {
|
|
274
|
+
return new AutonomousLoop(config);
|
|
275
|
+
}
|
|
276
|
+
// Singleton instance
|
|
277
|
+
let loopInstance = null;
|
|
278
|
+
function getAutonomousLoop(config) {
|
|
279
|
+
if (!loopInstance) {
|
|
280
|
+
loopInstance = createAutonomousLoop(config);
|
|
281
|
+
}
|
|
282
|
+
return loopInstance;
|
|
283
|
+
}
|
|
284
|
+
function resetAutonomousLoop() {
|
|
285
|
+
if (loopInstance?.isRunning()) {
|
|
286
|
+
loopInstance.stop('reset');
|
|
287
|
+
}
|
|
288
|
+
loopInstance = null;
|
|
289
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Genesis 6.1 - Active Inference Core
|
|
3
|
+
*
|
|
4
|
+
* Pure mathematics for Active Inference (pymdp-style)
|
|
5
|
+
*
|
|
6
|
+
* Based on:
|
|
7
|
+
* - Free Energy Principle (Friston)
|
|
8
|
+
* - pymdp library (Python Active Inference)
|
|
9
|
+
* - RxInfer.jl (reactive message passing)
|
|
10
|
+
*
|
|
11
|
+
* Key functions:
|
|
12
|
+
* - inferStates: Update beliefs given observations
|
|
13
|
+
* - inferPolicies: Select policy by minimizing Expected Free Energy
|
|
14
|
+
* - sampleAction: Sample action from policy
|
|
15
|
+
*
|
|
16
|
+
* NO external dependencies - pure TypeScript math.
|
|
17
|
+
*/
|
|
18
|
+
import { Observation, Beliefs, Policy, ActiveInferenceConfig, ActionType, AIEventHandler } from './types.js';
|
|
19
|
+
export declare class ActiveInferenceEngine {
|
|
20
|
+
private config;
|
|
21
|
+
private A;
|
|
22
|
+
private B;
|
|
23
|
+
private C;
|
|
24
|
+
private D;
|
|
25
|
+
private beliefs;
|
|
26
|
+
private eventHandlers;
|
|
27
|
+
private stats;
|
|
28
|
+
constructor(config?: Partial<ActiveInferenceConfig>);
|
|
29
|
+
/**
|
|
30
|
+
* Update beliefs given observations (state inference)
|
|
31
|
+
*
|
|
32
|
+
* Uses Bayesian inference:
|
|
33
|
+
* P(s|o) ∝ P(o|s) * P(s)
|
|
34
|
+
*
|
|
35
|
+
* Iterates to find fixed point (variational inference)
|
|
36
|
+
*/
|
|
37
|
+
inferStates(observation: Observation): Beliefs;
|
|
38
|
+
/**
|
|
39
|
+
* Infer policies by minimizing Expected Free Energy (EFE)
|
|
40
|
+
*
|
|
41
|
+
* EFE = ambiguity + risk
|
|
42
|
+
* = E[H(o|s,π)] + D_KL[Q(s|π) || P(s)]
|
|
43
|
+
*
|
|
44
|
+
* Lower EFE = better policy
|
|
45
|
+
*/
|
|
46
|
+
inferPolicies(): Policy;
|
|
47
|
+
/**
|
|
48
|
+
* Sample action from policy
|
|
49
|
+
*/
|
|
50
|
+
sampleAction(policy: Policy): ActionType;
|
|
51
|
+
/**
|
|
52
|
+
* Full inference cycle: observe → infer → act
|
|
53
|
+
*/
|
|
54
|
+
step(observation: Observation): ActionType;
|
|
55
|
+
private computeLikelihoods;
|
|
56
|
+
private updateFactor;
|
|
57
|
+
private computeEFE;
|
|
58
|
+
private predictNextState;
|
|
59
|
+
private computeSurprise;
|
|
60
|
+
getBeliefs(): Beliefs;
|
|
61
|
+
getStats(): {
|
|
62
|
+
inferenceCount: number;
|
|
63
|
+
averageSurprise: number;
|
|
64
|
+
actionCounts: {
|
|
65
|
+
[k: string]: number;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
getMostLikelyState(): {
|
|
69
|
+
viability: string;
|
|
70
|
+
worldState: string;
|
|
71
|
+
coupling: string;
|
|
72
|
+
goalProgress: string;
|
|
73
|
+
};
|
|
74
|
+
on(handler: AIEventHandler): () => void;
|
|
75
|
+
private emit;
|
|
76
|
+
/**
|
|
77
|
+
* Update A matrix based on observation-state pairs (supervised)
|
|
78
|
+
*/
|
|
79
|
+
updateAMatrix(observation: Observation, trueState: Partial<Beliefs>): void;
|
|
80
|
+
/**
|
|
81
|
+
* Reset beliefs to priors
|
|
82
|
+
*/
|
|
83
|
+
resetBeliefs(): void;
|
|
84
|
+
}
|
|
85
|
+
export declare function createActiveInferenceEngine(config?: Partial<ActiveInferenceConfig>): ActiveInferenceEngine;
|