diff --git a/internal/plugin/github.go b/internal/plugin/github.go index a12319640..c8bb36a2b 100644 --- a/internal/plugin/github.go +++ b/internal/plugin/github.go @@ -407,6 +407,18 @@ func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, miss return l, whole, missing, nil } +// keyword is whether npm's keywords (a list of strings, anything else +// none) hold k +func keyword(kw any, k string) bool { + l, _ := kw.([]any) + for _, v := range l { + if s, ok := v.(string); ok && strings.EqualFold(strings.TrimSpace(s), k) { + return true + } + } + return false +} + // readTagged reads a tagged repository's package.json (on branch), and // npm's copy of the package it names: t as it is installed, whether it is // kept, and whether that is known (GitHub or npm not answering is not, @@ -428,7 +440,9 @@ func readTagged(pc context.Context, t Tagged, branch string) (Tagged, bool, bool Main string `json:"main"` Exports json.RawMessage `json:"exports"` License any `json:"license"` - Magpie struct { + // npm's keywords, any shape: a list that isn't one names none + Keywords any `json:"keywords"` + Magpie struct { Middleware string `json:"middleware"` Agent string `json:"agent"` Hooks string `json:"hooks"` @@ -469,7 +483,12 @@ func readTagged(pc context.Context, t Tagged, branch string) (Tagged, bool, bool // (plugins#737) case strings.TrimSpace(pj.Magpie.Agent) != "": t.Kind = "agent" - case !runs && strings.TrimSpace(pj.Magpie.Page.File) != "": + // a page with a module of its own (the local service its page + // talks to, magpie.page.service) is a page when it says so in its + // keywords, as every page in the plugins repository does; a + // provider or middleware with a page of settings beside it says + // "magpie-page" nowhere and stays what it is (plugins#1226) + case strings.TrimSpace(pj.Magpie.Page.File) != "" && (!runs || keyword(pj.Keywords, "magpie-page")): t.Kind = "page" // gateway hooks alone (magpie.hooks) need no entry of their // own, as the host loads them: the hooks file is the one to diff --git a/internal/plugin/github_page_test.go b/internal/plugin/github_page_test.go new file mode 100644 index 000000000..c08b78668 --- /dev/null +++ b/internal/plugin/github_page_test.go @@ -0,0 +1,66 @@ +package plugin + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "slices" + "strings" + "sync" + "testing" +) + +// A page whose page talks to a local service of its own ships that +// service as its main (magpie.page.service). The GitHub list read a +// package with a main as a provider, so xiaopeng66/magpie-model-lab +// showed under Providers (plugins#1226). A page that says +// "magpie-page" in its keywords, as every page in the plugins +// repository does, is a page, main or not; the file looked for is still +// its main. A provider with a page of settings beside it says no such +// keyword and stays a provider. The package.json is model-lab's, as it +// is, without its 900KB icon. +func TestTaggedPageWithMain(t *testing.T) { + var mu sync.Mutex + var asked []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, q *http.Request) { + mu.Lock() + asked = append(asked, q.URL.Path) + mu.Unlock() + name, ok := strings.CutPrefix(q.URL.Path, "/raw/") + f, err := os.ReadFile(filepath.Join("testdata/github/raw", filepath.FromSlash(name))) + if !ok || err != nil { + http.Error(w, `"Not Found"`, 404) + return + } + w.Write(f) + })) + t.Cleanup(srv.Close) + api, raw, reg := githubAPI, githubRaw, npmRegistry + githubAPI, githubRaw, npmRegistry = srv.URL, srv.URL+"/raw", srv.URL+"/npm" + t.Cleanup(func() { githubAPI, githubRaw, npmRegistry = api, raw, reg }) + + const repo = "xiaopeng66/magpie-model-lab" + got, keep, known, err := readTagged(context.Background(), Tagged{Spec: "github:" + repo, Repo: repo}, "main") + if !keep || !known || err != nil || got.Kind != "page" || got.Package != "magpie-pelican-bench" { + t.Fatalf("a page with a main: kind %q, package %q, kept %v, known %v, %v", got.Kind, got.Package, keep, known, err) + } + mu.Lock() + a := slices.Clone(asked) + mu.Unlock() + if !slices.Contains(a, "/raw/"+repo+"/main/index.mjs") { + t.Fatalf("its main wasn't looked for: asked %v", a) + } + + got, keep, _, err = readTagged(context.Background(), Tagged{Spec: "github:someone/provider-with-page", Repo: "someone/provider-with-page"}, "main") + if !keep || err != nil || got.Kind == "page" { + t.Fatalf("a provider with a settings page: kind %q, kept %v, %v", got.Kind, keep, err) + } + + // a page alone, no main, as before + got, keep, _, err = readTagged(context.Background(), Tagged{Spec: "github:someone/page-glance", Repo: "someone/page-glance"}, "main") + if !keep || err != nil || got.Kind != "page" { + t.Fatalf("a page alone: %+v, kept %v, %v", got, keep, err) + } +} diff --git a/internal/plugin/testdata/github/raw/someone/provider-with-page/main/index.mjs b/internal/plugin/testdata/github/raw/someone/provider-with-page/main/index.mjs new file mode 100644 index 000000000..b1c6ea436 --- /dev/null +++ b/internal/plugin/testdata/github/raw/someone/provider-with-page/main/index.mjs @@ -0,0 +1 @@ +export default {} diff --git a/internal/plugin/testdata/github/raw/someone/provider-with-page/main/package.json b/internal/plugin/testdata/github/raw/someone/provider-with-page/main/package.json new file mode 100644 index 000000000..6276afff4 --- /dev/null +++ b/internal/plugin/testdata/github/raw/someone/provider-with-page/main/package.json @@ -0,0 +1,9 @@ +{ + "name": "opencode-provider-with-page", + "version": "0.1.0", + "main": "./index.mjs", + "keywords": ["opencode", "opencode-plugin"], + "magpie": { + "page": { "file": "page/index.html", "service": { "port": 3478, "option": "diagnosticsPort" } } + } +} diff --git a/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/index.mjs b/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/index.mjs new file mode 100644 index 000000000..0d0d770fd --- /dev/null +++ b/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/index.mjs @@ -0,0 +1,2 @@ +// fixture: magpie-model-lab's module (its page service) +export default {} diff --git a/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/package.json b/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/package.json new file mode 100644 index 000000000..434bd69c2 --- /dev/null +++ b/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/package.json @@ -0,0 +1,76 @@ +{ + "name": "magpie-pelican-bench", + "version": "1.0.0", + "description": "Model Lab for Magpie: compare HTML animations, model fingerprints and candy reasoning, with per-model scheduled tests. 模型实验室:动画、指纹、糖果与自动测试。", + "type": "module", + "main": "index.mjs", + "license": "MIT", + "engines": { + "node": ">=22", + "magpie": ">=0.1.2905" + }, + "files": [ + "index.mjs", + "serve.mjs", + "lib", + "page", + "ui", + "vendor", + "docs/usage.md", + "docs/favorites-guide.md", + "docs/publishing.md", + "CHANGELOG.md", + "README.md", + "LICENSE", + "THIRD_PARTY_NOTICES.md" + ], + "keywords": [ + "magpie", + "magpie-plugin", + "magpie-page", + "model-lab", + "llm", + "benchmark", + "pelican", + "html", + "fingerprint", + "candy", + "scheduled-testing", + "模型实验室" + ], + "magpie": { + "name": "模型实验室", + "summary": { + "zh": "比较鹈鹕动画、模型指纹与糖果推理;按模型设置自动测试,查看成功率、动画长廊和批次历史。", + "en": "Compare HTML animations, model fingerprints and candy reasoning. Schedule each model and review success rates, animation galleries and batch history." + }, + "page": { + "title": "Model Lab", + "titles": { + "zh": "模型实验室", + "zh-TW": "模型實驗室" + }, + "file": "page/index.html", + "icon": "page/icon.png", + "service": { + "port": 3489, + "option": "port" + }, + "keepAlive": true + }, + "actions": [ + { + "id": "fingerprint", + "menu": "provider", + "title": "Fingerprint", + "titles": { + "zh": "检测指纹", + "zh-TW": "檢測指紋" + } + } + ], + "options": { + "port": 3489 + } + } +} diff --git a/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/page/index.html b/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/page/index.html new file mode 100644 index 000000000..23e12a803 --- /dev/null +++ b/internal/plugin/testdata/github/raw/xiaopeng66/magpie-model-lab/main/page/index.html @@ -0,0 +1 @@ +