/**
 * workflow-template-compiler.ts — Compile `/start` templates into run definitions.
 *
 * Purpose: let prompt-oriented workflow templates enter the same graph-backed
 * runtime as YAML workflow definitions while preserving the ergonomic `/start`
 * authoring surface.
 */
/**
 * Generate a step ID from a phase name and index.
 * Lowercases, slugifies, and limits to 40 characters.
 */
function stepIdForPhase(phase, index) {
	const slug = phase
		.toLowerCase()
		.replace(/[^a-z0-9]+/g, "-")
		.replace(/^-|-$/g, "")
		.slice(0, 40)
		.replace(/-$/, "");
	return slug || `phase-${index + 1}`;
}
/**
 * Build the prompt text for executing a single phase of a template.
 */
function phasePrompt(input, phase) {
	const guided = input.mode === "guided";
	return [
		`Run the "${phase}" phase of the ${input.template.name} workflow.`,
		"",
		`Template id: ${input.templateId}`,
		`Description: ${input.description || "(none provided)"}`,
		`Issue: ${input.issueRef || "(none)"}`,
		`Artifact directory: ${input.artifactDir || "(none)"}`,
		`Branch: ${input.branch}`,
		`Date: ${input.date}`,
		"",
		"Use the workflow template below as the source of truth. Execute only the current phase, produce the phase's required evidence, and leave enough notes for the next graph step to continue without re-deciding prior scope.",
		guided
			? "This is a guided /start run: ask concise clarifying questions when product intent, scope, acceptance criteria, or readiness is unclear, and pause rather than guessing through important product decisions."
			: "",
		"",
		`Current phase: ${phase}`,
		"",
		"--- Workflow Template ---",
		input.workflowContent,
	].join("\n");
}
/**
 * Check if a phase should have a guided review gate based on template config.
 */
function hasGuidedReviewGate(input, phase) {
	if (input.mode !== "guided") return false;
	const configured = input.template.interaction?.question_gates;
	if (configured) return configured.includes(phase);
	return (
		input.templateId === "product-plan" && ["scope", "ready"].includes(phase)
	);
}
/**
 * Compile a resolved workflow template into a V1 workflow definition.
 *
 * Purpose: bridge `/start` templates into the canonical custom workflow
 * graph runtime without requiring users to author YAML definitions.
 *
 * Consumer: `handleStart` before creating a template-backed workflow run.
 */
/**
 * Compile a workflow template into a WorkflowDefinition.
 * Bridges /start templates into the custom workflow graph runtime.
 */
export function compileTemplateRun(input) {
	return {
		version: 1,
		name: input.templateId,
		description: input.template.description,
		steps: input.template.phases.map((phase, index) => {
			const id = stepIdForPhase(phase, index);
			const previousPhase = input.template.phases[index - 1];
			return {
				id,
				name: phase,
				prompt: phasePrompt(input, phase),
				requires:
					index > 0 && previousPhase != null
						? [stepIdForPhase(previousPhase, index - 1)]
						: [],
				produces: [],
				verify: hasGuidedReviewGate(input, phase)
					? { policy: "human-review" }
					: undefined,
			};
		}),
	};
}
