Files
oh-my-pi/packages/agent/test/serialize-conversation.test.ts
T
can1357 e1814d9a08 refactor: renamed grammar module to dialect with unified transcript rendering
- Renamed ToolCallSyntax type to Dialect and Grammar interface to DialectDefinition across all packages.
- Moved grammar directory to dialect and updated all import paths in agent, ai, catalog, and coding-agent packages.
- Added renderTranscript and renderThinking methods to DialectDefinition, enabling native dialect-aware conversation serialization.
- Consolidated rendering utilities into new dialect/rendering.ts with shared helpers for ChatML, legacy text, and dialect-specific formatting.
- Updated conversation serialization in agent and coding-agent to use dialect.renderTranscript() for native turn envelope rendering.
2026-06-15 13:58:12 +02:00

99 lines
3.0 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { serializeConversation } from "@oh-my-pi/pi-agent-core/compaction";
import type { AssistantMessage, Message, ToolResultMessage, Usage } from "@oh-my-pi/pi-ai";
const ZERO_USAGE: Usage = {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
};
function assistantMessage(content: AssistantMessage["content"]): Message {
return {
role: "assistant",
content,
api: "mock",
provider: "mock",
model: "mock",
usage: ZERO_USAGE,
stopReason: "stop",
timestamp: 0,
};
}
function toolResultMessage(toolCallId: string, text: string, extra: Partial<ToolResultMessage> = {}): Message {
return {
role: "toolResult",
toolCallId,
toolName: "search",
content: [{ type: "text", text }],
isError: false,
timestamp: 0,
...extra,
};
}
describe("serializeConversation — useless pairs", () => {
test("skips a useless-flagged tool call/result pair but keeps its sibling", () => {
const out = serializeConversation([
assistantMessage([
{ type: "toolCall", id: "c-keep", name: "search", arguments: { pattern: "alpha" } },
{ type: "toolCall", id: "c-drop", name: "search", arguments: { pattern: "zzz_nothing" } },
]),
toolResultMessage("c-keep", "alpha match found in src/alpha.ts"),
toolResultMessage("c-drop", "No matches found", { useless: true }),
]);
expect(out).toContain('search(pattern="alpha")');
expect(out).toContain("alpha match found in src/alpha.ts");
expect(out).not.toContain("zzz_nothing");
expect(out).not.toContain("No matches found");
});
test("error results stay serialized even when flagged useless", () => {
const out = serializeConversation([
assistantMessage([{ type: "toolCall", id: "c-err", name: "search", arguments: { pattern: "beta" } }]),
toolResultMessage("c-err", "grep crashed", { useless: true, isError: true }),
]);
expect(out).toContain('search(pattern="beta")');
expect(out).toContain("[Tool Result]: grep crashed");
});
test("renders native dialect transcripts when a dialect is provided", () => {
const out = serializeConversation(
[
assistantMessage([
{ type: "text", text: "Searching." },
{ type: "toolCall", id: "c-native", name: "search", arguments: { pattern: "gamma" } },
]),
toolResultMessage("c-native", "gamma match found"),
],
"anthropic",
);
expect(out).toContain("\n\nAssistant:");
expect(out).toContain("<function_calls>");
expect(out).toContain("<function_results>");
expect(out).not.toContain("[Tool Call]:");
expect(out).not.toContain("[Assistant tool calls]:");
});
test("native dialect serialization drops empty assistants left by useless calls", () => {
const out = serializeConversation(
[
assistantMessage([
{ type: "toolCall", id: "c-drop", name: "search", arguments: { pattern: "zzz_nothing" } },
]),
toolResultMessage("c-drop", "No matches found", { useless: true }),
],
"harmony",
);
expect(out).toBe("");
});
});