chore: import upstream snapshot with attribution
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

This commit is contained in:
wehub-resource-sync
2026-07-13 13:20:55 +08:00
commit d25d482dc2
13754 changed files with 4996608 additions and 0 deletions
@@ -0,0 +1,296 @@
/**
* @vitest-environment node
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { clearLargeValueCacheForTests } from '@/lib/execution/payloads/cache'
import { isLargeArrayManifest } from '@/lib/execution/payloads/large-array-manifest-metadata'
import { BlockType } from '@/executor/constants'
import { VariablesBlockHandler } from '@/executor/handlers/variables/variables-handler'
import type { ExecutionContext } from '@/executor/types'
import type { SerializedBlock } from '@/serializer/types'
const { mockUploadFile } = vi.hoisted(() => ({
mockUploadFile: vi.fn(),
}))
vi.mock('@/lib/uploads', () => ({
StorageService: {
uploadFile: mockUploadFile,
},
}))
function createContext(overrides: Partial<ExecutionContext> = {}): ExecutionContext {
return {
workflowId: 'workflow-1',
workspaceId: 'workspace-1',
executionId: 'execution-1',
userId: 'user-1',
blockStates: new Map(),
blockLogs: [],
metadata: { duration: 0 },
environmentVariables: {},
workflowVariables: {
'var-1': { id: 'var-1', name: 'issues', type: 'array', value: [] },
},
decisions: { router: new Map(), condition: new Map() },
loopExecutions: new Map(),
executedBlocks: new Set(),
activeExecutionPath: new Set(),
completedLoops: new Set(),
...overrides,
}
}
function createBlock(): SerializedBlock {
return {
id: 'variables-block-1',
metadata: { id: BlockType.VARIABLES, name: 'Variables' },
position: { x: 0, y: 0 },
config: { tool: BlockType.VARIABLES, params: {} },
inputs: {},
outputs: {},
enabled: true,
}
}
describe('VariablesBlockHandler', () => {
beforeEach(() => {
vi.clearAllMocks()
clearLargeValueCacheForTests()
mockUploadFile.mockImplementation(async ({ customKey }) => ({ key: customKey }))
})
it('preserves small assignments inline', async () => {
const handler = new VariablesBlockHandler()
const ctx = createContext()
const value = [{ key: 'SIM-1', summary: 'Small issue' }]
const output = await handler.execute(ctx, createBlock(), {
variables: [
{
variableId: 'var-1',
variableName: 'issues',
type: 'array',
value,
},
],
})
expect(ctx.workflowVariables?.['var-1'].value).toEqual(value)
expect(output).toEqual({ issues: value })
expect(mockUploadFile).not.toHaveBeenCalled()
})
it('includes unmatched assignments in block output without mutating workflow variables', async () => {
const handler = new VariablesBlockHandler()
const ctx = createContext()
const value = [{ key: 'SIM-1', summary: 'Transient issue' }]
const output = await handler.execute(ctx, createBlock(), {
variables: [
{
variableName: 'transientIssues',
type: 'array',
value,
},
],
})
expect(ctx.workflowVariables).not.toHaveProperty('transientIssues')
expect(output).toEqual({ transientIssues: value })
})
it('keeps special unmatched assignment names as own output fields', async () => {
const handler = new VariablesBlockHandler()
const ctx = createContext()
const value = { polluted: true }
const output = await handler.execute(ctx, createBlock(), {
variables: [
{
variableName: '__proto__',
type: 'object',
value,
},
],
})
expect(Object.hasOwn(output, '__proto__')).toBe(true)
expect(output.__proto__).toEqual(value)
expect(Object.getPrototypeOf(output)).toBe(Object.prototype)
})
it('does not treat inherited prototype keys as existing workflow variable IDs', async () => {
const handler = new VariablesBlockHandler()
const ctx = createContext()
const value = { safe: true }
const originalPrototype = Object.getPrototypeOf(ctx.workflowVariables)
const output = await handler.execute(ctx, createBlock(), {
variables: [
{
variableId: '__proto__',
variableName: 'prototypeAssignment',
type: 'object',
value,
},
],
})
expect(Object.getPrototypeOf(ctx.workflowVariables)).toBe(originalPrototype)
expect(ctx.workflowVariables).not.toHaveProperty('__proto__')
expect(output).toEqual({ prototypeAssignment: value })
})
it('stores oversized array assignments as durable manifests in variables and block output', async () => {
const handler = new VariablesBlockHandler()
const ctx = createContext()
const value = Array.from({ length: 120_000 }, (_, index) => ({
key: `SIM-${index}`,
summary: 'Issue summary that keeps each item small',
}))
const output = await handler.execute(ctx, createBlock(), {
variables: [
{
variableId: 'var-1',
variableName: 'issues',
type: 'array',
value,
},
],
})
const storedValue = ctx.workflowVariables?.['var-1'].value
expect(isLargeArrayManifest(storedValue)).toBe(true)
expect(output.issues).toBe(storedValue)
expect(storedValue).toMatchObject({
__simLargeArrayManifest: true,
kind: 'array',
totalCount: value.length,
})
expect(storedValue.chunkCount).toBeGreaterThan(1)
expect(mockUploadFile).toHaveBeenCalledWith(
expect.objectContaining({
context: 'execution',
preserveKey: true,
customKey: expect.stringContaining('/execution-1/large-value-'),
})
)
})
it('fails clearly when durable context is missing for oversized assignments', async () => {
const handler = new VariablesBlockHandler()
const ctx = createContext({ workspaceId: undefined, executionId: undefined })
const value = Array.from({ length: 120_000 }, (_, index) => ({
key: `SIM-${index}`,
summary: 'Issue summary that keeps each item small',
}))
await expect(
handler.execute(ctx, createBlock(), {
variables: [
{
variableId: 'var-1',
variableName: 'issues',
type: 'array',
value,
},
],
})
).rejects.toThrow(
'Cannot persist large execution value without workspace, workflow, and execution IDs'
)
expect(mockUploadFile).not.toHaveBeenCalled()
})
it('preserves whole large refs before scalar type coercion', async () => {
const handler = new VariablesBlockHandler()
const ref = {
__simLargeValueRef: true,
version: 1,
id: 'lv_ABCDEFGHIJKL',
kind: 'object',
size: 12 * 1024 * 1024,
executionId: 'execution-1',
}
const ctx = createContext({
workflowVariables: {
stringVar: { id: 'stringVar', name: 'stringRef', type: 'string', value: '' },
plainVar: { id: 'plainVar', name: 'plainRef', type: 'plain', value: '' },
numberVar: { id: 'numberVar', name: 'numberRef', type: 'number', value: 0 },
booleanVar: { id: 'booleanVar', name: 'booleanRef', type: 'boolean', value: false },
},
})
await handler.execute(ctx, createBlock(), {
variables: [
{
variableId: 'stringVar',
variableName: 'stringRef',
type: 'string',
value: JSON.stringify(ref),
},
{
variableId: 'plainVar',
variableName: 'plainRef',
type: 'plain',
value: JSON.stringify(ref),
},
{
variableId: 'numberVar',
variableName: 'numberRef',
type: 'number',
value: JSON.stringify(ref),
},
{
variableId: 'booleanVar',
variableName: 'booleanRef',
type: 'boolean',
value: JSON.stringify(ref),
},
],
})
expect(ctx.workflowVariables?.stringVar.value).toEqual(ref)
expect(ctx.workflowVariables?.plainVar.value).toEqual(ref)
expect(ctx.workflowVariables?.numberVar.value).toEqual(ref)
expect(ctx.workflowVariables?.booleanVar.value).toEqual(ref)
})
it('preserves existing variable metadata when compacting reassignment', async () => {
const handler = new VariablesBlockHandler()
const ctx = createContext({
workflowVariables: {
'var-1': {
id: 'var-1',
name: 'issues',
type: 'array',
value: [],
isExisting: true,
},
},
})
const value = [{ key: 'SIM-1', summary: 'Updated' }]
await handler.execute(ctx, createBlock(), {
variables: [
{
variableId: 'var-1',
variableName: 'issues',
type: 'array',
value,
},
],
})
expect(ctx.workflowVariables?.['var-1']).toEqual({
id: 'var-1',
name: 'issues',
type: 'array',
value,
isExisting: true,
})
})
})
@@ -0,0 +1,207 @@
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { parseLargeExecutionValue } from '@/lib/execution/payloads/large-execution-value'
import { compactWorkflowVariableValue } from '@/lib/execution/payloads/serializer'
import type { BlockOutput } from '@/blocks/types'
import { BlockType } from '@/executor/constants'
import type { BlockHandler, ExecutionContext } from '@/executor/types'
import type { SerializedBlock } from '@/serializer/types'
const logger = createLogger('VariablesBlockHandler')
function setOutputValue(output: Record<string, any>, key: string, value: any): void {
Object.defineProperty(output, key, {
value,
enumerable: true,
configurable: true,
writable: true,
})
}
function getWorkflowVariableEntry(
workflowVariables: Record<string, any>,
variableId: string | undefined
): [string, any] | undefined {
if (!variableId || !Object.hasOwn(workflowVariables, variableId)) {
return undefined
}
return [variableId, workflowVariables[variableId]]
}
function setWorkflowVariableEntry(
workflowVariables: Record<string, any>,
id: string,
value: any
): void {
Object.defineProperty(workflowVariables, id, {
value,
enumerable: true,
configurable: true,
writable: true,
})
}
export class VariablesBlockHandler implements BlockHandler {
canHandle(block: SerializedBlock): boolean {
const canHandle = block.metadata?.id === BlockType.VARIABLES
return canHandle
}
async execute(
ctx: ExecutionContext,
block: SerializedBlock,
inputs: Record<string, any>
): Promise<BlockOutput> {
try {
if (!ctx.workflowVariables) {
ctx.workflowVariables = {}
}
const assignments = this.parseAssignments(inputs.variables)
const output: Record<string, any> = {}
for (const assignment of assignments) {
const existingEntry =
getWorkflowVariableEntry(ctx.workflowVariables, assignment.variableId) ??
Object.entries(ctx.workflowVariables).find(([_, v]) => v.name === assignment.variableName)
const value = await this.compactAssignmentValue(ctx, assignment.value)
if (existingEntry?.[1]) {
const [id, variable] = existingEntry
setWorkflowVariableEntry(ctx.workflowVariables, id, {
...variable,
value,
})
} else {
logger.warn(`Variable "${assignment.variableName}" not found in workflow variables`)
}
setOutputValue(output, assignment.variableName, value)
}
return output
} catch (error) {
const normalizedError = toError(error)
logger.error('Variables block execution failed:', normalizedError)
throw new Error(`Variables block execution failed: ${normalizedError.message}`)
}
}
private async compactAssignmentValue(ctx: ExecutionContext, value: any): Promise<any> {
return compactWorkflowVariableValue(value, {
workspaceId: ctx.workspaceId,
workflowId: ctx.workflowId,
executionId: ctx.executionId,
userId: ctx.userId,
largeValueExecutionIds: ctx.largeValueExecutionIds,
largeValueKeys: ctx.largeValueKeys,
allowLargeValueWorkflowScope: ctx.allowLargeValueWorkflowScope,
})
}
private parseAssignments(
assignmentsInput: any
): Array<{ variableId?: string; variableName: string; type: string; value: any }> {
const result: Array<{ variableId?: string; variableName: string; type: string; value: any }> =
[]
if (!assignmentsInput || !Array.isArray(assignmentsInput)) {
return result
}
for (const assignment of assignmentsInput) {
if (assignment?.variableName?.trim()) {
const name = assignment.variableName.trim()
const type = assignment.type || 'string'
const value = this.parseValueByType(assignment.value, type, name)
result.push({
variableId: assignment.variableId,
variableName: name,
type,
value,
})
}
}
return result
}
private parseValueByType(value: any, type: string, variableName?: string): any {
const refValue = parseLargeExecutionValue(value)
if (refValue !== undefined) {
return refValue
}
if (value === null || value === undefined || value === '') {
if (type === 'number') return 0
if (type === 'boolean') return false
if (type === 'array') return []
if (type === 'object') return {}
return ''
}
if (type === 'string' || type === 'plain') {
return typeof value === 'string' ? value : String(value)
}
if (type === 'number') {
if (typeof value === 'number') return value
if (typeof value === 'string') {
const trimmed = value.trim()
if (trimmed === '') return 0
const num = Number(trimmed)
if (Number.isNaN(num)) {
throw new Error(
`Invalid number value for variable "${variableName || 'unknown'}": "${value}". Expected a valid number.`
)
}
return num
}
throw new Error(
`Invalid type for variable "${variableName || 'unknown'}": expected number, got ${typeof value}`
)
}
if (type === 'boolean') {
if (typeof value === 'boolean') return value
if (typeof value === 'string') {
const lower = value.toLowerCase().trim()
if (lower === 'true') return true
if (lower === 'false') return false
throw new Error(
`Invalid boolean value for variable "${variableName || 'unknown'}": "${value}". Expected "true" or "false".`
)
}
return Boolean(value)
}
if (type === 'object' || type === 'array') {
// If value is already an object or array, accept it as-is
// The type hint is for UI purposes and string parsing, not runtime validation
if (typeof value === 'object' && value !== null) {
return value
}
// If it's a string, try to parse it as JSON
if (typeof value === 'string' && value.trim()) {
try {
const parsed = JSON.parse(value)
// Accept any valid JSON object or array
if (typeof parsed === 'object' && parsed !== null) {
return parsed
}
throw new Error(
`Invalid JSON for variable "${variableName || 'unknown'}": parsed value is not an object or array`
)
} catch (error: any) {
throw new Error(
`Invalid JSON for variable "${variableName || 'unknown'}": ${error.message}`
)
}
}
return type === 'array' ? [] : {}
}
return value
}
}