@fallom/trace 0.1.3 → 0.1.5

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.mjs CHANGED
@@ -1,15 +1,15 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
5
- };
1
+ import {
2
+ __export,
3
+ init,
4
+ prompts_exports
5
+ } from "./chunk-IGJD7GBO.mjs";
6
6
 
7
7
  // src/trace.ts
8
8
  var trace_exports = {};
9
9
  __export(trace_exports, {
10
10
  clearSession: () => clearSession,
11
11
  getSession: () => getSession,
12
- init: () => init,
12
+ init: () => init2,
13
13
  runWithSession: () => runWithSession,
14
14
  setSession: () => setSession,
15
15
  shutdown: () => shutdown,
@@ -664,7 +664,15 @@ var fallomSpanProcessor = {
664
664
  if (ctx) {
665
665
  span2.setAttribute("fallom.config_key", ctx.configKey);
666
666
  span2.setAttribute("fallom.session_id", ctx.sessionId);
667
- log(" Added session context:", ctx.configKey, ctx.sessionId);
667
+ if (ctx.customerId) {
668
+ span2.setAttribute("fallom.customer_id", ctx.customerId);
669
+ }
670
+ log(
671
+ " Added session context:",
672
+ ctx.configKey,
673
+ ctx.sessionId,
674
+ ctx.customerId
675
+ );
668
676
  } else {
669
677
  log(" No session context available");
670
678
  }
@@ -679,7 +687,7 @@ var fallomSpanProcessor = {
679
687
  return Promise.resolve();
680
688
  }
681
689
  };
682
- async function init(options = {}) {
690
+ async function init2(options = {}) {
683
691
  if (initialized) return;
684
692
  debugMode = options.debug ?? false;
685
693
  log("\u{1F680} Initializing Fallom tracing...");
@@ -722,13 +730,41 @@ async function init(options = {}) {
722
730
  }
723
731
  async function getInstrumentations() {
724
732
  const instrumentations = [];
725
- await tryAddInstrumentation(instrumentations, "@traceloop/instrumentation-openai", "OpenAIInstrumentation");
726
- await tryAddInstrumentation(instrumentations, "@traceloop/instrumentation-anthropic", "AnthropicInstrumentation");
727
- await tryAddInstrumentation(instrumentations, "@traceloop/instrumentation-cohere", "CohereInstrumentation");
728
- await tryAddInstrumentation(instrumentations, "@traceloop/instrumentation-bedrock", "BedrockInstrumentation");
729
- await tryAddInstrumentation(instrumentations, "@traceloop/instrumentation-google-generativeai", "GoogleGenerativeAIInstrumentation");
730
- await tryAddInstrumentation(instrumentations, "@traceloop/instrumentation-azure", "AzureOpenAIInstrumentation");
731
- await tryAddInstrumentation(instrumentations, "@traceloop/instrumentation-vertexai", "VertexAIInstrumentation");
733
+ await tryAddInstrumentation(
734
+ instrumentations,
735
+ "@traceloop/instrumentation-openai",
736
+ "OpenAIInstrumentation"
737
+ );
738
+ await tryAddInstrumentation(
739
+ instrumentations,
740
+ "@traceloop/instrumentation-anthropic",
741
+ "AnthropicInstrumentation"
742
+ );
743
+ await tryAddInstrumentation(
744
+ instrumentations,
745
+ "@traceloop/instrumentation-cohere",
746
+ "CohereInstrumentation"
747
+ );
748
+ await tryAddInstrumentation(
749
+ instrumentations,
750
+ "@traceloop/instrumentation-bedrock",
751
+ "BedrockInstrumentation"
752
+ );
753
+ await tryAddInstrumentation(
754
+ instrumentations,
755
+ "@traceloop/instrumentation-google-generativeai",
756
+ "GoogleGenerativeAIInstrumentation"
757
+ );
758
+ await tryAddInstrumentation(
759
+ instrumentations,
760
+ "@traceloop/instrumentation-azure",
761
+ "AzureOpenAIInstrumentation"
762
+ );
763
+ await tryAddInstrumentation(
764
+ instrumentations,
765
+ "@traceloop/instrumentation-vertexai",
766
+ "VertexAIInstrumentation"
767
+ );
732
768
  return instrumentations;
733
769
  }
734
770
  async function tryAddInstrumentation(instrumentations, pkg, className) {
@@ -736,25 +772,37 @@ async function tryAddInstrumentation(instrumentations, pkg, className) {
736
772
  const mod = await import(pkg);
737
773
  const InstrumentationClass = mod[className] || mod.default?.[className];
738
774
  if (InstrumentationClass) {
739
- instrumentations.push(new InstrumentationClass({ traceContent: captureContent }));
775
+ instrumentations.push(
776
+ new InstrumentationClass({ traceContent: captureContent })
777
+ );
740
778
  log(` \u2705 Loaded ${pkg}`);
741
779
  } else {
742
- log(` \u26A0\uFE0F ${pkg} loaded but ${className} not found. Available:`, Object.keys(mod));
780
+ log(
781
+ ` \u26A0\uFE0F ${pkg} loaded but ${className} not found. Available:`,
782
+ Object.keys(mod)
783
+ );
743
784
  }
744
785
  } catch (e) {
745
786
  log(` \u274C ${pkg} not installed`);
746
787
  }
747
788
  }
748
- function setSession(configKey, sessionId) {
789
+ function setSession(configKey, sessionId, customerId) {
749
790
  const store = sessionStorage.getStore();
750
791
  if (store) {
751
792
  store.configKey = configKey;
752
793
  store.sessionId = sessionId;
794
+ store.customerId = customerId;
753
795
  }
754
- fallbackSession = { configKey, sessionId };
796
+ fallbackSession = { configKey, sessionId, customerId };
755
797
  }
756
- function runWithSession(configKey, sessionId, fn) {
757
- return sessionStorage.run({ configKey, sessionId }, fn);
798
+ function runWithSession(configKey, sessionId, customerIdOrFn, fn) {
799
+ if (typeof customerIdOrFn === "function") {
800
+ return sessionStorage.run({ configKey, sessionId }, customerIdOrFn);
801
+ }
802
+ return sessionStorage.run(
803
+ { configKey, sessionId, customerId: customerIdOrFn },
804
+ fn
805
+ );
758
806
  }
759
807
  function getSession() {
760
808
  return sessionStorage.getStore() || fallbackSession || void 0;
@@ -823,12 +871,20 @@ async function sendTrace(trace) {
823
871
  }
824
872
  }
825
873
  function wrapOpenAI(client) {
826
- const originalCreate = client.chat.completions.create.bind(client.chat.completions);
874
+ const originalCreate = client.chat.completions.create.bind(
875
+ client.chat.completions
876
+ );
827
877
  client.chat.completions.create = async function(...args) {
828
878
  const ctx = sessionStorage.getStore() || fallbackSession;
829
879
  if (!ctx || !initialized) {
830
880
  return originalCreate(...args);
831
881
  }
882
+ let promptCtx = null;
883
+ try {
884
+ const { getPromptContext } = await import("./prompts-67DJ33I4.mjs");
885
+ promptCtx = getPromptContext();
886
+ } catch {
887
+ }
832
888
  const params = args[0] || {};
833
889
  const startTime = Date.now();
834
890
  try {
@@ -837,6 +893,7 @@ function wrapOpenAI(client) {
837
893
  sendTrace({
838
894
  config_key: ctx.configKey,
839
895
  session_id: ctx.sessionId,
896
+ customer_id: ctx.customerId,
840
897
  name: "chat.completions.create",
841
898
  model: response?.model || params?.model,
842
899
  start_time: new Date(startTime).toISOString(),
@@ -847,7 +904,11 @@ function wrapOpenAI(client) {
847
904
  completion_tokens: response?.usage?.completion_tokens,
848
905
  total_tokens: response?.usage?.total_tokens,
849
906
  input: captureContent ? JSON.stringify(params?.messages) : void 0,
850
- output: captureContent ? response?.choices?.[0]?.message?.content : void 0
907
+ output: captureContent ? response?.choices?.[0]?.message?.content : void 0,
908
+ prompt_key: promptCtx?.promptKey,
909
+ prompt_version: promptCtx?.promptVersion,
910
+ prompt_ab_test_key: promptCtx?.abTestKey,
911
+ prompt_variant_index: promptCtx?.variantIndex
851
912
  }).catch(() => {
852
913
  });
853
914
  return response;
@@ -856,13 +917,18 @@ function wrapOpenAI(client) {
856
917
  sendTrace({
857
918
  config_key: ctx.configKey,
858
919
  session_id: ctx.sessionId,
920
+ customer_id: ctx.customerId,
859
921
  name: "chat.completions.create",
860
922
  model: params?.model,
861
923
  start_time: new Date(startTime).toISOString(),
862
924
  end_time: new Date(endTime).toISOString(),
863
925
  duration_ms: endTime - startTime,
864
926
  status: "ERROR",
865
- error_message: error?.message
927
+ error_message: error?.message,
928
+ prompt_key: promptCtx?.promptKey,
929
+ prompt_version: promptCtx?.promptVersion,
930
+ prompt_ab_test_key: promptCtx?.abTestKey,
931
+ prompt_variant_index: promptCtx?.variantIndex
866
932
  }).catch(() => {
867
933
  });
868
934
  throw error;
@@ -877,6 +943,12 @@ function wrapAnthropic(client) {
877
943
  if (!ctx || !initialized) {
878
944
  return originalCreate(...args);
879
945
  }
946
+ let promptCtx = null;
947
+ try {
948
+ const { getPromptContext } = await import("./prompts-67DJ33I4.mjs");
949
+ promptCtx = getPromptContext();
950
+ } catch {
951
+ }
880
952
  const params = args[0] || {};
881
953
  const startTime = Date.now();
882
954
  try {
@@ -885,6 +957,7 @@ function wrapAnthropic(client) {
885
957
  sendTrace({
886
958
  config_key: ctx.configKey,
887
959
  session_id: ctx.sessionId,
960
+ customer_id: ctx.customerId,
888
961
  name: "messages.create",
889
962
  model: response?.model || params?.model,
890
963
  start_time: new Date(startTime).toISOString(),
@@ -895,7 +968,11 @@ function wrapAnthropic(client) {
895
968
  completion_tokens: response?.usage?.output_tokens,
896
969
  total_tokens: (response?.usage?.input_tokens || 0) + (response?.usage?.output_tokens || 0),
897
970
  input: captureContent ? JSON.stringify(params?.messages) : void 0,
898
- output: captureContent ? response?.content?.[0]?.text : void 0
971
+ output: captureContent ? response?.content?.[0]?.text : void 0,
972
+ prompt_key: promptCtx?.promptKey,
973
+ prompt_version: promptCtx?.promptVersion,
974
+ prompt_ab_test_key: promptCtx?.abTestKey,
975
+ prompt_variant_index: promptCtx?.variantIndex
899
976
  }).catch(() => {
900
977
  });
901
978
  return response;
@@ -904,13 +981,18 @@ function wrapAnthropic(client) {
904
981
  sendTrace({
905
982
  config_key: ctx.configKey,
906
983
  session_id: ctx.sessionId,
984
+ customer_id: ctx.customerId,
907
985
  name: "messages.create",
908
986
  model: params?.model,
909
987
  start_time: new Date(startTime).toISOString(),
910
988
  end_time: new Date(endTime).toISOString(),
911
989
  duration_ms: endTime - startTime,
912
990
  status: "ERROR",
913
- error_message: error?.message
991
+ error_message: error?.message,
992
+ prompt_key: promptCtx?.promptKey,
993
+ prompt_version: promptCtx?.promptVersion,
994
+ prompt_ab_test_key: promptCtx?.abTestKey,
995
+ prompt_variant_index: promptCtx?.variantIndex
914
996
  }).catch(() => {
915
997
  });
916
998
  throw error;
@@ -925,6 +1007,12 @@ function wrapGoogleAI(model) {
925
1007
  if (!ctx || !initialized) {
926
1008
  return originalGenerate(...args);
927
1009
  }
1010
+ let promptCtx = null;
1011
+ try {
1012
+ const { getPromptContext } = await import("./prompts-67DJ33I4.mjs");
1013
+ promptCtx = getPromptContext();
1014
+ } catch {
1015
+ }
928
1016
  const startTime = Date.now();
929
1017
  try {
930
1018
  const response = await originalGenerate(...args);
@@ -934,6 +1022,7 @@ function wrapGoogleAI(model) {
934
1022
  sendTrace({
935
1023
  config_key: ctx.configKey,
936
1024
  session_id: ctx.sessionId,
1025
+ customer_id: ctx.customerId,
937
1026
  name: "generateContent",
938
1027
  model: model?.model || "gemini",
939
1028
  start_time: new Date(startTime).toISOString(),
@@ -944,7 +1033,11 @@ function wrapGoogleAI(model) {
944
1033
  completion_tokens: usage?.candidatesTokenCount,
945
1034
  total_tokens: usage?.totalTokenCount,
946
1035
  input: captureContent ? JSON.stringify(args[0]) : void 0,
947
- output: captureContent ? result?.text?.() : void 0
1036
+ output: captureContent ? result?.text?.() : void 0,
1037
+ prompt_key: promptCtx?.promptKey,
1038
+ prompt_version: promptCtx?.promptVersion,
1039
+ prompt_ab_test_key: promptCtx?.abTestKey,
1040
+ prompt_variant_index: promptCtx?.variantIndex
948
1041
  }).catch(() => {
949
1042
  });
950
1043
  return response;
@@ -953,13 +1046,18 @@ function wrapGoogleAI(model) {
953
1046
  sendTrace({
954
1047
  config_key: ctx.configKey,
955
1048
  session_id: ctx.sessionId,
1049
+ customer_id: ctx.customerId,
956
1050
  name: "generateContent",
957
1051
  model: model?.model || "gemini",
958
1052
  start_time: new Date(startTime).toISOString(),
959
1053
  end_time: new Date(endTime).toISOString(),
960
1054
  duration_ms: endTime - startTime,
961
1055
  status: "ERROR",
962
- error_message: error?.message
1056
+ error_message: error?.message,
1057
+ prompt_key: promptCtx?.promptKey,
1058
+ prompt_version: promptCtx?.promptVersion,
1059
+ prompt_ab_test_key: promptCtx?.abTestKey,
1060
+ prompt_variant_index: promptCtx?.variantIndex
963
1061
  }).catch(() => {
964
1062
  });
965
1063
  throw error;
@@ -972,7 +1070,7 @@ function wrapGoogleAI(model) {
972
1070
  var models_exports = {};
973
1071
  __export(models_exports, {
974
1072
  get: () => get,
975
- init: () => init2
1073
+ init: () => init3
976
1074
  });
977
1075
  import { createHash } from "crypto";
978
1076
  var apiKey2 = null;
@@ -988,7 +1086,7 @@ function log2(msg) {
988
1086
  console.log(`[Fallom] ${msg}`);
989
1087
  }
990
1088
  }
991
- function init2(options = {}) {
1089
+ function init3(options = {}) {
992
1090
  apiKey2 = options.apiKey || process.env.FALLOM_API_KEY || null;
993
1091
  baseUrl2 = options.baseUrl || process.env.FALLOM_BASE_URL || "https://spans.fallom.com";
994
1092
  initialized2 = true;
@@ -1008,7 +1106,7 @@ function init2(options = {}) {
1008
1106
  function ensureInit() {
1009
1107
  if (!initialized2) {
1010
1108
  try {
1011
- init2();
1109
+ init3();
1012
1110
  } catch {
1013
1111
  }
1014
1112
  }
@@ -1192,15 +1290,19 @@ async function recordSession(configKey, version, sessionId, model) {
1192
1290
  }
1193
1291
 
1194
1292
  // src/init.ts
1195
- async function init3(options = {}) {
1293
+ async function init4(options = {}) {
1196
1294
  const baseUrl3 = options.baseUrl || process.env.FALLOM_BASE_URL || "https://spans.fallom.com";
1197
- await init({
1295
+ await init2({
1198
1296
  apiKey: options.apiKey,
1199
1297
  baseUrl: baseUrl3,
1200
1298
  captureContent: options.captureContent,
1201
1299
  debug: options.debug
1202
1300
  });
1203
- init2({
1301
+ init3({
1302
+ apiKey: options.apiKey,
1303
+ baseUrl: baseUrl3
1304
+ });
1305
+ init({
1204
1306
  apiKey: options.apiKey,
1205
1307
  baseUrl: baseUrl3
1206
1308
  });
@@ -1208,13 +1310,15 @@ async function init3(options = {}) {
1208
1310
 
1209
1311
  // src/index.ts
1210
1312
  var index_default = {
1211
- init: init3,
1313
+ init: init4,
1212
1314
  trace: trace_exports,
1213
- models: models_exports
1315
+ models: models_exports,
1316
+ prompts: prompts_exports
1214
1317
  };
1215
1318
  export {
1216
1319
  index_default as default,
1217
- init3 as init,
1320
+ init4 as init,
1218
1321
  models_exports as models,
1322
+ prompts_exports as prompts,
1219
1323
  trace_exports as trace
1220
1324
  };
@@ -0,0 +1,14 @@
1
+ import {
2
+ clearPromptContext,
3
+ get,
4
+ getAB,
5
+ getPromptContext,
6
+ init
7
+ } from "./chunk-IGJD7GBO.mjs";
8
+ export {
9
+ clearPromptContext,
10
+ get,
11
+ getAB,
12
+ getPromptContext,
13
+ init
14
+ };
@@ -0,0 +1,14 @@
1
+ import {
2
+ clearPromptContext,
3
+ get,
4
+ getAB,
5
+ getPromptContext,
6
+ init
7
+ } from "./chunk-VNUUS74T.mjs";
8
+ export {
9
+ clearPromptContext,
10
+ get,
11
+ getAB,
12
+ getPromptContext,
13
+ init
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fallom/trace",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Model A/B testing and tracing for LLM applications. Zero latency, production-ready.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",