patbat-codegen v0.1.1
PatBat - Constrained Code Generation
A tool for constrained code generation with integrated validation, powered by AI. Use it as a library in your TypeScript/JavaScript projects or directly from the command line.
Prerequisites
- Bun Runtime
- Environment Variables:
- For Anthropic:
ANTHROPIC_API_KEY
- For OpenAI:
OPENAI_API_KEY
- For Anthropic:
Installation
bunx patbat-codegen
Usage
SDK Mode
import { AITransformData } from "patbat-codegen";
const result = await AITransformData(
{
// Core inputs
problem: "transform data to get average age and oldest person",
inputSchema: "z.object({ name: z.string(), age: z.number() })",
inputData: [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 }
],
outputSchema: "z.object({ averageAge: z.string(), oldestPerson: z.string() })"
},
{
// Optional configuration
failOnInvalidData: false,
returnGeneratedCode: false,
minSamplesCount: 1,
maxSampleTokenSize: 1000,
validateOutput: false,
aiAdapterConfig: {
provider: 'anthropic',
model: 'claude-3-5-haiku-20241022',
temperature: 0
}
}
);
CLI Mode
The CLI supports multiple ways to provide input and configuration:
1. Single Input File
bunx patbat-codegen --input-path input.json
Where input.json
contains all required fields (inputSchema, inputData, outputSchema).
2. Component Files
You can provide each component separately:
bunx patbat-codegen \
--input-schema-path schema.ts \
--input-data-path data.json \
--output-schema-path output-schema.ts
3. Unix-Style Pipeline
Use stdin for any component while specifying which one via the --stdin flag:
# Pipe in complete input
cat input.json | bunx patbat-codegen --stdin input
# Pipe in just the data (or any other component via --stdin)
cat data.json | bunx patbat-codegen \
--stdin inputData \
--input-schema-path schema.ts \
--output-schema-path output-schema.ts
CLI Options
Core Options
--input-path <path>
- Path to complete input JSON file--config-path <path>
- Path to configuration JSON file--problem <text>
- Override or set the problem description
Component File Options
--input-schema-path <path>
- Path to input schema file--input-data-path <path>
- Path to input data JSON file--output-schema-path <path>
- Path to output schema file
Input Method
--stdin <type>
- Read from stdin: "input", "inputData", "inputSchema", or "outputSchema"
Debug Options
--debug
- Write generated code to code.tmp.ts--stats
- Write full response to stats.tmp.json
Output Handling
stdout
contains only the transformation result- Warnings and errors are written to
stderr
- Debug files (if enabled) are written to:
code.tmp.ts
- Generated TypeScript codestats.tmp.json
- Complete execution statistics
Configuration Options
Input Schema
type CodegenInput = {
problem: string; // Transformation description
inputSchema: string; // Zod schema for input validation
inputData: any[]; // Array of data to transform
outputSchema: string; // Zod schema for output validation
}
Configuration Schema
type CodegenConfig = {
failOnInvalidData: boolean; // Stop on validation errors
returnGeneratedCode: boolean; // Include code in response
minSamplesCount: number; // Minimum samples for context
maxSampleTokenSize: number; // Max tokens for sampling
validateOutput: boolean; // Validate transformation result
aiAdapterConfig: {
provider: 'anthropic' | 'openai-standard' | 'openai-custom';
model: string;
temperature?: number;
maxTokens?: number;
}
}
Testing
Run the CLI tests with:
bash ./samples/cli-sample1/run-test.sh
Future Improvements
Smart Sample Selection
Planning to integrate the "pickasso" npm package for more intelligent data sampling.Modular Architecture
The following modules are being prepared for separate npm packages:aidap
- AI adapter module for different providerszodinator
- Zod convenience utilities
These modules are already well-separated in the codebase and will be published independently.
maxRetries, timeoutMs, lifecycle hooks are not implemented yet on this branch
Correct shebangs for the right runtimes during build