Skip to content

Commit

Permalink
🔧 Update changelog formats, tests, and poetry lines
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 15, 2024
1 parent a9ad311 commit dc12309
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 20 deletions.
15 changes: 12 additions & 3 deletions docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,12 @@ Generate changelog formatter edits
`````js wrap title="system.changelog"
system({
title: "Generate changelog formatter edits",
lineNumbers: true
lineNumbers: true,
})

$`## CHANGELOG file format
$`For partial updates of files, return one or more ChangeLogs (CLs) formatted as follows. Each CL must contain
For partial updates of large files, return one or more ChangeLogs (CLs) formatted as follows. Each CL must contain
one or more code snippet changes for a single file. There can be multiple CLs for a single file.
Each CL must start with a description of its changes. The CL must then list one or more pairs of
(OriginalCode, ChangedCode) code snippets. In each such pair, OriginalCode must list all consecutive
Expand All @@ -519,7 +520,8 @@ lines of code (again including the same few lines before and after the changes).
OriginalCode and ChangedCode must start at the same source code line number N. Each listed code line,
in both the OriginalCode and ChangedCode snippets, must be prefixed with [N] that matches the line
index N in the above snippets, and then be prefixed with exactly the same whitespace indentation as
the original snippets above. See also the following examples of the expected response format.
the original snippets above. Each OriginalCode must be paired with ChangedCode. Do NOT add multiple ChangedCode per OriginalCode.
See also the following examples of the expected response format.
CHANGELOG:
\`\`\`\`\`changelog
Expand Down Expand Up @@ -553,7 +555,14 @@ OriginalCode@23-23:
ChangedCode@23-23:
[23] <white space> <changed code line>
\`\`\`\`\`
## Choosing what file format to use
- If the file content is small (< 20 lines), use the full FULL format.
- If the file content is large (> 50 lines), use CHANGELOG format.
- If the file content IS VERY LARGE, ALWAYS USE CHANGELOG to save tokens.
`

`````


Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/changelog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,24 @@ ChangedCode@105-107:
console.log(res)
assert.equal(res[0].filename, "src/edits/su/fib.ts")
})

test("unbalancred fences", () => {
const source = `\`\`\`\`\`changelog
ChangeLog:1@src/edits/bigfibs/fib.py
Description: Implemented new_function, removed comments and empty lines.
OriginalCode@48-51:
[48] def new_function(sum):
[49] # TODO
[50] return 0
[51] # return (10 - (sum % 10)) % 10;
ChangedCode@48-50:
[48] def new_function(sum):
[49] return (10 - (sum % 10)) % 10
[50]
\`\`\`
`
const res = parseChangeLogs(source)
console.log(res)
assert.equal(res[0].filename, "src/edits/bigfibs/fib.py")
})
})
28 changes: 23 additions & 5 deletions packages/core/src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* A changelog describes changes between original and modified code segments.
*/

import { unfence } from "./fence"

// Represents a chunk of code with a start and end line and its content.
export interface ChangeLogChunk {
start: number // Starting line number
Expand Down Expand Up @@ -31,19 +33,26 @@ export interface ChangeLog {
* @returns An array of parsed ChangeLog objects.
*/
export function parseChangeLogs(source: string): ChangeLog[] {
const lines = source.split("\n")
const lines = unfence(source, "changelog").split("\n")
const changelogs: ChangeLog[] = []

// Process each line to extract changelog information.
while (lines.length) {
if (!lines[0].trim()) {
lines.shift()
continue // Skip empty lines
continue
}

// each back ticks
if (/^[\`\.]{3,}/.test(lines[0])) {
lines.shift()
continue
}

// Parse the ChangeLog header line.
let m = /^ChangeLog:\s*(?<index>\d+)@(?<file>.*)$/i.exec(lines[0])
if (!m) throw new Error("missing ChangeLog header in " + lines[0])
let m = /^ChangeLog:\s*(?<index>\d+)@(?<file>.*)\s*$/i.exec(lines[0])
if (!m)
throw new Error("missing ChangeLog header in |" + lines[0] + "|")
const changelog: ChangeLog = {
index: parseInt(m.groups.index),
filename: m.groups.file.trim(),
Expand All @@ -66,6 +75,14 @@ export function parseChangeLogs(source: string): ChangeLog[] {
lines.shift()
continue
}

// each back ticks
if (/^[\`\.]{3,}/.test(lines[0])) {
// somehow we have finished this changed
lines.shift()
continue
}

// Attempt to parse a change.
const change = parseChange()
if (change) changelog.changes.push(change)
Expand All @@ -89,7 +106,8 @@ export function parseChangeLogs(source: string): ChangeLog[] {

lines.shift()
const changed = parseChunk(m)
return <ChangeLogChange>{ original, changed }
const res = <ChangeLogChange>{ original, changed }
return res
}

// Parses a chunk of code from the changelog.
Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/genaisrc/system.changelog.genai.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
system({
title: "Generate changelog formatter edits",
lineNumbers: true
lineNumbers: true,
})

$`## CHANGELOG file format
$`For partial updates of files, return one or more ChangeLogs (CLs) formatted as follows. Each CL must contain
For partial updates of large files, return one or more ChangeLogs (CLs) formatted as follows. Each CL must contain
one or more code snippet changes for a single file. There can be multiple CLs for a single file.
Each CL must start with a description of its changes. The CL must then list one or more pairs of
(OriginalCode, ChangedCode) code snippets. In each such pair, OriginalCode must list all consecutive
Expand All @@ -14,7 +15,8 @@ lines of code (again including the same few lines before and after the changes).
OriginalCode and ChangedCode must start at the same source code line number N. Each listed code line,
in both the OriginalCode and ChangedCode snippets, must be prefixed with [N] that matches the line
index N in the above snippets, and then be prefixed with exactly the same whitespace indentation as
the original snippets above. See also the following examples of the expected response format.
the original snippets above. Each OriginalCode must be paired with ChangedCode. Do NOT add multiple ChangedCode per OriginalCode.
See also the following examples of the expected response format.
CHANGELOG:
\`\`\`\`\`changelog
Expand Down Expand Up @@ -48,4 +50,10 @@ OriginalCode@23-23:
ChangedCode@23-23:
[23] <white space> <changed code line>
\`\`\`\`\`
`
## Choosing what file format to use
- If the file content is small (< 20 lines), use the full FULL format.
- If the file content is large (> 50 lines), use CHANGELOG format.
- If the file content IS VERY LARGE, ALWAYS USE CHANGELOG to save tokens.
`
8 changes: 5 additions & 3 deletions packages/core/src/genaisrc/system.files.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ system({
})

const folder = env.vars["outputFolder"] || "."
$`## Files
$`## FULL file format
When generating or updating files you will use the following syntax:`
When generating or updating files you may use the FULL file format:`

def(`File ${folder}/file1.ts`, `What goes in\n${folder}/file1.ts.`, {
language: "typescript",
Expand All @@ -23,7 +23,9 @@ def(`File /path_to_file/file2.md`, `What goes in\n/path_to_file/file2.md.`, {

$`- Make sure to use precisely \`\`\` to guard file code sections.
- Always sure to use precisely \`\`\`\`\` to guard file markdown sections.
- Use full path of filename in code section header.`
- Use full path of filename in code section header.
- do NOT use ## headers with filename
`
if (folder !== ".")
$`When generating new files, place files in folder "${folder}".`
$`- If a file does not have changes, do not regenerate.
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ function renderDefNode(def: PromptDefNode): string {
while (dfence && body.includes(dfence)) {
dfence += "`"
}
const diffFormat = "" // body.length > 500 ? "preferred_diff_format=DIFF" : ""
const diffFormat =
body.length > 500
? " preferred_output_format=CHANGELOG "
: body.length < 200
? " preferred_output_format=FILE "
: ""
const res =
(name ? name + ":\n" : "") +
dfence +
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/systems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function resolveSystems(
if (/\Wchangelog\W/i.test(jsSource)) systems.push("system.changelog")
else if (/\Wfile\W/i.test(jsSource)) {
systems.push("system.files")
// systems.push("system.diff")
systems.push("system.changelog")
// Add file schema system if schema is used
if (useSchema) systems.push("system.files_schema")
}
Expand Down
9 changes: 9 additions & 0 deletions packages/sample/src/edits/edits_files_changelog.genai.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { editTest } from "./fileedittest.mts"
script({
model: "large",
title: "system.diff test",
files: "src/edits/fibs/fib.*",
system: ["system", "system.files", "system.changelog"],
})

editTest()
7 changes: 4 additions & 3 deletions packages/sample/src/edits/fileedittest.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ export function editTest() {
def("FILE", env.files)

$`- Implement the functions with TODO.
- Delete all comments
- Delete all comments in the entire file. This is important.
- Delete all empty lines
- process all files, do NOT skip any
`

defOutputProcessor((output) => {
const { fileEdits } = output
if (Object.keys(fileEdits).length !== env.files.length)
throw new Error("no file edits")
const fns = Object.keys(fileEdits)
if (!fns.length) throw new Error("no file edits")
for (const [fn, fe] of Object.entries(fileEdits)) {
const res = fe.after
if (/^\s*(#|\/\/).*$/m.test(res)) {
Expand Down
Loading

0 comments on commit dc12309

Please sign in to comment.