Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

3.8 KB
Raw Blame
/**
 * Brain YouTube Ingest Routes
 *
 * Mounted at /api/brain/youtube. Kickoff returns a jobId immediately and the
 * work streams over SSE (same job/SSE shape as the track import and the Dev
 * Tools video downloader), so a 40-minute talk doesn't hold an HTTP request
 * open.
 */

import { Router } from 'express';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
import { youtubeIngestSchema, youtubeIngestSettingsSchema, youtubeIngestsQuerySchema } from '../lib/brainValidation.js';
import * as ingest from '../services/youtubeIngest.js';

const router = Router();

/**
 * POST /api/brain/youtube/ingest
 * Start an ingest. Body: { url, captureTranscript?, downloadVideo?, ingestAudio?,
 * note?, agentPrompt?, tags?, priority? } → { jobId, videoId }
 */
router.post('/ingest', asyncHandler(async (req, res) => {
  const data = validateRequest(youtubeIngestSchema, req.body || {});
  const result = await ingest.startYoutubeIngest(data);
  res.json(result);
}));

/** GET /api/brain/youtube/ingest/:jobId/events — SSE progress stream. */
router.get('/ingest/:jobId/events', asyncHandler(async (req, res) => {
  // Throw before SSE headers are written so the standard error envelope applies.
  if (!ingest.attachIngestSseClient(req.params.jobId, res)) {
    throw new ServerError('Ingest job not found', { status: 404, code: 'NOT_FOUND' });
  }
}));

/** POST /api/brain/youtube/ingest/:jobId/cancel */
router.post('/ingest/:jobId/cancel', asyncHandler(async (req, res) => {
  const canceled = ingest.cancelYoutubeIngest(req.params.jobId);
  if (!canceled) throw new ServerError('Ingest job not found or already finished', { status: 404, code: 'NOT_FOUND' });
  res.json({ message: 'Cancellation requested' });
}));

/**
 * GET /api/brain/youtube/ingests — every ingest, newest first. Query-less
 * callers keep the legacy `{ ingests }` full-history array; passing `limit`
 * opts into a bounded, cursor-continuable page (#8267).
 */
router.get('/ingests', asyncHandler(async (req, res) => {
  const { limit, cursor } = validateRequest(youtubeIngestsQuerySchema, req.query);
  if (limit === undefined && cursor === undefined) {
    res.json({ ingests: await ingest.listIngests() });
    return;
  }
  const { items, nextCursor } = await ingest.listIngests({ limit: limit ?? 50, cursor });
  res.json({ ingests: items, ...(nextCursor ? { nextCursor } : {}) });
}));

/** GET /api/brain/youtube/ingests/:videoId */
router.get('/ingests/:videoId', asyncHandler(async (req, res) => {
  const record = await ingest.getIngest(req.params.videoId);
  if (!record) throw new ServerError('Ingest not found', { status: 404, code: 'NOT_FOUND' });
  res.json(record);
}));

/** GET /api/brain/youtube/ingests/:videoId/transcript — the stored markdown. */
router.get('/ingests/:videoId/transcript', asyncHandler(async (req, res) => {
  const markdown = await ingest.getTranscript(req.params.videoId);
  if (markdown === null) {
    throw new ServerError('No transcript is stored for this video', { status: 404, code: 'TRANSCRIPT_NOT_FOUND' });
  }
  res.type('text/markdown').send(markdown);
}));

/** DELETE /api/brain/youtube/ingests/:videoId — drops local files + the note. */
router.delete('/ingests/:videoId', asyncHandler(async (req, res) => {
  const removed = await ingest.deleteIngest(req.params.videoId);
  if (!removed) throw new ServerError('Ingest not found', { status: 404, code: 'NOT_FOUND' });
  res.json({ message: 'Ingest removed' });
}));

router.get('/settings', asyncHandler(async (req, res) => {
  res.json(await ingest.getSettings());
}));

router.put('/settings', asyncHandler(async (req, res) => {
  const data = validateRequest(youtubeIngestSettingsSchema, req.body || {});
  res.json(await ingest.updateSettings(data));
}));

export default router;