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
131 lines (121 loc) · 4.79 KB
/
Copy pathcommand.tsx
File metadata and controls
131 lines (121 loc) · 4.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
import { getErrorMessage } from '../../errors';
import { loadDeployedProjectConfig } from '../../operations/resolve-agent';
import { COMMAND_DESCRIPTIONS } from '../../tui/copy';
import { requireProject } from '../../tui/guards';
import { handleTracesGet, handleTracesList } from './action';
import type { TracesGetOptions, TracesListOptions } from './types';
import type { Command } from '@commander-js/extra-typings';
import { Box, Text, render } from 'ink';
function formatTimestamp(ts: string): string {
const num = Number(ts);
if (isNaN(num)) return ts;
// Epoch ms → human-readable
return new Date(num)
.toISOString()
.replace('T', ' ')
.replace(/\.\d+Z$/, 'Z');
}
export const registerTraces = (program: Command) => {
const traces = program.command('traces').alias('t').description(COMMAND_DESCRIPTIONS.traces);
traces
.command('list')
.description('List recent traces for a deployed runtime')
.option('--runtime <name>', 'Select specific runtime')
.option('--limit <n>', 'Maximum number of traces to display', '20')
.option('--since <time>', 'Start time — defaults to 12h ago (e.g. 5m, 1h, 2d, ISO 8601, epoch ms)')
.option('--until <time>', 'End time — defaults to now (e.g. now, 1h, ISO 8601, epoch ms)')
.action(async (cliOptions: TracesListOptions) => {
requireProject();
try {
const context = await loadDeployedProjectConfig();
const result = await handleTracesList(context, cliOptions);
if (!result.success) {
render(
<Box flexDirection="column">
<Text color="red">Error: {result.error}</Text>
{result.consoleUrl && <Text color="gray">Console: {result.consoleUrl}</Text>}
</Box>
);
process.exit(1);
return;
}
render(
<Box flexDirection="column">
<Text bold>
Traces for {result.agentName} (target: {result.targetName})
</Text>
<Text> </Text>
{result.traces && result.traces.length > 0 ? (
<>
<Box>
<Box width={34}>
<Text bold>Trace ID</Text>
</Box>
<Box width={22}>
<Text bold>Timestamp</Text>
</Box>
<Box width={38}>
<Text bold>Session ID</Text>
</Box>
</Box>
{result.traces.map((trace, i) => (
<Box key={i}>
<Box width={34}>
<Text color="cyan">{trace.traceId}</Text>
</Box>
<Box width={22}>
<Text>{formatTimestamp(trace.timestamp)}</Text>
</Box>
<Box width={38}>
<Text color="magenta">{trace.sessionId ?? '-'}</Text>
</Box>
</Box>
))}
</>
) : (
<Text color="yellow">No traces found in the specified time range.</Text>
)}
<Text> </Text>
{result.consoleUrl && <Text color="gray">Console: {result.consoleUrl}</Text>}
{result.consoleUrl && <Text dimColor>Note: Traces may take 2-3 minutes to appear in CloudWatch</Text>}
</Box>
);
} catch (error) {
render(<Text color="red">Error: {getErrorMessage(error)}</Text>);
process.exit(1);
}
});
traces
.command('get <traceId>')
.description('Download a trace to a JSON file')
.option('--runtime <name>', 'Select specific runtime')
.option('--output <path>', 'Output file path')
.option('--since <time>', 'Start time — defaults to 12h ago (e.g. 5m, 1h, 2d, ISO 8601, epoch ms)')
.option('--until <time>', 'End time — defaults to now (e.g. now, 1h, ISO 8601, epoch ms)')
.action(async (traceId: string, cliOptions: TracesGetOptions) => {
requireProject();
try {
const context = await loadDeployedProjectConfig();
const result = await handleTracesGet(context, traceId, cliOptions);
if (!result.success) {
render(
<Box flexDirection="column">
<Text color="red">Error: {result.error}</Text>
{result.consoleUrl && <Text color="gray">Console: {result.consoleUrl}</Text>}
</Box>
);
process.exit(1);
return;
}
render(
<Box flexDirection="column">
<Text color="green">Trace saved to: {result.filePath}</Text>
{result.consoleUrl && <Text color="gray">Console: {result.consoleUrl}</Text>}
</Box>
);
} catch (error) {
render(<Text color="red">Error: {getErrorMessage(error)}</Text>);
process.exit(1);
}
});
};