anthropic

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: None detected not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package anthropic 实现 Anthropic Messages API 的 ModelProvider.

这是 flyto.ModelProvider 的 Anthropic 官方实现. 内部复用 internal/api/client.go(已有完整的 SSE 解析,流守卫,错误分类). 对外仅暴露 New(Config) 工厂函数,所有 Anthropic 特有能力(Thinking,Caching,Beta flags) 在 Config 中配置,引擎层完全不感知.

升华改进(ELEVATED): 早期实现将 Anthropic API 调用硬编码在 engine.go 中, 无法替换 provider.我们将其提取为独立的工厂方法, 引擎只持有 flyto.ModelProvider 接口,不依赖 Anthropic 特有的任何类型. 替代方案:<保留 engine.go 中的直接调用> - 否决:无法支持 OpenAI/MiniMax 等其他 provider, 也无法在测试中 mock.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// APIKey 是 Anthropic API 密钥(ANTHROPIC_API_KEY 环境变量的值).
	APIKey string

	// BaseURL 覆盖 API 端点(默认 https://api.anthropic.com).
	// 用于测试环境,私有化部署或代理.
	BaseURL string

	// ThinkingBudget 启用 Extended Thinking 并设置 budget_tokens.
	// 0 = 禁用(默认).建议值:8000-16000.
	ThinkingBudget int

	// EnableCaching 启用 Prompt Caching(需要模型支持,如 claude-sonnet-4-6).
	// 开启后系统提示词会自动加 cache_control 标记.
	EnableCaching bool

	// HTTPClient 注入自定义 HTTP 客户端(代理,超时,重试等).
	// nil = 使用默认 http.Client (带 Timeout 字段配置的 ResponseHeaderTimeout).
	// 非 nil 时 consumer 完全接管超时责任,下面的 Timeout 字段被忽略.
	HTTPClient *http.Client

	// Timeout 限制"从请求发出到收到响应首字节"的时间.
	//
	// 通过 http.Transport.ResponseHeaderTimeout 实现,**不影响** SSE 流式响应的后续
	// body 读取 -- 长流式回复 (2-5 分钟) 可以正常读完.这是 LLM provider 的正确
	// 超时语义: 捕捉服务端死等,放行长流式输出.
	//
	// 精妙之处(CLEVER): 不要误用 http.Client.Timeout -- 那会把 SSE 流砍死.
	// 详见 internal/transport/client.go DefaultResponseHeaderTimeout 注释.
	//
	// 0 = 使用 defaultTimeout (60s,适合 Anthropic 云端).
	// 仅当 HTTPClient 为 nil 时生效; 提供自定义 HTTPClient 时此字段被忽略.
	Timeout time.Duration

	// BearerAuth 切换鉴权方式为 "Authorization: Bearer <key>"(默认 "x-api-key: <key>").
	// 用于对接兼容 Anthropic 协议但使用 Bearer 鉴权的代理端点(如 AWS/Azure 转发层).
	BearerAuth bool

	// ModelOverrides 覆盖静态模型表(用于厂商发布新模型但我们尚未更新代码时).
	// key = 模型 ID,value = 模型信息.nil = 使用内置静态表.
	ModelOverrides []flyto.ModelInfo
}

Config 是 Anthropic provider 的配置.

所有 Anthropic 特有功能(Thinking,Caching,Beta flags)在此配置, flyto.Engine 通过 flyto.ModelProvider 接口调用,完全不感知这些字段.

func (Config) GoString

func (c Config) GoString() string

GoString 实现 fmt.GoStringer,防止 %#v 打印时泄露 APIKey. 升华改进(ELEVATED): 默认 %#v 会把结构体所有字段打印出来,APIKey 明文进日志必成安全事故. 替代方案:<自定义 log/slog.LogValuer 接口> - 当前 GoString 覆盖 fmt.Sprintf("%#v") 足矣.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider 是 Anthropic ModelProvider 实现.

func New

func New(cfg Config) *Provider

New 创建 Anthropic ModelProvider.

精妙之处(CLEVER): 返回 *Provider 而非 flyto.ModelProvider 接口-- 调用方可以将 *Provider 用于类型断言以获取 Anthropic 特有方法(未来扩展), 同时也满足 flyto.ModelProvider 接口约束(由编译器静态验证). 如果返回接口,就把这个可能性堵死了.

func (*Provider) Models

func (p *Provider) Models(_ context.Context) ([]flyto.ModelInfo, error)

Models 返回 Anthropic 可用模型列表(静态表).

历史包袱(LEGACY): Anthropic 官方没有提供公开的 /models 列表 API. 我们维护一张静态表,通过 Config.ModelOverrides 允许运行时注入新模型. 理想做法:等 Anthropic 开放模型列表 API 后,改为 live 拉取. 当前条件:2026-04 尚无公开 models 端点.

func (*Provider) Name

func (p *Provider) Name() string

Name 返回 provider 标识.

func (*Provider) Stream

func (p *Provider) Stream(ctx context.Context, req *flyto.Request) (<-chan flyto.Event, error)

Stream 向 Anthropic API 发起流式请求,返回 flyto.Event channel.

升华改进(ELEVATED): 早期方案需要 convertStream goroutine 将 api.StreamEvent 转为 flyto.Event-- 因为 client.CreateMessageStream 曾返回 api.StreamEvent(Anthropic 专有中间类型). 现在 wire.ParseAnthropicStream + StreamGuard 直接产出 flyto.Event, provider 层零转换成本,一行返回即可. 替代方案:<保留 convertStream + goroutine> - 否决:多余的 goroutine 增加内存开销和调试难度.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL