mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-12 00:01:09 +00:00
Add Health/Recommendations/Request/Webhook controllers
This commit is contained in:
17
src/app/controllers/HealthController.ts
Normal file
17
src/app/controllers/HealthController.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import type { FastifyInstance } from 'fastify';
|
||||||
|
import { Controller } from '../../core/server/Controller';
|
||||||
|
import type { AppConfig } from '../config/AppConfig';
|
||||||
|
|
||||||
|
export class HealthController extends Controller {
|
||||||
|
constructor(private readonly config: AppConfig) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
register(app: FastifyInstance): void {
|
||||||
|
app.get('/health', async () => ({
|
||||||
|
status: 'ok',
|
||||||
|
llm: this.config.LLM_ENABLED,
|
||||||
|
radarr: this.config.RADARR_ENABLED,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
81
src/app/controllers/RecommendationsController.ts
Normal file
81
src/app/controllers/RecommendationsController.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||||
|
import { Controller } from '../../core/server/Controller';
|
||||||
|
import type { AppConfig } from '../config/AppConfig';
|
||||||
|
import type { RecommendationPipeline } from '../pipelines/RecommendationPipeline';
|
||||||
|
|
||||||
|
interface ListQuery {
|
||||||
|
count?: string;
|
||||||
|
force_llm?: string;
|
||||||
|
force_tmdb?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SearchBody {
|
||||||
|
query?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RecommendationsController extends Controller {
|
||||||
|
constructor(
|
||||||
|
private readonly config: AppConfig,
|
||||||
|
private readonly pipeline: RecommendationPipeline
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
register(app: FastifyInstance): void {
|
||||||
|
app.get('/recommendations', (request, reply) => this.handleList(request as FastifyRequest<{ Querystring: ListQuery }>, reply));
|
||||||
|
app.post('/recommendations/search', (request, reply) =>
|
||||||
|
this.handleSearch(request as FastifyRequest<{ Body: SearchBody }>, reply)
|
||||||
|
);
|
||||||
|
app.post('/recommendations/trigger', (request, reply) => this.handleTrigger(request, reply));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleList(request: FastifyRequest<{ Querystring: ListQuery }>, reply: FastifyReply) {
|
||||||
|
const count = request.query.count ? Number(request.query.count) : this.config.RECOMMEND_COUNT;
|
||||||
|
const useLlm =
|
||||||
|
request.query.force_tmdb === 'true'
|
||||||
|
? false
|
||||||
|
: request.query.force_llm === 'true'
|
||||||
|
? true
|
||||||
|
: this.config.LLM_ENABLED;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const recommendations = await this.pipeline.runAsync({ count, useLlm });
|
||||||
|
reply.send(recommendations);
|
||||||
|
} catch (err) {
|
||||||
|
reply.code(500).send({ error: String(err) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleTrigger(_request: FastifyRequest, reply: FastifyReply) {
|
||||||
|
try {
|
||||||
|
const recommendations = await this.pipeline.runAsync({
|
||||||
|
count: this.config.RECOMMEND_COUNT,
|
||||||
|
useLlm: this.config.LLM_ENABLED,
|
||||||
|
});
|
||||||
|
if (this.config.RADARR_ENABLED) {
|
||||||
|
await this.pipeline.autoRequest(recommendations);
|
||||||
|
}
|
||||||
|
reply.send(recommendations);
|
||||||
|
} catch (err) {
|
||||||
|
reply.code(500).send({ error: String(err) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleSearch(request: FastifyRequest<{ Body: SearchBody }>, reply: FastifyReply) {
|
||||||
|
if (!this.config.LLM_ENABLED) {
|
||||||
|
reply.code(400).send({ error: 'LLM is not enabled' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const query = request.body?.query;
|
||||||
|
if (!query) {
|
||||||
|
reply.code(400).send({ error: 'body must be {"query": "..."}' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const recommendations = await this.pipeline.searchAsync(query, this.config.RECOMMEND_COUNT);
|
||||||
|
reply.send(recommendations);
|
||||||
|
} catch (err) {
|
||||||
|
reply.code(500).send({ error: String(err) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
42
src/app/controllers/RequestController.ts
Normal file
42
src/app/controllers/RequestController.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||||
|
import { Controller } from '../../core/server/Controller';
|
||||||
|
import type { AppConfig } from '../config/AppConfig';
|
||||||
|
import type { RadarrClient } from '../clients/radarr/RadarrClient';
|
||||||
|
|
||||||
|
interface RequestBody {
|
||||||
|
tmdb_id?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RequestController extends Controller {
|
||||||
|
constructor(
|
||||||
|
private readonly config: AppConfig,
|
||||||
|
private readonly radarr: RadarrClient | undefined
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
register(app: FastifyInstance): void {
|
||||||
|
app.post('/recommendations/request', (request, reply) =>
|
||||||
|
this.handle(request as FastifyRequest<{ Body: RequestBody }>, reply)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handle(request: FastifyRequest<{ Body: RequestBody }>, reply: FastifyReply) {
|
||||||
|
if (!this.config.RADARR_ENABLED || !this.radarr) {
|
||||||
|
reply.code(400).send({ error: 'Radarr is not enabled' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const tmdbId = request.body?.tmdb_id;
|
||||||
|
if (!tmdbId) {
|
||||||
|
reply.code(400).send({ error: 'body must be {"tmdb_id": 12345}' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const movie = await this.radarr.lookup(tmdbId);
|
||||||
|
await this.radarr.addMovie(movie);
|
||||||
|
reply.send({ requested: true, title: movie.title });
|
||||||
|
} catch (err) {
|
||||||
|
reply.code(500).send({ error: String(err) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/app/controllers/WebhookController.ts
Normal file
25
src/app/controllers/WebhookController.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||||
|
import { Controller } from '../../core/server/Controller';
|
||||||
|
import type { PromptService } from '../../core/langfuse/PromptService';
|
||||||
|
|
||||||
|
interface InvalidateBody {
|
||||||
|
promptName?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WebhookController extends Controller {
|
||||||
|
constructor(private readonly promptService: PromptService) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
register(app: FastifyInstance): void {
|
||||||
|
app.post('/webhooks/langfuse/invalidate', (request, reply) =>
|
||||||
|
this.handle(request as FastifyRequest<{ Body: InvalidateBody }>, reply)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handle(request: FastifyRequest<{ Body: InvalidateBody }>, reply: FastifyReply) {
|
||||||
|
const promptName = request.body?.promptName;
|
||||||
|
this.promptService.invalidate(promptName);
|
||||||
|
reply.send({ invalidated: true, promptName: promptName ?? 'all' });
|
||||||
|
}
|
||||||
|
}
|
||||||
15
test/app/controllers/HealthController.test.ts
Normal file
15
test/app/controllers/HealthController.test.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import Fastify from 'fastify';
|
||||||
|
import { HealthController } from '../../../src/app/controllers/HealthController';
|
||||||
|
import type { AppConfig } from '../../../src/app/config/AppConfig';
|
||||||
|
|
||||||
|
describe('HealthController', () => {
|
||||||
|
it('reports status ok with llm/radarr flags from config', async () => {
|
||||||
|
const app = Fastify();
|
||||||
|
new HealthController({ LLM_ENABLED: true, RADARR_ENABLED: false } as AppConfig).register(app);
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'GET', url: '/health' });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.json()).toEqual({ status: 'ok', llm: true, radarr: false });
|
||||||
|
});
|
||||||
|
});
|
||||||
66
test/app/controllers/RecommendationsController.test.ts
Normal file
66
test/app/controllers/RecommendationsController.test.ts
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
|
import Fastify from 'fastify';
|
||||||
|
import { RecommendationsController } from '../../../src/app/controllers/RecommendationsController';
|
||||||
|
import type { AppConfig } from '../../../src/app/config/AppConfig';
|
||||||
|
import type { RecommendationPipeline } from '../../../src/app/pipelines/RecommendationPipeline';
|
||||||
|
|
||||||
|
function makeApp(config: Partial<AppConfig>, pipeline: Partial<RecommendationPipeline>) {
|
||||||
|
const app = Fastify();
|
||||||
|
new RecommendationsController(config as AppConfig, pipeline as RecommendationPipeline).register(app);
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('RecommendationsController', () => {
|
||||||
|
it('GET /recommendations runs the pipeline with the configured count and LLM flag', async () => {
|
||||||
|
const runAsync = vi.fn().mockResolvedValue([{ title: 'Dune', year: 2021, tmdbId: 1, reason: 'r' }]);
|
||||||
|
const app = makeApp({ RECOMMEND_COUNT: 10, LLM_ENABLED: true }, { runAsync } as unknown as RecommendationPipeline);
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'GET', url: '/recommendations' });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(runAsync).toHaveBeenCalledWith({ count: 10, useLlm: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('GET /recommendations?count= and force_llm/force_tmdb override config', async () => {
|
||||||
|
const runAsync = vi.fn().mockResolvedValue([]);
|
||||||
|
const app = makeApp({ RECOMMEND_COUNT: 10, LLM_ENABLED: false }, { runAsync } as unknown as RecommendationPipeline);
|
||||||
|
|
||||||
|
await app.inject({ method: 'GET', url: '/recommendations?count=3&force_llm=true' });
|
||||||
|
expect(runAsync).toHaveBeenCalledWith({ count: 3, useLlm: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /recommendations/trigger auto-requests via Radarr when enabled', async () => {
|
||||||
|
const recs = [{ title: 'Dune', year: 2021, tmdbId: 1, reason: 'r' }];
|
||||||
|
const runAsync = vi.fn().mockResolvedValue(recs);
|
||||||
|
const autoRequest = vi.fn().mockResolvedValue(undefined);
|
||||||
|
const app = makeApp(
|
||||||
|
{ RECOMMEND_COUNT: 10, LLM_ENABLED: true, RADARR_ENABLED: true },
|
||||||
|
{ runAsync, autoRequest } as unknown as RecommendationPipeline
|
||||||
|
);
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/recommendations/trigger' });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(autoRequest).toHaveBeenCalledWith(recs);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /recommendations/search requires LLM to be enabled and a query body', async () => {
|
||||||
|
const app = makeApp({ LLM_ENABLED: false }, {} as RecommendationPipeline);
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/recommendations/search', payload: { query: 'x' } });
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /recommendations/search runs pipeline.searchAsync and returns results', async () => {
|
||||||
|
const searchAsync = vi.fn().mockResolvedValue([{ title: 'Dune', year: 2021, tmdbId: 1, reason: 'r' }]);
|
||||||
|
const app = makeApp(
|
||||||
|
{ LLM_ENABLED: true, RECOMMEND_COUNT: 10 },
|
||||||
|
{ searchAsync } as unknown as RecommendationPipeline
|
||||||
|
);
|
||||||
|
|
||||||
|
const res = await app.inject({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/recommendations/search',
|
||||||
|
payload: { query: 'sci-fi like Blade Runner' },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(searchAsync).toHaveBeenCalledWith('sci-fi like Blade Runner', 10);
|
||||||
|
});
|
||||||
|
});
|
||||||
36
test/app/controllers/RequestController.test.ts
Normal file
36
test/app/controllers/RequestController.test.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
|
import Fastify from 'fastify';
|
||||||
|
import { RequestController } from '../../../src/app/controllers/RequestController';
|
||||||
|
import type { AppConfig } from '../../../src/app/config/AppConfig';
|
||||||
|
import type { RadarrClient } from '../../../src/app/clients/radarr/RadarrClient';
|
||||||
|
|
||||||
|
describe('RequestController', () => {
|
||||||
|
it('returns 400 when Radarr is not enabled', async () => {
|
||||||
|
const app = Fastify();
|
||||||
|
new RequestController({ RADARR_ENABLED: false } as AppConfig, undefined).register(app);
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/recommendations/request', payload: { tmdb_id: 1 } });
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 400 when tmdb_id is missing', async () => {
|
||||||
|
const app = Fastify();
|
||||||
|
new RequestController({ RADARR_ENABLED: true } as AppConfig, {} as RadarrClient).register(app);
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/recommendations/request', payload: {} });
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('looks up and adds the movie, returning its title', async () => {
|
||||||
|
const radarr = {
|
||||||
|
lookup: vi.fn().mockResolvedValue({ title: 'Dune', tmdbId: 1 }),
|
||||||
|
addMovie: vi.fn().mockResolvedValue(undefined),
|
||||||
|
} as unknown as RadarrClient;
|
||||||
|
const app = Fastify();
|
||||||
|
new RequestController({ RADARR_ENABLED: true } as AppConfig, radarr).register(app);
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/recommendations/request', payload: { tmdb_id: 1 } });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.json()).toEqual({ requested: true, title: 'Dune' });
|
||||||
|
});
|
||||||
|
});
|
||||||
31
test/app/controllers/WebhookController.test.ts
Normal file
31
test/app/controllers/WebhookController.test.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
|
import Fastify from 'fastify';
|
||||||
|
import { WebhookController } from '../../../src/app/controllers/WebhookController';
|
||||||
|
import type { PromptService } from '../../../src/core/langfuse/PromptService';
|
||||||
|
|
||||||
|
describe('WebhookController', () => {
|
||||||
|
it('invalidates a single prompt when promptName is given', async () => {
|
||||||
|
const invalidate = vi.fn();
|
||||||
|
const app = Fastify();
|
||||||
|
new WebhookController({ invalidate } as unknown as PromptService).register(app);
|
||||||
|
|
||||||
|
const res = await app.inject({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/webhooks/langfuse/invalidate',
|
||||||
|
payload: { promptName: 'movie-recommender' },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(invalidate).toHaveBeenCalledWith('movie-recommender');
|
||||||
|
expect(res.json()).toEqual({ invalidated: true, promptName: 'movie-recommender' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('invalidates the whole cache when no promptName is given', async () => {
|
||||||
|
const invalidate = vi.fn();
|
||||||
|
const app = Fastify();
|
||||||
|
new WebhookController({ invalidate } as unknown as PromptService).register(app);
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'POST', url: '/webhooks/langfuse/invalidate', payload: {} });
|
||||||
|
expect(invalidate).toHaveBeenCalledWith(undefined);
|
||||||
|
expect(res.json()).toEqual({ invalidated: true, promptName: 'all' });
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user