@@ -13,6 +13,7 @@ import {
1313} from '@amzn/codewhisperer-streaming'
1414import { ChatTriggerType , TriggerPayload } from '../model'
1515import { undefinedIfEmpty } from '../../../../shared/utilities/textUtilities'
16+ import { getLogger } from '../../../../shared'
1617
1718const fqnNameSizeDownLimit = 1
1819const fqnNameSizeUpLimit = 256
@@ -38,6 +39,126 @@ const filePathSizeLimit = 4_000
3839const customerMessageSizeLimit = 4_000
3940
4041export function triggerPayloadToChatRequest ( triggerPayload : TriggerPayload ) : { conversationState : ConversationState } {
42+ // truncate
43+ let remainingPayloadSize = 100_000
44+ // Type A context: Preserving userInput as much as possible
45+ if ( triggerPayload . message !== undefined ) {
46+ if ( triggerPayload . message . length <= remainingPayloadSize ) {
47+ remainingPayloadSize -= triggerPayload . message . length
48+ } else {
49+ triggerPayload . message = triggerPayload . message . substring ( 0 , remainingPayloadSize )
50+ remainingPayloadSize = 0
51+ }
52+ }
53+ // TODO: send truncation telemetry if needed
54+ getLogger ( ) . debug ( `current request user input size: ${ triggerPayload . message ?. length } ` )
55+
56+ // Type B1(prompts) context: Preserving prompts as much as possible
57+ let totalPromptSize = 0
58+ if ( triggerPayload . additionalContents !== undefined ) {
59+ for ( const additionalContent of triggerPayload . additionalContents ) {
60+ if ( additionalContent . type === 'prompt' && additionalContent . innerContext !== undefined ) {
61+ if ( additionalContent . innerContext . length <= remainingPayloadSize ) {
62+ remainingPayloadSize -= additionalContent . innerContext . length
63+ } else {
64+ additionalContent . innerContext = additionalContent . innerContext . substring ( 0 , remainingPayloadSize )
65+ remainingPayloadSize = 0
66+ }
67+ totalPromptSize += additionalContent . innerContext . length
68+ }
69+ }
70+ }
71+
72+ getLogger ( ) . debug ( `current request total prompts size: ${ totalPromptSize } ` )
73+
74+ // Type C context: Preserving current file context as much as possible
75+ // truncate the text to keep texts in the middle instead of blindly truncating the tail
76+ if ( triggerPayload . fileText !== undefined ) {
77+ if ( triggerPayload . fileText . length <= remainingPayloadSize ) {
78+ remainingPayloadSize -= triggerPayload . fileText . length
79+ } else {
80+ // Calculate the middle point
81+ const middle = Math . floor ( triggerPayload . fileText . length / 2 )
82+ // Calculate how much text we can take from each side of the middle
83+ const halfRemaining = Math . floor ( remainingPayloadSize / 2 )
84+ // Get text from around the middle point
85+ const startPos = middle - halfRemaining
86+ const endPos = middle + halfRemaining
87+
88+ triggerPayload . fileText = triggerPayload . fileText . substring ( startPos , endPos )
89+ remainingPayloadSize = 0
90+ }
91+ }
92+ getLogger ( ) . debug ( `current request file content size: ${ triggerPayload . fileText ?. length } ` )
93+
94+ // Type B1(rules) context: Preserving rules as much as possible
95+ let totalRulesSize = 0
96+ if ( triggerPayload . additionalContents !== undefined ) {
97+ for ( const additionalContent of triggerPayload . additionalContents ) {
98+ if ( additionalContent . type === 'rule' && additionalContent . innerContext !== undefined ) {
99+ if ( additionalContent . innerContext . length <= remainingPayloadSize ) {
100+ remainingPayloadSize -= additionalContent . innerContext . length
101+ } else {
102+ additionalContent . innerContext = additionalContent . innerContext . substring ( 0 , remainingPayloadSize )
103+ remainingPayloadSize = 0
104+ }
105+ totalRulesSize += additionalContent . innerContext . length
106+ }
107+ }
108+ }
109+
110+ getLogger ( ) . debug ( `current request rules size: ${ totalRulesSize } ` )
111+
112+ // Type B2(explicit @files) context: Preserving files as much as possible
113+ if ( triggerPayload . additionalContents !== undefined ) {
114+ for ( const additionalContent of triggerPayload . additionalContents ) {
115+ if ( additionalContent . type === 'file' && additionalContent . innerContext !== undefined ) {
116+ if ( additionalContent . innerContext . length <= remainingPayloadSize ) {
117+ remainingPayloadSize -= additionalContent . innerContext . length
118+ } else {
119+ additionalContent . innerContext = additionalContent . innerContext . substring ( 0 , remainingPayloadSize )
120+ remainingPayloadSize = 0
121+ }
122+ }
123+ }
124+ }
125+
126+ // Type B3 @workspace context: Preserving workspace as much as possible
127+ let totalWorkspaceSize = 0
128+ if ( triggerPayload . relevantTextDocuments !== undefined ) {
129+ for ( const relevantDocument of triggerPayload . relevantTextDocuments ) {
130+ if ( relevantDocument . text !== undefined ) {
131+ if ( relevantDocument . text . length <= remainingPayloadSize ) {
132+ // remainingPayloadSize -= relevantDocument.text.length
133+ } else {
134+ // relevantDocument.text = relevantDocument.text.substring(0, remainingPayloadSize)
135+ // remainingPayloadSize = 0
136+ }
137+ totalWorkspaceSize += relevantDocument . text . length
138+ }
139+ }
140+ }
141+
142+ getLogger ( ) . debug ( `current request workspace size: ${ totalWorkspaceSize } ` )
143+
144+ getLogger ( ) . debug (
145+ `current request total payload size: ${ ( triggerPayload . message ?. length ?? 0 ) + totalPromptSize + ( triggerPayload . fileText ?. length ?? 0 ) + totalRulesSize + totalWorkspaceSize } `
146+ )
147+
148+ // Filter out empty innerContext from additionalContents
149+ if ( triggerPayload . additionalContents !== undefined ) {
150+ triggerPayload . additionalContents = triggerPayload . additionalContents . filter (
151+ ( content ) => content . innerContext !== undefined && content . innerContext !== ''
152+ )
153+ }
154+
155+ // Filter out empty text from relevantTextDocuments
156+ if ( triggerPayload . relevantTextDocuments !== undefined ) {
157+ triggerPayload . relevantTextDocuments = triggerPayload . relevantTextDocuments . filter (
158+ ( doc ) => doc . text !== undefined && doc . text !== ''
159+ )
160+ }
161+
41162 let document : TextDocument | undefined = undefined
42163 let cursorState : CursorState | undefined = undefined
43164
0 commit comments