MCPcopy Index your code
hub / github.com/simstudioai/sim / selectBestFromGroup

Function selectBestFromGroup

apps/sim/lib/workflows/triggers/trigger-utils.ts:336–377  ·  view source on GitHub ↗

* Selects the best trigger from a group based on priority

(
  candidates: StartBlockCandidate<T>[]
)

Source from the content-addressed store, hash-verified

334 * Selects the best trigger from a group based on priority
335 */
336function selectBestFromGroup<T extends { type: string; subBlocks?: Record<string, unknown> }>(
337 candidates: StartBlockCandidate<T>[]
338): StartBlockCandidate<T> {
339 if (candidates.length === 1) {
340 return candidates[0]
341 }
342
343 // Sort by priority (lower number = higher priority)
344 const sorted = [...candidates].sort((a, b) => {
345 const getPriority = (trigger: StartBlockCandidate<T>): number => {
346 // Start block - highest priority
347 if (trigger.path === StartBlockPath.UNIFIED) return 0
348 if (trigger.path === StartBlockPath.LEGACY_STARTER) return 1
349
350 // For external triggers, differentiate schedules from webhooks
351 if (trigger.path === StartBlockPath.EXTERNAL_TRIGGER) {
352 if (trigger.block.type === 'schedule') return 2
353 return 3 // Webhooks and other external triggers
354 }
355
356 // Other trigger types
357 if (trigger.path === StartBlockPath.SPLIT_API) return 4
358 if (trigger.path === StartBlockPath.SPLIT_INPUT) return 5
359 if (trigger.path === StartBlockPath.SPLIT_MANUAL) return 6
360 if (trigger.path === StartBlockPath.SPLIT_CHAT) return 7
361
362 return 99 // Unknown
363 }
364
365 return getPriority(a) - getPriority(b)
366 })
367
368 const selected = sorted[0]
369 logger.info('Selected best trigger from group', {
370 selectedId: selected.blockId,
371 selectedType: selected.block.type,
372 selectedPath: selected.path,
373 groupSize: candidates.length,
374 })
375
376 return selected
377}
378
379/**
380 * Checks if a trigger needs mock payload (external triggers/webhooks, but not schedules)

Callers 1

selectBestTriggerFunction · 0.85

Calls 2

getPriorityFunction · 0.85
infoMethod · 0.80

Tested by

no test coverage detected