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.
2026-02-18 01:24:02 -05:00
|
|
|
import {
|
|
|
|
|
getProviderCredentials,
|
|
|
|
|
markAccountUnavailable,
|
|
|
|
|
clearAccountError,
|
|
|
|
|
extractApiKey,
|
|
|
|
|
isValidApiKey,
|
|
|
|
|
} from "../services/auth.js";
|
2026-02-18 01:46:14 -05:00
|
|
|
import { getSettings } from "@/lib/localDb";
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
import { getModelInfo } from "../services/model.js";
|
|
|
|
|
import { handleEmbeddingsCore } from "open-sse/handlers/embeddingsCore.js";
|
|
|
|
|
import { errorResponse, unavailableResponse } from "open-sse/utils/error.js";
|
2026-03-12 05:20:46 -04:00
|
|
|
import { HTTP_STATUS } from "open-sse/config/runtimeConfig.js";
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
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)");
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 01:46:14 -05:00
|
|
|
// Enforce API key if enabled in settings
|
|
|
|
|
const settings = await getSettings();
|
|
|
|
|
if (settings.requireApiKey) {
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
if (!apiKey) {
|
2026-02-18 01:46:14 -05:00
|
|
|
log.warn("AUTH", "Missing API key (requireApiKey=true)");
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Missing API key");
|
|
|
|
|
}
|
|
|
|
|
const valid = await isValidApiKey(apiKey);
|
|
|
|
|
if (!valid) {
|
2026-02-18 01:46:14 -05:00
|
|
|
log.warn("AUTH", "Invalid API key (requireApiKey=true)");
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
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)
|
2026-03-13 22:37:29 -04:00
|
|
|
const excludeConnectionIds = new Set();
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
let lastError = null;
|
|
|
|
|
let lastStatus = null;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
2026-03-13 22:37:29 -04:00
|
|
|
const credentials = await getProviderCredentials(provider, excludeConnectionIds, model);
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
2026-03-13 22:37:29 -04:00
|
|
|
if (excludeConnectionIds.size === 0) {
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 07:04:38 -04:00
|
|
|
log.info("AUTH", `\x1b[32mUsing ${provider} account: ${credentials.connectionName}\x1b[0m`);
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
|
|
|
|
|
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 () => {
|
2026-02-27 22:04:57 -05:00
|
|
|
await clearAccountError(credentials.connectionId, credentials, model);
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (result.success) return result.response;
|
|
|
|
|
|
|
|
|
|
const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, provider, model);
|
|
|
|
|
|
|
|
|
|
if (shouldFallback) {
|
2026-03-11 07:04:38 -04:00
|
|
|
log.warn("AUTH", `Account ${credentials.connectionName} unavailable (${result.status}), trying fallback`);
|
2026-03-13 22:37:29 -04:00
|
|
|
excludeConnectionIds.add(credentials.connectionId);
|
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.
2026-02-18 01:24:02 -05:00
|
|
|
lastError = result.error;
|
|
|
|
|
lastStatus = result.status;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.response;
|
|
|
|
|
}
|
|
|
|
|
}
|