Skip to content

Commit 074f27e

Browse files
fix: restore original cache control as it can be used complementary to new auto strategy
1 parent 7f8a759 commit 074f27e

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/askui/models/shared/tools.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from askui.models.shared.agent_message_param import (
2222
Base64ImageSourceParam,
23+
CacheControlEphemeralParam,
2324
ContentBlockParam,
2425
ImageBlockParam,
2526
TextBlockParam,
@@ -394,7 +395,12 @@ def to_params(self) -> list[ToolParam]:
394395
for tool_name, tool in tool_map.items()
395396
if self._include is None or tool_name in self._include
396397
}
397-
return list(filtered_tool_map.values())
398+
result = list(filtered_tool_map.values())
399+
if result:
400+
result[-1]["cache_control"] = CacheControlEphemeralParam(
401+
type="ephemeral",
402+
)
403+
return result
398404

399405
def _get_mcp_tool_params(self) -> dict[str, ToolParam]:
400406
if not self._mcp_client:

src/askui/models/shared/truncation_strategies.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from typing_extensions import override
66

77
from askui.models.shared.agent_message_param import (
8+
CacheControlEphemeralParam,
89
MessageParam,
10+
TextBlockParam,
911
ToolParam,
1012
)
1113
from askui.models.shared.prompts import ActSystemPrompt
@@ -173,8 +175,37 @@ def _should_truncate(self) -> bool:
173175
@property
174176
@override
175177
def messages(self) -> list[MessageParam]:
178+
self._move_cache_control_to_last_non_tool_result_user_message()
176179
return self._messages
177180

181+
def _move_cache_control_to_last_non_tool_result_user_message(self) -> None:
182+
found_last = False
183+
for message in reversed(self._messages):
184+
if message.role == "user" and not _is_tool_result_user_message(message):
185+
if not found_last:
186+
found_last = True
187+
if isinstance(message.content, str):
188+
message.content = [
189+
TextBlockParam(
190+
text=message.content,
191+
cache_control=CacheControlEphemeralParam(
192+
type="ephemeral",
193+
),
194+
)
195+
]
196+
elif len(message.content) > 0:
197+
last_content = message.content[-1]
198+
if hasattr(last_content, "cache_control"):
199+
last_content.cache_control = CacheControlEphemeralParam(
200+
type="ephemeral",
201+
)
202+
else:
203+
if isinstance(message.content, list) and message.content:
204+
last_content = message.content[-1]
205+
if hasattr(last_content, "cache_control"):
206+
last_content.cache_control = None
207+
break
208+
178209
def _truncate(self) -> None: # noqa: C901
179210
messages_to_remove_min = min(
180211
len(self._messages) - self._max_messages_after_truncation, 0

0 commit comments

Comments
 (0)