import { queryAi } from '@utils/aiGatewayClient'; import { ShortcutsResponse } from '../types'; import { dbOperations } from '@utils/db'; export async function llamaCommand( parameters: Record | undefined ): Promise { const commandId = Math.random().toString(36).substring(7); const startTime = Date.now(); let question = ''; let response = ''; let success = false; let errorMessage: string | undefined; let tokensUsed: number | undefined; console.info(`[CMD-${commandId}] Llama command started`, { hasParameters: !!parameters, timestamp: new Date().toISOString() }); try { if (!parameters || !parameters['question']) { console.warn(`[CMD-${commandId}] Missing question parameter`); errorMessage = 'Need to provide a question.'; return { success: false, message: 'Sorry. Need to provide a question.' }; } question = parameters['question']; console.info(`[CMD-${commandId}] Processing question`, { questionLength: question.length, question: question.substring(0, 100) + (question.length > 100 ? '...' : '') }); const prompt = 'I want to know ' + question + '. Structure the response in a manner suitable for spoken communication.'; const aiResponse = await queryAi(prompt); response = aiResponse.text; tokensUsed = aiResponse.tokensUsed; success = true; const duration = Date.now() - startTime; console.info( `[CMD-${commandId}] Llama command completed in ${duration}ms`, { responseLength: response.length, tokensUsed, success: true } ); return { success: true, message: response, data: { tokensUsed } }; } catch (error) { const duration = Date.now() - startTime; console.error( `[CMD-${commandId}] Llama command failed after ${duration}ms:`, error ); success = false; errorMessage = error instanceof Error ? error.message : 'Unknown error'; response = 'Sorry. There was a problem with the AI service.'; return { success: false, message: response }; } finally { if (question) { try { console.info(`[CMD-${commandId}] Saving query to database`); await dbOperations.saveQuery({ question, response, success, errorMessage, tokensUsed }); console.info(`[CMD-${commandId}] Query saved to database successfully`); } catch (error) { console.error( `[CMD-${commandId}] Failed to log query to database:`, error ); } } } }