Fix session file and elapsed replay state
This commit is contained in:
@@ -222,6 +222,7 @@ type SessionFile = {
|
||||
};
|
||||
|
||||
type SessionFilesPayload = {
|
||||
session_id?: string | null;
|
||||
input?: SessionFile[];
|
||||
output?: SessionFile[];
|
||||
error?: string;
|
||||
@@ -243,24 +244,37 @@ function SessionFilesPanel({
|
||||
const activeSessionId = normalizeSessionId(
|
||||
sessionId ?? window.localStorage.getItem("claw.activeSessionId"),
|
||||
);
|
||||
if (!activeSessionId) {
|
||||
setPayload({ input: [], output: [] });
|
||||
setStatus("当前还没有可用会话。");
|
||||
return;
|
||||
}
|
||||
window.localStorage.setItem("claw.activeSessionId", activeSessionId);
|
||||
setStatus("正在读取聊天中的文件...");
|
||||
try {
|
||||
const url = new URL("/api/claw/files", window.location.origin);
|
||||
url.searchParams.set("session_id", activeSessionId);
|
||||
const response = await fetch(url, { cache: "no-store" });
|
||||
const nextPayload = (await response.json()) as SessionFilesPayload;
|
||||
let { response, payload: nextPayload } =
|
||||
await fetchSessionFiles(activeSessionId);
|
||||
if (
|
||||
activeSessionId &&
|
||||
response.ok &&
|
||||
sessionFilesCount(nextPayload) === 0
|
||||
) {
|
||||
const latest = await fetchSessionFiles(null);
|
||||
if (
|
||||
latest.response.ok &&
|
||||
latest.payload.session_id &&
|
||||
latest.payload.session_id !== activeSessionId
|
||||
) {
|
||||
response = latest.response;
|
||||
nextPayload = latest.payload;
|
||||
}
|
||||
}
|
||||
if (cancelled) return;
|
||||
if (!response.ok) {
|
||||
setPayload({ input: [], output: [], error: nextPayload.error });
|
||||
setStatus(nextPayload.error ?? "读取文件失败");
|
||||
return;
|
||||
}
|
||||
if (nextPayload.session_id) {
|
||||
window.localStorage.setItem(
|
||||
"claw.activeSessionId",
|
||||
nextPayload.session_id,
|
||||
);
|
||||
}
|
||||
setPayload(nextPayload);
|
||||
setStatus("");
|
||||
} catch (err) {
|
||||
@@ -322,6 +336,18 @@ function SessionFilesPanel({
|
||||
);
|
||||
}
|
||||
|
||||
async function fetchSessionFiles(sessionId: string | null) {
|
||||
const url = new URL("/api/claw/files", window.location.origin);
|
||||
if (sessionId) url.searchParams.set("session_id", sessionId);
|
||||
const response = await fetch(url, { cache: "no-store" });
|
||||
const payload = (await response.json()) as SessionFilesPayload;
|
||||
return { response, payload };
|
||||
}
|
||||
|
||||
function sessionFilesCount(payload: SessionFilesPayload) {
|
||||
return (payload.input?.length ?? 0) + (payload.output?.length ?? 0);
|
||||
}
|
||||
|
||||
function normalizeSessionId(value: unknown) {
|
||||
if (typeof value !== "string") return null;
|
||||
const trimmed = value.trim();
|
||||
|
||||
Reference in New Issue
Block a user