Skip to content

Commit

Permalink
Move GitHub Token export to backend (#6153)
Browse files Browse the repository at this point in the history
Co-authored-by: openhands <[email protected]>
  • Loading branch information
rbren and openhands-agent authored Jan 8, 2025
1 parent 386e04a commit c411a29
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
22 changes: 1 addition & 21 deletions frontend/src/routes/_oh.app/hooks/use-handle-runtime-active.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import React from "react";
import toast from "react-hot-toast";
import { useDispatch, useSelector } from "react-redux";
import { useAuth } from "#/context/auth-context";
import { useWsClient } from "#/context/ws-client-provider";
import { getGitHubTokenCommand } from "#/services/terminal-service";
import { setImportedProjectZip } from "#/state/initial-query-slice";
import { RootState } from "#/store";
import { base64ToBlob } from "#/utils/base64-to-blob";
import { useUploadFiles } from "../../../hooks/mutation/use-upload-files";
import { useGitHubUser } from "../../../hooks/query/use-github-user";
import { isGitHubErrorReponse } from "#/api/github-axios-instance";

import { RUNTIME_INACTIVE_STATES } from "#/types/agent-state";

export const useHandleRuntimeActive = () => {
const { gitHubToken } = useAuth();
const { send } = useWsClient();

const dispatch = useDispatch();

const { data: user } = useGitHubUser();
const { mutate: uploadFiles } = useUploadFiles();
const { curAgentState } = useSelector((state: RootState) => state.agent);

Expand All @@ -28,11 +20,6 @@ export const useHandleRuntimeActive = () => {
(state: RootState) => state.initialQuery,
);

const userId = React.useMemo(() => {
if (user && !isGitHubErrorReponse(user)) return user.id;
return null;
}, [user]);

const handleUploadFiles = (zip: string) => {
const blob = base64ToBlob(zip);
const file = new File([blob], "imported-project.zip", {
Expand All @@ -49,13 +36,6 @@ export const useHandleRuntimeActive = () => {
dispatch(setImportedProjectZip(null));
};

React.useEffect(() => {
if (runtimeActive && userId && gitHubToken) {
// Export if the user valid, this could happen mid-session so it is handled here
send(getGitHubTokenCommand(gitHubToken));
}
}, [userId, gitHubToken, runtimeActive]);

React.useEffect(() => {
if (runtimeActive && importedProjectZip) {
handleUploadFiles(importedProjectZip);
Expand Down
6 changes: 0 additions & 6 deletions frontend/src/services/terminal-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@ export function getTerminalCommand(command: string, hidden: boolean = false) {
const event = { action: ActionType.RUN, args: { command, hidden } };
return event;
}

export function getGitHubTokenCommand(gitHubToken: string) {
const command = `export GITHUB_TOKEN=${gitHubToken}`;
const event = getTerminalCommand(command, true);
return event;
}
3 changes: 2 additions & 1 deletion openhands/runtime/impl/remote/remote_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ def _resume_runtime(self):
timeout=60,
):
pass
self._wait_until_alive()
self.setup_initial_env()
self.log('debug', 'Runtime resumed.')

def _parse_runtime_response(self, response: requests.Response):
Expand Down Expand Up @@ -388,7 +390,6 @@ def _send_action_server_request(self, method, url, **kwargs):
elif e.response.status_code == 503:
self.log('warning', 'Runtime appears to be paused. Resuming...')
self._resume_runtime()
self._wait_until_alive()
return super()._send_action_server_request(method, url, **kwargs)
else:
raise e
8 changes: 8 additions & 0 deletions openhands/server/session/agent_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,21 @@ async def _create_runtime(

logger.debug(f'Initializing runtime `{runtime_name}` now...')
runtime_cls = get_runtime_cls(runtime_name)
env_vars = (
{
'GITHUB_TOKEN': github_token,
}
if github_token
else None
)
self.runtime = runtime_cls(
config=config,
event_stream=self.event_stream,
sid=self.sid,
plugins=agent.sandbox_plugins,
status_callback=self._status_callback,
headless_mode=False,
env_vars=env_vars,
)

# FIXME: this sleep is a terrible hack.
Expand Down

0 comments on commit c411a29

Please sign in to comment.