forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.ts
More file actions
171 lines (149 loc) · 4.79 KB
/
Copy pathconfig.ts
File metadata and controls
171 lines (149 loc) · 4.79 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
import { ConfigIO, findConfigRoot } from '../../../lib';
import type { AgentCoreProjectSpec, AgentEnvSpec, BuildType, ProtocolMode } from '../../../schema';
import { dirname, isAbsolute, join } from 'node:path';
export interface DevConfig {
agentName: string;
module: string;
directory: string;
hasConfig: boolean;
isPython: boolean;
buildType: BuildType;
protocol: ProtocolMode;
dockerfile?: string;
}
interface DevSupportResult {
supported: boolean;
reason?: string;
}
/**
* Checks if the agent is a Python agent by looking at the entrypoint.
*/
function isPythonAgent(agent: AgentEnvSpec): boolean {
return agent.entrypoint?.endsWith('.py') || agent.entrypoint?.includes('.py:');
}
/**
* Checks if dev mode is supported for the given agent.
*
* Requirements:
* - Agent must target Python (TypeScript support not yet implemented)
* - CodeZip agents must have entrypoint
*/
function isDevSupported(agent: AgentEnvSpec): DevSupportResult {
if (!agent.entrypoint) {
return {
supported: false,
reason: `Agent "${agent.name}" is missing entrypoint.`,
};
}
// Container agents are supported for dev mode (requires local container runtime)
if (agent.build === 'Container') {
return { supported: true };
}
// Currently only Python is supported for CodeZip dev mode
if (!isPythonAgent(agent)) {
return {
supported: false,
reason: `Dev mode only supports Python agents. Agent "${agent.name}" does not appear to be a Python agent.`,
};
}
return { supported: true };
}
/**
* Resolves the agent's code directory from codeLocation.
* codeLocation can be absolute or relative to the project root.
*/
function resolveCodeDirectory(codeLocation: string, configRoot: string): string {
const cleanPath = codeLocation.replace(/\/$/, '');
if (isAbsolute(cleanPath)) {
return cleanPath;
}
const projectRoot = dirname(configRoot);
return join(projectRoot, cleanPath);
}
/**
* Returns a list of agents that support dev mode.
*/
export function getDevSupportedAgents(project: AgentCoreProjectSpec | null): AgentEnvSpec[] {
if (!project?.runtimes) return [];
return project.runtimes.filter(agent => isDevSupported(agent).supported);
}
/**
* Get the port for a specific agent based on its index in the project.
* Base port + agent index = actual port
*/
export function getAgentPort(project: AgentCoreProjectSpec | null, agentName: string, basePort: number): number {
if (!project) return basePort;
const index = project.runtimes.findIndex(a => a.name === agentName);
return index >= 0 ? basePort + index : basePort;
}
/**
* Derives dev server configuration from project config.
* Falls back to sensible defaults if no config is available.
* @param workingDir
* @param project
* @param configRoot
* @param agentName - Optional agent name. If not provided, uses the first dev-supported agent.
*/
export function getDevConfig(
workingDir: string,
project: AgentCoreProjectSpec | null,
configRoot?: string,
agentName?: string
): DevConfig | null {
if (!project) {
throw new Error('No project configuration found');
}
// Find the target agent
let targetAgent: AgentEnvSpec | undefined;
if (agentName) {
targetAgent = project.runtimes.find(a => a.name === agentName);
if (!targetAgent) {
throw new Error(`Agent "${agentName}" not found in project.`);
}
} else {
// Default to first dev-supported agent
const supportedAgents = getDevSupportedAgents(project);
targetAgent = supportedAgents[0];
}
if (!targetAgent) {
// Return null instead of throwing - let caller handle the UI for no agents
return null;
}
const supportResult = isDevSupported(targetAgent);
if (!supportResult.supported) {
throw new Error(supportResult.reason ?? 'Agent does not support dev mode');
}
const directory =
configRoot && targetAgent.codeLocation ? resolveCodeDirectory(targetAgent.codeLocation, configRoot) : workingDir;
return {
agentName: targetAgent.name,
module: targetAgent.entrypoint,
directory,
hasConfig: true,
isPython: isPythonAgent(targetAgent),
buildType: targetAgent.build,
protocol: targetAgent.protocol ?? 'HTTP',
dockerfile: targetAgent.dockerfile,
};
}
/**
* Loads project configuration from the agentcore directory.
* Walks up from workingDir to find the agentcore config directory.
* Returns null if config doesn't exist or is invalid.
*/
export async function loadProjectConfig(workingDir: string): Promise<AgentCoreProjectSpec | null> {
const configRoot = findConfigRoot(workingDir);
if (!configRoot) {
return null;
}
const configIO = new ConfigIO({ baseDir: configRoot });
if (!configIO.configExists('project')) {
return null;
}
try {
return await configIO.readProjectSpec();
} catch {
// Invalid config
return null;
}
}