Registry Mirror
Skip to content

File tree

‎apps/server/src/orchestration-v2/ThreadSearch.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,59 @@ it.layer(layerTest)("ThreadSearch filters before it ranks", (it) => {
10711071
}),
10721072
);
10731073

1074+
it.effect("reads links with angle-bracketed addresses or titles as their text", () =>
1075+
Effect.gen(function* () {
1076+
const projections = yield* ProjectionStore.ProjectionStoreV2;
1077+
const search = yield* ThreadSearch.ThreadSearch;
1078+
const project = ProjectId.make("project:bittern");
1079+
const cited = ThreadId.make("thread:bittern-cited");
1080+
const titled = ThreadId.make("thread:bittern-titled");
1081+
const escaped = ThreadId.make("thread:bittern-escaped");
1082+
yield* createProject(project);
1083+
yield* Effect.forEach(
1084+
[
1085+
thread(cited, project),
1086+
message(
1087+
cited,
1088+
"bittern-cited",
1089+
"assistant",
1090+
"Edited [notes.md](</Users/me/Bittern Notes/notes.md>) and ![](</tmp/Bittern Shots/a.png>)",
1091+
),
1092+
thread(titled, project),
1093+
message(
1094+
titled,
1095+
"bittern-titled",
1096+
"user",
1097+
'Read [the guide](https://x.dev/a "Bittern guide")',
1098+
),
1099+
thread(escaped, project),
1100+
message(
1101+
escaped,
1102+
"bittern-escaped",
1103+
"assistant",
1104+
"Open [\\_\\_wader\\_\\_.py](</Users/me/Bittern Notes/__wader__.py>) now",
1105+
),
1106+
],
1107+
projections.apply,
1108+
{ discard: true },
1109+
);
1110+
// Each thread shows only the link text, so no address or title can be found.
1111+
assert.deepEqual((yield* search.search({ query: "bittern" })).matches, []);
1112+
assert.deepEqual(
1113+
(yield* search.search({ query: "notes.md" })).matches.map((match) => match.snippet),
1114+
["Edited notes.md and"],
1115+
);
1116+
assert.deepEqual(
1117+
(yield* search.search({ query: "guide" })).matches.map((match) => match.snippet),
1118+
["Read the guide"],
1119+
);
1120+
assert.deepEqual(
1121+
(yield* search.search({ query: "__wader__" })).matches.map((match) => match.snippet),
1122+
["Open __wader__.py now"],
1123+
);
1124+
}),
1125+
);
1126+
10741127
it.effect("reads at most the newest 10,000 index hits, however many the filters drop", () =>
10751128
Effect.gen(function* () {
10761129
const projections = yield* ProjectionStore.ProjectionStoreV2;

‎apps/server/src/orchestration-v2/ThreadSearch.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { threadSearchPullRequests } from "@t3tools/shared/threadPullRequests";
3737
import {
3838
foldedMessageSearchText,
3939
indexedMessageCondition,
40+
MARKDOWN_LINK,
4041
messageSearchText,
4142
nonPlaceholderMessageCondition,
4243
} from "./messageSearchIndex.ts";
@@ -193,11 +194,6 @@ function buildMessageSearch(
193194
};
194195
}
195196

196-
// A thread shows a link as its text, so in-thread find cannot reach a match
197-
// in the address; messages are matched as they read. A label may hold balanced
198-
// brackets and an address balanced or backslash-escaped parentheses, as CommonMark allows.
199-
const MARKDOWN_LINK = /!?\[((?:[^[\]\n]|\[[^[\]\n]*\])*)\]\((?:\\\S|[^()\s\\]|\([^()\s]*\))*\)/g;
200-
201197
// Result rows show one line, so the match must sit near the start to be seen.
202198
const SNIPPET_LEAD = 24;
203199

@@ -589,8 +585,9 @@ export const make = Effect.gen(function* () {
589585
const hitCount = (threadId: ThreadId) => hitCounts.get(threadId) ?? 1;
590586
const matchMessage = messageMatcher(input.query);
591587
for (const row of rows) {
592-
// Webhook URL tokens are withheld before matching, so a snippet cut inside the URL has none.
593-
const text = withoutWebhookUrlToken(row.text.replace(MARKDOWN_LINK, "$1"));
588+
// A thread shows a link as its text, so messages are matched as they read. Webhook URL
589+
// tokens are withheld before matching, so a snippet cut inside the URL has none.
590+
const text = withoutWebhookUrlToken(row.text.replace(MARKDOWN_LINK, "$2"));
594591
const match = matchMessage(text);
595592
if (match === null) continue;
596593
const hit = { row, text, ...match };

‎apps/server/src/orchestration-v2/messageSearchIndex.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ const readableText = (row: string) =>
5252
`COALESCE(${row}.search_text, json_extract(${row}.payload_json, '$.text'))`;
5353

5454
// A file mention stores `__init__.py` as `[\_\_init\_\_.py](...)`; the index
55-
// holds the label as it reads, so search can find the name as typed. Links are read as
56-
// ThreadSearch's MARKDOWN_LINK reads them: balanced brackets in a label, parentheses in an address.
57-
const LINK_LABEL = /(!?\[)((?:[^[\]\n]|\[[^[\]\n]*\])*)(\]\((?:\\\S|[^()\s\\]|\([^()\s]*\))*\))/g;
55+
// holds the label as it reads, so search can find the name as typed. As CommonMark allows, a label
56+
// may hold balanced brackets, an address balanced or escaped parentheses, or spaces inside <...>
57+
// (as file citations and image embeds write it), and either address may carry a "title".
58+
export const MARKDOWN_LINK =
59+
/(!?\[)((?:[^[\]\n]|\[[^[\]\n]*\])*)(\]\((?:<(?:\\.|[^<>\\\n])*>|(?:\\\S|[^()\s\\]|\([^()\s]*\))*)(?:[ \t]+(?:"[^"\n]*"|'[^'\n]*'))?[ \t]*\))/g;
5860

5961
/**
6062
* What search_text holds for a message: its text as it reads, a quote's
@@ -63,7 +65,7 @@ const LINK_LABEL = /(!?\[)((?:[^[\]\n]|\[[^[\]\n]*\])*)(\]\((?:\\\S|[^()\s\\]|\(
6365
*/
6466
export const readableSearchText = (text: string) => {
6567
const readable = assistantCitationsToPlainText(text).replace(
66-
LINK_LABEL,
68+
MARKDOWN_LINK,
6769
(link, _open: string, label: string) => {
6870
const decoded = label.replace(
6971
/\\([\\*_~&])|&#9(1|3);/g,

0 commit comments

Comments
 (0)