Lets agents in the browser read the page and act through the same controls people use.
Invoice PDF shows last month's address
Halcyon Health · Open
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"import { callTool } from "./agent.ts"import { SupportTicket } from "./ticket.ts"const ALLOW = ["SupportTicket.PickedStatus"]const Message = UiSchema.TaggedEnum({ ClickedRead: {}, ClickedAct: {}, CompletedTool: { title: Schema.String, json: Schema.String },})const Read = Command.make("ReadTicketAsAgent", {}, () => callTool("page_snapshot", {}, ALLOW).pipe( Effect.map((result) => { const all = (result as { readonly components?: ReadonlyArray<{ readonly id: string }> }).components ?? [] const mine = all.find((c) => c.id === "ticket-read-act") ?? null return Message.CompletedTool({ title: "page_snapshot", json: JSON.stringify(mine, null, 2) }) }), ),)const Act = Command.make("SolveTicketAsAgent", {}, () => callTool("page_act", { steps: [{ id: "ticket-read-act", action: "PickedStatus", input: { status: "Solved" } }] }, ALLOW).pipe( Effect.map((result) => Message.CompletedTool({ title: "page_act", json: JSON.stringify(result, null, 2) })), ),)const styles = stylex.create({ frame: { display: "grid", gap: space.lg, inlineSize: "min(100%, 34rem)" }, row: { display: "flex", flexWrap: "wrap", gap: space.sm }, output: { margin: 0, maxBlockSize: "16rem", overflow: "auto", padding: space.md, borderRadius: radius.md, backgroundColor: color.raised, color: color.fg, fontFamily: font.mono, fontSize: font.xs, whiteSpace: "pre-wrap", overflowWrap: "anywhere", }, label: { margin: 0, fontSize: font.sm, color: color.muted },})export const ReadAndAct = Html.component({ name: "ReadAndAct", model: Schema.Struct({ title: Schema.String, json: Schema.String }), message: Message,}).make({ init: () => ({ title: "", json: "" }), update: { ClickedRead: ($) => $.evolve({}).run(Read.make({})), ClickedAct: ($) => $.evolve({}).run(Act.make({})), CompletedTool: ($, result) => $.evolve(result), }, view: (h, model) => h.div.attrs(sx(styles.frame)).children( SupportTicket(h, { props: { agentId: "ticket-read-act" } }), h.div.attrs(sx(styles.row)).children( Button(h, {}).on("click", Message.ClickedRead()).children("Read it as an agent"), Button(h, {}).on("click", Message.ClickedAct()).children("Mark it solved as an agent"), ), model.title !== "" && h.div.attrs({ role: "status", ...sx(styles.frame) }).children( h.p.attrs(sx(styles.label)).children(`What ${model.title} returned`), h.pre.attrs({ tabindex: "0", ...sx(styles.output) }).children(model.json), ), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/agent-toolsUsage
import * as AgentConfirm from "./ui/uicontext/components/agent/AgentConfirm.ts"import * as AgentPage from "./ui/uicontext/components/agent/AgentPage.ts"const allow: AgentPage.AllowList = { actions: [ "SupportTicket.PickedStatus", // agents may change a ticket's status "Inbox", // and read the inbox, without acting on it ],}const root = document.getElementById("app")!BrowserHost.mount(SupportTicket, { agentId: "ticket-4821" }, root).pipe( Layer.provide(AgentPage.layer({ allow })), Layer.provide(AgentConfirm.layer()), Layer.launch, Effect.runFork,)Examples
What agents can't do
Agents get a plain error, never an exception: an action the allow list leaves out, an input that doesn't fit, a change that changes nothing.
Invoice PDF shows last month's address
Halcyon Health · Open
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"import { callTool, errorOf } from "./agent.ts"import { SupportTicket } from "./ticket.ts"// The app allows status changes only: deleting stays with people.const ALLOW = ["SupportTicket.PickedStatus"]const ID = "ticket-refused"const Attempt = Schema.Literals(["delete", "bad-input", "no-change"])const Message = UiSchema.TaggedEnum({ ClickedTry: { attempt: Attempt }, CompletedTry: { attempt: Attempt, error: Schema.String },})const steps = { delete: { id: ID, action: "ClickedDelete", target: { subject: "Invoice PDF shows last month's address" } }, "bad-input": { id: ID, action: "PickedStatus", input: { status: "Escalated" } }, "no-change": { id: ID, action: "PickedStatus", input: { status: "Open" } },} as constconst Try = Command.make("TryAsAgent", { args: { attempt: Attempt } }, ({ attempt }) => callTool("page_act", { steps: [steps[attempt]] }, ALLOW).pipe( Effect.map((result) => Message.CompletedTry({ attempt, error: errorOf(result) ?? "It ran." })), ),)const labels = { delete: "Delete the ticket", "bad-input": "Set status to “Escalated”", "no-change": "Set status to “Open” again",} as constconst styles = stylex.create({ frame: { display: "grid", gap: space.lg, inlineSize: "min(100%, 34rem)" }, row: { display: "flex", flexWrap: "wrap", gap: space.sm }, error: { margin: 0, fontSize: font.sm, color: color.muted, overflowWrap: "anywhere" }, strong: { color: color.fg, fontWeight: 500 },})export const RefusedSteps = Html.component({ name: "RefusedSteps", model: Schema.Struct({ attempt: Schema.NullOr(Attempt), error: Schema.String }), message: Message,}).make({ init: () => ({ attempt: null, error: "" }), update: { ClickedTry: ($, { attempt }) => $.evolve({ attempt, error: "" }).run(Try.make({ attempt })), CompletedTry: ($, { attempt, error }) => $.evolve({ attempt, error }), }, view: (h, model) => h.div.attrs(sx(styles.frame)).children( SupportTicket(h, { props: { agentId: ID } }), h.div .attrs(sx(styles.row)) .children( ...Attempt.literals.map((attempt) => Button(h, { size: "sm" }) .key(attempt) .on("click", Message.ClickedTry({ attempt })) .children(labels[attempt]), ), ), h.p .attrs({ role: "status", ...sx(styles.error) }) .children( model.attempt !== null && model.error !== "" && [h.span.attrs(sx(styles.strong)).children(`${labels[model.attempt]}: `), model.error], ), ),})Wiring it to your app
Give each component agents can use a stable ID, and list the actions they may take. Agents find actions with page_snapshot, read their inputs with page_query, then call page_act.
Mount one AgentConfirm dialog, with its policy, below the page layer. A task that must confirm its target takes the action's input and the target it saw separately. Pass an AbortSignal to cancel a pending tool call.
View a complete app example
import "@w3c/webmcp-polyfill/auto"import { BrowserHost, Html } from "@effect/ui/html"import { Effect, Layer, Schema } from "effect"import { Notes } from "./notes.ts"import { SaveWorkflow, Workflows } from "./workflows.ts"import { AgentConfirm, layer as confirmLayer } from "./ui/uicontext/components/agent/AgentConfirm.ts"import { layer as pageLayer, type TaskTool } from "./ui/uicontext/components/agent/AgentPage.ts"import "./base.css"const App = Html.component({ name: "ContractExamples", model: Schema.Null, message: Schema.Never }).make({ init: () => null, update: {}, view: (h) => h.div.attr("class", "contract-examples").children( h.h1.children("Controlled interfaces"), Notes(h, {}), Workflows(h, {}), AgentConfirm(h, {}), ),})const archiveSelection: TaskTool = { name: "archive_selection", title: "Archive selected workflows", description: "Archive exactly the selected workflow rows after a person reviews the selection.", target: { id: "workflows", action: "ClickedAction" }, input: Schema.Struct({ action: Schema.Literal("archive") }), targetAcknowledgement: "explicit", consequential: true,}const root = document.querySelector("main")if (root === null) throw new Error("Missing example root.")const main = BrowserHost.mount(App, {}, root).pipe( Layer.provide(SaveWorkflow.layer), Layer.provide(pageLayer({ allow: { actions: ["Table.CommittedEdit", "Table.RequestedSelection"], tasks: [archiveSelection.name], }, tasks: [archiveSelection], })), Layer.provide(confirmLayer()),)Effect.runFork(Layer.launch(main))entry.ts on GitHubAccessibility
- Agents in the browser get three tools:
page_snapshotlists what's on the page and the actions they may take,page_querylists a component's choices, andpage_actacts as a person would. - Nothing is allowed until the app's allow list names it, and every action goes through the same controls and checks a person's click does.
- A consequential action names the exact state it applies to, and a person confirms it before it runs.