feat: make context compaction visible in channels (#5656)

* feat: surface context compaction lifecycle

* fix: complete and simplify context compaction notifications
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-05 20:21:54 +08:00
1 parent 455533169d
commit 5229989f28
52 files changed
+1849 -140

No files matched your search

+32
View File
@@ -11,6 +11,8 @@ from contextlib import suppress
from dataclasses import dataclass, replace
from typing import TYPE_CHECKING, Any, Literal, cast
from loguru import logger
from nanobot import __version__
from nanobot.bus.events import INBOUND_META_USER_SHELL, OutboundMessage
from nanobot.command.router import CommandContext, CommandRouter, normalize_command_text
@@ -71,6 +73,12 @@ BUILTIN_COMMAND_SPECS: tuple[BuiltinCommandSpec, ...] = (
"square-pen",
lifecycle="finalize_active_turn",
),
BuiltinCommandSpec(
"/compact",
"Compact context",
"Compact this chat's context and continue the conversation.",
"archive",
),
BuiltinCommandSpec(
"/stop",
"Stop current task",
@@ -337,6 +345,29 @@ async def cmd_new(ctx: CommandContext) -> OutboundMessage:
)
async def cmd_compact(ctx: CommandContext) -> None:
"""Compact the current session without resetting the conversation."""
loop = ctx.loop
session = ctx.session or loop.sessions.get_or_create(ctx.key)
runtime = ctx.runtime or loop.runtime_for_session(session)
delivery = loop.turn_delivery_factory.create(ctx.msg, ctx.key)
try:
summary = await loop.consolidator.compact_idle_session(
ctx.key,
runtime=runtime,
on_compaction=delivery.context_compaction,
)
except Exception:
logger.exception("Manual context compaction failed for {}", ctx.key)
return
if summary:
refreshed = loop.sessions.get_or_create(ctx.key)
refreshed.provider_state = None
loop.sessions.save(refreshed)
def _format_preset_names(names: list[str]) -> str:
return ", ".join(f"`{name}`" for name in names) if names else "(none configured)"
@@ -1044,6 +1075,7 @@ def register_builtin_commands(router: CommandRouter) -> None:
router.priority("/restart", cmd_restart)
router.priority("/status", cmd_status)
router.exact("/new", cmd_new)
router.exact("/compact", cmd_compact)
router.exact("/status", cmd_status)
router.exact("/model", cmd_model)
router.prefix("/model ", cmd_model)