/**
* get_front_page tool handler
*/
import { ValidationError } from "../errors/index.js";
import { GetFrontPageInputSchema } from "../schemas/index.js";
import { apiClient } from "../services/hn-api-client.js";
import type { GetFrontPageInput } from "../types/index.js";
import { formatToolResponse } from "../utils/index.js";
/**
* Handle get_front_page tool call
*/
export async function handleGetFrontPage(args: unknown) {
// Validate input
const parseResult = GetFrontPageInputSchema.safeParse(args);
if (!parseResult.success) {
throw new ValidationError("Invalid front page parameters", parseResult.error.errors);
}
const input: GetFrontPageInput = parseResult.data;
// Search with front_page tag
const result = await apiClient.search({
tags: "(front_page)",
page: input.page,
hitsPerPage: input.hitsPerPage,
});
// Format response
const response = {
results: result.hits,
pagination: {
totalResults: result.nbHits,
currentPage: result.page,
totalPages: result.nbPages,
resultsPerPage: result.hitsPerPage,
},
processingTimeMS: result.processingTimeMS,
remainingQuota: apiClient.getRemainingQuota(),
};
return formatToolResponse(response);
}