Bundle / foldkit-bundle-surface

bundle-surface

Reusable foldkit-bundle widgets read and update a slice of your parent Model through update. Without registering those placements in foldkit-surface, Module.validate cannot catch two widgets colliding on the same Model path or overwriting a field already owned by foldkit-sync or foldkit-remote.

foldkit-bundle-surface generates a Surface ownership contract for each Bundle placement (bundle:Search@search) so Module.validate checks every placement, Sync projection, and Remote store together.

Install

bash
pnpm add foldkit-bundle-surface foldkit-bundle foldkit-surface effect foldkit

A first validated module

Wrap your Surface.application with BundleSurface.parent, place a Bundle, and pass the placements to Page.module before calling Module.validate:

ts
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Bundle } from 'foldkit-bundle'
import { BundleSurface } from 'foldkit-bundle-surface'
import { Module, Surface } from 'foldkit-surface'

const Search = Bundle.make('Search', {
  Model: Schema.Struct({ query: Schema.String }),
  Message: defineMessageUnion({ Typed: { query: Schema.String } }),
  init: () => ({ model: { query: '' } }),
  update: (_model, message) => ({ model: { query: message.query } }),
})
const Searchbox = Bundle.declare(Search, 'search')
const Model = Schema.Struct({ ...Searchbox.fields })
const Message = defineMessageUnion({ ...Searchbox.cases })
const App = Surface.application({ Model, Message })
const Page = BundleSurface.parent(App)
const placements = Page.assemble(Page.at(Searchbox))

const AppModule = Page.module(placements, [])
Module.validate(AppModule) // []

AppModule now records bundle:Search@search as the sole owner of search, and Module.validate(AppModule) returns [] (no conflicts) purely from inspecting declarations. Rendering a Surface over search observes that single owner, and Module.manifest and Module.toMarkdown include every placed Bundle.

Available helpers:

  • BundleSurface.parent(app): Creates a Bundle.parent bound to app with .module(assembly, items).
  • Page.module(assembly, items) / BundleSurface.module(app, assembly, items): Converts each placement into a contract, keeps any wiring contracts, and appends your other Module items.
  • BundleSurface.contract(app, placement): Builds a single contract for Module.make or Module.add, recording the owned path, parent Message tags, and args metadata when the Bundle declares args.
  • BundleSurface.link(fieldRef, wrapper): Builds a Link over a foldkit-surface field ref for custom placements.

What Module.validate catches

MistakeFinding
Two placements or a placement + Sync/Remote on one pathownership-overlap
Placement using another application's field refforeign-contract
Wrapper Message missing from the parent Message unionunknown-message
Link path absent from the parent Modelunknown-path