Files
openclaw/src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.emits-reasoning-as-separate-message-enabled.e2e.test.ts
2026-02-16 14:59:30 +00:00

74 lines
2.4 KiB
TypeScript

import type { AssistantMessage } from "@mariozechner/pi-ai";
import { describe, expect, it, vi } from "vitest";
import { createStubSessionHarness } from "./pi-embedded-subscribe.e2e-harness.js";
import { subscribeEmbeddedPiSession } from "./pi-embedded-subscribe.js";
describe("subscribeEmbeddedPiSession", () => {
const THINKING_TAG_CASES = [
{ tag: "think", open: "<think>", close: "</think>" },
{ tag: "thinking", open: "<thinking>", close: "</thinking>" },
{ tag: "thought", open: "<thought>", close: "</thought>" },
{ tag: "antthinking", open: "<antthinking>", close: "</antthinking>" },
] as const;
function createReasoningBlockReplyHarness() {
const { session, emit } = createStubSessionHarness();
const onBlockReply = vi.fn();
subscribeEmbeddedPiSession({
session,
runId: "run",
onBlockReply,
blockReplyBreak: "message_end",
reasoningMode: "on",
});
return { emit, onBlockReply };
}
it("emits reasoning as a separate message when enabled", () => {
const { emit, onBlockReply } = createReasoningBlockReplyHarness();
const assistantMessage = {
role: "assistant",
content: [
{ type: "thinking", thinking: "Because it helps" },
{ type: "text", text: "Final answer" },
],
} as AssistantMessage;
emit({ type: "message_end", message: assistantMessage });
expect(onBlockReply).toHaveBeenCalledTimes(2);
expect(onBlockReply.mock.calls[0][0].text).toBe("Reasoning:\n_Because it helps_");
expect(onBlockReply.mock.calls[1][0].text).toBe("Final answer");
});
it.each(THINKING_TAG_CASES)(
"promotes <%s> tags to thinking blocks at write-time",
({ open, close }) => {
const { emit, onBlockReply } = createReasoningBlockReplyHarness();
const assistantMessage = {
role: "assistant",
content: [
{
type: "text",
text: `${open}\nBecause it helps\n${close}\n\nFinal answer`,
},
],
} as AssistantMessage;
emit({ type: "message_end", message: assistantMessage });
expect(onBlockReply).toHaveBeenCalledTimes(2);
expect(onBlockReply.mock.calls[0][0].text).toBe("Reasoning:\n_Because it helps_");
expect(onBlockReply.mock.calls[1][0].text).toBe("Final answer");
expect(assistantMessage.content).toEqual([
{ type: "thinking", thinking: "Because it helps" },
{ type: "text", text: "Final answer" },
]);
},
);
});