mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
feat: add /v1/embeddings endpoint (OpenAI-compatible) (#146)
* feat: implement /v1/embeddings endpoint (#117) Add OpenAI-compatible POST /v1/embeddings endpoint that routes through the existing provider credential + fallback infrastructure. Changes: - open-sse/handlers/embeddingsCore.js: core handler (handleEmbeddingsCore) * Validates input (string or array), encoding_format * Builds provider-specific URL and headers for openai, openrouter, and openai-compatible providers * Handles 401/403 token refresh via executor.refreshCredentials * Returns normalized OpenAI-format response { object: 'list', data, model, usage } - cloud/src/handlers/embeddings.js: cloud Worker handler (handleEmbeddings) * Auth + machineId resolution identical to handleChat * Provider credential fallback loop with rate-limit tracking - cloud/src/index.js: wire new routes * POST /v1/embeddings (new format — machineId from API key) * POST /{machineId}/v1/embeddings (old format — machineId from URL) * test: add unit tests for /v1/embeddings endpoint - Setup vitest as test framework (tests/ directory) - embeddingsCore.test.js (36 tests): - buildEmbeddingsBody: single string, array, encoding_format, default float - buildEmbeddingsUrl: openai, openrouter, openai-compatible-*, unsupported - buildEmbeddingsHeaders: per-provider headers, accessToken fallback - handleEmbeddingsCore: input validation, success path, provider errors, network errors, invalid JSON, token refresh 401 handling - embeddings.cloud.test.js (23 tests): - CORS OPTIONS preflight - Auth: missing/invalid/old-format/wrong key → 401/400 - Body validation: bad JSON, missing model, missing input, bad model → 400 - Happy path: single string, array, delegation, CORS header, machineId override - Rate limiting: all-rate-limited → 429 + Retry-After, no credentials → 400 - Error propagation: non-fallback errors, 429 exhausts accounts Total: 59/59 tests passing Framework: vitest v4.0.18, Node v22.22.0 * feat: add Next.js API route for /v1/embeddings endpoint Wire the embeddings handler into Next.js App Router. - src/app/api/v1/embeddings/route.js: Next.js API route (POST + OPTIONS) - src/sse/handlers/embeddings.js: SSE-layer handler mirroring chat.js pattern Uses handleEmbeddingsCore from open-sse/handlers/embeddingsCore.js with the same auth, credential fallback, and token refresh logic as the chat handler. Supports REQUIRE_API_KEY env var, provider fallback loop, and consistent logging.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import {
|
||||
getProviderCredentials,
|
||||
markAccountUnavailable,
|
||||
clearAccountError,
|
||||
extractApiKey,
|
||||
isValidApiKey,
|
||||
} from "../services/auth.js";
|
||||
import { getModelInfo } from "../services/model.js";
|
||||
import { handleEmbeddingsCore } from "open-sse/handlers/embeddingsCore.js";
|
||||
import { errorResponse, unavailableResponse } from "open-sse/utils/error.js";
|
||||
import { HTTP_STATUS } from "open-sse/config/constants.js";
|
||||
import * as log from "../utils/logger.js";
|
||||
import { updateProviderCredentials, checkAndRefreshToken } from "../services/tokenRefresh.js";
|
||||
|
||||
/**
|
||||
* Handle embeddings request for the SSE/Next.js server.
|
||||
* Follows the same auth + fallback pattern as handleChat.
|
||||
*
|
||||
* @param {Request} request
|
||||
*/
|
||||
export async function handleEmbeddings(request) {
|
||||
let body;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
log.warn("EMBEDDINGS", "Invalid JSON body");
|
||||
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid JSON body");
|
||||
}
|
||||
|
||||
const url = new URL(request.url);
|
||||
const modelStr = body.model;
|
||||
|
||||
log.request("POST", `${url.pathname} | ${modelStr}`);
|
||||
|
||||
// Log API key (masked)
|
||||
const apiKey = extractApiKey(request);
|
||||
if (apiKey) {
|
||||
log.debug("AUTH", `API Key: ${log.maskKey(apiKey)}`);
|
||||
} else {
|
||||
log.debug("AUTH", "No API key provided (local mode)");
|
||||
}
|
||||
|
||||
// Optional strict API key validation
|
||||
if (process.env.REQUIRE_API_KEY === "true") {
|
||||
if (!apiKey) {
|
||||
log.warn("AUTH", "Missing API key while REQUIRE_API_KEY=true");
|
||||
return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Missing API key");
|
||||
}
|
||||
const valid = await isValidApiKey(apiKey);
|
||||
if (!valid) {
|
||||
log.warn("AUTH", "Invalid API key while REQUIRE_API_KEY=true");
|
||||
return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Invalid API key");
|
||||
}
|
||||
}
|
||||
|
||||
if (!modelStr) {
|
||||
log.warn("EMBEDDINGS", "Missing model");
|
||||
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Missing model");
|
||||
}
|
||||
|
||||
if (!body.input) {
|
||||
log.warn("EMBEDDINGS", "Missing input");
|
||||
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Missing required field: input");
|
||||
}
|
||||
|
||||
const modelInfo = await getModelInfo(modelStr);
|
||||
if (!modelInfo.provider) {
|
||||
log.warn("EMBEDDINGS", "Invalid model format", { model: modelStr });
|
||||
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid model format");
|
||||
}
|
||||
|
||||
const { provider, model } = modelInfo;
|
||||
|
||||
if (modelStr !== `${provider}/${model}`) {
|
||||
log.info("ROUTING", `${modelStr} → ${provider}/${model}`);
|
||||
} else {
|
||||
log.info("ROUTING", `Provider: ${provider}, Model: ${model}`);
|
||||
}
|
||||
|
||||
// Credential + fallback loop (mirrors handleChat)
|
||||
let excludeConnectionId = null;
|
||||
let lastError = null;
|
||||
let lastStatus = null;
|
||||
|
||||
while (true) {
|
||||
const credentials = await getProviderCredentials(provider, excludeConnectionId, model);
|
||||
|
||||
// All accounts unavailable
|
||||
if (!credentials || credentials.allRateLimited) {
|
||||
if (credentials?.allRateLimited) {
|
||||
const errorMsg = lastError || credentials.lastError || "Unavailable";
|
||||
const status = lastStatus || Number(credentials.lastErrorCode) || HTTP_STATUS.SERVICE_UNAVAILABLE;
|
||||
log.warn("EMBEDDINGS", `[${provider}/${model}] ${errorMsg} (${credentials.retryAfterHuman})`);
|
||||
return unavailableResponse(status, `[${provider}/${model}] ${errorMsg}`, credentials.retryAfter, credentials.retryAfterHuman);
|
||||
}
|
||||
if (!excludeConnectionId) {
|
||||
log.error("AUTH", `No credentials for provider: ${provider}`);
|
||||
return errorResponse(HTTP_STATUS.BAD_REQUEST, `No credentials for provider: ${provider}`);
|
||||
}
|
||||
log.warn("EMBEDDINGS", "No more accounts available", { provider });
|
||||
return errorResponse(lastStatus || HTTP_STATUS.SERVICE_UNAVAILABLE, lastError || "All accounts unavailable");
|
||||
}
|
||||
|
||||
const accountId = credentials.connectionId.slice(0, 8);
|
||||
log.info("AUTH", `Using ${provider} account: ${accountId}...`);
|
||||
|
||||
const refreshedCredentials = await checkAndRefreshToken(provider, credentials);
|
||||
|
||||
const result = await handleEmbeddingsCore({
|
||||
body: { ...body, model: `${provider}/${model}` },
|
||||
modelInfo: { provider, model },
|
||||
credentials: refreshedCredentials,
|
||||
log,
|
||||
onCredentialsRefreshed: async (newCreds) => {
|
||||
await updateProviderCredentials(credentials.connectionId, {
|
||||
accessToken: newCreds.accessToken,
|
||||
refreshToken: newCreds.refreshToken,
|
||||
providerSpecificData: newCreds.providerSpecificData,
|
||||
testStatus: "active"
|
||||
});
|
||||
},
|
||||
onRequestSuccess: async () => {
|
||||
await clearAccountError(credentials.connectionId, credentials);
|
||||
}
|
||||
});
|
||||
|
||||
if (result.success) return result.response;
|
||||
|
||||
const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, provider, model);
|
||||
|
||||
if (shouldFallback) {
|
||||
log.warn("AUTH", `Account ${accountId}... unavailable (${result.status}), trying fallback`);
|
||||
excludeConnectionId = credentials.connectionId;
|
||||
lastError = result.error;
|
||||
lastStatus = result.status;
|
||||
continue;
|
||||
}
|
||||
|
||||
return result.response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user