mengxiangziyou on X: "供应商里的LM Studio只能用本地的,怎么用局域网里其他机器 上的LM Studio, baseUrl 不能改". Moving a local preset (Ollama, LM Studio, oMLX, MLX-Serve) to another host has worked since #1165, but the window put the Address in a "Server address" fold under the key, while the Endpoints row showed http://localhost:1234/v1 as plain text. Reproduced in a sandbox: Add > LM Studio shows the URL and nothing to change it with unless the small fold is found. - GUI: the Server address field is shown first, above the key, never folded, for a new provider and a saved one alike (its label and hint are the strings the fold used, so no new i18n keys). Saving still sends the URLs moved only when the address was changed. - CLI: `magpie provider add|set <id> address=<host:port>` moves every URL the provider has (chat, Anthropic, Responses, decision), each keeping its path; from the preset's when it was saved with none, and `address=` alone puts a local server back at its preset's. A provider of the user's own moves its own URLs. Vertex refuses it as it does url=. Before, Ollama on another computer needed url= and anthropic=. - TUI: unchanged (asked on adding, `w` after). - docs/subsystems/providers-accounts.md: Local servers row. Security: only upstream URLs move. The gateway's own loopback/origin guards are about inbound requests and don't read them; the one rule that looks at an upstream being on the LAN (Unredacted) stays the user's own switch. Run at this tree (origin/main642180a80+ this change; rebased onto2f44e075a, which touches none of these files, then build, the new CLI tests and local-address.test.cjs run again there: ok): - New TestProviderAddressMovesALocalServer and TestProviderAddressPairs (internal/cli): fail on the old providers_cli.go with `unknown field "address"`, pass with it. - local-address.test.cjs (both engines, en/zh/zh-TW/ja/de, 900 and 440px): with the old app.js 3 subtests fail at "shown when added, nothing to open"; 22/22 pass with it. - GUI files add-sheet, decide-endpoint-label, local-address, nokey-editor, provider-insecure-tls, remote-magpie, provider-unmasked, i18n-keys, gui-de, gui-ja, gui-zh-tw: 161/161 pass. - go test -tags nogui -timeout 45m ./internal/cli ./internal/provider ./internal/tui ./internal/gui in a sandbox HOME: all ok. - gofmt -l clean; go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows go vet on internal/cli; go build, GOOS=linux go build -tags nogui, GOOS=windows go build: ok. - Real use, sandbox HOME, built binary (-tags dev): a fake LM Studio bound to the Mac's LAN address 10.0.0.111:41234. The window's Add > LM Studio showed the field; typing 10.0.0.111:41234 and Add saved chat http://10.0.0.111:41234/v1, listed lmstudio/lan-qwen-9b from that host (GET /v1/models there), and a chat completion through the gateway reached it. `provider set lmstudio address=` put it back at http://localhost:1234/v1; `address=10.0.0.111:41234` moved it again and the next request went there. Not run: go test ./... over every package (other sessions' suites on this machine, load ~60), the whole GUI suite, a real LM Studio on a second machine, the Windows and Linux apps.
91 lines
3.2 KiB
Go
91 lines
3.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// An LM Studio on another computer (mengxiangziyou on X: "baseUrl 不能改"):
|
|
// address= puts the preset's provider there, its models listed from that
|
|
// server, and address= alone puts it back at the preset's port.
|
|
func TestProviderAddressMovesALocalServer(t *testing.T) {
|
|
var mu sync.Mutex
|
|
var asked []string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
asked = append(asked, r.Method+" "+r.URL.Path)
|
|
mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if r.URL.Path == "/v1/models" {
|
|
w.Write([]byte(`{"object":"list","data":[{"id":"qwen3.5-9b-mlx","object":"model","owned_by":"organization_owner"}]}`))
|
|
return
|
|
}
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
if err := addProvider([]string{"lmstudio", "address=" + strings.TrimPrefix(srv.URL, "http://")}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := provider.Find("lmstudio")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Chat != srv.URL+"/v1" {
|
|
t.Fatalf("chat %q, want %s/v1", p.Chat, srv.URL)
|
|
}
|
|
mu.Lock()
|
|
got := strings.Join(asked, ",")
|
|
mu.Unlock()
|
|
if !strings.Contains(got, "GET /v1/models") {
|
|
t.Fatalf("its models were not asked of the server it was moved to: %q", got)
|
|
}
|
|
if ids := p.Exposed(); len(ids) != 1 || ids[0].ID != "qwen3.5-9b-mlx" {
|
|
t.Fatalf("models %+v, want the server's", ids)
|
|
}
|
|
|
|
// back where the preset puts it
|
|
if err := providerCmd([]string{"provider", "set", "lmstudio", "address="}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, _ = provider.Find("lmstudio"); p.Chat != "http://localhost:1234/v1" {
|
|
t.Fatalf("chat %q after address=, want the preset's", p.Chat)
|
|
}
|
|
}
|
|
|
|
// Every URL of a local server moves with its address, each keeping its
|
|
// path: Ollama's chat, Anthropic and decision APIs, from the preset's when
|
|
// it was saved with none; anything that is no address is refused, the URLs
|
|
// left as they were.
|
|
func TestProviderAddressPairs(t *testing.T) {
|
|
p := provider.Provider{ID: "ollama", Name: "Ollama", Preset: "ollama"}
|
|
if err := applyPairs(&p, []string{"address=192.168.1.5:11434"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Chat != "http://192.168.1.5:11434/v1" || p.Anthropic != "http://192.168.1.5:11434" || p.Decide != "http://192.168.1.5:11434/v1" {
|
|
t.Fatalf("not moved: %+v", p)
|
|
}
|
|
if err := applyPairs(&p, []string{"address=https://studio.lan:8443/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Chat != "https://studio.lan:8443/v1" || p.Anthropic != "https://studio.lan:8443" {
|
|
t.Fatalf("not moved again: %+v", p)
|
|
}
|
|
if err := applyPairs(&p, []string{"address=ftp://box"}); err == nil || p.Chat != "https://studio.lan:8443/v1" {
|
|
t.Fatalf("ftp://box: %v, chat %q", err, p.Chat)
|
|
}
|
|
// a provider of the user's own moves its own URLs
|
|
c := provider.Provider{Name: "vLLM box", Chat: "http://10.0.0.7:8000/v1"}
|
|
if err := applyPairs(&c, []string{"address=10.0.0.8:8001"}); err != nil || c.Chat != "http://10.0.0.8:8001/v1" {
|
|
t.Fatalf("custom: %v, chat %q", err, c.Chat)
|
|
}
|
|
if err := applyPairs(&c, []string{"address="}); err == nil {
|
|
t.Fatal("an empty address of a provider with no preset's to go back to was taken")
|
|
}
|
|
}
|