gateway: serve DeepSeek's chat page with magpie's models

@yanghonghang asked on X for DeepSeek's open-source web chat page
(deepseek-ai/chat-demo) to run on magpie, with its search and link
reading served by magpie's own. magpie now serves it at
<gateway>/deepseek-chat/, with no setup:

- The page is the v0.1.1 release (MIT, LICENSE kept in the zip),
  embedded and served from memory. Its API origin is rewritten to
  location.origin+"/deepseek-chat", and magpie.js adds a model picker:
  any magpie model answers, sent as the magpie_chat_model cookie.
- On this computer the page needs no key of the user's. Another
  computer types a gateway key, as for every other route.
- POST anthropic/v1/messages maps deepseek_web_search to magpie's
  web_search_20250305 and deepseek_read_url to a read_url tool magpie
  runs itself with fetchPage between rounds. Each round goes through the
  gateway's own Messages route, so routing, keys, limits and usage are
  the same as an agent's.
- Files (images, PDFs, text) are kept in memory until a restart; Word,
  PowerPoint and EPUB are refused with 415. ASR and TTS go to the speech
  models set in magpie.
- Only the page's GET/HEAD static files are keyless (readOnlyRoutes and
  lanGuard's chatPageOpen), served under a strict CSP. Its models, chat,
  files and speech routes need a key from another computer, and a
  read-only key is refused on each (readOnlyRefused).

Docs: docs/subsystems/gateway-routing.md, "DeepSeek's chat page".

Real use: a sandbox magpie (sandbox HOME, port 3486 on mote) served the
page in Chromium; a Claude answer with web search showed citations and
source chips, a DeepSeek link read answered from the page, and the
picker was checked in light and dark.

Run: go test -tags nogui -run 'TestChatPage|TestReadOnlyKeyEveryRoute'
./internal/gateway on mica, ok. Without this change every TestChatPage*
is red (the routes 404), and TestReadOnlyKeyEveryRoute is red with the
chat routes missing from readOnlyRefused.
This commit is contained in:
yetone
2026-11-14 23:50:45 +08:00
parent cd75e3ca80
commit 6bdeedc6fb
11 changed files with 1836 additions and 2 deletions
+42
View File
@@ -1639,6 +1639,48 @@ round's numbers and its gaps. Tests: [`session_speed_test.go`](../../internal/ga
(`TestSessionCurveFromARealStream`, `TestSessionCurveFromCodexOwnSignIn`)
and `sessions-speed.test.cjs`.
## DeepSeek's chat page
`/deepseek-chat/` is DeepSeek's open-source web chat page
([deepseek-ai/chat-demo](https://github.com/deepseek-ai/chat-demo), MIT,
release v0.1.1) served by the gateway, so any of magpie's models chats in
it and its search and link reading are magpie's (@yanghonghang on X). The
release zip is embedded as published (`chatDemoZip`, its LICENSE served at
`/deepseek-chat/LICENSE`; `TestChatPageIsTheRelease` holds it to the
release's sha256). Nothing is built: the one place the bundle names
`https://api.deepseek.com` is served as `location.origin+"/deepseek-chat"`,
so the page calls the routes below at whatever address it was opened at.
`chatpage/magpie.js`, added to its index.html, gives the page the key
`magpie-deepseek-chat` on this computer (an agent key, counted as
`deepseek-chat`) and draws a model picker in its top bar, read from
`GET /deepseek-chat/models` (the caller's models, a DeepSeek flash model
first as the page's default) and sent as the `magpie_chat_model` cookie.
- Files: the page's own files (GET and HEAD) are the only keyless routes,
in `readOnlyRoutes` and, while shared, `lanGuard`'s `chatPageOpen`, the
same bytes for everyone like the key page. A strict CSP keeps it to its
own origin and scripts. Every call it makes needs a key, which another
computer's user types into the page.
- `POST /deepseek-chat/anthropic/v1/messages` (`chatMessages`): the page's
tools `deepseek_web_search` and `deepseek_read_url` become Anthropic's
`web_search_20250305` (magpie's own search for a model that can't
search, with its citations) and a `read_url` function tool, which magpie
runs with `fetchPage` between rounds, all said to the page as one reply
in its own shapes (`chatTurn`). Each round goes through the gateway's
own Messages route, so routing, masking, limits and usage apply as to
any agent. The page shows sources only from text blocks' `citations`; a
model that names URLs in its text without citing gets no source chips.
- Files the page uploads (`/anthropic/v1/files`) are kept in memory for
the key that uploaded them until magpie restarts: images, PDFs and
text; Word, PowerPoint and EPUB files are refused.
- Voice input and read-aloud (`/v1/tools/asr`, `/v1/tools/tts`) use the
first speech-to-text or text-to-speech model a provider serves, and say
so when there is none.
Tests: `TestChatPageOpensWithoutAKey`, `TestChatPageConversation`,
`TestChatPageSearchesAndReadsLinks`, `TestChatPageFiles`,
`TestTextCallsCitationAfterItsBlock`, `gateway-chat-page.test.cjs`.
## Turning provider search off
[Settings](../../internal/settings/settings.go)' `searcher: "off"` disables magpie's provider-assisted search,