Skip to content

AI Chat Integration ​

A minimal runnable AI chat loop: oas-prompt-input owns input/stop, oas-message-scroller owns the message list and auto-follow, and oas-message-row / oas-bubble render each turn.

Streaming, stop and retry ​

Send → stream reply → stop / simulate error / retry last prompt

Hi, I am the OAS assistant. Ask me to explain a component, draft a schedule or review a snippet.
Simulate errorRetry lastWaiting
ts
const input = document.querySelector('oas-prompt-input')
const log = document.querySelector('oas-message-scroller')
const status = document.querySelector('#status')

input.addEventListener('oas-submit', async (e) => {
  const prompt = e.detail.value
  const reply = appendAssistantBubble()
  input.setAttribute('loading', '')
  try {
    // Scroll once per turn; streaming relies on auto-scroll to respect reader intent
    log.scrollToEnd()
    for await (const chunk of streamChat(prompt)) {
      reply.textContent += chunk
    }
    status.textContent = 'Done'
  } catch {
    status.textContent = 'Failed, retry available'
  } finally {
    input.removeAttribute('loading')
  }
})
input.addEventListener('oas-stop', () => {
  controller.abort()
  status.textContent = 'Stopped'
})
Integration notes
  • oas-submit only carries detail.value; Enter, Shift+Enter and clearing are handled by the component.
  • Set loading while generating; the send button becomes stop and dispatches oas-stop.
  • Call message-scroller.scrollToEnd() once per turn (on append/send), not for every streamed chunk; per-chunk calls drag readers back to the bottom while they scroll up. Use message-scroller.announce(text) (or your own role=status region) for generating/done/stopped/failed updates.
  • Keep the user message on failure; retry appends a new assistant turn instead of overwriting the error.
  • Use AbortController in real requests and clean up listeners on unmount.

Released under the MIT OR Apache-2.0 License.