feat: stream chat responses with automatic poll fallback#176
Open
HardeepAsrani wants to merge 3 commits into
Open
feat: stream chat responses with automatic poll fallback#176HardeepAsrani wants to merge 3 commits into
HardeepAsrani wants to merge 3 commits into
Conversation
- Redesign the frontend chat widget (header with avatar/name/status, modern bubbles, pill input, refined launcher, entrance animation). - Add a global "Chat Sound" toggle and a per-visitor mute control in the chat menu; remove the open/close click sound. - Add a "Message Timestamp" show/hide toggle. - Show the welcome message and suggested questions on first load of the inline chat block. - Inline the chat-button SVGs server-side (no runtime fetch / icon flash) and use a bundled SVG as the default icon instead of an emoji. - Keep the typing dots, suggestion chips, and menu legible on similar colors, and theme the menu from the chat colors. - Resolve outstanding PHPStan issues (gmdate guard, require_once ignore).
Reveal the assistant's reply progressively over Server-Sent Events, with a graceful fallback to the existing background + polling flow on hosts that buffer or cannot stream, so the bot keeps working everywhere. - Add a custom admin-ajax streaming endpoint (Stream) that proxies OpenAI's stream to the browser; the API key stays server-side. - Share a single prompt, JSON schema and response interpreter across the streaming and poll paths so replies never diverge by transport. - The widget tries streaming first and falls back to polling when no event arrives in time; the outcome is cached per browser and re-probed after 24h. - Decode the streamed `response` field incrementally via json_decode so escapes and surrogate-pair emoji render correctly while streaming. - Preserve existing contracts: moderation stays up front, hyve_chat_request and hyve_chat_response fire once per reply, analytics unaffected. Closes Codeinwp/hyve#183
f480aac to
450dbce
Compare
Member
Author
|
@copilot resolve the merge conflicts in this pull request |
…g-responses Co-authored-by: HardeepAsrani <2649903+HardeepAsrani@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds progressive (streaming) chat replies so the assistant's answer appears as it's written, with an automatic fallback to the existing background + polling flow on hosting that buffers or can't stream — the bot keeps working everywhere.
Implements Codeinwp/hyve#183.
How it works
admin-ajaxaction (inc/Stream.php) setstext/event-stream+X-Accel-Buffering: no, calls OpenAI withstream: true, and flushes each delta. The API key stays server-side (browser ↔ WP ↔ OpenAI).OpenAI::interpret_chat_payload/build_chat_items) drive both the streaming and poll paths, so answers never diverge by transport.responsefield is decoded viajson_decodewith incomplete trailing escapes/surrogates/UTF-8 held back, so escapes and emoji render correctly mid-stream and match the final reply.hyve_chat_request/hyve_chat_responsefire once per reply; thread logging, analytics and unanswered-question tracking are unaffected.Files
inc/Stream.php— SSE endpoint, sentinel-free structured-output streaming, partial-field extraction.inc/OpenAI.php—stream_response()(raw cURL), sharedget_chat_response_params/build_chat_items/interpret_chat_payload.inc/API.php—send_chatsplit intoprepare_chat+create_background_runwith amodeparam;get_chatuses the shared interpreter.inc/Main.php— registersStream, localizesajaxUrl+streamNonce.src/frontend/App.js— streaming attempt + poll fallback, progressive rendering, 24h capability cache.tests/php/unit/tests/test-stream.php— partial-extraction tests (escapes, emoji surrogate pairs, split multibyte, incomplete sequences).Test plan
default_messageshown, nothing shown before that's decided.proxy_buffering on): reply isn't progressive → falls back to poll within ~8s, bot still answers; subsequent messages skip straight to poll for 24h.Notes
phpcs:ignore) becausewp_remote_*can't read a response body incrementally.connection_abortedresidual edge documented, validation before SSE headers, embedding-transient TTL raised to cover the stream window.Checks: ESLint + frontend build clean, phpcs clean, phpstan at baseline; PHPUnit/e2e run on CI.