mirror of
https://github.com/anthropics/claude-code-sdk-python.git
synced 2025-07-07 22:55:01 +00:00
Merge pull request #17 from anthropics/fix-optional-cost-fields
Fix: Remove unused cost_usd field
This commit is contained in:
commit
9b1387a5ff
6 changed files with 13 additions and 20 deletions
|
@ -47,11 +47,9 @@ class InternalClient:
|
||||||
|
|
||||||
match data["type"]:
|
match data["type"]:
|
||||||
case "user":
|
case "user":
|
||||||
# Extract just the content from the nested structure
|
|
||||||
return UserMessage(content=data["message"]["content"])
|
return UserMessage(content=data["message"]["content"])
|
||||||
|
|
||||||
case "assistant":
|
case "assistant":
|
||||||
# Parse content blocks
|
|
||||||
content_blocks: list[ContentBlock] = []
|
content_blocks: list[ContentBlock] = []
|
||||||
for block in data["message"]["content"]:
|
for block in data["message"]["content"]:
|
||||||
match block["type"]:
|
match block["type"]:
|
||||||
|
@ -79,20 +77,18 @@ class InternalClient:
|
||||||
case "system":
|
case "system":
|
||||||
return SystemMessage(
|
return SystemMessage(
|
||||||
subtype=data["subtype"],
|
subtype=data["subtype"],
|
||||||
data=data, # Pass through all data
|
data=data,
|
||||||
)
|
)
|
||||||
|
|
||||||
case "result":
|
case "result":
|
||||||
# Map total_cost to total_cost_usd for consistency
|
|
||||||
return ResultMessage(
|
return ResultMessage(
|
||||||
subtype=data["subtype"],
|
subtype=data["subtype"],
|
||||||
cost_usd=data["cost_usd"],
|
|
||||||
duration_ms=data["duration_ms"],
|
duration_ms=data["duration_ms"],
|
||||||
duration_api_ms=data["duration_api_ms"],
|
duration_api_ms=data["duration_api_ms"],
|
||||||
is_error=data["is_error"],
|
is_error=data["is_error"],
|
||||||
num_turns=data["num_turns"],
|
num_turns=data["num_turns"],
|
||||||
session_id=data["session_id"],
|
session_id=data["session_id"],
|
||||||
total_cost_usd=data["total_cost"],
|
total_cost_usd=data.get("total_cost_usd"),
|
||||||
usage=data.get("usage"),
|
usage=data.get("usage"),
|
||||||
result=data.get("result"),
|
result=data.get("result"),
|
||||||
)
|
)
|
||||||
|
|
|
@ -190,7 +190,11 @@ class SubprocessCLITransport(Transport):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads(line_str)
|
data = json.loads(line_str)
|
||||||
yield data
|
try:
|
||||||
|
yield data
|
||||||
|
except GeneratorExit:
|
||||||
|
# Handle generator cleanup gracefully
|
||||||
|
return
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
if line_str.startswith("{") or line_str.startswith("["):
|
if line_str.startswith("{") or line_str.startswith("["):
|
||||||
raise SDKJSONDecodeError(line_str, e) from e
|
raise SDKJSONDecodeError(line_str, e) from e
|
||||||
|
@ -198,8 +202,6 @@ class SubprocessCLITransport(Transport):
|
||||||
|
|
||||||
except anyio.ClosedResourceError:
|
except anyio.ClosedResourceError:
|
||||||
pass
|
pass
|
||||||
finally:
|
|
||||||
tg.cancel_scope.cancel()
|
|
||||||
|
|
||||||
await self._process.wait()
|
await self._process.wait()
|
||||||
if self._process.returncode is not None and self._process.returncode != 0:
|
if self._process.returncode is not None and self._process.returncode != 0:
|
||||||
|
|
|
@ -75,13 +75,12 @@ class ResultMessage:
|
||||||
"""Result message with cost and usage information."""
|
"""Result message with cost and usage information."""
|
||||||
|
|
||||||
subtype: str
|
subtype: str
|
||||||
cost_usd: float
|
|
||||||
duration_ms: int
|
duration_ms: int
|
||||||
duration_api_ms: int
|
duration_api_ms: int
|
||||||
is_error: bool
|
is_error: bool
|
||||||
num_turns: int
|
num_turns: int
|
||||||
session_id: str
|
session_id: str
|
||||||
total_cost_usd: float
|
total_cost_usd: float | None = None
|
||||||
usage: dict[str, Any] | None = None
|
usage: dict[str, Any] | None = None
|
||||||
result: str | None = None
|
result: str | None = None
|
||||||
|
|
||||||
|
|
|
@ -88,13 +88,12 @@ class TestQueryFunction:
|
||||||
yield {
|
yield {
|
||||||
"type": "result",
|
"type": "result",
|
||||||
"subtype": "success",
|
"subtype": "success",
|
||||||
"cost_usd": 0.001,
|
|
||||||
"duration_ms": 1000,
|
"duration_ms": 1000,
|
||||||
"duration_api_ms": 800,
|
"duration_api_ms": 800,
|
||||||
"is_error": False,
|
"is_error": False,
|
||||||
"num_turns": 1,
|
"num_turns": 1,
|
||||||
"session_id": "test-session",
|
"session_id": "test-session",
|
||||||
"total_cost": 0.001,
|
"total_cost_usd": 0.001,
|
||||||
}
|
}
|
||||||
|
|
||||||
mock_transport.receive_messages = mock_receive
|
mock_transport.receive_messages = mock_receive
|
||||||
|
|
|
@ -43,13 +43,12 @@ class TestIntegration:
|
||||||
yield {
|
yield {
|
||||||
"type": "result",
|
"type": "result",
|
||||||
"subtype": "success",
|
"subtype": "success",
|
||||||
"cost_usd": 0.001,
|
|
||||||
"duration_ms": 1000,
|
"duration_ms": 1000,
|
||||||
"duration_api_ms": 800,
|
"duration_api_ms": 800,
|
||||||
"is_error": False,
|
"is_error": False,
|
||||||
"num_turns": 1,
|
"num_turns": 1,
|
||||||
"session_id": "test-session",
|
"session_id": "test-session",
|
||||||
"total_cost": 0.001,
|
"total_cost_usd": 0.001,
|
||||||
}
|
}
|
||||||
|
|
||||||
mock_transport.receive_messages = mock_receive
|
mock_transport.receive_messages = mock_receive
|
||||||
|
@ -71,7 +70,7 @@ class TestIntegration:
|
||||||
|
|
||||||
# Check result message
|
# Check result message
|
||||||
assert isinstance(messages[1], ResultMessage)
|
assert isinstance(messages[1], ResultMessage)
|
||||||
assert messages[1].cost_usd == 0.001
|
assert messages[1].total_cost_usd == 0.001
|
||||||
assert messages[1].session_id == "test-session"
|
assert messages[1].session_id == "test-session"
|
||||||
|
|
||||||
anyio.run(_test)
|
anyio.run(_test)
|
||||||
|
@ -109,13 +108,12 @@ class TestIntegration:
|
||||||
yield {
|
yield {
|
||||||
"type": "result",
|
"type": "result",
|
||||||
"subtype": "success",
|
"subtype": "success",
|
||||||
"cost_usd": 0.002,
|
|
||||||
"duration_ms": 1500,
|
"duration_ms": 1500,
|
||||||
"duration_api_ms": 1200,
|
"duration_api_ms": 1200,
|
||||||
"is_error": False,
|
"is_error": False,
|
||||||
"num_turns": 1,
|
"num_turns": 1,
|
||||||
"session_id": "test-session-2",
|
"session_id": "test-session-2",
|
||||||
"total_cost": 0.002,
|
"total_cost_usd": 0.002,
|
||||||
}
|
}
|
||||||
|
|
||||||
mock_transport.receive_messages = mock_receive
|
mock_transport.receive_messages = mock_receive
|
||||||
|
|
|
@ -45,7 +45,6 @@ class TestMessageTypes:
|
||||||
"""Test creating a ResultMessage."""
|
"""Test creating a ResultMessage."""
|
||||||
msg = ResultMessage(
|
msg = ResultMessage(
|
||||||
subtype="success",
|
subtype="success",
|
||||||
cost_usd=0.01,
|
|
||||||
duration_ms=1500,
|
duration_ms=1500,
|
||||||
duration_api_ms=1200,
|
duration_api_ms=1200,
|
||||||
is_error=False,
|
is_error=False,
|
||||||
|
@ -54,7 +53,7 @@ class TestMessageTypes:
|
||||||
total_cost_usd=0.01,
|
total_cost_usd=0.01,
|
||||||
)
|
)
|
||||||
assert msg.subtype == "success"
|
assert msg.subtype == "success"
|
||||||
assert msg.cost_usd == 0.01
|
assert msg.total_cost_usd == 0.01
|
||||||
assert msg.session_id == "session-123"
|
assert msg.session_id == "session-123"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue