-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
96 lines (74 loc) · 2.96 KB
/
Copy pathpreprocessing.py
File metadata and controls
96 lines (74 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""
System Prompt Pre-processing Module
This module provides functions to load system prompt configuration
from Claude Code's API request format.
Note: Anthropic's API uses 'system' as a separate parameter, not a message role.
System prompts and tools are API configuration, not part of the conversation trajectory.
"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Dict, List, Optional
import typer
def load_system_prompt(prompt_path: Path) -> Optional[Dict[str, Any]]:
"""Load the system prompt from cc_prompt.json.
The file contains system configuration including:
- system: List of system messages
- tools: List of tool definitions
- thinking: Thinking configuration
- Other metadata (status_code, timestamp, user_agent, placeholders)
Args:
prompt_path: Path to the cc_prompt.json file
Returns:
Dictionary with system configuration or None if loading fails
"""
try:
with open(prompt_path, "r", encoding="utf-8") as f:
data = json.load(f)
return data
except FileNotFoundError:
typer.echo(f"Warning: System prompt file not found: {prompt_path}", err=True)
return None
except Exception as e:
typer.echo(f"Warning: Failed to load system prompt: {e}", err=True)
return None
def extract_system_config(data: Dict[str, Any]) -> Dict[str, Any]:
"""Extract system configuration from cc_prompt.json data.
Returns the system prompt and tools in the format expected by
Anthropic's Messages API.
Args:
data: Loaded JSON data from cc_prompt.json
Returns:
Dictionary with system configuration:
{
"system": str or List[dict], # System prompt content
"tools": List[dict], # Tool definitions
"thinking": Optional[dict] # Thinking configuration
}
"""
# Extract system prompt (may be multiple system messages)
system_parts = []
if "system" in data:
for sys_msg in data["system"]:
if isinstance(sys_msg, dict) and "text" in sys_msg:
system_parts.append(sys_msg["text"])
# Join multiple system messages with newlines, or use as-is
system_prompt = "\n\n".join(system_parts) if len(system_parts) > 1 else system_parts[0] if system_parts else ""
# Extract tools - they're already in the right format
tools = data.get("tools", [])
# Extract thinking config if present
thinking = data.get("thinking", None)
return {
"system": system_prompt,
"tools": tools,
"thinking": thinking
}
def get_default_system_prompt_path() -> Path:
"""Get the default path to the system prompt file.
Returns:
Path to resource/cc_prompt.json relative to the script directory
"""
# Get the directory where this script is located
script_dir = Path(__file__).parent
return script_dir / "resource" / "cc_prompt.json"