Skip to content

Commit d6cc4e0

Browse files
authored
Merge pull request #4066 from Kilo-Org/track-sessions-from-the-extension
2 parents 5bbb94e + 7fbea75 commit d6cc4e0

34 files changed

+2225
-4220
lines changed

.changeset/lucky-chicken-doubt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"kilo-code": minor
3+
"@kilocode/cli": patch
4+
---
5+
6+
use shared session manager from extension folder

cli/src/cli.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import type { CLIOptions } from "./types/cli.js"
2424
import type { CLIConfig, ProviderConfig } from "./config/types.js"
2525
import { getModelIdKey } from "./constants/providers/models.js"
2626
import type { ProviderName } from "./types/messages.js"
27-
import { TrpcClient } from "./services/trpcClient.js"
28-
import { SessionService } from "./services/session.js"
27+
import { KiloCodePathProvider, ExtensionMessengerAdapter } from "./services/session-adapters.js"
2928
import { getKiloToken } from "./config/persistence.js"
29+
import { SessionManager } from "../../src/shared/kilocode/cli-sessions/core/SessionManager.js"
3030

3131
/**
3232
* Main application class that orchestrates the CLI lifecycle
@@ -37,7 +37,7 @@ export class CLI {
3737
private ui: Instance | null = null
3838
private options: CLIOptions
3939
private isInitialized = false
40-
private sessionService: SessionService | null = null
40+
private sessionService: SessionManager | null = null
4141

4242
constructor(options: CLIOptions = {}) {
4343
this.options = options
@@ -135,16 +135,31 @@ export class CLI {
135135
const kiloToken = getKiloToken(config)
136136

137137
if (kiloToken) {
138-
TrpcClient.init(kiloToken)
139-
logs.debug("TrpcClient initialized with kiloToken", "CLI")
138+
const pathProvider = new KiloCodePathProvider()
139+
const extensionMessenger = new ExtensionMessengerAdapter(this.service)
140+
141+
this.sessionService = SessionManager.init({
142+
pathProvider,
143+
logger: logs,
144+
extensionMessenger,
145+
getToken: () => Promise.resolve(kiloToken),
146+
onSessionCreated: (message) => {
147+
if (this.options.json) {
148+
console.log(JSON.stringify(message))
149+
}
150+
},
151+
onSessionRestored: () => {
152+
if (this.store) {
153+
this.store.set(taskResumedViaContinueOrSessionAtom, true)
154+
}
155+
},
156+
platform: "cli",
157+
})
158+
logs.debug("SessionManager initialized with dependencies", "CLI")
140159

141-
this.sessionService = SessionService.init(this.service, this.store, this.options.json)
142-
logs.debug("SessionService initialized with ExtensionService", "CLI")
143-
144-
// Set workspace directory for git operations (important for parallel mode/worktrees)
145160
const workspace = this.options.workspace || process.cwd()
146161
this.sessionService.setWorkspaceDirectory(workspace)
147-
logs.debug("SessionService workspace directory set", "CLI", { workspace })
162+
logs.debug("SessionManager workspace directory set", "CLI", { workspace })
148163

149164
if (this.options.session) {
150165
await this.sessionService.restoreSession(this.options.session)

cli/src/commands/__tests__/new.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
66
import { newCommand } from "../new.js"
77
import type { CommandContext } from "../core/types.js"
88
import { createMockContext } from "./helpers/mockContext.js"
9-
import { SessionService } from "../../services/session.js"
9+
import { SessionManager } from "../../../../src/shared/kilocode/cli-sessions/core/SessionManager.js"
1010

1111
describe("/new command", () => {
1212
let mockContext: CommandContext
13-
let mockSessionService: Partial<SessionService> & { destroy: ReturnType<typeof vi.fn> }
13+
let mockSessionManager: Partial<SessionManager> & { destroy: ReturnType<typeof vi.fn> }
1414

1515
beforeEach(() => {
1616
// Mock process.stdout.write to capture terminal clearing
@@ -20,14 +20,14 @@ describe("/new command", () => {
2020
input: "/new",
2121
})
2222

23-
// Mock SessionService
24-
mockSessionService = {
23+
// Mock SessionManager
24+
mockSessionManager = {
2525
destroy: vi.fn().mockResolvedValue(undefined),
2626
sessionId: "test-session-id",
2727
}
2828

29-
// Mock SessionService.init to return our mock
30-
vi.spyOn(SessionService, "init").mockReturnValue(mockSessionService as unknown as SessionService)
29+
// Mock SessionManager.init to return our mock
30+
vi.spyOn(SessionManager, "init").mockReturnValue(mockSessionManager as unknown as SessionManager)
3131
})
3232

3333
afterEach(() => {
@@ -74,13 +74,13 @@ describe("/new command", () => {
7474
it("should clear the session", async () => {
7575
await newCommand.handler(mockContext)
7676

77-
expect(SessionService.init).toHaveBeenCalled()
78-
expect(mockSessionService.destroy).toHaveBeenCalledTimes(1)
77+
expect(SessionManager.init).toHaveBeenCalled()
78+
expect(mockSessionManager.destroy).toHaveBeenCalledTimes(1)
7979
})
8080

8181
it("should continue execution even if session clearing fails", async () => {
8282
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
83-
mockSessionService.destroy.mockRejectedValue(new Error("Session error"))
83+
mockSessionManager.destroy.mockRejectedValue(new Error("Session error"))
8484

8585
await newCommand.handler(mockContext)
8686

@@ -121,7 +121,7 @@ describe("/new command", () => {
121121
callOrder.push("clearTask")
122122
})
123123

124-
mockSessionService.destroy = vi.fn().mockImplementation(async () => {
124+
mockSessionManager.destroy = vi.fn().mockImplementation(async () => {
125125
callOrder.push("sessionDestroy")
126126
})
127127

@@ -164,7 +164,7 @@ describe("/new command", () => {
164164

165165
// Verify all cleanup operations were performed
166166
expect(mockContext.clearTask).toHaveBeenCalled()
167-
expect(mockSessionService.destroy).toHaveBeenCalled()
167+
expect(mockSessionManager.destroy).toHaveBeenCalled()
168168
expect(mockContext.replaceMessages).toHaveBeenCalled()
169169

170170
// Verify welcome message was replaced

0 commit comments

Comments
 (0)