globals.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { AsyncLocalStorage } from "node:async_hooks";
  2. import { Client } from "../../client.js";
  3. import { getEnvironmentVariable } from "../env.js";
  4. import { isTracingEnabled } from "../../env.js";
  5. export const DEFAULT_TEST_CLIENT = new Client();
  6. export const testWrapperAsyncLocalStorageInstance = new AsyncLocalStorage();
  7. export function trackingEnabled(context) {
  8. if (typeof context.enableTestTracking === "boolean") {
  9. return context.enableTestTracking;
  10. }
  11. if (getEnvironmentVariable("LANGSMITH_TEST_TRACKING") === "false") {
  12. return false;
  13. }
  14. return isTracingEnabled();
  15. }
  16. export const evaluatorLogFeedbackPromises = new Set();
  17. export const syncExamplePromises = new Map();
  18. export function _logTestFeedback(params) {
  19. const { exampleId, feedback, context, runTree, client, sourceRunId } = params;
  20. if (trackingEnabled(context)) {
  21. if (exampleId === undefined) {
  22. throw new Error("Could not log feedback to LangSmith: missing example id. Please contact us for help.");
  23. }
  24. if (runTree === undefined) {
  25. throw new Error("Could not log feedback to LangSmith: missing run information. Please contact us for help.");
  26. }
  27. evaluatorLogFeedbackPromises.add((async () => {
  28. await syncExamplePromises.get(exampleId);
  29. await client?.logEvaluationFeedback(feedback, runTree, sourceRunId !== undefined
  30. ? { __run: { run_id: sourceRunId } }
  31. : undefined);
  32. })());
  33. }
  34. context.onFeedbackLogged?.(feedback);
  35. }