@juspay/neurolink 9.94.7 → 9.95.1

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 (38) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/analytics/index.d.ts +7 -0
  3. package/dist/analytics/index.js +7 -0
  4. package/dist/analytics/parseQualityScore.d.ts +9 -0
  5. package/dist/analytics/parseQualityScore.js +29 -0
  6. package/dist/analytics/pricing.d.ts +24 -0
  7. package/dist/analytics/pricing.js +37 -0
  8. package/dist/analytics/service.d.ts +36 -0
  9. package/dist/analytics/service.js +393 -0
  10. package/dist/analytics/storage.d.ts +19 -0
  11. package/dist/analytics/storage.js +32 -0
  12. package/dist/browser/neurolink.min.js +389 -389
  13. package/dist/cli/factories/commandFactory.js +88 -13
  14. package/dist/cli/utils/inputValidation.d.ts +22 -9
  15. package/dist/cli/utils/inputValidation.js +147 -61
  16. package/dist/lib/analytics/index.d.ts +7 -0
  17. package/dist/lib/analytics/index.js +8 -0
  18. package/dist/lib/analytics/parseQualityScore.d.ts +9 -0
  19. package/dist/lib/analytics/parseQualityScore.js +30 -0
  20. package/dist/lib/analytics/pricing.d.ts +24 -0
  21. package/dist/lib/analytics/pricing.js +38 -0
  22. package/dist/lib/analytics/service.d.ts +36 -0
  23. package/dist/lib/analytics/service.js +394 -0
  24. package/dist/lib/analytics/storage.d.ts +19 -0
  25. package/dist/lib/analytics/storage.js +33 -0
  26. package/dist/lib/neurolink.d.ts +54 -0
  27. package/dist/lib/neurolink.js +302 -204
  28. package/dist/lib/types/analytics.d.ts +127 -0
  29. package/dist/lib/types/cli.d.ts +7 -2
  30. package/dist/lib/utils/logger.d.ts +12 -0
  31. package/dist/lib/utils/logger.js +16 -0
  32. package/dist/neurolink.d.ts +54 -0
  33. package/dist/neurolink.js +302 -204
  34. package/dist/types/analytics.d.ts +127 -0
  35. package/dist/types/cli.d.ts +7 -2
  36. package/dist/utils/logger.d.ts +12 -0
  37. package/dist/utils/logger.js +16 -0
  38. package/package.json +2 -1
@@ -78,3 +78,130 @@ export type PerformanceMetrics = {
78
78
  external: number;
79
79
  };
80
80
  };
81
+ export type TimeRangeOption = {
82
+ start: Date;
83
+ end: Date;
84
+ };
85
+ /**
86
+ * Quality score subset captured on an analytics telemetry record.
87
+ */
88
+ export type AnalyticsQualityScore = {
89
+ overall: number;
90
+ relevance: number;
91
+ accuracy: number;
92
+ completeness: number;
93
+ reasoning?: string;
94
+ };
95
+ /**
96
+ * Single request lifecycle record for advanced analytics aggregation.
97
+ */
98
+ export type TelemetryRecord = {
99
+ id: string;
100
+ provider: string;
101
+ model: string;
102
+ userId?: string;
103
+ teamId?: string;
104
+ department?: string;
105
+ timestamp: number;
106
+ latency: number;
107
+ inputTokens: number;
108
+ outputTokens: number;
109
+ totalTokens: number;
110
+ cost: number;
111
+ isError: boolean;
112
+ errorMessage?: string;
113
+ qualityScore?: AnalyticsQualityScore;
114
+ };
115
+ /**
116
+ * Pluggable storage backend for analytics telemetry records.
117
+ */
118
+ export type AnalyticsStorage = {
119
+ /** Save a telemetry record */
120
+ saveRecord(record: TelemetryRecord): Promise<void>;
121
+ /** Retrieve all records */
122
+ getRecords(): Promise<TelemetryRecord[]>;
123
+ /** Clear storage */
124
+ clear(): Promise<void>;
125
+ };
126
+ /**
127
+ * Options for the bounded in-memory analytics storage backend.
128
+ */
129
+ export type InMemoryAnalyticsStorageOptions = {
130
+ /** Maximum records to retain. Oldest records are evicted when exceeded. */
131
+ maxRecords?: number;
132
+ };
133
+ export type ProviderMetricsOptions = {
134
+ providers?: string[];
135
+ timeRange?: TimeRangeOption | string;
136
+ metrics?: string[];
137
+ };
138
+ export type ProviderMetricItem = {
139
+ name: string;
140
+ averageLatency: number;
141
+ averageResponseTime: number;
142
+ totalTokens: number;
143
+ inputTokens: number;
144
+ outputTokens: number;
145
+ errorRate: number;
146
+ successRate: number;
147
+ costPerToken: number;
148
+ totalCost: number;
149
+ requestCount: number;
150
+ };
151
+ export type ProviderMetricsResult = {
152
+ averageLatency: number;
153
+ averageResponseTime: number;
154
+ totalTokens: number;
155
+ inputTokens: number;
156
+ outputTokens: number;
157
+ errorRate: number;
158
+ successRate: number;
159
+ costPerToken: number;
160
+ totalCost: number;
161
+ requestCount: number;
162
+ providers: ProviderMetricItem[];
163
+ };
164
+ export type CostAnalysisOptions = {
165
+ timeRange?: TimeRangeOption | string;
166
+ groupBy?: string | string[];
167
+ includeProjections?: boolean;
168
+ };
169
+ export type CostGroupItem = {
170
+ groupKey: string;
171
+ provider?: string;
172
+ model?: string;
173
+ userId?: string;
174
+ totalCost: number;
175
+ costPerToken: number;
176
+ inputTokens: number;
177
+ outputTokens: number;
178
+ totalTokens: number;
179
+ requestCount: number;
180
+ };
181
+ export type CostAnalysisResult = {
182
+ totalCost: number;
183
+ totalTokens: number;
184
+ inputTokens: number;
185
+ outputTokens: number;
186
+ requestCount: number;
187
+ groups: Record<string, CostGroupItem>;
188
+ providers: CostGroupItem[];
189
+ projections?: {
190
+ nextMonth: number;
191
+ nextQuarter: number;
192
+ };
193
+ };
194
+ export type TeamAnalyticsOptions = {
195
+ teamId?: string;
196
+ departments?: string[];
197
+ metrics?: string[];
198
+ timeRange?: TimeRangeOption | string;
199
+ };
200
+ export type TeamAnalyticsResult = {
201
+ totalRequests: number;
202
+ uniqueUsers: number;
203
+ providersUsed: string[];
204
+ costBreakdownByProvider: Record<string, number>;
205
+ costBreakdownByUser: Record<string, number>;
206
+ qualityScores?: AnalyticsQualityScore;
207
+ };
@@ -138,8 +138,8 @@ export type StreamCommandArgs = BaseCommandArgs & CliToolRoutingFlags & CliClass
138
138
  * Batch command arguments
139
139
  */
140
140
  export type BatchCommandArgs = BaseCommandArgs & CliToolRoutingFlags & CliClassifierRouterFlags & {
141
- /** Input file path */
142
- file?: string;
141
+ /** Prompts-list file path (the `<promptsFile>` positional) */
142
+ promptsFile?: string;
143
143
  /** AI provider to use */
144
144
  provider?: string;
145
145
  /** Model name */
@@ -1684,3 +1684,8 @@ export type CliAudioPlayerCommand = {
1684
1684
  command: string;
1685
1685
  args: string[];
1686
1686
  };
1687
+ /**
1688
+ * The CLI flags validated by `validateCliInputFiles` before a
1689
+ * generate/stream/batch run starts (--image/--csv/--pdf/--video/--file).
1690
+ */
1691
+ export type CliValidatedFileOption = "--image" | "--csv" | "--pdf" | "--video" | "--file";
@@ -162,6 +162,17 @@ declare class NeuroLinkLogger {
162
162
  * @param args - The arguments to log. These are passed directly to `console.log`.
163
163
  */
164
164
  always(...args: unknown[]): void;
165
+ /**
166
+ * Logs messages unconditionally using `console.error` (stderr).
167
+ *
168
+ * Same semantics as `always()` — bypasses log level checks and debug mode
169
+ * gating — but targets stderr instead of stdout. Use this for output that
170
+ * must stay visible (safety warnings, notices) without risking corruption
171
+ * of machine-readable stdout (e.g. `--format json`).
172
+ *
173
+ * @param args - The arguments to log. These are passed directly to `console.error`.
174
+ */
175
+ alwaysStderr(...args: unknown[]): void;
165
176
  /**
166
177
  * Displays tabular data unconditionally using `console.table`.
167
178
  *
@@ -200,6 +211,7 @@ export declare const logger: {
200
211
  warn: (...args: unknown[]) => void;
201
212
  error: (...args: unknown[]) => void;
202
213
  always: (...args: unknown[]) => void;
214
+ alwaysStderr: (...args: unknown[]) => void;
203
215
  table: (data: unknown) => void;
204
216
  shouldLog: (level: LogLevel) => boolean;
205
217
  setLogLevel: (level: LogLevel) => void;
@@ -347,6 +347,19 @@ class NeuroLinkLogger {
347
347
  always(...args) {
348
348
  console.log(...args);
349
349
  }
350
+ /**
351
+ * Logs messages unconditionally using `console.error` (stderr).
352
+ *
353
+ * Same semantics as `always()` — bypasses log level checks and debug mode
354
+ * gating — but targets stderr instead of stdout. Use this for output that
355
+ * must stay visible (safety warnings, notices) without risking corruption
356
+ * of machine-readable stdout (e.g. `--format json`).
357
+ *
358
+ * @param args - The arguments to log. These are passed directly to `console.error`.
359
+ */
360
+ alwaysStderr(...args) {
361
+ console.error(...args);
362
+ }
350
363
  /**
351
364
  * Displays tabular data unconditionally using `console.table`.
352
365
  *
@@ -436,6 +449,9 @@ export const logger = {
436
449
  always: (...args) => {
437
450
  neuroLinkLogger.always(...args);
438
451
  },
452
+ alwaysStderr: (...args) => {
453
+ neuroLinkLogger.alwaysStderr(...args);
454
+ },
439
455
  table: (data) => {
440
456
  neuroLinkLogger.table(data);
441
457
  },
@@ -13,6 +13,7 @@ import { ExternalServerManager } from "./mcp/externalServerManager.js";
13
13
  import { MCPToolRegistry } from "./mcp/toolRegistry.js";
14
14
  import type { DynamicOptions } from "./types/index.js";
15
15
  import { SkillsManager } from "./skills/skillsManager.js";
16
+ import type { CostAnalysisOptions, CostAnalysisResult, ProviderMetricsOptions, ProviderMetricsResult, TeamAnalyticsOptions, TeamAnalyticsResult } from "./types/index.js";
16
17
  import { TaskManager } from "./tasks/taskManager.js";
17
18
  import type { SessionExport, SessionListItem } from "./types/index.js";
18
19
  /**
@@ -220,6 +221,7 @@ export declare class NeuroLink {
220
221
  */
221
222
  private observabilityConfig?;
222
223
  private metricsAggregator;
224
+ private analyticsService;
223
225
  /**
224
226
  * Per-request metrics trace context backed by AsyncLocalStorage.
225
227
  * Safe for concurrent requests on the same SDK instance.
@@ -532,6 +534,30 @@ export declare class NeuroLink {
532
534
  * Record a span for metrics tracking
533
535
  */
534
536
  recordMetricsSpan(span: SpanData): void;
537
+ /**
538
+ * Get provider metrics analysis
539
+ * Retrieves aggregated performance, token usage, latency, and success rates per provider.
540
+ *
541
+ * @param options - Filtering options
542
+ * @returns Comprehensive provider metrics result
543
+ */
544
+ getProviderMetrics(options?: ProviderMetricsOptions): Promise<ProviderMetricsResult>;
545
+ /**
546
+ * Get cost analysis breakdown
547
+ * Analyzes AI generation costs across requested groups and provides future projections.
548
+ *
549
+ * @param options - Cost configuration options
550
+ * @returns Detailed cost analysis breakdown
551
+ */
552
+ getCostAnalysis(options?: CostAnalysisOptions): Promise<CostAnalysisResult>;
553
+ /**
554
+ * Get team-wide usage analytics
555
+ * Retrieves request counts, unique active users, provider breakdown, and quality scoring.
556
+ *
557
+ * @param options - Team query options
558
+ * @returns Comprehensive team analytics report
559
+ */
560
+ getTeamAnalytics(options?: TeamAnalyticsOptions): Promise<TeamAnalyticsResult>;
535
561
  /**
536
562
  * Record a memory operation span to both instance and global metrics aggregators.
537
563
  * This ensures memory spans are visible via sdk.getSpans() and getMetricsAggregator().getSpans().
@@ -546,6 +572,34 @@ export declare class NeuroLink {
546
572
  * Gracefully shutdown NeuroLink and all MCP connections
547
573
  */
548
574
  shutdown(): Promise<void>;
575
+ /**
576
+ * Fire-and-forget analytics tracking with rejection logging.
577
+ */
578
+ private enqueueAnalyticsTrackRequest;
579
+ /**
580
+ * Estimate cost using the canonical SDK pricing table (same source as
581
+ * AnalyticsService.trackRequest) — avoids diverging TokenTracker rates.
582
+ */
583
+ private estimateCostFromUsage;
584
+ /**
585
+ * Handle generation:end for Pipeline B spans + advanced analytics.
586
+ * When pipelineAHandled is set, skips Pipeline B spans but still tracks
587
+ * analytics so successful AI-SDK generate/stream calls are recorded.
588
+ * Stream analytics are owned here (not stream:complete) to avoid duplicates.
589
+ */
590
+ private handleGenerationEndMetrics;
591
+ /**
592
+ * Pipeline B span recording for stream:complete.
593
+ * Advanced analytics are recorded from generation:end only (avoids duplicates
594
+ * when both stream:complete and generation:end fire for the same request).
595
+ */
596
+ private handleStreamCompleteMetrics;
597
+ private handleToolEndMetrics;
598
+ /**
599
+ * Pipeline B span recording for stream:error.
600
+ * Analytics for failed streams come from generation:end (success: false).
601
+ */
602
+ private handleStreamErrorMetrics;
549
603
  /**
550
604
  * Initialize event listeners that feed span data to MetricsAggregator.
551
605
  * Listens to generation:end, stream:complete, and tool:end events.