openlit 1.12.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 (352) 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 +416 -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/index.d.ts +21 -0
  156. package/dist/instrumentation/cursor-sdk/index.js +58 -0
  157. package/dist/instrumentation/cursor-sdk/index.js.map +1 -0
  158. package/dist/instrumentation/cursor-sdk/wrapper.d.ts +17 -0
  159. package/dist/instrumentation/cursor-sdk/wrapper.js +785 -0
  160. package/dist/instrumentation/cursor-sdk/wrapper.js.map +1 -0
  161. package/dist/instrumentation/elevenlabs/index.d.ts +12 -0
  162. package/dist/instrumentation/elevenlabs/index.js +92 -0
  163. package/dist/instrumentation/elevenlabs/index.js.map +1 -0
  164. package/dist/instrumentation/elevenlabs/wrapper.d.ts +34 -0
  165. package/dist/instrumentation/elevenlabs/wrapper.js +280 -0
  166. package/dist/instrumentation/elevenlabs/wrapper.js.map +1 -0
  167. package/dist/instrumentation/firecrawl/index.d.ts +13 -0
  168. package/dist/instrumentation/firecrawl/index.js +115 -0
  169. package/dist/instrumentation/firecrawl/index.js.map +1 -0
  170. package/dist/instrumentation/firecrawl/wrapper.d.ts +22 -0
  171. package/dist/instrumentation/firecrawl/wrapper.js +378 -0
  172. package/dist/instrumentation/firecrawl/wrapper.js.map +1 -0
  173. package/dist/instrumentation/google-ai/wrapper.d.ts +13 -0
  174. package/dist/instrumentation/google-ai/wrapper.js +85 -1
  175. package/dist/instrumentation/google-ai/wrapper.js.map +1 -1
  176. package/dist/instrumentation/gpu/index.d.ts +38 -0
  177. package/dist/instrumentation/gpu/index.js +233 -0
  178. package/dist/instrumentation/gpu/index.js.map +1 -0
  179. package/dist/instrumentation/gradient/index.d.ts +12 -0
  180. package/dist/instrumentation/gradient/index.js +74 -0
  181. package/dist/instrumentation/gradient/index.js.map +1 -0
  182. package/dist/instrumentation/gradient/utils.d.ts +14 -0
  183. package/dist/instrumentation/gradient/utils.js +119 -0
  184. package/dist/instrumentation/gradient/utils.js.map +1 -0
  185. package/dist/instrumentation/gradient/wrapper.d.ts +76 -0
  186. package/dist/instrumentation/gradient/wrapper.js +584 -0
  187. package/dist/instrumentation/gradient/wrapper.js.map +1 -0
  188. package/dist/instrumentation/groq/wrapper.js +42 -0
  189. package/dist/instrumentation/groq/wrapper.js.map +1 -1
  190. package/dist/instrumentation/huggingface/wrapper.d.ts +13 -0
  191. package/dist/instrumentation/huggingface/wrapper.js +66 -0
  192. package/dist/instrumentation/huggingface/wrapper.js.map +1 -1
  193. package/dist/instrumentation/index.js +28 -0
  194. package/dist/instrumentation/index.js.map +1 -1
  195. package/dist/instrumentation/langchain/wrapper.js +5 -8
  196. package/dist/instrumentation/langchain/wrapper.js.map +1 -1
  197. package/dist/instrumentation/langgraph/wrapper.js +12 -11
  198. package/dist/instrumentation/langgraph/wrapper.js.map +1 -1
  199. package/dist/instrumentation/llamaindex/wrapper.d.ts +14 -1
  200. package/dist/instrumentation/llamaindex/wrapper.js +68 -0
  201. package/dist/instrumentation/llamaindex/wrapper.js.map +1 -1
  202. package/dist/instrumentation/mcp/index.d.ts +20 -0
  203. package/dist/instrumentation/mcp/index.js +282 -0
  204. package/dist/instrumentation/mcp/index.js.map +1 -0
  205. package/dist/instrumentation/mcp/wrapper.d.ts +72 -0
  206. package/dist/instrumentation/mcp/wrapper.js +571 -0
  207. package/dist/instrumentation/mcp/wrapper.js.map +1 -0
  208. package/dist/instrumentation/mem0/index.d.ts +17 -0
  209. package/dist/instrumentation/mem0/index.js +113 -0
  210. package/dist/instrumentation/mem0/index.js.map +1 -0
  211. package/dist/instrumentation/mem0/wrapper.d.ts +37 -0
  212. package/dist/instrumentation/mem0/wrapper.js +298 -0
  213. package/dist/instrumentation/mem0/wrapper.js.map +1 -0
  214. package/dist/instrumentation/mistral/wrapper.d.ts +13 -0
  215. package/dist/instrumentation/mistral/wrapper.js +60 -1
  216. package/dist/instrumentation/mistral/wrapper.js.map +1 -1
  217. package/dist/instrumentation/ollama/wrapper.d.ts +13 -0
  218. package/dist/instrumentation/ollama/wrapper.js +67 -1
  219. package/dist/instrumentation/ollama/wrapper.js.map +1 -1
  220. package/dist/instrumentation/openai/wrapper.d.ts +13 -0
  221. package/dist/instrumentation/openai/wrapper.js +89 -3
  222. package/dist/instrumentation/openai/wrapper.js.map +1 -1
  223. package/dist/instrumentation/pg/index.d.ts +20 -0
  224. package/dist/instrumentation/pg/index.js +74 -0
  225. package/dist/instrumentation/pg/index.js.map +1 -0
  226. package/dist/instrumentation/pg/wrapper.d.ts +66 -0
  227. package/dist/instrumentation/pg/wrapper.js +441 -0
  228. package/dist/instrumentation/pg/wrapper.js.map +1 -0
  229. package/dist/instrumentation/replicate/wrapper.d.ts +13 -0
  230. package/dist/instrumentation/replicate/wrapper.js +61 -0
  231. package/dist/instrumentation/replicate/wrapper.js.map +1 -1
  232. package/dist/instrumentation/strands/processor.d.ts +6 -0
  233. package/dist/instrumentation/strands/processor.js +73 -3
  234. package/dist/instrumentation/strands/processor.js.map +1 -1
  235. package/dist/instrumentation/strands/utils.d.ts +2 -2
  236. package/dist/instrumentation/strands/utils.js +15 -2
  237. package/dist/instrumentation/strands/utils.js.map +1 -1
  238. package/dist/instrumentation/together/wrapper.d.ts +13 -0
  239. package/dist/instrumentation/together/wrapper.js +59 -1
  240. package/dist/instrumentation/together/wrapper.js.map +1 -1
  241. package/dist/instrumentation/transformers/index.d.ts +35 -0
  242. package/dist/instrumentation/transformers/index.js +119 -0
  243. package/dist/instrumentation/transformers/index.js.map +1 -0
  244. package/dist/instrumentation/transformers/wrapper.d.ts +49 -0
  245. package/dist/instrumentation/transformers/wrapper.js +472 -0
  246. package/dist/instrumentation/transformers/wrapper.js.map +1 -0
  247. package/dist/instrumentation/vercel-ai/wrapper.d.ts +13 -0
  248. package/dist/instrumentation/vercel-ai/wrapper.js +106 -1
  249. package/dist/instrumentation/vercel-ai/wrapper.js.map +1 -1
  250. package/dist/instrumentation/vertexai/index.d.ts +11 -0
  251. package/dist/instrumentation/vertexai/index.js +70 -0
  252. package/dist/instrumentation/vertexai/index.js.map +1 -0
  253. package/dist/instrumentation/vertexai/wrapper.d.ts +67 -0
  254. package/dist/instrumentation/vertexai/wrapper.js +529 -0
  255. package/dist/instrumentation/vertexai/wrapper.js.map +1 -0
  256. package/dist/instrumentation/vllm/index.d.ts +13 -0
  257. package/dist/instrumentation/vllm/index.js +67 -0
  258. package/dist/instrumentation/vllm/index.js.map +1 -0
  259. package/dist/instrumentation/vllm/wrapper.d.ts +60 -0
  260. package/dist/instrumentation/vllm/wrapper.js +482 -0
  261. package/dist/instrumentation/vllm/wrapper.js.map +1 -0
  262. package/dist/llm/index.d.ts +9 -2
  263. package/dist/llm/index.js.map +1 -1
  264. package/dist/manual-trace.d.ts +40 -0
  265. package/dist/manual-trace.js +103 -0
  266. package/dist/manual-trace.js.map +1 -0
  267. package/dist/otel/metrics.d.ts +11 -0
  268. package/dist/otel/metrics.js +48 -0
  269. package/dist/otel/metrics.js.map +1 -1
  270. package/dist/register.d.ts +4 -0
  271. package/dist/register.js +52 -0
  272. package/dist/register.js.map +1 -0
  273. package/dist/score/__tests__/score.test.d.ts +1 -0
  274. package/dist/score/__tests__/score.test.js +143 -0
  275. package/dist/score/__tests__/score.test.js.map +1 -0
  276. package/dist/score/index.d.ts +2 -0
  277. package/dist/score/index.js +6 -0
  278. package/dist/score/index.js.map +1 -0
  279. package/dist/score/score.d.ts +15 -0
  280. package/dist/score/score.js +135 -0
  281. package/dist/score/score.js.map +1 -0
  282. package/dist/semantic-convention.d.ts +278 -0
  283. package/dist/semantic-convention.js +322 -0
  284. package/dist/semantic-convention.js.map +1 -1
  285. package/dist/types.d.ts +15 -1
  286. package/package.json +18 -1
  287. package/dist/evals/__tests__/anthropic.test.js +0 -9
  288. package/dist/evals/__tests__/anthropic.test.js.map +0 -1
  289. package/dist/evals/__tests__/base.test.js +0 -37
  290. package/dist/evals/__tests__/base.test.js.map +0 -1
  291. package/dist/evals/__tests__/core.test.js +0 -33
  292. package/dist/evals/__tests__/core.test.js.map +0 -1
  293. package/dist/evals/__tests__/metrics.test.js +0 -59
  294. package/dist/evals/__tests__/metrics.test.js.map +0 -1
  295. package/dist/evals/__tests__/openai.test.js +0 -9
  296. package/dist/evals/__tests__/openai.test.js.map +0 -1
  297. package/dist/evals/__tests__/providers.test.js +0 -14
  298. package/dist/evals/__tests__/providers.test.js.map +0 -1
  299. package/dist/evals/__tests__/utils.test.js +0 -46
  300. package/dist/evals/__tests__/utils.test.js.map +0 -1
  301. package/dist/evals/all.d.ts +0 -4
  302. package/dist/evals/all.js +0 -35
  303. package/dist/evals/all.js.map +0 -1
  304. package/dist/evals/base.d.ts +0 -15
  305. package/dist/evals/base.js +0 -51
  306. package/dist/evals/base.js.map +0 -1
  307. package/dist/evals/bias.d.ts +0 -4
  308. package/dist/evals/bias.js +0 -35
  309. package/dist/evals/bias.js.map +0 -1
  310. package/dist/evals/hallucination.d.ts +0 -4
  311. package/dist/evals/hallucination.js +0 -32
  312. package/dist/evals/hallucination.js.map +0 -1
  313. package/dist/evals/llm/anthropic.d.ts +0 -5
  314. package/dist/evals/llm/anthropic.js +0 -42
  315. package/dist/evals/llm/anthropic.js.map +0 -1
  316. package/dist/evals/llm/openai.d.ts +0 -6
  317. package/dist/evals/llm/openai.js +0 -28
  318. package/dist/evals/llm/openai.js.map +0 -1
  319. package/dist/evals/llm/providers.d.ts +0 -7
  320. package/dist/evals/llm/providers.js +0 -10
  321. package/dist/evals/llm/providers.js.map +0 -1
  322. package/dist/evals/metrics.d.ts +0 -9
  323. package/dist/evals/metrics.js +0 -38
  324. package/dist/evals/metrics.js.map +0 -1
  325. package/dist/evals/toxicity.d.ts +0 -4
  326. package/dist/evals/toxicity.js +0 -33
  327. package/dist/evals/toxicity.js.map +0 -1
  328. package/dist/evals/utils.d.ts +0 -4
  329. package/dist/evals/utils.js +0 -39
  330. package/dist/evals/utils.js.map +0 -1
  331. package/dist/guard/__tests__/gaurd.test.js +0 -136
  332. package/dist/guard/__tests__/gaurd.test.js.map +0 -1
  333. package/dist/guard/__tests__/utils.test.js +0 -64
  334. package/dist/guard/__tests__/utils.test.js.map +0 -1
  335. package/dist/guard/all.d.ts +0 -8
  336. package/dist/guard/all.js +0 -28
  337. package/dist/guard/all.js.map +0 -1
  338. package/dist/guard/types.d.ts +0 -26
  339. package/dist/guard/types.js +0 -4
  340. package/dist/guard/types.js.map +0 -1
  341. package/dist/guard/utils.d.ts +0 -13
  342. package/dist/guard/utils.js +0 -79
  343. package/dist/guard/utils.js.map +0 -1
  344. /package/dist/{evals/__tests__/anthropic.test.d.ts → __tests__/manual-trace.test.d.ts} +0 -0
  345. /package/dist/{evals/__tests__/metrics.test.d.ts → __tests__/register.test.d.ts} +0 -0
  346. /package/dist/evals/__tests__/{base.test.d.ts → offline.test.d.ts} +0 -0
  347. /package/dist/{evals/__tests__/core.test.d.ts → guard/__tests__/integration.test.d.ts} +0 -0
  348. /package/dist/{evals/__tests__/openai.test.d.ts → guard/__tests__/pii.test.d.ts} +0 -0
  349. /package/dist/{evals/__tests__/providers.test.d.ts → guard/__tests__/pipeline.test.d.ts} +0 -0
  350. /package/dist/{evals/__tests__/utils.test.d.ts → guard/__tests__/prompt-injection.test.d.ts} +0 -0
  351. /package/dist/{guard/__tests__/gaurd.test.d.ts → instrumentation/__tests__/assemblyai-wrapper.test.d.ts} +0 -0
  352. /package/dist/{guard/__tests__/utils.test.d.ts → instrumentation/__tests__/browser-use-wrapper.test.d.ts} +0 -0
@@ -0,0 +1,785 @@
1
+ "use strict";
2
+ /**
3
+ * Cursor SDK wrapper -- OTel GenAI semantic convention compliant.
4
+ *
5
+ * Wraps Agent.create(), Agent.resume(), and agent.send()
6
+ * to produce `create_agent`, `invoke_agent`, and `execute_tool` spans.
7
+ *
8
+ * Agent.prompt() is NOT wrapped separately -- it internally calls
9
+ * create() + send(), so the patched versions handle it automatically
10
+ * without producing duplicate spans.
11
+ *
12
+ * Token usage is captured via onDelta injection (TurnEndedUpdate).
13
+ * Tool call spans are created from SDKMessage stream events.
14
+ * The `system` stream event provides resolved model and tool definitions.
15
+ */
16
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ var desc = Object.getOwnPropertyDescriptor(m, k);
19
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
20
+ desc = { enumerable: true, get: function() { return m[k]; } };
21
+ }
22
+ Object.defineProperty(o, k2, desc);
23
+ }) : (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ o[k2] = m[k];
26
+ }));
27
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
28
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
29
+ }) : function(o, v) {
30
+ o["default"] = v;
31
+ });
32
+ var __importStar = (this && this.__importStar) || (function () {
33
+ var ownKeys = function(o) {
34
+ ownKeys = Object.getOwnPropertyNames || function (o) {
35
+ var ar = [];
36
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
37
+ return ar;
38
+ };
39
+ return ownKeys(o);
40
+ };
41
+ return function (mod) {
42
+ if (mod && mod.__esModule) return mod;
43
+ var result = {};
44
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
45
+ __setModuleDefault(result, mod);
46
+ return result;
47
+ };
48
+ })();
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.patchAgentCreate = patchAgentCreate;
54
+ exports.patchAgentResume = patchAgentResume;
55
+ const api_1 = require("@opentelemetry/api");
56
+ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
57
+ const semantic_convention_1 = __importDefault(require("../../semantic-convention"));
58
+ const config_1 = __importDefault(require("../../config"));
59
+ const helpers_1 = __importStar(require("../../helpers"));
60
+ const constant_1 = require("../../constant");
61
+ const metrics_1 = __importDefault(require("../../otel/metrics"));
62
+ // ---------------------------------------------------------------------------
63
+ // Constants
64
+ // ---------------------------------------------------------------------------
65
+ const [SERVER_ADDRESS, SERVER_PORT] = (0, helpers_1.getServerAddressForProvider)('cursor');
66
+ const CURSOR_STATUS_TO_FINISH_REASON = {
67
+ finished: 'stop',
68
+ error: 'error',
69
+ cancelled: 'cancelled',
70
+ };
71
+ class AgentCreationRegistry {
72
+ constructor() {
73
+ this._entries = new WeakMap();
74
+ }
75
+ register(agent, spanContext, options) {
76
+ this._entries.set(agent, { spanContext, options });
77
+ }
78
+ get(agent) {
79
+ return this._entries.get(agent);
80
+ }
81
+ }
82
+ const agentRegistry = new AgentCreationRegistry();
83
+ // ---------------------------------------------------------------------------
84
+ // Helpers
85
+ // ---------------------------------------------------------------------------
86
+ function truncateContent(content) {
87
+ const maxLen = config_1.default.maxContentLength;
88
+ if (maxLen != null && maxLen > 0 && content.length > maxLen) {
89
+ return content.slice(0, maxLen);
90
+ }
91
+ return content;
92
+ }
93
+ function mapRunStatusToFinishReason(status) {
94
+ if (!status)
95
+ return 'stop';
96
+ return CURSOR_STATUS_TO_FINISH_REASON[status] || status;
97
+ }
98
+ function resolveAgentName(options) {
99
+ if (!options)
100
+ return null;
101
+ const name = options.name;
102
+ if (name && typeof name === 'string' && name.trim())
103
+ return name.trim();
104
+ return null;
105
+ }
106
+ function resolveModelId(options) {
107
+ if (!options?.model)
108
+ return null;
109
+ const model = options.model;
110
+ if (typeof model === 'string')
111
+ return model;
112
+ if (typeof model === 'object' && model.id)
113
+ return String(model.id);
114
+ return null;
115
+ }
116
+ /**
117
+ * Stamp `openlit.agent.version_hash` (auto) and `gen_ai.agent.version`
118
+ * (user override, if set) on the span and return the same attributes so
119
+ * the caller can merge them into the inference event extras.
120
+ */
121
+ function stampAgentVersion(span, args) {
122
+ const out = {};
123
+ try {
124
+ const versionHash = helpers_1.default.computeAgentVersionHash({
125
+ systemInstructions: args.systemInstructionsJson ?? null,
126
+ toolDefinitions: args.toolDefinitionsJson ?? null,
127
+ primaryModel: args.primaryModel ?? null,
128
+ runtimeConfig: {
129
+ temperature: args.temperature ?? null,
130
+ top_p: args.top_p ?? null,
131
+ max_tokens: args.max_tokens ?? null,
132
+ provider: semantic_convention_1.default.GEN_AI_SYSTEM_CURSOR,
133
+ },
134
+ providers: [semantic_convention_1.default.GEN_AI_SYSTEM_CURSOR],
135
+ });
136
+ if (versionHash) {
137
+ out[semantic_convention_1.default.OPENLIT_AGENT_VERSION_HASH] = versionHash;
138
+ span.setAttribute(semantic_convention_1.default.OPENLIT_AGENT_VERSION_HASH, versionHash);
139
+ }
140
+ }
141
+ catch {
142
+ // Hash computation must never fail the wrapped call.
143
+ }
144
+ const versionLabel = (0, helpers_1.getCurrentAgentVersion)();
145
+ if (versionLabel) {
146
+ out[semantic_convention_1.default.GEN_AI_AGENT_VERSION] = versionLabel;
147
+ span.setAttribute(semantic_convention_1.default.GEN_AI_AGENT_VERSION, versionLabel);
148
+ }
149
+ return out;
150
+ }
151
+ function setCommonSpanAttributes(span) {
152
+ span.setAttribute(semantic_conventions_1.ATTR_TELEMETRY_SDK_NAME, constant_1.SDK_NAME);
153
+ span.setAttribute(semantic_convention_1.default.ATTR_DEPLOYMENT_ENVIRONMENT, config_1.default.environment ?? 'default');
154
+ span.setAttribute(semantic_conventions_1.ATTR_SERVICE_NAME, config_1.default.applicationName ?? 'default');
155
+ span.setAttribute(semantic_convention_1.default.GEN_AI_SDK_VERSION, constant_1.SDK_VERSION);
156
+ span.setAttribute(semantic_convention_1.default.SERVER_ADDRESS, SERVER_ADDRESS);
157
+ span.setAttribute(semantic_convention_1.default.SERVER_PORT, SERVER_PORT);
158
+ }
159
+ // ---------------------------------------------------------------------------
160
+ // Tool span tracker -- manages in-flight execute_tool spans from stream
161
+ // ---------------------------------------------------------------------------
162
+ class ToolSpanTracker {
163
+ constructor(tracer, parentSpan, captureContent) {
164
+ this._inFlight = new Map();
165
+ this._tracer = tracer;
166
+ this._parentSpan = parentSpan;
167
+ this._captureContent = captureContent;
168
+ }
169
+ startTool(toolName, callId, args) {
170
+ const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TOOLS} ${toolName}`;
171
+ const parentCtx = api_1.trace.setSpan(api_1.context.active(), this._parentSpan);
172
+ const span = this._tracer.startSpan(spanName, {
173
+ kind: api_1.SpanKind.INTERNAL,
174
+ attributes: {
175
+ [semantic_convention_1.default.GEN_AI_OPERATION]: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_TOOLS,
176
+ [semantic_convention_1.default.GEN_AI_PROVIDER_NAME_OTEL]: semantic_convention_1.default.GEN_AI_SYSTEM_CURSOR,
177
+ },
178
+ }, parentCtx);
179
+ span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_NAME, toolName);
180
+ span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_CALL_ID, callId);
181
+ span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_TYPE_OTEL, 'extension');
182
+ setCommonSpanAttributes(span);
183
+ if (this._captureContent && args != null) {
184
+ try {
185
+ const argsStr = typeof args === 'string' ? args : JSON.stringify(args);
186
+ span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_CALL_ARGUMENTS, truncateContent(argsStr));
187
+ }
188
+ catch { /* ignore */ }
189
+ }
190
+ (0, helpers_1.applyCustomSpanAttributes)(span);
191
+ this._inFlight.set(callId, span);
192
+ }
193
+ endTool(callId, result, isError = false) {
194
+ const span = this._inFlight.get(callId);
195
+ if (!span)
196
+ return;
197
+ this._inFlight.delete(callId);
198
+ if (isError) {
199
+ span.setAttribute(semantic_convention_1.default.ERROR_TYPE, 'ToolExecutionError');
200
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: 'tool execution failed' });
201
+ }
202
+ else {
203
+ if (this._captureContent && result != null) {
204
+ try {
205
+ const resultStr = typeof result === 'string' ? result : JSON.stringify(result);
206
+ span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_CALL_RESULT, truncateContent(resultStr));
207
+ }
208
+ catch { /* ignore */ }
209
+ }
210
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
211
+ }
212
+ span.end();
213
+ }
214
+ endAll() {
215
+ for (const [, span] of this._inFlight) {
216
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
217
+ span.end();
218
+ }
219
+ this._inFlight.clear();
220
+ }
221
+ }
222
+ function processStreamEvent(event, toolTracker, state) {
223
+ if (!event || !event.type)
224
+ return;
225
+ if (!state.runId && event.run_id) {
226
+ state.runId = event.run_id;
227
+ }
228
+ switch (event.type) {
229
+ case 'system': {
230
+ if (event.model) {
231
+ const modelId = typeof event.model === 'string' ? event.model : event.model?.id;
232
+ if (modelId)
233
+ state.resolvedModel = String(modelId);
234
+ }
235
+ if (Array.isArray(event.tools) && event.tools.length > 0) {
236
+ // Preserve full tool schemas (name/description/parameters) when the
237
+ // SDK provides them; fall back to name-only entries for older
238
+ // versions that emit strings.
239
+ state.toolDefinitions = event.tools.map((tool) => typeof tool === 'string' ? { name: tool } : tool);
240
+ }
241
+ if (typeof event.instructions === 'string' && event.instructions) {
242
+ state.systemInstructions = event.instructions;
243
+ }
244
+ else if (typeof event.systemPrompt === 'string' && event.systemPrompt) {
245
+ state.systemInstructions = event.systemPrompt;
246
+ }
247
+ break;
248
+ }
249
+ case 'tool_call': {
250
+ const callId = event.call_id;
251
+ const toolName = event.name || 'unknown';
252
+ const status = event.status;
253
+ if (status === 'running') {
254
+ toolTracker.startTool(toolName, callId, event.args);
255
+ state.toolCalls.push({ name: toolName, callId, args: event.args });
256
+ }
257
+ else if (status === 'completed') {
258
+ toolTracker.endTool(callId, event.result, false);
259
+ const tc = state.toolCalls.find(t => t.callId === callId);
260
+ if (tc)
261
+ tc.result = event.result;
262
+ }
263
+ else if (status === 'error') {
264
+ toolTracker.endTool(callId, event.result, true);
265
+ }
266
+ break;
267
+ }
268
+ case 'assistant': {
269
+ if (state.firstContentTimeMs === null) {
270
+ state.firstContentTimeMs = Date.now();
271
+ }
272
+ const content = event.message?.content;
273
+ if (Array.isArray(content)) {
274
+ for (const block of content) {
275
+ if (block.type === 'text' && block.text) {
276
+ state.assistantText += block.text;
277
+ }
278
+ }
279
+ }
280
+ break;
281
+ }
282
+ case 'thinking': {
283
+ if (state.firstContentTimeMs === null) {
284
+ state.firstContentTimeMs = Date.now();
285
+ }
286
+ if (event.text) {
287
+ state.thinkingText += event.text;
288
+ }
289
+ break;
290
+ }
291
+ }
292
+ }
293
+ // ---------------------------------------------------------------------------
294
+ // Build OTel input/output messages
295
+ // ---------------------------------------------------------------------------
296
+ function buildInputMessages(message) {
297
+ try {
298
+ if (typeof message === 'string') {
299
+ return JSON.stringify([{
300
+ role: 'user',
301
+ parts: [{ type: 'text', content: truncateContent(message) }],
302
+ }]);
303
+ }
304
+ const parts = [];
305
+ if (message?.text) {
306
+ parts.push({ type: 'text', content: truncateContent(message.text) });
307
+ }
308
+ if (Array.isArray(message?.images)) {
309
+ for (const img of message.images) {
310
+ parts.push({ type: 'image', mimeType: img.mimeType || 'image/png' });
311
+ }
312
+ }
313
+ if (parts.length === 0)
314
+ return null;
315
+ return JSON.stringify([{ role: 'user', parts }]);
316
+ }
317
+ catch {
318
+ return null;
319
+ }
320
+ }
321
+ function buildOutputMessages(state, finishReason) {
322
+ try {
323
+ const parts = [];
324
+ if (state.assistantText) {
325
+ parts.push({ type: 'text', content: truncateContent(state.assistantText) });
326
+ }
327
+ if (state.thinkingText) {
328
+ parts.push({ type: 'reasoning', content: truncateContent(state.thinkingText) });
329
+ }
330
+ for (const tc of state.toolCalls) {
331
+ const toolPart = {
332
+ type: 'tool_call',
333
+ id: tc.callId,
334
+ name: tc.name,
335
+ };
336
+ if (tc.args != null) {
337
+ toolPart.arguments = typeof tc.args === 'object' ? tc.args : {};
338
+ }
339
+ parts.push(toolPart);
340
+ }
341
+ if (parts.length === 0)
342
+ return null;
343
+ return JSON.stringify([{ role: 'assistant', parts, finish_reason: finishReason }]);
344
+ }
345
+ catch {
346
+ return null;
347
+ }
348
+ }
349
+ // ---------------------------------------------------------------------------
350
+ // Emit inference event for invoke_agent spans
351
+ // ---------------------------------------------------------------------------
352
+ function emitInvokeAgentEvent(span, agentId, model, responseModel, finishReason, inputTokens, outputTokens, inputMessagesJson, outputMessagesJson, systemInstructionsJson, toolDefinitionsJson, versionExtras) {
353
+ if (config_1.default.disableEvents)
354
+ return;
355
+ try {
356
+ const attributes = {
357
+ [semantic_convention_1.default.GEN_AI_OPERATION]: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_AGENT,
358
+ [semantic_convention_1.default.GEN_AI_PROVIDER_NAME_OTEL]: semantic_convention_1.default.GEN_AI_SYSTEM_CURSOR,
359
+ [semantic_convention_1.default.SERVER_ADDRESS]: SERVER_ADDRESS,
360
+ [semantic_convention_1.default.SERVER_PORT]: SERVER_PORT,
361
+ };
362
+ if (model)
363
+ attributes[semantic_convention_1.default.GEN_AI_REQUEST_MODEL] = model;
364
+ if (responseModel)
365
+ attributes[semantic_convention_1.default.GEN_AI_RESPONSE_MODEL] = responseModel;
366
+ if (agentId)
367
+ attributes[semantic_convention_1.default.GEN_AI_CONVERSATION_ID] = agentId;
368
+ if (finishReason)
369
+ attributes[semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON] = [finishReason];
370
+ if (inputTokens)
371
+ attributes[semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS] = inputTokens;
372
+ if (outputTokens)
373
+ attributes[semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS] = outputTokens;
374
+ if (inputMessagesJson != null) {
375
+ attributes[semantic_convention_1.default.GEN_AI_INPUT_MESSAGES] = inputMessagesJson;
376
+ }
377
+ if (outputMessagesJson != null) {
378
+ attributes[semantic_convention_1.default.GEN_AI_OUTPUT_MESSAGES] = outputMessagesJson;
379
+ }
380
+ if (systemInstructionsJson != null) {
381
+ attributes[semantic_convention_1.default.GEN_AI_SYSTEM_INSTRUCTIONS] = systemInstructionsJson;
382
+ }
383
+ if (toolDefinitionsJson != null) {
384
+ attributes[semantic_convention_1.default.GEN_AI_TOOL_DEFINITIONS] = toolDefinitionsJson;
385
+ }
386
+ if (versionExtras) {
387
+ Object.assign(attributes, versionExtras);
388
+ }
389
+ helpers_1.default.emitInferenceEvent(span, attributes);
390
+ }
391
+ catch { /* swallow */ }
392
+ }
393
+ // ---------------------------------------------------------------------------
394
+ // Record metrics for invoke_agent spans
395
+ // ---------------------------------------------------------------------------
396
+ function recordInvokeAgentMetrics(model, inputTokens, outputTokens, cost, duration, errorType) {
397
+ if (config_1.default.disableMetrics)
398
+ return;
399
+ try {
400
+ const attributes = {
401
+ [semantic_conventions_1.ATTR_TELEMETRY_SDK_NAME]: constant_1.SDK_NAME,
402
+ [semantic_conventions_1.ATTR_SERVICE_NAME]: config_1.default.applicationName ?? 'default',
403
+ [semantic_convention_1.default.ATTR_DEPLOYMENT_ENVIRONMENT]: config_1.default.environment ?? 'default',
404
+ [semantic_convention_1.default.GEN_AI_PROVIDER_NAME_OTEL]: semantic_convention_1.default.GEN_AI_SYSTEM_CURSOR,
405
+ [semantic_convention_1.default.GEN_AI_OPERATION]: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_AGENT,
406
+ [semantic_convention_1.default.SERVER_ADDRESS]: SERVER_ADDRESS,
407
+ [semantic_convention_1.default.SERVER_PORT]: SERVER_PORT,
408
+ };
409
+ if (model)
410
+ attributes[semantic_convention_1.default.GEN_AI_REQUEST_MODEL] = model;
411
+ if (errorType)
412
+ attributes[semantic_convention_1.default.ERROR_TYPE] = errorType;
413
+ if (metrics_1.default.genaiClientOperationDuration) {
414
+ metrics_1.default.genaiClientOperationDuration.record(duration, attributes);
415
+ }
416
+ if (inputTokens && metrics_1.default.genaiClientUsageTokens) {
417
+ metrics_1.default.genaiClientUsageTokens.record(inputTokens, {
418
+ ...attributes,
419
+ [semantic_convention_1.default.GEN_AI_TOKEN_TYPE]: semantic_convention_1.default.GEN_AI_TOKEN_TYPE_INPUT,
420
+ });
421
+ }
422
+ if (outputTokens && metrics_1.default.genaiClientUsageTokens) {
423
+ metrics_1.default.genaiClientUsageTokens.record(outputTokens, {
424
+ ...attributes,
425
+ [semantic_convention_1.default.GEN_AI_TOKEN_TYPE]: semantic_convention_1.default.GEN_AI_TOKEN_TYPE_OUTPUT,
426
+ });
427
+ }
428
+ if (cost && metrics_1.default.genaiCost) {
429
+ metrics_1.default.genaiCost.record(cost, attributes);
430
+ }
431
+ }
432
+ catch { /* swallow */ }
433
+ }
434
+ // ---------------------------------------------------------------------------
435
+ // wrapSend -- wraps agent.send() to produce invoke_agent spans
436
+ // ---------------------------------------------------------------------------
437
+ function wrapSend(tracer, originalSend, agentId, agentName, modelId) {
438
+ return function wrappedSend(message, options) {
439
+ const captureContent = config_1.default.captureMessageContent ?? true;
440
+ const displayName = agentName || agentId;
441
+ const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_AGENT} ${displayName}`;
442
+ const requestModel = modelId || resolveModelId(options) || 'unknown';
443
+ const creationInfo = agentRegistry.get(this);
444
+ const creationSpanCtx = creationInfo?.spanContext;
445
+ const links = [];
446
+ if (creationSpanCtx) {
447
+ links.push({ context: creationSpanCtx });
448
+ }
449
+ // Start invoke_agent in the same trace as create_agent by using its
450
+ // span context as parent. This keeps both spans in one trace while
451
+ // the span link provides explicit correlation.
452
+ let parentCtx = api_1.context.active();
453
+ if (creationSpanCtx) {
454
+ const remoteSpan = api_1.trace.wrapSpanContext(creationSpanCtx);
455
+ parentCtx = api_1.trace.setSpan(parentCtx, remoteSpan);
456
+ }
457
+ const span = tracer.startSpan(spanName, {
458
+ kind: api_1.SpanKind.CLIENT,
459
+ attributes: {
460
+ [semantic_convention_1.default.GEN_AI_OPERATION]: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_AGENT,
461
+ [semantic_convention_1.default.GEN_AI_PROVIDER_NAME_OTEL]: semantic_convention_1.default.GEN_AI_SYSTEM_CURSOR,
462
+ [semantic_convention_1.default.GEN_AI_REQUEST_MODEL]: requestModel,
463
+ [semantic_convention_1.default.SERVER_ADDRESS]: SERVER_ADDRESS,
464
+ [semantic_convention_1.default.SERVER_PORT]: SERVER_PORT,
465
+ },
466
+ links,
467
+ }, parentCtx);
468
+ setCommonSpanAttributes(span);
469
+ span.setAttribute(semantic_convention_1.default.GEN_AI_AGENT_ID, agentId);
470
+ span.setAttribute(semantic_convention_1.default.GEN_AI_CONVERSATION_ID, agentId);
471
+ if (agentName)
472
+ span.setAttribute(semantic_convention_1.default.GEN_AI_AGENT_NAME, agentName);
473
+ if (captureContent) {
474
+ const inputJson = buildInputMessages(message);
475
+ if (inputJson)
476
+ span.setAttribute(semantic_convention_1.default.GEN_AI_INPUT_MESSAGES, inputJson);
477
+ }
478
+ (0, helpers_1.applyCustomSpanAttributes)(span);
479
+ const startTime = Date.now() / 1000;
480
+ const startTimeMs = Date.now();
481
+ const usage = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 };
482
+ const toolTracker = new ToolSpanTracker(tracer, span, captureContent);
483
+ const streamState = {
484
+ assistantText: '',
485
+ thinkingText: '',
486
+ toolCalls: [],
487
+ resolvedModel: null,
488
+ toolDefinitions: null,
489
+ systemInstructions: null,
490
+ runId: null,
491
+ firstContentTimeMs: null,
492
+ };
493
+ // Seed system_instructions and tool_definitions from `Agent.create` options
494
+ // so they're available before the system stream event arrives.
495
+ const createOptions = creationInfo?.options;
496
+ if (createOptions) {
497
+ const seedInstructions = (typeof createOptions.instructions === 'string' && createOptions.instructions) ||
498
+ (typeof createOptions.systemPrompt === 'string' && createOptions.systemPrompt) ||
499
+ null;
500
+ if (seedInstructions)
501
+ streamState.systemInstructions = seedInstructions;
502
+ if (Array.isArray(createOptions.tools) && createOptions.tools.length > 0) {
503
+ streamState.toolDefinitions = createOptions.tools.slice();
504
+ }
505
+ }
506
+ const userOnDelta = options?.onDelta;
507
+ const mergedOptions = { ...options };
508
+ mergedOptions.onDelta = async (args) => {
509
+ try {
510
+ const update = args?.update;
511
+ if (update?.type === 'turn-ended' && update.usage) {
512
+ usage.inputTokens += update.usage.inputTokens || 0;
513
+ usage.outputTokens += update.usage.outputTokens || 0;
514
+ usage.cacheReadTokens += update.usage.cacheReadTokens || 0;
515
+ usage.cacheWriteTokens += update.usage.cacheWriteTokens || 0;
516
+ }
517
+ }
518
+ catch { /* swallow */ }
519
+ if (userOnDelta) {
520
+ return userOnDelta(args);
521
+ }
522
+ };
523
+ const spanContext = api_1.trace.setSpan(api_1.context.active(), span);
524
+ (0, helpers_1.setFrameworkLlmActive)();
525
+ let runPromise;
526
+ try {
527
+ runPromise = api_1.context.with(spanContext, () => {
528
+ return originalSend.call(this, message, mergedOptions);
529
+ });
530
+ }
531
+ catch (e) {
532
+ (0, helpers_1.resetFrameworkLlmActive)();
533
+ helpers_1.default.handleException(span, e);
534
+ span.end();
535
+ throw e;
536
+ }
537
+ return runPromise.then((run) => {
538
+ return createRunProxy(run, tracer, span, startTime, startTimeMs, usage, toolTracker, streamState, captureContent, agentId, agentName, requestModel, message);
539
+ }).catch((e) => {
540
+ (0, helpers_1.resetFrameworkLlmActive)();
541
+ helpers_1.default.handleException(span, e);
542
+ recordInvokeAgentMetrics(requestModel, 0, 0, 0, (Date.now() / 1000) - startTime, e?.constructor?.name || '_OTHER');
543
+ span.end();
544
+ throw e;
545
+ });
546
+ };
547
+ }
548
+ // ---------------------------------------------------------------------------
549
+ // createRunProxy -- proxies the Run to intercept stream() and wait()
550
+ // ---------------------------------------------------------------------------
551
+ function createRunProxy(run, tracer, span, startTime, startTimeMs, usage, toolTracker, streamState, captureContent, agentId, agentName, requestModel, message) {
552
+ let finalized = false;
553
+ let isStreamMode = false;
554
+ const finalizeSpan = (result, error) => {
555
+ if (finalized)
556
+ return;
557
+ finalized = true;
558
+ (0, helpers_1.resetFrameworkLlmActive)();
559
+ toolTracker.endAll();
560
+ const duration = (Date.now() / 1000) - startTime;
561
+ const status = result?.status || run.status || 'finished';
562
+ const finishReason = mapRunStatusToFinishReason(status);
563
+ const responseModel = streamState.resolvedModel || result?.model?.id || run.model?.id || null;
564
+ const durationMs = result?.durationMs || run.durationMs;
565
+ if (responseModel)
566
+ span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_MODEL, responseModel);
567
+ span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON, [finishReason]);
568
+ const runId = streamState.runId || run.id;
569
+ if (runId)
570
+ span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_ID, runId);
571
+ if (isStreamMode) {
572
+ span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_STREAM, true);
573
+ }
574
+ if (usage.inputTokens)
575
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS, usage.inputTokens);
576
+ if (usage.outputTokens)
577
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS, usage.outputTokens);
578
+ if (usage.cacheReadTokens)
579
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, usage.cacheReadTokens);
580
+ if (usage.cacheWriteTokens)
581
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, usage.cacheWriteTokens);
582
+ const effectiveDuration = durationMs ? durationMs / 1000 : duration;
583
+ span.setAttribute(semantic_convention_1.default.GEN_AI_CLIENT_OPERATION_DURATION, effectiveDuration);
584
+ if (isStreamMode && streamState.firstContentTimeMs !== null) {
585
+ const ttft = (streamState.firstContentTimeMs - startTimeMs) / 1000;
586
+ span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft);
587
+ }
588
+ const toolDefinitionsJson = streamState.toolDefinitions
589
+ ? helpers_1.default.buildToolDefinitions(streamState.toolDefinitions)
590
+ : undefined;
591
+ if (toolDefinitionsJson) {
592
+ span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_DEFINITIONS, toolDefinitionsJson);
593
+ }
594
+ // Compute system_instructions JSON regardless of captureContent so
595
+ // versions still group correctly when content capture is disabled.
596
+ const systemInstructionsJson = streamState.systemInstructions
597
+ ? JSON.stringify([{ type: 'text', content: streamState.systemInstructions }])
598
+ : undefined;
599
+ if (captureContent && systemInstructionsJson) {
600
+ span.setAttribute(semantic_convention_1.default.GEN_AI_SYSTEM_INSTRUCTIONS, systemInstructionsJson);
601
+ }
602
+ const versionExtras = stampAgentVersion(span, {
603
+ systemInstructionsJson,
604
+ toolDefinitionsJson,
605
+ primaryModel: responseModel || requestModel,
606
+ temperature: null,
607
+ top_p: null,
608
+ max_tokens: null,
609
+ });
610
+ const pricingInfo = config_1.default.pricingInfo || {};
611
+ const effectiveModel = responseModel || requestModel;
612
+ const cost = effectiveModel
613
+ ? helpers_1.default.getChatModelCost(effectiveModel, pricingInfo, usage.inputTokens, usage.outputTokens)
614
+ : 0;
615
+ if (cost)
616
+ span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_COST, cost);
617
+ let outputMessagesJson = null;
618
+ if (captureContent) {
619
+ const resultText = result?.result || run.result;
620
+ if (resultText && !streamState.assistantText) {
621
+ streamState.assistantText = resultText;
622
+ }
623
+ outputMessagesJson = buildOutputMessages(streamState, finishReason);
624
+ if (outputMessagesJson) {
625
+ span.setAttribute(semantic_convention_1.default.GEN_AI_OUTPUT_MESSAGES, outputMessagesJson);
626
+ }
627
+ }
628
+ if (error) {
629
+ helpers_1.default.handleException(span, error instanceof Error ? error : new Error(String(error)));
630
+ }
631
+ else if (status === 'error') {
632
+ span.setAttribute(semantic_convention_1.default.ERROR_TYPE, 'AgentError');
633
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: result?.result || 'agent error' });
634
+ }
635
+ else {
636
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
637
+ }
638
+ const inputMessagesJson = captureContent ? buildInputMessages(message) : null;
639
+ emitInvokeAgentEvent(span, agentId, requestModel, responseModel, finishReason, usage.inputTokens, usage.outputTokens, inputMessagesJson, outputMessagesJson, systemInstructionsJson ?? null, toolDefinitionsJson ?? null, versionExtras);
640
+ recordInvokeAgentMetrics(requestModel, usage.inputTokens, usage.outputTokens, cost, duration, error ? (error.constructor?.name || '_OTHER') : (status === 'error' ? 'AgentError' : undefined));
641
+ span.end();
642
+ };
643
+ return new Proxy(run, {
644
+ get(target, prop, receiver) {
645
+ if (prop === 'stream') {
646
+ const originalStream = target.stream;
647
+ if (typeof originalStream !== 'function')
648
+ return originalStream;
649
+ return function (...streamArgs) {
650
+ isStreamMode = true;
651
+ const generator = originalStream.apply(target, streamArgs);
652
+ return wrapAsyncGenerator(generator, toolTracker, streamState, finalizeSpan);
653
+ };
654
+ }
655
+ if (prop === 'wait') {
656
+ const originalWait = target.wait;
657
+ if (typeof originalWait !== 'function')
658
+ return originalWait;
659
+ return function (...waitArgs) {
660
+ return originalWait.apply(target, waitArgs).then((result) => {
661
+ finalizeSpan(result);
662
+ return result;
663
+ }).catch((e) => {
664
+ finalizeSpan(undefined, e);
665
+ throw e;
666
+ });
667
+ };
668
+ }
669
+ if (prop === 'cancel') {
670
+ const originalCancel = target.cancel;
671
+ if (typeof originalCancel !== 'function')
672
+ return originalCancel;
673
+ return function (...cancelArgs) {
674
+ return originalCancel.apply(target, cancelArgs).then((result) => {
675
+ finalizeSpan({ status: 'cancelled' });
676
+ return result;
677
+ });
678
+ };
679
+ }
680
+ return Reflect.get(target, prop, receiver);
681
+ },
682
+ });
683
+ }
684
+ // ---------------------------------------------------------------------------
685
+ // wrapAsyncGenerator -- wraps run.stream() to intercept SDKMessage events
686
+ // ---------------------------------------------------------------------------
687
+ async function* wrapAsyncGenerator(generator, toolTracker, streamState, finalizeSpan) {
688
+ try {
689
+ for await (const event of generator) {
690
+ try {
691
+ processStreamEvent(event, toolTracker, streamState);
692
+ }
693
+ catch { /* swallow processing errors */ }
694
+ yield event;
695
+ }
696
+ finalizeSpan();
697
+ }
698
+ catch (e) {
699
+ finalizeSpan(undefined, e);
700
+ throw e;
701
+ }
702
+ }
703
+ // ---------------------------------------------------------------------------
704
+ // patchAgentCreate -- wraps Agent.create() for create_agent spans
705
+ // ---------------------------------------------------------------------------
706
+ function patchAgentCreate(tracer) {
707
+ return (originalCreate) => {
708
+ return async function wrappedCreate(options) {
709
+ const agentName = resolveAgentName(options);
710
+ const modelId = resolveModelId(options);
711
+ const displayName = agentName || 'cursor-agent';
712
+ const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CREATE_AGENT} ${displayName}`;
713
+ const span = tracer.startSpan(spanName, {
714
+ kind: api_1.SpanKind.CLIENT,
715
+ attributes: {
716
+ [semantic_convention_1.default.GEN_AI_OPERATION]: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CREATE_AGENT,
717
+ [semantic_convention_1.default.GEN_AI_PROVIDER_NAME_OTEL]: semantic_convention_1.default.GEN_AI_SYSTEM_CURSOR,
718
+ [semantic_convention_1.default.SERVER_ADDRESS]: SERVER_ADDRESS,
719
+ [semantic_convention_1.default.SERVER_PORT]: SERVER_PORT,
720
+ },
721
+ });
722
+ setCommonSpanAttributes(span);
723
+ if (agentName)
724
+ span.setAttribute(semantic_convention_1.default.GEN_AI_AGENT_NAME, agentName);
725
+ if (modelId)
726
+ span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_MODEL, modelId);
727
+ const captureContent = config_1.default.captureMessageContent ?? true;
728
+ const instructionsText = (options && typeof options.instructions === 'string' && options.instructions) ||
729
+ (options && typeof options.systemPrompt === 'string' && options.systemPrompt) ||
730
+ null;
731
+ if (captureContent && instructionsText) {
732
+ span.setAttribute(semantic_convention_1.default.GEN_AI_SYSTEM_INSTRUCTIONS, JSON.stringify([{ type: 'text', content: instructionsText }]));
733
+ }
734
+ const optionTools = Array.isArray(options?.tools) ? options.tools : undefined;
735
+ const toolDefinitionsJson = helpers_1.default.buildToolDefinitions(optionTools);
736
+ if (toolDefinitionsJson) {
737
+ span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_DEFINITIONS, toolDefinitionsJson);
738
+ }
739
+ (0, helpers_1.applyCustomSpanAttributes)(span);
740
+ try {
741
+ const agent = await originalCreate.call(this, options);
742
+ const agentId = agent.agentId;
743
+ if (agentId) {
744
+ span.setAttribute(semantic_convention_1.default.GEN_AI_AGENT_ID, agentId);
745
+ }
746
+ agentRegistry.register(agent, span.spanContext(), options);
747
+ const resolvedModel = agent.model?.id || modelId;
748
+ if (resolvedModel)
749
+ span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_MODEL, resolvedModel);
750
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
751
+ span.end();
752
+ if (typeof agent.send === 'function') {
753
+ const originalAgentSend = agent.send.bind(agent);
754
+ agent.send = wrapSend(tracer, originalAgentSend, agentId, agentName, resolvedModel);
755
+ }
756
+ return agent;
757
+ }
758
+ catch (e) {
759
+ helpers_1.default.handleException(span, e);
760
+ span.end();
761
+ throw e;
762
+ }
763
+ };
764
+ };
765
+ }
766
+ // ---------------------------------------------------------------------------
767
+ // patchAgentResume -- wraps Agent.resume() to patch send() on resumed agents
768
+ // ---------------------------------------------------------------------------
769
+ function patchAgentResume(tracer) {
770
+ return (originalResume) => {
771
+ return async function wrappedResume(agentId, options) {
772
+ const agentName = resolveAgentName(options);
773
+ const modelId = resolveModelId(options);
774
+ const agent = await originalResume.call(this, agentId, options);
775
+ const resolvedAgentId = agent.agentId || agentId;
776
+ const resolvedModel = agent.model?.id || modelId;
777
+ if (typeof agent.send === 'function') {
778
+ const originalAgentSend = agent.send.bind(agent);
779
+ agent.send = wrapSend(tracer, originalAgentSend, resolvedAgentId, agentName, resolvedModel);
780
+ }
781
+ return agent;
782
+ };
783
+ };
784
+ }
785
+ //# sourceMappingURL=wrapper.js.map