globals.cjs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.syncExamplePromises = exports.evaluatorLogFeedbackPromises = exports.testWrapperAsyncLocalStorageInstance = exports.DEFAULT_TEST_CLIENT = void 0;
  4. exports.trackingEnabled = trackingEnabled;
  5. exports._logTestFeedback = _logTestFeedback;
  6. const node_async_hooks_1 = require("node:async_hooks");
  7. const client_js_1 = require("../../client.cjs");
  8. const env_js_1 = require("../env.cjs");
  9. const env_js_2 = require("../../env.cjs");
  10. exports.DEFAULT_TEST_CLIENT = new client_js_1.Client();
  11. exports.testWrapperAsyncLocalStorageInstance = new node_async_hooks_1.AsyncLocalStorage();
  12. function trackingEnabled(context) {
  13. if (typeof context.enableTestTracking === "boolean") {
  14. return context.enableTestTracking;
  15. }
  16. if ((0, env_js_1.getEnvironmentVariable)("LANGSMITH_TEST_TRACKING") === "false") {
  17. return false;
  18. }
  19. return (0, env_js_2.isTracingEnabled)();
  20. }
  21. exports.evaluatorLogFeedbackPromises = new Set();
  22. exports.syncExamplePromises = new Map();
  23. function _logTestFeedback(params) {
  24. const { exampleId, feedback, context, runTree, client, sourceRunId } = params;
  25. if (trackingEnabled(context)) {
  26. if (exampleId === undefined) {
  27. throw new Error("Could not log feedback to LangSmith: missing example id. Please contact us for help.");
  28. }
  29. if (runTree === undefined) {
  30. throw new Error("Could not log feedback to LangSmith: missing run information. Please contact us for help.");
  31. }
  32. exports.evaluatorLogFeedbackPromises.add((async () => {
  33. await exports.syncExamplePromises.get(exampleId);
  34. await client?.logEvaluationFeedback(feedback, runTree, sourceRunId !== undefined
  35. ? { __run: { run_id: sourceRunId } }
  36. : undefined);
  37. })());
  38. }
  39. context.onFeedbackLogged?.(feedback);
  40. }