Surface / foldkit-surface
surface
When a Foldkit app grows—one Model holding everything and one update handling every Message—different parts of your app still need clear boundaries. A screen should declare which fields it reads, an AI agent or page block should only be able to send specific Messages, and a sync or data-fetching layer needs to know what data a view depends on without running the UI or duplicating TypeScript interfaces by hand.
foldkit-surface derives typed field references, read projections, and Message subsets directly from your existing Model and Message Schemas, and pairs them into a named contract called a Surface. Because a Surface is inspectable data tied to your Model Schema, it cannot name a field your Model lacks, and packages such as foldkit-remote, foldkit-sync, and foldkit-agent can inspect what a feature reads and may trigger without running anything.
See five feature Surfaces and a Module wired together in examples/todo-app, or read Server-derived state.
How a Surface reads state and sends Messages
A Surface is built in three steps from your Model Schema: Surface.application generates typed field references from the Schema, a Projection selects the fields a feature reads, and a named Surface pairs that Projection with the Messages the feature may send. At run time, Surface.read extracts the feature's input purely from the Model, while dispatched Messages flow through your update to produce the next Model.
Listing Messages on a Surface constrains what consumers (such as an agent) may send; your update remains the single place that defines what those Messages do. If nothing needs to inspect a feature's data or Message dependencies, a plain function over your Model is simpler.
Install
pnpm add foldkit-surfacefoldkit and effect are peer dependencies.
A first Surface
This defines a todo app, pairs the todos and selectedTodoId fields with two allowed Messages into a TodoList Surface, and reads that slice from a sample Model:
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { ModelRef, Projection, Surface } from 'foldkit-surface'
const Todo = Schema.Struct({ id: Schema.String, title: Schema.String, done: Schema.Boolean })
const Model = Schema.Struct({
todos: Schema.Array(Todo),
todosById: Schema.Record(Schema.String, Todo),
selectedTodoId: Schema.Option(Schema.String),
})
const Message = defineMessageUnion({
CreatedTodo: { id: Schema.String, title: Schema.String },
ToggledTodo: { id: Schema.String },
SelectedTodo: { id: Schema.String },
})
const App = Surface.application({ Model, Message })
const TodoList = App.surface('TodoList', {
model: ({ model }) => ({ todos: model.todos, selectedTodoId: model.selectedTodoId }),
messages: [Message.ToggledTodo, Message.SelectedTodo],
})
const model: typeof Model.Type = {
todos: [{ id: 't1', title: 'Read the guide', done: false }],
todosById: {},
selectedTodoId: Option.some('t1'),
}
Surface.read(TodoList, model)
// { todos: [{ id: 't1', title: 'Read the guide', done: false }], selectedTodoId: Option.some('t1') }What happened:
TodoListbinds a projection oftodosandselectedTodoIdto[Message.ToggledTodo, Message.SelectedTodo], excludingCreatedTodo.Surface.read(TodoList, model)returns only{ todos, selectedTodoId }, omittingtodosById.Surface.view(TodoList, ...)binds a renderer to that slice, whilefoldkit-remote,foldkit-sync, andfoldkit-agentinspectTodoListto derive fetches, replication, and agent tools.
(todosById in Model is used by the dynamic record-lookup examples below; in a real application, keep a single authoritative collection.)