MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / SessionStore

Class SessionStore

packages/agent-core/src/session/store/session-store.ts:40–345  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38export type SessionStoreOptions = Record<string, never>;
39
40export class SessionStore {
41 readonly sessionsDir: string;
42
43 constructor(
44 readonly homeDir: string,
45 _options: SessionStoreOptions = {},
46 ) {
47 this.sessionsDir = join(homeDir, 'sessions');
48 }
49
50 sessionDirFor(input: { readonly id: string; readonly workDir: string }): string {
51 assertSafeSessionId(input.id);
52 return join(this.sessionsDir, encodeWorkDirKey(normalizeWorkDir(input.workDir)), input.id);
53 }
54
55 async create(input: CreateSessionRecordInput): Promise<SessionSummary> {
56 assertSafeSessionId(input.id);
57 const workDir = normalizeWorkDir(input.workDir);
58 const indexed = await this.findSessionEntry(input.id);
59 if (indexed !== undefined) {
60 throw new KimiError(ErrorCodes.SESSION_ALREADY_EXISTS, `Session "${input.id}" already exists`);
61 }
62
63 const dir = this.sessionDirFor({ id: input.id, workDir });
64 if (await isDirectory(dir)) {
65 throw new KimiError(ErrorCodes.SESSION_ALREADY_EXISTS, `Session "${input.id}" already exists`);
66 }
67
68 await mkdir(dir, { recursive: true, mode: 0o700 });
69 await appendSessionIndexEntry(this.homeDir, {
70 sessionId: input.id,
71 sessionDir: dir,
72 workDir,
73 });
74 return this.summaryFromDir(input.id, dir, workDir);
75 }
76
77 async fork(input: ForkSessionRecordInput): Promise<SessionSummary> {
78 const source = await this.findExistingSessionEntry(input.sourceId);
79 assertSafeSessionId(input.targetId);
80 const indexed = await this.findSessionEntry(input.targetId);
81 if (indexed !== undefined) {
82 throw new KimiError(ErrorCodes.SESSION_ALREADY_EXISTS, `Session "${input.targetId}" already exists`);
83 }
84
85 const targetDir = this.sessionDirFor({ id: input.targetId, workDir: source.workDir });
86 if (await isDirectory(targetDir)) {
87 throw new KimiError(ErrorCodes.SESSION_ALREADY_EXISTS, `Session "${input.targetId}" already exists`);
88 }
89
90 await mkdir(dirname(targetDir), { recursive: true, mode: 0o700 });
91 try {
92 await cp(source.sessionDir, targetDir, {
93 recursive: true,
94 force: false,
95 errorOnExist: true,
96 });
97 await dropForkedSessionFiles(targetDir);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected