@hilbras/sdk 3.5.0 → 3.6.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 +17 -10
- package/dist/core/execution/request-pipeline.js +138 -73
- package/dist/features/agent/plan-and-execute.js +1 -1
- package/dist/features/agent/react.js +1 -1
- package/dist/features/agent/tool-loop.js +1 -1
- package/dist/frameworks/angular/angular-shim.d.ts +1 -1
- package/dist/frameworks/angular/angular-shim.js +1 -1
- package/dist/frameworks/angular/chat.service.js +1 -1
- package/dist/frameworks/angular/completion.service.js +1 -1
- package/dist/frameworks/angular/object.service.js +1 -1
- package/dist/frameworks/nextjs/server.js +1 -1
- package/dist/frameworks/react-client/index.d.ts +35 -0
- package/dist/frameworks/react-client/index.js +31 -0
- package/dist/frameworks/react-client/provider.d.ts +45 -0
- package/dist/frameworks/react-client/provider.js +40 -0
- package/dist/frameworks/react-client/use-chat.d.ts +85 -0
- package/dist/frameworks/react-client/use-chat.js +202 -0
- package/dist/frameworks/react-client/use-completion.d.ts +66 -0
- package/dist/frameworks/react-client/use-completion.js +117 -0
- package/dist/frameworks/react-client/use-cost.d.ts +62 -0
- package/dist/frameworks/react-client/use-cost.js +68 -0
- package/dist/frameworks/svelte/use-chat.js +1 -1
- package/dist/frameworks/vue/use-chat.js +1 -1
- package/dist/security/audit-logger.d.ts +7 -1
- package/dist/security/audit-logger.js +7 -5
- package/dist/security/pii-guard.js +21 -1
- package/dist/security/prompt-injection-guard.js +1 -1
- package/dist/security/rate-limiter.d.ts +2 -0
- package/dist/security/rate-limiter.js +8 -1
- package/dist/security/rbac.js +2 -2
- package/dist/security/request-signer.d.ts +2 -0
- package/dist/security/request-signer.js +3 -1
- package/dist/security/url-guard.d.ts +1 -1
- package/dist/security/url-guard.js +4 -3
- package/dist/utils/sse-writer.js +1 -1
- package/package.json +9 -3
|
@@ -14,7 +14,7 @@ function generateId() {
|
|
|
14
14
|
return `msg_${Date.now()}_${_idCounter++}`;
|
|
15
15
|
}
|
|
16
16
|
export function useChat(options) {
|
|
17
|
-
const { api, initialMessages = [], provider, model, onFinish, onError, headers = {}, body = {}, maxSteps = 1, onToolCall,
|
|
17
|
+
const { api, initialMessages = [], provider, model, onFinish, onError, headers = {}, body = {}, maxSteps = 1, onToolCall, } = options;
|
|
18
18
|
const state = {
|
|
19
19
|
messages: [...initialMessages],
|
|
20
20
|
input: "",
|
|
@@ -11,7 +11,7 @@ function generateId() {
|
|
|
11
11
|
return `msg_${Date.now()}_${_idCounter++}`;
|
|
12
12
|
}
|
|
13
13
|
export function useChat(options) {
|
|
14
|
-
const { api, initialMessages = [], provider, model, onFinish, onError, headers = {}, body = {}, maxSteps = 1, onToolCall,
|
|
14
|
+
const { api, initialMessages = [], provider, model, onFinish, onError, headers = {}, body = {}, maxSteps = 1, onToolCall, } = options;
|
|
15
15
|
const messages = ref([...initialMessages]);
|
|
16
16
|
const input = ref("");
|
|
17
17
|
const isLoading = ref(false);
|
|
@@ -80,8 +80,14 @@ export interface SecurityAuditEntry extends AuditBase {
|
|
|
80
80
|
/** Security event type */
|
|
81
81
|
eventType: "ssrf_blocked" | "rate_limit" | "circuit_breaker" | "invalid_input" | "pii_detected" | "custom";
|
|
82
82
|
}
|
|
83
|
+
/** Error audit entry */
|
|
84
|
+
export interface ErrorAuditEntry extends AuditBase {
|
|
85
|
+
category: "error";
|
|
86
|
+
/** The error message */
|
|
87
|
+
error: string;
|
|
88
|
+
}
|
|
83
89
|
/** Union of all audit entry types */
|
|
84
|
-
export type AuditEntry = AuthAuditEntry | DataAccessAuditEntry | ConfigChangeAuditEntry | SecurityAuditEntry;
|
|
90
|
+
export type AuditEntry = AuthAuditEntry | DataAccessAuditEntry | ConfigChangeAuditEntry | SecurityAuditEntry | ErrorAuditEntry;
|
|
85
91
|
/** Configuration for the audit logger */
|
|
86
92
|
export interface AuditLoggerConfig {
|
|
87
93
|
/** Service name included in every entry */
|
|
@@ -26,13 +26,16 @@ function generateEventId() {
|
|
|
26
26
|
const cnt = _eventCounter.toString(36).padStart(4, "0");
|
|
27
27
|
return `audit_${ts}_${cnt}`;
|
|
28
28
|
}
|
|
29
|
-
function cloneValue(value) {
|
|
29
|
+
function cloneValue(value, seen = new WeakSet()) {
|
|
30
30
|
if (Array.isArray(value))
|
|
31
|
-
return value.map((item) => cloneValue(item));
|
|
31
|
+
return value.map((item) => cloneValue(item, seen));
|
|
32
32
|
if (value && typeof value === "object") {
|
|
33
|
+
if (seen.has(value))
|
|
34
|
+
return value; // circular reference — stop recursing
|
|
35
|
+
seen.add(value);
|
|
33
36
|
const result = {};
|
|
34
37
|
for (const [key, item] of Object.entries(value))
|
|
35
|
-
result[key] = cloneValue(item);
|
|
38
|
+
result[key] = cloneValue(item, seen);
|
|
36
39
|
return result;
|
|
37
40
|
}
|
|
38
41
|
return value;
|
|
@@ -100,8 +103,7 @@ export class AuditLogger {
|
|
|
100
103
|
logError(event) {
|
|
101
104
|
const entry = {
|
|
102
105
|
...this._base("error", "critical", event.action, event),
|
|
103
|
-
|
|
104
|
-
reason: event.error,
|
|
106
|
+
error: event.error,
|
|
105
107
|
};
|
|
106
108
|
this._emit(entry);
|
|
107
109
|
}
|
|
@@ -26,9 +26,27 @@ const PII_PATTERNS = {
|
|
|
26
26
|
credit_card: /\b(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6(?:011|5\d{2}))[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/g,
|
|
27
27
|
ip_address: /\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g,
|
|
28
28
|
uk_nhs: /\b\d{3}\s?\d{3}\s?\d{4}\b/g,
|
|
29
|
-
|
|
29
|
+
// Unrolled: IBANs are at most 34 chars (2+2+7*4+4). Nested quantifiers like
|
|
30
|
+
// ([\dA-Z]{4}[\s]?){1,7} followed by [\dA-Z]{1,4} are ReDoS-vulnerable, so
|
|
31
|
+
// the repeating group is written out explicitly instead.
|
|
32
|
+
eu_iban: /\b[A-Z]{2}\d{2}[\s]?[\dA-Z]{4}[\s]?[\dA-Z]{4}[\s]?[\dA-Z]{4}[\s]?[\dA-Z]{4}[\s]?[\dA-Z]{4}[\s]?[\dA-Z]{4}[\s]?[\dA-Z]{4}[\s]?[\dA-Z]{1,4}\b/g,
|
|
30
33
|
custom: /(?!.)/, // Never matches — placeholder
|
|
31
34
|
};
|
|
35
|
+
/** Validate an NHS number with the MOD 11 check-digit algorithm. */
|
|
36
|
+
function isValidUkNhs(value) {
|
|
37
|
+
const digits = value.replace(/\s/g, "");
|
|
38
|
+
if (!/^\d{10}$/.test(digits))
|
|
39
|
+
return false;
|
|
40
|
+
let sum = 0;
|
|
41
|
+
for (let i = 0; i < 9; i += 1)
|
|
42
|
+
sum += Number(digits[i]) * (10 - i);
|
|
43
|
+
const remainder = sum % 11;
|
|
44
|
+
if (remainder === 0)
|
|
45
|
+
return Number(digits[9]) === 0;
|
|
46
|
+
if (remainder === 1)
|
|
47
|
+
return false; // check digit would be 10 — invalid
|
|
48
|
+
return Number(digits[9]) === 11 - remainder;
|
|
49
|
+
}
|
|
32
50
|
/** Mask a value by keeping first and last characters */
|
|
33
51
|
function maskValue(value) {
|
|
34
52
|
if (value.length <= 4)
|
|
@@ -71,6 +89,8 @@ export function detectPii(text, config) {
|
|
|
71
89
|
re.lastIndex++;
|
|
72
90
|
continue;
|
|
73
91
|
}
|
|
92
|
+
if (type === "uk_nhs" && !isValidUkNhs(m[0]))
|
|
93
|
+
continue;
|
|
74
94
|
matches.push({
|
|
75
95
|
type,
|
|
76
96
|
value: m[0],
|
|
@@ -56,6 +56,8 @@ export declare class RateLimiter {
|
|
|
56
56
|
private _tokens;
|
|
57
57
|
private _lastRefill;
|
|
58
58
|
constructor(config?: RateLimiterConfig);
|
|
59
|
+
/** `true` when the bucket never refills (refillRate of 0 or negative). */
|
|
60
|
+
get _isStaticBucket(): boolean;
|
|
59
61
|
/** Current token count (refills on access) */
|
|
60
62
|
get tokens(): number;
|
|
61
63
|
/**
|
|
@@ -41,6 +41,10 @@ export class RateLimiter {
|
|
|
41
41
|
this._tokens = this._maxTokens;
|
|
42
42
|
this._lastRefill = Date.now();
|
|
43
43
|
}
|
|
44
|
+
/** `true` when the bucket never refills (refillRate of 0 or negative). */
|
|
45
|
+
get _isStaticBucket() {
|
|
46
|
+
return !Number.isFinite(this._refillRate) || this._refillRate <= 0;
|
|
47
|
+
}
|
|
44
48
|
/** Current token count (refills on access) */
|
|
45
49
|
get tokens() {
|
|
46
50
|
this._refill();
|
|
@@ -57,7 +61,10 @@ export class RateLimiter {
|
|
|
57
61
|
return { allowed: true, retryAfterMs: 0 };
|
|
58
62
|
}
|
|
59
63
|
const deficit = tokensNeeded - this._tokens;
|
|
60
|
-
|
|
64
|
+
// A rate of 0 (or negative) means the bucket never refills, so a request
|
|
65
|
+
// that cannot be afforded now will never become affordable. Division by
|
|
66
|
+
// zero would otherwise yield Infinity/NaN retry-after values.
|
|
67
|
+
const retryAfterMs = this._isStaticBucket ? Infinity : Math.ceil((deficit / this._refillRate) * 1000);
|
|
61
68
|
return { allowed: false, retryAfterMs };
|
|
62
69
|
}
|
|
63
70
|
/**
|
package/dist/security/rbac.js
CHANGED
|
@@ -116,7 +116,7 @@ export function createRBACMiddleware(config, getUserId, options) {
|
|
|
116
116
|
if (roleName === null)
|
|
117
117
|
roleName = config.defaultRole ?? "default";
|
|
118
118
|
const role = config.roles?.[roleName] ?? null;
|
|
119
|
-
if (
|
|
119
|
+
if (role === null) {
|
|
120
120
|
report({
|
|
121
121
|
code: "RBAC_UNRESOLVED_ROLE",
|
|
122
122
|
message: `role "${roleName}" is not defined in the configured roles`,
|
|
@@ -203,7 +203,7 @@ export function createRBACMiddleware(config, getUserId, options) {
|
|
|
203
203
|
report,
|
|
204
204
|
});
|
|
205
205
|
if (verdict.allow)
|
|
206
|
-
return ctx.next();
|
|
206
|
+
return await ctx.next();
|
|
207
207
|
auditLogger?.logSecurity({
|
|
208
208
|
eventType: "custom",
|
|
209
209
|
action: "rbac_denied",
|
|
@@ -78,6 +78,8 @@ export interface SignedRequest {
|
|
|
78
78
|
*/
|
|
79
79
|
export declare class RequestSigner {
|
|
80
80
|
private _config;
|
|
81
|
+
/** Lazily created default replay guard, so `verifyFresh` calls without a `guard` share one persistent seen-signature cache. */
|
|
82
|
+
private _defaultGuard?;
|
|
81
83
|
constructor(config: RequestSignerConfig);
|
|
82
84
|
/** The active wire-format profile. */
|
|
83
85
|
get profile(): SignatureProfile;
|
|
@@ -56,6 +56,8 @@ function isByteBody(body) {
|
|
|
56
56
|
*/
|
|
57
57
|
export class RequestSigner {
|
|
58
58
|
_config;
|
|
59
|
+
/** Lazily created default replay guard, so `verifyFresh` calls without a `guard` share one persistent seen-signature cache. */
|
|
60
|
+
_defaultGuard;
|
|
59
61
|
constructor(config) {
|
|
60
62
|
this._config = {
|
|
61
63
|
secret: config.secret,
|
|
@@ -204,7 +206,7 @@ export class RequestSigner {
|
|
|
204
206
|
const signatureOk = this.verify(url, request, receivedSignature);
|
|
205
207
|
if (!signatureOk)
|
|
206
208
|
return { valid: false, reason: "signature does not match" };
|
|
207
|
-
const freshness = (guard ?? new ReplayGuard(guardOptions)).verify(request.headers, receivedSignature);
|
|
209
|
+
const freshness = (guard ?? (this._defaultGuard ??= new ReplayGuard(guardOptions))).verify(request.headers, receivedSignature);
|
|
208
210
|
if (!freshness.valid)
|
|
209
211
|
return { valid: false, reason: freshness.reason };
|
|
210
212
|
return { valid: true };
|
|
@@ -54,7 +54,7 @@ export declare function validateBaseUrl(url: string, options?: UrlGuardOptions):
|
|
|
54
54
|
* for this string-level guard. This function covers the case where the
|
|
55
55
|
* initial resolution itself yields a private/blocked address.
|
|
56
56
|
*/
|
|
57
|
-
export declare function validateResolvedAddress(ip: string
|
|
57
|
+
export declare function validateResolvedAddress(ip: string): UrlGuardResult;
|
|
58
58
|
/**
|
|
59
59
|
* Perform all SSRF checks and return a result suitable for passing to the
|
|
60
60
|
* transport layer.
|
|
@@ -59,8 +59,9 @@ function normalizeIpLiteral(host) {
|
|
|
59
59
|
return `${(num >>> 24) & 0xff}.${(num >>> 16) & 0xff}.${(num >>> 8) & 0xff}.${num & 0xff}`;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
-
// Octal: 0177.0.0.1 or 017700000001 —
|
|
63
|
-
|
|
62
|
+
// Octal: 0177.0.0.1 or 017700000001 — each octet must contain only octal digits (0-7);
|
|
63
|
+
// "08" and "09" are not valid octal and must not be misparsed as decimal.
|
|
64
|
+
if (/^(?:0[0-7]{1,3}\.){3}0?[0-7]{1,3}$/.test(host)) {
|
|
64
65
|
const parts = host.split(".");
|
|
65
66
|
const decimal = parts.map((p) => parseInt(p, 8));
|
|
66
67
|
if (decimal.every((p) => p >= 0 && p <= 255)) {
|
|
@@ -387,7 +388,7 @@ export function validateBaseUrl(url, options = {}) {
|
|
|
387
388
|
* for this string-level guard. This function covers the case where the
|
|
388
389
|
* initial resolution itself yields a private/blocked address.
|
|
389
390
|
*/
|
|
390
|
-
export function validateResolvedAddress(ip
|
|
391
|
+
export function validateResolvedAddress(ip) {
|
|
391
392
|
const normalized = normalizeIpLiteral(ip);
|
|
392
393
|
if (!normalized) {
|
|
393
394
|
// Not an IP literal — nothing to check at this level.
|
package/dist/utils/sse-writer.js
CHANGED
|
@@ -105,7 +105,7 @@ export function createSSEStream(chunks, options) {
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
// Flush any remaining pending tool calls
|
|
108
|
-
for (const [id
|
|
108
|
+
for (const [id] of pendingToolCalls) {
|
|
109
109
|
writeEvent(controller, encoder, { type: "tool_call_end", id });
|
|
110
110
|
}
|
|
111
111
|
// Write done terminator
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hilbras/sdk",
|
|
3
|
-
"version": "3.
|
|
4
|
-
"description": "Provider-agnostic LLM client SDK
|
|
3
|
+
"version": "3.6.1",
|
|
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",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -167,6 +167,10 @@
|
|
|
167
167
|
"types": "./dist/frameworks/react/index.d.ts",
|
|
168
168
|
"import": "./dist/frameworks/react/index.js"
|
|
169
169
|
},
|
|
170
|
+
"./react-client": {
|
|
171
|
+
"types": "./dist/frameworks/react-client/index.d.ts",
|
|
172
|
+
"import": "./dist/frameworks/react-client/index.js"
|
|
173
|
+
},
|
|
170
174
|
"./vue": {
|
|
171
175
|
"types": "./dist/frameworks/vue/index.d.ts",
|
|
172
176
|
"import": "./dist/frameworks/vue/index.js"
|
|
@@ -250,7 +254,9 @@
|
|
|
250
254
|
"check:package": "npm run build && node tools/check-package.mjs",
|
|
251
255
|
"test:root": "vitest run",
|
|
252
256
|
"build:packages": "npm run build --workspaces --if-present",
|
|
253
|
-
"test:packages": "npm run build:packages && npm run test --workspace hilbras --workspace create-hilbras-app --workspace @hilbras/
|
|
257
|
+
"test:packages": "npm run build:packages && npm run test --workspace hilbras --workspace create-hilbras-app --workspace @hilbras/ui",
|
|
258
|
+
"check:docs": "bash tools/check-doc-examples.sh",
|
|
259
|
+
"check:subpaths": "node tools/gen-subpath-table.mjs --check"
|
|
254
260
|
},
|
|
255
261
|
"engines": {
|
|
256
262
|
"node": ">=18"
|