@@ -32,35 +32,42 @@ class ChatbotWithAssociations {
3232 console . log ( `User ID: ${ this . userId } ` ) ;
3333 console . log ( `Session ID: ${ this . sessionId } \n` ) ;
3434
35- // Set associations at the beginning of the conversation
35+ // Set standard associations at the beginning of the conversation
3636 // These will be automatically attached to all spans within this context
3737 traceloop . Associations . set ( [
3838 [ traceloop . AssociationProperty . CONVERSATION_ID , this . conversationId ] ,
3939 [ traceloop . AssociationProperty . USER_ID , this . userId ] ,
4040 [ traceloop . AssociationProperty . SESSION_ID , this . sessionId ] ,
4141 ] ) ;
4242
43- // First message
44- const greeting = await this . sendMessage (
45- "Hello! What's the weather like today?" ,
43+ // Use withAssociationProperties to add custom properties
44+ // Custom properties (like chat_subject) will be prefixed with traceloop.association.properties
45+ return traceloop . withAssociationProperties (
46+ { chat_subject : "general" } ,
47+ async ( ) => {
48+ // First message
49+ const greeting = await this . sendMessage (
50+ "Hello! What's the weather like today?" ,
51+ ) ;
52+ console . log ( `Bot: ${ greeting } \n` ) ;
53+
54+ // Second message in the same conversation
55+ const followup = await this . sendMessage (
56+ "What should I wear for that weather?" ,
57+ ) ;
58+ console . log ( `Bot: ${ followup } \n` ) ;
59+
60+ // Third message
61+ const final = await this . sendMessage ( "Thanks for the advice!" ) ;
62+ console . log ( `Bot: ${ final } \n` ) ;
63+
64+ return {
65+ greeting,
66+ followup,
67+ final,
68+ } ;
69+ } ,
4670 ) ;
47- console . log ( `Bot: ${ greeting } \n` ) ;
48-
49- // Second message in the same conversation
50- const followup = await this . sendMessage (
51- "What should I wear for that weather?" ,
52- ) ;
53- console . log ( `Bot: ${ followup } \n` ) ;
54-
55- // Third message
56- const final = await this . sendMessage ( "Thanks for the advice!" ) ;
57- console . log ( `Bot: ${ final } \n` ) ;
58-
59- return {
60- greeting,
61- followup,
62- final,
63- } ;
6471 }
6572
6673 /**
@@ -138,7 +145,7 @@ async function main() {
138145 console . log ( "============================================" ) ;
139146
140147 try {
141- // Example 1: Multi-turn chatbot conversation
148+ // Example 1: Multi-turn chatbot conversation with custom properties
142149 const chatbot = new ChatbotWithAssociations (
143150 "conv-abc-123" , // conversation_id
144151 "user-alice-456" , // user_id
@@ -155,7 +162,7 @@ async function main() {
155162 "Check your Traceloop dashboard to see the associations attached to traces!" ,
156163 ) ;
157164 console . log (
158- "You can filter and search by conversation_id, user_id, session_id, or customer_id ." ,
165+ "You can filter and search by conversation_id, user_id, session_id, customer_id, or custom properties like chat_subject ." ,
159166 ) ;
160167 } catch ( error ) {
161168 console . error ( "Error running demo:" , error ) ;
0 commit comments