-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathHarnessPrimitive.ts
More file actions
532 lines (483 loc) · 21.2 KB
/
Copy pathHarnessPrimitive.ts
File metadata and controls
532 lines (483 loc) · 21.2 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import { APP_DIR, ConfigIO, findConfigRoot } from '../../lib';
import type {
HarnessGatewayOutboundAuth,
HarnessModelProvider,
HarnessSpec,
MemoryStrategy,
MemoryStrategyType,
NetworkMode,
RuntimeAuthorizerType,
} from '../../schema';
import { DEFAULT_EPISODIC_REFLECTION_NAMESPACES, DEFAULT_STRATEGY_NAMESPACES, HarnessSpecSchema } from '../../schema';
import { deleteHarness } from '../aws/agentcore-harness';
import { getErrorMessage } from '../errors';
import type { RemovalPreview, RemovalResult, SchemaChange } from '../operations/remove/types';
import { getTemplatePath } from '../templates/templateRoot';
import { DEFAULT_MEMORY_EXPIRY_DAYS } from '../tui/screens/generate/defaults';
import { BasePrimitive } from './BasePrimitive';
import { buildAuthorizerConfigFromJwtConfig, createManagedOAuthCredential } from './auth-utils';
import type { JwtConfigOptions } from './auth-utils';
import type { AddResult, AddScreenComponent, RemovableResource } from './types';
import type { Command } from '@commander-js/extra-typings';
import { access, copyFile, mkdir, readFile, rm, writeFile } from 'fs/promises';
import { basename, dirname, isAbsolute, join, resolve } from 'path';
export interface AddHarnessOptions {
name: string;
modelProvider: HarnessModelProvider;
modelId: string;
apiKeyArn?: string;
systemPrompt?: string;
skipMemory?: boolean;
containerUri?: string;
dockerfilePath?: string;
maxIterations?: number;
maxTokens?: number;
timeoutSeconds?: number;
truncationStrategy?: 'sliding_window' | 'summarization';
networkMode?: NetworkMode;
subnets?: string[];
securityGroups?: string[];
idleTimeout?: number;
maxLifetime?: number;
sessionStoragePath?: string;
withInvokeScript?: boolean;
selectedTools?: string[];
mcpName?: string;
mcpUrl?: string;
gatewayArn?: string;
gatewayOutboundAuth?: 'awsIam' | 'none' | 'oauth';
gatewayProviderArn?: string;
gatewayScopes?: string[];
authorizerType?: RuntimeAuthorizerType;
jwtConfig?: JwtConfigOptions;
configBaseDir?: string;
}
export type RemovableHarness = RemovableResource;
export class HarnessPrimitive extends BasePrimitive<AddHarnessOptions, RemovableHarness> {
readonly kind = 'harness' as const;
readonly label = 'Harness';
readonly primitiveSchema = HarnessSpecSchema;
async add(options: AddHarnessOptions): Promise<AddResult<{ harnessName: string }>> {
try {
const configBaseDir = options.configBaseDir ?? findConfigRoot();
if (!configBaseDir) {
return { success: false, error: 'No agentcore project found. Run `agentcore create` first.' };
}
const configIO = new ConfigIO({ baseDir: configBaseDir });
const project = await this.readProjectSpec(configIO);
const harnesses = project.harnesses ?? [];
this.checkDuplicate(harnesses, options.name);
const memoryName = options.skipMemory ? undefined : `${options.name}Memory`;
let dockerfile: string | undefined;
if (options.dockerfilePath) {
const projectRoot = dirname(configBaseDir);
const srcPath = isAbsolute(options.dockerfilePath)
? options.dockerfilePath
: resolve(projectRoot, options.dockerfilePath);
try {
await access(srcPath);
} catch {
return { success: false, error: `Dockerfile not found at: ${srcPath}` };
}
const appDir = join(projectRoot, APP_DIR, options.name);
await mkdir(appDir, { recursive: true });
const destFilename = basename(srcPath);
await copyFile(srcPath, join(appDir, destFilename));
dockerfile = destFilename;
}
const tools: HarnessSpec['tools'] = [];
if (options.selectedTools) {
for (const toolType of options.selectedTools) {
if (toolType === 'agentcore_browser') {
tools.push({ type: 'agentcore_browser', name: 'browser' });
} else if (toolType === 'agentcore_code_interpreter') {
tools.push({ type: 'agentcore_code_interpreter', name: 'code-interpreter' });
} else if (toolType === 'remote_mcp' && options.mcpName && options.mcpUrl) {
tools.push({
type: 'remote_mcp',
name: options.mcpName,
config: { remoteMcp: { url: options.mcpUrl } },
});
} else if (toolType === 'agentcore_gateway' && options.gatewayArn) {
let outboundAuth: HarnessGatewayOutboundAuth | undefined;
if (options.gatewayOutboundAuth === 'awsIam') {
outboundAuth = { awsIam: {} };
} else if (options.gatewayOutboundAuth === 'none') {
outboundAuth = { none: {} };
} else if (
options.gatewayOutboundAuth === 'oauth' &&
options.gatewayProviderArn &&
options.gatewayScopes &&
options.gatewayScopes.length > 0
) {
outboundAuth = {
oauth: {
providerArn: options.gatewayProviderArn,
scopes: options.gatewayScopes,
},
};
}
tools.push({
type: 'agentcore_gateway',
name: 'gateway',
config: {
agentCoreGateway: {
gatewayArn: options.gatewayArn,
...(outboundAuth && { outboundAuth }),
},
},
});
}
}
}
const harnessSpec: HarnessSpec = {
name: options.name,
model: {
provider: options.modelProvider,
modelId: options.modelId,
...(options.apiKeyArn && { apiKeyArn: options.apiKeyArn }),
},
tools,
skills: [],
...(options.systemPrompt && { systemPrompt: options.systemPrompt }),
...(memoryName && { memory: { name: memoryName } }),
...(options.containerUri && { containerUri: options.containerUri }),
...(dockerfile && { dockerfile }),
...(options.maxIterations !== undefined && { maxIterations: options.maxIterations }),
...(options.maxTokens !== undefined && { maxTokens: options.maxTokens }),
...(options.timeoutSeconds !== undefined && { timeoutSeconds: options.timeoutSeconds }),
...(options.truncationStrategy && { truncation: { strategy: options.truncationStrategy } }),
...(options.networkMode && { networkMode: options.networkMode }),
...(options.networkMode === 'VPC' &&
options.subnets &&
options.securityGroups && {
networkConfig: {
subnets: options.subnets,
securityGroups: options.securityGroups,
},
}),
...(this.buildLifecycleConfig(options) && { lifecycleConfig: this.buildLifecycleConfig(options) }),
...(options.sessionStoragePath && { sessionStoragePath: options.sessionStoragePath }),
...(options.authorizerType && { authorizerType: options.authorizerType }),
...(options.authorizerType === 'CUSTOM_JWT' && options.jwtConfig
? { authorizerConfiguration: buildAuthorizerConfigFromJwtConfig(options.jwtConfig) }
: {}),
};
await configIO.writeHarnessSpec(options.name, harnessSpec);
const pathResolver = configIO.getPathResolver();
const harnessDir = pathResolver.getHarnessDir(options.name);
const systemPromptPath = join(harnessDir, 'system-prompt.md');
const systemPromptContent = options.systemPrompt ?? 'You are a helpful assistant';
await writeFile(systemPromptPath, systemPromptContent, 'utf-8');
if (options.withInvokeScript) {
const templatePath = getTemplatePath('harness', 'invoke.py.template');
const invokeScriptPath = join(harnessDir, 'invoke.py');
let template = await readFile(templatePath, 'utf-8');
template = template.replace('{{HARNESS_ARN}}', '<your-harness-arn>');
template = template.replace('{{REGION}}', '<your-region>');
await writeFile(invokeScriptPath, template, 'utf-8');
}
if (memoryName) {
const strategyTypes: MemoryStrategyType[] = ['SEMANTIC', 'USER_PREFERENCE', 'SUMMARIZATION', 'EPISODIC'];
const strategies: MemoryStrategy[] = strategyTypes.map(type => ({
type,
...(DEFAULT_STRATEGY_NAMESPACES[type] && { namespaces: DEFAULT_STRATEGY_NAMESPACES[type] }),
...(type === 'EPISODIC' && { reflectionNamespaces: DEFAULT_EPISODIC_REFLECTION_NAMESPACES }),
}));
project.memories.push({
name: memoryName,
eventExpiryDuration: DEFAULT_MEMORY_EXPIRY_DAYS,
strategies,
});
}
project.harnesses = [
...harnesses,
{
name: options.name,
path: `app/${options.name}`,
},
];
await this.writeProjectSpec(project, configIO);
if (options.jwtConfig?.clientId && options.jwtConfig?.clientSecret) {
await createManagedOAuthCredential(
options.name,
options.jwtConfig,
spec => this.writeProjectSpec(spec, configIO),
() => this.readProjectSpec(configIO)
);
}
return { success: true, harnessName: options.name };
} catch (err) {
return { success: false, error: getErrorMessage(err) };
}
}
async remove(harnessName: string): Promise<RemovalResult> {
try {
const configRoot = findConfigRoot();
if (!configRoot) {
return { success: false, error: 'No agentcore project found.' };
}
const configIO = new ConfigIO({ baseDir: configRoot });
const project = await this.readProjectSpec(configIO);
const harnesses = project.harnesses ?? [];
const harnessIndex = harnesses.findIndex(h => h.name === harnessName);
if (harnessIndex === -1) {
return { success: false, error: `Harness "${harnessName}" not found.` };
}
// Delete harness from AWS if it's deployed
try {
const deployedState = await configIO.readDeployedState();
for (const target of Object.values(deployedState.targets)) {
const deployedHarness = target.resources?.harnesses?.[harnessName];
if (deployedHarness) {
const targets = await configIO.resolveAWSDeploymentTargets();
const region = targets[0]?.region;
if (region) {
await deleteHarness({ region, harnessId: deployedHarness.harnessId });
}
delete target.resources!.harnesses![harnessName];
await configIO.writeDeployedState(deployedState);
break;
}
}
} catch {
// AWS deletion is best-effort; next deploy will clean up
}
harnesses.splice(harnessIndex, 1);
project.harnesses = harnesses;
await this.writeProjectSpec(project, configIO);
const pathResolver = configIO.getPathResolver();
const harnessDir = pathResolver.getHarnessDir(harnessName);
await rm(harnessDir, { recursive: true, force: true });
return { success: true };
} catch (err) {
return { success: false, error: getErrorMessage(err) };
}
}
async previewRemove(harnessName: string): Promise<RemovalPreview> {
const project = await this.readProjectSpec();
const harnesses = project.harnesses ?? [];
const harness = harnesses.find(h => h.name === harnessName);
if (!harness) {
throw new Error(`Harness "${harnessName}" not found.`);
}
const summary: string[] = [`Removing harness: ${harnessName}`];
const directoriesToDelete: string[] = [`app/${harnessName}`];
const schemaChanges: SchemaChange[] = [];
const afterSpec = {
...project,
harnesses: harnesses.filter(h => h.name !== harnessName),
};
schemaChanges.push({
file: 'agentcore/agentcore.json',
before: project,
after: afterSpec,
});
return { summary, directoriesToDelete, schemaChanges };
}
async getRemovable(): Promise<RemovableHarness[]> {
try {
const project = await this.readProjectSpec();
const harnesses = project.harnesses ?? [];
return harnesses.map(h => ({ name: h.name }));
} catch {
return [];
}
}
registerCommands(addCmd: Command, removeCmd: Command): void {
addCmd
.command('harness')
.description('Add a harness to the project')
.option('--name <name>', 'Harness name (start with letter, alphanumeric + underscores, max 48 chars)')
.option('--model-provider <provider>', 'Model provider: bedrock, open_ai, gemini')
.option('--model-id <id>', 'Model ID (e.g., anthropic.claude-3-5-sonnet-20240620-v1:0)')
.option('--api-key-arn <arn>', 'API key ARN for non-Bedrock providers')
.option('--container <uri-or-path>', 'Container image URI or path to a Dockerfile')
.option('--no-memory', 'Skip auto-creating memory')
.option('--max-iterations <n>', 'Max iterations', parseInt)
.option('--max-tokens <n>', 'Max tokens', parseInt)
.option('--timeout <seconds>', 'Timeout in seconds', parseInt)
.option('--truncation-strategy <strategy>', 'Truncation strategy: sliding_window or summarization')
.option('--network-mode <mode>', 'Network mode: PUBLIC or VPC')
.option('--subnets <ids>', 'Comma-separated subnet IDs (for VPC mode)')
.option('--security-groups <ids>', 'Comma-separated security group IDs (for VPC mode)')
.option('--idle-timeout <seconds>', 'Idle timeout in seconds', parseInt)
.option('--max-lifetime <seconds>', 'Max lifetime in seconds', parseInt)
.option('--session-storage <path>', 'Mount path for persistent session storage (e.g., /mnt/data/)')
.option('--with-invoke-script', 'Generate a standalone Python invoke script')
.option('--authorizer-type <type>', 'Authorizer type: AWS_IAM or CUSTOM_JWT')
.option('--discovery-url <url>', 'OIDC discovery URL (for CUSTOM_JWT)')
.option('--allowed-audience <audiences>', 'Comma-separated allowed audiences (for CUSTOM_JWT)')
.option('--allowed-clients <clients>', 'Comma-separated allowed client IDs (for CUSTOM_JWT)')
.option('--allowed-scopes <scopes>', 'Comma-separated allowed scopes (for CUSTOM_JWT)')
.option('--custom-claims <json>', 'Custom claims JSON array (for CUSTOM_JWT)')
.option('--client-id <id>', 'OAuth client ID (for CUSTOM_JWT)')
.option('--client-secret <secret>', 'OAuth client secret (for CUSTOM_JWT)')
.option('--json', 'Output as JSON')
.action(
async (cliOptions: {
name?: string;
modelProvider?: string;
modelId?: string;
apiKeyArn?: string;
container?: string;
memory?: boolean;
maxIterations?: number;
maxTokens?: number;
timeout?: number;
truncationStrategy?: string;
networkMode?: string;
subnets?: string;
securityGroups?: string;
idleTimeout?: number;
maxLifetime?: number;
sessionStorage?: string;
withInvokeScript?: boolean;
authorizerType?: string;
discoveryUrl?: string;
allowedAudience?: string;
allowedClients?: string;
allowedScopes?: string;
customClaims?: string;
clientId?: string;
clientSecret?: string;
json?: boolean;
}) => {
try {
if (!findConfigRoot()) {
console.error('No agentcore project found. Run `agentcore create` first.');
process.exit(1);
}
// Validate auth options
const { validateAddHarnessOptions } = await import('../commands/add/validate');
const authValidation = validateAddHarnessOptions({
...cliOptions,
authorizerType: cliOptions.authorizerType as RuntimeAuthorizerType | undefined,
});
if (!authValidation.valid) {
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error: authValidation.error }));
} else {
console.error(authValidation.error);
}
process.exit(1);
}
if (cliOptions.name || cliOptions.json) {
if (!cliOptions.name) {
const error = '--name is required';
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error }));
} else {
console.error(error);
}
process.exit(1);
}
const { DEFAULT_MODEL_IDS } = await import('../tui/screens/harness/types');
const provider = (cliOptions.modelProvider ?? 'bedrock') as HarnessModelProvider;
const modelId = cliOptions.modelId ?? DEFAULT_MODEL_IDS[provider];
const containerOption = this.parseContainerFlag(cliOptions.container);
const result = await this.add({
name: cliOptions.name,
modelProvider: provider,
modelId,
apiKeyArn: cliOptions.apiKeyArn,
containerUri: containerOption.containerUri,
dockerfilePath: containerOption.dockerfilePath,
skipMemory: cliOptions.memory === false,
maxIterations: cliOptions.maxIterations,
maxTokens: cliOptions.maxTokens,
timeoutSeconds: cliOptions.timeout,
truncationStrategy: cliOptions.truncationStrategy as 'sliding_window' | 'summarization' | undefined,
networkMode: cliOptions.networkMode as NetworkMode | undefined,
subnets: cliOptions.subnets?.split(',').map(s => s.trim()),
securityGroups: cliOptions.securityGroups?.split(',').map(s => s.trim()),
idleTimeout: cliOptions.idleTimeout,
maxLifetime: cliOptions.maxLifetime,
sessionStoragePath: cliOptions.sessionStorage,
withInvokeScript: cliOptions.withInvokeScript,
authorizerType: cliOptions.authorizerType as RuntimeAuthorizerType | undefined,
jwtConfig:
cliOptions.authorizerType === 'CUSTOM_JWT' && cliOptions.discoveryUrl
? {
discoveryUrl: cliOptions.discoveryUrl,
allowedAudience: cliOptions.allowedAudience?.split(',').map(s => s.trim()),
allowedClients: cliOptions.allowedClients?.split(',').map(s => s.trim()),
allowedScopes: cliOptions.allowedScopes?.split(',').map(s => s.trim()),
customClaims: cliOptions.customClaims
? (JSON.parse(cliOptions.customClaims) as JwtConfigOptions['customClaims'])
: undefined,
clientId: cliOptions.clientId,
clientSecret: cliOptions.clientSecret,
}
: undefined,
});
if (!result.success) {
if (cliOptions.json) {
console.log(JSON.stringify(result));
} else {
console.error(result.error);
}
process.exit(1);
}
if (cliOptions.json) {
console.log(JSON.stringify(result));
} else {
console.log(`Added harness '${result.harnessName}'.`);
}
process.exit(0);
} else {
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: 'harness' as const,
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;
}
parseContainerFlag(value?: string): { containerUri?: string; dockerfilePath?: string } {
if (!value) return {};
// Treat as Dockerfile if it uses a relative path prefix or ends with a
// Dockerfile extension. Bare absolute paths like /my-org/image:tag are
// valid container URIs so we don't match on leading / alone.
const looksLikeDockerfile =
value.endsWith('Dockerfile') ||
value.endsWith('.dockerfile') ||
value.startsWith('./') ||
value.startsWith('../');
if (looksLikeDockerfile) {
return { dockerfilePath: value };
}
return { containerUri: value };
}
private buildLifecycleConfig(options: { idleTimeout?: number; maxLifetime?: number }) {
if (options.idleTimeout === undefined && options.maxLifetime === undefined) return undefined;
return {
...(options.idleTimeout !== undefined && { idleRuntimeSessionTimeout: options.idleTimeout }),
...(options.maxLifetime !== undefined && { maxLifetime: options.maxLifetime }),
};
}
}