config

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package config provides configuration management for the OpenRouter CLI.

Index

Constants

View Source
const (
	// DefaultModel is the default model to use when not configured.
	DefaultModel = "moonshotai/kimi-k2.5"

	// DefaultImageModel is the default model for image generation.
	DefaultImageModel = "google/gemini-2.5-flash-image"

	// DefaultStreamTimeout is the default timeout for streaming requests.
	DefaultStreamTimeout = 5 * time.Minute

	// StreamChunkTimeout is the timeout for waiting for a single chunk.
	// If no data is received within this time, the stream is considered hung.
	StreamChunkTimeout = 30 * time.Second

	// DefaultTerminalWidth is the default terminal width when auto-detection fails.
	DefaultTerminalWidth = 80

	// EscDoublePressTimeout is the timeout for double-press ESC actions.
	EscDoublePressTimeout = 2 * time.Second

	// PreviewTruncateLength is the max length for session preview text.
	PreviewTruncateLength = 50

	// StreamChannelBuffer is the buffer size for stream chunk channels.
	StreamChannelBuffer = 100
)

Default configuration values.

Variables

View Source
var ErrSessionNotFound = errors.New("session not found")

ErrSessionNotFound is returned when a session cannot be found.

View Source
var GetConfigDir = func() (string, error) {
	configDir, err := os.UserConfigDir()
	if err != nil {
		return "", fmt.Errorf("failed to get user config directory: %w", err)
	}
	return filepath.Join(configDir, "openrouter"), nil
}

GetConfigDir returns the platform-specific config directory for openrouter. This is a variable to allow mocking in tests.

View Source
var GetSessionDir = func() (string, error) {
	configDir, err := GetConfigDir()
	if err != nil {
		return "", err
	}
	return filepath.Join(configDir, "sessions"), nil
}

GetSessionDir returns the directory where sessions are stored. This is a variable to allow mocking in tests.

Functions

func GetConfigPath

func GetConfigPath() (string, error)

GetConfigPath returns the full path to the config file.

func PromptForAPIKey

func PromptForAPIKey() (string, error)

PromptForAPIKey interactively prompts the user for their API key.

func Save

func Save(cfg *Config) error

Save writes the config to disk with secure permissions.

Types

type AppConfig

type AppConfig struct {
	// APIKey is the OpenRouter API key.
	APIKey string

	// DefaultModel is the default model to use.
	DefaultModel string

	// StreamTimeout is the timeout for streaming requests.
	StreamTimeout time.Duration

	// TerminalWidth is the terminal width (0 = auto-detect).
	TerminalWidth int
}

AppConfig holds all runtime configuration.

func NewAppConfig

func NewAppConfig() *AppConfig

NewAppConfig creates a new AppConfig with default values.

type Config

type Config struct {
	APIKey            string `json:"api_key"`
	DefaultModel      string `json:"default_model,omitempty"`
	DefaultImageModel string `json:"default_image_model,omitempty"`
}

Config holds the application configuration that is persisted to disk.

func Load

func Load() (*Config, error)

Load reads the config file and returns the Config struct. Returns an empty Config if the file doesn't exist. Applies default values for any missing model fields.

type Session

type Session struct {
	ID        string           `json:"id"`
	Model     string           `json:"model,omitempty"` // Model used for this session
	CreatedAt time.Time        `json:"created_at"`
	UpdatedAt time.Time        `json:"updated_at"`
	History   []string         `json:"history"`  // User input history for arrow key navigation
	Messages  []SessionMessage `json:"messages"` // Full conversation for resume
}

Session represents a CLI session with its history.

func GetLatestSession

func GetLatestSession() (*Session, error)

GetLatestSession returns the most recently updated session.

func LoadSession

func LoadSession(id string) (*Session, error)

LoadSession loads an existing session by ID.

func NewSession

func NewSession() *Session

NewSession creates a new session with a generated UUID.

func (*Session) AppendHistory

func (s *Session) AppendHistory(entry string) error

AppendHistory adds an entry to the history and saves.

func (*Session) AppendMessage

func (s *Session) AppendMessage(role, content string) error

AppendMessage adds a message to the conversation and saves.

func (*Session) Save

func (s *Session) Save() error

Save writes the session to disk.

type SessionMessage

type SessionMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

SessionMessage represents a message in the conversation.

type SessionSummary

type SessionSummary struct {
	ID           string
	Model        string
	CreatedAt    time.Time
	UpdatedAt    time.Time
	MessageCount int
	Preview      string // First user message, truncated to ~50 chars
}

SessionSummary represents a session for list display.

func ListSessions

func ListSessions() ([]SessionSummary, error)

ListSessions returns summaries of all sessions sorted by UpdatedAt descending.

Jump to

Keyboard shortcuts

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