|
1 | | -from abc import ABC, abstractmethod |
| 1 | +from abc import ABC |
2 | 2 | from collections.abc import Sequence |
3 | | -from typing import Any, ClassVar, TypeVar |
| 3 | +from typing import TYPE_CHECKING, Any, ClassVar, TypeVar |
4 | 4 |
|
5 | 5 | from pydantic import ConfigDict, Field, create_model |
6 | 6 | from rich.text import Text |
|
13 | 13 | from openhands.sdk.utils.visualize import display_dict |
14 | 14 |
|
15 | 15 |
|
| 16 | +if TYPE_CHECKING: |
| 17 | + from typing import Self |
| 18 | + |
16 | 19 | S = TypeVar("S", bound="Schema") |
17 | 20 |
|
18 | 21 |
|
@@ -190,23 +193,84 @@ def visualize(self) -> Text: |
190 | 193 | class Observation(Schema, ABC): |
191 | 194 | """Base schema for output observation.""" |
192 | 195 |
|
| 196 | + ERROR_MESSAGE_HEADER: ClassVar[str] = "[An error occurred during execution.]\n" |
| 197 | + |
| 198 | + content: list[TextContent | ImageContent] = Field( |
| 199 | + default_factory=list, |
| 200 | + description=( |
| 201 | + "Content returned from the tool as a list of " |
| 202 | + "TextContent/ImageContent objects. " |
| 203 | + "When there is an error, it should be written in this field." |
| 204 | + ), |
| 205 | + ) |
| 206 | + is_error: bool = Field( |
| 207 | + default=False, description="Whether the observation indicates an error" |
| 208 | + ) |
| 209 | + |
| 210 | + @classmethod |
| 211 | + def from_text( |
| 212 | + cls, |
| 213 | + text: str, |
| 214 | + is_error: bool = False, |
| 215 | + **kwargs: Any, |
| 216 | + ) -> "Self": |
| 217 | + """Utility to create an Observation from a simple text string. |
| 218 | +
|
| 219 | + Args: |
| 220 | + text: The text content to include in the observation. |
| 221 | + is_error: Whether this observation represents an error. |
| 222 | + **kwargs: Additional fields for the observation subclass. |
| 223 | +
|
| 224 | + Returns: |
| 225 | + An Observation instance with the text wrapped in a TextContent. |
| 226 | + """ |
| 227 | + return cls(content=[TextContent(text=text)], is_error=is_error, **kwargs) |
| 228 | + |
| 229 | + @property |
| 230 | + def text(self) -> str: |
| 231 | + """Extract all text content from the observation. |
| 232 | +
|
| 233 | + Returns: |
| 234 | + Concatenated text from all TextContent items in content. |
| 235 | + """ |
| 236 | + return "".join( |
| 237 | + item.text for item in self.content if isinstance(item, TextContent) |
| 238 | + ) |
| 239 | + |
193 | 240 | @property |
194 | | - @abstractmethod |
195 | 241 | def to_llm_content(self) -> Sequence[TextContent | ImageContent]: |
196 | | - """Get the observation string to show to the agent.""" |
| 242 | + """ |
| 243 | + Default content formatting for converting observation to LLM readable content. |
| 244 | + Subclasses can override to provide richer content (e.g., images, diffs). |
| 245 | + """ |
| 246 | + llm_content: list[TextContent | ImageContent] = [] |
| 247 | + |
| 248 | + # If is_error is true, prepend error message |
| 249 | + if self.is_error: |
| 250 | + llm_content.append(TextContent(text=self.ERROR_MESSAGE_HEADER)) |
| 251 | + |
| 252 | + # Add content (now always a list) |
| 253 | + llm_content.extend(self.content) |
| 254 | + |
| 255 | + return llm_content |
197 | 256 |
|
198 | 257 | @property |
199 | 258 | def visualize(self) -> Text: |
200 | | - """Return Rich Text representation of this action. |
| 259 | + """Return Rich Text representation of this observation. |
201 | 260 |
|
202 | | - This method can be overridden by subclasses to customize visualization. |
203 | | - The base implementation displays all action fields systematically. |
| 261 | + Subclasses can override for custom visualization; by default we show the |
| 262 | + same text that would be sent to the LLM. |
204 | 263 | """ |
205 | | - content = Text() |
| 264 | + text = Text() |
| 265 | + |
| 266 | + if self.is_error: |
| 267 | + text.append("❌ ", style="red bold") |
| 268 | + text.append(self.ERROR_MESSAGE_HEADER, style="bold red") |
| 269 | + |
206 | 270 | text_parts = content_to_str(self.to_llm_content) |
207 | 271 | if text_parts: |
208 | 272 | full_content = "".join(text_parts) |
209 | | - content.append(full_content) |
| 273 | + text.append(full_content) |
210 | 274 | else: |
211 | | - content.append("[no text content]") |
212 | | - return content |
| 275 | + text.append("[no text content]") |
| 276 | + return text |
0 commit comments