diff --git a/docs/reference.md b/docs/reference.md index 817d42db1..d939488f7 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -3868,6 +3868,7 @@ application: - /alpha/search - /mcp - /me + - /deepseek-chat - /join - /magpie-ca.crt services: diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 0650275b1..dd1bcfab5 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -1676,18 +1676,26 @@ first as the page's default) and sent as the `magpie_chat_model` cookie. 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. + any agent. A gateway key's MaxConcurrency holds a turn as it holds an + agent's request (`keySlotRoutes`, lazy: the rounds' tries take the + slot, which `chatInner`'s copy of the context carries), and the speech + routes eagerly; the file routes are free. 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. +- A LazyCat box's manifest makes `/deepseek-chat` public, as it does the + key page, for a key's holder who isn't signed in to the box + (docs/reference.md). Tests: `TestChatPageOpensWithoutAKey`, `TestChatPageConversation`, `TestChatPageSearchesAndReadsLinks`, `TestChatPageFiles`, -`TestTextCallsCitationAfterItsBlock`, `gateway-chat-page.test.cjs`. +`TestTextCallsCitationAfterItsBlock`, `TestKeyConcurrencyChatPage`, +`gateway-chat-page.test.cjs`. ## Turning provider search off diff --git a/internal/gateway/key_slots.go b/internal/gateway/key_slots.go index ded3e8410..d3caed0e8 100644 --- a/internal/gateway/key_slots.go +++ b/internal/gateway/key_slots.go @@ -47,41 +47,47 @@ const ( // their key's MaxConcurrency, and how; every other route of routes() is in // keySlotFree (key_slots_test.go). var keySlotRoutes = map[string]keySlotKind{ - "POST /v1/chat/completions": keySlotLazy, - "POST /chat/completions": keySlotLazy, - "POST /v1/responses": keySlotLazy, - "POST /responses": keySlotLazy, - "POST /v1/messages": keySlotLazy, - "POST /messages": keySlotLazy, - "POST /api/chat": keySlotLazy, - "POST /api/generate": keySlotLazy, - "POST /v1beta/models/{call...}": keySlotLazy, - CodexPath + "/": keySlotLazy, - "POST /v1/systemone": keySlotEager, - "POST /v1" + AlphaSearchPath: keySlotEager, - "POST " + AlphaSearchPath: keySlotEager, - "POST /v1/images/generations": keySlotEager, - "POST /images/generations": keySlotEager, - "POST /v1/images/edits": keySlotEager, - "POST /images/edits": keySlotEager, - "POST /v1/embeddings": keySlotEager, - "POST /embeddings": keySlotEager, - "POST /v1/rerank": keySlotEager, - "POST /rerank": keySlotEager, - "POST /v1/decisions": keySlotEager, - "POST /decisions": keySlotEager, - "POST /v1/audio/transcriptions": keySlotEager, - "POST /audio/transcriptions": keySlotEager, - "POST /v1/audio/translations": keySlotEager, - "POST /audio/translations": keySlotEager, - "POST /v1/audio/speech": keySlotEager, - "POST /audio/speech": keySlotEager, - "POST /v1/videos": keySlotEager, - "POST /videos": keySlotEager, - "POST " + NativePath + "{provider}/{path...}": keySlotEager, - "PUT " + NativePath + "{provider}/{path...}": keySlotEager, - "PATCH " + NativePath + "{provider}/{path...}": keySlotEager, - "DELETE " + NativePath + "{provider}/{path...}": keySlotEager, + "POST /v1/chat/completions": keySlotLazy, + "POST /chat/completions": keySlotLazy, + "POST /v1/responses": keySlotLazy, + "POST /responses": keySlotLazy, + "POST /v1/messages": keySlotLazy, + "POST /messages": keySlotLazy, + "POST /api/chat": keySlotLazy, + "POST /api/generate": keySlotLazy, + "POST /v1beta/models/{call...}": keySlotLazy, + CodexPath + "/": keySlotLazy, + "POST /v1/systemone": keySlotEager, + "POST /v1" + AlphaSearchPath: keySlotEager, + "POST " + AlphaSearchPath: keySlotEager, + "POST /v1/images/generations": keySlotEager, + "POST /images/generations": keySlotEager, + "POST /v1/images/edits": keySlotEager, + "POST /images/edits": keySlotEager, + "POST /v1/embeddings": keySlotEager, + "POST /embeddings": keySlotEager, + "POST /v1/rerank": keySlotEager, + "POST /rerank": keySlotEager, + "POST /v1/decisions": keySlotEager, + "POST /decisions": keySlotEager, + "POST /v1/audio/transcriptions": keySlotEager, + "POST /audio/transcriptions": keySlotEager, + "POST /v1/audio/translations": keySlotEager, + "POST /audio/translations": keySlotEager, + "POST /v1/audio/speech": keySlotEager, + "POST /audio/speech": keySlotEager, + "POST /v1/videos": keySlotEager, + "POST /videos": keySlotEager, + // DeepSeek's chat page: a turn's rounds go through the Messages + // route's tries inside it (chatInner keeps the hold), and its speech + // asks a model before its handler begins (chatpage.go) + "POST " + ChatPagePath + "/anthropic/v1/messages": keySlotLazy, + "POST " + ChatPagePath + "/v1/tools/asr": keySlotEager, + "POST " + ChatPagePath + "/v1/tools/tts": keySlotEager, + "POST " + NativePath + "{provider}/{path...}": keySlotEager, + "PUT " + NativePath + "{provider}/{path...}": keySlotEager, + "PATCH " + NativePath + "{provider}/{path...}": keySlotEager, + "DELETE " + NativePath + "{provider}/{path...}": keySlotEager, } // keyCapped says whether who's key, or the one it was issued under, has a diff --git a/internal/gateway/key_slots_test.go b/internal/gateway/key_slots_test.go index 919b6f645..6a3f7741a 100644 --- a/internal/gateway/key_slots_test.go +++ b/internal/gateway/key_slots_test.go @@ -30,6 +30,8 @@ var keySlotFree = []string{ "POST " + claudeMCPPath + "{token}", "/mcp/{name}", "/mcp/run/{name}", SearchMCPPath, VisionMCPPath, ImageMCPPath, BrowserMCPPath, MemoryMCPPath, ImageMCPFiles + "{name}", ToolsMCPPath, ToolsMCPOne + "{plugin}", HubMCPPath + "{agent}", HubMCPAll, "/", "POST " + joinData, AdminPath, + // the chat page's files are kept in magpie, never sent to a model + "POST " + ChatPagePath + "/anthropic/v1/files", "DELETE " + ChatPagePath + "/anthropic/v1/files/{id}", } // Every route is judged for a key's MaxConcurrency: held (keySlotRoutes) @@ -319,3 +321,55 @@ func TestKeyConcurrencyEager(t *testing.T) { t.Fatal("a GET took a slot") } } + +// DeepSeek's chat page asks a model through the Messages route's tries, +// called in process (chatInner): a turn there waits for its key's slot as +// an agent's request does, and holds it while it asks. +func TestKeyConcurrencyChatPage(t *testing.T) { + fresh(t) + v := &slowVendor{release: map[string]chan struct{}{}, quit: make(chan struct{})} + up := httptest.NewServer(v) + t.Cleanup(up.Close) + if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Key: "k", Models: []string{"m"}, Chat: up.URL + "/v1"}); err != nil { + t.Fatal(err) + } + keys, secrets := newCaller(t, "Team") + keyCap(t, keys[0].ID, 1) + s := New() + gw := lanServer(t, s) + t.Cleanup(func() { close(v.quit) }) + lane := func() Lane { return s.KeyLanes()[keys[0].ID] } + + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + done := make(chan struct{}, 2) + post := func(path, body string) { + go func() { + defer func() { done <- struct{}{} }() + req, _ := http.NewRequestWithContext(ctx, "POST", gw.URL+path, strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+secrets[0]) + req.AddCookie(&http.Cookie{Name: chatModelCookie, Value: "slow/m"}) + if res, err := http.DefaultClient.Do(req); err == nil { + io.Copy(io.Discard, res.Body) + res.Body.Close() + } + }() + } + post("/v1/chat/completions", `{"model":"slow/m","stream":true,"messages":[{"role":"user","content":"a"}]}`) + within(t, "a at the vendor", func() bool { return len(v.seen()) == 1 }) + post(ChatPagePath+"/anthropic/v1/messages", `{"model":"deepseek-flash","messages":[{"role":"user","content":"hi"}]}`) + within(t, "the chat page's turn waits for the key's slot", func() bool { return lane().Waiting == 1 }) + if n := len(v.seen()); n != 1 { + t.Fatalf("the chat page reached the vendor with the key's one slot taken: %v", v.seen()) + } + close(v.gate("a")) + within(t, "the chat page's turn at the vendor", func() bool { return len(v.seen()) == 2 }) + if l := lane(); l.Busy != 1 || l.Waiting != 0 { + t.Fatalf("key lane while the chat page asks = %+v, want its one slot held", l) + } + cancel() + <-done + <-done + within(t, "every key slot given back", func() bool { return len(s.KeyLanes()) == 0 }) +}