mtyrrell commited on
Commit
10a3be0
·
1 Parent(s): 3f4db57

ts convo history

Browse files
Files changed (1) hide show
  1. app/utils.py +41 -31
app/utils.py CHANGED
@@ -173,50 +173,60 @@ def build_conversation_context(messages, max_turns: int = 3, max_chars: int = 80
173
  logger.debug(f"Added first ASSISTANT message ({msg_chars} chars)")
174
 
175
  # Collect last N complete turns (user + assistant pairs)
 
 
 
 
 
 
176
  turn_count = 0
177
  recent_messages = []
178
- current_turn_messages = []
179
 
180
- # Process messages in reverse to get the most recent turns
181
- for i, msg in enumerate(reversed(messages)):
182
- # Skip if this is one of the first messages we already included
183
- if (first_user_msg and msg == first_user_msg) or (first_assistant_msg and msg == first_assistant_msg):
184
- continue
185
 
186
- role_label = msg.role.upper()
187
- content = msg.content
 
188
 
189
- # Build message text
190
- msg_text = f"{role_label}: {content}"
191
- msg_chars = len(msg_text)
 
 
 
 
 
 
192
 
193
- # Check character limit
194
- if char_count + msg_chars > max_chars:
195
  logger.info(f"Stopping context build: would exceed max_chars ({max_chars})")
196
  break
197
-
198
- # Add message to current turn
199
- current_turn_messages.insert(0, msg_text)
200
- char_count += msg_chars
201
  msgs_included += 1
202
 
203
- # Check if we've completed a turn (assistant message followed by user in reverse order)
204
- if msg.role == 'assistant':
205
- # Look ahead to see if there's a corresponding user message
206
- # In reverse order, we expect: assistant, user, assistant, user...
207
- if i + 1 < len(messages):
208
- next_msg = list(reversed(messages))[i + 1]
209
- if next_msg.role == 'user':
210
- # This is a complete turn, increment counter
211
- turn_count += 1
212
- if turn_count >= max_turns:
213
- logger.info(f"Stopping context build: reached max_turns ({max_turns})")
214
- break
215
 
216
- logger.debug(f"Added {role_label} message ({msg_chars} chars, {msgs_included} total messages)")
 
217
 
218
  # Add recent messages to context
219
- context_parts.extend(current_turn_messages)
220
 
221
  context = "\n\n".join(context_parts)
222
  logger.info(f"Built conversation context: {turn_count} recent user turns, {msgs_included} total messages, {char_count} chars")
 
173
  logger.debug(f"Added first ASSISTANT message ({msg_chars} chars)")
174
 
175
  # Collect last N complete turns (user + assistant pairs)
176
+ # Find the last N user messages and their corresponding assistant responses
177
+ user_messages = [msg for msg in messages if msg.role == 'user']
178
+
179
+ # Get the last N user messages (excluding the first one we already included)
180
+ recent_user_messages = user_messages[1:][-max_turns:] if len(user_messages) > 1 else []
181
+
182
  turn_count = 0
183
  recent_messages = []
 
184
 
185
+ # Process each recent user message and find its corresponding assistant response
186
+ for user_msg in recent_user_messages:
187
+ if turn_count >= max_turns:
188
+ break
 
189
 
190
+ # Find the assistant response that follows this user message
191
+ user_index = messages.index(user_msg)
192
+ assistant_msg = None
193
 
194
+ # Look for the next assistant message after this user message
195
+ for i in range(user_index + 1, len(messages)):
196
+ if messages[i].role == 'assistant':
197
+ assistant_msg = messages[i]
198
+ break
199
+
200
+ # Add user message
201
+ user_text = f"USER: {user_msg.content}"
202
+ user_chars = len(user_text)
203
 
204
+ if char_count + user_chars > max_chars:
 
205
  logger.info(f"Stopping context build: would exceed max_chars ({max_chars})")
206
  break
207
+
208
+ recent_messages.append(user_text)
209
+ char_count += user_chars
 
210
  msgs_included += 1
211
 
212
+ # Add assistant message if it exists
213
+ if assistant_msg:
214
+ assistant_text = f"ASSISTANT: {assistant_msg.content}"
215
+ assistant_chars = len(assistant_text)
216
+
217
+ if char_count + assistant_chars > max_chars:
218
+ logger.info(f"Stopping context build: would exceed max_chars ({max_chars})")
219
+ break
220
+
221
+ recent_messages.append(assistant_text)
222
+ char_count += assistant_chars
223
+ msgs_included += 1
224
 
225
+ turn_count += 1
226
+ logger.debug(f"Added turn {turn_count}: user + assistant messages")
227
 
228
  # Add recent messages to context
229
+ context_parts.extend(recent_messages)
230
 
231
  context = "\n\n".join(context_parts)
232
  logger.info(f"Built conversation context: {turn_count} recent user turns, {msgs_included} total messages, {char_count} chars")