33 propagateAttributes ,
44 startActiveObservation ,
55} from "@langfuse/tracing" ;
6+ import type { LangfuseSpanAttributes } from "@langfuse/tracing" ;
67
78type ObservationType = "span" | "generation" | "tool" ;
89
@@ -91,10 +92,10 @@ async function generateChildObservation(
9192) : Promise < void > {
9293 const type = observationType ( traceIndex , childIndex ) ;
9394 const name = `${ type } step ${ childIndex } ` ;
94- await startActiveObservation (
95- name ,
96- async ( span ) => {
97- if ( type === "generation" ) {
95+ if ( type === "generation" ) {
96+ await startActiveObservation (
97+ name ,
98+ async ( span ) => {
9899 const inputTokens = 120 + ( ( traceIndex + childIndex ) % 700 ) ;
99100 const outputTokens = 40 + ( ( traceIndex * 3 + childIndex ) % 260 ) ;
100101 span . update ( {
@@ -120,32 +121,41 @@ async function generateChildObservation(
120121 total : roundCost ( inputTokens * 0.0000015 + outputTokens * 0.000006 ) ,
121122 } ,
122123 } ) ;
123- return ;
124- }
124+ } ,
125+ { asType : "generation" } ,
126+ ) ;
127+ return ;
128+ }
125129
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- ) ;
130+ const updateSpan = async ( span : {
131+ update ( attributes : LangfuseSpanAttributes ) : unknown ;
132+ } ) => {
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+ if ( type === "tool" ) {
155+ await startActiveObservation ( name , updateSpan , { asType : "tool" } ) ;
156+ return ;
157+ }
158+ await startActiveObservation ( name , updateSpan ) ;
149159}
150160
151161function observationType (
0 commit comments