google / google/adk-python

Session Fork/Clone API for Conversation Branching

Đang mở
#4,010 2 bình luận 8 reaction 1 người được giao Được @wuliang229 nhận Xem trên GitHub
needs review services
Ngôn ngữ chính
Python
Star
21.5k
Fork
4k
Merge trung bình
1 ngày 14 giờ
Pull request đã merge (30 ngày)
37

Mô tả

**Is your feature request related to a problem? Please describe.**

When building applications that need to explore multiple conversation paths from a single point, there's no way to create a copy of an existing session without modifying the original.

The current `rewind_async()` API modifies the original session's state and artifacts, making it impossible to:
- Preserve the original conversation while exploring alternative paths
- Create "what-if" branches from a conversation midpoint
- Allow users to fork a conversation and continue in different directions

Example scenario: A user is at turn 5 of a conversation and wants to try a different approach from turn 3, but also keep the original turn 3→5 history intact. Currently this is not possible.

**Describe the solution you'd like**

Add a `fork_session()` or `clone_session()` API that creates a new independent session by copying events, state, and artifact references up to a specified invocation point.

```python
# Option A: In SessionService
new_session = await session_service.fork_session(
app_name=app_name,
user_id=user_id,
source_session_id=original_session.id,
fork_before_invocation_id="inv_3", # Fork point
)

# Option B: In Runner (similar to rewind_async)
new_session = await runner.fork_async(
user_id=user_id,
session_id=session_id,
fork_before_invocation_id="inv_3",
)
```

The original session remains completely unchanged.

**Describe alternatives you've considered**

1. **Rewind then copy**: Doesn't work because `rewind_async()` modifies the original session's state and artifacts before any copy could be made.

2. **Manual event copying**: Requires manually iterating events, recomputing state deltas, and handling artifact references. Error-prone and not officially supported.

3. **External backup before rewind**: Outside ADK's scope, doesn't integrate with artifact versioning, and requires users to implement their own session serialization.

**Additional context**

- Related existing feature: `rewind_async()` in `runners.py:509-647` - similar pattern could be followed
- Use cases: conversation branching UI, A/B testing agent responses, debugging/development snapshots
- I'm willing to contribute this feature if the design is approved

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.