@vite-hub/gateways 0.4.1 → 0.5.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 +69 -15
- package/dist/ai-sdk.d.ts +4 -5
- package/dist/ai-sdk.js +68 -25
- package/dist/{definition-BVc8HZWs.js → definition-IHIWW_eG.js} +18 -129
- package/dist/diagnostics-CNIdO5-M.js +121 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +5 -15
- package/dist/internal.d.ts +14 -7
- package/dist/internal.js +4 -3
- package/dist/jev-Kmwvxt8l.js +420 -0
- package/dist/presets-BowIdgYT.js +504 -0
- package/dist/{presets-QQzoSUSu.d.ts → presets-CmCCGdfk.d.ts} +21 -17
- package/dist/testing.d.ts +46 -0
- package/dist/testing.js +152 -0
- package/dist/{types-CF5dF7X1.d.ts → types-jSw-t3Dq.d.ts} +0 -8
- package/package.json +16 -9
- package/dist/presets-CRYxSoMw.js +0 -900
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
import { t as gatewayDiagnostics } from "./diagnostics-CNIdO5-M.js";
|
|
2
|
+
import { a as aiGatewayOptionalCredentials, c as isRecord, f as resolveAiGatewaySecret, i as aiGatewayOpenAIChat, l as normalizeAiGateway, o as aiGatewayStructuredOutputs, r as aiGatewayModelCredentials, t as aiGatewayAsks } from "./definition-IHIWW_eG.js";
|
|
3
|
+
import { n as cloudflareJevBaseURL, t as askJev } from "./jev-Kmwvxt8l.js";
|
|
4
|
+
// src/chatgpt.ts
|
|
5
|
+
/** Use a ChatGPT Connection with a Codex Driver or a model-backed Driver. Defaults to the `chatgpt` Connection. */
|
|
6
|
+
function chatgpt(options = {}) {
|
|
7
|
+
const connection = options.connection ?? "chatgpt";
|
|
8
|
+
if (Object.keys(options).some((key) => key !== "connection") || typeof connection !== "string" || !connection.trim() || connection !== connection.trim()) throw gatewayDiagnostics.GATEWAYS_R0001({ message: "[vitehub] chatgpt() accepts only a non-empty Connection name: chatgpt({ connection: \"chatgpt\" })." });
|
|
9
|
+
return Object.freeze({
|
|
10
|
+
kind: "chatgpt",
|
|
11
|
+
name: "chatgpt",
|
|
12
|
+
connection
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function isChatGPTGateway(value) {
|
|
16
|
+
return typeof value === "object" && value !== null && "kind" in value && value.kind === "chatgpt" && "name" in value && value.name === "chatgpt" && "connection" in value && typeof value.connection === "string" && value.connection.length > 0 && value.connection === value.connection.trim() && Object.keys(value).every((key) => [
|
|
17
|
+
"kind",
|
|
18
|
+
"name",
|
|
19
|
+
"connection"
|
|
20
|
+
].includes(key));
|
|
21
|
+
}
|
|
22
|
+
// src/env.ts
|
|
23
|
+
const aiGatewayEnv = {
|
|
24
|
+
anthropic: { apiKey: {
|
|
25
|
+
names: ["ANTHROPIC_API_KEY"],
|
|
26
|
+
secret: true
|
|
27
|
+
} },
|
|
28
|
+
cliproxy: {
|
|
29
|
+
apiKey: {
|
|
30
|
+
names: ["CLIPROXY_API_KEY"],
|
|
31
|
+
secret: true
|
|
32
|
+
},
|
|
33
|
+
url: { names: ["CLIPROXY_URL"] }
|
|
34
|
+
},
|
|
35
|
+
cloudflare: {
|
|
36
|
+
accountId: { names: ["CLOUDFLARE_ACCOUNT_ID"] },
|
|
37
|
+
gatewayId: { names: ["CLOUDFLARE_AI_GATEWAY_ID"] },
|
|
38
|
+
providerApiKey: {
|
|
39
|
+
names: ["CLOUDFLARE_AI_GATEWAY_API_KEY"],
|
|
40
|
+
secret: true
|
|
41
|
+
},
|
|
42
|
+
slug: { names: ["CLOUDFLARE_AI_GATEWAY_SLUG"] },
|
|
43
|
+
token: {
|
|
44
|
+
names: ["CLOUDFLARE_AI_GATEWAY_TOKEN"],
|
|
45
|
+
secret: true
|
|
46
|
+
},
|
|
47
|
+
typesafeApiKey: {
|
|
48
|
+
names: ["TYPESAFE_API_KEY"],
|
|
49
|
+
secret: true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
cloudflareAccess: {
|
|
53
|
+
clientId: {
|
|
54
|
+
names: ["CF_ACCESS_CLIENT_ID"],
|
|
55
|
+
secret: true
|
|
56
|
+
},
|
|
57
|
+
clientSecret: {
|
|
58
|
+
names: ["CF_ACCESS_CLIENT_SECRET"],
|
|
59
|
+
secret: true
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
emulate: { url: { names: ["VITEHUB_EMULATE_URL"] } },
|
|
63
|
+
litellm: {
|
|
64
|
+
apiKey: {
|
|
65
|
+
names: ["LITELLM_API_KEY"],
|
|
66
|
+
secret: true
|
|
67
|
+
},
|
|
68
|
+
url: { names: ["LITELLM_URL"] }
|
|
69
|
+
},
|
|
70
|
+
ollama: {
|
|
71
|
+
apiKey: {
|
|
72
|
+
names: ["OLLAMA_API_KEY"],
|
|
73
|
+
secret: true
|
|
74
|
+
},
|
|
75
|
+
url: { names: ["OLLAMA_URL"] }
|
|
76
|
+
},
|
|
77
|
+
openai: { apiKey: {
|
|
78
|
+
names: ["OPENAI_API_KEY"],
|
|
79
|
+
secret: true
|
|
80
|
+
} },
|
|
81
|
+
openrouter: { apiKey: {
|
|
82
|
+
names: ["OPENROUTER_API_KEY"],
|
|
83
|
+
secret: true
|
|
84
|
+
} },
|
|
85
|
+
typesafe: {
|
|
86
|
+
apiKey: {
|
|
87
|
+
names: ["TYPESAFE_API_KEY"],
|
|
88
|
+
secret: true
|
|
89
|
+
},
|
|
90
|
+
model: { names: ["TYPESAFE_DEFAULT_MODEL"] }
|
|
91
|
+
},
|
|
92
|
+
vercel: {
|
|
93
|
+
apiKey: {
|
|
94
|
+
names: ["AI_GATEWAY_API_KEY"],
|
|
95
|
+
secret: true
|
|
96
|
+
},
|
|
97
|
+
oidcToken: {
|
|
98
|
+
names: ["VERCEL_OIDC_TOKEN"],
|
|
99
|
+
secret: true
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const fields = aiGatewayEnv;
|
|
104
|
+
function nameParts(segment) {
|
|
105
|
+
return segment.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part.toUpperCase());
|
|
106
|
+
}
|
|
107
|
+
/** The variables of one preset value in lookup order: `VITEHUB_<PRESET>_<FIELD>`, then the vendor names. */
|
|
108
|
+
function aiGatewayEnvNames(preset, field) {
|
|
109
|
+
const vendor = fields[preset]?.[field]?.names ?? [];
|
|
110
|
+
return [.../* @__PURE__ */ new Set([[
|
|
111
|
+
"VITEHUB",
|
|
112
|
+
...nameParts(preset),
|
|
113
|
+
...nameParts(field)
|
|
114
|
+
].join("_"), ...vendor])];
|
|
115
|
+
}
|
|
116
|
+
/** The vendor names of one preset value, for diagnostics. */
|
|
117
|
+
function aiGatewayVendorNames(preset, field) {
|
|
118
|
+
return fields[preset]?.[field]?.names ?? [];
|
|
119
|
+
}
|
|
120
|
+
/** One preset value from `context.env`, else from `process.env`. An empty variable counts as unset. */
|
|
121
|
+
async function readAiGatewayEnv(context, preset, field) {
|
|
122
|
+
if (context.env) return await context.env(preset, field);
|
|
123
|
+
return aiGatewayEnvNames(preset, field).map((name) => globalThis.process?.env?.[name]).find((value) => value !== void 0 && value !== "");
|
|
124
|
+
}
|
|
125
|
+
// src/presets.ts
|
|
126
|
+
function origin(url, label) {
|
|
127
|
+
if (typeof url !== "string" || !url.trim()) throw gatewayDiagnostics.GATEWAYS_R0001({ message: `[vitehub] ${label}({ url }) must be a non-empty URL.` });
|
|
128
|
+
return url.trim().replace(/\/+$/, "").replace(/\/v1$/, "");
|
|
129
|
+
}
|
|
130
|
+
function present(value) {
|
|
131
|
+
return isRecord(value) || typeof value === "string" && value.trim() !== "";
|
|
132
|
+
}
|
|
133
|
+
function requiredEnv(preset, field, missing) {
|
|
134
|
+
return async (context) => {
|
|
135
|
+
const value = await readAiGatewayEnv(context, preset, field);
|
|
136
|
+
return present(value) ? value : missing(aiGatewayVendorNames(preset, field).join(" or "));
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function apiKeyFromEnv(gateway) {
|
|
140
|
+
return requiredEnv(gateway, "apiKey", (names) => {
|
|
141
|
+
throw gatewayDiagnostics.GATEWAYS_R0002({ message: `[vitehub] Gateway "${gateway}" needs an API key. Set ${names}, or pass apiKey.` });
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function hostedBaseURL(gateway, url, fallback) {
|
|
145
|
+
if (url !== void 0 || fallback !== void 0) {
|
|
146
|
+
const root = origin(url ?? fallback, gateway);
|
|
147
|
+
return {
|
|
148
|
+
"codex": `${root}/v1`,
|
|
149
|
+
"claude-code": root,
|
|
150
|
+
"openai-compatible": `${root}/v1`
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
const read = requiredEnv(gateway, "url", (names) => {
|
|
154
|
+
throw gatewayDiagnostics.GATEWAYS_R0004({ message: `[vitehub] Gateway "${gateway}" needs a URL. Set ${names}, or pass url.` });
|
|
155
|
+
});
|
|
156
|
+
return {
|
|
157
|
+
"codex": async (context) => `${origin(await read(context), gateway)}/v1`,
|
|
158
|
+
"claude-code": async (context) => origin(await read(context), gateway),
|
|
159
|
+
"openai-compatible": async (context) => `${origin(await read(context), gateway)}/v1`
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** The route resolver of an ask gateway. ViteHub Agents call it with their Server Env context. */
|
|
163
|
+
const aiGatewayAskRoute = Symbol.for("vitehub.aiGateway.askRoute");
|
|
164
|
+
function jevRoute(name, spec) {
|
|
165
|
+
return async (context) => {
|
|
166
|
+
const resolvedKey = await resolveAiGatewaySecret(spec.apiKey, context);
|
|
167
|
+
const oidcToken = resolvedKey ? void 0 : await resolveAiGatewaySecret(spec.oidcToken, context);
|
|
168
|
+
const apiKey = resolvedKey ?? oidcToken;
|
|
169
|
+
if (!apiKey) throw gatewayDiagnostics.GATEWAYS_R0002({ message: spec.keyEnv ? `[vitehub] Gateway "${name}" needs an API key. Set ${spec.keyEnv}, or pass apiKey.` : `[vitehub] Gateway "${name}" apiKey resolved to an empty value.` });
|
|
170
|
+
const headers = {};
|
|
171
|
+
for (const [header, input] of Object.entries(spec.optionalHeaders ?? {})) {
|
|
172
|
+
const value = await resolveAiGatewaySecret(input, context);
|
|
173
|
+
if (value) headers[header] = value;
|
|
174
|
+
}
|
|
175
|
+
for (const [header, input] of Object.entries(spec.headers ?? {})) {
|
|
176
|
+
const value = await resolveAiGatewaySecret(input, context);
|
|
177
|
+
if (!value) throw gatewayDiagnostics.GATEWAYS_R0003({ message: `[vitehub] Gateway "${name}" header "${header}" resolved to an empty value.` });
|
|
178
|
+
if (/[\r\n]/.test(value)) throw gatewayDiagnostics.GATEWAYS_R0003({ message: `[vitehub] Gateway "${name}" header "${header}" must not contain line breaks.` });
|
|
179
|
+
headers[header] = value;
|
|
180
|
+
}
|
|
181
|
+
if (oidcToken) headers["ai-gateway-auth-method"] = "oidc";
|
|
182
|
+
return {
|
|
183
|
+
...apiKey ? { apiKey } : {},
|
|
184
|
+
baseURL: await spec.baseURL(context),
|
|
185
|
+
...spec.fetch ? { fetch: spec.fetch } : {},
|
|
186
|
+
...Object.keys(headers).length ? { headers } : {},
|
|
187
|
+
...spec.keyEnv ? { keyEnv: spec.keyEnv } : {},
|
|
188
|
+
...spec.keyOption ? { keyOption: spec.keyOption } : {},
|
|
189
|
+
label: spec.label,
|
|
190
|
+
model: typeof spec.model === "string" ? spec.model : await spec.model(context),
|
|
191
|
+
name,
|
|
192
|
+
protocol: spec.protocol
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/** Asks through a gateway with a resolver context, observers, and usage records. ViteHub Agents use it for `driver.ask`. */
|
|
197
|
+
async function askAiGateway(gateway, state, questions, options = {}) {
|
|
198
|
+
const route = Reflect.get(gateway, aiGatewayAskRoute);
|
|
199
|
+
if (typeof route !== "function") return await unsupportedAsk(gateway.name)(state, questions);
|
|
200
|
+
const context = {
|
|
201
|
+
...options.context,
|
|
202
|
+
...options.signal ? { abortSignal: options.signal } : {}
|
|
203
|
+
};
|
|
204
|
+
return await askJev({
|
|
205
|
+
...options.signal ? { abortSignal: options.signal } : {},
|
|
206
|
+
...options.observe ? { observe: options.observe } : {},
|
|
207
|
+
...options.onUsage ? { recordUsage: options.onUsage } : {},
|
|
208
|
+
route: async () => await route(context)
|
|
209
|
+
}, state, questions);
|
|
210
|
+
}
|
|
211
|
+
function unsupportedAsk(name) {
|
|
212
|
+
return async () => {
|
|
213
|
+
throw gatewayDiagnostics.GATEWAYS_R0005({ message: `[vitehub] Gateway "${name}" routes model requests and cannot answer Jev questions. Use typesafe(), vercel(), or cloudflare() for ask().` });
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function createAiGateway(definition, { route, optional, modelApis, modelCredentials, openAIChat, structuredOutputs } = {}) {
|
|
217
|
+
const routes = normalizeAiGateway(route ? Object.defineProperty({ ...definition }, aiGatewayAsks, { value: true }) : definition);
|
|
218
|
+
const gateway = { ...routes };
|
|
219
|
+
if (modelApis) Object.defineProperty(gateway, "modelApis", { value: Object.freeze([...modelApis]) });
|
|
220
|
+
if (openAIChat) Object.defineProperty(gateway, aiGatewayOpenAIChat, { value: true });
|
|
221
|
+
if (optional) Object.defineProperty(gateway, aiGatewayOptionalCredentials, { value: Object.freeze({
|
|
222
|
+
...optional,
|
|
223
|
+
headers: Object.freeze([...optional.headers])
|
|
224
|
+
}) });
|
|
225
|
+
if (modelCredentials) Object.defineProperty(gateway, aiGatewayModelCredentials, { value: modelCredentials });
|
|
226
|
+
if (structuredOutputs) Object.defineProperty(gateway, aiGatewayStructuredOutputs, { value: true });
|
|
227
|
+
Object.defineProperty(gateway, "ask", { value: route ? (async (state, questions, options = {}) => await askAiGateway(gateway, state, questions, options)) : unsupportedAsk(routes.name) });
|
|
228
|
+
if (route) Object.defineProperties(gateway, {
|
|
229
|
+
[aiGatewayAsks]: { value: true },
|
|
230
|
+
[aiGatewayAskRoute]: { value: route }
|
|
231
|
+
});
|
|
232
|
+
return Object.freeze(gateway);
|
|
233
|
+
}
|
|
234
|
+
function preset(definition, options) {
|
|
235
|
+
const value = { ...definition };
|
|
236
|
+
if (options && Object.hasOwn(options, "apiKey")) value.apiKey = options.apiKey;
|
|
237
|
+
if (options?.headers !== void 0) value.headers = options.headers;
|
|
238
|
+
return value;
|
|
239
|
+
}
|
|
240
|
+
function keyEnv(preset, options) {
|
|
241
|
+
if (options && Object.hasOwn(options, "apiKey")) return void 0;
|
|
242
|
+
return [...aiGatewayVendorNames(preset, "apiKey"), ...preset === "vercel" ? aiGatewayVendorNames(preset, "oidcToken") : []].join(" or ");
|
|
243
|
+
}
|
|
244
|
+
function modelGateway(definition, extras) {
|
|
245
|
+
return createAiGateway(definition, extras);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Validate a custom gateway for an endpoint without a preset. Its type lists the model APIs in `baseURL`. It routes
|
|
249
|
+
* model requests; only the `typesafe()`, `vercel()`, and `cloudflare()` presets answer Jev questions.
|
|
250
|
+
*/
|
|
251
|
+
function defineAiGateway(gateway) {
|
|
252
|
+
return modelGateway(gateway);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Cloudflare Access service-token headers for a gateway behind Access.
|
|
256
|
+
* Reads `CF_ACCESS_CLIENT_ID` and `CF_ACCESS_CLIENT_SECRET` unless you pass the values.
|
|
257
|
+
*/
|
|
258
|
+
function cloudflareAccess(options = {}) {
|
|
259
|
+
const fromEnv = (field, header) => {
|
|
260
|
+
return requiredEnv("cloudflareAccess", field, (names) => {
|
|
261
|
+
throw gatewayDiagnostics.GATEWAYS_R0003({ message: `[vitehub] Gateway header "${header}" needs a value. Set ${names}, or pass cloudflareAccess({ ${field} }).` });
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
return {
|
|
265
|
+
"CF-Access-Client-Id": Object.hasOwn(options, "clientId") ? options.clientId : fromEnv("clientId", "CF-Access-Client-Id"),
|
|
266
|
+
"CF-Access-Client-Secret": Object.hasOwn(options, "clientSecret") ? options.clientSecret : fromEnv("clientSecret", "CF-Access-Client-Secret")
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/** [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI). Serves Codex and Claude Code. Reads `CLIPROXY_URL` and `CLIPROXY_API_KEY`. */
|
|
270
|
+
function cliproxy(options = {}) {
|
|
271
|
+
return modelGateway(preset({
|
|
272
|
+
name: "cliproxy",
|
|
273
|
+
baseURL: hostedBaseURL("cliproxy", options.url),
|
|
274
|
+
apiKey: apiKeyFromEnv("cliproxy")
|
|
275
|
+
}, options));
|
|
276
|
+
}
|
|
277
|
+
/** [LiteLLM Proxy](https://docs.litellm.ai/docs/simple_proxy). Serves Codex and Claude Code. Reads `LITELLM_URL` and `LITELLM_API_KEY`. */
|
|
278
|
+
function litellm(options = {}) {
|
|
279
|
+
return modelGateway(preset({
|
|
280
|
+
name: "litellm",
|
|
281
|
+
baseURL: hostedBaseURL("litellm", options.url),
|
|
282
|
+
apiKey: apiKeyFromEnv("litellm")
|
|
283
|
+
}, options));
|
|
284
|
+
}
|
|
285
|
+
/** [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. */
|
|
286
|
+
function ollama(options = {}) {
|
|
287
|
+
const read = (field) => async (context) => {
|
|
288
|
+
const value = await readAiGatewayEnv(context, "ollama", field);
|
|
289
|
+
return present(value) ? value : void 0;
|
|
290
|
+
};
|
|
291
|
+
const url = read("url");
|
|
292
|
+
const root = async (context) => origin(await url(context) ?? "http://localhost:11434", "ollama");
|
|
293
|
+
return modelGateway(preset({
|
|
294
|
+
name: "ollama",
|
|
295
|
+
baseURL: options.url !== void 0 ? hostedBaseURL("ollama", options.url) : {
|
|
296
|
+
"codex": async (context) => `${await root(context)}/v1`,
|
|
297
|
+
"claude-code": root,
|
|
298
|
+
"openai-compatible": async (context) => `${await root(context)}/v1`
|
|
299
|
+
},
|
|
300
|
+
apiKey: (async (context) => await read("apiKey")(context) ?? "ollama")
|
|
301
|
+
}, options));
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* The [AI emulator](https://emulate.dev/docs/ai) (`@emulators/ai`) for dev and tests. Serves AI SDK models with OpenAI
|
|
305
|
+
* Chat Completions (`api: "chat"`), OpenAI Responses (`"responses"`), and Anthropic Messages (`"messages"`), and answers
|
|
306
|
+
* from scripted rules with no AI key. It does not serve the Claude Code Driver, because the emulator rejects its `system`
|
|
307
|
+
* turns, or the Codex Driver, because the emulator rejects the tools that the Codex CLI sends. Reads
|
|
308
|
+
* `VITEHUB_EMULATE_URL`; defaults to `http://localhost:4014`. It reads no credentials.
|
|
309
|
+
*/
|
|
310
|
+
function emulate(options = {}) {
|
|
311
|
+
const root = async (context) => {
|
|
312
|
+
const value = await readAiGatewayEnv(context, "emulate", "url");
|
|
313
|
+
return origin(present(value) ? value : "http://localhost:4014", "emulate");
|
|
314
|
+
};
|
|
315
|
+
const url = options.url !== void 0 ? origin(options.url, "emulate") : void 0;
|
|
316
|
+
return modelGateway({
|
|
317
|
+
name: "emulate",
|
|
318
|
+
baseURL: url !== void 0 ? {
|
|
319
|
+
"claude-code": url,
|
|
320
|
+
"openai-compatible": `${url}/v1`
|
|
321
|
+
} : {
|
|
322
|
+
"claude-code": root,
|
|
323
|
+
"openai-compatible": async (context) => `${await root(context)}/v1`
|
|
324
|
+
},
|
|
325
|
+
apiKey: "emulate"
|
|
326
|
+
}, { structuredOutputs: true });
|
|
327
|
+
}
|
|
328
|
+
/** [OpenRouter](https://openrouter.ai/docs). Serves Codex and Claude Code. Reads `OPENROUTER_API_KEY`. */
|
|
329
|
+
function openrouter(options) {
|
|
330
|
+
return modelGateway(preset({
|
|
331
|
+
name: "openrouter",
|
|
332
|
+
baseURL: {
|
|
333
|
+
"codex": "https://openrouter.ai/api/v1",
|
|
334
|
+
"claude-code": "https://openrouter.ai/api",
|
|
335
|
+
"openai-compatible": "https://openrouter.ai/api/v1"
|
|
336
|
+
},
|
|
337
|
+
apiKey: apiKeyFromEnv("openrouter")
|
|
338
|
+
}, options));
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* [Vercel AI Gateway](https://vercel.com/docs/ai-gateway). Serves Codex, Claude Code, and AI SDK models, and answers
|
|
342
|
+
* Jev questions as `typesafe-ai/jev`. Reads `AI_GATEWAY_API_KEY`, then `VERCEL_OIDC_TOKEN`.
|
|
343
|
+
*/
|
|
344
|
+
function vercel(options) {
|
|
345
|
+
const envSecret = (field) => async (context) => {
|
|
346
|
+
return await readAiGatewayEnv(context, "vercel", field);
|
|
347
|
+
};
|
|
348
|
+
const apiKey = envSecret("apiKey");
|
|
349
|
+
const oidcToken = envSecret("oidcToken");
|
|
350
|
+
const explicitKey = options && Object.hasOwn(options, "apiKey");
|
|
351
|
+
const definition = preset({
|
|
352
|
+
name: "vercel",
|
|
353
|
+
baseURL: {
|
|
354
|
+
"codex": "https://ai-gateway.vercel.sh/codex/v1",
|
|
355
|
+
"claude-code": "https://ai-gateway.vercel.sh/claude-code",
|
|
356
|
+
"openai-compatible": "https://ai-gateway.vercel.sh/v1"
|
|
357
|
+
},
|
|
358
|
+
apiKey: async (context) => await resolveAiGatewaySecret(apiKey, context) ?? await resolveAiGatewaySecret(oidcToken, context)
|
|
359
|
+
}, options);
|
|
360
|
+
const modelCredentials = async (context) => {
|
|
361
|
+
const resolvedKey = await resolveAiGatewaySecret(explicitKey ? definition.apiKey : apiKey, context);
|
|
362
|
+
const token = resolvedKey || explicitKey ? void 0 : await resolveAiGatewaySecret(oidcToken, context);
|
|
363
|
+
return {
|
|
364
|
+
apiKey: resolvedKey ?? token,
|
|
365
|
+
headers: [["ai-gateway-auth-method", token ? "oidc" : "api-key"]]
|
|
366
|
+
};
|
|
367
|
+
};
|
|
368
|
+
return createAiGateway(definition, {
|
|
369
|
+
route: jevRoute("vercel", {
|
|
370
|
+
apiKey: explicitKey ? definition.apiKey : apiKey,
|
|
371
|
+
baseURL: async () => "https://ai-gateway.vercel.sh/v4/ai",
|
|
372
|
+
...options?.fetch ? { fetch: options.fetch } : {},
|
|
373
|
+
...definition.headers ? { headers: definition.headers } : {},
|
|
374
|
+
...keyEnv("vercel", options) ? { keyEnv: keyEnv("vercel", options) } : {},
|
|
375
|
+
label: "Vercel AI Gateway",
|
|
376
|
+
model: options?.model?.trim() || "typesafe-ai/jev",
|
|
377
|
+
...!explicitKey ? { oidcToken } : {},
|
|
378
|
+
protocol: "vercel"
|
|
379
|
+
}),
|
|
380
|
+
modelCredentials
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* The OpenAI API with an API key. Serves Codex and AI SDK models. Reads `OPENAI_API_KEY`. Its AI SDK models use
|
|
385
|
+
* `@ai-sdk/openai` for both `chat` and `responses`.
|
|
386
|
+
*/
|
|
387
|
+
function openai(options) {
|
|
388
|
+
return modelGateway(preset({
|
|
389
|
+
name: "openai",
|
|
390
|
+
baseURL: {
|
|
391
|
+
"codex": "https://api.openai.com/v1",
|
|
392
|
+
"openai-compatible": "https://api.openai.com/v1"
|
|
393
|
+
},
|
|
394
|
+
apiKey: apiKeyFromEnv("openai")
|
|
395
|
+
}, options), { openAIChat: true });
|
|
396
|
+
}
|
|
397
|
+
/** The Anthropic API with an API key. Serves Claude Code and AI SDK models. Reads `ANTHROPIC_API_KEY`. */
|
|
398
|
+
function anthropic(options) {
|
|
399
|
+
return modelGateway(preset({
|
|
400
|
+
name: "anthropic",
|
|
401
|
+
baseURL: { "claude-code": "https://api.anthropic.com" },
|
|
402
|
+
apiKey: apiKeyFromEnv("anthropic"),
|
|
403
|
+
auth: "x-api-key"
|
|
404
|
+
}, options));
|
|
405
|
+
}
|
|
406
|
+
/** [TypeSafe](https://typesafe.ai). Answers Jev questions; it serves no model API. Reads `TYPESAFE_API_KEY` and `TYPESAFE_DEFAULT_MODEL`. */
|
|
407
|
+
function typesafe(options) {
|
|
408
|
+
const definition = preset({
|
|
409
|
+
name: "typesafe",
|
|
410
|
+
baseURL: {},
|
|
411
|
+
apiKey: apiKeyFromEnv("typesafe")
|
|
412
|
+
}, options);
|
|
413
|
+
return createAiGateway(definition, { route: jevRoute("typesafe", {
|
|
414
|
+
apiKey: definition.apiKey,
|
|
415
|
+
baseURL: async () => "https://api.typesafe.ai",
|
|
416
|
+
...options?.fetch ? { fetch: options.fetch } : {},
|
|
417
|
+
...definition.headers ? { headers: definition.headers } : {},
|
|
418
|
+
...keyEnv("typesafe", options) ? { keyEnv: keyEnv("typesafe", options) } : {},
|
|
419
|
+
label: "TypeSafe",
|
|
420
|
+
model: options?.model?.trim() || (async (context) => {
|
|
421
|
+
const model = await readAiGatewayEnv(context, "typesafe", "model");
|
|
422
|
+
return typeof model === "string" && model.trim() ? model.trim() : "jev-latest";
|
|
423
|
+
}),
|
|
424
|
+
protocol: "typesafe"
|
|
425
|
+
}) });
|
|
426
|
+
}
|
|
427
|
+
function cloudflareRoot(options, preset) {
|
|
428
|
+
const required = async (context, field) => {
|
|
429
|
+
const given = options[field];
|
|
430
|
+
const read = given !== void 0 ? given : await readAiGatewayEnv(context, preset, field);
|
|
431
|
+
const resolved = typeof read === "string" ? read.trim() : "";
|
|
432
|
+
if (resolved) return resolved;
|
|
433
|
+
const label = field === "accountId" ? "an account ID" : "a gateway ID";
|
|
434
|
+
throw gatewayDiagnostics.GATEWAYS_R0004({ message: `[vitehub] Gateway "${preset}" needs ${label}. Set ${aiGatewayVendorNames(preset, field).join(" or ")}, or pass ${field}.` });
|
|
435
|
+
};
|
|
436
|
+
return async (context) => ({
|
|
437
|
+
accountId: await required(context, "accountId"),
|
|
438
|
+
gatewayId: await required(context, "gatewayId")
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
function cloudflareToken(options, preset) {
|
|
442
|
+
const token = Object.hasOwn(options, "token") ? options.token : async (context) => await readAiGatewayEnv(context, preset, "token");
|
|
443
|
+
return async (context) => {
|
|
444
|
+
const resolved = await resolveAiGatewaySecret(token, context);
|
|
445
|
+
return resolved ? `Bearer ${resolved}` : void 0;
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* [Cloudflare AI Gateway](https://developers.cloudflare.com/ai-gateway/). Serves AI SDK models through its
|
|
450
|
+
* [OpenAI-compatible endpoint](https://developers.cloudflare.com/ai-gateway/usage/chat-completion/), with
|
|
451
|
+
* 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
|
|
452
|
+
* through a TypeSafe [custom provider](https://developers.cloudflare.com/ai-gateway/configuration/custom-providers/).
|
|
453
|
+
* Each capability reads its own key: model requests need the gateway token, `CLOUDFLARE_AI_GATEWAY_API_KEY`, or both;
|
|
454
|
+
* `ask()` needs `TYPESAFE_API_KEY`. For Codex or Claude Code through Cloudflare, use `defineAiGateway()`.
|
|
455
|
+
*/
|
|
456
|
+
function cloudflare(options = {}) {
|
|
457
|
+
if (Object.hasOwn(options, "apiKey")) throw gatewayDiagnostics.GATEWAYS_R0001({ message: "[vitehub] cloudflare() reads two keys and has no apiKey. Pass typesafeApiKey for ask(), or providerApiKey for model requests." });
|
|
458
|
+
const root = cloudflareRoot(options, "cloudflare");
|
|
459
|
+
const token = cloudflareToken(options, "cloudflare");
|
|
460
|
+
const fromEnv = (field) => {
|
|
461
|
+
return async (context) => await readAiGatewayEnv(context, "cloudflare", field);
|
|
462
|
+
};
|
|
463
|
+
const definition = {
|
|
464
|
+
name: "cloudflare",
|
|
465
|
+
baseURL: { "openai-compatible": async (context) => {
|
|
466
|
+
const { accountId, gatewayId } = await root(context);
|
|
467
|
+
return `https://gateway.ai.cloudflare.com/v1/${encodeURIComponent(accountId)}/${encodeURIComponent(gatewayId)}/compat`;
|
|
468
|
+
} },
|
|
469
|
+
apiKey: Object.hasOwn(options, "providerApiKey") ? options.providerApiKey : fromEnv("providerApiKey"),
|
|
470
|
+
headers: {
|
|
471
|
+
...options.headers,
|
|
472
|
+
"cf-aig-authorization": token
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
const slug = async (context) => {
|
|
476
|
+
const read = options.slug !== void 0 ? options.slug : await readAiGatewayEnv(context, "cloudflare", "slug");
|
|
477
|
+
return typeof read === "string" ? read.trim() : "";
|
|
478
|
+
};
|
|
479
|
+
const explicitTypesafeKey = Object.hasOwn(options, "typesafeApiKey");
|
|
480
|
+
return createAiGateway(definition, {
|
|
481
|
+
route: jevRoute("cloudflare", {
|
|
482
|
+
apiKey: explicitTypesafeKey ? options.typesafeApiKey : fromEnv("typesafeApiKey"),
|
|
483
|
+
baseURL: async (context) => cloudflareJevBaseURL({
|
|
484
|
+
...await root(context),
|
|
485
|
+
slug: await slug(context)
|
|
486
|
+
}),
|
|
487
|
+
...options.fetch ? { fetch: options.fetch } : {},
|
|
488
|
+
...options.headers ? { headers: options.headers } : {},
|
|
489
|
+
optionalHeaders: { "cf-aig-authorization": token },
|
|
490
|
+
...explicitTypesafeKey ? {} : { keyEnv: aiGatewayVendorNames("cloudflare", "typesafeApiKey").join(" or ") },
|
|
491
|
+
keyOption: "typesafeApiKey",
|
|
492
|
+
label: "Cloudflare AI Gateway",
|
|
493
|
+
model: options.model?.trim() || "jev-latest",
|
|
494
|
+
protocol: "typesafe"
|
|
495
|
+
}),
|
|
496
|
+
modelApis: ["chat"],
|
|
497
|
+
optional: {
|
|
498
|
+
apiKey: true,
|
|
499
|
+
headers: ["cf-aig-authorization"],
|
|
500
|
+
missing: "Gateway \"cloudflare\" needs a gateway token or a provider key for model requests. Set CLOUDFLARE_AI_GATEWAY_TOKEN for stored provider keys or Unified Billing, or CLOUDFLARE_AI_GATEWAY_API_KEY, or pass token or providerApiKey."
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
export { isChatGPTGateway as _, cloudflareAccess as a, litellm as c, openrouter as d, typesafe as f, chatgpt as g, aiGatewayEnvNames as h, cloudflare as i, ollama as l, aiGatewayEnv as m, askAiGateway as n, defineAiGateway as o, vercel as p, cliproxy as r, emulate as s, anthropic as t, openai as u };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { a as AiGatewayAskResult, c as AiGatewayDefinition, f as AiGatewaySecret, k as AskQuestions, m as AiGatewayValue, p as AiGatewayUsage, s as AiGatewayContext, t as AiGateway, u as AiGatewayRoute } from "./types-
|
|
2
|
-
//#region src/chatgpt.d.ts
|
|
1
|
+
import { a as AiGatewayAskResult, c as AiGatewayDefinition, f as AiGatewaySecret, k as AskQuestions, m as AiGatewayValue, p as AiGatewayUsage, s as AiGatewayContext, t as AiGateway, u as AiGatewayRoute } from "./types-jSw-t3Dq.js";
|
|
3
2
|
/** A runtime-owned ChatGPT plan route. It contains no credentials. */
|
|
4
3
|
interface ChatGPTGateway {
|
|
5
4
|
readonly kind: "chatgpt";
|
|
@@ -11,27 +10,19 @@ declare function chatgpt(options?: {
|
|
|
11
10
|
connection?: string;
|
|
12
11
|
}): ChatGPTGateway;
|
|
13
12
|
declare function isChatGPTGateway(value: unknown): value is ChatGPTGateway;
|
|
14
|
-
//#endregion
|
|
15
|
-
//#region src/env.d.ts
|
|
16
13
|
/** One preset value that is read from the environment when the preset does not receive it. */
|
|
17
14
|
interface AiGatewayEnvField {
|
|
18
15
|
/** Vendor variable names in lookup order, after the `VITEHUB_` name. */
|
|
19
16
|
names: readonly [string, ...string[]];
|
|
20
17
|
secret?: true;
|
|
21
18
|
}
|
|
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
19
|
/** The presets that read the environment. */
|
|
27
|
-
type AiGatewayEnvPreset = "anthropic" | "cliproxy" | "cloudflare" | "cloudflareAccess" | "litellm" | "ollama" | "openai" | "openrouter" | "typesafe" | "vercel";
|
|
20
|
+
type AiGatewayEnvPreset = "anthropic" | "cliproxy" | "cloudflare" | "cloudflareAccess" | "emulate" | "litellm" | "ollama" | "openai" | "openrouter" | "typesafe" | "vercel";
|
|
28
21
|
declare const aiGatewayEnv: Readonly<Record<AiGatewayEnvPreset, Readonly<Record<string, AiGatewayEnvField>>>>;
|
|
29
22
|
/** A field of a preset, such as `apiKey`. */
|
|
30
23
|
type AiGatewayEnvFieldName<_TPreset extends AiGatewayEnvPreset = AiGatewayEnvPreset> = string;
|
|
31
24
|
/** The variables of one preset value in lookup order: `VITEHUB_<PRESET>_<FIELD>`, then the vendor names. */
|
|
32
25
|
declare function aiGatewayEnvNames<TPreset extends AiGatewayEnvPreset>(preset: TPreset, field: AiGatewayEnvFieldName<TPreset>): string[];
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/jev.d.ts
|
|
35
26
|
/** One trace event of a Jev request. ViteHub Agents record these in the Invocation trace. */
|
|
36
27
|
interface AiGatewayTraceEvent {
|
|
37
28
|
attributes: Record<string, unknown>;
|
|
@@ -67,8 +58,6 @@ interface JevRoute {
|
|
|
67
58
|
name: string;
|
|
68
59
|
protocol: "typesafe" | "vercel";
|
|
69
60
|
}
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region src/presets.d.ts
|
|
72
61
|
/** Options shared by every gateway preset. */
|
|
73
62
|
interface AiGatewayPresetOptions {
|
|
74
63
|
/** API key. When it is not set, the preset reads its Server Env value, such as `CLIPROXY_API_KEY`. */
|
|
@@ -81,6 +70,11 @@ interface AiHostedGatewayOptions extends AiGatewayPresetOptions {
|
|
|
81
70
|
/** 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
71
|
url?: string;
|
|
83
72
|
}
|
|
73
|
+
/** Options for `emulate()`. */
|
|
74
|
+
interface EmulateGatewayOptions {
|
|
75
|
+
/** Origin of the AI emulator, such as `http://localhost:4014`. A trailing `/v1` is removed. When it is not set, the preset reads `VITEHUB_EMULATE_URL`, then uses `http://localhost:4014`. */
|
|
76
|
+
url?: string;
|
|
77
|
+
}
|
|
84
78
|
/** Options for a gateway that answers TypeSafe Jev questions. */
|
|
85
79
|
interface AiAskGatewayOptions {
|
|
86
80
|
/** Custom fetch implementation for Jev requests. Default: `globalThis.fetch`. */
|
|
@@ -121,7 +115,6 @@ interface CloudflareAccessOptions {
|
|
|
121
115
|
/** Service token client secret. Defaults to `CF_ACCESS_CLIENT_SECRET`. */
|
|
122
116
|
clientSecret?: AiGatewaySecret;
|
|
123
117
|
}
|
|
124
|
-
/** The model APIs of a gateway that you host. */
|
|
125
118
|
type HostedRoutes = "claude-code" | "codex" | "openai-compatible";
|
|
126
119
|
/** Asks through a gateway with a resolver context, observers, and usage records. ViteHub Agents use it for `driver.ask`. */
|
|
127
120
|
declare function askAiGateway<const Q extends AskQuestions>(gateway: AiGateway, state: unknown, questions: Q, options?: {
|
|
@@ -153,6 +146,15 @@ declare function cliproxy(options?: AiHostedGatewayOptions): AiModelGateway<Host
|
|
|
153
146
|
declare function litellm(options?: AiHostedGatewayOptions): AiModelGateway<HostedRoutes>;
|
|
154
147
|
/** [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
148
|
declare function ollama(options?: AiHostedGatewayOptions): AiModelGateway<HostedRoutes>;
|
|
149
|
+
type EmulateRoutes = "claude-code" | "openai-compatible";
|
|
150
|
+
/**
|
|
151
|
+
* The [AI emulator](https://emulate.dev/docs/ai) (`@emulators/ai`) for dev and tests. Serves AI SDK models with OpenAI
|
|
152
|
+
* Chat Completions (`api: "chat"`), OpenAI Responses (`"responses"`), and Anthropic Messages (`"messages"`), and answers
|
|
153
|
+
* from scripted rules with no AI key. It does not serve the Claude Code Driver, because the emulator rejects its `system`
|
|
154
|
+
* turns, or the Codex Driver, because the emulator rejects the tools that the Codex CLI sends. Reads
|
|
155
|
+
* `VITEHUB_EMULATE_URL`; defaults to `http://localhost:4014`. It reads no credentials.
|
|
156
|
+
*/
|
|
157
|
+
declare function emulate(options?: EmulateGatewayOptions): AiModelGateway<EmulateRoutes>;
|
|
156
158
|
/** [OpenRouter](https://openrouter.ai/docs). Serves Codex and Claude Code. Reads `OPENROUTER_API_KEY`. */
|
|
157
159
|
declare function openrouter(options?: AiGatewayPresetOptions): AiModelGateway<HostedRoutes>;
|
|
158
160
|
/**
|
|
@@ -163,7 +165,10 @@ declare function vercel(options?: VercelGatewayOptions): AiGateway<{
|
|
|
163
165
|
ask: true;
|
|
164
166
|
routes: HostedRoutes;
|
|
165
167
|
}>;
|
|
166
|
-
/**
|
|
168
|
+
/**
|
|
169
|
+
* The OpenAI API with an API key. Serves Codex and AI SDK models. Reads `OPENAI_API_KEY`. Its AI SDK models use
|
|
170
|
+
* `@ai-sdk/openai` for both `chat` and `responses`.
|
|
171
|
+
*/
|
|
167
172
|
declare function openai(options?: AiGatewayPresetOptions): AiModelGateway<"codex" | "openai-compatible">;
|
|
168
173
|
/** The Anthropic API with an API key. Serves Claude Code and AI SDK models. Reads `ANTHROPIC_API_KEY`. */
|
|
169
174
|
declare function anthropic(options?: AiGatewayPresetOptions): AiModelGateway<"claude-code">;
|
|
@@ -185,5 +190,4 @@ declare function cloudflare(options?: CloudflareGatewayOptions): AiGateway<{
|
|
|
185
190
|
ask: true;
|
|
186
191
|
routes: "openai-compatible";
|
|
187
192
|
}>;
|
|
188
|
-
|
|
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 };
|
|
193
|
+
export { chatgpt as A, AiGatewayTraceEvent as C, aiGatewayEnv as D, AiGatewayEnvPreset as E, aiGatewayEnvNames as O, vercel as S, AiGatewayEnvFieldName as T, litellm as _, CloudflareAccessOptions as a, openrouter as b, TypesafeGatewayOptions as c, askAiGateway as d, cliproxy as f, emulate as g, defineAiGateway as h, AiModelGateway as i, isChatGPTGateway as j, ChatGPTGateway as k, VercelGatewayOptions as l, cloudflareAccess as m, AiGatewayPresetOptions as n, CloudflareGatewayOptions as o, cloudflare as p, AiHostedGatewayOptions as r, EmulateGatewayOptions as s, AiAskGatewayOptions as t, anthropic as u, ollama as v, AiGatewayEnvField as w, typesafe as x, openai as y };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { A as AskScoreAnswer, M as AskScoreQuestion, O as AskQuestion, S as AskChoiceCriteria, h as AskAnswer, j as AskScoreCriteria, k as AskQuestions, n as AiGatewayAsk, p as AiGatewayUsage, v as AskChanceAnswer, w as AskChoiceQuestion, x as AskChoiceAnswer } from "./types-jSw-t3Dq.js";
|
|
2
|
+
/** One `gateway.ask()` call that `scriptedAsk()` received. */
|
|
3
|
+
export interface ScriptedAskCall {
|
|
4
|
+
questions: AskQuestions;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
state: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface ScriptedAskOptions {
|
|
9
|
+
/** Returns this error from every `ask()`, as a provider would: `GATEWAYS_R0008` for a rejected key, `GATEWAYS_R0009` for a provider that keeps failing. */
|
|
10
|
+
error?: "GATEWAYS_R0008" | "GATEWAYS_R0009";
|
|
11
|
+
/** The token counts that `onUsage` receives after each `ask()`. Default: zero. */
|
|
12
|
+
usage?: AiGatewayUsage["usage"];
|
|
13
|
+
}
|
|
14
|
+
/** Returns the answer to one question. Build it with `answers`. */
|
|
15
|
+
export type AskScript = (key: string, question: AskQuestion, state: unknown) => AskAnswer<AskQuestion>;
|
|
16
|
+
declare function choice<const T extends AskChoiceCriteria>(question: AskChoiceQuestion<T>, label: keyof T & string, probability?: number): AskChoiceAnswer<T>;
|
|
17
|
+
declare function choice(question: AskQuestion, label: string, probability?: number): AskChoiceAnswer;
|
|
18
|
+
declare function score<const T extends AskScoreCriteria>(question: AskScoreQuestion<T>, level: number, probability?: number): AskScoreAnswer<T>;
|
|
19
|
+
declare function score(question: AskQuestion, level: number, probability?: number): AskScoreAnswer;
|
|
20
|
+
declare function chance(probability: number): AskChanceAnswer;
|
|
21
|
+
declare function first<const Q extends AskQuestion>(question: Q): AskAnswer<Q>;
|
|
22
|
+
/** Build answers that pass the same checks as a real Jev answer. */
|
|
23
|
+
export declare const answers: {
|
|
24
|
+
/** `{ chance }`, the probability of yes. */
|
|
25
|
+
chance: typeof chance;
|
|
26
|
+
/** `label` gets `probability` (default 1), and the other labels share the rest. The chosen label must tie for or have the highest probability. */
|
|
27
|
+
choice: typeof choice;
|
|
28
|
+
/** The first label of a choice or switch question, level 0 of a score question, and yes for chance and if questions. */
|
|
29
|
+
first: typeof first;
|
|
30
|
+
/** `level` gets `probability` (default 1), and the other levels share the rest. */
|
|
31
|
+
score: typeof score;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* A gateway with `ask()` that answers each question with `script(key, question, state)`, so tests need no model or key.
|
|
35
|
+
* It checks answers against their questions, records each call in `calls`, and calls `onUsage` after each successful `ask()`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const { gateway, calls } = scriptedAsk((key, question) => answers.first(question))
|
|
39
|
+
*/
|
|
40
|
+
export declare function scriptedAsk(script: AskScript, options?: ScriptedAskOptions): {
|
|
41
|
+
calls: ScriptedAskCall[];
|
|
42
|
+
gateway: {
|
|
43
|
+
ask: AiGatewayAsk;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export {};
|