Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 12 additions & 27 deletions ui/litellm-dashboard/src/components/networking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,21 @@
import { jsonFields } from "./common_components/check_openapi_schema";
import NotificationsManager from "./molecules/notifications_manager";
import { createApiClient, deriveErrorMessage } from "@/lib/http/client";
import { resolveApiBase } from "@/lib/http/resolveApiBase";

export { deriveErrorMessage };
export { ApiError } from "@/lib/http/client";

const isLocal = process.env.NODE_ENV === "development";
// In dev, if NEXT_PUBLIC_USE_REWRITES=true the Next.js dev server proxies API calls
// to the backend — use relative URLs (null) so rewrites can intercept them.
const defaultProxyBaseUrl = process.env.NEXT_PUBLIC_BASE_URL
? process.env.NEXT_PUBLIC_BASE_URL
: isLocal && process.env.NEXT_PUBLIC_USE_REWRITES !== "true"
? "http://localhost:4000"
: null;
const resolveDefaultBase = (fallback: string | null): string | null =>
process.env.NEXT_PUBLIC_BASE_URL
? process.env.NEXT_PUBLIC_BASE_URL
: isLocal && process.env.NEXT_PUBLIC_USE_REWRITES !== "true"
? "http://localhost:4000"
: fallback;
const defaultProxyBaseUrl = resolveDefaultBase(null);
const defaultServerRootPath = "/";
export let serverRootPath = defaultServerRootPath;
const WORKER_URL_KEY = "litellm_worker_url";
Expand Down Expand Up @@ -128,28 +131,10 @@
if (typeof window !== "undefined" && window.localStorage.getItem(WORKER_URL_KEY)) {
return;
}
const browserLocation = getWindowLocation();
const resolvedDefaultProxyBaseUrl = process.env.NEXT_PUBLIC_BASE_URL
? process.env.NEXT_PUBLIC_BASE_URL
: isLocal && process.env.NEXT_PUBLIC_USE_REWRITES !== "true"
? "http://localhost:4000"
: browserLocation?.origin ?? null;
let initialProxyBaseUrl = receivedProxyBaseUrl || resolvedDefaultProxyBaseUrl;
console.log("proxyBaseUrl:", proxyBaseUrl);
console.log("serverRootPath:", serverRootPath);

if (!initialProxyBaseUrl) {
proxyBaseUrl = proxyBaseUrl ?? null;
console.log("Updated proxyBaseUrl:", proxyBaseUrl);
return;
}

if (serverRootPath.length > 0 && !initialProxyBaseUrl.endsWith(serverRootPath) && serverRootPath != "/") {
initialProxyBaseUrl += serverRootPath;
}

proxyBaseUrl = initialProxyBaseUrl;
console.log("Updated proxyBaseUrl:", proxyBaseUrl);
proxyBaseUrl = resolveApiBase({
explicitBase: receivedProxyBaseUrl || resolveDefaultBase(getWindowLocation()?.origin ?? null),
serverRootPath,
});
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
};

const updateServerRootPath = (receivedServerRootPath: string) => {
Expand Down Expand Up @@ -226,7 +211,7 @@
export interface PromptTemplateBase {
litellm_prompt_id: string;
content: string;
metadata?: Record<string, any> | null;

Check warning on line 214 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
}

interface PromptInfoResponse {
Expand All @@ -243,7 +228,7 @@
organization_id: string;
organization_alias: string;
budget_id: string;
metadata: Record<string, any>;

Check warning on line 231 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
models: string[];
spend: number;
model_spend: Record<string, number>;
Expand All @@ -251,10 +236,10 @@
created_by: string;
updated_at: string;
updated_by: string;
litellm_budget_table: any; // Simplified to any since we don't need the detailed structure

Check warning on line 239 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
teams: any[] | null;

Check warning on line 240 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
users: any[] | null;

Check warning on line 241 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
members: any[] | null;

Check warning on line 242 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
object_permission?: {
object_permission_id: string;
mcp_servers: string[];
Expand All @@ -265,7 +250,7 @@

export interface CredentialItem {
credential_name: string;
credential_values: any;

Check warning on line 253 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
credential_info: {
custom_llm_provider?: string;
description?: string;
Expand Down Expand Up @@ -345,7 +330,7 @@

let lastErrorTime = 0;

export const handleError = async (errorData: string | any) => {

Check warning on line 333 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
const currentTime = Date.now();
if (currentTime - lastErrorTime > 60000) {
// 60000 milliseconds = 60 seconds
Expand Down Expand Up @@ -667,7 +652,7 @@

export const budgetCreateCall = async (
accessToken: string,
formValues: Record<string, any>, // Assuming formValues is an object

Check warning on line 655 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
) => {
try {
console.log("Form Values in budgetCreateCall:", formValues); // Log the form values before making the API call
Expand All @@ -690,7 +675,7 @@

export const budgetUpdateCall = async (
accessToken: string,
formValues: Record<string, any>, // Assuming formValues is an object

Check warning on line 678 in ui/litellm-dashboard/src/components/networking.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
) => {
try {
console.log("Form Values in budgetUpdateCall:", formValues); // Log the form values before making the API call
Expand Down
84 changes: 84 additions & 0 deletions ui/litellm-dashboard/src/lib/http/resolveApiBase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, expect, it } from "vitest";
import { resolveApiBase } from "./resolveApiBase";

describe("resolveApiBase", () => {
describe("same-origin (no explicit base)", () => {
it("returns an empty base so requests stay relative", () => {
expect(resolveApiBase({})).toBe("");
expect(resolveApiBase({ explicitBase: null, serverRootPath: "/" })).toBe("");
expect(resolveApiBase({ explicitBase: "", serverRootPath: undefined })).toBe("");
});

it("prefixes a relative base with the server root path", () => {
expect(resolveApiBase({ serverRootPath: "/litellm" })).toBe("/litellm");
});
});

describe("explicit base (split-origin / dev)", () => {
it("uses the explicit base verbatim when root path is mounted at /", () => {
expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "/" })).toBe(
"http://localhost:4000",
);
});

it("appends the server root path to the explicit base", () => {
expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "/litellm" })).toBe(
"http://localhost:4000/litellm",
);
});
});

describe("dedup — base already includes the root path", () => {
it("does not double the root path", () => {
expect(resolveApiBase({ explicitBase: "http://localhost:4000/litellm", serverRootPath: "/litellm" })).toBe(
"http://localhost:4000/litellm",
);
});
});

describe("normalization", () => {
it("trims a trailing slash from the explicit base before appending", () => {
expect(resolveApiBase({ explicitBase: "http://localhost:4000/", serverRootPath: "/litellm" })).toBe(
"http://localhost:4000/litellm",
);
});

it("trims a trailing slash from the explicit base when there is no root path", () => {
expect(resolveApiBase({ explicitBase: "http://localhost:4000/", serverRootPath: "/" })).toBe(
"http://localhost:4000",
);
});

it("adds a leading slash to a root path that lacks one", () => {
expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "litellm" })).toBe(
"http://localhost:4000/litellm",
);
});

it("trims a trailing slash from the root path", () => {
expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "/litellm/" })).toBe(
"http://localhost:4000/litellm",
);
});

it("ignores whitespace around inputs", () => {
expect(resolveApiBase({ explicitBase: " http://localhost:4000 ", serverRootPath: " /litellm " })).toBe(
"http://localhost:4000/litellm",
);
});
});

describe("multi-segment root paths", () => {
it("appends a nested root path", () => {
expect(resolveApiBase({ explicitBase: "http://localhost:4000", serverRootPath: "/team/litellm" })).toBe(
"http://localhost:4000/team/litellm",
);
});

it("dedups a nested root path", () => {
expect(
resolveApiBase({ explicitBase: "http://localhost:4000/team/litellm", serverRootPath: "/team/litellm" }),
).toBe("http://localhost:4000/team/litellm");
});
});
});
35 changes: 35 additions & 0 deletions ui/litellm-dashboard/src/lib/http/resolveApiBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export interface ApiBaseInputs {
/**
* Absolute API origin to target instead of same-origin. Comes from the
* NEXT_PUBLIC_BASE_URL build env (dev / split-origin) or the proxy_base_url
* the backend reports in its UI config. Empty/unset means same-origin.
*/
explicitBase?: string | null;
/**
* The proxy's mount path when it sits under a sub-path (e.g. "/litellm"
* behind a reverse proxy). "/" or empty means mounted at the root.
*/
serverRootPath?: string | null;
}

const normalizeRootPath = (serverRootPath: string | null | undefined): string => {
const trimmed = (serverRootPath ?? "").trim();
if (trimmed === "" || trimmed === "/") return "";
const withLeadingSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
return withLeadingSlash.replace(/\/+$/, "");
};

/**
* Resolve the single API base string that every request is prefixed with.
*
* Same-origin is represented as "" so requests stay relative and work behind
* any domain or reverse proxy without hardcoding an origin. An explicit base is
* used verbatim (trailing slash trimmed). The server root path is appended
* unless the base already ends with it.
*/
export const resolveApiBase = ({ explicitBase, serverRootPath }: ApiBaseInputs): string => {
const base = (explicitBase ?? "").trim().replace(/\/+$/, "");
const rootPath = normalizeRootPath(serverRootPath);
if (rootPath === "" || base.endsWith(rootPath)) return base;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
return `${base}${rootPath}`;
};
Loading