Skip to content

Commit d03e682

Browse files
fix: Correct events property return type and add get_index to EventsListBase
- Change ConversationState.events return type from Sequence[Event] to EventsListBase - Add get_index abstract method to EventsListBase for event lookup by ID - Implement get_index in RemoteEventsList class This fixes pyright type errors where code was calling .append() and .get_index() on state.events, which are not available on Sequence[Event]. Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 89ae8b3 commit d03e682

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

openhands-sdk/openhands/sdk/conversation/events_list_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections.abc import Sequence
33

44
from openhands.sdk.event import Event
5+
from openhands.sdk.event.types import EventID
56

67

78
class EventsListBase(Sequence[Event], ABC):
@@ -15,3 +16,8 @@ class EventsListBase(Sequence[Event], ABC):
1516
def append(self, event: Event) -> None:
1617
"""Add a new event to the list."""
1718
...
19+
20+
@abstractmethod
21+
def get_index(self, event_id: EventID) -> int:
22+
"""Return the integer index for a given event_id."""
23+
...

openhands-sdk/openhands/sdk/conversation/impl/remote_conversation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ def append(self, event: Event) -> None:
352352
"""Add a new event to the list (for compatibility with EventLog interface)."""
353353
self.add_event(event)
354354

355+
def get_index(self, event_id: str) -> int:
356+
"""Return the integer index for a given event_id."""
357+
with self._lock:
358+
for idx, event in enumerate(self._cached_events):
359+
if event.id == event_id:
360+
return idx
361+
raise KeyError(f"Unknown event_id: {event_id}")
362+
355363
def create_default_callback(self) -> ConversationCallbackType:
356364
"""Create a default callback that adds events to this list."""
357365

openhands-sdk/openhands/sdk/conversation/state.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from openhands.sdk.conversation.compliance import APIComplianceMonitor
1414
from openhands.sdk.conversation.conversation_stats import ConversationStats
1515
from openhands.sdk.conversation.event_store import EventLog
16+
from openhands.sdk.conversation.events_list_base import EventsListBase
1617
from openhands.sdk.conversation.fifo_lock import FIFOLock
1718
from openhands.sdk.conversation.persistence_const import BASE_STATE, EVENTS_DIR
1819
from openhands.sdk.conversation.secret_registry import SecretRegistry
@@ -213,10 +214,10 @@ def _handle_legacy_fields(cls, data: Any) -> Any:
213214
return data
214215

215216
@property
216-
def events(self) -> Sequence[Event]:
217+
def events(self) -> EventsListBase:
217218
"""Read-only view of conversation events.
218219
219-
Returns events as a Sequence to discourage direct mutation.
220+
Returns events as EventsListBase to discourage direct mutation.
220221
Use add_event() to add new events with compliance monitoring.
221222
"""
222223
return self._events

0 commit comments

Comments
 (0)