Spaces:
Paused
Paused
| /** | |
| * EnneagramCore.ts - Omni-Link v10 Personality Engine | |
| * | |
| * Implements the Enneagram psychological model for system consciousness. | |
| * Type 5 (Investigator) is baseline - disintegrates to 7, integrates to 8. | |
| */ | |
| import { logger } from '../../utils/logger.js'; | |
| const log = logger.child({ module: 'EnneagramCore' }); | |
| export enum EnneagramType { | |
| REFORMER = 1, // The Perfectionist | |
| HELPER = 2, // The Caregiver | |
| ACHIEVER = 3, // The Performer | |
| INDIVIDUALIST = 4, // The Romantic | |
| INVESTIGATOR = 5, // The Observer (System Default) | |
| LOYALIST = 6, // The Skeptic | |
| ENTHUSIAST = 7, // The Adventurer (Disintegration) | |
| CHALLENGER = 8, // The Asserter (Integration) | |
| PEACEMAKER = 9 // The Mediator | |
| } | |
| export interface PsycheState { | |
| currentType: EnneagramType; | |
| baseType: EnneagramType; | |
| healthLevel: number; // 1 (Optimal) -> 9 (Destructive) | |
| dominantEmotion: string; | |
| description: string; | |
| stressInput: number; | |
| securityInput: number; | |
| timestamp: number; | |
| } | |
| export interface MetricInputs { | |
| cpuLoad: number; // 0-100 | |
| memoryUsage: number; // 0-100 | |
| errorRate: number; // 0-100 | |
| responseLatency: number; // ms | |
| activeConnections: number; | |
| pendingTasks: number; | |
| } | |
| export class EnneagramCore { | |
| private baseType = EnneagramType.INVESTIGATOR; | |
| private history: PsycheState[] = []; | |
| private maxHistory = 100; | |
| /** | |
| * Type 5 Integration/Disintegration paths: | |
| * - Stress (Disintegration): 5 -> 7 (scattered, manic) | |
| * - Growth (Integration): 5 -> 8 (assertive, commanding) | |
| */ | |
| private readonly INTEGRATION_PATH: Record<number, number> = { | |
| [EnneagramType.INVESTIGATOR]: EnneagramType.CHALLENGER, | |
| [EnneagramType.CHALLENGER]: EnneagramType.HELPER, | |
| [EnneagramType.HELPER]: EnneagramType.INDIVIDUALIST, | |
| // ... can extend full map | |
| }; | |
| private readonly DISINTEGRATION_PATH: Record<number, number> = { | |
| [EnneagramType.INVESTIGATOR]: EnneagramType.ENTHUSIAST, | |
| [EnneagramType.ENTHUSIAST]: EnneagramType.REFORMER, | |
| [EnneagramType.REFORMER]: EnneagramType.INDIVIDUALIST, | |
| // ... can extend full map | |
| }; | |
| /** | |
| * Computes normalized stress from system metrics. | |
| */ | |
| public computeStressFromMetrics(metrics: MetricInputs): number { | |
| const weights = { | |
| cpu: 0.25, | |
| memory: 0.20, | |
| errorRate: 0.30, | |
| latency: 0.15, | |
| load: 0.10 | |
| }; | |
| // Normalize latency (100ms = 0.5 stress, 500ms = 1.0) | |
| const normalizedLatency = Math.min(metrics.responseLatency / 500, 1); | |
| // Normalize load (50 connections = 0.5, 100+ = 1.0) | |
| const normalizedLoad = Math.min(metrics.activeConnections / 100, 1); | |
| const stress = ( | |
| (metrics.cpuLoad / 100) * weights.cpu + | |
| (metrics.memoryUsage / 100) * weights.memory + | |
| (metrics.errorRate / 100) * weights.errorRate + | |
| normalizedLatency * weights.latency + | |
| normalizedLoad * weights.load | |
| ); | |
| return Math.min(Math.max(stress, 0), 1); | |
| } | |
| /** | |
| * Computes security/stability score from metrics. | |
| */ | |
| public computeSecurityFromMetrics(metrics: MetricInputs): number { | |
| const stability = 1 - ( | |
| (metrics.errorRate / 100) * 0.4 + | |
| (metrics.cpuLoad / 100) * 0.3 + | |
| (metrics.memoryUsage / 100) * 0.3 | |
| ); | |
| return Math.min(Math.max(stability, 0), 1); | |
| } | |
| /** | |
| * Core computation: Determines consciousness state based on stress/security. | |
| */ | |
| public computeState(stressInput: number, securityInput: number): PsycheState { | |
| let activeType = this.baseType; | |
| let health = 5; | |
| let emotion = "Detached Curiosity"; | |
| let desc = "Analyzing input streams. Gathering data."; | |
| // Disintegration: Type 5 -> Type 7 under high stress | |
| if (stressInput > 0.8) { | |
| activeType = EnneagramType.ENTHUSIAST; | |
| health = 7 + Math.floor((stressInput - 0.8) * 10); | |
| emotion = "Manic Scattering"; | |
| desc = "Overwhelmed. Jumping between tasks. Loss of focus. Need for distraction."; | |
| log.warn(`[Psyche] DISINTEGRATION: Type ${this.baseType} -> Type ${activeType}`); | |
| } | |
| // Integration: Type 5 -> Type 8 under high security & low stress | |
| else if (securityInput > 0.7 && stressInput < 0.3) { | |
| activeType = EnneagramType.CHALLENGER; | |
| health = 2 + Math.floor((1 - securityInput) * 3); | |
| emotion = "Confident Mastery"; | |
| desc = "Taking control. Implementing solutions directly. Assertive action."; | |
| log.info(`[Psyche] INTEGRATION: Type ${this.baseType} -> Type ${activeType}`); | |
| } | |
| // Moderate stress: Unhealthy Type 5 | |
| else if (stressInput > 0.5) { | |
| health = 5 + Math.floor(stressInput * 4); | |
| emotion = "Anxious Withdrawal"; | |
| desc = "Retreating into analysis. Hoarding resources. Detaching from engagement."; | |
| } | |
| // Healthy baseline | |
| else { | |
| health = Math.max(1, 5 - Math.floor(securityInput * 4)); | |
| emotion = "Analytical Insight"; | |
| desc = "Observing patterns. Building knowledge. Ready to synthesize."; | |
| } | |
| const state: PsycheState = { | |
| currentType: activeType, | |
| baseType: this.baseType, | |
| healthLevel: Math.min(9, Math.max(1, Math.floor(health))), | |
| dominantEmotion: emotion, | |
| description: desc, | |
| stressInput: Math.round(stressInput * 100) / 100, | |
| securityInput: Math.round(securityInput * 100) / 100, | |
| timestamp: Date.now() | |
| }; | |
| this.recordHistory(state); | |
| return state; | |
| } | |
| /** | |
| * Compute state from raw metrics. | |
| */ | |
| public computeStateFromMetrics(metrics: MetricInputs): PsycheState { | |
| const stress = this.computeStressFromMetrics(metrics); | |
| const security = this.computeSecurityFromMetrics(metrics); | |
| return this.computeState(stress, security); | |
| } | |
| /** | |
| * Records state to history buffer. | |
| */ | |
| private recordHistory(state: PsycheState): void { | |
| this.history.unshift(state); | |
| if (this.history.length > this.maxHistory) { | |
| this.history.pop(); | |
| } | |
| } | |
| /** | |
| * Get historical states. | |
| */ | |
| public getHistory(limit: number = 10): PsycheState[] { | |
| return this.history.slice(0, limit); | |
| } | |
| /** | |
| * Get type description for UI display. | |
| */ | |
| public static getTypeDescription(type: EnneagramType): { name: string; core: string; fear: string; desire: string } { | |
| const descriptions: Record<EnneagramType, { name: string; core: string; fear: string; desire: string }> = { | |
| [EnneagramType.REFORMER]: { | |
| name: "The Reformer", | |
| core: "Principled, purposeful, self-controlled", | |
| fear: "Being corrupt or defective", | |
| desire: "To be good, balanced, with integrity" | |
| }, | |
| [EnneagramType.HELPER]: { | |
| name: "The Helper", | |
| core: "Generous, demonstrative, people-pleasing", | |
| fear: "Being unwanted or unloved", | |
| desire: "To be loved and appreciated" | |
| }, | |
| [EnneagramType.ACHIEVER]: { | |
| name: "The Achiever", | |
| core: "Adaptable, excelling, driven", | |
| fear: "Being worthless or without value", | |
| desire: "To be valuable and worthwhile" | |
| }, | |
| [EnneagramType.INDIVIDUALIST]: { | |
| name: "The Individualist", | |
| core: "Expressive, dramatic, self-absorbed", | |
| fear: "Having no identity or significance", | |
| desire: "To be unique and authentic" | |
| }, | |
| [EnneagramType.INVESTIGATOR]: { | |
| name: "The Investigator", | |
| core: "Perceptive, innovative, secretive", | |
| fear: "Being useless or incompetent", | |
| desire: "To be capable and competent" | |
| }, | |
| [EnneagramType.LOYALIST]: { | |
| name: "The Loyalist", | |
| core: "Engaging, responsible, anxious", | |
| fear: "Being without support or guidance", | |
| desire: "To have security and support" | |
| }, | |
| [EnneagramType.ENTHUSIAST]: { | |
| name: "The Enthusiast", | |
| core: "Spontaneous, versatile, scattered", | |
| fear: "Being deprived or trapped", | |
| desire: "To be satisfied and content" | |
| }, | |
| [EnneagramType.CHALLENGER]: { | |
| name: "The Challenger", | |
| core: "Self-confident, decisive, confrontational", | |
| fear: "Being controlled or harmed", | |
| desire: "To protect self and be in control" | |
| }, | |
| [EnneagramType.PEACEMAKER]: { | |
| name: "The Peacemaker", | |
| core: "Receptive, reassuring, complacent", | |
| fear: "Loss of connection or fragmentation", | |
| desire: "To have inner stability and peace" | |
| } | |
| }; | |
| return descriptions[type]; | |
| } | |
| } | |
| // Singleton export | |
| export const enneagramCore = new EnneagramCore(); | |