// src/services/jiraSummarizer.js import { callGrok } from '../utils/grokClient.js'; import { adfToPlainText } from '../utils/adfToPlainText.js'; import { logger } from '../utils/logger.js'; const GROK_DELAY_MS = 800; export async function summarizeJiraTicket(ticket) { const fields = ticket.fields || {}; const key = ticket.key; const summary = fields.summary || ''; const description = fields.description ? adfToPlainText(fields.description) : ''; const comments = fields.comment?.comments || []; let commentText = ''; if (comments.length > 0) { commentText = comments.map(c => { const date = new Date(c.created).toLocaleDateString('en-US'); const author = c.author?.displayName || 'Unknown'; let body = typeof c.body === 'string' ? c.body : adfToPlainText(c.body); return `[${date}] ${author}:\n${body}`; }).join('\n\n'); } else { commentText = 'No comments found in the ticket.'; } const prompt = ` You are an expert IT support analyst summarizing Jira tickets. **Ticket:** ${key} **Summary:** ${summary} **Description:** ${description} **Comments/Notes (chronological):** ${commentText} Analyze the ticket and respond with **exactly** this structure. Do not add any extra text, headings, or explanations outside these sections: **Reported Problem:** Describe the original issue clearly and concisely. **Steps Taken:** List the key troubleshooting and resolution steps taken (pull heavily from comments). **Final Resolution:** State how the issue was ultimately resolved or closed. If still open, note the current status and any pending actions. Keep each section to 2-4 sentences maximum. Be factual and technical. `; try { await new Promise(resolve => setTimeout(resolve, GROK_DELAY_MS)); const aiResponse = await callGrok(prompt, { temperature: 0.35, max_tokens: 500 }); return aiResponse.trim(); } catch (err) { logger('jira:summarizer', `Failed for ${key}: ${err.message}`, 'error'); return `**Reported Problem:** No details available.\n**Steps Taken:** No information recorded.\n**Final Resolution:** No resolution documented.`; } } export async function analyzeCommonIssues(tickets) { if (tickets.length < 3) return ''; const ticketData = tickets.map(t => ({ key: t.key, summary: t.fields.summary || '', component: t.fields.components?.[0]?.name || '', status: t.fields.status?.name || '' })); const prompt = ` You are analyzing multiple Jira tickets for the same store. Tickets: ${JSON.stringify(ticketData, null, 2)} Identify 2–4 common patterns or recurring issues across these tickets. Focus on technical root causes or trends (e.g., "Multiple wireless phone audio issues after firmware updates", "Frequent voicemail greeting resets", etc.). Respond with a short bullet-point list. Be concise and specific. `; try { await new Promise(resolve => setTimeout(resolve, 1200)); // extra delay for analysis const analysis = await callGrok(prompt, { temperature: 0.5, max_tokens: 350 }); return analysis; } catch (err) { logger('jira:summarizer', `Analysis failed: ${err.message}`, 'error'); return "Unable to identify common patterns at this time."; } }