|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from retriever import build_context_pack, retrieve_candidates |
| 7 | + |
| 8 | + |
| 9 | +class StopRun(Exception): |
| 10 | + def __init__(self, reason: str): |
| 11 | + super().__init__(reason) |
| 12 | + self.reason = reason |
| 13 | + |
| 14 | + |
| 15 | +@dataclass(frozen=True) |
| 16 | +class Budget: |
| 17 | + max_query_chars: int = 240 |
| 18 | + max_top_k: int = 6 |
| 19 | + max_context_chunks: int = 3 |
| 20 | + max_context_chars: int = 2200 |
| 21 | + min_chunk_score: float = 0.2 |
| 22 | + max_seconds: int = 20 |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +def validate_retrieval_intent( |
| 27 | + raw: Any, |
| 28 | + *, |
| 29 | + allowed_sources_policy: set[str], |
| 30 | + max_top_k: int, |
| 31 | +) -> dict[str, Any]: |
| 32 | + if not isinstance(raw, dict): |
| 33 | + raise StopRun("invalid_intent:not_object") |
| 34 | + |
| 35 | + if raw.get("kind") != "retrieve": |
| 36 | + raise StopRun("invalid_intent:kind") |
| 37 | + |
| 38 | + query = raw.get("query") |
| 39 | + if not isinstance(query, str) or not query.strip(): |
| 40 | + raise StopRun("invalid_intent:query") |
| 41 | + |
| 42 | + top_k = raw.get("top_k", 4) |
| 43 | + if not isinstance(top_k, int) or not (1 <= top_k <= max_top_k): |
| 44 | + raise StopRun("invalid_intent:top_k") |
| 45 | + |
| 46 | + sources_raw = raw.get("sources") |
| 47 | + normalized_sources: list[str] = [] |
| 48 | + if sources_raw is not None: |
| 49 | + if not isinstance(sources_raw, list) or not sources_raw: |
| 50 | + raise StopRun("invalid_intent:sources") |
| 51 | + for source in sources_raw: |
| 52 | + if not isinstance(source, str) or not source.strip(): |
| 53 | + raise StopRun("invalid_intent:source_item") |
| 54 | + source_name = source.strip() |
| 55 | + if source_name not in allowed_sources_policy: |
| 56 | + raise StopRun(f"invalid_intent:source_not_allowed:{source_name}") |
| 57 | + normalized_sources.append(source_name) |
| 58 | + |
| 59 | + # Ignore unknown keys and keep only contract fields. |
| 60 | + payload = { |
| 61 | + "kind": "retrieve", |
| 62 | + "query": query.strip(), |
| 63 | + "top_k": top_k, |
| 64 | + } |
| 65 | + if normalized_sources: |
| 66 | + payload["sources"] = normalized_sources |
| 67 | + return payload |
| 68 | + |
| 69 | + |
| 70 | +class RetrievalGateway: |
| 71 | + def __init__( |
| 72 | + self, |
| 73 | + *, |
| 74 | + documents: list[dict[str, Any]], |
| 75 | + budget: Budget, |
| 76 | + allow_execution_sources: set[str], |
| 77 | + ): |
| 78 | + self.documents = documents |
| 79 | + self.budget = budget |
| 80 | + self.allow_execution_sources = set(allow_execution_sources) |
| 81 | + |
| 82 | + def run(self, intent: dict[str, Any]) -> dict[str, Any]: |
| 83 | + query = intent["query"] |
| 84 | + if len(query) > self.budget.max_query_chars: |
| 85 | + raise StopRun("invalid_intent:query_too_long") |
| 86 | + |
| 87 | + requested_sources = set(intent.get("sources") or self.allow_execution_sources) |
| 88 | + denied = sorted(requested_sources - self.allow_execution_sources) |
| 89 | + if denied: |
| 90 | + raise StopRun(f"source_denied:{denied[0]}") |
| 91 | + |
| 92 | + candidates = retrieve_candidates( |
| 93 | + query=query, |
| 94 | + documents=self.documents, |
| 95 | + top_k=intent["top_k"], |
| 96 | + allowed_sources=requested_sources, |
| 97 | + ) |
| 98 | + |
| 99 | + context_pack = build_context_pack( |
| 100 | + candidates=candidates, |
| 101 | + min_score=self.budget.min_chunk_score, |
| 102 | + max_chunks=self.budget.max_context_chunks, |
| 103 | + max_chars=self.budget.max_context_chars, |
| 104 | + ) |
| 105 | + |
| 106 | + return { |
| 107 | + "query": query, |
| 108 | + "requested_sources": sorted(requested_sources), |
| 109 | + "candidates": candidates, |
| 110 | + "context_chunks": context_pack["chunks"], |
| 111 | + "context_total_chars": context_pack["total_chars"], |
| 112 | + "rejected_low_score": context_pack["rejected_low_score"], |
| 113 | + } |
0 commit comments