refac
This commit is contained in:
1 parent
93a34bb25b
commit
15688686af
1 file changed
+6
-3
@@ -52,16 +52,19 @@ async def compact_messages_for_request(
|
|||||||
if not config['enable']:
|
if not config['enable']:
|
||||||
return messages, None, False
|
return messages, None, False
|
||||||
|
|
||||||
|
system_messages = messages[:1] if messages and messages[0].get('role') == 'system' else []
|
||||||
|
messages = messages[len(system_messages) :]
|
||||||
|
|
||||||
messages, previous_summary = _apply_latest_summary_checkpoint(messages)
|
messages, previous_summary = _apply_latest_summary_checkpoint(messages)
|
||||||
token_threshold = _resolve_token_threshold(config['token_threshold'], config['token_cap'], metadata)
|
token_threshold = _resolve_token_threshold(config['token_threshold'], config['token_cap'], metadata)
|
||||||
if not _exceeds_token_threshold(messages, system_prompt, previous_summary, token_threshold) or len(messages) <= 3:
|
if not _exceeds_token_threshold(messages, system_prompt, previous_summary, token_threshold) or len(messages) <= 3:
|
||||||
return messages, previous_summary, False
|
return [*system_messages, *messages], previous_summary, False
|
||||||
|
|
||||||
boundary = _find_compaction_boundary(messages, config['retention_percentage'])
|
boundary = _find_compaction_boundary(messages, config['retention_percentage'])
|
||||||
compacted_messages = messages[:boundary]
|
compacted_messages = messages[:boundary]
|
||||||
recent_messages = messages[boundary:]
|
recent_messages = messages[boundary:]
|
||||||
if not compacted_messages or not recent_messages:
|
if not compacted_messages or not recent_messages:
|
||||||
return messages, previous_summary, False
|
return [*system_messages, *messages], previous_summary, False
|
||||||
|
|
||||||
event_emitter = None
|
event_emitter = None
|
||||||
if metadata.get('chat_id') and metadata.get('message_id'):
|
if metadata.get('chat_id') and metadata.get('message_id'):
|
||||||
@@ -141,7 +144,7 @@ async def compact_messages_for_request(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
return recent_messages, summary, True
|
return [*system_messages, *recent_messages], summary, True
|
||||||
|
|
||||||
|
|
||||||
async def compact_chat_branch(request, user, chat: Any, model_id: str, models: dict) -> dict:
|
async def compact_chat_branch(request, user, chat: Any, model_id: str, models: dict) -> dict:
|
||||||
|
|||||||
Reference in new issue
Block a user