const MALFORMED_TEXT_PATTERNS = [
    { code: 'OBJECT_OBJECT', pattern: /\[object Object\]/, label: '[object Object]' },
    { code: 'OBJECT_PROMISE', pattern: /\[object Promise\]/, label: '[object Promise]' },
];
export function findMalformedToolOutput(result) {
    for (let index = 0; index < result.content.length; index++) {
        const item = result.content[index];
        const text = item?.text ?? '';
        for (const rule of MALFORMED_TEXT_PATTERNS) {
            if (rule.pattern.test(text)) {
                return {
                    code: rule.code,
                    pattern: rule.label,
                    contentIndex: index,
                };
            }
        }
    }
    return null;
}
export function guardToolResultOutput(toolName, result) {
    const issue = findMalformedToolOutput(result);
    if (issue === null) {
        return result;
    }
    const name = toolName ?? 'unknown';
    const structuredKeys = Object.keys(result.structuredContent ?? {});
    return {
        content: [
            {
                type: 'text',
                text: [
                    '❌ **Output integrity guard blocked malformed response**',
                    '',
                    `Tool \`${name}\` produced non-human-readable text (${issue.code}).`,
                    'The operation may have completed before the response was blocked.',
                    'Run `planu_status` or retry the tool after the formatting bug is fixed.',
                ].join('\n'),
            },
        ],
        isError: true,
        structuredContent: {
            error: 'OUTPUT_INTEGRITY_GUARD',
            toolName: name,
            pattern: issue.code,
            contentIndex: issue.contentIndex,
            operationMayHaveCompleted: true,
            originalStructuredContentKeys: structuredKeys,
            fixHint: 'A Planu tool attempted to render a structured object as human text. Format the object explicitly before returning content[].text.',
        },
    };
}
//# sourceMappingURL=output-integrity-guard.js.map