-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathoutputs.ts
More file actions
264 lines (228 loc) · 8.14 KB
/
Copy pathoutputs.ts
File metadata and controls
264 lines (228 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import type { AgentCoreDeployedState, DeployedState, MemoryDeployedState, TargetDeployedState } from '../../schema';
import { getCredentialProvider } from '../aws';
import { toPascalId } from './logical-ids';
import { getStackName } from './stack-discovery';
import { CloudFormationClient, DescribeStacksCommand } from '@aws-sdk/client-cloudformation';
export type StackOutputs = Record<string, string>;
/**
* Fetch CloudFormation stack outputs.
*/
export async function getStackOutputs(region: string, stackName: string): Promise<StackOutputs> {
const cfn = new CloudFormationClient({ region, credentials: getCredentialProvider() });
const resp = await cfn.send(new DescribeStacksCommand({ StackName: stackName }));
const stack = resp.Stacks?.[0];
if (!stack) {
throw new Error(`Stack ${stackName} not found`);
}
const outputs: StackOutputs = {};
for (const output of stack.Outputs ?? []) {
if (output.OutputKey && output.OutputValue) {
outputs[output.OutputKey] = output.OutputValue;
}
}
return outputs;
}
/**
* Parse stack outputs into deployed state for gateways.
*
* Output key pattern for gateways:
* Gateway{GatewayName}UrlOutput{Hash}
*
* Examples:
* - GatewayMyGatewayUrlOutput3E11FAB4
*/
export function parseGatewayOutputs(
outputs: StackOutputs,
gatewaySpecs: Record<string, unknown>
): Record<string, { gatewayId: string; gatewayArn: string; gatewayUrl?: string }> {
const gateways: Record<string, { gatewayId: string; gatewayArn: string; gatewayUrl?: string }> = {};
// Map PascalCase gateway names to original names for lookup
const gatewayNames = Object.keys(gatewaySpecs);
const gatewayIdMap = new Map(gatewayNames.map(name => [toPascalId(name), name]));
// Match patterns: Gateway{Name}{Type}Output
const outputPattern = /^Gateway(.+?)(Id|Arn|Url)Output/;
for (const [key, value] of Object.entries(outputs)) {
const match = outputPattern.exec(key);
if (!match) continue;
const logicalGateway = match[1];
const outputType = match[2];
if (!logicalGateway || !outputType) continue;
// Look up original gateway name from PascalCase version
const gatewayName = gatewayIdMap.get(logicalGateway) ?? logicalGateway;
gateways[gatewayName] ??= { gatewayId: gatewayName, gatewayArn: '' };
if (outputType === 'Id') {
gateways[gatewayName].gatewayId = value;
} else if (outputType === 'Arn') {
gateways[gatewayName].gatewayArn = value;
} else if (outputType === 'Url') {
gateways[gatewayName].gatewayUrl = value;
}
}
return gateways;
}
/**
* Parse stack outputs into deployed state for agents.
*
* Output key pattern after logical ID simplification:
* ApplicationAgent{AgentName}{OutputType}Output{Hash}
*
* Examples:
* - ApplicationAgentAdvancedAgentRuntimeIdOutput3E11FAB4
* - ApplicationAgentBasicStrandsRoleArnOutputF1FD8F36
*/
export function parseAgentOutputs(
outputs: StackOutputs,
agentNames: string[],
_stackName: string
): Record<string, AgentCoreDeployedState> {
const agents: Record<string, AgentCoreDeployedState> = {};
// Map PascalCase agent names to original names for lookup
const agentIdMap = new Map(agentNames.map(name => [toPascalId(name), name]));
const outputsByAgent: Record<
string,
{
runtimeId?: string;
runtimeArn?: string;
roleArn?: string;
memoryIds?: string;
browserId?: string;
codeInterpreterId?: string;
}
> = {};
// Match pattern: ApplicationAgent{AgentName}{OutputType}Output
const outputPattern =
/^ApplicationAgent(.+?)(RuntimeId|RuntimeArn|RoleArn|MemoryIds|BrowserId|CodeInterpreterId)Output/;
for (const [key, value] of Object.entries(outputs)) {
const match = outputPattern.exec(key);
if (!match) continue;
const logicalAgent = match[1];
const outputType = match[2];
if (!logicalAgent || !outputType) continue;
// Look up original agent name from PascalCase version
const agentName = agentIdMap.get(logicalAgent) ?? logicalAgent;
outputsByAgent[agentName] ??= {};
switch (outputType) {
case 'RuntimeId':
outputsByAgent[agentName].runtimeId = value;
break;
case 'RuntimeArn':
outputsByAgent[agentName].runtimeArn = value;
break;
case 'RoleArn':
outputsByAgent[agentName].roleArn = value;
break;
case 'MemoryIds':
outputsByAgent[agentName].memoryIds = value;
break;
case 'BrowserId':
outputsByAgent[agentName].browserId = value;
break;
case 'CodeInterpreterId':
outputsByAgent[agentName].codeInterpreterId = value;
break;
default:
break;
}
}
for (const [agentName, agentOutputs] of Object.entries(outputsByAgent)) {
if (!agentOutputs.runtimeId || !agentOutputs.runtimeArn || !agentOutputs.roleArn) {
continue;
}
const state: AgentCoreDeployedState = {
runtimeId: agentOutputs.runtimeId,
runtimeArn: agentOutputs.runtimeArn,
roleArn: agentOutputs.roleArn,
};
if (agentOutputs.memoryIds) {
state.memoryIds = agentOutputs.memoryIds.split(',');
}
if (agentOutputs.browserId) {
state.browserId = agentOutputs.browserId;
}
if (agentOutputs.codeInterpreterId) {
state.codeInterpreterId = agentOutputs.codeInterpreterId;
}
agents[agentName] = state;
}
return agents;
}
/**
* Parse stack outputs into deployed state for memories.
*
* Looks up outputs by constructing the expected key prefix from known memory names
*
* Output key pattern: ApplicationMemory{PascalName}(Id|Arn)Output{Hash}
*/
export function parseMemoryOutputs(outputs: StackOutputs, memoryNames: string[]): Record<string, MemoryDeployedState> {
const memories: Record<string, MemoryDeployedState> = {};
const outputKeys = Object.keys(outputs);
for (const memoryName of memoryNames) {
const pascal = toPascalId(memoryName);
const idPrefix = `ApplicationMemory${pascal}IdOutput`;
const arnPrefix = `ApplicationMemory${pascal}ArnOutput`;
const idKey = outputKeys.find(k => k.startsWith(idPrefix));
const arnKey = outputKeys.find(k => k.startsWith(arnPrefix));
if (idKey && arnKey) {
memories[memoryName] = {
memoryId: outputs[idKey]!,
memoryArn: outputs[arnKey]!,
};
}
}
return memories;
}
export interface BuildDeployedStateOptions {
targetName: string;
stackName: string;
agents: Record<string, AgentCoreDeployedState>;
gateways: Record<string, { gatewayId: string; gatewayArn: string; gatewayUrl?: string }>;
existingState?: DeployedState;
identityKmsKeyArn?: string;
credentials?: Record<string, { credentialProviderArn: string; clientSecretArn?: string; callbackUrl?: string }>;
memories?: Record<string, MemoryDeployedState>;
}
/**
* Build deployed state from stack outputs.
*/
export function buildDeployedState(opts: BuildDeployedStateOptions): DeployedState {
const { targetName, stackName, agents, gateways, existingState, identityKmsKeyArn, credentials, memories } = opts;
const targetState: TargetDeployedState = {
resources: {
agents: Object.keys(agents).length > 0 ? agents : undefined,
memories: memories && Object.keys(memories).length > 0 ? memories : undefined,
stackName,
identityKmsKeyArn,
},
};
// Add MCP state if gateways exist
if (Object.keys(gateways).length > 0) {
targetState.resources!.mcp = {
gateways,
};
}
// Add credential state if credentials exist
if (credentials && Object.keys(credentials).length > 0) {
targetState.resources!.credentials = credentials;
}
return {
targets: {
...existingState?.targets,
[targetName]: targetState,
},
};
}
/**
* Get stack outputs by project name (discovers stack via tags).
* Uses Resource Groups Tagging API to find the stack, then DescribeStacks for outputs.
*/
export async function getStackOutputsByProject(
region: string,
projectName: string,
targetName = 'default'
): Promise<StackOutputs> {
const stackName = await getStackName(region, projectName, targetName);
if (!stackName) {
throw new Error(`No AgentCore stack found for project "${projectName}" target "${targetName}"`);
}
return getStackOutputs(region, stackName);
}