123 lines
5.4 KiB
JavaScript
123 lines
5.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ASSISTANT_MCP_DISCOVERY_RESPONSE_POLICY_SCHEMA_VERSION = void 0;
|
|
exports.applyAssistantMcpDiscoveryResponsePolicy = applyAssistantMcpDiscoveryResponsePolicy;
|
|
const assistantMcpDiscoveryResponseCandidate_1 = require("./assistantMcpDiscoveryResponseCandidate");
|
|
exports.ASSISTANT_MCP_DISCOVERY_RESPONSE_POLICY_SCHEMA_VERSION = "assistant_mcp_discovery_response_policy_v1";
|
|
const ALLOWED_CANDIDATE_STATUSES = new Set([
|
|
"ready_for_guarded_use",
|
|
"checked_sources_only_candidate",
|
|
"clarification_candidate"
|
|
]);
|
|
function toRecordObject(value) {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
return null;
|
|
}
|
|
return value;
|
|
}
|
|
function toNonEmptyString(value) {
|
|
if (value === null || value === undefined) {
|
|
return null;
|
|
}
|
|
const text = String(value).trim();
|
|
return text.length > 0 ? text : null;
|
|
}
|
|
function normalizeReasonCode(value) {
|
|
const normalized = value
|
|
.trim()
|
|
.replace(/[^\p{L}\p{N}_.:-]+/gu, "_")
|
|
.replace(/^_+|_+$/g, "")
|
|
.toLowerCase();
|
|
return normalized.length > 0 ? normalized.slice(0, 120) : null;
|
|
}
|
|
function pushReason(target, value) {
|
|
const normalized = normalizeReasonCode(value);
|
|
if (normalized && !target.includes(normalized)) {
|
|
target.push(normalized);
|
|
}
|
|
}
|
|
function hasInternalMechanics(value) {
|
|
const text = value.toLowerCase();
|
|
return (text.includes("query_documents") ||
|
|
text.includes("query_movements") ||
|
|
text.includes("primitive") ||
|
|
text.includes("runtime_") ||
|
|
text.includes("planner_") ||
|
|
text.includes("catalog_") ||
|
|
text.includes("select "));
|
|
}
|
|
function isMcpDiscoveryEntryPointContract(value) {
|
|
const record = toRecordObject(value);
|
|
return (record?.schema_version === "assistant_mcp_discovery_runtime_entry_point_v1" &&
|
|
record?.policy_owner === "assistantMcpDiscoveryRuntimeEntryPoint");
|
|
}
|
|
function resolveEntryPoint(input) {
|
|
if (isMcpDiscoveryEntryPointContract(input.entryPoint)) {
|
|
return input.entryPoint;
|
|
}
|
|
const runtimeMetaEntryPoint = input.addressRuntimeMeta?.mcpDiscoveryRuntimeEntryPoint ??
|
|
input.addressRuntimeMeta?.assistantMcpDiscoveryRuntimeEntryPoint ??
|
|
input.addressRuntimeMeta?.assistant_mcp_discovery_entry_point_v1;
|
|
return isMcpDiscoveryEntryPointContract(runtimeMetaEntryPoint) ? runtimeMetaEntryPoint : null;
|
|
}
|
|
function isUnsupportedCurrentTurnBoundary(input) {
|
|
return (input.modeDecisionReason === "unsupported_current_turn_meaning_boundary" ||
|
|
input.livingChatSource === "deterministic_unsupported_current_turn_boundary" ||
|
|
input.currentReplySource === "deterministic_unsupported_current_turn_boundary");
|
|
}
|
|
function applyAssistantMcpDiscoveryResponsePolicy(input) {
|
|
const currentReply = String(input.currentReply ?? "");
|
|
const currentReplySource = toNonEmptyString(input.currentReplySource) ?? toNonEmptyString(input.livingChatSource) ?? "unknown";
|
|
const entryPoint = resolveEntryPoint(input);
|
|
const candidate = (0, assistantMcpDiscoveryResponseCandidate_1.buildAssistantMcpDiscoveryResponseCandidate)(entryPoint);
|
|
const reasonCodes = [...candidate.reason_codes];
|
|
if (!entryPoint) {
|
|
pushReason(reasonCodes, "mcp_discovery_response_policy_no_entry_point");
|
|
}
|
|
if (!isUnsupportedCurrentTurnBoundary(input)) {
|
|
pushReason(reasonCodes, "mcp_discovery_response_policy_not_unsupported_boundary");
|
|
}
|
|
if (!ALLOWED_CANDIDATE_STATUSES.has(candidate.candidate_status)) {
|
|
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_status_not_allowed");
|
|
}
|
|
if (!candidate.eligible_for_future_hot_runtime) {
|
|
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_not_eligible");
|
|
}
|
|
if (!toNonEmptyString(candidate.reply_text)) {
|
|
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_missing_reply_text");
|
|
}
|
|
if (candidate.reply_text && hasInternalMechanics(candidate.reply_text)) {
|
|
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_contains_internal_mechanics");
|
|
}
|
|
const canApply = Boolean(entryPoint) &&
|
|
isUnsupportedCurrentTurnBoundary(input) &&
|
|
ALLOWED_CANDIDATE_STATUSES.has(candidate.candidate_status) &&
|
|
candidate.eligible_for_future_hot_runtime &&
|
|
Boolean(toNonEmptyString(candidate.reply_text)) &&
|
|
!hasInternalMechanics(String(candidate.reply_text ?? ""));
|
|
if (!canApply) {
|
|
pushReason(reasonCodes, "mcp_discovery_response_policy_kept_current_reply");
|
|
return {
|
|
schema_version: exports.ASSISTANT_MCP_DISCOVERY_RESPONSE_POLICY_SCHEMA_VERSION,
|
|
policy_owner: "assistantMcpDiscoveryResponsePolicy",
|
|
decision: "keep_current_reply",
|
|
applied: false,
|
|
reply_text: currentReply,
|
|
reply_source: currentReplySource,
|
|
candidate,
|
|
reason_codes: reasonCodes
|
|
};
|
|
}
|
|
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_applied");
|
|
return {
|
|
schema_version: exports.ASSISTANT_MCP_DISCOVERY_RESPONSE_POLICY_SCHEMA_VERSION,
|
|
policy_owner: "assistantMcpDiscoveryResponsePolicy",
|
|
decision: "apply_candidate",
|
|
applied: true,
|
|
reply_text: String(candidate.reply_text),
|
|
reply_source: "mcp_discovery_response_candidate_guarded",
|
|
candidate,
|
|
reason_codes: reasonCodes
|
|
};
|
|
}
|