agentscope-ai / agentscope-ai/agentscope

[Feature] Emit SSE event before/after compress_context() for better UX

Đang mở
#2,144 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
31.5k
Fork
3.5k
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
95

Mô tả

## Problem

When `compress_context()` is triggered (token count exceeds threshold), the operation blocks silently with no SSE event emitted to the frontend. Users see a "thinking" spinner but don't know the system is actually compressing context.

Worse, if compression fails (e.g., `RuntimeError: Failed to generate structured output for model` due to unsupported `DataBlock` in context), the error is classified as `ErrorType.UNKNOWN` and surfaces as a generic "unknown error" to users.

## Reproduction

1. Have a long conversation that accumulates >52k tokens
2. Send a message that triggers `compress_context()`
3. Observe: no SSE event during compression, just silence
4. If compression fails: `REPLY_END(finished_reason=error, error.type=unknown)`

## Current Code

In `_agent.py:825-831`:
```python
if action == "reasoning":
# Compressed the memory if needed before reasoning
await self.compress_context() # ← blocking, no event
async for evt in self._inject_runtime_state():
yield evt
```

The `on_compress_context` middleware hook exists but has signature `-> None`, not `AsyncGenerator`, so it cannot yield events.

## Suggested Solution

Emit a `HintBlock` event before compression starts:

```python
if action == "reasoning":
if self._should_compress():
yield Msg(
name="system",
content=[HintBlock(hint="Compressing conversation context...")],
role="user",
)
await self.compress_context()
async for evt in self._inject_runtime_state():
yield evt
```

This would allow frontends to display "Compressing context..." during the operation.

## Environment

- AgentScope version: 2.0.4 (editable install from main branch)
- Model: DeepSeek V4
- Runtime: BusinessEmbedDemo

## Additional Context

The compression failure itself is a separate bug - `generate_structured_output()` doesn't handle `DataBlock` gracefully. But even if that's fixed, the UX issue of silent compression remains.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.