fix: improve chat message display and compression logic (#476)
Frontend: - Filter out assistant messages with empty content in mapHermesMessages() - Prevents displaying 194/585 empty messages in long sessions - Reduces visual clutter and improves UX Backend: - Add message count limit (<= 200) to compression threshold checks - Prevents compressing sessions with too many messages - Improves compression performance and reliability Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -119,10 +119,18 @@ async function buildContentBlocks(
|
||||
}
|
||||
|
||||
function mapHermesMessages(msgs: HermesMessage[]): Message[] {
|
||||
// Filter out assistant messages with empty content
|
||||
const filteredMsgs = msgs.filter(m => {
|
||||
if (m.role === 'assistant') {
|
||||
return m.content && m.content.trim() !== ''
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
// Build lookups from assistant messages with tool_calls
|
||||
const toolNameMap = new Map<string, string>()
|
||||
const toolArgsMap = new Map<string, string>()
|
||||
for (const msg of msgs) {
|
||||
for (const msg of filteredMsgs) {
|
||||
if (msg.role === 'assistant' && msg.tool_calls) {
|
||||
for (const tc of msg.tool_calls) {
|
||||
if (tc.id) {
|
||||
@@ -134,7 +142,7 @@ function mapHermesMessages(msgs: HermesMessage[]): Message[] {
|
||||
}
|
||||
|
||||
const result: Message[] = []
|
||||
for (const msg of msgs) {
|
||||
for (const msg of filteredMsgs) {
|
||||
// Skip assistant messages that only contain tool_calls (no meaningful content)
|
||||
if (msg.role === 'assistant' && msg.tool_calls?.length && !msg.content?.trim()) {
|
||||
// Emit a tool.started message for each tool call
|
||||
|
||||
@@ -578,7 +578,7 @@ export class ChatRunSocket {
|
||||
logger.info('[context-compress] session=%s: snapshot at %d, %d new messages, assembled ~%d tokens (threshold %d)',
|
||||
session_id, snapshot.lastMessageIndex, newMessages.length, totalTokens, triggerTokens)
|
||||
// triggerTokens
|
||||
if (totalTokens <= triggerTokens) {
|
||||
if (totalTokens <= triggerTokens && newMessages.length <= 200) {
|
||||
// Under threshold — use assembled context directly, no LLM call needed
|
||||
history = [
|
||||
{ role: 'user', content: SUMMARY_PREFIX + '\n\n' + snapshot.summary },
|
||||
@@ -684,7 +684,7 @@ export class ChatRunSocket {
|
||||
} else if (history.length > 4) {
|
||||
// No snapshot — check if raw history exceeds threshold
|
||||
|
||||
if (totalTokens <= triggerTokens) {
|
||||
if (totalTokens <= triggerTokens && history.length <= 200) {
|
||||
// Under threshold — use raw history as-is
|
||||
logger.info('[context-compress] session=%s: %d messages, ~%d tokens — under threshold, skip', session_id, history.length, totalTokens)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user