|
| 1 | +# Copyright 2025 © BeeAI a Series of LF Projects, LLC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import contextvars |
| 7 | +import json |
| 8 | +import traceback |
| 9 | +from collections.abc import AsyncIterator |
| 10 | +from contextlib import asynccontextmanager |
| 11 | +from types import NoneType |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +import pydantic |
| 15 | + |
| 16 | +from agentstack_sdk.a2a.extensions.base import ( |
| 17 | + BaseExtensionClient, |
| 18 | + BaseExtensionServer, |
| 19 | + BaseExtensionSpec, |
| 20 | +) |
| 21 | +from agentstack_sdk.a2a.types import AgentMessage, JsonDict, Metadata |
| 22 | + |
| 23 | + |
| 24 | +class Error(pydantic.BaseModel): |
| 25 | + """ |
| 26 | + Represents error information for displaying exceptions in the UI. |
| 27 | +
|
| 28 | + This extension helps display errors in a user-friendly way with: |
| 29 | + - A clear error title (exception type) |
| 30 | + - A descriptive error message |
| 31 | +
|
| 32 | + Visually, this may appear as an error card in the UI. |
| 33 | +
|
| 34 | + Properties: |
| 35 | + - title: Title of the error (typically the exception class name). |
| 36 | + - message: The error message describing what went wrong. |
| 37 | + """ |
| 38 | + |
| 39 | + title: str |
| 40 | + message: str |
| 41 | + |
| 42 | + |
| 43 | +class ErrorGroup(pydantic.BaseModel): |
| 44 | + """ |
| 45 | + Represents a group of errors. |
| 46 | +
|
| 47 | + Properties: |
| 48 | + - message: A message describing the group of errors. |
| 49 | + - errors: A list of error objects. |
| 50 | + """ |
| 51 | + |
| 52 | + message: str |
| 53 | + errors: list[Error] |
| 54 | + |
| 55 | + |
| 56 | +class ErrorMetadata(pydantic.BaseModel): |
| 57 | + """ |
| 58 | + Metadata containing an error (or group of errors) and an optional stack trace. |
| 59 | +
|
| 60 | + Properties: |
| 61 | + - error: The error object or group of errors. |
| 62 | + - stack_trace: Optional formatted stack trace for debugging. |
| 63 | + - context: Optional context dictionary. |
| 64 | + """ |
| 65 | + |
| 66 | + error: Error | ErrorGroup |
| 67 | + stack_trace: str | None = None |
| 68 | + context: JsonDict | None = None |
| 69 | + |
| 70 | + |
| 71 | +class ErrorExtensionParams(pydantic.BaseModel): |
| 72 | + """ |
| 73 | + Configuration parameters for the error extension. |
| 74 | +
|
| 75 | + Properties: |
| 76 | + - include_stacktrace: Whether to include stack traces in error messages (default: False). |
| 77 | + """ |
| 78 | + |
| 79 | + include_stacktrace: bool = False |
| 80 | + |
| 81 | + |
| 82 | +class ErrorExtensionSpec(BaseExtensionSpec[ErrorExtensionParams]): |
| 83 | + URI: str = "https://a2a-extensions.agentstack.beeai.dev/ui/error/v1" |
| 84 | + |
| 85 | + |
| 86 | +def _format_stacktrace(exc: BaseException, include_cause: bool = True) -> str: |
| 87 | + """Format exception with full traceback including nested causes.""" |
| 88 | + return "".join(traceback.format_exception(type(exc), exc, exc.__traceback__, chain=include_cause)) |
| 89 | + |
| 90 | + |
| 91 | +def _extract_error(exc: BaseException) -> Error | ErrorGroup: |
| 92 | + """ |
| 93 | + Extract error information from an exception, handling: |
| 94 | + - BaseExceptionGroup (returns ErrorGroup) |
| 95 | + - FrameworkError from beeai_framework (uses .explain() method) |
| 96 | + """ |
| 97 | + # Handle BaseExceptionGroup by recursively extracting errors from each exception |
| 98 | + if isinstance(exc, BaseExceptionGroup): |
| 99 | + errors: list[Error] = [] |
| 100 | + for sub_exc in exc.exceptions: |
| 101 | + extracted = _extract_error(sub_exc) |
| 102 | + if isinstance(extracted, ErrorGroup): |
| 103 | + errors.extend(extracted.errors) |
| 104 | + else: |
| 105 | + errors.append(extracted) |
| 106 | + return ErrorGroup(message=str(exc), errors=errors) |
| 107 | + |
| 108 | + # Try to handle FrameworkError if beeai_framework is available |
| 109 | + try: |
| 110 | + from beeai_framework.errors import FrameworkError |
| 111 | + |
| 112 | + if isinstance(exc, FrameworkError): |
| 113 | + # FrameworkError has special .explain() method |
| 114 | + return Error(title=exc.name(), message=exc.explain()) |
| 115 | + except ImportError: |
| 116 | + # beeai_framework not installed, continue with standard handling |
| 117 | + pass |
| 118 | + |
| 119 | + return Error(title=type(exc).__name__, message=str(exc)) |
| 120 | + |
| 121 | + |
| 122 | +class ErrorExtensionServer(BaseExtensionServer[ErrorExtensionSpec, NoneType]): |
| 123 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 124 | + super().__init__(*args, **kwargs) |
| 125 | + # Server-scoped ContextVar for request-scoped error context |
| 126 | + self._error_context_var: contextvars.ContextVar[JsonDict] = contextvars.ContextVar("error_context") |
| 127 | + |
| 128 | + @asynccontextmanager |
| 129 | + async def lifespan(self) -> AsyncIterator[None]: |
| 130 | + """Set up request-scoped error context using ContextVar.""" |
| 131 | + # Set an empty dict for this request's context |
| 132 | + token = self._error_context_var.set({}) |
| 133 | + |
| 134 | + try: |
| 135 | + yield |
| 136 | + finally: |
| 137 | + self._error_context_var.reset(token) |
| 138 | + |
| 139 | + @property |
| 140 | + def context(self) -> JsonDict: |
| 141 | + """Get the current request's error context.""" |
| 142 | + try: |
| 143 | + return self._error_context_var.get() |
| 144 | + except LookupError: |
| 145 | + # Fallback for when lifespan hasn't been entered yet |
| 146 | + return {} |
| 147 | + |
| 148 | + def error_metadata(self, error: BaseException) -> Metadata[str, Any]: |
| 149 | + """ |
| 150 | + Create metadata for an error. |
| 151 | +
|
| 152 | + Args: |
| 153 | + error: The exception to convert to metadata |
| 154 | +
|
| 155 | + Returns: |
| 156 | + Metadata dictionary with error information |
| 157 | + """ |
| 158 | + error_data = _extract_error(error) |
| 159 | + stack_trace = _format_stacktrace(error) if self.spec.params.include_stacktrace else None |
| 160 | + return Metadata( |
| 161 | + { |
| 162 | + self.spec.URI: ErrorMetadata( |
| 163 | + error=error_data, |
| 164 | + stack_trace=stack_trace, |
| 165 | + context=self.context, |
| 166 | + ).model_dump(mode="json") |
| 167 | + } |
| 168 | + ) |
| 169 | + |
| 170 | + def message( |
| 171 | + self, |
| 172 | + error: BaseException, |
| 173 | + ) -> AgentMessage: |
| 174 | + """ |
| 175 | + Create an AgentMessage with error metadata and serialized text representation. |
| 176 | +
|
| 177 | + Args: |
| 178 | + error: The exception to include in the message |
| 179 | +
|
| 180 | + Returns: |
| 181 | + AgentMessage with error metadata and markdown-formatted text |
| 182 | + """ |
| 183 | + metadata = self.error_metadata(error) |
| 184 | + error_metadata = ErrorMetadata.model_validate(metadata[self.spec.URI]) |
| 185 | + |
| 186 | + # Serialize to markdown for display |
| 187 | + text_lines: list[str] = [] |
| 188 | + if isinstance(error_metadata.error, ErrorGroup): |
| 189 | + text_lines.append(f"## {error_metadata.error.message}\n") |
| 190 | + for err in error_metadata.error.errors: |
| 191 | + text_lines.append(f"### {err.title}\n{err.message}") |
| 192 | + else: |
| 193 | + text_lines.append(f"## {error_metadata.error.title}\n{error_metadata.error.message}") |
| 194 | + |
| 195 | + # Add context if present |
| 196 | + if error_metadata.context: |
| 197 | + text_lines.append(f"## Context\n```json\n{json.dumps(error_metadata.context, indent=2)}\n```") |
| 198 | + |
| 199 | + if error_metadata.stack_trace: |
| 200 | + text_lines.append(f"## Stack Trace\n```\n{error_metadata.stack_trace}\n```") |
| 201 | + |
| 202 | + text = "\n\n".join(text_lines) |
| 203 | + |
| 204 | + return AgentMessage(text=text, metadata=metadata) |
| 205 | + |
| 206 | + |
| 207 | +class ErrorExtensionClient(BaseExtensionClient[ErrorExtensionSpec, ErrorMetadata]): ... |
0 commit comments