cloudflare / cloudflare/agents

think: session branching + tree demo

Open
#1,351 0 comments 0 reactions 1 assignee Claimed by @threepointone View on GitHub
enhancement on the roadmap think
Dominant language
TypeScript
Stars
5.6k
Forks
711
Avg merge
1d 20h
Merged PRs (30d)
53

Description

Parent roadmap: #1439

Related: #1375

## Summary

Build/fix the Think session branching + tree demo so regeneration and branch selection render the correct conversation path, not just the selected assistant bubble.

Think’s Session layer already stores messages as a tree and regeneration creates sibling assistant branches. The remaining demo/product issue is making the UI accurately represent that tree when a user switches between regenerated answers.

## Verified current state

Server-side behavior appears to exist and is tested:

- `packages/think/src/think.ts` handles `trigger: "regenerate-message"` by passing a `parentId` into `_streamResult(...)`.
- `_streamResult(...)` appends generated assistant messages with that `parentId`, so regenerated responses branch from the same user message.
- `packages/think/src/tests/client-tools.test.ts` includes regeneration tests that verify sibling branches, multiple regenerations, preserved pre-branch history, and `getBranches(...)` behavior.
- `examples/assistant/src/server.ts` exposes `getResponseVersions(userMessageId)` by returning `this.session.getBranches(userMessageId)`.

The likely bug is in the example UI:

- `examples/assistant/src/client.tsx` stores branch versions per user message and lets users select previous/next response versions.
- Rendering computes `displayMessage` by swapping the immediate assistant message with the selected branch version.
- The displayed `messages` array still comes from the latest leaf path, and selecting an older assistant branch does not appear to fetch/render that branch’s descendant path.

So if a conversation branches and later descendants differ, switching to an older answer can show the old answer while still showing descendants from the latest branch. We should verify manually, but the current code strongly suggests this limitation.

## Desired behavior

When a user selects a regenerated answer/version:

- The visible conversation should switch to the path rooted at that selected branch.
- Descendant messages should be the descendants/leaves for the selected branch, not stale descendants from the latest branch.
- Branch controls should appear at the correct user/assistant pairs.
- Switching back to the latest version should restore the latest-leaf path.
- The UI should make it clear which branch/version is selected.

Example:

```txt
U1: Question
A1-v1: Answer 1
U2: Follow-up on v1
A2: v1 follow-up answer
A1-v2: Answer 2
U3: Follow-up on v2
A3: v2 follow-up answer
```

If the user selects `A1-v1`, the UI should show `U1 -> A1-v1 -> U2 -> A2`, not `U1 -> A1-v1 -> U3 -> A3`.

## Implementation directions to consider

- Add a callable that returns `session.getHistory(leafId)` for a selected leaf/version and have the UI render that path.
- Track selected branch leaf IDs rather than only replacing a single message in the latest `messages` array.
- Add server/client helpers for branch tree metadata: parent id, children, selected leaf, latest leaf.
- Keep the simple latest-leaf streaming path unchanged for normal chat.
- Decide whether branch selection is purely local UI state or should update active session state on the server.

## Acceptance criteria

- The assistant demo shows branch/version controls for regenerated answers.
- Selecting a version updates the rendered descendant path to the correct branch/leaf.
- Regenerating from a selected branch creates a sibling at the correct point.
- Clearing history or switching chats resets branch UI state.
- Add a test or e2e scenario for at least: generate answer, regenerate, continue from one branch, switch branch, verify descendants change.
- Document any limitations if full arbitrary tree navigation is deferred.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.