chat

package
v0.31.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 14 Imported by: 0

README

Chat (go-tool-base adapter)

This package is go-tool-base's thin adapter over the standalone multi-provider AI chat client, gitlab.com/phpboyscout/go/chat.

The client itself — the ChatClient API, ReAct tool-calling loop, streaming, cross-provider fallback, persistence, and multimodal input — lives in the module and its per-provider modules (chat-anthropic, chat-openai, chat-gemini). Full docs: chat.go.phpboyscout.uk.

This adapter adds go-tool-base's framework integration:

  • SettingsFromProps / NewFromProps / NewWithFallbackFromProps — map a Props instance and GTB's Viper config into the module's typed Settings.
  • the GTB config-key schema (ConfigKeyClaudeKey, ConfigKeyAIProvider, …) and the credential-resolution precedence.
  • the hardened pkg/http transport and pkg/credentials keychain lookup, injected into the module via its seams.
  • blank-imports of all three provider modules, so every provider is registered.

It also re-exports the module's public types/constructors (chat.Config, chat.New, chat.Tool, …) so existing GTB call sites reference them from this package unchanged. See docs/explanation/components/chat/index.md and the migration note at docs/reference/migration/v0.x-chat-extracted.md.

Documentation

Overview

Package chat is go-tool-base's thin adapter over the standalone multi-provider chat client gitlab.com/phpboyscout/go/chat. It maps GTB's Props/Viper config into the module's typed Settings (see config_adapter.go), owns the GTB config key schema (see constants.go), and blank-imports every provider module (see providers.go). The declarations below re-export the module's public API so the existing GTB call sites keep referencing chat.Config, chat.New, etc. from this package unchanged.

Index

Constants

View Source
const (
	ConfigKeyOpenAIKey      = configRootOpenAI + ".key"
	ConfigKeyOpenAIEnv      = configRootOpenAI + ".env"
	ConfigKeyOpenAIKeychain = configRootOpenAI + ".keychain"
	EnvOpenAIKey            = "OPENAI_API_KEY"

	ConfigKeyClaudeKey      = configRootClaude + ".key"
	ConfigKeyClaudeEnv      = configRootClaude + ".env"
	ConfigKeyClaudeKeychain = configRootClaude + ".keychain"
	EnvClaudeKey            = "ANTHROPIC_API_KEY"

	ConfigKeyGeminiKey      = configRootGemini + ".key"
	ConfigKeyGeminiEnv      = configRootGemini + ".env"
	ConfigKeyGeminiKeychain = configRootGemini + ".keychain"
	EnvGeminiKey            = "GEMINI_API_KEY"
)

Per-provider credential config keys.

  • ConfigKey<Provider>Key — full config path for the literal API key.
  • ConfigKey<Provider>Env — full config path for an env-var reference (the value stored is the NAME of an env var holding the secret).
  • ConfigKey<Provider>Keychain — full config path for an OS-keychain reference.
  • Env<Provider>Key — well-known unprefixed environment variable used as the ecosystem fallback when no config is present.
View Source
const (
	ProviderOpenAI           = gochat.ProviderOpenAI
	ProviderOpenAICompatible = gochat.ProviderOpenAICompatible
	ProviderClaude           = gochat.ProviderClaude
	ProviderClaudeLocal      = gochat.ProviderClaudeLocal
	ProviderGemini           = gochat.ProviderGemini

	EventTextDelta     = gochat.EventTextDelta
	EventToolCallStart = gochat.EventToolCallStart
	EventToolCallEnd   = gochat.EventToolCallEnd
	EventComplete      = gochat.EventComplete
	EventError         = gochat.EventError

	FailoverNext  = gochat.FailoverNext
	FailoverFatal = gochat.FailoverFatal

	DefaultModelClaude        = gochat.DefaultModelClaude
	DefaultModelOpenAI        = gochat.DefaultModelOpenAI
	DefaultModelGemini        = gochat.DefaultModelGemini
	DefaultMaxSteps           = gochat.DefaultMaxSteps
	DefaultMaxTokensClaude    = gochat.DefaultMaxTokensClaude
	DefaultMaxTokensOpenAI    = gochat.DefaultMaxTokensOpenAI
	DefaultMaxTokensGemini    = gochat.DefaultMaxTokensGemini
	DefaultChatRequestTimeout = gochat.DefaultChatRequestTimeout
	MaxBaseURLLength          = gochat.MaxBaseURLLength
)

Re-exported constants (provider names, streaming events, defaults).

View Source
const ConfigKeyAIFallbackEnabled = "ai.fallback.enabled"

ConfigKeyAIFallbackEnabled toggles cross-provider failover.

View Source
const ConfigKeyAIFallbackProviders = "ai.fallback.providers"

ConfigKeyAIFallbackProviders is the ordered list of provider names to fail over across; index 0 is the primary.

View Source
const ConfigKeyAIProvider = "ai.provider"

ConfigKeyAIProvider is the config key for the AI provider.

View Source
const ConfigKeyAIRequestTimeout = "ai.request_timeout"

ConfigKeyAIRequestTimeout is the config key for the per-request AI timeout (a duration like "8m"). Overrides DefaultChatRequestTimeout.

View Source
const EnvAIProvider = "AI_PROVIDER"

EnvAIProvider is the environment variable for overriding the AI provider.

Variables

View Source
var (
	ErrInvalidBaseURL     = gochat.ErrInvalidBaseURL
	ErrInvalidSnapshotID  = gochat.ErrInvalidSnapshotID
	ErrMediaRejected      = gochat.ErrMediaRejected
	ErrMediaUnsupported   = gochat.ErrMediaUnsupported
	DefaultFailoverPolicy = gochat.DefaultFailoverPolicy
)

Re-exported sentinels and the default failover policy.

View Source
var (
	New                     = gochat.New
	NewSnapshot             = gochat.NewSnapshot
	NewFileStore            = gochat.NewFileStore
	WithEncryption          = gochat.WithEncryption
	WithLogger              = gochat.WithLogger
	GenerateEncryptionKey   = gochat.GenerateEncryptionKey
	ValidateBaseURL         = gochat.ValidateBaseURL
	ValidateSnapshotID      = gochat.ValidateSnapshotID
	ValidateMediaSet        = gochat.ValidateMediaSet
	NewFallback             = gochat.NewFallback
	NewFallbackFromSettings = gochat.NewFallbackFromSettings
	NewFallbackFromConfigs  = gochat.NewFallbackFromConfigs
	NewWithFallbackSettings = gochat.NewWithFallbackSettings
	WithFallbackLogger      = gochat.WithFallbackLogger
	WithFailoverPolicy      = gochat.WithFailoverPolicy
	WithOnFailover          = gochat.WithOnFailover
	WithStrictToolContext   = gochat.WithStrictToolContext
	RegisterProvider        = gochat.RegisterProvider
	RegisterStatusExtractor = gochat.RegisterStatusExtractor
	ResolveAPIKey           = gochat.ResolveAPIKey
	ChatHTTPClient          = gochat.ChatHTTPClient
	DispatchToolExecution   = gochat.DispatchToolExecution
	ExecuteTool             = gochat.ExecuteTool
	ExecuteToolsParallel    = gochat.ExecuteToolsParallel
	NewUsage                = gochat.NewUsage
)

Re-exported constructors and helpers (function values forwarding to the module).

Functions

func GenerateSchema

func GenerateSchema[T any]() any

GenerateSchema forwards to the module's generic schema generator (a generic function cannot be re-exported as a value, so it is wrapped).

Types

type ChatClient

type ChatClient = gochat.ChatClient

ChatClient is the multi-provider chat client interface.

func NewFromProps added in v0.30.0

func NewFromProps(ctx context.Context, p *props.Props, cfg Config) (ChatClient, error)

NewFromProps adapts GTB props into typed chat settings, then constructs a provider client with the reusable package constructor.

func NewWithFallback added in v0.22.0

func NewWithFallback(ctx context.Context, p *props.Props, cfg Config, opts ...FallbackOption) (ChatClient, error)

NewWithFallback adapts GTB props and framework config before constructing a chat client with optional provider failover.

func NewWithFallbackFromProps added in v0.30.0

func NewWithFallbackFromProps(ctx context.Context, p *props.Props, cfg Config, opts ...FallbackOption) (ChatClient, error)

NewWithFallbackFromProps adapts GTB props into package-owned chat settings, then constructs either a single provider client or a fallback composite.

type Config

type Config = gochat.Config

Config carries provider behaviour for a client.

type ConversationStore

type ConversationStore = gochat.ConversationStore

ConversationStore persists conversation snapshots.

type CredentialConfig added in v0.30.0

type CredentialConfig = gochat.CredentialConfig

CredentialConfig is the package-owned provider credential shape.

type FailoverDecision added in v0.22.0

type FailoverDecision = gochat.FailoverDecision

FailoverDecision is the outcome of classifying a provider error.

type FailoverPolicy added in v0.22.0

type FailoverPolicy = gochat.FailoverPolicy

FailoverPolicy classifies a provider error for failover.

type FallbackConfig added in v0.30.0

type FallbackConfig = gochat.FallbackConfig

FallbackConfig is the package-owned shape for ai.fallback.*.

type FallbackOption added in v0.22.0

type FallbackOption = gochat.FallbackOption

FallbackOption configures a fallback composite.

type FileStoreOption

type FileStoreOption = gochat.FileStoreOption

FileStoreOption configures a FileStore.

type HTTPStatusExtractor added in v0.31.1

type HTTPStatusExtractor = gochat.HTTPStatusExtractor

HTTPStatusExtractor pulls an HTTP status from a provider error.

type KeychainLookup added in v0.31.1

type KeychainLookup = gochat.KeychainLookup

KeychainLookup resolves a keychain reference to a secret.

type Media added in v0.28.0

type Media = gochat.Media

Media is a multimodal attachment.

type PersistentChatClient

type PersistentChatClient = gochat.PersistentChatClient

PersistentChatClient is the optional Save/Restore interface.

type Provider

type Provider = gochat.Provider

Provider names an AI service provider.

type ProviderFactory

type ProviderFactory = gochat.ProviderFactory

ProviderFactory constructs a ChatClient for a provider.

type ResolvedMedia added in v0.31.1

type ResolvedMedia = gochat.ResolvedMedia

ResolvedMedia is an attachment that passed the safety filter.

type RuntimeConfig added in v0.30.0

type RuntimeConfig = gochat.RuntimeConfig

RuntimeConfig is the package-owned shape for the ai.* config section.

type Settings added in v0.30.0

type Settings = gochat.Settings

Settings bundles Config with the package-owned logger.

func SettingsFromProps added in v0.30.0

func SettingsFromProps(p *props.Props, cfg Config) (Settings, error)

SettingsFromProps adapts GTB props and framework config into package-owned chat settings while preserving the existing config key layout and precedence.

type Snapshot

type Snapshot = gochat.Snapshot

Snapshot is a serialisable conversation snapshot.

type SnapshotSummary

type SnapshotSummary = gochat.SnapshotSummary

SnapshotSummary is the metadata for a stored snapshot.

type StreamCallback

type StreamCallback = gochat.StreamCallback

StreamCallback receives streaming events.

type StreamEvent

type StreamEvent = gochat.StreamEvent

StreamEvent is a single streaming event.

type StreamEventType

type StreamEventType = gochat.StreamEventType

StreamEventType enumerates streaming event kinds.

type StreamToolCall

type StreamToolCall = gochat.StreamToolCall

StreamToolCall describes a tool call surfaced during streaming.

type StreamingChatClient

type StreamingChatClient = gochat.StreamingChatClient

StreamingChatClient is the optional streaming interface.

type Tool

type Tool = gochat.Tool

Tool is a callable exposed to the model.

type ToolCall

type ToolCall = gochat.ToolCall

ToolCall is a single tool invocation request.

type ToolResult

type ToolResult = gochat.ToolResult

ToolResult is the result of a tool execution.

type Usage added in v0.17.0

type Usage = gochat.Usage

Usage reports token consumption.

type UsageTracker added in v0.31.1

type UsageTracker = gochat.UsageTracker

UsageTracker accumulates per-round-trip usage (embed to satisfy Usage()).

Jump to

Keyboard shortcuts

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