@timmeck/marketing-brain 0.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/.mcp.json +9 -0
- package/README.md +342 -0
- package/dashboard.html +666 -0
- package/dist/api/server.d.ts +15 -0
- package/dist/api/server.js +73 -0
- package/dist/api/server.js.map +1 -0
- package/dist/cli/colors.d.ts +43 -0
- package/dist/cli/colors.js +54 -0
- package/dist/cli/colors.js.map +1 -0
- package/dist/cli/commands/campaign.d.ts +2 -0
- package/dist/cli/commands/campaign.js +62 -0
- package/dist/cli/commands/campaign.js.map +1 -0
- package/dist/cli/commands/config.d.ts +2 -0
- package/dist/cli/commands/config.js +164 -0
- package/dist/cli/commands/config.js.map +1 -0
- package/dist/cli/commands/dashboard.d.ts +2 -0
- package/dist/cli/commands/dashboard.js +147 -0
- package/dist/cli/commands/dashboard.js.map +1 -0
- package/dist/cli/commands/doctor.d.ts +2 -0
- package/dist/cli/commands/doctor.js +111 -0
- package/dist/cli/commands/doctor.js.map +1 -0
- package/dist/cli/commands/export.d.ts +2 -0
- package/dist/cli/commands/export.js +37 -0
- package/dist/cli/commands/export.js.map +1 -0
- package/dist/cli/commands/import.d.ts +2 -0
- package/dist/cli/commands/import.js +76 -0
- package/dist/cli/commands/import.js.map +1 -0
- package/dist/cli/commands/insights.d.ts +2 -0
- package/dist/cli/commands/insights.js +41 -0
- package/dist/cli/commands/insights.js.map +1 -0
- package/dist/cli/commands/learn.d.ts +2 -0
- package/dist/cli/commands/learn.js +22 -0
- package/dist/cli/commands/learn.js.map +1 -0
- package/dist/cli/commands/network.d.ts +2 -0
- package/dist/cli/commands/network.js +66 -0
- package/dist/cli/commands/network.js.map +1 -0
- package/dist/cli/commands/post.d.ts +2 -0
- package/dist/cli/commands/post.js +45 -0
- package/dist/cli/commands/post.js.map +1 -0
- package/dist/cli/commands/query.d.ts +2 -0
- package/dist/cli/commands/query.js +96 -0
- package/dist/cli/commands/query.js.map +1 -0
- package/dist/cli/commands/rules.d.ts +2 -0
- package/dist/cli/commands/rules.js +25 -0
- package/dist/cli/commands/rules.js.map +1 -0
- package/dist/cli/commands/start.d.ts +2 -0
- package/dist/cli/commands/start.js +91 -0
- package/dist/cli/commands/start.js.map +1 -0
- package/dist/cli/commands/status.d.ts +2 -0
- package/dist/cli/commands/status.js +63 -0
- package/dist/cli/commands/status.js.map +1 -0
- package/dist/cli/commands/stop.d.ts +2 -0
- package/dist/cli/commands/stop.js +34 -0
- package/dist/cli/commands/stop.js.map +1 -0
- package/dist/cli/commands/suggest.d.ts +2 -0
- package/dist/cli/commands/suggest.js +57 -0
- package/dist/cli/commands/suggest.js.map +1 -0
- package/dist/cli/ipc-helper.d.ts +2 -0
- package/dist/cli/ipc-helper.js +26 -0
- package/dist/cli/ipc-helper.js.map +1 -0
- package/dist/cli/update-check.d.ts +2 -0
- package/dist/cli/update-check.js +58 -0
- package/dist/cli/update-check.js.map +1 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +111 -0
- package/dist/config.js.map +1 -0
- package/dist/dashboard/renderer.d.ts +11 -0
- package/dist/dashboard/renderer.js +112 -0
- package/dist/dashboard/renderer.js.map +1 -0
- package/dist/dashboard/server.d.ts +15 -0
- package/dist/dashboard/server.js +122 -0
- package/dist/dashboard/server.js.map +1 -0
- package/dist/db/connection.d.ts +2 -0
- package/dist/db/connection.js +19 -0
- package/dist/db/connection.js.map +1 -0
- package/dist/db/migrations/001_core_schema.d.ts +2 -0
- package/dist/db/migrations/001_core_schema.js +62 -0
- package/dist/db/migrations/001_core_schema.js.map +1 -0
- package/dist/db/migrations/002_learning_schema.d.ts +2 -0
- package/dist/db/migrations/002_learning_schema.js +45 -0
- package/dist/db/migrations/002_learning_schema.js.map +1 -0
- package/dist/db/migrations/003_synapse_schema.d.ts +2 -0
- package/dist/db/migrations/003_synapse_schema.js +26 -0
- package/dist/db/migrations/003_synapse_schema.js.map +1 -0
- package/dist/db/migrations/004_insights_schema.d.ts +2 -0
- package/dist/db/migrations/004_insights_schema.js +37 -0
- package/dist/db/migrations/004_insights_schema.js.map +1 -0
- package/dist/db/migrations/005_fts_indexes.d.ts +2 -0
- package/dist/db/migrations/005_fts_indexes.js +76 -0
- package/dist/db/migrations/005_fts_indexes.js.map +1 -0
- package/dist/db/migrations/index.d.ts +2 -0
- package/dist/db/migrations/index.js +47 -0
- package/dist/db/migrations/index.js.map +1 -0
- package/dist/db/repositories/audience.repository.d.ts +18 -0
- package/dist/db/repositories/audience.repository.js +45 -0
- package/dist/db/repositories/audience.repository.js.map +1 -0
- package/dist/db/repositories/campaign.repository.d.ts +15 -0
- package/dist/db/repositories/campaign.repository.js +58 -0
- package/dist/db/repositories/campaign.repository.js.map +1 -0
- package/dist/db/repositories/engagement.repository.d.ts +26 -0
- package/dist/db/repositories/engagement.repository.js +83 -0
- package/dist/db/repositories/engagement.repository.js.map +1 -0
- package/dist/db/repositories/insight.repository.d.ts +18 -0
- package/dist/db/repositories/insight.repository.js +87 -0
- package/dist/db/repositories/insight.repository.js.map +1 -0
- package/dist/db/repositories/post.repository.d.ts +21 -0
- package/dist/db/repositories/post.repository.js +105 -0
- package/dist/db/repositories/post.repository.js.map +1 -0
- package/dist/db/repositories/rule.repository.d.ts +16 -0
- package/dist/db/repositories/rule.repository.js +71 -0
- package/dist/db/repositories/rule.repository.js.map +1 -0
- package/dist/db/repositories/strategy.repository.d.ts +16 -0
- package/dist/db/repositories/strategy.repository.js +69 -0
- package/dist/db/repositories/strategy.repository.js.map +1 -0
- package/dist/db/repositories/synapse.repository.d.ts +25 -0
- package/dist/db/repositories/synapse.repository.js +115 -0
- package/dist/db/repositories/synapse.repository.js.map +1 -0
- package/dist/db/repositories/template.repository.d.ts +16 -0
- package/dist/db/repositories/template.repository.js +61 -0
- package/dist/db/repositories/template.repository.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +62 -0
- package/dist/index.js.map +1 -0
- package/dist/ipc/client.d.ts +13 -0
- package/dist/ipc/client.js +93 -0
- package/dist/ipc/client.js.map +1 -0
- package/dist/ipc/protocol.d.ts +8 -0
- package/dist/ipc/protocol.js +29 -0
- package/dist/ipc/protocol.js.map +1 -0
- package/dist/ipc/router.d.ts +30 -0
- package/dist/ipc/router.js +88 -0
- package/dist/ipc/router.js.map +1 -0
- package/dist/ipc/server.d.ts +14 -0
- package/dist/ipc/server.js +130 -0
- package/dist/ipc/server.js.map +1 -0
- package/dist/learning/confidence-scorer.d.ts +17 -0
- package/dist/learning/confidence-scorer.js +26 -0
- package/dist/learning/confidence-scorer.js.map +1 -0
- package/dist/learning/learning-engine.d.ts +33 -0
- package/dist/learning/learning-engine.js +211 -0
- package/dist/learning/learning-engine.js.map +1 -0
- package/dist/marketing-core.d.ts +17 -0
- package/dist/marketing-core.js +233 -0
- package/dist/marketing-core.js.map +1 -0
- package/dist/mcp/server.d.ts +1 -0
- package/dist/mcp/server.js +67 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tools.d.ts +3 -0
- package/dist/mcp/tools.js +138 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/research/research-engine.d.ts +28 -0
- package/dist/research/research-engine.js +211 -0
- package/dist/research/research-engine.js.map +1 -0
- package/dist/services/analytics.service.d.ts +116 -0
- package/dist/services/analytics.service.js +69 -0
- package/dist/services/analytics.service.js.map +1 -0
- package/dist/services/audience.service.d.ts +20 -0
- package/dist/services/audience.service.js +30 -0
- package/dist/services/audience.service.js.map +1 -0
- package/dist/services/campaign.service.d.ts +27 -0
- package/dist/services/campaign.service.js +65 -0
- package/dist/services/campaign.service.js.map +1 -0
- package/dist/services/insight.service.d.ts +18 -0
- package/dist/services/insight.service.js +40 -0
- package/dist/services/insight.service.js.map +1 -0
- package/dist/services/post.service.d.ts +48 -0
- package/dist/services/post.service.js +93 -0
- package/dist/services/post.service.js.map +1 -0
- package/dist/services/rule.service.d.ts +29 -0
- package/dist/services/rule.service.js +67 -0
- package/dist/services/rule.service.js.map +1 -0
- package/dist/services/strategy.service.d.ts +17 -0
- package/dist/services/strategy.service.js +39 -0
- package/dist/services/strategy.service.js.map +1 -0
- package/dist/services/synapse.service.d.ts +22 -0
- package/dist/services/synapse.service.js +22 -0
- package/dist/services/synapse.service.js.map +1 -0
- package/dist/services/template.service.d.ts +17 -0
- package/dist/services/template.service.js +37 -0
- package/dist/services/template.service.js.map +1 -0
- package/dist/synapses/activation.d.ts +13 -0
- package/dist/synapses/activation.js +50 -0
- package/dist/synapses/activation.js.map +1 -0
- package/dist/synapses/decay.d.ts +11 -0
- package/dist/synapses/decay.js +27 -0
- package/dist/synapses/decay.js.map +1 -0
- package/dist/synapses/hebbian.d.ts +13 -0
- package/dist/synapses/hebbian.js +35 -0
- package/dist/synapses/hebbian.js.map +1 -0
- package/dist/synapses/pathfinder.d.ts +14 -0
- package/dist/synapses/pathfinder.js +50 -0
- package/dist/synapses/pathfinder.js.map +1 -0
- package/dist/synapses/synapse-manager.d.ts +32 -0
- package/dist/synapses/synapse-manager.js +76 -0
- package/dist/synapses/synapse-manager.js.map +1 -0
- package/dist/types/config.types.d.ts +69 -0
- package/dist/types/config.types.js +2 -0
- package/dist/types/config.types.js.map +1 -0
- package/dist/types/ipc.types.d.ts +11 -0
- package/dist/types/ipc.types.js +2 -0
- package/dist/types/ipc.types.js.map +1 -0
- package/dist/types/post.types.d.ts +141 -0
- package/dist/types/post.types.js +2 -0
- package/dist/types/post.types.js.map +1 -0
- package/dist/types/synapse.types.d.ts +23 -0
- package/dist/types/synapse.types.js +2 -0
- package/dist/types/synapse.types.js.map +1 -0
- package/dist/utils/events.d.ts +57 -0
- package/dist/utils/events.js +23 -0
- package/dist/utils/events.js.map +1 -0
- package/dist/utils/hash.d.ts +1 -0
- package/dist/utils/hash.js +5 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/logger.d.ts +8 -0
- package/dist/utils/logger.js +39 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/paths.d.ts +3 -0
- package/dist/utils/paths.js +18 -0
- package/dist/utils/paths.js.map +1 -0
- package/package.json +40 -0
- package/seed-data.json +78 -0
- package/src/api/server.ts +86 -0
- package/src/cli/colors.ts +59 -0
- package/src/cli/commands/campaign.ts +66 -0
- package/src/cli/commands/config.ts +168 -0
- package/src/cli/commands/dashboard.ts +165 -0
- package/src/cli/commands/doctor.ts +110 -0
- package/src/cli/commands/export.ts +40 -0
- package/src/cli/commands/import.ts +84 -0
- package/src/cli/commands/insights.ts +44 -0
- package/src/cli/commands/learn.ts +24 -0
- package/src/cli/commands/network.ts +71 -0
- package/src/cli/commands/post.ts +47 -0
- package/src/cli/commands/query.ts +108 -0
- package/src/cli/commands/rules.ts +27 -0
- package/src/cli/commands/start.ts +100 -0
- package/src/cli/commands/status.ts +73 -0
- package/src/cli/commands/stop.ts +33 -0
- package/src/cli/commands/suggest.ts +64 -0
- package/src/cli/ipc-helper.ts +22 -0
- package/src/cli/update-check.ts +63 -0
- package/src/config.ts +110 -0
- package/src/dashboard/renderer.ts +136 -0
- package/src/dashboard/server.ts +140 -0
- package/src/db/connection.ts +22 -0
- package/src/db/migrations/001_core_schema.ts +63 -0
- package/src/db/migrations/002_learning_schema.ts +46 -0
- package/src/db/migrations/003_synapse_schema.ts +27 -0
- package/src/db/migrations/004_insights_schema.ts +38 -0
- package/src/db/migrations/005_fts_indexes.ts +77 -0
- package/src/db/migrations/index.ts +62 -0
- package/src/db/repositories/audience.repository.ts +53 -0
- package/src/db/repositories/campaign.repository.ts +72 -0
- package/src/db/repositories/engagement.repository.ts +108 -0
- package/src/db/repositories/insight.repository.ts +100 -0
- package/src/db/repositories/post.repository.ts +123 -0
- package/src/db/repositories/rule.repository.ts +87 -0
- package/src/db/repositories/strategy.repository.ts +82 -0
- package/src/db/repositories/synapse.repository.ts +148 -0
- package/src/db/repositories/template.repository.ts +76 -0
- package/src/index.ts +69 -0
- package/src/ipc/client.ts +110 -0
- package/src/ipc/protocol.ts +35 -0
- package/src/ipc/router.ts +126 -0
- package/src/ipc/server.ts +140 -0
- package/src/learning/confidence-scorer.ts +36 -0
- package/src/learning/learning-engine.ts +254 -0
- package/src/marketing-core.ts +285 -0
- package/src/mcp/server.ts +72 -0
- package/src/mcp/tools.ts +216 -0
- package/src/research/research-engine.ts +226 -0
- package/src/services/analytics.service.ts +73 -0
- package/src/services/audience.service.ts +40 -0
- package/src/services/campaign.service.ts +80 -0
- package/src/services/insight.service.ts +54 -0
- package/src/services/post.service.ts +116 -0
- package/src/services/rule.service.ts +90 -0
- package/src/services/strategy.service.ts +53 -0
- package/src/services/synapse.service.ts +32 -0
- package/src/services/template.service.ts +50 -0
- package/src/synapses/activation.ts +80 -0
- package/src/synapses/decay.ts +38 -0
- package/src/synapses/hebbian.ts +68 -0
- package/src/synapses/pathfinder.ts +81 -0
- package/src/synapses/synapse-manager.ts +115 -0
- package/src/types/config.types.ts +79 -0
- package/src/types/ipc.types.ts +8 -0
- package/src/types/post.types.ts +156 -0
- package/src/types/synapse.types.ts +43 -0
- package/src/utils/events.ts +44 -0
- package/src/utils/hash.ts +5 -0
- package/src/utils/logger.ts +48 -0
- package/src/utils/paths.ts +19 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { StrategyRepository } from '../db/repositories/strategy.repository.js';
|
|
2
|
+
import type { SynapseManager } from '../synapses/synapse-manager.js';
|
|
3
|
+
import type { Strategy, StrategyCreate } from '../types/post.types.js';
|
|
4
|
+
import { getEventBus } from '../utils/events.js';
|
|
5
|
+
|
|
6
|
+
export class StrategyService {
|
|
7
|
+
constructor(
|
|
8
|
+
private strategyRepo: StrategyRepository,
|
|
9
|
+
private synapseManager: SynapseManager,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
report(data: StrategyCreate): Strategy {
|
|
13
|
+
const id = this.strategyRepo.create(data);
|
|
14
|
+
const strategy = this.strategyRepo.getById(id)!;
|
|
15
|
+
|
|
16
|
+
if (data.post_id) {
|
|
17
|
+
this.synapseManager.strengthen(
|
|
18
|
+
{ type: 'strategy', id },
|
|
19
|
+
{ type: 'post', id: data.post_id },
|
|
20
|
+
'improves',
|
|
21
|
+
);
|
|
22
|
+
getEventBus().emit('strategy:reported', { strategyId: id, postId: data.post_id });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return strategy;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
suggest(query: string, limit: number = 5): Strategy[] {
|
|
29
|
+
return this.strategyRepo.search(query, limit);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getTopStrategies(minConfidence: number = 0.7, limit: number = 10): Strategy[] {
|
|
33
|
+
return this.strategyRepo.topByConfidence(minConfidence, limit);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
getById(id: number): Strategy | undefined {
|
|
37
|
+
return this.strategyRepo.getById(id);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
listAll(limit?: number): Strategy[] {
|
|
41
|
+
return this.strategyRepo.listAll(limit);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
updateConfidence(id: number, confidence: number): void {
|
|
45
|
+
this.strategyRepo.update(id, { confidence });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getStats() {
|
|
49
|
+
return {
|
|
50
|
+
total: this.strategyRepo.countAll(),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { SynapseManager } from '../synapses/synapse-manager.js';
|
|
2
|
+
import type { NodeType } from '../types/synapse.types.js';
|
|
3
|
+
|
|
4
|
+
export class SynapseService {
|
|
5
|
+
constructor(private synapseManager: SynapseManager) {}
|
|
6
|
+
|
|
7
|
+
getPostContext(postId: number) {
|
|
8
|
+
return this.synapseManager.getPostContext(postId);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
findPath(fromType: NodeType, fromId: number, toType: NodeType, toId: number) {
|
|
12
|
+
return this.synapseManager.findPath(
|
|
13
|
+
{ type: fromType, id: fromId },
|
|
14
|
+
{ type: toType, id: toId },
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getRelated(opts: { nodeType: NodeType; nodeId: number; maxDepth?: number }) {
|
|
19
|
+
return this.synapseManager.activate(
|
|
20
|
+
{ type: opts.nodeType, id: opts.nodeId },
|
|
21
|
+
opts.maxDepth,
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getNetworkStats() {
|
|
26
|
+
return this.synapseManager.getNetworkStats();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getStrongest(limit?: number) {
|
|
30
|
+
return this.synapseManager.getStrongestSynapses(limit);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { TemplateRepository } from '../db/repositories/template.repository.js';
|
|
2
|
+
import type { SynapseManager } from '../synapses/synapse-manager.js';
|
|
3
|
+
import type { ContentTemplate, ContentTemplateCreate } from '../types/post.types.js';
|
|
4
|
+
import { getEventBus } from '../utils/events.js';
|
|
5
|
+
|
|
6
|
+
export class TemplateService {
|
|
7
|
+
constructor(
|
|
8
|
+
private templateRepo: TemplateRepository,
|
|
9
|
+
private synapseManager: SynapseManager,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
create(data: ContentTemplateCreate): ContentTemplate {
|
|
13
|
+
const id = this.templateRepo.create(data);
|
|
14
|
+
const template = this.templateRepo.getById(id)!;
|
|
15
|
+
|
|
16
|
+
getEventBus().emit('template:created', { templateId: id, platform: data.platform ?? 'any' });
|
|
17
|
+
return template;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
find(query: string, limit: number = 5): ContentTemplate[] {
|
|
21
|
+
return this.templateRepo.search(query, limit);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
findByPlatform(platform: string, limit: number = 10): ContentTemplate[] {
|
|
25
|
+
return this.templateRepo.listByPlatform(platform, limit);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
useTemplate(templateId: number, postId: number): void {
|
|
29
|
+
this.templateRepo.incrementUseCount(templateId);
|
|
30
|
+
this.synapseManager.strengthen(
|
|
31
|
+
{ type: 'template', id: templateId },
|
|
32
|
+
{ type: 'post', id: postId },
|
|
33
|
+
'generated_from',
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getById(id: number): ContentTemplate | undefined {
|
|
38
|
+
return this.templateRepo.getById(id);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
listAll(limit?: number): ContentTemplate[] {
|
|
42
|
+
return this.templateRepo.listAll(limit);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getStats() {
|
|
46
|
+
return {
|
|
47
|
+
total: this.templateRepo.countAll(),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { NodeType } from '../types/synapse.types.js';
|
|
2
|
+
import type { SynapseRepository } from '../db/repositories/synapse.repository.js';
|
|
3
|
+
|
|
4
|
+
export interface ActivationNode {
|
|
5
|
+
type: NodeType;
|
|
6
|
+
id: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ActivationResult {
|
|
10
|
+
node: ActivationNode;
|
|
11
|
+
activation: number;
|
|
12
|
+
depth: number;
|
|
13
|
+
path: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function spreadingActivation(
|
|
17
|
+
repo: SynapseRepository,
|
|
18
|
+
startNode: ActivationNode,
|
|
19
|
+
maxDepth: number = 3,
|
|
20
|
+
minWeight: number = 0.2,
|
|
21
|
+
): ActivationResult[] {
|
|
22
|
+
const visited = new Set<string>();
|
|
23
|
+
const results: ActivationResult[] = [];
|
|
24
|
+
|
|
25
|
+
const queue: Array<{
|
|
26
|
+
node: ActivationNode;
|
|
27
|
+
depth: number;
|
|
28
|
+
pathWeight: number;
|
|
29
|
+
path: string[];
|
|
30
|
+
}> = [{ node: startNode, depth: 0, pathWeight: 1.0, path: [] }];
|
|
31
|
+
|
|
32
|
+
while (queue.length > 0) {
|
|
33
|
+
const current = queue.shift()!;
|
|
34
|
+
const key = `${current.node.type}:${current.node.id}`;
|
|
35
|
+
|
|
36
|
+
if (visited.has(key)) continue;
|
|
37
|
+
if (current.depth > maxDepth) continue;
|
|
38
|
+
if (current.pathWeight < minWeight) continue;
|
|
39
|
+
|
|
40
|
+
visited.add(key);
|
|
41
|
+
|
|
42
|
+
if (current.depth > 0) {
|
|
43
|
+
results.push({
|
|
44
|
+
node: current.node,
|
|
45
|
+
activation: current.pathWeight,
|
|
46
|
+
depth: current.depth,
|
|
47
|
+
path: current.path,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const outgoing = repo.getOutgoing(current.node.type, current.node.id);
|
|
52
|
+
const incoming = repo.getIncoming(current.node.type, current.node.id);
|
|
53
|
+
|
|
54
|
+
for (const synapse of outgoing) {
|
|
55
|
+
const nextWeight = current.pathWeight * synapse.weight;
|
|
56
|
+
if (nextWeight >= minWeight) {
|
|
57
|
+
queue.push({
|
|
58
|
+
node: { type: synapse.target_type, id: synapse.target_id },
|
|
59
|
+
depth: current.depth + 1,
|
|
60
|
+
pathWeight: nextWeight,
|
|
61
|
+
path: [...current.path, `--${synapse.synapse_type}-->`],
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for (const synapse of incoming) {
|
|
67
|
+
const nextWeight = current.pathWeight * synapse.weight;
|
|
68
|
+
if (nextWeight >= minWeight) {
|
|
69
|
+
queue.push({
|
|
70
|
+
node: { type: synapse.source_type, id: synapse.source_id },
|
|
71
|
+
depth: current.depth + 1,
|
|
72
|
+
pathWeight: nextWeight,
|
|
73
|
+
path: [...current.path, `<--${synapse.synapse_type}--`],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return results.sort((a, b) => b.activation - a.activation);
|
|
80
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { SynapseRepository } from '../db/repositories/synapse.repository.js';
|
|
2
|
+
|
|
3
|
+
export interface DecayConfig {
|
|
4
|
+
decayHalfLifeDays: number;
|
|
5
|
+
decayAfterDays: number;
|
|
6
|
+
pruneThreshold: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function timeDecayFactor(lastActivatedAt: string, halfLifeDays: number): number {
|
|
10
|
+
const now = Date.now();
|
|
11
|
+
const activated = new Date(lastActivatedAt).getTime();
|
|
12
|
+
const ageDays = (now - activated) / (1000 * 60 * 60 * 24);
|
|
13
|
+
return Math.pow(0.5, ageDays / halfLifeDays);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function decayAll(repo: SynapseRepository, config: DecayConfig): { decayed: number; pruned: number } {
|
|
17
|
+
const cutoff = new Date();
|
|
18
|
+
cutoff.setDate(cutoff.getDate() - config.decayAfterDays);
|
|
19
|
+
|
|
20
|
+
const stale = repo.findInactiveSince(cutoff.toISOString());
|
|
21
|
+
let pruned = 0;
|
|
22
|
+
let decayed = 0;
|
|
23
|
+
|
|
24
|
+
for (const synapse of stale) {
|
|
25
|
+
const factor = timeDecayFactor(synapse.last_activated_at, config.decayHalfLifeDays);
|
|
26
|
+
const newWeight = synapse.weight * factor;
|
|
27
|
+
|
|
28
|
+
if (newWeight < config.pruneThreshold) {
|
|
29
|
+
repo.delete(synapse.id);
|
|
30
|
+
pruned++;
|
|
31
|
+
} else {
|
|
32
|
+
repo.update(synapse.id, { weight: newWeight });
|
|
33
|
+
decayed++;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return { decayed, pruned };
|
|
38
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { SynapseRecord, NodeType, SynapseType } from '../types/synapse.types.js';
|
|
2
|
+
import type { SynapseRepository } from '../db/repositories/synapse.repository.js';
|
|
3
|
+
|
|
4
|
+
export interface HebbianConfig {
|
|
5
|
+
initialWeight: number;
|
|
6
|
+
learningRate: number;
|
|
7
|
+
pruneThreshold: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface NodeRef {
|
|
11
|
+
type: NodeType;
|
|
12
|
+
id: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function strengthen(
|
|
16
|
+
repo: SynapseRepository,
|
|
17
|
+
source: NodeRef,
|
|
18
|
+
target: NodeRef,
|
|
19
|
+
synapseType: SynapseType,
|
|
20
|
+
config: HebbianConfig,
|
|
21
|
+
context?: Record<string, unknown>,
|
|
22
|
+
): SynapseRecord {
|
|
23
|
+
const existing = repo.findBySourceTarget(
|
|
24
|
+
source.type, source.id, target.type, target.id, synapseType,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
if (existing) {
|
|
28
|
+
const newWeight = Math.min(
|
|
29
|
+
1.0,
|
|
30
|
+
existing.weight + (1.0 - existing.weight) * config.learningRate,
|
|
31
|
+
);
|
|
32
|
+
repo.update(existing.id, {
|
|
33
|
+
weight: newWeight,
|
|
34
|
+
activation_count: existing.activation_count + 1,
|
|
35
|
+
last_activated_at: new Date().toISOString(),
|
|
36
|
+
});
|
|
37
|
+
return { ...existing, weight: newWeight, activation_count: existing.activation_count + 1 };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const id = repo.create({
|
|
41
|
+
source_type: source.type,
|
|
42
|
+
source_id: source.id,
|
|
43
|
+
target_type: target.type,
|
|
44
|
+
target_id: target.id,
|
|
45
|
+
synapse_type: synapseType,
|
|
46
|
+
weight: config.initialWeight,
|
|
47
|
+
metadata: context ? JSON.stringify(context) : null,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return repo.getById(id)!;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function weaken(
|
|
54
|
+
repo: SynapseRepository,
|
|
55
|
+
synapseId: number,
|
|
56
|
+
config: HebbianConfig,
|
|
57
|
+
factor: number = 0.5,
|
|
58
|
+
): void {
|
|
59
|
+
const synapse = repo.getById(synapseId);
|
|
60
|
+
if (!synapse) return;
|
|
61
|
+
|
|
62
|
+
const newWeight = synapse.weight * factor;
|
|
63
|
+
if (newWeight < config.pruneThreshold) {
|
|
64
|
+
repo.delete(synapseId);
|
|
65
|
+
} else {
|
|
66
|
+
repo.update(synapseId, { weight: newWeight });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { NodeType, SynapseRecord } from '../types/synapse.types.js';
|
|
2
|
+
import type { SynapseRepository } from '../db/repositories/synapse.repository.js';
|
|
3
|
+
|
|
4
|
+
export interface PathNode {
|
|
5
|
+
type: NodeType;
|
|
6
|
+
id: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface SynapsePath {
|
|
10
|
+
from: PathNode;
|
|
11
|
+
to: PathNode;
|
|
12
|
+
synapses: SynapseRecord[];
|
|
13
|
+
totalWeight: number;
|
|
14
|
+
hops: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function findPath(
|
|
18
|
+
repo: SynapseRepository,
|
|
19
|
+
from: PathNode,
|
|
20
|
+
to: PathNode,
|
|
21
|
+
maxDepth: number = 5,
|
|
22
|
+
): SynapsePath | null {
|
|
23
|
+
const visited = new Set<string>();
|
|
24
|
+
|
|
25
|
+
const queue: Array<{
|
|
26
|
+
node: PathNode;
|
|
27
|
+
path: SynapseRecord[];
|
|
28
|
+
totalWeight: number;
|
|
29
|
+
}> = [{ node: from, path: [], totalWeight: 1.0 }];
|
|
30
|
+
|
|
31
|
+
let bestPath: SynapsePath | null = null;
|
|
32
|
+
|
|
33
|
+
while (queue.length > 0) {
|
|
34
|
+
const current = queue.shift()!;
|
|
35
|
+
const key = `${current.node.type}:${current.node.id}`;
|
|
36
|
+
|
|
37
|
+
if (visited.has(key)) continue;
|
|
38
|
+
visited.add(key);
|
|
39
|
+
|
|
40
|
+
if (current.node.type === to.type && current.node.id === to.id) {
|
|
41
|
+
if (!bestPath || current.totalWeight > bestPath.totalWeight) {
|
|
42
|
+
bestPath = {
|
|
43
|
+
from,
|
|
44
|
+
to,
|
|
45
|
+
synapses: current.path,
|
|
46
|
+
totalWeight: current.totalWeight,
|
|
47
|
+
hops: current.path.length,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (current.path.length >= maxDepth) continue;
|
|
54
|
+
|
|
55
|
+
const outgoing = repo.getOutgoing(current.node.type, current.node.id);
|
|
56
|
+
for (const synapse of outgoing) {
|
|
57
|
+
const targetKey = `${synapse.target_type}:${synapse.target_id}`;
|
|
58
|
+
if (!visited.has(targetKey)) {
|
|
59
|
+
queue.push({
|
|
60
|
+
node: { type: synapse.target_type, id: synapse.target_id },
|
|
61
|
+
path: [...current.path, synapse],
|
|
62
|
+
totalWeight: current.totalWeight * synapse.weight,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const incoming = repo.getIncoming(current.node.type, current.node.id);
|
|
68
|
+
for (const synapse of incoming) {
|
|
69
|
+
const sourceKey = `${synapse.source_type}:${synapse.source_id}`;
|
|
70
|
+
if (!visited.has(sourceKey)) {
|
|
71
|
+
queue.push({
|
|
72
|
+
node: { type: synapse.source_type, id: synapse.source_id },
|
|
73
|
+
path: [...current.path, synapse],
|
|
74
|
+
totalWeight: current.totalWeight * synapse.weight,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return bestPath;
|
|
81
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { SynapsesConfig } from '../types/config.types.js';
|
|
2
|
+
import type { NodeType, SynapseType, SynapseRecord, NetworkStats } from '../types/synapse.types.js';
|
|
3
|
+
import type { SynapseRepository } from '../db/repositories/synapse.repository.js';
|
|
4
|
+
import { strengthen, weaken, type NodeRef } from './hebbian.js';
|
|
5
|
+
import { decayAll } from './decay.js';
|
|
6
|
+
import { spreadingActivation, type ActivationResult } from './activation.js';
|
|
7
|
+
import { findPath, type SynapsePath } from './pathfinder.js';
|
|
8
|
+
import { getLogger } from '../utils/logger.js';
|
|
9
|
+
|
|
10
|
+
export class SynapseManager {
|
|
11
|
+
private logger = getLogger();
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
private repo: SynapseRepository,
|
|
15
|
+
private config: SynapsesConfig,
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
strengthen(
|
|
19
|
+
source: NodeRef,
|
|
20
|
+
target: NodeRef,
|
|
21
|
+
synapseType: SynapseType,
|
|
22
|
+
context?: Record<string, unknown>,
|
|
23
|
+
): SynapseRecord {
|
|
24
|
+
this.logger.debug(`Strengthening synapse ${source.type}:${source.id} --${synapseType}--> ${target.type}:${target.id}`);
|
|
25
|
+
return strengthen(this.repo, source, target, synapseType, {
|
|
26
|
+
initialWeight: this.config.initialWeight,
|
|
27
|
+
learningRate: this.config.learningRate,
|
|
28
|
+
pruneThreshold: this.config.pruneThreshold,
|
|
29
|
+
}, context);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
weaken(synapseId: number, factor: number = 0.5): void {
|
|
33
|
+
this.logger.debug(`Weakening synapse ${synapseId} by factor ${factor}`);
|
|
34
|
+
weaken(this.repo, synapseId, {
|
|
35
|
+
initialWeight: this.config.initialWeight,
|
|
36
|
+
learningRate: this.config.learningRate,
|
|
37
|
+
pruneThreshold: this.config.pruneThreshold,
|
|
38
|
+
}, factor);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
find(
|
|
42
|
+
source: NodeRef,
|
|
43
|
+
target: NodeRef,
|
|
44
|
+
synapseType: SynapseType,
|
|
45
|
+
): SynapseRecord | undefined {
|
|
46
|
+
return this.repo.findBySourceTarget(
|
|
47
|
+
source.type, source.id, target.type, target.id, synapseType,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
activate(
|
|
52
|
+
startNode: NodeRef,
|
|
53
|
+
maxDepth?: number,
|
|
54
|
+
minWeight?: number,
|
|
55
|
+
): ActivationResult[] {
|
|
56
|
+
return spreadingActivation(
|
|
57
|
+
this.repo,
|
|
58
|
+
startNode,
|
|
59
|
+
maxDepth ?? this.config.maxDepth,
|
|
60
|
+
minWeight ?? this.config.minActivationWeight,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
findPath(from: NodeRef, to: NodeRef, maxDepth?: number): SynapsePath | null {
|
|
65
|
+
return findPath(this.repo, from, to, maxDepth ?? this.config.maxDepth + 2);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
runDecay(): { decayed: number; pruned: number } {
|
|
69
|
+
this.logger.info('Running synapse decay cycle');
|
|
70
|
+
const result = decayAll(this.repo, {
|
|
71
|
+
decayHalfLifeDays: this.config.decayHalfLifeDays,
|
|
72
|
+
decayAfterDays: this.config.decayAfterDays,
|
|
73
|
+
pruneThreshold: this.config.pruneThreshold,
|
|
74
|
+
});
|
|
75
|
+
this.logger.info(`Decay complete: ${result.decayed} decayed, ${result.pruned} pruned`);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getPostContext(postId: number): {
|
|
80
|
+
campaigns: ActivationResult[];
|
|
81
|
+
similarPosts: ActivationResult[];
|
|
82
|
+
strategies: ActivationResult[];
|
|
83
|
+
rules: ActivationResult[];
|
|
84
|
+
templates: ActivationResult[];
|
|
85
|
+
insights: ActivationResult[];
|
|
86
|
+
} {
|
|
87
|
+
const all = this.activate({ type: 'post', id: postId });
|
|
88
|
+
return {
|
|
89
|
+
campaigns: all.filter(a => a.node.type === 'campaign'),
|
|
90
|
+
similarPosts: all.filter(a => a.node.type === 'post'),
|
|
91
|
+
strategies: all.filter(a => a.node.type === 'strategy'),
|
|
92
|
+
rules: all.filter(a => a.node.type === 'rule'),
|
|
93
|
+
templates: all.filter(a => a.node.type === 'template'),
|
|
94
|
+
insights: all.filter(a => a.node.type === 'insight'),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
getStrongestSynapses(limit: number = 20): SynapseRecord[] {
|
|
99
|
+
return this.repo.topByWeight(limit);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
getDiverseSynapses(perType: number = 25): SynapseRecord[] {
|
|
103
|
+
return this.repo.topDiverse(perType);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
getNetworkStats(): NetworkStats {
|
|
107
|
+
return {
|
|
108
|
+
totalNodes: this.repo.countNodes(),
|
|
109
|
+
totalSynapses: this.repo.totalCount(),
|
|
110
|
+
avgWeight: this.repo.avgWeight(),
|
|
111
|
+
nodesByType: {} as Record<NodeType, number>,
|
|
112
|
+
synapsesByType: this.repo.countByType() as Record<SynapseType, number>,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export interface IpcConfig {
|
|
2
|
+
pipeName: string;
|
|
3
|
+
timeout: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface LearningConfig {
|
|
7
|
+
intervalMs: number;
|
|
8
|
+
minOccurrences: number;
|
|
9
|
+
minConfidence: number;
|
|
10
|
+
pruneThreshold: number;
|
|
11
|
+
decayHalfLifeDays: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SynapsesConfig {
|
|
15
|
+
initialWeight: number;
|
|
16
|
+
learningRate: number;
|
|
17
|
+
decayHalfLifeDays: number;
|
|
18
|
+
pruneThreshold: number;
|
|
19
|
+
decayAfterDays: number;
|
|
20
|
+
maxDepth: number;
|
|
21
|
+
minActivationWeight: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ResearchConfig {
|
|
25
|
+
intervalMs: number;
|
|
26
|
+
initialDelayMs: number;
|
|
27
|
+
minDataPoints: number;
|
|
28
|
+
trendWindowDays: number;
|
|
29
|
+
insightExpiryDays: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface MatchingConfig {
|
|
33
|
+
similarityThreshold: number;
|
|
34
|
+
maxResults: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ApiConfig {
|
|
38
|
+
port: number;
|
|
39
|
+
enabled: boolean;
|
|
40
|
+
apiKey?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface McpHttpConfig {
|
|
44
|
+
port: number;
|
|
45
|
+
enabled: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface DashboardConfig {
|
|
49
|
+
port: number;
|
|
50
|
+
enabled: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface LogConfig {
|
|
54
|
+
level: string;
|
|
55
|
+
file: string;
|
|
56
|
+
maxSize: number;
|
|
57
|
+
maxFiles: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface RetentionConfig {
|
|
61
|
+
postDays: number;
|
|
62
|
+
strategyDays: number;
|
|
63
|
+
insightDays: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface MarketingBrainConfig {
|
|
67
|
+
dataDir: string;
|
|
68
|
+
dbPath: string;
|
|
69
|
+
ipc: IpcConfig;
|
|
70
|
+
api: ApiConfig;
|
|
71
|
+
mcpHttp: McpHttpConfig;
|
|
72
|
+
dashboard: DashboardConfig;
|
|
73
|
+
learning: LearningConfig;
|
|
74
|
+
matching: MatchingConfig;
|
|
75
|
+
synapses: SynapsesConfig;
|
|
76
|
+
research: ResearchConfig;
|
|
77
|
+
log: LogConfig;
|
|
78
|
+
retention: RetentionConfig;
|
|
79
|
+
}
|