provider

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package provider selects, configures and drives the LLM providers used to generate commit messages. Provider access goes through Fantasy (charm.land/fantasy), a multi-provider AI SDK, behind the small LLM interface.

Index

Constants

View Source
const (
	Anthropic        = "anthropic"
	Azure            = "azure"
	Bedrock          = "bedrock"
	DeepSeek         = "deepseek"
	Google           = "google"
	Groq             = "groq"
	HuggingFace      = "huggingface"
	Mistral          = "mistral"
	Ollama           = "ollama"
	OpenAI           = "openai"
	OpenAICompatible = "openai-compatible"
	OpenRouter       = "openrouter"
	XAI              = "xai"
)

Names of the supported providers, as accepted by the --provider flag.

View Source
const DefaultMaxOutputTokens int64 = 16384

DefaultMaxOutputTokens bounds the size of a generated commit message. It is generous on purpose: reasoning models spend part of this budget thinking before they answer.

Variables

This section is empty.

Functions

func Aliases added in v0.2.0

func Aliases() map[string]string

Aliases returns a copy of the alias to canonical provider name map.

func CallLLM

func CallLLM(
	ctx context.Context,
	llm LLM,
	llmAPICallTimeout time.Duration,
	prompt string,
) (string, error)

CallLLM asks the LLM for a completion, bounding the call with the given timeout on top of the caller's context.

func ChunkDiff

func ChunkDiff(maxChars int, diff string) ([]string, error)

ChunkDiff chunks the diff if it's too big.

func GenerateCommitMessage

func GenerateCommitMessage(
	ctx context.Context,
	llm LLM,
	llmAPICallTimeout time.Duration,
	stats, diff string,
	chunkNumber, totalChunks int,
	additionalInstructions string,
) (string, error)

GenerateCommitMessage generates a commit message using LLM API with additional instructions.

func GenerateCommitMessageLoop

func GenerateCommitMessageLoop(
	llm LLM,
	llmAPICallTimeout time.Duration,
	stats string, chunks []string,
	autoAcceptMode bool,
) (string, error)

GenerateCommitMessageLoop definition.

func HandleTryAgain

func HandleTryAgain() string

handleTryAgain handles the "Try again" choice.

func Names added in v0.2.0

func Names() []string

Names returns the canonical provider names in display order.

Types

type Config added in v0.2.0

type Config struct {
	// Provider is a canonical provider name or alias, see Lookup.
	Provider string

	// Model overrides the provider's default model.
	Model string

	// BaseURL overrides the provider endpoint.
	BaseURL string

	// MaxOutputTokens caps the response size. Zero means
	// DefaultMaxOutputTokens.
	MaxOutputTokens int64
}

Config selects and configures the LLM provider.

type Kind added in v0.2.0

type Kind string

Kind identifies which Fantasy provider package backs a Spec.

const (
	KindAnthropic    Kind = "anthropic"
	KindAzure        Kind = "azure"
	KindBedrock      Kind = "bedrock"
	KindGoogle       Kind = "google"
	KindOpenAI       Kind = "openai"
	KindOpenAICompat Kind = "openai-compat"
	KindOpenRouter   Kind = "openrouter"
)

Supported kinds.

type LLM added in v0.2.0

type LLM interface {
	// Generate returns the model's text response to a single user prompt.
	Generate(ctx context.Context, prompt string) (string, error)

	// Provider returns the provider name.
	Provider() string

	// Model returns the model identifier.
	Model() string
}

LLM is the minimal language model surface committer needs. It hides the underlying SDK so the rest of the application, and the tests, don't depend on it.

func InitializeLLMProvider

func InitializeLLMProvider(ctx context.Context, cfg Config) (LLM, error)

InitializeLLMProvider resolves the configuration against the registry and the environment, and returns a ready to use LLM.

type Spec added in v0.2.0

type Spec struct {
	// Name is the identifier accepted by the --provider flag.
	Name string

	// Description is a short, human readable summary shown in help.
	Description string

	// Kind selects the Fantasy provider package used to talk to the API.
	Kind Kind

	// APIKeyEnvs are the environment variables checked, in order, for the
	// API key. The first non-empty value wins.
	APIKeyEnvs []string

	// APIKeyRequired makes initialization fail early when no API key is
	// found. Providers whose SDK resolves credentials on its own (for
	// example Anthropic auth tokens, or AWS credentials for Bedrock) or that
	// need no key at all (Ollama) leave it false.
	APIKeyRequired bool

	// BaseURLEnvs are the environment variables checked, in order, for an
	// endpoint override. The --base-url flag takes precedence over them.
	BaseURLEnvs []string

	// DefaultBaseURL is used when neither the flag nor the environment
	// provide one. Empty means the SDK default.
	DefaultBaseURL string

	// BaseURLRequired makes initialization fail early when no base URL is
	// available.
	BaseURLRequired bool

	// DefaultModel is used when --model is not set. Empty means the model
	// must be provided explicitly.
	DefaultModel string
}

Spec describes how committer reaches one LLM provider.

func Lookup added in v0.2.0

func Lookup(name string) (Spec, bool)

Lookup finds a provider by canonical name or alias. Matching is case-insensitive and ignores surrounding whitespace.

func Specs added in v0.2.0

func Specs() []Spec

Specs returns the supported providers in display order.

func (Spec) APIKey added in v0.2.0

func (s Spec) APIKey() string

APIKey returns the first API key found in the environment, or an empty string.

func (Spec) BaseURL added in v0.2.0

func (s Spec) BaseURL(override string) string

BaseURL resolves the endpoint to use: the override (from the --base-url flag) wins, then the environment, then the provider default.

Jump to

Keyboard shortcuts

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