Files
ServerKit/agent/internal/agentui/server.go
T
Juan Denis 96a6f31d75 Add poll transport; Windows UI & service fixes
Introduce a fallback polling transport and manager: add internal/transport (Manager + poll client) so the agent can fall back from WebSocket to REST long-poll when tunnels mangle WS frames. Wire the Manager into the Agent and expose transport mode in IPC status.

Windows/service fixes: add service-scoped mutex to prevent concurrent service starts, probe IPC port on start to fail fast if occupied, and mirror main.Version into agent package at init. Fix spawned wizard visibility by setting HideWindow=false and add focus helpers that force foreground on Windows.

UI/asset changes: enable DevTools by default (env to disable), inject a small JS shim to forward console errors/navigation readiness to Go logs, hide WebView host until bundle ready (with timeout), and wrap static assets with no-store headers to avoid stale cached bundles. Add local /local/status endpoint so the UI can detect registration when IPC is unreachable.

Pairing/service handling: ensure service stop/start errors are surfaced, wait for service to reach RUNNING after pairing and surface helpful error messages if it doesn't. Implement platform-specific waitForServiceRunning and service control stubs.

Auth/backend alignment: expand API key prefix from 8 to 12 chars to match backend panel expectations and avoid authentication mismatches.

Misc: add focus_windows.go (Windows-only), various small refactors and logging improvements to make blank-page and pairing failures debuggable.
2026-04-30 22:31:04 -04:00

73 lines
2.3 KiB
Go

package agentui
import (
"context"
"fmt"
"net"
"net/http"
"time"
"github.com/serverkit/agent/internal/logger"
)
// startAssetServer binds an HTTP server to a free 127.0.0.1 port and serves
// the embedded UI bundle from it. WebView2 then navigates to the resulting
// URL — using a real HTTP origin (rather than file:// or a virtual host
// mapping) means relative imports, fetch(), and the dev tools "Network"
// panel all behave the same as when running the dev server, which keeps
// surprises out of the migration. Returns the URL to navigate to and a
// shutdown func that stops the server cleanly.
func startAssetServer(ctx context.Context, log *logger.Logger, configPath string) (url string, shutdown func(), err error) {
dist, err := distFS()
if err != nil {
return "", nil, fmt.Errorf("load embedded ui: %w", err)
}
// Bind to :0 so the OS hands us an unused port; we don't conflict with
// the agent's IPC server (19780) or any panel running locally.
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return "", nil, fmt.Errorf("bind asset server: %w", err)
}
mux := http.NewServeMux()
newLocalActions(configPath).register(mux)
newPairer(log, configPath).register(mux)
// Wrap the static file server with no-store headers. WebView2 caches
// per-user-data-folder aggressively; on an MSI upgrade the new bundle
// would otherwise be shadowed by the previous index.html for several
// runs. Asset payload is ~600KB total, so loading it fresh each launch
// has no perceptible cost and removes a whole class of stale-asset
// bug reports.
noCache := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
h.ServeHTTP(w, r)
})
}
mux.Handle("/", noCache(http.FileServer(http.FS(dist))))
srv := &http.Server{
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
}
go func() {
_ = srv.Serve(ln)
}()
addr := ln.Addr().(*net.TCPAddr)
url = fmt.Sprintf("http://127.0.0.1:%d/", addr.Port)
shutdown = func() {
ctxShut, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = srv.Shutdown(ctxShut)
}
return url, shutdown, nil
}