bitfab 0.66.0 → 0.68.0

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.
Files changed (54) hide show
  1. package/dist/{autoTrace-CQw0qMBt.cjs → autoTrace-BikML0lX.cjs} +86 -4
  2. package/dist/autoTrace-BikML0lX.cjs.map +1 -0
  3. package/dist/{autoTrace-O40k8f5M.js → autoTrace-CVt9EDyo.js} +75 -5
  4. package/dist/autoTrace-CVt9EDyo.js.map +1 -0
  5. package/dist/autoTrace.cjs +3 -1
  6. package/dist/autoTrace.d.cts +23 -0
  7. package/dist/autoTrace.d.ts +23 -0
  8. package/dist/autoTrace.js +1 -1
  9. package/dist/{babelPlugin-C70OxrqA.js → babelPlugin-a7ofCoOd.js} +83 -4
  10. package/dist/babelPlugin-a7ofCoOd.js.map +1 -0
  11. package/dist/{babelPlugin-Nta4-VcL.cjs → babelPlugin-s8EFjT6e.cjs} +83 -4
  12. package/dist/babelPlugin-s8EFjT6e.cjs.map +1 -0
  13. package/dist/{http-D91qP2J-.js → http-_cgR_DM2.js} +3 -3
  14. package/dist/{http-D91qP2J-.js.map → http-_cgR_DM2.js.map} +1 -1
  15. package/dist/{http-DIz8mEMq.cjs → http-psFF1ptn.cjs} +3 -3
  16. package/dist/{http-DIz8mEMq.cjs.map → http-psFF1ptn.cjs.map} +1 -1
  17. package/dist/{index-DiYFr-KV.d.cts → index-j7SrvOqe.d.cts} +44 -2
  18. package/dist/{index-DiYFr-KV.d.ts → index-j7SrvOqe.d.ts} +44 -2
  19. package/dist/index.cjs +2 -2
  20. package/dist/index.d.cts +2 -2
  21. package/dist/index.d.ts +2 -2
  22. package/dist/index.js +2 -2
  23. package/dist/node.cjs +3 -3
  24. package/dist/node.d.cts +2 -2
  25. package/dist/node.d.ts +2 -2
  26. package/dist/node.js +3 -3
  27. package/dist/registryCommand-Dk4Y1ZgW.js.map +1 -1
  28. package/dist/{src-CL5eQlNk.js → src-DyEKM7yn.js} +178 -14
  29. package/dist/src-DyEKM7yn.js.map +1 -0
  30. package/dist/{src-rJcHXJEz.cjs → src-Dze8o7iU.cjs} +178 -14
  31. package/dist/src-Dze8o7iU.cjs.map +1 -0
  32. package/dist/transform/babel.cjs +1 -1
  33. package/dist/transform/babel.d.cts +3 -0
  34. package/dist/transform/babel.d.ts +3 -0
  35. package/dist/transform/babel.js +1 -1
  36. package/dist/transform/loaderRuntime.cjs +2 -2
  37. package/dist/transform/loaderRuntime.js +2 -2
  38. package/dist/transform/nodeLoader.js +82 -3
  39. package/dist/transform/nodeLoader.js.map +1 -1
  40. package/dist/transform/swc.cjs +1 -1
  41. package/dist/transform/swc.js +1 -1
  42. package/dist/transform/unplugin.cjs +2 -2
  43. package/dist/transform/unplugin.js +2 -2
  44. package/dist/{transform-BjxHGGKc.cjs → transform-BBzrqpVH.cjs} +2 -2
  45. package/dist/{transform-BjxHGGKc.cjs.map → transform-BBzrqpVH.cjs.map} +1 -1
  46. package/dist/{transform-BcyUocwm.js → transform-DkcFn_tg.js} +2 -2
  47. package/dist/{transform-BcyUocwm.js.map → transform-DkcFn_tg.js.map} +1 -1
  48. package/package.json +2 -1
  49. package/dist/autoTrace-CQw0qMBt.cjs.map +0 -1
  50. package/dist/autoTrace-O40k8f5M.js.map +0 -1
  51. package/dist/babelPlugin-C70OxrqA.js.map +0 -1
  52. package/dist/babelPlugin-Nta4-VcL.cjs.map +0 -1
  53. package/dist/src-CL5eQlNk.js.map +0 -1
  54. package/dist/src-rJcHXJEz.cjs.map +0 -1
@@ -1,6 +1,6 @@
1
1
  const require_bamlPeer = require("./bamlPeer-BUoZgNxZ.cjs");
2
- const require_http = require("./http-DIz8mEMq.cjs");
3
- const require_autoTrace = require("./autoTrace-CQw0qMBt.cjs");
2
+ const require_http = require("./http-psFF1ptn.cjs");
3
+ const require_autoTrace = require("./autoTrace-BikML0lX.cjs");
4
4
  let superjson = require("superjson");
5
5
  superjson = require_bamlPeer.__toESM(superjson, 1);
6
6
  //#region src/categories.ts
@@ -1407,6 +1407,89 @@ var GradersClient = class {
1407
1407
  }
1408
1408
  };
1409
1409
  //#endregion
1410
+ //#region src/jev.ts
1411
+ const JEV_PROVIDER = "typesafe";
1412
+ const JEV_WATCH_STORAGE_SYMBOL = Symbol.for("bitfab.jevWatchStorage");
1413
+ const repliesRecordedByAutoTrace = /* @__PURE__ */ new WeakSet();
1414
+ function isRecord$3(value) {
1415
+ return typeof value === "object" && value !== null;
1416
+ }
1417
+ function isJevReply(value) {
1418
+ return isRecord$3(value) && typeof value.model === "string" && isRecord$3(value.usage);
1419
+ }
1420
+ function tokenCount(usage, key) {
1421
+ const count = usage[key];
1422
+ return typeof count === "number" ? count : null;
1423
+ }
1424
+ function jevContext(reply, durationMs) {
1425
+ if (!isJevReply(reply)) return null;
1426
+ const context = {
1427
+ provider: JEV_PROVIDER,
1428
+ model: reply.model
1429
+ };
1430
+ const inputTokens = tokenCount(reply.usage, "input_tokens");
1431
+ const outputTokens = tokenCount(reply.usage, "output_tokens");
1432
+ if (inputTokens !== null) context.inputTokens = inputTokens;
1433
+ if (outputTokens !== null) context.outputTokens = outputTokens;
1434
+ context.durationMs = durationMs;
1435
+ return context;
1436
+ }
1437
+ function recordJevReply(span, reply, durationMs) {
1438
+ try {
1439
+ const context = jevContext(reply, durationMs);
1440
+ if (context) span.addContext(context);
1441
+ } catch {}
1442
+ }
1443
+ let jevWatchStorage = null;
1444
+ function watchStorage() {
1445
+ if (jevWatchStorage) return jevWatchStorage;
1446
+ const shared = globalThis;
1447
+ const existing = shared[JEV_WATCH_STORAGE_SYMBOL];
1448
+ jevWatchStorage = existing ?? require_autoTrace.createAsyncLocalStorage();
1449
+ if (jevWatchStorage && !existing) shared[JEV_WATCH_STORAGE_SYMBOL] = jevWatchStorage;
1450
+ return jevWatchStorage;
1451
+ }
1452
+ function watchJevUsage(fn) {
1453
+ const storage = watchStorage();
1454
+ const watch = {
1455
+ recorded: false,
1456
+ parent: storage?.getStore() ?? null
1457
+ };
1458
+ return {
1459
+ watch,
1460
+ result: storage ? storage.run(watch, fn) : fn()
1461
+ };
1462
+ }
1463
+ function markEnclosingWatches(watch) {
1464
+ for (let current = watch.parent; current; current = current.parent) current.recorded = true;
1465
+ }
1466
+ function recordWatchedJevReply(span, watch, reply, durationMs) {
1467
+ if (watch.recorded || isRecordedByAutoTrace(reply)) {
1468
+ markEnclosingWatches(watch);
1469
+ return;
1470
+ }
1471
+ if (!isJevReply(reply)) return;
1472
+ recordJevReply(span, reply, durationMs);
1473
+ repliesRecordedByAutoTrace.add(reply);
1474
+ markEnclosingWatches(watch);
1475
+ }
1476
+ function isRecordedByAutoTrace(reply) {
1477
+ return isRecord$3(reply) && repliesRecordedByAutoTrace.has(reply);
1478
+ }
1479
+ function recordAutoTracedJev(invokeFn, span) {
1480
+ const startedAt = Date.now();
1481
+ const { watch, result } = watchJevUsage(invokeFn);
1482
+ const settle = (reply) => {
1483
+ recordWatchedJevReply(span, watch, reply, Date.now() - startedAt);
1484
+ };
1485
+ if (result instanceof Promise) return result.then((reply) => {
1486
+ settle(reply);
1487
+ return reply;
1488
+ });
1489
+ settle(result);
1490
+ return result;
1491
+ }
1492
+ //#endregion
1410
1493
  //#region src/langgraph.ts
1411
1494
  const LANGSMITH_HIDDEN_TAG = "langsmith:hidden";
1412
1495
  const CHAIN_RUN_TYPES = /* @__PURE__ */ new Set([
@@ -4713,6 +4796,62 @@ function extractContextFromCollector(collector) {
4713
4796
  return null;
4714
4797
  }
4715
4798
  }
4799
+ const BOUND_FUNCTION_NAME_PREFIX = /^bound /;
4800
+ function discoveredSpanType(definition, nodeConfiguration) {
4801
+ const recordsModelCall = definition.baml === true || definition.jev === true;
4802
+ const ownType = recordsModelCall ? "llm" : "function";
4803
+ if (nodeConfiguration === void 0) return ownType;
4804
+ if (recordsModelCall && nodeConfiguration.defaultType === true) return ownType;
4805
+ return nodeConfiguration.type ?? ownType;
4806
+ }
4807
+ const collectorsRecordedByAutoTrace = /* @__PURE__ */ new WeakSet();
4808
+ function isPlainObject(value) {
4809
+ return typeof value === "object" && value !== null && !Array.isArray(value);
4810
+ }
4811
+ function collectorsIn(options) {
4812
+ const collector = options.collector;
4813
+ if (collector === void 0 || collector === null) return [];
4814
+ return Array.isArray(collector) ? collector : [collector];
4815
+ }
4816
+ function recordBamlCollector(collector) {
4817
+ try {
4818
+ const prompt = extractPromptFromCollector(collector);
4819
+ if (prompt) getCurrentSpan().setPrompt(prompt);
4820
+ const metadata = extractContextFromCollector(collector);
4821
+ if (metadata) getCurrentSpan().addContext(metadata);
4822
+ } catch {}
4823
+ }
4824
+ function invokeBamlWithCollector(invokeFn, CollectorClass) {
4825
+ const collector = new CollectorClass("bitfab-baml-tracing");
4826
+ let existingCollectors = [];
4827
+ const result = invokeFn((callerOptions, clientOptions) => {
4828
+ const caller = isPlainObject(callerOptions) ? callerOptions : {};
4829
+ const client = isPlainObject(clientOptions) ? clientOptions : {};
4830
+ existingCollectors = collectorsIn(caller.collector === void 0 ? client : caller);
4831
+ return {
4832
+ ...caller,
4833
+ collector: [...existingCollectors, collector]
4834
+ };
4835
+ });
4836
+ const settle = () => {
4837
+ for (const existing of existingCollectors) if (typeof existing === "object" && existing !== null) collectorsRecordedByAutoTrace.add(existing);
4838
+ recordBamlCollector(collector);
4839
+ };
4840
+ if (result instanceof Promise) return result.then((value) => {
4841
+ settle();
4842
+ return value;
4843
+ });
4844
+ settle();
4845
+ return result;
4846
+ }
4847
+ function invokeAutoTracedBaml(invokeFn, isAsync) {
4848
+ if (cachedCollectorClass) return invokeBamlWithCollector(invokeFn, cachedCollectorClass);
4849
+ if (cachedCollectorClass === null || !isAsync) {
4850
+ loadCollectorClass();
4851
+ return invokeFn();
4852
+ }
4853
+ return loadCollectorClass().then((CollectorClass) => CollectorClass ? invokeBamlWithCollector(invokeFn, CollectorClass) : invokeFn());
4854
+ }
4716
4855
  function isSpanType(value) {
4717
4856
  return require_http.SPAN_TYPES.some((spanType) => spanType === value);
4718
4857
  }
@@ -4936,7 +5075,7 @@ var Bitfab = class {
4936
5075
  const fn = typeof optionsOrFn === "function" ? optionsOrFn : maybeFn;
4937
5076
  if (!fn) throw new require_http.BitfabError("bitfab.withNode requires a function");
4938
5077
  const configuration = this.resolveNodeConfiguration(options);
4939
- const functionName = internalFunctionName ?? fn.name;
5078
+ const functionName = (internalFunctionName ?? fn.name).replace(BOUND_FUNCTION_NAME_PREFIX, "");
4940
5079
  if (functionName === "") throw new require_http.BitfabError("bitfab.withNode requires a named function so the subtree transform can bind its configuration to the correct call.");
4941
5080
  return this.createAutoTraceNode(configuration, fn, functionName);
4942
5081
  }
@@ -4947,6 +5086,7 @@ var Bitfab = class {
4947
5086
  return {
4948
5087
  capture,
4949
5088
  type: options.type ?? "custom",
5089
+ ...options.type === void 0 && { defaultType: true },
4950
5090
  ...options.name !== void 0 && { name: options.name },
4951
5091
  ...experimentId !== void 0 && { experimentId },
4952
5092
  ...options.mockOnReplay !== void 0 && { mockOnReplay: options.mockOnReplay },
@@ -4970,6 +5110,7 @@ var Bitfab = class {
4970
5110
  createAutoTraceRoot(traceFunctionKey, name, options, fn, rootFunctionName = "") {
4971
5111
  const self = this;
4972
5112
  self.simulationPlan.refresh();
5113
+ loadCollectorClass();
4973
5114
  const runRootFunction = (receiver, args) => require_autoTrace.runAsAutoTraceRootFunction(rootFunctionName, () => fn.apply(receiver, args));
4974
5115
  const maxDepth = autoTraceLimit(options.maxDepth, DEFAULT_AUTO_TRACE_MAX_DEPTH);
4975
5116
  const maxCapturedSubtreeSpans = autoTraceLimit(options.maxCapturedSubtreeSpans, DEFAULT_AUTO_TRACE_MAX_CAPTURED_SUBTREE_SPANS);
@@ -5043,7 +5184,7 @@ var Bitfab = class {
5043
5184
  const nodeExperimentId = nodeConfiguration === void 0 ? void 0 : resolveSpanExperimentId(nodeConfiguration);
5044
5185
  const childOptions = {
5045
5186
  name: spanName,
5046
- type: nodeConfiguration?.type ?? "function",
5187
+ type: discoveredSpanType(definition, nodeConfiguration),
5047
5188
  captureWhen: "nested",
5048
5189
  surface: "opt-out",
5049
5190
  instrumentation: "trace",
@@ -5057,7 +5198,8 @@ var Bitfab = class {
5057
5198
  ...nodeConfiguration?.replayReusable !== void 0 && { replayReusable: nodeConfiguration.replayReusable },
5058
5199
  ...nodeConfiguration?.finalize !== void 0 && { finalize: nodeConfiguration.finalize }
5059
5200
  };
5060
- return openChild(entry, childOptions, inputs, definition.async === true, invokeFn);
5201
+ const isAsync = definition.async === true;
5202
+ return openChild(entry, childOptions, inputs, isAsync, definition.baml === true ? () => invokeAutoTracedBaml(invokeFn, isAsync) : definition.jev === true ? () => recordAutoTracedJev(invokeFn, getCurrentSpan()) : invokeFn);
5061
5203
  },
5062
5204
  invokeNestedRoot(nestedName, nestedFunctionName, inputs, invokeFn, entry, link, spanDetails) {
5063
5205
  if (excluded.has(nestedName) || excluded.has(nestedFunctionName) || !recordsSpan(entry, nestedName, true)) return invokeFn();
@@ -5328,18 +5470,37 @@ var Bitfab = class {
5328
5470
  }
5329
5471
  const result = await trackedMethod.bind(trackedClient)(...args);
5330
5472
  wrappedFn.collector = collector;
5331
- try {
5332
- const prompt = extractPromptFromCollector(collector);
5333
- if (prompt) getCurrentSpan().setPrompt(prompt);
5334
- const metadata = extractContextFromCollector(collector);
5335
- if (metadata) getCurrentSpan().addContext(metadata);
5336
- } catch {}
5473
+ if (!collectorsRecordedByAutoTrace.has(collector)) recordBamlCollector(collector);
5337
5474
  try {
5338
5475
  options?.onCollector?.(collector);
5339
5476
  } catch {}
5340
5477
  return result;
5341
5478
  };
5342
5479
  wrappedFn.collector = null;
5480
+ Object.defineProperty(wrappedFn, "name", {
5481
+ configurable: true,
5482
+ value: methodName
5483
+ });
5484
+ return wrappedFn;
5485
+ }
5486
+ wrapJev(method, options) {
5487
+ const methodName = method.name.replace(BOUND_FUNCTION_NAME_PREFIX, "");
5488
+ if (!methodName) throw new require_http.BitfabError("wrapJev requires a named function (e.g., jev.systemOne.bind(jev)).");
5489
+ const wrappedFn = async (...args) => {
5490
+ const span = getCurrentSpan();
5491
+ const startedAt = Date.now();
5492
+ const { watch, result } = watchJevUsage(() => method(...args));
5493
+ const reply = await result;
5494
+ recordWatchedJevReply(span, watch, reply, Date.now() - startedAt);
5495
+ try {
5496
+ options?.onReply?.(reply);
5497
+ } catch {}
5498
+ return reply;
5499
+ };
5500
+ Object.defineProperty(wrappedFn, "name", {
5501
+ configurable: true,
5502
+ value: methodName
5503
+ });
5343
5504
  return wrappedFn;
5344
5505
  }
5345
5506
  withSpan(traceFunctionKey, optionsOrFn, maybeFn) {
@@ -5978,7 +6139,7 @@ var Bitfab = class {
5978
6139
  await runWithSeedContext({ traceId }, async () => target(...args));
5979
6140
  } finally {
5980
6141
  try {
5981
- const { flushTraces } = await Promise.resolve().then(() => require("./http-DIz8mEMq.cjs")).then((n) => n.http_exports);
6142
+ const { flushTraces } = await Promise.resolve().then(() => require("./http-psFF1ptn.cjs")).then((n) => n.http_exports);
5982
6143
  await flushTraces(3e4);
5983
6144
  } finally {
5984
6145
  unrecorded = activeTraceStates.delete(traceId);
@@ -6064,6 +6225,9 @@ var BitfabFunction = class {
6064
6225
  wrapBAML(methodOrClient, maybeMethodOrOptions, maybeOptions) {
6065
6226
  return this.client.wrapBAML(methodOrClient, maybeMethodOrOptions, maybeOptions);
6066
6227
  }
6228
+ wrapJev(method, options) {
6229
+ return this.client.wrapJev(method, options);
6230
+ }
6067
6231
  };
6068
6232
  //#endregion
6069
6233
  //#region src/finalizers.ts
@@ -6166,7 +6330,7 @@ async function seedFromRegistry(registry, pipeline, cases, options = {}) {
6166
6330
  metadata: seedCase.metadata,
6167
6331
  sessionId: seedCase.sessionId
6168
6332
  }));
6169
- const { flushTraces } = await Promise.resolve().then(() => require("./http-DIz8mEMq.cjs")).then((n) => n.http_exports);
6333
+ const { flushTraces } = await Promise.resolve().then(() => require("./http-psFF1ptn.cjs")).then((n) => n.http_exports);
6170
6334
  await flushTraces(3e4);
6171
6335
  return {
6172
6336
  pipeline,
@@ -6410,4 +6574,4 @@ Object.defineProperty(exports, "serializeReplayResult", {
6410
6574
  }
6411
6575
  });
6412
6576
 
6413
- //# sourceMappingURL=src-rJcHXJEz.cjs.map
6577
+ //# sourceMappingURL=src-Dze8o7iU.cjs.map