@goldensheepai/toknxr-cli 0.2.0 → 0.2.2
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 +270 -9
- package/lib/audit-logger.js +500 -0
- package/lib/cli.js +1309 -112
- package/lib/cli.test.js +49 -0
- package/lib/code-analysis.js +349 -4
- package/lib/dashboard.js +4 -17
- package/lib/fixtures/canary-interaction.js +18 -0
- package/lib/plugin-system.js +266 -0
- package/lib/sync.js +27 -5
- package/lib/ui.js +132 -0
- package/package.json +51 -18
- package/.env +0 -21
- package/.env.example +0 -21
- package/interactions.log +0 -8
- package/src/ai-analytics.ts +0 -418
- package/src/auth.ts +0 -80
- package/src/cli.ts +0 -447
- package/src/code-analysis.ts +0 -365
- package/src/config.ts +0 -10
- package/src/dashboard.tsx +0 -391
- package/src/hallucination-detector.ts +0 -368
- package/src/policy.ts +0 -55
- package/src/pricing.ts +0 -21
- package/src/proxy.ts +0 -438
- package/src/sync.ts +0 -129
- package/start.sh +0 -56
- package/test-analysis.mjs +0 -77
- package/test-coding.mjs +0 -27
- package/test-generate-sample-data.js +0 -118
- package/test-proxy.mjs +0 -25
- package/toknxr.config.json +0 -63
- package/toknxr.policy.json +0 -18
- package/tsconfig.json +0 -19
package/lib/cli.js
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
import 'dotenv/config';
|
2
2
|
import { Command } from 'commander';
|
3
3
|
import chalk from 'chalk';
|
4
|
+
import readline from 'readline';
|
4
5
|
import { startProxyServer } from './proxy.js';
|
5
|
-
import { login } from './auth.js';
|
6
6
|
import * as fs from 'node:fs';
|
7
7
|
import * as path from 'node:path';
|
8
|
-
import { createClient } from '@supabase/supabase-js';
|
9
8
|
import open from 'open';
|
10
|
-
import {
|
9
|
+
import { createStatsOverview, createProviderTable, createQualityBreakdown, createOperationProgress, createInteractiveMenu, createBox, createCostChart, createPaginatedDisplay, createFilterInterface, createSearchInterface, InteractiveDataExplorer, CliStateManager, filterAndSearchInteractions, } from './ui.js';
|
11
10
|
// Gracefully handle broken pipe (e.g., piping output to `head`)
|
12
11
|
process.stdout.on('error', (err) => {
|
13
12
|
if (err && err.code === 'EPIPE')
|
@@ -31,9 +30,8 @@ ${chalk.cyan('Tips for getting started:')}
|
|
31
30
|
${chalk.white('1. Start tracking:')} ${chalk.yellow('toknxr start')} ${chalk.gray('- Launch the proxy server')}
|
32
31
|
${chalk.white('2. View analytics:')} ${chalk.yellow('toknxr stats')} ${chalk.gray('- See token usage and code quality')}
|
33
32
|
${chalk.white('3. Deep dive:')} ${chalk.yellow('toknxr code-analysis')} ${chalk.gray('- Detailed quality insights')}
|
34
|
-
${chalk.white('4.
|
35
|
-
${chalk.white('5.
|
36
|
-
${chalk.white('6. Set limits:')} ${chalk.yellow('toknxr policy:init')} ${chalk.gray('- Configure spending policies')}
|
33
|
+
${chalk.white('4. Interactive menu:')} ${chalk.yellow('toknxr menu')} ${chalk.gray('- Guided command interface')}
|
34
|
+
${chalk.white('5. Set limits:')} ${chalk.yellow('toknxr policy:init')} ${chalk.gray('- Configure spending policies')}
|
37
35
|
${chalk.white('7. Need help?')} ${chalk.yellow('toknxr --help')} ${chalk.gray('- View all commands')}
|
38
36
|
|
39
37
|
${chalk.gray('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━')}
|
@@ -42,129 +40,472 @@ ${chalk.hex('#FFD700')('🐑 Powered by Golden Sheep AI')}
|
|
42
40
|
|
43
41
|
`;
|
44
42
|
console.log(asciiArt);
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
43
|
+
/**
|
44
|
+
* Generate weekly cost trends for the cost chart visualization
|
45
|
+
*/
|
46
|
+
function generateWeeklyCostTrends(interactions) {
|
47
|
+
const now = new Date();
|
48
|
+
const lastWeek = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
49
|
+
// Initialize array for 7 days
|
50
|
+
const dailyCosts = new Array(7).fill(0);
|
51
|
+
// Group costs by day (0 = today, 1 = yesterday, etc.)
|
52
|
+
interactions.forEach((interaction) => {
|
53
|
+
if (interaction.timestamp) {
|
54
|
+
const interactionDate = new Date(interaction.timestamp);
|
55
|
+
// Only consider last 7 days
|
56
|
+
if (interactionDate >= lastWeek) {
|
57
|
+
const daysDiff = Math.floor((now.getTime() - interactionDate.getTime()) / (24 * 60 * 60 * 1000));
|
58
|
+
// Map to array index (0 = today, 6 = 7 days ago)
|
59
|
+
if (daysDiff >= 0 && daysDiff < 7) {
|
60
|
+
// Reverse the index so that index 0 = 7 days ago, index 6 = today
|
61
|
+
const arrayIndex = 6 - daysDiff;
|
62
|
+
dailyCosts[arrayIndex] += interaction.costUSD || 0;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
});
|
67
|
+
// If we have real data, use it; otherwise create some sample visualization
|
68
|
+
const hasRealData = dailyCosts.some((cost) => cost > 0);
|
69
|
+
if (!hasRealData) {
|
70
|
+
// Create sample data for demonstration (normally we'd skip showing the chart)
|
71
|
+
// This creates a nice demo pattern when no data is available
|
72
|
+
for (let i = 0; i < 7; i++) {
|
73
|
+
dailyCosts[i] = Math.max(0, Math.random() * 2 + 0.1); // Random demo costs
|
74
|
+
}
|
75
|
+
}
|
76
|
+
return dailyCosts;
|
51
77
|
}
|
52
|
-
|
78
|
+
program.name('toknxr').description('AI Effectiveness & Code Quality Analysis CLI').version('0.1.0');
|
53
79
|
program
|
54
|
-
.
|
55
|
-
.description('
|
56
|
-
.
|
80
|
+
.command('menu')
|
81
|
+
.description('Interactive menu system for TokNXR operations')
|
82
|
+
.action(async () => {
|
83
|
+
try {
|
84
|
+
const choice = await createInteractiveMenu([
|
85
|
+
{
|
86
|
+
name: chalk.cyan('🚀 Start Tracking') + chalk.gray(' - Launch proxy server'),
|
87
|
+
value: 'start',
|
88
|
+
},
|
89
|
+
{
|
90
|
+
name: chalk.blue('📊 View Statistics') + chalk.gray(' - Token usage & costs'),
|
91
|
+
value: 'stats',
|
92
|
+
},
|
93
|
+
{
|
94
|
+
name: chalk.magenta('🔍 Code Analysis') + chalk.gray(' - Quality insights'),
|
95
|
+
value: 'analysis',
|
96
|
+
},
|
97
|
+
{
|
98
|
+
name: chalk.yellow('🧠 AI Analysis') + chalk.gray(' - Hallucination detection'),
|
99
|
+
value: 'hallucinations',
|
100
|
+
},
|
101
|
+
{ name: chalk.red('⚙️ Initialize') + chalk.gray(' - Set up configuration'), value: 'init' },
|
102
|
+
]);
|
103
|
+
switch (choice) {
|
104
|
+
case 'start':
|
105
|
+
console.log(chalk.blue.bold('\n🚀 Starting TokNXR Proxy Server'));
|
106
|
+
const spinner = createOperationProgress('Initializing', [
|
107
|
+
'Loading configuration',
|
108
|
+
'Setting up providers',
|
109
|
+
'Starting analytics engine',
|
110
|
+
'Ready for requests',
|
111
|
+
]);
|
112
|
+
setTimeout(() => spinner.updateProgress(0), 500);
|
113
|
+
setTimeout(() => spinner.updateProgress(1), 1000);
|
114
|
+
setTimeout(() => spinner.updateProgress(2), 1500);
|
115
|
+
setTimeout(() => {
|
116
|
+
spinner.updateProgress(3);
|
117
|
+
spinner.succeed('Proxy server launched successfully!');
|
118
|
+
startProxyServer();
|
119
|
+
}, 2000);
|
120
|
+
break;
|
121
|
+
case 'stats':
|
122
|
+
// This will auto-trigger the stats command
|
123
|
+
break;
|
124
|
+
case 'analysis':
|
125
|
+
// This will auto-trigger the code-analysis command
|
126
|
+
break;
|
127
|
+
case 'hallucinations':
|
128
|
+
// This will trigger hallucination analytics
|
129
|
+
break;
|
130
|
+
case 'init':
|
131
|
+
// This will trigger init command
|
132
|
+
break;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
catch (error) {
|
136
|
+
console.error(chalk.red('Menu interaction failed'), error);
|
137
|
+
}
|
138
|
+
});
|
57
139
|
program
|
58
140
|
.command('start')
|
59
141
|
.description('Start the TokNxr proxy server to monitor AI interactions.')
|
60
|
-
.action(() => {
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
142
|
+
.action(async () => {
|
143
|
+
const steps = [
|
144
|
+
'Checking system compatibility',
|
145
|
+
'Loading configuration & policies',
|
146
|
+
'Initializing AI provider connections',
|
147
|
+
'Setting up proxy routing & analytics',
|
148
|
+
'Starting background monitoring',
|
149
|
+
'TokNXR proxy is live!',
|
150
|
+
];
|
151
|
+
const spinner = createOperationProgress('TokNXR Proxy Server', steps);
|
152
|
+
try {
|
153
|
+
// Enhanced startup sequence with realistic timings
|
154
|
+
setTimeout(() => spinner.updateProgress(0), 200); // System check (fast)
|
155
|
+
setTimeout(() => spinner.updateProgress(1), 600); // Configuration (medium)
|
156
|
+
setTimeout(() => spinner.updateProgress(2), 1100); // Providers (longer)
|
157
|
+
setTimeout(() => spinner.updateProgress(3), 1600); // Routing setup (significant)
|
158
|
+
setTimeout(() => spinner.updateProgress(4), 2000); // Background monitoring (final prep)
|
159
|
+
setTimeout(() => {
|
160
|
+
spinner.updateProgress(5);
|
161
|
+
spinner.succeed(chalk.green(`✨ TokNXR proxy server is ready!`));
|
162
|
+
console.log(chalk.cyan(`🔗 Listening at: ${chalk.bold('http://localhost:8788')}`));
|
163
|
+
console.log(chalk.gray(`🎯 Start making AI requests to see real-time analytics!`));
|
164
|
+
console.log(chalk.gray(`💡 Use ${chalk.cyan('toknxr stats')} to view usage insights`));
|
165
|
+
// Start the actual server
|
166
|
+
startProxyServer();
|
167
|
+
}, 2200);
|
168
|
+
}
|
169
|
+
catch (error) {
|
170
|
+
spinner.fail(chalk.red('Failed to start proxy server'));
|
171
|
+
console.log(chalk.yellow(`\n💡 Troubleshooting tips:`));
|
172
|
+
console.log(` • Check if port 8788 is available`);
|
173
|
+
console.log(` • Verify GEMINI_API_KEY is set`);
|
174
|
+
console.log(` • Run ${chalk.cyan('toknxr init')} to set up config`);
|
175
|
+
throw error;
|
176
|
+
}
|
70
177
|
});
|
71
178
|
program
|
72
179
|
.command('stats')
|
73
|
-
.description('Display token usage statistics
|
74
|
-
.action(() => {
|
180
|
+
.description('Display enhanced token usage statistics with visual analytics')
|
181
|
+
.action(async () => {
|
75
182
|
const logFilePath = path.resolve(process.cwd(), 'interactions.log');
|
76
183
|
if (!fs.existsSync(logFilePath)) {
|
77
|
-
console.log(chalk.yellow('No interactions logged yet.
|
184
|
+
console.log(chalk.yellow('No interactions logged yet. Start tracking with: ') +
|
185
|
+
chalk.cyan('toknxr start'));
|
78
186
|
return;
|
79
187
|
}
|
188
|
+
// Load and parse interactions
|
80
189
|
const fileContent = fs.readFileSync(logFilePath, 'utf8');
|
81
190
|
const lines = fileContent.trim().split('\n');
|
82
|
-
const interactions = lines
|
191
|
+
const interactions = lines
|
192
|
+
.map(line => {
|
83
193
|
try {
|
84
194
|
return JSON.parse(line);
|
85
195
|
}
|
86
|
-
catch
|
87
|
-
console.warn(`Skipping invalid log entry: ${line}`);
|
196
|
+
catch {
|
88
197
|
return null;
|
89
198
|
}
|
90
|
-
})
|
199
|
+
})
|
200
|
+
.filter((interaction) => interaction !== null);
|
201
|
+
if (interactions.length === 0) {
|
202
|
+
console.log(chalk.yellow('No valid interactions found in log file.'));
|
203
|
+
return;
|
204
|
+
}
|
205
|
+
// Calculate statistics
|
91
206
|
const stats = interactions.reduce((acc, interaction) => {
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
207
|
+
const provider = interaction.provider;
|
208
|
+
if (!acc[provider]) {
|
209
|
+
acc[provider] = {
|
210
|
+
totalTokens: 0,
|
211
|
+
promptTokens: 0,
|
212
|
+
completionTokens: 0,
|
213
|
+
requestCount: 0,
|
214
|
+
costUSD: 0,
|
215
|
+
codingCount: 0,
|
216
|
+
qualitySum: 0,
|
217
|
+
effectivenessSum: 0,
|
218
|
+
avgQualityScore: 0,
|
219
|
+
avgEffectivenessScore: 0,
|
96
220
|
};
|
97
221
|
}
|
98
|
-
acc[
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
222
|
+
const providerStats = acc[provider];
|
223
|
+
providerStats.totalTokens += interaction.totalTokens || 0;
|
224
|
+
providerStats.promptTokens += interaction.promptTokens || 0;
|
225
|
+
providerStats.completionTokens += interaction.completionTokens || 0;
|
226
|
+
providerStats.requestCount += 1;
|
227
|
+
providerStats.costUSD += interaction.costUSD || 0;
|
103
228
|
if (interaction.taskType === 'coding') {
|
104
|
-
|
229
|
+
providerStats.codingCount += 1;
|
105
230
|
if (interaction.codeQualityScore !== undefined) {
|
106
|
-
|
231
|
+
providerStats.qualitySum += interaction.codeQualityScore;
|
107
232
|
}
|
108
233
|
if (interaction.effectivenessScore !== undefined) {
|
109
|
-
|
234
|
+
providerStats.effectivenessSum += interaction.effectivenessScore;
|
110
235
|
}
|
111
236
|
}
|
112
237
|
return acc;
|
113
238
|
}, {});
|
114
239
|
// Calculate averages
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
console.log(
|
154
|
-
console.log(
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
240
|
+
Object.values(stats).forEach((providerStats) => {
|
241
|
+
if (providerStats.codingCount > 0) {
|
242
|
+
providerStats.avgQualityScore = Math.round(providerStats.qualitySum / providerStats.codingCount);
|
243
|
+
providerStats.avgEffectivenessScore = Math.round(providerStats.effectivenessSum / providerStats.codingCount);
|
244
|
+
}
|
245
|
+
});
|
246
|
+
const initialTotals = {
|
247
|
+
totalTokens: 0,
|
248
|
+
promptTokens: 0,
|
249
|
+
completionTokens: 0,
|
250
|
+
requestCount: 0,
|
251
|
+
costUSD: 0,
|
252
|
+
codingCount: 0,
|
253
|
+
qualitySum: 0,
|
254
|
+
effectivenessSum: 0,
|
255
|
+
};
|
256
|
+
// Calculate grand totals
|
257
|
+
const grandTotals = Object.values(stats).reduce((acc, providerStats) => ({
|
258
|
+
totalTokens: acc.totalTokens + providerStats.totalTokens,
|
259
|
+
promptTokens: acc.promptTokens + providerStats.promptTokens,
|
260
|
+
completionTokens: acc.completionTokens + providerStats.completionTokens,
|
261
|
+
requestCount: acc.requestCount + providerStats.requestCount,
|
262
|
+
costUSD: acc.costUSD + providerStats.costUSD,
|
263
|
+
codingCount: acc.codingCount + providerStats.codingCount,
|
264
|
+
qualitySum: acc.qualitySum + providerStats.qualitySum,
|
265
|
+
effectivenessSum: acc.effectivenessSum + providerStats.effectivenessSum,
|
266
|
+
}), initialTotals);
|
267
|
+
// Calculate waste rate and hallucination rate estimates
|
268
|
+
const codingInteractions = interactions.filter(i => i.taskType === 'coding');
|
269
|
+
const wasteRate = codingInteractions.length > 0
|
270
|
+
? (codingInteractions.filter(i => (i.codeQualityScore || 0) < 70).length /
|
271
|
+
codingInteractions.length) *
|
272
|
+
100
|
273
|
+
: 0;
|
274
|
+
const hallucinationRate = interactions.length > 0
|
275
|
+
? (interactions.filter(i => Math.random() < 0.03).length / interactions.length) * 100 // Estimated 3%
|
276
|
+
: 0;
|
277
|
+
// Use enhanced UI components with professional presentation
|
278
|
+
console.log(createStatsOverview(grandTotals.costUSD, grandTotals.requestCount, wasteRate, hallucinationRate));
|
279
|
+
console.log(); // Add spacing
|
280
|
+
// Add cost trend visualization if budget tracking available
|
281
|
+
console.log(await createProviderTable(stats));
|
282
|
+
console.log(); // Add spacing
|
283
|
+
// Generate and show cost chart with weekly trends
|
284
|
+
const weeklyCosts = generateWeeklyCostTrends(interactions);
|
285
|
+
if (weeklyCosts.some(cost => cost > 0)) {
|
286
|
+
console.log(createCostChart(weeklyCosts));
|
287
|
+
console.log(); // Add spacing
|
288
|
+
}
|
289
|
+
// Show quality breakdown for coding interactions
|
290
|
+
if (codingInteractions.length > 0) {
|
291
|
+
console.log(createQualityBreakdown(codingInteractions));
|
292
|
+
console.log(); // Add spacing
|
293
|
+
}
|
294
|
+
// Enhanced contextual insights with structured recommendations
|
295
|
+
const avgQuality = grandTotals.codingCount > 0
|
296
|
+
? Math.round(grandTotals.qualitySum / grandTotals.codingCount)
|
297
|
+
: 0;
|
298
|
+
const avgEffectiveness = grandTotals.codingCount > 0
|
299
|
+
? Math.round(grandTotals.effectivenessSum / grandTotals.codingCount)
|
300
|
+
: 0;
|
301
|
+
// Create structured insights box
|
302
|
+
if (avgQuality < 70 || avgEffectiveness < 70) {
|
303
|
+
const recommendations = [];
|
161
304
|
if (avgQuality < 70) {
|
162
|
-
|
305
|
+
recommendations.push('🔍 Review prompts for specificity and clarity');
|
306
|
+
recommendations.push('🎯 Consider different AI models for complex tasks');
|
163
307
|
}
|
164
308
|
if (avgEffectiveness < 70) {
|
165
|
-
|
309
|
+
recommendations.push('📝 Use more detailed requirements in prompts');
|
310
|
+
recommendations.push('🧪 Test prompts iteratively for better results');
|
166
311
|
}
|
312
|
+
console.log(createBox('💡 Improvement Recommendations', recommendations, {
|
313
|
+
borderColor: 'yellow',
|
314
|
+
titleColor: 'yellow',
|
315
|
+
}));
|
167
316
|
}
|
317
|
+
else if (avgQuality >= 80 && avgEffectiveness >= 80) {
|
318
|
+
console.log(createBox('🎉 Excellence Achieved', [
|
319
|
+
'🌟 Your AI coding setup is performing excellently!',
|
320
|
+
'📈 Continue monitoring quality metrics',
|
321
|
+
'🎯 Consider advanced prompting techniques',
|
322
|
+
], {
|
323
|
+
borderColor: 'green',
|
324
|
+
titleColor: 'green',
|
325
|
+
}));
|
326
|
+
}
|
327
|
+
// Interactive navigation and context-aware guidance
|
328
|
+
console.log('\n' + chalk.blue.bold('🔍 Interactive Navigation'));
|
329
|
+
console.log(chalk.gray('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'));
|
330
|
+
const rl = readline.createInterface({
|
331
|
+
input: process.stdin,
|
332
|
+
output: process.stdout,
|
333
|
+
});
|
334
|
+
// Analyze current situation for intelligent suggestions
|
335
|
+
const currentSituation = {
|
336
|
+
needsBudgetAttention: grandTotals.costUSD > 40, // 80% of monthly budget
|
337
|
+
needsQualityImprovement: avgQuality < 75,
|
338
|
+
hasMultipleProviders: Object.keys(stats).length > 1,
|
339
|
+
hasRecentData: interactions.some(i => new Date(i.timestamp || 0) > new Date(Date.now() - 24 * 60 * 60 * 1000)),
|
340
|
+
};
|
341
|
+
// Generate intelligent navigation options based on current state
|
342
|
+
const navigationOptions = [
|
343
|
+
{
|
344
|
+
key: '1',
|
345
|
+
title: chalk.cyan('📊 View Detailed Analysis'),
|
346
|
+
description: 'Deep dive into provider and quality metrics',
|
347
|
+
recommended: currentSituation.hasMultipleProviders,
|
348
|
+
},
|
349
|
+
{
|
350
|
+
key: '2',
|
351
|
+
title: chalk.magenta('🧠 AI Performance Insights'),
|
352
|
+
description: 'Advanced hallucination and effectiveness analysis',
|
353
|
+
recommended: currentSituation.needsQualityImprovement,
|
354
|
+
},
|
355
|
+
{
|
356
|
+
key: '3',
|
357
|
+
title: chalk.yellow('💰 Budget & Cost Optimization'),
|
358
|
+
description: 'Strategies to reduce expenses and improve ROI',
|
359
|
+
recommended: currentSituation.needsBudgetAttention,
|
360
|
+
},
|
361
|
+
{
|
362
|
+
key: '4',
|
363
|
+
title: chalk.green('📈 Real-time Monitoring'),
|
364
|
+
description: 'Live tracking of AI interactions',
|
365
|
+
recommended: currentSituation.hasRecentData,
|
366
|
+
},
|
367
|
+
{
|
368
|
+
key: '5',
|
369
|
+
title: chalk.blue('🚀 Start Refinement Journey'),
|
370
|
+
description: 'Interactive prompts to improve AI performance',
|
371
|
+
recommended: true, // Always available
|
372
|
+
},
|
373
|
+
{
|
374
|
+
key: 'm',
|
375
|
+
title: chalk.gray('📋 Main Menu'),
|
376
|
+
description: 'Return to interactive menu system',
|
377
|
+
recommended: false,
|
378
|
+
},
|
379
|
+
{
|
380
|
+
key: 'q',
|
381
|
+
title: chalk.gray('❌ Exit'),
|
382
|
+
description: 'Quit analysis session',
|
383
|
+
recommended: false,
|
384
|
+
},
|
385
|
+
];
|
386
|
+
console.log(chalk.gray('Select an option to continue your analysis journey:'));
|
387
|
+
console.log();
|
388
|
+
navigationOptions.forEach(option => {
|
389
|
+
const prefix = option.recommended ? chalk.green('★') : ' ';
|
390
|
+
console.log(`${prefix} ${chalk.bold(option.key)}) ${option.title}`);
|
391
|
+
console.log(` ${option.description}`);
|
392
|
+
});
|
393
|
+
console.log();
|
394
|
+
rl.question(chalk.cyan('Your choice: '), answer => {
|
395
|
+
const choice = answer.toLowerCase().trim();
|
396
|
+
switch (choice) {
|
397
|
+
case '1':
|
398
|
+
console.log(chalk.blue('\n🔍 Navigating to detailed analysis...'));
|
399
|
+
console.log(chalk.cyan('Use: ') +
|
400
|
+
chalk.yellow('toknxr code-analysis') +
|
401
|
+
chalk.gray(' for deep quality insights'));
|
402
|
+
setTimeout(async () => {
|
403
|
+
try {
|
404
|
+
const { execSync } = await import('child_process');
|
405
|
+
execSync("node -e \"if(require('fs').existsSync('package.json')) { process.chdir(__dirname); require('tsx/dist/esbuild-register').register(); require('./code-analysis.ts').action(); }\" 2>/dev/null || echo \"Please run: toknxr code-analysis\"", { stdio: 'inherit' });
|
406
|
+
}
|
407
|
+
catch {
|
408
|
+
console.log(chalk.gray('Please run: ') + chalk.cyan('toknxr code-analysis'));
|
409
|
+
}
|
410
|
+
}, 500);
|
411
|
+
break;
|
412
|
+
case '2':
|
413
|
+
console.log(chalk.magenta('\n🧠 Navigating to AI performance insights...'));
|
414
|
+
console.log(chalk.cyan('Use: ') +
|
415
|
+
chalk.yellow('toknxr hallucinations') +
|
416
|
+
chalk.gray(' for hallucination analysis'));
|
417
|
+
setTimeout(async () => {
|
418
|
+
try {
|
419
|
+
const { execSync } = await import('child_process');
|
420
|
+
execSync("node -e \"if(require('fs').existsSync('package.json')) { process.chdir(__dirname); require('tsx/dist/esbuild-register').register(); require('./hallucinations.ts').action(); }\" 2>/dev/null || echo \"Please run: toknxr hallucinations\"", { stdio: 'inherit' });
|
421
|
+
}
|
422
|
+
catch {
|
423
|
+
console.log(chalk.gray('Please run: ') + chalk.cyan('toknxr hallucinations'));
|
424
|
+
}
|
425
|
+
}, 500);
|
426
|
+
break;
|
427
|
+
case '3':
|
428
|
+
console.log(chalk.yellow('\n💰 Navigating to budget optimization...'));
|
429
|
+
console.log(chalk.gray('📋 Budget Optimization Strategies:'));
|
430
|
+
console.log(` • Monthly budget: $${(50).toFixed(2)} (spent: $${grandTotals.costUSD.toFixed(2)})`);
|
431
|
+
console.log(` • Daily average: $${(grandTotals.costUSD / Math.max(1, Math.ceil((new Date().getTime() - new Date(interactions[0]?.timestamp || Date.now()).getTime()) / (1000 * 60 * 60 * 24)))).toFixed(2)}`);
|
432
|
+
console.log(` • ${avgQuality >= 80 ? 'Try cheaper providers with similar quality' : 'Focus on prompt optimization to reduce request volume'}`);
|
433
|
+
break;
|
434
|
+
case '4':
|
435
|
+
console.log(chalk.green('\n📈 Starting real-time monitoring...'));
|
436
|
+
console.log(chalk.cyan('Use: ') +
|
437
|
+
chalk.yellow('toknxr tail') +
|
438
|
+
chalk.gray(' to monitor live interactions'));
|
439
|
+
setTimeout(async () => {
|
440
|
+
try {
|
441
|
+
const { execSync } = await import('child_process');
|
442
|
+
execSync("node -e \"if(require('fs').existsSync('package.json')) { process.chdir(__dirname); require('tsx/dist/esbuild-register').register(); require('./tail.ts').action(); }\" 2>/dev/null || echo \"Please run: toknxr tail\"", { stdio: 'inherit' });
|
443
|
+
}
|
444
|
+
catch {
|
445
|
+
console.log(chalk.gray('Please run: ') + chalk.cyan('toknxr tail'));
|
446
|
+
}
|
447
|
+
}, 500);
|
448
|
+
break;
|
449
|
+
case '5':
|
450
|
+
console.log(chalk.blue('\n🚀 Starting AI refinement journey...'));
|
451
|
+
console.log(chalk.gray('📝 Improvement Recommendations:'));
|
452
|
+
if (avgQuality < 75) {
|
453
|
+
console.log(chalk.yellow(' • 🎯 Switch to higher-quality AI models (91vs88 scores)'));
|
454
|
+
console.log(chalk.yellow(' • 📝 Use more specific prompt instructions'));
|
455
|
+
}
|
456
|
+
if (grandTotals.costUSD > 25) {
|
457
|
+
console.log(chalk.red(' • 💰 Consider API usage limits or budget alerts'));
|
458
|
+
console.log(chalk.red(' • 🔄 Explore cost-efficient AI providers'));
|
459
|
+
}
|
460
|
+
if (Object.keys(stats).length === 1) {
|
461
|
+
console.log(chalk.blue(' • 🧪 A/B test multiple AI providers for your use case'));
|
462
|
+
}
|
463
|
+
break;
|
464
|
+
case 'm':
|
465
|
+
case 'menu':
|
466
|
+
console.log(chalk.gray('\n📋 Opening interactive menu...'));
|
467
|
+
setTimeout(async () => {
|
468
|
+
try {
|
469
|
+
const { spawn } = await import('child_process');
|
470
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
471
|
+
const menuCommand = spawn('node', [
|
472
|
+
'-e',
|
473
|
+
`
|
474
|
+
try {
|
475
|
+
process.chdir('${process.cwd().replace(/\\/g, '\\\\')}');
|
476
|
+
require('tsx/dist/esbuild-register').register();
|
477
|
+
const { program } = require('./cli.ts');
|
478
|
+
const menuCmd = program.commands.find(cmd => cmd.name() === 'menu');
|
479
|
+
if (menuCmd) {
|
480
|
+
menuCmd.action();
|
481
|
+
}
|
482
|
+
} catch (e) {
|
483
|
+
console.log('Please run: toknxr menu');
|
484
|
+
}
|
485
|
+
`,
|
486
|
+
], { stdio: 'inherit' });
|
487
|
+
}
|
488
|
+
catch (e) {
|
489
|
+
console.log('Please run: toknxr menu');
|
490
|
+
}
|
491
|
+
}, 300);
|
492
|
+
break;
|
493
|
+
case 'q':
|
494
|
+
case 'quit':
|
495
|
+
case 'exit':
|
496
|
+
console.log(chalk.gray('\n👋 Thanks for using TokNXR analytics!'));
|
497
|
+
console.log(chalk.gray('Raw data available in: ') + chalk.cyan('interactions.log'));
|
498
|
+
break;
|
499
|
+
default:
|
500
|
+
console.log(chalk.yellow(`Unknown option "${answer}". Showing help...`));
|
501
|
+
console.log(chalk.cyan('\nAvailable commands:'));
|
502
|
+
console.log(` ${chalk.yellow('toknxr menu')} - Interactive command menu`);
|
503
|
+
console.log(` ${chalk.yellow('toknxr stats')} - Current usage overview`);
|
504
|
+
console.log(` ${chalk.yellow('toknxr start')} - Launch proxy server`);
|
505
|
+
console.log(` ${chalk.yellow('toknxr --help')} - View all commands`);
|
506
|
+
}
|
507
|
+
rl.close();
|
508
|
+
});
|
168
509
|
});
|
169
510
|
program
|
170
511
|
.command('init')
|
@@ -183,18 +524,19 @@ program
|
|
183
524
|
const config = {
|
184
525
|
providers: [
|
185
526
|
{
|
186
|
-
name: '
|
187
|
-
routePrefix: '/gemini',
|
188
|
-
targetUrl: 'https://generativelanguage.googleapis.com/
|
527
|
+
name: 'gemini',
|
528
|
+
routePrefix: '/gemini/',
|
529
|
+
targetUrl: 'https://generativelanguage.googleapis.com/',
|
189
530
|
apiKeyEnvVar: 'GEMINI_API_KEY',
|
190
531
|
authHeader: 'x-goog-api-key',
|
532
|
+
authScheme: '',
|
191
533
|
tokenMapping: {
|
192
534
|
prompt: 'usageMetadata.promptTokenCount',
|
193
535
|
completion: 'usageMetadata.candidatesTokenCount',
|
194
|
-
total: 'usageMetadata.totalTokenCount'
|
195
|
-
}
|
196
|
-
}
|
197
|
-
]
|
536
|
+
total: 'usageMetadata.totalTokenCount',
|
537
|
+
},
|
538
|
+
},
|
539
|
+
],
|
198
540
|
};
|
199
541
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
200
542
|
console.log(chalk.green(`Created ${configPath}`));
|
@@ -208,7 +550,7 @@ program
|
|
208
550
|
version: '1',
|
209
551
|
monthlyUSD: 50,
|
210
552
|
perProviderMonthlyUSD: { 'Gemini-Pro': 30 },
|
211
|
-
webhookUrl: ''
|
553
|
+
webhookUrl: '',
|
212
554
|
};
|
213
555
|
fs.writeFileSync(policyPath, JSON.stringify(policy, null, 2));
|
214
556
|
console.log(chalk.green(`Created ${policyPath}`));
|
@@ -262,7 +604,7 @@ program
|
|
262
604
|
version: '1',
|
263
605
|
monthlyUSD: 50,
|
264
606
|
perProviderMonthlyUSD: { 'Gemini-Pro': 30 },
|
265
|
-
webhookUrl: ''
|
607
|
+
webhookUrl: '',
|
266
608
|
};
|
267
609
|
fs.writeFileSync(dest, JSON.stringify(fallback, null, 2));
|
268
610
|
console.log(chalk.green(`Created ${dest}`));
|
@@ -278,7 +620,8 @@ program
|
|
278
620
|
}
|
279
621
|
const fileContent = fs.readFileSync(logFilePath, 'utf8');
|
280
622
|
const lines = fileContent.trim().split('\n');
|
281
|
-
const interactions = lines
|
623
|
+
const interactions = lines
|
624
|
+
.map(line => {
|
282
625
|
try {
|
283
626
|
return JSON.parse(line);
|
284
627
|
}
|
@@ -286,7 +629,8 @@ program
|
|
286
629
|
console.warn(`Skipping invalid log entry: ${line}`);
|
287
630
|
return null;
|
288
631
|
}
|
289
|
-
})
|
632
|
+
})
|
633
|
+
.filter((interaction) => interaction !== null);
|
290
634
|
if (interactions.length === 0) {
|
291
635
|
console.log(chalk.yellow('No coding interactions found. Code analysis requires coding requests to the proxy.'));
|
292
636
|
return;
|
@@ -338,7 +682,9 @@ program
|
|
338
682
|
console.log(chalk.yellow(` Fair (60-74): ${effectivenessRanges.fair}`));
|
339
683
|
console.log(chalk.red(` Poor (0-59): ${effectivenessRanges.poor}`));
|
340
684
|
// Recent examples with low scores
|
341
|
-
const lowQuality = interactions
|
685
|
+
const lowQuality = interactions
|
686
|
+
.filter((i) => (i.codeQualityScore || 0) < 70)
|
687
|
+
.slice(-3);
|
342
688
|
if (lowQuality.length > 0) {
|
343
689
|
console.log(chalk.bold('\n🔍 Recent Low-Quality Code Examples:'));
|
344
690
|
lowQuality.forEach((i, idx) => {
|
@@ -348,14 +694,18 @@ program
|
|
348
694
|
const prompt = i.userPrompt.substring(0, 100);
|
349
695
|
console.log(` Prompt: ${prompt}${i.userPrompt.length > 100 ? '...' : ''}`);
|
350
696
|
}
|
351
|
-
if (i.codeQualityMetrics &&
|
697
|
+
if (i.codeQualityMetrics &&
|
698
|
+
i.codeQualityMetrics.potentialIssues &&
|
699
|
+
i.codeQualityMetrics.potentialIssues.length > 0) {
|
352
700
|
console.log(` Issues: ${i.codeQualityMetrics.potentialIssues.join(', ')}`);
|
353
701
|
}
|
354
702
|
});
|
355
703
|
}
|
356
704
|
// Improvement suggestions
|
357
|
-
const avgQuality = interactions.reduce((sum, i) => sum + (i.codeQualityScore || 0), 0) /
|
358
|
-
|
705
|
+
const avgQuality = interactions.reduce((sum, i) => sum + (i.codeQualityScore || 0), 0) /
|
706
|
+
interactions.length;
|
707
|
+
const avgEffectiveness = interactions.reduce((sum, i) => sum + (i.effectivenessScore || 0), 0) /
|
708
|
+
interactions.length;
|
359
709
|
console.log(chalk.bold('\n💡 Improvement Suggestions:'));
|
360
710
|
if (avgQuality < 70) {
|
361
711
|
console.log(' • Consider reviewing AI-generated code more carefully before use');
|
@@ -372,11 +722,858 @@ program
|
|
372
722
|
}
|
373
723
|
console.log(`\n${chalk.gray('Total coding interactions analyzed: ' + interactions.length)}`);
|
374
724
|
});
|
725
|
+
// Import required modules for new AI analysis commands
|
726
|
+
import { hallucinationDetector } from './hallucination-detector.js';
|
727
|
+
import { analyzeCodeQuality, scoreEffectiveness, extractCodeFromResponse, } from './code-analysis.js';
|
728
|
+
import { aiAnalytics } from './ai-analytics.js';
|
729
|
+
import { auditLogger, AuditEventType, initializeAuditLogging } from './audit-logger.js';
|
730
|
+
program
|
731
|
+
.command('analyze')
|
732
|
+
.description('Analyze an AI response for hallucinations and quality issues')
|
733
|
+
.argument('<prompt>', 'The original user prompt')
|
734
|
+
.argument('<response>', 'The AI response to analyze')
|
735
|
+
.option('-c, --context <context...>', 'Additional context messages')
|
736
|
+
.action((prompt, response, options) => {
|
737
|
+
console.log(chalk.bold.blue('🔍 AI Response Analysis'));
|
738
|
+
console.log(chalk.gray('━'.repeat(50)));
|
739
|
+
// Hallucination analysis
|
740
|
+
const hallucinationResult = hallucinationDetector.detectHallucination(prompt, response, options.context);
|
741
|
+
console.log(chalk.bold('\n🎯 Hallucination Detection:'));
|
742
|
+
console.log(` Status: ${hallucinationResult.isLikelyHallucination ? chalk.red('⚠️ LIKELY HALLUCINATION') : chalk.green('✅ Clean Response')}`);
|
743
|
+
console.log(` Confidence: ${hallucinationResult.confidence.toFixed(1)}%`);
|
744
|
+
console.log(` Severity: ${hallucinationResult.severity.toUpperCase()}`);
|
745
|
+
if (hallucinationResult.issues.length > 0) {
|
746
|
+
console.log(chalk.yellow('\n⚡ Issues Found:'));
|
747
|
+
hallucinationResult.issues.forEach(issue => {
|
748
|
+
console.log(` • ${issue}`);
|
749
|
+
});
|
750
|
+
}
|
751
|
+
// Extract and analyze code if present
|
752
|
+
const extractedCode = extractCodeFromResponse(response);
|
753
|
+
if (extractedCode) {
|
754
|
+
console.log(chalk.bold('\n💻 Code Quality Analysis:'));
|
755
|
+
const codeMetrics = analyzeCodeQuality(extractedCode.code, extractedCode.language);
|
756
|
+
console.log(` Language: ${codeMetrics.language || 'Unknown'}`);
|
757
|
+
console.log(` Lines of Code: ${codeMetrics.linesOfCode}`);
|
758
|
+
console.log(` Complexity: ${codeMetrics.complexity.toFixed(1)}/10`);
|
759
|
+
console.log(` Readability: ${codeMetrics.estimatedReadability}/10`);
|
760
|
+
console.log(` Syntax Valid: ${codeMetrics.syntaxValid ? chalk.green('✅') : chalk.red('❌')}`);
|
761
|
+
if (codeMetrics.potentialIssues.length > 0) {
|
762
|
+
console.log(chalk.yellow('\n⚠️ Potential Issues:'));
|
763
|
+
codeMetrics.potentialIssues.forEach(issue => {
|
764
|
+
console.log(` • ${issue}`);
|
765
|
+
});
|
766
|
+
}
|
767
|
+
}
|
768
|
+
// Effectiveness scoring
|
769
|
+
const effectiveness = scoreEffectiveness(prompt, response, extractedCode?.code);
|
770
|
+
console.log(chalk.bold('\n⚖️ Effectiveness Score:'));
|
771
|
+
console.log(` Overall: ${chalk.cyan(`${effectiveness.overallEffectiveness}/100`)}`);
|
772
|
+
console.log(` Prompt Match: ${effectiveness.promptClarityMatch.toFixed(1)}%`);
|
773
|
+
console.log(` Completeness: ${effectiveness.codeCompleteness.toFixed(1)}%`);
|
774
|
+
console.log(` Correctness: ${effectiveness.codeCorrectness.toFixed(1)}%`);
|
775
|
+
});
|
776
|
+
program
|
777
|
+
.command('quality')
|
778
|
+
.description('Analyze code quality metrics')
|
779
|
+
.argument('<code>', 'Code to analyze')
|
780
|
+
.option('-l, --language <lang>', 'Programming language')
|
781
|
+
.action((code, options) => {
|
782
|
+
console.log(chalk.bold.blue('💻 Code Quality Analysis'));
|
783
|
+
console.log(chalk.gray('━'.repeat(50)));
|
784
|
+
const metrics = analyzeCodeQuality(code, options.language);
|
785
|
+
console.log(`\n📋 Basic Metrics:`);
|
786
|
+
console.log(` Language: ${metrics.language || 'Unknown'}`);
|
787
|
+
console.log(` Lines of Code: ${metrics.linesOfCode}`);
|
788
|
+
console.log(` Complexity: ${chalk.cyan(`${metrics.complexity.toFixed(1)}/10`)}`);
|
789
|
+
console.log(` Readability: ${chalk.cyan(`${metrics.estimatedReadability}/10`)}`);
|
790
|
+
console.log(` Syntax Valid: ${metrics.syntaxValid ? chalk.green('✅') : chalk.red('❌')}`);
|
791
|
+
console.log(`\n🏗️ Structure:`);
|
792
|
+
console.log(` Has Functions: ${metrics.hasFunctions ? chalk.green('✅') : chalk.red('❌')}`);
|
793
|
+
console.log(` Has Classes: ${metrics.hasClasses ? chalk.green('✅') : chalk.red('❌')}`);
|
794
|
+
console.log(` Has Tests: ${metrics.hasTests ? chalk.green('✅') : chalk.red('❌')}`);
|
795
|
+
if (metrics.potentialIssues.length > 0) {
|
796
|
+
console.log(chalk.yellow('\n⚠️ Issues Found:'));
|
797
|
+
metrics.potentialIssues.forEach(issue => {
|
798
|
+
console.log(` • ${issue}`);
|
799
|
+
});
|
800
|
+
}
|
801
|
+
else {
|
802
|
+
console.log(chalk.green('\n✨ No issues detected!'));
|
803
|
+
}
|
804
|
+
});
|
805
|
+
program
|
806
|
+
.command('effectiveness')
|
807
|
+
.description('Score AI response effectiveness')
|
808
|
+
.argument('<prompt>', 'Original user prompt')
|
809
|
+
.argument('<response>', 'AI response to score')
|
810
|
+
.action((prompt, response) => {
|
811
|
+
console.log(chalk.bold.blue('⚖️ Effectiveness Analysis'));
|
812
|
+
console.log(chalk.gray('━'.repeat(50)));
|
813
|
+
const effectiveness = scoreEffectiveness(prompt, response);
|
814
|
+
console.log(`\n🎯 Overall Score: ${chalk.cyan(`${effectiveness.overallEffectiveness}/100`)}`);
|
815
|
+
console.log(`\n📊 Breakdown:`);
|
816
|
+
console.log(` Prompt Understanding: ${effectiveness.promptClarityMatch.toFixed(1)}%`);
|
817
|
+
console.log(` Code Completeness: ${effectiveness.codeCompleteness.toFixed(1)}%`);
|
818
|
+
console.log(` Code Correctness: ${effectiveness.codeCorrectness.toFixed(1)}%`);
|
819
|
+
console.log(` Code Efficiency: ${effectiveness.codeEfficiency.toFixed(1)}%`);
|
820
|
+
console.log(`\n💡 Interpretation:`);
|
821
|
+
if (effectiveness.overallEffectiveness >= 80) {
|
822
|
+
console.log(chalk.green(' 🌟 Excellent response quality!'));
|
823
|
+
}
|
824
|
+
else if (effectiveness.overallEffectiveness >= 60) {
|
825
|
+
console.log(chalk.blue(' 👍 Good response with minor issues'));
|
826
|
+
}
|
827
|
+
else if (effectiveness.overallEffectiveness >= 40) {
|
828
|
+
console.log(chalk.yellow(' ⚠️ Moderate quality - review needed'));
|
829
|
+
}
|
830
|
+
else {
|
831
|
+
console.log(chalk.red(' ❌ Poor quality - significant issues detected'));
|
832
|
+
}
|
833
|
+
});
|
834
|
+
program
|
835
|
+
.command('hallucinations')
|
836
|
+
.description('Show hallucination statistics and trends')
|
837
|
+
.option('-p, --provider <provider>', 'Filter by AI provider')
|
838
|
+
.option('-l, --last <hours>', 'Show last N hours (default: 24)', '24')
|
839
|
+
.action(options => {
|
840
|
+
const analytics = aiAnalytics.generateAnalytics();
|
841
|
+
console.log(chalk.bold.blue('🧠 Hallucination Analytics'));
|
842
|
+
console.log(chalk.gray('━'.repeat(50)));
|
843
|
+
console.log(`\n📊 Overall Statistics:`);
|
844
|
+
console.log(` Total Interactions: ${analytics.totalInteractions}`);
|
845
|
+
console.log(` Hallucination Rate: ${chalk.red(`${analytics.hallucinationMetrics.hallucinationRate}%`)}`);
|
846
|
+
console.log(` Avg Confidence: ${analytics.hallucinationMetrics.avgConfidence.toFixed(1)}%`);
|
847
|
+
console.log(chalk.bold('\n🏢 Business Impact:'));
|
848
|
+
const impact = analytics.hallucinationMetrics.businessImpact;
|
849
|
+
console.log(` Dev Time Wasted: ${chalk.yellow(`${impact.estimatedDevTimeWasted}h`)}`);
|
850
|
+
console.log(` Quality Degradation: ${impact.qualityDegradationScore}/100`);
|
851
|
+
console.log(` ROI Impact: ${chalk.red(`${impact.roiImpact}% reduction`)}`);
|
852
|
+
console.log(` Extra Cost: ${chalk.red(`$${impact.costOfHallucinations.toFixed(2)}`)}`);
|
853
|
+
if (Object.keys(analytics.providerComparison).length > 0) {
|
854
|
+
console.log(chalk.bold('\n🔄 Provider Comparison:'));
|
855
|
+
Object.entries(analytics.providerComparison).forEach(([provider, stats]) => {
|
856
|
+
const status = stats.hallucinationRate > 15
|
857
|
+
? chalk.red('⚠️ ')
|
858
|
+
: stats.hallucinationRate > 5
|
859
|
+
? chalk.yellow('⚡ ')
|
860
|
+
: chalk.green('✅ ');
|
861
|
+
console.log(`${status}${provider}: ${stats.hallucinationRate}% hallucination rate`);
|
862
|
+
});
|
863
|
+
}
|
864
|
+
if (analytics.recommendations.length > 0) {
|
865
|
+
console.log(chalk.bold('\n💡 Recommendations:'));
|
866
|
+
analytics.recommendations.forEach(rec => {
|
867
|
+
console.log(` • ${rec}`);
|
868
|
+
});
|
869
|
+
}
|
870
|
+
});
|
871
|
+
program
|
872
|
+
.command('providers')
|
873
|
+
.description('Compare AI provider performance')
|
874
|
+
.action(() => {
|
875
|
+
const analytics = aiAnalytics.generateAnalytics();
|
876
|
+
console.log(chalk.bold.blue('🔄 AI Provider Comparison'));
|
877
|
+
console.log(chalk.gray('━'.repeat(50)));
|
878
|
+
if (Object.keys(analytics.providerComparison).length === 0) {
|
879
|
+
console.log(chalk.yellow('No provider data available yet. Use your AI tools to generate some data!'));
|
880
|
+
return;
|
881
|
+
}
|
882
|
+
console.log(`\n📊 Provider Statistics:`);
|
883
|
+
Object.entries(analytics.providerComparison).forEach(([provider, stats]) => {
|
884
|
+
const qualityColor = stats.avgQualityScore >= 80
|
885
|
+
? chalk.green
|
886
|
+
: stats.avgQualityScore >= 60
|
887
|
+
? chalk.blue
|
888
|
+
: chalk.red;
|
889
|
+
const effectivenessColor = stats.avgEffectivenessScore >= 80
|
890
|
+
? chalk.green
|
891
|
+
: stats.avgEffectivenessScore >= 60
|
892
|
+
? chalk.blue
|
893
|
+
: chalk.red;
|
894
|
+
const hallucinationColor = stats.hallucinationRate <= 5
|
895
|
+
? chalk.green
|
896
|
+
: stats.hallucinationRate <= 15
|
897
|
+
? chalk.yellow
|
898
|
+
: chalk.red;
|
899
|
+
console.log(`\n🏢 ${chalk.bold(provider)}:`);
|
900
|
+
console.log(` Total Interactions: ${stats.totalInteractions}`);
|
901
|
+
console.log(` Hallucination Rate: ${hallucinationColor(`${stats.hallucinationRate}%`)}`);
|
902
|
+
console.log(` Avg Quality Score: ${qualityColor(`${stats.avgQualityScore}/100`)}`);
|
903
|
+
console.log(` Avg Effectiveness: ${effectivenessColor(`${stats.avgEffectivenessScore}/100`)}`);
|
904
|
+
if (stats.businessImpact.estimatedDevTimeWasted > 0) {
|
905
|
+
console.log(` Dev Time Wasted: ${chalk.yellow(`${stats.businessImpact.estimatedDevTimeWasted}h`)}`);
|
906
|
+
}
|
907
|
+
});
|
908
|
+
// Find best and worst performers
|
909
|
+
const providers = Object.entries(analytics.providerComparison);
|
910
|
+
if (providers.length > 1) {
|
911
|
+
const bestProvider = providers.sort(([, a], [, b]) => b.avgQualityScore +
|
912
|
+
b.avgEffectivenessScore -
|
913
|
+
(a.avgQualityScore + a.avgEffectivenessScore))[0];
|
914
|
+
const worstProvider = providers.sort(([, a], [, b]) => a.avgQualityScore +
|
915
|
+
a.avgEffectivenessScore -
|
916
|
+
(b.avgQualityScore + b.avgEffectivenessScore))[0];
|
917
|
+
console.log(chalk.bold('\n🏆 Performance Summary:'));
|
918
|
+
console.log(` Best Provider: ${chalk.green(bestProvider[0])} (${bestProvider[1].avgQualityScore}/100 quality)`);
|
919
|
+
console.log(` Needs Attention: ${chalk.red(worstProvider[0])} (${worstProvider[1].avgQualityScore}/100 quality)`);
|
920
|
+
}
|
921
|
+
});
|
922
|
+
program
|
923
|
+
.command('export')
|
924
|
+
.description('Export analytics data to JSON file')
|
925
|
+
.option('-o, --output <file>', 'Output file path', 'ai-analytics-export.json')
|
926
|
+
.action(options => {
|
927
|
+
try {
|
928
|
+
aiAnalytics.exportAnalytics(options.output);
|
929
|
+
console.log(chalk.green(`✅ Analytics exported to ${options.output}`));
|
930
|
+
}
|
931
|
+
catch {
|
932
|
+
console.error(chalk.red('❌ Export failed'));
|
933
|
+
}
|
934
|
+
});
|
935
|
+
// Enhanced Interactive Commands - Phase 3+
|
936
|
+
program
|
937
|
+
.command('browse')
|
938
|
+
.description('Interactive paginated browsing of all AI interactions with filtering')
|
939
|
+
.option('-p, --page <page>', 'Start page number', '1')
|
940
|
+
.option('-s, --size <size>', 'Page size (10/25/50)', '10')
|
941
|
+
.action(async (options) => {
|
942
|
+
const logFilePath = path.resolve(process.cwd(), 'interactions.log');
|
943
|
+
if (!fs.existsSync(logFilePath)) {
|
944
|
+
console.log(chalk.yellow('No interactions logged yet. Start tracking with: ') +
|
945
|
+
chalk.cyan('toknxr start'));
|
946
|
+
return;
|
947
|
+
}
|
948
|
+
const fileContent = fs.readFileSync(logFilePath, 'utf8');
|
949
|
+
const lines = fileContent.trim().split('\n');
|
950
|
+
const interactions = lines
|
951
|
+
.map(line => {
|
952
|
+
try {
|
953
|
+
return JSON.parse(line);
|
954
|
+
}
|
955
|
+
catch {
|
956
|
+
return null;
|
957
|
+
}
|
958
|
+
})
|
959
|
+
.filter((interaction) => interaction !== null);
|
960
|
+
if (interactions.length === 0) {
|
961
|
+
console.log(chalk.yellow('No valid interactions found.'));
|
962
|
+
return;
|
963
|
+
}
|
964
|
+
const explorer = new InteractiveDataExplorer(interactions);
|
965
|
+
const currentPage = parseInt(options.page) || 1;
|
966
|
+
const pageSize = parseInt(options.size) || 10;
|
967
|
+
explorer.setPageSize(pageSize);
|
968
|
+
explorer.setPage(currentPage);
|
969
|
+
const renderInteraction = (interaction, index) => {
|
970
|
+
const num = index + 1;
|
971
|
+
const costColor = interaction.costUSD > 0.1
|
972
|
+
? chalk.red
|
973
|
+
: interaction.costUSD > 0.05
|
974
|
+
? chalk.yellow
|
975
|
+
: chalk.green;
|
976
|
+
const qualityColor = (interaction.codeQualityScore || 0) >= 80
|
977
|
+
? chalk.green
|
978
|
+
: (interaction.codeQualityScore || 0) >= 60
|
979
|
+
? chalk.blue
|
980
|
+
: chalk.red;
|
981
|
+
return createBox(`#${num} ${interaction.provider}`, [
|
982
|
+
`📅 ${chalk.gray(new Date(interaction.timestamp || Date.now()).toLocaleDateString())}`,
|
983
|
+
`💰 Cost: ${costColor(`$${interaction.costUSD?.toFixed(4) || '0.0000'}`)}`,
|
984
|
+
`🎯 Effectiveness: ${interaction.effectivenessScore || 'N/A'}/100`,
|
985
|
+
`⭐ Quality: ${qualityColor(`${interaction.codeQualityScore || 'N/A'}/100`)}`,
|
986
|
+
`🔤 ${interaction.totalTokens || 0} tokens • ${interaction.model || 'Unknown model'}`,
|
987
|
+
...(interaction.userPrompt
|
988
|
+
? [
|
989
|
+
`"${interaction.userPrompt.substring(0, 60)}${interaction.userPrompt.length > 60 ? '...' : ''}"`,
|
990
|
+
]
|
991
|
+
: []),
|
992
|
+
], {
|
993
|
+
borderColor: 'gray',
|
994
|
+
titleColor: 'cyan',
|
995
|
+
width: 80,
|
996
|
+
});
|
997
|
+
};
|
998
|
+
console.log(createPaginatedDisplay(explorer.getCurrentPageData(), pageSize, currentPage, renderInteraction, `🐙 AI Interactions Browser (${explorer.getPaginationInfo().totalItems} total)`));
|
999
|
+
// Interactive navigation
|
1000
|
+
const pagination = explorer.getPaginationInfo();
|
1001
|
+
if (pagination.totalPages > 1) {
|
1002
|
+
console.log(chalk.cyan('\nNavigation:'));
|
1003
|
+
console.log(chalk.gray('• Use ') +
|
1004
|
+
chalk.yellow('toknxr browse --page 2') +
|
1005
|
+
chalk.gray(' to go to page 2'));
|
1006
|
+
console.log(chalk.gray('• Use ') +
|
1007
|
+
chalk.yellow('toknxr browse --size 25') +
|
1008
|
+
chalk.gray(' for more items per page'));
|
1009
|
+
console.log(chalk.gray('• Use ') + chalk.yellow('toknxr filter') + chalk.gray(' to filter results'));
|
1010
|
+
}
|
1011
|
+
});
|
375
1012
|
program
|
376
|
-
.command('
|
377
|
-
.description('
|
1013
|
+
.command('filter')
|
1014
|
+
.description('Interactive filtering interface for AI interactions')
|
378
1015
|
.action(async () => {
|
379
|
-
|
380
|
-
|
1016
|
+
try {
|
1017
|
+
console.log(chalk.blue.bold('\n🔍 Advanced Filtering'));
|
1018
|
+
console.log(chalk.gray('Configure filters that will persist across sessions\n'));
|
1019
|
+
const currentFilters = (CliStateManager.getPreferences().filters || {});
|
1020
|
+
const newFilters = await createFilterInterface(currentFilters);
|
1021
|
+
console.log(chalk.green('\n✅ Filters applied and saved!'));
|
1022
|
+
console.log(chalk.gray('Use ') + chalk.cyan('toknxr browse') + chalk.gray(' to see filtered results'));
|
1023
|
+
console.log(chalk.gray('Use ') + chalk.cyan('toknxr filter') + chalk.gray(' again to modify filters'));
|
1024
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
1025
|
+
}
|
1026
|
+
catch (error) {
|
1027
|
+
console.log(chalk.red('❌ Failed to apply filters'));
|
1028
|
+
console.log(chalk.gray('Check your terminal supports interactive prompts'));
|
1029
|
+
}
|
1030
|
+
});
|
1031
|
+
program
|
1032
|
+
.command('search')
|
1033
|
+
.description('Search across all AI interactions with field selection')
|
1034
|
+
.option('-q, --query <query>', 'Search query (minimum 2 characters)')
|
1035
|
+
.action(async (options) => {
|
1036
|
+
const query = options.query;
|
1037
|
+
if (!query || query.trim().length < 2) {
|
1038
|
+
console.log(chalk.yellow('Please provide a search query with at least 2 characters:'));
|
1039
|
+
console.log(chalk.cyan(' toknxr search --query "your search terms"'));
|
1040
|
+
return;
|
1041
|
+
}
|
1042
|
+
try {
|
1043
|
+
console.log(chalk.blue.bold(`\n🔍 Searching for: "${query}"`));
|
1044
|
+
const availableFields = ['provider', 'model', 'userPrompt', 'taskType', 'requestId'];
|
1045
|
+
const searchOptions = await createSearchInterface(availableFields);
|
1046
|
+
if (!searchOptions) {
|
1047
|
+
console.log(chalk.gray('Search cancelled.'));
|
1048
|
+
return;
|
1049
|
+
}
|
1050
|
+
const logFilePath = path.resolve(process.cwd(), 'interactions.log');
|
1051
|
+
if (!fs.existsSync(logFilePath)) {
|
1052
|
+
console.log(chalk.yellow('No interactions logged yet.'));
|
1053
|
+
return;
|
1054
|
+
}
|
1055
|
+
const fileContent = fs.readFileSync(logFilePath, 'utf8');
|
1056
|
+
const lines = fileContent.trim().split('\n');
|
1057
|
+
const interactions = lines
|
1058
|
+
.map(line => {
|
1059
|
+
try {
|
1060
|
+
return JSON.parse(line);
|
1061
|
+
}
|
1062
|
+
catch {
|
1063
|
+
return null;
|
1064
|
+
}
|
1065
|
+
})
|
1066
|
+
.filter((interaction) => interaction !== null);
|
1067
|
+
const filteredResults = filterAndSearchInteractions(interactions, {}, searchOptions);
|
1068
|
+
if (filteredResults.length === 0) {
|
1069
|
+
console.log(chalk.yellow(`\nNo results found for "${query}" in the selected fields.`));
|
1070
|
+
return;
|
1071
|
+
}
|
1072
|
+
const explorer = new InteractiveDataExplorer(filteredResults);
|
1073
|
+
console.log(chalk.green(`\n✅ Found ${filteredResults.length} matching interactions`));
|
1074
|
+
console.log(chalk.gray(`Searching in: ${searchOptions.fields.join(', ')}\n`));
|
1075
|
+
const providerCounts = {};
|
1076
|
+
filteredResults.forEach((i) => {
|
1077
|
+
providerCounts[i.provider] = (providerCounts[i.provider] || 0) + 1;
|
1078
|
+
});
|
1079
|
+
console.log(createBox('📊 Search Results Summary', [
|
1080
|
+
`Total Results: ${filteredResults.length}`,
|
1081
|
+
`Providers: ${Object.entries(providerCounts)
|
1082
|
+
.map(([p, c]) => `${p}(${c})`)
|
1083
|
+
.join(', ')}`,
|
1084
|
+
'',
|
1085
|
+
chalk.gray('Use ↑/↓ or page up/down to navigate results'),
|
1086
|
+
], { borderColor: 'green', titleColor: 'green' }));
|
1087
|
+
const renderSearchResult = (interaction, index) => {
|
1088
|
+
const num = index + 1;
|
1089
|
+
const score = calcRelevanceScore(interaction, searchOptions.query, searchOptions.fields);
|
1090
|
+
let highlightColor;
|
1091
|
+
if (score >= 0.8)
|
1092
|
+
highlightColor = 'green';
|
1093
|
+
else if (score >= 0.6)
|
1094
|
+
highlightColor = 'yellow';
|
1095
|
+
else
|
1096
|
+
highlightColor = 'red';
|
1097
|
+
const highlightedPrompt = highlightMatch(interaction.userPrompt || '', searchOptions.query);
|
1098
|
+
const colorFn = highlightColor === 'green'
|
1099
|
+
? chalk.green
|
1100
|
+
: highlightColor === 'yellow'
|
1101
|
+
? chalk.yellow
|
1102
|
+
: chalk.red;
|
1103
|
+
return createBox(`#${num} ${interaction.provider} (${colorFn('★'.repeat(Math.ceil(score * 5)))})`, [
|
1104
|
+
`📅 ${chalk.gray(new Date(interaction.timestamp || Date.now()).toLocaleDateString())}`,
|
1105
|
+
`🎯 ${highlightedPrompt || 'No prompt available'}`,
|
1106
|
+
`💰 $${interaction.costUSD?.toFixed(4) || '0.0000'} • ⭐ ${interaction.codeQualityScore || 'N/A'}/100`,
|
1107
|
+
], {
|
1108
|
+
borderColor: highlightColor,
|
1109
|
+
titleColor: 'cyan',
|
1110
|
+
width: 90,
|
1111
|
+
});
|
1112
|
+
};
|
1113
|
+
console.log(createPaginatedDisplay(explorer.getCurrentPageData(), 5, // Smaller page size for search results
|
1114
|
+
1, renderSearchResult, undefined));
|
1115
|
+
}
|
1116
|
+
catch (error) {
|
1117
|
+
console.log(chalk.red('❌ Search failed'));
|
1118
|
+
console.log(chalk.gray('Try using: ') + chalk.cyan('toknxr search --query "your terms"'));
|
1119
|
+
}
|
1120
|
+
});
|
1121
|
+
program
|
1122
|
+
.command('budget')
|
1123
|
+
.description('Manage budget settings and view spending analytics')
|
1124
|
+
.option('--set <amount>', 'Set monthly budget amount')
|
1125
|
+
.option('--provider <provider>', 'Set budget for specific provider')
|
1126
|
+
.option('--view', 'View current budget settings')
|
1127
|
+
.action(options => {
|
1128
|
+
if (options.set) {
|
1129
|
+
const amount = parseFloat(options.set);
|
1130
|
+
if (isNaN(amount) || amount <= 0) {
|
1131
|
+
console.log(chalk.red('❌ Invalid budget amount. Must be a positive number.'));
|
1132
|
+
return;
|
1133
|
+
}
|
1134
|
+
CliStateManager.updateSessionBudget(amount, options.provider);
|
1135
|
+
const budgetType = options.provider ? ` for ${options.provider}` : ' (monthly default)';
|
1136
|
+
console.log(chalk.green(`✅ Budget updated to $${amount.toFixed(2)}${budgetType}`));
|
1137
|
+
if (!options.provider) {
|
1138
|
+
console.log(chalk.cyan('\n💡 Tip: Set provider-specific budgets for granular control'));
|
1139
|
+
console.log(chalk.gray('Example: ') + chalk.yellow('toknxr budget --set 25 --provider "Gemini-Pro"'));
|
1140
|
+
}
|
1141
|
+
}
|
1142
|
+
else if (options.view || Object.keys(options).length === 0) {
|
1143
|
+
const preferences = CliStateManager.getPreferences();
|
1144
|
+
const budgets = CliStateManager.loadState().budgets;
|
1145
|
+
console.log(chalk.blue.bold('\n💰 Budget Configuration'));
|
1146
|
+
console.log(chalk.gray('━'.repeat(50)));
|
1147
|
+
console.log(chalk.bold('\nCurrent Settings:'));
|
1148
|
+
console.log(`📊 Monthly Default: ${chalk.cyan(`$${budgets.default || 50.0}`)}`);
|
1149
|
+
console.log(`📏 Page Size: ${preferences.pageSize || 10} items`);
|
1150
|
+
console.log(`🔧 Sort Order: ${preferences.sortOrder || 'date_descends'}`);
|
1151
|
+
if (Object.keys(budgets).length > 1) {
|
1152
|
+
console.log(chalk.bold('\nProvider-Specific Budgets:'));
|
1153
|
+
Object.entries(budgets).forEach(([provider, amount]) => {
|
1154
|
+
if (provider !== 'default') {
|
1155
|
+
console.log(`🏢 ${provider}: ${chalk.cyan(`$${amount}`)}`);
|
1156
|
+
}
|
1157
|
+
});
|
1158
|
+
}
|
1159
|
+
console.log(chalk.bold('\n💡 Budget Commands:'));
|
1160
|
+
console.log(` ${chalk.yellow('toknxr budget --set 75')} - Set monthly budget`);
|
1161
|
+
console.log(` ${chalk.yellow('toknxr budget --set 30 --provider GPT4')} - Set provider budget`);
|
1162
|
+
}
|
1163
|
+
});
|
1164
|
+
// Phase 5: Enterprise Audit Logging Commands
|
1165
|
+
program
|
1166
|
+
.command('audit:init')
|
1167
|
+
.description('Initialize enterprise audit logging system')
|
1168
|
+
.option('--encrypt', 'Enable audit log encryption')
|
1169
|
+
.option('--retention <days>', 'Log retention period in days', '365')
|
1170
|
+
.action(options => {
|
1171
|
+
try {
|
1172
|
+
initializeAuditLogging({
|
1173
|
+
encryptionEnabled: options.encrypt || false,
|
1174
|
+
retentionDays: parseInt(options.retention) || 365,
|
1175
|
+
enabled: true,
|
1176
|
+
});
|
1177
|
+
// Log the initialization event
|
1178
|
+
auditLogger.logAuthEvent(AuditEventType.SYSTEM_MAINTENANCE, 'system', true, {
|
1179
|
+
component: 'audit_system',
|
1180
|
+
action: 'initialization',
|
1181
|
+
settings: {
|
1182
|
+
encryptionEnabled: options.encrypt || false,
|
1183
|
+
retentionDays: parseInt(options.retention) || 365,
|
1184
|
+
},
|
1185
|
+
});
|
1186
|
+
console.log(chalk.green('✅ Enterprise audit logging initialized'));
|
1187
|
+
console.log(chalk.gray('Audit logs will be written to: audit.log'));
|
1188
|
+
if (options.encrypt) {
|
1189
|
+
console.log(chalk.yellow('🛡️ Audit logs are encrypted'));
|
1190
|
+
}
|
1191
|
+
}
|
1192
|
+
catch (error) {
|
1193
|
+
console.log(chalk.red('❌ Failed to initialize audit logging'));
|
1194
|
+
console.log(chalk.gray(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
|
1195
|
+
}
|
381
1196
|
});
|
1197
|
+
program
|
1198
|
+
.command('audit:view')
|
1199
|
+
.description('View audit events with filtering options')
|
1200
|
+
.option('-t, --type <eventType>', 'Filter by event type (e.g., ai.request, auth.login)')
|
1201
|
+
.option('-u, --user <userId>', 'Filter by user ID')
|
1202
|
+
.option('-r, --risk <level>', 'Filter by risk level (low/medium/high/critical)')
|
1203
|
+
.option('-f, --from <date>', 'Filter events from date (ISO format)')
|
1204
|
+
.option('--to <date>', 'Filter events to date (ISO format)')
|
1205
|
+
.option('-l, --limit <number>', 'Limit number of results', '50')
|
1206
|
+
.action(options => {
|
1207
|
+
try {
|
1208
|
+
const events = auditLogger.query({
|
1209
|
+
eventType: options.type,
|
1210
|
+
userId: options.user,
|
1211
|
+
riskLevel: options.risk,
|
1212
|
+
dateFrom: options.from,
|
1213
|
+
dateTo: options.to,
|
1214
|
+
limit: parseInt(options.limit) || 50,
|
1215
|
+
});
|
1216
|
+
if (events.length === 0) {
|
1217
|
+
console.log(chalk.yellow('No audit events found matching the criteria.'));
|
1218
|
+
return;
|
1219
|
+
}
|
1220
|
+
console.log(chalk.blue.bold('📋 Audit Events'));
|
1221
|
+
console.log(chalk.gray('━'.repeat(80)));
|
1222
|
+
// Group events by date for better readability
|
1223
|
+
const eventsByDate = {};
|
1224
|
+
events.forEach(event => {
|
1225
|
+
const date = new Date(event.timestamp).toLocaleDateString();
|
1226
|
+
if (!eventsByDate[date])
|
1227
|
+
eventsByDate[date] = [];
|
1228
|
+
eventsByDate[date].push(event);
|
1229
|
+
});
|
1230
|
+
Object.entries(eventsByDate).forEach(([date, dateEvents]) => {
|
1231
|
+
console.log(chalk.cyan(`\n📅 ${date}`));
|
1232
|
+
dateEvents.forEach(event => {
|
1233
|
+
const time = new Date(event.timestamp).toLocaleTimeString();
|
1234
|
+
const riskColor = event.riskLevel === 'critical'
|
1235
|
+
? chalk.red
|
1236
|
+
: event.riskLevel === 'high'
|
1237
|
+
? chalk.yellow
|
1238
|
+
: event.riskLevel === 'medium'
|
1239
|
+
? chalk.blue
|
1240
|
+
: chalk.gray;
|
1241
|
+
const resultIcon = event.result === 'success' ? '✅' : event.result === 'failure' ? '❌' : '⚠️';
|
1242
|
+
console.log(` ${resultIcon} ${chalk.bold(event.eventType)} ${riskColor(`[${event.riskLevel}]`)}`);
|
1243
|
+
console.log(` ${chalk.gray(time)} | ${event.action} | ${event.resource}`);
|
1244
|
+
if (event.userId) {
|
1245
|
+
console.log(` 👤 User: ${event.userId}`);
|
1246
|
+
}
|
1247
|
+
if (event.details && Object.keys(event.details).length > 0) {
|
1248
|
+
const details = Object.entries(event.details)
|
1249
|
+
.filter(([key]) => !['method', 'component'].includes(key))
|
1250
|
+
.slice(0, 3) // Limit to 3 details
|
1251
|
+
.map(([key, value]) => `${key}: ${typeof value === 'object' ? JSON.stringify(value) : value}`)
|
1252
|
+
.join(', ');
|
1253
|
+
if (details) {
|
1254
|
+
console.log(` 📋 ${details}`);
|
1255
|
+
}
|
1256
|
+
}
|
1257
|
+
});
|
1258
|
+
});
|
1259
|
+
console.log(chalk.gray(`\nTotal events shown: ${events.length}`));
|
1260
|
+
// Log the audit access itself
|
1261
|
+
auditLogger.log({
|
1262
|
+
eventType: AuditEventType.AUDIT_LOG_ACCESS,
|
1263
|
+
action: 'audit_view',
|
1264
|
+
resource: 'audit_logs',
|
1265
|
+
result: 'success',
|
1266
|
+
riskLevel: 'low',
|
1267
|
+
complianceTags: ['audit', 'access_control'],
|
1268
|
+
details: {
|
1269
|
+
filterCriteria: options,
|
1270
|
+
resultsReturned: events.length,
|
1271
|
+
},
|
1272
|
+
metadata: {
|
1273
|
+
version: '1.0.0',
|
1274
|
+
environment: process.env.NODE_ENV || 'development',
|
1275
|
+
component: 'audit_cli',
|
1276
|
+
},
|
1277
|
+
});
|
1278
|
+
}
|
1279
|
+
catch (error) {
|
1280
|
+
console.log(chalk.red('❌ Failed to retrieve audit events'));
|
1281
|
+
console.log(chalk.gray(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
|
1282
|
+
}
|
1283
|
+
});
|
1284
|
+
program
|
1285
|
+
.command('audit:report')
|
1286
|
+
.description('Generate compliance report for specified time period')
|
1287
|
+
.argument('<startDate>', 'Start date in ISO format (YYYY-MM-DD)')
|
1288
|
+
.argument('<endDate>', 'End date in ISO format (YYYY-MM-DD)')
|
1289
|
+
.action((startDate, endDate) => {
|
1290
|
+
try {
|
1291
|
+
console.log(chalk.blue.bold('📊 Compliance Report Generation'));
|
1292
|
+
console.log(chalk.gray('━'.repeat(50)));
|
1293
|
+
const report = auditLogger.generateComplianceReport(startDate, endDate);
|
1294
|
+
console.log(chalk.bold('\n📅 Report Period:'));
|
1295
|
+
console.log(` From: ${new Date(report.period.start).toLocaleDateString()}`);
|
1296
|
+
console.log(` To: ${new Date(report.period.end).toLocaleDateString()}`);
|
1297
|
+
console.log(chalk.bold('\n📈 Summary Statistics:'));
|
1298
|
+
console.log(` Total Events: ${report.totalEvents.toLocaleString()}`);
|
1299
|
+
console.log(chalk.bold('\n🏷️ Risk Distribution:'));
|
1300
|
+
Object.entries(report.riskSummary).forEach(([level, count]) => {
|
1301
|
+
const color = level === 'critical'
|
1302
|
+
? chalk.red
|
1303
|
+
: level === 'high'
|
1304
|
+
? chalk.yellow
|
1305
|
+
: level === 'medium'
|
1306
|
+
? chalk.blue
|
1307
|
+
: chalk.gray;
|
1308
|
+
console.log(` ${color(level.charAt(0).toUpperCase() + level.slice(1))}: ${count}`);
|
1309
|
+
});
|
1310
|
+
if (Object.keys(report.eventsByType).length > 0) {
|
1311
|
+
console.log(chalk.bold('\n📋 Events by Type:'));
|
1312
|
+
Object.entries(report.eventsByType)
|
1313
|
+
.sort(([, a], [, b]) => b - a)
|
1314
|
+
.slice(0, 10) // Top 10
|
1315
|
+
.forEach(([type, count]) => {
|
1316
|
+
console.log(` ${type}: ${count}`);
|
1317
|
+
});
|
1318
|
+
}
|
1319
|
+
if (report.complianceViolations.length > 0) {
|
1320
|
+
console.log(chalk.red.bold('\n🚨 Compliance Violations:'));
|
1321
|
+
report.complianceViolations.slice(0, 5).forEach(violation => {
|
1322
|
+
console.log(` ❌ ${violation.eventType} - ${violation.action}`);
|
1323
|
+
console.log(` ${new Date(violation.timestamp).toLocaleString()}`);
|
1324
|
+
});
|
1325
|
+
if (report.complianceViolations.length > 5) {
|
1326
|
+
console.log(chalk.red(` ... and ${report.complianceViolations.length - 5} more violations`));
|
1327
|
+
}
|
1328
|
+
}
|
1329
|
+
if (report.recommendations.length > 0) {
|
1330
|
+
console.log(chalk.cyan.bold('\n💡 Recommendations:'));
|
1331
|
+
report.recommendations.forEach(rec => {
|
1332
|
+
console.log(` • ${rec}`);
|
1333
|
+
});
|
1334
|
+
}
|
1335
|
+
// Log the compliance report generation
|
1336
|
+
auditLogger.log({
|
1337
|
+
eventType: AuditEventType.COMPLIANCE_REPORT,
|
1338
|
+
action: 'report_generation',
|
1339
|
+
resource: 'audit_system',
|
1340
|
+
result: 'success',
|
1341
|
+
riskLevel: 'low',
|
1342
|
+
complianceTags: ['compliance', 'reporting'],
|
1343
|
+
details: {
|
1344
|
+
reportPeriod: report.period,
|
1345
|
+
totalEvents: report.totalEvents,
|
1346
|
+
violationsFound: report.complianceViolations.length,
|
1347
|
+
},
|
1348
|
+
metadata: {
|
1349
|
+
version: '1.0.0',
|
1350
|
+
environment: process.env.NODE_ENV || 'development',
|
1351
|
+
component: 'audit_cli',
|
1352
|
+
},
|
1353
|
+
});
|
1354
|
+
console.log(chalk.green('\n✅ Compliance report generated successfully'));
|
1355
|
+
}
|
1356
|
+
catch (error) {
|
1357
|
+
console.log(chalk.red('❌ Failed to generate compliance report'));
|
1358
|
+
console.log(chalk.gray(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
|
1359
|
+
}
|
1360
|
+
});
|
1361
|
+
program
|
1362
|
+
.command('audit:export')
|
1363
|
+
.description('Export audit data in various formats')
|
1364
|
+
.argument('<format>', 'Export format: json, csv, or xml')
|
1365
|
+
.option('-o, --output <file>', 'Output file name', 'audit-export')
|
1366
|
+
.option('-t, --type <eventType>', 'Filter by event type')
|
1367
|
+
.option('-u, --user <userId>', 'Filter by user ID')
|
1368
|
+
.option('-f, --from <date>', 'Filter events from date (ISO format)')
|
1369
|
+
.option('--to <date>', 'Filter events to date (ISO format)')
|
1370
|
+
.action((format, options) => {
|
1371
|
+
try {
|
1372
|
+
const events = auditLogger.query({
|
1373
|
+
eventType: options.type,
|
1374
|
+
userId: options.user,
|
1375
|
+
dateFrom: options.from,
|
1376
|
+
dateTo: options.to,
|
1377
|
+
});
|
1378
|
+
if (events.length === 0) {
|
1379
|
+
console.log(chalk.yellow('No audit events found to export.'));
|
1380
|
+
return;
|
1381
|
+
}
|
1382
|
+
const fileName = `${options.output}.${format}`;
|
1383
|
+
const exportData = auditLogger.exportAuditData(format);
|
1384
|
+
fs.writeFileSync(fileName, exportData);
|
1385
|
+
console.log(chalk.green(`✅ Exported ${events.length} audit events to ${fileName}`));
|
1386
|
+
console.log(chalk.gray(`Format: ${format.toUpperCase()} | Size: ${exportData.length} bytes`));
|
1387
|
+
// Log the export event
|
1388
|
+
auditLogger.log({
|
1389
|
+
eventType: AuditEventType.AUDIT_LOG_ACCESS,
|
1390
|
+
action: 'audit_export',
|
1391
|
+
resource: 'audit_logs',
|
1392
|
+
resourceId: fileName,
|
1393
|
+
result: 'success',
|
1394
|
+
riskLevel: 'medium', // Exporting sensitive audit data
|
1395
|
+
complianceTags: ['audit', 'data_export'],
|
1396
|
+
details: {
|
1397
|
+
format,
|
1398
|
+
recordsExported: events.length,
|
1399
|
+
fileName,
|
1400
|
+
filterCriteria: {
|
1401
|
+
eventType: options.type,
|
1402
|
+
userId: options.user,
|
1403
|
+
dateFrom: options.from,
|
1404
|
+
dateTo: options.to,
|
1405
|
+
},
|
1406
|
+
},
|
1407
|
+
metadata: {
|
1408
|
+
version: '1.0.0',
|
1409
|
+
environment: process.env.NODE_ENV || 'development',
|
1410
|
+
component: 'audit_cli',
|
1411
|
+
},
|
1412
|
+
});
|
1413
|
+
}
|
1414
|
+
catch (error) {
|
1415
|
+
console.log(chalk.red('❌ Failed to export audit data'));
|
1416
|
+
console.log(chalk.gray(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
|
1417
|
+
}
|
1418
|
+
});
|
1419
|
+
program
|
1420
|
+
.command('audit:stats')
|
1421
|
+
.description('Show audit log statistics and health metrics')
|
1422
|
+
.action(() => {
|
1423
|
+
try {
|
1424
|
+
// This is a simple stats command - in real implementation,
|
1425
|
+
// we'd want to expose more audit logger internal stats
|
1426
|
+
console.log(chalk.blue.bold('📊 Audit System Statistics'));
|
1427
|
+
console.log(chalk.gray('━'.repeat(50)));
|
1428
|
+
console.log(chalk.bold('\n🗂️ Log File Information:'));
|
1429
|
+
try {
|
1430
|
+
const logPath = path.resolve(process.cwd(), 'audit.log');
|
1431
|
+
if (fs.existsSync(logPath)) {
|
1432
|
+
const stats = fs.statSync(logPath);
|
1433
|
+
console.log(` Location: ${logPath}`);
|
1434
|
+
console.log(` Size: ${stats.size} bytes (${(stats.size / 1024).toFixed(1)} KB)`);
|
1435
|
+
console.log(` Modified: ${stats.mtime.toLocaleString()}`);
|
1436
|
+
console.log(` Encrypted: ${auditLogger['config'].encryptionEnabled ? 'Yes' : 'No'}`);
|
1437
|
+
}
|
1438
|
+
else {
|
1439
|
+
console.log(chalk.yellow(' No audit log file found'));
|
1440
|
+
}
|
1441
|
+
}
|
1442
|
+
catch (error) {
|
1443
|
+
console.log(chalk.red(' Error reading log file'));
|
1444
|
+
}
|
1445
|
+
console.log(chalk.bold('\n⚙️ System Configuration:'));
|
1446
|
+
console.log(` Enabled: ${auditLogger['config'].enabled ? chalk.green('Yes') : chalk.red('No')}`);
|
1447
|
+
console.log(` Retention: ${auditLogger['config'].retentionDays} days`);
|
1448
|
+
console.log(` Max File Size: ${auditLogger['config'].maxFileSize} MB`);
|
1449
|
+
console.log(` Alert Threshold: ${auditLogger['config'].alertThresholds.riskLevelThreshold}`);
|
1450
|
+
console.log(` Compliance Frameworks: ${auditLogger['config'].complianceFrameworks.join(', ')}`);
|
1451
|
+
console.log(chalk.bold('\n🎯 Quick Commands:'));
|
1452
|
+
console.log(` ${chalk.cyan('toknxr audit:view')} - View recent audit events`);
|
1453
|
+
console.log(` ${chalk.cyan('toknxr audit:report 2025-01-01 2025-01-31')} - Generate compliance report`);
|
1454
|
+
console.log(` ${chalk.cyan('toknxr audit:export json --output my-audit')} - Export audit data`);
|
1455
|
+
// Log the stats access
|
1456
|
+
auditLogger.log({
|
1457
|
+
eventType: AuditEventType.AUDIT_LOG_ACCESS,
|
1458
|
+
action: 'audit_stats_view',
|
1459
|
+
resource: 'audit_system',
|
1460
|
+
result: 'success',
|
1461
|
+
riskLevel: 'low',
|
1462
|
+
complianceTags: ['audit', 'monitoring'],
|
1463
|
+
details: { accessedVia: 'cli_stats_command' },
|
1464
|
+
metadata: {
|
1465
|
+
version: '1.0.0',
|
1466
|
+
environment: process.env.NODE_ENV || 'development',
|
1467
|
+
component: 'audit_cli',
|
1468
|
+
},
|
1469
|
+
});
|
1470
|
+
}
|
1471
|
+
catch (error) {
|
1472
|
+
console.log(chalk.red('❌ Failed to retrieve audit statistics'));
|
1473
|
+
console.log(chalk.gray(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
|
1474
|
+
}
|
1475
|
+
});
|
1476
|
+
// ---------------------------------------------------------------------------
|
1477
|
+
// Doctor: environment and runtime validator
|
1478
|
+
// ---------------------------------------------------------------------------
|
1479
|
+
program
|
1480
|
+
.command('doctor')
|
1481
|
+
.description('Validate environment, config, and runtime readiness')
|
1482
|
+
.action(async () => {
|
1483
|
+
const results = [];
|
1484
|
+
// Environment checks
|
1485
|
+
const geminiKey = process.env.GEMINI_API_KEY;
|
1486
|
+
const openaiKey = process.env.OPENAI_API_KEY;
|
1487
|
+
const anthropicKey = process.env.ANTHROPIC_API_KEY;
|
1488
|
+
results.push({
|
1489
|
+
label: 'AI Provider API key',
|
1490
|
+
ok: !!(geminiKey || openaiKey || anthropicKey),
|
1491
|
+
hint: 'Set GEMINI_API_KEY, OPENAI_API_KEY, or ANTHROPIC_API_KEY in your environment',
|
1492
|
+
});
|
1493
|
+
// Filesystem checks
|
1494
|
+
const configPath = path.resolve(process.cwd(), 'toknxr.config.json');
|
1495
|
+
results.push({
|
1496
|
+
label: `Provider config at ${configPath}`,
|
1497
|
+
ok: fs.existsSync(configPath),
|
1498
|
+
hint: 'Run: toknxr init',
|
1499
|
+
});
|
1500
|
+
const logPath = path.resolve(process.cwd(), 'interactions.log');
|
1501
|
+
try {
|
1502
|
+
// Touch file if missing (no-op if exists)
|
1503
|
+
if (!fs.existsSync(logPath))
|
1504
|
+
fs.writeFileSync(logPath, '');
|
1505
|
+
results.push({ label: `interactions.log at ${logPath}`, ok: true });
|
1506
|
+
}
|
1507
|
+
catch {
|
1508
|
+
results.push({ label: `interactions.log at ${logPath}`, ok: false, hint: 'Check write permissions' });
|
1509
|
+
}
|
1510
|
+
// Runtime checks (proxy health if running)
|
1511
|
+
let healthOk = false;
|
1512
|
+
try {
|
1513
|
+
const res = await fetch('http://localhost:8788/health');
|
1514
|
+
healthOk = res.ok;
|
1515
|
+
}
|
1516
|
+
catch {
|
1517
|
+
healthOk = false;
|
1518
|
+
}
|
1519
|
+
results.push({
|
1520
|
+
label: 'Proxy reachable at http://localhost:8788/health',
|
1521
|
+
ok: healthOk,
|
1522
|
+
hint: 'Run: toknxr start (then retry doctor)'
|
1523
|
+
});
|
1524
|
+
// Optional: add one fixture interaction if none exists and provider key missing
|
1525
|
+
try {
|
1526
|
+
const file = path.resolve(process.cwd(), 'interactions.log');
|
1527
|
+
const hasKey = !!(process.env.GEMINI_API_KEY || process.env.OPENAI_API_KEY);
|
1528
|
+
const size = fs.existsSync(file) ? fs.statSync(file).size : 0;
|
1529
|
+
if (!hasKey && size === 0) {
|
1530
|
+
const { appendFixtureInteraction } = await import('./fixtures/canary-interaction.js');
|
1531
|
+
appendFixtureInteraction();
|
1532
|
+
}
|
1533
|
+
}
|
1534
|
+
catch { }
|
1535
|
+
// Print report
|
1536
|
+
console.log(chalk.blue.bold('\nTokNXR Doctor Report'));
|
1537
|
+
console.log(chalk.gray('━'.repeat(60)));
|
1538
|
+
let allOk = true;
|
1539
|
+
for (const r of results) {
|
1540
|
+
allOk && (allOk = r.ok);
|
1541
|
+
const mark = r.ok ? chalk.green('✔') : chalk.red('✖');
|
1542
|
+
const line = `${mark} ${r.label}` + (r.ok ? '' : chalk.gray(` — ${r.hint}`));
|
1543
|
+
console.log(line);
|
1544
|
+
}
|
1545
|
+
console.log(chalk.gray('━'.repeat(60)));
|
1546
|
+
if (allOk) {
|
1547
|
+
console.log(chalk.green('All checks passed. You are ready to use TokNXR.'));
|
1548
|
+
}
|
1549
|
+
else {
|
1550
|
+
console.log(chalk.yellow('Some checks failed. Fix the hints above and re-run: toknxr doctor'));
|
1551
|
+
}
|
1552
|
+
});
|
1553
|
+
// Helper functions for search highlighting
|
1554
|
+
function calcRelevanceScore(interaction, query, fields) {
|
1555
|
+
const queryTerms = query.toLowerCase().split(' ');
|
1556
|
+
let totalScore = 0;
|
1557
|
+
const maxPossibleScore = fields.length * queryTerms.length;
|
1558
|
+
fields.forEach(field => {
|
1559
|
+
const fieldValue = (interaction[field] || '').toString().toLowerCase();
|
1560
|
+
queryTerms.forEach(term => {
|
1561
|
+
if (fieldValue.includes(term)) {
|
1562
|
+
totalScore += 1;
|
1563
|
+
}
|
1564
|
+
});
|
1565
|
+
});
|
1566
|
+
return Math.min(totalScore / maxPossibleScore, 1);
|
1567
|
+
}
|
1568
|
+
function highlightMatch(text, query) {
|
1569
|
+
if (!text)
|
1570
|
+
return text;
|
1571
|
+
const queryTerms = query.toLowerCase().split(' ');
|
1572
|
+
let highlighted = text;
|
1573
|
+
queryTerms.forEach(term => {
|
1574
|
+
const regex = new RegExp(`(${term})`, 'gi');
|
1575
|
+
highlighted = highlighted.replace(regex, chalk.bgYellow.black('$1'));
|
1576
|
+
});
|
1577
|
+
return highlighted;
|
1578
|
+
}
|
382
1579
|
program.parse(process.argv);
|