universal-agent-memory 0.7.3 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -20
- package/dist/cli/memory.js +27 -0
- package/dist/cli/memory.js.map +1 -1
- package/dist/cli/update.d.ts.map +1 -1
- package/dist/cli/update.js +2 -2
- package/dist/cli/update.js.map +1 -1
- package/dist/memory/adaptive-context.d.ts +100 -0
- package/dist/memory/adaptive-context.d.ts.map +1 -0
- package/dist/memory/adaptive-context.js +456 -0
- package/dist/memory/adaptive-context.js.map +1 -0
- package/dist/utils/calculate-average.d.ts +15 -0
- package/dist/utils/calculate-average.d.ts.map +1 -0
- package/dist/utils/calculate-average.js +21 -0
- package/dist/utils/calculate-average.js.map +1 -0
- package/dist/utils/config-manager.d.ts +30 -0
- package/dist/utils/config-manager.d.ts.map +1 -0
- package/dist/utils/config-manager.js +41 -0
- package/dist/utils/config-manager.js.map +1 -0
- package/dist/utils/fetch-with-retry.d.ts +5 -0
- package/dist/utils/fetch-with-retry.d.ts.map +1 -0
- package/dist/utils/fetch-with-retry.js +61 -0
- package/dist/utils/fetch-with-retry.js.map +1 -0
- package/dist/utils/order-processor-refactored.d.ts +126 -0
- package/dist/utils/order-processor-refactored.d.ts.map +1 -0
- package/dist/utils/order-processor-refactored.js +165 -0
- package/dist/utils/order-processor-refactored.js.map +1 -0
- package/dist/utils/order-processor-strategy.d.ts +72 -0
- package/dist/utils/order-processor-strategy.d.ts.map +1 -0
- package/dist/utils/order-processor-strategy.js +158 -0
- package/dist/utils/order-processor-strategy.js.map +1 -0
- package/dist/utils/order-processor.d.ts +242 -0
- package/dist/utils/order-processor.d.ts.map +1 -0
- package/dist/utils/order-processor.js +370 -0
- package/dist/utils/order-processor.js.map +1 -0
- package/dist/utils/rate-limiter-simple.d.ts +58 -0
- package/dist/utils/rate-limiter-simple.d.ts.map +1 -0
- package/dist/utils/rate-limiter-simple.js +100 -0
- package/dist/utils/rate-limiter-simple.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hybrid Adaptive Context Selector for UAM (Option 4)
|
|
3
|
+
*
|
|
4
|
+
* Combines task classification with time-budget awareness, runtime monitoring,
|
|
5
|
+
* and historical benefit tracking for optimal context loading decisions.
|
|
6
|
+
*
|
|
7
|
+
* Decision factors:
|
|
8
|
+
* 1. Task classification (reasoning vs domain-knowledge tasks)
|
|
9
|
+
* 2. Time budget (critical/high/medium/low pressure)
|
|
10
|
+
* 3. Historical success rate for similar tasks
|
|
11
|
+
* 4. Estimated overhead vs available time
|
|
12
|
+
*/
|
|
13
|
+
import { classifyTask as classifyTaskType } from './task-classifier.js';
|
|
14
|
+
// Task categories that typically don't benefit from UAM context
|
|
15
|
+
const LOW_BENEFIT_CATEGORIES = new Set([
|
|
16
|
+
'reasoning',
|
|
17
|
+
'scheduling',
|
|
18
|
+
'constraint-satisfaction',
|
|
19
|
+
'games',
|
|
20
|
+
'pure-logic',
|
|
21
|
+
'mathematical',
|
|
22
|
+
]);
|
|
23
|
+
// Keywords that suggest a task won't benefit from domain knowledge
|
|
24
|
+
const SKIP_UAM_KEYWORDS = [
|
|
25
|
+
'schedule',
|
|
26
|
+
'scheduling',
|
|
27
|
+
'calendar',
|
|
28
|
+
'meeting',
|
|
29
|
+
'constraint',
|
|
30
|
+
'satisfy',
|
|
31
|
+
'optimize',
|
|
32
|
+
'chess move',
|
|
33
|
+
'best move',
|
|
34
|
+
'game theory',
|
|
35
|
+
'mathematical proof',
|
|
36
|
+
'prove that',
|
|
37
|
+
'logic puzzle',
|
|
38
|
+
'sudoku',
|
|
39
|
+
];
|
|
40
|
+
// Keywords that strongly suggest UAM will help
|
|
41
|
+
const HIGH_BENEFIT_KEYWORDS = [
|
|
42
|
+
'password',
|
|
43
|
+
'hash',
|
|
44
|
+
'crack',
|
|
45
|
+
'decrypt',
|
|
46
|
+
'elf',
|
|
47
|
+
'binary',
|
|
48
|
+
'executable',
|
|
49
|
+
'extract',
|
|
50
|
+
'xss',
|
|
51
|
+
'injection',
|
|
52
|
+
'sanitize',
|
|
53
|
+
'filter',
|
|
54
|
+
'sqlite',
|
|
55
|
+
'database',
|
|
56
|
+
'recovery',
|
|
57
|
+
'wal',
|
|
58
|
+
'compile',
|
|
59
|
+
'build',
|
|
60
|
+
'makefile',
|
|
61
|
+
'cobol',
|
|
62
|
+
'modernize',
|
|
63
|
+
'legacy',
|
|
64
|
+
];
|
|
65
|
+
// Context sections with token counts and relevance keywords
|
|
66
|
+
const CONTEXT_SECTIONS = {
|
|
67
|
+
security: {
|
|
68
|
+
tokens: 150,
|
|
69
|
+
keywords: ['xss', 'password', 'hash', 'crack', 'secret', 'exploit', 'injection', 'sanitize'],
|
|
70
|
+
content: `### Security
|
|
71
|
+
- XSS: Use bleach.clean() or DOMPurify. Remove script, onclick, onerror, javascript:
|
|
72
|
+
- Password cracking: hashcat -m 11600 for 7z, -m 0 MD5, john for CPU
|
|
73
|
+
- Binary secrets: strings, objdump -d, check .rodata section
|
|
74
|
+
- Always validate and sanitize user input`,
|
|
75
|
+
},
|
|
76
|
+
file_formats: {
|
|
77
|
+
tokens: 120,
|
|
78
|
+
keywords: ['elf', 'sqlite', '7z', 'archive', 'binary', 'extract', 'format'],
|
|
79
|
+
content: `### File Formats
|
|
80
|
+
- ELF: Program headers at e_phoff. Use struct.unpack for parsing
|
|
81
|
+
- SQLite WAL: Header 32 bytes, frames follow. PRAGMA wal_checkpoint to recover
|
|
82
|
+
- 7z: Install p7zip-full, use 7z x -p for password protected archives`,
|
|
83
|
+
},
|
|
84
|
+
coding: {
|
|
85
|
+
tokens: 80,
|
|
86
|
+
keywords: ['implement', 'function', 'class', 'refactor', 'algorithm', 'code'],
|
|
87
|
+
content: `### Coding
|
|
88
|
+
- Use absolute paths (/app/...)
|
|
89
|
+
- Verify file exists before reading
|
|
90
|
+
- Handle edge cases in parsing
|
|
91
|
+
- Match exact output format required`,
|
|
92
|
+
},
|
|
93
|
+
tools: {
|
|
94
|
+
tokens: 100,
|
|
95
|
+
keywords: ['hashcat', 'john', 'strings', 'objdump', 'readelf', 'command', 'cli'],
|
|
96
|
+
content: `### Tools
|
|
97
|
+
- hashcat: GPU password cracking, -m flag for hash type
|
|
98
|
+
- john: CPU password cracking, flexible format support
|
|
99
|
+
- readelf/objdump: Binary analysis
|
|
100
|
+
- strings: Extract printable strings from binaries`,
|
|
101
|
+
},
|
|
102
|
+
legacy: {
|
|
103
|
+
tokens: 90,
|
|
104
|
+
keywords: ['cobol', 'fortran', 'legacy', 'modernize', 'mainframe'],
|
|
105
|
+
content: `### Legacy Code
|
|
106
|
+
- COBOL: Fixed-format columns, WORKING-STORAGE for variables
|
|
107
|
+
- Modernization: Preserve business logic, update data structures
|
|
108
|
+
- Test with original inputs to verify behavior`,
|
|
109
|
+
},
|
|
110
|
+
debugging: {
|
|
111
|
+
tokens: 70,
|
|
112
|
+
keywords: ['debug', 'error', 'fix', 'traceback', 'exception', 'crash'],
|
|
113
|
+
content: `### Debugging
|
|
114
|
+
- Check logs first: journalctl, /var/log/
|
|
115
|
+
- Use verbose flags: -v, --debug
|
|
116
|
+
- Isolate the problem: binary search through changes`,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
// Estimated overhead per token (ms) - accounts for context processing
|
|
120
|
+
const MS_PER_TOKEN = 4;
|
|
121
|
+
// Historical benefit threshold - below this, skip UAM
|
|
122
|
+
const BENEFIT_THRESHOLD = 0.1;
|
|
123
|
+
// In-memory historical data store (in production, use SQLite)
|
|
124
|
+
const historicalDataStore = new Map();
|
|
125
|
+
/**
|
|
126
|
+
* Classify task type from instruction text
|
|
127
|
+
*/
|
|
128
|
+
export function classifyTask(instruction) {
|
|
129
|
+
const lower = instruction.toLowerCase();
|
|
130
|
+
// Check skip keywords first (pure reasoning tasks)
|
|
131
|
+
for (const kw of SKIP_UAM_KEYWORDS) {
|
|
132
|
+
if (lower.includes(kw)) {
|
|
133
|
+
if (lower.includes('schedule') || lower.includes('calendar') || lower.includes('meeting')) {
|
|
134
|
+
return 'scheduling';
|
|
135
|
+
}
|
|
136
|
+
if (lower.includes('chess') || lower.includes('game') || lower.includes('move')) {
|
|
137
|
+
return 'games';
|
|
138
|
+
}
|
|
139
|
+
if (lower.includes('constraint') || lower.includes('satisfy')) {
|
|
140
|
+
return 'constraint-satisfaction';
|
|
141
|
+
}
|
|
142
|
+
if (lower.includes('prove') || lower.includes('proof') || lower.includes('logic')) {
|
|
143
|
+
return 'pure-logic';
|
|
144
|
+
}
|
|
145
|
+
if (lower.includes('sudoku') || lower.includes('puzzle')) {
|
|
146
|
+
return 'reasoning';
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// Check high-benefit keywords
|
|
151
|
+
for (const kw of HIGH_BENEFIT_KEYWORDS) {
|
|
152
|
+
if (lower.includes(kw)) {
|
|
153
|
+
if (lower.includes('password') || lower.includes('hash') || lower.includes('crack')) {
|
|
154
|
+
return 'security';
|
|
155
|
+
}
|
|
156
|
+
if (lower.includes('xss') || lower.includes('injection') || lower.includes('sanitize')) {
|
|
157
|
+
return 'security';
|
|
158
|
+
}
|
|
159
|
+
if (lower.includes('elf') || lower.includes('sqlite') || lower.includes('binary')) {
|
|
160
|
+
return 'file-ops';
|
|
161
|
+
}
|
|
162
|
+
if (lower.includes('cobol') || lower.includes('legacy') || lower.includes('modernize')) {
|
|
163
|
+
return 'legacy';
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// Fall back to task-classifier for detailed classification
|
|
168
|
+
const classification = classifyTaskType(instruction);
|
|
169
|
+
return classification.category;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Assess time pressure based on timeout and task complexity
|
|
173
|
+
*/
|
|
174
|
+
export function assessTimePressure(timeoutSec, taskType, difficulty = 'medium') {
|
|
175
|
+
// Expected duration multipliers by difficulty
|
|
176
|
+
const difficultyMultiplier = {
|
|
177
|
+
easy: 0.5,
|
|
178
|
+
medium: 1.0,
|
|
179
|
+
hard: 2.0,
|
|
180
|
+
};
|
|
181
|
+
// Base expected duration by task type (seconds)
|
|
182
|
+
const baseDuration = {
|
|
183
|
+
security: 120,
|
|
184
|
+
'file-ops': 90,
|
|
185
|
+
legacy: 150,
|
|
186
|
+
coding: 60,
|
|
187
|
+
debugging: 90,
|
|
188
|
+
scheduling: 45,
|
|
189
|
+
games: 30,
|
|
190
|
+
'constraint-satisfaction': 60,
|
|
191
|
+
'pure-logic': 90,
|
|
192
|
+
reasoning: 60,
|
|
193
|
+
general: 60,
|
|
194
|
+
sysadmin: 120,
|
|
195
|
+
'ml-training': 180,
|
|
196
|
+
testing: 60,
|
|
197
|
+
unknown: 60,
|
|
198
|
+
};
|
|
199
|
+
const expectedDuration = (baseDuration[taskType] || 60) * (difficultyMultiplier[difficulty] || 1.0);
|
|
200
|
+
const ratio = timeoutSec / expectedDuration;
|
|
201
|
+
if (ratio < 1.2)
|
|
202
|
+
return 'critical';
|
|
203
|
+
if (ratio < 1.5)
|
|
204
|
+
return 'high';
|
|
205
|
+
if (ratio < 2.0)
|
|
206
|
+
return 'medium';
|
|
207
|
+
return 'low';
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Get historical benefit ratio for a task type
|
|
211
|
+
*/
|
|
212
|
+
export function getHistoricalBenefit(taskType) {
|
|
213
|
+
const data = historicalDataStore.get(taskType);
|
|
214
|
+
if (!data || data.totalAttempts < 3) {
|
|
215
|
+
// Not enough data - use defaults based on category
|
|
216
|
+
if (LOW_BENEFIT_CATEGORIES.has(taskType)) {
|
|
217
|
+
return 0.05; // Very low default for reasoning tasks
|
|
218
|
+
}
|
|
219
|
+
return 0.5; // Neutral default
|
|
220
|
+
}
|
|
221
|
+
// Calculate benefit as improvement ratio
|
|
222
|
+
const uamRate = data.uamSuccesses / Math.max(data.totalAttempts / 2, 1);
|
|
223
|
+
const noUamRate = data.noUamSuccesses / Math.max(data.totalAttempts / 2, 1);
|
|
224
|
+
if (noUamRate === 0)
|
|
225
|
+
return uamRate > 0 ? 1.0 : 0.5;
|
|
226
|
+
return (uamRate - noUamRate) / Math.max(uamRate, noUamRate, 0.1);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Record task outcome for historical tracking
|
|
230
|
+
*/
|
|
231
|
+
export function recordOutcome(taskType, usedUam, success, durationMs) {
|
|
232
|
+
let data = historicalDataStore.get(taskType);
|
|
233
|
+
if (!data) {
|
|
234
|
+
data = {
|
|
235
|
+
taskType,
|
|
236
|
+
totalAttempts: 0,
|
|
237
|
+
uamSuccesses: 0,
|
|
238
|
+
noUamSuccesses: 0,
|
|
239
|
+
avgTimeWithUam: 0,
|
|
240
|
+
avgTimeWithoutUam: 0,
|
|
241
|
+
};
|
|
242
|
+
historicalDataStore.set(taskType, data);
|
|
243
|
+
}
|
|
244
|
+
data.totalAttempts++;
|
|
245
|
+
if (success) {
|
|
246
|
+
if (usedUam) {
|
|
247
|
+
data.uamSuccesses++;
|
|
248
|
+
data.avgTimeWithUam =
|
|
249
|
+
(data.avgTimeWithUam * (data.uamSuccesses - 1) + durationMs) / data.uamSuccesses;
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
data.noUamSuccesses++;
|
|
253
|
+
data.avgTimeWithoutUam =
|
|
254
|
+
(data.avgTimeWithoutUam * (data.noUamSuccesses - 1) + durationMs) / data.noUamSuccesses;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Select relevant context sections based on task type and instruction
|
|
260
|
+
*/
|
|
261
|
+
export function selectRelevantSections(instruction, taskType) {
|
|
262
|
+
const lower = instruction.toLowerCase();
|
|
263
|
+
const sections = [];
|
|
264
|
+
for (const [name, config] of Object.entries(CONTEXT_SECTIONS)) {
|
|
265
|
+
if (config.keywords.some((kw) => lower.includes(kw))) {
|
|
266
|
+
sections.push(name);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// Add default sections for certain task types
|
|
270
|
+
if (taskType === 'security' && !sections.includes('security')) {
|
|
271
|
+
sections.push('security');
|
|
272
|
+
}
|
|
273
|
+
if (taskType === 'file-ops' && !sections.includes('file_formats')) {
|
|
274
|
+
sections.push('file_formats');
|
|
275
|
+
}
|
|
276
|
+
if (taskType === 'legacy' && !sections.includes('legacy')) {
|
|
277
|
+
sections.push('legacy');
|
|
278
|
+
}
|
|
279
|
+
return sections;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Calculate estimated overhead for given sections in milliseconds
|
|
283
|
+
*/
|
|
284
|
+
export function calculateOverhead(sections) {
|
|
285
|
+
let totalTokens = 0;
|
|
286
|
+
for (const section of sections) {
|
|
287
|
+
totalTokens += CONTEXT_SECTIONS[section]?.tokens || 0;
|
|
288
|
+
}
|
|
289
|
+
return totalTokens * MS_PER_TOKEN;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Main decision function: determine optimal context level using hybrid approach
|
|
293
|
+
*
|
|
294
|
+
* Decision Matrix:
|
|
295
|
+
* 1. Task type is pure reasoning → skip UAM
|
|
296
|
+
* 2. Historical benefit < threshold → skip UAM
|
|
297
|
+
* 3. Critical time pressure → skip UAM
|
|
298
|
+
* 4. High time pressure → minimal UAM (essential only)
|
|
299
|
+
* 5. Default → full UAM with relevant sections
|
|
300
|
+
*/
|
|
301
|
+
export function decideContextLevel(instruction, metadata = {}) {
|
|
302
|
+
const taskType = classifyTask(instruction);
|
|
303
|
+
const timeoutSec = metadata.timeout_sec || 300;
|
|
304
|
+
const difficulty = metadata.difficulty || 'medium';
|
|
305
|
+
// Factor 1: Task classification - skip for pure reasoning
|
|
306
|
+
if (LOW_BENEFIT_CATEGORIES.has(taskType)) {
|
|
307
|
+
return {
|
|
308
|
+
level: 'none',
|
|
309
|
+
sections: [],
|
|
310
|
+
reason: `Task type '${taskType}' is pure reasoning - UAM adds no benefit`,
|
|
311
|
+
estimatedOverheadMs: 0,
|
|
312
|
+
taskType,
|
|
313
|
+
timePressure: 'low',
|
|
314
|
+
historicalBenefit: 0,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
// Factor 2: Time pressure assessment
|
|
318
|
+
const timePressure = assessTimePressure(timeoutSec, taskType, difficulty);
|
|
319
|
+
// Factor 3: Historical benefit
|
|
320
|
+
const historicalBenefit = metadata.historical_uam_benefit ?? getHistoricalBenefit(taskType);
|
|
321
|
+
// Factor 4: Check if historical data suggests skipping UAM
|
|
322
|
+
if (historicalBenefit < BENEFIT_THRESHOLD) {
|
|
323
|
+
return {
|
|
324
|
+
level: 'none',
|
|
325
|
+
sections: [],
|
|
326
|
+
reason: `Low historical benefit (${(historicalBenefit * 100).toFixed(1)}%) for ${taskType}`,
|
|
327
|
+
estimatedOverheadMs: 0,
|
|
328
|
+
taskType,
|
|
329
|
+
timePressure,
|
|
330
|
+
historicalBenefit,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
// Factor 5: Critical time pressure - skip UAM
|
|
334
|
+
if (timePressure === 'critical') {
|
|
335
|
+
return {
|
|
336
|
+
level: 'none',
|
|
337
|
+
sections: [],
|
|
338
|
+
reason: 'Critical time pressure - skipping UAM to avoid timeout',
|
|
339
|
+
estimatedOverheadMs: 0,
|
|
340
|
+
taskType,
|
|
341
|
+
timePressure,
|
|
342
|
+
historicalBenefit,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
// Factor 6: Select relevant sections
|
|
346
|
+
const relevantSections = selectRelevantSections(instruction, taskType);
|
|
347
|
+
const estimatedOverhead = calculateOverhead(relevantSections);
|
|
348
|
+
// Factor 7: Check if overhead fits within time budget
|
|
349
|
+
const overheadRatio = estimatedOverhead / (timeoutSec * 1000);
|
|
350
|
+
if (timePressure === 'high' || overheadRatio > 0.1) {
|
|
351
|
+
// Use minimal context - only most relevant section
|
|
352
|
+
const minimalSections = relevantSections.slice(0, 1);
|
|
353
|
+
return {
|
|
354
|
+
level: 'minimal',
|
|
355
|
+
sections: minimalSections,
|
|
356
|
+
reason: `High time pressure - using minimal context (${minimalSections.join(', ') || 'best_practices'})`,
|
|
357
|
+
estimatedOverheadMs: calculateOverhead(minimalSections),
|
|
358
|
+
taskType,
|
|
359
|
+
timePressure,
|
|
360
|
+
historicalBenefit,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
// Default: Full context for everything else
|
|
364
|
+
return {
|
|
365
|
+
level: 'full',
|
|
366
|
+
sections: relevantSections.length > 0 ? relevantSections : ['coding'],
|
|
367
|
+
reason: `Full context for ${taskType} task (${timePressure} pressure)`,
|
|
368
|
+
estimatedOverheadMs: estimatedOverhead,
|
|
369
|
+
taskType,
|
|
370
|
+
timePressure,
|
|
371
|
+
historicalBenefit,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Generate context string based on decision
|
|
376
|
+
*/
|
|
377
|
+
export function generateContext(decision) {
|
|
378
|
+
if (decision.level === 'none' || decision.sections.length === 0) {
|
|
379
|
+
return '';
|
|
380
|
+
}
|
|
381
|
+
const contextParts = ['## UAM Memory Context\n'];
|
|
382
|
+
for (const section of decision.sections) {
|
|
383
|
+
const sectionConfig = CONTEXT_SECTIONS[section];
|
|
384
|
+
if (sectionConfig) {
|
|
385
|
+
contextParts.push(sectionConfig.content);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return contextParts.join('\n');
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Progressive context strategy for retry scenarios
|
|
392
|
+
*
|
|
393
|
+
* Returns context levels to try in order based on initial failure analysis.
|
|
394
|
+
*/
|
|
395
|
+
export function getProgressiveContextLevels(instruction, initialError, metadata = {}) {
|
|
396
|
+
const decision = decideContextLevel(instruction, metadata);
|
|
397
|
+
// If we already decided 'none' for a good reason, don't retry with more
|
|
398
|
+
if (decision.level === 'none' && LOW_BENEFIT_CATEGORIES.has(decision.taskType)) {
|
|
399
|
+
return ['none']; // Don't escalate for pure reasoning tasks
|
|
400
|
+
}
|
|
401
|
+
// Analyze error to see if context might help
|
|
402
|
+
const errorLower = initialError.toLowerCase();
|
|
403
|
+
const contextMightHelp = errorLower.includes('unknown') ||
|
|
404
|
+
errorLower.includes('how to') ||
|
|
405
|
+
errorLower.includes('what is') ||
|
|
406
|
+
errorLower.includes('command not found') ||
|
|
407
|
+
errorLower.includes('invalid syntax') ||
|
|
408
|
+
errorLower.includes('format') ||
|
|
409
|
+
errorLower.includes('parse');
|
|
410
|
+
if (!contextMightHelp) {
|
|
411
|
+
return [decision.level]; // Don't escalate if error is unrelated to knowledge
|
|
412
|
+
}
|
|
413
|
+
// Progressive escalation based on starting point
|
|
414
|
+
switch (decision.level) {
|
|
415
|
+
case 'none':
|
|
416
|
+
return ['none', 'minimal', 'full'];
|
|
417
|
+
case 'minimal':
|
|
418
|
+
return ['minimal', 'full'];
|
|
419
|
+
case 'full':
|
|
420
|
+
return ['full']; // Already at max
|
|
421
|
+
default:
|
|
422
|
+
return ['none', 'minimal', 'full'];
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Export configuration for Python agent integration
|
|
427
|
+
*/
|
|
428
|
+
export function exportConfigForPython(instruction, metadata = {}) {
|
|
429
|
+
const decision = decideContextLevel(instruction, metadata);
|
|
430
|
+
const context = generateContext(decision);
|
|
431
|
+
return JSON.stringify({
|
|
432
|
+
level: decision.level,
|
|
433
|
+
sections: decision.sections,
|
|
434
|
+
reason: decision.reason,
|
|
435
|
+
estimatedOverheadMs: decision.estimatedOverheadMs,
|
|
436
|
+
taskType: decision.taskType,
|
|
437
|
+
timePressure: decision.timePressure,
|
|
438
|
+
historicalBenefit: decision.historicalBenefit,
|
|
439
|
+
context,
|
|
440
|
+
}, null, 2);
|
|
441
|
+
}
|
|
442
|
+
// Export main interface
|
|
443
|
+
export const HybridAdaptiveContext = {
|
|
444
|
+
classifyTask,
|
|
445
|
+
assessTimePressure,
|
|
446
|
+
getHistoricalBenefit,
|
|
447
|
+
recordOutcome,
|
|
448
|
+
decideContextLevel,
|
|
449
|
+
generateContext,
|
|
450
|
+
selectRelevantSections,
|
|
451
|
+
calculateOverhead,
|
|
452
|
+
getProgressiveContextLevels,
|
|
453
|
+
exportConfigForPython,
|
|
454
|
+
};
|
|
455
|
+
export default HybridAdaptiveContext;
|
|
456
|
+
//# sourceMappingURL=adaptive-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adaptive-context.js","sourceRoot":"","sources":["../../src/memory/adaptive-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AA+BxE,gEAAgE;AAChE,MAAM,sBAAsB,GAAG,IAAI,GAAG,CAAC;IACrC,WAAW;IACX,YAAY;IACZ,yBAAyB;IACzB,OAAO;IACP,YAAY;IACZ,cAAc;CACf,CAAC,CAAC;AAEH,mEAAmE;AACnE,MAAM,iBAAiB,GAAG;IACxB,UAAU;IACV,YAAY;IACZ,UAAU;IACV,SAAS;IACT,YAAY;IACZ,SAAS;IACT,UAAU;IACV,YAAY;IACZ,WAAW;IACX,aAAa;IACb,oBAAoB;IACpB,YAAY;IACZ,cAAc;IACd,QAAQ;CACT,CAAC;AAEF,+CAA+C;AAC/C,MAAM,qBAAqB,GAAG;IAC5B,UAAU;IACV,MAAM;IACN,OAAO;IACP,SAAS;IACT,KAAK;IACL,QAAQ;IACR,YAAY;IACZ,SAAS;IACT,KAAK;IACL,WAAW;IACX,UAAU;IACV,QAAQ;IACR,QAAQ;IACR,UAAU;IACV,UAAU;IACV,KAAK;IACL,SAAS;IACT,OAAO;IACP,UAAU;IACV,OAAO;IACP,WAAW;IACX,QAAQ;CACT,CAAC;AAEF,4DAA4D;AAC5D,MAAM,gBAAgB,GAGlB;IACF,QAAQ,EAAE;QACR,MAAM,EAAE,GAAG;QACX,QAAQ,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,M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
|
3
|
+
*
|
|
4
|
+
* @param numbers - An array of numbers to calculate the average of
|
|
5
|
+
* @returns The arithmetic mean of the numbers, or 0 if the array is empty
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* calculateAverage([1, 2, 3, 4, 5]); // Returns: 3
|
|
10
|
+
* calculateAverage([10, 20]); // Returns: 15
|
|
11
|
+
* calculateAverage([]); // Returns: 0
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function calculateAverage(numbers: number[]): number;
|
|
15
|
+
//# sourceMappingURL=calculate-average.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculate-average.d.ts","sourceRoot":"","sources":["../../src/utils/calculate-average.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAO1D"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the arithmetic mean of an array of numbers.
|
|
3
|
+
*
|
|
4
|
+
* @param numbers - An array of numbers to calculate the average of
|
|
5
|
+
* @returns The arithmetic mean of the numbers, or 0 if the array is empty
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* calculateAverage([1, 2, 3, 4, 5]); // Returns: 3
|
|
10
|
+
* calculateAverage([10, 20]); // Returns: 15
|
|
11
|
+
* calculateAverage([]); // Returns: 0
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function calculateAverage(numbers) {
|
|
15
|
+
if (numbers.length === 0) {
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
const sum = numbers.reduce((acc, num) => acc + num, 0);
|
|
19
|
+
return sum / numbers.length;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=calculate-average.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculate-average.js","sourceRoot":"","sources":["../../src/utils/calculate-average.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAiB;IAChD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,CAAC,CAAC;IACX,CAAC;IAED,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC;IACvD,OAAO,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Singleton configuration manager backed by an in-memory Map.
|
|
3
|
+
*/
|
|
4
|
+
export declare class ConfigManager {
|
|
5
|
+
private static instance;
|
|
6
|
+
private config;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new ConfigManager instance.
|
|
9
|
+
*/
|
|
10
|
+
private constructor();
|
|
11
|
+
/**
|
|
12
|
+
* Returns the singleton instance of the ConfigManager.
|
|
13
|
+
* @returns The singleton ConfigManager instance.
|
|
14
|
+
*/
|
|
15
|
+
static getInstance(): ConfigManager;
|
|
16
|
+
/**
|
|
17
|
+
* Gets a configuration value by key.
|
|
18
|
+
* @param key - Configuration key to retrieve.
|
|
19
|
+
* @returns The stored configuration value, or undefined if missing.
|
|
20
|
+
*/
|
|
21
|
+
get(key: string): any;
|
|
22
|
+
/**
|
|
23
|
+
* Sets a configuration value by key.
|
|
24
|
+
* @param key - Configuration key to set.
|
|
25
|
+
* @param value - Configuration value to store.
|
|
26
|
+
* @returns No return value.
|
|
27
|
+
*/
|
|
28
|
+
set(key: string, value: any): void;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=config-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-manager.d.ts","sourceRoot":"","sources":["../../src/utils/config-manager.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAgB;IACvC,OAAO,CAAC,MAAM,CAAmB;IAEjC;;OAEG;IACH,OAAO;IAIP;;;OAGG;WACW,WAAW,IAAI,aAAa;IAO1C;;;;OAIG;IACI,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG;IAI5B;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;CAG1C"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Singleton configuration manager backed by an in-memory Map.
|
|
3
|
+
*/
|
|
4
|
+
export class ConfigManager {
|
|
5
|
+
static instance;
|
|
6
|
+
config;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new ConfigManager instance.
|
|
9
|
+
*/
|
|
10
|
+
constructor() {
|
|
11
|
+
this.config = new Map();
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Returns the singleton instance of the ConfigManager.
|
|
15
|
+
* @returns The singleton ConfigManager instance.
|
|
16
|
+
*/
|
|
17
|
+
static getInstance() {
|
|
18
|
+
if (!ConfigManager.instance) {
|
|
19
|
+
ConfigManager.instance = new ConfigManager();
|
|
20
|
+
}
|
|
21
|
+
return ConfigManager.instance;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets a configuration value by key.
|
|
25
|
+
* @param key - Configuration key to retrieve.
|
|
26
|
+
* @returns The stored configuration value, or undefined if missing.
|
|
27
|
+
*/
|
|
28
|
+
get(key) {
|
|
29
|
+
return this.config.get(key);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Sets a configuration value by key.
|
|
33
|
+
* @param key - Configuration key to set.
|
|
34
|
+
* @param value - Configuration value to store.
|
|
35
|
+
* @returns No return value.
|
|
36
|
+
*/
|
|
37
|
+
set(key, value) {
|
|
38
|
+
this.config.set(key, value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=config-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-manager.js","sourceRoot":"","sources":["../../src/utils/config-manager.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,aAAa;IAChB,MAAM,CAAC,QAAQ,CAAgB;IAC/B,MAAM,CAAmB;IAEjC;;OAEG;IACH;QACE,IAAI,CAAC,MAAM,GAAG,IAAI,GAAG,EAAe,CAAC;IACvC,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,WAAW;QACvB,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE,CAAC;YAC5B,aAAa,CAAC,QAAQ,GAAG,IAAI,aAAa,EAAE,CAAC;QAC/C,CAAC;QACD,OAAO,aAAa,CAAC,QAAQ,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACI,GAAG,CAAC,GAAW;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,GAAW,EAAE,KAAU;QAChC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAC9B,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-with-retry.d.ts","sourceRoot":"","sources":["../../src/utils/fetch-with-retry.ts"],"names":[],"mappings":"AAAA,wBAAsB,cAAc,CAClC,GAAG,EAAE,MAAM,EACX,WAAW,CAAC,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACtD,OAAO,CAAC,QAAQ,CAAC,CA0FnB"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export async function fetchWithRetry(url, retryConfig) {
|
|
2
|
+
const config = {
|
|
3
|
+
maxRetries: retryConfig?.maxRetries ?? 3,
|
|
4
|
+
backoffMs: retryConfig?.backoffMs ?? 1000,
|
|
5
|
+
};
|
|
6
|
+
let lastError = null;
|
|
7
|
+
let lastStatusCode;
|
|
8
|
+
for (let attempt = 0; attempt <= config.maxRetries; attempt++) {
|
|
9
|
+
try {
|
|
10
|
+
if (attempt > 0) {
|
|
11
|
+
const delay = config.backoffMs * Math.pow(2, attempt - 1);
|
|
12
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
13
|
+
}
|
|
14
|
+
const response = await fetch(url);
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
lastStatusCode = response.status;
|
|
17
|
+
const isRetryable = response.status >= 500 ||
|
|
18
|
+
response.status === 408 ||
|
|
19
|
+
response.status === 429;
|
|
20
|
+
if (isRetryable && attempt < config.maxRetries) {
|
|
21
|
+
lastError = new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const httpError = new Error(`HTTP error ${response.status}: ${response.statusText}`);
|
|
25
|
+
httpError.name = 'FetchRetryError';
|
|
26
|
+
httpError.url = url;
|
|
27
|
+
httpError.attempts = attempt + 1;
|
|
28
|
+
httpError.statusCode = response.status;
|
|
29
|
+
httpError.lastError = null;
|
|
30
|
+
throw httpError;
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (error instanceof Error && error.name === 'FetchRetryError') {
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
if (error instanceof Error) {
|
|
39
|
+
lastError = error;
|
|
40
|
+
if (attempt < config.maxRetries) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const finalError = new Error(`Fetch failed after ${attempt + 1} attempts: ${error instanceof Error ? error.message : String(error)}`);
|
|
45
|
+
finalError.name = 'FetchRetryError';
|
|
46
|
+
finalError.url = url;
|
|
47
|
+
finalError.attempts = attempt + 1;
|
|
48
|
+
finalError.statusCode = lastStatusCode;
|
|
49
|
+
finalError.lastError = lastError;
|
|
50
|
+
throw finalError;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const exhaustedError = new Error(`All ${config.maxRetries + 1} attempts failed for ${url}`);
|
|
54
|
+
exhaustedError.name = 'FetchRetryError';
|
|
55
|
+
exhaustedError.url = url;
|
|
56
|
+
exhaustedError.attempts = config.maxRetries + 1;
|
|
57
|
+
exhaustedError.statusCode = lastStatusCode;
|
|
58
|
+
exhaustedError.lastError = lastError;
|
|
59
|
+
throw exhaustedError;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=fetch-with-retry.js.map
|