Skip to content

Commit ca51763

Browse files
committed
feat: implement modular primitive architecture
Refactor all resource types (agent, memory, credential, gateway, gateway-target) into self-contained primitive classes that own the full add/remove lifecycle. Each primitive extends BasePrimitive and implements add(), remove(), previewRemove(), getRemovable(), and registerCommands(). Primitives absorb scattered operation functions and register their own CLI subcommands via a registry loop.
1 parent 8c8b179 commit ca51763

113 files changed

Lines changed: 10116 additions & 2908 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

AGENTS.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ src/
1010
├── schema/ # Schema definitions with Zod validators
1111
├── lib/ # Shared utilities (ConfigIO, packaging)
1212
├── cli/ # CLI implementation
13-
│ ├── commands/ # CLI commands
13+
│ ├── primitives/ # Resource primitives (add/remove logic per resource type)
14+
│ ├── commands/ # CLI commands (thin Commander registration)
1415
│ ├── tui/ # Terminal UI (Ink/React)
15-
│ ├── operations/ # Business logic
16+
│ ├── operations/ # Shared business logic (schema mapping, deploy, etc.)
1617
│ ├── cdk/ # CDK toolkit wrapper for programmatic CDK operations
1718
│ └── templates/ # Project templating
1819
└── assets/ # Template assets vended to users
@@ -50,6 +51,25 @@ Note: CDK L3 constructs are in a separate package `@aws/agentcore-cdk`.
5051

5152
- MCP gateway and tool support (`add gateway`, `add mcp-tool`) - currently hidden
5253

54+
## Primitives Architecture
55+
56+
All resource types (agent, memory, identity, gateway, mcp-tool) are modeled as **primitives** — self-contained classes
57+
in `src/cli/primitives/` that own the full add/remove lifecycle for their resource type.
58+
59+
Each primitive extends `BasePrimitive` and implements: `add()`, `remove()`, `previewRemove()`, `getRemovable()`,
60+
`registerCommands()`, and `addScreen()`.
61+
62+
Current primitives:
63+
64+
- `AgentPrimitive` — agent creation (template + BYO), removal, credential resolution
65+
- `MemoryPrimitive` — memory creation with strategies, removal
66+
- `CredentialPrimitive` — credential/identity creation, .env management, removal
67+
- `GatewayPrimitive` — MCP gateway creation/removal (hidden, coming soon)
68+
- `GatewayTargetPrimitive` — MCP tool creation/removal with code generation (hidden, coming soon)
69+
70+
Singletons are created in `registry.ts` and wired into CLI commands via `cli.ts`. See `src/cli/AGENTS.md` for details on
71+
adding new primitives.
72+
5373
## Vended CDK Project
5474

5575
When users run `agentcore create`, we vend a CDK project at `agentcore/cdk/` that:
@@ -88,3 +108,16 @@ See `docs/TESTING.md` for details.
88108
## Related Package
89109

90110
- `@aws/agentcore-cdk` - CDK constructs used by vended projects
111+
112+
## Code Style
113+
114+
- Never use inline imports. Imports must always go at the top of the file.
115+
- Wheverever there is a requirement to use something that returns a success result and an error message you must use
116+
this format
117+
118+
```javascript
119+
{ success: Boolean, error?:string}
120+
```
121+
122+
- Always look for existing types before creating a new type inline.
123+
- Re-usable constants must be defined in a constants file in the closest sensible subdirectory.

RefactorTest/AGENTS.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# AgentCore Project
2+
3+
This project contains configuration and infrastructure for an Amazon Bedrock AgentCore application.
4+
5+
The `agentcore/` directory serves as a declarative model of an AgentCore project along with a concrete implementation
6+
through the `agentcore/cdk/` project which is modeled to take the configs as input. The project uses a **flat resource
7+
model** where agents, memories, and credentials are top-level arrays.
8+
9+
## Mental Model
10+
11+
The project uses a **flat resource model**. Agents, memories, and credentials are independent top-level arrays in
12+
`agentcore.json`. There is no binding or attachment between resources in the schema — each resource is provisioned
13+
independently. To use a memory or credential from an agent, the application code discovers the resource at runtime
14+
(e.g., via environment variables or SDK calls).
15+
16+
## Critical Invariants
17+
18+
1. **Schema-First Authority:** The `.json` files are the absolute source of truth. Do not attempt to modify agent
19+
behavior by editing the generated CDK code in `cdk/`.
20+
2. **Resource Identity:** The `name` field in the schema determines the CloudFormation Logical ID.
21+
- **Renaming** an agent or target will **destroy and recreate** that resource.
22+
- **Modifying** other fields (descriptions, config) will update the resource **in-place**.
23+
3. **1:1 Validation:** The schema maps directly to valid CloudFormation. If your JSON conforms to the types in
24+
`.llm-context/`, it will deploy successfully.
25+
4. **Resource Removal:** To remove all resources, use `agentcore remove all`. To tear down deployed infrastructure, run
26+
`agentcore deploy` after removal — it will detect the empty state and offer a teardown flow.
27+
28+
## Directory Structure
29+
30+
```
31+
myNewProject/
32+
├── AGENTS.md # This file - AI coding assistant context
33+
├── agentcore/ # AgentCore configuration directory
34+
│ ├── agentcore.json # Main project config (AgentCoreProjectSpec)
35+
│ ├── aws-targets.json # Deployment targets
36+
│ ├── .llm-context/ # TypeScript type definitions for AI coding assistants
37+
│ │ ├── README.md # Guide to using the schema files
38+
│ │ ├── agentcore.ts # AgentCoreProjectSpec types
39+
│ │ └── aws-targets.ts # AWS deployment target types
40+
│ └── cdk/ # AWS CDK project for deployment
41+
└── app/ # Application code (if agents were created)
42+
```
43+
44+
## Schema Reference
45+
46+
The `agentcore/.llm-context/` directory contains TypeScript type definitions optimized for AI coding assistants. Each
47+
file maps to a JSON config file and includes validation constraints as comments.
48+
49+
| JSON Config | Schema File | Root Type |
50+
| ---------------------------- | --------------------------------------- | ----------------------- |
51+
| `agentcore/agentcore.json` | `agentcore/.llm-context/agentcore.ts` | `AgentCoreProjectSpec` |
52+
| `agentcore/aws-targets.json` | `agentcore/.llm-context/aws-targets.ts` | `AWSDeploymentTarget[]` |
53+
54+
### Key Types
55+
56+
- **AgentCoreProjectSpec**: Root project configuration with `agents`, `memories`, `credentials` arrays
57+
- **AgentEnvSpec**: Agent configuration (runtime, entrypoint, code location)
58+
- **Memory**: Memory resource with strategies and expiry
59+
- **Credential**: API key credential provider
60+
61+
### Common Enum Values
62+
63+
- **BuildType**: `'CodeZip'` | `'Container'`
64+
- **NetworkMode**: `'PUBLIC'`
65+
- **RuntimeVersion**: `'PYTHON_3_10'` | `'PYTHON_3_11'` | `'PYTHON_3_12'` | `'PYTHON_3_13'`
66+
- **MemoryStrategyType**: `'SEMANTIC'` | `'SUMMARIZATION'` | `'USER_PREFERENCE'`
67+
68+
### Build Types
69+
70+
- **CodeZip**: Python source is packaged as a zip artifact and deployed directly to AgentCore Runtime.
71+
- **Container**: Agent code is built as a Docker container image. Requires a `Dockerfile` in the agent's `codeLocation`
72+
directory. At deploy time, the source is uploaded to S3, built in CodeBuild (ARM64), pushed to a per-agent ECR
73+
repository, and the container URI is provided to the AgentCore Runtime. For local development (`agentcore dev`), the
74+
container is built and run locally with volume-mounted hot-reload.
75+
76+
### Supported Frameworks (for template agents)
77+
78+
- **Strands** - Works with Bedrock, Anthropic, OpenAI, Gemini
79+
- **LangChain_LangGraph** - Works with Bedrock, Anthropic, OpenAI, Gemini
80+
- **CrewAI** - Works with Bedrock, Anthropic, OpenAI, Gemini
81+
- **GoogleADK** - Gemini only
82+
- **OpenAIAgents** - OpenAI only
83+
- **AutoGen** - Works with Bedrock, Anthropic, OpenAI, Gemini
84+
85+
### Specific Context
86+
87+
Directory pathing to local projects is required for runtimes. Both CodeZip (Python zip) and Container (Docker image)
88+
deployment options are available.
89+
90+
## Deployment
91+
92+
The `agentcore/cdk/` subdirectory contains an AWS CDK node project.
93+
94+
Deployments of this project are primarily intended to be orchestrated through the `agentcore deploy` command in the CLI.
95+
96+
Alternatively, the project can be deployed directly as a traditional CDK project:
97+
98+
```bash
99+
cd agentcore/cdk
100+
npm install
101+
npx cdk synth # Preview CloudFormation template
102+
npx cdk deploy # Deploy to AWS
103+
```
104+
105+
## Editing Schemas
106+
107+
When modifying JSON config files:
108+
109+
1. Read the corresponding `agentcore/.llm-context/*.ts` file for type definitions
110+
2. Check validation constraint comments (`@regex`, `@min`, `@max`)
111+
3. Use exact enum values as string literals
112+
4. Use CloudFormation-safe names (alphanumeric, start with letter)
113+
5. Run `agentcore validate` command to verify changes.

RefactorTest/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# AgentCore Project
2+
3+
This project was created with the [AgentCore CLI](https://github.com/aws/agentcore-cli).
4+
5+
## Project Structure
6+
7+
```
8+
.
9+
my-project/
10+
├── agentcore/
11+
│ ├── .env.local # API keys (gitignored)
12+
│ ├── agentcore.json # Resource specifications
13+
│ ├── aws-targets.json # Deployment targets
14+
│ └── cdk/ # CDK infrastructure
15+
├── app/ # Application code
16+
```
17+
18+
## Getting Started
19+
20+
### Prerequisites
21+
22+
- **Node.js** 20.x or later
23+
- **uv** for Python agents ([install](https://docs.astral.sh/uv/getting-started/installation/))
24+
25+
### Development
26+
27+
Run your agent locally:
28+
29+
```bash
30+
agentcore dev
31+
```
32+
33+
### Deployment
34+
35+
Deploy to AWS:
36+
37+
```bash
38+
agentcore deploy
39+
```
40+
41+
Or use CDK directly:
42+
43+
```bash
44+
cd agentcore/cdk
45+
npx cdk deploy
46+
```
47+
48+
## Configuration
49+
50+
Edit the JSON files in `agentcore/` to configure your agents, memory, and credentials. See `agentcore/.llm-context/` for
51+
type definitions and validation constraints.
52+
53+
The project uses a **flat resource model** where agents, memories, and credentials are top-level arrays in
54+
`agentcore.json`.
55+
56+
## Commands
57+
58+
| Command | Description |
59+
| -------------------- | ----------------------------------------------- |
60+
| `agentcore create` | Create a new AgentCore project |
61+
| `agentcore add` | Add resources (agent, memory, identity, target) |
62+
| `agentcore remove` | Remove resources |
63+
| `agentcore dev` | Run agent locally |
64+
| `agentcore deploy` | Deploy to AWS |
65+
| `agentcore status` | Show deployment status |
66+
| `agentcore invoke` | Invoke agent (local or deployed) |
67+
| `agentcore package` | Package agent artifacts |
68+
| `agentcore validate` | Validate configuration |
69+
| `agentcore update` | Check for CLI updates |
70+
71+
### Agent Types
72+
73+
- **Template agents**: Created from framework templates (Strands, LangChain_LangGraph, GoogleADK, OpenAIAgents)
74+
- **BYO agents**: Bring your own code with `agentcore add agent --type byo`
75+
76+
## Documentation
77+
78+
- [AgentCore CLI Documentation](https://github.com/aws/agentcore-cli)
79+
- [Amazon Bedrock AgentCore](https://aws.amazon.com/bedrock/agentcore/)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"targets": {}
3+
}

RefactorTest/agentcore/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Secrets (local environment files are never committed)
2+
.env.local
3+
4+
# CDK Build Artifacts
5+
cdk/cdk.out/
6+
cdk/node_modules/
7+
8+
# CLI Internals
9+
.cli/*
10+
11+
# Ephemeral Staging
12+
.cache/*
13+
14+
# Exception: Commit the State
15+
!.cli/deployed-state.json
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# LLM Context Files
2+
3+
**DO NOT EDIT THESE FILES** - They are read-only reference for AI coding assistants.
4+
5+
## Files
6+
7+
| File | JSON Config | Purpose |
8+
| ---------------- | ------------------ | ------------------------------------ |
9+
| `agentcore.ts` | `agentcore.json` | Project and agent environment config |
10+
| `aws-targets.ts` | `aws-targets.json` | Deployment targets |
11+
12+
## Usage
13+
14+
When editing schema JSON files, reference the corresponding `.ts` file here for type definitions and validation
15+
constraints (marked with `@regex`, `@min`, `@max`).
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
/**
3+
* READ-ONLY LLM CONTEXT - Do not edit this file.
4+
*
5+
* JSON File: agentcore/agentcore.json
6+
* Purpose: Top-level project configuration with flat resource model
7+
*/
8+
9+
// ─────────────────────────────────────────────────────────────────────────────
10+
// ROOT SCHEMA: AgentCoreProjectSpec
11+
// ─────────────────────────────────────────────────────────────────────────────
12+
13+
interface AgentCoreProjectSpec {
14+
name: string; // @regex ^[A-Za-z][A-Za-z0-9]{0,22}$ @max 23 - project name
15+
version: number; // Schema version (integer)
16+
agents: AgentEnvSpec[]; // Unique by name
17+
memories: Memory[]; // Unique by name
18+
credentials: Credential[]; // Unique by name
19+
}
20+
21+
// ─────────────────────────────────────────────────────────────────────────────
22+
// ENUMS
23+
// ─────────────────────────────────────────────────────────────────────────────
24+
25+
type BuildType = 'CodeZip' | 'Container';
26+
type PythonRuntime = 'PYTHON_3_10' | 'PYTHON_3_11' | 'PYTHON_3_12' | 'PYTHON_3_13';
27+
type NodeRuntime = 'NODE_18' | 'NODE_20' | 'NODE_22';
28+
type RuntimeVersion = PythonRuntime | NodeRuntime;
29+
type NetworkMode = 'PUBLIC' | 'VPC';
30+
type MemoryStrategyType = 'SEMANTIC' | 'SUMMARIZATION' | 'USER_PREFERENCE';
31+
type ModelProvider = 'Bedrock' | 'Gemini' | 'OpenAI' | 'Anthropic';
32+
33+
// ─────────────────────────────────────────────────────────────────────────────
34+
// NETWORK CONFIG
35+
// ─────────────────────────────────────────────────────────────────────────────
36+
37+
interface NetworkConfig {
38+
subnets: string[]; // @regex ^subnet-[0-9a-zA-Z]{8,17}$ @min 1 @max 16
39+
securityGroups: string[]; // @regex ^sg-[0-9a-zA-Z]{8,17}$ @min 1 @max 16
40+
}
41+
42+
// ─────────────────────────────────────────────────────────────────────────────
43+
// AGENT
44+
// ─────────────────────────────────────────────────────────────────────────────
45+
46+
interface AgentEnvSpec {
47+
type: 'AgentCoreRuntime';
48+
name: string; // @regex ^[a-zA-Z][a-zA-Z0-9_]{0,47}$ @max 48
49+
build: BuildType;
50+
entrypoint: string; // @regex ^[a-zA-Z0-9_][a-zA-Z0-9_/.-]*\.(py|ts|js)(:[a-zA-Z_][a-zA-Z0-9_]*)?$ e.g. "main.py:handler" or "index.ts"
51+
codeLocation: string; // Directory path
52+
runtimeVersion: RuntimeVersion;
53+
envVars?: EnvVar[];
54+
networkMode?: NetworkMode; // default 'PUBLIC'
55+
networkConfig?: NetworkConfig; // Required when networkMode is 'VPC'
56+
instrumentation?: Instrumentation; // OTel settings
57+
modelProvider?: ModelProvider; // Model provider used by this agent
58+
}
59+
60+
interface Instrumentation {
61+
enableOtel: boolean; // default true - wrap entrypoint with opentelemetry-instrument
62+
}
63+
64+
interface EnvVar {
65+
name: string; // @regex ^[A-Za-z_][A-Za-z0-9_]*$ @max 255
66+
value: string;
67+
}
68+
69+
// ─────────────────────────────────────────────────────────────────────────────
70+
// MEMORY
71+
// ─────────────────────────────────────────────────────────────────────────────
72+
73+
interface Memory {
74+
type: 'AgentCoreMemory';
75+
name: string; // @regex ^[a-zA-Z][a-zA-Z0-9_]{0,47}$ @max 48
76+
eventExpiryDuration: number; // @min 7 @max 365 (days)
77+
strategies: MemoryStrategy[]; // @min 1, unique by type
78+
}
79+
80+
interface MemoryStrategy {
81+
type: MemoryStrategyType;
82+
name?: string; // @regex ^[a-zA-Z][a-zA-Z0-9_]{0,47}$ @max 48
83+
description?: string;
84+
namespaces?: string[];
85+
}
86+
87+
// ─────────────────────────────────────────────────────────────────────────────
88+
// CREDENTIAL
89+
// ─────────────────────────────────────────────────────────────────────────────
90+
91+
interface Credential {
92+
type: 'ApiKeyCredentialProvider';
93+
name: string; // @regex ^[A-Za-z0-9_.-]+$ @min 3 @max 255
94+
}

0 commit comments

Comments
 (0)