generic.cjs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.wrapSDK = exports._wrapClient = void 0;
  4. const traceable_js_1 = require("../traceable.cjs");
  5. const _wrapClient = (sdk, runName, options) => {
  6. return new Proxy(sdk, {
  7. get(target, propKey, receiver) {
  8. const originalValue = target[propKey];
  9. if (typeof originalValue === "function") {
  10. return (0, traceable_js_1.traceable)(originalValue.bind(target), {
  11. run_type: "llm",
  12. ...options,
  13. name: [runName, propKey.toString()].join("."),
  14. });
  15. }
  16. else if (originalValue != null &&
  17. !Array.isArray(originalValue) &&
  18. // eslint-disable-next-line no-instanceof/no-instanceof
  19. !(originalValue instanceof Date) &&
  20. typeof originalValue === "object") {
  21. return (0, exports._wrapClient)(originalValue, [runName, propKey.toString()].join("."), options);
  22. }
  23. else {
  24. return Reflect.get(target, propKey, receiver);
  25. }
  26. },
  27. });
  28. };
  29. exports._wrapClient = _wrapClient;
  30. /**
  31. * Wrap an arbitrary SDK, enabling automatic LangSmith tracing.
  32. * Method signatures are unchanged.
  33. *
  34. * Note that this will wrap and trace ALL SDK methods, not just
  35. * LLM completion methods. If the passed SDK contains other methods,
  36. * we recommend using the wrapped instance for LLM calls only.
  37. * @param sdk An arbitrary SDK instance.
  38. * @param options LangSmith options.
  39. * @returns
  40. */
  41. const wrapSDK = (sdk, options) => {
  42. const traceableOptions = options ? { ...options } : undefined;
  43. if (traceableOptions != null) {
  44. delete traceableOptions.runName;
  45. delete traceableOptions.name;
  46. }
  47. return (0, exports._wrapClient)(sdk, options?.name ?? options?.runName ?? sdk.constructor?.name, traceableOptions);
  48. };
  49. exports.wrapSDK = wrapSDK;