d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
130 lines
3.2 KiB
TypeScript
130 lines
3.2 KiB
TypeScript
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,
|
|
}
|
|
}
|