Skip to content

Spell Checker

Automating and improving the efficiency of proofreading documents is a common need among developers and writers. This script addresses this need by checking and correcting spelling and grammar in Markdown files.

Code Explanation

Starting at the top of the script, we see that it’s a GenAI script, which is evident from the .mts extension and the script function call.

script({
title: "Spell checker",
system: ["system", "system.files", "system.diff"],
temperature: 0.1,
})

This block sets the title of the script to “Spell checker” and specifies that it uses several system prompts, such as file operations and diff generation. The temperature is set to 0.1, indicating that the script will generate output with low creativity, thus favoring precision.

Fetching Files for Checking

Next, we check for files to process, first from the environment and then from Git if none are provided.

let files = env.files
if (files.length === 0) {
const gitStatus = await host.exec("git diff --name-only --cached")
files = await Promise.all(
gitStatus.stdout
.split(/\r?\n/g)
.filter((filename) => /\.(md|mdx)$/.test(filename))
.map(async (filename) => await workspace.readText(filename))
)
}

In this block, we’re assigning files from the env variable, which would contain any files passed to the script. If no files are provided, we execute a Git command to get a list of all cached (staged) modified files and filter them to include only .md and .mdx files. We then read the content of these files for processing.

Defining the File Types to Work on

Following this, there’s a def call:

def("FILES", files, { endsWith: [".md", ".mdx"] })

This line defines FILES to be the array of files we gathered. The options object { endsWith: [".md", ".mdx"] } tells GenAI that we’re only interested in files ending with .md or .mdx.

The $-prefixed backtick notation is used to write the prompt template:

$`Fix the spelling and grammar of the content of FILES. Use diff format for small changes.
- do NOT fix the frontmatter
- do NOT fix code regions
- do NOT fix \`code\` and \`\`\`code\`\`\`
- in .mdx files, do NOT fix inline typescript code
`

This prompt instructs GenAI to fix spelling and grammar in the content of the defined FILES, outputting small changes in diff format. It also specifies constraints, such as not fixing the frontmatter, code regions, inline code in markdown, and inline TypeScript code in MDX files.

Finally, there is a defFileOutput call:

defFileOutput(files, "fixed markdown or mdx files")

This call declares the intent that the script will generate “fixed markdown or mdx files” based on the input files.

How to Run the Script with GenAIScript CLI

Running this spell checker script is straightforward with the GenAIScript CLI. First, ensure you have the CLI installed by following the instructions in the GenAIScript documentation.

Once you have the CLI installed, navigate to your local copy of the script in your terminal or command line interface. Run the following command to execute the spell checker:

Terminal window
genaiscript run sc

Remember, you do not need to specify the .genai.mts extension when using the run command.

And there you have it—a detailed walkthrough of a GenAI spell checker script for markdown files. Happy coding and perfecting your documents!

Full source (GitHub)

sc.genai.mts
script({
title: "Spell checker",
parameters: {
concurrency: {
type: "number",
description: "Number of concurrent jobs",
default: 1,
},
},
})
const { concurrency } = env.vars
// Get files from environment or modified files from Git if none provided
let files = env.files
if (files.length === 0) {
files = await git.listFiles("staged", {
paths: ["*.md", "*.mdx"],
})
if (!files.length)
files = await git.listFiles("modified-base", {
paths: ["*.md", "*.mdx"],
})
}
files = files.filter(({ filename }) => /.mdx?$/i.test(filename))
const jobs = host.promiseQueue(concurrency)
await jobs.mapAll(
files,
async (file) =>
await runPrompt(
(ctx) => {
ctx.def("FILES", file)
ctx.$`Analyze the spelling and grammar of the content of FILES.
If you find a spelling or grammar mistake, fix it. Use CHANGELOG file format for small changes.
If you do not find any mistakes, respond <NO> and nothing else.
- only fix major errors
- use a technical documentation tone
- minimize changes; do NOT change the meaning of the content
- if the grammar is good enough, do NOT change it
- do NOT modify the frontmatter. THIS IS IMPORTANT.
- do NOT modify code regions. THIS IS IMPORTANT.
- do NOT fix \`code\` and \`\`\`code\`\`\` sections
- in .mdx files, do NOT fix inline typescript code
`
ctx.defFileOutput(files, "fixed markdown or mdx files")
},
{
label: `spell check ${file.filename}`,
model: "large",
system: [
"system.assistant",
"system.files",
"system.changelog",
"system.safety_jailbreak",
"system.safety_harmful_content",
],
temperature: 0.2,
cache: "sc",
}
)
)

Content Safety

The following measures are taken to ensure the safety of the generated content.

  • This script includes system prompts to prevent prompt injection and harmful content generation.
  • The generated description is saved to a file at a specific path, which allows for a manual review before committing the changes.

Additional measures to further enhance safety would be to run a model with a safety filter or validate the message with a content safety service.

Refer to the Transparency Note for more information on content safety.