langgraph-ui-components 0.0.1 → 0.0.2

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.
Files changed (532) hide show
  1. package/dist/index.es.js +9 -27085
  2. package/dist/index.es.js.map +1 -1
  3. package/dist/index.es10.js +21 -0
  4. package/dist/index.es10.js.map +1 -0
  5. package/dist/index.es100.js +43 -0
  6. package/dist/index.es100.js.map +1 -0
  7. package/dist/index.es101.js +94 -0
  8. package/dist/index.es101.js.map +1 -0
  9. package/dist/index.es102.js +5 -0
  10. package/dist/index.es102.js.map +1 -0
  11. package/dist/index.es103.js +5 -0
  12. package/dist/index.es103.js.map +1 -0
  13. package/dist/index.es104.js +25 -0
  14. package/dist/index.es104.js.map +1 -0
  15. package/dist/index.es105.js +5 -0
  16. package/dist/index.es105.js.map +1 -0
  17. package/dist/index.es106.js +28 -0
  18. package/dist/index.es106.js.map +1 -0
  19. package/dist/index.es107.js +8 -0
  20. package/dist/index.es107.js.map +1 -0
  21. package/dist/index.es108.js +3 -0
  22. package/dist/index.es108.js.map +1 -0
  23. package/dist/index.es109.js +14 -0
  24. package/dist/index.es109.js.map +1 -0
  25. package/dist/index.es11.js +125 -0
  26. package/dist/index.es11.js.map +1 -0
  27. package/dist/index.es110.js +504 -0
  28. package/dist/index.es110.js.map +1 -0
  29. package/dist/index.es111.js +3 -0
  30. package/dist/index.es111.js.map +1 -0
  31. package/dist/index.es112.js +149 -0
  32. package/dist/index.es112.js.map +1 -0
  33. package/dist/index.es113.js +889 -0
  34. package/dist/index.es113.js.map +1 -0
  35. package/dist/index.es114.js +283 -0
  36. package/dist/index.es114.js.map +1 -0
  37. package/dist/index.es115.js +5 -0
  38. package/dist/index.es115.js.map +1 -0
  39. package/dist/index.es116.js +447 -0
  40. package/dist/index.es116.js.map +1 -0
  41. package/dist/index.es117.js +49 -0
  42. package/dist/index.es117.js.map +1 -0
  43. package/dist/index.es118.js +25 -0
  44. package/dist/index.es118.js.map +1 -0
  45. package/dist/index.es119.js +82 -0
  46. package/dist/index.es119.js.map +1 -0
  47. package/dist/index.es12.js +104 -0
  48. package/dist/index.es12.js.map +1 -0
  49. package/dist/index.es120.js +132 -0
  50. package/dist/index.es120.js.map +1 -0
  51. package/dist/index.es121.js +52 -0
  52. package/dist/index.es121.js.map +1 -0
  53. package/dist/index.es122.js +13 -0
  54. package/dist/index.es122.js.map +1 -0
  55. package/dist/index.es123.js +48 -0
  56. package/dist/index.es123.js.map +1 -0
  57. package/dist/index.es124.js +102 -0
  58. package/dist/index.es124.js.map +1 -0
  59. package/dist/index.es125.js +56 -0
  60. package/dist/index.es125.js.map +1 -0
  61. package/dist/index.es126.js +5 -0
  62. package/dist/index.es126.js.map +1 -0
  63. package/dist/index.es127.js +140 -0
  64. package/dist/index.es127.js.map +1 -0
  65. package/dist/index.es128.js +5 -0
  66. package/dist/index.es128.js.map +1 -0
  67. package/dist/index.es129.js +18 -0
  68. package/dist/index.es129.js.map +1 -0
  69. package/dist/index.es13.js +37 -0
  70. package/dist/index.es13.js.map +1 -0
  71. package/dist/index.es130.js +92 -0
  72. package/dist/index.es130.js.map +1 -0
  73. package/dist/index.es131.js +5 -0
  74. package/dist/index.es131.js.map +1 -0
  75. package/dist/index.es132.js +59 -0
  76. package/dist/index.es132.js.map +1 -0
  77. package/dist/index.es133.js +103 -0
  78. package/dist/index.es133.js.map +1 -0
  79. package/dist/index.es134.js +194 -0
  80. package/dist/index.es134.js.map +1 -0
  81. package/dist/index.es135.js +113 -0
  82. package/dist/index.es135.js.map +1 -0
  83. package/dist/index.es136.js +38 -0
  84. package/dist/index.es136.js.map +1 -0
  85. package/dist/index.es137.js +13 -0
  86. package/dist/index.es137.js.map +1 -0
  87. package/dist/index.es138.js +14 -0
  88. package/dist/index.es138.js.map +1 -0
  89. package/dist/index.es139.js +15 -0
  90. package/dist/index.es139.js.map +1 -0
  91. package/dist/index.es14.js +148 -0
  92. package/dist/index.es14.js.map +1 -0
  93. package/dist/index.es140.js +94 -0
  94. package/dist/index.es140.js.map +1 -0
  95. package/dist/index.es141.js +38 -0
  96. package/dist/index.es141.js.map +1 -0
  97. package/dist/index.es142.js +43 -0
  98. package/dist/index.es142.js.map +1 -0
  99. package/dist/index.es143.js +27 -0
  100. package/dist/index.es143.js.map +1 -0
  101. package/dist/index.es144.js +594 -0
  102. package/dist/index.es144.js.map +1 -0
  103. package/dist/index.es145.js +128 -0
  104. package/dist/index.es145.js.map +1 -0
  105. package/dist/index.es146.js +7 -0
  106. package/dist/index.es146.js.map +1 -0
  107. package/dist/index.es147.js +8 -0
  108. package/dist/index.es147.js.map +1 -0
  109. package/dist/index.es148.js +16 -0
  110. package/dist/index.es148.js.map +1 -0
  111. package/dist/index.es149.js +9 -0
  112. package/dist/index.es149.js.map +1 -0
  113. package/dist/index.es15.js +15 -0
  114. package/dist/index.es15.js.map +1 -0
  115. package/dist/index.es150.js +11 -0
  116. package/dist/index.es150.js.map +1 -0
  117. package/dist/index.es151.js +28 -0
  118. package/dist/index.es151.js.map +1 -0
  119. package/dist/index.es152.js +15 -0
  120. package/dist/index.es152.js.map +1 -0
  121. package/dist/index.es153.js +57 -0
  122. package/dist/index.es153.js.map +1 -0
  123. package/dist/index.es154.js +67 -0
  124. package/dist/index.es154.js.map +1 -0
  125. package/dist/index.es155.js +25 -0
  126. package/dist/index.es155.js.map +1 -0
  127. package/dist/index.es156.js +9 -0
  128. package/dist/index.es156.js.map +1 -0
  129. package/dist/index.es157.js +352 -0
  130. package/dist/index.es157.js.map +1 -0
  131. package/dist/index.es158.js +10 -0
  132. package/dist/index.es158.js.map +1 -0
  133. package/dist/index.es159.js +106 -0
  134. package/dist/index.es159.js.map +1 -0
  135. package/dist/index.es16.js +8 -0
  136. package/dist/index.es16.js.map +1 -0
  137. package/dist/index.es160.js +122 -0
  138. package/dist/index.es160.js.map +1 -0
  139. package/dist/index.es161.js +139 -0
  140. package/dist/index.es161.js.map +1 -0
  141. package/dist/index.es162.js +118 -0
  142. package/dist/index.es162.js.map +1 -0
  143. package/dist/index.es163.js +9 -0
  144. package/dist/index.es163.js.map +1 -0
  145. package/dist/index.es164.js +145 -0
  146. package/dist/index.es164.js.map +1 -0
  147. package/dist/index.es165.js +5 -0
  148. package/dist/index.es165.js.map +1 -0
  149. package/dist/index.es166.js +3685 -0
  150. package/dist/index.es166.js.map +1 -0
  151. package/dist/index.es167.js +345 -0
  152. package/dist/index.es167.js.map +1 -0
  153. package/dist/index.es168.js +112 -0
  154. package/dist/index.es168.js.map +1 -0
  155. package/dist/index.es169.js +136 -0
  156. package/dist/index.es169.js.map +1 -0
  157. package/dist/index.es17.js +36 -0
  158. package/dist/index.es17.js.map +1 -0
  159. package/dist/index.es170.js +18 -0
  160. package/dist/index.es170.js.map +1 -0
  161. package/dist/index.es171.js +155 -0
  162. package/dist/index.es171.js.map +1 -0
  163. package/dist/index.es172.js +18 -0
  164. package/dist/index.es172.js.map +1 -0
  165. package/dist/index.es173.js +11 -0
  166. package/dist/index.es173.js.map +1 -0
  167. package/dist/index.es174.js +24 -0
  168. package/dist/index.es174.js.map +1 -0
  169. package/dist/index.es175.js +93 -0
  170. package/dist/index.es175.js.map +1 -0
  171. package/dist/index.es176.js +27 -0
  172. package/dist/index.es176.js.map +1 -0
  173. package/dist/index.es177.js +9 -0
  174. package/dist/index.es177.js.map +1 -0
  175. package/dist/index.es178.js +7 -0
  176. package/dist/index.es178.js.map +1 -0
  177. package/dist/index.es179.js +21 -0
  178. package/dist/index.es179.js.map +1 -0
  179. package/dist/index.es18.js +126 -0
  180. package/dist/index.es18.js.map +1 -0
  181. package/dist/index.es180.js +35 -0
  182. package/dist/index.es180.js.map +1 -0
  183. package/dist/index.es181.js +9 -0
  184. package/dist/index.es181.js.map +1 -0
  185. package/dist/index.es182.js +109 -0
  186. package/dist/index.es182.js.map +1 -0
  187. package/dist/index.es183.js +16 -0
  188. package/dist/index.es183.js.map +1 -0
  189. package/dist/index.es184.js +21 -0
  190. package/dist/index.es184.js.map +1 -0
  191. package/dist/index.es185.js +34 -0
  192. package/dist/index.es185.js.map +1 -0
  193. package/dist/index.es186.js +7 -0
  194. package/dist/index.es186.js.map +1 -0
  195. package/dist/index.es187.js +8 -0
  196. package/dist/index.es187.js.map +1 -0
  197. package/dist/index.es188.js +8 -0
  198. package/dist/index.es188.js.map +1 -0
  199. package/dist/index.es189.js +40 -0
  200. package/dist/index.es189.js.map +1 -0
  201. package/dist/index.es19.js +41 -0
  202. package/dist/index.es19.js.map +1 -0
  203. package/dist/index.es190.js +11 -0
  204. package/dist/index.es190.js.map +1 -0
  205. package/dist/index.es191.js +9 -0
  206. package/dist/index.es191.js.map +1 -0
  207. package/dist/index.es192.js +10 -0
  208. package/dist/index.es192.js.map +1 -0
  209. package/dist/index.es193.js +45 -0
  210. package/dist/index.es193.js.map +1 -0
  211. package/dist/index.es194.js +16 -0
  212. package/dist/index.es194.js.map +1 -0
  213. package/dist/index.es195.js +245 -0
  214. package/dist/index.es195.js.map +1 -0
  215. package/dist/index.es196.js +54 -0
  216. package/dist/index.es196.js.map +1 -0
  217. package/dist/index.es197.js +38 -0
  218. package/dist/index.es197.js.map +1 -0
  219. package/dist/index.es198.js +16 -0
  220. package/dist/index.es198.js.map +1 -0
  221. package/dist/index.es199.js +11 -0
  222. package/dist/index.es199.js.map +1 -0
  223. package/dist/index.es2.js +142 -0
  224. package/dist/index.es2.js.map +1 -0
  225. package/dist/index.es20.js +46 -0
  226. package/dist/index.es20.js.map +1 -0
  227. package/dist/index.es200.js +10 -0
  228. package/dist/index.es200.js.map +1 -0
  229. package/dist/index.es201.js +68 -0
  230. package/dist/index.es201.js.map +1 -0
  231. package/dist/index.es202.js +44 -0
  232. package/dist/index.es202.js.map +1 -0
  233. package/dist/index.es203.js +35 -0
  234. package/dist/index.es203.js.map +1 -0
  235. package/dist/index.es204.js +65 -0
  236. package/dist/index.es204.js.map +1 -0
  237. package/dist/index.es205.js +18 -0
  238. package/dist/index.es205.js.map +1 -0
  239. package/dist/index.es206.js +26 -0
  240. package/dist/index.es206.js.map +1 -0
  241. package/dist/index.es207.js +8 -0
  242. package/dist/index.es207.js.map +1 -0
  243. package/dist/index.es208.js +20 -0
  244. package/dist/index.es208.js.map +1 -0
  245. package/dist/index.es209.js +36 -0
  246. package/dist/index.es209.js.map +1 -0
  247. package/dist/index.es21.js +21 -0
  248. package/dist/index.es21.js.map +1 -0
  249. package/dist/index.es210.js +8 -0
  250. package/dist/index.es210.js.map +1 -0
  251. package/dist/index.es211.js +8 -0
  252. package/dist/index.es211.js.map +1 -0
  253. package/dist/index.es212.js +8 -0
  254. package/dist/index.es212.js.map +1 -0
  255. package/dist/index.es213.js +11 -0
  256. package/dist/index.es213.js.map +1 -0
  257. package/dist/index.es214.js +95 -0
  258. package/dist/index.es214.js.map +1 -0
  259. package/dist/index.es215.js +21 -0
  260. package/dist/index.es215.js.map +1 -0
  261. package/dist/index.es216.js +765 -0
  262. package/dist/index.es216.js.map +1 -0
  263. package/dist/index.es217.js +69 -0
  264. package/dist/index.es217.js.map +1 -0
  265. package/dist/index.es218.js +177 -0
  266. package/dist/index.es218.js.map +1 -0
  267. package/dist/index.es219.js +41 -0
  268. package/dist/index.es219.js.map +1 -0
  269. package/dist/index.es22.js +76 -0
  270. package/dist/index.es22.js.map +1 -0
  271. package/dist/index.es220.js +140 -0
  272. package/dist/index.es220.js.map +1 -0
  273. package/dist/index.es221.js +96 -0
  274. package/dist/index.es221.js.map +1 -0
  275. package/dist/index.es222.js +36 -0
  276. package/dist/index.es222.js.map +1 -0
  277. package/dist/index.es223.js +288 -0
  278. package/dist/index.es223.js.map +1 -0
  279. package/dist/index.es224.js +29 -0
  280. package/dist/index.es224.js.map +1 -0
  281. package/dist/index.es225.js +27 -0
  282. package/dist/index.es225.js.map +1 -0
  283. package/dist/index.es226.js +18 -0
  284. package/dist/index.es226.js.map +1 -0
  285. package/dist/index.es227.js +18 -0
  286. package/dist/index.es227.js.map +1 -0
  287. package/dist/index.es228.js +29 -0
  288. package/dist/index.es228.js.map +1 -0
  289. package/dist/index.es229.js +49 -0
  290. package/dist/index.es229.js.map +1 -0
  291. package/dist/index.es23.js +34 -0
  292. package/dist/index.es23.js.map +1 -0
  293. package/dist/index.es230.js +15 -0
  294. package/dist/index.es230.js.map +1 -0
  295. package/dist/index.es231.js +15 -0
  296. package/dist/index.es231.js.map +1 -0
  297. package/dist/index.es232.js +15 -0
  298. package/dist/index.es232.js.map +1 -0
  299. package/dist/index.es233.js +18 -0
  300. package/dist/index.es233.js.map +1 -0
  301. package/dist/index.es234.js +15 -0
  302. package/dist/index.es234.js.map +1 -0
  303. package/dist/index.es235.js +15 -0
  304. package/dist/index.es235.js.map +1 -0
  305. package/dist/index.es236.js +15 -0
  306. package/dist/index.es236.js.map +1 -0
  307. package/dist/index.es237.js +19 -0
  308. package/dist/index.es237.js.map +1 -0
  309. package/dist/index.es238.js +15 -0
  310. package/dist/index.es238.js.map +1 -0
  311. package/dist/index.es239.js +15 -0
  312. package/dist/index.es239.js.map +1 -0
  313. package/dist/index.es24.js +97 -0
  314. package/dist/index.es24.js.map +1 -0
  315. package/dist/index.es240.js +15 -0
  316. package/dist/index.es240.js.map +1 -0
  317. package/dist/index.es241.js +15 -0
  318. package/dist/index.es241.js.map +1 -0
  319. package/dist/index.es242.js +15 -0
  320. package/dist/index.es242.js.map +1 -0
  321. package/dist/index.es243.js +15 -0
  322. package/dist/index.es243.js.map +1 -0
  323. package/dist/index.es244.js +15 -0
  324. package/dist/index.es244.js.map +1 -0
  325. package/dist/index.es245.js +15 -0
  326. package/dist/index.es245.js.map +1 -0
  327. package/dist/index.es246.js +60 -0
  328. package/dist/index.es246.js.map +1 -0
  329. package/dist/index.es247.js +53 -0
  330. package/dist/index.es247.js.map +1 -0
  331. package/dist/index.es248.js +123 -0
  332. package/dist/index.es248.js.map +1 -0
  333. package/dist/index.es249.js +388 -0
  334. package/dist/index.es249.js.map +1 -0
  335. package/dist/index.es25.js +74 -0
  336. package/dist/index.es25.js.map +1 -0
  337. package/dist/index.es250.js +22 -0
  338. package/dist/index.es250.js.map +1 -0
  339. package/dist/index.es251.js +15 -0
  340. package/dist/index.es251.js.map +1 -0
  341. package/dist/index.es252.js +35 -0
  342. package/dist/index.es252.js.map +1 -0
  343. package/dist/index.es253.js +35 -0
  344. package/dist/index.es253.js.map +1 -0
  345. package/dist/index.es254.js +66 -0
  346. package/dist/index.es254.js.map +1 -0
  347. package/dist/index.es255.js +21 -0
  348. package/dist/index.es255.js.map +1 -0
  349. package/dist/index.es256.js +81 -0
  350. package/dist/index.es256.js.map +1 -0
  351. package/dist/index.es257.js +15 -0
  352. package/dist/index.es257.js.map +1 -0
  353. package/dist/index.es258.js +15 -0
  354. package/dist/index.es258.js.map +1 -0
  355. package/dist/index.es259.js +19 -0
  356. package/dist/index.es259.js.map +1 -0
  357. package/dist/index.es26.js +180 -0
  358. package/dist/index.es26.js.map +1 -0
  359. package/dist/index.es260.js +56 -0
  360. package/dist/index.es260.js.map +1 -0
  361. package/dist/index.es261.js +170 -0
  362. package/dist/index.es261.js.map +1 -0
  363. package/dist/index.es262.js +5 -0
  364. package/dist/index.es262.js.map +1 -0
  365. package/dist/index.es263.js +14 -0
  366. package/dist/index.es263.js.map +1 -0
  367. package/dist/index.es264.js +23 -0
  368. package/dist/index.es264.js.map +1 -0
  369. package/dist/index.es265.js +42 -0
  370. package/dist/index.es265.js.map +1 -0
  371. package/dist/index.es27.js +16 -0
  372. package/dist/index.es27.js.map +1 -0
  373. package/dist/index.es28.js +441 -0
  374. package/dist/index.es28.js.map +1 -0
  375. package/dist/index.es29.js +122 -0
  376. package/dist/index.es29.js.map +1 -0
  377. package/dist/index.es3.js +30 -0
  378. package/dist/index.es3.js.map +1 -0
  379. package/dist/index.es30.js +77 -0
  380. package/dist/index.es30.js.map +1 -0
  381. package/dist/index.es31.js +15 -0
  382. package/dist/index.es31.js.map +1 -0
  383. package/dist/index.es32.js +262 -0
  384. package/dist/index.es32.js.map +1 -0
  385. package/dist/index.es33.js +47 -0
  386. package/dist/index.es33.js.map +1 -0
  387. package/dist/index.es34.js +215 -0
  388. package/dist/index.es34.js.map +1 -0
  389. package/dist/index.es35.js +71 -0
  390. package/dist/index.es35.js.map +1 -0
  391. package/dist/index.es36.js +146 -0
  392. package/dist/index.es36.js.map +1 -0
  393. package/dist/index.es37.js +28 -0
  394. package/dist/index.es37.js.map +1 -0
  395. package/dist/index.es38.js +228 -0
  396. package/dist/index.es38.js.map +1 -0
  397. package/dist/index.es39.js +13 -0
  398. package/dist/index.es39.js.map +1 -0
  399. package/dist/index.es4.js +30 -0
  400. package/dist/index.es4.js.map +1 -0
  401. package/dist/index.es40.js +1203 -0
  402. package/dist/index.es40.js.map +1 -0
  403. package/dist/index.es41.js +12 -0
  404. package/dist/index.es41.js.map +1 -0
  405. package/dist/index.es42.js +151 -0
  406. package/dist/index.es42.js.map +1 -0
  407. package/dist/index.es43.js +18 -0
  408. package/dist/index.es43.js.map +1 -0
  409. package/dist/index.es44.js +11 -0
  410. package/dist/index.es44.js.map +1 -0
  411. package/dist/index.es45.js +97 -0
  412. package/dist/index.es45.js.map +1 -0
  413. package/dist/index.es46.js +7 -0
  414. package/dist/index.es46.js.map +1 -0
  415. package/dist/index.es47.js +39 -0
  416. package/dist/index.es47.js.map +1 -0
  417. package/dist/index.es48.js +75 -0
  418. package/dist/index.es48.js.map +1 -0
  419. package/dist/index.es49.js +41 -0
  420. package/dist/index.es49.js.map +1 -0
  421. package/dist/index.es5.js +26 -0
  422. package/dist/index.es5.js.map +1 -0
  423. package/dist/index.es50.js +309 -0
  424. package/dist/index.es50.js.map +1 -0
  425. package/dist/index.es51.js +12 -0
  426. package/dist/index.es51.js.map +1 -0
  427. package/dist/index.es52.js +88 -0
  428. package/dist/index.es52.js.map +1 -0
  429. package/dist/index.es53.js +11 -0
  430. package/dist/index.es53.js.map +1 -0
  431. package/dist/index.es54.js +18 -0
  432. package/dist/index.es54.js.map +1 -0
  433. package/dist/index.es55.js +144 -0
  434. package/dist/index.es55.js.map +1 -0
  435. package/dist/index.es56.js +303 -0
  436. package/dist/index.es56.js.map +1 -0
  437. package/dist/index.es57.js +69 -0
  438. package/dist/index.es57.js.map +1 -0
  439. package/dist/index.es58.js +216 -0
  440. package/dist/index.es58.js.map +1 -0
  441. package/dist/index.es59.js +37 -0
  442. package/dist/index.es59.js.map +1 -0
  443. package/dist/index.es6.js +122 -0
  444. package/dist/index.es6.js.map +1 -0
  445. package/dist/index.es60.js +12 -0
  446. package/dist/index.es60.js.map +1 -0
  447. package/dist/index.es61.js +15 -0
  448. package/dist/index.es61.js.map +1 -0
  449. package/dist/index.es62.js +190 -0
  450. package/dist/index.es62.js.map +1 -0
  451. package/dist/index.es63.js +19 -0
  452. package/dist/index.es63.js.map +1 -0
  453. package/dist/index.es64.js +1403 -0
  454. package/dist/index.es64.js.map +1 -0
  455. package/dist/index.es65.js +12 -0
  456. package/dist/index.es65.js.map +1 -0
  457. package/dist/index.es66.js +11 -0
  458. package/dist/index.es66.js.map +1 -0
  459. package/dist/index.es67.js +9 -0
  460. package/dist/index.es67.js.map +1 -0
  461. package/dist/index.es68.js +8 -0
  462. package/dist/index.es68.js.map +1 -0
  463. package/dist/index.es69.js +8 -0
  464. package/dist/index.es69.js.map +1 -0
  465. package/dist/index.es7.js +19 -0
  466. package/dist/index.es7.js.map +1 -0
  467. package/dist/index.es70.js +15 -0
  468. package/dist/index.es70.js.map +1 -0
  469. package/dist/index.es71.js +26 -0
  470. package/dist/index.es71.js.map +1 -0
  471. package/dist/index.es72.js +37 -0
  472. package/dist/index.es72.js.map +1 -0
  473. package/dist/index.es73.js +235 -0
  474. package/dist/index.es73.js.map +1 -0
  475. package/dist/index.es74.js +71 -0
  476. package/dist/index.es74.js.map +1 -0
  477. package/dist/index.es75.js +72 -0
  478. package/dist/index.es75.js.map +1 -0
  479. package/dist/index.es76.js +60 -0
  480. package/dist/index.es76.js.map +1 -0
  481. package/dist/index.es77.js +16 -0
  482. package/dist/index.es77.js.map +1 -0
  483. package/dist/index.es78.js +140 -0
  484. package/dist/index.es78.js.map +1 -0
  485. package/dist/index.es79.js +24 -0
  486. package/dist/index.es79.js.map +1 -0
  487. package/dist/index.es8.js +61 -0
  488. package/dist/index.es8.js.map +1 -0
  489. package/dist/index.es80.js +185 -0
  490. package/dist/index.es80.js.map +1 -0
  491. package/dist/index.es81.js +244 -0
  492. package/dist/index.es81.js.map +1 -0
  493. package/dist/index.es82.js +358 -0
  494. package/dist/index.es82.js.map +1 -0
  495. package/dist/index.es83.js +140 -0
  496. package/dist/index.es83.js.map +1 -0
  497. package/dist/index.es84.js +73 -0
  498. package/dist/index.es84.js.map +1 -0
  499. package/dist/index.es85.js +40 -0
  500. package/dist/index.es85.js.map +1 -0
  501. package/dist/index.es86.js +40 -0
  502. package/dist/index.es86.js.map +1 -0
  503. package/dist/index.es87.js +169 -0
  504. package/dist/index.es87.js.map +1 -0
  505. package/dist/index.es88.js +206 -0
  506. package/dist/index.es88.js.map +1 -0
  507. package/dist/index.es89.js +17 -0
  508. package/dist/index.es89.js.map +1 -0
  509. package/dist/index.es9.js +109 -0
  510. package/dist/index.es9.js.map +1 -0
  511. package/dist/index.es90.js +33 -0
  512. package/dist/index.es90.js.map +1 -0
  513. package/dist/index.es91.js +37 -0
  514. package/dist/index.es91.js.map +1 -0
  515. package/dist/index.es92.js +3160 -0
  516. package/dist/index.es92.js.map +1 -0
  517. package/dist/index.es93.js +84 -0
  518. package/dist/index.es93.js.map +1 -0
  519. package/dist/index.es94.js +5 -0
  520. package/dist/index.es94.js.map +1 -0
  521. package/dist/index.es95.js +13 -0
  522. package/dist/index.es95.js.map +1 -0
  523. package/dist/index.es96.js +278 -0
  524. package/dist/index.es96.js.map +1 -0
  525. package/dist/index.es97.js +5 -0
  526. package/dist/index.es97.js.map +1 -0
  527. package/dist/index.es98.js +167 -0
  528. package/dist/index.es98.js.map +1 -0
  529. package/dist/index.es99.js +54 -0
  530. package/dist/index.es99.js.map +1 -0
  531. package/dist/styles.css +1 -1
  532. package/package.json +12 -10
@@ -0,0 +1,3685 @@
1
+ import * as uuid from "uuid";
2
+ import { LangSmithToOTELTranslator } from "./index.es167.js";
3
+ import { getOTELTrace, getOTELContext, getDefaultOTLPTracerComponents } from "./index.es168.js";
4
+ import { AsyncCaller } from "./index.es169.js";
5
+ import { isLangChainMessage, convertLangChainMessageToExample } from "./index.es170.js";
6
+ import { getEnvironmentVariable, getLangSmithEnvironmentVariable, getOtelEnabled, getLangSmithEnvVarsMetadata, getEnv, getRuntimeEnvironment } from "./index.es171.js";
7
+ import { __version__ } from "./index.es150.js";
8
+ import { assertUuid } from "./index.es172.js";
9
+ import { warnOnce } from "./index.es173.js";
10
+ import { parsePromptIdentifier } from "./index.es174.js";
11
+ import { raiseForStatus, isLangSmithNotFoundError } from "./index.es175.js";
12
+ import { _getFetchImplementation, _globalFetchImplementationIsNodeFetch } from "./index.es176.js";
13
+ import { serialize } from "./index.es112.js";
14
+ function mergeRuntimeEnvIntoRun(run, cachedEnvVars, omitTracedRuntimeInfo) {
15
+ if (omitTracedRuntimeInfo) {
16
+ return run;
17
+ }
18
+ const runtimeEnv = getRuntimeEnvironment();
19
+ const envVars = cachedEnvVars ?? getLangSmithEnvVarsMetadata();
20
+ const extra = run.extra ?? {};
21
+ const metadata = extra.metadata;
22
+ run.extra = {
23
+ ...extra,
24
+ runtime: {
25
+ ...runtimeEnv,
26
+ ...extra?.runtime
27
+ },
28
+ metadata: {
29
+ ...envVars,
30
+ ...envVars.revision_id || "revision_id" in run && run.revision_id ? {
31
+ revision_id: ("revision_id" in run ? run.revision_id : void 0) ?? envVars.revision_id
32
+ } : {},
33
+ ...metadata
34
+ }
35
+ };
36
+ return run;
37
+ }
38
+ const getTracingSamplingRate = (configRate) => {
39
+ const samplingRateStr = configRate?.toString() ?? getLangSmithEnvironmentVariable("TRACING_SAMPLING_RATE");
40
+ if (samplingRateStr === void 0) {
41
+ return void 0;
42
+ }
43
+ const samplingRate = parseFloat(samplingRateStr);
44
+ if (samplingRate < 0 || samplingRate > 1) {
45
+ throw new Error(`LANGSMITH_TRACING_SAMPLING_RATE must be between 0 and 1 if set. Got: ${samplingRate}`);
46
+ }
47
+ return samplingRate;
48
+ };
49
+ const isLocalhost = (url) => {
50
+ const strippedUrl = url.replace("http://", "").replace("https://", "");
51
+ const hostname = strippedUrl.split("/")[0].split(":")[0];
52
+ return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1";
53
+ };
54
+ async function toArray(iterable) {
55
+ const result = [];
56
+ for await (const item of iterable) {
57
+ result.push(item);
58
+ }
59
+ return result;
60
+ }
61
+ function trimQuotes(str) {
62
+ if (str === void 0) {
63
+ return void 0;
64
+ }
65
+ return str.trim().replace(/^"(.*)"$/, "$1").replace(/^'(.*)'$/, "$1");
66
+ }
67
+ const handle429 = async (response) => {
68
+ if (response?.status === 429) {
69
+ const retryAfter = parseInt(response.headers.get("retry-after") ?? "10", 10) * 1e3;
70
+ if (retryAfter > 0) {
71
+ await new Promise((resolve) => setTimeout(resolve, retryAfter));
72
+ return true;
73
+ }
74
+ }
75
+ return false;
76
+ };
77
+ function _formatFeedbackScore(score) {
78
+ if (typeof score === "number") {
79
+ return Number(score.toFixed(4));
80
+ }
81
+ return score;
82
+ }
83
+ const DEFAULT_UNCOMPRESSED_BATCH_SIZE_LIMIT_BYTES = 24 * 1024 * 1024;
84
+ const DEFAULT_MAX_SIZE_BYTES = 1024 * 1024 * 1024;
85
+ const SERVER_INFO_REQUEST_TIMEOUT_MS = 1e4;
86
+ const DEFAULT_BATCH_SIZE_LIMIT = 100;
87
+ const DEFAULT_API_URL = "https://api.smith.langchain.com";
88
+ class AutoBatchQueue {
89
+ constructor(maxSizeBytes) {
90
+ Object.defineProperty(this, "items", {
91
+ enumerable: true,
92
+ configurable: true,
93
+ writable: true,
94
+ value: []
95
+ });
96
+ Object.defineProperty(this, "sizeBytes", {
97
+ enumerable: true,
98
+ configurable: true,
99
+ writable: true,
100
+ value: 0
101
+ });
102
+ Object.defineProperty(this, "maxSizeBytes", {
103
+ enumerable: true,
104
+ configurable: true,
105
+ writable: true,
106
+ value: void 0
107
+ });
108
+ this.maxSizeBytes = maxSizeBytes ?? DEFAULT_MAX_SIZE_BYTES;
109
+ }
110
+ peek() {
111
+ return this.items[0];
112
+ }
113
+ push(item) {
114
+ let itemPromiseResolve;
115
+ const itemPromise = new Promise((resolve) => {
116
+ itemPromiseResolve = resolve;
117
+ });
118
+ const size = serialize(item.item, `Serializing run with id: ${item.item.id}`).length;
119
+ if (this.sizeBytes + size > this.maxSizeBytes && this.items.length > 0) {
120
+ console.warn(`AutoBatchQueue size limit (${this.maxSizeBytes} bytes) exceeded. Dropping run with id: ${item.item.id}. Current queue size: ${this.sizeBytes} bytes, attempted addition: ${size} bytes.`);
121
+ itemPromiseResolve();
122
+ return itemPromise;
123
+ }
124
+ this.items.push({
125
+ action: item.action,
126
+ payload: item.item,
127
+ otelContext: item.otelContext,
128
+ apiKey: item.apiKey,
129
+ apiUrl: item.apiUrl,
130
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
131
+ itemPromiseResolve,
132
+ itemPromise,
133
+ size
134
+ });
135
+ this.sizeBytes += size;
136
+ return itemPromise;
137
+ }
138
+ pop({ upToSizeBytes, upToSize }) {
139
+ if (upToSizeBytes < 1) {
140
+ throw new Error("Number of bytes to pop off may not be less than 1.");
141
+ }
142
+ const popped = [];
143
+ let poppedSizeBytes = 0;
144
+ while (poppedSizeBytes + (this.peek()?.size ?? 0) < upToSizeBytes && this.items.length > 0 && popped.length < upToSize) {
145
+ const item = this.items.shift();
146
+ if (item) {
147
+ popped.push(item);
148
+ poppedSizeBytes += item.size;
149
+ this.sizeBytes -= item.size;
150
+ }
151
+ }
152
+ if (popped.length === 0 && this.items.length > 0) {
153
+ const item = this.items.shift();
154
+ popped.push(item);
155
+ poppedSizeBytes += item.size;
156
+ this.sizeBytes -= item.size;
157
+ }
158
+ return [
159
+ popped.map((it) => ({
160
+ action: it.action,
161
+ item: it.payload,
162
+ otelContext: it.otelContext,
163
+ apiKey: it.apiKey,
164
+ apiUrl: it.apiUrl,
165
+ size: it.size
166
+ })),
167
+ () => popped.forEach((it) => it.itemPromiseResolve())
168
+ ];
169
+ }
170
+ }
171
+ class Client {
172
+ get _fetch() {
173
+ return this.fetchImplementation || _getFetchImplementation(this.debug);
174
+ }
175
+ constructor(config = {}) {
176
+ Object.defineProperty(this, "apiKey", {
177
+ enumerable: true,
178
+ configurable: true,
179
+ writable: true,
180
+ value: void 0
181
+ });
182
+ Object.defineProperty(this, "apiUrl", {
183
+ enumerable: true,
184
+ configurable: true,
185
+ writable: true,
186
+ value: void 0
187
+ });
188
+ Object.defineProperty(this, "webUrl", {
189
+ enumerable: true,
190
+ configurable: true,
191
+ writable: true,
192
+ value: void 0
193
+ });
194
+ Object.defineProperty(this, "workspaceId", {
195
+ enumerable: true,
196
+ configurable: true,
197
+ writable: true,
198
+ value: void 0
199
+ });
200
+ Object.defineProperty(this, "caller", {
201
+ enumerable: true,
202
+ configurable: true,
203
+ writable: true,
204
+ value: void 0
205
+ });
206
+ Object.defineProperty(this, "batchIngestCaller", {
207
+ enumerable: true,
208
+ configurable: true,
209
+ writable: true,
210
+ value: void 0
211
+ });
212
+ Object.defineProperty(this, "timeout_ms", {
213
+ enumerable: true,
214
+ configurable: true,
215
+ writable: true,
216
+ value: void 0
217
+ });
218
+ Object.defineProperty(this, "_tenantId", {
219
+ enumerable: true,
220
+ configurable: true,
221
+ writable: true,
222
+ value: null
223
+ });
224
+ Object.defineProperty(this, "hideInputs", {
225
+ enumerable: true,
226
+ configurable: true,
227
+ writable: true,
228
+ value: void 0
229
+ });
230
+ Object.defineProperty(this, "hideOutputs", {
231
+ enumerable: true,
232
+ configurable: true,
233
+ writable: true,
234
+ value: void 0
235
+ });
236
+ Object.defineProperty(this, "omitTracedRuntimeInfo", {
237
+ enumerable: true,
238
+ configurable: true,
239
+ writable: true,
240
+ value: void 0
241
+ });
242
+ Object.defineProperty(this, "tracingSampleRate", {
243
+ enumerable: true,
244
+ configurable: true,
245
+ writable: true,
246
+ value: void 0
247
+ });
248
+ Object.defineProperty(this, "filteredPostUuids", {
249
+ enumerable: true,
250
+ configurable: true,
251
+ writable: true,
252
+ value: /* @__PURE__ */ new Set()
253
+ });
254
+ Object.defineProperty(this, "autoBatchTracing", {
255
+ enumerable: true,
256
+ configurable: true,
257
+ writable: true,
258
+ value: true
259
+ });
260
+ Object.defineProperty(this, "autoBatchQueue", {
261
+ enumerable: true,
262
+ configurable: true,
263
+ writable: true,
264
+ value: void 0
265
+ });
266
+ Object.defineProperty(this, "autoBatchTimeout", {
267
+ enumerable: true,
268
+ configurable: true,
269
+ writable: true,
270
+ value: void 0
271
+ });
272
+ Object.defineProperty(this, "autoBatchAggregationDelayMs", {
273
+ enumerable: true,
274
+ configurable: true,
275
+ writable: true,
276
+ value: 250
277
+ });
278
+ Object.defineProperty(this, "batchSizeBytesLimit", {
279
+ enumerable: true,
280
+ configurable: true,
281
+ writable: true,
282
+ value: void 0
283
+ });
284
+ Object.defineProperty(this, "batchSizeLimit", {
285
+ enumerable: true,
286
+ configurable: true,
287
+ writable: true,
288
+ value: void 0
289
+ });
290
+ Object.defineProperty(this, "fetchOptions", {
291
+ enumerable: true,
292
+ configurable: true,
293
+ writable: true,
294
+ value: void 0
295
+ });
296
+ Object.defineProperty(this, "settings", {
297
+ enumerable: true,
298
+ configurable: true,
299
+ writable: true,
300
+ value: void 0
301
+ });
302
+ Object.defineProperty(this, "blockOnRootRunFinalization", {
303
+ enumerable: true,
304
+ configurable: true,
305
+ writable: true,
306
+ value: getEnvironmentVariable("LANGSMITH_TRACING_BACKGROUND") === "false"
307
+ });
308
+ Object.defineProperty(this, "traceBatchConcurrency", {
309
+ enumerable: true,
310
+ configurable: true,
311
+ writable: true,
312
+ value: 5
313
+ });
314
+ Object.defineProperty(this, "_serverInfo", {
315
+ enumerable: true,
316
+ configurable: true,
317
+ writable: true,
318
+ value: void 0
319
+ });
320
+ Object.defineProperty(this, "_getServerInfoPromise", {
321
+ enumerable: true,
322
+ configurable: true,
323
+ writable: true,
324
+ value: void 0
325
+ });
326
+ Object.defineProperty(this, "manualFlushMode", {
327
+ enumerable: true,
328
+ configurable: true,
329
+ writable: true,
330
+ value: false
331
+ });
332
+ Object.defineProperty(this, "langSmithToOTELTranslator", {
333
+ enumerable: true,
334
+ configurable: true,
335
+ writable: true,
336
+ value: void 0
337
+ });
338
+ Object.defineProperty(this, "fetchImplementation", {
339
+ enumerable: true,
340
+ configurable: true,
341
+ writable: true,
342
+ value: void 0
343
+ });
344
+ Object.defineProperty(this, "cachedLSEnvVarsForMetadata", {
345
+ enumerable: true,
346
+ configurable: true,
347
+ writable: true,
348
+ value: void 0
349
+ });
350
+ Object.defineProperty(this, "multipartStreamingDisabled", {
351
+ enumerable: true,
352
+ configurable: true,
353
+ writable: true,
354
+ value: false
355
+ });
356
+ Object.defineProperty(this, "_multipartDisabled", {
357
+ enumerable: true,
358
+ configurable: true,
359
+ writable: true,
360
+ value: false
361
+ });
362
+ Object.defineProperty(this, "debug", {
363
+ enumerable: true,
364
+ configurable: true,
365
+ writable: true,
366
+ value: getEnvironmentVariable("LANGSMITH_DEBUG") === "true"
367
+ });
368
+ const defaultConfig = Client.getDefaultClientConfig();
369
+ this.tracingSampleRate = getTracingSamplingRate(config.tracingSamplingRate);
370
+ this.apiUrl = trimQuotes(config.apiUrl ?? defaultConfig.apiUrl) ?? "";
371
+ if (this.apiUrl.endsWith("/")) {
372
+ this.apiUrl = this.apiUrl.slice(0, -1);
373
+ }
374
+ this.apiKey = trimQuotes(config.apiKey ?? defaultConfig.apiKey);
375
+ this.webUrl = trimQuotes(config.webUrl ?? defaultConfig.webUrl);
376
+ if (this.webUrl?.endsWith("/")) {
377
+ this.webUrl = this.webUrl.slice(0, -1);
378
+ }
379
+ this.workspaceId = trimQuotes(config.workspaceId ?? getLangSmithEnvironmentVariable("WORKSPACE_ID"));
380
+ this.timeout_ms = config.timeout_ms ?? 9e4;
381
+ this.caller = new AsyncCaller({
382
+ ...config.callerOptions ?? {},
383
+ maxRetries: 4,
384
+ debug: config.debug ?? this.debug
385
+ });
386
+ this.traceBatchConcurrency = config.traceBatchConcurrency ?? this.traceBatchConcurrency;
387
+ if (this.traceBatchConcurrency < 1) {
388
+ throw new Error("Trace batch concurrency must be positive.");
389
+ }
390
+ this.debug = config.debug ?? this.debug;
391
+ this.fetchImplementation = config.fetchImplementation;
392
+ const maxMemory = config.maxIngestMemoryBytes ?? DEFAULT_MAX_SIZE_BYTES;
393
+ this.batchIngestCaller = new AsyncCaller({
394
+ maxRetries: 4,
395
+ maxConcurrency: this.traceBatchConcurrency,
396
+ maxQueueSizeBytes: maxMemory,
397
+ ...config.callerOptions ?? {},
398
+ onFailedResponseHook: handle429,
399
+ debug: config.debug ?? this.debug
400
+ });
401
+ this.hideInputs = config.hideInputs ?? config.anonymizer ?? defaultConfig.hideInputs;
402
+ this.hideOutputs = config.hideOutputs ?? config.anonymizer ?? defaultConfig.hideOutputs;
403
+ this.omitTracedRuntimeInfo = config.omitTracedRuntimeInfo ?? false;
404
+ this.autoBatchTracing = config.autoBatchTracing ?? this.autoBatchTracing;
405
+ this.autoBatchQueue = new AutoBatchQueue(maxMemory);
406
+ this.blockOnRootRunFinalization = config.blockOnRootRunFinalization ?? this.blockOnRootRunFinalization;
407
+ this.batchSizeBytesLimit = config.batchSizeBytesLimit;
408
+ this.batchSizeLimit = config.batchSizeLimit;
409
+ this.fetchOptions = config.fetchOptions || {};
410
+ this.manualFlushMode = config.manualFlushMode ?? this.manualFlushMode;
411
+ if (getOtelEnabled()) {
412
+ this.langSmithToOTELTranslator = new LangSmithToOTELTranslator();
413
+ }
414
+ this.cachedLSEnvVarsForMetadata = getLangSmithEnvVarsMetadata();
415
+ }
416
+ static getDefaultClientConfig() {
417
+ const apiKey = getLangSmithEnvironmentVariable("API_KEY");
418
+ const apiUrl = getLangSmithEnvironmentVariable("ENDPOINT") ?? DEFAULT_API_URL;
419
+ const hideInputs = getLangSmithEnvironmentVariable("HIDE_INPUTS") === "true";
420
+ const hideOutputs = getLangSmithEnvironmentVariable("HIDE_OUTPUTS") === "true";
421
+ return {
422
+ apiUrl,
423
+ apiKey,
424
+ webUrl: void 0,
425
+ hideInputs,
426
+ hideOutputs
427
+ };
428
+ }
429
+ getHostUrl() {
430
+ if (this.webUrl) {
431
+ return this.webUrl;
432
+ } else if (isLocalhost(this.apiUrl)) {
433
+ this.webUrl = "http://localhost:3000";
434
+ return this.webUrl;
435
+ } else if (this.apiUrl.endsWith("/api/v1")) {
436
+ this.webUrl = this.apiUrl.replace("/api/v1", "");
437
+ return this.webUrl;
438
+ } else if (this.apiUrl.includes("/api") && !this.apiUrl.split(".", 1)[0].endsWith("api")) {
439
+ this.webUrl = this.apiUrl.replace("/api", "");
440
+ return this.webUrl;
441
+ } else if (this.apiUrl.split(".", 1)[0].includes("dev")) {
442
+ this.webUrl = "https://dev.smith.langchain.com";
443
+ return this.webUrl;
444
+ } else if (this.apiUrl.split(".", 1)[0].includes("eu")) {
445
+ this.webUrl = "https://eu.smith.langchain.com";
446
+ return this.webUrl;
447
+ } else if (this.apiUrl.split(".", 1)[0].includes("beta")) {
448
+ this.webUrl = "https://beta.smith.langchain.com";
449
+ return this.webUrl;
450
+ } else {
451
+ this.webUrl = "https://smith.langchain.com";
452
+ return this.webUrl;
453
+ }
454
+ }
455
+ get headers() {
456
+ const headers = {
457
+ "User-Agent": `langsmith-js/${__version__}`
458
+ };
459
+ if (this.apiKey) {
460
+ headers["x-api-key"] = `${this.apiKey}`;
461
+ }
462
+ if (this.workspaceId) {
463
+ headers["x-tenant-id"] = this.workspaceId;
464
+ }
465
+ return headers;
466
+ }
467
+ _getPlatformEndpointPath(path) {
468
+ const needsV1Prefix = this.apiUrl.slice(-3) !== "/v1" && this.apiUrl.slice(-4) !== "/v1/";
469
+ return needsV1Prefix ? `/v1/platform/${path}` : `/platform/${path}`;
470
+ }
471
+ async processInputs(inputs) {
472
+ if (this.hideInputs === false) {
473
+ return inputs;
474
+ }
475
+ if (this.hideInputs === true) {
476
+ return {};
477
+ }
478
+ if (typeof this.hideInputs === "function") {
479
+ return this.hideInputs(inputs);
480
+ }
481
+ return inputs;
482
+ }
483
+ async processOutputs(outputs) {
484
+ if (this.hideOutputs === false) {
485
+ return outputs;
486
+ }
487
+ if (this.hideOutputs === true) {
488
+ return {};
489
+ }
490
+ if (typeof this.hideOutputs === "function") {
491
+ return this.hideOutputs(outputs);
492
+ }
493
+ return outputs;
494
+ }
495
+ async prepareRunCreateOrUpdateInputs(run) {
496
+ const runParams = { ...run };
497
+ if (runParams.inputs !== void 0) {
498
+ runParams.inputs = await this.processInputs(runParams.inputs);
499
+ }
500
+ if (runParams.outputs !== void 0) {
501
+ runParams.outputs = await this.processOutputs(runParams.outputs);
502
+ }
503
+ return runParams;
504
+ }
505
+ async _getResponse(path, queryParams) {
506
+ const paramsString = queryParams?.toString() ?? "";
507
+ const url = `${this.apiUrl}${path}?${paramsString}`;
508
+ const response = await this.caller.call(async () => {
509
+ const res = await this._fetch(url, {
510
+ method: "GET",
511
+ headers: this.headers,
512
+ signal: AbortSignal.timeout(this.timeout_ms),
513
+ ...this.fetchOptions
514
+ });
515
+ await raiseForStatus(res, `fetch ${path}`);
516
+ return res;
517
+ });
518
+ return response;
519
+ }
520
+ async _get(path, queryParams) {
521
+ const response = await this._getResponse(path, queryParams);
522
+ return response.json();
523
+ }
524
+ async *_getPaginated(path, queryParams = new URLSearchParams(), transform) {
525
+ let offset = Number(queryParams.get("offset")) || 0;
526
+ const limit = Number(queryParams.get("limit")) || 100;
527
+ while (true) {
528
+ queryParams.set("offset", String(offset));
529
+ queryParams.set("limit", String(limit));
530
+ const url = `${this.apiUrl}${path}?${queryParams}`;
531
+ const response = await this.caller.call(async () => {
532
+ const res = await this._fetch(url, {
533
+ method: "GET",
534
+ headers: this.headers,
535
+ signal: AbortSignal.timeout(this.timeout_ms),
536
+ ...this.fetchOptions
537
+ });
538
+ await raiseForStatus(res, `fetch ${path}`);
539
+ return res;
540
+ });
541
+ const items = transform ? transform(await response.json()) : await response.json();
542
+ if (items.length === 0) {
543
+ break;
544
+ }
545
+ yield items;
546
+ if (items.length < limit) {
547
+ break;
548
+ }
549
+ offset += items.length;
550
+ }
551
+ }
552
+ async *_getCursorPaginatedList(path, body = null, requestMethod = "POST", dataKey = "runs") {
553
+ const bodyParams = body ? { ...body } : {};
554
+ while (true) {
555
+ const body2 = JSON.stringify(bodyParams);
556
+ const response = await this.caller.call(async () => {
557
+ const res = await this._fetch(`${this.apiUrl}${path}`, {
558
+ method: requestMethod,
559
+ headers: { ...this.headers, "Content-Type": "application/json" },
560
+ signal: AbortSignal.timeout(this.timeout_ms),
561
+ ...this.fetchOptions,
562
+ body: body2
563
+ });
564
+ await raiseForStatus(res, `fetch ${path}`);
565
+ return res;
566
+ });
567
+ const responseBody = await response.json();
568
+ if (!responseBody) {
569
+ break;
570
+ }
571
+ if (!responseBody[dataKey]) {
572
+ break;
573
+ }
574
+ yield responseBody[dataKey];
575
+ const cursors = responseBody.cursors;
576
+ if (!cursors) {
577
+ break;
578
+ }
579
+ if (!cursors.next) {
580
+ break;
581
+ }
582
+ bodyParams.cursor = cursors.next;
583
+ }
584
+ }
585
+ // Allows mocking for tests
586
+ _shouldSample() {
587
+ if (this.tracingSampleRate === void 0) {
588
+ return true;
589
+ }
590
+ return Math.random() < this.tracingSampleRate;
591
+ }
592
+ _filterForSampling(runs, patch = false) {
593
+ if (this.tracingSampleRate === void 0) {
594
+ return runs;
595
+ }
596
+ if (patch) {
597
+ const sampled = [];
598
+ for (const run of runs) {
599
+ if (!this.filteredPostUuids.has(run.trace_id)) {
600
+ sampled.push(run);
601
+ } else if (run.id === run.trace_id) {
602
+ this.filteredPostUuids.delete(run.trace_id);
603
+ }
604
+ }
605
+ return sampled;
606
+ } else {
607
+ const sampled = [];
608
+ for (const run of runs) {
609
+ const traceId = run.trace_id ?? run.id;
610
+ if (this.filteredPostUuids.has(traceId)) {
611
+ continue;
612
+ }
613
+ if (run.id === traceId) {
614
+ if (this._shouldSample()) {
615
+ sampled.push(run);
616
+ } else {
617
+ this.filteredPostUuids.add(traceId);
618
+ }
619
+ } else {
620
+ sampled.push(run);
621
+ }
622
+ }
623
+ return sampled;
624
+ }
625
+ }
626
+ async _getBatchSizeLimitBytes() {
627
+ const serverInfo = await this._ensureServerInfo();
628
+ return this.batchSizeBytesLimit ?? serverInfo?.batch_ingest_config?.size_limit_bytes ?? DEFAULT_UNCOMPRESSED_BATCH_SIZE_LIMIT_BYTES;
629
+ }
630
+ /**
631
+ * Get the maximum number of operations to batch in a single request.
632
+ */
633
+ async _getBatchSizeLimit() {
634
+ const serverInfo = await this._ensureServerInfo();
635
+ return this.batchSizeLimit ?? serverInfo?.batch_ingest_config?.size_limit ?? DEFAULT_BATCH_SIZE_LIMIT;
636
+ }
637
+ async _getDatasetExamplesMultiPartSupport() {
638
+ const serverInfo = await this._ensureServerInfo();
639
+ return serverInfo.instance_flags?.dataset_examples_multipart_enabled ?? false;
640
+ }
641
+ drainAutoBatchQueue({ batchSizeLimitBytes, batchSizeLimit }) {
642
+ const promises = [];
643
+ while (this.autoBatchQueue.items.length > 0) {
644
+ const [batch, done] = this.autoBatchQueue.pop({
645
+ upToSizeBytes: batchSizeLimitBytes,
646
+ upToSize: batchSizeLimit
647
+ });
648
+ if (!batch.length) {
649
+ done();
650
+ break;
651
+ }
652
+ const batchesByDestination = batch.reduce((acc, item) => {
653
+ const apiUrl = item.apiUrl ?? this.apiUrl;
654
+ const apiKey = item.apiKey ?? this.apiKey;
655
+ const isDefault = item.apiKey === this.apiKey && item.apiUrl === this.apiUrl;
656
+ const batchKey = isDefault ? "default" : `${apiUrl}|${apiKey}`;
657
+ if (!acc[batchKey]) {
658
+ acc[batchKey] = [];
659
+ }
660
+ acc[batchKey].push(item);
661
+ return acc;
662
+ }, {});
663
+ const batchPromises = [];
664
+ for (const [batchKey, batch2] of Object.entries(batchesByDestination)) {
665
+ const batchPromise = this._processBatch(batch2, {
666
+ apiUrl: batchKey === "default" ? void 0 : batchKey.split("|")[0],
667
+ apiKey: batchKey === "default" ? void 0 : batchKey.split("|")[1]
668
+ });
669
+ batchPromises.push(batchPromise);
670
+ }
671
+ const allBatchesPromise = Promise.all(batchPromises).finally(done);
672
+ promises.push(allBatchesPromise);
673
+ }
674
+ return Promise.all(promises);
675
+ }
676
+ async _processBatch(batch, options) {
677
+ if (!batch.length) {
678
+ return;
679
+ }
680
+ const batchSizeBytes = batch.reduce((sum, item) => sum + (item.size ?? 0), 0);
681
+ try {
682
+ if (this.langSmithToOTELTranslator !== void 0) {
683
+ this._sendBatchToOTELTranslator(batch);
684
+ } else {
685
+ const ingestParams = {
686
+ runCreates: batch.filter((item) => item.action === "create").map((item) => item.item),
687
+ runUpdates: batch.filter((item) => item.action === "update").map((item) => item.item)
688
+ };
689
+ const serverInfo = await this._ensureServerInfo();
690
+ const useMultipart = !this._multipartDisabled && (serverInfo?.batch_ingest_config?.use_multipart_endpoint ?? true);
691
+ if (useMultipart) {
692
+ const useGzip = serverInfo?.instance_flags?.gzip_body_enabled;
693
+ try {
694
+ await this.multipartIngestRuns(ingestParams, {
695
+ ...options,
696
+ useGzip,
697
+ sizeBytes: batchSizeBytes
698
+ });
699
+ } catch (e) {
700
+ if (isLangSmithNotFoundError(e)) {
701
+ this._multipartDisabled = true;
702
+ await this.batchIngestRuns(ingestParams, {
703
+ ...options,
704
+ sizeBytes: batchSizeBytes
705
+ });
706
+ } else {
707
+ throw e;
708
+ }
709
+ }
710
+ } else {
711
+ await this.batchIngestRuns(ingestParams, {
712
+ ...options,
713
+ sizeBytes: batchSizeBytes
714
+ });
715
+ }
716
+ }
717
+ } catch (e) {
718
+ console.error("Error exporting batch:", e);
719
+ }
720
+ }
721
+ _sendBatchToOTELTranslator(batch) {
722
+ if (this.langSmithToOTELTranslator !== void 0) {
723
+ const otelContextMap = /* @__PURE__ */ new Map();
724
+ const operations = [];
725
+ for (const item of batch) {
726
+ if (item.item.id && item.otelContext) {
727
+ otelContextMap.set(item.item.id, item.otelContext);
728
+ if (item.action === "create") {
729
+ operations.push({
730
+ operation: "post",
731
+ id: item.item.id,
732
+ trace_id: item.item.trace_id ?? item.item.id,
733
+ run: item.item
734
+ });
735
+ } else {
736
+ operations.push({
737
+ operation: "patch",
738
+ id: item.item.id,
739
+ trace_id: item.item.trace_id ?? item.item.id,
740
+ run: item.item
741
+ });
742
+ }
743
+ }
744
+ }
745
+ this.langSmithToOTELTranslator.exportBatch(operations, otelContextMap);
746
+ }
747
+ }
748
+ async processRunOperation(item) {
749
+ clearTimeout(this.autoBatchTimeout);
750
+ this.autoBatchTimeout = void 0;
751
+ item.item = mergeRuntimeEnvIntoRun(item.item, this.cachedLSEnvVarsForMetadata, this.omitTracedRuntimeInfo);
752
+ const itemPromise = this.autoBatchQueue.push(item);
753
+ if (this.manualFlushMode) {
754
+ return itemPromise;
755
+ }
756
+ const sizeLimitBytes = await this._getBatchSizeLimitBytes();
757
+ const sizeLimit = await this._getBatchSizeLimit();
758
+ if (this.autoBatchQueue.sizeBytes > sizeLimitBytes || this.autoBatchQueue.items.length > sizeLimit) {
759
+ void this.drainAutoBatchQueue({
760
+ batchSizeLimitBytes: sizeLimitBytes,
761
+ batchSizeLimit: sizeLimit
762
+ });
763
+ }
764
+ if (this.autoBatchQueue.items.length > 0) {
765
+ this.autoBatchTimeout = setTimeout(() => {
766
+ this.autoBatchTimeout = void 0;
767
+ void this.drainAutoBatchQueue({
768
+ batchSizeLimitBytes: sizeLimitBytes,
769
+ batchSizeLimit: sizeLimit
770
+ });
771
+ }, this.autoBatchAggregationDelayMs);
772
+ }
773
+ return itemPromise;
774
+ }
775
+ async _getServerInfo() {
776
+ const response = await this.caller.call(async () => {
777
+ const res = await this._fetch(`${this.apiUrl}/info`, {
778
+ method: "GET",
779
+ headers: { Accept: "application/json" },
780
+ signal: AbortSignal.timeout(SERVER_INFO_REQUEST_TIMEOUT_MS),
781
+ ...this.fetchOptions
782
+ });
783
+ await raiseForStatus(res, "get server info");
784
+ return res;
785
+ });
786
+ const json = await response.json();
787
+ if (this.debug) {
788
+ console.log("\n=== LangSmith Server Configuration ===\n" + JSON.stringify(json, null, 2) + "\n");
789
+ }
790
+ return json;
791
+ }
792
+ async _ensureServerInfo() {
793
+ if (this._getServerInfoPromise === void 0) {
794
+ this._getServerInfoPromise = (async () => {
795
+ if (this._serverInfo === void 0) {
796
+ try {
797
+ this._serverInfo = await this._getServerInfo();
798
+ } catch (e) {
799
+ console.warn(`[LANGSMITH]: Failed to fetch info on supported operations. Falling back to batch operations and default limits. Info: ${e.status ?? "Unspecified status code"} ${e.message}`);
800
+ }
801
+ }
802
+ return this._serverInfo ?? {};
803
+ })();
804
+ }
805
+ return this._getServerInfoPromise.then((serverInfo) => {
806
+ if (this._serverInfo === void 0) {
807
+ this._getServerInfoPromise = void 0;
808
+ }
809
+ return serverInfo;
810
+ });
811
+ }
812
+ async _getSettings() {
813
+ if (!this.settings) {
814
+ this.settings = this._get("/settings");
815
+ }
816
+ return await this.settings;
817
+ }
818
+ /**
819
+ * Flushes current queued traces.
820
+ */
821
+ async flush() {
822
+ const sizeLimitBytes = await this._getBatchSizeLimitBytes();
823
+ const sizeLimit = await this._getBatchSizeLimit();
824
+ await this.drainAutoBatchQueue({
825
+ batchSizeLimitBytes: sizeLimitBytes,
826
+ batchSizeLimit: sizeLimit
827
+ });
828
+ }
829
+ _cloneCurrentOTELContext() {
830
+ const otel_trace = getOTELTrace();
831
+ const otel_context = getOTELContext();
832
+ if (this.langSmithToOTELTranslator !== void 0) {
833
+ const currentSpan = otel_trace.getActiveSpan();
834
+ if (currentSpan) {
835
+ return otel_trace.setSpan(otel_context.active(), currentSpan);
836
+ }
837
+ }
838
+ return void 0;
839
+ }
840
+ async createRun(run, options) {
841
+ if (!this._filterForSampling([run]).length) {
842
+ return;
843
+ }
844
+ const headers = {
845
+ ...this.headers,
846
+ "Content-Type": "application/json"
847
+ };
848
+ const session_name = run.project_name;
849
+ delete run.project_name;
850
+ const runCreate = await this.prepareRunCreateOrUpdateInputs({
851
+ session_name,
852
+ ...run,
853
+ start_time: run.start_time ?? Date.now()
854
+ });
855
+ if (this.autoBatchTracing && runCreate.trace_id !== void 0 && runCreate.dotted_order !== void 0) {
856
+ const otelContext = this._cloneCurrentOTELContext();
857
+ void this.processRunOperation({
858
+ action: "create",
859
+ item: runCreate,
860
+ otelContext,
861
+ apiKey: options?.apiKey,
862
+ apiUrl: options?.apiUrl
863
+ }).catch(console.error);
864
+ return;
865
+ }
866
+ const mergedRunCreateParam = mergeRuntimeEnvIntoRun(runCreate, this.cachedLSEnvVarsForMetadata, this.omitTracedRuntimeInfo);
867
+ if (options?.apiKey !== void 0) {
868
+ headers["x-api-key"] = options.apiKey;
869
+ }
870
+ if (options?.workspaceId !== void 0) {
871
+ headers["x-tenant-id"] = options.workspaceId;
872
+ }
873
+ const body = serialize(mergedRunCreateParam, `Creating run with id: ${mergedRunCreateParam.id}`);
874
+ await this.caller.call(async () => {
875
+ const res = await this._fetch(`${options?.apiUrl ?? this.apiUrl}/runs`, {
876
+ method: "POST",
877
+ headers,
878
+ signal: AbortSignal.timeout(this.timeout_ms),
879
+ ...this.fetchOptions,
880
+ body
881
+ });
882
+ await raiseForStatus(res, "create run", true);
883
+ return res;
884
+ });
885
+ }
886
+ /**
887
+ * Batch ingest/upsert multiple runs in the Langsmith system.
888
+ * @param runs
889
+ */
890
+ async batchIngestRuns({ runCreates, runUpdates }, options) {
891
+ if (runCreates === void 0 && runUpdates === void 0) {
892
+ return;
893
+ }
894
+ let preparedCreateParams = await Promise.all(runCreates?.map((create) => this.prepareRunCreateOrUpdateInputs(create)) ?? []);
895
+ let preparedUpdateParams = await Promise.all(runUpdates?.map((update) => this.prepareRunCreateOrUpdateInputs(update)) ?? []);
896
+ if (preparedCreateParams.length > 0 && preparedUpdateParams.length > 0) {
897
+ const createById = preparedCreateParams.reduce((params, run) => {
898
+ if (!run.id) {
899
+ return params;
900
+ }
901
+ params[run.id] = run;
902
+ return params;
903
+ }, {});
904
+ const standaloneUpdates = [];
905
+ for (const updateParam of preparedUpdateParams) {
906
+ if (updateParam.id !== void 0 && createById[updateParam.id]) {
907
+ createById[updateParam.id] = {
908
+ ...createById[updateParam.id],
909
+ ...updateParam
910
+ };
911
+ } else {
912
+ standaloneUpdates.push(updateParam);
913
+ }
914
+ }
915
+ preparedCreateParams = Object.values(createById);
916
+ preparedUpdateParams = standaloneUpdates;
917
+ }
918
+ const rawBatch = {
919
+ post: preparedCreateParams,
920
+ patch: preparedUpdateParams
921
+ };
922
+ if (!rawBatch.post.length && !rawBatch.patch.length) {
923
+ return;
924
+ }
925
+ const batchChunks = {
926
+ post: [],
927
+ patch: []
928
+ };
929
+ for (const k of ["post", "patch"]) {
930
+ const key = k;
931
+ const batchItems = rawBatch[key].reverse();
932
+ let batchItem = batchItems.pop();
933
+ while (batchItem !== void 0) {
934
+ batchChunks[key].push(batchItem);
935
+ batchItem = batchItems.pop();
936
+ }
937
+ }
938
+ if (batchChunks.post.length > 0 || batchChunks.patch.length > 0) {
939
+ const runIds = batchChunks.post.map((item) => item.id).concat(batchChunks.patch.map((item) => item.id)).join(",");
940
+ await this._postBatchIngestRuns(serialize(batchChunks, `Ingesting runs with ids: ${runIds}`), options);
941
+ }
942
+ }
943
+ async _postBatchIngestRuns(body, options) {
944
+ const headers = {
945
+ ...this.headers,
946
+ "Content-Type": "application/json",
947
+ Accept: "application/json"
948
+ };
949
+ if (options?.apiKey !== void 0) {
950
+ headers["x-api-key"] = options.apiKey;
951
+ }
952
+ await this.batchIngestCaller.callWithOptions({ sizeBytes: options?.sizeBytes }, async () => {
953
+ const res = await this._fetch(`${options?.apiUrl ?? this.apiUrl}/runs/batch`, {
954
+ method: "POST",
955
+ headers,
956
+ signal: AbortSignal.timeout(this.timeout_ms),
957
+ ...this.fetchOptions,
958
+ body
959
+ });
960
+ await raiseForStatus(res, "batch create run", true);
961
+ return res;
962
+ });
963
+ }
964
+ /**
965
+ * Batch ingest/upsert multiple runs in the Langsmith system.
966
+ * @param runs
967
+ */
968
+ async multipartIngestRuns({ runCreates, runUpdates }, options) {
969
+ if (runCreates === void 0 && runUpdates === void 0) {
970
+ return;
971
+ }
972
+ const allAttachments = {};
973
+ let preparedCreateParams = [];
974
+ for (const create of runCreates ?? []) {
975
+ const preparedCreate = await this.prepareRunCreateOrUpdateInputs(create);
976
+ if (preparedCreate.id !== void 0 && preparedCreate.attachments !== void 0) {
977
+ allAttachments[preparedCreate.id] = preparedCreate.attachments;
978
+ }
979
+ delete preparedCreate.attachments;
980
+ preparedCreateParams.push(preparedCreate);
981
+ }
982
+ let preparedUpdateParams = [];
983
+ for (const update of runUpdates ?? []) {
984
+ preparedUpdateParams.push(await this.prepareRunCreateOrUpdateInputs(update));
985
+ }
986
+ const invalidRunCreate = preparedCreateParams.find((runCreate) => {
987
+ return runCreate.trace_id === void 0 || runCreate.dotted_order === void 0;
988
+ });
989
+ if (invalidRunCreate !== void 0) {
990
+ throw new Error(`Multipart ingest requires "trace_id" and "dotted_order" to be set when creating a run`);
991
+ }
992
+ const invalidRunUpdate = preparedUpdateParams.find((runUpdate) => {
993
+ return runUpdate.trace_id === void 0 || runUpdate.dotted_order === void 0;
994
+ });
995
+ if (invalidRunUpdate !== void 0) {
996
+ throw new Error(`Multipart ingest requires "trace_id" and "dotted_order" to be set when updating a run`);
997
+ }
998
+ if (preparedCreateParams.length > 0 && preparedUpdateParams.length > 0) {
999
+ const createById = preparedCreateParams.reduce((params, run) => {
1000
+ if (!run.id) {
1001
+ return params;
1002
+ }
1003
+ params[run.id] = run;
1004
+ return params;
1005
+ }, {});
1006
+ const standaloneUpdates = [];
1007
+ for (const updateParam of preparedUpdateParams) {
1008
+ if (updateParam.id !== void 0 && createById[updateParam.id]) {
1009
+ createById[updateParam.id] = {
1010
+ ...createById[updateParam.id],
1011
+ ...updateParam
1012
+ };
1013
+ } else {
1014
+ standaloneUpdates.push(updateParam);
1015
+ }
1016
+ }
1017
+ preparedCreateParams = Object.values(createById);
1018
+ preparedUpdateParams = standaloneUpdates;
1019
+ }
1020
+ if (preparedCreateParams.length === 0 && preparedUpdateParams.length === 0) {
1021
+ return;
1022
+ }
1023
+ const accumulatedContext = [];
1024
+ const accumulatedParts = [];
1025
+ for (const [method, payloads] of [
1026
+ ["post", preparedCreateParams],
1027
+ ["patch", preparedUpdateParams]
1028
+ ]) {
1029
+ for (const originalPayload of payloads) {
1030
+ const { inputs, outputs, events, extra, error, serialized, attachments, ...payload } = originalPayload;
1031
+ const fields = { inputs, outputs, events, extra, error, serialized };
1032
+ const stringifiedPayload = serialize(payload, `Serializing for multipart ingestion of run with id: ${payload.id}`);
1033
+ accumulatedParts.push({
1034
+ name: `${method}.${payload.id}`,
1035
+ payload: new Blob([stringifiedPayload], {
1036
+ type: `application/json; length=${stringifiedPayload.length}`
1037
+ // encoding=gzip
1038
+ })
1039
+ });
1040
+ for (const [key, value] of Object.entries(fields)) {
1041
+ if (value === void 0) {
1042
+ continue;
1043
+ }
1044
+ const stringifiedValue = serialize(value, `Serializing ${key} for multipart ingestion of run with id: ${payload.id}`);
1045
+ accumulatedParts.push({
1046
+ name: `${method}.${payload.id}.${key}`,
1047
+ payload: new Blob([stringifiedValue], {
1048
+ type: `application/json; length=${stringifiedValue.length}`
1049
+ })
1050
+ });
1051
+ }
1052
+ if (payload.id !== void 0) {
1053
+ const attachments2 = allAttachments[payload.id];
1054
+ if (attachments2) {
1055
+ delete allAttachments[payload.id];
1056
+ for (const [name, attachment] of Object.entries(attachments2)) {
1057
+ let contentType;
1058
+ let content;
1059
+ if (Array.isArray(attachment)) {
1060
+ [contentType, content] = attachment;
1061
+ } else {
1062
+ contentType = attachment.mimeType;
1063
+ content = attachment.data;
1064
+ }
1065
+ if (name.includes(".")) {
1066
+ console.warn(`Skipping attachment '${name}' for run ${payload.id}: Invalid attachment name. Attachment names must not contain periods ('.'). Please rename the attachment and try again.`);
1067
+ continue;
1068
+ }
1069
+ accumulatedParts.push({
1070
+ name: `attachment.${payload.id}.${name}`,
1071
+ payload: new Blob([content], {
1072
+ type: `${contentType}; length=${content.byteLength}`
1073
+ })
1074
+ });
1075
+ }
1076
+ }
1077
+ }
1078
+ accumulatedContext.push(`trace=${payload.trace_id},id=${payload.id}`);
1079
+ }
1080
+ }
1081
+ await this._sendMultipartRequest(accumulatedParts, accumulatedContext.join("; "), options);
1082
+ }
1083
+ async _createNodeFetchBody(parts, boundary) {
1084
+ const chunks = [];
1085
+ for (const part of parts) {
1086
+ chunks.push(new Blob([`--${boundary}\r
1087
+ `]));
1088
+ chunks.push(new Blob([
1089
+ `Content-Disposition: form-data; name="${part.name}"\r
1090
+ `,
1091
+ `Content-Type: ${part.payload.type}\r
1092
+ \r
1093
+ `
1094
+ ]));
1095
+ chunks.push(part.payload);
1096
+ chunks.push(new Blob(["\r\n"]));
1097
+ }
1098
+ chunks.push(new Blob([`--${boundary}--\r
1099
+ `]));
1100
+ const body = new Blob(chunks);
1101
+ const arrayBuffer = await body.arrayBuffer();
1102
+ return arrayBuffer;
1103
+ }
1104
+ async _createMultipartStream(parts, boundary) {
1105
+ const encoder = new TextEncoder();
1106
+ const stream = new ReadableStream({
1107
+ async start(controller) {
1108
+ const writeChunk = async (chunk) => {
1109
+ if (typeof chunk === "string") {
1110
+ controller.enqueue(encoder.encode(chunk));
1111
+ } else {
1112
+ controller.enqueue(chunk);
1113
+ }
1114
+ };
1115
+ for (const part of parts) {
1116
+ await writeChunk(`--${boundary}\r
1117
+ `);
1118
+ await writeChunk(`Content-Disposition: form-data; name="${part.name}"\r
1119
+ `);
1120
+ await writeChunk(`Content-Type: ${part.payload.type}\r
1121
+ \r
1122
+ `);
1123
+ const payloadStream = part.payload.stream();
1124
+ const reader = payloadStream.getReader();
1125
+ try {
1126
+ let result;
1127
+ while (!(result = await reader.read()).done) {
1128
+ controller.enqueue(result.value);
1129
+ }
1130
+ } finally {
1131
+ reader.releaseLock();
1132
+ }
1133
+ await writeChunk("\r\n");
1134
+ }
1135
+ await writeChunk(`--${boundary}--\r
1136
+ `);
1137
+ controller.close();
1138
+ }
1139
+ });
1140
+ return stream;
1141
+ }
1142
+ async _sendMultipartRequest(parts, context, options) {
1143
+ const boundary = "----LangSmithFormBoundary" + Math.random().toString(36).slice(2);
1144
+ const isNodeFetch = _globalFetchImplementationIsNodeFetch();
1145
+ const buildBuffered = () => this._createNodeFetchBody(parts, boundary);
1146
+ const buildStream = () => this._createMultipartStream(parts, boundary);
1147
+ const sendWithRetry = async (bodyFactory) => {
1148
+ return this.batchIngestCaller.callWithOptions({ sizeBytes: options?.sizeBytes }, async () => {
1149
+ const body = await bodyFactory();
1150
+ const headers = {
1151
+ ...this.headers,
1152
+ "Content-Type": `multipart/form-data; boundary=${boundary}`
1153
+ };
1154
+ if (options?.apiKey !== void 0) {
1155
+ headers["x-api-key"] = options.apiKey;
1156
+ }
1157
+ let transformedBody = body;
1158
+ if (options?.useGzip && typeof body === "object" && "pipeThrough" in body) {
1159
+ transformedBody = body.pipeThrough(new CompressionStream("gzip"));
1160
+ headers["Content-Encoding"] = "gzip";
1161
+ }
1162
+ const response = await this._fetch(`${options?.apiUrl ?? this.apiUrl}/runs/multipart`, {
1163
+ method: "POST",
1164
+ headers,
1165
+ body: transformedBody,
1166
+ duplex: "half",
1167
+ signal: AbortSignal.timeout(this.timeout_ms),
1168
+ ...this.fetchOptions
1169
+ });
1170
+ await raiseForStatus(response, `Failed to send multipart request`, true);
1171
+ return response;
1172
+ });
1173
+ };
1174
+ try {
1175
+ let res;
1176
+ let streamedAttempt = false;
1177
+ if (!isNodeFetch && !this.multipartStreamingDisabled && getEnv() !== "bun") {
1178
+ streamedAttempt = true;
1179
+ res = await sendWithRetry(buildStream);
1180
+ } else {
1181
+ res = await sendWithRetry(buildBuffered);
1182
+ }
1183
+ if ((!this.multipartStreamingDisabled || streamedAttempt) && res.status === 422 && (options?.apiUrl ?? this.apiUrl) !== DEFAULT_API_URL) {
1184
+ console.warn(`Streaming multipart upload to ${options?.apiUrl ?? this.apiUrl}/runs/multipart failed. This usually means the host does not support chunked uploads. Retrying with a buffered upload for operation "${context}".`);
1185
+ this.multipartStreamingDisabled = true;
1186
+ res = await sendWithRetry(buildBuffered);
1187
+ }
1188
+ } catch (e) {
1189
+ if (isLangSmithNotFoundError(e)) {
1190
+ throw e;
1191
+ }
1192
+ console.warn(`${e.message.trim()}
1193
+
1194
+ Context: ${context}`);
1195
+ }
1196
+ }
1197
+ async updateRun(runId, run, options) {
1198
+ assertUuid(runId);
1199
+ if (run.inputs) {
1200
+ run.inputs = await this.processInputs(run.inputs);
1201
+ }
1202
+ if (run.outputs) {
1203
+ run.outputs = await this.processOutputs(run.outputs);
1204
+ }
1205
+ const data = { ...run, id: runId };
1206
+ if (!this._filterForSampling([data], true).length) {
1207
+ return;
1208
+ }
1209
+ if (this.autoBatchTracing && data.trace_id !== void 0 && data.dotted_order !== void 0) {
1210
+ const otelContext = this._cloneCurrentOTELContext();
1211
+ if (run.end_time !== void 0 && data.parent_run_id === void 0 && this.blockOnRootRunFinalization && !this.manualFlushMode) {
1212
+ await this.processRunOperation({
1213
+ action: "update",
1214
+ item: data,
1215
+ otelContext,
1216
+ apiKey: options?.apiKey,
1217
+ apiUrl: options?.apiUrl
1218
+ }).catch(console.error);
1219
+ return;
1220
+ } else {
1221
+ void this.processRunOperation({
1222
+ action: "update",
1223
+ item: data,
1224
+ otelContext,
1225
+ apiKey: options?.apiKey,
1226
+ apiUrl: options?.apiUrl
1227
+ }).catch(console.error);
1228
+ }
1229
+ return;
1230
+ }
1231
+ const headers = {
1232
+ ...this.headers,
1233
+ "Content-Type": "application/json"
1234
+ };
1235
+ if (options?.apiKey !== void 0) {
1236
+ headers["x-api-key"] = options.apiKey;
1237
+ }
1238
+ if (options?.workspaceId !== void 0) {
1239
+ headers["x-tenant-id"] = options.workspaceId;
1240
+ }
1241
+ const body = serialize(run, `Serializing payload to update run with id: ${runId}`);
1242
+ await this.caller.call(async () => {
1243
+ const res = await this._fetch(`${options?.apiUrl ?? this.apiUrl}/runs/${runId}`, {
1244
+ method: "PATCH",
1245
+ headers,
1246
+ signal: AbortSignal.timeout(this.timeout_ms),
1247
+ ...this.fetchOptions,
1248
+ body
1249
+ });
1250
+ await raiseForStatus(res, "update run", true);
1251
+ return res;
1252
+ });
1253
+ }
1254
+ async readRun(runId, { loadChildRuns } = { loadChildRuns: false }) {
1255
+ assertUuid(runId);
1256
+ let run = await this._get(`/runs/${runId}`);
1257
+ if (loadChildRuns) {
1258
+ run = await this._loadChildRuns(run);
1259
+ }
1260
+ return run;
1261
+ }
1262
+ async getRunUrl({ runId, run, projectOpts }) {
1263
+ if (run !== void 0) {
1264
+ let sessionId;
1265
+ if (run.session_id) {
1266
+ sessionId = run.session_id;
1267
+ } else if (projectOpts?.projectName) {
1268
+ sessionId = (await this.readProject({ projectName: projectOpts?.projectName })).id;
1269
+ } else if (projectOpts?.projectId) {
1270
+ sessionId = projectOpts?.projectId;
1271
+ } else {
1272
+ const project = await this.readProject({
1273
+ projectName: getLangSmithEnvironmentVariable("PROJECT") || "default"
1274
+ });
1275
+ sessionId = project.id;
1276
+ }
1277
+ const tenantId = await this._getTenantId();
1278
+ return `${this.getHostUrl()}/o/${tenantId}/projects/p/${sessionId}/r/${run.id}?poll=true`;
1279
+ } else if (runId !== void 0) {
1280
+ const run_ = await this.readRun(runId);
1281
+ if (!run_.app_path) {
1282
+ throw new Error(`Run ${runId} has no app_path`);
1283
+ }
1284
+ const baseUrl = this.getHostUrl();
1285
+ return `${baseUrl}${run_.app_path}`;
1286
+ } else {
1287
+ throw new Error("Must provide either runId or run");
1288
+ }
1289
+ }
1290
+ async _loadChildRuns(run) {
1291
+ const childRuns = await toArray(this.listRuns({
1292
+ isRoot: false,
1293
+ projectId: run.session_id,
1294
+ traceId: run.trace_id
1295
+ }));
1296
+ const treemap = {};
1297
+ const runs = {};
1298
+ childRuns.sort((a, b) => (a?.dotted_order ?? "").localeCompare(b?.dotted_order ?? ""));
1299
+ for (const childRun of childRuns) {
1300
+ if (childRun.parent_run_id === null || childRun.parent_run_id === void 0) {
1301
+ throw new Error(`Child run ${childRun.id} has no parent`);
1302
+ }
1303
+ if (childRun.dotted_order?.startsWith(run.dotted_order ?? "") && childRun.id !== run.id) {
1304
+ if (!(childRun.parent_run_id in treemap)) {
1305
+ treemap[childRun.parent_run_id] = [];
1306
+ }
1307
+ treemap[childRun.parent_run_id].push(childRun);
1308
+ runs[childRun.id] = childRun;
1309
+ }
1310
+ }
1311
+ run.child_runs = treemap[run.id] || [];
1312
+ for (const runId in treemap) {
1313
+ if (runId !== run.id) {
1314
+ runs[runId].child_runs = treemap[runId];
1315
+ }
1316
+ }
1317
+ return run;
1318
+ }
1319
+ /**
1320
+ * List runs from the LangSmith server.
1321
+ * @param projectId - The ID of the project to filter by.
1322
+ * @param projectName - The name of the project to filter by.
1323
+ * @param parentRunId - The ID of the parent run to filter by.
1324
+ * @param traceId - The ID of the trace to filter by.
1325
+ * @param referenceExampleId - The ID of the reference example to filter by.
1326
+ * @param startTime - The start time to filter by.
1327
+ * @param isRoot - Indicates whether to only return root runs.
1328
+ * @param runType - The run type to filter by.
1329
+ * @param error - Indicates whether to filter by error runs.
1330
+ * @param id - The ID of the run to filter by.
1331
+ * @param query - The query string to filter by.
1332
+ * @param filter - The filter string to apply to the run spans.
1333
+ * @param traceFilter - The filter string to apply on the root run of the trace.
1334
+ * @param treeFilter - The filter string to apply on other runs in the trace.
1335
+ * @param limit - The maximum number of runs to retrieve.
1336
+ * @returns {AsyncIterable<Run>} - The runs.
1337
+ *
1338
+ * @example
1339
+ * // List all runs in a project
1340
+ * const projectRuns = client.listRuns({ projectName: "<your_project>" });
1341
+ *
1342
+ * @example
1343
+ * // List LLM and Chat runs in the last 24 hours
1344
+ * const todaysLLMRuns = client.listRuns({
1345
+ * projectName: "<your_project>",
1346
+ * start_time: new Date(Date.now() - 24 * 60 * 60 * 1000),
1347
+ * run_type: "llm",
1348
+ * });
1349
+ *
1350
+ * @example
1351
+ * // List traces in a project
1352
+ * const rootRuns = client.listRuns({
1353
+ * projectName: "<your_project>",
1354
+ * execution_order: 1,
1355
+ * });
1356
+ *
1357
+ * @example
1358
+ * // List runs without errors
1359
+ * const correctRuns = client.listRuns({
1360
+ * projectName: "<your_project>",
1361
+ * error: false,
1362
+ * });
1363
+ *
1364
+ * @example
1365
+ * // List runs by run ID
1366
+ * const runIds = [
1367
+ * "a36092d2-4ad5-4fb4-9c0d-0dba9a2ed836",
1368
+ * "9398e6be-964f-4aa4-8ae9-ad78cd4b7074",
1369
+ * ];
1370
+ * const selectedRuns = client.listRuns({ run_ids: runIds });
1371
+ *
1372
+ * @example
1373
+ * // List all "chain" type runs that took more than 10 seconds and had `total_tokens` greater than 5000
1374
+ * const chainRuns = client.listRuns({
1375
+ * projectName: "<your_project>",
1376
+ * filter: 'and(eq(run_type, "chain"), gt(latency, 10), gt(total_tokens, 5000))',
1377
+ * });
1378
+ *
1379
+ * @example
1380
+ * // List all runs called "extractor" whose root of the trace was assigned feedback "user_score" score of 1
1381
+ * const goodExtractorRuns = client.listRuns({
1382
+ * projectName: "<your_project>",
1383
+ * filter: 'eq(name, "extractor")',
1384
+ * traceFilter: 'and(eq(feedback_key, "user_score"), eq(feedback_score, 1))',
1385
+ * });
1386
+ *
1387
+ * @example
1388
+ * // List all runs that started after a specific timestamp and either have "error" not equal to null or a "Correctness" feedback score equal to 0
1389
+ * const complexRuns = client.listRuns({
1390
+ * projectName: "<your_project>",
1391
+ * filter: 'and(gt(start_time, "2023-07-15T12:34:56Z"), or(neq(error, null), and(eq(feedback_key, "Correctness"), eq(feedback_score, 0.0))))',
1392
+ * });
1393
+ *
1394
+ * @example
1395
+ * // List all runs where `tags` include "experimental" or "beta" and `latency` is greater than 2 seconds
1396
+ * const taggedRuns = client.listRuns({
1397
+ * projectName: "<your_project>",
1398
+ * filter: 'and(or(has(tags, "experimental"), has(tags, "beta")), gt(latency, 2))',
1399
+ * });
1400
+ */
1401
+ async *listRuns(props) {
1402
+ const { projectId, projectName, parentRunId, traceId, referenceExampleId, startTime, executionOrder, isRoot, runType, error, id, query, filter, traceFilter, treeFilter, limit, select, order } = props;
1403
+ let projectIds = [];
1404
+ if (projectId) {
1405
+ projectIds = Array.isArray(projectId) ? projectId : [projectId];
1406
+ }
1407
+ if (projectName) {
1408
+ const projectNames = Array.isArray(projectName) ? projectName : [projectName];
1409
+ const projectIds_ = await Promise.all(projectNames.map((name) => this.readProject({ projectName: name }).then((project) => project.id)));
1410
+ projectIds.push(...projectIds_);
1411
+ }
1412
+ const default_select = [
1413
+ "app_path",
1414
+ "completion_cost",
1415
+ "completion_tokens",
1416
+ "dotted_order",
1417
+ "end_time",
1418
+ "error",
1419
+ "events",
1420
+ "extra",
1421
+ "feedback_stats",
1422
+ "first_token_time",
1423
+ "id",
1424
+ "inputs",
1425
+ "name",
1426
+ "outputs",
1427
+ "parent_run_id",
1428
+ "parent_run_ids",
1429
+ "prompt_cost",
1430
+ "prompt_tokens",
1431
+ "reference_example_id",
1432
+ "run_type",
1433
+ "session_id",
1434
+ "start_time",
1435
+ "status",
1436
+ "tags",
1437
+ "total_cost",
1438
+ "total_tokens",
1439
+ "trace_id"
1440
+ ];
1441
+ const body = {
1442
+ session: projectIds.length ? projectIds : null,
1443
+ run_type: runType,
1444
+ reference_example: referenceExampleId,
1445
+ query,
1446
+ filter,
1447
+ trace_filter: traceFilter,
1448
+ tree_filter: treeFilter,
1449
+ execution_order: executionOrder,
1450
+ parent_run: parentRunId,
1451
+ start_time: startTime ? startTime.toISOString() : null,
1452
+ error,
1453
+ id,
1454
+ limit,
1455
+ trace: traceId,
1456
+ select: select ? select : default_select,
1457
+ is_root: isRoot,
1458
+ order
1459
+ };
1460
+ if (body.select.includes("child_run_ids")) {
1461
+ warnOnce("Deprecated: 'child_run_ids' in the listRuns select parameter is deprecated and will be removed in a future version.");
1462
+ }
1463
+ let runsYielded = 0;
1464
+ for await (const runs of this._getCursorPaginatedList("/runs/query", body)) {
1465
+ if (limit) {
1466
+ if (runsYielded >= limit) {
1467
+ break;
1468
+ }
1469
+ if (runs.length + runsYielded > limit) {
1470
+ const newRuns = runs.slice(0, limit - runsYielded);
1471
+ yield* newRuns;
1472
+ break;
1473
+ }
1474
+ runsYielded += runs.length;
1475
+ yield* runs;
1476
+ } else {
1477
+ yield* runs;
1478
+ }
1479
+ }
1480
+ }
1481
+ async *listGroupRuns(props) {
1482
+ const { projectId, projectName, groupBy, filter, startTime, endTime, limit, offset } = props;
1483
+ const sessionId = projectId || (await this.readProject({ projectName })).id;
1484
+ const baseBody = {
1485
+ session_id: sessionId,
1486
+ group_by: groupBy,
1487
+ filter,
1488
+ start_time: startTime ? startTime.toISOString() : null,
1489
+ end_time: endTime ? endTime.toISOString() : null,
1490
+ limit: Number(limit) || 100
1491
+ };
1492
+ let currentOffset = Number(offset) || 0;
1493
+ const path = "/runs/group";
1494
+ const url = `${this.apiUrl}${path}`;
1495
+ while (true) {
1496
+ const currentBody = {
1497
+ ...baseBody,
1498
+ offset: currentOffset
1499
+ };
1500
+ const filteredPayload = Object.fromEntries(Object.entries(currentBody).filter(([_, value]) => value !== void 0));
1501
+ const body = JSON.stringify(filteredPayload);
1502
+ const response = await this.caller.call(async () => {
1503
+ const res = await this._fetch(url, {
1504
+ method: "POST",
1505
+ headers: { ...this.headers, "Content-Type": "application/json" },
1506
+ signal: AbortSignal.timeout(this.timeout_ms),
1507
+ ...this.fetchOptions,
1508
+ body
1509
+ });
1510
+ await raiseForStatus(res, `Failed to fetch ${path}`);
1511
+ return res;
1512
+ });
1513
+ const items = await response.json();
1514
+ const { groups, total } = items;
1515
+ if (groups.length === 0) {
1516
+ break;
1517
+ }
1518
+ for (const thread of groups) {
1519
+ yield thread;
1520
+ }
1521
+ currentOffset += groups.length;
1522
+ if (currentOffset >= total) {
1523
+ break;
1524
+ }
1525
+ }
1526
+ }
1527
+ async getRunStats({ id, trace, parentRun, runType, projectNames, projectIds, referenceExampleIds, startTime, endTime, error, query, filter, traceFilter, treeFilter, isRoot, dataSourceType }) {
1528
+ let projectIds_ = projectIds || [];
1529
+ if (projectNames) {
1530
+ projectIds_ = [
1531
+ ...projectIds || [],
1532
+ ...await Promise.all(projectNames.map((name) => this.readProject({ projectName: name }).then((project) => project.id)))
1533
+ ];
1534
+ }
1535
+ const payload = {
1536
+ id,
1537
+ trace,
1538
+ parent_run: parentRun,
1539
+ run_type: runType,
1540
+ session: projectIds_,
1541
+ reference_example: referenceExampleIds,
1542
+ start_time: startTime,
1543
+ end_time: endTime,
1544
+ error,
1545
+ query,
1546
+ filter,
1547
+ trace_filter: traceFilter,
1548
+ tree_filter: treeFilter,
1549
+ is_root: isRoot,
1550
+ data_source_type: dataSourceType
1551
+ };
1552
+ const filteredPayload = Object.fromEntries(Object.entries(payload).filter(([_, value]) => value !== void 0));
1553
+ const body = JSON.stringify(filteredPayload);
1554
+ const response = await this.caller.call(async () => {
1555
+ const res = await this._fetch(`${this.apiUrl}/runs/stats`, {
1556
+ method: "POST",
1557
+ headers: { ...this.headers, "Content-Type": "application/json" },
1558
+ signal: AbortSignal.timeout(this.timeout_ms),
1559
+ ...this.fetchOptions,
1560
+ body
1561
+ });
1562
+ await raiseForStatus(res, "get run stats");
1563
+ return res;
1564
+ });
1565
+ const result = await response.json();
1566
+ return result;
1567
+ }
1568
+ async shareRun(runId, { shareId } = {}) {
1569
+ const data = {
1570
+ run_id: runId,
1571
+ share_token: shareId || uuid.v4()
1572
+ };
1573
+ assertUuid(runId);
1574
+ const body = JSON.stringify(data);
1575
+ const response = await this.caller.call(async () => {
1576
+ const res = await this._fetch(`${this.apiUrl}/runs/${runId}/share`, {
1577
+ method: "PUT",
1578
+ headers: this.headers,
1579
+ signal: AbortSignal.timeout(this.timeout_ms),
1580
+ ...this.fetchOptions,
1581
+ body
1582
+ });
1583
+ await raiseForStatus(res, "share run");
1584
+ return res;
1585
+ });
1586
+ const result = await response.json();
1587
+ if (result === null || !("share_token" in result)) {
1588
+ throw new Error("Invalid response from server");
1589
+ }
1590
+ return `${this.getHostUrl()}/public/${result["share_token"]}/r`;
1591
+ }
1592
+ async unshareRun(runId) {
1593
+ assertUuid(runId);
1594
+ await this.caller.call(async () => {
1595
+ const res = await this._fetch(`${this.apiUrl}/runs/${runId}/share`, {
1596
+ method: "DELETE",
1597
+ headers: this.headers,
1598
+ signal: AbortSignal.timeout(this.timeout_ms),
1599
+ ...this.fetchOptions
1600
+ });
1601
+ await raiseForStatus(res, "unshare run", true);
1602
+ return res;
1603
+ });
1604
+ }
1605
+ async readRunSharedLink(runId) {
1606
+ assertUuid(runId);
1607
+ const response = await this.caller.call(async () => {
1608
+ const res = await this._fetch(`${this.apiUrl}/runs/${runId}/share`, {
1609
+ method: "GET",
1610
+ headers: this.headers,
1611
+ signal: AbortSignal.timeout(this.timeout_ms),
1612
+ ...this.fetchOptions
1613
+ });
1614
+ await raiseForStatus(res, "read run shared link");
1615
+ return res;
1616
+ });
1617
+ const result = await response.json();
1618
+ if (result === null || !("share_token" in result)) {
1619
+ return void 0;
1620
+ }
1621
+ return `${this.getHostUrl()}/public/${result["share_token"]}/r`;
1622
+ }
1623
+ async listSharedRuns(shareToken, { runIds } = {}) {
1624
+ const queryParams = new URLSearchParams({
1625
+ share_token: shareToken
1626
+ });
1627
+ if (runIds !== void 0) {
1628
+ for (const runId of runIds) {
1629
+ queryParams.append("id", runId);
1630
+ }
1631
+ }
1632
+ assertUuid(shareToken);
1633
+ const response = await this.caller.call(async () => {
1634
+ const res = await this._fetch(`${this.apiUrl}/public/${shareToken}/runs${queryParams}`, {
1635
+ method: "GET",
1636
+ headers: this.headers,
1637
+ signal: AbortSignal.timeout(this.timeout_ms),
1638
+ ...this.fetchOptions
1639
+ });
1640
+ await raiseForStatus(res, "list shared runs");
1641
+ return res;
1642
+ });
1643
+ const runs = await response.json();
1644
+ return runs;
1645
+ }
1646
+ async readDatasetSharedSchema(datasetId, datasetName) {
1647
+ if (!datasetId && !datasetName) {
1648
+ throw new Error("Either datasetId or datasetName must be given");
1649
+ }
1650
+ if (!datasetId) {
1651
+ const dataset = await this.readDataset({ datasetName });
1652
+ datasetId = dataset.id;
1653
+ }
1654
+ assertUuid(datasetId);
1655
+ const response = await this.caller.call(async () => {
1656
+ const res = await this._fetch(`${this.apiUrl}/datasets/${datasetId}/share`, {
1657
+ method: "GET",
1658
+ headers: this.headers,
1659
+ signal: AbortSignal.timeout(this.timeout_ms),
1660
+ ...this.fetchOptions
1661
+ });
1662
+ await raiseForStatus(res, "read dataset shared schema");
1663
+ return res;
1664
+ });
1665
+ const shareSchema = await response.json();
1666
+ shareSchema.url = `${this.getHostUrl()}/public/${shareSchema.share_token}/d`;
1667
+ return shareSchema;
1668
+ }
1669
+ async shareDataset(datasetId, datasetName) {
1670
+ if (!datasetId && !datasetName) {
1671
+ throw new Error("Either datasetId or datasetName must be given");
1672
+ }
1673
+ if (!datasetId) {
1674
+ const dataset = await this.readDataset({ datasetName });
1675
+ datasetId = dataset.id;
1676
+ }
1677
+ const data = {
1678
+ dataset_id: datasetId
1679
+ };
1680
+ assertUuid(datasetId);
1681
+ const body = JSON.stringify(data);
1682
+ const response = await this.caller.call(async () => {
1683
+ const res = await this._fetch(`${this.apiUrl}/datasets/${datasetId}/share`, {
1684
+ method: "PUT",
1685
+ headers: this.headers,
1686
+ signal: AbortSignal.timeout(this.timeout_ms),
1687
+ ...this.fetchOptions,
1688
+ body
1689
+ });
1690
+ await raiseForStatus(res, "share dataset");
1691
+ return res;
1692
+ });
1693
+ const shareSchema = await response.json();
1694
+ shareSchema.url = `${this.getHostUrl()}/public/${shareSchema.share_token}/d`;
1695
+ return shareSchema;
1696
+ }
1697
+ async unshareDataset(datasetId) {
1698
+ assertUuid(datasetId);
1699
+ await this.caller.call(async () => {
1700
+ const res = await this._fetch(`${this.apiUrl}/datasets/${datasetId}/share`, {
1701
+ method: "DELETE",
1702
+ headers: this.headers,
1703
+ signal: AbortSignal.timeout(this.timeout_ms),
1704
+ ...this.fetchOptions
1705
+ });
1706
+ await raiseForStatus(res, "unshare dataset", true);
1707
+ return res;
1708
+ });
1709
+ }
1710
+ async readSharedDataset(shareToken) {
1711
+ assertUuid(shareToken);
1712
+ const response = await this.caller.call(async () => {
1713
+ const res = await this._fetch(`${this.apiUrl}/public/${shareToken}/datasets`, {
1714
+ method: "GET",
1715
+ headers: this.headers,
1716
+ signal: AbortSignal.timeout(this.timeout_ms),
1717
+ ...this.fetchOptions
1718
+ });
1719
+ await raiseForStatus(res, "read shared dataset");
1720
+ return res;
1721
+ });
1722
+ const dataset = await response.json();
1723
+ return dataset;
1724
+ }
1725
+ /**
1726
+ * Get shared examples.
1727
+ *
1728
+ * @param {string} shareToken The share token to get examples for. A share token is the UUID (or LangSmith URL, including UUID) generated when explicitly marking an example as public.
1729
+ * @param {Object} [options] Additional options for listing the examples.
1730
+ * @param {string[] | undefined} [options.exampleIds] A list of example IDs to filter by.
1731
+ * @returns {Promise<Example[]>} The shared examples.
1732
+ */
1733
+ async listSharedExamples(shareToken, options) {
1734
+ const params = {};
1735
+ if (options?.exampleIds) {
1736
+ params.id = options.exampleIds;
1737
+ }
1738
+ const urlParams = new URLSearchParams();
1739
+ Object.entries(params).forEach(([key, value]) => {
1740
+ if (Array.isArray(value)) {
1741
+ value.forEach((v) => urlParams.append(key, v));
1742
+ } else {
1743
+ urlParams.append(key, value);
1744
+ }
1745
+ });
1746
+ const response = await this.caller.call(async () => {
1747
+ const res = await this._fetch(`${this.apiUrl}/public/${shareToken}/examples?${urlParams.toString()}`, {
1748
+ method: "GET",
1749
+ headers: this.headers,
1750
+ signal: AbortSignal.timeout(this.timeout_ms),
1751
+ ...this.fetchOptions
1752
+ });
1753
+ await raiseForStatus(res, "list shared examples");
1754
+ return res;
1755
+ });
1756
+ const result = await response.json();
1757
+ if (!response.ok) {
1758
+ if ("detail" in result) {
1759
+ throw new Error(`Failed to list shared examples.
1760
+ Status: ${response.status}
1761
+ Message: ${Array.isArray(result.detail) ? result.detail.join("\n") : "Unspecified error"}`);
1762
+ }
1763
+ throw new Error(`Failed to list shared examples: ${response.status} ${response.statusText}`);
1764
+ }
1765
+ return result.map((example) => ({
1766
+ ...example,
1767
+ _hostUrl: this.getHostUrl()
1768
+ }));
1769
+ }
1770
+ async createProject({ projectName, description = null, metadata = null, upsert = false, projectExtra = null, referenceDatasetId = null }) {
1771
+ const upsert_ = upsert ? `?upsert=true` : "";
1772
+ const endpoint = `${this.apiUrl}/sessions${upsert_}`;
1773
+ const extra = projectExtra || {};
1774
+ if (metadata) {
1775
+ extra["metadata"] = metadata;
1776
+ }
1777
+ const body = {
1778
+ name: projectName,
1779
+ extra,
1780
+ description
1781
+ };
1782
+ if (referenceDatasetId !== null) {
1783
+ body["reference_dataset_id"] = referenceDatasetId;
1784
+ }
1785
+ const serializedBody = JSON.stringify(body);
1786
+ const response = await this.caller.call(async () => {
1787
+ const res = await this._fetch(endpoint, {
1788
+ method: "POST",
1789
+ headers: { ...this.headers, "Content-Type": "application/json" },
1790
+ signal: AbortSignal.timeout(this.timeout_ms),
1791
+ ...this.fetchOptions,
1792
+ body: serializedBody
1793
+ });
1794
+ await raiseForStatus(res, "create project");
1795
+ return res;
1796
+ });
1797
+ const result = await response.json();
1798
+ return result;
1799
+ }
1800
+ async updateProject(projectId, { name = null, description = null, metadata = null, projectExtra = null, endTime = null }) {
1801
+ const endpoint = `${this.apiUrl}/sessions/${projectId}`;
1802
+ let extra = projectExtra;
1803
+ if (metadata) {
1804
+ extra = { ...extra || {}, metadata };
1805
+ }
1806
+ const body = JSON.stringify({
1807
+ name,
1808
+ extra,
1809
+ description,
1810
+ end_time: endTime ? new Date(endTime).toISOString() : null
1811
+ });
1812
+ const response = await this.caller.call(async () => {
1813
+ const res = await this._fetch(endpoint, {
1814
+ method: "PATCH",
1815
+ headers: { ...this.headers, "Content-Type": "application/json" },
1816
+ signal: AbortSignal.timeout(this.timeout_ms),
1817
+ ...this.fetchOptions,
1818
+ body
1819
+ });
1820
+ await raiseForStatus(res, "update project");
1821
+ return res;
1822
+ });
1823
+ const result = await response.json();
1824
+ return result;
1825
+ }
1826
+ async hasProject({ projectId, projectName }) {
1827
+ let path = "/sessions";
1828
+ const params = new URLSearchParams();
1829
+ if (projectId !== void 0 && projectName !== void 0) {
1830
+ throw new Error("Must provide either projectName or projectId, not both");
1831
+ } else if (projectId !== void 0) {
1832
+ assertUuid(projectId);
1833
+ path += `/${projectId}`;
1834
+ } else if (projectName !== void 0) {
1835
+ params.append("name", projectName);
1836
+ } else {
1837
+ throw new Error("Must provide projectName or projectId");
1838
+ }
1839
+ const response = await this.caller.call(async () => {
1840
+ const res = await this._fetch(`${this.apiUrl}${path}?${params}`, {
1841
+ method: "GET",
1842
+ headers: this.headers,
1843
+ signal: AbortSignal.timeout(this.timeout_ms),
1844
+ ...this.fetchOptions
1845
+ });
1846
+ await raiseForStatus(res, "has project");
1847
+ return res;
1848
+ });
1849
+ try {
1850
+ const result = await response.json();
1851
+ if (!response.ok) {
1852
+ return false;
1853
+ }
1854
+ if (Array.isArray(result)) {
1855
+ return result.length > 0;
1856
+ }
1857
+ return true;
1858
+ } catch (e) {
1859
+ return false;
1860
+ }
1861
+ }
1862
+ async readProject({ projectId, projectName, includeStats }) {
1863
+ let path = "/sessions";
1864
+ const params = new URLSearchParams();
1865
+ if (projectId !== void 0 && projectName !== void 0) {
1866
+ throw new Error("Must provide either projectName or projectId, not both");
1867
+ } else if (projectId !== void 0) {
1868
+ assertUuid(projectId);
1869
+ path += `/${projectId}`;
1870
+ } else if (projectName !== void 0) {
1871
+ params.append("name", projectName);
1872
+ } else {
1873
+ throw new Error("Must provide projectName or projectId");
1874
+ }
1875
+ if (includeStats !== void 0) {
1876
+ params.append("include_stats", includeStats.toString());
1877
+ }
1878
+ const response = await this._get(path, params);
1879
+ let result;
1880
+ if (Array.isArray(response)) {
1881
+ if (response.length === 0) {
1882
+ throw new Error(`Project[id=${projectId}, name=${projectName}] not found`);
1883
+ }
1884
+ result = response[0];
1885
+ } else {
1886
+ result = response;
1887
+ }
1888
+ return result;
1889
+ }
1890
+ async getProjectUrl({ projectId, projectName }) {
1891
+ if (projectId === void 0 && projectName === void 0) {
1892
+ throw new Error("Must provide either projectName or projectId");
1893
+ }
1894
+ const project = await this.readProject({ projectId, projectName });
1895
+ const tenantId = await this._getTenantId();
1896
+ return `${this.getHostUrl()}/o/${tenantId}/projects/p/${project.id}`;
1897
+ }
1898
+ async getDatasetUrl({ datasetId, datasetName }) {
1899
+ if (datasetId === void 0 && datasetName === void 0) {
1900
+ throw new Error("Must provide either datasetName or datasetId");
1901
+ }
1902
+ const dataset = await this.readDataset({ datasetId, datasetName });
1903
+ const tenantId = await this._getTenantId();
1904
+ return `${this.getHostUrl()}/o/${tenantId}/datasets/${dataset.id}`;
1905
+ }
1906
+ async _getTenantId() {
1907
+ if (this._tenantId !== null) {
1908
+ return this._tenantId;
1909
+ }
1910
+ const queryParams = new URLSearchParams({ limit: "1" });
1911
+ for await (const projects of this._getPaginated("/sessions", queryParams)) {
1912
+ this._tenantId = projects[0].tenant_id;
1913
+ return projects[0].tenant_id;
1914
+ }
1915
+ throw new Error("No projects found to resolve tenant.");
1916
+ }
1917
+ async *listProjects({ projectIds, name, nameContains, referenceDatasetId, referenceDatasetName, includeStats, datasetVersion, referenceFree, metadata } = {}) {
1918
+ const params = new URLSearchParams();
1919
+ if (projectIds !== void 0) {
1920
+ for (const projectId of projectIds) {
1921
+ params.append("id", projectId);
1922
+ }
1923
+ }
1924
+ if (name !== void 0) {
1925
+ params.append("name", name);
1926
+ }
1927
+ if (nameContains !== void 0) {
1928
+ params.append("name_contains", nameContains);
1929
+ }
1930
+ if (referenceDatasetId !== void 0) {
1931
+ params.append("reference_dataset", referenceDatasetId);
1932
+ } else if (referenceDatasetName !== void 0) {
1933
+ const dataset = await this.readDataset({
1934
+ datasetName: referenceDatasetName
1935
+ });
1936
+ params.append("reference_dataset", dataset.id);
1937
+ }
1938
+ if (includeStats !== void 0) {
1939
+ params.append("include_stats", includeStats.toString());
1940
+ }
1941
+ if (datasetVersion !== void 0) {
1942
+ params.append("dataset_version", datasetVersion);
1943
+ }
1944
+ if (referenceFree !== void 0) {
1945
+ params.append("reference_free", referenceFree.toString());
1946
+ }
1947
+ if (metadata !== void 0) {
1948
+ params.append("metadata", JSON.stringify(metadata));
1949
+ }
1950
+ for await (const projects of this._getPaginated("/sessions", params)) {
1951
+ yield* projects;
1952
+ }
1953
+ }
1954
+ async deleteProject({ projectId, projectName }) {
1955
+ let projectId_;
1956
+ if (projectId === void 0 && projectName === void 0) {
1957
+ throw new Error("Must provide projectName or projectId");
1958
+ } else if (projectId !== void 0 && projectName !== void 0) {
1959
+ throw new Error("Must provide either projectName or projectId, not both");
1960
+ } else if (projectId === void 0) {
1961
+ projectId_ = (await this.readProject({ projectName })).id;
1962
+ } else {
1963
+ projectId_ = projectId;
1964
+ }
1965
+ assertUuid(projectId_);
1966
+ await this.caller.call(async () => {
1967
+ const res = await this._fetch(`${this.apiUrl}/sessions/${projectId_}`, {
1968
+ method: "DELETE",
1969
+ headers: this.headers,
1970
+ signal: AbortSignal.timeout(this.timeout_ms),
1971
+ ...this.fetchOptions
1972
+ });
1973
+ await raiseForStatus(res, `delete session ${projectId_} (${projectName})`, true);
1974
+ return res;
1975
+ });
1976
+ }
1977
+ async uploadCsv({ csvFile, fileName, inputKeys, outputKeys, description, dataType, name }) {
1978
+ const url = `${this.apiUrl}/datasets/upload`;
1979
+ const formData = new FormData();
1980
+ formData.append("file", csvFile, fileName);
1981
+ inputKeys.forEach((key) => {
1982
+ formData.append("input_keys", key);
1983
+ });
1984
+ outputKeys.forEach((key) => {
1985
+ formData.append("output_keys", key);
1986
+ });
1987
+ if (description) {
1988
+ formData.append("description", description);
1989
+ }
1990
+ if (dataType) {
1991
+ formData.append("data_type", dataType);
1992
+ }
1993
+ if (name) {
1994
+ formData.append("name", name);
1995
+ }
1996
+ const response = await this.caller.call(async () => {
1997
+ const res = await this._fetch(url, {
1998
+ method: "POST",
1999
+ headers: this.headers,
2000
+ signal: AbortSignal.timeout(this.timeout_ms),
2001
+ ...this.fetchOptions,
2002
+ body: formData
2003
+ });
2004
+ await raiseForStatus(res, "upload CSV");
2005
+ return res;
2006
+ });
2007
+ const result = await response.json();
2008
+ return result;
2009
+ }
2010
+ async createDataset(name, { description, dataType, inputsSchema, outputsSchema, metadata } = {}) {
2011
+ const body = {
2012
+ name,
2013
+ description,
2014
+ extra: metadata ? { metadata } : void 0
2015
+ };
2016
+ if (dataType) {
2017
+ body.data_type = dataType;
2018
+ }
2019
+ if (inputsSchema) {
2020
+ body.inputs_schema_definition = inputsSchema;
2021
+ }
2022
+ if (outputsSchema) {
2023
+ body.outputs_schema_definition = outputsSchema;
2024
+ }
2025
+ const serializedBody = JSON.stringify(body);
2026
+ const response = await this.caller.call(async () => {
2027
+ const res = await this._fetch(`${this.apiUrl}/datasets`, {
2028
+ method: "POST",
2029
+ headers: { ...this.headers, "Content-Type": "application/json" },
2030
+ signal: AbortSignal.timeout(this.timeout_ms),
2031
+ ...this.fetchOptions,
2032
+ body: serializedBody
2033
+ });
2034
+ await raiseForStatus(res, "create dataset");
2035
+ return res;
2036
+ });
2037
+ const result = await response.json();
2038
+ return result;
2039
+ }
2040
+ async readDataset({ datasetId, datasetName }) {
2041
+ let path = "/datasets";
2042
+ const params = new URLSearchParams({ limit: "1" });
2043
+ if (datasetId && datasetName) {
2044
+ throw new Error("Must provide either datasetName or datasetId, not both");
2045
+ } else if (datasetId) {
2046
+ assertUuid(datasetId);
2047
+ path += `/${datasetId}`;
2048
+ } else if (datasetName) {
2049
+ params.append("name", datasetName);
2050
+ } else {
2051
+ throw new Error("Must provide datasetName or datasetId");
2052
+ }
2053
+ const response = await this._get(path, params);
2054
+ let result;
2055
+ if (Array.isArray(response)) {
2056
+ if (response.length === 0) {
2057
+ throw new Error(`Dataset[id=${datasetId}, name=${datasetName}] not found`);
2058
+ }
2059
+ result = response[0];
2060
+ } else {
2061
+ result = response;
2062
+ }
2063
+ return result;
2064
+ }
2065
+ async hasDataset({ datasetId, datasetName }) {
2066
+ try {
2067
+ await this.readDataset({ datasetId, datasetName });
2068
+ return true;
2069
+ } catch (e) {
2070
+ if (
2071
+ // eslint-disable-next-line no-instanceof/no-instanceof
2072
+ e instanceof Error && e.message.toLocaleLowerCase().includes("not found")
2073
+ ) {
2074
+ return false;
2075
+ }
2076
+ throw e;
2077
+ }
2078
+ }
2079
+ async diffDatasetVersions({ datasetId, datasetName, fromVersion, toVersion }) {
2080
+ let datasetId_ = datasetId;
2081
+ if (datasetId_ === void 0 && datasetName === void 0) {
2082
+ throw new Error("Must provide either datasetName or datasetId");
2083
+ } else if (datasetId_ !== void 0 && datasetName !== void 0) {
2084
+ throw new Error("Must provide either datasetName or datasetId, not both");
2085
+ } else if (datasetId_ === void 0) {
2086
+ const dataset = await this.readDataset({ datasetName });
2087
+ datasetId_ = dataset.id;
2088
+ }
2089
+ const urlParams = new URLSearchParams({
2090
+ from_version: typeof fromVersion === "string" ? fromVersion : fromVersion.toISOString(),
2091
+ to_version: typeof toVersion === "string" ? toVersion : toVersion.toISOString()
2092
+ });
2093
+ const response = await this._get(`/datasets/${datasetId_}/versions/diff`, urlParams);
2094
+ return response;
2095
+ }
2096
+ async readDatasetOpenaiFinetuning({ datasetId, datasetName }) {
2097
+ const path = "/datasets";
2098
+ if (datasetId !== void 0) ;
2099
+ else if (datasetName !== void 0) {
2100
+ datasetId = (await this.readDataset({ datasetName })).id;
2101
+ } else {
2102
+ throw new Error("Must provide either datasetName or datasetId");
2103
+ }
2104
+ const response = await this._getResponse(`${path}/${datasetId}/openai_ft`);
2105
+ const datasetText = await response.text();
2106
+ const dataset = datasetText.trim().split("\n").map((line) => JSON.parse(line));
2107
+ return dataset;
2108
+ }
2109
+ async *listDatasets({ limit = 100, offset = 0, datasetIds, datasetName, datasetNameContains, metadata } = {}) {
2110
+ const path = "/datasets";
2111
+ const params = new URLSearchParams({
2112
+ limit: limit.toString(),
2113
+ offset: offset.toString()
2114
+ });
2115
+ if (datasetIds !== void 0) {
2116
+ for (const id_ of datasetIds) {
2117
+ params.append("id", id_);
2118
+ }
2119
+ }
2120
+ if (datasetName !== void 0) {
2121
+ params.append("name", datasetName);
2122
+ }
2123
+ if (datasetNameContains !== void 0) {
2124
+ params.append("name_contains", datasetNameContains);
2125
+ }
2126
+ if (metadata !== void 0) {
2127
+ params.append("metadata", JSON.stringify(metadata));
2128
+ }
2129
+ for await (const datasets of this._getPaginated(path, params)) {
2130
+ yield* datasets;
2131
+ }
2132
+ }
2133
+ /**
2134
+ * Update a dataset
2135
+ * @param props The dataset details to update
2136
+ * @returns The updated dataset
2137
+ */
2138
+ async updateDataset(props) {
2139
+ const { datasetId, datasetName, ...update } = props;
2140
+ if (!datasetId && !datasetName) {
2141
+ throw new Error("Must provide either datasetName or datasetId");
2142
+ }
2143
+ const _datasetId = datasetId ?? (await this.readDataset({ datasetName })).id;
2144
+ assertUuid(_datasetId);
2145
+ const body = JSON.stringify(update);
2146
+ const response = await this.caller.call(async () => {
2147
+ const res = await this._fetch(`${this.apiUrl}/datasets/${_datasetId}`, {
2148
+ method: "PATCH",
2149
+ headers: { ...this.headers, "Content-Type": "application/json" },
2150
+ signal: AbortSignal.timeout(this.timeout_ms),
2151
+ ...this.fetchOptions,
2152
+ body
2153
+ });
2154
+ await raiseForStatus(res, "update dataset");
2155
+ return res;
2156
+ });
2157
+ return await response.json();
2158
+ }
2159
+ /**
2160
+ * Updates a tag on a dataset.
2161
+ *
2162
+ * If the tag is already assigned to a different version of this dataset,
2163
+ * the tag will be moved to the new version. The as_of parameter is used to
2164
+ * determine which version of the dataset to apply the new tags to.
2165
+ *
2166
+ * It must be an exact version of the dataset to succeed. You can
2167
+ * use the "readDatasetVersion" method to find the exact version
2168
+ * to apply the tags to.
2169
+ * @param params.datasetId The ID of the dataset to update. Must be provided if "datasetName" is not provided.
2170
+ * @param params.datasetName The name of the dataset to update. Must be provided if "datasetId" is not provided.
2171
+ * @param params.asOf The timestamp of the dataset to apply the new tags to.
2172
+ * @param params.tag The new tag to apply to the dataset.
2173
+ */
2174
+ async updateDatasetTag(props) {
2175
+ const { datasetId, datasetName, asOf, tag } = props;
2176
+ if (!datasetId && !datasetName) {
2177
+ throw new Error("Must provide either datasetName or datasetId");
2178
+ }
2179
+ const _datasetId = datasetId ?? (await this.readDataset({ datasetName })).id;
2180
+ assertUuid(_datasetId);
2181
+ const body = JSON.stringify({
2182
+ as_of: typeof asOf === "string" ? asOf : asOf.toISOString(),
2183
+ tag
2184
+ });
2185
+ await this.caller.call(async () => {
2186
+ const res = await this._fetch(`${this.apiUrl}/datasets/${_datasetId}/tags`, {
2187
+ method: "PUT",
2188
+ headers: { ...this.headers, "Content-Type": "application/json" },
2189
+ signal: AbortSignal.timeout(this.timeout_ms),
2190
+ ...this.fetchOptions,
2191
+ body
2192
+ });
2193
+ await raiseForStatus(res, "update dataset tags", true);
2194
+ return res;
2195
+ });
2196
+ }
2197
+ async deleteDataset({ datasetId, datasetName }) {
2198
+ let path = "/datasets";
2199
+ let datasetId_ = datasetId;
2200
+ if (datasetId !== void 0 && datasetName !== void 0) {
2201
+ throw new Error("Must provide either datasetName or datasetId, not both");
2202
+ } else if (datasetName !== void 0) {
2203
+ const dataset = await this.readDataset({ datasetName });
2204
+ datasetId_ = dataset.id;
2205
+ }
2206
+ if (datasetId_ !== void 0) {
2207
+ assertUuid(datasetId_);
2208
+ path += `/${datasetId_}`;
2209
+ } else {
2210
+ throw new Error("Must provide datasetName or datasetId");
2211
+ }
2212
+ await this.caller.call(async () => {
2213
+ const res = await this._fetch(this.apiUrl + path, {
2214
+ method: "DELETE",
2215
+ headers: this.headers,
2216
+ signal: AbortSignal.timeout(this.timeout_ms),
2217
+ ...this.fetchOptions
2218
+ });
2219
+ await raiseForStatus(res, `delete ${path}`, true);
2220
+ return res;
2221
+ });
2222
+ }
2223
+ async indexDataset({ datasetId, datasetName, tag }) {
2224
+ let datasetId_ = datasetId;
2225
+ if (!datasetId_ && !datasetName) {
2226
+ throw new Error("Must provide either datasetName or datasetId");
2227
+ } else if (datasetId_ && datasetName) {
2228
+ throw new Error("Must provide either datasetName or datasetId, not both");
2229
+ } else if (!datasetId_) {
2230
+ const dataset = await this.readDataset({ datasetName });
2231
+ datasetId_ = dataset.id;
2232
+ }
2233
+ assertUuid(datasetId_);
2234
+ const data = {
2235
+ tag
2236
+ };
2237
+ const body = JSON.stringify(data);
2238
+ const response = await this.caller.call(async () => {
2239
+ const res = await this._fetch(`${this.apiUrl}/datasets/${datasetId_}/index`, {
2240
+ method: "POST",
2241
+ headers: { ...this.headers, "Content-Type": "application/json" },
2242
+ signal: AbortSignal.timeout(this.timeout_ms),
2243
+ ...this.fetchOptions,
2244
+ body
2245
+ });
2246
+ await raiseForStatus(res, "index dataset");
2247
+ return res;
2248
+ });
2249
+ await response.json();
2250
+ }
2251
+ /**
2252
+ * Lets you run a similarity search query on a dataset.
2253
+ *
2254
+ * Requires the dataset to be indexed. Please see the `indexDataset` method to set up indexing.
2255
+ *
2256
+ * @param inputs The input on which to run the similarity search. Must have the
2257
+ * same schema as the dataset.
2258
+ *
2259
+ * @param datasetId The dataset to search for similar examples.
2260
+ *
2261
+ * @param limit The maximum number of examples to return. Will return the top `limit` most
2262
+ * similar examples in order of most similar to least similar. If no similar
2263
+ * examples are found, random examples will be returned.
2264
+ *
2265
+ * @param filter A filter string to apply to the search. Only examples will be returned that
2266
+ * match the filter string. Some examples of filters
2267
+ *
2268
+ * - eq(metadata.mykey, "value")
2269
+ * - and(neq(metadata.my.nested.key, "value"), neq(metadata.mykey, "value"))
2270
+ * - or(eq(metadata.mykey, "value"), eq(metadata.mykey, "othervalue"))
2271
+ *
2272
+ * @returns A list of similar examples.
2273
+ *
2274
+ *
2275
+ * @example
2276
+ * dataset_id = "123e4567-e89b-12d3-a456-426614174000"
2277
+ * inputs = {"text": "How many people live in Berlin?"}
2278
+ * limit = 5
2279
+ * examples = await client.similarExamples(inputs, dataset_id, limit)
2280
+ */
2281
+ async similarExamples(inputs, datasetId, limit, { filter } = {}) {
2282
+ const data = {
2283
+ limit,
2284
+ inputs
2285
+ };
2286
+ if (filter !== void 0) {
2287
+ data["filter"] = filter;
2288
+ }
2289
+ assertUuid(datasetId);
2290
+ const body = JSON.stringify(data);
2291
+ const response = await this.caller.call(async () => {
2292
+ const res = await this._fetch(`${this.apiUrl}/datasets/${datasetId}/search`, {
2293
+ headers: { ...this.headers, "Content-Type": "application/json" },
2294
+ signal: AbortSignal.timeout(this.timeout_ms),
2295
+ ...this.fetchOptions,
2296
+ method: "POST",
2297
+ body
2298
+ });
2299
+ await raiseForStatus(res, "fetch similar examples");
2300
+ return res;
2301
+ });
2302
+ const result = await response.json();
2303
+ return result["examples"];
2304
+ }
2305
+ async createExample(inputsOrUpdate, outputs, options) {
2306
+ if (isExampleCreate(inputsOrUpdate)) {
2307
+ if (outputs !== void 0 || options !== void 0) {
2308
+ throw new Error("Cannot provide outputs or options when using ExampleCreate object");
2309
+ }
2310
+ }
2311
+ let datasetId_ = outputs ? options?.datasetId : inputsOrUpdate.dataset_id;
2312
+ const datasetName_ = outputs ? options?.datasetName : inputsOrUpdate.dataset_name;
2313
+ if (datasetId_ === void 0 && datasetName_ === void 0) {
2314
+ throw new Error("Must provide either datasetName or datasetId");
2315
+ } else if (datasetId_ !== void 0 && datasetName_ !== void 0) {
2316
+ throw new Error("Must provide either datasetName or datasetId, not both");
2317
+ } else if (datasetId_ === void 0) {
2318
+ const dataset = await this.readDataset({ datasetName: datasetName_ });
2319
+ datasetId_ = dataset.id;
2320
+ }
2321
+ const createdAt_ = (outputs ? options?.createdAt : inputsOrUpdate.created_at) || /* @__PURE__ */ new Date();
2322
+ let data;
2323
+ if (!isExampleCreate(inputsOrUpdate)) {
2324
+ data = {
2325
+ inputs: inputsOrUpdate,
2326
+ outputs,
2327
+ created_at: createdAt_?.toISOString(),
2328
+ id: options?.exampleId,
2329
+ metadata: options?.metadata,
2330
+ split: options?.split,
2331
+ source_run_id: options?.sourceRunId,
2332
+ use_source_run_io: options?.useSourceRunIO,
2333
+ use_source_run_attachments: options?.useSourceRunAttachments,
2334
+ attachments: options?.attachments
2335
+ };
2336
+ } else {
2337
+ data = inputsOrUpdate;
2338
+ }
2339
+ const response = await this._uploadExamplesMultipart(datasetId_, [data]);
2340
+ const example = await this.readExample(response.example_ids?.[0] ?? uuid.v4());
2341
+ return example;
2342
+ }
2343
+ async createExamples(propsOrUploads) {
2344
+ if (Array.isArray(propsOrUploads)) {
2345
+ if (propsOrUploads.length === 0) {
2346
+ return [];
2347
+ }
2348
+ const uploads = propsOrUploads;
2349
+ let datasetId_2 = uploads[0].dataset_id;
2350
+ const datasetName_2 = uploads[0].dataset_name;
2351
+ if (datasetId_2 === void 0 && datasetName_2 === void 0) {
2352
+ throw new Error("Must provide either datasetName or datasetId");
2353
+ } else if (datasetId_2 !== void 0 && datasetName_2 !== void 0) {
2354
+ throw new Error("Must provide either datasetName or datasetId, not both");
2355
+ } else if (datasetId_2 === void 0) {
2356
+ const dataset = await this.readDataset({ datasetName: datasetName_2 });
2357
+ datasetId_2 = dataset.id;
2358
+ }
2359
+ const response2 = await this._uploadExamplesMultipart(datasetId_2, uploads);
2360
+ const examples2 = await Promise.all(response2.example_ids.map((id) => this.readExample(id)));
2361
+ return examples2;
2362
+ }
2363
+ const { inputs, outputs, metadata, splits, sourceRunIds, useSourceRunIOs, useSourceRunAttachments, attachments, exampleIds, datasetId, datasetName } = propsOrUploads;
2364
+ if (inputs === void 0) {
2365
+ throw new Error("Must provide inputs when using legacy parameters");
2366
+ }
2367
+ let datasetId_ = datasetId;
2368
+ const datasetName_ = datasetName;
2369
+ if (datasetId_ === void 0 && datasetName_ === void 0) {
2370
+ throw new Error("Must provide either datasetName or datasetId");
2371
+ } else if (datasetId_ !== void 0 && datasetName_ !== void 0) {
2372
+ throw new Error("Must provide either datasetName or datasetId, not both");
2373
+ } else if (datasetId_ === void 0) {
2374
+ const dataset = await this.readDataset({ datasetName: datasetName_ });
2375
+ datasetId_ = dataset.id;
2376
+ }
2377
+ const formattedExamples = inputs.map((input, idx) => {
2378
+ return {
2379
+ dataset_id: datasetId_,
2380
+ inputs: input,
2381
+ outputs: outputs?.[idx],
2382
+ metadata: metadata?.[idx],
2383
+ split: splits?.[idx],
2384
+ id: exampleIds?.[idx],
2385
+ attachments: attachments?.[idx],
2386
+ source_run_id: sourceRunIds?.[idx],
2387
+ use_source_run_io: useSourceRunIOs?.[idx],
2388
+ use_source_run_attachments: useSourceRunAttachments?.[idx]
2389
+ };
2390
+ });
2391
+ const response = await this._uploadExamplesMultipart(datasetId_, formattedExamples);
2392
+ const examples = await Promise.all(response.example_ids.map((id) => this.readExample(id)));
2393
+ return examples;
2394
+ }
2395
+ async createLLMExample(input, generation, options) {
2396
+ return this.createExample({ input }, { output: generation }, options);
2397
+ }
2398
+ async createChatExample(input, generations, options) {
2399
+ const finalInput = input.map((message) => {
2400
+ if (isLangChainMessage(message)) {
2401
+ return convertLangChainMessageToExample(message);
2402
+ }
2403
+ return message;
2404
+ });
2405
+ const finalOutput = isLangChainMessage(generations) ? convertLangChainMessageToExample(generations) : generations;
2406
+ return this.createExample({ input: finalInput }, { output: finalOutput }, options);
2407
+ }
2408
+ async readExample(exampleId) {
2409
+ assertUuid(exampleId);
2410
+ const path = `/examples/${exampleId}`;
2411
+ const rawExample = await this._get(path);
2412
+ const { attachment_urls, ...rest } = rawExample;
2413
+ const example = rest;
2414
+ if (attachment_urls) {
2415
+ example.attachments = Object.entries(attachment_urls).reduce((acc, [key, value]) => {
2416
+ acc[key.slice("attachment.".length)] = {
2417
+ presigned_url: value.presigned_url,
2418
+ mime_type: value.mime_type
2419
+ };
2420
+ return acc;
2421
+ }, {});
2422
+ }
2423
+ return example;
2424
+ }
2425
+ async *listExamples({ datasetId, datasetName, exampleIds, asOf, splits, inlineS3Urls, metadata, limit, offset, filter, includeAttachments } = {}) {
2426
+ let datasetId_;
2427
+ if (datasetId !== void 0 && datasetName !== void 0) {
2428
+ throw new Error("Must provide either datasetName or datasetId, not both");
2429
+ } else if (datasetId !== void 0) {
2430
+ datasetId_ = datasetId;
2431
+ } else if (datasetName !== void 0) {
2432
+ const dataset = await this.readDataset({ datasetName });
2433
+ datasetId_ = dataset.id;
2434
+ } else {
2435
+ throw new Error("Must provide a datasetName or datasetId");
2436
+ }
2437
+ const params = new URLSearchParams({ dataset: datasetId_ });
2438
+ const dataset_version = asOf ? typeof asOf === "string" ? asOf : asOf?.toISOString() : void 0;
2439
+ if (dataset_version) {
2440
+ params.append("as_of", dataset_version);
2441
+ }
2442
+ const inlineS3Urls_ = inlineS3Urls ?? true;
2443
+ params.append("inline_s3_urls", inlineS3Urls_.toString());
2444
+ if (exampleIds !== void 0) {
2445
+ for (const id_ of exampleIds) {
2446
+ params.append("id", id_);
2447
+ }
2448
+ }
2449
+ if (splits !== void 0) {
2450
+ for (const split of splits) {
2451
+ params.append("splits", split);
2452
+ }
2453
+ }
2454
+ if (metadata !== void 0) {
2455
+ const serializedMetadata = JSON.stringify(metadata);
2456
+ params.append("metadata", serializedMetadata);
2457
+ }
2458
+ if (limit !== void 0) {
2459
+ params.append("limit", limit.toString());
2460
+ }
2461
+ if (offset !== void 0) {
2462
+ params.append("offset", offset.toString());
2463
+ }
2464
+ if (filter !== void 0) {
2465
+ params.append("filter", filter);
2466
+ }
2467
+ if (includeAttachments === true) {
2468
+ ["attachment_urls", "outputs", "metadata"].forEach((field) => params.append("select", field));
2469
+ }
2470
+ let i = 0;
2471
+ for await (const rawExamples of this._getPaginated("/examples", params)) {
2472
+ for (const rawExample of rawExamples) {
2473
+ const { attachment_urls, ...rest } = rawExample;
2474
+ const example = rest;
2475
+ if (attachment_urls) {
2476
+ example.attachments = Object.entries(attachment_urls).reduce((acc, [key, value]) => {
2477
+ acc[key.slice("attachment.".length)] = {
2478
+ presigned_url: value.presigned_url,
2479
+ mime_type: value.mime_type || void 0
2480
+ };
2481
+ return acc;
2482
+ }, {});
2483
+ }
2484
+ yield example;
2485
+ i++;
2486
+ }
2487
+ if (limit !== void 0 && i >= limit) {
2488
+ break;
2489
+ }
2490
+ }
2491
+ }
2492
+ async deleteExample(exampleId) {
2493
+ assertUuid(exampleId);
2494
+ const path = `/examples/${exampleId}`;
2495
+ await this.caller.call(async () => {
2496
+ const res = await this._fetch(this.apiUrl + path, {
2497
+ method: "DELETE",
2498
+ headers: this.headers,
2499
+ signal: AbortSignal.timeout(this.timeout_ms),
2500
+ ...this.fetchOptions
2501
+ });
2502
+ await raiseForStatus(res, `delete ${path}`, true);
2503
+ return res;
2504
+ });
2505
+ }
2506
+ /**
2507
+ * Delete multiple examples by ID.
2508
+ * @param exampleIds - The IDs of the examples to delete
2509
+ * @param options - Optional settings for deletion
2510
+ * @param options.hardDelete - If true, permanently delete examples. If false (default), soft delete them.
2511
+ */
2512
+ async deleteExamples(exampleIds, options) {
2513
+ exampleIds.forEach((id) => assertUuid(id));
2514
+ if (options?.hardDelete) {
2515
+ const path = this._getPlatformEndpointPath("datasets/examples/delete");
2516
+ await this.caller.call(async () => {
2517
+ const res = await this._fetch(`${this.apiUrl}${path}`, {
2518
+ method: "POST",
2519
+ headers: { ...this.headers, "Content-Type": "application/json" },
2520
+ body: JSON.stringify({
2521
+ example_ids: exampleIds,
2522
+ hard_delete: true
2523
+ }),
2524
+ signal: AbortSignal.timeout(this.timeout_ms),
2525
+ ...this.fetchOptions
2526
+ });
2527
+ await raiseForStatus(res, "hard delete examples", true);
2528
+ return res;
2529
+ });
2530
+ } else {
2531
+ const params = new URLSearchParams();
2532
+ exampleIds.forEach((id) => params.append("example_ids", id));
2533
+ await this.caller.call(async () => {
2534
+ const res = await this._fetch(`${this.apiUrl}/examples?${params.toString()}`, {
2535
+ method: "DELETE",
2536
+ headers: this.headers,
2537
+ signal: AbortSignal.timeout(this.timeout_ms),
2538
+ ...this.fetchOptions
2539
+ });
2540
+ await raiseForStatus(res, "delete examples", true);
2541
+ return res;
2542
+ });
2543
+ }
2544
+ }
2545
+ async updateExample(exampleIdOrUpdate, update) {
2546
+ let exampleId;
2547
+ if (update) {
2548
+ exampleId = exampleIdOrUpdate;
2549
+ } else {
2550
+ exampleId = exampleIdOrUpdate.id;
2551
+ }
2552
+ assertUuid(exampleId);
2553
+ let updateToUse;
2554
+ if (update) {
2555
+ updateToUse = { id: exampleId, ...update };
2556
+ } else {
2557
+ updateToUse = exampleIdOrUpdate;
2558
+ }
2559
+ let datasetId;
2560
+ if (updateToUse.dataset_id !== void 0) {
2561
+ datasetId = updateToUse.dataset_id;
2562
+ } else {
2563
+ const example = await this.readExample(exampleId);
2564
+ datasetId = example.dataset_id;
2565
+ }
2566
+ return this._updateExamplesMultipart(datasetId, [updateToUse]);
2567
+ }
2568
+ async updateExamples(update) {
2569
+ let datasetId;
2570
+ if (update[0].dataset_id === void 0) {
2571
+ const example = await this.readExample(update[0].id);
2572
+ datasetId = example.dataset_id;
2573
+ } else {
2574
+ datasetId = update[0].dataset_id;
2575
+ }
2576
+ return this._updateExamplesMultipart(datasetId, update);
2577
+ }
2578
+ /**
2579
+ * Get dataset version by closest date or exact tag.
2580
+ *
2581
+ * Use this to resolve the nearest version to a given timestamp or for a given tag.
2582
+ *
2583
+ * @param options The options for getting the dataset version
2584
+ * @param options.datasetId The ID of the dataset
2585
+ * @param options.datasetName The name of the dataset
2586
+ * @param options.asOf The timestamp of the dataset to retrieve
2587
+ * @param options.tag The tag of the dataset to retrieve
2588
+ * @returns The dataset version
2589
+ */
2590
+ async readDatasetVersion({ datasetId, datasetName, asOf, tag }) {
2591
+ let resolvedDatasetId;
2592
+ if (!datasetId) {
2593
+ const dataset = await this.readDataset({ datasetName });
2594
+ resolvedDatasetId = dataset.id;
2595
+ } else {
2596
+ resolvedDatasetId = datasetId;
2597
+ }
2598
+ assertUuid(resolvedDatasetId);
2599
+ if (asOf && tag || !asOf && !tag) {
2600
+ throw new Error("Exactly one of asOf and tag must be specified.");
2601
+ }
2602
+ const params = new URLSearchParams();
2603
+ if (asOf !== void 0) {
2604
+ params.append("as_of", typeof asOf === "string" ? asOf : asOf.toISOString());
2605
+ }
2606
+ if (tag !== void 0) {
2607
+ params.append("tag", tag);
2608
+ }
2609
+ const response = await this.caller.call(async () => {
2610
+ const res = await this._fetch(`${this.apiUrl}/datasets/${resolvedDatasetId}/version?${params.toString()}`, {
2611
+ method: "GET",
2612
+ headers: { ...this.headers },
2613
+ signal: AbortSignal.timeout(this.timeout_ms),
2614
+ ...this.fetchOptions
2615
+ });
2616
+ await raiseForStatus(res, "read dataset version");
2617
+ return res;
2618
+ });
2619
+ return await response.json();
2620
+ }
2621
+ async listDatasetSplits({ datasetId, datasetName, asOf }) {
2622
+ let datasetId_;
2623
+ if (datasetId === void 0 && datasetName === void 0) {
2624
+ throw new Error("Must provide dataset name or ID");
2625
+ } else if (datasetId !== void 0 && datasetName !== void 0) {
2626
+ throw new Error("Must provide either datasetName or datasetId, not both");
2627
+ } else if (datasetId === void 0) {
2628
+ const dataset = await this.readDataset({ datasetName });
2629
+ datasetId_ = dataset.id;
2630
+ } else {
2631
+ datasetId_ = datasetId;
2632
+ }
2633
+ assertUuid(datasetId_);
2634
+ const params = new URLSearchParams();
2635
+ const dataset_version = asOf ? typeof asOf === "string" ? asOf : asOf?.toISOString() : void 0;
2636
+ if (dataset_version) {
2637
+ params.append("as_of", dataset_version);
2638
+ }
2639
+ const response = await this._get(`/datasets/${datasetId_}/splits`, params);
2640
+ return response;
2641
+ }
2642
+ async updateDatasetSplits({ datasetId, datasetName, splitName, exampleIds, remove = false }) {
2643
+ let datasetId_;
2644
+ if (datasetId === void 0 && datasetName === void 0) {
2645
+ throw new Error("Must provide dataset name or ID");
2646
+ } else if (datasetId !== void 0 && datasetName !== void 0) {
2647
+ throw new Error("Must provide either datasetName or datasetId, not both");
2648
+ } else if (datasetId === void 0) {
2649
+ const dataset = await this.readDataset({ datasetName });
2650
+ datasetId_ = dataset.id;
2651
+ } else {
2652
+ datasetId_ = datasetId;
2653
+ }
2654
+ assertUuid(datasetId_);
2655
+ const data = {
2656
+ split_name: splitName,
2657
+ examples: exampleIds.map((id) => {
2658
+ assertUuid(id);
2659
+ return id;
2660
+ }),
2661
+ remove
2662
+ };
2663
+ const body = JSON.stringify(data);
2664
+ await this.caller.call(async () => {
2665
+ const res = await this._fetch(`${this.apiUrl}/datasets/${datasetId_}/splits`, {
2666
+ method: "PUT",
2667
+ headers: { ...this.headers, "Content-Type": "application/json" },
2668
+ signal: AbortSignal.timeout(this.timeout_ms),
2669
+ ...this.fetchOptions,
2670
+ body
2671
+ });
2672
+ await raiseForStatus(res, "update dataset splits", true);
2673
+ return res;
2674
+ });
2675
+ }
2676
+ async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, feedbackId, feedbackConfig, projectId, comparativeExperimentId }) {
2677
+ if (!runId && !projectId) {
2678
+ throw new Error("One of runId or projectId must be provided");
2679
+ }
2680
+ if (runId && projectId) {
2681
+ throw new Error("Only one of runId or projectId can be provided");
2682
+ }
2683
+ const feedback_source = {
2684
+ type: feedbackSourceType ?? "api",
2685
+ metadata: sourceInfo ?? {}
2686
+ };
2687
+ if (sourceRunId !== void 0 && feedback_source?.metadata !== void 0 && !feedback_source.metadata["__run"]) {
2688
+ feedback_source.metadata["__run"] = { run_id: sourceRunId };
2689
+ }
2690
+ if (feedback_source?.metadata !== void 0 && feedback_source.metadata["__run"]?.run_id !== void 0) {
2691
+ assertUuid(feedback_source.metadata["__run"].run_id);
2692
+ }
2693
+ const feedback = {
2694
+ id: feedbackId ?? uuid.v4(),
2695
+ run_id: runId,
2696
+ key,
2697
+ score: _formatFeedbackScore(score),
2698
+ value,
2699
+ correction,
2700
+ comment,
2701
+ feedback_source,
2702
+ comparative_experiment_id: comparativeExperimentId,
2703
+ feedbackConfig,
2704
+ session_id: projectId
2705
+ };
2706
+ const body = JSON.stringify(feedback);
2707
+ const url = `${this.apiUrl}/feedback`;
2708
+ await this.caller.call(async () => {
2709
+ const res = await this._fetch(url, {
2710
+ method: "POST",
2711
+ headers: { ...this.headers, "Content-Type": "application/json" },
2712
+ signal: AbortSignal.timeout(this.timeout_ms),
2713
+ ...this.fetchOptions,
2714
+ body
2715
+ });
2716
+ await raiseForStatus(res, "create feedback", true);
2717
+ return res;
2718
+ });
2719
+ return feedback;
2720
+ }
2721
+ async updateFeedback(feedbackId, { score, value, correction, comment }) {
2722
+ const feedbackUpdate = {};
2723
+ if (score !== void 0 && score !== null) {
2724
+ feedbackUpdate["score"] = _formatFeedbackScore(score);
2725
+ }
2726
+ if (value !== void 0 && value !== null) {
2727
+ feedbackUpdate["value"] = value;
2728
+ }
2729
+ if (correction !== void 0 && correction !== null) {
2730
+ feedbackUpdate["correction"] = correction;
2731
+ }
2732
+ if (comment !== void 0 && comment !== null) {
2733
+ feedbackUpdate["comment"] = comment;
2734
+ }
2735
+ assertUuid(feedbackId);
2736
+ const body = JSON.stringify(feedbackUpdate);
2737
+ await this.caller.call(async () => {
2738
+ const res = await this._fetch(`${this.apiUrl}/feedback/${feedbackId}`, {
2739
+ method: "PATCH",
2740
+ headers: { ...this.headers, "Content-Type": "application/json" },
2741
+ signal: AbortSignal.timeout(this.timeout_ms),
2742
+ ...this.fetchOptions,
2743
+ body
2744
+ });
2745
+ await raiseForStatus(res, "update feedback", true);
2746
+ return res;
2747
+ });
2748
+ }
2749
+ async readFeedback(feedbackId) {
2750
+ assertUuid(feedbackId);
2751
+ const path = `/feedback/${feedbackId}`;
2752
+ const response = await this._get(path);
2753
+ return response;
2754
+ }
2755
+ async deleteFeedback(feedbackId) {
2756
+ assertUuid(feedbackId);
2757
+ const path = `/feedback/${feedbackId}`;
2758
+ await this.caller.call(async () => {
2759
+ const res = await this._fetch(this.apiUrl + path, {
2760
+ method: "DELETE",
2761
+ headers: this.headers,
2762
+ signal: AbortSignal.timeout(this.timeout_ms),
2763
+ ...this.fetchOptions
2764
+ });
2765
+ await raiseForStatus(res, `delete ${path}`, true);
2766
+ return res;
2767
+ });
2768
+ }
2769
+ async *listFeedback({ runIds, feedbackKeys, feedbackSourceTypes } = {}) {
2770
+ const queryParams = new URLSearchParams();
2771
+ if (runIds) {
2772
+ for (const runId of runIds) {
2773
+ assertUuid(runId);
2774
+ queryParams.append("run", runId);
2775
+ }
2776
+ }
2777
+ if (feedbackKeys) {
2778
+ for (const key of feedbackKeys) {
2779
+ queryParams.append("key", key);
2780
+ }
2781
+ }
2782
+ if (feedbackSourceTypes) {
2783
+ for (const type of feedbackSourceTypes) {
2784
+ queryParams.append("source", type);
2785
+ }
2786
+ }
2787
+ for await (const feedbacks of this._getPaginated("/feedback", queryParams)) {
2788
+ yield* feedbacks;
2789
+ }
2790
+ }
2791
+ /**
2792
+ * Creates a presigned feedback token and URL.
2793
+ *
2794
+ * The token can be used to authorize feedback metrics without
2795
+ * needing an API key. This is useful for giving browser-based
2796
+ * applications the ability to submit feedback without needing
2797
+ * to expose an API key.
2798
+ *
2799
+ * @param runId The ID of the run.
2800
+ * @param feedbackKey The feedback key.
2801
+ * @param options Additional options for the token.
2802
+ * @param options.expiration The expiration time for the token.
2803
+ *
2804
+ * @returns A promise that resolves to a FeedbackIngestToken.
2805
+ */
2806
+ async createPresignedFeedbackToken(runId, feedbackKey, { expiration, feedbackConfig } = {}) {
2807
+ const body = {
2808
+ run_id: runId,
2809
+ feedback_key: feedbackKey,
2810
+ feedback_config: feedbackConfig
2811
+ };
2812
+ if (expiration) {
2813
+ if (typeof expiration === "string") {
2814
+ body["expires_at"] = expiration;
2815
+ } else if (expiration?.hours || expiration?.minutes || expiration?.days) {
2816
+ body["expires_in"] = expiration;
2817
+ }
2818
+ } else {
2819
+ body["expires_in"] = {
2820
+ hours: 3
2821
+ };
2822
+ }
2823
+ const serializedBody = JSON.stringify(body);
2824
+ const response = await this.caller.call(async () => {
2825
+ const res = await this._fetch(`${this.apiUrl}/feedback/tokens`, {
2826
+ method: "POST",
2827
+ headers: { ...this.headers, "Content-Type": "application/json" },
2828
+ signal: AbortSignal.timeout(this.timeout_ms),
2829
+ ...this.fetchOptions,
2830
+ body: serializedBody
2831
+ });
2832
+ await raiseForStatus(res, "create presigned feedback token");
2833
+ return res;
2834
+ });
2835
+ return await response.json();
2836
+ }
2837
+ async createComparativeExperiment({ name, experimentIds, referenceDatasetId, createdAt, description, metadata, id }) {
2838
+ if (experimentIds.length === 0) {
2839
+ throw new Error("At least one experiment is required");
2840
+ }
2841
+ if (!referenceDatasetId) {
2842
+ referenceDatasetId = (await this.readProject({
2843
+ projectId: experimentIds[0]
2844
+ })).reference_dataset_id;
2845
+ }
2846
+ if (!referenceDatasetId == null) {
2847
+ throw new Error("A reference dataset is required");
2848
+ }
2849
+ const body = {
2850
+ id,
2851
+ name,
2852
+ experiment_ids: experimentIds,
2853
+ reference_dataset_id: referenceDatasetId,
2854
+ description,
2855
+ created_at: (createdAt ?? /* @__PURE__ */ new Date())?.toISOString(),
2856
+ extra: {}
2857
+ };
2858
+ if (metadata)
2859
+ body.extra["metadata"] = metadata;
2860
+ const serializedBody = JSON.stringify(body);
2861
+ const response = await this.caller.call(async () => {
2862
+ const res = await this._fetch(`${this.apiUrl}/datasets/comparative`, {
2863
+ method: "POST",
2864
+ headers: { ...this.headers, "Content-Type": "application/json" },
2865
+ signal: AbortSignal.timeout(this.timeout_ms),
2866
+ ...this.fetchOptions,
2867
+ body: serializedBody
2868
+ });
2869
+ await raiseForStatus(res, "create comparative experiment");
2870
+ return res;
2871
+ });
2872
+ return response.json();
2873
+ }
2874
+ /**
2875
+ * Retrieves a list of presigned feedback tokens for a given run ID.
2876
+ * @param runId The ID of the run.
2877
+ * @returns An async iterable of FeedbackIngestToken objects.
2878
+ */
2879
+ async *listPresignedFeedbackTokens(runId) {
2880
+ assertUuid(runId);
2881
+ const params = new URLSearchParams({ run_id: runId });
2882
+ for await (const tokens of this._getPaginated("/feedback/tokens", params)) {
2883
+ yield* tokens;
2884
+ }
2885
+ }
2886
+ _selectEvalResults(results) {
2887
+ let results_;
2888
+ if ("results" in results) {
2889
+ results_ = results.results;
2890
+ } else if (Array.isArray(results)) {
2891
+ results_ = results;
2892
+ } else {
2893
+ results_ = [results];
2894
+ }
2895
+ return results_;
2896
+ }
2897
+ async _logEvaluationFeedback(evaluatorResponse, run, sourceInfo) {
2898
+ const evalResults = this._selectEvalResults(evaluatorResponse);
2899
+ const feedbacks = [];
2900
+ for (const res of evalResults) {
2901
+ let sourceInfo_ = sourceInfo || {};
2902
+ if (res.evaluatorInfo) {
2903
+ sourceInfo_ = { ...res.evaluatorInfo, ...sourceInfo_ };
2904
+ }
2905
+ let runId_ = null;
2906
+ if (res.targetRunId) {
2907
+ runId_ = res.targetRunId;
2908
+ } else if (run) {
2909
+ runId_ = run.id;
2910
+ }
2911
+ feedbacks.push(await this.createFeedback(runId_, res.key, {
2912
+ score: res.score,
2913
+ value: res.value,
2914
+ comment: res.comment,
2915
+ correction: res.correction,
2916
+ sourceInfo: sourceInfo_,
2917
+ sourceRunId: res.sourceRunId,
2918
+ feedbackConfig: res.feedbackConfig,
2919
+ feedbackSourceType: "model"
2920
+ }));
2921
+ }
2922
+ return [evalResults, feedbacks];
2923
+ }
2924
+ async logEvaluationFeedback(evaluatorResponse, run, sourceInfo) {
2925
+ const [results] = await this._logEvaluationFeedback(evaluatorResponse, run, sourceInfo);
2926
+ return results;
2927
+ }
2928
+ /**
2929
+ * API for managing annotation queues
2930
+ */
2931
+ /**
2932
+ * List the annotation queues on the LangSmith API.
2933
+ * @param options - The options for listing annotation queues
2934
+ * @param options.queueIds - The IDs of the queues to filter by
2935
+ * @param options.name - The name of the queue to filter by
2936
+ * @param options.nameContains - The substring that the queue name should contain
2937
+ * @param options.limit - The maximum number of queues to return
2938
+ * @returns An iterator of AnnotationQueue objects
2939
+ */
2940
+ async *listAnnotationQueues(options = {}) {
2941
+ const { queueIds, name, nameContains, limit } = options;
2942
+ const params = new URLSearchParams();
2943
+ if (queueIds) {
2944
+ queueIds.forEach((id, i) => {
2945
+ assertUuid(id, `queueIds[${i}]`);
2946
+ params.append("ids", id);
2947
+ });
2948
+ }
2949
+ if (name)
2950
+ params.append("name", name);
2951
+ if (nameContains)
2952
+ params.append("name_contains", nameContains);
2953
+ params.append("limit", (limit !== void 0 ? Math.min(limit, 100) : 100).toString());
2954
+ let count = 0;
2955
+ for await (const queues of this._getPaginated("/annotation-queues", params)) {
2956
+ yield* queues;
2957
+ count++;
2958
+ if (limit !== void 0 && count >= limit)
2959
+ break;
2960
+ }
2961
+ }
2962
+ /**
2963
+ * Create an annotation queue on the LangSmith API.
2964
+ * @param options - The options for creating an annotation queue
2965
+ * @param options.name - The name of the annotation queue
2966
+ * @param options.description - The description of the annotation queue
2967
+ * @param options.queueId - The ID of the annotation queue
2968
+ * @returns The created AnnotationQueue object
2969
+ */
2970
+ async createAnnotationQueue(options) {
2971
+ const { name, description, queueId, rubricInstructions } = options;
2972
+ const body = {
2973
+ name,
2974
+ description,
2975
+ id: queueId || uuid.v4(),
2976
+ rubric_instructions: rubricInstructions
2977
+ };
2978
+ const serializedBody = JSON.stringify(Object.fromEntries(Object.entries(body).filter(([_, v]) => v !== void 0)));
2979
+ const response = await this.caller.call(async () => {
2980
+ const res = await this._fetch(`${this.apiUrl}/annotation-queues`, {
2981
+ method: "POST",
2982
+ headers: { ...this.headers, "Content-Type": "application/json" },
2983
+ signal: AbortSignal.timeout(this.timeout_ms),
2984
+ ...this.fetchOptions,
2985
+ body: serializedBody
2986
+ });
2987
+ await raiseForStatus(res, "create annotation queue");
2988
+ return res;
2989
+ });
2990
+ return response.json();
2991
+ }
2992
+ /**
2993
+ * Read an annotation queue with the specified queue ID.
2994
+ * @param queueId - The ID of the annotation queue to read
2995
+ * @returns The AnnotationQueueWithDetails object
2996
+ */
2997
+ async readAnnotationQueue(queueId) {
2998
+ const response = await this.caller.call(async () => {
2999
+ const res = await this._fetch(`${this.apiUrl}/annotation-queues/${assertUuid(queueId, "queueId")}`, {
3000
+ method: "GET",
3001
+ headers: this.headers,
3002
+ signal: AbortSignal.timeout(this.timeout_ms),
3003
+ ...this.fetchOptions
3004
+ });
3005
+ await raiseForStatus(res, "read annotation queue");
3006
+ return res;
3007
+ });
3008
+ return response.json();
3009
+ }
3010
+ /**
3011
+ * Update an annotation queue with the specified queue ID.
3012
+ * @param queueId - The ID of the annotation queue to update
3013
+ * @param options - The options for updating the annotation queue
3014
+ * @param options.name - The new name for the annotation queue
3015
+ * @param options.description - The new description for the annotation queue
3016
+ */
3017
+ async updateAnnotationQueue(queueId, options) {
3018
+ const { name, description, rubricInstructions } = options;
3019
+ const body = JSON.stringify({
3020
+ name,
3021
+ description,
3022
+ rubric_instructions: rubricInstructions
3023
+ });
3024
+ await this.caller.call(async () => {
3025
+ const res = await this._fetch(`${this.apiUrl}/annotation-queues/${assertUuid(queueId, "queueId")}`, {
3026
+ method: "PATCH",
3027
+ headers: { ...this.headers, "Content-Type": "application/json" },
3028
+ signal: AbortSignal.timeout(this.timeout_ms),
3029
+ ...this.fetchOptions,
3030
+ body
3031
+ });
3032
+ await raiseForStatus(res, "update annotation queue", true);
3033
+ return res;
3034
+ });
3035
+ }
3036
+ /**
3037
+ * Delete an annotation queue with the specified queue ID.
3038
+ * @param queueId - The ID of the annotation queue to delete
3039
+ */
3040
+ async deleteAnnotationQueue(queueId) {
3041
+ await this.caller.call(async () => {
3042
+ const res = await this._fetch(`${this.apiUrl}/annotation-queues/${assertUuid(queueId, "queueId")}`, {
3043
+ method: "DELETE",
3044
+ headers: { ...this.headers, Accept: "application/json" },
3045
+ signal: AbortSignal.timeout(this.timeout_ms),
3046
+ ...this.fetchOptions
3047
+ });
3048
+ await raiseForStatus(res, "delete annotation queue", true);
3049
+ return res;
3050
+ });
3051
+ }
3052
+ /**
3053
+ * Add runs to an annotation queue with the specified queue ID.
3054
+ * @param queueId - The ID of the annotation queue
3055
+ * @param runIds - The IDs of the runs to be added to the annotation queue
3056
+ */
3057
+ async addRunsToAnnotationQueue(queueId, runIds) {
3058
+ const body = JSON.stringify(runIds.map((id, i) => assertUuid(id, `runIds[${i}]`).toString()));
3059
+ await this.caller.call(async () => {
3060
+ const res = await this._fetch(`${this.apiUrl}/annotation-queues/${assertUuid(queueId, "queueId")}/runs`, {
3061
+ method: "POST",
3062
+ headers: { ...this.headers, "Content-Type": "application/json" },
3063
+ signal: AbortSignal.timeout(this.timeout_ms),
3064
+ ...this.fetchOptions,
3065
+ body
3066
+ });
3067
+ await raiseForStatus(res, "add runs to annotation queue", true);
3068
+ return res;
3069
+ });
3070
+ }
3071
+ /**
3072
+ * Get a run from an annotation queue at the specified index.
3073
+ * @param queueId - The ID of the annotation queue
3074
+ * @param index - The index of the run to retrieve
3075
+ * @returns A Promise that resolves to a RunWithAnnotationQueueInfo object
3076
+ * @throws {Error} If the run is not found at the given index or for other API-related errors
3077
+ */
3078
+ async getRunFromAnnotationQueue(queueId, index) {
3079
+ const baseUrl = `/annotation-queues/${assertUuid(queueId, "queueId")}/run`;
3080
+ const response = await this.caller.call(async () => {
3081
+ const res = await this._fetch(`${this.apiUrl}${baseUrl}/${index}`, {
3082
+ method: "GET",
3083
+ headers: this.headers,
3084
+ signal: AbortSignal.timeout(this.timeout_ms),
3085
+ ...this.fetchOptions
3086
+ });
3087
+ await raiseForStatus(res, "get run from annotation queue");
3088
+ return res;
3089
+ });
3090
+ return response.json();
3091
+ }
3092
+ /**
3093
+ * Delete a run from an an annotation queue.
3094
+ * @param queueId - The ID of the annotation queue to delete the run from
3095
+ * @param queueRunId - The ID of the run to delete from the annotation queue
3096
+ */
3097
+ async deleteRunFromAnnotationQueue(queueId, queueRunId) {
3098
+ await this.caller.call(async () => {
3099
+ const res = await this._fetch(`${this.apiUrl}/annotation-queues/${assertUuid(queueId, "queueId")}/runs/${assertUuid(queueRunId, "queueRunId")}`, {
3100
+ method: "DELETE",
3101
+ headers: { ...this.headers, Accept: "application/json" },
3102
+ signal: AbortSignal.timeout(this.timeout_ms),
3103
+ ...this.fetchOptions
3104
+ });
3105
+ await raiseForStatus(res, "delete run from annotation queue", true);
3106
+ return res;
3107
+ });
3108
+ }
3109
+ /**
3110
+ * Get the size of an annotation queue.
3111
+ * @param queueId - The ID of the annotation queue
3112
+ */
3113
+ async getSizeFromAnnotationQueue(queueId) {
3114
+ const response = await this.caller.call(async () => {
3115
+ const res = await this._fetch(`${this.apiUrl}/annotation-queues/${assertUuid(queueId, "queueId")}/size`, {
3116
+ method: "GET",
3117
+ headers: this.headers,
3118
+ signal: AbortSignal.timeout(this.timeout_ms),
3119
+ ...this.fetchOptions
3120
+ });
3121
+ await raiseForStatus(res, "get size from annotation queue");
3122
+ return res;
3123
+ });
3124
+ return response.json();
3125
+ }
3126
+ async _currentTenantIsOwner(owner) {
3127
+ const settings = await this._getSettings();
3128
+ return owner == "-" || settings.tenant_handle === owner;
3129
+ }
3130
+ async _ownerConflictError(action, owner) {
3131
+ const settings = await this._getSettings();
3132
+ return new Error(`Cannot ${action} for another tenant.
3133
+
3134
+ Current tenant: ${settings.tenant_handle}
3135
+
3136
+ Requested tenant: ${owner}`);
3137
+ }
3138
+ async _getLatestCommitHash(promptOwnerAndName) {
3139
+ const response = await this.caller.call(async () => {
3140
+ const res = await this._fetch(`${this.apiUrl}/commits/${promptOwnerAndName}/?limit=${1}&offset=${0}`, {
3141
+ method: "GET",
3142
+ headers: this.headers,
3143
+ signal: AbortSignal.timeout(this.timeout_ms),
3144
+ ...this.fetchOptions
3145
+ });
3146
+ await raiseForStatus(res, "get latest commit hash");
3147
+ return res;
3148
+ });
3149
+ const json = await response.json();
3150
+ if (json.commits.length === 0) {
3151
+ return void 0;
3152
+ }
3153
+ return json.commits[0].commit_hash;
3154
+ }
3155
+ async _likeOrUnlikePrompt(promptIdentifier, like) {
3156
+ const [owner, promptName, _] = parsePromptIdentifier(promptIdentifier);
3157
+ const body = JSON.stringify({ like });
3158
+ const response = await this.caller.call(async () => {
3159
+ const res = await this._fetch(`${this.apiUrl}/likes/${owner}/${promptName}`, {
3160
+ method: "POST",
3161
+ headers: { ...this.headers, "Content-Type": "application/json" },
3162
+ signal: AbortSignal.timeout(this.timeout_ms),
3163
+ ...this.fetchOptions,
3164
+ body
3165
+ });
3166
+ await raiseForStatus(res, `${like ? "like" : "unlike"} prompt`);
3167
+ return res;
3168
+ });
3169
+ return response.json();
3170
+ }
3171
+ async _getPromptUrl(promptIdentifier) {
3172
+ const [owner, promptName, commitHash] = parsePromptIdentifier(promptIdentifier);
3173
+ if (!await this._currentTenantIsOwner(owner)) {
3174
+ if (commitHash !== "latest") {
3175
+ return `${this.getHostUrl()}/hub/${owner}/${promptName}/${commitHash.substring(0, 8)}`;
3176
+ } else {
3177
+ return `${this.getHostUrl()}/hub/${owner}/${promptName}`;
3178
+ }
3179
+ } else {
3180
+ const settings = await this._getSettings();
3181
+ if (commitHash !== "latest") {
3182
+ return `${this.getHostUrl()}/prompts/${promptName}/${commitHash.substring(0, 8)}?organizationId=${settings.id}`;
3183
+ } else {
3184
+ return `${this.getHostUrl()}/prompts/${promptName}?organizationId=${settings.id}`;
3185
+ }
3186
+ }
3187
+ }
3188
+ async promptExists(promptIdentifier) {
3189
+ const prompt = await this.getPrompt(promptIdentifier);
3190
+ return !!prompt;
3191
+ }
3192
+ async likePrompt(promptIdentifier) {
3193
+ return this._likeOrUnlikePrompt(promptIdentifier, true);
3194
+ }
3195
+ async unlikePrompt(promptIdentifier) {
3196
+ return this._likeOrUnlikePrompt(promptIdentifier, false);
3197
+ }
3198
+ async *listCommits(promptOwnerAndName) {
3199
+ for await (const commits of this._getPaginated(`/commits/${promptOwnerAndName}/`, new URLSearchParams(), (res) => res.commits)) {
3200
+ yield* commits;
3201
+ }
3202
+ }
3203
+ async *listPrompts(options) {
3204
+ const params = new URLSearchParams();
3205
+ params.append("sort_field", options?.sortField ?? "updated_at");
3206
+ params.append("sort_direction", "desc");
3207
+ params.append("is_archived", (!!options?.isArchived).toString());
3208
+ if (options?.isPublic !== void 0) {
3209
+ params.append("is_public", options.isPublic.toString());
3210
+ }
3211
+ if (options?.query) {
3212
+ params.append("query", options.query);
3213
+ }
3214
+ for await (const prompts of this._getPaginated("/repos", params, (res) => res.repos)) {
3215
+ yield* prompts;
3216
+ }
3217
+ }
3218
+ async getPrompt(promptIdentifier) {
3219
+ const [owner, promptName, _] = parsePromptIdentifier(promptIdentifier);
3220
+ const response = await this.caller.call(async () => {
3221
+ const res = await this._fetch(`${this.apiUrl}/repos/${owner}/${promptName}`, {
3222
+ method: "GET",
3223
+ headers: this.headers,
3224
+ signal: AbortSignal.timeout(this.timeout_ms),
3225
+ ...this.fetchOptions
3226
+ });
3227
+ if (res?.status === 404) {
3228
+ return null;
3229
+ }
3230
+ await raiseForStatus(res, "get prompt");
3231
+ return res;
3232
+ });
3233
+ const result = await response?.json();
3234
+ if (result?.repo) {
3235
+ return result.repo;
3236
+ } else {
3237
+ return null;
3238
+ }
3239
+ }
3240
+ async createPrompt(promptIdentifier, options) {
3241
+ const settings = await this._getSettings();
3242
+ if (options?.isPublic && !settings.tenant_handle) {
3243
+ throw new Error(`Cannot create a public prompt without first
3244
+
3245
+ creating a LangChain Hub handle.
3246
+ You can add a handle by creating a public prompt at:
3247
+
3248
+ https://smith.langchain.com/prompts`);
3249
+ }
3250
+ const [owner, promptName, _] = parsePromptIdentifier(promptIdentifier);
3251
+ if (!await this._currentTenantIsOwner(owner)) {
3252
+ throw await this._ownerConflictError("create a prompt", owner);
3253
+ }
3254
+ const data = {
3255
+ repo_handle: promptName,
3256
+ ...options?.description && { description: options.description },
3257
+ ...options?.readme && { readme: options.readme },
3258
+ ...options?.tags && { tags: options.tags },
3259
+ is_public: !!options?.isPublic
3260
+ };
3261
+ const body = JSON.stringify(data);
3262
+ const response = await this.caller.call(async () => {
3263
+ const res = await this._fetch(`${this.apiUrl}/repos/`, {
3264
+ method: "POST",
3265
+ headers: { ...this.headers, "Content-Type": "application/json" },
3266
+ signal: AbortSignal.timeout(this.timeout_ms),
3267
+ ...this.fetchOptions,
3268
+ body
3269
+ });
3270
+ await raiseForStatus(res, "create prompt");
3271
+ return res;
3272
+ });
3273
+ const { repo } = await response.json();
3274
+ return repo;
3275
+ }
3276
+ async createCommit(promptIdentifier, object, options) {
3277
+ if (!await this.promptExists(promptIdentifier)) {
3278
+ throw new Error("Prompt does not exist, you must create it first.");
3279
+ }
3280
+ const [owner, promptName, _] = parsePromptIdentifier(promptIdentifier);
3281
+ const resolvedParentCommitHash = options?.parentCommitHash === "latest" || !options?.parentCommitHash ? await this._getLatestCommitHash(`${owner}/${promptName}`) : options?.parentCommitHash;
3282
+ const payload = {
3283
+ manifest: JSON.parse(JSON.stringify(object)),
3284
+ parent_commit: resolvedParentCommitHash
3285
+ };
3286
+ const body = JSON.stringify(payload);
3287
+ const response = await this.caller.call(async () => {
3288
+ const res = await this._fetch(`${this.apiUrl}/commits/${owner}/${promptName}`, {
3289
+ method: "POST",
3290
+ headers: { ...this.headers, "Content-Type": "application/json" },
3291
+ signal: AbortSignal.timeout(this.timeout_ms),
3292
+ ...this.fetchOptions,
3293
+ body
3294
+ });
3295
+ await raiseForStatus(res, "create commit");
3296
+ return res;
3297
+ });
3298
+ const result = await response.json();
3299
+ return this._getPromptUrl(`${owner}/${promptName}${result.commit_hash ? `:${result.commit_hash}` : ""}`);
3300
+ }
3301
+ /**
3302
+ * Update examples with attachments using multipart form data.
3303
+ * @param updates List of ExampleUpdateWithAttachments objects to upsert
3304
+ * @returns Promise with the update response
3305
+ */
3306
+ async updateExamplesMultipart(datasetId, updates = []) {
3307
+ return this._updateExamplesMultipart(datasetId, updates);
3308
+ }
3309
+ async _updateExamplesMultipart(datasetId, updates = []) {
3310
+ if (!await this._getDatasetExamplesMultiPartSupport()) {
3311
+ throw new Error("Your LangSmith deployment does not allow using the multipart examples endpoint, please upgrade your deployment to the latest version.");
3312
+ }
3313
+ const formData = new FormData();
3314
+ for (const example of updates) {
3315
+ const exampleId = example.id;
3316
+ const exampleBody = {
3317
+ ...example.metadata && { metadata: example.metadata },
3318
+ ...example.split && { split: example.split }
3319
+ };
3320
+ const stringifiedExample = serialize(exampleBody, `Serializing body for example with id: ${exampleId}`);
3321
+ const exampleBlob = new Blob([stringifiedExample], {
3322
+ type: "application/json"
3323
+ });
3324
+ formData.append(exampleId, exampleBlob);
3325
+ if (example.inputs) {
3326
+ const stringifiedInputs = serialize(example.inputs, `Serializing inputs for example with id: ${exampleId}`);
3327
+ const inputsBlob = new Blob([stringifiedInputs], {
3328
+ type: "application/json"
3329
+ });
3330
+ formData.append(`${exampleId}.inputs`, inputsBlob);
3331
+ }
3332
+ if (example.outputs) {
3333
+ const stringifiedOutputs = serialize(example.outputs, `Serializing outputs whle updating example with id: ${exampleId}`);
3334
+ const outputsBlob = new Blob([stringifiedOutputs], {
3335
+ type: "application/json"
3336
+ });
3337
+ formData.append(`${exampleId}.outputs`, outputsBlob);
3338
+ }
3339
+ if (example.attachments) {
3340
+ for (const [name, attachment] of Object.entries(example.attachments)) {
3341
+ let mimeType;
3342
+ let data;
3343
+ if (Array.isArray(attachment)) {
3344
+ [mimeType, data] = attachment;
3345
+ } else {
3346
+ mimeType = attachment.mimeType;
3347
+ data = attachment.data;
3348
+ }
3349
+ const attachmentBlob = new Blob([data], {
3350
+ type: `${mimeType}; length=${data.byteLength}`
3351
+ });
3352
+ formData.append(`${exampleId}.attachment.${name}`, attachmentBlob);
3353
+ }
3354
+ }
3355
+ if (example.attachments_operations) {
3356
+ const stringifiedAttachmentsOperations = serialize(example.attachments_operations, `Serializing attachments while updating example with id: ${exampleId}`);
3357
+ const attachmentsOperationsBlob = new Blob([stringifiedAttachmentsOperations], {
3358
+ type: "application/json"
3359
+ });
3360
+ formData.append(`${exampleId}.attachments_operations`, attachmentsOperationsBlob);
3361
+ }
3362
+ }
3363
+ const datasetIdToUse = datasetId ?? updates[0]?.dataset_id;
3364
+ const response = await this.caller.call(async () => {
3365
+ const res = await this._fetch(`${this.apiUrl}${this._getPlatformEndpointPath(`datasets/${datasetIdToUse}/examples`)}`, {
3366
+ method: "PATCH",
3367
+ headers: this.headers,
3368
+ signal: AbortSignal.timeout(this.timeout_ms),
3369
+ ...this.fetchOptions,
3370
+ body: formData
3371
+ });
3372
+ await raiseForStatus(res, "update examples");
3373
+ return res;
3374
+ });
3375
+ return response.json();
3376
+ }
3377
+ /**
3378
+ * Upload examples with attachments using multipart form data.
3379
+ * @param uploads List of ExampleUploadWithAttachments objects to upload
3380
+ * @returns Promise with the upload response
3381
+ * @deprecated This method is deprecated and will be removed in future LangSmith versions, please use `createExamples` instead
3382
+ */
3383
+ async uploadExamplesMultipart(datasetId, uploads = []) {
3384
+ return this._uploadExamplesMultipart(datasetId, uploads);
3385
+ }
3386
+ async _uploadExamplesMultipart(datasetId, uploads = []) {
3387
+ if (!await this._getDatasetExamplesMultiPartSupport()) {
3388
+ throw new Error("Your LangSmith deployment does not allow using the multipart examples endpoint, please upgrade your deployment to the latest version.");
3389
+ }
3390
+ const formData = new FormData();
3391
+ for (const example of uploads) {
3392
+ const exampleId = (example.id ?? uuid.v4()).toString();
3393
+ const exampleBody = {
3394
+ created_at: example.created_at,
3395
+ ...example.metadata && { metadata: example.metadata },
3396
+ ...example.split && { split: example.split },
3397
+ ...example.source_run_id && { source_run_id: example.source_run_id },
3398
+ ...example.use_source_run_io && {
3399
+ use_source_run_io: example.use_source_run_io
3400
+ },
3401
+ ...example.use_source_run_attachments && {
3402
+ use_source_run_attachments: example.use_source_run_attachments
3403
+ }
3404
+ };
3405
+ const stringifiedExample = serialize(exampleBody, `Serializing body for uploaded example with id: ${exampleId}`);
3406
+ const exampleBlob = new Blob([stringifiedExample], {
3407
+ type: "application/json"
3408
+ });
3409
+ formData.append(exampleId, exampleBlob);
3410
+ if (example.inputs) {
3411
+ const stringifiedInputs = serialize(example.inputs, `Serializing inputs for uploaded example with id: ${exampleId}`);
3412
+ const inputsBlob = new Blob([stringifiedInputs], {
3413
+ type: "application/json"
3414
+ });
3415
+ formData.append(`${exampleId}.inputs`, inputsBlob);
3416
+ }
3417
+ if (example.outputs) {
3418
+ const stringifiedOutputs = serialize(example.outputs, `Serializing outputs for uploaded example with id: ${exampleId}`);
3419
+ const outputsBlob = new Blob([stringifiedOutputs], {
3420
+ type: "application/json"
3421
+ });
3422
+ formData.append(`${exampleId}.outputs`, outputsBlob);
3423
+ }
3424
+ if (example.attachments) {
3425
+ for (const [name, attachment] of Object.entries(example.attachments)) {
3426
+ let mimeType;
3427
+ let data;
3428
+ if (Array.isArray(attachment)) {
3429
+ [mimeType, data] = attachment;
3430
+ } else {
3431
+ mimeType = attachment.mimeType;
3432
+ data = attachment.data;
3433
+ }
3434
+ const attachmentBlob = new Blob([data], {
3435
+ type: `${mimeType}; length=${data.byteLength}`
3436
+ });
3437
+ formData.append(`${exampleId}.attachment.${name}`, attachmentBlob);
3438
+ }
3439
+ }
3440
+ }
3441
+ const response = await this.caller.call(async () => {
3442
+ const res = await this._fetch(`${this.apiUrl}${this._getPlatformEndpointPath(`datasets/${datasetId}/examples`)}`, {
3443
+ method: "POST",
3444
+ headers: this.headers,
3445
+ signal: AbortSignal.timeout(this.timeout_ms),
3446
+ ...this.fetchOptions,
3447
+ body: formData
3448
+ });
3449
+ await raiseForStatus(res, "upload examples");
3450
+ return res;
3451
+ });
3452
+ return response.json();
3453
+ }
3454
+ async updatePrompt(promptIdentifier, options) {
3455
+ if (!await this.promptExists(promptIdentifier)) {
3456
+ throw new Error("Prompt does not exist, you must create it first.");
3457
+ }
3458
+ const [owner, promptName] = parsePromptIdentifier(promptIdentifier);
3459
+ if (!await this._currentTenantIsOwner(owner)) {
3460
+ throw await this._ownerConflictError("update a prompt", owner);
3461
+ }
3462
+ const payload = {};
3463
+ if (options?.description !== void 0)
3464
+ payload.description = options.description;
3465
+ if (options?.readme !== void 0)
3466
+ payload.readme = options.readme;
3467
+ if (options?.tags !== void 0)
3468
+ payload.tags = options.tags;
3469
+ if (options?.isPublic !== void 0)
3470
+ payload.is_public = options.isPublic;
3471
+ if (options?.isArchived !== void 0)
3472
+ payload.is_archived = options.isArchived;
3473
+ if (Object.keys(payload).length === 0) {
3474
+ throw new Error("No valid update options provided");
3475
+ }
3476
+ const body = JSON.stringify(payload);
3477
+ const response = await this.caller.call(async () => {
3478
+ const res = await this._fetch(`${this.apiUrl}/repos/${owner}/${promptName}`, {
3479
+ method: "PATCH",
3480
+ headers: {
3481
+ ...this.headers,
3482
+ "Content-Type": "application/json"
3483
+ },
3484
+ signal: AbortSignal.timeout(this.timeout_ms),
3485
+ ...this.fetchOptions,
3486
+ body
3487
+ });
3488
+ await raiseForStatus(res, "update prompt");
3489
+ return res;
3490
+ });
3491
+ return response.json();
3492
+ }
3493
+ async deletePrompt(promptIdentifier) {
3494
+ if (!await this.promptExists(promptIdentifier)) {
3495
+ throw new Error("Prompt does not exist, you must create it first.");
3496
+ }
3497
+ const [owner, promptName, _] = parsePromptIdentifier(promptIdentifier);
3498
+ if (!await this._currentTenantIsOwner(owner)) {
3499
+ throw await this._ownerConflictError("delete a prompt", owner);
3500
+ }
3501
+ const response = await this.caller.call(async () => {
3502
+ const res = await this._fetch(`${this.apiUrl}/repos/${owner}/${promptName}`, {
3503
+ method: "DELETE",
3504
+ headers: this.headers,
3505
+ signal: AbortSignal.timeout(this.timeout_ms),
3506
+ ...this.fetchOptions
3507
+ });
3508
+ await raiseForStatus(res, "delete prompt");
3509
+ return res;
3510
+ });
3511
+ return response.json();
3512
+ }
3513
+ async pullPromptCommit(promptIdentifier, options) {
3514
+ const [owner, promptName, commitHash] = parsePromptIdentifier(promptIdentifier);
3515
+ const response = await this.caller.call(async () => {
3516
+ const res = await this._fetch(`${this.apiUrl}/commits/${owner}/${promptName}/${commitHash}${options?.includeModel ? "?include_model=true" : ""}`, {
3517
+ method: "GET",
3518
+ headers: this.headers,
3519
+ signal: AbortSignal.timeout(this.timeout_ms),
3520
+ ...this.fetchOptions
3521
+ });
3522
+ await raiseForStatus(res, "pull prompt commit");
3523
+ return res;
3524
+ });
3525
+ const result = await response.json();
3526
+ return {
3527
+ owner,
3528
+ repo: promptName,
3529
+ commit_hash: result.commit_hash,
3530
+ manifest: result.manifest,
3531
+ examples: result.examples
3532
+ };
3533
+ }
3534
+ /**
3535
+ * This method should not be used directly, use `import { pull } from "langchain/hub"` instead.
3536
+ * Using this method directly returns the JSON string of the prompt rather than a LangChain object.
3537
+ * @private
3538
+ */
3539
+ async _pullPrompt(promptIdentifier, options) {
3540
+ const promptObject = await this.pullPromptCommit(promptIdentifier, {
3541
+ includeModel: options?.includeModel
3542
+ });
3543
+ const prompt = JSON.stringify(promptObject.manifest);
3544
+ return prompt;
3545
+ }
3546
+ async pushPrompt(promptIdentifier, options) {
3547
+ if (await this.promptExists(promptIdentifier)) {
3548
+ if (options && Object.keys(options).some((key) => key !== "object")) {
3549
+ await this.updatePrompt(promptIdentifier, {
3550
+ description: options?.description,
3551
+ readme: options?.readme,
3552
+ tags: options?.tags,
3553
+ isPublic: options?.isPublic
3554
+ });
3555
+ }
3556
+ } else {
3557
+ await this.createPrompt(promptIdentifier, {
3558
+ description: options?.description,
3559
+ readme: options?.readme,
3560
+ tags: options?.tags,
3561
+ isPublic: options?.isPublic
3562
+ });
3563
+ }
3564
+ if (!options?.object) {
3565
+ return await this._getPromptUrl(promptIdentifier);
3566
+ }
3567
+ const url = await this.createCommit(promptIdentifier, options?.object, {
3568
+ parentCommitHash: options?.parentCommitHash
3569
+ });
3570
+ return url;
3571
+ }
3572
+ /**
3573
+ * Clone a public dataset to your own langsmith tenant.
3574
+ * This operation is idempotent. If you already have a dataset with the given name,
3575
+ * this function will do nothing.
3576
+
3577
+ * @param {string} tokenOrUrl The token of the public dataset to clone.
3578
+ * @param {Object} [options] Additional options for cloning the dataset.
3579
+ * @param {string} [options.sourceApiUrl] The URL of the langsmith server where the data is hosted. Defaults to the API URL of your current client.
3580
+ * @param {string} [options.datasetName] The name of the dataset to create in your tenant. Defaults to the name of the public dataset.
3581
+ * @returns {Promise<void>}
3582
+ */
3583
+ async clonePublicDataset(tokenOrUrl, options = {}) {
3584
+ const { sourceApiUrl = this.apiUrl, datasetName } = options;
3585
+ const [parsedApiUrl, tokenUuid] = this.parseTokenOrUrl(tokenOrUrl, sourceApiUrl);
3586
+ const sourceClient = new Client({
3587
+ apiUrl: parsedApiUrl,
3588
+ // Placeholder API key not needed anymore in most cases, but
3589
+ // some private deployments may have API key-based rate limiting
3590
+ // that would cause this to fail if we provide no value.
3591
+ apiKey: "placeholder"
3592
+ });
3593
+ const ds = await sourceClient.readSharedDataset(tokenUuid);
3594
+ const finalDatasetName = datasetName || ds.name;
3595
+ try {
3596
+ if (await this.hasDataset({ datasetId: finalDatasetName })) {
3597
+ console.log(`Dataset ${finalDatasetName} already exists in your tenant. Skipping.`);
3598
+ return;
3599
+ }
3600
+ } catch (_) {
3601
+ }
3602
+ const examples = await sourceClient.listSharedExamples(tokenUuid);
3603
+ const dataset = await this.createDataset(finalDatasetName, {
3604
+ description: ds.description,
3605
+ dataType: ds.data_type || "kv",
3606
+ inputsSchema: ds.inputs_schema_definition ?? void 0,
3607
+ outputsSchema: ds.outputs_schema_definition ?? void 0
3608
+ });
3609
+ try {
3610
+ await this.createExamples({
3611
+ inputs: examples.map((e) => e.inputs),
3612
+ outputs: examples.flatMap((e) => e.outputs ? [e.outputs] : []),
3613
+ datasetId: dataset.id
3614
+ });
3615
+ } catch (e) {
3616
+ console.error(`An error occurred while creating dataset ${finalDatasetName}. You should delete it manually.`);
3617
+ throw e;
3618
+ }
3619
+ }
3620
+ parseTokenOrUrl(urlOrToken, apiUrl, numParts = 2, kind = "dataset") {
3621
+ try {
3622
+ assertUuid(urlOrToken);
3623
+ return [apiUrl, urlOrToken];
3624
+ } catch (_) {
3625
+ }
3626
+ try {
3627
+ const parsedUrl = new URL(urlOrToken);
3628
+ const pathParts = parsedUrl.pathname.split("/").filter((part) => part !== "");
3629
+ if (pathParts.length >= numParts) {
3630
+ const tokenUuid = pathParts[pathParts.length - numParts];
3631
+ return [apiUrl, tokenUuid];
3632
+ } else {
3633
+ throw new Error(`Invalid public ${kind} URL: ${urlOrToken}`);
3634
+ }
3635
+ } catch (error) {
3636
+ throw new Error(`Invalid public ${kind} URL or token: ${urlOrToken}`);
3637
+ }
3638
+ }
3639
+ /**
3640
+ * Awaits all pending trace batches. Useful for environments where
3641
+ * you need to be sure that all tracing requests finish before execution ends,
3642
+ * such as serverless environments.
3643
+ *
3644
+ * @example
3645
+ * ```
3646
+ * import { Client } from "langsmith";
3647
+ *
3648
+ * const client = new Client();
3649
+ *
3650
+ * try {
3651
+ * // Tracing happens here
3652
+ * ...
3653
+ * } finally {
3654
+ * await client.awaitPendingTraceBatches();
3655
+ * }
3656
+ * ```
3657
+ *
3658
+ * @returns A promise that resolves once all currently pending traces have sent.
3659
+ */
3660
+ async awaitPendingTraceBatches() {
3661
+ if (this.manualFlushMode) {
3662
+ console.warn("[WARNING]: When tracing in manual flush mode, you must call `await client.flush()` manually to submit trace batches.");
3663
+ return Promise.resolve();
3664
+ }
3665
+ await new Promise((resolve) => setTimeout(resolve, 1));
3666
+ await Promise.all([
3667
+ ...this.autoBatchQueue.items.map(({ itemPromise }) => itemPromise),
3668
+ this.batchIngestCaller.queue.onIdle()
3669
+ ]);
3670
+ if (this.langSmithToOTELTranslator !== void 0) {
3671
+ await getDefaultOTLPTracerComponents()?.DEFAULT_LANGSMITH_SPAN_PROCESSOR?.forceFlush();
3672
+ }
3673
+ }
3674
+ }
3675
+ function isExampleCreate(input) {
3676
+ return "dataset_id" in input || "dataset_name" in input;
3677
+ }
3678
+ export {
3679
+ AutoBatchQueue,
3680
+ Client,
3681
+ DEFAULT_MAX_SIZE_BYTES,
3682
+ DEFAULT_UNCOMPRESSED_BATCH_SIZE_LIMIT_BYTES,
3683
+ mergeRuntimeEnvIntoRun
3684
+ };
3685
+ //# sourceMappingURL=index.es166.js.map