fix: preserve workflow prompt state in agent mode
This commit is contained in:
parent
328f1f4915
commit
14b6b3ec10
@ -5637,6 +5637,17 @@ export async function POST(request: Request) {
|
|||||||
? applyHiddenPromptToMessages(uiMessages, hiddenPromptText)
|
? applyHiddenPromptToMessages(uiMessages, hiddenPromptText)
|
||||||
: uiMessages;
|
: uiMessages;
|
||||||
|
|
||||||
|
if (hiddenPromptText) {
|
||||||
|
const hiddenPromptLines = hiddenPromptText
|
||||||
|
.split("\n")
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
console.info("[Chat API] Hidden workflow prompt summary:", {
|
||||||
|
lineCount: hiddenPromptLines.length,
|
||||||
|
preview: hiddenPromptLines.slice(0, 12),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const { longitude, latitude, city, country } = geolocation(request);
|
const { longitude, latitude, city, country } = geolocation(request);
|
||||||
|
|
||||||
const requestHints: RequestHints = {
|
const requestHints: RequestHints = {
|
||||||
|
|||||||
@ -133,6 +133,29 @@ const SIZE_PRESETS = [
|
|||||||
{ label: "9:16", value: "9:16", width: 1024, height: 1820 },
|
{ label: "9:16", value: "9:16", width: 1024, height: 1820 },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
function hasMeaningfulWorkflowState(
|
||||||
|
workflowState: AgentWorkflowPersistedState | null | undefined
|
||||||
|
) {
|
||||||
|
if (!workflowState) return false;
|
||||||
|
|
||||||
|
const inputs = workflowState.inputs;
|
||||||
|
const hasInputs = Boolean(
|
||||||
|
String(inputs?.primarySku || "").trim() ||
|
||||||
|
String(inputs?.secondarySku || "").trim() ||
|
||||||
|
String(inputs?.brand || "").trim() ||
|
||||||
|
String(inputs?.store || "").trim() ||
|
||||||
|
String(inputs?.priceBand || "").trim()
|
||||||
|
);
|
||||||
|
|
||||||
|
const hasSelections = Object.values(workflowState.selections || {}).some((value) =>
|
||||||
|
Boolean(String(value || "").trim())
|
||||||
|
);
|
||||||
|
const hasBrandModel = Boolean(String(workflowState.selectedBrandModel?.imageUrl || "").trim());
|
||||||
|
const hasPreset = Boolean(String(workflowState.selectedAgentPresetId || "").trim());
|
||||||
|
|
||||||
|
return hasInputs || hasSelections || hasBrandModel || hasPreset;
|
||||||
|
}
|
||||||
|
|
||||||
const FLOATING_PANEL_WIDTH = 460;
|
const FLOATING_PANEL_WIDTH = 460;
|
||||||
const FLOATING_PANEL_INSET = 16;
|
const FLOATING_PANEL_INSET = 16;
|
||||||
const DOCKED_PANEL_INSET = 0;
|
const DOCKED_PANEL_INSET = 0;
|
||||||
@ -306,6 +329,7 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
const previousNodeIdsRef = useRef<Set<string>>(new Set());
|
const previousNodeIdsRef = useRef<Set<string>>(new Set());
|
||||||
const previousBrandForModelRef = useRef("");
|
const previousBrandForModelRef = useRef("");
|
||||||
const autoSelectedBrandModelBrandRef = useRef("");
|
const autoSelectedBrandModelBrandRef = useRef("");
|
||||||
|
const lastLoadedCanvasIdRef = useRef<string>("");
|
||||||
const requestedAgentMode = mode === "agent";
|
const requestedAgentMode = mode === "agent";
|
||||||
const isAgentWorkspaceEnabled = siteSettingsData?.agentEnabled !== false;
|
const isAgentWorkspaceEnabled = siteSettingsData?.agentEnabled !== false;
|
||||||
const isCanvasAgentModeEnabled = siteSettingsData?.canvasAgentModeEnabled !== false;
|
const isCanvasAgentModeEnabled = siteSettingsData?.canvasAgentModeEnabled !== false;
|
||||||
@ -364,24 +388,58 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
setPanelMode("chat");
|
setPanelMode("chat");
|
||||||
}, [agentProjectId, isAgentMode, updateCanvasSearchParams]);
|
}, [agentProjectId, isAgentMode, updateCanvasSearchParams]);
|
||||||
|
|
||||||
|
const buildCurrentWorkflowPersistedState = useCallback((): AgentWorkflowPersistedState => {
|
||||||
|
return {
|
||||||
|
inputs: {
|
||||||
|
primarySku: workflowInputs.primarySku.trim(),
|
||||||
|
secondarySku: workflowInputs.secondarySku.trim(),
|
||||||
|
brand: workflowInputs.brand.trim(),
|
||||||
|
store: workflowInputs.store.trim(),
|
||||||
|
priceBand: workflowInputs.priceBand.trim(),
|
||||||
|
},
|
||||||
|
materials: workflowMaterials,
|
||||||
|
selections: workflowSelections,
|
||||||
|
selectedBrandModel,
|
||||||
|
selectedAgentPresetId: selectedAgentPresetId || null,
|
||||||
|
currentStage: currentAgentStage,
|
||||||
|
agentModeState,
|
||||||
|
};
|
||||||
|
}, [
|
||||||
|
agentModeState,
|
||||||
|
currentAgentStage,
|
||||||
|
selectedAgentPresetId,
|
||||||
|
selectedBrandModel,
|
||||||
|
workflowInputs,
|
||||||
|
workflowMaterials,
|
||||||
|
workflowSelections,
|
||||||
|
]);
|
||||||
|
|
||||||
// 加载画布数据
|
// 加载画布数据
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
console.log('[Canvas] Loading project:', id);
|
console.log('[Canvas] Loading project:', id);
|
||||||
|
|
||||||
const loadProject = async () => {
|
const loadProject = async () => {
|
||||||
|
const preservedWorkflowState = buildCurrentWorkflowPersistedState();
|
||||||
|
const shouldPreserveCurrentWorkflowState =
|
||||||
|
requestedAgentMode &&
|
||||||
|
lastLoadedCanvasIdRef.current === id &&
|
||||||
|
hasMeaningfulWorkflowState(preservedWorkflowState);
|
||||||
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setWorkflowHydrationSource("pending");
|
setWorkflowHydrationSource("pending");
|
||||||
setWorkflowInputs({
|
if (!shouldPreserveCurrentWorkflowState) {
|
||||||
primarySku: "",
|
setWorkflowInputs({
|
||||||
secondarySku: "",
|
primarySku: "",
|
||||||
brand: "",
|
secondarySku: "",
|
||||||
store: "",
|
brand: "",
|
||||||
priceBand: "",
|
store: "",
|
||||||
});
|
priceBand: "",
|
||||||
setWorkflowMaterials(createEmptyAgentWorkflowMaterialSelections());
|
});
|
||||||
setWorkflowSelections({});
|
setWorkflowMaterials(createEmptyAgentWorkflowMaterialSelections());
|
||||||
setSelectedBrandModel(null);
|
setWorkflowSelections({});
|
||||||
setSelectedAgentPresetId(null);
|
setSelectedBrandModel(null);
|
||||||
|
setSelectedAgentPresetId(null);
|
||||||
|
}
|
||||||
setAgentModeState(createEmptyAgentModeWorkspaceState());
|
setAgentModeState(createEmptyAgentModeWorkspaceState());
|
||||||
setAgentWorkspaceState(createEmptyAgentGenerationProjectWorkspaceState());
|
setAgentWorkspaceState(createEmptyAgentGenerationProjectWorkspaceState());
|
||||||
setAgentProjectTitle("未命名生图项目");
|
setAgentProjectTitle("未命名生图项目");
|
||||||
@ -447,6 +505,8 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
normalizeAgentModeWorkspaceState(persistedWorkflow.agentModeState)
|
normalizeAgentModeWorkspaceState(persistedWorkflow.agentModeState)
|
||||||
);
|
);
|
||||||
setWorkflowHydrationSource("project");
|
setWorkflowHydrationSource("project");
|
||||||
|
} else if (requestedAgentMode && shouldPreserveCurrentWorkflowState) {
|
||||||
|
setWorkflowHydrationSource("project");
|
||||||
} else {
|
} else {
|
||||||
setWorkflowHydrationSource("empty");
|
setWorkflowHydrationSource("empty");
|
||||||
}
|
}
|
||||||
@ -468,10 +528,11 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
lastLoadedCanvasIdRef.current = id;
|
||||||
};
|
};
|
||||||
|
|
||||||
loadProject();
|
loadProject();
|
||||||
}, [id, requestedAgentMode]);
|
}, [buildCurrentWorkflowPersistedState, id, requestedAgentMode]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const updateViewportWidth = () => setViewportWidth(window.innerWidth);
|
const updateViewportWidth = () => setViewportWidth(window.innerWidth);
|
||||||
@ -663,7 +724,7 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
try {
|
try {
|
||||||
const result = await agentProjectService.createProject({
|
const result = await agentProjectService.createProject({
|
||||||
title: buildDefaultAgentProjectTitle(),
|
title: buildDefaultAgentProjectTitle(),
|
||||||
projectState: createEmptyAgentGenerationProjectPersistedState(),
|
projectState: buildPersistedAgentProjectState(),
|
||||||
});
|
});
|
||||||
if (!result.success || !result.data) {
|
if (!result.success || !result.data) {
|
||||||
throw new Error(result.error || "创建生图项目失败");
|
throw new Error(result.error || "创建生图项目失败");
|
||||||
@ -688,7 +749,13 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
return () => {
|
return () => {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
};
|
};
|
||||||
}, [agentProjectId, isAgentWorkspaceEnabled, requestedAgentMode, updateCanvasSearchParams]);
|
}, [
|
||||||
|
agentProjectId,
|
||||||
|
buildPersistedAgentProjectState,
|
||||||
|
isAgentWorkspaceEnabled,
|
||||||
|
requestedAgentMode,
|
||||||
|
updateCanvasSearchParams,
|
||||||
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!requestedAgentMode || !agentProjectId) return;
|
if (!requestedAgentMode || !agentProjectId) return;
|
||||||
@ -706,7 +773,12 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
const persistedState = normalizeAgentGenerationProjectPersistedState(
|
const persistedState = normalizeAgentGenerationProjectPersistedState(
|
||||||
result.data.projectState
|
result.data.projectState
|
||||||
);
|
);
|
||||||
const persistedWorkflow = persistedState.workflowState;
|
const currentWorkflowState = buildCurrentWorkflowPersistedState();
|
||||||
|
const persistedWorkflow = hasMeaningfulWorkflowState(persistedState.workflowState)
|
||||||
|
? persistedState.workflowState
|
||||||
|
: hasMeaningfulWorkflowState(currentWorkflowState)
|
||||||
|
? currentWorkflowState
|
||||||
|
: persistedState.workflowState;
|
||||||
|
|
||||||
setWorkflowInputs({
|
setWorkflowInputs({
|
||||||
primarySku: String(persistedWorkflow.inputs?.primarySku || "").trim(),
|
primarySku: String(persistedWorkflow.inputs?.primarySku || "").trim(),
|
||||||
@ -742,7 +814,7 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
return () => {
|
return () => {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
};
|
};
|
||||||
}, [agentProjectId, requestedAgentMode]);
|
}, [agentProjectId, buildCurrentWorkflowPersistedState, requestedAgentMode]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isAgentMode || !agentProjectId || isAgentProjectLoading) return;
|
if (!isAgentMode || !agentProjectId || isAgentProjectLoading) return;
|
||||||
@ -843,7 +915,7 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
|
|
||||||
const result = await agentProjectService.createProject({
|
const result = await agentProjectService.createProject({
|
||||||
title: buildDefaultAgentProjectTitle(),
|
title: buildDefaultAgentProjectTitle(),
|
||||||
projectState: createEmptyAgentGenerationProjectPersistedState(),
|
projectState: buildPersistedAgentProjectState(),
|
||||||
});
|
});
|
||||||
if (!result.success || !result.data) {
|
if (!result.success || !result.data) {
|
||||||
toast.error(result.error || "创建生图项目失败");
|
toast.error(result.error || "创建生图项目失败");
|
||||||
@ -854,7 +926,12 @@ export default function CanvasEditorPage({ params }: PageProps) {
|
|||||||
mode: "agent",
|
mode: "agent",
|
||||||
agentProjectId: result.data.id,
|
agentProjectId: result.data.id,
|
||||||
});
|
});
|
||||||
}, [agentProjectId, persistAgentProject, updateCanvasSearchParams]);
|
}, [
|
||||||
|
agentProjectId,
|
||||||
|
buildPersistedAgentProjectState,
|
||||||
|
persistAgentProject,
|
||||||
|
updateCanvasSearchParams,
|
||||||
|
]);
|
||||||
|
|
||||||
const handleSelectAgentProject = useCallback(
|
const handleSelectAgentProject = useCallback(
|
||||||
async (nextProjectId: string) => {
|
async (nextProjectId: string) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user