@@ -30,6 +30,7 @@ import {
3030 QuickCommandGroupActionClick ,
3131 MergedRelevantDocument ,
3232 FileClick ,
33+ RelevantTextDocumentAddition ,
3334} from './model'
3435import {
3536 AppToWebViewMessageDispatcher ,
@@ -43,7 +44,7 @@ import { EditorContextCommand } from '../../commands/registerCommands'
4344import { PromptsGenerator } from './prompts/promptsGenerator'
4445import { TriggerEventsStorage } from '../../storages/triggerEvents'
4546import { SendMessageRequest } from '@amzn/amazon-q-developer-streaming-client'
46- import { CodeWhispererStreamingServiceException , RelevantTextDocument } from '@amzn/codewhisperer-streaming'
47+ import { CodeWhispererStreamingServiceException } from '@amzn/codewhisperer-streaming'
4748import { UserIntentRecognizer } from './userIntent/userIntentRecognizer'
4849import { CWCTelemetryHelper , recordTelemetryChatRunCommand } from './telemetryHelper'
4950import { CodeWhispererTracker } from '../../../codewhisperer/tracker/codewhispererTracker'
@@ -600,6 +601,8 @@ export class ChatController {
600601 if ( ! lineRanges ) {
601602 return
602603 }
604+
605+ // TODO: Fix for multiple workspace setup
603606 const projectRoot = workspace . workspaceFolders ?. [ 0 ] ?. uri . fsPath
604607 if ( ! projectRoot ) {
605608 return
@@ -612,15 +615,25 @@ export class ChatController {
612615 const editor = await window . showTextDocument ( document , ViewColumn . Active )
613616
614617 // Create multiple selections based on line ranges
615- const selections : Selection [ ] = lineRanges . map ( ( { first, second } ) => {
616- const startPosition = new Position ( first - 1 , 0 ) // Convert 1-based to 0-based
617- const endPosition = new Position ( second - 1 , document . lineAt ( second - 1 ) . range . end . character )
618- return new Selection ( startPosition . line , startPosition . character , endPosition . line , endPosition . character )
619- } )
618+ const selections : Selection [ ] = lineRanges
619+ . filter ( ( { first, second } ) => first !== - 1 && second !== - 1 )
620+ . map ( ( { first, second } ) => {
621+ const startPosition = new Position ( first - 1 , 0 ) // Convert 1-based to 0-based
622+ const endPosition = new Position ( second - 1 , document . lineAt ( second - 1 ) . range . end . character )
623+ return new Selection (
624+ startPosition . line ,
625+ startPosition . character ,
626+ endPosition . line ,
627+ endPosition . character
628+ )
629+ } )
620630
621631 // Apply multiple selections to the editor using the new API
622- editor . selection = selections [ 0 ] // Set the first selection as active
623- editor . selections = selections // Apply multiple selections
632+ if ( selections . length > 0 ) {
633+ editor . selection = selections [ 0 ] // Set the first selection as active
634+ editor . selections = selections // Apply multiple selections
635+ editor . revealRange ( selections [ 0 ] , vscode . TextEditorRevealType . InCenter )
636+ }
624637 }
625638
626639 private processException ( e : any , tabID : string ) {
@@ -868,22 +881,24 @@ export class ChatController {
868881 this . messenger . sendStaticTextResponse ( responseType , triggerID , tabID )
869882 }
870883
871- private async resolveContextCommandPayload ( triggerPayload : TriggerPayload ) {
884+ private async resolveContextCommandPayload ( triggerPayload : TriggerPayload ) : Promise < string [ ] > {
872885 if ( triggerPayload . context === undefined || triggerPayload . context . length === 0 ) {
873- return
886+ return [ ]
874887 }
875888 const contextCommands : ContextCommandItem [ ] = [ ]
889+ const relativePaths : string [ ] = [ ]
876890 for ( const context of triggerPayload . context ) {
877891 if ( typeof context !== 'string' && context . route && context . route . length === 2 ) {
878892 contextCommands . push ( {
879893 workspaceFolder : context . route ?. [ 0 ] || '' ,
880894 type : context . icon === 'folder' ? 'folder' : 'file' ,
881895 relativePath : context . route ?. [ 1 ] || '' ,
882896 } )
897+ relativePaths . push ( context . route [ 1 ] )
883898 }
884899 }
885900 if ( contextCommands . length === 0 ) {
886- return
901+ return [ ]
887902 }
888903 const prompts = await LspClient . instance . getContextCommandPrompt ( contextCommands )
889904 if ( prompts . length > 0 ) {
@@ -902,6 +917,7 @@ export class ChatController {
902917 `Retrieved chunks of additional context count: ${ triggerPayload . additionalContents . length } `
903918 )
904919 }
920+ return relativePaths
905921 }
906922
907923 private async generateResponse (
@@ -937,7 +953,7 @@ export class ChatController {
937953 return
938954 }
939955
940- await this . resolveContextCommandPayload ( triggerPayload )
956+ const relativePaths = await this . resolveContextCommandPayload ( triggerPayload )
941957 // TODO: resolve the context into real context up to 90k
942958 triggerPayload . useRelevantDocuments = false
943959 if ( triggerPayload . message ) {
@@ -988,11 +1004,24 @@ export class ChatController {
9881004
9891005 session . currentContextId ++
9901006 session . contexts . set ( session . currentContextId , new Map ( ) )
991- if ( triggerPayload . mergedRelevantDocuments ) {
992- for ( const doc of triggerPayload . mergedRelevantDocuments ) {
993- const currentContext = session . contexts . get ( session . currentContextId )
994- if ( currentContext ) {
995- currentContext . set ( doc . relativeFilePath , doc . lineRanges )
1007+ if ( triggerPayload . mergedRelevantDocuments !== undefined ) {
1008+ const relativePathsOfMergedRelevantDocuments = triggerPayload . mergedRelevantDocuments . map (
1009+ ( doc ) => doc . relativeFilePath
1010+ )
1011+ for ( const relativePath of relativePaths ) {
1012+ if ( ! relativePathsOfMergedRelevantDocuments . includes ( relativePath ) ) {
1013+ triggerPayload . mergedRelevantDocuments . push ( {
1014+ relativeFilePath : relativePath ,
1015+ lineRanges : [ { first : - 1 , second : - 1 } ] ,
1016+ } )
1017+ }
1018+ }
1019+ if ( triggerPayload . mergedRelevantDocuments ) {
1020+ for ( const doc of triggerPayload . mergedRelevantDocuments ) {
1021+ const currentContext = session . contexts . get ( session . currentContextId )
1022+ if ( currentContext ) {
1023+ currentContext . set ( doc . relativeFilePath , doc . lineRanges )
1024+ }
9961025 }
9971026 }
9981027 }
@@ -1037,7 +1066,7 @@ export class ChatController {
10371066 }
10381067
10391068 private mergeRelevantTextDocuments (
1040- documents : RelevantTextDocument [ ] | undefined
1069+ documents : RelevantTextDocumentAddition [ ] | undefined
10411070 ) : MergedRelevantDocument [ ] | undefined {
10421071 if ( documents === undefined ) {
10431072 return undefined
0 commit comments