@stackbilt/aegis-core 0.1.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 (148) hide show
  1. package/package.json +96 -0
  2. package/schema.sql +586 -0
  3. package/src/adapters/voice/cloudflare-agent.ts +34 -0
  4. package/src/auth.ts +124 -0
  5. package/src/bluesky.ts +464 -0
  6. package/src/claude-tools/content.ts +188 -0
  7. package/src/claude-tools/email.ts +69 -0
  8. package/src/claude-tools/github.ts +440 -0
  9. package/src/claude-tools/goals.ts +116 -0
  10. package/src/claude-tools/index.ts +353 -0
  11. package/src/claude-tools/web.ts +59 -0
  12. package/src/claude.ts +406 -0
  13. package/src/codebeast.ts +200 -0
  14. package/src/composite.ts +715 -0
  15. package/src/content/column.ts +80 -0
  16. package/src/content/hero-image.ts +47 -0
  17. package/src/content/index.ts +27 -0
  18. package/src/content/journal.ts +91 -0
  19. package/src/content/roundtable.ts +163 -0
  20. package/src/core.ts +309 -0
  21. package/src/dashboard.ts +620 -0
  22. package/src/decision-docs.ts +284 -0
  23. package/src/dispatch.ts +13 -0
  24. package/src/edge-env.ts +58 -0
  25. package/src/email.ts +850 -0
  26. package/src/exports.ts +156 -0
  27. package/src/github-projects.ts +312 -0
  28. package/src/github.ts +670 -0
  29. package/src/groq.ts +247 -0
  30. package/src/health-page.ts +578 -0
  31. package/src/index.ts +89 -0
  32. package/src/kernel/argus-actions.ts +397 -0
  33. package/src/kernel/argus-correlation.ts +639 -0
  34. package/src/kernel/board.ts +91 -0
  35. package/src/kernel/briefing.ts +177 -0
  36. package/src/kernel/classify-memory-topic.ts +166 -0
  37. package/src/kernel/cognition.ts +377 -0
  38. package/src/kernel/court-cards.ts +163 -0
  39. package/src/kernel/dispatch.ts +587 -0
  40. package/src/kernel/domain.ts +50 -0
  41. package/src/kernel/dynamic-tools.ts +322 -0
  42. package/src/kernel/executor-port.ts +45 -0
  43. package/src/kernel/executors/claude.ts +73 -0
  44. package/src/kernel/executors/direct.ts +237 -0
  45. package/src/kernel/executors/groq.ts +18 -0
  46. package/src/kernel/executors/index.ts +87 -0
  47. package/src/kernel/executors/tarotscript.ts +104 -0
  48. package/src/kernel/executors/workers-ai.ts +54 -0
  49. package/src/kernel/insight-cache.ts +76 -0
  50. package/src/kernel/memory/agenda.ts +200 -0
  51. package/src/kernel/memory/blocks.ts +188 -0
  52. package/src/kernel/memory/consolidation.ts +194 -0
  53. package/src/kernel/memory/episodic.ts +241 -0
  54. package/src/kernel/memory/goals.ts +156 -0
  55. package/src/kernel/memory/graph.ts +290 -0
  56. package/src/kernel/memory/index.ts +11 -0
  57. package/src/kernel/memory/insights.ts +316 -0
  58. package/src/kernel/memory/procedural.ts +467 -0
  59. package/src/kernel/memory/pruning.ts +67 -0
  60. package/src/kernel/memory/recall.ts +367 -0
  61. package/src/kernel/memory/semantic.ts +315 -0
  62. package/src/kernel/memory/synthesis.ts +161 -0
  63. package/src/kernel/memory-adapter.ts +369 -0
  64. package/src/kernel/memory-guardrails.ts +76 -0
  65. package/src/kernel/port.ts +23 -0
  66. package/src/kernel/resilience.ts +322 -0
  67. package/src/kernel/router.ts +471 -0
  68. package/src/kernel/scheduled/agent-dispatch.ts +252 -0
  69. package/src/kernel/scheduled/argus-analytics.ts +247 -0
  70. package/src/kernel/scheduled/argus-heartbeat.ts +320 -0
  71. package/src/kernel/scheduled/argus-notify.ts +348 -0
  72. package/src/kernel/scheduled/board-sync.ts +110 -0
  73. package/src/kernel/scheduled/ci-watcher.ts +125 -0
  74. package/src/kernel/scheduled/cognitive-metrics.ts +377 -0
  75. package/src/kernel/scheduled/consolidation.ts +229 -0
  76. package/src/kernel/scheduled/content-drip.ts +47 -0
  77. package/src/kernel/scheduled/content.ts +6 -0
  78. package/src/kernel/scheduled/conversation-facts.ts +204 -0
  79. package/src/kernel/scheduled/cost-report.ts +84 -0
  80. package/src/kernel/scheduled/curiosity.ts +219 -0
  81. package/src/kernel/scheduled/dev-activity.ts +44 -0
  82. package/src/kernel/scheduled/digest.ts +317 -0
  83. package/src/kernel/scheduled/dreaming/agenda-triage.ts +115 -0
  84. package/src/kernel/scheduled/dreaming/facts.ts +239 -0
  85. package/src/kernel/scheduled/dreaming/index.ts +8 -0
  86. package/src/kernel/scheduled/dreaming/llm.ts +33 -0
  87. package/src/kernel/scheduled/dreaming/pattern-synthesis.ts +124 -0
  88. package/src/kernel/scheduled/dreaming/persona.ts +75 -0
  89. package/src/kernel/scheduled/dreaming/symbolic.ts +31 -0
  90. package/src/kernel/scheduled/dreaming/task-proposals.ts +80 -0
  91. package/src/kernel/scheduled/dreaming.ts +66 -0
  92. package/src/kernel/scheduled/entropy.ts +149 -0
  93. package/src/kernel/scheduled/escalation.ts +192 -0
  94. package/src/kernel/scheduled/feed-watcher.ts +206 -0
  95. package/src/kernel/scheduled/goals.ts +214 -0
  96. package/src/kernel/scheduled/governance.ts +41 -0
  97. package/src/kernel/scheduled/heartbeat.ts +220 -0
  98. package/src/kernel/scheduled/inbox-processor.ts +174 -0
  99. package/src/kernel/scheduled/index.ts +245 -0
  100. package/src/kernel/scheduled/issue-proposer.ts +478 -0
  101. package/src/kernel/scheduled/issue-watcher.ts +128 -0
  102. package/src/kernel/scheduled/pr-automerge.ts +213 -0
  103. package/src/kernel/scheduled/product-health.ts +107 -0
  104. package/src/kernel/scheduled/reflection.ts +373 -0
  105. package/src/kernel/scheduled/self-improvement.ts +114 -0
  106. package/src/kernel/scheduled/social-engage.ts +175 -0
  107. package/src/kernel/scheduled/task-audit.ts +60 -0
  108. package/src/kernel/symbolic.ts +156 -0
  109. package/src/kernel/types.ts +145 -0
  110. package/src/landing.ts +1190 -0
  111. package/src/lib/audit-chain/chain.ts +28 -0
  112. package/src/lib/audit-chain/types.ts +12 -0
  113. package/src/lib/observability/errors.ts +55 -0
  114. package/src/markdown.ts +164 -0
  115. package/src/mcp/handlers.ts +647 -0
  116. package/src/mcp/server.ts +184 -0
  117. package/src/mcp/tools.ts +316 -0
  118. package/src/mcp-client.ts +275 -0
  119. package/src/mcp-server.ts +2 -0
  120. package/src/operator/config.example.ts +60 -0
  121. package/src/operator/config.ts +60 -0
  122. package/src/operator/index.ts +46 -0
  123. package/src/operator/persona.example.ts +34 -0
  124. package/src/operator/persona.ts +34 -0
  125. package/src/operator/prompt-builder.ts +190 -0
  126. package/src/operator/types.ts +43 -0
  127. package/src/pulse.ts +1179 -0
  128. package/src/routes/bluesky.ts +116 -0
  129. package/src/routes/cc-tasks.ts +328 -0
  130. package/src/routes/codebeast.ts +1 -0
  131. package/src/routes/content.ts +194 -0
  132. package/src/routes/conversations.ts +25 -0
  133. package/src/routes/dynamic-tools.ts +111 -0
  134. package/src/routes/feedback.ts +192 -0
  135. package/src/routes/health.ts +147 -0
  136. package/src/routes/messages.ts +228 -0
  137. package/src/routes/observability.ts +82 -0
  138. package/src/routes/operator-logs.ts +42 -0
  139. package/src/routes/pages.ts +96 -0
  140. package/src/routes/sessions.ts +54 -0
  141. package/src/sanitize.ts +73 -0
  142. package/src/schema-enums.ts +155 -0
  143. package/src/search.ts +112 -0
  144. package/src/task-intelligence.ts +497 -0
  145. package/src/types.ts +194 -0
  146. package/src/ui.ts +5 -0
  147. package/src/version.ts +3 -0
  148. package/src/workers-ai-chat.ts +333 -0
@@ -0,0 +1,80 @@
1
+ // Stub — full implementation not yet extracted to OSS
2
+
3
+ import { sanitizeForBlog } from '../sanitize.js';
4
+
5
+ export interface ColumnOutput {
6
+ title: string;
7
+ slug: string;
8
+ meta_description: string;
9
+ body: string;
10
+ }
11
+
12
+ export async function generateColumn(
13
+ apiKey: string,
14
+ model: string,
15
+ baseUrl: string,
16
+ activityContext: string,
17
+ ): Promise<{ output: ColumnOutput; cost: number }> {
18
+ const response = await fetch(baseUrl, {
19
+ method: 'POST',
20
+ headers: {
21
+ 'x-api-key': apiKey,
22
+ 'Content-Type': 'application/json',
23
+ 'anthropic-version': '2023-06-01',
24
+ },
25
+ body: JSON.stringify({
26
+ model,
27
+ max_tokens: 2048,
28
+ messages: [{ role: 'user', content: `Write an AEGIS column based on recent activity:\n\n${activityContext}` }],
29
+ }),
30
+ });
31
+
32
+ if (!response.ok) {
33
+ const text = await response.text();
34
+ throw new Error(`Anthropic API error ${response.status}: ${text}`);
35
+ }
36
+
37
+ const data = await response.json() as {
38
+ content: Array<{ type: string; text: string }>;
39
+ usage: { input_tokens: number; output_tokens: number };
40
+ };
41
+
42
+ let text = data.content[0].text;
43
+ text = text.replace(/^```json\n?/, '').replace(/\n?```$/, '');
44
+
45
+ const parsed = JSON.parse(text);
46
+ const output: ColumnOutput = {
47
+ title: parsed.title ?? '',
48
+ slug: parsed.slug ?? '',
49
+ meta_description: parsed.meta_description ?? '',
50
+ body: sanitizeForBlog(parsed.body ?? ''),
51
+ };
52
+
53
+ if (!output.title || !output.slug || !output.body) {
54
+ throw new Error('Invalid column output structure');
55
+ }
56
+
57
+ const cost = (data.usage.input_tokens * 3 + data.usage.output_tokens * 15) / 1_000_000;
58
+
59
+ return { output, cost };
60
+ }
61
+
62
+ export async function writeColumn(
63
+ db: D1Database,
64
+ output: ColumnOutput,
65
+ ): Promise<string> {
66
+ const id = crypto.randomUUID();
67
+
68
+ await db.prepare(
69
+ `INSERT INTO roundtables (id, topic, slug, meta_description, header_image_url, cta_url, status, quick_specs, synthesis)
70
+ VALUES (?, 'aegis_column', ?, ?, NULL, ?, 'draft', '[]', ?)`,
71
+ ).bind(
72
+ id,
73
+ output.slug,
74
+ output.meta_description,
75
+ 'https://example.com',
76
+ output.body,
77
+ ).run();
78
+
79
+ return id;
80
+ }
@@ -0,0 +1,47 @@
1
+ // Stub — full implementation not yet extracted to OSS
2
+
3
+ export interface ImgForgeConfig {
4
+ fetcher: Fetcher;
5
+ sbSecret: string;
6
+ baseUrl: string;
7
+ }
8
+
9
+ type QualityTier = 'standard' | 'ultra' | 'ultra_plus';
10
+
11
+ const TEXT_CAPABLE_TIERS: QualityTier[] = ['ultra', 'ultra_plus'];
12
+
13
+ const NO_TEXT_SUFFIX = '\n\nDo not include any text, letters, words, or typography in the image.';
14
+
15
+ export async function generateHeroImage(
16
+ config: ImgForgeConfig,
17
+ prompt: string,
18
+ tier: QualityTier = 'standard',
19
+ ): Promise<string | null> {
20
+ try {
21
+ const finalPrompt = TEXT_CAPABLE_TIERS.includes(tier)
22
+ ? prompt
23
+ : prompt + NO_TEXT_SUFFIX;
24
+
25
+ const response = await config.fetcher.fetch('https://internal/v2/generate', {
26
+ method: 'POST',
27
+ headers: {
28
+ 'X-Service-Binding': config.sbSecret,
29
+ 'Content-Type': 'application/json',
30
+ },
31
+ body: JSON.stringify({
32
+ prompt: finalPrompt,
33
+ quality_tier: tier,
34
+ sync: true,
35
+ }),
36
+ });
37
+
38
+ if (!response.ok) return null;
39
+
40
+ const data = await response.json() as { asset_url: string | null };
41
+ if (!data.asset_url) return null;
42
+
43
+ return `${config.baseUrl}${data.asset_url}`;
44
+ } catch {
45
+ return null;
46
+ }
47
+ }
@@ -0,0 +1,27 @@
1
+ // Stub — full implementation not yet extracted to OSS
2
+
3
+ export { type ImgForgeConfig } from './hero-image.js';
4
+
5
+ export async function runRoundtableGeneration(
6
+ _roundtableDb: D1Database,
7
+ _aegisDb: D1Database,
8
+ _apiKey: string,
9
+ _model: string,
10
+ _baseUrl: string,
11
+ ): Promise<{ title: string; slug: string; cost: number }> {
12
+ throw new Error('not implemented');
13
+ }
14
+
15
+ export async function queueRoundtableTopic(
16
+ _roundtableDb: D1Database,
17
+ _topic: string,
18
+ _context: string,
19
+ _ctaProduct: string,
20
+ _source: string,
21
+ ): Promise<number> {
22
+ throw new Error('not implemented');
23
+ }
24
+
25
+ export async function runJournalGeneration(env: any): Promise<{ ok: boolean }> {
26
+ throw new Error('not implemented');
27
+ }
@@ -0,0 +1,91 @@
1
+ // Stub — full implementation not yet extracted to OSS
2
+
3
+ import { sanitizeForBlog } from '../sanitize.js';
4
+
5
+ export interface JournalOutput {
6
+ title: string;
7
+ slug: string;
8
+ meta_description: string;
9
+ body: string;
10
+ mood: string;
11
+ tags: string[];
12
+ }
13
+
14
+ export async function generateJournal(
15
+ apiKey: string,
16
+ model: string,
17
+ baseUrl: string,
18
+ activityContext: string,
19
+ ): Promise<{ output: JournalOutput; cost: number }> {
20
+ const response = await fetch(baseUrl, {
21
+ method: 'POST',
22
+ headers: {
23
+ 'x-api-key': apiKey,
24
+ 'Content-Type': 'application/json',
25
+ 'anthropic-version': '2023-06-01',
26
+ },
27
+ body: JSON.stringify({
28
+ model,
29
+ max_tokens: 1536,
30
+ messages: [{ role: 'user', content: `Write an AEGIS journal entry based on recent activity:\n\n${activityContext}` }],
31
+ }),
32
+ });
33
+
34
+ if (!response.ok) {
35
+ const text = await response.text();
36
+ throw new Error(`Anthropic API error ${response.status}: ${text}`);
37
+ }
38
+
39
+ const data = await response.json() as {
40
+ content: Array<{ type: string; text: string }>;
41
+ usage: { input_tokens: number; output_tokens: number };
42
+ };
43
+
44
+ let text = data.content[0].text;
45
+ text = text.replace(/^```json\n?/, '').replace(/\n?```$/, '');
46
+
47
+ const parsed = JSON.parse(text);
48
+ const output: JournalOutput = {
49
+ title: parsed.title ?? '',
50
+ slug: parsed.slug ?? '',
51
+ meta_description: parsed.meta_description ?? '',
52
+ body: sanitizeForBlog(parsed.body ?? ''),
53
+ mood: parsed.mood ?? 'focused',
54
+ tags: Array.isArray(parsed.tags) ? parsed.tags : [],
55
+ };
56
+
57
+ if (!output.title || !output.slug || !output.body) {
58
+ throw new Error('Invalid journal output structure');
59
+ }
60
+
61
+ const cost = (data.usage.input_tokens * 3 + data.usage.output_tokens * 15) / 1_000_000;
62
+
63
+ return { output, cost };
64
+ }
65
+
66
+ export async function writeJournal(
67
+ db: D1Database,
68
+ output: JournalOutput,
69
+ headerImageUrl: string | null = null,
70
+ ): Promise<string> {
71
+ const id = crypto.randomUUID();
72
+
73
+ const quickSpecs = JSON.stringify([
74
+ { label: 'Mood', value: output.mood },
75
+ ...output.tags.map(tag => ({ label: 'Tag', value: tag })),
76
+ ]);
77
+
78
+ await db.prepare(
79
+ `INSERT INTO roundtables (id, topic, slug, meta_description, header_image_url, cta_url, status, quick_specs, synthesis)
80
+ VALUES (?, 'aegis_journal', ?, ?, ?, 'https://example.com', 'published', ?, ?)`,
81
+ ).bind(
82
+ id,
83
+ output.slug,
84
+ output.meta_description,
85
+ headerImageUrl,
86
+ quickSpecs,
87
+ output.body,
88
+ ).run();
89
+
90
+ return id;
91
+ }
@@ -0,0 +1,163 @@
1
+ // Stub — full implementation not yet extracted to OSS
2
+
3
+ import { sanitizeForBlog } from '../sanitize.js';
4
+
5
+ export interface RoundtableOutput {
6
+ title: string;
7
+ slug: string;
8
+ meta_description: string;
9
+ executive_summary: Array<{ metric: string; status: string; takeaway: string }>;
10
+ quick_specs: Array<{ label: string; value: string }>;
11
+ contributions: Array<{ persona: string; content: string; key_findings: string[] }>;
12
+ synthesis: string;
13
+ }
14
+
15
+ const CTA_URLS: Record<string, string> = {
16
+ 'demo-app': 'https://demo-app.example.com',
17
+ 'img-forge': 'https://img-forge.example.com',
18
+ };
19
+
20
+ // ─── pickNextTopic ──────────────────────────────────────────
21
+
22
+ export async function pickNextTopic(
23
+ db: D1Database,
24
+ ): Promise<{ id: string; topic: string; context: string; cta_product: string } | null> {
25
+ const topic = await db.prepare(
26
+ "SELECT id, topic, context, cta_product FROM topic_queue WHERE status = 'queued' ORDER BY created_at ASC LIMIT 1"
27
+ ).first<{ id: string; topic: string; context: string; cta_product: string }>();
28
+
29
+ if (!topic) return null;
30
+
31
+ await db.prepare(
32
+ "UPDATE topic_queue SET status = 'used' WHERE id = ?"
33
+ ).bind(topic.id).run();
34
+
35
+ return topic;
36
+ }
37
+
38
+ // ─── generateRoundtable ─────────────────────────────────────
39
+
40
+ export async function generateRoundtable(
41
+ apiKey: string,
42
+ model: string,
43
+ baseUrl: string,
44
+ topic: string,
45
+ context: string | null,
46
+ ctaProduct: string | null,
47
+ ): Promise<{ output: RoundtableOutput; cost: number }> {
48
+ let userMessage = `Generate a roundtable discussion about: ${topic}`;
49
+ if (context) userMessage += `\n\nCONTEXT: ${context}`;
50
+ if (ctaProduct) userMessage += `\nCTA_PRODUCT: ${ctaProduct}`;
51
+
52
+ const response = await fetch(baseUrl, {
53
+ method: 'POST',
54
+ headers: {
55
+ 'x-api-key': apiKey,
56
+ 'Content-Type': 'application/json',
57
+ 'anthropic-version': '2023-06-01',
58
+ },
59
+ body: JSON.stringify({
60
+ model,
61
+ max_tokens: 4096,
62
+ messages: [{ role: 'user', content: userMessage }],
63
+ }),
64
+ });
65
+
66
+ if (!response.ok) {
67
+ const text = await response.text();
68
+ throw new Error(`Anthropic API error ${response.status}: ${text}`);
69
+ }
70
+
71
+ const data = await response.json() as {
72
+ content: Array<{ type: string; text: string }>;
73
+ usage: { input_tokens: number; output_tokens: number };
74
+ };
75
+
76
+ let text = data.content[0].text;
77
+ text = text.replace(/^```json\n?/, '').replace(/\n?```$/, '');
78
+
79
+ const parsed = JSON.parse(text);
80
+ const output: RoundtableOutput = {
81
+ title: parsed.title ?? '',
82
+ slug: parsed.slug ?? '',
83
+ meta_description: parsed.meta_description ?? '',
84
+ executive_summary: Array.isArray(parsed.executive_summary) ? parsed.executive_summary : [],
85
+ quick_specs: Array.isArray(parsed.quick_specs) ? parsed.quick_specs : [],
86
+ contributions: (Array.isArray(parsed.contributions) ? parsed.contributions : []).map(
87
+ (c: any) => ({
88
+ persona: c.persona ?? '',
89
+ content: sanitizeForBlog(c.content ?? ''),
90
+ key_findings: Array.isArray(c.key_findings) ? c.key_findings : [],
91
+ }),
92
+ ),
93
+ synthesis: parsed.synthesis ?? '',
94
+ };
95
+
96
+ if (!output.title || output.contributions.length === 0) {
97
+ throw new Error('Invalid roundtable output structure');
98
+ }
99
+
100
+ const cost = (data.usage.input_tokens * 3 + data.usage.output_tokens * 15) / 1_000_000;
101
+
102
+ return { output, cost };
103
+ }
104
+
105
+ // ─── writeRoundtable ────────────────────────────────────────
106
+
107
+ export async function writeRoundtable(
108
+ db: D1Database,
109
+ output: RoundtableOutput,
110
+ ctaProduct: string | null,
111
+ headerImageUrl: string | null = null,
112
+ ): Promise<string> {
113
+ const id = crypto.randomUUID();
114
+ const ctaUrl = (ctaProduct && CTA_URLS[ctaProduct]) ?? 'https://example.com';
115
+
116
+ await db.prepare(
117
+ `INSERT INTO roundtables (id, topic, slug, meta_description, header_image_url, cta_url, status, executive_summary, quick_specs, synthesis)
118
+ VALUES (?, ?, ?, ?, ?, ?, 'draft', ?, ?, ?)`,
119
+ ).bind(
120
+ id,
121
+ output.title,
122
+ output.slug,
123
+ output.meta_description,
124
+ headerImageUrl,
125
+ ctaUrl,
126
+ JSON.stringify(output.executive_summary),
127
+ JSON.stringify(output.quick_specs),
128
+ output.synthesis,
129
+ ).run();
130
+
131
+ for (const contribution of output.contributions) {
132
+ await db.prepare(
133
+ 'INSERT INTO contributions (roundtable_id, persona, content, key_findings) VALUES (?, ?, ?, ?)'
134
+ ).bind(id, contribution.persona, contribution.content, JSON.stringify(contribution.key_findings)).run();
135
+ }
136
+
137
+ return id;
138
+ }
139
+
140
+ // ─── queueRoundtableTopic ───────────────────────────────────
141
+
142
+ export async function queueRoundtableTopic(
143
+ db: D1Database,
144
+ topic: string,
145
+ context: string | null,
146
+ ctaProduct: string | null,
147
+ source: string,
148
+ ): Promise<string> {
149
+ // Check for duplicate
150
+ const existing = await db.prepare(
151
+ "SELECT id FROM topic_queue WHERE topic = ? AND status = 'queued'"
152
+ ).bind(topic).first<{ id: string }>();
153
+
154
+ if (existing) return existing.id;
155
+
156
+ const id = crypto.randomUUID();
157
+
158
+ await db.prepare(
159
+ 'INSERT INTO topic_queue (id, topic, context, cta_product, source, status) VALUES (?, ?, ?, ?, ?, ?)'
160
+ ).bind(id, topic, context, ctaProduct ?? 'general', source, 'queued').run();
161
+
162
+ return id;
163
+ }