shield360 1.0.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.
- package/LICENSE +201 -0
- package/README.md +310 -0
- package/dist/__tests__/manual-trace.test.d.ts +1 -0
- package/dist/__tests__/manual-trace.test.js +118 -0
- package/dist/__tests__/manual-trace.test.js.map +1 -0
- package/dist/__tests__/register.test.d.ts +0 -0
- package/dist/__tests__/register.test.js +90 -0
- package/dist/__tests__/register.test.js.map +1 -0
- package/dist/config.d.ts +28 -0
- package/dist/config.js +22 -0
- package/dist/config.js.map +1 -0
- package/dist/constant.d.ts +6 -0
- package/dist/constant.js +14 -0
- package/dist/constant.js.map +1 -0
- package/dist/evals/__tests__/offline.test.d.ts +1 -0
- package/dist/evals/__tests__/offline.test.js +427 -0
- package/dist/evals/__tests__/offline.test.js.map +1 -0
- package/dist/evals/index.d.ts +2 -0
- package/dist/evals/index.js +15 -0
- package/dist/evals/index.js.map +1 -0
- package/dist/evals/offline.d.ts +6 -0
- package/dist/evals/offline.js +350 -0
- package/dist/evals/offline.js.map +1 -0
- package/dist/evals/types.d.ts +74 -0
- package/dist/evals/types.js +23 -0
- package/dist/evals/types.js.map +1 -0
- package/dist/features/__tests__/rule-engine.test.d.ts +1 -0
- package/dist/features/__tests__/rule-engine.test.js +146 -0
- package/dist/features/__tests__/rule-engine.test.js.map +1 -0
- package/dist/features/base.d.ts +8 -0
- package/dist/features/base.js +15 -0
- package/dist/features/base.js.map +1 -0
- package/dist/features/prompt-hub.d.ts +4 -0
- package/dist/features/prompt-hub.js +66 -0
- package/dist/features/prompt-hub.js.map +1 -0
- package/dist/features/rule-engine.d.ts +6 -0
- package/dist/features/rule-engine.js +60 -0
- package/dist/features/rule-engine.js.map +1 -0
- package/dist/features/vault.d.ts +4 -0
- package/dist/features/vault.js +63 -0
- package/dist/features/vault.js.map +1 -0
- package/dist/guard/__tests__/findings.test.d.ts +1 -0
- package/dist/guard/__tests__/findings.test.js +79 -0
- package/dist/guard/__tests__/findings.test.js.map +1 -0
- package/dist/guard/__tests__/integration.test.d.ts +1 -0
- package/dist/guard/__tests__/integration.test.js +215 -0
- package/dist/guard/__tests__/integration.test.js.map +1 -0
- package/dist/guard/__tests__/pii.test.d.ts +1 -0
- package/dist/guard/__tests__/pii.test.js +131 -0
- package/dist/guard/__tests__/pii.test.js.map +1 -0
- package/dist/guard/__tests__/pipeline.test.d.ts +1 -0
- package/dist/guard/__tests__/pipeline.test.js +162 -0
- package/dist/guard/__tests__/pipeline.test.js.map +1 -0
- package/dist/guard/__tests__/prompt-injection.test.d.ts +1 -0
- package/dist/guard/__tests__/prompt-injection.test.js +92 -0
- package/dist/guard/__tests__/prompt-injection.test.js.map +1 -0
- package/dist/guard/__tests__/report.test.d.ts +1 -0
- package/dist/guard/__tests__/report.test.js +50 -0
- package/dist/guard/__tests__/report.test.js.map +1 -0
- package/dist/guard/base.d.ts +79 -0
- package/dist/guard/base.js +136 -0
- package/dist/guard/base.js.map +1 -0
- package/dist/guard/custom.d.ts +19 -0
- package/dist/guard/custom.js +46 -0
- package/dist/guard/custom.js.map +1 -0
- package/dist/guard/findings.d.ts +31 -0
- package/dist/guard/findings.js +156 -0
- package/dist/guard/findings.js.map +1 -0
- package/dist/guard/index.d.ts +24 -0
- package/dist/guard/index.js +42 -0
- package/dist/guard/index.js.map +1 -0
- package/dist/guard/integration.d.ts +44 -0
- package/dist/guard/integration.js +363 -0
- package/dist/guard/integration.js.map +1 -0
- package/dist/guard/moderation.d.ts +18 -0
- package/dist/guard/moderation.js +61 -0
- package/dist/guard/moderation.js.map +1 -0
- package/dist/guard/pii.d.ts +17 -0
- package/dist/guard/pii.js +114 -0
- package/dist/guard/pii.js.map +1 -0
- package/dist/guard/pipeline.d.ts +26 -0
- package/dist/guard/pipeline.js +139 -0
- package/dist/guard/pipeline.js.map +1 -0
- package/dist/guard/prompt-injection.d.ts +21 -0
- package/dist/guard/prompt-injection.js +70 -0
- package/dist/guard/prompt-injection.js.map +1 -0
- package/dist/guard/report.d.ts +34 -0
- package/dist/guard/report.js +182 -0
- package/dist/guard/report.js.map +1 -0
- package/dist/guard/schema.d.ts +20 -0
- package/dist/guard/schema.js +99 -0
- package/dist/guard/schema.js.map +1 -0
- package/dist/guard/sensitive-topic.d.ts +23 -0
- package/dist/guard/sensitive-topic.js +89 -0
- package/dist/guard/sensitive-topic.js.map +1 -0
- package/dist/guard/topic-restriction.d.ts +22 -0
- package/dist/guard/topic-restriction.js +54 -0
- package/dist/guard/topic-restriction.js.map +1 -0
- package/dist/helpers.d.ts +246 -0
- package/dist/helpers.js +903 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +234 -0
- package/dist/index.js.map +1 -0
- package/dist/instrumentation/__tests__/ai21-rag-trace-comparison.test.d.ts +13 -0
- package/dist/instrumentation/__tests__/ai21-rag-trace-comparison.test.js +176 -0
- package/dist/instrumentation/__tests__/ai21-rag-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/ai21-trace-comparison.test.d.ts +10 -0
- package/dist/instrumentation/__tests__/ai21-trace-comparison.test.js +203 -0
- package/dist/instrumentation/__tests__/ai21-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/anthropic-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/anthropic-wrapper.test.js +284 -0
- package/dist/instrumentation/__tests__/anthropic-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/assemblyai-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/assemblyai-wrapper.test.js +284 -0
- package/dist/instrumentation/__tests__/assemblyai-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/astra-trace-comparison.test.d.ts +7 -0
- package/dist/instrumentation/__tests__/astra-trace-comparison.test.js +160 -0
- package/dist/instrumentation/__tests__/astra-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/base-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/base-wrapper.test.js +171 -0
- package/dist/instrumentation/__tests__/base-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/bedrock-trace-comparison.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/bedrock-trace-comparison.test.js +422 -0
- package/dist/instrumentation/__tests__/bedrock-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/browser-use-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/browser-use-wrapper.test.js +284 -0
- package/dist/instrumentation/__tests__/browser-use-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/cache-pricing.test.d.ts +15 -0
- package/dist/instrumentation/__tests__/cache-pricing.test.js +155 -0
- package/dist/instrumentation/__tests__/cache-pricing.test.js.map +1 -0
- package/dist/instrumentation/__tests__/chroma-trace-comparison.test.d.ts +18 -0
- package/dist/instrumentation/__tests__/chroma-trace-comparison.test.js +209 -0
- package/dist/instrumentation/__tests__/chroma-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/cohere-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/cohere-wrapper.test.js +256 -0
- package/dist/instrumentation/__tests__/cohere-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/elevenlabs-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/elevenlabs-wrapper.test.js +282 -0
- package/dist/instrumentation/__tests__/elevenlabs-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/firecrawl-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/firecrawl-wrapper.test.js +238 -0
- package/dist/instrumentation/__tests__/firecrawl-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/google-ai-trace-comparison.test.d.ts +4 -0
- package/dist/instrumentation/__tests__/google-ai-trace-comparison.test.js +229 -0
- package/dist/instrumentation/__tests__/google-ai-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/gpu-collector.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/gpu-collector.test.js +247 -0
- package/dist/instrumentation/__tests__/gpu-collector.test.js.map +1 -0
- package/dist/instrumentation/__tests__/gradient-trace-comparison.test.d.ts +11 -0
- package/dist/instrumentation/__tests__/gradient-trace-comparison.test.js +391 -0
- package/dist/instrumentation/__tests__/gradient-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/groq-trace-comparison.test.d.ts +7 -0
- package/dist/instrumentation/__tests__/groq-trace-comparison.test.js +687 -0
- package/dist/instrumentation/__tests__/groq-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/helpers-genai.test.d.ts +14 -0
- package/dist/instrumentation/__tests__/helpers-genai.test.js +347 -0
- package/dist/instrumentation/__tests__/helpers-genai.test.js.map +1 -0
- package/dist/instrumentation/__tests__/huggingface-trace-comparison.test.d.ts +7 -0
- package/dist/instrumentation/__tests__/huggingface-trace-comparison.test.js +529 -0
- package/dist/instrumentation/__tests__/huggingface-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/langchain-role-map.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/langchain-role-map.test.js +19 -0
- package/dist/instrumentation/__tests__/langchain-role-map.test.js.map +1 -0
- package/dist/instrumentation/__tests__/langchain-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/langchain-wrapper.test.js +282 -0
- package/dist/instrumentation/__tests__/langchain-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/letta-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/letta-wrapper.test.js +285 -0
- package/dist/instrumentation/__tests__/letta-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/mem0-trace-comparison.test.d.ts +13 -0
- package/dist/instrumentation/__tests__/mem0-trace-comparison.test.js +208 -0
- package/dist/instrumentation/__tests__/mem0-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/milvus-trace-comparison.test.d.ts +17 -0
- package/dist/instrumentation/__tests__/milvus-trace-comparison.test.js +206 -0
- package/dist/instrumentation/__tests__/milvus-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/mistral-trace-comparison.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/mistral-trace-comparison.test.js +345 -0
- package/dist/instrumentation/__tests__/mistral-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/openai-wrapper.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/openai-wrapper.test.js +179 -0
- package/dist/instrumentation/__tests__/openai-wrapper.test.js.map +1 -0
- package/dist/instrumentation/__tests__/pg-trace-comparison.test.d.ts +21 -0
- package/dist/instrumentation/__tests__/pg-trace-comparison.test.js +455 -0
- package/dist/instrumentation/__tests__/pg-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/qdrant-trace-comparison.test.d.ts +17 -0
- package/dist/instrumentation/__tests__/qdrant-trace-comparison.test.js +182 -0
- package/dist/instrumentation/__tests__/qdrant-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/replicate-trace-comparison.test.d.ts +8 -0
- package/dist/instrumentation/__tests__/replicate-trace-comparison.test.js +346 -0
- package/dist/instrumentation/__tests__/replicate-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/together-trace-comparison.test.d.ts +4 -0
- package/dist/instrumentation/__tests__/together-trace-comparison.test.js +289 -0
- package/dist/instrumentation/__tests__/together-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/trace-comparison-utils.d.ts +67 -0
- package/dist/instrumentation/__tests__/trace-comparison-utils.js +255 -0
- package/dist/instrumentation/__tests__/trace-comparison-utils.js.map +1 -0
- package/dist/instrumentation/__tests__/transformers-trace-comparison.test.d.ts +11 -0
- package/dist/instrumentation/__tests__/transformers-trace-comparison.test.js +214 -0
- package/dist/instrumentation/__tests__/transformers-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/vercel-ai-trace-comparison.test.d.ts +8 -0
- package/dist/instrumentation/__tests__/vercel-ai-trace-comparison.test.js +446 -0
- package/dist/instrumentation/__tests__/vercel-ai-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/vertexai-trace-comparison.test.d.ts +6 -0
- package/dist/instrumentation/__tests__/vertexai-trace-comparison.test.js +409 -0
- package/dist/instrumentation/__tests__/vertexai-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/__tests__/vllm-trace-comparison.test.d.ts +1 -0
- package/dist/instrumentation/__tests__/vllm-trace-comparison.test.js +519 -0
- package/dist/instrumentation/__tests__/vllm-trace-comparison.test.js.map +1 -0
- package/dist/instrumentation/ai21/index.d.ts +11 -0
- package/dist/instrumentation/ai21/index.js +67 -0
- package/dist/instrumentation/ai21/index.js.map +1 -0
- package/dist/instrumentation/ai21/wrapper.d.ts +54 -0
- package/dist/instrumentation/ai21/wrapper.js +511 -0
- package/dist/instrumentation/ai21/wrapper.js.map +1 -0
- package/dist/instrumentation/anthropic/index.d.ts +11 -0
- package/dist/instrumentation/anthropic/index.js +45 -0
- package/dist/instrumentation/anthropic/index.js.map +1 -0
- package/dist/instrumentation/anthropic/wrapper.d.ts +34 -0
- package/dist/instrumentation/anthropic/wrapper.js +426 -0
- package/dist/instrumentation/anthropic/wrapper.js.map +1 -0
- package/dist/instrumentation/assemblyai/index.d.ts +12 -0
- package/dist/instrumentation/assemblyai/index.js +91 -0
- package/dist/instrumentation/assemblyai/index.js.map +1 -0
- package/dist/instrumentation/assemblyai/wrapper.d.ts +35 -0
- package/dist/instrumentation/assemblyai/wrapper.js +238 -0
- package/dist/instrumentation/assemblyai/wrapper.js.map +1 -0
- package/dist/instrumentation/astra/index.d.ts +11 -0
- package/dist/instrumentation/astra/index.js +87 -0
- package/dist/instrumentation/astra/index.js.map +1 -0
- package/dist/instrumentation/astra/wrapper.d.ts +39 -0
- package/dist/instrumentation/astra/wrapper.js +349 -0
- package/dist/instrumentation/astra/wrapper.js.map +1 -0
- package/dist/instrumentation/azure-ai-inference/index.d.ts +11 -0
- package/dist/instrumentation/azure-ai-inference/index.js +76 -0
- package/dist/instrumentation/azure-ai-inference/index.js.map +1 -0
- package/dist/instrumentation/azure-ai-inference/wrapper.d.ts +55 -0
- package/dist/instrumentation/azure-ai-inference/wrapper.js +580 -0
- package/dist/instrumentation/azure-ai-inference/wrapper.js.map +1 -0
- package/dist/instrumentation/base-wrapper.d.ts +16 -0
- package/dist/instrumentation/base-wrapper.js +139 -0
- package/dist/instrumentation/base-wrapper.js.map +1 -0
- package/dist/instrumentation/bedrock/index.d.ts +11 -0
- package/dist/instrumentation/bedrock/index.js +52 -0
- package/dist/instrumentation/bedrock/index.js.map +1 -0
- package/dist/instrumentation/bedrock/wrapper.d.ts +41 -0
- package/dist/instrumentation/bedrock/wrapper.js +494 -0
- package/dist/instrumentation/bedrock/wrapper.js.map +1 -0
- package/dist/instrumentation/browser-use/index.d.ts +11 -0
- package/dist/instrumentation/browser-use/index.js +98 -0
- package/dist/instrumentation/browser-use/index.js.map +1 -0
- package/dist/instrumentation/browser-use/wrapper.d.ts +12 -0
- package/dist/instrumentation/browser-use/wrapper.js +706 -0
- package/dist/instrumentation/browser-use/wrapper.js.map +1 -0
- package/dist/instrumentation/chroma/index.d.ts +11 -0
- package/dist/instrumentation/chroma/index.js +70 -0
- package/dist/instrumentation/chroma/index.js.map +1 -0
- package/dist/instrumentation/chroma/wrapper.d.ts +10 -0
- package/dist/instrumentation/chroma/wrapper.js +127 -0
- package/dist/instrumentation/chroma/wrapper.js.map +1 -0
- package/dist/instrumentation/claude-agent-sdk/index.d.ts +23 -0
- package/dist/instrumentation/claude-agent-sdk/index.js +83 -0
- package/dist/instrumentation/claude-agent-sdk/index.js.map +1 -0
- package/dist/instrumentation/claude-agent-sdk/wrapper.d.ts +13 -0
- package/dist/instrumentation/claude-agent-sdk/wrapper.js +1123 -0
- package/dist/instrumentation/claude-agent-sdk/wrapper.js.map +1 -0
- package/dist/instrumentation/cohere/index.d.ts +11 -0
- package/dist/instrumentation/cohere/index.js +53 -0
- package/dist/instrumentation/cohere/index.js.map +1 -0
- package/dist/instrumentation/cohere/wrapper.d.ts +50 -0
- package/dist/instrumentation/cohere/wrapper.js +452 -0
- package/dist/instrumentation/cohere/wrapper.js.map +1 -0
- package/dist/instrumentation/cursor-sdk/index.d.ts +21 -0
- package/dist/instrumentation/cursor-sdk/index.js +58 -0
- package/dist/instrumentation/cursor-sdk/index.js.map +1 -0
- package/dist/instrumentation/cursor-sdk/wrapper.d.ts +17 -0
- package/dist/instrumentation/cursor-sdk/wrapper.js +789 -0
- package/dist/instrumentation/cursor-sdk/wrapper.js.map +1 -0
- package/dist/instrumentation/elevenlabs/index.d.ts +12 -0
- package/dist/instrumentation/elevenlabs/index.js +92 -0
- package/dist/instrumentation/elevenlabs/index.js.map +1 -0
- package/dist/instrumentation/elevenlabs/wrapper.d.ts +34 -0
- package/dist/instrumentation/elevenlabs/wrapper.js +280 -0
- package/dist/instrumentation/elevenlabs/wrapper.js.map +1 -0
- package/dist/instrumentation/firecrawl/index.d.ts +13 -0
- package/dist/instrumentation/firecrawl/index.js +115 -0
- package/dist/instrumentation/firecrawl/index.js.map +1 -0
- package/dist/instrumentation/firecrawl/wrapper.d.ts +22 -0
- package/dist/instrumentation/firecrawl/wrapper.js +378 -0
- package/dist/instrumentation/firecrawl/wrapper.js.map +1 -0
- package/dist/instrumentation/google-adk/index.d.ts +57 -0
- package/dist/instrumentation/google-adk/index.js +371 -0
- package/dist/instrumentation/google-adk/index.js.map +1 -0
- package/dist/instrumentation/google-adk/utils.d.ts +45 -0
- package/dist/instrumentation/google-adk/utils.js +663 -0
- package/dist/instrumentation/google-adk/utils.js.map +1 -0
- package/dist/instrumentation/google-adk/wrapper.d.ts +11 -0
- package/dist/instrumentation/google-adk/wrapper.js +391 -0
- package/dist/instrumentation/google-adk/wrapper.js.map +1 -0
- package/dist/instrumentation/google-ai/index.d.ts +11 -0
- package/dist/instrumentation/google-ai/index.js +48 -0
- package/dist/instrumentation/google-ai/index.js.map +1 -0
- package/dist/instrumentation/google-ai/wrapper.d.ts +52 -0
- package/dist/instrumentation/google-ai/wrapper.js +466 -0
- package/dist/instrumentation/google-ai/wrapper.js.map +1 -0
- package/dist/instrumentation/gpu/index.d.ts +38 -0
- package/dist/instrumentation/gpu/index.js +233 -0
- package/dist/instrumentation/gpu/index.js.map +1 -0
- package/dist/instrumentation/gradient/index.d.ts +12 -0
- package/dist/instrumentation/gradient/index.js +74 -0
- package/dist/instrumentation/gradient/index.js.map +1 -0
- package/dist/instrumentation/gradient/utils.d.ts +14 -0
- package/dist/instrumentation/gradient/utils.js +119 -0
- package/dist/instrumentation/gradient/utils.js.map +1 -0
- package/dist/instrumentation/gradient/wrapper.d.ts +76 -0
- package/dist/instrumentation/gradient/wrapper.js +584 -0
- package/dist/instrumentation/gradient/wrapper.js.map +1 -0
- package/dist/instrumentation/groq/index.d.ts +11 -0
- package/dist/instrumentation/groq/index.js +43 -0
- package/dist/instrumentation/groq/index.js.map +1 -0
- package/dist/instrumentation/groq/wrapper.d.ts +35 -0
- package/dist/instrumentation/groq/wrapper.js +442 -0
- package/dist/instrumentation/groq/wrapper.js.map +1 -0
- package/dist/instrumentation/huggingface/index.d.ts +13 -0
- package/dist/instrumentation/huggingface/index.js +110 -0
- package/dist/instrumentation/huggingface/index.js.map +1 -0
- package/dist/instrumentation/huggingface/wrapper.d.ts +54 -0
- package/dist/instrumentation/huggingface/wrapper.js +514 -0
- package/dist/instrumentation/huggingface/wrapper.js.map +1 -0
- package/dist/instrumentation/index.d.ts +7 -0
- package/dist/instrumentation/index.js +176 -0
- package/dist/instrumentation/index.js.map +1 -0
- package/dist/instrumentation/langchain/index.d.ts +16 -0
- package/dist/instrumentation/langchain/index.js +91 -0
- package/dist/instrumentation/langchain/index.js.map +1 -0
- package/dist/instrumentation/langchain/wrapper.d.ts +41 -0
- package/dist/instrumentation/langchain/wrapper.js +1235 -0
- package/dist/instrumentation/langchain/wrapper.js.map +1 -0
- package/dist/instrumentation/langgraph/index.d.ts +12 -0
- package/dist/instrumentation/langgraph/index.js +99 -0
- package/dist/instrumentation/langgraph/index.js.map +1 -0
- package/dist/instrumentation/langgraph/wrapper.d.ts +20 -0
- package/dist/instrumentation/langgraph/wrapper.js +620 -0
- package/dist/instrumentation/langgraph/wrapper.js.map +1 -0
- package/dist/instrumentation/letta/index.d.ts +10 -0
- package/dist/instrumentation/letta/index.js +122 -0
- package/dist/instrumentation/letta/index.js.map +1 -0
- package/dist/instrumentation/letta/utils.d.ts +24 -0
- package/dist/instrumentation/letta/utils.js +343 -0
- package/dist/instrumentation/letta/utils.js.map +1 -0
- package/dist/instrumentation/letta/wrapper.d.ts +37 -0
- package/dist/instrumentation/letta/wrapper.js +180 -0
- package/dist/instrumentation/letta/wrapper.js.map +1 -0
- package/dist/instrumentation/llamaindex/index.d.ts +36 -0
- package/dist/instrumentation/llamaindex/index.js +221 -0
- package/dist/instrumentation/llamaindex/index.js.map +1 -0
- package/dist/instrumentation/llamaindex/wrapper.d.ts +36 -0
- package/dist/instrumentation/llamaindex/wrapper.js +811 -0
- package/dist/instrumentation/llamaindex/wrapper.js.map +1 -0
- package/dist/instrumentation/mcp/index.d.ts +20 -0
- package/dist/instrumentation/mcp/index.js +282 -0
- package/dist/instrumentation/mcp/index.js.map +1 -0
- package/dist/instrumentation/mcp/wrapper.d.ts +72 -0
- package/dist/instrumentation/mcp/wrapper.js +571 -0
- package/dist/instrumentation/mcp/wrapper.js.map +1 -0
- package/dist/instrumentation/mem0/index.d.ts +17 -0
- package/dist/instrumentation/mem0/index.js +113 -0
- package/dist/instrumentation/mem0/index.js.map +1 -0
- package/dist/instrumentation/mem0/wrapper.d.ts +37 -0
- package/dist/instrumentation/mem0/wrapper.js +298 -0
- package/dist/instrumentation/mem0/wrapper.js.map +1 -0
- package/dist/instrumentation/milvus/index.d.ts +11 -0
- package/dist/instrumentation/milvus/index.js +67 -0
- package/dist/instrumentation/milvus/index.js.map +1 -0
- package/dist/instrumentation/milvus/wrapper.d.ts +15 -0
- package/dist/instrumentation/milvus/wrapper.js +219 -0
- package/dist/instrumentation/milvus/wrapper.js.map +1 -0
- package/dist/instrumentation/mistral/index.d.ts +11 -0
- package/dist/instrumentation/mistral/index.js +66 -0
- package/dist/instrumentation/mistral/index.js.map +1 -0
- package/dist/instrumentation/mistral/wrapper.d.ts +49 -0
- package/dist/instrumentation/mistral/wrapper.js +482 -0
- package/dist/instrumentation/mistral/wrapper.js.map +1 -0
- package/dist/instrumentation/ollama/index.d.ts +11 -0
- package/dist/instrumentation/ollama/index.js +72 -0
- package/dist/instrumentation/ollama/index.js.map +1 -0
- package/dist/instrumentation/ollama/wrapper.d.ts +73 -0
- package/dist/instrumentation/ollama/wrapper.js +629 -0
- package/dist/instrumentation/ollama/wrapper.js.map +1 -0
- package/dist/instrumentation/openai/index.d.ts +11 -0
- package/dist/instrumentation/openai/index.js +79 -0
- package/dist/instrumentation/openai/index.js.map +1 -0
- package/dist/instrumentation/openai/wrapper.d.ts +80 -0
- package/dist/instrumentation/openai/wrapper.js +1121 -0
- package/dist/instrumentation/openai/wrapper.js.map +1 -0
- package/dist/instrumentation/openai-agents/index.d.ts +20 -0
- package/dist/instrumentation/openai-agents/index.js +174 -0
- package/dist/instrumentation/openai-agents/index.js.map +1 -0
- package/dist/instrumentation/openai-agents/processor.d.ts +35 -0
- package/dist/instrumentation/openai-agents/processor.js +249 -0
- package/dist/instrumentation/openai-agents/processor.js.map +1 -0
- package/dist/instrumentation/openai-agents/utils.d.ts +20 -0
- package/dist/instrumentation/openai-agents/utils.js +624 -0
- package/dist/instrumentation/openai-agents/utils.js.map +1 -0
- package/dist/instrumentation/pg/index.d.ts +20 -0
- package/dist/instrumentation/pg/index.js +74 -0
- package/dist/instrumentation/pg/index.js.map +1 -0
- package/dist/instrumentation/pg/wrapper.d.ts +66 -0
- package/dist/instrumentation/pg/wrapper.js +441 -0
- package/dist/instrumentation/pg/wrapper.js.map +1 -0
- package/dist/instrumentation/pinecone/index.d.ts +11 -0
- package/dist/instrumentation/pinecone/index.js +79 -0
- package/dist/instrumentation/pinecone/index.js.map +1 -0
- package/dist/instrumentation/pinecone/wrapper.d.ts +14 -0
- package/dist/instrumentation/pinecone/wrapper.js +198 -0
- package/dist/instrumentation/pinecone/wrapper.js.map +1 -0
- package/dist/instrumentation/qdrant/index.d.ts +11 -0
- package/dist/instrumentation/qdrant/index.js +67 -0
- package/dist/instrumentation/qdrant/index.js.map +1 -0
- package/dist/instrumentation/qdrant/wrapper.d.ts +14 -0
- package/dist/instrumentation/qdrant/wrapper.js +213 -0
- package/dist/instrumentation/qdrant/wrapper.js.map +1 -0
- package/dist/instrumentation/replicate/index.d.ts +11 -0
- package/dist/instrumentation/replicate/index.js +57 -0
- package/dist/instrumentation/replicate/index.js.map +1 -0
- package/dist/instrumentation/replicate/wrapper.d.ts +28 -0
- package/dist/instrumentation/replicate/wrapper.js +242 -0
- package/dist/instrumentation/replicate/wrapper.js.map +1 -0
- package/dist/instrumentation/strands/index.d.ts +21 -0
- package/dist/instrumentation/strands/index.js +83 -0
- package/dist/instrumentation/strands/index.js.map +1 -0
- package/dist/instrumentation/strands/processor.d.ts +51 -0
- package/dist/instrumentation/strands/processor.js +625 -0
- package/dist/instrumentation/strands/processor.js.map +1 -0
- package/dist/instrumentation/strands/utils.d.ts +24 -0
- package/dist/instrumentation/strands/utils.js +373 -0
- package/dist/instrumentation/strands/utils.js.map +1 -0
- package/dist/instrumentation/together/index.d.ts +11 -0
- package/dist/instrumentation/together/index.js +43 -0
- package/dist/instrumentation/together/index.js.map +1 -0
- package/dist/instrumentation/together/wrapper.d.ts +48 -0
- package/dist/instrumentation/together/wrapper.js +409 -0
- package/dist/instrumentation/together/wrapper.js.map +1 -0
- package/dist/instrumentation/transformers/index.d.ts +35 -0
- package/dist/instrumentation/transformers/index.js +119 -0
- package/dist/instrumentation/transformers/index.js.map +1 -0
- package/dist/instrumentation/transformers/wrapper.d.ts +49 -0
- package/dist/instrumentation/transformers/wrapper.js +472 -0
- package/dist/instrumentation/transformers/wrapper.js.map +1 -0
- package/dist/instrumentation/vercel-ai/index.d.ts +10 -0
- package/dist/instrumentation/vercel-ai/index.js +59 -0
- package/dist/instrumentation/vercel-ai/index.js.map +1 -0
- package/dist/instrumentation/vercel-ai/wrapper.d.ts +50 -0
- package/dist/instrumentation/vercel-ai/wrapper.js +539 -0
- package/dist/instrumentation/vercel-ai/wrapper.js.map +1 -0
- package/dist/instrumentation/vertexai/index.d.ts +11 -0
- package/dist/instrumentation/vertexai/index.js +70 -0
- package/dist/instrumentation/vertexai/index.js.map +1 -0
- package/dist/instrumentation/vertexai/wrapper.d.ts +67 -0
- package/dist/instrumentation/vertexai/wrapper.js +530 -0
- package/dist/instrumentation/vertexai/wrapper.js.map +1 -0
- package/dist/instrumentation/vllm/index.d.ts +13 -0
- package/dist/instrumentation/vllm/index.js +67 -0
- package/dist/instrumentation/vllm/index.js.map +1 -0
- package/dist/instrumentation/vllm/wrapper.d.ts +60 -0
- package/dist/instrumentation/vllm/wrapper.js +482 -0
- package/dist/instrumentation/vllm/wrapper.js.map +1 -0
- package/dist/llm/anthropic.d.ts +5 -0
- package/dist/llm/anthropic.js +39 -0
- package/dist/llm/anthropic.js.map +1 -0
- package/dist/llm/index.d.ts +10 -0
- package/dist/llm/index.js +93 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/openai.d.ts +6 -0
- package/dist/llm/openai.js +52 -0
- package/dist/llm/openai.js.map +1 -0
- package/dist/llm/providers.d.ts +7 -0
- package/dist/llm/providers.js +10 -0
- package/dist/llm/providers.js.map +1 -0
- package/dist/manual-trace.d.ts +40 -0
- package/dist/manual-trace.js +103 -0
- package/dist/manual-trace.js.map +1 -0
- package/dist/otel/__tests__/metrics.test.d.ts +1 -0
- package/dist/otel/__tests__/metrics.test.js +40 -0
- package/dist/otel/__tests__/metrics.test.js.map +1 -0
- package/dist/otel/events.d.ts +11 -0
- package/dist/otel/events.js +74 -0
- package/dist/otel/events.js.map +1 -0
- package/dist/otel/metrics.d.ts +35 -0
- package/dist/otel/metrics.js +219 -0
- package/dist/otel/metrics.js.map +1 -0
- package/dist/otel/tracing.d.ts +12 -0
- package/dist/otel/tracing.js +93 -0
- package/dist/otel/tracing.js.map +1 -0
- package/dist/otel/utils.d.ts +11 -0
- package/dist/otel/utils.js +34 -0
- package/dist/otel/utils.js.map +1 -0
- package/dist/pricing.json +951 -0
- package/dist/prompt-hub.d.ts +4 -0
- package/dist/prompt-hub.js +66 -0
- package/dist/prompt-hub.js.map +1 -0
- package/dist/register.d.ts +4 -0
- package/dist/register.js +52 -0
- package/dist/register.js.map +1 -0
- package/dist/score/__tests__/score.test.d.ts +1 -0
- package/dist/score/__tests__/score.test.js +143 -0
- package/dist/score/__tests__/score.test.js.map +1 -0
- package/dist/score/index.d.ts +2 -0
- package/dist/score/index.js +6 -0
- package/dist/score/index.js.map +1 -0
- package/dist/score/score.d.ts +15 -0
- package/dist/score/score.js +135 -0
- package/dist/score/score.js.map +1 -0
- package/dist/semantic-convention.d.ts +555 -0
- package/dist/semantic-convention.js +630 -0
- package/dist/semantic-convention.js.map +1 -0
- package/dist/types.d.ts +131 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +136 -0
|
@@ -0,0 +1,1121 @@
|
|
|
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
|
+
function spanCreationAttrs(operationName, requestModel) {
|
|
45
|
+
return {
|
|
46
|
+
[semantic_convention_1.default.GEN_AI_OPERATION]: operationName,
|
|
47
|
+
[semantic_convention_1.default.GEN_AI_PROVIDER_NAME_OTEL]: semantic_convention_1.default.GEN_AI_SYSTEM_OPENAI,
|
|
48
|
+
[semantic_convention_1.default.GEN_AI_REQUEST_MODEL]: requestModel,
|
|
49
|
+
[semantic_convention_1.default.SERVER_ADDRESS]: OpenAIWrapper.serverAddress,
|
|
50
|
+
[semantic_convention_1.default.SERVER_PORT]: OpenAIWrapper.serverPort,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
class OpenAIWrapper extends base_wrapper_1.default {
|
|
54
|
+
/**
|
|
55
|
+
* Stamp `shield360.agent.version_hash` (auto) and `gen_ai.agent.version`
|
|
56
|
+
* (user override, if set) on the span and return the same attributes so
|
|
57
|
+
* the caller can merge them into the inference event extras.
|
|
58
|
+
*/
|
|
59
|
+
static _stampAgentVersion(span, args) {
|
|
60
|
+
const out = {};
|
|
61
|
+
try {
|
|
62
|
+
const versionHash = helpers_1.default.computeAgentVersionHash({
|
|
63
|
+
systemInstructions: args.systemInstructionsJson ?? null,
|
|
64
|
+
toolDefinitions: args.toolDefinitionsJson ?? null,
|
|
65
|
+
primaryModel: args.primaryModel ?? null,
|
|
66
|
+
runtimeConfig: {
|
|
67
|
+
temperature: args.temperature ?? null,
|
|
68
|
+
top_p: args.top_p ?? null,
|
|
69
|
+
max_tokens: args.max_tokens ?? null,
|
|
70
|
+
provider: semantic_convention_1.default.GEN_AI_SYSTEM_OPENAI,
|
|
71
|
+
},
|
|
72
|
+
providers: [semantic_convention_1.default.GEN_AI_SYSTEM_OPENAI],
|
|
73
|
+
});
|
|
74
|
+
if (versionHash) {
|
|
75
|
+
out[semantic_convention_1.default.SHIELD360_AGENT_VERSION_HASH] = versionHash;
|
|
76
|
+
span.setAttribute(semantic_convention_1.default.SHIELD360_AGENT_VERSION_HASH, versionHash);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
// Hash computation must never fail the wrapped call.
|
|
81
|
+
}
|
|
82
|
+
const versionLabel = (0, helpers_1.getCurrentAgentVersion)();
|
|
83
|
+
if (versionLabel) {
|
|
84
|
+
out[semantic_convention_1.default.GEN_AI_AGENT_VERSION] = versionLabel;
|
|
85
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_AGENT_VERSION, versionLabel);
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
static _patchChatCompletionCreate(tracer) {
|
|
90
|
+
const genAIEndpoint = 'openai.resources.chat.completions';
|
|
91
|
+
return (originalMethod) => {
|
|
92
|
+
return async function (...args) {
|
|
93
|
+
if ((0, helpers_1.isFrameworkLlmActive)())
|
|
94
|
+
return originalMethod.apply(this, args);
|
|
95
|
+
const requestModel = args[0]?.model || 'gpt-4o';
|
|
96
|
+
const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CHAT} ${requestModel}`;
|
|
97
|
+
const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
|
|
98
|
+
const span = tracer.startSpan(spanName, {
|
|
99
|
+
kind: api_1.SpanKind.CLIENT,
|
|
100
|
+
attributes: spanCreationAttrs(semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CHAT, requestModel),
|
|
101
|
+
}, effectiveCtx);
|
|
102
|
+
return api_1.context
|
|
103
|
+
.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
|
|
104
|
+
return originalMethod.apply(this, args);
|
|
105
|
+
})
|
|
106
|
+
.then((response) => {
|
|
107
|
+
const { stream = false } = args[0];
|
|
108
|
+
if (stream) {
|
|
109
|
+
return helpers_1.default.createStreamProxy(response, OpenAIWrapper._chatCompletionGenerator({
|
|
110
|
+
args,
|
|
111
|
+
genAIEndpoint,
|
|
112
|
+
response,
|
|
113
|
+
span,
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
return OpenAIWrapper._chatCompletion({ args, genAIEndpoint, response, span });
|
|
117
|
+
})
|
|
118
|
+
.catch((e) => {
|
|
119
|
+
helpers_1.default.handleException(span, e);
|
|
120
|
+
base_wrapper_1.default.recordMetrics(span, {
|
|
121
|
+
genAIEndpoint,
|
|
122
|
+
model: requestModel,
|
|
123
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
124
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
125
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
126
|
+
errorType: e?.constructor?.name || '_OTHER',
|
|
127
|
+
});
|
|
128
|
+
span.end();
|
|
129
|
+
throw e;
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
static async _chatCompletion({ args, genAIEndpoint, response, span, }) {
|
|
135
|
+
let metricParams;
|
|
136
|
+
try {
|
|
137
|
+
metricParams = await OpenAIWrapper._chatCompletionCommonSetter({
|
|
138
|
+
args,
|
|
139
|
+
genAIEndpoint,
|
|
140
|
+
result: response,
|
|
141
|
+
span,
|
|
142
|
+
});
|
|
143
|
+
return response;
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
helpers_1.default.handleException(span, e);
|
|
147
|
+
throw e;
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
span.end();
|
|
151
|
+
if (metricParams) {
|
|
152
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
static async *_chatCompletionGenerator({ args, genAIEndpoint, response, span, }) {
|
|
157
|
+
let metricParams;
|
|
158
|
+
const timestamps = [];
|
|
159
|
+
const startTime = Date.now();
|
|
160
|
+
try {
|
|
161
|
+
const { messages } = args[0];
|
|
162
|
+
let { tools } = args[0];
|
|
163
|
+
const result = {
|
|
164
|
+
id: '0',
|
|
165
|
+
created: -1,
|
|
166
|
+
model: '',
|
|
167
|
+
system_fingerprint: '',
|
|
168
|
+
service_tier: 'auto',
|
|
169
|
+
choices: [
|
|
170
|
+
{
|
|
171
|
+
index: 0,
|
|
172
|
+
logprobs: null,
|
|
173
|
+
finish_reason: 'stop',
|
|
174
|
+
message: { role: 'assistant', content: '' },
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
usage: {
|
|
178
|
+
prompt_tokens: 0,
|
|
179
|
+
completion_tokens: 0,
|
|
180
|
+
total_tokens: 0,
|
|
181
|
+
completion_tokens_details: {
|
|
182
|
+
reasoning_tokens: 0,
|
|
183
|
+
audio_tokens: 0,
|
|
184
|
+
},
|
|
185
|
+
prompt_tokens_details: {
|
|
186
|
+
cached_tokens: 0,
|
|
187
|
+
cache_creation_tokens: 0,
|
|
188
|
+
cache_write_tokens: 0,
|
|
189
|
+
audio_tokens: 0,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
const toolCalls = [];
|
|
194
|
+
for await (const chunk of response) {
|
|
195
|
+
timestamps.push(Date.now());
|
|
196
|
+
result.id = chunk.id;
|
|
197
|
+
result.created = chunk.created;
|
|
198
|
+
result.model = chunk.model;
|
|
199
|
+
if (chunk.system_fingerprint) {
|
|
200
|
+
result.system_fingerprint = chunk.system_fingerprint;
|
|
201
|
+
}
|
|
202
|
+
if (chunk.service_tier) {
|
|
203
|
+
result.service_tier = chunk.service_tier;
|
|
204
|
+
}
|
|
205
|
+
if (chunk.choices[0]?.finish_reason) {
|
|
206
|
+
result.choices[0].finish_reason = chunk.choices[0].finish_reason;
|
|
207
|
+
}
|
|
208
|
+
if (chunk.choices[0]?.logprobs) {
|
|
209
|
+
result.choices[0].logprobs = chunk.choices[0].logprobs;
|
|
210
|
+
}
|
|
211
|
+
if (chunk.choices[0]?.delta.content) {
|
|
212
|
+
result.choices[0].message.content += chunk.choices[0].delta.content;
|
|
213
|
+
}
|
|
214
|
+
if (chunk.choices[0]?.delta.tool_calls) {
|
|
215
|
+
const deltaTools = chunk.choices[0].delta.tool_calls;
|
|
216
|
+
for (const tool of deltaTools) {
|
|
217
|
+
const idx = tool.index || 0;
|
|
218
|
+
while (toolCalls.length <= idx) {
|
|
219
|
+
toolCalls.push({
|
|
220
|
+
id: '',
|
|
221
|
+
type: 'function',
|
|
222
|
+
function: { name: '', arguments: '' }
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (tool.id) {
|
|
226
|
+
toolCalls[idx].id = tool.id;
|
|
227
|
+
toolCalls[idx].type = tool.type || 'function';
|
|
228
|
+
if (tool.function?.name) {
|
|
229
|
+
toolCalls[idx].function.name = tool.function.name;
|
|
230
|
+
}
|
|
231
|
+
if (tool.function?.arguments) {
|
|
232
|
+
toolCalls[idx].function.arguments = tool.function.arguments;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else if (tool.function?.arguments) {
|
|
236
|
+
toolCalls[idx].function.arguments += tool.function.arguments;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
tools = true;
|
|
240
|
+
}
|
|
241
|
+
if (chunk.usage) {
|
|
242
|
+
result.usage = {
|
|
243
|
+
...result.usage,
|
|
244
|
+
...chunk.usage,
|
|
245
|
+
prompt_tokens_details: {
|
|
246
|
+
...result.usage.prompt_tokens_details,
|
|
247
|
+
...(chunk.usage.prompt_tokens_details || {}),
|
|
248
|
+
},
|
|
249
|
+
completion_tokens_details: {
|
|
250
|
+
...result.usage.completion_tokens_details,
|
|
251
|
+
...(chunk.usage.completion_tokens_details || {}),
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
yield chunk;
|
|
256
|
+
}
|
|
257
|
+
if (toolCalls.length > 0) {
|
|
258
|
+
result.choices[0].message = {
|
|
259
|
+
...result.choices[0].message,
|
|
260
|
+
tool_calls: toolCalls
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
if (!result.usage.prompt_tokens && !result.usage.completion_tokens) {
|
|
264
|
+
let promptTokens = 0;
|
|
265
|
+
for (const message of messages || []) {
|
|
266
|
+
promptTokens += helpers_1.default.openaiTokens(message.content, result.model) ?? 0;
|
|
267
|
+
}
|
|
268
|
+
const completionTokens = helpers_1.default.openaiTokens(result.choices[0].message.content ?? '', result.model);
|
|
269
|
+
if (completionTokens) {
|
|
270
|
+
result.usage = {
|
|
271
|
+
prompt_tokens: promptTokens,
|
|
272
|
+
completion_tokens: completionTokens,
|
|
273
|
+
total_tokens: promptTokens + completionTokens,
|
|
274
|
+
completion_tokens_details: result.usage.completion_tokens_details,
|
|
275
|
+
prompt_tokens_details: result.usage.prompt_tokens_details,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
args[0].tools = tools;
|
|
280
|
+
const ttft = timestamps.length > 0 ? (timestamps[0] - startTime) / 1000 : 0;
|
|
281
|
+
let tbt = 0;
|
|
282
|
+
if (timestamps.length > 1) {
|
|
283
|
+
const timeDiffs = timestamps.slice(1).map((t, i) => t - timestamps[i]);
|
|
284
|
+
tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;
|
|
285
|
+
}
|
|
286
|
+
metricParams = await OpenAIWrapper._chatCompletionCommonSetter({
|
|
287
|
+
args,
|
|
288
|
+
genAIEndpoint,
|
|
289
|
+
result,
|
|
290
|
+
span,
|
|
291
|
+
ttft,
|
|
292
|
+
tbt,
|
|
293
|
+
});
|
|
294
|
+
return result;
|
|
295
|
+
}
|
|
296
|
+
catch (e) {
|
|
297
|
+
helpers_1.default.handleException(span, e);
|
|
298
|
+
throw e;
|
|
299
|
+
}
|
|
300
|
+
finally {
|
|
301
|
+
span.end();
|
|
302
|
+
if (metricParams) {
|
|
303
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
static async _chatCompletionCommonSetter({ args, genAIEndpoint, result, span, ttft = 0, tbt = 0, }) {
|
|
308
|
+
const captureContent = config_1.default.captureMessageContent;
|
|
309
|
+
const requestModel = args[0]?.model || 'gpt-4o';
|
|
310
|
+
const { messages, frequency_penalty = 0, max_tokens = null, n = 1, presence_penalty = 0, seed = null, stop = null, temperature = 1, top_p, user, stream = false, tools: _tools, service_tier, } = args[0];
|
|
311
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TOP_P, top_p || 1);
|
|
312
|
+
if (max_tokens != null) {
|
|
313
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_MAX_TOKENS, max_tokens);
|
|
314
|
+
}
|
|
315
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TEMPERATURE, temperature);
|
|
316
|
+
if (presence_penalty) {
|
|
317
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty);
|
|
318
|
+
}
|
|
319
|
+
if (frequency_penalty) {
|
|
320
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty);
|
|
321
|
+
}
|
|
322
|
+
if (seed != null) {
|
|
323
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_SEED, Number(seed));
|
|
324
|
+
}
|
|
325
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IS_STREAM, stream);
|
|
326
|
+
if (stop) {
|
|
327
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_STOP_SEQUENCES, Array.isArray(stop) ? stop : [stop]);
|
|
328
|
+
}
|
|
329
|
+
if (n && n !== 1) {
|
|
330
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_CHOICE_COUNT, n);
|
|
331
|
+
}
|
|
332
|
+
if (service_tier && service_tier !== 'auto') {
|
|
333
|
+
span.setAttribute(semantic_convention_1.default.OPENAI_REQUEST_SERVICE_TIER, service_tier);
|
|
334
|
+
}
|
|
335
|
+
if (captureContent) {
|
|
336
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_INPUT_MESSAGES, helpers_1.default.buildInputMessages(messages || []));
|
|
337
|
+
}
|
|
338
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_ID, result.id);
|
|
339
|
+
const responseModel = result.model || requestModel;
|
|
340
|
+
const pricingInfo = config_1.default.pricingInfo || {};
|
|
341
|
+
// OpenAI reports prompt_tokens inclusive of cached (cache read) tokens, so
|
|
342
|
+
// flag the prompt tokens as cache-inclusive to avoid billing cached tokens
|
|
343
|
+
// twice once a model defines cacheReadPrice. GPT-5.6+ also reports
|
|
344
|
+
// cache_write_tokens (creation) under prompt_tokens_details.
|
|
345
|
+
const promptDetails = result.usage.prompt_tokens_details || {};
|
|
346
|
+
const cacheReadTokens = Number(promptDetails.cached_tokens) || 0;
|
|
347
|
+
const cacheCreationTokens = Number(promptDetails.cache_creation_tokens) ||
|
|
348
|
+
Number(promptDetails.cache_write_tokens) ||
|
|
349
|
+
0;
|
|
350
|
+
const cost = helpers_1.default.getChatModelCost(requestModel, pricingInfo, result.usage.prompt_tokens, result.usage.completion_tokens, cacheReadTokens, cacheCreationTokens, true);
|
|
351
|
+
OpenAIWrapper.setBaseSpanAttributes(span, {
|
|
352
|
+
genAIEndpoint,
|
|
353
|
+
model: requestModel,
|
|
354
|
+
user,
|
|
355
|
+
cost,
|
|
356
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
357
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
358
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
359
|
+
});
|
|
360
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_MODEL, responseModel);
|
|
361
|
+
span.setAttribute(semantic_convention_1.default.OPENAI_API_TYPE, 'chat_completions');
|
|
362
|
+
if (result.system_fingerprint) {
|
|
363
|
+
span.setAttribute(semantic_convention_1.default.OPENAI_RESPONSE_SYSTEM_FINGERPRINT, result.system_fingerprint);
|
|
364
|
+
}
|
|
365
|
+
if (result.service_tier) {
|
|
366
|
+
span.setAttribute(semantic_convention_1.default.OPENAI_RESPONSE_SERVICE_TIER, result.service_tier);
|
|
367
|
+
}
|
|
368
|
+
const inputTokens = result.usage.prompt_tokens;
|
|
369
|
+
const outputTokens = result.usage.completion_tokens;
|
|
370
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);
|
|
371
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);
|
|
372
|
+
if (cacheReadTokens) {
|
|
373
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cacheReadTokens);
|
|
374
|
+
}
|
|
375
|
+
if (cacheCreationTokens) {
|
|
376
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cacheCreationTokens);
|
|
377
|
+
}
|
|
378
|
+
if (ttft > 0) {
|
|
379
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_SERVER_TTFT, ttft);
|
|
380
|
+
}
|
|
381
|
+
if (tbt > 0) {
|
|
382
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_SERVER_TBT, tbt);
|
|
383
|
+
}
|
|
384
|
+
if (result.choices[0].finish_reason) {
|
|
385
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON, [result.choices[0].finish_reason]);
|
|
386
|
+
}
|
|
387
|
+
const outputType = typeof result.choices[0].message.content === 'string'
|
|
388
|
+
? semantic_convention_1.default.GEN_AI_OUTPUT_TYPE_TEXT
|
|
389
|
+
: semantic_convention_1.default.GEN_AI_OUTPUT_TYPE_JSON;
|
|
390
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_OUTPUT_TYPE, outputType);
|
|
391
|
+
if (result.choices[0].message.tool_calls) {
|
|
392
|
+
const toolCalls = result.choices[0].message.tool_calls;
|
|
393
|
+
const toolNames = toolCalls.map((t) => t.function?.name || '').filter(Boolean);
|
|
394
|
+
const toolIds = toolCalls.map((t) => t.id || '').filter(Boolean);
|
|
395
|
+
const toolArgs = toolCalls.map((t) => t.function?.arguments || '').filter(Boolean);
|
|
396
|
+
if (toolNames.length > 0) {
|
|
397
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_NAME, toolNames.join(', '));
|
|
398
|
+
}
|
|
399
|
+
if (toolIds.length > 0) {
|
|
400
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_CALL_ID, toolIds.join(', '));
|
|
401
|
+
}
|
|
402
|
+
if (toolArgs.length > 0) {
|
|
403
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_ARGS, toolArgs.join(', '));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
let inputMessagesJson;
|
|
407
|
+
let outputMessagesJson;
|
|
408
|
+
const toolDefinitionsJson = helpers_1.default.buildToolDefinitions(_tools);
|
|
409
|
+
// Compute system_instructions and version hash regardless of
|
|
410
|
+
// captureContent so versions still group correctly when content
|
|
411
|
+
// capture is disabled.
|
|
412
|
+
const systemInstructionsJson = helpers_1.default.buildSystemInstructionsFromMessages(messages || []);
|
|
413
|
+
const versionExtras = OpenAIWrapper._stampAgentVersion(span, {
|
|
414
|
+
systemInstructionsJson,
|
|
415
|
+
toolDefinitionsJson,
|
|
416
|
+
primaryModel: responseModel || requestModel,
|
|
417
|
+
temperature,
|
|
418
|
+
top_p,
|
|
419
|
+
max_tokens,
|
|
420
|
+
});
|
|
421
|
+
if (captureContent) {
|
|
422
|
+
const toolCalls = result.choices[0].message.tool_calls;
|
|
423
|
+
outputMessagesJson = helpers_1.default.buildOutputMessages(result.choices[0].message.content || '', result.choices[0].finish_reason || 'stop', toolCalls);
|
|
424
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_OUTPUT_MESSAGES, outputMessagesJson);
|
|
425
|
+
inputMessagesJson = helpers_1.default.buildInputMessages(messages || []);
|
|
426
|
+
if (systemInstructionsJson) {
|
|
427
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_SYSTEM_INSTRUCTIONS, systemInstructionsJson);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
if (toolDefinitionsJson) {
|
|
431
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_DEFINITIONS, toolDefinitionsJson);
|
|
432
|
+
}
|
|
433
|
+
if (!config_1.default.disableEvents) {
|
|
434
|
+
const eventAttrs = {
|
|
435
|
+
[semantic_convention_1.default.GEN_AI_OPERATION]: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CHAT,
|
|
436
|
+
[semantic_convention_1.default.GEN_AI_REQUEST_MODEL]: requestModel,
|
|
437
|
+
[semantic_convention_1.default.GEN_AI_RESPONSE_MODEL]: responseModel,
|
|
438
|
+
[semantic_convention_1.default.SERVER_ADDRESS]: OpenAIWrapper.serverAddress,
|
|
439
|
+
[semantic_convention_1.default.SERVER_PORT]: OpenAIWrapper.serverPort,
|
|
440
|
+
[semantic_convention_1.default.GEN_AI_RESPONSE_ID]: result.id,
|
|
441
|
+
[semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON]: [result.choices[0].finish_reason],
|
|
442
|
+
[semantic_convention_1.default.GEN_AI_OUTPUT_TYPE]: outputType,
|
|
443
|
+
[semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS]: inputTokens,
|
|
444
|
+
[semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS]: outputTokens,
|
|
445
|
+
...versionExtras,
|
|
446
|
+
};
|
|
447
|
+
if (captureContent) {
|
|
448
|
+
if (inputMessagesJson)
|
|
449
|
+
eventAttrs[semantic_convention_1.default.GEN_AI_INPUT_MESSAGES] = inputMessagesJson;
|
|
450
|
+
if (outputMessagesJson)
|
|
451
|
+
eventAttrs[semantic_convention_1.default.GEN_AI_OUTPUT_MESSAGES] = outputMessagesJson;
|
|
452
|
+
if (systemInstructionsJson)
|
|
453
|
+
eventAttrs[semantic_convention_1.default.GEN_AI_SYSTEM_INSTRUCTIONS] = systemInstructionsJson;
|
|
454
|
+
}
|
|
455
|
+
if (toolDefinitionsJson)
|
|
456
|
+
eventAttrs[semantic_convention_1.default.GEN_AI_TOOL_DEFINITIONS] = toolDefinitionsJson;
|
|
457
|
+
helpers_1.default.emitInferenceEvent(span, eventAttrs);
|
|
458
|
+
}
|
|
459
|
+
return {
|
|
460
|
+
genAIEndpoint,
|
|
461
|
+
model: requestModel,
|
|
462
|
+
user,
|
|
463
|
+
cost,
|
|
464
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
static _patchEmbedding(tracer) {
|
|
468
|
+
const genAIEndpoint = 'openai.resources.embeddings';
|
|
469
|
+
return (originalMethod) => {
|
|
470
|
+
return async function (...args) {
|
|
471
|
+
if ((0, helpers_1.isFrameworkLlmActive)())
|
|
472
|
+
return originalMethod.apply(this, args);
|
|
473
|
+
const requestModel = args[0]?.model || 'text-embedding-ada-002';
|
|
474
|
+
const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_EMBEDDING} ${requestModel}`;
|
|
475
|
+
const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
|
|
476
|
+
const span = tracer.startSpan(spanName, {
|
|
477
|
+
kind: api_1.SpanKind.CLIENT,
|
|
478
|
+
attributes: spanCreationAttrs(semantic_convention_1.default.GEN_AI_OPERATION_TYPE_EMBEDDING, requestModel),
|
|
479
|
+
}, effectiveCtx);
|
|
480
|
+
return api_1.context.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
|
|
481
|
+
const captureContent = config_1.default.captureMessageContent;
|
|
482
|
+
let metricParams;
|
|
483
|
+
try {
|
|
484
|
+
const response = await originalMethod.apply(this, args);
|
|
485
|
+
const _responseModel = response.model || requestModel;
|
|
486
|
+
const pricingInfo = config_1.default.pricingInfo || {};
|
|
487
|
+
const cost = helpers_1.default.getEmbedModelCost(requestModel, pricingInfo, response.usage.prompt_tokens);
|
|
488
|
+
const { dimensions, encoding_format = 'float', input, user } = args[0];
|
|
489
|
+
OpenAIWrapper.setBaseSpanAttributes(span, {
|
|
490
|
+
genAIEndpoint,
|
|
491
|
+
model: requestModel,
|
|
492
|
+
user,
|
|
493
|
+
cost,
|
|
494
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
495
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
496
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
497
|
+
});
|
|
498
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IS_STREAM, false);
|
|
499
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_ENCODING_FORMATS, [encoding_format]);
|
|
500
|
+
if (dimensions) {
|
|
501
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_EMBEDDINGS_DIMENSION_COUNT, dimensions);
|
|
502
|
+
}
|
|
503
|
+
if (captureContent) {
|
|
504
|
+
const formattedInput = typeof input === 'string' ? input : JSON.stringify(input);
|
|
505
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_INPUT_MESSAGES, formattedInput);
|
|
506
|
+
}
|
|
507
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS, response.usage.prompt_tokens);
|
|
508
|
+
metricParams = {
|
|
509
|
+
genAIEndpoint,
|
|
510
|
+
model: requestModel,
|
|
511
|
+
user,
|
|
512
|
+
cost,
|
|
513
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
514
|
+
};
|
|
515
|
+
return response;
|
|
516
|
+
}
|
|
517
|
+
catch (e) {
|
|
518
|
+
helpers_1.default.handleException(span, e);
|
|
519
|
+
throw e;
|
|
520
|
+
}
|
|
521
|
+
finally {
|
|
522
|
+
span.end();
|
|
523
|
+
if (metricParams) {
|
|
524
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
};
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
static _patchFineTune(tracer) {
|
|
532
|
+
const genAIEndpoint = 'openai.resources.fine_tuning.jobs';
|
|
533
|
+
return (originalMethod) => {
|
|
534
|
+
return async function (...args) {
|
|
535
|
+
if ((0, helpers_1.isFrameworkLlmActive)())
|
|
536
|
+
return originalMethod.apply(this, args);
|
|
537
|
+
const requestModel = args[0]?.model || 'gpt-3.5-turbo';
|
|
538
|
+
const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_FINETUNING} ${requestModel}`;
|
|
539
|
+
const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
|
|
540
|
+
const span = tracer.startSpan(spanName, {
|
|
541
|
+
kind: api_1.SpanKind.CLIENT,
|
|
542
|
+
attributes: spanCreationAttrs(semantic_convention_1.default.GEN_AI_OPERATION_TYPE_FINETUNING, requestModel),
|
|
543
|
+
}, effectiveCtx);
|
|
544
|
+
return api_1.context.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
|
|
545
|
+
let metricParams;
|
|
546
|
+
try {
|
|
547
|
+
const response = await originalMethod.apply(this, args);
|
|
548
|
+
const { hyperparameters = {}, suffix = '', training_file, user, validation_file, } = args[0];
|
|
549
|
+
OpenAIWrapper.setBaseSpanAttributes(span, {
|
|
550
|
+
genAIEndpoint,
|
|
551
|
+
model: requestModel,
|
|
552
|
+
user,
|
|
553
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
554
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
555
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
556
|
+
});
|
|
557
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TRAINING_FILE, training_file);
|
|
558
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_VALIDATION_FILE, validation_file);
|
|
559
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_FINETUNE_BATCH_SIZE, hyperparameters?.batch_size || 'auto');
|
|
560
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_FINETUNE_MODEL_LRM, hyperparameters?.learning_rate_multiplier || 'auto');
|
|
561
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_FINETUNE_MODEL_EPOCHS, hyperparameters?.n_epochs || 'auto');
|
|
562
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_FINETUNE_MODEL_SUFFIX, suffix);
|
|
563
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_ID, response.id);
|
|
564
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS, response.usage.prompt_tokens);
|
|
565
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_FINETUNE_STATUS, response.status);
|
|
566
|
+
metricParams = {
|
|
567
|
+
genAIEndpoint,
|
|
568
|
+
model: requestModel,
|
|
569
|
+
user,
|
|
570
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
571
|
+
};
|
|
572
|
+
return response;
|
|
573
|
+
}
|
|
574
|
+
catch (e) {
|
|
575
|
+
helpers_1.default.handleException(span, e);
|
|
576
|
+
throw e;
|
|
577
|
+
}
|
|
578
|
+
finally {
|
|
579
|
+
span.end();
|
|
580
|
+
if (metricParams) {
|
|
581
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
};
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
static _patchImageGenerate(tracer) {
|
|
589
|
+
const genAIEndpoint = 'openai.resources.images';
|
|
590
|
+
return (originalMethod) => {
|
|
591
|
+
return async function (...args) {
|
|
592
|
+
if ((0, helpers_1.isFrameworkLlmActive)())
|
|
593
|
+
return originalMethod.apply(this, args);
|
|
594
|
+
const requestModel = args[0]?.model || 'dall-e-2';
|
|
595
|
+
const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_IMAGE} ${requestModel}`;
|
|
596
|
+
const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
|
|
597
|
+
const span = tracer.startSpan(spanName, {
|
|
598
|
+
kind: api_1.SpanKind.CLIENT,
|
|
599
|
+
attributes: spanCreationAttrs(semantic_convention_1.default.GEN_AI_OPERATION_TYPE_IMAGE, requestModel),
|
|
600
|
+
}, effectiveCtx);
|
|
601
|
+
return api_1.context.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
|
|
602
|
+
const captureContent = config_1.default.captureMessageContent;
|
|
603
|
+
let metricParams;
|
|
604
|
+
try {
|
|
605
|
+
const response = await originalMethod.apply(this, args);
|
|
606
|
+
const { prompt, quality = 'standard', response_format = 'url', size = '1024x1024', style = 'vivid', user, } = args[0];
|
|
607
|
+
const responseModel = response.model || requestModel;
|
|
608
|
+
const pricingInfo = config_1.default.pricingInfo || {};
|
|
609
|
+
const cost = (response.data?.length || 1) *
|
|
610
|
+
helpers_1.default.getImageModelCost(responseModel, pricingInfo, size, quality);
|
|
611
|
+
OpenAIWrapper.setBaseSpanAttributes(span, {
|
|
612
|
+
genAIEndpoint,
|
|
613
|
+
model: requestModel,
|
|
614
|
+
user,
|
|
615
|
+
cost,
|
|
616
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
617
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
618
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
619
|
+
});
|
|
620
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IMAGE_SIZE, size);
|
|
621
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IMAGE_QUALITY, quality);
|
|
622
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IMAGE_STYLE, style);
|
|
623
|
+
if (captureContent) {
|
|
624
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_INPUT_MESSAGES, prompt);
|
|
625
|
+
}
|
|
626
|
+
if (response.data) {
|
|
627
|
+
const imageUrls = [];
|
|
628
|
+
const revisedPrompts = [];
|
|
629
|
+
for (const items of response.data) {
|
|
630
|
+
revisedPrompts.push(items.revised_prompt || '');
|
|
631
|
+
const value = items[response_format];
|
|
632
|
+
imageUrls.push(value && !String(value).startsWith('data:') ? value : '[base64_image_data]');
|
|
633
|
+
}
|
|
634
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_IMAGE, imageUrls);
|
|
635
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_CONTENT_REVISED_PROMPT, revisedPrompts);
|
|
636
|
+
}
|
|
637
|
+
metricParams = {
|
|
638
|
+
genAIEndpoint,
|
|
639
|
+
model: requestModel,
|
|
640
|
+
user,
|
|
641
|
+
cost,
|
|
642
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
643
|
+
};
|
|
644
|
+
return response;
|
|
645
|
+
}
|
|
646
|
+
catch (e) {
|
|
647
|
+
helpers_1.default.handleException(span, e);
|
|
648
|
+
throw e;
|
|
649
|
+
}
|
|
650
|
+
finally {
|
|
651
|
+
span.end();
|
|
652
|
+
if (metricParams) {
|
|
653
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
});
|
|
657
|
+
};
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
static _patchImageVariation(tracer) {
|
|
661
|
+
const genAIEndpoint = 'openai.resources.images';
|
|
662
|
+
return (originalMethod) => {
|
|
663
|
+
return async function (...args) {
|
|
664
|
+
if ((0, helpers_1.isFrameworkLlmActive)())
|
|
665
|
+
return originalMethod.apply(this, args);
|
|
666
|
+
const requestModel = args[0]?.model || 'dall-e-2';
|
|
667
|
+
const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_IMAGE} ${requestModel}`;
|
|
668
|
+
const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
|
|
669
|
+
const span = tracer.startSpan(spanName, {
|
|
670
|
+
kind: api_1.SpanKind.CLIENT,
|
|
671
|
+
attributes: spanCreationAttrs(semantic_convention_1.default.GEN_AI_OPERATION_TYPE_IMAGE, requestModel),
|
|
672
|
+
}, effectiveCtx);
|
|
673
|
+
return api_1.context.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
|
|
674
|
+
const captureContent = config_1.default.captureMessageContent;
|
|
675
|
+
let metricParams;
|
|
676
|
+
try {
|
|
677
|
+
const response = await originalMethod.apply(this, args);
|
|
678
|
+
const { prompt, quality = 'standard', response_format = 'url', size = '1024x1024', style = 'vivid', user, } = args[0];
|
|
679
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_ID, response.created);
|
|
680
|
+
const responseModel = response.model || requestModel;
|
|
681
|
+
const pricingInfo = config_1.default.pricingInfo || {};
|
|
682
|
+
const cost = (response.data?.length || 1) *
|
|
683
|
+
helpers_1.default.getImageModelCost(responseModel, pricingInfo, size, quality);
|
|
684
|
+
OpenAIWrapper.setBaseSpanAttributes(span, {
|
|
685
|
+
genAIEndpoint,
|
|
686
|
+
model: requestModel,
|
|
687
|
+
user,
|
|
688
|
+
cost,
|
|
689
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
690
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
691
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
692
|
+
});
|
|
693
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IMAGE_SIZE, size);
|
|
694
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IMAGE_QUALITY, quality);
|
|
695
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IMAGE_STYLE, style);
|
|
696
|
+
if (captureContent) {
|
|
697
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_INPUT_MESSAGES, prompt);
|
|
698
|
+
}
|
|
699
|
+
if (response.data) {
|
|
700
|
+
const imageUrls = [];
|
|
701
|
+
const revisedPrompts = [];
|
|
702
|
+
for (const items of response.data) {
|
|
703
|
+
revisedPrompts.push(items.revised_prompt || '');
|
|
704
|
+
const value = items[response_format];
|
|
705
|
+
imageUrls.push(value && !String(value).startsWith('data:') ? value : '[base64_image_data]');
|
|
706
|
+
}
|
|
707
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_IMAGE, imageUrls);
|
|
708
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_CONTENT_REVISED_PROMPT, revisedPrompts);
|
|
709
|
+
}
|
|
710
|
+
metricParams = {
|
|
711
|
+
genAIEndpoint,
|
|
712
|
+
model: requestModel,
|
|
713
|
+
user,
|
|
714
|
+
cost,
|
|
715
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
716
|
+
};
|
|
717
|
+
return response;
|
|
718
|
+
}
|
|
719
|
+
catch (e) {
|
|
720
|
+
helpers_1.default.handleException(span, e);
|
|
721
|
+
throw e;
|
|
722
|
+
}
|
|
723
|
+
finally {
|
|
724
|
+
span.end();
|
|
725
|
+
if (metricParams) {
|
|
726
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
});
|
|
730
|
+
};
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
static _patchAudioCreate(tracer) {
|
|
734
|
+
const genAIEndpoint = 'openai.resources.audio.speech';
|
|
735
|
+
return (originalMethod) => {
|
|
736
|
+
return async function (...args) {
|
|
737
|
+
if ((0, helpers_1.isFrameworkLlmActive)())
|
|
738
|
+
return originalMethod.apply(this, args);
|
|
739
|
+
const requestModel = args[0]?.model || 'tts-1';
|
|
740
|
+
const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_AUDIO} ${requestModel}`;
|
|
741
|
+
const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
|
|
742
|
+
const span = tracer.startSpan(spanName, {
|
|
743
|
+
kind: api_1.SpanKind.CLIENT,
|
|
744
|
+
attributes: spanCreationAttrs(semantic_convention_1.default.GEN_AI_OPERATION_TYPE_AUDIO, requestModel),
|
|
745
|
+
}, effectiveCtx);
|
|
746
|
+
return api_1.context.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
|
|
747
|
+
const captureContent = config_1.default.captureMessageContent;
|
|
748
|
+
let metricParams;
|
|
749
|
+
try {
|
|
750
|
+
const response = await originalMethod.apply(this, args);
|
|
751
|
+
const { input, user, voice, response_format = 'mp3', speed = 1 } = args[0];
|
|
752
|
+
const responseModel = response.model || requestModel;
|
|
753
|
+
const pricingInfo = config_1.default.pricingInfo || {};
|
|
754
|
+
const cost = helpers_1.default.getAudioModelCost(responseModel, pricingInfo, input);
|
|
755
|
+
OpenAIWrapper.setBaseSpanAttributes(span, {
|
|
756
|
+
genAIEndpoint,
|
|
757
|
+
model: requestModel,
|
|
758
|
+
user,
|
|
759
|
+
cost,
|
|
760
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
761
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
762
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
763
|
+
});
|
|
764
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_AUDIO_VOICE, voice);
|
|
765
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT, response_format);
|
|
766
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_AUDIO_SPEED, speed);
|
|
767
|
+
if (captureContent) {
|
|
768
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_INPUT_MESSAGES, input);
|
|
769
|
+
}
|
|
770
|
+
metricParams = {
|
|
771
|
+
genAIEndpoint,
|
|
772
|
+
model: requestModel,
|
|
773
|
+
user,
|
|
774
|
+
cost,
|
|
775
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
776
|
+
};
|
|
777
|
+
return response;
|
|
778
|
+
}
|
|
779
|
+
catch (e) {
|
|
780
|
+
helpers_1.default.handleException(span, e);
|
|
781
|
+
throw e;
|
|
782
|
+
}
|
|
783
|
+
finally {
|
|
784
|
+
span.end();
|
|
785
|
+
if (metricParams) {
|
|
786
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
static _patchResponsesCreate(tracer) {
|
|
794
|
+
const genAIEndpoint = 'openai.resources.responses';
|
|
795
|
+
return (originalMethod) => {
|
|
796
|
+
return async function (...args) {
|
|
797
|
+
if ((0, helpers_1.isFrameworkLlmActive)())
|
|
798
|
+
return originalMethod.apply(this, args);
|
|
799
|
+
const requestModel = args[0]?.model || 'gpt-4o';
|
|
800
|
+
const spanName = `${semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CHAT} ${requestModel}`;
|
|
801
|
+
const effectiveCtx = (0, helpers_1.getFrameworkParentContext)() ?? api_1.context.active();
|
|
802
|
+
const span = tracer.startSpan(spanName, {
|
|
803
|
+
kind: api_1.SpanKind.CLIENT,
|
|
804
|
+
attributes: spanCreationAttrs(semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CHAT, requestModel),
|
|
805
|
+
}, effectiveCtx);
|
|
806
|
+
return api_1.context
|
|
807
|
+
.with(api_1.trace.setSpan(effectiveCtx, span), async () => {
|
|
808
|
+
return originalMethod.apply(this, args);
|
|
809
|
+
})
|
|
810
|
+
.then((response) => {
|
|
811
|
+
const { stream = false } = args[0];
|
|
812
|
+
if (stream) {
|
|
813
|
+
return helpers_1.default.createStreamProxy(response, OpenAIWrapper._responsesGenerator({
|
|
814
|
+
args,
|
|
815
|
+
genAIEndpoint,
|
|
816
|
+
response,
|
|
817
|
+
span,
|
|
818
|
+
}));
|
|
819
|
+
}
|
|
820
|
+
return OpenAIWrapper._responsesComplete({ args, genAIEndpoint, response, span });
|
|
821
|
+
})
|
|
822
|
+
.catch((e) => {
|
|
823
|
+
helpers_1.default.handleException(span, e);
|
|
824
|
+
base_wrapper_1.default.recordMetrics(span, {
|
|
825
|
+
genAIEndpoint,
|
|
826
|
+
model: requestModel,
|
|
827
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
828
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
829
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
830
|
+
errorType: e?.constructor?.name || '_OTHER',
|
|
831
|
+
});
|
|
832
|
+
span.end();
|
|
833
|
+
throw e;
|
|
834
|
+
});
|
|
835
|
+
};
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
static async _responsesComplete({ args, genAIEndpoint, response, span, }) {
|
|
839
|
+
let metricParams;
|
|
840
|
+
try {
|
|
841
|
+
metricParams = await OpenAIWrapper._responsesCommonSetter({
|
|
842
|
+
args,
|
|
843
|
+
genAIEndpoint,
|
|
844
|
+
result: response,
|
|
845
|
+
span,
|
|
846
|
+
});
|
|
847
|
+
return response;
|
|
848
|
+
}
|
|
849
|
+
catch (e) {
|
|
850
|
+
helpers_1.default.handleException(span, e);
|
|
851
|
+
throw e;
|
|
852
|
+
}
|
|
853
|
+
finally {
|
|
854
|
+
span.end();
|
|
855
|
+
if (metricParams) {
|
|
856
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
static async *_responsesGenerator({ args, genAIEndpoint, response, span, }) {
|
|
861
|
+
let metricParams;
|
|
862
|
+
const timestamps = [];
|
|
863
|
+
const startTime = Date.now();
|
|
864
|
+
try {
|
|
865
|
+
const result = {
|
|
866
|
+
id: '',
|
|
867
|
+
model: '',
|
|
868
|
+
service_tier: 'default',
|
|
869
|
+
status: 'completed',
|
|
870
|
+
output: [],
|
|
871
|
+
usage: {
|
|
872
|
+
input_tokens: 0,
|
|
873
|
+
output_tokens: 0,
|
|
874
|
+
input_tokens_details: {
|
|
875
|
+
cached_tokens: 0,
|
|
876
|
+
cache_creation_tokens: 0,
|
|
877
|
+
cache_write_tokens: 0,
|
|
878
|
+
},
|
|
879
|
+
output_tokens_details: {
|
|
880
|
+
reasoning_tokens: 0,
|
|
881
|
+
},
|
|
882
|
+
},
|
|
883
|
+
};
|
|
884
|
+
let llmResponse = '';
|
|
885
|
+
const responseTools = [];
|
|
886
|
+
for await (const chunk of response) {
|
|
887
|
+
timestamps.push(Date.now());
|
|
888
|
+
if (chunk.type === 'response.output_text.delta') {
|
|
889
|
+
llmResponse += chunk.delta || '';
|
|
890
|
+
}
|
|
891
|
+
else if (chunk.type === 'response.output_item.added') {
|
|
892
|
+
const item = chunk.item;
|
|
893
|
+
if (item?.type === 'function_call') {
|
|
894
|
+
responseTools.push({
|
|
895
|
+
id: item.id,
|
|
896
|
+
call_id: item.call_id,
|
|
897
|
+
name: item.name,
|
|
898
|
+
type: item.type,
|
|
899
|
+
arguments: item.arguments || '',
|
|
900
|
+
status: item.status,
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
else if (chunk.type === 'response.function_call_arguments.delta') {
|
|
905
|
+
const itemId = chunk.item_id;
|
|
906
|
+
const delta = chunk.delta || '';
|
|
907
|
+
const tool = responseTools.find(t => t.id === itemId);
|
|
908
|
+
if (tool) {
|
|
909
|
+
tool.arguments += delta;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
else if (chunk.type === 'response.completed') {
|
|
913
|
+
const responseData = chunk.response;
|
|
914
|
+
result.id = responseData.id;
|
|
915
|
+
result.model = responseData.model;
|
|
916
|
+
result.status = responseData.status;
|
|
917
|
+
const usage = responseData.usage || {};
|
|
918
|
+
result.usage.input_tokens = usage.input_tokens || 0;
|
|
919
|
+
result.usage.output_tokens = usage.output_tokens || 0;
|
|
920
|
+
result.usage.input_tokens_details = {
|
|
921
|
+
...(result.usage.input_tokens_details || {}),
|
|
922
|
+
...(usage.input_tokens_details || {}),
|
|
923
|
+
};
|
|
924
|
+
result.usage.output_tokens_details.reasoning_tokens =
|
|
925
|
+
usage.output_tokens_details?.reasoning_tokens || 0;
|
|
926
|
+
}
|
|
927
|
+
yield chunk;
|
|
928
|
+
}
|
|
929
|
+
if (llmResponse) {
|
|
930
|
+
result.output.push({
|
|
931
|
+
type: 'message',
|
|
932
|
+
content: [{ type: 'text', text: llmResponse }],
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
if (responseTools.length > 0) {
|
|
936
|
+
result.output.push(...responseTools);
|
|
937
|
+
}
|
|
938
|
+
const ttft = timestamps.length > 0 ? (timestamps[0] - startTime) / 1000 : 0;
|
|
939
|
+
let tbt = 0;
|
|
940
|
+
if (timestamps.length > 1) {
|
|
941
|
+
const timeDiffs = timestamps.slice(1).map((t, i) => t - timestamps[i]);
|
|
942
|
+
tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;
|
|
943
|
+
}
|
|
944
|
+
metricParams = await OpenAIWrapper._responsesCommonSetter({
|
|
945
|
+
args,
|
|
946
|
+
genAIEndpoint,
|
|
947
|
+
result,
|
|
948
|
+
span,
|
|
949
|
+
ttft,
|
|
950
|
+
tbt,
|
|
951
|
+
});
|
|
952
|
+
return result;
|
|
953
|
+
}
|
|
954
|
+
catch (e) {
|
|
955
|
+
helpers_1.default.handleException(span, e);
|
|
956
|
+
throw e;
|
|
957
|
+
}
|
|
958
|
+
finally {
|
|
959
|
+
span.end();
|
|
960
|
+
if (metricParams) {
|
|
961
|
+
base_wrapper_1.default.recordMetrics(span, metricParams);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
static async _responsesCommonSetter({ args, genAIEndpoint, result, span, ttft = 0, tbt = 0, }) {
|
|
966
|
+
const captureContent = config_1.default.captureMessageContent;
|
|
967
|
+
const requestModel = args[0]?.model || 'gpt-4o';
|
|
968
|
+
const { input, temperature = 1.0, top_p = 1.0, max_output_tokens, reasoning, stream = false, instructions, tools: responsesTools, } = args[0];
|
|
969
|
+
const responsesMessages = typeof input === 'string'
|
|
970
|
+
? [{ role: 'user', content: input }]
|
|
971
|
+
: (Array.isArray(input) ? input : []);
|
|
972
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TEMPERATURE, temperature);
|
|
973
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_TOP_P, top_p);
|
|
974
|
+
if (max_output_tokens != null) {
|
|
975
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_MAX_TOKENS, max_output_tokens);
|
|
976
|
+
}
|
|
977
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_IS_STREAM, stream);
|
|
978
|
+
if (reasoning?.effort) {
|
|
979
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_REQUEST_REASONING_EFFORT, reasoning.effort);
|
|
980
|
+
}
|
|
981
|
+
if (captureContent) {
|
|
982
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_INPUT_MESSAGES, helpers_1.default.buildInputMessages(responsesMessages));
|
|
983
|
+
}
|
|
984
|
+
const responseModel = result.model || requestModel;
|
|
985
|
+
const pricingInfo = config_1.default.pricingInfo || {};
|
|
986
|
+
const inputTokens = result.usage?.input_tokens || 0;
|
|
987
|
+
const outputTokens = result.usage?.output_tokens || 0;
|
|
988
|
+
// Responses API reports input_tokens inclusive of cached tokens. Map
|
|
989
|
+
// cache_write_tokens / cache_creation_tokens into creation for pricing.
|
|
990
|
+
const inputDetails = result.usage?.input_tokens_details || {};
|
|
991
|
+
const cacheReadTokens = Number(inputDetails.cached_tokens) || 0;
|
|
992
|
+
const cacheCreationTokens = Number(inputDetails.cache_creation_tokens) ||
|
|
993
|
+
Number(inputDetails.cache_write_tokens) ||
|
|
994
|
+
0;
|
|
995
|
+
const cost = helpers_1.default.getChatModelCost(requestModel, pricingInfo, inputTokens, outputTokens, cacheReadTokens, cacheCreationTokens, true);
|
|
996
|
+
OpenAIWrapper.setBaseSpanAttributes(span, {
|
|
997
|
+
genAIEndpoint,
|
|
998
|
+
model: requestModel,
|
|
999
|
+
user: '',
|
|
1000
|
+
cost,
|
|
1001
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
1002
|
+
serverAddress: OpenAIWrapper.serverAddress,
|
|
1003
|
+
serverPort: OpenAIWrapper.serverPort,
|
|
1004
|
+
});
|
|
1005
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_ID, result.id);
|
|
1006
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_MODEL, responseModel);
|
|
1007
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON, [result.status || 'completed']);
|
|
1008
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_OUTPUT_TYPE, semantic_convention_1.default.GEN_AI_OUTPUT_TYPE_TEXT);
|
|
1009
|
+
span.setAttribute(semantic_convention_1.default.OPENAI_API_TYPE, 'responses');
|
|
1010
|
+
if (result.service_tier) {
|
|
1011
|
+
span.setAttribute(semantic_convention_1.default.OPENAI_RESPONSE_SERVICE_TIER, result.service_tier);
|
|
1012
|
+
}
|
|
1013
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS, inputTokens);
|
|
1014
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);
|
|
1015
|
+
if (cacheReadTokens) {
|
|
1016
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cacheReadTokens);
|
|
1017
|
+
}
|
|
1018
|
+
if (cacheCreationTokens) {
|
|
1019
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cacheCreationTokens);
|
|
1020
|
+
}
|
|
1021
|
+
if (result.usage?.output_tokens_details?.reasoning_tokens) {
|
|
1022
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_USAGE_REASONING_TOKENS, result.usage.output_tokens_details.reasoning_tokens);
|
|
1023
|
+
}
|
|
1024
|
+
if (ttft > 0) {
|
|
1025
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_SERVER_TTFT, ttft);
|
|
1026
|
+
}
|
|
1027
|
+
if (tbt > 0) {
|
|
1028
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_SERVER_TBT, tbt);
|
|
1029
|
+
}
|
|
1030
|
+
let completionText = '';
|
|
1031
|
+
if (result.output && Array.isArray(result.output)) {
|
|
1032
|
+
for (const item of result.output) {
|
|
1033
|
+
if (item.type === 'message' && item.content) {
|
|
1034
|
+
for (const content of item.content) {
|
|
1035
|
+
if (content.type === 'text' || content.type === 'output_text') {
|
|
1036
|
+
completionText += content.text || '';
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
const toolCalls = result.tools || [];
|
|
1043
|
+
if (toolCalls.length > 0) {
|
|
1044
|
+
const toolNames = toolCalls.map((t) => t.name || '').filter(Boolean);
|
|
1045
|
+
const toolIds = toolCalls.map((t) => t.call_id || '').filter(Boolean);
|
|
1046
|
+
const toolArgs = toolCalls.map((t) => t.arguments || '').filter(Boolean);
|
|
1047
|
+
if (toolNames.length > 0) {
|
|
1048
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_NAME, toolNames.join(', '));
|
|
1049
|
+
}
|
|
1050
|
+
if (toolIds.length > 0) {
|
|
1051
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_CALL_ID, toolIds.join(', '));
|
|
1052
|
+
}
|
|
1053
|
+
if (toolArgs.length > 0) {
|
|
1054
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_ARGS, toolArgs.join(', '));
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
let inputMessagesJson;
|
|
1058
|
+
let outputMessagesJson;
|
|
1059
|
+
const toolDefinitionsJson = helpers_1.default.buildToolDefinitions(responsesTools);
|
|
1060
|
+
const systemInstructionsJson = instructions
|
|
1061
|
+
? JSON.stringify([{ type: 'text', content: String(instructions) }])
|
|
1062
|
+
: undefined;
|
|
1063
|
+
const versionExtras = OpenAIWrapper._stampAgentVersion(span, {
|
|
1064
|
+
systemInstructionsJson,
|
|
1065
|
+
toolDefinitionsJson,
|
|
1066
|
+
primaryModel: responseModel || requestModel,
|
|
1067
|
+
temperature,
|
|
1068
|
+
top_p,
|
|
1069
|
+
max_tokens: max_output_tokens ?? null,
|
|
1070
|
+
});
|
|
1071
|
+
if (captureContent) {
|
|
1072
|
+
outputMessagesJson = helpers_1.default.buildOutputMessages(completionText, result.status || 'stop');
|
|
1073
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_OUTPUT_MESSAGES, outputMessagesJson);
|
|
1074
|
+
inputMessagesJson = helpers_1.default.buildInputMessages(responsesMessages);
|
|
1075
|
+
if (systemInstructionsJson) {
|
|
1076
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_SYSTEM_INSTRUCTIONS, systemInstructionsJson);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
if (toolDefinitionsJson) {
|
|
1080
|
+
span.setAttribute(semantic_convention_1.default.GEN_AI_TOOL_DEFINITIONS, toolDefinitionsJson);
|
|
1081
|
+
}
|
|
1082
|
+
if (!config_1.default.disableEvents) {
|
|
1083
|
+
const eventAttrs = {
|
|
1084
|
+
[semantic_convention_1.default.GEN_AI_OPERATION]: semantic_convention_1.default.GEN_AI_OPERATION_TYPE_CHAT,
|
|
1085
|
+
[semantic_convention_1.default.GEN_AI_REQUEST_MODEL]: requestModel,
|
|
1086
|
+
[semantic_convention_1.default.GEN_AI_RESPONSE_MODEL]: responseModel,
|
|
1087
|
+
[semantic_convention_1.default.SERVER_ADDRESS]: OpenAIWrapper.serverAddress,
|
|
1088
|
+
[semantic_convention_1.default.SERVER_PORT]: OpenAIWrapper.serverPort,
|
|
1089
|
+
[semantic_convention_1.default.GEN_AI_RESPONSE_ID]: result.id,
|
|
1090
|
+
[semantic_convention_1.default.GEN_AI_RESPONSE_FINISH_REASON]: [result.status || 'completed'],
|
|
1091
|
+
[semantic_convention_1.default.GEN_AI_OUTPUT_TYPE]: semantic_convention_1.default.GEN_AI_OUTPUT_TYPE_TEXT,
|
|
1092
|
+
[semantic_convention_1.default.GEN_AI_USAGE_INPUT_TOKENS]: inputTokens,
|
|
1093
|
+
[semantic_convention_1.default.GEN_AI_USAGE_OUTPUT_TOKENS]: outputTokens,
|
|
1094
|
+
...versionExtras,
|
|
1095
|
+
};
|
|
1096
|
+
if (captureContent) {
|
|
1097
|
+
if (inputMessagesJson)
|
|
1098
|
+
eventAttrs[semantic_convention_1.default.GEN_AI_INPUT_MESSAGES] = inputMessagesJson;
|
|
1099
|
+
if (outputMessagesJson)
|
|
1100
|
+
eventAttrs[semantic_convention_1.default.GEN_AI_OUTPUT_MESSAGES] = outputMessagesJson;
|
|
1101
|
+
if (systemInstructionsJson)
|
|
1102
|
+
eventAttrs[semantic_convention_1.default.GEN_AI_SYSTEM_INSTRUCTIONS] = systemInstructionsJson;
|
|
1103
|
+
}
|
|
1104
|
+
if (toolDefinitionsJson)
|
|
1105
|
+
eventAttrs[semantic_convention_1.default.GEN_AI_TOOL_DEFINITIONS] = toolDefinitionsJson;
|
|
1106
|
+
helpers_1.default.emitInferenceEvent(span, eventAttrs);
|
|
1107
|
+
}
|
|
1108
|
+
return {
|
|
1109
|
+
genAIEndpoint,
|
|
1110
|
+
model: requestModel,
|
|
1111
|
+
user: '',
|
|
1112
|
+
cost,
|
|
1113
|
+
aiSystem: OpenAIWrapper.aiSystem,
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
OpenAIWrapper.aiSystem = semantic_convention_1.default.GEN_AI_SYSTEM_OPENAI;
|
|
1118
|
+
OpenAIWrapper.serverAddress = 'api.openai.com';
|
|
1119
|
+
OpenAIWrapper.serverPort = 443;
|
|
1120
|
+
exports.default = OpenAIWrapper;
|
|
1121
|
+
//# sourceMappingURL=wrapper.js.map
|