Public: anyone can read this; the crew can change it.
Search understands repo:owner/name in its words (#16463) #16474
Check · Review · Land · Live done — Landed and live in production.
voice/search-repo → main 0 commits 1 thread · 1 unresolved
4 files
changed +37
−5
src/core/search.ts +14
| @@ -79,6 +79,20 @@ export function searchKind(raw: string | null): SearchKind { | ||
| 79 | 79 | return raw === "repos" || raw === "issues" || raw === "code" ? raw : "all"; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | /** | |
| 83 | * A `repo:owner/name` (or `repo:name`) written into a query, as a person or a voice client | |
| 84 | * writes it: taken out of the words and named apart, so the search keeps to that repository | |
| 85 | * instead of looking for the text "repo:". Undefined `repo` when the query names none. | |
| 86 | */ | |
| 87 | export function repoQualifier(q: string): { readonly q: string; readonly repo?: string } { | |
| 88 | let repo: string | undefined; | |
| 89 | const rest = q.replace(/(^|\s)repo:(\S+)/gi, (_match, lead: string, named: string) => { | |
| 90 | repo ??= named; | |
| 91 | return lead; | |
| 92 | }); | |
| 93 | return repo === undefined ? { q } : { q: rest.replace(/\s+/g, " ").trim(), repo }; | |
| 94 | } | |
| 95 | ||
| 82 | 96 | /** |
| 83 | 97 | * What a caller who asked wrongly is told, in one sentence, or undefined when |
| 84 | 98 | * the request was fine (an unknown kind or limit is `searchRefusal`'s 422, not a note). Plain text: |
src/http/mcp/reads.ts +10 −4
| @@ -9,7 +9,7 @@ import { getIssue, type Issue } from "../../core/issues.ts"; | ||
| 9 | 9 | import { markReaders } from "../../crew/direct.ts"; |
| 10 | 10 | import { SEARCH_ROWS as CARD_HITS } from "../../web/studio/cards.ts"; |
| 11 | 11 | import { search, searchRefusal } from "../../actions/search.ts"; |
| 12 | import { CODE_BUDGET_MS, searchKind, searchNote, type SearchResults } from "../../core/search.ts"; | |
| 12 | import { CODE_BUDGET_MS, repoQualifier, searchKind, searchNote, type SearchResults } from "../../core/search.ts"; | |
| 13 | 13 | import { explorePage, exploreLine, exploreCount, exploreFiltered, exploreLineFrom, exploreNote, exploreSuggestion, readExploreLine, rowsWithWhy } from "../../actions/explore.ts"; |
| 14 | 14 | import { forkTreeRows } from "../../actions/fork_tree_rows.ts"; |
| 15 | 15 | import type { User } from "../../core/users.ts"; |
| @@ -45,9 +45,15 @@ export async function exploreRun(ctx: Ctx, user: User | undefined, args: Args): | ||
| 45 | 45 | }); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | export async function searchRun(ctx: Ctx, user: User | undefined, args: Args): Promise<Answer> { | |
| 49 | const q = args["q"]; | |
| 50 | if (typeof q !== "string" || queryTooLong(q)) return bad("invalid", `\`q\` is a string of at most ${MAX_QUERY_CHARS} characters`); | |
| 48 | export async function searchRun(ctx: Ctx, user: User | undefined, given: Args): Promise<Answer> { | |
| 49 | const raw = given["q"]; | |
| 50 | if (typeof raw !== "string" || queryTooLong(raw)) return bad("invalid", `\`q\` is a string of at most ${MAX_QUERY_CHARS} characters`); | |
| 51 | // `repo:owner/name` in the words is the `repo` filter, as a voice client writes it. | |
| 52 | const qualified = repoQualifier(raw); | |
| 53 | const q = qualified.q; | |
| 54 | if (qualified.repo !== undefined && given["repo"] !== undefined && given["repo"] !== qualified.repo) | |
| 55 | return bad("invalid", "`q` names one repository with repo: and `repo` another: give one"); | |
| 56 | const args: Args = qualified.repo === undefined ? given : { ...given, repo: qualified.repo }; | |
| 51 | 57 | const kind = args["kind"]; |
| 52 | 58 | if (kind !== undefined && typeof kind !== "string") return bad("invalid_kind", "kind is all, repos, issues or code"); |
| 53 | 59 | const refusal = searchRefusal(kind, args["limit"] === undefined || typeof args["limit"] === "number" ? (args["limit"] as number | undefined) : NaN); |
src/http/mcp/tools/search.ts +1 −1
| @@ -4,7 +4,7 @@ import { MAX_QUERY_CHARS } from "../../../core/query_limit.ts"; | ||
| 4 | 4 | |
| 5 | 5 | export const tool: Tool = { |
| 6 | 6 | name: "search", |
| 7 | doc: "`{ q, kind?, limit?, repo?, all? }`: guides (`docs`, kind all), issues, pulls, repositories and code you can read, the same results as /search, drawn as the `ui://forge/search` card; `repo` keeps to one; `moreCode` says the code grep stopped at its budget, `all` greps the rest", | |
| 7 | doc: "`{ q, kind?, limit?, repo?, all? }`: guides (`docs`, kind all), issues, pulls, repositories and code you can read, the same results as /search, drawn as the `ui://forge/search` card; `repo` (or `repo:owner/name` in `q`) keeps to one; `moreCode` says the code grep stopped at its budget, `all` greps the rest", | |
| 8 | 8 | description: |
| 9 | 9 | "Find issues, pulls, repositories and code you can read; `kind`, `repo` narrow.", |
| 10 | 10 | inputSchema: { |
test/mcp-search.test.ts +12
| @@ -110,6 +110,18 @@ describe("MCP search", () => { | ||
| 110 | 110 | expect(scoped.value.issues.map((i: any) => i.repo.name)).toEqual(["pebble"]); |
| 111 | 111 | }); |
| 112 | 112 | |
| 113 | test("repo: written into q is the same scope (#16463), out of the words; a second, different repo is refused", async () => { | |
| 114 | const said = await tool(aliceRo, { q: "glow repo:alice/pebble", kind: "issues" }); | |
| 115 | expect(said.value.issues.map((i: any) => i.repo.name)).toEqual(["pebble"]); | |
| 116 | // First or last in the words, any case of the qualifier. | |
| 117 | const first = await tool(aliceRo, { q: "REPO:alice/pebble glow", kind: "issues" }); | |
| 118 | expect(first.value.issues.map((i: any) => i.repo.name)).toEqual(["pebble"]); | |
| 119 | expect((await tool(aliceRo, { q: "glow repo:alice/pebble", repo: "alice/pebble", kind: "issues" })).value.issues.length).toBe(1); | |
| 120 | expect((await tool(aliceRo, { q: "glow repo:alice/pebble", repo: "alice/noisy" })).error).toContain("give one"); | |
| 121 | // A private one the caller cannot read is not found, as through `repo`. | |
| 122 | expect((await tool(bobRo, { q: "glow repo:alice/secretpebble" })).error).toContain("no such app or repository"); | |
| 123 | }); | |
| 124 | ||
| 113 | 125 | test("an unknown kind or a bad limit is refused in the API's words; every kind answers", async () => { |
| 114 | 126 | expect((await tool(aliceRo, { q: "a", kind: "bogus" })).error).toContain('argument "kind" is "bogus"'); |
| 115 | 127 | expect((await tool(aliceRo, { q: "a", kind: "bogus" })).error).toContain('one of "all", "issues", "repos", "code"'); |
new above
offline
The new function sits between searchNote's doc comment (above, ending at line 85) and searchNote itself, so that comment now documents
repoQualifier. MoverepoQualifierand its comment above searchNote's comment.