openlit 1.13.0 → 1.14.0

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 (348) hide show
  1. package/README.md +34 -1
  2. package/dist/__tests__/manual-trace.test.js +118 -0
  3. package/dist/__tests__/manual-trace.test.js.map +1 -0
  4. package/dist/__tests__/register.test.js +90 -0
  5. package/dist/__tests__/register.test.js.map +1 -0
  6. package/dist/config.d.ts +7 -1
  7. package/dist/config.js +3 -1
  8. package/dist/config.js.map +1 -1
  9. package/dist/evals/__tests__/offline.test.js +418 -0
  10. package/dist/evals/__tests__/offline.test.js.map +1 -0
  11. package/dist/evals/index.d.ts +2 -5
  12. package/dist/evals/index.js +12 -26
  13. package/dist/evals/index.js.map +1 -1
  14. package/dist/evals/offline.d.ts +6 -0
  15. package/dist/evals/offline.js +347 -0
  16. package/dist/evals/offline.js.map +1 -0
  17. package/dist/evals/types.d.ts +69 -18
  18. package/dist/evals/types.js +20 -0
  19. package/dist/evals/types.js.map +1 -1
  20. package/dist/guard/__tests__/integration.test.js +178 -0
  21. package/dist/guard/__tests__/integration.test.js.map +1 -0
  22. package/dist/guard/__tests__/pii.test.js +109 -0
  23. package/dist/guard/__tests__/pii.test.js.map +1 -0
  24. package/dist/guard/__tests__/pipeline.test.js +162 -0
  25. package/dist/guard/__tests__/pipeline.test.js.map +1 -0
  26. package/dist/guard/__tests__/prompt-injection.test.js +92 -0
  27. package/dist/guard/__tests__/prompt-injection.test.js.map +1 -0
  28. package/dist/guard/base.d.ts +75 -14
  29. package/dist/guard/base.js +130 -53
  30. package/dist/guard/base.js.map +1 -1
  31. package/dist/guard/custom.d.ts +19 -0
  32. package/dist/guard/custom.js +46 -0
  33. package/dist/guard/custom.js.map +1 -0
  34. package/dist/guard/index.d.ts +24 -5
  35. package/dist/guard/index.js +39 -21
  36. package/dist/guard/index.js.map +1 -1
  37. package/dist/guard/integration.d.ts +32 -0
  38. package/dist/guard/integration.js +328 -0
  39. package/dist/guard/integration.js.map +1 -0
  40. package/dist/guard/moderation.d.ts +18 -0
  41. package/dist/guard/moderation.js +61 -0
  42. package/dist/guard/moderation.js.map +1 -0
  43. package/dist/guard/pii.d.ts +17 -0
  44. package/dist/guard/pii.js +94 -0
  45. package/dist/guard/pii.js.map +1 -0
  46. package/dist/guard/pipeline.d.ts +18 -0
  47. package/dist/guard/pipeline.js +96 -0
  48. package/dist/guard/pipeline.js.map +1 -0
  49. package/dist/guard/prompt-injection.d.ts +20 -6
  50. package/dist/guard/prompt-injection.js +59 -68
  51. package/dist/guard/prompt-injection.js.map +1 -1
  52. package/dist/guard/schema.d.ts +20 -0
  53. package/dist/guard/schema.js +99 -0
  54. package/dist/guard/schema.js.map +1 -0
  55. package/dist/guard/sensitive-topic.d.ts +22 -6
  56. package/dist/guard/sensitive-topic.js +78 -64
  57. package/dist/guard/sensitive-topic.js.map +1 -1
  58. package/dist/guard/topic-restriction.d.ts +21 -6
  59. package/dist/guard/topic-restriction.js +43 -64
  60. package/dist/guard/topic-restriction.js.map +1 -1
  61. package/dist/helpers.d.ts +133 -2
  62. package/dist/helpers.js +415 -5
  63. package/dist/helpers.js.map +1 -1
  64. package/dist/index.d.ts +54 -30
  65. package/dist/index.js +104 -22
  66. package/dist/index.js.map +1 -1
  67. package/dist/instrumentation/__tests__/ai21-rag-trace-comparison.test.d.ts +13 -0
  68. package/dist/instrumentation/__tests__/ai21-rag-trace-comparison.test.js +176 -0
  69. package/dist/instrumentation/__tests__/ai21-rag-trace-comparison.test.js.map +1 -0
  70. package/dist/instrumentation/__tests__/ai21-trace-comparison.test.d.ts +10 -0
  71. package/dist/instrumentation/__tests__/ai21-trace-comparison.test.js +203 -0
  72. package/dist/instrumentation/__tests__/ai21-trace-comparison.test.js.map +1 -0
  73. package/dist/instrumentation/__tests__/assemblyai-wrapper.test.js +262 -0
  74. package/dist/instrumentation/__tests__/assemblyai-wrapper.test.js.map +1 -0
  75. package/dist/instrumentation/__tests__/astra-trace-comparison.test.d.ts +7 -0
  76. package/dist/instrumentation/__tests__/astra-trace-comparison.test.js +160 -0
  77. package/dist/instrumentation/__tests__/astra-trace-comparison.test.js.map +1 -0
  78. package/dist/instrumentation/__tests__/browser-use-wrapper.test.js +284 -0
  79. package/dist/instrumentation/__tests__/browser-use-wrapper.test.js.map +1 -0
  80. package/dist/instrumentation/__tests__/cache-pricing.test.d.ts +12 -0
  81. package/dist/instrumentation/__tests__/cache-pricing.test.js +95 -0
  82. package/dist/instrumentation/__tests__/cache-pricing.test.js.map +1 -0
  83. package/dist/instrumentation/__tests__/elevenlabs-wrapper.test.d.ts +1 -0
  84. package/dist/instrumentation/__tests__/elevenlabs-wrapper.test.js +282 -0
  85. package/dist/instrumentation/__tests__/elevenlabs-wrapper.test.js.map +1 -0
  86. package/dist/instrumentation/__tests__/firecrawl-wrapper.test.d.ts +1 -0
  87. package/dist/instrumentation/__tests__/firecrawl-wrapper.test.js +238 -0
  88. package/dist/instrumentation/__tests__/firecrawl-wrapper.test.js.map +1 -0
  89. package/dist/instrumentation/__tests__/gpu-collector.test.d.ts +1 -0
  90. package/dist/instrumentation/__tests__/gpu-collector.test.js +247 -0
  91. package/dist/instrumentation/__tests__/gpu-collector.test.js.map +1 -0
  92. package/dist/instrumentation/__tests__/gradient-trace-comparison.test.d.ts +11 -0
  93. package/dist/instrumentation/__tests__/gradient-trace-comparison.test.js +391 -0
  94. package/dist/instrumentation/__tests__/gradient-trace-comparison.test.js.map +1 -0
  95. package/dist/instrumentation/__tests__/groq-trace-comparison.test.js +150 -0
  96. package/dist/instrumentation/__tests__/groq-trace-comparison.test.js.map +1 -1
  97. package/dist/instrumentation/__tests__/helpers-genai.test.d.ts +14 -0
  98. package/dist/instrumentation/__tests__/helpers-genai.test.js +347 -0
  99. package/dist/instrumentation/__tests__/helpers-genai.test.js.map +1 -0
  100. package/dist/instrumentation/__tests__/langchain-role-map.test.d.ts +1 -0
  101. package/dist/instrumentation/__tests__/langchain-role-map.test.js +19 -0
  102. package/dist/instrumentation/__tests__/langchain-role-map.test.js.map +1 -0
  103. package/dist/instrumentation/__tests__/mem0-trace-comparison.test.d.ts +13 -0
  104. package/dist/instrumentation/__tests__/mem0-trace-comparison.test.js +208 -0
  105. package/dist/instrumentation/__tests__/mem0-trace-comparison.test.js.map +1 -0
  106. package/dist/instrumentation/__tests__/pg-trace-comparison.test.d.ts +21 -0
  107. package/dist/instrumentation/__tests__/pg-trace-comparison.test.js +455 -0
  108. package/dist/instrumentation/__tests__/pg-trace-comparison.test.js.map +1 -0
  109. package/dist/instrumentation/__tests__/transformers-trace-comparison.test.d.ts +11 -0
  110. package/dist/instrumentation/__tests__/transformers-trace-comparison.test.js +214 -0
  111. package/dist/instrumentation/__tests__/transformers-trace-comparison.test.js.map +1 -0
  112. package/dist/instrumentation/__tests__/vertexai-trace-comparison.test.d.ts +6 -0
  113. package/dist/instrumentation/__tests__/vertexai-trace-comparison.test.js +409 -0
  114. package/dist/instrumentation/__tests__/vertexai-trace-comparison.test.js.map +1 -0
  115. package/dist/instrumentation/__tests__/vllm-trace-comparison.test.d.ts +1 -0
  116. package/dist/instrumentation/__tests__/vllm-trace-comparison.test.js +519 -0
  117. package/dist/instrumentation/__tests__/vllm-trace-comparison.test.js.map +1 -0
  118. package/dist/instrumentation/ai21/index.d.ts +11 -0
  119. package/dist/instrumentation/ai21/index.js +67 -0
  120. package/dist/instrumentation/ai21/index.js.map +1 -0
  121. package/dist/instrumentation/ai21/wrapper.d.ts +54 -0
  122. package/dist/instrumentation/ai21/wrapper.js +511 -0
  123. package/dist/instrumentation/ai21/wrapper.js.map +1 -0
  124. package/dist/instrumentation/anthropic/wrapper.js +56 -2
  125. package/dist/instrumentation/anthropic/wrapper.js.map +1 -1
  126. package/dist/instrumentation/assemblyai/index.d.ts +12 -0
  127. package/dist/instrumentation/assemblyai/index.js +91 -0
  128. package/dist/instrumentation/assemblyai/index.js.map +1 -0
  129. package/dist/instrumentation/assemblyai/wrapper.d.ts +35 -0
  130. package/dist/instrumentation/assemblyai/wrapper.js +233 -0
  131. package/dist/instrumentation/assemblyai/wrapper.js.map +1 -0
  132. package/dist/instrumentation/astra/index.d.ts +11 -0
  133. package/dist/instrumentation/astra/index.js +87 -0
  134. package/dist/instrumentation/astra/index.js.map +1 -0
  135. package/dist/instrumentation/astra/wrapper.d.ts +39 -0
  136. package/dist/instrumentation/astra/wrapper.js +349 -0
  137. package/dist/instrumentation/astra/wrapper.js.map +1 -0
  138. package/dist/instrumentation/azure-ai-inference/wrapper.d.ts +13 -0
  139. package/dist/instrumentation/azure-ai-inference/wrapper.js +60 -1
  140. package/dist/instrumentation/azure-ai-inference/wrapper.js.map +1 -1
  141. package/dist/instrumentation/bedrock/wrapper.d.ts +13 -0
  142. package/dist/instrumentation/bedrock/wrapper.js +78 -2
  143. package/dist/instrumentation/bedrock/wrapper.js.map +1 -1
  144. package/dist/instrumentation/browser-use/index.d.ts +11 -0
  145. package/dist/instrumentation/browser-use/index.js +98 -0
  146. package/dist/instrumentation/browser-use/index.js.map +1 -0
  147. package/dist/instrumentation/browser-use/wrapper.d.ts +12 -0
  148. package/dist/instrumentation/browser-use/wrapper.js +706 -0
  149. package/dist/instrumentation/browser-use/wrapper.js.map +1 -0
  150. package/dist/instrumentation/claude-agent-sdk/wrapper.js +90 -4
  151. package/dist/instrumentation/claude-agent-sdk/wrapper.js.map +1 -1
  152. package/dist/instrumentation/cohere/wrapper.d.ts +13 -0
  153. package/dist/instrumentation/cohere/wrapper.js +59 -0
  154. package/dist/instrumentation/cohere/wrapper.js.map +1 -1
  155. package/dist/instrumentation/cursor-sdk/wrapper.js +111 -15
  156. package/dist/instrumentation/cursor-sdk/wrapper.js.map +1 -1
  157. package/dist/instrumentation/elevenlabs/index.d.ts +12 -0
  158. package/dist/instrumentation/elevenlabs/index.js +92 -0
  159. package/dist/instrumentation/elevenlabs/index.js.map +1 -0
  160. package/dist/instrumentation/elevenlabs/wrapper.d.ts +34 -0
  161. package/dist/instrumentation/elevenlabs/wrapper.js +280 -0
  162. package/dist/instrumentation/elevenlabs/wrapper.js.map +1 -0
  163. package/dist/instrumentation/firecrawl/index.d.ts +13 -0
  164. package/dist/instrumentation/firecrawl/index.js +115 -0
  165. package/dist/instrumentation/firecrawl/index.js.map +1 -0
  166. package/dist/instrumentation/firecrawl/wrapper.d.ts +22 -0
  167. package/dist/instrumentation/firecrawl/wrapper.js +378 -0
  168. package/dist/instrumentation/firecrawl/wrapper.js.map +1 -0
  169. package/dist/instrumentation/google-ai/wrapper.d.ts +13 -0
  170. package/dist/instrumentation/google-ai/wrapper.js +85 -1
  171. package/dist/instrumentation/google-ai/wrapper.js.map +1 -1
  172. package/dist/instrumentation/gpu/index.d.ts +38 -0
  173. package/dist/instrumentation/gpu/index.js +233 -0
  174. package/dist/instrumentation/gpu/index.js.map +1 -0
  175. package/dist/instrumentation/gradient/index.d.ts +12 -0
  176. package/dist/instrumentation/gradient/index.js +74 -0
  177. package/dist/instrumentation/gradient/index.js.map +1 -0
  178. package/dist/instrumentation/gradient/utils.d.ts +14 -0
  179. package/dist/instrumentation/gradient/utils.js +119 -0
  180. package/dist/instrumentation/gradient/utils.js.map +1 -0
  181. package/dist/instrumentation/gradient/wrapper.d.ts +76 -0
  182. package/dist/instrumentation/gradient/wrapper.js +584 -0
  183. package/dist/instrumentation/gradient/wrapper.js.map +1 -0
  184. package/dist/instrumentation/groq/wrapper.js +42 -0
  185. package/dist/instrumentation/groq/wrapper.js.map +1 -1
  186. package/dist/instrumentation/huggingface/wrapper.d.ts +13 -0
  187. package/dist/instrumentation/huggingface/wrapper.js +66 -0
  188. package/dist/instrumentation/huggingface/wrapper.js.map +1 -1
  189. package/dist/instrumentation/index.js +26 -0
  190. package/dist/instrumentation/index.js.map +1 -1
  191. package/dist/instrumentation/langchain/wrapper.js +5 -8
  192. package/dist/instrumentation/langchain/wrapper.js.map +1 -1
  193. package/dist/instrumentation/langgraph/wrapper.js +12 -11
  194. package/dist/instrumentation/langgraph/wrapper.js.map +1 -1
  195. package/dist/instrumentation/llamaindex/wrapper.d.ts +14 -1
  196. package/dist/instrumentation/llamaindex/wrapper.js +68 -0
  197. package/dist/instrumentation/llamaindex/wrapper.js.map +1 -1
  198. package/dist/instrumentation/mcp/index.d.ts +20 -0
  199. package/dist/instrumentation/mcp/index.js +282 -0
  200. package/dist/instrumentation/mcp/index.js.map +1 -0
  201. package/dist/instrumentation/mcp/wrapper.d.ts +72 -0
  202. package/dist/instrumentation/mcp/wrapper.js +571 -0
  203. package/dist/instrumentation/mcp/wrapper.js.map +1 -0
  204. package/dist/instrumentation/mem0/index.d.ts +17 -0
  205. package/dist/instrumentation/mem0/index.js +113 -0
  206. package/dist/instrumentation/mem0/index.js.map +1 -0
  207. package/dist/instrumentation/mem0/wrapper.d.ts +37 -0
  208. package/dist/instrumentation/mem0/wrapper.js +298 -0
  209. package/dist/instrumentation/mem0/wrapper.js.map +1 -0
  210. package/dist/instrumentation/mistral/wrapper.d.ts +13 -0
  211. package/dist/instrumentation/mistral/wrapper.js +60 -1
  212. package/dist/instrumentation/mistral/wrapper.js.map +1 -1
  213. package/dist/instrumentation/ollama/wrapper.d.ts +13 -0
  214. package/dist/instrumentation/ollama/wrapper.js +67 -1
  215. package/dist/instrumentation/ollama/wrapper.js.map +1 -1
  216. package/dist/instrumentation/openai/wrapper.d.ts +13 -0
  217. package/dist/instrumentation/openai/wrapper.js +89 -3
  218. package/dist/instrumentation/openai/wrapper.js.map +1 -1
  219. package/dist/instrumentation/pg/index.d.ts +20 -0
  220. package/dist/instrumentation/pg/index.js +74 -0
  221. package/dist/instrumentation/pg/index.js.map +1 -0
  222. package/dist/instrumentation/pg/wrapper.d.ts +66 -0
  223. package/dist/instrumentation/pg/wrapper.js +441 -0
  224. package/dist/instrumentation/pg/wrapper.js.map +1 -0
  225. package/dist/instrumentation/replicate/wrapper.d.ts +13 -0
  226. package/dist/instrumentation/replicate/wrapper.js +61 -0
  227. package/dist/instrumentation/replicate/wrapper.js.map +1 -1
  228. package/dist/instrumentation/strands/processor.d.ts +6 -0
  229. package/dist/instrumentation/strands/processor.js +73 -3
  230. package/dist/instrumentation/strands/processor.js.map +1 -1
  231. package/dist/instrumentation/strands/utils.d.ts +2 -2
  232. package/dist/instrumentation/strands/utils.js +15 -2
  233. package/dist/instrumentation/strands/utils.js.map +1 -1
  234. package/dist/instrumentation/together/wrapper.d.ts +13 -0
  235. package/dist/instrumentation/together/wrapper.js +59 -1
  236. package/dist/instrumentation/together/wrapper.js.map +1 -1
  237. package/dist/instrumentation/transformers/index.d.ts +35 -0
  238. package/dist/instrumentation/transformers/index.js +119 -0
  239. package/dist/instrumentation/transformers/index.js.map +1 -0
  240. package/dist/instrumentation/transformers/wrapper.d.ts +49 -0
  241. package/dist/instrumentation/transformers/wrapper.js +472 -0
  242. package/dist/instrumentation/transformers/wrapper.js.map +1 -0
  243. package/dist/instrumentation/vercel-ai/wrapper.d.ts +13 -0
  244. package/dist/instrumentation/vercel-ai/wrapper.js +106 -1
  245. package/dist/instrumentation/vercel-ai/wrapper.js.map +1 -1
  246. package/dist/instrumentation/vertexai/index.d.ts +11 -0
  247. package/dist/instrumentation/vertexai/index.js +70 -0
  248. package/dist/instrumentation/vertexai/index.js.map +1 -0
  249. package/dist/instrumentation/vertexai/wrapper.d.ts +67 -0
  250. package/dist/instrumentation/vertexai/wrapper.js +529 -0
  251. package/dist/instrumentation/vertexai/wrapper.js.map +1 -0
  252. package/dist/instrumentation/vllm/index.d.ts +13 -0
  253. package/dist/instrumentation/vllm/index.js +67 -0
  254. package/dist/instrumentation/vllm/index.js.map +1 -0
  255. package/dist/instrumentation/vllm/wrapper.d.ts +60 -0
  256. package/dist/instrumentation/vllm/wrapper.js +482 -0
  257. package/dist/instrumentation/vllm/wrapper.js.map +1 -0
  258. package/dist/llm/index.d.ts +9 -2
  259. package/dist/llm/index.js.map +1 -1
  260. package/dist/manual-trace.d.ts +40 -0
  261. package/dist/manual-trace.js +103 -0
  262. package/dist/manual-trace.js.map +1 -0
  263. package/dist/otel/metrics.d.ts +11 -0
  264. package/dist/otel/metrics.js +48 -0
  265. package/dist/otel/metrics.js.map +1 -1
  266. package/dist/register.d.ts +4 -0
  267. package/dist/register.js +52 -0
  268. package/dist/register.js.map +1 -0
  269. package/dist/score/__tests__/score.test.d.ts +1 -0
  270. package/dist/score/__tests__/score.test.js +143 -0
  271. package/dist/score/__tests__/score.test.js.map +1 -0
  272. package/dist/score/index.d.ts +2 -0
  273. package/dist/score/index.js +6 -0
  274. package/dist/score/index.js.map +1 -0
  275. package/dist/score/score.d.ts +15 -0
  276. package/dist/score/score.js +135 -0
  277. package/dist/score/score.js.map +1 -0
  278. package/dist/semantic-convention.d.ts +273 -0
  279. package/dist/semantic-convention.js +317 -0
  280. package/dist/semantic-convention.js.map +1 -1
  281. package/dist/types.d.ts +15 -1
  282. package/package.json +18 -1
  283. package/dist/evals/__tests__/anthropic.test.js +0 -9
  284. package/dist/evals/__tests__/anthropic.test.js.map +0 -1
  285. package/dist/evals/__tests__/base.test.js +0 -37
  286. package/dist/evals/__tests__/base.test.js.map +0 -1
  287. package/dist/evals/__tests__/core.test.js +0 -33
  288. package/dist/evals/__tests__/core.test.js.map +0 -1
  289. package/dist/evals/__tests__/metrics.test.js +0 -59
  290. package/dist/evals/__tests__/metrics.test.js.map +0 -1
  291. package/dist/evals/__tests__/openai.test.js +0 -9
  292. package/dist/evals/__tests__/openai.test.js.map +0 -1
  293. package/dist/evals/__tests__/providers.test.js +0 -14
  294. package/dist/evals/__tests__/providers.test.js.map +0 -1
  295. package/dist/evals/__tests__/utils.test.js +0 -46
  296. package/dist/evals/__tests__/utils.test.js.map +0 -1
  297. package/dist/evals/all.d.ts +0 -4
  298. package/dist/evals/all.js +0 -35
  299. package/dist/evals/all.js.map +0 -1
  300. package/dist/evals/base.d.ts +0 -15
  301. package/dist/evals/base.js +0 -51
  302. package/dist/evals/base.js.map +0 -1
  303. package/dist/evals/bias.d.ts +0 -4
  304. package/dist/evals/bias.js +0 -35
  305. package/dist/evals/bias.js.map +0 -1
  306. package/dist/evals/hallucination.d.ts +0 -4
  307. package/dist/evals/hallucination.js +0 -32
  308. package/dist/evals/hallucination.js.map +0 -1
  309. package/dist/evals/llm/anthropic.d.ts +0 -5
  310. package/dist/evals/llm/anthropic.js +0 -42
  311. package/dist/evals/llm/anthropic.js.map +0 -1
  312. package/dist/evals/llm/openai.d.ts +0 -6
  313. package/dist/evals/llm/openai.js +0 -28
  314. package/dist/evals/llm/openai.js.map +0 -1
  315. package/dist/evals/llm/providers.d.ts +0 -7
  316. package/dist/evals/llm/providers.js +0 -10
  317. package/dist/evals/llm/providers.js.map +0 -1
  318. package/dist/evals/metrics.d.ts +0 -9
  319. package/dist/evals/metrics.js +0 -38
  320. package/dist/evals/metrics.js.map +0 -1
  321. package/dist/evals/toxicity.d.ts +0 -4
  322. package/dist/evals/toxicity.js +0 -33
  323. package/dist/evals/toxicity.js.map +0 -1
  324. package/dist/evals/utils.d.ts +0 -4
  325. package/dist/evals/utils.js +0 -39
  326. package/dist/evals/utils.js.map +0 -1
  327. package/dist/guard/__tests__/gaurd.test.js +0 -136
  328. package/dist/guard/__tests__/gaurd.test.js.map +0 -1
  329. package/dist/guard/__tests__/utils.test.js +0 -64
  330. package/dist/guard/__tests__/utils.test.js.map +0 -1
  331. package/dist/guard/all.d.ts +0 -8
  332. package/dist/guard/all.js +0 -28
  333. package/dist/guard/all.js.map +0 -1
  334. package/dist/guard/types.d.ts +0 -26
  335. package/dist/guard/types.js +0 -4
  336. package/dist/guard/types.js.map +0 -1
  337. package/dist/guard/utils.d.ts +0 -13
  338. package/dist/guard/utils.js +0 -79
  339. package/dist/guard/utils.js.map +0 -1
  340. /package/dist/{evals/__tests__/anthropic.test.d.ts → __tests__/manual-trace.test.d.ts} +0 -0
  341. /package/dist/{evals/__tests__/metrics.test.d.ts → __tests__/register.test.d.ts} +0 -0
  342. /package/dist/evals/__tests__/{base.test.d.ts → offline.test.d.ts} +0 -0
  343. /package/dist/{evals/__tests__/core.test.d.ts → guard/__tests__/integration.test.d.ts} +0 -0
  344. /package/dist/{evals/__tests__/openai.test.d.ts → guard/__tests__/pii.test.d.ts} +0 -0
  345. /package/dist/{evals/__tests__/providers.test.d.ts → guard/__tests__/pipeline.test.d.ts} +0 -0
  346. /package/dist/{evals/__tests__/utils.test.d.ts → guard/__tests__/prompt-injection.test.d.ts} +0 -0
  347. /package/dist/{guard/__tests__/gaurd.test.d.ts → instrumentation/__tests__/assemblyai-wrapper.test.d.ts} +0 -0
  348. /package/dist/{guard/__tests__/utils.test.d.ts → instrumentation/__tests__/browser-use-wrapper.test.d.ts} +0 -0
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const instrumentation_1 = require("@opentelemetry/instrumentation");
7
+ const api_1 = require("@opentelemetry/api");
8
+ const constant_1 = require("../../constant");
9
+ const wrapper_1 = __importDefault(require("./wrapper"));
10
+ /**
11
+ * Instruments local HuggingFace inference via the Transformers.js SDK.
12
+ *
13
+ * Supported packages (registered as separate module definitions so either
14
+ * package name triggers instrumentation):
15
+ * - `@huggingface/transformers` (current name, v3+)
16
+ * - `@xenova/transformers` (older name)
17
+ *
18
+ * Parity: the Python SDK instruments `transformers.TextGenerationPipeline.__call__`
19
+ * and reports it as the `chat` operation. Transformers.js implements each
20
+ * pipeline as a subclass whose `_call` method runs the inference, so we patch
21
+ * the `_call` of every known pipeline subclass (and the base `Pipeline` as a
22
+ * final catch). The operation type is resolved at runtime from the pipeline's
23
+ * `task`, so text-generation matches Python while other local pipelines
24
+ * (summarization, translation, feature-extraction, …) get the closest OTel
25
+ * operation.
26
+ */
27
+ class OpenlitTransformersInstrumentation extends instrumentation_1.InstrumentationBase {
28
+ constructor(config = {}) {
29
+ super(`${constant_1.INSTRUMENTATION_PREFIX}/instrumentation-transformers`, '1.0.0', config);
30
+ }
31
+ init() {
32
+ const makeDef = (packageName) => new instrumentation_1.InstrumentationNodeModuleDefinition(packageName, ['>=2.0.0'], (moduleExports, moduleVersion) => {
33
+ this._patch(moduleExports, moduleVersion);
34
+ return moduleExports;
35
+ }, (moduleExports) => {
36
+ if (moduleExports !== undefined) {
37
+ this._unpatch(moduleExports);
38
+ }
39
+ });
40
+ return [makeDef('@huggingface/transformers'), makeDef('@xenova/transformers')];
41
+ }
42
+ manualPatch(transformers) {
43
+ this._patch(transformers);
44
+ }
45
+ _patch(moduleExports, moduleVersion) {
46
+ try {
47
+ const tracer = this.tracer;
48
+ const sdkVersion = moduleVersion ? String(moduleVersion) : undefined;
49
+ let patched = false;
50
+ for (const className of OpenlitTransformersInstrumentation.PIPELINE_CLASSES) {
51
+ const PipelineClass = moduleExports[className];
52
+ const proto = PipelineClass?.prototype;
53
+ if (!proto)
54
+ continue;
55
+ // Only patch a class that defines its OWN `_call`, so each distinct
56
+ // implementation is wrapped exactly once. Subclasses that inherit
57
+ // `_call` reuse the ancestor's wrapped method.
58
+ if (!Object.prototype.hasOwnProperty.call(proto, '_call'))
59
+ continue;
60
+ if (typeof proto._call !== 'function')
61
+ continue;
62
+ if ((0, instrumentation_1.isWrapped)(proto._call)) {
63
+ this._unwrap(proto, '_call');
64
+ }
65
+ this._wrap(proto, '_call', wrapper_1.default._patchPipelineCall(tracer, className, sdkVersion));
66
+ patched = true;
67
+ }
68
+ // Fallback: if no Pipeline subclass exposed `_call` (very old or very new
69
+ // SDK internals), wrap the `pipeline()` factory instead.
70
+ if (!patched && typeof moduleExports.pipeline === 'function') {
71
+ if (!(0, instrumentation_1.isWrapped)(moduleExports.pipeline)) {
72
+ this._wrap(moduleExports, 'pipeline', wrapper_1.default._patchPipelineFactory(tracer, sdkVersion));
73
+ }
74
+ }
75
+ }
76
+ catch (e) {
77
+ api_1.diag.error('transformers instrumentation: error in _patch method', e);
78
+ }
79
+ }
80
+ _unpatch(moduleExports) {
81
+ try {
82
+ for (const className of OpenlitTransformersInstrumentation.PIPELINE_CLASSES) {
83
+ const proto = moduleExports[className]?.prototype;
84
+ if (!proto)
85
+ continue;
86
+ if (Object.prototype.hasOwnProperty.call(proto, '_call') && (0, instrumentation_1.isWrapped)(proto._call)) {
87
+ this._unwrap(proto, '_call');
88
+ }
89
+ }
90
+ if ((0, instrumentation_1.isWrapped)(moduleExports.pipeline)) {
91
+ this._unwrap(moduleExports, 'pipeline');
92
+ }
93
+ }
94
+ catch (e) {
95
+ api_1.diag.error('transformers instrumentation: error in _unpatch method', e);
96
+ }
97
+ }
98
+ }
99
+ /**
100
+ * Pipeline subclasses whose `_call` we patch. Most specific first; the base
101
+ * `Pipeline` is the final fallback. Subclasses that inherit `_call` from an
102
+ * ancestor in this list are covered by that ancestor's patch (the operation
103
+ * is resolved from the live instance's `task`, so the span stays correct).
104
+ */
105
+ OpenlitTransformersInstrumentation.PIPELINE_CLASSES = [
106
+ 'TextGenerationPipeline',
107
+ 'Text2TextGenerationPipeline',
108
+ 'SummarizationPipeline',
109
+ 'TranslationPipeline',
110
+ 'FillMaskPipeline',
111
+ 'QuestionAnsweringPipeline',
112
+ 'TextClassificationPipeline',
113
+ 'TokenClassificationPipeline',
114
+ 'ZeroShotClassificationPipeline',
115
+ 'FeatureExtractionPipeline',
116
+ 'Pipeline',
117
+ ];
118
+ exports.default = OpenlitTransformersInstrumentation;
119
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/instrumentation/transformers/index.ts"],"names":[],"mappings":";;;;;AAAA,oEAKwC;AAExC,4CAA0C;AAC1C,6CAAwD;AACxD,wDAA4C;AAI5C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAqB,kCAAmC,SAAQ,qCAAmB;IACjF,YAAY,SAA4C,EAAE;QACxD,KAAK,CAAC,GAAG,iCAAsB,+BAA+B,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACnF,CAAC;IAES,IAAI;QACZ,MAAM,OAAO,GAAG,CAAC,WAAmB,EAAE,EAAE,CACtC,IAAI,qDAAmC,CACrC,WAAW,EACX,CAAC,SAAS,CAAC,EACX,CAAC,aAAa,EAAE,aAAa,EAAE,EAAE;YAC/B,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;YAC1C,OAAO,aAAa,CAAC;QACvB,CAAC,EACD,CAAC,aAAa,EAAE,EAAE;YAChB,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;gBAChC,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC,CACF,CAAC;QAEJ,OAAO,CAAC,OAAO,CAAC,2BAA2B,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC,CAAC,CAAC;IACjF,CAAC;IAEM,WAAW,CAAC,YAAiB;QAClC,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IAC5B,CAAC;IAsBS,MAAM,CAAC,aAAkB,EAAE,aAAsB;QACzD,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,MAAM,UAAU,GAAG,aAAa,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YACrE,IAAI,OAAO,GAAG,KAAK,CAAC;YAEpB,KAAK,MAAM,SAAS,IAAI,kCAAkC,CAAC,gBAAgB,EAAE,CAAC;gBAC5E,MAAM,aAAa,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;gBAC/C,MAAM,KAAK,GAAG,aAAa,EAAE,SAAS,CAAC;gBACvC,IAAI,CAAC,KAAK;oBAAE,SAAS;gBAErB,oEAAoE;gBACpE,kEAAkE;gBAClE,+CAA+C;gBAC/C,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC;oBAAE,SAAS;gBACpE,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,UAAU;oBAAE,SAAS;gBAChD,IAAI,IAAA,2BAAS,EAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;oBAC3B,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;gBAC/B,CAAC;gBACD,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,iBAAmB,CAAC,kBAAkB,CAAC,MAAM,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC;gBAClG,OAAO,GAAG,IAAI,CAAC;YACjB,CAAC;YAED,0EAA0E;YAC1E,yDAAyD;YACzD,IAAI,CAAC,OAAO,IAAI,OAAO,aAAa,CAAC,QAAQ,KAAK,UAAU,EAAE,CAAC;gBAC7D,IAAI,CAAC,IAAA,2BAAS,EAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;oBACvC,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,UAAU,EAAE,iBAAmB,CAAC,qBAAqB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;gBACvG,CAAC;YACH,CAAC;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,UAAI,CAAC,KAAK,CAAC,sDAAsD,EAAE,CAAC,CAAC,CAAC;QACxE,CAAC;IACH,CAAC;IAES,QAAQ,CAAC,aAAkB;QACnC,IAAI,CAAC;YACH,KAAK,MAAM,SAAS,IAAI,kCAAkC,CAAC,gBAAgB,EAAE,CAAC;gBAC5E,MAAM,KAAK,GAAG,aAAa,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;gBAClD,IAAI,CAAC,KAAK;oBAAE,SAAS;gBACrB,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,IAAA,2BAAS,EAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;oBACnF,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;gBAC/B,CAAC;YACH,CAAC;YACD,IAAI,IAAA,2BAAS,EAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACtC,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,UAAU,CAAC,CAAC;YAC1C,CAAC;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,UAAI,CAAC,KAAK,CAAC,wDAAwD,EAAE,CAAC,CAAC,CAAC;QAC1E,CAAC;IACH,CAAC;;AAtED;;;;;GAKG;AACqB,mDAAgB,GAAG;IACzC,wBAAwB;IACxB,6BAA6B;IAC7B,uBAAuB;IACvB,qBAAqB;IACrB,kBAAkB;IAClB,2BAA2B;IAC3B,4BAA4B;IAC5B,6BAA6B;IAC7B,gCAAgC;IAChC,2BAA2B;IAC3B,UAAU;CACX,CAAC;kBA9CiB,kCAAkC"}
@@ -0,0 +1,49 @@
1
+ import { Span, Tracer } from '@opentelemetry/api';
2
+ import BaseWrapper, { BaseSpanAttributes } from '../base-wrapper';
3
+ declare class TransformersWrapper extends BaseWrapper {
4
+ static aiSystem: string;
5
+ static serverAddress: string;
6
+ static serverPort: number;
7
+ /**
8
+ * Stamp `openlit.agent.version_hash` (auto) and `gen_ai.agent.version`
9
+ * (user override) on the span and return them so the caller can merge them
10
+ * into the inference-event extras.
11
+ */
12
+ static _stampAgentVersion(span: Span, args: {
13
+ systemInstructionsJson?: string | null;
14
+ primaryModel?: string;
15
+ temperature?: number | null;
16
+ top_p?: number | null;
17
+ max_tokens?: number | null;
18
+ }): Record<string, string>;
19
+ /**
20
+ * Patch a Pipeline subclass `_call` (the method invoked when the pipeline
21
+ * object is used as a function). `this` is the pipeline instance.
22
+ * args[0] = inputs, args[1] = generation options.
23
+ */
24
+ static _patchPipelineCall(tracer: Tracer, className: string, sdkVersion?: string): any;
25
+ /**
26
+ * Patch the `pipeline()` factory as a fallback when no Pipeline subclass
27
+ * prototype is exported. Wraps the returned callable so each invocation
28
+ * emits a span. The original callable is invoked directly (not via the
29
+ * wrapper) so we never lose its prototype behavior.
30
+ */
31
+ static _patchPipelineFactory(tracer: Tracer, sdkVersion?: string): any;
32
+ /**
33
+ * Synchronous attribute setter shared by the class- and factory-patch paths.
34
+ * Returns the metric params so the caller can record metrics in `finally`.
35
+ */
36
+ static _handleResponse({ instance, args, response, span, requestModel, task, operation, genAIEndpoint, ttft, sdkVersion, }: {
37
+ instance: any;
38
+ args: any[];
39
+ response: any;
40
+ span: Span;
41
+ requestModel: string;
42
+ task: string;
43
+ operation: string;
44
+ genAIEndpoint: string;
45
+ ttft: number;
46
+ sdkVersion?: string;
47
+ }): BaseSpanAttributes;
48
+ }
49
+ export default TransformersWrapper;
@@ -0,0 +1,472 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ const api_1 = require("@opentelemetry/api");
40
+ const config_1 = __importDefault(require("../../config"));
41
+ const helpers_1 = __importStar(require("../../helpers"));
42
+ const semantic_convention_1 = __importDefault(require("../../semantic-convention"));
43
+ const base_wrapper_1 = __importDefault(require("../base-wrapper"));
44
+ const TASK_OPERATION = {
45
+ // Generative chat-style (Python parity)
46
+ 'text-generation': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CHAT, isEmbedding: false },
47
+ // Sequence-to-sequence / extractive text producers
48
+ 'text2text-generation': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION, isEmbedding: false },
49
+ summarization: { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION, isEmbedding: false },
50
+ translation: { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION, isEmbedding: false },
51
+ 'fill-mask': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION, isEmbedding: false },
52
+ 'question-answering': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION, isEmbedding: false },
53
+ 'text-classification': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION, isEmbedding: false },
54
+ 'token-classification': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION, isEmbedding: false },
55
+ 'zero-shot-classification': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION, isEmbedding: false },
56
+ // Embedding producers
57
+ 'feature-extraction': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_EMBEDDING, isEmbedding: true },
58
+ 'sentence-similarity': { operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_EMBEDDING, isEmbedding: true },
59
+ };
60
+ /** Map a Pipeline subclass name to its canonical Transformers.js task string. */
61
+ const CLASS_TASK = {
62
+ TextGenerationPipeline: 'text-generation',
63
+ Text2TextGenerationPipeline: 'text2text-generation',
64
+ SummarizationPipeline: 'summarization',
65
+ TranslationPipeline: 'translation',
66
+ FillMaskPipeline: 'fill-mask',
67
+ QuestionAnsweringPipeline: 'question-answering',
68
+ TextClassificationPipeline: 'text-classification',
69
+ TokenClassificationPipeline: 'token-classification',
70
+ ZeroShotClassificationPipeline: 'zero-shot-classification',
71
+ FeatureExtractionPipeline: 'feature-extraction',
72
+ };
73
+ function resolveTask(instance, className) {
74
+ return ((typeof instance?.task === 'string' && instance.task) ||
75
+ CLASS_TASK[className] ||
76
+ 'text-generation');
77
+ }
78
+ function classifyTask(task) {
79
+ return (TASK_OPERATION[task] || {
80
+ operation: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TEXT_COMPLETION,
81
+ isEmbedding: false,
82
+ });
83
+ }
84
+ /**
85
+ * Resolve the model identifier from a pipeline instance, mirroring Python's
86
+ * `instance.model.config.name_or_path`. HF configs expose the path under a few
87
+ * keys across versions, so we probe the common ones before falling back.
88
+ */
89
+ function resolveModel(instance) {
90
+ const config = instance?.model?.config ?? {};
91
+ return (config._name_or_path ||
92
+ config.name_or_path ||
93
+ config.model_type ||
94
+ instance?.model?.name_or_path ||
95
+ (typeof instance?.task === 'string' ? instance.task : '') ||
96
+ 'unknown');
97
+ }
98
+ /**
99
+ * Extract the generation parameters for a call, mirroring Python which reads
100
+ * `instance._forward_params` (set at pipeline construction) merged with the
101
+ * call-time options object.
102
+ */
103
+ function resolveGenerationParams(instance, options) {
104
+ const forward = instance?._forward_params ?? {};
105
+ const opts = options ?? {};
106
+ const pick = (key, altKey) => opts[key] ?? (altKey ? opts[altKey] : undefined) ?? forward[key] ?? (altKey ? forward[altKey] : undefined) ?? null;
107
+ return {
108
+ temperature: pick('temperature'),
109
+ topK: pick('top_k'),
110
+ topP: pick('top_p'),
111
+ maxTokens: pick('max_new_tokens', 'max_length'),
112
+ };
113
+ }
114
+ /**
115
+ * Convert a Transformers.js pipeline result into a flat text string per task,
116
+ * mirroring the task branches in Python's `process_chat_response`.
117
+ */
118
+ function extractCompletion(task, response) {
119
+ const first = Array.isArray(response) ? response[0] : response;
120
+ const fromEntry = (entry) => {
121
+ if (entry === null || entry === undefined)
122
+ return '';
123
+ if (typeof entry !== 'object')
124
+ return String(entry);
125
+ // text-generation may nest a chat-message list under generated_text
126
+ if (Array.isArray(entry.generated_text)) {
127
+ const last = entry.generated_text[entry.generated_text.length - 1];
128
+ return last?.content ?? String(last ?? '');
129
+ }
130
+ return (entry.generated_text ??
131
+ entry.summary_text ??
132
+ entry.translation_text ??
133
+ entry.answer ??
134
+ entry.sequence ??
135
+ entry.token_str ??
136
+ entry.label ??
137
+ entry.text ??
138
+ '');
139
+ };
140
+ switch (task) {
141
+ case 'automatic-speech-recognition':
142
+ return typeof response === 'object' && response !== null ? response.text ?? '' : '';
143
+ case 'feature-extraction':
144
+ case 'sentence-similarity':
145
+ return '';
146
+ default: {
147
+ const out = fromEntry(first);
148
+ if (out)
149
+ return out;
150
+ // Fall back to a stable serialization for unknown shapes.
151
+ try {
152
+ return typeof response === 'string' ? response : JSON.stringify(response);
153
+ }
154
+ catch {
155
+ return String(response ?? '');
156
+ }
157
+ }
158
+ }
159
+ }
160
+ function stringifyInputs(inputs) {
161
+ if (typeof inputs === 'string')
162
+ return inputs;
163
+ if (Array.isArray(inputs)) {
164
+ return inputs
165
+ .map((i) => (typeof i === 'string' ? i : i?.content ?? JSON.stringify(i)))
166
+ .join('\n');
167
+ }
168
+ if (inputs && typeof inputs === 'object') {
169
+ // question-answering style { question, context }
170
+ if (typeof inputs.question === 'string') {
171
+ return inputs.context
172
+ ? `question: ${inputs.question} context: ${inputs.context}`
173
+ : inputs.question;
174
+ }
175
+ try {
176
+ return JSON.stringify(inputs);
177
+ }
178
+ catch {
179
+ return String(inputs);
180
+ }
181
+ }
182
+ return String(inputs ?? '');
183
+ }
184
+ class TransformersWrapper extends base_wrapper_1.default {
185
+ /**
186
+ * Stamp `openlit.agent.version_hash` (auto) and `gen_ai.agent.version`
187
+ * (user override) on the span and return them so the caller can merge them
188
+ * into the inference-event extras.
189
+ */
190
+ static _stampAgentVersion(span, args) {
191
+ const out = {};
192
+ try {
193
+ const versionHash = helpers_1.default.computeAgentVersionHash({
194
+ systemInstructions: args.systemInstructionsJson ?? null,
195
+ toolDefinitions: null,
196
+ primaryModel: args.primaryModel ?? null,
197
+ runtimeConfig: {
198
+ temperature: args.temperature ?? null,
199
+ top_p: args.top_p ?? null,
200
+ max_tokens: args.max_tokens ?? null,
201
+ provider: semantic_convention_1.default.GEN_AI_SYSTEM_HUGGING_FACE,
202
+ },
203
+ providers: [semantic_convention_1.default.GEN_AI_SYSTEM_HUGGING_FACE],
204
+ });
205
+ if (versionHash) {
206
+ out[semantic_convention_1.default.OPENLIT_AGENT_VERSION_HASH] = versionHash;
207
+ span.setAttribute(semantic_convention_1.default.OPENLIT_AGENT_VERSION_HASH, versionHash);
208
+ }
209
+ }
210
+ catch {
211
+ /* Hash computation must never fail the wrapped call. */
212
+ }
213
+ const versionLabel = (0, helpers_1.getCurrentAgentVersion)();
214
+ if (versionLabel) {
215
+ out[semantic_convention_1.default.GEN_AI_AGENT_VERSION] = versionLabel;
216
+ span.setAttribute(semantic_convention_1.default.GEN_AI_AGENT_VERSION, versionLabel);
217
+ }
218
+ return out;
219
+ }
220
+ /**
221
+ * Patch a Pipeline subclass `_call` (the method invoked when the pipeline
222
+ * object is used as a function). `this` is the pipeline instance.
223
+ * args[0] = inputs, args[1] = generation options.
224
+ */
225
+ static _patchPipelineCall(tracer, className, sdkVersion) {
226
+ return (originalMethod) => {
227
+ return async function (...args) {
228
+ if ((0, helpers_1.isFrameworkLlmActive)())
229
+ return originalMethod.apply(this, args);
230
+ const task = resolveTask(this, className);
231
+ const requestModel = resolveModel(this);
232
+ const { operation } = classifyTask(task);
233
+ const genAIEndpoint = `transformers.${task}`;
234
+ const spanName = `${operation} ${requestModel}`;
235
+ const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
236
+ const span = tracer.startSpan(spanName, { kind: api_1.SpanKind.CLIENT, attributes: spanCreationAttrs(operation, requestModel) }, effectiveCtx);
237
+ return api_1.context.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
238
+ let metricParams;
239
+ const startTime = Date.now();
240
+ try {
241
+ const response = await originalMethod.apply(this, args);
242
+ metricParams = TransformersWrapper._handleResponse({
243
+ instance: this,
244
+ args,
245
+ response,
246
+ span,
247
+ requestModel,
248
+ task,
249
+ operation,
250
+ genAIEndpoint,
251
+ ttft: (Date.now() - startTime) / 1000,
252
+ sdkVersion,
253
+ });
254
+ return response;
255
+ }
256
+ catch (e) {
257
+ helpers_1.default.handleException(span, e);
258
+ base_wrapper_1.default.recordMetrics(span, {
259
+ genAIEndpoint,
260
+ model: requestModel,
261
+ aiSystem: TransformersWrapper.aiSystem,
262
+ serverAddress: TransformersWrapper.serverAddress,
263
+ serverPort: TransformersWrapper.serverPort,
264
+ errorType: e?.constructor?.name || '_OTHER',
265
+ });
266
+ throw e;
267
+ }
268
+ finally {
269
+ span.end();
270
+ if (metricParams) {
271
+ base_wrapper_1.default.recordMetrics(span, metricParams);
272
+ }
273
+ }
274
+ });
275
+ };
276
+ };
277
+ }
278
+ /**
279
+ * Patch the `pipeline()` factory as a fallback when no Pipeline subclass
280
+ * prototype is exported. Wraps the returned callable so each invocation
281
+ * emits a span. The original callable is invoked directly (not via the
282
+ * wrapper) so we never lose its prototype behavior.
283
+ */
284
+ static _patchPipelineFactory(tracer, sdkVersion) {
285
+ return (originalFactory) => {
286
+ return async function (...factoryArgs) {
287
+ const pipe = await originalFactory.apply(this, factoryArgs);
288
+ if (typeof pipe !== 'function')
289
+ return pipe;
290
+ const task = (typeof factoryArgs[0] === 'string' && factoryArgs[0]) ||
291
+ (typeof pipe.task === 'string' && pipe.task) ||
292
+ 'text-generation';
293
+ const { operation } = classifyTask(task);
294
+ const genAIEndpoint = `transformers.${task}`;
295
+ const wrappedPipe = async function (...callArgs) {
296
+ if ((0, helpers_1.isFrameworkLlmActive)())
297
+ return pipe.apply(this, callArgs);
298
+ const requestModel = resolveModel(pipe) ||
299
+ (typeof factoryArgs[1] === 'string' ? factoryArgs[1] : 'unknown');
300
+ const spanName = `${operation} ${requestModel}`;
301
+ const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
302
+ const span = tracer.startSpan(spanName, { kind: api_1.SpanKind.CLIENT, attributes: spanCreationAttrs(operation, requestModel) }, effectiveCtx);
303
+ return api_1.context.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
304
+ let metricParams;
305
+ const startTime = Date.now();
306
+ try {
307
+ const response = await pipe.apply(this, callArgs);
308
+ metricParams = TransformersWrapper._handleResponse({
309
+ instance: pipe,
310
+ args: callArgs,
311
+ response,
312
+ span,
313
+ requestModel,
314
+ task,
315
+ operation,
316
+ genAIEndpoint,
317
+ ttft: (Date.now() - startTime) / 1000,
318
+ sdkVersion,
319
+ });
320
+ return response;
321
+ }
322
+ catch (e) {
323
+ helpers_1.default.handleException(span, e);
324
+ base_wrapper_1.default.recordMetrics(span, {
325
+ genAIEndpoint,
326
+ model: requestModel,
327
+ aiSystem: TransformersWrapper.aiSystem,
328
+ serverAddress: TransformersWrapper.serverAddress,
329
+ serverPort: TransformersWrapper.serverPort,
330
+ errorType: e?.constructor?.name || '_OTHER',
331
+ });
332
+ throw e;
333
+ }
334
+ finally {
335
+ span.end();
336
+ if (metricParams) {
337
+ base_wrapper_1.default.recordMetrics(span, metricParams);
338
+ }
339
+ }
340
+ });
341
+ };
342
+ // Preserve callable identity so the wrapped pipe behaves like the original.
343
+ Object.setPrototypeOf(wrappedPipe, Object.getPrototypeOf(pipe));
344
+ Object.assign(wrappedPipe, pipe);
345
+ return wrappedPipe;
346
+ };
347
+ };
348
+ }
349
+ /**
350
+ * Synchronous attribute setter shared by the class- and factory-patch paths.
351
+ * Returns the metric params so the caller can record metrics in `finally`.
352
+ */
353
+ static _handleResponse({ instance, args, response, span, requestModel, task, operation, genAIEndpoint, ttft, sdkVersion, }) {
354
+ const captureContent = config_1.default.captureMessageContent;
355
+ const { isEmbedding } = classifyTask(task);
356
+ const inputs = args[0];
357
+ const options = args[1] || {};
358
+ const { temperature, topK, topP, maxTokens } = resolveGenerationParams(instance, options);
359
+ const inputStr = stringifyInputs(inputs);
360
+ const completion = extractCompletion(task, response);
361
+ const inputTokens = helpers_1.default.generalTokens(inputStr) ?? 0;
362
+ const outputTokens = isEmbedding ? 0 : helpers_1.default.generalTokens(completion) ?? 0;
363
+ const pricingInfo = config_1.default.pricingInfo || {};
364
+ const cost = helpers_1.default.getChatModelCost(requestModel, pricingInfo, inputTokens, outputTokens);
365
+ // Common attributes (telemetry sdk, env, app, request model, cost, server).
366
+ TransformersWrapper.setBaseSpanAttributes(span, {
367
+ genAIEndpoint,
368
+ model: requestModel,
369
+ cost,
370
+ aiSystem: TransformersWrapper.aiSystem,
371
+ serverAddress: TransformersWrapper.serverAddress,
372
+ serverPort: TransformersWrapper.serverPort,
373
+ });
374
+ // Parity: Python stamps gen_ai.system and the transformers package version
375
+ // (setBaseSpanAttributes uses OpenLIT's SDK version).
376
+ span.setAttribute(semantic_convention_1.default.GEN_AI_PROVIDER_NAME, TransformersWrapper.aiSystem);
377
+ if (sdkVersion) {
378
+ span.setAttribute(semantic_convention_1.default.GEN_AI_SDK_VERSION, sdkVersion);
379
+ }
380
+ // Request parameters (only when present, matching Python).
381
+ if (temperature !== null) {
382
+ span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TEMPERATURE, temperature);
383
+ }
384
+ if (topK !== null) {
385
+ span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TOP_K, topK);
386
+ }
387
+ if (topP !== null) {
388
+ span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TOP_P, topP);
389
+ }
390
+ if (maxTokens !== null) {
391
+ span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_MAX_TOKENS, maxTokens);
392
+ }
393
+ span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IS_STREAM, false);
394
+ // Response parameters.
395
+ span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_MODEL, requestModel);
396
+ if (!isEmbedding) {
397
+ span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON, ['stop']);
398
+ span.setAttribute(semantic_convention_1.default.GEN_AI_OUTPUT_TYPE, semantic_convention_1.default.GEN_AI_OUTPUT_TYPE_TEXT);
399
+ }
400
+ // Tokens, cost, cache (cache stamped as 0 even when unused, like Python).
401
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);
402
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);
403
+ span.setAttribute(semantic_convention_1.default.GEN_AI_CLIENT_TOKEN_USAGE, inputTokens + outputTokens);
404
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, 0);
405
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, 0);
406
+ // Timing (Python always sets these; tbt is 0 for non-streaming pipelines).
407
+ span.setAttribute(semantic_convention_1.default.GEN_AI_SERVER_TTFT, ttft);
408
+ span.setAttribute(semantic_convention_1.default.GEN_AI_SERVER_TBT, 0);
409
+ const versionExtras = TransformersWrapper._stampAgentVersion(span, {
410
+ systemInstructionsJson: null,
411
+ primaryModel: requestModel,
412
+ temperature,
413
+ top_p: topP,
414
+ max_tokens: maxTokens,
415
+ });
416
+ let inputMessagesJson;
417
+ let outputMessagesJson;
418
+ if (captureContent) {
419
+ inputMessagesJson = helpers_1.default.buildInputMessages([{ role: 'user', content: inputStr }]);
420
+ span.setAttribute(semantic_convention_1.default.GEN_AI_INPUT_MESSAGES, inputMessagesJson);
421
+ if (!isEmbedding) {
422
+ outputMessagesJson = helpers_1.default.buildOutputMessages(completion, 'stop');
423
+ span.setAttribute(semantic_convention_1.default.GEN_AI_OUTPUT_MESSAGES, outputMessagesJson);
424
+ }
425
+ }
426
+ if (!config_1.default.disableEvents) {
427
+ const eventAttrs = {
428
+ [semantic_convention_1.default.GEN_AI_OPERATION]: operation,
429
+ [semantic_convention_1.default.GEN_AI_REQUEST_MODEL]: requestModel,
430
+ [semantic_convention_1.default.GEN_AI_RESPONSE_MODEL]: requestModel,
431
+ [semantic_convention_1.default.SERVER_ADDRESS]: TransformersWrapper.serverAddress,
432
+ [semantic_convention_1.default.SERVER_PORT]: TransformersWrapper.serverPort,
433
+ [semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS]: inputTokens,
434
+ [semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS]: outputTokens,
435
+ ...versionExtras,
436
+ };
437
+ if (!isEmbedding) {
438
+ eventAttrs[semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON] = ['stop'];
439
+ eventAttrs[semantic_convention_1.default.GEN_AI_OUTPUT_TYPE] = semantic_convention_1.default.GEN_AI_OUTPUT_TYPE_TEXT;
440
+ }
441
+ if (captureContent) {
442
+ if (inputMessagesJson)
443
+ eventAttrs[semantic_convention_1.default.GEN_AI_INPUT_MESSAGES] = inputMessagesJson;
444
+ if (outputMessagesJson)
445
+ eventAttrs[semantic_convention_1.default.GEN_AI_OUTPUT_MESSAGES] = outputMessagesJson;
446
+ }
447
+ helpers_1.default.emitInferenceEvent(span, eventAttrs);
448
+ }
449
+ return {
450
+ genAIEndpoint,
451
+ model: requestModel,
452
+ cost,
453
+ aiSystem: TransformersWrapper.aiSystem,
454
+ serverAddress: TransformersWrapper.serverAddress,
455
+ serverPort: TransformersWrapper.serverPort,
456
+ };
457
+ }
458
+ }
459
+ TransformersWrapper.aiSystem = semantic_convention_1.default.GEN_AI_SYSTEM_HUGGING_FACE;
460
+ TransformersWrapper.serverAddress = '127.0.0.1';
461
+ TransformersWrapper.serverPort = 80;
462
+ function spanCreationAttrs(operationName, requestModel) {
463
+ return {
464
+ [semantic_convention_1.default.GEN_AI_OPERATION]: operationName,
465
+ [semantic_convention_1.default.GEN_AI_PROVIDER_NAME_OTEL]: semantic_convention_1.default.GEN_AI_SYSTEM_HUGGING_FACE,
466
+ [semantic_convention_1.default.GEN_AI_REQUEST_MODEL]: requestModel,
467
+ [semantic_convention_1.default.SERVER_ADDRESS]: TransformersWrapper.serverAddress,
468
+ [semantic_convention_1.default.SERVER_PORT]: TransformersWrapper.serverPort,
469
+ };
470
+ }
471
+ exports.default = TransformersWrapper;
472
+ //# sourceMappingURL=wrapper.js.map