Skip to content
Draft
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 @@ -3,6 +3,8 @@
import { useMemo, useState } from 'react'
import { ChipCombobox, type ComboboxOption } from '@sim/emcn'
import { Loader } from '@sim/emcn/icons'
import { generateShortId } from '@sim/utils/id'
import { useParams } from 'next/navigation'
import { SEARCH_DEBOUNCE_MS } from '@/lib/url-state'
import { SELECTOR_CONTEXT_FIELDS } from '@/lib/workflows/subblocks/context'
import { getDependsOnFields } from '@/lib/workflows/subblocks/dependencies'
Expand Down Expand Up @@ -41,11 +43,19 @@ export function ConnectorSelectorField({
canonicalModes,
disabled,
}: ConnectorSelectorFieldProps) {
const params = useParams<{ workspaceId: string; id: string }>()
const isMulti = Boolean(field.multi)
const [searchTerm, setSearchTerm] = useState('')
const selectorCacheScope = useMemo(
() => generateShortId(),
[field.id, field.selectorKey, credentialId, sourceConfig]
)

const context = useMemo<SelectorContext>(() => {
const ctx: SelectorContext = {}
const ctx: SelectorContext = {
workspaceId: params.workspaceId,
selectorCacheScope,
}
if (credentialId) ctx.oauthCredential = credentialId
if (field.mimeType) ctx.mimeType = field.mimeType

Expand All @@ -60,7 +70,16 @@ export function ConnectorSelectorField({
}

return ctx
}, [credentialId, field.mimeType, field.dependsOn, sourceConfig, configFields, canonicalModes])
}, [
credentialId,
field.mimeType,
field.dependsOn,
sourceConfig,
configFields,
canonicalModes,
params.workspaceId,
selectorCacheScope,
])

const depsResolved = useMemo(() => {
if (!field.dependsOn) return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use client'

import { useMemo } from 'react'
import { generateShortId } from '@sim/utils/id'
import { useParams } from 'next/navigation'
import { SELECTOR_CONTEXT_FIELDS } from '@/lib/workflows/subblocks/context'
import type { SubBlockConfig } from '@/blocks/types'
import { extractEnvVarName, isEnvVarReference, isReference } from '@/executor/constants'
import { usePersonalEnvironment } from '@/hooks/queries/environment'
import {
applySelectorDependenciesToContext,
resolveSelectorDependencyValues,
} from '@/hooks/selectors/context-resolution'
import { getSelectorDefinition } from '@/hooks/selectors/registry'
import type { SelectorContext, SelectorKey } from '@/hooks/selectors/types'
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
import { useDependsOnGate } from './use-depends-on-gate'
Expand Down Expand Up @@ -35,6 +39,15 @@ export function useSelectorSetup(

const { data: envVariables = {} } = usePersonalEnvironment()

const selectorKey = (subBlock.selectorKey ?? null) as SelectorKey | null
const serverResolvedContextFields = useMemo(
() =>
new Set<keyof SelectorContext>(
selectorKey ? (getSelectorDefinition(selectorKey).serverResolvedContextFields ?? []) : []
),
[selectorKey]
)

const { finalDisabled, dependencyValues, canonicalIndex } = useDependsOnGate(
blockId,
subBlock,
Expand All @@ -43,42 +56,33 @@ export function useSelectorSetup(

const [impersonateUserEmail] = useSubBlockValue<string | null>(blockId, 'impersonateUserEmail')

const selectorCacheScope = useMemo(
() => generateShortId(),
[blockId, subBlock.id, selectorKey, dependencyValues]
)

const resolvedDependencyValues = useMemo(() => {
const resolved: Record<string, unknown> = {}
for (const [key, value] of Object.entries(dependencyValues)) {
if (value === null || value === undefined) {
resolved[key] = value
continue
}
const str = String(value)
if (isEnvVarReference(str)) {
const varName = extractEnvVarName(str)
resolved[key] = envVariables[varName]?.value || undefined
} else {
resolved[key] = value
}
}
return resolved
}, [dependencyValues, envVariables])
return resolveSelectorDependencyValues({
dependencyValues,
personalEnvironment: envVariables,
canonicalIndex,
serverResolvedContextFields,
})
}, [dependencyValues, envVariables, canonicalIndex, serverResolvedContextFields])

const selectorContext = useMemo<SelectorContext>(() => {
const context: SelectorContext = {
workflowId,
workspaceId: workspaceId || undefined,
selectorCacheScope,
mimeType: subBlock.mimeType,
}

for (const [depKey, value] of Object.entries(resolvedDependencyValues)) {
if (value === null || value === undefined) continue
const strValue = String(value)
if (!strValue) continue
if (isReference(strValue)) continue

const canonicalParamId = canonicalIndex.canonicalIdBySubBlockId[depKey] ?? depKey
if (SELECTOR_CONTEXT_FIELDS.has(canonicalParamId as keyof SelectorContext)) {
context[canonicalParamId as keyof SelectorContext] = strValue
}
}
applySelectorDependenciesToContext({
context,
dependencyValues: resolvedDependencyValues,
canonicalIndex,
})

if (context.oauthCredential && impersonateUserEmail) {
context.impersonateUserEmail = impersonateUserEmail
Expand All @@ -90,12 +94,13 @@ export function useSelectorSetup(
canonicalIndex,
workflowId,
workspaceId,
selectorCacheScope,
subBlock.mimeType,
impersonateUserEmail,
])

return {
selectorKey: (subBlock.selectorKey ?? null) as SelectorKey | null,
selectorKey,
selectorContext,
allowSearch: subBlock.selectorAllowSearch ?? true,
disabled: finalDisabled || !subBlock.selectorKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useMemo } from 'react'
import { ChipCombobox, type ComboboxOption, Loader } from '@sim/emcn'
import { generateShortId } from '@sim/utils/id'
import { dependentFieldNoun } from '@/ee/workspace-forking/components/fork-sync/dependent-field-noun'
import type { SelectorContext, SelectorKey } from '@/hooks/selectors/types'
import { useSelectorOptions } from '@/hooks/selectors/use-selector-query'
Expand Down Expand Up @@ -31,11 +32,13 @@ export function DependentFieldSelector({
onChange,
title,
}: DependentFieldSelectorProps) {
const dependencyIdentity = JSON.stringify(context)
const selectorCacheScope = useMemo(() => generateShortId(), [selectorKey, dependencyIdentity])
const selectorContext = useMemo<SelectorContext>(() => {
const ctx: SelectorContext = {}
const ctx: SelectorContext = { selectorCacheScope }
Object.assign(ctx, context)
return ctx
}, [context])
}, [context, selectorCacheScope])

const { data: options = [], isLoading } = useSelectorOptions(selectorKey, {
context: selectorContext,
Expand Down
66 changes: 66 additions & 0 deletions apps/sim/hooks/selectors/context-resolution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import type { CanonicalIndex } from '@/lib/workflows/subblocks/visibility'
import {
applySelectorDependenciesToContext,
resolveSelectorDependencyValues,
} from '@/hooks/selectors/context-resolution'
import type { SelectorContext } from '@/hooks/selectors/types'

const canonicalIndex: CanonicalIndex = {
groupsById: {},
canonicalIdBySubBlockId: {
domainInput: 'domain',
tokenInput: 'oauthCredential',
},
}

describe('selector dependency context resolution', () => {
it('preserves exact references for opted-in canonical fields', () => {
const result = resolveSelectorDependencyValues({
dependencyValues: { domainInput: '{{SHARED_DOMAIN}}' },
personalEnvironment: { SHARED_DOMAIN: { value: 'personal.example.com' } },
canonicalIndex,
serverResolvedContextFields: new Set<keyof SelectorContext>(['domain']),
})

expect(result).toEqual({ domainInput: '{{SHARED_DOMAIN}}' })
})

it('retains personal browser resolution for non-opted fields', () => {
const result = resolveSelectorDependencyValues({
dependencyValues: { tokenInput: '{{PERSONAL_TOKEN}}' },
personalEnvironment: { PERSONAL_TOKEN: { value: 'personal-plaintext' } },
canonicalIndex,
serverResolvedContextFields: new Set(),
})

expect(result).toEqual({ tokenInput: 'personal-plaintext' })
})

it('keeps embedded interpolation unchanged for opted-in fields', () => {
const result = resolveSelectorDependencyValues({
dependencyValues: { domainInput: 'https://{{DOMAIN}}' },
personalEnvironment: { DOMAIN: { value: 'plaintext.example.com' } },
canonicalIndex,
serverResolvedContextFields: new Set<keyof SelectorContext>(['domain']),
})

expect(result).toEqual({ domainInput: 'https://{{DOMAIN}}' })
})

it('maps preserved references into context and excludes runtime block outputs', () => {
const context = applySelectorDependenciesToContext({
context: { workflowId: 'workflow-1' },
dependencyValues: {
domainInput: '{{SHARED_DOMAIN}}',
tokenInput: '<runtime.block.output>',
},
canonicalIndex,
})

expect(context).toEqual({ workflowId: 'workflow-1', domain: '{{SHARED_DOMAIN}}' })
})
})
60 changes: 60 additions & 0 deletions apps/sim/hooks/selectors/context-resolution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { SELECTOR_CONTEXT_FIELDS } from '@/lib/workflows/subblocks/context'
import type { CanonicalIndex } from '@/lib/workflows/subblocks/visibility'
import { extractEnvVarName, isEnvVarReference, isReference } from '@/executor/constants'
import type { SelectorContext } from '@/hooks/selectors/types'

interface PersonalEnvironmentValue {
value?: string
}

/** Preserves opted-in environment references and keeps legacy personal resolution elsewhere. */
export function resolveSelectorDependencyValues(input: {
dependencyValues: Record<string, unknown>
personalEnvironment: Record<string, PersonalEnvironmentValue>
canonicalIndex: CanonicalIndex
serverResolvedContextFields: ReadonlySet<keyof SelectorContext>
}): Record<string, unknown> {
const resolved: Record<string, unknown> = {}
for (const [key, value] of Object.entries(input.dependencyValues)) {
if (value === null || value === undefined) {
resolved[key] = value
continue
}

const stringValue = String(value)
if (!isEnvVarReference(stringValue)) {
resolved[key] = value
continue
}

const canonicalParamId = input.canonicalIndex.canonicalIdBySubBlockId[key] ?? key
if (input.serverResolvedContextFields.has(canonicalParamId as keyof SelectorContext)) {
resolved[key] = stringValue
continue
}

const variableName = extractEnvVarName(stringValue)
resolved[key] = input.personalEnvironment[variableName]?.value || undefined
}
return resolved
}

/** Adds eligible dependencies to selector context while excluding runtime block references. */
export function applySelectorDependenciesToContext(input: {
context: SelectorContext
dependencyValues: Record<string, unknown>
canonicalIndex: CanonicalIndex
}): SelectorContext {
for (const [dependencyKey, value] of Object.entries(input.dependencyValues)) {
if (value === null || value === undefined) continue
const stringValue = String(value)
if (!stringValue || isReference(stringValue)) continue

const canonicalParamId =
input.canonicalIndex.canonicalIdBySubBlockId[dependencyKey] ?? dependencyKey
if (SELECTOR_CONTEXT_FIELDS.has(canonicalParamId as keyof SelectorContext)) {
input.context[canonicalParamId as keyof SelectorContext] = stringValue
}
}
return input.context
}
Loading
Loading