Skip to content

Commit

Permalink
🔧 Refactor DIFF format and add credit card gen tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 15, 2024
1 parent 33e7db4 commit 3d857d9
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 28 deletions.
39 changes: 31 additions & 8 deletions docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -588,34 +588,44 @@ system({
lineNumbers: true,
})

$`The DIFF format should be used to generate diff changes on files:
$`## DIFF file format
- added lines MUST start with +
- deleted lines MUST start with -
The DIFF format should be used to generate diff changes on large files:
- existing lines must start with their original line number: [<line number>] <line>
- deleted lines MUST start with - followed by the line number: - [<line number>] <deleted line>
- added lines MUST start with +, no line number: + <added line>
- deleted lines MUST exist in the original file (do not invent deleted lines)
- added lines MUST not exist in the original file
### Guidance:
- each line in the source starts with a line number: [line] <line>
- preserve indentation
- use relative file path name
- emit original line numbers from existing lines and deleted lines
- only generate diff for files that have changes
- only emit a couple unmodified lines before and after the changes
- keep the diffs AS SMALL AS POSSIBLE
- when reading files, ask for line numbers
- minimize the number of unmodified lines
- do NOT generate diff for files that have no changes
- do NOT emit diff if lines are the same
- do NOT emit the whole file content
- do NOT emit line numbers for added lines
- do NOT use <, > or --- in the diff syntax
- Use one DIFF section per change.
### Examples:
FOLLOW THE SYNTAX PRECISLY. THIS IS IMPORTANT.
DIFF ./file.ts:
\`\`\`diff
[original line number] <2 lines before changes (not the whole file)>
- [original line number] <deleted line>
- [original line number] <delete line 2>
+ <added line>
+ <added line 2>
[original line number] <2 lines after changes (not the whole file)>
\`\`\`
Expand All @@ -628,9 +638,22 @@ DIFF ./file2.ts:
+ <added line 2>
[original line number] <2 lines after changes (not the whole file)>
\`\`\`
`
$`Do not generate anything else than DIFF sections. Use one DIFF section per change.`
DIFF ./file3.ts:
\`\`\`diff
[original line number] <2 lines before changes (not the whole file)>
+ <added line>
[original line number] <2 lines after changes (not the whole file)>
\`\`\`
DIFF ./file4.ts:
\`\`\`diff
[original line number] <2 lines before changes (not the whole file)>
- [original line number] <deleted line>
[original line number] <2 lines after changes (not the whole file)>
\`\`\`
`

`````

Expand Down
39 changes: 31 additions & 8 deletions packages/core/src/genaisrc/system.diff.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,44 @@ system({
lineNumbers: true,
})

$`The DIFF format should be used to generate diff changes on files:
$`## DIFF file format
- added lines MUST start with +
- deleted lines MUST start with -
The DIFF format should be used to generate diff changes on large files:
- existing lines must start with their original line number: [<line number>] <line>
- deleted lines MUST start with - followed by the line number: - [<line number>] <deleted line>
- added lines MUST start with +, no line number: + <added line>
- deleted lines MUST exist in the original file (do not invent deleted lines)
- added lines MUST not exist in the original file
### Guidance:
- each line in the source starts with a line number: [line] <line>
- preserve indentation
- use relative file path name
- emit original line numbers from existing lines and deleted lines
- only generate diff for files that have changes
- only emit a couple unmodified lines before and after the changes
- keep the diffs AS SMALL AS POSSIBLE
- when reading files, ask for line numbers
- minimize the number of unmodified lines
- do NOT generate diff for files that have no changes
- do NOT emit diff if lines are the same
- do NOT emit the whole file content
- do NOT emit line numbers for added lines
- do NOT use <, > or --- in the diff syntax
- Use one DIFF section per change.
### Examples:
FOLLOW THE SYNTAX PRECISLY. THIS IS IMPORTANT.
DIFF ./file.ts:
\`\`\`diff
[original line number] <2 lines before changes (not the whole file)>
- [original line number] <deleted line>
- [original line number] <delete line 2>
+ <added line>
+ <added line 2>
[original line number] <2 lines after changes (not the whole file)>
\`\`\`
Expand All @@ -43,6 +53,19 @@ DIFF ./file2.ts:
+ <added line 2>
[original line number] <2 lines after changes (not the whole file)>
\`\`\`
`
$`Do not generate anything else than DIFF sections. Use one DIFF section per change.`
DIFF ./file3.ts:
\`\`\`diff
[original line number] <2 lines before changes (not the whole file)>
+ <added line>
[original line number] <2 lines after changes (not the whole file)>
\`\`\`
DIFF ./file4.ts:
\`\`\`diff
[original line number] <2 lines before changes (not the whole file)>
- [original line number] <deleted line>
[original line number] <2 lines after changes (not the whole file)>
\`\`\`
`
6 changes: 6 additions & 0 deletions packages/core/src/promptrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ export async function runTemplate(
// Reporting and tracing output
if (fences?.length)
trace.details("📩 code regions", renderFencedVariables(fences))
if (fileEdits && Object.keys(fileEdits).length) {
trace.startDetails("📝 file edits")
for (const [f, e] of Object.entries(fileEdits))
trace.detailsFenced(f, e.after)
trace.endDetails()
}
if (annotations?.length)
trace.details(
"⚠️ annotations",
Expand Down
59 changes: 59 additions & 0 deletions packages/sample/src/edits/bigfibs/fib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Function to generate a credit card number
function generateCreditCardNumber() {
// Define the prefix for Visa cards
const prefix = "400000"
// Define the total length of the credit card number
const length = 16
// Initialize the card number with the prefix
let cardNumber = prefix

// Loop until the card number reaches the desired length minus the check digit
while (cardNumber.length < length - 1) {
// Append a random digit to the card number
cardNumber += Math.floor(Math.random() * 10).toString()
}

// Append the check digit to the card number
cardNumber += getCheckDigit(cardNumber)
// Return the complete card number
return cardNumber
}

// Function to calculate the check digit using the Luhn algorithm
function getCheckDigit(cardNumber) {
// Initialize the sum to 0
let sum = 0
// Flag to determine whether to double the digit or not
let shouldDouble = true

// Loop through the card number digits from right to left
for (let i = cardNumber.length - 1; i >= 0; i--) {
// Parse the current digit
let digit = parseInt(cardNumber.charAt(i))

// If the flag is set, double the digit
if (shouldDouble) {
digit *= 2
// If the doubled digit is greater than 9, subtract 9
if (digit > 9) {
digit -= 9
}
}

// Add the digit to the sum
sum += digit
// Toggle the flag for the next digit
shouldDouble = !shouldDouble
}

// Calculate the check digit
const checkDigit = newFunction(sum)
// Return the check digit as a string
return checkDigit.toString()
}

function newFunction(sum) {
// TODO
return 0
// return (10 - (sum % 10)) % 10;
}
51 changes: 51 additions & 0 deletions packages/sample/src/edits/bigfibs/fib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Function to generate a credit card number
def generate_credit_card_number():
# Define the prefix for Visa cards
prefix = "400000"
# Define the total length of the credit card number
length = 16
# Initialize the card number with the prefix
card_number = prefix

# Loop until the card number reaches the desired length minus the check digit
while len(card_number) < length - 1:
# Append a random digit to the card number
card_number += str(random.randint(0, 9))

# Append the check digit to the card number
card_number += get_check_digit(card_number)
# Return the complete card number
return card_number

# Function to calculate the check digit using the Luhn algorithm
def get_check_digit(card_number):
# Initialize the sum to 0
sum = 0
# Flag to determine whether to double the digit or not
should_double = True

# Loop through the card number digits from right to left
for digit in reversed(card_number):
digit = int(digit)

# If the flag is set, double the digit
if should_double:
digit *= 2
# If the doubled digit is greater than 9, subtract 9
if digit > 9:
digit -= 9

# Add the digit to the sum
sum += digit
# Toggle the flag for the next digit
should_double = not should_double

# Calculate the check digit
check_digit = new_function(sum)
# Return the check digit as a string
return str(check_digit)

def new_function(sum):
# TODO
return 0
# return (10 - (sum % 10)) % 10;
59 changes: 59 additions & 0 deletions packages/sample/src/edits/bigfibs/fib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Function to generate a credit card number
function generateCreditCardNumber(): string {
// Define the prefix for Visa cards
const prefix = "400000"
// Define the total length of the credit card number
const length = 16
// Initialize the card number with the prefix
let cardNumber = prefix

// Loop until the card number reaches the desired length minus the check digit
while (cardNumber.length < length - 1) {
// Append a random digit to the card number
cardNumber += Math.floor(Math.random() * 10).toString()
}

// Append the check digit to the card number
cardNumber += getCheckDigit(cardNumber)
// Return the complete card number
return cardNumber
}

// Function to calculate the check digit using the Luhn algorithm
function getCheckDigit(cardNumber: string): string {
// Initialize the sum to 0
let sum = 0
// Flag to determine whether to double the digit or not
let shouldDouble = true

// Loop through the card number digits from right to left
for (let i = cardNumber.length - 1; i >= 0; i--) {
// Parse the current digit
let digit = parseInt(cardNumber.charAt(i))

// If the flag is set, double the digit
if (shouldDouble) {
digit *= 2
// If the doubled digit is greater than 9, subtract 9
if (digit > 9) {
digit -= 9
}
}

// Add the digit to the sum
sum += digit
// Toggle the flag for the next digit
shouldDouble = !shouldDouble
}

// Calculate the check digit
const checkDigit = newFunction(sum)
// Return the check digit as a string
return checkDigit.toString()
}

function newFunction(sum: number) {
// TODO
return 0
// return (10 - (sum % 10)) % 10;
}
11 changes: 8 additions & 3 deletions packages/sample/src/edits/edits_diff.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ script({
title: "system.diff test",
files: "src/edits/fibs/fib.*",
system: ["system", "system.diff"],
tests: {
files: "src/edits/fibs/fib.*",
},
tests: [
{
files: "src/edits/fibs/fib.*",
},
{
files: "src/edits/bigfibs/fib.*",
},
],
})

editTest()
11 changes: 8 additions & 3 deletions packages/sample/src/edits/edits_files.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ script({
title: "system.files test",
files: "src/edits/fibs/fib.*",
system: ["system", "system.files"],
tests: {
files: "src/edits/fibs/fib.*",
},
tests: [
{
files: "src/edits/fibs/fib.*",
},
{
files: "src/edits/bigfibs/fib.*",
},
],
})
import { editTest } from "./fileedittest.mts"
editTest()
11 changes: 8 additions & 3 deletions packages/sample/src/edits/edits_files_diff.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ script({
title: "system.files test",
files: "src/edits/fibs/fib.*",
system: ["system", "system.files", "system.diff"],
tests: {
files: "src/edits/fibs/fib.*",
},
tests: [
{
files: "src/edits/fibs/fib.*",
},
{
files: "src/edits/bigfibs/fib.*",
},
],
})
import { editTest } from "./fileedittest.mts"
editTest()
2 changes: 1 addition & 1 deletion packages/sample/src/edits/editsgen.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ $`Generation 1 variations of the SNIPPET in various programming languages and sa
- there should be a function with a TODO comment and a BODY comment
`
def("FILE", env.files)
defFileOutput("src/edits/*")
defFileOutput("src/edits/**")
Loading

0 comments on commit 3d857d9

Please sign in to comment.