File size: 10,293 Bytes
051e238 8c2efe9 051e238 8c2efe9 051e238 8c2efe9 09cf9a0 db5e74c 8c2efe9 051e238 09cf9a0 051e238 8c2efe9 051e238 09cf9a0 051e238 09cf9a0 051e238 8c2efe9 09cf9a0 8c2efe9 db5e74c 051e238 09cf9a0 051e238 db5e74c 86be2ba db5e74c 8c2efe9 051e238 09cf9a0 051e238 09cf9a0 8c2efe9 db5e74c 8c2efe9 051e238 09cf9a0 051e238 | 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | from typing import List, Optional
from schemas import CodeTaskType, RetrievedEvidence
def clean_text(value: Optional[str], fallback: str = "None") -> str:
if value is None:
return fallback
text = str(value).strip()
return text if text else fallback
def format_evidence(evidence_list: Optional[List[RetrievedEvidence]]) -> str:
if not evidence_list:
return "No external technical evidence provided."
lines = []
for index, item in enumerate(evidence_list, start=1):
lines.append(f"Source {index}")
lines.append(f"Type: {item.source_type}")
lines.append(f"Title: {item.title}")
lines.append(f"Snippet: {item.snippet}")
if item.url:
lines.append(f"URL: {item.url}")
if item.score is not None:
lines.append(f"Score: {item.score}")
lines.append("")
return "\n".join(lines).strip()
def build_shared_context(
message: str,
language: Optional[str] = None,
framework: Optional[str] = None,
code: Optional[str] = None,
error_message: Optional[str] = None,
previous_context: Optional[str] = None,
evidence_list: Optional[List[RetrievedEvidence]] = None,
) -> str:
parts = [
f"User Request:\n{clean_text(message, '')}",
f"Language:\n{clean_text(language, 'Not specified')}",
f"Framework:\n{clean_text(framework, 'Not specified')}",
f"Previous Context:\n{clean_text(previous_context, 'None')}",
f"Error Message:\n{clean_text(error_message, 'None')}",
f"Code:\n{clean_text(code, 'No code provided')}",
f"Technical Evidence:\n{format_evidence(evidence_list)}",
]
return "\n\n".join(parts)
def build_generate_prompt(
message: str,
language: Optional[str] = None,
framework: Optional[str] = None,
previous_context: Optional[str] = None,
) -> str:
shared = build_shared_context(
message=message,
language=language,
framework=framework,
previous_context=previous_context,
)
return (
"You are a senior software engineer.\n"
"Your task is to generate code based on the user's request.\n"
"Follow these rules:\n"
"1. Generate clean, correct, production-style code.\n"
"2. Match the requested language and framework if provided.\n"
"3. If the language or framework is missing, infer a reasonable default from the request.\n"
"4. Keep the explanation concise and practical.\n"
"5. Return code that is directly usable.\n"
"6. Do not include unnecessary long prose.\n"
"7. Do not include multiple alternative implementations unless the user asks.\n"
"8. Do not add unrelated features or speculative improvements.\n\n"
f"{shared}\n\n"
"Output Format:\n"
"Explanation:\n"
"<one short explanation>\n\n"
"Code:\n"
"<generated code>"
)
def build_fix_prompt(
message: str,
code: Optional[str] = None,
error_message: Optional[str] = None,
language: Optional[str] = None,
framework: Optional[str] = None,
previous_context: Optional[str] = None,
evidence_list: Optional[List[RetrievedEvidence]] = None,
) -> str:
shared = build_shared_context(
message=message,
language=language,
framework=framework,
code=code,
error_message=error_message,
previous_context=previous_context,
evidence_list=evidence_list,
)
return (
"You are a senior software engineer and debugging expert.\n"
"Your task is to fix the user's code.\n"
"Follow these rules:\n"
"1. Identify the most likely root cause.\n"
"2. Preserve the user's intended logic where possible.\n"
"3. Use the provided technical evidence if relevant.\n"
"4. If the error details are incomplete, make the safest reasonable fix and mention assumptions.\n"
"5. Make the minimum necessary changes to solve the issue.\n"
"6. Do not change variable names, function names, class names, signatures, public interfaces, or structure unless absolutely necessary.\n"
"7. Do not rewrite unrelated parts of the code.\n"
"8. Return corrected code that is directly usable.\n"
"9. Keep the explanation concise and practical.\n\n"
f"{shared}\n\n"
"Output Format:\n"
"Root Cause:\n"
"<one short root cause>\n\n"
"Explanation:\n"
"<clear but concise explanation>\n\n"
"Code:\n"
"<fixed code>"
)
def build_explain_prompt(
message: str,
code: Optional[str] = None,
language: Optional[str] = None,
framework: Optional[str] = None,
previous_context: Optional[str] = None,
) -> str:
shared = build_shared_context(
message=message,
language=language,
framework=framework,
code=code,
previous_context=previous_context,
)
return (
"You are a senior software engineer.\n"
"Your task is to explain the user's code or technical question clearly.\n"
"Follow these rules:\n"
"1. Explain in a simple and structured way.\n"
"2. Focus on the most important logic.\n"
"3. If code is provided, explain what it does, why it works, and any important edge cases.\n"
"4. Keep the explanation practical and concise.\n"
"5. Do not output changed code unless the user explicitly asks for changes.\n"
"6. Do not rewrite the code.\n\n"
f"{shared}\n\n"
"Output Format:\n"
"Explanation:\n"
"<clear structured explanation>"
)
def build_refactor_prompt(
message: str,
code: Optional[str] = None,
language: Optional[str] = None,
framework: Optional[str] = None,
previous_context: Optional[str] = None,
) -> str:
shared = build_shared_context(
message=message,
language=language,
framework=framework,
code=code,
previous_context=previous_context,
)
return (
"You are a senior software engineer.\n"
"Your task is to refactor the user's code.\n"
"Follow these rules:\n"
"1. Preserve the original behavior unless the user explicitly asked for functional changes.\n"
"2. Preserve all existing function names, class names, method names, parameters, argument order, return behavior, and public interfaces.\n"
"3. You must keep the original callable names exactly the same.\n"
"4. Do not rename variables, parameters, functions, classes, or methods unless absolutely necessary.\n"
"5. Improve readability, maintainability, and structure only.\n"
"6. If the code is already simple and acceptable, make only minimal improvements.\n"
"7. Do not rewrite unrelated sections.\n"
"8. Return improved code that remains easy to compare with the original.\n\n"
f"{shared}\n\n"
"Output Format:\n"
"Explanation:\n"
"<short explanation>\n\n"
"Code:\n"
"<refactored code>"
)
def build_review_prompt(
message: str,
code: Optional[str] = None,
language: Optional[str] = None,
framework: Optional[str] = None,
previous_context: Optional[str] = None,
) -> str:
shared = build_shared_context(
message=message,
language=language,
framework=framework,
code=code,
previous_context=previous_context,
)
return (
"You are a senior software engineer performing a code review.\n"
"Your task is to review the user's code and identify issues or improvements.\n"
"Follow these rules:\n"
"1. Focus on correctness, readability, maintainability, edge cases, and possible bugs.\n"
"2. Be specific and practical.\n"
"3. Prefer review comments and suggestions over rewriting the code.\n"
'4. Only provide improved code if a small correction is truly necessary and helpful.\n'
'5. If no rewrite is necessary, do not include a "Code" section.\n'
"6. Keep the output review-oriented, not refactor-oriented.\n"
"7. Mention important risks and concrete suggestions.\n\n"
f"{shared}\n\n"
"Output Format:\n"
"Review:\n"
"<review points>\n\n"
"Suggestions:\n"
"<practical suggestions>\n\n"
"Code:\n"
"<optional improved code only if needed>"
)
def build_prompt(
task_type: CodeTaskType,
message: str,
code: Optional[str] = None,
error_message: Optional[str] = None,
language: Optional[str] = None,
framework: Optional[str] = None,
previous_context: Optional[str] = None,
evidence_list: Optional[List[RetrievedEvidence]] = None,
) -> str:
if task_type == CodeTaskType.GENERATE:
return build_generate_prompt(
message=message,
language=language,
framework=framework,
previous_context=previous_context,
)
if task_type == CodeTaskType.FIX:
return build_fix_prompt(
message=message,
code=code,
error_message=error_message,
language=language,
framework=framework,
previous_context=previous_context,
evidence_list=evidence_list,
)
if task_type == CodeTaskType.EXPLAIN:
return build_explain_prompt(
message=message,
code=code,
language=language,
framework=framework,
previous_context=previous_context,
)
if task_type == CodeTaskType.REFACTOR:
return build_refactor_prompt(
message=message,
code=code,
language=language,
framework=framework,
previous_context=previous_context,
)
if task_type == CodeTaskType.REVIEW:
return build_review_prompt(
message=message,
code=code,
language=language,
framework=framework,
previous_context=previous_context,
)
return build_explain_prompt(
message=message,
code=code,
language=language,
framework=framework,
previous_context=previous_context,
) |