GenAIScript Cheat Sheet
Section titled “GenAIScript Cheat Sheet”Quick reference guide for GenAIScript syntax, core functions, and common patterns.
Quick Reference
Section titled “Quick Reference”Function | Purpose | Example |
---|---|---|
script() | Configure script metadata | script({ title: "My Script", model: "openai:gpt-4o" }) |
def() | Include content in prompt | def("FILES", env.files) |
$``...`` | Create prompt template | $ Analyze the FILES |
defTool() | Define custom tool | defTool("weather", "Get weather", {}, () => "sunny") |
defSchema() | Define data schema | defSchema("DATA", { type: "object" }) |
runPrompt() | Execute inline prompt | await runPrompt((_) => _.$ Hello) |
Basic Script Structure
Section titled “Basic Script Structure”Minimal Script
Section titled “Minimal Script”$`Write a poem about code.`;
Complete Script
Section titled “Complete Script”script({ title: "My Script", description: "Does something useful", model: "openai:gpt-4o", temperature: 0.1,});
def("FILES", env.files);$`Analyze FILES and provide insights.`;
Markdown Script Alternative
Section titled “Markdown Script Alternative”---title: "My Script"description: "Does something useful"model: "openai:gpt-4o"---
# Task
Analyze the provided files and provide insights.
Core Functions
Section titled “Core Functions”script() - Script Configuration
Section titled “script() - Script Configuration”script({ // Metadata title: "Script Name", description: "What it does", group: "category",
// Model settings model: "openai:gpt-4o", temperature: 0.1, maxTokens: 4000,
// File handling files: "**/*.md", accept: ".md,.txt,.js", ignore: "**/node_modules/**",
// Tools & capabilities tools: ["fs_read_file", "web_search"], vision: true, cache: true,
// Testing tests: [ { files: "test.md", keywords: ["expected", "output"], }, ],});
def() - Include Content
Section titled “def() - Include Content”// Basic usagedef("VAR_NAME", content);
// With optionsdef("FILES", env.files, { lineNumbers: true, // Add line numbers maxTokens: 1000, // Limit token count language: "markdown", // Specify language glob: "**/*.md", // Filter files by pattern endsWith: ".js", // Filter by extension sliceHead: 100, // Take first N lines sliceTail: 50, // Take last N lines});
// From filedef("CONFIG", await workspace.readText("config.json"));
// Multiple filesdef( "DOCS", env.files.filter((f) => f.endsWith(".md")),);
$“ - Template Prompts
Section titled “$“ - Template Prompts”// Simple prompt$`Write a summary of the content.`;
// Multi-line prompt$`You are an expert reviewer.Analyze the FILES and:1. Check for errors2. Suggest improvements3. Rate quality 1-10`;
// With variablesconst task = "summarize";$`Please ${task} the provided content.`;
env.files
Section titled “env.files”// Current selection/context filesdef("FILES", env.files);
// Filter filesconst mdFiles = env.files.filter((f) => f.endsWith(".md"));const jsFiles = env.files.filter((f) => f.filename.endsWith(".js"),);
Parameters
Section titled “Parameters”script({ parameters: { topic: { type: "string", description: "Topic to write about", }, count: { type: "number", default: 5 }, },});
$`Write ${env.parameters.count} facts about ${env.parameters.topic}.`;
Inline Prompts
Section titled “Inline Prompts”const summary = await runPrompt((_) => { _.def("FILE", file); _.$`Summarize FILE in one sentence.`;});def("SUMMARY", summary.text);
File Operations
Section titled “File Operations”Reading Files
Section titled “Reading Files”// Read text fileconst content = await workspace.readText("path/to/file.txt");
// Read JSONconst data = await workspace.readJSON("data.json");
// Search filesconst { files } = await workspace.grep(/pattern/, { globs: "**/*.js",});
// Find filesconst matches = await workspace.findFiles("**/*.md", { maxFiles: 100,});
Data Handling
Section titled “Data Handling”Schemas & Validation
Section titled “Schemas & Validation”const schema = defSchema("DATA", { type: "object", properties: { name: { type: "string" }, age: { type: "number" }, skills: { type: "array", items: { type: "string" }, }, }, required: ["name"],});
$`Extract data using ${schema} format.`;
File Parsers
Section titled “File Parsers”// CSV - structured dataconst rows = await parsers.CSV(file);defData("TABLE", rows, { sliceHead: 10 });
// PDF - documentsconst { pages } = await parsers.PDF(file);def("CONTENT", pages.map((p) => p.content).join("\n"));
// DOCX - Word documentsconst { content } = await parsers.DOCX(file);
// XLSX - spreadsheetsconst { worksheets } = await parsers.XLSX(file);const sheet1 = worksheets[0];
// JSON - structured configconst config = await parsers.JSON(file);
// YAML - configuration filesconst settings = await parsers.YAML(file);
// XML - markup dataconst { content } = await parsers.XML(file);
Data Transformation
Section titled “Data Transformation”// Filter and process dataconst markdownFiles = env.files.filter((f) => f.endsWith(".md"),);const recentFiles = env.files.filter( (f) => new Date(f.lastModified) > new Date("2024-01-01"),);
// Transform data for contextdefData("SUMMARY", { totalFiles: env.files.length, fileTypes: [...new Set(env.files.map((f) => f.extension))], totalSize: env.files.reduce((sum, f) => sum + f.size, 0),});
Tools & Agents
Section titled “Tools & Agents”Built-in Tools
Section titled “Built-in Tools”script({ tools: [ "fs_read_file", // File system read "fs_find_files", // File search "web_search", // Web search "math_eval", // Math evaluation "python_code_interpreter", // Code execution ],});
Custom Tools
Section titled “Custom Tools”defTool( "weather", "Get current weather", { city: { type: "string", description: "City name" }, }, async ({ city }) => { // Tool implementation return `Weather in ${city}: Sunny, 25°C`; },);
Agents
Section titled “Agents”script({ tools: "agent_fs" }); // File system agentscript({ tools: "agent_git" }); // Git operations agentscript({ tools: "agent_playwright" }); // Web automation agent
// Custom agentdefAgent("myagent", "Description", "You are helpful", { tools: ["fs_read_file", "web_search"],});
Images & Media
Section titled “Images & Media”Images
Section titled “Images”// Include images in promptdefImages( env.files.filter((f) => f.endsWith(".png")), { autoCrop: true, details: "low", // or "high" },);
// Generate imagesconst { image } = await generateImage("a cute cat");
Videos
Section titled “Videos”// Extract framesconst frames = await ffmpeg.extractFrames(videoFile, { count: 5,});defImages(frames);
// Transcriptionconst transcript = await transcript(audioFile);def("TRANSCRIPT", transcript.text);
CLI Commands
Section titled “CLI Commands”Running Scripts
Section titled “Running Scripts”# Run script on filesnpx genaiscript run scriptname "**/*.md"
# With specific modelnpx genaiscript run scriptname --model openai:gpt-4o
# Apply edits directlynpx genaiscript run scriptname --apply-edits
# Pull request modenpx genaiscript run scriptname --pull-request-reviews
# Dry run (no changes)npx genaiscript run scriptname --dry-run
# With temperature settingnpx genaiscript run scriptname --temperature 0.1
# Maximum tokensnpx genaiscript run scriptname --max-tokens 2000