Skip to content

feat: add option to automatically create video chapters with AI - #11

Merged
its-gabo merged 2 commits into
mainfrom
gs_feat_add_option_to_autogenerate_chapters_with_ai
Nov 21, 2026
Merged

its-gabo merged 2 commits into
mainfrom
gs_feat_add_option_to_autogenerate_chapters_with_ai

Conversation

@its-gabo

Copy link
Copy Markdown
Owner

Summary

Members can have chapters suggested from a video's caption track. An organization's provider writes them with the model the owner chooses for it, gpt-6-luna by default. The suggestions fill the chapter editor as a draft, so nothing is saved until the member reviews and saves them.

Providers work like transcription providers. The owner connects OpenAI or an OpenAI-compatible server once per organization, picks its model, then chooses a provider per library. Both transcription and chapter generation providers can now have a monthly usage limit: minutes of audio for transcription, tokens for chapter generation.

What changed

Chapter generation (new chapter-generation API module)

  • Providers: chapter_generation_providers table, with create, update, delete and connection-test routes for owners.
    • Each provider has its own model. It defaults to gpt-6-luna and must support structured output.
    • API keys are stored encrypted and shown only as their last characters.
    • A provider that libraries still use can't be deleted.
  • Library setting: libraries.chapter_generation_provider_id, read and changed with GET/PUT /libraries/:libraryId/chapter-generation. Only the owner can do this.
  • Suggestions: POST /videos/:videoId/chapter-suggestions { captionId }. Any member can call it. It answers with draft chapters and saves nothing.
  • AI calls use the Vercel AI SDK with structured output (Output.object with a zod schema):
    • OpenAI goes through the Responses API (@ai-sdk/openai).
    • Compatible servers go through Chat Completions with structured outputs (@ai-sdk/openai-compatible).
    • A custom fetch reuses the transcription request helper. It refuses private addresses unless allowed, doesn't follow redirects, limits the answer's size and time, and passes abort signals through.
  • What the model reads: caption cues merged into timestamped lines, [123] text, with long transcripts shortened evenly.
  • Cleaning up the model's answer:
    • chapters past the end are dropped
    • each start moves to the nearest cue start
    • chapters are sorted and the first starts at 0:00
    • chapters are at least 10 seconds apart
    • titles are trimmed to the length limit
  • Timeout: CHAPTER_GENERATION_REQUEST_TIMEOUT_MS defaults to 100s and config refuses anything over 110s, so it always ends before the server's 120s idle timeout. When the member's connection closes, the provider request is cancelled.
  • Errors: a refused key, a refused request (e.g. an unknown model), an unreachable provider, an unusable answer, a reached limit and a missing provider each have their own error code.

Monthly usage limits

  • Transcription (monthly_minute_limit):
    • A request is refused when this month's minutes plus the video's length would go over the limit. An automatic transcription that would go over is recorded as failed with usage_limit_reached, and the failure webhook fires.
    • A new column, transcribed_audio_seconds, records the audio the provider answered for, chunk by chunk and across attempts. A failed transcription counts that amount. Any other transcription counts the larger of the video's length and that amount, so audio sent again on a retry is included.
  • Chapter generation (monthly_token_limit):
    • Each request reserves tokens in chapter_generation_usages before the provider is asked: its input, counted with gpt-tokenizer (o200k) plus 10% and 200 for overhead, and room for the answer. The answer gets up to 16,000 tokens, or whatever the limit leaves.
    • A request is refused when the limit leaves less than 1,000 tokens for the answer. maxOutputTokens is capped to the reserved room.
    • After the answer, the provider's own count replaces the reservation. If the provider gives no count, the reservation is kept. A failed request counts only the tokens the provider reported.
    • Each usage row records the model it was asked for.
    • Recording usage never fails a request that already got its answer. A reservation that was never settled stops counting 60 seconds after its request would have timed out.
  • Concurrency: reservations and checks run while the provider row is locked, so requests running at the same time can't pass the limit together.
  • Period: limits cover the calendar month in UTC.

Fixes in existing code

  • When a provider is updated, whether its address changed is now decided against the locked row, for both transcription and chapter generation providers. Before, two owners saving at once could send a saved API key to an address it wasn't saved for.
  • The shared request helper accepts any HTTP method and an abort signal. The abort is wired up by hand, because Bun's node:http ignores the signal option.

Web

  • A new Chapter generation organization page lists providers with their model and usage this month, and each library gets a Chapters tab to pick one.
  • The provider form has a Model field, prefilled with gpt-6-luna.
  • The transcription providers page shows this month's minutes against the limit.
  • Both provider forms have a limit field (empty means no limit) with a short explanation. A whole number can be grouped in threes; 1.5 is refused instead of being read as 15.
  • The chapter editor has a Suggest from captions button. Its dialog cancels the request when it closes, so a late answer can't overwrite rows the member edited.
  • Translations are added in English and Polish.

Database

  • One migration, 0015_add_chapter_generation_and_usage_limits, covering the new tables and columns, the activity actions and the failure reason.

@its-gabo its-gabo self-assigned this Nov 21, 2026
@its-gabo
its-gabo merged commit a6d9995 into main Nov 21, 2026
1 check passed
@its-gabo
its-gabo deleted the gs_feat_add_option_to_autogenerate_chapters_with_ai branch November 21, 2026 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant