fix(minimax): strip tool invocation XML from assistant text
This commit is contained in:
committed by
Peter Steinberger
parent
1f3ae2346e
commit
350f956f2c
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 { 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 {
|
||||
const isTextBlock = (
|
||||
block: unknown,
|
||||
@@ -13,7 +32,7 @@ export function extractAssistantText(msg: AssistantMessage): string {
|
||||
const blocks = Array.isArray(msg.content)
|
||||
? msg.content
|
||||
.filter(isTextBlock)
|
||||
.map((c) => c.text.trim())
|
||||
.map((c) => stripMinimaxToolCallXml(c.text).trim())
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
return blocks.join("\n").trim();
|
||||
|
||||
Reference in New Issue
Block a user