From 75269add9644540b3142bbdfeda69bfd8e7f5f9b Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Wed, 31 Dec 2025 13:21:55 +0100 Subject: [PATCH] Export serializeConversation and document in compaction.md Shows how to convert messages to text for custom summarization. --- packages/coding-agent/docs/compaction.md | 32 ++++++++++++++++++++++++ packages/coding-agent/src/index.ts | 1 + 2 files changed, 33 insertions(+) diff --git a/packages/coding-agent/docs/compaction.md b/packages/coding-agent/docs/compaction.md index 86836d3b..bb11867b 100644 --- a/packages/coding-agent/docs/compaction.md +++ b/packages/coding-agent/docs/compaction.md @@ -298,6 +298,38 @@ pi.on("session_before_compact", async (event, ctx) => { }); ``` +#### Converting Messages to Text + +To generate a summary with your own model, convert messages to text using `serializeConversation`: + +```typescript +import { serializeConversation } from "@mariozechner/pi-coding-agent"; + +pi.on("session_before_compact", async (event, ctx) => { + const { preparation } = event; + + // Convert messages to readable text format + const conversationText = serializeConversation(preparation.messagesToSummarize); + // Returns: + // [User]: message text + // [Assistant thinking]: thinking content + // [Assistant]: response text + // [Assistant tool calls]: read(path="..."); bash(command="...") + // [Tool result]: output text + + // Now send to your model for summarization + const summary = await myModel.summarize(conversationText); + + return { + compaction: { + summary, + firstKeptEntryId: preparation.firstKeptEntryId, + tokensBefore: preparation.tokensBefore, + } + }; +}); +``` + See [examples/hooks/custom-compaction.ts](../examples/hooks/custom-compaction.ts) for a complete example using a different model. ### session_before_tree diff --git a/packages/coding-agent/src/index.ts b/packages/coding-agent/src/index.ts index 4efaacd4..0eeaef4c 100644 --- a/packages/coding-agent/src/index.ts +++ b/packages/coding-agent/src/index.ts @@ -30,6 +30,7 @@ export { generateSummary, getLastAssistantUsage, prepareBranchEntries, + serializeConversation, shouldCompact, } from "./core/compaction/index.js"; // Custom tools