Truncate tool result if it exceeds max_chars. Keeps head + tail with a truncation notice in the middle. max_chars <= 0 disables truncation.
(self, content: str, max_chars: int)
| 1428 | return str(result) |
| 1429 | |
| 1430 | def _truncate_tool_result(self, content: str, max_chars: int) -> str: |
| 1431 | """Truncate tool result if it exceeds max_chars. |
| 1432 | |
| 1433 | Keeps head + tail with a truncation notice in the middle. |
| 1434 | max_chars <= 0 disables truncation. |
| 1435 | """ |
| 1436 | if max_chars <= 0 or len(content) <= max_chars: |
| 1437 | return content |
| 1438 | |
| 1439 | head = max_chars * 2 // 3 |
| 1440 | tail = max_chars // 6 |
| 1441 | omitted = len(content) - head - tail |
| 1442 | notice = ( |
| 1443 | f"\n\n--- TRUNCATED: {omitted:,} chars omitted " |
| 1444 | f"({len(content):,} total) ---\n\n" |
| 1445 | ) |
| 1446 | truncated = content[:head] + notice + content[-tail:] |
| 1447 | logger.info( |
| 1448 | f"Truncated tool result from {len(content):,} to {len(truncated):,} chars" |
| 1449 | ) |
| 1450 | return truncated |
| 1451 | |
| 1452 | def _add_assistant_message_with_tool_calls( |
| 1453 | self, tool_calls: list[Any], reasoning_content: str | None = None |
no outgoing calls