@hilbras/sdk 3.4.4 → 3.5.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 +2 -2
- package/dist/frameworks/astro/endpoints.js +58 -16
- package/dist/frameworks/astro/index.d.ts +1 -0
- package/dist/frameworks/nextjs/api/index.d.ts +15 -0
- package/dist/frameworks/nextjs/api/index.js +14 -0
- package/dist/frameworks/nextjs/api/stream.d.ts +44 -0
- package/dist/frameworks/nextjs/api/stream.js +150 -0
- package/dist/frameworks/nextjs/edge/index.d.ts +12 -0
- package/dist/frameworks/nextjs/edge/index.js +11 -0
- package/dist/frameworks/nextjs/edge/middleware.d.ts +85 -0
- package/dist/frameworks/nextjs/edge/middleware.js +113 -0
- package/dist/frameworks/nextjs/index.d.ts +1 -0
- package/dist/frameworks/nextjs/next-runtime.d.ts +63 -0
- package/dist/frameworks/nextjs/next-runtime.js +41 -0
- package/dist/frameworks/nextjs/route-handlers.js +51 -27
- package/dist/frameworks/nextjs/server.d.ts +24 -1
- package/dist/frameworks/nextjs/server.js +24 -1
- package/dist/frameworks/remix/actions.js +58 -16
- package/dist/frameworks/remix/index.d.ts +1 -0
- package/dist/frameworks/shared/handler-core.d.ts +143 -2
- package/dist/frameworks/shared/handler-core.js +134 -5
- package/package.json +10 -2
|
@@ -17,6 +17,14 @@
|
|
|
17
17
|
import { HilbrasClient } from "../../client/client.js";
|
|
18
18
|
import { getProviderCatalog, listProviders } from "../../catalog/index.js";
|
|
19
19
|
import { ConfigurationError, RequestValidationError } from "../../errors/index.js";
|
|
20
|
+
export const DEFAULT_HANDLER_LIMITS = Object.freeze({
|
|
21
|
+
maxBodyBytes: 1024 * 1024,
|
|
22
|
+
maxMessages: 200,
|
|
23
|
+
maxTools: 64,
|
|
24
|
+
maxStepsClamp: 25,
|
|
25
|
+
maxTokensClamp: 32768,
|
|
26
|
+
temperatureClamp: [0, 2],
|
|
27
|
+
});
|
|
20
28
|
/** Resolve a provider spelling to its canonical catalog id. */
|
|
21
29
|
export function resolveCatalogProviderId(provider) {
|
|
22
30
|
return (listProviders().find((id) => id.toLowerCase() === provider.toLowerCase()) ?? provider);
|
|
@@ -77,23 +85,63 @@ export function createFrameworkClientPool(options) {
|
|
|
77
85
|
};
|
|
78
86
|
return pool;
|
|
79
87
|
}
|
|
88
|
+
/** Merge caller-supplied limits over the defaults. */
|
|
89
|
+
export function resolveLimits(overrides) {
|
|
90
|
+
if (!overrides)
|
|
91
|
+
return { ...DEFAULT_HANDLER_LIMITS };
|
|
92
|
+
return {
|
|
93
|
+
...DEFAULT_HANDLER_LIMITS,
|
|
94
|
+
...overrides,
|
|
95
|
+
temperatureClamp: [
|
|
96
|
+
overrides.temperatureClamp?.[0] ?? DEFAULT_HANDLER_LIMITS.temperatureClamp[0],
|
|
97
|
+
overrides.temperatureClamp?.[1] ?? DEFAULT_HANDLER_LIMITS.temperatureClamp[1],
|
|
98
|
+
],
|
|
99
|
+
};
|
|
100
|
+
}
|
|
80
101
|
/**
|
|
81
|
-
* Read
|
|
102
|
+
* Read the request body as text, rejecting anything over `maxBodyBytes`.
|
|
82
103
|
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
104
|
+
* `content-length` is consulted first because it is free, but it is only a hint:
|
|
105
|
+
* a request with no header, or a lying one, is still measured by reading the
|
|
106
|
+
* body. The host normally caps `Content-Length` for us, so it cannot be relied
|
|
107
|
+
* on to reject an oversized body before it is buffered.
|
|
85
108
|
*/
|
|
86
|
-
export async function
|
|
109
|
+
export async function readBodyText(request, limits = DEFAULT_HANDLER_LIMITS) {
|
|
110
|
+
const declared = Number(request.headers.get("content-length") ?? Number.NaN);
|
|
111
|
+
if (Number.isFinite(declared) && declared > limits.maxBodyBytes) {
|
|
112
|
+
throw new RequestValidationError(`Request body exceeds the ${limits.maxBodyBytes} byte limit`, "Raise `limits.maxBodyBytes` on the handler factory if this is expected.", 413);
|
|
113
|
+
}
|
|
87
114
|
let raw;
|
|
88
115
|
try {
|
|
89
|
-
|
|
116
|
+
if (typeof request.text === "function") {
|
|
117
|
+
raw = await request.text();
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
raw = new TextDecoder().decode(await request.arrayBuffer());
|
|
121
|
+
}
|
|
90
122
|
}
|
|
91
123
|
catch {
|
|
92
124
|
throw new RequestValidationError("Request body could not be read");
|
|
93
125
|
}
|
|
126
|
+
// `text()` yields a UTF-16 string, so measure the encoded byte length rather
|
|
127
|
+
// than the character count: a body of multi-byte characters has fewer
|
|
128
|
+
// characters than bytes, and comparing lengths would under-count it.
|
|
129
|
+
if (new TextEncoder().encode(raw).byteLength > limits.maxBodyBytes) {
|
|
130
|
+
throw new RequestValidationError(`Request body exceeds the ${limits.maxBodyBytes} byte limit`, "Raise `limits.maxBodyBytes` on the handler factory if this is expected.", 413);
|
|
131
|
+
}
|
|
94
132
|
if (raw.trim().length === 0) {
|
|
95
133
|
throw new RequestValidationError("Request body is empty", "Send a JSON object.");
|
|
96
134
|
}
|
|
135
|
+
return raw;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Read and parse a JSON request body, enforcing `maxBodyBytes`.
|
|
139
|
+
*
|
|
140
|
+
* Malformed JSON previously threw a bare `SyntaxError` out of the handler, so
|
|
141
|
+
* the response depended entirely on the host framework's error handling.
|
|
142
|
+
*/
|
|
143
|
+
export async function readJsonBody(request, limits = DEFAULT_HANDLER_LIMITS) {
|
|
144
|
+
const raw = await readBodyText(request, limits);
|
|
97
145
|
try {
|
|
98
146
|
const parsed = JSON.parse(raw);
|
|
99
147
|
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
@@ -123,6 +171,87 @@ export function requireArray(body, field) {
|
|
|
123
171
|
}
|
|
124
172
|
return value;
|
|
125
173
|
}
|
|
174
|
+
/** Clamp a number into `[min, max]`. */
|
|
175
|
+
function clampNumber(value, min, max) {
|
|
176
|
+
return Math.min(Math.max(value, min), max);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Resolve the fields a chat request may contribute.
|
|
180
|
+
*
|
|
181
|
+
* This is the single place that decides what a caller is allowed to influence,
|
|
182
|
+
* which is why all four chat-shaped handlers share it. `trustClientFields`
|
|
183
|
+
* selects *whether* the body is consulted; the limits bound *how far* it is
|
|
184
|
+
* trusted.
|
|
185
|
+
*/
|
|
186
|
+
export function resolveChatFields(body, options) {
|
|
187
|
+
const messages = requireArray(body, "messages");
|
|
188
|
+
if (messages.length > options.limits.maxMessages) {
|
|
189
|
+
throw new RequestValidationError(`Request has ${messages.length} messages, exceeding the limit of ${options.limits.maxMessages}`, "Raise `limits.maxMessages` on the handler factory if this is expected.");
|
|
190
|
+
}
|
|
191
|
+
const trust = options.trustClientFields;
|
|
192
|
+
const model = trust && typeof body.model === "string" && body.model.length > 0
|
|
193
|
+
? body.model
|
|
194
|
+
: options.model;
|
|
195
|
+
let tools = options.tools;
|
|
196
|
+
if (tools && tools.length > options.limits.maxTools) {
|
|
197
|
+
throw new RequestValidationError(`Handler is configured with ${tools.length} tools, exceeding the limit of ${options.limits.maxTools}`, "Raise `limits.maxTools` on the handler factory if this is expected.");
|
|
198
|
+
}
|
|
199
|
+
if (trust && Array.isArray(body.tools)) {
|
|
200
|
+
if (body.tools.length > options.limits.maxTools) {
|
|
201
|
+
throw new RequestValidationError(`Request has ${body.tools.length} tools, exceeding the limit of ${options.limits.maxTools}`, "Raise `limits.maxTools` on the handler factory if this is expected.");
|
|
202
|
+
}
|
|
203
|
+
tools = body.tools;
|
|
204
|
+
}
|
|
205
|
+
const [tempMin, tempMax] = options.limits.temperatureClamp;
|
|
206
|
+
const maxStepsRaw = trust && typeof body.maxSteps === "number" ? body.maxSteps : options.maxSteps;
|
|
207
|
+
return {
|
|
208
|
+
model,
|
|
209
|
+
messages,
|
|
210
|
+
tools,
|
|
211
|
+
maxSteps: typeof maxStepsRaw === "number" ? Math.min(maxStepsRaw, options.limits.maxStepsClamp) : undefined,
|
|
212
|
+
maxTokens: trust && typeof body.maxTokens === "number"
|
|
213
|
+
? Math.min(body.maxTokens, options.limits.maxTokensClamp)
|
|
214
|
+
: undefined,
|
|
215
|
+
temperature: trust && typeof body.temperature === "number"
|
|
216
|
+
? clampNumber(body.temperature, tempMin, tempMax)
|
|
217
|
+
: undefined,
|
|
218
|
+
stream: body.stream !== false,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Resolve the fields a completion request may contribute.
|
|
223
|
+
*
|
|
224
|
+
* `stream` is resolved but not yet acted on: the completion handlers answer with
|
|
225
|
+
* a JSON body either way, so a caller sending `stream: true` is served the same
|
|
226
|
+
* documented shape rather than a half-implemented stream.
|
|
227
|
+
*/
|
|
228
|
+
export function resolveCompletionFields(body, options) {
|
|
229
|
+
const prompt = requireString(body, "prompt");
|
|
230
|
+
const trust = options.trustClientFields;
|
|
231
|
+
const [tempMin, tempMax] = options.limits.temperatureClamp;
|
|
232
|
+
return {
|
|
233
|
+
model: trust && typeof body.model === "string" && body.model.length > 0
|
|
234
|
+
? body.model
|
|
235
|
+
: options.model,
|
|
236
|
+
prompt,
|
|
237
|
+
maxTokens: trust && typeof body.maxTokens === "number"
|
|
238
|
+
? Math.min(body.maxTokens, options.limits.maxTokensClamp)
|
|
239
|
+
: undefined,
|
|
240
|
+
temperature: trust && typeof body.temperature === "number"
|
|
241
|
+
? clampNumber(body.temperature, tempMin, tempMax)
|
|
242
|
+
: undefined,
|
|
243
|
+
stream: body.stream !== false,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Classify an error for the `onError` hook.
|
|
248
|
+
*
|
|
249
|
+
* A validation failure — including one raised by `onRequest` — is the caller's
|
|
250
|
+
* fault and is not worth an error-level log; anything else is.
|
|
251
|
+
*/
|
|
252
|
+
export function isRequestPhaseError(error) {
|
|
253
|
+
return error instanceof RequestValidationError;
|
|
254
|
+
}
|
|
126
255
|
/**
|
|
127
256
|
* Map a thrown error to an HTTP response.
|
|
128
257
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hilbras/sdk",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
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",
|
|
@@ -191,6 +191,14 @@
|
|
|
191
191
|
"types": "./dist/frameworks/nextjs/index.d.ts",
|
|
192
192
|
"import": "./dist/frameworks/nextjs/index.js"
|
|
193
193
|
},
|
|
194
|
+
"./nextjs/api": {
|
|
195
|
+
"types": "./dist/frameworks/nextjs/api/index.d.ts",
|
|
196
|
+
"import": "./dist/frameworks/nextjs/api/index.js"
|
|
197
|
+
},
|
|
198
|
+
"./nextjs/edge": {
|
|
199
|
+
"types": "./dist/frameworks/nextjs/edge/index.d.ts",
|
|
200
|
+
"import": "./dist/frameworks/nextjs/edge/index.js"
|
|
201
|
+
},
|
|
194
202
|
"./astro": {
|
|
195
203
|
"types": "./dist/frameworks/astro/index.d.ts",
|
|
196
204
|
"import": "./dist/frameworks/astro/index.js"
|
|
@@ -242,7 +250,7 @@
|
|
|
242
250
|
"check:package": "npm run build && node tools/check-package.mjs",
|
|
243
251
|
"test:root": "vitest run",
|
|
244
252
|
"build:packages": "npm run build --workspaces --if-present",
|
|
245
|
-
"test:packages": "npm run build:packages && npm run test --workspace hilbras --workspace create-hilbras-app --workspace @hilbras/
|
|
253
|
+
"test:packages": "npm run build:packages && npm run test --workspace hilbras --workspace create-hilbras-app --workspace @hilbras/react --workspace @hilbras/ui"
|
|
246
254
|
},
|
|
247
255
|
"engines": {
|
|
248
256
|
"node": ">=18"
|