Skip to content

Commit

Permalink
Fix cody web file/symbol mention search (make it respect initial repo…
Browse files Browse the repository at this point in the history
…sitory context)
  • Loading branch information
vovakulikov committed Aug 22, 2024
1 parent aa6d660 commit fb13009
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 56 deletions.
1 change: 1 addition & 0 deletions vscode/src/chat/chat-view/ChatController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ export class ChatController implements vscode.Disposable, vscode.WebviewViewProv
const repos =
explicitRepos ??
(await this.repoPicker?.show(this.remoteSearch.getRepos(RepoInclusion.Manual)))

if (repos) {
this.chatModel.setSelectedRepos(repos)
this.remoteSearch.setRepos(repos, RepoInclusion.Manual)
Expand Down
6 changes: 3 additions & 3 deletions vscode/src/editor/utils/editor-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export async function getFileContextFiles(options: FileContextItemsOptions): Pro
source: ContextItemSource.User,
remoteRepositoryName: item.repository.name,
isIgnored: contextFiltersProvider.isRepoNameIgnored(item.repository.name),
uri: URI.file(item.repository.name + item.file.path),
uri: URI.file(`${item.repository.name}/${item.file.path}`),
}))
}

Expand Down Expand Up @@ -192,7 +192,7 @@ export async function getSymbolContextFiles(
item.symbols.map(symbol => ({
type: 'symbol',
remoteRepositoryName: item.repository.name,
uri: URI.file(item.repository.name + symbol.location.resource.path),
uri: URI.file(`${item.repository.name}/${symbol.location.resource.path}`),
isIgnored: contextFiltersProvider.isRepoNameIgnored(item.repository.name),
source: ContextItemSource.User,
symbolName: symbol.name,
Expand Down Expand Up @@ -461,7 +461,7 @@ async function resolveFileOrSymbolContextItem(
return {
...contextItem,
title: path,
uri: URI.parse(`${graphqlClient.endpoint}${repository}/-/blob/${path}`),
uri: URI.parse(`${graphqlClient.endpoint}${repository}/-/blob${path}`),
content: resultOrError,
repoName: repository,
source: ContextItemSource.Unified,
Expand Down
4 changes: 4 additions & 0 deletions web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.0
- Changes API for initial context (now it supports only one repository as an initial context mention)
- Fixes problem with file and symbol mention search don't respect initial repository as a context

## 0.5.2
- Improves repository mention result ordering (for repositories, files and directories mentions)

Expand Down
28 changes: 8 additions & 20 deletions web/demo/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FC } from 'react'

import { CodyWebPanel, CodyWebPanelProvider, type Repository } from '../lib'
import { CodyWebPanel, CodyWebPanelProvider, type InitialContext } from '../lib'

// @ts-ignore
import AgentWorker from '../lib/agent/agent.worker.ts?worker'
Expand All @@ -22,25 +22,13 @@ const serverEndpoint = localStorage.getItem('serverEndpoint') || DOTCOM_SERVER_E
const accessTokenStorageKey = `accessToken:${serverEndpoint}`
let accessToken = localStorage.getItem(accessTokenStorageKey)

// Only for testing/demo purpose, in real-life usage consumer
// should provide context repo information for Cody chat component
const MOCK_DOT_COM_SOURCEGRAPH_REPOSITORY: Repository[] =
serverEndpoint === DOTCOM_SERVER_ENDPOINT
? [
{
id: 'UmVwb3NpdG9yeTozNjgwOTI1MA==',
name: 'github.com/sourcegraph/sourcegraph',
},
]
: []

const MOCK_INITIAL_DOT_COM_CONTEXT =
serverEndpoint === DOTCOM_SERVER_ENDPOINT
? {
fileURL: 'internal/codeintel/ranking/internal/background/mapper/config.go',
repositories: MOCK_DOT_COM_SOURCEGRAPH_REPOSITORY,
}
: undefined
const MOCK_INITIAL_DOT_COM_CONTEXT: InitialContext = {
fileURL: 'web/demo/App.tsx',
repository: {
id: 'UmVwb3NpdG9yeTo2MTMyNTMyOA==',
name: 'github.com/sourcegraph/cody',
},
}

if (!accessToken) {
accessToken = window.prompt(`Enter an access token for ${serverEndpoint}:`)
Expand Down
36 changes: 19 additions & 17 deletions web/lib/components/CodyWebPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,27 @@ export const CodyWebPanel: FC<CodyWebPanelProps> = props => {
)

const clientState: ClientStateForWebview = useMemo<ClientStateForWebview>(() => {
const { repositories = [], fileURL } = initialContext ?? {}
const { repository, fileURL } = initialContext ?? {}

if (repositories.length === 0) {
if (!repository) {
return { initialContext: [] }
}

const mentions: ContextItem[] = repositories.map<ContextItemRepository>(repo => ({
type: 'repository',
id: repo.id,
name: repo.name,
repoID: repo.id,
repoName: repo.name,
description: repo.name,
uri: URI.parse(`repo:${repo.name}`),
content: null,
source: ContextItemSource.Initial,
icon: 'folder',
title: 'Current Repository',
}))
const mentions: ContextItem[] = [
{
type: 'repository',
id: repository.id,
name: repository.name,
repoID: repository.id,
repoName: repository.name,
description: repository.name,
uri: URI.parse(`repo:${repository.name}`),
content: null,
source: ContextItemSource.Initial,
icon: 'folder',
title: 'Current Repository',
} as ContextItemRepository,
]

if (fileURL) {
mentions.push({
Expand All @@ -168,8 +170,8 @@ export const CodyWebPanel: FC<CodyWebPanelProps> = props => {
end: { line: initialContext.fileRange.endLine + 1, character: 0 },
}
: undefined,
remoteRepositoryName: repositories[0].name,
uri: URI.file(repositories[0].name + fileURL),
remoteRepositoryName: repository.name,
uri: URI.file(repository.name + fileURL),
source: ContextItemSource.Initial,
})
}
Expand Down
34 changes: 20 additions & 14 deletions web/lib/components/CodyWebPanelProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const CodyWebPanelProvider: FunctionComponent<PropsWithChildren<CodyWebPa
accessToken: accessToken ?? '',
})

// Create an new chat each time.
// Create a new chat each time
await createChat(client)

setClient(client)
Expand All @@ -114,6 +114,24 @@ export const CodyWebPanelProvider: FunctionComponent<PropsWithChildren<CodyWebPa
})()
}, [accessToken, serverEndpoint, createAgentWorker, customHeaders, telemetryClientName])

useEffect(() => {
if (!client || isErrorLike(client)) {
return
}

// Set initial context after we restore history so context won't be
// overridden by the previous chat session context
if (initialContext?.repository) {
void client.rpc.sendRequest('webview/receiveMessage', {
id: activeWebviewPanelID,
message: {
command: 'context/choose-remote-search-repo',
explicitRepos: [initialContext?.repository],
},
})
}
}, [client, initialContext, activeWebviewPanelID])

const createChat = useCallback(
async (agent = client) => {
if (!agent || isErrorLike(agent)) {
Expand All @@ -133,20 +151,8 @@ export const CodyWebPanelProvider: FunctionComponent<PropsWithChildren<CodyWebPa
id: activeWebviewPanelIDRef.current,
message: { chatID: chatId, command: 'restoreHistory' },
})

// Set initial context after we restore history so context won't be
// overridden by the previous chat session context
if (initialContext?.repositories.length) {
await agent.rpc.sendRequest('webview/receiveMessage', {
id: activeWebviewPanelIDRef.current,
message: {
command: 'context/choose-remote-search-repo',
explicitRepos: initialContext?.repositories ?? [],
},
})
}
},
[client, initialContext]
[client]
)

const vscodeAPI = useMemo<VSCodeWrapper | null>(() => {
Expand Down
2 changes: 1 addition & 1 deletion web/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Repository {
}

export type InitialContext = {
repositories: Repository[]
repository: Repository
fileURL?: string
fileRange?: { startLine: number; endLine: number }
}
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sourcegraph/cody-web",
"version": "0.5.2",
"version": "0.6.0",
"description": "Cody standalone web app",
"license": "Apache-2.0",
"repository": {
Expand Down

0 comments on commit fb13009

Please sign in to comment.