Skip to content

Commit aca5970

Browse files
authored
fix: handle masked content in generate response and stream processing (#19)
Working alone on this project so I need to approve myself.
1 parent fbdd231 commit aca5970

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/handlers/generate.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,21 @@ async fn handle_non_streaming_generate(
148148
return build_violation_response(response_body);
149149
}
150150

151-
// Return safe response
151+
// If the response was allowed but PANW provided masked content, use it
152+
if assessment.is_masked {
153+
debug!("Using masked content for generate response");
154+
155+
response_body.response = assessment.final_content;
156+
157+
let json_bytes = serde_json::to_vec(&response_body).map_err(|e| {
158+
error!("Failed to serialize modified response: {}", e);
159+
ApiError::InternalError("Failed to serialize response".to_string())
160+
})?;
161+
162+
return build_json_response(json_bytes.into());
163+
}
164+
165+
// Return original (safe) response
152166
build_json_response(body_bytes)
153167
}
154168

src/stream.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,31 @@ where
662662
return None;
663663
}
664664

665+
// If the assessment indicates masking, replace the pending buffer with the masked content
666+
if assessment.is_masked {
667+
// Clear existing pending chunks
668+
buffer.pending_buffer.clear();
669+
670+
// Build a single masked JSON object and return it as the only chunk.
671+
// Add a trailing newline to make NDJSON consumers happier.
672+
let masked_json = serde_json::json!({
673+
"created_at": chrono::Utc::now().to_rfc3339(),
674+
"done": true,
675+
"message": { "content": assessment.final_content, "role": "assistant" }
676+
});
677+
678+
let mut vec = serde_json::to_vec(&masked_json).unwrap_or_else(|_| assessment.final_content.clone().into_bytes());
679+
vec.push(b'\n');
680+
let bytes = Bytes::from(vec);
681+
682+
// Mark the buffer as blocked/finished so no further inner stream data is processed
683+
buffer.waiting_for_assessment = false;
684+
buffer.accumulating = false;
685+
buffer.blocked = true;
686+
687+
return Some(Ok(bytes));
688+
}
689+
665690
// Mark the content as safe by updating the read position and clearing code buffer
666691
buffer.commit(true);
667692

0 commit comments

Comments
 (0)