Open History to see the last commit
1import { createServerFn } from '@tanstack/react-start'
2import { rerank } from '@tanstack/ai'
3import { COHERE_RERANK_MODELS, cohereRerank } from '@tanstack/ai-cohere'
4import { openRouterRerank } from '@tanstack/ai-openrouter'
5import { SUPPORT_DOCS } from './documents'
6import { isProvider } from './models'
7import type { CohereRerankModel } from '@tanstack/ai-cohere'
8import type { Provider } from './models'
9
10interface RerankInput {
11 query: string
12 provider: Provider
13 model: string
14 topN: number
15}
16
17/**
18 * Narrows a wire string to a Cohere rerank model. The `cohereRerank` factory is
19 * generic over the model literal, so the model has to be a known slug before it
20 * reaches the adapter — a plain `string` would not type-check.
21 */
22function isCohereRerankModel(model: string): model is CohereRerankModel {
23 return COHERE_RERANK_MODELS.some((known) => known === model)
24}
25
26/**
27 * Reranks the support corpus against a query.
28 *
29 * The API key never leaves the server: both adapters read their key from the
30 * environment (`COHERE_API_KEY` / `OPENROUTER_API_KEY`) inside this handler.
31 *
32 * Note the two branches call the *same* `rerank()` with a different adapter —
33 * that is the provider-agnostic contract. They are written out separately
34 * rather than sharing an `adapter` variable so each call site keeps the
35 * adapter's literal model type, and with it the per-model `modelOptions`
36 * inference.
37 */
38export const rerankDocumentsFn = createServerFn({ method: 'POST' })
39 .inputValidator((data: RerankInput) => {
40 if (!data.query.trim()) throw new Error('Query is required')
41 if (!isProvider(data.provider)) {
42 throw new Error(`Unknown provider: ${data.provider}`)
43 }
44 if (!data.model) throw new Error('Model is required')
45 if (!Number.isInteger(data.topN) || data.topN < 1) {
46 throw new Error('topN must be a positive integer')
47 }
48 return data
49 })
50 .handler(async ({ data }) => {
51 const { query, model, topN } = data
52
53 if (data.provider === 'cohere') {
54 if (!isCohereRerankModel(model)) {
55 throw new Error(`Unknown Cohere rerank model: ${model}`)
56 }
57 return await rerank({
58 adapter: cohereRerank(model),
59 query,
60 // Object documents: JSON-serialized on the way out, and the original
61 // `SupportDoc` comes back on `ranking[n].document`.
62 documents: SUPPORT_DOCS,
63 topN,
64 // Per-model provider option, typed by the `cohereRerank(model)` literal.
65 modelOptions: { maxTokensPerDoc: 4096 },
66 })
67 }
68
69 return await rerank({
70 adapter: openRouterRerank(model),
71 query,
72 documents: SUPPORT_DOCS,
73 topN,
74 })
75 })