@foresthubai/workflow-builder 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/LICENSE +661 -661
  2. package/NOTICE +16 -16
  3. package/README.md +110 -110
  4. package/dist/components/ui/command.d.ts +2 -2
  5. package/dist/components/ui/input.d.ts +1 -1
  6. package/dist/components/ui/resizable.d.ts +1 -1
  7. package/dist/components/ui/textarea.d.ts +1 -1
  8. package/dist/graph/BaseNode.js +10 -10
  9. package/dist/graph/reactFlowRegistry.d.ts.map +1 -1
  10. package/package.json +2 -2
  11. package/src/BuilderLayout.tsx +345 -345
  12. package/src/Canvas.tsx +261 -261
  13. package/src/CanvasEditor.tsx +142 -142
  14. package/src/CanvasTabsToolbar.tsx +176 -176
  15. package/src/RightConfigPanel.tsx +266 -266
  16. package/src/WorkflowBuilder.tsx +408 -408
  17. package/src/cn.ts +6 -6
  18. package/src/components/ui/add-button.tsx +39 -39
  19. package/src/components/ui/alert-dialog.tsx +141 -141
  20. package/src/components/ui/alert.tsx +59 -59
  21. package/src/components/ui/badge.tsx +36 -36
  22. package/src/components/ui/button.tsx +85 -85
  23. package/src/components/ui/card.tsx +79 -79
  24. package/src/components/ui/checkbox.tsx +28 -28
  25. package/src/components/ui/collapsible.tsx +9 -9
  26. package/src/components/ui/command.tsx +153 -153
  27. package/src/components/ui/delete-button.tsx +23 -23
  28. package/src/components/ui/dialog.tsx +125 -125
  29. package/src/components/ui/dropdown-menu.tsx +198 -198
  30. package/src/components/ui/input.tsx +55 -55
  31. package/src/components/ui/label.tsx +24 -24
  32. package/src/components/ui/readonly-banner.tsx +15 -15
  33. package/src/components/ui/resizable.tsx +43 -43
  34. package/src/components/ui/scroll-area.tsx +102 -102
  35. package/src/components/ui/select.tsx +160 -160
  36. package/src/components/ui/separator.tsx +29 -29
  37. package/src/components/ui/switch.tsx +27 -27
  38. package/src/components/ui/textarea.tsx +51 -51
  39. package/src/components/ui/toast.tsx +127 -127
  40. package/src/components/ui/toaster.tsx +33 -33
  41. package/src/components/ui/toggle-group.tsx +59 -59
  42. package/src/components/ui/toggle.tsx +43 -43
  43. package/src/components/ui/tooltip.tsx +32 -32
  44. package/src/dialogs/NodePickerDialog.tsx +84 -84
  45. package/src/dialogs/ValidationDialog.tsx +184 -184
  46. package/src/graph/BaseNode.tsx +557 -557
  47. package/src/graph/CustomEdge.tsx +185 -185
  48. package/src/graph/CustomNode.tsx +16 -16
  49. package/src/graph/FunctionCallNode.tsx +30 -30
  50. package/src/graph/PortHandle.tsx +189 -189
  51. package/src/graph/reactFlowRegistry.ts +26 -26
  52. package/src/hooks/use-toast.ts +125 -125
  53. package/src/hooks/useAvailableVariables.ts +20 -20
  54. package/src/hooks/useCanvasHistory.ts +22 -22
  55. package/src/hooks/useCanvasTabs.ts +168 -168
  56. package/src/hooks/useFunctionDiagnosticsSync.ts +40 -40
  57. package/src/hooks/useFunctionRegistry.ts +26 -26
  58. package/src/hooks/useFunctions.ts +44 -44
  59. package/src/hooks/useGraph.ts +161 -161
  60. package/src/hooks/useNodeDefinitions.ts +82 -82
  61. package/src/hooks/useParamErrors.ts +26 -26
  62. package/src/hooks/useResolvedTheme.ts +30 -30
  63. package/src/hooks/useResourceDiagnosticsSync.ts +58 -58
  64. package/src/hooks/useSuppressThemeTransition.ts +79 -79
  65. package/src/hooks/useWorkflowSerialization.ts +141 -141
  66. package/src/i18n/index.ts +53 -53
  67. package/src/i18n/locales/de.json +501 -501
  68. package/src/i18n/locales/en.json +557 -557
  69. package/src/index.ts +27 -27
  70. package/src/inputs/ExpressionInput.tsx +297 -297
  71. package/src/inputs/ParameterEditor.tsx +515 -515
  72. package/src/inputs/PortSection.tsx +144 -144
  73. package/src/panels/BuilderSidebar.tsx +301 -301
  74. package/src/panels/ChannelConfigPanel.tsx +49 -49
  75. package/src/panels/ChannelsPanel.tsx +28 -28
  76. package/src/panels/DebugConsolePanel.tsx +73 -73
  77. package/src/panels/DebugContextPanel.tsx +77 -77
  78. package/src/panels/DebugExternalIOPanel.tsx +180 -180
  79. package/src/panels/DiagnosticsPanel.tsx +170 -170
  80. package/src/panels/EdgeConfigPanel.tsx +104 -104
  81. package/src/panels/FunctionConfigPanel.tsx +179 -179
  82. package/src/panels/FunctionListPanel.tsx +45 -45
  83. package/src/panels/MemoryConfigPanel.tsx +55 -55
  84. package/src/panels/MemoryPanel.tsx +40 -40
  85. package/src/panels/ModelConfigPanel.tsx +41 -41
  86. package/src/panels/ModelsPanel.tsx +36 -36
  87. package/src/panels/NodeConfigPanel.tsx +630 -630
  88. package/src/panels/NodeLibrary.tsx +288 -288
  89. package/src/panels/ResourceConfigPanel.tsx +132 -132
  90. package/src/panels/ResourceListPanel.tsx +113 -113
  91. package/src/panels/VariableConfigPanel.tsx +161 -161
  92. package/src/panels/VariablesPanel.tsx +145 -145
  93. package/src/stores/canvasStore.test.ts +44 -44
  94. package/src/stores/canvasStore.ts +245 -245
  95. package/src/stores/debugStore.ts +74 -74
  96. package/src/stores/diagnosticsStore.ts +130 -130
  97. package/src/stores/editorStore.ts +190 -190
  98. package/src/styles/index.css +526 -526
  99. package/src/utils/categoryConstants.ts +26 -26
  100. package/src/utils/channelOperations.ts +86 -86
  101. package/src/utils/connectionRules.ts +137 -137
  102. package/src/utils/functionOperations.ts +179 -179
  103. package/src/utils/graphOperations.ts +550 -550
  104. package/src/utils/history.ts +207 -207
  105. package/src/utils/memoryOperations.ts +57 -57
  106. package/src/utils/migrateFunctionNodes.ts +107 -107
  107. package/src/utils/modelOperations.ts +55 -55
  108. package/src/utils/paramDisplay.ts +71 -71
  109. package/src/utils/resourceHelpers.ts +32 -32
  110. package/src/utils/translation.ts +28 -28
  111. package/src/utils/variableOperations.ts +75 -75
  112. package/tailwind-preset.ts +166 -166
  113. package/dist/lib/utils.d.ts +0 -3
  114. package/dist/lib/utils.d.ts.map +0 -1
  115. package/dist/lib/utils.js +0 -6
  116. package/dist/lib/utils.js.map +0 -1
  117. package/dist/toolbars/CanvasTabsToolbar.d.ts +0 -11
  118. package/dist/toolbars/CanvasTabsToolbar.d.ts.map +0 -1
  119. package/dist/toolbars/CanvasTabsToolbar.js +0 -101
  120. package/dist/toolbars/CanvasTabsToolbar.js.map +0 -1
@@ -1,207 +1,207 @@
1
- import { StateCreator, StoreMutatorIdentifier } from "zustand";
2
-
3
- // Opaque type for exporting/importing history state
4
- export interface HistoryData {
5
- past: HistoryFrame<unknown>[];
6
- future: HistoryFrame<unknown>[];
7
- }
8
-
9
- // Public interface - only actions
10
- export interface History {
11
- /**
12
- * Manually saves the current state as an undo checkpoint.
13
- * Must be called BEFORE a state change that should be undoable.
14
- */
15
- takeCheckpoint: () => void;
16
-
17
- /**
18
- * Run the given operation and save a checkpoint if the state did change.
19
- * Good to wrap multiple state changes in a single undoable action.
20
- * If the operation throws, no checkpoint is saved and the error is propagated.
21
- * @param operation Function that may perform a state change.
22
- * @returns The result of the operation.
23
- */
24
- withCheckpoint: <R>(operation: () => R) => R;
25
-
26
- /**
27
- * Reverts the state to the previous snapshot in the undo history, if available.
28
- * Moves the current state to the redo history.
29
- */
30
- undo: () => void;
31
-
32
- /**
33
- * Reapplies the most recently undone state from the redo history, if available.
34
- * Moves the current state to the undo history.
35
- */
36
- redo: () => void;
37
-
38
- /**
39
- * Clears both the undo and redo history, but does not affect the current state.
40
- */
41
- clearHistory: () => void;
42
-
43
- /**
44
- * Returns true if there is at least one state in the undo history.
45
- */
46
- canUndo: () => boolean;
47
-
48
- /**
49
- * Returns true if there is at least one state in the redo history.
50
- */
51
- canRedo: () => boolean;
52
-
53
- /**
54
- * Exports the current undo/redo history as an opaque blob.
55
- */
56
- exportHistory: () => HistoryData;
57
-
58
- /**
59
- * Restores a previously exported history blob.
60
- */
61
- importHistory: (data: HistoryData) => void;
62
- }
63
-
64
- // A single frame in history
65
- interface HistoryFrame<T> {
66
- state: T;
67
- timestamp: number;
68
- }
69
-
70
- // Internal state - not exposed publicly
71
- interface HistoryState<T> {
72
- _history_past: HistoryFrame<T>[];
73
- _history_future: HistoryFrame<T>[];
74
- }
75
-
76
- /**
77
- * Public, opaque change signal: a monotonic counter the middleware bumps on every
78
- * history transition (checkpoint, undo, redo, clear, import). Subscribe to it to
79
- * detect domain mutations and undo/redo in O(1). It never ticks on selection or
80
- * drag — those mutate state without a checkpoint. Mirrors editorStore's counter
81
- * of the same name for project-scoped (channel/memory/model) mutations.
82
- */
83
- export interface MutationCount {
84
- readonly mutationCount: number;
85
- }
86
-
87
- // Configuration for history middleware
88
- interface HistoryConfig<T> {
89
- limit?: number;
90
- partialize?: (state: T) => Partial<T>;
91
- equality?: (before: Partial<T>, after: Partial<T>) => boolean;
92
- }
93
-
94
- export const history =
95
- <T>(config: HistoryConfig<T> = {}) =>
96
- <Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(
97
- storeInitializer: StateCreator<T, Mps, Mcs>,
98
- ): StateCreator<T & HistoryState<T> & History & MutationCount, Mps, Mcs> => {
99
- const {
100
- limit = 50,
101
- partialize = (state) => state,
102
- equality = (before, after) => JSON.stringify(before) !== JSON.stringify(after),
103
- } = config;
104
-
105
- return (set, get, store) => {
106
- type FullState = T & HistoryState<T> & History & MutationCount;
107
-
108
- const initialState = storeInitializer(
109
- set as Parameters<StateCreator<T, Mps, Mcs>>[0],
110
- get as Parameters<StateCreator<T, Mps, Mcs>>[1],
111
- store as Parameters<StateCreator<T, Mps, Mcs>>[2],
112
- );
113
-
114
- return {
115
- ...initialState,
116
- _history_past: [],
117
- _history_future: [],
118
- mutationCount: 0,
119
-
120
- takeCheckpoint: () => {
121
- const currentState = partialize(get() as T);
122
- set((state: FullState) => ({
123
- ...state,
124
- _history_past: [...state._history_past, { state: currentState as T, timestamp: Date.now() }].slice(-limit),
125
- _history_future: [],
126
- mutationCount: state.mutationCount + 1,
127
- }));
128
- },
129
-
130
- withCheckpoint: <R>(operation: () => R): R => {
131
- const beforeState = partialize(get() as T);
132
- // Execute operation and capture result to return later to caller
133
- const result = operation();
134
- const afterState = partialize(get() as T);
135
- // Compare using provided equality function
136
- const areEqual = equality(beforeState, afterState);
137
- if (!areEqual) {
138
- set((state: FullState) => ({
139
- ...state,
140
- _history_past: [...state._history_past, { state: beforeState as T, timestamp: Date.now() }].slice(-limit),
141
- _history_future: [],
142
- mutationCount: state.mutationCount + 1,
143
- }));
144
- }
145
- return result;
146
- },
147
-
148
- undo: () => {
149
- const state = get();
150
- if (state._history_past.length === 0) return;
151
- const past = [...state._history_past];
152
- const previousFrame = past.pop()!;
153
- // Save current state to future before restoring
154
- const currentState = partialize(state as T);
155
- set({
156
- ...state,
157
- ...previousFrame.state,
158
- _history_past: past,
159
- _history_future: [...state._history_future, { state: currentState as T, timestamp: Date.now() }],
160
- mutationCount: state.mutationCount + 1,
161
- } as FullState);
162
- },
163
-
164
- redo: () => {
165
- const state = get();
166
- if (state._history_future.length === 0) return;
167
- const future = [...state._history_future];
168
- const nextFrame = future.pop()!;
169
- // Save current state to past before restoring
170
- const currentState = partialize(state as T);
171
- set({
172
- ...state,
173
- ...nextFrame.state,
174
- _history_past: [...state._history_past, { state: currentState as T, timestamp: Date.now() }].slice(-limit),
175
- _history_future: future,
176
- mutationCount: state.mutationCount + 1,
177
- } as FullState);
178
- },
179
-
180
- clearHistory: () => {
181
- set((state: FullState) => ({
182
- ...state,
183
- _history_past: [],
184
- _history_future: [],
185
- mutationCount: state.mutationCount + 1,
186
- }));
187
- },
188
-
189
- canUndo: () => (get() as FullState)._history_past.length > 0,
190
- canRedo: () => (get() as FullState)._history_future.length > 0,
191
-
192
- exportHistory: () => ({
193
- past: [...(get() as FullState)._history_past],
194
- future: [...(get() as FullState)._history_future],
195
- }),
196
-
197
- importHistory: (data: HistoryData) => {
198
- set((state: FullState) => ({
199
- ...state,
200
- _history_past: [...data.past] as HistoryFrame<T>[],
201
- _history_future: [...data.future] as HistoryFrame<T>[],
202
- mutationCount: state.mutationCount + 1,
203
- }));
204
- },
205
- } as FullState;
206
- };
207
- };
1
+ import { StateCreator, StoreMutatorIdentifier } from "zustand";
2
+
3
+ // Opaque type for exporting/importing history state
4
+ export interface HistoryData {
5
+ past: HistoryFrame<unknown>[];
6
+ future: HistoryFrame<unknown>[];
7
+ }
8
+
9
+ // Public interface - only actions
10
+ export interface History {
11
+ /**
12
+ * Manually saves the current state as an undo checkpoint.
13
+ * Must be called BEFORE a state change that should be undoable.
14
+ */
15
+ takeCheckpoint: () => void;
16
+
17
+ /**
18
+ * Run the given operation and save a checkpoint if the state did change.
19
+ * Good to wrap multiple state changes in a single undoable action.
20
+ * If the operation throws, no checkpoint is saved and the error is propagated.
21
+ * @param operation Function that may perform a state change.
22
+ * @returns The result of the operation.
23
+ */
24
+ withCheckpoint: <R>(operation: () => R) => R;
25
+
26
+ /**
27
+ * Reverts the state to the previous snapshot in the undo history, if available.
28
+ * Moves the current state to the redo history.
29
+ */
30
+ undo: () => void;
31
+
32
+ /**
33
+ * Reapplies the most recently undone state from the redo history, if available.
34
+ * Moves the current state to the undo history.
35
+ */
36
+ redo: () => void;
37
+
38
+ /**
39
+ * Clears both the undo and redo history, but does not affect the current state.
40
+ */
41
+ clearHistory: () => void;
42
+
43
+ /**
44
+ * Returns true if there is at least one state in the undo history.
45
+ */
46
+ canUndo: () => boolean;
47
+
48
+ /**
49
+ * Returns true if there is at least one state in the redo history.
50
+ */
51
+ canRedo: () => boolean;
52
+
53
+ /**
54
+ * Exports the current undo/redo history as an opaque blob.
55
+ */
56
+ exportHistory: () => HistoryData;
57
+
58
+ /**
59
+ * Restores a previously exported history blob.
60
+ */
61
+ importHistory: (data: HistoryData) => void;
62
+ }
63
+
64
+ // A single frame in history
65
+ interface HistoryFrame<T> {
66
+ state: T;
67
+ timestamp: number;
68
+ }
69
+
70
+ // Internal state - not exposed publicly
71
+ interface HistoryState<T> {
72
+ _history_past: HistoryFrame<T>[];
73
+ _history_future: HistoryFrame<T>[];
74
+ }
75
+
76
+ /**
77
+ * Public, opaque change signal: a monotonic counter the middleware bumps on every
78
+ * history transition (checkpoint, undo, redo, clear, import). Subscribe to it to
79
+ * detect domain mutations and undo/redo in O(1). It never ticks on selection or
80
+ * drag — those mutate state without a checkpoint. Mirrors editorStore's counter
81
+ * of the same name for project-scoped (channel/memory/model) mutations.
82
+ */
83
+ export interface MutationCount {
84
+ readonly mutationCount: number;
85
+ }
86
+
87
+ // Configuration for history middleware
88
+ interface HistoryConfig<T> {
89
+ limit?: number;
90
+ partialize?: (state: T) => Partial<T>;
91
+ equality?: (before: Partial<T>, after: Partial<T>) => boolean;
92
+ }
93
+
94
+ export const history =
95
+ <T>(config: HistoryConfig<T> = {}) =>
96
+ <Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(
97
+ storeInitializer: StateCreator<T, Mps, Mcs>,
98
+ ): StateCreator<T & HistoryState<T> & History & MutationCount, Mps, Mcs> => {
99
+ const {
100
+ limit = 50,
101
+ partialize = (state) => state,
102
+ equality = (before, after) => JSON.stringify(before) !== JSON.stringify(after),
103
+ } = config;
104
+
105
+ return (set, get, store) => {
106
+ type FullState = T & HistoryState<T> & History & MutationCount;
107
+
108
+ const initialState = storeInitializer(
109
+ set as Parameters<StateCreator<T, Mps, Mcs>>[0],
110
+ get as Parameters<StateCreator<T, Mps, Mcs>>[1],
111
+ store as Parameters<StateCreator<T, Mps, Mcs>>[2],
112
+ );
113
+
114
+ return {
115
+ ...initialState,
116
+ _history_past: [],
117
+ _history_future: [],
118
+ mutationCount: 0,
119
+
120
+ takeCheckpoint: () => {
121
+ const currentState = partialize(get() as T);
122
+ set((state: FullState) => ({
123
+ ...state,
124
+ _history_past: [...state._history_past, { state: currentState as T, timestamp: Date.now() }].slice(-limit),
125
+ _history_future: [],
126
+ mutationCount: state.mutationCount + 1,
127
+ }));
128
+ },
129
+
130
+ withCheckpoint: <R>(operation: () => R): R => {
131
+ const beforeState = partialize(get() as T);
132
+ // Execute operation and capture result to return later to caller
133
+ const result = operation();
134
+ const afterState = partialize(get() as T);
135
+ // Compare using provided equality function
136
+ const areEqual = equality(beforeState, afterState);
137
+ if (!areEqual) {
138
+ set((state: FullState) => ({
139
+ ...state,
140
+ _history_past: [...state._history_past, { state: beforeState as T, timestamp: Date.now() }].slice(-limit),
141
+ _history_future: [],
142
+ mutationCount: state.mutationCount + 1,
143
+ }));
144
+ }
145
+ return result;
146
+ },
147
+
148
+ undo: () => {
149
+ const state = get();
150
+ if (state._history_past.length === 0) return;
151
+ const past = [...state._history_past];
152
+ const previousFrame = past.pop()!;
153
+ // Save current state to future before restoring
154
+ const currentState = partialize(state as T);
155
+ set({
156
+ ...state,
157
+ ...previousFrame.state,
158
+ _history_past: past,
159
+ _history_future: [...state._history_future, { state: currentState as T, timestamp: Date.now() }],
160
+ mutationCount: state.mutationCount + 1,
161
+ } as FullState);
162
+ },
163
+
164
+ redo: () => {
165
+ const state = get();
166
+ if (state._history_future.length === 0) return;
167
+ const future = [...state._history_future];
168
+ const nextFrame = future.pop()!;
169
+ // Save current state to past before restoring
170
+ const currentState = partialize(state as T);
171
+ set({
172
+ ...state,
173
+ ...nextFrame.state,
174
+ _history_past: [...state._history_past, { state: currentState as T, timestamp: Date.now() }].slice(-limit),
175
+ _history_future: future,
176
+ mutationCount: state.mutationCount + 1,
177
+ } as FullState);
178
+ },
179
+
180
+ clearHistory: () => {
181
+ set((state: FullState) => ({
182
+ ...state,
183
+ _history_past: [],
184
+ _history_future: [],
185
+ mutationCount: state.mutationCount + 1,
186
+ }));
187
+ },
188
+
189
+ canUndo: () => (get() as FullState)._history_past.length > 0,
190
+ canRedo: () => (get() as FullState)._history_future.length > 0,
191
+
192
+ exportHistory: () => ({
193
+ past: [...(get() as FullState)._history_past],
194
+ future: [...(get() as FullState)._history_future],
195
+ }),
196
+
197
+ importHistory: (data: HistoryData) => {
198
+ set((state: FullState) => ({
199
+ ...state,
200
+ _history_past: [...data.past] as HistoryFrame<T>[],
201
+ _history_future: [...data.future] as HistoryFrame<T>[],
202
+ mutationCount: state.mutationCount + 1,
203
+ }));
204
+ },
205
+ } as FullState;
206
+ };
207
+ };
@@ -1,57 +1,57 @@
1
- import { MemoryRegistry, type MemoryType, type Memory } from "@foresthubai/workflow-core/memory";
2
- import { useEditorStore } from "../stores/editorStore";
3
- import { generateId } from "@foresthubai/workflow-core/id";
4
- import { seedDefaultArguments, uniqueName } from "./resourceHelpers";
5
-
6
- /** Default label prefix per memory type. */
7
- const LABEL_PREFIX: Record<MemoryType, string> = {
8
- MemoryFile: "memory",
9
- VectorDatabase: "vectordb",
10
- };
11
-
12
- /** Create a new memory primitive of the given type in the editor store. Returns the new instance. */
13
- export function addMemory(type: MemoryType): Memory {
14
- const id = generateId();
15
- const existing = Object.values(useEditorStore.getState().memory).map((m) => m.label);
16
- const instance: Memory = {
17
- id,
18
- label: uniqueName(LABEL_PREFIX[type], existing),
19
- type,
20
- arguments: seedDefaultArguments(MemoryRegistry.getByType(type)?.parameters ?? []),
21
- };
22
- useEditorStore.getState().setMemory((mem) => ({ ...mem, [id]: instance }));
23
- return instance;
24
- }
25
-
26
- /**
27
- * Apply a partial patch to a memory primitive. Top-level `label` and the
28
- * `arguments` record are merged separately. The `type` discriminator is fixed
29
- * at creation (the user picks a type when adding), so it is never patched here.
30
- */
31
- export function updateMemory(id: string, patch: { label?: string; arguments?: Record<string, unknown> }): void {
32
- const key = id;
33
- useEditorStore.getState().setMemory((mem) => {
34
- const existing = mem[key];
35
- if (!existing) return mem;
36
- return {
37
- ...mem,
38
- [key]: {
39
- ...existing,
40
- ...(patch.label !== undefined ? { label: patch.label } : {}),
41
- ...(patch.arguments ? { arguments: { ...existing.arguments, ...patch.arguments } } : {}),
42
- },
43
- };
44
- });
45
- }
46
-
47
- export function deleteMemory(id: string): void {
48
- const key = id;
49
- useEditorStore.getState().setMemory((mem) => {
50
- const { [key]: _drop, ...rest } = mem;
51
- return rest;
52
- });
53
- const sel = useEditorStore.getState().selection;
54
- if (sel.kind === "memory" && sel.id === id) {
55
- useEditorStore.getState().clearSelection();
56
- }
57
- }
1
+ import { MemoryRegistry, type MemoryType, type Memory } from "@foresthubai/workflow-core/memory";
2
+ import { useEditorStore } from "../stores/editorStore";
3
+ import { generateId } from "@foresthubai/workflow-core/id";
4
+ import { seedDefaultArguments, uniqueName } from "./resourceHelpers";
5
+
6
+ /** Default label prefix per memory type. */
7
+ const LABEL_PREFIX: Record<MemoryType, string> = {
8
+ MemoryFile: "memory",
9
+ VectorDatabase: "vectordb",
10
+ };
11
+
12
+ /** Create a new memory primitive of the given type in the editor store. Returns the new instance. */
13
+ export function addMemory(type: MemoryType): Memory {
14
+ const id = generateId();
15
+ const existing = Object.values(useEditorStore.getState().memory).map((m) => m.label);
16
+ const instance: Memory = {
17
+ id,
18
+ label: uniqueName(LABEL_PREFIX[type], existing),
19
+ type,
20
+ arguments: seedDefaultArguments(MemoryRegistry.getByType(type)?.parameters ?? []),
21
+ };
22
+ useEditorStore.getState().setMemory((mem) => ({ ...mem, [id]: instance }));
23
+ return instance;
24
+ }
25
+
26
+ /**
27
+ * Apply a partial patch to a memory primitive. Top-level `label` and the
28
+ * `arguments` record are merged separately. The `type` discriminator is fixed
29
+ * at creation (the user picks a type when adding), so it is never patched here.
30
+ */
31
+ export function updateMemory(id: string, patch: { label?: string; arguments?: Record<string, unknown> }): void {
32
+ const key = id;
33
+ useEditorStore.getState().setMemory((mem) => {
34
+ const existing = mem[key];
35
+ if (!existing) return mem;
36
+ return {
37
+ ...mem,
38
+ [key]: {
39
+ ...existing,
40
+ ...(patch.label !== undefined ? { label: patch.label } : {}),
41
+ ...(patch.arguments ? { arguments: { ...existing.arguments, ...patch.arguments } } : {}),
42
+ },
43
+ };
44
+ });
45
+ }
46
+
47
+ export function deleteMemory(id: string): void {
48
+ const key = id;
49
+ useEditorStore.getState().setMemory((mem) => {
50
+ const { [key]: _drop, ...rest } = mem;
51
+ return rest;
52
+ });
53
+ const sel = useEditorStore.getState().selection;
54
+ if (sel.kind === "memory" && sel.id === id) {
55
+ useEditorStore.getState().clearSelection();
56
+ }
57
+ }