function estimateMessageSize(message) {
    if (message.role === "user") {
        return typeof message.content === "string" ? message.content.length : JSON.stringify(message.content).length;
    }
    if (message.role === "assistant" || message.role === "toolResult") {
        return JSON.stringify(message).length;
    }
    return 120;
}
function selectTailMessages(messages, budget) {
    if (budget <= 0) {
        return [];
    }
    const selected = [];
    let used = 0;
    for (let i = messages.length - 1; i >= 0; i--) {
        const size = estimateMessageSize(messages[i]);
        if (selected.length > 0 && used + size > budget) {
            break;
        }
        selected.unshift(messages[i]);
        used += size;
    }
    return selected;
}
function buildProjectionText(inputs) {
    const lines = [];
    lines.push("Local projected context:");
    lines.push(`- turn: ${inputs.turnIndex}`);
    lines.push(`- task.ageInTurns: ${inputs.taskState.ageInTurns}`);
    if (inputs.taskState.summary) {
        lines.push(`- task.summary: ${inputs.taskState.summary}`);
    }
    if (inputs.taskState.nextStep) {
        lines.push(`- task.nextStep: ${inputs.taskState.nextStep}`);
    }
    lines.push(`- workingMemory.confidence: ${inputs.workingMemory.confidence.toFixed(2)}`);
    for (const item of inputs.workingMemory.items.slice(0, 3)) {
        lines.push(`- wm: ${item.text}`);
    }
    for (const segment of inputs.fileSegments.slice(0, 3)) {
        lines.push(`- file(${segment.path}): ${segment.snippet}`);
    }
    return lines.join("\n");
}
export function buildProjectedContext(messages, inputs) {
    const sessionBudget = Math.max(200, inputs.policy.sessionSummaryBudget + inputs.policy.taskStateBudget + inputs.policy.workingMemoryBudget);
    const projectedTail = selectTailMessages(messages, sessionBudget);
    const projectionBlock = {
        role: "custom",
        customType: "local_projection",
        content: buildProjectionText(inputs),
        display: false,
        details: {
            kind: "local_projection",
            turn: inputs.turnIndex,
        },
        timestamp: Date.now(),
    };
    return [projectionBlock, ...projectedTail];
}
//# sourceMappingURL=projected-context.js.map