@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.
@@ -6,7 +6,7 @@ import {
6
6
  replayContextReady,
7
7
  runWithReplayContext,
8
8
  warnOnce
9
- } from "./chunk-XREH4NJT.js";
9
+ } from "./chunk-MC2NKYDY.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-VJOLQ7UY.js.map
1260
+ //# sourceMappingURL=chunk-WEUG3N7A.js.map
@@ -6,7 +6,7 @@ import {
6
6
  flushTraces,
7
7
  parseRetryAfterMs,
8
8
  serializePayloadBody
9
- } from "./chunk-6KVKJQNH.js";
9
+ } from "./chunk-GQNLAY72.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-NVPWO4TN.js.map
19
+ //# sourceMappingURL=http-3NVEDIBT.js.map
@@ -6,7 +6,7 @@ import {
6
6
  flushTraces,
7
7
  parseRetryAfterMs,
8
8
  serializePayloadBody
9
- } from "./chunk-XREH4NJT.js";
9
+ } from "./chunk-MC2NKYDY.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-NPOFVS25.js.map
20
+ //# sourceMappingURL=http-L357FQLN.js.map
package/dist/index.cjs CHANGED
@@ -42,7 +42,7 @@ var __version__, __packageName__;
42
42
  var init_version_generated = __esm({
43
43
  "src/version.generated.ts"() {
44
44
  "use strict";
45
- __version__ = "0.54.10";
45
+ __version__ = "0.54.11";
46
46
  __packageName__ = "@bitfab/sdk";
47
47
  }
48
48
  });
@@ -68,20 +68,20 @@ var init_traceCompletion = __esm({
68
68
  this.closed = /* @__PURE__ */ new Map();
69
69
  }
70
70
  state(traceId) {
71
- let state = this.traces.get(traceId);
72
- if (state === void 0) {
73
- state = { spanIds: /* @__PURE__ */ new Set(), active: /* @__PURE__ */ new Set() };
74
- this.traces.set(traceId, state);
71
+ let state2 = this.traces.get(traceId);
72
+ if (state2 === void 0) {
73
+ state2 = { spanIds: /* @__PURE__ */ new Set(), active: /* @__PURE__ */ new Set() };
74
+ this.traces.set(traceId, state2);
75
75
  }
76
- return state;
76
+ return state2;
77
77
  }
78
78
  start(traceId, spanId) {
79
79
  if (this.closed.has(traceId)) {
80
80
  return;
81
81
  }
82
- const state = this.state(traceId);
83
- state.spanIds.add(spanId);
84
- state.active.add(spanId);
82
+ const state2 = this.state(traceId);
83
+ state2.spanIds.add(spanId);
84
+ state2.active.add(spanId);
85
85
  }
86
86
  open(traceId) {
87
87
  if (!this.closed.has(traceId)) {
@@ -97,24 +97,24 @@ var init_traceCompletion = __esm({
97
97
  }
98
98
  }
99
99
  end(traceId, spanId) {
100
- const state = this.traces.get(traceId);
101
- if (state !== void 0) {
102
- state.active.delete(spanId);
103
- this.drain(traceId, state);
100
+ const state2 = this.traces.get(traceId);
101
+ if (state2 !== void 0) {
102
+ state2.active.delete(spanId);
103
+ this.drain(traceId, state2);
104
104
  }
105
105
  }
106
106
  abort(traceId, spanId) {
107
- const state = this.traces.get(traceId);
108
- if (state === void 0) {
107
+ const state2 = this.traces.get(traceId);
108
+ if (state2 === void 0) {
109
109
  return;
110
110
  }
111
- state.active.delete(spanId);
112
- state.spanIds.delete(spanId);
113
- if (state.active.size === 0 && state.spanIds.size === 0 && state.complete === void 0) {
111
+ state2.active.delete(spanId);
112
+ state2.spanIds.delete(spanId);
113
+ if (state2.active.size === 0 && state2.spanIds.size === 0 && state2.complete === void 0) {
114
114
  this.traces.delete(traceId);
115
115
  return;
116
116
  }
117
- this.drain(traceId, state);
117
+ this.drain(traceId, state2);
118
118
  }
119
119
  close(traceId, complete, dropped = false) {
120
120
  const count = this.closed.get(traceId);
@@ -122,29 +122,29 @@ var init_traceCompletion = __esm({
122
122
  complete(count);
123
123
  return;
124
124
  }
125
- const state = this.traces.get(traceId);
126
- if (state === void 0) {
125
+ const state2 = this.traces.get(traceId);
126
+ if (state2 === void 0) {
127
127
  return;
128
128
  }
129
- state.complete = complete;
129
+ state2.complete = complete;
130
130
  if (dropped) {
131
- state.active.clear();
131
+ state2.active.clear();
132
132
  }
133
- this.drain(traceId, state);
133
+ this.drain(traceId, state2);
134
134
  }
135
- drain(traceId, state) {
136
- if (state.active.size !== 0 || state.complete === void 0) {
135
+ drain(traceId, state2) {
136
+ if (state2.active.size !== 0 || state2.complete === void 0) {
137
137
  return;
138
138
  }
139
139
  this.traces.delete(traceId);
140
- this.closed.set(traceId, state.spanIds.size);
140
+ this.closed.set(traceId, state2.spanIds.size);
141
141
  if (this.closed.size > 1024) {
142
142
  const oldest = this.closed.keys().next().value;
143
143
  if (oldest !== void 0) {
144
144
  this.closed.delete(oldest);
145
145
  }
146
146
  }
147
- state.complete(state.spanIds.size);
147
+ state2.complete(state2.spanIds.size);
148
148
  }
149
149
  };
150
150
  }
@@ -395,8 +395,8 @@ function text(value) {
395
395
  const trimmed = value?.trim();
396
396
  return trimmed ? trimmed : null;
397
397
  }
398
- function isEmptyGitState(state) {
399
- return Object.values(state).every((value) => value === null);
398
+ function isEmptyGitState(state2) {
399
+ return Object.values(state2).every((value) => value === null);
400
400
  }
401
401
  async function resolveGitState(cwd) {
402
402
  const git = await gitRunner({
@@ -417,14 +417,14 @@ async function resolveGitState(cwd) {
417
417
  if (!root) {
418
418
  return null;
419
419
  }
420
- const state = {
420
+ const state2 = {
421
421
  githubEmail: text(email),
422
422
  branch: text(branch),
423
423
  commitSha: sha(commitSha),
424
424
  baseSha: sha(baseSha),
425
425
  experimentSha: await resolveWorkingTreeSha(run, root.trim())
426
426
  };
427
- return isEmptyGitState(state) ? null : state;
427
+ return isEmptyGitState(state2) ? null : state2;
428
428
  }
429
429
  async function resolveWorkingTreeSha(run, root) {
430
430
  let indexPath;
@@ -6971,6 +6971,83 @@ var BitfabOpenAIAgentHandler = class {
6971
6971
  }
6972
6972
  };
6973
6973
 
6974
+ // src/openaiAgentsSpanContext.ts
6975
+ var OPENAI_AGENTS_CONTEXT_STORAGE = /* @__PURE__ */ Symbol.for(
6976
+ "openai.agents.core.asyncLocalStorage"
6977
+ );
6978
+ var MAX_OPENAI_AGENTS_SPAN_CHAIN = 64;
6979
+ var spanGlobal = globalThis;
6980
+ var state = spanGlobal.__bitfabOpenAiAgentsSpansV1 ?? {
6981
+ bitfabTraceIds: /* @__PURE__ */ new Map(),
6982
+ endedSpanIds: /* @__PURE__ */ new Map()
6983
+ };
6984
+ spanGlobal.__bitfabOpenAiAgentsSpansV1 = state;
6985
+ function mapOpenAiAgentsTrace(agentsTraceId, bitfabTraceId) {
6986
+ state.bitfabTraceIds.set(agentsTraceId, bitfabTraceId);
6987
+ }
6988
+ function unmapOpenAiAgentsTrace(agentsTraceId) {
6989
+ state.bitfabTraceIds.delete(agentsTraceId);
6990
+ state.endedSpanIds.delete(agentsTraceId);
6991
+ }
6992
+ function endOpenAiAgentsSpan(agentsTraceId, spanId) {
6993
+ if (!state.bitfabTraceIds.has(agentsTraceId)) {
6994
+ return;
6995
+ }
6996
+ const ended = state.endedSpanIds.get(agentsTraceId) ?? /* @__PURE__ */ new Set();
6997
+ ended.add(spanId);
6998
+ state.endedSpanIds.set(agentsTraceId, ended);
6999
+ }
7000
+ function isOpenAiAgentsContextStorage(value) {
7001
+ return value !== null && typeof value === "object" && typeof value.getStore === "function";
7002
+ }
7003
+ function currentOpenAiAgentsSpan() {
7004
+ const storage2 = spanGlobal[OPENAI_AGENTS_CONTEXT_STORAGE];
7005
+ if (!isOpenAiAgentsContextStorage(storage2)) {
7006
+ return void 0;
7007
+ }
7008
+ try {
7009
+ const store = storage2.getStore();
7010
+ return store?.active === true ? store.span ?? void 0 : void 0;
7011
+ } catch {
7012
+ return void 0;
7013
+ }
7014
+ }
7015
+ function currentOpenAiAgentsSpans() {
7016
+ const spans = [];
7017
+ let span = currentOpenAiAgentsSpan();
7018
+ while (span !== void 0 && spans.length < MAX_OPENAI_AGENTS_SPAN_CHAIN) {
7019
+ spans.push(span);
7020
+ span = span.previousSpan ?? void 0;
7021
+ }
7022
+ return spans;
7023
+ }
7024
+ function openAiAgentsSpanChain() {
7025
+ if (state.bitfabTraceIds.size === 0) {
7026
+ return [];
7027
+ }
7028
+ return currentOpenAiAgentsSpans().map((span) => span.spanId).filter((spanId) => typeof spanId === "string");
7029
+ }
7030
+ function openAiAgentsParentSpanId(bitfabTraceId, enclosingSpanIds) {
7031
+ if (state.bitfabTraceIds.size === 0) {
7032
+ return void 0;
7033
+ }
7034
+ for (const span of currentOpenAiAgentsSpans()) {
7035
+ if (typeof span.spanId !== "string" || typeof span.traceId !== "string") {
7036
+ return void 0;
7037
+ }
7038
+ if (state.bitfabTraceIds.get(span.traceId) !== bitfabTraceId) {
7039
+ return void 0;
7040
+ }
7041
+ if (enclosingSpanIds?.includes(span.spanId)) {
7042
+ return void 0;
7043
+ }
7044
+ if (!state.endedSpanIds.get(span.traceId)?.has(span.spanId)) {
7045
+ return span.spanId;
7046
+ }
7047
+ }
7048
+ return void 0;
7049
+ }
7050
+
6974
7051
  // src/client.ts
6975
7052
  init_policyRefresh();
6976
7053
  init_randomUuid();
@@ -7694,6 +7771,9 @@ var BitfabOpenAITracingProcessor = class {
7694
7771
  const activeContext = this.getActiveSpanContext?.();
7695
7772
  if (activeContext) {
7696
7773
  this.activeSpanMappings[trace.traceId] = activeContext;
7774
+ if (this.shouldRecord()) {
7775
+ mapOpenAiAgentsTrace(trace.traceId, activeContext.traceId);
7776
+ }
7697
7777
  }
7698
7778
  const canonicalTraceId = activeContext?.traceId ?? this.getCanonicalTraceId(trace.traceId);
7699
7779
  this.canonicalTraceIds[trace.traceId] = canonicalTraceId;
@@ -7731,6 +7811,7 @@ var BitfabOpenAITracingProcessor = class {
7731
7811
  }
7732
7812
  this.endedTraces.delete(traceId);
7733
7813
  delete this.activeSpanMappings[traceId];
7814
+ unmapOpenAiAgentsTrace(traceId);
7734
7815
  delete this.canonicalTraceIds[traceId];
7735
7816
  delete this.activeTraces[traceId];
7736
7817
  }
@@ -7755,6 +7836,9 @@ var BitfabOpenAITracingProcessor = class {
7755
7836
  async onSpanEnd(span) {
7756
7837
  const originalTraceId = span.traceId ?? "";
7757
7838
  const traceId = this.activeSpanMappings[originalTraceId]?.traceId ?? originalTraceId;
7839
+ if (span.spanId !== void 0) {
7840
+ endOpenAiAgentsSpan(originalTraceId, span.spanId);
7841
+ }
7758
7842
  try {
7759
7843
  this.sendSpan(span);
7760
7844
  } finally {
@@ -7775,6 +7859,9 @@ var BitfabOpenAITracingProcessor = class {
7775
7859
  * Called when the trace processor is shutting down.
7776
7860
  */
7777
7861
  async shutdown(timeout) {
7862
+ for (const traceId of Object.keys(this.activeSpanMappings)) {
7863
+ unmapOpenAiAgentsTrace(traceId);
7864
+ }
7778
7865
  this.activeTraces = {};
7779
7866
  this.activeSpanMappings = {};
7780
7867
  this.canonicalTraceIds = {};
@@ -8932,16 +9019,16 @@ var Bitfab = class {
8932
9019
  this.simulationPlan.send(payload, rootTraceFunctionKeyOf(payload), submit);
8933
9020
  }
8934
9021
  refreshAutoTraceCapturePolicy(traceFunctionKey) {
8935
- const state = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
8936
- this.autoTracePolicyRefreshes.set(traceFunctionKey, state);
8937
- state.run(
9022
+ const state2 = this.autoTracePolicyRefreshes.get(traceFunctionKey) ?? new PolicyRefresh();
9023
+ this.autoTracePolicyRefreshes.set(traceFunctionKey, state2);
9024
+ state2.run(
8938
9025
  () => this.httpClient.getAutoTracePolicy(
8939
9026
  traceFunctionKey,
8940
9027
  AUTO_TRACE_PROTOCOL
8941
9028
  ),
8942
9029
  (policy) => {
8943
9030
  if (policy.protocol !== AUTO_TRACE_PROTOCOL) {
8944
- state.hold(AUTO_TRACE_POLICY_RETRY_MS);
9031
+ state2.hold(AUTO_TRACE_POLICY_RETRY_MS);
8945
9032
  return;
8946
9033
  }
8947
9034
  const functionIds = Array.isArray(policy.functionIds) ? policy.functionIds.filter(
@@ -8952,10 +9039,10 @@ var Bitfab = class {
8952
9039
  traceFunctionKey,
8953
9040
  policy.revision === null ? void 0 : functionIds
8954
9041
  );
8955
- state.hold(AUTO_TRACE_POLICY_REFRESH_MS);
9042
+ state2.hold(AUTO_TRACE_POLICY_REFRESH_MS);
8956
9043
  },
8957
9044
  () => {
8958
- state.hold(AUTO_TRACE_POLICY_RETRY_MS);
9045
+ state2.hold(AUTO_TRACE_POLICY_RETRY_MS);
8959
9046
  }
8960
9047
  );
8961
9048
  }
@@ -9566,7 +9653,11 @@ var Bitfab = class {
9566
9653
  const seedCtxForTraceId = parentContext ? null : getSeedContext();
9567
9654
  const traceId = parentContext?.traceId ?? replayCtxForTraceId?.traceId ?? seedCtxForTraceId?.traceId ?? randomUuid();
9568
9655
  const spanId = randomUuid();
9569
- const parentSpanId = parentContext?.spanId ?? null;
9656
+ const openAiAgentsParent = parentContext !== void 0 && options.instrumentation === "trace" ? openAiAgentsParentSpanId(
9657
+ parentContext.traceId,
9658
+ parentContext.openAiAgentsSpanIds
9659
+ ) : void 0;
9660
+ const parentSpanId = openAiAgentsParent ?? parentContext?.spanId ?? null;
9570
9661
  const isRootSpan = parentSpanId === null;
9571
9662
  if (options.enclosingTrace !== void 0) {
9572
9663
  options.enclosingTrace.link.traceId = traceId;
@@ -9580,11 +9671,13 @@ var Bitfab = class {
9580
9671
  parentContext?.surface,
9581
9672
  traceFunctionKey
9582
9673
  );
9674
+ const openAiAgentsSpanIds = openAiAgentsSpanChain();
9583
9675
  const newContext = {
9584
9676
  traceId,
9585
9677
  spanId,
9586
9678
  contexts: [],
9587
- ...surface !== void 0 && { surface }
9679
+ ...surface !== void 0 && { surface },
9680
+ ...openAiAgentsSpanIds.length > 0 && { openAiAgentsSpanIds }
9588
9681
  };
9589
9682
  newStack = [...currentStack, newContext];
9590
9683
  const inputs = args;