Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ jobs:
app/tests/name-the-admins-layout.test.tsx
app/tests/new-conversation-picked-model.test.tsx
app/tests/offline-conversation-layout.test.tsx
app/tests/older-safari-build.test.ts
app/tests/one-press-connect-layout.test.tsx
app/tests/organization-held-actions.test.tsx
app/tests/organization-held-recovery.test.tsx
Expand Down
2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"zod": "^4.4.3"
},
"devDependencies": {
"@eslint-community/regexpp": "4.12.3",
"@happy-dom/global-registrator": "^20.11.2",
"@tailwindcss/vite": "^4.3.3",
"@tanstack/router-plugin": "^1.168.30",
Expand All @@ -69,6 +70,7 @@
"@types/react": "^19.2.0",
"@types/react-dom": "^19.2.0",
"@vitejs/plugin-react": "^5.0.4",
"acorn": "8.19.0",
"happy-dom": "^20.11.2",
"msw": "2.15.0",
"tailwindcss": "^4.3.3",
Expand Down
10 changes: 9 additions & 1 deletion app/src/components/audit/audit-cells.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,21 @@ export function WhenCell({ event }: { event: AuditEvent }) {
);
}

/**
* `text` cut after each `.`, `/`, `_` and `-`, each part keeping the seam it ends on. Matched rather
* than split with a lookbehind, which Safari reads only from 16.4 (app/tests/older-safari-build.test.ts).
*/
export function seamsOf(text: string): string[] {
return text.match(/[^./_-]*[./_-]|[^./_-]+/g) ?? [""];
}

/**
* An identifier with a break allowed after each `.`, `/`, `_` and `-`, so `mcp.tools_discovered` or
* a file path wraps at its own seams when the column is narrow, and is never cut mid-word.
*/
function breakable(text: string): ReactNode {
let at = 0;
return text.split(/(?<=[./_-])/).map((part) => {
return seamsOf(text).map((part) => {
const start = at;
at += part.length;
return (
Expand Down
23 changes: 20 additions & 3 deletions app/src/lib/boundaries/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,25 @@ function asTitle(name: string): string {
return /[^.]\.$/.test(trimmed) ? trimmed.slice(0, -1) : trimmed;
}

/** A sentence's end: ".", "!" or "?", the spaces after it, and a capital to come. */
const SENTENCE_END = /[.!?]\s+(?=[A-Z])/g;

/**
* `text` cut into sentences at each {@link SENTENCE_END}, each keeping its stop and losing the spaces
* after it. Found by matching the stop rather than looking behind for it, which Safari reads only
* from 16.4 (app/tests/older-safari-build.test.ts).
*/
export function sentencesOf(text: string): string[] {
const sentences: string[] = [];
let from = 0;
for (const end of text.matchAll(SENTENCE_END)) {
sentences.push(text.slice(from, end.index + 1));
from = end.index + end[0].length;
}
sentences.push(text.slice(from));
return sentences;
}

function wordsOf(rule: BoundaryRule): RuleLabel & { code: string | null } {
if (rule.name)
return {
Expand All @@ -227,9 +246,7 @@ function wordsOf(rule: BoundaryRule): RuleLabel & { code: string | null } {
// OpenBot's own reading of a rule nobody described: its first sentence, which says what it
// stops, is the row's name, and the rest is said under it.
if (rule.english && rule.englishFrom === "reading") {
const [first, ...rest] = rule.english
.trim()
.split(/(?<=[.!?])\s+(?=[A-Z])/);
const [first, ...rest] = sentencesOf(rule.english.trim());
return {
name: first ?? rule.english,
description: rest.join(" ") || rule.description || "",
Expand Down
66 changes: 66 additions & 0 deletions app/src/lib/markdown-email-links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* THE EMAIL ADDRESSES IN A BOT'S WORDS, FOUND WITHOUT A LOOKBEHIND.
*
* GitHub-flavoured Markdown links an address it finds in text (`dana@northwind.test`). Its
* mdast-util-gfm-autolink-literal finds one with
*
* /(?<=^|\s|\p{P}|\p{S})([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/gu
*
* on every reply, and Safari reads a lookbehind only from 16.4: in Safari 16.0 to 16.3, and in
* OpenBot Desktop on macOS before 13.3, every reply in Markdown and every document failed to draw.
* Its newer releases use more lookbehinds, not fewer. So the build gives it this instead
* (`lookbehindFreeEmailLinks` in app/vite.config.ts): a regular expression whose `exec` finds the same
* match from the same `lastIndex`, which is all mdast-util-find-and-replace asks of it. Leaving the
* lookbehind out alone would find the same addresses, but try every character of a long word
* (a key, a hash) to the word's end: seconds on a long one. This tries only where the lookbehind
* would, the start or after a space, punctuation or a symbol, as quickly.
*
* app/tests/older-safari-same-words.test.ts holds it to the original's matches.
*/

/** An address from where it starts, as the original matches it after its lookbehind. */
const ADDRESS = /([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/uy;
/** What an address may start after: the original's lookbehind, one character. */
const BOUNDARY = /^(?:\s|\p{P}|\p{S})$/u;
/** What an address starts with. */
const FIRST = /[-.\w+]/;

/** Whether the original's lookbehind holds at `at`: the start, or after a space, punctuation or a symbol. */
function boundaryBefore(text: string, at: number): boolean {
if (at === 0) return true;
// The character before, a whole code point, as a lookbehind in `u` mode reads it.
const low = text.charCodeAt(at - 1);
const pair =
at >= 2 &&
low >= 0xdc00 &&
low <= 0xdfff &&
text.charCodeAt(at - 2) >= 0xd800 &&
text.charCodeAt(at - 2) <= 0xdbff;
return BOUNDARY.test(text.slice(pair ? at - 2 : at - 1, at));
}

export class EmailAfterBoundary extends RegExp {
constructor() {
super(ADDRESS.source, "gu");
}

override exec(input: string): RegExpExecArray | null {
const text = String(input);
for (let at = this.lastIndex; at < text.length; at++) {
if (!FIRST.test(text.charAt(at)) || !boundaryBefore(text, at)) continue;
ADDRESS.lastIndex = at;
const match = ADDRESS.exec(text);
if (match) {
this.lastIndex = at + match[0].length;
return match;
}
}
this.lastIndex = 0;
return null;
}
}

/** The finder mdast-util-gfm-autolink-literal is built with in place of its lookbehind. */
export function emailAfterBoundary(): RegExp {
return new EmailAfterBoundary();
}
18 changes: 15 additions & 3 deletions app/src/lib/maths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ function lineAfter(text: string, index: number): string {
* A dollar sign inside a formula, as KaTeX reads one. Models write "\\(P=$25,000\\)" for a price
* in a formula; a bare `$` there would end the `$$` early or reach KaTeX as an error.
*/
function escapeDollars(formula: string): string {
return formula.replace(/(?<!\\)\$/g, "\\$$");
export function escapeDollars(formula: string): string {
// Each `$` not already after a backslash. Read off the formula rather than with a lookbehind,
// which Safari reads only from 16.4 (app/tests/older-safari-build.test.ts).
return formula.replace(/\$/g, (dollar: string, at: number) =>
formula[at - 1] === "\\" ? dollar : "\\$",
);
}

/**
Expand Down Expand Up @@ -150,10 +154,18 @@ function rewriteProse(text: string): string {
return out;
}

/**
* `markdown`'s lines, each with its line ending. Matched rather than split after each "\n" with a
* lookbehind, which Safari reads only from 16.4 (app/tests/older-safari-build.test.ts).
*/
export function linesOf(markdown: string): string[] {
return markdown.match(/[^\n]*\n|[^\n]+/g) ?? [""];
}

/** `markdown` with `\( \)` and `\[ \]` maths written in the `$$` the maths plugin reads. */
export function withMathDelimiters(markdown: string): string {
if (!markdown.includes("\\(") && !markdown.includes("\\[")) return markdown;
const lines = markdown.split(/(?<=\n)/);
const lines = linesOf(markdown);
let out = "";
let prose = "";
let fence: string | null = null;
Expand Down
72 changes: 61 additions & 11 deletions app/src/lib/plugins/tool-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,33 @@ export function toolName(serverId: string, name: string): string {
* id is not shown at all: a row with the tool's words and no sentence says less, but nothing wrong.
*/
export function toolSummary(description: string): string | null {
const first = description
.trim()
.split(/(?<=[.!?])\s+(?=[A-Z])|;\s/)[0]
?.trim();
const first = firstClause(description.trim()).trim();
if (!first || NAMES_AN_ID.test(first)) return null;
const plain = inPlainWords(first);
if (!plain) return null;
return /[.!?]$/.test(plain) ? plain : `${plain}.`;
}

/*
* NO LOOKBEHIND IN THIS FILE, NOR ANYWHERE THE APP SHIPS. Safari reads `(?<=` and `(?<!` only from
* 16.4, and the build is for 16.0: one here was compiled as its chunk loaded, and every Admin page
* drew "This page couldn't be shown" in Safari 16.0 to 16.3 and in OpenBot Desktop on macOS before
* 13.3. app/tests/older-safari-build.test.ts holds the built app to it.
*/

/** Where a description's first clause ends: a sentence's end, or a semicolon and a space. */
const CLAUSE_END = /[.!?]\s+(?=[A-Z])|;\s/;

/**
* The text up to the first sentence's end ("." "!" or "?", then spaces and a capital, the stop kept)
* or its first "; ", whichever comes first, or all of it.
*/
export function firstClause(text: string): string {
const end = CLAUSE_END.exec(text);
if (!end) return text;
return text.slice(0, end[0].startsWith(";") ? end.index : end.index + 1);
}

/**
* Text that names an id rather than says a thing: code in backticks, words joined by an underscore
* (`user_id`, `GMAIL_SEND_DRAFT`), a word that starts lower case and carries a capital
Expand All @@ -67,10 +84,9 @@ const NAMES_AN_ID =
function inPlainWords(sentence: string): string {
const bare = sentence.replace(/^_(.*)_(\.?)$/, "$1$2");
const command = COMMAND.test(bare);
const plain = bare
.replace(COMMAND, "")
.replace(/^tool that\s+/i, "")
.replace(BY_ITS_ID, withoutFindingById)
const plain = withoutFindingByIdIn(
bare.replace(COMMAND, "").replace(/^tool that\s+/i, ""),
)
.replace(/\b[A-Z]{2,}\b(:?)/g, (word: string, label: string) => {
const bare = word.slice(0, word.length - label.length);
if (!shouted(bare)) return word;
Expand Down Expand Up @@ -156,18 +172,52 @@ function saysIt(verb: string): string {
/**
* How a tool finds a thing by its ID: "by ID", "by its ID", "by thread ID", "using its ID", "by
* message and attachment IDs", and "identified by its ID" or "specified by its ID", with the commas
* around it. Never where it orders or filters rather than finds ("sorted by ID").
* around it. Never where it orders or filters rather than finds ("sorted by ID": {@link ORDERS_BY}).
* The spaces before "by" are a group of their own, so where "by" starts is known.
*/
const BY_ITS_ID =
/(,?\s+(?:identified|specified))?\s+(?<!\b(?:sorted|ordered|filterable|filtered|filtering)\s+)(?:by|using)\s+(?:its\s+|their\s+|the\s+)?(?:[a-z]+\s+(?:and\s+[a-z]+\s+)?)?IDs?\b(,)?/g;
/(,?\s+(?:identified|specified))?(\s+)(?:by|using)\s+(?:its\s+|their\s+|the\s+)?(?:[a-z]+\s+(?:and\s+[a-z]+\s+)?)?IDs?\b(,)?/g;

/** The text before a "by" or "using" that orders or filters rather than finds: "sorted by ID". */
const ORDERS_BY = /\b(?:sorted|ordered|filterable|filtered|filtering)\s+$/;

/**
* `sentence` with each {@link BY_ITS_ID} that finds a thing taken out ({@link withoutFindingById}).
*
* One that orders or filters is not a match at all, and the search goes on from the next character,
* just as a regular expression looks past a place its lookbehind refuses: the same matches, found
* without a lookbehind.
*/
export function withoutFindingByIdIn(sentence: string): string {
let out = "";
let from = 0;
BY_ITS_ID.lastIndex = 0;
for (
let match = BY_ITS_ID.exec(sentence);
match;
match = BY_ITS_ID.exec(sentence)
) {
const [found, named, spaces, comma] = match;
const by = match.index + (named?.length ?? 0) + (spaces?.length ?? 0);
if (ORDERS_BY.test(sentence.slice(0, by))) {
BY_ITS_ID.lastIndex = match.index + 1;
continue;
}
out +=
sentence.slice(from, match.index) +
withoutFindingById(found, named, comma, match.index, sentence);
from = match.index + found.length;
}
return out + sentence.slice(from);
}

/**
* One {@link BY_ITS_ID} taken out, or left where the ID is one of the things a tool finds by, which a
* person can give the others of: "by its ID or name", "by its channel ID and timestamp", "by their IDs
* (or a specified unique property)", "by ID, email, name, or other text". An "and" that goes on to
* what the tool does is not one of them: "by ID and removes it from messages".
*/
function withoutFindingById(
export function withoutFindingById(
found: string,
named: string | undefined,
comma: string | undefined,
Expand Down
28 changes: 25 additions & 3 deletions app/src/lib/tasks/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,27 @@ function onlySources(said: string): boolean {
/** A table's rule under its heading: `| --- | :--: |`. */
const TABLE_RULE = /^:?-{2,}:?$/;

/**
* A table row cut at each `|` that is not escaped (`\|`), read character by character rather than
* with a lookbehind, which Safari reads only from 16.4 (app/tests/older-safari-build.test.ts).
*/
export function cellsOf(row: string): string[] {
const cells: string[] = [];
let start = 0;
for (let at = 0; at < row.length; at++) {
if (row[at] !== "|" || row[at - 1] === "\\") continue;
cells.push(row.slice(start, at));
start = at + 1;
}
cells.push(row.slice(start));
return cells;
}

/** One row of a Markdown table as a sentence would say it: "Begins: Columbia Lake". */
function tableRow(line: string): string | null {
const row = line.trim();
if (!row.startsWith("|") && !row.endsWith("|")) return null;
const cells = row
.split(/(?<!\\)\|/)
const cells = cellsOf(row)
.map((cell) => cell.trim())
.filter(Boolean);
if (cells.length === 0 || cells.every((cell) => TABLE_RULE.test(cell)))
Expand All @@ -56,6 +71,13 @@ function tableRow(line: string): string | null {
return rest.length ? `${first}: ${rest.join(", ")}` : (first ?? "");
}

/**
* Underscores that close emphasis: after a character that is not a space, before the end or a
* character that is not a letter or a number. The character before is matched and put back, rather
* than looked behind at, which Safari reads only from 16.4 (app/tests/older-safari-build.test.ts).
*/
export const CLOSING_UNDERSCORES = /(\S)_+(?=$|[^\p{L}\p{N}])/gu;

/** Markdown's marks taken off one line, leaving the words. */
function withoutMarks(line: string): string {
return (
Expand All @@ -78,7 +100,7 @@ function withoutMarks(line: string): string {
.replace(/`+/g, "")
.replace(/\*+|~~/g, "")
.replace(/(^|[^\p{L}\p{N}])_+(?=\S)/gu, "$1")
.replace(/(?<=\S)_+(?=$|[^\p{L}\p{N}])/gu, "")
.replace(CLOSING_UNDERSCORES, "$1")
.replace(/\\([\\`*_{}[\]()#+\-.!|>~])/g, "$1")
.replace(/\s+/g, " ")
.trim()
Expand Down
Loading
Loading