@tehreet/conduit
Version:
LLM API gateway with intelligent routing, robust process management, and health monitoring
231 lines (198 loc) • 6.37 kB
JavaScript
#!/usr/bin/env node
// Test Phase 5 implementation
const {
tokenCounter,
getAdvancedRoutingEngine,
registerRoutingRule,
createCommonRoutingRules,
registerCustomEvaluator
} = require('./dist/lib/index.js');
async function testPhase5() {
console.log('Testing Phase 5 - Enhanced Core Features...');
// Test 1: Enhanced Token Counter with Caching
console.log('\n1. Testing Enhanced Token Counter...');
const testText = 'This is a test message for token counting with caching support.';
const model = 'claude-3-5-sonnet-20241022';
// Test normal counting
const result1 = await tokenCounter.countTokens(testText, model);
console.log('✅ Token counting result:', {
count: result1.count,
method: result1.method
});
// Test caching (should be faster)
const result2 = await tokenCounter.countTokens(testText, model);
console.log('✅ Cached token counting result:', {
count: result2.count,
method: result2.method
});
// Test batch counting
const batchRequests = [
{ id: 'test1', text: 'Hello world', model },
{ id: 'test2', text: 'How are you today?', model },
{ id: 'test3', text: 'This is a longer text for testing batch processing capabilities', model }
];
const batchResults = await tokenCounter.batchCountTokens(batchRequests);
console.log('✅ Batch token counting working:', {
totalRequests: batchResults.length,
sampleResult: batchResults[0]
});
// Test cache statistics
const cacheStats = tokenCounter.getCacheStats();
console.log('✅ Cache statistics:', {
size: cacheStats.size,
hasEntries: cacheStats.size > 0
});
// Test 2: Advanced Routing Engine
console.log('\n2. Testing Advanced Routing Engine...');
const routingEngine = getAdvancedRoutingEngine();
// Register common routing rules
const commonRules = createCommonRoutingRules();
commonRules.forEach(rule => {
registerRoutingRule(rule);
});
console.log('✅ Registered routing rules:', commonRules.length);
// Test routing decision for simple request
const simpleContext = {
request: {
body: {
messages: [{ role: 'user', content: 'Hello' }],
thinking: false
}
},
tokenCount: 500,
config: {
Router: {
default: 'claude-3-5-sonnet-20241022'
}
},
env: process.env,
synapseContext: {},
timestamp: new Date()
};
const simpleDecision = await routingEngine.evaluate(simpleContext);
console.log('✅ Simple routing decision:', {
model: simpleDecision.selectedModel,
confidence: simpleDecision.confidence,
reason: simpleDecision.reason
});
// Test routing decision for complex request
const complexContext = {
request: {
body: {
messages: [{
role: 'user',
content: 'Please help me implement a complex algorithm with multiple data structures and optimization techniques. I need detailed explanations and code examples.'
}],
thinking: false
}
},
tokenCount: 5000,
config: {
Router: {
default: 'claude-3-5-sonnet-20241022'
}
},
env: process.env,
synapseContext: {},
timestamp: new Date()
};
const complexDecision = await routingEngine.evaluate(complexContext);
console.log('✅ Complex routing decision:', {
model: complexDecision.selectedModel,
confidence: complexDecision.confidence,
reason: complexDecision.reason
});
// Test routing decision for thinking request
const thinkingContext = {
request: {
body: {
messages: [{
role: 'user',
content: 'I need to solve a complex mathematical problem step by step.'
}],
thinking: true
}
},
tokenCount: 2000,
config: {
Router: {
default: 'claude-3-5-sonnet-20241022'
}
},
env: process.env,
synapseContext: {},
timestamp: new Date()
};
const thinkingDecision = await routingEngine.evaluate(thinkingContext);
console.log('✅ Thinking routing decision:', {
model: thinkingDecision.selectedModel,
confidence: thinkingDecision.confidence,
reason: thinkingDecision.reason
});
// Test 3: Custom Evaluators
console.log('\n3. Testing Custom Evaluators...');
// Register a custom evaluator
registerCustomEvaluator('is_weekend', (condition, context) => {
const day = context.timestamp.getDay();
return day === 0 || day === 6; // Sunday = 0, Saturday = 6
});
// Register a custom rule using the evaluator
const weekendRule = {
id: 'weekend-rule',
name: 'Weekend Rule',
description: 'Use different model on weekends',
priority: 110,
conditions: [
{
type: 'custom',
field: 'is_weekend',
operator: '=',
value: true
}
],
model: 'claude-3-5-haiku-20241022',
confidence: 0.7,
enabled: true
};
registerRoutingRule(weekendRule);
const weekendDecision = await routingEngine.evaluate(simpleContext);
console.log('✅ Weekend routing decision:', {
model: weekendDecision.selectedModel,
confidence: weekendDecision.confidence,
reason: weekendDecision.reason
});
// Test 4: Model-Specific Configuration
console.log('\n4. Testing Model-Specific Configuration...');
const modelConfigs = [
'claude-3-5-sonnet-20241022',
'claude-3-5-haiku-20241022',
'claude-3-opus-20240229',
'gpt-4',
'gpt-3.5-turbo'
];
modelConfigs.forEach(model => {
const config = tokenCounter.getModelConfig(model);
console.log(`✅ ${model} config:`, config);
});
// Test 5: Token Counter Options
console.log('\n5. Testing Token Counter Options...');
const originalOptions = tokenCounter.getOptions();
console.log('✅ Original options:', {
useCache: originalOptions.useCache,
enableBatching: originalOptions.enableBatching,
batchSize: originalOptions.batchSize
});
// Update options
tokenCounter.updateOptions({
cacheMaxAge: 10 * 60 * 1000, // 10 minutes
batchSize: 20
});
const updatedOptions = tokenCounter.getOptions();
console.log('✅ Updated options:', {
cacheMaxAge: updatedOptions.cacheMaxAge,
batchSize: updatedOptions.batchSize
});
console.log('\n🎉 All Phase 5 tests passed!');
}
// Run the test
testPhase5().catch(console.error);