@kernel.chat/kbot 2.13.1 → 2.14.1
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 +38 -2
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +65 -4
- package/dist/agent.js.map +1 -1
- package/dist/emergent-swarm.d.ts +41 -0
- package/dist/emergent-swarm.d.ts.map +1 -0
- package/dist/emergent-swarm.js +246 -0
- package/dist/emergent-swarm.js.map +1 -0
- package/dist/entropy-context.d.ts +40 -0
- package/dist/entropy-context.d.ts.map +1 -0
- package/dist/entropy-context.js +144 -0
- package/dist/entropy-context.js.map +1 -0
- package/dist/error-correction.d.ts +37 -0
- package/dist/error-correction.d.ts.map +1 -0
- package/dist/error-correction.js +174 -0
- package/dist/error-correction.js.map +1 -0
- package/dist/godel-limits.d.ts +46 -0
- package/dist/godel-limits.d.ts.map +1 -0
- package/dist/godel-limits.js +251 -0
- package/dist/godel-limits.js.map +1 -0
- package/dist/simulation.d.ts +50 -0
- package/dist/simulation.d.ts.map +1 -0
- package/dist/simulation.js +240 -0
- package/dist/simulation.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// Emergent role discovery for multi-agent swarms — inspired by Project Sid
|
|
2
|
+
// where 1,000+ AI agents autonomously developed specialized roles.
|
|
3
|
+
// Instead of hardcoded roles, agents evolve roles dynamically per task.
|
|
4
|
+
import { runAgent } from './agent.js';
|
|
5
|
+
import { loadConfig } from './auth.js';
|
|
6
|
+
const MAX_ADAPTATIONS = 3;
|
|
7
|
+
const OVERLAP_THRESHOLD = 0.7;
|
|
8
|
+
const MIN_QUALITY = 0.3;
|
|
9
|
+
const ANALYZE_PROMPT = `You are a task decomposition engine. Analyze this task and determine what specialized capabilities are needed.
|
|
10
|
+
|
|
11
|
+
TASK: {task}
|
|
12
|
+
|
|
13
|
+
Respond in JSON only:
|
|
14
|
+
{
|
|
15
|
+
"capabilities": ["list", "of", "required", "capabilities"],
|
|
16
|
+
"complexity": "simple|moderate|complex",
|
|
17
|
+
"estimatedAgents": 2-5
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
Examples of capabilities: "code_analysis", "security_review", "api_design", "data_modeling", "testing", "documentation", "performance_profiling", "ux_evaluation", "architecture", "debugging", "research", "writing", "critique"
|
|
21
|
+
|
|
22
|
+
Respond ONLY with the JSON object.`;
|
|
23
|
+
const ROLE_DISCOVERY_PROMPT = `You are a swarm role designer. Given a task and required capabilities, create specialized agent roles.
|
|
24
|
+
|
|
25
|
+
TASK: {task}
|
|
26
|
+
|
|
27
|
+
REQUIRED CAPABILITIES: {capabilities}
|
|
28
|
+
|
|
29
|
+
NUMBER OF AGENTS: {count}
|
|
30
|
+
|
|
31
|
+
Design {count} specialized roles. Each role should be focused, non-overlapping, and complementary. Do NOT use generic roles like "manager" or "assistant". Make them task-specific.
|
|
32
|
+
|
|
33
|
+
Respond in JSON only:
|
|
34
|
+
[
|
|
35
|
+
{
|
|
36
|
+
"id": "role-id",
|
|
37
|
+
"name": "Role Name",
|
|
38
|
+
"description": "What this role does for THIS specific task",
|
|
39
|
+
"strengths": ["strength1", "strength2"]
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
Respond ONLY with the JSON array.`;
|
|
44
|
+
const SYNTHESIS_PROMPT = `You are a swarm synthesis engine. Combine the outputs from multiple specialized agents into a unified response.
|
|
45
|
+
|
|
46
|
+
TASK: {task}
|
|
47
|
+
|
|
48
|
+
CONTRIBUTIONS:
|
|
49
|
+
{contributions}
|
|
50
|
+
|
|
51
|
+
Synthesize these into a single coherent response. Resolve any contradictions by favoring the contribution from the most relevant role. Preserve important details from each contribution.`;
|
|
52
|
+
export async function analyzeTaskRequirements(task) {
|
|
53
|
+
const config = loadConfig();
|
|
54
|
+
if (!config) {
|
|
55
|
+
return { capabilities: ['general'], complexity: 'simple', estimatedAgents: 2 };
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const result = await runAgent(ANALYZE_PROMPT.replace('{task}', task), { agent: 'kernel', stream: false, skipPlanner: true });
|
|
59
|
+
const match = result.content.match(/\{[\s\S]*\}/);
|
|
60
|
+
if (match) {
|
|
61
|
+
const parsed = JSON.parse(match[0]);
|
|
62
|
+
return {
|
|
63
|
+
capabilities: parsed.capabilities || ['general'],
|
|
64
|
+
complexity: parsed.complexity || 'moderate',
|
|
65
|
+
estimatedAgents: Math.min(5, Math.max(2, parsed.estimatedAgents || 3)),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch { /* fall through */ }
|
|
70
|
+
return { capabilities: ['general'], complexity: 'moderate', estimatedAgents: 3 };
|
|
71
|
+
}
|
|
72
|
+
export function discoverRoles(task, requirements) {
|
|
73
|
+
// Deterministic fallback based on capabilities
|
|
74
|
+
return requirements.capabilities.slice(0, requirements.estimatedAgents).map((cap, i) => ({
|
|
75
|
+
id: `role-${i}`,
|
|
76
|
+
name: cap.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase()),
|
|
77
|
+
description: `Specialist in ${cap.replace(/_/g, ' ')} for this task`,
|
|
78
|
+
strengths: [cap],
|
|
79
|
+
emergenceScore: 0,
|
|
80
|
+
taskHistory: [],
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
async function discoverRolesWithAI(task, requirements) {
|
|
84
|
+
try {
|
|
85
|
+
const result = await runAgent(ROLE_DISCOVERY_PROMPT
|
|
86
|
+
.replace('{task}', task)
|
|
87
|
+
.replace('{capabilities}', requirements.capabilities.join(', '))
|
|
88
|
+
.replace(/\{count\}/g, String(requirements.estimatedAgents)), { agent: 'kernel', stream: false, skipPlanner: true });
|
|
89
|
+
const match = result.content.match(/\[[\s\S]*\]/);
|
|
90
|
+
if (match) {
|
|
91
|
+
const parsed = JSON.parse(match[0]);
|
|
92
|
+
return parsed.map(r => ({
|
|
93
|
+
...r,
|
|
94
|
+
emergenceScore: 0,
|
|
95
|
+
taskHistory: [],
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch { /* fall through */ }
|
|
100
|
+
return discoverRoles(task, requirements);
|
|
101
|
+
}
|
|
102
|
+
function wordSet(text) {
|
|
103
|
+
return new Set(text.toLowerCase().split(/\s+/).filter(w => w.length > 2));
|
|
104
|
+
}
|
|
105
|
+
function wordOverlap(a, b) {
|
|
106
|
+
const setA = wordSet(a);
|
|
107
|
+
const setB = wordSet(b);
|
|
108
|
+
if (setA.size === 0 || setB.size === 0)
|
|
109
|
+
return 0;
|
|
110
|
+
let intersection = 0;
|
|
111
|
+
for (const w of setA) {
|
|
112
|
+
if (setB.has(w))
|
|
113
|
+
intersection++;
|
|
114
|
+
}
|
|
115
|
+
const union = new Set([...setA, ...setB]).size;
|
|
116
|
+
return union === 0 ? 0 : intersection / union;
|
|
117
|
+
}
|
|
118
|
+
export class EmergentSwarm {
|
|
119
|
+
task;
|
|
120
|
+
maxAgents;
|
|
121
|
+
state;
|
|
122
|
+
contributions = [];
|
|
123
|
+
constructor(task, maxAgents = 5) {
|
|
124
|
+
this.task = task;
|
|
125
|
+
this.maxAgents = Math.min(maxAgents, 5);
|
|
126
|
+
this.state = {
|
|
127
|
+
roles: [],
|
|
128
|
+
iteration: 0,
|
|
129
|
+
consensusHistory: [],
|
|
130
|
+
adaptations: 0,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
async initialize() {
|
|
134
|
+
const requirements = await analyzeTaskRequirements(this.task);
|
|
135
|
+
this.state.roles = await discoverRolesWithAI(this.task, requirements);
|
|
136
|
+
}
|
|
137
|
+
async execute() {
|
|
138
|
+
if (this.state.roles.length === 0)
|
|
139
|
+
await this.initialize();
|
|
140
|
+
// Execute each role
|
|
141
|
+
for (const role of this.state.roles) {
|
|
142
|
+
const rolePrompt = [
|
|
143
|
+
`You are a specialist with the role: ${role.name}`,
|
|
144
|
+
`Your focus: ${role.description}`,
|
|
145
|
+
`Your strengths: ${role.strengths.join(', ')}`,
|
|
146
|
+
'',
|
|
147
|
+
`TASK: ${this.task}`,
|
|
148
|
+
'',
|
|
149
|
+
'Provide your specialized contribution. Focus only on your area of expertise.',
|
|
150
|
+
].join('\n');
|
|
151
|
+
try {
|
|
152
|
+
const result = await runAgent(rolePrompt, {
|
|
153
|
+
agent: 'kernel',
|
|
154
|
+
stream: false,
|
|
155
|
+
skipPlanner: true,
|
|
156
|
+
});
|
|
157
|
+
const quality = result.content.length > 50 ? 0.8 : 0.3;
|
|
158
|
+
this.contributions.push({ role, output: result.content, quality });
|
|
159
|
+
role.taskHistory.push(this.task.slice(0, 100));
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
this.contributions.push({ role, output: '', quality: 0 });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
this.state.iteration++;
|
|
166
|
+
// Adapt roles based on output analysis
|
|
167
|
+
await this.adaptRoles();
|
|
168
|
+
// Synthesize
|
|
169
|
+
const synthesis = await this.synthesize();
|
|
170
|
+
const roleContributions = new Map();
|
|
171
|
+
for (const c of this.contributions) {
|
|
172
|
+
roleContributions.set(c.role.name, c.output);
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
synthesis,
|
|
176
|
+
roleContributions,
|
|
177
|
+
adaptations: this.state.adaptations,
|
|
178
|
+
iterations: this.state.iteration,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
async adapt(feedback) {
|
|
182
|
+
if (this.state.adaptations >= MAX_ADAPTATIONS)
|
|
183
|
+
return;
|
|
184
|
+
this.state.consensusHistory.push(feedback);
|
|
185
|
+
await this.adaptRoles();
|
|
186
|
+
}
|
|
187
|
+
getState() {
|
|
188
|
+
return { ...this.state };
|
|
189
|
+
}
|
|
190
|
+
async adaptRoles() {
|
|
191
|
+
if (this.state.adaptations >= MAX_ADAPTATIONS)
|
|
192
|
+
return;
|
|
193
|
+
if (this.contributions.length < 2)
|
|
194
|
+
return;
|
|
195
|
+
// Check for overlapping roles (merge if >70% word overlap)
|
|
196
|
+
for (let i = 0; i < this.contributions.length; i++) {
|
|
197
|
+
for (let j = i + 1; j < this.contributions.length; j++) {
|
|
198
|
+
const overlap = wordOverlap(this.contributions[i].output, this.contributions[j].output);
|
|
199
|
+
if (overlap > OVERLAP_THRESHOLD) {
|
|
200
|
+
// Merge: keep the higher-quality one
|
|
201
|
+
const keep = this.contributions[i].quality >= this.contributions[j].quality ? i : j;
|
|
202
|
+
const drop = keep === i ? j : i;
|
|
203
|
+
this.contributions[keep].output += '\n\n' + this.contributions[drop].output;
|
|
204
|
+
this.contributions[keep].role.strengths.push(...this.contributions[drop].role.strengths);
|
|
205
|
+
this.contributions.splice(drop, 1);
|
|
206
|
+
this.state.roles = this.state.roles.filter(r => r.id !== this.contributions[drop]?.role.id);
|
|
207
|
+
this.state.adaptations++;
|
|
208
|
+
if (this.state.adaptations >= MAX_ADAPTATIONS)
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Drop low-quality contributors
|
|
214
|
+
const weak = this.contributions.filter(c => c.quality < MIN_QUALITY);
|
|
215
|
+
for (const w of weak) {
|
|
216
|
+
if (this.state.adaptations >= MAX_ADAPTATIONS)
|
|
217
|
+
break;
|
|
218
|
+
const strongest = this.contributions.reduce((best, c) => c.quality > best.quality ? c : best);
|
|
219
|
+
if (strongest !== w) {
|
|
220
|
+
strongest.role.taskHistory.push(`absorbed: ${w.role.name}`);
|
|
221
|
+
this.contributions = this.contributions.filter(c => c !== w);
|
|
222
|
+
this.state.roles = this.state.roles.filter(r => r.id !== w.role.id);
|
|
223
|
+
this.state.adaptations++;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
async synthesize() {
|
|
228
|
+
if (this.contributions.length === 0)
|
|
229
|
+
return 'No contributions generated.';
|
|
230
|
+
if (this.contributions.length === 1)
|
|
231
|
+
return this.contributions[0].output;
|
|
232
|
+
const contribText = this.contributions
|
|
233
|
+
.map(c => `## ${c.role.name}\n${c.output}`)
|
|
234
|
+
.join('\n\n---\n\n');
|
|
235
|
+
try {
|
|
236
|
+
const result = await runAgent(SYNTHESIS_PROMPT
|
|
237
|
+
.replace('{task}', this.task)
|
|
238
|
+
.replace('{contributions}', contribText), { agent: 'kernel', stream: false, skipPlanner: true });
|
|
239
|
+
return result.content;
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
return contribText;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=emergent-swarm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emergent-swarm.js","sourceRoot":"","sources":["../src/emergent-swarm.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,mEAAmE;AACnE,wEAAwE;AAExE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAqCtC,MAAM,eAAe,GAAG,CAAC,CAAA;AACzB,MAAM,iBAAiB,GAAG,GAAG,CAAA;AAC7B,MAAM,WAAW,GAAG,GAAG,CAAA;AAEvB,MAAM,cAAc,GAAG;;;;;;;;;;;;;mCAaY,CAAA;AAEnC,MAAM,qBAAqB,GAAG;;;;;;;;;;;;;;;;;;;;kCAoBI,CAAA;AAElC,MAAM,gBAAgB,GAAG;;;;;;;0LAOiK,CAAA;AAE1L,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAAC,IAAY;IACxD,MAAM,MAAM,GAAG,UAAU,EAAE,CAAA;IAC3B,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,EAAE,YAAY,EAAE,CAAC,SAAS,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAE,eAAe,EAAE,CAAC,EAAE,CAAA;IAChF,CAAC;IAED,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,QAAQ,CAC3B,cAAc,CAAC,OAAO,CAAC,QAAQ,EAA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AC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;QAExE,MAAM,WAAW,GAAG,IAAI,CAAC,aAAa;aACnC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;aAC1C,IAAI,CAAC,aAAa,CAAC,CAAA;QAEtB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,QAAQ,CAC3B,gBAAgB;iBACb,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC;iBAC5B,OAAO,CAAC,iBAAiB,EAAE,WAAW,CAAC,EAC1C,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,CACtD,CAAA;YACD,OAAO,MAAM,CAAC,OAAO,CAAA;QACvB,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,WAAW,CAAA;QACpB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ConversationTurn } from './context-manager.js';
|
|
2
|
+
export interface EntropyScore {
|
|
3
|
+
entropy: number;
|
|
4
|
+
novelty: number;
|
|
5
|
+
density: number;
|
|
6
|
+
composite: number;
|
|
7
|
+
}
|
|
8
|
+
export interface RankedTurn {
|
|
9
|
+
turn: ConversationTurn;
|
|
10
|
+
score: EntropyScore;
|
|
11
|
+
rank: number;
|
|
12
|
+
}
|
|
13
|
+
export declare const STOPWORDS: Set<string>;
|
|
14
|
+
/** Shannon entropy — bits per character. Higher = more diverse/novel text. */
|
|
15
|
+
export declare function calculateEntropy(text: string): number;
|
|
16
|
+
/** How much new information a turn adds vs what's already in context. */
|
|
17
|
+
export declare function calculateSemanticNovelty(turn: string, previousTurns: string[]): number;
|
|
18
|
+
/** Ratio of unique meaningful tokens to total tokens. */
|
|
19
|
+
export declare function informationDensity(text: string): number;
|
|
20
|
+
export declare class EntropyScorer {
|
|
21
|
+
/**
|
|
22
|
+
* Composite score formula:
|
|
23
|
+
* 0.4 * novelty + 0.35 * density + 0.25 * normalizedEntropy
|
|
24
|
+
*
|
|
25
|
+
* Novelty weighted highest: redundant info is the biggest waste.
|
|
26
|
+
* Density second: filler text wastes tokens.
|
|
27
|
+
* Raw entropy lowest: can be high for random/noisy text too.
|
|
28
|
+
*/
|
|
29
|
+
scoreTurn(turn: ConversationTurn, history: ConversationTurn[]): EntropyScore;
|
|
30
|
+
/** Rank all turns by information value (highest first). */
|
|
31
|
+
rankTurns(turns: ConversationTurn[]): RankedTurn[];
|
|
32
|
+
/**
|
|
33
|
+
* Keep highest-entropy turns within token budget.
|
|
34
|
+
* Low-entropy turns are summarized or dropped.
|
|
35
|
+
*/
|
|
36
|
+
compress(turns: ConversationTurn[], tokenBudget: number, keepRecentCount?: number): ConversationTurn[];
|
|
37
|
+
/** Returns true if turn adds < 0.2 novelty — candidate for eviction. */
|
|
38
|
+
shouldEvict(turn: ConversationTurn, history: ConversationTurn[]): boolean;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=entropy-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entropy-context.d.ts","sourceRoot":"","sources":["../src/entropy-context.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,gBAAgB,CAAA;IACtB,KAAK,EAAE,YAAY,CAAA;IACnB,IAAI,EAAE,MAAM,CAAA;CACb;AAED,eAAO,MAAM,SAAS,aAQpB,CAAA;AAEF,8EAA8E;AAC9E,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAiBrD;AAED,yEAAyE;AACzE,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,MAAM,EACZ,aAAa,EAAE,MAAM,EAAE,GACtB,MAAM,CAqBR;AAED,yDAAyD;AACzD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAOvD;AAeD,qBAAa,aAAa;IACxB;;;;;;;OAOG;IACH,SAAS,CAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE,gBAAgB,EAAE,GAAG,YAAY;IAgB5E,2DAA2D;IAC3D,SAAS,CAAC,KAAK,EAAE,gBAAgB,EAAE,GAAG,UAAU,EAAE;IAWlD;;;OAGG;IACH,QAAQ,CACN,KAAK,EAAE,gBAAgB,EAAE,EACzB,WAAW,EAAE,MAAM,EACnB,eAAe,SAAI,GAClB,gBAAgB,EAAE;IA+BrB,wEAAwE;IACxE,WAAW,CAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE,gBAAgB,EAAE,GAAG,OAAO;CAK1E"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Information-theoretic context management — inspired by Melvin Vopson's
|
|
2
|
+
// research that the universe minimizes informational load. Uses Shannon
|
|
3
|
+
// entropy to measure actual information content of conversation turns.
|
|
4
|
+
import { estimateTokens } from './context-manager.js';
|
|
5
|
+
export const STOPWORDS = new Set([
|
|
6
|
+
'the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
|
|
7
|
+
'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could',
|
|
8
|
+
'should', 'may', 'might', 'shall', 'can', 'to', 'of', 'in', 'for',
|
|
9
|
+
'on', 'with', 'at', 'by', 'from', 'as', 'into', 'through', 'during',
|
|
10
|
+
'before', 'after', 'and', 'but', 'or', 'nor', 'not', 'so', 'yet',
|
|
11
|
+
'it', 'its', 'this', 'that', 'these', 'those', 'i', 'you', 'he',
|
|
12
|
+
'she', 'we', 'they', 'me', 'him', 'her', 'us', 'them', 'my', 'your',
|
|
13
|
+
]);
|
|
14
|
+
/** Shannon entropy — bits per character. Higher = more diverse/novel text. */
|
|
15
|
+
export function calculateEntropy(text) {
|
|
16
|
+
if (!text || text.length === 0)
|
|
17
|
+
return 0;
|
|
18
|
+
const freq = new Map();
|
|
19
|
+
const lower = text.toLowerCase();
|
|
20
|
+
for (let i = 0; i < lower.length; i++) {
|
|
21
|
+
const ch = lower[i];
|
|
22
|
+
freq.set(ch, (freq.get(ch) || 0) + 1);
|
|
23
|
+
}
|
|
24
|
+
let entropy = 0;
|
|
25
|
+
const len = lower.length;
|
|
26
|
+
for (const count of freq.values()) {
|
|
27
|
+
const p = count / len;
|
|
28
|
+
if (p > 0)
|
|
29
|
+
entropy -= p * Math.log2(p);
|
|
30
|
+
}
|
|
31
|
+
return entropy;
|
|
32
|
+
}
|
|
33
|
+
/** How much new information a turn adds vs what's already in context. */
|
|
34
|
+
export function calculateSemanticNovelty(turn, previousTurns) {
|
|
35
|
+
if (previousTurns.length === 0)
|
|
36
|
+
return 1.0;
|
|
37
|
+
const turnBigrams = extractBigrams(turn);
|
|
38
|
+
if (turnBigrams.size === 0)
|
|
39
|
+
return 0.5;
|
|
40
|
+
const contextBigrams = new Set();
|
|
41
|
+
for (const prev of previousTurns) {
|
|
42
|
+
for (const bg of extractBigrams(prev)) {
|
|
43
|
+
contextBigrams.add(bg);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (contextBigrams.size === 0)
|
|
47
|
+
return 1.0;
|
|
48
|
+
// Jaccard distance: 1 - (intersection / union)
|
|
49
|
+
let intersection = 0;
|
|
50
|
+
for (const bg of turnBigrams) {
|
|
51
|
+
if (contextBigrams.has(bg))
|
|
52
|
+
intersection++;
|
|
53
|
+
}
|
|
54
|
+
const union = new Set([...turnBigrams, ...contextBigrams]).size;
|
|
55
|
+
return union === 0 ? 0 : 1 - (intersection / union);
|
|
56
|
+
}
|
|
57
|
+
/** Ratio of unique meaningful tokens to total tokens. */
|
|
58
|
+
export function informationDensity(text) {
|
|
59
|
+
const words = text.toLowerCase().split(/\s+/).filter(w => w.length > 0);
|
|
60
|
+
if (words.length === 0)
|
|
61
|
+
return 0;
|
|
62
|
+
const meaningful = words.filter(w => !STOPWORDS.has(w) && w.length > 2);
|
|
63
|
+
const unique = new Set(meaningful);
|
|
64
|
+
return unique.size / words.length;
|
|
65
|
+
}
|
|
66
|
+
function extractBigrams(text) {
|
|
67
|
+
const words = text.toLowerCase().split(/\s+/).filter(w => w.length > 1);
|
|
68
|
+
const bigrams = new Set();
|
|
69
|
+
for (let i = 0; i < words.length - 1; i++) {
|
|
70
|
+
bigrams.add(`${words[i]} ${words[i + 1]}`);
|
|
71
|
+
}
|
|
72
|
+
return bigrams;
|
|
73
|
+
}
|
|
74
|
+
function turnContent(turn) {
|
|
75
|
+
return typeof turn.content === 'string' ? turn.content : JSON.stringify(turn.content);
|
|
76
|
+
}
|
|
77
|
+
export class EntropyScorer {
|
|
78
|
+
/**
|
|
79
|
+
* Composite score formula:
|
|
80
|
+
* 0.4 * novelty + 0.35 * density + 0.25 * normalizedEntropy
|
|
81
|
+
*
|
|
82
|
+
* Novelty weighted highest: redundant info is the biggest waste.
|
|
83
|
+
* Density second: filler text wastes tokens.
|
|
84
|
+
* Raw entropy lowest: can be high for random/noisy text too.
|
|
85
|
+
*/
|
|
86
|
+
scoreTurn(turn, history) {
|
|
87
|
+
const content = turnContent(turn);
|
|
88
|
+
const prevContents = history.map(turnContent);
|
|
89
|
+
const entropy = calculateEntropy(content);
|
|
90
|
+
const novelty = calculateSemanticNovelty(content, prevContents);
|
|
91
|
+
const density = informationDensity(content);
|
|
92
|
+
// Normalize entropy to 0-1 (English text typically 3.5-4.5 bits/char)
|
|
93
|
+
const normalizedEntropy = Math.min(1, entropy / 5);
|
|
94
|
+
const composite = 0.4 * novelty + 0.35 * density + 0.25 * normalizedEntropy;
|
|
95
|
+
return { entropy, novelty, density, composite };
|
|
96
|
+
}
|
|
97
|
+
/** Rank all turns by information value (highest first). */
|
|
98
|
+
rankTurns(turns) {
|
|
99
|
+
const scored = turns.map((turn, i) => {
|
|
100
|
+
const history = turns.slice(0, i);
|
|
101
|
+
return { turn, score: this.scoreTurn(turn, history), rank: 0 };
|
|
102
|
+
});
|
|
103
|
+
scored.sort((a, b) => b.score.composite - a.score.composite);
|
|
104
|
+
scored.forEach((item, i) => item.rank = i + 1);
|
|
105
|
+
return scored;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Keep highest-entropy turns within token budget.
|
|
109
|
+
* Low-entropy turns are summarized or dropped.
|
|
110
|
+
*/
|
|
111
|
+
compress(turns, tokenBudget, keepRecentCount = 4) {
|
|
112
|
+
if (turns.length <= keepRecentCount)
|
|
113
|
+
return turns;
|
|
114
|
+
// Always keep recent turns
|
|
115
|
+
const recent = turns.slice(-keepRecentCount);
|
|
116
|
+
const older = turns.slice(0, -keepRecentCount);
|
|
117
|
+
const recentTokens = recent.reduce((sum, t) => sum + estimateTokens(turnContent(t)), 0);
|
|
118
|
+
const remaining = tokenBudget - recentTokens;
|
|
119
|
+
if (remaining <= 0)
|
|
120
|
+
return recent;
|
|
121
|
+
// Rank older turns by entropy, keep highest-value ones
|
|
122
|
+
const ranked = this.rankTurns(older);
|
|
123
|
+
const kept = [];
|
|
124
|
+
let used = 0;
|
|
125
|
+
for (const item of ranked) {
|
|
126
|
+
const tokens = estimateTokens(turnContent(item.turn));
|
|
127
|
+
if (used + tokens <= remaining) {
|
|
128
|
+
kept.push(item.turn);
|
|
129
|
+
used += tokens;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Restore chronological order
|
|
133
|
+
const keptSet = new Set(kept);
|
|
134
|
+
const ordered = older.filter(t => keptSet.has(t));
|
|
135
|
+
return [...ordered, ...recent];
|
|
136
|
+
}
|
|
137
|
+
/** Returns true if turn adds < 0.2 novelty — candidate for eviction. */
|
|
138
|
+
shouldEvict(turn, history) {
|
|
139
|
+
const content = turnContent(turn);
|
|
140
|
+
const novelty = calculateSemanticNovelty(content, history.map(turnContent));
|
|
141
|
+
return novelty < 0.2;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=entropy-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entropy-context.js","sourceRoot":"","sources":["../src/entropy-context.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,wEAAwE;AACxE,uEAAuE;AAEvE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAgBrD,MAAM,CAAC,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC;IAC/B,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO;IACnE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IACnE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK;IACjE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ;IACnE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK;IAChE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI;IAC/D,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CACpE,CAAC,CAAA;A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= "hallucination",
|
|
3
|
+
wrong_tool = "wrong_tool",
|
|
4
|
+
stale_context = "stale_context",
|
|
5
|
+
incomplete = "incomplete",
|
|
6
|
+
off_topic = "off_topic",
|
|
7
|
+
syntax_error = "syntax_error",
|
|
8
|
+
logic_error = "logic_error"
|
|
9
|
+
}
|
|
10
|
+
export interface CorrectionStrategy {
|
|
11
|
+
errorType: ErrorType;
|
|
12
|
+
description: string;
|
|
13
|
+
correctionPrompt: string;
|
|
14
|
+
severity: number;
|
|
15
|
+
}
|
|
16
|
+
export interface ClassificationResult {
|
|
17
|
+
errorType: ErrorType;
|
|
18
|
+
confidence: number;
|
|
19
|
+
evidence: string;
|
|
20
|
+
}
|
|
21
|
+
export interface CorrectionRecord {
|
|
22
|
+
errorType: ErrorType;
|
|
23
|
+
confidence: number;
|
|
24
|
+
evidence: string;
|
|
25
|
+
correctionApplied: string;
|
|
26
|
+
attempt: number;
|
|
27
|
+
}
|
|
28
|
+
export interface ErrorCorrectionResult {
|
|
29
|
+
response: string;
|
|
30
|
+
corrections: CorrectionRecord[];
|
|
31
|
+
retries: number;
|
|
32
|
+
}
|
|
33
|
+
export declare const CORRECTION_STRATEGIES: Record<ErrorType, CorrectionStrategy>;
|
|
34
|
+
export declare function classifyError(query: string, response: string, context?: string): Promise<ClassificationResult | null>;
|
|
35
|
+
export declare function applyCorrection(query: string, response: string, errorType: ErrorType, evidence: string): string;
|
|
36
|
+
export declare function withErrorCorrection(generateFn: (injectedPrompt?: string) => Promise<string>, query: string, context?: string, maxRetries?: number): Promise<ErrorCorrectionResult>;
|
|
37
|
+
//# sourceMappingURL=error-correction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-correction.d.ts","sourceRoot":"","sources":["../src/error-correction.ts"],"names":[],"mappings":"AAMA,oBAAY,SAAS;IACnB,aAAa,kBAAkB;IAC/B,UAAU,eAAe;IACzB,aAAa,kBAAkB;IAC/B,UAAU,eAAe;IACzB,SAAS,cAAc;I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