Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Add button to export trajectory on chat panel #6378

Merged
merged 9 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

@enyst enyst Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to comment on a closed PR, I was just thinking: hidden=True will probably hides commands like clone the selected repo, the one performed automatically when the user selects it from the home page. Is that intended?

Cc: @li-boxuan

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't think that's intended. Actually I wasn't even aware of

hidden=True will probably hides commands like clone the selected repo, the one performed automatically when the user selects it from the home page

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't try it, I was thinking what does it take to restore the runtime environment, and with UI, the agent history is replayed over a workspace which... starts with a repo (if the user chose a repo). The initial command to clone the repo is 'hidden', as in hidden from the agent, so it's not in history. And it's not in traj.

So if I have a traj, I want to replay, I need to start with a repo that was cloned the same way 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that sounds legitimate! Created an issue to track this: #6749 will get to this once I got a chance

)
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}',
},
)
Loading