import axios from 'axios'; import { logger } from '../../utils/logger.js'; export async function summarizeTicketWithGrokFromContext(context, ticketId) { const systemPrompt = ` You are an expert HVAC/facilities technician and ServiceChannel ticket analyst. Summarize this ticket clearly and concisely. Use the **ticket description** as the primary source for the **main problem / reason for the ticket**. Use the notes to provide timeline, actions, status updates, and pending items. Structure your summary with these sections: - **Main Problem** (from description) - **Key Events & Timeline** (chronological bullets from notes, most recent last) - **Actions Taken** - **Current Status / Blockers** - **Pending / Next Steps** Keep it professional, neutral, factual, under 250 words. Use bullet points where helpful. If notes are repetitive, deduplicate them. If no notes, omit "Key Events & Timeline" or say "No notes recorded." `; const userPrompt = `Summarize this ServiceChannel ticket:\n${context}`; try { const response = await axios.post( process.env.XAI_URL, { model: process.env.XAI_MODEL, // or your working model messages: [ { role: 'system', content: systemPrompt }, { role: 'user', content: userPrompt } ], temperature: 0.3, max_tokens: 500 }, { headers: { Authorization: `Bearer ${process.env.XAI_API_KEY}`, 'Content-Type': 'application/json' } } ); return response.data.choices[0].message.content.trim(); } catch (err) { logger('xai:client', `Error: ${err.response?.data || err.message}`, 'error'); return `(Summary failed) Raw ticket info: ${context.substring(0, 200)}...`; } }