-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy path_summarizer_checker.py
More file actions
176 lines (121 loc) · 5.43 KB
/
Copy path_summarizer_checker.py
File metadata and controls
176 lines (121 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
"""Summarizer checker functions."""
from __future__ import annotations
import time
from typing import Callable
from typing import List
from trpc_agent_sdk.events import Event
from trpc_agent_sdk.log import logger
from ._session import Session
CheckSummarizerFunction = Callable[[Session], bool]
def _leading_summary_event(session: Session) -> Event | None:
"""Return the summary anchor when the active event window starts with one."""
if session.events and session.events[0].is_summary_event():
return session.events[0]
return None
def _events_after_summary_anchor(session: Session) -> list[Event]:
"""Return active events after the leading summary anchor."""
return session.events[1:] if _leading_summary_event(session) else session.events
def set_summarizer_token_threshold(token_count: int) -> CheckSummarizerFunction:
"""Set the token threshold for summarizer.
Args:
token_count: The token count to check
Returns:
True if summarization is needed, False otherwise
"""
def _decorator(session: Session) -> bool:
# Filter events with usage_metadata
events_with_metadata = [
event for event in _events_after_summary_anchor(session) if event.usage_metadata is not None
]
# If no events have usage_metadata, log a warning and return False
if not events_with_metadata:
logger.warning(
"No events with usage_metadata found in session %s. "
"Token-based summarization check returning False. "
"This may indicate that LLM responses are not properly recording token usage.", session.id)
return False
# Calculate total token count
total_token_count = sum(event.usage_metadata.total_token_count for event in events_with_metadata)
return total_token_count > token_count
return _decorator
def set_summarizer_events_count_threshold(event_count: int = 30) -> CheckSummarizerFunction:
"""Set the number of events threshold for summarizer.
Args:
event_count: The event count to check
Returns:
True if summarization is needed, False otherwise
"""
def _decorator(session: Session) -> bool:
# Check if we have enough events to warrant summarization
return len(_events_after_summary_anchor(session)) > event_count
return _decorator
def set_summarizer_time_interval_threshold(time_interval: float = 300.0) -> CheckSummarizerFunction:
"""Set the time interval threshold for summarizer.
Args:
time_interval: The time interval to check
Returns:
True if summarization is needed, False otherwise
"""
def _decorator(session: Session) -> bool:
summary_event = _leading_summary_event(session)
events_after_summary = _events_after_summary_anchor(session)
if not events_after_summary:
return False
if summary_event is not None:
return time.time() - summary_event.timestamp > time_interval
return time.time() - events_after_summary[-1].timestamp > time_interval
return _decorator
def set_summarizer_important_content_threshold(important_content_count: int = 10) -> CheckSummarizerFunction:
"""Set the important content threshold for summarizer.
Args:
important_content: The important content to check
Returns:
True if summarization is needed, False otherwise
"""
def _decorator(session: Session) -> bool:
# Check if there's important content to summarize
for event in _events_after_summary_anchor(session):
if event.content and event.content.parts:
for part in event.content.parts:
if part.text and len(part.text.strip()) > important_content_count:
return True
return False
return _decorator
def set_summarizer_conversation_threshold(conversation_count: int = 100) -> CheckSummarizerFunction:
"""Set the conversation count threshold for summarizer.
Args:
conversation_count: The conversation count to check
Returns:
True if summarization is needed, False otherwise
"""
def _decorator(session: Session) -> bool:
if session.conversation_count > conversation_count:
session.conversation_count = 0
return True
return False
return _decorator
def set_summarizer_check_functions_by_and(funcs: List[CheckSummarizerFunction]) -> CheckSummarizerFunction:
"""Set the check functions for summarizer, all the functions must return True.
Args:
funcs: The list of check summarizer functions
Returns:
True if summarization is needed, False otherwise
"""
def _decorator(session: Session) -> bool:
return all(func(session) for func in funcs)
return _decorator
def set_summarizer_check_functions_by_or(funcs: List[CheckSummarizerFunction]) -> CheckSummarizerFunction:
"""Set the check functions for summarizer, any of the functions return True.
Args:
funcs: The list of check summarizer functions
Returns:
True if summarization is needed, False otherwise
"""
def _decorator(session: Session) -> bool:
return any(func(session) for func in funcs)
return _decorator