Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: answers as an Ollama server, so Copilot in Visual Studio and VS Code lists magpie's models when given the gateway as its Ollama URL (wani on Discord)

yetone94df239265a2
diff --git a/docs/reference.md b/docs/reference.md
index f04068a3e..67a32afc2 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -1693,6 +1693,7 @@ It exposes:
| `/v1/audio/transcriptions`, `/v1/audio/translations` | OpenAI speech to text (multipart, passed through) |
| `/v1/models`, `/v1beta/models` | the catalog |
| `/v1/codex/models` | the catalog as Codex's `model_catalog_url` reads it ([Codex on another computer](#providers-and-the-gateway)) |
+| `/api/version`, `/api/tags`, `/api/show` | the catalog as an Ollama server lists it, for a client that takes a custom endpoint only as Ollama (below) |
Each `/v1/models` entry includes `reasoning` and `supported_reasoning_levels`
(`[{"effort":"low"}, ...]`), and the same levels as plain names in
@@ -1709,6 +1710,16 @@ what the agent asked, as before. `native_endpoints`
straight through on; it is left out of a routing group, and of a model every
request to which is translated anyway.
+A client that takes a custom endpoint only as an Ollama server (Visual
+Studio's and VS Code's Copilot: Manage Models, then Ollama) is given
+`http://127.0.0.1:3425` as the Ollama URL, no `/v1`. It reads the models
+from `/api/tags`, each one's tools, vision, thinking and window from
+`/api/show`, and chats on `/v1/chat/completions`. Ollama's own chat
+(`/api/chat`, `/api/generate`, so `ollama run`) isn't served. Copilot sends
+no key, so this works on this computer only; behind a reverse proxy with a
+single public URL, `/api` goes to `magpie web`'s page, so give the
+gateway's own port.
+
Requests pass straight through when the vendor speaks the agent's API and
are translated otherwise, streaming, tool calls and reasoning included. The
key is `magpie` (any value works; the gateway only listens on loopback), and
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 09a32468c..01090d794 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -28,7 +28,7 @@ backfilled; see [Gateway Session Aggregation](gateway-sessions.md).
| --- | --- | --- |
| Address and caller | `DefaultAddr`. The local token `magpie` and the per-agent `magpie-<agent>` (`TokenFor`). `agentOf` tells which agent asked, first by token, then by User-Agent. Claude Science, whose User-Agent is the Anthropic SDK's, is told by its client's `X-Anthropic-Surface: operon` (`isClaudeScience`). It is listed every model as `claude-magpie-<number>` (`scienceModels`; its picker keeps only a `claude-` id whose name isn't a lower-case slug), and a Claude id magpie doesn't serve (its default before a pick, its haiku side tasks) goes to the model its last turn with tools was on, else the first it is listed (`scienceTurn`, kept in `claude-science.model`). | [`gateway.go`](../../internal/gateway/gateway.go) |
| TLS | `--tls-cert`/`--tls-key` (`MAGPIE_TLS_CERT`, `MAGPIE_TLS_KEY`) make `ListenAndServe`, `Relisten` (both through `Server.listen`) and `magpie web`'s page (`ServeTLS`) wrap the listener in `tlsListener`. A connection from another computer is `tls.Server` at once, so its plain HTTP gets net/http's 400. One from this computer (`fromThisComputer`: loopback, or a peer address equal to the local one) is sniffed on its own goroutine, 10s at most (`sniffWait`), and 0x16 makes it TLS, so `URL()`, the agents, MCP servers, `ServedBy` and the healthcheck keep `http://127.0.0.1`. `certFiles.get` reloads the pair when either file's mtime or size changes; a pair that doesn't load keeps the one served (logged once per change). `tlsConfig` refuses half a pair or files that don't load before binding. HTTP/1.1 only over TLS (hijack, Codex's websocket). `Scheme()` gives the scheme of `LANURLs`, a scheme-less `MAGPIE_PUBLIC_URL` and the page's network links. Host and Origin checks (`foreignPage`, `rebound`) don't depend on the scheme. | [`tls.go`](../../internal/gateway/tls.go), [`lan.go`](../../internal/gateway/lan.go) |
-| HTTP surface | `Handler` routes `/v1/chat/completions`, `/v1/responses`, `/v1/messages` (each also without `/v1`), `/v1/models`, Gemini's `/v1beta/models/…`, the Codex backend path, `/v1/codex/models` (Codex's catalog shape for a Codex elsewhere with a gateway key, `codexCatalog`, kept to the key's models by `keyAllowed`; #1281), `/mcp/{name}`, `/mcp/magpie/web-search` and `/mcp/magpie/vision` (magpie's own web search and image recognition as MCP servers, below) and the `/v1/magpie/*` status endpoints. `/v1/magpie/heap` (`heapProfile`, [`heap.go`](../../internal/gateway/heap.go), for `magpie heap`) is held tighter than the rest by `heapRefused` in the handler itself, so it holds where `Handler` is served without `lanGuard` too: only a loopback caller that isn't proxied (`local`), carries no browser mark (`fromBrowser`) and names a loopback `Host` (`loopbackHost`) gets the profile; a gateway key never lets anything else in. `/v1/magpie/cpu` (`cpuProfile`, [`cpu.go`](../../internal/gateway/cpu.go), for `magpie cpu`) records a CPU profile for `?seconds=` (30, from 5 to 120) and is held to the same callers by `cpuRefused`, which shares `profileRefused` with `heapRefused`; one profile records at a time (`cpuRecording`), a second is answered 409, and a caller that hangs up stops it. `/v1/alpha/search` (also without `/v1`, and on the Codex backend path for a magpie model) is Codex's web.run search (`alphaSearch`, `codex_search.go`): a ChatGPT account's model goes to the ChatGPT backend's `/alpha/search` on the account's sign-in, the next account asked on 401, 403, 429 or 5xx; any other model's `search_query` is searched by `webSearch`, and when nothing answers the reason is a 200 with `output`, since Codex ends the turn on any failed web.run (anton on Discord). A GET on `/v1/responses` (a client opening Responses over a WebSocket) is answered 426, a plain one 405: Responses is relayed over HTTP only. Every route is wrapped in `counted`, `corsGuard`, `callerGuard`, `withCaller` and `keyLimited`; `/v1/messages`, `/messages` and `/v1/messages/count_tokens` in `keepsSignIn` too. `corsGuard` (#1051) serves CORS only to the origins listed in Settings › Network › Web pages (`settings.CORSOrigins`, cleaned to `scheme://host[:port]` by `settings.CleanOrigins`, which refuses a wildcard). A browser extension's own pages, popup and service worker are listed the same way by their origin, which is the browser's scheme and the extension's id: `chrome-extension://<id>` (Chromium, the id 32 letters a–p), `moz-extension://<uuid>` (Firefox, the Internal UUID about:debugging shows, not the add-on id) and `safari-web-extension://<uuid>` (Safari, which picks a new UUID on each launch, so a listing lasts until Safari restarts); the id must have that browser's shape, cased any way, and nothing else is taken — no bare scheme, no wildcard, no path (SkyAerope on Discord: an extension without host permissions is held to CORS like a page). A content script's fetch carries the page's origin, not the extension's. A listed extension is treated exactly as a listed page, below; one not listed passes as before (other schemes, below) with no CORS headers. For a listed origin it answers the preflight (204) and adds `Access-Control-Allow-Origin`, and every other request from it needs an enabled gateway key (`access.Authenticate`), even on loopback, or gets a 401. It takes the key from the caller `lanGuard` recorded too, since on the running server `lanGuard` has already put magpie's own token in the key's place. Any other web page is refused 403 on every route (`foreignPage`): on loopback a call needs no key and the body's Content-Type isn't checked, so a page the user visits could otherwise send a "simple" `text/plain` POST and spend their subscriptions blind. Only an `http`/`https` Origin is judged, and one on this computer (`localhost`, `*.localhost`, `127.0.0.0/8`, `[::1]`, any port) or the request's own origin (its host and port equal to `Host`, such as a page on the gateway's LAN address) passes, as does usemagpie.ai on `/v1/magpie/omarchy`. Other schemes (`app://`, `file://`, `tauri://`, `vscode-webview://`, `chrome-extension://`) come from desktop apps, webviews and extensions that don't enforce CORS, and pass keyless as before; `null` is not one of them, since any web page sends it from a sandboxed frame and an opened file sends it too, so a call with it needs an enabled gateway key as a listed origin's does, and is answered 403 without one (no CORS headers either way, so a frame or a local page still reads no reply). A page at a subdomain of localhost (`localhostSubdomain`, #2256) needs a key the same way: Chrome and Firefox resolve `*.localhost` to loopback themselves, but Safari and other WebKit views ask the system resolver, which before macOS 26 asks the network's DNS (WebKit bug 160504), so a resolver that answers serves such a page from anywhere. `tauri.localhost` and `wails.localhost` stay keyless on Windows only, where Tauri's and Wails' webviews serve themselves at those names; on macOS and Linux those apps use `tauri://` and `wails://`. A request with no Origin (every agent's) passes through with no CORS headers, as before. A page whose own name resolves to 127.0.0.1 (DNS rebinding) would count as same-origin here, so `lanGuard` refuses it first (`rebound`, 403): a loopback request carrying a browser's marks (`Origin`, `Sec-Fetch-Site`/`Sec-Fetch-Mode`, a `Mozilla/` user agent) whose `Host` isn't this computer's (an IP, `localhost` but not a subdomain of it, which WebKit asks the DNS for (#2256), a container runtime's name for the host such as `host.docker.internal`, `MAGPIE_PUBLIC_URL`'s or `MAGPIE_ADDR`'s host, the machine's own name) needs an enabled gateway key. Agents send none of those marks and pass under any name. Before any of that, `lanGuard` lets in one request without a key from anywhere: `POST /_magpie/claude-mcp/<token>` with the token of a Claude subscription run now live (`ownCallback`, see [the Claude subscription bridge](claude-subscription-bridge.md)), the run's MCP helper calling back to a `MAGPIE_ADDR` that isn't loopback (#1546). Next, before the key or `rebound` is looked at, the allow list (lc on Discord, magpie in Docker on a public host): with `MAGPIE_ALLOW_IPS` or Settings › Network › Allowed addresses (`settings.AllowIPs`, this computer's own, never brought by a sync or restore: see [sync and backup](sync-backup.md#what-stays-on-each-computer); the variable wins and the page then only shows it) naming IPs and CIDR ranges, a request whose connection (`RemoteAddr`, IPv4-mapped IPv6 unmapped, zone dropped) is outside them is refused 403 naming its address, even with an enabled gateway key (`refuseNotAllowed`). Loopback and this computer's own interface addresses always pass. A forwarding header is never taken for the address, so a proxy or tunnel on this computer passes (it arrives from loopback) and its clients are limited at the proxy. An entry that doesn't parse lets nothing in and is logged once; the list stays on. Settings' list is read with `settings.LoadChecked`: a settings.json that can't be read, or doesn't parse with no last good generation, is unknown, never "no list", so the list read last in this process stays in force (`lastAllow`), and with none read yet only this computer passes, the 403 saying settings.json couldn't be read; logged once. No file, or an empty one, is no list. The Settings page's saves read with `LoadChecked` too and refuse such a file, as `settings.Save` does. `magpie web`'s server is wrapped in the same check (`AllowGuard`). Settings refuses a list that would leave out the address the page saving it is open from (`KeepsOut`); the page names that address, which behind Docker or a proxy is the proxy's, and offers to add it to the refused list (`allowSays`, Mathresearcher on Discord). | [`gateway.go`](../../internal/gateway/gateway.go), [`allow.go`](../../internal/gateway/allow.go), [`internal/settings/allowips.go`](../../internal/settings/allowips.go), [`cors.go`](../../internal/gateway/cors.go), [`codex_catalog.go`](../../internal/gateway/codex_catalog.go), [`internal/settings/origins.go`](../../internal/settings/origins.go) |
+| HTTP surface | `Handler` routes `/v1/chat/completions`, `/v1/responses`, `/v1/messages` (each also without `/v1`), `/v1/models`, Gemini's `/v1beta/models/…`, the Codex backend path, `/v1/codex/models` (Codex's catalog shape for a Codex elsewhere with a gateway key, `codexCatalog`, kept to the key's models by `keyAllowed`; #1281), Ollama's `GET /api/version`, `GET /api/tags` and `POST /api/show` (`ollama.go`, for a client that takes a custom endpoint only as an Ollama server: Visual Studio's and VS Code's Copilot, wani on Discord; the list is `catalogFor`'s, so a key's models alone, `/api/show` tells `capabilities` (`completion`, `tools`, `vision` from `Images`, `thinking` from `Thinks`) and `model_info` with `general.architecture` `magpie`, `general.basename`, `magpie.context_length` (32768, VS Code's own default, when the window isn't known, since VS Code would take a 0) and the `general.parameter_count` and `magpie.embedding_length` the ollama CLI before 0.7 reads unchecked; the client then chats on `/v1/chat/completions`, and Ollama's `/api/chat` and `/api/generate` aren't served), `/mcp/{name}`, `/mcp/magpie/web-search` and `/mcp/magpie/vision` (magpie's own web search and image recognition as MCP servers, below) and the `/v1/magpie/*` status endpoints. `/v1/magpie/heap` (`heapProfile`, [`heap.go`](../../internal/gateway/heap.go), for `magpie heap`) is held tighter than the rest by `heapRefused` in the handler itself, so it holds where `Handler` is served without `lanGuard` too: only a loopback caller that isn't proxied (`local`), carries no browser mark (`fromBrowser`) and names a loopback `Host` (`loopbackHost`) gets the profile; a gateway key never lets anything else in. `/v1/magpie/cpu` (`cpuProfile`, [`cpu.go`](../../internal/gateway/cpu.go), for `magpie cpu`) records a CPU profile for `?seconds=` (30, from 5 to 120) and is held to the same callers by `cpuRefused`, which shares `profileRefused` with `heapRefused`; one profile records at a time (`cpuRecording`), a second is answered 409, and a caller that hangs up stops it. `/v1/alpha/search` (also without `/v1`, and on the Codex backend path for a magpie model) is Codex's web.run search (`alphaSearch`, `codex_search.go`): a ChatGPT account's model goes to the ChatGPT backend's `/alpha/search` on the account's sign-in, the next account asked on 401, 403, 429 or 5xx; any other model's `search_query` is searched by `webSearch`, and when nothing answers the reason is a 200 with `output`, since Codex ends the turn on any failed web.run (anton on Discord). A GET on `/v1/responses` (a client opening Responses over a WebSocket) is answered 426, a plain one 405: Responses is relayed over HTTP only. Every route is wrapped in `counted`, `corsGuard`, `callerGuard`, `withCaller` and `keyLimited`; `/v1/messages`, `/messages` and `/v1/messages/count_tokens` in `keepsSignIn` too. `corsGuard` (#1051) serves CORS only to the origins listed in Settings › Network › Web pages (`settings.CORSOrigins`, cleaned to `scheme://host[:port]` by `settings.CleanOrigins`, which refuses a wildcard). A browser extension's own pages, popup and service worker are listed the same way by their origin, which is the browser's scheme and the extension's id: `chrome-extension://<id>` (Chromium, the id 32 letters a–p), `moz-extension://<uuid>` (Firefox, the Internal UUID about:debugging shows, not the add-on id) and `safari-web-extension://<uuid>` (Safari, which picks a new UUID on each launch, so a listing lasts until Safari restarts); the id must have that browser's shape, cased any way, and nothing else is taken — no bare scheme, no wildcard, no path (SkyAerope on Discord: an extension without host permissions is held to CORS like a page). A content script's fetch carries the page's origin, not the extension's. A listed extension is treated exactly as a listed page, below; one not listed passes as before (other schemes, below) with no CORS headers. For a listed origin it answers the preflight (204) and adds `Access-Control-Allow-Origin`, and every other request from it needs an enabled gateway key (`access.Authenticate`), even on loopback, or gets a 401. It takes the key from the caller `lanGuard` recorded too, since on the running server `lanGuard` has already put magpie's own token in the key's place. Any other web page is refused 403 on every route (`foreignPage`): on loopback a call needs no key and the body's Content-Type isn't checked, so a page the user visits could otherwise send a "simple" `text/plain` POST and spend their subscriptions blind. Only an `http`/`https` Origin is judged, and one on this computer (`localhost`, `*.localhost`, `127.0.0.0/8`, `[::1]`, any port) or the request's own origin (its host and port equal to `Host`, such as a page on the gateway's LAN address) passes, as does usemagpie.ai on `/v1/magpie/omarchy`. Other schemes (`app://`, `file://`, `tauri://`, `vscode-webview://`, `chrome-extension://`) come from desktop apps, webviews and extensions that don't enforce CORS, and pass keyless as before; `null` is not one of them, since any web page sends it from a sandboxed frame and an opened file sends it too, so a call with it needs an enabled gateway key as a listed origin's does, and is answered 403 without one (no CORS headers either way, so a frame or a local page still reads no reply). A page at a subdomain of localhost (`localhostSubdomain`, #2256) needs a key the same way: Chrome and Firefox resolve `*.localhost` to loopback themselves, but Safari and other WebKit views ask the system resolver, which before macOS 26 asks the network's DNS (WebKit bug 160504), so a resolver that answers serves such a page from anywhere. `tauri.localhost` and `wails.localhost` stay keyless on Windows only, where Tauri's and Wails' webviews serve themselves at those names; on macOS and Linux those apps use `tauri://` and `wails://`. A request with no Origin (every agent's) passes through with no CORS headers, as before. A page whose own name resolves to 127.0.0.1 (DNS rebinding) would count as same-origin here, so `lanGuard` refuses it first (`rebound`, 403): a loopback request carrying a browser's marks (`Origin`, `Sec-Fetch-Site`/`Sec-Fetch-Mode`, a `Mozilla/` user agent) whose `Host` isn't this computer's (an IP, `localhost` but not a subdomain of it, which WebKit asks the DNS for (#2256), a container runtime's name for the host such as `host.docker.internal`, `MAGPIE_PUBLIC_URL`'s or `MAGPIE_ADDR`'s host, the machine's own name) needs an enabled gateway key. Agents send none of those marks and pass under any name. Before any of that, `lanGuard` lets in one request without a key from anywhere: `POST /_magpie/claude-mcp/<token>` with the token of a Claude subscription run now live (`ownCallback`, see [the Claude subscription bridge](claude-subscription-bridge.md)), the run's MCP helper calling back to a `MAGPIE_ADDR` that isn't loopback (#1546). Next, before the key or `rebound` is looked at, the allow list (lc on Discord, magpie in Docker on a public host): with `MAGPIE_ALLOW_IPS` or Settings › Network › Allowed addresses (`settings.AllowIPs`, this computer's own, never brought by a sync or restore: see [sync and backup](sync-backup.md#what-stays-on-each-computer); the variable wins and the page then only shows it) naming IPs and CIDR ranges, a request whose connection (`RemoteAddr`, IPv4-mapped IPv6 unmapped, zone dropped) is outside them is refused 403 naming its address, even with an enabled gateway key (`refuseNotAllowed`). Loopback and this computer's own interface addresses always pass. A forwarding header is never taken for the address, so a proxy or tunnel on this computer passes (it arrives from loopback) and its clients are limited at the proxy. An entry that doesn't parse lets nothing in and is logged once; the list stays on. Settings' list is read with `settings.LoadChecked`: a settings.json that can't be read, or doesn't parse with no last good generation, is unknown, never "no list", so the list read last in this process stays in force (`lastAllow`), and with none read yet only this computer passes, the 403 saying settings.json couldn't be read; logged once. No file, or an empty one, is no list. The Settings page's saves read with `LoadChecked` too and refuse such a file, as `settings.Save` does. `magpie web`'s server is wrapped in the same check (`AllowGuard`). Settings refuses a list that would leave out the address the page saving it is open from (`KeepsOut`); the page names that address, which behind Docker or a proxy is the proxy's, and offers to add it to the refused list (`allowSays`, Mathresearcher on Discord). | [`gateway.go`](../../internal/gateway/gateway.go), [`allow.go`](../../internal/gateway/allow.go), [`internal/settings/allowips.go`](../../internal/settings/allowips.go), [`cors.go`](../../internal/gateway/cors.go), [`codex_catalog.go`](../../internal/gateway/codex_catalog.go), [`ollama.go`](../../internal/gateway/ollama.go), [`internal/settings/origins.go`](../../internal/settings/origins.go) |
| Model resolution | `serve` resolves the model the agent asked for: Claude tier stand-ins, a routing group (`provider.GroupFor`: its id, the model's id however a vendor spells it, else the one group whose name it is, unless a provider serves a model by that id; the user's own groups only while the `groups` plugin is on, [the plugin runtime](plugin-runtime.md#gateway-extension-points-p3-router-strategy)), auto stand-ins, the agent's own stand-in (`StandIn`: Codex's config.toml `model` when magpie is its provider, for a name it sends that magpie doesn't serve, such as its auto-review's `codex-auto-review`), itself taken through `GroupFor` and auto stand-ins so a config naming a group bare reaches it, then `provider.Resolve`. A Codex memory call or auto-review (`x-openai-subagent: guardian`, or `codex-auto-review`) that still names no served model goes to the model Codex's last turn was answered on (`codexMemoryStandIn`, per agent and gateway key), so a Codex on magpie through another provider table, a profile or another computer isn't answered 404 (Adam on Discord, `TestCodexAutoReviewReachesItsTurnsModel`). A bare model id several providers serve (MOMO on Discord: `glm-5.3` under opencode-go and a6api) is never refused: with found groups on (the default) `GroupFor` takes it to the group magpie found for it (`auto-<slug>`), its members in the Providers order, routed and failed over as any group, a conversation staying with the member that answered it while its cache is worth it (affinity); with them off, `Resolve` takes the first provider in the Providers order that exposes it, with no fallback. magpie writes only `provider/model` ids into agents' configs (`magpieModels`), so a bare id is one the user wrote. `/v1/models` lists bare ids only when the user asks for them ([Model ids without the provider](#model-ids-without-the-provider)). Every reply says who answered in `X-Magpie-Provider` and `X-Magpie-Model` (`noteMember`), and the usage log and the Routing page keep it. A model id with `~` and no slash that resolves as nothing else is the one served id (a provider's model or a group) whose slashes it spells as `~` (`unflat`, `provider.Unflat`; MiniMax Code's spelling, #1387), taken back in `validateModel` before key limits, middleware and usage see it; two ids spelled alike are neither. A Claude model id whose version is dashed (`relay/claude-opus-4-6-us`, with or without `:level` and `[1m]`) that is not served as spelled is the one served id it dashes (`provider.ClaudeUndashed`; the spelling magpie gives Claude Code, which reads a dotted version as Claude Opus 4), taken back in `validateModel` and `unprefixed` the same way. An unknown model gets a 404 naming why: a switched-off provider, a disabled group or an empty group. Otherwise it names up to three ids nearest to what was asked (`provider.Closest`: words of a model's display name count before words of its id, and a provider's id or name only alongside them, so "Zen-Space-Bunny" is told `opencode-zen-free/space-bunny-free`, MOMO on Discord), then the ways a model can be named, then every id (`unknownModel`; chat and embeddings). | [`gateway.go`](../../internal/gateway/gateway.go), [`internal/provider/group.go`](../../internal/provider/group.go), [`internal/provider/flat.go`](../../internal/provider/flat.go), [`internal/provider/claudedash.go`](../../internal/provider/claudedash.go) |
| Candidates | `plan` lists one provider's candidates: one per key or account on, then each `Fallback` model, with resting ones last (`restLast`). `planGroup` and `planLevel` weigh a group's members together. A nested group plans by its own routing (#576). So does a member whose provider routes other than Smart (`ownRouting`): weighed as one by its first not resting (`firstAwake`), tried whole in its own order; `leastHeld` leaves such a group's order alone too. A provider on Smart, or routed as the group is, is pooled with the group's other keys and accounts. `group_own_routing_test.go`. A member's own fallbacks are not the group's. | [`fallback.go`](../../internal/gateway/fallback.go) |
| Order of keys and accounts | `Routing`: smart (""), `order`, `rotate`, `usage` (least used, with an `usageHalfLife` of 1h), `pace` or `weight`. `weighRouted` reads each candidate into a `routes.Seat` and asks the strategy serving its routing (`routes.Find`, the router strategy point of [the plugin runtime](plugin-runtime.md#gateway-extension-points-p3-router-strategy)) for the order: Smart is `routes.Smart`, the core's; the others are `routes.Builtin`, registered by the built-in plugin `rotation`. With it off a routing no strategy serves is tried in its listed order, and `routingOf` treats it as `order` wherever the core asks about a routing (a turn's mark, `goesRound`, `sinks`, a rotate group's sessions, a group's heads). Smart, `usage` and `pace` weigh a subscription by its allowance (`allowances`), and a key by its own usage windows when magpie reads them (`keyAllowance`: a sub2api key given a 5-hour, day or 7-day limit, or a key bought as a plan, such as Kimi Code's or a GLM Coding Plan's); any other key has none. A group routed `weight` (#1910) takes its members, not their keys, by smooth weighted round robin: `planLevel` makes each model member (its keys ordered by its own provider's routing) and each nested group one unit, and `weightedUnits` puts the round's pick (`routes.Round` over `group/<id>`, keyed by the member's spelling, `Group.Weights`, 1 if unset) first and the rest in member order for failover; a unit whose head rests leaves the round, and a kept conversation moves the round's charge to the member that kept it (`wrrSeat`, `routes.MoveRound`). A lone key or account is weighed against nobody, but what it has left is still read into the trace (`weighRouted`), so the Routing page shows it as it does for several (#1016); all but used up, it keeps its conversation rather than being said to move (`affine`). A failure rests a candidate (`failure`, `failureOf`). | [`routing.go`](../../internal/gateway/routing.go), [`weighted.go`](../../internal/gateway/weighted.go), [`internal/routes/`](../../internal/routes), [`internal/provider/routing.go`](../../internal/provider/routing.go) |
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index 2399dcbea..95b5961d0 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -628,6 +628,11 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /api/hello", func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{"name": "magpie", "version": Version})
})
+ // the Ollama API's model routes, for a client that takes a custom
+ // endpoint only as an Ollama server (ollama.go)
+ mux.HandleFunc("GET /api/version", s.ollamaVersion)
+ mux.HandleFunc("GET /api/tags", s.ollamaTags)
+ mux.HandleFunc("POST /api/show", s.ollamaShow)
mux.HandleFunc("GET /v1/magpie/quotas", s.quotas)
mux.HandleFunc("GET /v1/magpie/usage", s.usageSummary)
mux.HandleFunc("GET /v1/magpie/usage/requests", s.usageRequests)
diff --git a/internal/gateway/lan_test.go b/internal/gateway/lan_test.go
index 84caeb9dc..3d381613f 100644
--- a/internal/gateway/lan_test.go
+++ b/internal/gateway/lan_test.go
@@ -332,6 +332,7 @@ func TestNetworkGatewayGuardsEveryRoute(t *testing.T) {
routes := [][2]string{
{"GET", "/"}, {"GET", "/v1"}, {"GET", "/v1/models"}, {"GET", "/models"}, {"GET", "/v1/models/x"},
{"GET", "/muse-code/models"}, {"GET", "/api/hello"},
+ {"GET", "/api/version"}, {"GET", "/api/tags"}, {"POST", "/api/show"},
{"GET", "/v1/magpie/quotas"}, {"GET", "/v1/magpie/quotas/history"},
{"GET", provider.RemoteCardsPath}, {"POST", provider.RemoteRefreshPath},
{"GET", "/v1/magpie/route?session=s"}, {"GET", "/v1/magpie/concurrency"}, {"GET", "/v1/magpie/limit"},
diff --git a/internal/gateway/ollama.go b/internal/gateway/ollama.go
new file mode 100644
index 000000000..56fb98a0f
--- /dev/null
+++ b/internal/gateway/ollama.go
@@ -0,0 +1,159 @@
+package gateway
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "net/http"
+ "strings"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// The Ollama API's model routes, for a client that takes a custom endpoint
+// only as an Ollama server (Visual Studio's Copilot BYOK, wani on Discord;
+// VS Code's Copilot "Ollama" provider): the user enters the gateway's own
+// address (http://127.0.0.1:3425) as the Ollama endpoint. VS Code's
+// Copilot (vscode-copilot-chat, ollamaProvider.ts) asks /api/version and
+// refuses a server older than 0.6.4, lists /api/tags and takes each
+// model's "model", asks /api/show of each (a model whose show fails or
+// has no capabilities is left out), and chats on the server's
+// OpenAI-compatible /v1/chat/completions, which magpie serves already.
+// These routes sit on Handler's mux, so lanGuard, corsGuard and the key
+// limits judge them as every route.
+
+// ollamaVersion is the Ollama release magpie answers /api/version as: one
+// past every client's minimum (VS Code's Copilot asks 0.6.4).
+const ollamaVersion = "0.12.6"
+
+func (s *Server) ollamaVersion(w http.ResponseWriter, r *http.Request) {
+ writeJSON(w, 200, map[string]any{"version": ollamaVersion})
+}
+
+// ollamaTags lists the models the agent asking is shown, as Ollama's
+// /api/tags lists the pulled ones: name and model are magpie's id, which
+// the client sends back as the model to chat with.
+func (s *Server) ollamaTags(w http.ResponseWriter, r *http.Request) {
+ models := []map[string]any{}
+ for _, e := range catalogFor(r) {
+ models = append(models, map[string]any{
+ "name": e.ID, "model": e.ID, "modified_at": ollamaModified, "size": 0,
+ "digest": ollamaDigest(e.ID), "details": ollamaDetails(e), "capabilities": ollamaCapabilities(e),
+ })
+ }
+ writeJSON(w, 200, map[string]any{"models": models})
+}
+
+// ollamaShow is one model as Ollama's /api/show tells it. A client reads
+// what it takes from capabilities, its window from model_info's
+// "<general.architecture>.context_length" (VS Code's Copilot takes 32768
+// without it) and its name from "general.basename".
+func (s *Server) ollamaShow(w http.ResponseWriter, r *http.Request) {
+ body, ok := s.requestBody(w, r, provider.Chat)
+ if !ok {
+ return
+ }
+ var req struct {
+ Model string `json:"model"`
+ Name string `json:"name"` // what clients before Ollama 0.5 send
+ }
+ if err := json.Unmarshal(body, &req); err != nil {
+ ollamaError(w, http.StatusBadRequest, "invalid request: "+err.Error())
+ return
+ }
+ id := strings.TrimSpace(req.Model)
+ if id == "" {
+ id = strings.TrimSpace(req.Name)
+ }
+ if id == "" {
+ ollamaError(w, http.StatusBadRequest, "model is required")
+ return
+ }
+ e, ok := ollamaEntry(r, id)
+ if !ok {
+ ollamaError(w, http.StatusNotFound, "model '"+id+"' not found")
+ return
+ }
+ // every key here is sent: the ollama CLI before 0.7 reads
+ // parameter_count, context_length and embedding_length without
+ // checking them and panics on one missing ("ollama show", 0.6.8)
+ window := e.Context
+ if window <= 0 {
+ // what VS Code's Copilot assumes without one; a 0 it would take
+ // as the window
+ window = ollamaDefaultContext
+ }
+ info := map[string]any{
+ "general.architecture": ollamaArch, "general.basename": provider.Labels([]provider.Entry{e})[0],
+ "general.parameter_count": 0, ollamaArch + ".context_length": window, ollamaArch + ".embedding_length": 0,
+ }
+ writeJSON(w, 200, map[string]any{
+ "modelfile": "", "parameters": "", "template": "",
+ "details": ollamaDetails(e), "model_info": info, "capabilities": ollamaCapabilities(e),
+ "modified_at": ollamaModified,
+ })
+}
+
+// ollamaCapabilities are what the model does, in Ollama's words: it
+// completes and calls tools (every model magpie serves is asked with
+// tools), sees when it takes images, and thinks when it reasons.
+func ollamaCapabilities(e provider.Entry) []string {
+ caps := []string{"completion", "tools"}
+ if e.Images {
+ caps = append(caps, "vision")
+ }
+ if e.Thinks() {
+ caps = append(caps, "thinking")
+ }
+ return caps
+}
+
+// ollamaArch is the architecture every model is told as: model_info's
+// keys are named after it.
+const ollamaArch = "magpie"
+
+// ollamaDefaultContext is the window told for a model whose own magpie
+// doesn't know: the one VS Code's Copilot takes when none is told.
+const ollamaDefaultContext = 32768
+
+// ollamaModified stands in for when a model was pulled, which no model
+// here was.
+const ollamaModified = "2025-01-01T00:00:00Z"
+
+// ollamaEntry is the model id names among those the caller is shown (a
+// gateway key held to some models is shown those alone): by its catalog
+// id, or its bare id when the user lists bare ids.
+func ollamaEntry(r *http.Request, id string) (provider.Entry, bool) {
+ shown := catalogFor(r)
+ id = unprefixed(id)
+ for _, e := range shown {
+ if e.ID == id {
+ return e, true
+ }
+ }
+ if provider.BareModelIDs() {
+ return provider.BareEntry(shown, id)
+ }
+ return provider.Entry{}, false
+}
+
+func ollamaDetails(e provider.Entry) map[string]any {
+ d := map[string]any{"parent_model": "", "format": "", "family": ollamaArch, "families": []string{ollamaArch},
+ "parameter_size": "", "quantization_level": ""}
+ if e.Context > 0 {
+ d["context_length"] = e.Context
+ }
+ return d
+}
+
+// ollamaDigest stands in for a model file's digest: the same id always
+// gives the same one.
+func ollamaDigest(id string) string {
+ sum := sha256.Sum256([]byte(id))
+ return hex.EncodeToString(sum[:])
+}
+
+// ollamaError is an error as Ollama's API says one.
+func ollamaError(w http.ResponseWriter, status int, msg string) {
+ writeJSON(w, status, map[string]any{"error": msg})
+}
diff --git a/internal/gateway/ollama_test.go b/internal/gateway/ollama_test.go
new file mode 100644
index 000000000..45ded2244
--- /dev/null
+++ b/internal/gateway/ollama_test.go
@@ -0,0 +1,277 @@
+package gateway
+
+import (
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/access"
+ "github.com/yetone/magpie/internal/provider"
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// ollamaPlan is a provider with a model that sees, thinks and has a
+// window, and one that does none of these, for the Ollama routes to tell
+// of.
+func ollamaPlan(t *testing.T, chat string) {
+ t.Helper()
+ yes, no := true, false
+ if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: chat,
+ Models: []string{"seer", "plain"}, Contexts: map[string]int{"seer": 400000}}); err != nil {
+ t.Fatal(err)
+ }
+ for _, err := range []error{
+ provider.SetModelImage("plan/seer", &yes), provider.SetModelImage("plan/plain", &no),
+ provider.SetModelEfforts("plan/seer", []string{"low", "high"}),
+ } {
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+}
+
+// A client that takes a custom endpoint only as an Ollama server (Visual
+// Studio's Copilot BYOK, wani on Discord; VS Code's Copilot Ollama
+// provider) asks /api/version, lists /api/tags and reads each model's
+// capabilities and window from /api/show, then chats on
+// /v1/chat/completions. The list is magpie's catalog by its ids, and a
+// model's window and what it takes are its catalog entry's.
+func TestOllamaModelRoutes(t *testing.T) {
+ fresh(t)
+ ollamaPlan(t, "http://127.0.0.1:1/v1")
+ h := New().Handler()
+ do := func(method, path, body string) *httptest.ResponseRecorder {
+ t.Helper()
+ r := httptest.NewRequest(method, path, strings.NewReader(body))
+ r.RemoteAddr = "127.0.0.1:50123"
+ w := httptest.NewRecorder()
+ h.ServeHTTP(w, r)
+ return w
+ }
+ if w := do("GET", "/api/version", ""); w.Code != 200 || !strings.Contains(w.Body.String(), `"version":"`+ollamaVersion+`"`) {
+ t.Fatalf("version: %d %s", w.Code, w.Body)
+ }
+ var tags struct {
+ Models []struct {
+ Name, Model, Digest string
+ Details struct{ Family string }
+ }
+ }
+ w := do("GET", "/api/tags", "")
+ if err := json.Unmarshal(w.Body.Bytes(), &tags); err != nil || w.Code != 200 {
+ t.Fatalf("tags: %d %s", w.Code, w.Body)
+ }
+ var names []string
+ for _, m := range tags.Models {
+ if m.Name != m.Model || len(m.Digest) != 64 {
+ t.Fatalf("tag %+v", m)
+ }
+ names = append(names, m.Name)
+ }
+ if !slices.Contains(names, "plan/seer") || !slices.Contains(names, "plan/plain") {
+ t.Fatalf("tags list %v", names)
+ }
+ type shown struct {
+ Capabilities []string
+ ModelInfo map[string]any `json:"model_info"`
+ }
+ show := func(body string) (int, shown) {
+ t.Helper()
+ w := do("POST", "/api/show", body)
+ var s shown
+ if w.Code == 200 {
+ if err := json.Unmarshal(w.Body.Bytes(), &s); err != nil {
+ t.Fatal(w.Body.String())
+ }
+ }
+ return w.Code, s
+ }
+ c, s := show(`{"model":"plan/seer"}`)
+ arch, _ := s.ModelInfo["general.architecture"].(string)
+ if c != 200 || !slices.Equal(s.Capabilities, []string{"completion", "tools", "vision", "thinking"}) || s.ModelInfo[arch+".context_length"] != float64(400000) ||
+ !strings.Contains(fmt.Sprint(s.ModelInfo["general.basename"]), "seer") {
+ t.Fatalf("show seer: %d %+v", c, s)
+ }
+ // a client from before Ollama 0.5 names it "name"; a window magpie
+ // doesn't know is told as the one VS Code's Copilot assumes, never 0
+ c, s = show(`{"name":"plan/plain"}`)
+ if c != 200 || !slices.Equal(s.Capabilities, []string{"completion", "tools"}) || s.ModelInfo[arch+".context_length"] != float64(32768) {
+ t.Fatalf("show plain: %d %+v", c, s)
+ }
+ // the ollama CLI before 0.7 reads these as numbers unchecked, and
+ // panics on one missing ("ollama show" with 0.6.8)
+ for _, k := range []string{"general.parameter_count", arch + ".context_length", arch + ".embedding_length"} {
+ if _, ok := s.ModelInfo[k].(float64); !ok {
+ t.Fatalf("model_info %s: %v", k, s.ModelInfo[k])
+ }
+ }
+ if c, _ := show(`{"model":"plan/nothing"}`); c != 404 {
+ t.Fatal("an unknown model:", c)
+ }
+ if c, _ := show(`{}`); c != 400 {
+ t.Fatal("no model:", c)
+ }
+}
+
+// A gateway key held to some models is listed and shown those alone on the
+// Ollama routes, as on /v1/models.
+func TestOllamaRoutesHoldAKey(t *testing.T) {
+ fresh(t)
+ ollamaPlan(t, "http://127.0.0.1:1/v1")
+ keys, secrets := newCaller(t, "Held")
+ if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"plan/plain"}}); err != nil {
+ t.Fatal(err)
+ }
+ h := New().Handler()
+ do := func(method, path, body string) *httptest.ResponseRecorder {
+ r := httptest.NewRequest(method, path, strings.NewReader(body))
+ r.Header.Set("Authorization", "Bearer "+secrets[0])
+ w := httptest.NewRecorder()
+ h.ServeHTTP(w, r)
+ return w
+ }
+ if w := do("GET", "/api/tags", ""); w.Code != 200 || !strings.Contains(w.Body.String(), "plan/plain") || strings.Contains(w.Body.String(), "plan/seer") {
+ t.Fatalf("held key's tags: %d %s", w.Code, w.Body)
+ }
+ if w := do("POST", "/api/show", `{"model":"plan/seer"}`); w.Code != 404 {
+ t.Fatalf("held key shown a model it may not use: %d %s", w.Code, w.Body)
+ }
+ if w := do("POST", "/api/show", `{"model":"plan/plain"}`); w.Code != 200 {
+ t.Fatalf("held key's own model: %d %s", w.Code, w.Body)
+ }
+}
+
+// The Ollama routes are guarded as every gateway route is, through the
+// real server stack: with no key, a page elsewhere, a sandboxed frame
+// (Origin null), a page at a localhost subdomain, a page rebound to
+// loopback under its own name, and another machine while magpie isn't
+// shared or sends none are all refused, and none is told a model. An
+// agent on this computer, and a keyed call, are answered.
+func TestOllamaRoutesGuarded(t *testing.T) {
+ fresh(t)
+ t.Setenv("MAGPIE_ADDR", "")
+ t.Setenv("MAGPIE_PUBLIC_URL", "")
+ t.Setenv("MAGPIE_TRUST_PROXY", "")
+ ollamaPlan(t, "http://127.0.0.1:1/v1")
+ srv := lanGuard(New().Handler())
+ routes := []struct{ method, path, body string }{
+ {"GET", "/api/version", ""},
+ {"GET", "/api/tags", ""},
+ {"POST", "/api/show", `{"model":"plan/seer"}`},
+ }
+ const gw = "127.0.0.1:3425"
+ browser := []string{"User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15",
+ "Sec-Fetch-Mode", "no-cors", "Content-Type", "text/plain;charset=UTF-8"}
+ do := func(method, path, body, from, host string, hdr ...string) *httptest.ResponseRecorder {
+ t.Helper()
+ r := httptest.NewRequest(method, path, strings.NewReader(body))
+ r.RemoteAddr, r.Host = from, host
+ for i := 0; i+1 < len(hdr); i += 2 {
+ r.Header.Set(hdr[i], hdr[i+1])
+ }
+ w := httptest.NewRecorder()
+ srv.ServeHTTP(w, r)
+ return w
+ }
+ refused := func(what string, w *httptest.ResponseRecorder, codes ...int) {
+ t.Helper()
+ if !slices.Contains(codes, w.Code) || strings.Contains(w.Body.String(), "plan/") || w.Header().Get("Access-Control-Allow-Origin") != "" {
+ t.Errorf("%s: %d %s", what, w.Code, w.Body)
+ }
+ }
+ for _, rt := range routes {
+ at := rt.method + " " + rt.path
+ // pages on this computer's browser, with no key
+ refused(at+" from a page elsewhere", do(rt.method, rt.path, rt.body, "127.0.0.1:50123", gw, append([]string{"Origin", "https://evil.example", "Sec-Fetch-Site", "cross-site"}, browser...)...), http.StatusForbidden)
+ refused(at+" from Origin null", do(rt.method, rt.path, rt.body, "127.0.0.1:50123", gw, append([]string{"Origin", "null", "Sec-Fetch-Site", "cross-site"}, browser...)...), http.StatusForbidden)
+ refused(at+" from a localhost subdomain", do(rt.method, rt.path, rt.body, "127.0.0.1:50123", gw, append([]string{"Origin", "http://evil.localhost", "Sec-Fetch-Site", "cross-site"}, browser...)...), http.StatusForbidden)
+ // rebound: no Origin on a GET, the page's own on a POST
+ hdr := append([]string{"Sec-Fetch-Site", "same-origin"}, browser...)
+ if rt.method == "POST" {
+ hdr = append(hdr, "Origin", "http://rebind.attacker.example:3425")
+ }
+ refused(at+" rebound", do(rt.method, rt.path, rt.body, "127.0.0.1:50123", "rebind.attacker.example:3425", hdr...), http.StatusForbidden)
+ refused(at+" rebound at a localhost subdomain", do(rt.method, rt.path, rt.body, "127.0.0.1:50123", "evil.localhost:3425", hdr...), http.StatusForbidden)
+ // another machine, no Origin at all: magpie not shared
+ refused(at+" from the LAN, not shared", do(rt.method, rt.path, rt.body, "192.168.1.9:5000", "192.168.1.2:3425"), http.StatusForbidden)
+ // a tunnel on loopback
+ refused(at+" through a tunnel, not shared", do(rt.method, rt.path, rt.body, "127.0.0.1:50123", gw, "X-Forwarded-For", "203.0.113.9"), http.StatusForbidden)
+ // an agent here, as VS Code's Copilot calls it: answered
+ if w := do(rt.method, rt.path, rt.body, "127.0.0.1:50123", gw, "User-Agent", "node-fetch"); w.Code != 200 {
+ t.Errorf("%s from an agent here: %d %s", at, w.Code, w.Body)
+ }
+ }
+ // shared: another machine needs an enabled key
+ _, secrets := newCaller(t, "Visual Studio")
+ for _, rt := range routes {
+ at := rt.method + " " + rt.path
+ for _, hdr := range [][]string{nil, {"Authorization", "Bearer magpie"}, {"Authorization", "Bearer sk-magpie-wrong"}} {
+ resetAuthThrottle()
+ refused(at+" from the LAN with "+strings.Join(hdr, " "), do(rt.method, rt.path, rt.body, "192.168.1.9:5000", "192.168.1.2:3425", hdr...), http.StatusUnauthorized)
+ }
+ if w := do(rt.method, rt.path, rt.body, "192.168.1.9:5000", "192.168.1.2:3425", "Authorization", "Bearer "+secrets[0]); w.Code != 200 {
+ t.Errorf("%s from the LAN with its key: %d %s", at, w.Code, w.Body)
+ }
+ // a sandboxed frame or a rebound page with an enabled key is
+ // answered, as on /v1
+ if w := do(rt.method, rt.path, rt.body, "127.0.0.1:50123", gw, append([]string{"Origin", "null", "Authorization", "Bearer " + secrets[0]}, browser...)...); w.Code != 200 {
+ t.Errorf("%s from Origin null with a key: %d %s", at, w.Code, w.Body)
+ }
+ }
+ settings.Save(settings.Settings{LAN: false})
+}
+
+// VS Code's Copilot chats with a model it listed from /api/tags on
+// <endpoint>/v1/chat/completions, streamed, with an empty bearer (its
+// Ollama provider has no key field) and stream_options it adds itself
+// (openAIEndpoint.ts interceptBody): the request reaches the model's
+// provider by the id the list gave, and the stream comes back.
+func TestOllamaClientChats(t *testing.T) {
+ fresh(t)
+ var got []string
+ up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ b, _ := io.ReadAll(r.Body)
+ var req struct{ Model string }
+ json.Unmarshal(b, &req)
+ got = append(got, r.URL.Path+" "+req.Model+" "+r.Header.Get("Authorization"))
+ w.Header().Set("Content-Type", "text/event-stream")
+ io.WriteString(w, "data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"hello from seer\"},\"finish_reason\":null}]}\n\n"+
+ "data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":5,\"completion_tokens\":3}}\n\ndata: [DONE]\n\n")
+ }))
+ defer up.Close()
+ ollamaPlan(t, up.URL+"/v1")
+ srv := lanGuard(New().Handler())
+ var tags struct{ Models []struct{ Model string } }
+ r := httptest.NewRequest("GET", "/api/tags", nil)
+ r.RemoteAddr, r.Host = "127.0.0.1:50123", "127.0.0.1:3425"
+ w := httptest.NewRecorder()
+ srv.ServeHTTP(w, r)
+ if err := json.Unmarshal(w.Body.Bytes(), &tags); err != nil || len(tags.Models) == 0 {
+ t.Fatalf("tags: %d %s", w.Code, w.Body)
+ }
+ model := ""
+ for _, m := range tags.Models {
+ if strings.HasSuffix(m.Model, "/seer") {
+ model = m.Model
+ }
+ }
+ body := `{"messages":[{"role":"system","content":"You are an AI programming assistant."},{"role":"user","content":"hi"}],"model":"` + model +
+ `","temperature":0.1,"top_p":1,"n":1,"stream":true,"tools":[{"type":"function","function":{"name":"read_file","description":"Read a file","parameters":{"type":"object","properties":{"path":{"type":"string"}}}}}],"stream_options":{"include_usage":true}}`
+ r = httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
+ r.RemoteAddr, r.Host = "127.0.0.1:50123", "127.0.0.1:3425"
+ r.Header.Set("Content-Type", "application/json")
+ r.Header.Set("Authorization", "Bearer ")
+ w = httptest.NewRecorder()
+ srv.ServeHTTP(w, r)
+ if w.Code != 200 || !strings.Contains(w.Body.String(), "hello from seer") {
+ t.Fatalf("chat: %d %s", w.Code, w.Body)
+ }
+ if len(got) != 1 || got[0] != "/v1/chat/completions seer Bearer upstream-secret" {
+ t.Fatalf("upstream got %q", got)
+ }
+}
diff --git a/site/public/docs/integrate.html b/site/public/docs/integrate.html
index e34b9c2da..1ff43e88a 100644
--- a/site/public/docs/integrate.html
+++ b/site/public/docs/integrate.html
@@ -108,6 +108,7 @@ curl -s http://127.0.0.1:3425/v1/models -H "Authorization: Bearer magpie-acme"</
<tr><td><code>/v1/messages</code>, <code>/v1/messages/count_tokens</code></td><td>Anthropic Messages</td></tr>
<tr><td><code>/v1beta/models/{model}:generateContent</code> (and <code>:streamGenerateContent</code>)</td><td>Google Gemini</td></tr>
<tr><td><code>/v1/models</code></td><td>the catalog</td></tr>
+ <tr><td><code>/api/version</code>, <code>/api/tags</code>, <code>/api/show</code></td><td>the catalog as an Ollama server lists it, for a client that takes only an Ollama endpoint (Copilot in Visual Studio and VS Code: enter <code>http://127.0.0.1:&lt;port&gt;</code> as the Ollama URL; it chats on <code>/v1/chat/completions</code>, and <code>/api/chat</code> isn’t served)</td></tr>
</table></div>
<h3>The key names your app</h3>
diff --git a/site/public/docs/zh/integrate.html b/site/public/docs/zh/integrate.html
index 3b1e1db81..4ea3cae2f 100644
--- a/site/public/docs/zh/integrate.html
+++ b/site/public/docs/zh/integrate.html
@@ -108,6 +108,7 @@ curl -s http://127.0.0.1:3425/v1/models -H "Authorization: Bearer magpie-acme"</
<tr><td><code>/v1/messages</code>、<code>/v1/messages/count_tokens</code></td><td>Anthropic Messages</td></tr>
<tr><td><code>/v1beta/models/{model}:generateContent</code>(以及 <code>:streamGenerateContent</code>)</td><td>Google Gemini</td></tr>
<tr><td><code>/v1/models</code></td><td>模型目录</td></tr>
+ <tr><td><code>/api/version</code>、<code>/api/tags</code>、<code>/api/show</code></td><td>以 Ollama 服务器的格式列出模型目录,供只接受 Ollama 端点的客户端使用(Visual Studio 和 VS Code 里的 Copilot:Ollama 地址填 <code>http://127.0.0.1:&lt;端口&gt;</code>;对话走 <code>/v1/chat/completions</code>,不提供 <code>/api/chat</code>)</td></tr>
</table></div>
<h3>用 key 标明你的应用</h3>