Skip to content

Commit

Permalink
Import event histories
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartManoj committed Aug 23, 2024
1 parent 16b5348 commit 7d9314f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,4 @@ containers/runtime/Dockerfile
containers/runtime/project.tar.gz
/workspace2
/workspace3
trajectory.json
2 changes: 1 addition & 1 deletion openhands/events/serialization/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def event_from_dict(data) -> 'Event':
elif 'log' in data:
evt = LogEvent(log=data['log'])
else:
raise ValueError('Unknown event type: ' + data)
raise ValueError(f'Unknown event: {data}')
for key in UNDERSCORE_KEYS:
if key in data:
value = data[key]
Expand Down
18 changes: 18 additions & 0 deletions openhands/events/stream.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import os
import threading
from datetime import datetime
from enum import Enum
Expand Down Expand Up @@ -37,6 +38,23 @@ def __init__(self, sid: str, file_store: FileStore):
self._lock = threading.Lock()
self._reinitialize_from_file_store()

# add events from trajectory.json
event_history_path = r'event_history.json'
if not os.path.exists(event_history_path):
return
for event in json.loads(open(event_history_path).read()):
if event.get('status'):
continue
if event.get('action') == 'initialize':
continue
event = event_from_dict(event)
source = (
EventSource.AGENT
if event.source == EventSource.AGENT
else EventSource.USER
)
self.add_event(event, source)

def _reinitialize_from_file_store(self) -> None:
try:
events = self.file_store.list(f'sessions/{self.sid}/events')
Expand Down
39 changes: 39 additions & 0 deletions openhands/import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
import os

import requests

# ==============================INPUTS==============================
feedback_id = 'd9467879dfb19113adf4bd7ae67a5b83194a00b7d8c03eb7729706ebd3676a5e'
step_count = -1
# ==================================================================


data = {'feedback_id': feedback_id}
file_name = f'_{feedback_id}.json'
disable_import = False
if disable_import:
with open('event_history.json', 'w') as f:
json.dump([], f)
if os.path.exists(file_name):
with open(file_name, 'r') as f:
event_history = json.load(f)
else:
response = requests.post(
'https://show-od-event_history-3u9bw9tx.uc.gateway.dev/show-od-event_history',
json=data,
)
event_history = response.json()['trajectory']

if step_count != -1:
new_event_history = []
for i in event_history:
if i.get('log'):
step_count -= 1
print(i)
if step_count == 0:
break
new_event_history.append(i)

with open('event_history.json', 'w') as f:
json.dump(event_history, f, indent=4)

0 comments on commit 7d9314f

Please sign in to comment.