import { z } from "zod";
import { api } from "../services/api.js";
const schema = z.object({
market_slugs: z
.array(z.string())
.min(2)
.max(10)
.describe("List of market slugs to compare (2-10 markets)"),
});
export const compareMarketsTool = {
name: "compare_markets",
description:
"Compare multiple markets by slug. Source: slugs from search_markets/list_markets. Returns market data or error per slug. Example: market_slugs=[a,b].",
parameters: schema,
execute: async (args: z.infer<typeof schema>) => {
try {
const results = await Promise.allSettled(
args.market_slugs.map((slug) => api.getMarketBySlug(slug)),
);
const markets = results.map((r, i) => {
if (r.status === "fulfilled") return r.value;
return { slug: args.market_slugs[i], error: r.reason?.message ?? "Failed to fetch" };
});
return JSON.stringify(markets, null, 2);
} catch (error) {
return JSON.stringify({ error: error instanceof Error ? error.message : String(error) });
}
},
};