MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / buildMessageLookups

Function buildMessageLookups

source code/utils/messages.ts:1172–1342  ·  view source on GitHub ↗
(
  normalizedMessages: NormalizedMessage[],
  messages: Message[],
)

Source from the content-addressed store, hash-verified

1170 * getSiblingToolUseIDs, and hasUnresolvedHooks for each message.
1171 */
1172export function buildMessageLookups(
1173 normalizedMessages: NormalizedMessage[],
1174 messages: Message[],
1175): MessageLookups {
1176 // First pass: group assistant messages by ID and collect all tool use IDs per message
1177 const toolUseIDsByMessageID = new Map<string, Set<string>>()
1178 const toolUseIDToMessageID = new Map<string, string>()
1179 const toolUseByToolUseID = new Map<string, ToolUseBlockParam>()
1180 for (const msg of messages) {
1181 if (msg.type === 'assistant') {
1182 const id = msg.message.id
1183 let toolUseIDs = toolUseIDsByMessageID.get(id)
1184 if (!toolUseIDs) {
1185 toolUseIDs = new Set()
1186 toolUseIDsByMessageID.set(id, toolUseIDs)
1187 }
1188 for (const content of msg.message.content) {
1189 if (content.type === 'tool_use') {
1190 toolUseIDs.add(content.id)
1191 toolUseIDToMessageID.set(content.id, id)
1192 toolUseByToolUseID.set(content.id, content)
1193 }
1194 }
1195 }
1196 }
1197
1198 // Build sibling lookup - each tool use ID maps to all sibling tool use IDs
1199 const siblingToolUseIDs = new Map<string, Set<string>>()
1200 for (const [toolUseID, messageID] of toolUseIDToMessageID) {
1201 siblingToolUseIDs.set(toolUseID, toolUseIDsByMessageID.get(messageID)!)
1202 }
1203
1204 // Single pass over normalizedMessages to build progress, hook, and tool result lookups
1205 const progressMessagesByToolUseID = new Map<string, ProgressMessage[]>()
1206 const inProgressHookCounts = new Map<string, Map<HookEvent, number>>()
1207 // Track unique hook names per (toolUseID, hookEvent) to match getResolvedHookCount behavior.
1208 // A single hook can produce multiple attachment messages (e.g., hook_success + hook_additional_context),
1209 // so we deduplicate by hookName.
1210 const resolvedHookNames = new Map<string, Map<HookEvent, Set<string>>>()
1211 const toolResultByToolUseID = new Map<string, NormalizedMessage>()
1212 // Track resolved/errored tool use IDs (replaces separate useMemos in Messages.tsx)
1213 const resolvedToolUseIDs = new Set<string>()
1214 const erroredToolUseIDs = new Set<string>()
1215
1216 for (const msg of normalizedMessages) {
1217 if (msg.type === 'progress') {
1218 // Build progress messages lookup
1219 const toolUseID = msg.parentToolUseID
1220 const existing = progressMessagesByToolUseID.get(toolUseID)
1221 if (existing) {
1222 existing.push(msg)
1223 } else {
1224 progressMessagesByToolUseID.set(toolUseID, [msg])
1225 }
1226
1227 // Count in-progress hooks
1228 if (msg.data.type === 'hook_progress') {
1229 const hookEvent = msg.data.hookEvent

Callers 1

MessagesImplFunction · 0.85

Calls 5

isHookAttachmentMessageFunction · 0.85
setMethod · 0.80
getMethod · 0.45
addMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected