109 lines · 4.3 KB
import { z } from "zod";
import { forbidden } from "../../errors";
import { limitWrite } from "../../limits";
import { hasPermission } from "../../repos";
import { scopedRepoPermission, TOKEN_AREAS } from "../../token-scope";
import type { TokenArea, TokenPermissions } from "../../token-scope";
import type { Permission } from "~/lib/types";
import { mintFineGrainedToken } from "../grants";
import { defineTool, repoArgs, scopedRepo, ToolError } from "./base";

const MAX_TOKEN_DAYS = 7;
const DAY_MS = 24 * 60 * 60 * 1000;
const CONFIRM_KEY = "confirm_token";

function needed(area: TokenArea, access: "read" | "write"): Permission {
  if (area === "admin") return access === "write" ? "admin" : "read";
  return access;
}

const createTokenTool = defineTool({
  name: "create_repo_token",
  title: "Create scoped repository token",
  description: `Mint a fine-grained token for one repository (e.g. for a sub-agent or CI), valid up to ${MAX_TOKEN_DAYS} days. It can never exceed this grant's own access; clients that support elicitation are asked to confirm.`,
  input: z.object({
    ...repoArgs,
    name: z.string().trim().min(1).max(80),
    permissions: z.object(Object.fromEntries(TOKEN_AREAS.map((area) => [area, z.enum(["read", "write"]).optional()]))),
    expires_in_days: z.number().int().min(1).max(MAX_TOKEN_DAYS).default(1),
  }),
  write: true,
  async run({ caller, inputResponses }, args) {
    await limitWrite(caller.viewer);
    const { repo } = await scopedRepo(caller, args.owner, args.repo, "contents", "read");
    const permissions: TokenPermissions = {};
    for (const area of TOKEN_AREAS) {
      const access = args.permissions[area];
      if (access !== "read" && access !== "write") continue;
      if (!hasPermission(await scopedRepoPermission(repo, caller.credential, area), needed(area, access))) {
        throw forbidden(`This grant cannot delegate ${area}:${access}`);
      }