-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathConfigBundlePrimitive.ts
More file actions
237 lines (207 loc) · 8.12 KB
/
Copy pathConfigBundlePrimitive.ts
File metadata and controls
237 lines (207 loc) · 8.12 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { findConfigRoot } from '../../lib';
import type { ConfigBundle } from '../../schema';
import { ConfigBundleSchema } from '../../schema';
import { getErrorMessage } from '../errors';
import type { RemovalPreview, RemovalResult, SchemaChange } from '../operations/remove/types';
import { BasePrimitive } from './BasePrimitive';
import type { AddResult, AddScreenComponent, RemovableResource } from './types';
import type { Command } from '@commander-js/extra-typings';
import { readFileSync } from 'fs';
export interface AddConfigBundleOptions {
name: string;
description?: string;
components: Record<string, { configuration: Record<string, unknown> }>;
branchName?: string;
commitMessage?: string;
}
export type RemovableConfigBundle = RemovableResource;
/**
* ConfigBundlePrimitive handles all configuration bundle add/remove operations.
*
* Configuration bundles are versioned collections of component configurations
* (system prompts, tool configs) keyed by component ARN. They are created via
* direct API calls (not CloudFormation) and stored in agentcore.json for
* lifecycle management.
*/
export class ConfigBundlePrimitive extends BasePrimitive<AddConfigBundleOptions, RemovableConfigBundle> {
readonly kind = 'config-bundle' as const;
readonly label = 'Configuration Bundle';
override readonly article = 'a';
readonly primitiveSchema = ConfigBundleSchema;
async add(options: AddConfigBundleOptions): Promise<AddResult<{ bundleName: string }>> {
try {
const bundle = await this.createConfigBundle(options);
return { success: true, bundleName: bundle.name };
} catch (err) {
return { success: false, error: getErrorMessage(err) };
}
}
async remove(bundleName: string): Promise<RemovalResult> {
try {
const project = await this.readProjectSpec();
const index = (project.configBundles ?? []).findIndex(b => b.name === bundleName);
if (index === -1) {
return { success: false, error: `Configuration bundle "${bundleName}" not found.` };
}
project.configBundles.splice(index, 1);
await this.writeProjectSpec(project);
return { success: true };
} catch (err) {
return { success: false, error: getErrorMessage(err) };
}
}
async previewRemove(bundleName: string): Promise<RemovalPreview> {
const project = await this.readProjectSpec();
const bundle = (project.configBundles ?? []).find(b => b.name === bundleName);
if (!bundle) {
throw new Error(`Configuration bundle "${bundleName}" not found.`);
}
const summary: string[] = [`Removing configuration bundle: ${bundleName}`];
const schemaChanges: SchemaChange[] = [];
const afterSpec = {
...project,
configBundles: (project.configBundles ?? []).filter(b => b.name !== bundleName),
};
schemaChanges.push({
file: 'agentcore/agentcore.json',
before: project,
after: afterSpec,
});
return { summary, directoriesToDelete: [], schemaChanges };
}
async getRemovable(): Promise<RemovableConfigBundle[]> {
try {
const project = await this.readProjectSpec();
return (project.configBundles ?? []).map(b => ({ name: b.name }));
} catch {
return [];
}
}
async getAllNames(): Promise<string[]> {
try {
const project = await this.readProjectSpec();
return (project.configBundles ?? []).map(b => b.name);
} catch {
return [];
}
}
registerCommands(addCmd: Command, removeCmd: Command): void {
addCmd
.command(this.kind)
.description('[preview] Add a configuration bundle to the project')
.option('--name <name>', 'Bundle name')
.option('--description <text>', 'Bundle description')
.option(
'--components <json>',
'Components map as inline JSON. Keys are ARNs or placeholders: {{runtime:<name>}}, {{gateway:<name>}}. Placeholders resolve to real ARNs at deploy time.'
)
.option('--components-file <path>', 'Path to components JSON file (same format as --components)')
.option('--branch <name>', 'Branch name for versioning')
.option('--commit-message <text>', 'Commit message for this version')
.option('--json', 'Output as JSON')
.action(
async (cliOptions: {
name?: string;
description?: string;
components?: string;
componentsFile?: string;
branch?: string;
commitMessage?: string;
json?: boolean;
}) => {
try {
if (!findConfigRoot()) {
console.error('No agentcore project found. Run `agentcore create` first.');
process.exit(1);
}
if (cliOptions.name || cliOptions.json) {
const fail = (error: string) => {
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error }));
} else {
console.error(error);
}
process.exit(1);
};
if (!cliOptions.name) {
fail('--name is required in non-interactive mode');
}
if (!cliOptions.components && !cliOptions.componentsFile) {
fail('Either --components or --components-file is required');
}
let components: Record<string, { configuration: Record<string, unknown> }>;
if (cliOptions.componentsFile) {
const raw = readFileSync(cliOptions.componentsFile, 'utf-8');
components = JSON.parse(raw) as Record<string, { configuration: Record<string, unknown> }>;
} else {
components = JSON.parse(cliOptions.components!) as Record<
string,
{ configuration: Record<string, unknown> }
>;
}
const result = await this.add({
name: cliOptions.name!,
description: cliOptions.description,
components,
branchName: cliOptions.branch,
commitMessage: cliOptions.commitMessage,
});
if (cliOptions.json) {
console.log(JSON.stringify(result));
} else if (result.success) {
console.log(`Added configuration bundle '${result.bundleName}'`);
} else {
console.error(result.error);
}
process.exit(result.success ? 0 : 1);
} else {
// TUI fallback
const [{ render }, { default: React }, { AddFlow }] = await Promise.all([
import('ink'),
import('react'),
import('../tui/screens/add/AddFlow'),
]);
const { clear, unmount } = render(
React.createElement(AddFlow, {
isInteractive: false,
initialResource: 'config-bundle',
onExit: () => {
clear();
unmount();
process.exit(0);
},
})
);
}
} catch (error) {
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error: getErrorMessage(error) }));
} else {
console.error(getErrorMessage(error));
}
process.exit(1);
}
}
);
this.registerRemoveSubcommand(removeCmd);
}
addScreen(): AddScreenComponent {
return null;
}
private async createConfigBundle(options: AddConfigBundleOptions): Promise<ConfigBundle> {
const project = await this.readProjectSpec();
this.checkDuplicate(project.configBundles ?? [], options.name);
const bundle: ConfigBundle = {
name: options.name,
type: 'ConfigurationBundle',
...(options.description && { description: options.description }),
components: options.components,
branchName: options.branchName ?? 'mainline',
...(options.commitMessage && { commitMessage: options.commitMessage }),
};
project.configBundles ??= [];
project.configBundles.push(bundle);
await this.writeProjectSpec(project);
return bundle;
}
}