MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / formatMessagesCompact

Function formatMessagesCompact

packages/mcp-server/src/format.ts:118–150  ·  view source on GitHub ↗
(messages: FormattableMessage[])

Source from the content-addressed store, hash-verified

116 * Filters out noise messages, merges same-sender runs, truncates long content.
117 */
118export function formatMessagesCompact(messages: FormattableMessage[]): string {
119 if (messages.length === 0) return ''
120
121 const valid = messages.filter((m) => m.content && isValidMessage(m.content))
122 if (valid.length === 0) return ''
123
124 const lines: string[] = []
125 let prevSender = ''
126 let pendingContents: string[] = []
127 let pendingTs = 0
128
129 const flush = () => {
130 if (pendingContents.length === 0) return
131 const combined = pendingContents.join('; ')
132 const maxLen = pendingContents.length > 1 ? MAX_MERGED_CONTENT_LENGTH : MAX_CONTENT_LENGTH
133 lines.push(`${formatTimestamp(pendingTs)} ${prevSender}: ${truncate(combined, maxLen)}`)
134 }
135
136 for (const msg of valid) {
137 const content = msg.content!.trim()
138 if (msg.senderName === prevSender) {
139 pendingContents.push(content)
140 } else {
141 flush()
142 prevSender = msg.senderName
143 pendingContents = [content]
144 pendingTs = msg.timestamp
145 }
146 }
147 flush()
148
149 return lines.join('\n')
150}
151
152/**
153 * Try to parse a tool result's JSON content and convert to compact text.

Callers 1

formatObjectWithMessagesFunction · 0.85

Calls 2

flushFunction · 0.85
isValidMessageFunction · 0.70

Tested by

no test coverage detected