@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.
- package/CHANGELOG.md +6 -0
- package/dist/adapters/tts/googleTTSHandler.js +26 -1
- package/dist/adapters/video/vertexVideoHandler.js +23 -17
- package/dist/cli/commands/config.d.ts +3 -3
- package/dist/cli/commands/observability.d.ts +53 -0
- package/dist/cli/commands/observability.js +453 -0
- package/dist/cli/commands/telemetry.d.ts +63 -0
- package/dist/cli/commands/telemetry.js +689 -0
- package/dist/cli/factories/commandFactory.js +29 -15
- package/dist/cli/parser.js +6 -9
- package/dist/cli/utils/formatters.d.ts +13 -0
- package/dist/cli/utils/formatters.js +23 -0
- package/dist/constants/contextWindows.js +6 -0
- package/dist/constants/enums.d.ts +6 -0
- package/dist/constants/enums.js +8 -2
- package/dist/context/budgetChecker.js +75 -48
- package/dist/context/contextCompactor.js +135 -127
- package/dist/core/baseProvider.d.ts +5 -0
- package/dist/core/baseProvider.js +158 -102
- package/dist/core/conversationMemoryInitializer.js +7 -4
- package/dist/core/conversationMemoryManager.d.ts +2 -0
- package/dist/core/conversationMemoryManager.js +6 -2
- package/dist/core/modules/GenerationHandler.d.ts +2 -2
- package/dist/core/modules/GenerationHandler.js +12 -12
- package/dist/evaluation/ragasEvaluator.js +39 -19
- package/dist/evaluation/scoring.js +46 -20
- package/dist/features/ppt/presentationOrchestrator.js +23 -0
- package/dist/features/ppt/slideGenerator.js +13 -0
- package/dist/features/ppt/slideRenderers.d.ts +1 -1
- package/dist/features/ppt/slideRenderers.js +6 -4
- package/dist/features/ppt/slideTypeInference.d.ts +1 -1
- package/dist/features/ppt/slideTypeInference.js +75 -73
- package/dist/files/fileTools.d.ts +6 -6
- package/dist/index.d.ts +46 -12
- package/dist/index.js +79 -17
- package/dist/lib/adapters/tts/googleTTSHandler.js +26 -1
- package/dist/lib/adapters/video/vertexVideoHandler.js +23 -17
- package/dist/lib/constants/contextWindows.js +6 -0
- package/dist/lib/constants/enums.d.ts +6 -0
- package/dist/lib/constants/enums.js +8 -2
- package/dist/lib/context/budgetChecker.js +75 -48
- package/dist/lib/context/contextCompactor.js +135 -127
- package/dist/lib/core/baseProvider.d.ts +5 -0
- package/dist/lib/core/baseProvider.js +158 -102
- package/dist/lib/core/conversationMemoryInitializer.js +7 -4
- package/dist/lib/core/conversationMemoryManager.d.ts +2 -0
- package/dist/lib/core/conversationMemoryManager.js +6 -2
- package/dist/lib/core/modules/GenerationHandler.d.ts +2 -2
- package/dist/lib/core/modules/GenerationHandler.js +12 -12
- package/dist/lib/evaluation/ragasEvaluator.js +39 -19
- package/dist/lib/evaluation/scoring.js +46 -20
- package/dist/lib/features/ppt/presentationOrchestrator.js +23 -0
- package/dist/lib/features/ppt/slideGenerator.js +13 -0
- package/dist/lib/features/ppt/slideRenderers.d.ts +1 -1
- package/dist/lib/features/ppt/slideRenderers.js +6 -4
- package/dist/lib/features/ppt/slideTypeInference.d.ts +1 -1
- package/dist/lib/features/ppt/slideTypeInference.js +75 -73
- package/dist/lib/files/fileTools.d.ts +6 -6
- package/dist/lib/index.d.ts +46 -12
- package/dist/lib/index.js +79 -17
- package/dist/lib/mcp/httpRateLimiter.js +39 -12
- package/dist/lib/mcp/httpRetryHandler.js +22 -1
- package/dist/lib/mcp/mcpClientFactory.js +13 -15
- package/dist/lib/memory/memoryRetrievalTools.js +22 -0
- package/dist/lib/neurolink.d.ts +64 -72
- package/dist/lib/neurolink.js +984 -566
- package/dist/lib/observability/exporterRegistry.d.ts +152 -0
- package/dist/lib/observability/exporterRegistry.js +414 -0
- package/dist/lib/observability/exporters/arizeExporter.d.ts +32 -0
- package/dist/lib/observability/exporters/arizeExporter.js +139 -0
- package/dist/lib/observability/exporters/baseExporter.d.ts +117 -0
- package/dist/lib/observability/exporters/baseExporter.js +191 -0
- package/dist/lib/observability/exporters/braintrustExporter.d.ts +30 -0
- package/dist/lib/observability/exporters/braintrustExporter.js +155 -0
- package/dist/lib/observability/exporters/datadogExporter.d.ts +37 -0
- package/dist/lib/observability/exporters/datadogExporter.js +197 -0
- package/dist/lib/observability/exporters/index.d.ts +13 -0
- package/dist/lib/observability/exporters/index.js +14 -0
- package/dist/lib/observability/exporters/laminarExporter.d.ts +48 -0
- package/dist/lib/observability/exporters/laminarExporter.js +303 -0
- package/dist/lib/observability/exporters/langfuseExporter.d.ts +47 -0
- package/dist/lib/observability/exporters/langfuseExporter.js +200 -0
- package/dist/lib/observability/exporters/langsmithExporter.d.ts +26 -0
- package/dist/lib/observability/exporters/langsmithExporter.js +124 -0
- package/dist/lib/observability/exporters/otelExporter.d.ts +39 -0
- package/dist/lib/observability/exporters/otelExporter.js +165 -0
- package/dist/lib/observability/exporters/posthogExporter.d.ts +48 -0
- package/dist/lib/observability/exporters/posthogExporter.js +288 -0
- package/dist/lib/observability/exporters/sentryExporter.d.ts +32 -0
- package/dist/lib/observability/exporters/sentryExporter.js +166 -0
- package/dist/lib/observability/index.d.ts +25 -0
- package/dist/lib/observability/index.js +32 -0
- package/dist/lib/observability/metricsAggregator.d.ts +260 -0
- package/dist/lib/observability/metricsAggregator.js +553 -0
- package/dist/lib/observability/otelBridge.d.ts +49 -0
- package/dist/lib/observability/otelBridge.js +132 -0
- package/dist/lib/observability/retryPolicy.d.ts +192 -0
- package/dist/lib/observability/retryPolicy.js +384 -0
- package/dist/lib/observability/sampling/index.d.ts +4 -0
- package/dist/lib/observability/sampling/index.js +5 -0
- package/dist/lib/observability/sampling/samplers.d.ts +116 -0
- package/dist/lib/observability/sampling/samplers.js +217 -0
- package/dist/lib/observability/spanProcessor.d.ts +129 -0
- package/dist/lib/observability/spanProcessor.js +288 -0
- package/dist/lib/observability/tokenTracker.d.ts +156 -0
- package/dist/lib/observability/tokenTracker.js +414 -0
- package/dist/lib/observability/types/exporterTypes.d.ts +250 -0
- package/dist/lib/observability/types/exporterTypes.js +6 -0
- package/dist/lib/observability/types/index.d.ts +6 -0
- package/dist/lib/observability/types/index.js +5 -0
- package/dist/lib/observability/types/spanTypes.d.ts +244 -0
- package/dist/lib/observability/types/spanTypes.js +93 -0
- package/dist/lib/observability/utils/index.d.ts +4 -0
- package/dist/lib/observability/utils/index.js +5 -0
- package/dist/lib/observability/utils/spanSerializer.d.ts +115 -0
- package/dist/lib/observability/utils/spanSerializer.js +287 -0
- package/dist/lib/providers/amazonSagemaker.d.ts +5 -4
- package/dist/lib/providers/amazonSagemaker.js +3 -4
- package/dist/lib/providers/googleVertex.d.ts +7 -0
- package/dist/lib/providers/googleVertex.js +80 -2
- package/dist/lib/rag/pipeline/RAGPipeline.d.ts +0 -5
- package/dist/lib/rag/pipeline/RAGPipeline.js +122 -87
- package/dist/lib/rag/ragIntegration.js +30 -0
- package/dist/lib/rag/retrieval/hybridSearch.js +22 -0
- package/dist/lib/server/abstract/baseServerAdapter.js +51 -19
- package/dist/lib/server/middleware/common.js +44 -12
- package/dist/lib/services/server/ai/observability/instrumentation.d.ts +2 -2
- package/dist/lib/services/server/ai/observability/instrumentation.js +10 -5
- package/dist/lib/types/conversationMemoryInterface.d.ts +2 -0
- package/dist/lib/types/modelTypes.d.ts +18 -18
- package/dist/lib/types/providers.d.ts +5 -0
- package/dist/lib/utils/pricing.js +25 -1
- package/dist/lib/utils/ttsProcessor.js +74 -59
- package/dist/lib/workflow/config.d.ts +36 -36
- package/dist/lib/workflow/core/ensembleExecutor.js +10 -0
- package/dist/lib/workflow/core/judgeScorer.js +20 -2
- package/dist/lib/workflow/core/workflowRunner.js +34 -1
- package/dist/mcp/httpRateLimiter.js +39 -12
- package/dist/mcp/httpRetryHandler.js +22 -1
- package/dist/mcp/mcpClientFactory.js +13 -15
- package/dist/memory/memoryRetrievalTools.js +22 -0
- package/dist/neurolink.d.ts +64 -72
- package/dist/neurolink.js +984 -566
- package/dist/observability/FEATURE-STATUS.md +269 -0
- package/dist/observability/exporterRegistry.d.ts +152 -0
- package/dist/observability/exporterRegistry.js +413 -0
- package/dist/observability/exporters/arizeExporter.d.ts +32 -0
- package/dist/observability/exporters/arizeExporter.js +138 -0
- package/dist/observability/exporters/baseExporter.d.ts +117 -0
- package/dist/observability/exporters/baseExporter.js +190 -0
- package/dist/observability/exporters/braintrustExporter.d.ts +30 -0
- package/dist/observability/exporters/braintrustExporter.js +154 -0
- package/dist/observability/exporters/datadogExporter.d.ts +37 -0
- package/dist/observability/exporters/datadogExporter.js +196 -0
- package/dist/observability/exporters/index.d.ts +13 -0
- package/dist/observability/exporters/index.js +13 -0
- package/dist/observability/exporters/laminarExporter.d.ts +48 -0
- package/dist/observability/exporters/laminarExporter.js +302 -0
- package/dist/observability/exporters/langfuseExporter.d.ts +47 -0
- package/dist/observability/exporters/langfuseExporter.js +199 -0
- package/dist/observability/exporters/langsmithExporter.d.ts +26 -0
- package/dist/observability/exporters/langsmithExporter.js +123 -0
- package/dist/observability/exporters/otelExporter.d.ts +39 -0
- package/dist/observability/exporters/otelExporter.js +164 -0
- package/dist/observability/exporters/posthogExporter.d.ts +48 -0
- package/dist/observability/exporters/posthogExporter.js +287 -0
- package/dist/observability/exporters/sentryExporter.d.ts +32 -0
- package/dist/observability/exporters/sentryExporter.js +165 -0
- package/dist/observability/index.d.ts +25 -0
- package/dist/observability/index.js +31 -0
- package/dist/observability/metricsAggregator.d.ts +260 -0
- package/dist/observability/metricsAggregator.js +552 -0
- package/dist/observability/otelBridge.d.ts +49 -0
- package/dist/observability/otelBridge.js +131 -0
- package/dist/observability/retryPolicy.d.ts +192 -0
- package/dist/observability/retryPolicy.js +383 -0
- package/dist/observability/sampling/index.d.ts +4 -0
- package/dist/observability/sampling/index.js +4 -0
- package/dist/observability/sampling/samplers.d.ts +116 -0
- package/dist/observability/sampling/samplers.js +216 -0
- package/dist/observability/spanProcessor.d.ts +129 -0
- package/dist/observability/spanProcessor.js +287 -0
- package/dist/observability/tokenTracker.d.ts +156 -0
- package/dist/observability/tokenTracker.js +413 -0
- package/dist/observability/types/exporterTypes.d.ts +250 -0
- package/dist/observability/types/exporterTypes.js +5 -0
- package/dist/observability/types/index.d.ts +6 -0
- package/dist/observability/types/index.js +4 -0
- package/dist/observability/types/spanTypes.d.ts +244 -0
- package/dist/observability/types/spanTypes.js +92 -0
- package/dist/observability/utils/index.d.ts +4 -0
- package/dist/observability/utils/index.js +4 -0
- package/dist/observability/utils/spanSerializer.d.ts +115 -0
- package/dist/observability/utils/spanSerializer.js +286 -0
- package/dist/providers/amazonSagemaker.d.ts +5 -4
- package/dist/providers/amazonSagemaker.js +3 -4
- package/dist/providers/googleVertex.d.ts +7 -0
- package/dist/providers/googleVertex.js +80 -2
- package/dist/rag/pipeline/RAGPipeline.d.ts +0 -5
- package/dist/rag/pipeline/RAGPipeline.js +122 -87
- package/dist/rag/ragIntegration.js +30 -0
- package/dist/rag/retrieval/hybridSearch.js +22 -0
- package/dist/server/abstract/baseServerAdapter.js +51 -19
- package/dist/server/middleware/common.js +44 -12
- package/dist/services/server/ai/observability/instrumentation.d.ts +2 -2
- package/dist/services/server/ai/observability/instrumentation.js +10 -5
- package/dist/types/conversationMemoryInterface.d.ts +2 -0
- package/dist/types/providers.d.ts +5 -0
- package/dist/utils/pricing.js +25 -1
- package/dist/utils/ttsProcessor.js +74 -59
- package/dist/workflow/config.d.ts +52 -52
- package/dist/workflow/core/ensembleExecutor.js +10 -0
- package/dist/workflow/core/judgeScorer.js +20 -2
- package/dist/workflow/core/workflowRunner.js +34 -1
- package/package.json +1 -1
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Span Processor
|
|
3
|
+
* Handles span processing before export - enrichment, filtering, and transformation
|
|
4
|
+
* Fills the 9% gap in pattern compliance
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* No-op processor that passes spans through unchanged
|
|
8
|
+
*/
|
|
9
|
+
export class PassThroughProcessor {
|
|
10
|
+
name = "pass-through";
|
|
11
|
+
process(span) {
|
|
12
|
+
return span;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Attribute enrichment processor
|
|
17
|
+
* Adds additional attributes to spans based on configuration
|
|
18
|
+
*/
|
|
19
|
+
export class AttributeEnrichmentProcessor {
|
|
20
|
+
name = "attribute-enrichment";
|
|
21
|
+
staticAttributes;
|
|
22
|
+
dynamicAttributes;
|
|
23
|
+
constructor(config) {
|
|
24
|
+
this.staticAttributes = config.staticAttributes ?? {};
|
|
25
|
+
this.dynamicAttributes = config.dynamicAttributes ?? (() => ({}));
|
|
26
|
+
}
|
|
27
|
+
process(span) {
|
|
28
|
+
return {
|
|
29
|
+
...span,
|
|
30
|
+
attributes: {
|
|
31
|
+
...this.staticAttributes,
|
|
32
|
+
...span.attributes,
|
|
33
|
+
...this.dynamicAttributes(span),
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Filter processor - drops spans based on conditions
|
|
40
|
+
*/
|
|
41
|
+
export class FilterProcessor {
|
|
42
|
+
name = "filter";
|
|
43
|
+
predicate;
|
|
44
|
+
constructor(predicate) {
|
|
45
|
+
this.predicate = predicate;
|
|
46
|
+
}
|
|
47
|
+
process(span) {
|
|
48
|
+
return this.predicate(span) ? span : null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Redaction processor - removes sensitive data from spans
|
|
53
|
+
*/
|
|
54
|
+
export class RedactionProcessor {
|
|
55
|
+
name = "redaction";
|
|
56
|
+
sensitiveKeys;
|
|
57
|
+
redactedValue;
|
|
58
|
+
constructor(config) {
|
|
59
|
+
this.sensitiveKeys = new Set(config?.sensitiveKeys ?? [
|
|
60
|
+
"api_key",
|
|
61
|
+
"apiKey",
|
|
62
|
+
"secret",
|
|
63
|
+
"password",
|
|
64
|
+
"token",
|
|
65
|
+
"authorization",
|
|
66
|
+
"credentials",
|
|
67
|
+
"private_key",
|
|
68
|
+
"privateKey",
|
|
69
|
+
]);
|
|
70
|
+
this.redactedValue = config?.redactedValue ?? "[REDACTED]";
|
|
71
|
+
}
|
|
72
|
+
process(span) {
|
|
73
|
+
const redactedAttributes = {};
|
|
74
|
+
for (const [key, value] of Object.entries(span.attributes)) {
|
|
75
|
+
const lowerKey = key.toLowerCase();
|
|
76
|
+
const isSensitive = Array.from(this.sensitiveKeys).some((sensitiveKey) => lowerKey.includes(sensitiveKey.toLowerCase()));
|
|
77
|
+
if (isSensitive && typeof value === "string") {
|
|
78
|
+
redactedAttributes[key] = this.redactedValue;
|
|
79
|
+
}
|
|
80
|
+
else if (typeof value === "object" && value !== null) {
|
|
81
|
+
redactedAttributes[key] = this.redactObject(value);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
redactedAttributes[key] = value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
...span,
|
|
89
|
+
attributes: redactedAttributes,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
redactObject(obj) {
|
|
93
|
+
if (typeof obj !== "object" || obj === null) {
|
|
94
|
+
return obj;
|
|
95
|
+
}
|
|
96
|
+
if (Array.isArray(obj)) {
|
|
97
|
+
return obj.map((item) => this.redactObject(item));
|
|
98
|
+
}
|
|
99
|
+
const result = {};
|
|
100
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
101
|
+
const lowerKey = key.toLowerCase();
|
|
102
|
+
const isSensitive = Array.from(this.sensitiveKeys).some((sensitiveKey) => lowerKey.includes(sensitiveKey.toLowerCase()));
|
|
103
|
+
if (isSensitive && typeof value === "string") {
|
|
104
|
+
result[key] = this.redactedValue;
|
|
105
|
+
}
|
|
106
|
+
else if (typeof value === "object" && value !== null) {
|
|
107
|
+
result[key] = this.redactObject(value);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
result[key] = value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Truncation processor - truncates large attribute values
|
|
118
|
+
*/
|
|
119
|
+
export class TruncationProcessor {
|
|
120
|
+
name = "truncation";
|
|
121
|
+
maxStringLength;
|
|
122
|
+
maxArrayLength;
|
|
123
|
+
constructor(config) {
|
|
124
|
+
this.maxStringLength = config?.maxStringLength ?? 10000;
|
|
125
|
+
this.maxArrayLength = config?.maxArrayLength ?? 100;
|
|
126
|
+
}
|
|
127
|
+
process(span) {
|
|
128
|
+
return {
|
|
129
|
+
...span,
|
|
130
|
+
attributes: this.truncateAttributes(span.attributes),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
truncateAttributes(attrs) {
|
|
134
|
+
const result = {};
|
|
135
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
136
|
+
result[key] = this.truncateValue(value);
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
truncateValue(value) {
|
|
141
|
+
if (typeof value === "string" && value.length > this.maxStringLength) {
|
|
142
|
+
return value.substring(0, this.maxStringLength) + "...[truncated]";
|
|
143
|
+
}
|
|
144
|
+
if (Array.isArray(value) && value.length > this.maxArrayLength) {
|
|
145
|
+
return [
|
|
146
|
+
...value.slice(0, this.maxArrayLength),
|
|
147
|
+
`...[${value.length - this.maxArrayLength} more items]`,
|
|
148
|
+
];
|
|
149
|
+
}
|
|
150
|
+
if (typeof value === "object" && value !== null) {
|
|
151
|
+
const result = {};
|
|
152
|
+
for (const [k, v] of Object.entries(value)) {
|
|
153
|
+
result[k] = this.truncateValue(v);
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Composite processor - chains multiple processors together
|
|
162
|
+
*/
|
|
163
|
+
export class CompositeProcessor {
|
|
164
|
+
name = "composite";
|
|
165
|
+
processors;
|
|
166
|
+
constructor(processors) {
|
|
167
|
+
this.processors = processors;
|
|
168
|
+
}
|
|
169
|
+
process(span) {
|
|
170
|
+
let result = span;
|
|
171
|
+
for (const processor of this.processors) {
|
|
172
|
+
if (result === null) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
result = processor.process(result);
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
async processAsync(span) {
|
|
180
|
+
let result = span;
|
|
181
|
+
for (const processor of this.processors) {
|
|
182
|
+
if (result === null) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
if (processor.processAsync) {
|
|
186
|
+
result = await processor.processAsync(result);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
result = processor.process(result);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
async shutdown() {
|
|
195
|
+
await Promise.all(this.processors.map((p) => p.shutdown ? p.shutdown() : Promise.resolve()));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Batch processor - collects spans and processes them in batches
|
|
200
|
+
*/
|
|
201
|
+
export class BatchProcessor {
|
|
202
|
+
name = "batch";
|
|
203
|
+
innerProcessor;
|
|
204
|
+
batchSize;
|
|
205
|
+
flushIntervalMs;
|
|
206
|
+
batch = [];
|
|
207
|
+
flushTimer = null;
|
|
208
|
+
onBatchReady;
|
|
209
|
+
constructor(config) {
|
|
210
|
+
this.innerProcessor = config.processor ?? new PassThroughProcessor();
|
|
211
|
+
this.batchSize = config.batchSize ?? 100;
|
|
212
|
+
this.flushIntervalMs = config.flushIntervalMs ?? 5000;
|
|
213
|
+
this.onBatchReady = config.onBatchReady;
|
|
214
|
+
this.startFlushTimer();
|
|
215
|
+
}
|
|
216
|
+
process(span) {
|
|
217
|
+
const processed = this.innerProcessor.process(span);
|
|
218
|
+
if (processed) {
|
|
219
|
+
this.batch.push(processed);
|
|
220
|
+
if (this.batch.length >= this.batchSize) {
|
|
221
|
+
this.flush();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return processed;
|
|
225
|
+
}
|
|
226
|
+
startFlushTimer() {
|
|
227
|
+
this.flushTimer = setInterval(() => {
|
|
228
|
+
this.flush();
|
|
229
|
+
}, this.flushIntervalMs);
|
|
230
|
+
}
|
|
231
|
+
flush() {
|
|
232
|
+
if (this.batch.length > 0 && this.onBatchReady) {
|
|
233
|
+
const spans = [...this.batch];
|
|
234
|
+
this.batch = [];
|
|
235
|
+
this.onBatchReady(spans);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
async shutdown() {
|
|
239
|
+
if (this.flushTimer) {
|
|
240
|
+
clearInterval(this.flushTimer);
|
|
241
|
+
this.flushTimer = null;
|
|
242
|
+
}
|
|
243
|
+
this.flush();
|
|
244
|
+
if (this.innerProcessor.shutdown) {
|
|
245
|
+
await this.innerProcessor.shutdown();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Factory for creating span processors
|
|
251
|
+
*/
|
|
252
|
+
export class SpanProcessorFactory {
|
|
253
|
+
/**
|
|
254
|
+
* Create a standard processor pipeline for production use
|
|
255
|
+
*/
|
|
256
|
+
static createProductionPipeline(config) {
|
|
257
|
+
const processors = [
|
|
258
|
+
// Add service context
|
|
259
|
+
new AttributeEnrichmentProcessor({
|
|
260
|
+
staticAttributes: {
|
|
261
|
+
"service.name": config?.serviceName ?? "neurolink",
|
|
262
|
+
"deployment.environment": config?.environment ?? "production",
|
|
263
|
+
},
|
|
264
|
+
}),
|
|
265
|
+
// Redact sensitive data
|
|
266
|
+
new RedactionProcessor(),
|
|
267
|
+
// Truncate large values
|
|
268
|
+
new TruncationProcessor(),
|
|
269
|
+
];
|
|
270
|
+
if (config?.additionalProcessors) {
|
|
271
|
+
processors.push(...config.additionalProcessors);
|
|
272
|
+
}
|
|
273
|
+
return new CompositeProcessor(processors);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Create a minimal processor pipeline for development
|
|
277
|
+
*/
|
|
278
|
+
static createDevelopmentPipeline() {
|
|
279
|
+
return new CompositeProcessor([
|
|
280
|
+
new AttributeEnrichmentProcessor({
|
|
281
|
+
staticAttributes: {
|
|
282
|
+
"deployment.environment": "development",
|
|
283
|
+
},
|
|
284
|
+
}),
|
|
285
|
+
]);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
//# sourceMappingURL=spanProcessor.js.map
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token Usage Tracker
|
|
3
|
+
* Aggregates token usage and cost across spans
|
|
4
|
+
*/
|
|
5
|
+
import type { SpanData } from "./types/spanTypes.js";
|
|
6
|
+
/**
|
|
7
|
+
* Token usage statistics by provider
|
|
8
|
+
*/
|
|
9
|
+
export type ProviderTokenStats = {
|
|
10
|
+
provider: string;
|
|
11
|
+
inputTokens: number;
|
|
12
|
+
outputTokens: number;
|
|
13
|
+
totalTokens: number;
|
|
14
|
+
cost: number;
|
|
15
|
+
requestCount: number;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Token usage statistics by model
|
|
19
|
+
*/
|
|
20
|
+
export type ModelTokenStats = {
|
|
21
|
+
model: string;
|
|
22
|
+
provider: string;
|
|
23
|
+
inputTokens: number;
|
|
24
|
+
outputTokens: number;
|
|
25
|
+
totalTokens: number;
|
|
26
|
+
cost: number;
|
|
27
|
+
requestCount: number;
|
|
28
|
+
avgTokensPerRequest: number;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Aggregated token usage statistics
|
|
32
|
+
*/
|
|
33
|
+
export type TokenUsageStats = {
|
|
34
|
+
totalInputTokens: number;
|
|
35
|
+
totalOutputTokens: number;
|
|
36
|
+
totalTokens: number;
|
|
37
|
+
cacheReadTokens: number;
|
|
38
|
+
cacheCreationTokens: number;
|
|
39
|
+
reasoningTokens: number;
|
|
40
|
+
totalCost: number;
|
|
41
|
+
byProvider: Map<string, ProviderTokenStats>;
|
|
42
|
+
byModel: Map<string, ModelTokenStats>;
|
|
43
|
+
bySpanType: Map<string, number>;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Model pricing information
|
|
47
|
+
*/
|
|
48
|
+
export type ModelPricing = {
|
|
49
|
+
inputPricePerMillion: number;
|
|
50
|
+
outputPricePerMillion: number;
|
|
51
|
+
cachedInputPricePerMillion?: number;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Token tracker for aggregating usage across spans
|
|
55
|
+
*/
|
|
56
|
+
export declare class TokenTracker {
|
|
57
|
+
private stats;
|
|
58
|
+
private customPricing;
|
|
59
|
+
/**
|
|
60
|
+
* Set custom pricing for a single model
|
|
61
|
+
* @param modelName - The model name (e.g., "gpt-4o", "claude-3-5-sonnet")
|
|
62
|
+
* @param pricing - The pricing information
|
|
63
|
+
*/
|
|
64
|
+
setModelPricing(modelName: string, pricing: ModelPricing): void;
|
|
65
|
+
/**
|
|
66
|
+
* Update pricing for an existing model (alias for setModelPricing)
|
|
67
|
+
* @param model - The model name
|
|
68
|
+
* @param pricing - The new pricing information
|
|
69
|
+
*/
|
|
70
|
+
updatePricing(model: string, pricing: ModelPricing): void;
|
|
71
|
+
/**
|
|
72
|
+
* Load pricing configuration from a config object
|
|
73
|
+
* Useful for loading pricing from environment or config files
|
|
74
|
+
* @param config - Record of model names to pricing information
|
|
75
|
+
*/
|
|
76
|
+
loadPricingFromConfig(config: Record<string, ModelPricing>): void;
|
|
77
|
+
/**
|
|
78
|
+
* Get pricing for a specific model
|
|
79
|
+
* @param model - The model name
|
|
80
|
+
* @returns The pricing information or undefined if not found
|
|
81
|
+
*/
|
|
82
|
+
getModelPricing(model: string): ModelPricing | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Get all available model pricing (custom + built-in)
|
|
85
|
+
* @returns Record of all model pricing
|
|
86
|
+
*/
|
|
87
|
+
getAllPricing(): Record<string, ModelPricing>;
|
|
88
|
+
/**
|
|
89
|
+
* Remove custom pricing for a model (falls back to built-in)
|
|
90
|
+
* @param model - The model name to remove custom pricing for
|
|
91
|
+
*/
|
|
92
|
+
removeCustomPricing(model: string): boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Track token usage from a span
|
|
95
|
+
*/
|
|
96
|
+
trackSpan(span: SpanData): void;
|
|
97
|
+
/**
|
|
98
|
+
* Calculate cost from token counts and provider/model
|
|
99
|
+
*/
|
|
100
|
+
private calculateCost;
|
|
101
|
+
/**
|
|
102
|
+
* Track token usage from a simple usage object
|
|
103
|
+
* This is a convenience method for tracking usage without a full span
|
|
104
|
+
* @param usage - Token usage data
|
|
105
|
+
*/
|
|
106
|
+
trackUsage(usage: {
|
|
107
|
+
promptTokens?: number;
|
|
108
|
+
completionTokens?: number;
|
|
109
|
+
totalTokens?: number;
|
|
110
|
+
model?: string;
|
|
111
|
+
provider?: string;
|
|
112
|
+
}): void;
|
|
113
|
+
/**
|
|
114
|
+
* Get current stats
|
|
115
|
+
*/
|
|
116
|
+
getStats(): TokenUsageStats;
|
|
117
|
+
/**
|
|
118
|
+
* Get stats for a specific time window of spans
|
|
119
|
+
*/
|
|
120
|
+
getStatsForWindow(spans: SpanData[]): TokenUsageStats;
|
|
121
|
+
/**
|
|
122
|
+
* Reset all stats
|
|
123
|
+
*/
|
|
124
|
+
reset(): void;
|
|
125
|
+
/**
|
|
126
|
+
* Export stats as JSON
|
|
127
|
+
*/
|
|
128
|
+
toJSON(): Record<string, unknown>;
|
|
129
|
+
/**
|
|
130
|
+
* Format cost as currency string
|
|
131
|
+
*/
|
|
132
|
+
formatCost(cost: number, currency?: string): string;
|
|
133
|
+
/**
|
|
134
|
+
* Get a summary string of current stats
|
|
135
|
+
*/
|
|
136
|
+
getSummary(): string;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Enrich span with token usage attributes
|
|
140
|
+
*/
|
|
141
|
+
export declare function enrichSpanWithTokenUsage(span: SpanData, usage: {
|
|
142
|
+
promptTokens?: number;
|
|
143
|
+
completionTokens?: number;
|
|
144
|
+
totalTokens?: number;
|
|
145
|
+
cacheCreationTokens?: number;
|
|
146
|
+
cacheReadTokens?: number;
|
|
147
|
+
reasoningTokens?: number;
|
|
148
|
+
}): SpanData;
|
|
149
|
+
/**
|
|
150
|
+
* Get the global token tracker instance
|
|
151
|
+
*/
|
|
152
|
+
export declare function getTokenTracker(): TokenTracker;
|
|
153
|
+
/**
|
|
154
|
+
* Reset the global token tracker (for testing)
|
|
155
|
+
*/
|
|
156
|
+
export declare function resetTokenTracker(): void;
|