Skip to content

feat: add support for merging external events into Prompt.custom - #2205

Merged
IMax153 merged 3 commits into
Effect-TS:mainfrom
lloydrichards:feat/prompt-events
Jun 11, 2026
Merged

IMax153 merged 3 commits into
Effect-TS:mainfrom
lloydrichards:feat/prompt-events

Conversation

@lloydrichards

@lloydrichards lloydrichards commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Type

  • Feature

Description

I've been wanting a way to link in external data into the Prompt.custom in a way that triggers a re-render without needing to press a key stroke. In the current implementation I need to fake a key stroke using process.stdin as a hack-y work around or build me own render loop from scratch. Ideally what I wanted was an API where I could just pass in a stream of events and be able to trigger the action with the next state:

const eventQueue = yield * Queue.make<number>()

const prompt = Prompt.custom(
  { count: 0 },
  Queue.asDequeue(eventQueue), // <-- provide the event queue as a dequeue to the prompt
  {
    render: (state) => Effect.succeed(`Count: ${state.count}`),
    process: (input, state) =>
      Effect.succeed(
        Match.value(input).pipe(
          // handle user input
          Match.tag("Input", () => Action.Submit({ value: state.count })),
          // handle external events from the queue
          Match.tag("Event", (input) => Action.NextFrame({ state: { count: state.count + input.value } })),
          Match.exhaustive
        )
      ),
    clear: () => Effect.succeed("")
  }
)

I've been digging around in the Prompt.ts for a while now so I already knew the Terminal.readInput was a Queue and that it shouldn't be too hard to just merge two Queues together.

What I settled on was a type overload where there is the current implementation without the event doing the same thing (non-breaking) and the new type that adds the new events Queue.Dequeued argument and extends the handlers with the received callback. This way adding the queue can be a progressive enhancement on an already functioning user input only prompt.

Some things I needed to mull over:

  • The Prompt isn't responsible for cleaning up the Queue, this needs to be handled by the user
  • There is a very small possibility of an event and a user input happening simultaneously and which one is picked first is indeterministic (I think)
  • Queue needs to not fail (Queue.Dequeue<A, never> so the user is responsible outside of the Prompt to handle these causes.

Related

I wanted to make an issue first but needed to open the repo to poke around in the types and next thing I know I was already halfway through implementing it 🤦‍♂️

Open to other suggestions or requests to change the approach.


Edit: Have changed the api a bit to put the event before the handler as well as removed the new received callback in favour of overloading the process with a taggedEnum when event us present.

@changeset-bot

changeset-bot Bot commented May 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7938dac

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
effect Major
@effect/opentelemetry Major
@effect/platform-browser Major
@effect/platform-bun Major
@effect/platform-node-shared Major
@effect/platform-node Major
@effect/vitest Major
@effect/ai-anthropic Major
@effect/ai-openai-compat Major
@effect/ai-openai Major
@effect/ai-openrouter Major
@effect/atom-react Major
@effect/atom-solid Major
@effect/atom-vue Major
@effect/sql-clickhouse Major
@effect/sql-d1 Major
@effect/sql-libsql Major
@effect/sql-mssql Major
@effect/sql-mysql2 Major
@effect/sql-pg Major
@effect/sql-pglite Major
@effect/sql-sqlite-bun Major
@effect/sql-sqlite-do Major
@effect/sql-sqlite-node Major
@effect/sql-sqlite-react-native Major
@effect/sql-sqlite-wasm Major
@effect/openapi-generator Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@lloydrichards

lloydrichards commented May 17, 2026 •

Copy link
Copy Markdown
Contributor Author

here are a couple concrete examples of where this would be used:

  • log viewer <- being able to pass in streams logs to show in a scrollable viewport
  • perlin flow <- add a a tickEvent which can move the animation loop along

readonly receive?: (value: A, state: State) => Effect.Effect<Action<State, Output>, never, Environment>
},
events?: Queue.Dequeue<A, never>
): Prompt<Output> => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I would prefer avoiding a separate receive handler and instead merge the event types into the process input, and force the user to handle that logic there. Otherwise, we have two separate handlers that are doing the same thing. (required)
  • The overload that takes the events Queue probably should have the Queue passed before the handlers so that the type of the events gets fixed before you specify the handlers. (maybe)
  • One other thing to consider is using an options object to pass the external events Queue to "future proof" addition of further custom options. (maybe)

@lloydrichards lloydrichards Jun 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the changes:

  • remove the receive callback
  • combine the new ProcessInput taggedEnum with the Terminal.UserInput for the process
  • moved the events Queue to before the handler
  • added the typeTests

I've left out the option object since it kind of conflicts with the moving of the events Queue since I would expect the option key to be an optional arg at the end of a function call but would make the A type defined afterward. Happy to reconsider this if you think it would be better to look like:

const prompt = Prompt.custom(
  { count: 0 },
  {
    render: (state) => Effect.succeed(`Count: ${state.count}`),
    process: (input, state) =>
      Effect.succeed(
        Match.value(input).pipe(
          Match.tag("Input", () => Action.Submit({ value: state.count })),
          Match.tag("Event", (input) => Action.NextFrame({ state: { count: state.count + input.value } })),
          Match.exhaustive
        )
      ),
    clear: () => Effect.succeed("")
  },
  {
    events: Queue.asDequeue(eventQueue)
  }
)

0,
{
render: () => Effect.succeed(""),
process: (_input, state) => Effect.succeed({ _tag: "Submit" as const, value: state }),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not doing anything currently here to ensure the type of _input is an untagged user input.

@lloydrichards lloydrichards Jun 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an explicit expect in the "without events, process receives Terminal.UserInput" test above, but I've gone and added a Match against the output so there is also a type check against the types in action

IMax153
IMax153 previously approved these changes Jun 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

File Name Current Size Previous Size Difference
basic.ts 6.47 KB 6.47 KB 0.00 KB (0.00%)
batching.ts 8.98 KB 8.98 KB 0.00 KB (0.00%)
brand.ts 6.14 KB 6.14 KB 0.00 KB (0.00%)
cache.ts 9.70 KB 9.70 KB 0.00 KB (0.00%)
config.ts 18.52 KB 18.52 KB 0.00 KB (0.00%)
differ.ts 17.01 KB 17.01 KB 0.00 KB (0.00%)
http-client.ts 20.19 KB 20.19 KB 0.00 KB (0.00%)
logger.ts 10.00 KB 10.00 KB 0.00 KB (0.00%)
metric.ts 8.32 KB 8.32 KB 0.00 KB (0.00%)
optic.ts 7.35 KB 7.35 KB 0.00 KB (0.00%)
pubsub.ts 13.81 KB 13.81 KB 0.00 KB (0.00%)
queue.ts 10.89 KB 10.89 KB 0.00 KB (0.00%)
schedule.ts 10.08 KB 10.08 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 3.15 KB 3.15 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 26.47 KB 26.47 KB 0.00 KB (0.00%)
schema-string-transformation.ts 12.71 KB 12.71 KB 0.00 KB (0.00%)
schema-string.ts 10.42 KB 10.42 KB 0.00 KB (0.00%)
schema-template-literal.ts 14.04 KB 14.04 KB 0.00 KB (0.00%)
schema-toArbitraryLazy.ts 20.68 KB 20.68 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 21.25 KB 21.25 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 17.84 KB 17.84 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 17.73 KB 17.73 KB 0.00 KB (0.00%)
schema-toFormatter.ts 17.58 KB 17.58 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 20.09 KB 20.09 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 18.15 KB 18.15 KB 0.00 KB (0.00%)
schema.ts 17.18 KB 17.18 KB 0.00 KB (0.00%)
stm.ts 11.76 KB 11.76 KB 0.00 KB (0.00%)
stream.ts 9.10 KB 9.10 KB 0.00 KB (0.00%)

@IMax153
IMax153 merged commit d944330 into Effect-TS:main Jun 11, 2026
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants