Skip to content

Commit

Permalink
defDiff (#733)
Browse files Browse the repository at this point in the history
* diff package

* Add createDiff function and tests, extend defDiff options in types

* Add createDefDiff function and refactor diff handling across core modules

* Remove unused logDiff console output and extra newline

* Update investigator report formatting for commit and diff links
  • Loading branch information
pelikhan authored Sep 27, 2024
1 parent de9e02d commit a9c2db7
Show file tree
Hide file tree
Showing 27 changed files with 629 additions and 144 deletions.
35 changes: 29 additions & 6 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 29 additions & 6 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 29 additions & 6 deletions packages/auto/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"promptfoo": "0.89.3"
},
"devDependencies": {
"@types/diff": "^5.2.2",
"@types/dockerode": "^3.3.31",
"@types/fs-extra": "^11.0.4",
"@types/memorystream": "^0.3.4",
Expand All @@ -65,6 +66,7 @@
"@types/replace-ext": "^2.0.2",
"@types/ws": "^8.5.12",
"commander": "^12.1.0",
"diff": "^7.0.0",
"dotenv": "^16.4.5",
"esbuild": "^0.24.0",
"execa": "^9.4.0",
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@azure/identity": "^4.4.1",
"@huggingface/jinja": "^0.3.1",
"@tidyjs/tidy": "^2.5.2",
"@types/diff": "^5.2.2",
"@types/html-escaper": "^3.0.2",
"@types/html-to-text": "^9.0.4",
"@types/inflection": "^1.13.2",
Expand Down
32 changes: 31 additions & 1 deletion packages/core/src/diff.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, test } from "node:test"
import assert from "node:assert/strict"
import { parseLLMDiffs } from "./diff"
import { createDiff, parseLLMDiffs } from "./diff"

describe("diff", () => {
test("is_valid_email", () => {
Expand Down Expand Up @@ -287,3 +287,33 @@ describe("diff", () => {
assert.notEqual(chunks.length, 0)
})
})
test("createDiff with context", () => {
const left = {
filename: "file1.txt",
content: "line1\nline2\nline3\nline4\nline5\n",
}
const right = {
filename: "file1.txt",
content: "line1\nline2\nline3\nline4 modified\nline5\n",
}
const diff = createDiff(left, right, { context: 2 })
assert(diff.includes("@@ -2,4 +2,4 @@"))
assert(diff.includes("-line4"))
assert(diff.includes("+line4 modified"))
})

test("createDiff without context", () => {
const left = {
filename: "file1.txt",
content: "line1\nline2\nline3\nline4\nline5\n",
}
const right = {
filename: "file1.txt",
content: "line1\nline2\nline3\nline4 modified\nline5\n",
}
const diff = createDiff(left, right)
console.log(diff)
assert(diff.includes("@@ -1,5 +1,5 @@"))
assert(diff.includes("-line4"))
assert(diff.includes("+line4 modified"))
})
22 changes: 22 additions & 0 deletions packages/core/src/diff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assert } from "./util"
import parseDiff from "parse-diff"
import { createTwoFilesPatch } from "diff"

export interface Chunk {
state: "existing" | "deleted" | "added"
Expand Down Expand Up @@ -283,3 +284,24 @@ export function llmifyDiff(diff: string) {

return result
}

export function createDiff(
left: WorkspaceFile,
right: WorkspaceFile,
options?: { context?: number }
) {
const res = createTwoFilesPatch(
left.filename,
right.filename,
left.content,
right.content,
undefined,
undefined,
{
ignoreCase: true,
ignoreWhitespace: true,
...(options ?? {}),
}
)
return res.replace(/^[^=]*={10,}\n/, "")
}
35 changes: 29 additions & 6 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a9c2db7

Please sign in to comment.