/**
* get_post tool handler
*/
import { ValidationError } from "../errors/index.js";
import { GetPostInputSchema } from "../schemas/index.js";
import {
buildCommentTree,
calculateNestingDepth,
countComments,
} from "../services/comment-service.js";
import { apiClient } from "../services/hn-api-client.js";
import type { GetPostInput } from "../types/index.js";
import { formatToolResponse } from "../utils/index.js";
/**
* Handle get_post tool call
*/
export async function handleGetPost(args: unknown) {
// Validate input
const parseResult = GetPostInputSchema.safeParse(args);
if (!parseResult.success) {
throw new ValidationError("Invalid post ID", parseResult.error.errors);
}
const input: GetPostInput = parseResult.data;
// Get post from API
const post = await apiClient.getPost(input.postId);
// Build comment tree (already nested from API)
const postWithTree = buildCommentTree(post);
// Calculate metadata
const totalComments = countComments(postWithTree);
const nestingDepth = calculateNestingDepth(postWithTree);
// Format response
const response = {
post: postWithTree,
metadata: {
totalComments,
nestingDepth,
hasComments: totalComments > 0,
},
remainingQuota: apiClient.getRemainingQuota(),
};
return formatToolResponse(response);
}