MCPcopy Create free account
hub / github.com/astercloud/aster / redactWithTracking

Method redactWithTracking

pkg/security/pii_middleware.go:151–180  ·  view source on GitHub ↗

redactWithTracking 脱敏文本并追踪 PII 匹配。

(ctx context.Context, text string, agentID string)

Source from the content-addressed store, hash-verified

149
150// redactWithTracking 脱敏文本并追踪 PII 匹配。
151func (m *PIIRedactionMiddleware) redactWithTracking(ctx context.Context, text string, agentID string) (string, []PIIMatch, error) {
152 if text == "" {
153 return text, nil, nil
154 }
155
156 // 检测和脱敏
157 matches, err := m.redactor.detector.Detect(ctx, text)
158 if err != nil {
159 return text, nil, err
160 }
161
162 if len(matches) == 0 {
163 return text, nil, nil
164 }
165
166 // 如果启用追踪,保存匹配信息
167 if m.enableTracking {
168 m.mu.Lock()
169 m.tracking[agentID] = append(m.tracking[agentID], matches...)
170 m.mu.Unlock()
171 }
172
173 // 脱敏
174 redacted, err := m.redactor.Redact(ctx, text)
175 if err != nil {
176 return text, nil, err
177 }
178
179 return redacted, matches, nil
180}
181
182// OnAgentStop 在 Agent 停止时清除追踪信息。
183func (m *PIIRedactionMiddleware) OnAgentStop(ctx context.Context, agentID string) error {

Callers 3

WrapModelCallMethod · 0.95
redactMessageMethod · 0.95
redactContentBlockMethod · 0.95

Calls 2

DetectMethod · 0.65
RedactMethod · 0.65

Tested by

no test coverage detected