Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

feat(meetings): find a mark of the browser in the stored transcript

Guido Boyke64971f950d30
diff --git a/app/helpers/meetings_helper.rb b/app/helpers/meetings_helper.rb
index 96d0234..b18c1aa 100644
--- a/app/helpers/meetings_helper.rb
+++ b/app/helpers/meetings_helper.rb
@@ -271,6 +271,14 @@ module MeetingsHelper
meeting_markdown_source(meeting.transcript, speakers: participants.map(&:display_name))
end
+ # The HTML the transcript block of the page holds: the component's own render
+ # of meeting_transcript_source. Meetings::TranscriptMark reads the text the
+ # browser shows out of it, to find a mark of the reader in the stored
+ # transcript (K4b); rendered the same way, the two texts are the same.
+ def meeting_transcript_html(meeting, participants)
+ Neo::MarkdownPreviewComponent.new(markdown: meeting_transcript_source(meeting, participants)).rendered_markdown
+ end
+
# Plain-text source for the header "Abriss": the AI summary if present, else the
# notes. Both run through the SAME path — frontmatter strip + StripDown (markdown →
# plain text) — so a leading metadata block and "##"/"**" syntax never surface as
diff --git a/app/services/meetings/term_replacement.rb b/app/services/meetings/term_replacement.rb
index d83e2eb..6046f0b 100644
--- a/app/services/meetings/term_replacement.rb
+++ b/app/services/meetings/term_replacement.rb
@@ -35,10 +35,14 @@ module Meetings
Match = Data.define(:line_index, :char_offset, :length, :in_label)
Replaced = Data.define(:text, :count)
- def initialize(text:, heard:, participants: [])
+ # skip_frontmatter: false for a text that has none by construction (the
+ # text a page shows, Meetings::TranscriptMark), where a leading block of
+ # "---" lines is content.
+ def initialize(text:, heard:, participants: [], skip_frontmatter: true)
@text = text.to_s
@words = heard.to_s.split(/[[:space:]]+/).reject(&:empty?)
@participants = Array(participants)
+ @skip_frontmatter = skip_frontmatter
end
# Every whole-word match outside the frontmatter, in text order, the ones
@@ -124,7 +128,7 @@ module Meetings
# block that ends the text without a line break ends on a line of its own.
def first_content_line
@first_content_line ||= begin
- block = @text[Meeting::FRONTMATTER_BLOCK]
+ block = @text[Meeting::FRONTMATTER_BLOCK] if @skip_frontmatter
if block.nil? then 0
elsif block.end_with?("\n") then block.count("\n")
else block.count("\n") + 1
diff --git a/app/services/meetings/transcript_mark.rb b/app/services/meetings/transcript_mark.rb
new file mode 100644
index 0000000..e0fbc68
--- /dev/null
+++ b/app/services/meetings/transcript_mark.rb
@@ -0,0 +1,130 @@
+# frozen_string_literal: true
+
+# Where a mark of the browser lies in the stored transcript (K4b). The page
+# shows the transcript through Markdown (Neo::MarkdownPreviewComponent): bold
+# speakers, no frontmatter, a link shows its text and not its target. A
+# position in the page is no position in the stored text, and the page
+# carries none of the source (and must not: the component and
+# Lalabase::SafeMarkdown stay as they are).
+#
+# So the browser sends what the reader marked, up to CONTEXT_LENGTH
+# characters of the text the page shows before and after it, and how many
+# characters of that text stand before it. This class reads the same text out
+# of the same HTML (MeetingsHelper#meeting_transcript_html), finds the mark
+# there (the offset first, the context as its proof; else the one place with
+# that context), asks which whole-word match of the form it is, and takes the
+# match with the same number in the stored transcript
+# (Meetings::TermReplacement#scan, the rule of counting and replacing).
+#
+# It refuses rather than guesses:
+# * not_found: no place shows the mark with its context, or several do;
+# * not_a_word: the mark is part of a longer word;
+# * ambiguous: the page and the stored text hold the form a different number
+# of times (Markdown hid some of it, a link target or an escaped sign);
+# * speaker_label: the mark stands in the speaker label of its line;
+# * line_break, too_long: no heard form at all.
+#
+# Characters, not bytes and not UTF-16 units: the browser counts code points.
+#
+# Entry point: .resolve
+module Meetings
+ class TranscriptMark
+ # What the browser sends on each side, and the most this class reads.
+ CONTEXT_LENGTH = 40
+ MAX_CONTEXT_LENGTH = 200
+
+ Place = Data.define(:line_index, :char_offset, :length)
+
+ Result = Data.define(:place, :error) do
+ def success?
+ error.nil?
+ end
+ end
+
+ def self.resolve(**)
+ new(**).resolve
+ end
+
+ # transcript: as stored. displayed_html: what the page shows of it.
+ # participants: the meeting's (anything with a display_name). text,
+ # before, after: strings from the browser. offset: an Integer or nil.
+ def initialize(transcript:, displayed_html:, participants:, text:, before:, after:, offset:)
+ @transcript = transcript.to_s
+ @displayed_html = displayed_html.to_s
+ @participants = participants
+ raw = text.to_s
+ lead = raw[/\A[[:space:]]*/]
+ trail = raw[/[[:space:]]*\z/]
+ @mark = raw[lead.length...(raw.length - trail.length)].to_s
+ @before = before.to_s.last(MAX_CONTEXT_LENGTH) + lead
+ @after = trail + after.to_s.first(MAX_CONTEXT_LENGTH)
+ @offset = offset && (offset + lead.length)
+ end
+
+ def resolve
+ return refuse(:not_found) if @mark.empty?
+ return refuse(:line_break) if @mark.match?(/[\r\n]/)
+ return refuse(:too_long) if @mark.length > GlossaryTerm::MAX_LENGTH
+
+ start = locate
+ return refuse(:not_found) unless start
+
+ map(start)
+ end
+
+ private
+
+ def map(start)
+ shown = TermReplacement.new(text: visible, heard: @mark, skip_frontmatter: false).scan
+ line_index, column = line_and_column(start)
+ ordinal = shown.index { |match| match.line_index == line_index && match.char_offset == column }
+ return refuse(:not_a_word) unless ordinal
+
+ stored = TermReplacement.new(text: @transcript, heard: @mark, participants: @participants).scan
+ return refuse(:ambiguous) unless stored.size == shown.size
+
+ match = stored[ordinal]
+ return refuse(:speaker_label) if match.in_label
+
+ Result.new(place: Place.new(line_index: match.line_index, char_offset: match.char_offset, length: match.length),
+ error: nil)
+ end
+
+ # The text the content element of the page holds, as the browser reads it
+ # (the text of every node, in order).
+ def visible
+ @visible ||= Nokogiri::HTML5.fragment(@displayed_html).text
+ end
+
+ def locate
+ return @offset if @offset && fits?(@offset)
+
+ starts = []
+ from = 0
+ while (found = visible.index(@mark, from))
+ starts << found if fits?(found)
+ from = found + 1
+ end
+ starts.first if starts.one?
+ end
+
+ def fits?(start)
+ finish = start + @mark.length
+ return false if start < @before.length || finish + @after.length > visible.length
+
+ visible[start, @mark.length] == @mark &&
+ visible[start - @before.length, @before.length] == @before &&
+ visible[finish, @after.length] == @after
+ end
+
+ def line_and_column(start)
+ prefix = visible[0, start]
+ line_start = prefix.rindex("\n")
+ [prefix.count("\n"), line_start ? start - line_start - 1 : start]
+ end
+
+ def refuse(error)
+ Result.new(place: nil, error: error)
+ end
+ end
+end
diff --git a/spec/services/meetings/speaker_markup_spec.rb b/spec/services/meetings/speaker_markup_spec.rb
index eb2ac02..cc9f1ae 100644
--- a/spec/services/meetings/speaker_markup_spec.rb
+++ b/spec/services/meetings/speaker_markup_spec.rb
@@ -120,6 +120,7 @@ RSpec.describe Meetings::SpeakerMarkup do
expect(mark(text, ['Bert Bauer', 'Karl Krause'])).to eq(text)
end
end
+
# The label of one line (K4b): what Meetings::TermReplacement never replaces
# and reads the speaker from. The same participant list decides, in the plain
# and in the already bolded form.
diff --git a/spec/services/meetings/transcript_mark_spec.rb b/spec/services/meetings/transcript_mark_spec.rb
new file mode 100644
index 0000000..6628170
--- /dev/null
+++ b/spec/services/meetings/transcript_mark_spec.rb
@@ -0,0 +1,128 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# Where a mark of the browser lies in the stored transcript (K4b). The page
+# shows the transcript through Markdown, so the browser can only say what the
+# reader marked and what the page shows around it. The class reads the same
+# text out of the same HTML the page shows (MeetingsHelper#meeting_transcript_html)
+# and maps the n-th whole-word match there to the n-th in the stored text, or
+# refuses. Invented words and names only.
+RSpec.describe Meetings::TranscriptMark do
+ def participant(name)
+ Struct.new(:display_name).new(name)
+ end
+
+ let(:participants) { [participant('Anna Ananas'), participant('Ben Birne')] }
+
+ def displayed_html(transcript)
+ ApplicationController.helpers.meeting_transcript_html(Struct.new(:transcript).new(transcript), participants)
+ end
+
+ # The text the browser shows: what the content element of the page holds.
+ def visible(transcript)
+ Nokogiri::HTML5.fragment(displayed_html(transcript)).text
+ end
+
+ # What the browser sends for the nth (0-based) place of `mark` in the text
+ # it shows: the mark, up to 40 characters around it, and its offset.
+ def browser_mark(transcript, mark, nth: 0)
+ shown = visible(transcript)
+ start = shown.to_enum(:scan, mark).map { Regexp.last_match.begin(0) }.fetch(nth)
+ { text: mark, before: shown[[start - 40, 0].max...start], after: shown[start + mark.length, 40], offset: start }
+ end
+
+ def resolve(transcript, **mark)
+ described_class.resolve(transcript: transcript, displayed_html: displayed_html(transcript), participants: participants, **mark)
+ end
+
+ let(:transcript) do
+ <<~TEXT
+ ---
+ id: aufnahme-7
+ ---
+ Anna: Wir reden über Lalabeis.
+ Ben: **Lalabeis** ist gut, und Lalabeis-Konten auch.
+
+ - ein Punkt mit Lalabeis
+ Anna: Zum Schluss noch einmal Lalabeis.
+ TEXT
+ end
+
+ it 'finds the place in the stored text, lines counted with the frontmatter, in characters' do
+ result = resolve(transcript, **browser_mark(transcript, 'Lalabeis', nth: 2))
+
+ expect(result).to be_success
+ expect(result.place.to_h).to eq(line_index: 4, char_offset: 31, length: 8)
+ end
+
+ it 'finds every place of the form, in the order the page shows them' do
+ places = (0..4).map { |nth| resolve(transcript, **browser_mark(transcript, 'Lalabeis', nth: nth)).place.to_h.values_at(:line_index, :char_offset) }
+
+ expect(places).to eq([[3, 21], [4, 7], [4, 31], [6, 16], [7, 30]])
+ end
+
+ it 'takes the context alone when the offset is off, if only one place has it' do
+ mark = browser_mark(transcript, 'Lalabeis', nth: 3).merge(offset: 3)
+
+ expect(resolve(transcript, **mark).place.to_h.values_at(:line_index, :char_offset)).to eq([6, 16])
+ end
+
+ it 'refuses when the offset is off and the context fits several places' do
+ text = "Ben: ja Lalabeis ja\nBen: ja Lalabeis ja\n"
+ mark = browser_mark(text, 'Lalabeis').merge(before: 'ja ', after: ' ja', offset: 0)
+
+ expect(resolve(text, **mark).error).to eq(:not_found)
+ end
+
+ it 'refuses a mark the page does not show there' do
+ mark = browser_mark(transcript, 'Lalabeis').merge(before: 'nirgends ', offset: 1)
+
+ expect(resolve(transcript, **mark).error).to eq(:not_found)
+ end
+
+ it 'refuses part of a longer word' do
+ mark = browser_mark(transcript, 'Lalabei')
+
+ expect(resolve(transcript, **mark).error).to eq(:not_a_word)
+ end
+
+ it 'refuses a mark in the speaker label' do
+ mark = browser_mark(transcript, 'Anna')
+
+ expect(resolve(transcript, **mark).error).to eq(:speaker_label)
+ end
+
+ # A link shows its text, not its target: the page holds the form once, the
+ # stored text twice. Which one is which is a guess, so it is refused.
+ it 'refuses when Markdown shows the form a different number of times than the stored text holds' do
+ text = "Ben: [Seite](https://example.com/lalabeis) und lalabeis.\n"
+
+ expect(resolve(text, **browser_mark(text, 'lalabeis')).error).to eq(:ambiguous)
+ end
+
+ it 'refuses a mark over a line break and a mark too long for the glossary' do
+ two_lines = "Anna: eins Lala\nBeis zwei\n"
+ long = "Anna: #{'x' * 300}\n"
+
+ expect(resolve(two_lines, text: "Lala\nBeis", before: 'eins ', after: ' zwei', offset: 11).error).to eq(:line_break)
+ expect(resolve(long, **browser_mark(long, 'x' * 300)).error).to eq(:too_long)
+ end
+
+ it 'takes whitespace around the mark for context' do
+ mark = browser_mark(transcript, 'Lalabeis', nth: 1)
+ padded = mark.merge(text: ' Lalabeis ', before: mark[:before][0...-1], after: mark[:after][1..], offset: mark[:offset] - 1)
+
+ expect(resolve(transcript, **padded).place.to_h.values_at(:line_index, :char_offset)).to eq([4, 7])
+ end
+
+ it 'counts the offset in characters, as the browser sends it' do
+ text = "Anna: Grüße 👋 und Lalabeis\n"
+
+ expect(resolve(text, **browser_mark(text, 'Lalabeis')).place.to_h.values_at(:line_index, :char_offset)).to eq([0, 18])
+ end
+
+ it 'refuses a blank mark' do
+ expect(resolve(transcript, text: ' ', before: '', after: '', offset: 0).error).to eq(:not_found)
+ end
+end