feat(web): dev backend switcher and engine badge for dual-engine debugging - #1592
Merged
Conversation
…gging Add the plumbing to debug kimi-web against the v1 (server) and v2 (kap-server) engines side by side: - root dev:v1 / dev:v2 scripts; v2 boots kap-server with the multi_server flag on a fixed port so both engines can run at once - dev-proxy backend switcher: GET/POST /__kimi-dev/backend repoints the /api/v1 proxy at runtime (HTTP + WS) without a Vite restart - dev-only sidebar backend pill reading /meta's backend field, with a one-click switcher menu; Settings shows the backend engine too - re-fetch /meta on every WS (re)connect so the badge stays truthful across backend restarts and switches - strip the browser Origin header on proxied requests: v1's WS upgrade path rejected the Vite-origin vs server-Host mismatch with 403
🦋 Changeset detectedLatest commit: 308a5c1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
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.
Related Issue
No linked issue — this came from a maintainer request; the problem is explained below.
Problem
Debugging kimi-web against the two backend engines (v1
@moonshot-ai/server, v2@moonshot-ai/kap-server) had three frictions:KIMI_SERVER_URL.Host(changeOrigin) but leaks the browser'sOrigin(the Vite origin); v1's WS upgrade path rejects that Origin-vs-Host mismatch with 403. kap-server doesn't enforce this, which is why v2 appeared to work — the failure only showed up when targeting v1.What changed
pnpm dev:v1/pnpm dev:v2scripts. v2 boots kap-server withKIMI_CODE_EXPERIMENTAL_MULTI_SERVER=1on a fixed port (58628; v1 keeps 58627), so both engines can run side by side — v2 registers in the instance registry instead of taking the single-instance lock.apps/kimi-web/vite.config.ts, dev only):GET/POST /__kimi-dev/backendreads/repoints the/api/v1proxy target at runtime, for both HTTP and WS. Note: vite's vendored http-proxy has noroutersupport (verified in the bundled source), so the plugin captures the live proxy-options object viaconfigureand mutatestargeton it, which vite consults per HTTP request (shallow copy) and per WS upgrade (direct reference). Presets default to 58627/58628, overridable viaKIMI_BACKEND_V1_URL/KIMI_BACKEND_V2_URL.v1/v2+ endpoint sourced fromGET /api/v1/meta'sbackendfield (absent ⇒ v1), with a one-click menu that switches the proxy and reloads. Responsive: the endpoint collapses below 320px sidebar width, the product name below 250px./metais re-fetched on every WS (re)connect, so version/backend can never go stale across backend restarts or proxy switches.Originheader on forwarded requests (proxyReq+proxyReqWs). An Origin-less request is treated as a non-browser client by both engines, and the browser needs no CORS in same-origin mode. This fixes the v1 WS 403 without weakening any server-side check — the proxy was the layer creating the mismatch.workspace-statecoverage for thebackendmapping (incl. absent ⇒ v1) and the reconnect meta refresh.Verification:
vue-tsc,vitest(458 passed),check:style(no new findings),oxlint(0 errors). Functionally verified against live v1/v2 servers: switcher endpoints, proxy retargeting for HTTP and WS (dead-port probe proves the target actually moved), and the 403→401 fix with a browser Origin header. Visually verified light/dark pill, the switcher menu, and the Settings row via headless Chrome.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update — contributor-facing dev tooling plus one Settings label; no CLI/user documentation impact.