@@ -23,9 +23,17 @@ import type {
2323 Response ,
2424 SnapshotParams ,
2525} from './tools/ToolDefinition.js' ;
26+ import type { InsightName , TraceResult } from './trace-processing/parse.js' ;
27+ import { getInsightOutput , getTraceSummary } from './trace-processing/parse.js' ;
2628import { paginate } from './utils/pagination.js' ;
2729import type { PaginationOptions } from './utils/types.js' ;
2830
31+ interface TraceInsightData {
32+ trace : TraceResult ;
33+ insightSetId : string ;
34+ insightName : InsightName ;
35+ }
36+
2937export class McpResponse implements Response {
3038 #includePages = false ;
3139 #snapshotParams?: SnapshotParams ;
@@ -35,6 +43,8 @@ export class McpResponse implements Response {
3543 responseFilePath ?: string ;
3644 } ;
3745 #attachedConsoleMessageId?: number ;
46+ #attachedTraceSummary?: TraceResult ;
47+ #attachedTraceInsight?: TraceInsightData ;
3848 #textResponseLines: string [ ] = [ ] ;
3949 #images: ImageContentData [ ] = [ ] ;
4050 #networkRequestsOptions?: {
@@ -137,10 +147,34 @@ export class McpResponse implements Response {
137147 this . #attachedConsoleMessageId = msgid ;
138148 }
139149
150+ attachTraceSummary ( result : TraceResult ) : void {
151+ this . #attachedTraceSummary = result ;
152+ }
153+
154+ attachTraceInsight (
155+ trace : TraceResult ,
156+ insightSetId : string ,
157+ insightName : InsightName ,
158+ ) : void {
159+ this . #attachedTraceInsight = {
160+ trace,
161+ insightSetId,
162+ insightName,
163+ } ;
164+ }
165+
140166 get includePages ( ) : boolean {
141167 return this . #includePages;
142168 }
143169
170+ get attachedTraceSummary ( ) : TraceResult | undefined {
171+ return this . #attachedTraceSummary;
172+ }
173+
174+ get attachedTracedInsight ( ) : TraceInsightData | undefined {
175+ return this . #attachedTraceInsight;
176+ }
177+
144178 get includeNetworkRequests ( ) : boolean {
145179 return this . #networkRequestsOptions?. include ?? false ;
146180 }
@@ -359,6 +393,8 @@ export class McpResponse implements Response {
359393 snapshot,
360394 detailedNetworkRequest,
361395 networkRequests,
396+ traceInsight : this . #attachedTraceInsight,
397+ traceSummary : this . #attachedTraceSummary,
362398 } ) ;
363399 }
364400
@@ -371,6 +407,8 @@ export class McpResponse implements Response {
371407 snapshot : SnapshotFormatter | string | undefined ;
372408 detailedNetworkRequest ?: NetworkFormatter ;
373409 networkRequests ?: NetworkFormatter [ ] ;
410+ traceSummary ?: TraceResult ;
411+ traceInsight ?: TraceInsightData ;
374412 } ,
375413 ) : { content : Array < TextContent | ImageContent > ; structuredContent : object } {
376414 const response = [ `# ${ toolName } response` ] ;
@@ -434,12 +472,42 @@ Call ${handleDialog.name} to handle it before continuing.`);
434472 networkRequests ?: object [ ] ;
435473 consoleMessage ?: object ;
436474 consoleMessages ?: object [ ] ;
475+ traceSummary ?: string ;
476+ traceInsights ?: Array < { insightName : string ; insightKey : string } > ;
437477 } = { } ;
438478
439479 if ( this . #tabId) {
440480 structuredContent . tabId = this . #tabId;
441481 }
442482
483+ if ( data . traceSummary ) {
484+ const summary = getTraceSummary ( data . traceSummary ) ;
485+ response . push ( summary ) ;
486+ structuredContent . traceSummary = summary ;
487+ structuredContent . traceInsights = [ ] ;
488+ for ( const insightSet of data . traceSummary . insights ?. values ( ) ?? [ ] ) {
489+ for ( const [ insightName , model ] of Object . entries ( insightSet . model ) ) {
490+ structuredContent . traceInsights . push ( {
491+ insightName,
492+ insightKey : model . insightKey ,
493+ } ) ;
494+ }
495+ }
496+ }
497+
498+ if ( data . traceInsight ) {
499+ const insightOutput = getInsightOutput (
500+ data . traceInsight . trace ,
501+ data . traceInsight . insightSetId ,
502+ data . traceInsight . insightName ,
503+ ) ;
504+ if ( 'error' in insightOutput ) {
505+ response . push ( insightOutput . error ) ;
506+ } else {
507+ response . push ( insightOutput . output ) ;
508+ }
509+ }
510+
443511 if ( data . snapshot ) {
444512 if ( typeof data . snapshot === 'string' ) {
445513 response . push ( `Saved snapshot to ${ data . snapshot } .` ) ;
0 commit comments