Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(reranker): Voyage rerank-2.5 and rerank-2.5-lite through OpenRouter (#5020)
time-attack808608ac7949
diff --git a/src/core/ai/recipes/openrouter.ts b/src/core/ai/recipes/openrouter.tsindex 46f4e6a991..a57b7959e4 100644--- a/src/core/ai/recipes/openrouter.ts+++ b/src/core/ai/recipes/openrouter.ts@@ -210,8 +210,8 @@ export const openrouterCompatFetch = (async (* envelope, not every individual model's capability. When in doubt about a* specific model, check https://openrouter.ai/models.*- * Reranker: `/api/v1/rerank` proxies cross-encoder rerankers (Cohere v3.5/4-fast/4-pro- * and NVIDIA Nemotron VL). Wire shape matches `gateway.rerank()`:+ * Reranker: `/api/v1/rerank` proxies cross-encoder rerankers (Cohere v3.5/4-fast/4-pro,+ * NVIDIA Nemotron VL and Voyage rerank-2.5 / rerank-2.5-lite). Wire shape matches `gateway.rerank()`:* `{ query, documents, model }` → `{ results: [{ index, relevance_score }] }`.* Unlike embedding/chat, the reranker path strictly enforces the `models`* allowlist (no openai-compat bypass) — adding new rerank models requires a@@ -350,6 +350,9 @@ export const openrouter: Recipe = {'cohere/rerank-4-fast','cohere/rerank-4-pro','nvidia/llama-nemotron-rerank-vl-1b-v2:free',+ // Voyage bills per token; priced by full routed id in embedding-pricing.ts.+ 'voyageai/rerank-2.5-lite',+ 'voyageai/rerank-2.5',],default_model: 'cohere/rerank-v3.5',// Cohere bills per-search, not per-token. This is a pseudo-per-1M ratediff --git a/src/core/embedding-pricing.ts b/src/core/embedding-pricing.tsindex 961b58a6dd..fef88c8eb9 100644--- a/src/core/embedding-pricing.ts+++ b/src/core/embedding-pricing.ts@@ -53,6 +53,10 @@ export const EMBEDDING_PRICING: Record<string, EmbeddingPricing> = {'voyage:rerank-2.5-lite': { pricePerMTok: 0.02 },'voyage:rerank-3': { pricePerMTok: 0.05 },'voyage:rerank-3-lite': { pricePerMTok: 0.02 },+ // Voyage rerankers routed through OpenRouter, keyed by the full routed id+ // (https://openrouter.ai/voyageai/rerank-2.5 and /rerank-2.5-lite, verified 2026-10-06).+ 'openrouter:voyageai/rerank-2.5': { pricePerMTok: 0.05 },+ 'openrouter:voyageai/rerank-2.5-lite': { pricePerMTok: 0.02 },// voyage-4-nano is deliberately absent: it's the open-weight variant (see// src/core/ai/recipes/voyage.ts) and Voyage's pricing page lists no hosted// rate for it. A 0 entry would under-estimate anyone paying for it via thediff --git a/test/ai/rerank.test.ts b/test/ai/rerank.test.tsindex 376698a8ce..0bf9ce15f4 100644--- a/test/ai/rerank.test.ts+++ b/test/ai/rerank.test.ts@@ -23,13 +23,18 @@*/import { describe, test, expect, afterEach, beforeEach } from 'bun:test';+import { mkdtempSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';import {configureGateway,resetGateway,rerank,RerankError,+ withBudgetTracker,__setRerankTransportForTests,} from '../../src/core/ai/gateway.ts';+import { BudgetExhausted, BudgetTracker } from '../../src/core/budget/budget-tracker.ts';function configureVoyage(model: string = 'voyage:rerank-2.5'): void {configureGateway({@@ -563,3 +568,45 @@ describe('gateway.rerank() — v0.46.3 Voyage wire dialect', () => {expect(called).toBe(false);});});++describe('gateway.rerank() — Voyage rerank-2.5 routes through OpenRouter (#3657)', () => {+ const docs = ['alpha '.repeat(200), 'beta '.repeat(200)];+ let auditDir: string;++ function configureOpenRouter(model: string): void {+ configureGateway({ reranker_model: model, env: { OPENROUTER_API_KEY: 'sk-or-test' } });+ }++ beforeEach(() => { auditDir = mkdtempSync(join(tmpdir(), 'gb-rerank-budget-')); });++ for (const [model, pricePerMTok] of [['voyageai/rerank-2.5-lite', 0.02], ['voyageai/rerank-2.5', 0.05]] as const) {+ test(`openrouter:${model} is allowlisted, posts the routed id, and reserves and records at its exact rate under a cap`, async () => {+ configureOpenRouter(`openrouter:${model}`);+ let sentModel = '';+ let capturedUrl = '';+ __setRerankTransportForTests(async (url, init) => {+ capturedUrl = url;+ sentModel = JSON.parse(String(init.body)).model;+ return mockResp({ results: [{ index: 1, relevance_score: 0.8 }, { index: 0, relevance_score: 0.3 }] });+ });+ const tracker = new BudgetTracker({ maxCostUsd: 1, label: 'rerank-test', auditPath: join(auditDir, 'audit.jsonl') });+ const results = await withBudgetTracker(tracker, () => rerank({ query: 'q', documents: docs }));+ expect(results.map(r => r.index)).toEqual([1, 0]);+ expect(capturedUrl).toBe('https://openrouter.ai/api/v1/rerank');+ expect(sentModel).toBe(model);+ const chars = 1 + docs.reduce((s, d) => s + d.length, 0);+ expect(tracker.totalSpent).toBeCloseTo((Math.ceil(chars / 4) * pricePerMTok) / 1_000_000, 12);+ });+ }++ test('a cap smaller than the projected cost refuses before any request', async () => {+ configureOpenRouter('openrouter:voyageai/rerank-2.5');+ let calls = 0;+ __setRerankTransportForTests(async () => { calls++; return mockResp({ results: [] }); });+ const tracker = new BudgetTracker({ maxCostUsd: 1e-9, label: 'rerank-test', auditPath: join(auditDir, 'audit.jsonl') });+ const err = await withBudgetTracker(tracker, () => rerank({ query: 'q', documents: docs })).catch((e: unknown) => e);+ expect(err).toBeInstanceOf(BudgetExhausted);+ expect((err as BudgetExhausted).reason).toBe('cost');+ expect(calls).toBe(0);+ });+});diff --git a/test/openrouter-reranker-recipe.test.ts b/test/openrouter-reranker-recipe.test.tsindex 8f333ba80b..9c92273e7b 100644--- a/test/openrouter-reranker-recipe.test.ts+++ b/test/openrouter-reranker-recipe.test.ts@@ -1,5 +1,6 @@import { describe, test, expect } from 'bun:test';import { getRecipe } from '../src/core/ai/recipes/index.ts';+import { isModelPriceable } from '../src/core/budget/reservation-cost.ts';describe('OpenRouter recipe — reranker touchpoint', () => {test('declares a reranker touchpoint', () => {@@ -19,6 +20,15 @@ describe('OpenRouter recipe — reranker touchpoint', () => {expect(m).toContain('nvidia/llama-nemotron-rerank-vl-1b-v2:free');});+ test('Voyage rerank-2.5 routes are allowlisted and priced by full routed id (not unpriced)', () => {+ const r = getRecipe('openrouter')!;+ for (const model of ['voyageai/rerank-2.5', 'voyageai/rerank-2.5-lite']) {+ expect(r.touchpoints.reranker!.models).toContain(model);+ expect(isModelPriceable(`openrouter:${model}`, 'rerank')).toBe(true);+ expect(r.unpriced_models ?? []).not.toContain(model);+ }+ });+test('default_model is cohere/rerank-v3.5', () => {const tp = getRecipe('openrouter')!.touchpoints.reranker!;expect(tp.default_model).toBe('cohere/rerank-v3.5');