forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
216 lines (186 loc) · 6.69 KB
/
cli.ts
File metadata and controls
216 lines (186 loc) · 6.69 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
import { registerAdd } from './commands/add';
import { registerCreate } from './commands/create';
import { registerDeploy } from './commands/deploy';
import { registerDev } from './commands/dev';
import { registerEval } from './commands/eval';
import { registerFetch } from './commands/fetch';
import { registerHelp } from './commands/help';
import { registerImport } from './commands/import';
import { registerInvoke } from './commands/invoke';
import { registerLogs } from './commands/logs';
import { registerPackage } from './commands/package';
import { registerPause } from './commands/pause';
import { registerRemove } from './commands/remove';
import { registerResume } from './commands/resume';
import { registerRun } from './commands/run';
import { registerStatus } from './commands/status';
import { registerTelemetry } from './commands/telemetry';
import { registerTraces } from './commands/traces';
import { registerUpdate } from './commands/update';
import { registerValidate } from './commands/validate';
import { PACKAGE_VERSION } from './constants';
import { getOrCreateInstallationId } from './global-config';
import { ALL_PRIMITIVES } from './primitives';
import { App } from './tui/App';
import { LayoutProvider } from './tui/context';
import { COMMAND_DESCRIPTIONS } from './tui/copy';
import { clearExitMessage, getExitMessage } from './tui/exit-message';
import { CommandListScreen } from './tui/screens/home';
import { getCommandsForUI } from './tui/utils';
import { type UpdateCheckResult, checkForUpdate, printUpdateNotification } from './update-notifier';
import { Command } from '@commander-js/extra-typings';
import { render } from 'ink';
import React from 'react';
// ANSI escape sequences
const ENTER_ALT_SCREEN = '\x1B[?1049h\x1B[H';
const EXIT_ALT_SCREEN = '\x1B[?1049l';
const SHOW_CURSOR = '\x1B[?25h';
// Track if we're in alternate screen mode
let inAltScreen = false;
/**
* Global terminal cleanup - ensures cursor is always restored on exit.
* Registered once at startup, catches all exit scenarios.
*/
function setupGlobalCleanup() {
const cleanup = () => {
if (inAltScreen) {
process.stdout.write(EXIT_ALT_SCREEN);
}
process.stdout.write(SHOW_CURSOR);
};
process.on('exit', cleanup);
process.on('SIGINT', () => {
cleanup();
process.exit(0);
});
process.on('SIGTERM', () => {
cleanup();
process.exit(0);
});
}
function printTelemetryNotice(): void {
const yellow = '\x1b[33m';
const reset = '\x1b[0m';
process.stderr.write(
[
'',
`${yellow}The AgentCore CLI will soon begin collecting aggregated, anonymous usage`,
'analytics to help improve the tool.',
'To opt out: agentcore telemetry disable',
`To learn more: agentcore telemetry --help${reset}`,
'',
'',
].join('\n')
);
}
function printPostCommandNotices(isFirstRun: boolean, updateCheck: Promise<UpdateCheckResult | null>): Promise<void> {
if (isFirstRun) {
printTelemetryNotice();
}
return updateCheck.then(result => {
if (result?.updateAvailable) {
printUpdateNotification(result);
}
});
}
/**
* Render the TUI in alternate screen buffer mode.
*/
function renderTUI(updateCheck: Promise<UpdateCheckResult | null>, isFirstRun: boolean) {
inAltScreen = true;
process.stdout.write(ENTER_ALT_SCREEN);
const { waitUntilExit } = render(React.createElement(App));
void waitUntilExit().then(async () => {
inAltScreen = false;
process.stdout.write(EXIT_ALT_SCREEN);
process.stdout.write(SHOW_CURSOR);
// Print any exit message set by screens (e.g., after successful project creation)
const exitMessage = getExitMessage();
if (exitMessage) {
console.log(exitMessage);
clearExitMessage();
}
await printPostCommandNotices(isFirstRun, updateCheck);
});
}
function renderHelp(program: Command): void {
const commands = getCommandsForUI(program);
render(React.createElement(LayoutProvider, null, React.createElement(CommandListScreen, { commands })));
}
export function createProgram(): Command {
const program = new Command();
program
.name('agentcore')
.description(COMMAND_DESCRIPTIONS.program)
.version(PACKAGE_VERSION)
.showHelpAfterError()
.showSuggestionAfterError();
// Custom help only for main program
program.addHelpCommand(false); // Disable default help subcommand
program.helpOption('-h, --help', 'Display help');
// Override help action for main program only
program.on('option:help', () => {
renderHelp(program);
process.exit(0);
});
registerCommands(program);
// Add help footer to all subcommands explaining interactive vs non-interactive
const helpFooter =
'\nRun without flags for interactive mode. Flags marked [non-interactive] trigger CLI mode.\nRun `agentcore help modes` for details.';
program.commands.forEach(cmd => {
cmd.addHelpText('after', helpFooter);
// Also add to nested subcommands (e.g., add agent, remove agent)
cmd.commands.forEach(subcmd => {
subcmd.addHelpText('after', helpFooter);
});
});
return program;
}
export function registerCommands(program: Command) {
const addCmd = registerAdd(program);
registerDev(program);
registerDeploy(program);
registerCreate(program);
registerEval(program);
registerFetch(program);
registerHelp(program);
registerImport(program);
registerInvoke(program);
registerLogs(program);
registerPackage(program);
registerPause(program);
const removeCmd = registerRemove(program);
registerResume(program);
registerRun(program);
registerStatus(program);
registerTelemetry(program);
registerTraces(program);
registerUpdate(program);
registerValidate(program);
// Register primitive subcommands (add agent, remove agent, add memory, etc.)
for (const primitive of ALL_PRIMITIVES) {
primitive.registerCommands(addCmd, removeCmd);
}
}
export const main = async (argv: string[]) => {
// Register global cleanup handlers once at startup
setupGlobalCleanup();
// Generate installationId on first run and show telemetry notice
const { created: isFirstRun } = await getOrCreateInstallationId();
const program = createProgram();
const args = argv.slice(2);
// Fire off non-blocking update check (skip for `update` command)
const isUpdateCommand = args[0] === 'update';
const updateCheck = isUpdateCommand ? Promise.resolve(null) : checkForUpdate();
// Show TUI for no arguments, commander handles --help via configureHelp()
if (args.length === 0) {
renderTUI(updateCheck, isFirstRun);
return;
}
if (isFirstRun) {
printTelemetryNotice();
}
await program.parseAsync(argv);
// Telemetry notice already printed above; only run update check here.
await printPostCommandNotices(false, updateCheck);
};