mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-04 08:13:16 +00:00
* fix(prompt): remove duplicate Team Members section from system prompt The TEAM.md context file already provides the Members section with better formatting. The system prompt section was redundant and inconsistently formatted. - Remove buildTeamMembersSection() call from system prompt building - Remove unused buildTeamMembersSection() function * fix(mcp): wire store to manager for prompt preview tool visibility MCP manager needs database access to query configured servers for tool visibility in prompt preview. - Add SetStore() method to MCP manager - Wire pgStores.MCP to manager after initialization - Add debug logging for MCP initialization flow * fix(systemprompt): hide Tooling section when agent has no tools The Tooling section header and boilerplate were displayed even when the agent had no tools available. Add early return in buildToolingSection() to skip the section entirely when toolNames is empty. This reduces prompt noise for agents with no tool access. * feat(mcp): cache tool descriptions for prompt preview visibility MCP tools now show descriptions in prompt preview without requiring a live server connection. - Add CacheToolDescriptions() method to MCPServerStore (PG + SQLite) - Cache tool descriptions in settings['tool_cache'] when server connects - Use cached descriptions in ListToolsForAgent (fallback: hints → cache → global) - Descriptions are auto-populated from live server manifest on connection - Admins can still override via tool_hints in server settings * test(mcp): add CacheToolDescriptions to MCPServerStore test fakes Commit 5ce410b6 added CacheToolDescriptions() to the MCPServerStore interface but missed updating test mock implementations, breaking go vet across internal/mcp, internal/agent, internal/http, and internal/channels/bitrix24. Add no-op implementations matching each fake's existing style. * fix(providers): enforce per-agent tool policy for Claude CLI provider The Claude CLI provider (stdio+MCP bridge) was not enforcing per-agent tool policy, unlike other providers where the policy-filtered tool list already drives the system prompt's Tooling section. Two gaps closed: 1. --disallowedTools was previously skipped entirely when no MCP config path was resolved, letting the CLI subprocess run with its full native toolset (Bash, Edit, Read, Write, Glob, Grep, WebFetch, WebSearch) regardless of agent policy. It's now unconditional and derived from the agent's actual allowed-tools list (state.Tool.AllowedTools), mapped to Claude CLI's native tool names. 2. The MCP bridge server executed any tool call without checking the calling agent's policy. It now resolves the agent's policy from context (via the existing HMAC-verified agent lookup) and denies calls to tools outside that agent's allowed set, logging security.mcp_bridge_denied on denial. Both gaps were closed using existing plumbing (PolicyEngine.WouldAllow, AgentData.ParseToolsConfig, the bridge context middleware) — no new cross-cutting mechanism was introduced. * feat(web): show MCP/tool schemas in system prompt preview dialog The prompt-preview API response includes a separate `tools` field (the actual JSON schemas sent to the LLM as the tools API parameter) alongside `prompt` (the system prompt text), but the web UI only rendered `prompt`, silently dropping the tools list. Add a collapsible Tools section to both the full-screen System Prompt dialog and the inline agent-detail preview, showing tool count, name, description, and expandable parameter schema per tool. i18n keys added to en/vi/zh locales. * fix(prompt): render pinned skills on bootstrap turns Pinned skills are documented (web UI copy) as "always inlined in the system prompt", but the entire Skills section was gated behind !cfg.IsBootstrap, so pinned skill XML never appeared on bootstrap turns (first message of a session) despite the promise. Separate pinned-skill rendering from bootstrap-suppressed guidance: - Bootstrap + pinned skills present: render pinned XML only, no search/manage guidance (which stays suppressed as before) - Non-bootstrap: unchanged behavior - Minimal/none modes: pinned skills always render regardless of bootstrap state Add regression tests covering all four prompt modes on bootstrap turns, plus a non-bootstrap guard confirming existing behavior is preserved. * fix(skills): resolve managed skills directory per-tenant, not master-only skills.Loader was wired at startup to scan a single fixed directory (the master tenant's managed-skills dir), making any skill belonging to a non-master tenant invisible to both pinned-skills prompt resolution and skill_search/use_skill, regardless of DB visibility settings. - Loader now resolves the calling tenant's managed-skills directory per-call via context (store.TenantIDFromContext), never enumerating other tenants' directories - Skill cache is now tenant-keyed to prevent slug collisions and cross-tenant cache leaks across tenants using the same skill slug - gateway_setup.go passes the root data dir instead of a pre-resolved master-tenant path Write-side tooling (skill_manage, publish_skill) was already correctly tenant-scoped per-operation — no changes needed there. Added TestLoader_ManagedSkills_TenantIsolation proving two tenants with same-slug/different-content skills never see each other's content, including after cache population from a different tenant's lookup. Known follow-up (not in this commit): skill_search's BM25 index is still a single process-global index shared across tenants, which is a related but separate cross-tenant search-result leak requiring its own scoped fix (per-tenant index maps + threading tenant context through ensureIndex/rebuildIndex). * fix(tools): scope skill_search BM25 index per-tenant SkillSearchTool held a single process-global BM25 index built once from whichever tenant's context first triggered ensureIndex, then reused for all subsequent Execute() calls regardless of caller — leaking one tenant's skill search results into another's, the search-path counterpart to the managed-directory bug fixed in 7b4668ad. - index/lastVersion are now keyed per-tenant (map[uuid.UUID]*tenantIndexState) - ensureIndex resolves the calling tenant from context and only builds/reads that tenant's index entry, never touching another tenant's cached state - Builtin/bundled skills remain visible in every tenant's index (Loader already merges those tiers correctly per 7b4668ad) Loader.Version() remains a single global counter — a version bump in one tenant causes unnecessary rebuilds in others but does not cause cross-tenant leakage, an acceptable tradeoff to avoid scope creep. Added TestSkillSearchTool_TenantIsolation proving two tenants with same-slug/different-content skills never see each other's search results, including after cache population from a different tenant. * fix(tools): fix group-spec expansion in tool policy engine PolicyEngine.registry was only ever set via SetRegistry(), which was never called in production (only in one test) — so pe.registry was permanently nil in production. Every group-expansion helper (applyProfile, intersectWithSpec, unionWithSpec, subtractSpec, expandSpec, matchDenySpec, filterByCapability) silently dropped any "group:*" spec entry instead of expanding it when registry was nil. Concretely: "group:mcp" (auto-injected into agentToolPolicy.AlsoAllow for any agent with MCP tools) never resolved to real tool names, so MCP tools connected successfully and appeared in prompt text (which reads the registry directly, bypassing PolicyEngine) but were never included in the actual ChatRequest.Tools payload sent to the LLM — confirmed live via mcp.agent.tools_loaded tools=6 immediately followed by mcp.filtered_tools mcp_defs_count=0 in the same request. This affects any agent relying on group-based grants, not just MCP. PolicyEngine is a shared/global singleton used concurrently across all agents (constructed once at gateway startup), so mutating a registry field per-call would be a data race. Fix instead threads the registry as an explicit parameter from FilterTools down through all internal group-expansion helpers, and adds IsDenied/WouldAllow registry parameters, removing the dead SetRegistry() mechanism entirely. Also fixes group expansion for the per-user-MCP-tools path: FilterTools is sometimes called with a userToolOverlay wrapping a *Registry rather than a *Registry directly; added Unwrap() to userToolOverlay so the new registry-resolution logic works for both cases. Added 4 tests proving group expansion works via the threaded parameter alone (no SetRegistry): plain registry allow, userToolOverlay allow, deny-side group expansion, and the WouldAllow bridge-server path. Blast radius note: this restores intended access for every agent configured with group:* specs (group:mcp, group:vault, group:goclaw, group:coding, etc.) that were silently inert before. Existing agent configs relying on group grants will gain the tool access they were nominally already configured for. * fix(mcp): cache tool descriptions from pool-connected servers too 5ce410b6 added tool-description caching (for prompt-preview visibility) only inside connectServer. connectViaPool — the separate connect path used when MCP connections go through the shared pool — never got the same caching hook, even though it shares the same underlying connectAndDiscover wire handshake. Confirmed live: cloudflare/docker connected via connectViaPool and received real descriptions over the wire, but prompt preview still showed blank descriptions because this path never wrote to the cache. Also removes the temporary mcp.connect.raw_tool debug log added earlier this session for diagnosing the same issue — no longer needed now that the root cause is fixed. * chore(skills): remove temporary pinned-skills diagnostic logging Confirmed live: pinned skills (caveman, infra-ansible-knowledge) now resolve correctly end-to-end for tenant-scoped agents. Debug logging added to trace the resolution chain is no longer needed. * fix(tools): deny always wins over AlsoAllow group grants AlsoAllow's unionWithSpec could reintroduce a tool explicitly listed in Deny, since it added tools back from allTools without re-checking deny specs. Previously masked because AlsoAllow's group-expansion was also broken (fixed in f7af95de this session) — group specs silently expanded to nothing, so this ordering bug never manifested. Now that group expansion works, an admin-denied tool that's also reachable via a group:* AlsoAllow entry (e.g. group:mcp) would silently reappear. Re-apply deny-spec subtraction as a final step after AlsoAllow union, for both global and per-agent policy, so deny always wins regardless of which allow mechanism tries to add a tool back. Added tests proving global and per-agent Deny correctly override an overlapping AlsoAllow group grant, while sibling non-denied tools in the same group remain allowed. * fix(mcp): enumerate cached tools instead of wildcard placeholder in prompt preview ListToolsForAgent (the prompt-preview path) collapsed any server with an empty ToolAllow (unrestricted grant — the common case) into a single "server__*" placeholder entry, even when tool_cache already had every real tool name and description from connect time (5ce410b6, 8ffd67b9). This meant agents with unrestricted MCP server access never saw individual tool names or descriptions in prompt preview, forcing trial-and-error tool usage. When ToolAllow is empty and tool_cache is populated, enumerate every cached tool (skipping any explicitly denied) and emit one MCPToolPreviewInfo per tool, matching the construction logic already used for the ToolAllow-non-empty case. Falls back to the single placeholder only when tool_cache is also empty (server never connected). The live (non-preview) conversation path, buildMCPToolDescs, does not have this bug — it resolves tool identity from the live connected registry, never from ToolAllow, so no placeholder shortcut exists there. Added tests covering both the cache-populated enumeration case and the no-cache placeholder fallback. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(prompt): filter alias re-injection and add MCP schemas to preview ToolDefs Prompt-preview's tools: schema array (PreviewResult.ToolDefs) had two bugs, both preview-only — confirmed live conversations already use correctly-filtered tool payloads via buildToolsPayload/PolicyEngine.FilterTools, unaffected by either bug: 1. Alias re-injection iterated ALL registry aliases globally with no check against the deny-filtered toolNames list, letting denied tools reappear via their alias name (e.g. a denied canonical tool still showing up as its Claude-Code-compat alias like Bash/Edit/Write/Read). Now skips any alias whose canonical tool isn't in the filtered set. 2. MCP tool descriptions (from the store-based, connection-free ListToolsForAgent path) only ever fed the prompt TEXT section, never got converted into ToolDefinition schema objects — so the tools: array never showed MCP tools at all, even when the MCP section of the prompt text correctly listed them. Now appends a ToolDefinition per MCP tool with name+description populated and a placeholder {"type":"object"} Parameters schema, documented as preview-only (real parameter schemas require a live MCP connection, only available during an actual conversation turn). Added tests proving denied-tool-alias exclusion and MCP tool inclusion in preview ToolDefs. * feat(skills): inline full content for pinned skills instead of pointer-only Web UI documented "Pinned skills are always inlined in the system prompt", but BuildPinnedSummary was just BuildSummary with an allowlist filter — same as the general searchable skill list: name/description(truncated)/location pointer only, requiring use_skill+read_file round trips to get actual content. No code path inlined real SKILL.md content for pinned skills specifically. BuildPinnedSummary now reads and inlines full SKILL.md content (frontmatter stripped) per pinned skill inside <skill_instructions> tags. Per-skill (10000 bytes) and total (30000 bytes) size caps fall back to the original pointer-only format with a note when a skill is too large to inline, so oversized skills degrade gracefully instead of blowing the prompt budget. Added tests: full-content inlining, size-cap fallback, and tenant isolation for the new inline path (mirroring the existing managed- skills tenant isolation test). * fix(mcp): real parameter schemas and full policy enforcement in prompt preview Three interconnected fixes to prompt preview, none affecting the live conversation path (which was already correct): 1. Real MCP parameter schemas instead of a useless empty placeholder. tool_cache previously stored only name+description; extended to also capture the real JSON Schema from the MCP server's tools/list response (CachedToolInfo{Description, Parameters}) at connect time, for both direct-connect and pool-connect paths. Preview now shows complete, real input schemas instead of {"type":"object"} with no properties — verified against actual wire data from a live cloudflare MCP server showing genuinely rich schemas (zone_id, type, name, content, ttl, proxied, all typed with descriptions and correct required arrays) that were previously being discarded. Backward-compatible: old-shape cache entries degrade gracefully to description-only rather than crashing, self-healing on next connect. 2. Global tool deny now enforced in preview. BuildPreviewPrompt previously hand-rolled a partial policy reimplementation (per-agent deny only, explicitly skipping the full PolicyEngine "because runtime state isn't available in preview") — but PolicyEngine.WouldAllow already handles this per-tool-name without needing channel context. A tool denied via the global config (not per-agent) would appear in preview despite being correctly denied in every real conversation. Preview now calls WouldAllow per candidate tool, with a graceful per-agent-deny-only fallback when no PolicyEngine is wired (e.g. in tests). 3. MCP tools now also subject to the same policy check — previously the MCP tool supplement (store-based, connection-free tool listing) added MCP tool names unconditionally, bypassing WouldAllow entirely, so a denied MCP tool could still appear in preview. Added tests for all three: real-schema presence, global-deny exclusion for both core and MCP tools, and backward-compat cache handling. * fix(prompt): remove redundant per-tool MCP enumeration from prompt text Now that MCP tool schemas in the tools: API parameter are real and complete (1290d4f1), the ## MCP Tools prompt-text section's per-tool "- mcp_x__y: description" enumeration is pure duplication with zero added value — the model already gets each tool's real schema (including description) via tools:. buildMCPToolsInlineSection now keeps only the behavioral instructions that aren't expressible via JSON schema and thus aren't duplicated: prefer-MCP-over-core-tools guidance, and the optional-parameter guidance (don't guess/fill optional fields). The per-tool name+ description enumeration loop is removed. Section still only appears when the agent has MCP tools (len(cfg.MCPToolDescs) > 0, unchanged gate). Updated tests to assert the enumeration is gone while the behavioral instructions remain; ToolDefs assertions are now the authoritative check for MCP tool allow/deny filtering behavior (prompt text no longer enumerates names at all). * test(agent): update TeamContextInjection test for removed Team Members section TestBuildSystemPrompt_TeamContextInjection asserted the presence of a 'Team Members' prompt-text section that was intentionally removed in 71d33180 (duplicate of the canonical TEAM.md-context-file Members section, which has better formatting). The test was never updated to match, causing it to fail on every run since. Moved the assertion from wantIn to wantNotIn for the 3 affected subtests -- BuildSystemPrompt correctly no longer renders team-member roster info directly; that info now comes exclusively from the TEAM.md context-file mechanism, outside this test's isolated scope. * fix(prompt): resolve real registry for WouldAllow calls in preview BuildPreviewPrompt's two WouldAllow calls hardcoded reg=nil, silently breaking group:* expansion (e.g. group:mcp) needed to resolve the AlsoAllow grant production actually uses to grant MCP tool access (resolver_helpers.go's agentToolPolicyWithMCP injects AlsoAllow: ["group:mcp"]). With reg=nil, WouldAllow could match literal tool names fine (the 18 core/static tools) but could never resolve group-based grants, so every MCP tool silently failed WouldAllow and was excluded from preview -- confirmed live via curl: 18 tools returned, zero mcp_* ones, for an agent with genuinely working MCP access in real conversations. Live conversations were never affected -- internal/mcp/bridge_server.go's WouldAllow call already correctly passes a real registry. Fix resolves a real *tools.Registry from deps.ToolLister via tools.ResolveConcreteRegistry (the same helper used at the live call site), passing it to both WouldAllow calls instead of nil. Falls back to nil gracefully for test mocks that don't implement the full ToolExecutor interface, preserving existing test behavior. Added a test proving an MCP tool granted via the exact production AlsoAllow: ["group:mcp"] pattern is now correctly included in preview ToolDefs, where the old reg=nil bug would have silently excluded it. * fix(prompt): use literal deny check for MCP tools in preview, not group expansion The MCP-tools policy gate added in 1290d4f1 called WouldAllow with a real registry (per 9b5fd2eb), which requires group:mcp expansion against that registry to grant access via the production AlsoAllow: ["group:mcp"] pattern. But MCP tools are only ever registered into ephemeral per-agent registry clones at live connection time (manager_connect.go) -- never into the shared/global registry preview uses. group:mcp always resolved empty in preview's connection-free context, so WouldAllow denied every MCP tool -- confirmed live via tool-name diff: live conversations correctly included all 6 MCP tools, preview included zero. MCP access-granting is already correctly handled by ListToolsForAgent's own per-server tool_allow/tool_deny grant logic (confirmed working correctly earlier this session). The preview gate only needs to catch the narrower case of a literally-denied tool name via global/per-agent policy config -- it never needed group expansion. Replaced WouldAllow with IsDenied(nil, name, agentPolicy), which forces a pure literal-name match with zero registry dependency, matching the existing usage pattern already established elsewhere in policy.go. This class of bug cannot recur: there's no registry-passing code path left in this check to silently reintroduce group-expansion dependence. Added a test proving MCP tool inclusion in preview is independent of group-expansion outcome (no AlsoAllow: group:mcp needed for a non-denied tool to appear). Also reverts the temporary loop.filtered_tool_names/ preview_prompt.filtered_tool_names diagnostic logging used to capture the live-vs-preview tool-name comparison that diagnosed this bug. * fix(http): preserve real MCP parameter schemas through HTTP preview adapter mcpPreviewAdapter.ListToolsForAgent (the HTTP-layer glue converting mcp.MCPToolPreviewInfo to agent.MCPToolPreviewInfo for BuildPreviewPrompt) only copied RegisteredName and Description, silently dropping Parameters -- a bug present since this adapter was introduced (2499d0be/7e250244), unrelated to today's other MCP preview fixes. This was masked until d5fc6344 fixed MCP tools being excluded from preview entirely (a separate bug) -- once MCP tools started appearing again, this pre-existing adapter gap became visible: tools showed up correctly, but always with the bare {"type":"object"} placeholder instead of their real cached schema (confirmed live: update_dns_record missing its 7 real properties). One-line fix: copy Parameters through in the adapter's struct literal. Added a regression test constructing a real *mcp.Manager with populated tool_cache, asserting the adapter's output preserves specific real schema properties (not just non-nil Parameters) -- verified this test fails without the fix and passes with it. --------- Co-authored-by: Bruno Clermont <bruno.clermont@gmail.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
848 lines
31 KiB
Go
848 lines
31 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"crypto/subtle"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/nextlevelbuilder/goclaw/internal/agent"
|
|
"github.com/nextlevelbuilder/goclaw/internal/bus"
|
|
"github.com/nextlevelbuilder/goclaw/internal/config"
|
|
httpapi "github.com/nextlevelbuilder/goclaw/internal/http"
|
|
mcpbridge "github.com/nextlevelbuilder/goclaw/internal/mcp"
|
|
"github.com/nextlevelbuilder/goclaw/internal/permissions"
|
|
"github.com/nextlevelbuilder/goclaw/internal/providers"
|
|
"github.com/nextlevelbuilder/goclaw/internal/store"
|
|
"github.com/nextlevelbuilder/goclaw/internal/tools"
|
|
"github.com/nextlevelbuilder/goclaw/internal/webui"
|
|
"github.com/nextlevelbuilder/goclaw/pkg/protocol"
|
|
)
|
|
|
|
// Server is the main gateway server handling WebSocket and HTTP connections.
|
|
// routeRegistrar is implemented by all HTTP API handlers that register routes on a mux.
|
|
type routeRegistrar interface {
|
|
RegisterRoutes(mux *http.ServeMux)
|
|
}
|
|
|
|
type Server struct {
|
|
cfg *config.Config
|
|
eventPub bus.EventPublisher
|
|
agents *agent.Router
|
|
sessions store.SessionStore
|
|
tools *tools.Registry
|
|
router *MethodRouter
|
|
|
|
// HTTP API handlers — all implement routeRegistrar.
|
|
// Registered via Set*Handler() setters, routes added in BuildMux() via single loop.
|
|
handlers []routeRegistrar
|
|
|
|
// Non-handler dependencies (don't implement RegisterRoutes)
|
|
policyEngine *permissions.PolicyEngine
|
|
pairingService store.PairingStore
|
|
apiKeyStore store.APIKeyStore // for API key auth lookup
|
|
agentStore store.AgentStore // for context injection in tools_invoke
|
|
msgBus *bus.MessageBus // for MCP bridge media delivery
|
|
toolPolicy *tools.PolicyEngine // for per-agent tool policy enforcement in MCP bridge
|
|
|
|
upgrader websocket.Upgrader
|
|
rateLimiter *RateLimiter
|
|
clients map[string]*Client
|
|
mu sync.RWMutex
|
|
|
|
startedAt time.Time
|
|
version string
|
|
db interface{ PingContext(context.Context) error } // for health check DB ping
|
|
updateChecker *UpdateChecker
|
|
|
|
logTee *LogTee // optional; auto-unsubscribes clients on disconnect
|
|
postTurn tools.PostTurnProcessor // optional; for team task dispatch in HTTP API paths
|
|
|
|
httpServer *http.Server
|
|
mux *http.ServeMux
|
|
|
|
// publicURLSnapshot remembers the gateway's externally reachable base URL
|
|
// learned from inbound HTTP requests. Reset to a fresh snapshot per Server
|
|
// so test servers don't share state. Read by RPC methods that need to
|
|
// advertise URLs back to external systems (e.g. Bitrix24 install link).
|
|
publicURLSnapshot *PublicURLSnapshot
|
|
}
|
|
|
|
// SetPostTurnProcessor sets the post-turn processor for team task dispatch in HTTP API handlers.
|
|
func (s *Server) SetPostTurnProcessor(pt tools.PostTurnProcessor) {
|
|
s.postTurn = pt
|
|
}
|
|
|
|
// SetToolPolicy sets the tool policy engine used to enforce per-agent tool
|
|
// access on the MCP bridge server (see internal/mcp/bridge_server.go).
|
|
func (s *Server) SetToolPolicy(pe *tools.PolicyEngine) {
|
|
s.toolPolicy = pe
|
|
}
|
|
|
|
// NewServer creates a new gateway server.
|
|
func NewServer(cfg *config.Config, eventPub bus.EventPublisher, agents *agent.Router, sess store.SessionStore, toolsReg ...*tools.Registry) *Server {
|
|
s := &Server{
|
|
cfg: cfg,
|
|
eventPub: eventPub,
|
|
agents: agents,
|
|
sessions: sess,
|
|
clients: make(map[string]*Client),
|
|
startedAt: time.Now(),
|
|
publicURLSnapshot: NewPublicURLSnapshot(),
|
|
}
|
|
|
|
s.upgrader = websocket.Upgrader{
|
|
ReadBufferSize: 1024,
|
|
WriteBufferSize: 1024,
|
|
CheckOrigin: s.checkOrigin,
|
|
}
|
|
|
|
if len(toolsReg) > 0 && toolsReg[0] != nil {
|
|
s.tools = toolsReg[0]
|
|
}
|
|
|
|
// Initialize rate limiter.
|
|
// rate_limit_rpm > 0 → enabled at that RPM
|
|
// rate_limit_rpm == 0 → disabled (default, backward compat)
|
|
// rate_limit_rpm < 0 → disabled explicitly
|
|
s.rateLimiter = NewRateLimiter(cfg.Gateway.RateLimitRPM, 5)
|
|
|
|
s.router = NewMethodRouter(s)
|
|
return s
|
|
}
|
|
|
|
// RateLimiter returns the server's rate limiter for use by method handlers.
|
|
func (s *Server) RateLimiter() *RateLimiter { return s.rateLimiter }
|
|
|
|
// PublicURLSnapshot returns the snapshot of the gateway's externally reachable
|
|
// base URL. Updated by the snapshot middleware on every inbound request.
|
|
func (s *Server) PublicURLSnapshot() *PublicURLSnapshot { return s.publicURLSnapshot }
|
|
|
|
// checkOrigin validates WebSocket connection origin against the allowed origins whitelist.
|
|
// If no origins are configured, all origins are allowed (backward compatibility / dev mode).
|
|
// Empty Origin header (non-browser clients like CLI/SDK) is always allowed.
|
|
func (s *Server) checkOrigin(r *http.Request) bool {
|
|
allowed := s.cfg.Gateway.AllowedOrigins
|
|
if len(allowed) == 0 {
|
|
return true // no config = allow all (backward compat)
|
|
}
|
|
origin := r.Header.Get("Origin")
|
|
if origin == "" {
|
|
return true // non-browser clients (CLI, SDK, channels)
|
|
}
|
|
for _, a := range allowed {
|
|
if origin == a || a == "*" {
|
|
return true
|
|
}
|
|
}
|
|
slog.Warn("security.cors_rejected", "origin", origin)
|
|
return false
|
|
}
|
|
|
|
// BuildMux creates and caches the HTTP mux with all routes registered.
|
|
// Call this before Start() if you need the mux for additional listeners (e.g. Tailscale).
|
|
func (s *Server) BuildMux() *http.ServeMux {
|
|
if s.mux != nil {
|
|
return s.mux
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
// WebSocket endpoint
|
|
mux.HandleFunc("/ws", s.handleWebSocket)
|
|
|
|
// HTTP API endpoints
|
|
mux.HandleFunc("/health", s.handleHealth)
|
|
|
|
// OpenAI-compatible chat completions
|
|
isManaged := s.agentStore != nil
|
|
chatHandler := httpapi.NewChatCompletionsHandler(s.agents, s.sessions, isManaged)
|
|
if s.rateLimiter.Enabled() {
|
|
chatHandler.SetRateLimiter(s.rateLimiter.Allow)
|
|
}
|
|
if s.postTurn != nil {
|
|
chatHandler.SetPostTurnProcessor(s.postTurn)
|
|
}
|
|
mux.Handle("/v1/chat/completions", chatHandler)
|
|
|
|
// OpenResponses protocol
|
|
responsesHandler := httpapi.NewResponsesHandler(s.agents, s.sessions)
|
|
if s.postTurn != nil {
|
|
responsesHandler.SetPostTurnProcessor(s.postTurn)
|
|
}
|
|
mux.Handle("/v1/responses", responsesHandler)
|
|
|
|
// Direct tool invocation
|
|
if s.tools != nil {
|
|
toolsHandler := httpapi.NewToolsInvokeHandler(s.tools, s.agentStore)
|
|
mux.Handle("/v1/tools/invoke", toolsHandler)
|
|
}
|
|
|
|
// Read-only HTTP compatibility for automation clients.
|
|
if s.sessions != nil {
|
|
httpapi.NewSessionsHandler(s.sessions, s.cfg.Gateway.OwnerIDs).RegisterRoutes(mux)
|
|
}
|
|
|
|
// Register all HTTP API handlers (agents, skills, teams, storage, etc.)
|
|
for _, h := range s.handlers {
|
|
if h != nil {
|
|
h.RegisterRoutes(mux)
|
|
}
|
|
}
|
|
|
|
httpapi.RegisterAPINotFoundRoute(mux)
|
|
|
|
// MCP bridge: expose GoClaw tools to Claude CLI via streamable-http.
|
|
// Only listens on localhost (CLI runs on the same machine).
|
|
// Protected by gateway token; disabled when no token is configured to
|
|
// prevent unauthenticated tool invocations if port is exposed.
|
|
if s.tools != nil {
|
|
if s.cfg.Gateway.Token != "" {
|
|
bridgeHandler := mcpbridge.NewBridgeServer(s.tools, "1.0.0", s.msgBus, s.toolPolicy)
|
|
handler := tokenAuthMiddleware(s.cfg.Gateway.Token,
|
|
bridgeContextMiddleware(s.cfg.Gateway.Token, s.agentStore, bridgeHandler))
|
|
mux.Handle("/mcp/bridge", handler)
|
|
} else {
|
|
slog.Warn("security.mcp_bridge_disabled: no gateway token configured, MCP bridge is disabled")
|
|
mux.HandleFunc("/mcp/bridge", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusForbidden)
|
|
_, _ = w.Write([]byte(`{"error":"mcp bridge disabled: set GOCLAW_GATEWAY_TOKEN to enable"}`))
|
|
})
|
|
}
|
|
}
|
|
|
|
// Embedded web UI (built with -tags embedui). Catch-all after all API routes.
|
|
// When the build does NOT include the embedui tag, webui.Handler() returns nil
|
|
// and there's no handler for "/" — http.ServeMux would then return an opaque
|
|
// 404 for the root URL, confusing operators who open the deployed URL in a
|
|
// browser to check the service. Install a minimal JSON index handler in that
|
|
// case so the root responds with something useful (and any unmatched path
|
|
// still returns 404, just with a JSON body).
|
|
if h := webui.Handler(); h != nil {
|
|
mux.Handle("/", h)
|
|
slog.Info("serving embedded web UI")
|
|
} else {
|
|
mux.HandleFunc("/", s.handleIndex)
|
|
}
|
|
|
|
s.mux = mux
|
|
return mux
|
|
}
|
|
|
|
// bridgeContextMiddleware extracts X-Agent-ID, X-User-ID, and X-Workspace headers
|
|
// from the MCP bridge request and injects them into the context so bridge tools can
|
|
// access agent/user scope and resolve workspace-relative paths.
|
|
// When a gateway token is configured, the context headers must be accompanied by
|
|
// a valid X-Bridge-Sig HMAC to prevent forgery.
|
|
func bridgeContextMiddleware(gatewayToken string, agentStore store.AgentStore, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
agentIDStr := r.Header.Get("X-Agent-ID")
|
|
userID := r.Header.Get("X-User-ID")
|
|
channel := r.Header.Get("X-Channel")
|
|
chatID := r.Header.Get("X-Chat-ID")
|
|
peerKind := r.Header.Get("X-Peer-Kind")
|
|
workspace := r.Header.Get("X-Workspace")
|
|
localKey := r.Header.Get("X-Local-Key")
|
|
sessionKey := r.Header.Get("X-Session-Key")
|
|
|
|
if agentIDStr != "" || userID != "" {
|
|
// Reject context headers when no gateway token — prevents unauthenticated impersonation.
|
|
if gatewayToken == "" {
|
|
slog.Warn("security.mcp_bridge: no gateway token, ignoring context headers",
|
|
"agent_id", agentIDStr, "user_id", userID)
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
// Verify HMAC signature over all context fields.
|
|
tenantIDStr := r.Header.Get("X-Tenant-ID")
|
|
sig := r.Header.Get("X-Bridge-Sig")
|
|
ok, tenantVerified := providers.VerifyBridgeContext(gatewayToken, agentIDStr, userID, channel, chatID, peerKind, workspace, tenantIDStr, sig, localKey, sessionKey)
|
|
if !ok {
|
|
slog.Warn("security.mcp_bridge: invalid bridge context signature",
|
|
"agent_id", agentIDStr, "user_id", userID)
|
|
http.Error(w, `{"error":"invalid bridge context signature"}`, http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
if agentIDStr != "" {
|
|
if id, err := uuid.Parse(agentIDStr); err == nil {
|
|
ctx = store.WithAgentID(ctx, id)
|
|
|
|
// Inject per-agent shell deny group overrides so the exec tool
|
|
// respects the same policy as the normal agent loop.
|
|
if agentStore != nil {
|
|
ag, err := agentStore.GetByIDUnscoped(ctx, id)
|
|
if err == nil && ag != nil {
|
|
// Propagate the agent key so bridged session tools (sessions_list/
|
|
// history/send) can resolve identity via ToolAgentKeyFromCtx. The MCP
|
|
// bridge otherwise injects only the agent UUID, leaving the key empty
|
|
// -> session tools fail with "agent context required".
|
|
ctx = tools.WithToolAgentKey(ctx, ag.AgentKey)
|
|
// Propagate the agent's per-agent tool policy so the MCP bridge
|
|
// handler can enforce the same policy-filtered allowlist the
|
|
// normal agent loop uses (see bridge_server.go makeToolHandler),
|
|
// instead of exposing all BridgeToolNames unconditionally.
|
|
ctx = tools.WithToolAgentPolicy(ctx, ag.ParseToolsConfig())
|
|
groups := ag.ParseShellDenyGroups()
|
|
if groups != nil {
|
|
ctx = store.WithShellDenyGroups(ctx, groups)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if userID != "" {
|
|
ctx = store.WithUserID(ctx, userID)
|
|
}
|
|
// Only inject tenant_id when HMAC actually covers it (level 1).
|
|
// Fallback levels (pre-tenantID sessions) must not trust unsigned tenant headers.
|
|
if tenantVerified && tenantIDStr != "" {
|
|
if tid, err := uuid.Parse(tenantIDStr); err == nil {
|
|
ctx = store.WithTenantID(ctx, tid)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Inject channel routing context for tools like message, cron, etc.
|
|
if channel != "" {
|
|
ctx = tools.WithToolChannel(ctx, channel)
|
|
}
|
|
if chatID != "" {
|
|
ctx = tools.WithToolChatID(ctx, chatID)
|
|
}
|
|
if peerKind != "" {
|
|
ctx = tools.WithToolPeerKind(ctx, peerKind)
|
|
}
|
|
// Inject workspace so bridge tools (read_image, read_file, etc.) can resolve paths.
|
|
// Only when agent context is present (HMAC-protected) to prevent unauthenticated path injection.
|
|
if workspace != "" && (agentIDStr != "" || userID != "") {
|
|
ctx = tools.WithToolWorkspace(ctx, workspace)
|
|
}
|
|
// Routing context (localKey, sessionKey) is injected unconditionally like channel/chatID.
|
|
// These are used for message routing (forum topics), not security-sensitive operations.
|
|
// Without valid agent context, tool execution will fail anyway.
|
|
if localKey != "" {
|
|
ctx = tools.WithToolLocalKey(ctx, localKey)
|
|
}
|
|
if sessionKey != "" {
|
|
ctx = tools.WithToolSessionKey(ctx, sessionKey)
|
|
}
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
|
|
// tokenAuthMiddleware wraps an http.Handler with Bearer token authentication.
|
|
func tokenAuthMiddleware(token string, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
auth := r.Header.Get("Authorization")
|
|
provided := strings.TrimPrefix(auth, "Bearer ")
|
|
if !strings.HasPrefix(auth, "Bearer ") || subtle.ConstantTimeCompare([]byte(provided), []byte(token)) != 1 {
|
|
http.Error(w, `{"error":"unauthorized"}`, http.StatusUnauthorized)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// Start begins listening for WebSocket and HTTP connections.
|
|
func (s *Server) Start(ctx context.Context) error {
|
|
mux := s.BuildMux()
|
|
|
|
// Wrap with CORS for desktop dev mode (Wails serves frontend on different port).
|
|
var handler http.Handler = mux
|
|
if os.Getenv("GOCLAW_DESKTOP") == "1" {
|
|
handler = desktopCORS(mux)
|
|
}
|
|
// NOTE: The public-URL snapshot is intentionally NOT updated by a global
|
|
// middleware. An unauthenticated probe with a forged Host header could
|
|
// otherwise poison the URL we hand back to clients (which then ends up
|
|
// in OAuth callbacks and would leak tokens to an attacker-controlled
|
|
// host). Instead, the snapshot is updated inside handleConnect AFTER
|
|
// token authentication succeeds (see internal/gateway/router.go), and
|
|
// from /bitrix24/install (already gated by valid OAuth state).
|
|
|
|
addr := fmt.Sprintf("%s:%d", s.cfg.Gateway.Host, s.cfg.Gateway.Port)
|
|
s.httpServer = &http.Server{
|
|
Addr: addr,
|
|
Handler: handler,
|
|
}
|
|
|
|
slog.Info("gateway starting", "addr", addr)
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
s.httpServer.Shutdown(shutdownCtx)
|
|
}()
|
|
|
|
if err := s.httpServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
|
return fmt.Errorf("gateway server: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// handleWebSocket upgrades HTTP to WebSocket and manages the connection.
|
|
func (s *Server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
|
conn, err := s.upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
slog.Error("websocket upgrade failed", "error", err)
|
|
return
|
|
}
|
|
|
|
client := NewClient(conn, s, clientIP(r))
|
|
// Capture the public URL from the HTTP upgrade request. We DON'T snapshot
|
|
// it server-wide yet — that happens only after the client authenticates
|
|
// in handleConnect. This prevents an unauthenticated probe with a forged
|
|
// Host header from poisoning the gateway-wide public URL.
|
|
client.setUpgradeURL(derivePublicURLFromRequest(r))
|
|
s.registerClient(client)
|
|
|
|
defer func() {
|
|
s.unregisterClient(client)
|
|
client.Close()
|
|
}()
|
|
|
|
client.Run(r.Context())
|
|
}
|
|
|
|
// handleHealth returns a simple health check response.
|
|
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprintf(w, `{"status":"ok","protocol":%d}`, protocol.ProtocolVersion)
|
|
}
|
|
|
|
// handleIndex is the fallback "/" handler when no embedded web UI is present.
|
|
// It returns a small JSON service-info document for exact-match "/" requests
|
|
// and a JSON 404 for everything else — http.ServeMux routes "/" as a
|
|
// catch-all, so unrelated paths fall through here too.
|
|
func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if r.URL.Path != "/" {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(`{"error":"not found"}`))
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprintf(w,
|
|
`{"service":"goclaw","status":"ok","protocol":%d,`+
|
|
`"endpoints":["/health","/v1/chat/completions","/v1/responses","/v1/tools/invoke","/ws"]}`,
|
|
protocol.ProtocolVersion)
|
|
}
|
|
|
|
// clientIP extracts the real client IP from the request, checking proxy headers first.
|
|
func clientIP(r *http.Request) string {
|
|
if ip := r.Header.Get("X-Real-IP"); ip != "" {
|
|
return ip
|
|
}
|
|
if fwd := r.Header.Get("X-Forwarded-For"); fwd != "" {
|
|
if i := strings.IndexByte(fwd, ','); i > 0 {
|
|
return strings.TrimSpace(fwd[:i])
|
|
}
|
|
return strings.TrimSpace(fwd)
|
|
}
|
|
host, _, _ := net.SplitHostPort(r.RemoteAddr)
|
|
return host
|
|
}
|
|
|
|
// Router returns the method router for registering additional handlers.
|
|
func (s *Server) Router() *MethodRouter { return s.router }
|
|
|
|
// SetPolicyEngine sets the permission policy engine for RPC method authorization.
|
|
func (s *Server) SetPolicyEngine(pe *permissions.PolicyEngine) { s.policyEngine = pe }
|
|
|
|
// SetPairingService sets the pairing service for channel authentication.
|
|
func (s *Server) SetPairingService(ps store.PairingStore) { s.pairingService = ps }
|
|
|
|
// SetAgentsHandler sets the agent CRUD handler.
|
|
func (s *Server) SetAgentsHandler(h *httpapi.AgentsHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetSkillsHandler sets the skill management handler.
|
|
func (s *Server) SetSkillsHandler(h *httpapi.SkillsHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetTracesHandler sets the LLM trace listing handler.
|
|
func (s *Server) SetTracesHandler(h *httpapi.TracesHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetWakeHandler sets the external wake/trigger handler.
|
|
func (s *Server) SetWakeHandler(h *httpapi.WakeHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetMCPHandler sets the MCP server management handler.
|
|
func (s *Server) SetMCPHandler(h *httpapi.MCPHandler) { s.handlers = append(s.handlers, h) }
|
|
func (s *Server) SetMCPOAuthHandler(h *httpapi.MCPOAuthHandler) { s.handlers = append(s.handlers, h) }
|
|
func (s *Server) SetMCPUserCredentialsHandler(h *httpapi.MCPUserCredentialsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetChannelInstancesHandler sets the channel instance CRUD handler.
|
|
func (s *Server) SetChannelInstancesHandler(h *httpapi.ChannelInstancesHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetProvidersHandler sets the provider CRUD handler.
|
|
func (s *Server) SetProvidersHandler(h *httpapi.ProvidersHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetTeamEventsHandler sets the team event history handler.
|
|
func (s *Server) SetTeamEventsHandler(h *httpapi.TeamEventsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetTeamAttachmentsHandler sets the team attachment download handler.
|
|
func (s *Server) SetTeamAttachmentsHandler(h *httpapi.TeamAttachmentsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetWorkspaceUploadHandler sets the team workspace file upload handler.
|
|
func (s *Server) SetWorkspaceUploadHandler(h *httpapi.WorkspaceUploadHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetPendingMessagesHandler sets the pending messages handler.
|
|
func (s *Server) SetPendingMessagesHandler(h *httpapi.PendingMessagesHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetBuiltinToolsHandler sets the builtin tool management handler.
|
|
func (s *Server) SetBuiltinToolsHandler(h *httpapi.BuiltinToolsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetSecureCLIHandler sets the secure CLI credential CRUD handler.
|
|
func (s *Server) SetSecureCLIHandler(h *httpapi.SecureCLIHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetSecureCLIGrantHandler sets the per-agent secure CLI grant handler.
|
|
func (s *Server) SetSecureCLIGrantHandler(h *httpapi.SecureCLIGrantHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetBrowserCookiesHandler sets the selected browser-cookie sync handler.
|
|
func (s *Server) SetBrowserCookiesHandler(h *httpapi.BrowserCookiesHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetPackagesHandler sets the runtime package management handler.
|
|
func (s *Server) SetPackagesHandler(h *httpapi.PackagesHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetGatewayUpgradeHandler sets the host-local gateway upgrade trigger handler.
|
|
func (s *Server) SetGatewayUpgradeHandler(h *httpapi.GatewayUpgradeHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetOAuthHandler sets the OAuth handler (available in all modes).
|
|
func (s *Server) SetOAuthHandler(h *httpapi.OAuthHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetAPIKeysHandler sets the API key management handler.
|
|
func (s *Server) SetAPIKeysHandler(h *httpapi.APIKeysHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetWebhooksAdminHandler registers the webhook admin CRUD handler.
|
|
func (s *Server) SetWebhooksAdminHandler(h *httpapi.WebhooksAdminHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetWebhookMessageHandler registers the POST /v1/webhooks/message runtime handler.
|
|
// Only called when edition.Current().AllowsChannels() is true (Standard edition).
|
|
func (s *Server) SetWebhookMessageHandler(h *httpapi.WebhookMessageHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetWebhookLLMHandler registers the POST /v1/webhooks/llm runtime handler.
|
|
// Available in all editions (Standard + Lite). Localhost-only enforcement is
|
|
// handled by WebhookAuthMiddleware at request time via webhook.LocalhostOnly.
|
|
func (s *Server) SetWebhookLLMHandler(h *httpapi.WebhookLLMHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetTenantsHandler sets the tenant management handler.
|
|
func (s *Server) SetTenantsHandler(h *httpapi.TenantsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetAPIKeyStore sets the API key store for token-based auth lookup.
|
|
func (s *Server) SetAPIKeyStore(st store.APIKeyStore) { s.apiKeyStore = st }
|
|
|
|
// SetFilesHandler sets the workspace file serving handler.
|
|
func (s *Server) SetFilesHandler(h *httpapi.FilesHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetStorageHandler sets the storage file management handler.
|
|
func (s *Server) SetStorageHandler(h *httpapi.StorageHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetMediaUploadHandler sets the media upload handler.
|
|
func (s *Server) SetMediaUploadHandler(h *httpapi.MediaUploadHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetMediaServeHandler sets the media serve handler.
|
|
func (s *Server) SetMediaServeHandler(h *httpapi.MediaServeHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetMemoryHandler sets the memory management handler.
|
|
func (s *Server) SetMemoryHandler(h *httpapi.MemoryHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetKnowledgeGraphHandler sets the knowledge graph handler.
|
|
func (s *Server) SetKnowledgeGraphHandler(h *httpapi.KnowledgeGraphHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetEvolutionHandler sets the evolution metrics + suggestions handler.
|
|
func (s *Server) SetEvolutionHandler(h *httpapi.EvolutionHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetVoicesHandler sets the ElevenLabs voices list + refresh handler.
|
|
func (s *Server) SetVoicesHandler(h *httpapi.VoicesHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetTTSHandler sets the TTS synthesize handler.
|
|
func (s *Server) SetTTSHandler(h *httpapi.TTSHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetTTSConfigHandler sets the per-tenant TTS config handler.
|
|
func (s *Server) SetTTSConfigHandler(h *httpapi.TTSConfigHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetVaultHandler sets the Knowledge Vault document handler.
|
|
func (s *Server) SetVaultHandler(h *httpapi.VaultHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetVaultGraphHandler sets the lightweight graph visualization handler.
|
|
func (s *Server) SetVaultGraphHandler(h *httpapi.VaultGraphHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetEpisodicHandler sets the episodic memory handler.
|
|
func (s *Server) SetEpisodicHandler(h *httpapi.EpisodicHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetOrchestrationHandler sets the orchestration mode handler.
|
|
func (s *Server) SetOrchestrationHandler(h *httpapi.OrchestrationHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetV3FlagsHandler sets the per-agent v3 feature flag handler.
|
|
func (s *Server) SetV3FlagsHandler(h *httpapi.V3FlagsHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetActivityHandler sets the activity audit log handler.
|
|
func (s *Server) SetActivityHandler(h *httpapi.ActivityHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetRuntimeLogsHandler sets the runtime log aggregate handler.
|
|
func (s *Server) SetRuntimeLogsHandler(h *httpapi.RuntimeLogsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetSystemConfigsHandler sets the system configs handler.
|
|
func (s *Server) SetSystemConfigsHandler(h *httpapi.SystemConfigsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetUsageHandler sets the usage analytics handler.
|
|
func (s *Server) SetUsageHandler(h *httpapi.UsageHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetUsageCapsHandler sets usage cap and model pricing handlers.
|
|
func (s *Server) SetUsageCapsHandler(h *httpapi.UsageCapsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetBackupHandler sets the system backup handler.
|
|
func (s *Server) SetBackupHandler(h *httpapi.BackupHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetRestoreHandler sets the system restore handler.
|
|
func (s *Server) SetRestoreHandler(h *httpapi.RestoreHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetBackupS3Handler sets the S3 backup integration handler.
|
|
func (s *Server) SetBackupS3Handler(h *httpapi.BackupS3Handler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetTenantBackupHandler sets the tenant-scoped backup/restore handler.
|
|
func (s *Server) SetTenantBackupHandler(h *httpapi.TenantBackupHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetDocsHandler sets the OpenAPI spec + Swagger UI handler.
|
|
func (s *Server) SetDocsHandler(h *httpapi.DocsHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetEditionHandler sets the edition info handler.
|
|
func (s *Server) SetEditionHandler(h *httpapi.EditionHandler) { s.handlers = append(s.handlers, h) }
|
|
|
|
// SetAgentStore sets the agent store for context injection in tools_invoke.
|
|
func (s *Server) SetAgentStore(as store.AgentStore) { s.agentStore = as }
|
|
|
|
// SetMessageBus sets the message bus for MCP bridge media delivery.
|
|
func (s *Server) SetMessageBus(mb *bus.MessageBus) { s.msgBus = mb }
|
|
|
|
// SetWorkstationsHandler sets the workstations CRUD handler (Standard edition only).
|
|
func (s *Server) SetWorkstationsHandler(h *httpapi.WorkstationsHandler) {
|
|
s.handlers = append(s.handlers, h)
|
|
}
|
|
|
|
// SetVersion sets the server version for health responses.
|
|
func (s *Server) SetVersion(v string) { s.version = v }
|
|
|
|
// SetDB sets the database connection for health check pings.
|
|
func (s *Server) SetDB(db interface{ PingContext(context.Context) error }) { s.db = db }
|
|
|
|
// StartedAt returns the server start time.
|
|
func (s *Server) StartedAt() time.Time { return s.startedAt }
|
|
|
|
// Version returns the server version string.
|
|
func (s *Server) Version() string { return s.version }
|
|
|
|
// StartUpdateChecker starts a background goroutine that periodically checks
|
|
// GitHub for new releases and caches the result for the health endpoint.
|
|
func (s *Server) StartUpdateChecker(ctx context.Context) {
|
|
s.updateChecker = NewUpdateChecker(s.version)
|
|
s.updateChecker.Start(ctx)
|
|
}
|
|
|
|
// ClientList returns a snapshot of all connected clients.
|
|
func (s *Server) ClientList() []*Client {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
list := make([]*Client, 0, len(s.clients))
|
|
for _, c := range s.clients {
|
|
list = append(list, c)
|
|
}
|
|
return list
|
|
}
|
|
|
|
// BroadcastEvent sends an event to all connected clients.
|
|
func (s *Server) BroadcastEvent(event protocol.EventFrame) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
for _, client := range s.clients {
|
|
client.SendEvent(event)
|
|
}
|
|
}
|
|
|
|
// DisconnectByPairing force-closes WebSocket connections authenticated via the
|
|
// given pairing senderID and channel. Called after revoking a paired device so
|
|
// that the revoked client cannot continue operating with its old role.
|
|
func (s *Server) DisconnectByPairing(senderID, channel string) {
|
|
s.mu.RLock()
|
|
var targets []*Client
|
|
for _, c := range s.clients {
|
|
if c.pairedSenderID == senderID && c.pairedChannel == channel {
|
|
targets = append(targets, c)
|
|
}
|
|
}
|
|
s.mu.RUnlock()
|
|
|
|
for _, c := range targets {
|
|
slog.Info("disconnecting revoked paired device", "client", c.id, "sender_id", senderID, "channel", channel)
|
|
c.conn.Close()
|
|
}
|
|
}
|
|
|
|
func (s *Server) registerClient(c *Client) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.clients[c.id] = c
|
|
|
|
// Subscribe to bus events with per-user/team filtering.
|
|
s.eventPub.Subscribe(c.id, func(event bus.Event) {
|
|
if clientCanReceiveEvent(c, event) {
|
|
c.SendEvent(*protocol.NewEvent(event.Name, event.Payload))
|
|
}
|
|
})
|
|
|
|
slog.Info("client connected", "id", c.id)
|
|
}
|
|
|
|
func (s *Server) unregisterClient(c *Client) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
delete(s.clients, c.id)
|
|
s.eventPub.Unsubscribe(c.id)
|
|
if s.logTee != nil {
|
|
s.logTee.Unsubscribe(c.id)
|
|
}
|
|
slog.Info("client disconnected", "id", c.id)
|
|
}
|
|
|
|
// SetLogTee attaches a LogTee so that disconnecting clients are auto-unsubscribed.
|
|
func (s *Server) SetLogTee(lt *LogTee) {
|
|
s.logTee = lt
|
|
}
|
|
|
|
// StartTestServer creates a listener on :0 (random port) and returns the
|
|
// actual address and a start function. Used for integration tests.
|
|
func StartTestServer(s *Server, ctx context.Context) (addr string, start func()) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/ws", s.handleWebSocket)
|
|
mux.HandleFunc("/health", s.handleHealth)
|
|
|
|
isManaged := s.agentStore != nil
|
|
chatHandler := httpapi.NewChatCompletionsHandler(s.agents, s.sessions, isManaged)
|
|
if s.rateLimiter.Enabled() {
|
|
chatHandler.SetRateLimiter(s.rateLimiter.Allow)
|
|
}
|
|
if s.postTurn != nil {
|
|
chatHandler.SetPostTurnProcessor(s.postTurn)
|
|
}
|
|
mux.Handle("/v1/chat/completions", chatHandler)
|
|
|
|
responsesHandler := httpapi.NewResponsesHandler(s.agents, s.sessions)
|
|
if s.postTurn != nil {
|
|
responsesHandler.SetPostTurnProcessor(s.postTurn)
|
|
}
|
|
mux.Handle("/v1/responses", responsesHandler)
|
|
|
|
if s.tools != nil {
|
|
toolsHandler := httpapi.NewToolsInvokeHandler(s.tools, s.agentStore)
|
|
mux.Handle("/v1/tools/invoke", toolsHandler)
|
|
}
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
panic("listen: " + err.Error())
|
|
}
|
|
|
|
s.httpServer = &http.Server{Handler: mux}
|
|
addr = ln.Addr().String()
|
|
|
|
start = func() {
|
|
go func() {
|
|
<-ctx.Done()
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
s.httpServer.Shutdown(shutdownCtx)
|
|
}()
|
|
s.httpServer.Serve(ln)
|
|
}
|
|
|
|
return addr, start
|
|
}
|
|
|
|
// desktopCORS wraps a handler with permissive CORS headers for desktop dev mode.
|
|
// Only active when GOCLAW_DESKTOP=1 (set by desktop app.go).
|
|
func desktopCORS(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, X-GoClaw-Tenant-Id, X-GoClaw-User-Id")
|
|
if r.Method == "OPTIONS" {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|