mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
Merge pull request #65 from ramhaidar/fix/provider-connection-ux
fix(api-key): auto-validate on save to improve UX
This commit is contained in:
@@ -66,8 +66,8 @@ export default function ProviderDetailPage() {
|
|||||||
const fetchConnections = useCallback(async () => {
|
const fetchConnections = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const [connectionsRes, nodesRes] = await Promise.all([
|
const [connectionsRes, nodesRes] = await Promise.all([
|
||||||
fetch("/api/providers"),
|
fetch("/api/providers", { cache: "no-store" }),
|
||||||
fetch("/api/provider-nodes"),
|
fetch("/api/provider-nodes", { cache: "no-store" }),
|
||||||
]);
|
]);
|
||||||
const connectionsData = await connectionsRes.json();
|
const connectionsData = await connectionsRes.json();
|
||||||
const nodesData = await nodesRes.json();
|
const nodesData = await nodesRes.json();
|
||||||
@@ -76,7 +76,21 @@ export default function ProviderDetailPage() {
|
|||||||
setConnections(filtered);
|
setConnections(filtered);
|
||||||
}
|
}
|
||||||
if (nodesRes.ok) {
|
if (nodesRes.ok) {
|
||||||
const node = (nodesData.nodes || []).find((entry) => entry.id === providerId) || null;
|
let node = (nodesData.nodes || []).find((entry) => entry.id === providerId) || null;
|
||||||
|
|
||||||
|
// Newly created compatible nodes can be briefly unavailable on one worker.
|
||||||
|
// Retry a few times before showing "Provider not found".
|
||||||
|
if (!node && isCompatible) {
|
||||||
|
for (let attempt = 0; attempt < 3; attempt += 1) {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 150));
|
||||||
|
const retryRes = await fetch("/api/provider-nodes", { cache: "no-store" });
|
||||||
|
if (!retryRes.ok) continue;
|
||||||
|
const retryData = await retryRes.json();
|
||||||
|
node = (retryData.nodes || []).find((entry) => entry.id === providerId) || null;
|
||||||
|
if (node) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setProviderNode(node);
|
setProviderNode(node);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -1025,6 +1039,7 @@ function AddApiKeyModal({ isOpen, provider, providerName, isCompatible, isAnthro
|
|||||||
});
|
});
|
||||||
const [validating, setValidating] = useState(false);
|
const [validating, setValidating] = useState(false);
|
||||||
const [validationResult, setValidationResult] = useState(null);
|
const [validationResult, setValidationResult] = useState(null);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
|
||||||
const handleValidate = async () => {
|
const handleValidate = async () => {
|
||||||
setValidating(true);
|
setValidating(true);
|
||||||
@@ -1043,13 +1058,38 @@ function AddApiKeyModal({ isOpen, provider, providerName, isCompatible, isAnthro
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = () => {
|
const handleSubmit = async () => {
|
||||||
onSave({
|
if (!provider || !formData.apiKey) return;
|
||||||
name: formData.name,
|
|
||||||
apiKey: formData.apiKey,
|
setSaving(true);
|
||||||
priority: formData.priority,
|
try {
|
||||||
testStatus: validationResult === "success" ? "active" : "unknown",
|
let isValid = false;
|
||||||
});
|
try {
|
||||||
|
setValidating(true);
|
||||||
|
setValidationResult(null);
|
||||||
|
const res = await fetch("/api/providers/validate", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ provider, apiKey: formData.apiKey }),
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
isValid = !!data.valid;
|
||||||
|
setValidationResult(isValid ? "success" : "failed");
|
||||||
|
} catch {
|
||||||
|
setValidationResult("failed");
|
||||||
|
} finally {
|
||||||
|
setValidating(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
await onSave({
|
||||||
|
name: formData.name,
|
||||||
|
apiKey: formData.apiKey,
|
||||||
|
priority: formData.priority,
|
||||||
|
testStatus: isValid ? "active" : "unknown",
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!provider) return null;
|
if (!provider) return null;
|
||||||
@@ -1072,7 +1112,7 @@ function AddApiKeyModal({ isOpen, provider, providerName, isCompatible, isAnthro
|
|||||||
className="flex-1"
|
className="flex-1"
|
||||||
/>
|
/>
|
||||||
<div className="pt-6">
|
<div className="pt-6">
|
||||||
<Button onClick={handleValidate} disabled={!formData.apiKey || validating} variant="secondary">
|
<Button onClick={handleValidate} disabled={!formData.apiKey || validating || saving} variant="secondary">
|
||||||
{validating ? "Checking..." : "Check"}
|
{validating ? "Checking..." : "Check"}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
@@ -1097,8 +1137,8 @@ function AddApiKeyModal({ isOpen, provider, providerName, isCompatible, isAnthro
|
|||||||
onChange={(e) => setFormData({ ...formData, priority: Number.parseInt(e.target.value) || 1 })}
|
onChange={(e) => setFormData({ ...formData, priority: Number.parseInt(e.target.value) || 1 })}
|
||||||
/>
|
/>
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button onClick={handleSubmit} fullWidth disabled={!formData.name || !formData.apiKey}>
|
<Button onClick={handleSubmit} fullWidth disabled={!formData.name || !formData.apiKey || saving}>
|
||||||
Save
|
{saving ? "Saving..." : "Save"}
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={onClose} variant="ghost" fullWidth>
|
<Button onClick={onClose} variant="ghost" fullWidth>
|
||||||
Cancel
|
Cancel
|
||||||
@@ -1129,6 +1169,7 @@ function EditConnectionModal({ isOpen, connection, onSave, onClose }) {
|
|||||||
const [testResult, setTestResult] = useState(null);
|
const [testResult, setTestResult] = useState(null);
|
||||||
const [validating, setValidating] = useState(false);
|
const [validating, setValidating] = useState(false);
|
||||||
const [validationResult, setValidationResult] = useState(null);
|
const [validationResult, setValidationResult] = useState(null);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (connection) {
|
if (connection) {
|
||||||
@@ -1176,17 +1217,41 @@ function EditConnectionModal({ isOpen, connection, onSave, onClose }) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = () => {
|
const handleSubmit = async () => {
|
||||||
const updates = { name: formData.name, priority: formData.priority };
|
setSaving(true);
|
||||||
if (!isOAuth && formData.apiKey) {
|
try {
|
||||||
updates.apiKey = formData.apiKey;
|
const updates = { name: formData.name, priority: formData.priority };
|
||||||
if (validationResult === "success") {
|
if (!isOAuth && formData.apiKey) {
|
||||||
updates.testStatus = "active";
|
updates.apiKey = formData.apiKey;
|
||||||
updates.lastError = null;
|
let isValid = validationResult === "success";
|
||||||
updates.lastErrorAt = null;
|
if (!isValid) {
|
||||||
|
try {
|
||||||
|
setValidating(true);
|
||||||
|
setValidationResult(null);
|
||||||
|
const res = await fetch("/api/providers/validate", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ provider: connection.provider, apiKey: formData.apiKey }),
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
isValid = !!data.valid;
|
||||||
|
setValidationResult(isValid ? "success" : "failed");
|
||||||
|
} catch {
|
||||||
|
setValidationResult("failed");
|
||||||
|
} finally {
|
||||||
|
setValidating(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isValid) {
|
||||||
|
updates.testStatus = "active";
|
||||||
|
updates.lastError = null;
|
||||||
|
updates.lastErrorAt = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
await onSave(updates);
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
}
|
}
|
||||||
onSave(updates);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!connection) return null;
|
if (!connection) return null;
|
||||||
@@ -1228,7 +1293,7 @@ function EditConnectionModal({ isOpen, connection, onSave, onClose }) {
|
|||||||
className="flex-1"
|
className="flex-1"
|
||||||
/>
|
/>
|
||||||
<div className="pt-6">
|
<div className="pt-6">
|
||||||
<Button onClick={handleValidate} disabled={!formData.apiKey || validating} variant="secondary">
|
<Button onClick={handleValidate} disabled={!formData.apiKey || validating || saving} variant="secondary">
|
||||||
{validating ? "Checking..." : "Check"}
|
{validating ? "Checking..." : "Check"}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
@@ -1256,7 +1321,7 @@ function EditConnectionModal({ isOpen, connection, onSave, onClose }) {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button onClick={handleSubmit} fullWidth>Save</Button>
|
<Button onClick={handleSubmit} fullWidth disabled={saving}>{saving ? "Saving..." : "Save"}</Button>
|
||||||
<Button onClick={onClose} variant="ghost" fullWidth>Cancel</Button>
|
<Button onClick={onClose} variant="ghost" fullWidth>Cancel</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+79
-17
@@ -54,6 +54,60 @@ const defaultData = {
|
|||||||
pricing: {} // NEW: pricing configuration
|
pricing: {} // NEW: pricing configuration
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function cloneDefaultData() {
|
||||||
|
return {
|
||||||
|
providerConnections: [],
|
||||||
|
providerNodes: [],
|
||||||
|
modelAliases: {},
|
||||||
|
combos: [],
|
||||||
|
apiKeys: [],
|
||||||
|
settings: {
|
||||||
|
cloudEnabled: false,
|
||||||
|
stickyRoundRobinLimit: 3,
|
||||||
|
requireLogin: true,
|
||||||
|
},
|
||||||
|
pricing: {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensureDbShape(data) {
|
||||||
|
const defaults = cloneDefaultData();
|
||||||
|
const next = data && typeof data === "object" ? data : {};
|
||||||
|
let changed = false;
|
||||||
|
|
||||||
|
for (const [key, defaultValue] of Object.entries(defaults)) {
|
||||||
|
if (next[key] === undefined || next[key] === null) {
|
||||||
|
next[key] = defaultValue;
|
||||||
|
changed = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
key === "settings" &&
|
||||||
|
(typeof next.settings !== "object" || Array.isArray(next.settings))
|
||||||
|
) {
|
||||||
|
next.settings = { ...defaultValue };
|
||||||
|
changed = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
key === "settings" &&
|
||||||
|
typeof next.settings === "object" &&
|
||||||
|
!Array.isArray(next.settings)
|
||||||
|
) {
|
||||||
|
for (const [settingKey, settingDefault] of Object.entries(defaultValue)) {
|
||||||
|
if (next.settings[settingKey] === undefined) {
|
||||||
|
next.settings[settingKey] = settingDefault;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { data: next, changed };
|
||||||
|
}
|
||||||
|
|
||||||
// Singleton instance
|
// Singleton instance
|
||||||
let dbInstance = null;
|
let dbInstance = null;
|
||||||
|
|
||||||
@@ -64,35 +118,43 @@ export async function getDb() {
|
|||||||
if (isCloud) {
|
if (isCloud) {
|
||||||
// Return in-memory DB for Workers
|
// Return in-memory DB for Workers
|
||||||
if (!dbInstance) {
|
if (!dbInstance) {
|
||||||
dbInstance = new Low({ read: async () => {}, write: async () => {} }, defaultData);
|
const data = cloneDefaultData();
|
||||||
dbInstance.data = defaultData;
|
dbInstance = new Low({ read: async () => {}, write: async () => {} }, data);
|
||||||
|
dbInstance.data = data;
|
||||||
}
|
}
|
||||||
return dbInstance;
|
return dbInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dbInstance) {
|
if (!dbInstance) {
|
||||||
const adapter = new JSONFile(DB_FILE);
|
const adapter = new JSONFile(DB_FILE);
|
||||||
dbInstance = new Low(adapter, defaultData);
|
dbInstance = new Low(adapter, cloneDefaultData());
|
||||||
|
}
|
||||||
|
|
||||||
// Try to read DB with error recovery for corrupt JSON
|
// Always read latest disk state to avoid stale singleton data across route workers.
|
||||||
try {
|
try {
|
||||||
await dbInstance.read();
|
await dbInstance.read();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof SyntaxError) {
|
if (error instanceof SyntaxError) {
|
||||||
console.warn('[DB] Corrupt JSON detected, resetting to defaults...');
|
console.warn('[DB] Corrupt JSON detected, resetting to defaults...');
|
||||||
dbInstance.data = defaultData;
|
dbInstance.data = cloneDefaultData();
|
||||||
await dbInstance.write();
|
await dbInstance.write();
|
||||||
} else {
|
} else {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize with default data if empty
|
// Initialize/migrate missing keys for older DB schema versions.
|
||||||
if (!dbInstance.data) {
|
if (!dbInstance.data) {
|
||||||
dbInstance.data = defaultData;
|
dbInstance.data = cloneDefaultData();
|
||||||
|
await dbInstance.write();
|
||||||
|
} else {
|
||||||
|
const { data, changed } = ensureDbShape(dbInstance.data);
|
||||||
|
dbInstance.data = data;
|
||||||
|
if (changed) {
|
||||||
await dbInstance.write();
|
await dbInstance.write();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dbInstance;
|
return dbInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user