fitRecommendationRequest
import { fitRecommendationRequest } from '@rudra-js/a2ui/server';Call fitRecommendationRequest to drop a hinted SKU or category that none of your candidates has, before the hints reach core.
fitRecommendationRequest(request: RecommendationRequest, candidates: readonly Pick<Product, 'sku' | 'category'>[]) => RecommendationRequestReference
fitRecommendationRequest(request, candidates)
Parameters
request: The hints fromparseRecommendationRequest.candidates: The products you picked for this shopper. It reads onlyskuandcategory, so pass the same list you give core.
Returns
A new RecommendationRequest. It keeps currentSku only when a candidate has that SKU, and currentCategory only when a candidate is in that category. searchQuery and maxItems pass through as they are.
Caveats
- Pick the candidates in your own code, not from the hints. Then fit the hints to them.
- The match is exact. A hint of
socksis dropped when your category isSocks.
Usage
Keeping the hints to products you sell
Fit the hints after you pick the candidates, and spread what it returns into context.
import { createComponentGenerator, parseTrackingInput } from '@rudra-js/core';
import {
fitRecommendationRequest,
parseRecommendationRequest,
type RecommendationRequest,
type RecommendationResult,
} from '@rudra-js/a2ui/server';
import { candidatesFor, shopperFor } from './your-shop.js';
const generator = createComponentGenerator({ provider: null });
export async function POST(request: Request): Promise<Response> {
let asked: RecommendationRequest;
try {
asked = parseRecommendationRequest(await request.json());
} catch {
return new Response('Bad request', { status: 400 });
}
const shopper = await shopperFor(request);
const candidates = await candidatesFor(shopper);
const hints = fitRecommendationRequest(asked, candidates);
const input = parseTrackingInput({
user: { id: shopper.id },
context: { surface: 'assistant', locale: shopper.locale, ...hints },
candidates,
signals: shopper.signals,
});
const spec = await generator.generate(input);
const result: RecommendationResult = { spec, products: input.candidates, bundles: input.bundles };
return Response.json(result);
}When the agent hints at a SKU none of your candidates has, currentSku is left out. Core builds the block without it.