AutolangDocs

Examples

Concrete examples showing how to wire Autolang into real scenarios โ€” database access, AI agents, and more.

These examples assume you've already read Native Libraries. The patterns used here build directly on what's covered there.

๐Ÿ“Š Fluent Query Builder

This example shows how to wrap a JavaScript query builder (like Knex or a custom ORM) using @js_object, so Autolang scripts can chain method calls in a type-safe way while the native object stays on the host side.

import { ACompiler } from 'autolang-compiler'; const compiler = await ACompiler.create(); compiler.registerBuiltInLibrary("store/database", ` class Customer ( val id: Int, val name: String, val level: String ) class Order ( val orderId: Int, val customerId: Int, val item: String, val amount: Float, val status: String ) @js_object class QueryBuilder { @native("query_where") fun where(field: String, value: String): QueryBuilder @native("query_and_where") fun andWhere(field: String, value: String): QueryBuilder @native("query_order_by") fun orderBy(field: String, direction: String): QueryBuilder @native("query_limit") fun limit(count: Int): QueryBuilder @native("query_exec") fun execute(): Array<Order> } class Database { @native("db_get_customers") static fun getCustomers(): Array<Customer> @native("db_create_query") static fun createQuery(): QueryBuilder } `, { autoImport: true }, { db_get_customers() { return [ { id: 1, name: "Alice", level: "VIP" }, { id: 2, name: "Bob", level: "Standard" }, { id: 3, name: "Charlie", level: "VIP" } ]; }, db_create_query() { return new JSQueryBuilder(); }, query_where(field, value) { return this.where(field, value); }, query_and_where(field, value) { return this.andWhere(field, value); }, query_order_by(field, direction) { return this.orderBy(field, direction); }, query_limit(count) { return this.limit(count); }, query_exec() { return this.execute(); } });

This pattern uses @js_object โ€” see Native Libraries โ†’ Wrapping JavaScript Objects for a full explanation.

๐Ÿค– AI Agent Interaction

โ„น๏ธ
Setup used in this exampleModel: gemini-2.5-flash. The entire autolang-ai-reference.md was provided as context, along with the following system prompt:
You are an AI assistant with access to the store database. Use @import("store/database") to query customers and orders. Write a script to process and output what the user requested.
The schema for Database, Customer, Order, and QueryBuilder was appended after the AI Reference.

Below are actual prompts and the scripts the agent generated, using the store/database library from the example above.

Show me the top 2 highest completed orders for Alice, sorted by amount in descending order.
AI
AGENT
@import("store/database") val customers = Database.getCustomers() val alice = customers.filter {|c| c.name == "Alice" }[0] ?? null if (alice == null) { println("Customer Alice not found.") } else { val orders = Database.createQuery() .where("customerId", alice.id.toString()) .andWhere("status", "completed") .orderBy("amount", "desc") .limit(2) .execute() println("Alice's top 2 completed orders:") orders.forEach {|o| println("- Order #" + o.orderId + ": " + o.item + " ($" + o.amount + ")") } }
I'm filtering for Alice in the customer list first, then building a query that chains .where(), .andWhere(), .orderBy(), and .limit() to get only her top 2 completed orders by amount. The result is printed via println() inside the script.
Get the total amount of all pending orders for Bob.
AI
AGENT
@import("store/database") val customers = Database.getCustomers() val bob = customers.filter {|c| c.name == "Bob" }[0] ?? null if (bob == null) { println("Customer Bob not found.") } else { val pendingOrders = Database.createQuery() .where("customerId", bob.id.toString()) .andWhere("status", "pending") .execute() var total = 0.0 pendingOrders.forEach {|o| total += o.amount } println("Bob's total pending: $" + total) }
I'm fetching Bob's pending orders via .where("status", "pending"), then summing the amount field in a loop. The total is printed at the end.