@aleph-ai/tinyaleph 1.2.1 → 1.3.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.
@@ -0,0 +1,570 @@
1
+ # Symbolic AI Module API Reference
2
+
3
+ The symbolic AI module provides symbol inference, cultural tagging, compound building, and golden ratio resonance measurement.
4
+
5
+ ## Symbol Database
6
+
7
+ ### SymbolDatabase
8
+
9
+ Singleton database containing 184+ emoji symbols with prime assignments.
10
+
11
+ ```javascript
12
+ const { symbolDatabase, SymbolDatabase } = require('./core');
13
+ ```
14
+
15
+ #### Symbol Structure
16
+
17
+ ```typescript
18
+ interface Symbol {
19
+ id: string; // Unique identifier (e.g., 'hero', 'fire')
20
+ unicode: string; // Emoji character (e.g., '🦸', '🔥')
21
+ category: SymbolCategory;
22
+ meaning: string; // Human-readable description
23
+ culturalTags: string[]; // ['greek', 'norse', 'universal', etc.]
24
+ prime: number; // Unique prime number
25
+ }
26
+
27
+ enum SymbolCategory {
28
+ PEOPLE_ARCHETYPES = 'PEOPLE_ARCHETYPES',
29
+ PLACES_LOCATIONS = 'PLACES_LOCATIONS',
30
+ OBJECTS_TOOLS = 'OBJECTS_TOOLS',
31
+ ABSTRACT_CONCEPTS = 'ABSTRACT_CONCEPTS',
32
+ NATURAL_ELEMENTS = 'NATURAL_ELEMENTS'
33
+ }
34
+ ```
35
+
36
+ #### Methods
37
+
38
+ ##### `getSymbol(id: string): Symbol | null`
39
+
40
+ Get a symbol by its ID.
41
+
42
+ ```javascript
43
+ const hero = symbolDatabase.getSymbol('hero');
44
+ // { id: 'hero', unicode: '🦸', prime: 1013, ... }
45
+ ```
46
+
47
+ ##### `getSymbolByPrime(prime: number): Symbol | null`
48
+
49
+ Get a symbol by its prime number.
50
+
51
+ ```javascript
52
+ const symbol = symbolDatabase.getSymbolByPrime(1013);
53
+ // { id: 'hero', ... }
54
+ ```
55
+
56
+ ##### `search(query: string): Symbol[]`
57
+
58
+ Search symbols by ID, meaning, or cultural tags.
59
+
60
+ ```javascript
61
+ const results = symbolDatabase.search('fire');
62
+ // [{ id: 'fire', ... }, { id: 'phoenix', ... }, ...]
63
+ ```
64
+
65
+ ##### `getByCategory(category: SymbolCategory): Symbol[]`
66
+
67
+ Get all symbols in a category.
68
+
69
+ ```javascript
70
+ const archetypes = symbolDatabase.getByCategory(SymbolCategory.PEOPLE_ARCHETYPES);
71
+ // 50 symbols
72
+ ```
73
+
74
+ ##### `getSymbolsByTag(tag: string): Symbol[]`
75
+
76
+ Get symbols with a specific cultural tag.
77
+
78
+ ```javascript
79
+ const greekSymbols = symbolDatabase.getSymbolsByTag('greek');
80
+ // [{ id: 'zeus', ... }, { id: 'athena', ... }, ...]
81
+ ```
82
+
83
+ ##### `encode(ids: string[]): bigint`
84
+
85
+ Encode symbol IDs to a prime signature (product of primes).
86
+
87
+ ```javascript
88
+ const signature = symbolDatabase.encode(['hero', 'journey', 'mountain']);
89
+ // 8035067n (bigint)
90
+ ```
91
+
92
+ ##### `decode(signature: bigint): Symbol[]`
93
+
94
+ Decode a prime signature back to symbols.
95
+
96
+ ```javascript
97
+ const symbols = symbolDatabase.decode(8035067n);
98
+ // [{ id: 'hero', ... }, { id: 'journey', ... }, { id: 'mountain', ... }]
99
+ ```
100
+
101
+ ##### `getAllSymbols(): Symbol[]`
102
+
103
+ Get all symbols in the database.
104
+
105
+ ```javascript
106
+ const all = symbolDatabase.getAllSymbols();
107
+ // 184 symbols
108
+ ```
109
+
110
+ ##### `getCategoryStats(): Map<SymbolCategory, number>`
111
+
112
+ Get count of symbols per category.
113
+
114
+ ```javascript
115
+ const stats = symbolDatabase.getCategoryStats();
116
+ // Map { 'PEOPLE_ARCHETYPES' => 50, 'PLACES_LOCATIONS' => 31, ... }
117
+ ```
118
+
119
+ ---
120
+
121
+ ## Semantic Inference
122
+
123
+ ### SemanticInference
124
+
125
+ Engine for inferring symbols from natural language text.
126
+
127
+ ```javascript
128
+ const { semanticInference, SemanticInference } = require('./core');
129
+ ```
130
+
131
+ #### InferenceResult Structure
132
+
133
+ ```typescript
134
+ interface InferenceResult {
135
+ symbol: Symbol;
136
+ method: 'direct' | 'regex' | 'semantic' | 'category';
137
+ confidence: number; // 0.0 - 1.0
138
+ resonanceBonus?: number;
139
+ attentionWeight?: number;
140
+ contextResonance?: number;
141
+ }
142
+ ```
143
+
144
+ #### Methods
145
+
146
+ ##### `inferSymbol(text: string): InferenceResult | null`
147
+
148
+ Infer a single symbol from text.
149
+
150
+ ```javascript
151
+ const result = semanticInference.inferSymbol('brave warrior');
152
+ // { symbol: { id: 'warrior', ... }, method: 'regex', confidence: 0.85 }
153
+ ```
154
+
155
+ Inference methods (in priority order):
156
+ 1. **direct**: Exact match on symbol ID
157
+ 2. **regex**: Pattern matching (100+ rules)
158
+ 3. **semantic**: Word overlap with meaning
159
+ 4. **category**: Fallback to category keywords
160
+
161
+ ##### `inferSymbols(entities: string[]): InferenceResult[]`
162
+
163
+ Infer symbols for multiple entities.
164
+
165
+ ```javascript
166
+ const results = semanticInference.inferSymbols(['hero', 'temple', 'fire']);
167
+ // [{ symbol: hero, ... }, { symbol: temple, ... }, { symbol: fire, ... }]
168
+ ```
169
+
170
+ ##### `extractEntities(text: string): string[]`
171
+
172
+ Extract potential entities from text.
173
+
174
+ ```javascript
175
+ const entities = semanticInference.extractEntities('The hero John went to the temple');
176
+ // ['hero', 'john', 'temple']
177
+ ```
178
+
179
+ ##### `extractAndInfer(text: string): InferenceResult[]`
180
+
181
+ Extract entities and infer symbols in one call.
182
+
183
+ ```javascript
184
+ const results = semanticInference.extractAndInfer('The hero fought the shadow');
185
+ // [{ entity: 'hero', symbol: ... }, { entity: 'shadow', symbol: ... }]
186
+ ```
187
+
188
+ ##### `inferWithResonance(text: string, options?): InferenceResult[]`
189
+
190
+ Infer symbols with resonance-based ranking using ResoFormer attention.
191
+
192
+ ```javascript
193
+ const results = semanticInference.inferWithResonance(
194
+ 'The hero fought the shadow in the temple of fire',
195
+ { topK: 5 }
196
+ );
197
+
198
+ for (const r of results) {
199
+ console.log(`${r.symbol.unicode} ${r.symbol.id}`);
200
+ console.log(` confidence: ${r.confidence}`);
201
+ console.log(` resonance bonus: ${r.resonanceBonus}`);
202
+ console.log(` attention weight: ${r.attentionWeight}`);
203
+ }
204
+ ```
205
+
206
+ Options:
207
+ - `topK?: number` - Maximum results to return (default: all)
208
+
209
+ ##### `inferMostResonant(text: string, contextSymbols: Symbol[]): InferenceResult`
210
+
211
+ Find the symbol that best resonates with an existing context.
212
+
213
+ ```javascript
214
+ const context = [
215
+ symbolDatabase.getSymbol('warrior'),
216
+ symbolDatabase.getSymbol('temple'),
217
+ symbolDatabase.getSymbol('fire')
218
+ ];
219
+
220
+ const best = semanticInference.inferMostResonant('weapon', context);
221
+ // { symbol: sword, contextResonance: 0.2993, ... }
222
+ ```
223
+
224
+ This uses ResoFormer's attention mechanism:
225
+ 1. Convert symbols to `SparsePrimeState`
226
+ 2. Calculate resonance scores using Jaccard + quaternion alignment
227
+ 3. Apply softmax attention
228
+ 4. Return highest-weighted candidate
229
+
230
+ ---
231
+
232
+ ## Compound Builder
233
+
234
+ ### CompoundBuilder
235
+
236
+ Build multi-symbol concepts through composition.
237
+
238
+ ```javascript
239
+ const { compoundBuilder, CompoundBuilder } = require('./core');
240
+ ```
241
+
242
+ #### CompoundSymbol Structure
243
+
244
+ ```typescript
245
+ interface CompoundSymbol {
246
+ id: string;
247
+ components: Symbol[];
248
+ unicode: string; // Concatenated emoji
249
+ meaning: string;
250
+ culturalTags: string[];
251
+ primeSignature: bigint; // Product of component primes
252
+ }
253
+ ```
254
+
255
+ #### SymbolSequence Structure
256
+
257
+ ```typescript
258
+ interface SymbolSequence {
259
+ id: string;
260
+ symbols: Symbol[];
261
+ type: 'narrative' | 'transformation' | 'progression';
262
+ description: string;
263
+ }
264
+ ```
265
+
266
+ #### Methods
267
+
268
+ ##### `createCompound(id, componentIds, meaning, culturalTags?): CompoundSymbol`
269
+
270
+ Create a new compound symbol.
271
+
272
+ ```javascript
273
+ const fireMage = compoundBuilder.createCompound(
274
+ 'fire_mage',
275
+ ['magician', 'fire', 'staff'],
276
+ 'Fire Mage - Wielder of flame magic',
277
+ ['fantasy', 'magic']
278
+ );
279
+ ```
280
+
281
+ ##### `getCompound(id: string): CompoundSymbol | null`
282
+
283
+ Get a pre-built compound.
284
+
285
+ ```javascript
286
+ const greekWarrior = compoundBuilder.getCompound('greek_warrior');
287
+ // { unicode: '⚔️⛩️🦉', meaning: 'Greek Warrior: Temple guardian...', ... }
288
+ ```
289
+
290
+ Pre-built compounds:
291
+ - `greek_warrior` - Temple guardian blessed by Athena
292
+ - `viking_warrior` - Norse warrior of the sea
293
+ - `samurai_warrior` - Japanese warrior path
294
+ - `philosopher_king` - Platonic ruler ideal
295
+ - `shadow_self` - Jungian shadow archetype
296
+
297
+ ##### `createSequence(id, symbolIds, type, description?): SymbolSequence`
298
+
299
+ Create an ordered symbol sequence.
300
+
301
+ ```javascript
302
+ const loveStory = compoundBuilder.createSequence(
303
+ 'love_story',
304
+ ['lover', 'love', 'conflict', 'unity'],
305
+ 'narrative',
306
+ 'Classic love story arc'
307
+ );
308
+ ```
309
+
310
+ ##### `getSequence(id: string): SymbolSequence | null`
311
+
312
+ Get a pre-built sequence.
313
+
314
+ ```javascript
315
+ const journey = compoundBuilder.getSequence('heros_journey');
316
+ // 🦸 → 🛤️ → 🌀 → 💥 → 🦋
317
+ ```
318
+
319
+ Pre-built sequences:
320
+ - `heros_journey` - Campbell's monomyth
321
+ - `alchemical_transformation` - Nigredo → Albedo → Rubedo
322
+
323
+ ##### `calculateCompoundResonance(compound: CompoundSymbol): number`
324
+
325
+ Calculate internal harmony of a compound.
326
+
327
+ ```javascript
328
+ const harmony = compoundBuilder.calculateCompoundResonance(fireMage);
329
+ // 0.234 (average pairwise resonance)
330
+ ```
331
+
332
+ ##### `mergeCompounds(compound1, compound2, newId, meaning): CompoundSymbol`
333
+
334
+ Merge two compounds into one.
335
+
336
+ ```javascript
337
+ const merged = compoundBuilder.mergeCompounds(
338
+ greekWarrior,
339
+ fireMage,
340
+ 'greek_fire_mage',
341
+ 'Greek fire warrior'
342
+ );
343
+ ```
344
+
345
+ ##### `createCulturalVariant(baseId, culture, additionalSymbols, meaning): CompoundSymbol`
346
+
347
+ Create a cultural variant of an existing compound.
348
+
349
+ ```javascript
350
+ const norseWarrior = compoundBuilder.createCulturalVariant(
351
+ 'greek_warrior',
352
+ 'norse',
353
+ ['ocean'],
354
+ 'Norse sea warrior'
355
+ );
356
+ ```
357
+
358
+ ---
359
+
360
+ ## Resonance Calculator
361
+
362
+ ### ResonanceCalculator
363
+
364
+ Calculate prime pair resonance based on golden ratio theory.
365
+
366
+ ```javascript
367
+ const { ResonanceCalculator, calculateResonance } = require('./core');
368
+ ```
369
+
370
+ #### Theory
371
+
372
+ Primes whose ratio approaches φ ≈ 1.618 have "natural harmony":
373
+
374
+ ```
375
+ R(p1, p2) = 1/ratio + φ_bonus
376
+ where φ_bonus = 0.3 if |ratio - φ| < 0.1
377
+ ```
378
+
379
+ Fibonacci pairs (3/5, 5/8, etc.) naturally achieve high resonance.
380
+
381
+ #### Methods
382
+
383
+ ##### `calculateResonance(p1: number, p2: number): number`
384
+
385
+ Calculate resonance between two primes.
386
+
387
+ ```javascript
388
+ calculateResonance(3, 5); // 0.9 (Fibonacci pair)
389
+ calculateResonance(7, 11); // 0.936 (close to φ)
390
+ calculateResonance(2, 17); // 0.118 (far from φ)
391
+ ```
392
+
393
+ ##### `findGoldenPairs(primes: number[]): GoldenPair[]`
394
+
395
+ Find all pairs with ratio close to φ.
396
+
397
+ ```javascript
398
+ const pairs = resonanceCalculator.findGoldenPairs([2, 3, 5, 7, 11, 13]);
399
+ // [{ p1: 3, p2: 5, ratio: 1.667, resonance: 0.9 }, ...]
400
+ ```
401
+
402
+ ```typescript
403
+ interface GoldenPair {
404
+ p1: number;
405
+ p2: number;
406
+ ratio: number;
407
+ resonance: number;
408
+ }
409
+ ```
410
+
411
+ ##### `resonanceSignature(primes: number[]): ResonanceSignature`
412
+
413
+ Calculate resonance statistics for a prime set.
414
+
415
+ ```javascript
416
+ const sig = resonanceCalculator.resonanceSignature([2, 3, 5, 7, 11]);
417
+ // { mean: 0.456, variance: 0.032, goldenCount: 2 }
418
+ ```
419
+
420
+ ```typescript
421
+ interface ResonanceSignature {
422
+ mean: number; // Average pairwise resonance
423
+ variance: number; // Variance of resonances
424
+ goldenCount: number; // Pairs within 0.1 of φ
425
+ }
426
+ ```
427
+
428
+ ##### `calculateMatrix(primes: number[]): number[][]`
429
+
430
+ Calculate full resonance matrix.
431
+
432
+ ```javascript
433
+ const matrix = resonanceCalculator.calculateMatrix([2, 3, 5, 7]);
434
+ // matrix[i][j] = resonance(primes[i], primes[j])
435
+ // Diagonal = 1.0
436
+ ```
437
+
438
+ ##### `findMostResonant(target: number, candidates: number[]): { prime, resonance }`
439
+
440
+ Find the candidate most resonant with target.
441
+
442
+ ```javascript
443
+ const best = resonanceCalculator.findMostResonant(7, [2, 3, 5, 11, 13]);
444
+ // { prime: 11, resonance: 0.936 }
445
+ ```
446
+
447
+ ##### `findClusters(primes: number[], threshold?: number): number[][]`
448
+
449
+ Find clusters of mutually resonant primes.
450
+
451
+ ```javascript
452
+ const clusters = resonanceCalculator.findClusters([2, 3, 5, 7, 11, 13], 0.5);
453
+ // [[3, 5], [7, 11], ...]
454
+ ```
455
+
456
+ ---
457
+
458
+ ## Convenience Functions
459
+
460
+ ### Exported from `core/index.js`
461
+
462
+ ```javascript
463
+ const {
464
+ // Symbol Database
465
+ getSymbol,
466
+ getSymbolByPrime,
467
+ symbolDatabase,
468
+ SymbolDatabase,
469
+ SymbolCategory,
470
+
471
+ // Semantic Inference
472
+ inferSymbol,
473
+ inferSymbols,
474
+ inferWithResonance,
475
+ inferMostResonant,
476
+ extractEntities,
477
+ extractAndInfer,
478
+ semanticInference,
479
+ SemanticInference,
480
+
481
+ // Compound Builder
482
+ createCompound,
483
+ getCompound,
484
+ createSequence,
485
+ getSequence,
486
+ compoundBuilder,
487
+ CompoundBuilder,
488
+ CompoundSymbol,
489
+ SymbolSequence,
490
+
491
+ // Resonance Calculator
492
+ calculateResonance,
493
+ findGoldenPairs,
494
+ resonanceSignature,
495
+ ResonanceCalculator,
496
+ GOLDEN_RATIO
497
+ } = require('./core');
498
+ ```
499
+
500
+ ---
501
+
502
+ ## Integration with ResoFormer
503
+
504
+ The symbolic AI uses ResoFormer primitives for resonance attention:
505
+
506
+ ```javascript
507
+ const { SparsePrimeState, resonantAttention, resonanceScore } = require('./core/rformer');
508
+
509
+ // Convert symbol to sparse prime state
510
+ function symbolToState(symbol) {
511
+ const state = new SparsePrimeState();
512
+ state.activations.set(symbol.prime, 1.0);
513
+
514
+ // Add cultural tag activations
515
+ for (const tag of symbol.culturalTags) {
516
+ const tagPrime = hashTag(tag);
517
+ state.activations.set(tagPrime, 0.3);
518
+ }
519
+
520
+ return state;
521
+ }
522
+
523
+ // Calculate resonance score
524
+ const score = resonanceScore(state1, state2);
525
+ // Uses: Jaccard(primes) + QuaternionAlign + PhaseCoherence
526
+
527
+ // Apply attention
528
+ const { output, weights } = resonantAttention(query, keys, values);
529
+ ```
530
+
531
+ ---
532
+
533
+ ## Examples
534
+
535
+ ### Full Pipeline Example
536
+
537
+ ```javascript
538
+ const {
539
+ inferWithResonance,
540
+ inferMostResonant,
541
+ createCompound,
542
+ calculateResonance,
543
+ getSymbol
544
+ } = require('./core');
545
+
546
+ // 1. Infer symbols from narrative
547
+ const text = 'The hero descended into the dark cave seeking the golden treasure';
548
+ const symbols = inferWithResonance(text);
549
+ console.log('Symbols:', symbols.map(s => s.symbol.unicode).join(' '));
550
+
551
+ // 2. Find complementary symbol
552
+ const context = symbols.map(s => s.symbol);
553
+ const weapon = inferMostResonant('weapon', context);
554
+ console.log('Suggested weapon:', weapon.symbol.unicode);
555
+
556
+ // 3. Build compound
557
+ const questGroup = createCompound(
558
+ 'quest_group',
559
+ [...symbols.map(s => s.symbol.id), weapon.symbol.id],
560
+ 'Complete quest party'
561
+ );
562
+
563
+ // 4. Measure harmony
564
+ const primes = questGroup.components.map(c => c.prime);
565
+ for (let i = 0; i < primes.length; i++) {
566
+ for (let j = i + 1; j < primes.length; j++) {
567
+ const r = calculateResonance(primes[i], primes[j]);
568
+ console.log(`${questGroup.components[i].id} ↔ ${questGroup.components[j].id}: ${r.toFixed(3)}`);
569
+ }
570
+ }