netra-sdk 1.8.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 +68 -0
- package/dist/index.cjs +248 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +106 -17
- package/dist/index.d.ts +106 -17
- package/dist/index.js +222 -61
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
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
|
@@ -2461,14 +2461,14 @@ var require_otel = __commonJS({
|
|
|
2461
2461
|
getActiveSpan() {
|
|
2462
2462
|
return void 0;
|
|
2463
2463
|
}
|
|
2464
|
-
setSpan(
|
|
2465
|
-
return
|
|
2464
|
+
setSpan(context17, _span) {
|
|
2465
|
+
return context17;
|
|
2466
2466
|
}
|
|
2467
2467
|
getSpan(_context) {
|
|
2468
2468
|
return void 0;
|
|
2469
2469
|
}
|
|
2470
|
-
setSpanContext(
|
|
2471
|
-
return
|
|
2470
|
+
setSpanContext(context17, _spanContext) {
|
|
2471
|
+
return context17;
|
|
2472
2472
|
}
|
|
2473
2473
|
getTracerProvider() {
|
|
2474
2474
|
return void 0;
|
|
@@ -3035,17 +3035,17 @@ var require_p_retry = __commonJS({
|
|
|
3035
3035
|
}
|
|
3036
3036
|
const retriesLeft = Number.isFinite(options.retries) ? Math.max(0, options.retries - retriesConsumed) : options.retries;
|
|
3037
3037
|
const maxRetryTime = options.maxRetryTime ?? Number.POSITIVE_INFINITY;
|
|
3038
|
-
const
|
|
3038
|
+
const context17 = Object.freeze({
|
|
3039
3039
|
error: normalizedError,
|
|
3040
3040
|
attemptNumber,
|
|
3041
3041
|
retriesLeft,
|
|
3042
3042
|
retriesConsumed
|
|
3043
3043
|
});
|
|
3044
|
-
await options.onFailedAttempt(
|
|
3044
|
+
await options.onFailedAttempt(context17);
|
|
3045
3045
|
if (calculateRemainingTime(startTime, maxRetryTime) <= 0) {
|
|
3046
3046
|
throw normalizedError;
|
|
3047
3047
|
}
|
|
3048
|
-
const consumeRetry = await options.shouldConsumeRetry(
|
|
3048
|
+
const consumeRetry = await options.shouldConsumeRetry(context17);
|
|
3049
3049
|
const remainingTime = calculateRemainingTime(startTime, maxRetryTime);
|
|
3050
3050
|
if (remainingTime <= 0 || retriesLeft <= 0) {
|
|
3051
3051
|
throw normalizedError;
|
|
@@ -3057,7 +3057,7 @@ var require_p_retry = __commonJS({
|
|
|
3057
3057
|
options.signal?.throwIfAborted();
|
|
3058
3058
|
return false;
|
|
3059
3059
|
}
|
|
3060
|
-
if (!await options.shouldRetry(
|
|
3060
|
+
if (!await options.shouldRetry(context17)) {
|
|
3061
3061
|
throw normalizedError;
|
|
3062
3062
|
}
|
|
3063
3063
|
if (!consumeRetry) {
|
|
@@ -3165,16 +3165,16 @@ var require_eventemitter3 = __commonJS({
|
|
|
3165
3165
|
Events.prototype = /* @__PURE__ */ Object.create(null);
|
|
3166
3166
|
if (!new Events().__proto__) prefix = false;
|
|
3167
3167
|
}
|
|
3168
|
-
function EE(fn,
|
|
3168
|
+
function EE(fn, context17, once) {
|
|
3169
3169
|
this.fn = fn;
|
|
3170
|
-
this.context =
|
|
3170
|
+
this.context = context17;
|
|
3171
3171
|
this.once = once || false;
|
|
3172
3172
|
}
|
|
3173
|
-
function addListener(emitter, event, fn,
|
|
3173
|
+
function addListener(emitter, event, fn, context17, once) {
|
|
3174
3174
|
if (typeof fn !== "function") {
|
|
3175
3175
|
throw new TypeError("The listener must be a function");
|
|
3176
3176
|
}
|
|
3177
|
-
var listener = new EE(fn,
|
|
3177
|
+
var listener = new EE(fn, context17 || emitter, once), evt = prefix ? prefix + event : event;
|
|
3178
3178
|
if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
|
|
3179
3179
|
else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
|
|
3180
3180
|
else emitter._events[evt] = [emitter._events[evt], listener];
|
|
@@ -3265,13 +3265,13 @@ var require_eventemitter3 = __commonJS({
|
|
|
3265
3265
|
}
|
|
3266
3266
|
return true;
|
|
3267
3267
|
};
|
|
3268
|
-
EventEmitter.prototype.on = function on(event, fn,
|
|
3269
|
-
return addListener(this, event, fn,
|
|
3268
|
+
EventEmitter.prototype.on = function on(event, fn, context17) {
|
|
3269
|
+
return addListener(this, event, fn, context17, false);
|
|
3270
3270
|
};
|
|
3271
|
-
EventEmitter.prototype.once = function once(event, fn,
|
|
3272
|
-
return addListener(this, event, fn,
|
|
3271
|
+
EventEmitter.prototype.once = function once(event, fn, context17) {
|
|
3272
|
+
return addListener(this, event, fn, context17, true);
|
|
3273
3273
|
};
|
|
3274
|
-
EventEmitter.prototype.removeListener = function removeListener(event, fn,
|
|
3274
|
+
EventEmitter.prototype.removeListener = function removeListener(event, fn, context17, once) {
|
|
3275
3275
|
var evt = prefix ? prefix + event : event;
|
|
3276
3276
|
if (!this._events[evt]) return this;
|
|
3277
3277
|
if (!fn) {
|
|
@@ -3280,12 +3280,12 @@ var require_eventemitter3 = __commonJS({
|
|
|
3280
3280
|
}
|
|
3281
3281
|
var listeners = this._events[evt];
|
|
3282
3282
|
if (listeners.fn) {
|
|
3283
|
-
if (listeners.fn === fn && (!once || listeners.once) && (!
|
|
3283
|
+
if (listeners.fn === fn && (!once || listeners.once) && (!context17 || listeners.context === context17)) {
|
|
3284
3284
|
clearEvent(this, evt);
|
|
3285
3285
|
}
|
|
3286
3286
|
} else {
|
|
3287
3287
|
for (var i = 0, events = [], length = listeners.length; i < length; i++) {
|
|
3288
|
-
if (listeners[i].fn !== fn || once && !listeners[i].once ||
|
|
3288
|
+
if (listeners[i].fn !== fn || once && !listeners[i].once || context17 && listeners[i].context !== context17) {
|
|
3289
3289
|
events.push(listeners[i]);
|
|
3290
3290
|
}
|
|
3291
3291
|
}
|
|
@@ -5844,7 +5844,7 @@ var require_error = __commonJS({
|
|
|
5844
5844
|
}
|
|
5845
5845
|
};
|
|
5846
5846
|
exports$1.LangSmithConflictError = LangSmithConflictError;
|
|
5847
|
-
async function raiseForStatus(response,
|
|
5847
|
+
async function raiseForStatus(response, context17, consumeOnSuccess) {
|
|
5848
5848
|
let errorBody;
|
|
5849
5849
|
if (response.ok) {
|
|
5850
5850
|
if (consumeOnSuccess) {
|
|
@@ -5872,7 +5872,7 @@ var require_error = __commonJS({
|
|
|
5872
5872
|
errorBody = "";
|
|
5873
5873
|
}
|
|
5874
5874
|
}
|
|
5875
|
-
const fullMessage = `Failed to ${
|
|
5875
|
+
const fullMessage = `Failed to ${context17}. Received status [${response.status}]: ${response.statusText}. Message: ${errorBody}`;
|
|
5876
5876
|
if (response.status === 409) {
|
|
5877
5877
|
throw new LangSmithConflictError(fullMessage);
|
|
5878
5878
|
}
|
|
@@ -7225,7 +7225,7 @@ var require_client = __commonJS({
|
|
|
7225
7225
|
});
|
|
7226
7226
|
return stream;
|
|
7227
7227
|
}
|
|
7228
|
-
async _sendMultipartRequest(parts,
|
|
7228
|
+
async _sendMultipartRequest(parts, context17, options) {
|
|
7229
7229
|
const boundary = "----LangSmithFormBoundary" + Math.random().toString(36).slice(2);
|
|
7230
7230
|
const isNodeFetch = (0, fetch_js_1._globalFetchImplementationIsNodeFetch)();
|
|
7231
7231
|
const buildBuffered = () => this._createNodeFetchBody(parts, boundary);
|
|
@@ -7267,14 +7267,14 @@ var require_client = __commonJS({
|
|
|
7267
7267
|
res = await sendWithRetry(buildBuffered);
|
|
7268
7268
|
}
|
|
7269
7269
|
if ((!this.multipartStreamingDisabled || streamedAttempt) && res.status === 422 && (options?.apiUrl ?? this.apiUrl) !== DEFAULT_API_URL) {
|
|
7270
|
-
console.warn(`Streaming multipart upload to ${options?.apiUrl ?? this.apiUrl}/runs/multipart failed. This usually means the host does not support chunked uploads. Retrying with a buffered upload for operation "${
|
|
7270
|
+
console.warn(`Streaming multipart upload to ${options?.apiUrl ?? this.apiUrl}/runs/multipart failed. This usually means the host does not support chunked uploads. Retrying with a buffered upload for operation "${context17}".`);
|
|
7271
7271
|
this.multipartStreamingDisabled = true;
|
|
7272
7272
|
res = await sendWithRetry(buildBuffered);
|
|
7273
7273
|
}
|
|
7274
7274
|
} catch (e) {
|
|
7275
7275
|
console.warn(`${e.message.trim()}
|
|
7276
7276
|
|
|
7277
|
-
Context: ${
|
|
7277
|
+
Context: ${context17}`);
|
|
7278
7278
|
}
|
|
7279
7279
|
}
|
|
7280
7280
|
async updateRun(runId, run, options) {
|
|
@@ -15049,7 +15049,7 @@ var Usage = class {
|
|
|
15049
15049
|
};
|
|
15050
15050
|
|
|
15051
15051
|
// src/version.ts
|
|
15052
|
-
var SDK_VERSION = "1.
|
|
15052
|
+
var SDK_VERSION = "1.10.0";
|
|
15053
15053
|
|
|
15054
15054
|
// src/config.ts
|
|
15055
15055
|
var NetraInstruments = /* @__PURE__ */ ((NetraInstruments2) => {
|
|
@@ -15139,11 +15139,6 @@ var _Config = class _Config {
|
|
|
15139
15139
|
"NETRA_DEBUG",
|
|
15140
15140
|
false
|
|
15141
15141
|
);
|
|
15142
|
-
this.enableRootSpan = this._getBoolConfig(
|
|
15143
|
-
config.enableRootSpan,
|
|
15144
|
-
"NETRA_ENABLE_ROOT_SPAN",
|
|
15145
|
-
false
|
|
15146
|
-
);
|
|
15147
15142
|
this.enableScrubbing = this._getBoolConfig(
|
|
15148
15143
|
config.enableScrubbing,
|
|
15149
15144
|
"NETRA_ENABLE_SCRUBBING",
|
|
@@ -15293,6 +15288,11 @@ _Config.SDK_NAME = "netra";
|
|
|
15293
15288
|
_Config.LIBRARY_NAME = "netra";
|
|
15294
15289
|
_Config.LIBRARY_VERSION = SDK_VERSION;
|
|
15295
15290
|
_Config.TRIAL_BLOCK_DURATION_SECONDS = 900;
|
|
15291
|
+
// 15 minutes
|
|
15292
|
+
// Root-span attribute marking traces produced by evaluation/simulation runs
|
|
15293
|
+
// so the FE/BE can distinguish them from normal workflow invocations.
|
|
15294
|
+
_Config.TRACE_ORIGIN_KEY = "netra.trace.origin";
|
|
15295
|
+
_Config.TRACE_ORIGIN_EVALUATION = "evaluation";
|
|
15296
15296
|
var Config = _Config;
|
|
15297
15297
|
|
|
15298
15298
|
// src/utils/pattern-matching.ts
|
|
@@ -16413,13 +16413,13 @@ async function runSingleEvaluator(params) {
|
|
|
16413
16413
|
if (config) {
|
|
16414
16414
|
expectedName = config.name;
|
|
16415
16415
|
}
|
|
16416
|
-
const
|
|
16416
|
+
const context17 = {
|
|
16417
16417
|
input: itemInput,
|
|
16418
16418
|
taskOutput,
|
|
16419
16419
|
expectedOutput,
|
|
16420
16420
|
metadata
|
|
16421
16421
|
};
|
|
16422
|
-
let result = evaluator.evaluate(
|
|
16422
|
+
let result = evaluator.evaluate(context17);
|
|
16423
16423
|
if (result instanceof Promise) {
|
|
16424
16424
|
result = await result;
|
|
16425
16425
|
}
|
|
@@ -16763,7 +16763,11 @@ var Evaluation = class {
|
|
|
16763
16763
|
*/
|
|
16764
16764
|
async executeItemPipeline(runId, runName, ctx, task2, evaluators, results, bgEvalTasks) {
|
|
16765
16765
|
const spanName = `TestRun.${runName}`;
|
|
16766
|
-
const span2 = new SpanWrapper(
|
|
16766
|
+
const span2 = new SpanWrapper(
|
|
16767
|
+
spanName,
|
|
16768
|
+
{ [Config.TRACE_ORIGIN_KEY]: Config.TRACE_ORIGIN_EVALUATION },
|
|
16769
|
+
"netra.evaluation"
|
|
16770
|
+
);
|
|
16767
16771
|
span2.start();
|
|
16768
16772
|
try {
|
|
16769
16773
|
const otelSpan = span2.getCurrentSpan();
|
|
@@ -17166,6 +17170,42 @@ var DashboardHttpClient = class extends NetraHttpClient {
|
|
|
17166
17170
|
return null;
|
|
17167
17171
|
}
|
|
17168
17172
|
}
|
|
17173
|
+
/**
|
|
17174
|
+
* Get detailed information for a specific session including all its traces.
|
|
17175
|
+
*
|
|
17176
|
+
* Args:
|
|
17177
|
+
* sessionId: The session ID to retrieve details for.
|
|
17178
|
+
*
|
|
17179
|
+
* Returns:
|
|
17180
|
+
* The session details response data or null on error.
|
|
17181
|
+
*/
|
|
17182
|
+
async getSessionDetails(sessionId) {
|
|
17183
|
+
if (!this.isInitialized()) {
|
|
17184
|
+
Logger.error(
|
|
17185
|
+
"netra.dashboard: Dashboard client is not initialized; cannot get session details"
|
|
17186
|
+
);
|
|
17187
|
+
return null;
|
|
17188
|
+
}
|
|
17189
|
+
try {
|
|
17190
|
+
const url = `/public/dashboard/session/${encodeURIComponent(sessionId)}`;
|
|
17191
|
+
const response = await this.get(url);
|
|
17192
|
+
if (!response.ok) {
|
|
17193
|
+
const errorMessage = response.data?.error?.message ?? "Unknown error";
|
|
17194
|
+
Logger.error(
|
|
17195
|
+
`netra.dashboard: Failed to fetch session details: ${errorMessage}`
|
|
17196
|
+
);
|
|
17197
|
+
return null;
|
|
17198
|
+
}
|
|
17199
|
+
return response.data;
|
|
17200
|
+
} catch (err) {
|
|
17201
|
+
const message = err?.response?.data?.error?.message ?? "";
|
|
17202
|
+
Logger.error(
|
|
17203
|
+
"netra.dashboard: Failed to fetch session details:",
|
|
17204
|
+
message
|
|
17205
|
+
);
|
|
17206
|
+
return null;
|
|
17207
|
+
}
|
|
17208
|
+
}
|
|
17169
17209
|
};
|
|
17170
17210
|
|
|
17171
17211
|
// src/api/dashboard/models.ts
|
|
@@ -17389,6 +17429,19 @@ var Dashboard = class {
|
|
|
17389
17429
|
}
|
|
17390
17430
|
return result.data ?? {};
|
|
17391
17431
|
}
|
|
17432
|
+
async getSessionDetails(sessionId) {
|
|
17433
|
+
if (!sessionId) {
|
|
17434
|
+
Logger.error(
|
|
17435
|
+
"netra.dashboard: session_id is required to fetch session details"
|
|
17436
|
+
);
|
|
17437
|
+
return null;
|
|
17438
|
+
}
|
|
17439
|
+
const result = await this.client.getSessionDetails(sessionId);
|
|
17440
|
+
if (!result) {
|
|
17441
|
+
return null;
|
|
17442
|
+
}
|
|
17443
|
+
return result.data ?? result;
|
|
17444
|
+
}
|
|
17392
17445
|
isValidScope(scope) {
|
|
17393
17446
|
return Object.values(Scope).includes(scope);
|
|
17394
17447
|
}
|
|
@@ -17409,6 +17462,33 @@ var Dashboard = class {
|
|
|
17409
17462
|
}
|
|
17410
17463
|
};
|
|
17411
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
|
+
|
|
17412
17492
|
// src/api/prompts/client.ts
|
|
17413
17493
|
var PromptsHttpClient = class extends NetraHttpClient {
|
|
17414
17494
|
constructor(config) {
|
|
@@ -17444,17 +17524,27 @@ var PromptsHttpClient = class extends NetraHttpClient {
|
|
|
17444
17524
|
}
|
|
17445
17525
|
};
|
|
17446
17526
|
|
|
17527
|
+
// src/api/prompts/models.ts
|
|
17528
|
+
var PROMPT_CACHE_TTL_SECONDS = 60;
|
|
17529
|
+
|
|
17447
17530
|
// src/api/prompts/api.ts
|
|
17448
17531
|
var Prompts = class {
|
|
17449
17532
|
constructor(config) {
|
|
17450
17533
|
this.config = config;
|
|
17451
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();
|
|
17452
17540
|
}
|
|
17453
17541
|
/**
|
|
17454
17542
|
* Fetch prompt version by name and label.
|
|
17455
17543
|
*
|
|
17456
|
-
* @param params.name
|
|
17457
|
-
* @param params.label
|
|
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)
|
|
17458
17548
|
*/
|
|
17459
17549
|
async getPrompt(params) {
|
|
17460
17550
|
if (!params || typeof params.name !== "string" || !params.name) {
|
|
@@ -17462,11 +17552,99 @@ var Prompts = class {
|
|
|
17462
17552
|
return null;
|
|
17463
17553
|
}
|
|
17464
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
|
+
}
|
|
17465
17563
|
const result = await this.client.getPromptVersion(params.name, label);
|
|
17466
17564
|
if (!result) {
|
|
17467
17565
|
return null;
|
|
17468
17566
|
}
|
|
17469
|
-
|
|
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;
|
|
17470
17648
|
}
|
|
17471
17649
|
};
|
|
17472
17650
|
|
|
@@ -18304,6 +18482,13 @@ var SpanIOProcessor = class {
|
|
|
18304
18482
|
if (key === _USAGE_OUTPUT_TOKENS) {
|
|
18305
18483
|
return original(_USAGE_COMPLETION_TOKENS, value);
|
|
18306
18484
|
}
|
|
18485
|
+
if (key === "db.statement") {
|
|
18486
|
+
original(key, value);
|
|
18487
|
+
if (!inputLocked() && inputIsEmpty() && !isEmpty(value)) {
|
|
18488
|
+
original("input", typeof value === "string" ? value : String(value));
|
|
18489
|
+
}
|
|
18490
|
+
return span2;
|
|
18491
|
+
}
|
|
18307
18492
|
return original(key, value);
|
|
18308
18493
|
} catch (e) {
|
|
18309
18494
|
Logger.debug(`SpanIOProcessor: error processing key=${key}`, e);
|
|
@@ -18884,7 +19069,7 @@ function setUsageAttributes(span2, response) {
|
|
|
18884
19069
|
Number(cacheReadTokens)
|
|
18885
19070
|
);
|
|
18886
19071
|
}
|
|
18887
|
-
const cacheCreationTokens = usage.cache_creation_input_tokens;
|
|
19072
|
+
const cacheCreationTokens = usage.cache_creation_input_tokens ?? (usage.prompt_tokens_details ?? usage.input_tokens_details)?.cache_write_tokens;
|
|
18888
19073
|
if (cacheCreationTokens !== void 0) {
|
|
18889
19074
|
span2.setAttribute(
|
|
18890
19075
|
SpanAttributes2.LLM_USAGE_CACHE_CREATION_INPUT_TOKENS,
|
|
@@ -26829,7 +27014,7 @@ var SimulationHttpClient = class {
|
|
|
26829
27014
|
* Used in the two-phase flow so that beforeAll/before hooks can run
|
|
26830
27015
|
* before any LLM spend.
|
|
26831
27016
|
*/
|
|
26832
|
-
async initializeRun(name, datasetId,
|
|
27017
|
+
async initializeRun(name, datasetId, context17, hooksMeta) {
|
|
26833
27018
|
if (!this.client) {
|
|
26834
27019
|
Logger.error(`${LOG_PREFIX5}: Client not initialized`);
|
|
26835
27020
|
return null;
|
|
@@ -26839,7 +27024,7 @@ var SimulationHttpClient = class {
|
|
|
26839
27024
|
const payload = {
|
|
26840
27025
|
name,
|
|
26841
27026
|
datasetId,
|
|
26842
|
-
context:
|
|
27027
|
+
context: context17 || {}
|
|
26843
27028
|
};
|
|
26844
27029
|
if (hooksMeta) {
|
|
26845
27030
|
payload.lifecycleHooks = hooksMeta;
|
|
@@ -27157,7 +27342,7 @@ var Simulation = class {
|
|
|
27157
27342
|
name,
|
|
27158
27343
|
datasetId,
|
|
27159
27344
|
task: task2,
|
|
27160
|
-
context:
|
|
27345
|
+
context: context17,
|
|
27161
27346
|
maxConcurrency = 5,
|
|
27162
27347
|
hooks
|
|
27163
27348
|
} = options;
|
|
@@ -27169,7 +27354,7 @@ var Simulation = class {
|
|
|
27169
27354
|
const initResult = await this._client.initializeRun(
|
|
27170
27355
|
name,
|
|
27171
27356
|
datasetId,
|
|
27172
|
-
|
|
27357
|
+
context17,
|
|
27173
27358
|
hooksMeta && Object.keys(hooksMeta).length > 0 ? hooksMeta : null
|
|
27174
27359
|
);
|
|
27175
27360
|
if (!initResult) {
|
|
@@ -27470,7 +27655,11 @@ var Simulation = class {
|
|
|
27470
27655
|
let sessionId = null;
|
|
27471
27656
|
let rawFiles = initialFiles ?? [];
|
|
27472
27657
|
while (true) {
|
|
27473
|
-
const span2 = new SpanWrapper(
|
|
27658
|
+
const span2 = new SpanWrapper(
|
|
27659
|
+
SPAN_NAME,
|
|
27660
|
+
{ [Config.TRACE_ORIGIN_KEY]: Config.TRACE_ORIGIN_EVALUATION },
|
|
27661
|
+
LOG_PREFIX7
|
|
27662
|
+
);
|
|
27474
27663
|
span2.start();
|
|
27475
27664
|
try {
|
|
27476
27665
|
const traceId = span2.getCurrentSpan()?.spanContext().traceId ?? "";
|
|
@@ -27731,6 +27920,9 @@ var Netra = class {
|
|
|
27731
27920
|
static isInitialized() {
|
|
27732
27921
|
return this._initialized;
|
|
27733
27922
|
}
|
|
27923
|
+
/**
|
|
27924
|
+
* Initialize the Netra SDK.
|
|
27925
|
+
*/
|
|
27734
27926
|
static async init(config = {}) {
|
|
27735
27927
|
if (this._initialized) {
|
|
27736
27928
|
Logger.warn("Netra.init() called more than once; ignoring subsequent calls.");
|
|
@@ -27772,6 +27964,11 @@ var Netra = class {
|
|
|
27772
27964
|
} catch (e) {
|
|
27773
27965
|
Logger.warn("Netra: failed to initialize prompts client:", e);
|
|
27774
27966
|
}
|
|
27967
|
+
try {
|
|
27968
|
+
this.models = new Models(cfg);
|
|
27969
|
+
} catch (e) {
|
|
27970
|
+
Logger.warn("Netra: failed to initialize models client:", e);
|
|
27971
|
+
}
|
|
27775
27972
|
this._initialized = true;
|
|
27776
27973
|
Logger.info("Netra successfully initialized.");
|
|
27777
27974
|
{
|
|
@@ -27810,26 +28007,6 @@ Received ${signal}. Shutting down Netra SDK...`);
|
|
|
27810
28007
|
process.once("SIGINT", () => handleSignal("SIGINT"));
|
|
27811
28008
|
process.once("SIGTERM", () => handleSignal("SIGTERM"));
|
|
27812
28009
|
process.once("uncaughtException", handleUncaughtException);
|
|
27813
|
-
if (cfg.enableRootSpan) {
|
|
27814
|
-
const tracer2 = this._tracer || api.trace.getTracer("netra.root.span");
|
|
27815
|
-
const rootName = `${Config.LIBRARY_NAME}.root.span`;
|
|
27816
|
-
this._rootSpan = tracer2.startSpan(rootName, {
|
|
27817
|
-
kind: api.SpanKind.INTERNAL
|
|
27818
|
-
});
|
|
27819
|
-
if (this._rootSpan) {
|
|
27820
|
-
if (cfg.appName) {
|
|
27821
|
-
this._rootSpan.setAttribute("service.name", cfg.appName);
|
|
27822
|
-
}
|
|
27823
|
-
this._rootSpan.setAttribute("netra.environment", cfg.environment);
|
|
27824
|
-
this._rootSpan.setAttribute(
|
|
27825
|
-
"netra.library.version",
|
|
27826
|
-
Config.LIBRARY_VERSION
|
|
27827
|
-
);
|
|
27828
|
-
Logger.info(
|
|
27829
|
-
"Netra root span created. Use Netra.runWithRootSpan() to parent spans under it."
|
|
27830
|
-
);
|
|
27831
|
-
}
|
|
27832
|
-
}
|
|
27833
28010
|
await instrumentationsReady;
|
|
27834
28011
|
}
|
|
27835
28012
|
static async shutdown() {
|
|
@@ -27841,14 +28018,6 @@ Received ${signal}. Shutting down Netra SDK...`);
|
|
|
27841
28018
|
} catch (e) {
|
|
27842
28019
|
Logger.error("Error during uninstrumentAll:", e);
|
|
27843
28020
|
}
|
|
27844
|
-
if (this._rootSpan) {
|
|
27845
|
-
try {
|
|
27846
|
-
this._rootSpan.end();
|
|
27847
|
-
} catch (e) {
|
|
27848
|
-
} finally {
|
|
27849
|
-
this._rootSpan = void 0;
|
|
27850
|
-
}
|
|
27851
|
-
}
|
|
27852
28021
|
const FLUSH_TIMEOUT_MS = 5e3;
|
|
27853
28022
|
try {
|
|
27854
28023
|
const traceProvider = api.trace.getTracerProvider();
|
|
@@ -27867,6 +28036,12 @@ Received ${signal}. Shutting down Netra SDK...`);
|
|
|
27867
28036
|
} catch (e) {
|
|
27868
28037
|
Logger.error("Error during Netra trace shutdown:", e);
|
|
27869
28038
|
}
|
|
28039
|
+
try {
|
|
28040
|
+
this.prompts?.clearCache();
|
|
28041
|
+
this.models?.clearCache();
|
|
28042
|
+
} catch (e) {
|
|
28043
|
+
Logger.error("Error clearing SDK API caches:", e);
|
|
28044
|
+
}
|
|
27870
28045
|
this._initialized = false;
|
|
27871
28046
|
this._tracer = void 0;
|
|
27872
28047
|
}
|
|
@@ -27885,20 +28060,6 @@ Received ${signal}. Shutting down Netra SDK...`);
|
|
|
27885
28060
|
static setRootOutput(value) {
|
|
27886
28061
|
SessionManager.setRootOutput(value);
|
|
27887
28062
|
}
|
|
27888
|
-
/**
|
|
27889
|
-
* Run a function with the root span as the active parent context.
|
|
27890
|
-
* All spans created within this function will be children of the root span.
|
|
27891
|
-
* Note: required in JS because OTel JS has no persistent context.attach()
|
|
27892
|
-
*/
|
|
27893
|
-
static runWithRootSpan(fn) {
|
|
27894
|
-
if (!this._rootSpan) {
|
|
27895
|
-
Logger.warn(
|
|
27896
|
-
"runWithRootSpan: No root span available. Running function without parent context."
|
|
27897
|
-
);
|
|
27898
|
-
return fn();
|
|
27899
|
-
}
|
|
27900
|
-
return api.context.with(api.trace.setSpan(api.context.active(), this._rootSpan), fn);
|
|
27901
|
-
}
|
|
27902
28063
|
static setSessionId(sessionId) {
|
|
27903
28064
|
if (typeof sessionId !== "string") {
|
|
27904
28065
|
Logger.error(`setSessionId: sessionId must be a string, got ${typeof sessionId}`);
|
|
@@ -28055,12 +28216,15 @@ exports.FilterType = FilterType;
|
|
|
28055
28216
|
exports.FilteringSpanExporter = FilteringSpanExporter;
|
|
28056
28217
|
exports.GroupBy = GroupBy;
|
|
28057
28218
|
exports.InstrumentationSpanProcessor = InstrumentationSpanProcessor;
|
|
28219
|
+
exports.MODEL_PRICING_CACHE_TTL_SECONDS = MODEL_PRICING_CACHE_TTL_SECONDS;
|
|
28058
28220
|
exports.Measure = Measure;
|
|
28221
|
+
exports.Models = Models;
|
|
28059
28222
|
exports.Netra = Netra;
|
|
28060
28223
|
exports.NetraAgentsTracingProcessor = NetraAgentsTracingProcessor;
|
|
28061
28224
|
exports.NetraInstruments = NetraInstruments;
|
|
28062
28225
|
exports.NetraOpenAIAgentsInstrumentor = NetraOpenAIAgentsInstrumentor;
|
|
28063
28226
|
exports.Operator = Operator;
|
|
28227
|
+
exports.PROMPT_CACHE_TTL_SECONDS = PROMPT_CACHE_TTL_SECONDS;
|
|
28064
28228
|
exports.Prompts = Prompts;
|
|
28065
28229
|
exports.RunEntryContext = RunEntryContext;
|
|
28066
28230
|
exports.RunStatus = RunStatus;
|