mirror of
https://github.com/decolua/9router.git
synced 2026-05-08 12:01:28 +00:00
feat: add Azure OpenAI provider support
This commit is contained in:
@@ -357,6 +357,11 @@ export const PROVIDERS = {
|
||||
format: "perplexity-web",
|
||||
authType: "cookie"
|
||||
},
|
||||
azure: {
|
||||
baseUrl: "",
|
||||
format: "openai",
|
||||
headers: {}
|
||||
},
|
||||
};
|
||||
|
||||
export const OLLAMA_LOCAL_DEFAULT_HOST = "http://localhost:11434";
|
||||
|
||||
57
open-sse/executors/azure.js
Normal file
57
open-sse/executors/azure.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import { DefaultExecutor } from "./default.js";
|
||||
|
||||
export class AzureExecutor extends DefaultExecutor {
|
||||
constructor() {
|
||||
super("azure");
|
||||
}
|
||||
|
||||
buildUrl(model, stream, urlIndex = 0, credentials = null) {
|
||||
const azureEndpoint = credentials?.providerSpecificData?.azureEndpoint
|
||||
|| process.env.AZURE_ENDPOINT
|
||||
|| "https://api.openai.com";
|
||||
|
||||
const apiVersion = credentials?.providerSpecificData?.apiVersion
|
||||
|| process.env.AZURE_API_VERSION
|
||||
|| "2024-10-01-preview";
|
||||
|
||||
const deployment = credentials?.providerSpecificData?.deployment
|
||||
|| model
|
||||
|| process.env.AZURE_DEPLOYMENT
|
||||
|| "gpt-4";
|
||||
|
||||
const endpoint = azureEndpoint.replace(/\/$/, "");
|
||||
return `${endpoint}/openai/deployments/${deployment}/chat/completions?api-version=${apiVersion}`;
|
||||
}
|
||||
|
||||
buildHeaders(credentials, stream = true) {
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
...this.config.headers
|
||||
};
|
||||
|
||||
const apiKey = credentials?.apiKey
|
||||
|| credentials?.accessToken
|
||||
|| process.env.OPENAI_API_KEY;
|
||||
|
||||
if (apiKey) {
|
||||
headers["api-key"] = apiKey;
|
||||
}
|
||||
|
||||
const organization = credentials?.providerSpecificData?.organization
|
||||
|| process.env.AZURE_ORGANIZATION;
|
||||
|
||||
if (organization) {
|
||||
headers["OpenAI-Organization"] = organization;
|
||||
}
|
||||
|
||||
if (stream) {
|
||||
headers["Accept"] = "text/event-stream";
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
transformRequest(model, body, stream, credentials) {
|
||||
return body;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { AntigravityExecutor } from "./antigravity.js";
|
||||
import { AzureExecutor } from "./azure.js";
|
||||
import { GeminiCLIExecutor } from "./gemini-cli.js";
|
||||
import { GithubExecutor } from "./github.js";
|
||||
import { IFlowExecutor } from "./iflow.js";
|
||||
@@ -16,6 +17,7 @@ import { DefaultExecutor } from "./default.js";
|
||||
|
||||
const executors = {
|
||||
antigravity: new AntigravityExecutor(),
|
||||
azure: new AzureExecutor(),
|
||||
"gemini-cli": new GeminiCLIExecutor(),
|
||||
github: new GithubExecutor(),
|
||||
iflow: new IFlowExecutor(),
|
||||
@@ -47,6 +49,7 @@ export function hasSpecializedExecutor(provider) {
|
||||
|
||||
export { BaseExecutor } from "./base.js";
|
||||
export { AntigravityExecutor } from "./antigravity.js";
|
||||
export { AzureExecutor } from "./azure.js";
|
||||
export { GeminiCLIExecutor } from "./gemini-cli.js";
|
||||
export { GithubExecutor } from "./github.js";
|
||||
export { IFlowExecutor } from "./iflow.js";
|
||||
|
||||
@@ -13,6 +13,8 @@ export default function AddApiKeyModal({ isOpen, provider, providerName, isCompa
|
||||
? (provider === "grok-web" ? "sso=xxxxx... or just the raw value" : "eyJhbGciOi...")
|
||||
: "";
|
||||
|
||||
const isAzure = provider === "azure";
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
name: "",
|
||||
apiKey: "",
|
||||
@@ -20,6 +22,12 @@ export default function AddApiKeyModal({ isOpen, provider, providerName, isCompa
|
||||
proxyPoolId: NONE_PROXY_POOL_VALUE,
|
||||
ollamaHostUrl: "",
|
||||
});
|
||||
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);
|
||||
@@ -28,6 +36,14 @@ export default function AddApiKeyModal({ isOpen, provider, providerName, isCompa
|
||||
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,
|
||||
};
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
@@ -164,6 +180,38 @@ export default function AddApiKeyModal({ isOpen, provider, providerName, isCompa
|
||||
}
|
||||
</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"
|
||||
@@ -193,7 +241,8 @@ export default function AddApiKeyModal({ isOpen, provider, providerName, isCompa
|
||||
</p>
|
||||
|
||||
<div className="flex gap-2">
|
||||
<Button onClick={handleSubmit} fullWidth disabled={saving || (!isOllamaLocal && (!formData.name || !formData.apiKey))}>
|
||||
<Button onClick={handleSubmit} fullWidth disabled={saving || (!isOllamaLocal && (!formData.name || !formData.apiKey)) || (isAzure && (!azureData.azureEndpoint || !azureData.deployment || !azureData.organization))}>
|
||||
|
||||
{saving ? "Saving..." : "Save"}
|
||||
</Button>
|
||||
<Button onClick={onClose} variant="ghost" fullWidth>
|
||||
|
||||
@@ -366,6 +366,21 @@ async function testApiKeyConnection(connection, effectiveProxy = null) {
|
||||
|
||||
try {
|
||||
switch (connection.provider) {
|
||||
case "azure": {
|
||||
const psd = connection.providerSpecificData || {};
|
||||
const endpoint = (psd.azureEndpoint || "").replace(/\/$/, "");
|
||||
const deployment = psd.deployment || "gpt-4";
|
||||
const apiVersion = psd.apiVersion || "2024-10-01-preview";
|
||||
const url = `${endpoint}/openai/deployments/${deployment}/chat/completions?api-version=${apiVersion}`;
|
||||
const headers = { "api-key": connection.apiKey, "Content-Type": "application/json" };
|
||||
if (psd.organization) headers["OpenAI-Organization"] = psd.organization;
|
||||
const res = await fetchWithConnectionProxy(url, {
|
||||
method: "POST", headers,
|
||||
body: JSON.stringify({ messages: [{ role: "user", content: "test" }], max_completion_tokens: 1 }),
|
||||
}, effectiveProxy);
|
||||
const valid = res.status !== 401 && res.status !== 403;
|
||||
return { valid, error: valid ? null : "Invalid API key or Azure configuration" };
|
||||
}
|
||||
case "openai": {
|
||||
const res = await fetchWithConnectionProxy("https://api.openai.com/v1/models", { headers: { Authorization: `Bearer ${connection.apiKey}` } }, effectiveProxy);
|
||||
return { valid: res.ok, error: res.ok ? null : "Invalid API key" };
|
||||
|
||||
@@ -116,7 +116,7 @@ export async function POST(request) {
|
||||
return NextResponse.json({ error: "Name is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
let providerSpecificData = null;
|
||||
let providerSpecificData = body.providerSpecificData || null;
|
||||
|
||||
if (isOpenAICompatibleProvider(provider)) {
|
||||
const node = await getProviderNodeById(provider);
|
||||
|
||||
@@ -63,6 +63,35 @@ export async function POST(request) {
|
||||
});
|
||||
}
|
||||
|
||||
if (provider === "azure") {
|
||||
const { providerSpecificData } = body;
|
||||
const endpoint = (providerSpecificData?.azureEndpoint || "").replace(/\/$/, "");
|
||||
const deployment = providerSpecificData?.deployment || "gpt-4";
|
||||
const apiVersion = providerSpecificData?.apiVersion || "2024-10-01-preview";
|
||||
const organization = providerSpecificData?.organization;
|
||||
|
||||
const url = `${endpoint}/openai/deployments/${deployment}/chat/completions?api-version=${apiVersion}`;
|
||||
const headers = {
|
||||
"api-key": apiKey,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
if (organization) headers["OpenAI-Organization"] = organization;
|
||||
|
||||
const azureRes = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
messages: [{ role: "user", content: "test" }],
|
||||
max_tokens: 1,
|
||||
}),
|
||||
});
|
||||
isValid = azureRes.status !== 401 && azureRes.status !== 403;
|
||||
return NextResponse.json({
|
||||
valid: isValid,
|
||||
error: isValid ? null : "Invalid API key or Azure configuration",
|
||||
});
|
||||
}
|
||||
|
||||
switch (provider) {
|
||||
case "openai":
|
||||
const openaiRes = await fetch("https://api.openai.com/v1/models", {
|
||||
|
||||
@@ -14,6 +14,12 @@ export default function EditConnectionModal({ isOpen, connection, proxyPools, on
|
||||
priority: 1,
|
||||
apiKey: "",
|
||||
});
|
||||
const [azureData, setAzureData] = useState({
|
||||
azureEndpoint: "",
|
||||
apiVersion: "2024-10-01-preview",
|
||||
deployment: "",
|
||||
organization: "",
|
||||
});
|
||||
const [testing, setTesting] = useState(false);
|
||||
const [testResult, setTestResult] = useState(null);
|
||||
const [validating, setValidating] = useState(false);
|
||||
@@ -27,12 +33,22 @@ export default function EditConnectionModal({ isOpen, connection, proxyPools, on
|
||||
priority: connection.priority || 1,
|
||||
apiKey: "",
|
||||
});
|
||||
// Load Azure-specific data if present
|
||||
if (connection.provider === "azure" && connection.providerSpecificData) {
|
||||
setAzureData({
|
||||
azureEndpoint: connection.providerSpecificData.azureEndpoint || "",
|
||||
apiVersion: connection.providerSpecificData.apiVersion || "2024-10-01-preview",
|
||||
deployment: connection.providerSpecificData.deployment || "",
|
||||
organization: connection.providerSpecificData.organization || "",
|
||||
});
|
||||
}
|
||||
setTestResult(null);
|
||||
setValidationResult(null);
|
||||
}
|
||||
}, [connection]);
|
||||
|
||||
const isOAuth = connection?.authType === "oauth";
|
||||
const isAzure = connection?.provider === "azure";
|
||||
const isCompatible = connection
|
||||
? (isOpenAICompatibleProvider(connection.provider) || isAnthropicCompatibleProvider(connection.provider))
|
||||
: false;
|
||||
@@ -60,7 +76,11 @@ export default function EditConnectionModal({ isOpen, connection, proxyPools, on
|
||||
const res = await fetch("/api/providers/validate", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ provider: connection.provider, apiKey: formData.apiKey }),
|
||||
body: JSON.stringify({
|
||||
provider: connection.provider,
|
||||
apiKey: formData.apiKey,
|
||||
...(isAzure ? { providerSpecificData: azureData } : {}),
|
||||
}),
|
||||
});
|
||||
const data = await res.json();
|
||||
setValidationResult(data.valid ? "success" : "failed");
|
||||
@@ -89,7 +109,11 @@ export default function EditConnectionModal({ isOpen, connection, proxyPools, on
|
||||
const res = await fetch("/api/providers/validate", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ provider: connection.provider, apiKey: formData.apiKey }),
|
||||
body: JSON.stringify({
|
||||
provider: connection.provider,
|
||||
apiKey: formData.apiKey,
|
||||
...(isAzure ? { providerSpecificData: azureData } : {}),
|
||||
}),
|
||||
});
|
||||
const data = await res.json();
|
||||
isValid = !!data.valid;
|
||||
@@ -106,6 +130,17 @@ export default function EditConnectionModal({ isOpen, connection, proxyPools, on
|
||||
updates.lastErrorAt = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Add Azure-specific data if this is an Azure connection
|
||||
if (isAzure) {
|
||||
updates.providerSpecificData = {
|
||||
azureEndpoint: azureData.azureEndpoint,
|
||||
apiVersion: azureData.apiVersion,
|
||||
deployment: azureData.deployment,
|
||||
organization: azureData.organization,
|
||||
};
|
||||
}
|
||||
|
||||
await onSave(updates);
|
||||
} finally {
|
||||
setSaving(false);
|
||||
@@ -162,7 +197,43 @@ export default function EditConnectionModal({ isOpen, connection, proxyPools, on
|
||||
</>
|
||||
)}
|
||||
|
||||
{!isCompatible && (
|
||||
{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"
|
||||
hint="Your Azure OpenAI resource endpoint URL"
|
||||
/>
|
||||
<Input
|
||||
label="Deployment Name"
|
||||
value={azureData.deployment}
|
||||
onChange={(e) => setAzureData({ ...azureData, deployment: e.target.value })}
|
||||
placeholder="gpt-4"
|
||||
hint="The deployment name in your Azure resource"
|
||||
/>
|
||||
<Input
|
||||
label="API Version"
|
||||
value={azureData.apiVersion}
|
||||
onChange={(e) => setAzureData({ ...azureData, apiVersion: e.target.value })}
|
||||
placeholder="2024-10-01-preview"
|
||||
hint="Azure OpenAI API version to use"
|
||||
/>
|
||||
<Input
|
||||
label="Organization"
|
||||
value={azureData.organization}
|
||||
onChange={(e) => setAzureData({ ...azureData, organization: e.target.value })}
|
||||
placeholder="Organization ID"
|
||||
hint="Required for billing"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!isCompatible && !isAzure && (
|
||||
<div className="flex items-center gap-3">
|
||||
<Button onClick={handleTest} variant="secondary" disabled={testing}>
|
||||
{testing ? "Testing..." : "Test Connection"}
|
||||
@@ -202,3 +273,4 @@ EditConnectionModal.propTypes = {
|
||||
onSave: PropTypes.func.isRequired,
|
||||
onClose: PropTypes.func.isRequired,
|
||||
};
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ export const APIKEY_PROVIDERS = {
|
||||
openai: { id: "openai", alias: "openai", name: "OpenAI", icon: "auto_awesome", color: "#10A37F", textIcon: "OA", website: "https://platform.openai.com", serviceKinds: ["llm", "embedding", "tts", "image", "imageToText", "webSearch"], thinkingConfig: THINKING_CONFIG.effort },
|
||||
anthropic: { id: "anthropic", alias: "anthropic", name: "Anthropic", icon: "smart_toy", color: "#D97757", textIcon: "AN", website: "https://console.anthropic.com", serviceKinds: ["llm", "imageToText"] },
|
||||
"opencode-go": { id: "opencode-go", alias: "ocg", name: "OpenCode Go", icon: "terminal", color: "#E87040", textIcon: "OC", website: "https://opencode.ai/auth", notice: { text: "OpenCode Go subscription: $5/mo (then $10/mo). Access to Kimi, GLM, Qwen, MiMo, MiniMax models.", apiKeyUrl: "https://opencode.ai/auth" } },
|
||||
|
||||
azure: { id: "azure", alias: "azure", name: "Azure OpenAI", icon: "cloud", color: "#0078D4", textIcon: "AZ", website: "https://azure.microsoft.com/en-us/products/ai-services/openai-service", hasProviderSpecificData: true },
|
||||
|
||||
deepseek: { id: "deepseek", alias: "ds", name: "DeepSeek", icon: "bolt", color: "#4D6BFE", textIcon: "DS", website: "https://deepseek.com" },
|
||||
groq: { id: "groq", alias: "groq", name: "Groq", icon: "speed", color: "#F55036", textIcon: "GQ", website: "https://groq.com", serviceKinds: ["llm", "imageToText"] },
|
||||
|
||||
Reference in New Issue
Block a user