Integrating with Supersonic
kit
excels at understanding and analyzing codebases, while Supersonic provides a high-level Python API specifically designed for programmatically creating GitHub Pull Requests. Combining them allows you to build powerful workflows that analyze code, generate changes, and automatically propose those changes via PRs.
The Workflow: Analyze with kit
, Act with Supersonic
Section titled “The Workflow: Analyze with kit, Act with Supersonic”A typical integration pattern looks like this:
- Analyze Code with
kit
: Usekit.Repository
methods likeextract_symbols
,find_symbol_usages
, orsearch_semantic
to understand the codebase or identify areas for modification. - Generate Changes: Based on the analysis (potentially involving an LLM), generate the new code content or identify necessary file modifications.
- Create PR with
Supersonic
: UseSupersonic
’s simple API (create_pr_from_content
,create_pr_from_file
, etc.) to package the generated changes into a new Pull Request on GitHub.
Example: AI Refactoring Suggestion
Section titled “Example: AI Refactoring Suggestion”Imagine an AI tool that uses kit
to analyze a Python file, identifies a potential refactoring, generates the improved code, and then uses Supersonic
to create a PR.
import kitfrom supersonic import Supersonicimport os
# Assume kit.Repository is initialized with a local pathLOCAL_REPO_PATH = "/path/to/your/local/repo/clone"# repo_analyzer = kit.Repository(LOCAL_REPO_PATH)# Note: kit analysis methods like extract_symbols would still be used here in a real scenario.
# Assume 'ai_generate_refactoring' is your function that uses an LLM# potentially fed with context from kit (not shown here for brevity)def ai_generate_refactoring(original_code: str) -> str: # ... your AI logic here ... improved_code = original_code.replace("old_function", "new_function") # Simplified example return improved_code
# --- Configuration ---GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")REPO_OWNER_SLASH_NAME = "your-org/your-repo" # For Supersonic PR creationRELATIVE_FILE_PATH = "src/legacy_module.py" # Relative path within the repoFULL_FILE_PATH = os.path.join(LOCAL_REPO_PATH, RELATIVE_FILE_PATH)TARGET_BRANCH = "main" # Or dynamically determine
# --- Main Workflow ---
try: # 1. Get original content (assuming local repo) if not os.path.exists(FULL_FILE_PATH): print(f"Error: File not found at {FULL_FILE_PATH}") exit()
with open(FULL_FILE_PATH, 'r') as f: original_content = f.read()
# 2. Generate Changes (using AI or other logic) refactored_content = ai_generate_refactoring(original_content)
if refactored_content != original_content: # 3. Create PR with Supersonic supersonic_client = Supersonic(GITHUB_TOKEN) pr_title = f"AI Refactor: Improve {RELATIVE_FILE_PATH}" pr_body = f""" AI analysis suggests refactoring in `{RELATIVE_FILE_PATH}`.
This PR applies the suggested changes. Please review carefully. """
pr_url = supersonic_client.create_pr_from_content( repo=REPO_OWNER_SLASH_NAME, content=refactored_content, upstream_path=RELATIVE_FILE_PATH, # Path within the target repo title=pr_title, description=pr_body, base_branch=TARGET_BRANCH, labels=["ai-refactor", "needs-review"], draft=True # Good practice for AI suggestions ) print(f"Successfully created PR: {pr_url}") else: print("No changes generated.")
except Exception as e: print(f"An error occurred: {e}")
This example illustrates how kit
’s analytical strengths can be combined with Supersonic
’s action-oriented PR capabilities to build powerful code automation.