Files
9router/src/app/(dashboard)/dashboard/providers/[id]/AddApiKeyModal.js
kundeng 00bd1a4151 fix: add Azure validation and make Organization required
- Add Azure case to /api/providers/validate that sends a test chat
  completion with api-key header and organization
- Pass Azure-specific data (endpoint, deployment, apiVersion, org) from
  Add modal to validate endpoint
- Make Organization field required (needed for billing)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-20 01:24:39 -04:00

216 lines
7.2 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, proxyPools, onSave, onClose }) {
const NONE_PROXY_POOL_VALUE = "__none__";
const isAzure = provider === "azure";
const [formData, setFormData] = useState({
name: "",
apiKey: "",
priority: 1,
proxyPoolId: NONE_PROXY_POOL_VALUE,
});
const [azureData, setAzureData] = useState({
azureEndpoint: "",
apiVersion: "2024-10-01-preview",
deployment: "",
organization: "",
});
const [validating, setValidating] = useState(false);
const [validationResult, setValidationResult] = useState(null);
const [saving, setSaving] = useState(false);
const validatePayload = () => {
const payload = { provider, apiKey: formData.apiKey };
if (isAzure) payload.providerSpecificData = azureData;
return payload;
};
const handleValidate = async () => {
setValidating(true);
try {
const res = await fetch("/api/providers/validate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(validatePayload()),
});
const data = await res.json();
setValidationResult(data.valid ? "success" : "failed");
} catch {
setValidationResult("failed");
} finally {
setValidating(false);
}
};
const handleSubmit = async () => {
if (!provider || !formData.apiKey) 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(validatePayload()),
});
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,
proxyPoolId: formData.proxyPoolId === NONE_PROXY_POOL_VALUE ? null : formData.proxyPoolId,
testStatus: isValid ? "active" : "unknown",
providerSpecificData: isAzure ? {
azureEndpoint: azureData.azureEndpoint,
apiVersion: azureData.apiVersion,
deployment: azureData.deployment,
organization: azureData.organization,
} : undefined
});
} finally {
setSaving(false);
}
};
if (!provider) return null;
return (
<Modal isOpen={isOpen} title={`Add ${providerName || provider} API Key`} onClose={onClose}>
<div className="flex flex-col gap-4">
<Input
label="Name"
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
placeholder="Production Key"
/>
<div className="flex gap-2">
<Input
label="API Key"
type="password"
value={formData.apiKey}
onChange={(e) => setFormData({ ...formData, apiKey: e.target.value })}
className="flex-1"
/>
<div className="pt-6">
<Button onClick={handleValidate} disabled={!formData.apiKey || validating || saving} variant="secondary">
{validating ? "Checking..." : "Check"}
</Button>
</div>
</div>
{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>
)}
{isAzure && (
<div className="bg-sidebar/50 p-4 rounded-lg border border-accent/20">
<h3 className="font-semibold mb-3 text-sm">Azure OpenAI Configuration</h3>
<div className="flex flex-col gap-3">
<Input
label="Azure Endpoint"
value={azureData.azureEndpoint}
onChange={(e) => setAzureData({ ...azureData, azureEndpoint: e.target.value })}
placeholder="https://your-resource.openai.azure.com"
/>
<Input
label="Deployment Name"
value={azureData.deployment}
onChange={(e) => setAzureData({ ...azureData, deployment: e.target.value })}
placeholder="gpt-4"
/>
<Input
label="API Version"
value={azureData.apiVersion}
onChange={(e) => setAzureData({ ...azureData, apiVersion: e.target.value })}
placeholder="2024-10-01-preview"
/>
<Input
label="Organization"
value={azureData.organization}
onChange={(e) => setAzureData({ ...azureData, organization: e.target.value })}
placeholder="Organization ID"
/>
</div>
</div>
)}
<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={!formData.name || !formData.apiKey || (isAzure && (!azureData.azureEndpoint || !azureData.deployment || !azureData.organization)) || saving}>
{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,
proxyPools: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
name: PropTypes.string,
})),
onSave: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
};