From 3a9eccddbd9aba23dc74e486229bccb9425e9093 Mon Sep 17 00:00:00 2001 From: Oleg Ivaniv Date: Tue, 5 May 2026 10:24:53 +0200 Subject: [PATCH] refactor(instance-ai): use native agent for compaction --- .../src/compaction/compaction-helper.ts | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/packages/@n8n/instance-ai/src/compaction/compaction-helper.ts b/packages/@n8n/instance-ai/src/compaction/compaction-helper.ts index 2f405215163..5bf94af8d23 100644 --- a/packages/@n8n/instance-ai/src/compaction/compaction-helper.ts +++ b/packages/@n8n/instance-ai/src/compaction/compaction-helper.ts @@ -1,4 +1,4 @@ -import { Agent } from '@mastra/core/agent'; +import { Agent, type AgentMessage, type ModelConfig as NativeModelConfig } from '@n8n/agents'; import type { ModelConfig } from '../types'; @@ -38,23 +38,40 @@ export interface CompactionInput { messageBatch: Array<{ role: string; text: string }>; } +function extractAssistantText(messages: AgentMessage[]): string { + return messages + .filter((message) => 'role' in message && message.role === 'assistant') + .flatMap((message) => ('content' in message ? message.content : [])) + .filter((part) => part.type === 'text') + .map((part) => part.text) + .join(''); +} + +function toNativeModelConfig(modelId: ModelConfig): NativeModelConfig { + if (typeof modelId === 'string') return modelId; + + if ('id' in modelId && typeof modelId.id === 'string') { + const nativeConfig: Extract = { id: modelId.id }; + if (modelId.apiKey !== undefined) nativeConfig.apiKey = modelId.apiKey; + if (modelId.url !== undefined) nativeConfig.url = modelId.url; + if (modelId.headers !== undefined) nativeConfig.headers = modelId.headers; + return nativeConfig; + } + + throw new Error('Native compaction requires a native agents model config'); +} + /** * Generate a compacted summary from a previous summary and a batch of messages. - * Uses a lightweight Mastra Agent with no tools and no memory. + * Uses a lightweight native Agent with no tools and no memory. */ export async function generateCompactionSummary( modelId: ModelConfig, input: CompactionInput, ): Promise { - const agent = new Agent({ - id: 'compaction-summarizer', - name: 'Compaction Summarizer', - instructions: { - role: 'system' as const, - content: COMPACTION_SYSTEM_PROMPT, - }, - model: modelId, - }); + const agent = new Agent('Compaction Summarizer') + .model(toNativeModelConfig(modelId)) + .instructions(COMPACTION_SYSTEM_PROMPT); const parts: string[] = []; @@ -70,6 +87,6 @@ export async function generateCompactionSummary( parts.push('Produce the updated summary now. Return only the five sections, nothing else.'); - const result = await agent.generate(parts.join('\n\n'), { maxSteps: 1 }); - return result.text; + const result = await agent.generate(parts.join('\n\n'), { maxIterations: 1 }); + return extractAssistantText(result.messages); }