Skip to content

Commit 3bfc592

Browse files
committed
grep search UX change.
1 parent 8557658 commit 3bfc592

File tree

4 files changed

+300
-60
lines changed

4 files changed

+300
-60
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,27 @@ export class ChatSession {
4040
private _pairProgrammingModeOn: boolean = true
4141
private _fsWriteBackups: Map<string, FsWriteBackup> = new Map()
4242
private _agenticLoopInProgress: boolean = false
43+
private _searchResults: DocumentReference[] = []
44+
45+
// // Search-related properties
46+
// public _lastSearchQuery?: string
47+
// public _lastSearchPath?: string
4348

4449
/**
4550
* True if messages from local history have been sent to session.
4651
*/
4752
localHistoryHydrated: boolean = false
4853
private _messageIdToUpdate: string | undefined
4954
private _messageIdToUpdateListDirectory: string | undefined
55+
private _messageIdToUpdateGrepSearch: string | undefined
5056

5157
contexts: Map<string, { first: number; second: number }[]> = new Map()
5258
// TODO: doesn't handle the edge case when two files share the same relativePath string but from different root
5359
// e.g. root_a/file1 vs root_b/file1
5460
relativePathToWorkspaceRoot: Map<string, string> = new Map()
61+
62+
// lastSearchQuery: string | undefined
63+
// lastSearchPath: string | undefined
5564
public get sessionIdentifier(): string {
5665
return this.sessionId
5766
}
@@ -71,6 +80,14 @@ export class ChatSession {
7180
this._messageIdToUpdateListDirectory = messageId
7281
}
7382

83+
public get messageIdToUpdateGrepSearch(): string | undefined {
84+
return this._messageIdToUpdateGrepSearch
85+
}
86+
87+
public setMessageIdToUpdateGrepSearch(messageId: string | undefined) {
88+
this._messageIdToUpdateGrepSearch = messageId
89+
}
90+
7491
public get agenticLoopInProgress(): boolean {
7592
return this._agenticLoopInProgress
7693
}
@@ -147,6 +164,10 @@ export class ChatSession {
147164
public get readFiles(): DocumentReference[] {
148165
return this._readFiles
149166
}
167+
168+
public get searchResults(): DocumentReference[] {
169+
return this._searchResults
170+
}
150171
public get readFolders(): DocumentReference[] {
151172
return this._readFolders
152173
}
@@ -159,6 +180,9 @@ export class ChatSession {
159180
public addToReadFiles(filePath: DocumentReference) {
160181
this._readFiles.push(filePath)
161182
}
183+
public setSearchResults(searchResults: DocumentReference[]) {
184+
this.searchResults.push(...searchResults)
185+
}
162186
public clearListOfReadFiles() {
163187
this._readFiles = []
164188
}
@@ -168,6 +192,10 @@ export class ChatSession {
168192
public clearListOfReadFolders() {
169193
this._readFolders = []
170194
}
195+
public clearSearchResults() {
196+
this._searchResults = []
197+
this._messageIdToUpdateGrepSearch = undefined
198+
}
171199
async chatIam(chatRequest: SendMessageRequest): Promise<SendMessageCommandOutput> {
172200
const client = await createQDeveloperStreamingClient()
173201

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

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import { ConversationTracker } from '../../../storages/conversationTracker'
7171
import { waitTimeout, Timeout } from '../../../../shared/utilities/timeoutUtils'
7272
import { FsReadParams } from '../../../tools/fsRead'
7373
import { ListDirectoryParams } from '../../../tools/listDirectory'
74+
import { SanitizedRipgrepOutput } from '../../../tools/grepSearch'
7475

7576
export type StaticTextResponseType = 'quick-action-help' | 'onboarding-help' | 'transform' | 'help'
7677

@@ -307,7 +308,13 @@ export class Messenger {
307308
let explanation: string | undefined = undefined
308309
let changeList: Change[] | undefined = undefined
309310
let messageIdToUpdate: string | undefined = undefined
310-
const isReadOrList: boolean = [ToolType.FsRead, ToolType.ListDirectory].includes(
311+
// eslint-disable-next-line prettier/prettier
312+
const isReadOrList: boolean = [
313+
ToolType.FsRead,
314+
ToolType.ListDirectory,
315+
ToolType.GrepSearch,
316+
].includes(
317+
// eslint-disable-next-line prettier/prettier
311318
tool.type
312319
)
313320
if (tool.type === ToolType.ExecuteBash) {
@@ -391,6 +398,12 @@ export class Messenger {
391398
) {
392399
session.setMessageIdToUpdateListDirectory(toolUse.toolUseId)
393400
}
401+
if (
402+
session.messageIdToUpdateGrepSearch === undefined &&
403+
tool.type === ToolType.GrepSearch
404+
) {
405+
session.setMessageIdToUpdateGrepSearch(toolUse.toolUseId)
406+
}
394407
getLogger().debug(
395408
`SetToolUseWithError: ${toolUse.name}:${toolUse.toolUseId} with no error`
396409
)
@@ -743,6 +756,80 @@ export class Messenger {
743756
)
744757
}
745758

759+
private sendGrepSearchToolMessage(
760+
message: string,
761+
toolUse: ToolUse,
762+
session: ChatSession,
763+
tabID: string,
764+
triggerID: string,
765+
messageIdToUpdate?: string
766+
) {
767+
getLogger().info(`Grep search update message is: "${message}"`)
768+
let searchResults = session.searchResults
769+
770+
// Check if the message contains grep search results
771+
if (message.includes('Found') && message.includes('matches')) {
772+
try {
773+
// Remove the first line summary if present
774+
const jsonStartIndex = message.indexOf('{')
775+
if (jsonStartIndex !== -1) {
776+
const jsonString = message.substring(jsonStartIndex)
777+
778+
// Parse the JSON string to get the SanitizedRipgrepOutput
779+
const ripgrepOutput: SanitizedRipgrepOutput = JSON.parse(jsonString)
780+
781+
// Convert the fileMatches to the format expected by session.searchResults
782+
searchResults = ripgrepOutput.fileMatches.map((file) => ({
783+
relativeFilePath: file.filePath,
784+
// Get line numbers from the matches object
785+
lineRanges: Object.keys(file.matches || {}).map((lineNum) => ({
786+
first: parseInt(lineNum, 10),
787+
second: parseInt(lineNum, 10),
788+
})),
789+
}))
790+
791+
// Store the search results in the session for context transparency
792+
session.setSearchResults(searchResults)
793+
}
794+
} catch (error) {
795+
getLogger().error(`Error parsing grep search results: ${error}`)
796+
}
797+
}
798+
799+
const contextList = session.searchResults
800+
801+
const itemCount = contextList.length
802+
803+
// Create a title based on search results
804+
const title =
805+
itemCount === 0 ? 'No search results found' : `Found ${itemCount} match${itemCount !== 1 ? 'es' : ''}`
806+
807+
// Send the tool message with the context list
808+
this.dispatcher.sendToolMessage(
809+
new ToolMessage(
810+
{
811+
message: '',
812+
messageType: 'answer-part',
813+
followUps: undefined,
814+
followUpsHeader: undefined,
815+
relatedSuggestions: undefined,
816+
triggerID,
817+
messageID: messageIdToUpdate ?? toolUse?.toolUseId ?? '',
818+
userIntent: undefined,
819+
codeBlockLanguage: undefined,
820+
contextList,
821+
canBeVoted: false,
822+
buttons: undefined,
823+
fullWidth: false,
824+
padding: false,
825+
codeBlockActions: undefined,
826+
rootFolderTitle: title,
827+
},
828+
tabID
829+
)
830+
)
831+
}
832+
746833
public sendPartialToolLog(
747834
message: string,
748835
tabID: string,
@@ -757,12 +844,18 @@ export class Messenger {
757844
return
758845
}
759846

760-
// Handle read tool and list directory messages
847+
// Handle read tool, grep search, and list directory messages
761848
if (
762-
(toolUse?.name === ToolType.FsRead || toolUse?.name === ToolType.ListDirectory) &&
849+
(toolUse?.name === ToolType.FsRead ||
850+
toolUse?.name === ToolType.ListDirectory ||
851+
toolUse?.name === ToolType.GrepSearch) &&
763852
!validation.requiresAcceptance
764853
) {
765-
return this.sendReadAndListDirToolMessage(toolUse, session, tabID, triggerID, messageIdToUpdate)
854+
if (toolUse?.name === ToolType.GrepSearch) {
855+
return this.sendGrepSearchToolMessage(message, toolUse, session, tabID, triggerID, messageIdToUpdate)
856+
} else {
857+
return this.sendReadAndListDirToolMessage(toolUse, session, tabID, triggerID, messageIdToUpdate)
858+
}
766859
}
767860

768861
// Handle file write tool, execute bash tool and bash command output log messages

0 commit comments

Comments
 (0)