netra-sdk 1.9.0 → 1.10.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/README.md CHANGED
@@ -10,6 +10,7 @@
10
10
  - 🔧 **Multi-Provider Support**: Works with OpenAI, Google GenAI, Mistral, Anthropic, and more
11
11
  - 📈 **Session Management**: Track user sessions and custom attributes
12
12
  - 🌐 **Automatic Instrumentation**: Zero-code instrumentation for popular frameworks and libraries
13
+ - ⚡ **Opt-in Read Caching**: In-memory TTL caching for read-heavy SDK calls (`getPrompt`, `getModelPricing`)
13
14
 
14
15
  ## 📦 Installation
15
16
 
@@ -184,6 +185,73 @@ async function generateContent(prompt: string) {
184
185
  }
185
186
  ```
186
187
 
188
+ ## 📝 Prompts API
189
+
190
+ Fetch prompt versions from Prompt Studio via `Netra.prompts.getPrompt()`. Caching is **opt-in per call** — omit `useCache` (or set it to `false`) to always hit the API.
191
+
192
+ Default TTL is **60 seconds** (`PROMPT_CACHE_TTL_SECONDS`). Override TTL for a single call with `cacheTtl`.
193
+
194
+ ```typescript
195
+ import { Netra } from "netra-sdk";
196
+
197
+ await Netra.init({
198
+ appName: "my-ai-app",
199
+ });
200
+
201
+ // Always fetches from the API (default)
202
+ const prompt = await Netra.prompts.getPrompt({ name: "my-prompt" });
203
+
204
+ // Cached for 60s (default TTL)
205
+ const cached = await Netra.prompts.getPrompt({
206
+ name: "my-prompt",
207
+ useCache: true,
208
+ });
209
+
210
+ // Cached for 30s for this call only
211
+ const shortLived = await Netra.prompts.getPrompt({
212
+ name: "my-prompt",
213
+ label: "production", // default label when omitted
214
+ useCache: true,
215
+ cacheTtl: 30,
216
+ });
217
+ ```
218
+
219
+ > **Note**: Cached prompts may be stale for up to the TTL after dashboard edits. Use `useCache: false` when you need the latest version immediately. `Netra.shutdown()` clears in-memory caches.
220
+
221
+ ## 💰 Models API
222
+
223
+ Fetch model pricing via `Netra.models.getModelPricing()`. Caching is **opt-in per call** — omit `useCache` (or set it to `false`) to always hit the API.
224
+
225
+ Default TTL is **300 seconds** (`MODEL_PRICING_CACHE_TTL_SECONDS`). Override TTL for a single call with `cacheTtl`.
226
+
227
+ ```typescript
228
+ import { Netra } from "netra-sdk";
229
+
230
+ await Netra.init({
231
+ appName: "my-ai-app",
232
+ });
233
+
234
+ // Always fetches from the API (default)
235
+ const pricing = await Netra.models.getModelPricing();
236
+
237
+ // Optional name filter
238
+ const gptPricing = await Netra.models.getModelPricing({ name: "gpt-4o" });
239
+
240
+ // Cached for 300s (default TTL)
241
+ const cached = await Netra.models.getModelPricing({
242
+ useCache: true,
243
+ });
244
+
245
+ // Cached for 60s for this call only
246
+ const shortLived = await Netra.models.getModelPricing({
247
+ name: "gpt-4o",
248
+ useCache: true,
249
+ cacheTtl: 60,
250
+ });
251
+ ```
252
+
253
+ > **Note**: Cached pricing may be stale for up to the TTL after dashboard edits. Use `useCache: false` when you need the latest values immediately. `Netra.shutdown()` clears in-memory caches.
254
+
187
255
  ## 🔧 Environment Variables
188
256
 
189
257
  You can configure the SDK using environment variables:
package/dist/index.cjs CHANGED
@@ -15049,7 +15049,7 @@ var Usage = class {
15049
15049
  };
15050
15050
 
15051
15051
  // src/version.ts
15052
- var SDK_VERSION = "1.8.0";
15052
+ var SDK_VERSION = "1.10.0";
15053
15053
 
15054
15054
  // src/config.ts
15055
15055
  var NetraInstruments = /* @__PURE__ */ ((NetraInstruments2) => {
@@ -17462,6 +17462,33 @@ var Dashboard = class {
17462
17462
  }
17463
17463
  };
17464
17464
 
17465
+ // src/cache.ts
17466
+ var TTLCache = class {
17467
+ constructor(defaultTtl = 60) {
17468
+ this.store = /* @__PURE__ */ new Map();
17469
+ this.defaultTtl = defaultTtl;
17470
+ }
17471
+ get(key) {
17472
+ const entry = this.store.get(key);
17473
+ if (!entry) return void 0;
17474
+ if (performance.now() > entry.expiresAt) {
17475
+ this.store.delete(key);
17476
+ return void 0;
17477
+ }
17478
+ return entry.value;
17479
+ }
17480
+ set(key, value, ttl) {
17481
+ const ttlMs = (ttl ?? this.defaultTtl) * 1e3;
17482
+ this.store.set(key, { value, expiresAt: performance.now() + ttlMs });
17483
+ }
17484
+ invalidate(key) {
17485
+ this.store.delete(key);
17486
+ }
17487
+ clear() {
17488
+ this.store.clear();
17489
+ }
17490
+ };
17491
+
17465
17492
  // src/api/prompts/client.ts
17466
17493
  var PromptsHttpClient = class extends NetraHttpClient {
17467
17494
  constructor(config) {
@@ -17497,17 +17524,27 @@ var PromptsHttpClient = class extends NetraHttpClient {
17497
17524
  }
17498
17525
  };
17499
17526
 
17527
+ // src/api/prompts/models.ts
17528
+ var PROMPT_CACHE_TTL_SECONDS = 60;
17529
+
17500
17530
  // src/api/prompts/api.ts
17501
17531
  var Prompts = class {
17502
17532
  constructor(config) {
17503
17533
  this.config = config;
17504
17534
  this.client = new PromptsHttpClient(config);
17535
+ this.cache = new TTLCache(PROMPT_CACHE_TTL_SECONDS);
17536
+ }
17537
+ /** Clear all cached prompt entries. */
17538
+ clearCache() {
17539
+ this.cache.clear();
17505
17540
  }
17506
17541
  /**
17507
17542
  * Fetch prompt version by name and label.
17508
17543
  *
17509
- * @param params.name - Name of the prompt (required)
17510
- * @param params.label - Label of the prompt version (default: "production")
17544
+ * @param params.name - Name of the prompt (required)
17545
+ * @param params.label - Label of the prompt version (default: "production")
17546
+ * @param params.useCache - When true, read/write the in-memory cache (default: false)
17547
+ * @param params.cacheTtl - Per-call cache TTL in seconds (default: PROMPT_CACHE_TTL_SECONDS)
17511
17548
  */
17512
17549
  async getPrompt(params) {
17513
17550
  if (!params || typeof params.name !== "string" || !params.name) {
@@ -17515,11 +17552,99 @@ var Prompts = class {
17515
17552
  return null;
17516
17553
  }
17517
17554
  const label = params.label ?? "production";
17555
+ const useCache = params.useCache === true;
17556
+ const cacheKey = `prompt:${params.name}:${label}`;
17557
+ if (useCache) {
17558
+ const cached = this.cache.get(cacheKey);
17559
+ if (cached !== void 0) {
17560
+ return cached;
17561
+ }
17562
+ }
17518
17563
  const result = await this.client.getPromptVersion(params.name, label);
17519
17564
  if (!result) {
17520
17565
  return null;
17521
17566
  }
17522
- return result.data ?? null;
17567
+ const data = result.data ?? null;
17568
+ if (data !== null && useCache) {
17569
+ this.cache.set(cacheKey, data, params.cacheTtl);
17570
+ }
17571
+ return data;
17572
+ }
17573
+ };
17574
+
17575
+ // src/api/models/client.ts
17576
+ var ModelsHttpClient = class extends NetraHttpClient {
17577
+ constructor(config) {
17578
+ super(config, "NETRA_MODELS_TIMEOUT", 10);
17579
+ }
17580
+ async getModelPricing(name) {
17581
+ if (!this.isInitialized()) {
17582
+ Logger.error(
17583
+ "netra.models: Models client is not initialized; cannot fetch model pricing"
17584
+ );
17585
+ return null;
17586
+ }
17587
+ try {
17588
+ const params = name ? { name } : void 0;
17589
+ const response = await this.get("/sdk/models", params);
17590
+ if (!response.ok) {
17591
+ const errorMessage = response.data?.error?.message ?? "Unknown error";
17592
+ Logger.error(
17593
+ `netra.models: Failed to fetch model pricing: ${errorMessage}`
17594
+ );
17595
+ return null;
17596
+ }
17597
+ const items = this.extractData(response, null);
17598
+ if (items !== null && !Array.isArray(items)) {
17599
+ Logger.error(
17600
+ "netra.models: Unexpected response format; expected an array"
17601
+ );
17602
+ return null;
17603
+ }
17604
+ return items;
17605
+ } catch (err) {
17606
+ const message = err?.response?.data?.error?.message ?? err?.message ?? "";
17607
+ Logger.error("netra.models: Failed to fetch model pricing:", message);
17608
+ return null;
17609
+ }
17610
+ }
17611
+ };
17612
+
17613
+ // src/api/models/models.ts
17614
+ var MODEL_PRICING_CACHE_TTL_SECONDS = 300;
17615
+
17616
+ // src/api/models/api.ts
17617
+ var Models = class {
17618
+ constructor(config) {
17619
+ this.config = config;
17620
+ this.client = new ModelsHttpClient(config);
17621
+ this.cache = new TTLCache(MODEL_PRICING_CACHE_TTL_SECONDS);
17622
+ }
17623
+ /** Clear all cached model pricing entries. */
17624
+ clearCache() {
17625
+ this.cache.clear();
17626
+ }
17627
+ /**
17628
+ * Fetch model pricing from the backend.
17629
+ *
17630
+ * @param params.name - Optional model name filter
17631
+ * @param params.useCache - When true, read/write the in-memory cache (default: false)
17632
+ * @param params.cacheTtl - Per-call cache TTL in seconds (default: MODEL_PRICING_CACHE_TTL_SECONDS)
17633
+ */
17634
+ async getModelPricing(params = {}) {
17635
+ const useCache = params.useCache === true;
17636
+ const cacheKey = `model:pricing:${params.name ?? "all"}`;
17637
+ if (useCache) {
17638
+ const cached = this.cache.get(cacheKey);
17639
+ if (cached !== void 0) {
17640
+ return cached;
17641
+ }
17642
+ }
17643
+ const data = await this.client.getModelPricing(params.name);
17644
+ if (data !== null && useCache) {
17645
+ this.cache.set(cacheKey, data, params.cacheTtl);
17646
+ }
17647
+ return data;
17523
17648
  }
17524
17649
  };
17525
17650
 
@@ -27795,6 +27920,9 @@ var Netra = class {
27795
27920
  static isInitialized() {
27796
27921
  return this._initialized;
27797
27922
  }
27923
+ /**
27924
+ * Initialize the Netra SDK.
27925
+ */
27798
27926
  static async init(config = {}) {
27799
27927
  if (this._initialized) {
27800
27928
  Logger.warn("Netra.init() called more than once; ignoring subsequent calls.");
@@ -27836,6 +27964,11 @@ var Netra = class {
27836
27964
  } catch (e) {
27837
27965
  Logger.warn("Netra: failed to initialize prompts client:", e);
27838
27966
  }
27967
+ try {
27968
+ this.models = new Models(cfg);
27969
+ } catch (e) {
27970
+ Logger.warn("Netra: failed to initialize models client:", e);
27971
+ }
27839
27972
  this._initialized = true;
27840
27973
  Logger.info("Netra successfully initialized.");
27841
27974
  {
@@ -27903,6 +28036,12 @@ Received ${signal}. Shutting down Netra SDK...`);
27903
28036
  } catch (e) {
27904
28037
  Logger.error("Error during Netra trace shutdown:", e);
27905
28038
  }
28039
+ try {
28040
+ this.prompts?.clearCache();
28041
+ this.models?.clearCache();
28042
+ } catch (e) {
28043
+ Logger.error("Error clearing SDK API caches:", e);
28044
+ }
27906
28045
  this._initialized = false;
27907
28046
  this._tracer = void 0;
27908
28047
  }
@@ -28077,12 +28216,15 @@ exports.FilterType = FilterType;
28077
28216
  exports.FilteringSpanExporter = FilteringSpanExporter;
28078
28217
  exports.GroupBy = GroupBy;
28079
28218
  exports.InstrumentationSpanProcessor = InstrumentationSpanProcessor;
28219
+ exports.MODEL_PRICING_CACHE_TTL_SECONDS = MODEL_PRICING_CACHE_TTL_SECONDS;
28080
28220
  exports.Measure = Measure;
28221
+ exports.Models = Models;
28081
28222
  exports.Netra = Netra;
28082
28223
  exports.NetraAgentsTracingProcessor = NetraAgentsTracingProcessor;
28083
28224
  exports.NetraInstruments = NetraInstruments;
28084
28225
  exports.NetraOpenAIAgentsInstrumentor = NetraOpenAIAgentsInstrumentor;
28085
28226
  exports.Operator = Operator;
28227
+ exports.PROMPT_CACHE_TTL_SECONDS = PROMPT_CACHE_TTL_SECONDS;
28086
28228
  exports.Prompts = Prompts;
28087
28229
  exports.RunEntryContext = RunEntryContext;
28088
28230
  exports.RunStatus = RunStatus;