predict

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package predict provides LLM-based prediction functionality for the gsh REPL. It includes prefix-based prediction, null-state prediction, command explanation, and a router to coordinate between different prediction strategies.

Index

Constants

View Source
const BestPractices = `* Git commit messages should follow conventional commit message format`

BestPractices contains shell command best practices used in prediction prompts.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextFormatter

type ContextFormatter interface {
	// FormatContext formats the context map into a string.
	FormatContext(contextMap map[string]string) string
}

ContextFormatter formats a context map into a string suitable for LLM prompts.

type DefaultContextFormatter

type DefaultContextFormatter struct{}

DefaultContextFormatter is the default implementation of ContextFormatter.

func (*DefaultContextFormatter) FormatContext

func (f *DefaultContextFormatter) FormatContext(contextMap map[string]string) string

FormatContext formats the context map into a string with labeled sections.

type NullStatePredictor

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

NullStatePredictor predicts commands when the input is empty. It uses context (cwd, git status, history, etc.) to suggest a likely next command.

func NewNullStatePredictor

func NewNullStatePredictor(cfg NullStatePredictorConfig) *NullStatePredictor

NewNullStatePredictor creates a new NullStatePredictor with the given configuration.

func (*NullStatePredictor) Predict

func (p *NullStatePredictor) Predict(ctx context.Context, input string) (string, error)

Predict returns a prediction for the next command when input is empty.

func (*NullStatePredictor) UpdateContext

func (p *NullStatePredictor) UpdateContext(contextMap map[string]string)

UpdateContext updates the context information used for predictions.

type NullStatePredictorConfig

type NullStatePredictorConfig struct {
	// ModelResolver resolves to an LLM model for predictions.
	// Can be a direct ModelValue or an SDKModelRef for lazy resolution.
	ModelResolver interpreter.ModelResolver

	// Logger for debug output. If nil, a no-op logger is used.
	Logger *zap.Logger

	// Formatter for context text. If nil, DefaultContextFormatter is used.
	Formatter ContextFormatter
}

NullStatePredictorConfig holds configuration for creating a NullStatePredictor.

type Predictor

type Predictor interface {
	// Predict returns a prediction for the given input.
	// The context can be used for cancellation.
	// Returns the predicted command and any error that occurred.
	Predict(ctx context.Context, input string) (prediction string, err error)

	// UpdateContext updates the context information used for predictions.
	// The context map contains key-value pairs like "cwd", "git", "history", etc.
	UpdateContext(contextMap map[string]string)
}

Predictor defines the interface for making command predictions. Implementations can use different strategies (history, LLM, etc.)

type PrefixPredictor

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

PrefixPredictor predicts command completions based on a partial command prefix. It uses an LLM to generate predictions that start with the given prefix.

func NewPrefixPredictor

func NewPrefixPredictor(cfg PrefixPredictorConfig) *PrefixPredictor

NewPrefixPredictor creates a new PrefixPredictor with the given configuration.

func (*PrefixPredictor) Predict

func (p *PrefixPredictor) Predict(ctx context.Context, input string) (string, error)

Predict returns a prediction for the given input prefix. The prediction will start with the input prefix.

func (*PrefixPredictor) UpdateContext

func (p *PrefixPredictor) UpdateContext(contextMap map[string]string)

UpdateContext updates the context information used for predictions.

type PrefixPredictorConfig

type PrefixPredictorConfig struct {
	// ModelResolver resolves to an LLM model for predictions.
	// Can be a direct ModelValue or an SDKModelRef for lazy resolution.
	ModelResolver interpreter.ModelResolver

	// Logger for debug output. If nil, a no-op logger is used.
	Logger *zap.Logger

	// Formatter for context text. If nil, DefaultContextFormatter is used.
	Formatter ContextFormatter
}

PrefixPredictorConfig holds configuration for creating a PrefixPredictor.

type Router

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

Router coordinates between different prediction strategies. It routes prediction requests to the appropriate predictor based on the input state.

func NewRouter

func NewRouter(cfg RouterConfig) *Router

NewRouter creates a new Router with the given configuration.

func NewRouterFromConfig

func NewRouterFromConfig(modelResolver interpreter.ModelResolver, logger *zap.Logger) *Router

NewRouterFromConfig creates a Router configured from a model resolver. It sets up the prefix and null-state predictors using the provided model resolver. Returns nil if no model resolver is provided.

func (*Router) NullStatePredictor

func (r *Router) NullStatePredictor() Predictor

NullStatePredictor returns the null state predictor (for testing).

func (*Router) Predict

func (r *Router) Predict(ctx context.Context, input string) (string, error)

Predict routes the prediction request to the appropriate predictor. If input is empty, uses null state predictor; otherwise uses prefix predictor.

func (*Router) PrefixPredictor

func (r *Router) PrefixPredictor() Predictor

PrefixPredictor returns the prefix predictor (for testing).

func (*Router) UpdateContext

func (r *Router) UpdateContext(contextMap map[string]string)

UpdateContext updates the context for all predictors.

type RouterConfig

type RouterConfig struct {
	// PrefixPredictor handles predictions when there is input text.
	PrefixPredictor Predictor

	// NullStatePredictor handles predictions when input is empty.
	NullStatePredictor Predictor

	// Logger for debug output. If nil, a no-op logger is used.
	Logger *zap.Logger
}

RouterConfig holds configuration for creating a Router.

Jump to

Keyboard shortcuts

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