|
1 | 1 | import { createLogger } from '@sim/logger' |
| 2 | +import { buildSelectorContextFromBlock } from '@/lib/workflows/subblocks/context' |
2 | 3 | import { getBlock } from '@/blocks/registry' |
3 | 4 | import { SELECTOR_TYPES_HYDRATION_REQUIRED, type SubBlockConfig } from '@/blocks/types' |
4 | 5 | import { CREDENTIAL_SET, isUuid } from '@/executor/constants' |
5 | 6 | import { fetchCredentialSetById } from '@/hooks/queries/credential-sets' |
6 | 7 | import { fetchOAuthCredentialDetail } from '@/hooks/queries/oauth-credentials' |
7 | 8 | import { getSelectorDefinition } from '@/hooks/selectors/registry' |
8 | 9 | import { resolveSelectorForSubBlock } from '@/hooks/selectors/resolution' |
9 | | -import type { SelectorKey } from '@/hooks/selectors/types' |
| 10 | +import type { SelectorContext, SelectorKey } from '@/hooks/selectors/types' |
10 | 11 | import type { WorkflowState } from '@/stores/workflows/workflow/types' |
11 | 12 |
|
12 | 13 | const logger = createLogger('ResolveValues') |
@@ -39,24 +40,6 @@ interface ResolutionContext { |
39 | 40 | blockId?: string |
40 | 41 | } |
41 | 42 |
|
42 | | -/** |
43 | | - * Extended context extracted from block subBlocks for selector resolution |
44 | | - */ |
45 | | -interface ExtendedSelectorContext { |
46 | | - credentialId?: string |
47 | | - domain?: string |
48 | | - projectId?: string |
49 | | - planId?: string |
50 | | - teamId?: string |
51 | | - knowledgeBaseId?: string |
52 | | - siteId?: string |
53 | | - collectionId?: string |
54 | | - spreadsheetId?: string |
55 | | - baseId?: string |
56 | | - datasetId?: string |
57 | | - serviceDeskId?: string |
58 | | -} |
59 | | - |
60 | 43 | function getSemanticFallback(subBlockId: string, subBlockConfig?: SubBlockConfig): string { |
61 | 44 | if (subBlockConfig?.title) { |
62 | 45 | return subBlockConfig.title.toLowerCase() |
@@ -150,26 +133,10 @@ async function resolveWorkflow(workflowId: string): Promise<string | null> { |
150 | 133 | async function resolveSelectorValue( |
151 | 134 | value: string, |
152 | 135 | selectorKey: SelectorKey, |
153 | | - extendedContext: ExtendedSelectorContext, |
154 | | - workflowId: string |
| 136 | + selectorContext: SelectorContext |
155 | 137 | ): Promise<string | null> { |
156 | 138 | try { |
157 | 139 | const definition = getSelectorDefinition(selectorKey) |
158 | | - const selectorContext = { |
159 | | - workflowId, |
160 | | - credentialId: extendedContext.credentialId, |
161 | | - domain: extendedContext.domain, |
162 | | - projectId: extendedContext.projectId, |
163 | | - planId: extendedContext.planId, |
164 | | - teamId: extendedContext.teamId, |
165 | | - knowledgeBaseId: extendedContext.knowledgeBaseId, |
166 | | - siteId: extendedContext.siteId, |
167 | | - collectionId: extendedContext.collectionId, |
168 | | - spreadsheetId: extendedContext.spreadsheetId, |
169 | | - baseId: extendedContext.baseId, |
170 | | - datasetId: extendedContext.datasetId, |
171 | | - serviceDeskId: extendedContext.serviceDeskId, |
172 | | - } |
173 | 140 |
|
174 | 141 | if (definition.fetchById) { |
175 | 142 | const result = await definition.fetchById({ |
@@ -219,37 +186,14 @@ export function formatValueForDisplay(value: unknown): string { |
219 | 186 | return String(value) |
220 | 187 | } |
221 | 188 |
|
222 | | -/** |
223 | | - * Extracts extended context from a block's subBlocks for selector resolution. |
224 | | - * This mirrors the context extraction done in the UI components. |
225 | | - */ |
226 | | -function extractExtendedContext( |
| 189 | +function extractSelectorContext( |
227 | 190 | blockId: string, |
228 | | - currentState: WorkflowState |
229 | | -): ExtendedSelectorContext { |
| 191 | + currentState: WorkflowState, |
| 192 | + workflowId: string |
| 193 | +): SelectorContext { |
230 | 194 | const block = currentState.blocks?.[blockId] |
231 | | - if (!block?.subBlocks) return {} |
232 | | - |
233 | | - const getStringValue = (id: string): string | undefined => { |
234 | | - const subBlock = block.subBlocks[id] as { value?: unknown } | undefined |
235 | | - const val = subBlock?.value |
236 | | - return typeof val === 'string' ? val : undefined |
237 | | - } |
238 | | - |
239 | | - return { |
240 | | - credentialId: getStringValue('credential'), |
241 | | - domain: getStringValue('domain'), |
242 | | - projectId: getStringValue('projectId'), |
243 | | - planId: getStringValue('planId'), |
244 | | - teamId: getStringValue('teamId'), |
245 | | - knowledgeBaseId: getStringValue('knowledgeBaseId'), |
246 | | - siteId: getStringValue('siteId'), |
247 | | - collectionId: getStringValue('collectionId'), |
248 | | - spreadsheetId: getStringValue('spreadsheetId') || getStringValue('fileId'), |
249 | | - baseId: getStringValue('baseId') || getStringValue('baseSelector'), |
250 | | - datasetId: getStringValue('datasetId') || getStringValue('datasetSelector'), |
251 | | - serviceDeskId: getStringValue('serviceDeskId') || getStringValue('serviceDeskSelector'), |
252 | | - } |
| 195 | + if (!block?.subBlocks) return { workflowId } |
| 196 | + return buildSelectorContextFromBlock(block.type, block.subBlocks, { workflowId }) |
253 | 197 | } |
254 | 198 |
|
255 | 199 | /** |
@@ -277,9 +221,9 @@ export async function resolveValueForDisplay( |
277 | 221 | const subBlockConfig = blockConfig?.subBlocks.find((sb) => sb.id === context.subBlockId) |
278 | 222 | const semanticFallback = getSemanticFallback(context.subBlockId, subBlockConfig) |
279 | 223 |
|
280 | | - const extendedContext = context.blockId |
281 | | - ? extractExtendedContext(context.blockId, context.currentState) |
282 | | - : {} |
| 224 | + const selectorCtx = context.blockId |
| 225 | + ? extractSelectorContext(context.blockId, context.currentState, context.workflowId) |
| 226 | + : { workflowId: context.workflowId } |
283 | 227 |
|
284 | 228 | // Credential fields (oauth-input or credential subBlockId) |
285 | 229 | const isCredentialField = |
@@ -311,29 +255,10 @@ export async function resolveValueForDisplay( |
311 | 255 | // Selector types that require hydration (file-selector, sheet-selector, etc.) |
312 | 256 | // These support external service IDs like Google Drive file IDs |
313 | 257 | if (subBlockConfig && SELECTOR_TYPES_HYDRATION_REQUIRED.includes(subBlockConfig.type)) { |
314 | | - const resolution = resolveSelectorForSubBlock(subBlockConfig, { |
315 | | - workflowId: context.workflowId, |
316 | | - credentialId: extendedContext.credentialId, |
317 | | - domain: extendedContext.domain, |
318 | | - projectId: extendedContext.projectId, |
319 | | - planId: extendedContext.planId, |
320 | | - teamId: extendedContext.teamId, |
321 | | - knowledgeBaseId: extendedContext.knowledgeBaseId, |
322 | | - siteId: extendedContext.siteId, |
323 | | - collectionId: extendedContext.collectionId, |
324 | | - spreadsheetId: extendedContext.spreadsheetId, |
325 | | - baseId: extendedContext.baseId, |
326 | | - datasetId: extendedContext.datasetId, |
327 | | - serviceDeskId: extendedContext.serviceDeskId, |
328 | | - }) |
| 258 | + const resolution = resolveSelectorForSubBlock(subBlockConfig, selectorCtx) |
329 | 259 |
|
330 | 260 | if (resolution?.key) { |
331 | | - const label = await resolveSelectorValue( |
332 | | - value, |
333 | | - resolution.key, |
334 | | - extendedContext, |
335 | | - context.workflowId |
336 | | - ) |
| 261 | + const label = await resolveSelectorValue(value, resolution.key, selectorCtx) |
337 | 262 | if (label) { |
338 | 263 | return { original: value, displayLabel: label, resolved: true } |
339 | 264 | } |
|
0 commit comments