mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 06:04:51 +00:00
Export serializeConversation and document in compaction.md
Shows how to convert messages to text for custom summarization.
This commit is contained in:
parent
67a1b9581c
commit
75269add96
2 changed files with 33 additions and 0 deletions
|
|
@ -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.
|
See [examples/hooks/custom-compaction.ts](../examples/hooks/custom-compaction.ts) for a complete example using a different model.
|
||||||
|
|
||||||
### session_before_tree
|
### session_before_tree
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ export {
|
||||||
generateSummary,
|
generateSummary,
|
||||||
getLastAssistantUsage,
|
getLastAssistantUsage,
|
||||||
prepareBranchEntries,
|
prepareBranchEntries,
|
||||||
|
serializeConversation,
|
||||||
shouldCompact,
|
shouldCompact,
|
||||||
} from "./core/compaction/index.js";
|
} from "./core/compaction/index.js";
|
||||||
// Custom tools
|
// Custom tools
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue