Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Replace ThunkAction with CompositeAction and Enhance State Management in GraphProvider #49

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { GiselleNode } from "../giselle-node/types";
import type { ThunkAction } from "../graph/context";
import type { CompositeAction } from "../graph/context";
import { playgroundModes } from "../graph/types";
import { updateMode } from "../graph/v2/mode";
import { setFlow } from "./action";
import { runAction } from "./server-action";
import { flowStatuses } from "./types";
import { createFlowActionId, createFlowId, resolveActionLayers } from "./utils";

export function runFlow(finalNode: GiselleNode): ThunkAction {
export function runFlow(finalNode: GiselleNode): CompositeAction {
return async (dispatch, getState) => {
const state = getState();
dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
updateNodeProperty,
updateNodesUI,
} from "../../../graph/actions";
import { type ThunkAction, useGraph } from "../../../graph/context";
import { type CompositeAction, useGraph } from "../../../graph/context";
import type {
TextContent,
TextContentReference,
Expand All @@ -44,7 +44,7 @@ import { Block } from "./block";
function setTextToPropertyAndOutput(
nodeId: GiselleNodeId,
text: string,
): ThunkAction {
): CompositeAction {
return (dispatch) => {
dispatch(
updateNodeProperty({
Expand Down
14 changes: 7 additions & 7 deletions app/(playground)/p/[agentId]/beta-proto/graph/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
webSearchItemStatus,
webSearchStatus,
} from "../web-search/types";
import type { ThunkAction } from "./context";
import type { CompositeAction } from "./context";
import {
generateArtifactStream,
parseFile,
Expand Down Expand Up @@ -171,7 +171,7 @@ type AddNodesAndConnectArgs = {
};
export const addNodesAndConnect = (
args: AddNodesAndConnectArgs,
): ThunkAction => {
): CompositeAction => {
return (dispatch, getState) => {
const state = getState();
const hasFinalNode = state.graph.nodes.some((node) => node.isFinal);
Expand Down Expand Up @@ -268,7 +268,7 @@ export const selectNodeAndSetPanelTab = (args: {
id: GiselleNodeId;
panelTab: PanelTab;
};
}): ThunkAction => {
}): CompositeAction => {
return (dispatch) => {
dispatch(
selectNode({
Expand Down Expand Up @@ -469,7 +469,7 @@ export const removeArtifact = (
};

export const generateText =
(args: GenerateTextArgs): ThunkAction =>
(args: GenerateTextArgs): CompositeAction =>
async (dispatch, getState) => {
dispatch(
setNodeOutput({
Expand Down Expand Up @@ -815,7 +815,7 @@ type AddSourceToPromptNodeArgs = {
};
export function addSourceToPromptNode(
args: AddSourceToPromptNodeArgs,
): ThunkAction {
): CompositeAction {
return async (dispatch, getState) => {
const state = getState();
const targetPromptNode = state.graph.nodes.find(
Expand Down Expand Up @@ -1053,7 +1053,7 @@ type RemoveSourceFromPromptNodeArgs = {
};
export function removeSourceFromPromptNode(
args: RemoveSourceFromPromptNodeArgs,
): ThunkAction {
): CompositeAction {
return (dispatch, getState) => {
const state = getState();
const targetNode = state.graph.nodes.find(
Expand Down Expand Up @@ -1233,7 +1233,7 @@ export function removeNode(args: RemoveNodeArgs): RemoveNodeAction {
};
}

export function removeSelectedNodesOrFeedback(): ThunkAction {
export function removeSelectedNodesOrFeedback(): CompositeAction {
return (dispatch, getState) => {
const state = getState();
const selectedNodes = state.graph.nodes.filter((node) => node.ui.selected);
Expand Down
4 changes: 2 additions & 2 deletions app/(playground)/p/[agentId]/beta-proto/graph/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { createContext, useContext } from "react";
import type { GraphAction } from "./actions";
import type { GraphState } from "./types";

export type ThunkAction = (
export type CompositeAction = (
dispatch: EnhancedDispatch,
getState: () => GraphState,
) => void | Promise<void>;
export type EnhancedDispatch = (action: GraphAction | ThunkAction) => void;
export type EnhancedDispatch = (action: GraphAction | CompositeAction) => void;
export type GraphContext = {
state: GraphState;
dispatch: EnhancedDispatch;
Expand Down
32 changes: 18 additions & 14 deletions app/(playground)/p/[agentId]/beta-proto/graph/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,37 @@ export const GraphProvider: FC<PropsWithChildren<GraphProviderProps>> = ({
agentId,
defaultGraph,
}) => {
const [state, originalDispatch] = useReducer(graphReducer, {
const [originalState, originalDispatch] = useReducer(graphReducer, {
graph: defaultGraph,
});
const isInitialMount = useRef(true);
const stateRef = useRef(originalState);

useEffect(() => {
stateRef.current = originalState;
}, [originalState]);

const deboucedSetGraphToDb = useDebounce(async (graph: Graph) => {
setGraphToDb(agentId, graph);
}, 500);
const enhancedDispatch: EnhancedDispatch = useCallback(
async (action) => {
if (typeof action === "function") {
await action(enhancedDispatch, () => state);
} else {
originalDispatch(action);
}
},
[state],
);
const enhancedDispatch: EnhancedDispatch = useCallback(async (action) => {
if (typeof action === "function") {
await action(enhancedDispatch, () => stateRef.current);
} else {
originalDispatch(action);
}
}, []);
useEffect(() => {
if (isInitialMount.current) {
isInitialMount.current = false;
} else {
deboucedSetGraphToDb(state.graph);
deboucedSetGraphToDb(originalState.graph);
}
}, [state, deboucedSetGraphToDb]);
}, [originalState, deboucedSetGraphToDb]);
return (
<GraphContext.Provider value={{ state, dispatch: enhancedDispatch }}>
<GraphContext.Provider
value={{ state: originalState, dispatch: enhancedDispatch }}
>
{children}
</GraphContext.Provider>
);
Expand Down