Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

15.8 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: capture.sql

package store

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const advanceCaptureTranscriptCoverage = `-- name: AdvanceCaptureTranscriptCoverage :one
WITH writable_session AS (
  SELECT session.id,session.transcript_end_offset_ms
  FROM capture_sessions session
  JOIN rooms room
    ON room.id=session.room_id AND room.tenant_id=session.tenant_id AND room.deleted_at IS NULL
  JOIN actors caller
    ON caller.tenant_id=session.tenant_id AND caller.account_id=$5::text AND caller.kind='human'
  JOIN room_members membership
    ON membership.room_id=session.room_id AND membership.actor_id=caller.id
  WHERE session.id=$6::text
    AND session.room_id=$7::text
    AND session.status IN ('recording','finalizing')
    AND (
      session.transcript_end_offset_ms IS NULL OR
      $3::bigint <= session.transcript_end_offset_ms
    )
  FOR NO KEY UPDATE OF session
)
INSERT INTO capture_transcript_coverage(
  session_id,stream_id,lane,start_offset_ms,end_offset_ms
) SELECT
  session.id,$1::text,$2::text,
  $3::bigint,
  least($4::bigint,coalesce(session.transcript_end_offset_ms,$4::bigint))
FROM writable_session session
ON CONFLICT (session_id,stream_id) DO UPDATE
SET end_offset_ms=greatest(capture_transcript_coverage.end_offset_ms,EXCLUDED.end_offset_ms),updated_at=now()
WHERE capture_transcript_coverage.lane=EXCLUDED.lane
  AND capture_transcript_coverage.start_offset_ms=EXCLUDED.start_offset_ms
RETURNING end_offset_ms
`

type AdvanceCaptureTranscriptCoverageParams struct {
	StreamID      string `json:"stream_id"`
	Lane          string `json:"lane"`
	StartOffsetMs int64  `json:"start_offset_ms"`
	EndOffsetMs   int64  `json:"end_offset_ms"`
	AccountID     string `json:"account_id"`
	SessionID     string `json:"session_id"`
	RoomID        string `json:"room_id"`
}

func (q *Queries) AdvanceCaptureTranscriptCoverage(ctx context.Context, arg AdvanceCaptureTranscriptCoverageParams) (int64, error) {
	row := q.db.QueryRow(ctx, advanceCaptureTranscriptCoverage,
		arg.StreamID,
		arg.Lane,
		arg.StartOffsetMs,
		arg.EndOffsetMs,
		arg.AccountID,
		arg.SessionID,
		arg.RoomID,
	)
	var end_offset_ms int64
	err := row.Scan(&end_offset_ms)
	return end_offset_ms, err
}

const attachCaptureTranscriptDocument = `-- name: AttachCaptureTranscriptDocument :one
UPDATE capture_sessions
SET transcript_document_id=$2,transcript_document_version_id=$3,updated_at=clock_timestamp()
WHERE id=$1 AND status='finalized'
  AND transcript_document_id IS NULL AND transcript_document_version_id IS NULL
RETURNING id, tenant_id, room_id, created_by_actor_id, status, created_at, updated_at, transcript_start_offset_ms, transcript_end_offset_ms, document_id, document_version_id, transcript_document_id, transcript_document_version_id, voice_session_id, transcript_integrity_lost, source_audio_document_id, source_audio_document_version_id, transcript_recovered_at, requested_transcript_start_offset_ms, voice_completion_attempts, voice_completion_failed_at, voice_completion_failure_code, staging_deleted_at, stop_requested_at, materialize_requested_at, materialize_attempts, materialize_retry_at, transcript_recovery_status, transcript_recovery_progress_ms, transcript_recovery_total_ms, transcript_recovery_failures, transcript_recovery_failure_code, transcript_recovery_retry_at, hidden_at, quick_notes_state, quick_notes
`

type AttachCaptureTranscriptDocumentParams struct {
	ID                          string      `json:"id"`
	TranscriptDocumentID        pgtype.Text `json:"transcript_document_id"`
	TranscriptDocumentVersionID pgtype.Text `json:"transcript_document_version_id"`
}

func (q *Queries) AttachCaptureTranscriptDocument(ctx context.Context, arg AttachCaptureTranscriptDocumentParams) (CaptureSession, error) {
	row := q.db.QueryRow(ctx, attachCaptureTranscriptDocument, arg.ID, arg.TranscriptDocumentID, arg.TranscriptDocumentVersionID)
	var i CaptureSession
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.RoomID,
		&i.CreatedByActorID,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.TranscriptStartOffsetMs,
		&i.TranscriptEndOffsetMs,
		&i.DocumentID,
		&i.DocumentVersionID,
		&i.TranscriptDocumentID,
		&i.TranscriptDocumentVersionID,
		&i.VoiceSessionID,
		&i.TranscriptIntegrityLost,
		&i.SourceAudioDocumentID,
		&i.SourceAudioDocumentVersionID,
		&i.TranscriptRecoveredAt,
		&i.RequestedTranscriptStartOffsetMs,
		&i.VoiceCompletionAttempts,
		&i.VoiceCompletionFailedAt,
		&i.VoiceCompletionFailureCode,
		&i.StagingDeletedAt,
		&i.StopRequestedAt,
		&i.MaterializeRequestedAt,
		&i.MaterializeAttempts,
		&i.MaterializeRetryAt,
		&i.TranscriptRecoveryStatus,
		&i.TranscriptRecoveryProgressMs,
		&i.TranscriptRecoveryTotalMs,
		&i.TranscriptRecoveryFailures,
		&i.TranscriptRecoveryFailureCode,
		&i.TranscriptRecoveryRetryAt,
		&i.HiddenAt,
		&i.QuickNotesState,
		&i.QuickNotes,
	)
	return i, err
}

const getCaptureSessionForAccount = `-- name: GetCaptureSessionForAccount :one
SELECT session.id, session.tenant_id, session.room_id, session.created_by_actor_id, session.status, session.created_at, session.updated_at, session.transcript_start_offset_ms, session.transcript_end_offset_ms, session.document_id, session.document_version_id, session.transcript_document_id, session.transcript_document_version_id, session.voice_session_id, session.transcript_integrity_lost, session.source_audio_document_id, session.source_audio_document_version_id, session.transcript_recovered_at, session.requested_transcript_start_offset_ms, session.voice_completion_attempts, session.voice_completion_failed_at, session.voice_completion_failure_code, session.staging_deleted_at, session.stop_requested_at, session.materialize_requested_at, session.materialize_attempts, session.materialize_retry_at, session.transcript_recovery_status, session.transcript_recovery_progress_ms, session.transcript_recovery_total_ms, session.transcript_recovery_failures, session.transcript_recovery_failure_code, session.transcript_recovery_retry_at, session.hidden_at, session.quick_notes_state, session.quick_notes
FROM capture_sessions session
JOIN actors caller
  ON caller.tenant_id=session.tenant_id AND caller.account_id=$2 AND caller.kind='human'
JOIN room_members membership
  ON membership.room_id=session.room_id AND membership.actor_id=caller.id
JOIN rooms room
  ON room.id=session.room_id AND room.tenant_id=session.tenant_id AND room.deleted_at IS NULL
WHERE session.id=$1
FOR KEY SHARE OF membership
`

type GetCaptureSessionForAccountParams struct {
	ID        string      `json:"id"`
	AccountID pgtype.Text `json:"account_id"`
}

func (q *Queries) GetCaptureSessionForAccount(ctx context.Context, arg GetCaptureSessionForAccountParams) (CaptureSession, error) {
	row := q.db.QueryRow(ctx, getCaptureSessionForAccount, arg.ID, arg.AccountID)
	var i CaptureSession
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.RoomID,
		&i.CreatedByActorID,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.TranscriptStartOffsetMs,
		&i.TranscriptEndOffsetMs,
		&i.DocumentID,
		&i.DocumentVersionID,
		&i.TranscriptDocumentID,
		&i.TranscriptDocumentVersionID,
		&i.VoiceSessionID,
		&i.TranscriptIntegrityLost,
		&i.SourceAudioDocumentID,
		&i.SourceAudioDocumentVersionID,
		&i.TranscriptRecoveredAt,
		&i.RequestedTranscriptStartOffsetMs,
		&i.VoiceCompletionAttempts,
		&i.VoiceCompletionFailedAt,
		&i.VoiceCompletionFailureCode,
		&i.StagingDeletedAt,
		&i.StopRequestedAt,
		&i.MaterializeRequestedAt,
		&i.MaterializeAttempts,
		&i.MaterializeRetryAt,
		&i.TranscriptRecoveryStatus,
		&i.TranscriptRecoveryProgressMs,
		&i.TranscriptRecoveryTotalMs,
		&i.TranscriptRecoveryFailures,
		&i.TranscriptRecoveryFailureCode,
		&i.TranscriptRecoveryRetryAt,
		&i.HiddenAt,
		&i.QuickNotesState,
		&i.QuickNotes,
	)
	return i, err
}

const getCaptureTranscriptAcceptedOffset = `-- name: GetCaptureTranscriptAcceptedOffset :one
WITH ordered AS (
  SELECT coverage.start_offset_ms,coverage.end_offset_ms,session.transcript_start_offset_ms,
    max(coverage.end_offset_ms) OVER (
      ORDER BY coverage.start_offset_ms,coverage.end_offset_ms,coverage.stream_id
      ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
    ) AS previous_end_offset_ms
  FROM capture_sessions session
  LEFT JOIN capture_transcript_coverage coverage ON coverage.session_id=session.id
  WHERE session.id=$1
), grouped AS (
  SELECT start_offset_ms, end_offset_ms, transcript_start_offset_ms, previous_end_offset_ms,sum(CASE
    WHEN start_offset_ms IS NULL THEN 0
    WHEN start_offset_ms > coalesce(previous_end_offset_ms,transcript_start_offset_ms) THEN 1
    ELSE 0
  END) OVER (
    ORDER BY start_offset_ms,end_offset_ms
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  ) AS gap_group
  FROM ordered
)
SELECT coalesce(
  max(end_offset_ms) FILTER (WHERE gap_group=0),
  max(transcript_start_offset_ms),
  0
)::bigint
FROM grouped
`

func (q *Queries) GetCaptureTranscriptAcceptedOffset(ctx context.Context, id string) (int64, error) {
	row := q.db.QueryRow(ctx, getCaptureTranscriptAcceptedOffset, id)
	var column_1 int64
	err := row.Scan(&column_1)
	return column_1, err
}

const getPendingCaptureQuickNotes = `-- name: GetPendingCaptureQuickNotes :one
SELECT session.id, session.tenant_id, session.room_id, session.created_by_actor_id, session.status, session.created_at, session.updated_at, session.transcript_start_offset_ms, session.transcript_end_offset_ms, session.document_id, session.document_version_id, session.transcript_document_id, session.transcript_document_version_id, session.voice_session_id, session.transcript_integrity_lost, session.source_audio_document_id, session.source_audio_document_version_id, session.transcript_recovered_at, session.requested_transcript_start_offset_ms, session.voice_completion_attempts, session.voice_completion_failed_at, session.voice_completion_failure_code, session.staging_deleted_at, session.stop_requested_at, session.materialize_requested_at, session.materialize_attempts, session.materialize_retry_at, session.transcript_recovery_status, session.transcript_recovery_progress_ms, session.transcript_recovery_total_ms, session.transcript_recovery_failures, session.transcript_recovery_failure_code, session.transcript_recovery_retry_at, session.hidden_at, session.quick_notes_state, session.quick_notes
FROM capture_sessions session
WHERE session.id=$1 AND session.quick_notes_state='pending'
`

func (q *Queries) GetPendingCaptureQuickNotes(ctx context.Context, id string) (CaptureSession, error) {
	row := q.db.QueryRow(ctx, getPendingCaptureQuickNotes, id)
	var i CaptureSession
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.RoomID,
		&i.CreatedByActorID,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.TranscriptStartOffsetMs,
		&i.TranscriptEndOffsetMs,
		&i.DocumentID,
		&i.DocumentVersionID,
		&i.TranscriptDocumentID,
		&i.TranscriptDocumentVersionID,
		&i.VoiceSessionID,
		&i.TranscriptIntegrityLost,
		&i.SourceAudioDocumentID,
		&i.SourceAudioDocumentVersionID,
		&i.TranscriptRecoveredAt,
		&i.RequestedTranscriptStartOffsetMs,
		&i.VoiceCompletionAttempts,
		&i.VoiceCompletionFailedAt,
		&i.VoiceCompletionFailureCode,
		&i.StagingDeletedAt,
		&i.StopRequestedAt,
		&i.MaterializeRequestedAt,
		&i.MaterializeAttempts,
		&i.MaterializeRetryAt,
		&i.TranscriptRecoveryStatus,
		&i.TranscriptRecoveryProgressMs,
		&i.TranscriptRecoveryTotalMs,
		&i.TranscriptRecoveryFailures,
		&i.TranscriptRecoveryFailureCode,
		&i.TranscriptRecoveryRetryAt,
		&i.HiddenAt,
		&i.QuickNotesState,
		&i.QuickNotes,
	)
	return i, err
}

const listCaptureTranscriptSegments = `-- name: ListCaptureTranscriptSegments :many
SELECT id, event_id, voice_session_id, capture_session_id, media_epoch, lane, start_offset_ms, end_offset_ms, text, normalized_text, kind, accepted_at, speaker_id, seq, superseded_at FROM voice_transcript_segments
WHERE capture_session_id=$1::text AND superseded_at IS NULL
  AND start_offset_ms >= $2::bigint AND end_offset_ms <= $3::bigint
ORDER BY start_offset_ms,end_offset_ms,id
`

type ListCaptureTranscriptSegmentsParams struct {
	CaptureSessionID string `json:"capture_session_id"`
	StartOffsetMs    int64  `json:"start_offset_ms"`
	EndOffsetMs      int64  `json:"end_offset_ms"`
}

// A Capture's transcript: its live speech that archive recovery did not
// replace, and the recovered text.
func (q *Queries) ListCaptureTranscriptSegments(ctx context.Context, arg ListCaptureTranscriptSegmentsParams) ([]VoiceTranscriptSegment, error) {
	rows, err := q.db.Query(ctx, listCaptureTranscriptSegments, arg.CaptureSessionID, arg.StartOffsetMs, arg.EndOffsetMs)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []VoiceTranscriptSegment{}
	for rows.Next() {
		var i VoiceTranscriptSegment
		if err := rows.Scan(
			&i.ID,
			&i.EventID,
			&i.VoiceSessionID,
			&i.CaptureSessionID,
			&i.MediaEpoch,
			&i.Lane,
			&i.StartOffsetMs,
			&i.EndOffsetMs,
			&i.Text,
			&i.NormalizedText,
			&i.Kind,
			&i.AcceptedAt,
			&i.SpeakerID,
			&i.Seq,
			&i.SupersededAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listLiveKitCaptureCompletionSegments = `-- name: ListLiveKitCaptureCompletionSegments :many
SELECT segment.id, segment.event_id, segment.voice_session_id, segment.capture_session_id, segment.media_epoch, segment.lane, segment.start_offset_ms, segment.end_offset_ms, segment.text, segment.normalized_text, segment.kind, segment.accepted_at, segment.speaker_id, segment.seq, segment.superseded_at FROM voice_transcript_segments segment
JOIN capture_sessions session ON session.id=segment.capture_session_id
WHERE session.id=$1 AND segment.superseded_at IS NULL
  AND segment.start_offset_ms >= session.requested_transcript_start_offset_ms
  AND segment.start_offset_ms < session.transcript_end_offset_ms
ORDER BY segment.start_offset_ms,segment.end_offset_ms,segment.id
`

// Completion keeps speech that starts at or after the requested Start,
// including a final that straddles Stop. Speech before the requested Start stays
// private. The requested Start, not the later recorder media clock, is the
// boundary, so speech during recorder startup reaches the delivered transcript.
func (q *Queries) ListLiveKitCaptureCompletionSegments(ctx context.Context, id string) ([]VoiceTranscriptSegment, error) {
	rows, err := q.db.Query(ctx, listLiveKitCaptureCompletionSegments, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []VoiceTranscriptSegment{}
	for rows.Next() {
		var i VoiceTranscriptSegment
		if err := rows.Scan(
			&i.ID,
			&i.EventID,
			&i.VoiceSessionID,
			&i.CaptureSessionID,
			&i.MediaEpoch,
			&i.Lane,
			&i.StartOffsetMs,
			&i.EndOffsetMs,
			&i.Text,
			&i.NormalizedText,
			&i.Kind,
			&i.AcceptedAt,
			&i.SpeakerID,
			&i.Seq,
			&i.SupersededAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markCaptureQuickNotesPending = `-- name: MarkCaptureQuickNotesPending :exec
UPDATE capture_sessions
SET quick_notes_state='pending', quick_notes=NULL
WHERE id=$1
`

// Its completion Message promised quick notes; the job writes them.
func (q *Queries) MarkCaptureQuickNotesPending(ctx context.Context, id string) error {
	_, err := q.db.Exec(ctx, markCaptureQuickNotesPending, id)
	return err
}

const settleCaptureQuickNotes = `-- name: SettleCaptureQuickNotes :execrows
UPDATE capture_sessions
SET quick_notes_state=$1, quick_notes=$2
WHERE id=$3 AND quick_notes_state='pending'
`

type SettleCaptureQuickNotesParams struct {
	State pgtype.Text `json:"state"`
	Notes []byte      `json:"notes"`
	ID    string      `json:"id"`
}

// Writes the notes (ready) or gives up (failed) once, while they are pending.
func (q *Queries) SettleCaptureQuickNotes(ctx context.Context, arg SettleCaptureQuickNotesParams) (int64, error) {
	result, err := q.db.Exec(ctx, settleCaptureQuickNotes, arg.State, arg.Notes, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}