@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.
- package/package.json +96 -0
- package/schema.sql +586 -0
- package/src/adapters/voice/cloudflare-agent.ts +34 -0
- package/src/auth.ts +124 -0
- package/src/bluesky.ts +464 -0
- package/src/claude-tools/content.ts +188 -0
- package/src/claude-tools/email.ts +69 -0
- package/src/claude-tools/github.ts +440 -0
- package/src/claude-tools/goals.ts +116 -0
- package/src/claude-tools/index.ts +353 -0
- package/src/claude-tools/web.ts +59 -0
- package/src/claude.ts +406 -0
- package/src/codebeast.ts +200 -0
- package/src/composite.ts +715 -0
- package/src/content/column.ts +80 -0
- package/src/content/hero-image.ts +47 -0
- package/src/content/index.ts +27 -0
- package/src/content/journal.ts +91 -0
- package/src/content/roundtable.ts +163 -0
- package/src/core.ts +309 -0
- package/src/dashboard.ts +620 -0
- package/src/decision-docs.ts +284 -0
- package/src/dispatch.ts +13 -0
- package/src/edge-env.ts +58 -0
- package/src/email.ts +850 -0
- package/src/exports.ts +156 -0
- package/src/github-projects.ts +312 -0
- package/src/github.ts +670 -0
- package/src/groq.ts +247 -0
- package/src/health-page.ts +578 -0
- package/src/index.ts +89 -0
- package/src/kernel/argus-actions.ts +397 -0
- package/src/kernel/argus-correlation.ts +639 -0
- package/src/kernel/board.ts +91 -0
- package/src/kernel/briefing.ts +177 -0
- package/src/kernel/classify-memory-topic.ts +166 -0
- package/src/kernel/cognition.ts +377 -0
- package/src/kernel/court-cards.ts +163 -0
- package/src/kernel/dispatch.ts +587 -0
- package/src/kernel/domain.ts +50 -0
- package/src/kernel/dynamic-tools.ts +322 -0
- package/src/kernel/executor-port.ts +45 -0
- package/src/kernel/executors/claude.ts +73 -0
- package/src/kernel/executors/direct.ts +237 -0
- package/src/kernel/executors/groq.ts +18 -0
- package/src/kernel/executors/index.ts +87 -0
- package/src/kernel/executors/tarotscript.ts +104 -0
- package/src/kernel/executors/workers-ai.ts +54 -0
- package/src/kernel/insight-cache.ts +76 -0
- package/src/kernel/memory/agenda.ts +200 -0
- package/src/kernel/memory/blocks.ts +188 -0
- package/src/kernel/memory/consolidation.ts +194 -0
- package/src/kernel/memory/episodic.ts +241 -0
- package/src/kernel/memory/goals.ts +156 -0
- package/src/kernel/memory/graph.ts +290 -0
- package/src/kernel/memory/index.ts +11 -0
- package/src/kernel/memory/insights.ts +316 -0
- package/src/kernel/memory/procedural.ts +467 -0
- package/src/kernel/memory/pruning.ts +67 -0
- package/src/kernel/memory/recall.ts +367 -0
- package/src/kernel/memory/semantic.ts +315 -0
- package/src/kernel/memory/synthesis.ts +161 -0
- package/src/kernel/memory-adapter.ts +369 -0
- package/src/kernel/memory-guardrails.ts +76 -0
- package/src/kernel/port.ts +23 -0
- package/src/kernel/resilience.ts +322 -0
- package/src/kernel/router.ts +471 -0
- package/src/kernel/scheduled/agent-dispatch.ts +252 -0
- package/src/kernel/scheduled/argus-analytics.ts +247 -0
- package/src/kernel/scheduled/argus-heartbeat.ts +320 -0
- package/src/kernel/scheduled/argus-notify.ts +348 -0
- package/src/kernel/scheduled/board-sync.ts +110 -0
- package/src/kernel/scheduled/ci-watcher.ts +125 -0
- package/src/kernel/scheduled/cognitive-metrics.ts +377 -0
- package/src/kernel/scheduled/consolidation.ts +229 -0
- package/src/kernel/scheduled/content-drip.ts +47 -0
- package/src/kernel/scheduled/content.ts +6 -0
- package/src/kernel/scheduled/conversation-facts.ts +204 -0
- package/src/kernel/scheduled/cost-report.ts +84 -0
- package/src/kernel/scheduled/curiosity.ts +219 -0
- package/src/kernel/scheduled/dev-activity.ts +44 -0
- package/src/kernel/scheduled/digest.ts +317 -0
- package/src/kernel/scheduled/dreaming/agenda-triage.ts +115 -0
- package/src/kernel/scheduled/dreaming/facts.ts +239 -0
- package/src/kernel/scheduled/dreaming/index.ts +8 -0
- package/src/kernel/scheduled/dreaming/llm.ts +33 -0
- package/src/kernel/scheduled/dreaming/pattern-synthesis.ts +124 -0
- package/src/kernel/scheduled/dreaming/persona.ts +75 -0
- package/src/kernel/scheduled/dreaming/symbolic.ts +31 -0
- package/src/kernel/scheduled/dreaming/task-proposals.ts +80 -0
- package/src/kernel/scheduled/dreaming.ts +66 -0
- package/src/kernel/scheduled/entropy.ts +149 -0
- package/src/kernel/scheduled/escalation.ts +192 -0
- package/src/kernel/scheduled/feed-watcher.ts +206 -0
- package/src/kernel/scheduled/goals.ts +214 -0
- package/src/kernel/scheduled/governance.ts +41 -0
- package/src/kernel/scheduled/heartbeat.ts +220 -0
- package/src/kernel/scheduled/inbox-processor.ts +174 -0
- package/src/kernel/scheduled/index.ts +245 -0
- package/src/kernel/scheduled/issue-proposer.ts +478 -0
- package/src/kernel/scheduled/issue-watcher.ts +128 -0
- package/src/kernel/scheduled/pr-automerge.ts +213 -0
- package/src/kernel/scheduled/product-health.ts +107 -0
- package/src/kernel/scheduled/reflection.ts +373 -0
- package/src/kernel/scheduled/self-improvement.ts +114 -0
- package/src/kernel/scheduled/social-engage.ts +175 -0
- package/src/kernel/scheduled/task-audit.ts +60 -0
- package/src/kernel/symbolic.ts +156 -0
- package/src/kernel/types.ts +145 -0
- package/src/landing.ts +1190 -0
- package/src/lib/audit-chain/chain.ts +28 -0
- package/src/lib/audit-chain/types.ts +12 -0
- package/src/lib/observability/errors.ts +55 -0
- package/src/markdown.ts +164 -0
- package/src/mcp/handlers.ts +647 -0
- package/src/mcp/server.ts +184 -0
- package/src/mcp/tools.ts +316 -0
- package/src/mcp-client.ts +275 -0
- package/src/mcp-server.ts +2 -0
- package/src/operator/config.example.ts +60 -0
- package/src/operator/config.ts +60 -0
- package/src/operator/index.ts +46 -0
- package/src/operator/persona.example.ts +34 -0
- package/src/operator/persona.ts +34 -0
- package/src/operator/prompt-builder.ts +190 -0
- package/src/operator/types.ts +43 -0
- package/src/pulse.ts +1179 -0
- package/src/routes/bluesky.ts +116 -0
- package/src/routes/cc-tasks.ts +328 -0
- package/src/routes/codebeast.ts +1 -0
- package/src/routes/content.ts +194 -0
- package/src/routes/conversations.ts +25 -0
- package/src/routes/dynamic-tools.ts +111 -0
- package/src/routes/feedback.ts +192 -0
- package/src/routes/health.ts +147 -0
- package/src/routes/messages.ts +228 -0
- package/src/routes/observability.ts +82 -0
- package/src/routes/operator-logs.ts +42 -0
- package/src/routes/pages.ts +96 -0
- package/src/routes/sessions.ts +54 -0
- package/src/sanitize.ts +73 -0
- package/src/schema-enums.ts +155 -0
- package/src/search.ts +112 -0
- package/src/task-intelligence.ts +497 -0
- package/src/types.ts +194 -0
- package/src/ui.ts +5 -0
- package/src/version.ts +3 -0
- package/src/workers-ai-chat.ts +333 -0
package/src/groq.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// Edge-native Groq client — fetch-based, no OpenAI SDK dependency
|
|
2
|
+
|
|
3
|
+
export async function askGroq(
|
|
4
|
+
apiKey: string,
|
|
5
|
+
model: string,
|
|
6
|
+
systemPrompt: string,
|
|
7
|
+
userPrompt: string,
|
|
8
|
+
baseUrl = 'https://api.groq.com',
|
|
9
|
+
): Promise<string> {
|
|
10
|
+
const response = await fetch(`${baseUrl}/openai/v1/chat/completions`, {
|
|
11
|
+
method: 'POST',
|
|
12
|
+
headers: {
|
|
13
|
+
'Content-Type': 'application/json',
|
|
14
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
15
|
+
},
|
|
16
|
+
body: JSON.stringify({
|
|
17
|
+
model,
|
|
18
|
+
temperature: 0.3,
|
|
19
|
+
max_tokens: 500,
|
|
20
|
+
messages: [
|
|
21
|
+
{ role: 'system', content: systemPrompt },
|
|
22
|
+
{ role: 'user', content: userPrompt },
|
|
23
|
+
],
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
const errText = await response.text();
|
|
29
|
+
throw new Error(`Groq API error ${response.status}: ${errText}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const data = await response.json<{
|
|
33
|
+
choices: { message: { content: string } }[];
|
|
34
|
+
usage?: { total_tokens: number };
|
|
35
|
+
}>();
|
|
36
|
+
|
|
37
|
+
return data.choices[0]?.message?.content ?? '';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ─── Logprobs-enabled classification ─────────────────────────
|
|
41
|
+
// Requests logprobs from Groq's OpenAI-compatible API and computes
|
|
42
|
+
// geometric mean of token probabilities for the classification value.
|
|
43
|
+
// Returns both self-reported confidence and token-level confidence.
|
|
44
|
+
|
|
45
|
+
export interface LogprobClassification {
|
|
46
|
+
pattern: string;
|
|
47
|
+
complexity: number;
|
|
48
|
+
needs_tools: boolean;
|
|
49
|
+
selfReportedConfidence: number;
|
|
50
|
+
tokenConfidence: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function askGroqWithLogprobs(
|
|
54
|
+
apiKey: string,
|
|
55
|
+
model: string,
|
|
56
|
+
systemPrompt: string,
|
|
57
|
+
userPrompt: string,
|
|
58
|
+
baseUrl = 'https://api.groq.com',
|
|
59
|
+
): Promise<LogprobClassification> {
|
|
60
|
+
const response = await fetch(`${baseUrl}/openai/v1/chat/completions`, {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
headers: {
|
|
63
|
+
'Content-Type': 'application/json',
|
|
64
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
65
|
+
},
|
|
66
|
+
body: JSON.stringify({
|
|
67
|
+
model,
|
|
68
|
+
temperature: 0.1,
|
|
69
|
+
max_tokens: 200,
|
|
70
|
+
response_format: { type: 'json_object' },
|
|
71
|
+
logprobs: true,
|
|
72
|
+
top_logprobs: 3,
|
|
73
|
+
messages: [
|
|
74
|
+
{ role: 'system', content: systemPrompt },
|
|
75
|
+
{ role: 'user', content: userPrompt },
|
|
76
|
+
],
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (!response.ok) {
|
|
81
|
+
const errText = await response.text();
|
|
82
|
+
throw new Error(`Groq logprobs API error ${response.status}: ${errText}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface LogprobToken {
|
|
86
|
+
token: string;
|
|
87
|
+
logprob: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const data = await response.json<{
|
|
91
|
+
choices: {
|
|
92
|
+
message: { content: string };
|
|
93
|
+
logprobs?: { content?: LogprobToken[] };
|
|
94
|
+
}[];
|
|
95
|
+
}>();
|
|
96
|
+
|
|
97
|
+
const raw = data.choices[0]?.message?.content ?? '{}';
|
|
98
|
+
const parsed = JSON.parse(raw) as {
|
|
99
|
+
pattern?: string;
|
|
100
|
+
complexity?: number;
|
|
101
|
+
needs_tools?: boolean;
|
|
102
|
+
confidence?: number;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Compute geometric mean of token logprobs
|
|
106
|
+
let tokenConfidence = 0.5; // fallback if no logprobs
|
|
107
|
+
const logprobTokens = data.choices[0]?.logprobs?.content;
|
|
108
|
+
if (logprobTokens && logprobTokens.length > 0) {
|
|
109
|
+
const sumLogprobs = logprobTokens.reduce((sum, t) => sum + t.logprob, 0);
|
|
110
|
+
tokenConfidence = Math.exp(sumLogprobs / logprobTokens.length);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
pattern: (parsed.pattern ?? 'general_knowledge').toLowerCase().replace(/[^a-z_]/g, ''),
|
|
115
|
+
complexity: parsed.complexity ?? 2,
|
|
116
|
+
needs_tools: parsed.needs_tools ?? false,
|
|
117
|
+
selfReportedConfidence: parsed.confidence ?? 0.5,
|
|
118
|
+
tokenConfidence,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ─── Self-consistency probe ──────────────────────────────────
|
|
123
|
+
// Fire 3 parallel Groq 8B calls and check agreement via cosine
|
|
124
|
+
// similarity (BGE embeddings from Memory Worker). Falls back to
|
|
125
|
+
// Jaccard when memoryBinding is unavailable.
|
|
126
|
+
// Returns σ metric: 0=all agree, 0.5=partial, 1.0=disagree.
|
|
127
|
+
|
|
128
|
+
import { tokenize, jaccardSimilarity } from './kernel/memory/index.js';
|
|
129
|
+
import { cosineSimilarity } from './kernel/memory/semantic.js';
|
|
130
|
+
import type { MemoryServiceBinding } from './types.js';
|
|
131
|
+
|
|
132
|
+
const PROBE_TIMEOUT_MS = 3_000;
|
|
133
|
+
const JACCARD_AGREEMENT_THRESHOLD = 0.5;
|
|
134
|
+
const COSINE_AGREEMENT_THRESHOLD = 0.85;
|
|
135
|
+
|
|
136
|
+
export interface ProbeResult {
|
|
137
|
+
sigma: number; // 0, 0.5, or 1.0
|
|
138
|
+
agreedText: string | null;
|
|
139
|
+
responses: string[];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function probeConsistency(
|
|
143
|
+
apiKey: string,
|
|
144
|
+
model: string,
|
|
145
|
+
systemPrompt: string,
|
|
146
|
+
userPrompt: string,
|
|
147
|
+
baseUrl = 'https://api.groq.com',
|
|
148
|
+
memoryBinding?: MemoryServiceBinding,
|
|
149
|
+
): Promise<ProbeResult> {
|
|
150
|
+
const call = () => askGroq(apiKey, model, systemPrompt, userPrompt, baseUrl);
|
|
151
|
+
|
|
152
|
+
const responses = await Promise.race([
|
|
153
|
+
Promise.all([call(), call(), call()]),
|
|
154
|
+
new Promise<never>((_, rej) =>
|
|
155
|
+
setTimeout(() => rej(new Error('Probe timeout')), PROBE_TIMEOUT_MS),
|
|
156
|
+
),
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
const pairs: [number, number][] = [[0, 1], [0, 2], [1, 2]];
|
|
160
|
+
let similarities: number[];
|
|
161
|
+
let threshold: number;
|
|
162
|
+
|
|
163
|
+
// Prefer cosine similarity via Memory Worker embeddings
|
|
164
|
+
if (memoryBinding) {
|
|
165
|
+
try {
|
|
166
|
+
const { embeddings } = await memoryBinding.embed('aegis', responses);
|
|
167
|
+
similarities = pairs.map(([a, b]) => cosineSimilarity(embeddings[a], embeddings[b]));
|
|
168
|
+
threshold = COSINE_AGREEMENT_THRESHOLD;
|
|
169
|
+
} catch (err) {
|
|
170
|
+
console.warn('[probe] cosine embed failed, falling back to Jaccard:', err instanceof Error ? err.message : String(err));
|
|
171
|
+
const tokens = responses.map(r => tokenize(r));
|
|
172
|
+
similarities = pairs.map(([a, b]) => jaccardSimilarity(tokens[a], tokens[b]));
|
|
173
|
+
threshold = JACCARD_AGREEMENT_THRESHOLD;
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
// Jaccard fallback when memoryBinding unavailable
|
|
177
|
+
const tokens = responses.map(r => tokenize(r));
|
|
178
|
+
similarities = pairs.map(([a, b]) => jaccardSimilarity(tokens[a], tokens[b]));
|
|
179
|
+
threshold = JACCARD_AGREEMENT_THRESHOLD;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const minSim = Math.min(...similarities);
|
|
183
|
+
const maxSim = Math.max(...similarities);
|
|
184
|
+
|
|
185
|
+
if (minSim >= threshold) {
|
|
186
|
+
// All pairs agree → σ=0
|
|
187
|
+
return { sigma: 0, agreedText: responses[0], responses };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (maxSim >= threshold) {
|
|
191
|
+
// At least one pair agrees → σ=0.5 (partial)
|
|
192
|
+
return { sigma: 0.5, agreedText: null, responses };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// No pair agrees → σ=1.0 (total disagreement)
|
|
196
|
+
return { sigma: 1.0, agreedText: null, responses };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// JSON-mode variant — returns parsed JSON + raw string
|
|
200
|
+
export async function askGroqJson<T = unknown>(
|
|
201
|
+
apiKey: string,
|
|
202
|
+
model: string,
|
|
203
|
+
systemPrompt: string,
|
|
204
|
+
userPrompt: string,
|
|
205
|
+
baseUrl = 'https://api.groq.com',
|
|
206
|
+
options?: { maxTokens?: number; temperature?: number; prefill?: string },
|
|
207
|
+
): Promise<{ parsed: T; raw: string; usage?: { prompt_tokens: number; completion_tokens: number } }> {
|
|
208
|
+
const messages: Array<{ role: string; content: string }> = [
|
|
209
|
+
{ role: 'system', content: systemPrompt },
|
|
210
|
+
{ role: 'user', content: userPrompt },
|
|
211
|
+
];
|
|
212
|
+
// Prefilling: seed the assistant response to steer tone/format
|
|
213
|
+
if (options?.prefill) {
|
|
214
|
+
messages.push({ role: 'assistant', content: options.prefill });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const response = await fetch(`${baseUrl}/openai/v1/chat/completions`, {
|
|
218
|
+
method: 'POST',
|
|
219
|
+
headers: {
|
|
220
|
+
'Content-Type': 'application/json',
|
|
221
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
222
|
+
},
|
|
223
|
+
body: JSON.stringify({
|
|
224
|
+
model,
|
|
225
|
+
temperature: options?.temperature ?? 0.2,
|
|
226
|
+
max_tokens: options?.maxTokens ?? 2000,
|
|
227
|
+
response_format: { type: 'json_object' },
|
|
228
|
+
messages,
|
|
229
|
+
}),
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
if (!response.ok) {
|
|
233
|
+
const errText = await response.text();
|
|
234
|
+
throw new Error(`Groq API error ${response.status}: ${errText}`);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const data = await response.json<{
|
|
238
|
+
choices: { message: { content: string } }[];
|
|
239
|
+
usage?: { prompt_tokens: number; completion_tokens: number };
|
|
240
|
+
}>();
|
|
241
|
+
|
|
242
|
+
const completion = data.choices[0]?.message?.content ?? '{}';
|
|
243
|
+
// If prefilled, the model continues from the prefill — concatenate for valid JSON
|
|
244
|
+
const raw = options?.prefill ? options.prefill + completion : completion;
|
|
245
|
+
const parsed = JSON.parse(raw) as T;
|
|
246
|
+
return { parsed, raw, usage: data.usage };
|
|
247
|
+
}
|