|
1 | 1 | import { requirePrincipalSubjectUserId } from '@sim/auth/principal' |
2 | 2 | import { getErrorMessage } from '@sim/utils/errors' |
| 3 | +import { generateShortId } from '@sim/utils/id' |
3 | 4 | import { isPlainRecord } from '@sim/utils/object' |
4 | 5 | import { findWorkflowReferenceTokens } from '@sim/utils/workflow-references' |
5 | 6 | import type { CursorKey } from '@/lib/api/list-query' |
@@ -258,74 +259,89 @@ function hasRuntimeReference(value: string): boolean { |
258 | 259 | /** |
259 | 260 | * Outside Function code a reference resolves to JSON text (objects, arrays, numbers, booleans), so |
260 | 261 | * `{"name": <start.name>}` or a whole-field `<start.row>` is valid JSON at runtime but not before. |
261 | | - * Each reference written outside a JSON string is replaced by a placeholder so the Table block's |
262 | | - * own transform reads the literal parts: a quoted string, or a one-item list for slots such as a |
263 | | - * filter `in`, trying every combination. A field that is a single whole reference has no literal |
264 | | - * part, so if no combination fits it is left out and returned in `runtimeFields`. When the search |
265 | | - * is exhaustive and still fails, the error comes from the literal parts (malformed JSON, an |
266 | | - * invalid literal condition) and is rethrown. Past the search bound that cannot be told apart, so |
267 | | - * every reference-bearing field is left out instead and the other inputs are still checked. Only |
268 | | - * this lint-local copy is rewritten; references to string outputs left unquoted are reported by |
269 | | - * `collectUnquotedJsonStringReferences`. |
| 262 | + * Each reference written outside a JSON string becomes a quoted placeholder so the Table block's |
| 263 | + * own transform reads the literal parts (see {@link withReferencePlaceholders}). If the transform |
| 264 | + * still fails and a field has no literal part, that field is left out, returned in |
| 265 | + * `runtimeFields`, and the transform runs once more; an error from that final attempt comes from |
| 266 | + * the literal parts and is rethrown. Only this lint-local copy is rewritten; references to string |
| 267 | + * outputs left unquoted are reported by `collectUnquotedJsonStringReferences`. |
270 | 268 | */ |
271 | 269 | function normalizeTableInputs( |
272 | 270 | transform: (params: Record<string, unknown>) => Record<string, unknown>, |
273 | 271 | params: Record<string, unknown> |
274 | 272 | ): { normalized: Record<string, unknown>; runtimeFields: string[] } { |
275 | | - const referenceFields: string[] = [] |
276 | | - const wholeReferenceFields: string[] = [] |
| 273 | + const prepared: Record<string, unknown> = {} |
| 274 | + const runtimeFields: string[] = [] |
277 | 275 | for (const [key, value] of Object.entries(params)) { |
278 | | - if (typeof value !== 'string') continue |
279 | | - const trimmed = value.trim() |
280 | | - const tokens = findWorkflowReferenceTokens(trimmed) |
281 | | - if (tokens.length === 0) continue |
282 | | - referenceFields.push(key) |
283 | | - if (tokens.length === 1 && tokens[0].start === 0 && tokens[0].end === trimmed.length) { |
284 | | - wholeReferenceFields.push(key) |
285 | | - } |
286 | | - } |
287 | | - if (referenceFields.length === 0) return { normalized: transform(params), runtimeFields: [] } |
288 | | - const tiers = wholeReferenceFields.length > 0 ? [[], wholeReferenceFields] : [[]] |
289 | | - let searchBounded = false |
290 | | - let firstError: unknown |
291 | | - for (const runtimeFields of tiers) { |
292 | | - const withPlaceholders = (asList: (ordinal: number) => boolean) => { |
293 | | - let ordinal = 0 |
294 | | - const candidate: Record<string, unknown> = {} |
295 | | - for (const [key, value] of Object.entries(params)) { |
296 | | - if (runtimeFields.includes(key)) continue |
297 | | - candidate[key] = |
298 | | - typeof value === 'string' |
299 | | - ? replaceBareReferences(value, (reference) => |
300 | | - asList(ordinal++) ? `[${JSON.stringify(reference)}]` : JSON.stringify(reference) |
301 | | - ) |
302 | | - : value |
303 | | - } |
304 | | - return { candidate, count: ordinal } |
305 | | - } |
306 | | - const { count } = withPlaceholders(() => false) |
307 | | - if (count > MAX_PLACEHOLDER_SEARCH_REFERENCES) { |
308 | | - searchBounded = true |
| 276 | + if (typeof value !== 'string') { |
| 277 | + prepared[key] = value |
309 | 278 | continue |
310 | 279 | } |
311 | | - for (let mask = 0; mask < 2 ** count; mask++) { |
312 | | - try { |
313 | | - const { candidate } = withPlaceholders((ordinal) => ((mask >> ordinal) & 1) === 1) |
314 | | - return { normalized: transform(candidate), runtimeFields } |
315 | | - } catch (error) { |
316 | | - firstError ??= error |
317 | | - } |
318 | | - } |
| 280 | + const input = withReferencePlaceholders(value) |
| 281 | + prepared[key] = input.value |
| 282 | + if (input.runtimeOnly) runtimeFields.push(key) |
| 283 | + } |
| 284 | + try { |
| 285 | + return { normalized: transform(prepared), runtimeFields: [] } |
| 286 | + } catch (error) { |
| 287 | + if (runtimeFields.length === 0) throw error |
319 | 288 | } |
320 | | - if (!searchBounded) throw firstError |
321 | | - const literal = Object.fromEntries( |
322 | | - Object.entries(params).filter(([key]) => !referenceFields.includes(key)) |
323 | | - ) |
324 | | - return { normalized: transform(literal), runtimeFields: referenceFields } |
| 289 | + for (const key of runtimeFields) delete prepared[key] |
| 290 | + return { normalized: transform(prepared), runtimeFields } |
325 | 291 | } |
326 | 292 |
|
327 | | -/** Bounds the placeholder search to 2^6 pure transform calls per Table block. */ |
328 | | -const MAX_PLACEHOLDER_SEARCH_REFERENCES = 6 |
| 293 | +/** |
| 294 | + * Quotes each bare reference occurrence under its own placeholder, so an already-quoted string |
| 295 | + * with the same text is never mistaken for one. A quoted reference fits row values and most |
| 296 | + * filter values; the value of an `in`/`nin` condition must be a list, so it becomes a one-item |
| 297 | + * list, and a condition or group made only of references has no literal part, so it is left out |
| 298 | + * of an `all`/`any` group that keeps a literal member. `runtimeOnly` marks a field whose whole |
| 299 | + * value is such a reference or group. |
| 300 | + */ |
| 301 | +function withReferencePlaceholders(json: string): { value: string; runtimeOnly: boolean } { |
| 302 | + const nonce = generateShortId() |
| 303 | + const references = new Map<string, string>() |
| 304 | + const quoted = replaceBareReferences(json, (reference) => { |
| 305 | + const placeholder = `${nonce}:${references.size}` |
| 306 | + references.set(placeholder, reference) |
| 307 | + return JSON.stringify(placeholder) |
| 308 | + }) |
| 309 | + if (references.size === 0) return { value: json, runtimeOnly: false } |
| 310 | + let parsed: unknown |
| 311 | + try { |
| 312 | + parsed = JSON.parse(quoted) |
| 313 | + } catch { |
| 314 | + const readable = replaceBareReferences(json, (reference) => JSON.stringify(reference)) |
| 315 | + return { value: readable, runtimeOnly: false } |
| 316 | + } |
| 317 | + const isPlaceholder = (node: unknown): node is string => |
| 318 | + typeof node === 'string' && references.has(node) |
| 319 | + const isDynamic = (node: unknown): boolean => { |
| 320 | + if (isPlaceholder(node)) return true |
| 321 | + if (!isPlainRecord(node) || Object.keys(node).length !== 1) return false |
| 322 | + const members = node.all ?? node.any |
| 323 | + return Array.isArray(members) && members.length > 0 && members.every(isDynamic) |
| 324 | + } |
| 325 | + const adapt = (node: unknown): unknown => { |
| 326 | + if (isPlaceholder(node)) return references.get(node) |
| 327 | + if (Array.isArray(node)) return node.map(adapt) |
| 328 | + if (!isPlainRecord(node)) return node |
| 329 | + return Object.fromEntries( |
| 330 | + Object.entries(node).map(([key, value]) => { |
| 331 | + const name = references.get(key) ?? key |
| 332 | + if ((key === 'all' || key === 'any') && Array.isArray(value)) { |
| 333 | + const literal = value.filter((member) => !isDynamic(member)) |
| 334 | + return [name, adapt(literal.length > 0 ? literal : value)] |
| 335 | + } |
| 336 | + if (key === 'value' && (node.op === 'in' || node.op === 'nin') && isPlaceholder(value)) { |
| 337 | + return [name, [adapt(value)]] |
| 338 | + } |
| 339 | + return [name, adapt(value)] |
| 340 | + }) |
| 341 | + ) |
| 342 | + } |
| 343 | + return { value: JSON.stringify(adapt(parsed)), runtimeOnly: isDynamic(parsed) } |
| 344 | +} |
329 | 345 |
|
330 | 346 | function replaceBareReferences(json: string, placeholder: (reference: string) => string): string { |
331 | 347 | const tokens = findWorkflowReferenceTokens(json) |
@@ -411,12 +427,14 @@ async function readTableDiagnostics( |
411 | 427 | const transform = getBlock(block.type)?.tools.config?.params |
412 | 428 | if (!transform) throw new Error('Table input normalization is unavailable') |
413 | 429 | let normalized: Record<string, unknown> |
| 430 | + let runtimeFields: string[] |
414 | 431 | try { |
415 | 432 | const inputs = normalizeTableInputs(transform, params) |
416 | 433 | normalized = inputs.normalized |
417 | | - if (inputs.runtimeFields.length > 0) { |
| 434 | + runtimeFields = inputs.runtimeFields |
| 435 | + if (runtimeFields.length > 0) { |
418 | 436 | result.notes.push( |
419 | | - `Table inputs in block "${block.name || blockId}" depend on references resolved at runtime and were not checked: ${inputs.runtimeFields.join(', ')}.` |
| 437 | + `Table inputs in block "${block.name || blockId}" depend on references resolved at runtime and were not checked: ${runtimeFields.join(', ')}.` |
420 | 438 | ) |
421 | 439 | } |
422 | 440 | } catch (error) { |
@@ -464,8 +482,10 @@ async function readTableDiagnostics( |
464 | 482 | }) |
465 | 483 | } |
466 | 484 | } |
467 | | - if (Object.hasOwn(normalized, 'data')) inspectRow(normalized.data, 'data') |
468 | | - if (Object.hasOwn(normalized, 'rows')) { |
| 485 | + if (Object.hasOwn(normalized, 'data') && !runtimeFields.includes('data')) { |
| 486 | + inspectRow(normalized.data, 'data') |
| 487 | + } |
| 488 | + if (Object.hasOwn(normalized, 'rows') && !runtimeFields.includes('rows')) { |
469 | 489 | if (Array.isArray(normalized.rows)) { |
470 | 490 | normalized.rows.forEach((row, index) => inspectRow(row, `rows[${index}]`)) |
471 | 491 | } else { |
|
0 commit comments