neatlogs 1.1.21 → 1.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -536,7 +536,7 @@ matrix; API-shaped rows deliberately make no blanket semver claim.
536
536
  | Google GenAI | `@google/genai` 1.34.x | `wrapGoogleGenAI(client)` / `wrapGoogleGenAIChat(chat)` | `neatlogs/google-genai` |
537
537
  | Vertex AI through `@google/genai` | `@google/genai` 1.34.x | `wrapVertexAI(client)` / `wrapVertexAIChat(chat)` | `neatlogs/vertex-ai` |
538
538
  | OpenRouter Agent | `@openrouter/agent` 0.7.x | `wrapOpenRouterAgent(client)` / `wrapCallModel(fn)` | `neatlogs/openrouter-agent` |
539
- | Vercel AI SDK | `ai` 6.x | `wrapAISDK(ai)` | `neatlogs/ai` |
539
+ | Vercel AI SDK | `ai` 6.x and 7.x | `wrapAISDK(ai)` | `neatlogs/ai` |
540
540
  | Mastra | `@mastra/core` 1.32.x | `wrapMastra(entity)` / `wrapMastraRerank(fn)` | `neatlogs/mastra` |
541
541
  | Claude Agent SDK | documented `query()` API | `wrapClaudeAgentSDK(sdk)` | `neatlogs/claude-agent-sdk` |
542
542
  | LangChain / LangGraph | `@langchain/core` 0.3.x | `langchainHandler()` callback | `neatlogs` or `neatlogs/langchain` |
@@ -565,22 +565,25 @@ const { text } = await generateText({
565
565
  prompt: 'What is TypeScript?',
566
566
  });
567
567
 
568
- // AI SDK v6 agents are supported too. The wrapper injects telemetry into the
569
- // constructor settings, including calls returned from a custom prepareCall.
568
+ // Agents are supported in both versions. The wrapper selects `telemetry` for
569
+ // AI SDK v7 and `experimental_telemetry` for AI SDK v6 automatically.
570
570
  const agent = new ToolLoopAgent({
571
571
  id: 'support-agent',
572
572
  model: openai('gpt-4o-mini'),
573
- experimental_telemetry: { functionId: 'support-agent' },
573
+ telemetry: { functionId: 'support-agent' }, // use experimental_telemetry on v6
574
574
  });
575
575
  await agent.generate({ prompt: 'Help me debug my order' });
576
576
 
577
577
  await shutdown();
578
578
  ```
579
579
 
580
- If a call already supplies an `experimental_telemetry.tracer` (for example,
581
- Laminar), the wrapper mirrors the AI SDK's native spans to both that tracer and
582
- Neatlogs. The caller-owned tracer remains the global context owner; Neatlogs
583
- keeps separate parent context and export state in its private provider.
580
+ AI SDK v7 users must also install the telemetry adapter with
581
+ `npm install @ai-sdk/otel`. AI SDK v6 users do not need that package.
582
+
583
+ If a call supplies a tracer through `createAITelemetry` (for example, Laminar),
584
+ the wrapper mirrors the AI SDK's native spans to both that tracer and Neatlogs.
585
+ The caller-owned tracer remains the global context owner; Neatlogs keeps
586
+ separate parent context and export state in its private provider.
584
587
 
585
588
  The same coexistence is available without the wrapper by passing the existing
586
589
  tracer to `createAITelemetry`:
@@ -593,7 +596,7 @@ import { createAITelemetry } from 'neatlogs/ai';
593
596
  await streamText({
594
597
  model,
595
598
  prompt,
596
- experimental_telemetry: createAITelemetry({
599
+ telemetry: createAITelemetry({ // use experimental_telemetry on AI SDK v6
597
600
  tracer: getTracer(),
598
601
  functionId: 'progress-narration',
599
602
  }),
package/dist/ai-sdk.cjs CHANGED
@@ -126,10 +126,105 @@ function withNeatlogsSpan(span, fn, baseContext, rootSpan) {
126
126
 
127
127
  // src/ai-sdk.ts
128
128
  var TRACER_NAME = "neatlogs.ai-sdk";
129
+ var NEATLOGS_V7_INTEGRATION = /* @__PURE__ */ Symbol("neatlogs.ai-sdk.v7-integration");
130
+ var AI_SDK_OTEL_MODULE = "@ai-sdk/otel";
131
+ var LazyV7OpenTelemetryIntegration = class {
132
+ constructor(tracer, metadata) {
133
+ this.tracer = tracer;
134
+ this.metadata = metadata;
135
+ }
136
+ tracer;
137
+ metadata;
138
+ [NEATLOGS_V7_INTEGRATION] = true;
139
+ delegatePromise;
140
+ getDelegate() {
141
+ return this.delegatePromise ??= import(AI_SDK_OTEL_MODULE).then(
142
+ ({ OpenTelemetry }) => new OpenTelemetry({
143
+ tracer: this.tracer,
144
+ enrichSpan: () => this.metadata
145
+ })
146
+ ).catch((error) => {
147
+ const detail = error instanceof Error ? `: ${error.message}` : "";
148
+ throw new Error(
149
+ "Vercel AI SDK v7 telemetry requires the optional @ai-sdk/otel peer dependency" + detail
150
+ );
151
+ });
152
+ }
153
+ async notify(method, event) {
154
+ const delegate = await this.getDelegate();
155
+ const fn = delegate[method];
156
+ if (typeof fn === "function") {
157
+ await Reflect.apply(fn, delegate, [event]);
158
+ }
159
+ }
160
+ onStart(event) {
161
+ return this.notify("onStart", event);
162
+ }
163
+ onStepStart(event) {
164
+ return this.notify("onStepStart", event);
165
+ }
166
+ onLanguageModelCallStart(event) {
167
+ return this.notify("onLanguageModelCallStart", event);
168
+ }
169
+ onLanguageModelCallEnd(event) {
170
+ return this.notify("onLanguageModelCallEnd", event);
171
+ }
172
+ onToolExecutionStart(event) {
173
+ return this.notify("onToolExecutionStart", event);
174
+ }
175
+ onToolExecutionEnd(event) {
176
+ return this.notify("onToolExecutionEnd", event);
177
+ }
178
+ onStepEnd(event) {
179
+ return this.notify("onStepEnd", event);
180
+ }
181
+ onStepFinish(event) {
182
+ return this.notify("onStepFinish", event);
183
+ }
184
+ onObjectStepStart(event) {
185
+ return this.notify("onObjectStepStart", event);
186
+ }
187
+ onObjectStepEnd(event) {
188
+ return this.notify("onObjectStepEnd", event);
189
+ }
190
+ onEmbedStart(event) {
191
+ return this.notify("onEmbedStart", event);
192
+ }
193
+ onEmbedEnd(event) {
194
+ return this.notify("onEmbedEnd", event);
195
+ }
196
+ onRerankStart(event) {
197
+ return this.notify("onRerankStart", event);
198
+ }
199
+ onRerankEnd(event) {
200
+ return this.notify("onRerankEnd", event);
201
+ }
202
+ onEnd(event) {
203
+ return this.notify("onEnd", event);
204
+ }
205
+ onAbort(event) {
206
+ return this.notify("onAbort", event);
207
+ }
208
+ onError(event) {
209
+ return this.notify("onError", event);
210
+ }
211
+ async executeLanguageModelCall(options) {
212
+ const delegate = await this.getDelegate();
213
+ const fn = delegate.executeLanguageModelCall;
214
+ return typeof fn === "function" ? Reflect.apply(fn, delegate, [options]) : options.execute();
215
+ }
216
+ async executeTool(options) {
217
+ const delegate = await this.getDelegate();
218
+ const fn = delegate.executeTool;
219
+ return typeof fn === "function" ? Reflect.apply(fn, delegate, [options]) : options.execute();
220
+ }
221
+ };
129
222
  function createAITelemetry(opts = {}) {
130
223
  const userMeta = opts.metadata ?? {};
131
224
  const neatlogsTracer = getRoutingNeatlogsTracer(TRACER_NAME);
132
225
  const callerTracer = opts.tracer;
226
+ const tracer = callerTracer ? createMirroredTracer(callerTracer, neatlogsTracer) : neatlogsTracer;
227
+ const metadata = callerTracer ? { ...userMeta } : { ...userMeta, neatlogsWrapped: true };
133
228
  return {
134
229
  isEnabled: true,
135
230
  recordInputs: true,
@@ -138,11 +233,12 @@ function createAITelemetry(opts = {}) {
138
233
  // internally, which would otherwise parent its native spans from the foreign
139
234
  // global context AND push them onto it (so a co-tenant's next span inherits
140
235
  // ours). The facade routes both through the private Neatlogs context.
141
- tracer: callerTracer ? createMirroredTracer(callerTracer, neatlogsTracer) : neatlogsTracer,
236
+ tracer,
142
237
  ...opts.functionId !== void 0 ? { functionId: opts.functionId } : {},
143
238
  // The marker is an implementation detail used only when Neatlogs owns the
144
239
  // telemetry stream. Do not leak it into caller-owned providers.
145
- metadata: callerTracer ? { ...userMeta } : { ...userMeta, neatlogsWrapped: true }
240
+ metadata,
241
+ integrations: [new LazyV7OpenTelemetryIntegration(tracer, metadata)]
146
242
  };
147
243
  }
148
244
  function safeStringify(value) {
@@ -175,7 +271,7 @@ function setOutputValue(span, result) {
175
271
  span.setAttribute("output.value", text);
176
272
  }
177
273
  if (r.finishReason) {
178
- span.setAttribute("gen_ai.finish_reason", String(r.finishReason));
274
+ span.setAttribute("neatlogs.llm.finish_reason", String(r.finishReason));
179
275
  }
180
276
  return;
181
277
  }
@@ -184,7 +280,7 @@ function setOutputValue(span, result) {
184
280
  span.setAttribute("output.value", safeStringify(r.object));
185
281
  }
186
282
  if (r.finishReason) {
187
- span.setAttribute("gen_ai.finish_reason", String(r.finishReason));
283
+ span.setAttribute("neatlogs.llm.finish_reason", String(r.finishReason));
188
284
  }
189
285
  return;
190
286
  }
@@ -204,7 +300,7 @@ function setStreamOutputValue(span, event) {
204
300
  if (stringified) span.setAttribute("output.value", stringified);
205
301
  }
206
302
  if (e.finishReason) {
207
- span.setAttribute("gen_ai.finish_reason", String(e.finishReason));
303
+ span.setAttribute("neatlogs.llm.finish_reason", String(e.finishReason));
208
304
  }
209
305
  }
210
306
  var WRAPPED_FUNCTIONS = [
@@ -224,6 +320,8 @@ var wrappedExports = /* @__PURE__ */ new WeakSet();
224
320
  var wrapperByOriginal = /* @__PURE__ */ new WeakMap();
225
321
  function wrapAISDK(aiModule) {
226
322
  const wrapped = { ...aiModule };
323
+ const hasRegisterTelemetry = "registerTelemetry" in aiModule && typeof Reflect.get(aiModule, "registerTelemetry") === "function";
324
+ const telemetryKey = hasRegisterTelemetry ? "telemetry" : "experimental_telemetry";
227
325
  for (const name of WRAPPED_FUNCTIONS) {
228
326
  const original = aiModule[name];
229
327
  if (typeof original !== "function") continue;
@@ -235,12 +333,16 @@ function wrapAISDK(aiModule) {
235
333
  if (name === "streamText" || name === "streamObject") {
236
334
  wrapped[name] = cacheWrapper(
237
335
  original,
238
- createStreamWrapper(name, original)
336
+ createStreamWrapper(name, original, telemetryKey)
239
337
  );
240
338
  } else {
241
339
  wrapped[name] = cacheWrapper(
242
340
  original,
243
- createAsyncWrapper(name, original)
341
+ createAsyncWrapper(
342
+ name,
343
+ original,
344
+ telemetryKey
345
+ )
244
346
  );
245
347
  }
246
348
  }
@@ -250,7 +352,7 @@ function wrapAISDK(aiModule) {
250
352
  const existing = getExistingWrapper(original);
251
353
  wrapped[name] = existing ?? cacheWrapper(
252
354
  original,
253
- createAgentConstructorWrapper(original)
355
+ createAgentConstructorWrapper(original, telemetryKey)
254
356
  );
255
357
  }
256
358
  return wrapped;
@@ -264,26 +366,30 @@ function cacheWrapper(original, wrapped) {
264
366
  wrappedExports.add(wrapped);
265
367
  return wrapped;
266
368
  }
267
- function createAgentConstructorWrapper(original) {
369
+ function createAgentConstructorWrapper(original, telemetryKey) {
268
370
  return new Proxy(original, {
269
371
  construct(target, args, newTarget) {
270
372
  if (args.length === 0 || typeof args[0] !== "object" || args[0] === null) {
271
373
  return Reflect.construct(target, args, newTarget);
272
374
  }
273
- const settings = mergeAgentSettings(args[0]);
375
+ const settings = mergeAgentSettings(args[0], telemetryKey);
274
376
  return Reflect.construct(target, [settings, ...args.slice(1)], newTarget);
275
377
  }
276
378
  });
277
379
  }
278
- function mergeAgentSettings(settings) {
279
- const merged = mergeTelemetry(settings);
380
+ function mergeAgentSettings(settings, telemetryKey) {
381
+ const merged = mergeTelemetry(settings, telemetryKey);
280
382
  const userPrepareCall = settings.prepareCall;
281
383
  if (typeof userPrepareCall !== "function") return merged;
282
384
  return {
283
385
  ...merged,
284
386
  prepareCall: async function wrappedPrepareCall(...args) {
285
387
  const prepared = await Reflect.apply(userPrepareCall, this, args);
286
- return prepared == null ? prepared : mergeTelemetry(prepared, settings.experimental_telemetry);
388
+ return prepared == null ? prepared : mergeTelemetry(
389
+ prepared,
390
+ telemetryKey,
391
+ settings.telemetry ?? settings.experimental_telemetry
392
+ );
287
393
  }
288
394
  };
289
395
  }
@@ -295,7 +401,7 @@ function rootSpanKind(name) {
295
401
  function getParentContext() {
296
402
  return getNeatlogsParentContext();
297
403
  }
298
- function createAsyncWrapper(name, original) {
404
+ function createAsyncWrapper(name, original, telemetryKey) {
299
405
  return async function wrappedAsyncFn(opts) {
300
406
  const tracer = getNeatlogsTracer(TRACER_NAME);
301
407
  const parentContext = getParentContext();
@@ -315,7 +421,7 @@ function createAsyncWrapper(name, original) {
315
421
  if (name === "rerank" && opts?.query) {
316
422
  span.setAttribute("ai.rerank.query", String(opts.query));
317
423
  }
318
- const merged = mergeTelemetry(opts);
424
+ const merged = mergeTelemetry(opts, telemetryKey);
319
425
  const result = await original(merged);
320
426
  if (!isEmbedOrRerank) {
321
427
  setOutputValue(span, result);
@@ -332,7 +438,7 @@ function createAsyncWrapper(name, original) {
332
438
  );
333
439
  };
334
440
  }
335
- function createStreamWrapper(name, original) {
441
+ function createStreamWrapper(name, original, telemetryKey) {
336
442
  return function wrappedStreamFn(opts) {
337
443
  const tracer = getNeatlogsTracer(TRACER_NAME);
338
444
  const parentContext = getParentContext();
@@ -352,7 +458,7 @@ function createStreamWrapper(name, original) {
352
458
  };
353
459
  try {
354
460
  setInputValue(span, opts);
355
- const merged = mergeTelemetry(opts);
461
+ const merged = mergeTelemetry(opts, telemetryKey);
356
462
  const userOnFinish = opts?.onFinish;
357
463
  const userOnError = opts?.onError;
358
464
  const wrappedOpts = {
@@ -477,25 +583,47 @@ function createMirroredTracer(primary, secondary) {
477
583
  })
478
584
  };
479
585
  }
480
- function mergeTelemetry(opts, fallbackTelemetry) {
586
+ function isNeatlogsV7Integration(integration) {
587
+ return typeof integration === "object" && integration !== null && NEATLOGS_V7_INTEGRATION in integration;
588
+ }
589
+ function mergeTelemetry(opts, telemetryKey, fallbackTelemetry) {
590
+ const legacyTelemetry = opts?.experimental_telemetry ?? {};
591
+ const v7Telemetry = opts?.telemetry ?? {};
592
+ const preferredTelemetry = telemetryKey === "telemetry" ? { ...legacyTelemetry, ...v7Telemetry } : { ...v7Telemetry, ...legacyTelemetry };
481
593
  const requestedTelemetry = {
482
594
  ...fallbackTelemetry,
483
- ...opts?.experimental_telemetry,
595
+ ...preferredTelemetry,
484
596
  metadata: {
485
597
  ...fallbackTelemetry?.metadata,
486
- ...opts?.experimental_telemetry?.metadata
598
+ ...legacyTelemetry.metadata,
599
+ ...v7Telemetry.metadata
487
600
  }
488
601
  };
489
- const baseTelemetry = createAITelemetry({
602
+ const existingNeatlogsIntegration = Array.isArray(
603
+ requestedTelemetry.integrations
604
+ ) ? requestedTelemetry.integrations.find(isNeatlogsV7Integration) : void 0;
605
+ const baseTelemetry = existingNeatlogsIntegration ? {
606
+ isEnabled: true,
607
+ recordInputs: true,
608
+ recordOutputs: true,
609
+ tracer: requestedTelemetry.tracer,
610
+ ...requestedTelemetry.functionId !== void 0 ? { functionId: requestedTelemetry.functionId } : {},
611
+ metadata: requestedTelemetry.metadata,
612
+ integrations: [existingNeatlogsIntegration]
613
+ } : createAITelemetry({
490
614
  functionId: requestedTelemetry.functionId,
491
615
  metadata: requestedTelemetry.metadata,
492
616
  tracer: requestedTelemetry.tracer
493
617
  });
494
618
  const callerTracer = requestedTelemetry.tracer;
495
619
  const hasCallerTracer = callerTracer !== void 0;
620
+ const requestedIntegrations = Array.isArray(requestedTelemetry.integrations) ? requestedTelemetry.integrations.filter(
621
+ (integration) => !isNeatlogsV7Integration(integration)
622
+ ) : [];
623
+ const { telemetry: _telemetry, experimental_telemetry: _legacy, ...rest } = opts ?? {};
496
624
  return {
497
- ...opts,
498
- experimental_telemetry: {
625
+ ...rest,
626
+ [telemetryKey]: {
499
627
  ...baseTelemetry,
500
628
  ...requestedTelemetry,
501
629
  isEnabled: true,
@@ -505,7 +633,8 @@ function mergeTelemetry(opts, fallbackTelemetry) {
505
633
  // Do not add Neatlogs-only marker metadata to a caller-owned telemetry
506
634
  // pipeline such as Laminar. Both providers receive the same AI SDK span
507
635
  // data, while their providers, parent contexts, and exporters stay separate.
508
- metadata: hasCallerTracer ? requestedTelemetry.metadata : baseTelemetry.metadata
636
+ metadata: hasCallerTracer ? requestedTelemetry.metadata : baseTelemetry.metadata,
637
+ integrations: [...baseTelemetry.integrations, ...requestedIntegrations]
509
638
  }
510
639
  };
511
640
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/ai-sdk.ts","../src/core/provider.ts","../src/core/active-client.ts"],"sourcesContent":["/**\n * Vercel AI SDK wrapper — inline implementation.\n *\n * Wraps `generateText`, `streamText`, `generateObject`, `streamObject` from\n * the `ai` package with OTel parent spans + forced telemetry. Static export,\n * no dynamic imports — bundler-friendly (works with Turbopack, webpack, esbuild).\n *\n * Usage:\n * import { wrapAISDK } from 'neatlogs';\n * import * as ai from 'ai';\n * const { streamText, generateText, ToolLoopAgent } = wrapAISDK(ai);\n */\n\nimport {\n SpanStatusCode,\n type AttributeValue,\n type Context,\n type Span,\n type SpanOptions,\n type Tracer,\n} from '@opentelemetry/api';\nimport {\n getNeatlogsTracer,\n getNeatlogsParentContext,\n getRoutingNeatlogsTracer,\n withNeatlogsSpan,\n} from './core/provider.js';\n\nconst TRACER_NAME = 'neatlogs.ai-sdk';\n\n// -- Telemetry config --------------------------------------------------------\n\nexport interface CreateAITelemetryOptions {\n /** Identifier used by the AI SDK to group telemetry for this operation. */\n functionId?: string;\n metadata?: Record<string, AttributeValue>;\n /**\n * Existing telemetry tracer to preserve alongside Neatlogs (for example,\n * Laminar). Native AI SDK spans are mirrored to both isolated pipelines.\n */\n tracer?: Tracer;\n}\n\nexport interface AITelemetryConfig {\n isEnabled: true;\n recordInputs: true;\n recordOutputs: true;\n tracer: Tracer;\n functionId?: string;\n metadata: Record<string, AttributeValue>;\n}\n\nexport function createAITelemetry(\n opts: CreateAITelemetryOptions = {},\n): AITelemetryConfig {\n const userMeta = opts.metadata ?? {};\n const neatlogsTracer = getRoutingNeatlogsTracer(TRACER_NAME);\n const callerTracer = opts.tracer;\n return {\n isEnabled: true,\n recordInputs: true,\n recordOutputs: true,\n // Hand the AI SDK an isolation-aware tracer: it calls startActiveSpan()\n // internally, which would otherwise parent its native spans from the foreign\n // global context AND push them onto it (so a co-tenant's next span inherits\n // ours). The facade routes both through the private Neatlogs context.\n tracer: callerTracer\n ? createMirroredTracer(callerTracer, neatlogsTracer)\n : neatlogsTracer,\n ...(opts.functionId !== undefined ? { functionId: opts.functionId } : {}),\n // The marker is an implementation detail used only when Neatlogs owns the\n // telemetry stream. Do not leak it into caller-owned providers.\n metadata: callerTracer ? { ...userMeta } : { ...userMeta, neatlogsWrapped: true },\n };\n}\n\n// -- Span attributes ---------------------------------------------------------\n\nfunction safeStringify(value: unknown): string {\n try {\n return JSON.stringify(value);\n } catch {\n return '';\n }\n}\n\nfunction setInputValue(span: Span, opts: Record<string, unknown>): void {\n // For generateText/streamText — only capture prompt or messages, not full model config\n if (opts && ('prompt' in opts || 'messages' in opts)) {\n const input = opts.messages ?? opts.prompt;\n const stringified = safeStringify(input);\n if (stringified) {\n span.setAttribute('input.value', stringified);\n }\n return;\n }\n const stringified = safeStringify(opts);\n if (stringified) {\n span.setAttribute('input.value', stringified);\n }\n}\n\nfunction setOutputValue(span: Span, result: unknown): void {\n if (result && typeof result === 'object') {\n const r = result as Record<string, unknown>;\n // GenerateTextResult / StreamTextResult — extract meaningful fields\n if ('text' in r && 'finishReason' in r) {\n const text = String(r.text ?? '');\n if (text) {\n span.setAttribute('output.value', text);\n }\n if (r.finishReason) {\n span.setAttribute('gen_ai.finish_reason', String(r.finishReason));\n }\n return;\n }\n // GenerateObjectResult — the structured object is the output, not `text`.\n // Without this the envelope (object+usage+response+…) gets stringified whole.\n if ('object' in r && 'finishReason' in r) {\n if (r.object !== undefined) {\n span.setAttribute('output.value', safeStringify(r.object));\n }\n if (r.finishReason) {\n span.setAttribute('gen_ai.finish_reason', String(r.finishReason));\n }\n return;\n }\n }\n const stringified = safeStringify(result);\n if (stringified) {\n span.setAttribute('output.value', stringified);\n }\n}\n\n// Extract output from a streamText/streamObject `onFinish` event. The event\n// extends StepResult, so `text` (streamText) / `object` (streamObject) sit at\n// the top level alongside `finishReason` — but the event also carries `steps`,\n// `usage`, etc., so we pull only the meaningful fields instead of stringifying\n// the whole envelope.\nfunction setStreamOutputValue(span: Span, event: unknown): void {\n if (!event || typeof event !== 'object') return;\n const e = event as Record<string, unknown>;\n if (typeof e.text === 'string' && e.text) {\n span.setAttribute('output.value', e.text);\n } else if ('object' in e && e.object !== undefined) {\n const stringified = safeStringify(e.object);\n if (stringified) span.setAttribute('output.value', stringified);\n }\n if (e.finishReason) {\n span.setAttribute('gen_ai.finish_reason', String(e.finishReason));\n }\n}\n\n// -- Wrapping ----------------------------------------------------------------\n\ntype WrappedFunctionName =\n | 'generateText'\n | 'streamText'\n | 'generateObject'\n | 'streamObject'\n | 'embed'\n | 'embedMany'\n | 'rerank';\n\nconst WRAPPED_FUNCTIONS: readonly WrappedFunctionName[] = [\n 'generateText',\n 'streamText',\n 'generateObject',\n 'streamObject',\n 'embed',\n 'embedMany',\n 'rerank',\n] as const;\n\ntype WrappedAgentConstructorName = 'ToolLoopAgent' | 'Experimental_Agent';\n\nconst WRAPPED_AGENT_CONSTRUCTORS: readonly WrappedAgentConstructorName[] = [\n 'ToolLoopAgent',\n 'Experimental_Agent',\n] as const;\n\ntype AgentConstructor = new (...args: any[]) => unknown;\n\nconst wrappedExports = new WeakSet<Function>();\nconst wrapperByOriginal = new WeakMap<Function, Function>();\n\n/**\n * Wrap the `ai` module namespace so that every supported generation, embedding,\n * and reranking call:\n *\n * 1. Opens a parent OTel span on the active TracerProvider.\n * 2. Forces `experimental_telemetry: { isEnabled: true }`, merging user metadata.\n * 3. Records input/output on the parent span and propagates errors.\n *\n * AI SDK v6's `ToolLoopAgent` (and its `Experimental_Agent` alias) is wrapped at\n * construction time so its internal model and tool calls receive the same native\n * telemetry configuration. Other exports pass through unchanged.\n */\nexport function wrapAISDK<T extends Record<string, unknown>>(aiModule: T): T {\n const wrapped: Record<string, unknown> = { ...aiModule };\n\n for (const name of WRAPPED_FUNCTIONS) {\n const original = aiModule[name];\n if (typeof original !== 'function') continue;\n\n const existing = getExistingWrapper(original);\n if (existing) {\n wrapped[name] = existing;\n continue;\n }\n\n if (name === 'streamText' || name === 'streamObject') {\n wrapped[name] = cacheWrapper(\n original,\n createStreamWrapper(name, original as (opts: any) => unknown),\n );\n } else {\n wrapped[name] = cacheWrapper(\n original,\n createAsyncWrapper(name, original as (opts: any) => Promise<unknown>),\n );\n }\n }\n\n for (const name of WRAPPED_AGENT_CONSTRUCTORS) {\n const original = aiModule[name];\n if (typeof original !== 'function') continue;\n\n const existing = getExistingWrapper(original);\n wrapped[name] =\n existing ??\n cacheWrapper(\n original,\n createAgentConstructorWrapper(original as AgentConstructor),\n );\n }\n\n return wrapped as T;\n}\n\nfunction getExistingWrapper(original: Function): Function | undefined {\n if (wrappedExports.has(original)) return original;\n return wrapperByOriginal.get(original);\n}\n\nfunction cacheWrapper(original: Function, wrapped: Function): Function {\n wrapperByOriginal.set(original, wrapped);\n wrappedExports.add(wrapped);\n return wrapped;\n}\n\nfunction createAgentConstructorWrapper(\n original: AgentConstructor,\n): AgentConstructor {\n return new Proxy(original, {\n construct(target, args, newTarget) {\n if (\n args.length === 0 ||\n typeof args[0] !== 'object' ||\n args[0] === null\n ) {\n return Reflect.construct(target, args, newTarget);\n }\n\n const settings = mergeAgentSettings(args[0]);\n return Reflect.construct(target, [settings, ...args.slice(1)], newTarget);\n },\n });\n}\n\nfunction mergeAgentSettings(settings: any): any {\n const merged = mergeTelemetry(settings);\n const userPrepareCall = settings.prepareCall;\n if (typeof userPrepareCall !== 'function') return merged;\n\n return {\n ...merged,\n prepareCall: async function wrappedPrepareCall(\n this: unknown,\n ...args: any[]\n ) {\n const prepared = await Reflect.apply(userPrepareCall, this, args);\n return prepared == null\n ? prepared\n : mergeTelemetry(prepared, settings.experimental_telemetry);\n },\n };\n}\n\nfunction rootSpanKind(name: WrappedFunctionName): string {\n if (name === 'embed' || name === 'embedMany' || name === 'rerank')\n return 'CHAIN';\n return 'WORKFLOW';\n}\n\nfunction getParentContext() {\n // Our parent comes solely from the private span store; a\n // foreign provider's active span must never become our ancestor.\n return getNeatlogsParentContext();\n}\n\nfunction createAsyncWrapper(\n name: WrappedFunctionName,\n original: (opts: any) => Promise<unknown>,\n): (opts: any) => Promise<unknown> {\n return async function wrappedAsyncFn(opts: any): Promise<unknown> {\n const tracer = getNeatlogsTracer(TRACER_NAME);\n // startSpan (NOT startActiveSpan) + withNeatlogsSpan: startActiveSpan would\n // push our span onto the GLOBAL OTel context, so a foreign tracer's\n // startSpan() inside generateText() would read it as parent and inherit our\n // trace id. withNeatlogsSpan carries the parent in the private store in\n // the private context, leaving the global context untouched.\n const parentContext = getParentContext();\n const span = tracer.startSpan(\n `ai.${name}`,\n { attributes: { 'openinference.span.kind': rootSpanKind(name) } },\n parentContext,\n );\n return withNeatlogsSpan(\n span,\n async () => {\n try {\n const isEmbedOrRerank =\n name === 'embed' || name === 'embedMany' || name === 'rerank';\n if (!isEmbedOrRerank) {\n setInputValue(span, opts);\n }\n if (name === 'rerank' && opts?.query) {\n span.setAttribute('ai.rerank.query', String(opts.query));\n }\n const merged = mergeTelemetry(opts);\n const result = await original(merged);\n if (!isEmbedOrRerank) {\n setOutputValue(span, result);\n }\n return result;\n } catch (err) {\n recordSpanError(span, err);\n throw err;\n } finally {\n span.end();\n }\n },\n parentContext,\n );\n };\n}\n\n// streamText/streamObject return synchronously while the model keeps producing\n// tokens for seconds afterwards. Ending the span in a `finally` (as a plain sync\n// wrapper would) closes it in ~2ms with no output — the output only exists once\n// the stream finishes. Instead we keep the span open and end it from the AI SDK's\n// `onFinish` callback, where the final text/object is available. Any user-provided\n// `onFinish` is preserved and invoked first.\nfunction createStreamWrapper(\n name: WrappedFunctionName,\n original: (opts: any) => unknown,\n): (opts: any) => unknown {\n return function wrappedStreamFn(opts: any): unknown {\n const tracer = getNeatlogsTracer(TRACER_NAME);\n // startSpan + withNeatlogsSpan (see createAsyncWrapper) so streamText's\n // internals never see our span on the global OTel context. The span stays\n // open past the run scope and is ended from onFinish/onError.\n const parentContext = getParentContext();\n const span = tracer.startSpan(\n `ai.${name}`,\n { attributes: { 'openinference.span.kind': rootSpanKind(name) } },\n parentContext,\n );\n return withNeatlogsSpan(\n span,\n () => {\n let spanEnded = false;\n const endOnce = () => {\n if (spanEnded) return;\n spanEnded = true;\n span.end();\n };\n try {\n setInputValue(span, opts);\n const merged = mergeTelemetry(opts);\n const userOnFinish = opts?.onFinish;\n const userOnError = opts?.onError;\n const wrappedOpts = {\n ...merged,\n onFinish: async (event: any) => {\n try {\n setStreamOutputValue(span, event);\n } finally {\n endOnce();\n }\n if (typeof userOnFinish === 'function') {\n return userOnFinish(event);\n }\n },\n onError: (event: any) => {\n recordSpanError(span, (event && event.error) ?? event);\n endOnce();\n if (typeof userOnError === 'function') {\n return userOnError(event);\n }\n },\n };\n return original(wrappedOpts);\n } catch (err) {\n // Synchronous throw (e.g. bad arguments) — the stream never started.\n recordSpanError(span, err);\n endOnce();\n throw err;\n }\n },\n parentContext,\n );\n };\n}\n\nfunction createMirroredSpan(primary: Span, secondary: Span): Span {\n const mirrored: Span = {\n spanContext() {\n // The caller-owned tracer remains the process-global context owner. Its\n // context is therefore the one external instrumentation must observe.\n return primary.spanContext();\n },\n setAttribute(key, value) {\n primary.setAttribute(key, value);\n secondary.setAttribute(key, value);\n return mirrored;\n },\n setAttributes(attributes) {\n primary.setAttributes(attributes);\n secondary.setAttributes(attributes);\n return mirrored;\n },\n addEvent(name, attributesOrStartTime, startTime) {\n primary.addEvent(name, attributesOrStartTime, startTime);\n secondary.addEvent(name, attributesOrStartTime, startTime);\n return mirrored;\n },\n addLink(link) {\n primary.addLink(link);\n secondary.addLink(link);\n return mirrored;\n },\n addLinks(links) {\n primary.addLinks(links);\n secondary.addLinks(links);\n return mirrored;\n },\n setStatus(status) {\n primary.setStatus(status);\n secondary.setStatus(status);\n return mirrored;\n },\n updateName(name) {\n primary.updateName(name);\n secondary.updateName(name);\n return mirrored;\n },\n end(endTime) {\n primary.end(endTime);\n secondary.end(endTime);\n },\n isRecording() {\n return primary.isRecording() || secondary.isRecording();\n },\n recordException(exception, time) {\n primary.recordException(exception, time);\n secondary.recordException(exception, time);\n },\n };\n return mirrored;\n}\n\n/**\n * Mirror one AI SDK telemetry stream to two isolated tracer pipelines.\n *\n * The caller-owned tracer is deliberately outermost so its normal global OTel\n * activation and parentage stay unchanged. The Neatlogs routing tracer keeps\n * its span active only in Neatlogs' private AsyncLocalStorage context.\n */\nfunction createMirroredTracer(primary: Tracer, secondary: Tracer): Tracer {\n return {\n startSpan(name: string, options?: SpanOptions, context?: Context): Span {\n const primarySpan = primary.startSpan(name, options, context);\n const secondarySpan = secondary.startSpan(name, options);\n return createMirroredSpan(primarySpan, secondarySpan);\n },\n startActiveSpan: (<F extends (span: Span) => unknown>(\n name: string,\n arg2?: SpanOptions | F,\n arg3?: Context | F,\n arg4?: F,\n ): ReturnType<F> => {\n let options: SpanOptions | undefined;\n let context: Context | undefined;\n let fn: F;\n\n if (typeof arg2 === 'function') {\n fn = arg2;\n } else if (typeof arg3 === 'function') {\n options = arg2;\n fn = arg3;\n } else {\n options = arg2;\n context = arg3 as Context;\n fn = arg4!;\n }\n\n const runSecondary = (primarySpan: Span): ReturnType<F> => {\n const onSecondarySpan = (secondarySpan: Span) =>\n fn(createMirroredSpan(primarySpan, secondarySpan)) as ReturnType<F>;\n return options === undefined\n ? secondary.startActiveSpan(name, onSecondarySpan)\n : secondary.startActiveSpan(name, options, onSecondarySpan);\n };\n\n if (context !== undefined) {\n return primary.startActiveSpan(\n name,\n options ?? {},\n context,\n runSecondary,\n ) as ReturnType<F>;\n }\n return options === undefined\n ? (primary.startActiveSpan(name, runSecondary) as ReturnType<F>)\n : (primary.startActiveSpan(name, options, runSecondary) as ReturnType<F>);\n }) as Tracer['startActiveSpan'],\n };\n}\n\nfunction mergeTelemetry(opts: any, fallbackTelemetry?: any): any {\n const requestedTelemetry = {\n ...fallbackTelemetry,\n ...opts?.experimental_telemetry,\n metadata: {\n ...fallbackTelemetry?.metadata,\n ...opts?.experimental_telemetry?.metadata,\n },\n };\n const baseTelemetry: AITelemetryConfig = createAITelemetry({\n functionId: requestedTelemetry.functionId,\n metadata: requestedTelemetry.metadata,\n tracer: requestedTelemetry.tracer as Tracer | undefined,\n });\n const callerTracer = requestedTelemetry.tracer as Tracer | undefined;\n const hasCallerTracer = callerTracer !== undefined;\n\n return {\n ...opts,\n experimental_telemetry: {\n ...baseTelemetry,\n ...requestedTelemetry,\n isEnabled: true,\n recordInputs: requestedTelemetry.recordInputs ?? true,\n recordOutputs: requestedTelemetry.recordOutputs ?? true,\n tracer: baseTelemetry.tracer,\n // Do not add Neatlogs-only marker metadata to a caller-owned telemetry\n // pipeline such as Laminar. Both providers receive the same AI SDK span\n // data, while their providers, parent contexts, and exporters stay separate.\n metadata: hasCallerTracer\n ? requestedTelemetry.metadata\n : baseTelemetry.metadata,\n },\n };\n}\n\nfunction recordSpanError(span: Span, err: unknown): void {\n if (err instanceof Error) {\n span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });\n span.recordException(err);\n } else {\n span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });\n }\n}\n","/**\n * Neatlogs-owned tracing state.\n *\n * Spans are created by the private Neatlogs provider and their parent is carried\n * in a private context key. The process-global OpenTelemetry span is\n * deliberately left untouched so other observability SDKs cannot export or\n * become parents of Neatlogs spans.\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport {\n ROOT_CONTEXT,\n INVALID_SPAN_CONTEXT,\n createContextKey,\n trace as otelTrace,\n type Context,\n type Span,\n type SpanOptions,\n type Tracer,\n type TracerProvider,\n} from '@opentelemetry/api';\nimport { getActiveClient } from './active-client.js';\n\n// Carries the trace-ROOT span down the private context so descendants can target\n// it (e.g. setTraceOutput). getActiveNeatlogsSpan() returns the innermost span,\n// which is not the root once nested; this key preserves the root reference.\nconst NEATLOGS_ROOT_SPAN_KEY = createContextKey('neatlogs.root_span');\n\n// Entry points are bundled independently (`neatlogs`, `neatlogs/openai`,\n// `neatlogs/ai`, `neatlogs/mastra`, … each in both CJS and ESM), so every piece\n// of shared tracing state — the private span store AND the resolved provider /\n// provider — must live on `globalThis` behind a `Symbol.for` key.\n// Otherwise `init()` (run from the `neatlogs` bundle) sets `_provider` in ITS\n// module copy while a wrapper imported from `neatlogs/openai` reads a different,\n// still-null copy and silently falls back to the foreign global provider.\nconst PRIVATE_SPAN_STORAGE_KEY = Symbol.for(\n 'neatlogs.private_span_async_local_storage',\n);\nconst PRIVATE_PROVIDER_STATE_KEY = Symbol.for(\n 'neatlogs.private_provider_state',\n);\ninterface PrivateProviderState {\n provider: TracerProvider | null;\n}\ntype NeatlogsGlobal = typeof globalThis & {\n [PRIVATE_SPAN_STORAGE_KEY]?: AsyncLocalStorage<Context>;\n [PRIVATE_PROVIDER_STATE_KEY]?: PrivateProviderState;\n};\nconst neatlogsGlobal = globalThis as NeatlogsGlobal;\n// Stores the full Neatlogs Context (parent span PLUS any threaded values such as\n// trace()'s prompt-template keys), not just the span. We never\n// activate the OTel global context, so this private store is the ONLY channel\n// through which those values propagate down to descendant spans.\nconst privateContextStorage =\n neatlogsGlobal[PRIVATE_SPAN_STORAGE_KEY] ??\n (neatlogsGlobal[PRIVATE_SPAN_STORAGE_KEY] = new AsyncLocalStorage<Context>());\nconst providerState: PrivateProviderState =\n neatlogsGlobal[PRIVATE_PROVIDER_STATE_KEY] ??\n (neatlogsGlobal[PRIVATE_PROVIDER_STATE_KEY] = {\n provider: null,\n });\n// Wrappers may be constructed or accidentally invoked before init(). They must\n// never fall back to a foreign process-global provider, so pre-init calls use a\n// local no-op tracer and safely emit no exported spans.\nconst preInitTracer: Tracer = {\n startSpan(): Span {\n return otelTrace.wrapSpanContext(INVALID_SPAN_CONTEXT);\n },\n startActiveSpan<F extends (span: Span) => unknown>(\n _name: string,\n arg2?: SpanOptions | Context | F,\n arg3?: Context | F,\n arg4?: F,\n ): ReturnType<F> {\n const fn =\n typeof arg2 === 'function'\n ? arg2\n : typeof arg3 === 'function'\n ? arg3\n : arg4;\n return fn!(otelTrace.wrapSpanContext(INVALID_SPAN_CONTEXT)) as ReturnType<F>;\n },\n};\n\n/** @internal Configure the provider used by Neatlogs-created spans. */\nexport function _setNeatlogsProvider(provider: TracerProvider | null): void {\n providerState.provider = provider;\n}\n\n/** Resolve a tracer from the private provider when one is configured. */\nexport function getNeatlogsTracer(name: string): Tracer {\n const client = getActiveClient();\n if (client) return client.getTracer(name);\n return providerState.provider?.getTracer(name) ?? preInitTracer;\n}\n\n/** A reusable tracer facade that resolves the active Client on every call. */\nexport function getRoutingNeatlogsTracer(name: string): Tracer {\n return {\n startSpan(spanName: string, options?: SpanOptions, context?: Context): Span {\n return isolateTracer(getNeatlogsTracer(name)).startSpan(\n spanName,\n options,\n context,\n );\n },\n startActiveSpan: ((...args: any[]) =>\n (isolateTracer(getNeatlogsTracer(name)).startActiveSpan as (...inner: any[]) => any)(\n ...args,\n )) as Tracer['startActiveSpan'],\n };\n}\n\n/**\n * @internal The private Neatlogs provider, or null before\n * init(). Used by integrations that must repoint a self-instrumenting library's\n * captured provider onto ours.\n */\nexport function getNeatlogsProvider(): TracerProvider | null {\n const client = getActiveClient();\n if (client) return client.tracerProvider;\n return providerState.provider;\n}\n\n/** Run a Client callback without inheriting another pipeline's private span. */\nexport function runWithFreshNeatlogsContext<T>(fn: () => T): T {\n return privateContextStorage.run(ROOT_CONTEXT, fn);\n}\n\n/**\n * Wrap a tracer so that spans it creates parent from — and, for\n * `startActiveSpan`, activate on — the PRIVATE Neatlogs context instead of the\n * global one.\n *\n * We hand this to libraries that create their own spans off a tracer we give\n * them (the Vercel AI SDK's `experimental_telemetry.tracer`, which calls\n * `tracer.startActiveSpan()` internally). Without the facade the AI SDK's native\n * spans would parent from `context.active()` — the foreign co-tenant's context —\n * and `startActiveSpan` would push them onto the GLOBAL context, so a foreign\n * tracer's next span reads our native span as its parent. Both directions leak.\n *\n */\nexport function isolateTracer(tracer: Tracer): Tracer {\n const facade: Tracer = {\n startSpan(name: string, options?: SpanOptions, context?: Context): Span {\n const parent = context ?? getNeatlogsActiveContext();\n return tracer.startSpan(name, options, parent);\n },\n startActiveSpan<F extends (span: Span) => unknown>(\n name: string,\n arg2?: SpanOptions | Context | F,\n arg3?: Context | F,\n arg4?: F,\n ): ReturnType<F> {\n // Normalize the 2/3/4-arg overloads of startActiveSpan.\n let options: SpanOptions | undefined;\n let context: Context | undefined;\n let fn: F;\n if (typeof arg2 === 'function') {\n fn = arg2 as F;\n } else if (typeof arg3 === 'function') {\n options = arg2 as SpanOptions;\n fn = arg3 as F;\n } else {\n options = arg2 as SpanOptions;\n context = arg3 as Context;\n fn = arg4 as F;\n }\n const parent = context ?? getNeatlogsActiveContext();\n const span = tracer.startSpan(name, options, parent);\n return withNeatlogsSpan(span, () => fn(span), parent) as ReturnType<F>;\n },\n };\n return facade;\n}\n\n/**\n * The base context to build new Neatlogs spans and values on. NEVER contains a\n * foreign provider's span: it reads our private store, or ROOT_CONTEXT when\n * nothing is active.\n */\nexport function getNeatlogsActiveContext(): Context {\n return privateContextStorage.getStore() ?? ROOT_CONTEXT;\n}\n\n/** Return only the active Neatlogs span (never a foreign provider's span). */\nexport function getActiveNeatlogsSpan(): Span | undefined {\n return otelTrace.getSpan(getNeatlogsActiveContext());\n}\n\n/**\n * Return the trace-ROOT Neatlogs span, or undefined when no trace is active.\n *\n * Unlike {@link getActiveNeatlogsSpan} (innermost), this is the outermost span\n * of the current trace — the one the backend derives trace-level output from\n * (`parent_span_id=''`). It is stashed on the private context the first time a\n * span becomes active, so nested calls still resolve to the root.\n */\nexport function getNeatlogsRootSpan(): Span | undefined {\n return getNeatlogsActiveContext().getValue(NEATLOGS_ROOT_SPAN_KEY) as\n | Span\n | undefined;\n}\n\n/**\n * Build a parent context that cannot contain a foreign provider's span.\n *\n * The active Neatlogs context already carries the parent span AND any values\n * the caller threaded in upstream (e.g. `trace()`'s prompt-template values), so\n * those values reach the span processor via `onStart(parentContext)`. An\n * explicit `baseContext` (a caller's own value-carrying context) is honored as-is.\n */\nexport function getNeatlogsParentContext(baseContext?: Context): Context {\n return baseContext ?? getNeatlogsActiveContext();\n}\n\n/**\n * A base context for callers that thread parent linkage themselves\n * (callback/event handlers that keep their own run-id → span map: LangChain,\n * OpenAI-Agents, Claude Agent SDK).\n *\n * This is the ACTIVE Neatlogs context — the private store's\n * context if a Neatlogs `trace()`/`span()` encloses this call, else\n * ROOT_CONTEXT. So a handler's own root/entry span nests under an enclosing\n * Neatlogs trace (preserving its session + end-user id) when one exists, and\n * auto-roots cleanly when one doesn't. A foreign provider's active span can\n * never leak in as an ancestor because the global context is never read.\n */\nexport function getNeatlogsBaseContext(baseContext?: Context): Context {\n return baseContext ?? getNeatlogsActiveContext();\n}\n\n/**\n * Build an execution context for a Neatlogs span while preserving the active\n * foreign context. The span rides our private context store.\n */\nexport function getNeatlogsExecutionContext(\n span: Span,\n baseContext: Context = ROOT_CONTEXT,\n): Context {\n return otelTrace.setSpan(baseContext, span);\n}\n\n/**\n * Run a callback with a Neatlogs span active under the appropriate policy.\n *\n * The stored context is `setSpan(base, span)` — carrying the span PLUS whatever\n * values `base` holds — so threaded values (prompt templates)\n * propagate to descendant spans through our private store instead of the global\n * OTel context we deliberately never touch.\n */\nexport function withNeatlogsSpan<T>(\n span: Span,\n fn: () => T,\n baseContext?: Context,\n rootSpan?: Span,\n): T {\n const base = baseContext ?? getNeatlogsActiveContext();\n let ctx = otelTrace.setSpan(base, span);\n // The first span activated in a context with no root recorded IS the root of\n // this trace; remember it so descendants (setTraceOutput) can target it.\n if (base.getValue(NEATLOGS_ROOT_SPAN_KEY) === undefined) {\n ctx = ctx.setValue(NEATLOGS_ROOT_SPAN_KEY, rootSpan ?? span);\n }\n return privateContextStorage.run(ctx, fn);\n}\n\n/**\n * @internal Run with a private Neatlogs parent without treating that parent as\n * a locally-recording trace root. This is used for an extracted remote parent:\n * the first local recording span should remain the target for local trace-level\n * output, while still inheriting the remote trace/span IDs.\n */\nexport function withNeatlogsRemoteParent<T>(span: Span, fn: () => T): T {\n return privateContextStorage.run(otelTrace.setSpan(ROOT_CONTEXT, span), fn);\n}\n","import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Tracer, TracerProvider } from '@opentelemetry/api';\n\nexport interface ActiveNeatlogsClient {\n readonly workflowName: string;\n readonly tracerProvider: TracerProvider;\n getTracer(scope: string): Tracer;\n getLogger(): any | null;\n}\n\nconst ACTIVE_CLIENT_STORAGE_KEY = Symbol.for(\n 'neatlogs.active_client_async_local_storage',\n);\ntype NeatlogsGlobal = typeof globalThis & {\n [ACTIVE_CLIENT_STORAGE_KEY]?: AsyncLocalStorage<ActiveNeatlogsClient>;\n};\nconst neatlogsGlobal = globalThis as NeatlogsGlobal;\nconst storage =\n neatlogsGlobal[ACTIVE_CLIENT_STORAGE_KEY] ??\n (neatlogsGlobal[ACTIVE_CLIENT_STORAGE_KEY] =\n new AsyncLocalStorage<ActiveNeatlogsClient>());\n\nexport function getActiveClient(): ActiveNeatlogsClient | undefined {\n return storage.getStore();\n}\n\nexport function runWithClient<T>(\n client: ActiveNeatlogsClient,\n fn: () => T,\n): T {\n return storage.run(client, fn);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,IAAAA,cAOO;;;ACXP,IAAAC,2BAAkC;AAClC,iBAUO;;;ACpBP,8BAAkC;AAUlC,IAAM,4BAA4B,uBAAO;AAAA,EACvC;AACF;AAIA,IAAM,iBAAiB;AACvB,IAAM,UACJ,eAAe,yBAAyB,MACvC,eAAe,yBAAyB,IACvC,IAAI,0CAAwC;AAEzC,SAAS,kBAAoD;AAClE,SAAO,QAAQ,SAAS;AAC1B;;;ADEA,IAAM,6BAAyB,6BAAiB,oBAAoB;AASpE,IAAM,2BAA2B,uBAAO;AAAA,EACtC;AACF;AACA,IAAM,6BAA6B,uBAAO;AAAA,EACxC;AACF;AAQA,IAAMC,kBAAiB;AAKvB,IAAM,wBACJA,gBAAe,wBAAwB,MACtCA,gBAAe,wBAAwB,IAAI,IAAI,2CAA2B;AAC7E,IAAM,gBACJA,gBAAe,0BAA0B,MACxCA,gBAAe,0BAA0B,IAAI;AAAA,EAC5C,UAAU;AACZ;AAIF,IAAM,gBAAwB;AAAA,EAC5B,YAAkB;AAChB,WAAO,WAAAC,MAAU,gBAAgB,+BAAoB;AAAA,EACvD;AAAA,EACA,gBACE,OACA,MACA,MACA,MACe;AACf,UAAM,KACJ,OAAO,SAAS,aACZ,OACA,OAAO,SAAS,aACd,OACA;AACR,WAAO,GAAI,WAAAA,MAAU,g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AAA,IACH,wBAAwB;AAAA,MACtB,GAAG;AAAA,MACH,GAAG;AAAA,MACH,WAAW;AAAA,MACX,cAAc,mBAAmB,gBAAgB;AAAA,MACjD,eAAe,mBAAmB,iBAAiB;AAAA,MACnD,QAAQ,cAAc;AAAA;AAAA;AAAA;AAAA,MAItB,UAAU,kBACN,mBAAmB,WACnB,cAAc;AAAA,IACpB;AAAA,EACF;AACF;AAEA,SAAS,gBAAgB,MAAY,KAAoB;AACvD,MAAI,eAAe,OAAO;AACxB,SAAK,UAAU,EAAE,MAAM,2BAAe,OAAO,SAAS,IAAI,QAAQ,CAAC;AACnE,SAAK,gBAAgB,GAAG;AAAA,EAC1B,OAAO;AACL,SAAK,UAAU,EAAE,MAAM,2BAAe,OAAO,SAAS,OAAO,GAAG,EAAE,CAAC;AAAA,EACrE;AACF;","names":["import_api","import_node_async_hooks","neatlogsGlobal","otelTrace","otelTrace","stringified"]}
1
+ {"version":3,"sources":["../src/ai-sdk.ts","../src/core/provider.ts","../src/core/active-client.ts"],"sourcesContent":["/**\n * Vercel AI SDK wrapper — inline implementation.\n *\n * Wraps `generateText`, `streamText`, `generateObject`, `streamObject` from\n * the `ai` package with OTel parent spans + forced telemetry. AI SDK v7's\n * optional OpenTelemetry adapter is loaded only when its integration runs.\n *\n * Usage:\n * import { wrapAISDK } from 'neatlogs';\n * import * as ai from 'ai';\n * const { streamText, generateText, ToolLoopAgent } = wrapAISDK(ai);\n */\n\nimport {\n SpanStatusCode,\n type AttributeValue,\n type Context,\n type Span,\n type SpanOptions,\n type Tracer,\n} from '@opentelemetry/api';\nimport {\n getNeatlogsTracer,\n getNeatlogsParentContext,\n getRoutingNeatlogsTracer,\n withNeatlogsSpan,\n} from './core/provider.js';\n\nconst TRACER_NAME = 'neatlogs.ai-sdk';\n\n// -- Telemetry config --------------------------------------------------------\n\nexport interface CreateAITelemetryOptions {\n /** Identifier used by the AI SDK to group telemetry for this operation. */\n functionId?: string;\n metadata?: Record<string, AttributeValue>;\n /**\n * Existing telemetry tracer to preserve alongside Neatlogs (for example,\n * Laminar). Native AI SDK spans are mirrored to both isolated pipelines.\n */\n tracer?: Tracer;\n}\n\nexport interface AITelemetryConfig {\n isEnabled: true;\n recordInputs: true;\n recordOutputs: true;\n tracer: Tracer;\n functionId?: string;\n metadata: Record<string, AttributeValue>;\n /** AI SDK v7 telemetry integrations. Ignored by AI SDK v6. */\n integrations: V7TelemetryIntegration[];\n}\n\n/**\n * Small structural surface shared by the AI SDK v6 and v7 telemetry integration types.\n * Keeping this local avoids making either AI SDK version part of Neatlogs' public type identity.\n */\ninterface V7TelemetryIntegration {\n onStart(event: unknown): Promise<void>;\n}\n\nconst NEATLOGS_V7_INTEGRATION = Symbol('neatlogs.ai-sdk.v7-integration');\nconst AI_SDK_OTEL_MODULE = '@ai-sdk/otel';\n\n/**\n * AI SDK v7 moved OpenTelemetry support into `@ai-sdk/otel` and now invokes a\n * telemetry integration instead of accepting a tracer directly. Keep the\n * package optional for AI SDK v6 users and load it only if v7 calls one of the\n * integration hooks.\n */\nclass LazyV7OpenTelemetryIntegration {\n readonly [NEATLOGS_V7_INTEGRATION] = true;\n private delegatePromise?: Promise<Record<string, any>>;\n\n constructor(\n private readonly tracer: Tracer,\n private readonly metadata: Record<string, AttributeValue>,\n ) {}\n\n private getDelegate(): Promise<Record<string, any>> {\n // Keep the v7-only adapter out of TypeScript's eager module resolution.\n // npm skips this optional dependency on Node 18/20 because AI SDK v7 and\n // @ai-sdk/otel require Node 22; v6 consumers must still be able to build.\n return (this.delegatePromise ??= import(AI_SDK_OTEL_MODULE)\n .then(({ OpenTelemetry }) =>\n new OpenTelemetry({\n tracer: this.tracer,\n enrichSpan: () => this.metadata,\n }) as unknown as Record<string, any>,\n )\n .catch((error: unknown) => {\n const detail = error instanceof Error ? `: ${error.message}` : '';\n throw new Error(\n 'Vercel AI SDK v7 telemetry requires the optional @ai-sdk/otel peer dependency' +\n detail,\n );\n }));\n }\n\n private async notify(method: string, event: unknown): Promise<void> {\n const delegate = await this.getDelegate();\n const fn = delegate[method];\n if (typeof fn === 'function') {\n await Reflect.apply(fn, delegate, [event]);\n }\n }\n\n onStart(event: unknown) { return this.notify('onStart', event); }\n onStepStart(event: unknown) { return this.notify('onStepStart', event); }\n onLanguageModelCallStart(event: unknown) {\n return this.notify('onLanguageModelCallStart', event);\n }\n onLanguageModelCallEnd(event: unknown) {\n return this.notify('onLanguageModelCallEnd', event);\n }\n onToolExecutionStart(event: unknown) {\n return this.notify('onToolExecutionStart', event);\n }\n onToolExecutionEnd(event: unknown) {\n return this.notify('onToolExecutionEnd', event);\n }\n onStepEnd(event: unknown) { return this.notify('onStepEnd', event); }\n onStepFinish(event: unknown) { return this.notify('onStepFinish', event); }\n onObjectStepStart(event: unknown) {\n return this.notify('onObjectStepStart', event);\n }\n onObjectStepEnd(event: unknown) {\n return this.notify('onObjectStepEnd', event);\n }\n onEmbedStart(event: unknown) { return this.notify('onEmbedStart', event); }\n onEmbedEnd(event: unknown) { return this.notify('onEmbedEnd', event); }\n onRerankStart(event: unknown) { return this.notify('onRerankStart', event); }\n onRerankEnd(event: unknown) { return this.notify('onRerankEnd', event); }\n onEnd(event: unknown) { return this.notify('onEnd', event); }\n onAbort(event: unknown) { return this.notify('onAbort', event); }\n onError(event: unknown) { return this.notify('onError', event); }\n\n async executeLanguageModelCall<T>(options: {\n execute: () => PromiseLike<T>;\n } & Record<string, unknown>): Promise<T> {\n const delegate = await this.getDelegate();\n const fn = delegate.executeLanguageModelCall;\n return typeof fn === 'function'\n ? Reflect.apply(fn, delegate, [options])\n : options.execute();\n }\n\n async executeTool<T>(options: {\n execute: () => PromiseLike<T>;\n } & Record<string, unknown>): Promise<T> {\n const delegate = await this.getDelegate();\n const fn = delegate.executeTool;\n return typeof fn === 'function'\n ? Reflect.apply(fn, delegate, [options])\n : options.execute();\n }\n}\n\nexport function createAITelemetry(\n opts: CreateAITelemetryOptions = {},\n): AITelemetryConfig {\n const userMeta = opts.metadata ?? {};\n const neatlogsTracer = getRoutingNeatlogsTracer(TRACER_NAME);\n const callerTracer = opts.tracer;\n const tracer = callerTracer\n ? createMirroredTracer(callerTracer, neatlogsTracer)\n : neatlogsTracer;\n const metadata = callerTracer\n ? { ...userMeta }\n : { ...userMeta, neatlogsWrapped: true };\n return {\n isEnabled: true,\n recordInputs: true,\n recordOutputs: true,\n // Hand the AI SDK an isolation-aware tracer: it calls startActiveSpan()\n // internally, which would otherwise parent its native spans from the foreign\n // global context AND push them onto it (so a co-tenant's next span inherits\n // ours). The facade routes both through the private Neatlogs context.\n tracer,\n ...(opts.functionId !== undefined ? { functionId: opts.functionId } : {}),\n // The marker is an implementation detail used only when Neatlogs owns the\n // telemetry stream. Do not leak it into caller-owned providers.\n metadata,\n integrations: [new LazyV7OpenTelemetryIntegration(tracer, metadata)],\n };\n}\n\n// -- Span attributes ---------------------------------------------------------\n\nfunction safeStringify(value: unknown): string {\n try {\n return JSON.stringify(value);\n } catch {\n return '';\n }\n}\n\nfunction setInputValue(span: Span, opts: Record<string, unknown>): void {\n // For generateText/streamText — only capture prompt or messages, not full model config\n if (opts && ('prompt' in opts || 'messages' in opts)) {\n const input = opts.messages ?? opts.prompt;\n const stringified = safeStringify(input);\n if (stringified) {\n span.setAttribute('input.value', stringified);\n }\n return;\n }\n const stringified = safeStringify(opts);\n if (stringified) {\n span.setAttribute('input.value', stringified);\n }\n}\n\nfunction setOutputValue(span: Span, result: unknown): void {\n if (result && typeof result === 'object') {\n const r = result as Record<string, unknown>;\n // GenerateTextResult / StreamTextResult — extract meaningful fields\n if ('text' in r && 'finishReason' in r) {\n const text = String(r.text ?? '');\n if (text) {\n span.setAttribute('output.value', text);\n }\n if (r.finishReason) {\n span.setAttribute('neatlogs.llm.finish_reason', String(r.finishReason));\n }\n return;\n }\n // GenerateObjectResult — the structured object is the output, not `text`.\n // Without this the envelope (object+usage+response+…) gets stringified whole.\n if ('object' in r && 'finishReason' in r) {\n if (r.object !== undefined) {\n span.setAttribute('output.value', safeStringify(r.object));\n }\n if (r.finishReason) {\n span.setAttribute('neatlogs.llm.finish_reason', String(r.finishReason));\n }\n return;\n }\n }\n const stringified = safeStringify(result);\n if (stringified) {\n span.setAttribute('output.value', stringified);\n }\n}\n\n// Extract output from a streamText/streamObject `onFinish` event. The event\n// extends StepResult, so `text` (streamText) / `object` (streamObject) sit at\n// the top level alongside `finishReason` — but the event also carries `steps`,\n// `usage`, etc., so we pull only the meaningful fields instead of stringifying\n// the whole envelope.\nfunction setStreamOutputValue(span: Span, event: unknown): void {\n if (!event || typeof event !== 'object') return;\n const e = event as Record<string, unknown>;\n if (typeof e.text === 'string' && e.text) {\n span.setAttribute('output.value', e.text);\n } else if ('object' in e && e.object !== undefined) {\n const stringified = safeStringify(e.object);\n if (stringified) span.setAttribute('output.value', stringified);\n }\n if (e.finishReason) {\n span.setAttribute('neatlogs.llm.finish_reason', String(e.finishReason));\n }\n}\n\n// -- Wrapping ----------------------------------------------------------------\n\ntype WrappedFunctionName =\n | 'generateText'\n | 'streamText'\n | 'generateObject'\n | 'streamObject'\n | 'embed'\n | 'embedMany'\n | 'rerank';\n\nconst WRAPPED_FUNCTIONS: readonly WrappedFunctionName[] = [\n 'generateText',\n 'streamText',\n 'generateObject',\n 'streamObject',\n 'embed',\n 'embedMany',\n 'rerank',\n] as const;\n\ntype WrappedAgentConstructorName = 'ToolLoopAgent' | 'Experimental_Agent';\n\ntype CompatibleAITelemetryConfig = CreateAITelemetryOptions & {\n isEnabled?: boolean;\n recordInputs?: boolean;\n recordOutputs?: boolean;\n integrations?: V7TelemetryIntegration[];\n};\n\ntype TelemetryField<Options, Key extends PropertyKey> = Key extends keyof Options\n ? Options[Key]\n : never;\n\ntype CompatibleWrappedFunction<Fn> = Fn extends (...args: any[]) => any\n ? Fn &\n ((options: Omit<\n Parameters<Fn>[0],\n 'experimental_telemetry' | 'telemetry'\n > & {\n experimental_telemetry?:\n | TelemetryField<Parameters<Fn>[0], 'experimental_telemetry'>\n | CompatibleAITelemetryConfig;\n telemetry?:\n | TelemetryField<Parameters<Fn>[0], 'telemetry'>\n | CompatibleAITelemetryConfig;\n }) => ReturnType<Fn>)\n : Fn;\n\nexport type WrappedAISDK<T extends Record<string, unknown>> = T & {\n [Name in Extract<keyof T, WrappedFunctionName>]: CompatibleWrappedFunction<T[Name]>;\n};\n\nconst WRAPPED_AGENT_CONSTRUCTORS: readonly WrappedAgentConstructorName[] = [\n 'ToolLoopAgent',\n 'Experimental_Agent',\n] as const;\n\ntype AgentConstructor = new (...args: any[]) => unknown;\n\nconst wrappedExports = new WeakSet<Function>();\nconst wrapperByOriginal = new WeakMap<Function, Function>();\n\n/**\n * Wrap the `ai` module namespace so that every supported generation, embedding,\n * and reranking call:\n *\n * 1. Opens a parent OTel span on the active TracerProvider.\n * 2. Forces the version-appropriate telemetry option, merging user metadata.\n * 3. Records input/output on the parent span and propagates errors.\n *\n * `ToolLoopAgent` (and AI SDK v6's `Experimental_Agent` alias) is wrapped at\n * construction time so its internal model and tool calls receive the same native\n * telemetry configuration. Other exports pass through unchanged.\n */\nexport function wrapAISDK<T extends Record<string, unknown>>(\n aiModule: T,\n): WrappedAISDK<T> {\n const wrapped: Record<string, unknown> = { ...aiModule };\n // Vitest and some bundlers expose module namespaces through proxies which\n // throw when a missing export is read. Check membership before accessing the\n // v7-only registerTelemetry export so v6 and partial module mocks stay safe.\n const hasRegisterTelemetry =\n 'registerTelemetry' in aiModule &&\n typeof Reflect.get(aiModule, 'registerTelemetry') === 'function';\n const telemetryKey: TelemetryKey =\n hasRegisterTelemetry ? 'telemetry' : 'experimental_telemetry';\n\n for (const name of WRAPPED_FUNCTIONS) {\n const original = aiModule[name];\n if (typeof original !== 'function') continue;\n\n const existing = getExistingWrapper(original);\n if (existing) {\n wrapped[name] = existing;\n continue;\n }\n\n if (name === 'streamText' || name === 'streamObject') {\n wrapped[name] = cacheWrapper(\n original,\n createStreamWrapper(name, original as (opts: any) => unknown, telemetryKey),\n );\n } else {\n wrapped[name] = cacheWrapper(\n original,\n createAsyncWrapper(\n name,\n original as (opts: any) => Promise<unknown>,\n telemetryKey,\n ),\n );\n }\n }\n\n for (const name of WRAPPED_AGENT_CONSTRUCTORS) {\n const original = aiModule[name];\n if (typeof original !== 'function') continue;\n\n const existing = getExistingWrapper(original);\n wrapped[name] =\n existing ??\n cacheWrapper(\n original,\n createAgentConstructorWrapper(original as AgentConstructor, telemetryKey),\n );\n }\n\n return wrapped as WrappedAISDK<T>;\n}\n\nfunction getExistingWrapper(original: Function): Function | undefined {\n if (wrappedExports.has(original)) return original;\n return wrapperByOriginal.get(original);\n}\n\nfunction cacheWrapper(original: Function, wrapped: Function): Function {\n wrapperByOriginal.set(original, wrapped);\n wrappedExports.add(wrapped);\n return wrapped;\n}\n\nfunction createAgentConstructorWrapper(\n original: AgentConstructor,\n telemetryKey: TelemetryKey,\n): AgentConstructor {\n return new Proxy(original, {\n construct(target, args, newTarget) {\n if (\n args.length === 0 ||\n typeof args[0] !== 'object' ||\n args[0] === null\n ) {\n return Reflect.construct(target, args, newTarget);\n }\n\n const settings = mergeAgentSettings(args[0], telemetryKey);\n return Reflect.construct(target, [settings, ...args.slice(1)], newTarget);\n },\n });\n}\n\nfunction mergeAgentSettings(settings: any, telemetryKey: TelemetryKey): any {\n const merged = mergeTelemetry(settings, telemetryKey);\n const userPrepareCall = settings.prepareCall;\n if (typeof userPrepareCall !== 'function') return merged;\n\n return {\n ...merged,\n prepareCall: async function wrappedPrepareCall(\n this: unknown,\n ...args: any[]\n ) {\n const prepared = await Reflect.apply(userPrepareCall, this, args);\n return prepared == null\n ? prepared\n : mergeTelemetry(\n prepared,\n telemetryKey,\n settings.telemetry ?? settings.experimental_telemetry,\n );\n },\n };\n}\n\nfunction rootSpanKind(name: WrappedFunctionName): string {\n if (name === 'embed' || name === 'embedMany' || name === 'rerank')\n return 'CHAIN';\n return 'WORKFLOW';\n}\n\nfunction getParentContext() {\n // Our parent comes solely from the private span store; a\n // foreign provider's active span must never become our ancestor.\n return getNeatlogsParentContext();\n}\n\nfunction createAsyncWrapper(\n name: WrappedFunctionName,\n original: (opts: any) => Promise<unknown>,\n telemetryKey: TelemetryKey,\n): (opts: any) => Promise<unknown> {\n return async function wrappedAsyncFn(opts: any): Promise<unknown> {\n const tracer = getNeatlogsTracer(TRACER_NAME);\n // startSpan (NOT startActiveSpan) + withNeatlogsSpan: startActiveSpan would\n // push our span onto the GLOBAL OTel context, so a foreign tracer's\n // startSpan() inside generateText() would read it as parent and inherit our\n // trace id. withNeatlogsSpan carries the parent in the private store in\n // the private context, leaving the global context untouched.\n const parentContext = getParentContext();\n const span = tracer.startSpan(\n `ai.${name}`,\n { attributes: { 'openinference.span.kind': rootSpanKind(name) } },\n parentContext,\n );\n return withNeatlogsSpan(\n span,\n async () => {\n try {\n const isEmbedOrRerank =\n name === 'embed' || name === 'embedMany' || name === 'rerank';\n if (!isEmbedOrRerank) {\n setInputValue(span, opts);\n }\n if (name === 'rerank' && opts?.query) {\n span.setAttribute('ai.rerank.query', String(opts.query));\n }\n const merged = mergeTelemetry(opts, telemetryKey);\n const result = await original(merged);\n if (!isEmbedOrRerank) {\n setOutputValue(span, result);\n }\n return result;\n } catch (err) {\n recordSpanError(span, err);\n throw err;\n } finally {\n span.end();\n }\n },\n parentContext,\n );\n };\n}\n\n// streamText/streamObject return synchronously while the model keeps producing\n// tokens for seconds afterwards. Ending the span in a `finally` (as a plain sync\n// wrapper would) closes it in ~2ms with no output — the output only exists once\n// the stream finishes. Instead we keep the span open and end it from the AI SDK's\n// `onFinish` callback, where the final text/object is available. Any user-provided\n// `onFinish` is preserved and invoked first.\nfunction createStreamWrapper(\n name: WrappedFunctionName,\n original: (opts: any) => unknown,\n telemetryKey: TelemetryKey,\n): (opts: any) => unknown {\n return function wrappedStreamFn(opts: any): unknown {\n const tracer = getNeatlogsTracer(TRACER_NAME);\n // startSpan + withNeatlogsSpan (see createAsyncWrapper) so streamText's\n // internals never see our span on the global OTel context. The span stays\n // open past the run scope and is ended from onFinish/onError.\n const parentContext = getParentContext();\n const span = tracer.startSpan(\n `ai.${name}`,\n { attributes: { 'openinference.span.kind': rootSpanKind(name) } },\n parentContext,\n );\n return withNeatlogsSpan(\n span,\n () => {\n let spanEnded = false;\n const endOnce = () => {\n if (spanEnded) return;\n spanEnded = true;\n span.end();\n };\n try {\n setInputValue(span, opts);\n const merged = mergeTelemetry(opts, telemetryKey);\n const userOnFinish = opts?.onFinish;\n const userOnError = opts?.onError;\n const wrappedOpts = {\n ...merged,\n onFinish: async (event: any) => {\n try {\n setStreamOutputValue(span, event);\n } finally {\n endOnce();\n }\n if (typeof userOnFinish === 'function') {\n return userOnFinish(event);\n }\n },\n onError: (event: any) => {\n recordSpanError(span, (event && event.error) ?? event);\n endOnce();\n if (typeof userOnError === 'function') {\n return userOnError(event);\n }\n },\n };\n return original(wrappedOpts);\n } catch (err) {\n // Synchronous throw (e.g. bad arguments) — the stream never started.\n recordSpanError(span, err);\n endOnce();\n throw err;\n }\n },\n parentContext,\n );\n };\n}\n\nfunction createMirroredSpan(primary: Span, secondary: Span): Span {\n const mirrored: Span = {\n spanContext() {\n // The caller-owned tracer remains the process-global context owner. Its\n // context is therefore the one external instrumentation must observe.\n return primary.spanContext();\n },\n setAttribute(key, value) {\n primary.setAttribute(key, value);\n secondary.setAttribute(key, value);\n return mirrored;\n },\n setAttributes(attributes) {\n primary.setAttributes(attributes);\n secondary.setAttributes(attributes);\n return mirrored;\n },\n addEvent(name, attributesOrStartTime, startTime) {\n primary.addEvent(name, attributesOrStartTime, startTime);\n secondary.addEvent(name, attributesOrStartTime, startTime);\n return mirrored;\n },\n addLink(link) {\n primary.addLink(link);\n secondary.addLink(link);\n return mirrored;\n },\n addLinks(links) {\n primary.addLinks(links);\n secondary.addLinks(links);\n return mirrored;\n },\n setStatus(status) {\n primary.setStatus(status);\n secondary.setStatus(status);\n return mirrored;\n },\n updateName(name) {\n primary.updateName(name);\n secondary.updateName(name);\n return mirrored;\n },\n end(endTime) {\n primary.end(endTime);\n secondary.end(endTime);\n },\n isRecording() {\n return primary.isRecording() || secondary.isRecording();\n },\n recordException(exception, time) {\n primary.recordException(exception, time);\n secondary.recordException(exception, time);\n },\n };\n return mirrored;\n}\n\n/**\n * Mirror one AI SDK telemetry stream to two isolated tracer pipelines.\n *\n * The caller-owned tracer is deliberately outermost so its normal global OTel\n * activation and parentage stay unchanged. The Neatlogs routing tracer keeps\n * its span active only in Neatlogs' private AsyncLocalStorage context.\n */\nfunction createMirroredTracer(primary: Tracer, secondary: Tracer): Tracer {\n return {\n startSpan(name: string, options?: SpanOptions, context?: Context): Span {\n const primarySpan = primary.startSpan(name, options, context);\n const secondarySpan = secondary.startSpan(name, options);\n return createMirroredSpan(primarySpan, secondarySpan);\n },\n startActiveSpan: (<F extends (span: Span) => unknown>(\n name: string,\n arg2?: SpanOptions | F,\n arg3?: Context | F,\n arg4?: F,\n ): ReturnType<F> => {\n let options: SpanOptions | undefined;\n let context: Context | undefined;\n let fn: F;\n\n if (typeof arg2 === 'function') {\n fn = arg2;\n } else if (typeof arg3 === 'function') {\n options = arg2;\n fn = arg3;\n } else {\n options = arg2;\n context = arg3 as Context;\n fn = arg4!;\n }\n\n const runSecondary = (primarySpan: Span): ReturnType<F> => {\n const onSecondarySpan = (secondarySpan: Span) =>\n fn(createMirroredSpan(primarySpan, secondarySpan)) as ReturnType<F>;\n return options === undefined\n ? secondary.startActiveSpan(name, onSecondarySpan)\n : secondary.startActiveSpan(name, options, onSecondarySpan);\n };\n\n if (context !== undefined) {\n return primary.startActiveSpan(\n name,\n options ?? {},\n context,\n runSecondary,\n ) as ReturnType<F>;\n }\n return options === undefined\n ? (primary.startActiveSpan(name, runSecondary) as ReturnType<F>)\n : (primary.startActiveSpan(name, options, runSecondary) as ReturnType<F>);\n }) as Tracer['startActiveSpan'],\n };\n}\n\ntype TelemetryKey = 'telemetry' | 'experimental_telemetry';\n\nfunction isNeatlogsV7Integration(\n integration: unknown,\n): integration is LazyV7OpenTelemetryIntegration {\n return (\n typeof integration === 'object' &&\n integration !== null &&\n NEATLOGS_V7_INTEGRATION in integration\n );\n}\n\nfunction mergeTelemetry(\n opts: any,\n telemetryKey: TelemetryKey,\n fallbackTelemetry?: any,\n): any {\n const legacyTelemetry = opts?.experimental_telemetry ?? {};\n const v7Telemetry = opts?.telemetry ?? {};\n const preferredTelemetry =\n telemetryKey === 'telemetry'\n ? { ...legacyTelemetry, ...v7Telemetry }\n : { ...v7Telemetry, ...legacyTelemetry };\n const requestedTelemetry = {\n ...fallbackTelemetry,\n ...preferredTelemetry,\n metadata: {\n ...fallbackTelemetry?.metadata,\n ...legacyTelemetry.metadata,\n ...v7Telemetry.metadata,\n },\n };\n const existingNeatlogsIntegration = Array.isArray(\n requestedTelemetry.integrations,\n )\n ? requestedTelemetry.integrations.find(isNeatlogsV7Integration)\n : undefined;\n const baseTelemetry: AITelemetryConfig = existingNeatlogsIntegration\n ? {\n isEnabled: true,\n recordInputs: true,\n recordOutputs: true,\n tracer: requestedTelemetry.tracer as Tracer,\n ...(requestedTelemetry.functionId !== undefined\n ? { functionId: requestedTelemetry.functionId }\n : {}),\n metadata: requestedTelemetry.metadata,\n integrations: [existingNeatlogsIntegration],\n }\n : createAITelemetry({\n functionId: requestedTelemetry.functionId,\n metadata: requestedTelemetry.metadata,\n tracer: requestedTelemetry.tracer as Tracer | undefined,\n });\n const callerTracer = requestedTelemetry.tracer as Tracer | undefined;\n const hasCallerTracer = callerTracer !== undefined;\n const requestedIntegrations = Array.isArray(requestedTelemetry.integrations)\n ? requestedTelemetry.integrations.filter(\n (integration: unknown) => !isNeatlogsV7Integration(integration),\n )\n : [];\n const { telemetry: _telemetry, experimental_telemetry: _legacy, ...rest } =\n opts ?? {};\n\n return {\n ...rest,\n [telemetryKey]: {\n ...baseTelemetry,\n ...requestedTelemetry,\n isEnabled: true,\n recordInputs: requestedTelemetry.recordInputs ?? true,\n recordOutputs: requestedTelemetry.recordOutputs ?? true,\n tracer: baseTelemetry.tracer,\n // Do not add Neatlogs-only marker metadata to a caller-owned telemetry\n // pipeline such as Laminar. Both providers receive the same AI SDK span\n // data, while their providers, parent contexts, and exporters stay separate.\n metadata: hasCallerTracer\n ? requestedTelemetry.metadata\n : baseTelemetry.metadata,\n integrations: [...baseTelemetry.integrations, ...requestedIntegrations],\n },\n };\n}\n\nfunction recordSpanError(span: Span, err: unknown): void {\n if (err instanceof Error) {\n span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });\n span.recordException(err);\n } else {\n span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });\n }\n}\n","/**\n * Neatlogs-owned tracing state.\n *\n * Spans are created by the private Neatlogs provider and their parent is carried\n * in a private context key. The process-global OpenTelemetry span is\n * deliberately left untouched so other observability SDKs cannot export or\n * become parents of Neatlogs spans.\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport {\n ROOT_CONTEXT,\n INVALID_SPAN_CONTEXT,\n createContextKey,\n trace as otelTrace,\n type Context,\n type Span,\n type SpanOptions,\n type Tracer,\n type TracerProvider,\n} from '@opentelemetry/api';\nimport { getActiveClient } from './active-client.js';\n\n// Carries the trace-ROOT span down the private context so descendants can target\n// it (e.g. setTraceOutput). getActiveNeatlogsSpan() returns the innermost span,\n// which is not the root once nested; this key preserves the root reference.\nconst NEATLOGS_ROOT_SPAN_KEY = createContextKey('neatlogs.root_span');\n\n// Entry points are bundled independently (`neatlogs`, `neatlogs/openai`,\n// `neatlogs/ai`, `neatlogs/mastra`, … each in both CJS and ESM), so every piece\n// of shared tracing state — the private span store AND the resolved provider /\n// provider — must live on `globalThis` behind a `Symbol.for` key.\n// Otherwise `init()` (run from the `neatlogs` bundle) sets `_provider` in ITS\n// module copy while a wrapper imported from `neatlogs/openai` reads a different,\n// still-null copy and silently falls back to the foreign global provider.\nconst PRIVATE_SPAN_STORAGE_KEY = Symbol.for(\n 'neatlogs.private_span_async_local_storage',\n);\nconst PRIVATE_PROVIDER_STATE_KEY = Symbol.for(\n 'neatlogs.private_provider_state',\n);\ninterface PrivateProviderState {\n provider: TracerProvider | null;\n}\ntype NeatlogsGlobal = typeof globalThis & {\n [PRIVATE_SPAN_STORAGE_KEY]?: AsyncLocalStorage<Context>;\n [PRIVATE_PROVIDER_STATE_KEY]?: PrivateProviderState;\n};\nconst neatlogsGlobal = globalThis as NeatlogsGlobal;\n// Stores the full Neatlogs Context (parent span PLUS any threaded values such as\n// trace()'s prompt-template keys), not just the span. We never\n// activate the OTel global context, so this private store is the ONLY channel\n// through which those values propagate down to descendant spans.\nconst privateContextStorage =\n neatlogsGlobal[PRIVATE_SPAN_STORAGE_KEY] ??\n (neatlogsGlobal[PRIVATE_SPAN_STORAGE_KEY] = new AsyncLocalStorage<Context>());\nconst providerState: PrivateProviderState =\n neatlogsGlobal[PRIVATE_PROVIDER_STATE_KEY] ??\n (neatlogsGlobal[PRIVATE_PROVIDER_STATE_KEY] = {\n provider: null,\n });\n// Wrappers may be constructed or accidentally invoked before init(). They must\n// never fall back to a foreign process-global provider, so pre-init calls use a\n// local no-op tracer and safely emit no exported spans.\nconst preInitTracer: Tracer = {\n startSpan(): Span {\n return otelTrace.wrapSpanContext(INVALID_SPAN_CONTEXT);\n },\n startActiveSpan<F extends (span: Span) => unknown>(\n _name: string,\n arg2?: SpanOptions | Context | F,\n arg3?: Context | F,\n arg4?: F,\n ): ReturnType<F> {\n const fn =\n typeof arg2 === 'function'\n ? arg2\n : typeof arg3 === 'function'\n ? arg3\n : arg4;\n return fn!(otelTrace.wrapSpanContext(INVALID_SPAN_CONTEXT)) as ReturnType<F>;\n },\n};\n\n/** @internal Configure the provider used by Neatlogs-created spans. */\nexport function _setNeatlogsProvider(provider: TracerProvider | null): void {\n providerState.provider = provider;\n}\n\n/** Resolve a tracer from the private provider when one is configured. */\nexport function getNeatlogsTracer(name: string): Tracer {\n const client = getActiveClient();\n if (client) return client.getTracer(name);\n return providerState.provider?.getTracer(name) ?? preInitTracer;\n}\n\n/** A reusable tracer facade that resolves the active Client on every call. */\nexport function getRoutingNeatlogsTracer(name: string): Tracer {\n return {\n startSpan(spanName: string, options?: SpanOptions, context?: Context): Span {\n return isolateTracer(getNeatlogsTracer(name)).startSpan(\n spanName,\n options,\n context,\n );\n },\n startActiveSpan: ((...args: any[]) =>\n (isolateTracer(getNeatlogsTracer(name)).startActiveSpan as (...inner: any[]) => any)(\n ...args,\n )) as Tracer['startActiveSpan'],\n };\n}\n\n/**\n * @internal The private Neatlogs provider, or null before\n * init(). Used by integrations that must repoint a self-instrumenting library's\n * captured provider onto ours.\n */\nexport function getNeatlogsProvider(): TracerProvider | null {\n const client = getActiveClient();\n if (client) return client.tracerProvider;\n return providerState.provider;\n}\n\n/** Run a Client callback without inheriting another pipeline's private span. */\nexport function runWithFreshNeatlogsContext<T>(fn: () => T): T {\n return privateContextStorage.run(ROOT_CONTEXT, fn);\n}\n\n/**\n * Wrap a tracer so that spans it creates parent from — and, for\n * `startActiveSpan`, activate on — the PRIVATE Neatlogs context instead of the\n * global one.\n *\n * We hand this to libraries that create their own spans off a tracer we give\n * them (the Vercel AI SDK's `experimental_telemetry.tracer`, which calls\n * `tracer.startActiveSpan()` internally). Without the facade the AI SDK's native\n * spans would parent from `context.active()` — the foreign co-tenant's context —\n * and `startActiveSpan` would push them onto the GLOBAL context, so a foreign\n * tracer's next span reads our native span as its parent. Both directions leak.\n *\n */\nexport function isolateTracer(tracer: Tracer): Tracer {\n const facade: Tracer = {\n startSpan(name: string, options?: SpanOptions, context?: Context): Span {\n const parent = context ?? getNeatlogsActiveContext();\n return tracer.startSpan(name, options, parent);\n },\n startActiveSpan<F extends (span: Span) => unknown>(\n name: string,\n arg2?: SpanOptions | Context | F,\n arg3?: Context | F,\n arg4?: F,\n ): ReturnType<F> {\n // Normalize the 2/3/4-arg overloads of startActiveSpan.\n let options: SpanOptions | undefined;\n let context: Context | undefined;\n let fn: F;\n if (typeof arg2 === 'function') {\n fn = arg2 as F;\n } else if (typeof arg3 === 'function') {\n options = arg2 as SpanOptions;\n fn = arg3 as F;\n } else {\n options = arg2 as SpanOptions;\n context = arg3 as Context;\n fn = arg4 as F;\n }\n const parent = context ?? getNeatlogsActiveContext();\n const span = tracer.startSpan(name, options, parent);\n return withNeatlogsSpan(span, () => fn(span), parent) as ReturnType<F>;\n },\n };\n return facade;\n}\n\n/**\n * The base context to build new Neatlogs spans and values on. NEVER contains a\n * foreign provider's span: it reads our private store, or ROOT_CONTEXT when\n * nothing is active.\n */\nexport function getNeatlogsActiveContext(): Context {\n return privateContextStorage.getStore() ?? ROOT_CONTEXT;\n}\n\n/** Return only the active Neatlogs span (never a foreign provider's span). */\nexport function getActiveNeatlogsSpan(): Span | undefined {\n return otelTrace.getSpan(getNeatlogsActiveContext());\n}\n\n/**\n * Return the trace-ROOT Neatlogs span, or undefined when no trace is active.\n *\n * Unlike {@link getActiveNeatlogsSpan} (innermost), this is the outermost span\n * of the current trace — the one the backend derives trace-level output from\n * (`parent_span_id=''`). It is stashed on the private context the first time a\n * span becomes active, so nested calls still resolve to the root.\n */\nexport function getNeatlogsRootSpan(): Span | undefined {\n return getNeatlogsActiveContext().getValue(NEATLOGS_ROOT_SPAN_KEY) as\n | Span\n | undefined;\n}\n\n/**\n * Build a parent context that cannot contain a foreign provider's span.\n *\n * The active Neatlogs context already carries the parent span AND any values\n * the caller threaded in upstream (e.g. `trace()`'s prompt-template values), so\n * those values reach the span processor via `onStart(parentContext)`. An\n * explicit `baseContext` (a caller's own value-carrying context) is honored as-is.\n */\nexport function getNeatlogsParentContext(baseContext?: Context): Context {\n return baseContext ?? getNeatlogsActiveContext();\n}\n\n/**\n * A base context for callers that thread parent linkage themselves\n * (callback/event handlers that keep their own run-id → span map: LangChain,\n * OpenAI-Agents, Claude Agent SDK).\n *\n * This is the ACTIVE Neatlogs context — the private store's\n * context if a Neatlogs `trace()`/`span()` encloses this call, else\n * ROOT_CONTEXT. So a handler's own root/entry span nests under an enclosing\n * Neatlogs trace (preserving its session + end-user id) when one exists, and\n * auto-roots cleanly when one doesn't. A foreign provider's active span can\n * never leak in as an ancestor because the global context is never read.\n */\nexport function getNeatlogsBaseContext(baseContext?: Context): Context {\n return baseContext ?? getNeatlogsActiveContext();\n}\n\n/**\n * Build an execution context for a Neatlogs span while preserving the active\n * foreign context. The span rides our private context store.\n */\nexport function getNeatlogsExecutionContext(\n span: Span,\n baseContext: Context = ROOT_CONTEXT,\n): Context {\n return otelTrace.setSpan(baseContext, span);\n}\n\n/**\n * Run a callback with a Neatlogs span active under the appropriate policy.\n *\n * The stored context is `setSpan(base, span)` — carrying the span PLUS whatever\n * values `base` holds — so threaded values (prompt templates)\n * propagate to descendant spans through our private store instead of the global\n * OTel context we deliberately never touch.\n */\nexport function withNeatlogsSpan<T>(\n span: Span,\n fn: () => T,\n baseContext?: Context,\n rootSpan?: Span,\n): T {\n const base = baseContext ?? getNeatlogsActiveContext();\n let ctx = otelTrace.setSpan(base, span);\n // The first span activated in a context with no root recorded IS the root of\n // this trace; remember it so descendants (setTraceOutput) can target it.\n if (base.getValue(NEATLOGS_ROOT_SPAN_KEY) === undefined) {\n ctx = ctx.setValue(NEATLOGS_ROOT_SPAN_KEY, rootSpan ?? span);\n }\n return privateContextStorage.run(ctx, fn);\n}\n\n/**\n * @internal Run with a private Neatlogs parent without treating that parent as\n * a locally-recording trace root. This is used for an extracted remote parent:\n * the first local recording span should remain the target for local trace-level\n * output, while still inheriting the remote trace/span IDs.\n */\nexport function withNeatlogsRemoteParent<T>(span: Span, fn: () => T): T {\n return privateContextStorage.run(otelTrace.setSpan(ROOT_CONTEXT, span), fn);\n}\n","import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Tracer, TracerProvider } from '@opentelemetry/api';\n\nexport interface ActiveNeatlogsClient {\n readonly workflowName: string;\n readonly tracerProvider: TracerProvider;\n getTracer(scope: string): Tracer;\n getLogger(): any | null;\n}\n\nconst ACTIVE_CLIENT_STORAGE_KEY = Symbol.for(\n 'neatlogs.active_client_async_local_storage',\n);\ntype NeatlogsGlobal = typeof globalThis & {\n [ACTIVE_CLIENT_STORAGE_KEY]?: AsyncLocalStorage<ActiveNeatlogsClient>;\n};\nconst neatlogsGlobal = globalThis as NeatlogsGlobal;\nconst storage =\n neatlogsGlobal[ACTIVE_CLIENT_STORAGE_KEY] ??\n (neatlogsGlobal[ACTIVE_CLIENT_STORAGE_KEY] =\n new AsyncLocalStorage<ActiveNeatlogsClient>());\n\nexport function getActiveClient(): ActiveNeatlogsClient | undefined {\n return storage.getStore();\n}\n\nexport function runWithClient<T>(\n client: ActiveNeatlogsClient,\n fn: () => T,\n): T {\n return storage.run(client, fn);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,IAAAA,cAOO;;;ACXP,IAAAC,2BAAkC;AAClC,iBAUO;;;ACpBP,8BAAkC;AAUlC,IAAM,4BAA4B,uBAAO;AAAA,EACvC;AACF;AAIA,IAAM,iBAAiB;AACvB,IAAM,UACJ,eAAe,yBAAyB,MACvC,eAAe,yBAAyB,IACvC,IAAI,0CAAwC;AAEzC,SAAS,kBAAoD;AAClE,SAAO,QAAQ,SAAS;AAC1B;;;ADEA,IAAM,6BAAyB,6BAAiB,oBAAoB;AASpE,IAAM,2BAA2B,uBAAO;AAAA,EACtC;AACF;AACA,IAAM,6BAA6B,uBAAO;AAAA,EACxC;AACF;AAQA,IAAMC,kBAAiB;AAKvB,IAAM,wBACJA,gBAAe,wBAAwB,MACtCA,gBAAe,wBAAwB,IAAI,IAAI,2CAA2B;AAC7E,IAAM,gBACJA,gBAAe,0BAA0B,MACxCA,gBAAe,0BAA0B,IAAI;AAAA,EAC5C,UAAU;AACZ;AAIF,IAAM,gBAAwB;AAAA,EAC5B,YAAkB;AAChB,WAAO,WAAAC,MAAU,gBAAgB,+BAAoB;AAAA,EACvD;AAAA,EACA,gBACE,OACA,MACA,MACA,MACe;AACf,UAAM,KACJ,OAAO,SAAS,aACZ,OACA,OAAO,SAAS,aACd,OACA;AACR,WAAO,GAAI,WAAAA,MAAU,gBAAgB,+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