Skip to content

Sourcegraph Cody

Sourcegraph

Sourcegraph 推出的 AI 编程助手,擅长理解大型代码库。

Official Site
01

概述

Cody 是 Sourcegraph 的 AI 编程助手,专注于理解大型代码库。其核心优势是利用 Sourcegraph 的代码搜索和索引技术来理解整个代码库的上下文。Cody 支持代码补全、AI 聊天、命令、代码搜索等功能。与普通 AI 助手不同,Cody 能回答关于整个代码库的问题,例如"这个函数在哪里被调用?"。它支持多种 LLM 后端,包括 Claude、GPT-4、本地模型等。

02

安装

Cody 通过 IDE 插件安装。支持 VS Code、JetBrains、Neovim、Emacs 等。安装后需要登录 Sourcegraph 账户(免费版)或配置企业实例。

bash
# VS Code installation
# 1. Open the extension store
# 2. Search for "Cody"
# 3. Click install
# 4. Restart VS Code
# 5. Log in to your Sourcegraph account

# JetBrains installation
# Settings -> Plugins -> Search for "Cody"
04

自动补全

Cody 提供智能代码补全,支持单行和多行补全。它基于代码库上下文提供建议,理解项目结构和模式。补全速度快、延迟低。支持多种语言的补全。

bash
# Cody configuration
{
  "cody.autocomplete.enabled": true,
  "cody.autocomplete.languages": {
    "*": true,
    "python": true,
    "typescript": true
  },
  "cody.autocomplete.smartThrottle": true
}
05

聊天

Cody Chat 提供对话式 AI 助手,可以回答关于代码库的问题。聊天能理解整个代码库上下文,可引用多个文件。支持代码生成、解释、重构、调试等。聊天记录可保存和搜索。

bash
# Use Chat
# Shortcut: Alt+C

# Example conversations:
# "Explain what this function does"
# "What is the call chain of this API?"
# "Add error handling to this function"
# "Which places need to be modified to support the new requirement?"
06

命令

Cody 提供预定义命令来快速执行常见操作,如解释代码、生成测试、修复 bug、生成文档等。也支持自定义命令。命令可针对选中的代码或当前文件执行。

bash
# Built-in commands
# /explain - explain code
# /test - generate tests
# /smell - detect code smells
# /doc - generate documentation

# Custom commands
# .vscode/cody.json
{
  "commands": {
    "refactor": {
      "description": "Refactor the selected code",
      "prompt": "Refactor the following code to improve readability: {selection}"
    }
  }
}
07

上下文

Cody 的核心优势是上下文理解。它可以将整个代码库作为上下文提供准确答案。支持多种上下文来源:当前文件、打开的文件、搜索结果、自定义上下文等。可以手动添加或移除上下文文件。

bash
# Context configuration
{
  "cody.context": {
    "currentFile": true,
    "openTabs": true,
    "searchResults": true,
    "customPatterns": [
      "**/*.ts",
      "**/*.tsx"
    ]
  }
}
08

企业版

Cody Enterprise 连接到 Sourcegraph 实例,提供企业级代码理解能力。支持私有代码库、SSO、审计日志、访问控制等。企业版可以利用 Sourcegraph 的代码图提供更深度的代码理解。适合大型企业。

bash
# Enterprise configuration
{
  "cody.serverEndpoint": "https://sourcegraph.company.com",
  "cody.enterprise": true,
  "cody.codebase": "github.com/company/repo"
}
09

配置

Cody 提供丰富的配置选项。可以选择 LLM 后端(Claude、GPT-4、本地模型),调整补全行为、配置上下文等。支持项目级配置。

bash
# Full configuration example
{
  "cody.autocomplete.enabled": true,
  "cody.chat.model": "claude-3-5-sonnet",
  "cody.autocomplete.model": "starcoder-2",
  "cody.context.currentFile": true,
  "cody.telemetry": "off"
}

Ready to try Sourcegraph Cody?

Visit the official site for the latest version and full documentation.

Visit Sourcegraph Cody