(userPrompt: string)
| 175 | * Sets the interaction context for all subsequent operations. |
| 176 | */ |
| 177 | export function startInteractionSpan(userPrompt: string): Span { |
| 178 | ensureCleanupInterval() |
| 179 | |
| 180 | // Start Perfetto span regardless of OTel tracing state |
| 181 | const perfettoSpanId = isPerfettoTracingEnabled() |
| 182 | ? startInteractionPerfettoSpan(userPrompt) |
| 183 | : undefined |
| 184 | |
| 185 | if (!isAnyTracingEnabled()) { |
| 186 | // Still track Perfetto span even if OTel is disabled |
| 187 | if (perfettoSpanId) { |
| 188 | const dummySpan = trace.getActiveSpan() || getTracer().startSpan('dummy') |
| 189 | const spanId = getSpanId(dummySpan) |
| 190 | const spanContextObj: SpanContext = { |
| 191 | span: dummySpan, |
| 192 | startTime: Date.now(), |
| 193 | attributes: {}, |
| 194 | perfettoSpanId, |
| 195 | } |
| 196 | activeSpans.set(spanId, new WeakRef(spanContextObj)) |
| 197 | interactionContext.enterWith(spanContextObj) |
| 198 | return dummySpan |
| 199 | } |
| 200 | return trace.getActiveSpan() || getTracer().startSpan('dummy') |
| 201 | } |
| 202 | |
| 203 | const tracer = getTracer() |
| 204 | const isUserPromptLoggingEnabled = isEnvTruthy( |
| 205 | process.env.OTEL_LOG_USER_PROMPTS, |
| 206 | ) |
| 207 | const promptToLog = isUserPromptLoggingEnabled ? userPrompt : '<REDACTED>' |
| 208 | |
| 209 | interactionSequence++ |
| 210 | |
| 211 | const attributes = createSpanAttributes('interaction', { |
| 212 | user_prompt: promptToLog, |
| 213 | user_prompt_length: userPrompt.length, |
| 214 | 'interaction.sequence': interactionSequence, |
| 215 | }) |
| 216 | |
| 217 | const span = tracer.startSpan('ncode.interaction', { |
| 218 | attributes, |
| 219 | }) |
| 220 | |
| 221 | // Add experimental attributes (new_context) |
| 222 | addBetaInteractionAttributes(span, userPrompt) |
| 223 | |
| 224 | const spanId = getSpanId(span) |
| 225 | const spanContextObj: SpanContext = { |
| 226 | span, |
| 227 | startTime: Date.now(), |
| 228 | attributes, |
| 229 | perfettoSpanId, |
| 230 | } |
| 231 | activeSpans.set(spanId, new WeakRef(spanContextObj)) |
| 232 | |
| 233 | interactionContext.enterWith(spanContextObj) |
| 234 |
no test coverage detected