Yolk SDK
Build guides

Build a React chat UI

Use headless React helpers for streaming, stop, retry, edit, regenerate, and HITL controls.

@yolk-sdk/agent/react is headless. It manages agent chat state; your app renders the UI.

Install

pnpm add @yolk-sdk/agent@canary effect@4.2.0 react react-dom

Client component

'use client'

import { useState } from 'react'
import { Option, Predicate } from 'effect'
import { agentStatusRetry } from '@yolk-sdk/agent/protocol'
import { buildAgentChatItems, useAgentChat } from '@yolk-sdk/agent/react'

export function AgentChat() {
  const [input, setInput] = useState('')
  const chat = useAgentChat({
    sessionId: 'browser-session',
    endpoint: '/api/agent',
    model: 'gpt-5.5'
  })
  const items = buildAgentChatItems({
    messages: chat.chatMessages,
    isRunning: chat.isRunning,
    activeToolLabel: Option.none(),
    retryInfo: agentStatusRetry(chat.status)
  })

  return (
    <form
      onSubmit={event => {
        event.preventDefault()
        const result = chat.submitText(input)
        if (result._tag === 'Submitted') setInput('')
      }}
    >
      <ol>
        {items.map(item => (
          <li key={item.id}>{renderChatItem(item)}</li>
        ))}
      </ol>
      {Predicate.isTagged(chat.status, 'Failed') ? <p role="alert">{chat.status.message}</p> : null}
      <input
        value={input}
        disabled={!chat.canSubmitText(input)}
        onChange={event => setInput(event.target.value)}
      />
      <button type="submit" disabled={!chat.canSubmitText(input)}>
        Send
      </button>
      <button type="button" disabled={!chat.isRunning} onClick={chat.stop}>
        Stop
      </button>
    </form>
  )
}

renderChatItem is app-owned. Switch on item._tag and render your own message, tool, retry, status, and error components.

Render host-authored messages

UserMessage and AssistantMessage items carry their message's envelope (createdAtMs, author, annotations, each only when set), from live events and from snapshots rebuilt with buildAgentChatMessages alike. A host that writes its own assistant messages, for example a note after a background check, marks them with annotations and renders them differently:

const renderChatItem = (item: AgentChatItem) => {
  if (item._tag === 'AssistantMessage' && item.annotations?.source === 'background_check') {
    return <CheckMarker at={item.createdAtMs} content={item.content} /> // app-owned
  }
  // ...
}

Annotations are model-visible context (Protocol): keep them short and never put instructions in them. Other items of the same message (reasoning, tool runs) share its messageId.

HITL controls

When chat.isWaiting is true (chat.status is Blocked), render approval/question/input controls from tool run items. chat.status.kind says whether the run needs a permission or a question (see Agent status). These are alternative fragments; use the method matching the pending request:

chat.submitToolApprovalResponse(approvalResponse)
chat.submitQuestionResponse(questionResponse)
chat.submitInputResponse(inputResponse)

Build response values from @yolk-sdk/agent/protocol after matching the pending request id in your UI state. Render typed inputs by InputRequest.input.kind using host-owned forms, and keep them pending until server acceptance rather than creating optimistic results. See Collect typed input for handler wiring and correction.

Durable streams

Use useAgentChat for normal browser chats. For replayed durable event logs, use applyAgentEventToChatProjection directly so overlapping chunks de-dupe by eventId.

UX checklist

  • Disable submit while running or awaiting HITL.
  • Keep stop local: it aborts the current browser stream. Server-side cancellation is a separate route.
  • Show retry state from agentStatusRetry(chat.status) and failures from Failed statuses.
  • Keep approval/question/input controls accessible and auditable.
  • Never render raw provider error bodies.

On this page