概述
Cody 是 Sourcegraph 的 AI 编程助手,专注于理解大型代码库。其核心优势是利用 Sourcegraph 的代码搜索和索引技术来理解整个代码库的上下文。Cody 支持代码补全、AI 聊天、命令、代码搜索等功能。与普通 AI 助手不同,Cody 能回答关于整个代码库的问题,例如"这个函数在哪里被调用?"。它支持多种 LLM 后端,包括 Claude、GPT-4、本地模型等。
安装
Cody 通过 IDE 插件安装。支持 VS Code、JetBrains、Neovim、Emacs 等。安装后需要登录 Sourcegraph 账户(免费版)或配置企业实例。
# 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"代码搜索
Cody 集成了 Sourcegraph 的代码搜索能力。可以搜索整个代码库、查找定义、引用、使用模式等。搜索支持正则表达式、文件过滤器、语言过滤器等。这是 Cody 的核心优势,能理解大型代码库。
# Code search example
# In Cody chat:
# "Find all places that call the getUserData function"
# "Which files use React hooks?"
# "Find all TODO comments"
# "Show the inheritance hierarchy of the UserService class"自动补全
Cody 提供智能代码补全,支持单行和多行补全。它基于代码库上下文提供建议,理解项目结构和模式。补全速度快、延迟低。支持多种语言的补全。
# Cody configuration
{
"cody.autocomplete.enabled": true,
"cody.autocomplete.languages": {
"*": true,
"python": true,
"typescript": true
},
"cody.autocomplete.smartThrottle": true
}聊天
Cody Chat 提供对话式 AI 助手,可以回答关于代码库的问题。聊天能理解整个代码库上下文,可引用多个文件。支持代码生成、解释、重构、调试等。聊天记录可保存和搜索。
# 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?"命令
Cody 提供预定义命令来快速执行常见操作,如解释代码、生成测试、修复 bug、生成文档等。也支持自定义命令。命令可针对选中的代码或当前文件执行。
# 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}"
}
}
}上下文
Cody 的核心优势是上下文理解。它可以将整个代码库作为上下文提供准确答案。支持多种上下文来源:当前文件、打开的文件、搜索结果、自定义上下文等。可以手动添加或移除上下文文件。
# Context configuration
{
"cody.context": {
"currentFile": true,
"openTabs": true,
"searchResults": true,
"customPatterns": [
"**/*.ts",
"**/*.tsx"
]
}
}企业版
Cody Enterprise 连接到 Sourcegraph 实例,提供企业级代码理解能力。支持私有代码库、SSO、审计日志、访问控制等。企业版可以利用 Sourcegraph 的代码图提供更深度的代码理解。适合大型企业。
# Enterprise configuration
{
"cody.serverEndpoint": "https://sourcegraph.company.com",
"cody.enterprise": true,
"cody.codebase": "github.com/company/repo"
}配置
Cody 提供丰富的配置选项。可以选择 LLM 后端(Claude、GPT-4、本地模型),调整补全行为、配置上下文等。支持项目级配置。
# 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