Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

143 lines
5.4 KiB
TypeScript

import { createLogger } from '@sim/logger'
import { isPlainRecord } from '@sim/utils/object'
import { DEFAULT_SUBBLOCK_TYPE } from '@sim/workflow-persistence/subblocks'
import { getBlock } from '@/blocks'
import { isCustomBlockType } from '@/blocks/custom/build-config'
import type { BlockState } from '@/stores/workflows/workflow/types'
const logger = createLogger('WorkflowSubblockSanitization')
interface SanitizeMalformedSubBlocksOptions {
convertEmptyStringToNull?: boolean
}
interface SanitizableBlock {
id: string
type: string
subBlocks?: Record<string, unknown>
}
/**
* Repairs legacy subBlock metadata when the map key identifies a real field,
* and drops entries that cannot be associated with a stable subBlock.
*
* For keys the block registry declares, the CONFIGURED type is authoritative
* and overwrites a contradicting stored type. Stored types drift in two ways:
* fallback writers stamp a plausible-but-wrong default (`short-input`) when
* they synthesize a missing structure entry, and block configs evolve their
* declared types over time while persisted rows keep the old one. A wrong
* stored type silently disables type-gated logic downstream — most damaging
* for `condition-input`/`router-input`, where copy-time id remapping skips
* the conditions array while edge handles still remap, orphaning the edges.
* Draft loads persist this repair via `persistMigratedBlocks`, so stored
* state converges back to the registry.
*
* Custom blocks are schema-agnostic here: their server-side config never
* declares the per-field input sub-blocks (the execution overlay passes bare
* wiring rows, and this may run with no overlay at all), so "not in config"
* carries no signal for them. A consumer-typed field value stored via the
* realtime `type: 'unknown'` fallback must be repaired to a concrete type and
* kept — dropping it would delete user input from the draft. Values for fields
* the source workflow no longer has are filtered at serialization/execution
* (`customBlockHasDeclaredInputs`, `remapCustomBlockInputKeys`), never at rest.
*/
export function sanitizeMalformedSubBlocks(
block: SanitizableBlock,
options: SanitizeMalformedSubBlocksOptions = {}
): { subBlocks: Record<string, BlockState['subBlocks'][string]>; changed: boolean } {
let changed = false
const blockConfig = getBlock(block.type)
const schemaAgnostic = isCustomBlockType(block.type)
const result: Record<string, BlockState['subBlocks'][string]> = {}
for (const [subBlockId, subBlock] of Object.entries(block.subBlocks || {})) {
if (subBlockId === 'undefined') {
logger.warn('Skipping malformed subBlock with key "undefined"', { blockId: block.id })
changed = true
continue
}
const configuredType = blockConfig?.subBlocks?.find((config) => config.id === subBlockId)?.type
if (!isPlainRecord(subBlock)) {
if (!configuredType && !schemaAgnostic) {
logger.warn('Skipping malformed subBlock: unrecognized value entry', {
blockId: block.id,
subBlockId,
})
changed = true
continue
}
logger.warn('Repairing malformed subBlock value', { blockId: block.id, subBlockId })
result[subBlockId] = {
id: subBlockId,
type: configuredType || DEFAULT_SUBBLOCK_TYPE,
value: options.convertEmptyStringToNull && subBlock === '' ? null : subBlock,
} as BlockState['subBlocks'][string]
changed = true
continue
}
if (subBlock.type === 'unknown' && !configuredType && !schemaAgnostic) {
logger.warn('Skipping malformed subBlock: type is "unknown"', {
blockId: block.id,
subBlockId,
})
changed = true
continue
}
const id = typeof subBlock.id === 'string' && subBlock.id.length > 0 ? subBlock.id : subBlockId
const typeFromConfig =
configuredType || blockConfig?.subBlocks?.find((config) => config.id === id)?.type
const missingMetadata =
typeof subBlock.id !== 'string' ||
subBlock.id.length === 0 ||
typeof subBlock.type !== 'string' ||
subBlock.type.length === 0
if (missingMetadata && !typeFromConfig && !schemaAgnostic) {
logger.warn('Skipping malformed subBlock: unrecognized metadata entry', {
blockId: block.id,
subBlockId,
})
changed = true
continue
}
const storedType =
typeof subBlock.type === 'string' && subBlock.type.length > 0 && subBlock.type !== 'unknown'
? subBlock.type
: null
const type = typeFromConfig ?? storedType ?? DEFAULT_SUBBLOCK_TYPE
const hasValue = Object.hasOwn(subBlock, 'value')
const value =
options.convertEmptyStringToNull && subBlock.value === ''
? null
: hasValue
? subBlock.value
: null
const repairedMetadata = id !== subBlock.id || type !== subBlock.type
const normalizedValue = hasValue && value !== subBlock.value
if (repairedMetadata) {
logger.warn('Repairing malformed subBlock metadata', {
blockId: block.id,
subBlockId,
storedType: subBlock.type,
repairedType: type,
})
changed = true
} else if (normalizedValue) {
logger.warn('Normalizing malformed subBlock value', { blockId: block.id, subBlockId })
changed = true
}
result[subBlockId] = { ...subBlock, id, type, value } as BlockState['subBlocks'][string]
}
return { subBlocks: changed ? result : (block.subBlocks as BlockState['subBlocks']), changed }
}