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

View file

@ -14,7 +14,7 @@
*/ */
import { complete, getModel } from "@mariozechner/pi-ai"; 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"; import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
export default function (pi: HookAPI) { export default function (pi: HookAPI) {
@ -46,21 +46,20 @@ export default function (pi: HookAPI) {
"info", "info",
); );
// Transform app messages to pi-ai package format // Convert messages to readable text format
const transformedMessages = convertToLlm(allMessages); const conversationText = serializeConversation(convertToLlm(allMessages));
// Include previous summary context if available // Include previous summary context if available
const previousContext = previousSummary ? `\n\nPrevious session summary for context:\n${previousSummary}` : ""; const previousContext = previousSummary ? `\n\nPrevious session summary for context:\n${previousSummary}` : "";
// Build messages that ask for a comprehensive summary // Build messages that ask for a comprehensive summary
const summaryMessages = [ const summaryMessages = [
...transformedMessages,
{ {
role: "user" as const, role: "user" as const,
content: [ content: [
{ {
type: "text" as const, 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 1. The main goals and objectives discussed
2. Key decisions made and their rationale 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. 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(), timestamp: Date.now(),