Add server-side rendered setup UI accessible via `heatwave web`. The dashboard is now re-rendered per request and includes a nav bar linking to the new /setup page. Setup provides full CRUD for profiles, rooms, devices, occupants, AC units (with room assignment), scenario toggles, and forecast fetching — all via POST/redirect/GET forms. - Add ShowNav field to DashboardData for conditional nav bar - Extract fetchForecastForProfile() for reuse by web handler - Create setup.html.tmpl with Tailwind-styled entity sections - Create web_handlers.go with 15 route handlers and flash cookies - Switch web.go from pre-rendered to per-request dashboard rendering - Graceful dashboard fallback when no forecast data exists
107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
package llm
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Anthropic implements Provider using the Anthropic Messages API.
|
|
type Anthropic struct {
|
|
apiKey string
|
|
model string
|
|
client *http.Client
|
|
baseURL string
|
|
}
|
|
|
|
// NewAnthropic creates a new Anthropic provider.
|
|
func NewAnthropic(apiKey, model string, client *http.Client) *Anthropic {
|
|
if client == nil {
|
|
client = &http.Client{Timeout: 60 * time.Second}
|
|
}
|
|
if model == "" {
|
|
model = "claude-sonnet-4-5-20250929"
|
|
}
|
|
return &Anthropic{apiKey: apiKey, model: model, client: client, baseURL: "https://api.anthropic.com"}
|
|
}
|
|
|
|
func (a *Anthropic) Name() string { return "anthropic" }
|
|
|
|
type anthropicRequest struct {
|
|
Model string `json:"model"`
|
|
MaxTokens int `json:"max_tokens"`
|
|
System string `json:"system"`
|
|
Messages []anthropicMessage `json:"messages"`
|
|
}
|
|
|
|
type anthropicMessage struct {
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
type anthropicResponse struct {
|
|
Content []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
} `json:"content"`
|
|
Error *struct {
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
|
|
func (a *Anthropic) call(ctx context.Context, systemPrompt, userMessage string, maxTokens int) (string, error) {
|
|
reqBody := anthropicRequest{
|
|
Model: a.model,
|
|
MaxTokens: maxTokens,
|
|
System: systemPrompt,
|
|
Messages: []anthropicMessage{
|
|
{Role: "user", Content: userMessage},
|
|
},
|
|
}
|
|
body, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return "", fmt.Errorf("marshal request: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.baseURL+"/v1/messages", strings.NewReader(string(body)))
|
|
if err != nil {
|
|
return "", fmt.Errorf("build request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("x-api-key", a.apiKey)
|
|
req.Header.Set("anthropic-version", "2023-06-01")
|
|
|
|
resp, err := a.client.Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("anthropic call: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result anthropicResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return "", fmt.Errorf("decode response: %w", err)
|
|
}
|
|
if result.Error != nil {
|
|
return "", fmt.Errorf("anthropic error: %s", result.Error.Message)
|
|
}
|
|
if len(result.Content) == 0 {
|
|
return "", fmt.Errorf("empty response from anthropic")
|
|
}
|
|
return result.Content[0].Text, nil
|
|
}
|
|
|
|
func (a *Anthropic) Summarize(ctx context.Context, input SummaryInput) (string, error) {
|
|
return a.call(ctx, SummarizeSystemPrompt(), BuildSummaryPrompt(input), 300)
|
|
}
|
|
|
|
func (a *Anthropic) RewriteAction(ctx context.Context, input ActionInput) (string, error) {
|
|
return a.call(ctx, RewriteActionSystemPrompt(), BuildRewriteActionPrompt(input), 100)
|
|
}
|
|
|
|
func (a *Anthropic) GenerateHeatPlan(ctx context.Context, input HeatPlanInput) (string, error) {
|
|
return a.call(ctx, HeatPlanSystemPrompt(), BuildHeatPlanPrompt(input), 2000)
|
|
}
|