File size: 13,579 Bytes
f59fbe2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | import * as path from "node:path";
import * as vscode from "vscode";
import {
validateRpcMessage,
type RpcMethod,
type RpcResult,
} from "../shared/bridge";
import { VSCodeSettings } from "./config/vscode-settings";
import { handlers, type BroadcastFn, type HandlerContext, type ReloadWebviewFn, type ShowLogsFn } from "./handlers";
import { BaselineManager, type BaselineSession } from "./managers/baseline.manager";
import { FileManager } from "./managers/file.manager";
import { KimiRuntime } from "./runtime/kimi-runtime";
import type { SessionRuntime } from "./runtime/session-runtime";
import { areSameFsPath } from "./utils/fs-path";
import {
isWorkspacePathContained,
isWorkspacePathContainedSync,
relativeWorkspacePath,
resolveWorkspacePath,
type WorkspacePath,
workDirUriFromPath,
} from "./utils/workspace-path";
export class BridgeHandler {
readonly baselineManager: BaselineManager;
readonly runtime: KimiRuntime;
private readonly customWorkDirs = new Map<string, string>();
private readonly fileManager: FileManager;
constructor(
private readonly broadcast: BroadcastFn,
private readonly workspaceState: vscode.Memento,
globalStoragePath: string,
private readonly reloadWebview: ReloadWebviewFn,
private readonly showLogs: ShowLogsFn,
private readonly writeLog: (message: string) => void,
) {
try {
this.runtime = new KimiRuntime({
version: VSCodeSettings.getExtensionConfig().version,
broadcast,
captureBaseline: (session, filePath, webviewIds) => {
this.captureFileBaseline(session, filePath, webviewIds);
},
log: (message, error) => this.logRuntimeError(message, error),
});
} catch (error) {
throw new Error(
`Failed to start the Kimi engine: ${error instanceof Error ? error.message : String(error)}.`,
{ cause: error },
);
}
this.baselineManager = new BaselineManager(globalStoragePath, this.runtime.harness.homeDir);
this.fileManager = new FileManager(this.baselineManager, broadcast);
}
async handle(value: unknown, webviewId: string): Promise<RpcResult> {
const startedAt = Date.now();
const validation = validateRpcMessage(value);
if (!validation.ok) {
this.trace(validation.id, validation.method, Date.now() - startedAt, false);
this.logRuntimeError(`Bridge request rejected: ${validation.method}`, validation.error);
return { id: validation.id, error: validation.error };
}
const msg = validation.message;
try {
const result = await this.dispatch(msg.method, msg.params, webviewId);
this.trace(msg.id, msg.method, Date.now() - startedAt, true);
return { id: msg.id, result };
} catch (error) {
this.trace(msg.id, msg.method, Date.now() - startedAt, false);
this.logRuntimeError(`Bridge request failed: ${msg.method}`, error);
return {
id: msg.id,
error: error instanceof Error ? error.message : String(error),
};
}
}
private get workspaceRoot(): string | null {
return this.workspaceRootUri?.fsPath ?? null;
}
private get workspaceRootUri(): vscode.Uri | null {
return vscode.workspace.workspaceFolders?.[0]?.uri ?? null;
}
private getWorkDir(webviewId: string): string | null {
return this.customWorkDirs.get(webviewId) ?? this.workspaceRoot;
}
private getWorkDirUri(webviewId: string): vscode.Uri | null {
const workspaceRoot = this.workspaceRoot;
const workspaceRootUri = this.workspaceRootUri;
const workDir = this.getWorkDir(webviewId);
if (workspaceRoot === null || workspaceRootUri === null || workDir === null) return null;
return workDirUriFromPath(workspaceRootUri, workspaceRoot, workDir) ?? null;
}
private async setCustomWorkDir(webviewId: string, workDir: string | null): Promise<void> {
const workspaceRoot = this.workspaceRoot;
const workspaceRootUri = this.workspaceRootUri;
if (workspaceRoot === null || workspaceRootUri === null) throw new Error("No workspace folder open");
if (workDir !== null) {
const workDirUri = workDirUriFromPath(workspaceRootUri, workspaceRoot, workDir);
if (workDirUri === undefined || !(await isWorkspacePathContained(workspaceRootUri, workDirUri))) {
throw new Error("Working directory must be within the workspace");
}
}
if (workDir && workDir !== this.workspaceRoot) {
this.customWorkDirs.set(webviewId, workDir);
} else {
this.customWorkDirs.delete(webviewId);
}
await this.runtime.detachView(webviewId);
this.fileManager.clearSession(webviewId);
}
private requireWorkDir(webviewId: string): string {
const workDir = this.getWorkDir(webviewId);
if (!workDir) throw new Error("No workspace folder open");
return workDir;
}
private requireWorkDirUri(webviewId: string): vscode.Uri {
const workDirUri = this.getWorkDirUri(webviewId);
if (!workDirUri) throw new Error("No workspace folder open");
return workDirUri;
}
private async dispatch(method: RpcMethod, params: unknown, webviewId: string): Promise<unknown> {
if (!Object.hasOwn(handlers, method)) throw new Error(`Unknown method: ${method}`);
const handler = handlers[method];
if (!handler) throw new Error(`Unknown method: ${method}`);
return handler(params, this.createContext(webviewId));
}
private createContext(webviewId: string): HandlerContext {
return {
webviewId,
workDir: this.getWorkDir(webviewId),
workDirUri: this.getWorkDirUri(webviewId),
workspaceRoot: this.workspaceRoot,
workspaceRootUri: this.workspaceRootUri,
workspaceState: this.workspaceState,
requireWorkDir: () => this.requireWorkDir(webviewId),
requireWorkDirUri: () => this.requireWorkDirUri(webviewId),
broadcast: this.broadcast,
fileManager: this.fileManager,
baselineManager: this.baselineManager,
runtime: this.runtime,
harness: this.runtime.harness,
reloadWebview: () => this.reloadWebview(webviewId),
showLogs: this.showLogs,
logError: (message, error) => this.logRuntimeError(message, error),
getSession: () => this.runtime.getSessionForView(webviewId),
getSessionId: () => this.fileManager.getSessionId(webviewId),
getOrCreateSession: async (model, effort, sessionId) => {
const runtime = await this.runtime.openSession({
webviewId,
workDir: this.requireWorkDir(webviewId),
model,
effort,
yoloMode: VSCodeSettings.yoloMode,
...(sessionId === undefined ? {} : { sessionId }),
});
this.fileManager.setSession(webviewId, baselineSession(runtime));
return runtime;
},
resumeSession: async (sessionId) => {
const current = this.runtime.getSession(sessionId);
const session =
current?.session ??
(await this.runtime.harness.resumeSession({ id: sessionId, includeSubagents: true }));
if (!areSameFsPath(session.workDir, this.requireWorkDir(webviewId))) {
if (current === undefined) {
await session.close().catch((error: unknown) => {
this.logRuntimeError("Unable to close a rejected session", error);
});
}
throw new Error("The selected session belongs to a different working directory.");
}
const runtime = await this.runtime.attachResumedSession(
webviewId,
session,
VSCodeSettings.yoloMode,
);
this.fileManager.setSession(webviewId, baselineSession(runtime));
return runtime;
},
closeSession: async () => {
await this.runtime.detachView(webviewId);
this.fileManager.clearSession(webviewId);
},
saveAllDirty: () => this.saveAllDirty(),
setCustomWorkDir: (workDir) => this.setCustomWorkDir(webviewId, workDir),
};
}
private async saveAllDirty(): Promise<void> {
const dirty = vscode.workspace.textDocuments.filter((document) => document.isDirty && !document.isUntitled);
await Promise.all(dirty.map((document) => document.save()));
}
async disposeView(webviewId: string): Promise<void> {
await this.runtime.detachView(webviewId);
this.customWorkDirs.delete(webviewId);
this.fileManager.disposeView(webviewId);
}
async getEditorMention(
webviewId: string,
documentUri: vscode.Uri,
selection: vscode.Selection,
): Promise<string | null> {
const workDirUri = this.getWorkDirUri(webviewId);
// Mirror the CLI/TUI: no UI-level directory gate on mentions. Inside the
// working directory the mention is relative; outside it (for example a
// file under the session's additionalDirs) it falls back to the absolute
// path, and the session's tool layer decides readability. Virtual
// documents (untitled:, git:, ...) have no meaningful path to mention.
if (workDirUri === null || documentUri.scheme !== workDirUri.scheme) return null;
const filePath = relativeWorkspacePath(workDirUri, documentUri) ?? documentUri.fsPath;
// Quote paths containing spaces, as the CLI/TUI mention completers do, so
// whitespace cannot split the path; any line range goes after the quote.
const mentionTarget = filePath.includes(" ") ? `"${filePath}"` : filePath;
if (selection.isEmpty) return `@${mentionTarget}`;
return selection.start.line === selection.end.line
? `@${mentionTarget}:${selection.start.line + 1}`
: `@${mentionTarget}:${selection.start.line + 1}-${selection.end.line + 1}`;
}
captureFileBaseline(
session: BaselineSession,
filePath: string,
webviewIds: readonly string[],
): void {
const workspaceRoot = this.workspaceRoot;
const workspaceRootUri = this.workspaceRootUri;
if (workspaceRoot === null || workspaceRootUri === null) return;
const workDirUri = workDirUriFromPath(workspaceRootUri, workspaceRoot, session.workDir);
if (
workDirUri === undefined ||
!isWorkspacePathContainedSync(workspaceRootUri, workDirUri)
) {
this.logRuntimeError(
"Unable to capture a file baseline",
new Error("Session working directory is outside the workspace"),
);
return;
}
const resolved = resolveSessionFilePath(workDirUri, session.workDir, filePath);
if (
resolved === undefined ||
!isWorkspacePathContainedSync(workDirUri, resolved.uri, { allowMissing: true })
) {
this.logRuntimeError(
"Unable to capture a file baseline",
new Error("File is outside the session working directory"),
);
return;
}
const capture = this.baselineManager.capture(session, resolved.uri.fsPath);
void capture
.then(async () => {
await Promise.all(
webviewIds.map(async (webviewId) => {
this.fileManager.trackFile(webviewId, resolved.uri.fsPath);
await this.fileManager.refreshChanges(webviewId);
}),
);
})
.catch((error) => {
this.logRuntimeError("Unable to capture a file baseline", error);
});
}
async dispose(): Promise<void> {
this.fileManager.dispose();
await this.runtime.dispose();
}
async getBaselineContent(sessionId: string, filePath: string): Promise<string> {
const active = this.runtime.getSession(sessionId)?.summary;
const summary = active ?? (await this.runtime.harness.listSessions({ sessionId }))[0];
if (summary === undefined) throw new Error("Session was not found.");
return this.baselineManager.getContent(baselineSummary(summary), filePath);
}
private trace(id: string, method: string, durationMs: number, ok: boolean): void {
// Deliberately exclude params, prompt text, file paths, and credentials.
const line = `[bridge] id=${id} method=${method} ok=${String(ok)} durationMs=${durationMs}`;
console.debug(`[kimi-vscode] ${line}`);
this.writeLog(line);
}
private logRuntimeError(message: string, error?: unknown): void {
const detail = errorDetail(error);
const line = `${message}${detail ? `: ${detail}` : ""}`;
console.error(`[kimi-vscode] ${line}`);
this.writeLog(line);
}
}
function errorDetail(error: unknown): string {
if (error === undefined) return "";
if (error instanceof Error) return `${error.name}: ${error.message}`;
if (typeof error === "string") return error;
if (typeof error === "number" || typeof error === "bigint" || typeof error === "boolean") {
return String(error);
}
return "Unknown error";
}
function baselineSession(runtime: SessionRuntime): BaselineSession {
return baselineSummary({
id: runtime.id,
workDir: runtime.session.workDir,
metadata: runtime.summary?.metadata,
});
}
function baselineSummary(summary: Pick<BaselineSession, "id" | "workDir" | "metadata">): BaselineSession {
return {
id: summary.id,
workDir: summary.workDir,
...(summary.metadata === undefined ? {} : { metadata: summary.metadata }),
};
}
function resolveSessionFilePath(
workDirUri: vscode.Uri,
workDir: string,
filePath: string,
): WorkspacePath | undefined {
if (path.isAbsolute(filePath) || path.win32.isAbsolute(filePath)) {
const uri = workDirUriFromPath(workDirUri, workDir, filePath);
if (uri === undefined) return undefined;
const relativePath = relativeWorkspacePath(workDirUri, uri);
return relativePath === undefined ? undefined : { uri, relativePath };
}
return resolveWorkspacePath(workDirUri, filePath);
}
|