Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
5 changes: 5 additions & 0 deletions .changeset/sync-api-key-sessions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilocode/cli": patch
---

Sync CLI sessions to Kilo session history when authenticated with `KILO_API_KEY`.
Comment thread
lambertjosh marked this conversation as resolved.
Resolved
3 changes: 3 additions & 0 deletions packages/opencode/src/kilo-sessions/kilo-sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export namespace KiloSessions {
if (auth?.type === "api" && auth.key.length > 0) return auth.key
if (auth?.type === "oauth" && auth.access.length > 0) return auth.access
if (auth?.type === "wellknown" && auth.token.length > 0) return auth.token

const key = process.env["KILO_API_KEY"]?.trim()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the the first priority.

if (key) return key
return undefined
})
}
Expand Down
89 changes: 89 additions & 0 deletions packages/opencode/test/kilocode/kilo-sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { Auth } from "../../src/auth"
import { Bus } from "../../src/bus"
import type { Config } from "../../src/config/config"
import { clearInFlightCache } from "../../src/kilo-sessions/inflight-cache"
import { KiloSessions } from "../../src/kilo-sessions/kilo-sessions"
import { ProjectID } from "../../src/project/schema"
import { Session } from "../../src/session/session"
Expand All @@ -25,6 +26,12 @@ function layer(overrides: Partial<Config.Interface> = {}) {
)
}

function reset(...tokens: string[]) {
clearInFlightCache("kilo-sessions:token")
clearInFlightCache("kilo-sessions:client")
for (const token of tokens) clearInFlightCache(`kilo-sessions:token-valid:${token}`)
}

it.instance("initializes once per instance through Config.Service", () => {
let reads = 0

Expand All @@ -46,6 +53,86 @@ it.instance("initializes once per instance through Config.Service", () => {
)
})

it.instance("bootstraps session ingest from KILO_API_KEY without stored auth", () => {
const original = process.env.KILO_API_KEY
const calls: string[] = []
const fetch: typeof globalThis.fetch = Object.assign(
async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input)
if (url.endsWith("/api/user")) {
calls.push(new Headers(init?.headers).get("Authorization") ?? "")
return new Response("{}", { status: 200 })
}
if (url.endsWith("/api/session")) {
calls.push(new Headers(init?.headers).get("Authorization") ?? "")
return Response.json({ id: "remote-env", ingestPath: "/api/ingest/env" })
}
return new Response("{}", { status: 200 })
},
{ preconnect: globalThis.fetch.preconnect },
)
const request = spyOn(globalThis, "fetch").mockImplementation(fetch)

process.env.KILO_API_KEY = "env-token"
reset("env-token")

return Effect.promise(() => KiloSessions.bootstrap("session-env")).pipe(
Effect.andThen(() => Effect.sync(() => expect(calls).toEqual(["Bearer env-token", "Bearer env-token"]))),
Effect.ensuring(
Effect.sync(() => {
if (original === undefined) delete process.env.KILO_API_KEY
else process.env.KILO_API_KEY = original
reset("env-token")
request.mockRestore()
}),
),
Effect.provide(layer()),
)
})

it.instance("prefers stored auth over KILO_API_KEY for session ingest", () => {
const original = process.env.KILO_API_KEY
const calls: string[] = []
const fetch: typeof globalThis.fetch = Object.assign(
async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input)
if (url.endsWith("/api/user")) {
calls.push(new Headers(init?.headers).get("Authorization") ?? "")
return new Response("{}", { status: 200 })
}
if (url.endsWith("/api/session")) {
calls.push(new Headers(init?.headers).get("Authorization") ?? "")
return Response.json({ id: "remote-auth", ingestPath: "/api/ingest/auth" })
}
return new Response("{}", { status: 200 })
},
{ preconnect: globalThis.fetch.preconnect },
)
const request = spyOn(globalThis, "fetch").mockImplementation(fetch)

process.env.KILO_API_KEY = "env-token"
reset("env-token", "stored-token")

return Effect.gen(function* () {
const auth = yield* Auth.Service
yield* auth.set("kilo", { type: "api", key: "stored-token" })
yield* Effect.promise(() => KiloSessions.bootstrap("session-auth"))
expect(calls).toEqual(["Bearer stored-token", "Bearer stored-token"])
}).pipe(
Effect.ensuring(
Effect.gen(function* () {
const auth = yield* Auth.Service
yield* auth.remove("kilo").pipe(Effect.orDie)
if (original === undefined) delete process.env.KILO_API_KEY
else process.env.KILO_API_KEY = original
reset("env-token", "stored-token")
request.mockRestore()
}),
),
Effect.provide(layer()),
)
})

it.instance("does not duplicate created-session subscribers when init is repeated", () => {
const calls: string[] = []
const fetch: typeof globalThis.fetch = Object.assign(
Expand All @@ -62,6 +149,7 @@ it.instance("does not duplicate created-session subscribers when init is repeate
)
const request = spyOn(globalThis, "fetch").mockImplementation(fetch)

reset("test-token")
const id = SessionID.descending("session-created")

return Effect.gen(function* () {
Expand Down Expand Up @@ -91,6 +179,7 @@ it.instance("does not duplicate created-session subscribers when init is repeate
Effect.gen(function* () {
const auth = yield* Auth.Service
yield* auth.remove("kilo").pipe(Effect.orDie)
reset("test-token")
request.mockRestore()
}),
),
Expand Down
Loading