Examples
Concrete examples showing how to wire Autolang into real scenarios - business services, 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 Order Filter Interface
This example shows how to wrap a host-managed filter service using @js_object, so Autolang scripts can chain method calls in a type-safe way while the underlying host object stays on the host side.
import { ACompiler } from 'autolang-compiler';
const compiler = await ACompiler.create();
compiler.registerBuiltInLibrary("services/orders", `
class Customer (
val id: Int,
val name: String,
val tier: String
)
class Order (
val orderId: Int,
val customerId: Int,
val item: String,
val amount: Float,
val status: String
)
@js_object
class OrderFilter {
@native("filter_where")
fun where(field: String, value: String): OrderFilter
@native("filter_and_where")
fun andWhere(field: String, value: String): OrderFilter
@native("filter_order_by")
fun orderBy(field: String, direction: String): OrderFilter
@native("filter_limit")
fun limit(count: Int): OrderFilter
@native("filter_exec")
fun execute(): Array<Order>
}
class OrderService {
@native("service_get_customers")
static fun getCustomers(): Array<Customer>
@native("service_create_filter")
static fun createFilter(): OrderFilter
}
`, { autoImport: true }, {
service_get_customers() {
return [
{ id: 1, name: "Alice", tier: "VIP" },
{ id: 2, name: "Bob", tier: "Standard" },
{ id: 3, name: "Charlie", tier: "VIP" }
];
},
service_create_filter() {
return new HostOrderFilter();
},
filter_where(field, value) { return this.where(field, value); },
filter_and_where(field, value) { return this.andWhere(field, value); },
filter_order_by(field, direction) { return this.orderBy(field, direction); },
filter_limit(count) { return this.limit(count); },
filter_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: The schema for
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 orchestrating the sales orders service.
Use @import("services/orders") to query customers and orders.
Write a script to process and output what the user requested.OrderService, Customer, Order, and OrderFilter was appended after the AI Reference.Below are actual prompts and the scripts the agent generated, using the services/orders library from the example above.
Show me the top 2 highest completed orders for Alice, sorted by amount in descending order.
AI
AGENT@import("services/orders")
val customers = OrderService.getCustomers()
val alice = customers.filter {|c| c.name == "Alice" }[0] ?? null
if (alice == null) {
println("Customer Alice not found.")
} else {
val orders = OrderService.createFilter()
.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 filter chain with .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("services/orders")
val customers = OrderService.getCustomers()
val bob = customers.filter {|c| c.name == "Bob" }[0] ?? null
if (bob == null) {
println("Customer Bob not found.")
} else {
val pendingOrders = OrderService.createFilter()
.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.