This commit is contained in:
2026-02-21 17:04:34 -05:00
parent 330c8604c9
commit a70ebd92a1
4 changed files with 59 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
# ai_client.py # ai_client.py
import tomllib import tomllib
import json import json
import datetime 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}" full_message = f"<context>\n{md_content}\n</context>\n\n{user_message}"
_append_comms("OUT", "request", { _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) response = _gemini_chat.send_message(full_message)
@@ -546,6 +546,21 @@ def _build_files_context_blocks(
return 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( def _run_anthropic_loop(
user_content: list[dict], user_content: list[dict],
user_message: str, user_message: str,
@@ -559,6 +574,7 @@ def _run_anthropic_loop(
""" """
global _anthropic_history global _anthropic_history
_strip_cache_controls(_anthropic_history)
_anthropic_history.append({"role": "user", "content": user_content}) _anthropic_history.append({"role": "user", "content": user_content})
_append_comms("OUT", "request", { _append_comms("OUT", "request", {
@@ -780,3 +796,4 @@ def send(
elif _provider == "anthropic": elif _provider == "anthropic":
return _send_anthropic(md_content, user_message, base_dir, file_items) return _send_anthropic(md_content, user_message, base_dir, file_items)
raise ValueError(f"unknown provider: {_provider}") raise ValueError(f"unknown provider: {_provider}")

View File

@@ -23,12 +23,10 @@ paths = []
[discussion] [discussion]
history = [ history = [
"[User]\nAdd support for properly handling files that we have in config with anthropics api (your context will have documentation reguarding it). This will allow us to free up the main markdown file were sending as a message.\nWe also want to make sure the messaging of that giant markdown is chunked as anthropic does have an upper limit per messages.", "User:\nAdd support for properly handling files that we have in config with anthropics api (your context will have documentation reguarding it). This will allow us to free up the main markdown file were sending as a message.\nWe also want to make sure the messaging of that giant markdown is chunked as anthropic does have an upper limit per messages.\nAI:\nGood. Now the big rewrite of `ai_client.py`. The strategy:\n\n- Add a new `_send_anthropic_files` path that uses `file_cache.get_file_id()` to upload files individually as `document`/`image` content blocks, keeping the context message lean\n- The fallback (and also for oversized content) uses chunked text blocks — split the markdown into ≤180k-char chunks (well under the ~200k token limit with headroom for overhead), each sent as a separate `text` content block with `cache_control` on the last one\n- `send()` now accepts an optional `file_items` parameter from aggregate\n- `reset_session()` also resets the file_cache client\nUser:\ncontinue. Also got this error when trying to run latest version of this program. However I can use an old version for now.\nAi:\nNow fix `gui.py` — patch the three 2-tuple unpack sites and thread `file_items` through to `send()`:\n[User]\ncontinue. My bad the error in comms was shown int he screenshot I linked. in the md context. Make sure to update MainContext.md when done.\nAI:\nfinished without sending a response...\nUser:\nSeems to be working...! \nMake a richer discussioon history editor, with structual support for the user/ai message nodes and buttons in the reponse and message panels for adding their entries to it on click.\nAI:\nThe existing format already uses `[User]\\n...` and `[AI]\\n...` prefixes. The new structured editor will parse those naturally and write them back in the same format. Now let me implement the full changes:\nUser:\nYou had failres in your last few called scripts. Git diff shows now changes to python files.\nI'll add the comms and tool logs to your context. \nWe should remove the script content from the tool log and just save the script name so that it doesn't bloat it. \nSame with omitting full message sent in the comms log, very expensive to send to you or read manually.\nVendor API:\nERROR: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'A maximum of 4 blocks with cache_control may be provided. Found 5.'}, 'request_id': 'req_011CYMxgMXvcZUVFNyFz1Lx8'}",
"[AI]\nGood. Now the big rewrite of `ai_client.py`. The strategy:\n\n- Add a new `_send_anthropic_files` path that uses `file_cache.get_file_id()` to upload files individually as `document`/`image` content blocks, keeping the context message lean\n- The fallback (and also for oversized content) uses chunked text blocks — split the markdown into ≤180k-char chunks (well under the ~200k token limit with headroom for overhead), each sent as a separate `text` content block with `cache_control` on the last one\n- `send()` now accepts an optional `file_items` parameter from aggregate\n- `reset_session()` also resets the file_cache client", "[User]:\nI'm not able to send a message for a while now...\n(Message finally sent)",
"[User]\ncontinue. Also got this error when trying to run latest version of this program. However I can use an old version for now.", "[AI]\nCompleted without a response message showing up.",
"[Ai]\nNow fix `gui.py` — patch the three 2-tuple unpack sites and thread `file_items` through to `send()`:", "[User]\nRestarts custom prompting program.",
"[User]\ncontinue. My bad the error in comms was shown int he screenshot I linked. in the md context. Make sure to update MainContext.md when done.",
"[AI]\nfinished without sending a repose...",
] ]
[ai] [ai]

View File

@@ -107,7 +107,7 @@ DockId=0x0000001F,0
[Window][###106] [Window][###106]
Pos=551,0 Pos=551,0
Size=1544,2137 Size=1275,2137
Collapsed=0 Collapsed=0
DockId=0x00000012,2 DockId=0x00000012,2
@@ -174,7 +174,7 @@ DockId=0x00000016,0
[Window][###112] [Window][###112]
Pos=551,0 Pos=551,0
Size=1544,2137 Size=1275,2137
Collapsed=0 Collapsed=0
DockId=0x00000012,1 DockId=0x00000012,1
@@ -321,26 +321,26 @@ Collapsed=0
DockId=0x00000026,0 DockId=0x00000026,0
[Window][###82] [Window][###82]
Pos=2097,0 Pos=1828,0
Size=1743,1162 Size=2012,1162
Collapsed=0 Collapsed=0
DockId=0x0000001F,0 DockId=0x0000001F,0
[Window][###89] [Window][###89]
Pos=551,0 Pos=551,0
Size=1544,2137 Size=1275,2137
Collapsed=0 Collapsed=0
DockId=0x00000012,0 DockId=0x00000012,0
[Window][###97] [Window][###97]
Pos=2097,1649 Pos=1828,1649
Size=1743,488 Size=2012,488
Collapsed=0 Collapsed=0
DockId=0x00000028,0 DockId=0x00000028,0
[Window][###104] [Window][###104]
Pos=2097,1164 Pos=1828,1164
Size=1743,483 Size=2012,483
Collapsed=0 Collapsed=0
DockId=0x00000027,0 DockId=0x00000027,0
@@ -404,6 +404,31 @@ Pos=1578,868
Size=700,440 Size=700,440
Collapsed=0 Collapsed=0
[Window][###139]
Pos=1578,868
Size=700,440
Collapsed=0
[Window][###241]
Pos=1578,868
Size=700,440
Collapsed=0
[Window][###346]
Pos=1578,868
Size=700,440
Collapsed=0
[Window][###457]
Pos=1578,868
Size=700,440
Collapsed=0
[Window][###577]
Pos=1578,868
Size=700,440
Collapsed=0
[Docking][Data] [Docking][Data]
DockSpace ID=0x7C6B3D9B Window=0xA87D555D Pos=0,0 Size=3840,2137 Split=X Selected=0x40484D8F DockSpace ID=0x7C6B3D9B Window=0xA87D555D Pos=0,0 Size=3840,2137 Split=X Selected=0x40484D8F
DockNode ID=0x00000003 Parent=0x7C6B3D9B SizeRef=549,1161 Split=Y Selected=0xEE087978 DockNode ID=0x00000003 Parent=0x7C6B3D9B SizeRef=549,1161 Split=Y Selected=0xEE087978

View File

@@ -123,3 +123,4 @@ def log_tool_call(script: str, result: str, script_path: str | None):
pass pass
return str(ps1_path) if ps1_path else None return str(ps1_path) if ps1_path else None