11import { generateObject } from "ai" ;
22import { z } from "zod" ;
33import { createLogger , DEBUG } from "@/lib/logger" ;
4- import {
5- endActiveSpan ,
6- flushSpanProcessor ,
7- updateObservation ,
8- updateTrace ,
9- } from "@/lib/observability/telemetry-helpers" ;
104import { getAIModel } from "@/lib/providers/model-factory" ;
115import type { AIProvider } from "@/lib/providers/registry" ;
126import type { CompressedFieldData } from "@/types/autofill" ;
@@ -148,6 +142,9 @@ Be precise and consider context. For example:
148142 : `Information: ${ answer } ` ;
149143
150144 if ( DEBUG ) {
145+ const { updateObservation, updateTrace } = await import (
146+ "@/lib/observability/telemetry-helpers"
147+ ) ;
151148 await updateObservation ( {
152149 input : { answer, question } ,
153150 } ) ;
@@ -177,6 +174,9 @@ Be precise and consider context. For example:
177174 } ) ;
178175
179176 if ( DEBUG ) {
177+ const { updateObservation, updateTrace, endActiveSpan } = await import (
178+ "@/lib/observability/telemetry-helpers"
179+ ) ;
180180 await updateObservation ( {
181181 output : result . object ,
182182 } ) ;
@@ -192,6 +192,9 @@ Be precise and consider context. For example:
192192
193193 if ( DEBUG ) {
194194 try {
195+ const { updateObservation, updateTrace, endActiveSpan } = await import (
196+ "@/lib/observability/telemetry-helpers"
197+ ) ;
195198 await updateObservation ( {
196199 output : error ,
197200 level : "ERROR" ,
@@ -212,6 +215,9 @@ Be precise and consider context. For example:
212215 } finally {
213216 if ( DEBUG ) {
214217 try {
218+ const { flushSpanProcessor } = await import (
219+ "@/lib/observability/telemetry-helpers"
220+ ) ;
215221 await flushSpanProcessor ( ) ;
216222 } catch ( telemetryError ) {
217223 logger . error (
@@ -333,6 +339,9 @@ export const rephraseAgent = async (
333339 const userPrompt = `Original Question: "${ question || "Not provided" } "\nOriginal Answer: "${ answer } "` ;
334340
335341 if ( DEBUG ) {
342+ const { updateObservation, updateTrace } = await import (
343+ "@/lib/observability/telemetry-helpers"
344+ ) ;
336345 await updateObservation ( {
337346 input : { answer, question } ,
338347 } ) ;
@@ -353,6 +362,9 @@ export const rephraseAgent = async (
353362 } ) ;
354363
355364 if ( DEBUG ) {
365+ const { updateObservation, updateTrace, endActiveSpan } = await import (
366+ "@/lib/observability/telemetry-helpers"
367+ ) ;
356368 await updateObservation ( {
357369 output : object ,
358370 } ) ;
@@ -368,6 +380,9 @@ export const rephraseAgent = async (
368380
369381 if ( DEBUG ) {
370382 try {
383+ const { updateObservation, updateTrace, endActiveSpan } = await import (
384+ "@/lib/observability/telemetry-helpers"
385+ ) ;
371386 await updateObservation ( {
372387 output : error ,
373388 level : "ERROR" ,
@@ -385,6 +400,9 @@ export const rephraseAgent = async (
385400 } finally {
386401 if ( DEBUG ) {
387402 try {
403+ const { flushSpanProcessor } = await import (
404+ "@/lib/observability/telemetry-helpers"
405+ ) ;
388406 await flushSpanProcessor ( ) ;
389407 } catch ( telemetryError ) {
390408 logger . error ( "Telemetry flush error in rephrase:" , telemetryError ) ;
0 commit comments