feat: add durable context compression
Long direct-model conversations mixed provider usage with local estimates, and Agent tool rounds could remain above the configured compression target. Compression state and status markers also did not reliably survive restarts or bounded history rollover. Direct-model calls now prefer provider-reported usage, compact complete conversation turns and Agent tool rounds within reserved payload budgets, and persist reusable summaries with scope-specific markers. The chat meter separates latest-call usage from estimated compressed conversation size, while failed or cancelled calls retain the last successful measurement. Release note: 直连模型现可在长对话和多轮工具执行中自动压缩旧上下文,并分别显示本次调用用量与压缩后对话估算;摘要会自动保存并跨重启复用,无需手动操作。
This commit is contained in:
+26
-7
@@ -17,6 +17,7 @@ import type {
|
||||
} from './capability-contracts'
|
||||
import {
|
||||
assistantIdSchema,
|
||||
conversationContextCompressionStateSchema,
|
||||
legacyWorkModeSchema,
|
||||
type AssistantProject,
|
||||
type AssistantArtifact,
|
||||
@@ -30,6 +31,7 @@ import {
|
||||
type TokenUsageSummary,
|
||||
type ConversationSnapshot,
|
||||
type ConversationAttachment,
|
||||
type ConversationContextCompressionState,
|
||||
type LocalConversationSaveBatch,
|
||||
type WorkspaceChanges,
|
||||
type WorkspaceDirectoryListing,
|
||||
@@ -220,7 +222,12 @@ export const agentRequestSchema = z
|
||||
.strict()
|
||||
)
|
||||
.max(500)
|
||||
.optional()
|
||||
.optional(),
|
||||
historyMessageIds: z.array(z.string().uuid()).max(500).optional(),
|
||||
currentUserMessageId: z.string().uuid().optional(),
|
||||
currentAssistantMessageId: z.string().uuid().optional(),
|
||||
contextCompressionState:
|
||||
conversationContextCompressionStateSchema.optional()
|
||||
})
|
||||
.strict()
|
||||
.superRefine((request, context) => {
|
||||
@@ -236,6 +243,17 @@ export const agentRequestSchema = z
|
||||
message: '会话历史总长度不能超过 2,000,000 个字符'
|
||||
})
|
||||
}
|
||||
if (
|
||||
request.historyMessageIds &&
|
||||
request.historyMessageIds.length !==
|
||||
(request.history?.length ?? 0)
|
||||
) {
|
||||
context.addIssue({
|
||||
code: 'custom',
|
||||
path: ['historyMessageIds'],
|
||||
message: '会话历史消息 ID 必须与历史消息一一对应'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export type AgentRequest = z.input<typeof agentRequestSchema>
|
||||
@@ -919,25 +937,26 @@ export type AgentEvent =
|
||||
| {
|
||||
requestId: string
|
||||
type: 'context-metrics'
|
||||
estimatedInputTokens: number
|
||||
contextTokens: number
|
||||
effectiveTriggerTokens: number
|
||||
contextWindowTokens?: number
|
||||
compressionEnabled: boolean
|
||||
recentRawTokens: number
|
||||
coveredMessageCount: number
|
||||
summaryTokens: number
|
||||
source: 'provider' | 'estimated'
|
||||
}
|
||||
| {
|
||||
requestId: string
|
||||
type: 'context-compression'
|
||||
state: 'started' | 'completed'
|
||||
scope?: 'conversation' | 'agent-run'
|
||||
state: 'started' | 'completed' | 'failed'
|
||||
estimatedBeforeTokens: number
|
||||
estimatedAfterTokens?: number
|
||||
effectiveTriggerTokens: number
|
||||
contextWindowTokens?: number
|
||||
recentRawTokens: number
|
||||
coveredMessageCount: number
|
||||
coveredMessageCount?: number
|
||||
compressionCount?: number
|
||||
summaryTokens?: number
|
||||
conversationState?: ConversationContextCompressionState
|
||||
}
|
||||
| {
|
||||
requestId: string
|
||||
|
||||
Reference in New Issue
Block a user