refactor(workflow): streamline workflow settings management

- Introduced new utility functions `settingsFromWorkflowGraph` and `workflowGraphWithSettings` to encapsulate settings extraction and graph updates.
- Updated `useWorkflowEditorState` to utilize the new utility functions for improved clarity and maintainability.
- Refactored `AssistantPage` and `WorkflowCanvas` components to leverage the new settings management functions, ensuring consistent handling of workflow settings across the application.
- Removed redundant settings handling code, enhancing overall code cleanliness.
This commit is contained in:
Xin Wang
2026-07-29 15:09:00 +08:00
parent 774825593d
commit 5ef376b657
4 changed files with 81 additions and 82 deletions

View File

@@ -56,6 +56,7 @@ import {
type WorkflowNodeType,
} from "./specs";
import type { WorkflowEditorProps } from "./types";
import { workflowGraphWithSettings } from "./graph-settings";
let nodeSeq = 0;
@@ -160,47 +161,28 @@ export function WorkflowCanvas({
const [addPosition, setAddPosition] = useState<{ x: number; y: number } | null>(null);
const { screenToFlowPosition } = useReactFlow();
// 回传画布状态给外部(助手 graph)。用 ref 避免把 onChange 放进依赖导致循环。
// 只在可持久化的画布内容真正变化时回传。React Flow 挂载时会写入节点尺寸等
// 内部状态,这些不属于 workflow graph不能因此把刚加载的助手标记为未保存。
const onChangeRef = useRef(onChange);
const lastSyncedGraphRef = useRef(
JSON.stringify(
workflowGraphWithSettings(
fromFlow(initial.nodes, initial.edges),
settings,
),
),
);
useEffect(() => {
onChangeRef.current = onChange;
}, [onChange]);
useEffect(() => {
const graph = fromFlow(nodes, edges);
graph.settings = {
globalPrompt: settings.globalPrompt,
defaultLlmResourceId: settings.llm ?? "",
defaultAsrResourceId: settings.asr ?? "",
defaultTtsResourceId: settings.tts ?? "",
visionEnabled: settings.visionEnabled,
visionModelResourceId: settings.visionModelResourceId,
toolIds: settings.toolIds,
knowledgeBaseId: settings.knowledgeBaseId,
knowledgeMode: settings.knowledgeRetrievalConfig.mode,
knowledgeTopN: settings.knowledgeRetrievalConfig.topN,
knowledgeScoreThreshold:
settings.knowledgeRetrievalConfig.scoreThreshold,
enableInterrupt: settings.allowInterrupt,
turnConfig: settings.turnConfig,
};
const graph = workflowGraphWithSettings(fromFlow(nodes, edges), settings);
const nextGraphJson = JSON.stringify(graph);
if (nextGraphJson === lastSyncedGraphRef.current) return;
lastSyncedGraphRef.current = nextGraphJson;
onChangeRef.current?.(graph);
}, [
nodes,
edges,
settings.globalPrompt,
settings.llm,
settings.asr,
settings.tts,
settings.visionEnabled,
settings.visionModelResourceId,
settings.toolIds,
settings.knowledgeBaseId,
settings.knowledgeRetrievalConfig.mode,
settings.knowledgeRetrievalConfig.topN,
settings.knowledgeRetrievalConfig.scoreThreshold,
settings.allowInterrupt,
settings.turnConfig,
]);
}, [nodes, edges, settings]);
const onConnect = useCallback(
(connection: Connection) => {