File size: 4,614 Bytes
2adb7d8
 
 
d2e9fbe
2adb7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2e9fbe
 
 
 
2adb7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import ast
import os
import logging
from app.services.llm import get_completion

logger = logging.getLogger(__name__)


# ----------------------------
# Generate docstring for a single chunk
# ----------------------------
def generate_docstring(chunk: dict) -> str:
    """
    Sends a code chunk to Groq and returns a generated docstring.
    """
    prompt = f"""You are an expert Python developer. Write a concise docstring for the following function or class.

Rules:
- One to three sentences maximum
- Describe what it does, not how
- Do not include parameter descriptions unless critical
- Return ONLY the docstring text, no quotes, no def line, no extra explanation

CODE:
{chunk.get('code')}

Docstring:"""

    return get_completion([
        {"role": "system", "content": "You are an expert Python developer who writes clean, concise docstrings."},
        {"role": "user", "content": prompt}
    ], max_tokens=150).strip()


# ----------------------------
# Inject docstring into source file
# ----------------------------
def inject_docstring(file_content: str, chunk: dict, docstring: str) -> str:
    """
    Injects a generated docstring into the source file content.
    Returns updated file content.
    """
    try:
        tree = ast.parse(file_content)
        lines = file_content.splitlines()

        for node in ast.walk(tree):
            if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
                continue

            if node.name != chunk.get("name"):
                continue

            if node.lineno != chunk.get("start_line"):
                continue

            # Check if docstring already exists
            if (node.body and
                isinstance(node.body[0], ast.Expr) and
                isinstance(node.body[0].value, ast.Constant)):
                logger.debug(f"Docstring already exists for {node.name}, skipping")
                return file_content

            # Find the line after def/class signature
            # Handle multiline signatures (def func(\n  arg1,\n  arg2\n):)
            insert_after_line = node.body[0].lineno - 1  # 0-indexed

            # Build indentation from first body line
            first_body_line = lines[insert_after_line]
            indent = len(first_body_line) - len(first_body_line.lstrip())
            indent_str = " " * indent

            # Format docstring
            docstring_lines = [f'{indent_str}"""']
            for line in docstring.splitlines():
                docstring_lines.append(f"{indent_str}{line}")
            docstring_lines.append(f'{indent_str}"""')

            # Inject into lines
            lines = (
                lines[:insert_after_line] +
                docstring_lines +
                lines[insert_after_line:]
            )

            return "\n".join(lines)

    except Exception as e:
        logger.error(f"Failed to inject docstring for {chunk.get('name')}: {e}")

    return file_content


# ----------------------------
# Process entire file
# ----------------------------
def process_file(file_path: str, chunks: list[dict]) -> dict:
    """
    Generates and injects docstrings for all chunks in a file.
    Returns stats about what was updated.
    """
    try:
        with open(file_path, "r", encoding="utf-8") as f:
            content = f.read()
    except Exception as e:
        return {"file": file_path, "error": str(e), "updated": 0}

    updated = 0
    skipped = 0

    # Sort chunks by start_line in reverse so injections don't shift line numbers
    sorted_chunks = sorted(chunks, key=lambda c: c.get("start_line", 0), reverse=True)

    for chunk in sorted_chunks:
        # Skip if docstring already exists
        if chunk.get("docstring"):
            logger.debug(f"Skipping {chunk['name']} — docstring exists")
            skipped += 1
            continue

        try:
            docstring = generate_docstring(chunk)
            content = inject_docstring(content, chunk, docstring)
            updated += 1
            logger.info(f"Generated docstring for {chunk['name']} in {file_path}")
        except Exception as e:
            logger.warning(f"Failed to generate docstring for {chunk['name']}: {e}")
            skipped += 1

    # Write updated content back to file
    if updated > 0:
        try:
            with open(file_path, "w", encoding="utf-8") as f:
                f.write(content)
        except Exception as e:
            return {"file": file_path, "error": str(e), "updated": 0}

    return {
        "file": file_path,
        "updated": updated,
        "skipped": skipped
    }