From ebc4a207d303223ceab3cd9186962171b3dbe4bc Mon Sep 17 00:00:00 2001 From: TryWorld Date: Tue, 6 Oct 2026 00:23:15 +0800 Subject: [PATCH] word-guard 0.1.1: a Gemini reply is masked too, streamed or whole (#27) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- packages/word-guard/README.md | 2 +- packages/word-guard/cases.json | 13 ++++++++++++- packages/word-guard/package.json | 2 +- packages/word-guard/word-guard.middleware.js | 6 ++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/word-guard/README.md b/packages/word-guard/README.md index b15e18a..44dbdf4 100644 --- a/packages/word-guard/README.md +++ b/packages/word-guard/README.md @@ -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. diff --git a/packages/word-guard/cases.json b/packages/word-guard/cases.json index 543260b..720d025 100644 --- a/packages/word-guard/cases.json +++ b/packages/word-guard/cases.json @@ -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": "***" }] } }] } } ] diff --git a/packages/word-guard/package.json b/packages/word-guard/package.json index 2cc6a8c..7a7ee6a 100644 --- a/packages/word-guard/package.json +++ b/packages/word-guard/package.json @@ -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"], diff --git a/packages/word-guard/word-guard.middleware.js b/packages/word-guard/word-guard.middleware.js index 693e749..c5e81f8 100644 --- a/packages/word-guard/word-guard.middleware.js +++ b/packages/word-guard/word-guard.middleware.js @@ -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 }