import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; import type { LLMAnalyst } from '../services/LLMAnalyst'; import { CatalystAnalyst } from '../services/CatalystAnalyst'; import { analyzeSchema } from '../types/schemas'; export class AnalyzeController { constructor( private readonly catalyst: CatalystAnalyst, private readonly llm: LLMAnalyst, ) {} register(app: FastifyInstance): void { app.post('/api/analyze', { schema: analyzeSchema }, this.analyze.bind(this)); } private async analyze(req: FastifyRequest, reply: FastifyReply) { if (!this.llm.isAvailable) { return reply.code(400).send({ error: 'ANTHROPIC_API_KEY is not set in .env' }); } const tickers = (req.body as { tickers: string[] }).tickers.map((t) => t.toUpperCase()); const stories = await this.catalyst.fetchStoriesForTickers(tickers); if (!stories.length) return reply.code(200).send({ analysis: null, reason: 'no_stories' }); const { tickerFrequency } = CatalystAnalyst.rankTickers(stories); const analysis = await this.llm.analyze(stories, tickers, tickerFrequency); return { analysis }; } }