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
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-submitonly carriesdetail.value; Enter, Shift+Enter and clearing are handled by the component.- Set
loadingwhile generating; the send button becomes stop and dispatchesoas-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. Usemessage-scroller.announce(text)(or your ownrole=statusregion) for generating/done/stopped/failed updates. - Keep the user message on failure; retry appends a new assistant turn instead of overwriting the error.
- Use
AbortControllerin real requests and clean up listeners on unmount.