MCPcopy Create free account
hub / github.com/Noumena-Network/code / processUserMessage

Function processUserMessage

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

Source from the content-addressed store, hash-verified

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

Callers 1

approximateMessageTokensFunction · 0.85

Calls 4

jsonStringifyFunction · 0.85
setMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected