Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defDiff #733

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@

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/, "")

Check failure on line 306 in packages/core/src/diff.ts

View workflow job for this annotation

GitHub Actions / build

There is no error handling for the `createTwoFilesPatch` function. It's good practice to handle potential errors that might occur during the execution of this function. 🛠️
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createDiff function does not handle potential errors that might occur during the creation of the diff. It's recommended to wrap the function body in a try-catch block to handle any exceptions that might occur. 🛠️

generated by pr-review-commit missing_error_handling

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