import type { TaskNotificationBlock } from "../types/messaging.js"; export function taskNotificationToXml(block: TaskNotificationBlock): string { let xml = `\n`; xml += `${block.taskId}\n`; xml += `${block.taskType}\n`; if (block.outputFile) { xml += `${block.outputFile}\n`; } xml += `${block.status}\n`; xml += `${block.summary}\n`; xml += ``; return xml; } function extractTag(xml: string, tag: string): string | null { const regex = new RegExp(`<${tag}>(.*?)`, "s"); const match = xml.match(regex); return match ? match[1] : null; } export function parseTaskNotificationXml( xml: string, ): TaskNotificationBlock | null { try { const taskId = extractTag(xml, "task-id"); const taskType = extractTag(xml, "task-type") as | "shell" | "agent" | "workflow" | null; const status = extractTag(xml, "status") as | "completed" | "failed" | "killed" | "aborted" | null; const summary = extractTag(xml, "summary"); if (!taskId || !taskType || !status || !summary) { return null; } const outputFile = extractTag(xml, "output-file") || undefined; return { type: "task_notification", taskId, taskType, status, summary, ...(outputFile && { outputFile }), }; } catch { return null; } }