feat(usecase): read stored transcript before fetching (ADR-021)
The engine now resolves transcripts store-first: a stored transcript — including a stored SourceNone — is summarized without touching YouTube, so re-analysis never re-fetches. On a miss it fetches through the source (caption call still gated, ADR-014) and persists the terminal outcome for the next analysis by any user. A transient SourceRateLimited is surfaced to the runner for per-user backoff but never cached, so persistence can never mask a 429 as a permanent "no transcript". The TranscriptStore is optional (nil → fetch every time), keeping the pure-core and scaffold wiring valid. cmd/tapir wires the store as both summary sink and transcript cache, so `tapir run` and the web summarize path (incl. paste + onboarding) all share the dedup. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,13 @@ type Engine struct {
|
||||
AI ports.Summarizer
|
||||
Sinks []ports.Sink
|
||||
|
||||
// Transcripts, when set, is the shared transcript cache (ADR-021): the engine
|
||||
// reads it before any caption fetch and writes resolved transcripts back, so
|
||||
// re-analysis — the same user re-summarizing, or a second user with the same
|
||||
// video — never re-touches YouTube (ADR-010/014). Optional: nil disables
|
||||
// persistence (fetch every time), keeping the pure-core/scaffold wiring valid.
|
||||
Transcripts ports.TranscriptStore
|
||||
|
||||
// processed dedups videos within this engine's lifetime so a video is not
|
||||
// summarized twice when the watcher sees it again. Durable cross-restart
|
||||
// dedup is the store's concern (a resolved TRANSCRIPT / existing SUMMARY,
|
||||
@@ -57,9 +64,9 @@ type ProcessResult struct {
|
||||
// resolve transcript -> (summarize -> deliver) | skip.
|
||||
// See docs/use-cases/summarize_new_video.feature.
|
||||
func (e *Engine) ProcessNewVideo(ctx context.Context, v domain.Video) (ProcessResult, error) {
|
||||
t, err := e.Source.FetchTranscript(ctx, v)
|
||||
t, err := e.resolveTranscript(ctx, v)
|
||||
if err != nil {
|
||||
return ProcessResult{Video: v}, fmt.Errorf("fetch transcript: %w", err)
|
||||
return ProcessResult{Video: v}, err
|
||||
}
|
||||
if !t.HasText() {
|
||||
// No usable transcript: record the skip, produce no summary, deliver nothing
|
||||
@@ -86,6 +93,39 @@ func (e *Engine) ProcessNewVideo(ctx context.Context, v domain.Video) (ProcessRe
|
||||
return ProcessResult{Video: v, Summary: &sum, TranscriptSource: string(t.Source)}, errors.Join(errs...)
|
||||
}
|
||||
|
||||
// resolveTranscript returns v's transcript, reading the shared store first
|
||||
// (ADR-021): a stored transcript — including a stored SourceNone (captions
|
||||
// permanently absent) — is returned without touching YouTube, so re-analysis
|
||||
// never re-fetches. On a store miss it fetches through the source (which gates
|
||||
// the caption call, ADR-014) and persists the terminal outcome so the next
|
||||
// analysis, for any user, reads from the store. A transient SourceRateLimited is
|
||||
// returned to the caller (the runner stamps a per-user backoff) but never stored,
|
||||
// so persistence can never mask a 429 as a permanent "no transcript". When no
|
||||
// TranscriptStore is wired the engine simply fetches every time.
|
||||
func (e *Engine) resolveTranscript(ctx context.Context, v domain.Video) (domain.Transcript, error) {
|
||||
if e.Transcripts != nil {
|
||||
stored, ok, err := e.Transcripts.GetTranscript(ctx, string(v.Provider), v.ProviderVideoID)
|
||||
if err != nil {
|
||||
return domain.Transcript{}, fmt.Errorf("get stored transcript: %w", err)
|
||||
}
|
||||
if ok {
|
||||
return stored, nil
|
||||
}
|
||||
}
|
||||
|
||||
t, err := e.Source.FetchTranscript(ctx, v)
|
||||
if err != nil {
|
||||
return domain.Transcript{}, fmt.Errorf("fetch transcript: %w", err)
|
||||
}
|
||||
|
||||
if e.Transcripts != nil && t.Source != domain.SourceRateLimited {
|
||||
if err := e.Transcripts.SaveTranscript(ctx, string(v.Provider), v.ProviderVideoID, t); err != nil {
|
||||
return domain.Transcript{}, fmt.Errorf("save transcript: %w", err)
|
||||
}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// ProcessNewVideos walks a user's subscriptions and processes each newly seen
|
||||
// video. Only videos surfaced via the user's subscriptions are considered, so a
|
||||
// channel the user is not subscribed to is never processed. A video already
|
||||
|
||||
Reference in New Issue
Block a user