Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions core/autocomplete/postprocessing/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { postprocessCompletion } from "./index";

describe("postprocessCompletion - removeBackticks", () => {
const mockLLM = { model: "test-model" } as any;

it("should remove first line starting with ``` and last line that is ```", () => {
const completion =
"```typescript\nfunction hello() {\n return 'world';\n}\n```";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe("function hello() {\n return 'world';\n}");
});

it("should remove only first line if it starts with ```", () => {
const completion = "```javascript\nconst x = 5;\nconsole.log(x);";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe("const x = 5;\nconsole.log(x);");
});

it("should remove only last line if it is ```", () => {
const completion = "const y = 10;\nconsole.log(y);\n```";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe("const y = 10;\nconsole.log(y);");
});

it("should not modify completion without backticks", () => {
const completion = "function test() {\n return true;\n}";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe("function test() {\n return true;\n}");
});

it("should handle completion with backticks in the middle", () => {
const completion = "const str = `template ${literal}`;\nconsole.log(str);";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe(
"const str = `template ${literal}`;\nconsole.log(str);",
);
});

it("should handle first line with leading whitespace before ```", () => {
const completion = " ```python\ndef hello():\n pass\n```";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe("def hello():\n pass");
});

it("should handle last line with whitespace around ```", () => {
const completion = "```\ncode here\n ``` ";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe("code here");
});

it("should handle single line completion", () => {
const completion = "const x = 5;";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe("const x = 5;");
});

it("should handle empty completion", () => {
const completion = "";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBeUndefined();
});

it("should not remove ``` if it's not on its own line at the end", () => {
const completion = "```typescript\nconst x = 5; // end```";
const result = postprocessCompletion({
completion,
llm: mockLLM,
prefix: "",
suffix: "",
});
expect(result).toBe("const x = 5; // end```");
});
});
35 changes: 35 additions & 0 deletions core/autocomplete/postprocessing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ function isBlank(completion: string): boolean {
return completion.trim().length === 0;
}

/**
* Removes markdown code block delimiters from completion.
* Removes the first line if it starts with "```" and the last line if it is exactly "```".
*/
function removeBackticks(completion: string): string {
const lines = completion.split("\n");

if (lines.length === 0) {
return completion;
}

let startIdx = 0;
let endIdx = lines.length;

// Remove first line if it starts with ```
if (lines[0].trimStart().startsWith("```")) {
startIdx = 1;
}

// Remove last line if it is exactly ```
if (lines.length > startIdx && lines[lines.length - 1].trim() === "```") {
endIdx = lines.length - 1;
}

// If we removed lines, return the modified completion
if (startIdx > 0 || endIdx < lines.length) {
return lines.slice(startIdx, endIdx).join("\n");
}

return completion;
}

export function postprocessCompletion({
completion,
llm,
Expand Down Expand Up @@ -156,5 +188,8 @@ export function postprocessCompletion({
completion = completion.slice(1);
}

// Remove markdown code block delimiters
completion = removeBackticks(completion);

return completion;
}
Loading