plugin: options saved from a hooks package's own page reach its hook
SetPageOptions saved without starting the host again for any package whose
page and panels looked like all it had. Window() only reads main/exports,
so a package with magpie.hooks and a page (magpie-community/plugins#117's
PowerContext) counted as page-only. The host hands each hook the options it
loaded with, so a Service URL or key saved from the page did nothing until
magpie restarted. A gateway hook now goes through SetOptions, as a
middleware and an agent already did.
Run at d2e0ea393 plus this change:
- TestGatewayHookSeesOptionsSavedFromItsPage: red on the old page.go
("tag=first after the page saved, want second"), green with the fix.
- go test -tags nogui ./internal/plugin: ok (118s).
- go test -tags nogui -run 'TestAsyncGateway|TestGatewayHook' ./internal/gateway: ok.
- go test -tags nogui -run 'Page|Plugin' ./internal/gui: ok.
- GOOS=linux and GOOS=windows go vet of internal/plugin and internal/gateway; gofmt -l clean.
- The real PowerContext package (PR head 3a6aa8f rebased on plugins main)
in the real Bun host behind New().Handler(): before the fix a page save
sent nothing to the new server; after it, the next request resolved,
prepared and hinted against the new server with the new key.
Not run: the full go test ./... (machine at load ~340).
This commit is contained in:
@@ -434,3 +434,52 @@ export async function onResponse(reply,ctx){await fetch(ctx.options.url,{method:
|
||||
t.Fatal("onResponse never ran after the client hung up on a whole reply")
|
||||
}
|
||||
}
|
||||
|
||||
// A hooks package with its own page saves its options from that page. The
|
||||
// host hands each hook the options it loaded with, so the save starts the
|
||||
// host again; one that didn't left the hook on the old options until magpie
|
||||
// restarted (magpie-community/plugins#117's PowerContext page).
|
||||
func TestGatewayHookSeesOptionsSavedFromItsPage(t *testing.T) {
|
||||
t.Setenv("MAGPIE_BUN", testenv.Bun(t))
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
t.Cleanup(plugin.Settle)
|
||||
dir := filepath.Join(t.TempDir(), "paged-hook")
|
||||
if err := os.MkdirAll(filepath.Join(dir, "page"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, body := range map[string]string{
|
||||
"package.json": `{"name":"paged-hook","version":"0.0.1","magpie":{"hooks":"./paged.hooks.mjs","page":{"title":"Paged","file":"page/index.html"}}}`,
|
||||
"paged.hooks.mjs": `export function onRequest(body,ctx){body.tag=ctx.options.tag;return body}`,
|
||||
"page/index.html": `<!doctype html><title>Paged</title>`,
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := plugin.Add(t.Context(), dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := plugin.SetOptions(dir, map[string]any{"tag": "first"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tag := func() any {
|
||||
req := httptest.NewRequest("POST", "http://127.0.0.1/v1/chat/completions", nil)
|
||||
req.RemoteAddr = "127.0.0.1:1234"
|
||||
var got map[string]any
|
||||
w := httptest.NewRecorder()
|
||||
_, b, done := gatewayHooks(w, req, provider.Chat, []byte(`{"model":"fake/m1","messages":[{"role":"user","content":"hello"}]}`))
|
||||
done()
|
||||
json.Unmarshal(b, &got)
|
||||
return got["tag"]
|
||||
}
|
||||
if got := tag(); got != "first" {
|
||||
t.Fatalf("tag=%v before the page saved, want first", got)
|
||||
}
|
||||
if err := plugin.SetPageOptions(dir, map[string]any{"tag": "second"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := tag(); got != "second" {
|
||||
t.Fatalf("tag=%v after the page saved, want second", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -257,7 +257,8 @@ func FileIn(dir, rel string) (string, bool) {
|
||||
|
||||
// SetPageOptions sets the options of a plugin from its page or a panel.
|
||||
// One whose page and panels are all it has is never loaded by the host, so the host goes on as
|
||||
// it was; any other is SetOptions, which starts the host again for it.
|
||||
// it was; any other (a middleware, an agent, a gateway hook, a provider) is SetOptions, which
|
||||
// starts the host again for it.
|
||||
func SetPageOptions(spec string, opts map[string]any) error {
|
||||
t := Target(spec)
|
||||
if mw, _ := Middleware(t); mw != "" {
|
||||
@@ -266,6 +267,10 @@ func SetPageOptions(spec string, opts map[string]any) error {
|
||||
if ag, _ := Agent(t); ag != "" {
|
||||
return SetOptions(spec, opts)
|
||||
}
|
||||
// a gateway hook is handed the options the host loaded it with
|
||||
if Gateway(t) != "" {
|
||||
return SetOptions(spec, opts)
|
||||
}
|
||||
if w, only := Window(t); w == nil || !only {
|
||||
return SetOptions(spec, opts)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user