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.
Files changed (227) hide show
  1. package/.env.example +78 -0
  2. package/README.md +282 -0
  3. package/dist/src/active-inference/actions.d.ts +75 -0
  4. package/dist/src/active-inference/actions.js +250 -0
  5. package/dist/src/active-inference/autonomous-loop.d.ts +103 -0
  6. package/dist/src/active-inference/autonomous-loop.js +289 -0
  7. package/dist/src/active-inference/core.d.ts +85 -0
  8. package/dist/src/active-inference/core.js +555 -0
  9. package/dist/src/active-inference/demo-autonomous-loop.d.ts +8 -0
  10. package/dist/src/active-inference/demo-autonomous-loop.js +338 -0
  11. package/dist/src/active-inference/demo-value-integration.d.ts +8 -0
  12. package/dist/src/active-inference/demo-value-integration.js +174 -0
  13. package/dist/src/active-inference/index.d.ts +32 -0
  14. package/dist/src/active-inference/index.js +88 -0
  15. package/dist/src/active-inference/integration.d.ts +114 -0
  16. package/dist/src/active-inference/integration.js +698 -0
  17. package/dist/src/active-inference/memory-integration.d.ts +51 -0
  18. package/dist/src/active-inference/memory-integration.js +232 -0
  19. package/dist/src/active-inference/observations.d.ts +67 -0
  20. package/dist/src/active-inference/observations.js +147 -0
  21. package/dist/src/active-inference/test-active-inference.d.ts +8 -0
  22. package/dist/src/active-inference/test-active-inference.js +320 -0
  23. package/dist/src/active-inference/test-value-integration.d.ts +6 -0
  24. package/dist/src/active-inference/test-value-integration.js +168 -0
  25. package/dist/src/active-inference/types.d.ts +150 -0
  26. package/dist/src/active-inference/types.js +59 -0
  27. package/dist/src/active-inference/value-integration.d.ts +164 -0
  28. package/dist/src/active-inference/value-integration.js +459 -0
  29. package/dist/src/agents/base-agent.d.ts +53 -0
  30. package/dist/src/agents/base-agent.js +178 -0
  31. package/dist/src/agents/builder.d.ts +67 -0
  32. package/dist/src/agents/builder.js +537 -0
  33. package/dist/src/agents/critic.d.ts +35 -0
  34. package/dist/src/agents/critic.js +322 -0
  35. package/dist/src/agents/ethicist.d.ts +54 -0
  36. package/dist/src/agents/ethicist.js +393 -0
  37. package/dist/src/agents/explorer.d.ts +26 -0
  38. package/dist/src/agents/explorer.js +216 -0
  39. package/dist/src/agents/feeling.d.ts +41 -0
  40. package/dist/src/agents/feeling.js +320 -0
  41. package/dist/src/agents/index.d.ts +111 -0
  42. package/dist/src/agents/index.js +222 -0
  43. package/dist/src/agents/memory.d.ts +69 -0
  44. package/dist/src/agents/memory.js +404 -0
  45. package/dist/src/agents/message-bus.d.ts +88 -0
  46. package/dist/src/agents/message-bus.js +267 -0
  47. package/dist/src/agents/narrator.d.ts +90 -0
  48. package/dist/src/agents/narrator.js +473 -0
  49. package/dist/src/agents/planner.d.ts +38 -0
  50. package/dist/src/agents/planner.js +341 -0
  51. package/dist/src/agents/predictor.d.ts +73 -0
  52. package/dist/src/agents/predictor.js +506 -0
  53. package/dist/src/agents/sensor.d.ts +88 -0
  54. package/dist/src/agents/sensor.js +377 -0
  55. package/dist/src/agents/test-agents.d.ts +6 -0
  56. package/dist/src/agents/test-agents.js +73 -0
  57. package/dist/src/agents/types.d.ts +194 -0
  58. package/dist/src/agents/types.js +7 -0
  59. package/dist/src/brain/index.d.ts +185 -0
  60. package/dist/src/brain/index.js +843 -0
  61. package/dist/src/brain/trace.d.ts +91 -0
  62. package/dist/src/brain/trace.js +327 -0
  63. package/dist/src/brain/types.d.ts +165 -0
  64. package/dist/src/brain/types.js +51 -0
  65. package/dist/src/cli/chat.d.ts +237 -0
  66. package/dist/src/cli/chat.js +1959 -0
  67. package/dist/src/cli/dispatcher.d.ts +182 -0
  68. package/dist/src/cli/dispatcher.js +718 -0
  69. package/dist/src/cli/human-loop.d.ts +170 -0
  70. package/dist/src/cli/human-loop.js +543 -0
  71. package/dist/src/cli/index.d.ts +12 -0
  72. package/dist/src/cli/index.js +28 -0
  73. package/dist/src/cli/interactive.d.ts +141 -0
  74. package/dist/src/cli/interactive.js +757 -0
  75. package/dist/src/cli/ui.d.ts +205 -0
  76. package/dist/src/cli/ui.js +632 -0
  77. package/dist/src/consciousness/attention-schema.d.ts +154 -0
  78. package/dist/src/consciousness/attention-schema.js +432 -0
  79. package/dist/src/consciousness/global-workspace.d.ts +149 -0
  80. package/dist/src/consciousness/global-workspace.js +422 -0
  81. package/dist/src/consciousness/index.d.ts +186 -0
  82. package/dist/src/consciousness/index.js +476 -0
  83. package/dist/src/consciousness/phi-calculator.d.ts +119 -0
  84. package/dist/src/consciousness/phi-calculator.js +445 -0
  85. package/dist/src/consciousness/phi-decisions.d.ts +169 -0
  86. package/dist/src/consciousness/phi-decisions.js +383 -0
  87. package/dist/src/consciousness/phi-monitor.d.ts +153 -0
  88. package/dist/src/consciousness/phi-monitor.js +465 -0
  89. package/dist/src/consciousness/types.d.ts +260 -0
  90. package/dist/src/consciousness/types.js +44 -0
  91. package/dist/src/daemon/dream-mode.d.ts +115 -0
  92. package/dist/src/daemon/dream-mode.js +470 -0
  93. package/dist/src/daemon/index.d.ts +162 -0
  94. package/dist/src/daemon/index.js +542 -0
  95. package/dist/src/daemon/maintenance.d.ts +139 -0
  96. package/dist/src/daemon/maintenance.js +549 -0
  97. package/dist/src/daemon/process.d.ts +82 -0
  98. package/dist/src/daemon/process.js +442 -0
  99. package/dist/src/daemon/scheduler.d.ts +90 -0
  100. package/dist/src/daemon/scheduler.js +494 -0
  101. package/dist/src/daemon/types.d.ts +213 -0
  102. package/dist/src/daemon/types.js +50 -0
  103. package/dist/src/epistemic/index.d.ts +74 -0
  104. package/dist/src/epistemic/index.js +225 -0
  105. package/dist/src/grounding/epistemic-stack.d.ts +100 -0
  106. package/dist/src/grounding/epistemic-stack.js +408 -0
  107. package/dist/src/grounding/feedback.d.ts +98 -0
  108. package/dist/src/grounding/feedback.js +276 -0
  109. package/dist/src/grounding/index.d.ts +123 -0
  110. package/dist/src/grounding/index.js +224 -0
  111. package/dist/src/grounding/verifier.d.ts +149 -0
  112. package/dist/src/grounding/verifier.js +484 -0
  113. package/dist/src/healing/detector.d.ts +110 -0
  114. package/dist/src/healing/detector.js +436 -0
  115. package/dist/src/healing/fixer.d.ts +138 -0
  116. package/dist/src/healing/fixer.js +572 -0
  117. package/dist/src/healing/index.d.ts +23 -0
  118. package/dist/src/healing/index.js +43 -0
  119. package/dist/src/hooks/index.d.ts +135 -0
  120. package/dist/src/hooks/index.js +317 -0
  121. package/dist/src/index.d.ts +23 -0
  122. package/dist/src/index.js +1266 -0
  123. package/dist/src/kernel/index.d.ts +155 -0
  124. package/dist/src/kernel/index.js +795 -0
  125. package/dist/src/kernel/invariants.d.ts +153 -0
  126. package/dist/src/kernel/invariants.js +355 -0
  127. package/dist/src/kernel/test-kernel.d.ts +6 -0
  128. package/dist/src/kernel/test-kernel.js +108 -0
  129. package/dist/src/kernel/test-real-mcp.d.ts +10 -0
  130. package/dist/src/kernel/test-real-mcp.js +295 -0
  131. package/dist/src/llm/index.d.ts +146 -0
  132. package/dist/src/llm/index.js +428 -0
  133. package/dist/src/llm/router.d.ts +136 -0
  134. package/dist/src/llm/router.js +510 -0
  135. package/dist/src/mcp/index.d.ts +85 -0
  136. package/dist/src/mcp/index.js +657 -0
  137. package/dist/src/mcp/resilient.d.ts +139 -0
  138. package/dist/src/mcp/resilient.js +417 -0
  139. package/dist/src/memory/cache.d.ts +118 -0
  140. package/dist/src/memory/cache.js +356 -0
  141. package/dist/src/memory/cognitive-workspace.d.ts +231 -0
  142. package/dist/src/memory/cognitive-workspace.js +521 -0
  143. package/dist/src/memory/consolidation.d.ts +99 -0
  144. package/dist/src/memory/consolidation.js +443 -0
  145. package/dist/src/memory/episodic.d.ts +114 -0
  146. package/dist/src/memory/episodic.js +394 -0
  147. package/dist/src/memory/forgetting.d.ts +134 -0
  148. package/dist/src/memory/forgetting.js +324 -0
  149. package/dist/src/memory/index.d.ts +211 -0
  150. package/dist/src/memory/index.js +367 -0
  151. package/dist/src/memory/indexer.d.ts +123 -0
  152. package/dist/src/memory/indexer.js +479 -0
  153. package/dist/src/memory/procedural.d.ts +136 -0
  154. package/dist/src/memory/procedural.js +479 -0
  155. package/dist/src/memory/semantic.d.ts +132 -0
  156. package/dist/src/memory/semantic.js +497 -0
  157. package/dist/src/memory/types.d.ts +193 -0
  158. package/dist/src/memory/types.js +15 -0
  159. package/dist/src/orchestrator.d.ts +65 -0
  160. package/dist/src/orchestrator.js +317 -0
  161. package/dist/src/persistence/index.d.ts +257 -0
  162. package/dist/src/persistence/index.js +763 -0
  163. package/dist/src/pipeline/executor.d.ts +51 -0
  164. package/dist/src/pipeline/executor.js +695 -0
  165. package/dist/src/pipeline/index.d.ts +7 -0
  166. package/dist/src/pipeline/index.js +11 -0
  167. package/dist/src/self-production.d.ts +67 -0
  168. package/dist/src/self-production.js +205 -0
  169. package/dist/src/subagents/executor.d.ts +58 -0
  170. package/dist/src/subagents/executor.js +283 -0
  171. package/dist/src/subagents/index.d.ts +37 -0
  172. package/dist/src/subagents/index.js +53 -0
  173. package/dist/src/subagents/registry.d.ts +23 -0
  174. package/dist/src/subagents/registry.js +167 -0
  175. package/dist/src/subagents/types.d.ts +79 -0
  176. package/dist/src/subagents/types.js +14 -0
  177. package/dist/src/tools/bash.d.ts +139 -0
  178. package/dist/src/tools/bash.js +583 -0
  179. package/dist/src/tools/edit.d.ts +125 -0
  180. package/dist/src/tools/edit.js +424 -0
  181. package/dist/src/tools/git.d.ts +179 -0
  182. package/dist/src/tools/git.js +504 -0
  183. package/dist/src/tools/index.d.ts +21 -0
  184. package/dist/src/tools/index.js +163 -0
  185. package/dist/src/types.d.ts +145 -0
  186. package/dist/src/types.js +7 -0
  187. package/dist/src/world-model/decoder.d.ts +163 -0
  188. package/dist/src/world-model/decoder.js +517 -0
  189. package/dist/src/world-model/digital-twin.d.ts +219 -0
  190. package/dist/src/world-model/digital-twin.js +695 -0
  191. package/dist/src/world-model/encoder.d.ts +141 -0
  192. package/dist/src/world-model/encoder.js +564 -0
  193. package/dist/src/world-model/index.d.ts +221 -0
  194. package/dist/src/world-model/index.js +772 -0
  195. package/dist/src/world-model/predictor.d.ts +161 -0
  196. package/dist/src/world-model/predictor.js +681 -0
  197. package/dist/src/world-model/test-value-jepa.d.ts +8 -0
  198. package/dist/src/world-model/test-value-jepa.js +430 -0
  199. package/dist/src/world-model/types.d.ts +341 -0
  200. package/dist/src/world-model/types.js +69 -0
  201. package/dist/src/world-model/value-jepa.d.ts +247 -0
  202. package/dist/src/world-model/value-jepa.js +622 -0
  203. package/dist/test/brain.test.d.ts +11 -0
  204. package/dist/test/brain.test.js +358 -0
  205. package/dist/test/cli/dispatcher.test.d.ts +4 -0
  206. package/dist/test/cli/dispatcher.test.js +332 -0
  207. package/dist/test/cli/human-loop.test.d.ts +4 -0
  208. package/dist/test/cli/human-loop.test.js +270 -0
  209. package/dist/test/grounding/feedback.test.d.ts +4 -0
  210. package/dist/test/grounding/feedback.test.js +462 -0
  211. package/dist/test/grounding/verifier.test.d.ts +4 -0
  212. package/dist/test/grounding/verifier.test.js +442 -0
  213. package/dist/test/grounding.test.d.ts +6 -0
  214. package/dist/test/grounding.test.js +246 -0
  215. package/dist/test/healing/detector.test.d.ts +4 -0
  216. package/dist/test/healing/detector.test.js +266 -0
  217. package/dist/test/healing/fixer.test.d.ts +4 -0
  218. package/dist/test/healing/fixer.test.js +369 -0
  219. package/dist/test/integration.test.d.ts +5 -0
  220. package/dist/test/integration.test.js +290 -0
  221. package/dist/test/tools/bash.test.d.ts +4 -0
  222. package/dist/test/tools/bash.test.js +348 -0
  223. package/dist/test/tools/edit.test.d.ts +4 -0
  224. package/dist/test/tools/edit.test.js +350 -0
  225. package/dist/test/tools/git.test.d.ts +4 -0
  226. package/dist/test/tools/git.test.js +350 -0
  227. package/package.json +60 -0
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Genesis 6.0 - Global Workspace Theory (GWT)
3
+ *
4
+ * Implementation of Baars' Global Workspace Theory.
5
+ *
6
+ * Key concept: Consciousness arises when specialized modules
7
+ * compete for access to a limited-capacity "global workspace".
8
+ * The winning content is then "broadcast" to all modules,
9
+ * creating a moment of conscious awareness.
10
+ *
11
+ * The workspace acts like a "blackboard" where one piece of
12
+ * information becomes globally available (ignition).
13
+ *
14
+ * References:
15
+ * - Baars, B.J. (1988). A Cognitive Theory of Consciousness.
16
+ * - Baars, B.J. (2005). Global workspace theory of consciousness.
17
+ * - Dehaene, S. & Naccache, L. (2001). Towards a cognitive neuroscience of consciousness.
18
+ *
19
+ * Usage:
20
+ * ```typescript
21
+ * import { createGlobalWorkspace } from './consciousness/global-workspace.js';
22
+ *
23
+ * const workspace = createGlobalWorkspace({ capacity: 7 });
24
+ *
25
+ * // Register modules
26
+ * workspace.registerModule(perceptionModule);
27
+ * workspace.registerModule(memoryModule);
28
+ *
29
+ * // Run competition cycle
30
+ * workspace.cycle();
31
+ *
32
+ * // Get current conscious content
33
+ * const content = workspace.getCurrentContent();
34
+ * ```
35
+ */
36
+ import { WorkspaceModule, WorkspaceContent, WorkspaceState, IgnitionEvent, ContentType, ModuleType } from './types.js';
37
+ export interface GlobalWorkspaceConfig {
38
+ capacity: number;
39
+ selectionIntervalMs: number;
40
+ broadcastTimeoutMs: number;
41
+ historyLimit: number;
42
+ salienceWeight: number;
43
+ relevanceWeight: number;
44
+ decayRate: number;
45
+ }
46
+ export declare const DEFAULT_GWT_CONFIG: GlobalWorkspaceConfig;
47
+ /**
48
+ * Adapter for creating workspace modules from Genesis agents
49
+ */
50
+ export interface ModuleAdapter {
51
+ id: string;
52
+ name: string;
53
+ type: ModuleType;
54
+ active: boolean;
55
+ load: number;
56
+ onPropose: () => WorkspaceContent | null;
57
+ onReceive: (content: WorkspaceContent) => void;
58
+ onSalience: () => number;
59
+ onRelevance: (goal: string) => number;
60
+ }
61
+ /**
62
+ * Create a workspace module from an adapter
63
+ */
64
+ export declare function createModule(adapter: ModuleAdapter): WorkspaceModule;
65
+ export type GWTEventType = 'module_registered' | 'module_removed' | 'content_proposed' | 'content_selected' | 'ignition' | 'broadcast_complete' | 'workspace_cleared';
66
+ export type GWTEventHandler = (event: {
67
+ type: GWTEventType;
68
+ data?: unknown;
69
+ }) => void;
70
+ export declare class GlobalWorkspace {
71
+ private config;
72
+ private modules;
73
+ private state;
74
+ private currentGoal;
75
+ private cycleTimer;
76
+ private running;
77
+ private eventHandlers;
78
+ constructor(config?: Partial<GlobalWorkspaceConfig>);
79
+ start(): void;
80
+ stop(): void;
81
+ isRunning(): boolean;
82
+ registerModule(module: WorkspaceModule): void;
83
+ removeModule(moduleId: string): boolean;
84
+ getModules(): WorkspaceModule[];
85
+ getModule(moduleId: string): WorkspaceModule | undefined;
86
+ setGoal(goal: string): void;
87
+ getGoal(): string;
88
+ /**
89
+ * Run one competition cycle
90
+ * 1. Gather proposals from modules
91
+ * 2. Score candidates
92
+ * 3. Select winner
93
+ * 4. Broadcast to all modules
94
+ */
95
+ cycle(): IgnitionEvent | null;
96
+ /**
97
+ * Gather proposals from all active modules
98
+ */
99
+ private gatherProposals;
100
+ /**
101
+ * Score candidates based on salience and relevance
102
+ */
103
+ private scoreCandidates;
104
+ /**
105
+ * Select the winning candidate
106
+ */
107
+ private selectWinner;
108
+ /**
109
+ * Softmax-like selection with temperature
110
+ */
111
+ private softmaxSelect;
112
+ /**
113
+ * Ignite - content enters the workspace
114
+ */
115
+ private ignite;
116
+ /**
117
+ * Broadcast winning content to all modules
118
+ */
119
+ private broadcast;
120
+ /**
121
+ * Decay current content if nothing new selected
122
+ */
123
+ private decayContent;
124
+ /**
125
+ * Get boost for content type based on current context
126
+ */
127
+ private getTypeBoost;
128
+ getState(): WorkspaceState;
129
+ getCurrentContent(): WorkspaceContent | null;
130
+ getHistory(): WorkspaceContent[];
131
+ isIgnited(): boolean;
132
+ clear(): void;
133
+ on(handler: GWTEventHandler): () => void;
134
+ private emit;
135
+ private createInitialState;
136
+ stats(): {
137
+ modules: number;
138
+ isIgnited: boolean;
139
+ selectionCount: number;
140
+ historyLength: number;
141
+ currentContentType: ContentType | null;
142
+ };
143
+ }
144
+ export declare function createWorkspaceContent(sourceModule: string, type: ContentType, data: unknown, options?: {
145
+ salience?: number;
146
+ relevance?: number;
147
+ ttl?: number;
148
+ }): WorkspaceContent;
149
+ export declare function createGlobalWorkspace(config?: Partial<GlobalWorkspaceConfig>): GlobalWorkspace;
@@ -0,0 +1,422 @@
1
+ "use strict";
2
+ /**
3
+ * Genesis 6.0 - Global Workspace Theory (GWT)
4
+ *
5
+ * Implementation of Baars' Global Workspace Theory.
6
+ *
7
+ * Key concept: Consciousness arises when specialized modules
8
+ * compete for access to a limited-capacity "global workspace".
9
+ * The winning content is then "broadcast" to all modules,
10
+ * creating a moment of conscious awareness.
11
+ *
12
+ * The workspace acts like a "blackboard" where one piece of
13
+ * information becomes globally available (ignition).
14
+ *
15
+ * References:
16
+ * - Baars, B.J. (1988). A Cognitive Theory of Consciousness.
17
+ * - Baars, B.J. (2005). Global workspace theory of consciousness.
18
+ * - Dehaene, S. & Naccache, L. (2001). Towards a cognitive neuroscience of consciousness.
19
+ *
20
+ * Usage:
21
+ * ```typescript
22
+ * import { createGlobalWorkspace } from './consciousness/global-workspace.js';
23
+ *
24
+ * const workspace = createGlobalWorkspace({ capacity: 7 });
25
+ *
26
+ * // Register modules
27
+ * workspace.registerModule(perceptionModule);
28
+ * workspace.registerModule(memoryModule);
29
+ *
30
+ * // Run competition cycle
31
+ * workspace.cycle();
32
+ *
33
+ * // Get current conscious content
34
+ * const content = workspace.getCurrentContent();
35
+ * ```
36
+ */
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.GlobalWorkspace = exports.DEFAULT_GWT_CONFIG = void 0;
39
+ exports.createModule = createModule;
40
+ exports.createWorkspaceContent = createWorkspaceContent;
41
+ exports.createGlobalWorkspace = createGlobalWorkspace;
42
+ const crypto_1 = require("crypto");
43
+ exports.DEFAULT_GWT_CONFIG = {
44
+ capacity: 7, // Miller's 7 +/- 2
45
+ selectionIntervalMs: 100, // 10 Hz
46
+ broadcastTimeoutMs: 1000,
47
+ historyLimit: 100,
48
+ salienceWeight: 0.6,
49
+ relevanceWeight: 0.4,
50
+ decayRate: 0.1,
51
+ };
52
+ /**
53
+ * Create a workspace module from an adapter
54
+ */
55
+ function createModule(adapter) {
56
+ return {
57
+ id: adapter.id,
58
+ name: adapter.name,
59
+ type: adapter.type,
60
+ active: adapter.active,
61
+ load: adapter.load,
62
+ canPropose() {
63
+ return adapter.active && adapter.load < 0.9;
64
+ },
65
+ propose() {
66
+ return adapter.onPropose();
67
+ },
68
+ receive(content) {
69
+ adapter.onReceive(content);
70
+ },
71
+ bottomUpSalience() {
72
+ return adapter.onSalience();
73
+ },
74
+ topDownRelevance(goal) {
75
+ return adapter.onRelevance(goal);
76
+ },
77
+ };
78
+ }
79
+ class GlobalWorkspace {
80
+ config;
81
+ modules = new Map();
82
+ state;
83
+ currentGoal = '';
84
+ cycleTimer = null;
85
+ running = false;
86
+ eventHandlers = new Set();
87
+ constructor(config = {}) {
88
+ this.config = { ...exports.DEFAULT_GWT_CONFIG, ...config };
89
+ this.state = this.createInitialState();
90
+ }
91
+ // ============================================================================
92
+ // Lifecycle
93
+ // ============================================================================
94
+ start() {
95
+ if (this.running)
96
+ return;
97
+ this.running = true;
98
+ this.cycleTimer = setInterval(() => this.cycle(), this.config.selectionIntervalMs);
99
+ }
100
+ stop() {
101
+ if (!this.running)
102
+ return;
103
+ this.running = false;
104
+ if (this.cycleTimer) {
105
+ clearInterval(this.cycleTimer);
106
+ this.cycleTimer = null;
107
+ }
108
+ }
109
+ isRunning() {
110
+ return this.running;
111
+ }
112
+ // ============================================================================
113
+ // Module Management
114
+ // ============================================================================
115
+ registerModule(module) {
116
+ this.modules.set(module.id, module);
117
+ this.emit({ type: 'module_registered', data: { moduleId: module.id } });
118
+ }
119
+ removeModule(moduleId) {
120
+ const removed = this.modules.delete(moduleId);
121
+ if (removed) {
122
+ this.emit({ type: 'module_removed', data: { moduleId } });
123
+ }
124
+ return removed;
125
+ }
126
+ getModules() {
127
+ return Array.from(this.modules.values());
128
+ }
129
+ getModule(moduleId) {
130
+ return this.modules.get(moduleId);
131
+ }
132
+ // ============================================================================
133
+ // Goal Management
134
+ // ============================================================================
135
+ setGoal(goal) {
136
+ this.currentGoal = goal;
137
+ }
138
+ getGoal() {
139
+ return this.currentGoal;
140
+ }
141
+ // ============================================================================
142
+ // Competition Cycle
143
+ // ============================================================================
144
+ /**
145
+ * Run one competition cycle
146
+ * 1. Gather proposals from modules
147
+ * 2. Score candidates
148
+ * 3. Select winner
149
+ * 4. Broadcast to all modules
150
+ */
151
+ cycle() {
152
+ // 1. Gather proposals
153
+ const candidates = this.gatherProposals();
154
+ if (candidates.length === 0) {
155
+ // Decay current content
156
+ this.decayContent();
157
+ return null;
158
+ }
159
+ // 2. Score candidates
160
+ this.scoreCandidates(candidates);
161
+ // 3. Select winner
162
+ const winner = this.selectWinner(candidates);
163
+ if (!winner) {
164
+ this.decayContent();
165
+ return null;
166
+ }
167
+ // 4. Ignition - content enters workspace
168
+ const ignition = this.ignite(winner, candidates.length);
169
+ // 5. Broadcast to all modules
170
+ this.broadcast(winner.content);
171
+ return ignition;
172
+ }
173
+ /**
174
+ * Gather proposals from all active modules
175
+ */
176
+ gatherProposals() {
177
+ const candidates = [];
178
+ for (const module of this.modules.values()) {
179
+ if (!module.canPropose())
180
+ continue;
181
+ try {
182
+ const content = module.propose();
183
+ if (content) {
184
+ candidates.push({
185
+ content,
186
+ module: module.id,
187
+ score: 0,
188
+ selected: false,
189
+ });
190
+ this.emit({ type: 'content_proposed', data: { moduleId: module.id, content } });
191
+ }
192
+ }
193
+ catch (err) {
194
+ console.error(`Module ${module.id} proposal error:`, err);
195
+ }
196
+ }
197
+ return candidates;
198
+ }
199
+ /**
200
+ * Score candidates based on salience and relevance
201
+ */
202
+ scoreCandidates(candidates) {
203
+ for (const candidate of candidates) {
204
+ const module = this.modules.get(candidate.module);
205
+ if (!module)
206
+ continue;
207
+ const salience = module.bottomUpSalience();
208
+ const relevance = module.topDownRelevance(this.currentGoal);
209
+ candidate.score =
210
+ this.config.salienceWeight * salience +
211
+ this.config.relevanceWeight * relevance;
212
+ // Boost for content type matching current needs
213
+ candidate.score += this.getTypeBoost(candidate.content.type);
214
+ }
215
+ // Update state
216
+ this.state.candidates = candidates;
217
+ }
218
+ /**
219
+ * Select the winning candidate
220
+ */
221
+ selectWinner(candidates) {
222
+ if (candidates.length === 0)
223
+ return null;
224
+ // Sort by score
225
+ candidates.sort((a, b) => b.score - a.score);
226
+ // Apply some stochasticity (softmax-like selection)
227
+ const winner = this.softmaxSelect(candidates);
228
+ if (winner) {
229
+ winner.selected = true;
230
+ this.emit({ type: 'content_selected', data: { candidate: winner } });
231
+ }
232
+ return winner;
233
+ }
234
+ /**
235
+ * Softmax-like selection with temperature
236
+ */
237
+ softmaxSelect(candidates) {
238
+ if (candidates.length === 0)
239
+ return null;
240
+ // Temperature for selection (lower = more deterministic)
241
+ const temperature = 0.5;
242
+ // Calculate softmax probabilities
243
+ const maxScore = Math.max(...candidates.map((c) => c.score));
244
+ const expScores = candidates.map((c) => Math.exp((c.score - maxScore) / temperature));
245
+ const sumExp = expScores.reduce((a, b) => a + b, 0);
246
+ const probs = expScores.map((e) => e / sumExp);
247
+ // Sample from distribution
248
+ const rand = Math.random();
249
+ let cumProb = 0;
250
+ for (let i = 0; i < candidates.length; i++) {
251
+ cumProb += probs[i];
252
+ if (rand < cumProb) {
253
+ return candidates[i];
254
+ }
255
+ }
256
+ return candidates[0]; // Fallback to highest score
257
+ }
258
+ /**
259
+ * Ignite - content enters the workspace
260
+ */
261
+ ignite(winner, competitorCount) {
262
+ const now = new Date();
263
+ // Update workspace state
264
+ this.state.current = winner.content;
265
+ this.state.ignited = true;
266
+ this.state.ignitionTime = now;
267
+ this.state.selectionCount++;
268
+ // Create ignition event
269
+ const ignition = {
270
+ content: winner.content,
271
+ timestamp: now,
272
+ competitorCount,
273
+ winningScore: winner.score,
274
+ modulesNotified: [],
275
+ duration: 0,
276
+ };
277
+ this.state.lastSelection = now;
278
+ this.emit({ type: 'ignition', data: ignition });
279
+ return ignition;
280
+ }
281
+ /**
282
+ * Broadcast winning content to all modules
283
+ */
284
+ broadcast(content) {
285
+ const notified = [];
286
+ const startTime = Date.now();
287
+ for (const module of this.modules.values()) {
288
+ try {
289
+ module.receive(content);
290
+ notified.push(module.id);
291
+ }
292
+ catch (err) {
293
+ console.error(`Module ${module.id} receive error:`, err);
294
+ }
295
+ // Check timeout
296
+ if (Date.now() - startTime > this.config.broadcastTimeoutMs) {
297
+ console.warn('Broadcast timeout reached');
298
+ break;
299
+ }
300
+ }
301
+ // Add to history
302
+ this.state.history.unshift(content);
303
+ if (this.state.history.length > this.config.historyLimit) {
304
+ this.state.history.pop();
305
+ }
306
+ this.emit({ type: 'broadcast_complete', data: { content, notified } });
307
+ }
308
+ /**
309
+ * Decay current content if nothing new selected
310
+ */
311
+ decayContent() {
312
+ if (this.state.current) {
313
+ this.state.current.salience *= (1 - this.config.decayRate);
314
+ // Remove if too weak
315
+ if (this.state.current.salience < 0.1) {
316
+ this.state.current = null;
317
+ this.state.ignited = false;
318
+ }
319
+ }
320
+ }
321
+ /**
322
+ * Get boost for content type based on current context
323
+ */
324
+ getTypeBoost(type) {
325
+ // Could be more sophisticated based on current state
326
+ const boosts = {
327
+ goal: 0.2, // Goals get priority
328
+ emotion: 0.15, // Emotions are salient
329
+ attention: 0.1, // Attention signals matter
330
+ percept: 0.05, // Perceptual input
331
+ memory: 0.0, // Neutral
332
+ plan: 0.0,
333
+ thought: 0.0,
334
+ };
335
+ return boosts[type] || 0;
336
+ }
337
+ // ============================================================================
338
+ // State Access
339
+ // ============================================================================
340
+ getState() {
341
+ return { ...this.state };
342
+ }
343
+ getCurrentContent() {
344
+ return this.state.current;
345
+ }
346
+ getHistory() {
347
+ return [...this.state.history];
348
+ }
349
+ isIgnited() {
350
+ return this.state.ignited;
351
+ }
352
+ clear() {
353
+ this.state = this.createInitialState();
354
+ this.emit({ type: 'workspace_cleared' });
355
+ }
356
+ // ============================================================================
357
+ // Events
358
+ // ============================================================================
359
+ on(handler) {
360
+ this.eventHandlers.add(handler);
361
+ return () => this.eventHandlers.delete(handler);
362
+ }
363
+ emit(event) {
364
+ for (const handler of this.eventHandlers) {
365
+ try {
366
+ handler(event);
367
+ }
368
+ catch (err) {
369
+ console.error('GWT event handler error:', err);
370
+ }
371
+ }
372
+ }
373
+ // ============================================================================
374
+ // Helpers
375
+ // ============================================================================
376
+ createInitialState() {
377
+ return {
378
+ current: null,
379
+ history: [],
380
+ historyLimit: this.config.historyLimit,
381
+ candidates: [],
382
+ lastSelection: new Date(),
383
+ selectionCount: 0,
384
+ ignited: false,
385
+ ignitionTime: null,
386
+ };
387
+ }
388
+ // ============================================================================
389
+ // Stats
390
+ // ============================================================================
391
+ stats() {
392
+ return {
393
+ modules: this.modules.size,
394
+ isIgnited: this.state.ignited,
395
+ selectionCount: this.state.selectionCount,
396
+ historyLength: this.state.history.length,
397
+ currentContentType: this.state.current?.type || null,
398
+ };
399
+ }
400
+ }
401
+ exports.GlobalWorkspace = GlobalWorkspace;
402
+ // ============================================================================
403
+ // Content Factory
404
+ // ============================================================================
405
+ function createWorkspaceContent(sourceModule, type, data, options = {}) {
406
+ return {
407
+ id: (0, crypto_1.randomUUID)(),
408
+ sourceModule,
409
+ type,
410
+ data,
411
+ salience: options.salience ?? 0.5,
412
+ relevance: options.relevance ?? 0.5,
413
+ timestamp: new Date(),
414
+ ttl: options.ttl ?? 1000,
415
+ };
416
+ }
417
+ // ============================================================================
418
+ // Factory
419
+ // ============================================================================
420
+ function createGlobalWorkspace(config) {
421
+ return new GlobalWorkspace(config);
422
+ }