import { formatPromptDeliverableRepairObservation, } from "./deliverable-observation.js";
import { buildRuntimeObservation } from "./runtime-observation-builder.js";
import { formatAgentVisibleRuntimeError } from "../../shared/utils/agent-error.js";
export function appendRuntimeObservation(messages, observation) {
    return [
        ...messages,
        {
            content: formatRuntimeObservation(observation),
            code: observation.code,
            role: "runtime",
            severity: observation.severity,
        },
    ];
}
export function formatRuntimeObservation(observation) {
    return [
        `Runtime observation [${observation.severity.toUpperCase()} ${observation.code}]:`,
        observation.message,
    ].join("\n");
}
export function textualToolCallObservation(input) {
    const subject = input.toolName
        ? input.toolKnown === false
            ? `${input.toolName} tool call, but that tool is not available`
            : `${input.toolName} tool call`
        : (input.description ?? "a textual tool-call/control payload");
    return buildRuntimeObservation({
        code: "TEXTUAL_TOOL_CALL",
        severity: "error",
        lines: formatAgentVisibleRuntimeError({
            code: "TEXTUAL_TOOL_CALL",
            message: input.toolName
                ? `Provider described a ${subject} in text instead of emitting a native decision`
                : `Provider exposed ${subject}`,
        }),
    });
}
export function structuredActionRequiredObservation(input) {
    const excerpt = formatResponseExcerpt(input.responseText);
    return buildRuntimeObservation({
        code: "STRUCTURED_ACTION_REQUIRED",
        severity: "error",
        lines: formatAgentVisibleRuntimeError({
            code: "STRUCTURED_ACTION_REQUIRED",
            message: "Direct prose is not valid for this structured runtime turn",
            details: [
                `Allowed runtime actions: ${input.actionPolicy.allowedActions.join(", ")}`,
                ...(excerpt ? [`Provider response excerpt: ${excerpt}`] : []),
            ],
        }),
    });
}
export function terminalOutputInvalidObservation(input) {
    const runtimeJsonContentHint = input.reasonCode === "runtime-decision-json"
        ? "If the user explicitly requested this JSON as content, return it in a fenced code block or write it to the requested file."
        : undefined;
    const deliverableLines = input.deliverableFailures && input.deliverableFailures.length > 0
        ? ["", formatPromptDeliverableRepairObservation(input.deliverableFailures)]
        : [];
    if (input.reasonCode === "provider-native-tool-call-json") {
        return buildRuntimeObservation({
            code: "CONTROL_PAYLOAD_LEAK",
            severity: "error",
            lines: [
                "Your previous visible text looked like provider-native tool-call JSON.",
                "Reason code: provider-native-tool-call-json.",
                `Allowed runtime actions: ${input.actionPolicy.allowedActions.join(", ")}.`,
                ...deliverableLines,
            ],
        });
    }
    if (input.invalidKind === "textual-tool-call") {
        const observation = textualToolCallObservation({
            actionPolicy: input.actionPolicy,
            description: input.reasonCode === "runtime-decision-json"
                ? "a runtime control JSON payload"
                : "a textual tool-call/control payload",
            toolKnown: input.leakedToolKnown,
            toolName: input.leakedToolName,
        });
        return {
            ...observation,
            message: buildRuntimeObservation({
                code: observation.code,
                severity: observation.severity,
                lines: [
                    observation.message,
                    input.reasonCode ? `Reason code: ${input.reasonCode}.` : undefined,
                    runtimeJsonContentHint,
                    ...deliverableLines,
                ],
            }).message,
        };
    }
    return buildRuntimeObservation({
        code: "CONTROL_PAYLOAD_LEAK",
        severity: "error",
        lines: [
            "Your previous visible text looked like internal control payload.",
            input.reasonCode ? `Reason code: ${input.reasonCode}.` : undefined,
            runtimeJsonContentHint,
            `Allowed runtime actions: ${input.actionPolicy.allowedActions.join(", ")}.`,
            ...deliverableLines,
        ],
    });
}
function formatResponseExcerpt(text) {
    const compact = text.replace(/\s+/gu, " ").trim();
    if (!compact) {
        return undefined;
    }
    return compact.length > 500 ? `${compact.slice(0, 500)}...` : compact;
}
//# sourceMappingURL=runtime-observation.js.map