fix command compression token estimate (#751)
This commit is contained in:
@@ -66,6 +66,24 @@ export async function buildDbHistory(
|
|||||||
}).filter((m): m is ChatMessage => m !== null)
|
}).filter((m): m is ChatMessage => m !== null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function estimateSnapshotAwareHistoryUsage(
|
||||||
|
sessionId: string,
|
||||||
|
history: ChatMessage[],
|
||||||
|
): { messageCount: number; tokenCount: number } {
|
||||||
|
const snapshot = getCompressionSnapshot(sessionId)
|
||||||
|
const messages = snapshot
|
||||||
|
? [
|
||||||
|
{ role: 'user', content: SUMMARY_PREFIX + snapshot.summary },
|
||||||
|
...history.slice(snapshot.lastMessageIndex + 1),
|
||||||
|
]
|
||||||
|
: history
|
||||||
|
const usage = estimateUsageTokensFromMessages(messages)
|
||||||
|
return {
|
||||||
|
messageCount: messages.length,
|
||||||
|
tokenCount: usage.inputTokens + usage.outputTokens,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function buildCompressedHistory(
|
export async function buildCompressedHistory(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
profile: string,
|
profile: string,
|
||||||
@@ -210,12 +228,13 @@ export async function forceCompressBridgeHistory(
|
|||||||
|
|
||||||
const upstream = getUpstream(profile).replace(/\/$/, '')
|
const upstream = getUpstream(profile).replace(/\/$/, '')
|
||||||
const apiKey = getApiKey(profile) || undefined
|
const apiKey = getApiKey(profile) || undefined
|
||||||
const beforeUsage = estimateUsageTokensFromMessages(history)
|
const beforeUsage = estimateSnapshotAwareHistoryUsage(sessionId, history)
|
||||||
const totalTokens = beforeUsage.inputTokens + beforeUsage.outputTokens
|
const totalTokens = beforeUsage.tokenCount
|
||||||
bridgeLogger.info({
|
bridgeLogger.info({
|
||||||
sessionId,
|
sessionId,
|
||||||
profile,
|
profile,
|
||||||
historyMessages: history.length,
|
historyMessages: history.length,
|
||||||
|
snapshotAwareMessages: beforeUsage.messageCount,
|
||||||
bridgeProvidedMessages: Array.isArray(_messages) ? _messages.length : 0,
|
bridgeProvidedMessages: Array.isArray(_messages) ? _messages.length : 0,
|
||||||
tokenEstimate: totalTokens,
|
tokenEstimate: totalTokens,
|
||||||
snapshotAware: true,
|
snapshotAware: true,
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ import { addMessage, clearSessionMessages, createSession, getSession, renameSess
|
|||||||
import { logger } from '../../logger'
|
import { logger } from '../../logger'
|
||||||
import type { AgentBridgeClient } from '../agent-bridge'
|
import type { AgentBridgeClient } from '../agent-bridge'
|
||||||
import { flushBridgePendingToDb } from './bridge-message'
|
import { flushBridgePendingToDb } from './bridge-message'
|
||||||
import { buildDbHistory, forceCompressBridgeHistory, getOrCreateSession, replaceState } from './compression'
|
import { buildDbHistory, estimateSnapshotAwareHistoryUsage, forceCompressBridgeHistory, getOrCreateSession, replaceState } from './compression'
|
||||||
import { handleAbort } from './abort'
|
import { handleAbort } from './abort'
|
||||||
import { calcAndUpdateUsage, estimateUsageTokensFromMessages } from './usage'
|
import { calcAndUpdateUsage } from './usage'
|
||||||
import type { ContentBlock, QueuedRun, SessionState } from './types'
|
import type { ContentBlock, QueuedRun, SessionState } from './types'
|
||||||
|
|
||||||
type CommandName =
|
type CommandName =
|
||||||
@@ -232,12 +232,11 @@ export async function handleSessionCommand(
|
|||||||
const emit = (event: string, payload: any) => emitToSession(ctx.nsp, ctx.socket, sessionId, event, payload)
|
const emit = (event: string, payload: any) => emitToSession(ctx.nsp, ctx.socket, sessionId, event, payload)
|
||||||
try {
|
try {
|
||||||
const history = await buildDbHistory(sessionId, { excludeLastUser: true })
|
const history = await buildDbHistory(sessionId, { excludeLastUser: true })
|
||||||
const usageEstimate = estimateUsageTokensFromMessages(history)
|
const usageEstimate = estimateSnapshotAwareHistoryUsage(sessionId, history)
|
||||||
const tokenEstimate = usageEstimate.inputTokens + usageEstimate.outputTokens
|
|
||||||
emit('compression.started', {
|
emit('compression.started', {
|
||||||
event: 'compression.started',
|
event: 'compression.started',
|
||||||
message_count: history.length,
|
message_count: usageEstimate.messageCount,
|
||||||
token_count: tokenEstimate,
|
token_count: usageEstimate.tokenCount,
|
||||||
source: 'command',
|
source: 'command',
|
||||||
})
|
})
|
||||||
const result = await forceCompressBridgeHistory(
|
const result = await forceCompressBridgeHistory(
|
||||||
|
|||||||
Reference in New Issue
Block a user