ClaudeCodeSDK.ContentExtractor (claude_code_sdk v0.0.1)

View Source

Content extraction helper for Claude Code SDK messages.

This module provides utilities to extract text content from Claude messages, which can have various formats depending on the type of response. It handles simple text, structured content blocks, tool responses, and nested formats.

Content Format Examples

Claude messages can contain content in several formats:

Simple Text

%{"content" => "Hello, world!"}

Text Blocks

%{"content" => [
  %{"type" => "text", "text" => "Hello"},
  %{"type" => "text", "text" => "World"}
]}

Tool Responses

%{"content" => [
  %{"type" => "text", "text" => "I'll help you with that."},
  %{"type" => "tool_use", "name" => "bash", "input" => %{"command" => "ls"}},
  %{"type" => "tool_result", "content" => "file1.txt file2.txt"}
]}

Basic Usage

# Extract text from assistant messages
content = ClaudeCodeSDK.ContentExtractor.extract_text(message)

# Check if message has text content
if ClaudeCodeSDK.ContentExtractor.has_text?(message) do
  IO.puts("Message contains: #{ClaudeCodeSDK.ContentExtractor.extract_text(message)}")
end

# Extract all text from a stream of messages
all_text = 
  stream
  |> Stream.filter(&ClaudeCodeSDK.ContentExtractor.has_text?/1)
  |> Stream.map(&ClaudeCodeSDK.ContentExtractor.extract_text/1)
  |> Enum.join("\n")

Summary

Functions

Extracts all text content from a stream of messages.

Extracts text from various content formats.

Extracts text content from a Claude message.

Checks if a message contains extractable text content.

Summarizes content from a message, truncating if too long.

Functions

extract_all_text(messages, separator \\ "\n")

@spec extract_all_text(Enumerable.t(), String.t()) :: String.t()

Extracts all text content from a stream of messages.

Convenience function to extract and concatenate text from multiple messages in a stream. Filters out messages without text content.

Parameters

  • messages - Stream or enumerable of ClaudeCodeSDK.Message structs
  • separator - String to join messages with (default: "\n")

Returns

  • String containing all extracted text, joined with separator

Examples

messages = [
  %ClaudeCodeSDK.Message{type: :assistant, data: %{message: %{"content" => "Hello"}}},
  %ClaudeCodeSDK.Message{type: :assistant, data: %{message: %{"content" => "World"}}},
  %ClaudeCodeSDK.Message{type: :system, data: %{}}  # No text content
]

ClaudeCodeSDK.ContentExtractor.extract_all_text(messages)
# => "Hello\nWorld"

ClaudeCodeSDK.ContentExtractor.extract_all_text(messages, " | ")
# => "Hello | World"

extract_content_text(content)

@spec extract_content_text(any()) :: String.t()

Extracts text from various content formats.

This function handles the actual content extraction from different Claude content formats, including simple strings, text block arrays, and mixed content with tool usage.

Parameters

  • content - Content in various formats (string, list, map, etc.)

Returns

  • String containing the extracted text

Examples

# Simple string
ClaudeCodeSDK.ContentExtractor.extract_content_text("Hello")
# => "Hello"

# Text blocks
ClaudeCodeSDK.ContentExtractor.extract_content_text([
  %{"type" => "text", "text" => "Hello"},
  %{"type" => "text", "text" => "World"}
])
# => "Hello World"

# Mixed content with tools
ClaudeCodeSDK.ContentExtractor.extract_content_text([
  %{"type" => "text", "text" => "Let me help:"},
  %{"type" => "tool_use", "name" => "calculator", "input" => %{}},
  %{"type" => "text", "text" => "Done!"}
])
# => "Let me help: [Tool: calculator] Done!"

extract_text(arg1)

@spec extract_text(ClaudeCodeSDK.Message.t() | map()) :: String.t() | nil

Extracts text content from a Claude message.

Handles various message types and content formats, returning the readable text portion of the message. Tool responses are represented as placeholders.

Parameters

Returns

  • String containing the extracted text content
  • nil if the message doesn't contain extractable text

Examples

# Simple text content
message = %ClaudeCodeSDK.Message{
  type: :assistant,
  data: %{message: %{"content" => "Hello, world!"}}
}
ClaudeCodeSDK.ContentExtractor.extract_text(message)
# => "Hello, world!"

# Array-based content with multiple text blocks
message = %ClaudeCodeSDK.Message{
  type: :assistant,
  data: %{message: %{"content" => [
    %{"type" => "text", "text" => "Here's the answer:"},
    %{"type" => "text", "text" => "42"}
  ]}}
}
ClaudeCodeSDK.ContentExtractor.extract_text(message)
# => "Here's the answer: 42"

# Content with tool usage
message = %ClaudeCodeSDK.Message{
  type: :assistant,
  data: %{message: %{"content" => [
    %{"type" => "text", "text" => "Let me check that file:"},
    %{"type" => "tool_use", "name" => "read_file", "input" => %{"path" => "data.txt"}},
    %{"type" => "text", "text" => "Done!"}
  ]}}
}
ClaudeCodeSDK.ContentExtractor.extract_text(message)
# => "Let me check that file: [Tool: read_file] Done!"

has_text?(message)

@spec has_text?(ClaudeCodeSDK.Message.t() | map()) :: boolean()

Checks if a message contains extractable text content.

Parameters

Returns

  • true if the message contains extractable text
  • false otherwise

Examples

message = %ClaudeCodeSDK.Message{
  type: :assistant, 
  data: %{message: %{"content" => "Hello"}}
}
ClaudeCodeSDK.ContentExtractor.has_text?(message)
# => true

message = %ClaudeCodeSDK.Message{
  type: :system, 
  data: %{session_id: "123"}
}
ClaudeCodeSDK.ContentExtractor.has_text?(message)
# => true (system messages can have extractable info)

message = %ClaudeCodeSDK.Message{
  type: :unknown, 
  data: %{}
}
ClaudeCodeSDK.ContentExtractor.has_text?(message)
# => false

summarize(message, max_length \\ 100)

@spec summarize(ClaudeCodeSDK.Message.t() | map(), pos_integer()) :: String.t()

Summarizes content from a message, truncating if too long.

Useful for logging or displaying preview text without overwhelming output.

Parameters

Returns

  • String containing truncated text with "..." if truncated, or full text if short enough

Examples

message = %ClaudeCodeSDK.Message{
  type: :assistant, 
  data: %{message: %{"content" => "This is a very long message that should be truncated"}}
}

ClaudeCodeSDK.ContentExtractor.summarize(message, 20)
# => "This is a very long..."