@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
package/dist/testing.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { t as gatewayDiagnostics } from "./diagnostics-CNIdO5-M.js";
|
|
2
|
+
import { r as hasValidChoiceAnswer } from "./jev-Kmwvxt8l.js";
|
|
3
|
+
// src/testing.ts
|
|
4
|
+
function invalid(problem) {
|
|
5
|
+
return gatewayDiagnostics.GATEWAYS_TESTING_ANSWER_INVALID({ problem });
|
|
6
|
+
}
|
|
7
|
+
function isProbability(value) {
|
|
8
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1;
|
|
9
|
+
}
|
|
10
|
+
function checkProbability(helper, probability) {
|
|
11
|
+
if (!isProbability(probability)) throw invalid(`${helper} got the probability ${probability}. It must be from 0 to 1.`);
|
|
12
|
+
}
|
|
13
|
+
function choice(question, label, probability = 1) {
|
|
14
|
+
if (question.type !== "choice") throw invalid(`answers.choice() needs a choice question. It got a ${question.type} question.`);
|
|
15
|
+
if (!Object.hasOwn(question.criteria, label)) throw invalid(`answers.choice() got the label ${JSON.stringify(label)}. The question has ${Object.keys(question.criteria).map((item) => JSON.stringify(item)).join(", ")}.`);
|
|
16
|
+
checkProbability("answers.choice()", probability);
|
|
17
|
+
const labels = Object.keys(question.criteria);
|
|
18
|
+
const minimum = 1 / labels.length;
|
|
19
|
+
if (probability < minimum) throw invalid(`answers.choice() needs a probability of at least ${minimum} so the chosen label has a highest probability. It got ${probability}.`);
|
|
20
|
+
const rest = labels.length === 1 ? 0 : probability === minimum ? probability : (1 - probability) / (labels.length - 1);
|
|
21
|
+
return {
|
|
22
|
+
choice: label,
|
|
23
|
+
confidence: probability - rest,
|
|
24
|
+
probabilities: Object.fromEntries(labels.map((item) => [item, item === label ? probability : rest])),
|
|
25
|
+
type: "choice"
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function score(question, level, probability = 1) {
|
|
29
|
+
if (question.type !== "score") throw invalid(`answers.score() needs a score question. It got a ${question.type} question.`);
|
|
30
|
+
const levels = question.criteria.length;
|
|
31
|
+
if (!Number.isInteger(level) || level < 0 || level >= levels) throw invalid(`answers.score() got the level ${level}. The question has levels 0 to ${levels - 1}.`);
|
|
32
|
+
checkProbability("answers.score()", probability);
|
|
33
|
+
const rest = (1 - probability) / (levels - 1);
|
|
34
|
+
const probabilities = question.criteria.map((_, index) => index === level ? probability : rest);
|
|
35
|
+
const value = probabilities.reduce((sum, item, index) => sum + item * index, 0);
|
|
36
|
+
return {
|
|
37
|
+
confidence: probability,
|
|
38
|
+
legend: Object.fromEntries(question.criteria.map((entry, index) => [String(index), entry])),
|
|
39
|
+
probabilities: Object.fromEntries(probabilities.map((item, index) => [String(index), item])),
|
|
40
|
+
ratio: value / (levels - 1),
|
|
41
|
+
score: value,
|
|
42
|
+
type: "score"
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function chance(probability) {
|
|
46
|
+
checkProbability("answers.chance()", probability);
|
|
47
|
+
return {
|
|
48
|
+
chance: probability,
|
|
49
|
+
type: "chance"
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function first(question) {
|
|
53
|
+
switch (question.type) {
|
|
54
|
+
case "choice": return choice(question, Object.keys(question.criteria)[0]);
|
|
55
|
+
case "switch": return Object.keys(question.criteria)[0];
|
|
56
|
+
case "score": return score(question, 0);
|
|
57
|
+
case "chance": return chance(1);
|
|
58
|
+
case "if": return true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Build answers that pass the same checks as a real Jev answer. */
|
|
62
|
+
const answers = {
|
|
63
|
+
chance,
|
|
64
|
+
choice,
|
|
65
|
+
first,
|
|
66
|
+
score
|
|
67
|
+
};
|
|
68
|
+
function scriptedError(code) {
|
|
69
|
+
if (code === "GATEWAYS_R0008") return Object.assign(gatewayDiagnostics.GATEWAYS_R0008({
|
|
70
|
+
provider: "scripted",
|
|
71
|
+
status: 401
|
|
72
|
+
}), {
|
|
73
|
+
providerStatus: 401,
|
|
74
|
+
status: 503
|
|
75
|
+
});
|
|
76
|
+
return Object.assign(gatewayDiagnostics.GATEWAYS_R0009({
|
|
77
|
+
attempts: 3,
|
|
78
|
+
provider: "scripted",
|
|
79
|
+
status: 503
|
|
80
|
+
}), {
|
|
81
|
+
providerStatus: 503,
|
|
82
|
+
status: 502
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function isRecord(value) {
|
|
86
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
87
|
+
}
|
|
88
|
+
function validAnswer(question, answer) {
|
|
89
|
+
switch (question.type) {
|
|
90
|
+
case "if": return typeof answer === "boolean";
|
|
91
|
+
case "switch": return typeof answer === "string" && Object.hasOwn(question.criteria, answer);
|
|
92
|
+
case "chance": return isRecord(answer) && answer.type === "chance" && isProbability(answer.chance);
|
|
93
|
+
case "score": {
|
|
94
|
+
if (!isRecord(answer) || answer.type !== "score" || !isProbability(answer.confidence) || !isProbability(answer.ratio) || typeof answer.score !== "number" || !Number.isFinite(answer.score) || answer.score < 0 || answer.score > question.criteria.length - 1 || !isRecord(answer.legend) || !isRecord(answer.probabilities)) return false;
|
|
95
|
+
const { legend, probabilities } = answer;
|
|
96
|
+
return question.criteria.every((_, level) => Object.hasOwn(legend, level) && legend[level] !== void 0 && isProbability(probabilities[level]));
|
|
97
|
+
}
|
|
98
|
+
case "choice": {
|
|
99
|
+
if (!hasValidChoiceAnswer(question, answer)) return false;
|
|
100
|
+
const { choice, probabilities } = answer;
|
|
101
|
+
return Object.values(probabilities).every((item) => item <= probabilities[choice]);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* A gateway with `ask()` that answers each question with `script(key, question, state)`, so tests need no model or key.
|
|
107
|
+
* It checks answers against their questions, records each call in `calls`, and calls `onUsage` after each successful `ask()`.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* const { gateway, calls } = scriptedAsk((key, question) => answers.first(question))
|
|
111
|
+
*/
|
|
112
|
+
function scriptedAsk(script, options = {}) {
|
|
113
|
+
const calls = [];
|
|
114
|
+
const ask = async (state, questions, askOptions) => {
|
|
115
|
+
calls.push({
|
|
116
|
+
questions,
|
|
117
|
+
signal: askOptions?.signal,
|
|
118
|
+
state
|
|
119
|
+
});
|
|
120
|
+
askOptions?.signal?.throwIfAborted();
|
|
121
|
+
if (options.error) return [scriptedError(options.error), void 0];
|
|
122
|
+
const entries = Object.entries(questions).map(([name, question]) => {
|
|
123
|
+
const answer = script(name, question, state);
|
|
124
|
+
if (answer === void 0) throw gatewayDiagnostics.GATEWAYS_JEV_RESPONSE_INVALID({
|
|
125
|
+
name,
|
|
126
|
+
problem: "missing"
|
|
127
|
+
});
|
|
128
|
+
if (!validAnswer(question, answer)) {
|
|
129
|
+
if (question.type === "choice") throw gatewayDiagnostics.GATEWAYS_JEV_RESPONSE_INVALID({
|
|
130
|
+
name,
|
|
131
|
+
problem: "choice"
|
|
132
|
+
});
|
|
133
|
+
throw invalid(`scriptedAsk() got an invalid answer for the ${question.type} question ${JSON.stringify(name)}.`);
|
|
134
|
+
}
|
|
135
|
+
return [name, answer];
|
|
136
|
+
});
|
|
137
|
+
askOptions?.onUsage?.({
|
|
138
|
+
provider: "scripted",
|
|
139
|
+
usage: options.usage ?? {
|
|
140
|
+
inputTokens: 0,
|
|
141
|
+
outputTokens: 0,
|
|
142
|
+
totalTokens: 0
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return [void 0, Object.fromEntries(entries)];
|
|
146
|
+
};
|
|
147
|
+
return {
|
|
148
|
+
calls,
|
|
149
|
+
gateway: { ask }
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
export { answers, scriptedAsk };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Diagnostic } from "nostics";
|
|
2
|
-
//#region src/ask-model.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* An AI SDK model instance or gateway model id. This structural boundary keeps the optional SDK out of root declarations.
|
|
5
4
|
* The SDK owns the call options and results; the adapter passes the model through without calling these hooks itself.
|
|
@@ -68,8 +67,6 @@ interface AskCall {
|
|
|
68
67
|
}): Promise<AskResult<AskSchemaOutput<TSchema>>>;
|
|
69
68
|
(prompt: string, options: AskOptions): Promise<AskResult<string>>;
|
|
70
69
|
}
|
|
71
|
-
//#endregion
|
|
72
|
-
//#region src/ask.d.ts
|
|
73
70
|
/** A JSON value that TypeSafe Jev can read. */
|
|
74
71
|
type AskJson = string | number | boolean | null | AskJson[] | {
|
|
75
72
|
[key: string]: AskJson;
|
|
@@ -89,9 +86,7 @@ type AskChanceCriteria = {
|
|
|
89
86
|
false?: AskEntry;
|
|
90
87
|
true?: AskEntry;
|
|
91
88
|
} | null;
|
|
92
|
-
/** Labels become criteria with `null` descriptions. An object stays as it is. */
|
|
93
89
|
type AskNamedCriteria<T> = T extends AskChoiceLabels ? { [K in T[number]]: null; } : T;
|
|
94
|
-
/** Score level keys: the tuple indices for a fixed rubric, otherwise any numeric string. */
|
|
95
90
|
type AskScoreLevel<T extends AskScoreCriteria> = number extends T["length"] ? `${number}` : Extract<keyof T, `${number}`>;
|
|
96
91
|
interface AskChoiceAnswer<T extends AskChoiceCriteria = AskChoiceCriteria> {
|
|
97
92
|
readonly type: "choice";
|
|
@@ -200,8 +195,6 @@ declare const ask: AskCall & {
|
|
|
200
195
|
score: typeof score;
|
|
201
196
|
switch: typeof askSwitch;
|
|
202
197
|
};
|
|
203
|
-
//#endregion
|
|
204
|
-
//#region src/types.d.ts
|
|
205
198
|
/** What a gateway value resolver receives. */
|
|
206
199
|
interface AiGatewayContext {
|
|
207
200
|
/** Aborts a resolver that reads remote storage. */
|
|
@@ -317,5 +310,4 @@ type AiGateway<TCapabilities extends Partial<AiGatewayCapabilities> = Partial<Ai
|
|
|
317
310
|
} : {
|
|
318
311
|
ask?: AiGatewayAsk;
|
|
319
312
|
});
|
|
320
|
-
//#endregion
|
|
321
313
|
export { AskScoreAnswer as A, askWithModel as B, AskChoiceLabels as C, AskJson as D, AskIfQuestion as E, AskError as F, AskModel as I, AskOptions as L, AskScoreQuestion as M, AskSwitchQuestion as N, AskQuestion as O, ask as P, AskResult as R, AskChoiceCriteria as S, AskEntry as T, AskAnswers as _, AiGatewayAskResult as a, AskChanceQuestion as b, AiGatewayDefinition as c, AiGatewaySealedSecret as d, AiGatewaySecret as f, AskAnswerType as g, AskAnswer as h, AiGatewayAskOptions as i, AskScoreCriteria as j, AskQuestions as k, AiGatewayDriver as l, AiGatewayValue as m, AiGatewayAsk as n, AiGatewayCapabilities as o, AiGatewayUsage as p, AiGatewayAskError as r, AiGatewayContext as s, AiGateway as t, AiGatewayRoute as u, AskChanceAnswer as v, AskChoiceQuestion as w, AskChoiceAnswer as x, AskChanceCriteria as y, AskSchema as z };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/gateways",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "One AI gateway object for every consumer: route Codex, Claude Code, and AI SDK models, and ask TypeSafe Jev typed questions, through Vercel AI Gateway, Cloudflare AI Gateway, OpenRouter, LiteLLM, CLIProxyAPI, Ollama, OpenAI, Anthropic, or TypeSafe.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -52,24 +52,31 @@
|
|
|
52
52
|
"import": "./dist/internal.js",
|
|
53
53
|
"default": "./dist/internal.js"
|
|
54
54
|
},
|
|
55
|
+
"./testing": {
|
|
56
|
+
"types": "./dist/testing.d.ts",
|
|
57
|
+
"import": "./dist/testing.js",
|
|
58
|
+
"default": "./dist/testing.js"
|
|
59
|
+
},
|
|
55
60
|
"./package.json": "./package.json"
|
|
56
61
|
},
|
|
57
62
|
"dependencies": {
|
|
58
63
|
"nostics": "1.4.0"
|
|
59
64
|
},
|
|
60
65
|
"devDependencies": {
|
|
61
|
-
"@ai-sdk/anthropic": "4.0.
|
|
62
|
-
"@ai-sdk/openai": "4.0.
|
|
63
|
-
"@ai-sdk/openai-compatible": "3.0.
|
|
64
|
-
"@ai-sdk/provider": "
|
|
65
|
-
"@
|
|
66
|
+
"@ai-sdk/anthropic": "4.0.107",
|
|
67
|
+
"@ai-sdk/openai": "4.0.122",
|
|
68
|
+
"@ai-sdk/openai-compatible": "3.0.94",
|
|
69
|
+
"@ai-sdk/provider": "4.0.39",
|
|
70
|
+
"@emulators/ai": "0.14.0",
|
|
71
|
+
"@types/node": "^24.19.2",
|
|
66
72
|
"@vite-hub/internal": "0.0.1",
|
|
67
73
|
"advocaat": "^0.0.6",
|
|
68
|
-
"ai": "7.0.
|
|
74
|
+
"ai": "7.0.179",
|
|
75
|
+
"emulate": "0.14.0",
|
|
69
76
|
"publint": "^0.3.25",
|
|
70
77
|
"typescript": "^6.0.3",
|
|
71
|
-
"vite-plus": "^
|
|
72
|
-
"vitest": "
|
|
78
|
+
"vite-plus": "^1.7.0",
|
|
79
|
+
"vitest": "5.1.0"
|
|
73
80
|
},
|
|
74
81
|
"peerDependencies": {
|
|
75
82
|
"@ai-sdk/anthropic": "^4.0.0",
|