1919class ConversationHistoryService :
2020 """Service for managing conversation history in judge tools."""
2121
22- def __init__ (self , config : Config , db_provider : ConversationHistoryDB | None = None ):
22+ def __init__ (
23+ self , config : Config , db_provider : ConversationHistoryDB | None = None
24+ ):
2325 """
2426 Initialize the conversation history service.
2527
@@ -30,7 +32,9 @@ def __init__(self, config: Config, db_provider: ConversationHistoryDB | None = N
3032 self .config = config
3133 self .db = db_provider or create_database_provider (config )
3234
33- async def load_context_for_enrichment (self , session_id : str ) -> list [ConversationRecord ]:
35+ async def load_context_for_enrichment (
36+ self , session_id : str
37+ ) -> list [ConversationRecord ]:
3438 """
3539 Load recent conversation records for LLM context enrichment.
3640
@@ -45,18 +49,14 @@ async def load_context_for_enrichment(self, session_id: str) -> list[Conversatio
4549 # Load recent conversations for this session
4650 recent_records = await self .db .get_session_conversations (
4751 session_id = session_id ,
48- limit = self .config .database .context_enrichment_count # load last X records
52+ limit = self .config .database .context_enrichment_count , # load last X records
4953 )
5054
5155 logger .info (f"📚 Retrieved { len (recent_records )} conversation records from DB" )
5256 return recent_records
5357
5458 async def save_tool_interaction (
55- self ,
56- session_id : str ,
57- tool_name : str ,
58- tool_input : str ,
59- tool_output : str
59+ self , session_id : str , tool_name : str , tool_input : str , tool_output : str
6060 ) -> str :
6161 """
6262 Save a tool interaction as a conversation record.
@@ -70,13 +70,15 @@ async def save_tool_interaction(
7070 Returns:
7171 ID of the created conversation record
7272 """
73- logger .info (f"💾 Saving tool interaction to SQLite DB for session: { session_id } , tool: { tool_name } " )
73+ logger .info (
74+ f"💾 Saving tool interaction to SQLite DB for session: { session_id } , tool: { tool_name } "
75+ )
7476
7577 record_id = await self .db .save_conversation (
7678 session_id = session_id ,
7779 source = tool_name ,
7880 input_data = tool_input ,
79- output = tool_output
81+ output = tool_output ,
8082 )
8183
8284 logger .info (f"✅ Saved conversation record with ID: { record_id } " )
@@ -96,19 +98,27 @@ def format_context_for_llm(self, context_records: list[ConversationRecord]) -> s
9698 logger .info ("📝 No conversation history to format for LLM context" )
9799 return "No previous conversation history available."
98100
99- logger .info (f"📝 Formatting { len (context_records )} conversation records for LLM context enrichment" )
101+ logger .info (
102+ f"📝 Formatting { len (context_records )} conversation records for LLM context enrichment"
103+ )
100104
101105 context_lines = ["## Previous Conversation History" ]
102- context_lines .append ("Here are the recent interactions in this session for context:" )
106+ context_lines .append (
107+ "Here are the recent interactions in this session for context:"
108+ )
103109 context_lines .append ("" )
104110
105111 # Format records (most recent first)
106112 for i , record in enumerate (context_records , 1 ):
107- context_lines .append (f"### { i } . { record .source } ({ record .timestamp .strftime ('%Y-%m-%d %H:%M:%S' )} )" )
113+ context_lines .append (
114+ f"### { i } . { record .source } ({ record .timestamp .strftime ('%Y-%m-%d %H:%M:%S' )} )"
115+ )
108116 context_lines .append (f"**Input:** { record .input } " )
109117 context_lines .append (f"**Output:** { record .output } " )
110118 context_lines .append ("" )
111- logger .info (f" Formatted record { i } : { record .source } from { record .timestamp } " )
119+ logger .info (
120+ f" Formatted record { i } : { record .source } from { record .timestamp } "
121+ )
112122
113123 context_lines .append ("---" )
114124 context_lines .append ("Use this context to make more informed decisions." )
@@ -141,18 +151,19 @@ async def get_session_summary(self, session_id: str) -> dict:
141151 "session_id" : session_id ,
142152 "total_interactions" : len (all_records ),
143153 "tool_usage" : tool_counts ,
144- "latest_interaction" : all_records [0 ].timestamp .isoformat () if all_records else None ,
154+ "latest_interaction" : all_records [0 ].timestamp .isoformat ()
155+ if all_records
156+ else None ,
145157 "context_enrichment_count" : self .config .database .context_enrichment_count ,
146- "max_context_records" : self .config .database .max_context_records
158+ "max_context_records" : self .config .database .max_context_records ,
147159 }
148160
149161
150162# Convenience functions for easy integration with existing tools
151163
164+
152165async def enrich_with_context (
153- service : ConversationHistoryService ,
154- session_id : str ,
155- base_prompt : str
166+ service : ConversationHistoryService , session_id : str , base_prompt : str
156167) -> str :
157168 """
158169 Enrich a base prompt with conversation history context.
@@ -165,16 +176,17 @@ async def enrich_with_context(
165176 Returns:
166177 Enriched prompt with conversation history context
167178 """
168- logger .info (f"🔄 Starting context enrichment for session { session_id } , base_prompt: { base_prompt } " )
179+ logger .info (
180+ f"🔄 Starting context enrichment for session { session_id } , base_prompt: { base_prompt } "
181+ )
169182
170183 context_records = await service .load_context_for_enrichment (session_id )
171184 context_text = service .format_context_for_llm (context_records )
172185
173186 enriched_prompt = f"{ context_text } \n ## Current Request\n { base_prompt } "
174187
175- logger .info (f"🎯 Context enrichment completed for session { session_id } , enriched_prompt: { enriched_prompt } " )
188+ logger .info (
189+ f"🎯 Context enrichment completed for session { session_id } , enriched_prompt: { enriched_prompt } "
190+ )
176191
177192 return enriched_prompt
178-
179-
180-
0 commit comments