ccproxy.claude_sdk¶
ccproxy.claude_sdk
¶
Claude SDK integration module.
ClaudeSDKClient
¶
Minimal Claude SDK client wrapper that handles core SDK interactions.
This class provides a clean interface to the Claude Code SDK while handling error translation and basic query execution. Supports both stateless query() calls and pooled connection reuse for improved performance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
Settings | None
|
Application settings for session pool configuration |
None
|
session_manager
|
SessionManager | None
|
Optional SessionManager instance for dependency injection |
None
|
Source code in ccproxy/claude_sdk/client.py
query_completion
async
¶
Execute a query using the Claude Code SDK and return a StreamHandle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
SDKMessage
|
SDKMessage to send to Claude SDK |
required |
options
|
ClaudeCodeOptions
|
Claude Code options configuration |
required |
request_id
|
str | None
|
Optional request ID for correlation |
None
|
session_id
|
str | None
|
Optional session ID for conversation continuity |
None
|
Returns:
Type | Description |
---|---|
StreamHandle
|
StreamHandle that can create listeners for the stream |
Raises:
Type | Description |
---|---|
ClaudeSDKError
|
If the query fails |
Source code in ccproxy/claude_sdk/client.py
validate_health
async
¶
Validate that the Claude SDK is healthy.
Returns:
Type | Description |
---|---|
bool
|
True if healthy, False otherwise |
Source code in ccproxy/claude_sdk/client.py
interrupt_session
async
¶
Interrupt a specific session due to client disconnection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_id
|
str
|
The session ID to interrupt |
required |
Returns:
Type | Description |
---|---|
bool
|
True if session was found and interrupted, False otherwise |
Source code in ccproxy/claude_sdk/client.py
MessageConverter
¶
Handles conversion between Anthropic API format and Claude SDK format.
format_messages_to_prompt
staticmethod
¶
Convert Anthropic messages format to a single prompt string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
list[dict[str, Any]]
|
List of messages in Anthropic format |
required |
Returns:
Type | Description |
---|---|
str
|
Single prompt string formatted for Claude SDK |
Source code in ccproxy/claude_sdk/converter.py
convert_to_anthropic_response
staticmethod
¶
convert_to_anthropic_response(
assistant_message,
result_message,
model,
mode=FORWARD,
pretty_format=True,
)
Convert Claude SDK messages to Anthropic API response format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
assistant_message
|
AssistantMessage
|
The assistant message from Claude SDK |
required |
result_message
|
ResultMessage
|
The result message from Claude SDK |
required |
model
|
str
|
The model name used |
required |
mode
|
SDKMessageMode
|
System message handling mode (forward, ignore, formatted) |
FORWARD
|
pretty_format
|
bool
|
Whether to use pretty formatting (true: indented JSON with newlines, false: compact with escaped content) |
True
|
Returns:
Type | Description |
---|---|
MessageResponse
|
Response in Anthropic API format |
Source code in ccproxy/claude_sdk/converter.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
create_streaming_start_chunks
staticmethod
¶
Create the initial streaming chunks for Anthropic API format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message_id
|
str
|
The message ID |
required |
model
|
str
|
The model name |
required |
input_tokens
|
int
|
Number of input tokens for the request |
0
|
Returns:
Type | Description |
---|---|
list[tuple[str, dict[str, Any]]]
|
List of tuples (event_type, chunk) for initial streaming chunks |
Source code in ccproxy/claude_sdk/converter.py
create_streaming_delta_chunk
staticmethod
¶
Create a streaming delta chunk for Anthropic API format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text content to include |
required |
Returns:
Type | Description |
---|---|
tuple[str, dict[str, Any]]
|
Tuple of (event_type, chunk) |
Source code in ccproxy/claude_sdk/converter.py
create_streaming_end_chunks
staticmethod
¶
Create the final streaming chunks for Anthropic API format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stop_reason
|
str
|
The reason for stopping |
'end_turn'
|
stop_sequence
|
str | None
|
The stop sequence used (if any) |
None
|
Returns:
Type | Description |
---|---|
list[tuple[str, dict[str, Any]]]
|
List of tuples (event_type, chunk) for final streaming chunks |
Source code in ccproxy/claude_sdk/converter.py
StreamTimeoutError
¶
Bases: ClaudeSDKError
Stream timeout error when no SDK message is received within timeout.
Source code in ccproxy/claude_sdk/exceptions.py
OptionsHandler
¶
Handles creation and management of Claude SDK options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
Settings | None
|
Application settings containing default Claude options |
None
|
Source code in ccproxy/claude_sdk/options.py
create_options
¶
create_options(
model,
temperature=None,
max_tokens=None,
system_message=None,
**additional_options,
)
Create Claude SDK options from API parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
The model name |
required |
temperature
|
float | None
|
Temperature for response generation |
None
|
max_tokens
|
int | None
|
Maximum tokens in response |
None
|
system_message
|
str | None
|
System message to include |
None
|
**additional_options
|
Any
|
Additional options to set on the ClaudeCodeOptions instance |
{}
|
Returns:
Type | Description |
---|---|
ClaudeCodeOptions
|
Configured ClaudeCodeOptions instance |
Source code in ccproxy/claude_sdk/options.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
extract_system_message
staticmethod
¶
Extract system message from Anthropic messages format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
list[dict[str, Any]]
|
List of messages in Anthropic format |
required |
Returns:
Type | Description |
---|---|
str | None
|
System message content if found, None otherwise |
Source code in ccproxy/claude_sdk/options.py
get_supported_models
staticmethod
¶
Get list of supported Claude models.
Returns:
Type | Description |
---|---|
list[str]
|
List of supported model names |
Source code in ccproxy/claude_sdk/options.py
validate_model
staticmethod
¶
parse_formatted_sdk_content
¶
Parse XML-formatted SDK content from text blocks.
This is the main parsing function that handles all types of XML-formatted SDK content by applying the appropriate parsing functions in sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
Text content that may contain XML-formatted SDK data |
required |
collect_tool_calls
|
bool
|
Whether to collect tool calls for OpenAI format conversion (used by OpenAI adapter, not by streaming processor) |
False
|
Returns:
Type | Description |
---|---|
str
|
Tuple of (cleaned_text, tool_calls_list) |
list[Any]
|
|
tuple[str, list[Any]]
|
|