refactor: enhance session content handling in AIAgent and update TTS output path
- Introduced a new static method `_clean_session_content` in the `AIAgent` class to convert REASONING_SCRATCHPAD tags to <think> blocks and clean up whitespace in session logs. - Updated the `_save_session_log` method to utilize the cleaned content for assistant messages, ensuring consistency in session logs. - Changed the default output directory for TTS audio files from `~/voice-memos` to `~/.hermes/audio_cache`, reflecting a more appropriate storage location.
This commit is contained in:
parent
41df8ee4f5
commit
f64a87209d
2 changed files with 25 additions and 4 deletions
25
run_agent.py
25
run_agent.py
|
|
@ -899,6 +899,18 @@ class AIAgent:
|
||||||
logging.warning(f"Failed to dump API request debug payload: {dump_error}")
|
logging.warning(f"Failed to dump API request debug payload: {dump_error}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _clean_session_content(content: str) -> str:
|
||||||
|
"""Convert REASONING_SCRATCHPAD to think tags and clean up whitespace."""
|
||||||
|
if not content:
|
||||||
|
return content
|
||||||
|
content = convert_scratchpad_to_think(content)
|
||||||
|
# Strip extra newlines before/after think blocks
|
||||||
|
import re
|
||||||
|
content = re.sub(r'\n+(<think>)', r'\n\1', content)
|
||||||
|
content = re.sub(r'(</think>)\n+', r'\1\n', content)
|
||||||
|
return content.strip()
|
||||||
|
|
||||||
def _save_session_log(self, messages: List[Dict[str, Any]] = None):
|
def _save_session_log(self, messages: List[Dict[str, Any]] = None):
|
||||||
"""
|
"""
|
||||||
Save the full raw session to a JSON file.
|
Save the full raw session to a JSON file.
|
||||||
|
|
@ -908,6 +920,7 @@ class AIAgent:
|
||||||
tool responses (with tool_call_id, tool_name), and injected system
|
tool responses (with tool_call_id, tool_name), and injected system
|
||||||
messages (compression summaries, todo snapshots, etc.).
|
messages (compression summaries, todo snapshots, etc.).
|
||||||
|
|
||||||
|
REASONING_SCRATCHPAD tags are converted to <think> blocks for consistency.
|
||||||
Overwritten after each turn so it always reflects the latest state.
|
Overwritten after each turn so it always reflects the latest state.
|
||||||
"""
|
"""
|
||||||
messages = messages or self._session_messages
|
messages = messages or self._session_messages
|
||||||
|
|
@ -915,6 +928,14 @@ class AIAgent:
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Clean assistant content for session logs
|
||||||
|
cleaned = []
|
||||||
|
for msg in messages:
|
||||||
|
if msg.get("role") == "assistant" and msg.get("content"):
|
||||||
|
msg = dict(msg)
|
||||||
|
msg["content"] = self._clean_session_content(msg["content"])
|
||||||
|
cleaned.append(msg)
|
||||||
|
|
||||||
entry = {
|
entry = {
|
||||||
"session_id": self.session_id,
|
"session_id": self.session_id,
|
||||||
"model": self.model,
|
"model": self.model,
|
||||||
|
|
@ -922,8 +943,8 @@ class AIAgent:
|
||||||
"platform": self.platform,
|
"platform": self.platform,
|
||||||
"session_start": self.session_start.isoformat(),
|
"session_start": self.session_start.isoformat(),
|
||||||
"last_updated": datetime.now().isoformat(),
|
"last_updated": datetime.now().isoformat(),
|
||||||
"message_count": len(messages),
|
"message_count": len(cleaned),
|
||||||
"messages": messages,
|
"messages": cleaned,
|
||||||
}
|
}
|
||||||
|
|
||||||
with open(self.session_log_file, "w", encoding="utf-8") as f:
|
with open(self.session_log_file, "w", encoding="utf-8") as f:
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ DEFAULT_ELEVENLABS_VOICE_ID = "pNInz6obpgDQGcFmaJgB" # Adam
|
||||||
DEFAULT_ELEVENLABS_MODEL_ID = "eleven_multilingual_v2"
|
DEFAULT_ELEVENLABS_MODEL_ID = "eleven_multilingual_v2"
|
||||||
DEFAULT_OPENAI_MODEL = "gpt-4o-mini-tts"
|
DEFAULT_OPENAI_MODEL = "gpt-4o-mini-tts"
|
||||||
DEFAULT_OPENAI_VOICE = "alloy"
|
DEFAULT_OPENAI_VOICE = "alloy"
|
||||||
DEFAULT_OUTPUT_DIR = os.path.expanduser("~/voice-memos")
|
DEFAULT_OUTPUT_DIR = os.path.expanduser("~/.hermes/audio_cache")
|
||||||
MAX_TEXT_LENGTH = 4000
|
MAX_TEXT_LENGTH = 4000
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -435,7 +435,7 @@ TTS_SCHEMA = {
|
||||||
},
|
},
|
||||||
"output_path": {
|
"output_path": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Optional custom file path to save the audio. Defaults to ~/voice-memos/<timestamp>.mp3"
|
"description": "Optional custom file path to save the audio. Defaults to ~/.hermes/audio_cache/<timestamp>.mp3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": ["text"]
|
"required": ["text"]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue