-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrepository.ts
More file actions
453 lines (399 loc) · 11.9 KB
/
Copy pathrepository.ts
File metadata and controls
453 lines (399 loc) · 11.9 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import { cloneRepository, openRepository } from "es-git";
import type { Repository } from "es-git";
import { basename, dirname, join } from "@std/path";
/** The FEP repository URL */
const FEP_REPO_URL = "https://codeberg.org/fediverse/fep.git";
/** Environment variable for overriding the shared repository directory */
const REPOSITORY_DIR_ENV = "FEP_MCP_REPOSITORY_DIR";
/** Maximum retry attempts for clone operation */
const MAX_RETRIES = 3;
/** Base delay for exponential backoff (ms) */
const BASE_DELAY_MS = 1000;
/** Current repository instance */
let currentRepo: Repository | null = null;
/** Current repository path */
let currentRepoPath: string | null = null;
export interface InitializeRepositoryOptions {
repositoryDir?: string;
repositoryUrl?: string;
onBeforeInstallClone?: (() => Promise<void>) | undefined;
}
/**
* Removes a path if it exists.
*/
async function removePathIfExists(path: string): Promise<void> {
try {
await Deno.remove(path, { recursive: true });
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
console.error(`Warning: Failed to remove ${path}:`, error);
}
}
}
/**
* Checks whether a path exists.
*/
async function pathExists(path: string): Promise<boolean> {
try {
await Deno.stat(path);
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return false;
}
throw error;
}
}
/**
* Delays execution for the specified milliseconds.
*/
function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Gets the user's home directory.
*/
function getHomeDirectory(): string {
const home = Deno.env.get("HOME") ?? Deno.env.get("USERPROFILE");
if (!home) {
throw new Error("Unable to determine the user's home directory.");
}
return home;
}
/**
* Gets the default cache directory for the current platform.
*/
function getDefaultCacheDirectory(): string {
switch (Deno.build.os) {
case "windows":
return Deno.env.get("LOCALAPPDATA") ??
Deno.env.get("APPDATA") ??
join(getHomeDirectory(), "AppData", "Local");
case "darwin":
return join(getHomeDirectory(), "Library", "Caches");
default:
return Deno.env.get("XDG_CACHE_HOME") ??
join(getHomeDirectory(), ".cache");
}
}
/**
* Resolves the shared repository path.
*/
function resolveRepositoryPath(
options: InitializeRepositoryOptions = {},
): string {
return options.repositoryDir ??
Deno.env.get(REPOSITORY_DIR_ENV) ??
join(getDefaultCacheDirectory(), "fep-mcp", "repository");
}
/**
* Resolves the repository URL.
*/
function resolveRepositoryUrl(
options: InitializeRepositoryOptions = {},
): string {
return options.repositoryUrl ?? FEP_REPO_URL;
}
/**
* Creates a unique temporary directory beside the shared repository path.
*/
async function createTempCloneDir(repositoryPath: string): Promise<string> {
await Deno.mkdir(dirname(repositoryPath), { recursive: true });
return await Deno.makeTempDir({
dir: dirname(repositoryPath),
prefix: `${basename(repositoryPath)}.tmp-`,
});
}
/**
* Clones the FEP repository with retry logic.
*
* @param repoUrl Remote repository URL
* @param destPath Destination path for the cloned repository
* @returns The cloned repository instance
* @throws Error if clone fails after all retries
*/
async function cloneWithRetry(
repoUrl: string,
destPath: string,
): Promise<Repository> {
let lastError: Error | null = null;
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
console.error(
`Cloning FEP repository into shared cache (attempt ${attempt}/${MAX_RETRIES})...`,
);
const repo = await cloneRepository(repoUrl, destPath);
console.error("FEP repository cloned successfully.");
return repo;
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
console.error(`Clone attempt ${attempt} failed:`, lastError.message);
if (attempt < MAX_RETRIES) {
const delayMs = BASE_DELAY_MS * Math.pow(2, attempt - 1);
console.error(`Retrying in ${delayMs}ms...`);
await delay(delayMs);
await removePathIfExists(destPath);
}
}
}
throw new Error(
`Failed to clone FEP repository after ${MAX_RETRIES} attempts: ${lastError?.message}`,
);
}
/**
* Opens an existing repository if the shared cache contains a valid clone.
*/
async function openExistingRepository(
repositoryPath: string,
): Promise<Repository | null> {
try {
await Deno.stat(repositoryPath);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return null;
}
throw error;
}
try {
return await openRepository(repositoryPath);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(
`Shared repository cache at ${repositoryPath} is invalid; recreating it: ${message}`,
);
await removePathIfExists(repositoryPath);
return null;
}
}
/**
* Installs a freshly cloned repository into the shared cache path.
*/
async function installClonedRepository(
clonePath: string,
repositoryPath: string,
): Promise<void> {
if (await pathExists(repositoryPath)) {
console.error(
`Shared repository cache at ${repositoryPath} appeared during clone; reusing it.`,
);
try {
await openRepository(repositoryPath);
await removePathIfExists(clonePath);
return;
} catch (openError) {
const message = openError instanceof Error
? openError.message
: String(openError);
console.error(
`Existing shared repository cache was invalid after race; replacing it: ${message}`,
);
await removePathIfExists(repositoryPath);
}
}
try {
await Deno.rename(clonePath, repositoryPath);
} catch (error) {
if (
!(error instanceof Deno.errors.AlreadyExists) &&
!(await pathExists(repositoryPath))
) {
throw error;
}
console.error(
`Shared repository cache at ${repositoryPath} appeared during clone; reusing it.`,
);
try {
await openRepository(repositoryPath);
} catch (openError) {
const message = openError instanceof Error
? openError.message
: String(openError);
console.error(
`Existing shared repository cache was invalid after race; replacing it: ${message}`,
);
await removePathIfExists(repositoryPath);
await Deno.rename(clonePath, repositoryPath);
return;
}
await removePathIfExists(clonePath);
}
}
/**
* Syncs the working tree to the fetched remote default branch.
*/
async function syncRepositoryToRemoteDefaultBranch(
repo: Repository,
repositoryPath: string,
): Promise<Repository> {
const remote = repo.getRemote("origin");
await remote.fetch([]);
repo = await openRepository(repositoryPath);
const refreshedRemote = repo.getRemote("origin");
const defaultBranchRef = await refreshedRemote.defaultBranch();
repo = await openRepository(repositoryPath);
if (!defaultBranchRef.startsWith("refs/heads/")) {
throw new Error(
`Remote default branch has unexpected format: ${defaultBranchRef}`,
);
}
const branchName = defaultBranchRef.slice("refs/heads/".length);
const remoteTrackingRef = repo.getReference(
`refs/remotes/origin/${branchName}`,
)
.resolve();
const targetOid = remoteTrackingRef.target();
if (!targetOid) {
throw new Error(
`Remote tracking branch refs/remotes/origin/${branchName} has no target.`,
);
}
repo.setHeadDetached(repo.getCommit(targetOid));
repo.checkoutHead({ force: true, removeUntracked: true });
return await openRepository(repositoryPath);
}
/**
* Initializes the FEP repository using a shared on-disk cache.
* This should be called on server startup.
*
* @returns The shared repository path
* @throws Error if initialization fails
*/
export async function initializeRepository(
options: InitializeRepositoryOptions = {},
): Promise<string> {
currentRepo = null;
currentRepoPath = null;
const repositoryPath = resolveRepositoryPath(options);
const existingRepo = await openExistingRepository(repositoryPath);
if (existingRepo) {
currentRepo = existingRepo;
currentRepoPath = repositoryPath;
console.error(`Reusing shared FEP repository cache at: ${repositoryPath}`);
try {
await refreshRepository();
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(
`Warning: Failed to refresh shared repository cache, continuing with stale data: ${message}`,
);
}
return repositoryPath;
}
const tempClonePath = await createTempCloneDir(repositoryPath);
try {
await cloneWithRetry(resolveRepositoryUrl(options), tempClonePath);
await options.onBeforeInstallClone?.();
await installClonedRepository(tempClonePath, repositoryPath);
currentRepo = await openRepository(repositoryPath);
currentRepoPath = repositoryPath;
return repositoryPath;
} catch (error) {
await removePathIfExists(tempClonePath);
throw error;
}
}
/**
* Gets the current repository path.
*
* @returns The repository path or null if not initialized
*/
export function getRepositoryPath(): string | null {
return currentRepoPath;
}
/**
* Refreshes the repository by fetching the latest changes from origin.
*
* @throws Error if refresh fails
*/
export async function refreshRepository(): Promise<void> {
if (!currentRepo || !currentRepoPath) {
throw new Error(
"Repository not initialized. Call initializeRepository() first.",
);
}
try {
console.error("Refreshing shared FEP repository cache...");
currentRepo = await syncRepositoryToRemoteDefaultBranch(
currentRepo,
currentRepoPath,
);
console.error("FEP repository refreshed successfully.");
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to refresh repository: ${message}`);
}
}
/**
* Reads a file from the repository.
*
* @param relativePath Path relative to the repository root
* @returns The file content as a string
* @throws Error if the file cannot be read
*/
export async function readFile(relativePath: string): Promise<string> {
if (!currentRepoPath) {
throw new Error(
"Repository not initialized. Call initializeRepository() first.",
);
}
const fullPath = join(currentRepoPath, relativePath);
try {
return await Deno.readTextFile(fullPath);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
throw new Error(`File not found: ${relativePath}`);
}
throw error;
}
}
/**
* Checks if a file exists in the repository.
*
* @param relativePath Path relative to the repository root
* @returns true if the file exists
*/
export async function fileExists(relativePath: string): Promise<boolean> {
if (!currentRepoPath) {
return false;
}
const fullPath = join(currentRepoPath, relativePath);
try {
await Deno.stat(fullPath);
return true;
} catch {
return false;
}
}
/**
* Lists files in a directory within the repository.
*
* @param relativePath Path relative to the repository root
* @returns Array of file/directory names
*/
export async function listDirectory(relativePath: string): Promise<string[]> {
if (!currentRepoPath) {
throw new Error(
"Repository not initialized. Call initializeRepository() first.",
);
}
const fullPath = join(currentRepoPath, relativePath);
const entries: string[] = [];
try {
for await (const entry of Deno.readDir(fullPath)) {
entries.push(entry.name);
}
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
throw new Error(`Directory not found: ${relativePath}`);
}
throw error;
}
return entries;
}
/**
* Releases in-process repository state on shutdown.
*/
export function cleanupRepository(): void {
currentRepo = null;
currentRepoPath = null;
}