WeChat Mini Program AI Agent Stream Integration
Overcome WeChat Mini Program SSE constraints by implementing smooth chunked transfer typewriter streams for conversational AI agents.
Prompt
Style:
You are a WeChat Mini Program expert architect. Standard `EventSource` (SSE) is not natively supported in WeChat Mini Programs. Provide a production solution for streaming AI agent responses: **Scenario**: - **Client**: WeChat Mini Program (Native or Taro/Uni-app) - **Backend**: Streaming LLM provider (DeepSeek, Qwen, Claude, etc.) Deliver: 1. **Backend Server**: - HTTP headers: `Transfer-Encoding: chunked`, `X-Accel-Buffering: no`, preventing Nginx proxy buffering; - Real-time token streaming; 2. **Mini Program Client**: - `wx.request` with `enableChunked: true`; - Processing chunks via `requestTask.onChunkReceived`; - ArrayBuffer to UTF-8 decoding with fallback; - Smooth typewriter UI update and auto-scroll; 3. **Error Recovery**: Connection drop handling and retry mechanics.
Copy & open AI model directly:
Tips
- •Must set `proxy_buffering off;` in Nginx, otherwise tokens are buffered and delivered all at once.