/** * Pure helper functions for devil's advocate debate. * Extracted for testability — no external deps. */ import type { DebateOutput } from "./types"; // Magic numbers export const ROUND_HISTORY_TRUNCATE = 500; export const CONCESSION_INDICATOR_WEIGHT = 0.5; export const CONSENSUS_ROUNDS_REQUIRED = 2; /** Extract text from LLM response content */ export function textFromContent( content: Array<{ type: string; text?: string }>, ): string { return content .filter((c): c is { type: "text"; text: string } => c.type === "text") .map((c) => c.text) .join("\n"); } /** Create standard cancelled result */ export function abortedResult( roundsData: string[], blockingIssuesFound: number, ): DebateOutput { return { synthesis: "Debate cancelled by user.", roundsData, recommendation: "proceed_with_caution", blockingRaised: blockingIssuesFound, blockingRemaining: blockingIssuesFound, aborted: true, }; } /** Check if signal is aborted and return early with cancelled result */ export function checkAborted( signal: AbortSignal | undefined, roundsData: string[], blockingIssuesFound: number, ): DebateOutput | null { if (signal?.aborted) return abortedResult(roundsData, blockingIssuesFound); return null; } /** Count blocking issues in devil output */ export function countBlockingIssues(text: string): number { return (text.match(/^###\s+(?:\[?blocking\]?|\*\*blocking\*\*)\b/gim) || []) .length; } /** Calculate concession score from proposer response */ export function calcConcessionScore(text: string): number { const resolvedCount = ( text.match(/\*\*status\*\*:\s*resolved|status:\s*resolved/gi) || [] ).length; const softIndicators = ( text.match( /fair point|good point|you're right|concede|acknowledge|valid point|no new blocking/i, ) || [] ).length; return resolvedCount + softIndicators * CONCESSION_INDICATOR_WEIGHT; } /** Build context text from key-value pairs */ export function buildContextText(context?: Record): string { if (!context) return "No additional context provided."; return Object.entries(context) .map(([k, v]) => `- **${k}**: ${v}`) .join("\n"); } /** Build the prompt text for a debate round */ export function buildRoundPrompt( round: number, maxRounds: number, idea: string, contextText: string, roundHistory: string, ): string { const history = roundHistory ? `Previous rounds:\n${roundHistory}` : "This is the first round."; return [ `Context:\n${contextText}`, `Idea: ${idea}`, history, `Round ${round}/${maxRounds}. Identify the most blocking concern not yet resolved.`, ].join("\n\n"); } /** Build the proposer prompt text */ export function buildProposerPrompt( idea: string, roundHistory: string, round: number, devilOutput: string, ): string { const history = roundHistory ? `Previous rounds:\n${roundHistory}` : "This is the first round."; return [ `Your idea: ${idea}`, history, `Current challenge (Round ${round}):`, devilOutput, "Respond to this challenge.", ].join("\n\n"); } /** Clean transcript text for display */ export function cleanTranscriptText(text: string, maxChars: number): string { const clean = text .replace(/\r/g, "") .replace(/\n{3,}/g, "\n\n") .trim(); if (clean.length <= maxChars) return clean; return `${clean.slice(0, maxChars).trimEnd()}…`; } /** Extract first meaningful line from devil's challenge for widget display */ export function extractSnippet(text: string, maxLen = 60): string { const lines = text.split("\n").filter((l) => l.trim().length > 0); for (const line of lines) { const clean = line .replace(/^#{1,6}\s*/, "") .replace(/\*\*/g, "") .trim(); if ( clean.length > 5 && !clean.startsWith("Concern:") && !clean.startsWith("Fix:") && !clean.startsWith("Status:") ) { return clean.length > maxLen ? clean.slice(0, maxLen) + "…" : clean; } } return lines[0]?.slice(0, maxLen) ?? ""; }