OpenCode

repository·main·Indexed 12 days ago

https://github.com/opencode-ai/opencode

A terminal-based AI assistant for developers featuring an interactive TUI for coding assistance, debugging, and tool execution. It supports multiple AI providers including OpenAI, Anthropic, Google, AWS Bedrock, and self-hosted OpenAI-compatible endpoints. OpenCode includes built-in tools for file system interaction, shell execution, and integration with Model Context Protocol (MCP) servers and Language Server Protocol (LSP) for code intelligence.

Tokens
7.3K
Snippets
26
Records
36
Agent score
96%

What's inside OpenCode

  1. Create and use Custom Commands

    main

    Custom commands are predefined prompts stored as Markdown files. When executed, the file content is sent to the AI assistant.

    Command Locations

    1. User Commands (prefixed with user:):
      • $XDG_CONFIG_HOME/opencode/commands/ (e.g., ~/.config/opencode/commands/)
      • $HOME/.opencode/commands/
    2. Project Commands (prefixed with project:):
      • <PROJECT DIR>/.opencode/commands/

    Creating Commands

    Each .md file name (without extension) becomes the command ID. Subdirectories create nested IDs (e.g., ~/.config/opencode/commands/git/commit.md becomes user:git:commit).

    Using Arguments

    Use $NAME placeholders (uppercase, numbers, underscores, starting with a letter) to define arguments. OpenCode will prompt for these values at runtime.

    Example ~/.config/opencode/commands/prime-context.md:

    RUN git ls-files
    READ README.md

    Example with arguments:

    # Fetch Context for Issue $ISSUE_NUMBER
    
    RUN gh issue view $ISSUE_NUMBER --json title,body,comments
    RUN git grep --author="$AUTHOR_NAME" -n .

    Execution

    1. Press Ctrl+K to open the command dialog.
    2. Select your command (prefixed with user: or project:).
    3. Press Enter to execute.
    # Fetch Context for Issue $ISSUE_NUMBER
    
    RUN gh issue view $ISSUE_NUMBER --json title,body,comments
    RUN git grep --author="$AUTHOR_NAME" -n .
    RUN grep -R "$SEARCH_PATTERN" $DIRECTORY
  2. OpenCode Keyboard Shortcuts

    main

    OpenCode uses various keyboard shortcuts to navigate the TUI, manage sessions, and control the editor.

    Global Shortcuts

    • Ctrl+C: Quit application
    • Ctrl+? or ?: Toggle help dialog
    • Ctrl+L: View logs
    • Ctrl+A: Switch session
    • Ctrl+K: Command dialog
    • Ctrl+O: Toggle model selection dialog
    • Esc: Close current overlay/dialog or return to previous mode

    Chat Page Shortcuts

    • Ctrl+N: Create new session
    • Ctrl+X: Cancel current operation/generation
    • i: Focus editor (when not in writing mode)
    • Esc: Exit writing mode and focus messages

    Editor Shortcuts

    • Ctrl+S or Enter: Send message
    • Ctrl+E: Open external editor
    • Esc: Blur editor and focus messages

    Dialog Shortcuts

    • Session Dialog: /k (Previous), /j (Next), Enter (Select), Esc (Close)
    • Model Dialog: /k (Up), /j (Down), /h (Prev provider), /l (Next provider), Esc (Close)
    • Permission Dialog: /left (Left), /right/tab (Right), Enter/space (Confirm), a (Allow), A (Allow for session), d (Deny)

    Logs Page

    • Backspace or q: Return to chat page
  3. Configure a self-hosted model provider

    main

    OpenCode can use models from a self-hosted (OpenAI-compatible) provider. To enable this, set the LOCAL_ENDPOINT environment variable to the URL of your local endpoint. This tells OpenCode to route model requests to your specified address instead of the default provider.

    LOCAL_ENDPOINT=http://localhost:1235/v1
  4. Run OpenCode in Non-interactive Prompt Mode

    main

    For scripting or automation, you can run OpenCode without the TUI by passing a prompt directly via the --prompt (or -p) flag. In this mode, all permissions are auto-approved, and the output is printed to standard output before the application exits.

    Output Formats

    Use the --output-format (or -f) flag to specify the output type:

    • text: Plain text output (default)
    • json: Output wrapped in a JSON object

    Usage Examples

    # Run a single prompt and print the AI's response to the terminal
    opencode -p "Explain the use of context in Go"
    
    # Get response in JSON format
    opencode -p "Explain the use of context in Go" -f json
    
    # Run without showing the spinner (useful for scripts)
    opencode -p "Explain the use of context in Go" -q
    opencode -p "Explain the use of context in Go" -f json
  5. Use the OpenCode JSON Schema for validation and autocompletion

    main

    Once you have generated opencode-schema.json, you can use it in the following ways:

    1. Editor Integration: Configure editors like VS Code or JetBrains IDEs to use the generated schema for .opencode.json files to get real-time validation and autocompletion.
    2. Validation Tools: Use CLI tools like jsonschema to programmatically validate your configuration files against the schema.
    3. Documentation: Refer to the schema to understand available configuration options, types, and default values.
  6. Build OpenCode from source

    main

    To build OpenCode manually, ensure you have Go 1.24.0 or higher installed. Clone the repository, use go build to generate the binary, and then execute the resulting file.

    # Clone the repository
    git clone https://github.com/opencode-ai/opencode.git
    cd opencode
    
    # Build
    go build -o opencode
    
    # Run
    ./opencode
  7. Install OpenCode

    main

    You can install OpenCode using several methods depending on your operating system and preferred package manager.

    Using the Install Script

    To install the latest version or a specific version via curl:

    # Install the latest version
    curl -fsSL https://raw.githubusercontent.com/opencode-ai/opencode/refs/heads/main/install | bash
    
    # Install a specific version
    curl -fsSL https://raw.githubusercontent.com/opencode-ai/opencode/refs/heads/main/install | VERSION=0.1.0 bash

    Using Homebrew (macOS and Linux)

    brew install opencode-ai/tap/opencode

    Using AUR (Arch Linux)

    # Using yay
    yay -S opencode-ai-bin
    
    # Using paru
    paru -S opencode-ai-bin

    Using Go

    go install github.com/opencode-ai/opencode@latest
    # Install the latest version
    curl -fsSL https://raw.githubusercontent.com/opencode-ai/opencode/refs/heads/main/install | bash
  8. Generate the OpenCode JSON Schema

    main

    Use the cmd/schema tool to generate a JSON Schema file. This schema is used to validate .opencode.json configuration files and enable autocompletion in supported editors.

    Run the following command to output the schema to a file named opencode-schema.json:

    go run cmd/schema/main.go > opencode-schema.json
  9. Use GitHub Copilot with OpenCode

    main

    Copilot support is experimental. To use it, you must have Copilot chat enabled in your GitHub settings and use one of the following:

    • VSCode GitHub Copilot chat extension
    • GitHub gh CLI
    • Neovim GitHub Copilot plugin (copilot.vim or copilot.lua)
    • A GitHub token with copilot permissions

    Ensure you have authenticated the tool with your GitHub account. If using an explicit token, set the $GITHUB_TOKEN environment variable or add it to opencode.json under providers.copilot.apiKey.

  10. Use the OpenCode CLI

    main

    OpenCode is a terminal-based AI assistant for software development. It can be used in two primary modes:

    1. Interactive Mode: Launches a full Terminal User Interface (TUI) with an interactive chat interface, code analysis, and LSP integration.
    2. Non-Interactive Mode: Allows you to send a single prompt and receive a response directly in your terminal, which is useful for scripting or quick queries.

    To start the interactive TUI, simply run the command without additional arguments.

    # Run in interactive mode
    opencode
  11. Configure LSP (Language Server Protocol)

    main

    OpenCode integrates with LSP to provide code intelligence. Currently, the AI assistant uses LSP primarily through the diagnostics tool to check for errors and suggest fixes.

    Configure language servers in the lsp section of your configuration file.

    {
      "lsp": {
        "go": {
          "disabled": false,
          "command": "gopls"
        },
        "typescript": {
          "disabled": false,
          "command": "typescript-language-server",
          "args": ["--stdio"]
        }
      }
    }
  12. Configure a self-hosted model in the agents section

    main

    To specify a particular model from your self-hosted provider, update the agents section in your OpenCode configuration file. You can define the model name and set the reasoningEffort level for specific agents (e.g., the coder agent).

    {
      "agents": {
        "coder": {
          "model": "local.granite-3.3-2b-instruct@q8_0",
          "reasoningEffort": "high"
        }
      }
    }