π OpenHands: AI-Driven Development
Fetch all workspace subdirectory pages (#1566)
Vasco Schiavoe045dc152177
diff --git a/__tests__/components/features/home/workspace-selection-form.test.tsx b/__tests__/components/features/home/workspace-selection-form.test.tsxindex 5c96e7a06d..21b527417b 100644--- a/__tests__/components/features/home/workspace-selection-form.test.tsx+++ b/__tests__/components/features/home/workspace-selection-form.test.tsx@@ -213,6 +213,48 @@ describe("WorkspaceSelectionForm (server-backed workspaces)", () => {expect(await within(menu).findByText("repo1")).toBeInTheDocument();});+ it("renders parent-derived workspaces from all paginated folders", async () => {+ const workspaceParent = {+ id: "/Users/me/dev",+ name: "dev",+ path: "/Users/me/dev",+ };+ mockSearchSubdirectories.mockImplementation(+ async (path: string, options?: { pageId?: string | null }) => {+ if (path === workspaceParent.path && !options?.pageId) {+ return {+ items: [{ name: "alpha", path: "/Users/me/dev/alpha" }],+ next_page_id: "page-2",+ };+ }+ if (path === workspaceParent.path && options?.pageId === "page-2") {+ return {+ items: [{ name: "paper", path: "/Users/me/dev/paper" }],+ next_page_id: null,+ };+ }+ return { items: [], next_page_id: null };+ },+ );+ renderForm({ workspaceParents: [workspaceParent] });+ const user = userEvent.setup();++ const menu = await openWorkspaceDropdown(user);++ expect(await within(menu).findByText("alpha")).toBeInTheDocument();+ expect(await within(menu).findByText("paper")).toBeInTheDocument();+ expect(mockSearchSubdirectories).toHaveBeenCalledWith(+ workspaceParent.path,+ undefined,+ );+ expect(mockSearchSubdirectories).toHaveBeenCalledWith(+ workspaceParent.path,+ {+ pageId: "page-2",+ },+ );+ });+it("restores the selected workspace after unmounting and remounting", async () => {const workspace = {id: "/Users/me/dev/repo1",@@ -336,6 +378,52 @@ describe("WorkspaceSelectionForm (server-backed workspaces)", () => {]);});+ it("shows all paginated folders in the Add Workspace browser", async () => {+ mockSearchSubdirectories.mockImplementation(+ async (path: string, options?: { pageId?: string | null }) => {+ if (path === "/Users/me" && !options?.pageId) {+ return {+ items: [{ name: "alpha", path: "/Users/me/alpha" }],+ next_page_id: "page-2",+ };+ }+ if (path === "/Users/me" && options?.pageId === "page-2") {+ return {+ items: [+ { name: "paper", path: "/Users/me/paper" },+ { name: "zeta", path: "/Users/me/zeta" },+ ],+ next_page_id: null,+ };+ }+ return { items: [], next_page_id: null };+ },+ );+ renderForm();+ const user = userEvent.setup();++ await user.click(await screen.findByTestId("workspace-dropdown"));+ await user.click(await screen.findByTestId("add-workspaces-button"));+ await screen.findByTestId("folder-browser-modal");++ expect(+ await screen.findByTestId("folder-browser-entry-alpha"),+ ).toBeVisible();+ expect(+ await screen.findByTestId("folder-browser-entry-paper"),+ ).toBeVisible();+ expect(+ await screen.findByTestId("folder-browser-entry-zeta"),+ ).toBeVisible();+ expect(mockSearchSubdirectories).toHaveBeenCalledWith(+ "/Users/me",+ undefined,+ );+ expect(mockSearchSubdirectories).toHaveBeenCalledWith("/Users/me", {+ pageId: "page-2",+ });+ });+it("handles Windows paths when browsing and adding a workspace", async () => {const homePath = String.raw`C:\Users\me`;const devPath = String.raw`C:\Users\me\dev`;diff --git a/src/hooks/query/use-resolved-workspaces.ts b/src/hooks/query/use-resolved-workspaces.tsindex b6c8c2d8e2..7cd1050a11 100644--- a/src/hooks/query/use-resolved-workspaces.ts+++ b/src/hooks/query/use-resolved-workspaces.ts@@ -1,12 +1,9 @@import { useMemo } from "react";import { useQueries } from "@tanstack/react-query";-import {- FileClient,- isAgentServerVersionError,-} from "@openhands/typescript-client/clients";+import { isAgentServerVersionError } from "@openhands/typescript-client/clients";-import { getAgentServerClientOptions } from "#/api/agent-server-client-options";import { useLocalWorkspaces } from "#/hooks/query/use-local-workspaces";+import { searchAllSubdirectories } from "#/hooks/query/use-search-subdirs";import { LocalWorkspace, LocalWorkspaceParent } from "#/types/workspace";interface UseResolvedWorkspacesResult {@@ -82,10 +79,7 @@ export function useResolvedWorkspaces(): UseResolvedWorkspacesResult {? []: workspaceParents.map((parent) => ({queryKey: ["file", "search_subdirs", parent.path],- queryFn: () =>- new FileClient(getAgentServerClientOptions()).searchSubdirectories(- parent.path,- ),+ queryFn: () => searchAllSubdirectories(parent.path),retry: false,meta: { disableToast: true },})),diff --git a/src/hooks/query/use-search-subdirs.ts b/src/hooks/query/use-search-subdirs.tsindex 6fe5c11484..60a2c5558c 100644--- a/src/hooks/query/use-search-subdirs.ts+++ b/src/hooks/query/use-search-subdirs.ts@@ -3,6 +3,11 @@ import { FileClient } from "@openhands/typescript-client/clients";import { getAgentServerClientOptions } from "#/api/agent-server-client-options";import { useActiveBackend } from "#/contexts/active-backend-context";+type FileSubdirectoryPage = Awaited<+ ReturnType<FileClient["searchSubdirectories"]>+>;+type FileClientLike = Pick<FileClient, "searchSubdirectories">;+export interface FileBrowserEntry {label: string;path: string;@@ -18,11 +23,38 @@ function getFileClient() {return new FileClient(getAgentServerClientOptions());}+export async function searchAllSubdirectories(+ path: string,+ fileClient: FileClientLike = getFileClient(),+): Promise<FileSubdirectoryPage> {+ const items: FileSubdirectoryPage["items"] = [];+ const seenPageIds = new Set<string>();+ let pageId: string | null | undefined;++ while (true) {+ const page = await fileClient.searchSubdirectories(+ path,+ pageId ? { pageId } : undefined,+ );+ items.push(...page.items);+ pageId = page.next_page_id;++ if (!pageId) {+ return { items, next_page_id: null };+ }++ if (seenPageIds.has(pageId)) {+ throw new Error("File search returned a repeated page id");+ }+ seenPageIds.add(pageId);+ }+}+export const useSearchSubdirs = (path: string | null) => {const active = useActiveBackend();return useQuery({queryKey: ["file", "search_subdirs", path, active.backend.id, active.orgId],- queryFn: () => getFileClient().searchSubdirectories(path as string),+ queryFn: () => searchAllSubdirectories(path as string),enabled: !!path,retry: false,meta: { disableToast: true },