fix(minimax): strip tool invocation XML from assistant text
This commit is contained in:
committed by
Peter Steinberger
parent
1f3ae2346e
commit
350f956f2c
@@ -23,8 +23,9 @@
|
|||||||
- Sandbox: support tool-policy groups in `tools.sandbox.tools` (e.g. `group:memory`, `group:fs`) to reduce config churn.
|
- Sandbox: support tool-policy groups in `tools.sandbox.tools` (e.g. `group:memory`, `group:fs`) to reduce config churn.
|
||||||
|
|
||||||
### Fixes
|
### Fixes
|
||||||
|
- Models/MiniMax: strip malformed tool invocation XML (`<invoke>...</invoke>` and `</minimax:tool_call>`) from assistant text to prevent tool call leaks into user messages.
|
||||||
- Tools/Models: MiniMax vision now uses the Coding Plan VLM endpoint (`/v1/coding_plan/vlm`) so the `image` tool works with MiniMax keys (also accepts `@/path/to/file.png`-style inputs).
|
- Tools/Models: MiniMax vision now uses the Coding Plan VLM endpoint (`/v1/coding_plan/vlm`) so the `image` tool works with MiniMax keys (also accepts `@/path/to/file.png`-style inputs).
|
||||||
- Gateway/macOS: reduce noisy loopback WS “closed before connect” logs during tests.
|
- Gateway/macOS: reduce noisy loopback WS "closed before connect" logs during tests.
|
||||||
- Auto-reply: resolve ambiguous `/model` fuzzy matches by picking the best candidate instead of erroring.
|
- Auto-reply: resolve ambiguous `/model` fuzzy matches by picking the best candidate instead of erroring.
|
||||||
|
|
||||||
## 2026.1.12-1
|
## 2026.1.12-1
|
||||||
|
|||||||
162
src/agents/pi-embedded-utils.test.ts
Normal file
162
src/agents/pi-embedded-utils.test.ts
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { extractAssistantText } from "./pi-embedded-utils.js";
|
||||||
|
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||||
|
|
||||||
|
describe("extractAssistantText", () => {
|
||||||
|
it("strips Minimax tool invocation XML from text", () => {
|
||||||
|
const msg: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `<invoke name="Bash">
|
||||||
|
<parameter name="command">netstat -tlnp | grep 18789</parameter>
|
||||||
|
</invoke>
|
||||||
|
</minimax:tool_call>`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = extractAssistantText(msg);
|
||||||
|
expect(result).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("strips multiple tool invocations", () => {
|
||||||
|
const msg: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `Let me check that.<invoke name="Read">
|
||||||
|
<parameter name="path">/home/admin/test.txt</parameter>
|
||||||
|
</invoke>
|
||||||
|
</minimax:tool_call>`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = extractAssistantText(msg);
|
||||||
|
expect(result).toBe("Let me check that.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves normal text without tool invocations", () => {
|
||||||
|
const msg: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "This is a normal response without any tool calls.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = extractAssistantText(msg);
|
||||||
|
expect(result).toBe("This is a normal response without any tool calls.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("strips tool XML mixed with regular content", () => {
|
||||||
|
const msg: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `I'll help you with that.<invoke name="Bash">
|
||||||
|
<parameter name="command">ls -la</parameter>
|
||||||
|
</invoke>
|
||||||
|
</minimax:tool_call>Here are the results.`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = extractAssistantText(msg);
|
||||||
|
expect(result).toBe("I'll help you with that.\nHere are the results.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles multiple invoke blocks in one message", () => {
|
||||||
|
const msg: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `First check.<invoke name="Read">
|
||||||
|
<parameter name="path">file1.txt</parameter>
|
||||||
|
</invoke>
|
||||||
|
</minimax:tool_call>Second check.<invoke name="Bash">
|
||||||
|
<parameter name="command">pwd</parameter>
|
||||||
|
</invoke>
|
||||||
|
</minimax:tool_call>Done.`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = extractAssistantText(msg);
|
||||||
|
expect(result).toBe("First check.\nSecond check.\nDone.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles stray closing tags without opening tags", () => {
|
||||||
|
const msg: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Some text here.</minimax:tool_call>More text.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = extractAssistantText(msg);
|
||||||
|
expect(result).toBe("Some text here.More text.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns empty string when message is only tool invocations", () => {
|
||||||
|
const msg: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `<invoke name="Bash">
|
||||||
|
<parameter name="command">test</parameter>
|
||||||
|
</invoke>
|
||||||
|
</minimax:tool_call>`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = extractAssistantText(msg);
|
||||||
|
expect(result).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles multiple text blocks", () => {
|
||||||
|
const msg: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "First block.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `<invoke name="Bash">
|
||||||
|
<parameter name="command">ls</parameter>
|
||||||
|
</invoke>
|
||||||
|
</minimax:tool_call>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Third block.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = extractAssistantText(msg);
|
||||||
|
expect(result).toBe("First block.\nThird block.");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,25 @@
|
|||||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||||
import { formatToolDetail, resolveToolDisplay } from "./tool-display.js";
|
import { formatToolDetail, resolveToolDisplay } from "./tool-display.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strip malformed Minimax tool invocations that leak into text content.
|
||||||
|
* Minimax sometimes embeds tool calls as XML in text blocks instead of
|
||||||
|
* proper structured tool calls. This removes:
|
||||||
|
* - <invoke name="...">...</invoke> blocks
|
||||||
|
* - </minimax:tool_call> closing tags
|
||||||
|
*/
|
||||||
|
function stripMinimaxToolCallXml(text: string): string {
|
||||||
|
if (!text) return text;
|
||||||
|
|
||||||
|
// Remove <invoke name="...">...</invoke> blocks (non-greedy to handle multiple)
|
||||||
|
let cleaned = text.replace(/<invoke\s+name="[^"]*">[\s\S]*?<\/invoke>/gi, "");
|
||||||
|
|
||||||
|
// Remove stray </minimax:tool_call> tags
|
||||||
|
cleaned = cleaned.replace(/<\/minimax:tool_call>/gi, "");
|
||||||
|
|
||||||
|
return cleaned.trim();
|
||||||
|
}
|
||||||
|
|
||||||
export function extractAssistantText(msg: AssistantMessage): string {
|
export function extractAssistantText(msg: AssistantMessage): string {
|
||||||
const isTextBlock = (
|
const isTextBlock = (
|
||||||
block: unknown,
|
block: unknown,
|
||||||
@@ -13,7 +32,7 @@ export function extractAssistantText(msg: AssistantMessage): string {
|
|||||||
const blocks = Array.isArray(msg.content)
|
const blocks = Array.isArray(msg.content)
|
||||||
? msg.content
|
? msg.content
|
||||||
.filter(isTextBlock)
|
.filter(isTextBlock)
|
||||||
.map((c) => c.text.trim())
|
.map((c) => stripMinimaxToolCallXml(c.text).trim())
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
: [];
|
: [];
|
||||||
return blocks.join("\n").trim();
|
return blocks.join("\n").trim();
|
||||||
|
|||||||
Reference in New Issue
Block a user