File size: 10,270 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
import * as path from "node:path";
import * as vscode from "vscode";
import type { SessionSummary } from "@moonshot-ai/kimi-code-sdk";

import { Events, Methods } from "../../shared/bridge";
import type { SessionInfo } from "../../shared/legacy-sdk";
import type { BaselineSession } from "../managers/baseline.manager";
import { replaySessionToWebviewEvents } from "../runtime/replay-adapter";
import { areSameFsPath, isFsPathInsideOrEqual } from "../utils/fs-path";
import {
  isWorkspacePathContained,
  workDirUriFromPath,
} from "../utils/workspace-path";
import type { Handler } from "./types";

const SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;

interface LoadHistoryParams {
  kimiSessionId: string;
}

interface DeleteSessionParams {
  sessionId: string;
}

interface ForkSessionParams {
  sessionId: string;
  turnIndex: number;
}

export const sessionHandlers: Record<string, Handler<any, any>> = {
  [Methods.GetKimiSessions]: async (_, ctx): Promise<SessionInfo[]> => {
    if (!ctx.workDir) return [];
    return (await ctx.harness.listSessions({ workDir: ctx.workDir })).map(toSessionInfo);
  },

  [Methods.GetAllKimiSessions]: async (_, ctx): Promise<SessionInfo[]> => {
    if (!ctx.workspaceRoot) return [];
    return (await ctx.harness.listSessions())
      .filter((session) => isInsideOrEqual(ctx.workspaceRoot!, session.workDir))
      .map(toSessionInfo);
  },

  [Methods.GetRegisteredWorkDirs]: async (_, ctx): Promise<string[]> => {
    if (!ctx.workspaceRoot) return [];
    const sessions = await ctx.harness.listSessions();
    const candidates = [ctx.workspaceRoot, ctx.workDir, ...sessions.map((session) => session.workDir)];
    return [
      ...new Set(
        candidates.filter((workDir): workDir is string => workDir !== null && isInsideOrEqual(ctx.workspaceRoot!, workDir)),
      ),
    ].toSorted();
  },

  [Methods.SetWorkDir]: async (params: { workDir: string | null }, ctx) => {
    if (!ctx.workspaceRoot || !ctx.workspaceRootUri) return { ok: false };
    const target = params.workDir;
    if (target) {
      const targetUri = workDirUriFromPath(ctx.workspaceRootUri, ctx.workspaceRoot, target);
      if (targetUri === undefined || !(await isWorkspacePathContained(ctx.workspaceRootUri, targetUri))) {
        return { ok: false };
      }
    }
    try {
      await ctx.setCustomWorkDir(target);
    } catch {
      return { ok: false };
    }
    return { ok: true, workDir: target ?? ctx.workspaceRoot };
  },

  [Methods.BrowseWorkDir]: async (_, ctx) => {
    if (!ctx.workspaceRoot || !ctx.workspaceRootUri) return { ok: false, workDir: null };
    const workspaceUri = ctx.workspaceRootUri;
    let subdirectories: string[] = [];
    try {
      const entries = await vscode.workspace.fs.readDirectory(workspaceUri);
      subdirectories = entries
        .filter(([name, type]) => type === vscode.FileType.Directory && !name.startsWith("."))
        .map(([name]) => name)
        .toSorted();
    } catch {
      // The native picker remains available when directory enumeration fails.
    }

    const picked = await vscode.window.showQuickPick(
      [
        { label: "$(folder) Browse...", description: "Open folder picker", alwaysShow: true },
        { label: "", kind: vscode.QuickPickItemKind.Separator },
        ...subdirectories.map((name) => ({
          label: `$(folder) ${name}`,
          description: path.join(ctx.workspaceRoot!, name),
        })),
      ],
      { placeHolder: "Select a subdirectory or browse...", title: "Working Directory" },
    );
    if (!picked) return { ok: false, workDir: null };

    let selectedUri: vscode.Uri;
    if (picked.label === "$(folder) Browse...") {
      const result = await vscode.window.showOpenDialog({
        canSelectFiles: false,
        canSelectFolders: true,
        canSelectMany: false,
        defaultUri: workspaceUri,
        openLabel: "Select Working Directory",
      });
      if (!result?.[0]) return { ok: false, workDir: null };
      selectedUri = result[0];
    } else if (picked.description) {
      const pickedUri = workDirUriFromPath(workspaceUri, ctx.workspaceRoot, picked.description);
      if (pickedUri === undefined) return { ok: false, workDir: null };
      selectedUri = pickedUri;
    } else {
      return { ok: false, workDir: null };
    }

    if (!(await isWorkspacePathContained(workspaceUri, selectedUri))) {
      await vscode.window.showWarningMessage("Selected directory must be within the workspace.");
      return { ok: false, workDir: null };
    }
    const selected = selectedUri.fsPath;
    await ctx.setCustomWorkDir(selected === ctx.workspaceRoot ? null : selected);
    return { ok: true, workDir: selected };
  },

  [Methods.LoadKimiSessionHistory]: async (params: LoadHistoryParams, ctx) => {
    if (!ctx.workDir || !isSessionId(params.kimiSessionId)) return [];
    const runtime = await ctx.resumeSession(params.kimiSessionId);
    if (!areSameFsPath(runtime.session.workDir, ctx.workDir)) {
      await ctx.closeSession();
      throw new Error("The selected session belongs to a different working directory.");
    }

    let history: ReturnType<typeof replaySessionToWebviewEvents>;
    try {
      const resumeState = runtime.session.getResumeState();
      if (resumeState?.agents["main"] === undefined) {
        throw new Error("Session history is unavailable.");
      }
      history = replaySessionToWebviewEvents(resumeState, runtime.id);
    } catch (error) {
      await ctx.closeSession();
      throw error;
    }

    ctx.fileManager.clearTracked(ctx.webviewId);
    const baseline = baselineSession(runtime.summary ?? {
      id: runtime.id,
      workDir: runtime.session.workDir,
    });
    try {
      const changes = await ctx.baselineManager.getChanges(baseline);
      for (const change of changes) {
        ctx.fileManager.trackFile(ctx.webviewId, path.join(baseline.workDir, change.path));
      }
      ctx.broadcast(Events.FileChangesUpdated, changes, ctx.webviewId);
    } catch (error) {
      ctx.logError("Unable to restore session file changes", error);
      ctx.broadcast(Events.FileChangesUpdated, [], ctx.webviewId);
      void Promise.resolve(
        vscode.window.showWarningMessage(
          "Kimi: This conversation opened, but its file change history is unavailable.",
          "Show Logs",
        ),
      )
        .then((action) => {
          if (action === "Show Logs") ctx.showLogs();
        })
        .catch((noticeError: unknown) => {
          ctx.logError("Unable to show the file change warning", noticeError);
        });
    }
    return history;
  },

  [Methods.DeleteKimiSession]: async (params: DeleteSessionParams, ctx): Promise<{ ok: boolean }> => {
    if (!isSessionId(params.sessionId) || !ctx.workspaceRoot) return { ok: false };
    const summary = (await ctx.harness.listSessions({ sessionId: params.sessionId }))[0];
    if (summary === undefined || !isInsideOrEqual(ctx.workspaceRoot, summary.workDir)) {
      return { ok: false };
    }
    const affectedViews = ctx.runtime.getSession(params.sessionId)?.subscribers ?? [];
    await ctx.runtime.deleteSession(params.sessionId);
    await ctx.baselineManager.deleteSession(params.sessionId);
    for (const webviewId of affectedViews) {
      ctx.fileManager.clearSession(webviewId);
      ctx.broadcast(Events.FileChangesUpdated, [], webviewId);
      if (webviewId !== ctx.webviewId) {
        ctx.broadcast(Events.NewConversation, {}, webviewId);
      }
    }
    return { ok: true };
  },

  [Methods.ForkKimiSession]: async (params: ForkSessionParams, ctx) => {
    if (!ctx.workDir || !isSessionId(params.sessionId) || !Number.isInteger(params.turnIndex) || params.turnIndex < 0) {
      return null;
    }
    const summaries = await ctx.harness.listSessions({ sessionId: params.sessionId });
    const sourceSummary = summaries[0];
    if (
      sourceSummary === undefined ||
      !ctx.workspaceRoot ||
      !isInsideOrEqual(ctx.workspaceRoot, sourceSummary.workDir)
    ) return null;

    const forkSettledSession = async () => {
      const fork = await ctx.harness.forkSession({ id: params.sessionId, turnIndex: params.turnIndex });
      const targetSummary = fork.summary;
      if (targetSummary === undefined) {
        await fork.close();
        throw new Error("Forked session metadata is unavailable.");
      }

      let materializeError: unknown;
      try {
        await ctx.baselineManager.materializeToFork(
          baselineSession(sourceSummary),
          baselineSession(targetSummary),
        );
      } catch (error) {
        materializeError = error;
      }

      try {
        await fork.close();
      } catch (error) {
        ctx.logError("Unable to close a forked session", error);
      }
      if (materializeError !== undefined) {
        await ctx.harness.deleteSession(targetSummary.id).catch((error: unknown) => {
          ctx.logError(`Unable to remove failed fork "${targetSummary.id}"`, error);
        });
        await ctx.baselineManager.deleteSession(targetSummary.id).catch((error: unknown) => {
          ctx.logError(`Unable to remove failed fork baseline "${targetSummary.id}"`, error);
        });
        throw materializeError instanceof Error
          ? materializeError
          : new Error("Unable to materialize the forked session baseline.", {
              cause: materializeError,
            });
      }
      return { sessionId: targetSummary.id };
    };

    const active = ctx.runtime.getSession(params.sessionId);
    return active === undefined
      ? forkSettledSession()
      : active.runExclusiveAfterCancelling(forkSettledSession);
  },
};

function toSessionInfo(summary: SessionSummary): SessionInfo {
  return {
    id: summary.id,
    workDir: summary.workDir,
    updatedAt: summary.updatedAt,
    brief: summary.title ?? summary.lastPrompt ?? "",
  };
}

function baselineSession(summary: Pick<SessionSummary, "id" | "workDir" | "metadata">): BaselineSession {
  return { id: summary.id, workDir: summary.workDir, metadata: summary.metadata };
}

function isInsideOrEqual(root: string, candidate: string): boolean {
  return isFsPathInsideOrEqual(root, candidate);
}

function isSessionId(value: string): boolean {
  return SESSION_ID.test(value);
}