provider

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

internal/provider/message.go

internal/provider/provider.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveProviderName

func ResolveProviderName(model string) string

ResolveProviderName 根据模型名推断 Provider

Types

type AnthropicProvider

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

AnthropicProvider Claude API Provider

func NewAnthropicProvider

func NewAnthropicProvider(apiKey, model, baseURL, authSource string) (*AnthropicProvider, error)

NewAnthropicProvider 创建 Anthropic Provider authSource 标识认证来源,"cc-oauth" 时使用 Bearer token + beta header

func (*AnthropicProvider) Capabilities

func (p *AnthropicProvider) Capabilities() Capabilities

func (*AnthropicProvider) Name

func (p *AnthropicProvider) Name() string

func (*AnthropicProvider) SetModel

func (p *AnthropicProvider) SetModel(model string)

func (*AnthropicProvider) Stream

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

type BlockType

type BlockType int

BlockType 内容块类型

const (
	BlockText BlockType = iota
	BlockThinking
	BlockImage
	BlockToolUse
	BlockToolResult
)

type Capabilities

type Capabilities struct {
	Thinking   bool
	Vision     bool
	ToolUse    bool
	Streaming  bool
	MaxContext int
}

Capabilities Provider 支持的能力

type ContentBlock

type ContentBlock struct {
	Type       BlockType
	Text       string
	Thinking   string
	ImageURL   string
	ToolCall   *ToolCall
	ToolResult *ToolResult
}

ContentBlock 消息内容块

type Event

type Event struct {
	Type     EventType
	Text     string
	Thinking *ThinkingBlock
	ToolCall *ToolCall
	Usage    *Usage
	Error    error
}

Event 流式事件

type EventType

type EventType int

EventType 流式事件类型

const (
	EventTextDelta EventType = iota
	EventThinking
	EventToolUse
	EventUsage
	EventDone
	EventError
)

type Message

type Message struct {
	Role    Role
	Content []ContentBlock
}

Message 统一消息格式

func NewTextMessage

func NewTextMessage(role Role, text string) Message

NewTextMessage 创建纯文本消息

func NewToolResultMessage

func NewToolResultMessage(toolUseID, content string, isError bool) Message

NewToolResultMessage 创建工具结果消息

func (Message) TextContent

func (m Message) TextContent() string

TextContent 提取消息中的纯文本内容

func (Message) ToolCalls

func (m Message) ToolCalls() []*ToolCall

ToolCalls 提取消息中的工具调用

type OpenAIProvider

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

OpenAIProvider OpenAI API Provider(兼容 OpenRouter / 任意兼容端点)

func NewOpenAIProvider

func NewOpenAIProvider(apiKey, model, baseURL string) (*OpenAIProvider, error)

NewOpenAIProvider 创建 OpenAI Provider

func (*OpenAIProvider) Capabilities

func (p *OpenAIProvider) Capabilities() Capabilities

func (*OpenAIProvider) Name

func (p *OpenAIProvider) Name() string

func (*OpenAIProvider) SetModel

func (p *OpenAIProvider) SetModel(model string)

func (*OpenAIProvider) Stream

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

type Provider

type Provider interface {
	// Name 返回 Provider 标识符,如 "anthropic"、"openai"
	Name() string
	// Stream 发起流式对话请求,返回事件 channel
	// 真实请求模型由 req.Model 决定(非空时覆盖 provider 默认模型)
	Stream(ctx context.Context, req *Request) (<-chan Event, error)
	// Capabilities 返回该 Provider 支持的能力(基于当前模型)
	Capabilities() Capabilities
	// SetModel 更新 provider 的默认模型(影响 Capabilities 和无 req.Model 时的兜底)
	SetModel(model string)
}

Provider 多模型提供者接口

func NewProvider

func NewProvider(name, apiKey, model, baseURL, authSource string) (Provider, error)

NewProvider 根据名称创建 Provider 实例 authSource 标识认证来源("cc-oauth" / "env:xxx" / "config" 等)

type Request

type Request struct {
	Model        string
	System       string        // 兼容:单字符串 system prompt
	SystemBlocks []SystemBlock // 优先:分块 system prompt(支持 cache_control)
	Messages     []Message
	Tools        []ToolDef
	MaxTokens    int
	Temperature  float64
}

Request 统一的 API 请求

func (*Request) EffectiveSystemBlocks

func (r *Request) EffectiveSystemBlocks() []SystemBlock

EffectiveSystemBlocks 返回生效的 SystemBlock 列表 如果 SystemBlocks 非空则使用它,否则将 System string 转为单个 block

type Role

type Role string

Role 消息角色

const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
)

type SystemBlock

type SystemBlock struct {
	Text         string `json:"text"`
	CacheControl string `json:"cache_control,omitempty"` // "ephemeral" 表示启用缓存
}

SystemBlock 系统提示词块(支持 cache_control)

type ThinkingBlock

type ThinkingBlock struct {
	Text string `json:"text"`
}

ThinkingBlock Extended Thinking 内容

type ToolCall

type ToolCall struct {
	ID    string `json:"id"`
	Name  string `json:"name"`
	Input string `json:"input"` // JSON string
}

ToolCall API 返回的工具调用

type ToolDef

type ToolDef struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	InputSchema map[string]any `json:"input_schema"`
}

ToolDef 工具定义(传给 API 的 schema)

type ToolResult

type ToolResult struct {
	ToolUseID string `json:"tool_use_id"`
	Content   string `json:"content"`
	IsError   bool   `json:"is_error,omitempty"`
}

ToolResult 工具执行结果

type Usage

type Usage struct {
	InputTokens              int `json:"input_tokens"`
	OutputTokens             int `json:"output_tokens"`
	CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
	CacheReadInputTokens     int `json:"cache_read_input_tokens,omitempty"`
}

Usage token 使用量

Jump to

Keyboard shortcuts

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