Skip to content

Commit

Permalink
fix old snapshot format
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk committed Nov 15, 2024
1 parent 169aa39 commit e40dfa5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export type WorkflowSnapshotsType = {
title: string;
isSaved?: boolean;
state?: WorkflowStateType;

// old format
nodes?: Node[];
edges?: Edge[];
chatConfig?: AppChatConfigType;
};

export type WorkflowStateType = {
Expand Down Expand Up @@ -902,6 +907,38 @@ const WorkflowContextProvider = ({
});
}, [appId]);

const convertOldFormatHistory = (past: WorkflowSnapshotsType[]) => {
const baseState = {
nodes: past[past.length - 1].state?.nodes || [],
edges: past[past.length - 1].state?.edges || [],
chatConfig: past[past.length - 1].state?.chatConfig || {}
};

return past.map((item, index) => {
if (index === past.length - 1) {
return {
title: item.title,
isSaved: item.isSaved,
state: baseState
};
}

const currentState = {
nodes: item.nodes || [],
edges: item.edges || [],
chatConfig: item.chatConfig || {}
};

const diff = diffPatcher.diff(baseState, currentState);

return {
title: item.title || formatTime2YMDHMS(new Date()),
isSaved: item.isSaved,
diff
};
});
};

const initData = useCallback(
async (
e: {
Expand All @@ -920,20 +957,42 @@ const WorkflowContextProvider = ({
chatConfig: e.chatConfig || appDetail.chatConfig
};

// If initializing and has past snapshots, restore from latest snapshot
if (isInit && past.length > 0 && past[0].diff) {
const targetState = diffPatcher.patch(
structuredClone(past[past.length - 1].state),
past[0].diff
) as WorkflowStateType;

setNodes(targetState.nodes);
setEdges(targetState.edges);
setAppDetail((state) => ({
...state,
chatConfig: targetState.chatConfig
}));
return;
if (isInit && past.length > 0) {
// new format
if (past[0].diff) {
const targetState = diffPatcher.patch(
structuredClone(past[past.length - 1].state),
past[0].diff
) as WorkflowStateType;

setNodes(targetState.nodes);
setEdges(targetState.edges);
setAppDetail((state) => ({
...state,
chatConfig: targetState.chatConfig
}));
return;
}

// old format
if (past.some((item) => !item.state && (item.nodes || item.edges))) {
const newPast = convertOldFormatHistory(past);

setPast(newPast);

const latestState = diffPatcher.patch(
structuredClone(newPast[newPast.length - 1].state),
newPast[0].diff
) as WorkflowStateType;

setNodes(latestState.nodes);
setEdges(latestState.edges);
setAppDetail((state) => ({
...state,
chatConfig: latestState.chatConfig
}));
return;
}
}

setNodes(nodes);
Expand Down
23 changes: 8 additions & 15 deletions projects/app/src/web/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,20 +632,13 @@ export const compareSnapshot = (
return isEqual(node1, node2);
};

// Simplify nodes, remove system config node & node size
// remove node size
export const simplifyNodes = (nodes: Node[]) => {
return nodes
.filter((node) => {
if (!node) return;
if (FlowNodeTypeEnum.systemConfig === node.type) return;

return true;
})
.map((node) => ({
id: node.id,
type: node.type,
position: node.position,
data: node.data,
zIndex: node.zIndex
}));
return nodes.map((node) => ({
id: node.id,
type: node.type,
position: node.position,
data: node.data,
zIndex: node.zIndex
}));
};

0 comments on commit e40dfa5

Please sign in to comment.