@@ -203,6 +203,9 @@ export class InsightContext extends ConversationContext<TimelineUtils.InsightAIC
203
203
}
204
204
}
205
205
206
+ // 16k Tokens * ~4 char per token.
207
+ const MAX_FUNCTION_RESULT_BYTE_LENGTH = 16384 * 4 ;
208
+
206
209
export class PerformanceInsightsAgent extends AiAgent < TimelineUtils . InsightAIContext . ActiveInsight > {
207
210
#insight: ConversationContext < TimelineUtils . InsightAIContext . ActiveInsight > | undefined ;
208
211
@@ -299,6 +302,12 @@ export class PerformanceInsightsAgent extends AiAgent<TimelineUtils.InsightAICon
299
302
) ;
300
303
const formatted =
301
304
requests . map ( r => TraceEventFormatter . networkRequest ( r , activeInsight . parsedTrace , { verbose : false } ) ) ;
305
+
306
+ if ( this . #isFunctionResponseTooLarge( formatted . join ( '\n' ) ) ) {
307
+ return {
308
+ error : 'getNetworkActivitySummary response is too large. Try investigating using other functions' ,
309
+ } ;
310
+ }
302
311
const summaryFact : Host . AidaClient . RequestFact = {
303
312
text :
304
313
`This is the network summary for this insight. You can use this and not call getNetworkActivitySummary again:\n${
@@ -346,6 +355,11 @@ export class PerformanceInsightsAgent extends AiAgent<TimelineUtils.InsightAICon
346
355
return { error : 'Request not found' } ;
347
356
}
348
357
const formatted = TraceEventFormatter . networkRequest ( request , activeInsight . parsedTrace , { verbose : true } ) ;
358
+ if ( this . #isFunctionResponseTooLarge( formatted ) ) {
359
+ return {
360
+ error : 'getNetworkRequestDetail response is too large. Try investigating using other functions' ,
361
+ } ;
362
+ }
349
363
return { result : { request : formatted } } ;
350
364
} ,
351
365
} ) ;
@@ -396,6 +410,11 @@ The fields are:
396
410
return { error : 'No main thread activity found' } ;
397
411
}
398
412
const activity = tree . serialize ( ) ;
413
+ if ( this . #isFunctionResponseTooLarge( activity ) ) {
414
+ return {
415
+ error : 'getMainThreadActivity response is too large. Try investigating using other functions' ,
416
+ } ;
417
+ }
399
418
const activityFact : Host . AidaClient . RequestFact = {
400
419
text :
401
420
`This is the main thread activity for this insight. You can use this and not call getMainThreadActivity again:\n${
@@ -412,6 +431,10 @@ The fields are:
412
431
} ) ;
413
432
}
414
433
434
+ #isFunctionResponseTooLarge( response : string ) : boolean {
435
+ return response . length > MAX_FUNCTION_RESULT_BYTE_LENGTH ;
436
+ }
437
+
415
438
override parseTextResponse ( response : string ) : ParsedResponse {
416
439
/**
417
440
* Sometimes the LLM responds with code chunks that wrap a text based markdown response.
0 commit comments