Skip to content

Commit

Permalink
refactor: snapshot store to diff (#3155)
Browse files Browse the repository at this point in the history
* refactor: snapshot store to diff

* change initial state position

* fix old snapshot format

* encapsulate json diff
  • Loading branch information
newfish-cmyk authored Nov 21, 2024
1 parent 4f55025 commit 9b2c3b2
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 99 deletions.
121 changes: 109 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions projects/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"jest": "^29.5.0",
"js-yaml": "^4.1.0",
"json5": "^2.2.3",
"jsondiffpatch": "^0.6.0",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"mermaid": "^10.2.3",
Expand Down
16 changes: 12 additions & 4 deletions projects/app/src/pages/app/detail/components/Plugin/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { useTranslation } from 'next-i18next';

import MyIcon from '@fastgpt/web/components/common/Icon';
import { useContextSelector } from 'use-context-selector';
import { WorkflowContext, WorkflowSnapshotsType } from '../WorkflowComponents/context';
import {
WorkflowContext,
WorkflowSnapshotsType,
WorkflowStateType
} from '../WorkflowComponents/context';
import { AppContext, TabEnum } from '../context';
import RouteTab from '../RouteTab';
import { useRouter } from 'next/router';
Expand All @@ -34,6 +38,7 @@ import {
WorkflowInitContext
} from '../WorkflowComponents/context/workflowInitContext';
import { WorkflowEventContext } from '../WorkflowComponents/context/workflowEventContext';
import { applyDiff } from '@/web/core/app/diff';

const Header = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -76,11 +81,14 @@ const Header = () => {
[...future].reverse().find((snapshot) => snapshot.isSaved) ||
past.find((snapshot) => snapshot.isSaved);

const initialState = past[past.length - 1]?.state;
const savedSnapshotState = applyDiff(initialState, savedSnapshot?.diff);

const val = compareSnapshot(
{
nodes: savedSnapshot?.nodes,
edges: savedSnapshot?.edges,
chatConfig: savedSnapshot?.chatConfig
nodes: savedSnapshotState?.nodes,
edges: savedSnapshotState?.edges,
chatConfig: savedSnapshotState?.chatConfig
},
{
nodes: nodes,
Expand Down
16 changes: 9 additions & 7 deletions projects/app/src/pages/app/detail/components/SimpleApp/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import styles from './styles.module.scss';
import { useSystem } from '@fastgpt/web/hooks/useSystem';
import { useTranslation } from 'next-i18next';
import { onSaveSnapshotFnType, SimpleAppSnapshotType } from './useSnapshots';
import { applyDiff } from '@/web/core/app/diff';

const Edit = ({
appForm,
Expand All @@ -39,16 +40,19 @@ const Edit = ({
// show selected dataset
loadAllDatasets();

// Get the latest snapshot
if (past?.[0]?.appForm) {
return setAppForm(past[0].appForm);
}

const appForm = appWorkflow2Form({
nodes: appDetail.modules,
chatConfig: appDetail.chatConfig
});

// Get the latest snapshot
if (past?.[0]?.diff) {
const pastState = applyDiff(past[past.length - 1].state, past[0].diff);

return setAppForm(pastState);
}

setAppForm(appForm);
// Set the first snapshot
if (past.length === 0) {
saveSnapshot({
Expand All @@ -58,8 +62,6 @@ const Edit = ({
});
}

setAppForm(appForm);

if (appDetail.version !== 'v2') {
setAppForm(
appWorkflow2Form({
Expand Down
Loading

0 comments on commit 9b2c3b2

Please sign in to comment.