Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/anthropics/anthropic-sdk-go/v2"
|
|
"github.com/anthropics/anthropic-sdk-go/v2/option"
|
|
)
|
|
|
|
type anthropicClient struct {
|
|
client anthropic.Client
|
|
cfg Config
|
|
}
|
|
|
|
func newAnthropic(apiKey string, cfg Config) *anthropicClient {
|
|
return &anthropicClient{
|
|
client: anthropic.NewClient(option.WithAPIKey(apiKey)),
|
|
cfg: cfg,
|
|
}
|
|
}
|
|
|
|
// Models that accept server-side refusal fallbacks. On these, a request a
|
|
// safety classifier declines is re-served by another model in the same call
|
|
// instead of failing.
|
|
var fallbackModels = map[string]bool{
|
|
"claude-fable-5-2": true,
|
|
"claude-fable-5-1": true,
|
|
"claude-opus-5-5": true,
|
|
"claude-opus-5": true,
|
|
"claude-sonnet-5-5": true,
|
|
}
|
|
|
|
func (a *anthropicClient) Generate(ctx context.Context, req Request) (string, error) {
|
|
model := a.cfg.ModelFor(req.Task)
|
|
maxTokens := req.MaxTokens
|
|
if maxTokens <= 0 {
|
|
maxTokens = 16000
|
|
}
|
|
|
|
params := anthropic.BetaMessageNewParams{
|
|
Model: anthropic.Model(model),
|
|
MaxTokens: int64(maxTokens),
|
|
Messages: []anthropic.BetaMessageParam{
|
|
anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock(req.Prompt)),
|
|
},
|
|
}
|
|
if req.System != "" {
|
|
params.System = []anthropic.BetaTextBlockParam{{Text: req.System}}
|
|
}
|
|
// Thinking is left unset: on current models it runs adaptively, and effort
|
|
// is the control. Set effort explicitly, since defaults differ by model.
|
|
if effort := a.cfg.Effort; effort != "" {
|
|
params.OutputConfig.Effort = anthropic.BetaOutputConfigEffort(effort)
|
|
}
|
|
if req.Schema != nil {
|
|
params.OutputConfig.Format = anthropic.BetaJSONOutputFormatParam{Schema: req.Schema}
|
|
}
|
|
if fallbackModels[model] {
|
|
params.Betas = append(params.Betas, anthropic.AnthropicBetaServerSideFallback2026_07_01)
|
|
params.Fallbacks = anthropic.BetaFallbacksParamOfDefault()
|
|
}
|
|
|
|
msg, err := a.client.Beta.Messages.New(ctx, params)
|
|
if err != nil {
|
|
var apierr *anthropic.Error
|
|
if errors.As(err, &apierr) {
|
|
switch apierr.StatusCode {
|
|
case 401:
|
|
return "", fmt.Errorf("Anthropic rejected the API key (401); check it in Settings")
|
|
case 404:
|
|
return "", fmt.Errorf("Anthropic doesn't know the model %q (404); choose another in Settings", model)
|
|
case 429:
|
|
return "", fmt.Errorf("Anthropic rate limit reached (429); try again shortly")
|
|
}
|
|
}
|
|
return "", fmt.Errorf("Anthropic: %w", err)
|
|
}
|
|
|
|
switch msg.StopReason {
|
|
case anthropic.BetaStopReasonRefusal:
|
|
return "", fmt.Errorf("the model declined this request (%s)", msg.StopDetails.Category)
|
|
case anthropic.BetaStopReasonMaxTokens:
|
|
return "", fmt.Errorf("the reply was cut off at %d tokens", maxTokens)
|
|
}
|
|
|
|
var b strings.Builder
|
|
for _, block := range msg.Content {
|
|
if t, ok := block.AsAny().(anthropic.BetaTextBlock); ok {
|
|
b.WriteString(t.Text)
|
|
}
|
|
}
|
|
text := strings.TrimSpace(b.String())
|
|
if text == "" {
|
|
return "", fmt.Errorf("the model returned no text (stop reason %s)", msg.StopReason)
|
|
}
|
|
return text, nil
|
|
}
|