MCPcopy Create free account
hub / github.com/WJX20/claude-code / processUserMessage

Function processUserMessage

src/utils/analyzeContext.ts:808–840  ·  view source on GitHub ↗
(
  msg: UserMessage | NormalizedUserMessage,
  breakdown: MessageBreakdown,
  toolUseIdToName: Map<string, string>,
)

Source from the content-addressed store, hash-verified

806}
807
808function processUserMessage(
809 msg: UserMessage | NormalizedUserMessage,
810 breakdown: MessageBreakdown,
811 toolUseIdToName: Map<string, string>,
812): void {
813 // Handle both string and array content
814 if (typeof msg.message.content === 'string') {
815 // Simple string content
816 const tokens = roughTokenCountEstimation(msg.message.content)
817 breakdown.userMessageTokens += tokens
818 return
819 }
820
821 // Process each content block individually
822 for (const block of msg.message.content) {
823 const blockStr = jsonStringify(block)
824 const blockTokens = roughTokenCountEstimation(blockStr)
825
826 if ('type' in block && block.type === 'tool_result') {
827 breakdown.toolResultTokens += blockTokens
828 const toolUseId = 'tool_use_id' in block ? block.tool_use_id : undefined
829 const toolName =
830 (toolUseId ? toolUseIdToName.get(toolUseId) : undefined) || 'unknown'
831 breakdown.toolResultsByType.set(
832 toolName,
833 (breakdown.toolResultsByType.get(toolName) || 0) + blockTokens,
834 )
835 } else {
836 // Text blocks or other non-tool content
837 breakdown.userMessageTokens += blockTokens
838 }
839 }
840}
841
842function processAttachment(
843 msg: AttachmentMessage,

Callers 1

approximateMessageTokensFunction · 0.85

Calls 4

jsonStringifyFunction · 0.85
setMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected