// Checklist service for US1 (T035)
// Extracts diagnostic, troubleshooting, and mitigation steps from chunks
import { ValidationError } from '../utils/errors.mjs';
const CHECKLIST_PATTERNS = [
/troubleshoot/i,
/diagnosis/i,
/mitigation/i,
/step/i,
/procedure/i,
/initial/i,
/first.{0,10}check/i,
/verify/i
];
export function createChecklistService({ logger }) {
logger = logger || { log: () => {} };
function extractChecklist(chunks) {
if (!Array.isArray(chunks)) {
throw new ValidationError('chunks array required');
}
const checklist = [];
for (const chunk of chunks) {
const isChecklistRelevant = CHECKLIST_PATTERNS.some(pattern =>
pattern.test(chunk.heading || '') || pattern.test(chunk.text || '')
);
if (isChecklistRelevant) {
// Extract numbered steps or bullet points
const steps = extractSteps(chunk.text);
checklist.push({
section: chunk.heading || 'General',
docId: chunk.docId,
chunkId: chunk.id,
steps: steps.length > 0 ? steps : [chunk.text.slice(0, 200) + '...']
});
}
}
logger.log('checklist.extracted', { sectionCount: checklist.length });
return checklist;
}
function extractSteps(text) {
if (!text) return [];
// Match numbered lists (1. 2. etc) or bullet points
const numberedSteps = text.match(/^\s*\d+\.\s+.+$/gm) || [];
const bulletSteps = text.match(/^\s*[-*]\s+.+$/gm) || [];
const steps = [...numberedSteps, ...bulletSteps]
.map(step => step.trim())
.filter(Boolean);
return steps.length > 0 ? steps : [];
}
return Object.freeze({ extractChecklist });
}
export default { createChecklistService };