# Conflicts: # open-sse/executors/qoder.js # open-sse/handlers/chatCore.js # open-sse/handlers/chatCore/sseToJsonHandler.js # open-sse/providers/registry/commandcode.js # src/app/(dashboard)/dashboard/combos/page.js # src/app/api/v1/models/route.js # src/lib/db/repos/usageRepo.js # src/shared/components/UsageStats.js
423 lines
16 KiB
JavaScript
423 lines
16 KiB
JavaScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import PropTypes from "prop-types";
|
|
import { Button, Badge, Input, Modal, Select } from "@/shared/components";
|
|
import { AI_PROVIDERS } from "@/shared/constants/providers";
|
|
import { planBulkAdd } from "@/shared/utils/bulkAdd";
|
|
|
|
const BULK_PLACEHOLDER = `name1|sk-key1\nname2|sk-key2\nsk-key-only-auto-named`;
|
|
|
|
export default function AddApiKeyModal({ isOpen, provider, providerName, isCompatible, isAnthropic, authType, authHint, website, proxyPools, error, existingNames, onSave, onBulkDone, onClose }) {
|
|
const NONE_PROXY_POOL_VALUE = "__none__";
|
|
const isOllamaLocal = provider === "ollama-local";
|
|
const isCookie = authType === "cookie";
|
|
const isXaiApiKey = provider === "xai" && !isCookie;
|
|
const credentialLabel = isCookie ? "Cookie Value" : provider === "qoder" || provider === "qoder-cn" ? "Personal Access Token (PAT)" : "API Key";
|
|
const credentialPlaceholder = isCookie
|
|
? (provider === "grok-web" ? "sso=xxxxx... or just the raw value" : "eyJhbGciOi...")
|
|
: (isXaiApiKey ? "xai-..." : provider === "qoder" || provider === "qoder-cn" ? "pt-..." : "");
|
|
|
|
const isAzure = provider === "azure";
|
|
const isCloudflareAi = provider === "cloudflare-ai";
|
|
const providerRegions = AI_PROVIDERS?.[provider]?.regions || null;
|
|
const defaultRegion = AI_PROVIDERS?.[provider]?.defaultRegion || providerRegions?.[0]?.id || "";
|
|
|
|
const [formData, setFormData] = useState({
|
|
name: "",
|
|
apiKey: "",
|
|
defaultModel: "",
|
|
priority: 1,
|
|
proxyPoolId: NONE_PROXY_POOL_VALUE,
|
|
ollamaHostUrl: "",
|
|
});
|
|
const [azureData, setAzureData] = useState({
|
|
azureEndpoint: "",
|
|
apiVersion: "2024-10-01-preview",
|
|
deployment: "",
|
|
organization: "",
|
|
});
|
|
const [cloudflareData, setCloudflareData] = useState({ accountId: "" });
|
|
const [region, setRegion] = useState(defaultRegion);
|
|
const [validating, setValidating] = useState(false);
|
|
const [validationResult, setValidationResult] = useState(null);
|
|
const [saving, setSaving] = useState(false);
|
|
const bulkPlaceholder = isCloudflareAi
|
|
? `name1|sk-key1|acc123456\nname2|sk-key2|def789012\nsk-key-only-auto-named`
|
|
: provider === "qoder" || provider === "qoder-cn"
|
|
? `name1|pt-xxxxx\nname2|pt-yyyyy\npt-only-auto-named`
|
|
: BULK_PLACEHOLDER;
|
|
|
|
const [mode, setMode] = useState("single"); // "single" | "bulk"
|
|
const [bulkText, setBulkText] = useState("");
|
|
const [bulkResult, setBulkResult] = useState(null); // { success, failed }
|
|
|
|
const buildProviderSpecificData = () => {
|
|
if (isOllamaLocal && formData.ollamaHostUrl.trim()) {
|
|
return { baseUrl: formData.ollamaHostUrl.trim() };
|
|
}
|
|
if (isAzure) {
|
|
return {
|
|
azureEndpoint: azureData.azureEndpoint,
|
|
apiVersion: azureData.apiVersion,
|
|
deployment: azureData.deployment,
|
|
organization: azureData.organization,
|
|
};
|
|
}
|
|
if (isCloudflareAi) {
|
|
return { accountId: cloudflareData.accountId };
|
|
}
|
|
if (providerRegions && region) {
|
|
return { region };
|
|
}
|
|
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,
|
|
defaultModel: isCompatible ? formData.defaultModel.trim() : undefined,
|
|
priority: formData.priority,
|
|
proxyPoolId: formData.proxyPoolId === NONE_PROXY_POOL_VALUE ? null : formData.proxyPoolId,
|
|
testStatus: isValid ? "active" : "unknown",
|
|
providerSpecificData: buildProviderSpecificData()
|
|
});
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
const handleBulkSubmit = async () => {
|
|
const lines = bulkText.split("\n");
|
|
if (!lines.length) return;
|
|
// Plan collision-free names against existing connections so a generated
|
|
// "Key N" never matches a saved name (which the backend would upsert /
|
|
// overwrite instead of inserting). See bulkAdd.js for the full rationale.
|
|
const plan = planBulkAdd(lines, existingNames, { isCloudflareAi });
|
|
if (!plan.length) return;
|
|
setSaving(true);
|
|
setBulkResult(null);
|
|
let success = 0;
|
|
let failed = 0;
|
|
for (const entry of plan) {
|
|
try {
|
|
// Validate each key before saving so bulk-added connections get a
|
|
// real status (active/unknown) like single adds, instead of a
|
|
// hardcoded "unknown" that never flips until a manual test.
|
|
let isValid = false;
|
|
try {
|
|
const vres = await fetch("/api/providers/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ provider, apiKey: entry.apiKey }),
|
|
});
|
|
const vdata = await vres.json().catch(() => ({}));
|
|
isValid = !!vdata.valid;
|
|
} catch {
|
|
isValid = false;
|
|
}
|
|
const res = await fetch("/api/providers", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
provider,
|
|
apiKey: entry.apiKey,
|
|
name: entry.name,
|
|
priority: 1,
|
|
testStatus: isValid ? "active" : "unknown",
|
|
...(entry.providerSpecificData ? { providerSpecificData: entry.providerSpecificData } : {}),
|
|
}),
|
|
});
|
|
if (res.ok) success++;
|
|
else failed++;
|
|
} catch {
|
|
failed++;
|
|
}
|
|
}
|
|
setSaving(false);
|
|
setBulkResult({ success, failed });
|
|
if (success > 0 && onBulkDone) onBulkDone();
|
|
};
|
|
|
|
if (!provider) return null;
|
|
|
|
return (
|
|
<Modal isOpen={isOpen} title={`Add ${providerName || provider} ${credentialLabel}`} onClose={onClose}>
|
|
<div className="flex flex-col gap-4">
|
|
{/* Mode switcher */}
|
|
<div className="flex gap-2">
|
|
<Button size="sm" variant={mode === "single" ? "primary" : "ghost"} onClick={() => { setMode("single"); setBulkResult(null); }}>Single</Button>
|
|
<Button size="sm" variant={mode === "bulk" ? "primary" : "ghost"} onClick={() => { setMode("bulk"); setBulkResult(null); }}>Bulk Add</Button>
|
|
</div>
|
|
|
|
{mode === "bulk" && (
|
|
<div className="flex flex-col gap-3">
|
|
<p className="text-xs text-text-muted">
|
|
{isCloudflareAi
|
|
? <>One key per line. Format: <code>name|apiKey|accountId</code> or just <code>apiKey</code> (auto-named by index).</>
|
|
: provider === "qoder" || provider === "qoder-cn"
|
|
? <>One PAT per line. Format: <code>name|pt-...</code> or just <code>pt-...</code> (auto-named by index).</>
|
|
: <>One key per line. Format: <code>name|apiKey</code> or just <code>apiKey</code> (auto-named by index).</>
|
|
}
|
|
</p>
|
|
<textarea
|
|
className="w-full rounded border border-accent/30 bg-sidebar p-2 text-sm font-mono resize-y min-h-[140px] focus:outline-none focus:ring-1 focus:ring-primary"
|
|
placeholder={bulkPlaceholder}
|
|
value={bulkText}
|
|
onChange={(e) => setBulkText(e.target.value)}
|
|
/>
|
|
{bulkResult && (
|
|
<div className={`text-sm font-medium ${bulkResult.failed > 0 ? "text-yellow-400" : "text-green-400"}`}>
|
|
✓ {bulkResult.success} added{bulkResult.failed > 0 ? `, ✗ ${bulkResult.failed} failed` : ""}
|
|
</div>
|
|
)}
|
|
<div className="flex gap-2">
|
|
<Button onClick={handleBulkSubmit} fullWidth disabled={saving || !bulkText.trim()}>
|
|
{saving ? "Adding..." : "Add All Keys"}
|
|
</Button>
|
|
<Button onClick={onClose} variant="ghost" fullWidth>Cancel</Button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{mode === "single" && (<>
|
|
<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>
|
|
)}
|
|
{isXaiApiKey && (
|
|
<p className="text-xs text-text-muted">
|
|
Use a direct xAI API key from console.x.ai. This is separate from Grok Build OAuth.
|
|
</p>
|
|
)}
|
|
{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>
|
|
)}
|
|
{providerRegions && (
|
|
<Select
|
|
label="Region"
|
|
value={region}
|
|
onChange={(e) => setRegion(e.target.value)}
|
|
options={providerRegions.map((r) => ({ value: r.id, label: r.label }))}
|
|
/>
|
|
)}
|
|
{isCompatible && (
|
|
<Input
|
|
label="Default Model"
|
|
value={formData.defaultModel}
|
|
onChange={(e) => setFormData({ ...formData, defaultModel: e.target.value })}
|
|
placeholder={isAnthropic ? "claude-3-5-sonnet-latest" : "gpt-4o-mini"}
|
|
hint="Optional. Used only as the model for connection checks when no model is picked. The node's own model list takes precedence."
|
|
/>
|
|
)}
|
|
{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>
|
|
)}
|
|
{error && (
|
|
<p className="text-xs text-red-500 break-words">{error}</p>
|
|
)}
|
|
{isCloudflareAi && (
|
|
<div className="bg-sidebar/50 p-4 rounded-lg border border-accent/20">
|
|
<h3 className="font-semibold mb-3 text-sm">Cloudflare Workers AI</h3>
|
|
<Input
|
|
label="Account ID"
|
|
value={cloudflareData.accountId}
|
|
onChange={(e) => setCloudflareData({ ...cloudflareData, accountId: e.target.value })}
|
|
placeholder="abc123def456..."
|
|
/>
|
|
<p className="text-xs text-text-muted mt-2">
|
|
Find your Account ID in the right sidebar of <a href="https://dash.cloudflare.com" target="_blank" rel="noopener noreferrer" className="text-primary underline">dash.cloudflare.com</a>
|
|
</p>
|
|
</div>
|
|
)}
|
|
{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={saving || (!isOllamaLocal && (!formData.name || !formData.apiKey)) || (isAzure && (!azureData.azureEndpoint || !azureData.deployment || !azureData.organization)) || (isCloudflareAi && !cloudflareData.accountId)}>
|
|
{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,
|
|
})),
|
|
error: PropTypes.string,
|
|
existingNames: PropTypes.arrayOf(PropTypes.string),
|
|
onSave: PropTypes.func.isRequired,
|
|
onBulkDone: PropTypes.func,
|
|
onClose: PropTypes.func.isRequired,
|
|
};
|