@vite-hub/gateways 0.1.0 → 0.3.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.
@@ -1,24 +1,42 @@
1
- import { A as AiGatewayContext, C as AiGatewayEnvPreset, M as AiGatewayDriver, P as AiGatewaySecret, S as AiGatewayEnvFieldName, T as aiGatewayEnvNames, at as isChatGPTGateway, b as AiGatewayTraceEvent, j as AiGatewayDefinition, l as askAiGateway } from "./presets-CY0KQOx8.js";
1
+ import { a as AiGatewayContext, c as AiGatewayRoute, o as AiGatewayDefinition, u as AiGatewaySecret } from "./types-D0loH5yz.js";
2
+ import { C as AiGatewayEnvFieldName, E as aiGatewayEnvNames, k as isChatGPTGateway, u as askAiGateway, w as AiGatewayEnvPreset, x as AiGatewayTraceEvent } from "./presets-DbTUgWyL.js";
3
+ import { Diagnostic } from "nostics";
2
4
  //#region src/definition.d.ts
3
5
  /**
4
6
  * Marks an AI gateway that answers Jev questions. A symbol key, so key checks and copies skip it. Such a gateway may
5
- * have no Driver base URL.
7
+ * have no model API base URL.
6
8
  */
7
9
  export declare const aiGatewayAsks: unique symbol;
8
- /** Validate a gateway definition and return a frozen copy with its Driver routes. */
10
+ /** Validate a gateway definition and return a frozen copy with its model API routes. */
9
11
  export declare function normalizeAiGateway(value: unknown): AiGatewayDefinition;
10
12
  /** A secret as a trimmed string, or undefined when it is missing or empty. */
11
13
  export declare function resolveAiGatewaySecret(value: AiGatewaySecret, context: AiGatewayContext): Promise<string | undefined>;
12
- /** One Driver route of a gateway, with its key and headers resolved. */
13
- interface AiGatewayDriverRoute {
14
+ /** One model API route of a gateway, with its key and headers resolved. */
15
+ interface AiGatewayResolvedRoute {
14
16
  apiKey: string;
15
17
  auth: "bearer" | "x-api-key";
16
18
  baseURL: string;
17
19
  headers: [string, string][];
18
20
  }
19
- /** Whether a gateway has a base URL for the Driver. */
20
- export declare function aiGatewaySupports(gateway: AiGatewayDefinition, driver: AiGatewayDriver): boolean;
21
- /** Resolve the base URL, key, and headers of one Driver route. The caller checks `aiGatewaySupports()` first. */
22
- export declare function resolveAiGatewayDriverRoute(gateway: AiGatewayDefinition, driver: AiGatewayDriver, context: AiGatewayContext): Promise<AiGatewayDriverRoute>;
21
+ /** A model route whose gateway may send requests without a key. See `aiGatewayOptionalCredentials`. */
22
+ interface AiGatewayModelRoute extends Omit<AiGatewayResolvedRoute, "apiKey"> {
23
+ apiKey?: string;
24
+ }
25
+ /** Whether a gateway has a base URL for the model API. */
26
+ export declare function aiGatewaySupports(gateway: AiGatewayDefinition, route: AiGatewayRoute): boolean;
27
+ /** Resolve the base URL, key, and headers of one model API route. The caller checks `aiGatewaySupports()` first. */
28
+ export declare function resolveAiGatewayRoute(gateway: AiGatewayDefinition, route: AiGatewayRoute, context: AiGatewayContext): Promise<AiGatewayResolvedRoute>;
29
+ /**
30
+ * Resolve a route for an AI SDK model. A gateway with `aiGatewayOptionalCredentials` may resolve without its key or
31
+ * its optional headers, but not without all of them.
32
+ */
33
+ export declare function resolveAiGatewayModelRoute(gateway: AiGatewayDefinition, route: AiGatewayRoute, context: AiGatewayContext): Promise<AiGatewayModelRoute>;
34
+ //#endregion
35
+ //#region src/public-diagnostics.d.ts
36
+ /** Client-safe text captured when this package creates a known gateway diagnostic. */
37
+ export declare function getPublicAiGatewayDiagnostic(cause: unknown): {
38
+ message: string;
39
+ fix?: string;
40
+ } | undefined;
23
41
  //#endregion
24
- export { type AiGatewayDriverRoute, type AiGatewayEnvFieldName, type AiGatewayEnvPreset, type AiGatewayTraceEvent, aiGatewayEnvNames, askAiGateway, isChatGPTGateway };
42
+ export { type AiGatewayEnvFieldName, type AiGatewayEnvPreset, type AiGatewayModelRoute, type AiGatewayResolvedRoute, type AiGatewayTraceEvent, aiGatewayEnvNames, askAiGateway, isChatGPTGateway };
package/dist/internal.js CHANGED
@@ -1,2 +1,3 @@
1
- import { _ as resolveAiGatewaySecret, g as resolveAiGatewayDriverRoute, h as normalizeAiGateway, m as aiGatewaySupports, n as askAiGateway, p as aiGatewayAsks, x as isChatGPTGateway, y as aiGatewayEnvNames } from "./presets-B2Xutktt.js";
2
- export { aiGatewayAsks, aiGatewayEnvNames, aiGatewaySupports, askAiGateway, isChatGPTGateway, normalizeAiGateway, resolveAiGatewayDriverRoute, resolveAiGatewaySecret };
1
+ import { c as resolveAiGatewayRoute, d as getPublicAiGatewayDiagnostic, i as aiGatewaySupports, l as resolveAiGatewaySecret, o as normalizeAiGateway, s as resolveAiGatewayModelRoute, t as aiGatewayAsks } from "./definition-BVc8HZWs.js";
2
+ import { g as isChatGPTGateway, m as aiGatewayEnvNames, n as askAiGateway } from "./presets-DltoYDBd.js";
3
+ export { aiGatewayAsks, aiGatewayEnvNames, aiGatewaySupports, askAiGateway, getPublicAiGatewayDiagnostic, isChatGPTGateway, normalizeAiGateway, resolveAiGatewayModelRoute, resolveAiGatewayRoute, resolveAiGatewaySecret };
@@ -0,0 +1,189 @@
1
+ import { D as AskQuestions, a as AiGatewayContext, c as AiGatewayRoute, d as AiGatewayUsage, f as AiGatewayValue, h as AskAnswers, o as AiGatewayDefinition, t as AiGateway, u as AiGatewaySecret } from "./types-D0loH5yz.js";
2
+ //#region src/chatgpt.d.ts
3
+ /** A runtime-owned ChatGPT plan route. It contains no credentials. */
4
+ interface ChatGPTGateway {
5
+ readonly kind: "chatgpt";
6
+ readonly name: "chatgpt";
7
+ readonly connection: string;
8
+ }
9
+ /** Use a ChatGPT Connection with a Codex Driver or a model-backed Driver. Defaults to the `chatgpt` Connection. */
10
+ declare function chatgpt(options?: {
11
+ connection?: string;
12
+ }): ChatGPTGateway;
13
+ declare function isChatGPTGateway(value: unknown): value is ChatGPTGateway;
14
+ //#endregion
15
+ //#region src/env.d.ts
16
+ /** One preset value that is read from the environment when the preset does not receive it. */
17
+ interface AiGatewayEnvField {
18
+ /** Vendor variable names in lookup order, after the `VITEHUB_` name. */
19
+ names: readonly [string, ...string[]];
20
+ secret?: true;
21
+ }
22
+ /**
23
+ * The variables that each preset reads, keyed by preset and then by field. ViteHub declares the presets that Agent files
24
+ * use in Server Env as `env.server.<preset>.<field>`.
25
+ */
26
+ /** The presets that read the environment. */
27
+ type AiGatewayEnvPreset = "anthropic" | "cliproxy" | "cloudflare" | "cloudflareAccess" | "litellm" | "ollama" | "openai" | "openrouter" | "typesafe" | "vercel";
28
+ declare const aiGatewayEnv: Readonly<Record<AiGatewayEnvPreset, Readonly<Record<string, AiGatewayEnvField>>>>;
29
+ /** A field of a preset, such as `apiKey`. */
30
+ type AiGatewayEnvFieldName<_TPreset extends AiGatewayEnvPreset = AiGatewayEnvPreset> = string;
31
+ /** The variables of one preset value in lookup order: `VITEHUB_<PRESET>_<FIELD>`, then the vendor names. */
32
+ declare function aiGatewayEnvNames<TPreset extends AiGatewayEnvPreset>(preset: TPreset, field: AiGatewayEnvFieldName<TPreset>): string[];
33
+ //#endregion
34
+ //#region src/jev.d.ts
35
+ /** One trace event of a Jev request. ViteHub Agents record these in the Invocation trace. */
36
+ interface AiGatewayTraceEvent {
37
+ attributes: Record<string, unknown>;
38
+ name: string;
39
+ type: "error" | "run";
40
+ }
41
+ interface JevRequestContext {
42
+ abortSignal?: AbortSignal;
43
+ /** Receives one `agent.tool.*` event per question and one `agent.usage.recorded` event per Jev request. */
44
+ observe?: (event: AiGatewayTraceEvent) => Promise<void>;
45
+ /** Receives the usage of each Jev request: its model, provider, and token counts. */
46
+ recordUsage?: (usage: AiGatewayUsage) => void;
47
+ /** Resolves the route of the gateway for this request. */
48
+ route: () => Promise<JevRoute>;
49
+ }
50
+ /**
51
+ * One resolved Jev route. `protocol` selects the advocaat client: the TypeSafe API (also behind a Cloudflare AI Gateway
52
+ * custom provider) or Vercel AI Gateway.
53
+ */
54
+ interface JevRoute {
55
+ apiKey?: string;
56
+ baseURL?: string;
57
+ fetch?: typeof globalThis.fetch;
58
+ headers?: Record<string, string>;
59
+ /** The variables that hold the credentials, for the fix of a rejected key. Undefined when the caller passed them. */
60
+ keyEnv?: string;
61
+ /** The explicit option to rotate when the caller passed the key. */
62
+ keyOption?: string;
63
+ /** Shown in diagnostics, such as "Cloudflare AI Gateway". */
64
+ label: string;
65
+ model?: string;
66
+ /** The gateway name in usage records and traces, such as `cloudflare`. */
67
+ name: string;
68
+ protocol: "typesafe" | "vercel";
69
+ }
70
+ //#endregion
71
+ //#region src/presets.d.ts
72
+ /** Options shared by every gateway preset. */
73
+ interface AiGatewayPresetOptions {
74
+ /** API key. When it is not set, the preset reads its Server Env value, such as `CLIPROXY_API_KEY`. */
75
+ apiKey?: AiGatewaySecret;
76
+ /** Extra request headers, such as `cloudflareAccess()`. */
77
+ headers?: Record<string, AiGatewaySecret>;
78
+ }
79
+ /** Options for a gateway that you host. */
80
+ interface AiHostedGatewayOptions extends AiGatewayPresetOptions {
81
+ /** Origin of the gateway, such as `https://proxy.example.com`. A trailing `/v1` is removed. When it is not set, the preset reads its Server Env value, such as `CLIPROXY_URL`. */
82
+ url?: string;
83
+ }
84
+ /** Options for a gateway that answers TypeSafe Jev questions. */
85
+ interface AiAskGatewayOptions {
86
+ /** Custom fetch implementation for Jev requests. Default: `globalThis.fetch`. */
87
+ fetch?: typeof globalThis.fetch;
88
+ /** Jev model ID for `ask()`. Default: `jev-latest`, or `typesafe-ai/jev` through Vercel AI Gateway. */
89
+ model?: string;
90
+ }
91
+ /** Options for `typesafe()`. */
92
+ interface TypesafeGatewayOptions extends AiAskGatewayOptions, AiGatewayPresetOptions {}
93
+ /** Options for `vercel()`. `model` selects the Jev model of `ask()`; Driver models come from `driver.model`. */
94
+ interface VercelGatewayOptions extends AiAskGatewayOptions, AiGatewayPresetOptions {}
95
+ /** Options for `cloudflare()`. Each value defaults to its Server Env value. */
96
+ interface CloudflareGatewayOptions extends AiAskGatewayOptions {
97
+ /** Cloudflare account ID. Defaults to `CLOUDFLARE_ACCOUNT_ID`. */
98
+ accountId?: string;
99
+ /** A Cloudflare gateway reads two keys. Pass `typesafeApiKey` for `ask()` or `providerApiKey` for models. */
100
+ apiKey?: never;
101
+ /** AI Gateway ID. Defaults to `CLOUDFLARE_AI_GATEWAY_ID`. */
102
+ gatewayId?: string;
103
+ /** Extra request headers, such as `cloudflareAccess()`. */
104
+ headers?: Record<string, AiGatewaySecret>;
105
+ /**
106
+ * Provider key of model requests, sent as `Authorization: Bearer`. Defaults to `CLOUDFLARE_AI_GATEWAY_API_KEY`. Leave it
107
+ * unset when the gateway stores the provider keys or uses Unified Billing; the gateway token then authenticates.
108
+ */
109
+ providerApiKey?: AiGatewaySecret;
110
+ /** Custom provider slug of `ask()`; the gateway URL uses `custom-<slug>`. Defaults to `CLOUDFLARE_AI_GATEWAY_SLUG`, then `typesafe`. */
111
+ slug?: string;
112
+ /** Gateway token of an authenticated gateway, sent as `cf-aig-authorization`. Defaults to `CLOUDFLARE_AI_GATEWAY_TOKEN`. */
113
+ token?: AiGatewaySecret;
114
+ /** TypeSafe API key that the gateway forwards for `ask()`. Defaults to `TYPESAFE_API_KEY`. */
115
+ typesafeApiKey?: AiGatewaySecret;
116
+ }
117
+ /** Options for `cloudflareAccess()`. Each value defaults to its Server Env value. */
118
+ interface CloudflareAccessOptions {
119
+ /** Service token client ID. Defaults to `CF_ACCESS_CLIENT_ID`. */
120
+ clientId?: AiGatewaySecret;
121
+ /** Service token client secret. Defaults to `CF_ACCESS_CLIENT_SECRET`. */
122
+ clientSecret?: AiGatewaySecret;
123
+ }
124
+ /** The model APIs of a gateway that you host. */
125
+ type HostedRoutes = "claude-code" | "codex" | "openai-compatible";
126
+ /** Asks through a gateway with a resolver context, observers, and usage records. ViteHub Agents use it for `driver.ask`. */
127
+ declare function askAiGateway<const Q extends AskQuestions>(gateway: AiGateway, state: unknown, questions: Q, options?: {
128
+ context?: AiGatewayContext;
129
+ observe?: JevRequestContext["observe"];
130
+ onUsage?: (usage: AiGatewayUsage) => void;
131
+ signal?: AbortSignal;
132
+ }): Promise<AskAnswers<Q>>;
133
+ /** A gateway that routes model requests to `TRoutes` and has no `ask()`. */
134
+ type AiModelGateway<TRoutes extends AiGatewayRoute> = AiGateway<{
135
+ ask: false;
136
+ routes: TRoutes;
137
+ }>;
138
+ /**
139
+ * Validate a custom gateway for an endpoint without a preset. Its type lists the model APIs in `baseURL`. It routes
140
+ * model requests; only the `typesafe()`, `vercel()`, and `cloudflare()` presets answer Jev questions.
141
+ */
142
+ declare function defineAiGateway<const TRoutes extends AiGatewayRoute>(gateway: Omit<AiGatewayDefinition, "baseURL"> & {
143
+ baseURL: Record<TRoutes, AiGatewayValue<string>>;
144
+ }): AiModelGateway<TRoutes>;
145
+ /**
146
+ * Cloudflare Access service-token headers for a gateway behind Access.
147
+ * Reads `CF_ACCESS_CLIENT_ID` and `CF_ACCESS_CLIENT_SECRET` unless you pass the values.
148
+ */
149
+ declare function cloudflareAccess(options?: CloudflareAccessOptions): Record<string, AiGatewaySecret>;
150
+ /** [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI). Serves Codex and Claude Code. Reads `CLIPROXY_URL` and `CLIPROXY_API_KEY`. */
151
+ declare function cliproxy(options?: AiHostedGatewayOptions): AiModelGateway<HostedRoutes>;
152
+ /** [LiteLLM Proxy](https://docs.litellm.ai/docs/simple_proxy). Serves Codex and Claude Code. Reads `LITELLM_URL` and `LITELLM_API_KEY`. */
153
+ declare function litellm(options?: AiHostedGatewayOptions): AiModelGateway<HostedRoutes>;
154
+ /** [Ollama](https://docs.ollama.com). Serves Codex and Claude Code. Reads `OLLAMA_URL` and `OLLAMA_API_KEY`; defaults to `http://localhost:11434` without a key. */
155
+ declare function ollama(options?: AiHostedGatewayOptions): AiModelGateway<HostedRoutes>;
156
+ /** [OpenRouter](https://openrouter.ai/docs). Serves Codex and Claude Code. Reads `OPENROUTER_API_KEY`. */
157
+ declare function openrouter(options?: AiGatewayPresetOptions): AiModelGateway<HostedRoutes>;
158
+ /**
159
+ * [Vercel AI Gateway](https://vercel.com/docs/ai-gateway). Serves Codex, Claude Code, and AI SDK models, and answers
160
+ * Jev questions as `typesafe-ai/jev`. Reads `AI_GATEWAY_API_KEY`, then `VERCEL_OIDC_TOKEN`.
161
+ */
162
+ declare function vercel(options?: VercelGatewayOptions): AiGateway<{
163
+ ask: true;
164
+ routes: HostedRoutes;
165
+ }>;
166
+ /** The OpenAI API with an API key. Serves Codex and AI SDK models. Reads `OPENAI_API_KEY`. */
167
+ declare function openai(options?: AiGatewayPresetOptions): AiModelGateway<"codex" | "openai-compatible">;
168
+ /** The Anthropic API with an API key. Serves Claude Code and AI SDK models. Reads `ANTHROPIC_API_KEY`. */
169
+ declare function anthropic(options?: AiGatewayPresetOptions): AiModelGateway<"claude-code">;
170
+ /** [TypeSafe](https://typesafe.ai). Answers Jev questions; it serves no model API. Reads `TYPESAFE_API_KEY` and `TYPESAFE_DEFAULT_MODEL`. */
171
+ declare function typesafe(options?: TypesafeGatewayOptions): AiGateway<{
172
+ ask: true;
173
+ routes: never;
174
+ }>;
175
+ /**
176
+ * [Cloudflare AI Gateway](https://developers.cloudflare.com/ai-gateway/). Serves AI SDK models through its
177
+ * [OpenAI-compatible endpoint](https://developers.cloudflare.com/ai-gateway/usage/chat-completion/), with
178
+ * Chat Completions (`api: "chat"`) only, with `{provider}/{model}` model IDs such as `workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast`. Answers Jev questions
179
+ * through a TypeSafe [custom provider](https://developers.cloudflare.com/ai-gateway/configuration/custom-providers/).
180
+ * Each capability reads its own key: model requests need the gateway token, `CLOUDFLARE_AI_GATEWAY_API_KEY`, or both;
181
+ * `ask()` needs `TYPESAFE_API_KEY`. For Codex or Claude Code through Cloudflare, use `defineAiGateway()`.
182
+ */
183
+ declare function cloudflare(options?: CloudflareGatewayOptions): AiGateway<{
184
+ apis: "chat";
185
+ ask: true;
186
+ routes: "openai-compatible";
187
+ }>;
188
+ //#endregion
189
+ export { AiGatewayEnvFieldName as C, ChatGPTGateway as D, aiGatewayEnvNames as E, chatgpt as O, AiGatewayEnvField as S, aiGatewayEnv as T, openai as _, CloudflareAccessOptions as a, vercel as b, VercelGatewayOptions as c, cliproxy as d, cloudflare as f, ollama as g, litellm as h, AiModelGateway as i, isChatGPTGateway as k, anthropic as l, defineAiGateway as m, AiGatewayPresetOptions as n, CloudflareGatewayOptions as o, cloudflareAccess as p, AiHostedGatewayOptions as r, TypesafeGatewayOptions as s, AiAskGatewayOptions as t, askAiGateway as u, openrouter as v, AiGatewayEnvPreset as w, AiGatewayTraceEvent as x, typesafe as y };