From a50ac9c788c48f7acbe22e87be27fa5c3ad6ebf9 Mon Sep 17 00:00:00 2001 From: amanape <83104063+amanape@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:04:26 +0200 Subject: [PATCH 1/4] Display runtime ID if available --- frontend/src/components/chat-interface.tsx | 33 ++++++++++++++++++++-- openhands/server/listen.py | 16 +++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/chat-interface.tsx b/frontend/src/components/chat-interface.tsx index 626166f46260..6ce40221fb23 100644 --- a/frontend/src/components/chat-interface.tsx +++ b/frontend/src/components/chat-interface.tsx @@ -21,14 +21,25 @@ import { ScrollToBottomButton } from "./scroll-to-bottom-button"; import { Suggestions } from "./suggestions"; import { SUGGESTIONS } from "#/utils/suggestions"; import BuildIt from "#/icons/build-it.svg?react"; -import { useWsClient } from "#/context/ws-client-provider"; +import { + useWsClient, + WsClientProviderStatus, +} from "#/context/ws-client-provider"; +import { request } from "#/services/api"; + +const getRuntimeId = async (): Promise<{ runtime_id: string }> => { + const response = await request("/api/config"); + const data = await response.json(); + + return data; +}; const isErrorMessage = ( message: Message | ErrorMessage, ): message is ErrorMessage => "error" in message; export function ChatInterface() { - const { send } = useWsClient(); + const { send, status } = useWsClient(); const dispatch = useDispatch(); const scrollRef = React.useRef(null); const { scrollDomToBottom, onChatBodyScroll, hitBottom } = @@ -42,6 +53,19 @@ export function ChatInterface() { >("positive"); const [feedbackModalIsOpen, setFeedbackModalIsOpen] = React.useState(false); const [messageToSend, setMessageToSend] = React.useState(null); + const [runtimeId, setRuntimeId] = React.useState("ABC123"); + + React.useEffect(() => { + if (status === WsClientProviderStatus.ACTIVE) { + try { + getRuntimeId().then(({ runtime_id }) => { + setRuntimeId(runtime_id); + }); + } catch (e) { + console.warn("Runtime ID not available in this environment"); + } + } + }, [status]); const handleSendMessage = async (content: string, files: File[]) => { posthog.capture("user_message_sent", { @@ -74,6 +98,11 @@ export function ChatInterface() { return (
+ {runtimeId && ( + + Runtime ID: {runtimeId} + + )} {messages.length === 0 && (
diff --git a/openhands/server/listen.py b/openhands/server/listen.py index d18bea277404..b93ae3d79d38 100644 --- a/openhands/server/listen.py +++ b/openhands/server/listen.py @@ -564,6 +564,22 @@ def sanitize_filename(filename): return filename +@app.get('/api/config') +async def get_remote_runtime_config(request: Request): + """Retrieve the remote runtime configuration. + + Currently, this is the runtime ID. + """ + try: + runtime: Runtime = request.state.conversation.runtime + runtime_id = runtime.runtime_id + + return JSONResponse(content={runtime_id}) + except Exception as e: + logger.error(e) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"error": "Something went wrong"}) + + @app.post('/api/upload-files') async def upload_file(request: Request, files: list[UploadFile]): """Upload a list of files to the workspace. From 189455661203cc8b52bedd2b9465018e1f8be248 Mon Sep 17 00:00:00 2001 From: amanape <83104063+amanape@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:19:04 +0200 Subject: [PATCH 2/4] Refactor --- frontend/src/api/open-hands.ts | 7 +++++++ frontend/src/components/chat-interface.tsx | 11 ++--------- openhands/server/listen.py | 9 ++++++--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/frontend/src/api/open-hands.ts b/frontend/src/api/open-hands.ts index b3ce52a566c5..072588ce476e 100644 --- a/frontend/src/api/open-hands.ts +++ b/frontend/src/api/open-hands.ts @@ -183,6 +183,13 @@ class OpenHands { static async getVSCodeUrl(): Promise { return request(`/api/vscode-url`, {}, false, false, 1); } + + static async getRuntimeId(): Promise<{ runtime_id: string }> { + const response = await request("/api/config"); + const data = await response.json(); + + return data; + } } export default OpenHands; diff --git a/frontend/src/components/chat-interface.tsx b/frontend/src/components/chat-interface.tsx index 6ce40221fb23..58312facc36b 100644 --- a/frontend/src/components/chat-interface.tsx +++ b/frontend/src/components/chat-interface.tsx @@ -25,14 +25,7 @@ import { useWsClient, WsClientProviderStatus, } from "#/context/ws-client-provider"; -import { request } from "#/services/api"; - -const getRuntimeId = async (): Promise<{ runtime_id: string }> => { - const response = await request("/api/config"); - const data = await response.json(); - - return data; -}; +import OpenHands from "#/api/open-hands"; const isErrorMessage = ( message: Message | ErrorMessage, @@ -58,7 +51,7 @@ export function ChatInterface() { React.useEffect(() => { if (status === WsClientProviderStatus.ACTIVE) { try { - getRuntimeId().then(({ runtime_id }) => { + OpenHands.getRuntimeId().then(({ runtime_id }) => { setRuntimeId(runtime_id); }); } catch (e) { diff --git a/openhands/server/listen.py b/openhands/server/listen.py index b93ae3d79d38..4f3be0ba2fc9 100644 --- a/openhands/server/listen.py +++ b/openhands/server/listen.py @@ -567,17 +567,20 @@ def sanitize_filename(filename): @app.get('/api/config') async def get_remote_runtime_config(request: Request): """Retrieve the remote runtime configuration. - + Currently, this is the runtime ID. """ try: runtime: Runtime = request.state.conversation.runtime - runtime_id = runtime.runtime_id + runtime_id = runtime.runtime_id # type: ignore return JSONResponse(content={runtime_id}) except Exception as e: logger.error(e) - return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"error": "Something went wrong"}) + return JSONResponse( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content={'error': 'Something went wrong'}, + ) @app.post('/api/upload-files') From 20c4fa70628c9dec12b49bbdc85f8283f0fd9d09 Mon Sep 17 00:00:00 2001 From: amanape <83104063+amanape@users.noreply.github.com> Date: Thu, 14 Nov 2024 20:48:21 +0200 Subject: [PATCH 3/4] Python does NOT support proprty shorthand syntax --- frontend/src/components/chat-interface.tsx | 2 +- openhands/server/listen.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/chat-interface.tsx b/frontend/src/components/chat-interface.tsx index 58312facc36b..8bcee365359e 100644 --- a/frontend/src/components/chat-interface.tsx +++ b/frontend/src/components/chat-interface.tsx @@ -46,7 +46,7 @@ export function ChatInterface() { >("positive"); const [feedbackModalIsOpen, setFeedbackModalIsOpen] = React.useState(false); const [messageToSend, setMessageToSend] = React.useState(null); - const [runtimeId, setRuntimeId] = React.useState("ABC123"); + const [runtimeId, setRuntimeId] = React.useState(null); React.useEffect(() => { if (status === WsClientProviderStatus.ACTIVE) { diff --git a/openhands/server/listen.py b/openhands/server/listen.py index 4f3be0ba2fc9..c1b178b9c2b7 100644 --- a/openhands/server/listen.py +++ b/openhands/server/listen.py @@ -11,6 +11,7 @@ from pathspec import PathSpec from pathspec.patterns import GitWildMatchPattern +from openhands.runtime.impl.remote.remote_runtime import RemoteRuntime from openhands.security.options import SecurityAnalyzers from openhands.server.data_models.feedback import FeedbackDataModel, store_feedback from openhands.server.github import ( @@ -571,10 +572,14 @@ async def get_remote_runtime_config(request: Request): Currently, this is the runtime ID. """ try: - runtime: Runtime = request.state.conversation.runtime - runtime_id = runtime.runtime_id # type: ignore - - return JSONResponse(content={runtime_id}) + runtime = request.state.conversation.runtime + if isinstance(runtime, RemoteRuntime): + return JSONResponse(content={'runtime_id': runtime.runtime_id}) + else: + return JSONResponse( + status_code=status.HTTP_404_NOT_FOUND, + content={'error': 'Runtime ID not available in this environment'}, + ) except Exception as e: logger.error(e) return JSONResponse( From 81f6754bd6aed98bbae9e1c96b461f94d91bae71 Mon Sep 17 00:00:00 2001 From: amanape <83104063+amanape@users.noreply.github.com> Date: Fri, 15 Nov 2024 10:13:54 +0200 Subject: [PATCH 4/4] Log instead of display in the UI --- frontend/src/components/chat-interface.tsx | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/chat-interface.tsx b/frontend/src/components/chat-interface.tsx index 78b47d558881..361ea744cade 100644 --- a/frontend/src/components/chat-interface.tsx +++ b/frontend/src/components/chat-interface.tsx @@ -47,13 +47,17 @@ export function ChatInterface() { >("positive"); const [feedbackModalIsOpen, setFeedbackModalIsOpen] = React.useState(false); const [messageToSend, setMessageToSend] = React.useState(null); - const [runtimeId, setRuntimeId] = React.useState(null); React.useEffect(() => { if (status === WsClientProviderStatus.ACTIVE) { try { OpenHands.getRuntimeId().then(({ runtime_id }) => { - setRuntimeId(runtime_id); + // eslint-disable-next-line no-console + console.log( + "Runtime ID: %c%s", + "background: #444; color: #ffeb3b; font-weight: bold; padding: 2px 4px; border-radius: 4px;", + runtime_id, + ); }); } catch (e) { console.warn("Runtime ID not available in this environment"); @@ -92,11 +96,6 @@ export function ChatInterface() { return (
- {runtimeId && ( - - Runtime ID: {runtimeId} - - )} {messages.length === 0 && (