Threaded Conversations
A thread is a chain of conversation turns where each turn knows which message it follows. We'll use cross.stream to store those turns so we can rebuild the conversation history at any point.
For a full-featured project that manages LLM conversations this way, see gpt2099.
Serve
Start a local store in a separate terminal:
Record a conversation
In another terminal running Nushell, import xs.nu for some handy commands:
use xs.nu *
Now append a user message to the chat.turn topic:
| . chat.turn --meta
Grab the ID of that turn so later messages can reference it:
let first = .id
Respond with an assistant reply that continues the first turn:
| . chat.turn --meta
let second = .id
| . chat.turn --meta
let third = .id
| . chat.turn --meta
Following a thread
The continues field forms a linked list. We can walk that list to rebuild the conversation.
Here is a small helper that follows the chain from the most recent turn back to the start:
This prints:
What's the capital of France?
The capital is Paris.
Tell me a famous landmark there.
The Eiffel Tower is one of the most famous.
Branching the conversation
Because each turn records which message it continues from, you can fork a new branch at any point.
Continuing from $third creates a detail thread about the landmark:
| . chat.turn --meta
| . chat.turn --meta
You can also fork from an earlier turn. Continuing again from $second starts a different thread:
| . chat.turn --meta
let g1 = .id
| . chat.turn --meta
| . chat.turn --meta
You will now see multiple branches starting from the earlier messages.
flowchart TD
A[Message ID 1: Start] -->|continues| B[Message ID 2]
B -->|continues| C[Message ID 3]
C -->|continues| D[Message ID 4]
C -->|forks| E[Message ID 5: Detail Thread 1]
E -->|continues| F[Message ID 6: Detail Thread 1]
B -->|forks| G[Message ID 7: Detail Thread 2]
G -->|continues| H[Message ID 8: Detail Thread 2]
H -->|continues| I[Message ID 9: Detail Thread 2]