(opts: {
runtime: RuntimeKind;
ctx: PredicateContext;
enabledWorkflows?: string[];
excludeWorkflows?: string[];
})
| 109 | * Build a tool catalog from the YAML manifest system. |
| 110 | */ |
| 111 | export async function buildToolCatalogFromManifest(opts: { |
| 112 | runtime: RuntimeKind; |
| 113 | ctx: PredicateContext; |
| 114 | enabledWorkflows?: string[]; |
| 115 | excludeWorkflows?: string[]; |
| 116 | }): Promise<ToolCatalog> { |
| 117 | const manifest = loadManifest(); |
| 118 | const excludeSet = new Set(opts.excludeWorkflows?.map((w) => w.toLowerCase()) ?? []); |
| 119 | |
| 120 | // Get workflows to include |
| 121 | let workflowsToInclude: WorkflowManifestEntry[]; |
| 122 | if (opts.enabledWorkflows && opts.enabledWorkflows.length > 0) { |
| 123 | // Use specified workflows |
| 124 | workflowsToInclude = opts.enabledWorkflows |
| 125 | .map((id) => manifest.workflows.get(id)) |
| 126 | .filter((wf): wf is WorkflowManifestEntry => wf !== undefined); |
| 127 | } else { |
| 128 | // Use all workflows available for the runtime |
| 129 | workflowsToInclude = Array.from(manifest.workflows.values()); |
| 130 | } |
| 131 | |
| 132 | const filteredWorkflows = workflowsToInclude.filter( |
| 133 | (wf) => |
| 134 | !excludeSet.has(wf.id.toLowerCase()) && |
| 135 | isWorkflowAvailableForRuntime(wf, opts.runtime) && |
| 136 | isWorkflowEnabledForRuntime(wf, opts.ctx), |
| 137 | ); |
| 138 | |
| 139 | // Cache imported modules to avoid re-importing the same tool |
| 140 | const moduleCache = new Map<string, Awaited<ReturnType<typeof importToolModule>>>(); |
| 141 | const tools: ToolDefinition[] = []; |
| 142 | |
| 143 | for (const workflow of filteredWorkflows) { |
| 144 | for (const toolId of workflow.tools) { |
| 145 | const toolManifest = manifest.tools.get(toolId); |
| 146 | if (!toolManifest) continue; |
| 147 | |
| 148 | // Check tool availability for runtime |
| 149 | if (!isToolAvailableForRuntime(toolManifest, opts.runtime)) continue; |
| 150 | |
| 151 | // Check tool predicates |
| 152 | if (!isToolExposedForRuntime(toolManifest, opts.ctx)) continue; |
| 153 | |
| 154 | // Import the tool module (cached) |
| 155 | let toolModule = moduleCache.get(toolId); |
| 156 | if (!toolModule) { |
| 157 | try { |
| 158 | toolModule = await importToolModule(toolManifest.module); |
| 159 | moduleCache.set(toolId, toolModule); |
| 160 | } catch (err) { |
| 161 | log('warn', `Failed to import tool module ${toolManifest.module}: ${err}`); |
| 162 | continue; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | const cliName = getEffectiveCliName(toolManifest); |
| 167 | tools.push({ |
| 168 | id: toolManifest.id, |
no test coverage detected