Skip to content

code-agents/agentscript

Repository files navigation

AgentScript: Your AI-First Intermediate Language for Truly Robust Code

AgentScript is a new proposal that seamlessly bridges AI-generated code and human-readable syntax. Its mission is to serve as the ultimate "pseudo-code–but-real-language" that is both succinct and transpilable to TypeScript. Designed from the ground up to be AI-friendly, AgentScript enforces strong best practices from contract programming, functional programming, concurrency, and beyond—ensuring that generated code is thoroughly tested and future-proof.


Table of Contents

  1. Introduction
  2. Why AgentScript?
  3. Core Concepts
  4. Embedded Testing and Contract Programming
  5. Functional and Concurrency Primitives
  6. Transpilation Flow
  7. Compared to Other Languages
  8. Practical AI-Driven Benefits
  9. Implementation Workflow
  10. Long-Term Vision

Introduction

Modern software development is dominated by AI-driven code generation. Tools like Cursor, WindSurf, Cline, and other coding agents can spin up new features in seconds, but they often struggle with maintainability, testing, and clarity. Existing languages—TypeScript included—remain too verbose, lacking a concise “intermediate” syntax that is:

  1. Immediately clear to both humans and AI.
  2. Equipped with robust best practices (like contract programming).
  3. Easily transpilable to stable languages (like TypeScript).

AgentScript was conceived as a direct response to these realities. By blending functional programming, test enforcement, and advanced concurrency constructs into a succinct code structure, AgentScript drastically increases the reliability of AI-generated output. The result: developers (human or AI) spend less time on boilerplate, produce fewer bugs, and achieve higher confidence in their code.


Why AgentScript?

  1. AI-First Design: Encourages minimal token usage, making code generation more cost-effective.
  2. Declarative Contracts: Integrates heavyweight constructs from contract programming to catch errors early.
  3. Embedded Testing: Bundles success, performance, concurrency, and smoke tests within the same file.
  4. Functional + Concurrency: Adopts functional paradigms and concurrency checks.
  5. Human-Readable: Emphasizes short syntax that is easy to parse for developers reading or diffing code.
  6. Transpilable: Produced code can go straight into TypeScript or modern JavaScript with minimal overhead.

AgentScript focuses on being an "intermediate pseudo-code language," bridging the gap between purely natural language specifications and production-ready TypeScript.


Core Concepts

  1. Minimal Syntax: Think of TypeScript stripped to its essentials, with added meta blocks for tests and context.
  2. Intent & Context: Every function uses short metadata blocks for AI clarity and forward-compatibility.
  3. Error Tags: Predefined in each function or module, ensuring clarity and easier debugging.
  4. State Machines: For advanced workflows, you can embed state machines directly, making concurrency and transitions explicit.
  5. Functional Patterns: Pipeline operators and pattern matching are front and center for succinct expression of complex logic.

Example

function fetchTodosByUser(userId: ID): TodoList
  goal: "Fetch a list of TODO items for a given user"
  backstory: "Used by the main dashboard to show tasks"
  requires:
    userId >= 0: "User ID cannot be negative"
  return: fetchFromDatabase("todos", userId)
  errors:
    UserNotFound: "No user found with the given ID"
  tests:
    success:
      1 -> [ { id: 1, task: "Buy milk" }, ... ]
    performance:
      execution: expect < 3ms
      memory: expect < 5mb

Here, the function includes an actual implementation for the return statement, ensuring it is more than just pseudo-code. AI agents or human developers can update it easily while preserving the constraints and embedded tests.

Purity Enforcement & Strict Types

AgentScript can be configured to enforce purity in function definitions, prohibiting side effects and mutable state. This ensures functional correctness and makes concurrency patterns more predictable. By default, functions may mutate local state, but stricter lint or compiler settings allow you to mark everything as immutable and side-effect-free.

Under this strict mode:

  • Functions must avoid global or shared mutable state.
  • Any attempts to reassign function parameters or mutate objects in place will produce warnings or errors.
  • Return types must avoid “any” and be explicitly defined, preventing silent type failures.

Biome Integration

Biome is an advanced code analysis and transformation tool that can be configured in its strictest setting to enforce:

  • No use of “any” or “unknown” types without explicit justifications.
  • No hidden side effects or ambiguous type declarations.
  • Automatic refactoring suggestions to maintain “pure function” hygiene.

By integrating Biome into the AgentScript toolchain, teams (human or AI) can ensure that lazy or incorrect type usage is caught early, vastly improving code reliability and maintainability.


Embedded Testing and Contract Programming

Contract Programming

AgentScript introduces requires clauses that define input constraints, reminiscent of contract programming. These constraints compile directly into runtime checks in the TypeScript output, guaranteeing that your code will fail fast if an invalid argument is passed.

function processPayment(amount: number, currency: string)
  requires:
    amount > 0: "Amount must be positive"
    currency != "XYZ": "Unsupported currency format"
  ...

Embedded Tests

Tests are written inline to ensure code correctness, with categories:

  • success: Traditional unit tests.
  • performance: Enforces execution time or memory usage constraints.
  • smoke: Simple checks for edge cases.
  • concurrency: Stress tests and race condition detection.

These blocks compile to Vitest or k6-friendly test suites, reducing friction when verifying code reliability.


Functional and Concurrency Primitives

  1. Pipe Operator (|>)
    Promote a functional style that is easy for LLMs to read, and ensures transformations are explicit.
    data |> parseJSON |> validate |> transformData
  2. Pattern Matching (inspired by EcmaScript proposals and functional languages)
    match (user.role) {
      case "admin" -> handleAdmin(user)
      case "guest" -> handleGuest(user)
    }
  3. Concurrency
    • machine blocks for embedded state machines (like XState).
    • parallel test configurations for concurrency stress tests.
    • Built-in concurrency patterns (e.g., spool, watchers, supervisors) to manage parallel tasks more safely.

Transpilation Flow

  1. AgentScript Source: Minimal syntax, embedded tests, contract requirements, and concurrency hints.
  2. Parser & Transpiler: A specialized parser reads AgentScript, verifying the embedded metadata.
  3. Output:
    • TypeScript for main logic.
    • Vitest test suites for unit and integration tests.
    • k6 scripts for performance and concurrency testing.
    • Optional advanced concurrency semantics mapped to XState or other libraries.

Compared to Other Languages

Feature TypeScript Rust AgentScript Reason to Pick AgentScript
Concurrency ⚠️ Library-based ⚠️ Rayon ✅ Effect-based concurrency Easier concurrency setup and management
Parallelism ✅ Promise.all ✅ Rayon ✅ Native parallel constructs Simplified parallel testing and operations
State Machines ⚠️ Library-based ⚠️ Library-based ✅ Native integration More explicit checks with state machines
Type Safety ⚠️ Can be strict ✅ High (built-in checks) AI can rely on stable types inside code & tests
Null Safety ✅ Strict null checks ✅ Option types ✅ High (checked) Minimizes runtime exceptions due to null references
Error Safety ❌ Manual or throw-based ✅ Built-in requires constraints Early error detection with clear constraints
Pipes ✅ Pipeline operator More expressive transformations
Pattern Matching ⚠️ Partial (match enums) ✅ Full (match blocks) More expressive branching (inspired by functional languages)
AI-Friendliness ⚠️ Medium ⚠️ Medium ✅ High Designed for minimal token usage
Testing ⚠️ External, optional ⚠️ Inline, optional (module tests) ✅ Inline, enforced Ensures AI maintains full test coverage
Contract Programming ⚠️ Weak (runtime/manual checks) ⚠️ Partial (via Result/Option) ✅ Built-in through requires Early error detection with clear constraints

Practical AI-Driven Benefits

  1. Less Token Overhead: Succinct syntax means large language models can parse, generate, and correct code with minimal token usage.
  2. Autonomous Updates: AI agents can automatically update or refactor code while preserving embedded tests and constraints.
  3. Reduced Boilerplate: Built-in best practices mean fewer lines of scaffolding or repeated patterns.
  4. Transparent Intent: The intent and context fields let the AI keep track of the function’s purpose, preventing accidental misuse.

Implementation Workflow

  1. Define Requirements

    • Outline each function’s intent, context, and requires for clarity.
  2. Write Skeleton

    • Create initial AgentScript code with embedded tests and concurrency hints.
  3. Transpile to TypeScript

    • Use the AgentScript transpiler to produce TypeScript with test and concurrency scaffolding.
  4. Run Tests

    • Let Vitest and k6 run the inline tests to catch errors.
  5. Refine & Iterate

    • Tweak the AgentScript code or let an AI agent further refine constraints and concurrency patterns.

Long-Term Vision

We see AgentScript as the new baseline for how AI-driven development can produce stable, maintainable code at scale. By standardizing on robust patterns, embedded testing, concurrency checks, and contract programming, we aim to:

  • Elevate AI Productivity: Provide a structured yet flexible environment that suits LLM generation.
  • Set Industry Standards: Expand best practices for concurrency, functional patterns, and contract definitions.
  • Spark Evolution: Inspire TypeScript and other languages to adopt more explicit, embedded contracts and tests.

AgentScript is more than a language; it’s a commitment to pushing AI-driven software to new levels of reliability, readability, and efficiency.


Why TypeScript as the Primary Target?

TypeScript is among the most popular programming languages worldwide, enjoying rapid growth in community support and language evolution. Its type system provides strong guarantees while remaining flexible enough for nearly any environment or runtime—browsers, servers, serverless platforms, mobile apps, and even Infrastructure as Code (IaC). By transpiling AgentScript to TypeScript:

  1. You benefit from TypeScript’s thriving ecosystem of libraries and tooling.
  2. Deployment becomes considerably simpler, since the same TypeScript output can run on Node.js, Cloudflare, React Native, or the browser.
  3. The language updates regularly, which means AgentScript’s emitted TypeScript can stay current with evolving JavaScript and type system features.

Targeting TypeScript ensures you can deploy AgentScript-based code anywhere, with minimal overhead and maximum future compatibility.


About

AgentScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published