1- import { ConfigIO } from '../../../lib' ;
2- import type { AgentCoreProjectSpec , AwsDeploymentTargets , DeployedState } from '../../../schema' ;
3- import { buildTraceConsoleUrl , getTrace , listTraces , parseRuntimeArn } from '../../operations/traces' ;
1+ import { parseTimeString } from '../../../lib/utils' ;
2+ import type { AgentResolutionContext } from '../../operations/resolve-agent' ;
3+ import { resolveAgent } from '../../operations/resolve-agent' ;
4+ import { buildTraceConsoleUrl , getTrace , listTraces } from '../../operations/traces' ;
45import type { TracesGetOptions , TracesListOptions } from './types' ;
56
6- export interface TracesContext {
7- project : AgentCoreProjectSpec ;
8- deployedState : DeployedState ;
9- awsTargets : AwsDeploymentTargets ;
10- }
11-
12- export async function loadTracesConfig ( configIO : ConfigIO = new ConfigIO ( ) ) : Promise < TracesContext > {
13- return {
14- project : await configIO . readProjectSpec ( ) ,
15- deployedState : await configIO . readDeployedState ( ) ,
16- awsTargets : await configIO . readAWSDeploymentTargets ( ) ,
17- } ;
18- }
19-
20- interface ResolvedAgent {
21- agentName : string ;
22- region : string ;
23- accountId : string ;
24- runtimeId : string ;
25- targetName : string ;
26- }
27-
28- function resolveAgent ( context : TracesContext , options : { agent ?: string } ) : ResolvedAgent | { error : string } {
29- const { project, deployedState, awsTargets } = context ;
30-
31- const targetNames = Object . keys ( deployedState . targets ) ;
32- if ( targetNames . length === 0 ) {
33- return { error : 'No deployed targets found. Run `agentcore deploy` first.' } ;
34- }
35-
36- const selectedTargetName = targetNames [ 0 ] ! ;
37- const targetState = deployedState . targets [ selectedTargetName ] ;
38- const targetConfig = awsTargets . find ( t => t . name === selectedTargetName ) ;
39- if ( ! targetConfig ) {
40- return { error : `Target config '${ selectedTargetName } ' not found in aws-targets` } ;
41- }
42-
43- if ( project . agents . length === 0 ) {
44- return { error : 'No agents defined in configuration' } ;
45- }
46-
47- const agentNames = project . agents . map ( a => a . name ) ;
48- if ( ! options . agent && project . agents . length > 1 ) {
49- return { error : `Multiple agents found. Use --agent to specify one: ${ agentNames . join ( ', ' ) } ` } ;
50- }
51-
52- const agentSpec = options . agent ? project . agents . find ( a => a . name === options . agent ) : project . agents [ 0 ] ;
53- if ( options . agent && ! agentSpec ) {
54- return { error : `Agent '${ options . agent } ' not found. Available: ${ agentNames . join ( ', ' ) } ` } ;
55- }
56- if ( ! agentSpec ) {
57- return { error : 'No agents defined in configuration' } ;
58- }
59-
60- const agentState = targetState ?. resources ?. agents ?. [ agentSpec . name ] ;
61- if ( ! agentState ) {
62- return { error : `Agent '${ agentSpec . name } ' is not deployed to target '${ selectedTargetName } '` } ;
63- }
64-
65- const parsed = parseRuntimeArn ( agentState . runtimeArn ) ;
66- if ( ! parsed ) {
67- return { error : `Could not parse runtime ARN: ${ agentState . runtimeArn } ` } ;
68- }
69-
70- return {
71- agentName : agentSpec . name ,
72- region : targetConfig . region ,
73- accountId : parsed . accountId ,
74- runtimeId : parsed . runtimeId ,
75- targetName : selectedTargetName ,
76- } ;
77- }
78-
797export interface TracesListResult {
808 success : boolean ;
819 agentName ?: string ;
@@ -85,25 +13,43 @@ export interface TracesListResult {
8513 error ?: string ;
8614}
8715
88- export async function handleTracesList ( context : TracesContext , options : TracesListOptions ) : Promise < TracesListResult > {
16+ export async function handleTracesList (
17+ context : AgentResolutionContext ,
18+ options : TracesListOptions
19+ ) : Promise < TracesListResult > {
8920 const resolved = resolveAgent ( context , options ) ;
90- if ( 'error' in resolved ) {
21+ if ( ! resolved . success ) {
9122 return { success : false , error : resolved . error } ;
9223 }
9324
25+ const { agent } = resolved ;
26+
9427 const consoleUrl = buildTraceConsoleUrl ( {
95- region : resolved . region ,
96- accountId : resolved . accountId ,
97- runtimeId : resolved . runtimeId ,
98- agentName : resolved . agentName ,
28+ region : agent . region ,
29+ accountId : agent . accountId ,
30+ runtimeId : agent . runtimeId ,
31+ agentName : agent . agentName ,
9932 } ) ;
10033
10134 const limit = options . limit ? parseInt ( options . limit , 10 ) : 20 ;
35+
36+ // Parse time options
37+ let startTime : number | undefined ;
38+ let endTime : number | undefined ;
39+ if ( options . since ) {
40+ startTime = parseTimeString ( options . since ) ;
41+ }
42+ if ( options . until ) {
43+ endTime = parseTimeString ( options . until ) ;
44+ }
45+
10246 const result = await listTraces ( {
103- region : resolved . region ,
104- runtimeId : resolved . runtimeId ,
105- agentName : resolved . agentName ,
47+ region : agent . region ,
48+ runtimeId : agent . runtimeId ,
49+ agentName : agent . agentName ,
10650 limit,
51+ startTime,
52+ endTime,
10753 } ) ;
10854
10955 if ( ! result . success ) {
@@ -112,8 +58,8 @@ export async function handleTracesList(context: TracesContext, options: TracesLi
11258
11359 return {
11460 success : true ,
115- agentName : resolved . agentName ,
116- targetName : resolved . targetName ,
61+ agentName : agent . agentName ,
62+ targetName : agent . targetName ,
11763 consoleUrl,
11864 traces : result . traces ,
11965 } ;
@@ -129,26 +75,28 @@ export interface TracesGetResult {
12975}
13076
13177export async function handleTracesGet (
132- context : TracesContext ,
78+ context : AgentResolutionContext ,
13379 traceId : string ,
13480 options : TracesGetOptions
13581) : Promise < TracesGetResult > {
13682 const resolved = resolveAgent ( context , options ) ;
137- if ( 'error' in resolved ) {
83+ if ( ! resolved . success ) {
13884 return { success : false , error : resolved . error } ;
13985 }
14086
87+ const { agent } = resolved ;
88+
14189 const consoleUrl = buildTraceConsoleUrl ( {
142- region : resolved . region ,
143- accountId : resolved . accountId ,
144- runtimeId : resolved . runtimeId ,
145- agentName : resolved . agentName ,
90+ region : agent . region ,
91+ accountId : agent . accountId ,
92+ runtimeId : agent . runtimeId ,
93+ agentName : agent . agentName ,
14694 } ) ;
14795
14896 const result = await getTrace ( {
149- region : resolved . region ,
150- runtimeId : resolved . runtimeId ,
151- agentName : resolved . agentName ,
97+ region : agent . region ,
98+ runtimeId : agent . runtimeId ,
99+ agentName : agent . agentName ,
152100 traceId,
153101 outputPath : options . output ,
154102 } ) ;
@@ -159,8 +107,8 @@ export async function handleTracesGet(
159107
160108 return {
161109 success : true ,
162- agentName : resolved . agentName ,
163- targetName : resolved . targetName ,
110+ agentName : agent . agentName ,
111+ targetName : agent . targetName ,
164112 consoleUrl,
165113 filePath : result . filePath ,
166114 } ;
0 commit comments