Compare commits

..

17 Commits

Author SHA1 Message Date
openhands
7a2e7d6270 Revert changes to pyproject.toml 2025-03-20 17:01:50 +00:00
openhands
f5c58adaf7 Refactor: Clean up WebSocket client code and fix logging 2025-03-20 16:49:38 +00:00
Xingyao Wang
c6cb025afe Merge branch 'main' into allow-message-during-client-loading 2025-03-20 12:41:16 -04:00
openhands
cb8214676e Simplify frontend logic by removing unnecessary backend readiness handling 2025-03-19 15:21:48 +00:00
openhands
fdfb7308b8 Move message queueing from frontend to backend 2025-03-19 15:06:16 +00:00
Xingyao Wang
4785de91b0 Merge branch 'main' into allow-message-during-client-loading 2025-03-19 10:59:12 -04:00
Xingyao Wang
effd2b7d06 Merge branch 'main' into allow-message-during-client-loading 2025-03-05 13:41:43 -05:00
openhands
608dd8f2c2 Fix linting issues 2025-03-04 01:59:59 +00:00
openhands
6d0c03509e Simplify backend ready detection and message sending 2025-03-03 22:39:10 +00:00
openhands
3e1070bbe9 Add enhanced logging to debug WebSocket connection and message queuing 2025-03-03 22:36:29 +00:00
openhands
2045350720 Fix message queuing by waiting for backend ready signal 2025-03-03 22:27:54 +00:00
openhands
5f83d4cf9a Add info method to EventLogger 2025-03-03 22:25:43 +00:00
openhands
d5a996a9e1 Update i18n declaration file 2025-02-28 02:27:47 +00:00
openhands
b0d38bbeb8 Merge main into allow-message-during-client-loading and resolve conflicts 2025-02-28 02:26:10 +00:00
openhands
ed50b3ee8f Fix agent response by sending agent state change event 2025-02-27 17:31:17 +00:00
openhands
4e5ed36213 Fix linting issue with queueMessage function order 2025-02-27 16:36:05 +00:00
openhands
9060452af6 Allow sending messages while client is connecting 2025-02-27 16:21:30 +00:00
145 changed files with 2036 additions and 3514 deletions

View File

@@ -33,7 +33,6 @@ Frontend:
- Testing:
- Run tests: `npm run test`
- To run specific tests: `npm run test -- -t "TestName"`
- Our test framework is vitest
- Building:
- Build for production: `npm run build`
- Environment Variables:

View File

@@ -1,5 +0,0 @@
#! /bin/bash
echo "Setting up the environment..."
python -m pip install pre-commit

View File

@@ -56,10 +56,6 @@ docker run -it --rm --pull=always \
docker.all-hands.dev/all-hands-ai/openhands:0.29
```
> [!WARNING]
> On a public network? See our [Hardened Docker Installation](https://docs.all-hands.dev/modules/usage/runtimes/docker#hardened-docker-installation) guide
> to secure your deployment by restricting network binding and implementing additional security measures.
You'll find OpenHands running at [http://localhost:3000](http://localhost:3000)!
Finally, you'll need a model provider and API key.

View File

@@ -42,10 +42,6 @@ workspace_base = "./workspace"
# If it's a folder, the session id will be used as the file name
#save_trajectory_path="./trajectories"
# Whether to save screenshots in the trajectory
# The screenshots are encoded and can make trajectory json files very large
#save_screenshots_in_trajectory = false
# Path to replay a trajectory, must be a file path
# If provided, trajectory will be loaded and replayed before the
# agent responds to any user instruction

View File

@@ -1,24 +0,0 @@
# Repository Customization
You can customize how OpenHands works with your repository by creating a
`.openhands` directory at the root level.
## Microagents
You can use microagents to extend the OpenHands prompts with information
about your project and how you want OpenHands to work. See
[Repository Microagents](../prompting/microagents-repo) for more information.
## Setup Script
You can add `.openhands/setup.sh`, which will be run every time OpenHands begins
working with your repository. This is a good place to install dependencies, set
environment variables, etc.
For example:
```bash
#!/bin/bash
export MY_ENV_VAR="my value"
sudo apt-get update
sudo apt-get install -y lsof
cd frontend && npm install ; cd ..
```

View File

@@ -1,60 +0,0 @@
# OpenHands Feature Overview
![overview](https://www.all-hands.dev/assets/product/product-slide-1.webp)
## 1. Workspace
The Workspace feature provides a comprehensive development environment with the following key capabilities:
- File Explorer: Browse, view, and manage project files and directories
- Project Management: Import, create, and navigate between different projects
- Integrated Development Tools: Seamless integration with various development workflows
- File Operations:
* View file contents
* Create new files and folders
* Upload and download files
* Basic file manipulation
## 2. Jupyter Notebook
The Jupyter Notebook feature offers an interactive coding and data analysis environment:
- Interactive Code Cells: Execute Python code in a cell-based interface
- Input and Output Tracking: Maintain a history of code inputs and their corresponding outputs
- Persistent Session: Preserve code execution context between cells
- Supports various Python operations and data analysis tasks
- Real-time code execution and result visualization
## 3. Browser (Beta)
The Browser feature provides web interaction capabilities:
- Web Page Navigation: Open and browse websites within the application
- Screenshot Capture: Automatically generate screenshots of web pages
- Interaction Tools:
* Click elements
* Fill out forms
* Scroll pages
* Navigate through web content
- Supports 15 different browser interaction functions
## 4. Terminal
The Terminal feature offers a command-line interface within the application:
- Execute Shell Commands: Run bash and system commands
- Command History: Track and recall previous commands
- Environment Interaction: Interact directly with the system's command line
- Support for various programming and system administration tasks
## 5. Chat / AI Conversation
The Chat interface provides an AI-powered conversational experience:
- Interactive AI Assistant: Engage in natural language conversations
- Context-Aware Responses: AI understands and responds to development-related queries
- Action Suggestions: Provides actionable recommendations for tasks
- Conversation Management: Create, delete, and manage different conversation threads
## 6. App (Beta)
The main application interface combines all these features:
- Integrated Workspace: Seamless integration of workspace, browser, terminal, and AI chat
- Configurable Layout: Customize the arrangement of different feature panels
- State Management: Maintain context and state across different features
- Security and Privacy Controls: Manage application settings and permissions
### Additional Notes
- The application is currently in beta, with ongoing improvements and feature additions
- Supports various development workflows and AI-assisted coding
- Designed to enhance developer productivity through integrated tools and AI assistance

View File

@@ -10,7 +10,7 @@ OpenHands uses LiteLLM to make calls to Google's chat models. You can find their
When running OpenHands, you'll need to set the following in the OpenHands UI through the Settings:
- `LLM Provider` to `Gemini`
- `LLM Model` to the model you will be using.
If the model is not in the list, toggle `Advanced` options, and enter it in `Custom Model` (e.g. gemini/<model-name> like `gemini/gemini-2.0-flash`).
If the model is not in the list, toggle `Advanced` options, and enter it in `Custom Model` (e.g. gemini/<model-name> like `gemini/gemini-1.5-pro`).
- `API Key` to your Gemini API key
## VertexAI - Google Cloud Platform Configs

View File

@@ -1,24 +0,0 @@
# Runtime Configuration
A Runtime is an environment where the OpenHands agent can edit files and run
commands.
By default, OpenHands uses a Docker-based runtime, running on your local computer.
This means you only have to pay for the LLM you're using, and your code is only ever sent to the LLM.
We also support "remote" runtimes, which are typically managed by third-parties.
They can make setup a bit simpler and more scalable, especially
if you're running many OpenHands conversations in parallel (e.g. to do evaluation).
Additionally, we provide a "local" runtime that runs directly on your machine without Docker,
which can be useful in controlled environments like CI pipelines.
## Available Runtimes
OpenHands supports several different runtime environments:
- [Docker Runtime](./runtimes/docker.md) - The default runtime that uses Docker containers for isolation (recommended for most users)
- [OpenHands Remote Runtime](./runtimes/remote.md) - Cloud-based runtime for parallel execution (beta)
- [Modal Runtime](./runtimes/modal.md) - Runtime provided by our partners at Modal
- [Daytona Runtime](./runtimes/daytona.md) - Runtime provided by Daytona
- [Local Runtime](./runtimes/local.md) - Direct execution on your local machine without Docker

View File

@@ -1,8 +1,176 @@
---
title: Runtime Configuration
slug: /usage/runtimes
---
# Runtime Configuration
import { Redirect } from '@docusaurus/router';
A Runtime is an environment where the OpenHands agent can edit files and run
commands.
<Redirect to="/modules/usage/runtimes-index" />
By default, OpenHands uses a Docker-based runtime, running on your local computer.
This means you only have to pay for the LLM you're using, and your code is only ever sent to the LLM.
We also support "remote" runtimes, which are typically managed by third-parties.
They can make setup a bit simpler and more scalable, especially
if you're running many OpenHands conversations in parallel (e.g. to do evaluation).
Additionally, we provide a "local" runtime that runs directly on your machine without Docker,
which can be useful in controlled environments like CI pipelines.
## Docker Runtime
This is the default Runtime that's used when you start OpenHands. You might notice
some flags being passed to `docker run` that make this possible:
```
docker run # ...
-e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:0.29-nikolaik \
-v /var/run/docker.sock:/var/run/docker.sock \
# ...
```
The `SANDBOX_RUNTIME_CONTAINER_IMAGE` from nikolaik is a pre-built runtime image
that contains our Runtime server, as well as some basic utilities for Python and NodeJS.
You can also [build your own runtime image](how-to/custom-sandbox-guide).
### Connecting to Your filesystem
One useful feature here is the ability to connect to your local filesystem. To mount your filesystem into the runtime:
1. Set `WORKSPACE_BASE`:
```bash
export WORKSPACE_BASE=/path/to/your/code
# Linux and Mac Example
# export WORKSPACE_BASE=$HOME/OpenHands
# Will set $WORKSPACE_BASE to /home/<username>/OpenHands
#
# WSL on Windows Example
# export WORKSPACE_BASE=/mnt/c/dev/OpenHands
# Will set $WORKSPACE_BASE to C:\dev\OpenHands
```
2. Add the following options to the `docker run` command:
```bash
docker run # ...
-e SANDBOX_USER_ID=$(id -u) \
-e WORKSPACE_MOUNT_PATH=$WORKSPACE_BASE \
-v $WORKSPACE_BASE:/opt/workspace_base \
# ...
```
Be careful! There's nothing stopping the OpenHands agent from deleting or modifying
any files that are mounted into its workspace.
This setup can cause some issues with file permissions (hence the `SANDBOX_USER_ID` variable)
but seems to work well on most systems.
## OpenHands Remote Runtime
OpenHands Remote Runtime is currently in beta (read [here](https://runtime.all-hands.dev/) for more details), it allows you to launch runtimes in parallel in the cloud.
Fill out [this form](https://docs.google.com/forms/d/e/1FAIpQLSckVz_JFwg2_mOxNZjCtr7aoBFI2Mwdan3f75J_TrdMS1JV2g/viewform) to apply if you want to try this out!
NOTE: This runtime is specifically designed for agent evaluation purposes only through [OpenHands evaluation harness](https://github.com/All-Hands-AI/OpenHands/tree/main/evaluation). It should not be used to launch production OpenHands applications.
## Modal Runtime
Our partners at [Modal](https://modal.com/) have also provided a runtime for OpenHands.
To use the Modal Runtime, create an account, and then [create an API key.](https://modal.com/settings)
You'll then need to set the following environment variables when starting OpenHands:
```bash
docker run # ...
-e RUNTIME=modal \
-e MODAL_API_TOKEN_ID="your-id" \
-e MODAL_API_TOKEN_SECRET="your-secret" \
```
## Daytona Runtime
Another option is using [Daytona](https://www.daytona.io/) as a runtime provider:
### Step 1: Retrieve Your Daytona API Key
1. Visit the [Daytona Dashboard](https://app.daytona.io/dashboard/keys).
2. Click **"Create Key"**.
3. Enter a name for your key and confirm the creation.
4. Once the key is generated, copy it.
### Step 2: Set Your API Key as an Environment Variable
Run the following command in your terminal, replacing `<your-api-key>` with the actual key you copied:
```bash
export DAYTONA_API_KEY="<your-api-key>"
```
This step ensures that OpenHands can authenticate with the Daytona platform when it runs.
### Step 3: Run OpenHands Locally Using Docker
To start the latest version of OpenHands on your machine, execute the following command in your terminal:
```bash
bash -i <(curl -sL https://get.daytona.io/openhands)
```
#### What This Command Does:
- Downloads the latest OpenHands release script.
- Runs the script in an interactive Bash session.
- Automatically pulls and runs the OpenHands container using Docker.
Once executed, OpenHands should be running locally and ready for use.
For more details and manual initialization, view the entire [README.md](https://github.com/All-Hands-AI/OpenHands/blob/main/openhands/runtime/impl/daytona/README.md)
## Local Runtime
The Local Runtime allows the OpenHands agent to execute actions directly on your local machine without using Docker. This runtime is primarily intended for controlled environments like CI pipelines or testing scenarios where Docker is not available.
:::caution
**Security Warning**: The Local Runtime runs without any sandbox isolation. The agent can directly access and modify files on your machine. Only use this runtime in controlled environments or when you fully understand the security implications.
:::
### Prerequisites
Before using the Local Runtime, ensure you have the following dependencies installed:
1. You have followed the [Development setup instructions](https://github.com/All-Hands-AI/OpenHands/blob/main/Development.md).
2. tmux is available on your system.
### Configuration
To use the Local Runtime, besides required configurations like the model, API key, you'll need to set the following options via environment variables or the [config.toml file](https://github.com/All-Hands-AI/OpenHands/blob/main/config.template.toml) when starting OpenHands:
- Via environment variables:
```bash
# Required
export RUNTIME=local
# Optional but recommended
export WORKSPACE_BASE=/path/to/your/workspace
```
- Via `config.toml`:
```toml
[core]
runtime = "local"
workspace_base = "/path/to/your/workspace"
```
If `WORKSPACE_BASE` is not set, the runtime will create a temporary directory for the agent to work in.
### Example Usage
Here's an example of how to start OpenHands with the Local Runtime in Headless Mode:
```bash
# Set the runtime type to local
export RUNTIME=local
# Optionally set a workspace directory
export WORKSPACE_BASE=/path/to/your/project
# Start OpenHands
poetry run python -m openhands.core.main -t "write a bash script that prints hi"
```
### Use Cases
The Local Runtime is particularly useful for:
- CI/CD pipelines where Docker is not available.
- Testing and development of OpenHands itself.
- Environments where container usage is restricted.
- Scenarios where direct file system access is required.

View File

@@ -1,32 +0,0 @@
# Daytona Runtime
You can use [Daytona](https://www.daytona.io/) as a runtime provider:
## Step 1: Retrieve Your Daytona API Key
1. Visit the [Daytona Dashboard](https://app.daytona.io/dashboard/keys).
2. Click **"Create Key"**.
3. Enter a name for your key and confirm the creation.
4. Once the key is generated, copy it.
## Step 2: Set Your API Key as an Environment Variable
Run the following command in your terminal, replacing `<your-api-key>` with the actual key you copied:
```bash
export DAYTONA_API_KEY="<your-api-key>"
```
This step ensures that OpenHands can authenticate with the Daytona platform when it runs.
## Step 3: Run OpenHands Locally Using Docker
To start the latest version of OpenHands on your machine, execute the following command in your terminal:
```bash
bash -i <(curl -sL https://get.daytona.io/openhands)
```
### What This Command Does:
- Downloads the latest OpenHands release script.
- Runs the script in an interactive Bash session.
- Automatically pulls and runs the OpenHands container using Docker.
Once executed, OpenHands should be running locally and ready for use.
For more details and manual initialization, view the entire [README.md](https://github.com/All-Hands-AI/OpenHands/blob/main/openhands/runtime/impl/daytona/README.md)

View File

@@ -1,88 +0,0 @@
# Docker Runtime
This is the default Runtime that's used when you start OpenHands.
## Image
The `SANDBOX_RUNTIME_CONTAINER_IMAGE` from nikolaik is a pre-built runtime image
that contains our Runtime server, as well as some basic utilities for Python and NodeJS.
You can also [build your own runtime image](../how-to/custom-sandbox-guide).
## Connecting to Your filesystem
One useful feature here is the ability to connect to your local filesystem. To mount your filesystem into the runtime:
1. Set `WORKSPACE_BASE`:
```bash
export WORKSPACE_BASE=/path/to/your/code
# Linux and Mac Example
# export WORKSPACE_BASE=$HOME/OpenHands
# Will set $WORKSPACE_BASE to /home/<username>/OpenHands
#
# WSL on Windows Example
# export WORKSPACE_BASE=/mnt/c/dev/OpenHands
# Will set $WORKSPACE_BASE to C:\dev\OpenHands
```
2. Add the following options to the `docker run` command:
```bash
docker run # ...
-e SANDBOX_USER_ID=$(id -u) \
-e WORKSPACE_MOUNT_PATH=$WORKSPACE_BASE \
-v $WORKSPACE_BASE:/opt/workspace_base \
# ...
```
Be careful! There's nothing stopping the OpenHands agent from deleting or modifying
any files that are mounted into its workspace.
This setup can cause some issues with file permissions (hence the `SANDBOX_USER_ID` variable)
but seems to work well on most systems.
## Hardened Docker Installation
When deploying OpenHands in environments where security is a priority, you should consider implementing a hardened Docker configuration. This section provides recommendations for securing your OpenHands Docker deployment beyond the default configuration.
### Security Considerations
The default Docker configuration in the README is designed for ease of use on a local development machine. If you're running on a public network (e.g. airport WiFi),
you should implement additional security measures.
### Network Binding Security
By default, OpenHands binds to all network interfaces (`0.0.0.0`), which can expose your instance to all networks the host is connected to. For a more secure setup:
1. **Restrict Network Binding**:
Use the `runtime_binding_address` configuration to restrict which network interfaces OpenHands listens on:
```bash
docker run # ...
-e SANDBOX_RUNTIME_BINDING_ADDRESS=127.0.0.1 \
# ...
```
This configuration ensures OpenHands only listens on the loopback interface (`127.0.0.1`), making it accessible only from the local machine.
2. **Secure Port Binding**:
Modify the `-p` flag to bind only to localhost instead of all interfaces:
```bash
docker run # ... \
-p 127.0.0.1:3000:3000 \
```
This ensures that the OpenHands web interface is only accessible from the local machine, not from other machines on the network.
### Network Isolation
Use Docker's network features to isolate OpenHands:
```bash
# Create an isolated network
docker network create openhands-network
# Run OpenHands in the isolated network
docker run # ... \
--network openhands-network \
```

View File

@@ -1,62 +0,0 @@
# Local Runtime
The Local Runtime allows the OpenHands agent to execute actions directly on your local machine without using Docker. This runtime is primarily intended for controlled environments like CI pipelines or testing scenarios where Docker is not available.
:::caution
**Security Warning**: The Local Runtime runs without any sandbox isolation. The agent can directly access and modify files on your machine. Only use this runtime in controlled environments or when you fully understand the security implications.
:::
## Prerequisites
Before using the Local Runtime, ensure that:
1. You have followed the [Development setup instructions](https://github.com/All-Hands-AI/OpenHands/blob/main/Development.md).
2. tmux is available on your system.
## Configuration
To use the Local Runtime, besides required configurations like the model, API key, you'll need to set the following options via environment variables or the [config.toml file](https://github.com/All-Hands-AI/OpenHands/blob/main/config.template.toml) when starting OpenHands:
- Via environment variables:
```bash
# Required
export RUNTIME=local
# Optional but recommended
export WORKSPACE_BASE=/path/to/your/workspace
```
- Via `config.toml`:
```toml
[core]
runtime = "local"
workspace_base = "/path/to/your/workspace"
```
If `WORKSPACE_BASE` is not set, the runtime will create a temporary directory for the agent to work in.
## Example Usage
Here's an example of how to start OpenHands with the Local Runtime in Headless Mode:
```bash
# Set the runtime type to local
export RUNTIME=local
# Optionally set a workspace directory
export WORKSPACE_BASE=/path/to/your/project
# Start OpenHands
poetry run python -m openhands.core.main -t "write a bash script that prints hi"
```
## Use Cases
The Local Runtime is particularly useful for:
- CI/CD pipelines where Docker is not available.
- Testing and development of OpenHands itself.
- Environments where container usage is restricted.
- Scenarios where direct file system access is required.

View File

@@ -1,13 +0,0 @@
# Modal Runtime
Our partners at [Modal](https://modal.com/) have provided a runtime for OpenHands.
To use the Modal Runtime, create an account, and then [create an API key.](https://modal.com/settings)
You'll then need to set the following environment variables when starting OpenHands:
```bash
docker run # ...
-e RUNTIME=modal \
-e MODAL_API_TOKEN_ID="your-id" \
-e MODAL_API_TOKEN_SECRET="your-secret" \
```

View File

@@ -1,6 +0,0 @@
# OpenHands Remote Runtime
OpenHands Remote Runtime is currently in beta (read [here](https://runtime.all-hands.dev/) for more details), it allows you to launch runtimes in parallel in the cloud.
Fill out [this form](https://docs.google.com/forms/d/e/1FAIpQLSckVz_JFwg2_mOxNZjCtr7aoBFI2Mwdan3f75J_TrdMS1JV2g/viewform) to apply if you want to try this out!
NOTE: This runtime is specifically designed for agent evaluation purposes only through [OpenHands evaluation harness](https://github.com/All-Hands-AI/OpenHands/tree/main/evaluation). It should not be used to launch production OpenHands applications.

View File

@@ -13,11 +13,6 @@ const sidebars: SidebarsConfig = {
label: 'Getting Started',
id: 'usage/getting-started',
},
{
type: 'doc',
label: 'Key Features',
id: 'usage/key-features',
},
{
type: 'category',
label: 'Prompting',
@@ -50,17 +45,6 @@ const sidebars: SidebarsConfig = {
},
],
},
{
type: 'category',
label: 'Customization',
items: [
{
type: 'doc',
label: 'Repository Customization',
id: 'usage/customization/repository',
},
],
},
{
type: 'category',
label: 'Usage Methods',
@@ -156,40 +140,9 @@ const sidebars: SidebarsConfig = {
],
},
{
type: 'category',
type: 'doc',
label: 'Runtime Configuration',
items: [
{
type: 'doc',
label: 'Overview',
id: 'usage/runtimes-index',
},
{
type: 'doc',
label: 'Docker Runtime',
id: 'usage/runtimes/docker',
},
{
type: 'doc',
label: 'Remote Runtime',
id: 'usage/runtimes/remote',
},
{
type: 'doc',
label: 'Modal Runtime',
id: 'usage/runtimes/modal',
},
{
type: 'doc',
label: 'Daytona Runtime',
id: 'usage/runtimes/daytona',
},
{
type: 'doc',
label: 'Local Runtime',
id: 'usage/runtimes/local',
},
],
id: 'usage/runtimes',
},
{
type: 'doc',

View File

@@ -573,7 +573,6 @@ def get_default_sandbox_config_for_eval() -> SandboxConfig:
# large enough timeout, since some testcases take very long to run
timeout=300,
api_key=os.environ.get('ALLHANDS_API_KEY', None),
runtime_startup_env_vars={'NO_CHANGE_TIMEOUT_SECONDS': '30'},
remote_runtime_api_url=os.environ.get('SANDBOX_REMOTE_RUNTIME_API_URL'),
keep_runtime_alive=False,
remote_runtime_init_timeout=3600,

View File

@@ -50,29 +50,27 @@ This will start the application in development mode. Open [http://localhost:3001
### Running the Application with the Actual Backend (Production Mode)
To run the application with the actual backend:
There are two ways to run the application with the actual backend:
```sh
# Build the application from the root directory
make build
# Start the application
make run
make start
```
Or to run backend and frontend seperately.
OR
```sh
# Start the backend from the root directory
make start-backend
# Serve the frontend
make start-frontend or
cd frontend && npm start -- --port 3001
```
# Build the frontend
cd frontend && npm run build
Start frontend with Mock Service Worker (MSW), see testing for more info.
```sh
npm run dev:mock or npm run dev:mock:saas
# Serve the frontend
npm start -- --port 3001
```
### Environment Variables
@@ -123,113 +121,12 @@ components
## Testing
### Testing Framework and Tools
We use `Vitest` for testing. To run the tests, run the following command:
We use the following testing tools:
- **Test Runner**: Vitest
- **Rendering**: React Testing Library
- **User Interactions**: @testing-library/user-event
- **API Mocking**: [Mock Service Worker (MSW)](https://mswjs.io/)
- **Code Coverage**: Vitest with V8 coverage
### Running Tests
To run all tests:
```sh
npm run test
```
To run tests with coverage:
```sh
npm run test:coverage
```
### Testing Best Practices
1. **Component Testing**
- Test components in isolation
- Use our custom [`renderWithProviders()`](https://github.com/All-Hands-AI/OpenHands/blob/ce26f1c6d3feec3eedf36f823dee732b5a61e517/frontend/test-utils.tsx#L56-L85) that wraps the components we want to test in our providers. It is especially useful for components that use Redux
- Use `render()` from React Testing Library to render components
- Prefer querying elements by role, label, or test ID over CSS selectors
- Test both rendering and interaction scenarios
2. **User Event Simulation**
- Use `userEvent` for simulating realistic user interactions
- Test keyboard events, clicks, typing, and other user actions
- Handle edge cases like disabled states, empty inputs, etc.
3. **Mocking**
- We test components that make network requests by mocking those requests with Mock Service Worker (MSW)
- Use `vi.fn()` to create mock functions for callbacks and event handlers
- Mock external dependencies and API calls (more info)[https://mswjs.io/docs/getting-started]
- Verify mock function calls using `.toHaveBeenCalledWith()`, `.toHaveBeenCalledTimes()`
4. **Accessibility Testing**
- Use `toBeInTheDocument()` to check element presence
- Test keyboard navigation and screen reader compatibility
- Verify correct ARIA attributes and roles
5. **State and Prop Testing**
- Test component behavior with different prop combinations
- Verify state changes and conditional rendering
- Test error states and loading scenarios
6. **Internationalization (i18n) Testing**
- Test translation keys and placeholders
- Verify text rendering across different languages
Example Test Structure:
```typescript
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi } from "vitest";
describe("ComponentName", () => {
it("should render correctly", () => {
render(<Component />);
expect(screen.getByRole("button")).toBeInTheDocument();
});
it("should handle user interactions", async () => {
const mockCallback = vi.fn();
const user = userEvent.setup();
render(<Component onClick={mockCallback} />);
const button = screen.getByRole("button");
await user.click(button);
expect(mockCallback).toHaveBeenCalledOnce();
});
});
```
### Example Tests in the Codebase
For real-world examples of testing, check out these test files:
1. **Chat Input Component Test**:
[`__tests__/components/chat/chat-input.test.tsx`](https://github.com/All-Hands-AI/OpenHands/blob/main/frontend/__tests__/components/chat/chat-input.test.tsx)
- Demonstrates comprehensive testing of a complex input component
- Covers various scenarios like submission, disabled states, and user interactions
2. **File Explorer Component Test**:
[`__tests__/components/file-explorer/file-explorer.test.tsx`](https://github.com/All-Hands-AI/OpenHands/blob/main/frontend/__tests__/components/file-explorer/file-explorer.test.tsx)
- Shows testing of a more complex component with multiple interactions
- Illustrates testing of nested components and state management
### Test Coverage
- Aim for high test coverage, especially for critical components
- Focus on testing different scenarios and edge cases
- Use code coverage reports to identify untested code paths
### Continuous Integration
Tests are automatically run during:
- Pre-commit hooks
- Pull request checks
- CI/CD pipeline
## Contributing
Please read the [CONTRIBUTING.md](../CONTRIBUTING.md) file for details on our code of conduct, and the process for submitting pull requests to us.

View File

@@ -1,9 +1,8 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import { screen } from "@testing-library/react";
import { renderWithProviders } from "test-utils";
import { createRoutesStub } from "react-router";
import { ExpandableMessage } from "#/components/features/chat/expandable-message";
import OpenHands from "#/api/open-hands";
import { vi } from "vitest"
vi.mock("react-i18next", async () => {
const actual = await vi.importActual("react-i18next");
@@ -49,7 +48,7 @@ describe("ExpandableMessage", () => {
id="OBSERVATION_MESSAGE$RUN"
message="Command executed successfully"
type="action"
success
success={true}
/>,
);
const element = screen.getByText("OBSERVATION_MESSAGE$RUN");
@@ -94,31 +93,4 @@ describe("ExpandableMessage", () => {
expect(container).toHaveClass("border-neutral-300");
expect(screen.queryByTestId("status-icon")).not.toBeInTheDocument();
});
it("should render the out of credits message when the user is out of credits", async () => {
const getConfigSpy = vi.spyOn(OpenHands, "getConfig");
// @ts-expect-error - We only care about the APP_MODE and FEATURE_FLAGS fields
getConfigSpy.mockResolvedValue({
APP_MODE: "saas",
FEATURE_FLAGS: {
ENABLE_BILLING: true,
HIDE_LLM_SETTINGS: false,
},
});
const RouterStub = createRoutesStub([
{
Component: () => (
<ExpandableMessage
id="STATUS$ERROR_LLM_OUT_OF_CREDITS"
message=""
type=""
/>
),
path: "/",
},
]);
renderWithProviders(<RouterStub />);
await screen.findByTestId("out-of-credits");
});
});

View File

@@ -4,6 +4,7 @@ import { render, screen, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { AnalyticsConsentFormModal } from "#/components/features/analytics/analytics-consent-form-modal";
import OpenHands from "#/api/open-hands";
import { SettingsProvider } from "#/context/settings-context";
import { AuthProvider } from "#/context/auth-context";
describe("AnalyticsConsentFormModal", () => {
@@ -16,7 +17,7 @@ describe("AnalyticsConsentFormModal", () => {
wrapper: ({ children }) => (
<AuthProvider>
<QueryClientProvider client={new QueryClient()}>
{children}
<SettingsProvider>{children}</SettingsProvider>
</QueryClientProvider>
</AuthProvider>
),

View File

@@ -1,4 +1,4 @@
import { screen, within } from "@testing-library/react";
import { render, screen, within } from "@testing-library/react";
import {
afterAll,
afterEach,
@@ -10,7 +10,6 @@ import {
vi,
} from "vitest";
import userEvent from "@testing-library/user-event";
import { renderWithProviders } from "test-utils";
import { formatTimeDelta } from "#/utils/format-time-delta";
import { ConversationCard } from "#/components/features/conversation-panel/conversation-card";
import { clickOnEditButton } from "./utils";
@@ -19,13 +18,10 @@ describe("ConversationCard", () => {
const onClick = vi.fn();
const onDelete = vi.fn();
const onChangeTitle = vi.fn();
const onDownloadWorkspace = vi.fn();
beforeAll(() => {
vi.stubGlobal("window", {
open: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
});
vi.stubGlobal("window", { open: vi.fn() });
});
afterEach(() => {
@@ -37,7 +33,7 @@ describe("ConversationCard", () => {
});
it("should render the conversation card", () => {
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
onChangeTitle={onChangeTitle}
@@ -56,7 +52,7 @@ describe("ConversationCard", () => {
});
it("should render the selectedRepository if available", () => {
const { rerender } = renderWithProviders(
const { rerender } = render(
<ConversationCard
onDelete={onDelete}
onChangeTitle={onChangeTitle}
@@ -87,7 +83,7 @@ describe("ConversationCard", () => {
it("should toggle a context menu when clicking the ellipsis button", async () => {
const user = userEvent.setup();
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
onChangeTitle={onChangeTitle}
@@ -112,7 +108,7 @@ describe("ConversationCard", () => {
it("should call onDelete when the delete button is clicked", async () => {
const user = userEvent.setup();
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
isActive
@@ -136,7 +132,7 @@ describe("ConversationCard", () => {
test("clicking the selectedRepository should not trigger the onClick handler", async () => {
const user = userEvent.setup();
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
isActive
@@ -157,7 +153,7 @@ describe("ConversationCard", () => {
test("conversation title should call onChangeTitle when changed and blurred", async () => {
const user = userEvent.setup();
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
isActive
@@ -186,7 +182,7 @@ describe("ConversationCard", () => {
it("should reset title and not call onChangeTitle when the title is empty", async () => {
const user = userEvent.setup();
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
isActive
@@ -210,7 +206,7 @@ describe("ConversationCard", () => {
test("clicking the title should trigger the onClick handler", async () => {
const user = userEvent.setup();
renderWithProviders(
render(
<ConversationCard
onClick={onClick}
onDelete={onDelete}
@@ -230,7 +226,7 @@ describe("ConversationCard", () => {
test("clicking the title should not trigger the onClick handler if edit mode", async () => {
const user = userEvent.setup();
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
isActive
@@ -251,7 +247,7 @@ describe("ConversationCard", () => {
test("clicking the delete button should not trigger the onClick handler", async () => {
const user = userEvent.setup();
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
isActive
@@ -273,13 +269,14 @@ describe("ConversationCard", () => {
expect(onClick).not.toHaveBeenCalled();
});
it("should show display cost button only when showDisplayCostOption is true", async () => {
it("should call onDownloadWorkspace when the download button is clicked", async () => {
const user = userEvent.setup();
const { rerender } = renderWithProviders(
render(
<ConversationCard
onClick={onClick}
onDelete={onDelete}
onChangeTitle={onChangeTitle}
isActive
onDownloadWorkspace={onDownloadWorkspace}
title="Conversation 1"
selectedRepository={null}
lastUpdatedAt="2021-10-01T12:00:00Z"
@@ -289,64 +286,17 @@ describe("ConversationCard", () => {
const ellipsisButton = screen.getByTestId("ellipsis-button");
await user.click(ellipsisButton);
// Wait for context menu to appear
const menu = await screen.findByTestId("context-menu");
expect(
within(menu).queryByTestId("display-cost-button"),
).not.toBeInTheDocument();
// Close menu
await user.click(ellipsisButton);
rerender(
<ConversationCard
onDelete={onDelete}
onChangeTitle={onChangeTitle}
showDisplayCostOption
isActive
title="Conversation 1"
selectedRepository={null}
lastUpdatedAt="2021-10-01T12:00:00Z"
/>,
);
// Open menu again
await user.click(ellipsisButton);
// Wait for context menu to appear and check for display cost button
const newMenu = await screen.findByTestId("context-menu");
within(newMenu).getByTestId("display-cost-button");
});
it("should show metrics modal when clicking the display cost button", async () => {
const user = userEvent.setup();
renderWithProviders(
<ConversationCard
onDelete={onDelete}
isActive
onChangeTitle={onChangeTitle}
title="Conversation 1"
selectedRepository={null}
lastUpdatedAt="2021-10-01T12:00:00Z"
showDisplayCostOption
/>,
);
const ellipsisButton = screen.getByTestId("ellipsis-button");
await user.click(ellipsisButton);
const menu = screen.getByTestId("context-menu");
const displayCostButton = within(menu).getByTestId("display-cost-button");
const downloadButton = within(menu).getByTestId("download-button");
await user.click(displayCostButton);
await user.click(downloadButton);
// Verify if metrics modal is displayed by checking for the modal content
expect(screen.getByText("Metrics Information")).toBeInTheDocument();
expect(onDownloadWorkspace).toHaveBeenCalled();
});
it("should not display the edit or delete options if the handler is not provided", async () => {
const user = userEvent.setup();
const { rerender } = renderWithProviders(
const { rerender } = render(
<ConversationCard
onClick={onClick}
onChangeTitle={onChangeTitle}
@@ -359,9 +309,8 @@ describe("ConversationCard", () => {
const ellipsisButton = screen.getByTestId("ellipsis-button");
await user.click(ellipsisButton);
const menu = await screen.findByTestId("context-menu");
expect(within(menu).queryByTestId("edit-button")).toBeInTheDocument();
expect(within(menu).queryByTestId("delete-button")).not.toBeInTheDocument();
expect(screen.queryByTestId("edit-button")).toBeInTheDocument();
expect(screen.queryByTestId("delete-button")).not.toBeInTheDocument();
// toggle to hide the context menu
await user.click(ellipsisButton);
@@ -377,19 +326,18 @@ describe("ConversationCard", () => {
);
await user.click(ellipsisButton);
const newMenu = await screen.findByTestId("context-menu");
expect(
within(newMenu).queryByTestId("edit-button"),
).not.toBeInTheDocument();
expect(within(newMenu).queryByTestId("delete-button")).toBeInTheDocument();
expect(screen.queryByTestId("edit-button")).not.toBeInTheDocument();
expect(screen.queryByTestId("delete-button")).toBeInTheDocument();
});
it("should not render the ellipsis button if there are no actions", () => {
const { rerender } = renderWithProviders(
const { rerender } = render(
<ConversationCard
onClick={onClick}
onDelete={onDelete}
onChangeTitle={onChangeTitle}
onDownloadWorkspace={onDownloadWorkspace}
title="Conversation 1"
selectedRepository={null}
lastUpdatedAt="2021-10-01T12:00:00Z"
@@ -402,6 +350,7 @@ describe("ConversationCard", () => {
<ConversationCard
onClick={onClick}
onDelete={onDelete}
onDownloadWorkspace={onDownloadWorkspace}
title="Conversation 1"
selectedRepository={null}
lastUpdatedAt="2021-10-01T12:00:00Z"
@@ -410,6 +359,18 @@ describe("ConversationCard", () => {
expect(screen.getByTestId("ellipsis-button")).toBeInTheDocument();
rerender(
<ConversationCard
onClick={onClick}
onDownloadWorkspace={onDownloadWorkspace}
title="Conversation 1"
selectedRepository={null}
lastUpdatedAt="2021-10-01T12:00:00Z"
/>,
);
expect(screen.queryByTestId("ellipsis-button")).toBeInTheDocument();
rerender(
<ConversationCard
onClick={onClick}
@@ -424,7 +385,7 @@ describe("ConversationCard", () => {
describe("state indicator", () => {
it("should render the 'STOPPED' indicator by default", () => {
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
isActive
@@ -439,7 +400,7 @@ describe("ConversationCard", () => {
});
it("should render the other indicators when provided", () => {
renderWithProviders(
render(
<ConversationCard
onDelete={onDelete}
isActive

View File

@@ -1,4 +1,4 @@
import { screen, waitFor, within } from "@testing-library/react";
import { render, screen, waitFor, within } from "@testing-library/react";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import {
QueryClientProvider,
@@ -13,7 +13,6 @@ import OpenHands from "#/api/open-hands";
import { AuthProvider } from "#/context/auth-context";
import { clickOnEditButton } from "./utils";
import { queryClientConfig } from "#/query-client-config";
import { renderWithProviders } from "test-utils";
describe("ConversationPanel", () => {
const onCloseMock = vi.fn();
@@ -25,8 +24,14 @@ describe("ConversationPanel", () => {
]);
const renderConversationPanel = (config?: QueryClientConfig) =>
renderWithProviders(<RouterStub />, {
preloadedState: {}
render(<RouterStub />, {
wrapper: ({ children }) => (
<AuthProvider>
<QueryClientProvider client={new QueryClient(config)}>
{children}
</QueryClientProvider>
</AuthProvider>
),
});
const { endSessionMock } = vi.hoisted(() => ({
@@ -48,38 +53,9 @@ describe("ConversationPanel", () => {
}));
});
const mockConversations = [
{
conversation_id: "1",
title: "Conversation 1",
selected_repository: null,
last_updated_at: "2021-10-01T12:00:00Z",
created_at: "2021-10-01T12:00:00Z",
status: "STOPPED" as const,
},
{
conversation_id: "2",
title: "Conversation 2",
selected_repository: null,
last_updated_at: "2021-10-02T12:00:00Z",
created_at: "2021-10-02T12:00:00Z",
status: "STOPPED" as const,
},
{
conversation_id: "3",
title: "Conversation 3",
selected_repository: null,
last_updated_at: "2021-10-03T12:00:00Z",
created_at: "2021-10-03T12:00:00Z",
status: "STOPPED" as const,
},
];
beforeEach(() => {
vi.clearAllMocks();
vi.restoreAllMocks();
// Setup default mock for getUserConversations
vi.spyOn(OpenHands, "getUserConversations").mockResolvedValue([...mockConversations]);
});
it("should render the conversations", async () => {
@@ -107,7 +83,13 @@ describe("ConversationPanel", () => {
new Error("Failed to fetch conversations"),
);
renderConversationPanel();
renderConversationPanel({
defaultOptions: {
queries: {
retry: false,
},
},
});
const error = await screen.findByText("Failed to fetch conversations");
expect(error).toBeInTheDocument();
@@ -142,20 +124,6 @@ describe("ConversationPanel", () => {
it("should call endSession after deleting a conversation that is the current session", async () => {
const user = userEvent.setup();
const mockData = [...mockConversations];
const getUserConversationsSpy = vi.spyOn(OpenHands, "getUserConversations");
getUserConversationsSpy.mockImplementation(async () => mockData);
const deleteUserConversationSpy = vi.spyOn(OpenHands, "deleteUserConversation");
deleteUserConversationSpy.mockImplementation(async (id: string) => {
const index = mockData.findIndex(conv => conv.conversation_id === id);
if (index !== -1) {
mockData.splice(index, 1);
}
// Wait for React Query to update its cache
await new Promise(resolve => setTimeout(resolve, 0));
});
renderConversationPanel();
let cards = await screen.findAllByTestId("conversation-card");
@@ -172,60 +140,18 @@ describe("ConversationPanel", () => {
expect(screen.queryByText("Confirm")).not.toBeInTheDocument();
// Wait for the cards to update with a longer timeout
await waitFor(() => {
const updatedCards = screen.getAllByTestId("conversation-card");
expect(updatedCards).toHaveLength(2);
}, { timeout: 2000 });
// Ensure the conversation is deleted
cards = await screen.findAllByTestId("conversation-card");
expect(cards).toHaveLength(2);
expect(endSessionMock).toHaveBeenCalledOnce();
});
it("should delete a conversation", async () => {
const user = userEvent.setup();
const mockData = [
{
conversation_id: "1",
title: "Conversation 1",
selected_repository: null,
last_updated_at: "2021-10-01T12:00:00Z",
created_at: "2021-10-01T12:00:00Z",
status: "STOPPED" as const,
},
{
conversation_id: "2",
title: "Conversation 2",
selected_repository: null,
last_updated_at: "2021-10-02T12:00:00Z",
created_at: "2021-10-02T12:00:00Z",
status: "STOPPED" as const,
},
{
conversation_id: "3",
title: "Conversation 3",
selected_repository: null,
last_updated_at: "2021-10-03T12:00:00Z",
created_at: "2021-10-03T12:00:00Z",
status: "STOPPED" as const,
},
];
const getUserConversationsSpy = vi.spyOn(OpenHands, "getUserConversations");
getUserConversationsSpy.mockImplementation(async () => mockData);
const deleteUserConversationSpy = vi.spyOn(OpenHands, "deleteUserConversation");
deleteUserConversationSpy.mockImplementation(async (id: string) => {
const index = mockData.findIndex(conv => conv.conversation_id === id);
if (index !== -1) {
mockData.splice(index, 1);
}
});
renderConversationPanel();
let cards = await screen.findAllByTestId("conversation-card");
expect(cards).toHaveLength(3);
const ellipsisButton = within(cards[0]).getByTestId("ellipsis-button");
await user.click(ellipsisButton);
const deleteButton = screen.getByTestId("delete-button");
@@ -239,11 +165,9 @@ describe("ConversationPanel", () => {
expect(screen.queryByText("Confirm")).not.toBeInTheDocument();
// Wait for the cards to update
await waitFor(() => {
const updatedCards = screen.getAllByTestId("conversation-card");
expect(updatedCards).toHaveLength(2);
});
// Ensure the conversation is deleted
cards = await screen.findAllByTestId("conversation-card");
expect(cards).toHaveLength(1);
});
it("should rename a conversation", async () => {
@@ -265,7 +189,7 @@ describe("ConversationPanel", () => {
await user.tab();
// Ensure the conversation is renamed
expect(updateUserConversationSpy).toHaveBeenCalledWith("1", {
expect(updateUserConversationSpy).toHaveBeenCalledWith("3", {
title: "Conversation 1 Renamed",
});
});
@@ -290,7 +214,7 @@ describe("ConversationPanel", () => {
// Ensure the conversation is not renamed
expect(updateUserConversationSpy).not.toHaveBeenCalled();
await clickOnEditButton(user, card);
await clickOnEditButton(user);
await user.type(title, "Conversation 1");
await user.click(title);
@@ -305,21 +229,17 @@ describe("ConversationPanel", () => {
});
it("should call onClose after clicking a card", async () => {
const user = userEvent.setup();
renderConversationPanel();
const cards = await screen.findAllByTestId("conversation-card");
const firstCard = cards[1];
await user.click(firstCard);
await userEvent.click(firstCard);
expect(onCloseMock).toHaveBeenCalledOnce();
});
it("should refetch data on rerenders", async () => {
const user = userEvent.setup();
const getUserConversationsSpy = vi.spyOn(OpenHands, "getUserConversations");
getUserConversationsSpy.mockResolvedValue([...mockConversations]);
// We need to simulate the toggling of the component to test the refetching
function PanelWithToggle() {
const [isOpen, setIsOpen] = React.useState(true);
return (
@@ -339,23 +259,25 @@ describe("ConversationPanel", () => {
},
]);
renderWithProviders(<MyRouterStub />, {
preloadedState: {}
const getUserConversationsSpy = vi.spyOn(OpenHands, "getUserConversations");
render(<MyRouterStub />, {
wrapper: ({ children }) => (
<AuthProvider>
<QueryClientProvider client={new QueryClient(queryClientConfig)}>
{children}
</QueryClientProvider>
</AuthProvider>
),
});
const toggleButton = screen.getByText("Toggle");
await waitFor(() => expect(getUserConversationsSpy).toHaveBeenCalledOnce());
// Initial render
const cards = await screen.findAllByTestId("conversation-card");
expect(cards).toHaveLength(3);
const button = screen.getByText("Toggle");
await userEvent.click(button);
await userEvent.click(button);
// Toggle off
await user.click(toggleButton);
expect(screen.queryByTestId("conversation-card")).not.toBeInTheDocument();
// Toggle on
await user.click(toggleButton);
const newCards = await screen.findAllByTestId("conversation-card");
expect(newCards).toHaveLength(3);
await waitFor(() =>
expect(getUserConversationsSpy).toHaveBeenCalledTimes(2),
);
});
});

View File

@@ -30,10 +30,6 @@ describe("GitHubRepositorySelector", () => {
APP_SLUG: "openhands",
GITHUB_CLIENT_ID: "test-client-id",
POSTHOG_CLIENT_KEY: "test-posthog-key",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderWithProviders(

View File

@@ -4,8 +4,10 @@ import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, test, vi } from "vitest";
import OpenHands from "#/api/open-hands";
import { PaymentForm } from "#/components/features/payment/payment-form";
import * as featureFlags from "#/utils/feature-flags";
describe("PaymentForm", () => {
const billingSettingsSpy = vi.spyOn(featureFlags, "BILLING_SETTINGS");
const getBalanceSpy = vi.spyOn(OpenHands, "getBalance");
const createCheckoutSessionSpy = vi.spyOn(OpenHands, "createCheckoutSession");
const getConfigSpy = vi.spyOn(OpenHands, "getConfig");
@@ -20,16 +22,13 @@ describe("PaymentForm", () => {
});
beforeEach(() => {
// useBalance hook will return the balance only if the APP_MODE is "saas" and the billing feature is enabled
// useBalance hook will return the balance only if the APP_MODE is "saas"
getConfigSpy.mockResolvedValue({
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: true,
HIDE_LLM_SETTINGS: false,
},
});
billingSettingsSpy.mockReturnValue(true);
});
afterEach(() => {

View File

@@ -59,7 +59,7 @@ describe("TrajectoryActions", () => {
expect(onNegativeFeedback).toHaveBeenCalled();
});
it("should call onExportTrajectory when export button is clicked", async () => {
it("should call onExportTrajectory when negative feedback is clicked", async () => {
renderWithProviders(
<TrajectoryActions
onPositiveFeedback={onPositiveFeedback}

View File

@@ -1,12 +1,9 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import * as ChatSlice from "#/state/chat-slice";
import {
updateStatusWhenErrorMessagePresent,
WsClientProvider,
useWsClient,
} from "#/context/ws-client-provider";
import React from "react";
describe("Propagate error message", () => {
it("should do nothing when no message was passed from server", () => {
@@ -44,59 +41,3 @@ describe("Propagate error message", () => {
});
});
});
// Create a mock for socket.io-client
const mockEmit = vi.fn();
const mockOn = vi.fn();
const mockOff = vi.fn();
const mockDisconnect = vi.fn();
vi.mock("socket.io-client", () => {
return {
io: vi.fn(() => ({
emit: mockEmit,
on: mockOn,
off: mockOff,
disconnect: mockDisconnect,
io: {
opts: {
query: {},
},
},
})),
};
});
// Mock component to test the hook
const TestComponent = () => {
const { send } = useWsClient();
React.useEffect(() => {
// Send a test event
send({ type: "test_event" });
}, [send]);
return <div>Test Component</div>;
};
describe("WsClientProvider", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("should emit oh_user_action event when send is called", async () => {
const { getByText } = render(
<WsClientProvider conversationId="test-conversation-id">
<TestComponent />
</WsClientProvider>
);
// Assert
expect(getByText("Test Component")).toBeInTheDocument();
// Wait for the emit call to happen (useEffect needs time to run)
await waitFor(() => {
expect(mockEmit).toHaveBeenCalledWith("oh_user_action", { type: "test_event" });
}, { timeout: 1000 });
});
});

View File

@@ -3,18 +3,15 @@ import { describe, expect, it, vi } from "vitest";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import OpenHands from "#/api/open-hands";
import { useSaveSettings } from "#/hooks/mutation/use-save-settings";
import { AuthProvider } from "#/context/auth-context";
describe("useSaveSettings", () => {
it("should send an empty string for llm_api_key if an empty string is passed, otherwise undefined", async () => {
const saveSettingsSpy = vi.spyOn(OpenHands, "saveSettings");
const { result } = renderHook(() => useSaveSettings(), {
wrapper: ({ children }) => (
<AuthProvider>
<QueryClientProvider client={new QueryClient()}>
{children}
</QueryClientProvider>
</AuthProvider>
<QueryClientProvider client={new QueryClient()}>
{children}
</QueryClientProvider>
),
});

View File

@@ -1,24 +0,0 @@
import { useInitialQuery } from "#/hooks/query/use-initial-query";
import { vi, describe, it } from "vitest";
// Mock the query-redux-bridge
vi.mock("#/utils/query-redux-bridge", () => ({
getQueryReduxBridge: vi.fn(() => ({
getReduxSliceState: vi.fn(() => ({
files: [],
initialPrompt: null,
selectedRepository: null,
})),
})),
}));
// Skip tests for now due to JSX parsing issues
describe("useInitialQuery", () => {
it("should return initial query state", () => {
// Test implementation
});
it("should update initial query state", async () => {
// Test implementation
});
});

View File

@@ -1,23 +0,0 @@
import { useMetrics } from "#/hooks/query/use-metrics";
import { vi, describe, it } from "vitest";
// Mock the query-redux-bridge
vi.mock("#/utils/query-redux-bridge", () => ({
getQueryReduxBridge: vi.fn(() => ({
getReduxSliceState: vi.fn(() => ({
cost: null,
usage: null,
})),
})),
}));
// Skip tests for now due to JSX parsing issues
describe("useMetrics", () => {
it("should return initial metrics state", () => {
// Test implementation
});
it("should update metrics state", async () => {
// Test implementation
});
});

View File

@@ -1,44 +1,20 @@
import { describe, it, expect, vi } from "vitest";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook } from "@testing-library/react";
import React from "react";
import { useInitialQuery } from "../src/hooks/query/use-initial-query";
// Mock the query-redux-bridge
vi.mock("../src/utils/query-redux-bridge", () => ({
getQueryReduxBridge: vi.fn(() => ({
getReduxSliceState: vi.fn(() => ({
files: [],
initialPrompt: null,
selectedRepository: null,
})),
})),
}));
// Create a wrapper with QueryClientProvider
const createWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};
import { describe, it, expect } from "vitest";
import store from "../src/store";
import {
setInitialPrompt,
clearInitialPrompt,
} from "../src/state/initial-query-slice";
describe("Initial Query Behavior", () => {
it("should have initial state", () => {
const { result } = renderHook(() => useInitialQuery(), {
wrapper: createWrapper(),
});
// Verify initial state
expect(result.current.files).toEqual([]);
expect(result.current.initialPrompt).toBeNull();
expect(result.current.selectedRepository).toBeNull();
it("should clear initial query when clearInitialPrompt is dispatched", () => {
// Set up initial query in the store
store.dispatch(setInitialPrompt("test query"));
expect(store.getState().initialQuery.initialPrompt).toBe("test query");
// Clear the initial query
store.dispatch(clearInitialPrompt());
// Verify initial query is cleared
expect(store.getState().initialQuery.initialPrompt).toBeNull();
});
});

View File

@@ -5,8 +5,6 @@ import { screen, waitFor } from "@testing-library/react";
import App from "#/routes/_oh.app/route";
import OpenHands from "#/api/open-hands";
import * as CustomToast from "#/utils/custom-toast-handlers";
import { QueryClient } from "@tanstack/react-query";
import { initQueryReduxBridge } from "#/utils/query-redux-bridge";
describe("App", () => {
const errorToastSpy = vi.spyOn(CustomToast, "displayErrorToast");
@@ -20,10 +18,6 @@ describe("App", () => {
}));
beforeAll(() => {
// Initialize the QueryReduxBridge for tests
const queryClient = new QueryClient();
initQueryReduxBridge(queryClient);
vi.mock("#/hooks/use-end-session", () => ({
useEndSession: vi.fn(() => endSessionMock),
}));

View File

@@ -55,8 +55,7 @@ describe("frontend/routes/_oh", () => {
});
});
// FIXME: This test fails when it shouldn't be, please investigate
it.skip("should render and capture the user's consent if oss mode", async () => {
it("should render and capture the user's consent if oss mode", async () => {
const user = userEvent.setup();
const getConfigSpy = vi.spyOn(OpenHands, "getConfig");
const getSettingsSpy = vi.spyOn(OpenHands, "getSettings");
@@ -69,10 +68,6 @@ describe("frontend/routes/_oh", () => {
APP_MODE: "oss",
GITHUB_CLIENT_ID: "test-id",
POSTHOG_CLIENT_KEY: "test-key",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
// @ts-expect-error - We only care about the user_consents_to_analytics field
@@ -104,10 +99,6 @@ describe("frontend/routes/_oh", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "test-id",
POSTHOG_CLIENT_KEY: "test-key",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderWithProviders(<RouteStub />);

View File

@@ -8,6 +8,7 @@ import MainApp from "#/routes/_oh/route";
import SettingsScreen from "#/routes/settings";
import Home from "#/routes/_oh._index/route";
import OpenHands from "#/api/open-hands";
import * as FeatureFlags from "#/utils/feature-flags";
const createAxiosNotFoundErrorObject = () =>
new AxiosError(
@@ -51,8 +52,6 @@ afterEach(() => {
});
describe("Home Screen", () => {
const getConfigSpy = vi.spyOn(OpenHands, "getConfig");
it("should render the home screen", () => {
renderWithProviders(<RouterStub initialEntries={["/"]} />);
});
@@ -69,14 +68,6 @@ describe("Home Screen", () => {
});
it("should navigate to the settings when pressing 'Connect to GitHub' if the user isn't authenticated", async () => {
// @ts-expect-error - we only need APP_MODE for this test
getConfigSpy.mockResolvedValue({
APP_MODE: "oss",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
const user = userEvent.setup();
renderWithProviders(<RouterStub initialEntries={["/"]} />);
@@ -128,14 +119,10 @@ describe("Settings 404", () => {
});
it("should not open the settings modal if GET /settings fails but is SaaS mode", async () => {
// TODO: Remove HIDE_LLM_SETTINGS check once released
vi.spyOn(FeatureFlags, "HIDE_LLM_SETTINGS").mockReturnValue(true);
// @ts-expect-error - we only need APP_MODE for this test
getConfigSpy.mockResolvedValue({
APP_MODE: "saas",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
getConfigSpy.mockResolvedValue({ APP_MODE: "saas" });
const error = createAxiosNotFoundErrorObject();
getSettingsSpy.mockRejectedValue(error);
@@ -159,19 +146,14 @@ describe("Setup Payment modal", () => {
// @ts-expect-error - we only need the APP_MODE for this test
getConfigSpy.mockResolvedValue({
APP_MODE: "saas",
FEATURE_FLAGS: {
ENABLE_BILLING: true,
HIDE_LLM_SETTINGS: false,
},
});
vi.spyOn(FeatureFlags, "BILLING_SETTINGS").mockReturnValue(true);
const error = createAxiosNotFoundErrorObject();
getSettingsSpy.mockRejectedValue(error);
renderWithProviders(<RouterStub initialEntries={["/"]} />);
const setupPaymentModal = await screen.findByTestId(
"proceed-to-stripe-button",
);
const setupPaymentModal = await screen.findByTestId("proceed-to-stripe-button");
expect(setupPaymentModal).toBeInTheDocument();
});
});

View File

@@ -6,9 +6,11 @@ import { renderWithProviders } from "test-utils";
import OpenHands from "#/api/open-hands";
import SettingsScreen from "#/routes/settings";
import { PaymentForm } from "#/components/features/payment/payment-form";
import * as FeatureFlags from "#/utils/feature-flags";
describe("Settings Billing", () => {
const getConfigSpy = vi.spyOn(OpenHands, "getConfig");
vi.spyOn(FeatureFlags, "BILLING_SETTINGS").mockReturnValue(true);
const RoutesStub = createRoutesStub([
{
@@ -35,10 +37,6 @@ describe("Settings Billing", () => {
APP_MODE: "oss",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();
@@ -54,10 +52,6 @@ describe("Settings Billing", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: true,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();
@@ -75,10 +69,6 @@ describe("Settings Billing", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: true,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();

View File

@@ -1,6 +1,15 @@
import { render, screen, waitFor, within } from "@testing-library/react";
import { createRoutesStub } from "react-router";
import { afterEach, beforeEach, describe, expect, it, test, vi } from "vitest";
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
test,
vi,
} from "vitest";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import userEvent, { UserEvent } from "@testing-library/user-event";
import OpenHands from "#/api/open-hands";
@@ -11,6 +20,7 @@ import { MOCK_DEFAULT_USER_SETTINGS } from "#/mocks/handlers";
import { PostApiSettings } from "#/types/settings";
import * as ConsentHandlers from "#/utils/handle-capture-consent";
import AccountSettings from "#/routes/account-settings";
import * as FeatureFlags from "#/utils/feature-flags";
const toggleAdvancedSettings = async (user: UserEvent) => {
const advancedSwitch = await screen.findByTestId("advanced-settings-switch");
@@ -29,6 +39,11 @@ describe("Settings Screen", () => {
useAppLogout: vi.fn().mockReturnValue({ handleLogout: handleLogoutMock }),
}));
beforeAll(() => {
// TODO: Remove this once we release
vi.spyOn(FeatureFlags, "HIDE_LLM_SETTINGS").mockReturnValue(true);
});
afterEach(() => {
vi.clearAllMocks();
});
@@ -72,10 +87,6 @@ describe("Settings Screen", () => {
APP_MODE: "oss",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
});
@@ -112,7 +123,7 @@ describe("Settings Screen", () => {
});
});
it("should set '<hidden>' placeholder if the GitHub token is set", async () => {
it("should set asterik placeholder if the GitHub token is set", async () => {
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
github_token_is_set: true,
@@ -122,7 +133,7 @@ describe("Settings Screen", () => {
await waitFor(() => {
const input = screen.getByTestId("github-token-input");
expect(input).toHaveProperty("placeholder", "<hidden>");
expect(input).toHaveProperty("placeholder", "**********");
});
});
@@ -195,10 +206,6 @@ describe("Settings Screen", () => {
APP_MODE: "oss",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();
@@ -213,10 +220,6 @@ describe("Settings Screen", () => {
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
APP_SLUG: "test-app",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();
@@ -228,10 +231,6 @@ describe("Settings Screen", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();
@@ -309,10 +308,6 @@ describe("Settings Screen", () => {
APP_MODE: "oss",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
});
@@ -410,7 +405,7 @@ describe("Settings Screen", () => {
});
});
it("should set '<hidden>' placeholder if the LLM API key is set", async () => {
it("should set asterik placeholder if the LLM API key is set", async () => {
getSettingsSpy.mockResolvedValueOnce({
...MOCK_DEFAULT_USER_SETTINGS,
llm_api_key: "**********",
@@ -420,7 +415,7 @@ describe("Settings Screen", () => {
await waitFor(() => {
const input = screen.getByTestId("llm-api-key-input");
expect(input).toHaveProperty("placeholder", "<hidden>");
expect(input).toHaveProperty("placeholder", "**********");
});
});
@@ -454,10 +449,6 @@ describe("Settings Screen", () => {
APP_MODE: "oss",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();
@@ -472,10 +463,6 @@ describe("Settings Screen", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();
@@ -487,10 +474,6 @@ describe("Settings Screen", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
getSettingsSpy.mockResolvedValue({
@@ -509,10 +492,6 @@ describe("Settings Screen", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
renderSettingsScreen();
@@ -527,10 +506,6 @@ describe("Settings Screen", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: false,
},
});
getSettingsSpy.mockResolvedValue({
@@ -1007,10 +982,6 @@ describe("Settings Screen", () => {
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
POSTHOG_CLIENT_KEY: "456",
FEATURE_FLAGS: {
ENABLE_BILLING: false,
HIDE_LLM_SETTINGS: true,
},
});
});

View File

@@ -4,7 +4,6 @@ import store from "#/store";
import { trackError } from "#/utils/error-handler";
import ActionType from "#/types/action-type";
import { ActionMessage } from "#/types/message";
import * as queryReduxBridge from "#/utils/query-redux-bridge";
// Mock dependencies
vi.mock("#/utils/error-handler", () => ({
@@ -17,22 +16,13 @@ vi.mock("#/store", () => ({
},
}));
// Mock QueryReduxBridge
vi.mock("#/utils/query-redux-bridge", () => ({
getQueryReduxBridge: vi.fn(() => ({
isSliceMigrated: vi.fn(() => true),
syncReduxToQuery: vi.fn(),
conditionalDispatch: vi.fn(),
})),
}));
describe("Actions Service", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("handleStatusMessage", () => {
it("should handle info messages without dispatching to Redux (now using React Query)", () => {
it("should dispatch info messages to status state", () => {
const message = {
type: "info",
message: "Runtime is not available",
@@ -42,8 +32,9 @@ describe("Actions Service", () => {
handleStatusMessage(message);
// We no longer dispatch to Redux for info messages
expect(store.dispatch).not.toHaveBeenCalled();
expect(store.dispatch).toHaveBeenCalledWith(expect.objectContaining({
payload: message,
}));
});
it("should log error messages and display them in chat", () => {
@@ -69,54 +60,6 @@ describe("Actions Service", () => {
});
describe("handleActionMessage", () => {
it("should update metrics via React Query when metrics are available", () => {
const message: ActionMessage = {
id: 1,
action: ActionType.MESSAGE,
source: "agent",
message: "Test message",
timestamp: new Date().toISOString(),
args: {
content: "Test content",
},
llm_metrics: {
accumulated_cost: 0.05,
},
tool_call_metadata: {
model_response: {
usage: {
prompt_tokens: 100,
completion_tokens: 50,
total_tokens: 150,
}
}
}
};
const mockBridge = {
isSliceMigrated: vi.fn(() => true),
syncReduxToQuery: vi.fn(),
conditionalDispatch: vi.fn(),
};
vi.mocked(queryReduxBridge.getQueryReduxBridge).mockReturnValue(mockBridge as any);
handleActionMessage(message);
expect(mockBridge.isSliceMigrated).toHaveBeenCalledWith("metrics");
expect(mockBridge.syncReduxToQuery).toHaveBeenCalledWith(
["metrics"],
{
cost: 0.05,
usage: {
prompt_tokens: 100,
completion_tokens: 50,
total_tokens: 150,
}
}
);
});
it("should use first-person perspective for task completion messages", () => {
// Test partial completion
const messagePartial: ActionMessage = {

View File

@@ -0,0 +1,20 @@
import { vi } from "vitest";
import OpenHands from "#/api/open-hands";
export const setupTestConfig = () => {
const getConfigSpy = vi.spyOn(OpenHands, "getConfig");
getConfigSpy.mockResolvedValue({
APP_MODE: "oss",
GITHUB_CLIENT_ID: "test-id",
POSTHOG_CLIENT_KEY: "test-key",
});
};
export const setupSaasTestConfig = () => {
const getConfigSpy = vi.spyOn(OpenHands, "getConfig");
getConfigSpy.mockResolvedValue({
APP_MODE: "saas",
GITHUB_CLIENT_ID: "test-id",
POSTHOG_CLIENT_KEY: "test-key",
});
};

View File

@@ -1,154 +0,0 @@
# Redux to React Query Migration Guide
This guide outlines the process for migrating from Redux to React Query in our application.
## Overview
The migration strategy allows for a gradual transition from Redux to React Query, with the ability to migrate one slice at a time without breaking the application. This is achieved through a bridge that coordinates between Redux and React Query.
## Key Components
1. **QueryReduxBridge**: A utility class that manages the migration state and coordinates between Redux and React Query.
2. **Websocket Integration**: Modified to respect migration flags and update the appropriate state management system.
3. **React Query Hooks**: New hooks that replace Redux slice functionality.
## Migration Steps
### 1. Initialize the Bridge
In your main application file (e.g., `App.tsx`), initialize the bridge:
```tsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { initQueryReduxBridge } from '#/utils/query-redux-bridge';
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
},
},
...queryClientConfig,
});
// Initialize the bridge
initQueryReduxBridge(queryClient);
function App() {
return (
<QueryClientProvider client={queryClient}>
{/* Your app components */}
</QueryClientProvider>
);
}
```
### 2. Replace the WebSocket Provider
Replace the original WebSocket provider with the bridge-aware version:
```tsx
import { WsClientProviderWithBridge } from '#/context/ws-client-provider-with-bridge';
// Instead of
// <WsClientProvider conversationId={conversationId}>
// {children}
// </WsClientProvider>
// Use
<WsClientProviderWithBridge conversationId={conversationId}>
{children}
</WsClientProviderWithBridge>
```
### 3. Add the WebSocket Events Hook
Add the WebSocket events hook to your application to handle events for React Query:
```tsx
import { useWebsocketEvents } from '#/hooks/query/use-websocket-events';
function YourComponent() {
// This hook will process websocket events for React Query
useWebsocketEvents();
// Rest of your component
return (
// ...
);
}
```
### 4. Migrate Individual Slices
For each Redux slice you want to migrate:
1. Create a React Query hook that replaces the slice functionality
2. Mark the slice as migrated
3. Update components to use the new hook instead of Redux
Example for migrating the chat slice:
```tsx
import { useChatMessages } from '#/hooks/query/use-chat-messages';
import { getQueryReduxBridge } from '#/utils/query-redux-bridge';
// Mark the slice as migrated
getQueryReduxBridge().migrateSlice('chat');
function ChatComponent() {
// Instead of using useSelector and useDispatch
// const messages = useSelector((state) => state.chat.messages);
// const dispatch = useDispatch();
// Use the React Query hook
const {
messages,
addUserMessage,
addAssistantMessage,
addErrorMessage,
clearMessages
} = useChatMessages();
// Rest of your component using the new API
return (
// ...
);
}
```
## Testing the Migration
To test the migration of a single slice:
1. Create the React Query hook for the slice
2. Mark the slice as migrated using `getQueryReduxBridge().migrateSlice('sliceName')`
3. Update a single component to use the new hook
4. Test the application to ensure it works correctly
5. If issues arise, you can easily revert by removing the migration flag
## Troubleshooting
### Duplicate Updates
If you see duplicate updates (e.g., chat messages appearing twice), check:
1. Ensure you're using the bridge-aware WebSocket provider
2. Verify the slice is properly marked as migrated
3. Check that components aren't mixing Redux and React Query for the same slice
### Console Errors
If you encounter console errors:
1. Check for race conditions between Redux and React Query
2. Ensure the WebSocket events hook is properly mounted
3. Verify that the QueryReduxBridge is initialized before any components try to use it
## Complete Migration
Once all slices are migrated:
1. Remove the Redux store and related code
2. Simplify the bridge code to remove Redux dependencies
3. Update the WebSocket provider to directly update React Query without the bridge

View File

@@ -49,10 +49,6 @@ export interface GetConfigResponse {
GITHUB_CLIENT_ID: string;
POSTHOG_CLIENT_KEY: string;
STRIPE_PUBLISHABLE_KEY?: string;
FEATURE_FLAGS: {
ENABLE_BILLING: boolean;
HIDE_LLM_SETTINGS: boolean;
};
}
export interface GetVSCodeUrlResponse {

View File

@@ -4,7 +4,7 @@ import {
} from "#/components/shared/modals/confirmation-modals/base-modal";
import { ModalBackdrop } from "#/components/shared/modals/modal-backdrop";
import { ModalBody } from "#/components/shared/modals/modal-body";
import { useSaveSettings } from "#/hooks/mutation/use-save-settings";
import { useCurrentSettings } from "#/context/settings-context";
import { handleCaptureConsent } from "#/utils/handle-capture-consent";
import { BrandButton } from "../settings/brand-button";
@@ -15,14 +15,14 @@ interface AnalyticsConsentFormModalProps {
export function AnalyticsConsentFormModal({
onClose,
}: AnalyticsConsentFormModalProps) {
const { mutate: saveUserSettings } = useSaveSettings();
const { saveUserSettings } = useCurrentSettings();
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const analytics = formData.get("analytics") === "on";
saveUserSettings(
await saveUserSettings(
{ user_consents_to_analytics: analytics },
{
onSuccess: () => {

View File

@@ -1,8 +1,10 @@
import posthog from "posthog-js";
import React from "react";
import { useSelector } from "react-redux";
import { SuggestionItem } from "#/components/features/suggestions/suggestion-item";
import { DownloadModal } from "#/components/shared/download-modal";
import type { RootState } from "#/store";
import { useAuth } from "#/context/auth-context";
import { useInitialQuery } from "#/hooks/query/use-initial-query";
interface ActionSuggestionsProps {
onSuggestionsClick: (value: string) => void;
@@ -12,13 +14,25 @@ export function ActionSuggestions({
onSuggestionsClick,
}: ActionSuggestionsProps) {
const { githubTokenIsSet } = useAuth();
const { selectedRepository } = useInitialQuery();
const { selectedRepository } = useSelector(
(state: RootState) => state.initialQuery,
);
const [isDownloading, setIsDownloading] = React.useState(false);
const [hasPullRequest, setHasPullRequest] = React.useState(false);
const handleDownloadClose = () => {
setIsDownloading(false);
};
return (
<div className="flex flex-col gap-2 mb-2">
{githubTokenIsSet && selectedRepository && (
<DownloadModal
initialPath=""
onClose={handleDownloadClose}
isOpen={isDownloading}
/>
{githubTokenIsSet && selectedRepository ? (
<div className="flex flex-row gap-2 justify-center w-full">
{!hasPullRequest ? (
<>
@@ -60,6 +74,21 @@ export function ActionSuggestions({
/>
)}
</div>
) : (
<SuggestionItem
suggestion={{
label: !isDownloading
? "Download files"
: "Downloading, please wait...",
value: "Download files",
}}
onClick={() => {
posthog.capture("download_workspace_button_clicked");
if (!isDownloading) {
setIsDownloading(true);
}
}}
/>
)}
</div>
);

View File

@@ -13,25 +13,31 @@ import { generateAgentStateChangeEvent } from "#/services/agent-state-service";
import { FeedbackModal } from "../feedback/feedback-modal";
import { useScrollToBottom } from "#/hooks/use-scroll-to-bottom";
import { TypingIndicator } from "./typing-indicator";
import { useWsClient } from "#/context/ws-client-provider";
import {
useWsClient,
WsClientProviderStatus,
} from "#/context/ws-client-provider";
import { Messages } from "./messages";
import { ChatSuggestions } from "./chat-suggestions";
import { ActionSuggestions } from "./action-suggestions";
import { useInitialQuery } from "#/hooks/query/use-initial-query";
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-trajectory";
import { downloadTrajectory } from "#/utils/download-files";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
function getEntryPoint(hasRepository: boolean | null): string {
function getEntryPoint(
hasRepository: boolean | null,
hasImportedProjectZip: boolean | null,
): string {
if (hasRepository) return "github";
if (hasImportedProjectZip) return "zip";
return "direct";
}
export function ChatInterface() {
const { send, isLoadingMessages } = useWsClient();
const { send, isLoadingMessages, status, pendingMessages } = useWsClient();
const dispatch = useDispatch();
const scrollRef = React.useRef<HTMLDivElement>(null);
const { scrollDomToBottom, onChatBodyScroll, hitBottom } =
@@ -45,15 +51,24 @@ export function ChatInterface() {
>("positive");
const [feedbackModalIsOpen, setFeedbackModalIsOpen] = React.useState(false);
const [messageToSend, setMessageToSend] = React.useState<string | null>(null);
const { selectedRepository } = useInitialQuery();
const { selectedRepository, importedProjectZip } = useSelector(
(state: RootState) => state.initialQuery,
);
const params = useParams();
const { mutate: getTrajectory } = useGetTrajectory();
const isClientDisconnected = status === WsClientProviderStatus.DISCONNECTED;
const hasPendingMessages = pendingMessages.length > 0;
const handleSendMessage = async (content: string, files: File[]) => {
if (messages.length === 0) {
posthog.capture("initial_query_submitted", {
entry_point: getEntryPoint(selectedRepository !== null),
entry_point: getEntryPoint(
selectedRepository !== null,
importedProjectZip !== null,
),
query_character_length: content.length,
uploaded_zip_size: importedProjectZip?.length,
});
} else {
posthog.capture("user_message_sent", {
@@ -67,7 +82,15 @@ export function ChatInterface() {
const timestamp = new Date().toISOString();
const pending = true;
dispatch(addUserMessage({ content, imageUrls, timestamp, pending }));
send(createChatMessage(content, imageUrls, timestamp));
// Create and send the chat message
const chatMessage = createChatMessage(content, imageUrls, timestamp);
send(chatMessage);
// Send the agent state change event immediately
// The backend will handle the ordering and queueing
send(generateAgentStateChangeEvent(AgentState.RUNNING));
setMessageToSend(null);
};
@@ -76,6 +99,10 @@ export function ChatInterface() {
send(generateAgentStateChangeEvent(AgentState.STOPPED));
};
const handleSendContinueMsg = () => {
handleSendMessage("Continue", []);
};
const onClickShareFeedbackActionButton = async (
polarity: "positive" | "negative",
) => {
@@ -118,8 +145,20 @@ export function ChatInterface() {
className="flex flex-col grow overflow-y-auto overflow-x-hidden px-4 pt-4 gap-2"
>
{isLoadingMessages && (
<div className="flex justify-center">
<div className="flex flex-col items-center gap-2">
<LoadingSpinner size="small" />
{isClientDisconnected && (
<div className="text-sm text-neutral-400">
Waiting for client to become ready...
{hasPendingMessages && (
<div className="text-xs text-neutral-500 mt-1">
{pendingMessages.length} message
{pendingMessages.length !== 1 ? "s" : ""} will be sent when
connected
</div>
)}
</div>
)}
</div>
)}
@@ -152,6 +191,10 @@ export function ChatInterface() {
/>
<div className="absolute left-1/2 transform -translate-x-1/2 bottom-0">
{messages.length > 2 &&
curAgentState === AgentState.AWAITING_USER_INPUT && (
<ContinueButton onClick={handleSendContinueMsg} />
)}
{curAgentState === AgentState.RUNNING && <TypingIndicator />}
</div>
@@ -162,7 +205,7 @@ export function ChatInterface() {
onSubmit={handleSendMessage}
onStop={handleStop}
isDisabled={
curAgentState === AgentState.LOADING ||
// Allow input even when loading, but not during confirmation
curAgentState === AgentState.AWAITING_USER_CONFIRMATION
}
mode={curAgentState === AgentState.RUNNING ? "stop" : "submit"}

View File

@@ -11,6 +11,7 @@ import CheckCircle from "#/icons/check-circle-solid.svg?react";
import XCircle from "#/icons/x-circle-solid.svg?react";
import { cn } from "#/utils/utils";
import { useConfig } from "#/hooks/query/use-config";
import { BILLING_SETTINGS } from "#/utils/feature-flags";
interface ExpandableMessageProps {
id?: string;
@@ -42,15 +43,12 @@ export function ExpandableMessage({
const statusIconClasses = "h-4 w-4 ml-2 inline";
if (
config?.FEATURE_FLAGS.ENABLE_BILLING &&
BILLING_SETTINGS() &&
config?.APP_MODE === "saas" &&
id === "STATUS$ERROR_LLM_OUT_OF_CREDITS"
) {
return (
<div
data-testid="out-of-credits"
className="flex gap-2 items-center justify-start border-l-2 pl-2 my-2 py-2 border-danger"
>
<div className="flex gap-2 items-center justify-start border-l-2 pl-2 my-2 py-2 border-danger">
<div className="text-sm w-full">
<div className="font-bold text-danger">
{t("STATUS$ERROR_LLM_OUT_OF_CREDITS")}

View File

@@ -11,7 +11,6 @@ import {
} from "#/context/ws-client-provider";
import { useNotification } from "#/hooks/useNotification";
import { browserTab } from "#/utils/browser-tab";
import { useStatusMessage } from "#/hooks/query/use-status-message";
const notificationStates = [
AgentState.AWAITING_USER_INPUT,
@@ -22,11 +21,12 @@ const notificationStates = [
export function AgentStatusBar() {
const { t, i18n } = useTranslation();
const { curAgentState } = useSelector((state: RootState) => state.agent);
const { statusMessage: curStatusMessage } = useStatusMessage();
const { status } = useWsClient();
const { curStatusMessage } = useSelector((state: RootState) => state.status);
const { status, pendingMessages } = useWsClient();
const { notify } = useNotification();
const [statusMessage, setStatusMessage] = React.useState<string>("");
const hasPendingMessages = pendingMessages.length > 0;
const updateStatusMessage = () => {
let message = curStatusMessage.message || "";
@@ -72,7 +72,13 @@ export function AgentStatusBar() {
React.useEffect(() => {
if (status === WsClientProviderStatus.DISCONNECTED) {
setStatusMessage("Connecting...");
if (hasPendingMessages) {
setStatusMessage(
`Connecting... (${pendingMessages.length} pending message${pendingMessages.length !== 1 ? "s" : ""})`,
);
} else {
setStatusMessage("Connecting...");
}
} else {
setStatusMessage(AGENT_STATUS_MAP[curAgentState].message);
if (notificationStates.includes(curAgentState)) {
@@ -88,7 +94,7 @@ export function AgentStatusBar() {
}
}
}
}, [curAgentState, notify, t]);
}, [curAgentState, status, pendingMessages.length, notify, t]);
return (
<div className="flex flex-col items-center">

View File

@@ -1,11 +1,12 @@
import { useParams } from "react-router";
import React from "react";
import posthog from "posthog-js";
import { AgentControlBar } from "./agent-control-bar";
import { AgentStatusBar } from "./agent-status-bar";
import { SecurityLock } from "./security-lock";
import { useUserConversation } from "#/hooks/query/use-user-conversation";
import { ConversationCard } from "../conversation-panel/conversation-card";
import { useAutoTitle } from "#/hooks/use-auto-title";
import { DownloadModal } from "#/components/shared/download-modal";
interface ControlsProps {
setSecurityOpen: (isOpen: boolean) => void;
@@ -17,7 +18,13 @@ export function Controls({ setSecurityOpen, showSecurityLock }: ControlsProps) {
const { data: conversation } = useUserConversation(
params.conversationId ?? null,
);
useAutoTitle();
const [downloading, setDownloading] = React.useState(false);
const handleDownloadWorkspace = () => {
posthog.capture("download_workspace_button_clicked");
setDownloading(true);
};
return (
<div className="flex items-center justify-between">
@@ -32,12 +39,17 @@ export function Controls({ setSecurityOpen, showSecurityLock }: ControlsProps) {
<ConversationCard
variant="compact"
showDisplayCostOption
onDownloadWorkspace={handleDownloadWorkspace}
title={conversation?.title ?? ""}
lastUpdatedAt={conversation?.created_at ?? ""}
selectedRepository={conversation?.selected_repository ?? null}
status={conversation?.status}
conversationId={conversation?.conversation_id}
/>
<DownloadModal
initialPath=""
onClose={() => setDownloading(false)}
isOpen={downloading}
/>
</div>
);

View File

@@ -7,8 +7,7 @@ interface ConversationCardContextMenuProps {
onClose: () => void;
onDelete?: (event: React.MouseEvent<HTMLButtonElement>) => void;
onEdit?: (event: React.MouseEvent<HTMLButtonElement>) => void;
onDisplayCost?: (event: React.MouseEvent<HTMLButtonElement>) => void;
onDownloadViaVSCode?: (event: React.MouseEvent<HTMLButtonElement>) => void;
onDownload?: (event: React.MouseEvent<HTMLButtonElement>) => void;
position?: "top" | "bottom";
}
@@ -16,8 +15,7 @@ export function ConversationCardContextMenu({
onClose,
onDelete,
onEdit,
onDisplayCost,
onDownloadViaVSCode,
onDownload,
position = "bottom",
}: ConversationCardContextMenuProps) {
const ref = useClickOutsideElement<HTMLUListElement>(onClose);
@@ -42,20 +40,9 @@ export function ConversationCardContextMenu({
Edit Title
</ContextMenuListItem>
)}
{onDownloadViaVSCode && (
<ContextMenuListItem
testId="download-vscode-button"
onClick={onDownloadViaVSCode}
>
Download via VS Code
</ContextMenuListItem>
)}
{onDisplayCost && (
<ContextMenuListItem
testId="display-cost-button"
onClick={onDisplayCost}
>
Display Cost
{onDownload && (
<ContextMenuListItem testId="download-button" onClick={onDownload}>
Download Workspace
</ContextMenuListItem>
)}
</ContextMenu>

View File

@@ -1,5 +1,4 @@
import React from "react";
import posthog from "posthog-js";
import { formatTimeDelta } from "#/utils/format-time-delta";
import { ConversationRepoLink } from "./conversation-repo-link";
import {
@@ -9,44 +8,36 @@ import {
import { EllipsisButton } from "./ellipsis-button";
import { ConversationCardContextMenu } from "./conversation-card-context-menu";
import { cn } from "#/utils/utils";
import { BaseModal } from "../../shared/modals/base-modal/base-modal";
import { useMetrics } from "#/hooks/query/use-metrics";
interface ConversationCardProps {
onClick?: () => void;
onDelete?: () => void;
onChangeTitle?: (title: string) => void;
showDisplayCostOption?: boolean;
onDownloadWorkspace?: () => void;
isActive?: boolean;
title: string;
selectedRepository: string | null;
lastUpdatedAt: string; // ISO 8601
status?: ProjectStatus;
variant?: "compact" | "default";
conversationId?: string; // Optional conversation ID for VS Code URL
}
export function ConversationCard({
onClick,
onDelete,
onChangeTitle,
showDisplayCostOption,
onDownloadWorkspace,
isActive,
title,
selectedRepository,
lastUpdatedAt,
status = "STOPPED",
variant = "default",
conversationId,
}: ConversationCardProps) {
const [contextMenuVisible, setContextMenuVisible] = React.useState(false);
const [titleMode, setTitleMode] = React.useState<"view" | "edit">("view");
const [metricsModalVisible, setMetricsModalVisible] = React.useState(false);
const inputRef = React.useRef<HTMLInputElement>(null);
// Subscribe to metrics data from React Query
const { metrics } = useMetrics();
const handleBlur = () => {
if (inputRef.current?.value) {
const trimmed = inputRef.current.value.trim();
@@ -87,37 +78,9 @@ export function ConversationCard({
setContextMenuVisible(false);
};
const handleDownloadViaVSCode = async (
event: React.MouseEvent<HTMLButtonElement>,
) => {
event.preventDefault();
const handleDownload = (event: React.MouseEvent<HTMLButtonElement>) => {
event.stopPropagation();
posthog.capture("download_via_vscode_button_clicked");
// Fetch the VS Code URL from the API
if (conversationId) {
try {
const response = await fetch(
`/api/conversations/${conversationId}/vscode-url`,
);
const data = await response.json();
if (data.vscode_url) {
window.open(data.vscode_url, "_blank");
} else {
console.error("VS Code URL not available", data.error);
}
} catch (error) {
console.error("Failed to fetch VS Code URL", error);
}
}
setContextMenuVisible(false);
};
const handleDisplayCost = (event: React.MouseEvent<HTMLButtonElement>) => {
event.stopPropagation();
setMetricsModalVisible(true);
onDownloadWorkspace?.();
};
React.useEffect(() => {
@@ -126,114 +89,81 @@ export function ConversationCard({
}
}, [titleMode]);
const hasContextMenu = !!(onDelete || onChangeTitle || showDisplayCostOption);
const hasContextMenu = !!(onDelete || onChangeTitle || onDownloadWorkspace);
return (
<>
<div
data-testid="conversation-card"
onClick={onClick}
className={cn(
"h-[100px] w-full px-[18px] py-4 border-b border-neutral-600 cursor-pointer",
variant === "compact" &&
"h-auto w-fit rounded-xl border border-[#525252]",
)}
>
<div className="flex items-center justify-between w-full">
<div className="flex items-center gap-2 flex-1 min-w-0 overflow-hidden mr-2">
{isActive && (
<span className="w-2 h-2 bg-blue-500 rounded-full flex-shrink-0" />
)}
{titleMode === "edit" && (
<input
ref={inputRef}
data-testid="conversation-card-title"
onClick={handleInputClick}
onBlur={handleBlur}
onKeyUp={handleKeyUp}
type="text"
defaultValue={title}
className="text-sm leading-6 font-semibold bg-transparent w-full"
/>
)}
{titleMode === "view" && (
<p
data-testid="conversation-card-title"
className="text-sm leading-6 font-semibold bg-transparent truncate overflow-hidden"
title={title}
>
{title}
</p>
)}
</div>
<div className="flex items-center gap-2 relative">
<ConversationStateIndicator status={status} />
{hasContextMenu && (
<EllipsisButton
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
setContextMenuVisible((prev) => !prev);
}}
/>
)}
{contextMenuVisible && (
<ConversationCardContextMenu
onClose={() => setContextMenuVisible(false)}
onDelete={onDelete && handleDelete}
onEdit={onChangeTitle && handleEdit}
onDownloadViaVSCode={
conversationId ? handleDownloadViaVSCode : undefined
}
onDisplayCost={
showDisplayCostOption ? handleDisplayCost : undefined
}
position={variant === "compact" ? "top" : "bottom"}
/>
)}
</div>
<div
data-testid="conversation-card"
onClick={onClick}
className={cn(
"h-[100px] w-full px-[18px] py-4 border-b border-neutral-600 cursor-pointer",
variant === "compact" &&
"h-auto w-fit rounded-xl border border-[#525252]",
)}
>
<div className="flex items-center justify-between w-full">
<div className="flex items-center gap-2 flex-1 min-w-0 overflow-hidden mr-2">
{isActive && (
<span className="w-2 h-2 bg-blue-500 rounded-full flex-shrink-0" />
)}
{titleMode === "edit" && (
<input
ref={inputRef}
data-testid="conversation-card-title"
onClick={handleInputClick}
onBlur={handleBlur}
onKeyUp={handleKeyUp}
type="text"
defaultValue={title}
className="text-sm leading-6 font-semibold bg-transparent w-full"
/>
)}
{titleMode === "view" && (
<p
data-testid="conversation-card-title"
className="text-sm leading-6 font-semibold bg-transparent truncate overflow-hidden"
title={title}
>
{title}
</p>
)}
</div>
<div
className={cn(
variant === "compact" && "flex items-center justify-between mt-1",
<div className="flex items-center gap-2 relative">
<ConversationStateIndicator status={status} />
{hasContextMenu && (
<EllipsisButton
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
setContextMenuVisible((prev) => !prev);
}}
/>
)}
>
{selectedRepository && (
<ConversationRepoLink selectedRepository={selectedRepository} />
{contextMenuVisible && (
<ConversationCardContextMenu
onClose={() => setContextMenuVisible(false)}
onDelete={onDelete && handleDelete}
onEdit={onChangeTitle && handleEdit}
onDownload={onDownloadWorkspace && handleDownload}
position={variant === "compact" ? "top" : "bottom"}
/>
)}
<p className="text-xs text-neutral-400">
<time>{formatTimeDelta(new Date(lastUpdatedAt))} ago</time>
</p>
</div>
</div>
<BaseModal
isOpen={metricsModalVisible}
onOpenChange={setMetricsModalVisible}
title="Metrics Information"
testID="metrics-modal"
<div
className={cn(
variant === "compact" && "flex items-center justify-between mt-1",
)}
>
<div className="space-y-2">
{metrics?.cost !== null && (
<p>Total Cost: ${metrics.cost.toFixed(4)}</p>
)}
{metrics?.usage !== null && (
<>
<p>Tokens Used:</p>
<ul className="list-inside space-y-1 ml-2">
<li>- Input: {metrics.usage.prompt_tokens}</li>
<li>- Output: {metrics.usage.completion_tokens}</li>
<li>- Total: {metrics.usage.total_tokens}</li>
</ul>
</>
)}
{!metrics?.cost && !metrics?.usage && (
<p className="text-neutral-400">No metrics data available</p>
)}
</div>
</BaseModal>
</>
{selectedRepository && (
<ConversationRepoLink selectedRepository={selectedRepository} />
)}
<p className="text-xs text-neutral-400">
<time>{formatTimeDelta(new Date(lastUpdatedAt))} ago</time>
</p>
</div>
</div>
);
}

View File

@@ -1,29 +0,0 @@
import React from "react";
import { useCreateConversation } from "#/hooks/mutation/use-create-conversation";
import { useInitialQuery } from "#/hooks/query/use-initial-query";
const INITIAL_PROMPT = "";
export function CodeNotInGitHubLink() {
const { setInitialPrompt } = useInitialQuery();
const { mutate: createConversation } = useCreateConversation();
const handleStartFromScratch = () => {
// Set the initial prompt and create a new conversation
setInitialPrompt(INITIAL_PROMPT);
createConversation({ q: INITIAL_PROMPT });
};
return (
<div className="text-xs text-neutral-400">
Code not in GitHub?{" "}
<span
onClick={handleStartFromScratch}
className="underline cursor-pointer"
>
Start from scratch
</span>{" "}
and use the VS Code link to upload and download your code.
</div>
);
}

View File

@@ -5,11 +5,12 @@ import {
AutocompleteItem,
AutocompleteSection,
} from "@heroui/react";
import { useDispatch } from "react-redux";
import posthog from "posthog-js";
import { I18nKey } from "#/i18n/declaration";
import { setSelectedRepository } from "#/state/initial-query-slice";
import { useConfig } from "#/hooks/query/use-config";
import { sanitizeQuery } from "#/utils/sanitize-query";
import { useInitialQuery } from "#/hooks/query/use-initial-query";
interface GitHubRepositorySelectorProps {
onInputChange: (value: string) => void;
@@ -35,12 +36,12 @@ export function GitHubRepositorySelector({
...userRepositories,
];
const { setSelectedRepository } = useInitialQuery();
const dispatch = useDispatch();
const handleRepoSelection = (id: string | null) => {
const repo = allRepositories.find((r) => r.id.toString() === id);
if (repo) {
setSelectedRepository(repo.full_name);
dispatch(setSelectedRepository(repo.full_name));
posthog.capture("repository_selected");
onSelect();
setSelectedKey(id);
@@ -48,7 +49,7 @@ export function GitHubRepositorySelector({
};
const handleClearSelection = () => {
setSelectedRepository(null);
dispatch(setSelectedRepository(null));
};
const emptyContent = t(I18nKey.GITHUB$NO_RESULTS);

View File

@@ -63,8 +63,8 @@ export function PaymentForm() {
name="top-up-input"
onChange={handleTopUpInputChange}
type="text"
label="Add funds"
placeholder="Specify an amount (USD) to add to your account"
label="Top-up amount"
placeholder="Specify an amount to top up your credits"
className="w-[680px]"
/>

View File

@@ -10,6 +10,7 @@ import { DocsButton } from "#/components/shared/buttons/docs-button";
import { ExitProjectButton } from "#/components/shared/buttons/exit-project-button";
import { SettingsButton } from "#/components/shared/buttons/settings-button";
import { SettingsModal } from "#/components/shared/modals/settings/settings-modal";
import { useCurrentSettings } from "#/context/settings-context";
import { useSettings } from "#/hooks/query/use-settings";
import { ConversationPanel } from "../conversation-panel/conversation-panel";
import { useEndSession } from "#/hooks/use-end-session";
@@ -21,7 +22,7 @@ import { useLogout } from "#/hooks/mutation/use-logout";
import { useConfig } from "#/hooks/query/use-config";
import { cn } from "#/utils/utils";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
import { useSaveSettings } from "#/hooks/mutation/use-save-settings";
import { HIDE_LLM_SETTINGS } from "#/utils/feature-flags";
export function Sidebar() {
const location = useLocation();
@@ -30,13 +31,12 @@ export function Sidebar() {
const user = useGitHubUser();
const { data: config } = useConfig();
const {
data: settings,
error: settingsError,
isError: settingsIsError,
isFetching: isFetchingSettings,
} = useSettings();
const { mutateAsync: logout } = useLogout();
const { mutate: saveUserSettings } = useSaveSettings();
const { settings, saveUserSettings } = useCurrentSettings();
const [settingsModalIsOpen, setSettingsModalIsOpen] = React.useState(false);
@@ -44,11 +44,10 @@ export function Sidebar() {
React.useState(false);
// TODO: Remove HIDE_LLM_SETTINGS check once released
const shouldHideLlmSettings =
config?.FEATURE_FLAGS.HIDE_LLM_SETTINGS && config?.APP_MODE === "saas";
const isSaas = HIDE_LLM_SETTINGS() && config?.APP_MODE === "saas";
React.useEffect(() => {
if (shouldHideLlmSettings) return;
if (isSaas) return;
if (location.pathname === "/settings") {
setSettingsModalIsOpen(false);
@@ -79,7 +78,7 @@ export function Sidebar() {
const handleLogout = async () => {
if (config?.APP_MODE === "saas") await logout();
else saveUserSettings({ unset_github_token: true });
else await saveUserSettings({ unset_github_token: true });
posthog.reset();
};
@@ -105,10 +104,10 @@ export function Sidebar() {
)}
/>
</TooltipButton>
<DocsButton />
</div>
<div className="flex flex-row md:flex-col md:items-center gap-[26px] md:mb-4">
<DocsButton />
<NavLink
to="/settings"
className={({ isActive }) =>

View File

@@ -0,0 +1,33 @@
import { useTranslation } from "react-i18next";
import { I18nKey } from "#/i18n/declaration";
import { SuggestionBox } from "./suggestion-box";
interface ImportProjectSuggestionBoxProps {
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
}
export function ImportProjectSuggestionBox({
onChange,
}: ImportProjectSuggestionBoxProps) {
const { t } = useTranslation();
return (
<SuggestionBox
title={t(I18nKey.LANDING$IMPORT_PROJECT)}
content={
<label htmlFor="import-project" className="w-full flex justify-center">
<span className="border-2 border-dashed border-neutral-600 rounded px-2 py-1 cursor-pointer">
{t(I18nKey.LANDING$UPLOAD_ZIP)}
</span>
<input
hidden
type="file"
accept="application/zip"
id="import-project"
multiple={false}
onChange={onChange}
/>
</label>
}
/>
);
}

View File

@@ -23,7 +23,7 @@ export function NavTab({ to, label, icon, isBeta }: NavTabProps) {
>
{({ isActive }) => (
<>
<div className={cn(isActive && "text-logo")}>{icon}</div>
<div className={cn(isActive && "text-primary")}>{icon}</div>
{label}
{isBeta && <BetaBadge />}
</>

View File

@@ -0,0 +1,23 @@
import ChevronDoubleRight from "#/icons/chevron-double-right.svg?react";
import { cn } from "#/utils/utils";
interface ContinueButtonProps {
onClick: () => void;
}
export function ContinueButton({ onClick }: ContinueButtonProps) {
return (
<button
type="button"
onClick={onClick}
className={cn(
"button-base px-2 py-1",
"text-[11px] leading-4 tracking-[0.01em] font-[500]",
"flex items-center gap-2",
)}
>
<ChevronDoubleRight width={12} height={12} />
Continue
</button>
);
}

View File

@@ -0,0 +1,33 @@
import { useDownloadProgress } from "#/hooks/use-download-progress";
import { DownloadProgress } from "./download-progress";
interface DownloadModalProps {
initialPath: string;
onClose: () => void;
isOpen: boolean;
}
function ActiveDownload({
initialPath,
onClose,
}: {
initialPath: string;
onClose: () => void;
}) {
const { progress, cancelDownload } = useDownloadProgress(
initialPath,
onClose,
);
return <DownloadProgress progress={progress} onCancel={cancelDownload} />;
}
export function DownloadModal({
initialPath,
onClose,
isOpen,
}: DownloadModalProps) {
if (!isOpen) return null;
return <ActiveDownload initialPath={initialPath} onClose={onClose} />;
}

View File

@@ -0,0 +1,94 @@
import { useTranslation } from "react-i18next";
import { I18nKey } from "#/i18n/declaration";
export interface DownloadProgressState {
filesTotal: number;
filesDownloaded: number;
currentFile: string;
totalBytesDownloaded: number;
bytesDownloadedPerSecond: number;
isDiscoveringFiles: boolean;
}
interface DownloadProgressProps {
progress: DownloadProgressState;
onCancel: () => void;
}
export function DownloadProgress({
progress,
onCancel,
}: DownloadProgressProps) {
const { t } = useTranslation();
const formatBytes = (bytes: number) => {
const units = ["B", "KB", "MB", "GB"];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex += 1;
}
return `${size.toFixed(1)} ${units[unitIndex]}`;
};
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-20">
<div className="bg-[#1C1C1C] rounded-lg p-6 max-w-md w-full mx-4 border border-[#525252]">
<div className="mb-4">
<h3 className="text-lg font-semibold mb-2 text-white">
{progress.isDiscoveringFiles
? t(I18nKey.DOWNLOAD$PREPARING)
: t(I18nKey.DOWNLOAD$DOWNLOADING)}
</h3>
<p className="text-sm text-gray-400 truncate">
{progress.isDiscoveringFiles
? t(I18nKey.DOWNLOAD$FOUND_FILES, { count: progress.filesTotal })
: progress.currentFile}
</p>
</div>
<div className="mb-4">
<div className="h-2 bg-[#2C2C2C] rounded-full overflow-hidden">
{progress.isDiscoveringFiles ? (
<div
className="h-full bg-blue-500 animate-pulse"
style={{ width: "100%" }}
/>
) : (
<div
className="h-full bg-blue-500 transition-all duration-300"
style={{
width: `${(progress.filesDownloaded / progress.filesTotal) * 100}%`,
}}
/>
)}
</div>
</div>
<div className="flex justify-between text-sm text-gray-400">
<span>
{progress.isDiscoveringFiles
? t(I18nKey.DOWNLOAD$SCANNING)
: t(I18nKey.DOWNLOAD$FILES_PROGRESS, {
downloaded: progress.filesDownloaded,
total: progress.filesTotal,
})}
</span>
{!progress.isDiscoveringFiles && (
<span>{formatBytes(progress.bytesDownloadedPerSecond)}/s</span>
)}
</div>
<div className="mt-4 flex justify-end">
<button
type="button"
onClick={onCancel}
className="px-4 py-2 text-sm text-gray-400 hover:text-white transition-colors"
>
{t(I18nKey.DOWNLOAD$CANCEL)}
</button>
</div>
</div>
</div>
);
}

View File

@@ -9,12 +9,12 @@ import { extractSettings } from "#/utils/settings-utils";
import { useEndSession } from "#/hooks/use-end-session";
import { ModalBackdrop } from "../modal-backdrop";
import { ModelSelector } from "./model-selector";
import { useCurrentSettings } from "#/context/settings-context";
import { Settings } from "#/types/settings";
import { BrandButton } from "#/components/features/settings/brand-button";
import { KeyStatusIcon } from "#/components/features/settings/key-status-icon";
import { SettingsInput } from "#/components/features/settings/settings-input";
import { HelpLink } from "#/components/features/settings/help-link";
import { useSaveSettings } from "#/hooks/mutation/use-save-settings";
interface SettingsFormProps {
settings: Settings;
@@ -23,7 +23,7 @@ interface SettingsFormProps {
}
export function SettingsForm({ settings, models, onClose }: SettingsFormProps) {
const { mutate: saveUserSettings } = useSaveSettings();
const { saveUserSettings } = useCurrentSettings();
const endSession = useEndSession();
const location = useLocation();
@@ -96,7 +96,6 @@ export function SettingsForm({ settings, models, onClose }: SettingsFormProps) {
label="API Key"
type="password"
className="w-[680px]"
placeholder={isLLMKeySet ? "<hidden>" : ""}
startContent={isLLMKeySet && <KeyStatusIcon isSet={isLLMKeySet} />}
/>

View File

@@ -1,5 +1,8 @@
import React from "react";
import { useNavigation } from "react-router";
import { useDispatch, useSelector } from "react-redux";
import { RootState } from "#/store";
import { addFile, removeFile } from "#/state/initial-query-slice";
import { SuggestionBubble } from "#/components/features/suggestions/suggestion-bubble";
import { SUGGESTIONS } from "#/utils/suggestions";
import { convertImageToBase64 } from "#/utils/convert-image-to-base-64";
@@ -11,15 +14,16 @@ import { ImageCarousel } from "../features/images/image-carousel";
import { UploadImageInput } from "../features/images/upload-image-input";
import { useCreateConversation } from "#/hooks/mutation/use-create-conversation";
import { LoadingSpinner } from "./loading-spinner";
import { useInitialQuery } from "#/hooks/query/use-initial-query";
interface TaskFormProps {
ref: React.RefObject<HTMLFormElement | null>;
}
export function TaskForm({ ref }: TaskFormProps) {
const dispatch = useDispatch();
const navigation = useNavigation();
const { files, addFile, removeFile } = useInitialQuery();
const { files } = useSelector((state: RootState) => state.initialQuery);
const [text, setText] = React.useState("");
const [suggestion, setSuggestion] = React.useState(() => {
@@ -52,7 +56,7 @@ export function TaskForm({ ref }: TaskFormProps) {
};
return (
<div className="flex flex-col gap-1 w-full">
<div className="flex flex-col gap-2 w-full">
<form
ref={ref}
onSubmit={handleSubmit}
@@ -87,7 +91,7 @@ export function TaskForm({ ref }: TaskFormProps) {
const promises = imageFiles.map(convertImageToBase64);
const base64Images = await Promise.all(promises);
base64Images.forEach((base64) => {
addFile(base64);
dispatch(addFile(base64));
});
}}
value={text}
@@ -105,7 +109,7 @@ export function TaskForm({ ref }: TaskFormProps) {
const promises = uploadedFiles.map(convertImageToBase64);
const base64Images = await Promise.all(promises);
base64Images.forEach((base64) => {
addFile(base64);
dispatch(addFile(base64));
});
}}
label={<AttachImageLabel />}
@@ -114,7 +118,7 @@ export function TaskForm({ ref }: TaskFormProps) {
<ImageCarousel
size="large"
images={files}
onRemove={(index) => removeFile(index)}
onRemove={(index) => dispatch(removeFile(index))}
/>
)}
</div>

View File

@@ -0,0 +1,74 @@
import React from "react";
import { MutateOptions } from "@tanstack/react-query";
import { useSettings } from "#/hooks/query/use-settings";
import { useSaveSettings } from "#/hooks/mutation/use-save-settings";
import { PostSettings, Settings } from "#/types/settings";
import { retrieveAxiosErrorMessage } from "#/utils/retrieve-axios-error-message";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
type SaveUserSettingsConfig = {
onSuccess: MutateOptions<void, Error, Partial<PostSettings>>["onSuccess"];
};
interface SettingsContextType {
saveUserSettings: (
newSettings: Partial<PostSettings>,
config?: SaveUserSettingsConfig,
) => Promise<void>;
settings: Settings | undefined;
}
const SettingsContext = React.createContext<SettingsContextType | undefined>(
undefined,
);
interface SettingsProviderProps {
children: React.ReactNode;
}
export function SettingsProvider({ children }: SettingsProviderProps) {
const { data: userSettings } = useSettings();
const { mutateAsync: saveSettings } = useSaveSettings();
const saveUserSettings = async (
newSettings: Partial<PostSettings>,
config?: SaveUserSettingsConfig,
) => {
const updatedSettings: Partial<PostSettings> = {
...userSettings,
...newSettings,
};
if (updatedSettings.LLM_API_KEY === "**********") {
delete updatedSettings.LLM_API_KEY;
}
await saveSettings(updatedSettings, {
onSuccess: config?.onSuccess,
onError: (error) => {
const errorMessage = retrieveAxiosErrorMessage(error);
displayErrorToast(errorMessage);
},
});
};
const value = React.useMemo(
() => ({
saveUserSettings,
settings: userSettings,
}),
[saveUserSettings, userSettings],
);
return <SettingsContext value={value}>{children}</SettingsContext>;
}
export function useCurrentSettings() {
const context = React.useContext(SettingsContext);
if (context === undefined) {
throw new Error(
"useCurrentSettings must be used within a SettingsProvider",
);
}
return context;
}

View File

@@ -49,12 +49,14 @@ interface UseWsClient {
isLoadingMessages: boolean;
events: Record<string, unknown>[];
send: (event: Record<string, unknown>) => void;
pendingMessages: Record<string, unknown>[];
}
const WsClientContext = React.createContext<UseWsClient>({
status: WsClientProviderStatus.DISCONNECTED,
isLoadingMessages: true,
events: [],
pendingMessages: [],
send: () => {
throw new Error("not connected");
},
@@ -109,26 +111,43 @@ export function WsClientProvider({
WsClientProviderStatus.DISCONNECTED,
);
const [events, setEvents] = React.useState<Record<string, unknown>[]>([]);
const [pendingMessages, setPendingMessages] = React.useState<
Record<string, unknown>[]
>([]);
const lastEventRef = React.useRef<Record<string, unknown> | null>(null);
const messageRateHandler = useRate({ threshold: 250 });
// Private function to queue messages for later sending
const queueMessage = (event: Record<string, unknown>) => {
EventLogger.info(`Queueing message: ${JSON.stringify(event)}`);
setPendingMessages((prev) => [...prev, event]);
};
function send(event: Record<string, unknown>) {
if (!sioRef.current) {
EventLogger.error("WebSocket is not connected.");
EventLogger.info("WebSocket is not connected, queueing message");
queueMessage(event);
return;
}
sioRef.current.emit("oh_user_action", event);
// Send the message to the backend
EventLogger.info(`Sending message: ${JSON.stringify(event)}`);
sioRef.current.emit("oh_action", event);
}
function handleConnect() {
setStatus(WsClientProviderStatus.CONNECTED);
EventLogger.info(
`WebSocket connected. Pending messages: ${pendingMessages.length}`,
);
}
function handleMessage(event: Record<string, unknown>) {
if (isOpenHandsEvent(event) && isMessageAction(event)) {
messageRateHandler.record(new Date().getTime());
}
setEvents((prevEvents) => [...prevEvents, event]);
if (!Number.isNaN(parseInt(event.id as string, 10))) {
lastEventRef.current = event;
@@ -145,14 +164,39 @@ export function WsClientProvider({
}
sio.io.opts.query = sio.io.opts.query || {};
sio.io.opts.query.latest_event_id = lastEventRef.current?.id;
EventLogger.info(
`WebSocket disconnected. Latest event ID: ${lastEventRef.current?.id}`,
);
updateStatusWhenErrorMessagePresent(data);
}
function handleError(data: unknown) {
setStatus(WsClientProviderStatus.DISCONNECTED);
EventLogger.error(`WebSocket connection error: ${JSON.stringify(data)}`);
updateStatusWhenErrorMessagePresent(data);
}
// Process any pending messages when the WebSocket connects
React.useEffect(() => {
if (
status === WsClientProviderStatus.CONNECTED &&
pendingMessages.length > 0 &&
sioRef.current
) {
// We're connected and have pending messages
EventLogger.info(
`Connected! Sending ${pendingMessages.length} queued messages`,
);
pendingMessages.forEach((event) => {
sioRef.current?.emit("oh_action", event);
});
setPendingMessages([]);
EventLogger.info("All queued messages sent, queue cleared");
}
}, [status, pendingMessages.length]);
React.useEffect(() => {
lastEventRef.current = null;
}, [conversationId]);
@@ -210,9 +254,10 @@ export function WsClientProvider({
status,
isLoadingMessages: messageRateHandler.isUnderThreshold,
events,
pendingMessages,
send,
}),
[status, messageRateHandler.isUnderThreshold, events],
[status, messageRateHandler.isUnderThreshold, events, pendingMessages],
);
return <WsClientContext value={value}>{children}</WsClientContext>;

View File

@@ -11,11 +11,12 @@ import { hydrateRoot } from "react-dom/client";
import { Provider } from "react-redux";
import posthog from "posthog-js";
import "./i18n";
import { QueryClientProvider } from "@tanstack/react-query";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import store from "./store";
import { useConfig } from "./hooks/query/use-config";
import { AuthProvider } from "./context/auth-context";
import { initializeBridge, queryClient } from "./query-redux-bridge-init";
import { queryClientConfig } from "./query-client-config";
import { SettingsProvider } from "./context/settings-context";
function PosthogInit() {
const { data: config } = useConfig();
@@ -45,12 +46,9 @@ async function prepareApp() {
}
}
// queryClient is now imported from query-redux-bridge-init.ts
prepareApp().then(() => {
// Initialize the bridge and mark status slice as migrated
initializeBridge();
export const queryClient = new QueryClient(queryClientConfig);
prepareApp().then(() =>
startTransition(() => {
hydrateRoot(
document,
@@ -58,12 +56,14 @@ prepareApp().then(() => {
<Provider store={store}>
<AuthProvider>
<QueryClientProvider client={queryClient}>
<HydratedRouter />
<PosthogInit />
<SettingsProvider>
<HydratedRouter />
<PosthogInit />
</SettingsProvider>
</QueryClientProvider>
</AuthProvider>
</Provider>
</StrictMode>,
);
});
});
}),
);

View File

@@ -1,17 +1,32 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useNavigate } from "react-router";
import posthog from "posthog-js";
import { useDispatch, useSelector } from "react-redux";
import OpenHands from "#/api/open-hands";
import { useInitialQuery } from "#/hooks/query/use-initial-query";
import { setInitialPrompt } from "#/state/initial-query-slice";
import { RootState } from "#/store";
export const useCreateConversation = () => {
const navigate = useNavigate();
const dispatch = useDispatch();
const queryClient = useQueryClient();
const { selectedRepository, files, setInitialPrompt } = useInitialQuery();
const { selectedRepository, files, importedProjectZip } = useSelector(
(state: RootState) => state.initialQuery,
);
return useMutation({
mutationFn: async (variables: { q?: string }) => {
if (variables.q) setInitialPrompt(variables.q);
if (
!variables.q?.trim() &&
!selectedRepository &&
files.length === 0 &&
!importedProjectZip
) {
throw new Error("No query provided");
}
if (variables.q) dispatch(setInitialPrompt(variables.q));
return OpenHands.createConversation(
selectedRepository || undefined,

View File

@@ -2,7 +2,6 @@ import { useMutation, useQueryClient } from "@tanstack/react-query";
import { DEFAULT_SETTINGS } from "#/services/settings";
import OpenHands from "#/api/open-hands";
import { PostSettings, PostApiSettings } from "#/types/settings";
import { useSettings } from "../query/use-settings";
const saveSettingsMutationFn = async (settings: Partial<PostSettings>) => {
const resetLlmApiKey = settings.LLM_API_KEY === "";
@@ -30,25 +29,9 @@ const saveSettingsMutationFn = async (settings: Partial<PostSettings>) => {
export const useSaveSettings = () => {
const queryClient = useQueryClient();
const { data: currentSettings } = useSettings();
return useMutation({
mutationFn: async (settings: Partial<PostSettings>) => {
const newSettings = { ...currentSettings, ...settings };
// Temp hack for reset logic
if (
settings.LLM_API_KEY === undefined &&
settings.LLM_BASE_URL === undefined &&
settings.LLM_MODEL === undefined
) {
delete newSettings.LLM_API_KEY;
delete newSettings.LLM_BASE_URL;
delete newSettings.LLM_MODEL;
}
await saveSettingsMutationFn(newSettings);
},
mutationFn: saveSettingsMutationFn,
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["settings"] });
},

View File

@@ -1,6 +1,7 @@
import { useQuery } from "@tanstack/react-query";
import { useConfig } from "./use-config";
import OpenHands from "#/api/open-hands";
import { BILLING_SETTINGS } from "#/utils/feature-flags";
export const useBalance = () => {
const { data: config } = useConfig();
@@ -8,7 +9,6 @@ export const useBalance = () => {
return useQuery({
queryKey: ["user", "balance"],
queryFn: OpenHands.getBalance,
enabled:
config?.APP_MODE === "saas" && config?.FEATURE_FLAGS.ENABLE_BILLING,
enabled: config?.APP_MODE === "saas" && BILLING_SETTINGS(),
});
};

View File

@@ -5,13 +5,13 @@ import { useConfig } from "./use-config";
import OpenHands from "#/api/open-hands";
import { useAuth } from "#/context/auth-context";
import { useLogout } from "../mutation/use-logout";
import { useSaveSettings } from "../mutation/use-save-settings";
import { useCurrentSettings } from "#/context/settings-context";
export const useGitHubUser = () => {
const { githubTokenIsSet } = useAuth();
const { setGitHubTokenIsSet } = useAuth();
const { mutateAsync: logout } = useLogout();
const { mutate: saveUserSettings } = useSaveSettings();
const { saveUserSettings } = useCurrentSettings();
const { data: config } = useConfig();
const user = useQuery({
@@ -38,7 +38,7 @@ export const useGitHubUser = () => {
const handleLogout = async () => {
if (config?.APP_MODE === "saas") await logout();
else {
saveUserSettings({ unset_github_token: true });
await saveUserSettings({ unset_github_token: true });
setGitHubTokenIsSet(false);
}
posthog.reset();

View File

@@ -1,294 +0,0 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { getQueryReduxBridge } from "#/utils/query-redux-bridge";
interface InitialQueryState {
files: string[]; // base64 encoded images
initialPrompt: string | null;
selectedRepository: string | null;
}
// Initial state
const initialState: InitialQueryState = {
files: [],
initialPrompt: null,
selectedRepository: null,
};
/**
* Hook to access and manipulate initial query data using React Query
* This replaces the Redux initialQuery slice functionality
*/
export function useInitialQuery() {
const queryClient = useQueryClient();
// Try to get the bridge, but don't throw if it's not initialized (for tests)
let bridge: ReturnType<typeof getQueryReduxBridge> | null = null;
try {
bridge = getQueryReduxBridge();
} catch (error) {
// In tests, we might not have the bridge initialized
console.warn(
"QueryReduxBridge not initialized, using default initial query state",
);
}
// Get initial state from Redux if this is the first time accessing the data
const getInitialQueryState = (): InitialQueryState => {
// If we already have data in React Query, use that
const existingData = queryClient.getQueryData<InitialQueryState>([
"initialQuery",
]);
if (existingData) return existingData;
// Otherwise, get initial data from Redux if bridge is available
if (bridge) {
try {
return bridge.getReduxSliceState<InitialQueryState>("initialQuery");
} catch (error) {
// If we can't get the state from Redux, return the initial state
return initialState;
}
}
// If bridge is not available, return the initial state
return initialState;
};
// Query for initial query state
const query = useQuery({
queryKey: ["initialQuery"],
queryFn: () => getInitialQueryState(),
initialData: getInitialQueryState,
staleTime: Infinity, // We manage updates manually through mutations
});
// Mutation to add a file
const addFileMutation = useMutation({
mutationFn: (file: string) => Promise.resolve(file),
onMutate: async (file) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({ queryKey: ["initialQuery"] });
// Get current state
const previousState = queryClient.getQueryData<InitialQueryState>([
"initialQuery",
]);
// Update state
if (previousState) {
queryClient.setQueryData<InitialQueryState>(["initialQuery"], {
...previousState,
files: [...previousState.files, file],
});
}
return { previousState };
},
onError: (_, __, context) => {
// Restore previous state on error
if (context?.previousState) {
queryClient.setQueryData(["initialQuery"], context.previousState);
}
},
});
// Mutation to remove a file
const removeFileMutation = useMutation({
mutationFn: (index: number) => Promise.resolve(index),
onMutate: async (index) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({ queryKey: ["initialQuery"] });
// Get current state
const previousState = queryClient.getQueryData<InitialQueryState>([
"initialQuery",
]);
// Update state
if (previousState) {
const newFiles = [...previousState.files];
newFiles.splice(index, 1);
queryClient.setQueryData<InitialQueryState>(["initialQuery"], {
...previousState,
files: newFiles,
});
}
return { previousState };
},
onError: (_, __, context) => {
// Restore previous state on error
if (context?.previousState) {
queryClient.setQueryData(["initialQuery"], context.previousState);
}
},
});
// Mutation to clear files
const clearFilesMutation = useMutation({
mutationFn: () => Promise.resolve(),
onMutate: async () => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({ queryKey: ["initialQuery"] });
// Get current state
const previousState = queryClient.getQueryData<InitialQueryState>([
"initialQuery",
]);
// Update state
if (previousState) {
queryClient.setQueryData<InitialQueryState>(["initialQuery"], {
...previousState,
files: [],
});
}
return { previousState };
},
onError: (_, __, context) => {
// Restore previous state on error
if (context?.previousState) {
queryClient.setQueryData(["initialQuery"], context.previousState);
}
},
});
// Mutation to set initial prompt
const setInitialPromptMutation = useMutation({
mutationFn: (prompt: string) => Promise.resolve(prompt),
onMutate: async (prompt) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({ queryKey: ["initialQuery"] });
// Get current state
const previousState = queryClient.getQueryData<InitialQueryState>([
"initialQuery",
]);
// Update state
if (previousState) {
queryClient.setQueryData<InitialQueryState>(["initialQuery"], {
...previousState,
initialPrompt: prompt,
});
}
return { previousState };
},
onError: (_, __, context) => {
// Restore previous state on error
if (context?.previousState) {
queryClient.setQueryData(["initialQuery"], context.previousState);
}
},
});
// Mutation to clear initial prompt
const clearInitialPromptMutation = useMutation({
mutationFn: () => Promise.resolve(),
onMutate: async () => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({ queryKey: ["initialQuery"] });
// Get current state
const previousState = queryClient.getQueryData<InitialQueryState>([
"initialQuery",
]);
// Update state
if (previousState) {
queryClient.setQueryData<InitialQueryState>(["initialQuery"], {
...previousState,
initialPrompt: null,
});
}
return { previousState };
},
onError: (_, __, context) => {
// Restore previous state on error
if (context?.previousState) {
queryClient.setQueryData(["initialQuery"], context.previousState);
}
},
});
// Mutation to set selected repository
const setSelectedRepositoryMutation = useMutation({
mutationFn: (repository: string | null) => Promise.resolve(repository),
onMutate: async (repository) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({ queryKey: ["initialQuery"] });
// Get current state
const previousState = queryClient.getQueryData<InitialQueryState>([
"initialQuery",
]);
// Update state
if (previousState) {
queryClient.setQueryData<InitialQueryState>(["initialQuery"], {
...previousState,
selectedRepository: repository,
});
}
return { previousState };
},
onError: (_, __, context) => {
// Restore previous state on error
if (context?.previousState) {
queryClient.setQueryData(["initialQuery"], context.previousState);
}
},
});
// Mutation to clear selected repository
const clearSelectedRepositoryMutation = useMutation({
mutationFn: () => Promise.resolve(),
onMutate: async () => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({ queryKey: ["initialQuery"] });
// Get current state
const previousState = queryClient.getQueryData<InitialQueryState>([
"initialQuery",
]);
// Update state
if (previousState) {
queryClient.setQueryData<InitialQueryState>(["initialQuery"], {
...previousState,
selectedRepository: null,
});
}
return { previousState };
},
onError: (_, __, context) => {
// Restore previous state on error
if (context?.previousState) {
queryClient.setQueryData(["initialQuery"], context.previousState);
}
},
});
return {
// State
files: query.data?.files || initialState.files,
initialPrompt: query.data?.initialPrompt || initialState.initialPrompt,
selectedRepository:
query.data?.selectedRepository || initialState.selectedRepository,
isLoading: query.isLoading,
// Actions
addFile: addFileMutation.mutate,
removeFile: removeFileMutation.mutate,
clearFiles: clearFilesMutation.mutate,
setInitialPrompt: setInitialPromptMutation.mutate,
clearInitialPrompt: clearInitialPromptMutation.mutate,
setSelectedRepository: setSelectedRepositoryMutation.mutate,
clearSelectedRepository: clearSelectedRepositoryMutation.mutate,
};
}

View File

@@ -1,95 +0,0 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { getQueryReduxBridge } from "#/utils/query-redux-bridge";
interface MetricsState {
cost: number | null;
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
} | null;
}
// Initial metrics state
const initialMetrics: MetricsState = {
cost: null,
usage: null,
};
/**
* Hook to access and manipulate metrics data using React Query
* This replaces the Redux metrics slice functionality
*/
export function useMetrics() {
const queryClient = useQueryClient();
// Try to get the bridge, but don't throw if it's not initialized (for tests)
let bridge: ReturnType<typeof getQueryReduxBridge> | null = null;
try {
bridge = getQueryReduxBridge();
} catch (error) {
// In tests, we might not have the bridge initialized
console.warn("QueryReduxBridge not initialized, using default metrics");
}
// Get initial state from Redux if this is the first time accessing the data
const getInitialMetrics = (): MetricsState => {
// If we already have data in React Query, use that
const existingData = queryClient.getQueryData<MetricsState>(["metrics"]);
if (existingData) return existingData;
// Otherwise, get initial data from Redux if bridge is available
if (bridge) {
try {
return bridge.getReduxSliceState<MetricsState>("metrics");
} catch (error) {
// If we can't get the state from Redux, return the initial state
return initialMetrics;
}
}
// If bridge is not available, return the initial state
return initialMetrics;
};
// Query for metrics
const query = useQuery({
queryKey: ["metrics"],
queryFn: () => getInitialMetrics(),
initialData: getInitialMetrics,
staleTime: Infinity, // We manage updates manually through mutations
});
// Mutation to set metrics
const setMetricsMutation = useMutation({
mutationFn: (metrics: MetricsState) => Promise.resolve(metrics),
onMutate: async (metrics) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({
queryKey: ["metrics"],
});
// Get current metrics
const previousMetrics = queryClient.getQueryData<MetricsState>([
"metrics",
]);
// Update metrics
queryClient.setQueryData(["metrics"], metrics);
return { previousMetrics };
},
onError: (_, __, context) => {
// Restore previous metrics on error
if (context?.previousMetrics) {
queryClient.setQueryData(["metrics"], context.previousMetrics);
}
},
});
return {
metrics: query.data || initialMetrics,
isLoading: query.isLoading,
setMetrics: setMetricsMutation.mutate,
};
}

View File

@@ -44,13 +44,13 @@ export const useSettings = () => {
});
React.useEffect(() => {
if (query.isFetched && query.data?.LLM_API_KEY) {
if (query.data?.LLM_API_KEY) {
posthog.capture("user_activated");
}
}, [query.data?.LLM_API_KEY, query.isFetched]);
}, [query.data?.LLM_API_KEY]);
React.useEffect(() => {
if (query.isFetched) setGitHubTokenIsSet(!!query.data?.GITHUB_TOKEN_IS_SET);
setGitHubTokenIsSet(!!query.data?.GITHUB_TOKEN_IS_SET);
}, [query.data?.GITHUB_TOKEN_IS_SET, query.isFetched]);
// We want to return the defaults if the settings aren't found so the user can still see the

View File

@@ -1,101 +0,0 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { getQueryReduxBridge } from "#/utils/query-redux-bridge";
import { StatusMessage } from "#/types/message";
// Initial status message
const initialStatusMessage: StatusMessage = {
status_update: true,
type: "info",
id: "",
message: "",
};
/**
* Hook to access and manipulate status messages using React Query
* This replaces the Redux status slice functionality
*/
export function useStatusMessage() {
const queryClient = useQueryClient();
// Try to get the bridge, but don't throw if it's not initialized (for tests)
let bridge: ReturnType<typeof getQueryReduxBridge> | null = null;
try {
bridge = getQueryReduxBridge();
} catch (error) {
// In tests, we might not have the bridge initialized
console.warn(
"QueryReduxBridge not initialized, using default status message",
);
}
// Get initial state from Redux if this is the first time accessing the data
const getInitialStatusMessage = (): StatusMessage => {
// If we already have data in React Query, use that
const existingData = queryClient.getQueryData<StatusMessage>([
"status",
"currentMessage",
]);
if (existingData) return existingData;
// Otherwise, get initial data from Redux if bridge is available
if (bridge) {
try {
return bridge.getReduxSliceState<{ curStatusMessage: StatusMessage }>(
"status",
).curStatusMessage;
} catch (error) {
// If we can't get the state from Redux, return the initial state
return initialStatusMessage;
}
}
// If bridge is not available, return the initial state
return initialStatusMessage;
};
// Query for status message
const query = useQuery({
queryKey: ["status", "currentMessage"],
queryFn: () => getInitialStatusMessage(),
initialData: getInitialStatusMessage,
staleTime: Infinity, // We manage updates manually through mutations
});
// Mutation to set current status message
const setStatusMessageMutation = useMutation({
mutationFn: (statusMessage: StatusMessage) =>
Promise.resolve(statusMessage),
onMutate: async (statusMessage) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({
queryKey: ["status", "currentMessage"],
});
// Get current status message
const previousStatusMessage = queryClient.getQueryData<StatusMessage>([
"status",
"currentMessage",
]);
// Update status message
queryClient.setQueryData(["status", "currentMessage"], statusMessage);
return { previousStatusMessage };
},
onError: (_, __, context) => {
// Restore previous status message on error
if (context?.previousStatusMessage) {
queryClient.setQueryData(
["status", "currentMessage"],
context.previousStatusMessage,
);
}
},
});
return {
statusMessage: query.data || initialStatusMessage,
isLoading: query.isLoading,
setStatusMessage: setStatusMessageMutation.mutate,
};
}

View File

@@ -12,7 +12,9 @@ export const useVSCodeUrl = (config: { enabled: boolean }) => {
return OpenHands.getVSCodeUrl(conversationId);
},
enabled: !!conversationId && config.enabled,
refetchOnMount: true,
refetchOnMount: false,
staleTime: 1000 * 60 * 5, // 5 minutes
gcTime: 1000 * 60 * 15, // 15 minutes
});
return data;

View File

@@ -1,15 +1,15 @@
import { useCurrentSettings } from "#/context/settings-context";
import { useLogout } from "./mutation/use-logout";
import { useSaveSettings } from "./mutation/use-save-settings";
import { useConfig } from "./query/use-config";
export const useAppLogout = () => {
const { data: config } = useConfig();
const { mutateAsync: logout } = useLogout();
const { mutate: saveUserSettings } = useSaveSettings();
const { saveUserSettings } = useCurrentSettings();
const handleLogout = async () => {
if (config?.APP_MODE === "saas") await logout();
else saveUserSettings({ unset_github_token: true });
else await saveUserSettings({ unset_github_token: true });
};
return { handleLogout };

View File

@@ -1,82 +0,0 @@
import { useEffect } from "react";
import { useParams } from "react-router";
import { useSelector, useDispatch } from "react-redux";
import { useQueryClient } from "@tanstack/react-query";
import { useUpdateConversation } from "./mutation/use-update-conversation";
import { RootState } from "#/store";
import OpenHands from "#/api/open-hands";
import { useUserConversation } from "#/hooks/query/use-user-conversation";
const defaultTitlePattern = /^Conversation [a-f0-9]+$/;
/**
* Hook that monitors for the first agent message and triggers title generation.
* This approach is more robust as it ensures the user message has been processed
* by the backend and the agent has responded before generating the title.
*/
export function useAutoTitle() {
const { conversationId } = useParams<{ conversationId: string }>();
const { data: conversation } = useUserConversation(conversationId ?? null);
const queryClient = useQueryClient();
const dispatch = useDispatch();
const { mutate: updateConversation } = useUpdateConversation();
const messages = useSelector((state: RootState) => state.chat.messages);
useEffect(() => {
if (
!conversation ||
!conversationId ||
!messages ||
messages.length === 0
) {
return;
}
const hasAgentMessage = messages.some(
(message) => message.sender === "assistant",
);
const hasUserMessage = messages.some(
(message) => message.sender === "user",
);
if (!hasAgentMessage || !hasUserMessage) {
return;
}
if (conversation.title && !defaultTitlePattern.test(conversation.title)) {
return;
}
updateConversation(
{
id: conversationId,
conversation: { title: "" },
},
{
onSuccess: async () => {
try {
const updatedConversation =
await OpenHands.getConversation(conversationId);
queryClient.setQueryData(
["user", "conversation", conversationId],
updatedConversation,
);
} catch (error) {
queryClient.invalidateQueries({
queryKey: ["user", "conversation", conversationId],
});
}
},
},
);
}, [
messages,
conversationId,
conversation,
updateConversation,
queryClient,
dispatch,
]);
}

View File

@@ -1,30 +0,0 @@
import { useParams } from "react-router";
import { useEffect, useRef } from "react";
import { useUserConversation } from "./query/use-user-conversation";
/**
* Hook that updates the document title based on the current conversation.
* This ensures that any changes to the conversation title are reflected in the document title.
*
* @param suffix Optional suffix to append to the title (default: "OpenHands")
*/
export function useDocumentTitleFromState(suffix = "OpenHands") {
const params = useParams();
const { data: conversation } = useUserConversation(
params.conversationId ?? null,
);
const lastValidTitleRef = useRef<string | null>(null);
useEffect(() => {
if (conversation?.title) {
lastValidTitleRef.current = conversation.title;
document.title = `${conversation.title} - ${suffix}`;
} else {
document.title = suffix;
}
return () => {
document.title = suffix;
};
}, [conversation, suffix]);
}

View File

@@ -0,0 +1,80 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { downloadFiles } from "#/utils/download-files";
import { DownloadProgressState } from "#/components/shared/download-progress";
import { useConversation } from "#/context/conversation-context";
export const INITIAL_PROGRESS: DownloadProgressState = {
filesTotal: 0,
filesDownloaded: 0,
currentFile: "",
totalBytesDownloaded: 0,
bytesDownloadedPerSecond: 0,
isDiscoveringFiles: true,
};
export function useDownloadProgress(
initialPath: string | undefined,
onClose: () => void,
) {
const [isStarted, setIsStarted] = useState(false);
const [progress, setProgress] =
useState<DownloadProgressState>(INITIAL_PROGRESS);
const progressRef = useRef<DownloadProgressState>(INITIAL_PROGRESS);
const abortController = useRef<AbortController>(null);
const { conversationId } = useConversation();
// Create AbortController on mount
useEffect(() => {
const controller = new AbortController();
abortController.current = controller;
// Initialize progress ref with initial state
progressRef.current = INITIAL_PROGRESS;
return () => {
controller.abort();
abortController.current = null;
};
}, []); // Empty deps array - only run on mount/unmount
// Start download when isStarted becomes true
useEffect(() => {
if (!isStarted) {
setIsStarted(true);
return;
}
if (!abortController.current) return;
// Start download
const download = async () => {
try {
await downloadFiles(conversationId, initialPath, {
onProgress: (p) => {
// Update both the ref and state
progressRef.current = { ...p };
setProgress((prev: DownloadProgressState) => ({ ...prev, ...p }));
},
signal: abortController.current!.signal,
});
onClose();
} catch (error) {
if (error instanceof Error && error.message === "Download cancelled") {
onClose();
} else {
throw error;
}
}
};
download();
}, [initialPath, onClose, isStarted]);
// No longer need startDownload as it's handled in useEffect
const cancelDownload = useCallback(() => {
abortController.current?.abort();
}, []);
return {
progress,
cancelDownload,
};
}

View File

@@ -5,18 +5,17 @@ import {
setScreenshotSrc,
setUrl,
} from "#/state/browser-slice";
import { useInitialQuery } from "#/hooks/query/use-initial-query";
import { clearSelectedRepository } from "#/state/initial-query-slice";
export const useEndSession = () => {
const navigate = useNavigate();
const dispatch = useDispatch();
const { clearSelectedRepository } = useInitialQuery();
/**
* End the current session by clearing the token and redirecting to the home page.
*/
const endSession = () => {
clearSelectedRepository();
dispatch(clearSelectedRepository());
// Reset browser state to initial values
dispatch(setUrl(browserInitialState.url));

View File

@@ -1,9 +1,9 @@
import React from "react";
import { useCurrentSettings } from "#/context/settings-context";
import { handleCaptureConsent } from "#/utils/handle-capture-consent";
import { useSaveSettings } from "./mutation/use-save-settings";
export const useMigrateUserConsent = () => {
const { mutate: saveUserSettings } = useSaveSettings();
const { saveUserSettings } = useCurrentSettings();
/**
* Migrate user consent to the settings store on the server.

View File

@@ -1,9 +1,9 @@
import { useCallback, useRef } from "react";
import notificationSound from "../assets/notification.mp3";
import { useSettings } from "./query/use-settings";
import { useCurrentSettings } from "../context/settings-context";
export const useNotification = () => {
const { data: settings } = useSettings();
const { settings } = useCurrentSettings();
const audioRef = useRef<HTMLAudioElement | undefined>(undefined);
// Initialize audio only in browser environment

View File

@@ -222,7 +222,6 @@ export enum I18nKey {
STATUS$STARTING_CONTAINER = "STATUS$STARTING_CONTAINER",
STATUS$PREPARING_CONTAINER = "STATUS$PREPARING_CONTAINER",
STATUS$CONTAINER_STARTED = "STATUS$CONTAINER_STARTED",
STATUS$SETTING_UP_WORKSPACE = "STATUS$SETTING_UP_WORKSPACE",
ACCOUNT_SETTINGS_MODAL$DISCONNECT = "ACCOUNT_SETTINGS_MODAL$DISCONNECT",
ACCOUNT_SETTINGS_MODAL$SAVE = "ACCOUNT_SETTINGS_MODAL$SAVE",
ACCOUNT_SETTINGS_MODAL$CLOSE = "ACCOUNT_SETTINGS_MODAL$CLOSE",

View File

@@ -3308,21 +3308,6 @@
"tr": "Konteyner başlatıldı.",
"ja": "コンテナが開始されました"
},
"STATUS$SETTING_UP_WORKSPACE": {
"en": "Setting up workspace...",
"zh-CN": "正在设置工作区...",
"zh-TW": "正在設置工作區...",
"de": "Arbeitsbereich wird eingerichtet...",
"ko-KR": "작업 공간을 설정하는 중...",
"no": "Setter opp arbeidsområde...",
"it": "Configurazione dell'area di lavoro...",
"pt": "Configurando espaço de trabalho...",
"es": "Configurando espacio de trabajo...",
"ar": "جاري إعداد مساحة العمل...",
"fr": "Configuration de l'espace de travail...",
"tr": "Çalışma alanı ayarlanıyor...",
"ja": "ワークスペースを設定中..."
},
"ACCOUNT_SETTINGS_MODAL$DISCONNECT": {
"en": "Disconnect",
"es": "Desconectar",

View File

@@ -128,6 +128,7 @@ const openHandsHandlers = [
const url = new URL(request.url);
const file = url.searchParams.get("file")?.toString();
if (file) {
return HttpResponse.json({ code: `Content of ${file}` });
}
@@ -180,10 +181,6 @@ export const handlers = [
GITHUB_CLIENT_ID: "fake-github-client-id",
POSTHOG_CLIENT_KEY: "fake-posthog-client-key",
STRIPE_PUBLISHABLE_KEY: "",
FEATURE_FLAGS: {
ENABLE_BILLING: mockSaas,
HIDE_LLM_SETTINGS: mockSaas,
},
};
return HttpResponse.json(config);

View File

@@ -35,7 +35,7 @@ export const handlers: WebSocketHandler[] = [
);
}
io.client.on("oh_user_action", async (_, data) => {
io.client.on("oh_action", async (_, data) => {
if (isInitConfig(data)) {
io.client.emit(
"oh_event",

View File

@@ -1,31 +0,0 @@
import { QueryClient } from "@tanstack/react-query";
import {
initQueryReduxBridge,
getQueryReduxBridge,
SliceNames,
} from "./utils/query-redux-bridge";
import { queryClientConfig } from "./query-client-config";
// Create a query client
export const queryClient = new QueryClient(queryClientConfig);
// Initialize the bridge
export function initializeBridge() {
// Initialize the bridge with the query client
initQueryReduxBridge(queryClient);
// Mark slices as migrated to React Query
getQueryReduxBridge().migrateSlice("status");
getQueryReduxBridge().migrateSlice("metrics");
getQueryReduxBridge().migrateSlice("initialQuery");
}
// Export a function to check if a slice is migrated
export function isSliceMigrated(sliceName: SliceNames) {
try {
return getQueryReduxBridge().isSliceMigrated(sliceName);
} catch (error) {
// If the bridge is not initialized, return false
return false;
}
}

View File

@@ -1,13 +1,18 @@
import React from "react";
import { useDispatch } from "react-redux";
import posthog from "posthog-js";
import { setImportedProjectZip } from "#/state/initial-query-slice";
import { convertZipToBase64 } from "#/utils/convert-zip-to-base64";
import { useGitHubUser } from "#/hooks/query/use-github-user";
import { useGitHubAuthUrl } from "#/hooks/use-github-auth-url";
import { useConfig } from "#/hooks/query/use-config";
import { ImportProjectSuggestionBox } from "../../components/features/suggestions/import-project-suggestion-box";
import { GitHubRepositoriesSuggestionBox } from "#/components/features/github/github-repositories-suggestion-box";
import { CodeNotInGitHubLink } from "#/components/features/github/code-not-in-github-link";
import { HeroHeading } from "#/components/shared/hero-heading";
import { TaskForm } from "#/components/shared/task-form";
function Home() {
const dispatch = useDispatch();
const formRef = React.useRef<HTMLFormElement>(null);
const { data: config } = useConfig();
@@ -24,20 +29,29 @@ function Home() {
className="bg-base-secondary h-full rounded-xl flex flex-col items-center justify-center relative overflow-y-auto px-2"
>
<HeroHeading />
<div className="flex flex-col gap-1 w-full mt-8 md:w-[600px] items-center">
<div className="flex flex-col gap-8 w-full md:w-[600px] items-center">
<div className="flex flex-col gap-2 w-full">
<TaskForm ref={formRef} />
</div>
<div className="flex gap-4 w-full flex-col md:flex-row mt-8">
<div className="flex gap-4 w-full flex-col md:flex-row">
<GitHubRepositoriesSuggestionBox
handleSubmit={() => formRef.current?.requestSubmit()}
gitHubAuthUrl={gitHubAuthUrl}
user={user || null}
/>
</div>
<div className="w-full flex justify-start mt-2 ml-2">
<CodeNotInGitHubLink />
<ImportProjectSuggestionBox
onChange={async (event) => {
if (event.target.files) {
const zip = event.target.files[0];
dispatch(setImportedProjectZip(await convertZipToBase64(zip)));
posthog.capture("zip_file_uploaded");
formRef.current?.requestSubmit();
} else {
// TODO: handle error
}
}}
/>
</div>
</div>
</div>

View File

@@ -1,12 +1,44 @@
import { useSelector } from "react-redux";
import React from "react";
import { useDispatch, useSelector } from "react-redux";
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 { RUNTIME_INACTIVE_STATES } from "#/types/agent-state";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
export const useHandleRuntimeActive = () => {
const dispatch = useDispatch();
const { mutate: uploadFiles } = useUploadFiles();
const { curAgentState } = useSelector((state: RootState) => state.agent);
const runtimeActive = !RUNTIME_INACTIVE_STATES.includes(curAgentState);
return { runtimeActive };
const { importedProjectZip } = useSelector(
(state: RootState) => state.initialQuery,
);
const handleUploadFiles = (zip: string) => {
const blob = base64ToBlob(zip);
const file = new File([blob], "imported-project.zip", {
type: blob.type,
});
uploadFiles(
{ files: [file] },
{
onError: () => {
displayErrorToast("Failed to upload project files.");
},
},
);
dispatch(setImportedProjectZip(null));
};
React.useEffect(() => {
if (runtimeActive && importedProjectZip) {
handleUploadFiles(importedProjectZip);
}
}, [runtimeActive, importedProjectZip]);
};

View File

@@ -1,7 +1,7 @@
import { useDisclosure } from "@heroui/react";
import React from "react";
import { Outlet } from "react-router";
import { useDispatch } from "react-redux";
import { useDispatch, useSelector } from "react-redux";
import { FaServer } from "react-icons/fa";
import { useTranslation } from "react-i18next";
import { I18nKey } from "#/i18n/declaration";
@@ -33,9 +33,9 @@ import { useUserConversation } from "#/hooks/query/use-user-conversation";
import { ServedAppLabel } from "#/components/layout/served-app-label";
import { TerminalStatusLabel } from "#/components/features/terminal/terminal-status-label";
import { useSettings } from "#/hooks/query/use-settings";
import { clearFiles, clearInitialPrompt } from "#/state/initial-query-slice";
import { RootState } from "#/store";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
import { useInitialQuery } from "#/hooks/query/use-initial-query";
function AppContent() {
useConversationConfig();
@@ -45,8 +45,9 @@ function AppContent() {
const { data: conversation, isFetched } = useUserConversation(
conversationId || null,
);
const { initialPrompt, files, clearInitialPrompt, clearFiles } =
useInitialQuery();
const { initialPrompt, files } = useSelector(
(state: RootState) => state.initialQuery,
);
const dispatch = useDispatch();
const endSession = useEndSession();
@@ -85,8 +86,8 @@ function AppContent() {
pending: true,
}),
);
clearInitialPrompt();
clearFiles();
dispatch(clearInitialPrompt());
dispatch(clearFiles());
}
}, [conversationId]);

View File

@@ -20,6 +20,7 @@ import { useAuth } from "#/context/auth-context";
import { useMigrateUserConsent } from "#/hooks/use-migrate-user-consent";
import { useBalance } from "#/hooks/query/use-balance";
import { SetupPaymentModal } from "#/components/features/payment/setup-payment-modal";
import { BILLING_SETTINGS } from "#/utils/feature-flags";
import { displaySuccessToast } from "#/utils/custom-toast-handlers";
export function ErrorBoundary() {
@@ -144,7 +145,7 @@ export default function MainApp() {
/>
)}
{config.data?.FEATURE_FLAGS.ENABLE_BILLING &&
{BILLING_SETTINGS() &&
config.data?.APP_MODE === "saas" &&
settings?.IS_NEW_USER && <SetupPaymentModal />}
</div>

View File

@@ -26,6 +26,7 @@ import {
displaySuccessToast,
} from "#/utils/custom-toast-handlers";
import { PostSettings } from "#/types/settings";
import { HIDE_LLM_SETTINGS } from "#/utils/feature-flags";
const REMOTE_RUNTIME_OPTIONS = [
{ key: 1, label: "1x (2 core, 8G)" },
@@ -52,8 +53,7 @@ function AccountSettings() {
const isSuccess = isSuccessfulSettings && isSuccessfulResources;
const isSaas = config?.APP_MODE === "saas";
const shouldHandleSpecialSaasCase =
config?.FEATURE_FLAGS.HIDE_LLM_SETTINGS && isSaas;
const shouldHandleSpecialSaasCase = HIDE_LLM_SETTINGS() && isSaas;
const determineWhetherToToggleAdvancedSettings = () => {
if (shouldHandleSpecialSaasCase) return true;
@@ -288,7 +288,7 @@ function AccountSettings() {
startContent={
isLLMKeySet && <KeyStatusIcon isSet={isLLMKeySet} />
}
placeholder={isLLMKeySet ? "<hidden>" : ""}
placeholder={isLLMKeySet ? "**********" : ""}
/>
)}
@@ -407,9 +407,9 @@ function AccountSettings() {
<KeyStatusIcon isSet={!!isGitHubTokenSet} />
)
}
placeholder={isGitHubTokenSet ? "<hidden>" : ""}
placeholder={isGitHubTokenSet ? "**********" : ""}
/>
<p data-testid="github-token-help-anchor" className="text-xs">
<p data-testId="github-token-help-anchor" className="text-xs">
{" "}
Generate a token on{" "}
<b>

View File

@@ -2,16 +2,17 @@ import { redirect, useSearchParams } from "react-router";
import React from "react";
import { PaymentForm } from "#/components/features/payment/payment-form";
import { GetConfigResponse } from "#/api/open-hands.types";
import { queryClient } from "#/query-redux-bridge-init";
import { queryClient } from "#/entry.client";
import {
displayErrorToast,
displaySuccessToast,
} from "#/utils/custom-toast-handlers";
import { BILLING_SETTINGS } from "#/utils/feature-flags";
export const clientLoader = async () => {
const config = queryClient.getQueryData<GetConfigResponse>(["config"]);
if (config?.APP_MODE !== "saas" || !config.FEATURE_FLAGS.ENABLE_BILLING) {
if (config?.APP_MODE !== "saas" || !BILLING_SETTINGS()) {
return redirect("/settings");
}

View File

@@ -2,11 +2,11 @@ import { NavLink, Outlet } from "react-router";
import SettingsIcon from "#/icons/settings.svg?react";
import { cn } from "#/utils/utils";
import { useConfig } from "#/hooks/query/use-config";
import { BILLING_SETTINGS } from "#/utils/feature-flags";
function SettingsScreen() {
const { data: config } = useConfig();
const isSaas = config?.APP_MODE === "saas";
const billingIsEnabled = config?.FEATURE_FLAGS.ENABLE_BILLING;
return (
<main
@@ -18,7 +18,7 @@ function SettingsScreen() {
<h1 className="text-sm leading-6">Settings</h1>
</header>
{isSaas && billingIsEnabled && (
{isSaas && BILLING_SETTINGS() && (
<nav
data-testid="settings-navbar"
className="flex items-end gap-12 px-11 border-b border-tertiary"

View File

@@ -8,9 +8,8 @@ import { trackError } from "#/utils/error-handler";
import { appendSecurityAnalyzerInput } from "#/state/security-analyzer-slice";
import { setCode, setActiveFilepath } from "#/state/code-slice";
import { appendJupyterInput } from "#/state/jupyter-slice";
// Status and metrics slices are now handled by React Query
import { setCurStatusMessage } from "#/state/status-slice";
import store from "#/store";
import { getQueryReduxBridge } from "#/utils/query-redux-bridge";
import ActionType from "#/types/action-type";
import {
ActionMessage,
@@ -86,32 +85,6 @@ export function handleActionMessage(message: ActionMessage) {
return;
}
// Update metrics if available
if (
message.llm_metrics ||
message.tool_call_metadata?.model_response?.usage
) {
const metrics = {
cost: message.llm_metrics?.accumulated_cost ?? null,
usage: message.tool_call_metadata?.model_response?.usage ?? null,
};
try {
const bridge = getQueryReduxBridge();
if (bridge.isSliceMigrated("metrics")) {
// If metrics slice is migrated, update React Query directly
bridge.syncReduxToQuery(["metrics"], metrics);
} else {
// Otherwise, dispatch to Redux (handled by the bridge)
bridge.conditionalDispatch("metrics", {
type: "metrics/setMetrics",
payload: metrics,
});
}
} catch (error) {
console.warn("Failed to update metrics:", error);
}
}
if (message.action === ActionType.RUN) {
store.dispatch(appendInput(message.args.command));
}
@@ -138,8 +111,11 @@ export function handleActionMessage(message: ActionMessage) {
export function handleStatusMessage(message: StatusMessage) {
if (message.type === "info") {
// Status slice is now handled by React Query
// The websocket events hook will update the React Query cache
store.dispatch(
setCurStatusMessage({
...message,
}),
);
} else if (message.type === "error") {
trackError({
message: message.message,

View File

@@ -0,0 +1,58 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
type SliceState = {
files: string[]; // base64 encoded images
initialPrompt: string | null;
selectedRepository: string | null;
importedProjectZip: string | null; // base64 encoded zip
};
const initialState: SliceState = {
files: [],
initialPrompt: null,
selectedRepository: null,
importedProjectZip: null,
};
export const selectedFilesSlice = createSlice({
name: "initialQuery",
initialState,
reducers: {
addFile(state, action: PayloadAction<string>) {
state.files.push(action.payload);
},
removeFile(state, action: PayloadAction<number>) {
state.files.splice(action.payload, 1);
},
clearFiles(state) {
state.files = [];
},
setInitialPrompt(state, action: PayloadAction<string>) {
state.initialPrompt = action.payload;
},
clearInitialPrompt(state) {
state.initialPrompt = null;
},
setSelectedRepository(state, action: PayloadAction<string | null>) {
state.selectedRepository = action.payload;
},
clearSelectedRepository(state) {
state.selectedRepository = null;
},
setImportedProjectZip(state, action: PayloadAction<string | null>) {
state.importedProjectZip = action.payload;
},
},
});
export const {
addFile,
removeFile,
clearFiles,
setInitialPrompt,
clearInitialPrompt,
setSelectedRepository,
clearSelectedRepository,
setImportedProjectZip,
} = selectedFilesSlice.actions;
export default selectedFilesSlice.reducer;

View File

@@ -1,29 +0,0 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
interface MetricsState {
cost: number | null;
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
} | null;
}
const initialState: MetricsState = {
cost: null,
usage: null,
};
const metricsSlice = createSlice({
name: "metrics",
initialState,
reducers: {
setMetrics: (state, action: PayloadAction<MetricsState>) => {
state.cost = action.payload.cost;
state.usage = action.payload.usage;
},
},
});
export const { setMetrics } = metricsSlice.actions;
export default metricsSlice.reducer;

View File

@@ -0,0 +1,25 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { StatusMessage } from "#/types/message";
const initialStatusMessage: StatusMessage = {
status_update: true,
type: "info",
id: "",
message: "",
};
export const statusSlice = createSlice({
name: "status",
initialState: {
curStatusMessage: initialStatusMessage,
},
reducers: {
setCurStatusMessage: (state, action: PayloadAction<StatusMessage>) => {
state.curStatusMessage = action.payload;
},
},
});
export const { setCurStatusMessage } = statusSlice.actions;
export default statusSlice.reducer;

View File

@@ -4,13 +4,15 @@ import browserReducer from "./state/browser-slice";
import chatReducer from "./state/chat-slice";
import codeReducer from "./state/code-slice";
import fileStateReducer from "./state/file-state-slice";
import initialQueryReducer from "./state/initial-query-slice";
import commandReducer from "./state/command-slice";
import { jupyterReducer } from "./state/jupyter-slice";
import securityAnalyzerReducer from "./state/security-analyzer-slice";
// Status, metrics, and initialQuery slices are now handled by React Query
import statusReducer from "./state/status-slice";
export const rootReducer = combineReducers({
fileState: fileStateReducer,
initialQuery: initialQueryReducer,
browser: browserReducer,
chat: chatReducer,
code: codeReducer,
@@ -18,7 +20,7 @@ export const rootReducer = combineReducers({
agent: agentReducer,
jupyter: jupyterReducer,
securityAnalyzer: securityAnalyzerReducer,
// status, metrics, and initialQuery slices removed (migrated to React Query)
status: statusReducer,
});
const store = configureStore({

View File

@@ -15,22 +15,6 @@ export interface ActionMessage {
// The timestamp of the message
timestamp: string;
// LLM metrics information
llm_metrics?: {
accumulated_cost: number;
};
// Tool call metadata
tool_call_metadata?: {
model_response?: {
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
};
};
}
export interface ObservationMessage {

View File

@@ -0,0 +1,10 @@
export const convertZipToBase64 = async (file: File) => {
const reader = new FileReader();
return new Promise<string>((resolve) => {
reader.onload = () => {
resolve(reader.result as string);
};
reader.readAsDataURL(file);
});
};

View File

@@ -0,0 +1,356 @@
import OpenHands from "#/api/open-hands";
import { downloadWorkspace } from "./download-workspace";
interface DownloadProgress {
filesTotal: number;
filesDownloaded: number;
currentFile: string;
totalBytesDownloaded: number;
bytesDownloadedPerSecond: number;
isDiscoveringFiles: boolean;
}
interface DownloadOptions {
onProgress?: (progress: DownloadProgress) => void;
signal?: AbortSignal;
}
/**
* Checks if the File System Access API is supported
*/
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
*/
async function createSubdirectories(
baseHandle: FileSystemDirectoryHandle,
pathParts: string[],
): Promise<FileSystemDirectoryHandle> {
return pathParts.reduce(async (promise, part) => {
const handle = await promise;
return handle.getDirectoryHandle(part, { create: true });
}, Promise.resolve(baseHandle));
}
/**
* Recursively gets all files in a directory
*/
async function getAllFiles(
conversationID: string,
path: string,
progress: DownloadProgress,
options?: DownloadOptions,
): Promise<string[]> {
const entries = await OpenHands.getFiles(conversationID, path);
const processEntry = async (entry: string): Promise<string[]> => {
if (options?.signal?.aborted) {
throw new Error("Download cancelled");
}
const fullPath = path + entry;
if (entry.endsWith("/")) {
const subEntries = await OpenHands.getFiles(conversationID, fullPath);
const subFilesPromises = subEntries.map((subEntry) =>
processEntry(subEntry),
);
const subFilesArrays = await Promise.all(subFilesPromises);
return subFilesArrays.flat();
}
const updatedProgress = {
...progress,
filesTotal: progress.filesTotal + 1,
currentFile: fullPath,
};
options?.onProgress?.(updatedProgress);
return [fullPath];
};
const filePromises = entries.map((entry) => processEntry(entry));
const fileArrays = await Promise.all(filePromises);
const updatedProgress = {
...progress,
isDiscoveringFiles: false,
};
options?.onProgress?.(updatedProgress);
return fileArrays.flat();
}
/**
* Process a batch of files
*/
async function processBatch(
conversationID: string,
batch: string[],
directoryHandle: FileSystemDirectoryHandle,
progress: DownloadProgress,
startTime: number,
completedFiles: number,
totalBytes: number,
options?: DownloadOptions,
): Promise<{ newCompleted: number; newBytes: number }> {
if (options?.signal?.aborted) {
throw new Error("Download cancelled");
}
// Process files in the batch in parallel
const results = await Promise.all(
batch.map(async (path) => {
try {
const newProgress = {
...progress,
currentFile: path,
isDiscoveringFiles: false,
filesDownloaded: completedFiles,
totalBytesDownloaded: totalBytes,
bytesDownloadedPerSecond:
totalBytes / ((Date.now() - startTime) / 1000),
};
options?.onProgress?.(newProgress);
const content = await OpenHands.getFile(conversationID, path);
// Save to the selected directory preserving structure
const pathParts = path.split("/").filter(Boolean);
const fileName = pathParts.pop() || "file";
const dirHandle =
pathParts.length > 0
? await createSubdirectories(directoryHandle, pathParts)
: directoryHandle;
// Create and write the file
const fileHandle = await dirHandle.getFileHandle(fileName, {
create: true,
});
const writable = await fileHandle.createWritable();
await writable.write(content);
await writable.close();
// Return the size of this file
return new Blob([content]).size;
} catch (error) {
// Silently handle file processing errors and return 0 bytes
return 0;
}
}),
);
// Calculate batch totals
const batchBytes = results.reduce((sum, size) => sum + size, 0);
const newTotalBytes = totalBytes + batchBytes;
const newCompleted =
completedFiles + results.filter((size) => size > 0).length;
// Update progress with batch results
const updatedProgress = {
...progress,
filesDownloaded: newCompleted,
totalBytesDownloaded: newTotalBytes,
bytesDownloadedPerSecond: newTotalBytes / ((Date.now() - startTime) / 1000),
isDiscoveringFiles: false,
};
options?.onProgress?.(updatedProgress);
return {
newCompleted,
newBytes: newTotalBytes,
};
}
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
* @param options Download options including progress callback and abort signal
*/
export async function downloadFiles(
conversationID: string,
initialPath?: string,
options?: DownloadOptions,
): Promise<void> {
const startTime = Date.now();
const progress: DownloadProgress = {
filesTotal: 0, // Will be updated during file discovery
filesDownloaded: 0,
currentFile: "",
totalBytesDownloaded: 0,
bytesDownloadedPerSecond: 0,
isDiscoveringFiles: true,
};
try {
// Check if File System Access API is supported
if (!isFileSystemAccessSupported()) {
throw new Error(
"Your browser doesn't support downloading folders. Please use Chrome, Edge, or another browser that supports the File System Access API.",
);
}
// Show directory picker first
let directoryHandle: FileSystemDirectoryHandle;
try {
directoryHandle = await window.showDirectoryPicker();
} catch (error) {
if (error instanceof Error && error.name === "AbortError") {
throw new Error("Download cancelled");
}
if (error instanceof Error && error.name === "SecurityError") {
throw new Error(
"Permission denied. Please allow access to the download location when prompted.",
);
}
throw new Error("Failed to select download location. Please try again.");
}
// Then recursively get all files
const files = await getAllFiles(
conversationID,
initialPath || "",
progress,
options,
);
// Set isDiscoveringFiles to false now that we have the full list and preserve filesTotal
const finalTotal = progress.filesTotal;
options?.onProgress?.({
...progress,
filesTotal: finalTotal,
isDiscoveringFiles: false,
});
// Verify we still have permission after the potentially long file scan
try {
// Try to create and write to a test file to verify permissions
const testHandle = await directoryHandle.getFileHandle(
".openhands-test",
{ create: true },
);
const writable = await testHandle.createWritable();
await writable.close();
} catch (error) {
if (
error instanceof Error &&
error.message.includes("User activation is required")
) {
// Ask for permission again
try {
directoryHandle = await window.showDirectoryPicker();
} catch (permissionError) {
if (
permissionError instanceof Error &&
permissionError.name === "AbortError"
) {
throw new Error("Download cancelled");
}
if (
permissionError instanceof Error &&
permissionError.name === "SecurityError"
) {
throw new Error(
"Permission denied. Please allow access to the download location when prompted.",
);
}
throw new Error(
"Failed to select download location. Please try again.",
);
}
} else {
throw error;
}
}
// Process files in parallel batches to avoid overwhelming the browser
const BATCH_SIZE = 5;
const batches = Array.from(
{ length: Math.ceil(files.length / BATCH_SIZE) },
(_, i) => files.slice(i * BATCH_SIZE, (i + 1) * BATCH_SIZE),
);
// Keep track of completed files across all batches
let completedFiles = 0;
let totalBytesDownloaded = 0;
// Process batches sequentially to maintain order and avoid overwhelming the browser
await batches.reduce(
(promise, batch) =>
promise.then(async () => {
const { newCompleted, newBytes } = await processBatch(
conversationID,
batch,
directoryHandle,
progress,
startTime,
completedFiles,
totalBytesDownloaded,
options,
);
completedFiles = newCompleted;
totalBytesDownloaded = newBytes;
}),
Promise.resolve(),
);
} catch (error) {
if (error instanceof Error && error.message === "Download cancelled") {
throw error;
}
// Fallback to old style download
if (
error instanceof Error &&
(error.message.includes("browser doesn't support") ||
error.message.includes("Failed to select") ||
error.message.includes("Permission denied"))
) {
await downloadWorkspace(conversationID);
return;
}
// Otherwise, wrap it with a generic message
throw new Error(
`Failed to download files: ${error instanceof Error ? error.message : String(error)}`,
);
}
}

View File

@@ -1,30 +0,0 @@
function isSaveFilePickerSupported(): boolean {
return typeof window !== "undefined" && "showSaveFilePicker" in window;
}
export async function downloadTrajectory(
conversationId: string,
data: unknown[] | null,
): Promise<void> {
if (!isSaveFilePickerSupported()) {
throw new Error(
"Your browser doesn't support downloading files. Please use Chrome, Edge, or another browser that supports the File System Access API.",
);
}
const options: SaveFilePickerOptions = {
suggestedName: `trajectory-${conversationId}.json`,
types: [
{
description: "JSON File",
accept: {
"application/json": [".json"],
},
},
],
};
const fileHandle = await window.showSaveFilePicker(options);
const writable = await fileHandle.createWritable();
await writable.write(JSON.stringify(data, null, 2));
await writable.close();
}

View File

@@ -0,0 +1,16 @@
import OpenHands from "#/api/open-hands";
/**
* Downloads the current workspace as a .zip file.
*/
export const downloadWorkspace = async (conversationId: string) => {
const blob = await OpenHands.getWorkspaceZip(conversationId);
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "workspace.zip");
document.body.appendChild(link);
link.click();
link.parentNode?.removeChild(link);
};

View File

@@ -1,18 +1,28 @@
/* eslint-disable no-console */
/**
* A utility class for logging events. This class will only log events in development mode.
* A utility class for logging events. This class will log events in development mode
* and can be forced to log in any environment by setting FORCE_LOGGING to true.
*/
class EventLogger {
static isDevMode = process.env.NODE_ENV === "development";
static FORCE_LOGGING = false; // Set to false for production, true only for debugging
static shouldLog() {
return this.isDevMode || this.FORCE_LOGGING;
}
/**
* Format and log a message event
* @param event The raw event object
*/
static message(event: MessageEvent) {
if (this.isDevMode) {
console.warn(JSON.stringify(JSON.parse(event.data.toString()), null, 2));
if (this.shouldLog()) {
console.warn(
"[OpenHands]",
JSON.stringify(JSON.parse(event.data.toString()), null, 2),
);
}
}
@@ -22,8 +32,8 @@ class EventLogger {
* @param name The name of the event
*/
static event(event: Event, name?: string) {
if (this.isDevMode) {
console.warn(name || "EVENT", event);
if (this.shouldLog()) {
console.warn("[OpenHands]", name || "EVENT", event);
}
}
@@ -32,8 +42,18 @@ class EventLogger {
* @param warning The warning message
*/
static warning(warning: string) {
if (this.isDevMode) {
console.warn(warning);
if (this.shouldLog()) {
console.warn("[OpenHands]", warning);
}
}
/**
* Log an info message
* @param info The info message
*/
static info(info: string) {
if (this.shouldLog()) {
console.info("[OpenHands]", info);
}
}
@@ -42,8 +62,8 @@ class EventLogger {
* @param error The error message
*/
static error(error: string) {
if (this.isDevMode) {
console.error(error);
if (this.shouldLog()) {
console.error("[OpenHands]", error);
}
}
}

View File

@@ -1,4 +1,4 @@
export function loadFeatureFlag(
function loadFeatureFlag(
flagName: string,
defaultValue: boolean = false,
): boolean {
@@ -11,3 +11,8 @@ export function loadFeatureFlag(
return defaultValue;
}
}
export const BILLING_SETTINGS = () =>
true || loadFeatureFlag("BILLING_SETTINGS");
export const HIDE_LLM_SETTINGS = () =>
true || loadFeatureFlag("HIDE_LLM_SETTINGS");

Some files were not shown because too many files have changed in this diff Show More