word-guard 0.1.1: a Gemini reply is masked too, streamed or whole (#27)

onEvent masked an Anthropic delta, a Responses delta and a Chat chunk, but had
no branch for Gemini's own shape (candidates[].content.parts[].text) — while
onResponse right below it did. So with replies: true, a word the user asked to
keep out of replies was masked in every API but a streamed Gemini one, where it
went to the client untouched.

A case covers it in both directions: a streamed Gemini reply in its own place
(and a part that isn't text left alone), and a whole one. It fails on 0.1.0 —
"acme Inc" reaches the client — and passes here.

The reply is what the option promises, so a filter that silently skips one of
the four APIs magpie serves is a hole rather than a missing feature.

Co-authored-by: TryWorld2026 <tryworld@users.noreply.github.com>
This commit is contained in:
TryWorld
2026-10-06 00:23:15 +08:00
committed by GitHub
co-authored by TryWorld2026
parent 8c87a843b5
commit ebc4a207d3
4 changed files with 20 additions and 3 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @m
- **`patterns`.** Regular expressions, also matched ignoring case.
- **`action`.** `reject` (the default) turns the request away with a 400 that names the word, in the agent's API's error shape. `mask` replaces each match with `mask` (default `***`) and sends the request on.
- **`check`.** `last` (the default) reads only the last user message that has text, which is the turn being sent. That is what New API reads. `all` reads every user message, so a word earlier in the conversation keeps turning requests away.
- **`replies`.** Set it to `true` to mask matches in the text that comes back as well. A word split across two streamed events isn't caught.
- **`replies`.** Set it to `true` to mask matches in the text that comes back as well, in every API magpie serves — Anthropic, Chat, Responses and Gemini, streamed or whole. A word split across two streamed events isn't caught.
- **`message`.** The error text to use in place of the default one.
The middleware reads only the user's own text. It doesn't read tool results, tool calls or the system prompt, so a file an agent reads that contains the word doesn't stop it.
+12 -1
View File
@@ -56,5 +56,16 @@
"expect_response": { "content": [{ "type": "text", "text": "***" }, { "type": "tool_use", "input": { "q": "acme" } }] } },
{ "name": "replies untouched unless asked",
"options": { "words": ["acme"], "action": "mask" },
"response": { "content": [{ "type": "text", "text": "acme" }] } }
"response": { "content": [{ "type": "text", "text": "acme" }] } },
{ "name": "a Gemini reply is masked in its own place, streamed and whole",
"ctx": { "protocol": "gemini" },
"options": { "words": ["acme"], "action": "mask", "replies": true },
"events": [
{ "data": { "candidates": [{ "content": { "role": "model", "parts": [{ "text": "acme Inc" }] } }] } },
{ "data": { "candidates": [{ "content": { "role": "model", "parts": [{ "text": "and acme" }, { "functionCall": { "name": "acme", "args": {} } }] }, "finishReason": "STOP" }] } }],
"expect_events": [
{ "candidates": [{ "content": { "role": "model", "parts": [{ "text": "*** Inc" }] } }] },
{ "candidates": [{ "content": { "role": "model", "parts": [{ "text": "and ***" }, { "functionCall": { "name": "acme", "args": {} } }] }, "finishReason": "STOP" }] }],
"response": { "candidates": [{ "content": { "role": "model", "parts": [{ "text": "acme", "thought": true }, { "text": "acme" }] } }] },
"expect_response": { "candidates": [{ "content": { "role": "model", "parts": [{ "text": "***", "thought": true }, { "text": "***" }] } }] } }
]
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/middleware-word-guard",
"version": "0.1.0",
"version": "0.1.1",
"description": "New API's sensitive-word filter (敏感词过滤) as magpie gateway middleware: turn away or mask words and patterns in what agents send, and in replies",
"type": "module",
"files": ["word-guard.middleware.js", "README.md"],
@@ -67,6 +67,12 @@ export function onEvent(ev, ctx) {
else if (ev.type === "response.output_text.delta") set(ev, "delta")
else if (ev.type === "response.output_text.done") set(ev, "text")
else if (Array.isArray(ev.choices)) for (const c of ev.choices) set(c.delta, "content")
// Gemini streams its text in candidates[].content.parts[].text, the shape
// onResponse below already masks
else if (Array.isArray(ev.candidates))
for (const c of ev.candidates) {
if (c && c.content && Array.isArray(c.content.parts)) for (const p of c.content.parts) set(p, "text")
}
return changed ? ev : undefined
}