PriNova / codypy

Python Binding for Cody Agent from Sourcegraph: JSON-RPC over TCP or stdio. Establish seamless connectivity with the Cody Agent using this Python binding. Leverage the power of JSON-RPC over TCP or stdio for efficient communication.
MIT License
19 stars 6 forks source link

codypy πŸπŸ€–

This is a WIP (work-in-progress) project πŸš§πŸ‘·β€β™‚οΈ

codypy is a Python wrapper binding to Cody Agent through establishing a connection to the Cody-Agent server from Sourcegraph Cody using JSON-RPC (Remote Procedure Call) protocol over a TCP/stdio connection. It allows sending and receiving JSON-RPC messages asynchronously. πŸ“¨πŸ“₯

Note 1: You need to register an account at Sourcegraph and create an API key.

Note 2: This project is currently in an experimental alpha stage. The API and functionality may change and break in future versions. βš οΈπŸ”§

Features

Requirements

Installation

Linux

  1. Clone the repository:

    git clone https://github.com/PriNova/codypy.git
  2. Navigate to the project directory:

    cd codypy
  3. Ensure you have Python 3.7 or higher installed:

    python --version
  4. The asyncio library is included in the Python standard library, so no additional installation is required.

  5. Create and activate a virtual environment:

    python -m venv venv
    source venv/bin/activate
  6. Install the dependencies from the requirements.txt file:

    pip install -r requirements.txt
  7. Rename the provided env.example file to .env and set the SRC_ACCESS_TOKEN value to your API key and the path BINARY_PATH to where the cody agent binary should be downloaded and accessed. Use the following command in Linux to rename your file:

    mv env.example .env
  8. Run the script using python main.py.

You are now ready to use codypy!

You can also install the package in dev mode via pip install -e .

Usage as a library

  1. Required to set the 'BINARY_PATH' where to download or use the agent binary
  2. Required to set the 'SRC_ACCESS_TOKEN' env to the API Token from your Sourcegraph account.
  3. Set your workspace path in the 'workspaceRootUri' property to your local GitHub repository.
  4. Run the example script using python main.py.
  5. The script will attempt to connect to the Cody Agent.
  6. If the connection is successful, it will send an initialization message to the server.
  7. The script will then receive and process JSON-RPC messages from the server.
  8. It will extract and display the method and result from the received messages if is_debugging is set to True.
  9. You will be in 'chat' mode, where you can have a conversation with the Cody Agent based on your input and enhanced context about your codebase.
  10. The script will continue to receive messages until you input /quit. The server closes then the connection.

Usage as CLI tool

If installed as a package like mentioned above, you can also use codypy as a CLI tool. Simply export SRC_ACCESS_TOKEN and BINARY_PATH to your environment and in the terminal execute codypy-cli --help to see the available options and flags.

Example

For an example of initializing and chatting, look at main.py file

This example demonstrates how to use a complete cycle to establish a connection to the server and process JSON-RPC messages.

Roadmap

License

Copyright notices for third-party code included are licensed under their respective licenses.

This project is licensed under the MIT License.