style: run black formatter

This commit is contained in:
Vigilans 2026-04-21 22:36:06 +08:00
parent 21b9c64f19
commit 2ed5f90d8f
3 changed files with 39 additions and 29 deletions

View file

@ -332,8 +332,12 @@ class AnthropicResponsesStreamWrapper:
usage_delta["cache_read_input_tokens"] = cache_read_tokens
if self._web_tool_uses:
usage_delta["server_tool_use"] = {
"web_search_requests": sum(c["name"] == "web_search" for c in self._web_tool_uses),
"web_fetch_requests": sum(c["name"] == "web_fetch" for c in self._web_tool_uses),
"web_search_requests": sum(
c["name"] == "web_search" for c in self._web_tool_uses
),
"web_fetch_requests": sum(
c["name"] == "web_fetch" for c in self._web_tool_uses
),
}
self._chunk_queue.append(
@ -369,9 +373,7 @@ class AnthropicResponsesStreamWrapper:
},
}
)
self._chunk_queue.append(
{"type": "content_block_stop", "index": block_idx}
)
self._chunk_queue.append({"type": "content_block_stop", "index": block_idx})
def _emit_web_search_results(self, annotations: list) -> list:
"""Emit web_search_tool_result blocks and return citations for text emission."""
@ -387,9 +389,7 @@ class AnthropicResponsesStreamWrapper:
"content_block": block,
}
)
self._chunk_queue.append(
{"type": "content_block_stop", "index": block_idx}
)
self._chunk_queue.append({"type": "content_block_stop", "index": block_idx})
return citations
def _emit_cited_text_blocks(self, text: str, citations: list) -> None:
@ -418,9 +418,7 @@ class AnthropicResponsesStreamWrapper:
"delta": {"type": "citations_delta", "citation": cit},
}
)
self._chunk_queue.append(
{"type": "content_block_stop", "index": block_idx}
)
self._chunk_queue.append({"type": "content_block_stop", "index": block_idx})
def __aiter__(self) -> "AnthropicResponsesStreamWrapper":
return self

View file

@ -464,7 +464,9 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
)
content.extend(blocks)
content.extend(
build_text_blocks_with_citations(getattr(part, "text", ""), citations)
build_text_blocks_with_citations(
getattr(part, "text", ""), citations
)
)
break
@ -501,12 +503,16 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
elif item_type == "message" and web_tool_uses:
for part in item.get("content", []):
if isinstance(part, dict) and part.get("type") == "output_text":
blocks, citations = build_web_search_results_from_annotations(
web_tool_uses, part.get("annotations") or []
blocks, citations = (
build_web_search_results_from_annotations(
web_tool_uses, part.get("annotations") or []
)
)
content.extend(blocks)
content.extend(
build_text_blocks_with_citations(part.get("text", ""), citations)
build_text_blocks_with_citations(
part.get("text", ""), citations
)
)
break
elif item_type == "message":
@ -548,8 +554,12 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
if web_tool_uses:
anthropic_usage["server_tool_use"] = { # type: ignore[typeddict-unknown-key]
"web_search_requests": sum(c["name"] == "web_search" for c in web_tool_uses),
"web_fetch_requests": sum(c["name"] == "web_fetch" for c in web_tool_uses),
"web_search_requests": sum(
c["name"] == "web_search" for c in web_tool_uses
),
"web_fetch_requests": sum(
c["name"] == "web_fetch" for c in web_tool_uses
),
}
return AnthropicMessagesResponse(

View file

@ -64,12 +64,18 @@ def build_web_search_results_from_annotations(
start = ann.get("start_index", 0) or 0
end = ann.get("end_index", 0) or 0
citations.append((start, end, {
"type": "web_search_result_location",
"url": url,
"title": title,
"cited_text": title,
}))
citations.append(
(
start,
end,
{
"type": "web_search_result_location",
"url": url,
"title": title,
"cited_text": title,
},
)
)
if url and url not in seen_urls:
seen_urls[url] = {
@ -105,9 +111,7 @@ def build_text_blocks_with_citations(
text[start:end] is the cited range; everything else is uncited.
"""
if not citations:
return [
AnthropicResponseContentBlockText(type="text", text=text).model_dump()
]
return [AnthropicResponseContentBlockText(type="text", text=text).model_dump()]
blocks: List[Dict[str, Any]] = []
pos = 0
@ -129,9 +133,7 @@ def build_text_blocks_with_citations(
if pos < len(text):
blocks.append(
AnthropicResponseContentBlockText(
type="text", text=text[pos:]
).model_dump()
AnthropicResponseContentBlockText(type="text", text=text[pos:]).model_dump()
)
return blocks