mirror of
https://github.com/decolua/9router.git
synced 2026-05-08 12:01:28 +00:00
224 lines
7.7 KiB
JavaScript
224 lines
7.7 KiB
JavaScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import PropTypes from "prop-types";
|
|
import { Button, Badge, Input, Modal, Select } from "@/shared/components";
|
|
|
|
export default function AddApiKeyModal({ isOpen, provider, providerName, isCompatible, isAnthropic, authType, authHint, website, proxyPools, onSave, onClose }) {
|
|
const NONE_PROXY_POOL_VALUE = "__none__";
|
|
const isOllamaLocal = provider === "ollama-local";
|
|
const isCookie = authType === "cookie";
|
|
const credentialLabel = isCookie ? "Cookie Value" : "API Key";
|
|
const credentialPlaceholder = isCookie
|
|
? (provider === "grok-web" ? "sso=xxxxx... or just the raw value" : "eyJhbGciOi...")
|
|
: "";
|
|
|
|
const [formData, setFormData] = useState({
|
|
name: "",
|
|
apiKey: "",
|
|
priority: 1,
|
|
proxyPoolId: NONE_PROXY_POOL_VALUE,
|
|
ollamaHostUrl: "",
|
|
});
|
|
const [validating, setValidating] = useState(false);
|
|
const [validationResult, setValidationResult] = useState(null);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
const buildProviderSpecificData = () => {
|
|
if (isOllamaLocal && formData.ollamaHostUrl.trim()) {
|
|
return { baseUrl: formData.ollamaHostUrl.trim() };
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
const handleValidate = async () => {
|
|
setValidating(true);
|
|
try {
|
|
const res = await fetch("/api/providers/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ provider, apiKey: formData.apiKey, providerSpecificData: buildProviderSpecificData() }),
|
|
});
|
|
const data = await res.json();
|
|
setValidationResult(data.valid ? "success" : "failed");
|
|
} catch {
|
|
setValidationResult("failed");
|
|
} finally {
|
|
setValidating(false);
|
|
}
|
|
};
|
|
|
|
const handleSubmit = async () => {
|
|
if (!provider) return;
|
|
if (!isOllamaLocal && !formData.apiKey) return;
|
|
if (!isOllamaLocal) {
|
|
// Non-ollama providers require a name
|
|
if (!formData.name) return;
|
|
}
|
|
|
|
setSaving(true);
|
|
try {
|
|
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, providerSpecificData: buildProviderSpecificData() }),
|
|
});
|
|
const data = await res.json();
|
|
isValid = !!data.valid;
|
|
setValidationResult(isValid ? "success" : "failed");
|
|
} catch {
|
|
setValidationResult("failed");
|
|
} finally {
|
|
setValidating(false);
|
|
}
|
|
|
|
await onSave({
|
|
name: formData.name || (isOllamaLocal ? "Ollama Local" : ""),
|
|
apiKey: formData.apiKey,
|
|
priority: formData.priority,
|
|
proxyPoolId: formData.proxyPoolId === NONE_PROXY_POOL_VALUE ? null : formData.proxyPoolId,
|
|
testStatus: isValid ? "active" : "unknown",
|
|
providerSpecificData: buildProviderSpecificData()
|
|
});
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
if (!provider) return null;
|
|
|
|
return (
|
|
<Modal isOpen={isOpen} title={`Add ${providerName || provider} ${credentialLabel}`} onClose={onClose}>
|
|
<div className="flex flex-col gap-4">
|
|
<Input
|
|
label="Name"
|
|
value={formData.name}
|
|
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
|
placeholder={isOllamaLocal ? "Ollama Local" : "Production Key"}
|
|
/>
|
|
{isOllamaLocal && (
|
|
<div className="flex gap-2">
|
|
<Input
|
|
label="Ollama Host URL"
|
|
value={formData.ollamaHostUrl}
|
|
onChange={(e) => setFormData({ ...formData, ollamaHostUrl: e.target.value })}
|
|
placeholder="http://localhost:11434"
|
|
className="flex-1"
|
|
/>
|
|
<div className="pt-6">
|
|
<Button onClick={handleValidate} disabled={validating || saving} variant="secondary">
|
|
{validating ? "Checking..." : "Check"}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
{!isOllamaLocal && (
|
|
<div className="flex gap-2">
|
|
<Input
|
|
label={credentialLabel}
|
|
type={isCookie ? "text" : "password"}
|
|
value={formData.apiKey}
|
|
onChange={(e) => setFormData({ ...formData, apiKey: e.target.value })}
|
|
placeholder={credentialPlaceholder}
|
|
className="flex-1"
|
|
/>
|
|
<div className="pt-6">
|
|
<Button onClick={handleValidate} disabled={!formData.apiKey || validating || saving} variant="secondary">
|
|
{validating ? "Checking..." : "Check"}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
{isCookie && authHint && (
|
|
<p className="text-xs text-text-muted">
|
|
{authHint}
|
|
{website && (
|
|
<>
|
|
{" "}
|
|
<a href={website} target="_blank" rel="noopener noreferrer" className="text-primary underline">
|
|
Open {website.replace(/^https?:\/\//, "")}
|
|
</a>
|
|
</>
|
|
)}
|
|
</p>
|
|
)}
|
|
{isOllamaLocal && (
|
|
<p className="text-xs text-text-muted">
|
|
Leave blank to use <code>http://localhost:11434</code>. For remote Ollama, enter the full host URL (e.g. <code>http://192.168.1.10:11434</code>).
|
|
</p>
|
|
)}
|
|
{validationResult && (
|
|
<Badge variant={validationResult === "success" ? "success" : "error"}>
|
|
{validationResult === "success" ? "Valid" : "Invalid"}
|
|
</Badge>
|
|
)}
|
|
{isCompatible && (
|
|
<p className="text-xs text-text-muted">
|
|
{isAnthropic
|
|
? `Validation checks ${providerName || "Anthropic Compatible"} by verifying the API key.`
|
|
: `Validation checks ${providerName || "OpenAI Compatible"} via /models on your base URL.`
|
|
}
|
|
</p>
|
|
)}
|
|
<Input
|
|
label="Priority"
|
|
type="number"
|
|
value={formData.priority}
|
|
onChange={(e) => setFormData({ ...formData, priority: Number.parseInt(e.target.value) || 1 })}
|
|
/>
|
|
|
|
<Select
|
|
label="Proxy Pool"
|
|
value={formData.proxyPoolId}
|
|
onChange={(e) => setFormData({ ...formData, proxyPoolId: e.target.value })}
|
|
options={[
|
|
{ value: NONE_PROXY_POOL_VALUE, label: "None" },
|
|
...(proxyPools || []).map((pool) => ({ value: pool.id, label: pool.name })),
|
|
]}
|
|
placeholder="None"
|
|
/>
|
|
|
|
{(proxyPools || []).length === 0 && (
|
|
<p className="text-xs text-text-muted">
|
|
No active proxy pools available. Create one in Proxy Pools page first.
|
|
</p>
|
|
)}
|
|
|
|
<p className="text-xs text-text-muted">
|
|
Legacy manual proxy fields are still accepted by API for backward compatibility.
|
|
</p>
|
|
|
|
<div className="flex gap-2">
|
|
<Button onClick={handleSubmit} fullWidth disabled={saving || (!isOllamaLocal && (!formData.name || !formData.apiKey))}>
|
|
{saving ? "Saving..." : "Save"}
|
|
</Button>
|
|
<Button onClick={onClose} variant="ghost" fullWidth>
|
|
Cancel
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
AddApiKeyModal.propTypes = {
|
|
isOpen: PropTypes.bool.isRequired,
|
|
provider: PropTypes.string,
|
|
providerName: PropTypes.string,
|
|
isCompatible: PropTypes.bool,
|
|
isAnthropic: PropTypes.bool,
|
|
authType: PropTypes.string,
|
|
authHint: PropTypes.string,
|
|
website: PropTypes.string,
|
|
proxyPools: PropTypes.arrayOf(PropTypes.shape({
|
|
id: PropTypes.string,
|
|
name: PropTypes.string,
|
|
})),
|
|
onSave: PropTypes.func.isRequired,
|
|
onClose: PropTypes.func.isRequired,
|
|
};
|