index.cjs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.serialize = serialize;
  4. /* eslint-disable */
  5. // @ts-nocheck
  6. var LIMIT_REPLACE_NODE = "[...]";
  7. var CIRCULAR_REPLACE_NODE = { result: "[Circular]" };
  8. var arr = [];
  9. var replacerStack = [];
  10. const encoder = new TextEncoder();
  11. function defaultOptions() {
  12. return {
  13. depthLimit: Number.MAX_SAFE_INTEGER,
  14. edgesLimit: Number.MAX_SAFE_INTEGER,
  15. };
  16. }
  17. function encodeString(str) {
  18. return encoder.encode(str);
  19. }
  20. // Regular stringify
  21. function serialize(obj, errorContext, replacer, spacer, options) {
  22. try {
  23. const str = JSON.stringify(obj, replacer, spacer);
  24. return encodeString(str);
  25. }
  26. catch (e) {
  27. // Fall back to more complex stringify if circular reference
  28. if (!e.message?.includes("Converting circular structure to JSON")) {
  29. console.warn(`[WARNING]: LangSmith received unserializable value.${errorContext ? `\nContext: ${errorContext}` : ""}`);
  30. return encodeString("[Unserializable]");
  31. }
  32. console.warn(`[WARNING]: LangSmith received circular JSON. This will decrease tracer performance. ${errorContext ? `\nContext: ${errorContext}` : ""}`);
  33. if (typeof options === "undefined") {
  34. options = defaultOptions();
  35. }
  36. decirc(obj, "", 0, [], undefined, 0, options);
  37. let res;
  38. try {
  39. if (replacerStack.length === 0) {
  40. res = JSON.stringify(obj, replacer, spacer);
  41. }
  42. else {
  43. res = JSON.stringify(obj, replaceGetterValues(replacer), spacer);
  44. }
  45. }
  46. catch (_) {
  47. return encodeString("[unable to serialize, circular reference is too complex to analyze]");
  48. }
  49. finally {
  50. while (arr.length !== 0) {
  51. const part = arr.pop();
  52. if (part.length === 4) {
  53. Object.defineProperty(part[0], part[1], part[3]);
  54. }
  55. else {
  56. part[0][part[1]] = part[2];
  57. }
  58. }
  59. }
  60. return encodeString(res);
  61. }
  62. }
  63. function setReplace(replace, val, k, parent) {
  64. var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k);
  65. if (propertyDescriptor.get !== undefined) {
  66. if (propertyDescriptor.configurable) {
  67. Object.defineProperty(parent, k, { value: replace });
  68. arr.push([parent, k, val, propertyDescriptor]);
  69. }
  70. else {
  71. replacerStack.push([val, k, replace]);
  72. }
  73. }
  74. else {
  75. parent[k] = replace;
  76. arr.push([parent, k, val]);
  77. }
  78. }
  79. function decirc(val, k, edgeIndex, stack, parent, depth, options) {
  80. depth += 1;
  81. var i;
  82. if (typeof val === "object" && val !== null) {
  83. for (i = 0; i < stack.length; i++) {
  84. if (stack[i] === val) {
  85. setReplace(CIRCULAR_REPLACE_NODE, val, k, parent);
  86. return;
  87. }
  88. }
  89. if (typeof options.depthLimit !== "undefined" &&
  90. depth > options.depthLimit) {
  91. setReplace(LIMIT_REPLACE_NODE, val, k, parent);
  92. return;
  93. }
  94. if (typeof options.edgesLimit !== "undefined" &&
  95. edgeIndex + 1 > options.edgesLimit) {
  96. setReplace(LIMIT_REPLACE_NODE, val, k, parent);
  97. return;
  98. }
  99. stack.push(val);
  100. // Optimize for Arrays. Big arrays could kill the performance otherwise!
  101. if (Array.isArray(val)) {
  102. for (i = 0; i < val.length; i++) {
  103. decirc(val[i], i, i, stack, val, depth, options);
  104. }
  105. }
  106. else {
  107. var keys = Object.keys(val);
  108. for (i = 0; i < keys.length; i++) {
  109. var key = keys[i];
  110. decirc(val[key], key, i, stack, val, depth, options);
  111. }
  112. }
  113. stack.pop();
  114. }
  115. }
  116. // Stable-stringify
  117. function compareFunction(a, b) {
  118. if (a < b) {
  119. return -1;
  120. }
  121. if (a > b) {
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. function deterministicStringify(obj, replacer, spacer, options) {
  127. if (typeof options === "undefined") {
  128. options = defaultOptions();
  129. }
  130. var tmp = deterministicDecirc(obj, "", 0, [], undefined, 0, options) || obj;
  131. var res;
  132. try {
  133. if (replacerStack.length === 0) {
  134. res = JSON.stringify(tmp, replacer, spacer);
  135. }
  136. else {
  137. res = JSON.stringify(tmp, replaceGetterValues(replacer), spacer);
  138. }
  139. }
  140. catch (_) {
  141. return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]");
  142. }
  143. finally {
  144. // Ensure that we restore the object as it was.
  145. while (arr.length !== 0) {
  146. var part = arr.pop();
  147. if (part.length === 4) {
  148. Object.defineProperty(part[0], part[1], part[3]);
  149. }
  150. else {
  151. part[0][part[1]] = part[2];
  152. }
  153. }
  154. }
  155. return res;
  156. }
  157. function deterministicDecirc(val, k, edgeIndex, stack, parent, depth, options) {
  158. depth += 1;
  159. var i;
  160. if (typeof val === "object" && val !== null) {
  161. for (i = 0; i < stack.length; i++) {
  162. if (stack[i] === val) {
  163. setReplace(CIRCULAR_REPLACE_NODE, val, k, parent);
  164. return;
  165. }
  166. }
  167. try {
  168. if (typeof val.toJSON === "function") {
  169. return;
  170. }
  171. }
  172. catch (_) {
  173. return;
  174. }
  175. if (typeof options.depthLimit !== "undefined" &&
  176. depth > options.depthLimit) {
  177. setReplace(LIMIT_REPLACE_NODE, val, k, parent);
  178. return;
  179. }
  180. if (typeof options.edgesLimit !== "undefined" &&
  181. edgeIndex + 1 > options.edgesLimit) {
  182. setReplace(LIMIT_REPLACE_NODE, val, k, parent);
  183. return;
  184. }
  185. stack.push(val);
  186. // Optimize for Arrays. Big arrays could kill the performance otherwise!
  187. if (Array.isArray(val)) {
  188. for (i = 0; i < val.length; i++) {
  189. deterministicDecirc(val[i], i, i, stack, val, depth, options);
  190. }
  191. }
  192. else {
  193. // Create a temporary object in the required way
  194. var tmp = {};
  195. var keys = Object.keys(val).sort(compareFunction);
  196. for (i = 0; i < keys.length; i++) {
  197. var key = keys[i];
  198. deterministicDecirc(val[key], key, i, stack, val, depth, options);
  199. tmp[key] = val[key];
  200. }
  201. if (typeof parent !== "undefined") {
  202. arr.push([parent, k, val]);
  203. parent[k] = tmp;
  204. }
  205. else {
  206. return tmp;
  207. }
  208. }
  209. stack.pop();
  210. }
  211. }
  212. // wraps replacer function to handle values we couldn't replace
  213. // and mark them as replaced value
  214. function replaceGetterValues(replacer) {
  215. replacer =
  216. typeof replacer !== "undefined"
  217. ? replacer
  218. : function (k, v) {
  219. return v;
  220. };
  221. return function (key, val) {
  222. if (replacerStack.length > 0) {
  223. for (var i = 0; i < replacerStack.length; i++) {
  224. var part = replacerStack[i];
  225. if (part[1] === key && part[0] === val) {
  226. val = part[2];
  227. replacerStack.splice(i, 1);
  228. break;
  229. }
  230. }
  231. }
  232. return replacer.call(this, key, val);
  233. };
  234. }