Documentation
¶
Overview ¶
Package safeguard provides fail-closed input and output screening as Core chat middleware.
Matcher is the extension boundary for substring lists, classifiers, or remote moderation services. Middleware has no global registry or default policy: callers construct and compose an explicit instance.
Example ¶
package main
import (
"context"
"fmt"
"github.com/Tangerg/scope/core/chatclient/safeguard"
)
func main() {
matcher, err := safeguard.NewSubstringMatcher([]string{"secret"}, safeguard.SubstringConfig{})
if err != nil {
panic(err)
}
match, err := matcher.Match(context.Background(), "do not reveal the SECRET")
if err != nil {
panic(err)
}
fmt.Println(match.Found, match.Term)
}
Output: true secret
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsafeContent is the stable policy rejection sentinel unwrapped by // UnsafeError. ErrUnsafeContent = errors.New("safeguard: unsafe content") // ErrInvalidMiddlewareConfig identifies an incomplete screening boundary. ErrInvalidMiddlewareConfig = errors.New("safeguard: invalid middleware config") // ErrInvalidSubstringConfig identifies an empty term policy. ErrInvalidSubstringConfig = errors.New("safeguard: invalid substring matcher config") // ErrNilStream identifies a wrapped streamer that returned no iterator. ErrNilStream = errors.New("safeguard: nil stream sequence") )
Functions ¶
This section is empty.
Types ¶
type Match ¶
Match is a Matcher's decision for one text projection. Term should be empty when policy details must not be disclosed.
type Matcher ¶
type Matcher interface {
// Match evaluates one provider-neutral text projection. Found=false means the
// text passed; a returned error means no policy decision was reached. Remote
// implementations must honor ctx and preserve context errors.
Match(ctx context.Context, text string) (Match, error)
}
Matcher screens a text projection. Implementations may call remote policy services and must preserve context cancellation errors.
type MatcherFunc ¶
MatcherFunc adapts a screening function to Matcher.
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware screens model inputs and outputs at the model boundary.
func NewMiddleware ¶
func NewMiddleware(matcher Matcher, config MiddlewareConfig) (*Middleware, error)
NewMiddleware freezes screening direction and callback policy around one explicit Matcher.
func (*Middleware) Call ¶
func (m *Middleware) Call(next chat.Model) chat.Model
Call is a chat.CallMiddleware. Input is screened before the model runs; output is screened before a response becomes visible to the caller.
func (*Middleware) Stream ¶
func (m *Middleware) Stream(next chat.Streamer) chat.Streamer
Stream is a chat.StreamMiddleware. Output chunks are accumulated before screening so a term split across provider chunks is still detected. The chunk that completes an unsafe match is not yielded.
type MiddlewareConfig ¶
MiddlewareConfig controls one immutable Middleware. A zero Scope defaults to ScopeBoth. OnBlock runs synchronously before a rejection is returned.
type SubstringConfig ¶
SubstringConfig controls matching and disclosure. Case-insensitive matching is the default. HideMatch prevents a configured term from entering UnsafeError or OnBlock.
type SubstringMatcher ¶
type SubstringMatcher struct {
// contains filtered or unexported fields
}
SubstringMatcher is an immutable matcher for small policy term sets. It trims and de-duplicates configuration once, preserves declaration order for the first-match decision, and can withhold the matched term from downstream errors and callbacks without weakening the block decision.
func NewSubstringMatcher ¶
func NewSubstringMatcher(terms []string, config SubstringConfig) (*SubstringMatcher, error)
NewSubstringMatcher normalizes and deduplicates a small term policy once; matching never depends on later caller mutation.
type UnsafeError ¶
type UnsafeError struct {
Block Block
}
UnsafeError is a policy rejection. It unwraps to ErrUnsafeContent.
func (*UnsafeError) Error ¶
func (u *UnsafeError) Error() string
func (*UnsafeError) Unwrap ¶
func (u *UnsafeError) Unwrap() error