@hilbras/sdk 3.6.1 → 3.7.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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="https://img.shields.io/badge/version-3.
|
|
2
|
+
<img src="https://img.shields.io/badge/version-3.7.0-blue" alt="version">
|
|
3
3
|
<img src="https://img.shields.io/badge/license-MIT-green" alt="license">
|
|
4
4
|
<img src="https://img.shields.io/badge/node-%3E%3D18-brightgreen" alt="node">
|
|
5
5
|
<img src="https://img.shields.io/badge/types-strict-blueviolet" alt="types">
|
package/dist/client/client.js
CHANGED
|
@@ -435,7 +435,12 @@ export class HilbrasClient {
|
|
|
435
435
|
_resolveProviderModel(params) {
|
|
436
436
|
// If explicit provider + model, use them directly
|
|
437
437
|
if (params.provider && params.model) {
|
|
438
|
-
return {
|
|
438
|
+
return {
|
|
439
|
+
providerName: params.provider,
|
|
440
|
+
modelId: params.model,
|
|
441
|
+
score: 0,
|
|
442
|
+
reasons: ["Explicit provider/model"],
|
|
443
|
+
};
|
|
439
444
|
}
|
|
440
445
|
// Auto-connect: structured output requirement → router capability filter
|
|
441
446
|
const needsStructured = params.needsStructuredOutput ?? params.hasOutput ?? false;
|
|
@@ -455,7 +460,12 @@ export class HilbrasClient {
|
|
|
455
460
|
throw new Error("No model found matching the given requirements. " +
|
|
456
461
|
"Try relaxing constraints or registering more providers.");
|
|
457
462
|
}
|
|
458
|
-
return {
|
|
463
|
+
return {
|
|
464
|
+
providerName: result.provider,
|
|
465
|
+
modelId: result.model,
|
|
466
|
+
score: result.score,
|
|
467
|
+
reasons: result.reasons,
|
|
468
|
+
};
|
|
459
469
|
}
|
|
460
470
|
// ─── Message Normalization ──────────────────────────────────────────────
|
|
461
471
|
_normalizeMessages(raw) {
|
|
@@ -472,12 +482,12 @@ export class HilbrasClient {
|
|
|
472
482
|
const startTime = performance.now();
|
|
473
483
|
this._emit({ type: "request.start", requestId, timestamp: startTime, provider: params.provider, model: params.model, task: params.task });
|
|
474
484
|
// Resolve provider + model — either explicit or via router
|
|
475
|
-
const { providerName, modelId } = this._resolveProviderModel(params);
|
|
485
|
+
const { providerName, modelId, score, reasons } = this._resolveProviderModel(params);
|
|
476
486
|
const providerConfig = this._registry.getOrThrow(providerName);
|
|
477
487
|
const model = providerConfig.models.find((m) => m.id === modelId);
|
|
478
488
|
if (!model)
|
|
479
489
|
throw new ModelNotFoundError(modelId, providerName, providerConfig.models.map((m) => m.id));
|
|
480
|
-
this._emit({ type: "routing.resolved", requestId, timestamp: performance.now(), provider: providerName, model: modelId, score
|
|
490
|
+
this._emit({ type: "routing.resolved", requestId, timestamp: performance.now(), provider: providerName, model: modelId, score, reasons });
|
|
481
491
|
const messages = this._normalizeMessages(params.messages);
|
|
482
492
|
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
483
493
|
yield* this._requestPipeline.runStream({
|
|
@@ -521,7 +531,7 @@ export class HilbrasClient {
|
|
|
521
531
|
}
|
|
522
532
|
const providerName = resolved_.providerName;
|
|
523
533
|
const modelId = resolved_.modelId;
|
|
524
|
-
this._emit({ type: "routing.resolved", requestId, timestamp: performance.now(), provider: providerName, model: modelId, score:
|
|
534
|
+
this._emit({ type: "routing.resolved", requestId, timestamp: performance.now(), provider: providerName, model: modelId, score: resolved_.score, reasons: resolved_.reasons });
|
|
525
535
|
if (!params.output) {
|
|
526
536
|
const messages = this._normalizeMessages(params.messages);
|
|
527
537
|
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
@@ -284,6 +284,7 @@ export class RequestPipeline {
|
|
|
284
284
|
attempts: attempt + 1,
|
|
285
285
|
inputTokens,
|
|
286
286
|
outputTokens,
|
|
287
|
+
estimatedCost: input.estimatedCost,
|
|
287
288
|
structuredOutput: false,
|
|
288
289
|
});
|
|
289
290
|
await this.ports.plugins.fireResponse({
|
|
@@ -408,6 +409,7 @@ export class RequestPipeline {
|
|
|
408
409
|
attempts: attempt + 2,
|
|
409
410
|
inputTokens: fallbackInputTokens,
|
|
410
411
|
outputTokens: fallbackOutputTokens,
|
|
412
|
+
estimatedCost: input.estimatedCost,
|
|
411
413
|
structuredOutput: false,
|
|
412
414
|
});
|
|
413
415
|
await this.ports.plugins.fireResponse({
|
|
@@ -789,6 +791,7 @@ export class RequestPipeline {
|
|
|
789
791
|
model,
|
|
790
792
|
durationMs: this.ports.now() - input.startTime,
|
|
791
793
|
attempts,
|
|
794
|
+
estimatedCost: input.estimatedCost,
|
|
792
795
|
structuredOutput,
|
|
793
796
|
});
|
|
794
797
|
}
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* // Or use the built-in hook adapter
|
|
16
16
|
* otel.instrumentClient(client);
|
|
17
17
|
*/
|
|
18
|
-
import type { HookEvent, RequestCompletedEvent, RequestFailedEvent, RetryEvent } from "../types/observability.js";
|
|
18
|
+
import type { CircuitBreakerOpenEvent, HookEvent, RequestCompletedEvent, RequestFailedEvent, RetryEvent, RoutingResolvedEvent } from "../types/observability.js";
|
|
19
19
|
/** Configuration for the OpenTelemetry exporter */
|
|
20
20
|
export interface OpenTelemetryConfig {
|
|
21
21
|
/** Service name for traces and metrics */
|
|
@@ -40,6 +40,7 @@ export declare class OpenTelemetryExporter {
|
|
|
40
40
|
private _requestCount?;
|
|
41
41
|
private _errorCount?;
|
|
42
42
|
private _tokenUsage?;
|
|
43
|
+
private _cost?;
|
|
43
44
|
private _activeRequests?;
|
|
44
45
|
private _retryCount?;
|
|
45
46
|
constructor(config?: OpenTelemetryConfig);
|
|
@@ -50,6 +51,10 @@ export declare class OpenTelemetryExporter {
|
|
|
50
51
|
recordError(event: RequestFailedEvent): void;
|
|
51
52
|
/** Record a retry event */
|
|
52
53
|
recordRetry(event: RetryEvent): void;
|
|
54
|
+
/** Record a routing decision */
|
|
55
|
+
recordRouting(event: RoutingResolvedEvent): void;
|
|
56
|
+
/** Record a circuit breaker opening */
|
|
57
|
+
recordCircuitBreakerOpen(event: CircuitBreakerOpenEvent): void;
|
|
53
58
|
/** Record active request count */
|
|
54
59
|
recordActiveRequests(count: number): void;
|
|
55
60
|
/**
|
|
@@ -41,6 +41,7 @@ export class OpenTelemetryExporter {
|
|
|
41
41
|
_requestCount;
|
|
42
42
|
_errorCount;
|
|
43
43
|
_tokenUsage;
|
|
44
|
+
_cost;
|
|
44
45
|
_activeRequests;
|
|
45
46
|
_retryCount;
|
|
46
47
|
constructor(config) {
|
|
@@ -76,6 +77,9 @@ export class OpenTelemetryExporter {
|
|
|
76
77
|
this._tokenUsage = this._meter.createHistogram("llm.tokens", {
|
|
77
78
|
description: "Token usage per request",
|
|
78
79
|
});
|
|
80
|
+
this._cost = this._meter.createHistogram("llm.cost", {
|
|
81
|
+
description: "Estimated cost per request in USD",
|
|
82
|
+
});
|
|
79
83
|
this._activeRequests = this._meter.createGauge("llm.requests.active", {
|
|
80
84
|
description: "Number of currently active LLM requests",
|
|
81
85
|
});
|
|
@@ -98,6 +102,9 @@ export class OpenTelemetryExporter {
|
|
|
98
102
|
if (event.outputTokens) {
|
|
99
103
|
this._tokenUsage?.record(event.outputTokens, { ...attrs, token_type: "output" });
|
|
100
104
|
}
|
|
105
|
+
if (event.estimatedCost != null) {
|
|
106
|
+
this._cost?.record(event.estimatedCost, attrs);
|
|
107
|
+
}
|
|
101
108
|
// Trace span
|
|
102
109
|
if (this._tracer) {
|
|
103
110
|
const span = this._tracer.startSpan(`llm.complete`);
|
|
@@ -108,6 +115,8 @@ export class OpenTelemetryExporter {
|
|
|
108
115
|
span.setAttribute("llm.input_tokens", event.inputTokens);
|
|
109
116
|
if (event.outputTokens)
|
|
110
117
|
span.setAttribute("llm.output_tokens", event.outputTokens);
|
|
118
|
+
if (event.estimatedCost != null)
|
|
119
|
+
span.setAttribute("llm.estimated_cost", event.estimatedCost);
|
|
111
120
|
span.setStatus({ code: 0 }); // OK
|
|
112
121
|
span.end();
|
|
113
122
|
}
|
|
@@ -137,6 +146,28 @@ export class OpenTelemetryExporter {
|
|
|
137
146
|
reason: event.reason,
|
|
138
147
|
});
|
|
139
148
|
}
|
|
149
|
+
/** Record a routing decision */
|
|
150
|
+
recordRouting(event) {
|
|
151
|
+
if (!this._tracer)
|
|
152
|
+
return;
|
|
153
|
+
const span = this._tracer.startSpan(`llm.route`);
|
|
154
|
+
span.setAttribute("llm.provider", event.provider);
|
|
155
|
+
span.setAttribute("llm.model", event.model);
|
|
156
|
+
span.setAttribute("llm.routing.score", event.score);
|
|
157
|
+
span.setAttribute("llm.routing.reasons", event.reasons.join("; "));
|
|
158
|
+
span.setStatus({ code: 0 }); // OK
|
|
159
|
+
span.end();
|
|
160
|
+
}
|
|
161
|
+
/** Record a circuit breaker opening */
|
|
162
|
+
recordCircuitBreakerOpen(event) {
|
|
163
|
+
if (!this._tracer)
|
|
164
|
+
return;
|
|
165
|
+
const span = this._tracer.startSpan(`llm.circuit_breaker`);
|
|
166
|
+
span.setAttribute("llm.provider", event.provider);
|
|
167
|
+
span.setAttribute("llm.circuit_breaker.state", "open");
|
|
168
|
+
span.setStatus({ code: 0 }); // OK
|
|
169
|
+
span.end();
|
|
170
|
+
}
|
|
140
171
|
/** Record active request count */
|
|
141
172
|
recordActiveRequests(count) {
|
|
142
173
|
this._activeRequests?.record(count);
|
|
@@ -158,10 +189,20 @@ export class OpenTelemetryExporter {
|
|
|
158
189
|
if (e.type === "request.retrying")
|
|
159
190
|
this.recordRetry(e);
|
|
160
191
|
});
|
|
192
|
+
const unsub4 = client.on("routing.resolved", (e) => {
|
|
193
|
+
if (e.type === "routing.resolved")
|
|
194
|
+
this.recordRouting(e);
|
|
195
|
+
});
|
|
196
|
+
const unsub5 = client.on("circuit_breaker.open", (e) => {
|
|
197
|
+
if (e.type === "circuit_breaker.open")
|
|
198
|
+
this.recordCircuitBreakerOpen(e);
|
|
199
|
+
});
|
|
161
200
|
return () => {
|
|
162
201
|
unsub1();
|
|
163
202
|
unsub2();
|
|
164
203
|
unsub3();
|
|
204
|
+
unsub4();
|
|
205
|
+
unsub5();
|
|
165
206
|
};
|
|
166
207
|
}
|
|
167
208
|
/** Check if OpenTelemetry is available */
|
|
@@ -48,6 +48,8 @@ export interface RequestCompletedEvent extends BaseEvent {
|
|
|
48
48
|
inputTokens?: number;
|
|
49
49
|
/** Output tokens (if available from usage chunk) */
|
|
50
50
|
outputTokens?: number;
|
|
51
|
+
/** Estimated cost of the request in USD (if cost tracking is enabled) */
|
|
52
|
+
estimatedCost?: number;
|
|
51
53
|
/** Whether structured output was used */
|
|
52
54
|
structuredOutput: boolean;
|
|
53
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hilbras/sdk",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.0",
|
|
4
4
|
"description": "Provider-agnostic LLM client SDK \u2014 streaming, tool calling, embeddings, image generation, speech synthesis, transcription, reranking, circuit breaker, retry, reasoning normalization, cost enforcement, structured output with schema validation, type-safe tool builder, and SSRF-safe provider registration. Zero runtime dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -315,14 +315,15 @@
|
|
|
315
315
|
}
|
|
316
316
|
},
|
|
317
317
|
"devDependencies": {
|
|
318
|
-
"@types/node": "^26.
|
|
318
|
+
"@types/node": "^26.6.5",
|
|
319
319
|
"@types/react": "^19.3.0",
|
|
320
320
|
"@vitest/coverage-v8": "^4.1.11",
|
|
321
|
-
"oxlint": "^1.
|
|
322
|
-
"react": "^19.3.
|
|
321
|
+
"oxlint": "^1.88.0",
|
|
322
|
+
"react": "^19.3.1",
|
|
323
|
+
"react-dom": "^19.3.1",
|
|
323
324
|
"typescript": "^7.0.2",
|
|
324
325
|
"vitest": "^4.1.11",
|
|
325
|
-
"vue": "^3.5.
|
|
326
|
+
"vue": "^3.5.44"
|
|
326
327
|
},
|
|
327
328
|
"packageManager": "npm@11.19.0",
|
|
328
329
|
"workspaces": [
|