|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
| 6 | +import os |
6 | 7 | import re |
7 | 8 | from typing import Any, Dict, List, Mapping, Optional, Union |
8 | 9 | from uuid import uuid4 |
|
25 | 26 |
|
26 | 27 | logger = get_logger() |
27 | 28 |
|
| 29 | +ANTHROPIC_SYSTEM_PROMPT_DYNAMIC_BOUNDARY = "__SYSTEM_PROMPT_DYNAMIC_BOUNDARY__" |
| 30 | + |
28 | 31 |
|
29 | 32 | def _safe_int(value: object) -> int: |
30 | 33 | """Best-effort int conversion for usage counters.""" |
@@ -473,18 +476,26 @@ def build_full_system_prompt( |
473 | 476 | context: Dict[str, str], |
474 | 477 | tool_mode: str, |
475 | 478 | tools: List[Tool[Any, Any]], |
| 479 | + *, |
| 480 | + include_anthropic_cache_boundary: bool = False, |
476 | 481 | ) -> str: |
477 | 482 | """Compose the final system prompt including context and tool hints.""" |
478 | | - full_prompt = system_prompt |
| 483 | + dynamic_segments: List[str] = [] |
479 | 484 | if context: |
480 | 485 | context_reminder = format_context_as_system_reminder(context) |
481 | 486 | if context_reminder: |
482 | | - full_prompt = f"{system_prompt}\n\n{context_reminder}" |
| 487 | + dynamic_segments.append(context_reminder) |
483 | 488 | if tool_mode == "text": |
484 | 489 | tool_hint = _tool_prompt_for_text_mode(tools) |
485 | 490 | if tool_hint: |
486 | | - full_prompt = f"{full_prompt}\n\n{tool_hint}" |
487 | | - return full_prompt |
| 491 | + dynamic_segments.append(tool_hint) |
| 492 | + if include_anthropic_cache_boundary and dynamic_segments: |
| 493 | + return "\n\n".join( |
| 494 | + [system_prompt, ANTHROPIC_SYSTEM_PROMPT_DYNAMIC_BOUNDARY, *dynamic_segments] |
| 495 | + ) |
| 496 | + if dynamic_segments: |
| 497 | + return "\n\n".join([system_prompt, *dynamic_segments]) |
| 498 | + return system_prompt |
488 | 499 |
|
489 | 500 |
|
490 | 501 | def log_openai_messages(normalized_messages: List[Dict[str, Any]]) -> None: |
@@ -523,6 +534,121 @@ async def build_anthropic_tool_schemas(tools: List[Tool[Any, Any]]) -> List[Dict |
523 | 534 | return schemas |
524 | 535 |
|
525 | 536 |
|
| 537 | +def anthropic_prompt_caching_enabled() -> bool: |
| 538 | + """Return whether Anthropic prompt caching should be enabled for request shaping.""" |
| 539 | + return not ( |
| 540 | + os.getenv("RIPPERDOC_DISABLE_PROMPT_CACHING") |
| 541 | + or os.getenv("DISABLE_PROMPT_CACHING") |
| 542 | + ) |
| 543 | + |
| 544 | + |
| 545 | +def anthropic_cache_control() -> Dict[str, Any]: |
| 546 | + """Default Anthropic cache control payload matching Claude Code's ephemeral strategy.""" |
| 547 | + ttl = (os.getenv("RIPPERDOC_PROMPT_CACHE_TTL") or "").strip() |
| 548 | + payload: Dict[str, Any] = {"type": "ephemeral"} |
| 549 | + if ttl == "1h": |
| 550 | + payload["ttl"] = ttl |
| 551 | + return payload |
| 552 | + |
| 553 | + |
| 554 | +def build_anthropic_system_blocks( |
| 555 | + system_prompt: str, *, enable_prompt_caching: bool |
| 556 | +) -> str | List[Dict[str, Any]]: |
| 557 | + """Render Anthropic system blocks with optional cache-aware segmentation.""" |
| 558 | + text = (system_prompt or "").strip() |
| 559 | + if not text or not enable_prompt_caching: |
| 560 | + return text |
| 561 | + |
| 562 | + if ANTHROPIC_SYSTEM_PROMPT_DYNAMIC_BOUNDARY in text: |
| 563 | + prefix, suffix = text.split(ANTHROPIC_SYSTEM_PROMPT_DYNAMIC_BOUNDARY, 1) |
| 564 | + blocks: List[Dict[str, Any]] = [] |
| 565 | + prefix = prefix.strip() |
| 566 | + suffix = suffix.strip() |
| 567 | + if prefix: |
| 568 | + blocks.append( |
| 569 | + { |
| 570 | + "type": "text", |
| 571 | + "text": prefix, |
| 572 | + "cache_control": anthropic_cache_control(), |
| 573 | + } |
| 574 | + ) |
| 575 | + if suffix: |
| 576 | + blocks.append({"type": "text", "text": suffix}) |
| 577 | + return blocks |
| 578 | + |
| 579 | + return [ |
| 580 | + { |
| 581 | + "type": "text", |
| 582 | + "text": text, |
| 583 | + "cache_control": anthropic_cache_control(), |
| 584 | + } |
| 585 | + ] |
| 586 | + |
| 587 | + |
| 588 | +def apply_anthropic_prompt_cache_control_to_tool_schemas( |
| 589 | + tool_schemas: List[Dict[str, Any]], *, enable_prompt_caching: bool |
| 590 | +) -> List[Dict[str, Any]]: |
| 591 | + """Add Anthropic cache markers to tool definitions.""" |
| 592 | + if not enable_prompt_caching or not tool_schemas: |
| 593 | + return list(tool_schemas) |
| 594 | + cache_control = anthropic_cache_control() |
| 595 | + return [{**schema, "cache_control": dict(cache_control)} for schema in tool_schemas] |
| 596 | + |
| 597 | + |
| 598 | +def apply_anthropic_prompt_cache_control_to_messages( |
| 599 | + messages: List[Dict[str, Any]], |
| 600 | + *, |
| 601 | + enable_prompt_caching: bool, |
| 602 | + recent_messages: int = 2, |
| 603 | +) -> List[Dict[str, Any]]: |
| 604 | + """Attach cache markers to the tail of the Anthropic transcript.""" |
| 605 | + if not enable_prompt_caching or not messages: |
| 606 | + return list(messages) |
| 607 | + |
| 608 | + cache_control = anthropic_cache_control() |
| 609 | + start_index = max(0, len(messages) - max(recent_messages, 1)) |
| 610 | + shaped_messages: List[Dict[str, Any]] = [] |
| 611 | + |
| 612 | + for index, message in enumerate(messages): |
| 613 | + shaped_message = dict(message) |
| 614 | + content = message.get("content") |
| 615 | + if index < start_index: |
| 616 | + shaped_messages.append(shaped_message) |
| 617 | + continue |
| 618 | + |
| 619 | + if isinstance(content, str): |
| 620 | + shaped_message["content"] = [ |
| 621 | + { |
| 622 | + "type": "text", |
| 623 | + "text": content, |
| 624 | + "cache_control": dict(cache_control), |
| 625 | + } |
| 626 | + ] |
| 627 | + shaped_messages.append(shaped_message) |
| 628 | + continue |
| 629 | + |
| 630 | + if not isinstance(content, list): |
| 631 | + shaped_messages.append(shaped_message) |
| 632 | + continue |
| 633 | + |
| 634 | + copied_content = [dict(item) if isinstance(item, dict) else item for item in content] |
| 635 | + for content_index in range(len(copied_content) - 1, -1, -1): |
| 636 | + item = copied_content[content_index] |
| 637 | + if not isinstance(item, dict): |
| 638 | + continue |
| 639 | + if item.get("type") in {"thinking", "redacted_thinking"}: |
| 640 | + continue |
| 641 | + copied_content[content_index] = { |
| 642 | + **item, |
| 643 | + "cache_control": dict(cache_control), |
| 644 | + } |
| 645 | + break |
| 646 | + shaped_message["content"] = copied_content |
| 647 | + shaped_messages.append(shaped_message) |
| 648 | + |
| 649 | + return shaped_messages |
| 650 | + |
| 651 | + |
526 | 652 | async def build_openai_tool_schemas(tools: List[Tool[Any, Any]]) -> List[Dict[str, Any]]: |
527 | 653 | """Render tool schemas in OpenAI function-calling format.""" |
528 | 654 | openai_tools = [] |
|
0 commit comments