tuxevil-rotator 3.0.0 → 3.2.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 +37 -0
- package/README.md +20 -6
- package/package.json +1 -1
- package/src/account-store.ts +37 -5
- package/src/cli.ts +24 -3
- package/src/compat/model-specs.ts +1 -0
- package/src/compat.ts +540 -24
- package/src/config-normalize.ts +73 -30
- package/src/dashboard.ts +43 -42
- package/src/index.ts +1 -0
- package/src/onboarding.ts +349 -21
- package/src/providers/adapter.ts +18 -0
- package/src/providers/credential-helpers.ts +64 -0
- package/src/providers/google-antigravity/index.ts +9 -0
- package/src/providers/google-antigravity/quota.ts +2 -1
- package/src/providers/google-antigravity/translators.ts +83 -24
- package/src/providers/ollama/index.ts +8 -0
- package/src/providers/ollama/quota.ts +2 -1
- package/src/providers/openai-codex/catalog.ts +117 -0
- package/src/providers/openai-codex/compat.ts +556 -0
- package/src/providers/openai-codex/credentials.ts +82 -0
- package/src/providers/openai-codex/forward.ts +240 -0
- package/src/providers/openai-codex/index.ts +153 -0
- package/src/providers/openai-codex/login.ts +172 -0
- package/src/providers/openai-codex/oauth.ts +359 -0
- package/src/providers/openai-codex/quota.ts +231 -0
- package/src/providers/opencode-zen/catalog.ts +37 -0
- package/src/providers/opencode-zen/credentials.ts +31 -0
- package/src/providers/opencode-zen/forward.ts +214 -0
- package/src/providers/opencode-zen/index.ts +73 -0
- package/src/providers/opencode-zen/login.ts +81 -0
- package/src/providers/opencode-zen/quota.ts +67 -0
- package/src/providers/registry.ts +24 -5
- package/src/proxy.ts +75 -27
- package/src/rate-limit-parser.ts +2 -1
- package/src/rotator.ts +259 -48
- package/src/spend-logger.ts +2 -0
- package/src/static/dashboard-keys.js +152 -2
- package/src/static/dashboard.js +161 -37
- package/src/types.ts +83 -1
- package/src/validators.ts +11 -2
- package/tools/telemetry-receiver/receiver.js +14 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// OpenCode Zen request forwarding and SSE stream accumulation.
|
|
2
|
+
|
|
3
|
+
import type { AccountRuntime } from "../../types.js";
|
|
4
|
+
import type { ForwardedResponse, RequestBody } from "../../proxy.js";
|
|
5
|
+
import type { StreamAccumulator, TokenUsage } from "../adapter.js";
|
|
6
|
+
import { getAccountProxyDispatcher } from "../proxy-dispatcher.js";
|
|
7
|
+
import type { RequestInitWithDispatcher } from "../../fetch-with-retry.js";
|
|
8
|
+
import { getOpenCodeZenApiKey, OPENCODE_ZEN_PROVIDER_ID } from "./credentials.js";
|
|
9
|
+
import { OPENCODE_ZEN_CHAT_URL } from "./catalog.js";
|
|
10
|
+
import { isRecord } from "../../compat/schema-sanitizer.js";
|
|
11
|
+
|
|
12
|
+
const HOP_BY_HOP = new Set([
|
|
13
|
+
"connection",
|
|
14
|
+
"keep-alive",
|
|
15
|
+
"proxy-authenticate",
|
|
16
|
+
"proxy-authorization",
|
|
17
|
+
"te",
|
|
18
|
+
"trailers",
|
|
19
|
+
"transfer-encoding",
|
|
20
|
+
"upgrade",
|
|
21
|
+
"host",
|
|
22
|
+
"content-length",
|
|
23
|
+
"x-forwarded-for",
|
|
24
|
+
"x-forwarded-host",
|
|
25
|
+
"x-forwarded-proto",
|
|
26
|
+
"x-forwarded-port",
|
|
27
|
+
"x-real-ip",
|
|
28
|
+
"forwarded",
|
|
29
|
+
"via",
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
export function buildOpenCodeZenPayload(body: RequestBody): Record<string, unknown> {
|
|
33
|
+
const request = isRecord(body.request) ? body.request : {};
|
|
34
|
+
// OpenCode Zen does not support the `developer` role (an OpenAI o1/o3 extension).
|
|
35
|
+
// Normalise it to `system` so the upstream deserialises the request correctly.
|
|
36
|
+
const messages = Array.isArray(request.messages)
|
|
37
|
+
? (request.messages as unknown[]).map((msg) => {
|
|
38
|
+
if (isRecord(msg) && msg.role === "developer") {
|
|
39
|
+
return { ...msg, role: "system" };
|
|
40
|
+
}
|
|
41
|
+
return msg;
|
|
42
|
+
})
|
|
43
|
+
: request.messages;
|
|
44
|
+
return {
|
|
45
|
+
...request,
|
|
46
|
+
...(messages !== request.messages ? { messages } : {}),
|
|
47
|
+
model: body.model,
|
|
48
|
+
stream: Boolean(request.stream),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function forwardRequest(
|
|
53
|
+
account: AccountRuntime,
|
|
54
|
+
body: RequestBody,
|
|
55
|
+
originalHeaders: Record<string, string>,
|
|
56
|
+
signal?: AbortSignal,
|
|
57
|
+
): Promise<ForwardedResponse> {
|
|
58
|
+
const payload = buildOpenCodeZenPayload(body);
|
|
59
|
+
const apiKey = getOpenCodeZenApiKey(account.config) ?? "";
|
|
60
|
+
|
|
61
|
+
const forwardHeaders: Record<string, string> = {};
|
|
62
|
+
for (const [key, value] of Object.entries(originalHeaders)) {
|
|
63
|
+
const lower = key.toLowerCase();
|
|
64
|
+
if (
|
|
65
|
+
!HOP_BY_HOP.has(lower) &&
|
|
66
|
+
lower !== "authorization" &&
|
|
67
|
+
lower !== "content-type" &&
|
|
68
|
+
lower !== "accept"
|
|
69
|
+
) {
|
|
70
|
+
forwardHeaders[key] = value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
forwardHeaders["Authorization"] = `Bearer ${apiKey}`;
|
|
75
|
+
forwardHeaders["Content-Type"] = "application/json";
|
|
76
|
+
forwardHeaders["Accept"] = payload.stream === true ? "text/event-stream" : "application/json";
|
|
77
|
+
|
|
78
|
+
const endpoint = OPENCODE_ZEN_CHAT_URL;
|
|
79
|
+
const response = await fetch(endpoint, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
headers: forwardHeaders,
|
|
82
|
+
body: JSON.stringify(payload),
|
|
83
|
+
signal,
|
|
84
|
+
dispatcher: getAccountProxyDispatcher(account, OPENCODE_ZEN_PROVIDER_ID),
|
|
85
|
+
} as RequestInitWithDispatcher);
|
|
86
|
+
|
|
87
|
+
return { response, endpoint };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function extractUsageFromRecord(record: Record<string, unknown>): TokenUsage | null {
|
|
91
|
+
const usage = isRecord(record.usage) ? record.usage : record;
|
|
92
|
+
const inputTokens =
|
|
93
|
+
typeof usage.prompt_tokens === "number"
|
|
94
|
+
? usage.prompt_tokens
|
|
95
|
+
: typeof usage.input_tokens === "number"
|
|
96
|
+
? usage.input_tokens
|
|
97
|
+
: 0;
|
|
98
|
+
const outputTokens =
|
|
99
|
+
typeof usage.completion_tokens === "number"
|
|
100
|
+
? usage.completion_tokens
|
|
101
|
+
: typeof usage.output_tokens === "number"
|
|
102
|
+
? usage.output_tokens
|
|
103
|
+
: 0;
|
|
104
|
+
|
|
105
|
+
return inputTokens > 0 || outputTokens > 0
|
|
106
|
+
? { inputTokens, outputTokens }
|
|
107
|
+
: null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class OpenCodeZenSseAccumulator implements StreamAccumulator {
|
|
111
|
+
private buffer = "";
|
|
112
|
+
private accumulatedText = "";
|
|
113
|
+
private usage: TokenUsage | null = null;
|
|
114
|
+
|
|
115
|
+
append(chunkText: string): TokenUsage | null {
|
|
116
|
+
this.buffer += chunkText;
|
|
117
|
+
let newlyFound: TokenUsage | null = null;
|
|
118
|
+
let newlineIdx = this.buffer.indexOf("\n");
|
|
119
|
+
|
|
120
|
+
while (newlineIdx >= 0) {
|
|
121
|
+
const line = this.buffer.slice(0, newlineIdx).trim();
|
|
122
|
+
this.buffer = this.buffer.slice(newlineIdx + 1);
|
|
123
|
+
|
|
124
|
+
if (line.startsWith("data:")) {
|
|
125
|
+
const payload = line.slice(5).trim();
|
|
126
|
+
if (payload && payload !== "[DONE]") {
|
|
127
|
+
try {
|
|
128
|
+
const parsed = JSON.parse(payload) as Record<string, unknown>;
|
|
129
|
+
|
|
130
|
+
// Extract delta text if present
|
|
131
|
+
if (Array.isArray(parsed.choices) && parsed.choices.length > 0) {
|
|
132
|
+
const choice = parsed.choices[0];
|
|
133
|
+
if (isRecord(choice) && isRecord(choice.delta)) {
|
|
134
|
+
const content = choice.delta.content;
|
|
135
|
+
if (typeof content === "string" && this.accumulatedText.length < 100000) {
|
|
136
|
+
this.accumulatedText += content;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const extractedUsage = extractUsageFromRecord(parsed);
|
|
142
|
+
if (extractedUsage) {
|
|
143
|
+
this.usage = extractedUsage;
|
|
144
|
+
newlyFound = extractedUsage;
|
|
145
|
+
}
|
|
146
|
+
} catch {
|
|
147
|
+
// Ignore malformed SSE lines
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
newlineIdx = this.buffer.indexOf("\n");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return newlyFound;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
getText(): string {
|
|
159
|
+
return this.accumulatedText;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
final(): TokenUsage | null {
|
|
163
|
+
if (this.buffer.trim()) {
|
|
164
|
+
this.append(`${this.buffer}\n`);
|
|
165
|
+
}
|
|
166
|
+
return this.usage;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const BENCHMARK_MODEL = "deepseek-v4-flash-free";
|
|
171
|
+
|
|
172
|
+
export function getBenchmarkSpec(): {
|
|
173
|
+
body: RequestBody;
|
|
174
|
+
parseUsage(raw: string): { outputTokens: number } | null;
|
|
175
|
+
parseText(raw: string): string;
|
|
176
|
+
} {
|
|
177
|
+
return {
|
|
178
|
+
body: {
|
|
179
|
+
project: "",
|
|
180
|
+
model: BENCHMARK_MODEL,
|
|
181
|
+
request: {
|
|
182
|
+
model: BENCHMARK_MODEL,
|
|
183
|
+
messages: [{ role: "user", content: "Reply with: OK" }],
|
|
184
|
+
stream: false,
|
|
185
|
+
max_tokens: 16,
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
parseUsage(raw: string) {
|
|
189
|
+
try {
|
|
190
|
+
const parsed = JSON.parse(raw) as Record<string, unknown>;
|
|
191
|
+
const usage = extractUsageFromRecord(parsed);
|
|
192
|
+
return usage ? { outputTokens: usage.outputTokens } : null;
|
|
193
|
+
} catch {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
parseText(raw: string) {
|
|
198
|
+
try {
|
|
199
|
+
const parsed = JSON.parse(raw) as Record<string, unknown>;
|
|
200
|
+
if (Array.isArray(parsed.choices) && parsed.choices.length > 0) {
|
|
201
|
+
const choice = parsed.choices[0];
|
|
202
|
+
if (isRecord(choice) && isRecord(choice.message)) {
|
|
203
|
+
return typeof choice.message.content === "string"
|
|
204
|
+
? choice.message.content
|
|
205
|
+
: "";
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return raw.slice(0, 1000);
|
|
209
|
+
} catch {
|
|
210
|
+
return raw.slice(0, 1000);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// OpenCode Zen provider adapter.
|
|
2
|
+
|
|
3
|
+
import type { ProviderAdapter, ProviderFeatures } from "../adapter.js";
|
|
4
|
+
import { runLogin } from "./login.js";
|
|
5
|
+
import { getOpenCodeZenApiKey, validateCredentials, OPENCODE_ZEN_PROVIDER_ID } from "./credentials.js";
|
|
6
|
+
import { forwardRequest, OpenCodeZenSseAccumulator, getBenchmarkSpec } from "./forward.js";
|
|
7
|
+
import { fetchOpenCodeZenQuota } from "./quota.js";
|
|
8
|
+
import { isOpenCodeZenModel } from "./catalog.js";
|
|
9
|
+
import type { AccountRuntime } from "../../types.js";
|
|
10
|
+
|
|
11
|
+
const OPENCODE_ZEN_TIER_RANKING = {
|
|
12
|
+
free: 0,
|
|
13
|
+
unknown: 1,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const opencodeZenAdapter: ProviderAdapter = {
|
|
17
|
+
id: OPENCODE_ZEN_PROVIDER_ID,
|
|
18
|
+
displayName: "OpenCode Zen",
|
|
19
|
+
credentialKind: "api-key",
|
|
20
|
+
tierRanking: OPENCODE_ZEN_TIER_RANKING,
|
|
21
|
+
|
|
22
|
+
features: {
|
|
23
|
+
circuitBreakers: true,
|
|
24
|
+
concurrencyLimits: true,
|
|
25
|
+
proactiveRotation: false,
|
|
26
|
+
} satisfies ProviderFeatures,
|
|
27
|
+
|
|
28
|
+
runLogin,
|
|
29
|
+
|
|
30
|
+
validateCredentials,
|
|
31
|
+
|
|
32
|
+
hasValidCredentials(account: AccountRuntime): boolean {
|
|
33
|
+
const key = getOpenCodeZenApiKey(account.config);
|
|
34
|
+
return typeof key === "string" && key.trim().length > 0;
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
async ensureValidToken(): Promise<void> {},
|
|
38
|
+
|
|
39
|
+
getAuthHeader(account: AccountRuntime): string {
|
|
40
|
+
const key = getOpenCodeZenApiKey(account.config) ?? "";
|
|
41
|
+
return `Bearer ${key}`;
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
shouldRetryOnQuotaExhaustion(): boolean {
|
|
45
|
+
return true;
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
async fetchQuota(account: AccountRuntime, ctx): Promise<void> {
|
|
49
|
+
await fetchOpenCodeZenQuota(account, ctx);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
forwardRequest,
|
|
53
|
+
|
|
54
|
+
createStreamAccumulator: () => new OpenCodeZenSseAccumulator(),
|
|
55
|
+
|
|
56
|
+
getKickstartModelForPool(): string | undefined {
|
|
57
|
+
return undefined;
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
ownsModel(model: string): boolean {
|
|
61
|
+
return isOpenCodeZenModel(model);
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
getPoolKey(model: string): string {
|
|
65
|
+
return `${OPENCODE_ZEN_PROVIDER_ID}:${model}`;
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
getBenchmark() {
|
|
69
|
+
return getBenchmarkSpec();
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export { OPENCODE_ZEN_PROVIDER_ID };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// OpenCode Zen interactive login flow.
|
|
2
|
+
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
4
|
+
import type { AccountConfig } from "../../types.js";
|
|
5
|
+
import { OPENCODE_ZEN_MODELS_URL } from "./catalog.js";
|
|
6
|
+
import { defaultAccountEmail, OPENCODE_ZEN_PROVIDER_ID } from "./credentials.js";
|
|
7
|
+
|
|
8
|
+
function askQuestion(prompt: string): Promise<string> {
|
|
9
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
10
|
+
return new Promise((resolve) => {
|
|
11
|
+
rl.question(prompt, (answer) => {
|
|
12
|
+
rl.close();
|
|
13
|
+
resolve(answer.trim());
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function validateApiKey(
|
|
19
|
+
apiKey: string,
|
|
20
|
+
): Promise<{ ok: true } | { ok: false; status: number; error: string }> {
|
|
21
|
+
try {
|
|
22
|
+
const res = await fetch(OPENCODE_ZEN_MODELS_URL, {
|
|
23
|
+
method: "GET",
|
|
24
|
+
headers: {
|
|
25
|
+
Authorization: `Bearer ${apiKey}`,
|
|
26
|
+
Accept: "application/json",
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (!res.ok) {
|
|
31
|
+
const text = await res.text().catch(() => "");
|
|
32
|
+
return {
|
|
33
|
+
ok: false,
|
|
34
|
+
status: res.status,
|
|
35
|
+
error: `HTTP ${res.status}: ${text.slice(0, 200) || res.statusText}`,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return { ok: true };
|
|
40
|
+
} catch (err) {
|
|
41
|
+
return {
|
|
42
|
+
ok: false,
|
|
43
|
+
status: 0,
|
|
44
|
+
error: err instanceof Error ? err.message : String(err),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function runLogin(): Promise<AccountConfig> {
|
|
50
|
+
console.log("=== Tuxevil Rotator - Add OpenCode Zen Account ===");
|
|
51
|
+
console.log();
|
|
52
|
+
|
|
53
|
+
const apiKey = await askQuestion("Paste your OpenCode Zen API key: ");
|
|
54
|
+
|
|
55
|
+
if (!apiKey) {
|
|
56
|
+
console.error("No API key provided.");
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
console.log("Validating API key against opencode.ai/zen...");
|
|
61
|
+
const result = await validateApiKey(apiKey);
|
|
62
|
+
if (!result.ok) {
|
|
63
|
+
console.error(`API key validation failed: ${result.error}`);
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const email = defaultAccountEmail(apiKey);
|
|
68
|
+
const defaultLabel = email.split("@")[0];
|
|
69
|
+
const label = await askQuestion(`Account label (default: ${defaultLabel}): `);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
email,
|
|
73
|
+
credentials: [
|
|
74
|
+
{
|
|
75
|
+
provider: OPENCODE_ZEN_PROVIDER_ID,
|
|
76
|
+
apiKey,
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
label: label || defaultLabel,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// OpenCode Zen quota polling.
|
|
2
|
+
|
|
3
|
+
import { fetchWithRetry } from "../../fetch-with-retry.js";
|
|
4
|
+
import type { AccountRuntime, ModelQuota } from "../../types.js";
|
|
5
|
+
import type { QuotaFetchContext } from "../adapter.js";
|
|
6
|
+
import { getAccountProxyDispatcher } from "../proxy-dispatcher.js";
|
|
7
|
+
import { OPENCODE_ZEN_MODELS_URL } from "./catalog.js";
|
|
8
|
+
import { getOpenCodeZenApiKey, OPENCODE_ZEN_PROVIDER_ID } from "./credentials.js";
|
|
9
|
+
import { sortQuotaPools } from "../registry.js";
|
|
10
|
+
|
|
11
|
+
export async function fetchOpenCodeZenQuota(
|
|
12
|
+
account: AccountRuntime,
|
|
13
|
+
ctx: QuotaFetchContext,
|
|
14
|
+
): Promise<void> {
|
|
15
|
+
const apiKey = getOpenCodeZenApiKey(account.config);
|
|
16
|
+
if (!apiKey) return;
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const response = await fetchWithRetry(OPENCODE_ZEN_MODELS_URL, {
|
|
20
|
+
method: "GET",
|
|
21
|
+
headers: {
|
|
22
|
+
Authorization: `Bearer ${apiKey}`,
|
|
23
|
+
Accept: "application/json",
|
|
24
|
+
},
|
|
25
|
+
timeoutMs: 8000,
|
|
26
|
+
dispatcher: getAccountProxyDispatcher(account, OPENCODE_ZEN_PROVIDER_ID),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
if (response.status === 401 || response.status === 403) {
|
|
31
|
+
const errorText = await response.text().catch(() => "");
|
|
32
|
+
ctx.log(`${account.config.email}: OpenCode Zen API returned ${response.status}, flagging account`);
|
|
33
|
+
ctx.reportQuotaPollFlag(account, response.status, errorText);
|
|
34
|
+
ctx.markFlagged(
|
|
35
|
+
account,
|
|
36
|
+
`OpenCode Zen API ${response.status}: ${errorText}`,
|
|
37
|
+
{ triggerProtectivePause: false },
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const oldQuota = account.quota || [];
|
|
44
|
+
const otherProviders = oldQuota.filter(
|
|
45
|
+
(q) => (q as { providerId?: string }).providerId &&
|
|
46
|
+
(q as { providerId?: string }).providerId !== OPENCODE_ZEN_PROVIDER_ID,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const freshQuota: ModelQuota = {
|
|
50
|
+
modelKey: OPENCODE_ZEN_PROVIDER_ID,
|
|
51
|
+
displayName: "OpenCode Zen Free Pool",
|
|
52
|
+
providerId: OPENCODE_ZEN_PROVIDER_ID,
|
|
53
|
+
percentRemaining: 100,
|
|
54
|
+
resetTime: null,
|
|
55
|
+
timerType: "fresh",
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
account.quota = sortQuotaPools([...otherProviders, freshQuota]);
|
|
59
|
+
account.lastQuotaPoll = Date.now();
|
|
60
|
+
|
|
61
|
+
account.lastPollByProvider ??= {};
|
|
62
|
+
account.lastPollByProvider[OPENCODE_ZEN_PROVIDER_ID] =
|
|
63
|
+
`[${freshQuota.modelKey}: ${freshQuota.timerType} ${freshQuota.percentRemaining}%]`;
|
|
64
|
+
} catch {
|
|
65
|
+
// Swallow network errors; retain prior quota state
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -9,14 +9,12 @@
|
|
|
9
9
|
import type { ProviderAdapter } from "./adapter.js";
|
|
10
10
|
import { googleAntigravityAdapter } from "./google-antigravity/index.js";
|
|
11
11
|
import { ollamaAdapter } from "./ollama/index.js";
|
|
12
|
+
import { openaiCodexAdapter } from "./openai-codex/index.js";
|
|
13
|
+
import { opencodeZenAdapter } from "./opencode-zen/index.js";
|
|
12
14
|
import { logger } from "../logger.js";
|
|
13
15
|
import {
|
|
14
16
|
DEFAULT_PROVIDER,
|
|
15
17
|
primaryProviderId,
|
|
16
|
-
hasCredential,
|
|
17
|
-
getCredential,
|
|
18
|
-
type CredentialEntry,
|
|
19
|
-
type AccountLike as ProviderCredentialLike,
|
|
20
18
|
} from "./credential-helpers.js";
|
|
21
19
|
|
|
22
20
|
export {
|
|
@@ -26,11 +24,20 @@ export {
|
|
|
26
24
|
getCredential,
|
|
27
25
|
type CredentialEntry,
|
|
28
26
|
type AccountLike as ProviderCredentialLike,
|
|
27
|
+
PROVIDER_ORDER,
|
|
28
|
+
PROVIDER_ORDER_RANK,
|
|
29
|
+
getProviderIdForPoolKey,
|
|
30
|
+
getQuotaItemProviderId,
|
|
31
|
+
sortQuotaPools,
|
|
32
|
+
sortAccountCredentials,
|
|
33
|
+
type QuotaPoolLike,
|
|
29
34
|
} from "./credential-helpers.js";
|
|
30
35
|
|
|
31
36
|
const PROVIDERS: Record<string, ProviderAdapter> = {
|
|
32
37
|
"google-antigravity": googleAntigravityAdapter,
|
|
33
38
|
ollama: ollamaAdapter,
|
|
39
|
+
"openai-codex": openaiCodexAdapter,
|
|
40
|
+
"opencode-zen": opencodeZenAdapter,
|
|
34
41
|
};
|
|
35
42
|
|
|
36
43
|
export function getProviderAdapter(providerId: string): ProviderAdapter {
|
|
@@ -51,6 +58,19 @@ export function isKnownProvider(providerId: string): boolean {
|
|
|
51
58
|
return Object.prototype.hasOwnProperty.call(PROVIDERS, providerId);
|
|
52
59
|
}
|
|
53
60
|
|
|
61
|
+
export function findProviderForModel(
|
|
62
|
+
model: string,
|
|
63
|
+
context?: { ollamaModels?: Set<string>; codexModels?: Set<string> },
|
|
64
|
+
): ProviderAdapter | null {
|
|
65
|
+
// Check non-default explicit providers first to prevent Google fallback overlap
|
|
66
|
+
for (const adapter of [opencodeZenAdapter, openaiCodexAdapter, ollamaAdapter, googleAntigravityAdapter]) {
|
|
67
|
+
if (adapter.ownsModel?.(model, context)) {
|
|
68
|
+
return adapter;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
54
74
|
export function getProviderForAccount(
|
|
55
75
|
account: { credentials?: Array<{ provider: string }>; provider?: string },
|
|
56
76
|
providerId?: string,
|
|
@@ -67,4 +87,3 @@ export function getProviderForAccount(
|
|
|
67
87
|
return getProviderAdapter(DEFAULT_PROVIDER);
|
|
68
88
|
}
|
|
69
89
|
}
|
|
70
|
-
|