|
| 1 | +import { toError } from '@sim/utils/errors' |
| 2 | +import { |
| 3 | + isLargeArrayManifest, |
| 4 | + LARGE_ARRAY_MANIFEST_MARKER, |
| 5 | + materializeLargeArrayManifest, |
| 6 | +} from '@/lib/execution/payloads/large-array-manifest' |
| 7 | +import { isLargeValueRef, LARGE_VALUE_REF_MARKER } from '@/lib/execution/payloads/large-value-ref' |
| 8 | +import { MAX_DURABLE_LARGE_VALUE_BYTES } from '@/lib/execution/payloads/materialization.server' |
| 9 | +import { materializeLargeValueRef } from '@/lib/execution/payloads/store' |
| 10 | +import { REFERENCE } from '@/executor/constants' |
| 11 | +import type { ExecutionContext } from '@/executor/types' |
| 12 | +import type { VariableResolver } from '@/executor/variables/resolver' |
| 13 | + |
| 14 | +async function normalizeCollectionValue(ctx: ExecutionContext, value: unknown): Promise<any[]> { |
| 15 | + if (Array.isArray(value)) { |
| 16 | + return value |
| 17 | + } |
| 18 | + |
| 19 | + if (isLargeArrayManifest(value)) { |
| 20 | + return materializeLargeArrayManifest(value, { |
| 21 | + workspaceId: ctx.workspaceId, |
| 22 | + workflowId: ctx.workflowId, |
| 23 | + executionId: ctx.executionId, |
| 24 | + largeValueExecutionIds: ctx.largeValueExecutionIds, |
| 25 | + allowLargeValueWorkflowScope: ctx.allowLargeValueWorkflowScope, |
| 26 | + userId: ctx.userId, |
| 27 | + maxBytes: MAX_DURABLE_LARGE_VALUE_BYTES, |
| 28 | + }) |
| 29 | + } |
| 30 | + |
| 31 | + if (isLargeValueRef(value)) { |
| 32 | + const materialized = await materializeLargeValueRef(value, { |
| 33 | + workspaceId: ctx.workspaceId, |
| 34 | + workflowId: ctx.workflowId, |
| 35 | + executionId: ctx.executionId, |
| 36 | + largeValueExecutionIds: ctx.largeValueExecutionIds, |
| 37 | + allowLargeValueWorkflowScope: ctx.allowLargeValueWorkflowScope, |
| 38 | + userId: ctx.userId, |
| 39 | + maxBytes: MAX_DURABLE_LARGE_VALUE_BYTES, |
| 40 | + }) |
| 41 | + if (materialized === undefined) { |
| 42 | + throw new Error('Large execution value is unavailable.') |
| 43 | + } |
| 44 | + return normalizeCollectionValue(ctx, materialized) |
| 45 | + } |
| 46 | + |
| 47 | + if (typeof value === 'object' && value !== null) { |
| 48 | + if ((value as Record<string, unknown>)[LARGE_ARRAY_MANIFEST_MARKER] === true) { |
| 49 | + throw new Error('Invalid large array manifest.') |
| 50 | + } |
| 51 | + if ((value as Record<string, unknown>)[LARGE_VALUE_REF_MARKER] === true) { |
| 52 | + throw new Error('Invalid large value ref.') |
| 53 | + } |
| 54 | + return Object.entries(value) |
| 55 | + } |
| 56 | + |
| 57 | + if (value === null) { |
| 58 | + return [] |
| 59 | + } |
| 60 | + |
| 61 | + throw new Error('Value did not resolve to an array or object') |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Resolves loop/parallel collection inputs on the server, including durable |
| 66 | + * execution values that cannot be imported into client-reachable utilities. |
| 67 | + */ |
| 68 | +export async function resolveArrayInputAsync( |
| 69 | + ctx: ExecutionContext, |
| 70 | + items: any, |
| 71 | + resolver: VariableResolver | null |
| 72 | +): Promise<any[]> { |
| 73 | + if (typeof items !== 'string') { |
| 74 | + if (items === null) { |
| 75 | + return [] |
| 76 | + } |
| 77 | + if (!Array.isArray(items) && typeof items !== 'object') { |
| 78 | + if (!resolver) { |
| 79 | + return [] |
| 80 | + } |
| 81 | + try { |
| 82 | + const resolved = (await resolver.resolveInputs(ctx, 'subflow_items', { items })).items |
| 83 | + return normalizeCollectionValue(ctx, resolved) |
| 84 | + } catch (error) { |
| 85 | + if (error instanceof Error && error.message.startsWith('Resolved items')) { |
| 86 | + throw error |
| 87 | + } |
| 88 | + throw new Error(`Failed to resolve items: ${toError(error).message}`) |
| 89 | + } |
| 90 | + } |
| 91 | + return normalizeCollectionValue(ctx, items) |
| 92 | + } |
| 93 | + |
| 94 | + if (items.startsWith(REFERENCE.START) && items.endsWith(REFERENCE.END) && resolver) { |
| 95 | + try { |
| 96 | + const resolved = await resolver.resolveSingleReference(ctx, '', items, undefined, { |
| 97 | + allowLargeValueRefs: true, |
| 98 | + }) |
| 99 | + return normalizeCollectionValue(ctx, resolved) |
| 100 | + } catch (error) { |
| 101 | + if (error instanceof Error && error.message.startsWith('Reference "')) { |
| 102 | + throw error |
| 103 | + } |
| 104 | + throw new Error(`Failed to resolve reference "${items}": ${toError(error).message}`) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + try { |
| 109 | + const normalized = items.replace(/'/g, '"') |
| 110 | + const parsed = JSON.parse(normalized) |
| 111 | + return normalizeCollectionValue(ctx, parsed) |
| 112 | + } catch (error) { |
| 113 | + if (error instanceof Error && error.message.startsWith('Parsed value')) { |
| 114 | + throw error |
| 115 | + } |
| 116 | + throw new Error(`Failed to parse items as JSON: "${items}"`) |
| 117 | + } |
| 118 | +} |
0 commit comments