@bitfab/sdk 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.
package/dist/index.d.cts CHANGED
@@ -3602,7 +3602,7 @@ interface CommitRef {
3602
3602
  /**
3603
3603
  * SDK version from package.json (injected at build time)
3604
3604
  */
3605
- declare const __version__ = "0.54.10";
3605
+ declare const __version__ = "0.54.11";
3606
3606
 
3607
3607
  /**
3608
3608
  * Constants for the Bitfab SDK.
package/dist/index.d.ts CHANGED
@@ -3602,7 +3602,7 @@ interface CommitRef {
3602
3602
  /**
3603
3603
  * SDK version from package.json (injected at build time)
3604
3604
  */
3605
- declare const __version__ = "0.54.10";
3605
+ declare const __version__ = "0.54.11";
3606
3606
 
3607
3607
  /**
3608
3608
  * Constants for the Bitfab SDK.
package/dist/index.js CHANGED
@@ -21,7 +21,7 @@ import {
21
21
  getCurrentTrace,
22
22
  reseedFromRegistry,
23
23
  seedFromRegistry
24
- } from "./chunk-3F7SICKD.js";
24
+ } from "./chunk-NLJLEF7Q.js";
25
25
  import "./chunk-EGWJYCNT.js";
26
26
  import {
27
27
  BITFAB_PROGRESS_PREFIX,
@@ -30,7 +30,7 @@ import {
30
30
  ReplayError,
31
31
  reportReplayProgress,
32
32
  serializeReplayResult
33
- } from "./chunk-VJOLQ7UY.js";
33
+ } from "./chunk-WEUG3N7A.js";
34
34
  import {
35
35
  BitfabError,
36
36
  DEFAULT_SERVICE_URL,
@@ -38,7 +38,7 @@ import {
38
38
  MixedTracingError,
39
39
  __version__,
40
40
  flushTraces
41
- } from "./chunk-XREH4NJT.js";
41
+ } from "./chunk-MC2NKYDY.js";
42
42
  import "./chunk-VCRFFCLY.js";
43
43
  export {
44
44
  AssertionCategoriesClient,
package/dist/node.cjs CHANGED
@@ -88,7 +88,7 @@ var __version__, __packageName__;
88
88
  var init_version_generated = __esm({
89
89
  "src/version.generated.ts"() {
90
90
  "use strict";
91
- __version__ = "0.54.10";
91
+ __version__ = "0.54.11";
92
92
  __packageName__ = "@bitfab/sdk";
93
93
  }
94
94
  });
@@ -114,20 +114,20 @@ var init_traceCompletion = __esm({
114
114
  this.closed = /* @__PURE__ */ new Map();
115
115
  }
116
116
  state(traceId) {
117
- let state = this.traces.get(traceId);
118
- if (state === void 0) {
119
- state = { spanIds: /* @__PURE__ */ new Set(), active: /* @__PURE__ */ new Set() };
120
- this.traces.set(traceId, state);
117
+ let state2 = this.traces.get(traceId);
118
+ if (state2 === void 0) {
119
+ state2 = { spanIds: /* @__PURE__ */ new Set(), active: /* @__PURE__ */ new Set() };
120
+ this.traces.set(traceId, state2);
121
121
  }
122
- return state;
122
+ return state2;
123
123
  }
124
124
  start(traceId, spanId) {
125
125
  if (this.closed.has(traceId)) {
126
126
  return;
127
127
  }
128
- const state = this.state(traceId);
129
- state.spanIds.add(spanId);
130
- state.active.add(spanId);
128
+ const state2 = this.state(traceId);
129
+ state2.spanIds.add(spanId);
130
+ state2.active.add(spanId);
131
131
  }
132
132
  open(traceId) {
133
133
  if (!this.closed.has(traceId)) {
@@ -143,24 +143,24 @@ var init_traceCompletion = __esm({
143
143
  }
144
144
  }
145
145
  end(traceId, spanId) {
146
- const state = this.traces.get(traceId);
147
- if (state !== void 0) {
148
- state.active.delete(spanId);
149
- this.drain(traceId, state);
146
+ const state2 = this.traces.get(traceId);
147
+ if (state2 !== void 0) {
148
+ state2.active.delete(spanId);
149
+ this.drain(traceId, state2);
150
150
  }
151
151
  }
152
152
  abort(traceId, spanId) {
153
- const state = this.traces.get(traceId);
154
- if (state === void 0) {
153
+ const state2 = this.traces.get(traceId);
154
+ if (state2 === void 0) {
155
155
  return;
156
156
  }
157
- state.active.delete(spanId);
158
- state.spanIds.delete(spanId);
159
- if (state.active.size === 0 && state.spanIds.size === 0 && state.complete === void 0) {
157
+ state2.active.delete(spanId);
158
+ state2.spanIds.delete(spanId);
159
+ if (state2.active.size === 0 && state2.spanIds.size === 0 && state2.complete === void 0) {
160
160
  this.traces.delete(traceId);
161
161
  return;
162
162
  }
163
- this.drain(traceId, state);
163
+ this.drain(traceId, state2);
164
164
  }
165
165
  close(traceId, complete, dropped = false) {
166
166
  const count = this.closed.get(traceId);
@@ -168,29 +168,29 @@ var init_traceCompletion = __esm({
168
168
  complete(count);
169
169
  return;
170
170
  }
171
- const state = this.traces.get(traceId);
172
- if (state === void 0) {
171
+ const state2 = this.traces.get(traceId);
172
+ if (state2 === void 0) {
173
173
  return;
174
174
  }
175
- state.complete = complete;
175
+ state2.complete = complete;
176
176
  if (dropped) {
177
- state.active.clear();
177
+ state2.active.clear();
178
178
  }
179
- this.drain(traceId, state);
179
+ this.drain(traceId, state2);
180
180
  }
181
- drain(traceId, state) {
182
- if (state.active.size !== 0 || state.complete === void 0) {
181
+ drain(traceId, state2) {
182
+ if (state2.active.size !== 0 || state2.complete === void 0) {
183
183
  return;
184
184
  }
185
185
  this.traces.delete(traceId);
186
- this.closed.set(traceId, state.spanIds.size);
186
+ this.closed.set(traceId, state2.spanIds.size);
187
187
  if (this.closed.size > 1024) {
188
188
  const oldest = this.closed.keys().next().value;
189
189
  if (oldest !== void 0) {
190
190
  this.closed.delete(oldest);
191
191
  }
192
192
  }
193
- state.complete(state.spanIds.size);
193
+ state2.complete(state2.spanIds.size);
194
194
  }
195
195
  };
196
196
  }
@@ -441,8 +441,8 @@ function text(value) {
441
441
  const trimmed = value?.trim();
442
442
  return trimmed ? trimmed : null;
443
443
  }
444
- function isEmptyGitState(state) {
445
- return Object.values(state).every((value) => value === null);
444
+ function isEmptyGitState(state2) {
445
+ return Object.values(state2).every((value) => value === null);
446
446
  }
447
447
  async function resolveGitState(cwd) {
448
448
  const git = await gitRunner({
@@ -463,14 +463,14 @@ async function resolveGitState(cwd) {
463
463
  if (!root) {
464
464
  return null;
465
465
  }
466
- const state = {
466
+ const state2 = {
467
467
  githubEmail: text(email),
468
468
  branch: text(branch),
469
469
  commitSha: sha(commitSha),
470
470
  baseSha: sha(baseSha),
471
471
  experimentSha: await resolveWorkingTreeSha(run, root.trim())
472
472
  };
473
- return isEmptyGitState(state) ? null : state;
473
+ return isEmptyGitState(state2) ? null : state2;
474
474
  }
475
475
  async function resolveWorkingTreeSha(run, root) {
476
476
  let indexPath;
@@ -6985,6 +6985,83 @@ var BitfabOpenAIAgentHandler = class {
6985
6985
  }
6986
6986
  };
6987
6987
 
6988
+ // src/openaiAgentsSpanContext.ts
6989
+ var OPENAI_AGENTS_CONTEXT_STORAGE = /* @__PURE__ */ Symbol.for(
6990
+ "openai.agents.core.asyncLocalStorage"
6991
+ );
6992
+ var MAX_OPENAI_AGENTS_SPAN_CHAIN = 64;
6993
+ var spanGlobal = globalThis;
6994
+ var state = spanGlobal.__bitfabOpenAiAgentsSpansV1 ?? {
6995
+ bitfabTraceIds: /* @__PURE__ */ new Map(),
6996
+ endedSpanIds: /* @__PURE__ */ new Map()
6997
+ };
6998
+ spanGlobal.__bitfabOpenAiAgentsSpansV1 = state;
6999
+ function mapOpenAiAgentsTrace(agentsTraceId, bitfabTraceId) {
7000
+ state.bitfabTraceIds.set(agentsTraceId, bitfabTraceId);
7001
+ }
7002
+ function unmapOpenAiAgentsTrace(agentsTraceId) {
7003
+ state.bitfabTraceIds.delete(agentsTraceId);
7004
+ state.endedSpanIds.delete(agentsTraceId);
7005
+ }
7006
+ function endOpenAiAgentsSpan(agentsTraceId, spanId) {
7007
+ if (!state.bitfabTraceIds.has(agentsTraceId)) {
7008
+ return;
7009
+ }
7010
+ const ended = state.endedSpanIds.get(agentsTraceId) ?? /* @__PURE__ */ new Set();
7011
+ ended.add(spanId);
7012
+ state.endedSpanIds.set(agentsTraceId, ended);
7013
+ }
7014
+ function isOpenAiAgentsContextStorage(value) {
7015
+ return value !== null && typeof value === "object" && typeof value.getStore === "function";
7016
+ }
7017
+ function currentOpenAiAgentsSpan() {
7018
+ const storage2 = spanGlobal[OPENAI_AGENTS_CONTEXT_STORAGE];
7019
+ if (!isOpenAiAgentsContextStorage(storage2)) {
7020
+ return void 0;
7021
+ }
7022
+ try {
7023
+ const store = storage2.getStore();
7024
+ return store?.active === true ? store.span ?? void 0 : void 0;
7025
+ } catch {
7026
+ return void 0;
7027
+ }
7028
+ }
7029
+ function currentOpenAiAgentsSpans() {
7030
+ const spans = [];
7031
+ let span = currentOpenAiAgentsSpan();
7032
+ while (span !== void 0 && spans.length < MAX_OPENAI_AGENTS_SPAN_CHAIN) {
7033
+ spans.push(span);
7034
+ span = span.previousSpan ?? void 0;
7035
+ }
7036
+ return spans;
7037
+ }
7038
+ function openAiAgentsSpanChain() {
7039
+ if (state.bitfabTraceIds.size === 0) {
7040
+ return [];
7041
+ }
7042
+ return currentOpenAiAgentsSpans().map((span) => span.spanId).filter((spanId) => typeof spanId === "string");
7043
+ }
7044
+ function openAiAgentsParentSpanId(bitfabTraceId, enclosingSpanIds) {
7045
+ if (state.bitfabTraceIds.size === 0) {
7046
+ return void 0;
7047
+ }
7048
+ for (const span of currentOpenAiAgentsSpans()) {
7049
+ if (typeof span.spanId !== "string" || typeof span.traceId !== "string") {
7050
+ return void 0;
7051
+ }
7052
+ if (state.bitfabTraceIds.get(span.traceId) !== bitfabTraceId) {
7053
+ return void 0;
7054
+ }
7055
+ if (enclosingSpanIds?.includes(span.spanId)) {
7056
+ return void 0;
7057
+ }
7058
+ if (!state.endedSpanIds.get(span.traceId)?.has(span.spanId)) {
7059
+ return span.spanId;
7060
+ }
7061
+ }
7062
+ return void 0;
7063
+ }
7064
+
6988
7065
  // src/client.ts
6989
7066
  init_policyRefresh();
6990
7067
  init_randomUuid();
@@ -7708,6 +7785,9 @@ var BitfabOpenAITracingProcessor = class {
7708
7785
  const activeContext = this.getActiveSpanContext?.();
7709
7786
  if (activeContext) {
7710
7787
  this.activeSpanMappings[trace.traceId] = activeContext;
7788
+ if (this.shouldRecord()) {
7789
+ mapOpenAiAgentsTrace(trace.traceId, activeContext.traceId);
7790
+ }
7711
7791
  }
7712
7792
  const canonicalTraceId = activeContext?.traceId ?? this.getCanonicalTraceId(trace.traceId);
7713
7793
  this.canonicalTraceIds[trace.traceId] = canonicalTraceId;
@@ -7745,6 +7825,7 @@ var BitfabOpenAITracingProcessor = class {
7745
7825
  }
7746
7826
  this.endedTraces.delete(traceId);
7747
7827
  delete this.activeSpanMappings[traceId];
7828
+ unmapOpenAiAgentsTrace(traceId);
7748
7829
  delete this.canonicalTraceIds[traceId];
7749
7830
  delete this.activeTraces[traceId];
7750
7831
  }
@@ -7769,6 +7850,9 @@ var BitfabOpenAITracingProcessor = class {
7769
7850
  async onSpanEnd(span) {
7770
7851
  const originalTraceId = span.traceId ?? "";
7771
7852
  const traceId = this.activeSpanMappings[originalTraceId]?.traceId ?? originalTraceId;
7853
+ if (span.spanId !== void 0) {
7854
+ endOpenAiAgentsSpan(originalTraceId, span.spanId);
7855
+ }
7772
7856
  try {
7773
7857
  this.sendSpan(span);
7774
7858
  } finally {
@@ -7789,6 +7873,9 @@ var BitfabOpenAITracingProcessor = class {
7789
7873
  * Called when the trace processor is shutting down.
7790
7874
  */
7791
7875
  async shutdown(timeout) {
7876
+ for (const traceId of Object.keys(this.activeSpanMappings)) {
7877
+ unmapOpenAiAgentsTrace(traceId);
7878
+ }
7792
7879
  this.activeTraces = {};
7793
7880
  this.activeSpanMappings = {};
7794
7881
  this.canonicalTraceIds = {};
@@ -8946,16 +9033,16 @@ var Bitfab = class {
8946
9033
  this.simulationPlan.send(payload, rootTraceFunctionKeyOf(payload), submit);
8947
9034
  }
8948
9035
  refreshAutoTraceCapturePolicy(traceFunctionKey) {
8949
- const state = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
8950
- this.autoTracePolicyRefreshes.set(traceFunctionKey, state);
8951
- state.run(
9036
+ const state2 = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
9037
+ this.autoTracePolicyRefreshes.set(traceFunctionKey, state2);
9038
+ state2.run(
8952
9039
  () => this.httpClient.getAutoTracePolicy(
8953
9040
  traceFunctionKey,
8954
9041
  AUTO_TRACE_PROTOCOL
8955
9042
  ),
8956
9043
  (policy) => {
8957
9044
  if (policy.protocol !== AUTO_TRACE_PROTOCOL) {
8958
- state.hold(AUTO_TRACE_POLICY_RETRY_MS);
9045
+ state2.hold(AUTO_TRACE_POLICY_RETRY_MS);
8959
9046
  return;
8960
9047
  }
8961
9048
  const functionIds = Array.isArray(policy.functionIds) ? policy.functionIds.filter(
@@ -8966,10 +9053,10 @@ var Bitfab = class {
8966
9053
  traceFunctionKey,
8967
9054
  policy.revision === null ? void 0 : functionIds
8968
9055
  );
8969
- state.hold(AUTO_TRACE_POLICY_REFRESH_MS);
9056
+ state2.hold(AUTO_TRACE_POLICY_REFRESH_MS);
8970
9057
  },
8971
9058
  () => {
8972
- state.hold(AUTO_TRACE_POLICY_RETRY_MS);
9059
+ state2.hold(AUTO_TRACE_POLICY_RETRY_MS);
8973
9060
  }
8974
9061
  );
8975
9062
  }
@@ -9580,7 +9667,11 @@ var Bitfab = class {
9580
9667
  const seedCtxForTraceId = parentContext ? null : getSeedContext();
9581
9668
  const traceId = parentContext?.traceId ?? replayCtxForTraceId?.traceId ?? seedCtxForTraceId?.traceId ?? randomUuid();
9582
9669
  const spanId = randomUuid();
9583
- const parentSpanId = parentContext?.spanId ?? null;
9670
+ const openAiAgentsParent = parentContext !== void 0 && options.instrumentation === "trace" ? openAiAgentsParentSpanId(
9671
+ parentContext.traceId,
9672
+ parentContext.openAiAgentsSpanIds
9673
+ ) : void 0;
9674
+ const parentSpanId = openAiAgentsParent ?? parentContext?.spanId ?? null;
9584
9675
  const isRootSpan = parentSpanId === null;
9585
9676
  if (options.enclosingTrace !== void 0) {
9586
9677
  options.enclosingTrace.link.traceId = traceId;
@@ -9594,11 +9685,13 @@ var Bitfab = class {
9594
9685
  parentContext?.surface,
9595
9686
  traceFunctionKey
9596
9687
  );
9688
+ const openAiAgentsSpanIds = openAiAgentsSpanChain();
9597
9689
  const newContext = {
9598
9690
  traceId,
9599
9691
  spanId,
9600
9692
  contexts: [],
9601
- ...surface !== void 0 && { surface }
9693
+ ...surface !== void 0 && { surface },
9694
+ ...openAiAgentsSpanIds.length > 0 && { openAiAgentsSpanIds }
9602
9695
  };
9603
9696
  newStack = [...currentStack, newContext];
9604
9697
  const inputs = args;