Skip to content

Commit

Permalink
✨ refactor: update file edits logic and fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 15, 2024
1 parent 738dfda commit 2aa65ba
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
3 changes: 1 addition & 2 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ export async function runScript(
if (isJSONLFilename(outData)) await appendJSONL(outData, result.frames)
else await writeText(outData, JSON.stringify(result.frames, null, 2))

if (result.status === "success" && result.fileEdits && applyEdits)
await writeFileEdits(result.fileEdits, { trace })
await writeFileEdits(result.fileEdits, { applyEdits, trace })

Check failure on line 370 in packages/cli/src/run.ts

View workflow job for this annotation

GitHub Actions / build

The `result.fileEdits` and `applyEdits` are not checked before calling `writeFileEdits`. This could lead to unexpected behavior if `result.fileEdits` is undefined or `applyEdits` is false.

const promptjson = result.messages?.length
? JSON.stringify(result.messages, null, 2)
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/fileedits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,23 @@ function validateFileOutputs(
*/
export async function writeFileEdits(
fileEdits: Record<string, FileUpdate>, // Contains the edits to be applied to files
options?: TraceOptions
options?: { applyEdits?: boolean } & TraceOptions
) {
const { trace } = options || {}
const { applyEdits, trace } = options || {}
// Iterate over each file edit entry
for (const fileEdit of Object.entries(fileEdits || {})) {
// Destructure the filename, before content, after content, and validation from the entry
const [fn, { before, after, validation }] = fileEdit

if (!applyEdits && !validation?.valid) {

Check failure on line 293 in packages/core/src/fileedits.ts

View workflow job for this annotation

GitHub Actions / build

The condition `!applyEdits && !validation?.valid` will never be true because if `applyEdits` is false, the function will not be called due to the missing check in `run.ts`. This could lead to unexpected behavior.
// path not validated
continue
}

// Skip writing if the edit is invalid and applyEdits is false
if (validation?.valid === false) {
if (validation?.error) {
trace.detailsFenced(
`skipping ${fn}, invalid`,
`skipping ${fn}, invalid schema`,
validation.error,
"text"
)
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,7 @@ export function createChatGenerationContext(
)
)
tracePromptResult(runTrace, resp)
const { fileEdits } = resp
if (fileEdits?.length && applyEdits)
await writeFileEdits(fileEdits, { trace })
await writeFileEdits(resp.fileEdits, { applyEdits, trace })
return resp
} catch (e) {
runTrace.error(e)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export function validateJSONWithSchema(
object: any,
schema: JSONSchema,
options?: { trace: MarkdownTrace }
): JSONSchemaValidation {
): FileEditValidation {
const { trace } = options || {}
if (!schema)
return {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ interface JSONSchemaArray {

type JSONSchema = JSONSchemaObject | JSONSchemaArray

interface JSONSchemaValidation {
interface FileEditValidation {
schema?: JSONSchema
valid: boolean
error?: string
Expand All @@ -941,7 +941,7 @@ interface JSONSchemaValidation {
interface DataFrame {
schema?: string
data: unknown
validation?: JSONSchemaValidation
validation?: FileEditValidation
}

interface RunPromptResult {
Expand Down Expand Up @@ -1023,7 +1023,7 @@ interface Fenced {
content: string
args?: { schema?: string } & Record<string, string>

validation?: JSONSchemaValidation
validation?: FileEditValidation
}

interface XMLParseOptions {
Expand Down Expand Up @@ -1257,7 +1257,7 @@ interface Parsers {
* @param schema JSON schema instance
* @param content object to validate
*/
validateJSON(schema: JSONSchema, content: any): JSONSchemaValidation
validateJSON(schema: JSONSchema, content: any): FileEditValidation

/**
* Renders a mustache template
Expand Down Expand Up @@ -2107,7 +2107,7 @@ interface ChatTurnGenerationContext {
interface FileUpdate {
before: string
after: string
validation?: JSONSchemaValidation
validation?: FileEditValidation
}

interface RunPromptResultPromiseWithOptions extends Promise<RunPromptResult> {
Expand Down
2 changes: 1 addition & 1 deletion packages/sample/src/edits/editsgen.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
script({
files: "src/edits/fib.ts",
})
$`Generation 10 variations of the SNIPPET in various programming languages and save them in files.
$`Generation 1 variations of the SNIPPET in various programming languages and save them in files.
- there should be lines with comments
- there should be a function with a TODO comment and a BODY comment
Expand Down
2 changes: 1 addition & 1 deletion packages/sample/src/edits/fib.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def fibonacci(n)
# TODO: implement fibonacci algorithm
0 # BODY
return 0 # BODY
end # this one needs to go
# another comment

0 comments on commit 2aa65ba

Please sign in to comment.