Commit ·
f18f414
1
Parent(s): 285964f
Harden generated code cleanup
Browse filesCo-authored-by: Codex <codex@openai.com>
- app.py +28 -6
- static/engine.js +42 -5
app.py
CHANGED
|
@@ -274,12 +274,34 @@ def code_from_verdict(draft_code: str, verdict_json: str) -> str:
|
|
| 274 |
|
| 275 |
def strip_markdown_code_fence(code: str) -> str:
|
| 276 |
text = (code or "").strip()
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
|
| 285 |
async def run_sandbox(language: str, draft_code: str, verdict_json: str) -> str:
|
|
|
|
| 274 |
|
| 275 |
def strip_markdown_code_fence(code: str) -> str:
|
| 276 |
text = (code or "").strip()
|
| 277 |
+
if not text:
|
| 278 |
+
return ""
|
| 279 |
+
|
| 280 |
+
opening_fence = re.match(r"^```(?:[a-zA-Z0-9_+#.-]+)?\s*\n?", text)
|
| 281 |
+
if opening_fence:
|
| 282 |
+
text = text[opening_fence.end() :]
|
| 283 |
+
closing_index = text.find("```")
|
| 284 |
+
if closing_index >= 0:
|
| 285 |
+
text = text[:closing_index]
|
| 286 |
+
else:
|
| 287 |
+
first_fence = text.find("```")
|
| 288 |
+
if first_fence >= 0:
|
| 289 |
+
text = text[:first_fence]
|
| 290 |
+
|
| 291 |
+
return trim_markdown_explanation(text)
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
def trim_markdown_explanation(text: str) -> str:
|
| 295 |
+
explanation = re.compile(
|
| 296 |
+
r"^\s*(?:[-*]\s+|\d+\.\s+|#{1,6}\s+|Explanation\s*:|Steps\s*:|Notes?\s*:|The code\b|This code\b)",
|
| 297 |
+
re.IGNORECASE,
|
| 298 |
+
)
|
| 299 |
+
kept = []
|
| 300 |
+
for line in text.splitlines():
|
| 301 |
+
if explanation.match(line):
|
| 302 |
+
break
|
| 303 |
+
kept.append(line)
|
| 304 |
+
return "\n".join(kept).strip()
|
| 305 |
|
| 306 |
|
| 307 |
async def run_sandbox(language: str, draft_code: str, verdict_json: str) -> str:
|
static/engine.js
CHANGED
|
@@ -37,9 +37,19 @@ export async function generateCode(prompt, language, onToken, onComplete) {
|
|
| 37 |
const messages = [
|
| 38 |
{
|
| 39 |
role: "system",
|
| 40 |
-
content:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
},
|
| 42 |
-
{ role: "user", content: prompt },
|
| 43 |
];
|
| 44 |
|
| 45 |
const streamer = new TextStreamer(generator.tokenizer, {
|
|
@@ -84,9 +94,36 @@ function formatError(error) {
|
|
| 84 |
|
| 85 |
export function stripMarkdownCodeFence(text) {
|
| 86 |
const trimmed = String(text || "").trim();
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
Object.assign(window, {
|
|
|
|
| 37 |
const messages = [
|
| 38 |
{
|
| 39 |
role: "system",
|
| 40 |
+
content: [
|
| 41 |
+
`You are an expert ${language} programmer.`,
|
| 42 |
+
"Return raw source code only.",
|
| 43 |
+
"Do not use markdown fences.",
|
| 44 |
+
"Do not add explanations, bullet points, headings, comments, or usage notes.",
|
| 45 |
+
"Do not wrap the answer in ```.",
|
| 46 |
+
"The response must be directly executable or pasteable as a source file.",
|
| 47 |
+
].join(" "),
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
role: "user",
|
| 51 |
+
content: `${prompt}\n\nReturn only the ${language} code. No markdown. No comments. No explanation.`,
|
| 52 |
},
|
|
|
|
| 53 |
];
|
| 54 |
|
| 55 |
const streamer = new TextStreamer(generator.tokenizer, {
|
|
|
|
| 94 |
|
| 95 |
export function stripMarkdownCodeFence(text) {
|
| 96 |
const trimmed = String(text || "").trim();
|
| 97 |
+
if (!trimmed) return "";
|
| 98 |
+
|
| 99 |
+
let code = trimmed;
|
| 100 |
+
const openingFence = code.match(/^```(?:[a-zA-Z0-9_+#.-]+)?\s*\n?/);
|
| 101 |
+
if (openingFence) {
|
| 102 |
+
code = code.slice(openingFence[0].length);
|
| 103 |
+
const closingIndex = code.indexOf("```");
|
| 104 |
+
if (closingIndex >= 0) code = code.slice(0, closingIndex);
|
| 105 |
+
} else {
|
| 106 |
+
const firstFence = code.indexOf("```");
|
| 107 |
+
if (firstFence >= 0) code = code.slice(0, firstFence);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
return trimMarkdownExplanation(code);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
function trimMarkdownExplanation(text) {
|
| 114 |
+
const lines = String(text || "").split(/\r?\n/);
|
| 115 |
+
const explanationPattern =
|
| 116 |
+
/^\s*(?:[-*]\s+|\d+\.\s+|#{1,6}\s+|Explanation\s*:|Steps\s*:|Notes?\s*:|The code\b|This code\b)/i;
|
| 117 |
+
|
| 118 |
+
let cutIndex = lines.length;
|
| 119 |
+
for (let i = 0; i < lines.length; i += 1) {
|
| 120 |
+
if (explanationPattern.test(lines[i])) {
|
| 121 |
+
cutIndex = i;
|
| 122 |
+
break;
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
return lines.slice(0, cutIndex).join("\n").trim();
|
| 127 |
}
|
| 128 |
|
| 129 |
Object.assign(window, {
|