feat(context): detect compaction events from SDK compact_boundary messages

This commit is contained in:
Hibryda 2026-03-10 04:20:32 +01:00
parent d898586181
commit f4ec2f3762
2 changed files with 88 additions and 1 deletions

View file

@ -8,6 +8,7 @@ export type AgentMessageType =
| 'tool_call'
| 'tool_result'
| 'status'
| 'compaction'
| 'cost'
| 'error'
| 'unknown';
@ -62,6 +63,11 @@ export interface CostContent {
errors?: string[];
}
export interface CompactionContent {
trigger: 'manual' | 'auto';
preTokens: number;
}
export interface ErrorContent {
message: string;
}
@ -125,6 +131,21 @@ function adaptSystemMessage(
}];
}
if (subtype === 'compact_boundary') {
const meta = typeof raw.compact_metadata === 'object' && raw.compact_metadata !== null
? raw.compact_metadata as Record<string, unknown>
: {};
return [{
id: uuid,
type: 'compaction',
content: {
trigger: str(meta.trigger, 'auto') as 'manual' | 'auto',
preTokens: num(meta.pre_tokens),
} satisfies CompactionContent,
timestamp,
}];
}
return [{
id: uuid,
type: 'status',