@aleph-ai/tinyaleph 1.0.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.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +278 -0
  3. package/backends/cryptographic/index.js +196 -0
  4. package/backends/index.js +15 -0
  5. package/backends/interface.js +89 -0
  6. package/backends/scientific/index.js +272 -0
  7. package/backends/semantic/index.js +527 -0
  8. package/backends/semantic/surface.js +393 -0
  9. package/backends/semantic/two-layer.js +375 -0
  10. package/core/fano.js +127 -0
  11. package/core/hilbert.js +564 -0
  12. package/core/hypercomplex.js +141 -0
  13. package/core/index.js +133 -0
  14. package/core/llm.js +132 -0
  15. package/core/prime.js +184 -0
  16. package/core/resonance.js +695 -0
  17. package/core/rformer-tf.js +1086 -0
  18. package/core/rformer.js +806 -0
  19. package/core/sieve.js +350 -0
  20. package/data.json +8163 -0
  21. package/docs/EXAMPLES_PLAN.md +293 -0
  22. package/docs/README.md +159 -0
  23. package/docs/design/ALEPH_CHAT_ARCHITECTURE.md +499 -0
  24. package/docs/guide/01-quickstart.md +298 -0
  25. package/docs/guide/02-semantic-computing.md +409 -0
  26. package/docs/guide/03-cryptographic.md +420 -0
  27. package/docs/guide/04-scientific.md +494 -0
  28. package/docs/guide/05-llm-integration.md +568 -0
  29. package/docs/guide/06-advanced.md +996 -0
  30. package/docs/guide/README.md +188 -0
  31. package/docs/reference/01-core.md +695 -0
  32. package/docs/reference/02-physics.md +601 -0
  33. package/docs/reference/03-backends.md +892 -0
  34. package/docs/reference/04-engine.md +632 -0
  35. package/docs/reference/README.md +252 -0
  36. package/docs/theory/01-prime-semantics.md +327 -0
  37. package/docs/theory/02-hypercomplex-algebra.md +421 -0
  38. package/docs/theory/03-phase-synchronization.md +364 -0
  39. package/docs/theory/04-entropy-reasoning.md +348 -0
  40. package/docs/theory/05-non-commutativity.md +402 -0
  41. package/docs/theory/06-two-layer-meaning.md +414 -0
  42. package/docs/theory/07-resonant-field-interface.md +419 -0
  43. package/docs/theory/08-semantic-sieve.md +520 -0
  44. package/docs/theory/09-temporal-emergence.md +298 -0
  45. package/docs/theory/10-quaternionic-memory.md +415 -0
  46. package/docs/theory/README.md +162 -0
  47. package/engine/aleph.js +418 -0
  48. package/engine/index.js +7 -0
  49. package/index.js +23 -0
  50. package/modular.js +254 -0
  51. package/package.json +99 -0
  52. package/physics/collapse.js +95 -0
  53. package/physics/entropy.js +88 -0
  54. package/physics/index.js +65 -0
  55. package/physics/kuramoto.js +91 -0
  56. package/physics/lyapunov.js +80 -0
  57. package/physics/oscillator.js +95 -0
  58. package/types/index.d.ts +575 -0
@@ -0,0 +1,393 @@
1
+ /**
2
+ * Surface Layer - Word Selection with Bias
3
+ *
4
+ * This layer sits above the prime-based meaning core and handles:
5
+ * - Mapping primes → words (with style/register preferences)
6
+ * - Biasing word selection based on context
7
+ * - Managing multiple vocabulary registers (formal, casual, technical, etc.)
8
+ */
9
+
10
+ /**
11
+ * A Surface represents a specific vocabulary/style mapping
12
+ * Multiple words can map to the same prime signature with different biases
13
+ */
14
+ class Surface {
15
+ constructor(config = {}) {
16
+ this.name = config.name || 'default';
17
+
18
+ // Prime → [{ word, bias, context }]
19
+ // Multiple words can share the same primes but with different biases
20
+ this.primeToWords = new Map();
21
+
22
+ // Word → primes (reverse lookup)
23
+ this.wordToPrimes = new Map();
24
+
25
+ // Default biases for this surface
26
+ this.defaultBias = config.defaultBias || 1.0;
27
+
28
+ // Context stack affects word selection
29
+ this.contextStack = [];
30
+
31
+ // Register vocabulary
32
+ if (config.vocabulary) {
33
+ this.loadVocabulary(config.vocabulary);
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Load vocabulary with optional bias information
39
+ * vocabulary: { word: { primes: [...], bias?: number, contexts?: [...] } }
40
+ * or simple format: { word: [...primes] }
41
+ */
42
+ loadVocabulary(vocabulary) {
43
+ for (const [word, value] of Object.entries(vocabulary)) {
44
+ const entry = Array.isArray(value)
45
+ ? { primes: value, bias: this.defaultBias }
46
+ : { primes: value.primes, bias: value.bias || this.defaultBias, contexts: value.contexts || [] };
47
+
48
+ this.registerWord(word, entry.primes, entry.bias, entry.contexts);
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Register a word with its prime signature and bias
54
+ */
55
+ registerWord(word, primes, bias = 1.0, contexts = []) {
56
+ const signature = this.primeSignature(primes);
57
+
58
+ if (!this.primeToWords.has(signature)) {
59
+ this.primeToWords.set(signature, []);
60
+ }
61
+
62
+ this.primeToWords.get(signature).push({
63
+ word,
64
+ primes,
65
+ bias,
66
+ contexts
67
+ });
68
+
69
+ this.wordToPrimes.set(word, primes);
70
+ }
71
+
72
+ /**
73
+ * Create canonical signature for prime lookup
74
+ */
75
+ primeSignature(primes) {
76
+ return [...primes].sort((a, b) => a - b).join(',');
77
+ }
78
+
79
+ /**
80
+ * Push a context that affects word selection
81
+ */
82
+ pushContext(context) {
83
+ this.contextStack.push(context);
84
+ }
85
+
86
+ popContext() {
87
+ return this.contextStack.pop();
88
+ }
89
+
90
+ clearContext() {
91
+ this.contextStack = [];
92
+ }
93
+
94
+ /**
95
+ * Select best word for given primes considering biases and context
96
+ */
97
+ selectWord(primes, options = {}) {
98
+ const signature = this.primeSignature(primes);
99
+ const candidates = this.primeToWords.get(signature);
100
+
101
+ if (!candidates || candidates.length === 0) {
102
+ // No exact match - find closest
103
+ return this.selectClosestWord(primes, options);
104
+ }
105
+
106
+ if (candidates.length === 1) {
107
+ return candidates[0].word;
108
+ }
109
+
110
+ // Score candidates by bias and context match
111
+ const scored = candidates.map(c => ({
112
+ ...c,
113
+ score: this.scoreCandidate(c, options)
114
+ }));
115
+
116
+ scored.sort((a, b) => b.score - a.score);
117
+
118
+ // Option: deterministic (take best) or probabilistic (weighted random)
119
+ if (options.deterministic) {
120
+ return scored[0].word;
121
+ }
122
+
123
+ return this.weightedSelect(scored);
124
+ }
125
+
126
+ /**
127
+ * Score a candidate word based on bias and context
128
+ */
129
+ scoreCandidate(candidate, options = {}) {
130
+ let score = candidate.bias;
131
+
132
+ // Boost if candidate's contexts match current context
133
+ const currentContexts = [...this.contextStack, ...(options.contexts || [])];
134
+ for (const ctx of candidate.contexts) {
135
+ if (currentContexts.includes(ctx)) {
136
+ score *= 1.5; // Context match boost
137
+ }
138
+ }
139
+
140
+ // Apply external bias if provided
141
+ if (options.wordBiases && options.wordBiases[candidate.word]) {
142
+ score *= options.wordBiases[candidate.word];
143
+ }
144
+
145
+ // Penalize if marked as avoid
146
+ if (options.avoid && options.avoid.includes(candidate.word)) {
147
+ score *= 0.1;
148
+ }
149
+
150
+ // Boost if marked as prefer
151
+ if (options.prefer && options.prefer.includes(candidate.word)) {
152
+ score *= 2.0;
153
+ }
154
+
155
+ return score;
156
+ }
157
+
158
+ /**
159
+ * Weighted random selection based on scores
160
+ */
161
+ weightedSelect(scored) {
162
+ const totalScore = scored.reduce((sum, c) => sum + c.score, 0);
163
+ let random = Math.random() * totalScore;
164
+
165
+ for (const candidate of scored) {
166
+ random -= candidate.score;
167
+ if (random <= 0) {
168
+ return candidate.word;
169
+ }
170
+ }
171
+
172
+ return scored[0].word;
173
+ }
174
+
175
+ /**
176
+ * Find closest word when no exact prime match
177
+ */
178
+ selectClosestWord(targetPrimes, options = {}) {
179
+ const targetSet = new Set(targetPrimes);
180
+ let bestMatch = null;
181
+ let bestScore = -1;
182
+
183
+ for (const [signature, candidates] of this.primeToWords) {
184
+ for (const candidate of candidates) {
185
+ const candidateSet = new Set(candidate.primes);
186
+
187
+ // Jaccard similarity
188
+ const intersection = [...targetSet].filter(p => candidateSet.has(p)).length;
189
+ const union = new Set([...targetSet, ...candidateSet]).size;
190
+ const similarity = intersection / union;
191
+
192
+ const score = similarity * candidate.bias;
193
+
194
+ if (score > bestScore) {
195
+ bestScore = score;
196
+ bestMatch = candidate.word;
197
+ }
198
+ }
199
+ }
200
+
201
+ return bestMatch || `[${targetPrimes.join(',')}]`; // Fallback to raw primes
202
+ }
203
+
204
+ /**
205
+ * Encode word to primes
206
+ */
207
+ encode(word) {
208
+ return this.wordToPrimes.get(word.toLowerCase());
209
+ }
210
+
211
+ /**
212
+ * Decode primes to word
213
+ */
214
+ decode(primes, options = {}) {
215
+ return this.selectWord(primes, options);
216
+ }
217
+ }
218
+
219
+ /**
220
+ * SurfaceManager handles multiple vocabulary surfaces and switching between them
221
+ */
222
+ class SurfaceManager {
223
+ constructor() {
224
+ this.surfaces = new Map();
225
+ this.activeSurface = null;
226
+ this.contextStack = [];
227
+ }
228
+
229
+ /**
230
+ * Register a named surface
231
+ */
232
+ register(name, surface) {
233
+ this.surfaces.set(name, surface);
234
+ if (!this.activeSurface) {
235
+ this.activeSurface = name;
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Create and register a surface from config
241
+ */
242
+ create(name, config) {
243
+ const surface = new Surface({ name, ...config });
244
+ this.register(name, surface);
245
+ return surface;
246
+ }
247
+
248
+ /**
249
+ * Switch active surface
250
+ */
251
+ use(name) {
252
+ if (!this.surfaces.has(name)) {
253
+ throw new Error(`Unknown surface: ${name}`);
254
+ }
255
+ this.activeSurface = name;
256
+ return this.surfaces.get(name);
257
+ }
258
+
259
+ /**
260
+ * Get current surface
261
+ */
262
+ current() {
263
+ return this.surfaces.get(this.activeSurface);
264
+ }
265
+
266
+ /**
267
+ * Decode primes using current surface
268
+ */
269
+ decode(primes, options = {}) {
270
+ return this.current().selectWord(primes, {
271
+ ...options,
272
+ contexts: this.contextStack
273
+ });
274
+ }
275
+
276
+ /**
277
+ * Encode word using current surface
278
+ */
279
+ encode(word) {
280
+ return this.current().encode(word);
281
+ }
282
+
283
+ /**
284
+ * Push global context affecting all surfaces
285
+ */
286
+ pushContext(context) {
287
+ this.contextStack.push(context);
288
+ }
289
+
290
+ popContext() {
291
+ return this.contextStack.pop();
292
+ }
293
+
294
+ /**
295
+ * Translate word from one surface to another
296
+ */
297
+ translate(word, fromSurface, toSurface, options = {}) {
298
+ const primes = this.surfaces.get(fromSurface).encode(word);
299
+ if (!primes) return word; // Unknown word
300
+ return this.surfaces.get(toSurface).decode(primes, options);
301
+ }
302
+ }
303
+
304
+ /**
305
+ * BiasEngine provides dynamic bias adjustments
306
+ */
307
+ class BiasEngine {
308
+ constructor() {
309
+ this.wordBiases = new Map(); // Persistent word biases
310
+ this.contextBiases = new Map(); // Context → bias multiplier
311
+ this.temporalDecay = 0.95; // How fast temporary biases decay
312
+ this.temporaryBiases = new Map(); // Temporary biases (decay over time)
313
+ }
314
+
315
+ /**
316
+ * Set persistent bias for a word
317
+ */
318
+ setBias(word, bias) {
319
+ this.wordBiases.set(word, bias);
320
+ }
321
+
322
+ /**
323
+ * Set temporary bias (will decay)
324
+ */
325
+ setTemporaryBias(word, bias) {
326
+ this.temporaryBiases.set(word, bias);
327
+ }
328
+
329
+ /**
330
+ * Set context-based bias
331
+ */
332
+ setContextBias(context, bias) {
333
+ this.contextBiases.set(context, bias);
334
+ }
335
+
336
+ /**
337
+ * Get total bias for a word given current context
338
+ */
339
+ getBias(word, contexts = []) {
340
+ let bias = 1.0;
341
+
342
+ // Apply persistent bias
343
+ if (this.wordBiases.has(word)) {
344
+ bias *= this.wordBiases.get(word);
345
+ }
346
+
347
+ // Apply temporary bias
348
+ if (this.temporaryBiases.has(word)) {
349
+ bias *= this.temporaryBiases.get(word);
350
+ }
351
+
352
+ // Apply context biases
353
+ for (const ctx of contexts) {
354
+ if (this.contextBiases.has(ctx)) {
355
+ bias *= this.contextBiases.get(ctx);
356
+ }
357
+ }
358
+
359
+ return bias;
360
+ }
361
+
362
+ /**
363
+ * Decay temporary biases (call periodically)
364
+ */
365
+ decay() {
366
+ for (const [word, bias] of this.temporaryBiases) {
367
+ const newBias = bias * this.temporalDecay;
368
+ if (Math.abs(newBias - 1.0) < 0.01) {
369
+ this.temporaryBiases.delete(word);
370
+ } else {
371
+ this.temporaryBiases.set(word, newBias);
372
+ }
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Boost words recently used (recency bias)
378
+ */
379
+ boostRecent(word, amount = 1.2) {
380
+ const current = this.temporaryBiases.get(word) || 1.0;
381
+ this.temporaryBiases.set(word, current * amount);
382
+ }
383
+
384
+ /**
385
+ * Suppress words recently used (variety bias)
386
+ */
387
+ suppressRecent(word, amount = 0.8) {
388
+ const current = this.temporaryBiases.get(word) || 1.0;
389
+ this.temporaryBiases.set(word, current * amount);
390
+ }
391
+ }
392
+
393
+ module.exports = { Surface, SurfaceManager, BiasEngine };