import { renderThreadHandoffText, } from "@best-agent/protocol";
export function resolveWorkflowJoinContext(record, options = {}) {
    if (typeof record.payload !== "object" || record.payload === null) {
        return undefined;
    }
    const payload = record.payload;
    const phaseName = payload.phaseName;
    const workflowName = payload.workflowName;
    const childEvidence = payload.childEvidence;
    const output = typeof payload.output === "string" && payload.output.trim().length > 0 ? payload.output : undefined;
    const handoff = typeof payload.handoff === "object" && payload.handoff !== null
        ? renderThreadHandoffText(payload.handoff)
        : undefined;
    if ((phaseName !== "explore" && phaseName !== "plan" && phaseName !== "build" && phaseName !== "verify")
        || (workflowName !== "explore-build"
            && workflowName !== "build-verify"
            && workflowName !== "plan-build-verify"
            && workflowName !== "plan-explore-build-verify")) {
        return undefined;
    }
    if (Array.isArray(childEvidence) && childEvidence.length > 0) {
        return {
            phaseName,
            text: record.summary,
            type: "evidence",
            workflowName,
        };
    }
    if (output) {
        return {
            phaseName,
            text: output,
            type: "output",
            workflowName,
        };
    }
    if (handoff) {
        return {
            phaseName,
            text: handoff,
            type: "handoff",
            workflowName,
        };
    }
    if (options.fallbackToSummary && record.summary.trim().length > 0) {
        return {
            phaseName,
            text: record.summary,
            type: "output",
            workflowName,
        };
    }
    return undefined;
}
export function buildWorkflowJoinedPhaseContexts(joinedEvidence, workflowName) {
    return collectWorkflowJoinedPhaseContexts(joinedEvidence, { workflowName }).joinedContexts;
}
export function buildWorkflowPhaseOutputTexts(joinedContexts) {
    return new Map(Array.from(joinedContexts.entries()).map(([phaseName, joinedContext]) => [phaseName, joinedContext.text]));
}
export function renderWorkflowJoinedPhaseMessage(phaseName, joinedContext) {
    if (joinedContext.type === "evidence") {
        return `Workflow phase ${phaseName} joined evidence:\n${joinedContext.text}`;
    }
    if (joinedContext.type === "handoff") {
        return `Workflow phase ${phaseName} joined handoff:\n${joinedContext.text}`;
    }
    return `Workflow phase ${phaseName} output:\n${joinedContext.text}`;
}
export function extractWorkflowPhaseResultsFromEvidence(joinedEvidence, options = {}) {
    const { joinedContexts, workflowName } = collectWorkflowJoinedPhaseContexts(joinedEvidence, options);
    const phaseResults = {};
    for (const [phaseName, joinedContext] of joinedContexts.entries()) {
        phaseResults[phaseName] = joinedContext.text;
    }
    return { phaseResults, workflowName };
}
export function mergeWorkflowPhaseResultsFromReasoningItems(items, phaseResults, options = {}) {
    const mergedPhaseResults = { ...phaseResults };
    for (const item of items) {
        if (item.kind !== "reasoning") {
            continue;
        }
        if (options.latestWorkflowTurnId && item.turnId !== options.latestWorkflowTurnId) {
            continue;
        }
        const match = item.summary.match(/^Workflow phase (explore|plan|build|verify) result: ([\s\S]+)$/);
        if (!match) {
            continue;
        }
        const phase = match[1];
        mergedPhaseResults[phase] ??= match[2];
    }
    return mergedPhaseResults;
}
function collectWorkflowJoinedPhaseContexts(joinedEvidence, options = {}) {
    const joinedContexts = new Map();
    let workflowNameFromEvidence;
    for (const record of [...joinedEvidence].reverse()) {
        const joinedContext = resolveWorkflowJoinContext(record, { fallbackToSummary: options.fallbackToSummary });
        if (!joinedContext) {
            continue;
        }
        if (options.latestWorkflowTurnId && record.turnId !== options.latestWorkflowTurnId) {
            continue;
        }
        if (options.workflowName && joinedContext.workflowName !== options.workflowName) {
            continue;
        }
        if (options.allowedPhaseNames && !options.allowedPhaseNames.has(joinedContext.phaseName)) {
            continue;
        }
        if (joinedContexts.has(joinedContext.phaseName)) {
            continue;
        }
        joinedContexts.set(joinedContext.phaseName, {
            text: joinedContext.text,
            type: joinedContext.type,
        });
        workflowNameFromEvidence = joinedContext.workflowName;
    }
    return {
        joinedContexts,
        workflowName: workflowNameFromEvidence,
    };
}
//# sourceMappingURL=workflow-join.js.map