Skip to content
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

Refactor chat #2848

Merged
merged 6 commits into from
Jan 22, 2024
Merged

Refactor chat #2848

merged 6 commits into from
Jan 22, 2024

Conversation

beyang
Copy link
Member

@beyang beyang commented Jan 22, 2024

This PR addresses some code quality issues and complexity in the chat implementation.

  • refactor: move commands away from recipe #2542 introduced a separate code path for handling command-initiated chats. This introduced a flag that special-cased handling of commands in a number of places—in the view controller (SimpleChatPanelProvider), prompt constructor, and context fetcher. These flags make chat more difficult to reason about, requiring the dev to reason about the conditional flag at multiple places in the lifecycle of a chat request.

Code changes

  • Instead of checking a flag to see if the user request was a command in DefaultPrompter, introduces a new CommandPrompter to customize prompt construction for commands
  • Breaks apart ContextProvider into separate functions, moved to a context.ts file. Enhanced context can be fetched by either prompter.
  • Simplifies the core code paths in SimpleChatPanelProvider. Now there are two top-level message handlers: handleNewUserMessage (for regular chat) and handleCommand (for commands). These then execute the necessary steps without entangling other methods that must then support both code paths via conditional.
    • Previously, we had the following mess:
      • executeChat -> handleHumanMessageSubmitted
      • handleHumanMessageSubmitted -> 'handleChatRequestORhandleCommands`
      • handleCommands -> handleChatRequest
      • handleChatRequest -> generateAssistantResponse

Behavior changes

  • This change removes the already defunct cody.experimental.chatPredictions setting.
  • This change disables codebase-wide context fetching for commands. This field appears unused by first-party commands, and it's unclear whether it was actually being used for custom commands.

Follow-up work: This PR also flags some outstanding bugs uncovered in the refactor:

Test plan

  • Test chat with enhanced context
  • Test chat without context
  • Test all first-party commands
  • Test a custom command

@beyang beyang marked this pull request as draft January 22, 2024 01:50
@beyang beyang marked this pull request as ready for review January 22, 2024 02:45
Copy link
Contributor

@dominiccooney dominiccooney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, this is an improvement. @philipp-spiess was pursuing similar breakout of context in #2804 so it is clearly time. Suggestions inline.

@abeatrix how well are commands covered by E2E tests?

I see the TODO about enhanced context, @beyang what's the bottom line on the change to context?

@@ -118,11 +118,10 @@ export class ChatManager implements vscode.Disposable {
const requestID = uuid.v4()
telemetryService.log('CodyVSCodeExtension:chat-question:submitted', { requestID, ...args })
const chatProvider = await this.getChatProvider()
await chatProvider.handleHumanMessageSubmitted(requestID, question, 'user', [], false)
await chatProvider.handleNewUserMessage(requestID, question, 'user-newchat', [], true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why I'm not a fan of boolean parameters... They are mysterious at the callsite.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. In this case, there is a semantic change—we now use enhanced context when starting a chat from the command palette.

vscode/src/chat/chat-view/SimpleChatPanelProvider.ts Outdated Show resolved Hide resolved
return
}

if (!this.editor.getActiveTextEditorSelectionOrVisibleContent()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if the check for command.context.selection, etc., fetching that context, and the error reporting was all together instead of spread out like this.

if (command.mode !== 'ask') {
return
}
// trigger the context progress indicator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we have had this in a couple of places with this comment, why not just add a method triggerContextProgressIndicator?

vscode/src/chat/chat-view/SimpleChatPanelProvider.ts Outdated Show resolved Hide resolved
vscode/src/chat/chat-view/SimpleChatPanelProvider.ts Outdated Show resolved Hide resolved
Comment on lines 654 to 658
if (isRateLimitError(error)) {
this.postError(error, 'transcript')
} else {
this.postError(isError(error) ? error : new Error(`Error generating assistant response: ${error}`))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're cleaning things up, we can de-dup the postError here.

vscode/src/chat/chat-view/SimpleChatPanelProvider.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@abeatrix abeatrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Left a comment in line.

Edit: for dom's question, the e2e test for the core command should cover if the command includes the selection in file, and the response in transcript should be covered by the integration tests in both vs code and agent.

Ill try to clean up the code in Command controller next week too when i work on removing slash commands from chat 😅

return await vscode.commands.executeCommand('cody.action.chat', input, {
source: 'command',
})
await vscode.commands.executeCommand('cody.action.commands.exec', `${selectedCommandID} input`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await vscode.commands.executeCommand('cody.action.commands.exec', `${selectedCommandID} input`)
- await vscode.commands.executeCommand('cody.action.chat', input)

When the selectedCommandID is equal to "/ask" , it means this is a chat question submitted from the command menu, so we can send process this as a chat question with "cody.action.chat".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with executing the command cody.action.chat is that we lose the context of the user selection, so the response ends up being very low quality.

Comment on lines +218 to +221
return await vscode.commands.executeCommand(
'cody.action.commands.exec',
`${selectedCommandID} ${input}`
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return await vscode.commands.executeCommand(
'cody.action.commands.exec',
`${selectedCommandID} ${input}`
)
return await vscode.commands.executeCommand(
'cody.chat.action',
input
)

Same as above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous response

@beyang
Copy link
Member Author

beyang commented Jan 22, 2024

I see the TODO about enhanced context, @beyang what's the bottom line on the change to context?

The only change to context should be:

  • Codebase-wide context fetching is disabled now for commands. This field appears unused by first-party commands, and it's unclear whether it was actually being used for custom commands.

@beyang beyang merged commit c4c4818 into main Jan 22, 2024
15 checks passed
@beyang beyang deleted the bl/chat-refactor branch January 22, 2024 04:46
beyang added a commit that referenced this pull request Jan 22, 2024
Implements some follow-up tasks from
#2848:

* Move methods around in SimpleChatPanelProvider for greater clarity
(not doing in this PR to avoid making the diff larger)
* #2848 (comment)

We establish strong naming conventions for methods and document
invariants that should hold to prevent future quality regressions that
may make the logic more difficult to reason about.

## Test plan

There should be no behavior changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants