import { z } from "zod";
import { all } from "../../db";
import { listRepos } from "../../api/repos";
import { getRepo } from "../../api/repos";
import { REPO_SELECT, visibleReposClause } from "../../repos";
import { credentialAllows } from "../../token-scope";
import { repoJson } from "../../api/http";
import { blameFile, BlameUnavailable } from "../../code/blame";
import { contentTerms, ftsQuery, searchOwnerCode, searchRepoCode } from "../../code/search";
import { ensureCodeIndex } from "../../code/search-index";
import { withStore } from "../../git/store";
import { notFoundError } from "../../errors";
import { limitHeavy } from "../../limits";
import type { RepoRow } from "~/lib/types";
import { apiCtx, apiJson, defineTool, fileUri, owner, repoArgs, scopedRepo, truncateText } from "./base";
import { listTreeAt, readFileAt, resolveRef } from "./content";
const SEARCH_LIMIT = 30;
const MAX_BLAME_LINES = 2000;
const MAX_INLINE_BASE64 = 256 * 1024;
const listReposTool = defineTool({
name: "list_repos",
title: "List repositories",
description: "List repositories of an owner (defaults to you) that this grant can read.",
input: z.object({ owner: owner.optional() }),
write: false,
async run({ caller }, args) {
const repos = await apiJson<unknown[]>(
listRepos(apiCtx(caller, { search: args.owner ? { owner: args.owner } : {} })),
);
return { structured: { repos } };
},
});
const searchReposTool = defineTool({
name: "search_repos",
title: "Search repositories",
description: "Search repositories you can read by name or description.",
input: z.object({ query: z.string().trim().min(1).max(100) }),