Composer

Write and send a message, with attachments and modes.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/composer

Usage

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.

You've used this week's 500 agent messages. They reset on Monday.

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.

API reference

Props

Composer props
NameTypeDefaultDescription
agentIdstring—The id agents see this composer by; derived from label when left out
autoFocusboolean—Puts the cursor in the field when it appears: a screen whose only job is to write
labelrequiredstring—The field's accessible name: "Message the agent"
placeholderstring—Hint text in the empty field; "Write a message…" when left out
busyboolean—The agent is working: sending queues the message
runningboolean—The agent is running a reply: with nothing written, the send button is Stop
disabledReasonstring | 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
attachmentsboolean—Let the person attach files
mentionablesActor[]—Who "@" offers: the channel's members and agents
commandsComposerCommand[]—What "/" offers at the start of a message; Sent.command names the one used
channelsComposerChannel[]—What "#" offers: the channels a message can link to
editLastboolean—Up in an empty field asks to edit the last message (RequestedEditLast)
emojiboolean—":" offers emoji by name
initialDraftstring—The text the field starts with: the draft the app kept from Drafted (read once, as it mounts)
broadcaststring—In a thread: the label of a checkbox that sends the reply to the channel too, "Also send to #general"
hintsbooleanonShow the keyboard hints beside the send button (on by default where there's room)
outcomeOutcome | 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

Composer events
EventPayloadDescription
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

What agents can do with Composer
ActionDescription
ChangedDraftWrite the message (replaces the draft). Mention someone with "@Name" (see mentionable)
SubmittedDraftSend 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 GitHub

Accessibility

  • 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.

Source