|
1 | 1 | import { ConfigIO } from '../../lib'; |
2 | 2 | import type { AgentCoreProjectSpec, AwsDeploymentTargets, DeployedState } from '../../schema'; |
| 3 | +import { getHarness } from '../aws/agentcore-harness'; |
3 | 4 |
|
4 | 5 | export interface DeployedProjectConfig { |
5 | 6 | project: AgentCoreProjectSpec; |
@@ -97,3 +98,126 @@ export function resolveAgent( |
97 | 98 | }, |
98 | 99 | }; |
99 | 100 | } |
| 101 | + |
| 102 | +/** |
| 103 | + * Resolves a harness to a ResolvedAgent by looking up deployed state and |
| 104 | + * fetching the underlying agentRuntimeArn via the GetHarness API. |
| 105 | + */ |
| 106 | +export async function resolveHarness( |
| 107 | + context: DeployedProjectConfig, |
| 108 | + harnessName: string |
| 109 | +): Promise<{ success: true; agent: ResolvedAgent } | { success: false; error: string }> { |
| 110 | + const { project, deployedState, awsTargets } = context; |
| 111 | + |
| 112 | + const harnesses = project.harnesses ?? []; |
| 113 | + const harnessSpec = harnesses.find(h => h.name === harnessName); |
| 114 | + if (!harnessSpec) { |
| 115 | + const available = harnesses.map(h => h.name); |
| 116 | + return { |
| 117 | + success: false, |
| 118 | + error: |
| 119 | + available.length > 0 |
| 120 | + ? `Harness '${harnessName}' not found. Available: ${available.join(', ')}` |
| 121 | + : 'No harnesses defined in agentcore.json', |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + const targetNames = Object.keys(deployedState.targets); |
| 126 | + if (targetNames.length === 0) { |
| 127 | + return { success: false, error: 'No deployed targets found. Run `agentcore deploy` first.' }; |
| 128 | + } |
| 129 | + const selectedTargetName = targetNames[0]!; |
| 130 | + |
| 131 | + const targetState = deployedState.targets[selectedTargetName]; |
| 132 | + const targetConfig = awsTargets.find(t => t.name === selectedTargetName); |
| 133 | + |
| 134 | + if (!targetConfig) { |
| 135 | + return { success: false, error: `Target config '${selectedTargetName}' not found in aws-targets` }; |
| 136 | + } |
| 137 | + |
| 138 | + const harnessState = targetState?.resources?.harnesses?.[harnessName]; |
| 139 | + if (!harnessState) { |
| 140 | + return { |
| 141 | + success: false, |
| 142 | + error: `Harness '${harnessName}' is not deployed to target '${selectedTargetName}'. Run 'agentcore deploy' first.`, |
| 143 | + }; |
| 144 | + } |
| 145 | + |
| 146 | + let runtimeId: string | undefined; |
| 147 | + |
| 148 | + if (harnessState.agentRuntimeArn) { |
| 149 | + const arnMatch = /runtime\/([^/]+)/.exec(harnessState.agentRuntimeArn); |
| 150 | + if (arnMatch) { |
| 151 | + runtimeId = arnMatch[1]; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (!runtimeId) { |
| 156 | + try { |
| 157 | + await getHarness({ region: targetConfig.region, harnessId: harnessState.harnessId }); |
| 158 | + runtimeId = harnessState.harnessId; |
| 159 | + } catch (err) { |
| 160 | + return { |
| 161 | + success: false, |
| 162 | + error: `Failed to resolve runtime for harness '${harnessName}': ${(err as Error).message}`, |
| 163 | + }; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if (!runtimeId) { |
| 168 | + return { |
| 169 | + success: false, |
| 170 | + error: `Could not resolve runtime ID for harness '${harnessName}'. Re-deploy to populate agentRuntimeArn.`, |
| 171 | + }; |
| 172 | + } |
| 173 | + |
| 174 | + return { |
| 175 | + success: true, |
| 176 | + agent: { |
| 177 | + agentName: harnessName, |
| 178 | + targetName: selectedTargetName, |
| 179 | + region: targetConfig.region, |
| 180 | + accountId: targetConfig.account, |
| 181 | + runtimeId, |
| 182 | + }, |
| 183 | + }; |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Resolves either an agent runtime or a harness to a ResolvedAgent. |
| 188 | + * - If --harness is specified, resolves that harness. |
| 189 | + * - If --runtime is specified, resolves that runtime. |
| 190 | + * - If neither is specified, auto-selects: single runtime wins, or if no runtimes |
| 191 | + * but harnesses exist, auto-selects the single harness. |
| 192 | + */ |
| 193 | +export async function resolveAgentOrHarness( |
| 194 | + context: DeployedProjectConfig, |
| 195 | + options: { runtime?: string; harness?: string } |
| 196 | +): Promise<{ success: true; agent: ResolvedAgent } | { success: false; error: string }> { |
| 197 | + if (options.harness && options.runtime) { |
| 198 | + return { success: false, error: 'Cannot specify both --harness and --runtime' }; |
| 199 | + } |
| 200 | + |
| 201 | + if (options.harness) { |
| 202 | + return resolveHarness(context, options.harness); |
| 203 | + } |
| 204 | + |
| 205 | + if (options.runtime || context.project.runtimes.length > 0) { |
| 206 | + return resolveAgent(context, options); |
| 207 | + } |
| 208 | + |
| 209 | + const harnesses = context.project.harnesses ?? []; |
| 210 | + if (harnesses.length === 0) { |
| 211 | + return { success: false, error: 'No runtimes or harnesses defined in agentcore.json' }; |
| 212 | + } |
| 213 | + |
| 214 | + if (harnesses.length > 1) { |
| 215 | + const names = harnesses.map(h => h.name); |
| 216 | + return { |
| 217 | + success: false, |
| 218 | + error: `Multiple harnesses found. Use --harness to specify one: ${names.join(', ')}`, |
| 219 | + }; |
| 220 | + } |
| 221 | + |
| 222 | + return resolveHarness(context, harnesses[0]!.name); |
| 223 | +} |
0 commit comments