-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathindex.ts
More file actions
95 lines (88 loc) · 3.38 KB
/
index.ts
File metadata and controls
95 lines (88 loc) · 3.38 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
import type { AgentCoreProjectSpec, AgentEnvSpec, RuntimeVersion } from '../../schema';
import { DEFAULT_PYTHON_VERSION } from '../../schema';
import { ContainerPackager } from './container';
import { PackagingError } from './errors';
import { isNodeRuntime, isPythonRuntime } from './helpers';
import { NodeCodeZipPackager, NodeCodeZipPackagerSync } from './node';
import { PythonCodeZipPackager, PythonCodeZipPackagerSync } from './python';
import type {
ArtifactResult,
CodeBundleConfig,
CodeZipPackager,
PackageOptions,
RuntimePackager,
} from './types/packaging';
/**
* Validate that an agent exists in the config
* @param project AgentCore project configuration
* @param agentName Name of agent to validate
* @throws PackagingError if agent not found
*/
export function validateAgentExists(project: AgentCoreProjectSpec, agentName: string): void {
const agent = project.runtimes.find((a: AgentEnvSpec) => a.name === agentName);
if (!agent) {
const available = project.runtimes.map((a: AgentEnvSpec) => a.name).join(', ');
throw new PackagingError(`Agent '${agentName}' not found. Available agents: ${available}`);
}
}
/**
* Get the async runtime packager for CLI usage based on runtime version.
* Supports both Python and Node/TypeScript runtimes.
*/
export function getRuntimePackager(runtimeVersion: RuntimeVersion): RuntimePackager {
if (isPythonRuntime(runtimeVersion)) {
return new PythonCodeZipPackager();
}
if (isNodeRuntime(runtimeVersion)) {
return new NodeCodeZipPackager();
}
throw new PackagingError(`Unsupported runtime version: ${runtimeVersion}`);
}
/**
* Get the sync CodeZip packager for CDK bundling based on runtime version.
* Supports both Python and Node/TypeScript runtimes.
*/
export function getCodeZipPackager(runtimeVersion: RuntimeVersion): CodeZipPackager {
if (isPythonRuntime(runtimeVersion)) {
return new PythonCodeZipPackagerSync();
}
if (isNodeRuntime(runtimeVersion)) {
return new NodeCodeZipPackagerSync();
}
throw new PackagingError(`Unsupported runtime version: ${runtimeVersion}`);
}
/**
* Get the async runtime packager for Container agents.
*/
export function getContainerPackager(): RuntimePackager {
return new ContainerPackager();
}
/**
* Package a runtime asynchronously.
* This is the primary API for CLI usage.
* Automatically selects the appropriate packager based on build type and runtime version.
*/
export async function packRuntime(spec: AgentEnvSpec, options?: PackageOptions): Promise<ArtifactResult> {
const packager = spec.build === 'Container' ? getContainerPackager() : getRuntimePackager(spec.runtimeVersion!);
return packager.pack(spec, options);
}
/**
* Package a code bundle synchronously.
* This is the primary API for CDK bundling.
* Works with AgentEnvSpec or any object with name, codeLocation, and entrypoint.
* Defaults to Python if no runtimeVersion is specified.
*/
export function packCodeZipSync(config: CodeBundleConfig | AgentEnvSpec, options?: PackageOptions): ArtifactResult {
const runtimeVersion = config.runtimeVersion ?? DEFAULT_PYTHON_VERSION;
const packager = getCodeZipPackager(runtimeVersion);
return packager.packCodeZip(config as AgentEnvSpec, options);
}
export type {
ArtifactResult,
CodeBundleConfig,
CodeZipPackager,
PackageOptions,
RuntimePackager,
} from './types/packaging';
export * from './errors';
export { resolveCodeLocation } from './helpers';