Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SELECTOR_TYPES_HYDRATION_REQUIRED, type SubBlockConfig } from '@/blocks
import { useCollaborativeWorkflow } from '@/hooks/use-collaborative-workflow'
import { useCredentialDisplay } from '@/hooks/use-credential-display'
import { useDisplayName } from '@/hooks/use-display-name'
import { useVariablesStore } from '@/stores/panel/variables/store'
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
import { ActionBar, Connections } from './components'
Expand Down Expand Up @@ -77,12 +78,38 @@ const isPlainObject = (value: unknown): value is Record<string, unknown> => {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}

/**
* Type guard for variable assignments array
*/
const isVariableAssignmentsArray = (
value: unknown
): value is Array<{ id?: string; variableId?: string; variableName?: string; value: any }> => {
return (
Array.isArray(value) &&
value.length > 0 &&
value.every(
(item) =>
typeof item === 'object' &&
item !== null &&
('variableName' in item || 'variableId' in item)
)
)
}

/**
* Formats a subblock value for display, intelligently handling nested objects and arrays.
*/
const getDisplayValue = (value: unknown): string => {
if (value == null || value === '') return '-'

if (isVariableAssignmentsArray(value)) {
const names = value.map((a) => a.variableName).filter((name): name is string => !!name)
if (names.length === 0) return '-'
if (names.length === 1) return names[0]
if (names.length === 2) return `${names[0]}, ${names[1]}`
return `${names[0]}, ${names[1]} +${names.length - 2}`
}

if (isTableRowArray(value)) {
const nonEmptyRows = value.filter((row) => {
const cellValues = Object.values(row.cells)
Expand Down Expand Up @@ -172,13 +199,15 @@ const SubBlockRow = ({
subBlock,
rawValue,
workspaceId,
workflowId,
allSubBlockValues,
}: {
title: string
value?: string
subBlock?: SubBlockConfig
rawValue?: unknown
workspaceId?: string
workflowId?: string
allSubBlockValues?: Record<string, { value: unknown }>
}) => {
const getStringValue = useCallback(
Expand Down Expand Up @@ -235,11 +264,43 @@ const SubBlockRow = ({
planId: getStringValue('planId'),
})

// Subscribe to variables store to reactively update when variables change
const allVariables = useVariablesStore((state) => state.variables)

// Special handling for variables-input to hydrate variable IDs to names from variables store
const variablesDisplayValue = useMemo(() => {
if (subBlock?.type !== 'variables-input' || !isVariableAssignmentsArray(rawValue)) {
return null
}

const workflowVariables = Object.values(allVariables).filter(
(v: any) => v.workflowId === workflowId
)

const names = rawValue
.map((a) => {
// Prioritize ID lookup (source of truth) over stored name
if (a.variableId) {
const variable = workflowVariables.find((v: any) => v.id === a.variableId)
return variable?.name
}
if (a.variableName) return a.variableName
return null
})
.filter((name): name is string => !!name)

if (names.length === 0) return null
if (names.length === 1) return names[0]
if (names.length === 2) return `${names[0]}, ${names[1]}`
return `${names[0]}, ${names[1]} +${names.length - 2}`
}, [subBlock?.type, rawValue, workflowId, allVariables])

const isPasswordField = subBlock?.password === true
const maskedValue = isPasswordField && value && value !== '-' ? '•••' : null

const isSelectorType = subBlock?.type && SELECTOR_TYPES_HYDRATION_REQUIRED.includes(subBlock.type)
const hydratedName = credentialName || dropdownLabel || genericDisplayName
const hydratedName =
credentialName || dropdownLabel || variablesDisplayValue || genericDisplayName
const displayValue = maskedValue || hydratedName || (isSelectorType && value ? '-' : value)

return (
Expand Down Expand Up @@ -840,6 +901,7 @@ export const WorkflowBlock = memo(function WorkflowBlock({
subBlock={subBlock}
rawValue={rawValue}
workspaceId={workspaceId}
workflowId={currentWorkflowId}
allSubBlockValues={subBlockState}
/>
)
Expand Down
1 change: 1 addition & 0 deletions apps/sim/blocks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const SELECTOR_TYPES_HYDRATION_REQUIRED: SubBlockType[] = [
'project-selector',
'knowledge-base-selector',
'document-selector',
'variables-input',
] as const

export type ExtractToolOutput<T> = T extends ToolResponse ? T['output'] : never
Expand Down