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
177 lines
5.3 KiB
TypeScript
177 lines
5.3 KiB
TypeScript
import type { BlockStateController } from '@/executor/execution/types'
|
|
import type { BlockState, NormalizedBlockOutput } from '@/executor/types'
|
|
import { SubflowNodeIdCodec } from '@/executor/utils/subflow-node-id-codec'
|
|
import {
|
|
buildOuterBranchScopedId,
|
|
extractOuterBranchIndex,
|
|
stripCloneSuffixes,
|
|
} from '@/executor/utils/subflow-utils'
|
|
|
|
function normalizeLookupId(id: string): string {
|
|
return SubflowNodeIdCodec.normalizeLookupId(id)
|
|
}
|
|
|
|
function extractBranchSuffix(id: string): string {
|
|
return SubflowNodeIdCodec.extractBranchSuffix(id)
|
|
}
|
|
|
|
function extractLoopSuffix(id: string): string {
|
|
return SubflowNodeIdCodec.extractLoopSuffix(id)
|
|
}
|
|
export interface LoopScope {
|
|
iteration: number
|
|
currentIterationOutputs: Map<string, NormalizedBlockOutput>
|
|
allIterationOutputs: NormalizedBlockOutput[][]
|
|
maxIterations?: number
|
|
item?: any
|
|
items?: any[]
|
|
condition?: string
|
|
loopType?: 'for' | 'forEach' | 'while' | 'doWhile'
|
|
skipFirstConditionCheck?: boolean
|
|
skippedAtStart?: boolean
|
|
/** Error message if loop validation failed (e.g., exceeded max iterations) */
|
|
validationError?: string
|
|
}
|
|
|
|
export interface ParallelScope {
|
|
parallelId: string
|
|
totalBranches: number
|
|
batchSize?: number
|
|
currentBatchStart?: number
|
|
currentBatchSize?: number
|
|
accumulatedOutputs?: Map<number, NormalizedBlockOutput[]>
|
|
branchOutputs: Map<number, NormalizedBlockOutput[]>
|
|
items?: any[]
|
|
/** Error message if parallel validation failed (e.g., exceeded max branches) */
|
|
validationError?: string
|
|
/** Whether the parallel has an empty distribution and should be skipped */
|
|
isEmpty?: boolean
|
|
}
|
|
|
|
export class ExecutionState implements BlockStateController {
|
|
private readonly blockStates: Map<string, BlockState>
|
|
private readonly executedBlocks: Set<string>
|
|
|
|
constructor(blockStates?: Map<string, BlockState>, executedBlocks?: Set<string>) {
|
|
this.blockStates = blockStates ?? new Map()
|
|
this.executedBlocks = executedBlocks ?? new Set()
|
|
}
|
|
|
|
getBlockStates(): ReadonlyMap<string, BlockState> {
|
|
return this.blockStates
|
|
}
|
|
|
|
getExecutedBlocks(): ReadonlySet<string> {
|
|
return this.executedBlocks
|
|
}
|
|
|
|
getBlockOutput(blockId: string, currentNodeId?: string): NormalizedBlockOutput | undefined {
|
|
const normalizedId = normalizeLookupId(blockId)
|
|
if (normalizedId !== blockId) {
|
|
return this.blockStates.get(blockId)?.output
|
|
}
|
|
|
|
if (currentNodeId) {
|
|
const scopedOutput = this.getScopedBlockOutput(blockId, currentNodeId)
|
|
if (scopedOutput !== undefined) {
|
|
return scopedOutput
|
|
}
|
|
|
|
if (extractOuterBranchIndex(currentNodeId) !== undefined) {
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
const direct = this.blockStates.get(blockId)?.output
|
|
if (direct !== undefined) {
|
|
return direct
|
|
}
|
|
|
|
if (currentNodeId && extractBranchSuffix(currentNodeId) === '') {
|
|
const stableBranchZeroOutput = this.blockStates.get(
|
|
buildOuterBranchScopedId(blockId, 0)
|
|
)?.output
|
|
if (stableBranchZeroOutput !== undefined) {
|
|
return stableBranchZeroOutput
|
|
}
|
|
|
|
const branchZeroOutput = this.blockStates.get(
|
|
`${blockId}₍0₎${extractLoopSuffix(currentNodeId)}`
|
|
)?.output
|
|
if (branchZeroOutput !== undefined) {
|
|
return branchZeroOutput
|
|
}
|
|
}
|
|
|
|
for (const [storedId, state] of this.blockStates.entries()) {
|
|
if (normalizeLookupId(storedId) === blockId) {
|
|
return state.output
|
|
}
|
|
}
|
|
|
|
return undefined
|
|
}
|
|
|
|
private getScopedBlockOutput(
|
|
blockId: string,
|
|
currentNodeId: string
|
|
): NormalizedBlockOutput | undefined {
|
|
const currentBranchSuffix = extractBranchSuffix(currentNodeId)
|
|
const loopSuffix = extractLoopSuffix(currentNodeId)
|
|
|
|
const currentOuterBranchIndex = extractOuterBranchIndex(currentNodeId)
|
|
if (currentOuterBranchIndex !== undefined) {
|
|
for (const [storedId, state] of this.blockStates.entries()) {
|
|
if (stripCloneSuffixes(storedId) !== blockId) continue
|
|
if (extractOuterBranchIndex(storedId) !== currentOuterBranchIndex) continue
|
|
if (extractBranchSuffix(storedId) !== currentBranchSuffix) continue
|
|
if (extractLoopSuffix(storedId) !== loopSuffix) continue
|
|
|
|
return state.output
|
|
}
|
|
|
|
const siblingBranchOutput = this.blockStates.get(
|
|
`${blockId}₍${currentOuterBranchIndex}₎`
|
|
)?.output
|
|
if (siblingBranchOutput !== undefined) {
|
|
return siblingBranchOutput
|
|
}
|
|
} else {
|
|
const withSuffix = `${blockId}${currentBranchSuffix}${loopSuffix}`
|
|
const suffixedOutput = this.blockStates.get(withSuffix)?.output
|
|
if (suffixedOutput !== undefined) {
|
|
return suffixedOutput
|
|
}
|
|
}
|
|
|
|
return undefined
|
|
}
|
|
|
|
setBlockOutput(blockId: string, output: NormalizedBlockOutput, executionTime = 0): void {
|
|
this.blockStates.set(blockId, { output, executed: true, executionTime })
|
|
this.executedBlocks.add(blockId)
|
|
}
|
|
|
|
setBlockState(blockId: string, state: BlockState): void {
|
|
this.blockStates.set(blockId, state)
|
|
if (state.executed) {
|
|
this.executedBlocks.add(blockId)
|
|
} else {
|
|
this.executedBlocks.delete(blockId)
|
|
}
|
|
}
|
|
|
|
deleteBlockState(blockId: string): void {
|
|
this.blockStates.delete(blockId)
|
|
this.executedBlocks.delete(blockId)
|
|
}
|
|
|
|
unmarkExecuted(blockId: string): void {
|
|
this.executedBlocks.delete(blockId)
|
|
}
|
|
|
|
hasExecuted(blockId: string): boolean {
|
|
return this.executedBlocks.has(blockId)
|
|
}
|
|
}
|