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

CLI: restructure commands (breaking change!) #4792

Merged
merged 7 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ This is a log of all notable changes to the Cody command-line tool. [Unreleased]

### Changed

## 0.2.0

### Changed

- `cody-agent` is now just `cody`. Install the `@sourcegraph/cody` npm package instead of `@sourcegraph/cody-agent`.
- `cody-agent jsonrpc` is now `cody api jsonrpc-stdio`. If you previously relied on calling `node agent/dist/index.js`, now you need to call `node agent/dist/index.js api jsonrpc-stdio`.
- `cody-agent server` is now `cody api jsonrpc-websocket`
- `cody-agent cody-bench` is now `cody internal bench`
- Running `cody` now prints out the help message instead of defaulting to the old `cody-agent jsonrpc` command.

## 0.1.2

### Changed
Expand All @@ -20,7 +30,7 @@ This is a log of all notable changes to the Cody command-line tool. [Unreleased]

### Fixed

- Running `cody-agent help` should work now. It was previously crashing about a missing keytar dependencies.
- Running `cody help` should work now. It was previously crashing about a missing keytar dependencies.

## 0.1.1
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sourcegraph/cody-agent",
"version": "0.1.2",
"name": "@sourcegraph/cody",
"version": "0.2.0",
"description": "Cody JSON-RPC agent for consistent cross-editor support",
"license": "Apache-2.0",
"repository": {
Expand Down
5 changes: 3 additions & 2 deletions agent/src/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ export class TestClient extends MessageHandler {
'--enable-source-maps',
// '--expose-gc', // Uncoment when running memory.test.ts
agentScript,
'jsonrpc',
'api',
'jsonrpc-stdio',
]
: ['jsonrpc']
: ['api', 'jsonrpc-stdio']

const child = spawn(bin, args, {
stdio: 'pipe',
Expand Down
4 changes: 2 additions & 2 deletions agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import { AgentGlobalState } from './AgentGlobalState'
import { AgentProviders } from './AgentProviders'
import { AgentWebviewPanel, AgentWebviewPanels } from './AgentWebviewPanel'
import { AgentWorkspaceDocuments } from './AgentWorkspaceDocuments'
import type { PollyRequestError } from './cli/jsonrpc'
import type { PollyRequestError } from './cli/command-jsonrpc-stdio'
import { codyPaths } from './codyPaths'
import {
MessageHandler,
Expand Down Expand Up @@ -185,7 +185,7 @@ export async function newAgentClient(
const nodeArguments = process.argv0.endsWith('node')
? ['--enable-source-maps', ...process.argv.slice(1, 2)]
: []
nodeArguments.push('jsonrpc')
nodeArguments.push('api', 'jsonrpc-stdio')
const arg0 = clientInfo.codyAgentPath ?? process.argv[0]
const args = clientInfo.codyAgentPath ? [] : nodeArguments
const child = spawn(arg0, args, {
Expand Down
42 changes: 42 additions & 0 deletions agent/src/cli/__snapshots__/command-chat.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`--context-file (animal test) 1`] = `
"command: cody chat chat --context-file animal.ts -m implement a cow. Only print
the code without any explanation.
exitCode: 0
stdout: |+
\`\`\`typescript
class Cow implements StrangeAnimal {
makesSound(): 'moo' {
return 'moo';
}
}
\`\`\`

stderr: ""
"
`;

exports[`--context-repo (squirrel test) 1`] = `
"command: cody chat chat --context-repo github.com/sourcegraph/sourcegraph -m
what is squirrel? Explain as briefly as possible.
exitCode: 0
stdout: >+
Squirrel is a high-performance, open-source object-relational database (ORD)
that provides a SQL interface for storing and querying JSON data. It is
designed to be embedded in applications, making it suitable for use cases such
as caching, analytics, and data storage in edge computing environments.

stderr: ""
"
`;

exports[`--message (hello world test) 1`] = `
"command: cody chat chat -m respond with "hello" and nothing else
exitCode: 0
stdout: |+
hello

stderr: ""
"
`;
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getLanguageForFileName } from '../../language'
import { AutocompleteMatcher } from './AutocompleteMatcher'
import { EvaluationDocument } from './EvaluationDocument'
import { Queries } from './Queries'
import { BenchStrategy } from './cody-bench'
import { BenchStrategy } from './command-bench'
import { isWindows } from './isWindows'

describe.skipIf(isWindows())('AutocompleteMatcher', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type WrappedParser, createParser } from '../../../../vscode/src/tree-si

import { EvaluationDocument, type EvaluationDocumentParams } from './EvaluationDocument'
import type { Queries } from './Queries'
import { BenchStrategy } from './cody-bench'
import { BenchStrategy } from './command-bench'

export type AutocompleteMatchKind =
| 'if_statement'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ProtocolTextDocumentWithUri } from '../../../../vscode/src/jsonrpc/Text
import { AgentTextDocument } from '../../AgentTextDocument'

import type { AutocompleteMatchKind } from './AutocompleteMatcher'
import { BenchStrategy, type CodyBenchOptions } from './cody-bench'
import { BenchStrategy, type CodyBenchOptions } from './command-bench'
import type { EvaluateFileParams } from './evaluateEachFile'

export type EvaluationDocumentParams = Pick<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { CsvWriter } from 'csv-writer/src/lib/csv-writer'
import { rimraf } from 'rimraf'

import { type EvaluationDocument, headerItems } from './EvaluationDocument'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'

export class SnapshotWriter {
public csvWriter: CsvWriter<any> | undefined
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async function loadEvaluationConfig(options: CodyBenchOptions): Promise<CodyBenc
return result
}

export const codyBenchCommand = new commander.Command('cody-bench')
export const benchCommand = new commander.Command('bench')
.description(
'Evaluate Cody autocomplete by running the Agent in headless mode. ' +
'See the repo https://github.com/sourcegraph/cody-bench-data for ' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getLanguageForFileName } from '../../language'
import type { EvaluationDocument } from './EvaluationDocument'
import { Queries } from './Queries'
import { SnapshotWriter } from './SnapshotWriter'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'
import { matchesGlobPatterns } from './matchesGlobPatterns'
import { testCleanup, testInstall } from './testTypecheck'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ModelsService, type PromptString } from '@sourcegraph/cody-shared'
import { SourcegraphNodeCompletionsClient } from '../../../../vscode/src/completions/nodeClient'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'

export interface LlmJudgeScore {
score?: string // 'bad' | 'acceptable' | 'amazing'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { RpcMessageHandler } from '../../jsonrpc-alias'
import { execSync } from 'node:child_process'
import { type AutocompleteMatchKind, AutocompleteMatcher } from './AutocompleteMatcher'
import { EvaluationDocument } from './EvaluationDocument'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'
import { evaluateEachFile } from './evaluateEachFile'
import { matchesGlobPatterns } from './matchesGlobPatterns'
import { triggerAutocomplete } from './triggerAutocomplete'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as vscode from 'vscode'
import YAML from 'yaml'
import type { RpcMessageHandler } from '../../jsonrpc-alias'
import { EvaluationDocument } from './EvaluationDocument'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'
import { evaluateEachFile } from './evaluateEachFile'
import { LlmJudge, type LlmJudgeScore } from './llm-judge'
import { concisenessPrompt, helpfulnessPrompt } from './llm-judge-chat-template'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { RpcMessageHandler } from '../../jsonrpc-alias'
import { renderUnifiedDiff } from '../../renderUnifiedDiff'
import { vscodeRange } from '../../vscode-type-converters'
import { EvaluationDocument } from './EvaluationDocument'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'
import { evaluateEachFile } from './evaluateEachFile'
import { LlmJudge, type LlmJudgeScore } from './llm-judge'
import { llmJudgeFixTemplate } from './llm-judge-fix-template'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getLanguageForFileName } from '../../language'

import { EvaluationDocument } from './EvaluationDocument'
import { SnapshotWriter } from './SnapshotWriter'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'
import { matchesGlobPatterns } from './matchesGlobPatterns'
import { testCleanup, testInstall } from './testTypecheck'
import { triggerAutocomplete } from './triggerAutocomplete'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TestClient } from '../../TestClient'
import { getLanguageForFileName } from '../../language'
import type { ProtocolDiagnostic } from '../../protocol-alias'
import { EvaluationDocument } from './EvaluationDocument'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'
import { evaluateEachFile } from './evaluateEachFile'
import { prettyDiagnostic } from './prettyDiagnostic'
import { runVoidCommand } from './testTypecheck'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'node:path'

import type { TestParameters } from './TestParameters'
import { Timer } from './Timer'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'

async function runCommand(command: string | undefined, cwd: string): Promise<boolean> {
return new Promise<boolean>(resolve => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { WrappedParser } from '../../../../vscode/src/tree-sitter/parser'
import type { AutocompleteMatchKind } from './AutocompleteMatcher'
import type { EvaluationDocument } from './EvaluationDocument'
import type { TestParameters } from './TestParameters'
import type { CodyBenchOptions } from './cody-bench'
import type { CodyBenchOptions } from './command-bench'
import { testParses } from './testParse'
import { testTypecheck } from './testTypecheck'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { TESTING_CREDENTIALS } from '../../../vscode/src/testutils/testing-crede
import { buildAgentBinary, getAgentDir } from '../TestClient'
import { TestWorkspace } from '../TestWorkspace'
import { Streams, StringBufferStream } from './Streams'
import { type ChatOptions, chatAction, chatCommand } from './chat'
import { type ChatOptions, chatAction, chatCommand } from './command-chat'

process.env.CODY_SHIM_TESTING = 'true'
process.env.DISABLE_FEATURE_FLAGS = 'true'

interface ChatCommandResult {
command: string
Expand All @@ -32,6 +33,7 @@ describe('cody chat', () => {
// `--access-token` or `--endpoint` so we modify process.env instead.
process.env.SRC_ACCESS_TOKEN = credentials.token ?? credentials.redactedToken
process.env.SRC_ENDPOINT = credentials.serverEndpoint
process.env.DISABLE_FEATURE_FLAGS = 'true'
process.env.CODY_TELEMETRY_EXPORTER = 'testing'
const args = [...params.args, '--dir', tmp.rootPath, '--silent']

Expand Down
4 changes: 2 additions & 2 deletions agent/src/cli/chat.ts → agent/src/cli/command-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import packageJson from '../../package.json'
import { newEmbeddedAgentClient } from '../agent'
import type { ClientInfo } from '../protocol-alias'
import { Streams } from './Streams'
import { AuthenticatedAccount } from './auth/AuthenticatedAccount'
import { notLoggedIn } from './auth/messages'
import { AuthenticatedAccount } from './command-auth/AuthenticatedAccount'
import { notLoggedIn } from './command-auth/messages'

declare const process: { pkg: { entrypoint: string } } & NodeJS.Process
export interface ChatOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { startPollyRecording } from '../../../vscode/src/testutils/polly'
import { Agent } from '../agent'

import { activate } from '../../../vscode/src/extension.node'
import { booleanOption } from './cody-bench/cli-parsers'
import { booleanOption } from './command-bench/cli-parsers'

interface JsonrpcCommandOptions {
expiresIn?: string | null | undefined
Expand Down Expand Up @@ -57,7 +57,7 @@ const debugPort = process.env.CODY_AGENT_DEBUG_PORT
? Number.parseInt(process.env.CODY_AGENT_DEBUG_PORT, 10)
: 3113

export const jsonrpcCommand = new Command('jsonrpc')
export const jsonrpcCommand = new Command('jsonrpc-stdio')
.description(
'Interact with the Agent using JSON-RPC via stdout/stdin. ' +
'This is the subcommand that is used by Cody clients like the JetBrains and Neovim plugins.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { Command } from 'commander'
import { WebSocketServer } from 'ws'
import { newAgentClient } from '../agent'
import type { RpcMessageHandler } from '../jsonrpc-alias'
import { intOption } from './cody-bench/cli-parsers'
import { intOption } from './command-bench/cli-parsers'

interface ServerOptions {
port: number
}

export const serverCommand = new Command('server')
export const serverCommand = new Command('jsonrpc-websocket')
.description(
'Start a server that opens JSON-RPC connections through websockets. This command does not work at the moment.'
)
.option('--port <number>', 'Which port to listen to', intOption, 7000)
.action(async (options: ServerOptions) => {
const wss = new WebSocketServer({
Expand Down
20 changes: 20 additions & 0 deletions agent/src/cli/command-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Command } from 'commander'

import { authCommand } from './command-auth/command-auth'
import { benchCommand } from './command-bench/command-bench'
import { chatCommand } from './command-chat'
import { jsonrpcCommand } from './command-jsonrpc-stdio'
import { serverCommand } from './command-jsonrpc-websocket'

import { version } from '../../package.json'

export const rootCommand = new Command()
.name('cody')
.version(version)
.description(
'The Cody cli supports running Cody in headless mode and interacting with it via JSON-RPC. Run `cody chat -m "Hello" to get started.'
)
.addCommand(authCommand())
.addCommand(chatCommand())
.addCommand(new Command('api').addCommand(serverCommand).addCommand(jsonrpcCommand))
.addCommand(new Command('internal').addCommand(benchCommand))
20 changes: 0 additions & 20 deletions agent/src/cli/root.ts

This file was deleted.

2 changes: 1 addition & 1 deletion agent/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Agent', () => {
})
expect(valid?.isLoggedIn).toBeTruthy()

// Please don't update the recordings to use a different account without consulting #wg-cody-agent.
// Please don't update the recordings to use a different account without consulting #team-cody-core.
// When changing an account, you also need to update the REDACTED_ hash above.
//
// To update the recordings with the correct account, run the following command
Expand Down
6 changes: 1 addition & 5 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ console.log = console.error
// process.stdout, which breaks the `jsonrpc` command, which uses stdout/stdin
// to communicate with the agent client`jsonrpc` command, which uses
// stdout/stdin to communicate with the agent client.
const rootCommand: Command = require('./cli/root').rootCommand
const rootCommand: Command = require('./cli/command-root').rootCommand

process.on('uncaughtException', e => {
// By default, an uncaught exception will take down the entire process.
Expand All @@ -26,10 +26,6 @@ process.on('uncaughtException', e => {
registerLocalCertificates()

const args = process.argv.slice(2)
const { operands } = rootCommand.parseOptions(args)
if (operands.length === 0) {
args.push('jsonrpc')
}

rootCommand.parseAsync(args, { from: 'user' }).catch(error => {
console.error('Error:', error)
Expand Down
2 changes: 1 addition & 1 deletion agent/src/vscode-shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { AgentQuickPick } from './AgentQuickPick'
import { AgentTabGroups } from './AgentTabGroups'
import { AgentWorkspaceConfiguration } from './AgentWorkspaceConfiguration'
import type { Agent } from './agent'
import { matchesGlobPatterns } from './cli/cody-bench/matchesGlobPatterns'
import { matchesGlobPatterns } from './cli/command-bench/matchesGlobPatterns'
import type { ClientInfo, ExtensionConfiguration } from './protocol-alias'

// Not using CODY_TESTING because it changes the URL endpoint we send requests
Expand Down
Loading
Loading