Mark Pitt
claude-skills
  1. claude-skills
  2. AI engineering
  3. genaiscript
  4. resources/api-reference.md

A file in genaiscript

resources/api-reference.md

Markdown 19 KB
api-reference.mdMarkdown, rendered

GenAIScript API Reference

Complete reference for all GenAIScript functions and APIs.

Contents

Core Functions

script(options)

Defines script metadata and configuration.

Parameters:

{
    title?: string              // Display name
    description?: string        // What the script does
    model?: string             // Model alias (preferred) or provider id, e.g. "large"
    temperature?: number       // 0.0-2.0, creativity vs consistency
    maxTokens?: number         // Maximum response tokens
    topP?: number             // Nucleus sampling (0.0-1.0)
    system?: string[]         // System prompt templates
    tools?: string[]          // Available tools
    cache?: boolean           // Enable prompt caching
    cacheName?: string        // Cache identifier
    responseType?: string     // "json" | "yaml" | "json_object" | "json_schema"
    responseSchema?: JSONSchema // Validates the script's own output
    tests?: object | object[] // Script tests: files, rubrics, facts, asserts
    testModels?: string[]     // Run the test suite across several models
    mcpServers?: object       // MCP server config (see best-practices.md)
    systemSafety?: string     // "default" forces the default safety stack
    reasoningEffort?: string  // Only for reasoning models
    fallbackTools?: boolean   // For models without native tool calling
    parameters?: {            // User-provided parameters
        [key: string]: {
            type: string
            description: string
            default?: any
        }
    }
    files?: string | string[] // File patterns to include
}

Example:

script({
    title: "Code Reviewer",
    description: "Reviews code for issues",
    model: "large",
    temperature: 0.3,
    maxTokens: 4000,
    system: ["system.annotations"],
    cache: true,
    parameters: {
        severity: {
            type: "string",
            description: "Minimum severity level",
            default: "warning"
        }
    }
})

---

$(template)

Creates a prompt from a template string.

Parameters:

  • template (TemplateStringsArray): Template literal with interpolated values

Returns: void

Example:

const topic = "AI automation"
const tone = "professional"

$`
You are a ${tone} technical writer.
Write an article about ${topic}.

Include:
- Introduction
- Key concepts
- Examples
- Conclusion
`

---

def(name, content, options?)

Includes file content in the prompt with optimization.

Parameters:

def(
    name: string,           // Identifier in prompt
    content: string | string[],  // File path(s)
    options?: {
        endsWith?: string | string[]  // Filter by extension
        glob?: string | string[]      // Glob pattern
        lineNumbers?: boolean         // Add line numbers
        language?: string             // Syntax highlighting
        maxTokens?: number           // Token limit
        sliceHead?: number           // First N lines
        sliceTail?: number           // Last N lines
    }
)

Example:

// Basic usage
def("FILE", env.files)

// With filters
def("CODE", env.files, {
    endsWith: [".ts", ".tsx"],
    lineNumbers: true
})

// Token limiting
def("LARGE_FILE", env.files, {
    maxTokens: 2000,
    sliceHead: 500,
    sliceTail: 500
})

// Glob pattern
def("COMPONENTS", env.files, {
    glob: "src/components/**/*.tsx"
})

---

defData(name, data, options?)

Includes structured data in the prompt (rendered as YAML by default).

Parameters:

defData(
    name: string,           // Identifier in prompt
    data: any,             // Data to include
    options?: {
        format?: "yaml" | "json"  // Output format
        sliceHead?: number        // Limit array items (head)
        sliceTail?: number        // Limit array items (tail)
    }
)

Example:

// Object data
const config = { theme: "dark", language: "en" }
defData("CONFIG", config)

// Array data
const users = [
    { name: "Alice", role: "admin" },
    { name: "Bob", role: "user" }
]
defData("USERS", users)

// CSV parsing
const rows = await parsers.CSV(env.files[0])
defData("ROWS", rows, { sliceHead: 100 })

// JSON format
defData("DATA", largeObject, { format: "json" })

---

defSchema(name, schema)

Defines expected output structure using JSON Schema.

defSchema() or responseSchema? defSchema() declares a named schema inside the prompt, for the model to reference, or to constrain a defTool() output. responseSchema is a script() option that validates the script's own output, and pairs with responseType: "json_schema" for a strict machine contract. Use responseSchema when something downstream parses the result. See resources/best-practices.md → Structured Output Reliability.

Parameters:

defSchema(
    name: string,              // Schema identifier
    schema: JSONSchema         // JSON Schema object
)

Returns: Schema reference for use in prompts

Example:

// Simple array
const keywords = defSchema("KEYWORDS", {
    type: "array",
    items: { type: "string" }
})

// Object schema
const user = defSchema("USER", {
    type: "object",
    properties: {
        name: { type: "string" },
        email: { type: "string", format: "email" },
        age: { type: "number", minimum: 0 }
    },
    required: ["name", "email"]
})

// Complex nested schema
const analysis = defSchema("ANALYSIS", {
    type: "object",
    properties: {
        summary: {
            type: "string",
            minLength: 10,
            maxLength: 500
        },
        issues: {
            type: "array",
            items: {
                type: "object",
                properties: {
                    severity: {
                        type: "string",
                        enum: ["low", "medium", "high", "critical"]
                    },
                    description: { type: "string" },
                    line: { type: "number" },
                    file: { type: "string" }
                },
                required: ["severity", "description"]
            }
        },
        score: {
            type: "number",
            minimum: 0,
            maximum: 100
        }
    },
    required: ["summary", "score"]
})

// Use in prompt
$`Analyze the code and return results using ${analysis} schema.`

---

defTool(name, description, parameters, implementation)

Registers a JavaScript function as an LLM tool.

Parameters:

defTool(
    name: string,              // Tool name
    description: string,       // What the tool does
    parameters: {              // Parameter schema
        [key: string]: {
            type: string
            description?: string
            enum?: string[]
            default?: any
        }
    },
    implementation: (args: any) => Promise<any>  // Tool function
)

Example:

// Simple tool
defTool(
    "getCurrentTime",
    "Gets the current time in ISO format",
    {},
    async () => new Date().toISOString()
)

// Tool with parameters — note: validate all LLM-supplied arguments before external calls
// to prevent SSRF and injection via attacker-controlled prompt arguments
const WEATHER_API_BASE = "https://api.weather.com"
defTool(
    "fetchWeather",
    "Fetches weather data for a named city",
    {
        location: {
            type: "string",
            description: "City name (letters, spaces, commas only)"
        },
        units: {
            type: "string",
            enum: ["metric", "imperial"],
            default: "metric"
        }
    },
    async (args) => {
        // Validate LLM-supplied input before use in external request
        if (!/^[a-zA-Z\s,.-]{1,100}$/.test(args.location)) {
            throw new Error("Invalid location: only city names are accepted")
        }
        const units = ["metric", "imperial"].includes(args.units) ? args.units : "metric"
        const url = `${WEATHER_API_BASE}/v1/current?location=${encodeURIComponent(args.location)}&units=${units}`
        const response = await fetch(url)
        if (!response.ok) throw new Error(`Weather API error: ${response.status}`)
        const data = await response.json()
        // Return only typed, expected fields — not the raw API response
        return { temperature: data.temp, condition: data.weather, city: data.city }
    }
)

// Tool with file operations
defTool(
    "readConfig",
    "Reads configuration from a file",
    {
        path: {
            type: "string",
            description: "Config file path"
        }
    },
    async (args) => {
        const content = await host.readFile(args.path)
        return JSON.parse(content)
    }
)

// Tool with multiple parameters
defTool(
    "calculate",
    "Performs mathematical operations",
    {
        operation: {
            type: "string",
            enum: ["add", "subtract", "multiply", "divide"],
            description: "Math operation to perform"
        },
        a: {
            type: "number",
            description: "First operand"
        },
        b: {
            type: "number",
            description: "Second operand"
        }
    },
    async ({ operation, a, b }) => {
        switch (operation) {
            case "add": return a + b
            case "subtract": return a - b
            case "multiply": return a * b
            case "divide": return b !== 0 ? a / b : "Error: Division by zero"
        }
    }
)

---

defAgent(name, description, options, implementation)

Creates an agent that can use tools to accomplish tasks.

Parameters:

defAgent(
    name: string,              // Agent name
    description: string,       // Agent purpose
    options: {
        model?: string         // LLM model
        system?: string[]      // System prompts
        tools?: string[]       // Available tools
        temperature?: number   // Creativity setting
    },
    implementation?: (context) => Promise<any>  // Agent logic
)

Example:

// Research agent — SECURITY NOTE: agents with webSearch/external tools are susceptible
// to indirect prompt injection (W011). Web content may contain adversarial instructions.
// Mitigate by: (1) isolating content extraction from action execution, (2) using strict
// defSchema output, (3) including system.safety. See patterns.md → Security Patterns.
defAgent(
    "researcher",
    "Researches topics and summarizes findings",
    {
        model: "large",
        system: ["You are a research assistant", "system.safety"],
        tools: ["webSearch", "summarize"],
        temperature: 0.7
    }
)

// Custom agent with implementation
defAgent(
    "codeAnalyzer",
    "Analyzes code and suggests improvements",
    {
        model: "large",
        tools: ["readFile", "parseCode", "searchDocs"]
    },
    async (context) => {
        // Custom agent logic
        const files = await context.tool("readFile", { path: "src/" })
        const analysis = await context.generate(
            `Analyze these files: ${files}`
        )
        return analysis
    }
)

---

defFileOutput(pattern, description?)

Declares files that the script will generate.

Parameters:

defFileOutput(
    pattern: string,           // File path or pattern
    description?: string       // File purpose
)

Example:

// Single file
defFileOutput("output.md", "Generated documentation")

// Multiple files
defFileOutput("summary.txt", "Summary of analysis")
defFileOutput("data.json", "Extracted data")
defFileOutput("report.html", "HTML report")

// Pattern-based
defFileOutput("*.test.ts", "Generated test files")
defFileOutput("docs/*.md", "Documentation files")

---

Parsers

Built-in parsers for various file formats.

parsers.PDF(filePath)

Parses PDF files.

Returns:

{
    text: string          // Full text content
    pages: Array<{        // Per-page content
        pageNumber: number
        text: string
    }>
}

Example:

const { pages, text } = await parsers.PDF(env.files[0])
defData("PDF_CONTENT", pages)

---

parsers.CSV(filePath, options?)

Parses CSV files.

Parameters:

parsers.CSV(
    filePath: string,
    options?: {
        delimiter?: string     // Default: ","
        headers?: boolean      // Default: true
    }
)

Returns: Array<Record<string, string>>

Example:

const rows = await parsers.CSV(env.files[0])
defData("DATA", rows, { sliceHead: 100 })

// Custom delimiter
const tsvRows = await parsers.CSV(file, { delimiter: "\t" })

---

parsers.XLSX(filePath)

Parses Excel files.

Returns:

{
    [sheetName: string]: Array<Record<string, any>>
}

Example:

const sheets = await parsers.XLSX(env.files[0])
defData("EXCEL_DATA", sheets)

// Access specific sheet
const firstSheet = Object.values(sheets)[0]

---

parsers.DOCX(filePath)

Parses Word documents.

Returns:

{
    text: string          // Full text content
}

Example:

const { text } = await parsers.DOCX(env.files[0])
def("DOCUMENT", text)

---

parsers.JSON(filePath)

Parses JSON files.

Returns: any (parsed JSON)

Example:

const data = await parsers.JSON(env.files[0])
defData("JSON_DATA", data)

---

parsers.YAML(filePath)

Parses YAML files.

Returns: any (parsed YAML)

Example:

const config = await parsers.YAML(".github/workflows/ci.yml")
defData("CI_CONFIG", config)

---

Environment (env)

Access to runtime environment and variables.

env.files

Array of file paths provided to the script.

Type: string[]

Example:

if (!env.files || env.files.length === 0) {
    throw new Error("No files provided")
}

const tsFiles = env.files.filter(f => f.endsWith(".ts"))

---

env.vars

User-provided variables.

Type: Record<string, string>

Example:

const targetLang = env.vars.TARGET_LANG || "rust"
const severity = env.vars.MIN_SEVERITY || "warning"

---

env.script

Current script metadata.

Type:

{
    title: string
    description: string
    id: string
}

---

env.generator

Information about the LLM being used.

Type:

{
    model: string
    temperature: number
    maxTokens: number
}

---

Host (host)

Interface to system operations.

host.exec(command)

Executes shell command.

Parameters:

  • command (string): Shell command to execute

Returns:

{
    stdout: string
    stderr: string
    exitCode: number
}

Example:

// Git log
const { stdout } = await host.exec("git log --oneline -10")
defData("RECENT_COMMITS", stdout.split('\n'))

// Find files
const { stdout: files } = await host.exec("find . -name '*.ts'")

// Package info
const { stdout: version } = await host.exec("npm --version")

---

host.readFile(path)

Reads file content.

Parameters:

  • path (string): File path

Returns: Promise<string>

Example:

const content = await host.readFile("package.json")
const pkg = JSON.parse(content)

---

host.writeFile(path, content)

Writes file content.

Parameters:

  • path (string): File path
  • content (string): File content

Returns: Promise<void>

Example:

await host.writeFile("output.json", JSON.stringify(data, null, 2))

---

System Prompts

Built-in system prompts that can be included in script() configuration.

Available System Prompts

  • system.annotations - Use structured annotation format
  • system.safety - Safety and ethical guidelines
  • system.python - Python programming expert
  • system.typescript - TypeScript programming expert
  • system.javascript - JavaScript programming expert
  • system.files - File operation guidelines
  • system.diagram - Diagram generation (Mermaid, etc.)
  • system.math - Mathematical reasoning
  • system.explanations - Clear explanations

Example:

script({
    system: [
        "system.annotations",
        "system.typescript",
        "system.safety"
    ]
})

---

JSON Schema Types

Common JSON Schema patterns for defSchema().

String

{ type: "string" }
{ type: "string", minLength: 1, maxLength: 100 }
{ type: "string", pattern: "^[A-Z]" }
{ type: "string", format: "email" }  // email, uri, date-time
{ type: "string", enum: ["small", "medium", "large"] }

Number

{ type: "number" }
{ type: "number", minimum: 0, maximum: 100 }
{ type: "integer" }
{ type: "integer", multipleOf: 5 }

Boolean

{ type: "boolean" }

Array

{ type: "array", items: { type: "string" } }
{ type: "array", items: { type: "number" }, minItems: 1, maxItems: 10 }
{ type: "array", items: { ... }, uniqueItems: true }

Object

{
    type: "object",
    properties: {
        name: { type: "string" },
        age: { type: "number" }
    },
    required: ["name"]
}

Complex Types

{
    oneOf: [
        { type: "string" },
        { type: "number" }
    ]
}

{
    anyOf: [
        { type: "string", format: "email" },
        { type: "string", format: "uri" }
    ]
}

{
    allOf: [
        { type: "object", properties: { id: { type: "string" } } },
        { type: "object", properties: { name: { type: "string" } } }
    ]
}

---

Best Practices

1. Token Management

// ❌ May exceed token limits
def("LOGS", env.files)

// ✅ Controlled token usage
def("LOGS", env.files, {
    maxTokens: 2000,
    sliceHead: 500,
    sliceTail: 500
})

2. Schema Validation

// ❌ No structure
$`Extract data from the file`

// ✅ Structured output
const data = defSchema("DATA", { ... })
$`Extract data using ${data} schema`

3. Error Handling

// ✅ Validate inputs
if (!env.files || env.files.length === 0) {
    throw new Error("No files provided")
}

const validFiles = env.files.filter(f => f.endsWith(".ts"))
if (validFiles.length === 0) {
    throw new Error("No TypeScript files found")
}

4. Clear Prompts

// ❌ Vague
$`Analyze the code`

// ✅ Specific
$`
Analyze CODE for:
1. Type safety issues
2. Unused variables
3. Missing error handling

Provide specific line numbers and fix suggestions.
`

---

This API reference covers all major GenAIScript functions and patterns. For more examples, see examples.md.