9router/src/app/api/provider-nodes/[id]/route.js
decolua 0b8bed5793 Enhance image and embedding provider support
- Added new image models for GPT 5.2, 5.3, and 5.4, including capabilities for text-to-image and editing.
- Updated embedding handling to include optional dimensions in requests.
- Introduced support for custom embedding providers, allowing dynamic fetching and validation of custom nodes.
- Improved image generation handling with Codex integration, including progress tracking and error handling.
- Enhanced UI components to support adding custom embeddings and displaying their status.
2026-04-25 16:22:30 +07:00

101 lines
3.4 KiB
JavaScript

import { NextResponse } from "next/server";
import { deleteProviderConnectionsByProvider, deleteProviderNode, getProviderConnections, getProviderNodeById, updateProviderConnection, updateProviderNode } from "@/models";
// PUT /api/provider-nodes/[id] - Update provider node
export async function PUT(request, { params }) {
try {
const { id } = await params;
const body = await request.json();
const { name, prefix, apiType, baseUrl } = body;
const node = await getProviderNodeById(id);
if (!node) {
return NextResponse.json({ error: "Provider node not found" }, { status: 404 });
}
if (!name?.trim()) {
return NextResponse.json({ error: "Name is required" }, { status: 400 });
}
if (!prefix?.trim()) {
return NextResponse.json({ error: "Prefix is required" }, { status: 400 });
}
// Only validate apiType for OpenAI Compatible nodes
if (node.type === "openai-compatible" && (!apiType || !["chat", "responses"].includes(apiType))) {
return NextResponse.json({ error: "Invalid OpenAI compatible API type" }, { status: 400 });
}
if (!baseUrl?.trim()) {
return NextResponse.json({ error: "Base URL is required" }, { status: 400 });
}
let sanitizedBaseUrl = baseUrl.trim();
// Sanitize Base URL for Anthropic Compatible
if (node.type === "anthropic-compatible") {
sanitizedBaseUrl = sanitizedBaseUrl.replace(/\/$/, "");
if (sanitizedBaseUrl.endsWith("/messages")) {
sanitizedBaseUrl = sanitizedBaseUrl.slice(0, -9); // remove /messages
}
}
// Sanitize Base URL for Custom Embedding (strip trailing slash and /embeddings)
if (node.type === "custom-embedding") {
sanitizedBaseUrl = sanitizedBaseUrl.replace(/\/$/, "");
if (sanitizedBaseUrl.endsWith("/embeddings")) {
sanitizedBaseUrl = sanitizedBaseUrl.slice(0, -"/embeddings".length);
}
}
const updates = {
name: name.trim(),
prefix: prefix.trim(),
baseUrl: sanitizedBaseUrl,
};
if (node.type === "openai-compatible") {
updates.apiType = apiType;
}
const updated = await updateProviderNode(id, updates);
const connections = await getProviderConnections({ provider: id });
await Promise.all(connections.map((connection) => (
updateProviderConnection(connection.id, {
providerSpecificData: {
...(connection.providerSpecificData || {}),
prefix: prefix.trim(),
apiType: node.type === "openai-compatible" ? apiType : undefined,
baseUrl: sanitizedBaseUrl,
nodeName: updated.name,
}
})
)));
return NextResponse.json({ node: updated });
} catch (error) {
console.log("Error updating provider node:", error);
return NextResponse.json({ error: "Failed to update provider node" }, { status: 500 });
}
}
// DELETE /api/provider-nodes/[id] - Delete provider node and its connections
export async function DELETE(request, { params }) {
try {
const { id } = await params;
const node = await getProviderNodeById(id);
if (!node) {
return NextResponse.json({ error: "Provider node not found" }, { status: 404 });
}
await deleteProviderConnectionsByProvider(id);
await deleteProviderNode(id);
return NextResponse.json({ success: true });
} catch (error) {
console.log("Error deleting provider node:", error);
return NextResponse.json({ error: "Failed to delete provider node" }, { status: 500 });
}
}