feat(ai): enable automatic context compression

This commit is contained in:
digua committed 2026-08-07 15:35:29 +08:00
1 parent 5c7ad41b23
commit 17320045dc
43 files changed
+546 -309

No files matched your search

+2 -12
View File
@@ -12,6 +12,7 @@ import type {
} from '@openchatlab/node-runtime'
import {
CHART_CAPABILITY_CORE_TOOLS,
DEFAULT_CONTEXT_COMPRESSION_CONFIG,
SkillManager,
buildPiModel,
buildSkillMenuWithBuiltinChart,
@@ -93,7 +94,6 @@ export function createCliRunAgentStream(
skillId,
enableAutoSkill,
chartAutoMode,
compressionConfig,
ownerInfo,
mentionedMembers,
thinkingLevel,
@@ -110,7 +110,7 @@ export function createCliRunAgentStream(
}
const llmConfig = llmConfigStore.getDefaultAssistantConfig()
const maxToolResultPercent = compressionConfig?.maxToolResultPercent ?? 50
const maxToolResultPercent = DEFAULT_CONTEXT_COMPRESSION_CONFIG.maxToolResultPercent ?? 50
const contextWindow = llmConfig ? (buildPiModel(llmConfig).contextWindow ?? 128000) : 128000
const maxToolResultTokens = Math.floor(contextWindow * (maxToolResultPercent / 100))
@@ -196,15 +196,6 @@ export function createCliRunAgentStream(
agentTools.push(activateSkillTool as any)
}
const resolvedCompression = compressionConfig?.enabled
? {
enabled: true as const,
tokenThresholdPercent: compressionConfig.tokenThresholdPercent ?? 75,
bufferSizePercent: compressionConfig.bufferSizePercent ?? 20,
maxToolResultPercent: compressionConfig.maxToolResultPercent,
}
: undefined
let dataSnapshot: DataSnapshot | undefined
if (db) {
try {
@@ -223,7 +214,6 @@ export function createCliRunAgentStream(
assistantSystemPrompt,
skillMenu: resolvedSkillMenu,
skillDef: resolvedSkillDef,
compressionConfig: resolvedCompression,
tools: agentTools,
llmConfig,
aiChatManager,
+4 -6
View File
@@ -7,6 +7,7 @@
import {
DEFAULT_MAX_TOOL_ROUNDS,
DEFAULT_CONTEXT_COMPRESSION_CONFIG,
buildPiModel,
buildPlanGuidance,
createAnalysisPlanner,
@@ -27,7 +28,6 @@ import {
type PiMessage,
type SimpleHistoryMessage,
type AIChatManager,
type CompressionConfig,
type AgentTool,
type DataSnapshot,
type AIServiceConfig,
@@ -49,7 +49,6 @@ export interface RunAgentOptions {
assistantSystemPrompt?: string
skillMenu?: string | null
skillDef?: { name: string; prompt: string }
compressionConfig?: CompressionConfig
tools?: AgentTool[]
llmConfig: AIServiceConfig | null
aiChatManager: AIChatManager
@@ -72,7 +71,6 @@ export async function runServerAgent(options: RunAgentOptions): Promise<void> {
assistantSystemPrompt,
skillMenu,
skillDef,
compressionConfig,
tools = [],
llmConfig,
aiChatManager,
@@ -123,7 +121,7 @@ export async function runServerAgent(options: RunAgentOptions): Promise<void> {
systemPrompt,
})
if (compressionConfig?.enabled && historyLeafMessageId === undefined) {
if (historyLeafMessageId === undefined) {
const llmAdapter = createCompressionLlmAdapter({
piModel,
apiKey: llmConfig.apiKey,
@@ -131,7 +129,7 @@ export async function runServerAgent(options: RunAgentOptions): Promise<void> {
})
const compressionResult = await checkAndCompress(
aiChatId,
compressionConfig,
DEFAULT_CONTEXT_COMPRESSION_CONFIG,
systemPrompt,
llmAdapter,
aiChatManager,
@@ -148,7 +146,7 @@ export async function runServerAgent(options: RunAgentOptions): Promise<void> {
},
})
}
} else if (compressionConfig?.enabled && historyLeafMessageId !== undefined) {
} else {
aiLogger?.info?.('Compression', 'Skipping compression for edited branch request', {
aiChatId,
historyLeafMessageId,