Skip to content

Commit

Permalink
Merge branch 'main' into enyst/test_view_depth
Browse files Browse the repository at this point in the history
  • Loading branch information
enyst authored Jan 21, 2025
2 parents 3b064ab + 7f57dbe commit e055bb5
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 5 deletions.
3 changes: 3 additions & 0 deletions evaluation/benchmarks/miniwob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Please follow instruction [here](../../README.md#setup) to setup your local deve

## Test if your environment works

Follow the instructions here https://miniwob.farama.org/content/getting_started/ & https://miniwob.farama.org/content/viewing/
to set up MiniWoB server in your local environment at http://localhost:8080/miniwob/

Access with browser the above MiniWoB URLs and see if they load correctly.

## Run Evaluation
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/api/open-hands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AuthenticateResponse,
Conversation,
ResultSet,
GetTrajectoryResponse,
} from "./open-hands.types";
import { openHands } from "./open-hands-axios";
import { ApiSettings } from "#/services/settings";
Expand Down Expand Up @@ -354,6 +355,15 @@ class OpenHands {

return response.data.items;
}

static async getTrajectory(
conversationId: string,
): Promise<GetTrajectoryResponse> {
const { data } = await openHands.get<GetTrajectoryResponse>(
`/api/conversations/${conversationId}/trajectory`,
);
return data;
}
}

export default OpenHands;
5 changes: 5 additions & 0 deletions frontend/src/api/open-hands.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export interface GetVSCodeUrlResponse {
error?: string;
}

export interface GetTrajectoryResponse {
trajectory: unknown[] | null;
error?: string;
}

export interface AuthenticateResponse {
message?: string;
error?: string;
Expand Down
29 changes: 29 additions & 0 deletions frontend/src/components/features/chat/chat-interface.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { useDispatch, useSelector } from "react-redux";
import toast from "react-hot-toast";
import React from "react";
import posthog from "posthog-js";
import { useParams } from "react-router";
import { convertImageToBase64 } from "#/utils/convert-image-to-base-64";
import { FeedbackActions } from "../feedback/feedback-actions";
import { ExportActions } from "../export/export-actions";
import { createChatMessage } from "#/services/chat-service";
import { InteractiveChatBox } from "./interactive-chat-box";
import { addUserMessage } from "#/state/chat-slice";
Expand All @@ -19,6 +22,8 @@ import { ActionSuggestions } from "./action-suggestions";
import { ContinueButton } from "#/components/shared/buttons/continue-button";
import { ScrollToBottomButton } from "#/components/shared/buttons/scroll-to-bottom-button";
import { LoadingSpinner } from "#/components/shared/loading-spinner";
import { useGetTrajectory } from "#/hooks/mutation/use-get-trajectory";
import { downloadTrajectory } from "#/utils/download-files";

function getEntryPoint(
hasRepository: boolean | null,
Expand Down Expand Up @@ -47,6 +52,8 @@ export function ChatInterface() {
const { selectedRepository, importedProjectZip } = useSelector(
(state: RootState) => state.initialQuery,
);
const params = useParams();
const { mutate: getTrajectory } = useGetTrajectory();

const handleSendMessage = async (content: string, files: File[]) => {
if (messages.length === 0) {
Expand Down Expand Up @@ -90,6 +97,25 @@ export function ChatInterface() {
setFeedbackPolarity(polarity);
};

const onClickExportTrajectoryButton = () => {
if (!params.conversationId) {
toast.error("ConversationId unknown, cannot download trajectory");
return;
}

getTrajectory(params.conversationId, {
onSuccess: async (data) => {
await downloadTrajectory(
params.conversationId ?? "unknown",
data.trajectory,
);
},
onError: (error) => {
toast.error(error.message);
},
});
};

const isWaitingForUserInput =
curAgentState === AgentState.AWAITING_USER_INPUT ||
curAgentState === AgentState.FINISHED;
Expand Down Expand Up @@ -137,6 +163,9 @@ export function ChatInterface() {
onClickShareFeedbackActionButton("negative")
}
/>
<ExportActions
onExportTrajectory={() => onClickExportTrajectoryButton()}
/>

<div className="absolute left-1/2 transform -translate-x-1/2 bottom-0">
{messages.length > 2 &&
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/components/features/export/export-actions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ExportIcon from "#/icons/export.svg?react";
import { ExportActionButton } from "#/components/shared/buttons/export-action-button";

interface ExportActionsProps {
onExportTrajectory: () => void;
}

export function ExportActions({ onExportTrajectory }: ExportActionsProps) {
return (
<div data-testid="export-actions" className="flex gap-1">
<ExportActionButton
onClick={onExportTrajectory}
icon={<ExportIcon width={15} height={15} />}
/>
</div>
);
}
17 changes: 17 additions & 0 deletions frontend/src/components/shared/buttons/export-action-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
interface ExportActionButtonProps {
onClick: () => void;
icon: React.ReactNode;
}

export function ExportActionButton({ onClick, icon }: ExportActionButtonProps) {
return (
<button
type="button"
onClick={onClick}
className="button-base p-1 hover:bg-neutral-500"
title="Export trajectory"
>
{icon}
</button>
);
}
7 changes: 7 additions & 0 deletions frontend/src/hooks/mutation/use-get-trajectory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useMutation } from "@tanstack/react-query";
import OpenHands from "#/api/open-hands";

export const useGetTrajectory = () =>
useMutation({
mutationFn: (cid: string) => OpenHands.getTrajectory(cid),
});
5 changes: 5 additions & 0 deletions frontend/src/icons/export.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions frontend/src/types/file-system.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ interface FileSystemDirectoryHandle {
): Promise<FileSystemFileHandle>;
}

interface SaveFilePickerOptions {
suggestedName?: string;
types?: Array<{
description?: string;
accept: Record<string, string[]>;
}>;
excludeAcceptAllOption?: boolean;
}

interface Window {
showDirectoryPicker(): Promise<FileSystemDirectoryHandle>;
showSaveFilePicker(
options?: SaveFilePickerOptions,
): Promise<FileSystemFileHandle>;
}
40 changes: 40 additions & 0 deletions frontend/src/utils/download-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ function isFileSystemAccessSupported(): boolean {
return "showDirectoryPicker" in window;
}

/**
* Checks if the Save File Picker API is supported
*/
function isSaveFilePickerSupported(): boolean {
return "showSaveFilePicker" in window;
}

/**
* Creates subdirectories and returns the final directory handle
*/
Expand Down Expand Up @@ -162,6 +169,39 @@ async function processBatch(
};
}

export async function downloadTrajectory(
conversationId: string,
data: unknown[] | null,
): Promise<void> {
try {
if (!isSaveFilePickerSupported()) {
throw new Error(
"Your browser doesn't support downloading folders. Please use Chrome, Edge, or another browser that supports the File System Access API.",
);
}
const options = {
suggestedName: `trajectory-${conversationId}.json`,
types: [
{
description: "JSON File",
accept: {
"application/json": [".json"],
},
},
],
};

const handle = await window.showSaveFilePicker(options);
const writable = await handle.createWritable();
await writable.write(JSON.stringify(data, null, 2));
await writable.close();
} catch (error) {
throw new Error(
`Failed to download file: ${error instanceof Error ? error.message : String(error)}`,
);
}
}

/**
* Downloads files from the workspace one by one
* @param initialPath Initial path to start downloading from. If not provided, downloads from root
Expand Down
2 changes: 2 additions & 0 deletions openhands/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from openhands.server.routes.public import app as public_api_router
from openhands.server.routes.security import app as security_api_router
from openhands.server.routes.settings import app as settings_router
from openhands.server.routes.trajectory import app as trajectory_router
from openhands.server.shared import openhands_config, session_manager
from openhands.utils.import_utils import get_impl

Expand Down Expand Up @@ -69,6 +70,7 @@ async def health():
app.include_router(manage_conversation_api_router)
app.include_router(settings_router)
app.include_router(github_api_router)
app.include_router(trajectory_router)

AttachConversationMiddlewareImpl = get_impl(
AttachConversationMiddleware, openhands_config.attach_conversation_middleware_path
Expand Down
40 changes: 40 additions & 0 deletions openhands/server/routes/trajectory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse

from openhands.core.logger import openhands_logger as logger
from openhands.events.serialization import event_to_trajectory
from openhands.events.stream import AsyncEventStreamWrapper

app = APIRouter(prefix='/api/conversations/{conversation_id}')


@app.get('/trajectory')
async def get_trajectory(request: Request):
"""Get trajectory.
This function retrieves the current trajectory and returns it.
Args:
request (Request): The incoming request object.
Returns:
JSONResponse: A JSON response containing the trajectory as a list of
events.
"""
try:
async_stream = AsyncEventStreamWrapper(
request.state.conversation.event_stream, filter_hidden=True
)
trajectory = []
async for event in async_stream:
trajectory.append(event_to_trajectory(event))
return JSONResponse(status_code=200, content={'trajectory': trajectory})
except Exception as e:
logger.error(f'Error getting trajectory: {e}', exc_info=True)
return JSONResponse(
status_code=500,
content={
'trajectory': None,
'error': f'Error getting trajectory: {e}',
},
)
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ llama-index-embeddings-voyageai = "*"
[tool.poetry.group.dev.dependencies]
ruff = "0.9.2"
mypy = "1.14.1"
pre-commit = "4.0.1"
pre-commit = "4.1.0"
build = "*"

[tool.poetry.group.test.dependencies]
Expand Down

0 comments on commit e055bb5

Please sign in to comment.