@hilbras/sdk 3.4.0 → 3.4.2
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 +2 -2
- package/dist/client/client.d.ts +29 -1
- package/dist/client/client.js +18 -2
- package/dist/security/tool-policy.d.ts +12 -1
- package/dist/security/tool-policy.js +27 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="https://img.shields.io/badge/version-3.4.
|
|
2
|
+
<img src="https://img.shields.io/badge/version-3.4.2-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-
|
|
6
|
+
<img src="https://img.shields.io/badge/tests-1806%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
|
|
package/dist/client/client.d.ts
CHANGED
|
@@ -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.
|
package/dist/client/client.js
CHANGED
|
@@ -89,9 +89,25 @@ export class HilbrasClient {
|
|
|
89
89
|
// covered by the same policy.
|
|
90
90
|
const middlewareChain = [];
|
|
91
91
|
if (resolvedSdk.rbac) {
|
|
92
|
-
|
|
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
|
-
|
|
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) {
|
|
@@ -26,7 +26,18 @@ export interface ToolPolicyCheck {
|
|
|
26
26
|
export declare class ToolPolicy {
|
|
27
27
|
private readonly _allowed;
|
|
28
28
|
private readonly _denied;
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* @param input The allow-list and deny-list.
|
|
31
|
+
* @param options `emptyAllowListIsRestrictive` keeps an empty allow-list
|
|
32
|
+
* meaningful as "nothing is permitted". It is off by default because an
|
|
33
|
+
* empty `allowedTools` in configuration means "no restriction", and
|
|
34
|
+
* {@link narrow} needs the opposite meaning: the intersection of two
|
|
35
|
+
* allow-lists can legitimately be empty, and treating that as unrestricted
|
|
36
|
+
* would make narrowing wider instead of narrower.
|
|
37
|
+
*/
|
|
38
|
+
constructor(input?: ToolPolicyInput, options?: {
|
|
39
|
+
emptyAllowListIsRestrictive?: boolean;
|
|
40
|
+
});
|
|
30
41
|
/** True when the policy constrains nothing. */
|
|
31
42
|
isEmpty(): boolean;
|
|
32
43
|
get allowedTools(): readonly string[] | undefined;
|
|
@@ -14,10 +14,22 @@
|
|
|
14
14
|
export class ToolPolicy {
|
|
15
15
|
_allowed;
|
|
16
16
|
_denied;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
/**
|
|
18
|
+
* @param input The allow-list and deny-list.
|
|
19
|
+
* @param options `emptyAllowListIsRestrictive` keeps an empty allow-list
|
|
20
|
+
* meaningful as "nothing is permitted". It is off by default because an
|
|
21
|
+
* empty `allowedTools` in configuration means "no restriction", and
|
|
22
|
+
* {@link narrow} needs the opposite meaning: the intersection of two
|
|
23
|
+
* allow-lists can legitimately be empty, and treating that as unrestricted
|
|
24
|
+
* would make narrowing wider instead of narrower.
|
|
25
|
+
*/
|
|
26
|
+
constructor(input = {}, options = {}) {
|
|
27
|
+
const allowed = input.allowedTools;
|
|
28
|
+
this._allowed = allowed === undefined
|
|
29
|
+
? undefined
|
|
30
|
+
: allowed.length > 0 || options.emptyAllowListIsRestrictive
|
|
31
|
+
? Object.freeze([...allowed])
|
|
32
|
+
: undefined;
|
|
21
33
|
this._denied = Object.freeze([...(input.deniedTools ?? [])]);
|
|
22
34
|
}
|
|
23
35
|
/** True when the policy constrains nothing. */
|
|
@@ -55,15 +67,17 @@ export class ToolPolicy {
|
|
|
55
67
|
if (other === undefined)
|
|
56
68
|
return this;
|
|
57
69
|
const narrower = other instanceof ToolPolicy ? other : new ToolPolicy(other);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
let allowed;
|
|
71
|
+
if (this._allowed === undefined) {
|
|
72
|
+
allowed = narrower.allowedTools;
|
|
73
|
+
}
|
|
74
|
+
else if (narrower.allowedTools === undefined) {
|
|
75
|
+
allowed = this._allowed;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
allowed = this._allowed.filter((name) => narrower.allowedTools.includes(name));
|
|
79
|
+
}
|
|
80
|
+
return new ToolPolicy({ allowedTools: allowed, deniedTools: [...this._denied, ...narrower.deniedTools] }, { emptyAllowListIsRestrictive: true });
|
|
67
81
|
}
|
|
68
82
|
/**
|
|
69
83
|
* Validate a whole tool set at once. Returns the first violation, or `null`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hilbras/sdk",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.2",
|
|
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",
|