Gateway: harden custom session-store discovery

This commit is contained in:
Gustavo Madeira Santana
2026-03-12 15:31:16 +00:00
parent dc3bb1890b
commit 0d17babcbb
14 changed files with 517 additions and 171 deletions

View File

@@ -1,9 +1,15 @@
import type { Dirent } from "node:fs";
import fsSync, { type Dirent } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
export async function resolveAgentSessionDirs(stateDir: string): Promise<string[]> {
const agentsDir = path.join(stateDir, "agents");
function mapAgentSessionDirs(agentsDir: string, entries: Dirent[]): string[] {
return entries
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(agentsDir, entry.name, "sessions"))
.toSorted((a, b) => a.localeCompare(b));
}
export async function resolveAgentSessionDirsFromAgentsDir(agentsDir: string): Promise<string[]> {
let entries: Dirent[] = [];
try {
entries = await fs.readdir(agentsDir, { withFileTypes: true });
@@ -15,8 +21,24 @@ export async function resolveAgentSessionDirs(stateDir: string): Promise<string[
throw err;
}
return entries
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(agentsDir, entry.name, "sessions"))
.toSorted((a, b) => a.localeCompare(b));
return mapAgentSessionDirs(agentsDir, entries);
}
export function resolveAgentSessionDirsFromAgentsDirSync(agentsDir: string): string[] {
let entries: Dirent[] = [];
try {
entries = fsSync.readdirSync(agentsDir, { withFileTypes: true });
} catch (err) {
const code = (err as { code?: string }).code;
if (code === "ENOENT") {
return [];
}
throw err;
}
return mapAgentSessionDirs(agentsDir, entries);
}
export async function resolveAgentSessionDirs(stateDir: string): Promise<string[]> {
return await resolveAgentSessionDirsFromAgentsDir(path.join(stateDir, "agents"));
}