diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 8fd6e8d68..9bce818bf 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -360,7 +360,7 @@ function toneCheck(img) { swap(); }, { once: true }); } -const pngIcons = new Set(["crush", "zcode", "alma", "hanako", "cindy", "lody", "typesafe", "atomcode", "snow", "mlx-serve", "mothx", "zoo-code", "kimi-code-desktop", "openbot", "avibe", "kun"]); +const pngIcons = new Set(["crush", "zcode", "alma", "hanako", "cindy", "lody", "typesafe", "atomcode", "snow", "mlx-serve", "gpustack", "mothx", "zoo-code", "kimi-code-desktop", "openbot", "avibe", "kun"]); // lazy: a picture in a long list (Plugins' rows) is asked for near the // screen: each SVG an draws is a ~0.6 MB webview document, kept (#909) diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 484a27702..f3791d3bc 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -2838,6 +2838,7 @@ const I18N = { "Where the server listens; change the port, or give another computer's address": "服务监听的地址;可改端口,或填另一台电脑的地址", "Address: {v} isn't an address like http://localhost:11434": "地址:{v} 不是形如 http://localhost:11434 的地址", "your resource's endpoint and key": "你的资源接口地址和密钥", + "your own server and API key": "你自己的服务器和 API 密钥", "Your resource's endpoint, from Keys and Endpoint in the Azure portal. Once added, magpie asks the resource for its deployments with this key, over its v1 API; the deployments' names are the model ids.": "资源的接口地址,见 Azure 门户的「密钥和终结点」。添加后,magpie 会用这把 key 通过 v1 API 查询该资源的部署;部署名称就是模型 ID。", "No need to type them: once added, magpie lists the deployments this key can call. Type a deployment's name as a model id only if that list can't be fetched.": "不用手填:添加后 magpie 会列出这把 key 能调用的部署。只有列表拉取失败时,才把部署名称当作模型 ID 填写。", "Asking your resource for its deployments with this key…": "正在用这把 key 查询该资源的部署…", @@ -7836,6 +7837,7 @@ const I18N = { "Where the server listens; change the port, or give another computer's address": "伺服器監聽的位址;可改連接埠,或填另一臺電腦的位址", "Address: {v} isn't an address like http://localhost:11434": "位址:{v} 不是 http://localhost:11434 這樣的位址", "your resource's endpoint and key": "你的資源端點和金鑰", + "your own server and API key": "你自己的伺服器和 API 金鑰", "Your resource's endpoint, from Keys and Endpoint in the Azure portal. Once added, magpie asks the resource for its deployments with this key, over its v1 API; the deployments' names are the model ids.": "資源端點,見 Azure 入口網站的「金鑰和端點」。新增後,magpie 會用這把 key 透過 v1 API 查詢該資源的部署;部署名稱就是模型 ID。", "No need to type them: once added, magpie lists the deployments this key can call. Type a deployment's name as a model id only if that list can't be fetched.": "不用手動填寫:新增後 magpie 會列出這把 key 能呼叫的部署。只有清單取得失敗時,才把部署名稱當作模型 ID 填寫。", "Asking your resource for its deployments with this key…": "正在用這把 key 查詢該資源的部署…", @@ -12812,6 +12814,7 @@ const I18N = { "Where the server listens; change the port, or give another computer's address": "サーバーが待ち受けるアドレスです。ポートを変えるか、別のコンピューターのアドレスを入力します", "Address: {v} isn't an address like http://localhost:11434": "アドレス:{v} は http://localhost:11434 のようなアドレスではありません", "your resource's endpoint and key": "リソースのエンドポイントとキー", + "your own server and API key": "自分のサーバーと API キー", "Your resource's endpoint, from Keys and Endpoint in the Azure portal. Once added, magpie asks the resource for its deployments with this key, over its v1 API; the deployments' names are the model ids.": "リソースのエンドポイントです。Azure ポータルの「キーとエンドポイント」で確認できます。追加すると、magpie はこのキーで v1 API からリソースのデプロイを取得します。デプロイ名がモデル ID です。", "No need to type them: once added, magpie lists the deployments this key can call. Type a deployment's name as a model id only if that list can't be fetched.": "入力は不要です。追加すると、magpie はこのキーで呼び出せるデプロイを一覧にします。一覧を取得できない場合にのみ、デプロイ名をモデル ID として入力してください。", "Asking your resource for its deployments with this key…": "このキーでリソースのデプロイを取得しています…", @@ -17819,6 +17822,7 @@ const I18N = { "Where the server listens; change the port, or give another computer's address": "Wo der Server lauscht; ändern Sie den Port oder geben Sie die Adresse eines anderen Computers an", "Address: {v} isn't an address like http://localhost:11434": "Adresse: {v} ist keine Adresse wie http://localhost:11434", "your resource's endpoint and key": "Endpunkt und Schlüssel Ihrer Ressource", + "your own server and API key": "Ihr eigener Server und API-Schlüssel", "Your resource's endpoint, from Keys and Endpoint in the Azure portal. Once added, magpie asks the resource for its deployments with this key, over its v1 API; the deployments' names are the model ids.": "Der Endpunkt Ihrer Ressource, unter „Schlüssel und Endpunkt“ im Azure-Portal. Nach dem Hinzufügen fragt magpie mit diesem Schlüssel über die v1-API die Bereitstellungen der Ressource ab; deren Namen sind die Modell-IDs.", "No need to type them: once added, magpie lists the deployments this key can call. Type a deployment's name as a model id only if that list can't be fetched.": "Keine Eingabe nötig: Nach dem Hinzufügen listet magpie die Bereitstellungen auf, die dieser Schlüssel aufrufen kann. Geben Sie den Namen einer Bereitstellung nur dann als Modell-ID ein, wenn diese Liste nicht abgerufen werden kann.", "Asking your resource for its deployments with this key…": "Bereitstellungen Ihrer Ressource werden mit diesem Schlüssel abgefragt…", diff --git a/internal/gui/assets/icons/gpustack.png b/internal/gui/assets/icons/gpustack.png new file mode 100644 index 000000000..3fc8fe141 Binary files /dev/null and b/internal/gui/assets/icons/gpustack.png differ diff --git a/internal/gui/tests/gpustack-add.test.cjs b/internal/gui/tests/gpustack-add.test.cjs new file mode 100644 index 000000000..78898a50d --- /dev/null +++ b/internal/gui/tests/gpustack-add.test.cjs @@ -0,0 +1,90 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// GPUStack is the user's own server: a tile in the Add sheet whose editor asks +// for the Server address (empty, http://localhost its placeholder) and, unlike +// Ollama, a key that is not marked optional. Add sends the address's /v1 as +// the chat URL and the key; the tile shows its logo and its note in each +// language. English, Chinese (Simplified and Traditional), Japanese and +// German; no backend, the API is faked here. +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); + +const assets = path.resolve(__dirname, "../assets"); +const presets = [ + { id: "ollama", name: "Ollama", icon: "ollama", kind: "local", chat: "http://localhost:11434/v1", noKey: true, note: "your local models", added: false }, + { id: "gpustack", name: "GPUStack", icon: "gpustack", kind: "local", chat: "http://localhost/v1", note: "your own server and API key", added: false }, +]; +const note = { en: "your own server and API key", zh: "你自己的服务器和 API 密钥", "zh-TW": "你自己的伺服器和 API 金鑰", ja: "自分のサーバーと API キー", de: "Ihr eigener Server und API-Schlüssel" }; + +// one provider already added, so the list is not the empty welcome +const base = { + icon: "generic", chat: "", responses: "", anthropic: "", catalog: "", + models: [{ id: "qwen3", name: "Qwen3", on: true }], agents: [], fallback: [], headers: {}, + key: { set: false, masked: "", optional: true }, keyList: [], balanceToken: { takes: false, set: false }, proxy: "", searches: false, unredacted: false, +}; +const home = { ...base, id: "ollama", name: "Ollama", icon: "ollama", preset: "ollama", host: "localhost:11434", chat: "http://localhost:11434/v1" }; + +function server(lang, saves) { + const list = { providers: [home], presets, excluded: [], gateway: { running: true, window: true } }; + return async (route) => { + const url = new URL(route.request().url()); + const json = (data) => route.fulfill({ json: data }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/providers") return json(list); + if (url.pathname === "/api/provider/save") { saves.push(route.request().postDataJSON()); return json(list); } + if (url.pathname === "/api/groups") return json({ groups: [], pools: [] }); + if (url.pathname === "/api/gateway/trace") return json({ routes: [] }); + if (url.pathname.startsWith("/api/")) return json({}); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); } + }; +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(engine + ": GPUStack is added at its server's address with a key", async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const lang of Object.keys(note)) { + for (const width of [900, 440]) { + await t.test(`${lang} ${width}`, async () => { + const saves = []; + const page = await (await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", server(lang, saves)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator("#addProvider").click(); + const tile = page.locator("#addSheet .tile", { hasText: /GPUStack/ }); + assert.equal(await tile.count(), 1); + assert.equal(await tile.locator("img").first().getAttribute("src"), "icons/gpustack.png"); + assert.equal(await tile.getAttribute("title") === null, false); + assert.match(await tile.getAttribute("title"), new RegExp(note[lang].replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))); + await tile.click(); + const ed = page.locator(".editor"); + await ed.locator(".ehead b", { hasText: "GPUStack" }).waitFor(); + const address = ed.locator("input.address"); + assert.equal(await address.getAttribute("placeholder"), "http://localhost"); + const key = ed.locator("input[type=password]").first(); + assert.equal(await key.getAttribute("placeholder"), { en: "paste an API key" }[lang] ?? await key.getAttribute("placeholder")); + assert.notEqual(await key.getAttribute("placeholder"), { en: "optional for local servers" }.en, "its key is not marked optional"); + await address.fill("192.168.1.9:8080"); + await key.fill("gpustack_abc_secret"); + await ed.locator(".bar button").last().click(); + for (let i = 0; i < 50 && !saves.length; i++) await page.waitForTimeout(50); + assert.equal(saves.length, 1); + assert.equal(saves[0].preset, "gpustack"); + assert.equal(saves[0].chat, "http://192.168.1.9:8080/v1"); + assert.equal(saves[0].key, "gpustack_abc_secret"); + assert.deepEqual(errors, []); + }); + } + } + }); +} diff --git a/internal/provider/gpustack_test.go b/internal/provider/gpustack_test.go new file mode 100644 index 000000000..67069a958 --- /dev/null +++ b/internal/provider/gpustack_test.go @@ -0,0 +1,81 @@ +package provider + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) + +// GPUStack (github.com/gpustack/gpustack) is the user's own server: its +// OpenAI-compatible API is at /v1 with a Bearer key, so it is a +// local-kind preset (the address is the user's) that, unlike Ollama, wants +// a key; GET /v1/models lists the ready models. +func TestGPUStackPreset(t *testing.T) { + pr := Preset("gpustack") + if pr == nil { + t.Fatal("no gpustack preset") + } + if pr.Kind != KindLocal || pr.NoKey || pr.Chat != "http://localhost/v1" { + t.Fatalf("preset: %+v", pr) + } + at, err := AtAddress(*pr, "gpustack.example.com:8080") + if err != nil || at.Chat != "http://gpustack.example.com:8080/v1" { + t.Fatalf("at address: %q %v", at.Chat, err) + } + // a server on the internet is not asked with no key + p, err := FromPreset("gpustack") + if err != nil { + t.Fatal(err) + } + p.Chat = "https://gpustack.example.com/v1" + if keyOptional(p) { + t.Fatal("a GPUStack at a public host needs its key") + } +} + +// The list is GPUStack's own: routes/openai.py's SyncPage of OAIModel, +// {"object":"list","data":[{"id","object":"model","created","owned_by":"gpustack"}]}, +// asked with the key as a Bearer. +func TestGPUStackModels(t *testing.T) { + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/v1/models" { + http.NotFound(w, r) + return + } + if r.Header.Get("Authorization") != "Bearer gpustack_abc_secret" { + w.WriteHeader(401) + w.Write([]byte(`{"detail":"Invalid authentication credentials"}`)) + return + } + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(`{"object":"list","data":[` + + `{"id":"qwen3","object":"model","created":1760000000,"owned_by":"gpustack"},` + + `{"id":"deepseek-r1-distill-qwen-7b","object":"model","created":1760000001,"owned_by":"gpustack"}]}`)) + })) + defer srv.Close() + p, err := FromPreset("gpustack") + if err != nil { + t.Fatal(err) + } + if err := p.AtAddress(srv.URL); err != nil { + t.Fatal(err) + } + p.Key = "gpustack_abc_secret" + models, err := p.Fetch(context.Background()) + if err != nil { + t.Fatal(err) + } + var ids []string + for _, m := range models { + ids = append(ids, m.ID) + } + if len(ids) != 2 || ids[0] != "qwen3" || ids[1] != "deepseek-r1-distill-qwen-7b" { + t.Fatalf("models: %v", ids) + } + p.Key = "wrong" + if _, err := p.Fetch(context.Background()); err == nil { + t.Fatal("a wrong key listed models") + } +} diff --git a/internal/provider/mlxserve_decide.go b/internal/provider/mlxserve_decide.go index 1541555d6..628d7901d 100644 --- a/internal/provider/mlxserve_decide.go +++ b/internal/provider/mlxserve_decide.go @@ -12,6 +12,9 @@ import ( // MLXServePreset is the MLX-Serve preset's id. const MLXServePreset = "mlx-serve" +// GPUStackPreset is GPUStack's preset id. +const GPUStackPreset = "gpustack" + // MLX-Serve (github.com/ddalcu/mlx-serve) serves decision models — // Clef, Clef-Flash, Kev, Laya, D1 — at POST /v1/systemone (and /v1/decisions), // System One's questions and answers. Its /v1/models lists them alongside diff --git a/internal/provider/presets.go b/internal/provider/presets.go index 3b2c169c2..9719a116a 100644 --- a/internal/provider/presets.go +++ b/internal/provider/presets.go @@ -724,6 +724,14 @@ var presets = []PresetDef{ Chat: "http://localhost:8000/v1", Responses: "http://localhost:8000/v1", Anthropic: "http://localhost:8000", Decide: "http://localhost:8000/v1", Note: "local server, port 8000 by default", Website: "https://omlx.ai"}, + // GPUStack (github.com/gpustack/gpustack), a self-hosted model server: + // its OpenAI-compatible API is at /v1 (/v1-openai is the legacy + // path), a Bearer API key from its UI, and GET /v1/models lists the + // ready models the key may use. The server is the user's own: port 80 by + // default, on this machine or another. + {ID: GPUStackPreset, Name: "GPUStack", Icon: "gpustack", Kind: KindLocal, + Chat: "http://localhost/v1", + Note: "your own server and API key", Website: "https://github.com/gpustack/gpustack"}, // its decision models (Clef, Clef-Flash, Kev, Laya, D1), served at // /v1/systemone, are marked by capabilities in /v1/models (mlxserve_decide.go) {ID: MLXServePreset, Name: "MLX-Serve", Icon: "mlx-serve", Kind: KindLocal, NoKey: true,