"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WORKFLOW_TEMPLATE_WORKER_COMPATIBLE_SKILLS_RULE = exports.WORKFLOW_TEMPLATE_CAPABILITY_SELECTION_RULE = exports.WORKFLOW_TEMPLATE_AVAILABLE_MCP_REFS_RULE = exports.WORKFLOW_TEMPLATE_AVAILABLE_SKILL_REFS_RULE = exports.WORKFLOW_TEMPLATE_APP_ONLY_SKILL_REQUIRED_SKILLS_RULE = void 0;
exports.renderWorkflowTemplateSkillAndSelectionInstructions = renderWorkflowTemplateSkillAndSelectionInstructions;
exports.renderWorkflowTemplatePromptPreamble = renderWorkflowTemplatePromptPreamble;
exports.renderWorkflowTemplateExistingTemplatePaths = renderWorkflowTemplateExistingTemplatePaths;
exports.renderWorkflowTemplateRequiredSkills = renderWorkflowTemplateRequiredSkills;
exports.renderWorkflowTemplateRequiredMcp = renderWorkflowTemplateRequiredMcp;
exports.renderWorkflowTemplateCatalogEntries = renderWorkflowTemplateCatalogEntries;
exports.renderWorkflowTemplateReferenceContext = renderWorkflowTemplateReferenceContext;
const dedicated_prompt_support_1 = require("./dedicated-prompt-support");
const prompt_text_1 = require("./prompt-text");
const system_prompt_names_1 = require("./system-prompt-names");
const template_contract_support_1 = require("./template-contract-support");
exports.WORKFLOW_TEMPLATE_APP_ONLY_SKILL_REQUIRED_SKILLS_RULE = "Do not set requires.skills to skills whose description says they are limited to an app-only surface such as Codex App, Codex in-app browser, Claude Desktop, or desktop extensions; those may not be usable from CLI or background workers.";
exports.WORKFLOW_TEMPLATE_AVAILABLE_SKILL_REFS_RULE = "Only refs from `alpaca skill list` items with usableForRequiredSkills=true may be used in requires.skills. Search or catalog ids and install sources are not valid requires.skills values.";
exports.WORKFLOW_TEMPLATE_AVAILABLE_MCP_REFS_RULE = "Only refs from `alpaca mcp list` or `alpaca mcp verify` may be used in requires.mcp. Search or catalog ids, install results, and install sources are not valid requires.mcp values.";
exports.WORKFLOW_TEMPLATE_CAPABILITY_SELECTION_RULE = "For capability selection, use `alpaca capability list/search` as the cross-type discovery view. Choose requirements by purpose fit, target narrowness, source trust, and quality signals; when a skill and MCP satisfy the same need equally well, prefer the usable skill.";
exports.WORKFLOW_TEMPLATE_WORKER_COMPATIBLE_SKILLS_RULE = "Prefer bare refs when that leaves a compatible configured worker. Use provider-qualified refs only when the workflow truly requires that worker's skill, and ensure one configured worker can satisfy all requires.skills and requires.mcp entries together.";
function renderWorkflowTemplateSkillAndSelectionInstructions(input) {
    return [
        "Before choosing non-empty requires.skills or requires.mcp, decide whether the reusable workflow's primary intent, deliverable quality bar, or evaluation evidence truly needs a specialized capability.",
        exports.WORKFLOW_TEMPLATE_CAPABILITY_SELECTION_RULE,
        `Before setting requires.skills, run \`alpaca capability list\` and, when a query is needed, \`alpaca capability search "<capability>"\` for discovery, then run \`alpaca skill list\` to inspect usable skill refs. Use [] when ${input.noSpecificSkillsInstruction}.`,
        "Before setting requires.mcp, run `alpaca capability list/search` for cross-type discovery, then run `alpaca mcp verify` or `alpaca mcp list` to inspect configured MCP refs. Use [] when no MCP server is required.",
        exports.WORKFLOW_TEMPLATE_AVAILABLE_SKILL_REFS_RULE,
        exports.WORKFLOW_TEMPLATE_AVAILABLE_MCP_REFS_RULE,
        exports.WORKFLOW_TEMPLATE_WORKER_COMPATIBLE_SKILLS_RULE,
        "Do not invent skill or MCP refs. Do not treat requires.skills as skill install or skill creation work, and do not treat requires.mcp as MCP install or server creation work.",
        exports.WORKFLOW_TEMPLATE_APP_ONLY_SKILL_REQUIRED_SKILLS_RULE,
        "When a reusable workflow appears to need specialized capability, inspect available or installable candidates before deciding the template requirements. Use `alpaca capability list/search` for cross-type discovery; use `alpaca skill search` or `alpaca mcp search` only when working inside the type-specific resolution path.",
        "Follow the workflow-common and workflow-create system prompts for repository-specific workflow template authoring guidance.",
        input.targetSelectionInstruction,
    ];
}
function renderWorkflowTemplatePromptPreamble(input) {
    return (0, dedicated_prompt_support_1.renderDedicatedPromptHeader)({
        workerName: input.workerName,
        runDescription: input.runDescription,
        instructionLines: [
            prompt_text_1.PROMPT_TEXT.introInstructions,
            input.taskInstruction,
            "Use repository workflow-common and workflow-create system prompt guidance when present; it is repository-specific guidance and does not replace the template contract below.",
            (0, prompt_text_1.buildTemplateDraftLanguageInstruction)(input.outputLanguage),
            input.returnFieldsInstruction,
            ...input.operationInstructions,
            ...renderWorkflowTemplateShapeInstructions(),
            "Before returning JSON, self-review the template for reusable scope, clear boundaries, evidence quality, actionable final result, and absence of invented repository facts.",
        ],
        systemPrompts: input.systemPrompts,
        systemPromptNames: system_prompt_names_1.WORKFLOW_TEMPLATE_AUTHORING_SYSTEM_PROMPT_NAMES,
    });
}
function renderWorkflowTemplateExistingTemplatePaths(paths) {
    return paths.length > 0
        ? paths.map((templatePath) => `  - ${templatePath}`)
        : [`  ${prompt_text_1.PROMPT_TEXT.none}`];
}
function renderWorkflowTemplateRequiredSkills(requiredSkills) {
    return requiredSkills.length > 0
        ? requiredSkills.map((skillId) => `  - ${skillId}`)
        : [`  ${prompt_text_1.PROMPT_TEXT.none}`];
}
function renderWorkflowTemplateRequiredMcp(requiredMcp) {
    return requiredMcp.length > 0
        ? requiredMcp.map((mcpRef) => `  - ${mcpRef}`)
        : [`  ${prompt_text_1.PROMPT_TEXT.none}`];
}
function renderWorkflowTemplateCatalogEntries(entries) {
    if (entries === undefined || entries.length === 0) {
        return [`  ${prompt_text_1.PROMPT_TEXT.none}`];
    }
    return entries.flatMap((entry) => [
        [
            `  - ${entry.path}`,
            `targets=${formatWorkflowTemplateTargets(entry.targets)}`,
            `autoSelect=${formatWorkflowTemplateAutoSelect(entry.autoSelect)}`,
            `description=${entry.description ?? prompt_text_1.PROMPT_TEXT.none}`,
            `requires.skills=${entry.requiredSkills.length === 0
                ? prompt_text_1.PROMPT_TEXT.none
                : entry.requiredSkills.join(", ")}`,
            `requires.mcp=${entry.requiredMcp.length === 0
                ? prompt_text_1.PROMPT_TEXT.none
                : entry.requiredMcp.join(", ")}`,
        ].join(" | "),
        "    Body:",
        "```md",
        entry.body.trim() || prompt_text_1.PROMPT_TEXT.empty,
        "```",
    ]);
}
function renderWorkflowTemplateReferenceContext(entries) {
    if (entries === undefined || entries.length === 0) {
        return [`  ${prompt_text_1.PROMPT_TEXT.none}`];
    }
    return entries.map((entry) => [
        `  - ${entry.templatePath}:${entry.lineNumber}`,
        `targets=${formatWorkflowTemplateTargets(entry.targets)}`,
        `autoSelect=${formatWorkflowTemplateAutoSelect(entry.autoSelect)}`,
        `description=${entry.description ?? prompt_text_1.PROMPT_TEXT.none}`,
        `requires.skills=${entry.requiredSkills.length === 0
            ? prompt_text_1.PROMPT_TEXT.none
            : entry.requiredSkills.join(", ")}`,
        `requires.mcp=${entry.requiredMcp.length === 0
            ? prompt_text_1.PROMPT_TEXT.none
            : entry.requiredMcp.join(", ")}`,
        `call=${entry.sourceLine}`,
    ].join(" | "));
}
function formatWorkflowTemplateTargets(targets) {
    return targets.join(",");
}
function formatWorkflowTemplateAutoSelect(autoSelect) {
    return autoSelect ? "yes" : "no";
}
function renderWorkflowTemplateShapeInstructions() {
    return [
        "Workflow templates capture reusable work-judgment guidance, not issue-specific execution state. They define how a class of work should be performed; the issue defines what concrete target, deliverable, success condition, and constraints apply.",
        template_contract_support_1.WORKFLOW_TEMPLATE_SOURCE_REQUEST_RULE,
        "The requires.skills JSON field declares skills required to run the workflow. It is not an instruction to install or create skills.",
        "The requires.mcp JSON field declares MCP servers or tools required to run the workflow. It is not an instruction to install or configure MCP during ordinary workflow work.",
        "targets declares where the workflow may be selected: issue, followup, unplanned, or pull_request. `recurring` is issue schedule state, not a workflow target. Do not encode targets in the filename.",
        "autoSelect declares whether AlpacaLoop may choose this template automatically. Use false for reference-only templates.",
        "updateMode controls whether workflow CLI commands may implicitly update a template. Do not return updateMode in worker JSON; the CLI writes new templates with updateMode flexible and preserves explicitly existing updateMode during updates.",
    ];
}
