feat: 知识库文件夹/模板、技能压缩设置与摘要窗口调优
- 知识库新增文件夹与模板支持(模型/仓储/服务/迁移),前端知识管理组件 - 岗位管理模型与接口完善 - 技能新增压缩设置页与相关存储/工具能力 - 系统设置扩展配置项 - 摘要中间件 keep 窗口改用 token 预算(16000),减少重复压缩 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e6662cf673
commit
5ebbb82d66
54 files changed
+2658
-100
No files matched your search
+74
@@ -37,6 +37,15 @@ logger = logging.getLogger(__name__)
|
||||
# many threads can't grow it without limit. Oldest entries are evicted first.
|
||||
_SUMMARY_CACHE_MAX_THREADS = 512
|
||||
|
||||
# After compaction the preserved window must land *comfortably* below every
|
||||
# trigger threshold, otherwise the "compressed" context is still over the
|
||||
# trigger and re-summarizes on the very next turn (the sticky boundary never
|
||||
# sticks). We force the kept suffix down to this fraction of each trigger so a
|
||||
# re-compaction only happens once meaningfully *new* content accumulates — not
|
||||
# on every follow-up question. ``keep`` only sets a *target*; this is the hard
|
||||
# headroom guarantee on top of it.
|
||||
_KEEP_HEADROOM_FRACTION = 0.75
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SummarizationEvent:
|
||||
@@ -319,6 +328,15 @@ class DeerFlowSummarizationMiddleware(SummarizationMiddleware):
|
||||
if cutoff_index <= 0:
|
||||
return None
|
||||
|
||||
# Force the preserved window below every trigger (with headroom) so the
|
||||
# post-compaction context doesn't immediately re-trigger. Without this,
|
||||
# a ``keep`` expressed in *messages* (e.g. 20) can itself blow past a
|
||||
# *token* trigger (e.g. 60000) when recent messages are large (tool
|
||||
# outputs, pasted docs) — and then every follow-up question re-summarizes.
|
||||
cutoff_index = self._apply_stickiness_headroom(messages, cutoff_index)
|
||||
if cutoff_index <= 0:
|
||||
return None
|
||||
|
||||
messages_to_summarize, preserved = self._partition_with_skill_rescue(messages, cutoff_index)
|
||||
if not messages_to_summarize:
|
||||
return None
|
||||
@@ -368,6 +386,62 @@ class DeerFlowSummarizationMiddleware(SummarizationMiddleware):
|
||||
while len(self._summary_cache) > _SUMMARY_CACHE_MAX_THREADS:
|
||||
self._summary_cache.popitem(last=False)
|
||||
|
||||
def _apply_stickiness_headroom(self, messages: list[AnyMessage], cutoff_index: int) -> int:
|
||||
"""Advance ``cutoff_index`` (keep fewer recent messages) so the preserved
|
||||
suffix lands below every trigger threshold with headroom.
|
||||
|
||||
Only ever *raises* the cutoff (summarizes more / keeps less); never keeps
|
||||
more than the caller's ``keep`` policy already chose. This is what makes
|
||||
the sticky boundary actually stick: after compaction the kept context is
|
||||
guaranteed under ~``_KEEP_HEADROOM_FRACTION`` of each trigger, so a
|
||||
re-compaction waits for genuinely new content instead of firing every turn.
|
||||
"""
|
||||
n = len(messages)
|
||||
if n <= 1:
|
||||
return cutoff_index
|
||||
|
||||
for kind, value in getattr(self, "_trigger_conditions", []):
|
||||
if kind == "messages":
|
||||
keep_cap = max(1, int(int(value) * _KEEP_HEADROOM_FRACTION))
|
||||
cutoff_index = max(cutoff_index, n - keep_cap)
|
||||
elif kind == "tokens":
|
||||
target = max(1, int(int(value) * _KEEP_HEADROOM_FRACTION))
|
||||
cutoff_index = max(cutoff_index, self._suffix_token_cutoff(messages, target))
|
||||
# "fraction" triggers depend on model profile limits; the message/token
|
||||
# caps above (when present) already bound the window, so skip them here.
|
||||
|
||||
# Always keep at least the final message, and re-snap to a safe AI/Tool
|
||||
# boundary so we never orphan a tool result.
|
||||
cutoff_index = min(cutoff_index, n - 1)
|
||||
return self._find_safe_cutoff_point(messages, cutoff_index)
|
||||
|
||||
def _suffix_token_cutoff(self, messages: list[AnyMessage], target_tokens: int) -> int:
|
||||
"""Smallest index ``i`` such that ``token_counter(messages[i:]) <= target_tokens``.
|
||||
|
||||
Binary search mirroring the stock token-based cutoff; returns 0 when the
|
||||
whole list already fits, and never returns the full length (keeps ≥1).
|
||||
"""
|
||||
n = len(messages)
|
||||
if n == 0:
|
||||
return 0
|
||||
if self.token_counter(messages) <= target_tokens:
|
||||
return 0
|
||||
|
||||
left, right = 0, n
|
||||
cutoff = n
|
||||
for _ in range(n.bit_length() + 1):
|
||||
if left >= right:
|
||||
break
|
||||
mid = (left + right) // 2
|
||||
if self.token_counter(messages[mid:]) <= target_tokens:
|
||||
cutoff = mid
|
||||
right = mid
|
||||
else:
|
||||
left = mid + 1
|
||||
if cutoff == n:
|
||||
cutoff = left
|
||||
return min(cutoff, n - 1)
|
||||
|
||||
@staticmethod
|
||||
def _resolve_request_thread_id(request: ModelRequest) -> str | None:
|
||||
"""Best-effort thread id from the model request's runtime."""
|
||||
|
||||
Reference in new issue
Block a user