shared

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package shared provides common types, variables, and utilities for all command packages.

Index

Constants

This section is empty.

Variables

View Source
var (
	CfgFile              string
	Verbose              int
	DryRun               bool
	UltraCompact         bool
	SkipEnv              bool
	QueryIntent          string
	LLMEnabled           bool
	TokenBudget          int
	FallbackArgs         []string
	LayerPreset          string
	OutputFile           string
	QuietMode            bool
	JSONOutput           bool
	RemoteMode           bool
	CompressionAddr      string
	AnalyticsAddr        string
	RemoteTimeout        int
	CompactionEnabled    bool
	CompactionThreshold  int
	CompactionPreserve   int
	CompactionMaxTokens  int
	CompactionSnapshot   bool
	CompactionAutoDetect bool
	ReversibleEnabled    bool
	EnableLayers         []string
	DisableLayers        []string
	StreamMode           bool
)
View Source
var Version string = "dev"

Version is set at build time.

Functions

func ExecuteAndRecord

func ExecuteAndRecord(name string, fn func() (string, string, error)) error

ExecuteAndRecord runs a command function, prints output, and records metrics. This consolidates the common pattern of: time -> execute -> print -> record. Returns an error instead of calling os.Exit so callers control exit behavior.

func GetAnalyticsAddr

func GetAnalyticsAddr() string

GetAnalyticsAddr returns the analytics service address.

func GetBuffer

func GetBuffer() *bytes.Buffer

func GetCachedConfig

func GetCachedConfig() (*config.Config, error)

GetCachedConfig returns the cached config, loading it on first access.

func GetCompressionAddr

func GetCompressionAddr() string

GetCompressionAddr returns the compression service address.

func GetConfig

func GetConfig() (*config.Config, error)

GetConfig returns the cached configuration.

func GetDisableLayers

func GetDisableLayers() []string

GetDisableLayers returns layers to explicitly disable.

func GetEnableLayers

func GetEnableLayers() []string

GetEnableLayers returns layers to explicitly enable.

func GetLayerPreset

func GetLayerPreset() string

GetLayerPreset returns the layer preset from flag or environment.

func GetLayerProfile

func GetLayerProfile() string

GetLayerProfile returns the compression profile from flag or environment.

func GetModelFamily

func GetModelFamily(modelName string) string

GetModelFamily extracts the model family from a model name. Delegates to utils.GetModelFamily for single source of truth.

func GetQueryIntent

func GetQueryIntent() string

GetQueryIntent returns the query intent from flag or environment.

func GetRemoteClient

func GetRemoteClient() *client.Client

GetRemoteClient returns the global gRPC client (lazy initialization). Returns nil if remote mode is not enabled or connection failed.

func GetRemoteTimeout

func GetRemoteTimeout() int

GetRemoteTimeout returns the remote operation timeout in seconds.

func GetTokenBudget

func GetTokenBudget() int

GetTokenBudget returns the token budget from flag or environment.

func IsLLMEnabled

func IsLLMEnabled() bool

IsLLMEnabled returns true if LLM compression is enabled.

func IsQuietMode

func IsQuietMode() bool

IsQuietMode returns true if quiet mode is enabled.

func IsRemoteMode

func IsRemoteMode() bool

IsRemoteMode returns true if remote mode is enabled.

func IsReversibleEnabled

func IsReversibleEnabled() bool

IsReversibleEnabled returns true if reversible mode is enabled.

func IsStreamMode

func IsStreamMode() bool

IsStreamMode returns true if streaming mode is enabled for large inputs.

func IsUltraCompact

func IsUltraCompact() bool

IsUltraCompact returns true if ultra-compact mode is enabled.

func IsVerbose

func IsVerbose() bool

IsVerbose returns true if verbose mode is enabled.

func PrintTokenSavings

func PrintTokenSavings(originalTokens, filteredTokens int)

PrintTokenSavings prints token savings info to stderr when in verbose mode.

func PutBuffer

func PutBuffer(buf *bytes.Buffer)

func RecordCommand

func RecordCommand(command, originalOutput, filteredOutput string, execTimeMs int64, success bool) error

RecordCommand records command execution metrics to the tracking database.

func RemoteCompress

func RemoteCompress(input, mode string, budget int) (output string, tokensSaved int, err error)

RemoteCompress performs compression via gRPC service. Returns the compressed output and tokens saved, or an error.

func RemoteRecordAnalytics

func RemoteRecordAnalytics(command string, originalTokens, filteredTokens int, execTimeMs int64, success bool) error

RemoteRecordAnalytics sends command metrics to the analytics service.

func RootCmd

func RootCmd() any

RootCmd returns the stored root command.

func RunAndCapture

func RunAndCapture(cmd string, args []string) (output string, exitCode int, err error)

RunAndCapture executes a command and captures stdout/stderr. This consolidates the common pattern of: exec.Command -> StdoutPipe -> StderrPipe -> Start -> Wait. Returns combined stdout and stderr, exit code, and any execution error.

func SanitizeArgs

func SanitizeArgs(args []string) error

SanitizeArgs validates and sanitizes command arguments. Returns error if arguments contain dangerous patterns.

func SetConfig

func SetConfig(cfg FlagConfig)

Backward compatibility: SetConfig alias for SetFlags Deprecated: Use SetFlags instead

func SetConfigFile

func SetConfigFile(path string)

SetConfigFile sets the config file path.

func SetFlags

func SetFlags(cfg FlagConfig)

SetFlags sets all flag values atomically on the global state.

func SetRootCmd

func SetRootCmd(cmd any)

SetRootCmd stores the root command reference.

func ShortenPath

func ShortenPath(path string) string

ShortenPath shortens a file path for display.

func TeeOnFailure

func TeeOnFailure(raw string, commandSlug string, err error) string

TeeOnFailure writes output to tee file on error.

func Truncate

func Truncate(s string, maxLen int) string

Truncate truncates a string to maxLen characters.

func TruncateLine

func TruncateLine(line string, maxLen int) string

TruncateLine truncates a line to maxLen characters.

func TryJSONSchema

func TryJSONSchema(jsonStr string, maxDepth int) string

TryJSONSchema generates a JSON schema from a JSON string.

Types

type AppState

type AppState struct {

	// Global flags set by CLI
	CfgFile      string
	Verbose      int
	DryRun       bool
	UltraCompact bool
	SkipEnv      bool
	QueryIntent  string
	LLMEnabled   bool
	TokenBudget  int
	FallbackArgs []string
	LayerPreset  string
	LayerProfile string
	OutputFile   string
	QuietMode    bool
	JSONOutput   bool

	// Remote mode flags (Phase 4)
	RemoteMode      bool
	CompressionAddr string
	AnalyticsAddr   string
	RemoteTimeout   int // seconds

	// Compaction flags
	CompactionEnabled    bool
	CompactionThreshold  int
	CompactionPreserve   int
	CompactionMaxTokens  int
	CompactionSnapshot   bool
	CompactionAutoDetect bool

	// Reversible mode
	ReversibleEnabled bool

	// Custom layer configuration
	EnableLayers  []string
	DisableLayers []string
	StreamMode    bool
	// contains filtered or unexported fields
}

AppState encapsulates all CLI flag state in a single struct. This replaces the global variable pattern and enables: - Testability (pass different state to different tests) - Concurrency (multiple commands with different configs) - Dependency injection (pass state explicitly)

func Global

func Global() *AppState

Global returns the global AppState instance. Deprecated: Pass AppState explicitly where possible.

func (*AppState) GetAnalyticsAddr

func (s *AppState) GetAnalyticsAddr() string

GetAnalyticsAddr returns the analytics service address.

func (*AppState) GetCompressionAddr

func (s *AppState) GetCompressionAddr() string

GetCompressionAddr returns the compression service address.

func (*AppState) GetDisableLayers

func (s *AppState) GetDisableLayers() []string

GetDisableLayers returns layers to explicitly disable.

func (*AppState) GetEnableLayers

func (s *AppState) GetEnableLayers() []string

GetEnableLayers returns layers to explicitly enable.

func (*AppState) GetLayerPreset

func (s *AppState) GetLayerPreset() string

GetLayerPreset returns the layer preset from flag or environment.

func (*AppState) GetQueryIntent

func (s *AppState) GetQueryIntent() string

GetQueryIntent returns the query intent from flag or environment.

func (*AppState) GetRemoteTimeout

func (s *AppState) GetRemoteTimeout() int

GetRemoteTimeout returns the remote operation timeout in seconds.

func (*AppState) GetTokenBudget

func (s *AppState) GetTokenBudget() int

GetTokenBudget returns the token budget from flag or environment.

func (*AppState) IsLLMEnabled

func (s *AppState) IsLLMEnabled() bool

IsLLMEnabled returns true if LLM compression is enabled.

func (*AppState) IsQuietMode

func (s *AppState) IsQuietMode() bool

IsQuietMode returns true if quiet mode is enabled.

func (*AppState) IsRemoteMode

func (s *AppState) IsRemoteMode() bool

IsRemoteMode returns true if remote mode is enabled.

func (*AppState) IsReversibleEnabled

func (s *AppState) IsReversibleEnabled() bool

IsReversibleEnabled returns true if reversible mode is enabled.

func (*AppState) IsStreamMode

func (s *AppState) IsStreamMode() bool

IsStreamMode returns true if streaming mode is enabled for large inputs.

func (*AppState) IsUltraCompact

func (s *AppState) IsUltraCompact() bool

IsUltraCompact returns true if ultra-compact mode is enabled.

func (*AppState) IsVerbose

func (s *AppState) IsVerbose() bool

IsVerbose returns true if verbose mode is enabled.

func (*AppState) Set

func (s *AppState) Set(cfg FlagConfig)

Set sets all flag values atomically.

type FallbackHandler

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

FallbackHandler handles commands via TOML filter system.

func GetFallback

func GetFallback() *FallbackHandler

GetFallback returns the global fallback handler (singleton).

func NewFallbackHandler

func NewFallbackHandler() *FallbackHandler

NewFallbackHandler creates a new fallback handler.

func (*FallbackHandler) Handle

func (h *FallbackHandler) Handle(args []string) (string, bool, error)

Handle processes a command through the TOML filter system.

type FlagConfig

type FlagConfig struct {
	Verbose              int
	DryRun               bool
	UltraCompact         bool
	SkipEnv              bool
	QueryIntent          string
	LLMEnabled           bool
	TokenBudget          int
	FallbackArgs         []string
	LayerPreset          string
	LayerProfile         string
	OutputFile           string
	QuietMode            bool
	JSONOutput           bool
	RemoteMode           bool
	CompressionAddr      string
	AnalyticsAddr        string
	RemoteTimeout        int
	CompactionEnabled    bool
	CompactionThreshold  int
	CompactionPreserve   int
	CompactionMaxTokens  int
	CompactionSnapshot   bool
	CompactionAutoDetect bool
	ReversibleEnabled    bool
	EnableLayers         []string
	DisableLayers        []string
	StreamMode           bool
}

FlagConfig holds all flag values for atomic setting.

type OutputType

type OutputType int

OutputType represents the type of command output.

const (
	OutputTypeTest OutputType = iota
	OutputTypeBuild
	OutputTypeLog
	OutputTypeList
	OutputTypeJSON
	OutputTypeGeneric
)

Jump to

Keyboard shortcuts

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