Asks a person for the answer an agent needs to continue.
Which database should I run the migration against?
It adds a NOT NULL column to orders, which has 2.1M rows. I'll stop after it and show you the plan.
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 { Ask } from "./ui/uicontext/components/kit/Ask.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ frame: { inlineSize: "min(100%, 32rem)" },})const forge: Actor = { id: "forge", name: "Forge", kind: "agent" }const ada: Actor = { id: "ada", name: "Ada Lovelace", kind: "person" }const Message = UiSchema.TaggedEnum({ AnsweredDatabase: { value: Schema.String, request: Schema.String }, DeliveredAnswer: { value: Schema.String, request: Schema.String },})// stands in for the agent's server: it takes a moment, then has the answerconst DeliverAnswer = Command.make("Example.DeliverAnswer", { args: { value: Schema.String, request: Schema.String } }, (args) => Effect.sleep("700 millis").pipe(Effect.as(Message.DeliveredAnswer(args))))export const MigrationQuestion = Html.component({ name: "MigrationQuestion", model: Schema.Struct({ outcome: Schema.NullOr(Outcome), answer: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ outcome: null, answer: null }), update: { AnsweredDatabase: ($, { value, request }) => $.evolve({ outcome: { _tag: "Pending", request } }).run(DeliverAnswer.make({ value, request })), DeliveredAnswer: ($, { value, request }) => $.evolve({ outcome: { _tag: "Accepted", request }, answer: value }), }, view: (h, { outcome, answer }) => h.div.attrs(sx(styles.frame)).children( Ask(h, { props: { id: "migration-db", actor: forge, question: "Which database should I run the migration against?", detail: "It adds a NOT NULL column to orders, which has 2.1M rows. I'll stop after it and show you the plan.", options: [ { value: "local", label: "Local database", description: "Fast, and safe to throw away." }, { value: "staging", label: "Staging", description: "A copy of production from last night." }, ], other: "Use another database…", input: { placeholder: "db.internal:5432", pattern: "[A-Za-z0-9.-]+:[0-9]{2,5}", message: "Enter a host and port, like db.internal:5432." }, answer: answer === null ? null : { value: answer, actor: ada }, expires: "Forge waits up to 30 minutes", can: ["answer"], outcome, }, onEmit: { Answered: ({ value, request }) => Message.AnsweredDatabase({ value, request }) }, }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/askUsage
import type { Actor } from "./ui/uicontext/components/domain/Actor.ts"import type { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { Ask } from "./ui/uicontext/components/kit/Ask.ts"const scout: Actor = { id: "scout", name: "Scout", kind: "agent" }const Message = UiSchema.TaggedEnum({ AnsweredOwner: { value: Schema.String, request: Schema.String } })export const OwnerQuestion = (h: Html.H<typeof Message.Type>, outcome: Outcome | null) => Ask(h, { props: { id: "owner", actor: scout, question: "Who should own PAY-412 while Ben is out?", options: [ { value: "mira", label: "Mira Patel" }, { value: "queue", label: "Leave it in the Payments queue" }, ], can: ["answer"], outcome, }, // send the answer to the agent, then pass back an Outcome with the same request onEmit: { Answered: ({ value, request }) => Message.AnsweredOwner({ value, request }) }, })Examples
Checked answers
A number in range, an email the app refuses with a reason, and a longer note with a length limit. Try sending each one empty or wrong.
What's the most I should spend on the team dinner?
Eight people on Thursday. The two places with tables left start at 45 per person.
Which address should the calendar invite go to?
Dr. Osei from Halcyon Health is joining the review.
What should I tell the vendor about declining their offer?
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 { Ask } from "./ui/uicontext/components/kit/Ask.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ stack: { display: "grid", gap: 16, inlineSize: "min(100%, 32rem)" },})const juno: Actor = { id: "juno", name: "Juno", kind: "agent" }const lena: Actor = { id: "lena", name: "Lena Fischer", kind: "person" }const Message = UiSchema.TaggedEnum({ AnsweredBudget: { value: Schema.String, request: Schema.String }, AnsweredEmail: { value: Schema.String, request: Schema.String }, AnsweredNote: { value: Schema.String, request: Schema.String },})export const CheckedAnswers = Html.component({ name: "CheckedAnswers", model: Schema.Struct({ budget: Schema.NullOr(Schema.String), email: Schema.NullOr(Outcome), note: Schema.NullOr(Schema.String), }), message: Message,}).make({ init: () => ({ budget: null, email: null, note: null }), update: { AnsweredBudget: ($, { value }) => $.evolve({ budget: value }), // the app checks what the card can't: whether the address is on the guest list AnsweredEmail: ($, { value, request }) => $.evolve({ email: value.endsWith("@halcyonhealth.example") ? { _tag: "Accepted", request } : { _tag: "Refused", request, reason: `${value} isn't on the Halcyon Health guest list. Use their company address.` }, }), AnsweredNote: ($, { value }) => $.evolve({ note: value }), }, view: (h, { budget, email, note }) => h.div.attrs(sx(styles.stack)).children( Ask(h, { key: "budget", props: { id: "dinner-budget", actor: juno, question: "What's the most I should spend on the team dinner?", detail: "Eight people on Thursday. The two places with tables left start at 45 per person.", input: { kind: "number", min: 360, max: 2000, unit: "EUR", placeholder: "600" }, answer: budget === null ? null : { value: `${budget} EUR`, actor: lena }, can: ["answer"], }, onEmit: { Answered: ({ value, request }) => Message.AnsweredBudget({ value, request }) }, }), Ask(h, { key: "email", props: { id: "guest-email", actor: juno, question: "Which address should the calendar invite go to?", detail: "Dr. Osei from Halcyon Health is joining the review.", input: { kind: "email", placeholder: "[email protected]" }, answer: email?._tag === "Accepted" ? { value: "Invite sent", actor: lena } : null, can: ["answer"], outcome: email, }, onEmit: { Answered: ({ value, request }) => Message.AnsweredEmail({ value, request }) }, }), Ask(h, { key: "note", props: { id: "decline-note", actor: juno, question: "What should I tell the vendor about declining their offer?", input: { kind: "long", minLength: 20, maxLength: 400, placeholder: "A sentence or two, in your words" }, answer: note === null ? null : { value: note, actor: lena }, can: ["answer"], }, onEmit: { Answered: ({ value, request }) => Message.AnsweredNote({ value, request }) }, }), ),})Several choices
With multiple, the person ticks any that apply and sends them together. other adds a typed answer.
Where should I post the 2.14 release notes?
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 { Ask } from "./ui/uicontext/components/kit/Ask.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ frame: { inlineSize: "min(100%, 32rem)" },})const scout: Actor = { id: "scout", name: "Scout", kind: "agent" }const omar: Actor = { id: "omar", name: "Omar Haddad", kind: "person" }const Message = UiSchema.TaggedEnum({ AnsweredChannels: { value: Schema.String } })export const SeveralChoices = Html.component({ name: "SeveralChoices", model: Schema.Struct({ answer: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ answer: null }), update: { AnsweredChannels: ($, { value }) => $.evolve({ answer: value }) }, view: (h, { answer }) => h.div.attrs(sx(styles.frame)).children( Ask(h, { props: { id: "release-channels", actor: scout, question: "Where should I post the 2.14 release notes?", options: [ { value: "#releases", label: "#releases", description: "Everyone, 214 members" }, { value: "#support", label: "#support", description: "So the team can answer questions" }, { value: "changelog", label: "Public changelog", description: "Customers see it right away" }, ], multiple: true, other: "Somewhere else…", input: { placeholder: "#channel or a link" }, answer: answer === null ? null : { value: answer, actor: omar }, can: ["answer"], }, onEmit: { Answered: ({ value }) => Message.AnsweredChannels({ value }) }, }), ),})Answered, closed and read-only
Once answered, the card shows the answer and who gave it. A question that ran out says why; someone who can't answer sees who can.
Who should own PAY-412 while Ben is out?
Mira Patel · answered
Should I reopen ENG-88?
No answer in 30 minutes, so Scout left it closed.
Which card should the refund go to?
Only Mira can answer: she owns the account.
import type { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import type { Actor } from "./ui/uicontext/components/domain/Actor.ts"import { Ask } from "./ui/uicontext/components/kit/Ask.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ stack: { display: "grid", gap: 16, inlineSize: "min(100%, 32rem)" },})const scout: Actor = { id: "scout", name: "Scout", kind: "agent" }const mira: Actor = { id: "mira", name: "Mira Patel", kind: "person" }const options = [ { value: "mira", label: "Mira Patel" }, { value: "queue", label: "Leave it in the Payments queue" },]export const AskStates = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.stack)).children( Ask(h, { key: "answered", props: { id: "states-answered", actor: scout, question: "Who should own PAY-412 while Ben is out?", options, answer: { value: "mira", actor: mira }, can: ["answer"] }, onEmit: { Answered: () => undefined }, }), Ask(h, { key: "closed", props: { id: "states-closed", actor: scout, question: "Should I reopen ENG-88?", options, closed: "No answer in 30 minutes, so Scout left it closed.", can: ["answer"] }, onEmit: { Answered: () => undefined }, }), Ask(h, { key: "read-only", props: { id: "states-read-only", actor: scout, question: "Which card should the refund go to?", options, whoMayAnswer: "Only Mira can answer: she owns the account.", can: [] }, onEmit: { Answered: () => undefined }, }), )API reference
Props
| Name | Type | Description |
|---|---|---|
idrequired | string | Identifies the question, for agents and for the answer |
questionrequired | string | What the agent needs to know, as a question |
detail | string | Why it asks, or what it will do with the answer |
actor | Actor | The agent asking |
options | AskOption[] | Choices to pick from; without them, the person types an answer |
multiple | boolean | Pick any number of options, then send |
other | string | With options: a typed answer is allowed too, under this label ("Something else") |
input | AskInput | What a typed answer must be |
answer | { value, actor } | null | The answer once given, and who gave it |
closed | string | The question ran out of time, or the agent moved on, with the reason to show |
expires | string | "Expires in 10 min", under the question while it waits |
outcome | Outcome | null | The app's answer to the last Answered request |
canrequired | Can(["answer"]) | "answer" when this person may answer |
whoMayAnswer | string | Who may answer, shown to someone who can't: "Only Ada can answer" |
Events
| Event | Payload | Description |
|---|---|---|
Answered | { value: string; values?: string[]; request: string } | The person answered: an option's value, the picked values joined by ", " (with values), or what they typed. Settle request through outcome. |
Agent actions
| Action | Description |
|---|---|
ChoseOption | Answer with one of the options (see page_query) |
ToggledOption | Pick (on: true) or unpick one of several options; then SubmittedPicked sends them |
SubmittedPicked | Send the picked options |
ChangedText | Write a free-text answer |
SubmittedText | Send the written answer. It must pass the question's checks; problem in the snapshot says why it didn't. |
Accessibility
- The question is a region named by the question. Its options are buttons; without options it's a labelled text field that Enter sends.
- Once answered, the card says the answer and who gave it, and the choices go away.
- Agents read the question and whether it's answered, and must answer with one of its options.