@vite-hub/gateways 0.4.1 → 0.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.
@@ -0,0 +1,153 @@
1
+ import { t as gatewayDiagnostics } from "./diagnostics-KWRXsfZ3.js";
2
+ import { r as hasValidChoiceAnswer } from "./jev-BI0dIeB8.js";
3
+ //#region 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
+ //#endregion
153
+ export { answers, scriptedAsk };
@@ -89,9 +89,7 @@ type AskChanceCriteria = {
89
89
  false?: AskEntry;
90
90
  true?: AskEntry;
91
91
  } | null;
92
- /** Labels become criteria with `null` descriptions. An object stays as it is. */
93
92
  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
93
  type AskScoreLevel<T extends AskScoreCriteria> = number extends T["length"] ? `${number}` : Extract<keyof T, `${number}`>;
96
94
  interface AskChoiceAnswer<T extends AskChoiceCriteria = AskChoiceCriteria> {
97
95
  readonly type: "choice";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vite-hub/gateways",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
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,6 +52,11 @@
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": {
@@ -61,11 +66,13 @@
61
66
  "@ai-sdk/anthropic": "4.0.75",
62
67
  "@ai-sdk/openai": "4.0.88",
63
68
  "@ai-sdk/openai-compatible": "3.0.65",
64
- "@ai-sdk/provider": "^4.0.24",
69
+ "@ai-sdk/provider": "4.0.24",
70
+ "@emulators/ai": "0.14.0",
65
71
  "@types/node": "^24.19.1",
66
72
  "@vite-hub/internal": "0.0.1",
67
73
  "advocaat": "^0.0.6",
68
74
  "ai": "7.0.130",
75
+ "emulate": "0.14.0",
69
76
  "publint": "^0.3.25",
70
77
  "typescript": "^6.0.3",
71
78
  "vite-plus": "^0.3.3",