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
  flushTraces,
7
7
  parseRetryAfterMs,
8
8
  serializePayloadBody
9
- } from "./chunk-B2O33DBH.js";
9
+ } from "./chunk-FMUJONTQ.js";
10
10
  import "./chunk-VCRFFCLY.js";
11
11
  export {
12
12
  BitfabError,
@@ -17,4 +17,4 @@ export {
17
17
  parseRetryAfterMs,
18
18
  serializePayloadBody
19
19
  };
20
- //# sourceMappingURL=http-AVWWAMFV.js.map
20
+ //# sourceMappingURL=http-AXPT27DO.js.map
@@ -6,7 +6,7 @@ import {
6
6
  flushTraces,
7
7
  parseRetryAfterMs,
8
8
  serializePayloadBody
9
- } from "./chunk-XLWQOUOP.js";
9
+ } from "./chunk-RUBVHBIK.js";
10
10
  export {
11
11
  BitfabError,
12
12
  HttpClient,
@@ -16,4 +16,4 @@ export {
16
16
  parseRetryAfterMs,
17
17
  serializePayloadBody
18
18
  };
19
- //# sourceMappingURL=http-DH3O3L5X.js.map
19
+ //# sourceMappingURL=http-JQN2LHHF.js.map
package/dist/index.cjs CHANGED
@@ -51,7 +51,7 @@ var __version__, __packageName__;
51
51
  var init_version_generated = __esm({
52
52
  "src/version.generated.ts"() {
53
53
  "use strict";
54
- __version__ = "0.54.10";
54
+ __version__ = "0.54.11";
55
55
  __packageName__ = "bitfab";
56
56
  }
57
57
  });
@@ -77,20 +77,20 @@ var init_traceCompletion = __esm({
77
77
  this.closed = /* @__PURE__ */ new Map();
78
78
  }
79
79
  state(traceId) {
80
- let state = this.traces.get(traceId);
81
- if (state === void 0) {
82
- state = { spanIds: /* @__PURE__ */ new Set(), active: /* @__PURE__ */ new Set() };
83
- this.traces.set(traceId, state);
80
+ let state2 = this.traces.get(traceId);
81
+ if (state2 === void 0) {
82
+ state2 = { spanIds: /* @__PURE__ */ new Set(), active: /* @__PURE__ */ new Set() };
83
+ this.traces.set(traceId, state2);
84
84
  }
85
- return state;
85
+ return state2;
86
86
  }
87
87
  start(traceId, spanId) {
88
88
  if (this.closed.has(traceId)) {
89
89
  return;
90
90
  }
91
- const state = this.state(traceId);
92
- state.spanIds.add(spanId);
93
- state.active.add(spanId);
91
+ const state2 = this.state(traceId);
92
+ state2.spanIds.add(spanId);
93
+ state2.active.add(spanId);
94
94
  }
95
95
  open(traceId) {
96
96
  if (!this.closed.has(traceId)) {
@@ -106,24 +106,24 @@ var init_traceCompletion = __esm({
106
106
  }
107
107
  }
108
108
  end(traceId, spanId) {
109
- const state = this.traces.get(traceId);
110
- if (state !== void 0) {
111
- state.active.delete(spanId);
112
- this.drain(traceId, state);
109
+ const state2 = this.traces.get(traceId);
110
+ if (state2 !== void 0) {
111
+ state2.active.delete(spanId);
112
+ this.drain(traceId, state2);
113
113
  }
114
114
  }
115
115
  abort(traceId, spanId) {
116
- const state = this.traces.get(traceId);
117
- if (state === void 0) {
116
+ const state2 = this.traces.get(traceId);
117
+ if (state2 === void 0) {
118
118
  return;
119
119
  }
120
- state.active.delete(spanId);
121
- state.spanIds.delete(spanId);
122
- if (state.active.size === 0 && state.spanIds.size === 0 && state.complete === void 0) {
120
+ state2.active.delete(spanId);
121
+ state2.spanIds.delete(spanId);
122
+ if (state2.active.size === 0 && state2.spanIds.size === 0 && state2.complete === void 0) {
123
123
  this.traces.delete(traceId);
124
124
  return;
125
125
  }
126
- this.drain(traceId, state);
126
+ this.drain(traceId, state2);
127
127
  }
128
128
  close(traceId, complete, dropped = false) {
129
129
  const count = this.closed.get(traceId);
@@ -131,29 +131,29 @@ var init_traceCompletion = __esm({
131
131
  complete(count);
132
132
  return;
133
133
  }
134
- const state = this.traces.get(traceId);
135
- if (state === void 0) {
134
+ const state2 = this.traces.get(traceId);
135
+ if (state2 === void 0) {
136
136
  return;
137
137
  }
138
- state.complete = complete;
138
+ state2.complete = complete;
139
139
  if (dropped) {
140
- state.active.clear();
140
+ state2.active.clear();
141
141
  }
142
- this.drain(traceId, state);
142
+ this.drain(traceId, state2);
143
143
  }
144
- drain(traceId, state) {
145
- if (state.active.size !== 0 || state.complete === void 0) {
144
+ drain(traceId, state2) {
145
+ if (state2.active.size !== 0 || state2.complete === void 0) {
146
146
  return;
147
147
  }
148
148
  this.traces.delete(traceId);
149
- this.closed.set(traceId, state.spanIds.size);
149
+ this.closed.set(traceId, state2.spanIds.size);
150
150
  if (this.closed.size > 1024) {
151
151
  const oldest = this.closed.keys().next().value;
152
152
  if (oldest !== void 0) {
153
153
  this.closed.delete(oldest);
154
154
  }
155
155
  }
156
- state.complete(state.spanIds.size);
156
+ state2.complete(state2.spanIds.size);
157
157
  }
158
158
  };
159
159
  }
@@ -404,8 +404,8 @@ function text(value) {
404
404
  const trimmed = value?.trim();
405
405
  return trimmed ? trimmed : null;
406
406
  }
407
- function isEmptyGitState(state) {
408
- return Object.values(state).every((value) => value === null);
407
+ function isEmptyGitState(state2) {
408
+ return Object.values(state2).every((value) => value === null);
409
409
  }
410
410
  async function resolveGitState(cwd) {
411
411
  const git = await gitRunner({
@@ -426,14 +426,14 @@ async function resolveGitState(cwd) {
426
426
  if (!root) {
427
427
  return null;
428
428
  }
429
- const state = {
429
+ const state2 = {
430
430
  githubEmail: text(email),
431
431
  branch: text(branch),
432
432
  commitSha: sha(commitSha),
433
433
  baseSha: sha(baseSha),
434
434
  experimentSha: await resolveWorkingTreeSha(run, root.trim())
435
435
  };
436
- return isEmptyGitState(state) ? null : state;
436
+ return isEmptyGitState(state2) ? null : state2;
437
437
  }
438
438
  async function resolveWorkingTreeSha(run, root) {
439
439
  let indexPath;
@@ -6980,6 +6980,83 @@ var BitfabOpenAIAgentHandler = class {
6980
6980
  }
6981
6981
  };
6982
6982
 
6983
+ // src/openaiAgentsSpanContext.ts
6984
+ var OPENAI_AGENTS_CONTEXT_STORAGE = /* @__PURE__ */ Symbol.for(
6985
+ "openai.agents.core.asyncLocalStorage"
6986
+ );
6987
+ var MAX_OPENAI_AGENTS_SPAN_CHAIN = 64;
6988
+ var spanGlobal = globalThis;
6989
+ var state = spanGlobal.__bitfabOpenAiAgentsSpansV1 ?? {
6990
+ bitfabTraceIds: /* @__PURE__ */ new Map(),
6991
+ endedSpanIds: /* @__PURE__ */ new Map()
6992
+ };
6993
+ spanGlobal.__bitfabOpenAiAgentsSpansV1 = state;
6994
+ function mapOpenAiAgentsTrace(agentsTraceId, bitfabTraceId) {
6995
+ state.bitfabTraceIds.set(agentsTraceId, bitfabTraceId);
6996
+ }
6997
+ function unmapOpenAiAgentsTrace(agentsTraceId) {
6998
+ state.bitfabTraceIds.delete(agentsTraceId);
6999
+ state.endedSpanIds.delete(agentsTraceId);
7000
+ }
7001
+ function endOpenAiAgentsSpan(agentsTraceId, spanId) {
7002
+ if (!state.bitfabTraceIds.has(agentsTraceId)) {
7003
+ return;
7004
+ }
7005
+ const ended = state.endedSpanIds.get(agentsTraceId) ?? /* @__PURE__ */ new Set();
7006
+ ended.add(spanId);
7007
+ state.endedSpanIds.set(agentsTraceId, ended);
7008
+ }
7009
+ function isOpenAiAgentsContextStorage(value) {
7010
+ return value !== null && typeof value === "object" && typeof value.getStore === "function";
7011
+ }
7012
+ function currentOpenAiAgentsSpan() {
7013
+ const storage2 = spanGlobal[OPENAI_AGENTS_CONTEXT_STORAGE];
7014
+ if (!isOpenAiAgentsContextStorage(storage2)) {
7015
+ return void 0;
7016
+ }
7017
+ try {
7018
+ const store = storage2.getStore();
7019
+ return store?.active === true ? store.span ?? void 0 : void 0;
7020
+ } catch {
7021
+ return void 0;
7022
+ }
7023
+ }
7024
+ function currentOpenAiAgentsSpans() {
7025
+ const spans = [];
7026
+ let span = currentOpenAiAgentsSpan();
7027
+ while (span !== void 0 && spans.length < MAX_OPENAI_AGENTS_SPAN_CHAIN) {
7028
+ spans.push(span);
7029
+ span = span.previousSpan ?? void 0;
7030
+ }
7031
+ return spans;
7032
+ }
7033
+ function openAiAgentsSpanChain() {
7034
+ if (state.bitfabTraceIds.size === 0) {
7035
+ return [];
7036
+ }
7037
+ return currentOpenAiAgentsSpans().map((span) => span.spanId).filter((spanId) => typeof spanId === "string");
7038
+ }
7039
+ function openAiAgentsParentSpanId(bitfabTraceId, enclosingSpanIds) {
7040
+ if (state.bitfabTraceIds.size === 0) {
7041
+ return void 0;
7042
+ }
7043
+ for (const span of currentOpenAiAgentsSpans()) {
7044
+ if (typeof span.spanId !== "string" || typeof span.traceId !== "string") {
7045
+ return void 0;
7046
+ }
7047
+ if (state.bitfabTraceIds.get(span.traceId) !== bitfabTraceId) {
7048
+ return void 0;
7049
+ }
7050
+ if (enclosingSpanIds?.includes(span.spanId)) {
7051
+ return void 0;
7052
+ }
7053
+ if (!state.endedSpanIds.get(span.traceId)?.has(span.spanId)) {
7054
+ return span.spanId;
7055
+ }
7056
+ }
7057
+ return void 0;
7058
+ }
7059
+
6983
7060
  // src/client.ts
6984
7061
  init_policyRefresh();
6985
7062
  init_randomUuid();
@@ -7703,6 +7780,9 @@ var BitfabOpenAITracingProcessor = class {
7703
7780
  const activeContext = this.getActiveSpanContext?.();
7704
7781
  if (activeContext) {
7705
7782
  this.activeSpanMappings[trace.traceId] = activeContext;
7783
+ if (this.shouldRecord()) {
7784
+ mapOpenAiAgentsTrace(trace.traceId, activeContext.traceId);
7785
+ }
7706
7786
  }
7707
7787
  const canonicalTraceId = activeContext?.traceId ?? this.getCanonicalTraceId(trace.traceId);
7708
7788
  this.canonicalTraceIds[trace.traceId] = canonicalTraceId;
@@ -7740,6 +7820,7 @@ var BitfabOpenAITracingProcessor = class {
7740
7820
  }
7741
7821
  this.endedTraces.delete(traceId);
7742
7822
  delete this.activeSpanMappings[traceId];
7823
+ unmapOpenAiAgentsTrace(traceId);
7743
7824
  delete this.canonicalTraceIds[traceId];
7744
7825
  delete this.activeTraces[traceId];
7745
7826
  }
@@ -7764,6 +7845,9 @@ var BitfabOpenAITracingProcessor = class {
7764
7845
  async onSpanEnd(span) {
7765
7846
  const originalTraceId = span.traceId ?? "";
7766
7847
  const traceId = this.activeSpanMappings[originalTraceId]?.traceId ?? originalTraceId;
7848
+ if (span.spanId !== void 0) {
7849
+ endOpenAiAgentsSpan(originalTraceId, span.spanId);
7850
+ }
7767
7851
  try {
7768
7852
  this.sendSpan(span);
7769
7853
  } finally {
@@ -7784,6 +7868,9 @@ var BitfabOpenAITracingProcessor = class {
7784
7868
  * Called when the trace processor is shutting down.
7785
7869
  */
7786
7870
  async shutdown(timeout) {
7871
+ for (const traceId of Object.keys(this.activeSpanMappings)) {
7872
+ unmapOpenAiAgentsTrace(traceId);
7873
+ }
7787
7874
  this.activeTraces = {};
7788
7875
  this.activeSpanMappings = {};
7789
7876
  this.canonicalTraceIds = {};
@@ -8941,16 +9028,16 @@ var Bitfab = class {
8941
9028
  this.simulationPlan.send(payload, rootTraceFunctionKeyOf(payload), submit);
8942
9029
  }
8943
9030
  refreshAutoTraceCapturePolicy(traceFunctionKey) {
8944
- const state = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
8945
- this.autoTracePolicyRefreshes.set(traceFunctionKey, state);
8946
- state.run(
9031
+ const state2 = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
9032
+ this.autoTracePolicyRefreshes.set(traceFunctionKey, state2);
9033
+ state2.run(
8947
9034
  () => this.httpClient.getAutoTracePolicy(
8948
9035
  traceFunctionKey,
8949
9036
  AUTO_TRACE_PROTOCOL
8950
9037
  ),
8951
9038
  (policy) => {
8952
9039
  if (policy.protocol !== AUTO_TRACE_PROTOCOL) {
8953
- state.hold(AUTO_TRACE_POLICY_RETRY_MS);
9040
+ state2.hold(AUTO_TRACE_POLICY_RETRY_MS);
8954
9041
  return;
8955
9042
  }
8956
9043
  const functionIds = Array.isArray(policy.functionIds) ? policy.functionIds.filter(
@@ -8961,10 +9048,10 @@ var Bitfab = class {
8961
9048
  traceFunctionKey,
8962
9049
  policy.revision === null ? void 0 : functionIds
8963
9050
  );
8964
- state.hold(AUTO_TRACE_POLICY_REFRESH_MS);
9051
+ state2.hold(AUTO_TRACE_POLICY_REFRESH_MS);
8965
9052
  },
8966
9053
  () => {
8967
- state.hold(AUTO_TRACE_POLICY_RETRY_MS);
9054
+ state2.hold(AUTO_TRACE_POLICY_RETRY_MS);
8968
9055
  }
8969
9056
  );
8970
9057
  }
@@ -9575,7 +9662,11 @@ var Bitfab = class {
9575
9662
  const seedCtxForTraceId = parentContext ? null : getSeedContext();
9576
9663
  const traceId = parentContext?.traceId ?? replayCtxForTraceId?.traceId ?? seedCtxForTraceId?.traceId ?? randomUuid();
9577
9664
  const spanId = randomUuid();
9578
- const parentSpanId = parentContext?.spanId ?? null;
9665
+ const openAiAgentsParent = parentContext !== void 0 && options.instrumentation === "trace" ? openAiAgentsParentSpanId(
9666
+ parentContext.traceId,
9667
+ parentContext.openAiAgentsSpanIds
9668
+ ) : void 0;
9669
+ const parentSpanId = openAiAgentsParent ?? parentContext?.spanId ?? null;
9579
9670
  const isRootSpan = parentSpanId === null;
9580
9671
  if (options.enclosingTrace !== void 0) {
9581
9672
  options.enclosingTrace.link.traceId = traceId;
@@ -9589,11 +9680,13 @@ var Bitfab = class {
9589
9680
  parentContext?.surface,
9590
9681
  traceFunctionKey
9591
9682
  );
9683
+ const openAiAgentsSpanIds = openAiAgentsSpanChain();
9592
9684
  const newContext = {
9593
9685
  traceId,
9594
9686
  spanId,
9595
9687
  contexts: [],
9596
- ...surface !== void 0 && { surface }
9688
+ ...surface !== void 0 && { surface },
9689
+ ...openAiAgentsSpanIds.length > 0 && { openAiAgentsSpanIds }
9597
9690
  };
9598
9691
  newStack = [...currentStack, newContext];
9599
9692
  const inputs = args;