|
| 1 | +import codegen |
| 2 | +from codegen import Codebase |
| 3 | +from codegen.sdk.core.external_module import ExternalModule |
| 4 | +from codegen.sdk.core.import_resolution import Import |
| 5 | +from codegen.sdk.core.symbol import Symbol |
| 6 | + |
| 7 | + |
| 8 | +def hop_through_imports(imp: Import) -> Symbol | ExternalModule: |
| 9 | + """Finds the root symbol for an import""" |
| 10 | + if isinstance(imp.imported_symbol, Import): |
| 11 | + return hop_through_imports(imp.imported_symbol) |
| 12 | + return imp.imported_symbol |
| 13 | + |
| 14 | + |
| 15 | +def get_extended_context(symbol: Symbol, degree: int) -> tuple[set[Symbol], set[Symbol]]: |
| 16 | + """Recursively collect dependencies and usages up to the specified degree. |
| 17 | +
|
| 18 | + Args: |
| 19 | + symbol: The symbol to collect context for |
| 20 | + degree: How many levels deep to collect dependencies and usages |
| 21 | +
|
| 22 | + Returns: |
| 23 | + A tuple of (dependencies, usages) where each is a set of related Symbol objects |
| 24 | + """ |
| 25 | + dependencies = set() |
| 26 | + usages = set() |
| 27 | + |
| 28 | + if degree > 0: |
| 29 | + # Collect direct dependencies |
| 30 | + for dep in symbol.dependencies: |
| 31 | + # Hop through imports to find the root symbol |
| 32 | + if isinstance(dep, Import): |
| 33 | + dep = hop_through_imports(dep) |
| 34 | + |
| 35 | + if isinstance(dep, Symbol) and dep not in dependencies: |
| 36 | + dependencies.add(dep) |
| 37 | + dep_deps, dep_usages = get_extended_context(dep, degree - 1) |
| 38 | + dependencies.update(dep_deps) |
| 39 | + usages.update(dep_usages) |
| 40 | + |
| 41 | + # Collect usages in the current symbol |
| 42 | + for usage in symbol.usages: |
| 43 | + usage_symbol = usage.usage_symbol |
| 44 | + # Hop through imports for usage symbols too |
| 45 | + if isinstance(usage_symbol, Import): |
| 46 | + usage_symbol = hop_through_imports(usage_symbol) |
| 47 | + |
| 48 | + if isinstance(usage_symbol, Symbol) and usage_symbol not in usages: |
| 49 | + usages.add(usage_symbol) |
| 50 | + usage_deps, usage_usages = get_extended_context(usage_symbol, degree - 1) |
| 51 | + dependencies.update(usage_deps) |
| 52 | + usages.update(usage_usages) |
| 53 | + |
| 54 | + return dependencies, usages |
| 55 | + |
| 56 | + |
| 57 | +@codegen.function("document-functions") |
| 58 | +def run(codebase: Codebase): |
| 59 | + # Define the maximum degree of dependencies and usages to consider for context |
| 60 | + N_DEGREE = 2 |
| 61 | + |
| 62 | + # Filter out test and tutorial functions first |
| 63 | + functions = [f for f in codebase.functions if not any(pattern in f.name.lower() for pattern in ["test", "tutorial"]) and not any(pattern in f.filepath.lower() for pattern in ["test", "tutorial"])] |
| 64 | + |
| 65 | + # Track progress for user feedback |
| 66 | + total_functions = len(functions) |
| 67 | + processed = 0 |
| 68 | + |
| 69 | + print(f"Found {total_functions} functions to process (excluding tests and tutorials)") |
| 70 | + |
| 71 | + for function in functions: |
| 72 | + processed += 1 |
| 73 | + |
| 74 | + # Skip if already has docstring |
| 75 | + if function.docstring: |
| 76 | + print(f"[{processed}/{total_functions}] Skipping {function.name} - already has docstring") |
| 77 | + continue |
| 78 | + |
| 79 | + print(f"[{processed}/{total_functions}] Generating docstring for {function.name} at {function.filepath}") |
| 80 | + |
| 81 | + # Collect context using N-degree dependencies and usages |
| 82 | + dependencies, usages = get_extended_context(function, N_DEGREE) |
| 83 | + |
| 84 | + # Generate a docstring using the AI with the context |
| 85 | + docstring = codebase.ai( |
| 86 | + """ |
| 87 | + Generate a docstring for this function using the provided context. |
| 88 | + The context includes: |
| 89 | + - dependencies: other symbols this function depends on |
| 90 | + - usages: other symbols that use this function |
| 91 | + """, |
| 92 | + target=function, |
| 93 | + # `codebase.ai` is smart about stringifying symbols |
| 94 | + context={"dependencies": list(dependencies), "usages": list(usages)}, |
| 95 | + ) |
| 96 | + |
| 97 | + # Set the generated docstring for the function |
| 98 | + if docstring: |
| 99 | + function.set_docstring(docstring) |
| 100 | + print(" ✓ Generated docstring") |
| 101 | + else: |
| 102 | + print(" ✗ Failed to generate docstring") |
| 103 | + |
| 104 | + # Commit after each function so work is saved incrementally |
| 105 | + # This allows for: |
| 106 | + # 1. Safe early termination - progress won't be lost |
| 107 | + # 2. Immediate feedback - can check results while running |
| 108 | + # 3. Smaller atomic changes - easier to review/revert if needed |
| 109 | + codebase.commit() |
| 110 | + |
| 111 | + print(f"\nCompleted processing {total_functions} functions") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + print("Parsing codebase...") |
| 116 | + codebase = Codebase.from_repo("fastapi/fastapi", commit="887270ff8a54bb58c406b0651678a27589793d2f") |
| 117 | + |
| 118 | + print("Running function...") |
| 119 | + run(codebase) |
0 commit comments