Write and send a message, with attachments and modes.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import type { Actor } from "./ui/uicontext/components/domain/Actor.ts"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { Composer } from "./ui/uicontext/components/kit/Composer.ts"import { Thread, ThreadMessage } from "./ui/uicontext/components/kit/Thread.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ frame: { display: "grid", gap: 20, inlineSize: "min(100%, 36rem)" },})const me: Actor = { id: "ada", name: "Ada Lovelace", kind: "person" }const scout: Actor = { id: "scout", name: "Scout", kind: "agent" }// who "@" offers: the channel's members and its agentconst members: ReadonlyArray<Actor> = [ scout, { id: "ben", name: "Ben Okafor", kind: "person" }, { id: "mira", name: "Mira Patel", kind: "person" }, me,]const Message = UiSchema.TaggedEnum({ SentMessage: { text: Schema.String, files: Schema.Array(Schema.Struct({ id: Schema.String, name: Schema.String, size: Schema.Number, type: Schema.String })), request: Schema.optional(Schema.String), }, RequestedStopScout: {},})export const AskAgent = Html.component({ name: "AskAgent", model: Schema.Struct({ sent: Schema.Array(ThreadMessage), outcome: Schema.NullOr(Outcome), running: Schema.Boolean }), message: Message,}).make({ // Scout is still working on the last request, so the send button starts as Stop init: () => ({ sent: [], outcome: null, running: true }), update: { // The app accepts the message: the composer clears its draft once `outcome` names the request SentMessage: ($, { text, files, request }) => $.evolve({ // a real app stamps the time where it stores the message; the example counts minutes sent: (sent) => [ ...sent, { id: `m${sent.length + 1}`, actor: me, at: `2026-12-01T09:${String(40 + sent.length).padStart(2, "0")}:00Z`, body: text, // the files sent with it show as chips; a real app links each to where it uploaded it attachments: files, }, ], outcome: request === undefined ? null : { _tag: "Accepted", request }, }), RequestedStopScout: ($) => $.evolve({ running: false }), }, view: (h, { sent, outcome, running }) => h.div.attrs(sx(styles.frame)).children( sent.length > 0 && Thread(h, { messages: sent, me: me.id, label: "Sent messages" }), Composer(h, { props: { label: "Message Scout", placeholder: "Ask Scout to triage, draft or look something up… @ to mention", modes: [ { id: "agent", label: "Agent" }, { id: "ask", label: "Ask" }, ], attachments: true, mentionables: members, running, outcome, }, onEmit: { // `mentions` lists who the message names, to notify them or start the agent Sent: ({ text, files, request }) => Message.SentMessage({ text, files: (files ?? []).map(({ id, name, size, type }) => ({ id, name, size, type })), request }), Attached: () => undefined, RequestedStop: () => Message.RequestedStopScout(), RequestedEditLast: () => undefined, Drafted: () => undefined, }, }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/composerUsage
import type { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { Composer } from "./ui/uicontext/components/kit/Composer.ts"const Message = UiSchema.TaggedEnum({ SentMessage: { text: Schema.String, request: Schema.optional(Schema.String) } })// `outcome` is your app's answer to the last send: Accepted clears the draft, Refused keeps itexport const MessageBox = (h: Html.H<typeof Message.Type>, outcome: Outcome | null) => Composer(h, { props: { label: "Message Scout", placeholder: "Ask anything…", outcome }, onEmit: { Sent: ({ text, request }) => Message.SentMessage({ text, request }), Attached: () => undefined, RequestedStop: () => undefined, RequestedEditLast: () => undefined, Drafted: () => undefined, }, })Examples
Refused and unavailable
Send something in the first field: the app refuses and the draft stays. The second can't send at all, and says why.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { Composer } from "./ui/uicontext/components/kit/Composer.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ stack: { display: "grid", gap: 20, inlineSize: "min(100%, 34rem)" },})const Message = UiSchema.TaggedEnum({ SentToArchived: { request: Schema.optional(Schema.String) },})export const RefusedComposer = Html.component({ name: "RefusedComposer", model: Schema.Struct({ outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ outcome: null }), update: { // the app refuses: the draft stays, with the reason under it SentToArchived: ($, { request }) => request === undefined ? $.model : $.evolve({ outcome: { _tag: "Refused", request, reason: "This channel was archived. Unarchive it to post." } }), }, view: (h, { outcome }) => h.div.attrs(sx(styles.stack)).children( Composer(h, { key: "archived", props: { agentId: "composer:refused", label: "Message #launch-review", placeholder: "Write a message and press Enter", outcome }, onEmit: { Sent: ({ request }) => Message.SentToArchived({ request }), Attached: () => undefined, RequestedStop: () => undefined, RequestedEditLast: () => undefined, Drafted: () => undefined, }, }), // a limit known in advance disables sending and says why Composer(h, { key: "limit", props: { agentId: "composer:limit", label: "Message Scout", placeholder: "Ask Scout…", disabledReason: "You've used this week's 500 agent messages. They reset on Monday." }, onEmit: { Sent: () => undefined, Attached: () => undefined, RequestedStop: () => undefined, RequestedEditLast: () => undefined, Drafted: () => undefined }, }), ),})Slash commands, files and a busy agent
Type / for commands and @ to mention. Paste, drop or pick files. While the agent works, a message waits in the queue and Stop takes Send's place.
Forge is free.
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import type { Actor } from "./ui/uicontext/components/domain/Actor.ts"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { Composer, ComposerFile } from "./ui/uicontext/components/kit/Composer.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const forge: Actor = { id: "forge", name: "Forge", kind: "agent" }const grace: Actor = { id: "grace", name: "Grace Hopper", kind: "person" }const Sent = Schema.Struct({ text: Schema.String, command: Schema.NullOr(Schema.String), files: Schema.Array(ComposerFile), queued: Schema.Boolean,})const Message = UiSchema.TaggedEnum({ SentComposer: { sent: Sent, request: Schema.optional(Schema.String) }, RequestedStopComposer: {}, CompletedRun: {},})// Stands in for the agent working on a message: four seconds, then it's free again.const Work = Command.make("WorkOnMessage", {}, () => Effect.sleep("4 seconds").pipe(Effect.as(Message.CompletedRun())))const styles = stylex.create({ frame: { display: "grid", gap: space.md, inlineSize: "min(100%, 36rem)" }, log: { margin: 0, paddingInlineStart: space.xl, fontSize: font.sm, color: color.muted }, strong: { color: color.fg },})export const CodingComposer = Html.component({ name: "CodingComposer", model: Schema.Struct({ sent: Schema.Array(Sent), running: Schema.Boolean, outcome: Schema.NullOr(Outcome) }), message: Message,}).make({ init: () => ({ sent: [], running: false, outcome: null }), update: { // a queued message waits for the run to end; anything else starts one SentComposer: ($, { sent, request }) => $.evolve({ sent: (all) => [...all, sent], running: true, outcome: request === undefined ? null : { _tag: "Accepted", request }, }).run(...(sent.queued ? [] : [Work.make({})])), RequestedStopComposer: ($) => $.evolve({ running: false }), CompletedRun: ($) => $.evolve({ running: false }), }, view: (h, model) => h.div.attrs(sx(styles.frame)).children( model.sent.length > 0 && h.ol.attrs({ "aria-label": "Sent", ...sx(styles.log) }).children( ...model.sent.map((s, i) => h.li.key(String(i)).children( h.span.attrs(sx(styles.strong)).children(s.command === null ? "Message" : `/${s.command}`), ` ${s.text.replace(/^\/\S+\s*/u, "") === "" ? "" : `“${s.text.replace(/^\/\S+\s*/u, "")}”`}`, s.files.length > 0 ? ` with ${s.files.map((f) => f.name).join(", ")}` : "", s.queued ? " (queued)" : "", ), ), ), h.p.attrs({ role: "status", ...sx(styles.log) }).children(model.running ? "Forge is working…" : "Forge is free."), Composer(h, { props: { agentId: "composer-commands", label: "Message Forge", placeholder: "Ask Forge to change code… / for commands, @ to mention", attachments: true, mentionables: [forge, grace], commands: [ { name: "test", description: "Run the test suite" }, { name: "review", description: "Review the open diff" }, { name: "plan", description: "Plan before changing code" }, { name: "undo", description: "Undo the last change" }, ], // while Forge works, Stop shows and a new message joins the queue running: model.running, busy: model.running, outcome: model.outcome, }, onEmit: { Sent: ({ text, command, files, queued, request }) => Message.SentComposer({ sent: { text, command: command ?? null, files: files ?? [], queued }, ...(request === undefined ? {} : { request }), }), // a real app uploads the new files here, keyed by their ids Attached: () => undefined, RequestedStop: () => Message.RequestedStopComposer(), RequestedEditLast: () => undefined, Drafted: () => undefined, }, }), ),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
agentId | string | — | The id agents see this composer by; derived from label when left out |
autoFocus | boolean | — | Puts the cursor in the field when it appears: a screen whose only job is to write |
labelrequired | string | — | The field's accessible name: "Message the agent" |
placeholder | string | — | Hint text in the empty field; "Write a message…" when left out |
busy | boolean | — | The agent is working: sending queues the message |
running | boolean | — | The agent is running a reply: with nothing written, the send button is Stop |
disabledReason | string | null | — | Why the person can't send right now ("You've used this week's limit"), or null |
modes | { id, label }[] | — | Modes or models to pick from ("Agent", "Ask"); the first is the default |
attachments | boolean | — | Let the person attach files |
mentionables | Actor[] | — | Who "@" offers: the channel's members and agents |
commands | ComposerCommand[] | — | What "/" offers at the start of a message; Sent.command names the one used |
channels | ComposerChannel[] | — | What "#" offers: the channels a message can link to |
editLast | boolean | — | Up in an empty field asks to edit the last message (RequestedEditLast) |
emoji | boolean | — | ":" offers emoji by name |
initialDraft | string | — | The text the field starts with: the draft the app kept from Drafted (read once, as it mounts) |
broadcast | string | — | In a thread: the label of a checkbox that sends the reply to the channel too, "Also send to #general" |
hints | boolean | on | Show the keyboard hints beside the send button (on by default where there's room) |
outcome | Outcome | null | — | The app's answer to the latest send (Sent.request). Pass null on the first mount to keep the draft until the app accepts it; a refusal keeps the draft and shows the reason. |
Events
| Event | Payload | Description |
|---|---|---|
Sent | { text: string; mode: string | null; queued: boolean; request?: string; mentions?: Actor[]; files?: ComposerFile[]; command?: string; broadcast?: boolean } | The person sent what they wrote, with the chosen mode; queued while the agent is busy |
Attached | { names: string[]; files?: ComposerFile[]; added?: { id, file }[] } | The attachments changed: picked, pasted, dropped or removed. files is the list now, and added hands over the new Files, to upload or keep until Sent names them. |
RequestedStop | {} | The person pressed Stop while the agent was running |
RequestedEditLast | {} | Up in an empty field: open the person's last message for editing |
Drafted | { text: string } | The text changed, by typing, a pick or a send that emptied it: keep it as the draft |
Agent actions
| Action | Description |
|---|---|
ChangedDraft | Write the message (replaces the draft). Mention someone with "@Name" (see mentionable) |
SubmittedDraft | Send the draft |
Wiring it to your app
Key the composer to the conversation and start with outcome set to null. Sent carries the draft and a request ID; send it through your app command.
Return Accepted with that ID once your app accepts or queues the message. For a refusal, return Refused with a reason that says what to do next. The draft and files stay for a retry.
If the connection drops before you hear back, the message may have saved anyway. Keep the draft and check for it. To retry, reuse the original command ID and payload.
View a complete app example
import { UiSchema } from "@effect/ui"import { BrowserHost, Html } from "@effect/ui/html"import { Effect, Layer, Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { Composer } from "./ui/uicontext/components/kit/Composer.ts"import "./base.css"const Message = UiSchema.TaggedEnum({ Submitted: { request: Schema.String, text: Schema.String }, Resolved: { refused: Schema.NullOr(Schema.String) },})const App = Html.component({ name: "InstalledComposer", model: Schema.Struct({ request: Schema.NullOr(Schema.String), outcome: Schema.NullOr(Outcome), text: Schema.String, }), message: Message,}).make({ init: () => ({ request: null, outcome: null, text: "" }), update: { Submitted: ($, { request, text }) => $.evolve({ request, text }), Resolved: ($, { refused }) => $.model.request === null ? $.model : $.evolve({ outcome: refused === null ? { _tag: "Accepted", request: $.model.request } : { _tag: "Refused", request: $.model.request, reason: refused }, }), }, view: (h, model) => h.div.children( Composer(h, { props: { label: "Message", outcome: model.outcome }, onEmit: { Sent: ({ request, text }) => request === undefined ? undefined : Message.Submitted({ request, text }), Attached: () => undefined, RequestedStop: () => undefined, RequestedEditLast: () => undefined, Drafted: () => undefined, }, }), h.output.attr("data-request", model.request ?? "").children(model.text), h.button.attr("type", "button").on("click", Message.Resolved({ refused: "The session is full." })).children("Refuse"), h.button.attr("type", "button").on("click", Message.Resolved({ refused: null })).children("Accept"), ),})Effect.runFork(Layer.launch(BrowserHost.mount(App, {}, document.querySelector("main")!)))entry.ts on GitHubAccessibility
- Enter sends and Shift+Enter adds a line. The send button says why it's disabled when you can't send.
- A refused message keeps its draft, so the person can fix it and send again.