Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
improvement(selectors): simplify selector context + add tests#3453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { buildSelectorContextFromBlock } from '@/lib/workflows/subblocks/context' | ||
| import { getBlock } from '@/blocks/registry' | ||
| import { SELECTOR_TYPES_HYDRATION_REQUIRED, type SubBlockConfig } from '@/blocks/types' | ||
| import { CREDENTIAL_SET, isUuid } from '@/executor/constants' | ||
| import { fetchCredentialSetById } from '@/hooks/queries/credential-sets' | ||
| import { fetchOAuthCredentialDetail } from '@/hooks/queries/oauth-credentials' | ||
| import { getSelectorDefinition } from '@/hooks/selectors/registry' | ||
| import { resolveSelectorForSubBlock } from '@/hooks/selectors/resolution' | ||
| import type { SelectorKey } from '@/hooks/selectors/types' | ||
| import type { SelectorContext, SelectorKey } from '@/hooks/selectors/types' | ||
| import type { WorkflowState } from '@/stores/workflows/workflow/types' | ||
| const logger = createLogger('ResolveValues') | ||
| @@ -39,24 +40,6 @@ interface ResolutionContext { | ||
| blockId?: string | ||
| } | ||
| /** | ||
| * Extended context extracted from block subBlocks for selector resolution | ||
| */ | ||
| interface ExtendedSelectorContext { | ||
| credentialId?: string | ||
| domain?: string | ||
| projectId?: string | ||
| planId?: string | ||
| teamId?: string | ||
| knowledgeBaseId?: string | ||
| siteId?: string | ||
| collectionId?: string | ||
| spreadsheetId?: string | ||
| baseId?: string | ||
| datasetId?: string | ||
| serviceDeskId?: string | ||
| } | ||
| function getSemanticFallback(subBlockId: string, subBlockConfig?: SubBlockConfig): string { | ||
| if (subBlockConfig?.title) { | ||
| return subBlockConfig.title.toLowerCase() | ||
| @@ -150,26 +133,10 @@ async function resolveWorkflow(workflowId: string): Promise<string | null> { | ||
| async function resolveSelectorValue( | ||
| value: string, | ||
| selectorKey: SelectorKey, | ||
| extendedContext: ExtendedSelectorContext, | ||
| workflowId: string | ||
| selectorContext: SelectorContext | ||
| ): Promise<string | null> { | ||
| try { | ||
| const definition = getSelectorDefinition(selectorKey) | ||
| const selectorContext = { | ||
| workflowId, | ||
| credentialId: extendedContext.credentialId, | ||
| domain: extendedContext.domain, | ||
| projectId: extendedContext.projectId, | ||
| planId: extendedContext.planId, | ||
| teamId: extendedContext.teamId, | ||
| knowledgeBaseId: extendedContext.knowledgeBaseId, | ||
| siteId: extendedContext.siteId, | ||
| collectionId: extendedContext.collectionId, | ||
| spreadsheetId: extendedContext.spreadsheetId, | ||
| baseId: extendedContext.baseId, | ||
| datasetId: extendedContext.datasetId, | ||
| serviceDeskId: extendedContext.serviceDeskId, | ||
| } | ||
| if (definition.fetchById) { | ||
| const result = await definition.fetchById({ | ||
| @@ -219,37 +186,14 @@ export function formatValueForDisplay(value: unknown): string { | ||
| return String(value) | ||
| } | ||
| /** | ||
| * Extracts extended context from a block's subBlocks for selector resolution. | ||
| * This mirrors the context extraction done in the UI components. | ||
| */ | ||
| function extractExtendedContext( | ||
| function extractSelectorContext( | ||
| blockId: string, | ||
| currentState: WorkflowState | ||
| ): ExtendedSelectorContext { | ||
| currentState: WorkflowState, | ||
| workflowId: string | ||
| ): SelectorContext { | ||
icecrasher321 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const block = currentState.blocks?.[blockId] | ||
| if (!block?.subBlocks) return {} | ||
| const getStringValue = (id: string): string | undefined => { | ||
| const subBlock = block.subBlocks[id] as { value?: unknown } | undefined | ||
| const val = subBlock?.value | ||
| return typeof val === 'string' ? val : undefined | ||
| } | ||
| return { | ||
| credentialId: getStringValue('credential'), | ||
| domain: getStringValue('domain'), | ||
| projectId: getStringValue('projectId'), | ||
| planId: getStringValue('planId'), | ||
| teamId: getStringValue('teamId'), | ||
| knowledgeBaseId: getStringValue('knowledgeBaseId'), | ||
| siteId: getStringValue('siteId'), | ||
| collectionId: getStringValue('collectionId'), | ||
| spreadsheetId: getStringValue('spreadsheetId') || getStringValue('fileId'), | ||
| baseId: getStringValue('baseId') || getStringValue('baseSelector'), | ||
| datasetId: getStringValue('datasetId') || getStringValue('datasetSelector'), | ||
| serviceDeskId: getStringValue('serviceDeskId') || getStringValue('serviceDeskSelector'), | ||
| } | ||
| if (!block?.subBlocks) return { workflowId } | ||
| return buildSelectorContextFromBlock(block.type, block.subBlocks, { workflowId }) | ||
| } | ||
| /** | ||
| @@ -277,9 +221,9 @@ export async function resolveValueForDisplay( | ||
| const subBlockConfig = blockConfig?.subBlocks.find((sb) => sb.id === context.subBlockId) | ||
| const semanticFallback = getSemanticFallback(context.subBlockId, subBlockConfig) | ||
| const extendedContext = context.blockId | ||
| ? extractExtendedContext(context.blockId, context.currentState) | ||
| : {} | ||
| const selectorCtx = context.blockId | ||
| ? extractSelectorContext(context.blockId, context.currentState, context.workflowId) | ||
| : { workflowId: context.workflowId } | ||
| // Credential fields (oauth-input or credential subBlockId) | ||
| const isCredentialField = | ||
| @@ -311,29 +255,10 @@ export async function resolveValueForDisplay( | ||
| // Selector types that require hydration (file-selector, sheet-selector, etc.) | ||
| // These support external service IDs like Google Drive file IDs | ||
| if (subBlockConfig && SELECTOR_TYPES_HYDRATION_REQUIRED.includes(subBlockConfig.type)) { | ||
| const resolution = resolveSelectorForSubBlock(subBlockConfig, { | ||
| workflowId: context.workflowId, | ||
| credentialId: extendedContext.credentialId, | ||
| domain: extendedContext.domain, | ||
| projectId: extendedContext.projectId, | ||
| planId: extendedContext.planId, | ||
| teamId: extendedContext.teamId, | ||
| knowledgeBaseId: extendedContext.knowledgeBaseId, | ||
| siteId: extendedContext.siteId, | ||
| collectionId: extendedContext.collectionId, | ||
| spreadsheetId: extendedContext.spreadsheetId, | ||
| baseId: extendedContext.baseId, | ||
| datasetId: extendedContext.datasetId, | ||
| serviceDeskId: extendedContext.serviceDeskId, | ||
| }) | ||
| const resolution = resolveSelectorForSubBlock(subBlockConfig, selectorCtx) | ||
| if (resolution?.key) { | ||
| const label = await resolveSelectorValue( | ||
| value, | ||
| resolution.key, | ||
| extendedContext, | ||
| context.workflowId | ||
| ) | ||
| const label = await resolveSelectorValue(value, resolution.key, selectorCtx) | ||
| if (label) { | ||
| return { original: value, displayLabel: label, resolved: true } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.