totemheart 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +212 -0
- package/package.json +62 -0
- package/src/Totemheart.js +1363 -0
- package/src/behavior/AttentionFocus.js +69 -0
- package/src/behavior/ExpressionDebt.js +48 -0
- package/src/behavior/ExpressionDirectives.js +95 -0
- package/src/behavior/ExpressiveSuppression.js +45 -0
- package/src/behavior/IdleProcessing.js +64 -0
- package/src/behavior/LinguisticModulation.js +62 -0
- package/src/behavior/LogitBiasBuilder.js +31 -0
- package/src/behavior/RuminationChain.js +93 -0
- package/src/behavior/StyleMimicry.js +72 -0
- package/src/cognition/AmygdalaHijack.js +40 -0
- package/src/cognition/AppraisalAgreement.js +39 -0
- package/src/cognition/BayesianExpectation.js +59 -0
- package/src/cognition/CognitiveDissonance.js +71 -0
- package/src/cognition/ControllabilityEstimate.js +56 -0
- package/src/cognition/DecisionFatigue.js +44 -0
- package/src/cognition/DefenseMechanisms.js +32 -0
- package/src/cognition/EmotionalOntology.js +134 -0
- package/src/cognition/FuzzyNormativeCheck.js +60 -0
- package/src/cognition/Intuition.js +71 -0
- package/src/cognition/LifeEventCatalog.js +98 -0
- package/src/cognition/LoadScheduler.js +41 -0
- package/src/cognition/LogicEngine.js +69 -0
- package/src/cognition/NoveltyDetector.js +59 -0
- package/src/cognition/Reappraisal.js +34 -0
- package/src/cognition/RefractoryPeriod.js +38 -0
- package/src/cognition/RemConsolidation.js +79 -0
- package/src/cognition/SarcasmDetector.js +38 -0
- package/src/cognition/SemanticSimilarity.js +75 -0
- package/src/cognition/Sensitization.js +45 -0
- package/src/cognition/SituationalContext.js +48 -0
- package/src/cognition/TopicSatiation.js +54 -0
- package/src/cognition/VisualProsody.js +48 -0
- package/src/core/AffectEMA.js +26 -0
- package/src/core/CoreBeliefs.js +58 -0
- package/src/core/DecayEngine.js +56 -0
- package/src/core/EmotionSpace.js +146 -0
- package/src/core/HebbianPlasticity.js +59 -0
- package/src/core/HedonicAdaptation.js +62 -0
- package/src/core/Homeostasis.js +119 -0
- package/src/core/MicroEmotions.js +25 -0
- package/src/core/MoodTracker.js +53 -0
- package/src/core/Personality.js +84 -0
- package/src/core/PipelineResilience.js +24 -0
- package/src/core/TriggerSentinel.js +38 -0
- package/src/core/WornPathCache.js +57 -0
- package/src/economics/AnchoringBias.js +38 -0
- package/src/economics/ClassicalConditioning.js +59 -0
- package/src/economics/LossAversion.js +40 -0
- package/src/embodiment/HardwareInteroception.js +26 -0
- package/src/embodiment/InteroceptiveSignals.js +132 -0
- package/src/embodiment/SensoryOverload.js +45 -0
- package/src/index.js +14 -0
- package/src/integration/ContextAdapter.js +247 -0
- package/src/neurochemistry/ArousalKalmanFilter.js +39 -0
- package/src/neurochemistry/CircadianRhythm.js +29 -0
- package/src/neurochemistry/CortisolEngine.js +54 -0
- package/src/neurochemistry/DopaminergicEngine.js +42 -0
- package/src/providers/FunctionProvider.js +26 -0
- package/src/providers/HeuristicProvider.js +156 -0
- package/src/providers/LanguageProvider.js +13 -0
- package/src/providers/OllamaProvider.js +63 -0
- package/src/providers/TransformersProvider.js +154 -0
- package/src/social/Attachment.js +81 -0
- package/src/social/BystanderEffect.js +17 -0
- package/src/social/ChronicContagion.js +48 -0
- package/src/social/CounterfactualComparison.js +34 -0
- package/src/social/EgoConfidence.js +34 -0
- package/src/social/EgoProjection.js +20 -0
- package/src/social/EmotionalContagion.js +52 -0
- package/src/social/EpisodicMemory.js +266 -0
- package/src/social/FairnessMonitor.js +34 -0
- package/src/social/ForgettingCurve.js +28 -0
- package/src/social/GratitudeEngine.js +32 -0
- package/src/social/GuiltEngine.js +43 -0
- package/src/social/MonteCarloToM.js +49 -0
- package/src/social/ReputationEngine.js +45 -0
- package/src/social/SelfModel.js +50 -0
- package/src/social/StatusEnvy.js +34 -0
- package/src/social/TheoryOfMind.js +53 -0
- package/src/social/TribalCategorization.js +24 -0
- package/src/social/UncannyValleyDetector.js +59 -0
- package/src/text/EmotionalCorpus.js +70 -0
- package/src/text/EmotionalTextGenerator.js +56 -0
- package/src/text/ExplainabilityEngine.js +30 -0
- package/src/text/TemplateComposer.js +114 -0
- package/src/version.js +1 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Immutable axioms about the world, the AI itself, and the user.
|
|
3
|
+
* Acts as the base filter CognitiveDissonance checks incoming input against.
|
|
4
|
+
* Once added, an entry cannot be edited or removed — only new beliefs can be added.
|
|
5
|
+
*/
|
|
6
|
+
export class CoreBeliefs {
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
|
|
10
|
+
this.beliefs = new Map()
|
|
11
|
+
this.defenseCounts = new Map() // topic -> how many turns this belief has actually been defended, sunk-cost fallacy raw material
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
add( topic, statement, polarity = 1 ) {
|
|
16
|
+
|
|
17
|
+
if ( this.beliefs.has( topic ) ) throw new Error( `Core belief "${topic}" already exists and is immutable` )
|
|
18
|
+
this.beliefs.set( topic, { topic, statement, polarity } )
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get( topic ) {
|
|
23
|
+
|
|
24
|
+
return this.beliefs.get( topic ) || null
|
|
25
|
+
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getAll() {
|
|
29
|
+
|
|
30
|
+
return [ ...this.beliefs.values() ]
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Sunk-cost fallacy raw material: every turn LogicEngine's search actually
|
|
36
|
+
* picked "disagree" to defend this topic, that's real invested effort — not
|
|
37
|
+
* a citation of a specific escalation-of-commitment study, just tracking
|
|
38
|
+
* the plain fact of "how many times has this actually been defended".
|
|
39
|
+
*/
|
|
40
|
+
recordDefense( topic ) {
|
|
41
|
+
|
|
42
|
+
this.defenseCounts.set( topic, ( this.defenseCounts.get( topic ) ?? 0 ) + 1 )
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getInvestment( topic ) {
|
|
47
|
+
|
|
48
|
+
return this.defenseCounts.get( topic ) ?? 0
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getTotalInvestment() {
|
|
53
|
+
|
|
54
|
+
return [ ...this.defenseCounts.values() ].reduce( ( a, b ) => a + b, 0 )
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** E(t) = baseline + (E0 - baseline) * e^(-λt) */
|
|
2
|
+
export function decayTowards( current, baseline, lambda, dt ) {
|
|
3
|
+
|
|
4
|
+
return baseline + ( current - baseline ) * Math.exp( -lambda * dt )
|
|
5
|
+
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Cubic pull toward baseline: dD/dt = -k·D³, where D = current - baseline is
|
|
10
|
+
* the distance from baseline (not the raw emotion value — Totemheart decays
|
|
11
|
+
* toward a personality-informed mood baseline, never toward absolute zero,
|
|
12
|
+
* so the cubic term has to act on the offset, not the raw reading). Solved
|
|
13
|
+
* with one explicit Euler step, clamped so a big dt/k can't overshoot past
|
|
14
|
+
* baseline and oscillate: a small offset barely moves (D³ is tiny), an
|
|
15
|
+
* extreme one gets pulled back hard — the same qualitative "spring that gets
|
|
16
|
+
* much stiffer far from rest" shape a cubic restoring force has, own tuning,
|
|
17
|
+
* no citation.
|
|
18
|
+
*/
|
|
19
|
+
export function cubicDecayTowards( current, baseline, k, dt ) {
|
|
20
|
+
|
|
21
|
+
const offset = current - baseline
|
|
22
|
+
const rawStep = k * offset ** 3 * dt
|
|
23
|
+
const boundedStep = Math.sign( rawStep ) === Math.sign( offset ) ? Math.min( Math.abs( rawStep ), Math.abs( offset ) ) * Math.sign( rawStep ) : rawStep
|
|
24
|
+
|
|
25
|
+
return baseline + ( offset - boundedStep )
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Pulls EmotionSpace's vector back toward the current mood baseline over
|
|
31
|
+
* time, at a rate set by Personality (neuroticism = slower recovery from
|
|
32
|
+
* negative states). Blends the original exponential pull (gentle, constant
|
|
33
|
+
* relative rate — good for ordinary fluctuation) with a cubic term (own
|
|
34
|
+
* design) that only bites once the offset from baseline is large, so a
|
|
35
|
+
* state stuck at an extreme (mania/depression-shaped runaway) gets pulled
|
|
36
|
+
* back far more aggressively than the plain exponential alone would.
|
|
37
|
+
*/
|
|
38
|
+
export class DecayEngine {
|
|
39
|
+
|
|
40
|
+
apply( emotionSpace, mood, personality, dt, { cubicK = 0.15 } = {} ) {
|
|
41
|
+
|
|
42
|
+
const { valence, arousal } = emotionSpace.vector
|
|
43
|
+
const lambdaValence = personality.getEmotionalRecoveryRate( valence - mood.valence )
|
|
44
|
+
const lambdaArousal = personality.getEmotionalRecoveryRate( arousal - mood.arousal )
|
|
45
|
+
|
|
46
|
+
const expValence = decayTowards( valence, mood.valence, lambdaValence, dt )
|
|
47
|
+
const expArousal = decayTowards( arousal, mood.arousal, lambdaArousal, dt )
|
|
48
|
+
|
|
49
|
+
emotionSpace.setVector(
|
|
50
|
+
cubicDecayTowards( expValence, mood.valence, cubicK, dt ),
|
|
51
|
+
cubicDecayTowards( expArousal, mood.arousal, cubicK, dt ),
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Approximate position of each named emotion on the valence/arousal/dominance
|
|
3
|
+
* space. The 2-axis circumplex is Russell, J. A. (1980), "A circumplex model
|
|
4
|
+
* of affect", Journal of Personality and Social Psychology, 39(6), 1161-1178.
|
|
5
|
+
* The third axis (dominance — felt sense of control/power vs. submissiveness)
|
|
6
|
+
* extends this toward the PAD framework (Pleasure-Arousal-Dominance,
|
|
7
|
+
* Mehrabian & Russell's line of work) — direction of the axis taken from
|
|
8
|
+
* that tradition, the actual placement of these 20 labels on all three axes
|
|
9
|
+
* is our own, not a published mapping — see CALIBRATION.md.
|
|
10
|
+
*/
|
|
11
|
+
export const EMOTION_COORDS = {
|
|
12
|
+
joy : { valence: 0.8, arousal: 0.5, dominance: 0.4 },
|
|
13
|
+
sadness : { valence: -0.6, arousal: -0.4, dominance: -0.4 },
|
|
14
|
+
fear : { valence: -0.7, arousal: 0.7, dominance: -0.6 },
|
|
15
|
+
anger : { valence: -0.6, arousal: 0.8, dominance: 0.5 },
|
|
16
|
+
surprise : { valence: 0.2, arousal: 0.8, dominance: 0.1 },
|
|
17
|
+
disgust : { valence: -0.7, arousal: 0.2, dominance: 0.2 },
|
|
18
|
+
love : { valence: 0.9, arousal: 0.3, dominance: 0.2 },
|
|
19
|
+
shame : { valence: -0.6, arousal: -0.2, dominance: -0.8 },
|
|
20
|
+
pride : { valence: 0.7, arousal: 0.4, dominance: 0.7 },
|
|
21
|
+
guilt : { valence: -0.5, arousal: -0.1, dominance: -0.5 },
|
|
22
|
+
hope : { valence: 0.6, arousal: 0.3, dominance: 0.3 },
|
|
23
|
+
frustration : { valence: -0.4, arousal: 0.6, dominance: -0.2 },
|
|
24
|
+
despair : { valence: -0.8, arousal: -0.5, dominance: -0.7 },
|
|
25
|
+
compassion : { valence: 0.5, arousal: 0.1, dominance: 0.3 },
|
|
26
|
+
gratitude : { valence: 0.7, arousal: 0.2, dominance: 0.1 },
|
|
27
|
+
trust : { valence: 0.5, arousal: -0.1, dominance: 0.3 },
|
|
28
|
+
remorse : { valence: -0.5, arousal: -0.3, dominance: -0.5 },
|
|
29
|
+
envy : { valence: -0.4, arousal: 0.3, dominance: -0.3 },
|
|
30
|
+
jealousy : { valence: -0.5, arousal: 0.4, dominance: -0.2 },
|
|
31
|
+
nostalgia : { valence: 0.1, arousal: -0.3, dominance: -0.1 },
|
|
32
|
+
neutral : { valence: 0, arousal: 0, dominance: 0 },
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** "Survival" emotions AmygdalaHijack watches for. */
|
|
36
|
+
export const SURVIVAL_EMOTIONS = [ 'fear', 'anger' ]
|
|
37
|
+
|
|
38
|
+
function clamp( v, min = -1, max = 1 ) {
|
|
39
|
+
|
|
40
|
+
return Math.max( min, Math.min( max, v ) )
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function distance( a, b ) {
|
|
45
|
+
|
|
46
|
+
return Math.hypot( a.valence - b.valence, a.arousal - b.arousal, ( a.dominance ?? 0 ) - ( b.dominance ?? 0 ) )
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Blend of the top-K nearest named emotions to a point, inverse-distance
|
|
52
|
+
* weighted. `neuroticism` (0..1, optional) nudges the query point deeper
|
|
53
|
+
* into whichever quadrant it's already in before matching — the same raw
|
|
54
|
+
* state reads as milder for a low-neuroticism personality and as more
|
|
55
|
+
* intense for a high-neuroticism one. The *direction* of this effect is
|
|
56
|
+
* grounded in Costa & McCrae (1992) on neuroticism and emotional
|
|
57
|
+
* reactivity; the specific 0.15 magnitude is our own tuning, not a
|
|
58
|
+
* measured coefficient — see CALIBRATION.md. Dominance is left unbiased —
|
|
59
|
+
* neuroticism research speaks to emotional reactivity/valence, not felt
|
|
60
|
+
* control, so there's no basis for nudging that axis the same way.
|
|
61
|
+
*/
|
|
62
|
+
export function classify( point, topK = 3, neuroticism = 0 ) {
|
|
63
|
+
|
|
64
|
+
const biasedPoint = neuroticism > 0
|
|
65
|
+
? {
|
|
66
|
+
valence : point.valence + Math.sign( point.valence ) * neuroticism * 0.15,
|
|
67
|
+
arousal : point.arousal + Math.sign( point.arousal ) * neuroticism * 0.1,
|
|
68
|
+
dominance : point.dominance ?? 0,
|
|
69
|
+
}
|
|
70
|
+
: point
|
|
71
|
+
|
|
72
|
+
const scored = Object.entries( EMOTION_COORDS ).map( ( [ name, coords ] ) => {
|
|
73
|
+
|
|
74
|
+
const d = distance( biasedPoint, coords )
|
|
75
|
+
return { name, weight: 1 / ( 0.05 + d ) }
|
|
76
|
+
|
|
77
|
+
} )
|
|
78
|
+
|
|
79
|
+
scored.sort( ( a, b ) => b.weight - a.weight )
|
|
80
|
+
const top = scored.slice( 0, topK )
|
|
81
|
+
const total = top.reduce( ( sum, e ) => sum + e.weight, 0 )
|
|
82
|
+
|
|
83
|
+
return top.reduce( ( blend, e ) => {
|
|
84
|
+
|
|
85
|
+
blend[ e.name ] = e.weight / total
|
|
86
|
+
return blend
|
|
87
|
+
|
|
88
|
+
}, {} )
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Central emotional engine: a {valence, arousal, dominance} vector (PAD)
|
|
94
|
+
* plus a derived blend of named emotions. `applySpike` squashes valence and
|
|
95
|
+
* dominance through tanh (a smooth saturating nonlinearity, not a hard clip)
|
|
96
|
+
* — the real technique behind item #10 of the appraisal-to-affect mapping:
|
|
97
|
+
* accumulated cognitive evaluations pass through a sigmoid-family activation
|
|
98
|
+
* rather than being summed and truncated. `setVector`/`getState` keep a hard
|
|
99
|
+
* clamp since those are used for direct state assignment (restore, tests),
|
|
100
|
+
* where exact values matter more than a smooth curve.
|
|
101
|
+
*/
|
|
102
|
+
export class EmotionSpace {
|
|
103
|
+
|
|
104
|
+
constructor() {
|
|
105
|
+
|
|
106
|
+
this.vector = { valence: 0, arousal: 0, dominance: 0 }
|
|
107
|
+
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
setVector( valence, arousal, dominance = this.vector.dominance ?? 0 ) {
|
|
111
|
+
|
|
112
|
+
this.vector = { valence: clamp( valence ), arousal: clamp( arousal ), dominance: clamp( dominance ) }
|
|
113
|
+
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Additively apply a weighted spike (from MicroEmotions) to the current vector. */
|
|
117
|
+
applySpike( { valence = 0, arousal = 0, dominance = 0, weight = 1 } ) {
|
|
118
|
+
|
|
119
|
+
this.vector = {
|
|
120
|
+
valence : Math.tanh( this.vector.valence + valence * weight ),
|
|
121
|
+
arousal : clamp( this.vector.arousal + arousal * weight ),
|
|
122
|
+
dominance : Math.tanh( ( this.vector.dominance ?? 0 ) + dominance * weight ),
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getBlend( topK = 3, neuroticism = 0 ) {
|
|
128
|
+
|
|
129
|
+
return classify( this.vector, topK, neuroticism )
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
getDominantEmotion( neuroticism = 0 ) {
|
|
134
|
+
|
|
135
|
+
const blend = this.getBlend( 1, neuroticism )
|
|
136
|
+
return Object.keys( blend )[ 0 ]
|
|
137
|
+
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
getState( neuroticism = 0 ) {
|
|
141
|
+
|
|
142
|
+
return { vector: { ...this.vector }, blend: this.getBlend( 3, neuroticism ) }
|
|
143
|
+
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Real Hebbian update (Hebb, 1949 — "neurons that fire together wire
|
|
3
|
+
* together", the classic shape, not a citation of any specific numeric
|
|
4
|
+
* result): W(t+1) = W(t) + η(Mi·Mj) − γW(t), applied to co-activation of
|
|
5
|
+
* this project's own mechanism trigger flags instead of biological neurons.
|
|
6
|
+
* If SarcasmDetector and DefenseMechanisms keep firing on the same turns
|
|
7
|
+
* across a conversation, their association weight grows; if the
|
|
8
|
+
* conversation moves on and they stop co-firing, γ decays it back down —
|
|
9
|
+
* own tuning of η/γ, not a reproduction of a published parameter set.
|
|
10
|
+
*/
|
|
11
|
+
export class HebbianPlasticity {
|
|
12
|
+
|
|
13
|
+
constructor( { eta = 0.1, gamma = 0.02 } = {} ) {
|
|
14
|
+
|
|
15
|
+
this.eta = eta
|
|
16
|
+
this.gamma = gamma
|
|
17
|
+
this.weights = new Map() // "a|b" (sorted pair key) -> association weight
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#key( a, b ) {
|
|
22
|
+
|
|
23
|
+
return [ a, b ].sort().join( '|' )
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** `active` — array of mechanism names that fired (trigger==1) this turn. */
|
|
28
|
+
update( active ) {
|
|
29
|
+
|
|
30
|
+
for ( const [ key, w ] of this.weights ) this.weights.set( key, Math.max( 0, w - this.gamma * w ) )
|
|
31
|
+
|
|
32
|
+
for ( let i = 0; i < active.length; i++ ) {
|
|
33
|
+
|
|
34
|
+
for ( let j = i + 1; j < active.length; j++ ) {
|
|
35
|
+
|
|
36
|
+
const key = this.#key( active[ i ], active[ j ] )
|
|
37
|
+
const prior = this.weights.get( key ) ?? 0
|
|
38
|
+
this.weights.set( key, prior + this.eta * ( 1 - prior ) ) // co-activation term Mi·Mj = 1·1 here, bounded toward 1
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Extra decay pass with no co-activation term — REM's "synaptic pruning" of associations that went stale during a long silence. */
|
|
47
|
+
decayOnly( extraGamma ) {
|
|
48
|
+
|
|
49
|
+
for ( const [ key, w ] of this.weights ) this.weights.set( key, Math.max( 0, w - extraGamma * w ) )
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getAssociation( a, b ) {
|
|
54
|
+
|
|
55
|
+
return this.weights.get( this.#key( a, b ) ) ?? 0
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const COOLDOWN_MS = 1000 * 60 * 30 // 30 minutes without the stimulus resets habituation
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Diminishing impact for a repeated stimulus. Saying "te quiero" 50 times in
|
|
5
|
+
* a row should stop producing the same emotional spike each time.
|
|
6
|
+
*/
|
|
7
|
+
export class HedonicAdaptation {
|
|
8
|
+
|
|
9
|
+
constructor( { referenceAlpha = 0.95 } = {} ) {
|
|
10
|
+
|
|
11
|
+
this.seen = new Map()
|
|
12
|
+
this.referenceAlpha = referenceAlpha // long-run EMA persistence, distinct from the per-fingerprint discount below
|
|
13
|
+
this.referencePoint = 0 // "hedonic treadmill": a long-run EMA of received desirability, own design, no citation
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Sustained input (good or bad) moves what counts as "still positive" — a real reference-point shift, not a fixed 0 baseline. */
|
|
18
|
+
observeReferencePoint( desirability ) {
|
|
19
|
+
|
|
20
|
+
this.referencePoint = this.referenceAlpha * this.referencePoint + ( 1 - this.referenceAlpha ) * desirability
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getReferencePointShift() {
|
|
25
|
+
|
|
26
|
+
return this.referencePoint
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static fingerprintOf( text, emotionLabel = '' ) {
|
|
31
|
+
|
|
32
|
+
const normalized = ( text || '' ).toLowerCase().trim().slice( 0, 60 )
|
|
33
|
+
return `${emotionLabel}::${normalized}`
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getMultiplier( fingerprint, personality, now = Date.now() ) {
|
|
38
|
+
|
|
39
|
+
const entry = this.seen.get( fingerprint )
|
|
40
|
+
if ( !entry ) return 1
|
|
41
|
+
if ( now - entry.lastSeen > COOLDOWN_MS ) return 1
|
|
42
|
+
|
|
43
|
+
const k = personality.getHedonicAdaptationRate()
|
|
44
|
+
return 1 / ( 1 + k * entry.count )
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
record( fingerprint, now = Date.now() ) {
|
|
49
|
+
|
|
50
|
+
const entry = this.seen.get( fingerprint )
|
|
51
|
+
if ( !entry || now - entry.lastSeen > COOLDOWN_MS ) {
|
|
52
|
+
|
|
53
|
+
this.seen.set( fingerprint, { count: 1, lastSeen: now } )
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
entry.count += 1
|
|
58
|
+
entry.lastSeen = now
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
function clamp01( v ) {
|
|
2
|
+
|
|
3
|
+
return Math.max( 0, Math.min( 1, v ) )
|
|
4
|
+
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const BASE_DECAY = {
|
|
8
|
+
stamina : 0.01,
|
|
9
|
+
socialization : 0.008,
|
|
10
|
+
curiosity : 0.006,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Real PID controller per need: error = target(1, "fully satisfied") − current
|
|
15
|
+
* level. Proportional term reacts to how deprived the need is right now,
|
|
16
|
+
* Integral accumulates how long it's been neglected (a need that's been low
|
|
17
|
+
* for a while generates more urgency than one that just dipped), Derivative
|
|
18
|
+
* dampens the output if the need is already recovering fast (being satisfied
|
|
19
|
+
* quickly right now), so urgency doesn't overshoot right as it's being fixed.
|
|
20
|
+
* Gains (Kp/Ki/Kd) are engineering defaults, not tuned against any reference
|
|
21
|
+
* controller — see CALIBRATION.md.
|
|
22
|
+
*/
|
|
23
|
+
class PID {
|
|
24
|
+
|
|
25
|
+
constructor( { kp = 1, ki = 0.05, kd = 0.3, outputLimit = 1 } = {} ) {
|
|
26
|
+
|
|
27
|
+
this.kp = kp
|
|
28
|
+
this.ki = ki
|
|
29
|
+
this.kd = kd
|
|
30
|
+
this.outputLimit = outputLimit // saturation bound for anti-windup
|
|
31
|
+
this.integral = 0
|
|
32
|
+
this.prevError = 0
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Standard anti-windup by clamping: a plain PID's integral term keeps
|
|
38
|
+
* accumulating while the output is saturated, so once the error finally
|
|
39
|
+
* starts improving the controller keeps overreacting for a while purely
|
|
40
|
+
* because of that stale accumulated integral — the real cause of the
|
|
41
|
+
* "won't come back down" symptom in a saturating PID. Fix: freeze the
|
|
42
|
+
* integral (don't add this step's contribution) whenever the raw output
|
|
43
|
+
* would already be past ±outputLimit, exactly the standard back-calculation-
|
|
44
|
+
* free clamping technique from control theory, not a citation of a
|
|
45
|
+
* specific paper.
|
|
46
|
+
*/
|
|
47
|
+
step( current, target, dt ) {
|
|
48
|
+
|
|
49
|
+
const error = target - current
|
|
50
|
+
const derivative = dt > 0 ? ( error - this.prevError ) / dt : 0
|
|
51
|
+
this.prevError = error
|
|
52
|
+
|
|
53
|
+
const unclampedIntegral = this.integral + error * dt
|
|
54
|
+
const provisionalOutput = this.kp * error + this.ki * unclampedIntegral + this.kd * derivative
|
|
55
|
+
const saturated = Math.abs( provisionalOutput ) >= this.outputLimit
|
|
56
|
+
|
|
57
|
+
this.integral = saturated ? this.integral : unclampedIntegral
|
|
58
|
+
|
|
59
|
+
const output = this.kp * error + this.ki * this.integral + this.kd * derivative
|
|
60
|
+
return Math.max( -this.outputLimit, Math.min( this.outputLimit, output ) )
|
|
61
|
+
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Needs that decay over ticks (a clock, not wall time — the host decides the
|
|
68
|
+
* cadence). Each need's PID controller produces a graded "urgency" (drive
|
|
69
|
+
* intensity), not just a binary below/above-threshold alert.
|
|
70
|
+
*/
|
|
71
|
+
export class Homeostasis {
|
|
72
|
+
|
|
73
|
+
constructor( { alertThreshold = 0.2 } = {} ) {
|
|
74
|
+
|
|
75
|
+
this.needs = { stamina: 1, socialization: 1, curiosity: 1 }
|
|
76
|
+
this.alertThreshold = alertThreshold
|
|
77
|
+
this.controllers = { stamina: new PID(), socialization: new PID(), curiosity: new PID() }
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
tick( dt, personality ) {
|
|
82
|
+
|
|
83
|
+
this.needs.stamina = clamp01( this.needs.stamina - BASE_DECAY.stamina * dt )
|
|
84
|
+
this.needs.socialization = clamp01( this.needs.socialization - personality.getSocialDecayRate() * dt )
|
|
85
|
+
this.needs.curiosity = clamp01( this.needs.curiosity - BASE_DECAY.curiosity * dt )
|
|
86
|
+
|
|
87
|
+
for ( const need of Object.keys( this.needs ) ) this.controllers[ need ].step( this.needs[ need ], 1, dt )
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
satisfy( need, amount ) {
|
|
92
|
+
|
|
93
|
+
if ( !( need in this.needs ) ) return
|
|
94
|
+
this.needs[ need ] = clamp01( this.needs[ need ] + amount )
|
|
95
|
+
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** PID-driven urgency for a need — how insistently it should be "felt" right now. */
|
|
99
|
+
getUrgency( need ) {
|
|
100
|
+
|
|
101
|
+
return this.controllers[ need ] ? Math.max( 0, this.controllers[ need ].prevError * this.controllers[ need ].kp + this.controllers[ need ].integral * this.controllers[ need ].ki ) : 0
|
|
102
|
+
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getAlerts() {
|
|
106
|
+
|
|
107
|
+
return Object.entries( this.needs )
|
|
108
|
+
.filter( ( [ , value ] ) => value < this.alertThreshold )
|
|
109
|
+
.map( ( [ need, value ] ) => ( { need, value, urgency: this.getUrgency( need ) } ) )
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getState() {
|
|
114
|
+
|
|
115
|
+
return { ...this.needs }
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transient emotion spikes computed from an appraisal of the current input.
|
|
3
|
+
* Short-lived by nature — DecayEngine pulls them back toward the mood
|
|
4
|
+
* baseline over subsequent ticks, they are not meant to persist on their own.
|
|
5
|
+
*/
|
|
6
|
+
export class MicroEmotions {
|
|
7
|
+
|
|
8
|
+
generate( appraisal, dissonanceScore = 0, hedonicMultiplier = 1 ) {
|
|
9
|
+
|
|
10
|
+
const desirability = appraisal?.desirability ?? 0
|
|
11
|
+
const moralWeight = appraisal?.moralWeight ?? 0
|
|
12
|
+
|
|
13
|
+
const deltaValence = desirability * hedonicMultiplier - dissonanceScore * 0.4
|
|
14
|
+
const deltaArousal = ( Math.abs( desirability ) * 0.6 + moralWeight * 0.3 + dissonanceScore * 0.5 ) * hedonicMultiplier
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
valence : deltaValence,
|
|
18
|
+
arousal : deltaArousal,
|
|
19
|
+
weight : 1,
|
|
20
|
+
agency : appraisal?.agency ?? 'other',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { classify } from './EmotionSpace.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Rolling average of recent microemotions. A single "irá" spike is a
|
|
5
|
+
* microemotion; being irritable for the next several turns is the mood
|
|
6
|
+
* this module produces from a run of similar spikes.
|
|
7
|
+
*/
|
|
8
|
+
export class MoodTracker {
|
|
9
|
+
|
|
10
|
+
constructor( { windowSize = 10 } = {} ) {
|
|
11
|
+
|
|
12
|
+
this.windowSize = windowSize
|
|
13
|
+
this.window = []
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Defaults missing fields to 0, same convention EmotionSpace.applySpike already
|
|
19
|
+
* uses — a spike object that only sets `arousal` (e.g. a startle with no valence
|
|
20
|
+
* opinion) must not silently poison every future mood average with NaN via
|
|
21
|
+
* `undefined + number`. Real bug found running the full demo pipeline after this
|
|
22
|
+
* round's new spikes; fixed at the source instead of patching every call site.
|
|
23
|
+
*/
|
|
24
|
+
push( spike ) {
|
|
25
|
+
|
|
26
|
+
this.window.push( { valence: spike.valence ?? 0, arousal: spike.arousal ?? 0 } )
|
|
27
|
+
if ( this.window.length > this.windowSize ) this.window.shift()
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getMood() {
|
|
32
|
+
|
|
33
|
+
if ( !this.window.length ) return { valence: 0, arousal: 0 }
|
|
34
|
+
const sum = this.window.reduce( ( acc, e ) => ( {
|
|
35
|
+
valence : acc.valence + e.valence,
|
|
36
|
+
arousal : acc.arousal + e.arousal,
|
|
37
|
+
} ), { valence: 0, arousal: 0 } )
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
valence : sum.valence / this.window.length,
|
|
41
|
+
arousal : sum.arousal / this.window.length,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getMoodLabel( neuroticism = 0 ) {
|
|
47
|
+
|
|
48
|
+
const blend = classify( this.getMood(), 1, neuroticism )
|
|
49
|
+
return Object.keys( blend )[ 0 ]
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const TRAITS = [ 'openness', 'conscientiousness', 'extraversion', 'agreeableness', 'neuroticism' ]
|
|
2
|
+
|
|
3
|
+
function clamp01( v ) {
|
|
4
|
+
|
|
5
|
+
return Math.max( 0, Math.min( 1, v ) )
|
|
6
|
+
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* OCEAN trait matrix. Static per-instance values (0..1) that act as
|
|
11
|
+
* multipliers consumed by every other module (decay rate, hedonic
|
|
12
|
+
* adaptation speed, dissonance tolerance, defense-mechanism weights...).
|
|
13
|
+
*/
|
|
14
|
+
export class Personality {
|
|
15
|
+
|
|
16
|
+
constructor( traits = {} ) {
|
|
17
|
+
|
|
18
|
+
this.traits = {}
|
|
19
|
+
for ( const trait of TRAITS ) this.traits[ trait ] = clamp01( traits[ trait ] ?? 0.5 )
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get( trait ) {
|
|
24
|
+
|
|
25
|
+
return this.traits[ trait ] ?? 0.5
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Lambda for exponential decay back to baseline. Higher neuroticism = negative
|
|
30
|
+
* emotions linger longer (smaller lambda); positive emotions fade slightly faster. */
|
|
31
|
+
getEmotionalRecoveryRate( valenceSign ) {
|
|
32
|
+
|
|
33
|
+
const base = 0.25
|
|
34
|
+
if ( valenceSign < 0 ) return base * ( 1 - 0.6 * this.traits.neuroticism )
|
|
35
|
+
return base * ( 1 + 0.2 * this.traits.neuroticism )
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** How fast the "socialization" need drains — extraverts need interaction more. */
|
|
40
|
+
getSocialDecayRate() {
|
|
41
|
+
|
|
42
|
+
return 0.015 * ( 1 + this.traits.extraversion )
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** How fast repeated stimuli lose impact. High openness = slower habituation. */
|
|
47
|
+
getHedonicAdaptationRate() {
|
|
48
|
+
|
|
49
|
+
return 0.35 * ( 1 - 0.5 * this.traits.openness )
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Score above which a belief conflict registers as cognitive stress.
|
|
54
|
+
* More conscientious minds tolerate less inconsistency. */
|
|
55
|
+
getDissonanceThreshold() {
|
|
56
|
+
|
|
57
|
+
return 0.6 - 0.3 * this.traits.conscientiousness
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Relative weights for the three defense mechanisms. */
|
|
62
|
+
getDefenseWeights() {
|
|
63
|
+
|
|
64
|
+
const projection = 0.2 + 0.6 * ( 1 - this.traits.agreeableness )
|
|
65
|
+
const evasion = 0.2 + 0.6 * this.traits.neuroticism
|
|
66
|
+
const sarcasm = 0.2 + 0.6 * this.traits.openness
|
|
67
|
+
const total = projection + evasion + sarcasm
|
|
68
|
+
return {
|
|
69
|
+
projection : projection / total,
|
|
70
|
+
evasion : evasion / total,
|
|
71
|
+
sarcasm : sarcasm / total,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static random() {
|
|
77
|
+
|
|
78
|
+
const traits = {}
|
|
79
|
+
for ( const trait of TRAITS ) traits[ trait ] = Math.random()
|
|
80
|
+
return new Personality( traits )
|
|
81
|
+
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Self-healing" reframed honestly for a single-process library: Totemheart
|
|
3
|
+
* has no distributed workers to lose and reconstruct topologically — that
|
|
4
|
+
* framing doesn't apply here. What DOES apply, and is real: any one optional
|
|
5
|
+
* pipeline stage (a custom LanguageProvider throwing, a malformed input
|
|
6
|
+
* tripping a module) should never take down the whole turn. Each risky stage
|
|
7
|
+
* is wrapped so a failure degrades to a safe fallback and gets logged,
|
|
8
|
+
* instead of propagating and losing the turn's state entirely.
|
|
9
|
+
*/
|
|
10
|
+
export async function safeStep( explainability, stepName, fn, fallbackValue ) {
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
|
|
14
|
+
return await fn()
|
|
15
|
+
|
|
16
|
+
}
|
|
17
|
+
catch ( error ) {
|
|
18
|
+
|
|
19
|
+
explainability?.logDecision( 'degraded_step', `${stepName} failed and fell back to a safe default: ${error?.message ?? error}` )
|
|
20
|
+
return fallbackValue
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
}
|