"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerCatalogSearchSelectionPrompt = buildWorkerCatalogSearchSelectionPrompt;
exports.parseWorkerCatalogSearchSelection = parseWorkerCatalogSearchSelection;
const zod_1 = require("zod");
const decision_output_1 = require("./decision-output");
const dedicated_prompt_support_1 = require("./dedicated-prompt-support");
const structured_contract_support_1 = require("./structured-contract-support");
const catalogSearchSelectionSchema = zod_1.z
    .object({
    selected: zod_1.z
        .array(zod_1.z
        .object({
        id: zod_1.z.string().trim().min(1),
        reason: zod_1.z.string().trim().min(1),
        confidence: zod_1.z.number().min(0).max(1),
    })
        .strict())
        .max(10),
})
    .strict();
function buildWorkerCatalogSearchSelectionPrompt(workerName, input) {
    const contract = (0, structured_contract_support_1.buildWorkerStructuredContract)({
        schema: catalogSearchSelectionSchema,
        example: {
            selected: [
                {
                    id: input.kind === "skill" ? "openai-docs" : "github",
                    reason: "Best semantic fit for the requested capability.",
                    confidence: 0.92,
                },
            ],
        },
        rules: [
            "Return compact JSON only inside the required decision markers.",
            "Select only ids that appear in the read-only command outputs.",
            "Prefer semantic fit first; when candidates are comparable, use quality signals such as stars, updatedAt, and createdAt.",
            "Use confidence as a number from 0 to 1.",
        ],
    });
    return (0, dedicated_prompt_support_1.renderDedicatedPromptEnvelope)({
        preambleLines: (0, dedicated_prompt_support_1.renderDedicatedPromptHeader)({
            workerName,
            runDescription: "This is a dedicated AlpacaLoop catalog search selection run.",
            instructionLines: [
                "Use only read-only commands to inspect catalog topics and entries.",
                "Do not edit files, install anything, or call search with --with-worker.",
                "Return the selected entry ids for the user's request.",
            ],
            ...(input.systemPrompts === undefined
                ? {}
                : { systemPrompts: input.systemPrompts }),
        }),
        inputLines: [
            ...(0, dedicated_prompt_support_1.renderDedicatedPromptInputLines)({
                heading: "Catalog search selection input:",
                prompt: input.prompt,
                workspace: input,
            }),
            `- Catalog kind: ${input.kind}`,
            `- Search mode: ${input.mode}`,
            `- Target: ${input.target}`,
            "- You may use:",
            "  - alpaca catalog topics list",
            `  - alpaca ${input.kind} catalog list --include-system`,
            `  - alpaca ${input.kind} search <query>`,
            ...(input.mode === "installable"
                ? []
                : [`  - alpaca ${input.kind} list`]),
            "- Do not call search with --with-worker.",
            "- Run multiple search queries if useful.",
        ],
        contract: {
            heading: "Catalog search selection contract",
            rules: contract.rules,
            promptSchema: contract.promptSchema,
            example: contract.example,
        },
    });
}
function parseWorkerCatalogSearchSelection(rawOutput, _locale) {
    const rawJson = (0, decision_output_1.extractPreferredWorkerDecisionBlock)(rawOutput) ?? rawOutput;
    const parsed = JSON.parse(rawJson);
    return catalogSearchSelectionSchema.parse(parsed);
}
