-
Notifications
You must be signed in to change notification settings - Fork 86
refactor: align templates with old cli definition #2094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
667fb0e
66b85a5
457e397
978a20c
20911b3
e295d4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| import { ZodError, z } from "zod"; | ||
| import { PROJECT_TEMPLATES, type ProjectTemplate } from "../../handlers/project/types"; | ||
| import { HarnessSpecSchema } from "../../projectSchemas/harness"; | ||
| import { FsTreeNode } from "./fsTree"; | ||
| import type { AssetSource } from "./source"; | ||
| import { InputValidationError } from "../../errors/errors"; | ||
| import { | ||
| RUNTIME_TEMPLATE_SHORTCUTS, | ||
| type ScaffoldRuntimeInput, | ||
| } from "../../handlers/project/types"; | ||
|
|
||
| type TemplateSpec = { | ||
| runtimes?: unknown[]; | ||
|
|
@@ -16,25 +19,22 @@ type TemplateSpec = { | |
| * sections it registers in agentcore.json. Adding a template is one entry here plus its assets. | ||
| */ | ||
| type Template = { | ||
| /** Directory under app/ the template code is written to. */ | ||
| appDir: string; | ||
| /** Asset directory relative to the asset root, expanded into the app directory. */ | ||
| assetDir: string; | ||
| /** Resource sections this template contributes to agentcore.json. */ | ||
| spec: TemplateSpec; | ||
| }; | ||
|
|
||
| export const TEMPLATES: Record<ProjectTemplate, Template> = { | ||
| [PROJECT_TEMPLATES.HELLO_WORLD_PYTHON]: { | ||
| appDir: "hello-world", | ||
| const TEMPLATES: Record<string, Template> = { | ||
| [buildRuntimeTemplateKey(RUNTIME_TEMPLATE_SHORTCUTS["hello-world-python"])]: { | ||
| assetDir: "templates/hello-world-python", | ||
| spec: { | ||
| runtimes: [ | ||
| { | ||
| name: "hello_world", | ||
| build: "CodeZip", | ||
| entrypoint: "main.py", | ||
| codeLocation: "app/hello-world", | ||
| codeLocation: "app/hello_world", | ||
| // Required for CodeZip builds: the CDK construct library rejects a | ||
| // CodeZip runtime with no runtimeVersion, and it is what selects the | ||
| // packager. Container builds take their version from the image. | ||
|
|
@@ -43,45 +43,54 @@ export const TEMPLATES: Record<ProjectTemplate, Template> = { | |
| ], | ||
| }, | ||
| }, | ||
| [PROJECT_TEMPLATES.HELLO_WORLD_PYTHON_CONTAINER]: { | ||
| appDir: "hello-world", | ||
| [buildRuntimeTemplateKey(RUNTIME_TEMPLATE_SHORTCUTS["hello-world-python-container"])]: { | ||
| assetDir: "templates/hello-world-python-container", | ||
| spec: { | ||
| runtimes: [ | ||
| { | ||
| name: "hello_world", | ||
| build: "Container", | ||
| entrypoint: "main.py", | ||
| codeLocation: "app/hello-world", | ||
| codeLocation: "app/hello_world", | ||
| dockerfile: "Dockerfile", | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| function buildRuntimeTemplateKey(input: ScaffoldRuntimeInput): string { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i plan to refactor this in a future PR. We're going to want some of these parameters to determine the asset templates to render from, and some of them to be rendered into it with handlebars. However, this felt like the smallest change I could make to keep it functional with the new interface. This PR is intended to focus on aligning the handlers with what we want, then we work backwards from there to implement the functionality we need to support it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What makes this complexity necessary at this stage? Do we expect more than a handful of templates? |
||
| return `runtime_${input.build}_${input.framework}_${input.language}_${input.memory}_${input.modelProvider}`; | ||
| } | ||
|
|
||
| function resolveTemplate(input: ScaffoldRuntimeInput): Template | undefined { | ||
| return TEMPLATES[buildRuntimeTemplateKey(input)]; | ||
| } | ||
|
|
||
| /** Serializes a value as pretty-printed JSON with a trailing newline. */ | ||
| const json = (value: unknown): string => `${JSON.stringify(value, null, 2)}\n`; | ||
|
|
||
| /** | ||
| * Builds the agentcore.json spec by adding the template's resource sections to the shared base. | ||
| * The base fields and template sections never overlap so this is a plain spread. | ||
| */ | ||
| function agentcoreSpec(name: string, template: ProjectTemplate): unknown { | ||
| function agentcoreSpec(name: string, template: Template): unknown { | ||
| return { | ||
| name, | ||
| version: 1, | ||
| managedBy: "CDK", | ||
| ...TEMPLATES[template].spec, | ||
| ...template.spec, | ||
| }; | ||
| } | ||
|
|
||
| export async function createProjectTreeFromTemplate( | ||
| export async function createProjectTree( | ||
| name: string, | ||
| template: ProjectTemplate, | ||
| input: ScaffoldRuntimeInput, | ||
| src: AssetSource, | ||
| ): Promise<FsTreeNode> { | ||
| const { appDir, assetDir } = TEMPLATES[template]; | ||
| const template = resolveTemplate(input); | ||
| if (!template) | ||
| throw new InputValidationError(`unable to find template that matches given parameters`); | ||
| return FsTreeNode.createDirectory(".", [ | ||
| FsTreeNode.createFile(".gitignore", () => src.read("templates/shared/gitignore.template")), | ||
| FsTreeNode.createDirectory("agentcore", [ | ||
|
|
@@ -90,7 +99,10 @@ export async function createProjectTreeFromTemplate( | |
| FsTreeNode.createFile("aws-targets.json", async () => json([])), | ||
| FsTreeNode.createFile(".env.local", () => src.read("templates/shared/env.local.template")), | ||
| ]), | ||
| FsTreeNode.createDirectory("app", [await FsTreeNode.fromAssetSource(src, assetDir, appDir)]), | ||
| FsTreeNode.createDirectory("app", [ | ||
| // TODO: replace this hardcoded "hello_world" with the runtime name once templates are more flexible. | ||
| await FsTreeNode.fromAssetSource(src, template.assetDir, "hello_world"), | ||
| ]), | ||
| ]); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😭 I forgot
-is not supported in runtime