@hilbras/sdk 3.4.3 → 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/errors/index.d.ts +12 -0
- package/dist/errors/index.js +16 -0
- package/dist/frameworks/astro/endpoints.d.ts +6 -6
- package/dist/frameworks/astro/endpoints.js +114 -43
- 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.d.ts +29 -22
- package/dist/frameworks/nextjs/route-handlers.js +114 -51
- package/dist/frameworks/nextjs/server.d.ts +24 -1
- package/dist/frameworks/nextjs/server.js +24 -1
- package/dist/frameworks/remix/actions.d.ts +12 -10
- package/dist/frameworks/remix/actions.js +116 -43
- package/dist/frameworks/remix/index.d.ts +1 -0
- package/dist/frameworks/shared/handler-core.d.ts +232 -0
- package/dist/frameworks/shared/handler-core.js +295 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +10 -2
|
@@ -4,72 +4,145 @@
|
|
|
4
4
|
* Remix action helpers for streaming chat/completion.
|
|
5
5
|
* Uses the HilbrasClient to generate streaming responses with full protocol support.
|
|
6
6
|
*/
|
|
7
|
-
import { HilbrasClient } from "../../client/client.js";
|
|
8
7
|
import { createSSEResponse } from "../../utils/sse-writer.js";
|
|
8
|
+
import { createFrameworkClientPool, errorResponse, isRequestPhaseError, readJsonBody, resolveChatFields, resolveCompletionFields, resolveLimits, } from "../shared/handler-core.js";
|
|
9
9
|
/**
|
|
10
10
|
* Create a streaming chat action for Remix.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* ```ts
|
|
14
14
|
* // app/routes/api.chat.ts
|
|
15
|
-
* import { createChatAction } from "@hilbras/remix";
|
|
15
|
+
* import { createChatAction } from "@hilbras/sdk/remix";
|
|
16
16
|
*
|
|
17
17
|
* export const action = createChatAction({
|
|
18
|
-
* provider: "
|
|
18
|
+
* provider: "openai",
|
|
19
19
|
* model: "gpt-4o",
|
|
20
20
|
* });
|
|
21
21
|
* ```
|
|
22
22
|
*/
|
|
23
23
|
export function createChatAction(options) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
const pool = createFrameworkClientPool({
|
|
25
|
+
provider: options.provider,
|
|
26
|
+
model: options.model,
|
|
27
|
+
apiKey: options.apiKey,
|
|
28
|
+
});
|
|
29
|
+
const limits = resolveLimits(options.limits);
|
|
30
|
+
// This adapter never read `model`/`tools`/`maxSteps` from the body in v3.4, so
|
|
31
|
+
// its default is `false`. Defaulting to `true` would have widened the
|
|
32
|
+
// caller's influence over a route that did not previously have any.
|
|
33
|
+
const trustClientFields = options.trustClientFields ?? false;
|
|
34
|
+
const action = async ({ request }) => {
|
|
35
|
+
try {
|
|
36
|
+
const body = await readJsonBody(request, limits);
|
|
37
|
+
const fields = resolveChatFields(body, {
|
|
35
38
|
model: options.model,
|
|
36
|
-
|
|
39
|
+
tools: options.tools,
|
|
40
|
+
maxSteps: options.maxSteps,
|
|
41
|
+
trustClientFields,
|
|
42
|
+
limits,
|
|
37
43
|
});
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
})
|
|
42
|
-
|
|
44
|
+
const client = pool.get(fields.model);
|
|
45
|
+
const providerName = pool.providerName;
|
|
46
|
+
const effective = { ...fields, provider: providerName };
|
|
47
|
+
await options.onRequest?.({ request, body, effective });
|
|
48
|
+
const allMessages = options.systemPrompt
|
|
49
|
+
? [{ role: "system", content: options.systemPrompt }, ...fields.messages]
|
|
50
|
+
: fields.messages;
|
|
51
|
+
if (!effective.stream) {
|
|
52
|
+
const result = await client.complete({
|
|
53
|
+
provider: providerName,
|
|
54
|
+
model: fields.model,
|
|
55
|
+
messages: allMessages,
|
|
56
|
+
temperature: fields.temperature,
|
|
57
|
+
maxTokens: fields.maxTokens,
|
|
58
|
+
signal: request.signal,
|
|
59
|
+
});
|
|
60
|
+
return new Response(JSON.stringify({
|
|
61
|
+
id: `cmpl-${Date.now()}`,
|
|
62
|
+
choices: [{ message: { role: "assistant", content: result }, finishReason: "stop" }],
|
|
63
|
+
}), {
|
|
64
|
+
headers: { "Content-Type": "application/json" },
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const chunks = client.streamText({
|
|
68
|
+
provider: providerName,
|
|
69
|
+
model: fields.model,
|
|
70
|
+
messages: allMessages,
|
|
71
|
+
temperature: fields.temperature,
|
|
72
|
+
maxTokens: fields.maxTokens,
|
|
73
|
+
tools: fields.tools,
|
|
74
|
+
maxSteps: fields.maxSteps,
|
|
75
|
+
signal: request.signal,
|
|
43
76
|
});
|
|
77
|
+
return createSSEResponse(chunks);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
if (options.onError) {
|
|
81
|
+
await options.onError(error, {
|
|
82
|
+
request,
|
|
83
|
+
phase: isRequestPhaseError(error) ? "request" : "provider",
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return errorResponse(error);
|
|
44
87
|
}
|
|
45
|
-
const chunks = client.streamText({
|
|
46
|
-
provider: options.provider,
|
|
47
|
-
model: options.model,
|
|
48
|
-
messages: allMessages,
|
|
49
|
-
tools: options.tools,
|
|
50
|
-
maxSteps: options.maxSteps,
|
|
51
|
-
});
|
|
52
|
-
return createSSEResponse(chunks);
|
|
53
88
|
};
|
|
89
|
+
async function dispose() {
|
|
90
|
+
await pool.dispose();
|
|
91
|
+
}
|
|
92
|
+
return Object.assign(action, { dispose });
|
|
54
93
|
}
|
|
55
94
|
/**
|
|
56
95
|
* Create a non-streaming completion action for Remix.
|
|
57
96
|
*/
|
|
58
97
|
export function createCompletionAction(options) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
98
|
+
const pool = createFrameworkClientPool({
|
|
99
|
+
provider: options.provider,
|
|
100
|
+
model: options.model,
|
|
101
|
+
apiKey: options.apiKey,
|
|
102
|
+
});
|
|
103
|
+
const limits = resolveLimits(options.limits);
|
|
104
|
+
// This adapter never read `model`/`tools`/`maxSteps` from the body in v3.4, so
|
|
105
|
+
// its default is `false`. Defaulting to `true` would have widened the
|
|
106
|
+
// caller's influence over a route that did not previously have any.
|
|
107
|
+
const trustClientFields = options.trustClientFields ?? false;
|
|
108
|
+
const action = async ({ request }) => {
|
|
109
|
+
try {
|
|
110
|
+
const body = await readJsonBody(request, limits);
|
|
111
|
+
const fields = resolveCompletionFields(body, {
|
|
112
|
+
model: options.model,
|
|
113
|
+
trustClientFields,
|
|
114
|
+
limits,
|
|
115
|
+
});
|
|
116
|
+
const client = pool.get(fields.model);
|
|
117
|
+
const effective = { ...fields, provider: pool.providerName };
|
|
118
|
+
await options.onRequest?.({ request, body, effective });
|
|
119
|
+
const result = await client.complete({
|
|
120
|
+
provider: pool.providerName,
|
|
121
|
+
model: fields.model,
|
|
122
|
+
messages: [{ role: "user", content: fields.prompt }],
|
|
123
|
+
temperature: fields.temperature,
|
|
124
|
+
maxTokens: fields.maxTokens,
|
|
125
|
+
signal: request.signal,
|
|
126
|
+
});
|
|
127
|
+
return new Response(JSON.stringify({
|
|
128
|
+
id: `cmpl-${Date.now()}`,
|
|
129
|
+
choices: [{ message: { role: "assistant", content: result }, finishReason: "stop" }],
|
|
130
|
+
}), {
|
|
131
|
+
headers: { "Content-Type": "application/json" },
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
if (options.onError) {
|
|
136
|
+
await options.onError(error, {
|
|
137
|
+
request,
|
|
138
|
+
phase: isRequestPhaseError(error) ? "request" : "provider",
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return errorResponse(error);
|
|
142
|
+
}
|
|
74
143
|
};
|
|
144
|
+
async function dispose() {
|
|
145
|
+
await pool.dispose();
|
|
146
|
+
}
|
|
147
|
+
return Object.assign(action, { dispose });
|
|
75
148
|
}
|
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
* Actions and hooks for Remix.
|
|
5
5
|
*/
|
|
6
6
|
export { createChatAction, createCompletionAction, type ActionOptions, type ChatMessage } from "./actions.js";
|
|
7
|
+
export type { FrameworkErrorContext, FrameworkHandlerLimits, FrameworkRequestContext, } from "../shared/handler-core.js";
|
|
7
8
|
export { createChatHook, type UseChatOptions, type UseChatReturn, type Message } from "./hooks.js";
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — Shared framework handler core
|
|
3
|
+
*
|
|
4
|
+
* Every framework adapter (Next.js route handlers, Astro endpoints, Remix
|
|
5
|
+
* actions) previously re-implemented the same request handling. `actions.ts`
|
|
6
|
+
* was byte-for-byte identical to `endpoints.ts` across 33 lines, and all three
|
|
7
|
+
* shared the same defects: they discarded the canonical provider name returned
|
|
8
|
+
* by `addProviderFromCatalog` and then called with the caller's spelling, they
|
|
9
|
+
* built a fresh client per request and never disposed it, they read a hardcoded
|
|
10
|
+
* environment variable, and they reported configuration failures as an HTTP 200
|
|
11
|
+
* with the error buried in the stream body.
|
|
12
|
+
*
|
|
13
|
+
* This module owns the parts that must behave identically everywhere. Framework
|
|
14
|
+
* files keep only what is genuinely framework-specific: how to read the request
|
|
15
|
+
* and how to build a `Response`.
|
|
16
|
+
*/
|
|
17
|
+
import { HilbrasClient } from "../../client/client.js";
|
|
18
|
+
import type { Tool } from "../../types/tools.js";
|
|
19
|
+
/** Options common to every framework handler factory. */
|
|
20
|
+
export interface FrameworkHandlerBaseOptions {
|
|
21
|
+
/** Catalog provider id or display name, e.g. `"openai"` or `"OpenAI"`. */
|
|
22
|
+
provider: string;
|
|
23
|
+
/** Model id used when the request does not select one. */
|
|
24
|
+
model: string;
|
|
25
|
+
/** Prepended to the conversation as a system message. */
|
|
26
|
+
systemPrompt?: string;
|
|
27
|
+
/**
|
|
28
|
+
* API key. When omitted, `AI_API_KEY` is tried, then the provider's own
|
|
29
|
+
* catalog `envKey` (for example `OPENAI_API_KEY`).
|
|
30
|
+
*/
|
|
31
|
+
apiKey?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Whether fields in the request body may override the factory's own options.
|
|
34
|
+
*
|
|
35
|
+
* `true` (the default, and the v3.4 behaviour) lets a caller choose
|
|
36
|
+
* `body.model`, `body.temperature`, `body.maxTokens`, `body.tools` and
|
|
37
|
+
* `body.maxSteps`. `false` ignores all of them and uses only the values
|
|
38
|
+
* configured on the factory, which is what a public route wants.
|
|
39
|
+
*
|
|
40
|
+
* The default flips to `false` in 4.0.0.
|
|
41
|
+
*/
|
|
42
|
+
trustClientFields?: boolean;
|
|
43
|
+
/** Bounded request limits. Every field is individually overridable. */
|
|
44
|
+
limits?: Partial<FrameworkHandlerLimits>;
|
|
45
|
+
/**
|
|
46
|
+
* Called once per request after the body is validated and before any provider
|
|
47
|
+
* call. Throw to reject the request; `errorResponse` maps the thrown error, so
|
|
48
|
+
* a `RequestValidationError` carrying 401 or 403 becomes that status and
|
|
49
|
+
* anything else becomes a generic 500.
|
|
50
|
+
*
|
|
51
|
+
* This is the authentication and authorization extension point.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* createChatHandler({
|
|
56
|
+
* provider: "openai",
|
|
57
|
+
* model: "gpt-4o",
|
|
58
|
+
* onRequest({ request }) {
|
|
59
|
+
* if (request.headers.get("authorization") !== `Bearer ${process.env.SESSION}`) {
|
|
60
|
+
* throw new RequestValidationError("Unauthorized", undefined, 401);
|
|
61
|
+
* }
|
|
62
|
+
* },
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
onRequest?: (context: FrameworkRequestContext) => void | Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Called for every request that fails, including one rejected by `onRequest`.
|
|
69
|
+
* This is where the server-side log belongs: `errorResponse` deliberately
|
|
70
|
+
* withholds detail from the response body, so without this hook a provider
|
|
71
|
+
* error is invisible to the operator.
|
|
72
|
+
*/
|
|
73
|
+
onError?: (error: unknown, context: FrameworkErrorContext) => void | Promise<void>;
|
|
74
|
+
}
|
|
75
|
+
/** What an `onRequest` hook receives. */
|
|
76
|
+
export interface FrameworkRequestContext {
|
|
77
|
+
/** The incoming request. */
|
|
78
|
+
request: Request;
|
|
79
|
+
/** The parsed JSON body. */
|
|
80
|
+
body: Record<string, unknown>;
|
|
81
|
+
/**
|
|
82
|
+
* The fields the handler will actually use, after `trustClientFields` and the
|
|
83
|
+
* limits have been applied. A hook that authorizes per-model should read this
|
|
84
|
+
* rather than `body`, because these are the values that reach the provider.
|
|
85
|
+
*/
|
|
86
|
+
effective: FrameworkEffectiveFields;
|
|
87
|
+
}
|
|
88
|
+
/** The resolved request parameters, after allowlist and limit handling. */
|
|
89
|
+
export interface FrameworkEffectiveFields {
|
|
90
|
+
model: string;
|
|
91
|
+
provider: string;
|
|
92
|
+
messages?: unknown[];
|
|
93
|
+
prompt?: string;
|
|
94
|
+
tools?: Tool[];
|
|
95
|
+
maxSteps?: number;
|
|
96
|
+
maxTokens?: number;
|
|
97
|
+
temperature?: number;
|
|
98
|
+
stream: boolean;
|
|
99
|
+
}
|
|
100
|
+
/** What an `onError` hook receives. */
|
|
101
|
+
export interface FrameworkErrorContext {
|
|
102
|
+
request: Request;
|
|
103
|
+
/** `"request"` for a caller or validation failure, `"provider"` otherwise. */
|
|
104
|
+
phase: "request" | "provider";
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Bounds on what a single request may ask for.
|
|
108
|
+
*
|
|
109
|
+
* The defaults are deliberately generous — no realistic chat request reaches
|
|
110
|
+
* them — so enabling them does not change the behaviour of an existing route,
|
|
111
|
+
* while still bounding the worst case an unauthenticated caller can force.
|
|
112
|
+
*/
|
|
113
|
+
export interface FrameworkHandlerLimits {
|
|
114
|
+
/** Maximum request body size in bytes. Default 1 MiB. */
|
|
115
|
+
maxBodyBytes: number;
|
|
116
|
+
/** Maximum messages in a chat request. Default 200. */
|
|
117
|
+
maxMessages: number;
|
|
118
|
+
/** Maximum tools in a chat request. Default 64. */
|
|
119
|
+
maxTools: number;
|
|
120
|
+
/** Upper clamp applied to `maxSteps`. Default 25. */
|
|
121
|
+
maxStepsClamp: number;
|
|
122
|
+
/** Upper clamp applied to `maxTokens`. Default 32768. */
|
|
123
|
+
maxTokensClamp: number;
|
|
124
|
+
/** Inclusive clamp applied to `temperature`. Default `[0, 2]`. */
|
|
125
|
+
temperatureClamp: [number, number];
|
|
126
|
+
}
|
|
127
|
+
export declare const DEFAULT_HANDLER_LIMITS: Readonly<FrameworkHandlerLimits>;
|
|
128
|
+
/** Resolve a provider spelling to its canonical catalog id. */
|
|
129
|
+
export declare function resolveCatalogProviderId(provider: string): string;
|
|
130
|
+
/**
|
|
131
|
+
* Resolve the API key for a provider.
|
|
132
|
+
*
|
|
133
|
+
* Precedence: an explicit `apiKey`, then the shared `AI_API_KEY`, then the
|
|
134
|
+
* provider's catalog `envKey`. The previous hardcoded `process.env.AI_API_KEY`
|
|
135
|
+
* only meant an operator using the documented `OPENAI_API_KEY` naming got an
|
|
136
|
+
* empty key.
|
|
137
|
+
*/
|
|
138
|
+
export declare function resolveApiKey(provider: string, explicit?: string): string;
|
|
139
|
+
/**
|
|
140
|
+
* A lazily-built, cached `HilbrasClient` per `provider::model` pair.
|
|
141
|
+
*
|
|
142
|
+
* Reusing a client preserves its connection pool and its adapter registry.
|
|
143
|
+
* Building one per request discarded both, and left a live cleanup interval
|
|
144
|
+
* behind for every request served.
|
|
145
|
+
*/
|
|
146
|
+
export interface FrameworkClientPool {
|
|
147
|
+
/** Get a client with the requested provider and model registered. */
|
|
148
|
+
get(model: string): HilbrasClient;
|
|
149
|
+
/** The canonical registered provider name, for provider-scoped calls. */
|
|
150
|
+
readonly providerName: string;
|
|
151
|
+
/** Release every cached client. */
|
|
152
|
+
dispose(): Promise<void>;
|
|
153
|
+
}
|
|
154
|
+
export declare function createFrameworkClientPool(options: {
|
|
155
|
+
provider: string;
|
|
156
|
+
model: string;
|
|
157
|
+
apiKey?: string;
|
|
158
|
+
client?: HilbrasClient;
|
|
159
|
+
}): FrameworkClientPool;
|
|
160
|
+
/** Merge caller-supplied limits over the defaults. */
|
|
161
|
+
export declare function resolveLimits(overrides?: Partial<FrameworkHandlerLimits>): FrameworkHandlerLimits;
|
|
162
|
+
/**
|
|
163
|
+
* Read the request body as text, rejecting anything over `maxBodyBytes`.
|
|
164
|
+
*
|
|
165
|
+
* `content-length` is consulted first because it is free, but it is only a hint:
|
|
166
|
+
* a request with no header, or a lying one, is still measured by reading the
|
|
167
|
+
* body. The host normally caps `Content-Length` for us, so it cannot be relied
|
|
168
|
+
* on to reject an oversized body before it is buffered.
|
|
169
|
+
*/
|
|
170
|
+
export declare function readBodyText(request: Request, limits?: FrameworkHandlerLimits): Promise<string>;
|
|
171
|
+
/**
|
|
172
|
+
* Read and parse a JSON request body, enforcing `maxBodyBytes`.
|
|
173
|
+
*
|
|
174
|
+
* Malformed JSON previously threw a bare `SyntaxError` out of the handler, so
|
|
175
|
+
* the response depended entirely on the host framework's error handling.
|
|
176
|
+
*/
|
|
177
|
+
export declare function readJsonBody(request: Request, limits?: FrameworkHandlerLimits): Promise<Record<string, unknown>>;
|
|
178
|
+
/** Read a required non-empty string field from a parsed body. */
|
|
179
|
+
export declare function requireString(body: Record<string, unknown>, field: string): string;
|
|
180
|
+
/** Read a required array field from a parsed body. */
|
|
181
|
+
export declare function requireArray(body: Record<string, unknown>, field: string): unknown[];
|
|
182
|
+
/**
|
|
183
|
+
* Resolve the fields a chat request may contribute.
|
|
184
|
+
*
|
|
185
|
+
* This is the single place that decides what a caller is allowed to influence,
|
|
186
|
+
* which is why all four chat-shaped handlers share it. `trustClientFields`
|
|
187
|
+
* selects *whether* the body is consulted; the limits bound *how far* it is
|
|
188
|
+
* trusted.
|
|
189
|
+
*/
|
|
190
|
+
export declare function resolveChatFields(body: Record<string, unknown>, options: {
|
|
191
|
+
model: string;
|
|
192
|
+
tools?: Tool[];
|
|
193
|
+
maxSteps?: number;
|
|
194
|
+
trustClientFields: boolean;
|
|
195
|
+
limits: FrameworkHandlerLimits;
|
|
196
|
+
}): Omit<FrameworkEffectiveFields, "provider">;
|
|
197
|
+
/**
|
|
198
|
+
* Resolve the fields a completion request may contribute.
|
|
199
|
+
*
|
|
200
|
+
* `stream` is resolved but not yet acted on: the completion handlers answer with
|
|
201
|
+
* a JSON body either way, so a caller sending `stream: true` is served the same
|
|
202
|
+
* documented shape rather than a half-implemented stream.
|
|
203
|
+
*/
|
|
204
|
+
export declare function resolveCompletionFields(body: Record<string, unknown>, options: {
|
|
205
|
+
model: string;
|
|
206
|
+
trustClientFields: boolean;
|
|
207
|
+
limits: FrameworkHandlerLimits;
|
|
208
|
+
}): Omit<FrameworkEffectiveFields, "provider" | "messages" | "tools" | "maxSteps">;
|
|
209
|
+
/**
|
|
210
|
+
* Classify an error for the `onError` hook.
|
|
211
|
+
*
|
|
212
|
+
* A validation failure — including one raised by `onRequest` — is the caller's
|
|
213
|
+
* fault and is not worth an error-level log; anything else is.
|
|
214
|
+
*/
|
|
215
|
+
export declare function isRequestPhaseError(error: unknown): boolean;
|
|
216
|
+
/**
|
|
217
|
+
* Map a thrown error to an HTTP response.
|
|
218
|
+
*
|
|
219
|
+
* Two classes of error are reported with their message:
|
|
220
|
+
*
|
|
221
|
+
* - `RequestValidationError`, a caller error, whose message names the offending
|
|
222
|
+
* field and nothing else.
|
|
223
|
+
* - `ConfigurationError`, an operator-config error. Its text is built from the
|
|
224
|
+
* handler's own options, so it is safe to return and it is what makes a typo
|
|
225
|
+
* in `createChatHandler({ provider })` debuggable at all.
|
|
226
|
+
*
|
|
227
|
+
* Everything else — including any provider-side failure — becomes a generic 500
|
|
228
|
+
* with no detail, so an internal message, provider response body, or key cannot
|
|
229
|
+
* leak through a public route. Route the detail to a server-side log or an
|
|
230
|
+
* `onError` hook instead.
|
|
231
|
+
*/
|
|
232
|
+
export declare function errorResponse(error: unknown): Response;
|