Skip to content

Commit

Permalink
NLS: escape backslashes in query string
Browse files Browse the repository at this point in the history
  • Loading branch information
jtibshirani committed Jan 10, 2025
1 parent 798c73b commit ae3cf95
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion agent/src/cli/command-bench/strategy-chat-nls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import { graphqlClient, isError } from '@sourcegraph/cody-shared'
import { escapeNLSQuery } from '../../../../vscode/src/chat/chat-view/handlers/SearchHandler'
import { version } from '../../../package.json'
import type { CodyBenchOptions } from './command-bench'
import {
Expand Down Expand Up @@ -75,7 +76,7 @@ async function runNLSSearch(examples: Example[]): Promise<ExampleOutput[]> {
const repoNames = targetRepoRevs.map(repoRev => repoRev.repoName)
const repoFilter = 'repo:' + repoNames.join('|')

const fullQuery = `${repoFilter} content:"${query.replaceAll('"', '\\"')}"`
const fullQuery = `${repoFilter} content:"${escapeNLSQuery(query)}"`
const resultsResp = await graphqlClient.nlsSearchQuery({
query: fullQuery,
})
Expand Down
15 changes: 12 additions & 3 deletions vscode/src/chat/chat-view/handlers/SearchHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ export class SearchHandler implements AgentHandler {
? 'boost:relevant.repos()'
: ''

const query = `content:"${inputTextWithoutContextChips.replaceAll(
'"',
'\\"'
const query = `content:"${escapeNLSQuery(
inputTextWithoutContextChips
)}" ${currentRepoBoost} ${myProjectsBoost} ${scopes.length ? `(${scopes.join(' OR ')})` : ''}`

try {
Expand Down Expand Up @@ -75,6 +74,16 @@ export class SearchHandler implements AgentHandler {
}
}

export function escapeNLSQuery(query: string): string {
return (
query
// first escape backslashes
.replaceAll('\\', '\\\\')
// then escape quotes
.replaceAll('"', '\\"')
)
}

async function getSearchScopesFromMentions(mentions: ContextItem[]): Promise<string[]> {
const validMentions = mentions.reduce(
(groups, mention) => {
Expand Down
16 changes: 16 additions & 0 deletions vscode/src/chat/chat-view/handlers/__tests__/SearchHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from 'vitest'
import { escapeNLSQuery } from '../SearchHandler'

describe('escapeNLSQuery', () => {
it('escapes backslashes', () => {
expect(escapeNLSQuery('path\\to\\file')).toBe('path\\\\to\\\\file')
})

it('escapes double quotes', () => {
expect(escapeNLSQuery(`say "hello"`)).toBe(`say \\"hello\\"`)
})

it('escapes escaped quotes', () => {
expect(escapeNLSQuery(`c:\\path\\"file"`)).toBe(`c:\\\\path\\\\\\"file\\"`)
})
})

0 comments on commit ae3cf95

Please sign in to comment.