@inferencesh/sdk 0.21.0 → 0.22.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/CHANGELOG.md +47 -0
- package/dist/agent/api.d.ts +35 -1
- package/dist/agent/api.js +35 -1
- package/dist/agent/reducer.js +23 -7
- package/dist/agent/types.d.ts +29 -4
- package/dist/api/agents.d.ts +9 -3
- package/dist/api/agents.js +87 -63
- package/dist/api/api-keys.d.ts +2 -5
- package/dist/api/apps.d.ts +9 -2
- package/dist/api/apps.js +9 -1
- package/dist/api/chats.d.ts +13 -3
- package/dist/api/chats.js +13 -2
- package/dist/api/engines.d.ts +4 -2
- package/dist/api/engines.js +3 -2
- package/dist/api/flow-runs.d.ts +7 -2
- package/dist/api/flow-runs.js +7 -2
- package/dist/api/flows.d.ts +13 -2
- package/dist/api/flows.js +15 -1
- package/dist/api/knowledge.d.ts +7 -4
- package/dist/api/knowledge.js +6 -4
- package/dist/api/mcp-servers.d.ts +2 -1
- package/dist/api/projects.d.ts +5 -3
- package/dist/api/projects.js +3 -2
- package/dist/api/secrets.d.ts +2 -2
- package/dist/api/tasks.js +38 -56
- package/dist/api/teams.d.ts +4 -2
- package/dist/api/teams.js +3 -2
- package/dist/api/write-bodies.d.ts +110 -0
- package/dist/api/write-bodies.js +1 -0
- package/dist/http/client.d.ts +1 -1
- package/dist/http/client.js +2 -1
- package/dist/http/loopback.d.ts +8 -0
- package/dist/http/loopback.js +18 -0
- package/dist/http/stream-until-terminal.d.ts +55 -0
- package/dist/http/stream-until-terminal.js +88 -0
- package/dist/http/stream.d.ts +1 -0
- package/dist/http/stream.js +7 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/proxy/endpoints.json +156 -0
- package/dist/proxy/express.d.ts +5 -6
- package/dist/proxy/express.js +18 -2
- package/dist/proxy/hono.d.ts +5 -3
- package/dist/proxy/hono.js +4 -3
- package/dist/proxy/index.d.ts +165 -6
- package/dist/proxy/index.js +409 -25
- package/dist/proxy/nextjs.d.ts +29 -17
- package/dist/proxy/nextjs.js +72 -62
- package/dist/proxy/remix.d.ts +5 -3
- package/dist/proxy/remix.js +4 -3
- package/dist/proxy/svelte.d.ts +5 -4
- package/dist/proxy/svelte.js +4 -4
- package/dist/types.d.ts +423 -6
- package/dist/types.js +111 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +7 -0
- package/package.json +2 -1
package/dist/proxy/index.d.ts
CHANGED
|
@@ -4,11 +4,18 @@
|
|
|
4
4
|
* Protects API keys by proxying requests from frontend apps through your server.
|
|
5
5
|
* Supports Next.js (App & Page Router), Express, Hono, Remix, and SvelteKit.
|
|
6
6
|
*
|
|
7
|
+
* The proxy forwards only what a frontend needs to run things (app runs,
|
|
8
|
+
* agent chats, and reading or streaming their task or chat; see
|
|
9
|
+
* PROXY_ENDPOINTS) and refuses every other API call with 403.
|
|
10
|
+
*
|
|
7
11
|
* @example Next.js App Router
|
|
8
12
|
* ```typescript
|
|
9
13
|
* // app/api/inference/proxy/route.ts
|
|
10
14
|
* import { createHandler } from "@inferencesh/sdk/proxy/nextjs";
|
|
11
|
-
* export const { GET, POST
|
|
15
|
+
* export const { GET, POST } = createHandler({
|
|
16
|
+
* allowedEndpoints: ["my-team/support-agent"],
|
|
17
|
+
* isAuthenticated: async (req) => Boolean(req.cookies.get("session")),
|
|
18
|
+
* });
|
|
12
19
|
* ```
|
|
13
20
|
*/
|
|
14
21
|
/** Header name for target URL */
|
|
@@ -17,15 +24,24 @@ export declare const INF_TARGET_HEADER = "x-inf-target-url";
|
|
|
17
24
|
export declare const INF_TARGET_PARAM = "__inf_target";
|
|
18
25
|
/** Default proxy route path */
|
|
19
26
|
export declare const PROXY_PATH = "/api/inference/proxy";
|
|
27
|
+
/** The API the proxy forwards to unless `apiUrl` or `apiBaseUrl` says otherwise. */
|
|
28
|
+
export declare const DEFAULT_API_URL = "https://api.inference.sh";
|
|
29
|
+
/**
|
|
30
|
+
* Default `maxUploadBytes`: 100 MiB, well under the API's own per-file limit
|
|
31
|
+
* (storage.max_upload_bytes, 5 GiB by default).
|
|
32
|
+
*/
|
|
33
|
+
export declare const DEFAULT_MAX_UPLOAD_BYTES: number;
|
|
20
34
|
/** Header value from various HTTP libraries */
|
|
21
35
|
export type HttpHeaderValue = string | string[] | undefined | null;
|
|
22
36
|
/**
|
|
23
37
|
* Framework adapter interface for proxy handlers.
|
|
24
38
|
* Implement this to support a new framework.
|
|
25
39
|
*/
|
|
26
|
-
export interface ProxyAdapter<T> {
|
|
40
|
+
export interface ProxyAdapter<T, R = unknown> {
|
|
27
41
|
/** Framework name for user-agent */
|
|
28
42
|
framework: string;
|
|
43
|
+
/** The framework's request, handed to `isAuthenticated` */
|
|
44
|
+
request?: R;
|
|
29
45
|
/** HTTP method of the request */
|
|
30
46
|
method: string;
|
|
31
47
|
/** Get request body as string */
|
|
@@ -44,17 +60,159 @@ export interface ProxyAdapter<T> {
|
|
|
44
60
|
respond: (response: Response) => Promise<T>;
|
|
45
61
|
/** Custom API key resolver (optional) */
|
|
46
62
|
apiKey?: () => Promise<string | undefined>;
|
|
63
|
+
/**
|
|
64
|
+
* Aborts the upstream request (optional). Adapters that copy the
|
|
65
|
+
* upstream body themselves abort it when their client disconnects, so a
|
|
66
|
+
* stream nobody reads any more (GET /chats/{id}/stream never ends on its
|
|
67
|
+
* own) does not stay open.
|
|
68
|
+
*/
|
|
69
|
+
signal?: AbortSignal;
|
|
47
70
|
}
|
|
48
71
|
/**
|
|
49
72
|
* Proxy handler options.
|
|
50
73
|
*/
|
|
51
|
-
export interface ProxyOptions {
|
|
74
|
+
export interface ProxyOptions<R = unknown> {
|
|
52
75
|
/** Custom API key (defaults to INFERENCE_API_KEY env var) */
|
|
53
76
|
apiKey?: string;
|
|
54
|
-
/**
|
|
77
|
+
/**
|
|
78
|
+
* The API the frontend's SDK client calls (its `baseUrl`), default
|
|
79
|
+
* https://api.inference.sh. The proxy attaches the key only to requests
|
|
80
|
+
* for this origin (or an `allowedDomains` host); a target on any other
|
|
81
|
+
* host, other inference.sh hosts included, is refused with 412.
|
|
82
|
+
*/
|
|
83
|
+
apiUrl?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Rewrite every target to this API base (defaults to the
|
|
86
|
+
* INFERENCE_API_BASE_URL env var). When set, it is also the only origin
|
|
87
|
+
* the proxy forwards to.
|
|
88
|
+
*/
|
|
55
89
|
apiBaseUrl?: string;
|
|
56
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* Extra target hosts, besides the API's. Each pattern must match the
|
|
92
|
+
* whole host (`host:port` when the target names a port).
|
|
93
|
+
*/
|
|
57
94
|
allowedDomains?: RegExp[];
|
|
95
|
+
/**
|
|
96
|
+
* Largest file, in bytes, a visitor may upload through the proxy
|
|
97
|
+
* (default 100 MiB). `POST /files` declares each file's size and the API
|
|
98
|
+
* binds the upload URL to it; the bytes then go straight to storage and
|
|
99
|
+
* count against the API key owner's storage quota. One file per request.
|
|
100
|
+
*/
|
|
101
|
+
maxUploadBytes?: number;
|
|
102
|
+
/**
|
|
103
|
+
* Apps and agents visitors may run, as `namespace/name` globs: "ana/helper",
|
|
104
|
+
* or "ana/*" for all of a namespace. Unset or empty allows every app and
|
|
105
|
+
* agent the API key can run.
|
|
106
|
+
*
|
|
107
|
+
* Checked where a request names the app or agent: a new app run, a new
|
|
108
|
+
* chat or agent message, a chat handed to another agent, an agent's info.
|
|
109
|
+
* Requests about an existing task or chat name it by id, and the proxy
|
|
110
|
+
* cannot tell which app or agent that is: whoever holds a task or chat id
|
|
111
|
+
* can read and stream it, cancel or stop it, and send messages in the
|
|
112
|
+
* chat (which run the chat's agent). All visitors share one key, so the
|
|
113
|
+
* id is the only boundary between them. When set, ad-hoc agent configs
|
|
114
|
+
* (`agent_config`) and runs by `app_id`/`version_id` are refused.
|
|
115
|
+
*/
|
|
116
|
+
allowedEndpoints?: string[];
|
|
117
|
+
/**
|
|
118
|
+
* Match `allowedEndpoints` entries against the whole ref, version and
|
|
119
|
+
* function included, instead of as `namespace/name` globs: the entry
|
|
120
|
+
* "ana/helper@v2" lets only that exact ref through, and "ana/helper"
|
|
121
|
+
* only the unversioned ref. Entries are refs, without `*`.
|
|
122
|
+
*/
|
|
123
|
+
exactRefs?: boolean;
|
|
124
|
+
/**
|
|
125
|
+
* The calls to forward, a subset of PROXY_ENDPOINTS (default: all of
|
|
126
|
+
* them). Entries must come from PROXY_ENDPOINTS or `pickProxyEndpoint`,
|
|
127
|
+
* which can narrow one further but never widen it; anything else
|
|
128
|
+
* refuses every request with 500.
|
|
129
|
+
*/
|
|
130
|
+
endpoints?: readonly ProxyEndpoint[];
|
|
131
|
+
/**
|
|
132
|
+
* Decides whether a request may use the API key, e.g. by checking your
|
|
133
|
+
* session. Requests it answers false for are refused with 401. Receives
|
|
134
|
+
* the framework's request (NextRequest, Express Request, Hono Context, ...).
|
|
135
|
+
*/
|
|
136
|
+
isAuthenticated?: (request: R) => boolean | Promise<boolean>;
|
|
137
|
+
/**
|
|
138
|
+
* Without `isAuthenticated`, whether anyone may use the proxy (default
|
|
139
|
+
* true, for compatibility). Set it to false to require `isAuthenticated`:
|
|
140
|
+
* with neither, every request is refused.
|
|
141
|
+
*/
|
|
142
|
+
allowUnauthorizedRequests?: boolean;
|
|
143
|
+
}
|
|
144
|
+
/** Where a request names the app or agent it runs. */
|
|
145
|
+
type RefSource =
|
|
146
|
+
/** Body field naming the app or agent, and fields that would name one another way. */
|
|
147
|
+
{
|
|
148
|
+
body: string;
|
|
149
|
+
refuse: string[];
|
|
150
|
+
}
|
|
151
|
+
/** `/agents/{namespace}/{name}` in the path. */
|
|
152
|
+
| {
|
|
153
|
+
path: true;
|
|
154
|
+
};
|
|
155
|
+
/** One call of the API's proxy manifest (src/proxy/endpoints.json). */
|
|
156
|
+
export interface ProxyManifestEndpoint {
|
|
157
|
+
method: string;
|
|
158
|
+
/** API path template: `{namespace}`/`{name}` are an agent ref, every other `{…}` an id. */
|
|
159
|
+
path: string;
|
|
160
|
+
purpose: string;
|
|
161
|
+
/** Scopes the call takes: what the site's API key must hold (the manifest's preset grants them all). */
|
|
162
|
+
scopes: string[];
|
|
163
|
+
}
|
|
164
|
+
export interface ProxyEndpoint {
|
|
165
|
+
method: "GET" | "POST";
|
|
166
|
+
/** The manifest's route, "METHOD /path/{id}" (what pickProxyEndpoint takes). */
|
|
167
|
+
route: string;
|
|
168
|
+
/** API path, matched whole. */
|
|
169
|
+
path: RegExp;
|
|
170
|
+
/** What the endpoint is for (used in docs and tests). */
|
|
171
|
+
purpose: string;
|
|
172
|
+
/** Scopes the call takes, from the manifest. */
|
|
173
|
+
scopes: readonly string[];
|
|
174
|
+
/** Set when the request names an app or agent: checked against allowedEndpoints. */
|
|
175
|
+
ref?: RefSource;
|
|
176
|
+
/** Set when only these body fields may be sent. */
|
|
177
|
+
onlyFields?: string[];
|
|
178
|
+
/** Further checks of the body (a JSON object), given the proxy's limits. */
|
|
179
|
+
validate?: (body: Record<string, unknown>, limits: ProxyLimits) => PolicyRefusal | undefined;
|
|
180
|
+
}
|
|
181
|
+
/** Limits from the proxy options, handed to endpoint checks. */
|
|
182
|
+
export interface ProxyLimits {
|
|
183
|
+
maxUploadBytes: number;
|
|
184
|
+
}
|
|
185
|
+
/** What the proxy checks of a call's request, beyond the manifest. */
|
|
186
|
+
type EndpointRules = Pick<ProxyEndpoint, "ref" | "onlyFields" | "validate">;
|
|
187
|
+
/**
|
|
188
|
+
* The proxy's own checks, by manifest route. A route here that the manifest
|
|
189
|
+
* does not list is never forwarded (a test keeps the two in step).
|
|
190
|
+
*/
|
|
191
|
+
export declare const PROXY_ENDPOINT_RULES: Readonly<Record<string, EndpointRules>>;
|
|
192
|
+
/** The API's proxy manifest as shipped in this package. */
|
|
193
|
+
export declare const PROXY_MANIFEST: {
|
|
194
|
+
readonly preset: string;
|
|
195
|
+
readonly endpoints: readonly ProxyManifestEndpoint[];
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* The calls the proxy forwards: exactly the manifest's, with the proxy's
|
|
199
|
+
* request checks. Everything else is refused with 403.
|
|
200
|
+
*/
|
|
201
|
+
export declare const PROXY_ENDPOINTS: readonly ProxyEndpoint[];
|
|
202
|
+
/** The PROXY_ENDPOINTS entry for a request, or undefined when the proxy does not forward it. */
|
|
203
|
+
export declare function matchProxyEndpoint(method: string, pathname: string, endpoints?: readonly ProxyEndpoint[]): ProxyEndpoint | undefined;
|
|
204
|
+
/**
|
|
205
|
+
* The PROXY_ENDPOINTS entry for `route` ("METHOD /path", with `{id}`,
|
|
206
|
+
* `{namespace}` and `{name}` placeholders, e.g. "POST /chats/{id}/messages"),
|
|
207
|
+
* optionally narrowed: `onlyFields` must be a subset of the entry's own,
|
|
208
|
+
* and `validate` runs after the entry's own check. For the `endpoints`
|
|
209
|
+
* option. Throws when the proxy does not forward the route.
|
|
210
|
+
*/
|
|
211
|
+
export declare function pickProxyEndpoint(route: string, narrow?: Pick<ProxyEndpoint, "onlyFields" | "validate">): ProxyEndpoint;
|
|
212
|
+
/** A refusal: the HTTP status and the message sent back. */
|
|
213
|
+
export interface PolicyRefusal {
|
|
214
|
+
status: number;
|
|
215
|
+
error: string;
|
|
58
216
|
}
|
|
59
217
|
/**
|
|
60
218
|
* Process a proxied request to the Inference.sh API.
|
|
@@ -66,7 +224,7 @@ export interface ProxyOptions {
|
|
|
66
224
|
* @param options - Optional configuration
|
|
67
225
|
* @returns Promise resolving to the framework's response type
|
|
68
226
|
*/
|
|
69
|
-
export declare function processProxyRequest<T>(adapter: ProxyAdapter<T>, options?: ProxyOptions): Promise<T>;
|
|
227
|
+
export declare function processProxyRequest<T, R = unknown>(adapter: ProxyAdapter<T, R>, options?: ProxyOptions<R>): Promise<T>;
|
|
70
228
|
/**
|
|
71
229
|
* Convert Headers object to plain record.
|
|
72
230
|
*/
|
|
@@ -95,3 +253,4 @@ export declare const fromHeaders: typeof headersToRecord;
|
|
|
95
253
|
export declare const responsePassthrough: (res: Response) => Promise<Response>;
|
|
96
254
|
/** @deprecated Use getEnvApiKey */
|
|
97
255
|
export declare const resolveApiKeyFromEnv: () => Promise<string | undefined>;
|
|
256
|
+
export {};
|