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

Refactor: Remove file validation and add sample scripts #778

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@
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.

Choose a reason for hiding this comment

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

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.

generated by pr-review-commit missing_check


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 @@
*/
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.

Choose a reason for hiding this comment

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

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.

generated by pr-review-commit logic_error

// 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
67 changes: 0 additions & 67 deletions packages/core/src/promptrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,70 +292,3 @@ export async function runTemplate(
await runtimeHost.removeBrowsers()
}
}

// Validate file outputs against specified schemas and patterns
/**
* Validates file outputs based on provided patterns and schemas.
* @param fileOutputs List of file outputs to validate.
* @param trace The markdown trace for logging.
* @param fileEdits Record of file updates.
* @param schemas The JSON schemas for validation.
*/
function validateFileOutputs(
fileOutputs: FileOutput[],
trace: MarkdownTrace,
fileEdits: Record<string, FileUpdate>,
schemas: Record<string, JSONSchema>
) {
if (fileOutputs?.length && Object.keys(fileEdits || {}).length) {
trace.startDetails("🗂 file outputs")
for (const fileEditName of Object.keys(fileEdits)) {
const fe = fileEdits[fileEditName]
for (const fileOutput of fileOutputs) {
const { pattern, options } = fileOutput
if (isGlobMatch(fileEditName, pattern)) {
try {
trace.startDetails(`📁 ${fileEditName}`)
trace.itemValue(`pattern`, pattern)
const { schema: schemaId } = options || {}
if (/\.(json|yaml)$/i.test(fileEditName)) {
const { after } = fileEdits[fileEditName]
const data = /\.json$/i.test(fileEditName)
? JSON.parse(after)
: YAMLParse(after)
trace.detailsFenced("📝 data", data)
if (schemaId) {
const schema = schemas[schemaId]
if (!schema)
fe.validation = {
valid: false,
error: `schema ${schemaId} not found`,
}
else
fe.validation = validateJSONWithSchema(
data,
schema,
{
trace,
}
)
}
} else {
fe.validation = { valid: true }
}
} catch (e) {
trace.error(errorMessage(e))
fe.validation = {
valid: false,
error: errorMessage(e),
}
} finally {
trace.endDetails()
}
break
}
}
}
trace.endDetails()
}
}
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
1 change: 1 addition & 0 deletions packages/sample/src/edits/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
genaiscript.d.ts -diff merge=ours linguist-generated
4 changes: 4 additions & 0 deletions packages/sample/src/edits/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# auto-generated
genaiscript.d.ts
tsconfig.json
jsconfig.json
9 changes: 9 additions & 0 deletions packages/sample/src/edits/edits_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/fib.ts",
system: ["system", "system.changelog"],
})

editTest()
9 changes: 9 additions & 0 deletions packages/sample/src/edits/edits_diff.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/fib.ts",
system: ["system", "system.diff"],
})

editTest()
37 changes: 37 additions & 0 deletions packages/sample/src/edits/edits_files.genai.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
script({
model: "large",
title: "system.files test",
files: "src/edits/fib.*",
system: ["system", "system.files"],
tests: [
{
files: "src/edits/fib.ts",
},
{
files: "src/edits/fib.cpp",
},
{
files: "src/edits/fib.cs",
},
{
files: "src/edits/fib.go",
},
{
files: "src/edits/fib.java",
},
{
files: "src/edits/fib.js",
},
{
files: "src/edits/fib.kt",
},
{
files: "src/edits/fib.py",
},
{
files: "src/edits/fib.swift",
},
],
})
import { editTest } from "./fileedittest.mts"
editTest()
10 changes: 10 additions & 0 deletions packages/sample/src/edits/editsgen.genai.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
script({
files: "src/edits/fib.ts",
})
$`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
`
def("FILE", env.files)
defFileOutput("src/edits/*")
7 changes: 7 additions & 0 deletions packages/sample/src/edits/fib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int fibonacci(int n) {
// TODO: implement fibonacci algorithm
return 0; // BODY
} // this one needs to go
// another comment
7 changes: 7 additions & 0 deletions packages/sample/src/edits/fib.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Fibonacci {
public static int fibonacci(int n) {
// TODO: implement fibonacci algorithm
return 0; // BODY
} // this one needs to go
// another comment
}
7 changes: 7 additions & 0 deletions packages/sample/src/edits/fib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

func fibonacci(n int) int {
// TODO: implement fibonacci algorithm
return 0 // BODY
} // this one needs to go
// another comment
7 changes: 7 additions & 0 deletions packages/sample/src/edits/fib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Fibonacci {
public static int fibonacci(int n) {
// TODO: implement fibonacci algorithm
return 0; // BODY
} // this one needs to go
// another comment
}
5 changes: 5 additions & 0 deletions packages/sample/src/edits/fib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function fibonacci(n) {
// TODO: implement fibonacci algorithm
return 0; // BODY
} // this one needs to go
// another comment
5 changes: 5 additions & 0 deletions packages/sample/src/edits/fib.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fun fibonacci(n: Int): Int {
// TODO: implement fibonacci algorithm
return 0 // BODY
} // this one needs to go
// another comment
7 changes: 7 additions & 0 deletions packages/sample/src/edits/fib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
function fibonacci($n) {
// TODO: implement fibonacci algorithm
return 0; // BODY
} // this one needs to go
// another comment
?>
5 changes: 5 additions & 0 deletions packages/sample/src/edits/fib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def fibonacci(n):
# TODO: implement fibonacci algorithm
return 0 # BODY
# this one needs to go
# another comment
5 changes: 5 additions & 0 deletions packages/sample/src/edits/fib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def fibonacci(n)
# TODO: implement fibonacci algorithm
return 0 # BODY
end # this one needs to go
# another comment
5 changes: 5 additions & 0 deletions packages/sample/src/edits/fib.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
func fibonacci(_ n: Int) -> Int {
// TODO: implement fibonacci algorithm
return 0 // BODY
} // this one needs to go
// another comment
5 changes: 5 additions & 0 deletions packages/sample/src/edits/fib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function fibonacci(n: number): number {
// TODO: implement fibonacci algorithm
return 0 // BODY
} // this one needs to go
// another comment

Check failure on line 5 in packages/sample/src/edits/fib.ts

View workflow job for this annotation

GitHub Actions / build

The function `fibonacci` is not implemented and will always return 0. This could lead to incorrect results.

Choose a reason for hiding this comment

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

The function fibonacci is not implemented and will always return 0. This could lead to incorrect results.

generated by pr-review-commit unimplemented_code

17 changes: 17 additions & 0 deletions packages/sample/src/edits/fileedittest.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function editTest() {
def("FILE", env.files)

$`- Implement the functions with TODO.
- Remove all comments.
`

defOutputProcessor((output) => {
const { fileEdits } = output
//console.log(YAML.stringify(fileEdits))
const res = fileEdits[Object.keys(fileEdits)[0]].after
if (/^\s*\/\/.*$/.test(res))
throw new Error("some comments were not removed")
if (res.includes("// BODY"))
throw new Error("the // BODY comment was not removed")
})
}