Spaces:
Running
Running
Pulastya B
commited on
Commit
·
6a89a08
1
Parent(s):
b54c2e5
Fix ChatMessage object handling in pruning logic
Browse files- src/orchestrator.py +11 -4
src/orchestrator.py
CHANGED
|
@@ -2031,6 +2031,13 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 2031 |
# 🚀 SMART CONVERSATION PRUNING (Mistral-compatible)
|
| 2032 |
# Keep only: system + user + last 4 exchanges (8 messages)
|
| 2033 |
# CRITICAL: Maintain valid message ordering for Mistral API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2034 |
if len(messages) > 10:
|
| 2035 |
# Keep: system prompt [0], user query [1], last valid exchanges
|
| 2036 |
system_msg = messages[0]
|
|
@@ -2042,9 +2049,9 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 2042 |
cleaned_recent = []
|
| 2043 |
for i, msg in enumerate(recent_msgs):
|
| 2044 |
# Skip tool messages that aren't preceded by assistant
|
| 2045 |
-
if msg
|
| 2046 |
# Check if previous message is assistant
|
| 2047 |
-
if i > 0 and recent_msgs[i-1]
|
| 2048 |
cleaned_recent.append(msg)
|
| 2049 |
# Otherwise skip this orphaned tool message
|
| 2050 |
else:
|
|
@@ -2067,8 +2074,8 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 2067 |
# Clean orphaned tool messages
|
| 2068 |
cleaned_recent = []
|
| 2069 |
for i, msg in enumerate(recent_msgs):
|
| 2070 |
-
if msg
|
| 2071 |
-
if i > 0 and recent_msgs[i-1]
|
| 2072 |
cleaned_recent.append(msg)
|
| 2073 |
else:
|
| 2074 |
cleaned_recent.append(msg)
|
|
|
|
| 2031 |
# 🚀 SMART CONVERSATION PRUNING (Mistral-compatible)
|
| 2032 |
# Keep only: system + user + last 4 exchanges (8 messages)
|
| 2033 |
# CRITICAL: Maintain valid message ordering for Mistral API
|
| 2034 |
+
|
| 2035 |
+
# Helper function to get role from message (handles dict or ChatMessage object)
|
| 2036 |
+
def get_role(msg):
|
| 2037 |
+
if isinstance(msg, dict):
|
| 2038 |
+
return msg.get('role', '')
|
| 2039 |
+
return getattr(msg, 'role', '')
|
| 2040 |
+
|
| 2041 |
if len(messages) > 10:
|
| 2042 |
# Keep: system prompt [0], user query [1], last valid exchanges
|
| 2043 |
system_msg = messages[0]
|
|
|
|
| 2049 |
cleaned_recent = []
|
| 2050 |
for i, msg in enumerate(recent_msgs):
|
| 2051 |
# Skip tool messages that aren't preceded by assistant
|
| 2052 |
+
if get_role(msg) == 'tool':
|
| 2053 |
# Check if previous message is assistant
|
| 2054 |
+
if i > 0 and get_role(recent_msgs[i-1]) == 'assistant':
|
| 2055 |
cleaned_recent.append(msg)
|
| 2056 |
# Otherwise skip this orphaned tool message
|
| 2057 |
else:
|
|
|
|
| 2074 |
# Clean orphaned tool messages
|
| 2075 |
cleaned_recent = []
|
| 2076 |
for i, msg in enumerate(recent_msgs):
|
| 2077 |
+
if get_role(msg) == 'tool':
|
| 2078 |
+
if i > 0 and get_role(recent_msgs[i-1]) == 'assistant':
|
| 2079 |
cleaned_recent.append(msg)
|
| 2080 |
else:
|
| 2081 |
cleaned_recent.append(msg)
|