aura-ai
Version:
AI-powered marketing strategist CLI tool for developers
118 lines (98 loc) ⢠2.8 kB
JavaScript
import chatService from '../services/chatService.js'
import { readFileSync } from 'fs'
export async function chatCommand(question, options) {
const { context, system, maxTokens, stdin, json, quiet } = options
try {
// Get question from different sources
let userQuestion = question
if (stdin) {
// Read from stdin
userQuestion = await readStdin()
if (!userQuestion) {
throw new Error('No input received from stdin')
}
} else if (!userQuestion) {
throw new Error('Question required. Use --stdin to read from input.')
}
// Load additional context if provided
let additionalContext = ''
if (context) {
additionalContext = readFileSync(context, 'utf-8')
}
// Prepare messages
const messages = []
if (additionalContext) {
messages.push({
role: 'system',
content: `Additional context:\n${additionalContext}`
})
}
messages.push({
role: 'user',
content: userQuestion
})
if (!quiet && !json) {
console.log('š¤ Thinking...')
}
// Get AI response
const stream = await chatService.sendMessage(messages)
let fullResponse = ''
if (json) {
// For agents: print readable content
console.log('\n' + '='.repeat(60))
console.log('š¬ AURA MARKETING CONSULTATION')
console.log('='.repeat(60))
console.log('\nš Question:', userQuestion)
console.log('\nš Answer:\n')
// Stream and collect response
for await (const chunk of stream.textStream) {
process.stdout.write(chunk)
fullResponse += chunk
}
console.log('\n\n' + '='.repeat(60))
console.log('ā
Consultation complete')
console.log('='.repeat(60))
} else {
// Stream response to console
if (!quiet) {
console.log('\nš Aura:')
}
for await (const chunk of stream.textStream) {
process.stdout.write(chunk)
fullResponse += chunk
}
console.log('\n')
}
} catch (error) {
if (json) {
console.log(JSON.stringify({
status: 'error',
error: error.message
}))
process.exit(1)
} else {
console.error(`ā Error: ${error.message}`)
process.exit(1)
}
}
}
function readStdin() {
return new Promise((resolve, reject) => {
let data = ''
process.stdin.setEncoding('utf8')
process.stdin.on('data', chunk => {
data += chunk
})
process.stdin.on('end', () => {
resolve(data.trim())
})
process.stdin.on('error', reject)
// Set timeout for stdin
setTimeout(() => {
if (!data) {
process.stdin.destroy()
resolve('')
}
}, 100)
})
}