⭐ 一等公民
TLL 的核心设计哲学是AI-Native。Agent、Intent、Tool、Entity、API、Application 都是语言级一等公民,而非库调用。本章介绍这些独特设计。
1. Entity(数据模型)
Entity 是 TLL 的数据模型抽象,编译器自动生成数据库迁移、仓库和类型安全查询构建器。
entity User {
id: int = primary_key(auto_increment)
name: str
email: str = unique
password_hash: str
created_at: datetime = default(now)
updated_at: datetime = default(now, on_update: now)
}
entity Product {
id: int = primary_key(auto_increment)
name: str
price: float
stock: int = 0
category: str = index
}
Entity 特性:
- 数据库无关 — 支持 PostgreSQL、MySQL、SQLite
- 自动迁移 — 编译器根据 Entity 定义生成迁移脚本
- 类型安全查询 — 编译时检查字段名和类型
- 自动时间戳 —
created_at/updated_at自动管理
2. API(端点定义)
API 声明式定义 HTTP 端点,无需手写路由注册。
api UserApi {
base_path = "/api/users"
GET "/" -> List[User] {
description = "List all users"
handler = list_users
}
GET "/{id}" -> User {
description = "Get user by ID"
handler = get_user
}
POST "/" -> User {
description = "Create a new user"
body = CreateUserRequest
handler = create_user
}
PUT "/{id}" -> User {
description = "Update user"
body = UpdateUserRequest
handler = update_user
}
DELETE "/{id}" -> void {
description = "Delete user"
handler = delete_user
}
}
支持的 HTTP 方法:GET POST PUT PATCH DELETE HEAD OPTIONS
3. Agent(AI 代理)
Agent 是 TLL AI-Native 的核心。Agent 不是库,而是语言级构造。
agent Researcher {
name = "Researcher"
description = "Searches the web and summarizes findings"
tools = [web_search, web_fetch, summarize]
system = "You are a research assistant. Always cite sources."
config = {
model = "default",
temperature = 0.3,
max_tokens = 2048,
}
}
使用 Agent
fn main() {
let researcher = Researcher.new()
// 发送消息
let response = await researcher.send("What is TLL?")
io.println(response.text)
// 流式输出
let stream = agent.stream("Write a poem")
for await chunk in stream {
io.print(chunk)
}
}
Agent 组合
agent Manager {
name = "Manager"
tools = [delegate_to_researcher, delegate_to_coder]
system = "You coordinate a team of specialists."
}
fn delegate_to_researcher(task: str) -> str {
let researcher = Researcher.new()
return await researcher.send(task)
}
Agent 记忆
agent Assistant {
name = "Assistant"
// 短期记忆(最多 100 条)
memory = Memory.short_term(max_items: 100)
// 或持久化记忆
// memory = Memory.persistent(path: "./agent-memory")
}
4. Tool(工具定义)
Tool 是 Agent 可以调用的函数,自动生成 JSON Schema 供 LLM function calling。
tool web_search(query: str) -> List[SearchResult] {
description = "Search the web for the given query"
// 实现:调用搜索 API
}
tool web_fetch(url: str) -> str {
description = "Fetch the content of a web page"
// 实现
}
tool calculate(expression: str) -> float {
description = "Calculate a mathematical expression"
// 实现
}
5. Intent(意图)
Intent 是可以分派给 Agent 的高级目标。
intent Research(query: str) -> Report {
description = "Research a topic and produce a report"
agent = Researcher
}
// 使用 Intent
fn main() {
let report = await Research("TLL language features")
io.println(report.summary)
}
6. Application(服务容器)
Application 是 TLL 内置的应用框架,用于构建服务、API 和全栈应用。
application MyApp {
name = "My Application"
version = "0.1.0"
modules = [UserApi, ProductApi, AuthModule]
middleware = [Logger, Cors, AuthGuard]
config = {
port = 8080,
database = "postgres://localhost/mydb",
env = "development",
}
}
fn main() {
MyApp.run()
}
7. 完整示例:电商应用
application Shop {
name = "Shop"
version = "0.1.0"
modules = [ProductApi, OrderApi, ShopAgent]
middleware = [Logger, Cors]
config = { port = 8080 }
}
entity Product {
id: int = primary_key(auto_increment)
name: str
price: float
stock: int = 0
}
api ProductApi {
base_path = "/api/products"
GET "/" -> List[Product] { handler = list_products }
GET "/{id}" -> Product { handler = get_product }
POST "/" -> Product { body = CreateProduct, handler = create_product }
}
agent ShopAssistant {
name = "Shop Assistant"
tools = [search_products, recommend_products]
system = "You help customers find products."
}
fn main() {
Shop.run()
}