Skip to content

Commit 70f21fe

Browse files
committed
📦 NEW: Add branching example
1 parent 3c90ea0 commit 70f21fe

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import dotenv from 'dotenv';
2+
import {Langbase, ThreadMessage} from 'langbase';
3+
4+
dotenv.config();
5+
6+
const langbase = new Langbase({
7+
apiKey: process.env.LANGBASE_API_KEY!,
8+
});
9+
10+
// Helper to create initial conversation
11+
async function createConversation() {
12+
const thread = await langbase.threads.create({
13+
messages: [
14+
{
15+
role: 'user',
16+
content: 'I need to add state management to my React app',
17+
},
18+
{
19+
role: 'assistant',
20+
content:
21+
'I can help you with state management in React. How complex is your app \
22+
and what are your main requirements?',
23+
},
24+
{
25+
role: 'user',
26+
content:
27+
"It's a medium-sized app with user data, API calls, and real-time updates",
28+
},
29+
{
30+
role: 'assistant',
31+
content:
32+
'For your needs, you could use Redux for its mature ecosystem, or Zustand \
33+
for a simpler, more modern approach. Which direction interests you?',
34+
},
35+
],
36+
});
37+
38+
return thread.id;
39+
}
40+
41+
// Branch thread at decision point
42+
async function branchThread(threadId: string, branchAt: number) {
43+
// Get all messages
44+
const messages = await langbase.threads.messages.list({threadId});
45+
46+
// Take messages up to branch point
47+
const messagesToKeep = messages.slice(0, branchAt);
48+
49+
// Create new thread with selected messages
50+
const branch = await langbase.threads.create({
51+
messages: messagesToKeep as ThreadMessage[],
52+
metadata: {
53+
parent: threadId,
54+
branchedAt: branchAt.toString(),
55+
},
56+
});
57+
58+
return branch.id;
59+
}
60+
61+
async function main() {
62+
// Create original conversation
63+
const originalId = await createConversation();
64+
65+
// Branch at decision point (after state management options presented)
66+
const branchId = await branchThread(originalId, 4);
67+
68+
// Continue original thread with Redux
69+
await langbase.threads.append({
70+
threadId: originalId,
71+
messages: [
72+
{role: 'user', content: "Let's go with Redux"},
73+
{
74+
role: 'assistant',
75+
content:
76+
'Great choice for a robust solution! Redux with Redux Toolkit makes it \
77+
much easier. Let me show you the setup...',
78+
},
79+
],
80+
});
81+
82+
// Branch explores Zustand
83+
await langbase.threads.append({
84+
threadId: branchId,
85+
messages: [
86+
{role: 'user', content: 'Tell me about Zustand'},
87+
{
88+
role: 'assistant',
89+
content:
90+
"Zustand is lightweight and simple! It's only 2KB and doesn't need \
91+
providers. Here's how to get started...",
92+
},
93+
],
94+
});
95+
96+
console.log('\nOriginal thread:', originalId);
97+
console.log(
98+
'Original thread messages:',
99+
await langbase.threads.messages.list({threadId: originalId}),
100+
);
101+
console.log('Branched thread:', branchId);
102+
console.log(
103+
'Branched thread messages:',
104+
await langbase.threads.messages.list({threadId: branchId}),
105+
);
106+
}
107+
108+
main();

0 commit comments

Comments
 (0)