Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): strip only a trailing /api in getAGPTServerBaseUrl (#15310)

Nicholas Tindle4425a713411a
diff --git a/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts b/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts
index 8c755d6de3..897a0a2303 100644
--- a/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts
+++ b/autogpt_platform/frontend/src/services/environment/__tests__/index.test.ts
@@ -55,6 +55,19 @@ describe("AutoGPT server URL resolution", () => {
);
});
+ it.each([
+ ["http://localhost:8006/api", "http://localhost:8006"],
+ ["https://api.example.com/api", "https://api.example.com"],
+ ["https://example.com/apis/api", "https://example.com/apis"],
+ ["https://api.example.com/api/", "https://api.example.com"],
+ ["https://backend.example.com", "https://backend.example.com"],
+ ])("strips only a trailing /api from %s", (apiUrl, baseUrl) => {
+ vi.stubGlobal("window", undefined);
+ vi.stubEnv("AGPT_SERVER_URL", apiUrl);
+
+ expect(environment.getAGPTServerBaseUrl()).toBe(baseUrl);
+ });
+
it("leaves explicit server-side backend URLs untouched", () => {
vi.stubGlobal("window", undefined);
vi.stubEnv("AGPT_SERVER_URL", "http://backend.internal:8006/api");
diff --git a/autogpt_platform/frontend/src/services/environment/index.ts b/autogpt_platform/frontend/src/services/environment/index.ts
index 308bb2a334..8d323799e5 100644
--- a/autogpt_platform/frontend/src/services/environment/index.ts
+++ b/autogpt_platform/frontend/src/services/environment/index.ts
@@ -35,7 +35,9 @@ function getAGPTServerApiUrl() {
}
function getAGPTServerBaseUrl() {
- return getAGPTServerApiUrl().replace("/api", "");
+ // Only strip a trailing "/api"; a plain replace("/api", "") would hit the
+ // first "/api" anywhere, e.g. the host in https://api.example.com/api.
+ return getAGPTServerApiUrl().replace(/\/api\/?$/, "");
}
function getAGPTWsServerUrl() {