Skip to content

Commit 6271985

Browse files
committed
make it possible to configure the url of the kilocode api
PoC, does not look as good as we want in the settings yet
1 parent 5fe4eaf commit 6271985

File tree

3 files changed

+208
-2
lines changed

3 files changed

+208
-2
lines changed

src/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,18 @@
453453
"type": "string",
454454
"default": "",
455455
"description": "%settings.autoImportSettingsPath.description%"
456+
},
457+
"kilo-code.baseUrl": {
458+
"type": "string",
459+
"default": "https://kilocode.ai",
460+
"description": "TEST Base URL for Kilo Code backend services (Advanced)",
461+
"scope": "application",
462+
"tags": [
463+
"advanced",
464+
"usesOnlineServices"
465+
],
466+
"order": 999,
467+
"editPresentation": "multilineText"
456468
}
457469
}
458470
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
2+
3+
const mockPostMessage = vi.fn()
4+
vi.mock("@/utils/vscode", () => ({
5+
vscode: {
6+
postMessage: mockPostMessage,
7+
},
8+
}))
9+
10+
describe("helpers", () => {
11+
let originalAddEventListener: typeof window.addEventListener
12+
let mockEventListener: ReturnType<typeof vi.fn>
13+
14+
beforeEach(() => {
15+
mockPostMessage.mockClear()
16+
originalAddEventListener = window.addEventListener
17+
mockEventListener = vi.fn()
18+
window.addEventListener = mockEventListener
19+
vi.resetModules()
20+
})
21+
22+
afterEach(() => {
23+
window.addEventListener = originalAddEventListener
24+
})
25+
26+
describe("getKiloCodeBackendSignInUrl", () => {
27+
it("should return sign-in URL with default base URL", async () => {
28+
const { getKiloCodeBackendSignInUrl } = await import("../helpers")
29+
30+
const result = getKiloCodeBackendSignInUrl()
31+
32+
expect(result).toBe("https://kilocode.ai/users/sign_in?source=vscode")
33+
expect(mockPostMessage).toHaveBeenCalledWith({
34+
type: "getVSCodeSetting",
35+
setting: "kilo-code.baseUrl",
36+
})
37+
})
38+
39+
it("should use cached base URL after setting is received", async () => {
40+
const { getKiloCodeBackendSignInUrl } = await import("../helpers")
41+
42+
// Simulate receiving a setting update
43+
const messageEvent = new MessageEvent("message", {
44+
data: {
45+
type: "vsCodeSetting",
46+
setting: "kilo-code.baseUrl",
47+
value: "https://custom.example.com",
48+
},
49+
})
50+
51+
// Get the event listener that was registered
52+
expect(mockEventListener).toHaveBeenCalledWith("message", expect.any(Function))
53+
const eventHandler = mockEventListener.mock.calls[0][1]
54+
55+
// Trigger the event handler
56+
eventHandler(messageEvent)
57+
58+
const result = getKiloCodeBackendSignInUrl()
59+
expect(result).toBe("https://custom.example.com/users/sign_in?source=vscode")
60+
})
61+
62+
it("should fallback to default when setting value is empty", async () => {
63+
const { getKiloCodeBackendSignInUrl } = await import("../helpers")
64+
65+
// Simulate receiving an empty setting value
66+
const messageEvent = new MessageEvent("message", {
67+
data: {
68+
type: "vsCodeSetting",
69+
setting: "kilo-code.baseUrl",
70+
value: "",
71+
},
72+
})
73+
74+
const eventHandler = mockEventListener.mock.calls[0][1]
75+
eventHandler(messageEvent)
76+
77+
const result = getKiloCodeBackendSignInUrl()
78+
expect(result).toBe("https://kilocode.ai/users/sign_in?source=vscode")
79+
})
80+
81+
it("should support custom uriScheme and uiKind parameters", async () => {
82+
const { getKiloCodeBackendSignInUrl } = await import("../helpers")
83+
84+
const result = getKiloCodeBackendSignInUrl("custom-scheme", "Web")
85+
expect(result).toBe("https://kilocode.ai/users/sign_in?source=web")
86+
})
87+
})
88+
89+
describe("getKiloCodeBackendSignUpUrl", () => {
90+
it("should return sign-up URL with default base URL", async () => {
91+
const { getKiloCodeBackendSignUpUrl } = await import("../helpers")
92+
93+
const result = getKiloCodeBackendSignUpUrl()
94+
95+
expect(result).toBe("https://kilocode.ai/users/sign_up?source=vscode")
96+
expect(mockPostMessage).toHaveBeenCalledWith({
97+
type: "getVSCodeSetting",
98+
setting: "kilo-code.baseUrl",
99+
})
100+
})
101+
102+
it("should use cached base URL after setting is received", async () => {
103+
const { getKiloCodeBackendSignUpUrl } = await import("../helpers")
104+
105+
// Simulate receiving a setting update
106+
const messageEvent = new MessageEvent("message", {
107+
data: {
108+
type: "vsCodeSetting",
109+
setting: "kilo-code.baseUrl",
110+
value: "https://custom.example.com",
111+
},
112+
})
113+
114+
const eventHandler = mockEventListener.mock.calls[0][1]
115+
eventHandler(messageEvent)
116+
117+
const result = getKiloCodeBackendSignUpUrl()
118+
expect(result).toBe("https://custom.example.com/users/sign_up?source=vscode")
119+
})
120+
121+
it("should support custom uriScheme and uiKind parameters", async () => {
122+
const { getKiloCodeBackendSignUpUrl } = await import("../helpers")
123+
124+
const result = getKiloCodeBackendSignUpUrl("custom-scheme", "Web")
125+
expect(result).toBe("https://kilocode.ai/users/sign_up?source=web")
126+
})
127+
})
128+
129+
describe("message event handling", () => {
130+
it("should ignore non-vsCodeSetting messages", async () => {
131+
await import("../helpers")
132+
133+
const messageEvent = new MessageEvent("message", {
134+
data: {
135+
type: "otherMessage",
136+
setting: "kilo-code.baseUrl",
137+
value: "https://should-be-ignored.com",
138+
},
139+
})
140+
141+
const eventHandler = mockEventListener.mock.calls[0][1]
142+
eventHandler(messageEvent)
143+
144+
const { getKiloCodeBackendSignInUrl } = await import("../helpers")
145+
const result = getKiloCodeBackendSignInUrl()
146+
expect(result).toBe("https://kilocode.ai/users/sign_in?source=vscode")
147+
})
148+
149+
it("should ignore messages for different settings", async () => {
150+
await import("../helpers")
151+
152+
const messageEvent = new MessageEvent("message", {
153+
data: {
154+
type: "vsCodeSetting",
155+
setting: "other.setting",
156+
value: "https://should-be-ignored.com",
157+
},
158+
})
159+
160+
const eventHandler = mockEventListener.mock.calls[0][1]
161+
eventHandler(messageEvent)
162+
163+
const { getKiloCodeBackendSignInUrl } = await import("../helpers")
164+
const result = getKiloCodeBackendSignInUrl()
165+
expect(result).toBe("https://kilocode.ai/users/sign_in?source=vscode")
166+
})
167+
})
168+
})
Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
import { vscode } from "@/utils/vscode"
2+
3+
// Global state for base URL
4+
let cachedBaseUrl: string = "https://kilocode.ai"
5+
let hasRequestedSetting = false
6+
7+
// Request the setting from VS Code on first use
8+
function ensureBaseUrlRequested() {
9+
if (!hasRequestedSetting) {
10+
hasRequestedSetting = true
11+
vscode.postMessage({ type: "getVSCodeSetting", setting: "kilo-code.baseUrl" })
12+
}
13+
}
14+
15+
// Listen for VS Code setting responses
16+
window.addEventListener("message", (event) => {
17+
if (event.data?.type === "vsCodeSetting" && event.data?.setting === "kilo-code.baseUrl") {
18+
cachedBaseUrl = event.data.value || "https://kilocode.ai"
19+
}
20+
})
21+
22+
function getBaseUrl(): string {
23+
ensureBaseUrlRequested()
24+
return cachedBaseUrl
25+
}
26+
127
export function getKiloCodeBackendSignInUrl(uriScheme: string = "vscode", uiKind: string = "Desktop") {
2-
const baseUrl = "https://kilocode.ai"
28+
const baseUrl = getBaseUrl()
329
const source = uiKind === "Web" ? "web" : uriScheme
430
return `${baseUrl}/users/sign_in?source=${source}`
531
}
632

733
export function getKiloCodeBackendSignUpUrl(uriScheme: string = "vscode", uiKind: string = "Desktop") {
8-
const baseUrl = "https://kilocode.ai"
34+
const baseUrl = getBaseUrl()
935
const source = uiKind === "Web" ? "web" : uriScheme
1036
return `${baseUrl}/users/sign_up?source=${source}`
1137
}

0 commit comments

Comments
 (0)