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: commands #2869

Merged
merged 36 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
70a66d5
refactor: default commands
abeatrix Jan 23, 2024
18763e8
wip: clean up commands
abeatrix Jan 23, 2024
e9d0f5f
update agent test
abeatrix Jan 23, 2024
e876f42
wip: clean up menus and providers
abeatrix Jan 24, 2024
a3f215d
wip: context
abeatrix Jan 24, 2024
988e334
wip: clean up command names, singleton controller
abeatrix Jan 24, 2024
17409af
remove getContextForCommand
abeatrix Jan 24, 2024
a8d52a1
update agent test
abeatrix Jan 24, 2024
3dc6170
Merge branch 'main' into bee/refactor-commands
abeatrix Jan 24, 2024
76dab65
wip
abeatrix Jan 25, 2024
c9a2749
wip
abeatrix Jan 25, 2024
936abcc
update dir
abeatrix Jan 25, 2024
281ec2a
add e2e tests
abeatrix Jan 25, 2024
fe8cc26
Add unit tests, stronger type
abeatrix Jan 25, 2024
cd72f81
add e2e tests for custom command context, menu, and builder
abeatrix Jan 25, 2024
2cb7808
add error handling to command context
abeatrix Jan 26, 2024
b53e0c8
remove codebase context from menu
abeatrix Jan 26, 2024
309d05a
clean up menu items
abeatrix Jan 26, 2024
5a3a3e7
Merge branch 'main' into bee/refactor-commands
abeatrix Jan 26, 2024
75ad94f
Merge branch 'main' into bee/refactor-commands
abeatrix Jan 26, 2024
e9b9384
fix e2e
abeatrix Jan 26, 2024
09faa36
fix flanky tests
abeatrix Jan 26, 2024
c1c799f
again
abeatrix Jan 26, 2024
f72b691
mark to do
abeatrix Jan 26, 2024
197c597
Merge branch 'main' into bee/refactor-commands
abeatrix Jan 26, 2024
7efa348
clean up and fix menu, add type strong
abeatrix Jan 26, 2024
01f2367
add new e2e test for custom command menu
abeatrix Jan 26, 2024
104e946
fix e2e tests on windows
abeatrix Jan 26, 2024
81c3f4b
update agent test
abeatrix Jan 26, 2024
33bb570
update and add tests
abeatrix Jan 27, 2024
c8d6717
fix lint
abeatrix Jan 27, 2024
1a2e119
support windows path
abeatrix Jan 27, 2024
c5d7774
ooops
abeatrix Jan 27, 2024
62c923f
it was the .only in test
abeatrix Jan 27, 2024
64bded2
Merge branch 'main' into bee/refactor-commands
abeatrix Jan 29, 2024
a8ef81d
Merge branch 'main' into bee/refactor-commands
abeatrix Jan 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15,233 changes: 13,091 additions & 2,142 deletions agent/recordings/defaultClient_631904893/recording.har.yaml

Large diffs are not rendered by default.

168 changes: 101 additions & 67 deletions agent/recordings/enterpriseClient_3965582033/recording.har.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ export class Agent extends MessageHandler {
})

// The arguments to pass to the command to make sure edit commands would also run in chat mode
const commandArgs = [{ runInChatMode: true, source: 'editor' }]
const commandArgs = [{ source: 'editor' }]

this.registerAuthenticatedRequest('commands/explain', () => {
return this.createChatPanel(
Expand Down
221 changes: 104 additions & 117 deletions agent/src/index.test.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/shared/src/chat/transcript/messages.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ContextFile, PreciseContext } from '../../codebase-context/messages'
import type { CodyDefaultCommands } from '../../commands'
import type { Message } from '../../sourcegraph-api'

import type { TranscriptJSON } from '.'
import type { DefaultCodyCommands } from '../../commands/types'

export interface ChatButton {
label: string
Expand Down Expand Up @@ -68,7 +68,7 @@ export type ChatEventSource =
| 'custom-commands'
| 'test'
| 'code-lens'
| CodyDefaultCommands
| DefaultCodyCommands

/**
* Converts an Error to a ChatError. Note that this cannot be done naively,
Expand Down
49 changes: 0 additions & 49 deletions lib/shared/src/commands/index.ts

This file was deleted.

74 changes: 74 additions & 0 deletions lib/shared/src/commands/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// The default Cody Commands
export type DefaultCodyCommands = DefaultChatCommands | DefaultEditCommands

// Default Cody Commands that runs as a Chat request
export enum DefaultChatCommands {
Ask = 'chat', // Submit a question in chat
Explain = 'explain', // Explain code
Test = 'test', // Generate unit tests in Chat
Smell = 'smell', // Generate code smell report in Chat
}

// Default Cody Commands that runs as an Inline Edit command
export enum DefaultEditCommands {
Edit = 'edit', // Inline edit request
Unit = 'unit', // Generate unit tests with inline edit
Doc = 'doc', // Generate documentation with inline edit
}

// The blueprint of a Cody Custom Command
export interface CodyCommand {
slashCommand: string
prompt: string
description?: string
context?: CodyCommandContext
type?: CodyCommandType
mode?: CodyCommandMode

// Internal use - the ID of the request
requestID?: string
}

/**
* - 'ask' mode is the default mode, run prompt in chat view
* - 'edit' mode will run prompt with edit command which replace selection with cody's response
* - 'insert' mode is the same as edit, it adds to the top of the selection instead of replacing selection
* - 'file' mode create a new file with cody's response as content
*/
type CodyCommandMode = 'ask' | 'edit' | 'insert' | 'file'

// Type of context available for prompt building
export interface CodyCommandContext {
// Exclude any context.
// It takes precedence over all other context.
none?: boolean

// Tabs from the current workspace
openTabs?: boolean
// The directory with the current file opened in the editor
currentDir?: boolean
// The current file opened in the editor
currentFile?: boolean
// The current selection in the editor
// Default to use smart selection unless set to false specifically
selection?: boolean
// Shell command to run to get context
command?: string
// The relative path of a file within your workspace root
filePath?: string
// The relative path of a directory within your workspace root
directoryPath?: string

// NOTE: Currently not supported
// Codebase context from current codebase
codebase?: boolean
}

export type CodyCommandType = CustomCommandType | DefaultCommandType | 'recently used'

export enum CustomCommandType {
Workspace = 'workspace',
User = 'user',
}

export type DefaultCommandType = 'default' | 'experimental'
16 changes: 1 addition & 15 deletions lib/shared/src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,7 @@ export interface ActiveTextEditorVisibleContent {
revision?: string
}

export interface VsCodeCommandsController {
menu(type: 'custom' | 'config' | 'default', showDesc?: boolean): Promise<void>
}

export interface ActiveTextEditorViewControllers<
C extends VsCodeCommandsController = VsCodeCommandsController,
> {
readonly command?: C
}

export interface Editor<P extends VsCodeCommandsController = VsCodeCommandsController> {
controllers?: ActiveTextEditorViewControllers<P>

export interface Editor {
/** The URI of the workspace root. */
getWorkspaceRootUri(): URI | null

Expand Down Expand Up @@ -93,8 +81,6 @@ export interface Editor<P extends VsCodeCommandsController = VsCodeCommandsContr
}

export class NoopEditor implements Editor {
public controllers?: ActiveTextEditorViewControllers<VsCodeCommandsController> | undefined

public getWorkspaceRootUri(): URI | null {
return null
}
Expand Down
6 changes: 2 additions & 4 deletions lib/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export type {
PreciseContext,
SymbolKind,
} from './codebase-context/messages'
export { defaultCodyCommandContext, isFixupCommand } from './commands'
export type { CodyCommand, CodyCommandContext, CustomCommandType } from './commands'
export type { CodyCommand, CodyCommandContext, CodyCommandType } from './commands/types'
export { DefaultCodyCommands, DefaultChatCommands } from './commands/types'
export { dedupeWith, isDefined, isErrorLike, pluralize } from './common'
export {
ProgrammingLanguage,
Expand Down Expand Up @@ -84,10 +84,8 @@ export type {
ActiveTextEditorDiagnosticType,
ActiveTextEditorSelection,
ActiveTextEditorSelectionRange,
ActiveTextEditorViewControllers,
ActiveTextEditorVisibleContent,
Editor,
VsCodeCommandsController,
} from './editor'
export {
displayPath,
Expand Down
12 changes: 6 additions & 6 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
"when": "cody.activated && editorFocus"
},
{
"command": "cody.action.commands.custom.menu",
"command": "cody.menu.custom-commands",
"category": "Cody Command",
"title": "Custom Commands",
"icon": "$(tools)",
Expand Down Expand Up @@ -384,7 +384,7 @@
"enablement": "config.cody.experimental.guardrails && editorHasSelection"
},
{
"command": "cody.action.commands.menu",
"command": "cody.menu.commands",
"category": "Cody",
"title": "Commands",
"when": "cody.activated",
Expand Down Expand Up @@ -522,7 +522,7 @@
"when": "cody.activated && !editorReadonly && config.cody.internal.unstable"
},
{
"command": "cody.action.commands.menu",
"command": "cody.menu.commands",
"key": "alt+c",
"mac": "alt+c",
"when": "cody.activated"
Expand Down Expand Up @@ -574,7 +574,7 @@
"when": "cody.activated && editorIsOpen"
},
{
"command": "cody.action.commands.custom.menu",
"command": "cody.menu.custom-commands",
"when": "cody.activated"
},
{
Expand Down Expand Up @@ -642,7 +642,7 @@
"group": "command"
},
{
"command": "cody.action.commands.custom.menu",
"command": "cody.menu.custom-commands",
"when": "cody.activated",
"group": "custom-commands"
},
Expand Down Expand Up @@ -693,7 +693,7 @@
],
"editor/title": [
{
"command": "cody.action.commands.menu",
"command": "cody.menu.commands",
"when": "cody.activated && config.cody.editorTitleCommandIcon && resourceScheme == file && !editorReadonly",
"group": "navigation",
"visibility": "visible"
Expand Down
66 changes: 20 additions & 46 deletions vscode/src/chat/chat-view/ChatManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ import { debounce } from 'lodash'
import * as uuid from 'uuid'
import * as vscode from 'vscode'

import {
ChatModelProvider,
type ChatClient,
type ChatEventSource,
type CodyCommand,
type Guardrails,
} from '@sourcegraph/cody-shared'
import { ChatModelProvider, type ChatClient, type Guardrails } from '@sourcegraph/cody-shared'

import type { View } from '../../../webviews/NavBar'
import type { CodyCommandArgs } from '../../commands'
import type { CommandsController } from '../../commands/CommandsController'
import { CODY_PASSTHROUGH_VSCODE_OPEN_COMMAND_ID } from '../../commands/prompt/display-text'
import { CODY_PASSTHROUGH_VSCODE_OPEN_COMMAND_ID } from '../../commands/utils/display-text'
import { isRunningInsideAgent } from '../../jsonrpc/isRunningInsideAgent'
import type { LocalEmbeddingsController } from '../../local-context/local-embeddings'
import type { SymfRunner } from '../../local-context/symf'
Expand All @@ -26,6 +18,7 @@ import type { AuthStatus } from '../protocol'
import { ChatPanelsManager } from './ChatPanelsManager'
import { SidebarViewController, type SidebarViewOptions } from './SidebarViewController'
import type { ChatSession, SimpleChatPanelProvider } from './SimpleChatPanelProvider'
import type { ExecuteChatArguments } from '../../commands/default/ask'
import type { EnterpriseContextFactory } from '../../context/enterprise-context-factory'

export const CodyChatPanelViewType = 'cody.chatPanel'
Expand All @@ -50,8 +43,7 @@ export class ChatManager implements vscode.Disposable {
private enterpriseContext: EnterpriseContextFactory | null,
private localEmbeddings: LocalEmbeddingsController | null,
private symf: SymfRunner | null,
private guardrails: Guardrails,
private commandsController?: CommandsController
private guardrails: Guardrails
) {
logDebug(
'ChatManager:constructor',
Expand All @@ -68,15 +60,12 @@ export class ChatManager implements vscode.Disposable {
this.localEmbeddings,
this.symf,
this.enterpriseContext,
this.guardrails,
this.commandsController
this.guardrails
)

// Register Commands
this.disposables.push(
vscode.commands.registerCommand('cody.action.chat', (input, args) =>
this.executeChat(input, args)
),
vscode.commands.registerCommand('cody.action.chat', args => this.executeChat(args)),
vscode.commands.registerCommand('cody.chat.history.export', () => this.exportHistory()),
vscode.commands.registerCommand('cody.chat.history.clear', () => this.clearHistory()),
vscode.commands.registerCommand('cody.chat.history.delete', item => this.clearHistory(item)),
Expand Down Expand Up @@ -119,40 +108,25 @@ export class ChatManager implements vscode.Disposable {
await chatProvider?.setWebviewView(view)
}

// Execute a chat request in a new chat panel
public async executeChat(question: string, args?: { source?: ChatEventSource }): Promise<void> {
/**
* Execute a chat request in a new chat panel
*/
public async executeChat(args: ExecuteChatArguments): Promise<ChatSession | undefined> {
const requestID = uuid.v4()
telemetryService.log('CodyVSCodeExtension:chat-question:submitted', {
requestID,
...args,
source: args?.source,
})
const chatProvider = await this.getChatProvider()
await chatProvider.handleUserMessageSubmission(requestID, question, 'user-newchat', [], true)
}

// Execute a command request in a new chat panel
public async executeCommand(
command: CodyCommand,
args: CodyCommandArgs,
enabled = true
): Promise<ChatSession | undefined> {
if (!enabled) {
void vscode.window.showErrorMessage(
'This feature has been disabled by your Sourcegraph site admin.'
)
return
}

logDebug('ChatManager:executeCommand:called', command.slashCommand)
if (!vscode.window.visibleTextEditors.length) {
void vscode.window.showErrorMessage('Please open a file before running a command.')
return
}

// Else, open a new chanel panel and run the command in the new panel
const chatProvider = await this.getChatProvider()
await chatProvider.handleCommand(command, args)
return chatProvider
const provider = await this.getChatProvider()
await provider?.handleUserMessageSubmission(
requestID,
args.text,
args?.submitType,
args?.contextFiles ?? [],
args?.addEnhancedContext ?? true
)
return provider
}

private async editChatHistory(treeItem?: vscode.TreeItem): Promise<void> {
Expand Down
5 changes: 1 addition & 4 deletions vscode/src/chat/chat-view/ChatPanelsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
type Guardrails,
} from '@sourcegraph/cody-shared'

import type { CommandsController } from '../../commands/CommandsController'
import type { LocalEmbeddingsController } from '../../local-context/local-embeddings'
import type { SymfRunner } from '../../local-context/symf'
import { logDebug } from '../../log'
Expand Down Expand Up @@ -67,8 +66,7 @@ export class ChatPanelsManager implements vscode.Disposable {
private readonly localEmbeddings: LocalEmbeddingsController | null,
private readonly symf: SymfRunner | null,
private readonly enterpriseContext: EnterpriseContextFactory | null,
private readonly guardrails: Guardrails,
private readonly commandsController?: CommandsController
private readonly guardrails: Guardrails
) {
logDebug('ChatPanelsManager:constructor', 'init')
this.options = {
Expand Down Expand Up @@ -218,7 +216,6 @@ export class ChatPanelsManager implements vscode.Disposable {
symf: isConsumer ? this.symf : null,
enterpriseContext: isConsumer ? null : this.enterpriseContext,
models,
commandsController: this.commandsController,
guardrails: this.guardrails,
})
}
Expand Down