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 { createErrorResult, parseUpstreamError, formatProviderError } from "../utils/error.js";
|
feat(providers): self-hosted OpenAI-compatible STT, TTS and embedding providers
Add Self-hosted STT/TTS/Embedding providers that read baseUrl per connection
instead of a fixed registry endpoint, so 9Router can point at whisper.cpp,
faster-whisper, Kokoro-FastAPI, llama-server, vLLM, Infinity, and similar
OpenAI-compatible local servers.
Self-hosted Embedding refuses to run without a baseUrl rather than falling
back to api.openai.com like openaiCompatNode does, since that fallback would
silently send input text and the API key to OpenAI under a provider named
"Self-hosted". Also fixes embeddingsCore to catch adapter build errors as a
400 instead of letting them escape uncaught, and bounds the upstream fetch
with FETCH_CONNECT_TIMEOUT_MS to avoid hanging forever on a dead endpoint.
Self-hosted TTS treats a bare model value as the model rather than the voice,
since the generic OpenAI TTS convention (bare = voice) is backwards for a
provider where the model is the variable part.
2026-08-05 02:33:28 -04:00
|
|
|
import { HTTP_STATUS, FETCH_CONNECT_TIMEOUT_MS } from "../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 { getExecutor } from "../executors/index.js";
|
|
|
|
|
import { refreshWithRetry } from "../services/tokenRefresh.js";
|
2026-05-04 00:29:02 -04:00
|
|
|
import { getEmbeddingAdapter } from "./embeddingProviders/index.js";
|
2026-02-20 03:01:10 -05: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.
2026-02-18 01:24:02 -05:00
|
|
|
/**
|
2026-05-04 00:29:02 -04:00
|
|
|
* Core embeddings handler — orchestrator only. Provider-specific URL/headers/body/normalize
|
|
|
|
|
* live in `./embeddingProviders/{id}.js`.
|
2026-02-20 03:01:10 -05: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.
2026-02-18 01:24:02 -05:00
|
|
|
* @returns {Promise<{ success: boolean, response: Response, status?: number, error?: string }>}
|
|
|
|
|
*/
|
|
|
|
|
export async function handleEmbeddingsCore({
|
|
|
|
|
body,
|
|
|
|
|
modelInfo,
|
|
|
|
|
credentials,
|
|
|
|
|
log,
|
|
|
|
|
onCredentialsRefreshed,
|
2026-05-04 00:29:02 -04:00
|
|
|
onRequestSuccess,
|
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 { provider, model } = modelInfo;
|
|
|
|
|
|
|
|
|
|
// Validate input
|
|
|
|
|
const input = body.input;
|
|
|
|
|
if (!input) {
|
|
|
|
|
return createErrorResult(HTTP_STATUS.BAD_REQUEST, "Missing required field: input");
|
|
|
|
|
}
|
|
|
|
|
if (typeof input !== "string" && !Array.isArray(input)) {
|
|
|
|
|
return createErrorResult(HTTP_STATUS.BAD_REQUEST, "input must be a string or array of strings");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-04 00:29:02 -04:00
|
|
|
const adapter = getEmbeddingAdapter(provider);
|
|
|
|
|
if (!adapter) {
|
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 createErrorResult(
|
|
|
|
|
HTTP_STATUS.BAD_REQUEST,
|
2026-05-04 00:29:02 -04:00
|
|
|
`Provider '${provider}' does not support embeddings.`
|
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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-04 00:29:02 -04:00
|
|
|
const ctx = { input };
|
feat(providers): self-hosted OpenAI-compatible STT, TTS and embedding providers
Add Self-hosted STT/TTS/Embedding providers that read baseUrl per connection
instead of a fixed registry endpoint, so 9Router can point at whisper.cpp,
faster-whisper, Kokoro-FastAPI, llama-server, vLLM, Infinity, and similar
OpenAI-compatible local servers.
Self-hosted Embedding refuses to run without a baseUrl rather than falling
back to api.openai.com like openaiCompatNode does, since that fallback would
silently send input text and the API key to OpenAI under a provider named
"Self-hosted". Also fixes embeddingsCore to catch adapter build errors as a
400 instead of letting them escape uncaught, and bounds the upstream fetch
with FETCH_CONNECT_TIMEOUT_MS to avoid hanging forever on a dead endpoint.
Self-hosted TTS treats a bare model value as the model rather than the voice,
since the generic OpenAI TTS convention (bare = voice) is backwards for a
provider where the model is the variable part.
2026-08-05 02:33:28 -04:00
|
|
|
// buildUrl/buildHeaders/buildBody were called bare. An adapter that rejects a
|
|
|
|
|
// misconfigured connection — selfhosted-embedding throws when no baseUrl is set
|
|
|
|
|
// rather than silently falling back to api.openai.com — would have escaped this
|
|
|
|
|
// function uncaught, surfacing as a 500 or a request that never settles. A
|
|
|
|
|
// configuration mistake is a 400 with the reason in it.
|
|
|
|
|
let url, headers, requestBody;
|
|
|
|
|
try {
|
|
|
|
|
url = adapter.buildUrl(model, credentials, ctx);
|
|
|
|
|
headers = adapter.buildHeaders(credentials, ctx);
|
|
|
|
|
requestBody = adapter.buildBody(model, {
|
|
|
|
|
input,
|
|
|
|
|
encoding_format: body.encoding_format || "float",
|
|
|
|
|
dimensions: body.dimensions,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log?.debug?.("EMBEDDINGS", `Request build failed: ${error.message}`);
|
|
|
|
|
return createErrorResult(HTTP_STATUS.BAD_REQUEST, `[${provider}/${model}] ${error.message}`);
|
|
|
|
|
}
|
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?.debug?.("EMBEDDINGS", `${provider.toUpperCase()} | ${model} | input_type=${Array.isArray(input) ? `array[${input.length}]` : "string"}`);
|
|
|
|
|
|
|
|
|
|
let providerResponse;
|
|
|
|
|
try {
|
|
|
|
|
providerResponse = await fetch(url, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers,
|
2026-05-04 00:29:02 -04:00
|
|
|
body: JSON.stringify(requestBody),
|
feat(providers): self-hosted OpenAI-compatible STT, TTS and embedding providers
Add Self-hosted STT/TTS/Embedding providers that read baseUrl per connection
instead of a fixed registry endpoint, so 9Router can point at whisper.cpp,
faster-whisper, Kokoro-FastAPI, llama-server, vLLM, Infinity, and similar
OpenAI-compatible local servers.
Self-hosted Embedding refuses to run without a baseUrl rather than falling
back to api.openai.com like openaiCompatNode does, since that fallback would
silently send input text and the API key to OpenAI under a provider named
"Self-hosted". Also fixes embeddingsCore to catch adapter build errors as a
400 instead of letting them escape uncaught, and bounds the upstream fetch
with FETCH_CONNECT_TIMEOUT_MS to avoid hanging forever on a dead endpoint.
Self-hosted TTS treats a bare model value as the model rather than the voice,
since the generic OpenAI TTS convention (bare = voice) is backwards for a
provider where the model is the variable part.
2026-08-05 02:33:28 -04:00
|
|
|
...(typeof AbortSignal?.timeout === "function"
|
|
|
|
|
? { signal: AbortSignal.timeout(FETCH_CONNECT_TIMEOUT_MS) }
|
|
|
|
|
: {}),
|
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
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const errMsg = formatProviderError(error, provider, model, HTTP_STATUS.BAD_GATEWAY);
|
|
|
|
|
log?.debug?.("EMBEDDINGS", `Fetch error: ${errMsg}`);
|
|
|
|
|
return createErrorResult(HTTP_STATUS.BAD_GATEWAY, errMsg);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 23:14:50 -04:00
|
|
|
// Handle 401/403 — try token refresh (skip for noAuth providers)
|
|
|
|
|
const executor = getExecutor(provider);
|
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 (
|
2026-05-04 00:29:02 -04:00
|
|
|
!executor?.noAuth &&
|
2026-04-13 23:14:50 -04:00
|
|
|
(providerResponse.status === HTTP_STATUS.UNAUTHORIZED ||
|
2026-05-04 00:29:02 -04:00
|
|
|
providerResponse.status === HTTP_STATUS.FORBIDDEN)
|
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 newCredentials = await refreshWithRetry(
|
|
|
|
|
() => executor.refreshCredentials(credentials, log),
|
|
|
|
|
3,
|
|
|
|
|
log
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (newCredentials?.accessToken || newCredentials?.apiKey) {
|
|
|
|
|
log?.info?.("TOKEN", `${provider.toUpperCase()} | refreshed for embeddings`);
|
|
|
|
|
Object.assign(credentials, newCredentials);
|
2026-05-04 00:29:02 -04:00
|
|
|
if (onCredentialsRefreshed) await onCredentialsRefreshed(newCredentials);
|
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
|
|
|
|
|
|
|
|
try {
|
2026-05-04 00:29:02 -04:00
|
|
|
const retryHeaders = adapter.buildHeaders(credentials, ctx);
|
|
|
|
|
const retryUrl = adapter.buildUrl(model, credentials, ctx);
|
2026-02-20 03:01:10 -05:00
|
|
|
providerResponse = await fetch(retryUrl, {
|
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
|
|
|
method: "POST",
|
|
|
|
|
headers: retryHeaders,
|
2026-05-04 00:29:02 -04:00
|
|
|
body: JSON.stringify(requestBody),
|
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
|
|
|
});
|
2026-05-04 00:29:02 -04:00
|
|
|
} catch {
|
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?.warn?.("TOKEN", `${provider.toUpperCase()} | retry after refresh failed`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log?.warn?.("TOKEN", `${provider.toUpperCase()} | refresh failed`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!providerResponse.ok) {
|
2026-04-14 23:45:46 -04:00
|
|
|
const { statusCode, message } = await parseUpstreamError(providerResponse);
|
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 errMsg = formatProviderError(new Error(message), provider, model, statusCode);
|
|
|
|
|
log?.debug?.("EMBEDDINGS", `Provider error: ${errMsg}`);
|
|
|
|
|
return createErrorResult(statusCode, errMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let responseBody;
|
|
|
|
|
try {
|
|
|
|
|
responseBody = await providerResponse.json();
|
2026-05-04 00:29:02 -04:00
|
|
|
} catch {
|
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 createErrorResult(HTTP_STATUS.BAD_GATEWAY, `Invalid JSON response from ${provider}`);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-04 00:29:02 -04:00
|
|
|
if (onRequestSuccess) await onRequestSuccess();
|
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
|
|
|
|
2026-05-04 00:29:02 -04:00
|
|
|
const normalized = adapter.normalize(responseBody, 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
|
|
|
log?.debug?.("EMBEDDINGS", `Success | usage=${JSON.stringify(normalized.usage || {})}`);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
2026-07-23 05:06:16 -04:00
|
|
|
usage: normalized.usage || null,
|
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
|
|
|
response: new Response(JSON.stringify(normalized), {
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
2026-05-04 00:29:02 -04:00
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
|
},
|
|
|
|
|
}),
|
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
|
|
|
};
|
|
|
|
|
}
|