forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.tsx
More file actions
193 lines (179 loc) · 6.79 KB
/
Copy pathcommand.tsx
File metadata and controls
193 lines (179 loc) · 6.79 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
import { getErrorMessage } from '../../errors';
import { COMMAND_DESCRIPTIONS } from '../../tui/copy';
import { requireProject } from '../../tui/guards';
import { InvokeScreen } from '../../tui/screens/invoke';
import { parseHeaderFlags } from '../shared/header-utils';
import { handleInvoke, loadInvokeConfig } from './action';
import type { InvokeOptions } from './types';
import { validateInvokeOptions } from './validate';
import type { Command } from '@commander-js/extra-typings';
import { Text, render } from 'ink';
import React from 'react';
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): Promise<void> {
const validation = validateInvokeOptions(options);
if (!validation.valid) {
if (options.json) {
console.log(JSON.stringify({ success: false, error: validation.error }));
} else {
console.error(validation.error);
}
process.exit(1);
}
let spinner: NodeJS.Timeout | undefined;
try {
const context = await loadInvokeConfig();
// Show spinner for non-streaming, non-json, non-exec invocations
if (!options.stream && !options.json && !options.exec) {
spinner = startSpinner('Invoking agent...');
}
const result = await handleInvoke(context, options);
if (spinner) {
stopSpinner(spinner);
}
if (options.json) {
console.log(JSON.stringify(result));
} else if (options.stream) {
// Streaming already wrote to stdout, just show log path
if (result.logFilePath) {
console.error(`\nLog: ${result.logFilePath}`);
}
} else {
// Non-streaming, non-json: print provider info and response or error
if (result.success && result.response) {
console.log(result.response);
} else if (!result.success && result.error) {
console.error(result.error);
}
if (result.logFilePath) {
console.error(`\nLog: ${result.logFilePath}`);
}
}
process.exit(result.success ? 0 : 1);
} catch (err) {
if (spinner) {
stopSpinner(spinner);
}
if (options.json) {
console.log(JSON.stringify({ success: false, error: getErrorMessage(err) }));
} else {
console.error(getErrorMessage(err));
}
process.exit(1);
}
}
export const registerInvoke = (program: Command) => {
program
.command('invoke')
.alias('i')
.description(COMMAND_DESCRIPTIONS.invoke)
.argument('[prompt]', 'Prompt to send to the agent [non-interactive]')
.option('--prompt <text>', 'Prompt to send to the agent [non-interactive]')
.option('--runtime <name>', 'Select specific runtime [non-interactive]')
.option('--target <name>', 'Select deployment target [non-interactive]')
.option('--session-id <id>', 'Use specific session ID for conversation continuity')
.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 [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]')
.action(
async (
positionalPrompt: string | undefined,
cliOptions: {
prompt?: string;
runtime?: string;
target?: string;
sessionId?: string;
userId?: string;
json?: boolean;
stream?: boolean;
tool?: string;
input?: string;
exec?: boolean;
timeout?: number;
header?: string[];
bearerToken?: string;
}
) => {
try {
requireProject();
// --prompt flag takes precedence over positional argument
const prompt = cliOptions.prompt ?? positionalPrompt;
// Parse custom headers
let headers: Record<string, string> | undefined;
if (cliOptions.header && cliOptions.header.length > 0) {
headers = parseHeaderFlags(cliOptions.header);
}
// CLI mode if any CLI-specific options provided (follows deploy command pattern)
if (
prompt ||
cliOptions.json ||
cliOptions.target ||
cliOptions.stream ||
cliOptions.runtime ||
cliOptions.tool ||
cliOptions.exec ||
cliOptions.bearerToken
) {
await handleInvokeCLI({
prompt,
agentName: cliOptions.runtime,
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,
});
} else {
// No CLI options - interactive TUI mode (headers still passed if provided)
const { waitUntilExit } = render(
<InvokeScreen
isInteractive={true}
onExit={() => process.exit(0)}
initialSessionId={cliOptions.sessionId}
initialUserId={cliOptions.userId}
initialHeaders={headers}
initialBearerToken={cliOptions.bearerToken}
/>
);
await waitUntilExit();
}
} catch (error) {
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error: getErrorMessage(error) }));
} else {
render(<Text color="red">Error: {getErrorMessage(error)}</Text>);
}
process.exit(1);
}
}
);
};