150 lines · 5.4 KB
import { z } from "zod";
import { repoDb } from "../../scoped-db";
import { accountOrGhost, accountsById } from "../../people";
import { notFoundError } from "../../errors";
import { issueUrl, loadIssue } from "../../issues";
import type { IssueRow } from "../../issues";
import { limitWrite } from "../../limits";
import { markdownBody, title } from "~/lib/validation";
import { changeIssueState, commentsWithAuthors, insertComment, insertIssue } from "../issue-writes";
import { attributionNote, defineTool, repoArgs, scopedRepo, truncateText } from "./base";

const LIST_LIMIT = 50;
const MAX_COMMENTS = 100;
const number = z.number().int().positive();

const listIssuesTool = defineTool({
  name: "list_issues",
  title: "List issues",
  description: "Issues (not pull requests) in a repository, newest first.",
  input: z.object({
    ...repoArgs,
    state: z.enum(["open", "closed", "all"]).default("open"),
  }),
  write: false,
  async run({ caller }, args) {
    const { repo } = await scopedRepo(caller, args.owner, args.repo, "issues", "read");
    const issues = await repoDb(repo.id).all<IssueRow>(
      `SELECT i.* FROM issues i
       WHERE i.repo_id = ? AND i.is_pull = 0 AND i.hidden_at IS NULL AND (? = 'all' OR i.state = ?)
       ORDER BY i.number DESC LIMIT ${LIST_LIMIT}`,
      repo.id,
      args.state,
      args.state,
    );
    const authors = await accountsById(issues.map((row) => row.author_id));
    const rows = issues.map((row) => ({
      ...row,
      author: accountOrGhost(authors, row.author_id).name,
    }));
    return {