bitfab 0.54.10 → 0.54.11

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.
@@ -6,7 +6,7 @@ import {
6
6
  replayContextReady,
7
7
  runWithReplayContext,
8
8
  warnOnce
9
- } from "./chunk-B2O33DBH.js";
9
+ } from "./chunk-FMUJONTQ.js";
10
10
  import {
11
11
  runWithCaptureSuppressed
12
12
  } from "./chunk-VCRFFCLY.js";
@@ -1257,4 +1257,4 @@ export {
1257
1257
  sleepForReplayPersistence,
1258
1258
  replay
1259
1259
  };
1260
- //# sourceMappingURL=chunk-PRXW4B5X.js.map
1260
+ //# sourceMappingURL=chunk-G4LWALQ7.js.map
@@ -19,7 +19,7 @@ import {
19
19
  serializeValue,
20
20
  toJsonSafe,
21
21
  toJsonSafeReport
22
- } from "./chunk-PRXW4B5X.js";
22
+ } from "./chunk-G4LWALQ7.js";
23
23
  import {
24
24
  BitfabError,
25
25
  DEFAULT_SERVICE_URL,
@@ -36,7 +36,7 @@ import {
36
36
  recordCallerTraceMetadata,
37
37
  retireTraceMetadata,
38
38
  warnOnce
39
- } from "./chunk-B2O33DBH.js";
39
+ } from "./chunk-FMUJONTQ.js";
40
40
  import {
41
41
  __privateAdd,
42
42
  __privateGet,
@@ -2441,6 +2441,83 @@ var BitfabOpenAIAgentHandler = class {
2441
2441
  }
2442
2442
  };
2443
2443
 
2444
+ // src/openaiAgentsSpanContext.ts
2445
+ var OPENAI_AGENTS_CONTEXT_STORAGE = /* @__PURE__ */ Symbol.for(
2446
+ "openai.agents.core.asyncLocalStorage"
2447
+ );
2448
+ var MAX_OPENAI_AGENTS_SPAN_CHAIN = 64;
2449
+ var spanGlobal = globalThis;
2450
+ var state = spanGlobal.__bitfabOpenAiAgentsSpansV1 ?? {
2451
+ bitfabTraceIds: /* @__PURE__ */ new Map(),
2452
+ endedSpanIds: /* @__PURE__ */ new Map()
2453
+ };
2454
+ spanGlobal.__bitfabOpenAiAgentsSpansV1 = state;
2455
+ function mapOpenAiAgentsTrace(agentsTraceId, bitfabTraceId) {
2456
+ state.bitfabTraceIds.set(agentsTraceId, bitfabTraceId);
2457
+ }
2458
+ function unmapOpenAiAgentsTrace(agentsTraceId) {
2459
+ state.bitfabTraceIds.delete(agentsTraceId);
2460
+ state.endedSpanIds.delete(agentsTraceId);
2461
+ }
2462
+ function endOpenAiAgentsSpan(agentsTraceId, spanId) {
2463
+ if (!state.bitfabTraceIds.has(agentsTraceId)) {
2464
+ return;
2465
+ }
2466
+ const ended = state.endedSpanIds.get(agentsTraceId) ?? /* @__PURE__ */ new Set();
2467
+ ended.add(spanId);
2468
+ state.endedSpanIds.set(agentsTraceId, ended);
2469
+ }
2470
+ function isOpenAiAgentsContextStorage(value) {
2471
+ return value !== null && typeof value === "object" && typeof value.getStore === "function";
2472
+ }
2473
+ function currentOpenAiAgentsSpan() {
2474
+ const storage2 = spanGlobal[OPENAI_AGENTS_CONTEXT_STORAGE];
2475
+ if (!isOpenAiAgentsContextStorage(storage2)) {
2476
+ return void 0;
2477
+ }
2478
+ try {
2479
+ const store = storage2.getStore();
2480
+ return store?.active === true ? store.span ?? void 0 : void 0;
2481
+ } catch {
2482
+ return void 0;
2483
+ }
2484
+ }
2485
+ function currentOpenAiAgentsSpans() {
2486
+ const spans = [];
2487
+ let span = currentOpenAiAgentsSpan();
2488
+ while (span !== void 0 && spans.length < MAX_OPENAI_AGENTS_SPAN_CHAIN) {
2489
+ spans.push(span);
2490
+ span = span.previousSpan ?? void 0;
2491
+ }
2492
+ return spans;
2493
+ }
2494
+ function openAiAgentsSpanChain() {
2495
+ if (state.bitfabTraceIds.size === 0) {
2496
+ return [];
2497
+ }
2498
+ return currentOpenAiAgentsSpans().map((span) => span.spanId).filter((spanId) => typeof spanId === "string");
2499
+ }
2500
+ function openAiAgentsParentSpanId(bitfabTraceId, enclosingSpanIds) {
2501
+ if (state.bitfabTraceIds.size === 0) {
2502
+ return void 0;
2503
+ }
2504
+ for (const span of currentOpenAiAgentsSpans()) {
2505
+ if (typeof span.spanId !== "string" || typeof span.traceId !== "string") {
2506
+ return void 0;
2507
+ }
2508
+ if (state.bitfabTraceIds.get(span.traceId) !== bitfabTraceId) {
2509
+ return void 0;
2510
+ }
2511
+ if (enclosingSpanIds?.includes(span.spanId)) {
2512
+ return void 0;
2513
+ }
2514
+ if (!state.endedSpanIds.get(span.traceId)?.has(span.spanId)) {
2515
+ return span.spanId;
2516
+ }
2517
+ }
2518
+ return void 0;
2519
+ }
2520
+
2444
2521
  // src/replayBranch.ts
2445
2522
  var _url, _context;
2446
2523
  var ReplayBranch = class {
@@ -3137,6 +3214,9 @@ var BitfabOpenAITracingProcessor = class {
3137
3214
  const activeContext = this.getActiveSpanContext?.();
3138
3215
  if (activeContext) {
3139
3216
  this.activeSpanMappings[trace.traceId] = activeContext;
3217
+ if (this.shouldRecord()) {
3218
+ mapOpenAiAgentsTrace(trace.traceId, activeContext.traceId);
3219
+ }
3140
3220
  }
3141
3221
  const canonicalTraceId = activeContext?.traceId ?? this.getCanonicalTraceId(trace.traceId);
3142
3222
  this.canonicalTraceIds[trace.traceId] = canonicalTraceId;
@@ -3174,6 +3254,7 @@ var BitfabOpenAITracingProcessor = class {
3174
3254
  }
3175
3255
  this.endedTraces.delete(traceId);
3176
3256
  delete this.activeSpanMappings[traceId];
3257
+ unmapOpenAiAgentsTrace(traceId);
3177
3258
  delete this.canonicalTraceIds[traceId];
3178
3259
  delete this.activeTraces[traceId];
3179
3260
  }
@@ -3198,6 +3279,9 @@ var BitfabOpenAITracingProcessor = class {
3198
3279
  async onSpanEnd(span) {
3199
3280
  const originalTraceId = span.traceId ?? "";
3200
3281
  const traceId = this.activeSpanMappings[originalTraceId]?.traceId ?? originalTraceId;
3282
+ if (span.spanId !== void 0) {
3283
+ endOpenAiAgentsSpan(originalTraceId, span.spanId);
3284
+ }
3201
3285
  try {
3202
3286
  this.sendSpan(span);
3203
3287
  } finally {
@@ -3218,6 +3302,9 @@ var BitfabOpenAITracingProcessor = class {
3218
3302
  * Called when the trace processor is shutting down.
3219
3303
  */
3220
3304
  async shutdown(timeout) {
3305
+ for (const traceId of Object.keys(this.activeSpanMappings)) {
3306
+ unmapOpenAiAgentsTrace(traceId);
3307
+ }
3221
3308
  this.activeTraces = {};
3222
3309
  this.activeSpanMappings = {};
3223
3310
  this.canonicalTraceIds = {};
@@ -4374,16 +4461,16 @@ var Bitfab = class {
4374
4461
  this.simulationPlan.send(payload, rootTraceFunctionKeyOf(payload), submit);
4375
4462
  }
4376
4463
  refreshAutoTraceCapturePolicy(traceFunctionKey) {
4377
- const state = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
4378
- this.autoTracePolicyRefreshes.set(traceFunctionKey, state);
4379
- state.run(
4464
+ const state2 = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
4465
+ this.autoTracePolicyRefreshes.set(traceFunctionKey, state2);
4466
+ state2.run(
4380
4467
  () => this.httpClient.getAutoTracePolicy(
4381
4468
  traceFunctionKey,
4382
4469
  AUTO_TRACE_PROTOCOL
4383
4470
  ),
4384
4471
  (policy) => {
4385
4472
  if (policy.protocol !== AUTO_TRACE_PROTOCOL) {
4386
- state.hold(AUTO_TRACE_POLICY_RETRY_MS);
4473
+ state2.hold(AUTO_TRACE_POLICY_RETRY_MS);
4387
4474
  return;
4388
4475
  }
4389
4476
  const functionIds = Array.isArray(policy.functionIds) ? policy.functionIds.filter(
@@ -4394,10 +4481,10 @@ var Bitfab = class {
4394
4481
  traceFunctionKey,
4395
4482
  policy.revision === null ? void 0 : functionIds
4396
4483
  );
4397
- state.hold(AUTO_TRACE_POLICY_REFRESH_MS);
4484
+ state2.hold(AUTO_TRACE_POLICY_REFRESH_MS);
4398
4485
  },
4399
4486
  () => {
4400
- state.hold(AUTO_TRACE_POLICY_RETRY_MS);
4487
+ state2.hold(AUTO_TRACE_POLICY_RETRY_MS);
4401
4488
  }
4402
4489
  );
4403
4490
  }
@@ -5008,7 +5095,11 @@ var Bitfab = class {
5008
5095
  const seedCtxForTraceId = parentContext ? null : getSeedContext();
5009
5096
  const traceId = parentContext?.traceId ?? replayCtxForTraceId?.traceId ?? seedCtxForTraceId?.traceId ?? randomUuid();
5010
5097
  const spanId = randomUuid();
5011
- const parentSpanId = parentContext?.spanId ?? null;
5098
+ const openAiAgentsParent = parentContext !== void 0 && options.instrumentation === "trace" ? openAiAgentsParentSpanId(
5099
+ parentContext.traceId,
5100
+ parentContext.openAiAgentsSpanIds
5101
+ ) : void 0;
5102
+ const parentSpanId = openAiAgentsParent ?? parentContext?.spanId ?? null;
5012
5103
  const isRootSpan = parentSpanId === null;
5013
5104
  if (options.enclosingTrace !== void 0) {
5014
5105
  options.enclosingTrace.link.traceId = traceId;
@@ -5022,11 +5113,13 @@ var Bitfab = class {
5022
5113
  parentContext?.surface,
5023
5114
  traceFunctionKey
5024
5115
  );
5116
+ const openAiAgentsSpanIds = openAiAgentsSpanChain();
5025
5117
  const newContext = {
5026
5118
  traceId,
5027
5119
  spanId,
5028
5120
  contexts: [],
5029
- ...surface !== void 0 && { surface }
5121
+ ...surface !== void 0 && { surface },
5122
+ ...openAiAgentsSpanIds.length > 0 && { openAiAgentsSpanIds }
5030
5123
  };
5031
5124
  newStack = [...currentStack, newContext];
5032
5125
  const inputs = args;
@@ -5801,7 +5894,7 @@ var Bitfab = class {
5801
5894
  await runWithSeedContext({ traceId }, async () => target(...args));
5802
5895
  } finally {
5803
5896
  try {
5804
- const { flushTraces: flushTraces2 } = await import("./http-AVWWAMFV.js");
5897
+ const { flushTraces: flushTraces2 } = await import("./http-AXPT27DO.js");
5805
5898
  await flushTraces2(3e4);
5806
5899
  } finally {
5807
5900
  unrecorded = activeTraceStates.delete(traceId);
@@ -5860,7 +5953,7 @@ var Bitfab = class {
5860
5953
  `Function is wrapped with trace function key '${wrappedKey}' but replay was called with '${traceFunctionKey}'. Pass matching keys, or pass the unwrapped function to replay it under the explicit key.`
5861
5954
  );
5862
5955
  }
5863
- const { replay: doReplay } = await import("./replay-BCWIDCKH.js");
5956
+ const { replay: doReplay } = await import("./replay-DOG4KV7I.js");
5864
5957
  return doReplay(
5865
5958
  this.httpClient,
5866
5959
  this.serviceUrl,
@@ -6180,7 +6273,7 @@ async function seedFromRegistry(registry, pipeline, cases, options = {}) {
6180
6273
  sessionId: seedCase.sessionId
6181
6274
  })
6182
6275
  );
6183
- const { flushTraces: flushTraces2 } = await import("./http-AVWWAMFV.js");
6276
+ const { flushTraces: flushTraces2 } = await import("./http-AXPT27DO.js");
6184
6277
  await flushTraces2(3e4);
6185
6278
  return { pipeline, traceFunctionKey, traceIds };
6186
6279
  }
@@ -6219,4 +6312,4 @@ export {
6219
6312
  reseedFromRegistry,
6220
6313
  seedFromRegistry
6221
6314
  };
6222
- //# sourceMappingURL=chunk-A3OBQOLV.js.map
6315
+ //# sourceMappingURL=chunk-MEJOPEKV.js.map