Codebase
This class provides the main entrypoint for most programs to analyzing and manipulating codebases.
View source on GitHub
Attributes
classes
List of all Classes in the codebase.
codeowners
List all CodeOnwers in the codebase.
current_commit
Returns the current Git commit that is checked out in the repository.
default_branch
The default branch of this repository.
directories
List all directories in the codebase.
exports
Returns a list of all Export nodes in the codebase.
external_modules
Returns a list of all external modules in the codebase.
functions
List of all Functions in the codebase.
github
Access GitHub operations through the repo operator.
global_vars
List of all GlobalVars in the codebase.
imports
Returns a list of all Import nodes in the codebase.
interfaces
Retrieves all interfaces in the codebase.
language
The programming language of the repository.
name
The name of the repository.
repo_path
The path to the repository.
symbols
List of all top-level Symbols (Classes, Functions, etc.) in the codebase. Excludes Class
types
List of all Types in the codebase (Typescript only).
Methods
ai
Generates a response from the AI based on the provided prompt, target, and context.
View source on GitHub
Parameters
promptrequiredThe text prompt to send to the AI.
targetdefault: NoneAn optional editable object (like a function, class, etc.) that provides the main focus for the AI's response.
contextdefault: NoneAdditional context to help inform the AI's response.
modeldefault: "gpt-4o"The AI model to use for generating the response. Defaults to "gpt-4o".
Returns
The generated response from the AI.
checkout
Checks out a git branch or commit and syncs the codebase graph to the new state.
View source on GitHub
Parameters
commitdefault: NoneHash or GitCommit object to checkout. Cannot be used with branch.
branchdefault: NoneName of branch to checkout. Cannot be used with commit.
create_if_missingdefault: FalseIf True, creates the branch if it doesn't exist. Defaults to False.
remotedefault: FalseIf True, attempts to pull from remote when checking out branch. Defaults to False.
Returns
The result of the checkout operation.
commit
Commits all staged changes to the codebase graph and synchronizes the graph with the filesystem if specified.
View source on GitHub
Parameters
sync_graphdefault: TrueWhether to synchronize the graph after committing changes. Defaults to True.
Returns
create_directory
Creates a directory at the specified path.
View source on GitHub
Parameters
dir_pathrequiredThe path where the directory should be created.
exist_okdefault: FalseIf True, don't raise an error if the directory already exists. Defaults to False.
parentsdefault: FalseIf True, create any necessary parent directories. Defaults to False.
Returns
create_file
Creates a new file in the codebase with specified content.
View source on GitHub
Parameters
filepathrequiredThe path where the file should be created.
contentdefault: ""The content of the file to be created. Defaults to empty string.
syncdefault: TrueWhether to sync the graph after creating the file. Defaults to True.
Returns
The newly created file object.
create_pr
Creates a pull request from the current branch to the repository's default branch.
View source on GitHub
Parameters
titlerequiredThe title for the pull request
bodyrequiredThe description/body text for the pull request
Returns
The created GitHub pull request object
create_pr_comment
Create a comment on a pull request
View source on GitHub
Returns
create_pr_review_comment
Create a review comment on a pull request.
View source on GitHub
Returns
files
A list property that returns all files in the codebase.
View source on GitHub
Returns
A sorted list of source files in the codebase.
find_by_span
Finds editable objects that overlap with the given source code span.
View source on GitHub
Parameters
spanrequiredThe span object containing the filepath and byte range to search within.
Returns
A list of Editable objects that overlap with the given span.
from_files
Creates a Codebase instance from multiple files.
View source on GitHub
Returns
A Codebase instance initialized with the provided files
from_repo
Fetches a codebase from GitHub and returns a Codebase instance.
View source on GitHub
Parameters
repo_namerequiredThe name of the repository in format "owner/repo"
tmp_dirdefault: "/tmp/codegen"The directory to clone the repo into. Defaults to /tmp/codegen
commitdefault: NoneThe specific commit hash to clone. Defaults to HEAD
shallowrequiredWhether to do a shallow clone. Defaults to True
languagerequiredThe programming language of the repo. Defaults to None.
configrequiredConfiguration for the codebase. Defaults to pre-defined defaults if None.
secretsrequiredConfiguration for the secrets. Defaults to empty values if None.
Returns
A Codebase instance initialized with the cloned repository
from_string
Creates a Codebase instance from a string of code.
View source on GitHub
Returns
A Codebase instance initialized with the provided code Example: >>> # Python code >>> code = "def add(a, b): return a + b" >>> codebase = Codebase.from_string(code, language="python") >>> # TypeScript code >>> code = "function add(a: number, b: number): number { return a + b; }" >>> codebase = Codebase.from_string(code, language="typescript")
get_class
Returns a class that matches the given name.
View source on GitHub
Parameters
class_namerequiredThe name of the class to find.
optionaldefault: FalseIf True, return None when class is not found instead of raising ValueError. Defaults to False.
Returns
The class with the given name, or None if optional=True and class not found.
get_directory
Returns Directory by dir_path, or full path to the directory from codebase root.
View source on GitHub
Parameters
dir_pathrequiredThe path to the directory to retrieve.
optionaldefault: FalseIf True, return None when directory is not found. If False, raise ValueError.
Returns
The Directory object if found, None if optional=True and directory not found.
get_file
Retrieves a file from the codebase by its filepath.
View source on GitHub
Parameters
filepathrequiredThe path to the file, relative to the codebase root.
optionaldefault: FalseIf True, return None if file not found. If False, raise ValueError.
ignore_casedefault: FalseIf True, ignore case when checking for file existence. Defaults to False.
Returns
The source file if found, None if optional=True and file not found.
get_function
Retrieves a function from the codebase by its name.
View source on GitHub
Parameters
function_namerequiredThe name of the function to retrieve.
optionaldefault: FalseIf True, returns None when function is not found instead of raising ValueError.
Returns
The matching function if found. If optional=True and no match is found, returns None.
get_modified_symbols_in_pr
Get all modified symbols in a pull request
View source on GitHub
Returns
get_relative_path
Calculates a relative path from one file to another, removing the extension from the target file.
View source on GitHub
Parameters
from_filerequiredThe file path from which the relative path will be computed.
to_filerequiredThe file path (whose extension will be removed) to which the relative path will be computed.
Returns
The relative path from `from_file` to `to_file` (with the extension removed from `to_file`).
get_symbol
Returns a Symbol by name from the codebase.
View source on GitHub
Parameters
symbol_namerequiredThe name of the symbol to find.
optionaldefault: FalseIf True, returns None when symbol is not found. If False, raises ValueError. Defaults to False.
Returns
The matched Symbol if found, None if not found and optional=True.
get_symbols
Retrieves all symbols in the codebase that match the given symbol name.
View source on GitHub
Parameters
symbol_namerequiredThe name of the symbols to retrieve.
Returns
A list of Symbol objects that match the given name, sorted alphabetically.
git_commit
Stages + commits all changes to the codebase and git.
View source on GitHub
Parameters
messagerequiredThe commit message
verifydefault: FalseWhether to verify the commit before committing. Defaults to False.
Returns
The commit object if changes were committed, None otherwise.
has_directory
Returns a boolean indicating if a directory exists in the codebase.
View source on GitHub
Parameters
dir_pathrequiredThe path to the directory to check for, relative to the codebase root.
Returns
True if the directory exists in the codebase, False otherwise.
has_file
Determines if a file exists in the codebase.
View source on GitHub
Parameters
filepathrequiredThe filepath to check for existence.
ignore_casedefault: FalseIf True, ignore case when checking for file existence. Defaults to False.
Returns
True if the file exists in the codebase, False otherwise.
has_symbol
Returns whether a symbol exists in the codebase.
View source on GitHub
Parameters
symbol_namerequiredThe name of the symbol to look for.
Returns
True if a symbol with the given name exists in the codebase, False otherwise.
reset
Resets the codebase by
View source on GitHub
Returns
set_ai_key
Sets the OpenAI key for the current Codebase instance.
View source on GitHub
Returns
set_session_options
Sets the session options for the current codebase.
View source on GitHub
Parameters
max_transactionsrequiredThe maximum number of transactions
max_secondsrequiredThe maximum duration in seconds for a session
max_ai_requestsrequiredThe maximum number of AI requests
Returns
should_fix
Returns True if the flag should be fixed based on the current mode and active group.
View source on GitHub
Parameters
flagrequiredThe code flag to check.
Returns
True if the flag should be fixed, False if it should be ignored. Returns False in find mode. Returns True if no active group is set. Returns True if the flag's hash exists in the active group hashes.
visualize
Visualizes a NetworkX graph or Plotly figure.
View source on GitHub
Parameters
GrequiredA NetworkX graph or Plotly figure to visualize
rootdefault: NoneThe root node to visualize around. When specified, the visualization will be centered on this node. Defaults to None.