forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.ts
More file actions
211 lines (185 loc) · 8.04 KB
/
validate.ts
File metadata and controls
211 lines (185 loc) · 8.04 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 {
BuildTypeSchema,
ModelProviderSchema,
ProjectNameSchema,
ProtocolModeSchema,
SDKFrameworkSchema,
TargetLanguageSchema,
getSupportedFrameworksForProtocol,
getSupportedModelProviders,
matchEnumValue,
} from '../../../schema';
import type { ProtocolMode } from '../../../schema';
import { parseAndValidateLifecycleOptions } from '../shared/lifecycle-utils';
import { validateVpcOptions } from '../shared/vpc-utils';
import type { CreateOptions } from './types';
import { existsSync } from 'fs';
import { join } from 'path';
export interface ValidationResult {
valid: boolean;
error?: string;
}
const MEMORY_OPTIONS = ['none', 'shortTerm', 'longAndShortTerm', 'custom'] as const;
/** Check if a folder with the given name already exists in the directory */
export function validateFolderNotExists(name: string, cwd: string): true | string {
const projectPath = join(cwd, name);
if (existsSync(projectPath)) {
return `A folder named '${name}' already exists in this directory`;
}
return true;
}
export function validateCreateOptions(options: CreateOptions, cwd?: string): ValidationResult {
// Name is required for non-interactive mode
if (!options.name) {
return { valid: false, error: '--name is required' };
}
// Validate name format
const nameResult = ProjectNameSchema.safeParse(options.name);
if (!nameResult.success) {
return { valid: false, error: nameResult.error.issues[0]?.message ?? 'Invalid project name' };
}
// Check if directory already exists
const folderCheck = validateFolderNotExists(options.name, cwd ?? process.cwd());
if (folderCheck !== true) {
return { valid: false, error: folderCheck };
}
// If --no-agent (agent === false), no further validation needed
if (options.agent === false) {
return { valid: true };
}
// Import path: validate import-specific options
if (options.type === 'import') {
if (!options.agentId) return { valid: false, error: '--agent-id is required for import' };
if (!options.agentAliasId) return { valid: false, error: '--agent-alias-id is required for import' };
if (!options.region) return { valid: false, error: '--region is required for import' };
if (!options.framework)
return { valid: false, error: '--framework is required for import (Strands or LangChain_LangGraph)' };
const fw = matchEnumValue(SDKFrameworkSchema, options.framework) ?? options.framework;
options.framework = fw;
if (fw !== 'Strands' && fw !== 'LangChain_LangGraph') {
return { valid: false, error: `Import only supports Strands or LangChain_LangGraph, got: ${options.framework}` };
}
options.memory ??= 'none';
if (!MEMORY_OPTIONS.includes(options.memory as (typeof MEMORY_OPTIONS)[number])) {
return {
valid: false,
error: `Invalid memory option: ${options.memory}. Use none, shortTerm, longAndShortTerm, or custom`,
};
}
return { valid: true };
}
// Normalize enum flag values (case-insensitive matching)
if (options.protocol) options.protocol = matchEnumValue(ProtocolModeSchema, options.protocol) ?? options.protocol;
if (options.language) options.language = matchEnumValue(TargetLanguageSchema, options.language) ?? options.language;
if (options.framework) options.framework = matchEnumValue(SDKFrameworkSchema, options.framework) ?? options.framework;
if (options.modelProvider)
options.modelProvider = matchEnumValue(ModelProviderSchema, options.modelProvider) ?? options.modelProvider;
if (options.build) options.build = matchEnumValue(BuildTypeSchema, options.build) ?? options.build;
// Validate protocol if provided
let protocol: ProtocolMode = 'HTTP';
if (options.protocol) {
const protocolResult = ProtocolModeSchema.safeParse(options.protocol);
if (!protocolResult.success) {
return { valid: false, error: `Invalid protocol: ${options.protocol}. Use HTTP, MCP, or A2A` };
}
protocol = protocolResult.data;
}
// Validate build type if provided (applies to all protocols)
if (options.build) {
const buildResult = BuildTypeSchema.safeParse(options.build);
if (!buildResult.success) {
return { valid: false, error: `Invalid build type: ${options.build}. Use CodeZip or Container` };
}
}
// MCP protocol: only name, language, and build type required
if (protocol === 'MCP') {
if (options.framework) {
return { valid: false, error: '--framework is not applicable for MCP protocol' };
}
if (options.modelProvider) {
return { valid: false, error: '--model-provider is not applicable for MCP protocol' };
}
if (options.memory && options.memory !== 'none') {
return { valid: false, error: '--memory is not applicable for MCP protocol' };
}
if (options.language) {
const langResult = TargetLanguageSchema.safeParse(options.language);
if (!langResult.success) {
return { valid: false, error: `Invalid language: ${options.language}` };
}
}
return { valid: true };
}
// Without --no-agent, all agent options are required
const hasAllAgentOptions = options.framework && options.modelProvider && options.memory;
if (!hasAllAgentOptions) {
return {
valid: false,
error: 'Use --no-agent for project-only, or provide all: --framework, --model-provider, --memory',
};
}
// Validate all agent options
{
if (!options.language) {
return { valid: false, error: '--language is required when creating an agent' };
}
if (!options.framework) {
return { valid: false, error: '--framework is required when creating an agent' };
}
if (!options.modelProvider) {
return { valid: false, error: '--model-provider is required when creating an agent' };
}
if (!options.memory) {
return { valid: false, error: '--memory is required when creating an agent' };
}
// Validate language
const langResult = TargetLanguageSchema.safeParse(options.language);
if (!langResult.success) {
return { valid: false, error: `Invalid language: ${options.language}. Use Python` };
}
// Validate framework
const fwResult = SDKFrameworkSchema.safeParse(options.framework);
if (!fwResult.success) {
return { valid: false, error: `Invalid framework: ${options.framework}` };
}
// Validate framework is supported for the protocol
if (protocol !== 'HTTP') {
const supportedFrameworks = getSupportedFrameworksForProtocol(protocol);
if (!supportedFrameworks.includes(fwResult.data)) {
return { valid: false, error: `${options.framework} does not support ${protocol} protocol` };
}
}
// Validate model provider
const mpResult = ModelProviderSchema.safeParse(options.modelProvider);
if (!mpResult.success) {
return { valid: false, error: `Invalid model provider: ${options.modelProvider}` };
}
// Validate language is supported
if (options.language === 'TypeScript') {
return { valid: false, error: 'TypeScript is not yet supported. Currently supported: Python' };
}
// Validate framework/model compatibility
const supportedProviders = getSupportedModelProviders(fwResult.data);
if (!supportedProviders.includes(mpResult.data)) {
return { valid: false, error: `${options.framework} does not support ${options.modelProvider}` };
}
// Validate memory option
if (!MEMORY_OPTIONS.includes(options.memory as (typeof MEMORY_OPTIONS)[number])) {
return {
valid: false,
error: `Invalid memory option: ${options.memory}. Use none, shortTerm, longAndShortTerm, or custom`,
};
}
}
// Validate VPC options
const vpcResult = validateVpcOptions(options);
if (!vpcResult.valid) {
return { valid: false, error: vpcResult.error };
}
// Parse and validate lifecycle configuration
const lifecycleResult = parseAndValidateLifecycleOptions(options);
if (!lifecycleResult.valid) return lifecycleResult;
if (lifecycleResult.idleTimeout !== undefined) options.idleTimeout = lifecycleResult.idleTimeout;
if (lifecycleResult.maxLifetime !== undefined) options.maxLifetime = lifecycleResult.maxLifetime;
return { valid: true };
}