Fix session file and elapsed replay state
This commit is contained in:
@@ -16,15 +16,24 @@ export async function GET(req: Request) {
|
||||
|
||||
const url = new URL(req.url);
|
||||
const requestedPath = url.searchParams.get("path");
|
||||
const sessionId = url.searchParams.get("session_id");
|
||||
const sessionId = normalizeSessionId(url.searchParams.get("session_id"));
|
||||
if (!requestedPath && sessionId) {
|
||||
return Response.json({
|
||||
session_id: sessionId,
|
||||
input: await listSessionFiles(account.id, sessionId, "input"),
|
||||
output: await listSessionFiles(account.id, sessionId, "output"),
|
||||
});
|
||||
}
|
||||
if (!requestedPath) {
|
||||
return Response.json({ error: "缺少文件路径或会话 ID" }, { status: 400 });
|
||||
const latestSessionId = await findLatestSessionId(account.id);
|
||||
if (!latestSessionId) {
|
||||
return Response.json({ session_id: null, input: [], output: [] });
|
||||
}
|
||||
return Response.json({
|
||||
session_id: latestSessionId,
|
||||
input: await listSessionFiles(account.id, latestSessionId, "input"),
|
||||
output: await listSessionFiles(account.id, latestSessionId, "output"),
|
||||
});
|
||||
}
|
||||
|
||||
const accountRoot = path.resolve(accountBaseRoot(account.id));
|
||||
@@ -87,6 +96,37 @@ async function listSessionFiles(
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeSessionId(value: string | null) {
|
||||
const trimmed = value?.trim();
|
||||
return trimmed || null;
|
||||
}
|
||||
|
||||
async function findLatestSessionId(accountId: string) {
|
||||
const sessionsRoot = path.join(accountBaseRoot(accountId), "sessions");
|
||||
try {
|
||||
const entries = await readdir(sessionsRoot, { withFileTypes: true });
|
||||
const candidates = await Promise.all(
|
||||
entries
|
||||
.filter((entry) => entry.isDirectory())
|
||||
.map(async (entry) => {
|
||||
const sessionDir = path.join(sessionsRoot, entry.name);
|
||||
const sessionFile = path.join(sessionDir, "session.json");
|
||||
try {
|
||||
const sessionStat = await stat(sessionFile);
|
||||
return { id: entry.name, modifiedAt: sessionStat.mtimeMs };
|
||||
} catch {
|
||||
const dirStat = await stat(sessionDir);
|
||||
return { id: entry.name, modifiedAt: dirStat.mtimeMs };
|
||||
}
|
||||
}),
|
||||
);
|
||||
candidates.sort((a, b) => b.modifiedAt - a.modifiedAt);
|
||||
return candidates.at(0)?.id ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function contentTypeFor(filePath: string) {
|
||||
const ext = path.extname(filePath).toLowerCase();
|
||||
if (ext === ".json") return "application/json; charset=utf-8";
|
||||
|
||||
Reference in New Issue
Block a user