Skip to content

Commit

Permalink
Add unfence function to handle fenced code blocks and remove unused m…
Browse files Browse the repository at this point in the history
…odel specifications
  • Loading branch information
pelikhan committed Sep 20, 2024
1 parent f67de62 commit 0a4b28e
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 22 deletions.
6 changes: 4 additions & 2 deletions packages/core/src/fence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ ${validation.error.split("\n").join("\n> ")}`
}

export function unfence(text: string, language: string) {
const startRx = new RegExp(`^\`\`\`${language}\n`)
const endRx = /\n```[\n\s]*$/
if (!text) return text

const startRx = new RegExp(`^[\r\n\s]*\`\`\`${language}\s*\r?\n`)

Check failure

Code scanning / CodeQL

Useless regular-expression character escape High

The escape sequence '\s' is equivalent to just 's', so the sequence is not a character class when it is used in a
regular expression
.

Check failure

Code scanning / CodeQL

Useless regular-expression character escape High

The escape sequence '\s' is equivalent to just 's', so the sequence is not a character class when it is used in a
regular expression
.
const endRx = /\r?\n```[\r\n\s]*$/

Check failure on line 222 in packages/core/src/fence.ts

View workflow job for this annotation

GitHub Actions / build

The regular expressions for startRx and endRx have been modified to include carriage return and whitespace characters. This could lead to unexpected behavior if the input text does not match the expected format. Please ensure that the input text is properly formatted. 🧐
if (startRx.test(text) && endRx.test(text)) {
return text.replace(startRx, "").replace(endRx, "")
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/ini.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { parse, stringify } from "ini"
import { logError } from "./util"
import { unfence } from "./fence"

export function INIParse(text: string) {
return parse(text)
const cleaned = unfence(text, "ini")
return parse(cleaned)

Check failure on line 7 in packages/core/src/ini.ts

View workflow job for this annotation

GitHub Actions / build

The function `unfence` is being used to clean the input text before parsing it. This could lead to unexpected results if the input text does not contain the expected fenced code blocks. Please ensure that the input text is properly formatted. 😊
}

export function INITryParse(text: string, defaultValue?: any) {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/json5.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable curly */
import { parse, stringify } from "json5"
import { jsonrepair } from "jsonrepair"
import { unfence } from "./fence"

export function isJSONObjectOrArray(text: string) {
return /^\s*[\{\[]/.test(text)
Expand All @@ -20,6 +21,7 @@ export function JSON5parse<T = unknown>(
}
): T | undefined | null {
try {
text = unfence(text, "json")
if (options?.repair) {
try {
const res = parse(text)
Expand Down Expand Up @@ -57,9 +59,8 @@ const startRx = /^\s*\`\`\`json\s/
const endRx = /\`\`\`\s*$/
export function JSONLLMTryParse(s: string): any {
if (s === undefined || s === null) return s
if (startRx.test(s) && endRx.test(s))
s = s.replace(startRx, "").replace(endRx, "")
return JSON5TryParse(s)
const cleaned = unfence(s, "json")
return JSON5TryParse(cleaned)
}

export const JSON5Stringify = stringify
4 changes: 3 additions & 1 deletion packages/core/src/toml.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { parse } from "toml"
import { unfence } from "./fence"

export function TOMLTryParse(text: string, options?: { defaultValue?: any }) {
try {
const res = parse(text)
const cleaned = unfence(text, "toml")
const res = parse(cleaned)
return res
} catch (e) {
return options?.defaultValue
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/xml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { XMLParser } from "fast-xml-parser"
import { logError } from "./util"
import { unfence } from "./fence"

export function XMLTryParse(
text: string,
Expand All @@ -15,6 +16,7 @@ export function XMLTryParse(
}

export function XMLParse(text: string, options?: XMLParseOptions) {
const cleaned = unfence(text, "xml")
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "@_",
Expand All @@ -23,5 +25,5 @@ export function XMLParse(text: string, options?: XMLParseOptions) {
parseAttributeValue: true,
...(options || {}),
})
return parser.parse(text)
return parser.parse(cleaned)

Check failure on line 28 in packages/core/src/xml.ts

View workflow job for this annotation

GitHub Actions / build

The function `unfence` is being used to clean the input text before parsing it as XML. This could lead to unexpected results if the input text does not contain the expected fenced code blocks. Please ensure that the input text is properly formatted. 😊
}
1 change: 0 additions & 1 deletion packages/vscode/genaisrc/gcm.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
script({
title: "git commit message",
description: "Generate a commit message for all staged changes",
model: "openai:gpt-4o",
})

// TODO: update this diff command to match your workspace
Expand Down
2 changes: 0 additions & 2 deletions packages/vscode/genaisrc/iat.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
script({
title: "Image Alt Textify",
description: "Generate alt text for images in markdown files",
model: "openai:gpt-4o",
// these parameters can be overridden using `--vars doc=...`
parameters: {
docs: {
type: "string",
Expand Down
12 changes: 1 addition & 11 deletions packages/vscode/genaisrc/rv.genai.mts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
/**
* GenAIScript Reviewer
*
* This script is used to review the current files
* or the changes in the git repository.
*
* Don't hesitate to customize this script to fit your needs!
*/

script({
title: "Reviewer",
description: "Review the current files",
model: "openai:gpt-4o",
system: ["system.annotations"],
tools: ["fs_find_files", "fs_read_text"],
cache: "rv",
Expand Down Expand Up @@ -44,7 +34,7 @@ if (env.files.length) {
// No files selected, review the current changes
console.log("No files found. Using git diff.")
const { stdout: diff } = await host.exec("git diff -U6")
// customize git diff to filter some files
// customize git diff to filter some files
if (!diff) cancel("No changes found, did you forget to stage your changes?")
content = def("GIT_DIFF", diff, { language: "diff" })
}
Expand Down
1 change: 1 addition & 0 deletions packages/vscode/genaisrc/st.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ script({
},
},
})

const { pattern, glob, transform } = env.vars
if (!pattern) cancel("pattern is missing")
const patternRx = new RegExp(pattern, "g")
Expand Down

0 comments on commit 0a4b28e

Please sign in to comment.