Update custom-compaction example to use serializeConversation

Also fix docs to show convertToLlm is needed first.
This commit is contained in:
Mario Zechner 2025-12-31 13:24:23 +01:00
parent 75269add96
commit 027d39aa33
2 changed files with 14 additions and 9 deletions

View file

@ -303,13 +303,15 @@ pi.on("session_before_compact", async (event, ctx) => {
To generate a summary with your own model, convert messages to text using `serializeConversation`:
```typescript
import { serializeConversation } from "@mariozechner/pi-coding-agent";
import { convertToLlm, 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);
// Convert AgentMessage[] to Message[], then serialize to text
const conversationText = serializeConversation(
convertToLlm(preparation.messagesToSummarize)
);
// Returns:
// [User]: message text
// [Assistant thinking]: thinking content

View file

@ -14,7 +14,7 @@
*/
import { complete, getModel } from "@mariozechner/pi-ai";
import { convertToLlm } from "@mariozechner/pi-coding-agent";
import { convertToLlm, serializeConversation } from "@mariozechner/pi-coding-agent";
import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
export default function (pi: HookAPI) {
@ -46,21 +46,20 @@ export default function (pi: HookAPI) {
"info",
);
// Transform app messages to pi-ai package format
const transformedMessages = convertToLlm(allMessages);
// Convert messages to readable text format
const conversationText = serializeConversation(convertToLlm(allMessages));
// Include previous summary context if available
const previousContext = previousSummary ? `\n\nPrevious session summary for context:\n${previousSummary}` : "";
// Build messages that ask for a comprehensive summary
const summaryMessages = [
...transformedMessages,
{
role: "user" as const,
content: [
{
type: "text" as const,
text: `You are a conversation summarizer. Create a comprehensive summary of this entire conversation that captures:${previousContext}
text: `You are a conversation summarizer. Create a comprehensive summary of this conversation that captures:${previousContext}
1. The main goals and objectives discussed
2. Key decisions made and their rationale
@ -71,7 +70,11 @@ export default function (pi: HookAPI) {
Be thorough but concise. The summary will replace the ENTIRE conversation history, so include all information needed to continue the work effectively.
Format the summary as structured markdown with clear sections.`,
Format the summary as structured markdown with clear sections.
<conversation>
${conversationText}
</conversation>`,
},
],
timestamp: Date.now(),