|
| 1 | +import { fetchWorkflowState } from '@/lib/mothership/tools/handlers/agent-cli/commands/workflow-views' |
| 2 | +import { |
| 3 | + type AgentCliCommand, |
| 4 | + agentCliFail, |
| 5 | + agentCliOk, |
| 6 | +} from '@/lib/mothership/tools/handlers/agent-cli/types' |
| 7 | +import { normalizeName, SPECIAL_REFERENCE_PREFIXES } from '@/executor/constants' |
| 8 | + |
| 9 | +/** |
| 10 | + * `workflow deps <workflowId> <blockId>` — everything one block consumes, so the |
| 11 | + * agent knows exactly what to mock before running it in isolation with |
| 12 | + * run_block's variableInputs. Tokens are extracted with the same shapes the |
| 13 | + * executor resolves (`<ref.path>` templates, `{{ENV}}` secrets) and block heads |
| 14 | + * are matched through the executor's own name normalization — this command must |
| 15 | + * never re-invent resolution semantics. |
| 16 | + */ |
| 17 | + |
| 18 | +const TEMPLATE_REF = /<([^<>]+)>/g |
| 19 | +const ENV_REF = /\{\{\s*([A-Za-z0-9_-]+)\s*\}\}/g |
| 20 | + |
| 21 | +interface DepView { |
| 22 | + token: string |
| 23 | + kind: 'block' | 'loop' | 'parallel' | 'variable' | 'env' | 'unknown' |
| 24 | + blockId?: string |
| 25 | + blockName?: string |
| 26 | + paths?: string[] |
| 27 | +} |
| 28 | + |
| 29 | +function stringLeaves(value: unknown, out: string[]): void { |
| 30 | + if (typeof value === 'string') out.push(value) |
| 31 | + else if (Array.isArray(value)) for (const item of value) stringLeaves(item, out) |
| 32 | + else if (typeof value === 'object' && value !== null) |
| 33 | + for (const item of Object.values(value)) stringLeaves(item, out) |
| 34 | +} |
| 35 | + |
| 36 | +export const workflowDepsCommand: AgentCliCommand = { |
| 37 | + path: ['workflow', 'deps'], |
| 38 | + summary: |
| 39 | + 'List every reference one block consumes (upstream blocks, variables, env) — what to mock for an isolated run', |
| 40 | + usage: 'workflow deps <workflowId> <blockId>', |
| 41 | + async execute(rest, runtime) { |
| 42 | + const [workflowId, blockId] = rest |
| 43 | + if (!workflowId || !blockId) |
| 44 | + return agentCliFail('Usage: sim workflow deps <workflowId> <blockId>') |
| 45 | + const state = await fetchWorkflowState(runtime, workflowId) |
| 46 | + const blocks = (state.blocks ?? {}) as Record<string, Record<string, unknown>> |
| 47 | + const block = blocks[blockId] |
| 48 | + if (!block) return agentCliFail(`No block ${blockId} in workflow ${workflowId}`) |
| 49 | + |
| 50 | + const nameToId = new Map<string, string>() |
| 51 | + const idToName = new Map<string, string>() |
| 52 | + for (const [id, raw] of Object.entries(blocks)) { |
| 53 | + const name = typeof raw.name === 'string' ? raw.name : undefined |
| 54 | + if (name) { |
| 55 | + nameToId.set(normalizeName(name), id) |
| 56 | + idToName.set(id, name) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + const leaves: string[] = [] |
| 61 | + stringLeaves(block.subBlocks ?? block, leaves) |
| 62 | + |
| 63 | + const byToken = new Map<string, DepView>() |
| 64 | + const envs = new Set<string>() |
| 65 | + for (const leaf of leaves) { |
| 66 | + for (const match of leaf.matchAll(TEMPLATE_REF)) { |
| 67 | + const token = match[1] |
| 68 | + if (!token || byToken.has(token)) continue |
| 69 | + const [head = '', ...pathParts] = token.split('.') |
| 70 | + const path = pathParts.join('.') |
| 71 | + const special = (SPECIAL_REFERENCE_PREFIXES as readonly string[]).includes(head) |
| 72 | + if (special) { |
| 73 | + byToken.set(token, { token, kind: head as 'loop' | 'parallel' | 'variable' }) |
| 74 | + continue |
| 75 | + } |
| 76 | + const refBlockId = blocks[head] ? head : nameToId.get(normalizeName(head)) |
| 77 | + if (refBlockId && refBlockId !== blockId) { |
| 78 | + const existing = [...byToken.values()].find((d) => d.blockId === refBlockId) |
| 79 | + if (existing) { |
| 80 | + if (path && !existing.paths?.includes(path)) existing.paths?.push(path) |
| 81 | + byToken.set(token, existing) |
| 82 | + } else { |
| 83 | + byToken.set(token, { |
| 84 | + token, |
| 85 | + kind: 'block', |
| 86 | + blockId: refBlockId, |
| 87 | + blockName: idToName.get(refBlockId), |
| 88 | + paths: path ? [path] : [], |
| 89 | + }) |
| 90 | + } |
| 91 | + } else if (!refBlockId) { |
| 92 | + byToken.set(token, { token, kind: 'unknown' }) |
| 93 | + } |
| 94 | + } |
| 95 | + for (const match of leaf.matchAll(ENV_REF)) { |
| 96 | + if (match[1]) envs.add(match[1]) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + const deps = [...new Set(byToken.values())] |
| 101 | + const blockDeps = deps.filter((d) => d.kind === 'block') |
| 102 | + return agentCliOk( |
| 103 | + JSON.stringify( |
| 104 | + { |
| 105 | + blockId, |
| 106 | + blockName: idToName.get(blockId), |
| 107 | + references: deps, |
| 108 | + env: [...envs].sort(), |
| 109 | + // Ready-made skeleton for run_block's variableInputs: mock each upstream |
| 110 | + // block's output at the paths this block actually reads. |
| 111 | + mock: Object.fromEntries( |
| 112 | + blockDeps.map((d) => [ |
| 113 | + d.blockName ?? d.blockId, |
| 114 | + d.paths?.length ? d.paths : ['<output>'], |
| 115 | + ]) |
| 116 | + ), |
| 117 | + }, |
| 118 | + null, |
| 119 | + 2 |
| 120 | + ) |
| 121 | + ) |
| 122 | + }, |
| 123 | +} |
0 commit comments