noormme 1.0.6 → 1.2.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/README.md +92 -63
- package/dist/cjs/agentic/ActionJournal.js +13 -9
- package/dist/cjs/agentic/CapabilityManager.d.ts +9 -4
- package/dist/cjs/agentic/CapabilityManager.js +113 -17
- package/dist/cjs/agentic/CognitiveRepository.js +19 -9
- package/dist/cjs/agentic/ContextBuffer.js +24 -12
- package/dist/cjs/agentic/Cortex.d.ts +8 -1
- package/dist/cjs/agentic/Cortex.js +30 -7
- package/dist/cjs/agentic/EpisodicMemory.js +7 -5
- package/dist/cjs/agentic/PersonaManager.js +16 -8
- package/dist/cjs/agentic/PolicyEnforcer.js +31 -12
- package/dist/cjs/agentic/ResourceMonitor.js +4 -4
- package/dist/cjs/agentic/SessionCompressor.js +22 -14
- package/dist/cjs/agentic/SessionManager.js +36 -18
- package/dist/cjs/agentic/VectorIndexer.js +22 -18
- package/dist/cjs/agentic/improvement/AblationEngine.js +22 -15
- package/dist/cjs/agentic/improvement/ActionRefiner.js +12 -10
- package/dist/cjs/agentic/improvement/ConflictResolver.js +5 -5
- package/dist/cjs/agentic/improvement/CortexJanitor.d.ts +1 -1
- package/dist/cjs/agentic/improvement/CortexJanitor.js +64 -27
- package/dist/cjs/agentic/improvement/CuriosityEngine.d.ts +2 -2
- package/dist/cjs/agentic/improvement/CuriosityEngine.js +68 -59
- package/dist/cjs/agentic/improvement/EvolutionRitual.d.ts +24 -0
- package/dist/cjs/agentic/improvement/EvolutionRitual.js +91 -0
- package/dist/cjs/agentic/improvement/EvolutionaryPilot.d.ts +5 -0
- package/dist/cjs/agentic/improvement/EvolutionaryPilot.js +80 -7
- package/dist/cjs/agentic/improvement/GoalArchitect.d.ts +2 -2
- package/dist/cjs/agentic/improvement/GoalArchitect.js +20 -18
- package/dist/cjs/agentic/improvement/GovernanceManager.d.ts +1 -1
- package/dist/cjs/agentic/improvement/GovernanceManager.js +85 -49
- package/dist/cjs/agentic/improvement/HiveLink.d.ts +9 -0
- package/dist/cjs/agentic/improvement/HiveLink.js +120 -26
- package/dist/cjs/agentic/improvement/KnowledgeDistiller.d.ts +2 -0
- package/dist/cjs/agentic/improvement/KnowledgeDistiller.js +101 -47
- package/dist/cjs/agentic/improvement/RecursiveReasoner.d.ts +7 -1
- package/dist/cjs/agentic/improvement/RecursiveReasoner.js +106 -22
- package/dist/cjs/agentic/improvement/ReflectionEngine.js +10 -8
- package/dist/cjs/agentic/improvement/RitualOrchestrator.js +34 -22
- package/dist/cjs/agentic/improvement/RuleEngine.js +22 -17
- package/dist/cjs/agentic/improvement/SelfEvolution.js +24 -18
- package/dist/cjs/agentic/improvement/SelfTestRegistry.js +18 -15
- package/dist/cjs/agentic/improvement/SkillSynthesizer.d.ts +48 -0
- package/dist/cjs/agentic/improvement/SkillSynthesizer.js +288 -0
- package/dist/cjs/agentic/improvement/SovereignMetrics.js +19 -17
- package/dist/cjs/agentic/improvement/StrategicPlanner.d.ts +1 -1
- package/dist/cjs/agentic/improvement/StrategicPlanner.js +129 -55
- package/dist/cjs/agentic/telemetry/CognitiveSynthesizer.js +26 -12
- package/dist/cjs/agentic/telemetry/EventHarvester.js +3 -2
- package/dist/cjs/agentic/telemetry/ResearchAlchemist.js +13 -4
- package/dist/cjs/cache/cache-manager.js +7 -4
- package/dist/cjs/cli/commands/analyze.js +5 -4
- package/dist/cjs/cli/commands/generate.js +81 -44
- package/dist/cjs/cli/commands/init.js +7 -3
- package/dist/cjs/cli/commands/inspect.js +139 -36
- package/dist/cjs/cli/commands/migrate.js +5 -4
- package/dist/cjs/cli/commands/optimize.js +4 -4
- package/dist/cjs/cli/commands/status.js +9 -7
- package/dist/cjs/cli/commands/watch.js +7 -6
- package/dist/cjs/cli/index.js +2 -2
- package/dist/cjs/cli/ui/spinner.d.ts +15 -0
- package/dist/cjs/cli/ui/spinner.js +76 -0
- package/dist/cjs/dialect/database-introspector.js +3 -1
- package/dist/cjs/dialect/postgresql/postgresql-driver.js +3 -1
- package/dist/cjs/dialect/postgresql/postgresql-features.js +18 -8
- package/dist/cjs/dialect/postgresql/postgresql-introspector.js +2 -2
- package/dist/cjs/dialect/sqlite/sqlite-auto-indexer.js +47 -33
- package/dist/cjs/dialect/sqlite/sqlite-auto-optimizer.js +8 -7
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +2 -2
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +15 -12
- package/dist/cjs/edge-runtime/edge-config.js +21 -19
- package/dist/cjs/errors/NoormError.js +22 -20
- package/dist/cjs/helpers/agent-schema.js +3 -0
- package/dist/cjs/helpers/postgresql.js +7 -4
- package/dist/cjs/helpers/schema-evolution.js +31 -6
- package/dist/cjs/index.d.ts +18 -16
- package/dist/cjs/logging/logger.js +8 -4
- package/dist/cjs/migration/data_migrator.js +12 -11
- package/dist/cjs/migration/database_migration_manager.js +17 -13
- package/dist/cjs/migration/schema_differ.js +22 -14
- package/dist/cjs/migration/schema_introspector.js +8 -8
- package/dist/cjs/migration/type_mapper.js +68 -67
- package/dist/cjs/noormme.js +54 -37
- package/dist/cjs/performance/index.js +5 -5
- package/dist/cjs/performance/query-optimizer.js +26 -21
- package/dist/cjs/performance/services/cache-service.js +26 -16
- package/dist/cjs/performance/services/connection-factory.js +28 -23
- package/dist/cjs/performance/services/metrics-collector.js +41 -36
- package/dist/cjs/performance/utils/query-parser.js +15 -16
- package/dist/cjs/relationships/relationship-engine.js +10 -8
- package/dist/cjs/repository/repository-factory.js +97 -38
- package/dist/cjs/schema/core/coordinators/schema-discovery.coordinator.js +1 -3
- package/dist/cjs/schema/core/discovery/relationship-discovery.js +16 -16
- package/dist/cjs/schema/core/discovery/table-metadata-discovery.js +9 -9
- package/dist/cjs/schema/core/discovery/view-discovery.js +5 -4
- package/dist/cjs/schema/core/factories/discovery-factory.js +4 -4
- package/dist/cjs/schema/core/utils/name-generator.js +14 -5
- package/dist/cjs/schema/core/utils/type-mapper.js +24 -24
- package/dist/cjs/schema/dialects/postgresql/postgresql-discovery.coordinator.js +8 -7
- package/dist/cjs/schema/dialects/sqlite/discovery/sqlite-constraint-discovery.js +17 -15
- package/dist/cjs/schema/dialects/sqlite/discovery/sqlite-index-discovery.js +8 -8
- package/dist/cjs/schema/dialects/sqlite/introspection/sqlite-schema-introspector.js +6 -11
- package/dist/cjs/schema/dialects/sqlite/sqlite-discovery.coordinator.js +14 -13
- package/dist/cjs/schema/test/basic-schema-test.js +11 -9
- package/dist/cjs/schema/test/dialect-capabilities.test.js +6 -6
- package/dist/cjs/schema/test/discovery-factory.test.js +2 -2
- package/dist/cjs/schema/test/error-handling.test.js +8 -6
- package/dist/cjs/schema/test/integration.test.js +24 -18
- package/dist/cjs/schema/test/schema-discovery-coordinator.test.js +9 -9
- package/dist/cjs/schema/test/simple-schema-test.js +9 -9
- package/dist/cjs/schema/test/sqlite-discovery-coordinator.test.js +64 -48
- package/dist/cjs/schema/test/test-runner.js +3 -3
- package/dist/cjs/sqlite-migration/index.d.ts +2 -2
- package/dist/cjs/sqlite-migration/sqlite-migration-manager.js +21 -17
- package/dist/cjs/sqlite-migration/sqlite-migration-provider.js +38 -34
- package/dist/cjs/testing/test-utils.js +36 -34
- package/dist/cjs/types/index.d.ts +61 -4
- package/dist/cjs/types/index.js +6 -3
- package/dist/cjs/types/type-generator.js +46 -42
- package/dist/cjs/util/safe-sql-helpers.js +1 -1
- package/dist/cjs/util/security-validator.js +20 -9
- package/dist/cjs/utils/errorHelpers.js +20 -10
- package/dist/cjs/watch/schema-watcher.js +22 -23
- package/dist/esm/agentic/ActionJournal.js +13 -9
- package/dist/esm/agentic/CapabilityManager.d.ts +9 -4
- package/dist/esm/agentic/CapabilityManager.js +113 -17
- package/dist/esm/agentic/CognitiveRepository.js +19 -9
- package/dist/esm/agentic/ContextBuffer.js +24 -12
- package/dist/esm/agentic/Cortex.d.ts +8 -1
- package/dist/esm/agentic/Cortex.js +30 -7
- package/dist/esm/agentic/EpisodicMemory.js +7 -5
- package/dist/esm/agentic/PersonaManager.js +16 -8
- package/dist/esm/agentic/PolicyEnforcer.js +31 -12
- package/dist/esm/agentic/ResourceMonitor.js +4 -4
- package/dist/esm/agentic/SessionCompressor.js +22 -14
- package/dist/esm/agentic/SessionManager.js +36 -18
- package/dist/esm/agentic/VectorIndexer.js +22 -18
- package/dist/esm/agentic/improvement/AblationEngine.js +22 -15
- package/dist/esm/agentic/improvement/ActionRefiner.js +12 -10
- package/dist/esm/agentic/improvement/ConflictResolver.js +5 -5
- package/dist/esm/agentic/improvement/CortexJanitor.d.ts +1 -1
- package/dist/esm/agentic/improvement/CortexJanitor.js +64 -27
- package/dist/esm/agentic/improvement/CuriosityEngine.d.ts +2 -2
- package/dist/esm/agentic/improvement/CuriosityEngine.js +68 -59
- package/dist/esm/agentic/improvement/EvolutionRitual.d.ts +24 -0
- package/dist/esm/agentic/improvement/EvolutionRitual.js +88 -0
- package/dist/esm/agentic/improvement/EvolutionaryPilot.d.ts +5 -0
- package/dist/esm/agentic/improvement/EvolutionaryPilot.js +80 -7
- package/dist/esm/agentic/improvement/GoalArchitect.d.ts +2 -2
- package/dist/esm/agentic/improvement/GoalArchitect.js +20 -18
- package/dist/esm/agentic/improvement/GovernanceManager.d.ts +1 -1
- package/dist/esm/agentic/improvement/GovernanceManager.js +85 -49
- package/dist/esm/agentic/improvement/HiveLink.d.ts +9 -0
- package/dist/esm/agentic/improvement/HiveLink.js +120 -26
- package/dist/esm/agentic/improvement/KnowledgeDistiller.d.ts +2 -0
- package/dist/esm/agentic/improvement/KnowledgeDistiller.js +101 -47
- package/dist/esm/agentic/improvement/RecursiveReasoner.d.ts +7 -1
- package/dist/esm/agentic/improvement/RecursiveReasoner.js +106 -22
- package/dist/esm/agentic/improvement/ReflectionEngine.js +10 -8
- package/dist/esm/agentic/improvement/RitualOrchestrator.js +34 -22
- package/dist/esm/agentic/improvement/RuleEngine.js +22 -17
- package/dist/esm/agentic/improvement/SelfEvolution.js +24 -18
- package/dist/esm/agentic/improvement/SelfTestRegistry.js +18 -15
- package/dist/esm/agentic/improvement/SkillSynthesizer.d.ts +48 -0
- package/dist/esm/agentic/improvement/SkillSynthesizer.js +285 -0
- package/dist/esm/agentic/improvement/SovereignMetrics.js +19 -17
- package/dist/esm/agentic/improvement/StrategicPlanner.d.ts +1 -1
- package/dist/esm/agentic/improvement/StrategicPlanner.js +129 -55
- package/dist/esm/agentic/telemetry/CognitiveSynthesizer.js +26 -12
- package/dist/esm/agentic/telemetry/EventHarvester.js +3 -2
- package/dist/esm/agentic/telemetry/ResearchAlchemist.js +13 -4
- package/dist/esm/cache/cache-manager.js +7 -4
- package/dist/esm/cli/commands/analyze.js +5 -4
- package/dist/esm/cli/commands/generate.js +82 -45
- package/dist/esm/cli/commands/init.js +8 -4
- package/dist/esm/cli/commands/inspect.js +140 -37
- package/dist/esm/cli/commands/migrate.js +5 -4
- package/dist/esm/cli/commands/optimize.js +4 -4
- package/dist/esm/cli/commands/status.js +9 -7
- package/dist/esm/cli/commands/watch.js +7 -6
- package/dist/esm/cli/index.js +2 -2
- package/dist/esm/cli/ui/spinner.d.ts +15 -0
- package/dist/esm/cli/ui/spinner.js +70 -0
- package/dist/esm/dialect/database-introspector.js +3 -1
- package/dist/esm/dialect/postgresql/postgresql-driver.js +3 -1
- package/dist/esm/dialect/postgresql/postgresql-features.js +18 -8
- package/dist/esm/dialect/postgresql/postgresql-introspector.js +2 -2
- package/dist/esm/dialect/sqlite/sqlite-auto-indexer.js +47 -33
- package/dist/esm/dialect/sqlite/sqlite-auto-optimizer.js +8 -7
- package/dist/esm/dialect/sqlite/sqlite-driver.js +2 -2
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +15 -12
- package/dist/esm/dynamic/dynamic.js +1 -1
- package/dist/esm/edge-runtime/edge-config.js +21 -19
- package/dist/esm/errors/NoormError.js +22 -20
- package/dist/esm/helpers/agent-schema.js +3 -0
- package/dist/esm/helpers/postgresql.js +7 -4
- package/dist/esm/helpers/schema-evolution.js +31 -6
- package/dist/esm/index.d.ts +18 -16
- package/dist/esm/index.js +2 -2
- package/dist/esm/logging/logger.js +8 -4
- package/dist/esm/migration/data_migrator.js +12 -11
- package/dist/esm/migration/database_migration_manager.js +18 -14
- package/dist/esm/migration/schema_differ.js +22 -14
- package/dist/esm/migration/schema_introspector.js +8 -8
- package/dist/esm/migration/type_mapper.js +68 -67
- package/dist/esm/noormme.js +54 -37
- package/dist/esm/performance/index.js +5 -5
- package/dist/esm/performance/query-optimizer.js +26 -21
- package/dist/esm/performance/services/cache-service.js +26 -16
- package/dist/esm/performance/services/connection-factory.js +28 -23
- package/dist/esm/performance/services/metrics-collector.js +41 -36
- package/dist/esm/performance/utils/query-parser.js +15 -16
- package/dist/esm/raw-builder/sql.js +1 -1
- package/dist/esm/relationships/relationship-engine.js +10 -8
- package/dist/esm/repository/repository-factory.js +98 -39
- package/dist/esm/schema/builders/alter-table-add-index-builder.js +1 -1
- package/dist/esm/schema/builders/create-index-builder.js +2 -2
- package/dist/esm/schema/core/coordinators/schema-discovery.coordinator.js +1 -3
- package/dist/esm/schema/core/discovery/relationship-discovery.js +16 -16
- package/dist/esm/schema/core/discovery/table-metadata-discovery.js +9 -9
- package/dist/esm/schema/core/discovery/view-discovery.js +5 -4
- package/dist/esm/schema/core/factories/discovery-factory.js +4 -4
- package/dist/esm/schema/core/utils/name-generator.js +14 -5
- package/dist/esm/schema/core/utils/type-mapper.js +24 -24
- package/dist/esm/schema/dialects/postgresql/postgresql-discovery.coordinator.js +8 -7
- package/dist/esm/schema/dialects/sqlite/discovery/sqlite-constraint-discovery.js +17 -15
- package/dist/esm/schema/dialects/sqlite/discovery/sqlite-index-discovery.js +8 -8
- package/dist/esm/schema/dialects/sqlite/introspection/sqlite-schema-introspector.js +6 -11
- package/dist/esm/schema/dialects/sqlite/sqlite-discovery.coordinator.js +14 -13
- package/dist/esm/schema/test/basic-schema-test.js +11 -9
- package/dist/esm/schema/test/dialect-capabilities.test.js +6 -6
- package/dist/esm/schema/test/discovery-factory.test.js +2 -2
- package/dist/esm/schema/test/error-handling.test.js +8 -6
- package/dist/esm/schema/test/integration.test.js +24 -18
- package/dist/esm/schema/test/schema-discovery-coordinator.test.js +9 -9
- package/dist/esm/schema/test/simple-schema-test.js +9 -9
- package/dist/esm/schema/test/sqlite-discovery-coordinator.test.js +64 -48
- package/dist/esm/schema/test/test-runner.js +3 -3
- package/dist/esm/sqlite-migration/index.d.ts +2 -2
- package/dist/esm/sqlite-migration/sqlite-migration-manager.js +21 -17
- package/dist/esm/sqlite-migration/sqlite-migration-provider.js +38 -34
- package/dist/esm/testing/test-utils.js +36 -34
- package/dist/esm/types/index.d.ts +61 -4
- package/dist/esm/types/index.js +6 -3
- package/dist/esm/types/type-generator.js +46 -42
- package/dist/esm/util/safe-sql-helpers.js +1 -1
- package/dist/esm/util/security-validator.js +20 -9
- package/dist/esm/utils/errorHelpers.js +21 -11
- package/dist/esm/watch/schema-watcher.js +22 -23
- package/package.json +40 -44
|
@@ -18,16 +18,18 @@ export class CognitiveRepository {
|
|
|
18
18
|
*/
|
|
19
19
|
async triggerRules(operation, data) {
|
|
20
20
|
// Check if rules table exists to avoid errors during initialization
|
|
21
|
-
const rulesTable = this.cortex.config.agentic?.rulesTable ||
|
|
21
|
+
const rulesTable = this.cortex.config.agentic?.rulesTable ||
|
|
22
|
+
this.cortex.config.rulesTable ||
|
|
23
|
+
'agent_rules';
|
|
22
24
|
const tables = await this.cortex.db.introspection.getTables();
|
|
23
|
-
if (!tables.some(t => t.name === rulesTable)) {
|
|
25
|
+
if (!tables.some((t) => t.name === rulesTable)) {
|
|
24
26
|
return data;
|
|
25
27
|
}
|
|
26
28
|
const result = await this.cortex.rules.evaluateRules(this.table.name, operation, data);
|
|
27
29
|
if (result.action === 'deny') {
|
|
28
30
|
throw new NoormError(`Operation ${operation} on ${this.table.name} denied: ${result.reason}`, {
|
|
29
31
|
operation: 'cognitive_rule_enforcement',
|
|
30
|
-
suggestion: 'Check agent rules or adjust policy'
|
|
32
|
+
suggestion: 'Check agent rules or adjust policy',
|
|
31
33
|
});
|
|
32
34
|
}
|
|
33
35
|
if (result.action === 'audit') {
|
|
@@ -35,7 +37,7 @@ export class CognitiveRepository {
|
|
|
35
37
|
}
|
|
36
38
|
if (result.action === 'mask' && result.ruleId) {
|
|
37
39
|
const rules = await this.cortex.rules.getActiveRules(this.table.name, operation);
|
|
38
|
-
const rule = rules.find(r => r.id === result.ruleId);
|
|
40
|
+
const rule = rules.find((r) => r.id === result.ruleId);
|
|
39
41
|
if (rule) {
|
|
40
42
|
return this.cortex.rules.applyMasking(data, rule);
|
|
41
43
|
}
|
|
@@ -55,10 +57,18 @@ export class CognitiveRepository {
|
|
|
55
57
|
return await this.repository.delete(id);
|
|
56
58
|
}
|
|
57
59
|
// Delegate other methods to the internal repository
|
|
58
|
-
async findById(id) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
async
|
|
60
|
+
async findById(id) {
|
|
61
|
+
return this.repository.findById(id);
|
|
62
|
+
}
|
|
63
|
+
async findAll() {
|
|
64
|
+
return this.repository.findAll();
|
|
65
|
+
}
|
|
66
|
+
async count() {
|
|
67
|
+
return this.repository.count();
|
|
68
|
+
}
|
|
69
|
+
async exists(id) {
|
|
70
|
+
return this.repository.exists(id);
|
|
71
|
+
}
|
|
62
72
|
// Dynamic method delegation via Proxy
|
|
63
73
|
static createProxy(repository, table, cortex) {
|
|
64
74
|
const cognitive = new CognitiveRepository(repository, table, cortex);
|
|
@@ -68,7 +78,7 @@ export class CognitiveRepository {
|
|
|
68
78
|
return cognitive[prop];
|
|
69
79
|
}
|
|
70
80
|
return Reflect.get(target, prop, receiver);
|
|
71
|
-
}
|
|
81
|
+
},
|
|
72
82
|
});
|
|
73
83
|
}
|
|
74
84
|
}
|
|
@@ -50,7 +50,7 @@ export class ContextBuffer {
|
|
|
50
50
|
msg: m,
|
|
51
51
|
index,
|
|
52
52
|
isAnchor: !!m.metadata?.anchor,
|
|
53
|
-
tokens: this.estimateTokens(m.content)
|
|
53
|
+
tokens: this.estimateTokens(m.content),
|
|
54
54
|
}));
|
|
55
55
|
const sortedForSelection = [...scored].sort((a, b) => {
|
|
56
56
|
if (a.isAnchor !== b.isAnchor)
|
|
@@ -58,15 +58,18 @@ export class ContextBuffer {
|
|
|
58
58
|
return b.index - a.index; // Recency tie-breaker
|
|
59
59
|
});
|
|
60
60
|
const selected = new Set();
|
|
61
|
-
let currentTokens = systemMessage
|
|
61
|
+
let currentTokens = systemMessage
|
|
62
|
+
? this.estimateTokens(systemMessage.content)
|
|
63
|
+
: 0;
|
|
62
64
|
for (const item of sortedForSelection) {
|
|
63
|
-
if (selected.size < effectiveLimit &&
|
|
65
|
+
if (selected.size < effectiveLimit &&
|
|
66
|
+
currentTokens + item.tokens <= tokenLimit) {
|
|
64
67
|
selected.add(item.msg);
|
|
65
68
|
currentTokens += item.tokens;
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
71
|
// Maintain temporal order
|
|
69
|
-
const result = this.messages.filter(m => m === systemMessage || selected.has(m));
|
|
72
|
+
const result = this.messages.filter((m) => m === systemMessage || selected.has(m));
|
|
70
73
|
return result;
|
|
71
74
|
}
|
|
72
75
|
/**
|
|
@@ -74,14 +77,14 @@ export class ContextBuffer {
|
|
|
74
77
|
*/
|
|
75
78
|
toPromptString(limit) {
|
|
76
79
|
return this.getWindow({ maxMessages: limit })
|
|
77
|
-
.map(m => `[${m.role.toUpperCase()}]: ${m.content}`)
|
|
80
|
+
.map((m) => `[${m.role.toUpperCase()}]: ${m.content}`)
|
|
78
81
|
.join('\n\n');
|
|
79
82
|
}
|
|
80
83
|
/**
|
|
81
84
|
* Helper to identify if the buffer is becoming too large.
|
|
82
85
|
*/
|
|
83
86
|
shouldSummarize(tokenThreshold) {
|
|
84
|
-
const threshold = tokenThreshold ||
|
|
87
|
+
const threshold = tokenThreshold || this.maxTokens * 0.8;
|
|
85
88
|
return this.getTotalTokens() > threshold;
|
|
86
89
|
}
|
|
87
90
|
/**
|
|
@@ -92,7 +95,7 @@ export class ContextBuffer {
|
|
|
92
95
|
messageCount: this.messages.length,
|
|
93
96
|
totalTokens: this.getTotalTokens(),
|
|
94
97
|
maxTokens: this.maxTokens,
|
|
95
|
-
maxMessages: this.maxMessages
|
|
98
|
+
maxMessages: this.maxMessages,
|
|
96
99
|
};
|
|
97
100
|
}
|
|
98
101
|
getTotalTokens() {
|
|
@@ -105,28 +108,37 @@ export class ContextBuffer {
|
|
|
105
108
|
const initialCount = this.messages.length;
|
|
106
109
|
// Importance Trimming: Prefer keeping 'anchor' messages or high-priority messages
|
|
107
110
|
// We sort a copy to determine which ones to keep, then reconstruct in temporal order
|
|
108
|
-
const otherMessages = systemMessage
|
|
111
|
+
const otherMessages = systemMessage
|
|
112
|
+
? this.messages.slice(1)
|
|
113
|
+
: this.messages;
|
|
109
114
|
// Heuristic for importance: anchors > assistant > user (user input is often redundant if reflected/anchored)
|
|
110
115
|
const sortedByImportance = [...otherMessages].sort((a, b) => {
|
|
111
116
|
const aIsAnchor = a.metadata?.anchor ? 1 : 0;
|
|
112
117
|
const bIsAnchor = b.metadata?.anchor ? 1 : 0;
|
|
113
118
|
if (aIsAnchor !== bIsAnchor)
|
|
114
119
|
return bIsAnchor - aIsAnchor;
|
|
115
|
-
const rolePriority = {
|
|
120
|
+
const rolePriority = {
|
|
121
|
+
system: 3,
|
|
122
|
+
assistant: 2,
|
|
123
|
+
user: 1,
|
|
124
|
+
action: 2,
|
|
125
|
+
};
|
|
116
126
|
return (rolePriority[b.role] || 0) - (rolePriority[a.role] || 0);
|
|
117
127
|
});
|
|
118
128
|
const toKeep = new Set(sortedByImportance.slice(0, this.maxMessages));
|
|
119
|
-
this.messages = this.messages.filter(m => m === systemMessage || toKeep.has(m));
|
|
129
|
+
this.messages = this.messages.filter((m) => m === systemMessage || toKeep.has(m));
|
|
120
130
|
console.log(`[ContextBuffer] Importance Trimming: ${initialCount} -> ${this.messages.length} messages. preserved anchors and assistant reasoning.`);
|
|
121
131
|
}
|
|
122
132
|
}
|
|
123
133
|
estimateTokens(content) {
|
|
124
134
|
if (!content)
|
|
125
135
|
return 0;
|
|
126
|
-
// More sophisticated heuristic:
|
|
136
|
+
// More sophisticated heuristic:
|
|
127
137
|
// - JSON/Code tends to have more tokens per character due to symbols.
|
|
128
138
|
// - Natural language is ~4 chars per token.
|
|
129
|
-
const isStructured = content.startsWith('{') ||
|
|
139
|
+
const isStructured = content.startsWith('{') ||
|
|
140
|
+
content.startsWith('[') ||
|
|
141
|
+
content.includes('```');
|
|
130
142
|
const ratio = isStructured ? 3 : 4;
|
|
131
143
|
return Math.ceil(content.length / ratio);
|
|
132
144
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Kysely } from '../kysely.js';
|
|
2
|
-
import type { AgenticConfig, AgentMessage, NOORMConfig } from '../types/index.js';
|
|
2
|
+
import type { AgenticConfig, AgentMessage, NOORMConfig, LLMProvider } from '../types/index.js';
|
|
3
3
|
import { SessionManager } from './SessionManager.js';
|
|
4
4
|
import { ContextBuffer } from './ContextBuffer.js';
|
|
5
5
|
import { VectorIndexer } from './VectorIndexer.js';
|
|
@@ -29,6 +29,8 @@ import { StrategicPlanner } from './improvement/StrategicPlanner.js';
|
|
|
29
29
|
import { AblationEngine } from './improvement/AblationEngine.js';
|
|
30
30
|
import { SelfTestRegistry } from './improvement/SelfTestRegistry.js';
|
|
31
31
|
import { TelemetryOrchestrator } from './telemetry/TelemetryOrchestrator.js';
|
|
32
|
+
import { SkillSynthesizer } from './improvement/SkillSynthesizer.js';
|
|
33
|
+
import { EvolutionRitual } from './improvement/EvolutionRitual.js';
|
|
32
34
|
/**
|
|
33
35
|
* Cortex is the unified facade for agentic operations.
|
|
34
36
|
* It coordinates sessions, memory, reflection, and knowledge.
|
|
@@ -65,6 +67,11 @@ export declare class Cortex {
|
|
|
65
67
|
ablation: AblationEngine;
|
|
66
68
|
tests: SelfTestRegistry;
|
|
67
69
|
telemetry: TelemetryOrchestrator;
|
|
70
|
+
skillSynthesizer: SkillSynthesizer;
|
|
71
|
+
evolutionRitual: EvolutionRitual;
|
|
72
|
+
llm: LLMProvider | null;
|
|
73
|
+
llmFast: LLMProvider | null;
|
|
74
|
+
llmPremium: LLMProvider | null;
|
|
68
75
|
agenticConfig: AgenticConfig;
|
|
69
76
|
constructor(db: Kysely<any>, config: NOORMConfig);
|
|
70
77
|
/**
|
|
@@ -28,6 +28,8 @@ import { StrategicPlanner } from './improvement/StrategicPlanner.js';
|
|
|
28
28
|
import { AblationEngine } from './improvement/AblationEngine.js';
|
|
29
29
|
import { SelfTestRegistry } from './improvement/SelfTestRegistry.js';
|
|
30
30
|
import { TelemetryOrchestrator } from './telemetry/TelemetryOrchestrator.js';
|
|
31
|
+
import { SkillSynthesizer } from './improvement/SkillSynthesizer.js';
|
|
32
|
+
import { EvolutionRitual } from './improvement/EvolutionRitual.js';
|
|
31
33
|
/**
|
|
32
34
|
* Cortex is the unified facade for agentic operations.
|
|
33
35
|
* It coordinates sessions, memory, reflection, and knowledge.
|
|
@@ -64,15 +66,25 @@ export class Cortex {
|
|
|
64
66
|
ablation;
|
|
65
67
|
tests;
|
|
66
68
|
telemetry;
|
|
69
|
+
skillSynthesizer;
|
|
70
|
+
evolutionRitual;
|
|
71
|
+
llm;
|
|
72
|
+
llmFast;
|
|
73
|
+
llmPremium;
|
|
67
74
|
agenticConfig;
|
|
68
75
|
constructor(db, config) {
|
|
69
76
|
this.db = db;
|
|
70
77
|
this.config = config;
|
|
71
78
|
const agenticConfig = config.agentic || {};
|
|
72
79
|
this.agenticConfig = agenticConfig;
|
|
80
|
+
this.llm = agenticConfig.llm || null;
|
|
81
|
+
this.llmFast = agenticConfig.llmFast || this.llm;
|
|
82
|
+
this.llmPremium = agenticConfig.llmPremium || this.llm;
|
|
73
83
|
this.telemetry = new TelemetryOrchestrator(db, agenticConfig);
|
|
74
84
|
this.sessions = new SessionManager(db, agenticConfig, this.telemetry);
|
|
75
|
-
this.buffer = new ContextBuffer({
|
|
85
|
+
this.buffer = new ContextBuffer({
|
|
86
|
+
maxMessages: agenticConfig.contextWindowSize,
|
|
87
|
+
});
|
|
76
88
|
this.vectors = agenticConfig.vectorConfig
|
|
77
89
|
? new VectorIndexer(db, agenticConfig.vectorConfig, agenticConfig.memoriesTable)
|
|
78
90
|
: null;
|
|
@@ -81,7 +93,7 @@ export class Cortex {
|
|
|
81
93
|
this.actions = new ActionJournal(db, agenticConfig, this.telemetry);
|
|
82
94
|
this.resources = new ResourceMonitor(db, agenticConfig);
|
|
83
95
|
this.episodes = new EpisodicMemory(db, agenticConfig);
|
|
84
|
-
this.capabilities = new CapabilityManager(db, agenticConfig);
|
|
96
|
+
this.capabilities = new CapabilityManager(db, this, agenticConfig);
|
|
85
97
|
this.policies = new PolicyEnforcer(db, agenticConfig);
|
|
86
98
|
this.metrics = new SovereignMetrics(db, agenticConfig);
|
|
87
99
|
this.evolution = new SelfEvolution(db, config);
|
|
@@ -101,6 +113,8 @@ export class Cortex {
|
|
|
101
113
|
this.strategy = new StrategicPlanner(db, this, agenticConfig);
|
|
102
114
|
this.ablation = new AblationEngine(db, this, agenticConfig);
|
|
103
115
|
this.tests = new SelfTestRegistry(db, this, agenticConfig);
|
|
116
|
+
this.skillSynthesizer = new SkillSynthesizer(db, this, agenticConfig);
|
|
117
|
+
this.evolutionRitual = new EvolutionRitual(db, this, agenticConfig);
|
|
104
118
|
}
|
|
105
119
|
/**
|
|
106
120
|
* The "Soul-Searching" Loop: A top-level orchestration of all self-improvement rituals.
|
|
@@ -123,16 +137,22 @@ export class Cortex {
|
|
|
123
137
|
await this.ablation.monitorAblationPerformance();
|
|
124
138
|
// 4. Mutation & Strategy
|
|
125
139
|
await this.strategy.mutateStrategy();
|
|
126
|
-
// 5.
|
|
140
|
+
// 5. High-Throughput Evolution Pulse
|
|
141
|
+
await this.evolutionRitual.execute();
|
|
142
|
+
// 6. Broadcast knowledge & skills
|
|
127
143
|
await this.hive.broadcastKnowledge();
|
|
128
|
-
//
|
|
144
|
+
// 6b. Emergent Skill Synthesis
|
|
145
|
+
await this.skillSynthesizer.discoverAndSynthesize();
|
|
146
|
+
// 7. Evolutionary pulse
|
|
129
147
|
await this.pilot.runSelfImprovementCycle();
|
|
130
148
|
console.log('[Cortex] Soul-Searching loop completed.');
|
|
131
149
|
}
|
|
132
150
|
catch (err) {
|
|
133
151
|
console.error('[Cortex] Soul-Searching loop failed:', err);
|
|
134
152
|
// Telemetry: track failure
|
|
135
|
-
await this.telemetry.track('system', 'error', 'Self-iteration failed', {
|
|
153
|
+
await this.telemetry.track('system', 'error', 'Self-iteration failed', {
|
|
154
|
+
error: String(err),
|
|
155
|
+
});
|
|
136
156
|
}
|
|
137
157
|
}
|
|
138
158
|
/**
|
|
@@ -149,8 +169,11 @@ export class Cortex {
|
|
|
149
169
|
async recordInteraction(sessionId, role, content, options = {}) {
|
|
150
170
|
const message = await this.sessions.addMessage(sessionId, role, content);
|
|
151
171
|
// Telemetry: Track prompt and output
|
|
152
|
-
const type = role === 'user' ? 'prompt' :
|
|
153
|
-
await this.telemetry.track(sessionId, type, content, {
|
|
172
|
+
const type = role === 'user' ? 'prompt' : role === 'assistant' ? 'output' : 'action';
|
|
173
|
+
await this.telemetry.track(sessionId, type, content, {
|
|
174
|
+
role,
|
|
175
|
+
messageId: message.id,
|
|
176
|
+
});
|
|
154
177
|
if (options.index && options.embedding && this.vectors) {
|
|
155
178
|
await this.vectors.addMemory(content, options.embedding, sessionId);
|
|
156
179
|
}
|
|
@@ -26,7 +26,7 @@ export class EpisodicMemory {
|
|
|
26
26
|
name,
|
|
27
27
|
status: 'active',
|
|
28
28
|
start_time: new Date(),
|
|
29
|
-
metadata: metadata ? JSON.stringify(metadata) : null
|
|
29
|
+
metadata: metadata ? JSON.stringify(metadata) : null,
|
|
30
30
|
})
|
|
31
31
|
.returningAll()
|
|
32
32
|
.executeTakeFirstOrThrow();
|
|
@@ -52,7 +52,7 @@ export class EpisodicMemory {
|
|
|
52
52
|
status: 'completed',
|
|
53
53
|
summary,
|
|
54
54
|
end_time: new Date(),
|
|
55
|
-
metadata: JSON.stringify(newMeta)
|
|
55
|
+
metadata: JSON.stringify(newMeta),
|
|
56
56
|
})
|
|
57
57
|
.where('id', '=', episodeId)
|
|
58
58
|
.returningAll()
|
|
@@ -70,7 +70,7 @@ export class EpisodicMemory {
|
|
|
70
70
|
.where('session_id', '=', sessionId)
|
|
71
71
|
.orderBy('start_time', 'desc')
|
|
72
72
|
.execute();
|
|
73
|
-
return list.map(e => this.parseEpisode(e));
|
|
73
|
+
return list.map((e) => this.parseEpisode(e));
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* Get recently completed episodes across all sessions.
|
|
@@ -83,7 +83,7 @@ export class EpisodicMemory {
|
|
|
83
83
|
.orderBy('end_time', 'desc')
|
|
84
84
|
.limit(limit)
|
|
85
85
|
.execute();
|
|
86
|
-
return list.map(e => this.parseEpisode(e));
|
|
86
|
+
return list.map((e) => this.parseEpisode(e));
|
|
87
87
|
}
|
|
88
88
|
parseEpisode(episode) {
|
|
89
89
|
return {
|
|
@@ -94,7 +94,9 @@ export class EpisodicMemory {
|
|
|
94
94
|
status: episode.status,
|
|
95
95
|
startTime: new Date(episode.start_time),
|
|
96
96
|
endTime: episode.end_time ? new Date(episode.end_time) : undefined,
|
|
97
|
-
metadata: typeof episode.metadata === 'string'
|
|
97
|
+
metadata: typeof episode.metadata === 'string'
|
|
98
|
+
? JSON.parse(episode.metadata)
|
|
99
|
+
: episode.metadata || {},
|
|
98
100
|
};
|
|
99
101
|
}
|
|
100
102
|
}
|
|
@@ -26,10 +26,12 @@ export class PersonaManager {
|
|
|
26
26
|
const values = {
|
|
27
27
|
name,
|
|
28
28
|
role: options.role || null,
|
|
29
|
-
capabilities: options.capabilities
|
|
29
|
+
capabilities: options.capabilities
|
|
30
|
+
? JSON.stringify(options.capabilities)
|
|
31
|
+
: null,
|
|
30
32
|
policies: options.policies ? JSON.stringify(options.policies) : null,
|
|
31
33
|
metadata: options.metadata ? JSON.stringify(options.metadata) : null,
|
|
32
|
-
updated_at: new Date()
|
|
34
|
+
updated_at: new Date(),
|
|
33
35
|
};
|
|
34
36
|
if (existing) {
|
|
35
37
|
const updated = await this.typedDb
|
|
@@ -44,7 +46,7 @@ export class PersonaManager {
|
|
|
44
46
|
.insertInto(this.personasTable)
|
|
45
47
|
.values({
|
|
46
48
|
...values,
|
|
47
|
-
created_at: new Date()
|
|
49
|
+
created_at: new Date(),
|
|
48
50
|
})
|
|
49
51
|
.returningAll()
|
|
50
52
|
.executeTakeFirstOrThrow();
|
|
@@ -80,18 +82,24 @@ export class PersonaManager {
|
|
|
80
82
|
.selectAll()
|
|
81
83
|
.orderBy('name', 'asc')
|
|
82
84
|
.execute();
|
|
83
|
-
return list.map(p => this.parsePersona(p));
|
|
85
|
+
return list.map((p) => this.parsePersona(p));
|
|
84
86
|
}
|
|
85
87
|
parsePersona(p) {
|
|
86
88
|
return {
|
|
87
89
|
id: p.id,
|
|
88
90
|
name: p.name,
|
|
89
91
|
role: p.role || undefined,
|
|
90
|
-
capabilities: typeof p.capabilities === 'string'
|
|
91
|
-
|
|
92
|
-
|
|
92
|
+
capabilities: typeof p.capabilities === 'string'
|
|
93
|
+
? JSON.parse(p.capabilities)
|
|
94
|
+
: p.capabilities || [],
|
|
95
|
+
policies: typeof p.policies === 'string'
|
|
96
|
+
? JSON.parse(p.policies)
|
|
97
|
+
: p.policies || [],
|
|
98
|
+
metadata: typeof p.metadata === 'string'
|
|
99
|
+
? JSON.parse(p.metadata)
|
|
100
|
+
: p.metadata || {},
|
|
93
101
|
createdAt: new Date(p.created_at),
|
|
94
|
-
updatedAt: new Date(p.updated_at)
|
|
102
|
+
updatedAt: new Date(p.updated_at),
|
|
95
103
|
};
|
|
96
104
|
}
|
|
97
105
|
}
|
|
@@ -34,7 +34,7 @@ export class PolicyEnforcer {
|
|
|
34
34
|
type,
|
|
35
35
|
definition: JSON.stringify(definition),
|
|
36
36
|
is_enabled: isEnabled,
|
|
37
|
-
updated_at: new Date()
|
|
37
|
+
updated_at: new Date(),
|
|
38
38
|
})
|
|
39
39
|
.where('id', '=', existing.id)
|
|
40
40
|
.returningAll()
|
|
@@ -49,7 +49,7 @@ export class PolicyEnforcer {
|
|
|
49
49
|
definition: JSON.stringify(definition),
|
|
50
50
|
is_enabled: isEnabled,
|
|
51
51
|
created_at: new Date(),
|
|
52
|
-
updated_at: new Date()
|
|
52
|
+
updated_at: new Date(),
|
|
53
53
|
})
|
|
54
54
|
.returningAll()
|
|
55
55
|
.executeTakeFirstOrThrow();
|
|
@@ -73,20 +73,32 @@ export class PolicyEnforcer {
|
|
|
73
73
|
// 1. Threshold Check (Numeric)
|
|
74
74
|
if (typeof value === 'number') {
|
|
75
75
|
if (def.max !== undefined && value > def.max) {
|
|
76
|
-
return {
|
|
76
|
+
return {
|
|
77
|
+
allowed: false,
|
|
78
|
+
reason: `Value ${value} exceeds max ${def.max} for policy '${name}'`,
|
|
79
|
+
};
|
|
77
80
|
}
|
|
78
81
|
if (def.min !== undefined && value < def.min) {
|
|
79
|
-
return {
|
|
82
|
+
return {
|
|
83
|
+
allowed: false,
|
|
84
|
+
reason: `Value ${value} below min ${def.min} for policy '${name}'`,
|
|
85
|
+
};
|
|
80
86
|
}
|
|
81
87
|
}
|
|
82
88
|
// 2. Pattern Check (String/Regex)
|
|
83
89
|
if (typeof value === 'string' && def.pattern) {
|
|
84
90
|
const regex = new RegExp(def.pattern, 'i');
|
|
85
91
|
if (def.mustMatch && !regex.test(value)) {
|
|
86
|
-
return {
|
|
92
|
+
return {
|
|
93
|
+
allowed: false,
|
|
94
|
+
reason: `Value does not match required pattern for policy '${name}'`,
|
|
95
|
+
};
|
|
87
96
|
}
|
|
88
97
|
if (!def.mustMatch && regex.test(value)) {
|
|
89
|
-
return {
|
|
98
|
+
return {
|
|
99
|
+
allowed: false,
|
|
100
|
+
reason: `Value contains forbidden pattern for policy '${name}'`,
|
|
101
|
+
};
|
|
90
102
|
}
|
|
91
103
|
}
|
|
92
104
|
// 3. Budget Check (Cumulative)
|
|
@@ -95,7 +107,10 @@ export class PolicyEnforcer {
|
|
|
95
107
|
const limit = def.limit || 0;
|
|
96
108
|
const total = await this.getCumulativeMetric(def.metricName, period);
|
|
97
109
|
if (total + (typeof value === 'number' ? value : 0) > limit) {
|
|
98
|
-
return {
|
|
110
|
+
return {
|
|
111
|
+
allowed: false,
|
|
112
|
+
reason: `Cumulative budget for '${def.metricName}' exceeded (${total.toFixed(4)} / ${limit})`,
|
|
113
|
+
};
|
|
99
114
|
}
|
|
100
115
|
}
|
|
101
116
|
return { allowed: true };
|
|
@@ -122,7 +137,7 @@ export class PolicyEnforcer {
|
|
|
122
137
|
}
|
|
123
138
|
return {
|
|
124
139
|
allowed: violations.length === 0,
|
|
125
|
-
violations
|
|
140
|
+
violations,
|
|
126
141
|
};
|
|
127
142
|
}
|
|
128
143
|
/**
|
|
@@ -134,7 +149,7 @@ export class PolicyEnforcer {
|
|
|
134
149
|
.selectAll()
|
|
135
150
|
.where('is_enabled', '=', true)
|
|
136
151
|
.execute();
|
|
137
|
-
return list.map(p => this.parsePolicy(p));
|
|
152
|
+
return list.map((p) => this.parsePolicy(p));
|
|
138
153
|
}
|
|
139
154
|
async getCumulativeMetric(metricName, period) {
|
|
140
155
|
let cutoff = new Date(0); // beginning of time
|
|
@@ -158,11 +173,15 @@ export class PolicyEnforcer {
|
|
|
158
173
|
id: p.id,
|
|
159
174
|
name: p.name,
|
|
160
175
|
type: p.type,
|
|
161
|
-
definition: typeof p.definition === 'string'
|
|
176
|
+
definition: typeof p.definition === 'string'
|
|
177
|
+
? JSON.parse(p.definition)
|
|
178
|
+
: p.definition,
|
|
162
179
|
isEnabled: !!p.is_enabled,
|
|
163
|
-
metadata: typeof p.metadata === 'string'
|
|
180
|
+
metadata: typeof p.metadata === 'string'
|
|
181
|
+
? JSON.parse(p.metadata)
|
|
182
|
+
: p.metadata || {},
|
|
164
183
|
createdAt: new Date(p.created_at),
|
|
165
|
-
updatedAt: new Date(p.updated_at)
|
|
184
|
+
updatedAt: new Date(p.updated_at),
|
|
166
185
|
};
|
|
167
186
|
}
|
|
168
187
|
}
|
|
@@ -30,7 +30,7 @@ export class ResourceMonitor {
|
|
|
30
30
|
cost: cost || 0,
|
|
31
31
|
currency: 'USD',
|
|
32
32
|
metadata: metadata ? JSON.stringify(metadata) : null,
|
|
33
|
-
created_at: new Date()
|
|
33
|
+
created_at: new Date(),
|
|
34
34
|
})
|
|
35
35
|
.returningAll()
|
|
36
36
|
.executeTakeFirstOrThrow();
|
|
@@ -66,14 +66,14 @@ export class ResourceMonitor {
|
|
|
66
66
|
.select([
|
|
67
67
|
'model_name',
|
|
68
68
|
(eb) => eb.fn.sum(sql `input_tokens + output_tokens`).as('totalTokens'),
|
|
69
|
-
(eb) => eb.fn.sum('cost').as('totalCost')
|
|
69
|
+
(eb) => eb.fn.sum('cost').as('totalCost'),
|
|
70
70
|
])
|
|
71
71
|
.groupBy('model_name')
|
|
72
72
|
.execute();
|
|
73
73
|
return results.map((r) => ({
|
|
74
74
|
modelName: r.model_name,
|
|
75
75
|
totalTokens: Number(r.totalTokens),
|
|
76
|
-
totalCost: Number(r.totalCost)
|
|
76
|
+
totalCost: Number(r.totalCost),
|
|
77
77
|
}));
|
|
78
78
|
}
|
|
79
79
|
parseUsage(usage) {
|
|
@@ -86,7 +86,7 @@ export class ResourceMonitor {
|
|
|
86
86
|
outputTokens: usage.output_tokens,
|
|
87
87
|
cost: usage.cost,
|
|
88
88
|
currency: usage.currency,
|
|
89
|
-
createdAt: new Date(usage.created_at)
|
|
89
|
+
createdAt: new Date(usage.created_at),
|
|
90
90
|
};
|
|
91
91
|
}
|
|
92
92
|
}
|
|
@@ -17,7 +17,7 @@ export class SessionCompressor {
|
|
|
17
17
|
*/
|
|
18
18
|
async compress(sessionId, summary, startMessageId, endMessageId, anchors = [], metadata) {
|
|
19
19
|
console.log(`[SessionCompressor] Compressing session ${sessionId} with ${anchors.length} semantic anchors.`);
|
|
20
|
-
const epoch = await this.db
|
|
20
|
+
const epoch = (await this.db
|
|
21
21
|
.insertInto(this.epochsTable)
|
|
22
22
|
.values({
|
|
23
23
|
session_id: sessionId,
|
|
@@ -27,10 +27,10 @@ export class SessionCompressor {
|
|
|
27
27
|
metadata: metadata || anchors.length > 0
|
|
28
28
|
? JSON.stringify({ ...metadata, anchors })
|
|
29
29
|
: null,
|
|
30
|
-
created_at: new Date()
|
|
30
|
+
created_at: new Date(),
|
|
31
31
|
})
|
|
32
32
|
.returningAll()
|
|
33
|
-
.executeTakeFirstOrThrow();
|
|
33
|
+
.executeTakeFirstOrThrow());
|
|
34
34
|
return this.parseEpoch(epoch);
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
@@ -49,8 +49,8 @@ export class SessionCompressor {
|
|
|
49
49
|
eb('metadata', 'is', null),
|
|
50
50
|
eb.and([
|
|
51
51
|
eb('metadata', 'not like', '%"anchor":true%'),
|
|
52
|
-
eb('metadata', 'not like', '%"anchor": true%')
|
|
53
|
-
])
|
|
52
|
+
eb('metadata', 'not like', '%"anchor": true%'),
|
|
53
|
+
]),
|
|
54
54
|
]));
|
|
55
55
|
}
|
|
56
56
|
const result = await query.execute();
|
|
@@ -68,11 +68,15 @@ export class SessionCompressor {
|
|
|
68
68
|
if (epochs.length > 10) {
|
|
69
69
|
console.log(`[SessionCompressor] Consolidating ${epochs.length} epochs into a new Era for session ${sessionId}`);
|
|
70
70
|
const summary = `Consolidated era summary of ${epochs.length} epochs: ` +
|
|
71
|
-
epochs
|
|
71
|
+
epochs
|
|
72
|
+
.slice(0, 5)
|
|
73
|
+
.map((e) => e.summary)
|
|
74
|
+
.join('; ') +
|
|
75
|
+
'...';
|
|
72
76
|
const eraMetadata = {
|
|
73
77
|
type: 'era',
|
|
74
78
|
original_epochs: epochs.length,
|
|
75
|
-
consolidated_at: new Date().toISOString()
|
|
79
|
+
consolidated_at: new Date().toISOString(),
|
|
76
80
|
};
|
|
77
81
|
// In this version, we mark the new epoch as an "Era" in metadata
|
|
78
82
|
const consolidatedEpoch = await this.compress(sessionId, summary, epochs[0].startMessageId, epochs[epochs.length - 1].endMessageId, [], eraMetadata);
|
|
@@ -85,12 +89,16 @@ export class SessionCompressor {
|
|
|
85
89
|
session_id: sessionId,
|
|
86
90
|
role: 'system',
|
|
87
91
|
content: `[ERA SUMMARY] This era covers the initial phases of the session. Key takeaways: ${summary}`,
|
|
88
|
-
metadata: JSON.stringify({
|
|
89
|
-
|
|
92
|
+
metadata: JSON.stringify({
|
|
93
|
+
anchor: true,
|
|
94
|
+
type: 'era_reification',
|
|
95
|
+
epoch_id: consolidatedEpoch.id,
|
|
96
|
+
}),
|
|
97
|
+
created_at: new Date(),
|
|
90
98
|
})
|
|
91
99
|
.execute();
|
|
92
100
|
// Prune old epochs that were consolidated
|
|
93
|
-
const epochIds = epochs.map(e => e.id);
|
|
101
|
+
const epochIds = epochs.map((e) => e.id);
|
|
94
102
|
await this.db
|
|
95
103
|
.deleteFrom(this.epochsTable)
|
|
96
104
|
.where('id', 'in', epochIds)
|
|
@@ -101,13 +109,13 @@ export class SessionCompressor {
|
|
|
101
109
|
* Get epochs for a session
|
|
102
110
|
*/
|
|
103
111
|
async getEpochs(sessionId) {
|
|
104
|
-
const list = await this.db
|
|
112
|
+
const list = (await this.db
|
|
105
113
|
.selectFrom(this.epochsTable)
|
|
106
114
|
.selectAll()
|
|
107
115
|
.where('session_id', '=', sessionId)
|
|
108
116
|
.orderBy('created_at', 'asc')
|
|
109
|
-
.execute();
|
|
110
|
-
return list.map(e => this.parseEpoch(e));
|
|
117
|
+
.execute());
|
|
118
|
+
return list.map((e) => this.parseEpoch(e));
|
|
111
119
|
}
|
|
112
120
|
parseEpoch(e) {
|
|
113
121
|
return {
|
|
@@ -116,7 +124,7 @@ export class SessionCompressor {
|
|
|
116
124
|
startMessageId: e.start_message_id,
|
|
117
125
|
endMessageId: e.end_message_id,
|
|
118
126
|
metadata: typeof e.metadata === 'string' ? JSON.parse(e.metadata) : e.metadata,
|
|
119
|
-
createdAt: new Date(e.created_at)
|
|
127
|
+
createdAt: new Date(e.created_at),
|
|
120
128
|
};
|
|
121
129
|
}
|
|
122
130
|
}
|