Appearance
User-editable apps
An app can let its users, or an AI working for them, write part of it: their own pages and components, or functions the app calls. Their code is Weft, checked when it's saved and run in a sandbox with only what the app hands it.
main.weft
weft
import { memory, run } from "std/sandbox"
import { log } from "std/log"
let trees = memory()
try await trees.save("anna", {
files: [{ id: "main", source: "export fn main(caps: { add: (a: number, b: number) => number }, input: json): json {\n return caps.add(1, 2)\n}\n" }],
})
let three = try await run(trees.tree("anna"), { caps: { add: (a: number, b: number) => a + b } })
log(three)text
$ weft run main.weft
3save checks and compiles Anna's code before it keeps it. run calls her main in a VM of its own, with its own memory and limits, and gives it caps: the one function the app chose to share. Values are copies, so nothing either side holds can be changed by the other. Everything a run gets is in its options, a plain record: imports (the app's own components and functions it may use, below), caps, input, limits, and which function to call (file and call, main by default). run also takes code as text, for code that isn't kept: an agent's, a formula's.
What user code can't do
User code imports no I/O module (std/http, std/db, std/s3, std/process), and nothing of the app that its run doesn't import. Saving this page fails:
text
error[E1017]: sandboxed code can't import `std/db`: it reaches only what the app gives it (`std/sandbox`)
--> home.weft:1:24
|
1 | import { sqlite } from "std/db"
| ^^^^^^^^save fails with the TreeError Problems { problems }, which lists them, and the last good version stays. weft check --sandbox <files> checks code the same way. A turn of user code gets a tenth of a program's fuel, memory, stack and time by default, and running out stops it: run fails with the RunError Stopped { limit, message }.
Pages users write
A user tree is a manifest and its files. app.serve takes the trees to serve, first one first, for each request:
main.weft
weft
import { app } from "std/app"
import { session } from "std/http"
import type { Request } from "std/http"
import { dir } from "std/sandbox"
import Card from "components/ui/Card"
let trees = dir("trees")
try await app.serve(async (req: Request) => {
let user = try await session(req).user()
return [user != null ? trees.tree(user.id) : null, "./routes"]
}, {
sandbox: async (req: Request) => ({ imports: { Card }, caps: { user: (try await session(req).user())?.name ?? "" } }),
})A signed-in user's tree is served first; every path it doesn't have falls through to the app's routes/. For each request sandbox gives the page the same record run takes. In imports, the label is the name the user's code sees and the value is the app's own: { Card: FancyCard, money: cash } gives users <Card> and import { money } from "app", and you can rename FancyCard or cash whenever you like without touching their files. caps becomes the page's caps prop:
home.weft
weft
prop caps: json
---
<Box class="flex flex-col gap-2 p-6">
<Text class="text-2xl font-semibold">Hello from {caps.user}'s own page</Text>
<Text>Written by its user, checked when it was saved.</Text>
</Box>What every run imports together is the app's API for user code: weft api prints it, and changing it is what migrations and deprecations are for. A file using something its run doesn't import is served as the app's own (or not at all, for a page of the user's), as when an export it used is past its deprecation.
Trees live where the app keeps them: memory(), a folder with dir(path), or an S3 bucket's trees (std/s3). A folder and a bucket share one layout, so a tree can move between a device and a server as it is. The user-editable pages recipe builds the editing page too.
The edit API
The editor (a design mode and an IDE, written in Weft and overlaid on any running app) and the MCP server are designed but not built yet. Both will change apps through one checked edit API in Rust, so a user's edits, an AI's and a developer's are checked the same way. The whole of std/sandbox: the reference.