forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.tsx
More file actions
527 lines (492 loc) · 21.4 KB
/
Copy pathcommand.tsx
File metadata and controls
527 lines (492 loc) · 21.4 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
import { ValidationError, serializeResult } from '../../../lib';
import { COMMAND_DESCRIPTIONS } from '../../constants';
import { getErrorMessage } from '../../errors';
import { ADDITIONAL_PARAMS_JSON_ERROR } from '../../primitives/constants';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { renderTUI } from '../../tui';
import { requireProject, requireTTY } from '../../tui/guards';
import { parseHeaderFlags } from '../shared/header-utils';
import { type InvokeContext, handleHarnessInvokeByArn, handleInvoke, loadInvokeConfig } from './action';
import { resolvePrompt } from './resolve-prompt';
import type { InvokeOptions, InvokeResult } from './types';
import { computeInvokeAttrs } from './utils';
import { validateInvokeOptions } from './validate';
import type { Command } from '@commander-js/extra-typings';
import { Text, render } from 'ink';
const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
function startSpinner(message: string): NodeJS.Timeout {
let i = 0;
process.stderr.write(`${SPINNER_FRAMES[0]} ${message}`);
return setInterval(() => {
i = (i + 1) % SPINNER_FRAMES.length;
process.stderr.write(`\r${SPINNER_FRAMES[i]} ${message}`);
}, 80);
}
function stopSpinner(spinner: NodeJS.Timeout): void {
clearInterval(spinner);
process.stderr.write('\r\x1b[K'); // Clear line
}
async function handleInvokeCLI(options: InvokeOptions, preloadedContext?: InvokeContext): Promise<InvokeResult> {
const validation = validateInvokeOptions(options);
if (!validation.valid) {
return { success: false, error: new ValidationError(validation.error ?? 'Validation failed') };
}
let spinner: NodeJS.Timeout | undefined;
try {
// Direct harness invoke by ARN (no project required)
if (options.harnessArn) {
const region = options.region ?? process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION;
if (!region) {
const msg = '--region is required with --harness-arn (or set AWS_REGION)';
if (options.json) {
console.log(JSON.stringify({ success: false, error: msg }));
} else {
console.error(msg);
}
process.exit(1);
}
return handleHarnessInvokeByArn(options.harnessArn, region, options);
}
const context = preloadedContext ?? (await loadInvokeConfig());
// Show spinner for non-streaming, non-json, non-exec invocations
// Harness invoke always streams directly to stdout, so skip spinner for harness
const isHarness =
options.harnessName != null ||
((context.project.harnesses ?? []).length > 0 && context.project.runtimes.length === 0);
if (!options.stream && !options.json && !options.exec && !isHarness) {
spinner = startSpinner('Invoking agent...');
}
const result = await handleInvoke(context, options);
if (spinner) {
stopSpinner(spinner);
}
return result;
} catch (err) {
if (spinner) {
stopSpinner(spinner);
}
throw err;
}
}
export function redactSensitiveText(value: string): string {
return (
value
// AgentCore inbound bearer tokens are always CUSTOM_JWT/OIDC JWTs, and a JWT always begins
// with `eyJ` (base64url of `{"`, the start of its header/payload JSON). Matching by shape
// redacts the token wherever it appears and never touches prose like "bearer token".
// See https://stackoverflow.com/a/74181595 (why JWTs start with `eyJ`).
.replace(/eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g, '[REDACTED]')
.replace(/(client[_-]?secret["']?\s*[:=]\s*["']?)([^"',\s}]+)/gi, '$1[REDACTED]')
.replace(/((?:access[_-]?)?token["']?\s*[:=]\s*["']?)([^"',\s}]+)/gi, '$1[REDACTED]')
);
}
export function printInvokeResult(result: InvokeResult, options: InvokeOptions): void {
// Resume continuity only works when the target has memory; without it the
// agent is stateless per turn, so promising "To resume" would be misleading.
const printSession = (): void => {
if (!result.sessionId) return;
console.error(`\nSession: ${result.sessionId}`);
if (result.hasMemory) {
console.error(`To resume: agentcore invoke --session-id ${result.sessionId}`);
}
};
if (options.json) {
const serialized = serializeResult(result);
if (typeof serialized.response === 'string') serialized.response = redactSensitiveText(serialized.response);
if (typeof serialized.error === 'string') serialized.error = redactSensitiveText(serialized.error);
console.log(JSON.stringify(serialized));
} else if (options.stream) {
printSession();
if (result.logFilePath) {
console.error(`Log: ${result.logFilePath}`);
}
} else {
if (result.success && result.response) {
console.log(redactSensitiveText(result.response));
} else if (!result.success && result.error) {
console.error(redactSensitiveText(result.error.message));
}
printSession();
if (result.logFilePath) {
console.error(`Log: ${result.logFilePath}`);
}
}
}
export const registerInvoke = (program: Command) => {
const invokeCmd = program
.command('invoke')
.alias('i')
.description(COMMAND_DESCRIPTIONS.invoke)
.argument(
'[prompt]',
'Prompt to send to the agent. Also accepts piped stdin when no prompt is provided and stdin is not a TTY [non-interactive]'
)
.option('--prompt <text>', 'Prompt to send to the agent [non-interactive]')
.option(
'--prompt-file <path>',
'Read the prompt from a file (for long or structured payloads that exceed shell arg limits) [non-interactive]'
)
.option('--runtime <name>', 'Select specific runtime [non-interactive]')
.option('--gateway <name>', 'Invoke through a gateway [non-interactive]')
.option('--gateway-target-name <name>', 'HTTP runtime target on the gateway [non-interactive]')
.option('--target <name>', 'Select deployment target [non-interactive]')
.option(
'--session-id <id>',
'Use a specific session ID (groups turns; continuity requires memory — see `agentcore add memory`)'
)
.option('--user-id <id>', 'User ID for runtime invocation (default: "default-user")')
.option('--json', 'Output as JSON [non-interactive]')
.option('--stream', 'Stream response in real-time (TUI streams by default) [non-interactive]')
.option('--tool <name>', 'MCP tool name (use with "call-tool" prompt) [non-interactive]')
.option('--input <json>', 'MCP tool arguments as JSON (use with --tool) [non-interactive]')
.option(
'--exec',
'Execute a shell command in the runtime container (use `agentcore exec` instead) [non-interactive]'
)
.option('--timeout <seconds>', 'Timeout in seconds for --exec commands [non-interactive]', parseInt)
.option(
'-H, --header <header>',
'Custom header to forward to the agent (format: "Name: Value", repeatable) [non-interactive]',
(val: string, prev: string[]) => [...prev, val],
[] as string[]
)
.option('--bearer-token <token>', 'Bearer token for CUSTOM_JWT auth (bypasses SigV4) [non-interactive]')
.option('--payment-instrument-id <id>', 'Payment instrument ID for x402 payments [non-interactive]')
.option('--payment-session-id <id>', 'Payment session ID for budget tracking [non-interactive]')
.option('--auto-session', 'Auto-create/reuse a payment session for testing [non-interactive]')
.option(
'--payment-user-id <id>',
'End-user identity (wallet owner) for payments; scopes the instrument/session/budget. Defaults to --user-id when omitted.'
);
invokeCmd
.option('--harness <name>', 'Select specific harness to invoke [non-interactive]')
.option('--harness-arn <arn>', 'Invoke a harness by ARN (no project required) [non-interactive]')
.option('--region <region>', 'AWS region (required with --harness-arn when no project) [non-interactive]')
.option('--verbose', 'Print verbose streaming JSON events (harness only) [non-interactive]')
.option('--model-id <id>', 'Override model for this invocation (harness only) [non-interactive]')
.option(
'--model-provider <provider>',
'Override model provider: bedrock, open_ai, gemini, lite_llm (harness only) [non-interactive]'
)
.option('--api-key-arn <arn>', 'Override API key ARN for open_ai/gemini (harness only) [non-interactive]')
.option('--api-base <url>', 'Override LiteLLM API base URL (harness only, lite_llm) [non-interactive]')
.option(
'--additional-params <json>',
'Override LiteLLM additional params as a JSON object (harness only, lite_llm) [non-interactive]'
)
.option('--tools <tools>', 'Override tools, comma-separated (harness only) [non-interactive]')
.option('--max-iterations <n>', 'Override max iterations (harness only) [non-interactive]', parseInt)
.option('--max-tokens <n>', 'Override max tokens (harness only) [non-interactive]', parseInt)
.option('--harness-timeout <seconds>', 'Override timeout seconds (harness only) [non-interactive]', parseInt)
.option(
'--skills <sources>',
'Skills override, comma-separated (path, s3://uri, or https://git-url). Git auth not supported here — configure via agentcore add skill [non-interactive]'
)
.option('--system-prompt <text>', 'Override system prompt (harness only) [non-interactive]')
.option('--allowed-tools <tools>', 'Override allowed tools, comma-separated (harness only) [non-interactive]')
.option('--actor-id <id>', 'Override memory actor ID (harness only) [non-interactive]');
// Group the long flag list into labelled sections (mirrors `add ab-test`).
// Core flags (prompt/prompt-file/runtime/target/session-id/user-id) stay in the
// default "Options:" block; everything else is hidden there and re-listed under a
// section heading below.
const hiddenFromDefaultHelp = new Set<string>([
// Payments
'--payment-user-id',
'--payment-instrument-id',
'--payment-session-id',
'--auto-session',
// Output
'--json',
'--stream',
// MCP & advanced
'--tool',
'--input',
'--exec',
'--timeout',
'--header',
'--bearer-token',
// Harness + model overrides (preview)
'--harness',
'--harness-arn',
'--region',
'--verbose',
'--model-id',
'--model-provider',
'--api-key-arn',
'--tools',
'--max-iterations',
'--max-tokens',
'--harness-timeout',
'--skills',
'--system-prompt',
'--allowed-tools',
'--actor-id',
]);
for (const opt of invokeCmd.options) {
if (hiddenFromDefaultHelp.has(opt.long ?? '')) {
opt.hidden = true;
}
}
invokeCmd.addHelpText(
'after',
`
Payments [non-interactive]
--payment-user-id <id> End-user/wallet-owner identity (defaults to --user-id)
--payment-instrument-id <id> Payment instrument (wallet) ID
--payment-session-id <id> Payment session ID for budget tracking
--auto-session Auto-create/reuse a payment session for testing
Output [non-interactive]
--json Output as JSON
--stream Stream response in real-time
MCP & Advanced [non-interactive]
--tool <name> MCP tool name (use with "call-tool" prompt)
--input <json> MCP tool arguments as JSON (use with --tool)
--exec Execute a shell command in the runtime container
--timeout <seconds> Timeout in seconds for --exec commands
-H, --header <header> Custom header "Name: Value" (repeatable)
--bearer-token <token> Bearer token for CUSTOM_JWT auth (bypasses SigV4)
`
);
invokeCmd.addHelpText(
'after',
`
Harness [non-interactive]
--harness <name> Select specific harness to invoke
--harness-arn <arn> Invoke a harness by ARN (no project required)
--region <region> AWS region (required with --harness-arn)
--verbose Print verbose streaming JSON events
Model & Runtime Overrides (harness only) [non-interactive]
--model-id <id> Override model
--model-provider <provider> bedrock, open_ai, or gemini
--api-key-arn <arn> API key ARN for open_ai/gemini
--tools <tools> Override tools (comma-separated)
--allowed-tools <tools> Override allowed tools (comma-separated)
--skills <paths> Skills (comma-separated paths)
--system-prompt <text> Override system prompt
--actor-id <id> Override memory actor ID
--max-iterations <n> Override max iterations
--max-tokens <n> Override max tokens
--harness-timeout <seconds> Override timeout seconds
`
);
invokeCmd.action(
async (
positionalPrompt: string | undefined,
cliOptions: {
prompt?: string;
promptFile?: string;
runtime?: string;
gateway?: string;
gatewayTargetName?: string;
target?: string;
sessionId?: string;
userId?: string;
json?: boolean;
stream?: boolean;
tool?: string;
input?: string;
exec?: boolean;
timeout?: number;
header?: string[];
bearerToken?: string;
harness?: string;
harnessArn?: string;
region?: string;
verbose?: boolean;
modelId?: string;
modelProvider?: string;
apiKeyArn?: string;
apiBase?: string;
additionalParams?: string;
tools?: string;
maxIterations?: number;
maxTokens?: number;
harnessTimeout?: number;
skills?: string;
systemPrompt?: string;
allowedTools?: string;
actorId?: string;
paymentInstrumentId?: string;
paymentSessionId?: string;
autoSession?: boolean;
paymentUserId?: string;
}
) => {
try {
// Skip requireProject when --harness-arn provided
if (!cliOptions.harnessArn) {
requireProject();
}
// Load config once for protocol resolution and to pass into handleInvokeCLI
let invokeContext: InvokeContext | undefined;
let agentProtocol: string | undefined;
try {
invokeContext = await loadInvokeConfig();
const agent = cliOptions.runtime
? invokeContext.project.runtimes.find(a => a.name === cliOptions.runtime)
: invokeContext.project.runtimes[0];
agentProtocol = agent?.protocol;
} catch {
// Config load failure will be caught again inside handleInvokeCLI
}
// Resolve prompt from flag / positional / --prompt-file / stdin
const resolved = await resolvePrompt({
flag: cliOptions.prompt,
positional: positionalPrompt,
file: cliOptions.promptFile,
stdinPiped: !process.stdin.isTTY,
});
// CLI mode if any CLI-specific options provided, prompt resolved, or prompt resolution failed
// (follows deploy command pattern)
if (
!resolved.success ||
resolved.prompt !== undefined ||
cliOptions.json ||
cliOptions.target ||
cliOptions.gatewayTargetName ||
cliOptions.stream ||
cliOptions.runtime ||
cliOptions.gateway ||
cliOptions.tool ||
cliOptions.exec ||
cliOptions.bearerToken ||
cliOptions.harness ||
cliOptions.harnessArn ||
cliOptions.verbose
// Payment params (--auto-session / --payment-instrument-id /
// --payment-session-id / --payment-user-id) do NOT force CLI mode on
// their own — with a prompt they run one-shot (resolved.prompt above),
// without a prompt they carry into the interactive TUI. --auto-session
// mints/reuses a session at TUI start; see useInvokeFlow.
) {
const result = await withCommandRunTelemetry(
'invoke',
computeInvokeAttrs({
harnessName: cliOptions.harness,
harnessArn: cliOptions.harnessArn,
harnessCount: invokeContext?.project.harnesses?.length ?? 0,
runtimeCount: invokeContext?.project.runtimes.length ?? 0,
stream: cliOptions.stream ?? false,
hasSessionId: !!cliOptions.sessionId,
bearerToken: cliOptions.bearerToken,
agentProtocol: agentProtocol ?? (cliOptions.tool ? 'mcp' : undefined),
}),
async (): Promise<InvokeResult> => {
if (!resolved.success) {
return { success: false, error: new ValidationError(resolved.error ?? 'Prompt resolution failed') };
}
// Parse custom headers
let headers: Record<string, string> | undefined;
if (cliOptions.header && cliOptions.header.length > 0) {
headers = parseHeaderFlags(cliOptions.header);
}
let additionalParams: Record<string, unknown> | undefined;
if (cliOptions.additionalParams) {
try {
additionalParams = JSON.parse(cliOptions.additionalParams) as Record<string, unknown>;
} catch {
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error: ADDITIONAL_PARAMS_JSON_ERROR }));
} else {
console.error(ADDITIONAL_PARAMS_JSON_ERROR);
}
process.exit(1);
}
}
const options: InvokeOptions = {
prompt: resolved.prompt,
agentName: cliOptions.runtime,
gateway: cliOptions.gateway,
gatewayTarget: cliOptions.gatewayTargetName,
targetName: cliOptions.target ?? 'default',
sessionId: cliOptions.sessionId,
userId: cliOptions.userId,
json: cliOptions.json,
stream: cliOptions.stream,
tool: cliOptions.tool,
input: cliOptions.input,
exec: cliOptions.exec,
timeout: cliOptions.timeout,
headers,
bearerToken: cliOptions.bearerToken,
harnessName: cliOptions.harness,
harnessArn: cliOptions.harnessArn,
region: cliOptions.region,
verbose: cliOptions.verbose,
modelId: cliOptions.modelId,
modelProvider: cliOptions.modelProvider,
apiKeyArn: cliOptions.apiKeyArn,
apiBase: cliOptions.apiBase,
additionalParams,
tools: cliOptions.tools,
maxIterations: cliOptions.maxIterations,
maxTokens: cliOptions.maxTokens,
harnessTimeout: cliOptions.harnessTimeout,
skills: cliOptions.skills,
systemPrompt: cliOptions.systemPrompt,
allowedTools: cliOptions.allowedTools,
actorId: cliOptions.actorId,
paymentInstrumentId: cliOptions.paymentInstrumentId,
paymentSessionId: cliOptions.paymentSessionId,
autoSession: cliOptions.autoSession,
paymentUserId: cliOptions.paymentUserId,
};
return handleInvokeCLI(options, invokeContext);
}
);
printInvokeResult(result, {
json: cliOptions.json,
stream: cliOptions.stream,
});
process.exit(result.exitCode ?? (result.success ? 0 : 1));
} else {
// No CLI options - interactive TUI mode (headers still passed if provided)
// Validate flag combinations BEFORE the TTY check: a conflicting
// --auto-session + --payment-session-id is wrong regardless of terminal,
// and the flag-conflict message is clearer than the TTY guard. Single
// source of truth: validateInvokeOptions.
const validation = validateInvokeOptions({
autoSession: cliOptions.autoSession,
paymentSessionId: cliOptions.paymentSessionId,
});
if (!validation.valid) {
console.error(validation.error);
process.exit(1);
}
requireTTY();
// Parse custom headers for TUI mode
let headers: Record<string, string> | undefined;
if (cliOptions.header && cliOptions.header.length > 0) {
headers = parseHeaderFlags(cliOptions.header);
}
await renderTUI({
initialRoute: {
name: 'invoke',
sessionId: cliOptions.sessionId,
userId: cliOptions.userId,
headers,
bearerToken: cliOptions.bearerToken,
paymentInstrumentId: cliOptions.paymentInstrumentId,
paymentSessionId: cliOptions.paymentSessionId,
autoSession: cliOptions.autoSession,
// Default the payments wallet-owner identity to --user-id when
// --payment-user-id is omitted (same fallback as the command path).
paymentUserId: cliOptions.paymentUserId ?? cliOptions.userId,
},
enterAltScreen: false,
actionOnBack: 'exit',
isInteractive: false,
});
}
} catch (error) {
const msg = redactSensitiveText(getErrorMessage(error));
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error: msg }));
} else {
render(<Text color="red">Error: {msg}</Text>);
}
process.exit(1);
}
}
);
};