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
256 lines (225 loc) · 8.8 KB
/
Copy pathcommand.tsx
File metadata and controls
256 lines (225 loc) · 8.8 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
import { getErrorMessage } from '../../errors';
import { COMMAND_DESCRIPTIONS } from '../../tui/copy';
import { requireProject } from '../../tui/guards';
import type { ResourceStatusEntry } from './action';
import { handleProjectStatus, handleRuntimeLookup, loadStatusConfig } from './action';
import { DEPLOYMENT_STATE_COLORS, DEPLOYMENT_STATE_LABELS } from './constants';
import type { Command } from '@commander-js/extra-typings';
import { Box, Text, render } from 'ink';
const VALID_RESOURCE_TYPES = [
'agent',
'memory',
'credential',
'gateway',
'evaluator',
'online-eval',
'policy-engine',
'policy',
] as const;
const VALID_STATES = ['deployed', 'local-only', 'pending-removal'] as const;
interface StatusCliOptions {
runtimeId?: string;
target?: string;
type?: string;
state?: string;
runtime?: string;
json?: boolean;
}
function filterResources(
resources: ResourceStatusEntry[],
options: { type?: string; state?: string; runtime?: string }
): ResourceStatusEntry[] {
let filtered = resources;
if (options.type) {
filtered = filtered.filter(r => r.resourceType === options.type);
}
if (options.state) {
filtered = filtered.filter(r => r.deploymentState === options.state);
}
if (options.runtime) {
filtered = filtered.filter(r => r.resourceType !== 'agent' || r.name === options.runtime);
}
return filtered;
}
export const registerStatus = (program: Command) => {
program
.command('status')
.alias('s')
.description(COMMAND_DESCRIPTIONS.status)
.option('--runtime-id <id>', 'Look up a specific runtime by ID')
.option('--target <name>', 'Select deployment target')
.option(
'--type <type>',
'Filter by resource type (agent, memory, credential, gateway, evaluator, online-eval, policy-engine, policy)'
)
.option('--state <state>', 'Filter by deployment state (deployed, local-only, pending-removal)')
.option('--runtime <name>', 'Filter to a specific runtime')
.option('--json', 'Output as JSON')
.action(async (cliOptions: StatusCliOptions) => {
requireProject();
// Validate --type
if (cliOptions.type && !(VALID_RESOURCE_TYPES as readonly string[]).includes(cliOptions.type)) {
render(
<Text color="red">
Invalid resource type '{cliOptions.type}'. Valid types: {VALID_RESOURCE_TYPES.join(', ')}
</Text>
);
return;
}
// Validate --state
if (cliOptions.state && !(VALID_STATES as readonly string[]).includes(cliOptions.state)) {
render(
<Text color="red">
Invalid state '{cliOptions.state}'. Valid states: {VALID_STATES.join(', ')}
</Text>
);
return;
}
try {
const context = await loadStatusConfig();
// Direct runtime lookup by ID
if (cliOptions.runtimeId) {
const result = await handleRuntimeLookup(context, {
agentRuntimeId: cliOptions.runtimeId,
targetName: cliOptions.target,
});
if (cliOptions.json) {
console.log(JSON.stringify(result, null, 2));
return;
}
if (!result.success) {
render(<Text color="red">{result.error}</Text>);
return;
}
const runtimeStatus = result.runtimeStatus ? `Runtime status: ${result.runtimeStatus}` : '';
render(
<Text>
AgentCore Status - {result.runtimeId} (target: {result.targetName})
{runtimeStatus ? ` - ${runtimeStatus}` : ''}
</Text>
);
return;
}
// Default path: show all resource types with deployment state
const result = await handleProjectStatus(context, {
targetName: cliOptions.target,
});
if (cliOptions.json) {
const filtered = filterResources(result.resources, cliOptions);
console.log(JSON.stringify({ ...result, resources: filtered }, null, 2));
return;
}
if (!result.success) {
render(<Text color="red">{result.error}</Text>);
return;
}
const filtered = filterResources(result.resources, cliOptions);
const agents = filtered.filter(r => r.resourceType === 'agent');
const credentials = filtered.filter(r => r.resourceType === 'credential');
const memories = filtered.filter(r => r.resourceType === 'memory');
const gateways = filtered.filter(r => r.resourceType === 'gateway');
const evaluators = filtered.filter(r => r.resourceType === 'evaluator');
const onlineEvals = filtered.filter(r => r.resourceType === 'online-eval');
const policyEngines = filtered.filter(r => r.resourceType === 'policy-engine');
const policies = filtered.filter(r => r.resourceType === 'policy');
render(
<Box flexDirection="column">
<Text bold>
AgentCore Status (target: {result.targetName}
{result.targetRegion ? `, ${result.targetRegion}` : ''})
</Text>
{agents.length > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text bold>Agents</Text>
{agents.map(entry => (
<Box key={`${entry.resourceType}-${entry.name}`} flexDirection="column">
<ResourceEntry entry={entry} showRuntime />
{entry.invocationUrl && (
<Text dimColor>
{' '}URL: {entry.invocationUrl}
</Text>
)}
</Box>
))}
</Box>
)}
{memories.length > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text bold>Memories</Text>
{memories.map(entry => (
<ResourceEntry key={`${entry.resourceType}-${entry.name}`} entry={entry} />
))}
</Box>
)}
{credentials.length > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text bold>Credentials</Text>
{credentials.map(entry => (
<ResourceEntry key={`${entry.resourceType}-${entry.name}`} entry={entry} />
))}
</Box>
)}
{gateways.length > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text bold>Gateways</Text>
{gateways.map(entry => (
<ResourceEntry key={`${entry.resourceType}-${entry.name}`} entry={entry} />
))}
</Box>
)}
{evaluators.length > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text bold>Evaluators</Text>
{evaluators.map(entry => (
<ResourceEntry key={`${entry.resourceType}-${entry.name}`} entry={entry} />
))}
</Box>
)}
{onlineEvals.length > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text bold>Online Eval Configs</Text>
{onlineEvals.map(entry => (
<ResourceEntry key={`${entry.resourceType}-${entry.name}`} entry={entry} />
))}
</Box>
)}
{policyEngines.length > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text bold>Policy Engines</Text>
{policyEngines.map(entry => (
<ResourceEntry key={`${entry.resourceType}-${entry.name}`} entry={entry} />
))}
</Box>
)}
{policies.length > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text bold>Policies</Text>
{policies.map(entry => (
<ResourceEntry key={`${entry.resourceType}-${entry.detail}-${entry.name}`} entry={entry} />
))}
</Box>
)}
{filtered.length === 0 && <Text dimColor>No resources match the given filters.</Text>}
</Box>
);
} catch (error) {
render(<Text color="red">Error: {getErrorMessage(error)}</Text>);
process.exit(1);
}
});
};
function ResourceEntry({ entry, showRuntime }: { entry: ResourceStatusEntry; showRuntime?: boolean }) {
return (
<Text>
{' '}
{entry.name}:{' '}
<Text color={DEPLOYMENT_STATE_COLORS[entry.deploymentState] ?? 'gray'}>
{DEPLOYMENT_STATE_LABELS[entry.deploymentState] ?? entry.deploymentState}
</Text>
{entry.detail &&
(showRuntime ? <Text> - Runtime: {entry.detail}</Text> : <Text dimColor> ({entry.detail})</Text>)}
{entry.identifier && <Text dimColor> ({entry.identifier})</Text>}
{entry.error && <Text color="red"> - Error: {entry.error}</Text>}
</Text>
);
}