@juspay/neurolink 9.24.0 → 9.25.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 (215) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/adapters/tts/googleTTSHandler.js +26 -1
  3. package/dist/adapters/video/vertexVideoHandler.js +23 -17
  4. package/dist/cli/commands/config.d.ts +3 -3
  5. package/dist/cli/commands/observability.d.ts +53 -0
  6. package/dist/cli/commands/observability.js +453 -0
  7. package/dist/cli/commands/telemetry.d.ts +63 -0
  8. package/dist/cli/commands/telemetry.js +689 -0
  9. package/dist/cli/factories/commandFactory.js +29 -15
  10. package/dist/cli/parser.js +6 -9
  11. package/dist/cli/utils/formatters.d.ts +13 -0
  12. package/dist/cli/utils/formatters.js +23 -0
  13. package/dist/constants/contextWindows.js +6 -0
  14. package/dist/constants/enums.d.ts +6 -0
  15. package/dist/constants/enums.js +8 -2
  16. package/dist/context/budgetChecker.js +75 -48
  17. package/dist/context/contextCompactor.js +135 -127
  18. package/dist/core/baseProvider.d.ts +5 -0
  19. package/dist/core/baseProvider.js +158 -102
  20. package/dist/core/conversationMemoryInitializer.js +7 -4
  21. package/dist/core/conversationMemoryManager.d.ts +2 -0
  22. package/dist/core/conversationMemoryManager.js +6 -2
  23. package/dist/core/modules/GenerationHandler.d.ts +2 -2
  24. package/dist/core/modules/GenerationHandler.js +12 -12
  25. package/dist/evaluation/ragasEvaluator.js +39 -19
  26. package/dist/evaluation/scoring.js +46 -20
  27. package/dist/features/ppt/presentationOrchestrator.js +23 -0
  28. package/dist/features/ppt/slideGenerator.js +13 -0
  29. package/dist/features/ppt/slideRenderers.d.ts +1 -1
  30. package/dist/features/ppt/slideRenderers.js +6 -4
  31. package/dist/features/ppt/slideTypeInference.d.ts +1 -1
  32. package/dist/features/ppt/slideTypeInference.js +75 -73
  33. package/dist/files/fileTools.d.ts +6 -6
  34. package/dist/index.d.ts +46 -12
  35. package/dist/index.js +79 -17
  36. package/dist/lib/adapters/tts/googleTTSHandler.js +26 -1
  37. package/dist/lib/adapters/video/vertexVideoHandler.js +23 -17
  38. package/dist/lib/constants/contextWindows.js +6 -0
  39. package/dist/lib/constants/enums.d.ts +6 -0
  40. package/dist/lib/constants/enums.js +8 -2
  41. package/dist/lib/context/budgetChecker.js +75 -48
  42. package/dist/lib/context/contextCompactor.js +135 -127
  43. package/dist/lib/core/baseProvider.d.ts +5 -0
  44. package/dist/lib/core/baseProvider.js +158 -102
  45. package/dist/lib/core/conversationMemoryInitializer.js +7 -4
  46. package/dist/lib/core/conversationMemoryManager.d.ts +2 -0
  47. package/dist/lib/core/conversationMemoryManager.js +6 -2
  48. package/dist/lib/core/modules/GenerationHandler.d.ts +2 -2
  49. package/dist/lib/core/modules/GenerationHandler.js +12 -12
  50. package/dist/lib/evaluation/ragasEvaluator.js +39 -19
  51. package/dist/lib/evaluation/scoring.js +46 -20
  52. package/dist/lib/features/ppt/presentationOrchestrator.js +23 -0
  53. package/dist/lib/features/ppt/slideGenerator.js +13 -0
  54. package/dist/lib/features/ppt/slideRenderers.d.ts +1 -1
  55. package/dist/lib/features/ppt/slideRenderers.js +6 -4
  56. package/dist/lib/features/ppt/slideTypeInference.d.ts +1 -1
  57. package/dist/lib/features/ppt/slideTypeInference.js +75 -73
  58. package/dist/lib/files/fileTools.d.ts +6 -6
  59. package/dist/lib/index.d.ts +46 -12
  60. package/dist/lib/index.js +79 -17
  61. package/dist/lib/mcp/httpRateLimiter.js +39 -12
  62. package/dist/lib/mcp/httpRetryHandler.js +22 -1
  63. package/dist/lib/mcp/mcpClientFactory.js +13 -15
  64. package/dist/lib/memory/memoryRetrievalTools.js +22 -0
  65. package/dist/lib/neurolink.d.ts +64 -72
  66. package/dist/lib/neurolink.js +984 -566
  67. package/dist/lib/observability/exporterRegistry.d.ts +152 -0
  68. package/dist/lib/observability/exporterRegistry.js +414 -0
  69. package/dist/lib/observability/exporters/arizeExporter.d.ts +32 -0
  70. package/dist/lib/observability/exporters/arizeExporter.js +139 -0
  71. package/dist/lib/observability/exporters/baseExporter.d.ts +117 -0
  72. package/dist/lib/observability/exporters/baseExporter.js +191 -0
  73. package/dist/lib/observability/exporters/braintrustExporter.d.ts +30 -0
  74. package/dist/lib/observability/exporters/braintrustExporter.js +155 -0
  75. package/dist/lib/observability/exporters/datadogExporter.d.ts +37 -0
  76. package/dist/lib/observability/exporters/datadogExporter.js +197 -0
  77. package/dist/lib/observability/exporters/index.d.ts +13 -0
  78. package/dist/lib/observability/exporters/index.js +14 -0
  79. package/dist/lib/observability/exporters/laminarExporter.d.ts +48 -0
  80. package/dist/lib/observability/exporters/laminarExporter.js +303 -0
  81. package/dist/lib/observability/exporters/langfuseExporter.d.ts +47 -0
  82. package/dist/lib/observability/exporters/langfuseExporter.js +200 -0
  83. package/dist/lib/observability/exporters/langsmithExporter.d.ts +26 -0
  84. package/dist/lib/observability/exporters/langsmithExporter.js +124 -0
  85. package/dist/lib/observability/exporters/otelExporter.d.ts +39 -0
  86. package/dist/lib/observability/exporters/otelExporter.js +165 -0
  87. package/dist/lib/observability/exporters/posthogExporter.d.ts +48 -0
  88. package/dist/lib/observability/exporters/posthogExporter.js +288 -0
  89. package/dist/lib/observability/exporters/sentryExporter.d.ts +32 -0
  90. package/dist/lib/observability/exporters/sentryExporter.js +166 -0
  91. package/dist/lib/observability/index.d.ts +25 -0
  92. package/dist/lib/observability/index.js +32 -0
  93. package/dist/lib/observability/metricsAggregator.d.ts +260 -0
  94. package/dist/lib/observability/metricsAggregator.js +553 -0
  95. package/dist/lib/observability/otelBridge.d.ts +49 -0
  96. package/dist/lib/observability/otelBridge.js +132 -0
  97. package/dist/lib/observability/retryPolicy.d.ts +192 -0
  98. package/dist/lib/observability/retryPolicy.js +384 -0
  99. package/dist/lib/observability/sampling/index.d.ts +4 -0
  100. package/dist/lib/observability/sampling/index.js +5 -0
  101. package/dist/lib/observability/sampling/samplers.d.ts +116 -0
  102. package/dist/lib/observability/sampling/samplers.js +217 -0
  103. package/dist/lib/observability/spanProcessor.d.ts +129 -0
  104. package/dist/lib/observability/spanProcessor.js +288 -0
  105. package/dist/lib/observability/tokenTracker.d.ts +156 -0
  106. package/dist/lib/observability/tokenTracker.js +414 -0
  107. package/dist/lib/observability/types/exporterTypes.d.ts +250 -0
  108. package/dist/lib/observability/types/exporterTypes.js +6 -0
  109. package/dist/lib/observability/types/index.d.ts +6 -0
  110. package/dist/lib/observability/types/index.js +5 -0
  111. package/dist/lib/observability/types/spanTypes.d.ts +244 -0
  112. package/dist/lib/observability/types/spanTypes.js +93 -0
  113. package/dist/lib/observability/utils/index.d.ts +4 -0
  114. package/dist/lib/observability/utils/index.js +5 -0
  115. package/dist/lib/observability/utils/spanSerializer.d.ts +115 -0
  116. package/dist/lib/observability/utils/spanSerializer.js +287 -0
  117. package/dist/lib/providers/amazonSagemaker.d.ts +5 -4
  118. package/dist/lib/providers/amazonSagemaker.js +3 -4
  119. package/dist/lib/providers/googleVertex.d.ts +7 -0
  120. package/dist/lib/providers/googleVertex.js +80 -2
  121. package/dist/lib/rag/pipeline/RAGPipeline.d.ts +0 -5
  122. package/dist/lib/rag/pipeline/RAGPipeline.js +122 -87
  123. package/dist/lib/rag/ragIntegration.js +30 -0
  124. package/dist/lib/rag/retrieval/hybridSearch.js +22 -0
  125. package/dist/lib/server/abstract/baseServerAdapter.js +51 -19
  126. package/dist/lib/server/middleware/common.js +44 -12
  127. package/dist/lib/services/server/ai/observability/instrumentation.d.ts +2 -2
  128. package/dist/lib/services/server/ai/observability/instrumentation.js +10 -5
  129. package/dist/lib/types/conversationMemoryInterface.d.ts +2 -0
  130. package/dist/lib/types/modelTypes.d.ts +18 -18
  131. package/dist/lib/types/providers.d.ts +5 -0
  132. package/dist/lib/utils/pricing.js +25 -1
  133. package/dist/lib/utils/ttsProcessor.js +74 -59
  134. package/dist/lib/workflow/config.d.ts +36 -36
  135. package/dist/lib/workflow/core/ensembleExecutor.js +10 -0
  136. package/dist/lib/workflow/core/judgeScorer.js +20 -2
  137. package/dist/lib/workflow/core/workflowRunner.js +34 -1
  138. package/dist/mcp/httpRateLimiter.js +39 -12
  139. package/dist/mcp/httpRetryHandler.js +22 -1
  140. package/dist/mcp/mcpClientFactory.js +13 -15
  141. package/dist/memory/memoryRetrievalTools.js +22 -0
  142. package/dist/neurolink.d.ts +64 -72
  143. package/dist/neurolink.js +984 -566
  144. package/dist/observability/FEATURE-STATUS.md +269 -0
  145. package/dist/observability/exporterRegistry.d.ts +152 -0
  146. package/dist/observability/exporterRegistry.js +413 -0
  147. package/dist/observability/exporters/arizeExporter.d.ts +32 -0
  148. package/dist/observability/exporters/arizeExporter.js +138 -0
  149. package/dist/observability/exporters/baseExporter.d.ts +117 -0
  150. package/dist/observability/exporters/baseExporter.js +190 -0
  151. package/dist/observability/exporters/braintrustExporter.d.ts +30 -0
  152. package/dist/observability/exporters/braintrustExporter.js +154 -0
  153. package/dist/observability/exporters/datadogExporter.d.ts +37 -0
  154. package/dist/observability/exporters/datadogExporter.js +196 -0
  155. package/dist/observability/exporters/index.d.ts +13 -0
  156. package/dist/observability/exporters/index.js +13 -0
  157. package/dist/observability/exporters/laminarExporter.d.ts +48 -0
  158. package/dist/observability/exporters/laminarExporter.js +302 -0
  159. package/dist/observability/exporters/langfuseExporter.d.ts +47 -0
  160. package/dist/observability/exporters/langfuseExporter.js +199 -0
  161. package/dist/observability/exporters/langsmithExporter.d.ts +26 -0
  162. package/dist/observability/exporters/langsmithExporter.js +123 -0
  163. package/dist/observability/exporters/otelExporter.d.ts +39 -0
  164. package/dist/observability/exporters/otelExporter.js +164 -0
  165. package/dist/observability/exporters/posthogExporter.d.ts +48 -0
  166. package/dist/observability/exporters/posthogExporter.js +287 -0
  167. package/dist/observability/exporters/sentryExporter.d.ts +32 -0
  168. package/dist/observability/exporters/sentryExporter.js +165 -0
  169. package/dist/observability/index.d.ts +25 -0
  170. package/dist/observability/index.js +31 -0
  171. package/dist/observability/metricsAggregator.d.ts +260 -0
  172. package/dist/observability/metricsAggregator.js +552 -0
  173. package/dist/observability/otelBridge.d.ts +49 -0
  174. package/dist/observability/otelBridge.js +131 -0
  175. package/dist/observability/retryPolicy.d.ts +192 -0
  176. package/dist/observability/retryPolicy.js +383 -0
  177. package/dist/observability/sampling/index.d.ts +4 -0
  178. package/dist/observability/sampling/index.js +4 -0
  179. package/dist/observability/sampling/samplers.d.ts +116 -0
  180. package/dist/observability/sampling/samplers.js +216 -0
  181. package/dist/observability/spanProcessor.d.ts +129 -0
  182. package/dist/observability/spanProcessor.js +287 -0
  183. package/dist/observability/tokenTracker.d.ts +156 -0
  184. package/dist/observability/tokenTracker.js +413 -0
  185. package/dist/observability/types/exporterTypes.d.ts +250 -0
  186. package/dist/observability/types/exporterTypes.js +5 -0
  187. package/dist/observability/types/index.d.ts +6 -0
  188. package/dist/observability/types/index.js +4 -0
  189. package/dist/observability/types/spanTypes.d.ts +244 -0
  190. package/dist/observability/types/spanTypes.js +92 -0
  191. package/dist/observability/utils/index.d.ts +4 -0
  192. package/dist/observability/utils/index.js +4 -0
  193. package/dist/observability/utils/spanSerializer.d.ts +115 -0
  194. package/dist/observability/utils/spanSerializer.js +286 -0
  195. package/dist/providers/amazonSagemaker.d.ts +5 -4
  196. package/dist/providers/amazonSagemaker.js +3 -4
  197. package/dist/providers/googleVertex.d.ts +7 -0
  198. package/dist/providers/googleVertex.js +80 -2
  199. package/dist/rag/pipeline/RAGPipeline.d.ts +0 -5
  200. package/dist/rag/pipeline/RAGPipeline.js +122 -87
  201. package/dist/rag/ragIntegration.js +30 -0
  202. package/dist/rag/retrieval/hybridSearch.js +22 -0
  203. package/dist/server/abstract/baseServerAdapter.js +51 -19
  204. package/dist/server/middleware/common.js +44 -12
  205. package/dist/services/server/ai/observability/instrumentation.d.ts +2 -2
  206. package/dist/services/server/ai/observability/instrumentation.js +10 -5
  207. package/dist/types/conversationMemoryInterface.d.ts +2 -0
  208. package/dist/types/providers.d.ts +5 -0
  209. package/dist/utils/pricing.js +25 -1
  210. package/dist/utils/ttsProcessor.js +74 -59
  211. package/dist/workflow/config.d.ts +52 -52
  212. package/dist/workflow/core/ensembleExecutor.js +10 -0
  213. package/dist/workflow/core/judgeScorer.js +20 -2
  214. package/dist/workflow/core/workflowRunner.js +34 -1
  215. package/package.json +1 -1
@@ -0,0 +1,39 @@
1
+ /**
2
+ * OpenTelemetry Exporter
3
+ * Exports spans to OTLP-compatible backends
4
+ */
5
+ import type { ExporterHealthStatus, ExportResult, OtelExporterConfig, SpanData } from "../types/index.js";
6
+ import { BaseExporter } from "./baseExporter.js";
7
+ /**
8
+ * OpenTelemetry exporter for OTLP-compatible backends
9
+ * Supports HTTP, gRPC, and Zipkin protocols
10
+ */
11
+ export declare class OtelExporter extends BaseExporter {
12
+ private readonly endpoint;
13
+ private readonly protocol;
14
+ private readonly serviceName;
15
+ private readonly serviceVersion;
16
+ private readonly resourceAttributes;
17
+ private readonly compression;
18
+ constructor(config: OtelExporterConfig);
19
+ initialize(): Promise<void>;
20
+ exportSpan(span: SpanData): Promise<ExportResult>;
21
+ exportBatch(spans: SpanData[]): Promise<ExportResult>;
22
+ flush(): Promise<void>;
23
+ shutdown(): Promise<void>;
24
+ healthCheck(): Promise<ExporterHealthStatus>;
25
+ /**
26
+ * Verify connectivity to OTLP endpoint
27
+ */
28
+ protected ping(): Promise<void>;
29
+ /**
30
+ * Get the export URL based on protocol
31
+ */
32
+ private getExportUrl;
33
+ /**
34
+ * Send request with optional gzip compression
35
+ * @param endpoint - The URL to send to
36
+ * @param body - The payload to send
37
+ */
38
+ private sendRequest;
39
+ }
@@ -0,0 +1,164 @@
1
+ /**
2
+ * OpenTelemetry Exporter
3
+ * Exports spans to OTLP-compatible backends
4
+ */
5
+ import { gzipSync } from "zlib";
6
+ import { SpanSerializer } from "../utils/spanSerializer.js";
7
+ import { BaseExporter } from "./baseExporter.js";
8
+ /**
9
+ * OpenTelemetry exporter for OTLP-compatible backends
10
+ * Supports HTTP, gRPC, and Zipkin protocols
11
+ */
12
+ export class OtelExporter extends BaseExporter {
13
+ endpoint;
14
+ protocol;
15
+ serviceName;
16
+ serviceVersion;
17
+ resourceAttributes;
18
+ compression;
19
+ constructor(config) {
20
+ super("opentelemetry", config);
21
+ this.endpoint = config.endpoint;
22
+ this.protocol = config.protocol ?? "http";
23
+ this.serviceName = config.serviceName ?? "neurolink-ai";
24
+ this.serviceVersion = config.serviceVersion ?? "1.0.0";
25
+ this.resourceAttributes = config.resourceAttributes ?? {};
26
+ this.compression = config.compression ?? "none";
27
+ }
28
+ async initialize() {
29
+ if (this.initialized) {
30
+ return;
31
+ }
32
+ this.initialized = true;
33
+ this.startFlushInterval(this.config.flushIntervalMs ?? 5000);
34
+ }
35
+ async exportSpan(span) {
36
+ // Intentionally buffer spans rather than exporting immediately.
37
+ // OTLP is designed for batch export (resourceSpans envelope), so we
38
+ // accumulate spans and flush them together via exportBatch() on the
39
+ // configured flushInterval or when the buffer reaches maxBufferSize.
40
+ this.bufferSpan(span);
41
+ return this.createSuccessResult(0, 0);
42
+ }
43
+ async exportBatch(spans) {
44
+ const startTime = Date.now();
45
+ try {
46
+ const otelSpans = spans.map((s) => SpanSerializer.toOtelFormat(s));
47
+ const payload = {
48
+ resourceSpans: [
49
+ {
50
+ resource: {
51
+ attributes: [
52
+ {
53
+ key: "service.name",
54
+ value: { stringValue: this.serviceName },
55
+ },
56
+ {
57
+ key: "service.version",
58
+ value: { stringValue: this.serviceVersion },
59
+ },
60
+ ...Object.entries(this.resourceAttributes).map(([key, value]) => ({
61
+ key,
62
+ value: { stringValue: value },
63
+ })),
64
+ ],
65
+ },
66
+ scopeSpans: [
67
+ {
68
+ scope: {
69
+ name: "neurolink-observability",
70
+ version: "1.0.0",
71
+ },
72
+ spans: otelSpans,
73
+ },
74
+ ],
75
+ },
76
+ ],
77
+ };
78
+ const url = this.getExportUrl();
79
+ await this.sendRequest(url, payload);
80
+ return this.createSuccessResult(spans.length, Date.now() - startTime);
81
+ }
82
+ catch (error) {
83
+ return this.createFailureResult(spans.map((s) => s.spanId), error instanceof Error ? error.message : String(error), Date.now() - startTime);
84
+ }
85
+ }
86
+ async flush() {
87
+ if (this.buffer.length > 0) {
88
+ const spans = [...this.buffer];
89
+ this.buffer = [];
90
+ await this.exportBatch(spans);
91
+ }
92
+ }
93
+ async shutdown() {
94
+ await this.flush();
95
+ this.stopFlushInterval();
96
+ this.initialized = false;
97
+ }
98
+ async healthCheck() {
99
+ try {
100
+ await this.withRetry(() => this.ping(), "health check");
101
+ return this.createHealthStatus(true);
102
+ }
103
+ catch {
104
+ return this.createHealthStatus(false, ["Endpoint unreachable"]);
105
+ }
106
+ }
107
+ /**
108
+ * Verify connectivity to OTLP endpoint
109
+ */
110
+ async ping() {
111
+ const response = await fetch(this.endpoint, { method: "HEAD" });
112
+ // 405 (Method Not Allowed) is acceptable for HEAD requests
113
+ if (!response.ok && response.status !== 405) {
114
+ throw new Error(`OTLP endpoint unreachable: ${response.status}`);
115
+ }
116
+ }
117
+ /**
118
+ * Get the export URL based on protocol
119
+ */
120
+ getExportUrl() {
121
+ switch (this.protocol) {
122
+ case "http":
123
+ return `${this.endpoint}/v1/traces`;
124
+ case "zipkin":
125
+ return `${this.endpoint}/api/v2/spans`;
126
+ case "grpc":
127
+ // For gRPC, this would use @grpc/grpc-js
128
+ return this.endpoint;
129
+ default:
130
+ return `${this.endpoint}/v1/traces`;
131
+ }
132
+ }
133
+ /**
134
+ * Send request with optional gzip compression
135
+ * @param endpoint - The URL to send to
136
+ * @param body - The payload to send
137
+ */
138
+ async sendRequest(endpoint, body) {
139
+ const jsonBody = JSON.stringify(body);
140
+ let bodyData = jsonBody;
141
+ const headers = {
142
+ "Content-Type": "application/json",
143
+ };
144
+ // Apply gzip compression if configured
145
+ if (this.compression === "gzip") {
146
+ const compressed = gzipSync(Buffer.from(jsonBody));
147
+ // Convert Buffer to Uint8Array for fetch compatibility
148
+ bodyData = new Uint8Array(compressed);
149
+ headers["Content-Encoding"] = "gzip";
150
+ }
151
+ // Add any custom headers from config
152
+ if (this.config.headers) {
153
+ Object.assign(headers, this.config.headers);
154
+ }
155
+ const response = await fetch(endpoint, {
156
+ method: "POST",
157
+ headers,
158
+ body: bodyData,
159
+ });
160
+ if (!response.ok) {
161
+ throw new Error(`OTLP export failed: ${response.statusText}`);
162
+ }
163
+ }
164
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * PostHog Exporter
3
+ * Exports spans to PostHog product analytics platform
4
+ * @see https://posthog.com/docs/api
5
+ */
6
+ import type { ExporterHealthStatus, ExportResult, PostHogExporterConfig, SpanData } from "../types/index.js";
7
+ import { BaseExporter } from "./baseExporter.js";
8
+ /**
9
+ * PostHog exporter for product analytics and LLM event tracking
10
+ * Supports capturing LLM interactions as events with properties
11
+ */
12
+ export declare class PostHogExporter extends BaseExporter {
13
+ private readonly apiKey;
14
+ private readonly host;
15
+ private readonly personalApiKey?;
16
+ constructor(config: PostHogExporterConfig);
17
+ initialize(): Promise<void>;
18
+ /**
19
+ * Get authorization headers
20
+ */
21
+ private getHeaders;
22
+ exportSpan(span: SpanData): Promise<ExportResult>;
23
+ exportBatch(spans: SpanData[]): Promise<ExportResult>;
24
+ flush(): Promise<void>;
25
+ shutdown(): Promise<void>;
26
+ healthCheck(): Promise<ExporterHealthStatus>;
27
+ /**
28
+ * Verify connectivity to PostHog API
29
+ */
30
+ protected ping(): Promise<void>;
31
+ /**
32
+ * Convert span to PostHog event format
33
+ */
34
+ private convertToPostHogEvent;
35
+ /**
36
+ * Get event name based on span type
37
+ */
38
+ private getEventName;
39
+ /**
40
+ * Convert span status to string
41
+ */
42
+ private getStatusString;
43
+ /**
44
+ * Extract custom properties from span attributes
45
+ * Filters out standard attributes that are already handled
46
+ */
47
+ private extractCustomProperties;
48
+ }
@@ -0,0 +1,287 @@
1
+ /**
2
+ * PostHog Exporter
3
+ * Exports spans to PostHog product analytics platform
4
+ * @see https://posthog.com/docs/api
5
+ */
6
+ import { logger } from "../../utils/logger.js";
7
+ import { SpanStatus, SpanType } from "../types/spanTypes.js";
8
+ import { BaseExporter } from "./baseExporter.js";
9
+ /**
10
+ * PostHog exporter for product analytics and LLM event tracking
11
+ * Supports capturing LLM interactions as events with properties
12
+ */
13
+ export class PostHogExporter extends BaseExporter {
14
+ apiKey;
15
+ host;
16
+ personalApiKey;
17
+ constructor(config) {
18
+ super("posthog", config);
19
+ this.apiKey = config.apiKey;
20
+ this.host = config.host ?? "https://app.posthog.com";
21
+ this.personalApiKey = config.personalApiKey;
22
+ }
23
+ async initialize() {
24
+ if (this.initialized) {
25
+ return;
26
+ }
27
+ // Verify API key by making a test call
28
+ try {
29
+ const response = await fetch(`${this.host}/api/projects/`, {
30
+ headers: this.getHeaders(),
31
+ });
32
+ if (!response.ok && response.status !== 401) {
33
+ // 401 is expected with project API key
34
+ logger.warn("[PostHog] Could not verify API connection:", response.statusText);
35
+ }
36
+ }
37
+ catch (error) {
38
+ logger.warn("[PostHog] Could not verify API connection:", error instanceof Error ? error.message : error);
39
+ }
40
+ this.initialized = true;
41
+ this.startFlushInterval(this.config.flushIntervalMs ?? 5000);
42
+ }
43
+ /**
44
+ * Get authorization headers
45
+ */
46
+ getHeaders() {
47
+ const headers = {
48
+ "Content-Type": "application/json",
49
+ };
50
+ // Use personal API key for management endpoints, project API key for events
51
+ if (this.personalApiKey) {
52
+ headers["Authorization"] = `Bearer ${this.personalApiKey}`;
53
+ }
54
+ return headers;
55
+ }
56
+ async exportSpan(span) {
57
+ const startTime = Date.now();
58
+ try {
59
+ const event = this.convertToPostHogEvent(span);
60
+ const response = await fetch(`${this.host}/capture/`, {
61
+ method: "POST",
62
+ headers: {
63
+ "Content-Type": "application/json",
64
+ },
65
+ body: JSON.stringify(event),
66
+ });
67
+ if (!response.ok) {
68
+ throw new Error(`Export failed: ${response.statusText}`);
69
+ }
70
+ return this.createSuccessResult(1, Date.now() - startTime);
71
+ }
72
+ catch (error) {
73
+ return this.createFailureResult([span.spanId], error instanceof Error ? error.message : String(error), Date.now() - startTime);
74
+ }
75
+ }
76
+ async exportBatch(spans) {
77
+ const startTime = Date.now();
78
+ try {
79
+ const events = spans.map((s) => this.convertToPostHogEvent(s));
80
+ const response = await fetch(`${this.host}/batch/`, {
81
+ method: "POST",
82
+ headers: {
83
+ "Content-Type": "application/json",
84
+ },
85
+ body: JSON.stringify({
86
+ api_key: this.apiKey,
87
+ batch: events.map((e) => ({
88
+ ...e,
89
+ // For batch, we don't include api_key in each event
90
+ api_key: undefined,
91
+ })),
92
+ }),
93
+ });
94
+ if (!response.ok) {
95
+ throw new Error(`Batch export failed: ${response.statusText}`);
96
+ }
97
+ return this.createSuccessResult(spans.length, Date.now() - startTime);
98
+ }
99
+ catch (error) {
100
+ return this.createFailureResult(spans.map((s) => s.spanId), error instanceof Error ? error.message : String(error), Date.now() - startTime);
101
+ }
102
+ }
103
+ async flush() {
104
+ if (this.buffer.length > 0) {
105
+ const spans = [...this.buffer];
106
+ this.buffer = [];
107
+ await this.exportBatch(spans);
108
+ }
109
+ }
110
+ async shutdown() {
111
+ await this.flush();
112
+ this.stopFlushInterval();
113
+ this.initialized = false;
114
+ }
115
+ async healthCheck() {
116
+ try {
117
+ await this.withRetry(() => this.ping(), "health check");
118
+ return this.createHealthStatus(true);
119
+ }
120
+ catch {
121
+ return this.createHealthStatus(false, ["Health check failed"]);
122
+ }
123
+ }
124
+ /**
125
+ * Verify connectivity to PostHog API
126
+ */
127
+ async ping() {
128
+ // PostHog doesn't have a dedicated health endpoint, so we use decide endpoint
129
+ const response = await fetch(`${this.host}/decide/?v=3`, {
130
+ method: "POST",
131
+ headers: {
132
+ "Content-Type": "application/json",
133
+ },
134
+ body: JSON.stringify({
135
+ api_key: this.apiKey,
136
+ distinct_id: "health_check",
137
+ }),
138
+ });
139
+ if (!response.ok) {
140
+ throw new Error(`PostHog API unreachable: ${response.status}`);
141
+ }
142
+ }
143
+ /**
144
+ * Convert span to PostHog event format
145
+ */
146
+ convertToPostHogEvent(span) {
147
+ // Determine the event name based on span type
148
+ const eventName = this.getEventName(span);
149
+ // Get distinct ID from user.id or session.id, or use trace ID as fallback
150
+ const distinctId = span.attributes["user.id"] ||
151
+ span.attributes["session.id"] ||
152
+ span.traceId;
153
+ return {
154
+ api_key: this.apiKey,
155
+ event: eventName,
156
+ distinct_id: distinctId,
157
+ timestamp: span.startTime,
158
+ properties: {
159
+ // Core span data
160
+ $span_id: span.spanId,
161
+ $trace_id: span.traceId,
162
+ $parent_span_id: span.parentSpanId,
163
+ // AI-specific properties
164
+ ai_provider: span.attributes["ai.provider"],
165
+ ai_model: span.attributes["ai.model"],
166
+ ai_tokens_input: span.attributes["ai.tokens.input"],
167
+ ai_tokens_output: span.attributes["ai.tokens.output"],
168
+ ai_tokens_total: span.attributes["ai.tokens.total"],
169
+ ai_cost_total: span.attributes["ai.cost.total"],
170
+ ai_cost_currency: span.attributes["ai.cost.currency"] || "USD",
171
+ // Generation parameters
172
+ ai_temperature: span.attributes["ai.temperature"],
173
+ ai_max_tokens: span.attributes["ai.max_tokens"],
174
+ // Performance metrics
175
+ duration_ms: span.durationMs,
176
+ status: this.getStatusString(span.status),
177
+ status_message: span.statusMessage,
178
+ // Error tracking
179
+ is_error: span.status === SpanStatus.ERROR,
180
+ error_type: span.attributes["error.type"],
181
+ error_message: span.attributes["error.message"],
182
+ // Tool attributes
183
+ tool_name: span.attributes["tool.name"],
184
+ tool_server: span.attributes["tool.server"],
185
+ tool_success: span.attributes["tool.success"],
186
+ // Environment
187
+ environment: span.attributes["deployment.environment"] || this.config.environment,
188
+ service_name: span.attributes["service.name"],
189
+ service_version: span.attributes["service.version"] || this.config.version,
190
+ // Span type for filtering
191
+ span_type: span.type,
192
+ // Session tracking
193
+ $session_id: span.attributes["session.id"],
194
+ // Custom properties from attributes (filtered)
195
+ ...this.extractCustomProperties(span.attributes),
196
+ },
197
+ };
198
+ }
199
+ /**
200
+ * Get event name based on span type
201
+ */
202
+ getEventName(span) {
203
+ const eventNameMap = {
204
+ [SpanType.AGENT_RUN]: "ai_agent_run",
205
+ [SpanType.WORKFLOW_STEP]: "ai_workflow_step",
206
+ [SpanType.TOOL_CALL]: "ai_tool_call",
207
+ [SpanType.MODEL_GENERATION]: "ai_generation",
208
+ [SpanType.EMBEDDING]: "ai_embedding",
209
+ [SpanType.RETRIEVAL]: "ai_retrieval",
210
+ [SpanType.MEMORY]: "ai_memory_operation",
211
+ [SpanType.CONTEXT_COMPACTION]: "ai_context_compaction",
212
+ [SpanType.RAG]: "ai_rag_operation",
213
+ [SpanType.EVALUATION]: "ai_evaluation",
214
+ [SpanType.MCP_TRANSPORT]: "ai_mcp_transport",
215
+ [SpanType.MEDIA_GENERATION]: "ai_media_generation",
216
+ [SpanType.PPT_GENERATION]: "ai_ppt_generation",
217
+ [SpanType.WORKFLOW]: "ai_workflow",
218
+ [SpanType.TTS]: "ai_tts_synthesis",
219
+ [SpanType.SERVER_REQUEST]: "ai_server_request",
220
+ [SpanType.CUSTOM]: "ai_custom_span",
221
+ };
222
+ return eventNameMap[span.type] || "ai_span";
223
+ }
224
+ /**
225
+ * Convert span status to string
226
+ */
227
+ getStatusString(status) {
228
+ switch (status) {
229
+ case SpanStatus.OK:
230
+ return "ok";
231
+ case SpanStatus.ERROR:
232
+ return "error";
233
+ default:
234
+ return "unset";
235
+ }
236
+ }
237
+ /**
238
+ * Extract custom properties from span attributes
239
+ * Filters out standard attributes that are already handled
240
+ */
241
+ extractCustomProperties(attributes) {
242
+ const standardKeys = new Set([
243
+ "service.name",
244
+ "service.version",
245
+ "deployment.environment",
246
+ "user.id",
247
+ "session.id",
248
+ "ai.provider",
249
+ "ai.model",
250
+ "ai.model.version",
251
+ "ai.tokens.input",
252
+ "ai.tokens.output",
253
+ "ai.tokens.total",
254
+ "ai.tokens.cache_read",
255
+ "ai.tokens.cache_creation",
256
+ "ai.tokens.reasoning",
257
+ "ai.cost.input",
258
+ "ai.cost.output",
259
+ "ai.cost.total",
260
+ "ai.cost.currency",
261
+ "ai.temperature",
262
+ "ai.max_tokens",
263
+ "ai.top_p",
264
+ "ai.stop_sequences",
265
+ "tool.name",
266
+ "tool.server",
267
+ "tool.success",
268
+ "error.type",
269
+ "error.message",
270
+ "error.stack",
271
+ "error",
272
+ "input",
273
+ "output",
274
+ "expected",
275
+ "scores",
276
+ ]);
277
+ const custom = {};
278
+ for (const [key, value] of Object.entries(attributes)) {
279
+ if (!standardKeys.has(key) && value !== undefined) {
280
+ // PostHog recommends snake_case for property names
281
+ const snakeCaseKey = key.replace(/\./g, "_").replace(/-/g, "_");
282
+ custom[snakeCaseKey] = value;
283
+ }
284
+ }
285
+ return custom;
286
+ }
287
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Sentry Exporter
3
+ * Exports spans to Sentry error tracking and performance platform
4
+ */
5
+ import type { ExporterHealthStatus, ExportResult, SentryExporterConfig, SpanData } from "../types/index.js";
6
+ import { BaseExporter } from "./baseExporter.js";
7
+ /**
8
+ * Sentry exporter for error tracking and performance monitoring
9
+ * Captures AI errors as exceptions and traces as transactions
10
+ */
11
+ export declare class SentryExporter extends BaseExporter {
12
+ private readonly dsn;
13
+ private readonly tracesSampleRate;
14
+ private readonly release?;
15
+ private sentryHub;
16
+ constructor(config: SentryExporterConfig);
17
+ initialize(): Promise<void>;
18
+ /**
19
+ * Load Sentry SDK dynamically as an optional dependency
20
+ * @returns Sentry module or null if not installed
21
+ */
22
+ private loadSentry;
23
+ exportSpan(span: SpanData): Promise<ExportResult>;
24
+ exportBatch(spans: SpanData[]): Promise<ExportResult>;
25
+ flush(): Promise<void>;
26
+ shutdown(): Promise<void>;
27
+ healthCheck(): Promise<ExporterHealthStatus>;
28
+ /**
29
+ * Verify Sentry SDK is functional
30
+ */
31
+ protected ping(): Promise<void>;
32
+ }