Skip to content

feat: add the grepSearch tool #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion chat-client/src/client/mynahUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ export const createMynahUi = (
? ''
: `line ${range.first} - ${range.second}`
)
.join(', ') || '',
.join(', ') || fileDetails.description
? fileDetails.description
: '',
description: filePath,
clickable: true,
},
Expand Down
17 changes: 13 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion server/aws-lsp-codewhisperer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"shlex": "2.1.2",
"uuid": "^11.0.5",
"vscode-uri": "^3.1.0",
"@modelcontextprotocol/sdk": "^1.9.0"
"@modelcontextprotocol/sdk": "^1.9.0",
"@vscode/ripgrep": "^1.15.11"
},
"devDependencies": {
"@types/adm-zip": "^0.5.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Will be deleted or merged.
*/

import * as path from 'path'

Check warning on line 6 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "path"

Check warning on line 6 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "path"
import {
ChatTriggerType,
GenerateAssistantResponseCommandInput,
Expand Down Expand Up @@ -101,6 +101,7 @@
import { FsWrite, FsWriteParams, getDiffChanges } from './tools/fsWrite'
import { ExecuteBash, ExecuteBashOutput, ExecuteBashParams } from './tools/executeBash'
import { ExplanatoryParams, InvokeOutput, ToolApprovalException } from './tools/toolShared'
import { GrepSearch, SanitizedRipgrepOutput } from './tools/grepSearch'

type ChatHandlers = Omit<
LspHandlers<Chat>,
Expand Down Expand Up @@ -559,13 +560,15 @@
switch (toolUse.name) {
case 'fsRead':
case 'listDirectory':
case 'grepSearch':
case 'fsWrite':
case 'executeBash': {
const toolMap = {
fsRead: { Tool: FsRead },
listDirectory: { Tool: ListDirectory },
fsWrite: { Tool: FsWrite },
executeBash: { Tool: ExecuteBash },
grepSearch: { Tool: GrepSearch },
}

const { Tool } = toolMap[toolUse.name as keyof typeof toolMap]
Expand Down Expand Up @@ -642,6 +645,12 @@
// no need to write tool result for listDir and fsRead into chat stream
// executeBash will stream the output instead of waiting until the end
break
case 'grepSearch':
const grepSearchResult = this.#processGrepSearchResult(toolUse, result, chatResultStream)
if (grepSearchResult) {
await chatResultStream.writeResultBlock(grepSearchResult)
}
break
case 'fsWrite':
const chatResult = await this.#getFsWriteChatResult(toolUse)
const toolUseLookup = this.#triggerContext.getToolUseLookup()
Expand Down Expand Up @@ -914,6 +923,73 @@
}
}

/**
* Process grep search results and format them for display in the chat UI
*/
#processGrepSearchResult(
toolUse: ToolUse,
result: InvokeOutput,
chatResultStream: AgenticChatResultStream
): ChatMessage | undefined {
if (toolUse.name !== 'grepSearch') {
return undefined
}

let messageIdToUpdate = toolUse.toolUseId!
const currentId = chatResultStream.getMessageIdToUpdateForTool(toolUse.name!)

if (currentId) {
messageIdToUpdate = currentId
} else {
chatResultStream.setMessageIdToUpdateForTool(toolUse.name!, messageIdToUpdate)
}

// Extract search results from the tool output
const output = result.output.content as SanitizedRipgrepOutput
if (!output || !output.fileMatches || !Array.isArray(output.fileMatches)) {
return {
type: 'tool',
messageId: messageIdToUpdate,
body: 'No search results found.',
}
}

// Process the matches into a structured format
const matches = output.fileMatches
const fileDetails: Record<string, FileDetails> = {}

// Create file details directly from matches
for (const match of matches) {
const filePath = match.filePath
if (!filePath) continue

fileDetails[filePath] = {
description: `(${match.matchCount} results)`,
lineRanges: [{ first: -1, second: -1 }],
}
}

// Create sorted array of file paths
const sortedFilePaths = Object.keys(fileDetails)

// Create the context list for display
const query = (toolUse.input as any)?.query || 'search term'
const matchCount = matches.length

const contextList: FileList = {
rootFolderTitle: `Searched for "${query}", ${output.totalMatchCount} found`,
filePaths: sortedFilePaths,
details: fileDetails,
}

return {
type: 'tool',
contextList,
messageId: messageIdToUpdate,
body: '',
}
}

/**
* Updates the request input with tool results for the next iteration
*/
Expand Down
Loading
Loading