@timmeck/marketing-brain 0.2.1 → 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.
Files changed (123) hide show
  1. package/dist/cli/colors.d.ts +11 -24
  2. package/dist/cli/colors.js +3 -46
  3. package/dist/cli/colors.js.map +1 -1
  4. package/dist/cli/commands/peers.d.ts +2 -0
  5. package/dist/cli/commands/peers.js +38 -0
  6. package/dist/cli/commands/peers.js.map +1 -0
  7. package/dist/db/connection.d.ts +1 -2
  8. package/dist/db/connection.js +1 -18
  9. package/dist/db/connection.js.map +1 -1
  10. package/dist/hooks/post-tool-use.d.ts +2 -0
  11. package/dist/hooks/post-tool-use.js +182 -0
  12. package/dist/hooks/post-tool-use.js.map +1 -0
  13. package/dist/index.js +2 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/ipc/client.d.ts +1 -13
  16. package/dist/ipc/client.js +1 -92
  17. package/dist/ipc/client.js.map +1 -1
  18. package/dist/ipc/protocol.d.ts +1 -8
  19. package/dist/ipc/protocol.js +1 -28
  20. package/dist/ipc/protocol.js.map +1 -1
  21. package/dist/ipc/router.js +8 -0
  22. package/dist/ipc/router.js.map +1 -1
  23. package/dist/ipc/server.d.ts +1 -14
  24. package/dist/ipc/server.js +1 -129
  25. package/dist/ipc/server.js.map +1 -1
  26. package/dist/marketing-core.d.ts +1 -0
  27. package/dist/marketing-core.js +6 -1
  28. package/dist/marketing-core.js.map +1 -1
  29. package/dist/mcp/server.js +5 -60
  30. package/dist/mcp/server.js.map +1 -1
  31. package/dist/types/ipc.types.d.ts +1 -11
  32. package/dist/utils/events.d.ts +4 -8
  33. package/dist/utils/events.js +2 -14
  34. package/dist/utils/events.js.map +1 -1
  35. package/dist/utils/hash.d.ts +1 -1
  36. package/dist/utils/hash.js +1 -4
  37. package/dist/utils/hash.js.map +1 -1
  38. package/dist/utils/logger.d.ts +3 -2
  39. package/dist/utils/logger.js +8 -35
  40. package/dist/utils/logger.js.map +1 -1
  41. package/dist/utils/paths.d.ts +2 -1
  42. package/dist/utils/paths.js +4 -13
  43. package/dist/utils/paths.js.map +1 -1
  44. package/package.json +2 -1
  45. package/.github/FUNDING.yml +0 -1
  46. package/.github/workflows/ci.yml +0 -27
  47. package/.mcp.json +0 -9
  48. package/src/api/server.ts +0 -86
  49. package/src/cli/colors.ts +0 -59
  50. package/src/cli/commands/campaign.ts +0 -66
  51. package/src/cli/commands/config.ts +0 -168
  52. package/src/cli/commands/dashboard.ts +0 -165
  53. package/src/cli/commands/doctor.ts +0 -110
  54. package/src/cli/commands/export.ts +0 -40
  55. package/src/cli/commands/import.ts +0 -84
  56. package/src/cli/commands/insights.ts +0 -44
  57. package/src/cli/commands/learn.ts +0 -24
  58. package/src/cli/commands/network.ts +0 -71
  59. package/src/cli/commands/post.ts +0 -47
  60. package/src/cli/commands/query.ts +0 -108
  61. package/src/cli/commands/rules.ts +0 -27
  62. package/src/cli/commands/start.ts +0 -100
  63. package/src/cli/commands/status.ts +0 -73
  64. package/src/cli/commands/stop.ts +0 -33
  65. package/src/cli/commands/suggest.ts +0 -64
  66. package/src/cli/ipc-helper.ts +0 -22
  67. package/src/cli/update-check.ts +0 -63
  68. package/src/config.ts +0 -110
  69. package/src/dashboard/renderer.ts +0 -136
  70. package/src/dashboard/server.ts +0 -140
  71. package/src/db/connection.ts +0 -22
  72. package/src/db/migrations/001_core_schema.ts +0 -63
  73. package/src/db/migrations/002_learning_schema.ts +0 -46
  74. package/src/db/migrations/003_synapse_schema.ts +0 -27
  75. package/src/db/migrations/004_insights_schema.ts +0 -38
  76. package/src/db/migrations/005_fts_indexes.ts +0 -77
  77. package/src/db/migrations/index.ts +0 -62
  78. package/src/db/repositories/audience.repository.ts +0 -53
  79. package/src/db/repositories/campaign.repository.ts +0 -72
  80. package/src/db/repositories/engagement.repository.ts +0 -108
  81. package/src/db/repositories/insight.repository.ts +0 -100
  82. package/src/db/repositories/post.repository.ts +0 -123
  83. package/src/db/repositories/rule.repository.ts +0 -87
  84. package/src/db/repositories/strategy.repository.ts +0 -82
  85. package/src/db/repositories/synapse.repository.ts +0 -148
  86. package/src/db/repositories/template.repository.ts +0 -76
  87. package/src/index.ts +0 -69
  88. package/src/ipc/__tests__/protocol.test.ts +0 -153
  89. package/src/ipc/client.ts +0 -110
  90. package/src/ipc/protocol.ts +0 -35
  91. package/src/ipc/router.ts +0 -126
  92. package/src/ipc/server.ts +0 -140
  93. package/src/learning/confidence-scorer.ts +0 -36
  94. package/src/learning/learning-engine.ts +0 -254
  95. package/src/marketing-core.ts +0 -285
  96. package/src/mcp/server.ts +0 -72
  97. package/src/mcp/tools.ts +0 -216
  98. package/src/research/research-engine.ts +0 -226
  99. package/src/services/analytics.service.ts +0 -73
  100. package/src/services/audience.service.ts +0 -40
  101. package/src/services/campaign.service.ts +0 -80
  102. package/src/services/insight.service.ts +0 -54
  103. package/src/services/post.service.ts +0 -116
  104. package/src/services/rule.service.ts +0 -90
  105. package/src/services/strategy.service.ts +0 -53
  106. package/src/services/synapse.service.ts +0 -32
  107. package/src/services/template.service.ts +0 -50
  108. package/src/synapses/activation.ts +0 -80
  109. package/src/synapses/decay.ts +0 -38
  110. package/src/synapses/hebbian.ts +0 -68
  111. package/src/synapses/pathfinder.ts +0 -81
  112. package/src/synapses/synapse-manager.ts +0 -115
  113. package/src/types/config.types.ts +0 -79
  114. package/src/types/ipc.types.ts +0 -8
  115. package/src/types/post.types.ts +0 -156
  116. package/src/types/synapse.types.ts +0 -43
  117. package/src/utils/__tests__/hash.test.ts +0 -39
  118. package/src/utils/__tests__/paths.test.ts +0 -70
  119. package/src/utils/events.ts +0 -44
  120. package/src/utils/hash.ts +0 -5
  121. package/src/utils/logger.ts +0 -48
  122. package/src/utils/paths.ts +0 -19
  123. package/tsconfig.json +0 -18
@@ -1,40 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons } from '../colors.js';
4
-
5
- export function exportCommand(): Command {
6
- return new Command('export')
7
- .description('Export Marketing Brain data')
8
- .option('--format <fmt>', 'Output format: json (default)', 'json')
9
- .action(async () => {
10
- await withIpc(async (client) => {
11
- process.stderr.write(`${icons.chart} ${c.info('Exporting Marketing Brain data...')}\n`);
12
-
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- const summary: any = await client.request('analytics.summary', {});
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- const top: any = await client.request('analytics.top', { limit: 50 });
17
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
- const insights: any = await client.request('insight.list', { limit: 100 });
19
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
- const rules: any = await client.request('rule.list', {});
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- const strategies: any = await client.request('strategy.list', { limit: 100 });
23
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
- const network: any = await client.request('synapse.stats', {});
25
-
26
- const data = {
27
- exportedAt: new Date().toISOString(),
28
- summary,
29
- topPerformers: top,
30
- insights,
31
- rules,
32
- strategies,
33
- network,
34
- };
35
-
36
- console.log(JSON.stringify(data, null, 2));
37
- process.stderr.write(`${icons.ok} ${c.success('Export complete.')}\n`);
38
- });
39
- });
40
- }
@@ -1,84 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons } from '../colors.js';
4
-
5
- export function importCommand(): Command {
6
- return new Command('import')
7
- .description('Import posts from a JSON file')
8
- .argument('<file>', 'JSON file with posts array')
9
- .action(async (file) => {
10
- const fs = await import('node:fs');
11
- const path = await import('node:path');
12
-
13
- const filePath = path.default.resolve(file);
14
- if (!fs.default.existsSync(filePath)) {
15
- console.error(`${icons.error} ${c.error(`File not found: ${filePath}`)}`);
16
- process.exit(1);
17
- }
18
-
19
- const data = JSON.parse(fs.default.readFileSync(filePath, 'utf-8'));
20
- const posts = Array.isArray(data) ? data : data.posts;
21
-
22
- if (!Array.isArray(posts)) {
23
- console.error(`${icons.error} ${c.error('Expected JSON array or { posts: [...] }')}`);
24
- process.exit(1);
25
- }
26
-
27
- await withIpc(async (client) => {
28
- let imported = 0;
29
- let skipped = 0;
30
-
31
- for (const post of posts) {
32
- try {
33
- // Create campaign if specified
34
- let campaignId: number | null = null;
35
- if (post.campaign) {
36
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
- const camp: any = await client.request('campaign.create', { name: post.campaign, brand: post.brand });
38
- campaignId = camp.id;
39
- }
40
-
41
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
- const result: any = await client.request('post.report', {
43
- platform: post.platform,
44
- content: post.content,
45
- format: post.format ?? 'text',
46
- url: post.url ?? null,
47
- hashtags: post.hashtags ?? null,
48
- campaign_id: campaignId,
49
- status: post.status ?? 'published',
50
- published_at: post.published_at ?? null,
51
- });
52
-
53
- if (result.isNew) {
54
- imported++;
55
-
56
- // Import engagement if provided
57
- if (post.engagement) {
58
- await client.request('post.engagement', {
59
- post_id: result.post.id,
60
- ...post.engagement,
61
- });
62
- }
63
-
64
- // Import strategy if provided
65
- if (post.strategy) {
66
- await client.request('strategy.report', {
67
- post_id: result.post.id,
68
- description: post.strategy.description,
69
- approach: post.strategy.approach,
70
- outcome: post.strategy.outcome,
71
- });
72
- }
73
- } else {
74
- skipped++;
75
- }
76
- } catch (err) {
77
- console.error(`${icons.error} ${c.error(`Failed: ${err instanceof Error ? err.message : err}`)}`);
78
- }
79
- }
80
-
81
- console.log(`${icons.ok} ${c.success(`Import complete: ${imported} imported, ${skipped} skipped (duplicates)`)}`);
82
- });
83
- });
84
- }
@@ -1,44 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons } from '../colors.js';
4
-
5
- export function insightsCommand(): Command {
6
- return new Command('insights')
7
- .description('Show current marketing insights')
8
- .option('-t, --type <type>', 'Filter by type (trend, gap, synergy, template, optimization)')
9
- .option('-l, --limit <n>', 'Max results', '10')
10
- .action(async (opts) => {
11
- await withIpc(async (client) => {
12
- let insights: unknown[];
13
- if (opts.type) {
14
- insights = await client.request('insight.byType', { type: opts.type, limit: Number(opts.limit) }) as unknown[];
15
- } else {
16
- insights = await client.request('insight.list', { limit: Number(opts.limit) }) as unknown[];
17
- }
18
-
19
- if ((insights as unknown[]).length === 0) {
20
- console.log(`${c.dim('No active insights. Start tracking posts to generate insights!')}`);
21
- return;
22
- }
23
-
24
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
- for (const insight of insights as any[]) {
26
- const typeIcon = insight.type === 'trend' ? '📈'
27
- : insight.type === 'gap' ? '🕳️'
28
- : insight.type === 'synergy' ? '🔄'
29
- : insight.type === 'template' ? icons.template
30
- : insight.type === 'optimization' ? '⚡'
31
- : icons.insight;
32
-
33
- const priority = insight.priority >= 7 ? c.red(`[P${insight.priority}]`)
34
- : insight.priority >= 4 ? c.orange(`[P${insight.priority}]`)
35
- : c.dim(`[P${insight.priority}]`);
36
-
37
- console.log(` ${typeIcon} ${priority} ${c.value(insight.title)}`);
38
- console.log(` ${c.dim(insight.description)}`);
39
- console.log(` ${c.dim(`confidence: ${(insight.confidence * 100).toFixed(0)}%`)}`);
40
- console.log();
41
- }
42
- });
43
- });
44
- }
@@ -1,24 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons, header, keyValue, divider } from '../colors.js';
4
-
5
- export function learnCommand(): Command {
6
- return new Command('learn')
7
- .description('Trigger a learning cycle manually (pattern extraction + rule generation)')
8
- .action(async () => {
9
- await withIpc(async (client) => {
10
- console.log(`${icons.bolt} ${c.info('Running learning cycle...')}`);
11
-
12
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
- const result: any = await client.request('learning.run', {});
14
-
15
- console.log(header('Learning Cycle Complete', icons.bolt));
16
- console.log(keyValue('Rules created', result.rulesCreated ?? 0));
17
- console.log(keyValue('Rules updated', result.rulesUpdated ?? 0));
18
- console.log(keyValue('Strategies updated', result.strategiesUpdated ?? 0));
19
- console.log(keyValue('Synapses decayed', result.synapsesDecayed ?? 0));
20
- console.log(keyValue('Synapses pruned', result.synapsesPruned ?? 0));
21
- console.log(`\n${divider()}`);
22
- });
23
- });
24
- }
@@ -1,71 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons, header, keyValue, divider } from '../colors.js';
4
-
5
- export function networkCommand(): Command {
6
- return new Command('network')
7
- .description('Explore the synapse network')
8
- .option('--node <type:id>', 'Node to explore (e.g., post:42)')
9
- .option('-l, --limit <n>', 'Max synapses to show', '20')
10
- .action(async (opts) => {
11
- await withIpc(async (client) => {
12
- if (opts.node) {
13
- const [nodeType, nodeIdStr] = opts.node.split(':');
14
- const nodeId = parseInt(nodeIdStr, 10);
15
-
16
- if (!nodeType || isNaN(nodeId)) {
17
- console.error(c.error('Invalid node format. Use: --node post:42'));
18
- return;
19
- }
20
-
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- const related: any = await client.request('synapse.related', {
23
- nodeType,
24
- nodeId,
25
- maxDepth: 2,
26
- });
27
-
28
- if (!related?.length) {
29
- console.log(`${c.dim('No connections found for')} ${c.cyan(`${nodeType}:${nodeId}`)}`);
30
- return;
31
- }
32
-
33
- console.log(header(`Connections from ${nodeType}:${nodeId}`, icons.synapse));
34
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
- for (const r of related as any[]) {
36
- const weight = (r.activation ?? r.weight ?? 0);
37
- const weightColor = weight >= 0.7 ? c.green : weight >= 0.3 ? c.orange : c.dim;
38
- const nodeType = r.node?.type ?? r.nodeType ?? '?';
39
- const nodeId = r.node?.id ?? r.nodeId ?? '?';
40
- console.log(` ${c.cyan(icons.arrow)} ${c.value(`${nodeType}:${nodeId}`)} ${c.label('weight:')} ${weightColor(weight.toFixed(3))}`);
41
- }
42
- } else {
43
- // Show general network stats
44
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
- const stats: any = await client.request('synapse.stats', {});
46
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
- const strongest: any = await client.request('synapse.strongest', {
48
- limit: parseInt(opts.limit, 10),
49
- });
50
-
51
- console.log(header('Synapse Network', icons.synapse));
52
- console.log(keyValue('Total synapses', stats.totalSynapses ?? 0));
53
- console.log(keyValue('Average weight', (stats.avgWeight ?? 0).toFixed(3)));
54
- console.log();
55
-
56
- if (strongest?.length) {
57
- console.log(` ${c.purple.bold('Strongest connections:')}`);
58
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
- for (const s of strongest as any[]) {
60
- const weight = (s.weight ?? 0);
61
- const weightColor = weight >= 0.7 ? c.green : weight >= 0.3 ? c.orange : c.dim;
62
- const src = `${s.source_type}:${s.source_id}`;
63
- const tgt = `${s.target_type}:${s.target_id}`;
64
- console.log(` ${c.dim(src)} ${c.cyan(icons.arrow)} ${c.dim(tgt)} ${c.label(`[${s.synapse_type}]`)} ${weightColor(weight.toFixed(3))}`);
65
- }
66
- }
67
- }
68
- console.log(`\n${divider()}`);
69
- });
70
- });
71
- }
@@ -1,47 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons } from '../colors.js';
4
-
5
- export function postCommand(): Command {
6
- return new Command('post')
7
- .description('Report a published post')
8
- .argument('<platform>', 'Platform (x, reddit, linkedin, bluesky)')
9
- .argument('[url]', 'Post URL')
10
- .option('-c, --content <text>', 'Post content/text')
11
- .option('-f, --format <format>', 'Post format (text, image, video, thread)', 'text')
12
- .option('--campaign <name>', 'Campaign name')
13
- .option('--hashtags <tags>', 'Hashtags (comma-separated)')
14
- .action(async (platform, url, opts) => {
15
- if (!opts.content && !url) {
16
- console.error(`${icons.error} ${c.error('Provide either --content or a URL')}`);
17
- process.exit(1);
18
- }
19
-
20
- await withIpc(async (client) => {
21
- let campaignId: number | null = null;
22
- if (opts.campaign) {
23
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
- const campaign: any = await client.request('campaign.create', { name: opts.campaign });
25
- campaignId = campaign.id;
26
- }
27
-
28
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
- const result: any = await client.request('post.report', {
30
- platform,
31
- content: opts.content ?? `Post at ${url}`,
32
- format: opts.format,
33
- url: url ?? null,
34
- hashtags: opts.hashtags ?? null,
35
- campaign_id: campaignId,
36
- status: 'published',
37
- published_at: new Date().toISOString(),
38
- });
39
-
40
- if (result.isNew) {
41
- console.log(`${icons.ok} ${c.success('Post reported!')} ${c.dim(`#${result.post.id} on ${platform}`)}`);
42
- } else {
43
- console.log(`${icons.post} ${c.info('Post already tracked')} ${c.dim(`#${result.post.id}`)}`);
44
- }
45
- });
46
- });
47
- }
@@ -1,108 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons, header, divider } from '../colors.js';
4
-
5
- export function queryCommand(): Command {
6
- return new Command('query')
7
- .description('Search posts, strategies, and insights')
8
- .argument('<search>', 'Search term')
9
- .option('-l, --limit <n>', 'Maximum results per category', '10')
10
- .option('--posts-only', 'Only search posts')
11
- .option('--strategies-only', 'Only search strategies')
12
- .option('--insights-only', 'Only search insights')
13
- .option('--page <n>', 'Page number (starting from 1)', '1')
14
- .action(async (search: string, opts) => {
15
- await withIpc(async (client) => {
16
- const limit = parseInt(opts.limit, 10);
17
- const page = parseInt(opts.page, 10) || 1;
18
- const offset = (page - 1) * limit;
19
- const searchAll = !opts.postsOnly && !opts.strategiesOnly && !opts.insightsOnly;
20
- let totalResults = 0;
21
-
22
- // --- Posts ---
23
- if (searchAll || opts.postsOnly) {
24
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
- const results: any = await client.request('post.search', {
26
- query: search,
27
- limit: limit + offset,
28
- });
29
-
30
- const posts = Array.isArray(results) ? results.slice(offset, offset + limit) : [];
31
- if (posts.length > 0) {
32
- totalResults += posts.length;
33
- console.log(header(`Posts matching "${search}"`, icons.post));
34
-
35
- for (const post of posts) {
36
- const platformTag = c.cyan(`[${post.platform}]`);
37
- const formatTag = c.purple(post.format ?? 'text');
38
- console.log(` ${c.dim(`#${post.id}`)} ${platformTag} ${formatTag}`);
39
- console.log(` ${c.dim((post.content ?? '').slice(0, 120))}`);
40
- if (post.hashtags) {
41
- console.log(` ${c.orange(post.hashtags)}`);
42
- }
43
- console.log();
44
- }
45
- }
46
- }
47
-
48
- // --- Strategies ---
49
- if (searchAll || opts.strategiesOnly) {
50
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
- const strategies: any = await client.request('strategy.suggest', {
52
- query: search,
53
- limit: limit + offset,
54
- });
55
-
56
- const strats = Array.isArray(strategies) ? strategies.slice(offset, offset + limit) : [];
57
- if (strats.length > 0) {
58
- totalResults += strats.length;
59
- console.log(header(`Strategies matching "${search}"`, icons.campaign));
60
-
61
- for (const strat of strats) {
62
- const confidence = strat.confidence ?? 0;
63
- const confColor = confidence >= 0.7 ? c.green : confidence >= 0.4 ? c.orange : c.dim;
64
- console.log(` ${c.dim(`#${strat.id}`)} ${confColor(`[${(confidence * 100).toFixed(0)}%]`)} ${c.value(strat.description ?? '')}`);
65
- if (strat.approach) {
66
- console.log(` ${c.label('Approach:')} ${c.dim(strat.approach.slice(0, 120))}`);
67
- }
68
- console.log();
69
- }
70
- }
71
- }
72
-
73
- // --- Insights ---
74
- if (searchAll || opts.insightsOnly) {
75
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
- const insights: any = await client.request('insight.list', { limit: 100 });
77
-
78
- const allInsights = Array.isArray(insights) ? insights : [];
79
- const searchLower = search.toLowerCase();
80
- const matched = allInsights.filter((i: { title?: string; description?: string }) =>
81
- (i.title ?? '').toLowerCase().includes(searchLower) ||
82
- (i.description ?? '').toLowerCase().includes(searchLower)
83
- ).slice(offset, offset + limit);
84
-
85
- if (matched.length > 0) {
86
- totalResults += matched.length;
87
- console.log(header(`Insights matching "${search}"`, icons.insight));
88
-
89
- for (const ins of matched) {
90
- const typeTag = c.cyan(`[${ins.type}]`);
91
- console.log(` ${typeTag} ${c.value(ins.title)}`);
92
- if (ins.description) {
93
- console.log(` ${c.dim(ins.description.slice(0, 150))}`);
94
- }
95
- console.log();
96
- }
97
- }
98
- }
99
-
100
- if (totalResults === 0) {
101
- console.log(`\n${icons.insight} ${c.dim(`No results found for "${search}".`)}`);
102
- } else {
103
- console.log(` ${c.dim(`Page ${page} — showing ${totalResults} result(s). Use --page ${page + 1} for more.`)}`);
104
- console.log(divider());
105
- }
106
- });
107
- });
108
- }
@@ -1,27 +0,0 @@
1
- import { Command } from 'commander';
2
- import { withIpc } from '../ipc-helper.js';
3
- import { c, icons } from '../colors.js';
4
-
5
- export function rulesCommand(): Command {
6
- return new Command('rules')
7
- .description('Show learned marketing rules')
8
- .action(async () => {
9
- await withIpc(async (client) => {
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- const rules: any[] = await client.request('rule.list') as any[];
12
-
13
- if (rules.length === 0) {
14
- console.log(`${c.dim('No rules learned yet. Track more posts to generate rules!')}`);
15
- return;
16
- }
17
-
18
- for (const rule of rules) {
19
- const conf = (rule.confidence * 100).toFixed(0);
20
- console.log(` ${icons.rule} ${c.value(rule.pattern)}`);
21
- console.log(` ${c.dim(rule.recommendation)}`);
22
- console.log(` ${c.dim(`confidence: ${conf}% | triggers: ${rule.trigger_count} | success: ${rule.success_count}`)}`);
23
- console.log();
24
- }
25
- });
26
- });
27
- }
@@ -1,100 +0,0 @@
1
- import { Command } from 'commander';
2
- import { spawn, type ChildProcess } from 'node:child_process';
3
- import fs from 'node:fs';
4
- import path from 'node:path';
5
- import { getDataDir } from '../../utils/paths.js';
6
- import { c, icons } from '../colors.js';
7
-
8
- const MAX_RESTARTS = 5;
9
- const RESTART_WINDOW_MS = 5 * 60 * 1000; // 5 minutes
10
- const BASE_BACKOFF_MS = 1000;
11
-
12
- function spawnDaemon(entryPoint: string, args: string[]): ChildProcess {
13
- const child = spawn(process.execPath, [entryPoint, ...args], {
14
- detached: true,
15
- stdio: 'ignore',
16
- });
17
- child.unref();
18
- return child;
19
- }
20
-
21
- function startWatchdog(entryPoint: string, args: string[], pidPath: string): void {
22
- const restartTimes: number[] = [];
23
-
24
- function launch(): void {
25
- const child = spawnDaemon(entryPoint, args);
26
- console.log(`${icons.megaphone} ${c.info('Marketing Brain daemon starting')} ${c.dim(`(PID: ${child.pid})`)}`);
27
-
28
- child.on('exit', (code) => {
29
- // Normal shutdown (code 0 or SIGTERM) — don't restart
30
- if (code === 0 || code === null) return;
31
-
32
- const now = Date.now();
33
- restartTimes.push(now);
34
-
35
- // Only count restarts within the window
36
- const recentRestarts = restartTimes.filter((t) => now - t < RESTART_WINDOW_MS);
37
- restartTimes.length = 0;
38
- restartTimes.push(...recentRestarts);
39
-
40
- if (recentRestarts.length > MAX_RESTARTS) {
41
- console.error(`${icons.error} ${c.error(`Marketing Brain crashed ${MAX_RESTARTS} times in 5 minutes — giving up.`)}`);
42
- // Clean up stale PID file
43
- try { fs.unlinkSync(pidPath); } catch { /* ignore */ }
44
- return;
45
- }
46
-
47
- const backoff = BASE_BACKOFF_MS * Math.pow(2, recentRestarts.length - 1);
48
- console.log(`${icons.warn} ${c.warn(`Marketing Brain exited (code ${code}) — restarting in ${backoff / 1000}s...`)}`);
49
-
50
- setTimeout(launch, backoff);
51
- });
52
- }
53
-
54
- launch();
55
-
56
- // Wait briefly for PID file to appear
57
- setTimeout(() => {
58
- if (fs.existsSync(pidPath)) {
59
- console.log(`${icons.ok} ${c.success('Marketing Brain daemon started successfully.')} ${c.dim('(watchdog active)')}`);
60
- } else {
61
- console.log(`${icons.clock} ${c.warn('Daemon may still be starting.')} Check: ${c.cyan('marketing status')}`);
62
- }
63
- }, 1000);
64
- }
65
-
66
- export function startCommand(): Command {
67
- return new Command('start')
68
- .description('Start the Marketing Brain daemon')
69
- .option('-f, --foreground', 'Run in foreground (no detach)')
70
- .option('-c, --config <path>', 'Config file path')
71
- .action((opts) => {
72
- const pidPath = path.join(getDataDir(), 'marketing-brain.pid');
73
-
74
- if (fs.existsSync(pidPath)) {
75
- const pid = parseInt(fs.readFileSync(pidPath, 'utf8').trim(), 10);
76
- try {
77
- process.kill(pid, 0);
78
- console.log(`${icons.megaphone} Marketing Brain daemon is ${c.green('already running')} ${c.dim(`(PID: ${pid})`)}`);
79
- return;
80
- } catch {
81
- fs.unlinkSync(pidPath);
82
- }
83
- }
84
-
85
- if (opts.foreground) {
86
- import('../../marketing-core.js').then(({ MarketingCore }) => {
87
- const core = new MarketingCore();
88
- core.start(opts.config);
89
- });
90
- return;
91
- }
92
-
93
- // Spawn detached daemon with watchdog
94
- const args = ['daemon'];
95
- if (opts.config) args.push('-c', opts.config);
96
- const entryPoint = path.resolve(import.meta.dirname, '../../index.js');
97
-
98
- startWatchdog(entryPoint, args, pidPath);
99
- });
100
- }
@@ -1,73 +0,0 @@
1
- import { Command } from 'commander';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
4
- import { getDataDir } from '../../utils/paths.js';
5
- import { withIpc } from '../ipc-helper.js';
6
- import { c, icons, header, keyValue, divider } from '../colors.js';
7
-
8
- export function statusCommand(): Command {
9
- return new Command('status')
10
- .description('Show Marketing Brain daemon status')
11
- .action(async () => {
12
- const pidPath = path.join(getDataDir(), 'marketing-brain.pid');
13
-
14
- if (!fs.existsSync(pidPath)) {
15
- console.log(`${icons.megaphone} Marketing Brain Daemon: ${c.red.bold('NOT RUNNING')}`);
16
- return;
17
- }
18
-
19
- const pid = parseInt(fs.readFileSync(pidPath, 'utf8').trim(), 10);
20
- let running = false;
21
- try {
22
- process.kill(pid, 0);
23
- running = true;
24
- } catch { /* not running */ }
25
-
26
- if (!running) {
27
- console.log(`${icons.megaphone} Marketing Brain Daemon: ${c.red.bold('NOT RUNNING')} ${c.dim('(stale PID file)')}`);
28
- return;
29
- }
30
-
31
- console.log(header('Marketing Brain Status', icons.megaphone));
32
- console.log(` ${c.green(`${icons.dot} RUNNING`)} ${c.dim(`(PID ${pid})`)}`);
33
-
34
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
- await withIpc(async (client) => {
36
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
- const summary: any = await client.request('analytics.summary', {});
38
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
- const network: any = await client.request('synapse.stats', {});
40
-
41
- const dbPath = path.join(getDataDir(), 'marketing-brain.db');
42
- let dbSize = '?';
43
- try {
44
- const stat = fs.statSync(dbPath);
45
- dbSize = `${(stat.size / 1024 / 1024).toFixed(1)} MB`;
46
- } catch { /* ignore */ }
47
-
48
- console.log(keyValue('Database', `${dbPath} (${dbSize})`));
49
- console.log();
50
-
51
- console.log(` ${icons.post} ${c.purple.bold('Content')}`);
52
- console.log(` ${c.label('Posts:')} ${c.value(summary.posts?.total ?? 0)} total`);
53
- console.log(` ${c.label('Campaigns:')} ${c.value(summary.campaigns?.total ?? 0)}`);
54
- console.log(` ${c.label('Templates:')} ${c.value(summary.templates?.total ?? 0)}`);
55
- console.log();
56
-
57
- console.log(` ${icons.rule} ${c.blue.bold('Learning')}`);
58
- console.log(` ${c.label('Strategies:')} ${c.value(summary.strategies?.total ?? 0)}`);
59
- console.log(` ${c.label('Rules:')} ${c.green(summary.rules?.active ?? 0)} active`);
60
- console.log();
61
-
62
- console.log(` ${icons.synapse} ${c.cyan.bold('Synapse Network')}`);
63
- console.log(` ${c.label('Synapses:')} ${c.value(network.totalSynapses ?? 0)}`);
64
- console.log(` ${c.label('Avg weight:')} ${c.value((network.avgWeight ?? 0).toFixed(2))}`);
65
- console.log();
66
-
67
- console.log(` ${icons.insight} ${c.orange.bold('Research')}`);
68
- console.log(` ${c.label('Insights:')} ${c.value(summary.insights?.active ?? 0)} active`);
69
-
70
- console.log(`\n${divider()}`);
71
- });
72
- });
73
- }
@@ -1,33 +0,0 @@
1
- import { Command } from 'commander';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
4
- import { getDataDir } from '../../utils/paths.js';
5
- import { c, icons } from '../colors.js';
6
-
7
- export function stopCommand(): Command {
8
- return new Command('stop')
9
- .description('Stop the Marketing Brain daemon')
10
- .action(() => {
11
- const pidPath = path.join(getDataDir(), 'marketing-brain.pid');
12
-
13
- if (!fs.existsSync(pidPath)) {
14
- console.log(`${icons.megaphone} ${c.dim('Marketing Brain daemon is not running (no PID file found).')}`);
15
- return;
16
- }
17
-
18
- const pid = parseInt(fs.readFileSync(pidPath, 'utf8').trim(), 10);
19
-
20
- try {
21
- process.kill(pid, 'SIGTERM');
22
- console.log(`${icons.megaphone} ${c.success('Marketing Brain daemon stopped')} ${c.dim(`(PID: ${pid})`)}`);
23
- } catch (err) {
24
- if ((err as NodeJS.ErrnoException).code === 'ESRCH') {
25
- console.log(`${icons.megaphone} ${c.dim('Marketing Brain daemon was not running (stale PID file removed).')}`);
26
- } else {
27
- console.error(`${icons.error} ${c.error(`Failed to stop daemon: ${err}`)}`);
28
- }
29
- }
30
-
31
- try { fs.unlinkSync(pidPath); } catch { /* ignore */ }
32
- });
33
- }