injector

package
v0.5.21 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package injector provides autonomous stdin injection capabilities for the Superbrain system.

Package injector provides autonomous stdin injection capabilities for the Superbrain system. It can automatically respond to interactive CLI prompts to prevent processes from hanging while waiting for user input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultForbiddenPatterns

func DefaultForbiddenPatterns() []*regexp.Regexp

DefaultForbiddenPatterns returns patterns that should never trigger automatic responses. These patterns indicate potentially dangerous operations that require human approval.

func IsForbidden

func IsForbidden(logContent string, forbiddenPatterns []*regexp.Regexp) bool

IsForbidden checks if the log content contains any forbidden patterns. Returns true if a forbidden pattern is detected.

Types

type Config

type Config struct {
	// Mode controls stdin injection behavior.
	// Valid values: "disabled", "conservative", "autopilot"
	Mode string

	// CustomPatterns defines additional prompt patterns to recognize.
	CustomPatterns []config.StdinPattern

	// ForbiddenPatterns lists regex patterns that should never trigger automatic responses.
	ForbiddenPatterns []string

	// AuditLogger is the logger for recording injection attempts.
	// If nil, the global audit logger will be used.
	AuditLogger *audit.Logger
}

Config holds configuration for the StdinInjector.

type StdinInjector

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

StdinInjector provides autonomous stdin injection for interactive CLI prompts. It can automatically respond to permission prompts and other interactive questions to prevent processes from hanging while waiting for user input.

func NewStdinInjector

func NewStdinInjector(cfg Config) (*StdinInjector, error)

NewStdinInjector creates a new stdin injector with the specified configuration.

func (*StdinInjector) CanInject

func (s *StdinInjector) CanInject(pattern *StdinPattern) bool

CanInject checks if injection is allowed for the given pattern and current mode. Returns true if injection should proceed, false otherwise.

func (*StdinInjector) GetMode

func (s *StdinInjector) GetMode() string

GetMode returns the current injection mode.

func (*StdinInjector) GetPatterns

func (s *StdinInjector) GetPatterns() []*StdinPattern

GetPatterns returns all configured stdin patterns (default + custom).

func (*StdinInjector) Inject

func (s *StdinInjector) Inject(stdin io.Writer, response string) error

Inject writes the response to the process stdin. Returns an error if the write fails.

func (*StdinInjector) IsForbidden

func (s *StdinInjector) IsForbidden(logContent string) bool

IsForbidden checks if the log content contains any forbidden patterns. Returns true if a forbidden pattern is detected.

func (*StdinInjector) MatchPattern

func (s *StdinInjector) MatchPattern(logContent string) *StdinPattern

MatchPattern searches for a matching stdin pattern in the given log content. It returns the first matching pattern, or nil if no patterns match. Forbidden patterns are checked first and will prevent any match.

func (*StdinInjector) SetMode

func (s *StdinInjector) SetMode(mode string) error

SetMode updates the injection mode at runtime. Valid values: "disabled", "conservative", "autopilot"

func (*StdinInjector) TryInject

func (s *StdinInjector) TryInject(logContent string, stdin io.Writer) (*StdinPattern, bool, error)

TryInject attempts to match a pattern in the log content and inject the response if allowed. Returns the matched pattern, whether injection was performed, and any error. All injection attempts are logged to the audit log.

func (*StdinInjector) TryInjectWithContext

func (s *StdinInjector) TryInjectWithContext(logContent string, stdin io.Writer, requestID, provider, model string) (*StdinPattern, bool, error)

TryInjectWithContext attempts injection with additional context for audit logging. requestID, provider, and model are used for audit trail purposes.

type StdinPattern

type StdinPattern struct {
	// Name is a unique identifier for this pattern.
	Name string

	// Regex is the compiled regular expression pattern to match in process output.
	Regex *regexp.Regexp

	// Response is the text to inject into stdin when the pattern is matched.
	Response string

	// IsSafe indicates whether this pattern is safe for automatic injection in autopilot mode.
	// Safe patterns are those that don't perform destructive operations.
	IsSafe bool

	// Description provides human-readable context about what this pattern matches.
	Description string
}

StdinPattern defines a recognizable prompt pattern and its automatic response. Patterns are matched against process output to detect when stdin injection is needed.

func DefaultStdinPatterns

func DefaultStdinPatterns() []*StdinPattern

DefaultStdinPatterns returns the built-in patterns for common CLI prompts. These patterns cover the most common interactive prompts from Claude, Gemini, and other tools.

func MatchPattern

func MatchPattern(logContent string, patterns []*StdinPattern, forbiddenPatterns []*regexp.Regexp) *StdinPattern

MatchPattern searches for a matching stdin pattern in the given log content. It returns the first matching pattern, or nil if no patterns match. Forbidden patterns are checked first and will prevent any match.

Jump to

Keyboard shortcuts

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