-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathdefaults.ts
More file actions
129 lines (105 loc) · 3.23 KB
/
Copy pathdefaults.ts
File metadata and controls
129 lines (105 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { generateId } from '@sim/utils/id'
import { getEffectiveBlockOutputs } from '@/lib/workflows/blocks/block-outputs'
import { createDefaultInputFormatField } from '@/lib/workflows/input-format'
import { getBlock } from '@/blocks'
import type { BlockConfig, SubBlockConfig } from '@/blocks/types'
import type { BlockState, SubBlockState, WorkflowState } from '@/stores/workflows/workflow/types'
export interface DefaultWorkflowArtifacts {
workflowState: WorkflowState
subBlockValues: Record<string, Record<string, unknown>>
startBlockId: string
}
const START_BLOCK_TYPE = 'start_trigger'
const DEFAULT_START_POSITION = { x: 0, y: 0 }
function cloneDefaultValue(value: unknown): unknown {
if (Array.isArray(value)) {
return value.map((item) => cloneDefaultValue(item))
}
if (value && typeof value === 'object') {
return { ...(value as Record<string, unknown>) }
}
return value ?? null
}
function resolveInitialValue(subBlock: SubBlockConfig): unknown {
if (typeof subBlock.value === 'function') {
try {
return cloneDefaultValue(subBlock.value({}))
} catch (error) {
// Ignore resolution errors and fall back to default/null values
}
}
if (subBlock.defaultValue !== undefined) {
return cloneDefaultValue(subBlock.defaultValue)
}
if (subBlock.type === 'input-format') {
return [createDefaultInputFormatField()]
}
if (subBlock.type === 'table') {
return []
}
return null
}
function buildStartBlockConfig(): BlockConfig {
const blockConfig = getBlock(START_BLOCK_TYPE)
if (!blockConfig) {
throw new Error('Start trigger block configuration is not registered')
}
return blockConfig
}
function buildStartBlockState(
blockConfig: BlockConfig,
blockId: string
): { blockState: BlockState; subBlockValues: Record<string, unknown> } {
const subBlocks: Record<string, SubBlockState> = {}
const subBlockValues: Record<string, unknown> = {}
blockConfig.subBlocks.forEach((config) => {
const initialValue = resolveInitialValue(config)
subBlocks[config.id] = {
id: config.id,
type: config.type,
value: (initialValue ?? null) as SubBlockState['value'],
}
subBlockValues[config.id] = initialValue ?? null
})
const outputs = getEffectiveBlockOutputs(blockConfig.type, subBlocks, {
triggerMode: false,
preferToolOutputs: true,
})
const blockState: BlockState = {
id: blockId,
type: blockConfig.type,
name: blockConfig.name,
position: { ...DEFAULT_START_POSITION },
subBlocks,
outputs,
enabled: true,
horizontalHandles: true,
advancedMode: false,
triggerMode: false,
height: 0,
data: {},
locked: false,
}
return { blockState, subBlockValues }
}
export function buildDefaultWorkflowArtifacts(): DefaultWorkflowArtifacts {
const blockConfig = buildStartBlockConfig()
const startBlockId = generateId()
const { blockState, subBlockValues } = buildStartBlockState(blockConfig, startBlockId)
const workflowState: WorkflowState = {
blocks: {
[startBlockId]: blockState,
},
edges: [],
loops: {},
parallels: {},
lastSaved: Date.now(),
}
return {
workflowState,
subBlockValues: {
[startBlockId]: subBlockValues,
},
startBlockId,
}
}