fix: persist agent scene generation pending state
This commit is contained in:
parent
ae93dfcfda
commit
d73f6d6a29
@ -527,9 +527,76 @@ function getReplacementCardStatusMeta(status: ReplacementCardStatus) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface LocalGenerationPendingState {
|
interface LocalGenerationPendingState {
|
||||||
|
chatId: string;
|
||||||
modeType: AgentGenerationResultModeType;
|
modeType: AgentGenerationResultModeType;
|
||||||
expectedCount: number;
|
expectedCount: number;
|
||||||
userTurnCountAtStart: number;
|
userTurnCountAtStart: number;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AGENT_PENDING_GENERATION_STORAGE_KEY = "ai-chatbot-agent-pending-generations";
|
||||||
|
const AGENT_PENDING_GENERATION_EXPIRY_MS = 30 * 60 * 1000;
|
||||||
|
|
||||||
|
function getStoredAgentPendingGenerations(): LocalGenerationPendingState[] {
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const raw = window.localStorage.getItem(AGENT_PENDING_GENERATION_STORAGE_KEY);
|
||||||
|
if (!raw) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
|
const parsed = JSON.parse(raw);
|
||||||
|
if (!Array.isArray(parsed)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed.filter((item) => {
|
||||||
|
if (!item || typeof item !== "object") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const record = item as Record<string, unknown>;
|
||||||
|
const chatId = String(record.chatId || "").trim();
|
||||||
|
const modeType = String(record.modeType || "").trim();
|
||||||
|
const expectedCount = Number(record.expectedCount);
|
||||||
|
const userTurnCountAtStart = Number(record.userTurnCountAtStart);
|
||||||
|
const timestamp = Number(record.timestamp);
|
||||||
|
return (
|
||||||
|
Boolean(chatId) &&
|
||||||
|
(modeType === "scene_generation" ||
|
||||||
|
modeType === "detail_replacement" ||
|
||||||
|
modeType === "sku_replacement") &&
|
||||||
|
Number.isFinite(expectedCount) &&
|
||||||
|
expectedCount > 0 &&
|
||||||
|
Number.isFinite(userTurnCountAtStart) &&
|
||||||
|
Number.isFinite(timestamp) &&
|
||||||
|
now - timestamp < AGENT_PENDING_GENERATION_EXPIRY_MS
|
||||||
|
);
|
||||||
|
}) as LocalGenerationPendingState[];
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveStoredAgentPendingGenerations(items: LocalGenerationPendingState[]) {
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
window.localStorage.setItem(
|
||||||
|
AGENT_PENDING_GENERATION_STORAGE_KEY,
|
||||||
|
JSON.stringify(items)
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(
|
||||||
|
"[AgentGenerationWorkspaceDarkV2] Failed to persist pending generation state:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function AnimatedGenerationPlaceholderCard(params: {
|
function AnimatedGenerationPlaceholderCard(params: {
|
||||||
@ -693,6 +760,27 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
const [pendingGeneration, setPendingGeneration] =
|
const [pendingGeneration, setPendingGeneration] =
|
||||||
useState<LocalGenerationPendingState | null>(null);
|
useState<LocalGenerationPendingState | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const restored = getStoredAgentPendingGenerations().find(
|
||||||
|
(item) => item.chatId === chatId
|
||||||
|
);
|
||||||
|
if (!restored) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setPendingGeneration((current) => current || restored);
|
||||||
|
}, [chatId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const allItems = getStoredAgentPendingGenerations().filter(
|
||||||
|
(item) => item.chatId !== chatId
|
||||||
|
);
|
||||||
|
if (pendingGeneration) {
|
||||||
|
allItems.push(pendingGeneration);
|
||||||
|
}
|
||||||
|
saveStoredAgentPendingGenerations(allItems);
|
||||||
|
}, [chatId, pendingGeneration]);
|
||||||
|
|
||||||
const { data: historyData } = useSWR<{ messages: ChatMessage[] }>(
|
const { data: historyData } = useSWR<{ messages: ChatMessage[] }>(
|
||||||
rawChatId ? `/api/messages?chatId=${rawChatId}` : null,
|
rawChatId ? `/api/messages?chatId=${rawChatId}` : null,
|
||||||
fetcher,
|
fetcher,
|
||||||
@ -1613,6 +1701,10 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pendingGeneration.modeType === "scene_generation") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!hasAssistantFailureAfterLastUser) {
|
if (!hasAssistantFailureAfterLastUser) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1911,6 +2003,7 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
setActiveResultModeType(currentResultModeType);
|
setActiveResultModeType(currentResultModeType);
|
||||||
setSelectedResultUrl(null);
|
setSelectedResultUrl(null);
|
||||||
setPendingGeneration({
|
setPendingGeneration({
|
||||||
|
chatId,
|
||||||
modeType: currentResultModeType,
|
modeType: currentResultModeType,
|
||||||
expectedCount:
|
expectedCount:
|
||||||
currentResultModeType === "scene_generation"
|
currentResultModeType === "scene_generation"
|
||||||
@ -1919,6 +2012,7 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
? detailReplacementReferenceCards.length
|
? detailReplacementReferenceCards.length
|
||||||
: skuReplacementReferenceCards.length,
|
: skuReplacementReferenceCards.length,
|
||||||
userTurnCountAtStart: userMessageCount,
|
userTurnCountAtStart: userMessageCount,
|
||||||
|
timestamp: Date.now(),
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
sendMessage({ role: "user", parts });
|
sendMessage({ role: "user", parts });
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user