The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.
Rust agent-readableai-agentsarecclaude-codecodexcursorlocal-firstmacosprivacyrustscreen-recorderscreen-recordingtauritranscriptiontypescriptwindows
/**
* Apple's on-device recognizer returns one `SFTranscriptionSegment` per token,
* so a local recording's transcript is a column of single words each carrying
* the same second. That is the right shape for the AREC — an agent wants the
* word timings — and an unreadable shape for a human, so the viewer folds the
* tokens back into lines before drawing them.
*
* Display only. `transcript.json` and `recording.arec` keep every token.
*/
export interface TranscriptLineSegment {
startMs: number;
endMs: number;
text: string;
}
/** A silence this long reads as the end of a thought, so the line breaks. */
const PAUSE_MS = 700;
/** Ceiling so an unbroken monologue does not become one wall of text. */
const MAX_LINE_MS = 14_000;
/** Roughly two wrapped lines in the viewer's 380px column. */
const MAX_LINE_WIDTH = 90;
/**
* The recognizer is built from `NSLocale.currentLocale`, so the tokens can be
* Japanese, Chinese, Thai and other scripts that do not separate words with a
* space. Joining those with an ASCII space would render `你 好 世 界`, so the
* space is dropped whenever either side of the seam belongs to one of them.
* Hangul is deliberately absent — Korean does space its words.
*/
const NO_SPACE_SCRIPT =
/[--က-႟ក- -〿-ヿ㐀-䶿一-鿿豈--・]/;
/** Punctuation the recognizer emits as its own token; it takes no space. */
const CLINGS_LEFT = /^[,.!?;:…%)\]}'’”]/;
const SENTENCE_END = /[.!?…。!?]["'’”)\]}」』)]?$/;
function join(line: string, token: string): string {
if (CLINGS_LEFT.test(token)) return `${line}${token}`;
if (NO_SPACE_SCRIPT.test(line.slice(-1)) || NO_SPACE_SCRIPT.test(token[0])) {
return `${line}${token}`;
}
return `${line} ${token}`;
}
/**
* How much room a line asks for, not how many characters it holds: a
* full-width glyph occupies about two Latin ones, so counting them equally
* would let a Japanese line run to four wrapped rows instead of two.
*/
const WIDE_GLYPH =
/[\u1100-\u11FF\u2E80-\u303F\u3040-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uAC00-\uD7AF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFF60\uFFE0-\uFFE6]/;
const WIDTHLESS_MARK = /\p{M}/u;
function width(text: string): number {
let total = 0;
for (const char of text) {
if (WIDTHLESS_MARK.test(char)) continue; // marks take no room: 0
total += WIDE_GLYPH.test(char) ? 2 : 1;
}
return total;
}
function breaks(line: TranscriptLineSegment, next: TranscriptLineSegment, text: string): boolean {
if (SENTENCE_END.test(line.text)) return true;
if (next.startMs - line.endMs >= PAUSE_MS) return true;
if (next.endMs - line.startMs >= MAX_LINE_MS) return true;
return width(join(line.text, text)) > MAX_LINE_WIDTH;
}
export function groupTranscriptLines(
segments: readonly TranscriptLineSegment[]
): TranscriptLineSegment[] {
const lines: TranscriptLineSegment[] = [];
let line: TranscriptLineSegment | null = null;
for (const segment of segments) {
const text = segment.text.trim();
if (!text) continue;
if (line && !breaks(line, segment, text)) {
line = {
startMs: line.startMs,
endMs: Math.max(line.endMs, segment.endMs),
text: join(line.text, text),
};
continue;
}
if (line) lines.push(line);
line = {
startMs: segment.startMs,
endMs: Math.max(segment.startMs, segment.endMs),
text,
};
}
if (line) lines.push(line);
return lines;
}
export interface TranscriptMatchRange {
/** Inclusive UTF-16 start offset in the line's original `text`. */
start: number;
/** Exclusive UTF-16 end offset in the line's original `text`. */
end: number;
}
export interface TranscriptSearchMatch extends TranscriptMatchRange {
/** Index into the grouped `lines` array. */
lineIndex: number;
/** Start timestamp of the matching line, for seeking. */
startMs: number;
}
const COMBINING_MARK = /\p{Mn}/u;
const COMBINING_MARKS = /\p{Mn}/gu;
const ACCENT_RANGES: ReadonlyArray<readonly [number, number]> = [
[0x0300, 0x036f],
[0x0591, 0x05c7],
[0x0610, 0x061a],
[0x064b, 0x065f],
[0x0670, 0x0670],
[0x06d6, 0x06ed],
[0x08d3, 0x08ff],
[0x1ab0, 0x1aff],
[0x1dc0, 0x1dff],
[0xfe20, 0xfe2f],
];
function stripAccents(text: string): string {
return text.replace(COMBINING_MARKS, (mark) => {
const codePoint = mark.codePointAt(0)!;
return ACCENT_RANGES.some(([low, high]) => codePoint >= low && codePoint <= high)
? ""
: mark;
});
}
function foldText(text: string): string {
return stripAccents(text.normalize("NFD")).normalize("NFC").toLowerCase();
}
function extendsCharacter(codePoint: number): boolean {
return (
(codePoint >= 0x1160 && codePoint <= 0x11ff) ||
(codePoint >= 0xd7b0 && codePoint <= 0xd7ff) ||
COMBINING_MARK.test(String.fromCodePoint(codePoint))
);
}
/**
* Folds a string to lowercase with diacritics stripped, while tracking the
* original UTF-16 `[start, end)` span for each folded code unit so highlight
* ranges map back onto the unmodified text (including decomposed accents and
* surrogate pairs).
*/
function foldWithMap(text: string): { folded: string; starts: number[]; ends: number[] } {
let folded = "";
const starts: number[] = [];
const ends: number[] = [];
for (let offset = 0; offset < text.length; ) {
let end = offset + (text.codePointAt(offset)! > 0xffff ? 2 : 1);
while (end < text.length) {
const next = text.codePointAt(end)!;
if (!extendsCharacter(next)) break;
end += next > 0xffff ? 2 : 1;
}
const cleaned = foldText(text.slice(offset, end));
if (cleaned.length === 0) {
// A standalone combining mark extends the preceding base character's span.
if (ends.length > 0) {
const prevEnd = ends[ends.length - 1];
for (let i = ends.length - 1; i >= 0 && ends[i] === prevEnd; i--) {
ends[i] = end;
}
}
} else {
for (let i = 0; i < cleaned.length; i++) {
folded += cleaned[i];
starts.push(offset);
ends.push(end);
}
}
offset = end;
}
return { folded, starts, ends };
}
/**
* Finds every non-overlapping case- and accent-insensitive match of `query`
* inside `text`, returning UTF-16 `[start, end)` offsets in `text`.
*/
export function findTranscriptTextMatches(
text: string,
query: string
): TranscriptMatchRange[] {
const trimmed = query.trim();
if (!trimmed || !text) return [];
const needle = foldText(trimmed);
if (!needle) return [];
const { folded, starts, ends } = foldWithMap(text);
const ranges: TranscriptMatchRange[] = [];
let cursor = 0;
while (cursor <= folded.length - needle.length) {
const index = folded.indexOf(needle, cursor);
if (index < 0) break;
ranges.push({
start: starts[index],
end: ends[index + needle.length - 1],
});
cursor = index + needle.length;
}
return ranges;
}
/**
* Finds every match across all grouped transcript lines in reading order.
*/
export function findTranscriptMatches(
lines: readonly TranscriptLineSegment[],
query: string
): TranscriptSearchMatch[] {
const matches: TranscriptSearchMatch[] = [];
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
const line = lines[lineIndex];
for (const range of findTranscriptTextMatches(line.text, query)) {
matches.push({
lineIndex,
startMs: line.startMs,
start: range.start,
end: range.end,
});
}
}
return matches;
}