import { extractAssetContent } from "../run/flows/asset/extract.js";
import { executeMediaFile } from "../run/flows/asset/media.js";
import { executeAssetSummary } from "../run/flows/asset/summary.js";
function toAssetExecutionInput(input) {
    return {
        kind: "asset",
        sourceKind: input.sourceKind,
        source: input.sourceLabel,
        mediaType: input.attachment.mediaType,
        filename: input.attachment.filename,
    };
}
async function executeResolvedMedia(input, options) {
    if (!options.request.extractOnly) {
        options.emit({ type: "summary-started" });
    }
    const mediaResult = await executeMediaFile(options.context, {
        sourceKind: input.sourceKind,
        sourceLabel: input.sourceLabel,
        attachment: input.attachment,
        onModelChosen: (modelId) => options.emit({ type: "model-selected", modelId }),
    });
    if (mediaResult.kind === "summary" && !mediaResult.summary.summaryEmitted) {
        options.emitSummary(mediaResult.summary.summary);
    }
    const report = mediaResult.kind === "summary" ? await options.context.buildReport() : null;
    const result = {
        kind: "asset-media",
        input: toAssetExecutionInput(input),
        usedModel: options.getSelectedModel() ??
            (mediaResult.kind === "summary" ? (mediaResult.summary.llm?.model ?? null) : null),
        summaryFromCache: mediaResult.kind === "summary" ? mediaResult.summary.summaryFromCache : false,
        elapsedMs: options.elapsedMs(),
        report,
        costUsd: mediaResult.kind === "summary" ? await options.context.estimateCostUsd() : null,
        details: mediaResult,
    };
    options.emit({ type: "run-completed", result });
    return result;
}
async function executeResolvedAsset(input, options) {
    if (options.request.extractOnly) {
        const maxCharacters = options.request.input.kind === "url" || options.request.input.kind === "input-url"
            ? options.request.input.maxCharacters
            : null;
        const extractedAsset = await extractAssetContent({
            ctx: {
                env: options.context.env,
                envForRun: options.context.envForRun,
                execFileImpl: options.context.execFileImpl,
                timeoutMs: options.context.timeoutMs,
                preprocessMode: options.context.preprocessMode,
            },
            attachment: input.attachment,
            ...(typeof maxCharacters === "number" ? { maxCharacters } : {}),
        });
        const report = options.context.shouldComputeReport ? await options.context.buildReport() : null;
        const result = {
            kind: "asset-extraction",
            input: toAssetExecutionInput(input),
            extracted: extractedAsset,
            elapsedMs: options.elapsedMs(),
            report,
            costUsd: options.context.metricsEnabled && report ? await options.context.estimateCostUsd() : null,
        };
        options.emit({ type: "run-completed", result });
        return result;
    }
    options.emit({ type: "summary-started" });
    const assetResult = await executeAssetSummary(options.context, {
        sourceKind: input.sourceKind,
        sourceLabel: input.sourceLabel,
        attachment: input.attachment,
        onModelChosen: (modelId) => options.emit({ type: "model-selected", modelId }),
    });
    if (!assetResult.summaryEmitted) {
        options.emitSummary(assetResult.summary);
    }
    const result = {
        kind: "asset-summary",
        input: toAssetExecutionInput(input),
        summary: assetResult.summary,
        usedModel: options.getSelectedModel() ?? assetResult.llm?.model ?? null,
        summaryFromCache: assetResult.summaryFromCache,
        elapsedMs: options.elapsedMs(),
        report: await options.context.buildReport(),
        costUsd: await options.context.estimateCostUsd(),
        details: assetResult,
    };
    options.emit({ type: "run-completed", result });
    return result;
}
export function createAcquiredAssetExecutor(options) {
    const execute = async (input) => {
        const context = options.context;
        if (!context) {
            throw new Error(input.kind === "resolved-media"
                ? "Resolved media execution requires prepared asset resources"
                : "Resolved asset execution requires prepared asset resources");
        }
        const executionOptions = { ...options, context };
        return input.kind === "resolved-media"
            ? await executeResolvedMedia(input, executionOptions)
            : await executeResolvedAsset(input, executionOptions);
    };
    const emitProgress = (input) => {
        options.emit({
            type: "input-progress",
            phase: input.kind === "resolved-media"
                ? "transcribing"
                : options.request.extractOnly
                    ? "extracting"
                    : "summarizing",
            source: input.sourceLabel,
            filename: input.attachment.filename,
            mediaType: input.attachment.mediaType,
            sizeBytes: input.sizeBytes,
        });
    };
    return { execute, emitProgress };
}
//# sourceMappingURL=asset-execution.js.map