-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.ts
95 lines (84 loc) · 3.49 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
import { Command } from 'commander'
import chalk from 'chalk'
import dotenv from 'dotenv'
import { analyzeQuestions, setEnvironmentVariables } from './index.js'
import { startServer } from './server.js'
import { db } from './db.js'
// Initialize commander
const program = new Command()
.name('question-analyzer')
.description('Analyze questions from Voiceflow transcripts')
.option('-r, --range <range>', 'time range (today, yesterday, last7, last30, alltime)', 'last7')
.option('-t, --top <number>', 'number of top questions to show', '10')
.option('-s, --server', 'run in server mode')
.option('-p, --port <number>', 'port number for server mode', '3000')
.option('-c, --check <reportId>', 'check status of a specific report')
.parse(process.argv)
// Load env and override with CLI options
dotenv.config()
const options = program.opts()
async function displayReport(result: any) {
console.log(chalk.blue('\n📊 Top Questions:'))
result.questions.forEach((q: any, i: number) => {
console.log(chalk.white(`\n${i + 1}. "${q.question}"`))
console.log(chalk.gray(` Asked ${q.count} times`))
})
// Display token usage and cost information
console.log(chalk.blue('\n📈 Token Usage:'))
console.log(chalk.gray(` Prompt tokens: ${result.usage.prompt_tokens.toLocaleString()}`))
console.log(chalk.gray(` Completion tokens: ${result.usage.completion_tokens.toLocaleString()}`))
console.log(chalk.gray(` Total tokens: ${result.usage.total_tokens.toLocaleString()}`))
console.log(chalk.blue('\n💰 Cost:'))
console.log(chalk.gray(` Estimated cost: $${result.usage.estimated_cost_usd.toFixed(4)}`))
console.log('\n\n')
}
async function checkReport(reportId: string) {
const report = await db.getReport(reportId)
if (!report) {
console.error(chalk.red('\n❌ Error: Report not found'))
process.exit(1)
}
console.log(chalk.blue('\n📋 Report Status:'))
console.log(chalk.gray(` ID: ${report.id}`))
console.log(chalk.gray(` Status: ${report.status}`))
console.log(chalk.gray(` Time Range: ${report.timeRange}`))
console.log(chalk.gray(` Created: ${new Date(report.createdAt).toLocaleString()}`))
console.log(chalk.gray(` Updated: ${new Date(report.updatedAt).toLocaleString()}`))
if (report.status === 'completed' && report.result) {
await displayReport(report.result)
} else if (report.status === 'failed' && report.error) {
console.error(chalk.red('\n❌ Error:'), report.error)
}
}
async function runAnalysis() {
setEnvironmentVariables({
TIME_RANGE: options.range,
TOP_QUESTIONS: options.top.toString()
})
const result = await analyzeQuestions()
await displayReport(result)
process.exit(0)
}
async function main() {
try {
if (options.check) {
await checkReport(options.check)
} else if (options.server) {
console.log(chalk.blue('\n🚀 Starting server mode...'))
await startServer(parseInt(options.port))
console.log(chalk.gray('\nAvailable endpoints:'))
console.log(chalk.gray(' GET /health - Health check'))
console.log(chalk.gray(' POST /api/analyze - Start analysis'))
console.log(chalk.gray(' GET /api/reports/:reportId - Get report status and results'))
console.log(chalk.gray('\nExample:'))
console.log(chalk.gray(` curl -X POST "http://localhost:${options.port}/api/analyze?range=last7&top=5"`))
} else {
await runAnalysis()
}
} catch (error) {
console.error(chalk.red('\n❌ Error:'), error)
process.exit(1)
}
}
main()