@@ -37,7 +37,7 @@ type StepConfig<T = any> = {
3737type WorkflowConfig = {
3838 debug ?: boolean ;
3939 name ?: string ;
40- langbase : Langbase ;
40+ langbase ? : Langbase ; // Now optional for backward compatibility
4141} ;
4242
4343class TimeoutError extends Error {
@@ -51,9 +51,9 @@ export class Workflow {
5151 private context : WorkflowContext ;
5252 private debug : boolean ;
5353 private name : string ;
54- private traceManager : TraceManager ;
55- private traceId : string ;
56- private langbase : Langbase ;
54+ private traceManager ? : TraceManager ; // Optional
55+ private traceId ? : string ; // Optional
56+ private langbase ? : Langbase ; // Optional
5757
5858 private originalMethods : Map < string , Function > = new Map ( ) ;
5959 public readonly step : < T = any > ( config : StepConfig < T > ) => Promise < T > ;
@@ -63,20 +63,24 @@ export class Workflow {
6363 this . debug = config . debug ?? false ;
6464 this . name = config . name ?? 'workflow' ;
6565 this . langbase = config . langbase ;
66- this . traceManager = new TraceManager ( ) ;
67- this . traceId = this . traceManager . createTrace ( 'workflow' , {
68- name : this . name ,
69- } ) ;
70- this . step = this . _step . bind ( this ) ;
7166
72- // Set global debug flag
73- _global . _workflowDebugEnabled = this . debug ;
67+ // Only initialize tracing if langbase is provided
68+ if ( this . langbase ) {
69+ this . traceManager = new TraceManager ( ) ;
70+ this . traceId = this . traceManager . createTrace ( 'workflow' , {
71+ name : this . name ,
72+ } ) ;
73+ // Set global debug flag
74+ _global . _workflowDebugEnabled = this . debug ;
75+ }
76+ this . step = this . _step . bind ( this ) ;
7477 }
7578
7679 /**
7780 * Replace a method in the Langbase instance with a traced version
7881 */
7982 private interceptMethod ( obj : any , method : string , path : string = '' ) : void {
83+ if ( ! this . langbase ) return ; // Skip if no langbase instance provided (no tracing)
8084 if ( ! obj || typeof obj [ method ] !== 'function' ) return ;
8185
8286 const fullPath = path ? `${ path } .${ method } ` : method ;
@@ -146,6 +150,7 @@ export class Workflow {
146150 * Restore all original methods that were intercepted
147151 */
148152 private restoreOriginalMethods ( ) : void {
153+ if ( ! this . langbase ) return ; // Skip if no langbase (no tracing)
149154 this . originalMethods . forEach ( ( originalMethod , path ) => {
150155 // Parse the path to find the object and method
151156 const parts = path . split ( '.' ) ;
@@ -179,6 +184,7 @@ export class Workflow {
179184 * Intercept all important methods in the Langbase instance
180185 */
181186 private setupMethodInterceptors ( ) : void {
187+ if ( ! this . langbase ) return ; // Skip if no langbase (no tracing)
182188 // Agent methods
183189 this . interceptMethod ( this . langbase . agent , 'run' , 'agent' ) ;
184190
@@ -241,12 +247,12 @@ export class Workflow {
241247 ? config . retries . limit + 1
242248 : 1 ;
243249
244- // Set up method interceptors before running the step
245- this . setupMethodInterceptors ( ) ;
250+ // Set up method interceptors before running the step (only if tracing)
251+ if ( this . langbase ) this . setupMethodInterceptors ( ) ;
246252
247- // Set the global active trace collector
253+ // Set the global active trace collector (only if tracing)
248254 const previousTraceCollector = _global . _activeTraceCollector ;
249- _global . _activeTraceCollector = collectTrace ;
255+ if ( this . langbase ) _global . _activeTraceCollector = collectTrace ;
250256
251257 try {
252258 // Execute the step function directly
@@ -281,29 +287,32 @@ export class Workflow {
281287 }
282288 }
283289
284- // Create step trace
285- const stepEndTime = Date . now ( ) ;
286- const stepTrace : StepTrace = {
287- name : config . id ,
288- output : result ,
289- traces : stepTraces . length > 0 ? stepTraces : null ,
290- duration : stepEndTime - stepStartTime ,
291- startTime : stepStartTime ,
292- endTime : stepEndTime ,
293- } ;
294-
295- // Add step to trace manager
296- this . traceManager . addStep ( this . traceId , stepTrace ) ;
297-
298- // Restore original methods and trace collector
299- this . restoreOriginalMethods ( ) ;
300- _global . _activeTraceCollector = previousTraceCollector ;
290+ // Create step trace (only if tracing)
291+ if ( this . langbase && this . traceManager && this . traceId ) {
292+ const stepEndTime = Date . now ( ) ;
293+ const stepTrace : StepTrace = {
294+ name : config . id ,
295+ output : result ,
296+ traces : stepTraces . length > 0 ? stepTraces : null ,
297+ duration : stepEndTime - stepStartTime ,
298+ startTime : stepStartTime ,
299+ endTime : stepEndTime ,
300+ } ;
301+ this . traceManager . addStep ( this . traceId , stepTrace ) ;
302+ }
301303
304+ // Restore original methods and trace collector (only if tracing)
305+ if ( this . langbase ) {
306+ this . restoreOriginalMethods ( ) ;
307+ _global . _activeTraceCollector = previousTraceCollector ;
308+ }
302309 return result ;
303310 } catch ( error ) {
304- // Restore original methods and trace collector on error
305- this . restoreOriginalMethods ( ) ;
306- _global . _activeTraceCollector = previousTraceCollector ;
311+ // Restore original methods and trace collector on error (only if tracing)
312+ if ( this . langbase ) {
313+ this . restoreOriginalMethods ( ) ;
314+ _global . _activeTraceCollector = previousTraceCollector ;
315+ }
307316
308317 // Store error for potential retry or final throw
309318 lastError = error as Error ;
@@ -379,6 +388,8 @@ export class Workflow {
379388 }
380389
381390 public async end ( ) : Promise < void > {
391+ // If tracing is not enabled, do nothing (no-op for backward compatibility)
392+ if ( ! this . langbase || ! this . traceManager || ! this . traceId ) return ;
382393 // Finalise and grab the trace
383394 this . traceManager . endTrace ( this . traceId ) ;
384395 this . traceManager . printTrace ( this . traceId ) ;
0 commit comments