Skip to content

Commit 14c861d

Browse files
authored
Don't clobber KILOCODE_BACKEND_BASE_URL by assuming it must be localhost (#4217)
* Refactor API URL handling to avoid assuming localhost; add tests for custom backend URLs * change set * remove changeset
1 parent 08c03b7 commit 14c861d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

packages/types/src/__tests__/kilocode.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { describe, it, expect, vi, afterEach } from "vitest"
44
import {
55
ghostServiceSettingsSchema,
66
getAppUrl,
7+
getApiUrl,
78
getKiloUrlFromToken,
89
getExtensionConfigUrl,
910
} from "../kilocode/kilocode.js"
@@ -66,6 +67,34 @@ describe("URL functions", () => {
6667
it("should use subdomain structure for production", () => {
6768
expect(getExtensionConfigUrl()).toBe("https://api.kilo.ai/extension-config.json")
6869
})
70+
it("should use path structure for custom backend URLs", () => {
71+
process.env.KILOCODE_BACKEND_BASE_URL = "http://192.168.200.70:3000"
72+
expect(getExtensionConfigUrl()).toBe("http://192.168.200.70:3000/extension-config.json")
73+
})
74+
})
75+
76+
describe("getApiUrl", () => {
77+
it("should handle production URLs with api subdomain", () => {
78+
expect(getApiUrl()).toBe("https://api.kilo.ai/")
79+
expect(getApiUrl("/trpc/cliSessions.get")).toBe("https://api.kilo.ai/trpc/cliSessions.get")
80+
expect(getApiUrl("/api/profile")).toBe("https://api.kilo.ai/api/profile")
81+
})
82+
83+
it("should handle localhost development URLs", () => {
84+
process.env.KILOCODE_BACKEND_BASE_URL = "http://localhost:3000"
85+
86+
expect(getApiUrl()).toBe("http://localhost:3000/")
87+
expect(getApiUrl("/api/trpc/cliSessions.get")).toBe("http://localhost:3000/api/trpc/cliSessions.get")
88+
expect(getApiUrl("/api/profile")).toBe("http://localhost:3000/api/profile")
89+
})
90+
91+
it("should handle custom backend URLs (non-localhost)", () => {
92+
process.env.KILOCODE_BACKEND_BASE_URL = "http://192.168.200.70:3000"
93+
94+
expect(getApiUrl()).toBe("http://192.168.200.70:3000/")
95+
expect(getApiUrl("/api/trpc/cliSessions.get")).toBe("http://192.168.200.70:3000/api/trpc/cliSessions.get")
96+
expect(getApiUrl("/api/profile")).toBe("http://192.168.200.70:3000/api/profile")
97+
})
6998
})
7099

71100
describe("getAppUrl", () => {

packages/types/src/kilocode/kilocode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ export function getAppUrl(path: string = ""): string {
119119
export function getApiUrl(path: string = ""): string {
120120
const backend = getGlobalKilocodeBackendUrl()
121121

122-
// In development (localhost), API is served from the same origin (no /api prefix needed)
123-
if (backend.includes("localhost")) {
122+
// If using a custom backend (not the default production URL), use it directly
123+
if (backend !== DEFAULT_KILOCODE_BACKEND_URL) {
124124
return new URL(path, backend).toString()
125125
}
126126

@@ -136,7 +136,7 @@ export function getApiUrl(path: string = ""): string {
136136
export function getExtensionConfigUrl(): string {
137137
try {
138138
const backend = getGlobalKilocodeBackendUrl()
139-
if (backend.includes("localhost")) {
139+
if (backend !== DEFAULT_KILOCODE_BACKEND_URL) {
140140
return getAppUrl("/extension-config.json")
141141
} else {
142142
return "https://api.kilo.ai/extension-config.json"

0 commit comments

Comments
 (0)