Skip to content

Commit

Permalink
refactor: use conversation ID in websocket path
Browse files Browse the repository at this point in the history
- Remove session initialization from WsClientProvider
- Add conversation ID to websocket path
- Update backend to validate conversation ID in websocket path
- Store conversation ID in socket session data
  • Loading branch information
openhands-agent committed Dec 9, 2024
1 parent c26fb9f commit d6940f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
26 changes: 4 additions & 22 deletions frontend/src/context/ws-client-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface WsClientProviderProps {
ghToken: string | null;
selectedRepository: string | null;
settings: Settings | null;
conversationId: string | null;
}

export function WsClientProvider({
Expand All @@ -50,6 +51,7 @@ export function WsClientProvider({
ghToken,
selectedRepository,
settings,

Check failure on line 53 in frontend/src/context/ws-client-provider.tsx

View workflow job for this annotation

GitHub Actions / Lint frontend

'settings' is defined but never used
conversationId,
children,
}: React.PropsWithChildren<WsClientProviderProps>) {
const sioRef = React.useRef<Socket | null>(null);
Expand All @@ -74,28 +76,8 @@ export function WsClientProvider({
sioRef.current.emit("oh_action", event);
}

async function handleConnect() {
function handleConnect() {
setStatus(WsClientProviderStatus.OPENING);

try {
const lastEvent = lastEventRef.current;
const { token: newToken } = await OpenHands.initSession({
token,
githubToken: ghToken,
selectedRepository,
args: settings || undefined,
latestEventId: lastEvent?.id as number | undefined,
});

// Store the new token for future use
tokenRef.current = newToken;
// Extract conversation ID from the token (it's a JWT)
const payload = JSON.parse(atob(newToken.split('.')[1]));
setConversationId(payload.sid);
} catch (error) {
EventLogger.error("Failed to initialize session", error);
setStatus(WsClientProviderStatus.ERROR);
}
}

function handleMessage(event: Record<string, unknown>) {
Expand Down Expand Up @@ -157,7 +139,7 @@ export function WsClientProvider({

const baseUrl =
import.meta.env.VITE_BACKEND_BASE_URL || window?.location.host;
sio = io(baseUrl, {
sio = io(`${baseUrl}/conversation/${conversationId}`, {
transports: ["websocket"],
});
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/routes/_oh.app/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ConversationProvider } from "#/context/conversation-context";
function App() {
const { token, gitHubToken } = useAuth();
const { settings } = useUserPrefs();
const { conversationId } = useConversation();

const dispatch = useDispatch();
useConversationConfig();
Expand Down Expand Up @@ -67,6 +68,7 @@ function App() {
ghToken={gitHubToken}
selectedRepository={selectedRepository}
settings={settings}
conversationId={conversationId}
>
<ConversationProvider>
<EventHandler>
Expand Down
12 changes: 12 additions & 0 deletions openhands/server/listen_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
@sio.event
async def connect(connection_id: str, environ):
logger.info(f'sio:connect: {connection_id}')
# Extract conversation ID from the URL path
path = environ.get('HTTP_URI', '').split('?')[0]
if not path.startswith('/conversation/'):
await sio.emit('oh_event', {'error': 'Invalid path', 'error_code': 400}, to=connection_id)
return False
conversation_id = path.split('/conversation/')[1]
if not conversation_id:
await sio.emit('oh_event', {'error': 'Missing conversation ID', 'error_code': 400}, to=connection_id)
return False

# Store the conversation ID in the socket data
await sio.save_session(connection_id, {'conversation_id': conversation_id})


@sio.event
Expand Down

0 comments on commit d6940f7

Please sign in to comment.