|
32 | 32 | if TYPE_CHECKING: |
33 | 33 | from openarmature.llm.messages import ToolCall |
34 | 34 | from openarmature.llm.response import Usage |
35 | | - from openarmature.retrieval.response import EmbeddingUsage |
| 35 | + from openarmature.retrieval.response import EmbeddingUsage, RerankUsage, ScoredDocument |
36 | 36 |
|
37 | 37 | # Sentinel empty metadata mapping for events constructed without a |
38 | 38 | # live caller-metadata snapshot (test helpers, synthetic events). |
@@ -877,6 +877,142 @@ class EmbeddingFailedEvent: |
877 | 877 | caller_invocation_metadata: Mapping[str, AttributeValue] | None = None |
878 | 878 |
|
879 | 879 |
|
| 880 | +# Spec: realizes graph-engine §6 -- the typed RerankEvent / RerankFailedEvent |
| 881 | +# pair (proposal 0060, retrieval-provider rerank capability), the rerank |
| 882 | +# sibling to the EmbeddingEvent / EmbeddingFailedEvent pair. Dispatched on the |
| 883 | +# observer delivery queue per RerankProvider.rerank() call: the success variant |
| 884 | +# after the response is parsed + validated (retrieval-provider §6), the failure |
| 885 | +# variant alongside a raised §7 category exception (mutually exclusive per |
| 886 | +# call). Scalar fan_out_index / branch_name only, matching the embedding pair |
| 887 | +# (the lineage chains arrive uniformly across the provider events with proposal |
| 888 | +# 0084). query / documents / request_extras / output_results are payload- |
| 889 | +# bearing, populated unconditionally; observer-side privacy gates (OTel |
| 890 | +# disable_provider_payload, Langfuse equivalents) apply at rendering, symmetric |
| 891 | +# with EmbeddingEvent. |
| 892 | +@dataclass(frozen=True) |
| 893 | +class RerankEvent: |
| 894 | + """A typed rerank provider call event delivered to observers. |
| 895 | +
|
| 896 | + Carries identity, scoping, and outcome data for a successful |
| 897 | + ``RerankProvider.rerank()`` call. Observer code filters by type |
| 898 | + discrimination (``isinstance(event, RerankEvent)``). |
| 899 | +
|
| 900 | + The identity / scoping / request-side fields mirror ``EmbeddingEvent``'s |
| 901 | + convention; the outcome fields are rerank-specific: |
| 902 | +
|
| 903 | + - ``query``: the query string the call was made with; non-nullable, |
| 904 | + populated unconditionally (privacy gating is observer-side at |
| 905 | + rendering). |
| 906 | + - ``documents``: the input documents list; non-nullable, populated |
| 907 | + unconditionally. Same privacy posture as ``query``. |
| 908 | + - ``document_count``: ``len(documents)``; a convenience field. |
| 909 | + - ``top_k``: the caller-supplied ``top_k`` value; ``None`` when the |
| 910 | + caller passed ``None``. |
| 911 | + - ``result_count``: ``len(output_results)``; a convenience field. |
| 912 | + - ``output_results``: the scored results the call returned; populated |
| 913 | + unconditionally on the success event (privacy gating is observer-side |
| 914 | + at rendering). |
| 915 | + - ``response_model`` / ``response_id``: the provider-returned model and |
| 916 | + response identifiers; ``None`` when the provider returned none. |
| 917 | + - ``usage``: the rerank usage record; ``None`` when the call returned |
| 918 | + no usage. |
| 919 | + - ``request_params``: the rerank request parameters the caller supplied |
| 920 | + (e.g. ``return_documents``). Absence-is-meaningful: only supplied keys |
| 921 | + appear; an empty mapping when none. |
| 922 | + - ``request_extras``: the runtime-config extras pass-through bag. |
| 923 | + - ``active_prompt`` / ``active_prompt_group``: prompt-context snapshots |
| 924 | + at rerank-call time; ``None`` outside a binding. |
| 925 | + - ``call_id``: a per-call disambiguator, always present, freshly minted |
| 926 | + per ``rerank()`` call. |
| 927 | + """ |
| 928 | + |
| 929 | + invocation_id: str |
| 930 | + correlation_id: str | None |
| 931 | + node_name: str |
| 932 | + namespace: tuple[str, ...] |
| 933 | + attempt_index: int |
| 934 | + fan_out_index: int | None |
| 935 | + branch_name: str | None |
| 936 | + provider: str |
| 937 | + model: str |
| 938 | + response_id: str | None |
| 939 | + response_model: str | None |
| 940 | + # RerankUsage is a string-typed forward reference per the TYPE_CHECKING |
| 941 | + # import -- keeps the runtime import direction graph -> retrieval off the |
| 942 | + # module-load path. |
| 943 | + usage: "RerankUsage | None" |
| 944 | + latency_ms: float | None |
| 945 | + query: str |
| 946 | + documents: list[str] |
| 947 | + document_count: int |
| 948 | + top_k: int | None |
| 949 | + result_count: int |
| 950 | + # Spec graph-engine §6 (proposal 0089): the output scored results (sourced |
| 951 | + # from RerankResponse.results at dispatch). Populated unconditionally on the |
| 952 | + # success event -- payload-bearing like query / documents, gated observer- |
| 953 | + # side at the rendering boundary. The §8.4.7 rerank output mapping reads |
| 954 | + # this field. No output field on RerankFailedEvent (no response). |
| 955 | + output_results: list["ScoredDocument"] |
| 956 | + request_params: Mapping[str, Any] |
| 957 | + request_extras: Mapping[str, Any] |
| 958 | + active_prompt: Any |
| 959 | + active_prompt_group: Any |
| 960 | + call_id: str |
| 961 | + caller_invocation_metadata: Mapping[str, AttributeValue] | None = None |
| 962 | + |
| 963 | + |
| 964 | +# Spec: the failure sibling of RerankEvent (proposal 0060). Dispatched |
| 965 | +# whenever RerankProvider.rerank() raises a §7 category exception -- covers |
| 966 | +# both the provider-caught path and the pre-send validation raise |
| 967 | +# (provider_invalid_request on an empty query / documents list / non-positive |
| 968 | +# top_k). Dispatched ALONGSIDE the exception, not in place of it; mutually |
| 969 | +# exclusive with RerankEvent on the same call. The response-side fields |
| 970 | +# (usage / response_id / response_model / result_count / output_results) are |
| 971 | +# absent (no response). |
| 972 | +@dataclass(frozen=True) |
| 973 | +class RerankFailedEvent: |
| 974 | + """A typed rerank provider call failure event delivered to observers. |
| 975 | +
|
| 976 | + Carries identity, scoping, and failure-context data for a ``rerank()`` |
| 977 | + call that raised a retrieval-provider category exception. Observer code |
| 978 | + filters by type discrimination (``isinstance(event, RerankFailedEvent)``). |
| 979 | +
|
| 980 | + The identity / scoping / request-side field set mirrors ``RerankEvent``; |
| 981 | + the response-side fields are absent. Failure-specific fields: |
| 982 | +
|
| 983 | + - ``error_category``: the error category the call raised (one of the |
| 984 | + rerank-applicable provider categories). Always present. |
| 985 | + - ``error_type``: an optional impl-level / vendor-specific type or code; |
| 986 | + ``None`` when unavailable. |
| 987 | + - ``error_message``: a human-readable message; always present (the empty |
| 988 | + string when the exception carried no message). |
| 989 | + """ |
| 990 | + |
| 991 | + invocation_id: str |
| 992 | + correlation_id: str | None |
| 993 | + node_name: str |
| 994 | + namespace: tuple[str, ...] |
| 995 | + attempt_index: int |
| 996 | + fan_out_index: int | None |
| 997 | + branch_name: str | None |
| 998 | + provider: str |
| 999 | + model: str |
| 1000 | + latency_ms: float | None |
| 1001 | + query: str |
| 1002 | + documents: list[str] |
| 1003 | + document_count: int |
| 1004 | + top_k: int | None |
| 1005 | + request_params: Mapping[str, Any] |
| 1006 | + request_extras: Mapping[str, Any] |
| 1007 | + active_prompt: Any |
| 1008 | + active_prompt_group: Any |
| 1009 | + call_id: str |
| 1010 | + error_category: str |
| 1011 | + error_message: str |
| 1012 | + error_type: str | None = None |
| 1013 | + caller_invocation_metadata: Mapping[str, AttributeValue] | None = None |
| 1014 | + |
| 1015 | + |
880 | 1016 | # Spec: realizes pipeline-utilities §6.3 failure-isolation middleware |
881 | 1017 | # (proposal 0050). Emitted by FailureIsolationMiddleware when it |
882 | 1018 | # catches an exception escaping the inner chain and substitutes a |
@@ -1045,6 +1181,8 @@ class ToolCallFailedEvent: |
1045 | 1181 | "MetadataAugmentationEvent", |
1046 | 1182 | "NodeEvent", |
1047 | 1183 | "ParallelBranchesEventConfig", |
| 1184 | + "RerankEvent", |
| 1185 | + "RerankFailedEvent", |
1048 | 1186 | "ToolCallEvent", |
1049 | 1187 | "ToolCallFailedEvent", |
1050 | 1188 | ] |
0 commit comments