Skip to content

Commit be52c10

Browse files
committed
context fixes for multiroot workspace wetup
1 parent a687b2a commit be52c10

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

packages/core/src/codewhispererChat/clients/chat/v0/chat.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import { UserWrittenCodeTracker } from '../../../../codewhisperer/tracker/userWr
1414
export class ChatSession {
1515
private sessionId?: string
1616

17-
contexts: Map<number, Map<string, { first: number; second: number }[]>> = new Map()
18-
currentContextId: number = 0
17+
contexts: Map<string, { first: number; second: number }[]> = new Map()
18+
// TODO: doesn't handle the edge case when two files share the same relativePath string but from different root
19+
// e.g. root_a/file1 vs root_b/file1
20+
relativePathToWorkspaceRoot: Map<string, string> = new Map()
1921
public get sessionIdentifier(): string | undefined {
2022
return this.sessionId
2123
}

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
additionalContentInnerContextLimit,
8080
contextMaxLength,
8181
} from '../../constants'
82+
import { ChatSession } from '../../clients/chat/v0/chat'
8283

8384
export interface ChatControllerMessagePublishers {
8485
readonly processPromptChatMessage: MessagePublisher<PromptMessage>
@@ -589,14 +590,14 @@ export class ChatController {
589590
private async processFileClickMessage(message: FileClick) {
590591
const session = this.sessionStorage.getSession(message.tabID)
591592
// TODO remove currentContextId but use messageID to track context for each answer message
592-
const lineRanges = session.contexts.get(session.currentContextId)?.get(message.filePath)
593+
const lineRanges = session.contexts.get(message.filePath)
593594

594595
if (!lineRanges) {
595596
return
596597
}
597598

598599
// TODO: Fix for multiple workspace setup
599-
const projectRoot = workspace.workspaceFolders?.[0]?.uri.fsPath
600+
const projectRoot = session.relativePathToWorkspaceRoot.get(message.filePath)
600601
if (!projectRoot) {
601602
return
602603
}
@@ -884,7 +885,10 @@ export class ChatController {
884885
this.messenger.sendStaticTextResponse(responseType, triggerID, tabID)
885886
}
886887

887-
private async resolveContextCommandPayload(triggerPayload: TriggerPayload): Promise<string[]> {
888+
private async resolveContextCommandPayload(
889+
triggerPayload: TriggerPayload,
890+
session: ChatSession
891+
): Promise<string[]> {
888892
const contextCommands: ContextCommandItem[] = []
889893
const relativePaths: string[] = []
890894

@@ -921,7 +925,17 @@ export class ChatController {
921925
if (contextCommands.length === 0) {
922926
return []
923927
}
924-
const workspaceFolder = contextCommands[0].workspaceFolder
928+
const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri?.fsPath
929+
if (!workspaceFolder) {
930+
return []
931+
}
932+
for (const contextCommand of contextCommands) {
933+
let relativePath = path.relative(
934+
workspaceFolder,
935+
path.join(contextCommand.workspaceFolder, contextCommand.relativePath)
936+
)
937+
session.relativePathToWorkspaceRoot.set(relativePath, contextCommand.workspaceFolder)
938+
}
925939
const prompts = await LspClient.instance.getContextCommandPrompt(contextCommands)
926940
if (prompts.length === 0) {
927941
return []
@@ -1007,7 +1021,8 @@ export class ChatController {
10071021
return
10081022
}
10091023

1010-
const relativePathsOfContextCommandFiles = await this.resolveContextCommandPayload(triggerPayload)
1024+
const session = this.sessionStorage.getSession(tabID)
1025+
const relativePathsOfContextCommandFiles = await this.resolveContextCommandPayload(triggerPayload, session)
10111026
triggerPayload.useRelevantDocuments =
10121027
triggerPayload.context?.some(
10131028
(context) => typeof context !== 'string' && context.command === '@workspace'
@@ -1054,10 +1069,7 @@ export class ChatController {
10541069
triggerPayload.documentReferences = this.mergeRelevantTextDocuments(triggerPayload.relevantTextDocuments || [])
10551070

10561071
const request = triggerPayloadToChatRequest(triggerPayload)
1057-
const session = this.sessionStorage.getSession(tabID)
10581072

1059-
session.currentContextId++
1060-
session.contexts.set(session.currentContextId, new Map())
10611073
if (triggerPayload.documentReferences !== undefined) {
10621074
const relativePathsOfMergedRelevantDocuments = triggerPayload.documentReferences.map(
10631075
(doc) => doc.relativeFilePath
@@ -1072,10 +1084,7 @@ export class ChatController {
10721084
}
10731085
if (triggerPayload.documentReferences) {
10741086
for (const doc of triggerPayload.documentReferences) {
1075-
const currentContext = session.contexts.get(session.currentContextId)
1076-
if (currentContext) {
1077-
currentContext.set(doc.relativeFilePath, doc.lineRanges)
1078-
}
1087+
session.contexts.set(doc.relativeFilePath, doc.lineRanges)
10791088
}
10801089
}
10811090
}

0 commit comments

Comments
 (0)