Codebase
Main interface for codemods to interact with codebases, including utility methods etc..
Properties
classes
List of all Classes in the codebase
current_commit
The current commit checked out
default_branch
The default branch of this repository
directories
List of all Directories in the codebase
external_modules
List of all ExternalModules in the codebase
files
List of all Files in the codebase. If it’s a Python codebase, this will return PyFiles (python files). If
it’s a Typescript codebase, this will return TSFiles (typescript files). Does not include non-source files, such as
markdown files, image files, etc. Use codebase.get_file
to get a specific file by path, or use
codebase.get_files()
to get all files in the codebase.
functions
List of all Functions in the codebase
global_vars
List of all GlobalVars in the codebase
imports
List of all Imports in the codebase
interfaces
List of all Interfaces in the codebase (Typescript only)
symbols
List of all top-level Symbols (Classes, Functions, etc.) in the codebase. Excludes Class methods.
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.
This method sends a prompt to the AI client, optionally including a target and context to provide additional information. The AI processes the input and generates a response, which is then returned as a string.
By default, there is a maximum of 150 AI requests per codemod execution. You can change this limit by calling codebase.set_max_ai_requests with the number of requests you want to allow.
Args: prompt (str): The prompt to send to the AI. target (Editable | None): The target editable object for the AI request. context (Editable | list[Editable] | dict[str, Editable | list[Editable]] | None): The context for the AI request. model (str): The model to use for the AI request. Defaults to “gpt-4o-mini”.
Returns: str: The response generated by the AI.
Raises: MaxAIRequestsError: If the maximum number of AI requests is exceeded.
Example:
checkout
Discard changes, checkout a branch using git, then sync the codebase to the new branch.
Args: commit: Commit to checkout branch: Branch to checkout create_if_missing: If the branch doesn’t exist, create one remote: Try pulling a remote branch Examples:
codebase.checkout(branch=“branch”, create_if_missing=True)
commit
Commits all staged changes to the codebase. Must be called if you intend to have multiple overlapping edits on a single entity For example:
create_directory
Creates a directory at the specified dirpath
create_file
Creates a file with specified content at specified filepath
flag_ai
Determines whether to flag the symbol, file, attribute, or message using AI.
Flagging returns either a Codeflag object or None.
Use flag_ai
when you want to conditionally flag an entity based on the AI’s response.
By default, there is a maximum of 150 AI requests per codemod execution. You can change this limit by calling codebase.set_max_ai_requests with the number of requests you want to allow.
Args: prompt (str): The prompt to send to the AI. target (Editable | None): The target editable object for the AI request. context (Editable | list[Editable] | dict[str, Editable | list[Editable]] | None): The context for the AI request.
Returns: CodeFlag | None: The flag object if the AI request should be flagged, otherwise None.
Raises: MaxAIRequestsError: If the maximum number of AI requests is exceeded.
Example:
flag_instance
Flags a symbol, file or import. This enables enhanced tracking of changes and splitting into smaller PRs, etc. Should be called once per flaggable entity and should be called before any edits are made to the entity.
get_class
Returns a class that matches the given name. Raises ValueError if there are multiple.
get_directory
Returns Directory by dir_path
, or full path to the directory from codebase root.
get_file
Returns SourceFile by filepath
, or full path to the file from codebase root.
get_function
Returns a function that matches the given name. Raises ValueError if there are multiple.
get_symbol
Returns Symbol by name. Raises ValueError if there are multiple
get_symbols
Optimized implementation to retrieve multiple symbols by the same name.
Applies when there is a symbol without a unique name, in which case get_symbol
will raise ValueError
git_commit
Commits all staged changes to the codebase and git.
Returns: The commit hash if it was created
has_directory
Returns True iff the directory exists in the codebase
has_file
Returns True iff the file exists in the codebase
has_symbol
Returns True iff the symbol exists in the codebase
set_max_ai_requests
Sets the maximum number of AI requests. Hard limit capped at HARD_MAX_AI_LIMIT
Args: max_ai_requests (int | None): The maximum number of AI requests.
should_fix
Returns True if the flag should be fixed. This is used to filter out flags that are not in the active group.
visualize
Visualizes a NetworkX graph.
Args: G (Graph | go.Figure): A NetworkX DiGraph object root (Editable | str | int | None): The root node to visualize. Defaults to None.
Returns: None
Was this page helpful?