-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathvalidate.ts
More file actions
129 lines (109 loc) · 4.19 KB
/
validate.ts
File metadata and controls
129 lines (109 loc) · 4.19 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
import {
BuildTypeSchema,
ModelProviderSchema,
ProjectNameSchema,
SDKFrameworkSchema,
TargetLanguageSchema,
getSupportedModelProviders,
} from '../../../schema';
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'] 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 };
}
// Validate build type if provided
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` };
}
}
// 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 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, or longAndShortTerm`,
};
}
}
const vpcResult = validateVpcOptions(options);
if (!vpcResult.valid) return vpcResult;
return { valid: true };
}