import { z } from "zod";
import { DEFAULT_STRUCTURED_GENERATION_PROVIDERS, StructuredAgentFallbackError, StructuredAgentResponseError, generateStructuredAgentResponseWithFallback, } from "./agent-response-loop.js";
import { MAX_AUTO_AGENT_TITLE_CHARS } from "./agent-title-limits.js";
import { buildMetadataPrompt } from "../../utils/build-metadata-prompt.js";
function hasExplicitTitle(title) {
    return Boolean(title && title.trim().length > 0);
}
function normalizeAutoTitle(title) {
    const normalized = title.trim();
    if (!normalized) {
        return null;
    }
    return normalized.slice(0, MAX_AUTO_AGENT_TITLE_CHARS).trim() || null;
}
export async function determineAgentMetadataNeeds(options) {
    const prompt = options.initialPrompt?.trim();
    if (!prompt) {
        return { prompt: null, needsTitle: false };
    }
    const needsTitle = !hasExplicitTitle(options.explicitTitle);
    return {
        prompt,
        needsTitle,
    };
}
function buildMetadataSchema(needs) {
    if (!needs.needsTitle) {
        return null;
    }
    const shape = {};
    if (needs.needsTitle) {
        shape.title = z.string().min(1).max(MAX_AUTO_AGENT_TITLE_CHARS);
    }
    return z.object(shape);
}
async function buildPrompt(needs, options) {
    const beforeLines = ["Generate metadata for a coding agent based on the user prompt."];
    if (needs.needsTitle) {
        beforeLines.push(`Title: short descriptive label (<= ${MAX_AUTO_AGENT_TITLE_CHARS} chars).`);
    }
    return buildMetadataPrompt({
        cwd: options.cwd,
        workspaceGitService: options.workspaceGitService,
        configKey: "agentTitle",
        before: beforeLines.join("\n"),
        after: "Return JSON only with a single field 'title'.",
        trailing: `User prompt:\n${needs.prompt ?? ""}`,
    });
}
export async function generateAndApplyAgentMetadata(options) {
    const needs = await determineAgentMetadataNeeds(options);
    if (!needs.prompt) {
        return;
    }
    const schema = buildMetadataSchema(needs);
    if (!schema) {
        return;
    }
    const generator = options.deps?.generateStructuredAgentResponseWithFallback ??
        generateStructuredAgentResponseWithFallback;
    let result;
    try {
        result = await generator({
            manager: options.agentManager,
            cwd: options.cwd,
            prompt: await buildPrompt(needs, {
                cwd: options.cwd,
                workspaceGitService: options.workspaceGitService,
            }),
            schema,
            schemaName: "AgentMetadata",
            maxRetries: 2,
            providers: DEFAULT_STRUCTURED_GENERATION_PROVIDERS,
            persistSession: false,
            agentConfigOverrides: {
                title: "Agent metadata generator",
                internal: true,
            },
        });
    }
    catch (error) {
        if (error instanceof StructuredAgentResponseError ||
            error instanceof StructuredAgentFallbackError) {
            options.logger.warn({ err: error, agentId: options.agentId }, "Structured metadata generation failed");
            return;
        }
        options.logger.error({ err: error, agentId: options.agentId }, "Agent metadata generation failed");
        return;
    }
    if (needs.needsTitle && typeof result.title === "string") {
        const normalizedTitle = normalizeAutoTitle(result.title);
        if (normalizedTitle) {
            await options.agentManager.setTitle(options.agentId, normalizedTitle);
        }
    }
}
export function scheduleAgentMetadataGeneration(options) {
    queueMicrotask(() => {
        void generateAndApplyAgentMetadata(options).catch((error) => {
            options.logger.error({ err: error, agentId: options.agentId }, "Agent metadata generation crashed");
        });
    });
}
//# sourceMappingURL=agent-metadata-generator.js.map