mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-11 07:41:08 +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' });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user