function toJudgmentMatchMethod(matchedRuleMethod) {
    if (!matchedRuleMethod) {
        return undefined;
    }
    if (matchedRuleMethod === 'structured_output') {
        return 'structured_output';
    }
    if (matchedRuleMethod === 'ai_judge' || matchedRuleMethod === 'ai_judge_fallback') {
        return 'ai_judge';
    }
    if (matchedRuleMethod === 'phase3_tag' || matchedRuleMethod === 'phase1_tag') {
        return 'tag_fallback';
    }
    return undefined;
}
function serializeWorkflowStack(stack) {
    if (!stack || stack.length === 0) {
        return {};
    }
    return {
        workflow: stack[stack.length - 1]?.workflow,
        stack: stack.map((entry) => ({
            workflow: entry.workflow,
            ...(entry.workflow_ref ? { workflow_ref: entry.workflow_ref } : {}),
            step: entry.step,
            kind: entry.kind,
        })),
    };
}
export function buildInteractiveRecords(meta, sanitizeText) {
    return [
        { type: 'interactive_start', timestamp: new Date().toISOString() },
        {
            type: 'interactive_end',
            confirmed: meta.confirmed,
            ...(meta.task ? { task: sanitizeText(meta.task) } : {}),
            timestamp: new Date().toISOString(),
        },
    ];
}
export function buildPhaseStartRecord(step, phase, phaseName, instruction, promptParts, workflowStack, phaseExecutionId, iteration, sanitizeText) {
    return {
        type: 'phase_start',
        step: step.name,
        phase,
        phaseName,
        phaseExecutionId,
        timestamp: new Date().toISOString(),
        ...serializeWorkflowStack(workflowStack),
        instruction: sanitizeText(instruction),
        systemPrompt: sanitizeText(promptParts.systemPrompt),
        userInstruction: sanitizeText(promptParts.userInstruction),
        ...(iteration != null ? { iteration } : {}),
    };
}
export function buildPhaseCompleteRecord(step, phase, phaseName, content, phaseStatus, phaseError, workflowStack, phaseExecutionId, iteration, completedAt, sanitizeText) {
    return {
        type: 'phase_complete',
        step: step.name,
        phase,
        phaseName,
        phaseExecutionId,
        status: phaseStatus,
        content: sanitizeText(content),
        timestamp: completedAt,
        ...serializeWorkflowStack(workflowStack),
        ...(phaseError ? { error: sanitizeText(phaseError) } : {}),
        ...(iteration != null ? { iteration } : {}),
    };
}
export function buildPromptLogRecord(step, phase, iteration, phaseExecutionId, promptParts, content, timestamp, sanitizeText) {
    return {
        step: step.name,
        phase,
        iteration,
        phaseExecutionId,
        prompt: sanitizeText(promptParts.userInstruction),
        systemPrompt: sanitizeText(promptParts.systemPrompt),
        userInstruction: sanitizeText(promptParts.userInstruction),
        response: sanitizeText(content),
        timestamp,
    };
}
export function buildPhaseJudgeStageRecord(step, phase, phaseName, entry, workflowStack, phaseExecutionId, iteration, sanitizeText) {
    return {
        type: 'phase_judge_stage',
        step: step.name,
        phase,
        phaseName,
        phaseExecutionId,
        stage: entry.stage,
        method: entry.method,
        status: entry.status,
        ...serializeWorkflowStack(workflowStack),
        instruction: sanitizeText(entry.instruction),
        response: sanitizeText(entry.response),
        timestamp: new Date().toISOString(),
        ...(iteration != null ? { iteration } : {}),
    };
}
export function buildStepStartRecord(step, iteration, instruction, workflowStack, sanitizeText, providerInfo) {
    return {
        type: 'step_start',
        step: step.name,
        persona: step.personaDisplayName,
        iteration,
        timestamp: new Date().toISOString(),
        ...serializeWorkflowStack(workflowStack),
        ...(instruction ? { instruction: sanitizeText(instruction) } : {}),
        ...(providerInfo?.provider !== undefined ? { provider: providerInfo.provider } : {}),
        ...(providerInfo?.providerSource !== undefined ? { providerSource: providerInfo.providerSource } : {}),
        ...(providerInfo?.model !== undefined ? { model: providerInfo.model } : {}),
        ...(providerInfo?.modelSource !== undefined ? { modelSource: providerInfo.modelSource } : {}),
        ...(providerInfo?.providerOptions !== undefined ? { providerOptions: providerInfo.providerOptions } : {}),
        ...(providerInfo?.providerOptionsSources !== undefined ? { providerOptionsSources: providerInfo.providerOptionsSources } : {}),
    };
}
export function buildStepCompleteRecord(step, response, instruction, iteration, workflowStack, sanitizeText) {
    const matchMethod = toJudgmentMatchMethod(response.matchedRuleMethod);
    return {
        type: 'step_complete',
        step: step.name,
        persona: response.persona,
        iteration,
        status: response.status,
        content: sanitizeText(response.content),
        instruction: sanitizeText(instruction),
        ...serializeWorkflowStack(workflowStack),
        ...(response.matchedRuleIndex != null ? { matchedRuleIndex: response.matchedRuleIndex } : {}),
        ...(response.matchedRuleMethod ? { matchedRuleMethod: response.matchedRuleMethod } : {}),
        ...(matchMethod ? { matchMethod } : {}),
        ...(response.error ? { error: sanitizeText(response.error) } : {}),
        ...(response.failureCategory ? { failureCategory: response.failureCategory } : {}),
        timestamp: response.timestamp.toISOString(),
    };
}
export function buildWorkflowCompleteRecord(state) {
    return {
        type: 'workflow_complete',
        iterations: state.iteration,
        endTime: new Date().toISOString(),
    };
}
export function buildWorkflowAbortRecord(state, reason, sanitizeText) {
    return {
        type: 'workflow_abort',
        iterations: state.iteration,
        reason: sanitizeText(reason),
        endTime: new Date().toISOString(),
    };
}
//# sourceMappingURL=sessionLoggerRecordFactory.js.map