1+ import { dump as yamlDump } from 'js-yaml'
12import { create } from 'zustand'
23import { devtools } from 'zustand/middleware'
3- import { dump as yamlDump } from 'js-yaml'
4- import { getBlock } from '@/blocks'
54import { createLogger } from '@/lib/logs/console-logger'
6- import { useWorkflowStore } from '../workflow/store'
5+ import { getBlock } from '@/blocks'
6+ import type { SubBlockConfig } from '@/blocks/types'
77import { useSubBlockStore } from '../subblock/store'
8+ import { useWorkflowStore } from '../workflow/store'
89import type { BlockState , WorkflowState } from '../workflow/types'
9- import type { SubBlockConfig } from '@/blocks/types'
1010
1111const logger = createLogger ( 'WorkflowYamlStore' )
1212
@@ -58,13 +58,13 @@ function extractBlockInputs(blockState: BlockState, blockId: string): Record<str
5858 // Process each subBlock configuration
5959 blockConfig . subBlocks . forEach ( ( subBlockConfig : SubBlockConfig ) => {
6060 const subBlockId = subBlockConfig . id
61-
61+
6262 // Skip hidden or conditional fields that aren't active
6363 if ( subBlockConfig . hidden ) return
64-
64+
6565 // Get value from subblock store or fallback to block state
66- const value = subBlockStore . getValue ( blockId , subBlockId ) ??
67- blockState . subBlocks [ subBlockId ] ?. value
66+ const value =
67+ subBlockStore . getValue ( blockId , subBlockId ) ?? blockState . subBlocks [ subBlockId ] ?. value
6868
6969 // Include value if it exists and isn't empty
7070 if ( value !== undefined && value !== null && value !== '' ) {
@@ -76,14 +76,14 @@ function extractBlockInputs(blockState: BlockState, blockId: string): Record<str
7676 inputs [ subBlockId ] = value
7777 }
7878 break
79-
79+
8080 case 'checkbox-list' :
8181 // Checkbox lists return arrays
8282 if ( Array . isArray ( value ) && value . length > 0 ) {
8383 inputs [ subBlockId ] = value
8484 }
8585 break
86-
86+
8787 case 'code' :
8888 // Code blocks should preserve formatting
8989 if ( typeof value === 'string' && value . trim ( ) ) {
@@ -92,24 +92,31 @@ function extractBlockInputs(blockState: BlockState, blockId: string): Record<str
9292 inputs [ subBlockId ] = value
9393 }
9494 break
95-
95+
9696 case 'switch' :
9797 // Boolean values
9898 inputs [ subBlockId ] = Boolean ( value )
9999 break
100-
100+
101101 case 'slider' :
102102 // Numeric values
103- if ( typeof value === 'number' || ( typeof value === 'string' && ! isNaN ( Number ( value ) ) ) ) {
103+ if (
104+ typeof value === 'number' ||
105+ ( typeof value === 'string' && ! Number . isNaN ( Number ( value ) ) )
106+ ) {
104107 inputs [ subBlockId ] = Number ( value )
105108 }
106109 break
107-
110+
108111 default :
109112 // Text inputs, dropdowns, etc.
110113 if ( typeof value === 'string' && value . trim ( ) ) {
111114 inputs [ subBlockId ] = value . trim ( )
112- } else if ( typeof value === 'object' || typeof value === 'number' || typeof value === 'boolean' ) {
115+ } else if (
116+ typeof value === 'object' ||
117+ typeof value === 'number' ||
118+ typeof value === 'boolean'
119+ ) {
113120 inputs [ subBlockId ] = value
114121 }
115122 break
@@ -125,8 +132,8 @@ function extractBlockInputs(blockState: BlockState, blockId: string): Record<str
125132 */
126133function findPrecedingBlocks ( blockId : string , edges : any [ ] ) : string [ ] {
127134 return edges
128- . filter ( edge => edge . target === blockId )
129- . map ( edge => edge . source )
135+ . filter ( ( edge ) => edge . target === blockId )
136+ . map ( ( edge ) => edge . source )
130137 . filter ( ( source , index , arr ) => arr . indexOf ( source ) === index ) // Remove duplicates
131138}
132139
@@ -135,8 +142,8 @@ function findPrecedingBlocks(blockId: string, edges: any[]): string[] {
135142 */
136143function findFollowingBlocks ( blockId : string , edges : any [ ] ) : string [ ] {
137144 return edges
138- . filter ( edge => edge . source === blockId )
139- . map ( edge => edge . target )
145+ . filter ( ( edge ) => edge . source === blockId )
146+ . map ( ( edge ) => edge . target )
140147 . filter ( ( target , index , arr ) => arr . indexOf ( target ) === index ) // Remove duplicates
141148}
142149
@@ -147,7 +154,7 @@ function generateWorkflowYaml(workflowState: WorkflowState): string {
147154 try {
148155 const yamlWorkflow : YamlWorkflow = {
149156 version : '1.0' ,
150- blocks : { }
157+ blocks : { } ,
151158 }
152159
153160 // Process each block
@@ -158,7 +165,7 @@ function generateWorkflowYaml(workflowState: WorkflowState): string {
158165
159166 const yamlBlock : YamlBlock = {
160167 type : blockState . type ,
161- name : blockState . name
168+ name : blockState . name ,
162169 }
163170
164171 // Only include inputs if they exist
@@ -170,7 +177,7 @@ function generateWorkflowYaml(workflowState: WorkflowState): string {
170177 if ( preceding . length > 0 ) {
171178 yamlBlock . preceding = preceding
172179 }
173-
180+
174181 if ( following . length > 0 ) {
175182 yamlBlock . following = following
176183 }
@@ -183,7 +190,7 @@ function generateWorkflowYaml(workflowState: WorkflowState): string {
183190 indent : 2 ,
184191 lineWidth : - 1 , // Disable line wrapping
185192 noRefs : true ,
186- sortKeys : false
193+ sortKeys : false ,
187194 } )
188195 } catch ( error ) {
189196 logger . error ( 'Failed to generate workflow YAML:' , error )
@@ -200,32 +207,32 @@ export const useWorkflowYamlStore = create<WorkflowYamlStore>()(
200207 generateYaml : ( ) => {
201208 const workflowState = useWorkflowStore . getState ( )
202209 const yaml = generateWorkflowYaml ( workflowState )
203-
210+
204211 set ( {
205212 yaml,
206- lastGenerated : Date . now ( )
213+ lastGenerated : Date . now ( ) ,
207214 } )
208215 } ,
209216
210217 getYaml : ( ) => {
211218 const currentTime = Date . now ( )
212219 const { yaml, lastGenerated } = get ( )
213-
220+
214221 // Auto-refresh if data is stale (older than 1 second) or never generated
215222 if ( ! lastGenerated || currentTime - lastGenerated > 1000 ) {
216223 get ( ) . generateYaml ( )
217224 return get ( ) . yaml
218225 }
219-
226+
220227 return yaml
221228 } ,
222229
223230 refreshYaml : ( ) => {
224231 get ( ) . generateYaml ( )
225- }
232+ } ,
226233 } ) ,
227234 {
228- name : 'workflow-yaml-store'
235+ name : 'workflow-yaml-store' ,
229236 }
230237 )
231238)
@@ -236,16 +243,17 @@ let lastWorkflowState: { blockCount: number; edgeCount: number } | null = null
236243useWorkflowStore . subscribe ( ( state ) => {
237244 const currentState = {
238245 blockCount : Object . keys ( state . blocks ) . length ,
239- edgeCount : state . edges . length
246+ edgeCount : state . edges . length ,
240247 }
241-
248+
242249 // Only refresh if the structure has changed
243- if ( ! lastWorkflowState ||
244- lastWorkflowState . blockCount !== currentState . blockCount ||
245- lastWorkflowState . edgeCount !== currentState . edgeCount ) {
246-
250+ if (
251+ ! lastWorkflowState ||
252+ lastWorkflowState . blockCount !== currentState . blockCount ||
253+ lastWorkflowState . edgeCount !== currentState . edgeCount
254+ ) {
247255 lastWorkflowState = currentState
248-
256+
249257 // Debounce the refresh to avoid excessive updates
250258 const refreshYaml = useWorkflowYamlStore . getState ( ) . refreshYaml
251259 setTimeout ( refreshYaml , 100 )
@@ -257,12 +265,12 @@ let lastSubBlockChangeTime = 0
257265
258266useSubBlockStore . subscribe ( ( state ) => {
259267 const currentTime = Date . now ( )
260-
268+
261269 // Debounce rapid changes
262270 if ( currentTime - lastSubBlockChangeTime > 100 ) {
263271 lastSubBlockChangeTime = currentTime
264-
272+
265273 const refreshYaml = useWorkflowYamlStore . getState ( ) . refreshYaml
266274 setTimeout ( refreshYaml , 100 )
267275 }
268- } )
276+ } )
0 commit comments