Skip to content

Commit

Permalink
CLI: fix --context-repo (#5511)
Browse files Browse the repository at this point in the history
Fixes CODY-3687

Previously, `cody chat --context-repo REPO` didn't use the provided repo
as context due to a regresion in the PR
#5379 This bug was limited to
version 5.5.14 of the CLI, it was not part of 5.5.13.

This PR fixes the bug by adding repo context to the `'submit'`
`contextItem` property where we previously only sent files.


## Test plan
Manually tested with this command
```
❯ pnpm agent chat --show-context --context-file agent/README.md --context-repo github.com/sourcegraph/cody -m 'what is the agent?'
 ...
> Context items:

> 1. /Users/olafurpg/dev/sourcegraph/cody/agent/agent/README.md

> 2. https://sourcegraph.sourcegraph.com/github.com/sourcegraph/cody/-/blob/agent/src/__tests__/graph-test/README.md

> 3. https://sourcegraph.sourcegraph.com/github.com/sourcegraph/cody/-/blob/cli/README.md

> 4. https://sourcegraph.sourcegraph.com/github.com/sourcegraph/cody/-/blob/agent/src/local-e2e/README.md
...

```

We have an integration test for `--context-repo` but it got disabled a
couple months ago due to it being flaky.

<!-- Required. See
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles.
-->

## Changelog



<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->
  • Loading branch information
olafurpg authored Sep 9, 2024
1 parent c6f6909 commit 4e5dff2
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 33 deletions.
7 changes: 7 additions & 0 deletions agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ This is a log of all notable changes to the Cody command-line tool. [Unreleased]

### Changed

## 5.5.15

### Fixed

- `cody chat --context-repo` now works again as expected. The version 5.5.14 had
a regression where `--context-repo` did not work at all.

## 5.5.14

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sourcegraph/cody",
"version": "5.5.14",
"version": "5.5.15",
"description": "Cody CLI is the same technology that powers Cody in the IDE but available from the command-line.",
"license": "Apache-2.0",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions agent/src/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ export class TestClient extends MessageHandler {
command: 'edit',
text,
index: params?.index,
contextFiles: params?.contextFiles ?? [],
contextItems: params?.contextFiles ?? [],
addEnhancedContext: params?.addEnhancedContext ?? false,
},
})
Expand Down Expand Up @@ -818,7 +818,7 @@ export class TestClient extends MessageHandler {
text,
submitType: 'user',
addEnhancedContext: params?.addEnhancedContext ?? false,
contextFiles: params?.contextFiles,
contextItems: params?.contextFiles,
},
})
)
Expand Down
8 changes: 4 additions & 4 deletions agent/src/cli/command-bench/strategy-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export async function evaluateChatStrategy(
const task: ChatTask = YAML.parse(params.content)
const id = await client.request('chat/new', null)
client.request('chat/setModel', { id, model: chatModel })
const contextFiles: ContextItem[] = []
const contextItems: ContextItem[] = []
for (const relativePath of task.files ?? []) {
const uri = vscode.Uri.file(path.join(path.dirname(params.uri.fsPath), relativePath))
contextFiles.push({
contextItems.push({
type: 'file',
uri,
})
Expand All @@ -53,7 +53,7 @@ export async function evaluateChatStrategy(
command: 'submit',
submitType: 'user',
text: task.question,
contextFiles,
contextItems,
addEnhancedContext: isDefined(options.context),
},
})
Expand All @@ -75,7 +75,7 @@ export async function evaluateChatStrategy(
range,
chatReply: reply.text,
chatQuestion: task.question,
contextItems: contextItems,
contextItems,
questionClass: task.class,
llmJudgeScore: score.scoreNumeric,
concisenessScore: concisenessScore.scoreNumeric,
Expand Down
22 changes: 16 additions & 6 deletions agent/src/cli/command-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
endpointOption,
} from './command-auth/command-login'
import { errorSpinner, notAuthenticated } from './command-auth/messages'
import { isNonEmptyArray } from './isNonEmptyArray'
import { legacyCodyClientName } from './legacyCodyClientName'

declare const process: { pkg: { entrypoint: string } } & NodeJS.Process
Expand Down Expand Up @@ -192,6 +191,7 @@ export async function chatAction(options: ChatOptions): Promise<number> {
client.request('chat/setModel', { id, model: options.model })
}

const contextItems: ContextItem[] = []
if (options.contextRepo && options.contextRepo.length > 0) {
if (isDotCom(serverInfo.authStatus)) {
spinner.fail(
Expand Down Expand Up @@ -225,11 +225,21 @@ export async function chatAction(options: ChatOptions): Promise<number> {
)
return 1
}
for (const repo of repos) {
const repoUri = vscode.Uri.parse(`https://${endpoint}/${repo.name}`)
contextItems.push({
type: 'repository',
// TODO: confirm syntax for repo
uri: repoUri,
repoName: repo.name,
repoID: repo.id,
content: null,
})
}
}

const contextFiles: ContextItem[] = []
for (const relativeOrAbsolutePath of options.contextFile ?? []) {
contextFiles.push({
contextItems.push({
type: 'file',
uri: toUri(options.dir, relativeOrAbsolutePath),
})
Expand All @@ -242,7 +252,7 @@ export async function chatAction(options: ChatOptions): Promise<number> {
)
return 1
}
const addEnhancedContext = isNonEmptyArray(options.contextRepo)

const response = await client.request(
'chat/submitMessage',
{
Expand All @@ -251,8 +261,8 @@ export async function chatAction(options: ChatOptions): Promise<number> {
command: 'submit',
submitType: 'user',
text: messageText,
contextFiles,
addEnhancedContext,
addEnhancedContext: false,
contextItems,
},
},
{ token: tokenSource.token }
Expand Down
4 changes: 2 additions & 2 deletions vscode/src/chat/chat-view/ChatController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class ChatController implements vscode.Disposable, vscode.WebviewViewProv
uuid.v4(),
PromptString.unsafe_fromUserQuery(message.text),
message.submitType,
message.contextFiles ?? [],
message.contextItems ?? [],
message.editorState as SerializedPromptEditorState,
message.addEnhancedContext ?? false,
this.startNewSubmitOrEditOperation(),
Expand All @@ -300,7 +300,7 @@ export class ChatController implements vscode.Disposable, vscode.WebviewViewProv
uuid.v4(),
PromptString.unsafe_fromUserQuery(message.text),
message.index ?? undefined,
message.contextFiles ?? [],
message.contextItems ?? [],
message.editorState as SerializedPromptEditorState,
message.addEnhancedContext || false
)
Expand Down
4 changes: 2 additions & 2 deletions vscode/src/chat/chat-view/ChatsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export class ChatsController implements vscode.Disposable {
private async submitChat({
text,
submitType,
contextFiles,
contextItems,
addEnhancedContext,
source = DEFAULT_EVENT_SOURCE,
command,
Expand All @@ -334,7 +334,7 @@ export class ChatsController implements vscode.Disposable {
uuid.v4(),
text,
submitType,
contextFiles ?? [],
contextItems ?? [],
editorState,
addEnhancedContext ?? true,
abortSignal,
Expand Down
2 changes: 2 additions & 0 deletions vscode/src/chat/clientStateBroadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
displayLineRange,
displayPathBasename,
expandToLineRange,
logDebug,
openCtx,
subscriptionDisposable,
} from '@sourcegraph/cody-shared'
Expand Down Expand Up @@ -78,6 +79,7 @@ export function startClientStateBroadcaster({
}
}
const corpusItems = getCorpusContextItemsForEditorState(useRemoteSearch)
logDebug('corpusItems', JSON.stringify(await corpusItems, null, 2))
items.push(...(await corpusItems))

postMessage({ type: 'clientState', value: { initialContext: items } })
Expand Down
2 changes: 1 addition & 1 deletion vscode/src/chat/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ interface WebviewEditMessage extends WebviewContextMessage {

interface WebviewContextMessage {
addEnhancedContext?: boolean | undefined | null
contextFiles?: ContextItem[] | undefined | null
contextItems?: ContextItem[] | undefined | null
}

export interface ExtensionTranscriptMessage {
Expand Down
8 changes: 4 additions & 4 deletions vscode/src/commands/execute/explain-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ async function explainHistoryCommand(

logDebug('explainHistoryCommand', 'computed history options', JSON.stringify(historyOptions))

let contextFiles: ContextItem[] = []
let contextItems: ContextItem[] = []
try {
contextFiles = await commandsProvider.history(historyOptions.uri, historyOptions)
if (contextFiles.length === 0) {
contextItems = await commandsProvider.history(historyOptions.uri, historyOptions)
if (contextItems.length === 0) {
return {
level: 'warn',
reason: 'git-no-match',
Expand All @@ -63,7 +63,7 @@ async function explainHistoryCommand(
text: prompt,
submitType: 'user-newchat',
addEnhancedContext: false,
contextFiles,
contextItems,
source: args?.source,
}
}
Expand Down
2 changes: 1 addition & 1 deletion vscode/src/commands/execute/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function explainCommand(
return {
text: prompt,
submitType: 'user-newchat',
contextFiles: contextItems,
contextItems,
addEnhancedContext: false,
source: args?.source,
command: DefaultChatCommands.Explain,
Expand Down
2 changes: 1 addition & 1 deletion vscode/src/commands/execute/smell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function smellCommand(
return {
text: prompt,
submitType: 'user-newchat',
contextFiles: contextItems,
contextItems,
addEnhancedContext: false,
source: args?.source,
command: DefaultChatCommands.Smell,
Expand Down
2 changes: 1 addition & 1 deletion vscode/src/commands/execute/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function executeExplainOutput(
session: await executeChat({
text: prompt,
submitType: 'user-newchat',
contextFiles: [],
contextItems: [],
addEnhancedContext,
source,
}),
Expand Down
8 changes: 4 additions & 4 deletions vscode/src/commands/execute/test-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function unitTestCommand(

const editor = getEditor()?.active
const document = editor?.document
const contextFiles: ContextItem[] = []
const contextItems: ContextItem[] = []

if (document) {
try {
Expand All @@ -38,17 +38,17 @@ async function unitTestCommand(
'Selection content is empty. Please select some code to generate tests for.'
)
}
contextFiles.push(cursorContext)
contextItems.push(cursorContext)

contextFiles.push(...(await getContextFilesForTestCommand(document.uri)))
contextItems.push(...(await getContextFilesForTestCommand(document.uri)))
} catch (error) {
logError('testCommand', 'failed to fetch context', { verbose: error })
}
}

return {
text: prompt,
contextFiles,
contextItems,
addEnhancedContext: false,
source: args?.source,
submitType: 'user-newchat',
Expand Down
4 changes: 2 additions & 2 deletions vscode/src/commands/services/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ export class CommandRunner implements vscode.Disposable {
const prompt = PromptString.unsafe_fromUserQuery(this.command.prompt)

// Fetch context for the command
const contextFiles = await this.getContextFiles()
const contextItems = await this.getContextFiles()

// NOTE: (bee) codebase context is not supported for custom commands
return {
type: 'chat',
session: await executeChat({
text: prompt,
submitType: 'user',
contextFiles,
contextItems,
addEnhancedContext: this.command.context?.codebase ?? false,
source: 'custom-commands',
command: DefaultChatCommands.Custom,
Expand Down
4 changes: 2 additions & 2 deletions vscode/webviews/chat/Transcript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export function editHumanMessage(
index: messageIndexInTranscript,
text: editorValue.text,
editorState: editorValue.editorState,
contextFiles: editorValue.contextItems.map(deserializeContextItem),
contextItems: editorValue.contextItems.map(deserializeContextItem),
})
focusLastHumanMessageEditor()
}
Expand All @@ -289,7 +289,7 @@ function onFollowupSubmit(editorValue: SerializedPromptEditorValue): void {
submitType: 'user',
text: editorValue.text,
editorState: editorValue.editorState,
contextFiles: editorValue.contextItems.map(deserializeContextItem),
contextItems: editorValue.contextItems.map(deserializeContextItem),
})
focusLastHumanMessageEditor()
}

0 comments on commit 4e5dff2

Please sign in to comment.