|
| 1 | +// Copyright (c) 2026 Lark Technologies Pte. Ltd. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package contentsafety |
| 5 | + |
| 6 | +import "context" |
| 7 | + |
| 8 | +// Provider scans parsed response data for content-safety issues. |
| 9 | +// Implementations must be safe for concurrent use. |
| 10 | +type Provider interface { |
| 11 | + // Name returns a stable provider identifier. Used in Alert payloads |
| 12 | + // and diagnostic output. |
| 13 | + Name() string |
| 14 | + |
| 15 | + // Scan inspects req.Data and returns a non-nil Alert when any issue |
| 16 | + // is detected, or nil when the data is clean. |
| 17 | + // |
| 18 | + // Returning a non-nil error signals that the scan itself failed |
| 19 | + // (misconfiguration, transient I/O, internal panic). Callers are |
| 20 | + // expected to treat scan errors as fail-open. |
| 21 | + // |
| 22 | + // Scan must respect ctx cancellation and return promptly once |
| 23 | + // ctx.Err() becomes non-nil; callers may impose a deadline. |
| 24 | + Scan(ctx context.Context, req ScanRequest) (*Alert, error) |
| 25 | +} |
| 26 | + |
| 27 | +// ScanRequest carries the data to be scanned plus minimal context. |
| 28 | +type ScanRequest struct { |
| 29 | + // CmdPath is the normalized command path (e.g. "im.messages_search"). |
| 30 | + // Providers may use it for per-command logic; most can ignore it. |
| 31 | + CmdPath string |
| 32 | + |
| 33 | + // Data is the parsed response payload as it flows through the CLI's |
| 34 | + // output layer. It may be a map, slice, string, or a typed struct |
| 35 | + // depending on the originating command. Providers must not mutate it. |
| 36 | + // Providers that require a uniform shape should perform their own |
| 37 | + // normalization internally. |
| 38 | + Data any |
| 39 | +} |
| 40 | + |
| 41 | +// Alert describes content-safety issues discovered by a Provider. |
| 42 | +// An Alert only exists when at least one issue was found; nil means clean. |
| 43 | +type Alert struct { |
| 44 | + // Provider identifies which provider produced this alert. |
| 45 | + Provider string `json:"provider"` |
| 46 | + |
| 47 | + // Matches is the list of issues detected. Guaranteed non-empty |
| 48 | + // when the enclosing *Alert is non-nil. |
| 49 | + Matches []RuleMatch `json:"matches"` |
| 50 | +} |
| 51 | + |
| 52 | +// RuleMatch describes a single rule hit. |
| 53 | +type RuleMatch struct { |
| 54 | + // Rule is the stable identifier of the matched rule |
| 55 | + // (e.g. "instruction_override", "role_injection"). |
| 56 | + Rule string `json:"rule"` |
| 57 | +} |
0 commit comments