@cogitator-ai/self-modifying 0.1.0 → 2.0.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 +60 -50
- package/dist/architecture-evolution/capability-analyzer.d.ts.map +1 -1
- package/dist/architecture-evolution/capability-analyzer.js +105 -19
- package/dist/architecture-evolution/capability-analyzer.js.map +1 -1
- package/dist/architecture-evolution/evolution-strategy.d.ts.map +1 -1
- package/dist/architecture-evolution/evolution-strategy.js +2 -6
- package/dist/architecture-evolution/evolution-strategy.js.map +1 -1
- package/dist/architecture-evolution/index.d.ts +1 -1
- package/dist/architecture-evolution/index.d.ts.map +1 -1
- package/dist/architecture-evolution/index.js +1 -1
- package/dist/architecture-evolution/index.js.map +1 -1
- package/dist/architecture-evolution/parameter-optimizer.d.ts.map +1 -1
- package/dist/architecture-evolution/parameter-optimizer.js +1 -1
- package/dist/architecture-evolution/parameter-optimizer.js.map +1 -1
- package/dist/architecture-evolution/prompts.d.ts.map +1 -1
- package/dist/architecture-evolution/prompts.js +3 -3
- package/dist/architecture-evolution/prompts.js.map +1 -1
- package/dist/constraints/index.d.ts +1 -1
- package/dist/constraints/index.d.ts.map +1 -1
- package/dist/constraints/index.js +1 -1
- package/dist/constraints/index.js.map +1 -1
- package/dist/constraints/modification-validator.d.ts.map +1 -1
- package/dist/constraints/modification-validator.js +6 -7
- package/dist/constraints/modification-validator.js.map +1 -1
- package/dist/constraints/rollback-manager.d.ts.map +1 -1
- package/dist/constraints/rollback-manager.js.map +1 -1
- package/dist/constraints/safety-constraints.d.ts.map +1 -1
- package/dist/constraints/safety-constraints.js +1 -3
- package/dist/constraints/safety-constraints.js.map +1 -1
- package/dist/events/event-emitter.js +1 -1
- package/dist/events/event-emitter.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/meta-reasoning/meta-reasoner.d.ts.map +1 -1
- package/dist/meta-reasoning/meta-reasoner.js +6 -11
- package/dist/meta-reasoning/meta-reasoner.js.map +1 -1
- package/dist/meta-reasoning/observation-collector.d.ts.map +1 -1
- package/dist/meta-reasoning/observation-collector.js +1 -3
- package/dist/meta-reasoning/observation-collector.js.map +1 -1
- package/dist/meta-reasoning/prompts.d.ts.map +1 -1
- package/dist/meta-reasoning/prompts.js +5 -3
- package/dist/meta-reasoning/prompts.js.map +1 -1
- package/dist/meta-reasoning/strategy-selector.d.ts.map +1 -1
- package/dist/meta-reasoning/strategy-selector.js +3 -1
- package/dist/meta-reasoning/strategy-selector.js.map +1 -1
- package/dist/self-modifying-agent.d.ts.map +1 -1
- package/dist/self-modifying-agent.js +27 -23
- package/dist/self-modifying-agent.js.map +1 -1
- package/dist/tool-generation/gap-analyzer.d.ts.map +1 -1
- package/dist/tool-generation/gap-analyzer.js +5 -2
- package/dist/tool-generation/gap-analyzer.js.map +1 -1
- package/dist/tool-generation/generated-tool-store.d.ts.map +1 -1
- package/dist/tool-generation/generated-tool-store.js.map +1 -1
- package/dist/tool-generation/prompts.d.ts.map +1 -1
- package/dist/tool-generation/prompts.js +8 -18
- package/dist/tool-generation/prompts.js.map +1 -1
- package/dist/tool-generation/tool-generator.d.ts +1 -1
- package/dist/tool-generation/tool-generator.d.ts.map +1 -1
- package/dist/tool-generation/tool-generator.js +3 -2
- package/dist/tool-generation/tool-generator.js.map +1 -1
- package/dist/tool-generation/tool-sandbox.d.ts.map +1 -1
- package/dist/tool-generation/tool-sandbox.js +8 -9
- package/dist/tool-generation/tool-sandbox.js.map +1 -1
- package/dist/tool-generation/tool-validator.d.ts.map +1 -1
- package/dist/tool-generation/tool-validator.js +5 -5
- package/dist/tool-generation/tool-validator.js.map +1 -1
- package/package.json +4 -4
- package/src/__tests__/architecture-evolution.test.ts +131 -30
- package/src/__tests__/constraints.test.ts +61 -46
- package/src/__tests__/index.test.ts +4 -17
- package/src/__tests__/meta-reasoning.test.ts +246 -155
- package/src/__tests__/tool-generation.test.ts +26 -7
- package/src/architecture-evolution/capability-analyzer.ts +113 -31
- package/src/architecture-evolution/evolution-strategy.ts +20 -13
- package/src/architecture-evolution/index.ts +1 -4
- package/src/architecture-evolution/parameter-optimizer.ts +44 -25
- package/src/architecture-evolution/prompts.ts +17 -18
- package/src/constraints/index.ts +1 -4
- package/src/constraints/modification-validator.ts +10 -23
- package/src/constraints/rollback-manager.ts +1 -3
- package/src/constraints/safety-constraints.ts +1 -3
- package/src/events/event-emitter.ts +1 -1
- package/src/index.ts +6 -5
- package/src/meta-reasoning/meta-reasoner.ts +10 -17
- package/src/meta-reasoning/observation-collector.ts +3 -12
- package/src/meta-reasoning/prompts.ts +10 -10
- package/src/meta-reasoning/strategy-selector.ts +5 -1
- package/src/self-modifying-agent.ts +38 -45
- package/src/tool-generation/gap-analyzer.ts +19 -15
- package/src/tool-generation/generated-tool-store.ts +5 -8
- package/src/tool-generation/prompts.ts +8 -18
- package/src/tool-generation/tool-generator.ts +18 -14
- package/src/tool-generation/tool-sandbox.ts +12 -23
- package/src/tool-generation/tool-validator.ts +22 -18
|
@@ -14,7 +14,11 @@ import type {
|
|
|
14
14
|
MetaOpportunity,
|
|
15
15
|
} from '@cogitator-ai/types';
|
|
16
16
|
import { DEFAULT_META_REASONING_CONFIG } from '@cogitator-ai/types';
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
ObservationCollector,
|
|
19
|
+
type ActionRecord,
|
|
20
|
+
type ObservationContext,
|
|
21
|
+
} from './observation-collector';
|
|
18
22
|
import { StrategySelector } from './strategy-selector';
|
|
19
23
|
import {
|
|
20
24
|
buildMetaAssessmentPrompt,
|
|
@@ -98,8 +102,7 @@ export class MetaReasoner {
|
|
|
98
102
|
switch (trigger) {
|
|
99
103
|
case 'iteration_complete':
|
|
100
104
|
return (
|
|
101
|
-
context.iteration > 0 &&
|
|
102
|
-
context.iteration % this.config.triggerAfterIterations === 0
|
|
105
|
+
context.iteration > 0 && context.iteration % this.config.triggerAfterIterations === 0
|
|
103
106
|
);
|
|
104
107
|
|
|
105
108
|
case 'confidence_drop':
|
|
@@ -235,10 +238,7 @@ export class MetaReasoner {
|
|
|
235
238
|
|
|
236
239
|
switch (recommendation.action) {
|
|
237
240
|
case 'switch_mode':
|
|
238
|
-
if (
|
|
239
|
-
recommendation.newMode &&
|
|
240
|
-
this.config.allowedModes.includes(recommendation.newMode)
|
|
241
|
-
) {
|
|
241
|
+
if (recommendation.newMode && this.config.allowedModes.includes(recommendation.newMode)) {
|
|
242
242
|
newConfig = this.getModeConfig(recommendation.newMode);
|
|
243
243
|
adaptationType = 'mode_switch';
|
|
244
244
|
this.currentMode.set(runId, recommendation.newMode);
|
|
@@ -286,14 +286,11 @@ export class MetaReasoner {
|
|
|
286
286
|
const runAdaptations = this.adaptations.get(runId) ?? [];
|
|
287
287
|
const lastAdaptation = runAdaptations[runAdaptations.length - 1];
|
|
288
288
|
|
|
289
|
-
if (!lastAdaptation
|
|
289
|
+
if (!lastAdaptation?.rollbackable) {
|
|
290
290
|
return null;
|
|
291
291
|
}
|
|
292
292
|
|
|
293
|
-
if (
|
|
294
|
-
lastAdaptation.rollbackDeadline &&
|
|
295
|
-
Date.now() > lastAdaptation.rollbackDeadline
|
|
296
|
-
) {
|
|
293
|
+
if (lastAdaptation.rollbackDeadline && Date.now() > lastAdaptation.rollbackDeadline) {
|
|
297
294
|
return null;
|
|
298
295
|
}
|
|
299
296
|
|
|
@@ -328,11 +325,7 @@ export class MetaReasoner {
|
|
|
328
325
|
if (adaptation) {
|
|
329
326
|
adaptation.outcome = outcome;
|
|
330
327
|
|
|
331
|
-
if (
|
|
332
|
-
this.config.rollbackOnDecline &&
|
|
333
|
-
!outcome.improved &&
|
|
334
|
-
adaptation.rollbackable
|
|
335
|
-
) {
|
|
328
|
+
if (this.config.rollbackOnDecline && !outcome.improved && adaptation.rollbackable) {
|
|
336
329
|
this.rollback(runId);
|
|
337
330
|
}
|
|
338
331
|
}
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
MetaObservation,
|
|
3
|
-
ReasoningMode,
|
|
4
|
-
Insight,
|
|
5
|
-
} from '@cogitator-ai/types';
|
|
1
|
+
import type { MetaObservation, ReasoningMode, Insight } from '@cogitator-ai/types';
|
|
6
2
|
|
|
7
3
|
export interface ActionRecord {
|
|
8
4
|
type: string;
|
|
@@ -56,10 +52,7 @@ export class ObservationCollector {
|
|
|
56
52
|
}
|
|
57
53
|
}
|
|
58
54
|
|
|
59
|
-
collect(
|
|
60
|
-
context: ObservationContext,
|
|
61
|
-
insights: Insight[]
|
|
62
|
-
): MetaObservation {
|
|
55
|
+
collect(context: ObservationContext, insights: Insight[]): MetaObservation {
|
|
63
56
|
const { runId, iteration, goal, currentMode } = context;
|
|
64
57
|
const prevObservations = this.observations.get(runId) ?? [];
|
|
65
58
|
const lastObs = prevObservations[prevObservations.length - 1];
|
|
@@ -69,9 +62,7 @@ export class ObservationCollector {
|
|
|
69
62
|
const currentConfidence = confidenceHist[confidenceHist.length - 1] ?? 0.5;
|
|
70
63
|
const progressScore = this.calculateProgress(actions);
|
|
71
64
|
const progressDelta = lastObs ? progressScore - lastObs.progressScore : 0;
|
|
72
|
-
const stagnationCount = progressDelta < 0.05
|
|
73
|
-
? (lastObs?.stagnationCount ?? 0) + 1
|
|
74
|
-
: 0;
|
|
65
|
+
const stagnationCount = progressDelta < 0.05 ? (lastObs?.stagnationCount ?? 0) + 1 : 0;
|
|
75
66
|
const repetitionScore = this.calculateRepetition(actions);
|
|
76
67
|
const toolSuccessRate = this.calculateToolSuccessRate(actions);
|
|
77
68
|
const confidenceTrend = this.calculateTrend(confidenceHist);
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
MetaObservation,
|
|
3
|
-
ReasoningMode,
|
|
4
|
-
ReasoningModeConfig,
|
|
5
|
-
} from '@cogitator-ai/types';
|
|
1
|
+
import type { MetaObservation, ReasoningMode, ReasoningModeConfig } from '@cogitator-ai/types';
|
|
6
2
|
|
|
7
3
|
export function buildMetaAssessmentPrompt(
|
|
8
4
|
observation: MetaObservation,
|
|
@@ -44,10 +40,14 @@ Iterations remaining: ${observation.iterationsRemaining}
|
|
|
44
40
|
${(observation.recentActions ?? []).map((a) => `- ${a.type}: ${a.toolName ?? 'N/A'} ${a.error ? '(ERROR: ' + a.error + ')' : ''}`).join('\n') || 'None'}
|
|
45
41
|
|
|
46
42
|
## Recent Insights
|
|
47
|
-
${
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
${
|
|
44
|
+
(observation.recentInsights ?? ([] as Array<{ type?: string; content?: string }>))
|
|
45
|
+
.map((i) => {
|
|
46
|
+
const insight = i as { type?: string; content?: string };
|
|
47
|
+
return `- [${insight.type ?? 'insight'}] ${insight.content ?? ''}`;
|
|
48
|
+
})
|
|
49
|
+
.join('\n') || 'None'
|
|
50
|
+
}
|
|
51
51
|
|
|
52
52
|
## Available Modes for Switching
|
|
53
53
|
${context.allowedModes.map((m) => `- ${m}`).join('\n')}
|
|
@@ -111,7 +111,7 @@ export interface ParsedAssessment {
|
|
|
111
111
|
|
|
112
112
|
export function parseMetaAssessmentResponse(content: string): ParsedAssessment | null {
|
|
113
113
|
try {
|
|
114
|
-
const jsonMatch =
|
|
114
|
+
const jsonMatch = /\{[\s\S]*\}/.exec(content);
|
|
115
115
|
if (!jsonMatch) return null;
|
|
116
116
|
return JSON.parse(jsonMatch[0]) as ParsedAssessment;
|
|
117
117
|
} catch {
|
|
@@ -78,7 +78,11 @@ export class StrategySelector {
|
|
|
78
78
|
score += 0.2;
|
|
79
79
|
reasons.push('Thorough for complex tasks');
|
|
80
80
|
}
|
|
81
|
-
if (
|
|
81
|
+
if (
|
|
82
|
+
!profile.timeConstraint ||
|
|
83
|
+
profile.timeConstraint === 'none' ||
|
|
84
|
+
profile.timeConstraint === 'relaxed'
|
|
85
|
+
) {
|
|
82
86
|
score += 0.1;
|
|
83
87
|
reasons.push('Has time for systematic approach');
|
|
84
88
|
}
|
|
@@ -154,7 +154,7 @@ export class SelfModifyingAgent {
|
|
|
154
154
|
checkpoints: [],
|
|
155
155
|
};
|
|
156
156
|
|
|
157
|
-
this.emitter.emit({
|
|
157
|
+
void this.emitter.emit({
|
|
158
158
|
type: 'run_started',
|
|
159
159
|
runId,
|
|
160
160
|
timestamp: new Date(),
|
|
@@ -172,7 +172,7 @@ export class SelfModifyingAgent {
|
|
|
172
172
|
|
|
173
173
|
const modeConfig = this.metaReasoner.initializeRun(runId);
|
|
174
174
|
|
|
175
|
-
this.emitter.emit({
|
|
175
|
+
void this.emitter.emit({
|
|
176
176
|
type: 'strategy_changed',
|
|
177
177
|
runId,
|
|
178
178
|
timestamp: new Date(),
|
|
@@ -192,7 +192,7 @@ export class SelfModifyingAgent {
|
|
|
192
192
|
finalConfig: { ...this.currentContext.currentConfig },
|
|
193
193
|
};
|
|
194
194
|
|
|
195
|
-
this.emitter.emit({
|
|
195
|
+
void this.emitter.emit({
|
|
196
196
|
type: 'run_completed',
|
|
197
197
|
runId,
|
|
198
198
|
timestamp: new Date(),
|
|
@@ -205,7 +205,7 @@ export class SelfModifyingAgent {
|
|
|
205
205
|
|
|
206
206
|
return result;
|
|
207
207
|
} catch (error) {
|
|
208
|
-
this.emitter.emit({
|
|
208
|
+
void this.emitter.emit({
|
|
209
209
|
type: 'run_completed',
|
|
210
210
|
runId,
|
|
211
211
|
timestamp: new Date(),
|
|
@@ -232,7 +232,7 @@ export class SelfModifyingAgent {
|
|
|
232
232
|
if (result.success && result.tool) {
|
|
233
233
|
await this.toolStore.save(result.tool);
|
|
234
234
|
|
|
235
|
-
this.emitter.emit({
|
|
235
|
+
void this.emitter.emit({
|
|
236
236
|
type: 'tool_generation_completed',
|
|
237
237
|
runId: this.currentContext?.runId || 'manual',
|
|
238
238
|
timestamp: new Date(),
|
|
@@ -247,7 +247,7 @@ export class SelfModifyingAgent {
|
|
|
247
247
|
return result.tool;
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
-
this.emitter.emit({
|
|
250
|
+
void this.emitter.emit({
|
|
251
251
|
type: 'tool_generation_completed',
|
|
252
252
|
runId: this.currentContext?.runId || 'manual',
|
|
253
253
|
timestamp: new Date(),
|
|
@@ -262,11 +262,7 @@ export class SelfModifyingAgent {
|
|
|
262
262
|
return null;
|
|
263
263
|
}
|
|
264
264
|
|
|
265
|
-
async recordToolUsage(
|
|
266
|
-
toolId: string,
|
|
267
|
-
success: boolean,
|
|
268
|
-
executionTime: number
|
|
269
|
-
): Promise<void> {
|
|
265
|
+
async recordToolUsage(toolId: string, success: boolean, executionTime: number): Promise<void> {
|
|
270
266
|
await this.toolStore.recordUsage({
|
|
271
267
|
toolId,
|
|
272
268
|
timestamp: new Date(),
|
|
@@ -299,7 +295,7 @@ export class SelfModifyingAgent {
|
|
|
299
295
|
|
|
300
296
|
this.currentContext.checkpoints.push(checkpoint);
|
|
301
297
|
|
|
302
|
-
this.emitter.emit({
|
|
298
|
+
void this.emitter.emit({
|
|
303
299
|
type: 'checkpoint_created',
|
|
304
300
|
runId: this.currentContext.runId,
|
|
305
301
|
timestamp: new Date(),
|
|
@@ -317,7 +313,7 @@ export class SelfModifyingAgent {
|
|
|
317
313
|
this.currentContext.tools = restored.tools;
|
|
318
314
|
}
|
|
319
315
|
|
|
320
|
-
this.emitter.emit({
|
|
316
|
+
void this.emitter.emit({
|
|
321
317
|
type: 'rollback_performed',
|
|
322
318
|
runId: this.currentContext?.runId || 'manual',
|
|
323
319
|
timestamp: new Date(),
|
|
@@ -333,12 +329,7 @@ export class SelfModifyingAgent {
|
|
|
333
329
|
private async ensureInitialized(): Promise<void> {
|
|
334
330
|
if (this.isInitialized) return;
|
|
335
331
|
|
|
336
|
-
|
|
337
|
-
for (const tool of activeToos) {
|
|
338
|
-
if (tool.validationScore && tool.validationScore > 0.8) {
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
}
|
|
332
|
+
await this.toolStore.list({ status: 'active' });
|
|
342
333
|
|
|
343
334
|
this.isInitialized = true;
|
|
344
335
|
}
|
|
@@ -364,7 +355,14 @@ export class SelfModifyingAgent {
|
|
|
364
355
|
metaReasoning: {
|
|
365
356
|
enabled: true,
|
|
366
357
|
defaultMode: 'analytical',
|
|
367
|
-
allowedModes: [
|
|
358
|
+
allowedModes: [
|
|
359
|
+
'analytical',
|
|
360
|
+
'creative',
|
|
361
|
+
'systematic',
|
|
362
|
+
'intuitive',
|
|
363
|
+
'reflective',
|
|
364
|
+
'exploratory',
|
|
365
|
+
],
|
|
368
366
|
modeProfiles: {
|
|
369
367
|
analytical: { mode: 'analytical', temperature: 0.3, depth: 3 },
|
|
370
368
|
creative: { mode: 'creative', temperature: 0.9, depth: 2 },
|
|
@@ -413,7 +411,10 @@ export class SelfModifyingAgent {
|
|
|
413
411
|
enabled: partial.enabled ?? defaults.enabled,
|
|
414
412
|
toolGeneration: { ...defaults.toolGeneration, ...partial.toolGeneration },
|
|
415
413
|
metaReasoning: { ...defaults.metaReasoning, ...partial.metaReasoning },
|
|
416
|
-
architectureEvolution: {
|
|
414
|
+
architectureEvolution: {
|
|
415
|
+
...defaults.architectureEvolution,
|
|
416
|
+
...partial.architectureEvolution,
|
|
417
|
+
},
|
|
417
418
|
constraints: { ...defaults.constraints, ...partial.constraints },
|
|
418
419
|
};
|
|
419
420
|
}
|
|
@@ -439,7 +440,7 @@ export class SelfModifyingAgent {
|
|
|
439
440
|
if (validation.valid) {
|
|
440
441
|
this.currentContext.currentConfig = result.recommendedConfig;
|
|
441
442
|
|
|
442
|
-
this.emitter.emit({
|
|
443
|
+
void this.emitter.emit({
|
|
443
444
|
type: 'architecture_evolved',
|
|
444
445
|
runId: this.currentContext.runId,
|
|
445
446
|
timestamp: new Date(),
|
|
@@ -451,23 +452,18 @@ export class SelfModifyingAgent {
|
|
|
451
452
|
});
|
|
452
453
|
}
|
|
453
454
|
}
|
|
454
|
-
} catch {
|
|
455
|
-
|
|
456
|
-
}
|
|
455
|
+
} catch {}
|
|
457
456
|
}
|
|
458
457
|
|
|
459
458
|
private async analyzeAndGenerateTools(input: string): Promise<void> {
|
|
460
459
|
if (!this.currentContext) return;
|
|
461
460
|
|
|
462
461
|
try {
|
|
463
|
-
const analysis = await this.gapAnalyzer.analyze(
|
|
464
|
-
input,
|
|
465
|
-
this.currentContext.tools
|
|
466
|
-
);
|
|
462
|
+
const analysis = await this.gapAnalyzer.analyze(input, this.currentContext.tools);
|
|
467
463
|
|
|
468
464
|
for (const gap of analysis.gaps) {
|
|
469
465
|
if (gap.confidence >= this.config.toolGeneration.minConfidenceForGeneration) {
|
|
470
|
-
this.emitter.emit({
|
|
466
|
+
void this.emitter.emit({
|
|
471
467
|
type: 'tool_generation_started',
|
|
472
468
|
runId: this.currentContext.runId,
|
|
473
469
|
timestamp: new Date(),
|
|
@@ -483,15 +479,10 @@ export class SelfModifyingAgent {
|
|
|
483
479
|
}
|
|
484
480
|
}
|
|
485
481
|
}
|
|
486
|
-
} catch {
|
|
487
|
-
|
|
488
|
-
}
|
|
482
|
+
} catch {}
|
|
489
483
|
}
|
|
490
484
|
|
|
491
|
-
private async executeWithMetaReasoning(
|
|
492
|
-
input: string,
|
|
493
|
-
runId: string
|
|
494
|
-
): Promise<string> {
|
|
485
|
+
private async executeWithMetaReasoning(input: string, runId: string): Promise<string> {
|
|
495
486
|
if (!this.currentContext) {
|
|
496
487
|
throw new Error('No active run context');
|
|
497
488
|
}
|
|
@@ -520,15 +511,17 @@ export class SelfModifyingAgent {
|
|
|
520
511
|
|
|
521
512
|
this.currentContext.observations.push(observation);
|
|
522
513
|
|
|
523
|
-
if (
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
514
|
+
if (
|
|
515
|
+
this.metaReasoner.shouldTrigger(runId, 'periodic', {
|
|
516
|
+
iteration,
|
|
517
|
+
confidence: this.estimateConfidence(output),
|
|
518
|
+
progressDelta: output.length > 0 ? 0.1 : 0,
|
|
519
|
+
stagnationCount: 0,
|
|
520
|
+
})
|
|
521
|
+
) {
|
|
529
522
|
const assessment = await this.metaReasoner.assess(observation);
|
|
530
523
|
|
|
531
|
-
this.emitter.emit({
|
|
524
|
+
void this.emitter.emit({
|
|
532
525
|
type: 'meta_assessment',
|
|
533
526
|
runId,
|
|
534
527
|
timestamp: new Date(),
|
|
@@ -544,7 +537,7 @@ export class SelfModifyingAgent {
|
|
|
544
537
|
if (adaptation) {
|
|
545
538
|
this.currentContext.adaptations.push(adaptation);
|
|
546
539
|
|
|
547
|
-
this.emitter.emit({
|
|
540
|
+
void this.emitter.emit({
|
|
548
541
|
type: 'strategy_changed',
|
|
549
542
|
runId,
|
|
550
543
|
timestamp: new Date(),
|
|
@@ -41,22 +41,21 @@ export class GapAnalyzer {
|
|
|
41
41
|
description: t.description,
|
|
42
42
|
}));
|
|
43
43
|
|
|
44
|
-
const prompt = buildGapAnalysisPrompt(
|
|
45
|
-
userIntent,
|
|
46
|
-
toolSummaries,
|
|
47
|
-
context?.failedAttempts
|
|
48
|
-
);
|
|
44
|
+
const prompt = buildGapAnalysisPrompt(userIntent, toolSummaries, context?.failedAttempts);
|
|
49
45
|
|
|
50
|
-
const response = await this.callLLM(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
const response = await this.callLLM(
|
|
47
|
+
[
|
|
48
|
+
{
|
|
49
|
+
role: 'system',
|
|
50
|
+
content: `You are a capability analyzer for AI agents.
|
|
54
51
|
Identify gaps between user intent and available tools.
|
|
55
52
|
Be conservative - only report gaps when truly necessary.
|
|
56
53
|
Consider tool composition before suggesting new tools.`,
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
},
|
|
55
|
+
{ role: 'user', content: prompt },
|
|
56
|
+
],
|
|
57
|
+
0.3
|
|
58
|
+
);
|
|
60
59
|
|
|
61
60
|
const parsed = parseGapAnalysisResponse(response.content);
|
|
62
61
|
const filteredGaps = this.filterAndPrioritizeGaps(
|
|
@@ -149,7 +148,7 @@ Consider tool composition before suggesting new tools.`,
|
|
|
149
148
|
return 1.0;
|
|
150
149
|
}
|
|
151
150
|
|
|
152
|
-
const
|
|
151
|
+
const _intentWords = userIntent.toLowerCase().split(/\s+/).length;
|
|
153
152
|
const toolDescriptionWords = availableTools
|
|
154
153
|
.map((t) => t.description.toLowerCase().split(/\s+/))
|
|
155
154
|
.flat();
|
|
@@ -186,7 +185,9 @@ Consider tool composition before suggesting new tools.`,
|
|
|
186
185
|
for (const [action, related] of Object.entries(keywords)) {
|
|
187
186
|
if (intentLower.includes(action)) {
|
|
188
187
|
const relatedTools = availableTools.filter((t) =>
|
|
189
|
-
related.some(
|
|
188
|
+
related.some(
|
|
189
|
+
(r) => t.name.toLowerCase().includes(r) || t.description.toLowerCase().includes(r)
|
|
190
|
+
)
|
|
190
191
|
);
|
|
191
192
|
|
|
192
193
|
if (relatedTools.length >= 2) {
|
|
@@ -214,7 +215,10 @@ Consider tool composition before suggesting new tools.`,
|
|
|
214
215
|
}
|
|
215
216
|
|
|
216
217
|
private buildCacheKey(userIntent: string, tools: Tool[]): string {
|
|
217
|
-
const toolSignature = tools
|
|
218
|
+
const toolSignature = tools
|
|
219
|
+
.map((t) => t.name)
|
|
220
|
+
.sort()
|
|
221
|
+
.join(',');
|
|
218
222
|
return `${userIntent.slice(0, 100)}|${toolSignature}`;
|
|
219
223
|
}
|
|
220
224
|
|
|
@@ -73,10 +73,7 @@ export class InMemoryGeneratedToolStore implements IGeneratedToolStore {
|
|
|
73
73
|
return deleted;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
async updateStatus(
|
|
77
|
-
id: string,
|
|
78
|
-
status: GeneratedTool['status']
|
|
79
|
-
): Promise<boolean> {
|
|
76
|
+
async updateStatus(id: string, status: GeneratedTool['status']): Promise<boolean> {
|
|
80
77
|
const tool = this.tools.get(id);
|
|
81
78
|
if (!tool) return false;
|
|
82
79
|
|
|
@@ -121,7 +118,9 @@ export class InMemoryGeneratedToolStore implements IGeneratedToolStore {
|
|
|
121
118
|
};
|
|
122
119
|
}
|
|
123
120
|
|
|
124
|
-
async getTopTools(
|
|
121
|
+
async getTopTools(
|
|
122
|
+
limit: number = 10
|
|
123
|
+
): Promise<Array<{ tool: GeneratedTool; metrics: ToolMetrics }>> {
|
|
125
124
|
const result: Array<{ tool: GeneratedTool; metrics: ToolMetrics }> = [];
|
|
126
125
|
|
|
127
126
|
for (const tool of this.tools.values()) {
|
|
@@ -149,9 +148,7 @@ export class InMemoryGeneratedToolStore implements IGeneratedToolStore {
|
|
|
149
148
|
for (const tool of this.tools.values()) {
|
|
150
149
|
if (tool.status === 'deprecated') continue;
|
|
151
150
|
|
|
152
|
-
const toolWords = new Set(
|
|
153
|
-
`${tool.name} ${tool.description}`.toLowerCase().split(/\s+/)
|
|
154
|
-
);
|
|
151
|
+
const toolWords = new Set(`${tool.name} ${tool.description}`.toLowerCase().split(/\s+/));
|
|
155
152
|
|
|
156
153
|
let matchCount = 0;
|
|
157
154
|
for (const word of descWords) {
|
|
@@ -26,9 +26,7 @@ export function buildGapAnalysisPrompt(
|
|
|
26
26
|
availableTools: Array<{ name: string; description: string }>,
|
|
27
27
|
failedAttempts?: string[]
|
|
28
28
|
): string {
|
|
29
|
-
const toolList = availableTools
|
|
30
|
-
.map((t) => `- ${t.name}: ${t.description}`)
|
|
31
|
-
.join('\n');
|
|
29
|
+
const toolList = availableTools.map((t) => `- ${t.name}: ${t.description}`).join('\n');
|
|
32
30
|
|
|
33
31
|
const failureContext = failedAttempts?.length
|
|
34
32
|
? `\n\nPrevious failed attempts:\n${failedAttempts.map((f) => `- ${f}`).join('\n')}`
|
|
@@ -207,7 +205,7 @@ export function parseGapAnalysisResponse(response: string): {
|
|
|
207
205
|
canProceed: boolean;
|
|
208
206
|
alternativeApproach?: string;
|
|
209
207
|
} {
|
|
210
|
-
const jsonMatch =
|
|
208
|
+
const jsonMatch = /\{[\s\S]*\}/.exec(response);
|
|
211
209
|
if (!jsonMatch) {
|
|
212
210
|
return { hasGap: false, gaps: [], canProceed: true };
|
|
213
211
|
}
|
|
@@ -240,7 +238,7 @@ export function parseGapAnalysisResponse(response: string): {
|
|
|
240
238
|
}
|
|
241
239
|
|
|
242
240
|
export function parseToolGenerationResponse(response: string): GeneratedTool | null {
|
|
243
|
-
const jsonMatch =
|
|
241
|
+
const jsonMatch = /\{[\s\S]*\}/.exec(response);
|
|
244
242
|
if (!jsonMatch) {
|
|
245
243
|
return null;
|
|
246
244
|
}
|
|
@@ -270,7 +268,7 @@ export function parseToolGenerationResponse(response: string): GeneratedTool | n
|
|
|
270
268
|
}
|
|
271
269
|
|
|
272
270
|
export function parseValidationResponse(response: string): ToolValidationResult | null {
|
|
273
|
-
const jsonMatch =
|
|
271
|
+
const jsonMatch = /\{[\s\S]*\}/.exec(response);
|
|
274
272
|
if (!jsonMatch) {
|
|
275
273
|
return null;
|
|
276
274
|
}
|
|
@@ -280,18 +278,10 @@ export function parseValidationResponse(response: string): ToolValidationResult
|
|
|
280
278
|
|
|
281
279
|
return {
|
|
282
280
|
isValid: Boolean(parsed.isValid),
|
|
283
|
-
securityIssues: Array.isArray(parsed.securityIssues)
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
? parsed.logicIssues.map(String)
|
|
288
|
-
: [],
|
|
289
|
-
edgeCases: Array.isArray(parsed.edgeCases)
|
|
290
|
-
? parsed.edgeCases.map(String)
|
|
291
|
-
: [],
|
|
292
|
-
suggestions: Array.isArray(parsed.suggestions)
|
|
293
|
-
? parsed.suggestions.map(String)
|
|
294
|
-
: [],
|
|
281
|
+
securityIssues: Array.isArray(parsed.securityIssues) ? parsed.securityIssues.map(String) : [],
|
|
282
|
+
logicIssues: Array.isArray(parsed.logicIssues) ? parsed.logicIssues.map(String) : [],
|
|
283
|
+
edgeCases: Array.isArray(parsed.edgeCases) ? parsed.edgeCases.map(String) : [],
|
|
284
|
+
suggestions: Array.isArray(parsed.suggestions) ? parsed.suggestions.map(String) : [],
|
|
295
285
|
testResults: Array.isArray(parsed.testResults)
|
|
296
286
|
? parsed.testResults.map((tr: Record<string, unknown>) => ({
|
|
297
287
|
input: tr.input,
|
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
ToolValidationResult,
|
|
7
7
|
ToolSelfGenerationConfig,
|
|
8
8
|
} from '@cogitator-ai/types';
|
|
9
|
-
import type
|
|
9
|
+
import { z, type ZodType } from 'zod';
|
|
10
10
|
import { ToolValidator } from './tool-validator';
|
|
11
11
|
import {
|
|
12
12
|
TOOL_GENERATION_SYSTEM_PROMPT,
|
|
@@ -119,7 +119,7 @@ export class ToolGenerator {
|
|
|
119
119
|
async generateQuick(
|
|
120
120
|
description: string,
|
|
121
121
|
name: string,
|
|
122
|
-
|
|
122
|
+
_parameters: Record<string, unknown>
|
|
123
123
|
): Promise<GeneratedTool | null> {
|
|
124
124
|
const gap: CapabilityGap = {
|
|
125
125
|
id: `quick_${Date.now()}`,
|
|
@@ -150,10 +150,13 @@ export class ToolGenerator {
|
|
|
150
150
|
allowedModules: this.config.sandboxConfig?.allowedModules,
|
|
151
151
|
});
|
|
152
152
|
|
|
153
|
-
const response = await this.callLLM(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
153
|
+
const response = await this.callLLM(
|
|
154
|
+
[
|
|
155
|
+
{ role: 'system', content: TOOL_GENERATION_SYSTEM_PROMPT },
|
|
156
|
+
{ role: 'user', content: prompt },
|
|
157
|
+
],
|
|
158
|
+
0.4
|
|
159
|
+
);
|
|
157
160
|
|
|
158
161
|
const tool = parseToolGenerationResponse(response.content);
|
|
159
162
|
|
|
@@ -175,10 +178,13 @@ export class ToolGenerator {
|
|
|
175
178
|
): Promise<GeneratedTool | null> {
|
|
176
179
|
const prompt = buildToolImprovementPrompt(tool, validationResult, iteration);
|
|
177
180
|
|
|
178
|
-
const response = await this.callLLM(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
181
|
+
const response = await this.callLLM(
|
|
182
|
+
[
|
|
183
|
+
{ role: 'system', content: TOOL_GENERATION_SYSTEM_PROMPT },
|
|
184
|
+
{ role: 'user', content: prompt },
|
|
185
|
+
],
|
|
186
|
+
0.3
|
|
187
|
+
);
|
|
182
188
|
|
|
183
189
|
const improved = parseToolGenerationResponse(response.content);
|
|
184
190
|
|
|
@@ -198,7 +204,6 @@ export class ToolGenerator {
|
|
|
198
204
|
|
|
199
205
|
createExecutableTool(generated: GeneratedTool): Tool {
|
|
200
206
|
const execute = this.compileImplementation(generated.implementation);
|
|
201
|
-
const { z } = require('zod');
|
|
202
207
|
|
|
203
208
|
return {
|
|
204
209
|
name: generated.name,
|
|
@@ -216,10 +221,9 @@ export class ToolGenerator {
|
|
|
216
221
|
};
|
|
217
222
|
}
|
|
218
223
|
|
|
219
|
-
private compileImplementation(
|
|
220
|
-
implementation: string
|
|
221
|
-
): (params: unknown) => Promise<unknown> {
|
|
224
|
+
private compileImplementation(implementation: string): (params: unknown) => Promise<unknown> {
|
|
222
225
|
return async (params: unknown): Promise<unknown> => {
|
|
226
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
223
227
|
const factory = new Function(`
|
|
224
228
|
"use strict";
|
|
225
229
|
${implementation}
|