diff --git a/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts b/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts index ea0de2f..c7b50ff 100644 --- a/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts +++ b/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts @@ -211,7 +211,7 @@ export class CoordinatorAgentService implements OnModuleInit { const systemPrompt = this.buildSystemPromptBlocks(); // 3. Inject dynamic context - const enrichedMessages = await this.contextInjector.inject( + let enrichedMessages = await this.contextInjector.inject( { conversationId: context.conversationId, userId: context.userId, @@ -227,6 +227,13 @@ export class CoordinatorAgentService implements OnModuleInit { messages, ); + // 3.5. Auto-compact if messages exceed token threshold + const { messages: compactedMessages, wasCompacted } = + await this.contextInjector.autoCompactIfNeeded(enrichedMessages, this.anthropicClient); + if (wasCompacted) { + enrichedMessages = compactedMessages; + } + // 4. Create abort controller const abortController = new AbortController();