11import type { OnlineEvalConfig } from '../../../schema' ;
22import type { GetOnlineEvalConfigResult , OnlineEvalConfigSummary } from '../../aws/agentcore-control' ;
3- import { getOnlineEvaluationConfig , listAllOnlineEvaluationConfigs } from '../../aws/agentcore-control' ;
3+ import {
4+ getOnlineEvaluationConfig ,
5+ listAllAgentRuntimes ,
6+ listAllOnlineEvaluationConfigs ,
7+ } from '../../aws/agentcore-control' ;
48import { ANSI } from './constants' ;
5- import { failResult } from './import-utils' ;
9+ import { failResult , findResourceInDeployedState } from './import-utils' ;
610import { executeResourceImport } from './resource-import' ;
711import type { ImportResourceOptions , ImportResourceResult , ResourceImportDescriptor } from './types' ;
812import type { Command } from '@commander-js/extra-typings' ;
@@ -97,13 +101,12 @@ function createOnlineEvalDescriptor(): ResourceImportDescriptor<GetOnlineEvalCon
97101
98102 buildDeployedStateEntry : ( name , id , d ) => ( { type : 'online-eval' , name, id, arn : d . configArn } ) ,
99103
100- // eslint-disable-next-line @typescript-eslint/require-await -- interface requires Promise return type
101- beforeConfigWrite : async ( { detail, localName, projectSpec, target, onProgress, logger } ) => {
104+ beforeConfigWrite : async ( { detail, localName, projectSpec, ctx, target, onProgress, logger } ) => {
102105 logger . startStep ( 'Resolve references' ) ;
103106
104107 // Extract agent name from service names
105- const agentName = extractAgentName ( detail . serviceNames ?? [ ] ) ;
106- if ( ! agentName ) {
108+ const awsAgentName = extractAgentName ( detail . serviceNames ?? [ ] ) ;
109+ if ( ! awsAgentName ) {
107110 return failResult (
108111 logger ,
109112 'Could not determine agent name from online eval config. The config has no data source service names.' ,
@@ -112,12 +115,40 @@ function createOnlineEvalDescriptor(): ResourceImportDescriptor<GetOnlineEvalCon
112115 ) ;
113116 }
114117
115- // Validate agent exists in project
118+ // Resolve the local agent name. The AWS name from the OEC service names
119+ // may differ from the local name if the runtime was imported with --name.
116120 const agentNames = new Set ( ( projectSpec . runtimes ?? [ ] ) . map ( r => r . name ) ) ;
117- if ( ! agentNames . has ( agentName ) ) {
121+ let agentName : string | undefined ;
122+
123+ if ( agentNames . has ( awsAgentName ) ) {
124+ // Direct match — local name equals AWS name
125+ agentName = awsAgentName ;
126+ } else {
127+ // Look up the AWS runtime ID for the AWS name, then find the local name
128+ // that maps to it in deployed state.
129+ onProgress ( `Agent "${ awsAgentName } " not found by name, checking deployed state...` ) ;
130+ const runtimes = await listAllAgentRuntimes ( { region : target . region } ) ;
131+ const matchingRuntime = runtimes . find ( r => r . agentRuntimeName === awsAgentName ) ;
132+
133+ if ( matchingRuntime ) {
134+ const targetName = target . name ?? 'default' ;
135+ const localMatch = await findResourceInDeployedState (
136+ ctx . configIO ,
137+ targetName ,
138+ 'runtime' ,
139+ matchingRuntime . agentRuntimeId
140+ ) ;
141+ if ( localMatch && agentNames . has ( localMatch ) ) {
142+ agentName = localMatch ;
143+ onProgress ( `Resolved AWS runtime "${ awsAgentName } " to local name "${ agentName } "` ) ;
144+ }
145+ }
146+ }
147+
148+ if ( ! agentName ) {
118149 return failResult (
119150 logger ,
120- `Online eval config references agent "${ agentName } " which is not in this project. ` +
151+ `Online eval config references agent "${ awsAgentName } " which is not in this project. ` +
121152 `Import or add the agent first with \`agentcore import runtime\` or \`agentcore add agent\`.` ,
122153 'online-eval' ,
123154 localName
0 commit comments