import type { AssistantMessage } from "@mariozechner/pi-ai"; import { describe, expect, it } from "vitest"; import { extractAssistantText } from "./pi-embedded-utils.js"; describe("extractAssistantText", () => { it("strips Minimax tool invocation XML from text", () => { const msg: AssistantMessage = { role: "assistant", content: [ { type: "text", text: ` netstat -tlnp | grep 18789 `, }, ], 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. /home/admin/test.txt `, }, ], timestamp: Date.now(), }; const result = extractAssistantText(msg); expect(result).toBe("Let me check that."); }); it("keeps invoke snippets without Minimax markers", () => { const msg: AssistantMessage = { role: "assistant", content: [ { type: "text", text: `Example:\n\nls\n`, }, ], timestamp: Date.now(), }; const result = extractAssistantText(msg); expect(result).toBe( `Example:\n\nls\n`, ); }); 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 Minimax tool invocations with extra attributes", () => { const msg: AssistantMessage = { role: "assistant", content: [ { type: "text", text: `Before\nls\n\nAfter`, }, ], timestamp: Date.now(), }; const result = extractAssistantText(msg); expect(result).toBe("Before\nAfter"); }); it("strips minimax tool_call open and close tags", () => { const msg: AssistantMessage = { role: "assistant", content: [ { type: "text", text: "StartInnerEnd", }, ], timestamp: Date.now(), }; const result = extractAssistantText(msg); expect(result).toBe("StartInnerEnd"); }); it("ignores invoke blocks without minimax markers", () => { const msg: AssistantMessage = { role: "assistant", content: [ { type: "text", text: "BeforeKeepAfter", }, ], timestamp: Date.now(), }; const result = extractAssistantText(msg); expect(result).toBe("BeforeKeepAfter"); }); it("strips invoke blocks when minimax markers are present elsewhere", () => { const msg: AssistantMessage = { role: "assistant", content: [ { type: "text", text: "BeforeDropAfter", }, ], timestamp: Date.now(), }; const result = extractAssistantText(msg); expect(result).toBe("BeforeAfter"); }); it("strips invoke blocks with nested tags", () => { const msg: AssistantMessage = { role: "assistant", content: [ { type: "text", text: `A1B`, }, ], timestamp: Date.now(), }; const result = extractAssistantText(msg); expect(result).toBe("AB"); }); it("strips tool XML mixed with regular content", () => { const msg: AssistantMessage = { role: "assistant", content: [ { type: "text", text: `I'll help you with that. ls -la 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. file1.txt Second check. pwd 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.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: ` test `, }, ], 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: ` ls `, }, { type: "text", text: "Third block.", }, ], timestamp: Date.now(), }; const result = extractAssistantText(msg); expect(result).toBe("First block.\nThird block."); }); });