Merge remote-tracking branch 'upstream/master'

# Conflicts:
#	open-sse/handlers/chatCore.js
#	open-sse/providers/registry/index.js
#	open-sse/services/usage.js
This commit is contained in:
MUH. IQRAM BAHRING committed 2026-08-30 23:23:48 +08:00
commit eaf204b83c
114 files changed
+5587 -2184

No files matched your search

@@ -2,8 +2,8 @@
import { useEffect, useMemo, useRef, useState } from "react";
import { UPDATER_CONFIG } from "@/shared/constants/config";
import { readPresets, upsertPreset, deletePreset, subscribePresets, stripSlash } from "./cliEndpointPresets";
const STORAGE_KEY = "9router.cliToolEndpointPresets";
const CUSTOM_VALUE = "__custom__";
const SAVE_VALUE = "__save__";
@@ -13,22 +13,6 @@ const ensureV1 = (url) => {
return /\/v1$/.test(trimmed) ? trimmed : `${trimmed}/v1`;
};
const readSavedPresets = () => {
if (typeof window === "undefined") return [];
try {
const raw = JSON.parse(window.localStorage.getItem(STORAGE_KEY) || "[]");
if (!Array.isArray(raw)) return [];
return raw.filter((p) => p?.name && p?.baseUrl);
} catch {
return [];
}
};
const writeSavedPresets = (presets) => {
if (typeof window === "undefined") return;
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(presets));
};
const buildOptions = ({ requiresExternalUrl, tunnelEnabled, tunnelPublicUrl, tailscaleEnabled, tailscaleUrl, cloudEnabled, cloudUrl, savedPresets, withV1 }) => {
const opts = [];
const wrap = (url) => (withV1 ? ensureV1(url) : (url || "").replace(/\/+$/, ""));
@@ -66,14 +50,34 @@ export default function BaseUrlSelect({
cloudEnabled = false,
cloudUrl = "",
withV1 = true,
currentUrl = "",
}) {
const [savedPresets, setSavedPresets] = useState([]);
const [presetsLoaded, setPresetsLoaded] = useState(false);
const [mode, setMode] = useState("");
const [customInput, setCustomInput] = useState("");
const initializedRef = useRef(false);
const customInputRef = useRef("");
useEffect(() => {
setSavedPresets(readSavedPresets());
const sync = () => {
const presets = readPresets();
setSavedPresets(presets);
// A preset saved elsewhere (e.g. on Apply) takes over the custom slot
setMode((prev) => {
if (prev !== CUSTOM_VALUE) return prev;
const typed = stripSlash(customInputRef.current);
if (!typed) return prev;
const match = presets.find((p) => {
const saved = stripSlash(p.baseUrl);
return saved === typed || saved === ensureV1(typed);
});
return match ? `saved:${match.name}` : prev;
});
};
sync();
setPresetsLoaded(true);
return subscribePresets(sync);
}, []);
const options = useMemo(
@@ -81,19 +85,23 @@ export default function BaseUrlSelect({
[requiresExternalUrl, tunnelEnabled, tunnelPublicUrl, tailscaleEnabled, tailscaleUrl, cloudEnabled, cloudUrl, savedPresets, withV1]
);
// Always default to first option (127.0.0.1) on mount, ignore persisted value
// Prefer a saved preset matching the currently configured URL, else first option
useEffect(() => {
if (initializedRef.current) return;
if (options.length === 0) return;
if (!presetsLoaded || options.length === 0) return;
initializedRef.current = true;
const first = options.find((o) => o.value !== CUSTOM_VALUE);
if (first) {
setMode(first.value);
onChange(first.url);
const current = stripSlash(currentUrl);
const matched = current
? options.find((o) => o.saved && stripSlash(o.url) === current)
: null;
const target = matched || options.find((o) => o.value !== CUSTOM_VALUE);
if (target) {
setMode(target.value);
onChange(target.url);
} else {
setMode(CUSTOM_VALUE);
}
}, [options, onChange]);
}, [presetsLoaded, options, onChange, currentUrl]);
const handleSelect = (e) => {
const next = e.target.value;
@@ -103,11 +111,8 @@ export default function BaseUrlSelect({
let defaultName = trimmed;
try { defaultName = new URL(trimmed).host; } catch {}
const name = window.prompt("Save endpoint as:", defaultName);
if (!name?.trim()) return;
const updated = [...savedPresets.filter((p) => p.name !== name.trim()), { name: name.trim(), baseUrl: trimmed }]
.sort((a, b) => a.name.localeCompare(b.name));
setSavedPresets(updated);
writeSavedPresets(updated);
const saved = name?.trim() ? upsertPreset(trimmed, name.trim()) : null;
if (saved) setMode(`saved:${saved}`);
return;
}
setMode(next);
@@ -122,19 +127,23 @@ export default function BaseUrlSelect({
const handleCustomInput = (e) => {
const v = e.target.value;
customInputRef.current = v;
setCustomInput(v);
onChange(v);
};
const handleDeleteSaved = () => {
if (!mode.startsWith("saved:")) return;
const name = mode.slice(6);
const updated = savedPresets.filter((p) => p.name !== name);
setSavedPresets(updated);
writeSavedPresets(updated);
setMode(CUSTOM_VALUE);
deletePreset(mode.slice(6));
setCustomInput("");
onChange("");
const fallback = options.find((o) => o.value !== CUSTOM_VALUE && o.value !== mode);
if (fallback) {
setMode(fallback.value);
onChange(fallback.url);
} else {
setMode(CUSTOM_VALUE);
onChange("");
}
};
const isSaved = mode.startsWith("saved:");
@@ -4,6 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal, Tooltip } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -53,6 +54,8 @@ export default function ClaudeToolCard({
const [maxContextTokens, setMaxContextTokens] = useState("");
const hasInitializedModels = useRef(false);
const currentBaseUrl = claudeStatus?.settings?.env?.ANTHROPIC_BASE_URL || "";
const getConfigStatus = () => {
if (!claudeStatus?.installed) return null;
const currentUrl = claudeStatus.settings?.env?.ANTHROPIC_BASE_URL;
@@ -189,6 +192,8 @@ export default function ClaudeToolCard({
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
setClaudeStatus(prev => ({ ...prev, hasBackup: true, settings: { ...prev?.settings, env }, exaMcpEnabled }));
} else {
@@ -334,6 +339,7 @@ export default function ClaudeToolCard({
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -4,6 +4,7 @@ import { useState, useEffect } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -50,6 +51,8 @@ export default function ClineToolCard({ tool, isExpanded, onToggle, baseUrl, api
}
};
const currentBaseUrl = status?.settings?.openAiBaseUrl || "";
const getConfigStatus = () => {
if (!status?.installed) return null;
if (!status.has9Router) return "not_configured";
@@ -94,6 +97,8 @@ export default function ClineToolCard({ tool, isExpanded, onToggle, baseUrl, api
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkStatus();
} else {
@@ -226,6 +231,7 @@ export default function ClineToolCard({ tool, isExpanded, onToggle, baseUrl, api
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -6,6 +6,7 @@ import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
import { rememberEndpoint } from "./cliEndpointPresets";
export default function CodexToolCard({ tool, isExpanded, onToggle, baseUrl, apiKeys, activeProviders, cloudEnabled, initialStatus, tunnelEnabled, tunnelPublicUrl, tailscaleEnabled, tailscaleUrl }) {
const [codexStatus, setCodexStatus] = useState(initialStatus || null);
@@ -57,17 +58,22 @@ export default function CodexToolCard({ tool, isExpanded, onToggle, baseUrl, api
if (modelMatch) setSelectedModel(modelMatch[1]);
// Parse subagent settings
const subagentModelMatch = codexStatus.config.match(/\[agents\.subagent\]\s*\n\s*model\s*=\s*"([^"]+)"/m);
const subagentModelMatch = codexStatus.config.match(/^default_subagent_model\s*=\s*"([^"]+)"/m);
if (subagentModelMatch) setSubagentModel(subagentModelMatch[1]);
}
}, [codexStatus]);
const getCurrentBaseUrl = () => {
const parsed = codexStatus?.config?.match(/base_url\s*=\s*"([^"]+)"/);
return parsed ? parsed[1] : "";
};
const currentBaseUrl = getCurrentBaseUrl();
const getConfigStatus = () => {
if (!codexStatus?.installed) return null;
if (!codexStatus.config) return "not_configured";
const parsed = codexStatus.config.match(/base_url\s*=\s*"([^"]+)"/);
const currentUrl = parsed ? parsed[1] : "";
return matchKnownEndpoint(currentUrl, { tunnelPublicUrl, tailscaleUrl }) ? "configured" : "other";
return matchKnownEndpoint(currentBaseUrl, { tunnelPublicUrl, tailscaleUrl }) ? "configured" : "other";
};
const configStatus = getConfigStatus();
@@ -114,6 +120,8 @@ export default function CodexToolCard({ tool, isExpanded, onToggle, baseUrl, api
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkCodexStatus();
} else {
@@ -172,24 +180,18 @@ name = "9Router"
base_url = "${getEffectiveBaseUrl()}"
wire_api = "responses"
[agents.subagent]
model = "${effectiveSubagentModel}"
`;
[model_providers.9router.http_headers]
Authorization = "Bearer ${keyToUse}"
const authContent = JSON.stringify({
auth_mode: "apikey",
OPENAI_API_KEY: keyToUse
}, null, 2);
[agents]
default_subagent_model = "${effectiveSubagentModel}"
`;
return [
{
filename: "~/.codex/config.toml",
content: configContent,
},
{
filename: "~/.codex/auth.json",
content: authContent,
},
];
};
@@ -254,7 +256,7 @@ model = "${effectiveSubagentModel}"
<p className="text-text-muted">After installation, run <code className="px-1 bg-black/5 dark:bg-white/5 rounded">codex</code> to verify.</p>
<div className="pt-2 border-t border-border">
<p className="text-text-muted text-xs">
Codex uses <code className="px-1 bg-black/5 dark:bg-white/5 rounded">~/.codex/auth.json</code> with <code className="px-1 bg-black/5 dark:bg-white/5 rounded">OPENAI_API_KEY</code>.
Codex reads custom providers from <code className="px-1 bg-black/5 dark:bg-white/5 rounded">~/.codex/config.toml</code>.
Click &quot;Apply&quot; to auto-configure.
</p>
</div>
@@ -279,13 +281,12 @@ model = "${effectiveSubagentModel}"
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
{/* Current configured */}
{codexStatus?.config && (() => {
const parsed = codexStatus.config.match(/base_url\s*=\s*"([^"]+)"/);
const currentBaseUrl = parsed ? parsed[1] : null;
return currentBaseUrl ? (
<div className="grid grid-cols-1 gap-1.5 sm:grid-cols-[8rem_auto_1fr_auto] sm:items-center sm:gap-2">
<span className="text-xs font-semibold text-text-main sm:text-right sm:text-sm">Current</span>
@@ -4,6 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -77,6 +78,8 @@ export default function CopilotToolCard({ tool, isExpanded, onToggle, baseUrl, a
}
};
const currentBaseUrl = status?.currentUrl || "";
const getConfigStatus = () => {
if (!status) return null;
if (!status.has9Router) return "not_configured";
@@ -123,6 +126,8 @@ export default function CopilotToolCard({ tool, isExpanded, onToggle, baseUrl, a
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: data.message || "Settings applied! Reload VS Code." });
checkStatus();
} else {
@@ -230,6 +235,7 @@ export default function CopilotToolCard({ tool, isExpanded, onToggle, baseUrl, a
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -4,6 +4,7 @@ import { useState, useEffect } from "react";
import { Card, Button, ManualConfigModal, ComboFormModal, McpMarketplaceModal, ModelSelectModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
const ENDPOINT = "/api/cli-tools/cowork-settings";
@@ -110,6 +111,8 @@ export default function CoworkToolCard({
const getEffectiveBaseUrl = () => ensureV1(customBaseUrl);
const currentBaseUrl = status?.cowork?.baseUrl || "";
const getConfigStatus = () => {
if (!status?.installed) return null;
const url = status?.cowork?.baseUrl;
@@ -148,6 +151,8 @@ export default function CoworkToolCard({
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied. Quit & reopen Claude Desktop to load." });
checkStatus();
} else {
@@ -306,6 +311,7 @@ export default function CoworkToolCard({
tailscaleUrl={tailscaleUrl}
cloudEnabled={cloudEnabled}
cloudUrl={cloudUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -4,6 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -37,6 +38,8 @@ export default function DeepSeekTuiToolCard({
const [customBaseUrl, setCustomBaseUrl] = useState("");
const hasInitializedModel = useRef(false);
const currentBaseUrl = deepseekStatus?.settings?.["providers.openai"]?.base_url || "";
const getConfigStatus = () => {
if (!deepseekStatus?.installed) return null;
const openaiSection = deepseekStatus.settings?.["providers.openai"];
@@ -128,6 +131,8 @@ export default function DeepSeekTuiToolCard({
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkStatus();
} else {
@@ -263,6 +268,7 @@ model = "${selectedModel || "provider/model-id"}"
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -4,6 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -39,6 +40,8 @@ export default function DroidToolCard({
const [customBaseUrl, setCustomBaseUrl] = useState("");
const hasInitializedModel = useRef(false);
const currentBaseUrl = droidStatus?.settings?.customModels?.find((m) => m.id?.startsWith("custom:9Router"))?.baseUrl || "";
const getConfigStatus = () => {
if (!droidStatus?.installed) return null;
// Check for any 9Router model entry (support multi-model: custom:9Router-0, custom:9Router-1, ...)
@@ -154,6 +157,8 @@ export default function DroidToolCard({
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkDroidStatus();
} else {
@@ -299,6 +304,7 @@ export default function DroidToolCard({
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -5,6 +5,7 @@ import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/comp
import { useModelCaps } from "@/shared/hooks/useModelCaps";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -96,6 +97,7 @@ export default function GrokBuildToolCard({
const hasFetchedStatus = useRef(Boolean(initialStatus));
const configuredModel = grokStatus?.settings?.model;
const currentBaseUrl = configuredModel?.base_url || "";
const configStatus = !grokStatus?.installed
? null
: !configuredModel?.base_url
@@ -184,6 +186,8 @@ export default function GrokBuildToolCard({
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Main and subagent models applied successfully!" });
checkStatus();
} else {
@@ -310,7 +314,7 @@ export default function GrokBuildToolCard({
<div className="grid grid-cols-1 gap-1.5 sm:grid-cols-[8rem_auto_1fr] sm:items-center sm:gap-2">
<span className="text-xs font-semibold text-text-main sm:text-right sm:text-sm">Select Endpoint</span>
<span className="material-symbols-outlined hidden text-text-muted text-[14px] sm:inline">arrow_forward</span>
<BaseUrlSelect value={customBaseUrl || getEffectiveBaseUrl()} onChange={setCustomBaseUrl} requiresExternalUrl={tool.requiresExternalUrl} tunnelEnabled={tunnelEnabled} tunnelPublicUrl={tunnelPublicUrl} tailscaleEnabled={tailscaleEnabled} tailscaleUrl={tailscaleUrl} />
<BaseUrlSelect value={customBaseUrl || getEffectiveBaseUrl()} onChange={setCustomBaseUrl} requiresExternalUrl={tool.requiresExternalUrl} tunnelEnabled={tunnelEnabled} tunnelPublicUrl={tunnelPublicUrl} tailscaleEnabled={tailscaleEnabled} tailscaleUrl={tailscaleUrl} currentUrl={currentBaseUrl} />
</div>
{configuredModel?.base_url && (
@@ -4,6 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -37,6 +38,8 @@ export default function HermesToolCard({
const [customBaseUrl, setCustomBaseUrl] = useState("");
const hasInitializedModel = useRef(false);
const currentBaseUrl = hermesStatus?.settings?.model?.base_url || "";
const getConfigStatus = () => {
if (!hermesStatus?.installed) return null;
const cfg = hermesStatus.settings?.model;
@@ -128,6 +131,8 @@ export default function HermesToolCard({
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkStatus();
} else {
@@ -242,6 +247,7 @@ export default function HermesToolCard({
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -4,6 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -35,6 +36,8 @@ export default function JcodeToolCard({
const [customBaseUrl, setCustomBaseUrl] = useState("");
const hasInitializedModel = useRef(false);
const currentBaseUrl = jcodeStatus?.config?.providers?.["9router"]?.base_url || "";
const getConfigStatus = () => {
if (!jcodeStatus?.installed) return null;
if (!jcodeStatus?.has9Router) return "not_configured";
@@ -140,6 +143,8 @@ export default function JcodeToolCard({
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkJcodeStatus();
} else {
@@ -295,6 +300,7 @@ id = "${selectedModel || "cc/claude-opus-4-7"}"`;
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -4,6 +4,7 @@ import { useState, useEffect } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -88,6 +89,8 @@ export default function KiloToolCard({ tool, isExpanded, onToggle, baseUrl, apiK
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkStatus();
} else {
@@ -4,6 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -37,6 +38,8 @@ export default function OpenClawToolCard({
const [customBaseUrl, setCustomBaseUrl] = useState("");
const hasInitializedModel = useRef(false);
const currentBaseUrl = openclawStatus?.settings?.models?.providers?.["9router"]?.baseUrl || "";
const getConfigStatus = () => {
if (!openclawStatus?.installed) return null;
const currentProvider = openclawStatus.settings?.models?.providers?.["9router"];
@@ -146,6 +149,8 @@ export default function OpenClawToolCard({
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkOpenclawStatus();
} else {
@@ -291,6 +296,7 @@ export default function OpenClawToolCard({
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -4,6 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
import BaseUrlSelect from "./BaseUrlSelect";
import { rememberEndpoint } from "./cliEndpointPresets";
import ApiKeySelect from "./ApiKeySelect";
import { matchKnownEndpoint } from "./cliEndpointMatch";
@@ -94,6 +95,8 @@ export default function OpenCodeToolCard({ tool, isExpanded, onToggle, baseUrl,
}
};
const currentBaseUrl = status?.config?.provider?.["9router"]?.options?.baseURL || "";
const getConfigStatus = () => {
if (!status?.installed) return null;
if (!status.config) return "not_configured";
@@ -145,6 +148,8 @@ export default function OpenCodeToolCard({ tool, isExpanded, onToggle, baseUrl,
});
const data = await res.json();
if (res.ok) {
// Remember the endpoint so it stays selectable next time
rememberEndpoint(getEffectiveBaseUrl(), { tunnelPublicUrl, tailscaleUrl });
setMessage({ type: "success", text: "Settings applied successfully!" });
checkStatus();
} else {
@@ -297,6 +302,7 @@ export default function OpenCodeToolCard({ tool, isExpanded, onToggle, baseUrl,
tunnelPublicUrl={tunnelPublicUrl}
tailscaleEnabled={tailscaleEnabled}
tailscaleUrl={tailscaleUrl}
currentUrl={currentBaseUrl}
/>
</div>
@@ -0,0 +1,71 @@
import { UPDATER_CONFIG } from "@/shared/constants/config";
// Browser-local endpoint presets shared by every CLI tool card
const STORAGE_KEY = "9router.cliToolEndpointPresets";
const CHANGE_EVENT = "9router:endpoint-presets-changed";
const stripSlash = (url) => (url || "").replace(/\/+$/, "");
export function readPresets() {
if (typeof window === "undefined") return [];
try {
const raw = JSON.parse(window.localStorage.getItem(STORAGE_KEY) || "[]");
if (!Array.isArray(raw)) return [];
return raw.filter((p) => p?.name && p?.baseUrl);
} catch {
return [];
}
}
function writePresets(presets) {
if (typeof window === "undefined") return;
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(presets));
window.dispatchEvent(new CustomEvent(CHANGE_EVENT));
}
export function subscribePresets(handler) {
if (typeof window === "undefined") return () => {};
window.addEventListener(CHANGE_EVENT, handler);
return () => window.removeEventListener(CHANGE_EVENT, handler);
}
function defaultNameFor(url) {
try { return new URL(url).host; } catch { return url; }
}
// Adds or replaces a preset; returns the stored name, or null when skipped
export function upsertPreset(baseUrl, name) {
const url = stripSlash(baseUrl);
if (!url) return null;
const presets = readPresets();
const existing = presets.find((p) => stripSlash(p.baseUrl) === url);
if (existing && !name) return existing.name;
const finalName = (name || defaultNameFor(url)).trim();
if (!finalName) return null;
const next = [...presets.filter((p) => p.name !== finalName && stripSlash(p.baseUrl) !== url), { name: finalName, baseUrl: url }]
.sort((a, b) => a.name.localeCompare(b.name));
writePresets(next);
return finalName;
}
// Save an applied endpoint unless it exactly matches a built-in dropdown option
export function rememberEndpoint(baseUrl, { tunnelPublicUrl, tailscaleUrl, cloudUrl } = {}) {
const url = stripSlash(baseUrl);
if (!url) return null;
const builtIns = [`http://127.0.0.1:${UPDATER_CONFIG.appPort}`, tunnelPublicUrl, tailscaleUrl, cloudUrl]
.filter(Boolean)
.flatMap((u) => [stripSlash(u), `${stripSlash(u)}/v1`]);
if (builtIns.includes(url)) return null;
return upsertPreset(url);
}
export function deletePreset(name) {
writePresets(readPresets().filter((p) => p.name !== name));
}
export { stripSlash };
@@ -275,7 +275,7 @@ export function GenericExampleCard({ providerId, kind }) {
{/* API Key */}
<Row label="API Key">
<span className="px-3 py-1.5 text-sm font-mono text-text-main bg-sidebar rounded-lg truncate block">
{apiKey ? `${apiKey.slice(0, 8)}${"\u2022".repeat(Math.min(20, apiKey.length - 8))}` : <span className="text-text-muted italic">No key configured</span>}
{apiKey ? `${apiKey.slice(0, 8)}${"\u2022".repeat(Math.min(20, Math.max(0, apiKey.length - 8)))}` : <span className="text-text-muted italic">No key configured</span>}
</span>
</Row>
@@ -157,7 +157,7 @@ export function SttExampleCard({ providerId }) {
{/* API Key */}
<Row label="API Key">
<span className="px-3 py-1.5 text-sm font-mono text-text-main bg-sidebar rounded-lg truncate block">
{apiKey ? `${apiKey.slice(0, 8)}${"\u2022".repeat(Math.min(20, apiKey.length - 8))}` : <span className="text-text-muted italic">No key configured</span>}
{apiKey ? `${apiKey.slice(0, 8)}${"\u2022".repeat(Math.min(20, Math.max(0, apiKey.length - 8)))}` : <span className="text-text-muted italic">No key configured</span>}
</span>
</Row>
@@ -274,7 +274,7 @@ export function TtsExampleCard({ providerId }) {
<Row label="API Key">
<span className="px-3 py-1.5 text-sm font-mono text-text-main bg-sidebar rounded-lg truncate block">
{apiKey
? `${apiKey.slice(0, 8)}${"•".repeat(Math.min(20, apiKey.length - 8))}`
? `${apiKey.slice(0, 8)}${"•".repeat(Math.min(20, Math.max(0, apiKey.length - 8)))}`
: connectionCount > 0
? <span className="text-text-muted italic">Using stored key(s) · {connectionCount} connection{connectionCount > 1 ? "s" : ""}</span>
: <span className="text-text-muted italic">No key configured</span>}
@@ -0,0 +1,284 @@
"use client";
import { useState, useRef } from "react";
import { Modal, Button } from "@/shared/components";
import { translate } from "@/i18n/runtime";
const PLACEHOLDER = `[
{
"access_token": "eyJ0eXAiOiJhdCtqd3Qi...",
"refresh_token": "LZhriF9bf88pPykpXCuZ9...",
"id_token": "eyJ0eXAiOiJKV1QiLCJhbGci...",
"email": "account1@example.com"
},
{
"access_token": "eyJ0eXAiOiJhdCtqd3Qi...",
"refresh_token": "LZhriF9bf88pPykpXCuZ9...",
"id_token": "eyJ0eXAiOiJKV1QiLCJhbGci...",
"email": "account2@example.com"
}
]`;
function parseAccountsInput(rawText) {
const trimmed = rawText.trim();
if (!trimmed) return [];
let parsed;
try {
parsed = JSON.parse(trimmed);
} catch (initialErr) {
// If direct parse failed, try handling concatenated or comma-separated JSON objects
try {
let fixed = trimmed;
if (!fixed.startsWith("[")) {
fixed = fixed.replace(/\}\s*,\s*\{/g, "},{").replace(/\}\s*\{/g, "},{");
if (fixed.endsWith(",")) fixed = fixed.slice(0, -1);
fixed = `[${fixed}]`;
}
parsed = JSON.parse(fixed);
} catch {
throw initialErr;
}
}
if (Array.isArray(parsed)) {
return parsed;
}
if (parsed && typeof parsed === "object") {
if (Array.isArray(parsed.accounts)) return parsed.accounts;
return [parsed];
}
throw new Error("Input must be a JSON object or array of objects");
}
export default function BulkImportGrokCliModal({ isOpen, onClose, onSuccess }) {
const [jsonText, setJsonText] = useState("");
const [submitting, setSubmitting] = useState(false);
const [parseError, setParseError] = useState("");
const [result, setResult] = useState(null);
const [isDragging, setIsDragging] = useState(false);
const [fileCountInfo, setFileCountInfo] = useState(null);
const fileInputRef = useRef(null);
const handleClose = () => {
if (submitting) return;
setJsonText("");
setParseError("");
setResult(null);
setFileCountInfo(null);
setIsDragging(false);
onClose();
};
const processFiles = async (files) => {
if (!files || files.length === 0) return;
setParseError("");
const jsonFiles = Array.from(files).filter(
(file) => file.name.endsWith(".json") || file.type === "application/json" || file.type === ""
);
if (jsonFiles.length === 0) {
setParseError(translate("Please select valid .json files"));
return;
}
try {
const allAccounts = [];
for (const file of jsonFiles) {
const text = await file.text();
const accountsFromFile = parseAccountsInput(text);
if (Array.isArray(accountsFromFile)) {
allAccounts.push(...accountsFromFile);
} else if (accountsFromFile) {
allAccounts.push(accountsFromFile);
}
}
if (allAccounts.length === 0) {
setParseError(translate("No accounts found in selected files"));
return;
}
setJsonText(JSON.stringify(allAccounts, null, 2));
setFileCountInfo({
filesCount: jsonFiles.length,
accountsCount: allAccounts.length,
});
} catch (err) {
setParseError(`${translate("Error reading files")}: ${err.message}`);
}
};
const handleFileInputChange = (e) => {
processFiles(e.target.files);
if (e.target) e.target.value = "";
};
const handleDragOver = (e) => {
e.preventDefault();
setIsDragging(true);
};
const handleDragLeave = (e) => {
e.preventDefault();
setIsDragging(false);
};
const handleDrop = (e) => {
e.preventDefault();
setIsDragging(false);
if (e.dataTransfer?.files?.length > 0) {
processFiles(e.dataTransfer.files);
}
};
const handleSubmit = async () => {
setParseError("");
setResult(null);
let accounts;
try {
accounts = parseAccountsInput(jsonText);
} catch (err) {
setParseError(`${translate("Invalid JSON")}: ${err.message}`);
return;
}
if (!accounts || accounts.length === 0) {
setParseError(translate("No accounts found in input"));
return;
}
setSubmitting(true);
try {
const res = await fetch("/api/oauth/grok-cli/bulk-import", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ accounts }),
});
const data = await res.json();
if (!res.ok) {
setParseError(data?.error || `Request failed: ${res.status}`);
return;
}
setResult(data);
if (data.success > 0 && typeof onSuccess === "function") {
onSuccess();
}
} catch (err) {
setParseError(err.message || translate("Request failed"));
} finally {
setSubmitting(false);
}
};
const failedItems = result?.results?.filter((r) => !r.ok) || [];
return (
<Modal isOpen={isOpen} title={translate("Bulk Add Grok CLI Accounts")} onClose={handleClose}>
<div className="flex flex-col gap-4">
<div className="flex flex-wrap items-center justify-between gap-2">
<p className="text-xs text-text-muted">
{translate("Upload multiple .json files or paste JSON array / object.")}
</p>
<input
ref={fileInputRef}
type="file"
accept=".json,application/json"
multiple
className="hidden"
onChange={handleFileInputChange}
/>
<Button
type="button"
size="sm"
variant="secondary"
icon="upload_file"
onClick={() => fileInputRef.current?.click()}
disabled={submitting}
>
{translate("Upload JSON Files")}
</Button>
</div>
<div
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
className={`relative rounded border transition-colors ${
isDragging
? "border-primary bg-primary/10 ring-2 ring-primary/30"
: "border-accent/30 bg-sidebar"
}`}
>
<textarea
className="w-full rounded bg-transparent p-2.5 text-sm font-mono resize-y min-h-[240px] focus:outline-none focus:ring-1 focus:ring-primary"
placeholder={PLACEHOLDER}
value={jsonText}
onChange={(e) => {
setJsonText(e.target.value);
setFileCountInfo(null);
}}
disabled={submitting}
/>
{isDragging && (
<div className="absolute inset-0 flex flex-col items-center justify-center bg-sidebar/90 rounded pointer-events-none backdrop-blur-xs">
<span className="material-symbols-outlined text-3xl text-primary mb-1">upload_file</span>
<span className="text-sm font-medium text-primary">
{translate("Drop .json files here")}
</span>
</div>
)}
</div>
{fileCountInfo && (
<div className="flex items-center gap-1.5 text-xs text-green-400 font-medium bg-green-500/10 border border-green-500/20 px-2.5 py-1.5 rounded">
<span className="material-symbols-outlined text-sm">check_circle</span>
<span>
{translate("Loaded")} {fileCountInfo.accountsCount} {translate("account(s) from")}{" "}
{fileCountInfo.filesCount} {translate("file(s)")}
</span>
</div>
)}
{parseError && (
<p className="text-xs text-red-500 break-words">{parseError}</p>
)}
{result && result.failed > 0 && (
<div className="flex flex-col gap-2">
<div className="text-sm font-medium text-yellow-400">
✗ {result.failed} {translate("failed")}
</div>
{failedItems.length > 0 && (
<ul className="rounded border border-accent/20 bg-sidebar/50 p-2 text-xs font-mono max-h-40 overflow-y-auto">
{failedItems.map((item) => (
<li key={item.index} className="text-red-400">
[{item.index}] {item.error}
</li>
))}
</ul>
)}
</div>
)}
<div className="flex gap-2">
<Button
onClick={handleSubmit}
fullWidth
disabled={submitting || !jsonText.trim()}
>
{submitting ? translate("Importing...") : translate("Import All")}
</Button>
<Button onClick={handleClose} variant="ghost" fullWidth disabled={submitting}>
{translate("Close")}
</Button>
</div>
</div>
</Modal>
);
}
@@ -22,6 +22,7 @@ import AddApiKeyModal from "./AddApiKeyModal";
import EditCompatibleNodeModal from "./EditCompatibleNodeModal";
import AddCustomModelModal from "./AddCustomModelModal";
import BulkImportCodexModal from "./BulkImportCodexModal";
import BulkImportGrokCliModal from "./BulkImportGrokCliModal";
const ONE_BY_ONE_DELAY_MS = 1000;
@@ -48,6 +49,7 @@ export default function ProviderDetailPage() {
const [showAddApiKeyModal, setShowAddApiKeyModal] = useState(false);
const [addConnectionError, setAddConnectionError] = useState("");
const [showBulkImportCodex, setShowBulkImportCodex] = useState(false);
const [showBulkImportGrokCli, setShowBulkImportGrokCli] = useState(false);
const [showEditModal, setShowEditModal] = useState(false);
const [showEditNodeModal, setShowEditNodeModal] = useState(false);
const [showBulkProxyModal, setShowBulkProxyModal] = useState(false);
@@ -1624,6 +1626,11 @@ export default function ProviderDetailPage() {
{translate("Bulk Add")}
</Button>
)}
{providerId === "grok-cli" && (
<Button size="sm" icon="playlist_add" variant="secondary" onClick={() => setShowBulkImportGrokCli(true)}>
{translate("Bulk Add")}
</Button>
)}
<Button
size="sm"
icon="add"
@@ -1693,6 +1700,18 @@ export default function ProviderDetailPage() {
{translate("Bulk Add")}
</Button>
)}
{providerId === "grok-cli" && (
<Button
size="sm"
icon="playlist_add"
variant="secondary"
onClick={() => setShowBulkImportGrokCli(true)}
title={translate("Bulk import Grok CLI accounts from JSON")}
className="w-full sm:w-auto"
>
{translate("Bulk Add")}
</Button>
)}
{hasDualAuthModes ? (
<>
<Button
@@ -1873,6 +1892,14 @@ export default function ProviderDetailPage() {
/>
)}
{providerId === "grok-cli" && (
<BulkImportGrokCliModal
isOpen={showBulkImportGrokCli}
onClose={() => setShowBulkImportGrokCli(false)}
onSuccess={fetchConnections}
/>
)}
{/* AG Risk Confirmation Modal */}
<ConfirmModal
isOpen={showAgRiskModal}
@@ -115,9 +115,11 @@ export default function ProvidersPage() {
return () => unregisterSearch();
}, [registerSearch, unregisterSearch]);
const matchSearch = (name) =>
!searchQuery.trim() ||
name.toLowerCase().includes(searchQuery.trim().toLowerCase());
const matchSearch = (name) => {
if (!searchQuery.trim()) return true;
if (!name) return false;
return name.toLowerCase().includes(searchQuery.trim().toLowerCase());
};
const sortByPriority = (entries, authType) =>
[...entries].sort(([ka, a], [kb, b]) => {
@@ -14,6 +14,7 @@ export default function TokenSaverClient() {
const [rtkEnabled, setRtkEnabledState] = useState(true);
const [headroomEnabled, setHeadroomEnabled] = useState(false);
const [headroomUrl, setHeadroomUrl] = useState("http://localhost:8787");
const [headroomTimeoutMs, setHeadroomTimeoutMs] = useState(3000);
const [headroomStatus, setHeadroomStatus] = useState({
installed: false,
running: false,
@@ -406,6 +407,13 @@ export default function TokenSaverClient() {
patchSetting({ pxpipeMinChars: next });
};
const handleHeadroomTimeoutBlur = () => {
const raw = Math.round(Number(headroomTimeoutMs));
const next = Number.isFinite(raw) && raw > 0 ? raw : 3000;
setHeadroomTimeoutMs(next);
patchSetting({ headroomTimeoutMs: next });
};
useEffect(() => {
const loadSettings = async () => {
try {
@@ -415,6 +423,7 @@ export default function TokenSaverClient() {
setRtkEnabledState(data.rtkEnabled !== false);
setHeadroomEnabled(!!data.headroomEnabled);
setHeadroomUrl(data.headroomUrl || "http://localhost:8787");
if (typeof data.headroomTimeoutMs === "number") setHeadroomTimeoutMs(data.headroomTimeoutMs);
setCodeAware(data.headroomCodeAware === true);
setKompress(data.headroomKompress !== false);
setCavemanEnabled(!!data.cavemanEnabled);
@@ -818,6 +827,19 @@ export default function TokenSaverClient() {
like http://headroom:8787.
</p>
</div>
<div className="flex flex-col gap-1">
<p className="text-sm font-medium">Timeout (ms)</p>
<Input
value={String(headroomTimeoutMs)}
onChange={(e) => setHeadroomTimeoutMs(e.target.value)}
onBlur={handleHeadroomTimeoutBlur}
placeholder="3000"
className="font-mono text-sm"
/>
<p className="text-xs text-text-muted">
Request timeout in milliseconds. Defaults to 3000 ms.
</p>
</div>
{headroomManaged ? (
<Button
onClick={handleHeadroomStop}
@@ -150,6 +150,7 @@ export default function QuotaTable({
<div className="space-y-px">
{currentPageRows.map((quota) => {
const isUnlimited = quota.unlimited === true;
const colors = getColorClasses(quota.remaining);
const countdown = formatResetTime(quota.resetAt);
const resetDisplay = formatResetTimeDisplay(quota.resetAt);
@@ -174,6 +175,7 @@ export default function QuotaTable({
{/* Progress + used/total */}
<div className={`min-w-0 flex-1 ${compact ? "space-y-1" : "space-y-1.5"}`}>
{!isUnlimited && (
<div className={`${compact ? "h-1" : "h-1.5"} rounded-full overflow-hidden border ${colors.bgLight} ${
quota.remaining === 0 ? "border-black/10 dark:border-white/10" : "border-transparent"
}`}>
@@ -182,16 +184,23 @@ export default function QuotaTable({
style={{ width: `${Math.min(quota.remaining, 100)}%` }}
/>
</div>
)}
<div className={`flex items-center justify-between gap-1 min-w-0 ${compact ? "text-[10px]" : "text-xs"}`}>
<span
className="text-text-muted truncate"
title={`${quota.used.toLocaleString()} / ${quota.total > 0 ? quota.total.toLocaleString() : "∞"}`}
title={
isUnlimited
? `${quota.used.toLocaleString()} used · Unlimited`
: `${quota.used.toLocaleString()} / ${quota.total > 0 ? quota.total.toLocaleString() : "∞"}`
}
>
{quota.used.toLocaleString()} / {quota.total > 0 ? quota.total.toLocaleString() : "∞"}
{isUnlimited
? `${quota.used.toLocaleString()} used · Unlimited`
: `${quota.used.toLocaleString()} / ${quota.total > 0 ? quota.total.toLocaleString() : "∞"}`}
</span>
<span className={`font-medium ${colors.text} shrink-0`}>
{quota.remaining}%
<span className={`font-medium ${isUnlimited ? "text-green-600 dark:text-green-400" : colors.text} shrink-0`}>
{isUnlimited ? "Unlimited" : `${quota.remaining}%`}
</span>
</div>
</div>
@@ -1265,6 +1265,11 @@ export default function ProviderLimits() {
onHideQuota={(quotaRow) => handleHideQuota(conn.provider, quotaRow)}
/>
)}
{quota?.message && !error && !isLoading && (
<p className="mt-2 px-1 text-[10px] leading-relaxed text-text-muted">
{quota.message}
</p>
)}
{hiddenQuotaRows.length > 0 && (
<div className="mt-2 flex min-w-0 items-center gap-1 border-t border-black/5 pt-2 text-[10px] text-text-muted dark:border-white/5">
<span className="material-symbols-outlined shrink-0 text-[14px]">
@@ -379,8 +379,17 @@ export function parseQuotaData(provider, data) {
case "codex":
if (data.quotas) {
Object.entries(data.quotas).forEach(([quotaType, quota]) => {
let displayName = quotaType;
if (quotaType === "spark_session") displayName = "Spark (5h)";
else if (quotaType === "spark_weekly") displayName = "Spark (Weekly)";
else if (quotaType === "session") displayName = "5h";
else if (quotaType === "weekly") displayName = "Weekly";
else if (quotaType === "review_session") displayName = "Review (5h)";
else if (quotaType === "review_weekly") displayName = "Review (Weekly)";
normalizedQuotas.push({
name: quotaType,
name: displayName,
quotaType,
used: quota.used || 0,
total: quota.total || 0,
remaining: quota.remaining,
@@ -566,6 +575,22 @@ export function parseQuotaData(provider, data) {
}
break;
case "zed":
// Edit predictions + optional hosted model_requests; unlimited uses remainingPercentage.
if (data.quotas) {
Object.entries(data.quotas).forEach(([name, quota]) => {
normalizedQuotas.push({
name,
used: quota.used || 0,
total: quota.total || 0,
resetAt: quota.resetAt || null,
remainingPercentage: quota.remainingPercentage,
unlimited: quota.unlimited,
});
});
}
break;
default:
// Generic fallback for unknown providers
if (data.quotas) {
+7 -19
View File
@@ -135,35 +135,22 @@ export async function POST(request) {
// Update or create 9router provider section (no api_key - Codex reads from auth.json)
// Ensure /v1 suffix is added only once
const normalizedBaseUrl = baseUrl.endsWith("/v1") ? baseUrl : `${baseUrl}/v1`;
// Custom providers ignore auth.json - the key must travel as a static header
setNestedSection(parsed, "model_providers.9router", {
name: "9Router",
base_url: normalizedBaseUrl,
wire_api: "responses",
http_headers: { Authorization: `Bearer ${apiKey}` },
});
// Add subagent configuration
const effectiveSubagentModel = subagentModel || model;
setNestedSection(parsed, "agents.subagent", {
model: effectiveSubagentModel,
});
// Subagent model is a scalar under [agents]; agents.<role> now means a custom role
deleteNestedSection(parsed, "agents.subagent");
setNestedSection(parsed, "agents.default_subagent_model", subagentModel || model);
// Write merged config
const configContent = stringifyTOML(parsed);
await fs.writeFile(configPath, configContent);
// Update auth.json with OPENAI_API_KEY (Codex reads this first)
const authPath = getCodexAuthPath();
let authData = {};
try {
const existingAuth = await fs.readFile(authPath, "utf-8");
authData = JSON.parse(existingAuth);
} catch { /* No existing auth */ }
// Force apikey mode (keep existing tokens untouched for ChatGPT login reuse)
authData.OPENAI_API_KEY = apiKey;
authData.auth_mode = "apikey";
await fs.writeFile(authPath, JSON.stringify(authData, null, 2));
return NextResponse.json({
success: true,
message: "Codex settings applied successfully!",
@@ -204,7 +191,8 @@ export async function DELETE() {
// Remove 9router provider section
deleteNestedSection(parsed, "model_providers.9router");
// Remove subagent configuration
// Remove subagent configuration (both the current key and the legacy role form)
deleteNestedSection(parsed, "agents.default_subagent_model");
deleteNestedSection(parsed, "agents.subagent");
// Write updated config
+31
View File
@@ -0,0 +1,31 @@
import { NextResponse } from "next/server";
import fs from "node:fs";
import { getSyncState, syncModelCatalog } from "@/lib/modelCatalog/sync.js";
import { CATALOG_FILE } from "open-sse/providers/catalogOverride.js";
// GET /api/models/catalog-sync - Sync status and what the catalog currently holds
export async function GET() {
const state = getSyncState();
let catalog = null;
try {
const parsed = JSON.parse(fs.readFileSync(CATALOG_FILE, "utf8"));
catalog = {
syncedAt: parsed.syncedAt,
models: Object.keys(parsed.models || {}).length,
providers: Object.keys(parsed.providers || {}).length,
bytes: fs.statSync(CATALOG_FILE).size,
};
} catch {
catalog = null;
}
return NextResponse.json({ ...state, catalog });
}
// POST /api/models/catalog-sync - Run a sync now instead of waiting for the timer
export async function POST() {
const result = await syncModelCatalog();
if (!result) {
return NextResponse.json({ error: getSyncState().lastError || "sync in progress" }, { status: 503 });
}
return NextResponse.json({ success: true, result });
}
@@ -0,0 +1,116 @@
import { NextResponse } from "next/server";
import { createProviderConnection } from "@/models";
import { decodeXaiIdTokenEmail, extractEmailFromAccessToken } from "@/lib/oauth/providerHelpers";
/**
* POST /api/oauth/grok-cli/bulk-import
* Bulk import multiple Grok CLI (OAuth/Device) account JSON objects in one call.
*
* Body accepts any of:
* - Array: [{...}, {...}]
* - Single: {...}
* - Wrapped: { accounts: [{...}, ...] }
*
* Each item accepts snake_case or camelCase:
* access_token / accessToken
* refresh_token / refreshToken
* id_token / idToken
* email
* expires_in / expiresIn / expires_at / expiresAt
*/
export async function POST(request) {
let body;
try {
body = await request.json();
} catch (err) {
return NextResponse.json(
{ error: `Invalid JSON body: ${err.message}` },
{ status: 400 }
);
}
let accounts;
if (Array.isArray(body)) {
accounts = body;
} else if (body && typeof body === "object" && Array.isArray(body.accounts)) {
accounts = body.accounts;
} else if (body && typeof body === "object") {
accounts = [body];
} else {
accounts = null;
}
if (!Array.isArray(accounts) || accounts.length === 0) {
return NextResponse.json(
{ error: "No accounts provided" },
{ status: 400 }
);
}
const results = [];
let success = 0;
let failed = 0;
for (let i = 0; i < accounts.length; i++) {
const raw = accounts[i];
try {
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
throw new Error("Item is not an object");
}
const accessToken = raw.access_token || raw.accessToken;
const refreshToken = raw.refresh_token || raw.refreshToken || null;
const idToken = raw.id_token || raw.idToken || null;
let email = raw.email || null;
if (!accessToken || typeof accessToken !== "string") {
throw new Error("Missing access_token / accessToken");
}
if (!email) {
email =
decodeXaiIdTokenEmail(idToken) ||
extractEmailFromAccessToken(accessToken) ||
null;
}
let expiresAt = raw.expires_at || raw.expiresAt || null;
const expiresIn = raw.expires_in || raw.expiresIn;
if (!expiresAt && typeof expiresIn === "number" && expiresIn > 0) {
expiresAt = new Date(Date.now() + expiresIn * 1000).toISOString();
}
const psd = {
authMethod: "device_code",
...(idToken ? { idToken } : {}),
...(email ? { email } : {}),
...(raw.providerSpecificData || {}),
};
const created = await createProviderConnection({
provider: "grok-cli",
authType: "oauth",
accessToken,
refreshToken,
expiresAt,
email,
displayName: raw.displayName || raw.name || undefined,
providerSpecificData: psd,
testStatus: "active",
});
success++;
results.push({ index: i, ok: true, id: created.id, email: created.email });
} catch (err) {
failed++;
results.push({ index: i, ok: false, error: err.message });
}
}
return NextResponse.json({
total: accounts.length,
success,
failed,
results,
});
}
@@ -460,6 +460,12 @@ async function testOAuthConnection(connection, effectiveProxy = null) {
}
async function fetchWithConnectionProxy(url, options = {}, effectiveProxy = null) {
// Add a 15-second timeout to prevent connection testing from hanging indefinitely
// and exhausting the browser/Node.js connection pools.
if (!options.signal) {
options.signal = AbortSignal.timeout(15000);
}
// Vercel relay: forward via relay URL
if (effectiveProxy?.vercelRelayUrl) {
const { proxyAwareFetch } = await import("open-sse/utils/proxyFetch.js");
+1 -1
View File
@@ -20,7 +20,7 @@ async function probeWebProvider(provider, apiKey) {
if (!cfg) return null;
if (cfg.authType === "none") return true; // no-auth (e.g. searxng)
let url = cfg.baseUrl;
let url = cfg.validateUrl || cfg.baseUrl;
const headers = { "Content-Type": "application/json" };
let body;
+2 -2
View File
@@ -4,8 +4,8 @@
@custom-variant dark (&:where(.dark, .dark *));
/* Hide icon ligature text until font is ready */
.material-symbols-outlined { visibility: hidden; }
.fonts-loaded .material-symbols-outlined { visibility: visible; }
.material-symbols-outlined { opacity: 0; }
.fonts-loaded .material-symbols-outlined { opacity: 1; transition: opacity .12s ease-out; }
/* ============================================================
9Router palette — adopted from 9remote_private/web
+1 -1
View File
@@ -34,7 +34,7 @@ export default function RootLayout({ children }) {
<head>
<script
dangerouslySetInnerHTML={{
__html: `if(document.fonts&&document.fonts.ready){document.fonts.ready.then(function(){document.documentElement.classList.add('fonts-loaded')})}else{document.documentElement.classList.add('fonts-loaded')}`,
__html: `var d=document,r=d.documentElement,f=function(){r.classList.add('fonts-loaded')};if(d.fonts&&d.fonts.load){d.fonts.load('24px "Material Symbols Outlined"').then(f).catch(f);setTimeout(f,3000)}else{f()}`,
}}
/>
</head>
+8
View File
@@ -2,5 +2,13 @@ export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
const { initConsoleLogCapture } = await import("@/lib/consoleLogBuffer");
initConsoleLogCapture();
// Server-only: lets capabilities.js read the synced catalog without pulling
// node:fs into the dashboard's browser bundle.
const { installCatalogSource } = await import("open-sse/providers/catalogOverride.js");
await installCatalogSource();
const { startModelCatalogSync } = await import("@/lib/modelCatalog/sync.js");
startModelCatalogSync();
}
}
+1
View File
@@ -53,6 +53,7 @@ const DEFAULT_SETTINGS = {
headroomEnabled: false,
headroomUrl: DEFAULT_HEADROOM_URL,
headroomCompressUserMessages: false,
headroomTimeoutMs: 3000,
cavemanEnabled: false,
cavemanLevel: "full",
ponytailEnabled: false,
+247
View File
@@ -0,0 +1,247 @@
// Daily refresh of model capabilities from models.dev.
//
// Downloads the catalog, keeps only what differs from the hand-written tables,
// and writes it next to the database. Failures are swallowed on purpose: a
// stale or missing file just means those tables keep deciding on their own.
import fs from "node:fs";
import path from "node:path";
import { CATALOG_FILE, CATALOG_RAW_FILE, invalidateCatalog, installCatalogSource } from "open-sse/providers/catalogOverride.js";
const CATALOG_URL = "https://models.dev/api.json";
const FETCH_TIMEOUT_MS = 60000;
export const SYNC_INTERVAL_MS = 24 * 60 * 60 * 1000;
const STARTUP_DELAY_MS = 60 * 1000; // let the server boot and serve first requests
const RETRY_DELAY_MS = 30 * 60 * 1000;
const MODALITY_BY_INPUT = { image: "vision", pdf: "pdf", audio: "audioInput", video: "videoInput" };
// Gateways disagree about the same model, so a modality needs a majority of
// them to declare it — one reseller mislabelling a text model must not win.
const MIN_SHARE = 0.5;
// Ignore limit differences below this: gateways round 200000 vs 202752.
const LIMIT_TOLERANCE = 0.1;
// 9router provider id -> models.dev provider id, for context/maxOutput only.
// Providers absent here keep whatever the local pattern table resolves; names
// that already match are resolved automatically.
const PROVIDER_ALIASES = {
"glm": "zai",
"glm-cn": "zhipuai",
"claude": "anthropic",
"gemini": "google",
"kimi": "moonshotai",
"kimi-cn": "moonshotai-cn",
"qwen": "alibaba",
"qwen-cn": "alibaba-cn",
"zhipu": "zhipuai",
"hunyuan": "tencent",
"doubao": "volcengine",
"cloudflare-ai": "cloudflare-workers-ai",
};
let state = { running: false, lastSync: null, lastError: null, lastResult: null, etag: null };
let timer = null;
export function getSyncState() {
return { ...state, file: CATALOG_FILE, url: CATALOG_URL, intervalMs: SYNC_INTERVAL_MS };
}
// "zai-org/GLM-4.6V:free" -> "glm-4.6v"
function baseId(modelId) {
const withoutVendor = modelId.includes("/") ? modelId.split("/").pop() : modelId;
return withoutVendor.toLowerCase().split(":")[0];
}
function writeAtomic(file, contents) {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(`${file}.tmp`, contents, "utf8");
fs.renameSync(`${file}.tmp`, file);
}
// Trimmed copy of the upstream catalog, kept for the add-models skill: same
// models, ~470KB instead of 4.3MB.
function slim(catalog) {
const out = {};
for (const [providerId, provider] of Object.entries(catalog)) {
const models = {};
for (const [modelId, model] of Object.entries(provider?.models || {})) {
models[modelId] = {
i: (model?.modalities?.input || []).filter((x) => x !== "text"),
c: model?.limit?.context,
o: model?.limit?.output,
r: model?.reasoning || undefined,
};
}
out[providerId] = models;
}
return out;
}
function build(catalog, entries) {
// Index once: per provider for limits, and tallied across all of them for
// modalities.
const byProvider = {};
const tally = {};
for (const [providerId, provider] of Object.entries(catalog)) {
const models = {};
const counted = new Set();
for (const [modelId, model] of Object.entries(provider?.models || {})) {
const id = baseId(modelId);
models[id] = model;
// One vote per provider: several ids can normalize to the same model
// (claude-opus-4-thinking:1024, :8192, :32768 …) and must not stack.
if (counted.has(id)) continue;
counted.add(id);
const counts = tally[id] || (tally[id] = { total: 0 });
counts.total++;
for (const input of model?.modalities?.input || []) {
const key = MODALITY_BY_INPUT[input];
if (key) counts[key] = (counts[key] || 0) + 1;
}
}
byProvider[providerId] = models;
}
// Modalities belong to the model — every gateway serving it has the same
// weights — so they are keyed by model id and shared across providers.
const models = {};
for (const [id, counts] of Object.entries(tally)) {
const declared = {};
for (const key of Object.values(MODALITY_BY_INPUT)) {
if ((counts[key] || 0) / counts.total >= MIN_SHARE) declared[key] = true;
}
if (Object.keys(declared).length) models[id] = declared;
}
// Limits belong to the gateway — each truncates differently — so only the
// matching provider's own numbers are used, keyed by provider + model.
const providers = {};
for (const { provider, model, contextLength, current } of entries) {
const alias = PROVIDER_ALIASES[provider];
const upstream = catalog[provider] ? provider : (alias && catalog[alias] ? alias : null);
const entry = upstream && byProvider[upstream]?.[baseId(model)];
if (!entry) continue;
const delta = {};
const { context, output } = entry.limit || {};
if (context > 0 && !contextLength
&& Math.abs(context - current.contextWindow) / current.contextWindow > LIMIT_TOLERANCE) {
delta.contextWindow = context;
}
if (output > 0
&& Math.abs(output - current.maxOutput) / current.maxOutput > LIMIT_TOLERANCE) {
delta.maxOutput = output;
}
if (Object.keys(delta).length) (providers[provider] || (providers[provider] = {}))[model] = delta;
}
return { models, providers };
}
// Snapshot every registered model with the capabilities the hand-written tables
// resolve on their own, so build() can tell which upstream values are a change.
//
// The previous catalog MUST be detached first. Leaving it installed makes each
// delta relative to the last one, so a value that still agrees with upstream
// looks like "no change" and is dropped — the file erases itself over two runs.
async function collectEntries() {
const [{ default: registry }, { getCapabilitiesForModel, setCatalogSource }] = await Promise.all([
import("open-sse/providers/registry/index.js"),
import("open-sse/providers/capabilities.js"),
]);
setCatalogSource(null);
const entries = [];
for (const provider of registry) {
for (const model of provider.models || []) {
entries.push({
provider: provider.id,
model: model.id,
contextLength: model.contextLength,
current: getCapabilitiesForModel(provider.id, model.id),
});
}
}
return entries;
}
// Run one sync. Returns a summary, or null when it could not complete.
export async function syncModelCatalog() {
if (state.running) return null;
state.running = true;
try {
const headers = { accept: "application/json" };
if (state.etag) headers["if-none-match"] = state.etag;
const response = await fetch(CATALOG_URL, { headers, signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) });
let result;
if (response.status === 304) {
result = { status: "unchanged" };
} else if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
} else {
// ~23ms to parse, once a day, on a server that is otherwise idle at this
// point — not worth a worker thread.
const catalog = await response.json();
const etag = response.headers.get("etag") || null;
const entries = await collectEntries();
const { models, providers } = build(catalog, entries);
const serialized = JSON.stringify({ v: 1, etag, syncedAt: Date.now(), models, providers });
writeAtomic(CATALOG_FILE, serialized);
writeAtomic(CATALOG_RAW_FILE, JSON.stringify(slim(catalog)));
state.etag = etag;
invalidateCatalog();
result = {
status: "updated",
etag,
bytes: Buffer.byteLength(serialized),
models: Object.keys(models).length,
providers: Object.keys(providers).length,
};
console.log(`[modelCatalog] ${result.models} models, ${result.providers} providers, ${(result.bytes / 1024).toFixed(1)}KB`);
}
state.lastSync = Date.now();
state.lastError = null;
state.lastResult = result;
return result;
} catch (error) {
state.lastError = error?.message || String(error);
console.log(`[modelCatalog] sync failed: ${state.lastError}`);
return null;
} finally {
// collectEntries() detaches the reader; put it back whatever happened.
await installCatalogSource().catch(() => {});
state.running = false;
}
}
// The etag lives in the file we wrote, so a restart can resume from it instead
// of re-downloading 4.3MB to be told nothing changed.
function restoreEtag() {
try {
state.etag = JSON.parse(fs.readFileSync(CATALOG_FILE, "utf8")).etag || null;
state.lastSync = fs.statSync(CATALOG_FILE).mtimeMs;
} catch {
state.etag = null;
}
}
// Schedule the recurring sync. Disable entirely with MODEL_CATALOG_SYNC=off.
export function startModelCatalogSync() {
if (timer) return;
if (String(process.env.MODEL_CATALOG_SYNC || "").toLowerCase() === "off") return;
restoreEtag();
const schedule = (delay) => {
timer = setTimeout(async () => {
const result = await syncModelCatalog();
schedule(result ? SYNC_INTERVAL_MS : RETRY_DELAY_MS);
}, delay);
timer.unref?.();
};
schedule(STARTUP_DELAY_MS);
}
+3
View File
@@ -10,6 +10,9 @@ export const MITM_TOOLS = {
mitmDomain: "daily-cloudcode-pa.googleapis.com",
modelAliases: ["gemini-3.7-flash-high", "gemini-3.7-flash-medium", "gemini-3.7-flash-low", "gemini-3.6-flash-high", "gemini-3.6-flash-medium", "gemini-3.6-flash-low", "gemini-3.5-flash-low", "gemini-3-flash-agent", "gemini-3.5-flash-extra-low", "gemini-3.1-pro-low", "gemini-pro-agent", "claude-sonnet-4-6", "claude-opus-4-6-thinking", "gpt-oss-120b-medium", "gemini-3-flash"],
defaultModels: [
{ id: "gemini-3.7-flash-high", name: "Gemini 3.7 Flash (High)", alias: "gemini-3.7-flash-high" },
{ id: "gemini-3.7-flash-medium", name: "Gemini 3.7 Flash (Medium)", alias: "gemini-3.7-flash-medium" },
{ id: "gemini-3.7-flash-low", name: "Gemini 3.7 Flash (Low)", alias: "gemini-3.7-flash-low" },
{ id: "gemini-3.6-flash-high", name: "Gemini 3.6 Flash (High)", alias: "gemini-3.6-flash-high" },
{ id: "gemini-3.6-flash-medium", name: "Gemini 3.6 Flash (Medium)", alias: "gemini-3.6-flash-medium" },
{ id: "gemini-3.6-flash-low", name: "Gemini 3.6 Flash (Low)", alias: "gemini-3.6-flash-low" },
+1 -1
View File
@@ -5,7 +5,7 @@ import { RISK_NOTICE } from "@/shared/constants/providersDisplay";
const MEDIA_ENTRY_KEYS = [
"serviceKinds", "ttsConfig", "sttConfig", "embeddingConfig",
"imageConfig", "imageToTextConfig", "videoConfig", "musicConfig",
"searchViaChat", "searchConfig", "fetchConfig",
"searchViaChat", "searchConfig", "fetchConfig", "credentialFallback",
"modelsFetcher", "mediaPriority", "hiddenKinds",
];
+1 -1
View File
@@ -56,7 +56,7 @@ export const SKILLS = [
{
id: "9router-web-search",
name: "Web Search",
description: "Tavily / Exa / Brave / Serper / SearXNG / Google PSE / You.com.",
description: "Web and X search via Tavily / Exa / Brave / Serper / SearXNG / Google PSE / You.com / Xquik.",
endpoint: "/v1/search",
icon: "search",
},
+1
View File
@@ -5,6 +5,7 @@ const ICON_ALIASES = {
"perplexity-agent": "perplexity",
"gitlab-duo": "gitlab",
"vercel-ai-gateway": "vercel",
"ollama-search": "ollama",
};
// Runtime only — first 404 remembers id for the whole session
+18 -2
View File
@@ -7,6 +7,7 @@ import {
extractApiKey,
isValidApiKey,
} from "../services/auth.js";
import { handleAntigravityQuotaError } from "../services/antigravityQuota.js";
import { getSettings } from "@/lib/localDb";
import { getModelInfo, getComboModels } from "../services/model.js";
import { handleChatCore } from "open-sse/handlers/chatCore.js";
@@ -274,6 +275,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
headroomEnabled: !!chatSettings.headroomEnabled,
headroomUrl: chatSettings.headroomUrl || DEFAULT_HEADROOM_URL,
headroomCompressUserMessages: !!chatSettings.headroomCompressUserMessages,
headroomTimeoutMs: chatSettings.headroomTimeoutMs,
cavemanEnabled: !!chatSettings.cavemanEnabled,
cavemanLevel: chatSettings.cavemanLevel || "full",
ponytailEnabled: !!chatSettings.ponytailEnabled,
@@ -318,8 +320,22 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
if (result.success) return result.response;
// Mark account unavailable (auto-calculates cooldown with exponential backoff, or precise resetsAtMs)
const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, provider, model, result.resetsAtMs);
// Antigravity 409/429: refresh live quota to get exact resetAt before locking
let quotaResetMs = null;
let resetsAtMs = result.resetsAtMs;
if (provider === "antigravity" && (result.status === 409 || result.status === 429)) {
quotaResetMs = await handleAntigravityQuotaError(
credentials.connectionId, result.status, model,
refreshedCredentials.accessToken, credentials.providerSpecificData
);
if (quotaResetMs) resetsAtMs = quotaResetMs;
}
// Exhausted Antigravity model is blocked only in RAM cache until upstream resetAt.
// Do not persist a modelLock_* for this path.
const shouldFallback = provider === "antigravity" && quotaResetMs
? true
: (await markAccountUnavailable(credentials.connectionId, result.status, result.error, provider, model, resetsAtMs)).shouldFallback;
if (shouldFallback) {
log.warn("FALLBACK", `⇄ ACC:${credentials.connectionName} UNAVAILABLE (${result.status}) → NEXT ACCOUNT`);
+27 -2
View File
@@ -148,8 +148,33 @@ async function handleSingleProviderSearch(body, providerInput, request, apiKey,
let lastError = null;
let lastStatus = null;
// Credential fallback: some search providers reuse the API key of a related
// chat provider (e.g. ollama-search reuses the `ollama` chat key, zai-search
// reuses the `glm` chat key). When the search provider has no own connection,
// fall back to the linked provider's credentials.
const fallbackProviderId = resolvedProvider.credentialFallback;
// Lock scope for this handler. Without it markAccountUnavailable would write
// an account-wide `__all` lock, which on the credentialFallback path takes
// the shared chat key (e.g. glm) offline for chat as well. Must be passed to
// getProviderCredentials too, so the lock is read back under the same key.
const searchLockKey = `websearch:${providerId}`;
while (true) {
const credentials = await getProviderCredentials(providerId, excludeConnectionIds);
// Provider that actually owns the connection in use — differs from
// providerId once we fall back, and error locks must be attributed to it.
let credentialProviderId = providerId;
let credentials = await getProviderCredentials(providerId, excludeConnectionIds, searchLockKey);
// Fall back to the related chat provider's credentials when this search
// provider has none of its own (one key, chat + search).
if (!credentials && fallbackProviderId) {
credentials = await getProviderCredentials(fallbackProviderId, excludeConnectionIds, searchLockKey);
if (credentials) {
credentialProviderId = fallbackProviderId;
log.info("AUTH", `\x1b[32m${providerId} reusing ${fallbackProviderId} credentials\x1b[0m`);
}
}
if (!credentials || credentials.allRateLimited) {
if (credentials?.allRateLimited) {
@@ -191,7 +216,7 @@ async function handleSingleProviderSearch(body, providerInput, request, apiKey,
if (result.success) return result.response;
const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, providerId);
const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, credentialProviderId, searchLockKey);
if (shouldFallback) {
log.warn("AUTH", `Account ${credentials.connectionName} unavailable (${result.status}), trying fallback`);
+99
View File
@@ -0,0 +1,99 @@
/**
* Antigravity live quota cache — in-memory, refreshed on demand.
* Used by auth.js pre-filter to skip accounts with exhausted model quota.
* Also triggered by 409/429 error handler to sync exact resetAt from upstream.
*/
import { resolveConnectionProxyConfig } from "@/lib/network/connectionProxy";
import { getAntigravityUsage } from "open-sse/services/usage/google.js";
import * as log from "../utils/logger.js";
// In-memory cache: connectionId → { [modelId]: { remainingPercentage, resetAt } }
const quotaCache = new Map();
// Track last refresh per connection to avoid hammering
const lastRefreshAt = new Map();
// In-flight refresh promises — dedup concurrent 409/429 bursts
const inflightRefresh = new Map();
const MIN_REFRESH_INTERVAL_MS = 30_000; // 30s between refreshes per connection
/**
* Get the quota cache (read-only reference for auth.js pre-filter).
*/
export function getAntigravityQuotaCache() {
return quotaCache;
}
/**
* Refresh quota for a single antigravity connection from upstream API.
* Updates in-memory cache only. Cache expiry is the upstream model resetAt.
* @returns {object|null} quotas map or null on failure
*/
export async function refreshAntigravityQuota(connectionId, accessToken, providerSpecificData) {
const now = Date.now();
// Coalesce concurrent refreshes before applying the interval gate.
const inflight = inflightRefresh.get(connectionId);
if (inflight) return inflight;
const lastRefresh = lastRefreshAt.get(connectionId) || 0;
if (now - lastRefresh < MIN_REFRESH_INTERVAL_MS) {
log.debug("AG_QUOTA", `${connectionId.slice(0, 8)} | skip refresh (${Math.round((now - lastRefresh) / 1000)}s ago)`);
return quotaCache.get(connectionId) || null;
}
// Record every attempt so failed quota calls cannot amplify an upstream 429 burst.
lastRefreshAt.set(connectionId, now);
const promise = _doRefresh(connectionId, accessToken, providerSpecificData, now);
inflightRefresh.set(connectionId, promise);
try {
return await promise;
} finally {
inflightRefresh.delete(connectionId);
}
}
async function _doRefresh(connectionId, accessToken, providerSpecificData, now) {
try {
const proxyCfg = await resolveConnectionProxyConfig(providerSpecificData || {});
const proxyOptions = {
connectionProxyEnabled: proxyCfg.connectionProxyEnabled === true,
connectionProxyUrl: proxyCfg.connectionProxyUrl || "",
connectionNoProxy: proxyCfg.connectionNoProxy || "",
vercelRelayUrl: proxyCfg.vercelRelayUrl || "",
strictProxy: proxyCfg.strictProxy === true,
};
const usage = await getAntigravityUsage(accessToken, providerSpecificData, proxyOptions);
// 401/403 usage responses can contain an empty quotas object plus message.
// Preserve known cache instead of replacing it with an upstream error response.
if (!usage?.quotas || usage.message) return null;
// Update in-memory cache. Caller logs CACHE_BLOCK only if requested model is exhausted.
quotaCache.set(connectionId, usage.quotas);
return usage.quotas;
} catch (e) {
log.warn("AG_QUOTA", `${connectionId.slice(0, 8)} | refresh failed: ${e.message}`);
return null;
}
}
/**
* Handle Antigravity 409/429 — refresh RAM cache and return model resetAt when exhausted.
* Called from chat handler error path.
* @returns {number|null} resetAt timestamp ms (for resetsAtMs passthrough) or null
*/
export async function handleAntigravityQuotaError(connectionId, status, model, accessToken, providerSpecificData) {
log.info("AG_QUOTA", `${connectionId.slice(0, 8)} | ${status} on ${model} — refreshing quota`);
// Throttle applies to error paths too: one quota request per account/30s.
// The first 409/429 populates cache; concurrent or repeated errors reuse it.
const quota = (await refreshAntigravityQuota(connectionId, accessToken, providerSpecificData))?.[model];
if (!quota || quota.remainingPercentage > 0 || !quota.resetAt) return null;
const resetMs = new Date(quota.resetAt).getTime();
if (resetMs <= Date.now()) return null;
log.warn("AG_QUOTA", `${connectionId.slice(0, 8)} | UPSTREAM_${status} ${model} — quota exhausted; CACHE_BLOCK until ${quota.resetAt}`);
return resetMs;
}
+26 -3
View File
@@ -3,6 +3,7 @@ import { resolveConnectionProxyConfig, pickProxyPoolId } from "@/lib/network/con
import { formatRetryAfter, checkFallbackError, isModelLockActive, buildModelLockUpdate, getEarliestModelLockUntil } from "open-sse/services/accountFallback.js";
import { MAX_RATE_LIMIT_COOLDOWN_MS } from "open-sse/config/errorConfig.js";
import { resolveProviderId, FREE_PROVIDERS } from "@/shared/constants/providers.js";
import { getAntigravityQuotaCache } from "./antigravityQuota.js";
import * as log from "../utils/logger.js";
// Mutex to prevent race conditions during account selection
@@ -104,10 +105,23 @@ export async function getProviderCredentials(provider, excludeConnectionIds = nu
return null;
}
// Filter out model-locked and excluded connections
// Antigravity quota cache is lazy: only populated after that account returns 409/429.
const isAntigravity = providerId === "antigravity";
const antigravityQuotaCache = isAntigravity && model ? getAntigravityQuotaCache() : null;
// Filter out model-locked, excluded, and Antigravity quota-exhausted connections.
const availableConnections = connections.filter(c => {
if (excludeSet.has(c.id)) return false;
if (isModelLockActive(c, model)) return false;
// Antigravity: skip if live quota exhausted for this model
if (isAntigravity && model && antigravityQuotaCache) {
const quota = antigravityQuotaCache.get(c.id)?.[model];
if (quota && quota.remainingPercentage <= 0 && quota.resetAt && new Date(quota.resetAt).getTime() > Date.now()) {
const account = c.id?.slice(0, 8) || "unknown";
log.info("AG_QUOTA", `${account} | CACHE_BLOCK ${model} — skip upstream until ${quota.resetAt}`);
return false;
}
}
return true;
});
@@ -122,9 +136,15 @@ export async function getProviderCredentials(provider, excludeConnectionIds = nu
});
if (availableConnections.length === 0) {
// Find earliest lock expiry across all connections for retry timing
// Find earliest persistent lock or lazy Antigravity quota-cache reset for retry timing.
const lockedConns = connections.filter(c => isModelLockActive(c, model));
const expiries = lockedConns.map(c => getEarliestModelLockUntil(c)).filter(Boolean);
if (isAntigravity && model && antigravityQuotaCache) {
connections.forEach((c) => {
const resetAt = antigravityQuotaCache.get(c.id)?.[model]?.resetAt;
if (resetAt && new Date(resetAt).getTime() > Date.now()) expiries.push(resetAt);
});
}
const earliest = expiries.sort()[0] || null;
if (earliest) {
const earliestConn = lockedConns[0];
@@ -266,7 +286,10 @@ export async function markAccountUnavailable(connectionId, status, errorText, pr
newBackoffLevel = 0;
} else if (resetsAtMs && resetsAtMs > Date.now()) {
shouldFallback = true;
cooldownMs = Math.min(resetsAtMs - Date.now(), MAX_RATE_LIMIT_COOLDOWN_MS);
// Antigravity quota API provides exact per-model resetAt. Do not truncate it.
cooldownMs = resolveProviderId(provider) === "antigravity"
? resetsAtMs - Date.now()
: Math.min(resetsAtMs - Date.now(), MAX_RATE_LIMIT_COOLDOWN_MS);
newBackoffLevel = 0;
} else {
({ shouldFallback, cooldownMs, newBackoffLevel } = checkFallbackError(status, errorText, backoffLevel));