redactWithTracking 脱敏文本并追踪 PII 匹配。
(ctx context.Context, text string, agentID string)
| 149 | |
| 150 | // redactWithTracking 脱敏文本并追踪 PII 匹配。 |
| 151 | func (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 停止时清除追踪信息。 |
| 183 | func (m *PIIRedactionMiddleware) OnAgentStop(ctx context.Context, agentID string) error { |
no test coverage detected