@timmeck/marketing-brain 0.2.0 → 0.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.
- package/README.md +28 -13
- package/dist/cli/colors.d.ts +11 -24
- package/dist/cli/colors.js +3 -46
- package/dist/cli/colors.js.map +1 -1
- package/dist/cli/commands/dashboard.js +1 -1
- package/dist/cli/commands/peers.d.ts +2 -0
- package/dist/cli/commands/peers.js +38 -0
- package/dist/cli/commands/peers.js.map +1 -0
- package/dist/config.js +3 -3
- package/dist/db/connection.d.ts +1 -2
- package/dist/db/connection.js +1 -18
- package/dist/db/connection.js.map +1 -1
- package/dist/hooks/post-tool-use.d.ts +2 -0
- package/dist/hooks/post-tool-use.js +182 -0
- package/dist/hooks/post-tool-use.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/ipc/__tests__/protocol.test.d.ts +1 -0
- package/dist/ipc/__tests__/protocol.test.js +129 -0
- package/dist/ipc/__tests__/protocol.test.js.map +1 -0
- package/dist/ipc/client.d.ts +1 -13
- package/dist/ipc/client.js +1 -92
- package/dist/ipc/client.js.map +1 -1
- package/dist/ipc/protocol.d.ts +1 -8
- package/dist/ipc/protocol.js +1 -28
- package/dist/ipc/protocol.js.map +1 -1
- package/dist/ipc/router.js +8 -0
- package/dist/ipc/router.js.map +1 -1
- package/dist/ipc/server.d.ts +1 -14
- package/dist/ipc/server.js +1 -129
- package/dist/ipc/server.js.map +1 -1
- package/dist/marketing-core.d.ts +1 -0
- package/dist/marketing-core.js +6 -1
- package/dist/marketing-core.js.map +1 -1
- package/dist/mcp/server.js +5 -60
- package/dist/mcp/server.js.map +1 -1
- package/dist/types/ipc.types.d.ts +1 -11
- package/dist/utils/__tests__/hash.test.d.ts +1 -0
- package/dist/utils/__tests__/hash.test.js +30 -0
- package/dist/utils/__tests__/hash.test.js.map +1 -0
- package/dist/utils/__tests__/paths.test.d.ts +1 -0
- package/dist/utils/__tests__/paths.test.js +63 -0
- package/dist/utils/__tests__/paths.test.js.map +1 -0
- package/dist/utils/events.d.ts +4 -8
- package/dist/utils/events.js +2 -14
- package/dist/utils/events.js.map +1 -1
- package/dist/utils/hash.d.ts +1 -1
- package/dist/utils/hash.js +1 -4
- package/dist/utils/hash.js.map +1 -1
- package/dist/utils/logger.d.ts +3 -2
- package/dist/utils/logger.js +8 -35
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/paths.d.ts +2 -1
- package/dist/utils/paths.js +4 -13
- package/dist/utils/paths.js.map +1 -1
- package/package.json +2 -1
- package/.mcp.json +0 -9
- package/src/api/server.ts +0 -86
- package/src/cli/colors.ts +0 -59
- package/src/cli/commands/campaign.ts +0 -66
- package/src/cli/commands/config.ts +0 -168
- package/src/cli/commands/dashboard.ts +0 -165
- package/src/cli/commands/doctor.ts +0 -110
- package/src/cli/commands/export.ts +0 -40
- package/src/cli/commands/import.ts +0 -84
- package/src/cli/commands/insights.ts +0 -44
- package/src/cli/commands/learn.ts +0 -24
- package/src/cli/commands/network.ts +0 -71
- package/src/cli/commands/post.ts +0 -47
- package/src/cli/commands/query.ts +0 -108
- package/src/cli/commands/rules.ts +0 -27
- package/src/cli/commands/start.ts +0 -100
- package/src/cli/commands/status.ts +0 -73
- package/src/cli/commands/stop.ts +0 -33
- package/src/cli/commands/suggest.ts +0 -64
- package/src/cli/ipc-helper.ts +0 -22
- package/src/cli/update-check.ts +0 -63
- package/src/config.ts +0 -110
- package/src/dashboard/renderer.ts +0 -136
- package/src/dashboard/server.ts +0 -140
- package/src/db/connection.ts +0 -22
- package/src/db/migrations/001_core_schema.ts +0 -63
- package/src/db/migrations/002_learning_schema.ts +0 -46
- package/src/db/migrations/003_synapse_schema.ts +0 -27
- package/src/db/migrations/004_insights_schema.ts +0 -38
- package/src/db/migrations/005_fts_indexes.ts +0 -77
- package/src/db/migrations/index.ts +0 -62
- package/src/db/repositories/audience.repository.ts +0 -53
- package/src/db/repositories/campaign.repository.ts +0 -72
- package/src/db/repositories/engagement.repository.ts +0 -108
- package/src/db/repositories/insight.repository.ts +0 -100
- package/src/db/repositories/post.repository.ts +0 -123
- package/src/db/repositories/rule.repository.ts +0 -87
- package/src/db/repositories/strategy.repository.ts +0 -82
- package/src/db/repositories/synapse.repository.ts +0 -148
- package/src/db/repositories/template.repository.ts +0 -76
- package/src/index.ts +0 -69
- package/src/ipc/client.ts +0 -110
- package/src/ipc/protocol.ts +0 -35
- package/src/ipc/router.ts +0 -126
- package/src/ipc/server.ts +0 -140
- package/src/learning/confidence-scorer.ts +0 -36
- package/src/learning/learning-engine.ts +0 -254
- package/src/marketing-core.ts +0 -285
- package/src/mcp/server.ts +0 -72
- package/src/mcp/tools.ts +0 -216
- package/src/research/research-engine.ts +0 -226
- package/src/services/analytics.service.ts +0 -73
- package/src/services/audience.service.ts +0 -40
- package/src/services/campaign.service.ts +0 -80
- package/src/services/insight.service.ts +0 -54
- package/src/services/post.service.ts +0 -116
- package/src/services/rule.service.ts +0 -90
- package/src/services/strategy.service.ts +0 -53
- package/src/services/synapse.service.ts +0 -32
- package/src/services/template.service.ts +0 -50
- package/src/synapses/activation.ts +0 -80
- package/src/synapses/decay.ts +0 -38
- package/src/synapses/hebbian.ts +0 -68
- package/src/synapses/pathfinder.ts +0 -81
- package/src/synapses/synapse-manager.ts +0 -115
- package/src/types/config.types.ts +0 -79
- package/src/types/ipc.types.ts +0 -8
- package/src/types/post.types.ts +0 -156
- package/src/types/synapse.types.ts +0 -43
- package/src/utils/events.ts +0 -44
- package/src/utils/hash.ts +0 -5
- package/src/utils/logger.ts +0 -48
- package/src/utils/paths.ts +0 -19
- package/tsconfig.json +0 -18
package/src/ipc/server.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import net from 'node:net';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import { randomUUID } from 'node:crypto';
|
|
4
|
-
import { getLogger } from '../utils/logger.js';
|
|
5
|
-
|
|
6
|
-
const logger = getLogger();
|
|
7
|
-
import type { IpcMessage } from '../types/ipc.types.js';
|
|
8
|
-
import { encodeMessage, MessageDecoder } from './protocol.js';
|
|
9
|
-
import type { IpcRouter } from './router.js';
|
|
10
|
-
|
|
11
|
-
export class IpcServer {
|
|
12
|
-
private server: net.Server | null = null;
|
|
13
|
-
private clients = new Map<string, net.Socket>();
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
private router: IpcRouter,
|
|
17
|
-
private pipeName: string,
|
|
18
|
-
) {}
|
|
19
|
-
|
|
20
|
-
start(): void {
|
|
21
|
-
this.createServer();
|
|
22
|
-
this.listen();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
private createServer(): void {
|
|
26
|
-
this.server = net.createServer((socket) => {
|
|
27
|
-
const clientId = randomUUID();
|
|
28
|
-
this.clients.set(clientId, socket);
|
|
29
|
-
const decoder = new MessageDecoder();
|
|
30
|
-
|
|
31
|
-
logger.info(`IPC client connected: ${clientId}`);
|
|
32
|
-
|
|
33
|
-
socket.on('data', (chunk) => {
|
|
34
|
-
const messages = decoder.feed(chunk);
|
|
35
|
-
for (const msg of messages) {
|
|
36
|
-
this.handleMessage(clientId, msg, socket);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
socket.on('close', () => {
|
|
41
|
-
logger.info(`IPC client disconnected: ${clientId}`);
|
|
42
|
-
this.clients.delete(clientId);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
socket.on('error', (err) => {
|
|
46
|
-
logger.error(`IPC client ${clientId} error:`, err);
|
|
47
|
-
this.clients.delete(clientId);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private listen(retried = false): void {
|
|
53
|
-
if (!this.server) return;
|
|
54
|
-
|
|
55
|
-
this.server.on('error', (err: NodeJS.ErrnoException) => {
|
|
56
|
-
if (err.code === 'EADDRINUSE' && !retried) {
|
|
57
|
-
logger.warn(`IPC pipe in use, attempting to recover: ${this.pipeName}`);
|
|
58
|
-
this.recoverStalePipe();
|
|
59
|
-
} else {
|
|
60
|
-
logger.error('IPC server error:', err);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
this.server.listen(this.pipeName, () => {
|
|
65
|
-
logger.info(`IPC server listening on ${this.pipeName}`);
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
private recoverStalePipe(): void {
|
|
70
|
-
const probe = net.createConnection(this.pipeName);
|
|
71
|
-
|
|
72
|
-
probe.on('connect', () => {
|
|
73
|
-
probe.destroy();
|
|
74
|
-
logger.error('IPC pipe is held by another running daemon. Stop it first with: marketing stop');
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
probe.on('error', () => {
|
|
78
|
-
probe.destroy();
|
|
79
|
-
logger.info('Stale IPC pipe detected, reclaiming...');
|
|
80
|
-
|
|
81
|
-
if (process.platform !== 'win32') {
|
|
82
|
-
try { fs.unlinkSync(this.pipeName); } catch { /* ignore */ }
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this.createServer();
|
|
86
|
-
this.server!.on('error', (err) => {
|
|
87
|
-
logger.error('IPC server error after recovery:', err);
|
|
88
|
-
});
|
|
89
|
-
this.server!.listen(this.pipeName, () => {
|
|
90
|
-
logger.info(`IPC server recovered and listening on ${this.pipeName}`);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
probe.setTimeout(2000, () => {
|
|
95
|
-
probe.destroy();
|
|
96
|
-
logger.warn('IPC pipe probe timed out, treating as stale');
|
|
97
|
-
if (process.platform !== 'win32') {
|
|
98
|
-
try { fs.unlinkSync(this.pipeName); } catch { /* ignore */ }
|
|
99
|
-
}
|
|
100
|
-
this.createServer();
|
|
101
|
-
this.server!.on('error', (err) => {
|
|
102
|
-
logger.error('IPC server error after timeout recovery:', err);
|
|
103
|
-
});
|
|
104
|
-
this.server!.listen(this.pipeName, () => {
|
|
105
|
-
logger.info(`IPC server recovered (timeout) and listening on ${this.pipeName}`);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
private handleMessage(clientId: string, msg: IpcMessage, socket: net.Socket): void {
|
|
111
|
-
if (msg.type !== 'request' || !msg.method) return;
|
|
112
|
-
|
|
113
|
-
try {
|
|
114
|
-
const result = this.router.handle(msg.method, msg.params);
|
|
115
|
-
const response: IpcMessage = {
|
|
116
|
-
id: msg.id,
|
|
117
|
-
type: 'response',
|
|
118
|
-
result,
|
|
119
|
-
};
|
|
120
|
-
socket.write(encodeMessage(response));
|
|
121
|
-
} catch (err) {
|
|
122
|
-
const response: IpcMessage = {
|
|
123
|
-
id: msg.id,
|
|
124
|
-
type: 'response',
|
|
125
|
-
error: { code: -1, message: err instanceof Error ? err.message : String(err) },
|
|
126
|
-
};
|
|
127
|
-
socket.write(encodeMessage(response));
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
stop(): void {
|
|
132
|
-
for (const socket of this.clients.values()) {
|
|
133
|
-
socket.destroy();
|
|
134
|
-
}
|
|
135
|
-
this.clients.clear();
|
|
136
|
-
this.server?.close();
|
|
137
|
-
this.server = null;
|
|
138
|
-
logger.info('IPC server stopped');
|
|
139
|
-
}
|
|
140
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Wilson Score Interval — lower bound for confidence scoring.
|
|
3
|
-
* Used to evaluate rule confidence based on trigger/success counts.
|
|
4
|
-
*/
|
|
5
|
-
export function wilsonScore(successes: number, total: number, z: number = 1.96): number {
|
|
6
|
-
if (total === 0) return 0;
|
|
7
|
-
|
|
8
|
-
const p = successes / total;
|
|
9
|
-
const denominator = 1 + z * z / total;
|
|
10
|
-
const centre = p + z * z / (2 * total);
|
|
11
|
-
const offset = z * Math.sqrt((p * (1 - p) + z * z / (4 * total)) / total);
|
|
12
|
-
|
|
13
|
-
return (centre - offset) / denominator;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Compute engagement score from raw metrics.
|
|
18
|
-
* Weights: shares > comments > clicks > likes > impressions
|
|
19
|
-
*/
|
|
20
|
-
export function engagementScore(metrics: {
|
|
21
|
-
likes?: number;
|
|
22
|
-
comments?: number;
|
|
23
|
-
shares?: number;
|
|
24
|
-
impressions?: number;
|
|
25
|
-
clicks?: number;
|
|
26
|
-
saves?: number;
|
|
27
|
-
}): number {
|
|
28
|
-
return (
|
|
29
|
-
(metrics.likes ?? 0) * 1 +
|
|
30
|
-
(metrics.comments ?? 0) * 3 +
|
|
31
|
-
(metrics.shares ?? 0) * 5 +
|
|
32
|
-
(metrics.clicks ?? 0) * 2 +
|
|
33
|
-
(metrics.saves ?? 0) * 4 +
|
|
34
|
-
(metrics.impressions ?? 0) * 0.01
|
|
35
|
-
);
|
|
36
|
-
}
|
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
import type { LearningConfig } from '../types/config.types.js';
|
|
2
|
-
import type { PostRepository } from '../db/repositories/post.repository.js';
|
|
3
|
-
import type { EngagementRepository } from '../db/repositories/engagement.repository.js';
|
|
4
|
-
import type { RuleRepository } from '../db/repositories/rule.repository.js';
|
|
5
|
-
import type { StrategyRepository } from '../db/repositories/strategy.repository.js';
|
|
6
|
-
import type { SynapseManager } from '../synapses/synapse-manager.js';
|
|
7
|
-
import { wilsonScore, engagementScore } from './confidence-scorer.js';
|
|
8
|
-
import { getLogger } from '../utils/logger.js';
|
|
9
|
-
|
|
10
|
-
export interface LearningCycleResult {
|
|
11
|
-
rulesCreated: number;
|
|
12
|
-
rulesUpdated: number;
|
|
13
|
-
strategiesUpdated: number;
|
|
14
|
-
synapsesDecayed: number;
|
|
15
|
-
synapsesPruned: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export class LearningEngine {
|
|
19
|
-
private timer: ReturnType<typeof setInterval> | null = null;
|
|
20
|
-
private logger = getLogger();
|
|
21
|
-
|
|
22
|
-
constructor(
|
|
23
|
-
private config: LearningConfig,
|
|
24
|
-
private postRepo: PostRepository,
|
|
25
|
-
private engagementRepo: EngagementRepository,
|
|
26
|
-
private ruleRepo: RuleRepository,
|
|
27
|
-
private strategyRepo: StrategyRepository,
|
|
28
|
-
private synapseManager: SynapseManager,
|
|
29
|
-
) {}
|
|
30
|
-
|
|
31
|
-
start(): void {
|
|
32
|
-
this.timer = setInterval(() => {
|
|
33
|
-
try {
|
|
34
|
-
this.runCycle();
|
|
35
|
-
} catch (err) {
|
|
36
|
-
this.logger.error('Learning cycle error:', err);
|
|
37
|
-
}
|
|
38
|
-
}, this.config.intervalMs);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
stop(): void {
|
|
42
|
-
if (this.timer) {
|
|
43
|
-
clearInterval(this.timer);
|
|
44
|
-
this.timer = null;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
runCycle(): LearningCycleResult {
|
|
49
|
-
this.logger.info('Starting learning cycle');
|
|
50
|
-
const result: LearningCycleResult = {
|
|
51
|
-
rulesCreated: 0,
|
|
52
|
-
rulesUpdated: 0,
|
|
53
|
-
strategiesUpdated: 0,
|
|
54
|
-
synapsesDecayed: 0,
|
|
55
|
-
synapsesPruned: 0,
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// 1. Analyze recent posts for patterns
|
|
59
|
-
result.rulesCreated += this.extractTimingPatterns();
|
|
60
|
-
result.rulesCreated += this.extractFormatPatterns();
|
|
61
|
-
result.rulesCreated += this.extractPlatformPatterns();
|
|
62
|
-
|
|
63
|
-
// 2. Update strategy confidence based on engagement
|
|
64
|
-
result.strategiesUpdated = this.updateStrategyConfidence();
|
|
65
|
-
|
|
66
|
-
// 3. Update rule confidence via Wilson Score
|
|
67
|
-
result.rulesUpdated = this.updateRuleConfidence();
|
|
68
|
-
|
|
69
|
-
// 4. Run synapse decay
|
|
70
|
-
const decay = this.synapseManager.runDecay();
|
|
71
|
-
result.synapsesDecayed = decay.decayed;
|
|
72
|
-
result.synapsesPruned = decay.pruned;
|
|
73
|
-
|
|
74
|
-
// 5. Wire similar posts
|
|
75
|
-
this.wireSimilarPosts();
|
|
76
|
-
|
|
77
|
-
this.logger.info(`Learning cycle complete: ${JSON.stringify(result)}`);
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private extractTimingPatterns(): number {
|
|
82
|
-
let created = 0;
|
|
83
|
-
const recentPosts = this.postRepo.listPublished(100);
|
|
84
|
-
|
|
85
|
-
// Group by hour of day
|
|
86
|
-
const hourBuckets: Record<number, { total: number; avgScore: number }> = {};
|
|
87
|
-
|
|
88
|
-
for (const post of recentPosts) {
|
|
89
|
-
if (!post.published_at) continue;
|
|
90
|
-
const hour = new Date(post.published_at).getHours();
|
|
91
|
-
const eng = this.engagementRepo.getLatestByPost(post.id);
|
|
92
|
-
if (!eng) continue;
|
|
93
|
-
|
|
94
|
-
const score = engagementScore(eng);
|
|
95
|
-
if (!hourBuckets[hour]) hourBuckets[hour] = { total: 0, avgScore: 0 };
|
|
96
|
-
hourBuckets[hour].total++;
|
|
97
|
-
hourBuckets[hour].avgScore += score;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Find best/worst hours
|
|
101
|
-
for (const [hour, data] of Object.entries(hourBuckets)) {
|
|
102
|
-
if (data.total < this.config.minOccurrences) continue;
|
|
103
|
-
data.avgScore /= data.total;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const hours = Object.entries(hourBuckets)
|
|
107
|
-
.filter(([, d]) => d.total >= this.config.minOccurrences)
|
|
108
|
-
.sort(([, a], [, b]) => b.avgScore - a.avgScore);
|
|
109
|
-
|
|
110
|
-
if (hours.length >= 2) {
|
|
111
|
-
const bestHour = hours[0];
|
|
112
|
-
const worstHour = hours[hours.length - 1];
|
|
113
|
-
|
|
114
|
-
if (bestHour && worstHour && bestHour[1].avgScore > worstHour[1].avgScore * 2) {
|
|
115
|
-
this.ruleRepo.create({
|
|
116
|
-
pattern: `best_time_${bestHour[0]}h`,
|
|
117
|
-
recommendation: `Posts around ${bestHour[0]}:00 perform ${(bestHour[1].avgScore / Math.max(1, worstHour[1].avgScore)).toFixed(1)}x better than ${worstHour[0]}:00`,
|
|
118
|
-
confidence: wilsonScore(bestHour[1].total, recentPosts.length),
|
|
119
|
-
});
|
|
120
|
-
created++;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return created;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
private extractFormatPatterns(): number {
|
|
128
|
-
let created = 0;
|
|
129
|
-
const recentPosts = this.postRepo.listPublished(100);
|
|
130
|
-
|
|
131
|
-
const formatBuckets: Record<string, { total: number; avgScore: number }> = {};
|
|
132
|
-
|
|
133
|
-
for (const post of recentPosts) {
|
|
134
|
-
const eng = this.engagementRepo.getLatestByPost(post.id);
|
|
135
|
-
if (!eng) continue;
|
|
136
|
-
|
|
137
|
-
const score = engagementScore(eng);
|
|
138
|
-
if (!formatBuckets[post.format]) formatBuckets[post.format] = { total: 0, avgScore: 0 };
|
|
139
|
-
formatBuckets[post.format].total++;
|
|
140
|
-
formatBuckets[post.format].avgScore += score;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
for (const [format, data] of Object.entries(formatBuckets)) {
|
|
144
|
-
if (data.total < this.config.minOccurrences) continue;
|
|
145
|
-
data.avgScore /= data.total;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const formats = Object.entries(formatBuckets)
|
|
149
|
-
.filter(([, d]) => d.total >= this.config.minOccurrences)
|
|
150
|
-
.sort(([, a], [, b]) => b.avgScore - a.avgScore);
|
|
151
|
-
|
|
152
|
-
if (formats.length >= 2 && formats[0]) {
|
|
153
|
-
const [bestFormat, bestData] = formats[0];
|
|
154
|
-
this.ruleRepo.create({
|
|
155
|
-
pattern: `best_format_${bestFormat}`,
|
|
156
|
-
recommendation: `${bestFormat} posts average ${bestData.avgScore.toFixed(0)} engagement score (best format)`,
|
|
157
|
-
confidence: wilsonScore(bestData.total, recentPosts.length),
|
|
158
|
-
});
|
|
159
|
-
created++;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return created;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
private extractPlatformPatterns(): number {
|
|
166
|
-
let created = 0;
|
|
167
|
-
const platformStats = this.engagementRepo.avgByPlatform();
|
|
168
|
-
|
|
169
|
-
if (platformStats.length >= 2) {
|
|
170
|
-
const toScore = (p: typeof platformStats[number]) => engagementScore({
|
|
171
|
-
likes: p.avg_likes, comments: p.avg_comments,
|
|
172
|
-
shares: p.avg_shares, impressions: p.avg_impressions, clicks: p.avg_clicks,
|
|
173
|
-
});
|
|
174
|
-
const sorted = [...platformStats].sort((a, b) => toScore(b) - toScore(a));
|
|
175
|
-
const best = sorted[0];
|
|
176
|
-
if (best && best.post_count >= this.config.minOccurrences) {
|
|
177
|
-
this.ruleRepo.create({
|
|
178
|
-
pattern: `best_platform_${best.platform}`,
|
|
179
|
-
recommendation: `${best.platform} is your top-performing platform (${best.post_count} posts, avg ${best.avg_likes.toFixed(0)} likes)`,
|
|
180
|
-
confidence: wilsonScore(best.post_count, platformStats.reduce((s, p) => s + p.post_count, 0)),
|
|
181
|
-
});
|
|
182
|
-
created++;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return created;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
private updateStrategyConfidence(): number {
|
|
190
|
-
let updated = 0;
|
|
191
|
-
const strategies = this.strategyRepo.listAll(100);
|
|
192
|
-
|
|
193
|
-
for (const strategy of strategies) {
|
|
194
|
-
if (!strategy.post_id) continue;
|
|
195
|
-
const eng = this.engagementRepo.getLatestByPost(strategy.post_id);
|
|
196
|
-
if (!eng) continue;
|
|
197
|
-
|
|
198
|
-
const score = engagementScore(eng);
|
|
199
|
-
// Normalize: 0-10 → 0.0-1.0 confidence
|
|
200
|
-
const newConfidence = Math.min(1.0, score / 100);
|
|
201
|
-
|
|
202
|
-
if (Math.abs(newConfidence - strategy.confidence) > 0.05) {
|
|
203
|
-
this.strategyRepo.update(strategy.id, { confidence: newConfidence });
|
|
204
|
-
updated++;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return updated;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
private updateRuleConfidence(): number {
|
|
212
|
-
let updated = 0;
|
|
213
|
-
const rules = this.ruleRepo.listAll();
|
|
214
|
-
|
|
215
|
-
for (const rule of rules) {
|
|
216
|
-
if (rule.trigger_count < this.config.minOccurrences) continue;
|
|
217
|
-
|
|
218
|
-
const newConfidence = wilsonScore(rule.success_count, rule.trigger_count);
|
|
219
|
-
|
|
220
|
-
if (newConfidence < this.config.pruneThreshold && rule.active) {
|
|
221
|
-
this.ruleRepo.update(rule.id, { active: 0, confidence: newConfidence });
|
|
222
|
-
updated++;
|
|
223
|
-
} else if (Math.abs(newConfidence - rule.confidence) > 0.05) {
|
|
224
|
-
this.ruleRepo.update(rule.id, { confidence: newConfidence });
|
|
225
|
-
updated++;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return updated;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
private wireSimilarPosts(): void {
|
|
233
|
-
const posts = this.postRepo.listPublished(50);
|
|
234
|
-
|
|
235
|
-
for (let i = 0; i < posts.length; i++) {
|
|
236
|
-
for (let j = i + 1; j < Math.min(i + 5, posts.length); j++) {
|
|
237
|
-
const a = posts[i]!;
|
|
238
|
-
const b = posts[j]!;
|
|
239
|
-
|
|
240
|
-
// Simple similarity: same platform, similar content length, similar hashtags
|
|
241
|
-
if (a.platform === b.platform) {
|
|
242
|
-
const lengthRatio = Math.min(a.content.length, b.content.length) / Math.max(a.content.length, b.content.length);
|
|
243
|
-
if (lengthRatio > 0.5) {
|
|
244
|
-
this.synapseManager.strengthen(
|
|
245
|
-
{ type: 'post', id: a.id },
|
|
246
|
-
{ type: 'post', id: b.id },
|
|
247
|
-
'similar_to',
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|