From dabc64e125a9db7108b2abdedfb4d702b31dc380 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 19:32:23 -0700 Subject: [PATCH] tweak diff algo parameters for fuzzy matching --- core/diff/streamDiff.test.ts | 18 +++++++++-------- core/diff/test-examples/mock-llm.diff | 28 +++++++++++++-------------- core/diff/util.ts | 3 ++- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/core/diff/streamDiff.test.ts b/core/diff/streamDiff.test.ts index 94933623c..8978b92a5 100644 --- a/core/diff/streamDiff.test.ts +++ b/core/diff/streamDiff.test.ts @@ -8,6 +8,12 @@ import path from "node:path"; // "modification" is an extra type used to represent an "old" + "new" diff line type MyersDiffTypes = Extract | "modification"; +const UNIFIED_DIFF_SYMBOLS = { + same: "", + new: "+", + old: "-", +}; + async function* generateLines(lines: string[]): AsyncGenerator { for (const line of lines) { yield line; @@ -50,13 +56,9 @@ function getMyersDiffType(diff: any): MyersDiffTypes | undefined { } function displayDiff(diff: DiffLine[]) { - const symbol = { - same: "", - new: "+", - old: "-", - }; - - return diff.map(({ type, line }) => `${symbol[type]} ${line}`).join("\n"); + return diff + .map(({ type, line }) => `${UNIFIED_DIFF_SYMBOLS[type]} ${line}`) + .join("\n"); } async function expectDiff(file: string) { @@ -237,7 +239,7 @@ describe("streamDiff(", () => { await expectDiff("add-comments"); }); - test.skip("Mock LLM example", async () => { + test("Mock LLM example", async () => { await expectDiff("mock-llm"); }); }); diff --git a/core/diff/test-examples/mock-llm.diff b/core/diff/test-examples/mock-llm.diff index 2bf49c698..42321da5c 100644 --- a/core/diff/test-examples/mock-llm.diff +++ b/core/diff/test-examples/mock-llm.diff @@ -68,24 +68,24 @@ export default Mock; import { BaseLLM } from "../index.js"; class Mock extends BaseLLM { -- static Completion = "Test Completion"; -+ private static _completion: string = "Test Completion"; +- static Completion = "Test Completion"; ++ private static _completion: string = "Test Completion"; static providerName: ModelProvider = "mock"; -+ static get completion(): string { -+ return Mock._completion; -+ } -+ -+ static set completion(value: string) { -+ Mock._completion = value; -+ } -+ ++ static get completion(): string { ++ return Mock._completion; ++ } ++ ++ static set completion(value: string) { ++ Mock._completion = value; ++ } ++ protected async *_streamComplete( prompt: string, options: CompletionOptions, ): AsyncGenerator { -- yield Mock.Completion; -+ yield Mock.completion; +- yield Mock.Completion; ++ yield Mock.completion; } protected async *_streamChat( @@ -94,8 +94,8 @@ export default Mock; ): AsyncGenerator { yield { role: "assistant", -- content: Mock.Completion, -+ content: Mock.completion, +- content: Mock.Completion, ++ content: Mock.completion, }; } } diff --git a/core/diff/util.ts b/core/diff/util.ts index b3a895da9..0e24ee373 100644 --- a/core/diff/util.ts +++ b/core/diff/util.ts @@ -31,7 +31,8 @@ function linesMatch(lineA: string, lineB: string, linesBetween = 0): boolean { return ( // Should be more unlikely for lines to fuzzy match if they are further away - (d / Math.max(lineA.length, lineB.length) < 0.5 - linesBetween * 0.05 || + (d / Math.max(lineA.length, lineB.length) <= + Math.max(0, 0.48 - linesBetween * 0.06) || lineA.trim() === lineB.trim()) && lineA.trim() !== "" );