TypeScript
fix(clients): # suggestions put linked pull requests first for text queries (#26269)
Julius Marmingefe55faf379c7
diff --git a/apps/mobile/src/state/queries.ts b/apps/mobile/src/state/queries.tsindex f382af62712..f923314b4cb 100644--- a/apps/mobile/src/state/queries.ts+++ b/apps/mobile/src/state/queries.ts@@ -1,6 +1,7 @@import {composerPullRequestsFromLinks,filterComposerPullRequestMatches,+ withLinkedComposerPullRequestsFirst,} from "@t3tools/shared/composerPullRequestMatches";import { resolveFilesystemReadAccess } from "@t3tools/client-runtime/state/filesystem";import { environmentSession } from "./session";@@ -148,14 +149,22 @@ export function useComposerPullRequestSearch(input: {: null,);const entries = (() => {- if (!ready) return [];- if (numeric) {+ if (input.query === null || input.projectId === null || input.repository === null) return [];+ if (/^\d*$/.test(input.query)) {return filterComposerPullRequestMatches({- entries: [...(exact.data ? [exact.data] : []), ...(list.data?.entries ?? [])],+ entries: ready ? [...(exact.data ? [exact.data] : []), ...(list.data?.entries ?? [])] : [],linked,- projectId: input.projectId!,- repository: input.repository!,- query: query ?? "",+ projectId: input.projectId,+ repository: input.repository,+ query: input.query,+ limit: 20,+ });+ }+ if (!ready) {+ return withLinkedComposerPullRequestsFirst({+ matches: [],+ linked,+ query: input.query,limit: 20,});}@@ -170,9 +179,14 @@ export function useComposerPullRequestSearch(input: {);const unique = new Map<number, (typeof found)[number]>();for (const entry of found) if (!unique.has(entry.number)) unique.set(entry.number, entry);- return [...unique.values()]- .sort((left, right) => right.updatedAt.localeCompare(left.updatedAt))- .slice(0, 20);+ return withLinkedComposerPullRequestsFirst({+ matches: [...unique.values()].sort((left, right) =>+ right.updatedAt.localeCompare(left.updatedAt),+ ),+ linked,+ query: query ?? "",+ limit: 20,+ });})();return {entries,diff --git a/apps/web/src/components/chat/useComposerMenuItems.ts b/apps/web/src/components/chat/useComposerMenuItems.tsindex d9e11c53226..6468b2f5762 100644--- a/apps/web/src/components/chat/useComposerMenuItems.ts+++ b/apps/web/src/components/chat/useComposerMenuItems.ts@@ -16,6 +16,7 @@ import type {import {composerPullRequestsFromLinks,filterComposerPullRequestMatches,+ withLinkedComposerPullRequestsFirst,} from "@t3tools/shared/composerPullRequestMatches";import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";@@ -303,24 +304,30 @@ export function useComposerMenuItems({query: composerTrigger.query,limit: COMPOSER_PULL_REQUEST_RESULT_LIMIT,})- : rankPullRequestMatches(- (pullRequestLookup.data?.entries ?? []).filter((entry) => {- if (- entry.projectId !== pullRequestProjectId ||- entry.repository.trim().toLowerCase() !== pullRequestRepository.trim().toLowerCase()- ) {- return false;- }- const provider = pullRequestLookup.data?.providers.find(- (candidate) => candidate.host === entry.host,- );- return (- provider?.searchesOnHost === true ||- matchesPullRequestQuery(entry, composerTrigger.query)- );- }),- composerTrigger.query,- ).slice(0, COMPOSER_PULL_REQUEST_RESULT_LIMIT);+ : withLinkedComposerPullRequestsFirst({+ matches: rankPullRequestMatches(+ (pullRequestLookup.data?.entries ?? []).filter((entry) => {+ if (+ entry.projectId !== pullRequestProjectId ||+ entry.repository.trim().toLowerCase() !==+ pullRequestRepository.trim().toLowerCase()+ ) {+ return false;+ }+ const provider = pullRequestLookup.data?.providers.find(+ (candidate) => candidate.host === entry.host,+ );+ return (+ provider?.searchesOnHost === true ||+ matchesPullRequestQuery(entry, composerTrigger.query)+ );+ }),+ composerTrigger.query,+ ),+ linked: linkedPullRequests,+ query: composerTrigger.query,+ limit: COMPOSER_PULL_REQUEST_RESULT_LIMIT,+ });return matches.map((pullRequest) => ({id: `pull-request:${pullRequest.projectId}:${pullRequest.repository}:${pullRequest.number}`,type: "pull-request",diff --git a/packages/shared/src/composerPullRequestMatches.test.ts b/packages/shared/src/composerPullRequestMatches.test.tsindex e87dc5d622f..19e96072189 100644--- a/packages/shared/src/composerPullRequestMatches.test.ts+++ b/packages/shared/src/composerPullRequestMatches.test.ts@@ -5,6 +5,7 @@ import type { ThreadPullRequestLink } from "@t3tools/contracts";import {composerPullRequestsFromLinks,filterComposerPullRequestMatches,+ withLinkedComposerPullRequestsFirst,} from "./composerPullRequestMatches.ts";const entry = (number: number, updatedAt: string) => ({@@ -78,4 +79,30 @@ describe("linked pull requests", () => {});expect(result).toEqual([entry(4, "2026-01-03")]);});++ it("puts linked pull requests ahead of a text search, before and after it returns", () => {+ const linked = composerPullRequestsFromLinks(+ [+ link("owner/repo", 7, {+ snapshot: { ...link("owner/repo", 7).snapshot!, title: "Fix login" },+ }),+ link("owner/repo", 8),+ ],+ "p1",+ );+ const searching = withLinkedComposerPullRequestsFirst({+ matches: [],+ linked,+ query: "fix",+ limit: 10,+ });+ expect(searching.map((match) => match.number)).toEqual([7]);+ const searched = withLinkedComposerPullRequestsFirst({+ matches: [entry(9, "2026-01-05"), entry(8, "2026-01-03")],+ linked,+ query: "fix",+ limit: 10,+ });+ expect(searched).toEqual([linked[0], entry(8, "2026-01-03"), entry(9, "2026-01-05")]);+ });});diff --git a/packages/shared/src/composerPullRequestMatches.ts b/packages/shared/src/composerPullRequestMatches.tsindex 6a586a8a66b..4be2248a48a 100644--- a/packages/shared/src/composerPullRequestMatches.ts+++ b/packages/shared/src/composerPullRequestMatches.ts@@ -73,3 +73,34 @@ export function filterComposerPullRequestMatches<).slice(0, input.limit);}++/** Puts the thread's linked pull requests that match a text query ahead of the searched ones. */+export function withLinkedComposerPullRequestsFirst<+ Entry extends ComposerPullRequestMatch,+ Linked extends ComposerPullRequestMatch & {+ readonly title: string;+ readonly headBranch: string;+ },+>(input: {+ readonly matches: ReadonlyArray<Entry>;+ readonly linked: ReadonlyArray<Linked>;+ readonly query: string;+ readonly limit: number;+}): ReadonlyArray<Entry | Linked> {+ const keyOf = (entry: ComposerPullRequestMatch) =>+ `${entry.repository.trim().toLowerCase()}#${entry.number}`;+ const matchesByKey = new Map(input.matches.map((entry) => [keyOf(entry), entry]));+ const words = input.query.toLowerCase().split(/\s+/u).filter(Boolean);+ const linked = input.linked.flatMap((entry): Array<Entry | Linked> => {+ const match = matchesByKey.get(keyOf(entry));+ if (match !== undefined) return [match];+ const text =+ `#${entry.number} ${entry.title} ${entry.repository} ${entry.headBranch}`.toLowerCase();+ return words.every((word) => text.includes(word)) ? [entry] : [];+ });+ const linkedKeys = new Set(linked.map(keyOf));+ return [...linked, ...input.matches.filter((entry) => !linkedKeys.has(keyOf(entry)))].slice(+ 0,+ input.limit,+ );+}