summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/api/budget.go36
-rw-r--r--internal/api/budget_test.go68
-rw-r--r--internal/api/server.go2
-rw-r--r--internal/cli/serve.go3
4 files changed, 109 insertions, 0 deletions
diff --git a/internal/api/budget.go b/internal/api/budget.go
new file mode 100644
index 0000000..eee5d09
--- /dev/null
+++ b/internal/api/budget.go
@@ -0,0 +1,36 @@
+package api
+
+import (
+ "net/http"
+
+ "github.com/thepeterstone/claudomator/internal/budget"
+)
+
+// budgetReporter exposes per-provider spend headroom. Satisfied by
+// *budget.Accountant; an interface keeps the handler testable.
+type budgetReporter interface {
+ All() ([]budget.Headroom, error)
+}
+
+// SetBudget wires the budget accountant so GET /api/budget can report headroom.
+func (s *Server) SetBudget(b budgetReporter) {
+ s.budget = b
+}
+
+// handleGetBudget returns per-provider rolling-window spend headroom. When no
+// budget is configured it returns an empty list so the UI simply shows nothing.
+func (s *Server) handleGetBudget(w http.ResponseWriter, _ *http.Request) {
+ if s.budget == nil {
+ writeJSON(w, http.StatusOK, []budget.Headroom{})
+ return
+ }
+ hs, err := s.budget.All()
+ if err != nil {
+ writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
+ return
+ }
+ if hs == nil {
+ hs = []budget.Headroom{}
+ }
+ writeJSON(w, http.StatusOK, hs)
+}
diff --git a/internal/api/budget_test.go b/internal/api/budget_test.go
new file mode 100644
index 0000000..d59836d
--- /dev/null
+++ b/internal/api/budget_test.go
@@ -0,0 +1,68 @@
+package api
+
+import (
+ "encoding/json"
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/thepeterstone/claudomator/internal/budget"
+)
+
+type fakeBudget struct {
+ hs []budget.Headroom
+ err error
+}
+
+func (f fakeBudget) All() ([]budget.Headroom, error) { return f.hs, f.err }
+
+func TestHandleGetBudget_NoBudgetConfigured_ReturnsEmpty(t *testing.T) {
+ srv, _ := testServer(t)
+ req := httptest.NewRequest("GET", "/api/budget", nil)
+ w := httptest.NewRecorder()
+ srv.Handler().ServeHTTP(w, req)
+
+ if w.Code != http.StatusOK {
+ t.Fatalf("status: want 200, got %d", w.Code)
+ }
+ var hs []budget.Headroom
+ if err := json.Unmarshal(w.Body.Bytes(), &hs); err != nil {
+ t.Fatalf("unmarshal: %v", err)
+ }
+ if len(hs) != 0 {
+ t.Errorf("want empty list, got %+v", hs)
+ }
+}
+
+func TestHandleGetBudget_ReportsHeadroom(t *testing.T) {
+ srv, _ := testServer(t)
+ srv.SetBudget(fakeBudget{hs: []budget.Headroom{
+ {Provider: "claude", Limited: true, Limit: 10, Spent: 4, Remaining: 6, Fraction: 0.6},
+ }})
+ req := httptest.NewRequest("GET", "/api/budget", nil)
+ w := httptest.NewRecorder()
+ srv.Handler().ServeHTTP(w, req)
+
+ if w.Code != http.StatusOK {
+ t.Fatalf("status: want 200, got %d", w.Code)
+ }
+ var hs []budget.Headroom
+ if err := json.Unmarshal(w.Body.Bytes(), &hs); err != nil {
+ t.Fatalf("unmarshal: %v", err)
+ }
+ if len(hs) != 1 || hs[0].Provider != "claude" || hs[0].Remaining != 6 {
+ t.Errorf("unexpected headroom: %+v", hs)
+ }
+}
+
+func TestHandleGetBudget_ErrorReturns500(t *testing.T) {
+ srv, _ := testServer(t)
+ srv.SetBudget(fakeBudget{err: errors.New("db down")})
+ req := httptest.NewRequest("GET", "/api/budget", nil)
+ w := httptest.NewRecorder()
+ srv.Handler().ServeHTTP(w, req)
+ if w.Code != http.StatusInternalServerError {
+ t.Errorf("want 500, got %d", w.Code)
+ }
+}
diff --git a/internal/api/server.go b/internal/api/server.go
index a4b7ea1..ffa8cd4 100644
--- a/internal/api/server.go
+++ b/internal/api/server.go
@@ -61,6 +61,7 @@ type Server struct {
pushStore pushSubscriptionStore
dropsDir string
llm *llm.Client
+ budget budgetReporter // optional; per-provider spend headroom for GET /api/budget
}
// SetAPIToken configures a bearer token that must be supplied to access the API.
@@ -147,6 +148,7 @@ func (s *Server) routes() {
s.mux.HandleFunc("GET /api/tasks/{id}/events", s.handleListTaskEvents)
s.mux.HandleFunc("GET /api/executions", s.handleListRecentExecutions)
s.mux.HandleFunc("GET /api/stats", s.handleGetDashboardStats)
+ s.mux.HandleFunc("GET /api/budget", s.handleGetBudget)
s.mux.HandleFunc("GET /api/agents/status", s.handleGetAgentStatus)
s.mux.HandleFunc("GET /api/executions/{id}", s.handleGetExecution)
s.mux.HandleFunc("GET /api/executions/{id}/log", s.handleGetExecutionLog)
diff --git a/internal/cli/serve.go b/internal/cli/serve.go
index 55fdaf5..7afa678 100644
--- a/internal/cli/serve.go
+++ b/internal/cli/serve.go
@@ -171,6 +171,9 @@ func serve(addr string) error {
pool.RecoverStaleBlocked()
srv := api.NewServer(store, pool, agentRegistry, logger, cfg.ClaudeBinaryPath, cfg.GeminiBinaryPath)
+ if accountant != nil {
+ srv.SetBudget(accountant)
+ }
// Configure notifiers: combine webhook (if set) with web push.
notifiers := []notify.Notifier{}