enhancer

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PromptStyleAgent = "agent"
	PromptStyleHuman = "human"
)

Prompt style names selectable via OPENPE_PROMPT_STYLE. They answer "who is the enhanced prompt written for":

  • "agent" (default): the compiled-in defaultSystemPrompt above (v7i) — the smallest faithful expansion, addressed to the downstream coding agent, which unlike this enhancer can actually read the repository and is therefore the right place for technical decisions.
  • "human": the former default (v7h) kept VERBATIM below — a detailed report-style expansion (goal/steps/verification scaffolding) that some users prefer to read in the review preview as a worked example of systematically decomposing a vague request.

The gold-standard eval (2026-07-14/15, eval/out/gold-*) found the "agent" style decisively better as agent input (86.0% under the frozen opus-4-8 judge, consensus 81.4%, every category positive); "human" is preserved because its detailed register has independent value FOR HUMAN READERS, which that eval did not measure. Selection precedence: explicit OPENPE_SYSTEM_PROMPT[_FILE] > OPENPE_PROMPT_STYLE > built-in default.

Variables

This section is empty.

Functions

func ResolveSystemPrompt

func ResolveSystemPrompt(explicit, style string) (string, error)

ResolveSystemPrompt applies the system-prompt precedence and returns the override to hand to WithSystemPrompt: the explicit prompt when set (wins over everything), the human preset when that style is selected, or "" (meaning "keep the compiled-in default") for the default/agent style. An unknown non-empty style is a loud configuration error — never a silent fallback — so a typo in OPENPE_PROMPT_STYLE cannot masquerade as a style.

Types

type CompletionRequest

type CompletionRequest struct {
	System   string
	User     string
	Messages []Message
}

CompletionRequest is the provider-facing prompt. System is always the first message. When Messages is non-empty it is sent verbatim after System (hybrid multi-turn); otherwise the single User string is sent as one user message (flatten). Keeping both fields preserves backward compatibility for callers and tests that only populate User.

type CompletionResponse

type CompletionResponse struct {
	Text     string
	Provider string
	Model    string
}

type ContentWarningsConfig

type ContentWarningsConfig struct {
	Enabled bool
	// ExtraActions extends the irreversible-action word list (each entry is
	// its own synonym group; matched like the built-ins).
	ExtraActions []string
	// NumMaxLen is the maximum digit-run length checked by the number rule;
	// longer runs are ids/hashes/timestamps, not quantities. Default 5.
	NumMaxLen int
}

ContentWarningsConfig controls the output-side warning checks. The zero value disables them; config.Load wires the OPENPE_WARNINGS_* env vars.

type Context

type Context struct {
	Files     []ContextFile `json:"files,omitempty"`
	Retrieval []string      `json:"retrieval,omitempty"`
}

type ContextFile

type ContextFile struct {
	Path    string `json:"path,omitempty"`
	Content string `json:"content,omitempty"`
}

type ContextProvider

type ContextProvider interface {
	Retrieve(ctx context.Context, req Request) ([]string, error)
}

type LanguageGuardConfig

type LanguageGuardConfig struct {
	// Enabled turns the guard on.
	Enabled bool
	// Reanchor selects strategy 1 (re-request once with an explicit language
	// directive) on a detected mismatch. When false, the guard uses strategy 2
	// only (detect + warn, no extra model call), which adds zero latency.
	Reanchor bool
}

LanguageGuardConfig configures the post-processing language-preservation guard. The zero value (Enabled=false) is a no-op, so a Service built without the guard behaves exactly as before it existed.

type LanguageGuardEvent

type LanguageGuardEvent struct {
	InputLang  string // script class of the user's ORIGINAL prompt (the source of truth)
	OutputLang string // script class of the first model output
	Mismatch   bool   // input and output classes differed
	Retried    bool   // a re-anchored retry was issued
	Corrected  bool   // the retry produced the input's language class
	FinalLang  string // script class of the returned text
}

LanguageGuardEvent is emitted once per enhancement when the guard is enabled and both the input and output languages were classifiable. It is the observability hook: wire WithLanguageGuardObserver to feed Prometheus counters, and/or a *slog.Logger for a structured record.

type Message

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

type MessageStyle

type MessageStyle int

MessageStyle selects how an enhancement request is laid out on the wire.

const (
	// StyleFlatten (default) sends [system, user] where the single user message
	// embeds the conversation history as labeled "[role] content" text. This is
	// the historical, eval-validated layout.
	StyleFlatten MessageStyle = iota
	// StyleHybrid sends [system, prior user/assistant turns..., final user] where
	// prior conversation is delivered as real chat turns and only the final user
	// turn carries the rewrite instruction + original prompt. Opt-in until eval
	// A/B promotes it (OPENPE_MESSAGE_STYLE=hybrid).
	StyleHybrid
	// StyleStructured sends [system(+zone framing), (optional) read-only
	// reference-material message, prior user/assistant turns..., final user
	// task]. Unlike StyleHybrid it pulls the reference context (rules,
	// guidelines, context.files, context.retrieval) OUT of the final task
	// message into a dedicated, clearly-delimited read-only block placed before
	// the conversation, so the task turn carries only the rewrite instruction +
	// original prompt + this-turn metadata. Opt-in (OPENPE_MESSAGE_STYLE=
	// structured) until eval A/B promotes it. See docs/development/
	// 2026-07-01-message-construction-part2.md.
	StyleStructured
)

type Metadata

type Metadata struct {
	UsedContext []string      `json:"used_context,omitempty"`
	Sections    []SectionInfo `json:"sections,omitempty"`
	Provider    string        `json:"provider,omitempty"`
	Model       string        `json:"model,omitempty"`
}

type Options

type Options struct {
	MaxContextTokens int  `json:"max_context_tokens,omitempty"`
	ReturnMetadata   bool `json:"return_metadata,omitempty"`
}

type Provider

type Provider interface {
	Complete(ctx context.Context, req CompletionRequest) (CompletionResponse, error)
}

type Request

type Request struct {
	Prompt     string    `json:"prompt"`
	Client     string    `json:"client,omitempty"`
	CWD        string    `json:"cwd,omitempty"`
	Mode       string    `json:"mode,omitempty"`
	History    []Message `json:"history,omitempty"`
	Rules      []string  `json:"rules,omitempty"`
	Guidelines []string  `json:"guidelines,omitempty"`
	Context    Context   `json:"context,omitempty"`
	Options    Options   `json:"options,omitempty"`
}

type Response

type Response struct {
	EnhancedPrompt string   `json:"enhanced_prompt"`
	Warnings       []string `json:"warnings,omitempty"`
	Metadata       Metadata `json:"metadata,omitempty"`
}

type SectionInfo

type SectionInfo struct {
	Name      string `json:"name"`
	Length    int    `json:"length"`
	Truncated bool   `json:"truncated"`
}

type Service

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

func NewService

func NewService(provider Provider) *Service

func NewServiceWithContext

func NewServiceWithContext(provider Provider, contextProvider ContextProvider) *Service

func (*Service) Enhance

func (s *Service) Enhance(ctx context.Context, req Request) (Response, error)

func (*Service) WithContentWarnings

func (s *Service) WithContentWarnings(cfg ContentWarningsConfig, language string) *Service

WithContentWarnings enables the deterministic output-side warning checks (content_warnings.go), localized to language. Returns the Service for chaining; the zero config keeps them off.

func (*Service) WithLanguageGuard

func (s *Service) WithLanguageGuard(cfg LanguageGuardConfig) *Service

WithLanguageGuard enables the post-processing language-preservation guard. A disabled config leaves the guard off (identical to not calling this), so it is safe to wire unconditionally from config. Returns the Service for chaining.

func (*Service) WithLanguageGuardObserver

func (s *Service) WithLanguageGuardObserver(fn func(LanguageGuardEvent)) *Service

WithLanguageGuardObserver registers a metrics hook invoked once per guarded enhancement with the LanguageGuardEvent. nil disables it. Returns the Service.

func (*Service) WithLogger

func (s *Service) WithLogger(logger *slog.Logger) *Service

WithLogger sets the structured logger used for language-guard observability. nil (the default) disables logging. Returns the Service for chaining.

func (*Service) WithMessageStyle

func (s *Service) WithMessageStyle(style MessageStyle) *Service

WithMessageStyle selects the provider message layout (StyleFlatten default, StyleHybrid opt-in). Returns the Service for chaining.

func (*Service) WithSystemPrompt

func (s *Service) WithSystemPrompt(prompt string) *Service

WithSystemPrompt overrides the system prompt used for enhancement. An empty or whitespace-only value is ignored so callers may pass an optional config value unconditionally and keep the built-in default. Returns the Service for chaining.

type ValidationError

type ValidationError struct {
	Message string
}

func (ValidationError) Error

func (e ValidationError) Error() string

Jump to

Keyboard shortcuts

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