forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath-resolver.ts
More file actions
211 lines (184 loc) · 5.64 KB
/
Copy pathpath-resolver.ts
File metadata and controls
211 lines (184 loc) · 5.64 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
import { CLI_LOGS_DIR, CLI_SYSTEM_DIR, CONFIG_DIR, CONFIG_FILES as _CONFIG_FILES } from '../../constants';
import { existsSync } from 'fs';
import { dirname, join } from 'path';
// Re-export for backward compatibility
export const CONFIG_FILES = _CONFIG_FILES;
/**
* Error thrown when no AgentCore project is found.
*/
export class NoProjectError extends Error {
constructor(message?: string) {
super(message ?? 'No agentcore project found. Run "agentcore create" first.');
this.name = 'NoProjectError';
}
}
/**
* Get the working directory where the user invoked the CLI.
*
* When running via npm/bun scripts (e.g., `npm run cli`), the package manager
* changes process.cwd() to the package root. INIT_CWD preserves the original
* directory where the user ran the command.
*
* For globally installed CLIs, INIT_CWD is not set, so we fall back to process.cwd().
*/
export function getWorkingDirectory(): string {
return process.env.INIT_CWD ?? process.cwd();
}
/**
* Find the config root, throwing NoProjectError if not found.
* Use this when a project is required to proceed.
*/
export function requireConfigRoot(startDir?: string): string {
const configRoot = findConfigRoot(startDir);
if (!configRoot) {
throw new NoProjectError();
}
return configRoot;
}
/**
* Session-level project root override.
* Set this after init creates a project so subsequent discovery finds it.
*/
let sessionProjectRoot: string | null = null;
/**
* Set the project root for the current session.
* Call this after init creates a new project at cwd/projectName/.
* Subsequent calls to findConfigRoot() will check this location first.
*/
export function setSessionProjectRoot(projectRoot: string): void {
sessionProjectRoot = projectRoot;
}
/**
* Get the current session project root, if set.
*/
export function getSessionProjectRoot(): string | null {
return sessionProjectRoot;
}
/**
* Configuration for where AgentCore files are stored
*/
export interface PathConfig {
/** Base directory for all AgentCore files */
baseDir: string;
}
/**
* Check if a directory is a valid AgentCore config directory.
* A valid config directory contains agentcore.json.
*/
function isValidConfigDir(configPath: string): boolean {
return existsSync(join(configPath, _CONFIG_FILES.AGENT_ENV));
}
/**
* Walk up the directory tree from startDir looking for an agentcore directory.
* If a session project was set (via setSessionProjectRoot), checks there first.
* Returns the path to the agentcore directory if found, or null if not found.
*/
export function findConfigRoot(startDir: string = getWorkingDirectory()): string | null {
// Check session project first (set after init creates a project)
if (sessionProjectRoot) {
const sessionConfigPath = join(sessionProjectRoot, CONFIG_DIR);
if (existsSync(sessionConfigPath) && isValidConfigDir(sessionConfigPath)) {
return sessionConfigPath;
}
}
// Fall back to walking up the directory tree
let currentDir = startDir;
while (true) {
const configPath = join(currentDir, CONFIG_DIR);
if (existsSync(configPath) && isValidConfigDir(configPath)) {
return configPath;
}
const parentDir = dirname(currentDir);
// Reached filesystem root
if (parentDir === currentDir) {
return null;
}
currentDir = parentDir;
}
}
/**
* Get the project root directory (parent of agentcore/).
* Returns null if no project is found.
*/
export function findProjectRoot(startDir: string = process.cwd()): string | null {
const configRoot = findConfigRoot(startDir);
return configRoot ? dirname(configRoot) : null;
}
/**
* Default configuration uses agentcore/ in current working directory.
*/
export const DEFAULT_PATH_CONFIG: PathConfig = {
baseDir: join(getWorkingDirectory(), CONFIG_DIR),
};
/**
* Utility class for resolving AgentCore file paths
*/
export class PathResolver {
private config: PathConfig;
constructor(config?: Partial<PathConfig>) {
this.config = {
...DEFAULT_PATH_CONFIG,
...config,
};
}
/**
* Get the base directory path (the agentcore/ config directory)
*/
getBaseDir(): string {
return this.config.baseDir;
}
/**
* Get the project root directory (parent of agentcore/)
*/
getProjectRoot(): string {
return dirname(this.config.baseDir);
}
/**
* Get the path to the agent config file (agentcore.json)
*/
getAgentConfigPath(): string {
return join(this.config.baseDir, CONFIG_FILES.AGENT_ENV);
}
/**
* Get the path to the AWS targets config file (aws-targets.json)
*/
getAWSTargetsConfigPath(): string {
return join(this.config.baseDir, CONFIG_FILES.AWS_TARGETS);
}
/**
* Get the path to the CLI system directory (agentcore/.cli/)
*/
getCliSystemDir(): string {
return join(this.config.baseDir, CLI_SYSTEM_DIR);
}
/**
* Get the path to the logs directory (agentcore/.cli/logs/)
*/
getLogsDir(): string {
return join(this.config.baseDir, CLI_SYSTEM_DIR, CLI_LOGS_DIR);
}
/**
* Get the path to the invoke logs directory (agentcore/.cli/logs/invoke/)
*/
getInvokeLogsDir(): string {
return join(this.config.baseDir, CLI_SYSTEM_DIR, CLI_LOGS_DIR, 'invoke');
}
/**
* Get the path to the deployed state file (agentcore/.cli/deployed-state.json)
*/
getStatePath(): string {
return join(this.config.baseDir, CLI_SYSTEM_DIR, CONFIG_FILES.DEPLOYED_STATE);
}
/**
* Get the path to the MCP definitions file (mcp-defs.json)
*/
getMcpDefsPath(): string {
return join(this.config.baseDir, CONFIG_FILES.MCP_DEFS);
}
/**
* Update the base directory
*/
setBaseDir(baseDir: string): void {
this.config.baseDir = baseDir;
}
}