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 }