chatprovider

package
v2.33.5 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderCoderOwnerID identifies the Coder user who owns the chat.
	HeaderCoderOwnerID = "X-Coder-Owner-Id"
	// HeaderCoderChatID identifies the top-level (parent) chat.
	// For root chats this is the chat's own ID; for subchats it
	// is the parent chat's ID.
	HeaderCoderChatID = "X-Coder-Chat-Id"
	// HeaderCoderSubchatID identifies the current subchat. Only
	// present when the request originates from a child chat.
	HeaderCoderSubchatID = "X-Coder-Subchat-Id"
	// HeaderCoderWorkspaceID identifies the workspace associated
	// with the chat, if any.
	HeaderCoderWorkspaceID = "X-Coder-Workspace-Id"
)

Header constants sent on upstream LLM API requests so that intermediaries (e.g. aibridged) can correlate traffic back to Coder entities.

Variables

This section is empty.

Functions

func CloneWithPreviousResponseID

func CloneWithPreviousResponseID(
	opts fantasy.ProviderOptions,
	previousResponseID string,
) fantasy.ProviderOptions

CloneWithPreviousResponseID shallow-clones the provider options map and the OpenAI Responses entry, setting PreviousResponseID on the clone. The original map and entry are not mutated.

func CoderHeaders

func CoderHeaders(chat database.Chat) map[string]string

CoderHeaders builds the set of Coder identity headers to attach to outgoing LLM API requests for the given chat.

func CoderHeadersFromIDs

func CoderHeadersFromIDs(
	ownerID uuid.UUID,
	chatID uuid.UUID,
	parentChatID uuid.NullUUID,
	workspaceID uuid.NullUUID,
) map[string]string

CoderHeadersFromIDs is a convenience form of CoderHeaders for call sites that do not have a full database.Chat in scope.

func IsEnvPresetProvider

func IsEnvPresetProvider(provider string) bool

IsEnvPresetProvider reports whether provider supports env presets.

func IsResponsesStoreEnabled

func IsResponsesStoreEnabled(opts fantasy.ProviderOptions) bool

IsResponsesStoreEnabled checks if the OpenAI Responses provider options are present and have Store set to true. When true, the provider stores conversation history server-side, enabling follow-up chaining via PreviousResponseID.

func MergeMissingModelCostConfig

func MergeMissingModelCostConfig(
	dst **codersdk.ModelCostConfig,
	defaults *codersdk.ModelCostConfig,
)

MergeMissingModelCostConfig fills unset pricing metadata from defaults.

func MergeMissingProviderOptions

func MergeMissingProviderOptions(
	dst **codersdk.ChatModelProviderOptions,
	defaults *codersdk.ChatModelProviderOptions,
)

MergeMissingProviderOptions fills unset provider option fields from defaults.

func ModelFromConfig

func ModelFromConfig(
	providerHint string,
	modelName string,
	providerKeys ProviderAPIKeys,
	userAgent string,
	extraHeaders map[string]string,
	httpClient *http.Client,
) (fantasy.LanguageModel, error)

ModelFromConfig resolves a provider/model pair and constructs a fantasy language model client using the provided provider credentials. The userAgent is sent as the User-Agent header on every outgoing LLM API request. extraHeaders, when non-nil, are sent as additional HTTP headers on every request. httpClient, when non-nil, is used for all provider HTTP requests.

func NormalizeProvider

func NormalizeProvider(provider string) string

NormalizeProvider canonicalizes a provider name.

func OpenAITextVerbosityFromChat

func OpenAITextVerbosityFromChat(value *string) *fantasyopenai.TextVerbosity

OpenAITextVerbosityFromChat normalizes chat-config text verbosity values for OpenAI and returns the canonical provider verbosity value.

func ProviderAllowsAmbientCredentials added in v2.33.0

func ProviderAllowsAmbientCredentials(provider string) bool

ProviderAllowsAmbientCredentials reports whether provider can use ambient credentials from the Coder server instead of an explicit API key.

func ProviderDisplayName

func ProviderDisplayName(provider string) string

ProviderDisplayName returns a default display name for a provider.

func ProviderOptionsFromChatModelConfig

func ProviderOptionsFromChatModelConfig(
	model fantasy.LanguageModel,
	options *codersdk.ChatModelProviderOptions,
) fantasy.ProviderOptions

ProviderOptionsFromChatModelConfig converts chat model provider options to fantasy provider options used for inference calls.

func PruneDisabledProviderKeys

func PruneDisabledProviderKeys(keys *ProviderAPIKeys, enabledProviders map[string]struct{})

PruneDisabledProviderKeys removes entries from keys that do not belong to an enabled provider. It clears ByProvider and BaseURLByProvider entries for disabled providers and zeroes the legacy OpenAI and Anthropic fields when those providers are not enabled.

func ReasoningEffortFromChat

func ReasoningEffortFromChat(provider string, value *string) *string

ReasoningEffortFromChat normalizes chat-config reasoning effort values for a provider and returns the canonical provider effort value.

func ResolveModelWithProviderHint

func ResolveModelWithProviderHint(modelName, providerHint string) (provider string, model string, err error)

func SupportedProviders

func SupportedProviders() []string

SupportedProviders returns all chat providers supported by Fantasy.

func UserAgent

func UserAgent() string

UserAgent returns the User-Agent string sent on all outgoing LLM API requests made by Coder's built-in chat (chatd). The format mirrors conventions used by other coding agents so that LLM providers can identify traffic originating from Coder.

Example: coder-agents/v2.21.0 (linux/amd64)

Types

type ConfiguredModel

type ConfiguredModel struct {
	Provider    string
	Model       string
	DisplayName string
}

ConfiguredModel is an enabled model loaded from database config.

type ConfiguredProvider

type ConfiguredProvider struct {
	ProviderID                 uuid.UUID
	Provider                   string
	APIKey                     string
	BaseURL                    string
	CentralAPIKeyEnabled       bool
	AllowUserAPIKey            bool
	AllowCentralAPIKeyFallback bool
}

ConfiguredProvider is an enabled provider loaded from database config.

type ModelCatalog

type ModelCatalog struct{}

func NewModelCatalog

func NewModelCatalog() *ModelCatalog

func (*ModelCatalog) ListConfiguredModels

func (*ModelCatalog) ListConfiguredModels(
	configuredProviders []ConfiguredProvider,
	configuredModels []ConfiguredModel,
	availabilityByProvider map[string]ProviderAvailability,
	enabledProviders map[string]struct{},
) (codersdk.ChatModelsResponse, bool)

ListConfiguredModels returns a model catalog from enabled DB-backed model configs. The second return value reports whether DB-backed models were used.

func (*ModelCatalog) ListConfiguredProviderAvailability

func (*ModelCatalog) ListConfiguredProviderAvailability(
	availabilityByProvider map[string]ProviderAvailability,
	enabledProviders map[string]struct{},
) codersdk.ChatModelsResponse

ListConfiguredProviderAvailability returns provider availability derived from the policy-aware availability map for enabled providers.

type ProviderAPIKeys

type ProviderAPIKeys struct {
	OpenAI            string
	Anthropic         string
	ByProvider        map[string]string
	BaseURLByProvider map[string]string
}

ProviderAPIKeys contains API keys for provider calls.

func MergeProviderAPIKeys

func MergeProviderAPIKeys(fallback ProviderAPIKeys, providers []ConfiguredProvider) ProviderAPIKeys

MergeProviderAPIKeys overlays configured provider keys over fallback keys.

func ResolveUserProviderKeys

func ResolveUserProviderKeys(
	fallback ProviderAPIKeys,
	providers []ConfiguredProvider,
	userKeys []UserProviderKey,
) (ProviderAPIKeys, map[string]ProviderAvailability)

ResolveUserProviderKeys computes effective API keys and per-provider availability for a given user. It considers the provider's credential policy flags alongside central (DB/deployment) keys and the user's personal keys.

func (ProviderAPIKeys) APIKey

func (k ProviderAPIKeys) APIKey(provider string) string

APIKey returns the effective API key for a provider.

func (ProviderAPIKeys) BaseURL

func (k ProviderAPIKeys) BaseURL(provider string) string

BaseURL returns the configured base URL for a provider.

func (ProviderAPIKeys) HasProvider added in v2.33.0

func (k ProviderAPIKeys) HasProvider(provider string) bool

HasProvider reports whether a provider has an explicit resolved entry in the provider key map, even when the resolved key is empty.

type ProviderAvailability

type ProviderAvailability struct {
	Available         bool
	UnavailableReason codersdk.ChatModelProviderUnavailableReason
}

ProviderAvailability describes whether a provider has a usable API key and, if not, why.

type UserProviderKey

type UserProviderKey struct {
	ChatProviderID uuid.UUID
	APIKey         string
}

UserProviderKey is a user-supplied API key for a specific provider.

Jump to

Keyboard shortcuts

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