From e38f673edc7428c0c836c39f40707cb681defb14 Mon Sep 17 00:00:00 2001 From: Claude Sonnet 5 Date: Fri, 3 Jul 2026 09:03:01 +0000 Subject: feat(provider): add native Anthropic Messages API adapter (Phase 2) Adds internal/provider/anthropic, the first genuinely new provider.Provider implementation on top of Phase 1's provider-neutral tool-use loop, alongside (not replacing) the existing Docker/CLI-subprocess ContainerRunner path for the "claude" agent type: - internal/provider/anthropic: translates the neutral ChatRequest/ChatResponse shape to/from Anthropic's Messages API content-block format (system as a top-level field, tool_use/tool_result blocks, no "tool" role -- tool results become user-role messages), with a per-model-prefix pricing table for CostUSD - internal/retry: IsRateLimitError additively extended to recognize Anthropic's rate_limit_error/overloaded_error/529 shapes - internal/config: RunnersConfig.Anthropic/AnthropicEnabled() gate - internal/cli/serve.go, run.go: register runners["anthropic"] as a NativeRunner when [providers.anthropic].api_key is set and enabled -- tracked as a distinct executions.agent="anthropic" budget bucket, separate from the CLI-subprocess "claude" runner even though both bill the same Anthropic account go build/vet/test -race all pass. No live Anthropic API key is available in this environment, so verification is via fake-httptest-server adapter tests (12 cases, incl. multi-turn tool_result round-trip and rate-limit error matching) plus a Pool/NativeRunner routing test proving agent.type: "anthropic" actually reaches the new provider. Live end-to-end verification against the real API is a follow-up once a key is configured. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01V1moSNCJRcP6kykA4tyUSs --- internal/cli/run.go | 16 +++++++++++++++- internal/cli/serve.go | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'internal/cli') diff --git a/internal/cli/run.go b/internal/cli/run.go index 1c53b1a..a5a6419 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -6,12 +6,14 @@ import ( "os" "os/signal" "syscall" + "time" + "github.com/spf13/cobra" "github.com/thepeterstone/claudomator/internal/executor" + "github.com/thepeterstone/claudomator/internal/provider/anthropic" "github.com/thepeterstone/claudomator/internal/provider/openaicompat" "github.com/thepeterstone/claudomator/internal/storage" "github.com/thepeterstone/claudomator/internal/task" - "github.com/spf13/cobra" ) func newRunCmd() *cobra.Command { @@ -116,6 +118,18 @@ func runTasks(file string, parallel int, dryRun bool) error { } } + if pc, ok := cfg.Providers["anthropic"]; ok && pc.APIKey != "" && cfg.Runners.AnthropicEnabled() { + timeout := time.Duration(0) + if pc.TimeoutSeconds > 0 { + timeout = time.Duration(pc.TimeoutSeconds) * time.Second + } + runners["anthropic"] = &executor.NativeRunner{ + Provider: anthropic.New(pc.APIKey, pc.Endpoint, timeout), + Logger: logger, + LogDir: cfg.LogDir, + DefaultModel: pc.DefaultModel, + } + } pool := executor.NewPool(parallel, runners, store, logger) pool.Classifier = &executor.Classifier{ diff --git a/internal/cli/serve.go b/internal/cli/serve.go index d8ec6a1..d9bf609 100644 --- a/internal/cli/serve.go +++ b/internal/cli/serve.go @@ -10,15 +10,16 @@ import ( "syscall" "time" + "github.com/spf13/cobra" "github.com/thepeterstone/claudomator/internal/api" "github.com/thepeterstone/claudomator/internal/budget" "github.com/thepeterstone/claudomator/internal/config" "github.com/thepeterstone/claudomator/internal/executor" "github.com/thepeterstone/claudomator/internal/notify" + "github.com/thepeterstone/claudomator/internal/provider/anthropic" "github.com/thepeterstone/claudomator/internal/provider/openaicompat" "github.com/thepeterstone/claudomator/internal/storage" "github.com/thepeterstone/claudomator/internal/version" - "github.com/spf13/cobra" ) func newServeCmd() *cobra.Command { @@ -150,6 +151,20 @@ func serve(addr, basePath string) error { logger.Info("local runner registered", "endpoint", cfg.LocalModel.Endpoint, "model", cfg.LocalModel.Model) } + if pc, ok := cfg.Providers["anthropic"]; ok && pc.APIKey != "" && cfg.Runners.AnthropicEnabled() { + timeout := time.Duration(0) + if pc.TimeoutSeconds > 0 { + timeout = time.Duration(pc.TimeoutSeconds) * time.Second + } + runners["anthropic"] = &executor.NativeRunner{ + Provider: anthropic.New(pc.APIKey, pc.Endpoint, timeout), + Logger: logger, + LogDir: cfg.LogDir, + DefaultModel: pc.DefaultModel, + } + logger.Info("anthropic runner registered", "default_model", pc.DefaultModel) + } + pool := executor.NewPool(cfg.MaxConcurrent, runners, store, logger) pool.Classifier = &executor.Classifier{ LLM: localClient, -- cgit v1.2.3