@@ -23,6 +23,7 @@ import {
2323 GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE ,
2424 GEN_AI_SYSTEM_ATTRIBUTE ,
2525} from '../ai/gen-ai-attributes' ;
26+ import { truncateGenAiMessages } from '../ai/messageTruncation' ;
2627import { buildMethodPath , getFinalOperationName , getSpanOperation , setTokenUsageAttributes } from '../ai/utils' ;
2728import { handleCallbackErrors } from '../handleCallbackErrors' ;
2829import { instrumentAsyncIterableStream , instrumentMessageStream } from './streaming' ;
@@ -71,16 +72,16 @@ function extractRequestAttributes(args: unknown[], methodPath: string): Record<s
7172 return attributes ;
7273}
7374
74- /**
75- * Add private request attributes to spans.
76- * This is only recorded if recordInputs is true.
77- */
7875function addPrivateRequestAttributes ( span : Span , params : Record < string , unknown > ) : void {
7976 if ( 'messages' in params ) {
80- span . setAttributes ( { [ GEN_AI_REQUEST_MESSAGES_ATTRIBUTE ] : JSON . stringify ( params . messages ) } ) ;
77+ const messages = params . messages ;
78+ const truncatedMessages = truncateGenAiMessages ( messages as unknown [ ] ) ;
79+ span . setAttributes ( { [ GEN_AI_REQUEST_MESSAGES_ATTRIBUTE ] : JSON . stringify ( truncatedMessages ) } ) ;
8180 }
8281 if ( 'input' in params ) {
83- span . setAttributes ( { [ GEN_AI_REQUEST_MESSAGES_ATTRIBUTE ] : JSON . stringify ( params . input ) } ) ;
82+ const input = params . input ;
83+ const truncatedInput = truncateGenAiMessages ( input as unknown [ ] ) ;
84+ span . setAttributes ( { [ GEN_AI_REQUEST_MESSAGES_ATTRIBUTE ] : JSON . stringify ( truncatedInput ) } ) ;
8485 }
8586 if ( 'prompt' in params ) {
8687 span . setAttributes ( { [ GEN_AI_PROMPT_ATTRIBUTE ] : JSON . stringify ( params . prompt ) } ) ;
0 commit comments