redactContentBlock 脱敏内容块。
(ctx context.Context, block types.ContentBlock, agentID string)
| 119 | |
| 120 | // redactContentBlock 脱敏内容块。 |
| 121 | func (m *PIIRedactionMiddleware) redactContentBlock(ctx context.Context, block types.ContentBlock, agentID string) (types.ContentBlock, error) { |
| 122 | // 类型断言处理不同的内容块类型 |
| 123 | switch b := block.(type) { |
| 124 | case *types.TextBlock: |
| 125 | if b.Text != "" { |
| 126 | redacted, _, err := m.redactWithTracking(ctx, b.Text, agentID) |
| 127 | if err != nil { |
| 128 | return block, err |
| 129 | } |
| 130 | return &types.TextBlock{Text: redacted}, nil |
| 131 | } |
| 132 | case *types.ToolResultBlock: |
| 133 | // 工具结果也可能包含 PII |
| 134 | if b.Content != "" { |
| 135 | redacted, _, err := m.redactWithTracking(ctx, b.Content, agentID) |
| 136 | if err != nil { |
| 137 | return block, err |
| 138 | } |
| 139 | return &types.ToolResultBlock{ |
| 140 | ToolUseID: b.ToolUseID, |
| 141 | Content: redacted, |
| 142 | IsError: b.IsError, |
| 143 | }, nil |
| 144 | } |
| 145 | } |
| 146 | // 其他类型(如 ToolUseBlock)不需要脱敏 |
| 147 | return block, nil |
| 148 | } |
| 149 | |
| 150 | // redactWithTracking 脱敏文本并追踪 PII 匹配。 |
| 151 | func (m *PIIRedactionMiddleware) redactWithTracking(ctx context.Context, text string, agentID string) (string, []PIIMatch, error) { |
no test coverage detected