const ARCHITECTURE_CONTEXT_PATTERN = /架构|architecture|系统设计|技术方案|现有系统|差距分析|gap.?analysis/i;
import { safeJsonParse } from "../../engine/core/helpers.js";
export function stripPathLikeReferences(text) {
    return text.replace(/(^|[\s"'(（])(?:~\/|\.{1,2}\/|\/|[A-Za-z0-9_.-]+\/)[^\s"'，。；,;：:)）]+/g, "$1 ");
}
export function hasArchitectureIntentSignal(intent) {
    return ARCHITECTURE_CONTEXT_PATTERN.test(stripPathLikeReferences(intent ?? ""));
}
export function decodeIndexedJsonString(value) {
    if (!value || typeof value !== "object" || Array.isArray(value))
        return value;
    const entries = Object.entries(value);
    if (entries.length === 0)
        return value;
    const isIndexedChars = entries.every(([key, entryValue]) => /^\d+$/.test(key) && typeof entryValue === "string" && entryValue.length <= 1);
    if (!isIndexedChars)
        return value;
    const serialized = entries
        .sort(([a], [b]) => Number(a) - Number(b))
        .map(([, entryValue]) => entryValue)
        .join("")
        .trim();
    if (!serialized.startsWith("{"))
        return value;
    return safeJsonParse(serialized) ?? value;
}
/**
 * 解析 MCP 输入中的契约对象。
 * @description 部分 MCP 客户端会把 z.unknown() 对象参数序列化为 JSON 字符串；
 *              入口层必须兼容字符串对象，避免后续机制访问数组字段时触发 JS 运行时错误。
 */
export function parseContractObject(value, fieldName) {
    if (value === undefined || value === null)
        return {};
    // 字符串 → 尝试 JSON 解析（可能被 MCP 客户端序列化为 JSON 字符串）
    let parsed;
    if (typeof value === "string") {
        parsed = safeJsonParse(value);
        if (parsed === undefined) {
            return { error: `${fieldName} 的 JSON 解析失败。请检查 JSON 格式是否正确` };
        }
    }
    else {
        parsed = value;
    }
    const candidate = decodeIndexedJsonString(parsed);
    if (!candidate || typeof candidate !== "object" || Array.isArray(candidate)) {
        return { error: `${fieldName} 必须是 JSON 对象（当前类型: ${Array.isArray(candidate) ? "array" : typeof candidate}）。期望格式请参考 expected_schema` };
    }
    return { value: candidate };
}
/** 需要通过 parseContractObject 校验的契约字段列表 */
export const CONTRACT_OBJECT_FIELDS = [
    "architecture_decision_workshop", "decision_workshop",
    "technology_decision_contract", "technology_decision_record",
    "detail_discipline_contract", "provided_details",
    "first_principles_frame", "brainstorm_session",
    "design_artifact_pack", "ood_solid_summary",
    "backend_implementation_work_package", "code_observability_work_package",
];
/** 需要规范化的字段 */
export const NORMALIZED_FIELDS = {
    decision_workshop: "decision_workshop",
};
//# sourceMappingURL=utils.js.map