Planner Autonomy: выводить lane из metadata surface

This commit is contained in:
2026-05-01 13:57:40 +03:00
parent a4db26a76e
commit ccfa9283e9
6 changed files with 249 additions and 53 deletions
@@ -249,6 +249,73 @@ function mergeCatalogPrimitivesWithFallback(
return result;
}
function normalizeMetadataSurfaceToken(value: unknown): string {
return String(value ?? "").trim().toLowerCase().replace(/[\s_.-]+/g, "");
}
function metadataSurfaceValueSuggestsDocument(value: unknown): boolean {
const token = normalizeMetadataSurfaceToken(value);
return token.includes("document") || token.includes("invoice") || token.includes("waybill") || token.includes("act");
}
function metadataSurfaceValueSuggestsMovement(value: unknown): boolean {
const token = normalizeMetadataSurfaceToken(value);
return token.includes("register") || token.includes("movement") || token.includes("operation") || token.includes("bank");
}
function metadataSurfaceValueSuggestsCatalog(value: unknown): boolean {
const token = normalizeMetadataSurfaceToken(value);
return token.includes("catalog") || token.includes("directory");
}
function routeFamilyFromMetadataSurfaceRef(
surface: AssistantMcpDiscoveryMetadataSurfaceRef | null | undefined
): AssistantMcpDiscoveryMetadataRouteFamily | null {
if (!surface || surface.ambiguity_detected) {
return null;
}
if (surface.downstream_route_family) {
return surface.downstream_route_family;
}
const values = [surface.selected_entity_set ?? "", ...surface.selected_surface_objects];
const documentHit = values.some(metadataSurfaceValueSuggestsDocument);
const movementHit = values.some(metadataSurfaceValueSuggestsMovement);
const catalogHit = values.some(metadataSurfaceValueSuggestsCatalog);
const hitCount = [documentHit, movementHit, catalogHit].filter(Boolean).length;
if (hitCount !== 1) {
return null;
}
if (documentHit) {
return "document_evidence";
}
if (movementHit) {
return "movement_evidence";
}
return "catalog_drilldown";
}
function inferredRouteFamilyFromMetadataSurfaceRef(
surface: AssistantMcpDiscoveryMetadataSurfaceRef | null | undefined
): boolean {
return Boolean(surface && !surface.downstream_route_family && routeFamilyFromMetadataSurfaceRef(surface));
}
function preferredPrimitiveForRouteFamily(
routeFamily: AssistantMcpDiscoveryMetadataRouteFamily | null
): AssistantMcpDiscoveryPrimitive | null {
if (routeFamily === "document_evidence") {
return "query_documents";
}
if (routeFamily === "movement_evidence") {
return "query_movements";
}
if (routeFamily === "catalog_drilldown") {
return "drilldown_related_objects";
}
return null;
}
function preferredPrimitiveFromMetadataSurface(
surface: AssistantMcpDiscoveryMetadataSurfaceRef | null | undefined
): AssistantMcpDiscoveryPrimitive | null {
@@ -259,16 +326,7 @@ function preferredPrimitiveFromMetadataSurface(
if (recommendedPrimitive) {
return recommendedPrimitive;
}
if (surface?.downstream_route_family === "document_evidence") {
return "query_documents";
}
if (surface?.downstream_route_family === "movement_evidence") {
return "query_movements";
}
if (surface?.downstream_route_family === "catalog_drilldown") {
return "drilldown_related_objects";
}
return null;
return preferredPrimitiveForRouteFamily(routeFamilyFromMetadataSurfaceRef(surface));
}
function preferredPrimitiveFromExplicitDataNeedGraph(
@@ -466,7 +524,8 @@ function routeFamilyFromThinMetadataSurfaceInput(
input: AssistantMcpDiscoveryPlannerInput
): AssistantMcpDiscoveryMetadataRouteFamily | null {
const surface = input.metadataSurface ?? null;
if (!surface || surface.ambiguity_detected || !surface.downstream_route_family || !surface.recommended_next_primitive) {
const surfaceRouteFamily = routeFamilyFromMetadataSurfaceRef(surface);
if (!surface || surface.ambiguity_detected || !surfaceRouteFamily) {
return null;
}
const meaning = input.turnMeaning ?? null;
@@ -483,22 +542,16 @@ function routeFamilyFromThinMetadataSurfaceInput(
return null;
}
if (graphFactFamily === "document_evidence" || includesAny(combined, ["document", "documents", "list_documents"])) {
return surface.downstream_route_family === "document_evidence" ? "document_evidence" : null;
return surfaceRouteFamily === "document_evidence" ? "document_evidence" : null;
}
if (graphFactFamily === "movement_evidence" || includesAny(combined, ["movement", "movements", "list_movements", "bank_operations"])) {
return surface.downstream_route_family === "movement_evidence" ? "movement_evidence" : null;
return surfaceRouteFamily === "movement_evidence" ? "movement_evidence" : null;
}
if (graphFactFamily === "schema_surface" || includesAny(combined, ["catalog", "directory", "inspect_catalog"])) {
return surface.downstream_route_family === "catalog_drilldown" ? "catalog_drilldown" : null;
return surfaceRouteFamily === "catalog_drilldown" ? "catalog_drilldown" : null;
}
if (!graphFactFamily && !domain && !action) {
if (
surface.downstream_route_family === "document_evidence" ||
surface.downstream_route_family === "movement_evidence" ||
surface.downstream_route_family === "catalog_drilldown"
) {
return surface.downstream_route_family;
}
return surfaceRouteFamily;
}
return null;
}
@@ -555,7 +608,12 @@ function recipeFor(input: AssistantMcpDiscoveryPlannerInput): PlannerRecipe {
reason: "planner_selected_document_from_confirmed_metadata_surface_ref",
chainSummary:
"Ground the next checked document lane from the confirmed metadata surface, then fetch scoped document rows and probe coverage before answering.",
extraReasons: primitiveSelection.reasonCodes
extraReasons: [
...primitiveSelection.reasonCodes,
...(inferredRouteFamilyFromMetadataSurfaceRef(input.metadataSurface)
? ["planner_inferred_next_lane_from_unambiguous_metadata_surface"]
: [])
]
});
}
if (thinSurfaceRouteFamily === "movement_evidence") {
@@ -575,7 +633,12 @@ function recipeFor(input: AssistantMcpDiscoveryPlannerInput): PlannerRecipe {
reason: "planner_selected_movement_from_confirmed_metadata_surface_ref",
chainSummary:
"Ground the next checked movement lane from the confirmed metadata surface, then fetch scoped movement rows and probe coverage before answering.",
extraReasons: primitiveSelection.reasonCodes
extraReasons: [
...primitiveSelection.reasonCodes,
...(inferredRouteFamilyFromMetadataSurfaceRef(input.metadataSurface)
? ["planner_inferred_next_lane_from_unambiguous_metadata_surface"]
: [])
]
});
}
if (thinSurfaceRouteFamily === "catalog_drilldown") {
@@ -595,7 +658,12 @@ function recipeFor(input: AssistantMcpDiscoveryPlannerInput): PlannerRecipe {
reason: "planner_selected_catalog_drilldown_from_confirmed_metadata_surface_ref",
chainSummary:
"Drill deeper into the confirmed catalog-oriented metadata surface, inspect related metadata objects, and keep the next safe lane grounded in checked schema evidence.",
extraReasons: primitiveSelection.reasonCodes
extraReasons: [
...primitiveSelection.reasonCodes,
...(inferredRouteFamilyFromMetadataSurfaceRef(input.metadataSurface)
? ["planner_inferred_next_lane_from_unambiguous_metadata_surface"]
: [])
]
});
}