-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-index.js
More file actions
176 lines (147 loc) · 4.28 KB
/
Copy pathsession-index.js
File metadata and controls
176 lines (147 loc) · 4.28 KB
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
"use strict";
const { open, readdir } = require("node:fs/promises");
const os = require("node:os");
const path = require("node:path");
const SESSION_INDEX_FILENAME = "session_index.jsonl";
const SESSIONS_DIRECTORY_NAME = "sessions";
const SESSION_METADATA_PREFIX_BYTES = 64 * 1024;
const SESSION_ID_IN_FILENAME =
/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i;
function getCodexHome(environment = process.env, homeDirectory = os.homedir()) {
const configuredHome = environment.CODEX_HOME?.trim();
return configuredHome
? path.resolve(configuredHome)
: path.join(homeDirectory, ".codex");
}
function getSessionIndexPath(codexHome = getCodexHome()) {
return path.join(codexHome, SESSION_INDEX_FILENAME);
}
function getSessionsDirectory(codexHome = getCodexHome()) {
return path.join(codexHome, SESSIONS_DIRECTORY_NAME);
}
function parseSessionIndex(contents) {
const sessionsById = new Map();
for (const line of contents.split(/\r?\n/)) {
if (!line.trim()) {
continue;
}
let value;
try {
value = JSON.parse(line);
} catch {
continue;
}
if (
typeof value?.id !== "string" ||
!/^[a-zA-Z0-9_-]+$/.test(value.id)
) {
continue;
}
const updatedAt =
typeof value.updated_at === "string" &&
Number.isFinite(Date.parse(value.updated_at))
? value.updated_at
: undefined;
const title =
typeof value.thread_name === "string" && value.thread_name.trim()
? value.thread_name.trim()
: "Untitled chat";
const existing = sessionsById.get(value.id);
if (
!existing ||
(updatedAt &&
(!existing.updatedAt ||
Date.parse(updatedAt) >= Date.parse(existing.updatedAt)))
) {
sessionsById.set(value.id, {
id: value.id,
title,
updatedAt,
});
}
}
return [...sessionsById.values()].sort((left, right) => {
const timeDifference =
(right.updatedAt ? Date.parse(right.updatedAt) : 0) -
(left.updatedAt ? Date.parse(left.updatedAt) : 0);
return timeDifference || left.title.localeCompare(right.title);
});
}
async function readSessionWorkspace(filePath) {
let handle;
try {
handle = await open(filePath, "r");
const buffer = Buffer.alloc(SESSION_METADATA_PREFIX_BYTES);
const { bytesRead } = await handle.read(
buffer,
0,
buffer.length,
0,
);
const prefix = buffer.toString("utf8", 0, bytesRead);
const cwdMatch = prefix.match(/"cwd":("(?:\\.|[^"\\])*")/);
return cwdMatch ? JSON.parse(cwdMatch[1]) : undefined;
} catch {
return undefined;
} finally {
await handle?.close().catch(() => undefined);
}
}
async function findSessionFiles(directory, requestedSessionIds, results) {
let entries;
try {
entries = await readdir(directory, { withFileTypes: true });
} catch {
return;
}
await Promise.all(
entries.map(async (entry) => {
const entryPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
await findSessionFiles(entryPath, requestedSessionIds, results);
return;
}
const sessionId = entry.name.match(SESSION_ID_IN_FILENAME)?.[1];
if (
sessionId &&
requestedSessionIds.has(sessionId) &&
!results.has(sessionId)
) {
const workspacePath = await readSessionWorkspace(entryPath);
if (workspacePath) {
results.set(sessionId, workspacePath);
}
}
}),
);
}
async function loadSessionWorkspaces(sessionsDirectory, sessionIds) {
const requestedSessionIds = new Set(sessionIds);
const results = new Map();
if (requestedSessionIds.size > 0) {
await findSessionFiles(sessionsDirectory, requestedSessionIds, results);
}
return results;
}
function isPathInsideWorkspace(candidatePath, workspacePath) {
if (!candidatePath || !workspacePath) {
return false;
}
const relativePath = path.relative(
path.resolve(workspacePath),
path.resolve(candidatePath),
);
return (
relativePath === "" ||
(!relativePath.startsWith("..") && !path.isAbsolute(relativePath))
);
}
module.exports = {
SESSION_INDEX_FILENAME,
getCodexHome,
getSessionIndexPath,
getSessionsDirectory,
isPathInsideWorkspace,
loadSessionWorkspaces,
parseSessionIndex,
};