@hilbras/sdk 3.4.0 → 3.4.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.
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  <p align="center">
2
- <img src="https://img.shields.io/badge/version-3.4.0-blue" alt="version">
2
+ <img src="https://img.shields.io/badge/version-3.4.1-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">
6
- <img src="https://img.shields.io/badge/tests-1797%20passing-brightgreen" alt="tests">
6
+ <img src="https://img.shields.io/badge/tests-1803%20passing-brightgreen" alt="tests">
7
7
  <img src="https://img.shields.io/badge/runtime%20deps-zero-brightgreen" alt="zero deps">
8
8
  </p>
9
9
 
@@ -21,7 +21,7 @@ import type { EmbeddingResult, ImageResult, SpeechResult, TranscriptionResult, R
21
21
  import type { ConfigDiagnostic, ConfigSource, SafeSDKConfig } from "../config/config-schema.js";
22
22
  import { type ResolvedSDKConfig } from "../config/resolved-config.js";
23
23
  import type { SDKConfig } from "../config/schema.js";
24
- import type { AuthorizationDiagnostic } from "../security/authorization.js";
24
+ import type { AuthorizationDiagnostic, ResolveAuthorizationBudget } from "../security/authorization.js";
25
25
  import { ToolPolicy } from "../security/tool-policy.js";
26
26
  import { AdapterRegistry } from "../providers/adapter-registry.js";
27
27
  import { ModelRouter } from "../router/model-router.js";
@@ -90,6 +90,34 @@ export interface HilbrasClientConfig {
90
90
  configSources?: ConfigSource[];
91
91
  /** An already-resolved configuration, primarily for framework adapters. */
92
92
  resolvedConfig?: ResolvedSDKConfig;
93
+ /**
94
+ * Identity resolution for the authorization middleware installed by
95
+ * `config.rbac`.
96
+ *
97
+ * Without `resolveUserId` the client has no notion of a caller, so a
98
+ * per-user role cannot be selected and a per-role `rateLimit` cannot be
99
+ * keyed. The SDK then reports `RBAC_RATE_LIMIT_UNKEYED` rather than
100
+ * claiming to enforce a limit it cannot apply.
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * new HilbrasClient({
105
+ * config: { rbac: { roles, defaultRole: "viewer" } },
106
+ * authorization: {
107
+ * resolveUserId: () => currentUserId(),
108
+ * resolveRole: (userId) => roleForUser(userId),
109
+ * },
110
+ * });
111
+ * ```
112
+ */
113
+ authorization?: {
114
+ /** Resolve the calling identity, or `null` when anonymous. */
115
+ resolveUserId?: () => string | null | undefined;
116
+ /** Map a resolved identity onto a role name. */
117
+ resolveRole?: (userId: string | null) => string | null | undefined;
118
+ /** Supply a per-role budget view for `maxBudgetPerSession`. */
119
+ resolveBudget?: ResolveAuthorizationBudget;
120
+ };
93
121
  /**
94
122
  * v1.1.0: Optional telemetry sinks. When provided, the client automatically
95
123
  * forwards lifecycle events to the configured sinks.
@@ -89,9 +89,25 @@ export class HilbrasClient {
89
89
  // covered by the same policy.
90
90
  const middlewareChain = [];
91
91
  if (resolvedSdk.rbac) {
92
- middlewareChain.push(createRBACMiddleware(resolvedSdk.rbac, () => null, {
92
+ // The identity is resolved once per request and shared between the user
93
+ // id and role resolvers, so an expensive auth lookup happens only once.
94
+ const identityCache = new WeakMap();
95
+ const resolveIdentity = (key) => {
96
+ const cached = identityCache.get(key);
97
+ if (cached !== undefined)
98
+ return cached;
99
+ const resolved = config?.authorization?.resolveUserId?.() ?? null;
100
+ identityCache.set(key, resolved);
101
+ return resolved;
102
+ };
103
+ const authorization = config?.authorization;
104
+ middlewareChain.push(createRBACMiddleware(resolvedSdk.rbac, (ctx) => resolveIdentity(ctx), {
93
105
  resolveProviderByUrl: (url) => this._registry.findByUrl(url),
94
- resolveBudget: () => ({ spent: this._budgetTracker.report().totalActual }),
106
+ resolveRole: authorization?.resolveRole
107
+ ? (ctx) => authorization.resolveRole(resolveIdentity(ctx))
108
+ : undefined,
109
+ resolveBudget: authorization?.resolveBudget
110
+ ?? (() => ({ spent: this._budgetTracker.report().totalActual })),
95
111
  onDiagnostic: (diagnostic) => {
96
112
  this._authorizationDiagnostics.push(diagnostic);
97
113
  if (this._authorizationDiagnostics.length > AUTHORIZATION_DIAGNOSTIC_LIMIT) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hilbras/sdk",
3
- "version": "3.4.0",
3
+ "version": "3.4.1",
4
4
  "description": "Provider-agnostic LLM client SDK — 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",