CI / go (Media) (push) Successful in 11m59s
CI / go (Server) (push) Failing after 18m26s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 8m35s
CI / desktop (ubuntu-latest) (push) Successful in 1h10m6s
CI / mobile (push) Failing after 3m43s
CI / docker (push) Successful in 1h2m44s
Docs / build (push) Successful in 11m58s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
ffmpeg parses untrusted uploads, so it moves out of the server image into a container of its own (ADR-15): - Media/: docwell-media, a small Go wrapper around ffmpeg. POST /v1/split takes a recording and returns the 16 kHz mono MP3 pieces as multipart/mixed. ffmpeg may only open local files and common audio/video demuxers (no playlists); optional shared token, a job limit, a timeout and a 1 GB cap. - Server: DOCWELL_MEDIA_URL and DOCWELL_MEDIA_TOKEN send recordings over 25 MB to the service. When it is blank or unreachable, the server uses a local ffmpeg if it has one, else cuts MP3 and WAV in Go. The server image no longer installs ffmpeg; the all-in-one image keeps it to stay one container. - Compose: a media service in every stack, read-only, no capabilities, on an internal network only docwell-server joins. - CI tests the new module and checks that only the server reaches media; the release workflow publishes docwell-media. - Docs, ADR-15 and the .env examples. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
5.2 KiB
Go
164 lines
5.2 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package ai
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/apperr"
|
|
)
|
|
|
|
// ---- Audio attachments → transcript and summary (E2) -------------------------------
|
|
|
|
// MaxTranscribeBytes is the largest piece sent for transcription (the limit
|
|
// of OpenAI's API, which most compatible servers share); longer recordings
|
|
// are cut into pieces (audio.go).
|
|
const MaxTranscribeBytes = 25 << 20
|
|
|
|
// MaxRecordingBytes caps the recordings transcribed (attachments are capped
|
|
// by DOCWELL_MAX_UPLOAD_MB first).
|
|
const MaxRecordingBytes = 1 << 30
|
|
|
|
// Transcript is what a transcription produced. Summary is "" when it was not
|
|
// asked for or failed (SummaryError says why).
|
|
type Transcript struct {
|
|
Name string `json:"name"`
|
|
Text string `json:"text"`
|
|
Summary string `json:"summary"`
|
|
SummaryError string `json:"summaryError,omitempty"`
|
|
}
|
|
|
|
var mediaExt = []string{".mp3", ".mp4", ".mpeg", ".mpga", ".m4a", ".wav", ".webm", ".ogg", ".oga", ".flac", ".aac", ".opus", ".mov"}
|
|
|
|
// IsMedia reports whether a file looks like audio or video.
|
|
func IsMedia(mime, name string) bool {
|
|
if strings.HasPrefix(mime, "audio/") || strings.HasPrefix(mime, "video/") {
|
|
return true
|
|
}
|
|
ext := strings.ToLower(filepath.Ext(name))
|
|
for _, e := range mediaExt {
|
|
if e == ext {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
const transcriptSystem = `You summarise recordings for a team wiki. You get the transcript of an audio or video file. Reply in Markdown, in the language of the transcript, with:
|
|
|
|
- a short summary paragraph,
|
|
- "Key points" as a bulleted list,
|
|
- "Action items" as a task list ("- [ ] …", with who and when where the transcript says so), only if there are any.
|
|
|
|
No preamble. Do not invent anything that is not in the transcript.`
|
|
|
|
var errNoTranscriber = apperr.Invalid("No AI provider here can transcribe audio: a workspace admin can add a speech-to-text model to an OpenAI-compatible provider.")
|
|
|
|
// transcribePiece transcribes one piece on the first profile that manages.
|
|
func (s *Service) transcribePiece(ctx context.Context, ws string, chain []profileRecord, piece audioPiece) (string, error) {
|
|
var lastErr error = errNoTranscriber
|
|
for _, rec := range chain {
|
|
if rec.TranscribeModel == "" {
|
|
continue
|
|
}
|
|
p, err := s.provider(rec)
|
|
if err != nil {
|
|
lastErr = err
|
|
continue
|
|
}
|
|
text, err := p.Transcribe(ctx, rec.TranscribeModel, piece.Name, piece.Data)
|
|
// Speech-to-text APIs report no tokens; count the text's rough size.
|
|
s.record(ws, rec, Usage{OutputTokens: utf8.RuneCountInString(text) / 4}, err)
|
|
if err == nil {
|
|
return text, nil
|
|
}
|
|
if ctx.Err() != nil {
|
|
return "", ctx.Err()
|
|
}
|
|
s.log.Warn("ai: transcription failed, trying the next profile", "profile", rec.ID, "err", err)
|
|
lastErr = err
|
|
}
|
|
if _, ok := apperr.As(lastErr); ok {
|
|
return "", lastErr
|
|
}
|
|
return "", apperr.Invalid(userError(lastErr))
|
|
}
|
|
|
|
// Transcribe turns an audio or video attachment the caller can read into
|
|
// text, and (summarize) a summary with key points and action items.
|
|
func (s *Service) Transcribe(ctx context.Context, c access.Caller, fileID string, summarize bool) (Transcript, error) {
|
|
dl, err := s.dir.OpenAttachment(ctx, c, fileID, nil)
|
|
if err != nil {
|
|
return Transcript{}, err
|
|
}
|
|
defer dl.Body.Close()
|
|
if !IsMedia(dl.Mime, dl.Name) {
|
|
return Transcript{}, apperr.Invalid("Only audio and video files can be transcribed.")
|
|
}
|
|
if dl.Size > MaxRecordingBytes {
|
|
return Transcript{}, apperr.Invalid("Recordings up to 1 GB can be transcribed.")
|
|
}
|
|
page, err := s.dir.ReadPage(ctx, c, dl.Page)
|
|
if err != nil {
|
|
return Transcript{}, err
|
|
}
|
|
_, chain, err := s.ready(ctx, c, page.Workspace)
|
|
if err != nil {
|
|
return Transcript{}, err
|
|
}
|
|
if !slices.ContainsFunc(chain, func(r profileRecord) bool { return r.TranscribeModel != "" }) {
|
|
return Transcript{}, errNoTranscriber
|
|
}
|
|
data, err := io.ReadAll(io.LimitReader(dl.Body, MaxRecordingBytes+1))
|
|
if err != nil {
|
|
return Transcript{}, err
|
|
}
|
|
if len(data) > MaxRecordingBytes {
|
|
return Transcript{}, apperr.Invalid("Recordings up to 1 GB can be transcribed.")
|
|
}
|
|
pieces, err := s.splitRecording(ctx, dl.Name, data, MaxTranscribeBytes)
|
|
if err != nil {
|
|
return Transcript{}, err
|
|
}
|
|
|
|
out := Transcript{Name: dl.Name}
|
|
var texts []string
|
|
for _, piece := range pieces {
|
|
text, err := s.transcribePiece(ctx, page.Workspace, chain, piece)
|
|
if err != nil {
|
|
return Transcript{}, err
|
|
}
|
|
if text = strings.TrimSpace(text); text != "" {
|
|
texts = append(texts, text)
|
|
}
|
|
}
|
|
out.Text = strings.Join(texts, "\n\n")
|
|
if out.Text == "" {
|
|
return Transcript{}, apperr.Invalid("No speech was found in the recording.")
|
|
}
|
|
if !summarize {
|
|
return out, nil
|
|
}
|
|
var summary strings.Builder
|
|
_, err = s.chat(ctx, page.Workspace, chain, ChatRequest{
|
|
System: transcriptSystem, MaxTokens: 4000,
|
|
Messages: []Message{{Role: "user", Content: "Transcript of " + dl.Name + ":\n\n" + truncate(out.Text, 120000)}},
|
|
}, func(d string) { summary.WriteString(d) })
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) {
|
|
return Transcript{}, err
|
|
}
|
|
out.SummaryError = userError(err)
|
|
} else {
|
|
out.Summary = strings.TrimSpace(summary.String())
|
|
}
|
|
return out, nil
|
|
}
|