|
1 | 1 | import type { Evaluator } from '../../../schema'; |
2 | 2 | import type { EvaluatorSummary, GetEvaluatorResult } from '../../aws/agentcore-control'; |
3 | | -import { |
4 | | - getEvaluator, |
5 | | - getOnlineEvaluationConfig, |
6 | | - listAllEvaluators, |
7 | | - listAllOnlineEvaluationConfigs, |
8 | | - updateOnlineEvalExecutionStatus, |
9 | | -} from '../../aws/agentcore-control'; |
| 3 | +import { getEvaluator, listAllEvaluators } from '../../aws/agentcore-control'; |
10 | 4 | import { ANSI } from './constants'; |
11 | 5 | import { parseAndValidateArn } from './import-utils'; |
12 | 6 | import { executeResourceImport } from './resource-import'; |
@@ -54,136 +48,42 @@ export function toEvaluatorSpec(detail: GetEvaluatorResult, localName: string): |
54 | 48 | }; |
55 | 49 | } |
56 | 50 |
|
57 | | -/** |
58 | | - * Create an evaluator descriptor with closed-over state for tracking |
59 | | - * online eval configs that were temporarily disabled to unlock the evaluator. |
60 | | - */ |
61 | | -function createEvaluatorDescriptor(): { |
62 | | - descriptor: ResourceImportDescriptor<GetEvaluatorResult, EvaluatorSummary>; |
63 | | - getDisabledConfigs: () => { configId: string; configName: string }[]; |
64 | | - getRegion: () => string | undefined; |
65 | | -} { |
66 | | - const disabledConfigs: { configId: string; configName: string }[] = []; |
67 | | - let resolvedRegion: string | undefined; |
68 | | - |
69 | | - const descriptor: ResourceImportDescriptor<GetEvaluatorResult, EvaluatorSummary> = { |
70 | | - resourceType: 'evaluator', |
71 | | - displayName: 'evaluator', |
72 | | - logCommand: 'import-evaluator', |
73 | | - |
74 | | - listResources: region => listAllEvaluators({ region }), |
75 | | - getDetail: (region, id) => getEvaluator({ region, evaluatorId: id }), |
76 | | - parseResourceId: (arn, target) => parseAndValidateArn(arn, 'evaluator', target).resourceId, |
77 | | - |
78 | | - extractSummaryId: s => s.evaluatorId, |
79 | | - formatListItem: (s, i) => |
80 | | - ` ${ANSI.dim}[${i + 1}]${ANSI.reset} ${s.evaluatorName} — ${s.status}\n ${ANSI.dim}${s.evaluatorArn}${ANSI.reset}`, |
81 | | - formatAutoSelectMessage: s => `Found 1 evaluator: ${s.evaluatorName} (${s.evaluatorId}). Auto-selecting.`, |
82 | | - |
83 | | - extractDetailName: d => d.evaluatorName, |
84 | | - extractDetailArn: d => d.evaluatorArn, |
85 | | - readyStatus: 'ACTIVE', |
86 | | - extractDetailStatus: d => d.status, |
| 51 | +const evaluatorDescriptor: ResourceImportDescriptor<GetEvaluatorResult, EvaluatorSummary> = { |
| 52 | + resourceType: 'evaluator', |
| 53 | + displayName: 'evaluator', |
| 54 | + logCommand: 'import-evaluator', |
87 | 55 |
|
88 | | - getExistingNames: spec => (spec.evaluators ?? []).map(e => e.name), |
89 | | - addToProjectSpec: (detail, localName, spec) => { |
90 | | - (spec.evaluators ??= []).push(toEvaluatorSpec(detail, localName)); |
91 | | - }, |
| 56 | + listResources: region => listAllEvaluators({ region }), |
| 57 | + getDetail: (region, id) => getEvaluator({ region, evaluatorId: id }), |
| 58 | + parseResourceId: (arn, target) => parseAndValidateArn(arn, 'evaluator', target).resourceId, |
92 | 59 |
|
93 | | - cfnResourceType: 'AWS::BedrockAgentCore::Evaluator', |
94 | | - cfnNameProperty: 'EvaluatorName', |
95 | | - cfnIdentifierKey: 'EvaluatorId', |
| 60 | + extractSummaryId: s => s.evaluatorId, |
| 61 | + formatListItem: (s, i) => |
| 62 | + ` ${ANSI.dim}[${i + 1}]${ANSI.reset} ${s.evaluatorName} — ${s.status}\n ${ANSI.dim}${s.evaluatorArn}${ANSI.reset}`, |
| 63 | + formatAutoSelectMessage: s => `Found 1 evaluator: ${s.evaluatorName} (${s.evaluatorId}). Auto-selecting.`, |
96 | 64 |
|
97 | | - buildDeployedStateEntry: (name, id, d) => ({ type: 'evaluator', name, id, arn: d.evaluatorArn }), |
98 | | - |
99 | | - beforeConfigWrite: async ({ detail, target, onProgress, logger }) => { |
100 | | - resolvedRegion = target.region; |
101 | | - if (!detail.lockedForModification) return; |
102 | | - |
103 | | - logger.startStep('Unlock evaluator'); |
104 | | - onProgress('Evaluator is locked. Finding referencing online eval configs...'); |
105 | | - |
106 | | - const allConfigs = await listAllOnlineEvaluationConfigs({ region: target.region }); |
107 | | - const enabledConfigs = allConfigs.filter(c => c.executionStatus === 'ENABLED'); |
108 | | - |
109 | | - for (const config of enabledConfigs) { |
110 | | - const configDetail = await getOnlineEvaluationConfig({ |
111 | | - region: target.region, |
112 | | - configId: config.onlineEvaluationConfigId, |
113 | | - }); |
114 | | - if (configDetail.evaluatorIds?.includes(detail.evaluatorId)) { |
115 | | - onProgress(`Disabling online eval config: ${config.onlineEvaluationConfigName}`); |
116 | | - await updateOnlineEvalExecutionStatus({ |
117 | | - region: target.region, |
118 | | - onlineEvaluationConfigId: config.onlineEvaluationConfigId, |
119 | | - executionStatus: 'DISABLED', |
120 | | - }); |
121 | | - disabledConfigs.push({ |
122 | | - configId: config.onlineEvaluationConfigId, |
123 | | - configName: config.onlineEvaluationConfigName, |
124 | | - }); |
125 | | - } |
126 | | - } |
| 65 | + extractDetailName: d => d.evaluatorName, |
| 66 | + extractDetailArn: d => d.evaluatorArn, |
| 67 | + readyStatus: 'ACTIVE', |
| 68 | + extractDetailStatus: d => d.status, |
127 | 69 |
|
128 | | - if (disabledConfigs.length > 0) { |
129 | | - onProgress(`Disabled ${disabledConfigs.length} online eval config(s) to unlock evaluator`); |
130 | | - } else { |
131 | | - onProgress('Evaluator is locked but no enabled online eval configs reference it'); |
132 | | - } |
133 | | - logger.endStep('success'); |
134 | | - }, |
135 | | - }; |
| 70 | + getExistingNames: spec => (spec.evaluators ?? []).map(e => e.name), |
| 71 | + addToProjectSpec: (detail, localName, spec) => { |
| 72 | + (spec.evaluators ??= []).push(toEvaluatorSpec(detail, localName)); |
| 73 | + }, |
136 | 74 |
|
137 | | - return { |
138 | | - descriptor, |
139 | | - getDisabledConfigs: () => [...disabledConfigs], |
140 | | - getRegion: () => resolvedRegion, |
141 | | - }; |
142 | | -} |
| 75 | + cfnResourceType: 'AWS::BedrockAgentCore::Evaluator', |
| 76 | + cfnNameProperty: 'EvaluatorName', |
| 77 | + cfnIdentifierKey: 'EvaluatorId', |
143 | 78 |
|
144 | | -/** |
145 | | - * Re-enable online eval configs that were temporarily disabled during import. |
146 | | - */ |
147 | | -async function reEnableConfigs( |
148 | | - configs: { configId: string; configName: string }[], |
149 | | - region: string, |
150 | | - onWarn: (msg: string) => void |
151 | | -): Promise<void> { |
152 | | - for (const config of configs) { |
153 | | - try { |
154 | | - await updateOnlineEvalExecutionStatus({ |
155 | | - region, |
156 | | - onlineEvaluationConfigId: config.configId, |
157 | | - executionStatus: 'ENABLED', |
158 | | - }); |
159 | | - } catch (err) { |
160 | | - onWarn( |
161 | | - `Warning: Could not re-enable online eval config "${config.configName}" (${config.configId}): ${err instanceof Error ? err.message : String(err)}` |
162 | | - ); |
163 | | - } |
164 | | - } |
165 | | -} |
| 79 | + buildDeployedStateEntry: (name, id, d) => ({ type: 'evaluator', name, id, arn: d.evaluatorArn }), |
| 80 | +}; |
166 | 81 |
|
167 | 82 | /** |
168 | 83 | * Handle `agentcore import evaluator`. |
169 | 84 | */ |
170 | 85 | export async function handleImportEvaluator(options: ImportResourceOptions): Promise<ImportResourceResult> { |
171 | | - const { descriptor, getDisabledConfigs, getRegion } = createEvaluatorDescriptor(); |
172 | | - |
173 | | - try { |
174 | | - const result = await executeResourceImport(descriptor, options); |
175 | | - return result; |
176 | | - } finally { |
177 | | - const disabled = getDisabledConfigs(); |
178 | | - const region = getRegion(); |
179 | | - if (disabled.length > 0 && region) { |
180 | | - await reEnableConfigs(disabled, region, msg => console.warn(msg)); |
181 | | - const names = disabled.map(c => c.configName).join(', '); |
182 | | - console.warn( |
183 | | - `\n${ANSI.yellow}Warning:${ANSI.reset} ${disabled.length} online eval config(s) were temporarily disabled to unlock this evaluator and have been re-enabled: ${names}` |
184 | | - ); |
185 | | - } |
186 | | - } |
| 86 | + return executeResourceImport(evaluatorDescriptor, options); |
187 | 87 | } |
188 | 88 |
|
189 | 89 | /** |
|
0 commit comments