11import {
22 getActiveTraceId ,
3+ type LangfuseSpan ,
4+ type LangfuseTool ,
35 propagateAttributes ,
46 startActiveObservation ,
57} from "@langfuse/tracing" ;
@@ -91,13 +93,14 @@ async function generateChildObservation(
9193) : Promise < void > {
9294 const type = observationType ( traceIndex , childIndex ) ;
9395 const name = `${ type } step ${ childIndex } ` ;
94- await startActiveObservation (
95- name ,
96- async ( span ) => {
97- if ( type === "generation" ) {
96+
97+ if ( type === "generation" ) {
98+ await startActiveObservation (
99+ name ,
100+ async ( generation ) => {
98101 const inputTokens = 120 + ( ( traceIndex + childIndex ) % 700 ) ;
99102 const outputTokens = 40 + ( ( traceIndex * 3 + childIndex ) % 260 ) ;
100- span . update ( {
103+ generation . update ( {
101104 model : `gpt-perf-${ ( traceIndex + childIndex ) % 4 } ` ,
102105 input : makeMessages ( traceIndex , childIndex ) ,
103106 output : {
@@ -120,32 +123,43 @@ async function generateChildObservation(
120123 total : roundCost ( inputTokens * 0.0000015 + outputTokens * 0.000006 ) ,
121124 } ,
122125 } ) ;
123- return ;
124- }
126+ } ,
127+ { asType : "generation" } ,
128+ ) ;
129+ return ;
130+ }
125131
126- span . update ( {
127- input : {
128- step : childIndex ,
129- payload : sizedText ( "step input" , traceIndex , childIndex ) ,
130- } ,
131- output : {
132- ok : true ,
133- items : Array . from ( { length : 1 + ( childIndex % 4 ) } , ( _ , offset ) => ( {
134- id : `item-${ traceIndex } -${ childIndex } -${ offset } ` ,
135- score : Number ( ( ( traceIndex + offset ) % 100 ) / 100 ) . toFixed ( 2 ) ,
136- } ) ) ,
137- } ,
138- metadata : {
139- component : type ,
140- retry_count : traceIndex % 5 === 0 ? 1 : 0 ,
141- latency_bucket : [ "fast" , "normal" , "slow" ] [
142- ( traceIndex + childIndex ) % 3
143- ] ,
144- } ,
145- } ) ;
146- } ,
147- { asType : type } ,
148- ) ;
132+ const updateSpanLikeObservation = async ( span : LangfuseSpan | LangfuseTool ) => {
133+ span . update ( {
134+ input : {
135+ step : childIndex ,
136+ payload : sizedText ( "step input" , traceIndex , childIndex ) ,
137+ } ,
138+ output : {
139+ ok : true ,
140+ items : Array . from ( { length : 1 + ( childIndex % 4 ) } , ( _ , offset ) => ( {
141+ id : `item-${ traceIndex } -${ childIndex } -${ offset } ` ,
142+ score : Number ( ( ( traceIndex + offset ) % 100 ) / 100 ) . toFixed ( 2 ) ,
143+ } ) ) ,
144+ } ,
145+ metadata : {
146+ component : type ,
147+ retry_count : traceIndex % 5 === 0 ? 1 : 0 ,
148+ latency_bucket : [ "fast" , "normal" , "slow" ] [
149+ ( traceIndex + childIndex ) % 3
150+ ] ,
151+ } ,
152+ } ) ;
153+ } ;
154+
155+ if ( type === "tool" ) {
156+ await startActiveObservation ( name , updateSpanLikeObservation , {
157+ asType : "tool" ,
158+ } ) ;
159+ return ;
160+ }
161+
162+ await startActiveObservation ( name , updateSpanLikeObservation ) ;
149163}
150164
151165function observationType (
0 commit comments