UNPKG

aura-ai

Version:

AI-powered marketing strategist CLI tool for developers

118 lines (98 loc) • 2.8 kB
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) }) }