fixes
This commit is contained in:
23
ai_client.py
23
ai_client.py
@@ -1,4 +1,4 @@
|
||||
# ai_client.py
|
||||
# ai_client.py
|
||||
import tomllib
|
||||
import json
|
||||
import datetime
|
||||
@@ -318,7 +318,7 @@ def _send_gemini(md_content: str, user_message: str, base_dir: str) -> str:
|
||||
full_message = f"<context>\n{md_content}\n</context>\n\n{user_message}"
|
||||
|
||||
_append_comms("OUT", "request", {
|
||||
"message": full_message,
|
||||
"summary": f"[Gemini: context {len(md_content)} chars + user message {len(user_message)} chars]",
|
||||
})
|
||||
|
||||
response = _gemini_chat.send_message(full_message)
|
||||
@@ -546,6 +546,21 @@ def _build_files_context_blocks(
|
||||
return blocks
|
||||
|
||||
|
||||
|
||||
def _strip_cache_controls(history: list[dict]):
|
||||
"""
|
||||
Remove cache_control from all content blocks in the message history.
|
||||
Anthropic allows a maximum of 4 cache_control blocks total across
|
||||
system + tools + messages. We reserve those slots for the stable
|
||||
system/tools prefix and the current turn's context block, so all
|
||||
older history entries must be clean.
|
||||
"""
|
||||
for msg in history:
|
||||
content = msg.get("content")
|
||||
if isinstance(content, list):
|
||||
for block in content:
|
||||
if isinstance(block, dict):
|
||||
block.pop("cache_control", None)
|
||||
def _run_anthropic_loop(
|
||||
user_content: list[dict],
|
||||
user_message: str,
|
||||
@@ -559,6 +574,7 @@ def _run_anthropic_loop(
|
||||
"""
|
||||
global _anthropic_history
|
||||
|
||||
_strip_cache_controls(_anthropic_history)
|
||||
_anthropic_history.append({"role": "user", "content": user_content})
|
||||
|
||||
_append_comms("OUT", "request", {
|
||||
@@ -779,4 +795,5 @@ def send(
|
||||
return _send_gemini(md_content, user_message, base_dir)
|
||||
elif _provider == "anthropic":
|
||||
return _send_anthropic(md_content, user_message, base_dir, file_items)
|
||||
raise ValueError(f"unknown provider: {_provider}")
|
||||
raise ValueError(f"unknown provider: {_provider}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user