This commit is contained in:
overcuriousity
2025-08-16 17:11:03 +02:00
parent 3ad0d8120a
commit 1d98dd3257
4 changed files with 203648 additions and 152 deletions

View File

@@ -4,6 +4,7 @@ import { getCompressedToolsDataForAI } from './dataService.js';
import { embeddingsService, type EmbeddingData, type SimilarityResult } from './embeddings.js';
import { AI_PROMPTS, getPrompt } from '../config/prompts.js';
import { isToolHosted } from './toolHelpers.js';
import { auditService, type AuditEntry } from './auditService.js';
import dotenv from 'dotenv';
dotenv.config();
@@ -35,17 +36,6 @@ interface AnalysisResult {
};
}
interface AuditEntry {
timestamp: number;
phase: string;
action: string;
input: any;
output: any;
confidence: number;
processingTimeMs: number;
metadata: Record<string, any>;
}
interface AnalysisContext {
userQuery: string;
mode: string;
@@ -70,7 +60,6 @@ interface AnalysisContext {
relevance: string;
}>;
seenToolNames: Set<string>;
auditTrail: AuditEntry[];
embeddingsSimilarities: Map<string, number>;
aiSelectedTools?: any[];
aiSelectedConcepts?: any[];
@@ -100,10 +89,6 @@ class ImprovedMicroTaskAIPipeline {
private softwareSelectionRatio: number;
private maxContextTokens: number;
private maxPromptTokens: number;
private auditConfig: {
enabled: boolean;
detailLevel: string;
};
private confidenceConfig: {
semanticWeight: number;
suitabilityWeight: number;
@@ -134,11 +119,6 @@ class ImprovedMicroTaskAIPipeline {
this.maxContextTokens = this.getEnvInt('AI_MAX_CONTEXT_TOKENS', 4000);
this.maxPromptTokens = this.getEnvInt('AI_MAX_PROMPT_TOKENS', 1500);
this.auditConfig = {
enabled: process.env.FORENSIC_AUDIT_ENABLED === 'true',
detailLevel: process.env.FORENSIC_AUDIT_DETAIL_LEVEL || 'standard'
};
this.confidenceConfig = {
semanticWeight: this.getEnvFloat('CONFIDENCE_SEMANTIC_WEIGHT', 0.3),
suitabilityWeight: this.getEnvFloat('CONFIDENCE_SUITABILITY_WEIGHT', 0.7),
@@ -147,7 +127,7 @@ class ImprovedMicroTaskAIPipeline {
highThreshold: this.getEnvInt('CONFIDENCE_HIGH_THRESHOLD', 80)
};
this.logPipelineInit();
console.log('[AI-PIPELINE] Initialized with audit service integration');
}
private getRequiredEnv(key: string): string {
@@ -168,12 +148,7 @@ class ImprovedMicroTaskAIPipeline {
return value ? parseFloat(value) : defaultValue;
}
private logPipelineInit(): void {
console.log('[AI-PIPELINE] Initialized with audit:', this.auditConfig.enabled);
console.log('[AI-PIPELINE] Method/Software balance:',
`${(this.methodSelectionRatio * 100).toFixed(0)}%/${(this.softwareSelectionRatio * 100).toFixed(0)}%`);
}
// SIMPLIFIED AUDIT INTEGRATION - Use auditService instead of local implementation
private addAuditEntry(
context: AnalysisContext,
phase: string,
@@ -184,25 +159,7 @@ class ImprovedMicroTaskAIPipeline {
startTime: number,
metadata: Record<string, any> = {}
): void {
if (!this.auditConfig.enabled) return;
const entry: AuditEntry = {
timestamp: Date.now(),
phase,
action,
input,
output,
confidence: Math.round(confidence),
processingTimeMs: Date.now() - startTime,
metadata
};
context.auditTrail.push(entry);
this.logAuditEntry(phase, action, confidence, entry.processingTimeMs);
}
private logAuditEntry(phase: string, action: string, confidence: number, timeMs: number): void {
console.log(`[AUDIT] ${phase}/${action}: ${confidence}% confidence, ${timeMs}ms`);
auditService.addEntry(phase, action, input, output, confidence, startTime, metadata);
}
private calculateSelectionConfidence(result: any, candidateCount: number): number {
@@ -474,24 +431,22 @@ class ImprovedMicroTaskAIPipeline {
selectionMethod = 'full_dataset';
}
if (this.auditConfig.enabled) {
this.addAuditEntry(
context,
'retrieval',
'embeddings-search',
{ query: userQuery, threshold: this.similarityThreshold, candidates: this.embeddingCandidates },
{
candidatesFound: similarItems.length,
reductionRatio: reductionRatio,
usingEmbeddings: selectionMethod === 'embeddings_candidates',
totalAvailable: totalAvailableTools,
filtered: similarTools.length
},
selectionMethod === 'embeddings_candidates' ? 85 : 60,
embeddingsStart,
{ selectionMethod, embeddingsEnabled: true }
);
}
this.addAuditEntry(
context,
'retrieval',
'embeddings-search',
{ query: userQuery, threshold: this.similarityThreshold, candidates: this.embeddingCandidates },
{
candidatesFound: similarItems.length,
reductionRatio: reductionRatio,
usingEmbeddings: selectionMethod === 'embeddings_candidates',
totalAvailable: totalAvailableTools,
filtered: similarTools.length
},
selectionMethod === 'embeddings_candidates' ? 85 : 60,
embeddingsStart,
{ selectionMethod, embeddingsEnabled: true }
);
} else {
console.log('[AI-PIPELINE] Embeddings disabled, using full dataset');
candidateTools = toolsData.tools;
@@ -599,18 +554,16 @@ class ImprovedMicroTaskAIPipeline {
if (!result || !Array.isArray(result.selectedTools) || !Array.isArray(result.selectedConcepts)) {
console.error('[AI-PIPELINE] AI selection returned invalid structure');
if (this.auditConfig.enabled) {
this.addAuditEntry(
context,
'selection',
'ai-tool-selection-failed',
{ candidateCount: candidateTools.length, mode },
{ error: 'Invalid JSON structure' },
10,
selectionStart,
{ aiModel: this.config.model, selectionMethod }
);
}
this.addAuditEntry(
context,
'selection',
'ai-tool-selection-failed',
{ candidateCount: candidateTools.length, mode },
{ error: 'Invalid JSON structure' },
10,
selectionStart,
{ aiModel: this.config.model, selectionMethod }
);
throw new Error('AI selection failed to return valid tool and concept selection');
}
@@ -636,44 +589,40 @@ class ImprovedMicroTaskAIPipeline {
console.log('[AI-PIPELINE] AI selected:', selectedMethods.length, 'methods,', selectedSoftware.length, 'software,', selectedConcepts.length, 'concepts');
if (this.auditConfig.enabled) {
const confidence = this.calculateSelectionConfidence(result, candidateTools.length + candidateConcepts.length);
this.addAuditEntry(
context,
'selection',
'ai-tool-selection',
{ candidateCount: candidateTools.length, mode },
{
selectedMethodCount: selectedMethods.length,
selectedSoftwareCount: selectedSoftware.length,
selectedConceptCount: selectedConcepts.length,
reasoning: result.reasoning?.slice(0, 200),
methodBalance: `${((selectedMethods.length / (selectedTools.length || 1)) * 100).toFixed(0)}%`
},
confidence,
selectionStart,
{ aiModel: this.config.model, selectionMethod }
);
}
const confidence = this.calculateSelectionConfidence(result, candidateTools.length + candidateConcepts.length);
this.addAuditEntry(
context,
'selection',
'ai-tool-selection',
{ candidateCount: candidateTools.length, mode },
{
selectedMethodCount: selectedMethods.length,
selectedSoftwareCount: selectedSoftware.length,
selectedConceptCount: selectedConcepts.length,
reasoning: result.reasoning?.slice(0, 200),
methodBalance: `${((selectedMethods.length / (selectedTools.length || 1)) * 100).toFixed(0)}%`
},
confidence,
selectionStart,
{ aiModel: this.config.model, selectionMethod }
);
return { selectedTools, selectedConcepts };
} catch (error) {
console.error('[AI-PIPELINE] AI selection failed:', error);
if (this.auditConfig.enabled) {
this.addAuditEntry(
context,
'selection',
'ai-tool-selection-error',
{ candidateCount: candidateTools.length, mode },
{ error: error.message },
5,
selectionStart,
{ aiModel: this.config.model, selectionMethod }
);
}
this.addAuditEntry(
context,
'selection',
'ai-tool-selection-error',
{ candidateCount: candidateTools.length, mode },
{ error: error.message },
5,
selectionStart,
{ aiModel: this.config.model, selectionMethod }
);
throw error;
}
}
@@ -1371,6 +1320,9 @@ class ImprovedMicroTaskAIPipeline {
console.log('[AI-PIPELINE] Starting', mode, 'query processing');
// CLEAR AUDIT TRAIL for new analysis
auditService.clearAuditTrail();
try {
const toolsData = await getCompressedToolsDataForAI();
@@ -1382,7 +1334,6 @@ class ImprovedMicroTaskAIPipeline {
maxContextLength: this.maxContextTokens,
currentContextLength: 0,
seenToolNames: new Set<string>(),
auditTrail: [],
embeddingsSimilarities: new Map<string, number>(),
aiSelectedTools: [],
aiSelectedConcepts: []
@@ -1410,7 +1361,7 @@ class ImprovedMicroTaskAIPipeline {
{ candidateTools: filteredData.tools.length, candidateConcepts: filteredData.concepts.length },
90,
startTime,
{ auditEnabled: this.auditConfig.enabled }
{ auditEnabled: auditService.isEnabled() }
);
const analysisResult = await this.analyzeScenario(context);
@@ -1459,7 +1410,7 @@ class ImprovedMicroTaskAIPipeline {
'completion',
'pipeline-end',
{ completedTasks: completeTasks, failedTasks },
{ finalRecommendation: !!recommendation, auditEntriesGenerated: context.auditTrail.length },
{ finalRecommendation: !!recommendation, auditEntriesGenerated: auditService.getCurrentAuditTrail().length },
completeTasks > failedTasks ? 85 : 60,
startTime,
{ totalProcessingTimeMs: Date.now() - startTime }
@@ -1477,10 +1428,13 @@ class ImprovedMicroTaskAIPipeline {
console.log('[AI-PIPELINE] Processing complete. Tasks completed:', completeTasks, 'failed:', failedTasks);
// FINALIZE AUDIT TRAIL and get final trail
const finalAuditTrail = auditService.finalizeAuditTrail();
return {
recommendation: {
...recommendation,
auditTrail: this.auditConfig.enabled ? context.auditTrail : undefined
auditTrail: auditService.isEnabled() ? finalAuditTrail : undefined
},
processingStats
};
@@ -1522,8 +1476,6 @@ class ImprovedMicroTaskAIPipeline {
})) || []
};
const processedAuditTrail = this.auditConfig.enabled && context.auditTrail ? context.auditTrail : [];
if (isWorkflow) {
const recommendedToolsWithConfidence = context.selectedTools?.map((st: any) => {
const confidence = this.calculateRecommendationConfidence(
@@ -1565,8 +1517,7 @@ class ImprovedMicroTaskAIPipeline {
return {
...base,
recommended_tools: recommendedToolsWithConfidence,
workflow_suggestion: finalContent,
auditTrail: processedAuditTrail
workflow_suggestion: finalContent
};
} else {
const recommendedToolsWithConfidence = context.selectedTools?.map((st: any) => {
@@ -1610,8 +1561,7 @@ class ImprovedMicroTaskAIPipeline {
return {
...base,
recommended_tools: recommendedToolsWithConfidence,
additional_considerations: finalContent,
auditTrail: processedAuditTrail
additional_considerations: finalContent
};
}
}