forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolve-agent.ts
More file actions
99 lines (83 loc) · 2.94 KB
/
Copy pathresolve-agent.ts
File metadata and controls
99 lines (83 loc) · 2.94 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
import { ConfigIO } from '../../lib';
import type { AgentCoreProjectSpec, AwsDeploymentTargets, DeployedState } from '../../schema';
export interface DeployedProjectConfig {
project: AgentCoreProjectSpec;
deployedState: DeployedState;
awsTargets: AwsDeploymentTargets;
}
export interface ResolvedAgent {
agentName: string;
targetName: string;
region: string;
accountId: string;
runtimeId: string;
}
/**
* Loads the configuration files needed for agent resolution.
*/
export async function loadDeployedProjectConfig(configIO: ConfigIO = new ConfigIO()): Promise<DeployedProjectConfig> {
return {
project: await configIO.readProjectSpec(),
deployedState: await configIO.readDeployedState(),
awsTargets: await configIO.readAWSDeploymentTargets(),
};
}
/**
* Resolves which deployed agent to target from configuration and options.
*/
export function resolveAgent(
context: DeployedProjectConfig,
options: { runtime?: string }
): { success: true; agent: ResolvedAgent } | { success: false; error: string } {
const { project, deployedState, awsTargets } = context;
if (project.runtimes.length === 0) {
return { success: false, error: 'No runtimes defined in agentcore.json' };
}
// Resolve runtime
const runtimeNames = project.runtimes.map(a => a.name);
if (!options.runtime && project.runtimes.length > 1) {
return {
success: false,
error: `Multiple runtimes found. Use --runtime to specify one: ${runtimeNames.join(', ')}`,
};
}
const agentSpec = options.runtime ? project.runtimes.find(a => a.name === options.runtime) : project.runtimes[0];
if (options.runtime && !agentSpec) {
return {
success: false,
error: `Runtime '${options.runtime}' not found. Available: ${runtimeNames.join(', ')}`,
};
}
if (!agentSpec) {
return { success: false, error: 'No runtimes defined in agentcore.json' };
}
// Resolve target
const targetNames = Object.keys(deployedState.targets);
if (targetNames.length === 0) {
return { success: false, error: 'No deployed targets found. Run `agentcore deploy` first.' };
}
const selectedTargetName = targetNames[0]!;
const targetState = deployedState.targets[selectedTargetName];
const targetConfig = awsTargets.find(t => t.name === selectedTargetName);
if (!targetConfig) {
return { success: false, error: `Target config '${selectedTargetName}' not found in aws-targets` };
}
// Get the deployed state for this specific agent
const agentState = targetState?.resources?.runtimes?.[agentSpec.name];
if (!agentState) {
return {
success: false,
error: `Runtime '${agentSpec.name}' is not deployed to target '${selectedTargetName}'. Run 'agentcore deploy' first.`,
};
}
return {
success: true,
agent: {
agentName: agentSpec.name,
targetName: selectedTargetName,
region: targetConfig.region,
accountId: targetConfig.account,
runtimeId: agentState.runtimeId,
},
};
}