π OpenHands: AI-Driven Development
fix(conversations): save a connected repository on the cloud conversation (#18158)
Henrique Hirako8d39305538c1
diff --git a/__tests__/api/agent-server-conversation-service.test.ts b/__tests__/api/agent-server-conversation-service.test.tsindex a9e8d4423b..2f3fdd08fa 100644--- a/__tests__/api/agent-server-conversation-service.test.ts+++ b/__tests__/api/agent-server-conversation-service.test.ts@@ -1519,6 +1519,69 @@ describe("AgentServerConversationService", () => {expect(body.client_tools).toBeUndefined();});+ // Connect Repo on an existing cloud conversation must reach the server, so+ // other clients and server features see the repository.+ it("saves a connected repository on the cloud conversation", async () => {+ // Arrange+ const requests = captureRequests(+ ["patch", "get"],+ [{ id: "conv-cloud-1", selected_repository: "OpenHands/agent-canvas" }],+ );++ // Act+ await AgentServerConversationService.updateConversationRepository(+ "conv-cloud-1",+ "OpenHands/agent-canvas",+ "main",+ "github",+ );++ // Assert+ const patch = requests.find((request) => request.method === "PATCH");+ expect(patch?.url).toBe(+ `${cloudBackend.host}/api/v1/app-conversations/conv-cloud-1`,+ );+ expect(patch?.headers.authorization).toBe("Bearer bearer-token");+ expect(patch?.body).toEqual({+ selected_repository: "OpenHands/agent-canvas",+ selected_branch: "main",+ git_provider: "github",+ });+ });++ // Older servers reject some repository names (e.g. Azure DevOps+ // org/project/repo). Connect Repo must still work for the user.+ it("keeps the connected repository when the cloud server rejects it", async () => {+ // Arrange+ captureRequests(+ ["patch"],+ HttpResponse.json({ detail: "invalid" }, { status: 500 }),+ );+ captureRequests(["get"], [{ id: "conv-cloud-1" }]);+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});++ try {+ // Act+ const conversation =+ await AgentServerConversationService.updateConversationRepository(+ "conv-cloud-1",+ "contoso/project/repo",+ "main",+ "azure_devops",+ );++ // Assert+ expect(warn).toHaveBeenCalled();+ expect(conversation).toMatchObject({+ selected_repository: "contoso/project/repo",+ selected_branch: "main",+ git_provider: "azure_devops",+ });+ } finally {+ warn.mockRestore();+ }+ });+it("routes readConversationFile to the cloud file endpoint with the file_path query param", async () => {const requests = captureRequests(["get"], "# PLAN content");diff --git a/src/api/cloud/conversation-service.api.ts b/src/api/cloud/conversation-service.api.tsindex d807e4ff45..30dff4489c 100644--- a/src/api/cloud/conversation-service.api.ts+++ b/src/api/cloud/conversation-service.api.ts@@ -11,17 +11,16 @@ import type {AppConversationStartTask,} from "../conversation-service/agent-server-conversation-service.types";import { AGENT_CANVAS_CLIENT_HEADERS } from "../client-source";+import type { Provider } from "#/types/settings";import { callCloudProxy } from "./proxy";/**- * The cloud backend does not always echo `selected_repository` /- * `selected_branch` / `git_provider` back from- * `GET /api/v1/app-conversations` until its own background hydration- * completes. We persist the selection to local storage at connect time- * (see `AgentServerConversationService.updateConversationRepository`)- * and overlay it here so the chat-page git control bar reflects the- * connection immediately, instead of snapping back to the empty- * "Connect Repo" state on every refetch.+ * Connect Repo saves the selection on the server+ * (`updateCloudConversationRepository`) and also caches it in local storage+ * (see `AgentServerConversationService.updateConversationRepository`). The+ * cache is overlaid here for each field whose server value is empty: for+ * example older conversations connected before the server save existed, or+ * a repository the server rejected when the conversation had none.** Server values take precedence whenever they're populated; the* local-storage fallback only fills in fields the server returned as@@ -238,6 +237,30 @@ export async function updateCloudConversationTitle(});}+/**+ * Attach, change, or clear the repository of a cloud v1 app-conversation.+ * `PATCH /api/v1/app-conversations/{id}`. The server stores the fields but+ * does not clone; the caller asks the agent to clone the repository.+ */+export async function updateCloudConversationRepository(+ conversationId: string,+ repository: string | null,+ branch: string | null,+ gitProvider: Provider | null,+): Promise<AppConversation> {+ const backend = getActiveCloudBackend();+ return callCloudProxy<AppConversation>({+ backend,+ method: "PATCH",+ path: `/api/v1/app-conversations/${conversationId}`,+ body: {+ selected_repository: repository,+ selected_branch: branch,+ git_provider: gitProvider,+ },+ });+}+/*** Pause the cloud sandbox backing a v1 app-conversation. Mirrors* OpenHands' `SandboxService.pauseSandbox`:diff --git a/src/api/conversation-service/agent-server-conversation-service.api.ts b/src/api/conversation-service/agent-server-conversation-service.api.tsindex dda3398f35..817ae41705 100644--- a/src/api/conversation-service/agent-server-conversation-service.api.ts+++ b/src/api/conversation-service/agent-server-conversation-service.api.ts@@ -35,6 +35,7 @@ import {readCloudConversationFile,searchCloudConversations,updateCloudConversationPublicFlag,+ updateCloudConversationRepository,updateCloudConversationTitle,} from "../cloud/conversation-service.api";import {@@ -879,15 +880,34 @@ class AgentServerConversationService {conversationId: string,repository: string | null,branch?: string | null,- gitProvider?: string | null,+ gitProvider?: Provider | null,): Promise<AppConversation> {+ // Cloud conversations keep the repository on the server, so other+ // browsers and server features see it. Best effort: an older server can+ // reject some names (e.g. Azure DevOps org/project/repo), and Connect Repo+ // must still work, so a failure keeps the local selection only.+ if (getActiveBackend().backend.kind === "cloud") {+ try {+ await updateCloudConversationRepository(+ conversationId,+ repository,+ branch ?? null,+ gitProvider ?? null,+ );+ } catch (error) {+ console.warn(+ `Failed to save the repository of conversation ${conversationId}`,+ error,+ );+ }+ }if (repository) {const existing = getStoredConversationMetadata(conversationId);setStoredConversationMetadata(conversationId, {...(existing ?? {}),selected_repository: repository,selected_branch: branch ?? null,- git_provider: (gitProvider as Provider | null | undefined) ?? null,+ git_provider: gitProvider ?? null,});} else {removeStoredConversationMetadata(conversationId);